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
Unified · Split
| @@ -39,6 +39,8 @@ · 38 unmodified lines | ||
| 39 | 39 | #include "libavutil/mathematics.h" |
| 40 | 40 | |
| 41 | 41 | #define INBUF_SIZE 4096 |
| 42 | +#define AUDIO_INBUF_SIZE 20480 | |
| 43 | +#define AUDIO_REFILL_THRESH 4096 | |
| 42 | 44 | |
| 43 | 45 | /* |
| 44 | 46 | * Audio encoding example |
| @@ -118,7 +120,7 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 73 unmodified lines | ||
| 118 | 120 | int out_size, len; |
| 119 | 121 | FILE *f, *outfile; |
| 120 | 122 | 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]; | |
| 122 | 124 | AVPacket avpkt; |
| 123 | 125 | |
| 124 | 126 | av_init_packet(&avpkt); |
| @@ -155,12 +157,8 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 30 unmodified lines | ||
| 155 | 157 | |
| 156 | 158 | /* decode until eof */ |
| 157 | 159 | 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); | |
| 162 | 161 | |
| 163 | − avpkt.data = inbuf; | |
| 164 | 162 | while (avpkt.size > 0) { |
| 165 | 163 | out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; |
| 166 | 164 | 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 | ||
| 174 | 172 | } |
| 175 | 173 | avpkt.size -= len; |
| 176 | 174 | 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 | + } | |
| 177 | 187 | } |
| 178 | − } | |
| 179 | 188 | |
| 180 | 189 | fclose(outfile); |
| 181 | 190 | fclose(f); |