72
st = avformat_new_stream(oc, codec);
88
ost->st = avformat_new_stream(oc, codec);
74
90
fprintf(stderr, "Could not alloc stream\n");
80
96
/* put sample parameters */
81
c->sample_fmt = AV_SAMPLE_FMT_S16;
83
c->sample_rate = 44100;
97
c->sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
98
c->sample_rate = codec->supported_samplerates ? codec->supported_samplerates[0] : 44100;
99
c->channel_layout = codec->channel_layouts ? codec->channel_layouts[0] : AV_CH_LAYOUT_STEREO;
100
c->channels = av_get_channel_layout_nb_channels(c->channel_layout);
103
ost->st->time_base = (AVRational){ 1, c->sample_rate };
86
105
// some formats want stream headers to be separate
87
106
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
88
107
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
93
static void open_audio(AVFormatContext *oc, AVStream *st)
109
/* initialize sample format conversion;
110
* to simplify the code, we always pass the data through lavr, even
111
* if the encoder supports the generated format directly -- the price is
112
* some extra data copying;
114
ost->avr = avresample_alloc_context();
116
fprintf(stderr, "Error allocating the resampling context\n");
120
av_opt_set_int(ost->avr, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0);
121
av_opt_set_int(ost->avr, "in_sample_rate", 44100, 0);
122
av_opt_set_int(ost->avr, "in_channel_layout", AV_CH_LAYOUT_STEREO, 0);
123
av_opt_set_int(ost->avr, "out_sample_fmt", c->sample_fmt, 0);
124
av_opt_set_int(ost->avr, "out_sample_rate", c->sample_rate, 0);
125
av_opt_set_int(ost->avr, "out_channel_layout", c->channel_layout, 0);
127
ret = avresample_open(ost->avr);
129
fprintf(stderr, "Error opening the resampling context\n");
134
static AVFrame *alloc_audio_frame(enum AVSampleFormat sample_fmt,
135
uint64_t channel_layout,
136
int sample_rate, int nb_samples)
138
AVFrame *frame = av_frame_alloc();
142
fprintf(stderr, "Error allocating an audio frame\n");
146
frame->format = sample_fmt;
147
frame->channel_layout = channel_layout;
148
frame->sample_rate = sample_rate;
149
frame->nb_samples = nb_samples;
152
ret = av_frame_get_buffer(frame, 0);
154
fprintf(stderr, "Error allocating an audio buffer\n");
162
static void open_audio(AVFormatContext *oc, OutputStream *ost)
95
164
AVCodecContext *c;
100
170
if (avcodec_open2(c, NULL, NULL) < 0) {
105
175
/* init signal generator */
107
tincr = 2 * M_PI * 110.0 / c->sample_rate;
177
ost->tincr = 2 * M_PI * 110.0 / c->sample_rate;
108
178
/* increment frequency by 110 Hz per second */
109
tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
179
ost->tincr2 = 2 * M_PI * 110.0 / c->sample_rate / c->sample_rate;
111
181
if (c->codec->capabilities & CODEC_CAP_VARIABLE_FRAME_SIZE)
112
audio_input_frame_size = 10000;
114
audio_input_frame_size = c->frame_size;
115
samples = av_malloc(audio_input_frame_size *
116
av_get_bytes_per_sample(c->sample_fmt) *
184
nb_samples = c->frame_size;
186
ost->frame = alloc_audio_frame(c->sample_fmt, c->channel_layout,
187
c->sample_rate, nb_samples);
188
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, AV_CH_LAYOUT_STEREO,
120
192
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
121
193
* 'nb_channels' channels. */
122
static void get_audio_frame(int16_t *samples, int frame_size, int nb_channels)
194
static AVFrame *get_audio_frame(OutputStream *ost)
196
AVFrame *frame = ost->tmp_frame;
128
for (j = 0; j < frame_size; j++) {
129
v = (int)(sin(t) * 10000);
130
for (i = 0; i < nb_channels; i++)
198
int16_t *q = (int16_t*)frame->data[0];
200
/* check if we want to generate more frames */
201
if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
202
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
206
for (j = 0; j < frame->nb_samples; j++) {
207
v = (int)(sin(ost->t) * 10000);
208
for (i = 0; i < ost->st->codec->channels; i++)
210
ost->t += ost->tincr;
211
ost->tincr += ost->tincr2;
137
static void write_audio_frame(AVFormatContext *oc, AVStream *st)
217
/* if a frame is provided, send it to the encoder, otherwise flush the encoder;
218
* return 1 when encoding is finished, 0 otherwise
220
static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
140
223
AVPacket pkt = { 0 }; // data and size must be 0;
141
AVFrame *frame = av_frame_alloc();
144
226
av_init_packet(&pkt);
147
get_audio_frame(samples, audio_input_frame_size, c->channels);
148
frame->nb_samples = audio_input_frame_size;
149
avcodec_fill_audio_frame(frame, c->channels, c->sample_fmt,
151
audio_input_frame_size *
152
av_get_bytes_per_sample(c->sample_fmt) *
155
avcodec_encode_audio2(c, &pkt, frame, &got_packet);
159
pkt.stream_index = st->index;
161
/* Write the compressed frame to the media file. */
162
if (av_interleaved_write_frame(oc, &pkt) != 0) {
163
fprintf(stderr, "Error while writing audio frame\n");
227
avcodec_encode_audio2(ost->st->codec, &pkt, frame, &got_packet);
230
pkt.stream_index = ost->st->index;
232
av_packet_rescale_ts(&pkt, ost->st->codec->time_base, ost->st->time_base);
234
/* Write the compressed frame to the media file. */
235
if (av_interleaved_write_frame(oc, &pkt) != 0) {
236
fprintf(stderr, "Error while writing audio frame\n");
166
av_frame_free(&frame);
241
return (frame || got_packet) ? 0 : 1;
169
static void close_audio(AVFormatContext *oc, AVStream *st)
245
* encode one audio frame and send it to the muxer
246
* return 1 when encoding is finished, 0 otherwise
248
static int process_audio_stream(AVFormatContext *oc, OutputStream *ost)
171
avcodec_close(st->codec);
254
frame = get_audio_frame(ost);
255
got_output |= !!frame;
257
/* feed the data to lavr */
259
ret = avresample_convert(ost->avr, NULL, 0, 0,
260
frame->extended_data, frame->linesize[0],
263
fprintf(stderr, "Error feeding audio data to the resampler\n");
268
while ((frame && avresample_available(ost->avr) >= ost->frame->nb_samples) ||
269
(!frame && avresample_get_out_samples(ost->avr, 0))) {
270
/* when we pass a frame to the encoder, it may keep a reference to it
272
* make sure we do not overwrite it here
274
ret = av_frame_make_writable(ost->frame);
278
/* the difference between the two avresample calls here is that the
279
* first one just reads the already converted data that is buffered in
280
* the lavr output buffer, while the second one also flushes the
283
ret = avresample_read(ost->avr, ost->frame->extended_data,
284
ost->frame->nb_samples);
286
ret = avresample_convert(ost->avr, ost->frame->extended_data,
287
ost->frame->linesize[0], ost->frame->nb_samples,
292
fprintf(stderr, "Error while resampling\n");
294
} else if (frame && ret != ost->frame->nb_samples) {
295
fprintf(stderr, "Too few samples returned from lavr\n");
299
ost->frame->nb_samples = ret;
301
ost->frame->pts = ost->next_pts;
302
ost->next_pts += ost->frame->nb_samples;
304
got_output |= encode_audio_frame(oc, ost, ret ? ost->frame : NULL);
176
310
/**************************************************************/
177
311
/* video output */
179
static AVFrame *picture, *tmp_picture;
180
static int frame_count;
182
313
/* Add a video output stream. */
183
static AVStream *add_video_stream(AVFormatContext *oc, enum AVCodecID codec_id)
314
static void add_video_stream(OutputStream *ost, AVFormatContext *oc,
315
enum AVCodecID codec_id)
185
317
AVCodecContext *c;
189
320
/* find the video encoder */
227
359
/* Some formats want stream headers to be separate. */
228
360
if (oc->oformat->flags & AVFMT_GLOBALHEADER)
229
361
c->flags |= CODEC_FLAG_GLOBAL_HEADER;
234
364
static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height)
236
366
AVFrame *picture;
237
uint8_t *picture_buf;
240
369
picture = av_frame_alloc();
243
size = avpicture_get_size(pix_fmt, width, height);
244
picture_buf = av_malloc(size);
373
picture->format = pix_fmt;
374
picture->width = width;
375
picture->height = height;
377
/* allocate the buffers for the frame data */
378
ret = av_frame_get_buffer(picture, 32);
380
fprintf(stderr, "Could not allocate frame data.\n");
249
avpicture_fill((AVPicture *)picture, picture_buf,
250
pix_fmt, width, height);
254
static void open_video(AVFormatContext *oc, AVStream *st)
387
static void open_video(AVFormatContext *oc, OutputStream *ost)
256
389
AVCodecContext *c;
260
393
/* open the codec */
261
394
if (avcodec_open2(c, NULL, NULL) < 0) {
308
static void write_video_frame(AVFormatContext *oc, AVStream *st)
449
static AVFrame *get_video_frame(OutputStream *ost)
451
AVCodecContext *c = ost->st->codec;
453
/* check if we want to generate more frames */
454
if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
455
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
458
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
459
/* as we only generate a YUV420P picture, we must convert it
460
* to the codec pixel format if needed */
462
ost->sws_ctx = sws_getContext(c->width, c->height,
466
SCALE_FLAGS, NULL, NULL, NULL);
469
"Cannot initialize the conversion context\n");
473
fill_yuv_image(ost->tmp_frame, ost->next_pts, c->width, c->height);
474
sws_scale(ost->sws_ctx, ost->tmp_frame->data, ost->tmp_frame->linesize,
475
0, c->height, ost->frame->data, ost->frame->linesize);
477
fill_yuv_image(ost->frame, ost->next_pts, c->width, c->height);
480
ost->frame->pts = ost->next_pts++;
486
* encode one video frame and send it to the muxer
487
* return 1 when encoding is finished, 0 otherwise
489
static int write_video_frame(AVFormatContext *oc, OutputStream *ost)
311
492
AVCodecContext *c;
312
static struct SwsContext *img_convert_ctx;
316
if (frame_count >= STREAM_NB_FRAMES) {
317
/* No more frames to compress. The codec has a latency of a few
318
* frames if using B-frames, so we get the last frames by
319
* passing the same picture again. */
321
if (c->pix_fmt != AV_PIX_FMT_YUV420P) {
322
/* as we only generate a YUV420P picture, we must convert it
323
* to the codec pixel format if needed */
324
if (img_convert_ctx == NULL) {
325
img_convert_ctx = sws_getContext(c->width, c->height,
329
sws_flags, NULL, NULL, NULL);
330
if (img_convert_ctx == NULL) {
332
"Cannot initialize the conversion context\n");
336
fill_yuv_image(tmp_picture, frame_count, c->width, c->height);
337
sws_scale(img_convert_ctx, tmp_picture->data, tmp_picture->linesize,
338
0, c->height, picture->data, picture->linesize);
340
fill_yuv_image(picture, frame_count, c->width, c->height);
498
frame = get_video_frame(ost);
344
500
if (oc->oformat->flags & AVFMT_RAWPICTURE) {
345
/* Raw video case - the API will change slightly in the near
346
* future for that. */
501
/* a hack to avoid data copy with some raw video muxers */
348
503
av_init_packet(&pkt);
350
508
pkt.flags |= AV_PKT_FLAG_KEY;
351
pkt.stream_index = st->index;
352
pkt.data = (uint8_t *)picture;
509
pkt.stream_index = ost->st->index;
510
pkt.data = (uint8_t *)frame;
353
511
pkt.size = sizeof(AVPicture);
513
pkt.pts = pkt.dts = frame->pts;
514
av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
355
516
ret = av_interleaved_write_frame(oc, &pkt);
357
518
AVPacket pkt = { 0 };
359
519
av_init_packet(&pkt);
361
521
/* encode the image */
362
ret = avcodec_encode_video2(c, &pkt, picture, &got_packet);
363
/* If size is zero, it means the image was buffered. */
364
if (!ret && got_packet && pkt.size) {
365
if (pkt.pts != AV_NOPTS_VALUE) {
366
pkt.pts = av_rescale_q(pkt.pts,
367
c->time_base, st->time_base);
369
if (pkt.dts != AV_NOPTS_VALUE) {
370
pkt.dts = av_rescale_q(pkt.dts,
371
c->time_base, st->time_base);
373
pkt.stream_index = st->index;
522
ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
524
fprintf(stderr, "Error encoding a video frame\n");
529
av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
530
pkt.stream_index = ost->st->index;
375
532
/* Write the compressed frame to the media file. */
376
533
ret = av_interleaved_write_frame(oc, &pkt);
382
537
fprintf(stderr, "Error while writing video frame\n");
541
return (frame || got_packet) ? 0 : 1;
388
static void close_video(AVFormatContext *oc, AVStream *st)
544
static void close_stream(AVFormatContext *oc, OutputStream *ost)
390
avcodec_close(st->codec);
391
av_free(picture->data[0]);
394
av_free(tmp_picture->data[0]);
395
av_free(tmp_picture);
546
avcodec_close(ost->st->codec);
547
av_frame_free(&ost->frame);
548
av_frame_free(&ost->tmp_frame);
549
sws_freeContext(ost->sws_ctx);
550
avresample_free(&ost->avr);
399
553
/**************************************************************/
445
599
/* Add the audio and video streams using the default format codecs
446
600
* and initialize the codecs. */
449
601
if (fmt->video_codec != AV_CODEC_ID_NONE) {
450
video_st = add_video_stream(oc, fmt->video_codec);
602
add_video_stream(&video_st, oc, fmt->video_codec);
452
606
if (fmt->audio_codec != AV_CODEC_ID_NONE) {
453
audio_st = add_audio_stream(oc, fmt->audio_codec);
607
add_audio_stream(&audio_st, oc, fmt->audio_codec);
456
612
/* Now that all the parameters are set, we can open the audio and
457
613
* video codecs and allocate the necessary encode buffers. */
459
open_video(oc, video_st);
461
open_audio(oc, audio_st);
615
open_video(oc, &video_st);
617
open_audio(oc, &audio_st);
463
619
av_dump_format(oc, 0, filename, 1);
473
629
/* Write the stream header, if any. */
474
630
avformat_write_header(oc, NULL);
477
/* Compute current audio and video time. */
479
audio_pts = (double)audio_st->pts.val * audio_st->time_base.num / audio_st->time_base.den;
484
video_pts = (double)video_st->pts.val * video_st->time_base.num /
485
video_st->time_base.den;
489
if ((!audio_st || audio_pts >= STREAM_DURATION) &&
490
(!video_st || video_pts >= STREAM_DURATION))
493
/* write interleaved audio and video frames */
494
if (!video_st || (video_st && audio_st && audio_pts < video_pts)) {
495
write_audio_frame(oc, audio_st);
632
while (encode_video || encode_audio) {
633
/* select the stream to encode */
635
(!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
636
audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
637
encode_video = !write_video_frame(oc, &video_st);
497
write_video_frame(oc, video_st);
639
encode_audio = !process_audio_stream(oc, &audio_st);