Message ID | 20170328114802.10626-1-matthieu.bouron@gmail.com |
---|---|
State | Accepted |
Headers | show |
On Tue, Mar 28, 2017 at 1:48 PM, Matthieu Bouron <matthieu.bouron@gmail.com> wrote: > --- > doc/examples/extract_mvs.c | 33 ++++++++++++++++++++++----------- > 1 file changed, 22 insertions(+), 11 deletions(-) > > diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c > index 975189c77d..09c3d32389 100644 > --- a/doc/examples/extract_mvs.c > +++ b/doc/examples/extract_mvs.c > @@ -69,8 +69,7 @@ static int decode_packet(int *got_frame, int cached) > return decoded; > } > > -static int open_codec_context(int *stream_idx, > - AVFormatContext *fmt_ctx, enum AVMediaType > type) > +static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType > type) > { > int ret; > AVStream *st; > @@ -84,18 +83,29 @@ static int open_codec_context(int *stream_idx, > av_get_media_type_string(type), src_filename); > return ret; > } else { > - *stream_idx = ret; > - st = fmt_ctx->streams[*stream_idx]; > + int stream_idx = ret; > + st = fmt_ctx->streams[stream_idx]; > > /* find decoder for the stream */ > - dec_ctx = st->codec; > - dec = avcodec_find_decoder(dec_ctx->codec_id); > + dec = avcodec_find_decoder(st->codecpar->codec_id); > avcodec_find_decoder call removed locally and replaced by the codec returned by avformat_find_best_stream. [...]
On Tue, Mar 28, 2017 at 12:53 PM, Matthieu Bouron <matthieu.bouron@gmail.com > wrote: > > > On Tue, Mar 28, 2017 at 1:48 PM, Matthieu Bouron < > matthieu.bouron@gmail.com> wrote: > >> --- >> doc/examples/extract_mvs.c | 33 ++++++++++++++++++++++----------- >> 1 file changed, 22 insertions(+), 11 deletions(-) >> >> diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c >> index 975189c77d..09c3d32389 100644 >> --- a/doc/examples/extract_mvs.c >> +++ b/doc/examples/extract_mvs.c >> @@ -69,8 +69,7 @@ static int decode_packet(int *got_frame, int cached) >> return decoded; >> } >> >> -static int open_codec_context(int *stream_idx, >> - AVFormatContext *fmt_ctx, enum AVMediaType >> type) >> +static int open_codec_context(AVFormatContext *fmt_ctx, enum >> AVMediaType type) >> { >> int ret; >> AVStream *st; >> @@ -84,18 +83,29 @@ static int open_codec_context(int *stream_idx, >> av_get_media_type_string(type), src_filename); >> return ret; >> } else { >> - *stream_idx = ret; >> - st = fmt_ctx->streams[*stream_idx]; >> + int stream_idx = ret; >> + st = fmt_ctx->streams[stream_idx]; >> >> /* find decoder for the stream */ >> - dec_ctx = st->codec; >> - dec = avcodec_find_decoder(dec_ctx->codec_id); >> + dec = avcodec_find_decoder(st->codecpar->codec_id); >> > > avcodec_find_decoder call removed locally and replaced by the codec > returned by avformat_find_best_stream. > Patch pushed.
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c index 975189c77d..09c3d32389 100644 --- a/doc/examples/extract_mvs.c +++ b/doc/examples/extract_mvs.c @@ -69,8 +69,7 @@ static int decode_packet(int *got_frame, int cached) return decoded; } -static int open_codec_context(int *stream_idx, - AVFormatContext *fmt_ctx, enum AVMediaType type) +static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type) { int ret; AVStream *st; @@ -84,18 +83,29 @@ static int open_codec_context(int *stream_idx, av_get_media_type_string(type), src_filename); return ret; } else { - *stream_idx = ret; - st = fmt_ctx->streams[*stream_idx]; + int stream_idx = ret; + st = fmt_ctx->streams[stream_idx]; /* find decoder for the stream */ - dec_ctx = st->codec; - dec = avcodec_find_decoder(dec_ctx->codec_id); + dec = avcodec_find_decoder(st->codecpar->codec_id); if (!dec) { fprintf(stderr, "Failed to find %s codec\n", av_get_media_type_string(type)); return AVERROR(EINVAL); } + dec_ctx = avcodec_alloc_context3(dec); + if (!dec_ctx) { + fprintf(stderr, "Failed to allocate codec\n"); + return AVERROR(EINVAL); + } + + ret = avcodec_parameters_to_context(dec_ctx, st->codecpar); + if (ret < 0) { + fprintf(stderr, "Failed to copy codec parameters to codec context\n"); + return ret; + } + /* Init the video decoder */ av_dict_set(&opts, "flags2", "+export_mvs", 0); if ((ret = avcodec_open2(dec_ctx, dec, &opts)) < 0) { @@ -103,6 +113,10 @@ static int open_codec_context(int *stream_idx, av_get_media_type_string(type)); return ret; } + + video_stream_idx = stream_idx; + video_stream = fmt_ctx->streams[video_stream_idx]; + video_dec_ctx = dec_ctx; } return 0; @@ -130,10 +144,7 @@ int main(int argc, char **argv) exit(1); } - if (open_codec_context(&video_stream_idx, fmt_ctx, AVMEDIA_TYPE_VIDEO) >= 0) { - video_stream = fmt_ctx->streams[video_stream_idx]; - video_dec_ctx = video_stream->codec; - } + open_codec_context(fmt_ctx, AVMEDIA_TYPE_VIDEO); av_dump_format(fmt_ctx, 0, src_filename, 0); @@ -178,7 +189,7 @@ int main(int argc, char **argv) } while (got_frame); end: - avcodec_close(video_dec_ctx); + avcodec_free_context(&video_dec_ctx); avformat_close_input(&fmt_ctx); av_frame_free(&frame); return ret < 0;