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 static int audio_is_eof, video_is_eof;
48 #define STREAM_DURATION 10.0
49 #define STREAM_FRAME_RATE 25 /* 25 images/s */
50 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
52 #define SCALE_FLAGS SWS_BICUBIC
54 // a wrapper around a single output AVStream
55 typedef struct OutputStream {
61 float t, tincr, tincr2;
63 struct SwsContext *sws_ctx;
66 static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
68 AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
70 printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
71 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
72 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
73 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
77 static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
79 /* rescale output packet timestamp values from codec to stream timebase */
80 av_packet_rescale_ts(pkt, *time_base, st->time_base);
81 pkt->stream_index = st->index;
83 /* Write the compressed frame to the media file. */
84 log_packet(fmt_ctx, pkt);
85 return av_interleaved_write_frame(fmt_ctx, pkt);
88 /* Add an output stream. */
89 static void add_stream(OutputStream *ost, AVFormatContext *oc,
91 enum AVCodecID codec_id)
95 /* find the encoder */
96 *codec = avcodec_find_encoder(codec_id);
98 fprintf(stderr, "Could not find encoder for '%s'\n",
99 avcodec_get_name(codec_id));
103 ost->st = avformat_new_stream(oc, *codec);
105 fprintf(stderr, "Could not allocate stream\n");
108 ost->st->id = oc->nb_streams-1;
111 switch ((*codec)->type) {
112 case AVMEDIA_TYPE_AUDIO:
113 c->sample_fmt = (*codec)->sample_fmts ?
114 (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
116 c->sample_rate = 44100;
118 c->channel_layout = AV_CH_LAYOUT_STEREO;
121 case AVMEDIA_TYPE_VIDEO:
122 c->codec_id = codec_id;
124 c->bit_rate = 400000;
125 /* Resolution must be a multiple of two. */
128 /* timebase: This is the fundamental unit of time (in seconds) in terms
129 * of which frame timestamps are represented. For fixed-fps content,
130 * timebase should be 1/framerate and timestamp increments should be
132 c->time_base.den = STREAM_FRAME_RATE;
133 c->time_base.num = 1;
134 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
135 c->pix_fmt = STREAM_PIX_FMT;
136 if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
137 /* just for testing, we also add B frames */
140 if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
141 /* Needed to avoid using macroblocks in which some coeffs overflow.
142 * This does not happen with normal video, it just happens here as
143 * the motion of the chroma plane does not match the luma plane. */
152 /* Some formats want stream headers to be separate. */
153 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
154 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
157 /**************************************************************/
162 struct SwrContext *swr_ctx = NULL;
164 static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
172 ret = avcodec_open2(c, codec, NULL);
174 fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
178 /* init signal generator */
180 ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
181 /* increment frequency by 110 Hz per second */
182 ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
184 ost->frame = av_frame_alloc();
188 ost->frame->sample_rate = c->sample_rate;
189 ost->frame->format = AV_SAMPLE_FMT_S16;
190 ost->frame->channel_layout = c->channel_layout;
192 if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
193 ost->frame->nb_samples = 10000;
195 ost->frame->nb_samples = c->frame_size;
197 ost->tmp_frame = av_frame_alloc();
201 ost->tmp_frame->sample_rate = c->sample_rate;
202 ost->tmp_frame->format = c->sample_fmt;
203 ost->tmp_frame->channel_layout = c->channel_layout;
204 ost->tmp_frame->nb_samples = ost->frame->nb_samples;
206 /* create resampler context */
207 if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
208 swr_ctx = swr_alloc();
210 fprintf(stderr, "Could not allocate resampler context\n");
215 av_opt_set_int (swr_ctx, "in_channel_count", c->channels, 0);
216 av_opt_set_int (swr_ctx, "in_sample_rate", c->sample_rate, 0);
217 av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
218 av_opt_set_int (swr_ctx, "out_channel_count", c->channels, 0);
219 av_opt_set_int (swr_ctx, "out_sample_rate", c->sample_rate, 0);
220 av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
222 /* initialize the resampling context */
223 if ((ret = swr_init(swr_ctx)) < 0) {
224 fprintf(stderr, "Failed to initialize the resampling context\n");
229 ret = av_frame_get_buffer(ost->frame, 0);
231 fprintf(stderr, "Could not allocate an audio frame.\n");
234 ret = av_frame_get_buffer(ost->tmp_frame, 0);
236 fprintf(stderr, "Could not allocate an audio frame.\n");
241 /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
242 * 'nb_channels' channels. */
243 static void get_audio_frame(OutputStream *ost, AVFrame *frame, int nb_channels)
246 int16_t *q = (int16_t*)frame->data[0];
248 /* when we pass a frame to the encoder, it may keep a reference to it
250 * make sure we do not overwrite it here
252 ret = av_frame_make_writable(frame);
256 for (j = 0; j < frame->nb_samples; j++) {
257 v = (int)(sin(ost->t) * 10000);
258 for (i = 0; i < nb_channels; i++)
260 ost->t += ost->tincr;
261 ost->tincr += ost->tincr2;
265 static void write_audio_frame(AVFormatContext *oc, OutputStream *ost, int flush)
268 AVPacket pkt = { 0 }; // data and size must be 0;
273 av_init_packet(&pkt);
277 get_audio_frame(ost, ost->frame, c->channels);
279 /* convert samples from native format to destination codec format, using the resampler */
281 /* compute destination number of samples */
282 dst_nb_samples = av_rescale_rnd(swr_get_delay(swr_ctx, c->sample_rate) + ost->frame->nb_samples,
283 c->sample_rate, c->sample_rate, AV_ROUND_UP);
284 av_assert0(dst_nb_samples == ost->frame->nb_samples);
286 /* convert to destination format */
287 ret = swr_convert(swr_ctx,
288 ost->tmp_frame->data, dst_nb_samples,
289 (const uint8_t **)ost->frame->data, ost->frame->nb_samples);
291 fprintf(stderr, "Error while converting\n");
294 frame = ost->tmp_frame;
296 dst_nb_samples = ost->frame->nb_samples;
300 frame->pts = av_rescale_q(samples_count, (AVRational){1, c->sample_rate}, c->time_base);
301 samples_count += dst_nb_samples;
304 ret = avcodec_encode_audio2(c, &pkt, flush ? NULL : frame, &got_packet);
306 fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
317 ret = write_frame(oc, &c->time_base, ost->st, &pkt);
319 fprintf(stderr, "Error while writing audio frame: %s\n",
325 /**************************************************************/
328 static int frame_count;
330 static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
335 picture = av_frame_alloc();
339 picture->format = pix_fmt;
340 picture->width = width;
341 picture->height = height;
343 /* allocate the buffers for the frame data */
344 ret = av_frame_get_buffer(picture, 32);
346 fprintf(stderr, "Could not allocate frame data.\n");
353 static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
356 AVCodecContext *c = ost->st->codec;
359 ret = avcodec_open2(c, codec, NULL);
361 fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
365 /* allocate and init a re-usable frame */
366 ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
368 fprintf(stderr, "Could not allocate video frame\n");
372 /* If the output format is not YUV420P, then a temporary YUV420P
373 * picture is needed too. It is then converted to the required
375 ost->tmp_frame = NULL;
376 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
377 ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
378 if (!ost->tmp_frame) {
379 fprintf(stderr, "Could not allocate temporary picture\n");
385 /* Prepare a dummy image. */
386 static void fill_yuv_image(AVFrame *pict, int frame_index,
387 int width, int height)
391 /* when we pass a frame to the encoder, it may keep a reference to it
393 * make sure we do not overwrite it here
395 ret = av_frame_make_writable(pict);
402 for (y = 0; y < height; y++)
403 for (x = 0; x < width; x++)
404 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
407 for (y = 0; y < height / 2; y++) {
408 for (x = 0; x < width / 2; x++) {
409 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
410 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
415 static void write_video_frame(AVFormatContext *oc, OutputStream *ost, int flush)
418 AVCodecContext *c = ost->st->codec;
421 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
422 /* as we only generate a YUV420P picture, we must convert it
423 * to the codec pixel format if needed */
425 ost->sws_ctx = sws_getContext(c->width, c->height, AV_PIX_FMT_YUV420P,
428 SCALE_FLAGS, NULL, NULL, NULL);
431 "Could not initialize the conversion context\n");
435 fill_yuv_image(ost->tmp_frame, frame_count, c->width, c->height);
436 sws_scale(ost->sws_ctx,
437 (const uint8_t * const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
438 0, c->height, ost->frame->data, ost->frame->linesize);
440 fill_yuv_image(ost->frame, frame_count, c->width, c->height);
444 if (oc->oformat->flags & AVFMT_RAWPICTURE && !flush) {
445 /* Raw video case - directly store the picture in the packet */
447 av_init_packet(&pkt);
449 pkt.flags |= AV_PKT_FLAG_KEY;
450 pkt.stream_index = ost->st->index;
451 pkt.data = (uint8_t *)ost->frame;
452 pkt.size = sizeof(AVPicture);
454 ret = av_interleaved_write_frame(oc, &pkt);
456 AVPacket pkt = { 0 };
458 av_init_packet(&pkt);
460 /* encode the image */
461 ost->frame->pts = frame_count;
462 ret = avcodec_encode_video2(c, &pkt, flush ? NULL : ost->frame, &got_packet);
464 fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
467 /* If size is zero, it means the image was buffered. */
470 ret = write_frame(oc, &c->time_base, ost->st, &pkt);
479 fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
485 static void close_stream(AVFormatContext *oc, OutputStream *ost)
487 avcodec_close(ost->st->codec);
488 av_frame_free(&ost->frame);
489 av_frame_free(&ost->tmp_frame);
490 sws_freeContext(ost->sws_ctx);
493 /**************************************************************/
494 /* media file output */
496 int main(int argc, char **argv)
498 OutputStream video_st = { 0 }, audio_st = { 0 };
499 const char *filename;
502 AVCodec *audio_codec, *video_codec;
503 double audio_time, video_time;
505 int have_video = 0, have_audio = 0;
507 /* Initialize libavcodec, and register all codecs and formats. */
511 printf("usage: %s output_file\n"
512 "API example program to output a media file with libavformat.\n"
513 "This program generates a synthetic audio and video stream, encodes and\n"
514 "muxes them into a file named output_file.\n"
515 "The output format is automatically guessed according to the file extension.\n"
516 "Raw images can also be output by using '%%d' in the filename.\n"
523 /* allocate the output media context */
524 avformat_alloc_output_context2(&oc, NULL, NULL, filename);
526 printf("Could not deduce output format from file extension: using MPEG.\n");
527 avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
534 /* Add the audio and video streams using the default format codecs
535 * and initialize the codecs. */
536 if (fmt->video_codec != AV_CODEC_ID_NONE) {
537 add_stream(&video_st, oc, &video_codec, fmt->video_codec);
540 if (fmt->audio_codec != AV_CODEC_ID_NONE) {
541 add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
545 /* Now that all the parameters are set, we can open the audio and
546 * video codecs and allocate the necessary encode buffers. */
548 open_video(oc, video_codec, &video_st);
551 open_audio(oc, audio_codec, &audio_st);
553 av_dump_format(oc, 0, filename, 1);
555 /* open the output file, if needed */
556 if (!(fmt->flags & AVFMT_NOFILE)) {
557 ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
559 fprintf(stderr, "Could not open '%s': %s\n", filename,
565 /* Write the stream header, if any. */
566 ret = avformat_write_header(oc, NULL);
568 fprintf(stderr, "Error occurred when opening output file: %s\n",
574 while ((have_video && !video_is_eof) || (have_audio && !audio_is_eof)) {
575 /* Compute current audio and video time. */
576 audio_time = (have_audio && !audio_is_eof) ? audio_st.st->pts.val * av_q2d(audio_st.st->time_base) : INFINITY;
577 video_time = (have_video && !video_is_eof) ? video_st.st->pts.val * av_q2d(video_st.st->time_base) : INFINITY;
580 (!have_audio || audio_time >= STREAM_DURATION) &&
581 (!have_video || video_time >= STREAM_DURATION)) {
585 /* write interleaved audio and video frames */
586 if (have_audio && !audio_is_eof && audio_time <= video_time) {
587 write_audio_frame(oc, &audio_st, flush);
588 } else if (have_video && !video_is_eof && video_time < audio_time) {
589 write_video_frame(oc, &video_st, flush);
593 /* Write the trailer, if any. The trailer must be written before you
594 * close the CodecContexts open when you wrote the header; otherwise
595 * av_write_trailer() may try to use memory that was freed on
596 * av_codec_close(). */
597 av_write_trailer(oc);
599 /* Close each codec. */
601 close_stream(oc, &video_st);
603 close_stream(oc, &audio_st);
605 if (!(fmt->flags & AVFMT_NOFILE))
606 /* Close the output file. */
609 /* free the stream */
610 avformat_free_context(oc);