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
39#include "libavutil/mathematics.h"39#include "libavutil/mathematics.h"
40 40
41#define INBUF_SIZE 409641#define INBUF_SIZE 4096
42#define AUDIO_INBUF_SIZE 20480
43#define AUDIO_REFILL_THRESH 4096
42 44
43/*45/*
44 * Audio encoding example46 * Audio encoding example
@@ -118,7 +120,7 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 73 unmodified lines
118 int out_size, len;120 int out_size, len;
119 FILE *f, *outfile;121 FILE *f, *outfile;
120 uint8_t *outbuf;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 AVPacket avpkt;124 AVPacket avpkt;
123 125
124 av_init_packet(&avpkt);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 /* decode until eof */158 /* decode until eof */
157 avpkt.data = inbuf;159 avpkt.data = inbuf;
158 for(;;) {160 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
159 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
160 if (avpkt.size == 0)
161 break;
162 161
163 avpkt.data = inbuf;
164 while (avpkt.size > 0) {162 while (avpkt.size > 0) {
165 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;163 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
166 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);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 avpkt.size -= len;173 avpkt.size -= len;
176 avpkt.data += len;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 fclose(outfile);189 fclose(outfile);
181 fclose(f);190 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