| 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 7200
|
|---|
| 47 | #define STREAM_FRAME_RATE 30 /* 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 | int samples_count;
|
|---|
| 59 |
|
|---|
| 60 | AVFrame *frame;
|
|---|
| 61 | AVFrame *tmp_frame;
|
|---|
| 62 |
|
|---|
| 63 | float t, tincr, tincr2;
|
|---|
| 64 |
|
|---|
| 65 | struct SwsContext *sws_ctx;
|
|---|
| 66 | struct SwrContext *swr_ctx;
|
|---|
| 67 | } OutputStream;
|
|---|
| 68 |
|
|---|
| 69 | static void log_packet(const AVFormatContext *fmt_ctx, const AVPacket *pkt)
|
|---|
| 70 | {
|
|---|
| 71 | AVRational *time_base = &fmt_ctx->streams[pkt->stream_index]->time_base;
|
|---|
| 72 | /*
|
|---|
| 73 | printf("pts:%s pts_time:%s dts:%s dts_time:%s duration:%s duration_time:%s stream_index:%d\n",
|
|---|
| 74 | av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, time_base),
|
|---|
| 75 | av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, time_base),
|
|---|
| 76 | av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, time_base),
|
|---|
| 77 | pkt->stream_index);
|
|---|
| 78 | */
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | static int write_frame(AVFormatContext *fmt_ctx, const AVRational *time_base, AVStream *st, AVPacket *pkt)
|
|---|
| 82 | {
|
|---|
| 83 | /* rescale output packet timestamp values from codec to stream timebase */
|
|---|
| 84 | av_packet_rescale_ts(pkt, *time_base, st->time_base);
|
|---|
| 85 | pkt->stream_index = st->index;
|
|---|
| 86 |
|
|---|
| 87 | /* Write the compressed frame to the media file. */
|
|---|
| 88 | log_packet(fmt_ctx, pkt);
|
|---|
| 89 | return av_interleaved_write_frame(fmt_ctx, pkt);
|
|---|
| 90 | }
|
|---|
| 91 |
|
|---|
| 92 | /* Add an output stream. */
|
|---|
| 93 | static void add_stream(OutputStream *ost, AVFormatContext *oc,
|
|---|
| 94 | AVCodec **codec,
|
|---|
| 95 | enum AVCodecID codec_id)
|
|---|
| 96 | {
|
|---|
| 97 | AVCodecContext *c;
|
|---|
| 98 | int i;
|
|---|
| 99 |
|
|---|
| 100 | /* find the encoder */
|
|---|
| 101 | *codec = avcodec_find_encoder(codec_id);
|
|---|
| 102 | if (!(*codec)) {
|
|---|
| 103 | fprintf(stderr, "Could not find encoder for '%s'\n",
|
|---|
| 104 | avcodec_get_name(codec_id));
|
|---|
| 105 | exit(1);
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | ost->st = avformat_new_stream(oc, *codec);
|
|---|
| 109 | if (!ost->st) {
|
|---|
| 110 | fprintf(stderr, "Could not allocate stream\n");
|
|---|
| 111 | exit(1);
|
|---|
| 112 | }
|
|---|
| 113 | ost->st->id = oc->nb_streams-1;
|
|---|
| 114 | c = ost->st->codec;
|
|---|
| 115 |
|
|---|
| 116 | switch ((*codec)->type) {
|
|---|
| 117 | case AVMEDIA_TYPE_AUDIO:
|
|---|
| 118 | c->sample_fmt = (*codec)->sample_fmts ?
|
|---|
| 119 | (*codec)->sample_fmts[0] : AV_SAMPLE_FMT_FLTP;
|
|---|
| 120 | c->bit_rate = 64000;
|
|---|
| 121 | c->sample_rate = 44100;
|
|---|
| 122 | if ((*codec)->supported_samplerates) {
|
|---|
| 123 | c->sample_rate = (*codec)->supported_samplerates[0];
|
|---|
| 124 | for (i = 0; (*codec)->supported_samplerates[i]; i++) {
|
|---|
| 125 | if ((*codec)->supported_samplerates[i] == 44100)
|
|---|
| 126 | c->sample_rate = 44100;
|
|---|
| 127 | }
|
|---|
| 128 | }
|
|---|
| 129 | c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
|
|---|
| 130 | c->channel_layout = AV_CH_LAYOUT_STEREO;
|
|---|
| 131 | if ((*codec)->channel_layouts) {
|
|---|
| 132 | c->channel_layout = (*codec)->channel_layouts[0];
|
|---|
| 133 | for (i = 0; (*codec)->channel_layouts[i]; i++) {
|
|---|
| 134 | if ((*codec)->channel_layouts[i] == AV_CH_LAYOUT_STEREO)
|
|---|
| 135 | c->channel_layout = AV_CH_LAYOUT_STEREO;
|
|---|
| 136 | }
|
|---|
| 137 | }
|
|---|
| 138 | c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
|
|---|
| 139 | ost->st->time_base = (AVRational){ 1, c->sample_rate };
|
|---|
| 140 | break;
|
|---|
| 141 |
|
|---|
| 142 | case AVMEDIA_TYPE_VIDEO:
|
|---|
| 143 | c->codec_id = codec_id;
|
|---|
| 144 |
|
|---|
| 145 | c->bit_rate = 400000;
|
|---|
| 146 | /* Resolution must be a multiple of two. */
|
|---|
| 147 | c->width = 640;
|
|---|
| 148 | c->height = 480;
|
|---|
| 149 | /* timebase: This is the fundamental unit of time (in seconds) in terms
|
|---|
| 150 | * of which frame timestamps are represented. For fixed-fps content,
|
|---|
| 151 | * timebase should be 1/framerate and timestamp increments should be
|
|---|
| 152 | * identical to 1. */
|
|---|
| 153 | ost->st->time_base = (AVRational){ 1, STREAM_FRAME_RATE };
|
|---|
| 154 | c->time_base = ost->st->time_base;
|
|---|
| 155 |
|
|---|
| 156 | c->gop_size = 12; /* emit one intra frame every twelve frames at most */
|
|---|
| 157 | c->pix_fmt = STREAM_PIX_FMT;
|
|---|
| 158 | if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
|
|---|
| 159 | /* just for testing, we also add B frames */
|
|---|
| 160 | c->max_b_frames = 2;
|
|---|
| 161 | }
|
|---|
| 162 | if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
|
|---|
| 163 | /* Needed to avoid using macroblocks in which some coeffs overflow.
|
|---|
| 164 | * This does not happen with normal video, it just happens here as
|
|---|
| 165 | * the motion of the chroma plane does not match the luma plane. */
|
|---|
| 166 | c->mb_decision = 2;
|
|---|
| 167 | }
|
|---|
| 168 | break;
|
|---|
| 169 |
|
|---|
| 170 | default:
|
|---|
| 171 | break;
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | /* Some formats want stream headers to be separate. */
|
|---|
| 175 | if (oc->oformat->flags & AVFMT_GLOBALHEADER)
|
|---|
| 176 | c->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|---|
| 177 | }
|
|---|
| 178 |
|
|---|
| 179 | /**************************************************************/
|
|---|
| 180 | /* audio output */
|
|---|
| 181 |
|
|---|
| 182 | static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
|
|---|
| 183 | uint64_t channel_layout,
|
|---|
| 184 | int sample_rate, int nb_samples)
|
|---|
| 185 | {
|
|---|
| 186 | AVFrame *frame = av_frame_alloc();
|
|---|
| 187 | int ret;
|
|---|
| 188 |
|
|---|
| 189 | if (!frame) {
|
|---|
| 190 | fprintf(stderr, "Error allocating an audio frame\n");
|
|---|
| 191 | exit(1);
|
|---|
| 192 | }
|
|---|
| 193 |
|
|---|
| 194 | frame->format = sample_fmt;
|
|---|
| 195 | frame->channel_layout = channel_layout;
|
|---|
| 196 | frame->sample_rate = sample_rate;
|
|---|
| 197 | frame->nb_samples = nb_samples;
|
|---|
| 198 |
|
|---|
| 199 | if (nb_samples) {
|
|---|
| 200 | ret = av_frame_get_buffer(frame, 0);
|
|---|
| 201 | if (ret < 0) {
|
|---|
| 202 | fprintf(stderr, "Error allocating an audio buffer\n");
|
|---|
| 203 | exit(1);
|
|---|
| 204 | }
|
|---|
| 205 | }
|
|---|
| 206 |
|
|---|
| 207 | return frame;
|
|---|
| 208 | }
|
|---|
| 209 |
|
|---|
| 210 | static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
|
|---|
| 211 | {
|
|---|
| 212 | AVCodecContext *c;
|
|---|
| 213 | int nb_samples;
|
|---|
| 214 | int ret;
|
|---|
| 215 | AVDictionary *opt = NULL;
|
|---|
| 216 |
|
|---|
| 217 | c = ost->st->codec;
|
|---|
| 218 |
|
|---|
| 219 | /* open it */
|
|---|
| 220 | av_dict_copy(&opt, opt_arg, 0);
|
|---|
| 221 | ret = avcodec_open2(c, codec, &opt);
|
|---|
| 222 | av_dict_free(&opt);
|
|---|
| 223 | if (ret < 0) {
|
|---|
| 224 | fprintf(stderr, "Could not open audio codec: %s\n", av_err2str(ret));
|
|---|
| 225 | exit(1);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | /* init signal generator */
|
|---|
| 229 | ost->t = 0;
|
|---|
| 230 | ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
|
|---|
| 231 | /* increment frequency by 110 Hz per second */
|
|---|
| 232 | ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
|
|---|
| 233 |
|
|---|
| 234 | if (c->codec->capabilities & AV_CODEC_CAP_VARIABLE_FRAME_SIZE)
|
|---|
| 235 | nb_samples = 10000;
|
|---|
| 236 | else
|
|---|
| 237 | nb_samples = c->frame_size;
|
|---|
| 238 |
|
|---|
| 239 | ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout,
|
|---|
| 240 | c->sample_rate, nb_samples);
|
|---|
| 241 | ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
|
|---|
| 242 | c->sample_rate, nb_samples);
|
|---|
| 243 |
|
|---|
| 244 | /* create resampler context */
|
|---|
| 245 | ost->swr_ctx = swr_alloc();
|
|---|
| 246 | if (!ost->swr_ctx) {
|
|---|
| 247 | fprintf(stderr, "Could not allocate resampler context\n");
|
|---|
| 248 | exit(1);
|
|---|
| 249 | }
|
|---|
| 250 |
|
|---|
| 251 | /* set options */
|
|---|
| 252 | av_opt_set_int (ost->swr_ctx, "in_channel_count", c->channels, 0);
|
|---|
| 253 | av_opt_set_int (ost->swr_ctx, "in_sample_rate", c->sample_rate, 0);
|
|---|
| 254 | av_opt_set_sample_fmt(ost->swr_ctx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
|
|---|
| 255 | av_opt_set_int (ost->swr_ctx, "out_channel_count", c->channels, 0);
|
|---|
| 256 | av_opt_set_int (ost->swr_ctx, "out_sample_rate", c->sample_rate, 0);
|
|---|
| 257 | av_opt_set_sample_fmt(ost->swr_ctx, "out_sample_fmt", c->sample_fmt, 0);
|
|---|
| 258 |
|
|---|
| 259 | /* initialize the resampling context */
|
|---|
| 260 | if ((ret = swr_init(ost->swr_ctx)) < 0) {
|
|---|
| 261 | fprintf(stderr, "Failed to initialize the resampling context\n");
|
|---|
| 262 | exit(1);
|
|---|
| 263 | }
|
|---|
| 264 | }
|
|---|
| 265 |
|
|---|
| 266 | /* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
|
|---|
| 267 | * 'nb_channels' channels. */
|
|---|
| 268 | static AVFrame *get_audio_frame(OutputStream *ost)
|
|---|
| 269 | {
|
|---|
| 270 | AVFrame *frame = ost->tmp_frame;
|
|---|
| 271 | int j, i, v;
|
|---|
| 272 | int16_t *q = (int16_t*)frame->data[0];
|
|---|
| 273 |
|
|---|
| 274 | /* check if we want to generate more frames */
|
|---|
| 275 | if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
|
|---|
| 276 | STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
|
|---|
| 277 | return NULL;
|
|---|
| 278 |
|
|---|
| 279 | for (j = 0; j <frame->nb_samples; j++) {
|
|---|
| 280 | v = (int)(sin(ost->t) * 10000);
|
|---|
| 281 | for (i = 0; i < ost->st->codec->channels; i++)
|
|---|
| 282 | *q++ = v;
|
|---|
| 283 | ost->t += ost->tincr;
|
|---|
| 284 | ost->tincr += ost->tincr2;
|
|---|
| 285 | }
|
|---|
| 286 |
|
|---|
| 287 | frame->pts = ost->next_pts;
|
|---|
| 288 | ost->next_pts += frame->nb_samples;
|
|---|
| 289 |
|
|---|
| 290 | return frame;
|
|---|
| 291 | }
|
|---|
| 292 |
|
|---|
| 293 | /*
|
|---|
| 294 | * encode one audio frame and send it to the muxer
|
|---|
| 295 | * return 1 when encoding is finished, 0 otherwise
|
|---|
| 296 | */
|
|---|
| 297 | static int write_audio_frame(AVFormatContext *oc, OutputStream *ost)
|
|---|
| 298 | {
|
|---|
| 299 | AVCodecContext *c;
|
|---|
| 300 | AVPacket pkt = { 0 }; // data and size must be 0;
|
|---|
| 301 | AVFrame *frame;
|
|---|
| 302 | int ret;
|
|---|
| 303 | int got_packet;
|
|---|
| 304 | int dst_nb_samples;
|
|---|
| 305 |
|
|---|
| 306 | av_init_packet(&pkt);
|
|---|
| 307 | c = ost->st->codec;
|
|---|
| 308 |
|
|---|
| 309 | frame = get_audio_frame(ost);
|
|---|
| 310 |
|
|---|
| 311 | if (frame) {
|
|---|
| 312 | /* convert samples from native format to destination codec format, using the resampler */
|
|---|
| 313 | /* compute destination number of samples */
|
|---|
| 314 | dst_nb_samples = av_rescale_rnd(swr_get_delay(ost->swr_ctx, c->sample_rate) + frame->nb_samples,
|
|---|
| 315 | c->sample_rate, c->sample_rate, AV_ROUND_UP);
|
|---|
| 316 | av_assert0(dst_nb_samples == frame->nb_samples);
|
|---|
| 317 |
|
|---|
| 318 | /* when we pass a frame to the encoder, it may keep a reference to it
|
|---|
| 319 | * internally;
|
|---|
| 320 | * make sure we do not overwrite it here
|
|---|
| 321 | */
|
|---|
| 322 | ret = av_frame_make_writable(ost->frame);
|
|---|
| 323 | if (ret < 0)
|
|---|
| 324 | exit(1);
|
|---|
| 325 |
|
|---|
| 326 | /* convert to destination format */
|
|---|
| 327 | ret = swr_convert(ost->swr_ctx,
|
|---|
| 328 | ost->frame->data, dst_nb_samples,
|
|---|
| 329 | (const uint8_t **)frame->data, frame->nb_samples);
|
|---|
| 330 | if (ret < 0) {
|
|---|
| 331 | fprintf(stderr, "Error while converting\n");
|
|---|
| 332 | exit(1);
|
|---|
| 333 | }
|
|---|
| 334 | frame = ost->frame;
|
|---|
| 335 |
|
|---|
| 336 | frame->pts = av_rescale_q(ost->samples_count, (AVRational){1, c->sample_rate}, c->time_base);
|
|---|
| 337 | ost->samples_count += dst_nb_samples;
|
|---|
| 338 | }
|
|---|
| 339 |
|
|---|
| 340 | ret = avcodec_encode_audio2(c, &pkt, frame, &got_packet);
|
|---|
| 341 | if (ret < 0) {
|
|---|
| 342 | fprintf(stderr, "Error encoding audio frame: %s\n", av_err2str(ret));
|
|---|
| 343 | exit(1);
|
|---|
| 344 | }
|
|---|
| 345 |
|
|---|
| 346 | if (got_packet) {
|
|---|
| 347 | ret = write_frame(oc, &c->time_base, ost->st, &pkt);
|
|---|
| 348 | if (ret < 0) {
|
|---|
| 349 | fprintf(stderr, "Error while writing audio frame: %s\n",
|
|---|
| 350 | av_err2str(ret));
|
|---|
| 351 | exit(1);
|
|---|
| 352 | }
|
|---|
| 353 | }
|
|---|
| 354 |
|
|---|
| 355 | return (frame || got_packet) ? 0 : 1;
|
|---|
| 356 | }
|
|---|
| 357 |
|
|---|
| 358 | /**************************************************************/
|
|---|
| 359 | /* video output */
|
|---|
| 360 |
|
|---|
| 361 | static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
|
|---|
| 362 | {
|
|---|
| 363 | AVFrame *picture;
|
|---|
| 364 | int ret;
|
|---|
| 365 |
|
|---|
| 366 | picture = av_frame_alloc();
|
|---|
| 367 | if (!picture)
|
|---|
| 368 | return NULL;
|
|---|
| 369 |
|
|---|
| 370 | picture->format = pix_fmt;
|
|---|
| 371 | picture->width = width;
|
|---|
| 372 | picture->height = height;
|
|---|
| 373 |
|
|---|
| 374 | /* allocate the buffers for the frame data */
|
|---|
| 375 | ret = av_frame_get_buffer(picture, 32);
|
|---|
| 376 | if (ret < 0) {
|
|---|
| 377 | fprintf(stderr, "Could not allocate frame data.\n");
|
|---|
| 378 | exit(1);
|
|---|
| 379 | }
|
|---|
| 380 |
|
|---|
| 381 | return picture;
|
|---|
| 382 | }
|
|---|
| 383 |
|
|---|
| 384 | static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
|
|---|
| 385 | {
|
|---|
| 386 | int ret;
|
|---|
| 387 | AVCodecContext *c = ost->st->codec;
|
|---|
| 388 | AVDictionary *opt = NULL;
|
|---|
| 389 |
|
|---|
| 390 | av_dict_copy(&opt, opt_arg, 0);
|
|---|
| 391 |
|
|---|
| 392 | /* open the codec */
|
|---|
| 393 | ret = avcodec_open2(c, codec, &opt);
|
|---|
| 394 | av_dict_free(&opt);
|
|---|
| 395 | if (ret < 0) {
|
|---|
| 396 | fprintf(stderr, "Could not open video codec: %s\n", av_err2str(ret));
|
|---|
| 397 | exit(1);
|
|---|
| 398 | }
|
|---|
| 399 |
|
|---|
| 400 | /* allocate and init a re-usable frame */
|
|---|
| 401 | ost->frame = alloc_picture(c->pix_fmt, c->width, c->height);
|
|---|
| 402 | if (!ost->frame) {
|
|---|
| 403 | fprintf(stderr, "Could not allocate video frame\n");
|
|---|
| 404 | exit(1);
|
|---|
| 405 | }
|
|---|
| 406 |
|
|---|
| 407 | /* If the output format is not YUV420P, then a temporary YUV420P
|
|---|
| 408 | * picture is needed too. It is then converted to the required
|
|---|
| 409 | * output format. */
|
|---|
| 410 | ost->tmp_frame = NULL;
|
|---|
| 411 | if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
|
|---|
| 412 | ost->tmp_frame = alloc_picture(AV_PIX_FMT_YUV420P, c->width, c->height);
|
|---|
| 413 | if (!ost->tmp_frame) {
|
|---|
| 414 | fprintf(stderr, "Could not allocate temporary picture\n");
|
|---|
| 415 | exit(1);
|
|---|
| 416 | }
|
|---|
| 417 | }
|
|---|
| 418 | }
|
|---|
| 419 |
|
|---|
| 420 | /* Prepare a dummy image. */
|
|---|
| 421 | static void fill_yuv_image(AVFrame *pict, int frame_index,
|
|---|
| 422 | int width, int height)
|
|---|
| 423 | {
|
|---|
| 424 | int x, y, i, ret;
|
|---|
| 425 |
|
|---|
| 426 | /* when we pass a frame to the encoder, it may keep a reference to it
|
|---|
| 427 | * internally;
|
|---|
| 428 | * make sure we do not overwrite it here
|
|---|
| 429 | */
|
|---|
| 430 | ret = av_frame_make_writable(pict);
|
|---|
| 431 | if (ret < 0)
|
|---|
| 432 | exit(1);
|
|---|
| 433 |
|
|---|
| 434 | i = frame_index;
|
|---|
| 435 |
|
|---|
| 436 | /* Y */
|
|---|
| 437 | for (y = 0; y < height; y++)
|
|---|
| 438 | for (x = 0; x < width; x++)
|
|---|
| 439 | pict->data[0][y * pict->linesize[0] + x] = x + y + i * 3;
|
|---|
| 440 |
|
|---|
| 441 | /* Cb and Cr */
|
|---|
| 442 | for (y = 0; y < height / 2; y++) {
|
|---|
| 443 | for (x = 0; x < width / 2; x++) {
|
|---|
| 444 | pict->data[1][y * pict->linesize[1] + x] = 128 + y + i * 2;
|
|---|
| 445 | pict->data[2][y * pict->linesize[2] + x] = 64 + x + i * 5;
|
|---|
| 446 | }
|
|---|
| 447 | }
|
|---|
| 448 | }
|
|---|
| 449 |
|
|---|
| 450 | static AVFrame *get_video_frame(OutputStream *ost)
|
|---|
| 451 | {
|
|---|
| 452 | AVCodecContext *c = ost->st->codec;
|
|---|
| 453 |
|
|---|
| 454 | /* check if we want to generate more frames */
|
|---|
| 455 | if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
|
|---|
| 456 | STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
|
|---|
| 457 | return NULL;
|
|---|
| 458 |
|
|---|
| 459 | if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
|
|---|
| 460 | /* as we only generate a YUV420P picture, we must convert it
|
|---|
| 461 | * to the codec pixel format if needed */
|
|---|
| 462 | if (!ost->sws_ctx) {
|
|---|
| 463 | ost->sws_ctx = sws_getContext(c->width, c->height,
|
|---|
| 464 | AV_PIX_FMT_YUV420P,
|
|---|
| 465 | c->width, c->height,
|
|---|
| 466 | c->pix_fmt,
|
|---|
| 467 | SCALE_FLAGS, NULL, NULL, NULL);
|
|---|
| 468 | if (!ost->sws_ctx) {
|
|---|
| 469 | fprintf(stderr,
|
|---|
| 470 | "Could not initialize the conversion context\n");
|
|---|
| 471 | exit(1);
|
|---|
| 472 | }
|
|---|
| 473 | }
|
|---|
| 474 | fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
|
|---|
| 475 | sws_scale(ost->sws_ctx,
|
|---|
| 476 | (const uint8_t * const *)ost->tmp_frame->data, ost->tmp_frame->linesize,
|
|---|
| 477 | 0, c->height, ost->frame->data, ost->frame->linesize);
|
|---|
| 478 | } else {
|
|---|
| 479 | fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
|
|---|
| 480 | }
|
|---|
| 481 |
|
|---|
| 482 | ost->frame->pts = ost->next_pts++;
|
|---|
| 483 |
|
|---|
| 484 | return ost->frame;
|
|---|
| 485 | }
|
|---|
| 486 |
|
|---|
| 487 | /*
|
|---|
| 488 | * encode one video frame and send it to the muxer
|
|---|
| 489 | * return 1 when encoding is finished, 0 otherwise
|
|---|
| 490 | */
|
|---|
| 491 | static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
|
|---|
| 492 | {
|
|---|
| 493 | int ret;
|
|---|
| 494 | AVCodecContext *c;
|
|---|
| 495 | AVFrame *frame;
|
|---|
| 496 | int got_packet = 0;
|
|---|
| 497 | AVPacket pkt = { 0 };
|
|---|
| 498 |
|
|---|
| 499 | c = ost->st->codec;
|
|---|
| 500 |
|
|---|
| 501 | frame = get_video_frame(ost);
|
|---|
| 502 |
|
|---|
| 503 | av_init_packet(&pkt);
|
|---|
| 504 |
|
|---|
| 505 | /* encode the image */
|
|---|
| 506 | ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
|
|---|
| 507 | if (ret < 0) {
|
|---|
| 508 | fprintf(stderr, "Error encoding video frame: %s\n", av_err2str(ret));
|
|---|
| 509 | exit(1);
|
|---|
| 510 | }
|
|---|
| 511 |
|
|---|
| 512 | if (got_packet) {
|
|---|
| 513 | ret = write_frame(oc, &c->time_base, ost->st, &pkt);
|
|---|
| 514 | } else {
|
|---|
| 515 | ret = 0;
|
|---|
| 516 | }
|
|---|
| 517 |
|
|---|
| 518 | if (ret < 0) {
|
|---|
| 519 | fprintf(stderr, "Error while writing video frame: %s\n", av_err2str(ret));
|
|---|
| 520 | exit(1);
|
|---|
| 521 | }
|
|---|
| 522 |
|
|---|
| 523 | return (frame || got_packet) ? 0 : 1;
|
|---|
| 524 | }
|
|---|
| 525 |
|
|---|
| 526 | static void close_stream(AVFormatContext *oc, OutputStream *ost)
|
|---|
| 527 | {
|
|---|
| 528 | avcodec_close(ost->st->codec);
|
|---|
| 529 | av_frame_free(&ost->frame);
|
|---|
| 530 | av_frame_free(&ost->tmp_frame);
|
|---|
| 531 | sws_freeContext(ost->sws_ctx);
|
|---|
| 532 | swr_free(&ost->swr_ctx);
|
|---|
| 533 | }
|
|---|
| 534 |
|
|---|
| 535 | /**************************************************************/
|
|---|
| 536 | /* media file output */
|
|---|
| 537 |
|
|---|
| 538 | int main(int argc, char **argv)
|
|---|
| 539 | {
|
|---|
| 540 | OutputStream video_st = { 0 }, audio_st = { 0 };
|
|---|
| 541 | const char *filename;
|
|---|
| 542 | AVOutputFormat *fmt;
|
|---|
| 543 | AVFormatContext *oc;
|
|---|
| 544 | AVCodec *audio_codec, *video_codec;
|
|---|
| 545 | int ret;
|
|---|
| 546 | int have_video = 0, have_audio = 0;
|
|---|
| 547 | int encode_video = 0, encode_audio = 0;
|
|---|
| 548 | AVDictionary *opt = NULL;
|
|---|
| 549 |
|
|---|
| 550 | /* Initialize libavcodec, and register all codecs and formats. */
|
|---|
| 551 | av_register_all();
|
|---|
| 552 |
|
|---|
| 553 | if (argc < 2) {
|
|---|
| 554 | printf("usage: %s output_file\n"
|
|---|
| 555 | "API example program to output a media file with libavformat.\n"
|
|---|
| 556 | "This program generates a synthetic audio and video stream, encodes and\n"
|
|---|
| 557 | "muxes them into a file named output_file.\n"
|
|---|
| 558 | "The output format is automatically guessed according to the file extension.\n"
|
|---|
| 559 | "Raw images can also be output by using '%%d' in the filename.\n"
|
|---|
| 560 | "\n", argv[0]);
|
|---|
| 561 | return 1;
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | filename = argv[1];
|
|---|
| 565 | if (argc > 3 && !strcmp(argv[2], "-flags")) {
|
|---|
| 566 | av_dict_set(&opt, argv[2]+1, argv[3], 0);
|
|---|
| 567 | }
|
|---|
| 568 |
|
|---|
| 569 | /* allocate the output media context */
|
|---|
| 570 | avformat_alloc_output_context2(&oc, NULL, NULL, filename);
|
|---|
| 571 | if (!oc) {
|
|---|
| 572 | printf("Could not deduce output format from file extension: using MPEG.\n");
|
|---|
| 573 | avformat_alloc_output_context2(&oc, NULL, "mpeg", filename);
|
|---|
| 574 | }
|
|---|
| 575 | if (!oc)
|
|---|
| 576 | return 1;
|
|---|
| 577 |
|
|---|
| 578 | fmt = oc->oformat;
|
|---|
| 579 |
|
|---|
| 580 | /* Add the audio and video streams using the default format codecs
|
|---|
| 581 | * and initialize the codecs. */
|
|---|
| 582 | if (fmt->video_codec != AV_CODEC_ID_NONE) {
|
|---|
| 583 | add_stream(&video_st, oc, &video_codec, fmt->video_codec);
|
|---|
| 584 | have_video = 1;
|
|---|
| 585 | encode_video = 1;
|
|---|
| 586 | }
|
|---|
| 587 | if (fmt->audio_codec != AV_CODEC_ID_NONE) {
|
|---|
| 588 | add_stream(&audio_st, oc, &audio_codec, fmt->audio_codec);
|
|---|
| 589 | have_audio = 1;
|
|---|
| 590 | encode_audio = 1;
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | /* Now that all the parameters are set, we can open the audio and
|
|---|
| 594 | * video codecs and allocate the necessary encode buffers. */
|
|---|
| 595 | if (have_video)
|
|---|
| 596 | open_video(oc, video_codec, &video_st, opt);
|
|---|
| 597 |
|
|---|
| 598 | if (have_audio)
|
|---|
| 599 | open_audio(oc, audio_codec, &audio_st, opt);
|
|---|
| 600 |
|
|---|
| 601 | av_dump_format(oc, 0, filename, 1);
|
|---|
| 602 |
|
|---|
| 603 | /* open the output file, if needed */
|
|---|
| 604 | if (!(fmt->flags & AVFMT_NOFILE)) {
|
|---|
| 605 | ret = avio_open(&oc->pb, filename, AVIO_FLAG_WRITE);
|
|---|
| 606 | if (ret < 0) {
|
|---|
| 607 | fprintf(stderr, "Could not open '%s': %s\n", filename,
|
|---|
| 608 | av_err2str(ret));
|
|---|
| 609 | return 1;
|
|---|
| 610 | }
|
|---|
| 611 | }
|
|---|
| 612 |
|
|---|
| 613 | /* Write the stream header, if any. */
|
|---|
| 614 | ret = avformat_write_header(oc, &opt);
|
|---|
| 615 | if (ret < 0) {
|
|---|
| 616 | fprintf(stderr, "Error occurred when opening output file: %s\n",
|
|---|
| 617 | av_err2str(ret));
|
|---|
| 618 | return 1;
|
|---|
| 619 | }
|
|---|
| 620 |
|
|---|
| 621 | while (encode_video || encode_audio) {
|
|---|
| 622 | /* select the stream to encode */
|
|---|
| 623 | if (encode_video &&
|
|---|
| 624 | (!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
|
|---|
| 625 | audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
|
|---|
| 626 | encode_video = !write_video_frame(oc, &video_st);
|
|---|
| 627 | } else {
|
|---|
| 628 | encode_audio = !write_audio_frame(oc, &audio_st);
|
|---|
| 629 | }
|
|---|
| 630 | }
|
|---|
| 631 |
|
|---|
| 632 | /* Write the trailer, if any. The trailer must be written before you
|
|---|
| 633 | * close the CodecContexts open when you wrote the header; otherwise
|
|---|
| 634 | * av_write_trailer() may try to use memory that was freed on
|
|---|
| 635 | * av_codec_close(). */
|
|---|
| 636 | av_write_trailer(oc);
|
|---|
| 637 |
|
|---|
| 638 | /* Close each codec. */
|
|---|
| 639 | if (have_video)
|
|---|
| 640 | close_stream(oc, &video_st);
|
|---|
| 641 | if (have_audio)
|
|---|
| 642 | close_stream(oc, &audio_st);
|
|---|
| 643 |
|
|---|
| 644 | if (!(fmt->flags & AVFMT_NOFILE))
|
|---|
| 645 | /* Close the output file. */
|
|---|
| 646 | avio_closep(&oc->pb);
|
|---|
| 647 |
|
|---|
| 648 | /* free the stream */
|
|---|
| 649 | avformat_free_context(oc);
|
|---|
| 650 |
|
|---|
| 651 | return 0;
|
|---|
| 652 | }
|
|---|