--- branches/dev-api-3/xvidcore/examples/xvid_decraw.c 2003/01/19 15:10:11 794 +++ branches/dev-api-3/xvidcore/examples/xvid_decraw.c 2003/01/19 15:13:47 795 @@ -3,7 +3,7 @@ * XVID MPEG-4 VIDEO CODEC * - Console based decoding test application - * - * Copyright(C) 2002 Christoph Lampert + * Copyright(C) 2002-2003 Christoph Lampert * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -19,7 +19,7 @@ * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * - * $Id: xvid_decraw.c,v 1.1.2.2 2003-01-13 00:37:20 edgomez Exp $ + * $Id: xvid_decraw.c,v 1.1.2.3 2003-01-19 15:13:47 edgomez Exp $ * ****************************************************************************/ @@ -75,7 +75,6 @@ static int YDIM = 0; static int ARG_SAVEDECOUTPUT = 0; static int ARG_SAVEMPEGSTREAM = 0; -static int ARG_STREAMTYPE = 0; static char *ARG_INPUTFILE = NULL; @@ -84,12 +83,6 @@ # define BUFFER_SIZE 10*XDIM*YDIM -#define LONG_PACK(a,b,c,d) ((long) (((long)(a))<<24) | (((long)(b))<<16) | \ - (((long)(c))<<8) |((long)(d))) - -#define SWAP(a) ( (((a)&0x000000ff)<<24) | (((a)&0x0000ff00)<<8) | \ - (((a)&0x00ff0000)>>8) | (((a)&0xff000000)>>24) ) - /***************************************************************************** * Local prototypes ****************************************************************************/ @@ -99,9 +92,10 @@ unsigned char *image); static int dec_init(int use_assembler); static int dec_main(unsigned char *istream, - unsigned char *ostream, - int istream_size, - int *ostream_size); + unsigned char *ostream, + int istream_size, + int *ostream_size, + int *isframe); static int dec_stop(); static void usage(); @@ -115,7 +109,8 @@ unsigned char *mp4_ptr = NULL; unsigned char *out_buffer = NULL; int bigendian = 0; - int still_left_in_packet; + int still_left_in_buffer; + int delayed_frames; double totaldectime; @@ -131,7 +126,7 @@ int i; printf("xvid_decraw - raw mpeg4 bitstream decoder "); - printf("written by Christoph Lampert 2002\n\n"); + printf("written by Christoph Lampert 2002-2003\n\n"); /***************************************************************************** * Command line parsing @@ -162,10 +157,6 @@ i++; ARG_SAVEMPEGSTREAM = atoi(argv[i]); } - else if (strcmp("-t", argv[i]) == 0 && i < argc - 1 ) { - i++; - ARG_STREAMTYPE = atoi(argv[i]); - } else if (strcmp("-help", argv[i])) { usage(); return(0); @@ -230,37 +221,13 @@ * Main loop ****************************************************************************/ - totalsize = LONG_PACK('M','P','4','U'); - mp4_ptr = (unsigned char *)&totalsize; - if(*mp4_ptr == 'M') - bigendian = 1; - else - bigendian = 0; - - if(ARG_STREAMTYPE) { - - unsigned char header[4]; - - /* MP4U format : read header */ - if(feof(in_file)) - goto release_all; - fread(header, 4, 1, in_file); - - if(header[0] != 'M' || header[1] != 'P' || - header[2] != '4' || header[3] != 'U') { - fprintf(stderr, "Error, this not a mp4u container file\n"); - goto release_all; - } - - } - else { - fread(mp4_buffer, BUFFER_SIZE, 1, in_file); - } + /* Fill the buffer */ + still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); totaldectime = 0; totalsize = 0; filenr = 0; - still_left_in_packet = 0; + delayed_frames = 0; mp4_ptr = mp4_buffer; do { @@ -268,111 +235,74 @@ int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); int used_bytes = 0; double dectime; + int notification; - /* Read data from input file */ - if(ARG_STREAMTYPE ) { + /* + * If the buffer is half empty or there are no more bytes in it + * then fill it. + */ + if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2 || + still_left_in_buffer <= 0) { + int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); + + /* Move data if needed */ + if (rest) + memcpy(mp4_buffer, mp4_ptr, rest); - /* - * MP4U container - * - * When dealing with mp4u, we have to take care about PBB...B packets - * generated with some XviD options. - * - * We use the still_left_in_packet variable to keep trace of how many - * bytes we loaded at a time. - */ - - /* Still real frames in loaded packet ? */ - if(still_left_in_packet < 7) { - - /* Read stream size first */ - if(feof(in_file)) - break; - fread(&still_left_in_packet, sizeof(long), 1, in_file); - - /* Mp4U container is big endian */ - if(!bigendian) - mp4_size = SWAP(still_left_in_packet); - - /* Read mp4_size_bytes */ - if(feof(in_file)) - break; - fread(mp4_buffer, still_left_in_packet, 1, in_file); - - /* - * When reading mp4u, we don't have to care about buffer - * filling as we know exactly how much bytes there are in - * next frame - */ - mp4_ptr = mp4_buffer; + /* Update mp4_ptr */ + mp4_ptr = mp4_buffer; - } - - } - else { - - /* - * Real raw stream - * - * In raw stream mode,we don't have to care how many bytes there're - * still in packet because we simply let the decore decode frames - * without taking care of buffer overruns or underruns. - * - */ - still_left_in_packet = 0; - - /* buffer more than half empty -> Fill it */ - if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) { - int rest = (mp4_buffer + BUFFER_SIZE - mp4_ptr); - - /* Move data if needed */ - if (rest) - memcpy(mp4_buffer, mp4_ptr, rest); - - /* Update mp4_ptr */ - mp4_ptr = mp4_buffer; - - /* read new data */ - if(feof(in_file)) - break; - - fread(mp4_buffer + rest, BUFFER_SIZE - rest, 1, in_file); + /* read new data */ + if(feof(in_file)) + break; - } + still_left_in_buffer = fread(mp4_buffer + rest, + 1, + BUFFER_SIZE - rest, + in_file); } - /* The do loop is just to flush NVOPS */ + + /* This loop flushes N_VOPS (with vop_coded bit set to 0) */ do { /* Decode frame */ dectime = msecond(); - status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes); + status = dec_main(mp4_ptr, out_buffer, mp4_size, &used_bytes, ¬ification); dectime = msecond() - dectime; if (status) { break; } - /* - * Only needed for real raw stream, mp4u uses - * mp4_ptr = mp4_buffer for each frame - */ + /* Update buffer pointers */ mp4_ptr += used_bytes; - still_left_in_packet -= used_bytes; + still_left_in_buffer -= used_bytes; /* Total size */ totalsize += used_bytes; - }while(used_bytes <= 7); /* <= 7 bytes is a NVOPS */ + }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 */ totaldectime += dectime; /* Prints some decoding stats */ - printf("Frame %5d: dectime =%6.1f ms length=%7d bytes \n", + printf("Frame %5d: dectime(ms) =%6.1f, length(bytes) =%7d\n", filenr, dectime, used_bytes); - + /* Save individual mpeg4 stream if required */ if (ARG_SAVEMPEGSTREAM) { FILE *filehandle = NULL; @@ -385,7 +315,7 @@ filename); } else { - fwrite(mp4_buffer, used_bytes, 1, filehandle); + fwrite(mp4_buffer, 1, used_bytes, filehandle); fclose(filehandle); } } @@ -404,7 +334,45 @@ } while ( (status>=0) && (filenr