]> git.sesse.net Git - ffmpeg/blob - doc/examples/muxing.c
doc/examples/muxing: remove unused variable
[ffmpeg] / doc / examples / muxing.c
1 /*
2  * Copyright (c) 2003 Fabrice Bellard
3  *
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:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
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
20  * THE SOFTWARE.
21  */
22
23 /**
24  * @file
25  * libavformat API example.
26  *
27  * Output a media file in any supported libavformat format. The default
28  * codecs are used.
29  * @example muxing.c
30  */
31
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <string.h>
35 #include <math.h>
36
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>
45
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 */
49
50 #define SCALE_FLAGS SWS_BICUBIC
51
52 // a wrapper around a single output AVStream
53 typedef struct OutputStream {
54     AVStream *st;
55
56     /* pts of the next frame that will be generated */
57     int64_t next_pts;
58
59     AVFrame *frame;
60     AVFrame *tmp_frame;
61
62     float t, tincr, tincr2;
63
64     struct SwsContext *sws_ctx;
65 } OutputStream;
66
67 static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
68 {
69     AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
70
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),
75            pkt->stream_index);
76 }
77
78 static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
79 {
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;
83
84     /* Write the compressed frame to the media file. */
85     log_packet(fmt_ctx, pkt);
86     return av_interleaved_write_frame(fmt_ctx, pkt);
87 }
88
89 /* Add an output stream. */
90 static void add_stream(OutputStream *ost, AVFormatContext *oc,
91                        AVCodec **codec,
92                        enum AVCodecID codec_id)
93 {
94     AVCodecContext *c;
95
96     /* find the encoder */
97     *codec = avcodec_find_encoder(codec_id);
98     if (!(*codec)) {
99         fprintf(stderr, "Could not find encoder for '%s'\n",
100                 avcodec_get_name(codec_id));
101         exit(1);
102     }
103
104     ost->st = avformat_new_stream(oc, *codec);
105     if (!ost->st) {
106         fprintf(stderr, "Could not allocate stream\n");
107         exit(1);
108     }
109     ost->st->id = oc->nb_streams-1;
110     c = ost->st->codec;
111
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;
116         c->bit_rate    = 64000;
117         c->sample_rate = 44100;
118         c->channels    = 2;
119         c->channel_layout = AV_CH_LAYOUT_STEREO;
120         break;
121
122     case AVMEDIA_TYPE_VIDEO:
123         c->codec_id = codec_id;
124
125         c->bit_rate = 400000;
126         /* Resolution must be a multiple of two. */
127         c->width    = 352;
128         c->height   = 288;
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
132          * identical to 1. */
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 */
139             c->max_b_frames = 2;
140         }
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. */
145             c->mb_decision = 2;
146         }
147     break;
148
149     default:
150         break;
151     }
152
153     /* Some formats want stream headers to be separate. */
154     if (oc->oformat->flags & AVFMT_GLOBALHEADER)
155         c->flags |= CODEC_FLAG_GLOBAL_HEADER;
156 }
157
158 /**************************************************************/
159 /* audio output */
160
161 int samples_count;
162
163 struct SwrContext *swr_ctx = NULL;
164
165 static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
166 {
167     AVCodecContext *c;
168     int ret;
169
170     c = ost->st->codec;
171
172     /* open it */
173     ret = avcodec_open2(c, codec, NULL);
174     if (ret < 0) {
175         fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
176         exit(1);
177     }
178
179     /* init signal generator */
180     ost->t     = 0;
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;
184
185     ost->frame = av_frame_alloc();
186     if (!ost->frame)
187         exit(1);
188
189     ost->frame->sample_rate    = c->sample_rate;
190     ost->frame->format         = AV_SAMPLE_FMT_S16;
191     ost->frame->channel_layout = c->channel_layout;
192
193     if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
194         ost->frame->nb_samples = 10000;
195     else
196         ost->frame->nb_samples = c->frame_size;
197
198     ost->tmp_frame = av_frame_alloc();
199     if (!ost->frame)
200         exit(1);
201
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;
206
207     /* create resampler context */
208     if (c->sample_fmt != AV_SAMPLE_FMT_S16) {
209         swr_ctx = swr_alloc();
210         if (!swr_ctx) {
211             fprintf(stderr, "Could not allocate resampler context\n");
212             exit(1);
213         }
214
215         /* set options */
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);
222
223         /* initialize the resampling context */
224         if ((ret = swr_init(swr_ctx)) < 0) {
225             fprintf(stderr, "Failed to initialize the resampling context\n");
226             exit(1);
227         }
228     }
229
230     ret = av_frame_get_buffer(ost->frame, 0);
231     if (ret < 0) {
232         fprintf(stderr, "Could not allocate an audio frame.\n");
233         exit(1);
234     }
235     ret = av_frame_get_buffer(ost->tmp_frame, 0);
236     if (ret < 0) {
237         fprintf(stderr, "Could not allocate an audio frame.\n");
238         exit(1);
239     }
240 }
241
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)
245 {
246     int j, i, v, ret;
247     int16_t *q = (int16_t*)ost->frame->data[0];
248
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)
252         return NULL;
253
254     /* when we pass a frame to the encoder, it may keep a reference to it
255      * internally;
256      * make sure we do not overwrite it here
257      */
258     ret = av_frame_make_writable(ost->frame);
259     if (ret < 0)
260         exit(1);
261
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++)
265             *q++ = v;
266         ost->t     += ost->tincr;
267         ost->tincr += ost->tincr2;
268     }
269
270     ost->frame->pts = ost->next_pts;
271     ost->next_pts  += ost->frame->nb_samples;
272
273     return ost->frame;
274 }
275
276 /*
277  * encode one audio frame and send it to the muxer
278  * return 1 when encoding is finished, 0 otherwise
279  */
280 static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
281 {
282     AVCodecContext *c;
283     AVPacket pkt = { 0 }; // data and size must be 0;
284     AVFrame *frame;
285     int ret;
286     int got_packet;
287     int dst_nb_samples;
288
289     av_init_packet(&pkt);
290     c = ost->st->codec;
291
292     frame = get_audio_frame(ost);
293
294     if (frame) {
295         /* convert samples from native format to destination codec format, using the resampler */
296         if (swr_ctx) {
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);
301
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);
306             if (ret < 0) {
307                 fprintf(stderr, "Error while converting\n");
308                 exit(1);
309             }
310             frame = ost->tmp_frame;
311         } else {
312             dst_nb_samples = frame->nb_samples;
313         }
314
315         frame->pts = av_rescale_q(samples_count, (AVRational){1, c->sample_rate}, c->time_base);
316         samples_count += dst_nb_samples;
317     }
318
319     ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
320     if (ret < 0) {
321         fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
322         exit(1);
323     }
324
325     if (got_packet) {
326         ret = write_frame(oc, &c->time_base, ost->st, &pkt);
327         if (ret < 0) {
328             fprintf(stderr, "Error while writing audio frame: %s\n",
329                     av_err2str(ret));
330             exit(1);
331         }
332     }
333
334     return (frame || got_packet) ? 0 : 1;
335 }
336
337 /**************************************************************/
338 /* video output */
339
340 static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
341 {
342     AVFrame *picture;
343     int ret;
344
345     picture = av_frame_alloc();
346     if (!picture)
347         return NULL;
348
349     picture->format = pix_fmt;
350     picture->width  = width;
351     picture->height = height;
352
353     /* allocate the buffers for the frame data */
354     ret = av_frame_get_buffer(picture, 32);
355     if (ret < 0) {
356         fprintf(stderr, "Could not allocate frame data.\n");
357         exit(1);
358     }
359
360     return picture;
361 }
362
363 static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost)
364 {
365     int ret;
366     AVCodecContext *c = ost->st->codec;
367
368     /* open the codec */
369     ret = avcodec_open2(c, codec, NULL);
370     if (ret < 0) {
371         fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
372         exit(1);
373     }
374
375     /* allocate and init a re-usable frame */
376     ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
377     if (!ost->frame) {
378         fprintf(stderr, "Could not allocate video frame\n");
379         exit(1);
380     }
381
382     /* If the output format is not YUV420P, then a temporary YUV420P
383      * picture is needed too. It is then converted to the required
384      * output format. */
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");
390             exit(1);
391         }
392     }
393 }
394
395 /* Prepare a dummy image. */
396 static void fill_yuv_image(AVFrame *pict, int frame_index,
397                            int width, int height)
398 {
399     int x, y, i, ret;
400
401     /* when we pass a frame to the encoder, it may keep a reference to it
402      * internally;
403      * make sure we do not overwrite it here
404      */
405     ret = av_frame_make_writable(pict);
406     if (ret < 0)
407         exit(1);
408
409     i = frame_index;
410
411     /* Y */
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;
415
416     /* Cb and Cr */
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;
421         }
422     }
423 }
424
425 static AVFrame *get_video_frame(OutputStream *ost)
426 {
427     AVCodecContext *c = ost->st->codec;
428
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)
432         return NULL;
433
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 */
437         if (!ost->sws_ctx) {
438             ost->sws_ctx = sws_getContext(c->width, c->height,
439                                           AV_PIX_FMT_YUV420P,
440                                           c->width, c->height,
441                                           c->pix_fmt,
442                                           SCALE_FLAGS, NULL, NULL, NULL);
443             if (!ost->sws_ctx) {
444                 fprintf(stderr,
445                         "Could not initialize the conversion context\n");
446                 exit(1);
447             }
448         }
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);
453     } else {
454         fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
455     }
456
457     ost->frame->pts = ost->next_pts++;
458
459     return ost->frame;
460 }
461
462 /*
463  * encode one video frame and send it to the muxer
464  * return 1 when encoding is finished, 0 otherwise
465  */
466 static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
467 {
468     int ret;
469     AVCodecContext *c;
470     AVFrame *frame;
471     int got_packet = 0;
472
473     c = ost->st->codec;
474
475     frame = get_video_frame(ost);
476
477     if (oc->oformat->flags & AVFMT_RAWPICTURE) {
478         /* a hack to avoid data copy with some raw video muxers */
479         AVPacket pkt;
480         av_init_packet(&pkt);
481
482         if (!frame)
483             return 1;
484
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);
489
490         pkt.pts = pkt.dts = frame->pts;
491         av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
492
493         ret = av_interleaved_write_frame(oc, &pkt);
494     } else {
495         AVPacket pkt = { 0 };
496         av_init_packet(&pkt);
497
498         /* encode the image */
499         ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
500         if (ret < 0) {
501             fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
502             exit(1);
503         }
504
505         if (got_packet) {
506             ret = write_frame(oc, &c->time_base, ost->st, &pkt);
507         } else {
508             ret = 0;
509         }
510     }
511
512     if (ret < 0) {
513         fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
514         exit(1);
515     }
516
517     return (frame || got_packet) ? 0 : 1;
518 }
519
520 static void close_stream(AVFormatContext *oc, OutputStream *ost)
521 {
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);
526 }
527
528 /**************************************************************/
529 /* media file output */
530
531 int main(int argc, char **argv)
532 {
533     OutputStream video_st = { 0 }, audio_st = { 0 };
534     const char *filename;
535     AVOutputFormat *fmt;
536     AVFormatContext *oc;
537     AVCodec *audio_codec, *video_codec;
538     int ret;
539     int have_video = 0, have_audio = 0;
540     int encode_video = 0, encode_audio = 0;
541
542     /* Initialize libavcodec, and register all codecs and formats. */
543     av_register_all();
544
545     if (argc != 2) {
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"
552                "\n", argv[0]);
553         return 1;
554     }
555
556     filename = argv[1];
557
558     /* allocate the output media context */
559     avformat_alloc_output_context2(&oc, NULL, NULL, filename);
560     if (!oc) {
561         printf("Could not deduce output format from file extension: using MPEG.\n");
562         avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
563     }
564     if (!oc)
565         return 1;
566
567     fmt = oc->oformat;
568
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);
573         have_video = 1;
574         encode_video = 1;
575     }
576     if (fmt->audio_codec != AV_CODEC_ID_NONE) {
577         add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
578         have_audio = 1;
579         encode_audio = 1;
580     }
581
582     /* Now that all the parameters are set, we can open the audio and
583      * video codecs and allocate the necessary encode buffers. */
584     if (have_video)
585         open_video(oc, video_codec, &video_st);
586
587     if (have_audio)
588         open_audio(oc, audio_codec, &audio_st);
589
590     av_dump_format(oc, 0, filename, 1);
591
592     /* open the output file, if needed */
593     if (!(fmt->flags & AVFMT_NOFILE)) {
594         ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
595         if (ret < 0) {
596             fprintf(stderr, "Could not open '%s': %s\n", filename,
597                     av_err2str(ret));
598             return 1;
599         }
600     }
601
602     /* Write the stream header, if any. */
603     ret = avformat_write_header(oc, NULL);
604     if (ret < 0) {
605         fprintf(stderr, "Error occurred when opening output file: %s\n",
606                 av_err2str(ret));
607         return 1;
608     }
609
610     while (encode_video || encode_audio) {
611         /* select the stream to encode */
612         if (encode_video &&
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);
616         } else {
617             encode_audio = !write_audio_frame(oc, &audio_st);
618         }
619     }
620
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);
626
627     /* Close each codec. */
628     if (have_video)
629         close_stream(oc, &video_st);
630     if (have_audio)
631         close_stream(oc, &audio_st);
632
633     if (!(fmt->flags & AVFMT_NOFILE))
634         /* Close the output file. */
635         avio_close(oc->pb);
636
637     /* free the stream */
638     avformat_free_context(oc);
639
640     return 0;
641 }