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 |
|
|
71 |
|
|
72 |
#define BUFFER_SIZE (2*1024*1024) |
#define BUFFER_SIZE (2*1024*1024) |
73 |
|
|
74 |
|
static const int display_buffer_bytes = 0; |
75 |
|
|
76 |
|
#define MIN_USEFUL_BYTES 1 |
77 |
|
|
78 |
/***************************************************************************** |
/***************************************************************************** |
79 |
* Local prototypes |
* Local prototypes |
80 |
****************************************************************************/ |
****************************************************************************/ |
81 |
|
|
82 |
static double msecond(); |
static double msecond(); |
|
static int write_pgm(char *filename, |
|
|
unsigned char *image); |
|
83 |
static int dec_init(int use_assembler, int debug_level); |
static int dec_init(int use_assembler, int debug_level); |
84 |
static int dec_main(unsigned char *istream, |
static int dec_main(unsigned char *istream, |
85 |
unsigned char *ostream, |
unsigned char *ostream, |
87 |
xvid_dec_stats_t *xvid_dec_stats); |
xvid_dec_stats_t *xvid_dec_stats); |
88 |
static int dec_stop(); |
static int dec_stop(); |
89 |
static void usage(); |
static void usage(); |
90 |
int write_image(char *prefix, unsigned char *image); |
static int write_image(char *prefix, unsigned char *image); |
91 |
int write_pnm(char *filename, unsigned char *image); |
static int write_pnm(char *filename, unsigned char *image); |
92 |
int write_tga(char *filename, unsigned char *image); |
static int write_tga(char *filename, unsigned char *image); |
93 |
|
|
94 |
const char * type2str(int type) |
const char * type2str(int type) |
95 |
{ |
{ |
112 |
unsigned char *mp4_ptr = NULL; |
unsigned char *mp4_ptr = NULL; |
113 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
114 |
int useful_bytes; |
int useful_bytes; |
115 |
|
int chunk; |
116 |
xvid_dec_stats_t xvid_dec_stats; |
xvid_dec_stats_t xvid_dec_stats; |
117 |
|
|
118 |
double totaldectime; |
double totaldectime; |
246 |
totalsize = 0; |
totalsize = 0; |
247 |
filenr = 0; |
filenr = 0; |
248 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
249 |
|
chunk = 0; |
250 |
|
|
251 |
do { |
do { |
252 |
int used_bytes = 0; |
int used_bytes = 0; |
305 |
|
|
306 |
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
307 |
} |
} |
308 |
|
|
309 |
|
/* Save individual mpeg4 stream if required */ |
310 |
|
if(ARG_SAVEMPEGSTREAM) { |
311 |
|
FILE *filehandle = NULL; |
312 |
|
|
313 |
|
sprintf(filename, "%svolhdr.m4v", filepath); |
314 |
|
filehandle = fopen(filename, "wb"); |
315 |
|
if(!filehandle) { |
316 |
|
fprintf(stderr, |
317 |
|
"Error writing vol header mpeg4 stream to file %s\n", |
318 |
|
filename); |
319 |
|
} else { |
320 |
|
fwrite(mp4_ptr, 1, used_bytes, filehandle); |
321 |
|
fclose(filehandle); |
322 |
|
} |
323 |
|
} |
324 |
} |
} |
325 |
|
|
326 |
/* Update buffer pointers */ |
/* Update buffer pointers */ |
332 |
totalsize += used_bytes; |
totalsize += used_bytes; |
333 |
} |
} |
334 |
|
|
335 |
}while(xvid_dec_stats.type <= 0 && useful_bytes > 0); |
if (display_buffer_bytes) { |
336 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
337 |
|
} |
338 |
|
} while (xvid_dec_stats.type <= 0 && useful_bytes > MIN_USEFUL_BYTES); |
339 |
|
|
340 |
/* 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 |
341 |
* This means we went too far */ |
* This means we went too far */ |
346 |
totaldectime += dectime; |
totaldectime += dectime; |
347 |
|
|
348 |
|
|
349 |
|
if (!display_buffer_bytes) { |
350 |
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", |
351 |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
352 |
|
} |
353 |
|
|
354 |
/* Save individual mpeg4 stream if required */ |
/* Save individual mpeg4 stream if required */ |
355 |
if(ARG_SAVEMPEGSTREAM) { |
if(ARG_SAVEMPEGSTREAM) { |
373 |
sprintf(filename, "%sdec%05d", filepath, filenr); |
sprintf(filename, "%sdec%05d", filepath, filenr); |
374 |
if(write_image(filename, out_buffer)) { |
if(write_image(filename, out_buffer)) { |
375 |
fprintf(stderr, |
fprintf(stderr, |
376 |
"Error writing decoded PGM frame %s\n", |
"Error writing decoded frame %s\n", |
377 |
filename); |
filename); |
378 |
} |
} |
379 |
} |
} |
380 |
|
|
381 |
filenr++; |
filenr++; |
382 |
|
|
383 |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR)); |
} while (useful_bytes>MIN_USEFUL_BYTES || !feof(in_file)); |
384 |
|
|
385 |
|
useful_bytes = 0; /* Empty buffer */ |
386 |
|
|
387 |
/***************************************************************************** |
/***************************************************************************** |
388 |
* Flush decoder buffers |
* Flush decoder buffers |
398 |
dectime = msecond(); |
dectime = msecond(); |
399 |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
400 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
401 |
|
if (display_buffer_bytes) { |
402 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
403 |
|
} |
404 |
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
405 |
|
|
406 |
if (used_bytes < 0) { /* XVID_ERR_END */ |
if (used_bytes < 0) { /* XVID_ERR_END */ |
411 |
totaldectime += dectime; |
totaldectime += dectime; |
412 |
|
|
413 |
/* Prints some decoding stats */ |
/* Prints some decoding stats */ |
414 |
|
if (!display_buffer_bytes) { |
415 |
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", |
416 |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
417 |
|
} |
418 |
|
|
419 |
/* Save output frame if required */ |
/* Save output frame if required */ |
420 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
497 |
#else |
#else |
498 |
clock_t clk; |
clock_t clk; |
499 |
clk = clock(); |
clk = clock(); |
500 |
return(clk * 1000 / CLOCKS_PER_SEC); |
return(clk * 1000.0 / CLOCKS_PER_SEC); |
501 |
#endif |
#endif |
502 |
} |
} |
503 |
|
|
505 |
* output functions |
* output functions |
506 |
****************************************************************************/ |
****************************************************************************/ |
507 |
|
|
508 |
int write_image(char *prefix, unsigned char *image) |
static int write_image(char *prefix, unsigned char *image) |
509 |
{ |
{ |
510 |
char filename[1024]; |
char filename[1024]; |
511 |
char *ext; |
char *ext; |
533 |
return(ret); |
return(ret); |
534 |
} |
} |
535 |
|
|
536 |
int write_tga(char *filename, unsigned char *image) |
static int write_tga(char *filename, unsigned char *image) |
537 |
{ |
{ |
538 |
FILE * f; |
FILE * f; |
539 |
char hdr[18]; |
char hdr[18]; |
576 |
#else |
#else |
577 |
{ |
{ |
578 |
int i; |
int i; |
579 |
for (i=0; i<width*height*BPP;i+=BPP) { |
for (i=0; i<XDIM*YDIM*BPP;i+=BPP) { |
580 |
if (BPP == 1) { |
if (BPP == 1) { |
581 |
fputc(image+i, f); |
fputc(*(image+i), f); |
582 |
} else if (BPP == 2) { |
} else if (BPP == 2) { |
583 |
fputc(image+i+1, f); |
fputc(*(image+i+1), f); |
584 |
fputc(image+i+0, f); |
fputc(*(image+i+0), f); |
585 |
} else if (BPP == 3) { |
} else if (BPP == 3) { |
586 |
fputc(image+i+2, f); |
fputc(*(image+i+2), f); |
587 |
fputc(image+i+1, f); |
fputc(*(image+i+1), f); |
588 |
fputc(image+i+0, f); |
fputc(*(image+i+0), f); |
589 |
} else if (BPP == 4) { |
} else if (BPP == 4) { |
590 |
fputc(image+i+3, f); |
fputc(*(image+i+3), f); |
591 |
fputc(image+i+2, f); |
fputc(*(image+i+2), f); |
592 |
fputc(image+i+1, f); |
fputc(*(image+i+1), f); |
593 |
fputc(image+i+0, f); |
fputc(*(image+i+0), f); |
594 |
} |
} |
595 |
} |
} |
596 |
} |
} |
614 |
return(0); |
return(0); |
615 |
} |
} |
616 |
|
|
617 |
int write_pnm(char *filename, unsigned char *image) |
static int write_pnm(char *filename, unsigned char *image) |
618 |
{ |
{ |
619 |
FILE * f; |
FILE * f; |
620 |
|
|
653 |
|
|
654 |
return 0; |
return 0; |
655 |
} |
} |
656 |
|
|
657 |
/***************************************************************************** |
/***************************************************************************** |
658 |
* Routines for decoding: init decoder, use, and stop decoder |
* Routines for decoding: init decoder, use, and stop decoder |
659 |
****************************************************************************/ |
****************************************************************************/ |
667 |
xvid_gbl_init_t xvid_gbl_init; |
xvid_gbl_init_t xvid_gbl_init; |
668 |
xvid_dec_create_t xvid_dec_create; |
xvid_dec_create_t xvid_dec_create; |
669 |
|
|
670 |
|
/* Reset the structure with zeros */ |
671 |
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); |
672 |
|
memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t)); |
673 |
|
|
674 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
675 |
* XviD core initialization |
* XviD core initialization |
676 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
725 |
|
|
726 |
xvid_dec_frame_t xvid_dec_frame; |
xvid_dec_frame_t xvid_dec_frame; |
727 |
|
|
728 |
|
/* Reset all structures */ |
729 |
|
memset(&xvid_dec_frame, 0, sizeof(xvid_dec_frame_t)); |
730 |
|
memset(xvid_dec_stats, 0, sizeof(xvid_dec_stats_t)); |
731 |
|
|
732 |
/* Set version */ |
/* Set version */ |
733 |
xvid_dec_frame.version = XVID_VERSION; |
xvid_dec_frame.version = XVID_VERSION; |
734 |
xvid_dec_stats->version = XVID_VERSION; |
xvid_dec_stats->version = XVID_VERSION; |