// a wrapper around a single output AVStream
typedef struct OutputStream {
AVStream *st;
+ AVCodecContext *enc;
/* pts of the next frame that will be generated */
int64_t next_pts;
exit(1);
}
- ost->st = avformat_new_stream(oc, codec);
+ ost->st = avformat_new_stream(oc, NULL);
if (!ost->st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
- c = ost->st->codec;
+ c = avcodec_alloc_context3(codec);
+ if (!c) {
+ fprintf(stderr, "Could not alloc an encoding context\n");
+ exit(1);
+ }
+ ost->enc = c;
/* put sample parameters */
c->sample_fmt = codec->sample_fmts ? codec->sample_fmts[0] : AV_SAMPLE_FMT_S16;
static void open_audio(AVFormatContext *oc, OutputStream *ost)
{
AVCodecContext *c;
- int nb_samples;
+ int nb_samples, ret;
- c = ost->st->codec;
+ c = ost->enc;
/* open it */
if (avcodec_open2(c, NULL, NULL) < 0) {
c->sample_rate, nb_samples);
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, AV_CH_LAYOUT_STEREO,
44100, nb_samples);
+
+ /* copy the stream parameters to the muxer */
+ ret = avcodec_parameters_from_context(ost->st->codecpar, c);
+ if (ret < 0) {
+ fprintf(stderr, "Could not copy the stream parameters\n");
+ exit(1);
+ }
}
/* Prepare a 16 bit dummy audio frame of 'frame_size' samples and
int16_t *q = (int16_t*)frame->data[0];
/* check if we want to generate more frames */
- if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
+ if (av_compare_ts(ost->next_pts, ost->enc->time_base,
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
return NULL;
for (j = 0; j < frame->nb_samples; j++) {
v = (int)(sin(ost->t) * 10000);
- for (i = 0; i < ost->st->codec->channels; i++)
+ for (i = 0; i < ost->enc->channels; i++)
*q++ = v;
ost->t += ost->tincr;
ost->tincr += ost->tincr2;
static int encode_audio_frame(AVFormatContext *oc, OutputStream *ost,
AVFrame *frame)
{
- AVPacket pkt = { 0 }; // data and size must be 0;
- int got_packet;
+ int ret;
- av_init_packet(&pkt);
- avcodec_encode_audio2(ost->st->codec, &pkt, frame, &got_packet);
+ ret = avcodec_send_frame(ost->enc, frame);
+ if (ret < 0) {
+ fprintf(stderr, "Error submitting a frame for encoding\n");
+ exit(1);
+ }
- if (got_packet) {
- pkt.stream_index = ost->st->index;
+ while (ret >= 0) {
+ AVPacket pkt = { 0 }; // data and size must be 0;
- av_packet_rescale_ts(&pkt, ost->st->codec->time_base, ost->st->time_base);
+ av_init_packet(&pkt);
- /* Write the compressed frame to the media file. */
- if (av_interleaved_write_frame(oc, &pkt) != 0) {
- fprintf(stderr, "Error while writing audio frame\n");
+ ret = avcodec_receive_packet(ost->enc, &pkt);
+ if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
+ fprintf(stderr, "Error encoding a video frame\n");
exit(1);
+ } else if (ret >= 0) {
+ av_packet_rescale_ts(&pkt, ost->enc->time_base, ost->st->time_base);
+ pkt.stream_index = ost->st->index;
+
+ /* Write the compressed frame to the media file. */
+ ret = av_interleaved_write_frame(oc, &pkt);
+ if (ret < 0) {
+ fprintf(stderr, "Error while writing video frame\n");
+ exit(1);
+ }
}
}
- return (frame || got_packet) ? 0 : 1;
+ return ret == AVERROR_EOF;
}
/*
exit(1);
}
- ost->st = avformat_new_stream(oc, codec);
+ ost->st = avformat_new_stream(oc, NULL);
if (!ost->st) {
fprintf(stderr, "Could not alloc stream\n");
exit(1);
}
- c = ost->st->codec;
+ c = avcodec_alloc_context3(codec);
+ if (!c) {
+ fprintf(stderr, "Could not alloc an encoding context\n");
+ exit(1);
+ }
+ ost->enc = c;
/* Put sample parameters. */
c->bit_rate = 400000;
c->gop_size = 12; /* emit one intra frame every twelve frames at most */
c->pix_fmt = STREAM_PIX_FMT;
if (c->codec_id == AV_CODEC_ID_MPEG2VIDEO) {
- /* just for testing, we also add B frames */
+ /* just for testing, we also add B-frames */
c->max_b_frames = 2;
}
if (c->codec_id == AV_CODEC_ID_MPEG1VIDEO) {
static void open_video(AVFormatContext *oc, OutputStream *ost)
{
AVCodecContext *c;
+ int ret;
- c = ost->st->codec;
+ c = ost->enc;
/* open the codec */
if (avcodec_open2(c, NULL, NULL) < 0) {
exit(1);
}
}
+
+ /* copy the stream parameters to the muxer */
+ ret = avcodec_parameters_from_context(ost->st->codecpar, c);
+ if (ret < 0) {
+ fprintf(stderr, "Could not copy the stream parameters\n");
+ exit(1);
+ }
}
/* Prepare a dummy image. */
static AVFrame *get_video_frame(OutputStream *ost)
{
- AVCodecContext *c = ost->st->codec;
+ AVCodecContext *c = ost->enc;
/* check if we want to generate more frames */
- if (av_compare_ts(ost->next_pts, ost->st->codec->time_base,
+ if (av_compare_ts(ost->next_pts, c->time_base,
STREAM_DURATION, (AVRational){ 1, 1 }) >= 0)
return NULL;
int ret;
AVCodecContext *c;
AVFrame *frame;
- int got_packet = 0;
- c = ost->st->codec;
+ c = ost->enc;
frame = get_video_frame(ost);
- if (oc->oformat->flags & AVFMT_RAWPICTURE) {
- /* a hack to avoid data copy with some raw video muxers */
- AVPacket pkt;
- av_init_packet(&pkt);
-
- if (!frame)
- return 1;
-
- pkt.flags |= AV_PKT_FLAG_KEY;
- pkt.stream_index = ost->st->index;
- pkt.data = (uint8_t *)frame;
- pkt.size = sizeof(AVPicture);
-
- pkt.pts = pkt.dts = frame->pts;
- av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
+ /* encode the image */
+ ret = avcodec_send_frame(c, frame);
+ if (ret < 0) {
+ fprintf(stderr, "Error submitting a frame for encoding\n");
+ exit(1);
+ }
- ret = av_interleaved_write_frame(oc, &pkt);
- } else {
+ while (ret >= 0) {
AVPacket pkt = { 0 };
+
av_init_packet(&pkt);
- /* encode the image */
- ret = avcodec_encode_video2(c, &pkt, frame, &got_packet);
- if (ret < 0) {
+ ret = avcodec_receive_packet(c, &pkt);
+ if (ret < 0 && ret != AVERROR(EAGAIN) && ret != AVERROR_EOF) {
fprintf(stderr, "Error encoding a video frame\n");
exit(1);
- }
-
- if (got_packet) {
+ } else if (ret >= 0) {
av_packet_rescale_ts(&pkt, c->time_base, ost->st->time_base);
pkt.stream_index = ost->st->index;
/* Write the compressed frame to the media file. */
ret = av_interleaved_write_frame(oc, &pkt);
+ if (ret < 0) {
+ fprintf(stderr, "Error while writing video frame\n");
+ exit(1);
+ }
}
}
- if (ret != 0) {
- fprintf(stderr, "Error while writing video frame\n");
- exit(1);
- }
- return (frame || got_packet) ? 0 : 1;
+ return ret == AVERROR_EOF;
}
static void close_stream(AVFormatContext *oc, OutputStream *ost)
{
- avcodec_close(ost->st->codec);
+ avcodec_free_context(&ost->enc);
av_frame_free(&ost->frame);
av_frame_free(&ost->tmp_frame);
sws_freeContext(ost->sws_ctx);
while (encode_video || encode_audio) {
/* select the stream to encode */
if (encode_video &&
- (!encode_audio || av_compare_ts(video_st.next_pts, video_st.st->codec->time_base,
- audio_st.next_pts, audio_st.st->codec->time_base) <= 0)) {
+ (!encode_audio || av_compare_ts(video_st.next_pts, video_st.enc->time_base,
+ audio_st.next_pts, audio_st.enc->time_base) <= 0)) {
encode_video = !write_video_frame(oc, &video_st);
} else {
encode_audio = !process_audio_stream(oc, &audio_st);