History

api-example: update to use avcodec_decode_audio4()

Justin Ruggles committed 15 years ago

Commit 0bafe041 parent f199f381 file · +20 −9Browse files

Showing the changes to libavcodec/api-example.c (1 of 1 file). Show all

Unified · Split

libavcodec/api-example.c+20−9
@@ -38,6 +38,7 @@ · 37 unmodified lines
3838
3939 #include "libavcodec/avcodec.h"
4040 #include "libavutil/mathematics.h"
41+#include "libavutil/samplefmt.h"
4142
4243 #define INBUF_SIZE 4096
4344 #define AUDIO_INBUF_SIZE 20480
@@ -118,11 +119,11 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 74 unmodified lines
118119 {
119120 AVCodec *codec;
120121 AVCodecContext *c= NULL;
121− int out_size, len;
122+ int len;
122123 FILE *f, *outfile;
123− uint8_t *outbuf;
124124 uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
125125 AVPacket avpkt;
126+ AVFrame *decoded_frame = NULL;
126127
127128 av_init_packet(&avpkt);
128129
@@ -143,8 +144,6 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 14 unmodified lines
143144 exit(1);
144145 }
145146
146− outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
147−
148147 f = fopen(filename, "rb");
149148 if (!f) {
150149 fprintf(stderr, "could not open %s\n", filename);
@@ -161,15 +160,27 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 10 unmodified lines
161160 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
162161
163162 while (avpkt.size > 0) {
164− out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
165− len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
163+ int got_frame = 0;
164+
165+ if (!decoded_frame) {
166+ if (!(decoded_frame = avcodec_alloc_frame())) {
167+ fprintf(stderr, "out of memory\n");
168+ exit(1);
169+ }
170+ } else
171+ avcodec_get_frame_defaults(decoded_frame);
172+
173+ len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
166174 if (len < 0) {
167175 fprintf(stderr, "Error while decoding\n");
168176 exit(1);
169177 }
170− if (out_size > 0) {
178+ if (got_frame) {
171179 /* if a frame has been decoded, output it */
172− fwrite(outbuf, 1, out_size, outfile);
180+ int data_size = av_samples_get_buffer_size(NULL, c->channels,
181+ decoded_frame->nb_samples,
182+ c->sample_fmt, 1);
183+ fwrite(decoded_frame->data[0], 1, data_size, outfile);
173184 }
174185 avpkt.size -= len;
175186 avpkt.data += len;
@@ -189,10 +200,10 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 13 unmodified lines
189200
190201 fclose(outfile);
191202 fclose(f);
192− free(outbuf);
193203
194204 avcodec_close(c);
195205 av_free(c);
206+ av_free(decoded_frame);
196207 }
197208
198209 /*

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