yading@10
|
1 /*
|
yading@10
|
2 * Copyright (c) 2012 Stefano Sabatini
|
yading@10
|
3 *
|
yading@10
|
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
|
yading@10
|
5 * of this software and associated documentation files (the "Software"), to deal
|
yading@10
|
6 * in the Software without restriction, including without limitation the rights
|
yading@10
|
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
yading@10
|
8 * copies of the Software, and to permit persons to whom the Software is
|
yading@10
|
9 * furnished to do so, subject to the following conditions:
|
yading@10
|
10 *
|
yading@10
|
11 * The above copyright notice and this permission notice shall be included in
|
yading@10
|
12 * all copies or substantial portions of the Software.
|
yading@10
|
13 *
|
yading@10
|
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
yading@10
|
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
yading@10
|
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
yading@10
|
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
yading@10
|
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
yading@10
|
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
yading@10
|
20 * THE SOFTWARE.
|
yading@10
|
21 */
|
yading@10
|
22
|
yading@10
|
23 /**
|
yading@10
|
24 * @file
|
yading@10
|
25 * libavformat demuxing API use example.
|
yading@10
|
26 *
|
yading@10
|
27 * Show how to use the libavformat and libavcodec API to demux and
|
yading@10
|
28 * decode audio and video data.
|
yading@10
|
29 * @example doc/examples/demuxing.c
|
yading@10
|
30 */
|
yading@10
|
31
|
yading@10
|
32 #include <libavutil/imgutils.h>
|
yading@10
|
33 #include <libavutil/samplefmt.h>
|
yading@10
|
34 #include <libavutil/timestamp.h>
|
yading@10
|
35 #include <libavformat/avformat.h>
|
yading@10
|
36
|
yading@10
|
37 static AVFormatContext *fmt_ctx = NULL;
|
yading@10
|
38 static AVCodecContext *video_dec_ctx = NULL, *audio_dec_ctx;
|
yading@10
|
39 static AVStream *video_stream = NULL, *audio_stream = NULL;
|
yading@10
|
40 static const char *src_filename = NULL;
|
yading@10
|
41 static const char *video_dst_filename = NULL;
|
yading@10
|
42 static const char *audio_dst_filename = NULL;
|
yading@10
|
43 static FILE *video_dst_file = NULL;
|
yading@10
|
44 static FILE *audio_dst_file = NULL;
|
yading@10
|
45
|
yading@10
|
46 static uint8_t *video_dst_data[4] = {NULL};
|
yading@10
|
47 static int video_dst_linesize[4];
|
yading@10
|
48 static int video_dst_bufsize;
|
yading@10
|
49
|
yading@10
|
50 static uint8_t **audio_dst_data = NULL;
|
yading@10
|
51 static int audio_dst_linesize;
|
yading@10
|
52 static int audio_dst_bufsize;
|
yading@10
|
53
|
yading@10
|
54 static int video_stream_idx = -1, audio_stream_idx = -1;
|
yading@10
|
55 static AVFrame *frame = NULL;
|
yading@10
|
56 static AVPacket pkt;
|
yading@10
|
57 static int video_frame_count = 0;
|
yading@10
|
58 static int audio_frame_count = 0;
|
yading@10
|
59
|
yading@10
|
60 static int decode_packet(int *got_frame, int cached)
|
yading@10
|
61 {
|
yading@10
|
62 int ret = 0;
|
yading@10
|
63
|
yading@10
|
64 if (pkt.stream_index == video_stream_idx) {
|
yading@10
|
65 /* decode video frame */
|
yading@10
|
66 ret = avcodec_decode_video2(video_dec_ctx, frame, got_frame, &pkt);
|
yading@10
|
67 if (ret < 0) {
|
yading@10
|
68 fprintf(stderr, "Error decoding video frame\n");
|
yading@10
|
69 return ret;
|
yading@10
|
70 }
|
yading@10
|
71
|
yading@10
|
72 if (*got_frame) {
|
yading@10
|
73 printf("video_frame%s n:%d coded_n:%d pts:%s\n",
|
yading@10
|
74 cached ? "(cached)" : "",
|
yading@10
|
75 video_frame_count++, frame->coded_picture_number,
|
yading@10
|
76 av_ts2timestr(frame->pts, &video_dec_ctx->time_base));
|
yading@10
|
77
|
yading@10
|
78 /* copy decoded frame to destination buffer:
|
yading@10
|
79 * this is required since rawvideo expects non aligned data */
|
yading@10
|
80 av_image_copy(video_dst_data, video_dst_linesize,
|
yading@10
|
81 (const uint8_t **)(frame->data), frame->linesize,
|
yading@10
|
82 video_dec_ctx->pix_fmt, video_dec_ctx->width, video_dec_ctx->height);
|
yading@10
|
83
|
yading@10
|
84 /* write to rawvideo file */
|
yading@10
|
85 fwrite(video_dst_data[0], 1, video_dst_bufsize, video_dst_file);
|
yading@10
|
86 }
|
yading@10
|
87 } else if (pkt.stream_index == audio_stream_idx) {
|
yading@10
|
88 /* decode audio frame */
|
yading@10
|
89 ret = avcodec_decode_audio4(audio_dec_ctx, frame, got_frame, &pkt);
|
yading@10
|
90 if (ret < 0) {
|
yading@10
|
91 fprintf(stderr, "Error decoding audio frame\n");
|
yading@10
|
92 return ret;
|
yading@10
|
93 }
|
yading@10
|
94
|
yading@10
|
95 if (*got_frame) {
|
yading@10
|
96 printf("audio_frame%s n:%d nb_samples:%d pts:%s\n",
|
yading@10
|
97 cached ? "(cached)" : "",
|
yading@10
|
98 audio_frame_count++, frame->nb_samples,
|
yading@10
|
99 av_ts2timestr(frame->pts, &audio_dec_ctx->time_base));
|
yading@10
|
100
|
yading@10
|
101 ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, av_frame_get_channels(frame),
|
yading@10
|
102 frame->nb_samples, frame->format, 1);
|
yading@10
|
103 if (ret < 0) {
|
yading@10
|
104 fprintf(stderr, "Could not allocate audio buffer\n");
|
yading@10
|
105 return AVERROR(ENOMEM);
|
yading@10
|
106 }
|
yading@10
|
107
|
yading@10
|
108 /* TODO: extend return code of the av_samples_* functions so that this call is not needed */
|
yading@10
|
109 audio_dst_bufsize =
|
yading@10
|
110 av_samples_get_buffer_size(NULL, av_frame_get_channels(frame),
|
yading@10
|
111 frame->nb_samples, frame->format, 1);
|
yading@10
|
112
|
yading@10
|
113 /* copy audio data to destination buffer:
|
yading@10
|
114 * this is required since rawaudio expects non aligned data */
|
yading@10
|
115 av_samples_copy(audio_dst_data, frame->data, 0, 0,
|
yading@10
|
116 frame->nb_samples, av_frame_get_channels(frame), frame->format);
|
yading@10
|
117
|
yading@10
|
118 /* write to rawaudio file */
|
yading@10
|
119 fwrite(audio_dst_data[0], 1, audio_dst_bufsize, audio_dst_file);
|
yading@10
|
120 av_freep(&audio_dst_data[0]);
|
yading@10
|
121 }
|
yading@10
|
122 }
|
yading@10
|
123
|
yading@10
|
124 return ret;
|
yading@10
|
125 }
|
yading@10
|
126
|
yading@10
|
127 static int open_codec_context(int *stream_idx,
|
yading@10
|
128 AVFormatContext *fmt_ctx, enum AVMediaType type)
|
yading@10
|
129 {
|
yading@10
|
130 int ret;
|
yading@10
|
131 AVStream *st;
|
yading@10
|
132 AVCodecContext *dec_ctx = NULL;
|
yading@10
|
133 AVCodec *dec = NULL;
|
yading@10
|
134
|
yading@10
|
135 ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
|
yading@10
|
136 if (ret < 0) {
|
yading@10
|
137 fprintf(stderr, "Could not find %s stream in input file '%s'\n",
|
yading@10
|
138 av_get_media_type_string(type), src_filename);
|
yading@10
|
139 return ret;
|
yading@10
|
140 } else {
|
yading@10
|
141 *stream_idx = ret;
|
yading@10
|
142 st = fmt_ctx->streams[*stream_idx];
|
yading@10
|
143
|
yading@10
|
144 /* find decoder for the stream */
|
yading@10
|
145 dec_ctx = st->codec;
|
yading@10
|
146 dec = avcodec_find_decoder(dec_ctx->codec_id);
|
yading@10
|
147 if (!dec) {
|
yading@10
|
148 fprintf(stderr, "Failed to find %s codec\n",
|
yading@10
|
149 av_get_media_type_string(type));
|
yading@10
|
150 return ret;
|
yading@10
|
151 }
|
yading@10
|
152
|
yading@10
|
153 if ((ret = avcodec_open2(dec_ctx, dec, NULL)) < 0) {
|
yading@10
|
154 fprintf(stderr, "Failed to open %s codec\n",
|
yading@10
|
155 av_get_media_type_string(type));
|
yading@10
|
156 return ret;
|
yading@10
|
157 }
|
yading@10
|
158 }
|
yading@10
|
159
|
yading@10
|
160 return 0;
|
yading@10
|
161 }
|
yading@10
|
162
|
yading@10
|
163 static int get_format_from_sample_fmt(const char **fmt,
|
yading@10
|
164 enum AVSampleFormat sample_fmt)
|
yading@10
|
165 {
|
yading@10
|
166 int i;
|
yading@10
|
167 struct sample_fmt_entry {
|
yading@10
|
168 enum AVSampleFormat sample_fmt; const char *fmt_be, *fmt_le;
|
yading@10
|
169 } sample_fmt_entries[] = {
|
yading@10
|
170 { AV_SAMPLE_FMT_U8, "u8", "u8" },
|
yading@10
|
171 { AV_SAMPLE_FMT_S16, "s16be", "s16le" },
|
yading@10
|
172 { AV_SAMPLE_FMT_S32, "s32be", "s32le" },
|
yading@10
|
173 { AV_SAMPLE_FMT_FLT, "f32be", "f32le" },
|
yading@10
|
174 { AV_SAMPLE_FMT_DBL, "f64be", "f64le" },
|
yading@10
|
175 };
|
yading@10
|
176 *fmt = NULL;
|
yading@10
|
177
|
yading@10
|
178 for (i = 0; i < FF_ARRAY_ELEMS(sample_fmt_entries); i++) {
|
yading@10
|
179 struct sample_fmt_entry *entry = &sample_fmt_entries[i];
|
yading@10
|
180 if (sample_fmt == entry->sample_fmt) {
|
yading@10
|
181 *fmt = AV_NE(entry->fmt_be, entry->fmt_le);
|
yading@10
|
182 return 0;
|
yading@10
|
183 }
|
yading@10
|
184 }
|
yading@10
|
185
|
yading@10
|
186 fprintf(stderr,
|
yading@10
|
187 "sample format %s is not supported as output format\n",
|
yading@10
|
188 av_get_sample_fmt_name(sample_fmt));
|
yading@10
|
189 return -1;
|
yading@10
|
190 }
|
yading@10
|
191
|
yading@10
|
192 int main (int argc, char **argv)
|
yading@10
|
193 {
|
yading@10
|
194 int ret = 0, got_frame;
|
yading@10
|
195
|
yading@10
|
196 if (argc != 4) {
|
yading@10
|
197 fprintf(stderr, "usage: %s input_file video_output_file audio_output_file\n"
|
yading@10
|
198 "API example program to show how to read frames from an input file.\n"
|
yading@10
|
199 "This program reads frames from a file, decodes them, and writes decoded\n"
|
yading@10
|
200 "video frames to a rawvideo file named video_output_file, and decoded\n"
|
yading@10
|
201 "audio frames to a rawaudio file named audio_output_file.\n"
|
yading@10
|
202 "\n", argv[0]);
|
yading@10
|
203 exit(1);
|
yading@10
|
204 }
|
yading@10
|
205 src_filename = argv[1];
|
yading@10
|
206 video_dst_filename = argv[2];
|
yading@10
|
207 audio_dst_filename = argv[3];
|
yading@10
|
208
|
yading@10
|
209 /* register all formats and codecs */
|
yading@10
|
210 av_register_all();
|
yading@10
|
211
|
yading@10
|
212 /* open input file, and allocate format context */
|
yading@10
|
213 if (avformat_open_input(&fmt_ctx, src_filename, NULL, NULL) < 0) {
|
yading@10
|
214 fprintf(stderr, "Could not open source file %s\n", src_filename);
|
yading@10
|
215 exit(1);
|
yading@10
|
216 }
|
yading@10
|
217
|
yading@10
|
218 /* retrieve stream information */
|
yading@10
|
219 if (avformat_find_stream_info(fmt_ctx, NULL) < 0) {
|
yading@10
|
220 fprintf(stderr, "Could not find stream information\n");
|
yading@10
|
221 exit(1);
|
yading@10
|
222 }
|
yading@10
|
223
|
yading@10
|
224 if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) {
|
yading@10
|
225 video_stream = fmt_ctx->streams[video_stream_idx];
|
yading@10
|
226 video_dec_ctx = video_stream->codec;
|
yading@10
|
227
|
yading@10
|
228 video_dst_file = fopen(video_dst_filename, "wb");
|
yading@10
|
229 if (!video_dst_file) {
|
yading@10
|
230 fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
|
yading@10
|
231 ret = 1;
|
yading@10
|
232 goto end;
|
yading@10
|
233 }
|
yading@10
|
234
|
yading@10
|
235 /* allocate image where the decoded image will be put */
|
yading@10
|
236 ret = av_image_alloc(video_dst_data, video_dst_linesize,
|
yading@10
|
237 video_dec_ctx->width, video_dec_ctx->height,
|
yading@10
|
238 video_dec_ctx->pix_fmt, 1);
|
yading@10
|
239 if (ret < 0) {
|
yading@10
|
240 fprintf(stderr, "Could not allocate raw video buffer\n");
|
yading@10
|
241 goto end;
|
yading@10
|
242 }
|
yading@10
|
243 video_dst_bufsize = ret;
|
yading@10
|
244 }
|
yading@10
|
245
|
yading@10
|
246 if (open_codec_context(&audio_stream_idx, fmt_ctx, AVMEDIA_TYPE_AUDIO) >= 0) {
|
yading@10
|
247 int nb_planes;
|
yading@10
|
248
|
yading@10
|
249 audio_stream = fmt_ctx->streams[audio_stream_idx];
|
yading@10
|
250 audio_dec_ctx = audio_stream->codec;
|
yading@10
|
251 audio_dst_file = fopen(audio_dst_filename, "wb");
|
yading@10
|
252 if (!audio_dst_file) {
|
yading@10
|
253 fprintf(stderr, "Could not open destination file %s\n", video_dst_filename);
|
yading@10
|
254 ret = 1;
|
yading@10
|
255 goto end;
|
yading@10
|
256 }
|
yading@10
|
257
|
yading@10
|
258 nb_planes = av_sample_fmt_is_planar(audio_dec_ctx->sample_fmt) ?
|
yading@10
|
259 audio_dec_ctx->channels : 1;
|
yading@10
|
260 audio_dst_data = av_mallocz(sizeof(uint8_t *) * nb_planes);
|
yading@10
|
261 if (!audio_dst_data) {
|
yading@10
|
262 fprintf(stderr, "Could not allocate audio data buffers\n");
|
yading@10
|
263 ret = AVERROR(ENOMEM);
|
yading@10
|
264 goto end;
|
yading@10
|
265 }
|
yading@10
|
266 }
|
yading@10
|
267
|
yading@10
|
268 /* dump input information to stderr */
|
yading@10
|
269 av_dump_format(fmt_ctx, 0, src_filename, 0);
|
yading@10
|
270
|
yading@10
|
271 if (!audio_stream && !video_stream) {
|
yading@10
|
272 fprintf(stderr, "Could not find audio or video stream in the input, aborting\n");
|
yading@10
|
273 ret = 1;
|
yading@10
|
274 goto end;
|
yading@10
|
275 }
|
yading@10
|
276
|
yading@10
|
277 frame = avcodec_alloc_frame();
|
yading@10
|
278 if (!frame) {
|
yading@10
|
279 fprintf(stderr, "Could not allocate frame\n");
|
yading@10
|
280 ret = AVERROR(ENOMEM);
|
yading@10
|
281 goto end;
|
yading@10
|
282 }
|
yading@10
|
283
|
yading@10
|
284 /* initialize packet, set data to NULL, let the demuxer fill it */
|
yading@10
|
285 av_init_packet(&pkt);
|
yading@10
|
286 pkt.data = NULL;
|
yading@10
|
287 pkt.size = 0;
|
yading@10
|
288
|
yading@10
|
289 if (video_stream)
|
yading@10
|
290 printf("Demuxing video from file '%s' into '%s'\n", src_filename, video_dst_filename);
|
yading@10
|
291 if (audio_stream)
|
yading@10
|
292 printf("Demuxing audio from file '%s' into '%s'\n", src_filename, audio_dst_filename);
|
yading@10
|
293
|
yading@10
|
294 /* read frames from the file */
|
yading@10
|
295 while (av_read_frame(fmt_ctx, &pkt) >= 0) {
|
yading@10
|
296 decode_packet(&got_frame, 0);
|
yading@10
|
297 av_free_packet(&pkt);
|
yading@10
|
298 }
|
yading@10
|
299
|
yading@10
|
300 /* flush cached frames */
|
yading@10
|
301 pkt.data = NULL;
|
yading@10
|
302 pkt.size = 0;
|
yading@10
|
303 do {
|
yading@10
|
304 decode_packet(&got_frame, 1);
|
yading@10
|
305 } while (got_frame);
|
yading@10
|
306
|
yading@10
|
307 printf("Demuxing succeeded.\n");
|
yading@10
|
308
|
yading@10
|
309 if (video_stream) {
|
yading@10
|
310 printf("Play the output video file with the command:\n"
|
yading@10
|
311 "ffplay -f rawvideo -pix_fmt %s -video_size %dx%d %s\n",
|
yading@10
|
312 av_get_pix_fmt_name(video_dec_ctx->pix_fmt), video_dec_ctx->width, video_dec_ctx->height,
|
yading@10
|
313 video_dst_filename);
|
yading@10
|
314 }
|
yading@10
|
315
|
yading@10
|
316 if (audio_stream) {
|
yading@10
|
317 const char *fmt;
|
yading@10
|
318
|
yading@10
|
319 if ((ret = get_format_from_sample_fmt(&fmt, audio_dec_ctx->sample_fmt)) < 0)
|
yading@10
|
320 goto end;
|
yading@10
|
321 printf("Play the output audio file with the command:\n"
|
yading@10
|
322 "ffplay -f %s -ac %d -ar %d %s\n",
|
yading@10
|
323 fmt, audio_dec_ctx->channels, audio_dec_ctx->sample_rate,
|
yading@10
|
324 audio_dst_filename);
|
yading@10
|
325 }
|
yading@10
|
326
|
yading@10
|
327 end:
|
yading@10
|
328 if (video_dec_ctx)
|
yading@10
|
329 avcodec_close(video_dec_ctx);
|
yading@10
|
330 if (audio_dec_ctx)
|
yading@10
|
331 avcodec_close(audio_dec_ctx);
|
yading@10
|
332 avformat_close_input(&fmt_ctx);
|
yading@10
|
333 if (video_dst_file)
|
yading@10
|
334 fclose(video_dst_file);
|
yading@10
|
335 if (audio_dst_file)
|
yading@10
|
336 fclose(audio_dst_file);
|
yading@10
|
337 av_free(frame);
|
yading@10
|
338 av_free(video_dst_data[0]);
|
yading@10
|
339 av_free(audio_dst_data);
|
yading@10
|
340
|
yading@10
|
341 return ret < 0;
|
yading@10
|
342 }
|