--- branches/dev-api-4/xvidcore/examples/xvid_decraw.c 2003/03/11 23:39:47 918 +++ branches/dev-api-4/xvidcore/examples/xvid_decraw.c 2003/08/09 17:19:20 1119 @@ -4,6 +4,7 @@ * - Console based decoding test application - * * Copyright(C) 2002-2003 Christoph Lampert + * 2002-2003 Edouard Gomez * * 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 +20,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.7.2.1 2003-03-11 23:39:47 edgomez Exp $ + * $Id: xvid_decraw.c,v 1.7.2.5 2003-08-09 17:19:15 edgomez Exp $ * ****************************************************************************/ @@ -29,24 +30,11 @@ * * An MPEG-4 bitstream is read from an input file (or stdin) and decoded, * the speed for this is measured. - * - * The program is plain C and needs no libraries except for libxvidcore, - * and maths-lib, so with UN*X you simply compile by - * - * gcc xvid_decraw.c -lxvidcore -lm -o xvid_decraw - * - * 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) - * -i string : input filename (default=stdin) - * -t integer : input data type (raw=0, mp4u=1) - * -d : save decoder output (0 False*, !=0 True) - * -m : save mpeg4 raw stream to single files (0 False*, !=0 True) - * -help : This help message - * (* means default) + * The program is plain C and needs no libraries except for libxvidcore, + * and maths-lib. + * + * Use ./xvid_decraw -help for a list of options * ****************************************************************************/ @@ -79,7 +67,7 @@ static char filepath[256] = "./"; static void *dec_handle = NULL; -# define BUFFER_SIZE (2*1024*1024) +#define BUFFER_SIZE (2*1024*1024) /***************************************************************************** * Local prototypes @@ -96,6 +84,18 @@ static int dec_stop(); static void usage(); + +const char * type2str(int type) +{ + if (type==XVID_TYPE_IVOP) + return "I"; + if (type==XVID_TYPE_PVOP) + return "P"; + if (type==XVID_TYPE_BVOP) + return "B"; + return "S"; +} + /***************************************************************************** * Main program ****************************************************************************/ @@ -105,9 +105,7 @@ unsigned char *mp4_buffer = NULL; unsigned char *mp4_ptr = NULL; unsigned char *out_buffer = NULL; - unsigned char *type = NULL; - int still_left_in_buffer; - int delayed_frames; + int useful_bytes; xvid_dec_stats_t xvid_dec_stats; double totaldectime; @@ -134,26 +132,20 @@ if (strcmp("-asm", argv[i]) == 0 ) { use_assembler = 1; - } - else if (strcmp("-d", argv[i]) == 0) { + } else if (strcmp("-d", argv[i]) == 0) { ARG_SAVEDECOUTPUT = 1; - } - else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { + } else if (strcmp("-i", argv[i]) == 0 && i < argc - 1 ) { i++; ARG_INPUTFILE = argv[i]; - } - else if (strcmp("-m", argv[i]) == 0) { + } else if (strcmp("-m", argv[i]) == 0) { ARG_SAVEMPEGSTREAM = 1; - } - else if (strcmp("-help", argv[i])) { + } else if (strcmp("-help", argv[i]) == 0) { usage(); return(0); - } - else { + } else { usage(); exit(-1); } - } /***************************************************************************** @@ -199,17 +191,14 @@ ****************************************************************************/ /* Fill the buffer */ - still_left_in_buffer = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); + useful_bytes = fread(mp4_buffer, 1, BUFFER_SIZE, in_file); totaldectime = 0; totalsize = 0; filenr = 0; - delayed_frames = 0; mp4_ptr = mp4_buffer; do { - - int mp4_size = (mp4_buffer + BUFFER_SIZE - mp4_ptr); int used_bytes = 0; double dectime; @@ -217,25 +206,23 @@ * 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); + if (mp4_ptr > mp4_buffer + BUFFER_SIZE/2) { + int already_in_buffer = (mp4_buffer + BUFFER_SIZE - mp4_ptr); /* Move data if needed */ - if (rest) - memcpy(mp4_buffer, mp4_ptr, rest); + if (already_in_buffer > 0) + memcpy(mp4_buffer, mp4_ptr, already_in_buffer); /* Update mp4_ptr */ mp4_ptr = mp4_buffer; /* read new data */ - if(feof(in_file)) + if(feof(in_file)) break; - still_left_in_buffer = fread(mp4_buffer + rest, - 1, - BUFFER_SIZE - rest, - in_file); + useful_bytes += fread(mp4_buffer + already_in_buffer, + 1, BUFFER_SIZE - already_in_buffer, + in_file); } @@ -245,69 +232,53 @@ /* Decode frame */ dectime = msecond(); - used_bytes = dec_main(mp4_ptr, out_buffer, mp4_size, &xvid_dec_stats); + used_bytes = dec_main(mp4_ptr, out_buffer, useful_bytes, &xvid_dec_stats); dectime = msecond() - dectime; /* Resize image buffer if needed */ if(xvid_dec_stats.type == XVID_TYPE_VOL) { - /* Free old output buffer*/ - if(out_buffer) free(out_buffer); + /* Check if old buffer is smaller */ + if(XDIM*YDIM < xvid_dec_stats.data.vol.width*xvid_dec_stats.data.vol.height) { + + /* Copy new witdh and new height from the vol structure */ + XDIM = xvid_dec_stats.data.vol.width; + YDIM = xvid_dec_stats.data.vol.height; + + /* Free old output buffer*/ + if(out_buffer) free(out_buffer); + + /* Allocate the new buffer */ + out_buffer = (unsigned char*)malloc(XDIM*YDIM*4); + if(out_buffer == NULL) + goto free_all_memory; - /* Copy witdh and height from the vol structure */ - XDIM = xvid_dec_stats.data.vol.width; - YDIM = xvid_dec_stats.data.vol.height; - - /* Allocate the new buffer */ - if((out_buffer = (unsigned char*)malloc(XDIM*YDIM*4)) == NULL) - goto free_all_memory; + fprintf(stderr, "Resized frame buffer to %dx%d\n", XDIM, YDIM); + } } /* Update buffer pointers */ if(used_bytes > 0) { mp4_ptr += used_bytes; - still_left_in_buffer -= used_bytes; + useful_bytes -= used_bytes; /* Total size */ totalsize += used_bytes; } - }while(xvid_dec_stats.type != XVID_TYPE_IVOP && - xvid_dec_stats.type != XVID_TYPE_PVOP && - xvid_dec_stats.type != XVID_TYPE_BVOP && - xvid_dec_stats.type != XVID_TYPE_SVOP && - still_left_in_buffer > 0); + }while(xvid_dec_stats.type <= 0 && useful_bytes > 0); - /* Negative buffer would mean we went too far */ - if(still_left_in_buffer < 0) break; + /* Check if there is a negative number of useful bytes left in buffer + * This means we went too far */ + if(useful_bytes < 0) + break; - /* Skip when decoder is buffering images because of bframes */ - if(xvid_dec_stats.type == XVID_TYPE_NOTHING) { - delayed_frames++; - continue; - } - - /* Updated data - Count only usefull decode time */ + /* Updated data - Count only usefull decode time */ totaldectime += dectime; - /* Prints some decoding stats */ - switch(xvid_dec_stats.type) { - case XVID_TYPE_IVOP: - type = "I"; - break; - case XVID_TYPE_PVOP: - type = "P"; - break; - case XVID_TYPE_BVOP: - type = "B"; - break; - case XVID_TYPE_SVOP: - type = "S"; - break; - } - - printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", - filenr, type, dectime, used_bytes); + + printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", + filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); /* Save individual mpeg4 stream if required */ if(ARG_SAVEMPEGSTREAM) { @@ -344,23 +315,28 @@ * Flush decoder buffers ****************************************************************************/ - while(delayed_frames--) { + do { /* Fake vars */ int used_bytes; double dectime; - /* Decode frame */ - dectime = msecond(); - used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); - dectime = msecond() - dectime; + do { + dectime = msecond(); + used_bytes = dec_main(NULL, out_buffer, -1, &xvid_dec_stats); + dectime = msecond() - dectime; + }while(used_bytes>=0 && xvid_dec_stats.type <= 0); + + if (used_bytes < 0) { /* XVID_ERR_END */ + 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); + printf("Frame %5d: type = %s, dectime(ms) =%6.1f, length(bytes) =%7d\n", + filenr, type2str(xvid_dec_stats.type), dectime, used_bytes); /* Save output frame if required */ if (ARG_SAVEDECOUTPUT) { @@ -374,7 +350,7 @@ filenr++; - } + }while(1); /***************************************************************************** * Calculate totals and averages for output, print results @@ -411,11 +387,10 @@ static void usage() { - fprintf(stderr, "Usage : xvid_decraw <-w width> <-h height> [OPTIONS]\n"); + fprintf(stderr, "Usage : xvid_decraw [OPTIONS]\n"); fprintf(stderr, "Options :\n"); fprintf(stderr, " -asm : use assembly optimizations (default=disabled)\n"); fprintf(stderr, " -i string : input filename (default=stdin)\n"); - fprintf(stderr, " -t integer : input data type (raw=0, mp4u=1)\n"); fprintf(stderr, " -d : save decoder output\n"); fprintf(stderr, " -m : save mpeg4 raw stream to individual files\n"); fprintf(stderr, " -help : This help message\n");