examples/avcodec: split audio decoding into a separate example
[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 "libavcodec/avcodec.h"
33
34 #include "libavutil/frame.h"
35
36 #define AUDIO_INBUF_SIZE 20480
37 #define AUDIO_REFILL_THRESH 4096
38
39 int main(int argc, char **argv)
40 {
41 const char *outfilename, *filename;
42 AVCodec *codec;
43 AVCodecContext *c= NULL;
44 int len;
45 FILE *f, *outfile;
46 uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
47 AVPacket avpkt;
48 AVFrame *decoded_frame = NULL;
49
50 if (argc <= 2) {
51 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
52 exit(0);
53 }
54 filename = argv[1];
55 outfilename = argv[2];
56
57 /* register all the codecs */
58 avcodec_register_all();
59
60 av_init_packet(&avpkt);
61
62 /* find the MPEG audio decoder */
63 codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
64 if (!codec) {
65 fprintf(stderr, "codec not found\n");
66 exit(1);
67 }
68
69 c = avcodec_alloc_context3(codec);
70
71 /* open it */
72 if (avcodec_open2(c, codec, NULL) < 0) {
73 fprintf(stderr, "could not open codec\n");
74 exit(1);
75 }
76
77 f = fopen(filename, "rb");
78 if (!f) {
79 fprintf(stderr, "could not open %s\n", filename);
80 exit(1);
81 }
82 outfile = fopen(outfilename, "wb");
83 if (!outfile) {
84 av_free(c);
85 exit(1);
86 }
87
88 /* decode until eof */
89 avpkt.data = inbuf;
90 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
91
92 while (avpkt.size > 0) {
93 int got_frame = 0;
94
95 if (!decoded_frame) {
96 if (!(decoded_frame = av_frame_alloc())) {
97 fprintf(stderr, "out of memory\n");
98 exit(1);
99 }
100 }
101
102 len = avcodec_decode_audio4(c, decoded_frame, &got_frame, &avpkt);
103 if (len < 0) {
104 fprintf(stderr, "Error while decoding\n");
105 exit(1);
106 }
107 if (got_frame) {
108 /* if a frame has been decoded, output it */
109 int data_size = av_samples_get_buffer_size(NULL, c->channels,
110 decoded_frame->nb_samples,
111 c->sample_fmt, 1);
112 fwrite(decoded_frame->data[0], 1, data_size, outfile);
113 }
114 avpkt.size -= len;
115 avpkt.data += len;
116 if (avpkt.size < AUDIO_REFILL_THRESH) {
117 /* Refill the input buffer, to avoid trying to decode
118 * incomplete frames. Instead of this, one could also use
119 * a parser, or use a proper container format through
120 * libavformat. */
121 memmove(inbuf, avpkt.data, avpkt.size);
122 avpkt.data = inbuf;
123 len = fread(avpkt.data + avpkt.size, 1,
124 AUDIO_INBUF_SIZE - avpkt.size, f);
125 if (len > 0)
126 avpkt.size += len;
127 }
128 }
129
130 fclose(outfile);
131 fclose(f);
132
133 avcodec_free_context(&c);
134 av_frame_free(&decoded_frame);
135
136 return 0;
137 }