2 * copyright (c) 2001 Fabrice Bellard
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * audio decoding with libavcodec API example
25 * @example decode_audio.c
32 #include "libavutil/frame.h"
33 #include "libavutil/mem.h"
35 #include "libavcodec/avcodec.h"
37 #define AUDIO_INBUF_SIZE 20480
38 #define AUDIO_REFILL_THRESH 4096
40 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
45 /* send the packet with the compressed data to the decoder */
46 ret = avcodec_send_packet(dec_ctx, pkt);
48 fprintf(stderr, "Error submitting the packet to the decoder\n");
52 /* read all the output frames (in general there may be any number of them */
54 ret = avcodec_receive_frame(dec_ctx, frame);
55 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
58 fprintf(stderr, "Error during decoding\n");
62 data_size = av_samples_get_buffer_size(NULL, dec_ctx->channels,
64 dec_ctx->sample_fmt, 1);
65 fwrite(frame->data[0], 1, data_size, outfile);
69 int main(int argc, char **argv)
71 const char *outfilename, *filename;
73 AVCodecContext *c= NULL;
74 AVCodecParserContext *parser = NULL;
77 uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
81 AVFrame *decoded_frame = NULL;
84 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
88 outfilename = argv[2];
90 /* register all the codecs */
91 avcodec_register_all();
93 av_init_packet(&avpkt);
95 /* find the MPEG audio decoder */
96 codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
98 fprintf(stderr, "codec not found\n");
102 parser = av_parser_init(codec->id);
104 fprintf(stderr, "parser not found\n");
108 c = avcodec_alloc_context3(codec);
111 if (avcodec_open2(c, codec, NULL) < 0) {
112 fprintf(stderr, "could not open codec\n");
116 f = fopen(filename, "rb");
118 fprintf(stderr, "could not open %s\n", filename);
121 outfile = fopen(outfilename, "wb");
127 /* decode until eof */
129 data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
131 while (data_size > 0) {
132 if (!decoded_frame) {
133 if (!(decoded_frame = av_frame_alloc())) {
134 fprintf(stderr, "out of memory\n");
139 ret = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size,
141 AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
143 fprintf(stderr, "Error while parsing\n");
150 decode(c, &avpkt, decoded_frame, outfile);
152 if (data_size < AUDIO_REFILL_THRESH) {
153 memmove(inbuf, data, data_size);
155 len = fread(data + data_size, 1,
156 AUDIO_INBUF_SIZE - data_size, f);
165 avcodec_free_context(&c);
166 av_parser_close(parser);
167 av_frame_free(&decoded_frame);