diff options
author | Reinhard Tartler <siretart@tauware.de> | 2010-05-29 14:20:05 +0000 |
---|---|---|
committer | Reinhard Tartler <siretart@tauware.de> | 2010-05-29 14:20:05 +0000 |
commit | 46ac6315fdda4580dbefde96870d848c5bbca608 (patch) | |
tree | 1688b13115e632752de2d3a53b250651e7e5feb0 | |
parent | 9052b5b73ba45520ed4f9d7db1c3458c641f8d14 (diff) | |
download | ffmpeg-46ac6315fdda4580dbefde96870d848c5bbca608.tar.gz |
api-example: Try to avoid decoding incomplete frames
Use a larger input audio buffer, refill it when it has less than 4 KB data
left.
backport r23323 by mstorsjo
Originally committed as revision 23377 to svn://svn.ffmpeg.org/ffmpeg/branches/0.6
-rw-r--r-- | libavcodec/api-example.c | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/libavcodec/api-example.c b/libavcodec/api-example.c index fb48b1f6d6..f34075e666 100644 --- a/libavcodec/api-example.c +++ b/libavcodec/api-example.c @@ -39,6 +39,8 @@ #include "libavutil/mathematics.h" #define INBUF_SIZE 4096 +#define AUDIO_INBUF_SIZE 20480 +#define AUDIO_REFILL_THRESH 4096 /* * Audio encoding example @@ -118,7 +120,7 @@ static void audio_decode_example(const char *outfilename, const char *filename) int out_size, len; FILE *f, *outfile; uint8_t *outbuf; - uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; + uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; AVPacket avpkt; av_init_packet(&avpkt); @@ -155,25 +157,32 @@ static void audio_decode_example(const char *outfilename, const char *filename) /* decode until eof */ avpkt.data = inbuf; - for(;;) { - avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); - if (avpkt.size == 0) - break; - - avpkt.data = inbuf; - while (avpkt.size > 0) { - out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; - len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); - if (len < 0) { - fprintf(stderr, "Error while decoding\n"); - exit(1); - } - if (out_size > 0) { - /* if a frame has been decoded, output it */ - fwrite(outbuf, 1, out_size, outfile); - } - avpkt.size -= len; - avpkt.data += len; + avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); + + while (avpkt.size > 0) { + out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; + len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); + if (len < 0) { + fprintf(stderr, "Error while decoding\n"); + exit(1); + } + if (out_size > 0) { + /* if a frame has been decoded, output it */ + fwrite(outbuf, 1, out_size, outfile); + } + avpkt.size -= len; + avpkt.data += len; + if (avpkt.size < AUDIO_REFILL_THRESH) { + /* Refill the input buffer, to avoid trying to decode + * incomplete frames. Instead of this, one could also use + * a parser, or use a proper container format through + * libavformat. */ + memmove(inbuf, avpkt.data, avpkt.size); + avpkt.data = inbuf; + len = fread(avpkt.data + avpkt.size, 1, + AUDIO_INBUF_SIZE - avpkt.size, f); + if (len > 0) + avpkt.size += len; } } |