FFmpeg 4.2.2
decode_audio.c
Go to the documentation of this file.
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
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 */
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) {
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 }
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
76int 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;
85 uint8_t *data;
86 size_t data_size;
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
98
99 /* find the MPEG audio decoder */
101 if (!codec) {
102 fprintf(stderr, "Codec not found\n");
103 exit(1);
104 }
105
106 parser = av_parser_init(codec->id);
107 if (!parser) {
108 fprintf(stderr, "Parser not found\n");
109 exit(1);
110 }
111
112 c = avcodec_alloc_context3(codec);
113 if (!c) {
114 fprintf(stderr, "Could not allocate audio codec context\n");
115 exit(1);
116 }
117
118 /* open it */
119 if (avcodec_open2(c, codec, NULL) < 0) {
120 fprintf(stderr, "Could not open codec\n");
121 exit(1);
122 }
123
124 f = fopen(filename, "rb");
125 if (!f) {
126 fprintf(stderr, "Could not open %s\n", filename);
127 exit(1);
128 }
129 outfile = fopen(outfilename, "wb");
130 if (!outfile) {
131 av_free(c);
132 exit(1);
133 }
134
135 /* decode until eof */
136 data = inbuf;
137 data_size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
138
139 while (data_size > 0) {
140 if (!decoded_frame) {
141 if (!(decoded_frame = av_frame_alloc())) {
142 fprintf(stderr, "Could not allocate audio frame\n");
143 exit(1);
144 }
145 }
146
147 ret = av_parser_parse2(parser, c, &pkt->data, &pkt->size,
148 data, data_size,
150 if (ret < 0) {
151 fprintf(stderr, "Error while parsing\n");
152 exit(1);
153 }
154 data += ret;
155 data_size -= ret;
156
157 if (pkt->size)
158 decode(c, pkt, decoded_frame, outfile);
159
160 if (data_size < AUDIO_REFILL_THRESH) {
161 memmove(inbuf, data, data_size);
162 data = inbuf;
163 len = fread(data + data_size, 1,
164 AUDIO_INBUF_SIZE - data_size, f);
165 if (len > 0)
166 data_size += len;
167 }
168 }
169
170 /* flush the decoder */
171 pkt->data = NULL;
172 pkt->size = 0;
173 decode(c, pkt, decoded_frame, outfile);
174
175 fclose(outfile);
176 fclose(f);
177
179 av_parser_close(parser);
180 av_frame_free(&decoded_frame);
182
183 return 0;
184}
Libavcodec external API header.
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
#define AUDIO_INBUF_SIZE
Definition: decode_audio.c:39
int main(int argc, char **argv)
Definition: decode_audio.c:76
#define AUDIO_REFILL_THRESH
Definition: decode_audio.c:40
static AVPacket pkt
static AVFrame * frame
static AVCodecContext * dec_ctx
reference-counted frame API
int avcodec_open2(AVCodecContext *avctx, const AVCodec *codec, AVDictionary **options)
Initialize the AVCodecContext to use the given AVCodec.
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
@ AV_CODEC_ID_MP2
Definition: avcodec.h:564
int avcodec_receive_frame(AVCodecContext *avctx, AVFrame *frame)
Return decoded output data from a decoder.
AVCodec * avcodec_find_decoder(enum AVCodecID id)
Find a registered decoder with a matching codec ID.
int avcodec_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
Supply raw packet data as input to a decoder.
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding.
Definition: avcodec.h:790
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
void av_parser_close(AVCodecParserContext *s)
int av_parser_parse2(AVCodecParserContext *s, AVCodecContext *avctx, uint8_t **poutbuf, int *poutbuf_size, const uint8_t *buf, int buf_size, int64_t pts, int64_t dts, int64_t pos)
Parse a packet.
AVCodecParserContext * av_parser_init(int codec_id)
#define AVERROR_EOF
End of file.
Definition: error.h:55
#define AVERROR(e)
Definition: error.h:43
void av_frame_free(AVFrame **frame)
Free the frame and any dynamically allocated objects in it, e.g.
AVFrame * av_frame_alloc(void)
Allocate an AVFrame and set its fields to default values.
void av_free(void *ptr)
Free a memory block which has been allocated with a function of av_malloc() or av_realloc() family.
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
Memory handling functions.
main external API structure.
Definition: avcodec.h:1565
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:2233
int channels
number of audio channels
Definition: avcodec.h:2226
AVCodec.
Definition: avcodec.h:3481
enum AVCodecID id
Definition: avcodec.h:3495
This structure describes decoded (raw) audio or video data.
Definition: frame.h:295
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:361
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:309
This structure stores compressed data.
Definition: avcodec.h:1454
int size
Definition: avcodec.h:1478
uint8_t * data
Definition: avcodec.h:1477