diff mbox series

[FFmpeg-devel,12/15] avformat/pcmdec: Avoid av_freep(&(void*){NULL})

Message ID GV1P250MB0737D249F5FD5532761FBD628F302@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State New
Headers show
Series [FFmpeg-devel,01/15] configure: Make hls demuxer select AAC, AC3 and EAC3 demuxers | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt March 23, 2024, 2:06 a.m. UTC
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavformat/pcmdec.c | 61 ++++++++++++++++++++++++++++----------------
 1 file changed, 39 insertions(+), 22 deletions(-)
diff mbox series

Patch

diff --git a/libavformat/pcmdec.c b/libavformat/pcmdec.c
index e9c97f7959..03feefb5bb 100644
--- a/libavformat/pcmdec.c
+++ b/libavformat/pcmdec.c
@@ -37,31 +37,20 @@  typedef struct PCMAudioDemuxerContext {
     AVChannelLayout ch_layout;
 } PCMAudioDemuxerContext;
 
-static int pcm_read_header(AVFormatContext *s)
+static int pcm_handle_mime_type(AVFormatContext *s, const char *prefix,
+                                AVCodecParameters *par)
 {
-    PCMAudioDemuxerContext *s1 = s->priv_data;
-    AVCodecParameters *par;
-    AVStream *st;
+    int rate = 0, channels = 0, little_endian = 0;
     uint8_t *mime_type = NULL;
-    int ret;
+    const char *options;
 
-    st = avformat_new_stream(s, NULL);
-    if (!st)
-        return AVERROR(ENOMEM);
-    par = st->codecpar;
+    av_opt_get(s->pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
+    if (!mime_type)
+        return 0;
 
-    par->codec_type  = AVMEDIA_TYPE_AUDIO;
-    par->codec_id    = ffifmt(s->iformat)->raw_codec_id;
-    par->sample_rate = s1->sample_rate;
-    ret = av_channel_layout_copy(&par->ch_layout, &s1->ch_layout);
-    if (ret < 0)
-        return ret;
+    if (!av_stristart(mime_type, prefix, &options)) /* audio/L16 */
+        goto end;
 
-    av_opt_get(s->pb, "mime_type", AV_OPT_SEARCH_CHILDREN, &mime_type);
-    if (mime_type && s->iformat->mime_type) {
-        int rate = 0, channels = 0, little_endian = 0;
-        const char *options;
-        if (av_stristart(mime_type, s->iformat->mime_type, &options)) { /* audio/L16 */
             while (options = strchr(options, ';')) {
                 options++;
                 if (!rate)
@@ -89,10 +78,38 @@  static int pcm_read_header(AVFormatContext *s)
             }
             if (little_endian)
                 par->codec_id = AV_CODEC_ID_PCM_S16LE;
-        }
-    }
+
+end:
     av_freep(&mime_type);
 
+    return 0;
+}
+
+static int pcm_read_header(AVFormatContext *s)
+{
+    PCMAudioDemuxerContext *s1 = s->priv_data;
+    AVCodecParameters *par;
+    AVStream *st;
+    int ret;
+
+    st = avformat_new_stream(s, NULL);
+    if (!st)
+        return AVERROR(ENOMEM);
+    par = st->codecpar;
+
+    par->codec_type  = AVMEDIA_TYPE_AUDIO;
+    par->codec_id    = ffifmt(s->iformat)->raw_codec_id;
+    par->sample_rate = s1->sample_rate;
+    ret = av_channel_layout_copy(&par->ch_layout, &s1->ch_layout);
+    if (ret < 0)
+        return ret;
+
+    if (s->iformat->mime_type) {
+        ret = pcm_handle_mime_type(s, s->iformat->mime_type, par);
+        if (ret < 0)
+            return ret;
+    }
+
     par->bits_per_coded_sample = av_get_bits_per_sample(par->codec_id);
 
     av_assert0(par->bits_per_coded_sample > 0);