54 |
* Global vars in module and constants |
* Global vars in module and constants |
55 |
****************************************************************************/ |
****************************************************************************/ |
56 |
|
|
|
/* max number of frames */ |
|
|
#define ABS_MAXFRAMENR 9999 |
|
|
|
|
57 |
#define USE_PNM 0 |
#define USE_PNM 0 |
58 |
#define USE_TGA 1 |
#define USE_TGA 1 |
59 |
|
#define USE_YUV 2 |
60 |
|
|
61 |
static int XDIM = 0; |
static int XDIM = 0; |
62 |
static int YDIM = 0; |
static int YDIM = 0; |
72 |
|
|
73 |
#define BUFFER_SIZE (2*1024*1024) |
#define BUFFER_SIZE (2*1024*1024) |
74 |
|
|
75 |
|
static const int display_buffer_bytes = 0; |
76 |
|
|
77 |
|
#define MIN_USEFUL_BYTES 1 |
78 |
|
|
79 |
/***************************************************************************** |
/***************************************************************************** |
80 |
* Local prototypes |
* Local prototypes |
81 |
****************************************************************************/ |
****************************************************************************/ |
91 |
static int write_image(char *prefix, unsigned char *image); |
static int write_image(char *prefix, unsigned char *image); |
92 |
static int write_pnm(char *filename, unsigned char *image); |
static int write_pnm(char *filename, unsigned char *image); |
93 |
static int write_tga(char *filename, unsigned char *image); |
static int write_tga(char *filename, unsigned char *image); |
94 |
|
static int write_yuv(char *filename, unsigned char *image); |
95 |
|
|
96 |
const char * type2str(int type) |
const char * type2str(int type) |
97 |
{ |
{ |
114 |
unsigned char *mp4_ptr = NULL; |
unsigned char *mp4_ptr = NULL; |
115 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
116 |
int useful_bytes; |
int useful_bytes; |
117 |
|
int chunk; |
118 |
xvid_dec_stats_t xvid_dec_stats; |
xvid_dec_stats_t xvid_dec_stats; |
119 |
|
|
120 |
double totaldectime; |
double totaldectime; |
176 |
i++; |
i++; |
177 |
if (strcmp(argv[i], "tga") == 0) { |
if (strcmp(argv[i], "tga") == 0) { |
178 |
FORMAT = USE_TGA; |
FORMAT = USE_TGA; |
179 |
|
} else if (strcmp(argv[i], "yuv") == 0) { |
180 |
|
FORMAT = USE_YUV; |
181 |
} else { |
} else { |
182 |
FORMAT = USE_PNM; |
FORMAT = USE_PNM; |
183 |
} |
} |
216 |
if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { |
if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { |
217 |
FORMAT = USE_TGA; |
FORMAT = USE_TGA; |
218 |
} |
} |
219 |
|
if (BPP != 1 && FORMAT == USE_YUV) { |
220 |
|
FORMAT = USE_TGA; |
221 |
|
} |
222 |
|
|
223 |
/***************************************************************************** |
/***************************************************************************** |
224 |
* Memory allocation |
* Memory allocation |
253 |
totalsize = 0; |
totalsize = 0; |
254 |
filenr = 0; |
filenr = 0; |
255 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
256 |
|
chunk = 0; |
257 |
|
|
258 |
do { |
do { |
259 |
int used_bytes = 0; |
int used_bytes = 0; |
274 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
275 |
|
|
276 |
/* read new data */ |
/* read new data */ |
277 |
if(feof(in_file)) |
if(!feof(in_file)) { |
|
break; |
|
|
|
|
278 |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
279 |
1, BUFFER_SIZE - already_in_buffer, |
1, BUFFER_SIZE - already_in_buffer, |
280 |
in_file); |
in_file); |
281 |
|
} |
282 |
} |
} |
283 |
|
|
284 |
|
|
310 |
|
|
311 |
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
312 |
} |
} |
313 |
|
|
314 |
|
/* Save individual mpeg4 stream if required */ |
315 |
|
if(ARG_SAVEMPEGSTREAM) { |
316 |
|
FILE *filehandle = NULL; |
317 |
|
|
318 |
|
sprintf(filename, "%svolhdr.m4v", filepath); |
319 |
|
filehandle = fopen(filename, "wb"); |
320 |
|
if(!filehandle) { |
321 |
|
fprintf(stderr, |
322 |
|
"Error writing vol header mpeg4 stream to file %s\n", |
323 |
|
filename); |
324 |
|
} else { |
325 |
|
fwrite(mp4_ptr, 1, used_bytes, filehandle); |
326 |
|
fclose(filehandle); |
327 |
|
} |
328 |
|
} |
329 |
} |
} |
330 |
|
|
331 |
/* Update buffer pointers */ |
/* Update buffer pointers */ |
337 |
totalsize += used_bytes; |
totalsize += used_bytes; |
338 |
} |
} |
339 |
|
|
340 |
}while(xvid_dec_stats.type <= 0 && useful_bytes > 0); |
if (display_buffer_bytes) { |
341 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
342 |
|
} |
343 |
|
} while (xvid_dec_stats.type <= 0 && useful_bytes > MIN_USEFUL_BYTES); |
344 |
|
|
345 |
/* Check if there is a negative number of useful bytes left in buffer |
/* Check if there is a negative number of useful bytes left in buffer |
346 |
* This means we went too far */ |
* This means we went too far */ |
351 |
totaldectime += dectime; |
totaldectime += dectime; |
352 |
|
|
353 |
|
|
354 |
|
if (!display_buffer_bytes) { |
355 |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
356 |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
357 |
|
} |
358 |
|
|
359 |
/* Save individual mpeg4 stream if required */ |
/* Save individual mpeg4 stream if required */ |
360 |
if(ARG_SAVEMPEGSTREAM) { |
if(ARG_SAVEMPEGSTREAM) { |
375 |
|
|
376 |
/* Save output frame if required */ |
/* Save output frame if required */ |
377 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
378 |
|
if (FORMAT == USE_YUV) { |
379 |
|
sprintf(filename, "%sdec", filepath); |
380 |
|
} |
381 |
|
else { |
382 |
sprintf(filename, "%sdec%05d", filepath, filenr); |
sprintf(filename, "%sdec%05d", filepath, filenr); |
383 |
|
} |
384 |
if(write_image(filename, out_buffer)) { |
if(write_image(filename, out_buffer)) { |
385 |
fprintf(stderr, |
fprintf(stderr, |
386 |
"Error writing decoded frame %s\n", |
"Error writing decoded frame %s\n", |
390 |
|
|
391 |
filenr++; |
filenr++; |
392 |
|
|
393 |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR)); |
} while (useful_bytes>MIN_USEFUL_BYTES || !feof(in_file)); |
394 |
|
|
395 |
|
useful_bytes = 0; /* Empty buffer */ |
396 |
|
|
397 |
/***************************************************************************** |
/***************************************************************************** |
398 |
* Flush decoder buffers |
* Flush decoder buffers |
408 |
dectime = msecond(); |
dectime = msecond(); |
409 |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
410 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
411 |
|
if (display_buffer_bytes) { |
412 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
413 |
|
} |
414 |
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
415 |
|
|
416 |
if (used_bytes < 0) { /* XVID_ERR_END */ |
if (used_bytes < 0) { /* XVID_ERR_END */ |
421 |
totaldectime += dectime; |
totaldectime += dectime; |
422 |
|
|
423 |
/* Prints some decoding stats */ |
/* Prints some decoding stats */ |
424 |
|
if (!display_buffer_bytes) { |
425 |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
426 |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
427 |
|
} |
428 |
|
|
429 |
/* Save output frame if required */ |
/* Save output frame if required */ |
430 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
431 |
|
if (FORMAT == USE_YUV) { |
432 |
|
sprintf(filename, "%sdec", filepath); |
433 |
|
} |
434 |
|
else { |
435 |
sprintf(filename, "%sdec%05d", filepath, filenr); |
sprintf(filename, "%sdec%05d", filepath, filenr); |
436 |
|
} |
437 |
if(write_image(filename, out_buffer)) { |
if(write_image(filename, out_buffer)) { |
438 |
fprintf(stderr, |
fprintf(stderr, |
439 |
"Error writing decoded frame %s\n", |
"Error writing decoded frame %s\n", |
490 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
491 |
fprintf(stderr, " -d : save decoder output\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
492 |
fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); |
fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); |
493 |
fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm)\n"); |
fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm, yuv)\n"); |
494 |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
495 |
fprintf(stderr, " -help : This help message\n"); |
fprintf(stderr, " -help : This help message\n"); |
496 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
512 |
#else |
#else |
513 |
clock_t clk; |
clock_t clk; |
514 |
clk = clock(); |
clk = clock(); |
515 |
return(clk * 1000 / CLOCKS_PER_SEC); |
return(clk * 1000.0 / CLOCKS_PER_SEC); |
516 |
#endif |
#endif |
517 |
} |
} |
518 |
|
|
530 |
ext = "pgm"; |
ext = "pgm"; |
531 |
} else if (FORMAT == USE_PNM && BPP == 3) { |
} else if (FORMAT == USE_PNM && BPP == 3) { |
532 |
ext = "pnm"; |
ext = "pnm"; |
533 |
|
} else if (FORMAT == USE_YUV) { |
534 |
|
ext = "yuv"; |
535 |
} else if (FORMAT == USE_TGA) { |
} else if (FORMAT == USE_TGA) { |
536 |
ext = "tga"; |
ext = "tga"; |
537 |
} else { |
} else { |
543 |
|
|
544 |
if (FORMAT == USE_PNM) { |
if (FORMAT == USE_PNM) { |
545 |
ret = write_pnm(filename, image); |
ret = write_pnm(filename, image); |
546 |
|
} else if (FORMAT == USE_YUV) { |
547 |
|
ret = write_yuv(filename, image); |
548 |
} else { |
} else { |
549 |
ret = write_tga(filename, image); |
ret = write_tga(filename, image); |
550 |
} |
} |
595 |
#else |
#else |
596 |
{ |
{ |
597 |
int i; |
int i; |
598 |
for (i=0; i<width*height*BPP;i+=BPP) { |
for (i=0; i<XDIM*YDIM*BPP;i+=BPP) { |
599 |
if (BPP == 1) { |
if (BPP == 1) { |
600 |
fputc(image+i, f); |
fputc(*(image+i), f); |
601 |
} else if (BPP == 2) { |
} else if (BPP == 2) { |
602 |
fputc(image+i+1, f); |
fputc(*(image+i+1), f); |
603 |
fputc(image+i+0, f); |
fputc(*(image+i+0), f); |
604 |
} else if (BPP == 3) { |
} else if (BPP == 3) { |
605 |
fputc(image+i+2, f); |
fputc(*(image+i+2), f); |
606 |
fputc(image+i+1, f); |
fputc(*(image+i+1), f); |
607 |
fputc(image+i+0, f); |
fputc(*(image+i+0), f); |
608 |
} else if (BPP == 4) { |
} else if (BPP == 4) { |
609 |
fputc(image+i+3, f); |
fputc(*(image+i+3), f); |
610 |
fputc(image+i+2, f); |
fputc(*(image+i+2), f); |
611 |
fputc(image+i+1, f); |
fputc(*(image+i+1), f); |
612 |
fputc(image+i+0, f); |
fputc(*(image+i+0), f); |
613 |
} |
} |
614 |
} |
} |
615 |
} |
} |
644 |
|
|
645 |
if (BPP == 1) { |
if (BPP == 1) { |
646 |
int i; |
int i; |
647 |
fprintf(f, "P5\n#xvid\n%i %i\n255\n", XDIM, YDIM*3/2); |
fprintf(f, "P5\n%i %i\n255\n", XDIM, YDIM*3/2); |
648 |
|
|
649 |
fwrite(image, 1, XDIM*YDIM, f); |
fwrite(image, 1, XDIM*YDIM, f); |
650 |
|
|
673 |
return 0; |
return 0; |
674 |
} |
} |
675 |
|
|
676 |
|
static int write_yuv(char *filename, unsigned char *image) |
677 |
|
{ |
678 |
|
FILE * f; |
679 |
|
|
680 |
|
f = fopen(filename, "ab+"); |
681 |
|
if ( f == NULL) { |
682 |
|
return -1; |
683 |
|
} |
684 |
|
|
685 |
|
if (BPP == 1) { |
686 |
|
int i; |
687 |
|
|
688 |
|
fwrite(image, 1, XDIM*YDIM, f); |
689 |
|
|
690 |
|
for (i=0; i<YDIM/2;i++) { |
691 |
|
fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f); |
692 |
|
fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f); |
693 |
|
} |
694 |
|
} |
695 |
|
|
696 |
|
fclose(f); |
697 |
|
|
698 |
|
return 0; |
699 |
|
} |
700 |
|
|
701 |
/***************************************************************************** |
/***************************************************************************** |
702 |
* Routines for decoding: init decoder, use, and stop decoder |
* Routines for decoding: init decoder, use, and stop decoder |
703 |
****************************************************************************/ |
****************************************************************************/ |