[2/4] libavformat: Add support for muxer/demuxer specific defaults

Message ID 1332153486-73460-2-git-send-email-martin@martin.st
State Superseded
Headers show

Commit Message

Martin Storsjö March 19, 2012, 10:38 a.m.
---
 libavformat/avformat.h |   10 ++++++++++
 libavformat/utils.c    |   18 ++++++++++++++++++
 2 files changed, 28 insertions(+), 0 deletions(-)

Comments

Martin Storsjö March 20, 2012, 9:01 a.m. | #1
On Mon, 19 Mar 2012, Martin Storsjö wrote:

> ---
> libavformat/avformat.h |   10 ++++++++++
> libavformat/utils.c    |   18 ++++++++++++++++++
> 2 files changed, 28 insertions(+), 0 deletions(-)
>
> diff --git a/libavformat/avformat.h b/libavformat/avformat.h
> index e0b3b3f..93d61fd 100644
> --- a/libavformat/avformat.h
> +++ b/libavformat/avformat.h
> @@ -364,6 +364,11 @@ typedef struct AVOutputFormat {
>      *         A negative number if unknown.
>      */
>     int (*query_codec)(enum CodecID id, int std_compliance);
> +
> +    /**
> +     * Private muxer-specific defaults.
> +     */
> +    const AVCodecDefault *defaults;
> } AVOutputFormat;
> /**
>  * @}
> @@ -492,6 +497,11 @@ typedef struct AVInputFormat {
>      * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
>      */
>     int (*read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
> +
> +    /**
> +     * Private demuxer-specific defaults.
> +     */
> +    const AVCodecDefault *defaults;
> } AVInputFormat;
> /**
>  * @}
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 4ecf053..ab6e8c6 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -533,6 +533,15 @@ int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputForma
>     if ((ret = init_input(s, filename, &tmp)) < 0)
>         goto fail;
>
> +    if (s->iformat->defaults) {
> +        const AVCodecDefault *d = s->iformat->defaults;
> +        while (d->key) {
> +            int ret = av_opt_set(s, d->key, d->value, 0);
> +            av_assert0(ret >= 0);
> +            d++;
> +        }
> +    }
> +
>     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
>         goto fail;
>
> @@ -2760,6 +2769,15 @@ int avformat_write_header(AVFormatContext *s, AVDictionary **options)
>     AVStream *st;
>     AVDictionary *tmp = NULL;
>
> +    if (s->oformat->defaults) {
> +        const AVCodecDefault *d = s->oformat->defaults;
> +        while (d->key) {
> +            int ret = av_opt_set(s, d->key, d->value, 0);
> +            av_assert0(ret >= 0);
> +            d++;
> +        }
> +    }
> +
>     if (options)
>         av_dict_copy(&tmp, *options, 0);
>     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
> -- 
> 1.7.3.1

For the record, I'm dropping this patch since Anton pointed out that the 
defaults set here would overwrite whatever values the caller manually set 
in the AVFormatContext if it was preallocated before calling these 
functions.

// Martin

Patch

diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index e0b3b3f..93d61fd 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -364,6 +364,11 @@  typedef struct AVOutputFormat {
      *         A negative number if unknown.
      */
     int (*query_codec)(enum CodecID id, int std_compliance);
+
+    /**
+     * Private muxer-specific defaults.
+     */
+    const AVCodecDefault *defaults;
 } AVOutputFormat;
 /**
  * @}
@@ -492,6 +497,11 @@  typedef struct AVInputFormat {
      * Active streams are all streams that have AVStream.discard < AVDISCARD_ALL.
      */
     int (*read_seek2)(struct AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags);
+
+    /**
+     * Private demuxer-specific defaults.
+     */
+    const AVCodecDefault *defaults;
 } AVInputFormat;
 /**
  * @}
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 4ecf053..ab6e8c6 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -533,6 +533,15 @@  int avformat_open_input(AVFormatContext **ps, const char *filename, AVInputForma
     if ((ret = init_input(s, filename, &tmp)) < 0)
         goto fail;
 
+    if (s->iformat->defaults) {
+        const AVCodecDefault *d = s->iformat->defaults;
+        while (d->key) {
+            int ret = av_opt_set(s, d->key, d->value, 0);
+            av_assert0(ret >= 0);
+            d++;
+        }
+    }
+
     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
         goto fail;
 
@@ -2760,6 +2769,15 @@  int avformat_write_header(AVFormatContext *s, AVDictionary **options)
     AVStream *st;
     AVDictionary *tmp = NULL;
 
+    if (s->oformat->defaults) {
+        const AVCodecDefault *d = s->oformat->defaults;
+        while (d->key) {
+            int ret = av_opt_set(s, d->key, d->value, 0);
+            av_assert0(ret >= 0);
+            d++;
+        }
+    }
+
     if (options)
         av_dict_copy(&tmp, *options, 0);
     if ((ret = av_opt_set_dict(s, &tmp)) < 0)