]> git.sesse.net Git - ffmpeg/blob - doc/examples/decode_audio.c
examples/decode_audio: use the new audio decoding API
[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 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
41                    FILE *outfile)
42 {
43     int ret, data_size;
44
45     /* send the packet with the compressed data to the decoder */
46     ret = avcodec_send_packet(dec_ctx, pkt);
47     if (ret < 0) {
48         fprintf(stderr, "Error submitting the packet to the decoder\n");
49         exit(1);
50     }
51
52     /* read all the output frames (in general there may be any number of them */
53     while (ret >= 0) {
54         ret = avcodec_receive_frame(dec_ctx, frame);
55         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
56             return;
57         else if (ret < 0) {
58             fprintf(stderr, "Error during decoding\n");
59             exit(1);
60         }
61
62         data_size = av_samples_get_buffer_size(NULL, dec_ctx->channels,
63                                                frame->nb_samples,
64                                                dec_ctx->sample_fmt, 1);
65         fwrite(frame->data[0], 1, data_size, outfile);
66     }
67 }
68
69 int main(int argc, char **argv)
70 {
71     const char *outfilename, *filename;
72     const AVCodec *codec;
73     AVCodecContext *c= NULL;
74     AVCodecParserContext *parser = NULL;
75     int len, ret;
76     FILE *f, *outfile;
77     uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
78     uint8_t *data;
79     size_t   data_size;
80     AVPacket avpkt;
81     AVFrame *decoded_frame = NULL;
82
83     if (argc <= 2) {
84         fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
85         exit(0);
86     }
87     filename    = argv[1];
88     outfilename = argv[2];
89
90     /* register all the codecs */
91     avcodec_register_all();
92
93     av_init_packet(&avpkt);
94
95     /* find the MPEG audio decoder */
96     codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
97     if (!codec) {
98         fprintf(stderr, "codec not found\n");
99         exit(1);
100     }
101
102     parser = av_parser_init(codec->id);
103     if (!parser) {
104         fprintf(stderr, "parser not found\n");
105         exit(1);
106     }
107
108     c = avcodec_alloc_context3(codec);
109
110     /* open it */
111     if (avcodec_open2(c, codec, NULL) < 0) {
112         fprintf(stderr, "could not open codec\n");
113         exit(1);
114     }
115
116     f = fopen(filename, "rb");
117     if (!f) {
118         fprintf(stderr, "could not open %s\n", filename);
119         exit(1);
120     }
121     outfile = fopen(outfilename, "wb");
122     if (!outfile) {
123         av_free(c);
124         exit(1);
125     }
126
127     /* decode until eof */
128     data      = inbuf;
129     data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
130
131     while (data_size > 0) {
132         if (!decoded_frame) {
133             if (!(decoded_frame = av_frame_alloc())) {
134                 fprintf(stderr, "out of memory\n");
135                 exit(1);
136             }
137         }
138
139         ret = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size,
140                                data, data_size,
141                                AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
142         if (ret < 0) {
143             fprintf(stderr, "Error while parsing\n");
144             exit(1);
145         }
146         data      += ret;
147         data_size -= ret;
148
149         if (avpkt.size)
150             decode(c, &avpkt, decoded_frame, outfile);
151
152         if (data_size < AUDIO_REFILL_THRESH) {
153             memmove(inbuf, data, data_size);
154             data = inbuf;
155             len = fread(data + data_size, 1,
156                         AUDIO_INBUF_SIZE - data_size, f);
157             if (len > 0)
158                 data_size += len;
159         }
160     }
161
162     fclose(outfile);
163     fclose(f);
164
165     avcodec_free_context(&c);
166     av_parser_close(parser);
167     av_frame_free(&decoded_frame);
168
169     return 0;
170 }