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
55
57 {
59 int decoded = pkt.
size;
60
62 /* decode video frame */
64 if (ret < 0) {
65 fprintf(stderr, "Error decoding video frame\n");
67 }
68
69 if (*got_frame) {
70 printf("video_frame%s n:%d coded_n:%d pts:%s\n",
71 cached ? "(cached)" : "",
74
75 /* copy decoded frame to destination buffer:
76 * this is required since rawvideo expects non aligned data */
80
81 /* write to rawvideo file */
83 }
85 /* decode audio frame */
87 if (ret < 0) {
88 fprintf(stderr, "Error decoding audio frame\n");
90 }
91 /* Some audio decoders decode only part of the packet, and have to be
92 * called again with the remainder of the packet data.
93 * Sample: fate-suite/lossless-audio/luckynight-partial.shn
94 * Also, some decoders might over-read the packet. */
96
97 if (*got_frame) {
99 printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
100 cached ? "(cached)" : "",
103
104 /* Write the raw audio data samples of the first plane. This works
105 * fine for packed formats (e.g. AV_SAMPLE_FMT_S16). However,
106 * most audio decoders output planar audio, which uses a separate
107 * plane of audio samples for each channel (e.g. AV_SAMPLE_FMT_S16P).
108 * In other words, this code will write only the first audio channel
109 * in these cases.
110 * You should use libswresample or libavfilter to convert the frame
111 * to packed data. */
113 }
114 }
115
116 return decoded;
117 }
118
121 {
126
128 if (ret < 0) {
129 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
132 } else {
134 st = fmt_ctx->
streams[*stream_idx];
135
136 /* find decoder for the stream */
139 if (!dec) {
140 fprintf(stderr, "Failed to find %s codec\n",
143 }
144
146 fprintf(stderr, "Failed to open %s codec\n",
149 }
150 }
151
152 return 0;
153 }
154
157 {
158 int i;
159 struct sample_fmt_entry {
161 } sample_fmt_entries[] = {
167 };
168 *fmt = NULL;
169
171 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
172 if (sample_fmt == entry->sample_fmt) {
173 *fmt =
AV_NE(entry->fmt_be, entry->fmt_le);
174 return 0;
175 }
176 }
177
178 fprintf(stderr,
179 "sample format %s is not supported as output format\n",
181 return -1;
182 }
183
184 int main (
int argc,
char **argv)
185 {
186 int ret = 0, got_frame;
187
188 if (argc != 4) {
189 fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
190 "API example program to show how to read frames from an input file.\n"
191 "This program reads frames from a file, decodes them, and writes decoded\n"
192 "video frames to a rawvideo file named video_output_file, and decoded\n"
193 "audio frames to a rawaudio file named audio_output_file.\n"
194 "\n", argv[0]);
195 exit(1);
196 }
200
201 /* register all formats and codecs */
203
204 /* open input file, and allocate format context */
206 fprintf(stderr,
"Could not open source file %s\n",
src_filename);
207 exit(1);
208 }
209
210 /* retrieve stream information */
212 fprintf(stderr, "Could not find stream information\n");
213 exit(1);
214 }
215
218 video_dec_ctx = video_stream->
codec;
219
223 ret = 1;
225 }
226
227 /* allocate image where the decoded image will be put */
231 if (ret < 0) {
232 fprintf(stderr, "Could not allocate raw video buffer\n");
234 }
236 }
237
244 ret = 1;
246 }
247 }
248
249 /* dump input information to stderr */
251
253 fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
254 ret = 1;
256 }
257
259 if (!frame) {
260 fprintf(stderr, "Could not allocate frame\n");
263 }
264
265 /* initialize packet, set data to NULL, let the demuxer fill it */
269
270 if (video_stream)
274
275 /* read frames from the file */
278 do {
280 if (ret < 0)
281 break;
284 }
while (pkt.
size > 0);
286 }
287
288 /* flush cached frames */
291 do {
293 } while (got_frame);
294
295 printf("Demuxing succeeded.\n");
296
297 if (video_stream) {
298 printf("Play the output video file with the command:\n"
299 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
302 }
303
308
311 printf("Warning: the sample format the decoder produced is planar "
312 "(%s). This example will output the first channel only.\n",
313 packed ? packed : "?");
315 n_channels = 1;
316 }
317
320
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)
339
340 return ret < 0;
341 }