aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2012-01-28 19:15:15 +0100
committerReinhard Tartler <siretart@tauware.de>2012-02-26 09:09:26 +0100
commit571a4cf273a84b6f7f38697b462e667d4f0fddc4 (patch)
tree0b84c39993b378ca1a73fb339980819d04ad7fb8
parentbafd38a352126385ec0dcea51017229373b1c2f3 (diff)
downloadffmpeg-571a4cf273a84b6f7f38697b462e667d4f0fddc4.tar.gz
lavc: set AVCodecContext.codec in avcodec_get_context_defaults3().
This way, if the AVCodecContext is allocated for a specific codec, the caller doesn't need to store this codec separately and then pass it again to avcodec_open2(). It also allows to set codec private options using av_opt_set_* before opening the codec. (cherry picked from commit bc901998487bf9b77a423961d9f961bcc28a9291) Signed-off-by: Reinhard Tartler <siretart@tauware.de>
-rw-r--r--libavcodec/avcodec.h5
-rw-r--r--libavcodec/options.c1
-rw-r--r--libavcodec/utils.c17
-rw-r--r--libavformat/utils.c8
4 files changed, 23 insertions, 8 deletions
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 95e14d7c2d..2451294c1b 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -4059,6 +4059,11 @@ int avcodec_open(AVCodecContext *avctx, AVCodec *codec);
* @endcode
*
* @param avctx The context to initialize.
+ * @param codec The codec to open this context for. If a non-NULL codec has been
+ * previously passed to avcodec_alloc_context3() or
+ * avcodec_get_context_defaults3() for this context, then this
+ * parameter MUST be either NULL or equal to the previously passed
+ * codec.
* @param options A dictionary filled with AVCodecContext and codec-private options.
* On return this object will be filled with options that were not found.
*
diff --git a/libavcodec/options.c b/libavcodec/options.c
index 7481f1a685..26f3ab3b11 100644
--- a/libavcodec/options.c
+++ b/libavcodec/options.c
@@ -561,6 +561,7 @@ int avcodec_get_context_defaults3(AVCodecContext *s, AVCodec *codec){
s->av_class = &av_codec_context_class;
s->codec_type = codec ? codec->type : AVMEDIA_TYPE_UNKNOWN;
+ s->codec = codec;
av_opt_set_defaults(s);
s->time_base = (AVRational){0,1};
diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index b2bd70246a..5109bf8b31 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -640,6 +640,18 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVD
if (avcodec_is_open(avctx))
return 0;
+ if ((!codec && !avctx->codec)) {
+ av_log(avctx, AV_LOG_ERROR, "No codec provided to avcodec_open2().\n");
+ return AVERROR(EINVAL);
+ }
+ if ((codec && avctx->codec && codec != avctx->codec)) {
+ av_log(avctx, AV_LOG_ERROR, "This AVCodecContext was allocated for %s, "
+ "but %s passed to avcodec_open2().\n", avctx->codec->name, codec->name);
+ return AVERROR(EINVAL);
+ }
+ if (!codec)
+ codec = avctx->codec;
+
if (avctx->extradata_size < 0 || avctx->extradata_size >= FF_MAX_EXTRADATA_SIZE)
return AVERROR(EINVAL);
@@ -659,11 +671,6 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVD
goto end;
}
- if(avctx->codec || !codec) {
- ret = AVERROR(EINVAL);
- goto end;
- }
-
avctx->internal = av_mallocz(sizeof(AVCodecInternal));
if (!avctx->internal) {
ret = AVERROR(ENOMEM);
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 3733a50409..f2d55028f9 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -2140,7 +2140,9 @@ static int try_decode_frame(AVStream *st, AVPacket *avpkt, AVDictionary **option
if (!avcodec_is_open(st->codec)) {
AVDictionary *thread_opt = NULL;
- codec = avcodec_find_decoder(st->codec->codec_id);
+ codec = st->codec->codec ? st->codec->codec :
+ avcodec_find_decoder(st->codec->codec_id);
+
if (!codec)
return -1;
@@ -2306,8 +2308,8 @@ int avformat_find_stream_info(AVFormatContext *ic, AVDictionary **options)
st->parser->flags |= PARSER_FLAG_COMPLETE_FRAMES;
}
}
- assert(!st->codec->codec);
- codec = avcodec_find_decoder(st->codec->codec_id);
+ codec = st->codec->codec ? st->codec->codec :
+ avcodec_find_decoder(st->codec->codec_id);
/* force thread count to 1 since the h264 decoder will not extract SPS
* and PPS to extradata during multi-threaded decoding */