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.
28 * Output a media file in any supported libavformat format. The default
37 #include "libavutil/channel_layout.h"
38 #include "libavutil/mathematics.h"
39 #include "libavformat/avformat.h"
40 #include "libswscale/swscale.h"
42 /* 5 seconds stream duration */
43 #define STREAM_DURATION 5.0
44 #define STREAM_FRAME_RATE 25 /* 25 images/s */
45 #define STREAM_NB_FRAMES ((int)(STREAM_DURATION * STREAM_FRAME_RATE))
46 #define STREAM_PIX_FMT AV_PIX_FMT_YUV420P /* default pix_fmt */
48 #define SCALE_FLAGS SWS_BICUBIC
50 // a wrapper around a single output AVStream
51 typedef struct OutputStream {
57 float t, tincr, tincr2;
59 struct SwsContext *sws_ctx;
62 /**************************************************************/
66 * add an audio output stream
68 static void add_audio_stream(OutputStream *ost, AVFormatContext *oc,
69 enum AVCodecID codec_id)
74 /* find the audio encoder */
75 codec = avcodec_find_encoder(codec_id);
77 fprintf(stderr, "codec not found\n");
81 ost->st = avformat_new_stream(oc, codec);
83 fprintf(stderr, "Could not alloc stream\n");
89 /* put sample parameters */
90 c->sample_fmt = AV_SAMPLE_FMT_S16;
92 c->sample_rate = 44100;
94 c->channel_layout = AV_CH_LAYOUT_STEREO;
96 // some formats want stream headers to be separate
97 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
98 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
101 static void open_audio(AVFormatContext *oc, OutputStream *ost)
109 if (avcodec_open2(c, NULL, NULL) < 0) {
110 fprintf(stderr, "could not open codec\n");
114 /* init signal generator */
116 ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
117 /* increment frequency by 110 Hz per second */
118 ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
120 ost->frame = av_frame_alloc();
124 ost->frame->sample_rate = c->sample_rate;
125 ost->frame->format = AV_SAMPLE_FMT_S16;
126 ost->frame->channel_layout = c->channel_layout;
128 if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
129 ost->frame->nb_samples = 10000;
131 ost->frame->nb_samples = c->frame_size;
133 ret = av_frame_get_buffer(ost->frame, 0);
135 fprintf(stderr, "Could not allocate an audio frame.\n");
140 /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
141 * 'nb_channels' channels. */
142 static void get_audio_frame(OutputStream *ost, AVFrame *frame, int nb_channels)
145 int16_t *q = (int16_t*)frame->data[0];
147 /* when we pass a frame to the encoder, it may keep a reference to it
149 * make sure we do not overwrite it here
151 ret = av_frame_make_writable(frame);
155 for (j = 0; j < frame->nb_samples; j++) {
156 v = (int)(sin(ost->t) * 10000);
157 for (i = 0; i < nb_channels; i++)
159 ost->t += ost->tincr;
160 ost->tincr += ost->tincr2;
164 static void write_audio_frame(AVFormatContext *oc, OutputStream *ost)
167 AVPacket pkt = { 0 }; // data and size must be 0;
170 av_init_packet(&pkt);
173 get_audio_frame(ost, ost->frame, c->channels);
175 avcodec_encode_audio2(c, &pkt, ost->frame, &got_packet);
179 pkt.stream_index = ost->st->index;
181 /* Write the compressed frame to the media file. */
182 if (av_interleaved_write_frame(oc, &pkt) != 0) {
183 fprintf(stderr, "Error while writing audio frame\n");
188 /**************************************************************/
191 static int frame_count;
193 /* Add a video output stream. */
194 static void add_video_stream(OutputStream *ost, AVFormatContext *oc,
195 enum AVCodecID codec_id)
200 /* find the video encoder */
201 codec = avcodec_find_encoder(codec_id);
203 fprintf(stderr, "codec not found\n");
207 ost->st = avformat_new_stream(oc, codec);
209 fprintf(stderr, "Could not alloc stream\n");
215 /* Put sample parameters. */
216 c->bit_rate = 400000;
217 /* Resolution must be a multiple of two. */
220 /* timebase: This is the fundamental unit of time (in seconds) in terms
221 * of which frame timestamps are represented. For fixed-fps content,
222 * timebase should be 1/framerate and timestamp increments should be
224 c->time_base.den = STREAM_FRAME_RATE;
225 c->time_base.num = 1;
226 c->gop_size = 12; /* emit one intra frame every twelve frames at most */
227 c->pix_fmt = STREAM_PIX_FMT;
228 if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
229 /* just for testing, we also add B frames */
232 if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
233 /* Needed to avoid using macroblocks in which some coeffs overflow.
234 * This does not happen with normal video, it just happens here as
235 * the motion of the chroma plane does not match the luma plane. */
238 /* Some formats want stream headers to be separate. */
239 if (oc->oformat->flags & AVFMT_GLOBALHEADER)
240 c->flags |= CODEC_FLAG_GLOBAL_HEADER;
243 static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
248 picture = av_frame_alloc();
252 picture->format = pix_fmt;
253 picture->width = width;
254 picture->height = height;
256 /* allocate the buffers for the frame data */
257 ret = av_frame_get_buffer(picture, 32);
259 fprintf(stderr, "Could not allocate frame data.\n");
266 static void open_video(AVFormatContext *oc, OutputStream *ost)
273 if (avcodec_open2(c, NULL, NULL) < 0) {
274 fprintf(stderr, "could not open codec\n");
278 /* Allocate the encoded raw picture. */
279 ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
281 fprintf(stderr, "Could not allocate picture\n");
285 /* If the output format is not YUV420P, then a temporary YUV420P
286 * picture is needed too. It is then converted to the required
288 ost->tmp_frame = NULL;
289 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
290 ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
291 if (!ost->tmp_frame) {
292 fprintf(stderr, "Could not allocate temporary picture\n");
298 /* Prepare a dummy image. */
299 static void fill_yuv_image(AVFrame *pict, int frame_index,
300 int width, int height)
304 /* when we pass a frame to the encoder, it may keep a reference to it
306 * make sure we do not overwrite it here
308 ret = av_frame_make_writable(pict);
315 for (y = 0; y < height; y++)
316 for (x = 0; x < width; x++)
317 pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
320 for (y = 0; y < height / 2; y++) {
321 for (x = 0; x < width / 2; x++) {
322 pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
323 pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
328 static void write_video_frame(AVFormatContext *oc, OutputStream *ost)
335 if (frame_count >= STREAM_NB_FRAMES) {
336 /* No more frames to compress. The codec has a latency of a few
337 * frames if using B-frames, so we get the last frames by
338 * passing the same picture again. */
340 if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
341 /* as we only generate a YUV420P picture, we must convert it
342 * to the codec pixel format if needed */
344 ost->sws_ctx = sws_getContext(c->width, c->height,
348 SCALE_FLAGS, NULL, NULL, NULL);
351 "Cannot initialize the conversion context\n");
355 fill_yuv_image(ost->tmp_frame, frame_count, c->width, c->height);
356 sws_scale(ost->sws_ctx, ost->tmp_frame->data, ost->tmp_frame->linesize,
357 0, c->height, ost->frame->data, ost->frame->linesize);
359 fill_yuv_image(ost->frame, frame_count, c->width, c->height);
363 if (oc->oformat->flags & AVFMT_RAWPICTURE) {
364 /* Raw video case - the API will change slightly in the near
365 * future for that. */
367 av_init_packet(&pkt);
369 pkt.flags |= AV_PKT_FLAG_KEY;
370 pkt.stream_index = ost->st->index;
371 pkt.data = (uint8_t *)ost->frame;
372 pkt.size = sizeof(AVPicture);
374 ret = av_interleaved_write_frame(oc, &pkt);
376 AVPacket pkt = { 0 };
378 av_init_packet(&pkt);
380 /* encode the image */
381 ret = avcodec_encode_video2(c, &pkt, ost->frame, &got_packet);
382 /* If size is zero, it means the image was buffered. */
383 if (!ret && got_packet && pkt.size) {
384 av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
385 pkt.stream_index = ost->st->index;
387 /* Write the compressed frame to the media file. */
388 ret = av_interleaved_write_frame(oc, &pkt);
394 fprintf(stderr, "Error while writing video frame\n");
400 static void close_stream(AVFormatContext *oc, OutputStream *ost)
402 avcodec_close(ost->st->codec);
403 av_frame_free(&ost->frame);
404 av_frame_free(&ost->tmp_frame);
405 sws_freeContext(ost->sws_ctx);
408 /**************************************************************/
409 /* media file output */
411 int main(int argc, char **argv)
413 OutputStream video_st = { 0 }, audio_st = { 0 };
414 const char *filename;
417 double audio_pts, video_pts;
418 int have_video = 0, have_audio = 0;
421 /* Initialize libavcodec, and register all codecs and formats. */
425 printf("usage: %s output_file\n"
426 "API example program to output a media file with libavformat.\n"
427 "The output format is automatically guessed according to the file extension.\n"
428 "Raw images can also be output by using '%%d' in the filename\n"
435 /* Autodetect the output format from the name. default is MPEG. */
436 fmt = av_guess_format(NULL, filename, NULL);
438 printf("Could not deduce output format from file extension: using MPEG.\n");
439 fmt = av_guess_format("mpeg", NULL, NULL);
442 fprintf(stderr, "Could not find suitable output format\n");
446 /* Allocate the output media context. */
447 oc = avformat_alloc_context();
449 fprintf(stderr, "Memory error\n");
453 snprintf(oc->filename, sizeof(oc->filename), "%s", filename);
455 /* Add the audio and video streams using the default format codecs
456 * and initialize the codecs. */
457 if (fmt->video_codec != AV_CODEC_ID_NONE) {
458 add_video_stream(&video_st, oc, fmt->video_codec);
461 if (fmt->audio_codec != AV_CODEC_ID_NONE) {
462 add_audio_stream(&audio_st, oc, fmt->audio_codec);
466 /* Now that all the parameters are set, we can open the audio and
467 * video codecs and allocate the necessary encode buffers. */
469 open_video(oc, &video_st);
471 open_audio(oc, &audio_st);
473 av_dump_format(oc, 0, filename, 1);
475 /* open the output file, if needed */
476 if (!(fmt->flags & AVFMT_NOFILE)) {
477 if (avio_open(&oc->pb, filename, AVIO_FLAG_WRITE) < 0) {
478 fprintf(stderr, "Could not open '%s'\n", filename);
483 /* Write the stream header, if any. */
484 avformat_write_header(oc, NULL);
487 /* Compute current audio and video time. */
489 audio_pts = (double)audio_st.st->pts.val * audio_st.st->time_base.num / audio_st.st->time_base.den;
494 video_pts = (double)video_st.st->pts.val * video_st.st->time_base.num /
495 video_st.st->time_base.den;
499 if ((!have_audio || audio_pts >= STREAM_DURATION) &&
500 (!have_video || video_pts >= STREAM_DURATION))
503 /* write interleaved audio and video frames */
504 if (!have_video || (have_video && have_audio && audio_pts < video_pts)) {
505 write_audio_frame(oc, &audio_st);
507 write_video_frame(oc, &video_st);
511 /* Write the trailer, if any. The trailer must be written before you
512 * close the CodecContexts open when you wrote the header; otherwise
513 * av_write_trailer() may try to use memory that was freed on
514 * av_codec_close(). */
515 av_write_trailer(oc);
517 /* Close each codec. */
519 close_stream(oc, &video_st);
521 close_stream(oc, &audio_st);
523 /* Free the streams. */
524 for (i = 0; i < oc->nb_streams; i++) {
525 av_freep(&oc->streams[i]->codec);
526 av_freep(&oc->streams[i]);
529 if (!(fmt->flags & AVFMT_NOFILE))
530 /* Close the output file. */
533 /* free the stream */