History

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. Originally committed as revision 23323 to svn://svn.ffmpeg.org/ffmpeg/trunk

Martin Storsjö committed 16 years ago

Commit 11f61811 parent 0ecfa7b1 file · +16 −7Browse files

Unified · Split

libavcodec/api-example.c+16−7
@@ -39,6 +39,8 @@ · 38 unmodified lines
3939 #include "libavutil/mathematics.h"
4040
4141 #define INBUF_SIZE 4096
42+#define AUDIO_INBUF_SIZE 20480
43+#define AUDIO_REFILL_THRESH 4096
4244
4345 /*
4446 * Audio encoding example
@@ -118,7 +120,7 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 73 unmodified lines
118120 int out_size, len;
119121 FILE *f, *outfile;
120122 uint8_t *outbuf;
121− uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
123+ uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
122124 AVPacket avpkt;
123125
124126 av_init_packet(&avpkt);
@@ -155,12 +157,8 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 30 unmodified lines
155157
156158 /* decode until eof */
157159 avpkt.data = inbuf;
158− for(;;) {
159− avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
160− if (avpkt.size == 0)
161− break;
160+ avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
162161
163− avpkt.data = inbuf;
164162 while (avpkt.size > 0) {
165163 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
166164 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
@@ -174,8 +172,19 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 7 unmodified lines
174172 }
175173 avpkt.size -= len;
176174 avpkt.data += len;
175+ if (avpkt.size < AUDIO_REFILL_THRESH) {
176+ /* Refill the input buffer, to avoid trying to decode
177+ * incomplete frames. Instead of this, one could also use
178+ * a parser, or use a proper container format through
179+ * libavformat. */
180+ memmove(inbuf, avpkt.data, avpkt.size);
181+ avpkt.data = inbuf;
182+ len = fread(avpkt.data + avpkt.size, 1,
183+ AUDIO_INBUF_SIZE - avpkt.size, f);
184+ if (len > 0)
185+ avpkt.size += len;
186+ }
177187 }
178− }
179188
180189 fclose(outfile);
181190 fclose(f);

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t