Browse code

lavc: introduce avcodec_open2() as a replacement for avcodec_open().

Adds support for decoder-private options and makes setting other options
simpler.
(cherry picked from commit 0b950fe240936fa48fd41204bcfd04f35bbf39c3)

Conflicts:

libavcodec/avcodec.h

Signed-off-by: Anton Khirnov <anton@khirnov.net>

Anton Khirnov authored on 2011/05/22 21:10:49
Showing 7 changed files
... ...
@@ -291,7 +291,7 @@ static int open_input_file(AVFormatContext **fmt_ctx_ptr, const char *filename)
291 291
         if (!(codec = avcodec_find_decoder(stream->codec->codec_id))) {
292 292
             fprintf(stderr, "Unsupported codec with id %d for input stream %d\n",
293 293
                     stream->codec->codec_id, stream->index);
294
-        } else if (avcodec_open(stream->codec, codec) < 0) {
294
+        } else if (avcodec_open2(stream->codec, codec, NULL) < 0) {
295 295
             fprintf(stderr, "Error while opening codec for input stream %d\n",
296 296
                     stream->index);
297 297
         }
... ...
@@ -2116,7 +2116,7 @@ static void open_parser(AVFormatContext *s, int i)
2116 2116
         codec = avcodec_find_decoder(st->codec->codec_id);
2117 2117
         if (codec && (codec->capabilities & CODEC_CAP_PARSE_ONLY)) {
2118 2118
             st->codec->parse_only = 1;
2119
-            if (avcodec_open(st->codec, codec) < 0)
2119
+            if (avcodec_open2(st->codec, codec, NULL) < 0)
2120 2120
                 st->codec->parse_only = 0;
2121 2121
         }
2122 2122
     }
... ...
@@ -30,6 +30,7 @@
30 30
 #include "libavutil/samplefmt.h"
31 31
 #include "libavutil/avutil.h"
32 32
 #include "libavutil/cpu.h"
33
+#include "libavutil/dict.h"
33 34
 
34 35
 #include "libavcodec/version.h"
35 36
 
... ...
@@ -3615,6 +3616,7 @@ int avcodec_default_execute(AVCodecContext *c, int (*func)(AVCodecContext *c2, v
3615 3615
 int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2, void *arg2, int, int),void *arg, int *ret, int count);
3616 3616
 //FIXME func typedef
3617 3617
 
3618
+#if FF_API_AVCODEC_OPEN
3618 3619
 /**
3619 3620
  * Initialize the AVCodecContext to use the given AVCodec. Prior to using this
3620 3621
  * function the context has to be allocated.
... ...
@@ -3641,8 +3643,45 @@ int avcodec_default_execute2(AVCodecContext *c, int (*func)(AVCodecContext *c2,
3641 3641
  * @param codec The codec to use within the context.
3642 3642
  * @return zero on success, a negative value on error
3643 3643
  * @see avcodec_alloc_context, avcodec_find_decoder, avcodec_find_encoder, avcodec_close
3644
+ *
3645
+ * @deprecated use avcodec_open2
3644 3646
  */
3647
+attribute_deprecated
3645 3648
 int avcodec_open(AVCodecContext *avctx, AVCodec *codec);
3649
+#endif
3650
+
3651
+/**
3652
+ * Initialize the AVCodecContext to use the given AVCodec. Prior to using this
3653
+ * function the context has to be allocated with avcodec_alloc_context().
3654
+ *
3655
+ * The functions avcodec_find_decoder_by_name(), avcodec_find_encoder_by_name(),
3656
+ * avcodec_find_decoder() and avcodec_find_encoder() provide an easy way for
3657
+ * retrieving a codec.
3658
+ *
3659
+ * @warning This function is not thread safe!
3660
+ *
3661
+ * @code
3662
+ * avcodec_register_all();
3663
+ * av_dict_set(&opts, "b", "2.5M", 0);
3664
+ * codec = avcodec_find_decoder(CODEC_ID_H264);
3665
+ * if (!codec)
3666
+ *     exit(1);
3667
+ *
3668
+ * context = avcodec_alloc_context();
3669
+ *
3670
+ * if (avcodec_open(context, codec, opts) < 0)
3671
+ *     exit(1);
3672
+ * @endcode
3673
+ *
3674
+ * @param avctx The context to initialize.
3675
+ * @param options A dictionary filled with AVCodecContext and codec-private options.
3676
+ *                On return this object will be filled with options that were not found.
3677
+ *
3678
+ * @return zero on success, a negative value on error
3679
+ * @see avcodec_alloc_context3(), avcodec_find_decoder(), avcodec_find_encoder(),
3680
+ *      av_dict_set(), av_opt_find().
3681
+ */
3682
+int avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options);
3646 3683
 
3647 3684
 /**
3648 3685
  * Decode the audio frame of size avpkt->size from avpkt->data into samples.
... ...
@@ -972,7 +972,7 @@ static int estimate_best_b_count(MpegEncContext *s){
972 972
     c->time_base= s->avctx->time_base;
973 973
     c->max_b_frames= s->max_b_frames;
974 974
 
975
-    if (avcodec_open(c, codec) < 0)
975
+    if (avcodec_open2(c, codec, NULL) < 0)
976 976
         return -1;
977 977
 
978 978
     for(i=0; i<s->max_b_frames+2; i++){
... ...
@@ -32,6 +32,7 @@
32 32
 #include "libavutil/audioconvert.h"
33 33
 #include "libavutil/imgutils.h"
34 34
 #include "libavutil/samplefmt.h"
35
+#include "libavutil/dict.h"
35 36
 #include "avcodec.h"
36 37
 #include "dsputil.h"
37 38
 #include "libavutil/opt.h"
... ...
@@ -467,9 +468,20 @@ AVFrame *avcodec_alloc_frame(void){
467 467
     return pic;
468 468
 }
469 469
 
470
+#if FF_API_AVCODEC_OPEN
470 471
 int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
471 472
 {
473
+    return avcodec_open2(avctx, codec, NULL);
474
+}
475
+#endif
476
+
477
+int attribute_align_arg avcodec_open2(AVCodecContext *avctx, AVCodec *codec, AVDictionary **options)
478
+{
472 479
     int ret = 0;
480
+    AVDictionary *tmp = NULL;
481
+
482
+    if (options)
483
+        av_dict_copy(&tmp, *options, 0);
473 484
 
474 485
     /* If there is a user-supplied mutex locking routine, call it. */
475 486
     if (ff_lockmgr_cb) {
... ...
@@ -496,14 +508,18 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
496 496
             ret = AVERROR(ENOMEM);
497 497
             goto end;
498 498
         }
499
-        if(codec->priv_class){ //this can be droped once all user apps use   avcodec_get_context_defaults3()
499
+        if (codec->priv_class) {
500 500
             *(AVClass**)avctx->priv_data= codec->priv_class;
501 501
             av_opt_set_defaults(avctx->priv_data);
502 502
         }
503 503
       }
504
+      if (codec->priv_class && (ret = av_opt_set_dict(avctx->priv_data, &tmp) < 0))
505
+          goto free_and_end;
504 506
     } else {
505 507
         avctx->priv_data = NULL;
506 508
     }
509
+    if ((ret = av_opt_set_dict(avctx, &tmp)) < 0)
510
+        goto free_and_end;
507 511
 
508 512
     if(avctx->coded_width && avctx->coded_height)
509 513
         avcodec_set_dimensions(avctx, avctx->coded_width, avctx->coded_height);
... ...
@@ -615,8 +631,14 @@ end:
615 615
     if (ff_lockmgr_cb) {
616 616
         (*ff_lockmgr_cb)(&codec_mutex, AV_LOCK_RELEASE);
617 617
     }
618
+    if (options) {
619
+        av_dict_free(options);
620
+        *options = tmp;
621
+    }
622
+
618 623
     return ret;
619 624
 free_and_end:
625
+    av_dict_free(&tmp);
620 626
     av_freep(&avctx->priv_data);
621 627
     avctx->codec= NULL;
622 628
     goto end;
... ...
@@ -68,5 +68,8 @@
68 68
 #ifndef FF_API_GET_PIX_FMT_NAME
69 69
 #define FF_API_GET_PIX_FMT_NAME (LIBAVCODEC_VERSION_MAJOR < 54)
70 70
 #endif
71
+#ifndef FF_API_AVCODEC_OPEN
72
+#define FF_API_AVCODEC_OPEN     (LIBAVCODEC_VERSION_MAJOR < 54)
73
+#endif
71 74
 
72 75
 #endif /* AVCODEC_VERSION_H */
... ...
@@ -139,7 +139,7 @@ static int movie_init(AVFilterContext *ctx)
139 139
         return AVERROR(EINVAL);
140 140
     }
141 141
 
142
-    if ((ret = avcodec_open(movie->codec_ctx, codec)) < 0) {
142
+    if ((ret = avcodec_open2(movie->codec_ctx, codec, NULL)) < 0) {
143 143
         av_log(ctx, AV_LOG_ERROR, "Failed to open codec\n");
144 144
         return ret;
145 145
     }