4 |
* - Console based decoding test application - |
* - Console based decoding test application - |
5 |
* |
* |
6 |
* Copyright(C) 2002-2003 Christoph Lampert |
* Copyright(C) 2002-2003 Christoph Lampert |
7 |
|
* 2002-2003 Edouard Gomez <ed.gomez@free.fr> |
8 |
* |
* |
9 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
10 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
32 |
* the speed for this is measured. |
* the speed for this is measured. |
33 |
* |
* |
34 |
* The program is plain C and needs no libraries except for libxvidcore, |
* The program is plain C and needs no libraries except for libxvidcore, |
35 |
* and maths-lib, so with UN*X you simply compile by |
* and maths-lib. |
36 |
* |
* |
37 |
* gcc xvid_decraw.c -lxvidcore -lm -o xvid_decraw |
* Use ./xvid_decraw -help for a list of options |
|
* |
|
|
* You have to specify the image dimensions (until the add the feature |
|
|
* to read this from the bitstream) |
|
|
* |
|
|
* Usage : xvid_decraw <-w width> <-h height> [OPTIONS] |
|
|
* Options : |
|
|
* -asm : use assembly optimizations (default=disabled) |
|
|
* -w integer : frame width ([1.2048]) |
|
|
* -h integer : frame height ([1.2048]) |
|
|
* -i string : input filename (default=stdin) |
|
|
* -t integer : input data type (raw=0, mp4u=1) |
|
|
* -d boolean : save decoder output (0 False*, !=0 True) |
|
|
* -m boolean : save mpeg4 raw stream to single files (0 False*, !=0 True) |
|
|
* -help : This help message |
|
|
* (* means default) |
|
38 |
* |
* |
39 |
****************************************************************************/ |
****************************************************************************/ |
40 |
|
|
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 10*XDIM*YDIM |
#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 |
int *ostream_size, |
xvid_dec_stats_t *xvid_dec_stats); |
|
int *isframe); |
|
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) |
98 |
|
{ |
99 |
|
if (type==XVID_TYPE_IVOP) |
100 |
|
return "I"; |
101 |
|
if (type==XVID_TYPE_PVOP) |
102 |
|
return "P"; |
103 |
|
if (type==XVID_TYPE_BVOP) |
104 |
|
return "B"; |
105 |
|
return "S"; |
106 |
|
} |
107 |
|
|
108 |
/***************************************************************************** |
/***************************************************************************** |
109 |
* Main program |
* Main program |
114 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
115 |
unsigned char *mp4_ptr = NULL; |
unsigned char *mp4_ptr = NULL; |
116 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
117 |
int bigendian = 0; |
int useful_bytes; |
118 |
int still_left_in_buffer; |
int chunk; |
119 |
int delayed_frames; |
xvid_dec_stats_t xvid_dec_stats; |
120 |
|
|
121 |
double totaldectime; |
double totaldectime; |
122 |
|
|
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 ) { |
|
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
|
147 |
i++; |
i++; |
148 |
XDIM = atoi(argv[i]); |
if (sscanf(argv[i], "0x%x", &debug_level) != 1) { |
149 |
|
debug_level = atoi(argv[i]); |
150 |
} |
} |
151 |
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
} else if (strcmp("-d", argv[i]) == 0) { |
152 |
|
ARG_SAVEDECOUTPUT = 1; |
153 |
|
} else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
154 |
i++; |
i++; |
155 |
YDIM = atoi(argv[i]); |
ARG_INPUTFILE = argv[i]; |
156 |
} |
} else if (strcmp("-m", argv[i]) == 0) { |
157 |
else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) { |
ARG_SAVEMPEGSTREAM = 1; |
158 |
|
} else if (strcmp("-c", argv[i]) == 0 && i < argc - 1 ) { |
159 |
i++; |
i++; |
160 |
ARG_SAVEDECOUTPUT = atoi(argv[i]); |
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("-i", argv[i]) == 0 && i < argc - 1 ) { |
} else if (strcmp("-postproc", argv[i]) == 0 && i < argc - 1 ) { |
177 |
i++; |
i++; |
178 |
ARG_INPUTFILE = argv[i]; |
POSTPROC = atoi(argv[i]); |
179 |
} |
if (POSTPROC < 0) POSTPROC = 0; |
180 |
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
if (POSTPROC > 2) POSTPROC = 2; |
181 |
|
} else if (strcmp("-f", argv[i]) == 0 && i < argc -1) { |
182 |
i++; |
i++; |
183 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
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])) { |
} else if (strcmp("-help", argv[i]) == 0) { |
191 |
usage(); |
usage(); |
192 |
return(0); |
return(0); |
193 |
} |
} else { |
|
else { |
|
194 |
usage(); |
usage(); |
195 |
exit(-1); |
exit(-1); |
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 |
****************************************************************************/ |
****************************************************************************/ |
208 |
|
|
|
if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) { |
|
|
usage(); |
|
|
return -1; |
|
|
} |
|
|
|
|
209 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
210 |
in_file = stdin; |
in_file = stdin; |
211 |
} |
} |
214 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
215 |
if (in_file == NULL) { |
if (in_file == NULL) { |
216 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
217 |
return -1; |
return(-1); |
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 |
****************************************************************************/ |
****************************************************************************/ |
236 |
if (!mp4_buffer) |
if (!mp4_buffer) |
237 |
goto free_all_memory; |
goto free_all_memory; |
238 |
|
|
|
/* Memory for frame output */ |
|
|
out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); |
|
|
if (!out_buffer) |
|
|
goto free_all_memory; |
|
|
|
|
|
|
|
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); |
253 |
****************************************************************************/ |
****************************************************************************/ |
254 |
|
|
255 |
/* Fill the buffer */ |
/* Fill the buffer */ |
256 |
still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); |
useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); |
257 |
|
|
258 |
totaldectime = 0; |
totaldectime = 0; |
259 |
totalsize = 0; |
totalsize = 0; |
260 |
filenr = 0; |
filenr = 0; |
|
delayed_frames = 0; |
|
261 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
262 |
|
chunk = 0; |
263 |
|
|
264 |
do { |
do { |
|
|
|
|
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
|
265 |
int used_bytes = 0; |
int used_bytes = 0; |
266 |
double dectime; |
double dectime; |
|
int notification; |
|
267 |
|
|
268 |
/* |
/* |
269 |
* If the buffer is half empty or there are no more bytes in it |
* If the buffer is half empty or there are no more bytes in it |
270 |
* then fill it. |
* then fill it. |
271 |
*/ |
*/ |
272 |
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 || |
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) { |
273 |
still_left_in_buffer <= 0) { |
int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
|
int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
|
274 |
|
|
275 |
/* Move data if needed */ |
/* Move data if needed */ |
276 |
if (rest) |
if (already_in_buffer > 0) |
277 |
memcpy(mp4_buffer, mp4_ptr, rest); |
memcpy(mp4_buffer, mp4_ptr, already_in_buffer); |
278 |
|
|
279 |
/* Update mp4_ptr */ |
/* Update mp4_ptr */ |
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)) { |
284 |
break; |
useful_bytes += fread(mp4_buffer + already_in_buffer, |
285 |
|
1, BUFFER_SIZE - already_in_buffer, |
|
still_left_in_buffer = fread(mp4_buffer + rest, |
|
|
1, |
|
|
BUFFER_SIZE - rest, |
|
286 |
in_file); |
in_file); |
287 |
|
} |
288 |
} |
} |
289 |
|
|
290 |
|
|
291 |
/* This loop flushes N_VOPS (with vop_coded bit set to 0) */ |
/* This loop is needed to handle VOL/NVOP reading */ |
292 |
do { |
do { |
293 |
|
|
294 |
/* Decode frame */ |
/* Decode frame */ |
295 |
dectime = msecond(); |
dectime = msecond(); |
296 |
status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, ¬ification); |
used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats); |
297 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
298 |
|
|
299 |
if (status) { |
/* Resize image buffer if needed */ |
300 |
break; |
if(xvid_dec_stats.type == XVID_TYPE_VOL) { |
301 |
|
|
302 |
|
/* Check if old buffer is smaller */ |
303 |
|
if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) { |
304 |
|
|
305 |
|
/* Copy new witdh and new height from the vol structure */ |
306 |
|
XDIM = xvid_dec_stats.data.vol.width; |
307 |
|
YDIM = xvid_dec_stats.data.vol.height; |
308 |
|
|
309 |
|
/* Free old output buffer*/ |
310 |
|
if(out_buffer) free(out_buffer); |
311 |
|
|
312 |
|
/* Allocate the new buffer */ |
313 |
|
out_buffer = (unsigned char*)malloc(XDIM*YDIM*4); |
314 |
|
if(out_buffer == NULL) |
315 |
|
goto free_all_memory; |
316 |
|
|
317 |
|
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 */ |
338 |
|
if(used_bytes > 0) { |
339 |
mp4_ptr += used_bytes; |
mp4_ptr += used_bytes; |
340 |
still_left_in_buffer -= used_bytes; |
useful_bytes -= used_bytes; |
341 |
|
|
342 |
/* Total size */ |
/* Total size */ |
343 |
totalsize += used_bytes; |
totalsize += used_bytes; |
344 |
|
} |
345 |
|
|
346 |
}while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */ |
if (display_buffer_bytes) { |
347 |
|
printf("Data chunk %d: %d bytes consumed, %d bytes in buffer\n", chunk++, used_bytes, useful_bytes); |
|
/* Negative buffer would mean we went too far */ |
|
|
if(still_left_in_buffer < 0) break; |
|
|
|
|
|
/* Skip when decoder is buffering images or decoding VOL information */ |
|
|
if(notification != XVID_DEC_VOP) { |
|
|
/* It's a delay only if it notifies NOTHING */ |
|
|
if(notification == XVID_DEC_NOTHING) |
|
|
delayed_frames++; |
|
|
continue; |
|
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 |
352 |
|
* This means we went too far */ |
353 |
|
if(useful_bytes < 0) |
354 |
|
break; |
355 |
|
|
356 |
/* Updated data - Count only usefull decode time */ |
/* Updated data - Count only usefull decode time */ |
357 |
totaldectime += dectime; |
totaldectime += dectime; |
358 |
|
|
359 |
/* Prints some decoding stats */ |
|
360 |
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
if (!display_buffer_bytes) { |
361 |
filenr, dectime, used_bytes); |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
362 |
|
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 |
401 |
****************************************************************************/ |
****************************************************************************/ |
402 |
while(delayed_frames--) { |
|
403 |
|
do { |
404 |
|
|
405 |
/* Fake vars */ |
/* Fake vars */ |
406 |
int used_bytes, isframe; |
int used_bytes; |
407 |
double dectime; |
double dectime; |
408 |
|
|
409 |
/* Decode frame */ |
do { |
410 |
dectime = msecond(); |
dectime = msecond(); |
411 |
status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe); |
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); |
417 |
|
|
418 |
if (status) { |
if (used_bytes < 0) { /* XVID_ERR_END */ |
419 |
break; |
break; |
420 |
} |
} |
421 |
|
|
423 |
totaldectime += dectime; |
totaldectime += dectime; |
424 |
|
|
425 |
/* Prints some decoding stats */ |
/* Prints some decoding stats */ |
426 |
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
if (!display_buffer_bytes) { |
427 |
filenr, dectime, used_bytes); |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
428 |
|
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 |
} |
} |
441 |
|
|
442 |
filenr++; |
filenr++; |
443 |
|
|
444 |
} |
}while(1); |
445 |
|
|
446 |
/***************************************************************************** |
/***************************************************************************** |
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: |
471 |
free(out_buffer); |
free(out_buffer); |
472 |
free(mp4_buffer); |
free(mp4_buffer); |
473 |
|
|
474 |
return 0; |
return(0); |
475 |
} |
} |
476 |
|
|
477 |
/***************************************************************************** |
/***************************************************************************** |
481 |
static void usage() |
static void usage() |
482 |
{ |
{ |
483 |
|
|
484 |
fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [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, " -w integer : frame width ([1.2048])\n"); |
fprintf(stderr, " -debug : debug level (debug=0)\n"); |
|
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
|
488 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
489 |
fprintf(stderr, " -t integer : input data type (raw=0, mp4u=1)\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
490 |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
fprintf(stderr, " -c csp : choose colorspace output (rgb16, rgb24, rgb32, yv12, i420)\n"); |
491 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n"); |
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"); |
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"); |
496 |
|
|
507 |
#ifndef WIN32 |
#ifndef WIN32 |
508 |
struct timeval tv; |
struct timeval tv; |
509 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
510 |
return (double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3; |
return((double)tv.tv_sec*1.0e3 + (double)tv.tv_usec*1.0e-3); |
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 |
|
|
|
/* Write Y data */ |
|
|
fwrite(y, 1, XDIM*YDIM, filehandle); |
|
636 |
|
|
637 |
for(loop=0; loop<YDIM/2; loop++) |
/* Close the file */ |
638 |
|
fclose(f); |
639 |
|
|
640 |
|
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 |
|
if (BPP == 1) { |
653 |
|
int i; |
654 |
|
fprintf(f, "P5\n%i %i\n255\n", XDIM, YDIM*3/2); |
655 |
|
|
656 |
/* Update pointers */ |
fwrite(image, 1, XDIM*YDIM, f); |
|
u += XDIM/2; |
|
|
v += XDIM/2; |
|
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 xerr; |
int ret; |
708 |
|
|
709 |
|
xvid_gbl_init_t xvid_gbl_init; |
710 |
|
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 |
718 |
|
*----------------------------------------------------------------------*/ |
719 |
|
|
720 |
XVID_INIT_PARAM xinit; |
/* Version */ |
721 |
XVID_DEC_PARAM xparam; |
xvid_gbl_init.version = XVID_VERSION; |
722 |
|
|
723 |
|
/* Assembly setting */ |
724 |
if(use_assembler) |
if(use_assembler) |
725 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
726 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
727 |
#else |
#else |
728 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
729 |
#endif |
#endif |
730 |
else |
else |
731 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
732 |
|
|
733 |
xvid_init(NULL, 0, &xinit, NULL); |
xvid_gbl_init.debug = debug_level; |
|
xparam.width = XDIM; |
|
|
xparam.height = YDIM; |
|
734 |
|
|
735 |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
736 |
|
|
737 |
dec_handle = xparam.handle; |
/*------------------------------------------------------------------------ |
738 |
|
* Xvid decoder initialization |
739 |
|
*----------------------------------------------------------------------*/ |
740 |
|
|
741 |
return xerr; |
/* Version */ |
742 |
|
xvid_dec_create.version = XVID_VERSION; |
743 |
|
|
744 |
|
/* |
745 |
|
* Image dimensions -- set to 0, xvidcore will resize when ever it is |
746 |
|
* needed |
747 |
|
*/ |
748 |
|
xvid_dec_create.width = 0; |
749 |
|
xvid_dec_create.height = 0; |
750 |
|
|
751 |
|
ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL); |
752 |
|
|
753 |
|
dec_handle = xvid_dec_create.handle; |
754 |
|
|
755 |
|
return(ret); |
756 |
} |
} |
757 |
|
|
758 |
/* decode one frame */ |
/* decode one frame */ |
760 |
dec_main(unsigned char *istream, |
dec_main(unsigned char *istream, |
761 |
unsigned char *ostream, |
unsigned char *ostream, |
762 |
int istream_size, |
int istream_size, |
763 |
int *ostream_size, |
xvid_dec_stats_t *xvid_dec_stats) |
|
int *notification) |
|
764 |
{ |
{ |
765 |
|
|
766 |
int xerr; |
int ret; |
|
XVID_DEC_FRAME xframe; |
|
|
XVID_DEC_STATS xstats; |
|
767 |
|
|
768 |
xframe.general = 0; |
xvid_dec_frame_t xvid_dec_frame; |
769 |
xframe.bitstream = istream; |
|
770 |
xframe.length = istream_size; |
/* Reset all structures */ |
771 |
xframe.image = ostream; |
memset(&xvid_dec_frame, 0, sizeof(xvid_dec_frame_t)); |
772 |
xframe.stride = XDIM; |
memset(xvid_dec_stats, 0, sizeof(xvid_dec_stats_t)); |
773 |
xframe.colorspace = XVID_CSP_YV12; |
|
774 |
|
/* Set version */ |
775 |
|
xvid_dec_frame.version = XVID_VERSION; |
776 |
|
xvid_dec_stats->version = XVID_VERSION; |
777 |
|
|
778 |
|
/* 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; |
785 |
|
|
786 |
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats); |
/* Input stream */ |
787 |
|
xvid_dec_frame.bitstream = istream; |
788 |
|
xvid_dec_frame.length = istream_size; |
789 |
|
|
790 |
*ostream_size = xframe.length; |
/* Output frame structure */ |
791 |
|
xvid_dec_frame.output.plane[0] = ostream; |
792 |
|
xvid_dec_frame.output.stride[0] = XDIM*BPP; |
793 |
|
xvid_dec_frame.output.csp = CSP; |
794 |
|
|
795 |
*notification = xstats.notify; |
ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats); |
796 |
|
|
797 |
return xerr; |
return(ret); |
798 |
} |
} |
799 |
|
|
800 |
/* close decoder to release resources */ |
/* close decoder to release resources */ |
801 |
static int |
static int |
802 |
dec_stop() |
dec_stop() |
803 |
{ |
{ |
804 |
int xerr; |
int ret; |
805 |
|
|
806 |
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
807 |
|
|
808 |
return xerr; |
return(ret); |
809 |
} |
} |