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
36
38
39 #define AUDIO_INBUF_SIZE 20480
40 #define AUDIO_REFILL_THRESH 4096
41
44 {
46 struct sample_fmt_entry {
48 } sample_fmt_entries[] = {
54 };
56
58 struct sample_fmt_entry *entry = &sample_fmt_entries[
i];
59 if (sample_fmt == entry->sample_fmt) {
60 *fmt =
AV_NE(entry->fmt_be, entry->fmt_le);
61 return 0;
62 }
63 }
64
65 fprintf(stderr,
66 "sample format %s is not supported as output format\n",
68 return -1;
69 }
70
73 {
76
77 /* send the packet with the compressed data to the decoder */
80 fprintf(stderr, "Error submitting the packet to the decoder\n");
81 exit(1);
82 }
83
84 /* read all the output frames (in general there may be any number of them */
88 return;
90 fprintf(stderr, "Error during decoding\n");
91 exit(1);
92 }
94 if (data_size < 0) {
95 /* This should not occur, checking just for paranoia */
96 fprintf(stderr, "Failed to calculate data size\n");
97 exit(1);
98 }
99 for (
i = 0;
i <
frame->nb_samples;
i++)
101 fwrite(
frame->data[ch] + data_size*
i, 1, data_size,
outfile);
102 }
103 }
104
105 int main(
int argc,
char **argv)
106 {
107 const char *outfilename, *filename;
115 size_t data_size;
119 int n_channels = 0;
120 const char *fmt;
121
122 if (argc <= 2) {
123 fprintf(stderr, "Usage: %s <input file> <output file>\n", argv[0]);
124 exit(0);
125 }
126 filename = argv[1];
127 outfilename = argv[2];
128
130
131 /* find the MPEG audio decoder */
133 if (!codec) {
134 fprintf(stderr, "Codec not found\n");
135 exit(1);
136 }
137
139 if (!parser) {
140 fprintf(stderr, "Parser not found\n");
141 exit(1);
142 }
143
146 fprintf(stderr, "Could not allocate audio codec context\n");
147 exit(1);
148 }
149
150 /* open it */
152 fprintf(stderr, "Could not open codec\n");
153 exit(1);
154 }
155
156 f = fopen(filename,
"rb");
158 fprintf(stderr, "Could not open %s\n", filename);
159 exit(1);
160 }
161 outfile = fopen(outfilename,
"wb");
164 exit(1);
165 }
166
167 /* decode until eof */
170
171 while (data_size > 0) {
172 if (!decoded_frame) {
174 fprintf(stderr, "Could not allocate audio frame\n");
175 exit(1);
176 }
177 }
178
183 fprintf(stderr, "Error while parsing\n");
184 exit(1);
185 }
188
191
193 memmove(inbuf,
data, data_size);
195 len = fread(
data + data_size, 1,
199 }
200 }
201
202 /* flush the decoder */
206
207 /* print output pcm infomations, because there have no metadata of pcm */
208 sfmt =
c->sample_fmt;
209
212 printf(
"Warning: the sample format the decoder produced is planar "
213 "(%s). This example will output the first channel only.\n",
214 packed ? packed : "?");
216 }
217
218 n_channels =
c->channels;
220 goto end;
221
222 printf(
"Play the output audio file with the command:\n"
223 "ffplay -f %s -ac %d -ar %d %s\n",
224 fmt, n_channels,
c->sample_rate,
225 outfilename);
226 end:
229
234
235 return 0;
236 }