Showing the changes to libavcodec/api-example.c (1 of 1 file). Show all
Unified · Split
| @@ -38,6 +38,7 @@ · 37 unmodified lines | ||
| 38 | 38 | |
| 39 | 39 | #include "libavcodec/avcodec.h" |
| 40 | 40 | #include "libavutil/mathematics.h" |
| 41 | +#include "libavutil/samplefmt.h" | |
| 41 | 42 | |
| 42 | 43 | #define INBUF_SIZE 4096 |
| 43 | 44 | #define AUDIO_INBUF_SIZE 20480 |
| @@ -118,11 +119,11 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 74 unmodified lines | ||
| 118 | 119 | { |
| 119 | 120 | AVCodec *codec; |
| 120 | 121 | AVCodecContext *c= NULL; |
| 121 | − int out_size, len; | |
| 122 | + int len; | |
| 122 | 123 | FILE *f, *outfile; |
| 123 | − uint8_t *outbuf; | |
| 124 | 124 | uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
| 125 | 125 | AVPacket avpkt; |
| 126 | + AVFrame *decoded_frame = NULL; | |
| 126 | 127 | |
| 127 | 128 | av_init_packet(&avpkt); |
| 128 | 129 | |
| @@ -143,8 +144,6 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 14 unmodified lines | ||
| 143 | 144 | exit(1); |
| 144 | 145 | } |
| 145 | 146 | |
| 146 | − outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); | |
| 147 | − | |
| 148 | 147 | f = fopen(filename, "rb"); |
| 149 | 148 | if (!f) { |
| 150 | 149 | 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 | ||
| 161 | 160 | avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); |
| 162 | 161 | |
| 163 | 162 | 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); | |
| 166 | 174 | if (len < 0) { |
| 167 | 175 | fprintf(stderr, "Error while decoding\n"); |
| 168 | 176 | exit(1); |
| 169 | 177 | } |
| 170 | − if (out_size > 0) { | |
| 178 | + if (got_frame) { | |
| 171 | 179 | /* 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); | |
| 173 | 184 | } |
| 174 | 185 | avpkt.size -= len; |
| 175 | 186 | avpkt.data += len; |
| @@ -189,10 +200,10 @@ static void audio_decode_example(const char *outfilename, const char *filename) · 13 unmodified lines | ||
| 189 | 200 | |
| 190 | 201 | fclose(outfile); |
| 191 | 202 | fclose(f); |
| 192 | − free(outbuf); | |
| 193 | 203 | |
| 194 | 204 | avcodec_close(c); |
| 195 | 205 | av_free(c); |
| 206 | + av_free(decoded_frame); | |
| 196 | 207 | } |
| 197 | 208 | |
| 198 | 209 | /* |