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 static int get_format_from_sample_fmt(const char **fmt,
43 enum AVSampleFormat sample_fmt)
46 struct sample_fmt_entry {
47 enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
48 } sample_fmt_entries[] = {
49 { AV_SAMPLE_FMT_U8, "u8", "u8" },
50 { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
51 { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
52 { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
53 { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
57 for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
58 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
59 if (sample_fmt == entry->sample_fmt) {
60 *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
66 "sample format %s is not supported as output format\n",
67 av_get_sample_fmt_name(sample_fmt));
71 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
77 /* send the packet with the compressed data to the decoder */
78 ret = avcodec_send_packet(dec_ctx, pkt);
80 fprintf(stderr, "Error submitting the packet to the decoder\n");
84 /* read all the output frames (in general there may be any number of them */
86 ret = avcodec_receive_frame(dec_ctx, frame);
87 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
90 fprintf(stderr, "Error during decoding\n");
93 data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
95 /* This should not occur, checking just for paranoia */
96 fprintf(stderr, "Failed to calculate data size\n");
99 for (i = 0; i < frame->nb_samples; i++)
100 for (ch = 0; ch < dec_ctx->channels; ch++)
101 fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile);
105 int main(int argc, char **argv)
107 const char *outfilename, *filename;
108 const AVCodec *codec;
109 AVCodecContext *c= NULL;
110 AVCodecParserContext *parser = NULL;
113 uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
117 AVFrame *decoded_frame = NULL;
118 enum AVSampleFormat sfmt;
123 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
127 outfilename = argv[2];
129 pkt = av_packet_alloc();
131 /* find the MPEG audio decoder */
132 codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
134 fprintf(stderr, "Codec not found\n");
138 parser = av_parser_init(codec->id);
140 fprintf(stderr, "Parser not found\n");
144 c = avcodec_alloc_context3(codec);
146 fprintf(stderr, "Could not allocate audio codec context\n");
151 if (avcodec_open2(c, codec, NULL) < 0) {
152 fprintf(stderr, "Could not open codec\n");
156 f = fopen(filename, "rb");
158 fprintf(stderr, "Could not open %s\n", filename);
161 outfile = fopen(outfilename, "wb");
167 /* decode until eof */
169 data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
171 while (data_size > 0) {
172 if (!decoded_frame) {
173 if (!(decoded_frame = av_frame_alloc())) {
174 fprintf(stderr, "Could not allocate audio frame\n");
179 ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
181 AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
183 fprintf(stderr, "Error while parsing\n");
190 decode(c, pkt, decoded_frame, outfile);
192 if (data_size < AUDIO_REFILL_THRESH) {
193 memmove(inbuf, data, data_size);
195 len = fread(data + data_size, 1,
196 AUDIO_INBUF_SIZE - data_size, f);
202 /* flush the decoder */
205 decode(c, pkt, decoded_frame, outfile);
207 /* print output pcm infomations, because there have no metadata of pcm */
208 sfmt = c->sample_fmt;
210 if (av_sample_fmt_is_planar(sfmt)) {
211 const char *packed = av_get_sample_fmt_name(sfmt);
212 printf("Warning: the sample format the decoder produced is planar "
213 "(%s). This example will output the first channel only.\n",
214 packed ? packed : "?");
215 sfmt = av_get_packed_sample_fmt(sfmt);
218 n_channels = c->channels;
219 if ((ret = get_format_from_sample_fmt(&fmt, sfmt)) < 0)
222 printf("Play the output audio file with the command:\n"
223 "ffplay -f %s -ac %d -ar %d %s\n",
224 fmt, n_channels, c->sample_rate,
230 avcodec_free_context(&c);
231 av_parser_close(parser);
232 av_frame_free(&decoded_frame);
233 av_packet_free(&pkt);