// 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 allocate stream\n");
exit(1);
}
ost->st->id = oc->nb_streams-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;
switch ((*codec)->type) {
case AVMEDIA_TYPE_AUDIO:
int ret;
AVDictionary *opt = NULL;
- c = ost->st->codec;
+ c = ost->enc;
/* open it */
av_dict_copy(&opt, opt_arg, 0);
ost->tmp_frame = alloc_audio_frame(AV_SAMPLE_FMT_S16, c->channel_layout,
c->sample_rate, 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);
+ }
+
/* create resampler context */
ost->swr_ctx = swr_alloc();
if (!ost->swr_ctx) {
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;
int dst_nb_samples;
av_init_packet(&pkt);
- c = ost->st->codec;
+ c = ost->enc;
frame = get_audio_frame(ost);
static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, AVDictionary *opt_arg)
{
int ret;
- AVCodecContext *c = ost->st->codec;
+ AVCodecContext *c = ost->enc;
AVDictionary *opt = NULL;
av_dict_copy(&opt, opt_arg, 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 got_packet = 0;
AVPacket pkt = { 0 };
- c = ost->st->codec;
+ c = ost->enc;
frame = get_video_frame(ost);
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 = !write_audio_frame(oc, &audio_st);