56 |
|
|
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; |
66 |
static int CSP = XVID_CSP_I420; |
static int CSP = XVID_CSP_I420; |
67 |
static int BPP = 1; |
static int BPP = 1; |
68 |
static int FORMAT = USE_PNM; |
static int FORMAT = USE_PNM; |
69 |
|
static int POSTPROC = 0; |
70 |
|
static int ARG_THREADS = 0; |
71 |
|
|
72 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
73 |
static void *dec_handle = NULL; |
static void *dec_handle = NULL; |
90 |
xvid_dec_stats_t *xvid_dec_stats); |
xvid_dec_stats_t *xvid_dec_stats); |
91 |
static int dec_stop(); |
static int dec_stop(); |
92 |
static void usage(); |
static void usage(); |
93 |
static int write_image(char *prefix, unsigned char *image); |
static int write_image(char *prefix, unsigned char *image, int filenr); |
94 |
static int write_pnm(char *filename, unsigned char *image); |
static int write_pnm(char *filename, unsigned char *image); |
95 |
static int write_tga(char *filename, unsigned char *image); |
static int write_tga(char *filename, unsigned char *image); |
96 |
|
static int write_yuv(char *filename, unsigned char *image); |
97 |
|
|
98 |
const char * type2str(int type) |
const char * type2str(int type) |
99 |
{ |
{ |
124 |
long totalsize; |
long totalsize; |
125 |
int status; |
int status; |
126 |
|
|
127 |
int use_assembler = 0; |
int use_assembler = 1; |
128 |
int debug_level = 0; |
int debug_level = 0; |
129 |
|
|
130 |
char filename[256]; |
char filename[256]; |
134 |
int i; |
int i; |
135 |
|
|
136 |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
137 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert\n\n"); |
138 |
|
|
139 |
/***************************************************************************** |
/***************************************************************************** |
140 |
* Command line parsing |
* Command line parsing |
142 |
|
|
143 |
for (i=1; i< argc; i++) { |
for (i=1; i< argc; i++) { |
144 |
|
|
145 |
if (strcmp("-asm", argv[i]) == 0 ) { |
if (strcmp("-noasm", argv[i]) == 0 ) { |
146 |
use_assembler = 1; |
use_assembler = 0; |
147 |
} else if (strcmp("-debug", argv[i]) == 0 && i < argc - 1 ) { |
} else if (strcmp("-debug", argv[i]) == 0 && i < argc - 1 ) { |
148 |
i++; |
i++; |
149 |
if (sscanf(argv[i], "0x%x", &debug_level) != 1) { |
if (sscanf(argv[i], "0x%x", &debug_level) != 1) { |
174 |
CSP = XVID_CSP_I420; |
CSP = XVID_CSP_I420; |
175 |
BPP = 1; |
BPP = 1; |
176 |
} |
} |
177 |
|
} else if (strcmp("-postproc", argv[i]) == 0 && i < argc - 1 ) { |
178 |
|
i++; |
179 |
|
POSTPROC = atoi(argv[i]); |
180 |
|
if (POSTPROC < 0) POSTPROC = 0; |
181 |
|
if (POSTPROC > 2) POSTPROC = 2; |
182 |
} else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { |
} else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { |
183 |
i++; |
i++; |
184 |
if (strcmp(argv[i], "tga") == 0) { |
if (strcmp(argv[i], "tga") == 0) { |
185 |
FORMAT = USE_TGA; |
FORMAT = USE_TGA; |
186 |
|
} else if (strcmp(argv[i], "yuv") == 0) { |
187 |
|
FORMAT = USE_YUV; |
188 |
} else { |
} else { |
189 |
FORMAT = USE_PNM; |
FORMAT = USE_PNM; |
190 |
} |
} |
191 |
|
} else if (strcmp("-threads", argv[i]) == 0 && i < argc -1) { |
192 |
|
i++; |
193 |
|
ARG_THREADS = atoi(argv[i]); |
194 |
} else if (strcmp("-help", argv[i]) == 0) { |
} else if (strcmp("-help", argv[i]) == 0) { |
195 |
usage(); |
usage(); |
196 |
return(0); |
return(0); |
226 |
if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { |
if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { |
227 |
FORMAT = USE_TGA; |
FORMAT = USE_TGA; |
228 |
} |
} |
229 |
|
if (BPP != 1 && FORMAT == USE_YUV) { |
230 |
|
FORMAT = USE_TGA; |
231 |
|
} |
232 |
|
|
233 |
/***************************************************************************** |
/***************************************************************************** |
234 |
* Memory allocation |
* Memory allocation |
241 |
goto free_all_memory; |
goto free_all_memory; |
242 |
|
|
243 |
/***************************************************************************** |
/***************************************************************************** |
244 |
* XviD PART Start |
* Xvid PART Start |
245 |
****************************************************************************/ |
****************************************************************************/ |
246 |
|
|
247 |
status = dec_init(use_assembler, debug_level); |
status = dec_init(use_assembler, debug_level); |
284 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
285 |
|
|
286 |
/* read new data */ |
/* read new data */ |
287 |
if(feof(in_file)) |
if(!feof(in_file)) { |
|
break; |
|
|
|
|
288 |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
289 |
1, BUFFER_SIZE - already_in_buffer, |
1, BUFFER_SIZE - already_in_buffer, |
290 |
in_file); |
in_file); |
291 |
|
} |
292 |
} |
} |
293 |
|
|
294 |
|
|
385 |
|
|
386 |
/* Save output frame if required */ |
/* Save output frame if required */ |
387 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
388 |
sprintf(filename, "%sdec%05d", filepath, filenr); |
sprintf(filename, "%sdec", filepath); |
389 |
if(write_image(filename, out_buffer)) { |
|
390 |
|
if(write_image(filename, out_buffer, filenr)) { |
391 |
fprintf(stderr, |
fprintf(stderr, |
392 |
"Error writing decoded frame %s\n", |
"Error writing decoded frame %s\n", |
393 |
filename); |
filename); |
434 |
|
|
435 |
/* Save output frame if required */ |
/* Save output frame if required */ |
436 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
437 |
sprintf(filename, "%sdec%05d", filepath, filenr); |
sprintf(filename, "%sdec", filepath); |
438 |
if(write_image(filename, out_buffer)) { |
|
439 |
|
if(write_image(filename, out_buffer, filenr)) { |
440 |
fprintf(stderr, |
fprintf(stderr, |
441 |
"Error writing decoded frame %s\n", |
"Error writing decoded frame %s\n", |
442 |
filename); |
filename); |
461 |
} |
} |
462 |
|
|
463 |
/***************************************************************************** |
/***************************************************************************** |
464 |
* XviD PART Stop |
* Xvid PART Stop |
465 |
****************************************************************************/ |
****************************************************************************/ |
466 |
|
|
467 |
release_all: |
release_all: |
487 |
|
|
488 |
fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n"); |
fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n"); |
489 |
fprintf(stderr, "Options :\n"); |
fprintf(stderr, "Options :\n"); |
490 |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
fprintf(stderr, " -noasm : don't use assembly optimizations (default=enabled)\n"); |
491 |
fprintf(stderr, " -debug : debug level (debug=0)\n"); |
fprintf(stderr, " -debug : debug level (debug=0)\n"); |
492 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
493 |
fprintf(stderr, " -d : save decoder output\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
494 |
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"); |
495 |
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"); |
496 |
|
fprintf(stderr, " -postproc : postprocessing level (0=off, 1=deblock, 2=deblock+dering)\n"); |
497 |
|
fprintf(stderr, " -threads int : number of threads\n"); |
498 |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
499 |
fprintf(stderr, " -help : This help message\n"); |
fprintf(stderr, " -help : This help message\n"); |
500 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
524 |
* output functions |
* output functions |
525 |
****************************************************************************/ |
****************************************************************************/ |
526 |
|
|
527 |
static int write_image(char *prefix, unsigned char *image) |
static int write_image(char *prefix, unsigned char *image, int filenr) |
528 |
{ |
{ |
529 |
char filename[1024]; |
char filename[1024]; |
530 |
char *ext; |
char *ext; |
534 |
ext = "pgm"; |
ext = "pgm"; |
535 |
} else if (FORMAT == USE_PNM && BPP == 3) { |
} else if (FORMAT == USE_PNM && BPP == 3) { |
536 |
ext = "pnm"; |
ext = "pnm"; |
537 |
|
} else if (FORMAT == USE_YUV) { |
538 |
|
ext = "yuv"; |
539 |
} else if (FORMAT == USE_TGA) { |
} else if (FORMAT == USE_TGA) { |
540 |
ext = "tga"; |
ext = "tga"; |
541 |
} else { |
} else { |
543 |
exit(-1); |
exit(-1); |
544 |
} |
} |
545 |
|
|
546 |
|
if (FORMAT == USE_YUV) { |
547 |
sprintf(filename, "%s.%s", prefix, ext); |
sprintf(filename, "%s.%s", prefix, ext); |
548 |
|
|
549 |
|
if (!filenr) { |
550 |
|
FILE *fp = fopen(filename, "wb"); |
551 |
|
fclose(fp); |
552 |
|
} |
553 |
|
} else |
554 |
|
sprintf(filename, "%s%05d.%s", prefix, filenr, ext); |
555 |
|
|
556 |
if (FORMAT == USE_PNM) { |
if (FORMAT == USE_PNM) { |
557 |
ret = write_pnm(filename, image); |
ret = write_pnm(filename, image); |
558 |
|
} else if (FORMAT == USE_YUV) { |
559 |
|
ret = write_yuv(filename, image); |
560 |
} else { |
} else { |
561 |
ret = write_tga(filename, image); |
ret = write_tga(filename, image); |
562 |
} |
} |
656 |
|
|
657 |
if (BPP == 1) { |
if (BPP == 1) { |
658 |
int i; |
int i; |
659 |
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); |
660 |
|
|
661 |
fwrite(image, 1, XDIM*YDIM, f); |
fwrite(image, 1, XDIM*YDIM, f); |
662 |
|
|
685 |
return 0; |
return 0; |
686 |
} |
} |
687 |
|
|
688 |
|
static int write_yuv(char *filename, unsigned char *image) |
689 |
|
{ |
690 |
|
FILE * f; |
691 |
|
|
692 |
|
f = fopen(filename, "ab+"); |
693 |
|
if ( f == NULL) { |
694 |
|
return -1; |
695 |
|
} |
696 |
|
|
697 |
|
fwrite(image, 1, 3*XDIM*YDIM/2, f); |
698 |
|
|
699 |
|
fclose(f); |
700 |
|
|
701 |
|
return 0; |
702 |
|
} |
703 |
|
|
704 |
/***************************************************************************** |
/***************************************************************************** |
705 |
* Routines for decoding: init decoder, use, and stop decoder |
* Routines for decoding: init decoder, use, and stop decoder |
706 |
****************************************************************************/ |
****************************************************************************/ |
713 |
|
|
714 |
xvid_gbl_init_t xvid_gbl_init; |
xvid_gbl_init_t xvid_gbl_init; |
715 |
xvid_dec_create_t xvid_dec_create; |
xvid_dec_create_t xvid_dec_create; |
716 |
|
xvid_gbl_info_t xvid_gbl_info; |
717 |
|
|
718 |
/* Reset the structure with zeros */ |
/* Reset the structure with zeros */ |
719 |
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); |
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); |
720 |
memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t)); |
memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t)); |
721 |
|
memset(&xvid_gbl_info, 0, sizeof(xvid_gbl_info)); |
722 |
|
|
723 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
724 |
* XviD core initialization |
* Xvid core initialization |
725 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
726 |
|
|
727 |
|
xvid_gbl_info.version = XVID_VERSION; |
728 |
|
xvid_global(NULL, XVID_GBL_INFO, &xvid_gbl_info, NULL); |
729 |
|
|
730 |
|
if (xvid_gbl_info.build != NULL) { |
731 |
|
fprintf(stderr, "xvidcore build version: %s\n", xvid_gbl_info.build); |
732 |
|
} |
733 |
|
fprintf(stderr, "Bitstream version: %d.%d.%d\n", XVID_VERSION_MAJOR(xvid_gbl_info.actual_version), XVID_VERSION_MINOR(xvid_gbl_info.actual_version), XVID_VERSION_PATCH(xvid_gbl_info.actual_version)); |
734 |
|
fprintf(stderr, "Detected CPU flags: "); |
735 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_ASM) |
736 |
|
fprintf(stderr, "ASM "); |
737 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_MMX) |
738 |
|
fprintf(stderr, "MMX "); |
739 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_MMXEXT) |
740 |
|
fprintf(stderr, "MMXEXT "); |
741 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE) |
742 |
|
fprintf(stderr, "SSE "); |
743 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE2) |
744 |
|
fprintf(stderr, "SSE2 "); |
745 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE3) |
746 |
|
fprintf(stderr, "SSE3 "); |
747 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_SSE41) |
748 |
|
fprintf(stderr, "SSE41 "); |
749 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOW) |
750 |
|
fprintf(stderr, "3DNOW "); |
751 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_3DNOWEXT) |
752 |
|
fprintf(stderr, "3DNOWEXT "); |
753 |
|
if (xvid_gbl_info.cpu_flags & XVID_CPU_TSC) |
754 |
|
fprintf(stderr, "TSC "); |
755 |
|
fprintf(stderr, "\n"); |
756 |
|
fprintf(stderr, "Detected %d cpus,", xvid_gbl_info.num_threads); |
757 |
|
if (!ARG_THREADS) ARG_THREADS = xvid_gbl_info.num_threads; |
758 |
|
fprintf(stderr, " using %d threads.\n", ARG_THREADS); |
759 |
|
|
760 |
/* Version */ |
/* Version */ |
761 |
xvid_gbl_init.version = XVID_VERSION; |
xvid_gbl_init.version = XVID_VERSION; |
762 |
|
|
775 |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
776 |
|
|
777 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
778 |
* XviD encoder initialization |
* Xvid decoder initialization |
779 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
780 |
|
|
781 |
/* Version */ |
/* Version */ |
788 |
xvid_dec_create.width = 0; |
xvid_dec_create.width = 0; |
789 |
xvid_dec_create.height = 0; |
xvid_dec_create.height = 0; |
790 |
|
|
791 |
|
xvid_dec_create.num_threads = ARG_THREADS; |
792 |
|
|
793 |
ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL); |
ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL); |
794 |
|
|
795 |
dec_handle = xvid_dec_create.handle; |
dec_handle = xvid_dec_create.handle; |
818 |
xvid_dec_stats->version = XVID_VERSION; |
xvid_dec_stats->version = XVID_VERSION; |
819 |
|
|
820 |
/* No general flags to set */ |
/* No general flags to set */ |
821 |
|
if (POSTPROC == 1) |
822 |
|
xvid_dec_frame.general = XVID_DEBLOCKY | XVID_DEBLOCKUV; |
823 |
|
else if (POSTPROC==2) |
824 |
|
xvid_dec_frame.general = XVID_DEBLOCKY | XVID_DEBLOCKUV | XVID_DERINGY | XVID_DERINGUV; |
825 |
|
else |
826 |
xvid_dec_frame.general = 0; |
xvid_dec_frame.general = 0; |
827 |
|
|
828 |
/* Input stream */ |
/* Input stream */ |