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 | #include "libavcodec/avcodec.h" | 39 | #include "libavcodec/avcodec.h" |
| 40 | #include "libavutil/mathematics.h" | 40 | #include "libavutil/mathematics.h" |
| 41 | #include "libavutil/samplefmt.h" | ||
| 41 | 42 | ||
| 42 | #define INBUF_SIZE 4096 | 43 | #define INBUF_SIZE 4096 |
| 43 | #define AUDIO_INBUF_SIZE 20480 | 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 | AVCodec *codec; | 120 | AVCodec *codec; |
| 120 | AVCodecContext *c= NULL; | 121 | AVCodecContext *c= NULL; |
| 121 | int out_size, len; | 122 | int len; |
| 122 | FILE *f, *outfile; | 123 | FILE *f, *outfile; |
| 123 | uint8_t *outbuf; | ||
| 124 | uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; | 124 | uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE]; |
| 125 | AVPacket avpkt; | 125 | AVPacket avpkt; |
| 126 | AVFrame *decoded_frame = NULL; | ||
| 126 | 127 | ||
| 127 | av_init_packet(&avpkt); | 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 | exit(1); | 144 | exit(1); |
| 144 | } | 145 | } |
| 145 | 146 | ||
| 146 | outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); | ||
| 147 | |||
| 148 | f = fopen(filename, "rb"); | 147 | f = fopen(filename, "rb"); |
| 149 | if (!f) { | 148 | if (!f) { |
| 150 | fprintf(stderr, "could not open %s\n", filename); | 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 | avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); | 160 | avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f); |
| 162 | 161 | ||
| 163 | while (avpkt.size > 0) { | 162 | while (avpkt.size > 0) { |
| 164 | out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE; | 163 | int got_frame = 0; |
| 165 | len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt); | 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 | if (len < 0) { | 174 | if (len < 0) { |
| 167 | fprintf(stderr, "Error while decoding\n"); | 175 | fprintf(stderr, "Error while decoding\n"); |
| 168 | exit(1); | 176 | exit(1); |
| 169 | } | 177 | } |
| 170 | if (out_size > 0) { | 178 | if (got_frame) { |
| 171 | /* if a frame has been decoded, output it */ | 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 | avpkt.size -= len; | 185 | avpkt.size -= len; |
| 175 | avpkt.data += len; | 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 | fclose(outfile); | 201 | fclose(outfile); |
| 191 | fclose(f); | 202 | fclose(f); |
| 192 | free(outbuf); | ||
| 193 | 203 | ||
| 194 | avcodec_close(c); | 204 | avcodec_close(c); |
| 195 | av_free(c); | 205 | av_free(c); |
| 206 | av_free(decoded_frame); | ||
| 196 | } | 207 | } |
| 197 | 208 | ||
| 198 | /* | 209 | /* |