2 * Copyright (c) 2003 Fabrice Bellard
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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
25 * libavformat API example.
27 * Output a media file in any supported libavformat format.
28 * The default codecs are used.
29 * @example doc/examples/muxing.c
37 #include <libavutil/opt.h>
38 #include <libavutil/mathematics.h>
39 #include <libavformat/avformat.h>
40 #include <libswscale/swscale.h>
41 #include <libswresample/swresample.h>
43 /* 5 seconds stream duration */
44 #define STREAM_DURATION 200.0
45 #define STREAM_FRAME_RATE 25 /* 25 images/s */
46 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
47 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
49 static int sws_flags = SWS_BICUBIC;
51 /* Add an output stream. */
52 static AVStream *add_stream(AVFormatContext *oc, AVCodec **codec,
53 enum AVCodecID codec_id)
58 /* find the encoder */
59 *codec = avcodec_find_encoder(codec_id);
61 fprintf(stderr, "Could not find encoder for '%s'\n",
62 avcodec_get_name(codec_id));
66 st = avformat_new_stream(oc, *codec);
68 fprintf(stderr, "Could not allocate stream\n");
71 st->id = oc->nb_streams-1;
74 switch ((*codec)->type) {
75 case AVMEDIA_TYPE_AUDIO:
76 c->sample_fmt = AV_SAMPLE_FMT_FLTP;
78 c->sample_rate = 44100;
82 case AVMEDIA_TYPE_VIDEO:
83 c->codec_id = codec_id;
86 /* Resolution must be a multiple of two. */
89 /* timebase: This is the fundamental unit of time (in seconds) in terms
90 * of which frame timestamps are represented. For fixed-fps content,
91 * timebase should be 1/framerate and timestamp increments should be
93 c->time_base.den = STREAM_FRAME_RATE;
95 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
96 c->pix_fmt = STREAM_PIX_FMT;
97 if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
98 /* just for testing, we also add B frames */
101 if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
102 /* Needed to avoid using macroblocks in which some coeffs overflow.
103 * This does not happen with normal video, it just happens here as
104 * the motion of the chroma plane does not match the luma plane. */
113 /* Some formats want stream headers to be separate. */
114 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
115 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
120 /**************************************************************/
123 static float t, tincr, tincr2;
125 static uint8_t **src_samples_data;
126 static int src_samples_linesize;
127 static int src_nb_samples;
129 static int max_dst_nb_samples;
130 uint8_t **dst_samples_data;
131 int dst_samples_linesize;
132 int dst_samples_size;
134 struct SwrContext *swr_ctx = NULL;
136 static void open_audio(AVFormatContext *oc, AVCodec *codec, AVStream *st)
144 ret = avcodec_open2(c, codec, NULL);
146 fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
150 /* init signal generator */
152 tincr = 2 * M_PI * 110.0 / c->sample_rate;
153 /* increment frequency by 110 Hz per second */
154 tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
156 src_nb_samples = c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE ?
157 10000 : c->frame_size;
159 ret = av_samples_alloc_array_and_samples(&src_samples_data, &src_samples_linesize, c->channels,
160 src_nb_samples, c->sample_fmt, 0);
162 fprintf(stderr, "Could not allocate source samples\n");
166 /* create resampler context */
167 if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
168 swr_ctx = swr_alloc();
170 fprintf(stderr, "Could not allocate resampler context\n");
175 av_opt_set_int (swr_ctx, "in_channel_count", c->channels, 0);
176 av_opt_set_int (swr_ctx, "in_sample_rate", c->sample_rate, 0);
177 av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
178 av_opt_set_int (swr_ctx, "out_channel_count", c->channels, 0);
179 av_opt_set_int (swr_ctx, "out_sample_rate", c->sample_rate, 0);
180 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
182 /* initialize the resampling context */
183 if ((ret = swr_init(swr_ctx)) < 0) {
184 fprintf(stderr, "Failed to initialize the resampling context\n");
189 /* compute the number of converted samples: buffering is avoided
190 * ensuring that the output buffer will contain at least all the
191 * converted input samples */
192 max_dst_nb_samples = src_nb_samples;
193 ret = av_samples_alloc_array_and_samples(&dst_samples_data, &dst_samples_linesize, c->channels,
194 max_dst_nb_samples, c->sample_fmt, 0);
196 fprintf(stderr, "Could not allocate destination samples\n");
199 dst_samples_size = av_samples_get_buffer_size(NULL, c->channels, max_dst_nb_samples,
203 /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
204 * 'nb_channels' channels. */
205 static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
211 for (j = 0; j < frame_size; j++) {
212 v = (int)(sin(t) * 10000);
213 for (i = 0; i < nb_channels; i++)
220 static void write_audio_frame(AVFormatContext *oc, AVStream *st)
223 AVPacket pkt = { 0 }; // data and size must be 0;
224 AVFrame *frame = av_frame_alloc();
225 int got_packet, ret, dst_nb_samples;
227 av_init_packet(&pkt);
230 get_audio_frame((int16_t *)src_samples_data[0], src_nb_samples, c->channels);
232 /* convert samples from native format to destination codec format, using the resampler */
234 /* compute destination number of samples */
235 dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, c->sample_rate) + src_nb_samples,
236 c->sample_rate, c->sample_rate, AV_ROUND_UP);
237 if (dst_nb_samples > max_dst_nb_samples) {
238 av_free(dst_samples_data[0]);
239 ret = av_samples_alloc(dst_samples_data, &dst_samples_linesize, c->channels,
240 dst_nb_samples, c->sample_fmt, 0);
243 max_dst_nb_samples = dst_nb_samples;
244 dst_samples_size = av_samples_get_buffer_size(NULL, c->channels, dst_nb_samples,
248 /* convert to destination format */
249 ret = swr_convert(swr_ctx,
250 dst_samples_data, dst_nb_samples,
251 (const uint8_t **)src_samples_data, src_nb_samples);
253 fprintf(stderr, "Error while converting\n");
257 dst_samples_data[0] = src_samples_data[0];
258 dst_nb_samples = src_nb_samples;
261 frame->nb_samples = dst_nb_samples;
262 avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
263 dst_samples_data[0], dst_samples_size, 0);
265 ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
267 fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
274 pkt.stream_index = st->index;
276 /* Write the compressed frame to the media file. */
277 ret = av_interleaved_write_frame(oc, &pkt);
279 fprintf(stderr, "Error while writing audio frame: %s\n",
283 avcodec_free_frame(&frame);
286 static void close_audio(AVFormatContext *oc, AVStream *st)
288 avcodec_close(st->codec);
289 av_free(src_samples_data[0]);
290 av_free(dst_samples_data[0]);
293 /**************************************************************/
296 static AVFrame *frame;
297 static AVPicture src_picture, dst_picture;
298 static int frame_count;
300 static void open_video(AVFormatContext *oc, AVCodec *codec, AVStream *st)
303 AVCodecContext *c = st->codec;
306 ret = avcodec_open2(c, codec, NULL);
308 fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
312 /* allocate and init a re-usable frame */
313 frame = av_frame_alloc();
315 fprintf(stderr, "Could not allocate video frame\n");
319 /* Allocate the encoded raw picture. */
320 ret = avpicture_alloc(&dst_picture, c->pix_fmt, c->width, c->height);
322 fprintf(stderr, "Could not allocate picture: %s\n", av_err2str(ret));
326 /* If the output format is not YUV420P, then a temporary YUV420P
327 * picture is needed too. It is then converted to the required
329 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
330 ret = avpicture_alloc(&src_picture, AV_PIX_FMT_YUV420P, c->width, c->height);
332 fprintf(stderr, "Could not allocate temporary picture: %s\n",
338 /* copy data and linesize picture pointers to frame */
339 *((AVPicture *)frame) = dst_picture;
342 /* Prepare a dummy image. */
343 static void fill_yuv_image(AVPicture *pict, int frame_index,
344 int width, int height)
351 for (y = 0; y < height; y++)
352 for (x = 0; x < width; x++)
353 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
356 for (y = 0; y < height / 2; y++) {
357 for (x = 0; x < width / 2; x++) {
358 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
359 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
364 static void write_video_frame(AVFormatContext *oc, AVStream *st)
367 static struct SwsContext *sws_ctx;
368 AVCodecContext *c = st->codec;
370 if (frame_count >= STREAM_NB_FRAMES) {
371 /* No more frames to compress. The codec has a latency of a few
372 * frames if using B-frames, so we get the last frames by
373 * passing the same picture again. */
375 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
376 /* as we only generate a YUV420P picture, we must convert it
377 * to the codec pixel format if needed */
379 sws_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_YUV420P,
380 c->width, c->height, c->pix_fmt,
381 sws_flags, NULL, NULL, NULL);
384 "Could not initialize the conversion context\n");
388 fill_yuv_image(&src_picture, frame_count, c->width, c->height);
390 (const uint8_t * const *)src_picture.data, src_picture.linesize,
391 0, c->height, dst_picture.data, dst_picture.linesize);
393 fill_yuv_image(&dst_picture, frame_count, c->width, c->height);
397 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
398 /* Raw video case - directly store the picture in the packet */
400 av_init_packet(&pkt);
402 pkt.flags |= AV_PKT_FLAG_KEY;
403 pkt.stream_index = st->index;
404 pkt.data = dst_picture.data[0];
405 pkt.size = sizeof(AVPicture);
407 ret = av_interleaved_write_frame(oc, &pkt);
409 AVPacket pkt = { 0 };
411 av_init_packet(&pkt);
413 /* encode the image */
414 ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
416 fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
419 /* If size is zero, it means the image was buffered. */
421 if (!ret && got_packet && pkt.size) {
422 pkt.stream_index = st->index;
424 /* Write the compressed frame to the media file. */
425 ret = av_interleaved_write_frame(oc, &pkt);
431 fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
437 static void close_video(AVFormatContext *oc, AVStream *st)
439 avcodec_close(st->codec);
440 av_free(src_picture.data[0]);
441 av_free(dst_picture.data[0]);
445 /**************************************************************/
446 /* media file output */
448 int main(int argc, char **argv)
450 const char *filename;
453 AVStream *audio_st, *video_st;
454 AVCodec *audio_codec, *video_codec;
455 double audio_time, video_time;
458 /* Initialize libavcodec, and register all codecs and formats. */
462 printf("usage: %s output_file\n"
463 "API example program to output a media file with libavformat.\n"
464 "This program generates a synthetic audio and video stream, encodes and\n"
465 "muxes them into a file named output_file.\n"
466 "The output format is automatically guessed according to the file extension.\n"
467 "Raw images can also be output by using '%%d' in the filename.\n"
474 /* allocate the output media context */
475 avformat_alloc_output_context2(&oc, NULL, NULL, filename);
477 printf("Could not deduce output format from file extension: using MPEG.\n");
478 avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
485 /* Add the audio and video streams using the default format codecs
486 * and initialize the codecs. */
490 if (fmt->video_codec != AV_CODEC_ID_NONE) {
491 video_st = add_stream(oc, &video_codec, fmt->video_codec);
493 if (fmt->audio_codec != AV_CODEC_ID_NONE) {
494 audio_st = add_stream(oc, &audio_codec, fmt->audio_codec);
497 /* Now that all the parameters are set, we can open the audio and
498 * video codecs and allocate the necessary encode buffers. */
500 open_video(oc, video_codec, video_st);
502 open_audio(oc, audio_codec, audio_st);
504 av_dump_format(oc, 0, filename, 1);
506 /* open the output file, if needed */
507 if (!(fmt->flags & AVFMT_NOFILE)) {
508 ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
510 fprintf(stderr, "Could not open '%s': %s\n", filename,
516 /* Write the stream header, if any. */
517 ret = avformat_write_header(oc, NULL);
519 fprintf(stderr, "Error occurred when opening output file: %s\n",
527 /* Compute current audio and video time. */
528 audio_time = audio_st ? audio_st->pts.val * av_q2d(audio_st->time_base) : 0.0;
529 video_time = video_st ? video_st->pts.val * av_q2d(video_st->time_base) : 0.0;
531 if ((!audio_st || audio_time >= STREAM_DURATION) &&
532 (!video_st || video_time >= STREAM_DURATION))
535 /* write interleaved audio and video frames */
536 if (!video_st || (video_st && audio_st && audio_time < video_time)) {
537 write_audio_frame(oc, audio_st);
539 write_video_frame(oc, video_st);
540 frame->pts += av_rescale_q(1, video_st->codec->time_base, video_st->time_base);
544 /* Write the trailer, if any. The trailer must be written before you
545 * close the CodecContexts open when you wrote the header; otherwise
546 * av_write_trailer() may try to use memory that was freed on
547 * av_codec_close(). */
548 av_write_trailer(oc);
550 /* Close each codec. */
552 close_video(oc, video_st);
554 close_audio(oc, audio_st);
556 if (!(fmt->flags & AVFMT_NOFILE))
557 /* Close the output file. */
560 /* free the stream */
561 avformat_free_context(oc);