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