| 1 |
/* Copyright (C) 2008 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include "config.h" |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#if defined WIN32 || defined _WIN32 || defined MSDOS || defined __CYGWIN__ || defined __EMX__ || defined OS2 |
| 17 |
#include <io.h> |
| 18 |
#include <fcntl.h> |
| 19 |
#endif |
| 20 |
#if defined WIN32 || defined _WIN32 |
| 21 |
#include <process.h> |
| 22 |
#endif |
| 23 |
#ifdef HAVE_TIME_H |
| 24 |
#include <time.h> |
| 25 |
#endif |
| 26 |
#include <stdio.h> |
| 27 |
#ifdef HAVE_STDLIB_H |
| 28 |
#include <stdlib.h> |
| 29 |
#endif |
| 30 |
#ifdef HAVE_STRING_H |
| 31 |
#include <string.h> |
| 32 |
#endif |
| 33 |
#include <errno.h> |
| 34 |
#include <ogg/ogg.h> |
| 35 |
#include "kate/oggkate.h" |
| 36 |
#include "katedesc.h" |
| 37 |
|
| 38 |
extern int katedesc_parse(void); |
| 39 |
extern void katedesc_restart(FILE*); |
| 40 |
extern FILE *katedesc_in; |
| 41 |
extern FILE *katedesc_out; |
| 42 |
extern int nerrors; |
| 43 |
|
| 44 |
static ogg_stream_state os; |
| 45 |
kate_state k; |
| 46 |
kate_info ki; |
| 47 |
kate_comment kc; |
| 48 |
static ogg_int64_t packetno; |
| 49 |
char base_path[4096]=""; |
| 50 |
|
| 51 |
static char str[4096]; |
| 52 |
static int headers_written=0; |
| 53 |
#ifdef DEBUG |
| 54 |
static int raw=0; |
| 55 |
#endif |
| 56 |
static kate_float repeat_threshold=(kate_float)0; |
| 57 |
static kate_float keepalive_threshold=(kate_float)0; |
| 58 |
static kate_int64_t last_stream_time=0; |
| 59 |
static const unsigned char utf8bom[3]={0xef,0xbb,0xbf}; |
| 60 |
static const unsigned char utf16lebom[2]={0xff,0xfe}; |
| 61 |
static const unsigned char utf16bebom[2]={0xfe,0xff}; |
| 62 |
static const unsigned char utf32lebom[4]={0xff,0xfe,0x00,0x00}; |
| 63 |
static const unsigned char utf32bebom[4]={0x00,0x00,0xfe,0xff}; |
| 64 |
|
| 65 |
static const char *language=NULL; |
| 66 |
static const char *category=NULL; |
| 67 |
|
| 68 |
static int flush_page(FILE *f) |
| 69 |
{ |
| 70 |
ogg_page og; |
| 71 |
while (1) { |
| 72 |
int ret=ogg_stream_flush(&os,&og); |
| 73 |
if (ret==0) break; |
| 74 |
ret=fwrite(og.header,1,og.header_len,f); |
| 75 |
if (ret!=og.header_len) { |
| 76 |
fprintf(stderr,"Write failed (%s)\n",strerror(errno)); |
| 77 |
return -1; |
| 78 |
} |
| 79 |
ret=fwrite(og.body,1,og.body_len,f); |
| 80 |
if (ret!=og.body_len) { |
| 81 |
fprintf(stderr,"Write failed (%s)\n",strerror(errno)); |
| 82 |
return -1; |
| 83 |
} |
| 84 |
fflush(f); |
| 85 |
} |
| 86 |
return 0; |
| 87 |
} |
| 88 |
|
| 89 |
static int poll_page(FILE *f) |
| 90 |
{ |
| 91 |
/* discontinuous codec, so we want to flush pages every time, |
| 92 |
as Ogg keeps granules per page, not per packet */ |
| 93 |
return flush_page(f); |
| 94 |
} |
| 95 |
|
| 96 |
int write_headers(FILE *f) |
| 97 |
{ |
| 98 |
ogg_packet op; |
| 99 |
|
| 100 |
/* command line overrides */ |
| 101 |
if (language) { |
| 102 |
int ret=kate_info_set_language(&ki,language); |
| 103 |
if (ret<0) { |
| 104 |
fprintf(stderr,"Failed to set stream language to '%s': %d\n",language,ret); |
| 105 |
return ret; |
| 106 |
} |
| 107 |
} |
| 108 |
if (category) { |
| 109 |
int ret=kate_info_set_category(&ki,category); |
| 110 |
if (ret<0) { |
| 111 |
fprintf(stderr,"Failed to set stream category to '%s': %d\n",category,ret); |
| 112 |
return ret; |
| 113 |
} |
| 114 |
} |
| 115 |
|
| 116 |
/* warn if either language or category is missing */ |
| 117 |
if (!ki.category || !*ki.category) { |
| 118 |
fprintf(stderr,"warning: no category defined\n"); |
| 119 |
} |
| 120 |
if (!ki.language || !*ki.language) { |
| 121 |
fprintf(stderr,"warning: no language defined\n"); |
| 122 |
} |
| 123 |
|
| 124 |
while (1) { |
| 125 |
int ret=kate_ogg_encode_headers(&k,&kc,&op); |
| 126 |
if (ret<0) { |
| 127 |
fprintf(stderr,"error encoding headers: %d\n",ret); |
| 128 |
return ret; |
| 129 |
} |
| 130 |
if (ret>0) break; /* we're done */ |
| 131 |
|
| 132 |
#ifdef DEBUG |
| 133 |
if (raw) { |
| 134 |
int ret=send_packet(f,&op,-1); |
| 135 |
if (ret<0) { |
| 136 |
fprintf(stderr,"error sending packet: %d\n",ret); |
| 137 |
return ret; |
| 138 |
} |
| 139 |
} |
| 140 |
else |
| 141 |
#endif |
| 142 |
{ |
| 143 |
ogg_stream_packetin(&os,&op); |
| 144 |
ogg_packet_clear(&op); |
| 145 |
} |
| 146 |
|
| 147 |
/* place all headers on the same page, it keeps oggmerge happy */ |
| 148 |
/* poll_page(f); */ |
| 149 |
} |
| 150 |
|
| 151 |
return flush_page(f); |
| 152 |
} |
| 153 |
|
| 154 |
int send_packet(FILE *f,ogg_packet *op,kate_int64_t t) |
| 155 |
{ |
| 156 |
if (t>last_stream_time) |
| 157 |
last_stream_time=t; |
| 158 |
|
| 159 |
#ifdef DEBUG |
| 160 |
if (raw) { |
| 161 |
int ret; |
| 162 |
if (op->packetno>0) { |
| 163 |
ogg_int64_t bytes=op->bytes; |
| 164 |
ret=fwrite(&bytes,1,8,f); |
| 165 |
if (ret!=8) { |
| 166 |
fprintf(stderr,"Write failed (%s)\n",strerror(errno)); |
| 167 |
return -1; |
| 168 |
} |
| 169 |
} |
| 170 |
ret=fwrite(op->packet,1,op->bytes,f); |
| 171 |
if (ret!=op->bytes) { |
| 172 |
fprintf(stderr,"Write failed (%s)\n",strerror(errno)); |
| 173 |
return -1; |
| 174 |
} |
| 175 |
fflush(f); |
| 176 |
return 0; |
| 177 |
} |
| 178 |
else |
| 179 |
#endif |
| 180 |
{ |
| 181 |
ogg_stream_packetin(&os,op); |
| 182 |
ogg_packet_clear(op); |
| 183 |
return poll_page(f); |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
void cancel_packet(ogg_packet *op) |
| 188 |
{ |
| 189 |
ogg_packet_clear(op); |
| 190 |
} |
| 191 |
|
| 192 |
static int flush_headers(FILE *f) |
| 193 |
{ |
| 194 |
int ret=0; |
| 195 |
if (!headers_written) { |
| 196 |
ret=write_headers(f); |
| 197 |
headers_written=1; |
| 198 |
} |
| 199 |
return ret; |
| 200 |
} |
| 201 |
|
| 202 |
static const char *eat_arg(int argc,char **argv,int *n) |
| 203 |
{ |
| 204 |
if (*n==argc-1) { |
| 205 |
fprintf(stderr,"%s needs an argument\n",argv[*n]); |
| 206 |
exit(-1); |
| 207 |
} |
| 208 |
return argv[++*n]; |
| 209 |
} |
| 210 |
|
| 211 |
static kate_int64_t hmsms2ms(int h,int m,int s,int ms) |
| 212 |
{ |
| 213 |
return h*(kate_int64_t)3600000+m*60000+s*1000+ms; |
| 214 |
} |
| 215 |
|
| 216 |
static char *fgets2(char *s,size_t len,FILE *f,int ignore_hash) |
| 217 |
{ |
| 218 |
char *ret=fgets(s,len,f); |
| 219 |
if (ret) { |
| 220 |
char *ptr=strpbrk(s,"\r"); /* fix windows text files */ |
| 221 |
if (ptr) strcpy(ptr,"\n"); |
| 222 |
if (!ignore_hash) { |
| 223 |
ptr=strchr(s,'#'); |
| 224 |
if (ptr) strcpy(ptr,"\n"); |
| 225 |
} |
| 226 |
} |
| 227 |
else *s=0; |
| 228 |
return ret; |
| 229 |
} |
| 230 |
|
| 231 |
static int convert_kate(FILE *fin,FILE *fout) |
| 232 |
{ |
| 233 |
katedesc_in=fin; |
| 234 |
katedesc_out=fout; |
| 235 |
katedesc_restart(katedesc_in); |
| 236 |
nerrors=0; |
| 237 |
katedesc_parse(); |
| 238 |
cleanup_lexer(); |
| 239 |
if (nerrors>0) return -1; |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
static int is_line_empty(const char *s) |
| 244 |
{ |
| 245 |
while (*s) { |
| 246 |
if (!strchr(" \t\r\n",*s)) return 0; |
| 247 |
++s; |
| 248 |
} |
| 249 |
return 1; |
| 250 |
} |
| 251 |
|
| 252 |
void update_stream_time(kate_state *k,FILE *fout,kate_int64_t endt) |
| 253 |
{ |
| 254 |
ogg_packet op; |
| 255 |
kate_int64_t t; |
| 256 |
int ret; |
| 257 |
kate_int64_t step; |
| 258 |
kate_int64_t raw_repeat_threshold=kate_duration_granule(k->ki,repeat_threshold); |
| 259 |
kate_int64_t raw_keepalive_threshold=kate_duration_granule(k->ki,keepalive_threshold); |
| 260 |
|
| 261 |
/* work out the best step to walk the time segment */ |
| 262 |
step=0; |
| 263 |
if (step==0 || (raw_repeat_threshold>0 && raw_repeat_threshold<step)) |
| 264 |
step=raw_repeat_threshold; |
| 265 |
if (step==0 || (raw_keepalive_threshold>0 && raw_keepalive_threshold<step)) |
| 266 |
step=raw_keepalive_threshold; |
| 267 |
|
| 268 |
if (step<=0) return; |
| 269 |
|
| 270 |
for (t=last_stream_time;t<endt;t+=step) { |
| 271 |
/* try repeats first, no keepalives will be needed if we have one */ |
| 272 |
if (raw_repeat_threshold>0) { |
| 273 |
while (1) { |
| 274 |
ret=kate_ogg_encode_repeat_raw_times(k,t,raw_repeat_threshold,&op); |
| 275 |
if (ret<0) { |
| 276 |
fprintf(stderr,"Failed encoding repeat at %"PRId64" (%d), continuing anyway\n",t,ret); |
| 277 |
return; |
| 278 |
} |
| 279 |
if (ret==0) break; |
| 280 |
ret=send_packet(fout,&op,t); |
| 281 |
if (ret<0) { |
| 282 |
fprintf(stderr,"Failed sending repeat packet, continuing anyway\n"); |
| 283 |
return; |
| 284 |
} |
| 285 |
} |
| 286 |
} |
| 287 |
/* keepalives next */ |
| 288 |
if (raw_keepalive_threshold>0 && t-last_stream_time>=raw_keepalive_threshold) { |
| 289 |
ret=kate_ogg_encode_keepalive_raw_times(k,t,&op); |
| 290 |
if (ret<0) { |
| 291 |
fprintf(stderr,"Failed encoding keepalive at %"PRId64" (%d), continuing anyway\n",t,ret); |
| 292 |
return; |
| 293 |
} |
| 294 |
ret=send_packet(fout,&op,t); |
| 295 |
if (ret<0) { |
| 296 |
fprintf(stderr,"Failed sending keepalive packet, continuing anyway\n"); |
| 297 |
return; |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
static void remove_last_newline(char *text) |
| 304 |
{ |
| 305 |
size_t len; |
| 306 |
|
| 307 |
len=strlen(text); |
| 308 |
if (len>0 && text[len-1]=='\n') text[--len]=0; |
| 309 |
} |
| 310 |
|
| 311 |
static int emit_srt_event(FILE *fout,kate_int64_t t0,kate_int64_t t1,const char *text,int allow_srt_markup,int x1,int y1,int x2,int y2) |
| 312 |
{ |
| 313 |
ogg_packet op; |
| 314 |
size_t len; |
| 315 |
int ret; |
| 316 |
|
| 317 |
update_stream_time(&k,fout,t0); |
| 318 |
|
| 319 |
len=strlen(text); |
| 320 |
ret=kate_text_validate(kate_utf8,text,len+1); |
| 321 |
if (ret<0) { |
| 322 |
if (ret==KATE_E_TEXT) { |
| 323 |
fprintf(stderr,"Text is not valid UTF-8: %s\n",text); |
| 324 |
} |
| 325 |
else { |
| 326 |
fprintf(stderr,"Failed to validate text: %d\n",ret); |
| 327 |
} |
| 328 |
return ret; |
| 329 |
} |
| 330 |
if (allow_srt_markup) kate_encode_set_markup_type(&k,kate_markup_simple); |
| 331 |
if (x1!=-INT_MAX && y1!=-INT_MAX && x2!=-INT_MAX && y2!=-INT_MAX) { |
| 332 |
kate_region kr; |
| 333 |
kate_region_init(&kr); |
| 334 |
kr.metric=kate_pixel; |
| 335 |
kr.x=x1; |
| 336 |
kr.y=y1; |
| 337 |
kr.w=x2-x1+1; |
| 338 |
kr.h=y2-y1+1; |
| 339 |
ret=kate_encode_set_region(&k,&kr); |
| 340 |
if (ret<0) { |
| 341 |
fprintf(stderr,"Error setting region: %d\n",ret); |
| 342 |
return ret; |
| 343 |
} |
| 344 |
} |
| 345 |
ret=kate_ogg_encode_text_raw_times(&k,t0,t1,text,len,&op); |
| 346 |
if (ret<0) { |
| 347 |
fprintf(stderr,"Error encoding text: %d\n",ret); |
| 348 |
return ret; |
| 349 |
} |
| 350 |
else { |
| 351 |
ret=send_packet(fout,&op,t0); |
| 352 |
if (ret<0) return ret; |
| 353 |
} |
| 354 |
return ret; |
| 355 |
} |
| 356 |
|
| 357 |
static int convert_srt(FILE *fin,FILE *fout,int allow_srt_markup) |
| 358 |
{ |
| 359 |
enum { need_id, need_timing, need_text }; |
| 360 |
int need = need_id; |
| 361 |
int last_seen_id=0; |
| 362 |
int ret; |
| 363 |
int id; |
| 364 |
static char text[4096]; |
| 365 |
int h0,m0,s0,ms0,h1,m1,s1,ms1; |
| 366 |
int x1,y1,x2,y2; |
| 367 |
kate_int64_t t0=0,t1=0; |
| 368 |
int line=0; |
| 369 |
|
| 370 |
ret=flush_headers(fout); |
| 371 |
if (ret<0) return ret; |
| 372 |
|
| 373 |
fgets2(str,sizeof(str),fin,1); |
| 374 |
|
| 375 |
if (!memcmp(str,utf16lebom,sizeof(utf16lebom)) || !memcmp(str,utf16bebom,sizeof(utf16bebom))) { |
| 376 |
fprintf(stderr,"This file seems to be encoded in UTF-16, Kate only supports UTF-8\n"); |
| 377 |
fprintf(stderr,"You will need to convert it to UTF-8 first (eg, use iconv)\n"); |
| 378 |
return -1; |
| 379 |
} |
| 380 |
|
| 381 |
if (!memcmp(str,utf32lebom,sizeof(utf32lebom)) || !memcmp(str,utf32bebom,sizeof(utf32bebom))) { |
| 382 |
fprintf(stderr,"This file seems to be encoded in UTF-32, Kate only supports UTF-8\n"); |
| 383 |
fprintf(stderr,"You will need to convert it to UTF-8 first (eg, use iconv)\n"); |
| 384 |
return -1; |
| 385 |
} |
| 386 |
|
| 387 |
/* kill any utf-8 BOM present */ |
| 388 |
if (!memcmp(str,utf8bom,sizeof(utf8bom))) { |
| 389 |
/* printf("UTF-8 BOM found at start, skipped\n"); */ |
| 390 |
memmove(str,str+3,strlen(str+3)+1); |
| 391 |
} |
| 392 |
|
| 393 |
while (!feof(fin) || *str) { |
| 394 |
++line; |
| 395 |
switch (need) { |
| 396 |
case need_id: |
| 397 |
/* allow more than one blank line between events */ |
| 398 |
if (is_line_empty(str)) { |
| 399 |
break; |
| 400 |
} |
| 401 |
ret=sscanf(str,"%d\n",&id); |
| 402 |
if (ret!=1) { |
| 403 |
fprintf(stderr,"Syntax error at line %d: %s\n",line,str); |
| 404 |
return -1; |
| 405 |
} |
| 406 |
if (id!=last_seen_id+1) { |
| 407 |
fprintf(stderr,"Warning at line %d: non consecutive ids: %s\n",line,str); |
| 408 |
} |
| 409 |
last_seen_id=id; |
| 410 |
need=need_timing; |
| 411 |
strcpy(text,""); |
| 412 |
break; |
| 413 |
case need_timing: |
| 414 |
ret=sscanf(str,"%d:%d:%d%*[,.]%d --> %d:%d:%d%*[,.]%d %*[xX]1: %d %*[xX]2: %d %*[yY]1: %d %*[yY]2: %d\n",&h0,&m0,&s0,&ms0,&h1,&m1,&s1,&ms1,&x1,&x2,&y1,&y2); |
| 415 |
if (ret!=12) { |
| 416 |
x1=y1=x2=y2=-INT_MAX; |
| 417 |
ret=sscanf(str,"%d:%d:%d%*[,.]%d --> %d:%d:%d%*[,.]%d\n",&h0,&m0,&s0,&ms0,&h1,&m1,&s1,&ms1); |
| 418 |
if (ret!=8) { |
| 419 |
fprintf(stderr,"Syntax error at line %d: %s\n",line,str); |
| 420 |
return -1; |
| 421 |
} |
| 422 |
} |
| 423 |
|
| 424 |
if ((h0|m0|s0|ms0)<0 || (h1|m1|s1|ms1)<0) { |
| 425 |
fprintf(stderr,"Bad time specification at line %d: %s\n",line,str); |
| 426 |
return -1; |
| 427 |
} |
| 428 |
t0=hmsms2ms(h0,m0,s0,ms0); |
| 429 |
t1=hmsms2ms(h1,m1,s1,ms1); |
| 430 |
if (t1<t0) { |
| 431 |
fprintf(stderr,"Error at line %d: end time must not be less than start time\n",line); |
| 432 |
return -1; |
| 433 |
} |
| 434 |
if (x2<x1 || y2<y1) { |
| 435 |
fprintf(stderr,"Invalid region coordinates at line %d\n",line); |
| 436 |
return -1; |
| 437 |
} |
| 438 |
need=need_text; |
| 439 |
break; |
| 440 |
case need_text: |
| 441 |
if (is_line_empty(str)) { |
| 442 |
remove_last_newline(text); |
| 443 |
ret=emit_srt_event(fout,t0,t1,text,allow_srt_markup,x1,y1,x2,y2); |
| 444 |
if (ret<0) return ret; |
| 445 |
need=need_id; |
| 446 |
} |
| 447 |
else { |
| 448 |
size_t len=strlen(text); |
| 449 |
strncpy(text+len,str,sizeof(text)-len); |
| 450 |
text[sizeof(text)-1]=0; |
| 451 |
} |
| 452 |
break; |
| 453 |
} |
| 454 |
fgets2(str,sizeof(str),fin,1); |
| 455 |
} |
| 456 |
|
| 457 |
/* add any text we've accumulated if there's no empty line at the end */ |
| 458 |
if (need==need_text && text[0]) { |
| 459 |
fprintf(stderr, "Warning: last event is not followed by an empty line - input might be truncated\n"); |
| 460 |
remove_last_newline(text); |
| 461 |
ret=emit_srt_event(fout,t0,t1,text,allow_srt_markup,x1,y1,x2,y2); |
| 462 |
if (ret<0) return ret; |
| 463 |
} |
| 464 |
|
| 465 |
return 0; |
| 466 |
} |
| 467 |
|
| 468 |
static void add_kate_karaoke_tag(kate_motion *km,kate_int64_t dt,const char *str,size_t len,int line) |
| 469 |
{ |
| 470 |
kate_curve *kc; |
| 471 |
kate_int64_t ptr=-500; /* minus half a second, in milliseconds */ |
| 472 |
int ret; |
| 473 |
kate_curve **new_curves; |
| 474 |
kate_float *new_durations; |
| 475 |
|
| 476 |
if (dt<0) { |
| 477 |
fprintf(stderr, "Error: line %d: lyrics times must not be decreasing\n",line); |
| 478 |
return; |
| 479 |
} |
| 480 |
|
| 481 |
/* work out how many glyphs we have */ |
| 482 |
while (len>0) { |
| 483 |
ret=kate_text_get_character(kate_utf8,&str,&len); |
| 484 |
if (ret<0) { |
| 485 |
fprintf(stderr, "Error: line %d: failed to get UTF-8 glyph from string\n",line); |
| 486 |
return; |
| 487 |
} |
| 488 |
ptr+=1000; /* a second, in milliseconds */ |
| 489 |
} |
| 490 |
/* ptr now points to the middle of the glyph we're at */ |
| 491 |
|
| 492 |
kc=(kate_curve*)kate_malloc(sizeof(kate_curve)); |
| 493 |
if (kc) { |
| 494 |
kate_curve_init(kc); |
| 495 |
kc->type=kate_curve_static; |
| 496 |
kc->npts=1; |
| 497 |
kc->pts=(kate_float*)kate_malloc(2*sizeof(kate_float)); |
| 498 |
if (kc->pts) { |
| 499 |
kc->pts[0]=ptr/(kate_float)1000; |
| 500 |
kc->pts[1]=(kate_float)0; |
| 501 |
|
| 502 |
new_curves=(kate_curve**)kate_realloc(km->curves,(km->ncurves+1)*sizeof(kate_curve*)); |
| 503 |
if (new_curves) km->curves=new_curves; |
| 504 |
new_durations=(kate_float*)kate_realloc(km->durations,(km->ncurves+1)*sizeof(kate_float)); |
| 505 |
if (new_durations) km->durations=new_durations; |
| 506 |
|
| 507 |
if (new_curves && new_durations) { |
| 508 |
km->ncurves++; |
| 509 |
km->curves[km->ncurves-1]=kc; |
| 510 |
km->durations[km->ncurves-1]=dt/(kate_float)1000; |
| 511 |
} |
| 512 |
else { |
| 513 |
fprintf(stderr,"Error: failed to allocate memory to store curve/duration"); |
| 514 |
kate_free(kc->pts); |
| 515 |
kate_free(kc); |
| 516 |
} |
| 517 |
} |
| 518 |
else { |
| 519 |
fprintf(stderr,"Error: failed to allocate memory for curve points"); |
| 520 |
kate_free(kc); |
| 521 |
} |
| 522 |
} |
| 523 |
else { |
| 524 |
fprintf(stderr,"Error: failed to allocate memory for a kate_curve"); |
| 525 |
} |
| 526 |
} |
| 527 |
|
| 528 |
static int fraction_to_milliseconds(int fraction,int digits) |
| 529 |
{ |
| 530 |
while (digits<3) { |
| 531 |
fraction*=10; |
| 532 |
++digits; |
| 533 |
} |
| 534 |
while (digits>3) { |
| 535 |
fraction/=10; |
| 536 |
--digits; |
| 537 |
} |
| 538 |
return fraction; |
| 539 |
} |
| 540 |
|
| 541 |
static kate_motion *process_enhanced_lrc_tags(char *str,kate_int64_t start_time,kate_int64_t end_time,int line) |
| 542 |
{ |
| 543 |
char *start,*end; |
| 544 |
int ret; |
| 545 |
int m,s,fs; |
| 546 |
kate_motion *km=NULL; |
| 547 |
kate_int64_t current_time=start_time; |
| 548 |
int f0,f1; |
| 549 |
|
| 550 |
if (!str) return NULL; |
| 551 |
|
| 552 |
start=str; |
| 553 |
while (1) { |
| 554 |
start=strchr(start,'<'); |
| 555 |
if (!start) break; |
| 556 |
end=strchr(start+1,'>'); |
| 557 |
if (!end) break; |
| 558 |
|
| 559 |
/* we found a <> pair, parse it */ |
| 560 |
f0=f1=-1; |
| 561 |
ret=sscanf(start,"<%d:%d.%n%d%n>",&m,&s,&f0,&fs,&f1); |
| 562 |
|
| 563 |
/* remove the <> tag from input to get raw text */ |
| 564 |
memmove(start,end+1,strlen(end+1)+1); |
| 565 |
|
| 566 |
if (ret<3 || (f0|f1)<0 || f1<=f0 || (m|s|fs)<0) { |
| 567 |
fprintf(stderr,"Error: failed to process enhanced LRC tag (%*.*s) - ignored\n",(int)(end-start+1),(int)(end-start+1),start); |
| 568 |
} |
| 569 |
else { |
| 570 |
kate_int64_t tag_time=hmsms2ms(0,m,s,fraction_to_milliseconds(fs,f1-f0)); |
| 571 |
|
| 572 |
/* if this is the first tag in this line, create a kate motion */ |
| 573 |
if (!km) { |
| 574 |
km=(kate_motion*)kate_malloc(sizeof(kate_motion)); |
| 575 |
if (!km) { |
| 576 |
fprintf(stderr,"Error: failed to allocate memory - enhanced LRC tag will be ignored\n"); |
| 577 |
} |
| 578 |
else { |
| 579 |
kate_motion_init(km); |
| 580 |
km->semantics=kate_motion_semantics_glyph_pointer_1; |
| 581 |
} |
| 582 |
} |
| 583 |
/* add to the kate motion */ |
| 584 |
if (km) { |
| 585 |
add_kate_karaoke_tag(km,tag_time-current_time,str,start-str,line); |
| 586 |
current_time=tag_time; |
| 587 |
} |
| 588 |
} |
| 589 |
} |
| 590 |
|
| 591 |
/* if we've found karaoke info, extend the motion to the end time */ |
| 592 |
if (km) { |
| 593 |
add_kate_karaoke_tag(km,end_time-current_time,str,strlen(str),line); |
| 594 |
} |
| 595 |
|
| 596 |
return km; |
| 597 |
} |
| 598 |
|
| 599 |
static void add_kate_karaoke_style(kate_info *ki,unsigned char r,unsigned char g,unsigned char b,unsigned char a) |
| 600 |
{ |
| 601 |
kate_style *ks; |
| 602 |
int ret; |
| 603 |
|
| 604 |
if (!ki) return; |
| 605 |
|
| 606 |
ks=(kate_style*)kate_malloc(sizeof(kate_style)); |
| 607 |
if (ks) { |
| 608 |
kate_style_init(ks); |
| 609 |
ks->text_color.r = r; |
| 610 |
ks->text_color.g = g; |
| 611 |
ks->text_color.b = b; |
| 612 |
ks->text_color.a = a; |
| 613 |
ret=kate_info_add_style(ki,ks); |
| 614 |
if (ret<0) { |
| 615 |
fprintf(stderr,"WARNING - failed to add Kate karaoke style\n"); |
| 616 |
} |
| 617 |
} |
| 618 |
else { |
| 619 |
fprintf(stderr,"WARNING - failed to allocate memory for Kate karaoke style\n"); |
| 620 |
} |
| 621 |
} |
| 622 |
|
| 623 |
static int convert_lrc(FILE *fin,FILE *fout) |
| 624 |
{ |
| 625 |
int ret; |
| 626 |
static char text[4096]; |
| 627 |
int m,s,fs; |
| 628 |
kate_int64_t t; |
| 629 |
kate_int64_t start_time=-1; |
| 630 |
int offset; |
| 631 |
int line=0; |
| 632 |
fpos_t start; |
| 633 |
int has_karaoke; |
| 634 |
kate_motion *km; |
| 635 |
int f0,f1; |
| 636 |
size_t len; |
| 637 |
|
| 638 |
fgets2(str,sizeof(str),fin,1); |
| 639 |
++line; |
| 640 |
|
| 641 |
if (!memcmp(str,utf16lebom,sizeof(utf16lebom)) || !memcmp(str,utf16bebom,sizeof(utf16bebom))) { |
| 642 |
fprintf(stderr,"This file seems to be encoded in UTF-16, Kate only supports UTF-8\n"); |
| 643 |
fprintf(stderr,"You will need to convert it to UTF-8 first (eg, use iconv)\n"); |
| 644 |
return -1; |
| 645 |
} |
| 646 |
|
| 647 |
if (!memcmp(str,utf32lebom,sizeof(utf32lebom)) || !memcmp(str,utf32bebom,sizeof(utf32bebom))) { |
| 648 |
fprintf(stderr,"This file seems to be encoded in UTF-32, Kate only supports UTF-8\n"); |
| 649 |
fprintf(stderr,"You will need to convert it to UTF-8 first (eg, use iconv)\n"); |
| 650 |
return -1; |
| 651 |
} |
| 652 |
|
| 653 |
/* kill any utf-8 BOM present */ |
| 654 |
if (!memcmp(str,utf8bom,sizeof(utf8bom))) { |
| 655 |
/* printf("UTF-8 BOM found at start, skipped\n"); */ |
| 656 |
memmove(str,str+3,strlen(str+3)+1); |
| 657 |
} |
| 658 |
|
| 659 |
/* skip headers */ |
| 660 |
while (!feof(fin)) { |
| 661 |
ret=sscanf(str,"[%d:%d.%d]",&m,&s,&fs); |
| 662 |
if (ret==3) break; |
| 663 |
fgets2(str,sizeof(str),fin,1); |
| 664 |
++line; |
| 665 |
} |
| 666 |
|
| 667 |
/* there might be 'enhanced lrc' timing tags - if so, we'll include |
| 668 |
a timing motion for the appropriate events, as well as two styles |
| 669 |
in the headers, but we need to know beforehand if we'll need to |
| 670 |
add those styles, so they can go into headers */ |
| 671 |
if (fgetpos(fin,&start)<0) { |
| 672 |
fprintf(stderr,"Failed to get current file position\n"); |
| 673 |
return -1; |
| 674 |
} |
| 675 |
has_karaoke=0; |
| 676 |
strcpy(text,str); /* we'll need to restore str after peeking */ |
| 677 |
while (!feof(fin)) { |
| 678 |
ret=sscanf(str,"[%d:%d.%d]%n",&m,&s,&fs,&offset); |
| 679 |
if (ret==3) { |
| 680 |
const char *start=strchr(str+offset,'<'); |
| 681 |
if (start) { |
| 682 |
const char *end=strchr(start,'>'); |
| 683 |
if (end) { |
| 684 |
/* heuristics say we have enhanced lrc karaoke tags */ |
| 685 |
has_karaoke=1; |
| 686 |
break; |
| 687 |
} |
| 688 |
} |
| 689 |
} |
| 690 |
fgets2(str,sizeof(str),fin,1); |
| 691 |
} |
| 692 |
if (fsetpos(fin,&start)<0) { |
| 693 |
fprintf(stderr,"Failed to reset file position\n"); |
| 694 |
return -1; |
| 695 |
} |
| 696 |
strcpy(str,text); |
| 697 |
if (has_karaoke) { |
| 698 |
add_kate_karaoke_style(&ki, 255, 255, 255, 255); |
| 699 |
add_kate_karaoke_style(&ki, 255, 128, 128, 255); |
| 700 |
} |
| 701 |
|
| 702 |
ret=flush_headers(fout); |
| 703 |
if (ret<0) return ret; |
| 704 |
|
| 705 |
while (!feof(fin) || *str) { |
| 706 |
if (!is_line_empty(str)) { |
| 707 |
f0=f1=-1; |
| 708 |
ret=sscanf(str,"[%d:%d.%n%d%n]%n",&m,&s,&f0,&fs,&f1,&offset); |
| 709 |
if (ret<3 || (f0|f1)<0 || f1<=f0 || (m|s|fs)<0) { |
| 710 |
fprintf(stderr,"Syntax error at line %d: %s\n",line,str); |
| 711 |
return -1; |
| 712 |
} |
| 713 |
t=hmsms2ms(0,m,s,fraction_to_milliseconds(fs,f1-f0)); |
| 714 |
if (start_time>=0 && !is_line_empty(text)) { |
| 715 |
ogg_packet op; |
| 716 |
|
| 717 |
update_stream_time(&k,fout,start_time); |
| 718 |
|
| 719 |
if (text[strlen(text)-1]=='\n') text[strlen(text)-1]=0; |
| 720 |
km=process_enhanced_lrc_tags(text,start_time,t,line); |
| 721 |
if (km) { |
| 722 |
ret=kate_encode_set_style_index(&k, 0); |
| 723 |
if (ret < 0) { |
| 724 |
fprintf(stderr,"Failed encoding karaoke style - continuing anyway\n"); |
| 725 |
} |
| 726 |
ret=kate_encode_set_secondary_style_index(&k, 1); |
| 727 |
if (ret < 0) { |
| 728 |
fprintf(stderr,"Failed encoding karaoke style - continuing anyway\n"); |
| 729 |
} |
| 730 |
ret=kate_encode_add_motion(&k,km,1); |
| 731 |
if (ret<0) { |
| 732 |
fprintf(stderr,"Failed to add karaoke motion (%d)\n",ret); |
| 733 |
return ret; |
| 734 |
} |
| 735 |
} |
| 736 |
len=strlen(text); |
| 737 |
ret=kate_text_validate(kate_utf8,text,len+1); |
| 738 |
if (ret<0) { |
| 739 |
if (ret==KATE_E_TEXT) { |
| 740 |
fprintf(stderr,"Text is not valid UTF-8: %s\n",text); |
| 741 |
} |
| 742 |
else { |
| 743 |
fprintf(stderr,"Failed to validate text: %d\n",ret); |
| 744 |
} |
| 745 |
return ret; |
| 746 |
} |
| 747 |
ret=kate_ogg_encode_text_raw_times(&k,start_time,t,text,len,&op); |
| 748 |
if (ret<0) { |
| 749 |
fprintf(stderr,"Failed to add lyrics (%d)\n",ret); |
| 750 |
return ret; |
| 751 |
} |
| 752 |
ret=send_packet(fout,&op,start_time); |
| 753 |
if (ret<0) return ret; |
| 754 |
} |
| 755 |
start_time=t; |
| 756 |
strcpy(text,str+offset); |
| 757 |
} |
| 758 |
fgets2(str,sizeof(str),fin,1); |
| 759 |
++line; |
| 760 |
} |
| 761 |
return 0; |
| 762 |
} |
| 763 |
|
| 764 |
static void print_version(void) |
| 765 |
{ |
| 766 |
printf("Kate reference encoder - %s\n",kate_get_version_string()); |
| 767 |
} |
| 768 |
|
| 769 |
static void print_help(const char *argv0) |
| 770 |
{ |
| 771 |
printf("usage: %s [options] [filename]\n",argv0); |
| 772 |
printf(" -V version\n"); |
| 773 |
printf(" -h help\n"); |
| 774 |
printf(" -o <filename> set output filename\n"); |
| 775 |
printf(" -t <type> set input file type\n"); |
| 776 |
printf(" types can be: kate, srt, lrc\n"); |
| 777 |
printf(" -l <language> set stream language\n"); |
| 778 |
printf(" -c <category> set stream category\n"); |
| 779 |
printf(" -s <hex number> set serial number of output stream\n"); |
| 780 |
printf(" -R <threshold> Use repeat packets with given threshold (seconds)\n"); |
| 781 |
printf(" -K <threshold> Use keepalive packets with given threshold (seconds)\n"); |
| 782 |
printf(" -C <tag>=<value> Add comment to the Kate stream\n"); |
| 783 |
printf(" -M Allow simple markup in SRT files\n"); |
| 784 |
#ifdef DEBUG |
| 785 |
printf(" -r write raw Kate stream (experimental)\n"); |
| 786 |
#endif |
| 787 |
} |
| 788 |
|
| 789 |
#ifdef DEBUG |
| 790 |
static void print_rle_stats(void) |
| 791 |
{ |
| 792 |
int n,total=0; |
| 793 |
extern int kate_rle_stats_overall[8]; |
| 794 |
static const char *kate_rle_type_names[8]={ |
| 795 |
"empty", |
| 796 |
"basic", |
| 797 |
"delta", |
| 798 |
"stop", |
| 799 |
"start/end", |
| 800 |
"delta/stop", |
| 801 |
"basic/zero", |
| 802 |
"", |
| 803 |
}; |
| 804 |
for (n=0;n<8;++n) total+=kate_rle_stats_overall[n]; |
| 805 |
if (total) { |
| 806 |
for (n=0;n<8;++n) { |
| 807 |
printf("%s: %d (%.2f%%)\n",kate_rle_type_names[n],kate_rle_stats_overall[n],100.0f*kate_rle_stats_overall[n]/total); |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
#endif |
| 812 |
|
| 813 |
static void set_encoder_comment(kate_comment *kc) |
| 814 |
{ |
| 815 |
int ret; |
| 816 |
const char *base="kateenc - "; |
| 817 |
const char *version=kate_get_version_string(); |
| 818 |
size_t len=strlen(base)+strlen(version)+1; |
| 819 |
char *value=(char*)kate_malloc(len); |
| 820 |
if (!value) { |
| 821 |
fprintf(stderr,"Failed to allocate %lu bytes\n",(unsigned long)len); |
| 822 |
exit(-1); |
| 823 |
} |
| 824 |
sprintf(value,"%s%s",base,version); |
| 825 |
ret=kate_comment_add_tag(kc,"ENCODER",value); |
| 826 |
kate_free(value); |
| 827 |
if (ret<0) { |
| 828 |
fprintf(stderr,"kate_comment_add_tag failed: %d\n",ret); |
| 829 |
exit(-1); |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
int main(int argc,char **argv) |
| 834 |
{ |
| 835 |
int n,ret,failed=0; |
| 836 |
const char *input_filename=NULL; |
| 837 |
const char *output_filename=NULL; |
| 838 |
const char *input_file_type=NULL; |
| 839 |
uint32_t serial; |
| 840 |
const char *comment; |
| 841 |
const char *arg; |
| 842 |
char *endptr; |
| 843 |
FILE *fin,*fout; |
| 844 |
int allow_srt_markup=0; |
| 845 |
|
| 846 |
srand(time(NULL)^getpid()); |
| 847 |
serial=rand(); |
| 848 |
|
| 849 |
packetno=0; |
| 850 |
|
| 851 |
ret=kate_comment_init(&kc); |
| 852 |
if (ret<0) { |
| 853 |
fprintf(stderr,"kate_comment_init failed: %d\n",ret); |
| 854 |
exit(-1); |
| 855 |
} |
| 856 |
set_encoder_comment(&kc); |
| 857 |
if (ret<0) { |
| 858 |
fprintf(stderr,"Failed to set encoder tag: %d\n",ret); |
| 859 |
exit(-1); |
| 860 |
} |
| 861 |
ret=kate_info_init(&ki); |
| 862 |
if (ret<0) { |
| 863 |
fprintf(stderr,"kate_comment_init failed: %d\n",ret); |
| 864 |
exit(-1); |
| 865 |
} |
| 866 |
|
| 867 |
for (n=1;n<argc;++n) { |
| 868 |
if (argv[n][0]=='-') { |
| 869 |
switch (argv[n][1]) { |
| 870 |
case '-': |
| 871 |
if (!strcmp(argv[n],"--version")) { |
| 872 |
print_version(); |
| 873 |
exit(0); |
| 874 |
} |
| 875 |
else if (!strcmp(argv[n],"--help")) { |
| 876 |
print_help(argv[0]); |
| 877 |
exit(0); |
| 878 |
} |
| 879 |
else { |
| 880 |
fprintf(stderr,"Invalid option: %s\n",argv[n]); |
| 881 |
exit(-1); |
| 882 |
} |
| 883 |
break; |
| 884 |
case 'V': |
| 885 |
print_version(); |
| 886 |
exit(0); |
| 887 |
case 'h': |
| 888 |
print_help(argv[0]); |
| 889 |
exit(0); |
| 890 |
case 'o': |
| 891 |
if (!output_filename) { |
| 892 |
output_filename=eat_arg(argc,argv,&n); |
| 893 |
} |
| 894 |
else { |
| 895 |
fprintf(stderr,"Only one output filename may be given\n"); |
| 896 |
exit(-1); |
| 897 |
} |
| 898 |
break; |
| 899 |
case 't': |
| 900 |
if (!input_file_type) { |
| 901 |
input_file_type=eat_arg(argc,argv,&n); |
| 902 |
} |
| 903 |
else { |
| 904 |
fprintf(stderr,"Only one input file type may be given\n"); |
| 905 |
exit(-1); |
| 906 |
} |
| 907 |
break; |
| 908 |
case 'l': |
| 909 |
if (!language) { |
| 910 |
language=eat_arg(argc,argv,&n); |
| 911 |
} |
| 912 |
else { |
| 913 |
fprintf(stderr,"Only one language may be given\n"); |
| 914 |
exit(-1); |
| 915 |
} |
| 916 |
break; |
| 917 |
case 'c': |
| 918 |
if (!category) { |
| 919 |
category=eat_arg(argc,argv,&n); |
| 920 |
} |
| 921 |
else { |
| 922 |
fprintf(stderr,"Only one category may be given\n"); |
| 923 |
exit(-1); |
| 924 |
} |
| 925 |
break; |
| 926 |
case 's': |
| 927 |
serial=strtoul(eat_arg(argc,argv,&n),NULL,16); |
| 928 |
break; |
| 929 |
case 'C': |
| 930 |
comment=eat_arg(argc,argv,&n); |
| 931 |
/* check there's a = sign though */ |
| 932 |
if (!strchr(comment,'=')) { |
| 933 |
fprintf(stderr,"comments must be of the form tag=value\n"); |
| 934 |
exit(-1); |
| 935 |
} |
| 936 |
ret=kate_comment_add(&kc,comment); |
| 937 |
if (ret<0) { |
| 938 |
fprintf(stderr,"error adding comment \"%s\": %d\n",comment,ret); |
| 939 |
exit(-1); |
| 940 |
} |
| 941 |
break; |
| 942 |
case 'R': |
| 943 |
arg=eat_arg(argc,argv,&n); |
| 944 |
endptr=NULL; |
| 945 |
repeat_threshold=(kate_float)strtod(arg,&endptr); |
| 946 |
if (endptr==arg || *endptr) { |
| 947 |
fprintf(stderr,"error in repeat threshold: %s: should be a floating point value\n",arg); |
| 948 |
exit(-1); |
| 949 |
} |
| 950 |
break; |
| 951 |
case 'K': |
| 952 |
arg=eat_arg(argc,argv,&n); |
| 953 |
endptr=NULL; |
| 954 |
keepalive_threshold=(kate_float)strtod(arg,&endptr); |
| 955 |
if (endptr==arg || *endptr) { |
| 956 |
fprintf(stderr,"error in keepalive threshold: %s: should be a floating point value\n",arg); |
| 957 |
exit(-1); |
| 958 |
} |
| 959 |
break; |
| 960 |
case 'M': |
| 961 |
allow_srt_markup=1; |
| 962 |
break; |
| 963 |
#ifdef DEBUG |
| 964 |
case 'r': |
| 965 |
raw=1; |
| 966 |
break; |
| 967 |
#endif |
| 968 |
default: |
| 969 |
fprintf(stderr,"Invalid option: %s\n",argv[n]); |
| 970 |
exit(-1); |
| 971 |
} |
| 972 |
} |
| 973 |
else { |
| 974 |
if (!input_filename) { |
| 975 |
input_filename=argv[n]; |
| 976 |
} |
| 977 |
else { |
| 978 |
fprintf(stderr,"Only one input filename may be given\n"); |
| 979 |
exit(-1); |
| 980 |
} |
| 981 |
} |
| 982 |
} |
| 983 |
|
| 984 |
if (!input_file_type) { |
| 985 |
fprintf(stderr,"No input file type given\n"); |
| 986 |
exit(-1); |
| 987 |
} |
| 988 |
|
| 989 |
if (!input_filename || !strcmp(input_filename,"-")) { |
| 990 |
fin=stdin; |
| 991 |
} |
| 992 |
else { |
| 993 |
const char *ptr,*end; |
| 994 |
fin=fopen(input_filename,"r"); |
| 995 |
if (!fin) { |
| 996 |
fprintf(stderr,"%s: %s\n",input_filename,strerror(errno)); |
| 997 |
exit(-1); |
| 998 |
} |
| 999 |
ptr=input_filename; |
| 1000 |
end=NULL; |
| 1001 |
while (*ptr) { |
| 1002 |
if (*ptr=='/' || *ptr=='\\') |
| 1003 |
end=ptr; |
| 1004 |
++ptr; |
| 1005 |
} |
| 1006 |
if (end) { |
| 1007 |
/* we found a slash */ |
| 1008 |
if ((size_t)(end-input_filename+1)>=sizeof(base_path)) { |
| 1009 |
fprintf(stderr,"%s too long\n",input_filename); |
| 1010 |
exit(-1); |
| 1011 |
} |
| 1012 |
strncpy(base_path,input_filename,end-input_filename+1); |
| 1013 |
base_path[end-input_filename+1]=0; /* just after the last slash */ |
| 1014 |
} |
| 1015 |
else { |
| 1016 |
/* we did not found a slash */ |
| 1017 |
strcpy(base_path,""); |
| 1018 |
} |
| 1019 |
} |
| 1020 |
|
| 1021 |
if (!output_filename || !strcmp(output_filename,"-")) { |
| 1022 |
#if defined WIN32 || defined _WIN32 |
| 1023 |
_setmode(_fileno(stdout),_O_BINARY); |
| 1024 |
#else |
| 1025 |
#if defined MSDOS || defined __CYGWIN__ || defined __EMX__ || defined OS2 || defined __BORLANDC__ |
| 1026 |
setmode(fileno(stdout),_O_BINARY); |
| 1027 |
#endif |
| 1028 |
#endif |
| 1029 |
fout=stdout; |
| 1030 |
} |
| 1031 |
else { |
| 1032 |
fout=fopen(output_filename,"wb"); |
| 1033 |
if (!fout) { |
| 1034 |
fprintf(stderr,"%s: %s\n",output_filename,strerror(errno)); |
| 1035 |
exit(-1); |
| 1036 |
} |
| 1037 |
} |
| 1038 |
|
| 1039 |
ret=kate_encode_init(&k,&ki); |
| 1040 |
if (ret<0) { |
| 1041 |
fprintf(stderr,"kate_encode_init failed: %d\n",ret); |
| 1042 |
exit(-1); |
| 1043 |
} |
| 1044 |
|
| 1045 |
#ifdef DEBUG |
| 1046 |
if (!raw) |
| 1047 |
#endif |
| 1048 |
ogg_stream_init(&os,serial); |
| 1049 |
|
| 1050 |
ret=0; |
| 1051 |
if (!strcmp(input_file_type,"kate")) { |
| 1052 |
ret=convert_kate(fin,fout); |
| 1053 |
} |
| 1054 |
else if (!strcmp(input_file_type,"srt")) { |
| 1055 |
ret=convert_srt(fin,fout,allow_srt_markup); |
| 1056 |
} |
| 1057 |
else if (!strcmp(input_file_type,"lrc")) { |
| 1058 |
ret=convert_lrc(fin,fout); |
| 1059 |
} |
| 1060 |
else { |
| 1061 |
fprintf(stderr,"Invalid format type: %s\n",input_file_type); |
| 1062 |
ret=-1; |
| 1063 |
} |
| 1064 |
if (ret<0) failed=ret; |
| 1065 |
|
| 1066 |
if (ret==0) { |
| 1067 |
ogg_packet op; |
| 1068 |
ret=kate_ogg_encode_finish(&k,-1,&op); |
| 1069 |
if (ret<0) { |
| 1070 |
fprintf(stderr,"error encoding end packet: %d\n",ret); |
| 1071 |
failed=ret; |
| 1072 |
} |
| 1073 |
else { |
| 1074 |
ret=send_packet(fout,&op,-1); |
| 1075 |
if (ret<0) { |
| 1076 |
fprintf(stderr,"error sending end packet: %d\n",ret); |
| 1077 |
failed=ret; |
| 1078 |
} |
| 1079 |
else { |
| 1080 |
#ifdef DEBUG |
| 1081 |
if (!raw) |
| 1082 |
#endif |
| 1083 |
{ |
| 1084 |
ret=flush_page(fout); |
| 1085 |
if (ret<0) { |
| 1086 |
fprintf(stderr,"error flushing page: %d\n",ret); |
| 1087 |
failed=ret; |
| 1088 |
} |
| 1089 |
} |
| 1090 |
} |
| 1091 |
} |
| 1092 |
} |
| 1093 |
|
| 1094 |
#ifdef DEBUG |
| 1095 |
if (!raw) |
| 1096 |
#endif |
| 1097 |
ogg_stream_clear(&os); |
| 1098 |
|
| 1099 |
ret=kate_clear(&k); |
| 1100 |
if (ret<0) { |
| 1101 |
fprintf(stderr,"kate_clear failed: %d\n",ret); |
| 1102 |
/* continue anyway */ |
| 1103 |
} |
| 1104 |
|
| 1105 |
ret=kate_info_clear(&ki); |
| 1106 |
if (ret<0) { |
| 1107 |
fprintf(stderr,"kate_info_clear failed: %d\n",ret); |
| 1108 |
/* continue anyway */ |
| 1109 |
} |
| 1110 |
ret=kate_comment_clear(&kc); |
| 1111 |
if (ret<0) { |
| 1112 |
fprintf(stderr,"kate_comment_clear failed: %d\n",ret); |
| 1113 |
/* continue anyway */ |
| 1114 |
} |
| 1115 |
|
| 1116 |
if (fout!=stdout) { |
| 1117 |
fclose(fout); |
| 1118 |
if (ret<0) { |
| 1119 |
int unlink_ret=unlink(output_filename); |
| 1120 |
if (unlink_ret<0) { |
| 1121 |
fprintf(stderr,"unlink failed (%d) - corrupt file %s will not be deleted\n",ret,output_filename); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
if (fin!=stdin) fclose(fin); |
| 1126 |
|
| 1127 |
#ifdef DEBUG |
| 1128 |
print_rle_stats(); |
| 1129 |
#endif |
| 1130 |
|
| 1131 |
return failed?failed:ret; |
| 1132 |
} |