Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
@ 2022-02-01 22:30 Scott Theisen
  2022-02-01 22:34 ` James Almer
                   ` (4 more replies)
  0 siblings, 5 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-01 22:30 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

printf %s with a null pointer is undefined behavior
---
 libavutil/avutil.h | 3 +--
 libavutil/utils.c  | 3 ++-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..4bd468d72f 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -207,8 +207,7 @@ enum AVMediaType {
 };
 
 /**
- * Return a string describing the media_type enum, NULL if media_type
- * is unknown.
+ * Return a string describing the media_type enum, never NULL.
  */
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
diff --git a/libavutil/utils.c b/libavutil/utils.c
index ea9b5097b8..c85d7abace 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -71,12 +71,13 @@ const char *avutil_license(void)
 const char *av_get_media_type_string(enum AVMediaType media_type)
 {
     switch (media_type) {
+    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
     case AVMEDIA_TYPE_VIDEO:      return "video";
     case AVMEDIA_TYPE_AUDIO:      return "audio";
     case AVMEDIA_TYPE_DATA:       return "data";
     case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
     case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
-    default:                      return NULL;
+    default:                      return "invalid";
     }
 }
 
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-01 22:30 [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL Scott Theisen
@ 2022-02-01 22:34 ` James Almer
  2022-02-02  1:58   ` Scott Theisen
  2022-02-03 20:09 ` [FFmpeg-devel] [PATCH v2] create av_media_type_get_string() Scott Theisen
                   ` (3 subsequent siblings)
  4 siblings, 1 reply; 19+ messages in thread
From: James Almer @ 2022-02-01 22:34 UTC (permalink / raw)
  To: ffmpeg-devel

On 2/1/2022 7:30 PM, Scott Theisen wrote:
> printf %s with a null pointer is undefined behavior
> ---
>   libavutil/avutil.h | 3 +--
>   libavutil/utils.c  | 3 ++-
>   2 files changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/libavutil/avutil.h b/libavutil/avutil.h
> index 4d633156d1..4bd468d72f 100644
> --- a/libavutil/avutil.h
> +++ b/libavutil/avutil.h
> @@ -207,8 +207,7 @@ enum AVMediaType {
>   };
>   
>   /**
> - * Return a string describing the media_type enum, NULL if media_type
> - * is unknown.
> + * Return a string describing the media_type enum, never NULL.

This is an API breakage, so it's not ok.

The doxy states it returns NULL if media_type is unknown, so you're 
expected to check the returned pointer before trying to use it.

>    */
>   const char *av_get_media_type_string(enum AVMediaType media_type);
>   
> diff --git a/libavutil/utils.c b/libavutil/utils.c
> index ea9b5097b8..c85d7abace 100644
> --- a/libavutil/utils.c
> +++ b/libavutil/utils.c
> @@ -71,12 +71,13 @@ const char *avutil_license(void)
>   const char *av_get_media_type_string(enum AVMediaType media_type)
>   {
>       switch (media_type) {
> +    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
>       case AVMEDIA_TYPE_VIDEO:      return "video";
>       case AVMEDIA_TYPE_AUDIO:      return "audio";
>       case AVMEDIA_TYPE_DATA:       return "data";
>       case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
>       case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
> -    default:                      return NULL;
> +    default:                      return "invalid";
>       }
>   }
>   
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-01 22:34 ` James Almer
@ 2022-02-02  1:58   ` Scott Theisen
  2022-02-02  2:06     ` James Almer
                       ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-02  1:58 UTC (permalink / raw)
  To: ffmpeg-devel

On 2/1/22 17:34, James Almer wrote:
> This is an API breakage, so it's not ok. 
I'm new, so would this require a bump to the MINOR or MICRO version number?

> The doxy states it returns NULL if media_type is unknown, so you're 
> expected to check the returned pointer before trying to use it. 

git grep -E --count "av_get_media_type_string"

doc/APIchanges:1
doc/examples/demuxing_decoding.c:5
doc/examples/extract_mvs.c:2
fftools/cmdutils.c:1
fftools/cmdutils.h:1
fftools/ffmpeg.c:5
fftools/ffmpeg_opt.c:1
fftools/ffplay.c:2
fftools/ffprobe.c:3
libavcodec/avcodec.c:1
libavcodec/tests/avcodec.c:1
libavdevice/dshow.c:2
libavfilter/avfilter.c:2
libavfilter/avfiltergraph.c:2
libavfilter/src_movie.c:3
libavformat/avienc.c:1
libavformat/flvenc.c:1
libavformat/framehash.c:1
libavformat/mov.c:1
libavformat/mpegenc.c:1
libavformat/mpegts.c:1
libavformat/mxfdec.c:1
libavformat/segment.c:1
libavformat/tee.c:1
libavformat/uncodedframecrcenc.c:1
libavutil/avutil.h:1
libavutil/utils.c:1

44 total - 1 non-code - 1 prototype - 1 definition + 4 via macro - 1 
macro = 44 uses

The following uses of av_get_media_type_string() do not check for null:
doc/examples/demuxing_decoding.c: 5
doc/examples/extract_mvs.c: 2
fftools/cmdutils.c: 2 via the define in cmdutils.h
fftools/ffmpeg.c: 7, 2 via the define in cmdutils.h
fftools/ffmpeg_opt.c:1
fftools/ffplay.c:2
libavfilter/avfiltergraph.c:2
libavfilter/src_movie.c:3
libavformat/flvenc.c:1
libavformat/framehash.c:1
libavformat/mov.c:1
libavformat/mpegenc.c:1
libavformat/mpegts.c:1
libavformat/mxfdec.c:1
libavformat/segment.c:1
libavformat/tee.c:1

32/44 uses do not check for null.


libavcodec/tests/avcodec.c: this use is a workaround to 
av_get_media_type_string() returning null; checked 3 times


Let me know which version number to bump and I will submit a new version 
that also removes the now redundant null checks from the rest of the code.

Thanks,
Scott
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-02  1:58   ` Scott Theisen
@ 2022-02-02  2:06     ` James Almer
  2022-02-02  2:42       ` Scott Theisen
  2022-02-02  2:13     ` Andreas Rheinhardt
  2022-02-23  7:52     ` Anton Khirnov
  2 siblings, 1 reply; 19+ messages in thread
From: James Almer @ 2022-02-02  2:06 UTC (permalink / raw)
  To: ffmpeg-devel

On 2/1/2022 10:58 PM, Scott Theisen wrote:
> On 2/1/22 17:34, James Almer wrote:
>> This is an API breakage, so it's not ok. 
> I'm new, so would this require a bump to the MINOR or MICRO version number?

Making a behavior change like this requires a warning and a period to 
let library users adapt their code. You can't break it just like that 
with no warning at all.

> 
>> The doxy states it returns NULL if media_type is unknown, so you're 
>> expected to check the returned pointer before trying to use it. 
> 
> git grep -E --count "av_get_media_type_string"
> 
> doc/APIchanges:1
> doc/examples/demuxing_decoding.c:5
> doc/examples/extract_mvs.c:2
> fftools/cmdutils.c:1
> fftools/cmdutils.h:1
> fftools/ffmpeg.c:5
> fftools/ffmpeg_opt.c:1
> fftools/ffplay.c:2
> fftools/ffprobe.c:3
> libavcodec/avcodec.c:1
> libavcodec/tests/avcodec.c:1
> libavdevice/dshow.c:2
> libavfilter/avfilter.c:2
> libavfilter/avfiltergraph.c:2
> libavfilter/src_movie.c:3
> libavformat/avienc.c:1
> libavformat/flvenc.c:1
> libavformat/framehash.c:1
> libavformat/mov.c:1
> libavformat/mpegenc.c:1
> libavformat/mpegts.c:1
> libavformat/mxfdec.c:1
> libavformat/segment.c:1
> libavformat/tee.c:1
> libavformat/uncodedframecrcenc.c:1
> libavutil/avutil.h:1
> libavutil/utils.c:1
> 
> 44 total - 1 non-code - 1 prototype - 1 definition + 4 via macro - 1 
> macro = 44 uses
> 
> The following uses of av_get_media_type_string() do not check for null:
> doc/examples/demuxing_decoding.c: 5
> doc/examples/extract_mvs.c: 2
> fftools/cmdutils.c: 2 via the define in cmdutils.h
> fftools/ffmpeg.c: 7, 2 via the define in cmdutils.h
> fftools/ffmpeg_opt.c:1
> fftools/ffplay.c:2
> libavfilter/avfiltergraph.c:2
> libavfilter/src_movie.c:3
> libavformat/flvenc.c:1
> libavformat/framehash.c:1
> libavformat/mov.c:1
> libavformat/mpegenc.c:1
> libavformat/mpegts.c:1
> libavformat/mxfdec.c:1
> libavformat/segment.c:1
> libavformat/tee.c:1
> 
> 32/44 uses do not check for null.

But do they need to? Those modules have probably ensured media type is 
one of the known and supported ones by the time they call this function.

Why do you need this function to never return NULL, anyway? If you know 
any of these calls where media type can be UNKNOWN, then it's easier to 
just fix them by checking for NULL as per the doxy.

> 
> 
> libavcodec/tests/avcodec.c: this use is a workaround to 
> av_get_media_type_string() returning null; checked 3 times
> 
> 
> Let me know which version number to bump and I will submit a new version 
> that also removes the now redundant null checks from the rest of the code.
> 
> Thanks,
> Scott
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-02  1:58   ` Scott Theisen
  2022-02-02  2:06     ` James Almer
@ 2022-02-02  2:13     ` Andreas Rheinhardt
  2022-02-23  7:52     ` Anton Khirnov
  2 siblings, 0 replies; 19+ messages in thread
From: Andreas Rheinhardt @ 2022-02-02  2:13 UTC (permalink / raw)
  To: ffmpeg-devel

Scott Theisen:
> On 2/1/22 17:34, James Almer wrote:
>> This is an API breakage, so it's not ok. 
> I'm new, so would this require a bump to the MINOR or MICRO version number?
> 

That would need an announcement in doc/APIchanges; it can then be
changed at the next major version bump after two years have passed.

>> The doxy states it returns NULL if media_type is unknown, so you're
>> expected to check the returned pointer before trying to use it. 
> 
> git grep -E --count "av_get_media_type_string"
> 
> doc/APIchanges:1
> doc/examples/demuxing_decoding.c:5
> doc/examples/extract_mvs.c:2
> fftools/cmdutils.c:1
> fftools/cmdutils.h:1
> fftools/ffmpeg.c:5
> fftools/ffmpeg_opt.c:1
> fftools/ffplay.c:2
> fftools/ffprobe.c:3
> libavcodec/avcodec.c:1
> libavcodec/tests/avcodec.c:1
> libavdevice/dshow.c:2
> libavfilter/avfilter.c:2
> libavfilter/avfiltergraph.c:2
> libavfilter/src_movie.c:3
> libavformat/avienc.c:1
> libavformat/flvenc.c:1
> libavformat/framehash.c:1
> libavformat/mov.c:1
> libavformat/mpegenc.c:1
> libavformat/mpegts.c:1
> libavformat/mxfdec.c:1
> libavformat/segment.c:1
> libavformat/tee.c:1
> libavformat/uncodedframecrcenc.c:1
> libavutil/avutil.h:1
> libavutil/utils.c:1
> 
> 44 total - 1 non-code - 1 prototype - 1 definition + 4 via macro - 1
> macro = 44 uses
> 
> The following uses of av_get_media_type_string() do not check for null:
> doc/examples/demuxing_decoding.c: 5
> doc/examples/extract_mvs.c: 2
> fftools/cmdutils.c: 2 via the define in cmdutils.h
> fftools/ffmpeg.c: 7, 2 via the define in cmdutils.h
> fftools/ffmpeg_opt.c:1
> fftools/ffplay.c:2
> libavfilter/avfiltergraph.c:2
> libavfilter/src_movie.c:3
> libavformat/flvenc.c:1
> libavformat/framehash.c:1
> libavformat/mov.c:1
> libavformat/mpegenc.c:1
> libavformat/mpegts.c:1
> libavformat/mxfdec.c:1
> libavformat/segment.c:1
> libavformat/tee.c:1
> 
> 32/44 uses do not check for null.

Who sets invalid media types?
(In case of fftools/*: If they don't set it themselves, they (and all
our users) should be able to rely on our libraries to not set invalid
values. The same goes for all demuxers (as they set this value
themselves). Checks are only necessary where the media type comes from
the user; this means muxers (where the check should be done generically)
and possibly avfiltergraph.c (I am pretty sure that the type has already
been checked earlier for filters).)

- Andreas
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-02  2:06     ` James Almer
@ 2022-02-02  2:42       ` Scott Theisen
  0 siblings, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-02  2:42 UTC (permalink / raw)
  To: ffmpeg-devel

On 2/1/22 21:06, James Almer wrote:
>> 32/44 uses do not check for null.
>
> But do they need to? Those modules have probably ensured media type is 
> one of the known and supported ones by the time they call this function.
>
> Why do you need this function to never return NULL, anyway? If you 
> know any of these calls where media type can be UNKNOWN, then it's 
> easier to just fix them by checking for NULL as per the doxy. 
The impetus was the desire to use this function in MythTV without 
invoking undefined behavior.  Currently MythTV uses a modification to 
ffmpeg: 
https://github.com/MythTV/mythtv/blob/7fa02d4dc3ab0d3f2cbfcbc514047fe2736fe9cf/mythtv/external/FFmpeg/libavcodec/utils-mythtv.c#L228
(For reference, I just tested and a NULL/nullptr const char* becomes an 
empty QString, but this is not documented.)

Also, another reason was to match behavior with avcodec_get_name() which 
never returns NULL, instead it returns "unknown_codec".

On 2/1/22 21:13, Andreas Rheinhardt wrote:
> That would need an announcement in doc/APIchanges; it can then be
> changed at the next major version bump after two years have passed.
So, would creating a new function instead and deprecating the old one be 
a better option?  (e.g. av_get_media_type_name)
> Who sets invalid media types?
> (In case of fftools/*: If they don't set it themselves, they (and all
> our users) should be able to rely on our libraries to not set invalid
> values. The same goes for all demuxers (as they set this value
> themselves). Checks are only necessary where the media type comes from
> the user; this means muxers (where the check should be done generically)
> and possibly avfiltergraph.c (I am pretty sure that the type has already
> been checked earlier for filters).)
I agree invalid media types are probably not set by anyone, but without 
checking for non-NULL it potentially invokes undefined behavior.

Returning "unknown" for AVMEDIA_TYPE_UNKNOWN and returning a different 
value for the default case is, in my opinion clearer. 
"invalid_media_type" may be a better default string to match the 
behavior of avcodec_get_name().

Regards,
Scott
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v2] create av_media_type_get_string()
  2022-02-01 22:30 [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL Scott Theisen
  2022-02-01 22:34 ` James Almer
@ 2022-02-03 20:09 ` Scott Theisen
  2022-02-03 20:09   ` [FFmpeg-devel] [PATCH 1/2] libavutil: " Scott Theisen
  2022-02-03 20:09   ` [FFmpeg-devel] [PATCH 2/2] replace av_get_media_type_string() with av_media_type_get_string() Scott Theisen
  2022-02-13 21:52 ` [FFmpeg-devel] [PATCH v3] create av_media_type_get_string() Scott Theisen
                   ` (2 subsequent siblings)
  4 siblings, 2 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-03 20:09 UTC (permalink / raw)
  To: ffmpeg-devel

Instead of making an API breaking change, create a new function instead,
and deprecate the old one.

Then replace all uses in FFmpeg of the old function with the new one.


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH 1/2] libavutil: create av_media_type_get_string()
  2022-02-03 20:09 ` [FFmpeg-devel] [PATCH v2] create av_media_type_get_string() Scott Theisen
@ 2022-02-03 20:09   ` Scott Theisen
  2022-02-03 20:09   ` [FFmpeg-devel] [PATCH 2/2] replace av_get_media_type_string() with av_media_type_get_string() Scott Theisen
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-03 20:09 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

This deprecates av_get_media_type_string() which does return NULL.

printf %s with a null pointer is undefined behavior

This also matches behavior with avcodec_get_name() which never returns NULL,
instead it returns "unknown_codec".
---
 doc/APIchanges     |  3 +++
 libavutil/avutil.h |  7 +++++++
 libavutil/utils.c  | 13 +++++++++++++
 3 files changed, 23 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index 315a04c952..5b2ef724be 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-02-03 - xxxxxxxxxxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-01-26 - af94ab7c7c0 - lavu 57.19.100 - tx.h
   Add AV_TX_FLOAT_RDFT, AV_TX_DOUBLE_RDFT and AV_TX_INT32_RDFT.
 
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..6cabeb405a 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -209,9 +209,16 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index ea9b5097b8..92d242f678 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -80,6 +80,19 @@ const char *av_get_media_type_string(enum AVMediaType media_type)
     }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+    switch (media_type) {
+    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
+    case AVMEDIA_TYPE_VIDEO:      return "video";
+    case AVMEDIA_TYPE_AUDIO:      return "audio";
+    case AVMEDIA_TYPE_DATA:       return "data";
+    case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+    case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+    default:                      return "invalid_media_type";
+    }
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
     switch (pict_type) {
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH 2/2] replace av_get_media_type_string() with av_media_type_get_string()
  2022-02-03 20:09 ` [FFmpeg-devel] [PATCH v2] create av_media_type_get_string() Scott Theisen
  2022-02-03 20:09   ` [FFmpeg-devel] [PATCH 1/2] libavutil: " Scott Theisen
@ 2022-02-03 20:09   ` Scott Theisen
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-03 20:09 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

printf %s with a null pointer is undefined behavior.
---
 doc/examples/demuxing_decoding.c | 10 +++++-----
 doc/examples/extract_mvs.c       |  4 ++--
 fftools/cmdutils.c               |  7 +++----
 fftools/cmdutils.h               |  2 --
 fftools/ffmpeg.c                 | 14 +++++++-------
 fftools/ffmpeg_opt.c             |  2 +-
 fftools/ffplay.c                 |  4 ++--
 fftools/ffprobe.c                | 13 +++----------
 libavcodec/avcodec.c             |  2 +-
 libavcodec/tests/avcodec.c       | 10 ++--------
 libavdevice/dshow.c              |  4 ++--
 libavfilter/avfilter.c           |  4 ++--
 libavfilter/avfiltergraph.c      |  4 ++--
 libavfilter/src_movie.c          |  6 +++---
 libavformat/avienc.c             |  2 +-
 libavformat/flvenc.c             |  2 +-
 libavformat/framehash.c          |  2 +-
 libavformat/mov.c                |  2 +-
 libavformat/mpegenc.c            |  2 +-
 libavformat/mpegts.c             |  2 +-
 libavformat/mxfdec.c             |  2 +-
 libavformat/segment.c            |  2 +-
 libavformat/tee.c                |  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 24 files changed, 45 insertions(+), 63 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 8520d5b660..670f08779d 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
         dec = avcodec_find_decoder(st->codecpar->codec_id);
         if (!dec) {
             fprintf(stderr, "Failed to find %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(EINVAL);
         }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
         *dec_ctx = avcodec_alloc_context3(dec);
         if (!*dec_ctx) {
             fprintf(stderr, "Failed to allocate the %s codec context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(ENOMEM);
         }
 
         /* Copy codec parameters from input stream to output codec context */
         if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
             fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
         /* Init the decoders */
         if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
         *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
         av_dict_free(&opts);
         if (ret < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 4b50e15eef..4504d2711c 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -1961,7 +1961,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 0);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-               media_type_string(avfilter_pad_get_type(f->inputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -1972,7 +1972,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 1);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
-               media_type_string(avfilter_pad_get_type(f->outputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->outputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -2253,10 +2253,9 @@ static void print_device_list(const AVDeviceInfoList *device_list)
             device->device_name, device->device_description);
         if (device->nb_media_types > 0) {
             for (int j = 0; j < device->nb_media_types; ++j) {
-                const char* media_type = av_get_media_type_string(device->media_types[j]);
                 if (j > 0)
                     printf(", ");
-                printf("%s", media_type ? media_type : "unknown");
+                printf("%s", av_media_type_get_string(device->media_types[j]));
             }
         } else {
             printf("none");
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 50eed9b13a..a3e06b7b29 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -642,8 +642,6 @@ void *grow_array(void *array, int elem_size, int *size, int new_size);
  */
 void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
 
-#define media_type_string av_get_media_type_string
-
 #define GROW_ARRAY(array, nb_elems)\
     array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5d134b025f..149a28d685 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -853,7 +853,7 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int u
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
-                av_get_media_type_string(ost->enc_ctx->codec_type),
+                av_media_type_get_string(ost->enc_ctx->codec_type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 pkt->size
@@ -1611,7 +1611,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ist->nb_packets;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-                   i, j, media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
                    ist->nb_packets, ist->data_size);
 
@@ -1645,7 +1645,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ost->packets_written;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
-                   i, j, media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             if (ost->encoding_needed) {
                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
                        ost->frames_encoded);
@@ -3581,7 +3581,7 @@ static void report_new_stream(int input_index, AVPacket *pkt)
         return;
     av_log(file->ctx, AV_LOG_WARNING,
            "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            input_index, pkt->stream_index,
            pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
     file->nb_streams_warn = pkt->stream_index + 1;
@@ -4314,7 +4314,7 @@ static int process_input(int file_index)
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
                "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
                av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
@@ -4441,7 +4441,7 @@ static int process_input(int file_index)
                        "timestamp discontinuity for stream #%d:%d "
                        "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
                        ist->file_index, ist->st->index, ist->st->id,
-                       av_get_media_type_string(ist->dec_ctx->codec_type),
+                       av_media_type_get_string(ist->dec_ctx->codec_type),
                        delta, ifile->ts_offset);
                 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                 if (pkt->pts != AV_NOPTS_VALUE)
@@ -4470,7 +4470,7 @@ static int process_input(int file_index)
 
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
                av_ts2str(input_files[ist->file_index]->ts_offset),
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 9c820ab73f..78e332f61e 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1696,7 +1696,7 @@ static void check_streamcopy_filters(OptionsContext *o, AVFormatContext *oc,
                "Filtering and streamcopy cannot be used together.\n",
                ost->filters ? "Filtergraph" : "Filtergraph script",
                ost->filters ? ost->filters : ost->filters_script,
-               av_get_media_type_string(type), ost->file_index, ost->index);
+               av_media_type_get_string(type), ost->file_index, ost->index);
         exit_program(1);
     }
 }
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index e7b20be76b..d6c660dae4 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -2872,7 +2872,7 @@ static int read_thread(void *arg)
     }
     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
         if (wanted_stream_spec[i] && st_index[i] == -1) {
-            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_get_media_type_string(i));
+            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_media_type_get_string(i));
             st_index[i] = INT_MAX;
         }
     }
@@ -3203,7 +3203,7 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
     if (p && stream_index != -1)
         stream_index = p->stream_index[stream_index];
     av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
-           av_get_media_type_string(codec_type),
+           av_media_type_get_string(codec_type),
            old_index,
            stream_index);
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 20582ca7ac..69cde7a92d 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -2327,15 +2327,12 @@ static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int p
     char val_str[128];
     AVStream *st = ifile->streams[pkt->stream_index].st;
     AVBPrint pbuf;
-    const char *s;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
     writer_print_section_header(w, SECTION_ID_PACKET);
 
-    s = av_get_media_type_string(st->codecpar->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(st->codecpar->codec_type));
     print_int("stream_index",     pkt->stream_index);
     print_ts  ("pts",             pkt->pts);
     print_time("pts_time",        pkt->pts, &st->time_base);
@@ -2410,9 +2407,7 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
 
     writer_print_section_header(w, SECTION_ID_FRAME);
 
-    s = av_get_media_type_string(stream->codecpar->codec_type);
-    if (s) print_str    ("media_type", s);
-    else   print_str_opt("media_type", "unknown");
+    print_str("media_type", av_media_type_get_string(stream->codecpar->codec_type));
     print_int("stream_index",           stream->index);
     print_int("key_frame",              frame->key_frame);
     print_ts  ("pts",                   frame->pts);
@@ -2809,9 +2804,7 @@ static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_id
             print_str_opt("profile", "unknown");
     }
 
-    s = av_get_media_type_string(par->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(par->codec_type));
 
     /* print AVI/FourCC tag */
     print_str("codec_tag_string",    av_fourcc2str(par->codec_tag));
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index c00a9b2af8..04a9e588e3 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -531,7 +531,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
     if (!buf || buf_size <= 0)
         return;
     av_bprint_init_for_buffer(&bprint, buf, buf_size);
-    codec_type = av_get_media_type_string(enc->codec_type);
+    codec_type = av_media_type_get_string(enc->codec_type);
     codec_name = avcodec_get_name(enc->codec_id);
     profile = avcodec_profile_name(enc->codec_id, enc->profile);
 
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index 5d0ff9432c..34ab01bc2a 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -21,12 +21,6 @@
 #include "libavcodec/codec_desc.h"
 #include "libavcodec/internal.h"
 
-static const char *get_type_string(enum AVMediaType type)
-{
-    const char *ret = av_get_media_type_string(type);
-    return ret ? ret : "unknown";
-}
-
 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
 #define ERR_INTERNAL(msg, ...)                                  \
 do {                                                            \
@@ -74,7 +68,7 @@ int main(void){
             codec->type != AVMEDIA_TYPE_AUDIO &&
             codec->type != AVMEDIA_TYPE_SUBTITLE)
             ERR_EXT("Codec %s has unsupported type %s\n",
-                    get_type_string(codec->type));
+                    av_media_type_get_string(codec->type));
         if (codec->type != AVMEDIA_TYPE_AUDIO) {
             if (codec->channel_layouts || codec->sample_fmts ||
                 codec->supported_samplerates)
@@ -158,7 +152,7 @@ int main(void){
         } else if (desc->type != codec->type)
             ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
                     "%s vs %s\n",
-                    get_type_string(codec->type), get_type_string(desc->type));
+                    av_media_type_get_string(codec->type), av_media_type_get_string(desc->type));
     }
     return ret;
 }
diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 3a16f3720f..6ecb9c848b 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -577,10 +577,10 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
             else {
                 av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
                 if (nb_media_types > 0) {
-                    const char* media_type = av_get_media_type_string(media_types[0]);
+                    const char* media_type = av_media_type_get_string(media_types[0]);
                     av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
                     for (int i = 1; i < nb_media_types; ++i) {
-                        media_type = av_get_media_type_string(media_types[i]);
+                        media_type = av_media_type_get_string(media_types[i]);
                         av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
                     }
                     av_log(avctx, AV_LOG_INFO, ")");
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 7362bcdab5..a991a6272e 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -174,8 +174,8 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
         av_log(src, AV_LOG_ERROR,
                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
-               src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
-               dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
+               src->name, srcpad, av_media_type_get_string(src->output_pads[srcpad].type),
+               dst->name, dstpad, av_media_type_get_string(dst-> input_pads[dstpad].type));
         return AVERROR(EINVAL);
     }
 
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index b8b432e98b..fa971734d1 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -220,7 +220,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->input_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
@@ -230,7 +230,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->output_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index b89a680883..b09f5698fb 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -113,7 +113,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
         if (ret < 0) {
             av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
-                   av_get_media_type_string(type), stream_id);
+                   av_media_type_get_string(type), stream_id);
             return NULL;
         }
         return avf->streams[ret];
@@ -148,7 +148,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
         av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
                "currently unsupported by libavfilter\n", spec,
-               av_get_media_type_string(found->codecpar->codec_type));
+               av_media_type_get_string(found->codecpar->codec_type));
         return NULL;
     }
     return found;
@@ -426,7 +426,7 @@ static char *describe_frame_to_str(char *dst, size_t dst_size,
                  frame->nb_samples);
                  break;
     default:
-        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
+        snprintf(dst, dst_size, "%s BUG", av_media_type_get_string(frame_type));
         break;
     }
     return dst;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index be2493ce55..ec72a74eec 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -473,7 +473,7 @@ static int avi_write_header(AVFormatContext *s)
             default:
                 av_log(s, AV_LOG_ERROR,
                     "Invalid or not supported codec type '%s' found in the input\n",
-                    (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
+                    av_media_type_get_string(par->codec_type));
                 return AVERROR(EINVAL);
             }
             ff_end_tag(pb, strf);
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 66c530a2ff..1267ed00a8 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -670,7 +670,7 @@ static int flv_init(struct AVFormatContext *s)
             break;
         default:
             av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
-                   av_get_media_type_string(par->codec_type), i);
+                   av_media_type_get_string(par->codec_type), i);
             return AVERROR(EINVAL);
         }
         avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
diff --git a/libavformat/framehash.c b/libavformat/framehash.c
index 04c40825b9..b9f765f097 100644
--- a/libavformat/framehash.c
+++ b/libavformat/framehash.c
@@ -32,7 +32,7 @@ int ff_framehash_write_header(AVFormatContext *s)
         AVCodecParameters *avctx = st->codecpar;
         char buf[256] = { 0 };
         avio_printf(s->pb, "#tb %d: %d/%d\n", i, st->time_base.num, st->time_base.den);
-        avio_printf(s->pb, "#media_type %d: %s\n", i, av_get_media_type_string(avctx->codec_type));
+        avio_printf(s->pb, "#media_type %d: %s\n", i, av_media_type_get_string(avctx->codec_type));
         avio_printf(s->pb, "#codec_id %d: %s\n", i, avcodec_get_name(avctx->codec_id));
         switch (avctx->codec_type) {
         case AVMEDIA_TYPE_AUDIO:
diff --git a/libavformat/mov.c b/libavformat/mov.c
index a80fcc1606..b2041ec160 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7141,7 +7141,7 @@ static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     av_log(ctx, AV_LOG_TRACE,
            "%s stream %d KindBox(scheme: %s, value: %s)\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            st->index,
            scheme_buf.str, value_buf.str);
 
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index b1d8bf9c38..74e22988f0 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -456,7 +456,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
             break;
         default:
             av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
-                   av_get_media_type_string(st->codecpar->codec_type), i);
+                   av_media_type_get_string(st->codecpar->codec_type), i);
             return AVERROR(EINVAL);
         }
         stream->fifo = av_fifo_alloc(16);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index da15223b8a..2e2fec83f7 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2253,7 +2253,7 @@ static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int p
     if (found) {
         av_log(ts->stream, AV_LOG_VERBOSE,
                "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
-               av_get_media_type_string(found->codecpar->codec_type),
+               av_media_type_get_string(found->codecpar->codec_type),
                found->index, found->id, pid);
     }
 
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index b85c10bf19..5763c5db8b 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -2404,7 +2404,7 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
         av_dict_set(&st->metadata, "track_name", track->name, 0);
 
     codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &track->sequence->data_definition_ul);
-    av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
+    av_dict_set(&st->metadata, "data_type", av_media_type_get_string(codec_ul->id), 0);
     return 0;
 }
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index e9b0aa4fa8..2fac2647a1 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -737,7 +737,7 @@ static int seg_init(AVFormatContext *s)
         return ret;
     av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
            seg->reference_stream_index,
-           av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
+           av_media_type_get_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
 
     seg->oformat = av_guess_format(seg->format, s->url, NULL);
 
diff --git a/libavformat/tee.c b/libavformat/tee.c
index b3bcd70b9f..97db7c789b 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -419,7 +419,7 @@ static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
 
         av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
                i, avcodec_get_name(st->codecpar->codec_id),
-               av_get_media_type_string(st->codecpar->codec_type));
+               av_media_type_get_string(st->codecpar->codec_type));
 
         bsf_name = bsf->filter->priv_class ?
                    bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
diff --git a/libavformat/uncodedframecrcenc.c b/libavformat/uncodedframecrcenc.c
index e30fe4ddb9..04e8818711 100644
--- a/libavformat/uncodedframecrcenc.c
+++ b/libavformat/uncodedframecrcenc.c
@@ -130,7 +130,6 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     AVBPrint bp;
     int ret = 0;
     enum AVMediaType type;
-    const char *type_name;
 
     if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
         return 0;
@@ -139,8 +138,7 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     av_bprintf(&bp, "%d, %10"PRId64"",
                stream_index, (*frame)->pts);
     type = s->streams[stream_index]->codecpar->codec_type;
-    type_name = av_get_media_type_string(type);
-    av_bprintf(&bp, ", %s", type_name ? type_name : "unknown");
+    av_bprintf(&bp, ", %s", av_media_type_get_string(type));
     switch (type) {
         case AVMEDIA_TYPE_VIDEO:
             video_frame_cksum(&bp, *frame);
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v3] create av_media_type_get_string()
  2022-02-01 22:30 [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL Scott Theisen
  2022-02-01 22:34 ` James Almer
  2022-02-03 20:09 ` [FFmpeg-devel] [PATCH v2] create av_media_type_get_string() Scott Theisen
@ 2022-02-13 21:52 ` Scott Theisen
  2022-02-13 21:52   ` [FFmpeg-devel] [PATCH v3 1/2] libavutil: " Scott Theisen
  2022-02-13 21:52   ` [FFmpeg-devel] [PATCH v3 2/2] av_get_media_type_string(): replace with av_media_type_get_string() Scott Theisen
  2022-05-15 19:55 ` [FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string() Scott Theisen
  2022-06-01 19:22 ` [FFmpeg-devel] [PATCH] " Scott Theisen
  4 siblings, 2 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-13 21:52 UTC (permalink / raw)
  To: ffmpeg-devel

Identical to v2, but I reworded the second commit's message to add a context.


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v3 1/2] libavutil: create av_media_type_get_string()
  2022-02-13 21:52 ` [FFmpeg-devel] [PATCH v3] create av_media_type_get_string() Scott Theisen
@ 2022-02-13 21:52   ` Scott Theisen
  2022-02-13 21:52   ` [FFmpeg-devel] [PATCH v3 2/2] av_get_media_type_string(): replace with av_media_type_get_string() Scott Theisen
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-13 21:52 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

This deprecates av_get_media_type_string() which does return NULL.

printf %s with a null pointer is undefined behavior

This also matches behavior with avcodec_get_name() which never returns NULL,
instead it returns "unknown_codec".
---
 doc/APIchanges     |  3 +++
 libavutil/avutil.h |  7 +++++++
 libavutil/utils.c  | 13 +++++++++++++
 3 files changed, 23 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index ea402f6118..7733de8dea 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-02-XX - xxxxxxxxxxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-02-07 - xxxxxxxxxx - lavu 57.21.100 - fifo.h
   Deprecate AVFifoBuffer and the API around it, namely av_fifo_alloc(),
   av_fifo_alloc_array(), av_fifo_free(), av_fifo_freep(), av_fifo_reset(),
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..6cabeb405a 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -209,9 +209,16 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index ea9b5097b8..92d242f678 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -80,6 +80,19 @@ const char *av_get_media_type_string(enum AVMediaType media_type)
     }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+    switch (media_type) {
+    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
+    case AVMEDIA_TYPE_VIDEO:      return "video";
+    case AVMEDIA_TYPE_AUDIO:      return "audio";
+    case AVMEDIA_TYPE_DATA:       return "data";
+    case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+    case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+    default:                      return "invalid_media_type";
+    }
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
     switch (pict_type) {
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v3 2/2] av_get_media_type_string(): replace with av_media_type_get_string()
  2022-02-13 21:52 ` [FFmpeg-devel] [PATCH v3] create av_media_type_get_string() Scott Theisen
  2022-02-13 21:52   ` [FFmpeg-devel] [PATCH v3 1/2] libavutil: " Scott Theisen
@ 2022-02-13 21:52   ` Scott Theisen
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-13 21:52 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

printf %s with a null pointer is undefined behavior.
---
 doc/examples/demuxing_decoding.c | 10 +++++-----
 doc/examples/extract_mvs.c       |  4 ++--
 fftools/cmdutils.c               |  7 +++----
 fftools/cmdutils.h               |  2 --
 fftools/ffmpeg.c                 | 14 +++++++-------
 fftools/ffmpeg_opt.c             |  2 +-
 fftools/ffplay.c                 |  4 ++--
 fftools/ffprobe.c                | 13 +++----------
 libavcodec/avcodec.c             |  2 +-
 libavcodec/tests/avcodec.c       | 10 ++--------
 libavdevice/dshow.c              |  4 ++--
 libavfilter/avfilter.c           |  4 ++--
 libavfilter/avfiltergraph.c      |  4 ++--
 libavfilter/src_movie.c          |  6 +++---
 libavformat/avienc.c             |  2 +-
 libavformat/flvenc.c             |  2 +-
 libavformat/framehash.c          |  2 +-
 libavformat/mov.c                |  2 +-
 libavformat/mpegenc.c            |  2 +-
 libavformat/mpegts.c             |  2 +-
 libavformat/mxfdec.c             |  2 +-
 libavformat/segment.c            |  2 +-
 libavformat/tee.c                |  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 24 files changed, 45 insertions(+), 63 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 8520d5b660..670f08779d 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
         dec = avcodec_find_decoder(st->codecpar->codec_id);
         if (!dec) {
             fprintf(stderr, "Failed to find %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(EINVAL);
         }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
         *dec_ctx = avcodec_alloc_context3(dec);
         if (!*dec_ctx) {
             fprintf(stderr, "Failed to allocate the %s codec context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(ENOMEM);
         }
 
         /* Copy codec parameters from input stream to output codec context */
         if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
             fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
         /* Init the decoders */
         if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
         *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
         av_dict_free(&opts);
         if (ret < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
diff --git a/fftools/cmdutils.c b/fftools/cmdutils.c
index 4b50e15eef..4504d2711c 100644
--- a/fftools/cmdutils.c
+++ b/fftools/cmdutils.c
@@ -1961,7 +1961,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 0);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-               media_type_string(avfilter_pad_get_type(f->inputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -1972,7 +1972,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 1);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
-               media_type_string(avfilter_pad_get_type(f->outputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->outputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -2253,10 +2253,9 @@ static void print_device_list(const AVDeviceInfoList *device_list)
             device->device_name, device->device_description);
         if (device->nb_media_types > 0) {
             for (int j = 0; j < device->nb_media_types; ++j) {
-                const char* media_type = av_get_media_type_string(device->media_types[j]);
                 if (j > 0)
                     printf(", ");
-                printf("%s", media_type ? media_type : "unknown");
+                printf("%s", av_media_type_get_string(device->media_types[j]));
             }
         } else {
             printf("none");
diff --git a/fftools/cmdutils.h b/fftools/cmdutils.h
index 50eed9b13a..a3e06b7b29 100644
--- a/fftools/cmdutils.h
+++ b/fftools/cmdutils.h
@@ -642,8 +642,6 @@ void *grow_array(void *array, int elem_size, int *size, int new_size);
  */
 void *allocate_array_elem(void *array, size_t elem_size, int *nb_elems);
 
-#define media_type_string av_get_media_type_string
-
 #define GROW_ARRAY(array, nb_elems)\
     array = grow_array(array, sizeof(*array), &nb_elems, nb_elems + 1)
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 7beea11933..bd26adf35b 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -844,7 +844,7 @@ static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int u
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s size:%d\n",
-                av_get_media_type_string(ost->enc_ctx->codec_type),
+                av_media_type_get_string(ost->enc_ctx->codec_type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 pkt->size
@@ -1602,7 +1602,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ist->nb_packets;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-                   i, j, media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
                    ist->nb_packets, ist->data_size);
 
@@ -1636,7 +1636,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ost->packets_written;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
-                   i, j, media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             if (ost->encoding_needed) {
                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
                        ost->frames_encoded);
@@ -3565,7 +3565,7 @@ static void report_new_stream(int input_index, AVPacket *pkt)
         return;
     av_log(file->ctx, AV_LOG_WARNING,
            "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            input_index, pkt->stream_index,
            pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
     file->nb_streams_warn = pkt->stream_index + 1;
@@ -4298,7 +4298,7 @@ static int process_input(int file_index)
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
                "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
                av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
@@ -4425,7 +4425,7 @@ static int process_input(int file_index)
                        "timestamp discontinuity for stream #%d:%d "
                        "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
                        ist->file_index, ist->st->index, ist->st->id,
-                       av_get_media_type_string(ist->dec_ctx->codec_type),
+                       av_media_type_get_string(ist->dec_ctx->codec_type),
                        delta, ifile->ts_offset);
                 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                 if (pkt->pts != AV_NOPTS_VALUE)
@@ -4454,7 +4454,7 @@ static int process_input(int file_index)
 
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
                av_ts2str(input_files[ist->file_index]->ts_offset),
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 3102851885..653dee6d67 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1694,7 +1694,7 @@ static void check_streamcopy_filters(OptionsContext *o, AVFormatContext *oc,
                "Filtering and streamcopy cannot be used together.\n",
                ost->filters ? "Filtergraph" : "Filtergraph script",
                ost->filters ? ost->filters : ost->filters_script,
-               av_get_media_type_string(type), ost->file_index, ost->index);
+               av_media_type_get_string(type), ost->file_index, ost->index);
         exit_program(1);
     }
 }
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index ac48d8765d..2cc9804770 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -2868,7 +2868,7 @@ static int read_thread(void *arg)
     }
     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
         if (wanted_stream_spec[i] && st_index[i] == -1) {
-            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_get_media_type_string(i));
+            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_media_type_get_string(i));
             st_index[i] = INT_MAX;
         }
     }
@@ -3199,7 +3199,7 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
     if (p && stream_index != -1)
         stream_index = p->stream_index[stream_index];
     av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
-           av_get_media_type_string(codec_type),
+           av_media_type_get_string(codec_type),
            old_index,
            stream_index);
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 8a8e3de540..32723177a0 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -2327,15 +2327,12 @@ static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int p
     char val_str[128];
     AVStream *st = ifile->streams[pkt->stream_index].st;
     AVBPrint pbuf;
-    const char *s;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
     writer_print_section_header(w, SECTION_ID_PACKET);
 
-    s = av_get_media_type_string(st->codecpar->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(st->codecpar->codec_type));
     print_int("stream_index",     pkt->stream_index);
     print_ts  ("pts",             pkt->pts);
     print_time("pts_time",        pkt->pts, &st->time_base);
@@ -2410,9 +2407,7 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
 
     writer_print_section_header(w, SECTION_ID_FRAME);
 
-    s = av_get_media_type_string(stream->codecpar->codec_type);
-    if (s) print_str    ("media_type", s);
-    else   print_str_opt("media_type", "unknown");
+    print_str("media_type", av_media_type_get_string(stream->codecpar->codec_type));
     print_int("stream_index",           stream->index);
     print_int("key_frame",              frame->key_frame);
     print_ts  ("pts",                   frame->pts);
@@ -2809,9 +2804,7 @@ static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_id
             print_str_opt("profile", "unknown");
     }
 
-    s = av_get_media_type_string(par->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(par->codec_type));
 
     /* print AVI/FourCC tag */
     print_str("codec_tag_string",    av_fourcc2str(par->codec_tag));
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 4df834c708..ee4155d4e6 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -526,7 +526,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
     if (!buf || buf_size <= 0)
         return;
     av_bprint_init_for_buffer(&bprint, buf, buf_size);
-    codec_type = av_get_media_type_string(enc->codec_type);
+    codec_type = av_media_type_get_string(enc->codec_type);
     codec_name = avcodec_get_name(enc->codec_id);
     profile = avcodec_profile_name(enc->codec_id, enc->profile);
 
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index 5d0ff9432c..34ab01bc2a 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -21,12 +21,6 @@
 #include "libavcodec/codec_desc.h"
 #include "libavcodec/internal.h"
 
-static const char *get_type_string(enum AVMediaType type)
-{
-    const char *ret = av_get_media_type_string(type);
-    return ret ? ret : "unknown";
-}
-
 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
 #define ERR_INTERNAL(msg, ...)                                  \
 do {                                                            \
@@ -74,7 +68,7 @@ int main(void){
             codec->type != AVMEDIA_TYPE_AUDIO &&
             codec->type != AVMEDIA_TYPE_SUBTITLE)
             ERR_EXT("Codec %s has unsupported type %s\n",
-                    get_type_string(codec->type));
+                    av_media_type_get_string(codec->type));
         if (codec->type != AVMEDIA_TYPE_AUDIO) {
             if (codec->channel_layouts || codec->sample_fmts ||
                 codec->supported_samplerates)
@@ -158,7 +152,7 @@ int main(void){
         } else if (desc->type != codec->type)
             ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
                     "%s vs %s\n",
-                    get_type_string(codec->type), get_type_string(desc->type));
+                    av_media_type_get_string(codec->type), av_media_type_get_string(desc->type));
     }
     return ret;
 }
diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 3a16f3720f..6ecb9c848b 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -577,10 +577,10 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
             else {
                 av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
                 if (nb_media_types > 0) {
-                    const char* media_type = av_get_media_type_string(media_types[0]);
+                    const char* media_type = av_media_type_get_string(media_types[0]);
                     av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
                     for (int i = 1; i < nb_media_types; ++i) {
-                        media_type = av_get_media_type_string(media_types[i]);
+                        media_type = av_media_type_get_string(media_types[i]);
                         av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
                     }
                     av_log(avctx, AV_LOG_INFO, ")");
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 7362bcdab5..a991a6272e 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -174,8 +174,8 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
         av_log(src, AV_LOG_ERROR,
                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
-               src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
-               dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
+               src->name, srcpad, av_media_type_get_string(src->output_pads[srcpad].type),
+               dst->name, dstpad, av_media_type_get_string(dst-> input_pads[dstpad].type));
         return AVERROR(EINVAL);
     }
 
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index b8b432e98b..fa971734d1 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -220,7 +220,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->input_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
@@ -230,7 +230,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->output_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index b89a680883..b09f5698fb 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -113,7 +113,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
         if (ret < 0) {
             av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
-                   av_get_media_type_string(type), stream_id);
+                   av_media_type_get_string(type), stream_id);
             return NULL;
         }
         return avf->streams[ret];
@@ -148,7 +148,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
         av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
                "currently unsupported by libavfilter\n", spec,
-               av_get_media_type_string(found->codecpar->codec_type));
+               av_media_type_get_string(found->codecpar->codec_type));
         return NULL;
     }
     return found;
@@ -426,7 +426,7 @@ static char *describe_frame_to_str(char *dst, size_t dst_size,
                  frame->nb_samples);
                  break;
     default:
-        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
+        snprintf(dst, dst_size, "%s BUG", av_media_type_get_string(frame_type));
         break;
     }
     return dst;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index be2493ce55..ec72a74eec 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -473,7 +473,7 @@ static int avi_write_header(AVFormatContext *s)
             default:
                 av_log(s, AV_LOG_ERROR,
                     "Invalid or not supported codec type '%s' found in the input\n",
-                    (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
+                    av_media_type_get_string(par->codec_type));
                 return AVERROR(EINVAL);
             }
             ff_end_tag(pb, strf);
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 66c530a2ff..1267ed00a8 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -670,7 +670,7 @@ static int flv_init(struct AVFormatContext *s)
             break;
         default:
             av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
-                   av_get_media_type_string(par->codec_type), i);
+                   av_media_type_get_string(par->codec_type), i);
             return AVERROR(EINVAL);
         }
         avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
diff --git a/libavformat/framehash.c b/libavformat/framehash.c
index 04c40825b9..b9f765f097 100644
--- a/libavformat/framehash.c
+++ b/libavformat/framehash.c
@@ -32,7 +32,7 @@ int ff_framehash_write_header(AVFormatContext *s)
         AVCodecParameters *avctx = st->codecpar;
         char buf[256] = { 0 };
         avio_printf(s->pb, "#tb %d: %d/%d\n", i, st->time_base.num, st->time_base.den);
-        avio_printf(s->pb, "#media_type %d: %s\n", i, av_get_media_type_string(avctx->codec_type));
+        avio_printf(s->pb, "#media_type %d: %s\n", i, av_media_type_get_string(avctx->codec_type));
         avio_printf(s->pb, "#codec_id %d: %s\n", i, avcodec_get_name(avctx->codec_id));
         switch (avctx->codec_type) {
         case AVMEDIA_TYPE_AUDIO:
diff --git a/libavformat/mov.c b/libavformat/mov.c
index a80fcc1606..b2041ec160 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7141,7 +7141,7 @@ static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     av_log(ctx, AV_LOG_TRACE,
            "%s stream %d KindBox(scheme: %s, value: %s)\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            st->index,
            scheme_buf.str, value_buf.str);
 
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 64248695bd..cf1c89a04b 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -456,7 +456,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
             break;
         default:
             av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
-                   av_get_media_type_string(st->codecpar->codec_type), i);
+                   av_media_type_get_string(st->codecpar->codec_type), i);
             return AVERROR(EINVAL);
         }
         stream->fifo = av_fifo_alloc2(16, 1, 0);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index e23f596490..dfb898d07b 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2253,7 +2253,7 @@ static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int p
     if (found) {
         av_log(ts->stream, AV_LOG_VERBOSE,
                "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
-               av_get_media_type_string(found->codecpar->codec_type),
+               av_media_type_get_string(found->codecpar->codec_type),
                found->index, found->id, pid);
     }
 
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index b85c10bf19..5763c5db8b 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -2404,7 +2404,7 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
         av_dict_set(&st->metadata, "track_name", track->name, 0);
 
     codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &track->sequence->data_definition_ul);
-    av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
+    av_dict_set(&st->metadata, "data_type", av_media_type_get_string(codec_ul->id), 0);
     return 0;
 }
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index e9b0aa4fa8..2fac2647a1 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -737,7 +737,7 @@ static int seg_init(AVFormatContext *s)
         return ret;
     av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
            seg->reference_stream_index,
-           av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
+           av_media_type_get_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
 
     seg->oformat = av_guess_format(seg->format, s->url, NULL);
 
diff --git a/libavformat/tee.c b/libavformat/tee.c
index b3bcd70b9f..97db7c789b 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -419,7 +419,7 @@ static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
 
         av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
                i, avcodec_get_name(st->codecpar->codec_id),
-               av_get_media_type_string(st->codecpar->codec_type));
+               av_media_type_get_string(st->codecpar->codec_type));
 
         bsf_name = bsf->filter->priv_class ?
                    bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
diff --git a/libavformat/uncodedframecrcenc.c b/libavformat/uncodedframecrcenc.c
index e30fe4ddb9..04e8818711 100644
--- a/libavformat/uncodedframecrcenc.c
+++ b/libavformat/uncodedframecrcenc.c
@@ -130,7 +130,6 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     AVBPrint bp;
     int ret = 0;
     enum AVMediaType type;
-    const char *type_name;
 
     if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
         return 0;
@@ -139,8 +138,7 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     av_bprintf(&bp, "%d, %10"PRId64"",
                stream_index, (*frame)->pts);
     type = s->streams[stream_index]->codecpar->codec_type;
-    type_name = av_get_media_type_string(type);
-    av_bprintf(&bp, ", %s", type_name ? type_name : "unknown");
+    av_bprintf(&bp, ", %s", av_media_type_get_string(type));
     switch (type) {
         case AVMEDIA_TYPE_VIDEO:
             video_frame_cksum(&bp, *frame);
-- 
2.32.0

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-02  1:58   ` Scott Theisen
  2022-02-02  2:06     ` James Almer
  2022-02-02  2:13     ` Andreas Rheinhardt
@ 2022-02-23  7:52     ` Anton Khirnov
  2022-02-28 21:06       ` Scott Theisen
  2 siblings, 1 reply; 19+ messages in thread
From: Anton Khirnov @ 2022-02-23  7:52 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

Quoting Andreas Rheinhardt (2022-02-02 03:13:12)
> Who sets invalid media types?
> (In case of fftools/*: If they don't set it themselves, they (and all
> our users) should be able to rely on our libraries to not set invalid
> values. The same goes for all demuxers (as they set this value
> themselves). Checks are only necessary where the media type comes from
> the user; this means muxers (where the check should be done generically)
> and possibly avfiltergraph.c (I am pretty sure that the type has already
> been checked earlier for filters).)

Some demuxer code in lavf looks like it might result in a TYPE_UNKNOWN
AVStream. E.g. argo_brp, avi, both asfdecs, gxf.

-- 
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* Re: [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL
  2022-02-23  7:52     ` Anton Khirnov
@ 2022-02-28 21:06       ` Scott Theisen
  0 siblings, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-02-28 21:06 UTC (permalink / raw)
  To: ffmpeg-devel

On 2/23/22 02:52, Anton Khirnov wrote:
> Quoting Andreas Rheinhardt (2022-02-02 03:13:12)
>> Who sets invalid media types?
>> (In case of fftools/*: If they don't set it themselves, they (and all
>> our users) should be able to rely on our libraries to not set invalid
>> values. The same goes for all demuxers (as they set this value
>> themselves). Checks are only necessary where the media type comes from
>> the user; this means muxers (where the check should be done generically)
>> and possibly avfiltergraph.c (I am pretty sure that the type has already
>> been checked earlier for filters).)
> Some demuxer code in lavf looks like it might result in a TYPE_UNKNOWN
> AVStream. E.g. argo_brp, avi, both asfdecs, gxf.

Also, in libavcodec/codec_desc.c, avcodec_get_type(AV_CODEC_ID_NONE) 
will return AVMEDIA_TYPE_UNKNOWN.  The mpegts demuxer, for example, will 
set (or more likely leave) AV_CODEC_ID_NONE as the AVCodecID of a stream 
if it can't identify the codec.  This is probably true of all demuxers.

-Scott Theisen
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string()
  2022-02-01 22:30 [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL Scott Theisen
                   ` (2 preceding siblings ...)
  2022-02-13 21:52 ` [FFmpeg-devel] [PATCH v3] create av_media_type_get_string() Scott Theisen
@ 2022-05-15 19:55 ` Scott Theisen
  2022-05-15 19:55   ` [FFmpeg-devel] [PATCH v4 1/2] libavutil: " Scott Theisen
  2022-05-15 19:55   ` [FFmpeg-devel] [PATCH v4 2/2] av_get_media_type_string(): replace with av_media_type_get_string() Scott Theisen
  2022-06-01 19:22 ` [FFmpeg-devel] [PATCH] " Scott Theisen
  4 siblings, 2 replies; 19+ messages in thread
From: Scott Theisen @ 2022-05-15 19:55 UTC (permalink / raw)
  To: ffmpeg-devel

printf %s with NULL is undefined behavior.

This version adds #include "attributes.h" for -Wdeprecated-declarations.

-Scott Theisen


_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v4 1/2] libavutil: create av_media_type_get_string()
  2022-05-15 19:55 ` [FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string() Scott Theisen
@ 2022-05-15 19:55   ` Scott Theisen
  2022-05-15 19:55   ` [FFmpeg-devel] [PATCH v4 2/2] av_get_media_type_string(): replace with av_media_type_get_string() Scott Theisen
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-05-15 19:55 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

This deprecates av_get_media_type_string() which does return NULL.

printf %s with a null pointer is undefined behavior

This also matches behavior with avcodec_get_name() which never returns NULL,
instead it returns "unknown_codec".
---
 doc/APIchanges     |  3 +++
 libavutil/avutil.h | 10 ++++++++++
 libavutil/utils.c  | 13 +++++++++++++
 3 files changed, 26 insertions(+)

diff --git a/doc/APIchanges b/doc/APIchanges
index 1a9f0a303e..53f4378a50 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-05-XX - xxxxxxxxxxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-03-16 - xxxxxxxxxx - all libraries - version_major.h
   Add lib<name>/version_major.h as new installed headers, which only
   contain the major version number (and corresponding API deprecation
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 4d633156d1..0f03080dcb 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -21,6 +21,8 @@
 #ifndef AVUTIL_AVUTIL_H
 #define AVUTIL_AVUTIL_H
 
+#include "attributes.h"
+
 /**
  * @file
  * @ingroup lavu
@@ -209,9 +211,17 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
+attribute_deprecated
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index 94d247bbee..5e0be8361f 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -37,6 +37,19 @@ const char *av_get_media_type_string(enum AVMediaType media_type)
     }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+    switch (media_type) {
+    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
+    case AVMEDIA_TYPE_VIDEO:      return "video";
+    case AVMEDIA_TYPE_AUDIO:      return "audio";
+    case AVMEDIA_TYPE_DATA:       return "data";
+    case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+    case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+    default:                      return "invalid_media_type";
+    }
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
     switch (pict_type) {
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v4 2/2] av_get_media_type_string(): replace with av_media_type_get_string()
  2022-05-15 19:55 ` [FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string() Scott Theisen
  2022-05-15 19:55   ` [FFmpeg-devel] [PATCH v4 1/2] libavutil: " Scott Theisen
@ 2022-05-15 19:55   ` Scott Theisen
  1 sibling, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-05-15 19:55 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

printf %s with a null pointer is undefined behavior.
---
 doc/examples/demuxing_decoding.c | 10 +++++-----
 doc/examples/extract_mvs.c       |  4 ++--
 fftools/ffmpeg.c                 | 12 ++++++------
 fftools/ffmpeg_mux.c             |  2 +-
 fftools/ffmpeg_opt.c             |  2 +-
 fftools/ffplay.c                 |  4 ++--
 fftools/ffprobe.c                | 13 +++----------
 fftools/opt_common.c             |  7 +++----
 libavcodec/avcodec.c             |  2 +-
 libavcodec/tests/avcodec.c       | 10 ++--------
 libavdevice/dshow.c              |  4 ++--
 libavfilter/avfilter.c           |  4 ++--
 libavfilter/avfiltergraph.c      |  4 ++--
 libavfilter/src_movie.c          |  6 +++---
 libavformat/avienc.c             |  2 +-
 libavformat/flvenc.c             |  2 +-
 libavformat/framehash.c          |  2 +-
 libavformat/mov.c                |  2 +-
 libavformat/mpegenc.c            |  2 +-
 libavformat/mpegts.c             |  2 +-
 libavformat/mxfdec.c             |  2 +-
 libavformat/segment.c            |  2 +-
 libavformat/tee.c                |  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 24 files changed, 45 insertions(+), 61 deletions(-)

diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 999a78db0d..467eda8a51 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
         dec = avcodec_find_decoder(st->codecpar->codec_id);
         if (!dec) {
             fprintf(stderr, "Failed to find %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(EINVAL);
         }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
         *dec_ctx = avcodec_alloc_context3(dec);
         if (!*dec_ctx) {
             fprintf(stderr, "Failed to allocate the %s codec context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(ENOMEM);
         }
 
         /* Copy codec parameters from input stream to output codec context */
         if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
             fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
         /* Init the decoders */
         if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
         *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
         av_dict_free(&opts);
         if (ret < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index a85ed18b08..0382c80bd1 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1447,7 +1447,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ist->nb_packets;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-                   i, j, av_get_media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
                    ist->nb_packets, ist->data_size);
 
@@ -1481,7 +1481,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ost->packets_written;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
-                   i, j, av_get_media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             if (ost->encoding_needed) {
                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
                        ost->frames_encoded);
@@ -3321,7 +3321,7 @@ static void report_new_stream(int input_index, AVPacket *pkt)
         return;
     av_log(file->ctx, AV_LOG_WARNING,
            "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            input_index, pkt->stream_index,
            pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
     file->nb_streams_warn = pkt->stream_index + 1;
@@ -4055,7 +4055,7 @@ static int process_input(int file_index)
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
                "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
                av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
@@ -4183,7 +4183,7 @@ static int process_input(int file_index)
                        "timestamp discontinuity for stream #%d:%d "
                        "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
                        ist->file_index, ist->st->index, ist->st->id,
-                       av_get_media_type_string(ist->dec_ctx->codec_type),
+                       av_media_type_get_string(ist->dec_ctx->codec_type),
                        delta, ifile->ts_offset);
                 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                 if (pkt->pts != AV_NOPTS_VALUE)
@@ -4212,7 +4212,7 @@ static int process_input(int file_index)
 
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
                av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base),
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 3cdaa494d7..234819e942 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -170,7 +170,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
-                av_get_media_type_string(ost->enc_ctx->codec_type),
+                av_media_type_get_string(ost->enc_ctx->codec_type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 47e8b9b7bd..890476c46b 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1704,7 +1704,7 @@ static void check_streamcopy_filters(OptionsContext *o, AVFormatContext *oc,
                "Filtering and streamcopy cannot be used together.\n",
                ost->filters ? "Filtergraph" : "Filtergraph script",
                ost->filters ? ost->filters : ost->filters_script,
-               av_get_media_type_string(type), ost->file_index, ost->index);
+               av_media_type_get_string(type), ost->file_index, ost->index);
         exit_program(1);
     }
 }
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index 040afa0189..c44c5e4ea6 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -2860,7 +2860,7 @@ static int read_thread(void *arg)
     }
     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
         if (wanted_stream_spec[i] && st_index[i] == -1) {
-            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_get_media_type_string(i));
+            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_media_type_get_string(i));
             st_index[i] = INT_MAX;
         }
     }
@@ -3191,7 +3191,7 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
     if (p && stream_index != -1)
         stream_index = p->stream_index[stream_index];
     av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
-           av_get_media_type_string(codec_type),
+           av_media_type_get_string(codec_type),
            old_index,
            stream_index);
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index c51c82ff65..d5590be133 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -2404,15 +2404,12 @@ static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int p
     char val_str[128];
     AVStream *st = ifile->streams[pkt->stream_index].st;
     AVBPrint pbuf;
-    const char *s;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
     writer_print_section_header(w, SECTION_ID_PACKET);
 
-    s = av_get_media_type_string(st->codecpar->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(st->codecpar->codec_type));
     print_int("stream_index",     pkt->stream_index);
     print_ts  ("pts",             pkt->pts);
     print_time("pts_time",        pkt->pts, &st->time_base);
@@ -2487,9 +2484,7 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
 
     writer_print_section_header(w, SECTION_ID_FRAME);
 
-    s = av_get_media_type_string(stream->codecpar->codec_type);
-    if (s) print_str    ("media_type", s);
-    else   print_str_opt("media_type", "unknown");
+    print_str("media_type", av_media_type_get_string(stream->codecpar->codec_type));
     print_int("stream_index",           stream->index);
     print_int("key_frame",              frame->key_frame);
     print_ts  ("pts",                   frame->pts);
@@ -2890,9 +2885,7 @@ static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_id
             print_str_opt("profile", "unknown");
     }
 
-    s = av_get_media_type_string(par->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(par->codec_type));
 
     /* print AVI/FourCC tag */
     print_str("codec_tag_string",    av_fourcc2str(par->codec_tag));
diff --git a/fftools/opt_common.c b/fftools/opt_common.c
index c303db4d09..064bbec002 100644
--- a/fftools/opt_common.c
+++ b/fftools/opt_common.c
@@ -519,7 +519,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 0);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-               av_get_media_type_string(avfilter_pad_get_type(f->inputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -530,7 +530,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 1);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
-               av_get_media_type_string(avfilter_pad_get_type(f->outputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->outputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -1308,10 +1308,9 @@ static void print_device_list(const AVDeviceInfoList *device_list)
             device->device_name, device->device_description);
         if (device->nb_media_types > 0) {
             for (int j = 0; j < device->nb_media_types; ++j) {
-                const char* media_type = av_get_media_type_string(device->media_types[j]);
                 if (j > 0)
                     printf(", ");
-                printf("%s", media_type ? media_type : "unknown");
+                printf("%s", av_media_type_get_string(device->media_types[j]));
             }
         } else {
             printf("none");
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 5f6e71a39e..e02ac2e630 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -528,7 +528,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
     if (!buf || buf_size <= 0)
         return;
     av_bprint_init_for_buffer(&bprint, buf, buf_size);
-    codec_type = av_get_media_type_string(enc->codec_type);
+    codec_type = av_media_type_get_string(enc->codec_type);
     codec_name = avcodec_get_name(enc->codec_id);
     profile = avcodec_profile_name(enc->codec_id, enc->profile);
 
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index 08b5fbede1..c10a90fa92 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -22,12 +22,6 @@
 #include "libavcodec/codec_internal.h"
 #include "libavcodec/internal.h"
 
-static const char *get_type_string(enum AVMediaType type)
-{
-    const char *ret = av_get_media_type_string(type);
-    return ret ? ret : "unknown";
-}
-
 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
 #define ERR_INTERNAL(msg, ...)                                  \
 do {                                                            \
@@ -76,7 +70,7 @@ int main(void){
             codec->type != AVMEDIA_TYPE_AUDIO &&
             codec->type != AVMEDIA_TYPE_SUBTITLE)
             ERR_EXT("Codec %s has unsupported type %s\n",
-                    get_type_string(codec->type));
+                    av_media_type_get_string(codec->type));
         if (codec->type != AVMEDIA_TYPE_AUDIO) {
             if (codec->ch_layouts || codec->sample_fmts ||
                 codec->supported_samplerates)
@@ -179,7 +173,7 @@ int main(void){
         } else if (desc->type != codec->type)
             ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
                     "%s vs %s\n",
-                    get_type_string(codec->type), get_type_string(desc->type));
+                    av_media_type_get_string(codec->type), av_media_type_get_string(desc->type));
     }
     return ret;
 }
diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 5946a72cc2..7b36ea4bc9 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -577,10 +577,10 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
             else {
                 av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
                 if (nb_media_types > 0) {
-                    const char* media_type = av_get_media_type_string(media_types[0]);
+                    const char* media_type = av_media_type_get_string(media_types[0]);
                     av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
                     for (int i = 1; i < nb_media_types; ++i) {
-                        media_type = av_get_media_type_string(media_types[i]);
+                        media_type = av_media_type_get_string(media_types[i]);
                         av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
                     }
                     av_log(avctx, AV_LOG_INFO, ")");
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 965f5d0f63..b9be2eb6fe 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -153,8 +153,8 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
         av_log(src, AV_LOG_ERROR,
                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
-               src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
-               dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
+               src->name, srcpad, av_media_type_get_string(src->output_pads[srcpad].type),
+               dst->name, dstpad, av_media_type_get_string(dst-> input_pads[dstpad].type));
         return AVERROR(EINVAL);
     }
 
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index b7dbfc063b..b466c5c55c 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -218,7 +218,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->input_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
@@ -228,7 +228,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->output_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index 711854c23c..6484005921 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -115,7 +115,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
         if (ret < 0) {
             av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
-                   av_get_media_type_string(type), stream_id);
+                   av_media_type_get_string(type), stream_id);
             return NULL;
         }
         return avf->streams[ret];
@@ -150,7 +150,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
         av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
                "currently unsupported by libavfilter\n", spec,
-               av_get_media_type_string(found->codecpar->codec_type));
+               av_media_type_get_string(found->codecpar->codec_type));
         return NULL;
     }
     return found;
@@ -429,7 +429,7 @@ static char *describe_frame_to_str(char *dst, size_t dst_size,
                  frame->nb_samples);
                  break;
     default:
-        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
+        snprintf(dst, dst_size, "%s BUG", av_media_type_get_string(frame_type));
         break;
     }
     return dst;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 2264241d57..7a4e73807c 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -475,7 +475,7 @@ static int avi_write_header(AVFormatContext *s)
             default:
                 av_log(s, AV_LOG_ERROR,
                     "Invalid or not supported codec type '%s' found in the input\n",
-                    (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
+                    av_media_type_get_string(par->codec_type));
                 return AVERROR(EINVAL);
             }
             ff_end_tag(pb, strf);
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 4e65ba4066..8324d51db6 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -669,7 +669,7 @@ static int flv_init(struct AVFormatContext *s)
             break;
         default:
             av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
-                   av_get_media_type_string(par->codec_type), i);
+                   av_media_type_get_string(par->codec_type), i);
             return AVERROR(EINVAL);
         }
         avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
diff --git a/libavformat/framehash.c b/libavformat/framehash.c
index 654996e8e0..07c29da82c 100644
--- a/libavformat/framehash.c
+++ b/libavformat/framehash.c
@@ -33,7 +33,7 @@ int ff_framehash_write_header(AVFormatContext *s)
         AVCodecParameters *avctx = st->codecpar;
         char buf[256] = { 0 };
         avio_printf(s->pb, "#tb %d: %d/%d\n", i, st->time_base.num, st->time_base.den);
-        avio_printf(s->pb, "#media_type %d: %s\n", i, av_get_media_type_string(avctx->codec_type));
+        avio_printf(s->pb, "#media_type %d: %s\n", i, av_media_type_get_string(avctx->codec_type));
         avio_printf(s->pb, "#codec_id %d: %s\n", i, avcodec_get_name(avctx->codec_id));
         switch (avctx->codec_type) {
         case AVMEDIA_TYPE_AUDIO: {
diff --git a/libavformat/mov.c b/libavformat/mov.c
index d7be593a86..9514598718 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7306,7 +7306,7 @@ static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     av_log(ctx, AV_LOG_TRACE,
            "%s stream %d KindBox(scheme: %s, value: %s)\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            st->index,
            scheme_buf.str, value_buf.str);
 
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 62692bfcd1..3e44663715 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -458,7 +458,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
             break;
         default:
             av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
-                   av_get_media_type_string(st->codecpar->codec_type), i);
+                   av_media_type_get_string(st->codecpar->codec_type), i);
             return AVERROR(EINVAL);
         }
         stream->fifo = av_fifo_alloc2(16, 1, 0);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 6e761c07f1..13292f8041 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2258,7 +2258,7 @@ static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int p
     if (found) {
         av_log(ts->stream, AV_LOG_VERBOSE,
                "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
-               av_get_media_type_string(found->codecpar->codec_type),
+               av_media_type_get_string(found->codecpar->codec_type),
                found->index, found->id, pid);
     }
 
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 6a22c33995..0af77c681e 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -2418,7 +2418,7 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
         av_dict_set(&st->metadata, "track_name", track->name, 0);
 
     codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &track->sequence->data_definition_ul);
-    av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
+    av_dict_set(&st->metadata, "data_type", av_media_type_get_string(codec_ul->id), 0);
     return 0;
 }
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index fa435d9f32..231d845ab3 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -738,7 +738,7 @@ static int seg_init(AVFormatContext *s)
         return ret;
     av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
            seg->reference_stream_index,
-           av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
+           av_media_type_get_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
 
     seg->oformat = av_guess_format(seg->format, s->url, NULL);
 
diff --git a/libavformat/tee.c b/libavformat/tee.c
index f1f2a9d2a5..3144ab4000 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -419,7 +419,7 @@ static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
 
         av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
                i, avcodec_get_name(st->codecpar->codec_id),
-               av_get_media_type_string(st->codecpar->codec_type));
+               av_media_type_get_string(st->codecpar->codec_type));
 
         bsf_name = bsf->filter->priv_class ?
                    bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
diff --git a/libavformat/uncodedframecrcenc.c b/libavformat/uncodedframecrcenc.c
index 99990616d3..c6476a4c43 100644
--- a/libavformat/uncodedframecrcenc.c
+++ b/libavformat/uncodedframecrcenc.c
@@ -131,7 +131,6 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     AVBPrint bp;
     int ret = 0;
     enum AVMediaType type;
-    const char *type_name;
 
     if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
         return 0;
@@ -140,8 +139,7 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     av_bprintf(&bp, "%d, %10"PRId64"",
                stream_index, (*frame)->pts);
     type = s->streams[stream_index]->codecpar->codec_type;
-    type_name = av_get_media_type_string(type);
-    av_bprintf(&bp, ", %s", type_name ? type_name : "unknown");
+    av_bprintf(&bp, ", %s", av_media_type_get_string(type));
     switch (type) {
         case AVMEDIA_TYPE_VIDEO:
             video_frame_cksum(&bp, *frame);
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH] av_get_media_type_string(): replace with av_media_type_get_string()
  2022-02-01 22:30 [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL Scott Theisen
                   ` (3 preceding siblings ...)
  2022-05-15 19:55 ` [FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string() Scott Theisen
@ 2022-06-01 19:22 ` Scott Theisen
  2022-09-16 17:32   ` [FFmpeg-devel] [PATCH v2] " Scott Theisen
  4 siblings, 1 reply; 19+ messages in thread
From: Scott Theisen @ 2022-06-01 19:22 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

printf %s with a NULL pointer is undefined behavior.  Not returning
a NULL pointer makes for a nicer function for API users, including
in FFmpeg, since then you don't have to check it, instead you can
just print it.  32/44 uses in FFmpeg did not check for NULL.

This also matches behavior with avcodec_get_name() which never
returns NULL, instead it returns "unknown_codec".

In libavcodec/codec_desc.c, avcodec_get_type(AV_CODEC_ID_NONE) will
return AVMEDIA_TYPE_UNKNOWN.

The mpegts demuxer, for example, will set AV_CODEC_ID_NONE as the
AVCodecID of a stream if it can't identify the codec.  This is
probably true of all demuxers.

Therefore, AVMEDIA_TYPE_UNKNOWN is a valid value for the AVMediaType
enum and this should be differentiated from invalid values by
returning a different string for each case.
---
 doc/APIchanges                   |  3 +++
 doc/examples/demuxing_decoding.c | 10 +++++-----
 doc/examples/extract_mvs.c       |  4 ++--
 fftools/ffmpeg.c                 | 14 +++++++-------
 fftools/ffmpeg_mux.c             |  2 +-
 fftools/ffmpeg_opt.c             |  2 +-
 fftools/ffplay.c                 |  4 ++--
 fftools/ffprobe.c                | 13 +++----------
 fftools/opt_common.c             |  7 +++----
 libavcodec/avcodec.c             |  2 +-
 libavcodec/tests/avcodec.c       | 10 ++--------
 libavdevice/dshow.c              |  4 ++--
 libavfilter/avfilter.c           |  4 ++--
 libavfilter/avfiltergraph.c      |  4 ++--
 libavfilter/src_movie.c          |  6 +++---
 libavformat/avienc.c             |  2 +-
 libavformat/flvenc.c             |  2 +-
 libavformat/framehash.c          |  2 +-
 libavformat/mov.c                |  2 +-
 libavformat/mpegenc.c            |  2 +-
 libavformat/mpegts.c             |  2 +-
 libavformat/mxfdec.c             |  2 +-
 libavformat/segment.c            |  2 +-
 libavformat/tee.c                |  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 libavutil/avutil.h               | 10 ++++++++++
 libavutil/utils.c                | 13 +++++++++++++
 27 files changed, 72 insertions(+), 62 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 337f1466d8..f3ed86b9b2 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-06-XX - xxxxxxxxxxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-05-23 - xxxxxxxxx - lavu 57.25.100 - avutil.h
   Deprecate av_fopen_utf8() without replacement.
 
diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 999a78db0d..467eda8a51 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
         dec = avcodec_find_decoder(st->codecpar->codec_id);
         if (!dec) {
             fprintf(stderr, "Failed to find %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(EINVAL);
         }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
         *dec_ctx = avcodec_alloc_context3(dec);
         if (!*dec_ctx) {
             fprintf(stderr, "Failed to allocate the %s codec context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(ENOMEM);
         }
 
         /* Copy codec parameters from input stream to output codec context */
         if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
             fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
         /* Init the decoders */
         if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
         *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
         av_dict_free(&opts);
         if (ret < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5ed287c522..e5874dc218 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -894,7 +894,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 {
     AVCodecContext   *enc = ost->enc_ctx;
     AVPacket         *pkt = ost->pkt;
-    const char *type_desc = av_get_media_type_string(enc->codec_type);
+    const char *type_desc = av_media_type_get_string(enc->codec_type);
     const char    *action = frame ? "encode" : "flush";
     int ret;
 
@@ -1443,7 +1443,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ist->nb_packets;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-                   i, j, av_get_media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
                    ist->nb_packets, ist->data_size);
 
@@ -1477,7 +1477,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ost->packets_written;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
-                   i, j, av_get_media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             if (ost->encoding_needed) {
                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
                        ost->frames_encoded);
@@ -3267,7 +3267,7 @@ static void report_new_stream(int input_index, AVPacket *pkt)
         return;
     av_log(file->ctx, AV_LOG_WARNING,
            "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            input_index, pkt->stream_index,
            pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
     file->nb_streams_warn = pkt->stream_index + 1;
@@ -4001,7 +4001,7 @@ static int process_input(int file_index)
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
                "next_dts:%s next_dts_time:%s next_pts:%s next_pts_time:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(ist->next_dts), av_ts2timestr(ist->next_dts, &AV_TIME_BASE_Q),
                av_ts2str(ist->next_pts), av_ts2timestr(ist->next_pts, &AV_TIME_BASE_Q),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
@@ -4129,7 +4129,7 @@ static int process_input(int file_index)
                        "timestamp discontinuity for stream #%d:%d "
                        "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
                        ist->file_index, ist->st->index, ist->st->id,
-                       av_get_media_type_string(ist->dec_ctx->codec_type),
+                       av_media_type_get_string(ist->dec_ctx->codec_type),
                        delta, ifile->ts_offset);
                 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                 if (pkt->pts != AV_NOPTS_VALUE)
@@ -4158,7 +4158,7 @@ static int process_input(int file_index)
 
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
-               ifile->ist_index + pkt->stream_index, av_get_media_type_string(ist->dec_ctx->codec_type),
+               ifile->ist_index + pkt->stream_index, av_media_type_get_string(ist->dec_ctx->codec_type),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
                av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base),
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 794d580635..eb0cd3a045 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -157,7 +157,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
-                av_get_media_type_string(ost->enc_ctx->codec_type),
+                av_media_type_get_string(ost->enc_ctx->codec_type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 2c1b3bd0dd..48c4f29d57 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1705,7 +1705,7 @@ static void check_streamcopy_filters(OptionsContext *o, AVFormatContext *oc,
                "Filtering and streamcopy cannot be used together.\n",
                ost->filters ? "Filtergraph" : "Filtergraph script",
                ost->filters ? ost->filters : ost->filters_script,
-               av_get_media_type_string(type), ost->file_index, ost->index);
+               av_media_type_get_string(type), ost->file_index, ost->index);
         exit_program(1);
     }
 }
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index 040afa0189..c44c5e4ea6 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -2860,7 +2860,7 @@ static int read_thread(void *arg)
     }
     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
         if (wanted_stream_spec[i] && st_index[i] == -1) {
-            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_get_media_type_string(i));
+            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_media_type_get_string(i));
             st_index[i] = INT_MAX;
         }
     }
@@ -3191,7 +3191,7 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
     if (p && stream_index != -1)
         stream_index = p->stream_index[stream_index];
     av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
-           av_get_media_type_string(codec_type),
+           av_media_type_get_string(codec_type),
            old_index,
            stream_index);
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index c51c82ff65..d5590be133 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -2404,15 +2404,12 @@ static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int p
     char val_str[128];
     AVStream *st = ifile->streams[pkt->stream_index].st;
     AVBPrint pbuf;
-    const char *s;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
     writer_print_section_header(w, SECTION_ID_PACKET);
 
-    s = av_get_media_type_string(st->codecpar->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(st->codecpar->codec_type));
     print_int("stream_index",     pkt->stream_index);
     print_ts  ("pts",             pkt->pts);
     print_time("pts_time",        pkt->pts, &st->time_base);
@@ -2487,9 +2484,7 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
 
     writer_print_section_header(w, SECTION_ID_FRAME);
 
-    s = av_get_media_type_string(stream->codecpar->codec_type);
-    if (s) print_str    ("media_type", s);
-    else   print_str_opt("media_type", "unknown");
+    print_str("media_type", av_media_type_get_string(stream->codecpar->codec_type));
     print_int("stream_index",           stream->index);
     print_int("key_frame",              frame->key_frame);
     print_ts  ("pts",                   frame->pts);
@@ -2890,9 +2885,7 @@ static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_id
             print_str_opt("profile", "unknown");
     }
 
-    s = av_get_media_type_string(par->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(par->codec_type));
 
     /* print AVI/FourCC tag */
     print_str("codec_tag_string",    av_fourcc2str(par->codec_tag));
diff --git a/fftools/opt_common.c b/fftools/opt_common.c
index ae5e28a5af..48bedb6440 100644
--- a/fftools/opt_common.c
+++ b/fftools/opt_common.c
@@ -522,7 +522,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 0);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-               av_get_media_type_string(avfilter_pad_get_type(f->inputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -533,7 +533,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 1);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
-               av_get_media_type_string(avfilter_pad_get_type(f->outputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->outputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -1313,10 +1313,9 @@ static void print_device_list(const AVDeviceInfoList *device_list)
             device->device_name, device->device_description);
         if (device->nb_media_types > 0) {
             for (int j = 0; j < device->nb_media_types; ++j) {
-                const char* media_type = av_get_media_type_string(device->media_types[j]);
                 if (j > 0)
                     printf(", ");
-                printf("%s", media_type ? media_type : "unknown");
+                printf("%s", av_media_type_get_string(device->media_types[j]));
             }
         } else {
             printf("none");
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 5f6e71a39e..e02ac2e630 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -528,7 +528,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
     if (!buf || buf_size <= 0)
         return;
     av_bprint_init_for_buffer(&bprint, buf, buf_size);
-    codec_type = av_get_media_type_string(enc->codec_type);
+    codec_type = av_media_type_get_string(enc->codec_type);
     codec_name = avcodec_get_name(enc->codec_id);
     profile = avcodec_profile_name(enc->codec_id, enc->profile);
 
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index 08b5fbede1..c10a90fa92 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -22,12 +22,6 @@
 #include "libavcodec/codec_internal.h"
 #include "libavcodec/internal.h"
 
-static const char *get_type_string(enum AVMediaType type)
-{
-    const char *ret = av_get_media_type_string(type);
-    return ret ? ret : "unknown";
-}
-
 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
 #define ERR_INTERNAL(msg, ...)                                  \
 do {                                                            \
@@ -76,7 +70,7 @@ int main(void){
             codec->type != AVMEDIA_TYPE_AUDIO &&
             codec->type != AVMEDIA_TYPE_SUBTITLE)
             ERR_EXT("Codec %s has unsupported type %s\n",
-                    get_type_string(codec->type));
+                    av_media_type_get_string(codec->type));
         if (codec->type != AVMEDIA_TYPE_AUDIO) {
             if (codec->ch_layouts || codec->sample_fmts ||
                 codec->supported_samplerates)
@@ -179,7 +173,7 @@ int main(void){
         } else if (desc->type != codec->type)
             ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
                     "%s vs %s\n",
-                    get_type_string(codec->type), get_type_string(desc->type));
+                    av_media_type_get_string(codec->type), av_media_type_get_string(desc->type));
     }
     return ret;
 }
diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 5946a72cc2..7b36ea4bc9 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -577,10 +577,10 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
             else {
                 av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
                 if (nb_media_types > 0) {
-                    const char* media_type = av_get_media_type_string(media_types[0]);
+                    const char* media_type = av_media_type_get_string(media_types[0]);
                     av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
                     for (int i = 1; i < nb_media_types; ++i) {
-                        media_type = av_get_media_type_string(media_types[i]);
+                        media_type = av_media_type_get_string(media_types[i]);
                         av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
                     }
                     av_log(avctx, AV_LOG_INFO, ")");
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index 965f5d0f63..b9be2eb6fe 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -153,8 +153,8 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
         av_log(src, AV_LOG_ERROR,
                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
-               src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
-               dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
+               src->name, srcpad, av_media_type_get_string(src->output_pads[srcpad].type),
+               dst->name, dstpad, av_media_type_get_string(dst-> input_pads[dstpad].type));
         return AVERROR(EINVAL);
     }
 
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index b7dbfc063b..b466c5c55c 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -218,7 +218,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->input_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
@@ -228,7 +228,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->output_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index 711854c23c..6484005921 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -115,7 +115,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
         if (ret < 0) {
             av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
-                   av_get_media_type_string(type), stream_id);
+                   av_media_type_get_string(type), stream_id);
             return NULL;
         }
         return avf->streams[ret];
@@ -150,7 +150,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
         av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
                "currently unsupported by libavfilter\n", spec,
-               av_get_media_type_string(found->codecpar->codec_type));
+               av_media_type_get_string(found->codecpar->codec_type));
         return NULL;
     }
     return found;
@@ -429,7 +429,7 @@ static char *describe_frame_to_str(char *dst, size_t dst_size,
                  frame->nb_samples);
                  break;
     default:
-        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
+        snprintf(dst, dst_size, "%s BUG", av_media_type_get_string(frame_type));
         break;
     }
     return dst;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 2264241d57..7a4e73807c 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -475,7 +475,7 @@ static int avi_write_header(AVFormatContext *s)
             default:
                 av_log(s, AV_LOG_ERROR,
                     "Invalid or not supported codec type '%s' found in the input\n",
-                    (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
+                    av_media_type_get_string(par->codec_type));
                 return AVERROR(EINVAL);
             }
             ff_end_tag(pb, strf);
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 4e65ba4066..8324d51db6 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -669,7 +669,7 @@ static int flv_init(struct AVFormatContext *s)
             break;
         default:
             av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
-                   av_get_media_type_string(par->codec_type), i);
+                   av_media_type_get_string(par->codec_type), i);
             return AVERROR(EINVAL);
         }
         avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
diff --git a/libavformat/framehash.c b/libavformat/framehash.c
index 654996e8e0..07c29da82c 100644
--- a/libavformat/framehash.c
+++ b/libavformat/framehash.c
@@ -33,7 +33,7 @@ int ff_framehash_write_header(AVFormatContext *s)
         AVCodecParameters *avctx = st->codecpar;
         char buf[256] = { 0 };
         avio_printf(s->pb, "#tb %d: %d/%d\n", i, st->time_base.num, st->time_base.den);
-        avio_printf(s->pb, "#media_type %d: %s\n", i, av_get_media_type_string(avctx->codec_type));
+        avio_printf(s->pb, "#media_type %d: %s\n", i, av_media_type_get_string(avctx->codec_type));
         avio_printf(s->pb, "#codec_id %d: %s\n", i, avcodec_get_name(avctx->codec_id));
         switch (avctx->codec_type) {
         case AVMEDIA_TYPE_AUDIO: {
diff --git a/libavformat/mov.c b/libavformat/mov.c
index d7be593a86..9514598718 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7306,7 +7306,7 @@ static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     av_log(ctx, AV_LOG_TRACE,
            "%s stream %d KindBox(scheme: %s, value: %s)\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            st->index,
            scheme_buf.str, value_buf.str);
 
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 62692bfcd1..3e44663715 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -458,7 +458,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
             break;
         default:
             av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
-                   av_get_media_type_string(st->codecpar->codec_type), i);
+                   av_media_type_get_string(st->codecpar->codec_type), i);
             return AVERROR(EINVAL);
         }
         stream->fifo = av_fifo_alloc2(16, 1, 0);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 6e761c07f1..13292f8041 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2258,7 +2258,7 @@ static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int p
     if (found) {
         av_log(ts->stream, AV_LOG_VERBOSE,
                "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
-               av_get_media_type_string(found->codecpar->codec_type),
+               av_media_type_get_string(found->codecpar->codec_type),
                found->index, found->id, pid);
     }
 
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index 6a22c33995..0af77c681e 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -2418,7 +2418,7 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
         av_dict_set(&st->metadata, "track_name", track->name, 0);
 
     codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &track->sequence->data_definition_ul);
-    av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
+    av_dict_set(&st->metadata, "data_type", av_media_type_get_string(codec_ul->id), 0);
     return 0;
 }
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index fa435d9f32..231d845ab3 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -738,7 +738,7 @@ static int seg_init(AVFormatContext *s)
         return ret;
     av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
            seg->reference_stream_index,
-           av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
+           av_media_type_get_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
 
     seg->oformat = av_guess_format(seg->format, s->url, NULL);
 
diff --git a/libavformat/tee.c b/libavformat/tee.c
index f1f2a9d2a5..3144ab4000 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -419,7 +419,7 @@ static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
 
         av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
                i, avcodec_get_name(st->codecpar->codec_id),
-               av_get_media_type_string(st->codecpar->codec_type));
+               av_media_type_get_string(st->codecpar->codec_type));
 
         bsf_name = bsf->filter->priv_class ?
                    bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
diff --git a/libavformat/uncodedframecrcenc.c b/libavformat/uncodedframecrcenc.c
index 99990616d3..c6476a4c43 100644
--- a/libavformat/uncodedframecrcenc.c
+++ b/libavformat/uncodedframecrcenc.c
@@ -131,7 +131,6 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     AVBPrint bp;
     int ret = 0;
     enum AVMediaType type;
-    const char *type_name;
 
     if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
         return 0;
@@ -140,8 +139,7 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     av_bprintf(&bp, "%d, %10"PRId64"",
                stream_index, (*frame)->pts);
     type = s->streams[stream_index]->codecpar->codec_type;
-    type_name = av_get_media_type_string(type);
-    av_bprintf(&bp, ", %s", type_name ? type_name : "unknown");
+    av_bprintf(&bp, ", %s", av_media_type_get_string(type));
     switch (type) {
         case AVMEDIA_TYPE_VIDEO:
             video_frame_cksum(&bp, *frame);
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 64b68bdbd3..542cfd111e 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -21,6 +21,8 @@
 #ifndef AVUTIL_AVUTIL_H
 #define AVUTIL_AVUTIL_H
 
+#include "attributes.h"
+
 /**
  * @file
  * @ingroup lavu
@@ -209,9 +211,17 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
+attribute_deprecated
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index 94d247bbee..5e0be8361f 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -37,6 +37,19 @@ const char *av_get_media_type_string(enum AVMediaType media_type)
     }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+    switch (media_type) {
+    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
+    case AVMEDIA_TYPE_VIDEO:      return "video";
+    case AVMEDIA_TYPE_AUDIO:      return "audio";
+    case AVMEDIA_TYPE_DATA:       return "data";
+    case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+    case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+    default:                      return "invalid_media_type";
+    }
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
     switch (pict_type) {
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

* [FFmpeg-devel] [PATCH v2] av_get_media_type_string(): replace with av_media_type_get_string()
  2022-06-01 19:22 ` [FFmpeg-devel] [PATCH] " Scott Theisen
@ 2022-09-16 17:32   ` Scott Theisen
  0 siblings, 0 replies; 19+ messages in thread
From: Scott Theisen @ 2022-09-16 17:32 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Scott Theisen

printf %s with a NULL pointer is undefined behavior.  Not returning
a NULL pointer makes for a nicer function for API users, including
in FFmpeg, since then you don't have to check it, instead you can
just print it.  32/44 uses in FFmpeg did not check for NULL.

This also matches behavior with avcodec_get_name() which never
returns NULL, instead it returns "unknown_codec".

In libavcodec/codec_desc.c, avcodec_get_type(AV_CODEC_ID_NONE) will
return AVMEDIA_TYPE_UNKNOWN.

The mpegts demuxer, for example, will set AV_CODEC_ID_NONE as the
AVCodecID of a stream if it can't identify the codec.  This is
probably true of all demuxers.

Therefore, AVMEDIA_TYPE_UNKNOWN is a valid value for the AVMediaType
enum and this should be differentiated from invalid values by
returning a different string for each case.
---
 doc/APIchanges                   |  3 +++
 doc/examples/demuxing_decoding.c | 10 +++++-----
 doc/examples/extract_mvs.c       |  4 ++--
 fftools/ffmpeg.c                 | 10 +++++-----
 fftools/ffmpeg_demux.c           |  4 ++--
 fftools/ffmpeg_mux.c             |  2 +-
 fftools/ffmpeg_opt.c             |  2 +-
 fftools/ffplay.c                 |  4 ++--
 fftools/ffprobe.c                | 13 +++----------
 fftools/opt_common.c             |  7 +++----
 libavcodec/avcodec.c             |  2 +-
 libavcodec/tests/avcodec.c       | 10 ++--------
 libavdevice/dshow.c              |  4 ++--
 libavfilter/avfilter.c           |  4 ++--
 libavfilter/avfiltergraph.c      |  4 ++--
 libavfilter/src_movie.c          |  6 +++---
 libavformat/avienc.c             |  2 +-
 libavformat/flvenc.c             |  2 +-
 libavformat/framehash.c          |  2 +-
 libavformat/mov.c                |  2 +-
 libavformat/mpegenc.c            |  2 +-
 libavformat/mpegts.c             |  2 +-
 libavformat/mxfdec.c             |  2 +-
 libavformat/segment.c            |  2 +-
 libavformat/tee.c                |  2 +-
 libavformat/uncodedframecrcenc.c |  4 +---
 libavutil/avutil.h               | 10 ++++++++++
 libavutil/utils.c                | 13 +++++++++++++
 28 files changed, 72 insertions(+), 62 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 729f56be7b..56d64dbcef 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,9 @@ libavutil:     2021-04-27
 
 API changes, most recent first:
 
+2022-09-XX - xxxxxxxxxx - lavu 57.19.100 - avutil.h
+  Add av_media_type_get_string() which deprecates av_get_media_type_string().
+
 2022-09-03 - xxxxxxxxxx - lavu 57.36.100 - pixfmt.h
   Add AV_PIX_FMT_P012, AV_PIX_FMT_Y212, AV_PIX_FMT_XV30, AV_PIX_FMT_XV36
 
diff --git a/doc/examples/demuxing_decoding.c b/doc/examples/demuxing_decoding.c
index 999a78db0d..467eda8a51 100644
--- a/doc/examples/demuxing_decoding.c
+++ b/doc/examples/demuxing_decoding.c
@@ -155,7 +155,7 @@ static int open_codec_context(int *stream_idx,
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, NULL, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         stream_index = ret;
@@ -165,7 +165,7 @@ static int open_codec_context(int *stream_idx,
         dec = avcodec_find_decoder(st->codecpar->codec_id);
         if (!dec) {
             fprintf(stderr, "Failed to find %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(EINVAL);
         }
 
@@ -173,21 +173,21 @@ static int open_codec_context(int *stream_idx,
         *dec_ctx = avcodec_alloc_context3(dec);
         if (!*dec_ctx) {
             fprintf(stderr, "Failed to allocate the %s codec context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return AVERROR(ENOMEM);
         }
 
         /* Copy codec parameters from input stream to output codec context */
         if ((ret = avcodec_parameters_to_context(*dec_ctx, st->codecpar)) < 0) {
             fprintf(stderr, "Failed to copy %s codec parameters to decoder context\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
         /* Init the decoders */
         if ((ret = avcodec_open2(*dec_ctx, dec, NULL)) < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
         *stream_idx = stream_index;
diff --git a/doc/examples/extract_mvs.c b/doc/examples/extract_mvs.c
index cc1311da91..2fb5517708 100644
--- a/doc/examples/extract_mvs.c
+++ b/doc/examples/extract_mvs.c
@@ -85,7 +85,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
     ret = av_find_best_stream(fmt_ctx, type, -1, -1, &dec, 0);
     if (ret < 0) {
         fprintf(stderr, "Could not find %s stream in input file '%s'\n",
-                av_get_media_type_string(type), src_filename);
+                av_media_type_get_string(type), src_filename);
         return ret;
     } else {
         int stream_idx = ret;
@@ -109,7 +109,7 @@ static int open_codec_context(AVFormatContext *fmt_ctx, enum AVMediaType type)
         av_dict_free(&opts);
         if (ret < 0) {
             fprintf(stderr, "Failed to open %s codec\n",
-                    av_get_media_type_string(type));
+                    av_media_type_get_string(type));
             return ret;
         }
 
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0e1477299d..2fcc3e2add 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -911,7 +911,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 {
     AVCodecContext   *enc = ost->enc_ctx;
     AVPacket         *pkt = ost->pkt;
-    const char *type_desc = av_get_media_type_string(enc->codec_type);
+    const char *type_desc = av_media_type_get_string(enc->codec_type);
     const char    *action = frame ? "encode" : "flush";
     int ret;
 
@@ -1501,7 +1501,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += ist->nb_packets;
 
             av_log(NULL, AV_LOG_VERBOSE, "  Input stream #%d:%d (%s): ",
-                   i, j, av_get_media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" packets read (%"PRIu64" bytes); ",
                    ist->nb_packets, ist->data_size);
 
@@ -1535,7 +1535,7 @@ static void print_final_stats(int64_t total_size)
             total_packets += atomic_load(&ost->packets_written);
 
             av_log(NULL, AV_LOG_VERBOSE, "  Output stream #%d:%d (%s): ",
-                   i, j, av_get_media_type_string(type));
+                   i, j, av_media_type_get_string(type));
             if (ost->enc_ctx) {
                 av_log(NULL, AV_LOG_VERBOSE, "%"PRIu64" frames encoded",
                        ost->frames_encoded);
@@ -3724,7 +3724,7 @@ static void ts_discontinuity_detect(InputFile *ifile, InputStream *ist,
                        "timestamp discontinuity for stream #%d:%d "
                        "(id=%d, type=%s): %"PRId64", new offset= %"PRId64"\n",
                        ist->file_index, ist->st->index, ist->st->id,
-                       av_get_media_type_string(ist->par->codec_type),
+                       av_media_type_get_string(ist->par->codec_type),
                        delta, ifile->ts_offset_discont);
                 pkt->dts -= av_rescale_q(delta, AV_TIME_BASE_Q, ist->st->time_base);
                 if (pkt->pts != AV_NOPTS_VALUE)
@@ -3875,7 +3875,7 @@ static int process_input(int file_index)
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "demuxer+ffmpeg -> ist_index:%d type:%s pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s off:%s off_time:%s\n",
                ifile->ist_index + pkt->stream_index,
-               av_get_media_type_string(ist->par->codec_type),
+               av_media_type_get_string(ist->par->codec_type),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
                av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base),
diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c
index 6e89f5999a..f82628af79 100644
--- a/fftools/ffmpeg_demux.c
+++ b/fftools/ffmpeg_demux.c
@@ -45,7 +45,7 @@ static void report_new_stream(InputFile *file, const AVPacket *pkt)
         return;
     av_log(file->ctx, AV_LOG_WARNING,
            "New %s stream %d:%d at pos:%"PRId64" and DTS:%ss\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            file->index, pkt->stream_index,
            pkt->pos, av_ts2timestr(pkt->dts, &st->time_base));
     file->nb_streams_warn = pkt->stream_index + 1;
@@ -127,7 +127,7 @@ static void ts_fixup(InputFile *ifile, AVPacket *pkt, int *repeat_pict)
         av_log(NULL, AV_LOG_INFO, "demuxer -> ist_index:%d type:%s "
                "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s\n",
                ifile->ist_index + pkt->stream_index,
-               av_get_media_type_string(ist->st->codecpar->codec_type),
+               av_media_type_get_string(ist->st->codecpar->codec_type),
                av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ist->st->time_base),
                av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ist->st->time_base),
                av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ist->st->time_base));
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index b781e1f5a6..f5bd981fce 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -164,7 +164,7 @@ static int write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "muxer <- type:%s "
                 "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s duration:%s duration_time:%s size:%d\n",
-                av_get_media_type_string(st->codecpar->codec_type),
+                av_media_type_get_string(st->codecpar->codec_type),
                 av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->st->time_base),
                 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->st->time_base),
                 av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->st->time_base),
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 5febe319e4..98af30d7bd 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1814,7 +1814,7 @@ static void check_streamcopy_filters(OptionsContext *o, AVFormatContext *oc,
                "Filtering and streamcopy cannot be used together.\n",
                ost->filters ? "Filtergraph" : "Filtergraph script",
                ost->filters ? ost->filters : ost->filters_script,
-               av_get_media_type_string(type), ost->file_index, ost->index);
+               av_media_type_get_string(type), ost->file_index, ost->index);
         exit_program(1);
     }
 }
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index bcc00afe31..3003cdd483 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -2868,7 +2868,7 @@ static int read_thread(void *arg)
     }
     for (i = 0; i < AVMEDIA_TYPE_NB; i++) {
         if (wanted_stream_spec[i] && st_index[i] == -1) {
-            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_get_media_type_string(i));
+            av_log(NULL, AV_LOG_ERROR, "Stream specifier %s does not match any %s stream\n", wanted_stream_spec[i], av_media_type_get_string(i));
             st_index[i] = INT_MAX;
         }
     }
@@ -3199,7 +3199,7 @@ static void stream_cycle_channel(VideoState *is, int codec_type)
     if (p && stream_index != -1)
         stream_index = p->stream_index[stream_index];
     av_log(NULL, AV_LOG_INFO, "Switch %s stream from #%d to #%d\n",
-           av_get_media_type_string(codec_type),
+           av_media_type_get_string(codec_type),
            old_index,
            stream_index);
 
diff --git a/fftools/ffprobe.c b/fftools/ffprobe.c
index 8ee1b1036c..b1265d9bb6 100644
--- a/fftools/ffprobe.c
+++ b/fftools/ffprobe.c
@@ -2476,15 +2476,12 @@ static void show_packet(WriterContext *w, InputFile *ifile, AVPacket *pkt, int p
     char val_str[128];
     AVStream *st = ifile->streams[pkt->stream_index].st;
     AVBPrint pbuf;
-    const char *s;
 
     av_bprint_init(&pbuf, 1, AV_BPRINT_SIZE_UNLIMITED);
 
     writer_print_section_header(w, SECTION_ID_PACKET);
 
-    s = av_get_media_type_string(st->codecpar->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(st->codecpar->codec_type));
     print_int("stream_index",     pkt->stream_index);
     print_ts  ("pts",             pkt->pts);
     print_time("pts_time",        pkt->pts, &st->time_base);
@@ -2559,9 +2556,7 @@ static void show_frame(WriterContext *w, AVFrame *frame, AVStream *stream,
 
     writer_print_section_header(w, SECTION_ID_FRAME);
 
-    s = av_get_media_type_string(stream->codecpar->codec_type);
-    if (s) print_str    ("media_type", s);
-    else   print_str_opt("media_type", "unknown");
+    print_str("media_type", av_media_type_get_string(stream->codecpar->codec_type));
     print_int("stream_index",           stream->index);
     print_int("key_frame",              frame->key_frame);
     print_ts  ("pts",                   frame->pts);
@@ -2970,9 +2965,7 @@ static int show_stream(WriterContext *w, AVFormatContext *fmt_ctx, int stream_id
             print_str_opt("profile", "unknown");
     }
 
-    s = av_get_media_type_string(par->codec_type);
-    if (s) print_str    ("codec_type", s);
-    else   print_str_opt("codec_type", "unknown");
+    print_str("codec_type", av_media_type_get_string(par->codec_type));
 
     /* print AVI/FourCC tag */
     print_str("codec_tag_string",    av_fourcc2str(par->codec_tag));
diff --git a/fftools/opt_common.c b/fftools/opt_common.c
index 8a06df82df..d44fc175ed 100644
--- a/fftools/opt_common.c
+++ b/fftools/opt_common.c
@@ -525,7 +525,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 0);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->inputs, i),
-               av_get_media_type_string(avfilter_pad_get_type(f->inputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->inputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_INPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -536,7 +536,7 @@ static void show_help_filter(const char *name)
     count = avfilter_filter_pad_count(f, 1);
     for (i = 0; i < count; i++) {
         printf("       #%d: %s (%s)\n", i, avfilter_pad_get_name(f->outputs, i),
-               av_get_media_type_string(avfilter_pad_get_type(f->outputs, i)));
+               av_media_type_get_string(avfilter_pad_get_type(f->outputs, i)));
     }
     if (f->flags & AVFILTER_FLAG_DYNAMIC_OUTPUTS)
         printf("        dynamic (depending on the options)\n");
@@ -1314,10 +1314,9 @@ static void print_device_list(const AVDeviceInfoList *device_list)
             device->device_name, device->device_description);
         if (device->nb_media_types > 0) {
             for (int j = 0; j < device->nb_media_types; ++j) {
-                const char* media_type = av_get_media_type_string(device->media_types[j]);
                 if (j > 0)
                     printf(", ");
-                printf("%s", media_type ? media_type : "unknown");
+                printf("%s", av_media_type_get_string(device->media_types[j]));
             }
         } else {
             printf("none");
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index 29643199be..1f53706664 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -526,7 +526,7 @@ void avcodec_string(char *buf, int buf_size, AVCodecContext *enc, int encode)
     if (!buf || buf_size <= 0)
         return;
     av_bprint_init_for_buffer(&bprint, buf, buf_size);
-    codec_type = av_get_media_type_string(enc->codec_type);
+    codec_type = av_media_type_get_string(enc->codec_type);
     codec_name = avcodec_get_name(enc->codec_id);
     profile = avcodec_profile_name(enc->codec_id, enc->profile);
 
diff --git a/libavcodec/tests/avcodec.c b/libavcodec/tests/avcodec.c
index 3288a85f64..7c2849c53b 100644
--- a/libavcodec/tests/avcodec.c
+++ b/libavcodec/tests/avcodec.c
@@ -22,12 +22,6 @@
 #include "libavcodec/codec_internal.h"
 #include "libavcodec/internal.h"
 
-static const char *get_type_string(enum AVMediaType type)
-{
-    const char *ret = av_get_media_type_string(type);
-    return ret ? ret : "unknown";
-}
-
 #define AV_LOG(...) av_log(NULL, AV_LOG_FATAL, __VA_ARGS__)
 #define ERR_INTERNAL(msg, ...)                                  \
 do {                                                            \
@@ -76,7 +70,7 @@ int main(void){
             codec->type != AVMEDIA_TYPE_AUDIO &&
             codec->type != AVMEDIA_TYPE_SUBTITLE)
             ERR_EXT("Codec %s has unsupported type %s\n",
-                    get_type_string(codec->type));
+                    av_media_type_get_string(codec->type));
         if (codec->type != AVMEDIA_TYPE_AUDIO) {
             if (codec->ch_layouts || codec->sample_fmts ||
                 codec->supported_samplerates)
@@ -182,7 +176,7 @@ int main(void){
         } else if (desc->type != codec->type)
             ERR_EXT("The type of AVCodec %s and its AVCodecDescriptor differ: "
                     "%s vs %s\n",
-                    get_type_string(codec->type), get_type_string(desc->type));
+                    av_media_type_get_string(codec->type), av_media_type_get_string(desc->type));
     }
     return ret;
 }
diff --git a/libavdevice/dshow.c b/libavdevice/dshow.c
index 5946a72cc2..7b36ea4bc9 100644
--- a/libavdevice/dshow.c
+++ b/libavdevice/dshow.c
@@ -577,10 +577,10 @@ dshow_cycle_devices(AVFormatContext *avctx, ICreateDevEnum *devenum,
             else {
                 av_log(avctx, AV_LOG_INFO, "\"%s\"", friendly_name);
                 if (nb_media_types > 0) {
-                    const char* media_type = av_get_media_type_string(media_types[0]);
+                    const char* media_type = av_media_type_get_string(media_types[0]);
                     av_log(avctx, AV_LOG_INFO, " (%s", media_type ? media_type : "unknown");
                     for (int i = 1; i < nb_media_types; ++i) {
-                        media_type = av_get_media_type_string(media_types[i]);
+                        media_type = av_media_type_get_string(media_types[i]);
                         av_log(avctx, AV_LOG_INFO, ", %s", media_type ? media_type : "unknown");
                     }
                     av_log(avctx, AV_LOG_INFO, ")");
diff --git a/libavfilter/avfilter.c b/libavfilter/avfilter.c
index f34204e650..ee9947b88d 100644
--- a/libavfilter/avfilter.c
+++ b/libavfilter/avfilter.c
@@ -161,8 +161,8 @@ int avfilter_link(AVFilterContext *src, unsigned srcpad,
     if (src->output_pads[srcpad].type != dst->input_pads[dstpad].type) {
         av_log(src, AV_LOG_ERROR,
                "Media type mismatch between the '%s' filter output pad %d (%s) and the '%s' filter input pad %d (%s)\n",
-               src->name, srcpad, (char *)av_x_if_null(av_get_media_type_string(src->output_pads[srcpad].type), "?"),
-               dst->name, dstpad, (char *)av_x_if_null(av_get_media_type_string(dst-> input_pads[dstpad].type), "?"));
+               src->name, srcpad, av_media_type_get_string(src->output_pads[srcpad].type),
+               dst->name, dstpad, av_media_type_get_string(dst-> input_pads[dstpad].type));
         return AVERROR(EINVAL);
     }
 
diff --git a/libavfilter/avfiltergraph.c b/libavfilter/avfiltergraph.c
index 53f468494d..0b34b0d23c 100644
--- a/libavfilter/avfiltergraph.c
+++ b/libavfilter/avfiltergraph.c
@@ -218,7 +218,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->input_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Input pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any source\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
@@ -228,7 +228,7 @@ static int graph_check_validity(AVFilterGraph *graph, void *log_ctx)
                 pad = &filt->output_pads[j];
                 av_log(log_ctx, AV_LOG_ERROR,
                        "Output pad \"%s\" with type %s of the filter instance \"%s\" of %s not connected to any destination\n",
-                       pad->name, av_get_media_type_string(pad->type), filt->name, filt->filter->name);
+                       pad->name, av_media_type_get_string(pad->type), filt->name, filt->filter->name);
                 return AVERROR(EINVAL);
             }
         }
diff --git a/libavfilter/src_movie.c b/libavfilter/src_movie.c
index 711854c23c..6484005921 100644
--- a/libavfilter/src_movie.c
+++ b/libavfilter/src_movie.c
@@ -115,7 +115,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         ret = av_find_best_stream(avf, type, stream_id, -1, NULL, 0);
         if (ret < 0) {
             av_log(log, AV_LOG_ERROR, "No %s stream with index '%d' found\n",
-                   av_get_media_type_string(type), stream_id);
+                   av_media_type_get_string(type), stream_id);
             return NULL;
         }
         return avf->streams[ret];
@@ -150,7 +150,7 @@ static AVStream *find_stream(void *log, AVFormatContext *avf, const char *spec)
         found->codecpar->codec_type != AVMEDIA_TYPE_AUDIO) {
         av_log(log, AV_LOG_ERROR, "Stream specifier \"%s\" matched a %s stream,"
                "currently unsupported by libavfilter\n", spec,
-               av_get_media_type_string(found->codecpar->codec_type));
+               av_media_type_get_string(found->codecpar->codec_type));
         return NULL;
     }
     return found;
@@ -429,7 +429,7 @@ static char *describe_frame_to_str(char *dst, size_t dst_size,
                  frame->nb_samples);
                  break;
     default:
-        snprintf(dst, dst_size, "%s BUG", av_get_media_type_string(frame_type));
+        snprintf(dst, dst_size, "%s BUG", av_media_type_get_string(frame_type));
         break;
     }
     return dst;
diff --git a/libavformat/avienc.c b/libavformat/avienc.c
index 14115b3e2b..97ffde40e6 100644
--- a/libavformat/avienc.c
+++ b/libavformat/avienc.c
@@ -479,7 +479,7 @@ static int avi_write_header(AVFormatContext *s)
             default:
                 av_log(s, AV_LOG_ERROR,
                     "Invalid or not supported codec type '%s' found in the input\n",
-                    (char *)av_x_if_null(av_get_media_type_string(par->codec_type), "?"));
+                    av_media_type_get_string(par->codec_type));
                 return AVERROR(EINVAL);
             }
             ff_end_tag(pb, strf);
diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index 5d574fa790..ca5b02d57f 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -667,7 +667,7 @@ static int flv_init(struct AVFormatContext *s)
             break;
         default:
             av_log(s, AV_LOG_ERROR, "Codec type '%s' for stream %d is not compatible with FLV\n",
-                   av_get_media_type_string(par->codec_type), i);
+                   av_media_type_get_string(par->codec_type), i);
             return AVERROR(EINVAL);
         }
         avpriv_set_pts_info(s->streams[i], 32, 1, 1000); /* 32 bit pts in ms */
diff --git a/libavformat/framehash.c b/libavformat/framehash.c
index 654996e8e0..07c29da82c 100644
--- a/libavformat/framehash.c
+++ b/libavformat/framehash.c
@@ -33,7 +33,7 @@ int ff_framehash_write_header(AVFormatContext *s)
         AVCodecParameters *avctx = st->codecpar;
         char buf[256] = { 0 };
         avio_printf(s->pb, "#tb %d: %d/%d\n", i, st->time_base.num, st->time_base.den);
-        avio_printf(s->pb, "#media_type %d: %s\n", i, av_get_media_type_string(avctx->codec_type));
+        avio_printf(s->pb, "#media_type %d: %s\n", i, av_media_type_get_string(avctx->codec_type));
         avio_printf(s->pb, "#codec_id %d: %s\n", i, avcodec_get_name(avctx->codec_id));
         switch (avctx->codec_type) {
         case AVMEDIA_TYPE_AUDIO: {
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 720a72f3ec..05191c4358 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -7442,7 +7442,7 @@ static int mov_read_kind(MOVContext *c, AVIOContext *pb, MOVAtom atom)
 
     av_log(ctx, AV_LOG_TRACE,
            "%s stream %d KindBox(scheme: %s, value: %s)\n",
-           av_get_media_type_string(st->codecpar->codec_type),
+           av_media_type_get_string(st->codecpar->codec_type),
            st->index,
            scheme_buf.str, value_buf.str);
 
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index 3ab4bd3f9b..f3c2c1e43c 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -458,7 +458,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
             break;
         default:
             av_log(ctx, AV_LOG_ERROR, "Invalid media type %s for output stream #%d\n",
-                   av_get_media_type_string(st->codecpar->codec_type), i);
+                   av_media_type_get_string(st->codecpar->codec_type), i);
             return AVERROR(EINVAL);
         }
         stream->fifo = av_fifo_alloc2(16, 1, 0);
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 8a3436f2be..4291541ff4 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -2258,7 +2258,7 @@ static AVStream *find_matching_stream(MpegTSContext *ts, int pid, unsigned int p
     if (found) {
         av_log(ts->stream, AV_LOG_VERBOSE,
                "re-using existing %s stream %d (pid=0x%x) for new pid=0x%x\n",
-               av_get_media_type_string(found->codecpar->codec_type),
+               av_media_type_get_string(found->codecpar->codec_type),
                found->index, found->id, pid);
     }
 
diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index e63e803aa5..3bf7106962 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -2416,7 +2416,7 @@ static int mxf_add_metadata_stream(MXFContext *mxf, MXFTrack *track)
         av_dict_set(&st->metadata, "track_name", track->name, 0);
 
     codec_ul = mxf_get_codec_ul(ff_mxf_data_definition_uls, &track->sequence->data_definition_ul);
-    av_dict_set(&st->metadata, "data_type", av_get_media_type_string(codec_ul->id), 0);
+    av_dict_set(&st->metadata, "data_type", av_media_type_get_string(codec_ul->id), 0);
     return 0;
 }
 
diff --git a/libavformat/segment.c b/libavformat/segment.c
index c904e20708..1b20cafb8f 100644
--- a/libavformat/segment.c
+++ b/libavformat/segment.c
@@ -736,7 +736,7 @@ static int seg_init(AVFormatContext *s)
         return ret;
     av_log(s, AV_LOG_VERBOSE, "Selected stream id:%d type:%s\n",
            seg->reference_stream_index,
-           av_get_media_type_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
+           av_media_type_get_string(s->streams[seg->reference_stream_index]->codecpar->codec_type));
 
     seg->oformat = av_guess_format(seg->format, s->url, NULL);
 
diff --git a/libavformat/tee.c b/libavformat/tee.c
index dd408dd096..839abd5fa1 100644
--- a/libavformat/tee.c
+++ b/libavformat/tee.c
@@ -416,7 +416,7 @@ static void log_slave(TeeSlave *slave, void *log_ctx, int log_level)
 
         av_log(log_ctx, log_level, "    stream:%d codec:%s type:%s",
                i, avcodec_get_name(st->codecpar->codec_id),
-               av_get_media_type_string(st->codecpar->codec_type));
+               av_media_type_get_string(st->codecpar->codec_type));
 
         bsf_name = bsf->filter->priv_class ?
                    bsf->filter->priv_class->item_name(bsf) : bsf->filter->name;
diff --git a/libavformat/uncodedframecrcenc.c b/libavformat/uncodedframecrcenc.c
index 99990616d3..c6476a4c43 100644
--- a/libavformat/uncodedframecrcenc.c
+++ b/libavformat/uncodedframecrcenc.c
@@ -131,7 +131,6 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     AVBPrint bp;
     int ret = 0;
     enum AVMediaType type;
-    const char *type_name;
 
     if ((flags & AV_WRITE_UNCODED_FRAME_QUERY))
         return 0;
@@ -140,8 +139,7 @@ static int write_frame(struct AVFormatContext *s, int stream_index,
     av_bprintf(&bp, "%d, %10"PRId64"",
                stream_index, (*frame)->pts);
     type = s->streams[stream_index]->codecpar->codec_type;
-    type_name = av_get_media_type_string(type);
-    av_bprintf(&bp, ", %s", type_name ? type_name : "unknown");
+    av_bprintf(&bp, ", %s", av_media_type_get_string(type));
     switch (type) {
         case AVMEDIA_TYPE_VIDEO:
             video_frame_cksum(&bp, *frame);
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 64b68bdbd3..542cfd111e 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -21,6 +21,8 @@
 #ifndef AVUTIL_AVUTIL_H
 #define AVUTIL_AVUTIL_H
 
+#include "attributes.h"
+
 /**
  * @file
  * @ingroup lavu
@@ -209,9 +211,17 @@ enum AVMediaType {
 /**
  * Return a string describing the media_type enum, NULL if media_type
  * is unknown.
+ *
+ * @deprecated Use av_media_type_get_string() instead.
  */
+attribute_deprecated
 const char *av_get_media_type_string(enum AVMediaType media_type);
 
+/**
+ * Return a string describing the media_type enum, never NULL.
+ */
+const char *av_media_type_get_string(enum AVMediaType media_type);
+
 /**
  * @defgroup lavu_const Constants
  * @{
diff --git a/libavutil/utils.c b/libavutil/utils.c
index 94d247bbee..5e0be8361f 100644
--- a/libavutil/utils.c
+++ b/libavutil/utils.c
@@ -37,6 +37,19 @@ const char *av_get_media_type_string(enum AVMediaType media_type)
     }
 }
 
+const char *av_media_type_get_string(enum AVMediaType media_type)
+{
+    switch (media_type) {
+    case AVMEDIA_TYPE_UNKNOWN:    return "unknown";
+    case AVMEDIA_TYPE_VIDEO:      return "video";
+    case AVMEDIA_TYPE_AUDIO:      return "audio";
+    case AVMEDIA_TYPE_DATA:       return "data";
+    case AVMEDIA_TYPE_SUBTITLE:   return "subtitle";
+    case AVMEDIA_TYPE_ATTACHMENT: return "attachment";
+    default:                      return "invalid_media_type";
+    }
+}
+
 char av_get_picture_type_char(enum AVPictureType pict_type)
 {
     switch (pict_type) {
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2022-09-16 17:33 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-02-01 22:30 [FFmpeg-devel] [PATCH] lavu: make av_get_media_type_string() never return NULL Scott Theisen
2022-02-01 22:34 ` James Almer
2022-02-02  1:58   ` Scott Theisen
2022-02-02  2:06     ` James Almer
2022-02-02  2:42       ` Scott Theisen
2022-02-02  2:13     ` Andreas Rheinhardt
2022-02-23  7:52     ` Anton Khirnov
2022-02-28 21:06       ` Scott Theisen
2022-02-03 20:09 ` [FFmpeg-devel] [PATCH v2] create av_media_type_get_string() Scott Theisen
2022-02-03 20:09   ` [FFmpeg-devel] [PATCH 1/2] libavutil: " Scott Theisen
2022-02-03 20:09   ` [FFmpeg-devel] [PATCH 2/2] replace av_get_media_type_string() with av_media_type_get_string() Scott Theisen
2022-02-13 21:52 ` [FFmpeg-devel] [PATCH v3] create av_media_type_get_string() Scott Theisen
2022-02-13 21:52   ` [FFmpeg-devel] [PATCH v3 1/2] libavutil: " Scott Theisen
2022-02-13 21:52   ` [FFmpeg-devel] [PATCH v3 2/2] av_get_media_type_string(): replace with av_media_type_get_string() Scott Theisen
2022-05-15 19:55 ` [FFmpeg-devel] [Patch v4 0/2] create av_media_type_get_string() Scott Theisen
2022-05-15 19:55   ` [FFmpeg-devel] [PATCH v4 1/2] libavutil: " Scott Theisen
2022-05-15 19:55   ` [FFmpeg-devel] [PATCH v4 2/2] av_get_media_type_string(): replace with av_media_type_get_string() Scott Theisen
2022-06-01 19:22 ` [FFmpeg-devel] [PATCH] " Scott Theisen
2022-09-16 17:32   ` [FFmpeg-devel] [PATCH v2] " Scott Theisen

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git