3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - Console based decoding test application - |
* - Console based decoding test application - |
5 |
* |
* |
6 |
* Copyright(C) 2002-2003 Christoph Lampert |
* Copyright(C) 2002 Christoph Lampert |
7 |
* |
* |
8 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
9 |
* 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 |
56 |
#include <stdlib.h> |
#include <stdlib.h> |
57 |
#include <string.h> |
#include <string.h> |
58 |
#include <math.h> |
#include <math.h> |
59 |
#ifndef WIN32 |
#ifndef _MSC_VER |
60 |
#include <sys/time.h> |
#include <sys/time.h> |
61 |
#else |
#else |
62 |
#include <time.h> |
#include <time.h> |
75 |
static int YDIM = 0; |
static int YDIM = 0; |
76 |
static int ARG_SAVEDECOUTPUT = 0; |
static int ARG_SAVEDECOUTPUT = 0; |
77 |
static int ARG_SAVEMPEGSTREAM = 0; |
static int ARG_SAVEMPEGSTREAM = 0; |
78 |
|
static int ARG_STREAMTYPE = 0; |
79 |
static char *ARG_INPUTFILE = NULL; |
static char *ARG_INPUTFILE = NULL; |
80 |
|
|
81 |
|
|
84 |
|
|
85 |
# define BUFFER_SIZE 10*XDIM*YDIM |
# define BUFFER_SIZE 10*XDIM*YDIM |
86 |
|
|
87 |
|
#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ |
88 |
|
(((long)(c))<<8) |((long)(d))) |
89 |
|
|
90 |
|
#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ |
91 |
|
(((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) |
92 |
|
|
93 |
/***************************************************************************** |
/***************************************************************************** |
94 |
* Local prototypes |
* Local prototypes |
95 |
****************************************************************************/ |
****************************************************************************/ |
101 |
static int dec_main(unsigned char *istream, |
static int dec_main(unsigned char *istream, |
102 |
unsigned char *ostream, |
unsigned char *ostream, |
103 |
int istream_size, |
int istream_size, |
104 |
int *ostream_size, |
int *ostream_size); |
|
int *isframe); |
|
105 |
static int dec_stop(); |
static int dec_stop(); |
106 |
static void usage(); |
static void usage(); |
107 |
|
|
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 bigendian = 0; |
|
int still_left_in_buffer; |
|
|
int delayed_frames; |
|
118 |
|
|
119 |
double totaldectime; |
double totaldectime; |
120 |
|
|
130 |
int i; |
int i; |
131 |
|
|
132 |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
printf("xvid_decraw - raw mpeg4 bitstream decoder "); |
133 |
printf("written by Christoph Lampert 2002-2003\n\n"); |
printf("written by Christoph Lampert 2002\n\n"); |
134 |
|
|
135 |
/***************************************************************************** |
/***************************************************************************** |
136 |
* Command line parsing |
* Command line parsing |
161 |
i++; |
i++; |
162 |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
ARG_SAVEMPEGSTREAM = atoi(argv[i]); |
163 |
} |
} |
164 |
|
else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { |
165 |
|
i++; |
166 |
|
ARG_STREAMTYPE = atoi(argv[i]); |
167 |
|
} |
168 |
else if (strcmp("-help", argv[i])) { |
else if (strcmp("-help", argv[i])) { |
169 |
usage(); |
usage(); |
170 |
return(0); |
return(0); |
229 |
* Main loop |
* Main loop |
230 |
****************************************************************************/ |
****************************************************************************/ |
231 |
|
|
232 |
/* Fill the buffer */ |
if(ARG_STREAMTYPE) { |
233 |
still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); |
|
234 |
|
unsigned char header[4]; |
235 |
|
|
236 |
|
/* MP4U format : read header */ |
237 |
|
if(feof(in_file)) |
238 |
|
goto release_all; |
239 |
|
fread(header, 4, 1, in_file); |
240 |
|
|
241 |
|
if(header[0] != '2' || header[1] != 'M' || |
242 |
|
header[2] != 'O' || header[3] != 'G') { |
243 |
|
fprintf(stderr, "Error, this not a mp4u container file\n"); |
244 |
|
goto release_all; |
245 |
|
} |
246 |
|
|
247 |
|
} |
248 |
|
|
249 |
|
totalsize = LONG_PACK('M','P','4','U'); |
250 |
|
mp4_ptr = (unsigned char *)&totalsize; |
251 |
|
if(*mp4_ptr == 'M') |
252 |
|
bigendian = 1; |
253 |
|
else |
254 |
|
bigendian = 0; |
255 |
|
|
256 |
totaldectime = 0; |
totaldectime = 0; |
257 |
totalsize = 0; |
totalsize = 0; |
258 |
filenr = 0; |
filenr = 0; |
|
delayed_frames = 0; |
|
259 |
mp4_ptr = mp4_buffer; |
mp4_ptr = mp4_buffer; |
260 |
|
|
261 |
do { |
do { |
263 |
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
264 |
int used_bytes = 0; |
int used_bytes = 0; |
265 |
double dectime; |
double dectime; |
266 |
int notification; |
|
267 |
|
/* Read data from input file */ |
268 |
|
if(ARG_STREAMTYPE) { |
269 |
|
|
270 |
|
/* MP4U container */ |
271 |
|
|
272 |
|
/* Read stream size first */ |
273 |
|
if(feof(in_file)) |
274 |
|
break; |
275 |
|
fread(&mp4_size, sizeof(long), 1, in_file); |
276 |
|
|
277 |
|
if(bigendian) |
278 |
|
mp4_size = SWAP(mp4_size); |
279 |
|
|
280 |
|
/* Read mp4_size_bytes */ |
281 |
|
if(feof(in_file)) |
282 |
|
break; |
283 |
|
fread(mp4_buffer, mp4_size, 1, in_file); |
284 |
|
|
285 |
/* |
/* |
286 |
* If the buffer is half empty or there are no more bytes in it |
* When reading mp4u, we don't have to care about buffer |
287 |
* then fill it. |
* filling as we know exactly how much bytes there are in |
288 |
|
* next frame |
289 |
*/ |
*/ |
290 |
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 || |
mp4_ptr = mp4_buffer; |
291 |
still_left_in_buffer <= 0) { |
|
292 |
|
} |
293 |
|
else { |
294 |
|
|
295 |
|
/* Real raw stream */ |
296 |
|
|
297 |
|
/* buffer more than half empty -> Fill it */ |
298 |
|
if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) { |
299 |
int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); |
300 |
|
|
301 |
/* Move data if needed */ |
/* Move data if needed */ |
308 |
/* read new data */ |
/* read new data */ |
309 |
if(feof(in_file)) |
if(feof(in_file)) |
310 |
break; |
break; |
311 |
|
fread(mp4_buffer + rest, BUFFER_SIZE - rest, 1, in_file); |
|
still_left_in_buffer = fread(mp4_buffer + rest, |
|
|
1, |
|
|
BUFFER_SIZE - rest, |
|
|
in_file); |
|
|
|
|
312 |
} |
} |
313 |
|
|
314 |
|
} |
|
/* This loop flushes N_VOPS (with vop_coded bit set to 0) */ |
|
|
do { |
|
315 |
|
|
316 |
/* Decode frame */ |
/* Decode frame */ |
317 |
dectime = msecond(); |
dectime = msecond(); |
318 |
status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, ¬ification); |
status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes); |
319 |
dectime = msecond() - dectime; |
dectime = msecond() - dectime; |
320 |
|
|
321 |
if (status) { |
if (status) { |
322 |
break; |
break; |
323 |
} |
} |
324 |
|
|
325 |
/* Update buffer pointers */ |
/* |
326 |
|
* Only needed for real raw stream, mp4u uses |
327 |
|
* mp4_ptr = mp4_buffer for each frame |
328 |
|
*/ |
329 |
mp4_ptr += used_bytes; |
mp4_ptr += used_bytes; |
|
still_left_in_buffer -= used_bytes; |
|
330 |
|
|
331 |
/* Total size */ |
/* Updated data */ |
332 |
totalsize += used_bytes; |
totalsize += used_bytes; |
|
|
|
|
}while(used_bytes <= 7 && still_left_in_buffer > 0); /* <= 7 bytes is a NVOPS */ |
|
|
|
|
|
/* 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; |
|
|
} |
|
|
|
|
|
/* Updated data - Count only usefull decode time */ |
|
333 |
totaldectime += dectime; |
totaldectime += dectime; |
334 |
|
|
335 |
/* Prints some decoding stats */ |
/* Prints some decoding stats */ |
336 |
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n", |
337 |
filenr, dectime, used_bytes); |
filenr, dectime, used_bytes); |
338 |
|
|
339 |
/* Save individual mpeg4 stream if required */ |
/* Save individual mpeg4 strean if required */ |
340 |
if (ARG_SAVEMPEGSTREAM) { |
if (ARG_SAVEMPEGSTREAM) { |
341 |
FILE *filehandle = NULL; |
FILE *filehandle = NULL; |
342 |
|
|
348 |
filename); |
filename); |
349 |
} |
} |
350 |
else { |
else { |
351 |
fwrite(mp4_buffer, 1, used_bytes, filehandle); |
fwrite(mp4_buffer, used_bytes, 1, filehandle); |
352 |
fclose(filehandle); |
fclose(filehandle); |
353 |
} |
} |
354 |
} |
} |
355 |
|
|
356 |
|
|
357 |
/* Save output frame if required */ |
/* Save output frame if required */ |
358 |
if (ARG_SAVEDECOUTPUT) { |
if (ARG_SAVEDECOUTPUT) { |
359 |
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
368 |
|
|
369 |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR)); |
} while ( (status>=0) && (filenr<ABS_MAXFRAMENR)); |
370 |
|
|
|
/***************************************************************************** |
|
|
* Flush decoder buffers |
|
|
****************************************************************************/ |
|
|
while(delayed_frames--) { |
|
|
|
|
|
/* Fake vars */ |
|
|
int used_bytes, isframe; |
|
|
double dectime; |
|
|
|
|
|
/* Decode frame */ |
|
|
dectime = msecond(); |
|
|
status = dec_main(NULL, out_buffer, -1, &used_bytes, &isframe); |
|
|
dectime = msecond() - dectime; |
|
|
|
|
|
if (status) { |
|
|
break; |
|
|
} |
|
|
|
|
|
/* Updated data - Count only usefull decode time */ |
|
|
totaldectime += dectime; |
|
|
|
|
|
/* Prints some decoding stats */ |
|
|
printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", |
|
|
filenr, dectime, used_bytes); |
|
|
|
|
|
/* Save output frame if required */ |
|
|
if (ARG_SAVEDECOUTPUT) { |
|
|
sprintf(filename, "%sdec%05d.pgm", filepath, filenr); |
|
|
if(write_pgm(filename,out_buffer)) { |
|
|
fprintf(stderr, |
|
|
"Error writing decoded PGM frame %s\n", |
|
|
filename); |
|
|
} |
|
|
} |
|
|
|
|
|
filenr++; |
|
|
|
|
|
} |
|
371 |
|
|
372 |
/***************************************************************************** |
/***************************************************************************** |
373 |
* Calculate totals and averages for output, print results |
* Calculate totals and averages for output, print results |
376 |
totalsize /= filenr; |
totalsize /= filenr; |
377 |
totaldectime /= filenr; |
totaldectime /= filenr; |
378 |
|
|
379 |
printf("Avg: dectime(ms) =%7.2f, fps =%7.2f, length(bytes) =%7d\n", |
printf("Avg: dectime %5.2f ms, %5.2f fps, mp4 stream size =%d\n", |
380 |
totaldectime, 1000/totaldectime, (int)totalsize); |
totaldectime, 1000/totaldectime, totalsize); |
381 |
|
|
382 |
/***************************************************************************** |
/***************************************************************************** |
383 |
* XviD PART Stop |
* XviD PART Stop |
384 |
****************************************************************************/ |
/****************************************************************************/ |
385 |
|
|
386 |
release_all: |
release_all: |
387 |
if (dec_handle) { |
if (dec_handle) { |
426 |
static double |
static double |
427 |
msecond() |
msecond() |
428 |
{ |
{ |
429 |
#ifndef WIN32 |
#ifndef _MSC_VER |
430 |
struct timeval tv; |
struct timeval tv; |
431 |
gettimeofday(&tv, 0); |
gettimeofday(&tv, 0); |
432 |
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; |
437 |
#endif |
#endif |
438 |
} |
} |
439 |
|
|
440 |
|
|
441 |
/***************************************************************************** |
/***************************************************************************** |
442 |
* output functions |
* output functions |
443 |
****************************************************************************/ |
****************************************************************************/ |
512 |
xparam.height = YDIM; |
xparam.height = YDIM; |
513 |
|
|
514 |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
xerr = xvid_decore(NULL, XVID_DEC_CREATE, &xparam, NULL); |
|
|
|
515 |
dec_handle = xparam.handle; |
dec_handle = xparam.handle; |
516 |
|
|
517 |
return xerr; |
return xerr; |
522 |
dec_main(unsigned char *istream, |
dec_main(unsigned char *istream, |
523 |
unsigned char *ostream, |
unsigned char *ostream, |
524 |
int istream_size, |
int istream_size, |
525 |
int *ostream_size, |
int *ostream_size) |
|
int *notification) |
|
526 |
{ |
{ |
527 |
|
|
528 |
int xerr; |
int xerr; |
529 |
XVID_DEC_FRAME xframe; |
XVID_DEC_FRAME xframe; |
|
XVID_DEC_STATS xstats; |
|
530 |
|
|
|
xframe.general = 0; |
|
531 |
xframe.bitstream = istream; |
xframe.bitstream = istream; |
532 |
xframe.length = istream_size; |
xframe.length = istream_size; |
533 |
xframe.image = ostream; |
xframe.image = ostream; |
534 |
xframe.stride = XDIM; |
xframe.stride = XDIM; |
535 |
xframe.colorspace = XVID_CSP_YV12; |
xframe.colorspace = XVID_CSP_YV12; |
536 |
|
|
537 |
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, &xstats); |
xerr = xvid_decore(dec_handle, XVID_DEC_DECODE, &xframe, NULL); |
538 |
|
|
539 |
*ostream_size = xframe.length; |
*ostream_size = xframe.length; |
540 |
|
|
|
*notification = xstats.notify; |
|
|
|
|
541 |
return xerr; |
return xerr; |
542 |
} |
} |
543 |
|
|