2 * Copyright (c) 2001 Fabrice Bellard
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 * audio decoding with libavcodec API example
27 * @example decode_audio.c
34 #include <libavutil/frame.h>
35 #include <libavutil/mem.h>
37 #include <libavcodec/avcodec.h>
39 #define AUDIO_INBUF_SIZE 20480
40 #define AUDIO_REFILL_THRESH 4096
42 int main(int argc, char **argv)
44 const char *outfilename, *filename;
46 AVCodecContext *c= NULL;
49 uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
51 AVFrame *decoded_frame = NULL;
54 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
58 outfilename = argv[2];
60 /* register all the codecs */
61 avcodec_register_all();
63 av_init_packet(&avpkt);
65 /* find the MPEG audio decoder */
66 codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
68 fprintf(stderr, "Codec not found\n");
72 c = avcodec_alloc_context3(codec);
74 fprintf(stderr, "Could not allocate audio codec context\n");
79 if (avcodec_open2(c, codec, NULL) < 0) {
80 fprintf(stderr, "Could not open codec\n");
84 f = fopen(filename, "rb");
86 fprintf(stderr, "Could not open %s\n", filename);
89 outfile = fopen(outfilename, "wb");
95 /* decode until eof */
97 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
99 while (avpkt.size > 0) {
103 if (!decoded_frame) {
104 if (!(decoded_frame = av_frame_alloc())) {
105 fprintf(stderr, "Could not allocate audio frame\n");
110 len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
112 fprintf(stderr, "Error while decoding\n");
116 /* if a frame has been decoded, output it */
117 int data_size = av_get_bytes_per_sample(c->sample_fmt);
119 /* This should not occur, checking just for paranoia */
120 fprintf(stderr, "Failed to calculate data size\n");
123 for (i=0; i<decoded_frame->nb_samples; i++)
124 for (ch=0; ch<c->channels; ch++)
125 fwrite(decoded_frame->data[ch] + data_size*i, 1, data_size, outfile);
130 avpkt.pts = AV_NOPTS_VALUE;
131 if (avpkt.size < AUDIO_REFILL_THRESH) {
132 /* Refill the input buffer, to avoid trying to decode
133 * incomplete frames. Instead of this, one could also use
134 * a parser, or use a proper container format through
136 memmove(inbuf, avpkt.data, avpkt.size);
138 len = fread(avpkt.data + avpkt.size, 1,
139 AUDIO_INBUF_SIZE - avpkt.size, f);
148 avcodec_free_context(&c);
149 av_frame_free(&decoded_frame);