MCPcopy Create free account
hub / github.com/FFmpeg/FFmpeg / audio_decode_example

Function audio_decode_example

libavcodec/api-example.c:116–195  ·  view source on GitHub ↗

* Audio decoding. */

Source from the content-addressed store, hash-verified

114 * Audio decoding.
115 */
116static void audio_decode_example(const char *outfilename, const char *filename)
117{
118 AVCodec *codec;
119 AVCodecContext *c= NULL;
120 int out_size, len;
121 FILE *f, *outfile;
122 uint8_t *outbuf;
123 uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
124 AVPacket avpkt;
125
126 av_init_packet(&avpkt);
127
128 printf("Audio decoding\n");
129
130 /* find the mpeg audio decoder */
131 codec = avcodec_find_decoder(CODEC_ID_MP2);
132 if (!codec) {
133 fprintf(stderr, "codec not found\n");
134 exit(1);
135 }
136
137 c= avcodec_alloc_context();
138
139 /* open it */
140 if (avcodec_open(c, codec) < 0) {
141 fprintf(stderr, "could not open codec\n");
142 exit(1);
143 }
144
145 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
146
147 f = fopen(filename, "rb");
148 if (!f) {
149 fprintf(stderr, "could not open %s\n", filename);
150 exit(1);
151 }
152 outfile = fopen(outfilename, "wb");
153 if (!outfile) {
154 av_free(c);
155 exit(1);
156 }
157
158 /* decode until eof */
159 avpkt.data = inbuf;
160 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
161
162 while (avpkt.size > 0) {
163 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
164 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
165 if (len < 0) {
166 fprintf(stderr, "Error while decoding\n");
167 exit(1);
168 }
169 if (out_size > 0) {
170 /* if a frame has been decoded, output it */
171 fwrite(outbuf, 1, out_size, outfile);
172 }
173 avpkt.size -= len;

Callers 1

mainFunction · 0.85

Calls 7

av_init_packetFunction · 0.85
avcodec_find_decoderFunction · 0.85
avcodec_alloc_contextFunction · 0.85
avcodec_openFunction · 0.85
av_freeFunction · 0.85
avcodec_decode_audio3Function · 0.85
avcodec_closeFunction · 0.85

Tested by

no test coverage detected