diff options
author | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2016-04-11 16:06:43 +0300 |
---|---|---|
committer | Derek Buitenhuis <derek.buitenhuis@gmail.com> | 2016-04-11 16:06:43 +0300 |
commit | d76972b58156fc09d0caf26d4bd6462c22be51f1 (patch) | |
tree | 1ebe86e4c749c5daac4a1b4d52c37b8858dd20e3 | |
parent | b62ed56e25c8894a39352c82c7dadd2ebb9191f9 (diff) | |
parent | 9897d9f4e074cdc6c7f2409885ddefe300f18dc7 (diff) |
Merge commit '9897d9f4e074cdc6c7f2409885ddefe300f18dc7'
* commit '9897d9f4e074cdc6c7f2409885ddefe300f18dc7':
examples/output: convert to codecpar
Merged-by: Derek Buitenhuis <derek.buitenhuis@gmail.com>
-rw-r--r-- | doc/examples/muxing.c | 46 |
1 files changed, 33 insertions, 13 deletions
diff --git a/doc/examples/muxing.c b/doc/examples/muxing.c index d4dac5cd69..2fbc89bb02 100644 --- a/doc/examples/muxing.c +++ b/doc/examples/muxing.c @@ -52,6 +52,7 @@ // 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; @@ -104,13 +105,18 @@ static void add_stream(OutputStream *ost, AVFormatContext *oc, 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: @@ -213,7 +219,7 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A int ret; AVDictionary *opt = NULL; - c = ost->st->codec; + c = ost->enc; /* open it */ av_dict_copy(&opt, opt_arg, 0); @@ -240,6 +246,13 @@ static void open_audio(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A 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) { @@ -271,13 +284,13 @@ static AVFrame *get_audio_frame(OutputStream *ost) 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; @@ -303,7 +316,7 @@ static int write_audio_frame(AVFormatContext *oc, OutputStream *ost) int dst_nb_samples; av_init_packet(&pkt); - c = ost->st->codec; + c = ost->enc; frame = get_audio_frame(ost); @@ -383,7 +396,7 @@ static AVFrame *alloc_picture(enum AVPixelFormat pix_fmt, int width, int height) 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); @@ -414,6 +427,13 @@ static void open_video(AVFormatContext *oc, AVCodec *codec, OutputStream *ost, A 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. */ @@ -448,10 +468,10 @@ static void fill_yuv_image(AVFrame *pict, int frame_index, 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; @@ -495,7 +515,7 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *ost) int got_packet = 0; AVPacket pkt = { 0 }; - c = ost->st->codec; + c = ost->enc; frame = get_video_frame(ost); @@ -524,7 +544,7 @@ static int write_video_frame(AVFormatContext *oc, OutputStream *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); @@ -620,8 +640,8 @@ int main(int argc, char **argv) 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); |