20 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
22 |
* |
* |
23 |
* $Id: xvid_decraw.c,v 1.10.2.1 2004-04-07 22:02:56 edgomez Exp $ |
* $Id: xvid_decraw.c,v 1.10.2.4 2004-04-15 22:39:30 edgomez Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
57 |
/* max number of frames */ |
/* max number of frames */ |
58 |
#define ABS_MAXFRAMENR 9999 |
#define ABS_MAXFRAMENR 9999 |
59 |
|
|
60 |
|
#define USE_PNM 0 |
61 |
|
#define USE_TGA 1 |
62 |
|
|
63 |
static int XDIM = 0; |
static int XDIM = 0; |
64 |
static int YDIM = 0; |
static int YDIM = 0; |
65 |
static int ARG_SAVEDECOUTPUT = 0; |
static int ARG_SAVEDECOUTPUT = 0; |
67 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
68 |
static int CSP = XVID_CSP_I420; |
static int CSP = XVID_CSP_I420; |
69 |
static int BPP = 1; |
static int BPP = 1; |
70 |
|
static int FORMAT = USE_PNM; |
71 |
|
|
72 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
73 |
static void *dec_handle = NULL; |
static void *dec_handle = NULL; |
79 |
****************************************************************************/ |
****************************************************************************/ |
80 |
|
|
81 |
static double msecond(); |
static double msecond(); |
|
static int write_pgm(char *filename, |
|
|
unsigned char *image); |
|
82 |
static int dec_init(int use_assembler); |
static int dec_init(int use_assembler); |
83 |
static int dec_main(unsigned char *istream, |
static int dec_main(unsigned char *istream, |
84 |
unsigned char *ostream, |
unsigned char *ostream, |
86 |
xvid_dec_stats_t *xvid_dec_stats); |
xvid_dec_stats_t *xvid_dec_stats); |
87 |
static int dec_stop(); |
static int dec_stop(); |
88 |
static void usage(); |
static void usage(); |
89 |
|
static int write_image(char *prefix, unsigned char *image); |
90 |
|
static int write_pnm(char *filename, unsigned char *image); |
91 |
|
static int write_tga(char *filename, unsigned char *image); |
92 |
|
|
93 |
const char * type2str(int type) |
const char * type2str(int type) |
94 |
{ |
{ |
162 |
CSP = XVID_CSP_I420; |
CSP = XVID_CSP_I420; |
163 |
BPP = 1; |
BPP = 1; |
164 |
} |
} |
165 |
|
} else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { |
166 |
|
i++; |
167 |
|
if (strcmp(argv[i], "tga") == 0) { |
168 |
|
FORMAT = USE_TGA; |
169 |
|
} else { |
170 |
|
FORMAT = USE_PNM; |
171 |
|
} |
172 |
} else if (strcmp("-help", argv[i]) == 0) { |
} else if (strcmp("-help", argv[i]) == 0) { |
173 |
usage(); |
usage(); |
174 |
return(0); |
return(0); |
178 |
} |
} |
179 |
} |
} |
180 |
|
|
181 |
|
#if defined(_MSC_VER) |
182 |
|
if (ARG_INPUTFILE==NULL) { |
183 |
|
fprintf(stderr, "Warning: MSVC build does not read EOF correctly from stdin. Use the -i switch.\n\n"); |
184 |
|
} |
185 |
|
#endif |
186 |
|
|
187 |
/***************************************************************************** |
/***************************************************************************** |
188 |
* Values checking |
* Values checking |
189 |
****************************************************************************/ |
****************************************************************************/ |
200 |
} |
} |
201 |
} |
} |
202 |
|
|
203 |
|
/* PNM/PGM format can't handle 16/32 bit data */ |
204 |
|
if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { |
205 |
|
FORMAT = USE_TGA; |
206 |
|
} |
207 |
|
|
208 |
/***************************************************************************** |
/***************************************************************************** |
209 |
* Memory allocation |
* Memory allocation |
210 |
****************************************************************************/ |
****************************************************************************/ |
340 |
|
|
341 |
/* Save output frame if required */ |
/* Save output frame if required */ |
342 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
343 |
sprintf(filename, "%sdec%05d.tga", filepath, filenr); |
sprintf(filename, "%sdec%05d", filepath, filenr); |
344 |
if(write_tga(filename,out_buffer)) { |
if(write_image(filename, out_buffer)) { |
345 |
fprintf(stderr, |
fprintf(stderr, |
346 |
"Error writing decoded PGM frame %s\n", |
"Error writing decoded frame %s\n", |
347 |
filename); |
filename); |
348 |
} |
} |
349 |
} |
} |
381 |
|
|
382 |
/* Save output frame if required */ |
/* Save output frame if required */ |
383 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
384 |
sprintf(filename, "%sdec%05d.tga", filepath, filenr); |
sprintf(filename, "%sdec%05d", filepath, filenr); |
385 |
if(write_tga(filename, out_buffer)) { |
if(write_image(filename, out_buffer)) { |
386 |
fprintf(stderr, |
fprintf(stderr, |
387 |
"Error writing decoded PGM frame %s\n", |
"Error writing decoded frame %s\n", |
388 |
filename); |
filename); |
389 |
} |
} |
390 |
} |
} |
397 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
398 |
****************************************************************************/ |
****************************************************************************/ |
399 |
|
|
400 |
|
if (filenr>0) { |
401 |
totalsize /= filenr; |
totalsize /= filenr; |
402 |
totaldectime /= filenr; |
totaldectime /= filenr; |
|
|
|
403 |
printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", |
printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", |
404 |
totaldectime, 1000/totaldectime, (int)totalsize); |
totaldectime, 1000/totaldectime, (int)totalsize); |
405 |
|
}else{ |
406 |
|
printf("Nothing was decoded!\n"); |
407 |
|
} |
408 |
|
|
409 |
/***************************************************************************** |
/***************************************************************************** |
410 |
* XviD PART Stop |
* XviD PART Stop |
436 |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
437 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
438 |
fprintf(stderr, " -d : save decoder output\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
439 |
fprintf(stderr, " -c csp : choose colorspace output (csp can be one of rgb16, rgb24, rgb32, yv12, i420)\n"); |
fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); |
440 |
|
fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm)\n"); |
441 |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
442 |
fprintf(stderr, " -help : This help message\n"); |
fprintf(stderr, " -help : This help message\n"); |
443 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
467 |
* output functions |
* output functions |
468 |
****************************************************************************/ |
****************************************************************************/ |
469 |
|
|
470 |
int write_tga(char *filename, unsigned char *image) |
static int write_image(char *prefix, unsigned char *image) |
471 |
|
{ |
472 |
|
char filename[1024]; |
473 |
|
char *ext; |
474 |
|
int ret; |
475 |
|
|
476 |
|
if (FORMAT == USE_PNM && BPP == 1) { |
477 |
|
ext = "pgm"; |
478 |
|
} else if (FORMAT == USE_PNM && BPP == 3) { |
479 |
|
ext = "pnm"; |
480 |
|
} else if (FORMAT == USE_TGA) { |
481 |
|
ext = "tga"; |
482 |
|
} else { |
483 |
|
fprintf(stderr, "Bug: should not reach this path code -- please report to xvid-devel@xvid.org with command line options used"); |
484 |
|
exit(-1); |
485 |
|
} |
486 |
|
|
487 |
|
sprintf(filename, "%s.%s", prefix, ext); |
488 |
|
|
489 |
|
if (FORMAT == USE_PNM) { |
490 |
|
ret = write_pnm(filename, image); |
491 |
|
} else { |
492 |
|
ret = write_tga(filename, image); |
493 |
|
} |
494 |
|
|
495 |
|
return(ret); |
496 |
|
} |
497 |
|
|
498 |
|
static int write_tga(char *filename, unsigned char *image) |
499 |
{ |
{ |
500 |
FILE * f; |
FILE * f; |
501 |
char hdr[18]; |
char hdr[18]; |
564 |
|
|
565 |
/* Write the two chrominance planes */ |
/* Write the two chrominance planes */ |
566 |
for (i=0; i<YDIM/2; i++) { |
for (i=0; i<YDIM/2; i++) { |
567 |
fwrite(image+XDIM*YDIM+XDIM/2*i, 1, XDIM/2, f); |
fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f); |
568 |
fwrite(image+5*XDIM*YDIM/4 + XDIM/2*i, 1, XDIM/2, f); |
fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f); |
569 |
} |
} |
570 |
} |
} |
571 |
|
|
576 |
return(0); |
return(0); |
577 |
} |
} |
578 |
|
|
579 |
|
static int write_pnm(char *filename, unsigned char *image) |
580 |
|
{ |
581 |
|
FILE * f; |
582 |
|
|
583 |
|
f = fopen(filename, "wb"); |
584 |
|
if ( f == NULL) { |
585 |
|
return -1; |
586 |
|
} |
587 |
|
|
588 |
|
if (BPP == 1) { |
589 |
|
int i; |
590 |
|
fprintf(f, "P5\n#xvid\n%i %i\n255\n", XDIM, YDIM*3/2); |
591 |
|
|
592 |
|
fwrite(image, 1, XDIM*YDIM, f); |
593 |
|
|
594 |
|
for (i=0; i<YDIM/2;i++) { |
595 |
|
fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f); |
596 |
|
fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f); |
597 |
|
} |
598 |
|
} else if (BPP == 3) { |
599 |
|
int i; |
600 |
|
fprintf(f, "P6\n#xvid\n%i %i\n255\n", XDIM, YDIM); |
601 |
|
for (i=0; i<XDIM*YDIM*3; i+=3) { |
602 |
|
#ifdef ARCH_IS_LITTLE_ENDIAN |
603 |
|
fputc(image[i+2], f); |
604 |
|
fputc(image[i+1], f); |
605 |
|
fputc(image[i+0], f); |
606 |
|
#else |
607 |
|
fputc(image[i+0], f); |
608 |
|
fputc(image[i+1], f); |
609 |
|
fputc(image[i+2], f); |
610 |
|
#endif |
611 |
|
} |
612 |
|
} |
613 |
|
|
614 |
|
fclose(f); |
615 |
|
|
616 |
|
return 0; |
617 |
|
} |
618 |
|
|
619 |
/***************************************************************************** |
/***************************************************************************** |
620 |
* Routines for decoding: init decoder, use, and stop decoder |
* Routines for decoding: init decoder, use, and stop decoder |
621 |
****************************************************************************/ |
****************************************************************************/ |