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 libavcodec audio decoding API usage example
25 * @example decode_audio.c
26 *
27 * Decode data from an MP2 input file and generate a raw audio file to
28 * be played with ffplay.
29 */
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
37
39
40 #define AUDIO_INBUF_SIZE 20480
41 #define AUDIO_REFILL_THRESH 4096
42
45 {
47 struct sample_fmt_entry {
49 } sample_fmt_entries[] = {
55 };
57
59 struct sample_fmt_entry *
entry = &sample_fmt_entries[
i];
60 if (sample_fmt ==
entry->sample_fmt) {
62 return 0;
63 }
64 }
65
66 fprintf(stderr,
67 "sample format %s is not supported as output format\n",
69 return -1;
70 }
71
74 {
77
78 /* send the packet with the compressed data to the decoder */
81 fprintf(stderr, "Error submitting the packet to the decoder\n");
82 exit(1);
83 }
84
85 /* read all the output frames (in general there may be any number of them */
89 return;
91 fprintf(stderr, "Error during decoding\n");
92 exit(1);
93 }
95 if (data_size < 0) {
96 /* This should not occur, checking just for paranoia */
97 fprintf(stderr, "Failed to calculate data size\n");
98 exit(1);
99 }
100 for (
i = 0;
i <
frame->nb_samples;
i++)
102 fwrite(
frame->data[ch] + data_size*
i, 1, data_size,
outfile);
103 }
104 }
105
106 int main(
int argc,
char **argv)
107 {
108 const char *outfilename, *filename;
116 size_t data_size;
120 int n_channels = 0;
121 const char *fmt;
122
123 if (argc <= 2) {
124 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
125 exit(0);
126 }
127 filename = argv[1];
128 outfilename = argv[2];
129
132 fprintf(stderr, "Could not allocate AVPacket\n");
133 exit(1); /* or proper cleanup and returning */
134 }
135
136 /* find the MPEG audio decoder */
138 if (!codec) {
139 fprintf(stderr, "Codec not found\n");
140 exit(1);
141 }
142
144 if (!parser) {
145 fprintf(stderr, "Parser not found\n");
146 exit(1);
147 }
148
151 fprintf(stderr, "Could not allocate audio codec context\n");
152 exit(1);
153 }
154
155 /* open it */
157 fprintf(stderr, "Could not open codec\n");
158 exit(1);
159 }
160
161 f = fopen(filename,
"rb");
163 fprintf(stderr, "Could not open %s\n", filename);
164 exit(1);
165 }
166 outfile = fopen(outfilename,
"wb");
168 fprintf(stderr, "Could not open %s\n", outfilename);
169 exit(1);
170 }
171
172 /* decode until eof */
175
176 while (data_size > 0) {
177 if (!decoded_frame) {
179 fprintf(stderr, "Could not allocate audio frame\n");
180 exit(1);
181 }
182 }
183
188 fprintf(stderr, "Error while parsing\n");
189 exit(1);
190 }
193
196
198 memmove(inbuf,
data, data_size);
200 len = fread(
data + data_size, 1,
204 }
205 }
206
207 /* flush the decoder */
211
212 /* print output pcm infomations, because there have no metadata of pcm */
213 sfmt =
c->sample_fmt;
214
217 printf(
"Warning: the sample format the decoder produced is planar "
218 "(%s). This example will output the first channel only.\n",
219 packed ? packed : "?");
221 }
222
223 n_channels =
c->ch_layout.nb_channels;
225 goto end;
226
227 printf(
"Play the output audio file with the command:\n"
228 "ffplay -f %s -ac %d -ar %d %s\n",
229 fmt, n_channels,
c->sample_rate,
230 outfilename);
231 end:
234
239
240 return 0;
241 }