]> git.sesse.net Git - ffmpeg/commitdiff
lavc: set AVCodecContext.codec in avcodec_get_context_defaults3().
authorAnton Khirnov <anton@khirnov.net>
Sat, 28 Jan 2012 18:15:15 +0000 (19:15 +0100)
committerAnton Khirnov <anton@khirnov.net>
Tue, 31 Jan 2012 15:53:35 +0000 (16:53 +0100)
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.

libavcodec/avcodec.h
libavcodec/options.c
libavcodec/utils.c
libavformat/utils.c

index 77abec59f69c4d394f0fc4df5d3b270ebc7e9980..85bb59909b358d285f9013f26583a857eb2c8c31 100644 (file)
@@ -3525,6 +3525,11 @@ int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2,
  * @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.
  *
index 52fc66400a1f232bb00243c5958208867213cb19..eb803756fca61f6609de8180223f501511a219f4 100644 (file)
@@ -432,6 +432,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};
index 9cdae22305ebc74d8eb9c9878690342a2a373cf0..fa609534a5d388676295deab58010f6f9122a0ef 100644 (file)
@@ -630,6 +630,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);
 
@@ -649,11 +661,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);
index 1b2239a068f9af787c719171d285e392437b6eb0..cf7180b3cfa13f3859ed427f04506cbbfd438912 100644 (file)
@@ -2009,7 +2009,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;
 
@@ -2169,8 +2171,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 */