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. The default
37 #include <libavutil/avassert.h>
38 #include <libavutil/channel_layout.h>
39 #include <libavutil/opt.h>
40 #include <libavutil/mathematics.h>
41 #include <libavutil/timestamp.h>
42 #include <libavformat/avformat.h>
43 #include <libswscale/swscale.h>
44 #include <libswresample/swresample.h>
46 #define STREAM_DURATION 10.0
47 #define STREAM_FRAME_RATE 25 /* 25 images/s */
48 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
50 #define SCALE_FLAGS SWS_BICUBIC
52 // a wrapper around a single output AVStream
53 typedef struct OutputStream {
56 /* pts of the next frame that will be generated */
62 float t, tincr, tincr2;
64 struct SwsContext *sws_ctx;
67 static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
69 AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
71 printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
72 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
73 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
74 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
78 static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
80 /* rescale output packet timestamp values from codec to stream timebase */
81 av_packet_rescale_ts(pkt, *time_base, st->time_base);
82 pkt->stream_index = st->index;
84 /* Write the compressed frame to the media file. */
85 log_packet(fmt_ctx, pkt);
86 return av_interleaved_write_frame(fmt_ctx, pkt);
89 /* Add an output stream. */
90 static void add_stream(OutputStream *ost, AVFormatContext *oc,
92 enum AVCodecID codec_id)
96 /* find the encoder */
97 *codec = avcodec_find_encoder(codec_id);
99 fprintf(stderr, "Could not find encoder for '%s'\n",
100 avcodec_get_name(codec_id));
104 ost->st = avformat_new_stream(oc, *codec);
106 fprintf(stderr, "Could not allocate stream\n");
109 ost->st->id = oc->nb_streams-1;
112 switch ((*codec)->type) {
113 case AVMEDIA_TYPE_AUDIO:
114 c->sample_fmt = (*codec)->sample_fmts ?
115 (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
117 c->sample_rate = 44100;
119 c->channel_layout = AV_CH_LAYOUT_STEREO;
122 case AVMEDIA_TYPE_VIDEO:
123 c->codec_id = codec_id;
125 c->bit_rate = 400000;
126 /* Resolution must be a multiple of two. */
129 /* timebase: This is the fundamental unit of time (in seconds) in terms
130 * of which frame timestamps are represented. For fixed-fps content,
131 * timebase should be 1/framerate and timestamp increments should be
133 c->time_base.den = STREAM_FRAME_RATE;
134 c->time_base.num = 1;
135 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
136 c->pix_fmt = STREAM_PIX_FMT;
137 if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
138 /* just for testing, we also add B frames */
141 if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
142 /* Needed to avoid using macroblocks in which some coeffs overflow.
143 * This does not happen with normal video, it just happens here as
144 * the motion of the chroma plane does not match the luma plane. */
153 /* Some formats want stream headers to be separate. */
154 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
155 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
158 /**************************************************************/
163 struct SwrContext *swr_ctx = NULL;
165 static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
173 ret = avcodec_open2(c, codec, NULL);
175 fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
179 /* init signal generator */
181 ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
182 /* increment frequency by 110 Hz per second */
183 ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
185 ost->frame = av_frame_alloc();
189 ost->frame->sample_rate = c->sample_rate;
190 ost->frame->format = AV_SAMPLE_FMT_S16;
191 ost->frame->channel_layout = c->channel_layout;
193 if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
194 ost->frame->nb_samples = 10000;
196 ost->frame->nb_samples = c->frame_size;
198 ost->tmp_frame = av_frame_alloc();
202 ost->tmp_frame->sample_rate = c->sample_rate;
203 ost->tmp_frame->format = c->sample_fmt;
204 ost->tmp_frame->channel_layout = c->channel_layout;
205 ost->tmp_frame->nb_samples = ost->frame->nb_samples;
207 /* create resampler context */
208 if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
209 swr_ctx = swr_alloc();
211 fprintf(stderr, "Could not allocate resampler context\n");
216 av_opt_set_int (swr_ctx, "in_channel_count", c->channels, 0);
217 av_opt_set_int (swr_ctx, "in_sample_rate", c->sample_rate, 0);
218 av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
219 av_opt_set_int (swr_ctx, "out_channel_count", c->channels, 0);
220 av_opt_set_int (swr_ctx, "out_sample_rate", c->sample_rate, 0);
221 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
223 /* initialize the resampling context */
224 if ((ret = swr_init(swr_ctx)) < 0) {
225 fprintf(stderr, "Failed to initialize the resampling context\n");
230 ret = av_frame_get_buffer(ost->frame, 0);
232 fprintf(stderr, "Could not allocate an audio frame.\n");
235 ret = av_frame_get_buffer(ost->tmp_frame, 0);
237 fprintf(stderr, "Could not allocate an audio frame.\n");
242 /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
243 * 'nb_channels' channels. */
244 static AVFrame *get_audio_frame(OutputStream *ost)
247 int16_t *q = (int16_t*)ost->frame->data[0];
249 /* check if we want to generate more frames */
250 if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
251 STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
254 /* when we pass a frame to the encoder, it may keep a reference to it
256 * make sure we do not overwrite it here
258 ret = av_frame_make_writable(ost->frame);
262 for (j = 0; j < ost->frame->nb_samples; j++) {
263 v = (int)(sin(ost->t) * 10000);
264 for (i = 0; i < ost->st->codec->channels; i++)
266 ost->t += ost->tincr;
267 ost->tincr += ost->tincr2;
270 ost->frame->pts = ost->next_pts;
271 ost->next_pts += ost->frame->nb_samples;
277 * encode one audio frame and send it to the muxer
278 * return 1 when encoding is finished, 0 otherwise
280 static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
283 AVPacket pkt = { 0 }; // data and size must be 0;
289 av_init_packet(&pkt);
292 frame = get_audio_frame(ost);
295 /* convert samples from native format to destination codec format, using the resampler */
297 /* compute destination number of samples */
298 dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, c->sample_rate) + frame->nb_samples,
299 c->sample_rate, c->sample_rate, AV_ROUND_UP);
300 av_assert0(dst_nb_samples == frame->nb_samples);
302 /* convert to destination format */
303 ret = swr_convert(swr_ctx,
304 ost->tmp_frame->data, dst_nb_samples,
305 (const uint8_t **)frame->data, frame->nb_samples);
307 fprintf(stderr, "Error while converting\n");
310 frame = ost->tmp_frame;
312 dst_nb_samples = frame->nb_samples;
315 frame->pts = av_rescale_q(samples_count, (AVRational){1, c->sample_rate}, c->time_base);
316 samples_count += dst_nb_samples;
319 ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
321 fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
326 ret = write_frame(oc, &c->time_base, ost->st, &pkt);
328 fprintf(stderr, "Error while writing audio frame: %s\n",
334 return (frame || got_packet) ? 0 : 1;
337 /**************************************************************/
340 static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
345 picture = av_frame_alloc();
349 picture->format = pix_fmt;
350 picture->width = width;
351 picture->height = height;
353 /* allocate the buffers for the frame data */
354 ret = av_frame_get_buffer(picture, 32);
356 fprintf(stderr, "Could not allocate frame data.\n");
363 static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
366 AVCodecContext *c = ost->st->codec;
369 ret = avcodec_open2(c, codec, NULL);
371 fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
375 /* allocate and init a re-usable frame */
376 ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
378 fprintf(stderr, "Could not allocate video frame\n");
382 /* If the output format is not YUV420P, then a temporary YUV420P
383 * picture is needed too. It is then converted to the required
385 ost->tmp_frame = NULL;
386 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
387 ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
388 if (!ost->tmp_frame) {
389 fprintf(stderr, "Could not allocate temporary picture\n");
395 /* Prepare a dummy image. */
396 static void fill_yuv_image(AVFrame *pict, int frame_index,
397 int width, int height)
401 /* when we pass a frame to the encoder, it may keep a reference to it
403 * make sure we do not overwrite it here
405 ret = av_frame_make_writable(pict);
412 for (y = 0; y < height; y++)
413 for (x = 0; x < width; x++)
414 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
417 for (y = 0; y < height / 2; y++) {
418 for (x = 0; x < width / 2; x++) {
419 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
420 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
425 static AVFrame *get_video_frame(OutputStream *ost)
427 AVCodecContext *c = ost->st->codec;
429 /* check if we want to generate more frames */
430 if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
431 STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
434 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
435 /* as we only generate a YUV420P picture, we must convert it
436 * to the codec pixel format if needed */
438 ost->sws_ctx = sws_getContext(c->width, c->height,
442 SCALE_FLAGS, NULL, NULL, NULL);
445 "Could not initialize the conversion context\n");
449 fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
450 sws_scale(ost->sws_ctx,
451 (const uint8_t * const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
452 0, c->height, ost->frame->data, ost->frame->linesize);
454 fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
457 ost->frame->pts = ost->next_pts++;
463 * encode one video frame and send it to the muxer
464 * return 1 when encoding is finished, 0 otherwise
466 static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
475 frame = get_video_frame(ost);
477 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
478 /* a hack to avoid data copy with some raw video muxers */
480 av_init_packet(&pkt);
485 pkt.flags |= AV_PKT_FLAG_KEY;
486 pkt.stream_index = ost->st->index;
487 pkt.data = (uint8_t *)frame;
488 pkt.size = sizeof(AVPicture);
490 pkt.pts = pkt.dts = frame->pts;
491 av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
493 ret = av_interleaved_write_frame(oc, &pkt);
495 AVPacket pkt = { 0 };
496 av_init_packet(&pkt);
498 /* encode the image */
499 ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
501 fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
506 ret = write_frame(oc, &c->time_base, ost->st, &pkt);
513 fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
517 return (frame || got_packet) ? 0 : 1;
520 static void close_stream(AVFormatContext *oc, OutputStream *ost)
522 avcodec_close(ost->st->codec);
523 av_frame_free(&ost->frame);
524 av_frame_free(&ost->tmp_frame);
525 sws_freeContext(ost->sws_ctx);
528 /**************************************************************/
529 /* media file output */
531 int main(int argc, char **argv)
533 OutputStream video_st = { 0 }, audio_st = { 0 };
534 const char *filename;
537 AVCodec *audio_codec, *video_codec;
539 int have_video = 0, have_audio = 0;
540 int encode_video = 0, encode_audio = 0;
542 /* Initialize libavcodec, and register all codecs and formats. */
546 printf("usage: %s output_file\n"
547 "API example program to output a media file with libavformat.\n"
548 "This program generates a synthetic audio and video stream, encodes and\n"
549 "muxes them into a file named output_file.\n"
550 "The output format is automatically guessed according to the file extension.\n"
551 "Raw images can also be output by using '%%d' in the filename.\n"
558 /* allocate the output media context */
559 avformat_alloc_output_context2(&oc, NULL, NULL, filename);
561 printf("Could not deduce output format from file extension: using MPEG.\n");
562 avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
569 /* Add the audio and video streams using the default format codecs
570 * and initialize the codecs. */
571 if (fmt->video_codec != AV_CODEC_ID_NONE) {
572 add_stream(&video_st, oc, &video_codec, fmt->video_codec);
576 if (fmt->audio_codec != AV_CODEC_ID_NONE) {
577 add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
582 /* Now that all the parameters are set, we can open the audio and
583 * video codecs and allocate the necessary encode buffers. */
585 open_video(oc, video_codec, &video_st);
588 open_audio(oc, audio_codec, &audio_st);
590 av_dump_format(oc, 0, filename, 1);
592 /* open the output file, if needed */
593 if (!(fmt->flags & AVFMT_NOFILE)) {
594 ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
596 fprintf(stderr, "Could not open '%s': %s\n", filename,
602 /* Write the stream header, if any. */
603 ret = avformat_write_header(oc, NULL);
605 fprintf(stderr, "Error occurred when opening output file: %s\n",
610 while (encode_video || encode_audio) {
611 /* select the stream to encode */
613 (!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
614 audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
615 encode_video = !write_video_frame(oc, &video_st);
617 encode_audio = !write_audio_frame(oc, &audio_st);
621 /* Write the trailer, if any. The trailer must be written before you
622 * close the CodecContexts open when you wrote the header; otherwise
623 * av_write_trailer() may try to use memory that was freed on
624 * av_codec_close(). */
625 av_write_trailer(oc);
627 /* Close each codec. */
629 close_stream(oc, &video_st);
631 close_stream(oc, &audio_st);
633 if (!(fmt->flags & AVFMT_NOFILE))
634 /* Close the output file. */
637 /* free the stream */
638 avformat_free_context(oc);