examples/decode_audio: allocate the packet dynamically
[libav.git] / doc / examples / decode_audio.c
1 /*
2 * copyright (c) 2001 Fabrice Bellard
3 *
4 * This file is part of Libav.
5 *
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.
10 *
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.
15 *
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
19 */
20
21 /**
22 * @file
23 * audio decoding with libavcodec API example
24 *
25 * @example decode_audio.c
26 */
27
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31
32 #include "libavutil/channel_layout.h"
33 #include "libavutil/frame.h"
34 #include "libavutil/mem.h"
35
36 #include "libavcodec/avcodec.h"
37
38 #define AUDIO_INBUF_SIZE 20480
39 #define AUDIO_REFILL_THRESH 4096
40
41 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
42 FILE *outfile)
43 {
44 int16_t *interleave_buf;
45 int ret, data_size, i;
46
47 /* send the packet with the compressed data to the decoder */
48 ret = avcodec_send_packet(dec_ctx, pkt);
49 if (ret < 0) {
50 fprintf(stderr, "Error submitting the packet to the decoder\n");
51 exit(1);
52 }
53
54 /* read all the output frames (in general there may be any number of them */
55 while (ret >= 0) {
56 ret = avcodec_receive_frame(dec_ctx, frame);
57 if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
58 return;
59 else if (ret < 0) {
60 fprintf(stderr, "Error during decoding\n");
61 exit(1);
62 }
63
64 /* the stream parameters may change at any time, check that they are
65 * what we expect */
66 if (av_get_channel_layout_nb_channels(frame->channel_layout) != 2 ||
67 frame->format != AV_SAMPLE_FMT_S16P) {
68 fprintf(stderr, "Unsupported frame parameters\n");
69 exit(1);
70 }
71
72 /* The decoded data is signed 16-bit planar -- each channel in its own
73 * buffer. We interleave the two channels manually here, but using
74 * libavresample is recommended instead. */
75 data_size = sizeof(*interleave_buf) * 2 * frame->nb_samples;
76 interleave_buf = av_malloc(data_size);
77 if (!interleave_buf)
78 exit(1);
79
80 for (i = 0; i < frame->nb_samples; i++) {
81 interleave_buf[2 * i] = ((int16_t*)frame->data[0])[i];
82 interleave_buf[2 * i + 1] = ((int16_t*)frame->data[1])[i];
83 }
84 fwrite(interleave_buf, 1, data_size, outfile);
85 av_freep(&interleave_buf);
86 }
87 }
88
89 int main(int argc, char **argv)
90 {
91 const char *outfilename, *filename;
92 const AVCodec *codec;
93 AVCodecContext *c= NULL;
94 AVCodecParserContext *parser = NULL;
95 int len, ret;
96 FILE *f, *outfile;
97 uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
98 uint8_t *data;
99 size_t data_size;
100 AVPacket *pkt;
101 AVFrame *decoded_frame = NULL;
102
103 if (argc <= 2) {
104 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
105 exit(0);
106 }
107 filename = argv[1];
108 outfilename = argv[2];
109
110 /* register all the codecs */
111 avcodec_register_all();
112
113 pkt = av_packet_alloc();
114
115 /* find the MPEG audio decoder */
116 codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
117 if (!codec) {
118 fprintf(stderr, "codec not found\n");
119 exit(1);
120 }
121
122 parser = av_parser_init(codec->id);
123 if (!parser) {
124 fprintf(stderr, "parser not found\n");
125 exit(1);
126 }
127
128 c = avcodec_alloc_context3(codec);
129
130 /* open it */
131 if (avcodec_open2(c, codec, NULL) < 0) {
132 fprintf(stderr, "could not open codec\n");
133 exit(1);
134 }
135
136 f = fopen(filename, "rb");
137 if (!f) {
138 fprintf(stderr, "could not open %s\n", filename);
139 exit(1);
140 }
141 outfile = fopen(outfilename, "wb");
142 if (!outfile) {
143 av_free(c);
144 exit(1);
145 }
146
147 /* decode until eof */
148 data = inbuf;
149 data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
150
151 while (data_size > 0) {
152 if (!decoded_frame) {
153 if (!(decoded_frame = av_frame_alloc())) {
154 fprintf(stderr, "out of memory\n");
155 exit(1);
156 }
157 }
158
159 ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
160 data, data_size,
161 AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
162 if (ret < 0) {
163 fprintf(stderr, "Error while parsing\n");
164 exit(1);
165 }
166 data += ret;
167 data_size -= ret;
168
169 if (pkt->size)
170 decode(c, pkt, decoded_frame, outfile);
171
172 if (data_size < AUDIO_REFILL_THRESH) {
173 memmove(inbuf, data, data_size);
174 data = inbuf;
175 len = fread(data + data_size, 1,
176 AUDIO_INBUF_SIZE - data_size, f);
177 if (len > 0)
178 data_size += len;
179 }
180 }
181
182 fclose(outfile);
183 fclose(f);
184
185 avcodec_free_context(&c);
186 av_parser_close(parser);
187 av_frame_free(&decoded_frame);
188 av_packet_free(&pkt);
189
190 return 0;
191 }