Mercurial > libavcodec.hg
comparison api-example.c @ 11765:c96e649c4e0a libavcodec
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.
author | mstorsjo |
---|---|
date | Tue, 25 May 2010 19:13:28 +0000 |
parents | 7dd2a45249a9 |
children | 01408f7d629c |
comparison
equal
deleted
inserted
replaced
11764:e9c024d542f4 | 11765:c96e649c4e0a |
---|---|
37 | 37 |
38 #include "libavcodec/avcodec.h" | 38 #include "libavcodec/avcodec.h" |
39 #include "libavutil/mathematics.h" | 39 #include "libavutil/mathematics.h" |
40 | 40 |
41 #define INBUF_SIZE 4096 | 41 #define INBUF_SIZE 4096 |
42 #define AUDIO_INBUF_SIZE 20480 | |
43 #define AUDIO_REFILL_THRESH 4096 | |
42 | 44 |
43 /* | 45 /* |
44 * Audio encoding example | 46 * Audio encoding example |
45 */ | 47 */ |
46 static void audio_encode_example(const char *filename) | 48 static void audio_encode_example(const char *filename) |
116 AVCodec *codec; | 118 AVCodec *codec; |
117 AVCodecContext *c= NULL; | 119 AVCodecContext *c= NULL; |
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); |
125 | 127 |
126 printf("Audio decoding\n"); | 128 printf("Audio decoding\n"); |
153 exit(1); | 155 exit(1); |
154 } | 156 } |
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); | 161 |
160 if (avpkt.size == 0) | |
161 break; | |
162 | |
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); |
167 if (len < 0) { | 165 if (len < 0) { |
168 fprintf(stderr, "Error while decoding\n"); | 166 fprintf(stderr, "Error while decoding\n"); |
172 /* if a frame has been decoded, output it */ | 170 /* if a frame has been decoded, output it */ |
173 fwrite(outbuf, 1, out_size, outfile); | 171 fwrite(outbuf, 1, out_size, outfile); |
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); |
182 free(outbuf); | 191 free(outbuf); |
183 | 192 |