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 |
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.6 2003-02-15 15:22:17 edgomez Exp $ |
* $Id: xvid_decraw.c,v 1.7.2.3 2003-06-09 13:49:13 edgomez Exp $ |
24 |
* |
* |
25 |
****************************************************************************/ |
****************************************************************************/ |
26 |
|
|
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 |
|
|
67 |
static char filepath[256] = "./"; |
static char filepath[256] = "./"; |
68 |
static void *dec_handle = NULL; |
static void *dec_handle = NULL; |
69 |
|
|
70 |
# define BUFFER_SIZE 10*XDIM*YDIM |
# define BUFFER_SIZE (2*1024*1024) |
71 |
|
|
72 |
/***************************************************************************** |
/***************************************************************************** |
73 |
* Local prototypes |
* Local prototypes |
80 |
static int dec_main(unsigned char *istream, |
static int dec_main(unsigned char *istream, |
81 |
unsigned char *ostream, |
unsigned char *ostream, |
82 |
int istream_size, |
int istream_size, |
83 |
int *ostream_size, |
xvid_dec_stats_t *xvid_dec_stats); |
|
int *isframe); |
|
84 |
static int dec_stop(); |
static int dec_stop(); |
85 |
static void usage(); |
static void usage(); |
86 |
|
|
87 |
|
|
88 |
|
const char * type2str(int type) |
89 |
|
{ |
90 |
|
if (type==XVID_TYPE_IVOP) |
91 |
|
return "I"; |
92 |
|
if (type==XVID_TYPE_PVOP) |
93 |
|
return "P"; |
94 |
|
if (type==XVID_TYPE_BVOP) |
95 |
|
return "B"; |
96 |
|
return "S"; |
97 |
|
} |
98 |
|
|
99 |
/***************************************************************************** |
/***************************************************************************** |
100 |
* Main program |
* Main program |
101 |
****************************************************************************/ |
****************************************************************************/ |
105 |
unsigned char *mp4_buffer = NULL; |
unsigned char *mp4_buffer = NULL; |
106 |
unsigned char *mp4_ptr = NULL; |
unsigned char *mp4_ptr = NULL; |
107 |
unsigned char *out_buffer = NULL; |
unsigned char *out_buffer = NULL; |
|
int bigendian = 0; |
|
108 |
int still_left_in_buffer; |
int still_left_in_buffer; |
109 |
int delayed_frames; |
xvid_dec_stats_t xvid_dec_stats; |
110 |
|
|
111 |
double totaldectime; |
double totaldectime; |
112 |
|
|
133 |
if (strcmp("-asm", argv[i]) == 0 ) { |
if (strcmp("-asm", argv[i]) == 0 ) { |
134 |
use_assembler = 1; |
use_assembler = 1; |
135 |
} |
} |
136 |
else if (strcmp("-w", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-d", argv[i]) == 0) { |
137 |
i++; |
ARG_SAVEDECOUTPUT = 1; |
|
XDIM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-h", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
YDIM = atoi(argv[i]); |
|
|
} |
|
|
else if (strcmp("-d", argv[i]) == 0 && i < argc - 1 ) { |
|
|
i++; |
|
|
ARG_SAVEDECOUTPUT = atoi(argv[i]); |
|
138 |
} |
} |
139 |
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { |
140 |
i++; |
i++; |
141 |
ARG_INPUTFILE = argv[i]; |
ARG_INPUTFILE = argv[i]; |
142 |
} |
} |
143 |
else if (strcmp("-m", argv[i]) == 0 && i < argc - 1 ) { |
else if (strcmp("-m", argv[i]) == 0) { |
144 |
i++; |
ARG_SAVEMPEGSTREAM = 1; |
|
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
|
145 |
} |
} |
146 |
else if (strcmp("-help", argv[i])) { |
else if (strcmp("-help", argv[i])) { |
147 |
usage(); |
usage(); |
158 |
* Values checking |
* Values checking |
159 |
****************************************************************************/ |
****************************************************************************/ |
160 |
|
|
|
if(XDIM <= 0 || XDIM > 2048 || YDIM <= 0 || YDIM > 2048) { |
|
|
usage(); |
|
|
return -1; |
|
|
} |
|
|
|
|
161 |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
if ( ARG_INPUTFILE == NULL || strcmp(ARG_INPUTFILE, "stdin") == 0) { |
162 |
in_file = stdin; |
in_file = stdin; |
163 |
} |
} |
166 |
in_file = fopen(ARG_INPUTFILE, "rb"); |
in_file = fopen(ARG_INPUTFILE, "rb"); |
167 |
if (in_file == NULL) { |
if (in_file == NULL) { |
168 |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
fprintf(stderr, "Error opening input file %s\n", ARG_INPUTFILE); |
169 |
return -1; |
return(-1); |
170 |
} |
} |
171 |
} |
} |
172 |
|
|
180 |
if (!mp4_buffer) |
if (!mp4_buffer) |
181 |
goto free_all_memory; |
goto free_all_memory; |
182 |
|
|
|
/* Memory for frame output */ |
|
|
out_buffer = (unsigned char *) malloc(XDIM*YDIM*4); |
|
|
if (!out_buffer) |
|
|
goto free_all_memory; |
|
|
|
|
|
|
|
183 |
/***************************************************************************** |
/***************************************************************************** |
184 |
* XviD PART Start |
* XviD PART Start |
185 |
****************************************************************************/ |
****************************************************************************/ |
202 |
totaldectime = 0; |
totaldectime = 0; |
203 |
totalsize = 0; |
totalsize = 0; |
204 |
filenr = 0; |
filenr = 0; |
|
delayed_frames = 0; |
|
205 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
206 |
|
|
207 |
do { |
do { |
209 |
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
210 |
int used_bytes = 0; |
int used_bytes = 0; |
211 |
double dectime; |
double dectime; |
|
int notification; |
|
212 |
|
|
213 |
/* |
/* |
214 |
* 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 |
237 |
} |
} |
238 |
|
|
239 |
|
|
240 |
/* This loop flushes N_VOPS (with vop_coded bit set to 0) */ |
/* This loop is needed to handle VOL/NVOP reading */ |
241 |
do { |
do { |
242 |
|
|
243 |
/* Decode frame */ |
/* Decode frame */ |
244 |
dectime = msecond(); |
dectime = msecond(); |
245 |
status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, ¬ification); |
used_bytes = dec_main(mp4_ptr, out_buffer, mp4_size, &xvid_dec_stats); |
246 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
247 |
|
|
248 |
if (status) { |
/* Resize image buffer if needed */ |
249 |
break; |
if(xvid_dec_stats.type == XVID_TYPE_VOL) { |
250 |
|
|
251 |
|
/* Free old output buffer*/ |
252 |
|
if(out_buffer) free(out_buffer); |
253 |
|
|
254 |
|
/* Copy witdh and height from the vol structure */ |
255 |
|
XDIM = xvid_dec_stats.data.vol.width; |
256 |
|
YDIM = xvid_dec_stats.data.vol.height; |
257 |
|
|
258 |
|
/* Allocate the new buffer */ |
259 |
|
if((out_buffer = (unsigned char*)malloc(XDIM*YDIM*4)) == NULL) |
260 |
|
goto free_all_memory; |
261 |
} |
} |
262 |
|
|
263 |
/* Update buffer pointers */ |
/* Update buffer pointers */ |
264 |
|
if(used_bytes > 0) { |
265 |
mp4_ptr += used_bytes; |
mp4_ptr += used_bytes; |
266 |
still_left_in_buffer -= used_bytes; |
still_left_in_buffer -= used_bytes; |
267 |
|
|
268 |
/* Total size */ |
/* Total size */ |
269 |
totalsize += used_bytes; |
totalsize += used_bytes; |
270 |
|
} |
271 |
|
|
272 |
}while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */ |
}while(xvid_dec_stats.type <= 0 && still_left_in_buffer > 0); |
273 |
|
|
274 |
/* Negative buffer would mean we went too far */ |
/* Negative buffer would mean we went too far */ |
275 |
if(still_left_in_buffer < 0) break; |
if(still_left_in_buffer <= 0) |
276 |
|
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; |
|
|
} |
|
277 |
|
|
278 |
/* Updated data - Count only usefull decode time */ |
/* Updated data - Count only usefull decode time */ |
279 |
totaldectime += dectime; |
totaldectime += dectime; |
280 |
|
|
281 |
/* Prints some decoding stats */ |
|
282 |
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
283 |
filenr, dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
284 |
|
|
285 |
/* Save individual mpeg4 stream if required */ |
/* Save individual mpeg4 stream if required */ |
286 |
if (ARG_SAVEMPEGSTREAM) { |
if (ARG_SAVEMPEGSTREAM) { |
316 |
/***************************************************************************** |
/***************************************************************************** |
317 |
* Flush decoder buffers |
* Flush decoder buffers |
318 |
****************************************************************************/ |
****************************************************************************/ |
319 |
while(delayed_frames--) { |
|
320 |
|
do { |
321 |
|
|
322 |
/* Fake vars */ |
/* Fake vars */ |
323 |
int used_bytes, isframe; |
int used_bytes; |
324 |
double dectime; |
double dectime; |
325 |
|
|
326 |
/* Decode frame */ |
do { |
327 |
dectime = msecond(); |
dectime = msecond(); |
328 |
status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe); |
used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); |
329 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
330 |
|
}while(used_bytes>=0 && xvid_dec_stats.type <= 0); |
331 |
|
|
332 |
if (status) { |
if (used_bytes < 0) { /* XVID_ERR_END */ |
333 |
break; |
break; |
334 |
} |
} |
335 |
|
|
337 |
totaldectime += dectime; |
totaldectime += dectime; |
338 |
|
|
339 |
/* Prints some decoding stats */ |
/* Prints some decoding stats */ |
340 |
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", |
341 |
filenr, dectime, used_bytes); |
filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); |
342 |
|
|
343 |
/* Save output frame if required */ |
/* Save output frame if required */ |
344 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
352 |
|
|
353 |
filenr++; |
filenr++; |
354 |
|
|
355 |
} |
}while(1); |
356 |
|
|
357 |
/***************************************************************************** |
/***************************************************************************** |
358 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
379 |
free(out_buffer); |
free(out_buffer); |
380 |
free(mp4_buffer); |
free(mp4_buffer); |
381 |
|
|
382 |
return 0; |
return(0); |
383 |
} |
} |
384 |
|
|
385 |
/***************************************************************************** |
/***************************************************************************** |
392 |
fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n"); |
fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n"); |
393 |
fprintf(stderr, "Options :\n"); |
fprintf(stderr, "Options :\n"); |
394 |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); |
|
fprintf(stderr, " -w integer : frame width ([1.2048])\n"); |
|
|
fprintf(stderr, " -h integer : frame height ([1.2048])\n"); |
|
395 |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
fprintf(stderr, " -i string : input filename (default=stdin)\n"); |
396 |
fprintf(stderr, " -t integer : input data type (raw=0, mp4u=1)\n"); |
fprintf(stderr, " -t integer : input data type (raw=0, mp4u=1)\n"); |
397 |
fprintf(stderr, " -d boolean : save decoder output (0 False*, !=0 True)\n"); |
fprintf(stderr, " -d : save decoder output\n"); |
398 |
fprintf(stderr, " -m boolean : save mpeg4 raw stream to individual files (0 False*, !=0 True)\n"); |
fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); |
399 |
fprintf(stderr, " -help : This help message\n"); |
fprintf(stderr, " -help : This help message\n"); |
400 |
fprintf(stderr, " (* means default)\n"); |
fprintf(stderr, " (* means default)\n"); |
401 |
|
|
412 |
#ifndef WIN32 |
#ifndef WIN32 |
413 |
struct timeval tv; |
struct timeval tv; |
414 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
415 |
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); |
416 |
#else |
#else |
417 |
clock_t clk; |
clock_t clk; |
418 |
clk = clock(); |
clk = clock(); |
419 |
return clk * 1000 / CLOCKS_PER_SEC; |
return(clk * 1000 / CLOCKS_PER_SEC); |
420 |
#endif |
#endif |
421 |
} |
} |
422 |
|
|
461 |
/* Close file */ |
/* Close file */ |
462 |
fclose(filehandle); |
fclose(filehandle); |
463 |
|
|
464 |
return 0; |
return(0); |
465 |
} |
} |
466 |
else |
else |
467 |
return 1; |
return(1); |
468 |
} |
} |
469 |
|
|
470 |
/***************************************************************************** |
/***************************************************************************** |
475 |
static int |
static int |
476 |
dec_init(int use_assembler) |
dec_init(int use_assembler) |
477 |
{ |
{ |
478 |
int xerr; |
int ret; |
479 |
|
|
480 |
|
xvid_gbl_init_t xvid_gbl_init; |
481 |
|
xvid_dec_create_t xvid_dec_create; |
482 |
|
|
483 |
XVID_INIT_PARAM xinit; |
/*------------------------------------------------------------------------ |
484 |
XVID_DEC_PARAM xparam; |
* XviD core initialization |
485 |
|
*----------------------------------------------------------------------*/ |
486 |
|
|
487 |
|
/* Version */ |
488 |
|
xvid_gbl_init.version = XVID_VERSION; |
489 |
|
|
490 |
|
/* Assembly setting */ |
491 |
if(use_assembler) |
if(use_assembler) |
492 |
#ifdef ARCH_IA64 |
#ifdef ARCH_IS_IA64 |
493 |
xinit.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE | XVID_CPU_IA64; |
494 |
#else |
#else |
495 |
xinit.cpu_flags = 0; |
xvid_gbl_init.cpu_flags = 0; |
496 |
#endif |
#endif |
497 |
else |
else |
498 |
xinit.cpu_flags = XVID_CPU_FORCE; |
xvid_gbl_init.cpu_flags = XVID_CPU_FORCE; |
499 |
|
|
500 |
xvid_init(NULL, 0, &xinit, NULL); |
xvid_global(NULL, 0, &xvid_gbl_init, NULL); |
|
xparam.width = XDIM; |
|
|
xparam.height = YDIM; |
|
501 |
|
|
502 |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
/*------------------------------------------------------------------------ |
503 |
|
* XviD encoder initialization |
504 |
|
*----------------------------------------------------------------------*/ |
505 |
|
|
506 |
dec_handle = xparam.handle; |
/* Version */ |
507 |
|
xvid_dec_create.version = XVID_VERSION; |
508 |
|
|
509 |
return xerr; |
/* |
510 |
|
* Image dimensions -- set to 0, xvidcore will resize when ever it is |
511 |
|
* needed |
512 |
|
*/ |
513 |
|
xvid_dec_create.width = 0; |
514 |
|
xvid_dec_create.height = 0; |
515 |
|
|
516 |
|
ret = xvid_decore(NULL, XVID_DEC_CREATE, &xvid_dec_create, NULL); |
517 |
|
|
518 |
|
dec_handle = xvid_dec_create.handle; |
519 |
|
|
520 |
|
return(ret); |
521 |
} |
} |
522 |
|
|
523 |
/* decode one frame */ |
/* decode one frame */ |
525 |
dec_main(unsigned char *istream, |
dec_main(unsigned char *istream, |
526 |
unsigned char *ostream, |
unsigned char *ostream, |
527 |
int istream_size, |
int istream_size, |
528 |
int *ostream_size, |
xvid_dec_stats_t *xvid_dec_stats) |
|
int *notification) |
|
529 |
{ |
{ |
530 |
|
|
531 |
int xerr; |
int ret; |
532 |
XVID_DEC_FRAME xframe; |
|
533 |
XVID_DEC_STATS xstats; |
xvid_dec_frame_t xvid_dec_frame; |
534 |
|
|
535 |
|
/* Set version */ |
536 |
|
xvid_dec_frame.version = XVID_VERSION; |
537 |
|
xvid_dec_stats->version = XVID_VERSION; |
538 |
|
|
539 |
xframe.general = 0; |
/* No general flags to set */ |
540 |
xframe.bitstream = istream; |
xvid_dec_frame.general = 0; |
|
xframe.length = istream_size; |
|
|
xframe.image = ostream; |
|
|
xframe.stride = XDIM; |
|
|
xframe.colorspace = XVID_CSP_YV12; |
|
541 |
|
|
542 |
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats); |
/* Input stream */ |
543 |
|
xvid_dec_frame.bitstream = istream; |
544 |
|
xvid_dec_frame.length = istream_size; |
545 |
|
|
546 |
*ostream_size = xframe.length; |
/* Output frame structure */ |
547 |
|
xvid_dec_frame.output.plane[0] = ostream; |
548 |
|
xvid_dec_frame.output.stride[0] = XDIM; |
549 |
|
xvid_dec_frame.output.csp = XVID_CSP_I420; |
550 |
|
|
551 |
*notification = xstats.notify; |
ret = xvid_decore(dec_handle, XVID_DEC_DECODE, &xvid_dec_frame, xvid_dec_stats); |
552 |
|
|
553 |
return xerr; |
return(ret); |
554 |
} |
} |
555 |
|
|
556 |
/* close decoder to release resources */ |
/* close decoder to release resources */ |
557 |
static int |
static int |
558 |
dec_stop() |
dec_stop() |
559 |
{ |
{ |
560 |
int xerr; |
int ret; |
561 |
|
|
562 |
xerr = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
ret = xvid_decore(dec_handle, XVID_DEC_DESTROY, NULL, NULL); |
563 |
|
|
564 |
return xerr; |
return(ret); |
565 |
} |
} |