[FFmpeg-devel,2/7] avformat: add url field to AVFormatContext

Submitted by Marton Balint on Jan. 6, 2018, 8:50 p.m.

Details

Message ID 20180106205032.15186-2-cus@passwd.hu
State New
Headers show

Commit Message

Marton Balint Jan. 6, 2018, 8:50 p.m.
This will replace the 1024 character limited filename field. Compatiblity for
output contexts are provided by copying filename field to URL if URL is unset
and by providing an internal function for muxers to set both url and filename
at once.

Signed-off-by: Marton Balint <cus@passwd.hu>
---
 doc/APIchanges         |  3 +++
 libavformat/avformat.h | 15 +++++++++++++++
 libavformat/internal.h |  7 +++++++
 libavformat/mux.c      | 11 ++++++++++-
 libavformat/utils.c    | 14 ++++++++++++++
 libavformat/version.h  |  2 +-
 6 files changed, 50 insertions(+), 2 deletions(-)

Comments

Bang He Jan. 8, 2018, 7:12 a.m.
On Sun, Jan 7, 2018 at 4:50 AM, Marton Balint <cus@passwd.hu> wrote:

> This will replace the 1024 character limited filename field. Compatiblity
> for
> output contexts are provided by copying filename field to URL if URL is
> unset
> and by providing an internal function for muxers to set both url and
> filename
> at once.
>
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  doc/APIchanges         |  3 +++
>  libavformat/avformat.h | 15 +++++++++++++++
>  libavformat/internal.h |  7 +++++++
>  libavformat/mux.c      | 11 ++++++++++-
>  libavformat/utils.c    | 14 ++++++++++++++
>  libavformat/version.h  |  2 +-
>  6 files changed, 50 insertions(+), 2 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index d66c842521..4a91b7460a 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -15,6 +15,9 @@ libavutil:     2017-10-21
>
>  API changes, most recent first:
>
> +2018-xx-xx - xxxxxxx - lavf 58.4.100 - avformat.h
> +  Add url field to AVFormatContext and add ff_format_set_url helper
> function.
> +
>  2018-01-xx - xxxxxxx - lavfi 7.11.101 - avfilter.h
>    Deprecate avfilter_link_get_channels(). Use
> av_buffersink_get_channels().
>
> diff --git a/libavformat/avformat.h b/libavformat/avformat.h
> index 4f2798a871..9fa25abd90 100644
> --- a/libavformat/avformat.h
> +++ b/libavformat/avformat.h
> @@ -1391,6 +1391,21 @@ typedef struct AVFormatContext {
>      char filename[1024];
>
>      /**
> +     * input or output URL. Unlike the old filename field, this field has
> no
> +     * length restriction.
> +     *
> +     * - demuxing: set by avformat_open_input(), initialized to an empty
> +     *             string if url parameter was NULL in
> avformat_open_input().
> +     * - muxing: may be set by the caller before calling
> avformat_write_header()
> +     *           (or avformat_init_output() if that is called first) to a
> string
> +     *           which is freeable by av_free(). Set to an empty string
> if it
> +     *           was NULL in avformat_init_output().
> +     *
> +     * Freed by libavformat in avformat_free_context().
> +     */
> +    char *url;
> +
> +    /**
>       * Position of the first frame of the component, in
>       * AV_TIME_BASE fractional seconds. NEVER set this value directly:
>       * It is deduced from the AVStream values.
> diff --git a/libavformat/internal.h b/libavformat/internal.h
> index 0cd0556dc7..1e2a3e05a1 100644
> --- a/libavformat/internal.h
> +++ b/libavformat/internal.h
> @@ -696,4 +696,11 @@ int ff_interleaved_peek(AVFormatContext *s, int
> stream,
>  int ff_lock_avformat(void);
>  int ff_unlock_avformat(void);
>
> +/**
> + * Set AVFormatContext url field to the provided pointer. The pointer must
> + * point to a valid string. The existing url field is freed if necessary.
> Also
> + * set the legacy filename field to the same string which was provided in
> url.
> + */
> +void ff_format_set_url(AVFormatContext *s, char *url);
> +
>  #endif /* AVFORMAT_INTERNAL_H */
> diff --git a/libavformat/mux.c b/libavformat/mux.c
> index ea9f13fdf5..de63f2ca25 100644
> --- a/libavformat/mux.c
> +++ b/libavformat/mux.c
> @@ -186,8 +186,12 @@ int avformat_alloc_output_context2(AVFormatContext
> **avctx, AVOutputFormat *ofor
>      } else
>          s->priv_data = NULL;
>
> -    if (filename)
> +    if (filename) {
>          av_strlcpy(s->filename, filename, sizeof(s->filename));
> +        if (!(s->url = av_strdup(filename)))
> +            goto nomem;
> +
> +    }
>      *avctx = s;
>      return 0;
>  nomem:
> @@ -251,6 +255,11 @@ static int init_muxer(AVFormatContext *s,
> AVDictionary **options)
>          (ret = av_opt_set_dict2(s->priv_data, &tmp,
> AV_OPT_SEARCH_CHILDREN)) < 0)
>          goto fail;
>
> +    if (!s->url && !(s->url = av_strdup(s->filename))) {
> +        ret = AVERROR(ENOMEM);
> +        goto fail;
> +    }
> +
>  #if FF_API_LAVF_AVCTX
>  FF_DISABLE_DEPRECATION_WARNINGS
>      if (s->nb_streams && s->streams[0]->codec->flags &
> AV_CODEC_FLAG_BITEXACT) {
> diff --git a/libavformat/utils.c b/libavformat/utils.c
> index 2185a6f05b..fdfd3a088d 100644
> --- a/libavformat/utils.c
> +++ b/libavformat/utils.c
> @@ -551,6 +551,11 @@ int avformat_open_input(AVFormatContext **ps, const
> char *filename,
>      if ((ret = av_opt_set_dict(s, &tmp)) < 0)
>          goto fail;
>
> +    if (!(s->url = av_strdup(filename ? filename : ""))) {
> +        ret = AVERROR(ENOMEM);
> +        goto fail;
> +    }
> +
>      av_strlcpy(s->filename, filename ? filename : "",
> sizeof(s->filename));
>      if ((ret = init_input(s, filename, &tmp)) < 0)
>          goto fail;
> @@ -4357,6 +4362,7 @@ void avformat_free_context(AVFormatContext *s)
>      av_freep(&s->streams);
>      flush_packet_queue(s);
>      av_freep(&s->internal);
> +    av_freep(&s->url);
>      av_free(s);
>  }
>
> @@ -5624,3 +5630,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
>      return st->internal->avctx->time_base;
>  #endif
>  }
> +
> +void ff_format_set_url(AVFormatContext *s, char *url)
> +{
> +    av_assert0(url);
> +    av_freep(&s->url);
> +    s->url = url;
>
mybe you should: s->url=av_strdup(url)


> +    av_strlcpy(s->filename, url, sizeof(s->filename));
> +}
> diff --git a/libavformat/version.h b/libavformat/version.h
> index 5ced041f0a..49b9906a20 100644
> --- a/libavformat/version.h
> +++ b/libavformat/version.h
> @@ -32,7 +32,7 @@
>  // Major bumping may affect Ticket5467, 5421, 5451(compatibility with
> Chromium)
>  // Also please add any ticket numbers that you believe might be affected
> here
>  #define LIBAVFORMAT_VERSION_MAJOR  58
> -#define LIBAVFORMAT_VERSION_MINOR   3
> +#define LIBAVFORMAT_VERSION_MINOR   4
>  #define LIBAVFORMAT_VERSION_MICRO 100
>
>  #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR,
> \
> --
> 2.13.6
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Marton Balint Jan. 8, 2018, 4:52 p.m.
On Mon, 8 Jan 2018, Bang He wrote:

> On Sun, Jan 7, 2018 at 4:50 AM, Marton Balint <cus@passwd.hu> wrote:
>
>> This will replace the 1024 character limited filename field. Compatiblity
>> for
>> output contexts are provided by copying filename field to URL if URL is
>> unset
>> and by providing an internal function for muxers to set both url and
>> filename
>> at once.
>>
>> Signed-off-by: Marton Balint <cus@passwd.hu>
>> ---
>>  doc/APIchanges         |  3 +++
>>  libavformat/avformat.h | 15 +++++++++++++++
>>  libavformat/internal.h |  7 +++++++
>>  libavformat/mux.c      | 11 ++++++++++-
>>  libavformat/utils.c    | 14 ++++++++++++++
>>  libavformat/version.h  |  2 +-
>>  6 files changed, 50 insertions(+), 2 deletions(-)
>>

[...]

>> @@ -5624,3 +5630,11 @@ FF_ENABLE_DEPRECATION_WARNINGS
>>      return st->internal->avctx->time_base;
>>  #endif
>>  }
>> +
>> +void ff_format_set_url(AVFormatContext *s, char *url)
>> +{
>> +    av_assert0(url);
>> +    av_freep(&s->url);
>> +    s->url = url;
>>
> mybe you should: s->url=av_strdup(url)
>

This is intentional, as the URL can be typically a result of av_sprintf or 
av_bprintf buffer, so re-allocating it would be a waste. Also this way the 
function can remain void because it always succeeds.

Regards,
Marton
Michael Niedermayer Jan. 14, 2018, 10:03 p.m.
On Sat, Jan 06, 2018 at 09:50:27PM +0100, Marton Balint wrote:
> This will replace the 1024 character limited filename field. Compatiblity for
> output contexts are provided by copying filename field to URL if URL is unset
> and by providing an internal function for muxers to set both url and filename
> at once.
> 
> Signed-off-by: Marton Balint <cus@passwd.hu>
> ---
>  doc/APIchanges         |  3 +++
>  libavformat/avformat.h | 15 +++++++++++++++
>  libavformat/internal.h |  7 +++++++
>  libavformat/mux.c      | 11 ++++++++++-
>  libavformat/utils.c    | 14 ++++++++++++++
>  libavformat/version.h  |  2 +-
>  6 files changed, 50 insertions(+), 2 deletions(-)

LGTM

thx

[...]

Patch hide | download patch | download mbox

diff --git a/doc/APIchanges b/doc/APIchanges
index d66c842521..4a91b7460a 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@  libavutil:     2017-10-21
 
 API changes, most recent first:
 
+2018-xx-xx - xxxxxxx - lavf 58.4.100 - avformat.h
+  Add url field to AVFormatContext and add ff_format_set_url helper function.
+
 2018-01-xx - xxxxxxx - lavfi 7.11.101 - avfilter.h
   Deprecate avfilter_link_get_channels(). Use av_buffersink_get_channels().
 
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index 4f2798a871..9fa25abd90 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -1391,6 +1391,21 @@  typedef struct AVFormatContext {
     char filename[1024];
 
     /**
+     * input or output URL. Unlike the old filename field, this field has no
+     * length restriction.
+     *
+     * - demuxing: set by avformat_open_input(), initialized to an empty
+     *             string if url parameter was NULL in avformat_open_input().
+     * - muxing: may be set by the caller before calling avformat_write_header()
+     *           (or avformat_init_output() if that is called first) to a string
+     *           which is freeable by av_free(). Set to an empty string if it
+     *           was NULL in avformat_init_output().
+     *
+     * Freed by libavformat in avformat_free_context().
+     */
+    char *url;
+
+    /**
      * Position of the first frame of the component, in
      * AV_TIME_BASE fractional seconds. NEVER set this value directly:
      * It is deduced from the AVStream values.
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 0cd0556dc7..1e2a3e05a1 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -696,4 +696,11 @@  int ff_interleaved_peek(AVFormatContext *s, int stream,
 int ff_lock_avformat(void);
 int ff_unlock_avformat(void);
 
+/**
+ * Set AVFormatContext url field to the provided pointer. The pointer must
+ * point to a valid string. The existing url field is freed if necessary. Also
+ * set the legacy filename field to the same string which was provided in url.
+ */
+void ff_format_set_url(AVFormatContext *s, char *url);
+
 #endif /* AVFORMAT_INTERNAL_H */
diff --git a/libavformat/mux.c b/libavformat/mux.c
index ea9f13fdf5..de63f2ca25 100644
--- a/libavformat/mux.c
+++ b/libavformat/mux.c
@@ -186,8 +186,12 @@  int avformat_alloc_output_context2(AVFormatContext **avctx, AVOutputFormat *ofor
     } else
         s->priv_data = NULL;
 
-    if (filename)
+    if (filename) {
         av_strlcpy(s->filename, filename, sizeof(s->filename));
+        if (!(s->url = av_strdup(filename)))
+            goto nomem;
+
+    }
     *avctx = s;
     return 0;
 nomem:
@@ -251,6 +255,11 @@  static int init_muxer(AVFormatContext *s, AVDictionary **options)
         (ret = av_opt_set_dict2(s->priv_data, &tmp, AV_OPT_SEARCH_CHILDREN)) < 0)
         goto fail;
 
+    if (!s->url && !(s->url = av_strdup(s->filename))) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
 #if FF_API_LAVF_AVCTX
 FF_DISABLE_DEPRECATION_WARNINGS
     if (s->nb_streams && s->streams[0]->codec->flags & AV_CODEC_FLAG_BITEXACT) {
diff --git a/libavformat/utils.c b/libavformat/utils.c
index 2185a6f05b..fdfd3a088d 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -551,6 +551,11 @@  int avformat_open_input(AVFormatContext **ps, const char *filename,
     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
         goto fail;
 
+    if (!(s->url = av_strdup(filename ? filename : ""))) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
     av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
     if ((ret = init_input(s, filename, &tmp)) < 0)
         goto fail;
@@ -4357,6 +4362,7 @@  void avformat_free_context(AVFormatContext *s)
     av_freep(&s->streams);
     flush_packet_queue(s);
     av_freep(&s->internal);
+    av_freep(&s->url);
     av_free(s);
 }
 
@@ -5624,3 +5630,11 @@  FF_ENABLE_DEPRECATION_WARNINGS
     return st->internal->avctx->time_base;
 #endif
 }
+
+void ff_format_set_url(AVFormatContext *s, char *url)
+{
+    av_assert0(url);
+    av_freep(&s->url);
+    s->url = url;
+    av_strlcpy(s->filename, url, sizeof(s->filename));
+}
diff --git a/libavformat/version.h b/libavformat/version.h
index 5ced041f0a..49b9906a20 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -32,7 +32,7 @@ 
 // Major bumping may affect Ticket5467, 5421, 5451(compatibility with Chromium)
 // Also please add any ticket numbers that you believe might be affected here
 #define LIBAVFORMAT_VERSION_MAJOR  58
-#define LIBAVFORMAT_VERSION_MINOR   3
+#define LIBAVFORMAT_VERSION_MINOR   4
 #define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \