1 /*
2 * Copyright (c) 2012 Stefano Sabatini
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 * libavformat demuxing API use example.
26 *
27 * Show how to use the libavformat and libavcodec API to demux and
28 * decode audio and video data.
29 * @example doc/examples/demuxing.c
30 */
31
36
45
49
53
59
61 {
63
65 /* decode video frame */
67 if (ret < 0) {
68 fprintf(stderr, "Error decoding video frame\n");
70 }
71
72 if (*got_frame) {
73 printf("video_frame%s n:%d coded_n:%d pts:%s\n",
74 cached ? "(cached)" : "",
77
78 /* copy decoded frame to destination buffer:
79 * this is required since rawvideo expects non aligned data */
83
84 /* write to rawvideo file */
86 }
88 /* decode audio frame */
90 if (ret < 0) {
91 fprintf(stderr, "Error decoding audio frame\n");
93 }
94
95 if (*got_frame) {
96 printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
97 cached ? "(cached)" : "",
100
103 if (ret < 0) {
104 fprintf(stderr, "Could not allocate audio buffer\n");
106 }
107
108 /* TODO: extend return code of the av_samples_* functions so that this call is not needed */
112
113 /* copy audio data to destination buffer:
114 * this is required since rawaudio expects non aligned data */
117
118 /* write to rawaudio file */
121 }
122 }
123
125 }
126
129 {
134
136 if (ret < 0) {
137 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
140 } else {
142 st = fmt_ctx->
streams[*stream_idx];
143
144 /* find decoder for the stream */
147 if (!dec) {
148 fprintf(stderr, "Failed to find %s codec\n",
151 }
152
154 fprintf(stderr, "Failed to open %s codec\n",
157 }
158 }
159
160 return 0;
161 }
162
165 {
166 int i;
167 struct sample_fmt_entry {
169 } sample_fmt_entries[] = {
175 };
176 *fmt = NULL;
177
179 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
180 if (sample_fmt == entry->sample_fmt) {
181 *fmt =
AV_NE(entry->fmt_be, entry->fmt_le);
182 return 0;
183 }
184 }
185
186 fprintf(stderr,
187 "sample format %s is not supported as output format\n",
189 return -1;
190 }
191
192 int main (
int argc,
char **argv)
193 {
194 int ret = 0, got_frame;
195
196 if (argc != 4) {
197 fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
198 "API example program to show how to read frames from an input file.\n"
199 "This program reads frames from a file, decodes them, and writes decoded\n"
200 "video frames to a rawvideo file named video_output_file, and decoded\n"
201 "audio frames to a rawaudio file named audio_output_file.\n"
202 "\n", argv[0]);
203 exit(1);
204 }
208
209 /* register all formats and codecs */
211
212 /* open input file, and allocate format context */
214 fprintf(stderr,
"Could not open source file %s\n",
src_filename);
215 exit(1);
216 }
217
218 /* retrieve stream information */
220 fprintf(stderr, "Could not find stream information\n");
221 exit(1);
222 }
223
226 video_dec_ctx = video_stream->
codec;
227
231 ret = 1;
233 }
234
235 /* allocate image where the decoded image will be put */
239 if (ret < 0) {
240 fprintf(stderr, "Could not allocate raw video buffer\n");
242 }
244 }
245
247 int nb_planes;
248
254 ret = 1;
256 }
257
262 fprintf(stderr, "Could not allocate audio data buffers\n");
265 }
266 }
267
268 /* dump input information to stderr */
270
272 fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
273 ret = 1;
275 }
276
278 if (!frame) {
279 fprintf(stderr, "Could not allocate frame\n");
282 }
283
284 /* initialize packet, set data to NULL, let the demuxer fill it */
288
289 if (video_stream)
293
294 /* read frames from the file */
298 }
299
300 /* flush cached frames */
303 do {
305 } while (got_frame);
306
307 printf("Demuxing succeeded.\n");
308
309 if (video_stream) {
310 printf("Play the output video file with the command:\n"
311 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
314 }
315
318
321 printf("Play the output audio file with the command:\n"
322 "ffplay -f %s -ac %d -ar %d %s\n",
325 }
326
328 if (video_dec_ctx)
340
341 return ret < 0;
342 }