]> git.sesse.net Git - ffmpeg/blob - doc/examples/decode_audio.c
Merge commit '3d66717f7cb5555257244be8f5bce172ed3af7ac'
[ffmpeg] / doc / examples / decode_audio.c
1 /*
2  * Copyright (c) 2001 Fabrice Bellard
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * THE SOFTWARE.
21  */
22
23 /**
24  * @file
25  * audio decoding with libavcodec API example
26  *
27  * @example decode_audio.c
28  */
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33
34 #include <libavutil/frame.h>
35 #include <libavutil/mem.h>
36
37 #include <libavcodec/avcodec.h>
38
39 #define AUDIO_INBUF_SIZE 20480
40 #define AUDIO_REFILL_THRESH 4096
41
42 static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame,
43                    FILE *outfile)
44 {
45     int i, ch;
46     int ret, data_size;
47
48     /* send the packet with the compressed data to the decoder */
49     ret = avcodec_send_packet(dec_ctx, pkt);
50     if (ret < 0) {
51         fprintf(stderr, "Error submitting the packet to the decoder\n");
52         exit(1);
53     }
54
55     /* read all the output frames (in general there may be any number of them */
56     while (ret >= 0) {
57         ret = avcodec_receive_frame(dec_ctx, frame);
58         if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF)
59             return;
60         else if (ret < 0) {
61             fprintf(stderr, "Error during decoding\n");
62             exit(1);
63         }
64         data_size = av_get_bytes_per_sample(dec_ctx->sample_fmt);
65         if (data_size < 0) {
66             /* This should not occur, checking just for paranoia */
67             fprintf(stderr, "Failed to calculate data size\n");
68             exit(1);
69         }
70         for (i = 0; i < frame->nb_samples; i++)
71             for (ch = 0; ch < dec_ctx->channels; ch++)
72                 fwrite(frame->data[ch] + data_size*i, 1, data_size, outfile);
73     }
74 }
75
76 int main(int argc, char **argv)
77 {
78     const char *outfilename, *filename;
79     const AVCodec *codec;
80     AVCodecContext *c= NULL;
81     AVCodecParserContext *parser = NULL;
82     int len, ret;
83     FILE *f, *outfile;
84     uint8_t inbuf[AUDIO_INBUF_SIZE + AV_INPUT_BUFFER_PADDING_SIZE];
85     uint8_t *data;
86     size_t   data_size;
87     AVPacket avpkt;
88     AVFrame *decoded_frame = NULL;
89
90     if (argc <= 2) {
91         fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
92         exit(0);
93     }
94     filename    = argv[1];
95     outfilename = argv[2];
96
97     /* register all the codecs */
98     avcodec_register_all();
99
100     av_init_packet(&avpkt);
101
102     /* find the MPEG audio decoder */
103     codec = avcodec_find_decoder(AV_CODEC_ID_MP2);
104     if (!codec) {
105         fprintf(stderr, "Codec not found\n");
106         exit(1);
107     }
108
109     parser = av_parser_init(codec->id);
110     if (!parser) {
111         fprintf(stderr, "Parser not found\n");
112         exit(1);
113     }
114
115     c = avcodec_alloc_context3(codec);
116     if (!c) {
117         fprintf(stderr, "Could not allocate audio codec context\n");
118         exit(1);
119     }
120
121     /* open it */
122     if (avcodec_open2(c, codec, NULL) < 0) {
123         fprintf(stderr, "Could not open codec\n");
124         exit(1);
125     }
126
127     f = fopen(filename, "rb");
128     if (!f) {
129         fprintf(stderr, "Could not open %s\n", filename);
130         exit(1);
131     }
132     outfile = fopen(outfilename, "wb");
133     if (!outfile) {
134         av_free(c);
135         exit(1);
136     }
137
138     /* decode until eof */
139     data      = inbuf;
140     data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
141
142     while (data_size > 0) {
143         if (!decoded_frame) {
144             if (!(decoded_frame = av_frame_alloc())) {
145                 fprintf(stderr, "Could not allocate audio frame\n");
146                 exit(1);
147             }
148         }
149
150         ret = av_parser_parse2(parser, c, &avpkt.data, &avpkt.size,
151                                data, data_size,
152                                AV_NOPTS_VALUE, AV_NOPTS_VALUE, 0);
153         if (ret < 0) {
154             fprintf(stderr, "Error while parsing\n");
155             exit(1);
156         }
157         data      += ret;
158         data_size -= ret;
159
160         if (avpkt.size)
161             decode(c, &avpkt, decoded_frame, outfile);
162
163         if (data_size < AUDIO_REFILL_THRESH) {
164             memmove(inbuf, data, data_size);
165             data = inbuf;
166             len = fread(data + data_size, 1,
167                         AUDIO_INBUF_SIZE - data_size, f);
168             if (len > 0)
169                 data_size += len;
170         }
171     }
172
173     fclose(outfile);
174     fclose(f);
175
176     avcodec_free_context(&c);
177     av_parser_close(parser);
178     av_frame_free(&decoded_frame);
179
180     return 0;
181 }