| 1 |
/* Copyright (C) 2008, 2009 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 |
#include <stdarg.h> |
| 17 |
#include <stdio.h> |
| 18 |
#ifdef HAVE_STDLIB_H |
| 19 |
#include <stdlib.h> |
| 20 |
#endif |
| 21 |
#include <errno.h> |
| 22 |
#ifdef HAVE_STRING_H |
| 23 |
#include <string.h> |
| 24 |
#endif |
| 25 |
#include <ctype.h> |
| 26 |
#include <ogg/ogg.h> |
| 27 |
#include "kate/oggkate.h" |
| 28 |
#include "kate_internal.h" |
| 29 |
#include "ksrt.h" |
| 30 |
#include "klrc.h" |
| 31 |
#include "kkate.h" |
| 32 |
#include "kfuzz.h" |
| 33 |
#include "kstream.h" |
| 34 |
#include "kread.h" |
| 35 |
#include "kstrings.h" |
| 36 |
#include "kutil.h" |
| 37 |
|
| 38 |
/* |
| 39 |
** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html |
| 40 |
*/ |
| 41 |
#ifdef UNUSED |
| 42 |
#elif defined (__GNUC__) |
| 43 |
# define UNUSED(x) UNUSED_ ## x __attribute__ ((unused)) |
| 44 |
#elif defined (__LCLINT__) |
| 45 |
# define UNUSED(x) /*@unused@*/ x |
| 46 |
#else |
| 47 |
# define UNUSED(x) x |
| 48 |
#endif |
| 49 |
|
| 50 |
typedef struct { |
| 51 |
kate_stream_set kate_streams; |
| 52 |
int n_streams; |
| 53 |
|
| 54 |
const char *output_filename; |
| 55 |
int verbose; |
| 56 |
#ifdef DEBUG |
| 57 |
int fuzz; |
| 58 |
unsigned long fuzz_seed; |
| 59 |
#endif |
| 60 |
|
| 61 |
void (*write_start_function)(FILE*); |
| 62 |
void (*write_end_function)(FILE*); |
| 63 |
void (*write_headers_function)(FILE*,const kate_info*,const kate_comment*); |
| 64 |
void (*write_event_function)(FILE*,void*,const kate_event*,ogg_int64_t); |
| 65 |
void *(*new_event_function_data)(void); |
| 66 |
void (*free_event_function_data)(void*); |
| 67 |
} ogg_parser_data; |
| 68 |
|
| 69 |
static int ogg_parser_on_page(kate_uintptr_t data,long UNUSED(offset),ogg_page *og) |
| 70 |
{ |
| 71 |
ogg_parser_data *opd=(ogg_parser_data*)data; |
| 72 |
ogg_packet op; |
| 73 |
int ret; |
| 74 |
kate_stream *ks; |
| 75 |
|
| 76 |
/* check for hole */ |
| 77 |
if (!og) return 0; |
| 78 |
|
| 79 |
/* add the page in the correct stream */ |
| 80 |
if (ogg_page_bos(og)) { |
| 81 |
add_kate_stream(&opd->kate_streams,og,opd->n_streams++); |
| 82 |
ks=find_kate_stream_for_page(&opd->kate_streams,og); |
| 83 |
if (opd->new_event_function_data) ks->data=(*opd->new_event_function_data)(); |
| 84 |
if (opd->verbose) { |
| 85 |
printf("found BOS page, serial %08x\n",ogg_page_serialno(og)); |
| 86 |
} |
| 87 |
} |
| 88 |
else { |
| 89 |
ks=find_kate_stream_for_page(&opd->kate_streams,og); |
| 90 |
} |
| 91 |
|
| 92 |
if (ks) { |
| 93 |
while ((ret=ogg_stream_packetout(&ks->os,&op))) { |
| 94 |
if (ret<0) { |
| 95 |
fprintf(stderr,"Hole in data\n"); |
| 96 |
break; |
| 97 |
} |
| 98 |
|
| 99 |
if (ks->init<kstream_data) { |
| 100 |
ret=kate_ogg_decode_headerin(&ks->ki,&ks->kc,&op); |
| 101 |
if (ret>=0) { |
| 102 |
/* we found a Kate bitstream */ |
| 103 |
if (op.packetno==0) { |
| 104 |
if (opd->verbose) printf("Bitstream looks like Kate\n"); |
| 105 |
ks->init=kstream_header_info; |
| 106 |
} |
| 107 |
if (ret>0) { |
| 108 |
/* we're done parsing headers, go for data */ |
| 109 |
if (opd->verbose) { |
| 110 |
printf("Bitstream is Kate (bitstream v%d.%d, language \"%s\" category \"%s\", encoding %d (%s))\n", |
| 111 |
ks->ki.bitstream_version_major,ks->ki.bitstream_version_minor, |
| 112 |
ks->ki.language, |
| 113 |
ks->ki.category, |
| 114 |
ks->ki.text_encoding, |
| 115 |
encoding2text(ks->ki.text_encoding)); |
| 116 |
} |
| 117 |
|
| 118 |
ret=kate_decode_init(&ks->k,&ks->ki); |
| 119 |
if (ret<0) { |
| 120 |
fprintf(stderr,"kate_decode_init failed: %d\n",ret); |
| 121 |
exit(-1); |
| 122 |
} |
| 123 |
ks->init=kstream_data; |
| 124 |
|
| 125 |
if (!opd->output_filename || !strcmp(opd->output_filename,"-")) { |
| 126 |
if (find_kate_stream_for_file(&opd->kate_streams,stdout)) { |
| 127 |
fprintf(stderr,"Cannot write two Kate streams to the same output, new Kate stream will be ignored\n"); |
| 128 |
clear_and_remove_kate_stream(ks,&opd->kate_streams); |
| 129 |
break; |
| 130 |
} |
| 131 |
else { |
| 132 |
ks->fout=stdout; |
| 133 |
} |
| 134 |
} |
| 135 |
else { |
| 136 |
ks->filename=get_filename(opd->output_filename,ks,&opd->kate_streams); |
| 137 |
if (ks->filename) { |
| 138 |
ks->fout=fopen(ks->filename,"w"); |
| 139 |
if (!ks->fout) { |
| 140 |
fprintf(stderr,"%s: %s\n",ks->filename,strerror(errno)); |
| 141 |
exit(-1); |
| 142 |
} |
| 143 |
} |
| 144 |
else { |
| 145 |
/* get_filename returns NULL when it can't generate a filename because no format field |
| 146 |
was given in the output filename and we have more than one Kate stream */ |
| 147 |
fprintf(stderr,"Cannot write two Kate streams to the same output, new Kate stream will be ignored\n"); |
| 148 |
clear_and_remove_kate_stream(ks,&opd->kate_streams); |
| 149 |
break; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
if (ks) { |
| 154 |
if (opd->write_start_function) (*opd->write_start_function)(ks->fout); |
| 155 |
|
| 156 |
if (opd->write_headers_function) (*opd->write_headers_function)(ks->fout,&ks->ki,&ks->kc); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
else { |
| 161 |
if (ret==KATE_E_NOT_KATE) { |
| 162 |
/* Bitstream is not Kate, ignore */ |
| 163 |
} |
| 164 |
else { |
| 165 |
fprintf(stderr,"kate_decode_headerin: packetno %"PRId64": %d\n",op.packetno,ret); |
| 166 |
ks->ret=ret; |
| 167 |
} |
| 168 |
clear_and_remove_kate_stream(ks,&opd->kate_streams); |
| 169 |
break; |
| 170 |
} |
| 171 |
} |
| 172 |
else { |
| 173 |
ret=kate_ogg_decode_packetin(&ks->k,&op); |
| 174 |
if (ret<0) { |
| 175 |
fprintf(stderr,"error in kate_decode_packetin: %d\n",ret); |
| 176 |
ks->ret=ret; |
| 177 |
} |
| 178 |
else if (ret>0) { |
| 179 |
/* we're done */ |
| 180 |
if (opd->write_end_function) (*opd->write_end_function)(ks->fout); |
| 181 |
ks->init=kstream_eos; |
| 182 |
} |
| 183 |
else { |
| 184 |
const kate_event *ev=NULL; |
| 185 |
ret=kate_decode_eventout(&ks->k,&ev); |
| 186 |
if (ret<0) { |
| 187 |
fprintf(stderr,"error in kate_decode_eventout: %d\n",ret); |
| 188 |
ks->ret=ret; |
| 189 |
} |
| 190 |
else if (ret==0) { |
| 191 |
if (opd->write_event_function) { |
| 192 |
(*opd->write_event_function)(ks->fout,ks->data,ev,ogg_page_granulepos(og)); |
| 193 |
} |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
} |
| 198 |
} |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static void print_version(void) |
| 204 |
{ |
| 205 |
printf("Kate reference decoder - %s\n",kate_get_version_string()); |
| 206 |
} |
| 207 |
|
| 208 |
static void print_help(const char *argv0) |
| 209 |
{ |
| 210 |
printf("usage: %s [options] [filename]\n",argv0); |
| 211 |
printf(" -V version\n"); |
| 212 |
printf(" -v verbose\n"); |
| 213 |
printf(" -h help\n"); |
| 214 |
printf(" -o <filename> set output filename\n"); |
| 215 |
printf(" -t <type> set output format (kate (default), srt, lrc)\n"); |
| 216 |
#ifdef DEBUG |
| 217 |
printf(" -B write some bitmaps in /tmp (debug)\n"); |
| 218 |
printf(" -f <number> fuzz testing with given seed (debug)\n"); |
| 219 |
#endif |
| 220 |
} |
| 221 |
|
| 222 |
int main(int argc,char **argv) |
| 223 |
{ |
| 224 |
size_t bytes_read; |
| 225 |
const char *input_filename=NULL; |
| 226 |
const char *output_filename=NULL; |
| 227 |
const char *output_filename_type=NULL; |
| 228 |
FILE *fin; |
| 229 |
int verbose=0; |
| 230 |
int arg; |
| 231 |
char signature[64]; /* matches the size of the Kate ID header */ |
| 232 |
size_t signature_size; |
| 233 |
#ifdef DEBUG |
| 234 |
int fuzz=0; |
| 235 |
unsigned long fuzz_seed=0; |
| 236 |
#endif |
| 237 |
ogg_parser_data opd; |
| 238 |
ogg_parser_funcs opf; |
| 239 |
size_t n; |
| 240 |
|
| 241 |
for (arg=1;arg<argc;++arg) { |
| 242 |
if (argv[arg][0]=='-') { |
| 243 |
switch (argv[arg][1]) { |
| 244 |
case '-': |
| 245 |
if (!strcmp(argv[arg],"--version")) { |
| 246 |
print_version(); |
| 247 |
exit(0); |
| 248 |
} |
| 249 |
else if (!strcmp(argv[arg],"--help")) { |
| 250 |
print_help(argv[0]); |
| 251 |
exit(0); |
| 252 |
} |
| 253 |
else { |
| 254 |
fprintf(stderr,"Invalid option: %s\n",argv[arg]); |
| 255 |
exit(-1); |
| 256 |
} |
| 257 |
break; |
| 258 |
case 'V': |
| 259 |
print_version(); |
| 260 |
exit(0); |
| 261 |
case 'h': |
| 262 |
print_help(argv[0]); |
| 263 |
exit(0); |
| 264 |
case 'o': |
| 265 |
if (!output_filename) { |
| 266 |
output_filename=eat_arg(argc,argv,&arg); |
| 267 |
} |
| 268 |
else { |
| 269 |
fprintf(stderr,"Only one output filename may be converted at a time\n"); |
| 270 |
exit(-1); |
| 271 |
} |
| 272 |
break; |
| 273 |
case 'v': |
| 274 |
++verbose; |
| 275 |
break; |
| 276 |
case 't': |
| 277 |
if (!output_filename_type) { |
| 278 |
output_filename_type=eat_arg(argc,argv,&arg); |
| 279 |
} |
| 280 |
else { |
| 281 |
fprintf(stderr,"Only one output type may be given\n"); |
| 282 |
exit(-1); |
| 283 |
} |
| 284 |
break; |
| 285 |
#ifdef DEBUG |
| 286 |
case 'B': |
| 287 |
write_bitmaps=1; |
| 288 |
break; |
| 289 |
case 'f': |
| 290 |
fuzz=1; |
| 291 |
fuzz_seed=strtoul(eat_arg(argc,argv,&arg),NULL,10); |
| 292 |
break; |
| 293 |
#endif |
| 294 |
default: |
| 295 |
fprintf(stderr,"Invalid option: %s\n",argv[arg]); |
| 296 |
exit(-1); |
| 297 |
} |
| 298 |
} |
| 299 |
else { |
| 300 |
if (!input_filename) { |
| 301 |
input_filename=argv[arg]; |
| 302 |
} |
| 303 |
else { |
| 304 |
fprintf(stderr,"Only one input filename may be converted at a time\n"); |
| 305 |
exit(-1); |
| 306 |
} |
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
if (!output_filename_type || !strcmp(output_filename_type,"kate")) { |
| 311 |
opd.write_start_function=&write_kate_start; |
| 312 |
opd.write_headers_function=&write_kate_headers; |
| 313 |
opd.write_end_function=&write_kate_end; |
| 314 |
opd.write_event_function=&write_kate_event; |
| 315 |
opd.new_event_function_data=NULL; |
| 316 |
opd.free_event_function_data=NULL; |
| 317 |
} |
| 318 |
else if (!strcmp(output_filename_type,"srt")) { |
| 319 |
opd.write_start_function=NULL; |
| 320 |
opd.write_headers_function=NULL; |
| 321 |
opd.write_end_function=NULL; |
| 322 |
opd.write_event_function=&write_srt_event; |
| 323 |
opd.new_event_function_data=&new_srt_data; |
| 324 |
opd.free_event_function_data=&free_srt_data; |
| 325 |
} |
| 326 |
else if (!strcmp(output_filename_type,"lrc")) { |
| 327 |
opd.write_start_function=NULL; |
| 328 |
opd.write_headers_function=NULL; |
| 329 |
opd.write_end_function=&write_lrc_end; |
| 330 |
opd.write_event_function=&write_lrc_event; |
| 331 |
opd.new_event_function_data=&new_lrc_data; |
| 332 |
opd.free_event_function_data=&free_lrc_data; |
| 333 |
} |
| 334 |
else { |
| 335 |
fprintf(stderr,"Invalid format type: %s\n",output_filename_type); |
| 336 |
exit(-1); |
| 337 |
} |
| 338 |
|
| 339 |
fin=open_and_probe_stream(input_filename); |
| 340 |
if (!fin) exit(-1); |
| 341 |
|
| 342 |
#ifdef DEBUG |
| 343 |
/* first, read the first few bytes to know if we have a raw Kate stream |
| 344 |
or a Kate-in-Ogg stream */ |
| 345 |
bytes_read=fread(signature,1,sizeof(signature),fin); |
| 346 |
if (bytes_read!=sizeof(signature)) { |
| 347 |
/* A Kate stream's first packet is 64 bytes, so this cannot be one */ |
| 348 |
fprintf(stderr,"Failed to read first %lu bytes of stream\n",(unsigned long)sizeof(signature)); |
| 349 |
exit(-1); |
| 350 |
} |
| 351 |
#else |
| 352 |
bytes_read=0; |
| 353 |
#endif |
| 354 |
|
| 355 |
#ifdef DEBUG |
| 356 |
if (!memcmp(signature,"\200kate\0\0\0",8)) { |
| 357 |
/* raw Kate stream */ |
| 358 |
kate_state k; |
| 359 |
FILE *fout; |
| 360 |
int ret=-1; |
| 361 |
char *buffer=NULL; |
| 362 |
ogg_int64_t bytes; |
| 363 |
int headers_written=0; |
| 364 |
void *write_event_function_data=NULL; |
| 365 |
|
| 366 |
if (opd.new_event_function_data) write_event_function_data=(*opd.new_event_function_data)(); |
| 367 |
|
| 368 |
bytes=64; |
| 369 |
buffer=(char*)kate_malloc(bytes); |
| 370 |
if (!buffer) { |
| 371 |
fprintf(stderr,"failed to allocate %"PRId64" bytes\n",bytes); |
| 372 |
exit(-1); |
| 373 |
} |
| 374 |
memcpy(buffer,signature,bytes); |
| 375 |
|
| 376 |
if (!output_filename || !strcmp(output_filename,"-")) { |
| 377 |
fout=stdout; |
| 378 |
} |
| 379 |
else { |
| 380 |
char *filename=get_filename(output_filename,NULL,NULL); |
| 381 |
fout=fopen(filename,"w"); |
| 382 |
if (!fout) { |
| 383 |
fprintf(stderr,"%s: %s\n",filename,strerror(errno)); |
| 384 |
exit(-1); |
| 385 |
} |
| 386 |
kate_free(filename); |
| 387 |
} |
| 388 |
|
| 389 |
ret=kate_high_decode_init(&k); |
| 390 |
if (ret<0) { |
| 391 |
fprintf(stderr,"failed to init raw kate packet decoding (%d)\n",ret); |
| 392 |
exit(-1); |
| 393 |
} |
| 394 |
|
| 395 |
while (1) { |
| 396 |
const kate_event *ev=NULL; |
| 397 |
kate_packet kp; |
| 398 |
kate_packet_wrap(&kp,bytes,buffer); |
| 399 |
#ifdef DEBUG |
| 400 |
if (fuzz) fuzz_kate_packet(&fuzz_seed,&kp); |
| 401 |
#endif |
| 402 |
ret=kate_high_decode_packetin(&k,&kp,&ev); |
| 403 |
if (ret<0) { |
| 404 |
fprintf(stderr,"failed to decode raw kate packet (%d)\n",ret); |
| 405 |
exit(-1); |
| 406 |
} |
| 407 |
if (k.ki->probe<0 && !headers_written) { |
| 408 |
if (opd.write_start_function) (*opd.write_start_function)(fout); |
| 409 |
if (opd.write_headers_function) (*opd.write_headers_function)(fout,k.ki,NULL); |
| 410 |
headers_written=1; |
| 411 |
} |
| 412 |
if (ret>0) { |
| 413 |
if (opd.write_end_function) (*opd.write_end_function)(fout); |
| 414 |
break; /* last packet decoded */ |
| 415 |
} |
| 416 |
if (ev) { |
| 417 |
if (opd.write_event_function) { |
| 418 |
(*opd.write_event_function)(fout,write_event_function_data,ev,-1); |
| 419 |
} |
| 420 |
} |
| 421 |
|
| 422 |
/* all subsequent packets are prefixed with 64 bits (signed) of the packet length in bytes */ |
| 423 |
ret=read_raw_size_and_packet(fin,&buffer,&bytes); |
| 424 |
if (ret<0) exit(-1); |
| 425 |
} |
| 426 |
|
| 427 |
ret=kate_high_decode_clear(&k); |
| 428 |
if (ret<0) { |
| 429 |
fprintf(stderr,"kate_high_decode_clear failed: %d\n",ret); |
| 430 |
/* continue anyway */ |
| 431 |
} |
| 432 |
kate_free(buffer); |
| 433 |
|
| 434 |
if (opd.free_event_function_data) { |
| 435 |
(*opd.free_event_function_data)(write_event_function_data); |
| 436 |
} |
| 437 |
|
| 438 |
if (fout!=stdout) fclose(fout); |
| 439 |
} |
| 440 |
else |
| 441 |
#endif |
| 442 |
{ |
| 443 |
int ret=-1; |
| 444 |
signature_size=bytes_read; |
| 445 |
|
| 446 |
init_kate_stream_set(&opd.kate_streams); |
| 447 |
opd.n_streams=0; |
| 448 |
opd.verbose=verbose; |
| 449 |
#ifdef DEBUG |
| 450 |
opd.fuzz=fuzz; |
| 451 |
opd.fuzz_seed=fuzz_seed; |
| 452 |
#endif |
| 453 |
opd.output_filename=output_filename; |
| 454 |
|
| 455 |
opf.on_page=&ogg_parser_on_page; |
| 456 |
opf.on_hole=NULL; |
| 457 |
ret=parse_ogg_stream(fin,signature,signature_size,opf,(kate_uintptr_t)&opd); |
| 458 |
if (ret<0) { |
| 459 |
fprintf(stderr,"parse_ogg_stream failed: %d\n",ret); |
| 460 |
/* continue anyway */ |
| 461 |
} |
| 462 |
|
| 463 |
/* Force EOS on truncated streams */ |
| 464 |
for (n=0;n<opd.kate_streams.n_kate_streams;++n) { |
| 465 |
kate_stream *ks=opd.kate_streams.kate_streams[n]; |
| 466 |
if (ks->init!=kstream_eos) { |
| 467 |
if (opd.write_end_function) (*opd.write_end_function)(ks->fout); |
| 468 |
ks->init=kstream_eos; |
| 469 |
} |
| 470 |
} |
| 471 |
|
| 472 |
/* Clear user data */ |
| 473 |
for (n=0;n<opd.kate_streams.n_kate_streams;++n) { |
| 474 |
kate_stream *ks=opd.kate_streams.kate_streams[n]; |
| 475 |
if (opd.free_event_function_data) (*opd.free_event_function_data)(ks->data); |
| 476 |
} |
| 477 |
|
| 478 |
clear_kate_stream_set(&opd.kate_streams); |
| 479 |
} |
| 480 |
|
| 481 |
if (fin!=stdin) fclose(fin); |
| 482 |
|
| 483 |
return 0; |
| 484 |
} |