54 |
* Global vars in module and constants |
* Global vars in module and constants |
55 |
****************************************************************************/ |
****************************************************************************/ |
56 |
|
|
57 |
/* max number of frames */ |
#define USE_PNM 0 |
58 |
#define ABS_MAXFRAMENR 9999 |
#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; |
63 |
static int ARG_SAVEDECOUTPUT = 0; |
static int ARG_SAVEDECOUTPUT = 0; |
64 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
65 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
66 |
|
static int CSP = XVID_CSP_I420; |
67 |
|
static int BPP = 1; |
68 |
|
static int FORMAT = USE_PNM; |
69 |
|
static int POSTPROC = 0; |
70 |
|
|
71 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
72 |
static void *dec_handle = NULL; |
static void *dec_handle = NULL; |
73 |
|
|
74 |
#define BUFFER_SIZE (2*1024*1024) |
#define BUFFER_SIZE (2*1024*1024) |
75 |
|
|
76 |
|
static const int display_buffer_bytes = 0; |
77 |
|
|
78 |
|
#define MIN_USEFUL_BYTES 1 |
79 |
|
|
80 |
/***************************************************************************** |
/***************************************************************************** |
81 |
* Local prototypes |
* Local prototypes |
82 |
****************************************************************************/ |
****************************************************************************/ |
83 |
|
|
84 |
static double msecond(); |
static double msecond(); |
85 |
static int write_pgm(char *filename, |
static int dec_init(int use_assembler, int debug_level); |
|
unsigned char *image); |
|
|
static int dec_init(int use_assembler); |
|
86 |
static int dec_main(unsigned char *istream, |
static int dec_main(unsigned char *istream, |
87 |
unsigned char *ostream, |
unsigned char *ostream, |
88 |
int istream_size, |
int istream_size, |
89 |
xvid_dec_stats_t *xvid_dec_stats); |
xvid_dec_stats_t *xvid_dec_stats); |
90 |
static int dec_stop(); |
static int dec_stop(); |
91 |
static void usage(); |
static void usage(); |
92 |
|
static int write_image(char *prefix, unsigned char *image, int filenr); |
93 |
|
static int write_pnm(char *filename, unsigned char *image); |
94 |
|
static int write_tga(char *filename, unsigned char *image); |
95 |
|
static int write_yuv(char *filename, unsigned char *image); |
96 |
|
|
97 |
const char * type2str(int type) |
const char * type2str(int type) |
98 |
{ |
{ |
115 |
unsigned char *mp4_ptr = NULL; |
unsigned char *mp4_ptr = NULL; |
116 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
117 |
int useful_bytes; |
int useful_bytes; |
118 |
|
int chunk; |
119 |
xvid_dec_stats_t xvid_dec_stats; |
xvid_dec_stats_t xvid_dec_stats; |
120 |
|
|
121 |
double totaldectime; |
double totaldectime; |
123 |
long totalsize; |
long totalsize; |
124 |
int status; |
int status; |
125 |
|
|
126 |
int use_assembler = 0; |
int use_assembler = 1; |
127 |
|
int debug_level = 0; |
128 |
|
|
129 |
char filename[256]; |
char filename[256]; |
130 |
|
|
133 |
int i; |
int i; |
134 |
|
|
135 |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
136 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert\n\n"); |
137 |
|
|
138 |
/***************************************************************************** |
/***************************************************************************** |
139 |
* Command line parsing |
* Command line parsing |
141 |
|
|
142 |
for (i=1; i< argc; i++) { |
for (i=1; i< argc; i++) { |
143 |
|
|
144 |
if (strcmp("-asm", argv[i]) == 0 ) { |
if (strcmp("-noasm", argv[i]) == 0 ) { |
145 |
use_assembler = 1; |
use_assembler = 0; |
146 |
|
} else if (strcmp("-debug", argv[i]) == 0 && i < argc - 1 ) { |
147 |
|
i++; |
148 |
|
if (sscanf(argv[i], "0x%x", &debug_level) != 1) { |
149 |
|
debug_level = atoi(argv[i]); |
150 |
|
} |
151 |
} else if (strcmp("-d", argv[i]) == 0) { |
} else if (strcmp("-d", argv[i]) == 0) { |
152 |
ARG_SAVEDECOUTPUT = 1; |
ARG_SAVEDECOUTPUT = 1; |
153 |
} else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
} else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
155 |
ARG_INPUTFILE = argv[i]; |
ARG_INPUTFILE = argv[i]; |
156 |
} else if (strcmp("-m", argv[i]) == 0) { |
} else if (strcmp("-m", argv[i]) == 0) { |
157 |
ARG_SAVEMPEGSTREAM = 1; |
ARG_SAVEMPEGSTREAM = 1; |
158 |
|
} else if (strcmp("-c", argv[i]) == 0 && i < argc - 1 ) { |
159 |
|
i++; |
160 |
|
if (strcmp(argv[i], "rgb16") == 0) { |
161 |
|
CSP = XVID_CSP_RGB555; |
162 |
|
BPP = 2; |
163 |
|
} else if (strcmp(argv[i], "rgb24") == 0) { |
164 |
|
CSP = XVID_CSP_BGR; |
165 |
|
BPP = 3; |
166 |
|
} else if (strcmp(argv[i], "rgb32") == 0) { |
167 |
|
CSP = XVID_CSP_BGRA; |
168 |
|
BPP = 4; |
169 |
|
} else if (strcmp(argv[i], "yv12") == 0) { |
170 |
|
CSP = XVID_CSP_YV12; |
171 |
|
BPP = 1; |
172 |
|
} else { |
173 |
|
CSP = XVID_CSP_I420; |
174 |
|
BPP = 1; |
175 |
|
} |
176 |
|
} else if (strcmp("-postproc", argv[i]) == 0 && i < argc - 1 ) { |
177 |
|
i++; |
178 |
|
POSTPROC = atoi(argv[i]); |
179 |
|
if (POSTPROC < 0) POSTPROC = 0; |
180 |
|
if (POSTPROC > 2) POSTPROC = 2; |
181 |
|
} else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { |
182 |
|
i++; |
183 |
|
if (strcmp(argv[i], "tga") == 0) { |
184 |
|
FORMAT = USE_TGA; |
185 |
|
} else if (strcmp(argv[i], "yuv") == 0) { |
186 |
|
FORMAT = USE_YUV; |
187 |
|
} else { |
188 |
|
FORMAT = USE_PNM; |
189 |
|
} |
190 |
} else if (strcmp("-help", argv[i]) == 0) { |
} else if (strcmp("-help", argv[i]) == 0) { |
191 |
usage(); |
usage(); |
192 |
return(0); |
return(0); |
196 |
} |
} |
197 |
} |
} |
198 |
|
|
199 |
|
#if defined(_MSC_VER) |
200 |
|
if (ARG_INPUTFILE==NULL) { |
201 |
|
fprintf(stderr, "Warning: MSVC build does not read EOF correctly from stdin. Use the -i switch.\n\n"); |
202 |
|
} |
203 |
|
#endif |
204 |
|
|
205 |
/***************************************************************************** |
/***************************************************************************** |
206 |
* Values checking |
* Values checking |
207 |
****************************************************************************/ |
****************************************************************************/ |
218 |
} |
} |
219 |
} |
} |
220 |
|
|
221 |
|
/* PNM/PGM format can't handle 16/32 bit data */ |
222 |
|
if (BPP != 1 && BPP != 3 && FORMAT == USE_PNM) { |
223 |
|
FORMAT = USE_TGA; |
224 |
|
} |
225 |
|
if (BPP != 1 && FORMAT == USE_YUV) { |
226 |
|
FORMAT = USE_TGA; |
227 |
|
} |
228 |
|
|
229 |
/***************************************************************************** |
/***************************************************************************** |
230 |
* Memory allocation |
* Memory allocation |
231 |
****************************************************************************/ |
****************************************************************************/ |
237 |
goto free_all_memory; |
goto free_all_memory; |
238 |
|
|
239 |
/***************************************************************************** |
/***************************************************************************** |
240 |
* XviD PART Start |
* Xvid PART Start |
241 |
****************************************************************************/ |
****************************************************************************/ |
242 |
|
|
243 |
status = dec_init(use_assembler); |
status = dec_init(use_assembler, debug_level); |
244 |
if (status) { |
if (status) { |
245 |
fprintf(stderr, |
fprintf(stderr, |
246 |
"Decore INIT problem, return value %d\n", status); |
"Decore INIT problem, return value %d\n", status); |
259 |
totalsize = 0; |
totalsize = 0; |
260 |
filenr = 0; |
filenr = 0; |
261 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
262 |
|
chunk = 0; |
263 |
|
|
264 |
do { |
do { |
265 |
int used_bytes = 0; |
int used_bytes = 0; |
280 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
281 |
|
|
282 |
/* read new data */ |
/* read new data */ |
283 |
if(feof(in_file)) |
if(!feof(in_file)) { |
|
break; |
|
|
|
|
284 |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
285 |
1, BUFFER_SIZE - already_in_buffer, |
1, BUFFER_SIZE - already_in_buffer, |
286 |
in_file); |
in_file); |
287 |
|
} |
288 |
} |
} |
289 |
|
|
290 |
|
|
316 |
|
|
317 |
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); |
318 |
} |
} |
319 |
|
|
320 |
|
/* Save individual mpeg4 stream if required */ |
321 |
|
if(ARG_SAVEMPEGSTREAM) { |
322 |
|
FILE *filehandle = NULL; |
323 |
|
|
324 |
|
sprintf(filename, "%svolhdr.m4v", filepath); |
325 |
|
filehandle = fopen(filename, "wb"); |
326 |
|
if(!filehandle) { |
327 |
|
fprintf(stderr, |
328 |
|
"Error writing vol header mpeg4 stream to file %s\n", |
329 |
|
filename); |
330 |
|
} else { |
331 |
|
fwrite(mp4_ptr, 1, used_bytes, filehandle); |
332 |
|
fclose(filehandle); |
333 |
|
} |
334 |
|
} |
335 |
} |
} |
336 |
|
|
337 |
/* Update buffer pointers */ |
/* Update buffer pointers */ |
343 |
totalsize += used_bytes; |
totalsize += used_bytes; |
344 |
} |
} |
345 |
|
|
346 |
}while(xvid_dec_stats.type <= 0 && useful_bytes > 0); |
if (display_buffer_bytes) { |
347 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
348 |
|
} |
349 |
|
} while (xvid_dec_stats.type <= 0 && useful_bytes > MIN_USEFUL_BYTES); |
350 |
|
|
351 |
/* 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 |
352 |
* This means we went too far */ |
* This means we went too far */ |
357 |
totaldectime += dectime; |
totaldectime += dectime; |
358 |
|
|
359 |
|
|
360 |
|
if (!display_buffer_bytes) { |
361 |
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", |
362 |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
363 |
|
} |
364 |
|
|
365 |
/* Save individual mpeg4 stream if required */ |
/* Save individual mpeg4 stream if required */ |
366 |
if(ARG_SAVEMPEGSTREAM) { |
if(ARG_SAVEMPEGSTREAM) { |
374 |
filename); |
filename); |
375 |
} |
} |
376 |
else { |
else { |
377 |
fwrite(mp4_buffer, 1, used_bytes, filehandle); |
fwrite(mp4_ptr-used_bytes, 1, used_bytes, filehandle); |
378 |
fclose(filehandle); |
fclose(filehandle); |
379 |
} |
} |
380 |
} |
} |
381 |
|
|
382 |
/* Save output frame if required */ |
/* Save output frame if required */ |
383 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
384 |
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
sprintf(filename, "%sdec", filepath); |
385 |
if(write_pgm(filename,out_buffer)) { |
|
386 |
|
if(write_image(filename, out_buffer, filenr)) { |
387 |
fprintf(stderr, |
fprintf(stderr, |
388 |
"Error writing decoded PGM frame %s\n", |
"Error writing decoded frame %s\n", |
389 |
filename); |
filename); |
390 |
} |
} |
391 |
} |
} |
392 |
|
|
393 |
filenr++; |
filenr++; |
394 |
|
|
395 |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR)); |
} while (useful_bytes>MIN_USEFUL_BYTES || !feof(in_file)); |
396 |
|
|
397 |
|
useful_bytes = 0; /* Empty buffer */ |
398 |
|
|
399 |
/***************************************************************************** |
/***************************************************************************** |
400 |
* Flush decoder buffers |
* Flush decoder buffers |
410 |
dectime = msecond(); |
dectime = msecond(); |
411 |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
412 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
413 |
|
if (display_buffer_bytes) { |
414 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
415 |
|
} |
416 |
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
417 |
|
|
418 |
if (used_bytes < 0) { /* XVID_ERR_END */ |
if (used_bytes < 0) { /* XVID_ERR_END */ |
423 |
totaldectime += dectime; |
totaldectime += dectime; |
424 |
|
|
425 |
/* Prints some decoding stats */ |
/* Prints some decoding stats */ |
426 |
|
if (!display_buffer_bytes) { |
427 |
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", |
428 |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
429 |
|
} |
430 |
|
|
431 |
/* Save output frame if required */ |
/* Save output frame if required */ |
432 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
433 |
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
sprintf(filename, "%sdec", filepath); |
434 |
if(write_pgm(filename, out_buffer)) { |
|
435 |
|
if(write_image(filename, out_buffer, filenr)) { |
436 |
fprintf(stderr, |
fprintf(stderr, |
437 |
"Error writing decoded PGM frame %s\n", |
"Error writing decoded frame %s\n", |
438 |
filename); |
filename); |
439 |
} |
} |
440 |
} |
} |
447 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
448 |
****************************************************************************/ |
****************************************************************************/ |
449 |
|
|
450 |
|
if (filenr>0) { |
451 |
totalsize /= filenr; |
totalsize /= filenr; |
452 |
totaldectime /= filenr; |
totaldectime /= filenr; |
|
|
|
453 |
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", |
454 |
totaldectime, 1000/totaldectime, (int)totalsize); |
totaldectime, 1000/totaldectime, (int)totalsize); |
455 |
|
}else{ |
456 |
|
printf("Nothing was decoded!\n"); |
457 |
|
} |
458 |
|
|
459 |
/***************************************************************************** |
/***************************************************************************** |
460 |
* XviD PART Stop |
* Xvid PART Stop |
461 |
****************************************************************************/ |
****************************************************************************/ |
462 |
|
|
463 |
release_all: |
release_all: |
483 |
|
|
484 |
fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n"); |
fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n"); |
485 |
fprintf(stderr, "Options :\n"); |
fprintf(stderr, "Options :\n"); |
486 |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
fprintf(stderr, " -noasm : don't use assembly optimizations (default=enabled)\n"); |
487 |
|
fprintf(stderr, " -debug : debug level (debug=0)\n"); |
488 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
489 |
fprintf(stderr, " -d : save decoder output\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
490 |
|
fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); |
491 |
|
fprintf(stderr, " -f format : choose output file format (tga, pnm, pgm, yuv)\n"); |
492 |
|
fprintf(stderr, " -postproc : postprocessing level (0=off, 1=deblock, 2=deblock+dering)\n"); |
493 |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
494 |
fprintf(stderr, " -help : This help message\n"); |
fprintf(stderr, " -help : This help message\n"); |
495 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
511 |
#else |
#else |
512 |
clock_t clk; |
clock_t clk; |
513 |
clk = clock(); |
clk = clock(); |
514 |
return(clk * 1000 / CLOCKS_PER_SEC); |
return(clk * 1000.0 / CLOCKS_PER_SEC); |
515 |
#endif |
#endif |
516 |
} |
} |
517 |
|
|
519 |
* output functions |
* output functions |
520 |
****************************************************************************/ |
****************************************************************************/ |
521 |
|
|
522 |
static int |
static int write_image(char *prefix, unsigned char *image, int filenr) |
523 |
write_pgm(char *filename, |
{ |
524 |
unsigned char *image) |
char filename[1024]; |
525 |
|
char *ext; |
526 |
|
int ret; |
527 |
|
|
528 |
|
if (FORMAT == USE_PNM && BPP == 1) { |
529 |
|
ext = "pgm"; |
530 |
|
} else if (FORMAT == USE_PNM && BPP == 3) { |
531 |
|
ext = "pnm"; |
532 |
|
} else if (FORMAT == USE_YUV) { |
533 |
|
ext = "yuv"; |
534 |
|
} else if (FORMAT == USE_TGA) { |
535 |
|
ext = "tga"; |
536 |
|
} else { |
537 |
|
fprintf(stderr, "Bug: should not reach this path code -- please report to xvid-devel@xvid.org with command line options used"); |
538 |
|
exit(-1); |
539 |
|
} |
540 |
|
|
541 |
|
if (FORMAT == USE_YUV) { |
542 |
|
sprintf(filename, "%s.%s", prefix, ext); |
543 |
|
|
544 |
|
if (!filenr) { |
545 |
|
FILE *fp = fopen(filename, "wb"); |
546 |
|
fclose(fp); |
547 |
|
} |
548 |
|
} else |
549 |
|
sprintf(filename, "%s%05d.%s", prefix, filenr, ext); |
550 |
|
|
551 |
|
if (FORMAT == USE_PNM) { |
552 |
|
ret = write_pnm(filename, image); |
553 |
|
} else if (FORMAT == USE_YUV) { |
554 |
|
ret = write_yuv(filename, image); |
555 |
|
} else { |
556 |
|
ret = write_tga(filename, image); |
557 |
|
} |
558 |
|
|
559 |
|
return(ret); |
560 |
|
} |
561 |
|
|
562 |
|
static int write_tga(char *filename, unsigned char *image) |
563 |
{ |
{ |
564 |
int loop; |
FILE * f; |
565 |
|
char hdr[18]; |
566 |
|
|
567 |
unsigned char *y = image; |
f = fopen(filename, "wb"); |
568 |
unsigned char *u = image + XDIM*YDIM; |
if ( f == NULL) { |
569 |
unsigned char *v = image + XDIM*YDIM + XDIM/2*YDIM/2; |
return -1; |
570 |
|
} |
571 |
FILE *filehandle; |
|
572 |
filehandle=fopen(filename,"w+b"); |
hdr[0] = 0; /* ID length */ |
573 |
if (filehandle) { |
hdr[1] = 0; /* Color map type */ |
574 |
|
hdr[2] = (BPP>1)?2:3; /* Uncompressed true color (2) or greymap (3) */ |
575 |
|
hdr[3] = 0; /* Color map specification (not used) */ |
576 |
|
hdr[4] = 0; /* Color map specification (not used) */ |
577 |
|
hdr[5] = 0; /* Color map specification (not used) */ |
578 |
|
hdr[6] = 0; /* Color map specification (not used) */ |
579 |
|
hdr[7] = 0; /* Color map specification (not used) */ |
580 |
|
hdr[8] = 0; /* LSB X origin */ |
581 |
|
hdr[9] = 0; /* MSB X origin */ |
582 |
|
hdr[10] = 0; /* LSB Y origin */ |
583 |
|
hdr[11] = 0; /* MSB Y origin */ |
584 |
|
hdr[12] = (XDIM>>0)&0xff; /* LSB Width */ |
585 |
|
hdr[13] = (XDIM>>8)&0xff; /* MSB Width */ |
586 |
|
if (BPP > 1) { |
587 |
|
hdr[14] = (YDIM>>0)&0xff; /* LSB Height */ |
588 |
|
hdr[15] = (YDIM>>8)&0xff; /* MSB Height */ |
589 |
|
} else { |
590 |
|
hdr[14] = ((YDIM*3)>>1)&0xff; /* LSB Height */ |
591 |
|
hdr[15] = ((YDIM*3)>>9)&0xff; /* MSB Height */ |
592 |
|
} |
593 |
|
hdr[16] = BPP*8; |
594 |
|
hdr[17] = 0x00 | (1<<5) /* Up to down */ | (0<<4); /* Image descriptor */ |
595 |
|
|
596 |
/* Write header */ |
/* Write header */ |
597 |
fprintf(filehandle,"P5\n\n%d %d 255\n", XDIM,YDIM*3/2); |
fwrite(hdr, 1, sizeof(hdr), f); |
598 |
|
|
599 |
|
#ifdef ARCH_IS_LITTLE_ENDIAN |
600 |
|
/* write first plane */ |
601 |
|
fwrite(image, 1, XDIM*YDIM*BPP, f); |
602 |
|
#else |
603 |
|
{ |
604 |
|
int i; |
605 |
|
for (i=0; i<XDIM*YDIM*BPP;i+=BPP) { |
606 |
|
if (BPP == 1) { |
607 |
|
fputc(*(image+i), f); |
608 |
|
} else if (BPP == 2) { |
609 |
|
fputc(*(image+i+1), f); |
610 |
|
fputc(*(image+i+0), f); |
611 |
|
} else if (BPP == 3) { |
612 |
|
fputc(*(image+i+2), f); |
613 |
|
fputc(*(image+i+1), f); |
614 |
|
fputc(*(image+i+0), f); |
615 |
|
} else if (BPP == 4) { |
616 |
|
fputc(*(image+i+3), f); |
617 |
|
fputc(*(image+i+2), f); |
618 |
|
fputc(*(image+i+1), f); |
619 |
|
fputc(*(image+i+0), f); |
620 |
|
} |
621 |
|
} |
622 |
|
} |
623 |
|
#endif |
624 |
|
|
625 |
|
/* Write Y and V planes for YUV formats */ |
626 |
|
if (BPP == 1) { |
627 |
|
int i; |
628 |
|
|
629 |
|
/* Write the two chrominance planes */ |
630 |
|
for (i=0; i<YDIM/2; i++) { |
631 |
|
fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f); |
632 |
|
fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f); |
633 |
|
} |
634 |
|
} |
635 |
|
|
636 |
|
|
637 |
/* Write Y data */ |
/* Close the file */ |
638 |
fwrite(y, 1, XDIM*YDIM, filehandle); |
fclose(f); |
639 |
|
|
640 |
for(loop=0; loop<YDIM/2; loop++) |
return(0); |
641 |
|
} |
642 |
|
|
643 |
|
static int write_pnm(char *filename, unsigned char *image) |
644 |
{ |
{ |
645 |
/* Write U scanline */ |
FILE * f; |
|
fwrite(u, 1, XDIM/2, filehandle); |
|
646 |
|
|
647 |
/* Write V scanline */ |
f = fopen(filename, "wb"); |
648 |
fwrite(v, 1, XDIM/2, filehandle); |
if ( f == NULL) { |
649 |
|
return -1; |
650 |
|
} |
651 |
|
|
652 |
/* Update pointers */ |
if (BPP == 1) { |
653 |
u += XDIM/2; |
int i; |
654 |
v += XDIM/2; |
fprintf(f, "P5\n%i %i\n255\n", XDIM, YDIM*3/2); |
655 |
|
|
656 |
|
fwrite(image, 1, XDIM*YDIM, f); |
657 |
|
|
658 |
|
for (i=0; i<YDIM/2;i++) { |
659 |
|
fwrite(image+XDIM*YDIM + i*XDIM/2, 1, XDIM/2, f); |
660 |
|
fwrite(image+5*XDIM*YDIM/4 + i*XDIM/2, 1, XDIM/2, f); |
661 |
|
} |
662 |
|
} else if (BPP == 3) { |
663 |
|
int i; |
664 |
|
fprintf(f, "P6\n#xvid\n%i %i\n255\n", XDIM, YDIM); |
665 |
|
for (i=0; i<XDIM*YDIM*3; i+=3) { |
666 |
|
#ifdef ARCH_IS_LITTLE_ENDIAN |
667 |
|
fputc(image[i+2], f); |
668 |
|
fputc(image[i+1], f); |
669 |
|
fputc(image[i+0], f); |
670 |
|
#else |
671 |
|
fputc(image[i+0], f); |
672 |
|
fputc(image[i+1], f); |
673 |
|
fputc(image[i+2], f); |
674 |
|
#endif |
675 |
|
} |
676 |
} |
} |
677 |
|
|
678 |
/* Close file */ |
fclose(f); |
|
fclose(filehandle); |
|
679 |
|
|
680 |
return(0); |
return 0; |
681 |
} |
} |
682 |
else |
|
683 |
return(1); |
static int write_yuv(char *filename, unsigned char *image) |
684 |
|
{ |
685 |
|
FILE * f; |
686 |
|
|
687 |
|
f = fopen(filename, "ab+"); |
688 |
|
if ( f == NULL) { |
689 |
|
return -1; |
690 |
|
} |
691 |
|
|
692 |
|
fwrite(image, 1, 3*XDIM*YDIM/2, f); |
693 |
|
|
694 |
|
fclose(f); |
695 |
|
|
696 |
|
return 0; |
697 |
} |
} |
698 |
|
|
699 |
/***************************************************************************** |
/***************************************************************************** |
702 |
|
|
703 |
/* init decoder before first run */ |
/* init decoder before first run */ |
704 |
static int |
static int |
705 |
dec_init(int use_assembler) |
dec_init(int use_assembler, int debug_level) |
706 |
{ |
{ |
707 |
int ret; |
int ret; |
708 |
|
|
709 |
xvid_gbl_init_t xvid_gbl_init; |
xvid_gbl_init_t xvid_gbl_init; |
710 |
xvid_dec_create_t xvid_dec_create; |
xvid_dec_create_t xvid_dec_create; |
711 |
|
|
712 |
|
/* Reset the structure with zeros */ |
713 |
|
memset(&xvid_gbl_init, 0, sizeof(xvid_gbl_init_t)); |
714 |
|
memset(&xvid_dec_create, 0, sizeof(xvid_dec_create_t)); |
715 |
|
|
716 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
717 |
* XviD core initialization |
* Xvid core initialization |
718 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
719 |
|
|
720 |
/* Version */ |
/* Version */ |
730 |
else |
else |
731 |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
732 |
|
|
733 |
|
xvid_gbl_init.debug = debug_level; |
734 |
|
|
735 |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
736 |
|
|
737 |
/*------------------------------------------------------------------------ |
/*------------------------------------------------------------------------ |
738 |
* XviD encoder initialization |
* Xvid decoder initialization |
739 |
*----------------------------------------------------------------------*/ |
*----------------------------------------------------------------------*/ |
740 |
|
|
741 |
/* Version */ |
/* Version */ |
767 |
|
|
768 |
xvid_dec_frame_t xvid_dec_frame; |
xvid_dec_frame_t xvid_dec_frame; |
769 |
|
|
770 |
|
/* Reset all structures */ |
771 |
|
memset(&xvid_dec_frame, 0, sizeof(xvid_dec_frame_t)); |
772 |
|
memset(xvid_dec_stats, 0, sizeof(xvid_dec_stats_t)); |
773 |
|
|
774 |
/* Set version */ |
/* Set version */ |
775 |
xvid_dec_frame.version = XVID_VERSION; |
xvid_dec_frame.version = XVID_VERSION; |
776 |
xvid_dec_stats->version = XVID_VERSION; |
xvid_dec_stats->version = XVID_VERSION; |
777 |
|
|
778 |
/* No general flags to set */ |
/* No general flags to set */ |
779 |
|
if (POSTPROC == 1) |
780 |
|
xvid_dec_frame.general = XVID_DEBLOCKY | XVID_DEBLOCKUV; |
781 |
|
else if (POSTPROC==2) |
782 |
|
xvid_dec_frame.general = XVID_DEBLOCKY | XVID_DEBLOCKUV | XVID_DERINGY | XVID_DERINGUV; |
783 |
|
else |
784 |
xvid_dec_frame.general = 0; |
xvid_dec_frame.general = 0; |
785 |
|
|
786 |
/* Input stream */ |
/* Input stream */ |
789 |
|
|
790 |
/* Output frame structure */ |
/* Output frame structure */ |
791 |
xvid_dec_frame.output.plane[0] = ostream; |
xvid_dec_frame.output.plane[0] = ostream; |
792 |
xvid_dec_frame.output.stride[0] = XDIM; |
xvid_dec_frame.output.stride[0] = XDIM*BPP; |
793 |
xvid_dec_frame.output.csp = XVID_CSP_I420; |
xvid_dec_frame.output.csp = CSP; |
794 |
|
|
795 |
ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats); |
ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats); |
796 |
|
|