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] avformat: Combine hlsenc/dashenc codec string generation, make public API (PR #20739)
@ 2025-10-23 10:57 Martin Storsjö via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: Martin Storsjö via ffmpeg-devel @ 2025-10-23 10:57 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Martin Storsjö

PR #20739 opened by Martin Storsjö (mstorsjo)
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20739
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/20739.patch


From ec5f76d9e28a3e79bbd8e369470749597317490c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin@martin.st>
Date: Thu, 23 Oct 2025 13:35:12 +0300
Subject: [PATCH 1/3] avformat/vpcc: Generalize the AVFormatContext parameter
 to void*

This parameter is only used as a logging context; don't require
a full AVFormatContext here.
---
 libavformat/vpcc.c | 20 ++++++++++----------
 libavformat/vpcc.h |  6 +++---
 2 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/libavformat/vpcc.c b/libavformat/vpcc.c
index a5977916bf..f97700f5e6 100644
--- a/libavformat/vpcc.c
+++ b/libavformat/vpcc.c
@@ -35,7 +35,7 @@ enum VPX_CHROMA_SUBSAMPLING
     VPX_SUBSAMPLING_444 = 3,
 };
 
-static int get_vpx_chroma_subsampling(AVFormatContext *s,
+static int get_vpx_chroma_subsampling(void *logctx,
                                       enum AVPixelFormat pixel_format,
                                       enum AVChromaLocation chroma_location)
 {
@@ -51,15 +51,15 @@ static int get_vpx_chroma_subsampling(AVFormatContext *s,
             return VPX_SUBSAMPLING_444;
         }
     }
-    av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n", pixel_format);
+    av_log(logctx, AV_LOG_ERROR, "Unsupported pixel format (%d)\n", pixel_format);
     return -1;
 }
 
-static int get_bit_depth(AVFormatContext *s, enum AVPixelFormat pixel_format)
+static int get_bit_depth(void *logctx, enum AVPixelFormat pixel_format)
 {
     const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(pixel_format);
     if (desc == NULL) {
-        av_log(s, AV_LOG_ERROR, "Unsupported pixel format (%d)\n",
+        av_log(logctx, AV_LOG_ERROR, "Unsupported pixel format (%d)\n",
                pixel_format);
         return -1;
     }
@@ -150,16 +150,16 @@ static void parse_bitstream(GetBitContext *gb, int *profile, int *bit_depth) {
     *bit_depth = *profile <= 1 ? 8 : 10 + get_bits1(gb) * 2;
 }
 
-int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par,
+int ff_isom_get_vpcc_features(void *logctx, AVCodecParameters *par,
                               const uint8_t *data, int len,
                               AVRational *frame_rate, VPCC *vpcc)
 {
     int profile = par->profile;
     int level = par->level == AV_LEVEL_UNKNOWN ?
         get_vp9_level(par, frame_rate) : par->level;
-    int bit_depth = get_bit_depth(s, par->format);
+    int bit_depth = get_bit_depth(logctx, par->format);
     int vpx_chroma_subsampling =
-        get_vpx_chroma_subsampling(s, par->format, par->chroma_location);
+        get_vpx_chroma_subsampling(logctx, par->format, par->chroma_location);
     int vpx_video_full_range_flag =
         get_vpx_video_full_range_flag(par->color_range);
 
@@ -186,7 +186,7 @@ int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par,
     }
 
     if (profile == AV_PROFILE_UNKNOWN || !bit_depth)
-        av_log(s, AV_LOG_WARNING, "VP9 profile and/or bit depth not set or could not be derived\n");
+        av_log(logctx, AV_LOG_WARNING, "VP9 profile and/or bit depth not set or could not be derived\n");
 
     vpcc->profile            = profile;
     vpcc->level              = level;
@@ -197,14 +197,14 @@ int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par,
     return 0;
 }
 
-int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb,
+int ff_isom_write_vpcc(void *logctx, AVIOContext *pb,
                        const uint8_t *data, int len,
                        AVCodecParameters *par)
 {
     VPCC vpcc;
     int ret;
 
-    ret = ff_isom_get_vpcc_features(s, par, data, len, NULL, &vpcc);
+    ret = ff_isom_get_vpcc_features(logctx, par, data, len, NULL, &vpcc);
     if (ret < 0)
         return ret;
 
diff --git a/libavformat/vpcc.h b/libavformat/vpcc.h
index bad38c5615..2a1362abae 100644
--- a/libavformat/vpcc.h
+++ b/libavformat/vpcc.h
@@ -43,7 +43,7 @@ typedef struct VPCC {
 /**
  * Writes VP codec configuration to the provided AVIOContext.
  *
- * @param s address of the AVFormatContext for the logging context.
+ * @param logctx opaque pointer for logging.
  * @param pb address of the AVIOContext where the vpcC shall be written.
  * @param data address of a data array which contains coded bitstream data from
  *             which codec information can be extracted. May be NULL.
@@ -52,11 +52,11 @@ typedef struct VPCC {
  * @return >=0 in case of success, a negative value corresponding to an AVERROR
  *         code in case of failure
  */
-int ff_isom_write_vpcc(AVFormatContext *s, AVIOContext *pb,
+int ff_isom_write_vpcc(void *logctx, AVIOContext *pb,
                        const uint8_t *data, int len,
                        AVCodecParameters *par);
 
-int ff_isom_get_vpcc_features(AVFormatContext *s, AVCodecParameters *par,
+int ff_isom_get_vpcc_features(void *logctx, AVCodecParameters *par,
                               const uint8_t *data, int len,
                               AVRational *frame_rate, VPCC *vpcc);
 
-- 
2.49.1


From 1b125139ab3960521359995f03a14911620a9dee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin@martin.st>
Date: Thu, 23 Oct 2025 13:20:54 +0300
Subject: [PATCH 2/3] hlsenc, dashenc: Share the routine for generating a codec
 string

The one in dashenc was added in
fe5e6e34c05e274f98528be4f77f3c474473f977, while the one in hlsenc
was added later in 0afa171f25bc9b613a77328182e54d6e781269d3. Both
have had various additions on top; merge both implementations
into one shared. (Notable additions in
060e74e2a970bb12ab138cfff5aaff8d132ffc65,
1cf2f040e34bbfedde60ff3d91b2f7b770aca85b,
a2b1dd0ce301450a47c972745a6b33c4c273aa5d and
797f0b27c175022d896e46db4ac2873e3e0a70af.)

For H264/avc1, use the implementation from hlsenc (which doesn't
use temporary allocations). For most other codecs, use the
only implementation from whichever had one.

The original dashenc implementation tried to be generic based
on RFC 6381, looking up codec tags in ff_codec_movvideo_tags
or ff_codec_movaudio_tags, and doing specific extra additions
for "mp4a" and "mp4v". In practice, only AV_CODEC_ID_AAC
and AV_CODEC_ID_MPEG4 ever mapped to these; simplify this to
a more straightforward codec id based handling, and merge
with the AAC profile based code from hlsenc.

There's a slight behaviour difference from the old one in
dashenc; if there's no code for a specific codec ID, we previously
just output what we matched from the mov tag tables, but now
we won't output anything. But most commonly used codecs in
DASH should be covered here.
---
 libavformat/Makefile      |   4 +-
 libavformat/codecstring.c | 224 ++++++++++++++++++++++++++++++++++++++
 libavformat/dashenc.c     | 133 +---------------------
 libavformat/hlsenc.c      | 116 +-------------------
 libavformat/internal.h    |  14 +++
 5 files changed, 246 insertions(+), 245 deletions(-)
 create mode 100644 libavformat/codecstring.c

diff --git a/libavformat/Makefile b/libavformat/Makefile
index ed93458f03..86177af2f4 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -179,7 +179,7 @@ OBJS-$(CONFIG_CONCAT_DEMUXER)            += concatdec.o
 OBJS-$(CONFIG_CRC_MUXER)                 += crcenc.o
 OBJS-$(CONFIG_DATA_DEMUXER)              += rawdec.o
 OBJS-$(CONFIG_DATA_MUXER)                += rawenc.o
-OBJS-$(CONFIG_DASH_MUXER)                += dash.o dashenc.o hlsplaylist.o
+OBJS-$(CONFIG_DASH_MUXER)                += dash.o dashenc.o hlsplaylist.o codecstring.o
 OBJS-$(CONFIG_DASH_DEMUXER)              += dash.o dashdec.o
 OBJS-$(CONFIG_DAUD_DEMUXER)              += dauddec.o
 OBJS-$(CONFIG_DAUD_MUXER)                += daudenc.o
@@ -267,7 +267,7 @@ OBJS-$(CONFIG_HEVC_MUXER)                += rawenc.o
 OBJS-$(CONFIG_EVC_DEMUXER)               += evcdec.o rawdec.o
 OBJS-$(CONFIG_EVC_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_HLS_DEMUXER)               += hls.o hls_sample_encryption.o
-OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o hlsplaylist.o
+OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o hlsplaylist.o codecstring.o
 OBJS-$(CONFIG_HNM_DEMUXER)               += hnm.o
 OBJS-$(CONFIG_HXVS_DEMUXER)              += hxvs.o
 OBJS-$(CONFIG_IAMF_DEMUXER)              += iamfdec.o
diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
new file mode 100644
index 0000000000..589131fbea
--- /dev/null
+++ b/libavformat/codecstring.c
@@ -0,0 +1,224 @@
+/*
+ * Copyright (c) 2014 Martin Storsjo
+ * Copyright (c) 2018 Akamai Technologies, Inc.
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "libavutil/avstring.h"
+#include "libavutil/intreadwrite.h"
+#include "libavutil/mem.h"
+#include "libavutil/rational.h"
+
+#include "libavcodec/avcodec.h"
+
+#include "av1.h"
+#include "avc.h"
+#include "avformat.h"
+#include "internal.h"
+#include "nal.h"
+#include "vpcc.h"
+
+static const struct codec_string {
+    enum AVCodecID id;
+    const char str[8];
+} codecs[] = {
+    { AV_CODEC_ID_VP8, "vp8" },
+    { AV_CODEC_ID_VP9, "vp9" },
+    { AV_CODEC_ID_VORBIS, "vorbis" },
+    { AV_CODEC_ID_OPUS, "opus" },
+    { AV_CODEC_ID_FLAC, "flac" },
+    { AV_CODEC_ID_NONE }
+};
+
+static void set_vp9_codec_str(void *logctx, AVCodecParameters *par,
+                              AVRational *frame_rate, char *str, int size) {
+    VPCC vpcc;
+    int ret = ff_isom_get_vpcc_features(logctx, par, NULL, 0, frame_rate, &vpcc);
+    if (ret == 0) {
+        snprintf(str, size, "vp09.%02d.%02d.%02d",
+                 vpcc.profile, vpcc.level, vpcc.bitdepth);
+    } else {
+        // Default to just vp9 in case of error while finding out profile or level
+        av_log(logctx, AV_LOG_WARNING, "Could not find VP9 profile and/or level\n");
+        av_strlcpy(str, "vp9", size);
+    }
+}
+
+int ff_make_codec_str(void *logctx, AVCodecParameters *par,
+                      AVRational *frame_rate, char *str, int size)
+{
+    int i;
+
+    if (size < 5)
+        return AVERROR(EINVAL);
+
+    str[0] = '\0';
+
+    // common Webm codecs are not part of RFC 6381
+    for (i = 0; codecs[i].id != AV_CODEC_ID_NONE; i++)
+        if (codecs[i].id == par->codec_id) {
+            if (codecs[i].id == AV_CODEC_ID_VP9) {
+                set_vp9_codec_str(logctx, par, frame_rate, str, size);
+            } else {
+                av_strlcpy(str, codecs[i].str, size);
+            }
+            return 0;
+        }
+
+    if (par->codec_id == AV_CODEC_ID_H264) {
+        // RFC 6381
+        uint8_t *data = par->extradata;
+        if (data) {
+            const uint8_t *p;
+
+            if (AV_RB32(data) == 0x01 && (data[4] & 0x1F) == 7)
+                p = &data[5];
+            else if (AV_RB24(data) == 0x01 && (data[3] & 0x1F) == 7)
+                p = &data[4];
+            else if (data[0] == 0x01)  /* avcC */
+                p = &data[1];
+            else
+                return AVERROR(EINVAL);
+            snprintf(str, size,
+                     "avc1.%02x%02x%02x", p[0], p[1], p[2]);
+        } else {
+            return AVERROR(EINVAL);
+        }
+    } else if (par->codec_id == AV_CODEC_ID_HEVC) {
+        // 3GPP TS 26.244
+        uint8_t *data = par->extradata;
+        int profile = AV_PROFILE_UNKNOWN;
+        uint32_t profile_compatibility = AV_PROFILE_UNKNOWN;
+        char tier = 0;
+        int level = AV_LEVEL_UNKNOWN;
+        char constraints[8] = "";
+
+        if (par->profile != AV_PROFILE_UNKNOWN)
+            profile = par->profile;
+        if (par->level != AV_LEVEL_UNKNOWN)
+            level = par->level;
+
+        /* check the boundary of data which from current position is small than extradata_size */
+        while (data && (data - par->extradata + 19) < par->extradata_size) {
+            /* get HEVC SPS NAL and seek to profile_tier_level */
+            if (!(data[0] | data[1] | data[2]) && data[3] == 1 && ((data[4] & 0x7E) == 0x42)) {
+                uint8_t *rbsp_buf;
+                int remain_size = 0;
+                int rbsp_size = 0;
+                uint32_t profile_compatibility_flags = 0;
+                uint8_t high_nibble = 0;
+                /* skip start code + nalu header */
+                data += 6;
+                /* process by reference General NAL unit syntax */
+                remain_size = par->extradata_size - (data - par->extradata);
+                rbsp_buf = ff_nal_unit_extract_rbsp(data, remain_size, &rbsp_size, 0);
+                if (!rbsp_buf)
+                    return AVERROR(EINVAL);
+                if (rbsp_size < 13) {
+                    av_freep(&rbsp_buf);
+                    break;
+                }
+                /* skip sps_video_parameter_set_id   u(4),
+                 *      sps_max_sub_layers_minus1    u(3),
+                 *  and sps_temporal_id_nesting_flag u(1)
+                 *
+                 * TIER represents the general_tier_flag, with 'L' indicating the flag is 0,
+                 * and 'H' indicating the flag is 1
+                 */
+                tier = (rbsp_buf[1] & 0x20) == 0 ? 'L' : 'H';
+                profile = rbsp_buf[1] & 0x1f;
+                /* PROFILE_COMPATIBILITY is general_profile_compatibility_flags, but in reverse bit order,
+                 * in a hexadecimal representation (leading zeroes may be omitted).
+                 */
+                profile_compatibility_flags = AV_RB32(rbsp_buf + 2);
+                /* revise these bits to get the profile compatibility value */
+                profile_compatibility_flags = ((profile_compatibility_flags & 0x55555555U) << 1) | ((profile_compatibility_flags >> 1) & 0x55555555U);
+                profile_compatibility_flags = ((profile_compatibility_flags & 0x33333333U) << 2) | ((profile_compatibility_flags >> 2) & 0x33333333U);
+                profile_compatibility_flags = ((profile_compatibility_flags & 0x0F0F0F0FU) << 4) | ((profile_compatibility_flags >> 4) & 0x0F0F0F0FU);
+                profile_compatibility_flags = ((profile_compatibility_flags & 0x00FF00FFU) << 8) | ((profile_compatibility_flags >> 8) & 0x00FF00FFU);
+                profile_compatibility = (profile_compatibility_flags << 16) | (profile_compatibility_flags >> 16);
+                /* skip 8 + 8 + 32
+                 * CONSTRAINTS is a hexadecimal representation of the general_constraint_indicator_flags.
+                 * each byte is separated by a '.', and trailing zero bytes may be omitted.
+                 * drop the trailing zero bytes refer to ISO/IEC14496-15.
+                 */
+                high_nibble = rbsp_buf[7] >> 4;
+                snprintf(constraints, sizeof(constraints),
+                         high_nibble ? "%02x.%x" : "%02x",
+                         rbsp_buf[6], high_nibble);
+                /* skip 8 + 8 + 32 + 4 + 43 + 1 bit */
+                level = rbsp_buf[12];
+                av_freep(&rbsp_buf);
+                break;
+            }
+            data++;
+        }
+        if (par->codec_tag == MKTAG('h','v','c','1') &&
+            profile != AV_PROFILE_UNKNOWN &&
+            profile_compatibility != AV_PROFILE_UNKNOWN &&
+            tier != 0 &&
+            level != AV_LEVEL_UNKNOWN &&
+            constraints[0] != '\0') {
+            snprintf(str, size, "%s.%d.%x.%c%d.%s", av_fourcc2str(par->codec_tag), profile, profile_compatibility, tier, level, constraints);
+        } else
+            return AVERROR(EINVAL);
+    } else if (par->codec_id == AV_CODEC_ID_AV1) {
+        // https://aomediacodec.github.io/av1-isobmff/#codecsparam
+        AV1SequenceParameters seq;
+        int err;
+        if (!par->extradata_size)
+            return AVERROR(EINVAL);
+        if ((err = ff_av1_parse_seq_header(&seq, par->extradata, par->extradata_size)) < 0)
+            return err;
+
+        snprintf(str, size, "av01.%01u.%02u%s.%02u",
+                    seq.profile, seq.level, seq.tier ? "H" : "M", seq.bitdepth);
+        if (seq.color_description_present_flag)
+            av_strlcatf(str, size, ".%01u.%01u%01u%01u.%02u.%02u.%02u.%01u",
+                        seq.monochrome,
+                        seq.chroma_subsampling_x, seq.chroma_subsampling_y, seq.chroma_sample_position,
+                        seq.color_primaries, seq.transfer_characteristics, seq.matrix_coefficients,
+                        seq.color_range);
+    } else if (par->codec_id == AV_CODEC_ID_MPEG4) {
+        // RFC 6381
+        snprintf(str, size, "mp4v.20");
+        // Unimplemented, should output ProfileLevelIndication as a decimal number
+        av_log(logctx, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
+    } else if (par->codec_id == AV_CODEC_ID_MP2) {
+        snprintf(str, size, "mp4a.40.33");
+    } else if (par->codec_id == AV_CODEC_ID_MP3) {
+        snprintf(str, size, "mp4a.40.34");
+    } else if (par->codec_id == AV_CODEC_ID_AAC) {
+        // RFC 6381
+        int aot = 2;
+        if (par->extradata_size >= 2) {
+            aot = par->extradata[0] >> 3;
+            if (aot == 31)
+                aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
+        } else if (par->profile != AV_PROFILE_UNKNOWN)
+            aot = par->profile + 1;
+        snprintf(str, size, "mp4a.40.%d", aot);
+    } else if (par->codec_id == AV_CODEC_ID_AC3) {
+        snprintf(str, size, "ac-3");
+    } else if (par->codec_id == AV_CODEC_ID_EAC3) {
+        snprintf(str, size, "ec-3");
+    } else {
+        return AVERROR(EINVAL);
+    }
+    return 0;
+}
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index af92e38bbd..636e770450 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -42,8 +42,6 @@
 
 #include "libavcodec/avcodec.h"
 
-#include "av1.h"
-#include "avc.h"
 #include "avformat.h"
 #include "avio_internal.h"
 #include "hlsplaylist.h"
@@ -51,11 +49,9 @@
 #include "http.h"
 #endif
 #include "internal.h"
-#include "isom.h"
 #include "mux.h"
 #include "os_support.h"
 #include "url.h"
-#include "vpcc.h"
 #include "dash.h"
 
 typedef enum {
@@ -206,18 +202,6 @@ typedef struct DASHContext {
     int64_t update_period;
 } DASHContext;
 
-static const struct codec_string {
-    enum AVCodecID id;
-    const char str[8];
-} codecs[] = {
-    { AV_CODEC_ID_VP8, "vp8" },
-    { AV_CODEC_ID_VP9, "vp9" },
-    { AV_CODEC_ID_VORBIS, "vorbis" },
-    { AV_CODEC_ID_OPUS, "opus" },
-    { AV_CODEC_ID_FLAC, "flac" },
-    { AV_CODEC_ID_NONE }
-};
-
 static int dashenc_io_open(AVFormatContext *s, AVIOContext **pb, char *filename,
                            AVDictionary **options) {
     DASHContext *c = s->priv_data;
@@ -327,117 +311,6 @@ static int init_segment_types(AVFormatContext *s)
     return 0;
 }
 
-static void set_vp9_codec_str(AVFormatContext *s, AVCodecParameters *par,
-                              AVRational *frame_rate, char *str, int size) {
-    VPCC vpcc;
-    int ret = ff_isom_get_vpcc_features(s, par, NULL, 0, frame_rate, &vpcc);
-    if (ret == 0) {
-        av_strlcatf(str, size, "vp09.%02d.%02d.%02d",
-                    vpcc.profile, vpcc.level, vpcc.bitdepth);
-    } else {
-        // Default to just vp9 in case of error while finding out profile or level
-        av_log(s, AV_LOG_WARNING, "Could not find VP9 profile and/or level\n");
-        av_strlcpy(str, "vp9", size);
-    }
-    return;
-}
-
-static void set_codec_str(AVFormatContext *s, AVCodecParameters *par,
-                          AVRational *frame_rate, char *str, int size)
-{
-    const AVCodecTag *tags[2] = { NULL, NULL };
-    uint32_t tag;
-    int i;
-
-    // common Webm codecs are not part of RFC 6381
-    for (i = 0; codecs[i].id != AV_CODEC_ID_NONE; i++)
-        if (codecs[i].id == par->codec_id) {
-            if (codecs[i].id == AV_CODEC_ID_VP9) {
-                set_vp9_codec_str(s, par, frame_rate, str, size);
-            } else {
-                av_strlcpy(str, codecs[i].str, size);
-            }
-            return;
-        }
-
-    // for codecs part of RFC 6381
-    if (par->codec_type == AVMEDIA_TYPE_VIDEO)
-        tags[0] = ff_codec_movvideo_tags;
-    else if (par->codec_type == AVMEDIA_TYPE_AUDIO)
-        tags[0] = ff_codec_movaudio_tags;
-    else
-        return;
-
-    tag = par->codec_tag;
-    if (!tag)
-        tag = av_codec_get_tag(tags, par->codec_id);
-    if (!tag)
-        return;
-    if (size < 5)
-        return;
-
-    AV_WL32(str, tag);
-    str[4] = '\0';
-    if (!strcmp(str, "mp4a") || !strcmp(str, "mp4v")) {
-        uint32_t oti;
-        tags[0] = ff_mp4_obj_type;
-        oti = av_codec_get_tag(tags, par->codec_id);
-        if (oti)
-            av_strlcatf(str, size, ".%02"PRIx32, oti);
-        else
-            return;
-
-        if (tag == MKTAG('m', 'p', '4', 'a')) {
-            if (par->extradata_size >= 2) {
-                int aot = par->extradata[0] >> 3;
-                if (aot == 31)
-                    aot = ((AV_RB16(par->extradata) >> 5) & 0x3f) + 32;
-                av_strlcatf(str, size, ".%d", aot);
-            }
-        } else if (tag == MKTAG('m', 'p', '4', 'v')) {
-            // Unimplemented, should output ProfileLevelIndication as a decimal number
-            av_log(s, AV_LOG_WARNING, "Incomplete RFC 6381 codec string for mp4v\n");
-        }
-    } else if (!strcmp(str, "avc1")) {
-        uint8_t *tmpbuf = NULL;
-        uint8_t *extradata = par->extradata;
-        int extradata_size = par->extradata_size;
-        if (!extradata_size)
-            return;
-        if (extradata[0] != 1) {
-            AVIOContext *pb;
-            if (avio_open_dyn_buf(&pb) < 0)
-                return;
-            if (ff_isom_write_avcc(pb, extradata, extradata_size) < 0) {
-                ffio_free_dyn_buf(&pb);
-                return;
-            }
-            extradata_size = avio_close_dyn_buf(pb, &extradata);
-            tmpbuf = extradata;
-        }
-
-        if (extradata_size >= 4)
-            av_strlcatf(str, size, ".%02x%02x%02x",
-                        extradata[1], extradata[2], extradata[3]);
-        av_free(tmpbuf);
-    } else if (!strcmp(str, "av01")) {
-        AV1SequenceParameters seq;
-        if (!par->extradata_size)
-            return;
-        if (ff_av1_parse_seq_header(&seq, par->extradata, par->extradata_size) < 0)
-            return;
-
-        av_strlcatf(str, size, ".%01u.%02u%s.%02u",
-                    seq.profile, seq.level, seq.tier ? "H" : "M", seq.bitdepth);
-        if (seq.color_description_present_flag)
-            av_strlcatf(str, size, ".%01u.%01u%01u%01u.%02u.%02u.%02u.%01u",
-                        seq.monochrome,
-                        seq.chroma_subsampling_x, seq.chroma_subsampling_y, seq.chroma_sample_position,
-                        seq.color_primaries, seq.transfer_characteristics, seq.matrix_coefficients,
-                        seq.color_range);
-    }
-}
-
 static int flush_dynbuf(DASHContext *c, OutputStream *os, int *range_length)
 {
     uint8_t *buffer;
@@ -1701,8 +1574,8 @@ static int dash_init(AVFormatContext *s)
             c->has_video = 1;
         }
 
-        set_codec_str(s, st->codecpar, &st->avg_frame_rate, os->codec_str,
-                      sizeof(os->codec_str));
+        ff_make_codec_str(s, st->codecpar, &st->avg_frame_rate, os->codec_str,
+                          sizeof(os->codec_str));
         os->first_pts = AV_NOPTS_VALUE;
         os->max_pts = AV_NOPTS_VALUE;
         os->last_dts = AV_NOPTS_VALUE;
@@ -1838,7 +1711,7 @@ static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
 
     memcpy(par->extradata, extradata, extradata_size);
 
-    set_codec_str(s, par, frame_rate, os->codec_str, sizeof(os->codec_str));
+    ff_make_codec_str(s, par, frame_rate, os->codec_str, sizeof(os->codec_str));
 
     return 0;
 }
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index d3db83093c..8e36887e06 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -44,13 +44,11 @@
 
 #include "avformat.h"
 #include "avio_internal.h"
-#include "avc.h"
 #if CONFIG_HTTP_PROTOCOL
 #include "http.h"
 #endif
 #include "hlsplaylist.h"
 #include "internal.h"
-#include "nal.h"
 #include "mux.h"
 #include "os_support.h"
 #include "url.h"
@@ -358,118 +356,10 @@ static void write_codec_attr(AVStream *st, VariantStream *vs)
     if (vs->attr_status == CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN)
         return;
 
-    if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
-        uint8_t *data = st->codecpar->extradata;
-        if (data) {
-            const uint8_t *p;
-
-            if (AV_RB32(data) == 0x01 && (data[4] & 0x1F) == 7)
-                p = &data[5];
-            else if (AV_RB24(data) == 0x01 && (data[3] & 0x1F) == 7)
-                p = &data[4];
-            else if (data[0] == 0x01)  /* avcC */
-                p = &data[1];
-            else
-                goto fail;
-            snprintf(attr, sizeof(attr),
-                     "avc1.%02x%02x%02x", p[0], p[1], p[2]);
-        } else {
-            goto fail;
-        }
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
-        uint8_t *data = st->codecpar->extradata;
-        int profile = AV_PROFILE_UNKNOWN;
-        uint32_t profile_compatibility = AV_PROFILE_UNKNOWN;
-        char tier = 0;
-        int level = AV_LEVEL_UNKNOWN;
-        char constraints[8] = "";
-
-        if (st->codecpar->profile != AV_PROFILE_UNKNOWN)
-            profile = st->codecpar->profile;
-        if (st->codecpar->level != AV_LEVEL_UNKNOWN)
-            level = st->codecpar->level;
-
-        /* check the boundary of data which from current position is small than extradata_size */
-        while (data && (data - st->codecpar->extradata + 19) < st->codecpar->extradata_size) {
-            /* get HEVC SPS NAL and seek to profile_tier_level */
-            if (!(data[0] | data[1] | data[2]) && data[3] == 1 && ((data[4] & 0x7E) == 0x42)) {
-                uint8_t *rbsp_buf;
-                int remain_size = 0;
-                int rbsp_size = 0;
-                uint32_t profile_compatibility_flags = 0;
-                uint8_t high_nibble = 0;
-                /* skip start code + nalu header */
-                data += 6;
-                /* process by reference General NAL unit syntax */
-                remain_size = st->codecpar->extradata_size - (data - st->codecpar->extradata);
-                rbsp_buf = ff_nal_unit_extract_rbsp(data, remain_size, &rbsp_size, 0);
-                if (!rbsp_buf)
-                    return;
-                if (rbsp_size < 13) {
-                    av_freep(&rbsp_buf);
-                    break;
-                }
-                /* skip sps_video_parameter_set_id   u(4),
-                 *      sps_max_sub_layers_minus1    u(3),
-                 *  and sps_temporal_id_nesting_flag u(1)
-                 *
-                 * TIER represents the general_tier_flag, with 'L' indicating the flag is 0,
-                 * and 'H' indicating the flag is 1
-                 */
-                tier = (rbsp_buf[1] & 0x20) == 0 ? 'L' : 'H';
-                profile = rbsp_buf[1] & 0x1f;
-                /* PROFILE_COMPATIBILITY is general_profile_compatibility_flags, but in reverse bit order,
-                 * in a hexadecimal representation (leading zeroes may be omitted).
-                 */
-                profile_compatibility_flags = AV_RB32(rbsp_buf + 2);
-                /* revise these bits to get the profile compatibility value */
-                profile_compatibility_flags = ((profile_compatibility_flags & 0x55555555U) << 1) | ((profile_compatibility_flags >> 1) & 0x55555555U);
-                profile_compatibility_flags = ((profile_compatibility_flags & 0x33333333U) << 2) | ((profile_compatibility_flags >> 2) & 0x33333333U);
-                profile_compatibility_flags = ((profile_compatibility_flags & 0x0F0F0F0FU) << 4) | ((profile_compatibility_flags >> 4) & 0x0F0F0F0FU);
-                profile_compatibility_flags = ((profile_compatibility_flags & 0x00FF00FFU) << 8) | ((profile_compatibility_flags >> 8) & 0x00FF00FFU);
-                profile_compatibility = (profile_compatibility_flags << 16) | (profile_compatibility_flags >> 16);
-                /* skip 8 + 8 + 32
-                 * CONSTRAINTS is a hexadecimal representation of the general_constraint_indicator_flags.
-                 * each byte is separated by a '.', and trailing zero bytes may be omitted.
-                 * drop the trailing zero bytes refer to ISO/IEC14496-15.
-                 */
-                high_nibble = rbsp_buf[7] >> 4;
-                snprintf(constraints, sizeof(constraints),
-                         high_nibble ? "%02x.%x" : "%02x",
-                         rbsp_buf[6], high_nibble);
-                /* skip 8 + 8 + 32 + 4 + 43 + 1 bit */
-                level = rbsp_buf[12];
-                av_freep(&rbsp_buf);
-                break;
-            }
-            data++;
-        }
-        if (st->codecpar->codec_tag == MKTAG('h','v','c','1') &&
-            profile != AV_PROFILE_UNKNOWN &&
-            profile_compatibility != AV_PROFILE_UNKNOWN &&
-            tier != 0 &&
-            level != AV_LEVEL_UNKNOWN &&
-            constraints[0] != '\0') {
-            snprintf(attr, sizeof(attr), "%s.%d.%x.%c%d.%s", av_fourcc2str(st->codecpar->codec_tag), profile, profile_compatibility, tier, level, constraints);
-        } else
-            goto fail;
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_MP2) {
-        snprintf(attr, sizeof(attr), "mp4a.40.33");
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_MP3) {
-        snprintf(attr, sizeof(attr), "mp4a.40.34");
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_AAC) {
-        if (st->codecpar->profile != AV_PROFILE_UNKNOWN)
-            snprintf(attr, sizeof(attr), "mp4a.40.%d", st->codecpar->profile+1);
-        else
-            // This is for backward compatibility with the previous implementation.
-            snprintf(attr, sizeof(attr), "mp4a.40.2");
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_AC3) {
-        snprintf(attr, sizeof(attr), "ac-3");
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_EAC3) {
-        snprintf(attr, sizeof(attr), "ec-3");
-    } else {
+    if (ff_make_codec_str(vs->avf, st->codecpar, &st->avg_frame_rate,
+                          attr, sizeof(attr)) < 0)
         goto fail;
-    }
+
     // Don't write the same attribute multiple times
     if (!av_stristr(vs->codec_attr, attr)) {
         snprintf(vs->codec_attr + codec_strlen,
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 1a50ba07d3..4cd55a0426 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -661,4 +661,18 @@ int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestam
  */
 int ff_parse_opts_from_query_string(void *obj, const char *str, int allow_unkown);
 
+/**
+ * Make a RFC 6381 like string describing a codec.
+ *
+ * @param logctx a context for potential log messages
+ * @param par pointer to an AVCodecParameters struct describing the codec
+ * @param frame_rate an optional pointer to AVRational for the frame rate,
+ *                   for deciding the right profile for video codecs
+ * @param str the output string buffer
+ * @param size the size of the string pointed to by str
+ * @return <0 on error
+ */
+int ff_make_codec_str(void *logctx, AVCodecParameters *par,
+                      AVRational *frame_rate, char *str, int size);
+
 #endif /* AVFORMAT_INTERNAL_H */
-- 
2.49.1


From b8748f7664cbe070e620b05ced918c1d680cd2a0 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Storsj=C3=B6?= <martin@martin.st>
Date: Thu, 23 Oct 2025 13:49:15 +0300
Subject: [PATCH 3/3] avformat: Make av_make_codec_str a public API

Implementing this requires a bunch of nontrivial codec specific
parsing. As libavformat already implements this, expose it
for users as well.
---
 doc/APIchanges            |  3 +++
 libavformat/Makefile      | 12 ++++++++----
 libavformat/avformat.h    | 14 ++++++++++++++
 libavformat/codecstring.c |  2 +-
 libavformat/dashenc.c     |  4 ++--
 libavformat/hlsenc.c      |  2 +-
 libavformat/internal.h    | 14 --------------
 libavformat/version.h     |  4 ++--
 8 files changed, 31 insertions(+), 24 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 6e7f5d2037..06acccb605 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -2,6 +2,9 @@ The last version increases of all libraries were on 2025-03-28
 
 API changes, most recent first:
 
+2025-10-xx - xxxxxxxxxx - lavf 62.x.xxx - avformat.h
+  Add av_make_codec_str
+
 2025-08-xx - xxxxxxxxxx - lavf 62.6.100 - oggparsevorbis.h oggparseopus.h oggparseflac.h
   Drop header packets from secondary chained ogg/{flac, opus, vorbis} streams
   from demuxer output.
diff --git a/libavformat/Makefile b/libavformat/Makefile
index 86177af2f4..f78f673e58 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -7,9 +7,11 @@ HEADERS = avformat.h                                                    \
           version_major.h                                               \
 
 OBJS = allformats.o         \
+       av1.o                \
        avformat.o           \
        avio.o               \
        aviobuf.o            \
+       codecstring.o        \
        demux.o              \
        demux_utils.o        \
        dump.o               \
@@ -21,6 +23,7 @@ OBJS = allformats.o         \
        metadata.o           \
        mux.o                \
        mux_utils.o          \
+       nal.o                \
        options.o            \
        os_support.o         \
        protocols.o          \
@@ -31,12 +34,13 @@ OBJS = allformats.o         \
        urldecode.o          \
        utils.o              \
        version.o            \
+       vpcc.o               \
 
 OBJS-$(HAVE_LIBC_MSVCRT)                 += file_open.o
 
 # subsystems
 OBJS-$(CONFIG_ISO_MEDIA)                 += isom.o
-OBJS-$(CONFIG_ISO_WRITER)                += av1.o avc.o hevc.o nal.o vvc.o vpcc.o
+OBJS-$(CONFIG_ISO_WRITER)                += avc.o hevc.o vvc.o
 OBJS-$(CONFIG_IAMFDEC)                   += iamf_reader.o iamf_parse.o iamf.o
 OBJS-$(CONFIG_IAMFENC)                   += iamf_writer.o iamf.o
 OBJS-$(CONFIG_NETWORK)                   += network.o
@@ -179,7 +183,7 @@ OBJS-$(CONFIG_CONCAT_DEMUXER)            += concatdec.o
 OBJS-$(CONFIG_CRC_MUXER)                 += crcenc.o
 OBJS-$(CONFIG_DATA_DEMUXER)              += rawdec.o
 OBJS-$(CONFIG_DATA_MUXER)                += rawenc.o
-OBJS-$(CONFIG_DASH_MUXER)                += dash.o dashenc.o hlsplaylist.o codecstring.o
+OBJS-$(CONFIG_DASH_MUXER)                += dash.o dashenc.o hlsplaylist.o
 OBJS-$(CONFIG_DASH_DEMUXER)              += dash.o dashdec.o
 OBJS-$(CONFIG_DAUD_DEMUXER)              += dauddec.o
 OBJS-$(CONFIG_DAUD_MUXER)                += daudenc.o
@@ -267,7 +271,7 @@ OBJS-$(CONFIG_HEVC_MUXER)                += rawenc.o
 OBJS-$(CONFIG_EVC_DEMUXER)               += evcdec.o rawdec.o
 OBJS-$(CONFIG_EVC_MUXER)                 += rawenc.o
 OBJS-$(CONFIG_HLS_DEMUXER)               += hls.o hls_sample_encryption.o
-OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o hlsplaylist.o codecstring.o
+OBJS-$(CONFIG_HLS_MUXER)                 += hlsenc.o hlsplaylist.o
 OBJS-$(CONFIG_HNM_DEMUXER)               += hnm.o
 OBJS-$(CONFIG_HXVS_DEMUXER)              += hxvs.o
 OBJS-$(CONFIG_IAMF_DEMUXER)              += iamfdec.o
@@ -634,7 +638,7 @@ OBJS-$(CONFIG_WAV_DEMUXER)               += wavdec.o pcm.o
 OBJS-$(CONFIG_WAV_MUXER)                 += wavenc.o
 OBJS-$(CONFIG_WC3_DEMUXER)               += wc3movie.o
 OBJS-$(CONFIG_WEBM_MUXER)                += matroskaenc.o matroska.o \
-                                            av1.o avlanguage.o
+                                            avlanguage.o
 OBJS-$(CONFIG_WEBM_DASH_MANIFEST_MUXER)  += webmdashenc.o
 OBJS-$(CONFIG_WEBM_CHUNK_MUXER)          += webm_chunk.o
 OBJS-$(CONFIG_WEBP_MUXER)                += webpenc.o
diff --git a/libavformat/avformat.h b/libavformat/avformat.h
index a7446546e5..21356b2e84 100644
--- a/libavformat/avformat.h
+++ b/libavformat/avformat.h
@@ -2890,6 +2890,20 @@ int av_match_ext(const char *filename, const char *extensions);
 int avformat_query_codec(const AVOutputFormat *ofmt, enum AVCodecID codec_id,
                          int std_compliance);
 
+/**
+ * Make a RFC 6381 like string describing a codec.
+ *
+ * @param logctx a context for potential log messages
+ * @param par pointer to an AVCodecParameters struct describing the codec
+ * @param frame_rate an optional pointer to AVRational for the frame rate,
+ *                   for deciding the right profile for video codecs
+ * @param str the output string buffer
+ * @param size the size of the string pointed to by str
+ * @return <0 on error
+ */
+int av_make_codec_str(void *logctx, AVCodecParameters *par,
+                      AVRational *frame_rate, char *str, int size);
+
 /**
  * @defgroup riff_fourcc RIFF FourCCs
  * @{
diff --git a/libavformat/codecstring.c b/libavformat/codecstring.c
index 589131fbea..73e60b13a1 100644
--- a/libavformat/codecstring.c
+++ b/libavformat/codecstring.c
@@ -59,7 +59,7 @@ static void set_vp9_codec_str(void *logctx, AVCodecParameters *par,
     }
 }
 
-int ff_make_codec_str(void *logctx, AVCodecParameters *par,
+int av_make_codec_str(void *logctx, AVCodecParameters *par,
                       AVRational *frame_rate, char *str, int size)
 {
     int i;
diff --git a/libavformat/dashenc.c b/libavformat/dashenc.c
index 636e770450..8a19fcd764 100644
--- a/libavformat/dashenc.c
+++ b/libavformat/dashenc.c
@@ -1574,7 +1574,7 @@ static int dash_init(AVFormatContext *s)
             c->has_video = 1;
         }
 
-        ff_make_codec_str(s, st->codecpar, &st->avg_frame_rate, os->codec_str,
+        av_make_codec_str(s, st->codecpar, &st->avg_frame_rate, os->codec_str,
                           sizeof(os->codec_str));
         os->first_pts = AV_NOPTS_VALUE;
         os->max_pts = AV_NOPTS_VALUE;
@@ -1711,7 +1711,7 @@ static int update_stream_extradata(AVFormatContext *s, OutputStream *os,
 
     memcpy(par->extradata, extradata, extradata_size);
 
-    ff_make_codec_str(s, par, frame_rate, os->codec_str, sizeof(os->codec_str));
+    av_make_codec_str(s, par, frame_rate, os->codec_str, sizeof(os->codec_str));
 
     return 0;
 }
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 8e36887e06..a898ffcb93 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -356,7 +356,7 @@ static void write_codec_attr(AVStream *st, VariantStream *vs)
     if (vs->attr_status == CODEC_ATTRIBUTE_WILL_NOT_BE_WRITTEN)
         return;
 
-    if (ff_make_codec_str(vs->avf, st->codecpar, &st->avg_frame_rate,
+    if (av_make_codec_str(vs->avf, st->codecpar, &st->avg_frame_rate,
                           attr, sizeof(attr)) < 0)
         goto fail;
 
diff --git a/libavformat/internal.h b/libavformat/internal.h
index 4cd55a0426..1a50ba07d3 100644
--- a/libavformat/internal.h
+++ b/libavformat/internal.h
@@ -661,18 +661,4 @@ int ff_dict_set_timestamp(AVDictionary **dict, const char *key, int64_t timestam
  */
 int ff_parse_opts_from_query_string(void *obj, const char *str, int allow_unkown);
 
-/**
- * Make a RFC 6381 like string describing a codec.
- *
- * @param logctx a context for potential log messages
- * @param par pointer to an AVCodecParameters struct describing the codec
- * @param frame_rate an optional pointer to AVRational for the frame rate,
- *                   for deciding the right profile for video codecs
- * @param str the output string buffer
- * @param size the size of the string pointed to by str
- * @return <0 on error
- */
-int ff_make_codec_str(void *logctx, AVCodecParameters *par,
-                      AVRational *frame_rate, char *str, int size);
-
 #endif /* AVFORMAT_INTERNAL_H */
diff --git a/libavformat/version.h b/libavformat/version.h
index 35996a14ba..70c554c19c 100644
--- a/libavformat/version.h
+++ b/libavformat/version.h
@@ -31,8 +31,8 @@
 
 #include "version_major.h"
 
-#define LIBAVFORMAT_VERSION_MINOR   6
-#define LIBAVFORMAT_VERSION_MICRO 101
+#define LIBAVFORMAT_VERSION_MINOR   7
+#define LIBAVFORMAT_VERSION_MICRO 100
 
 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \
                                                LIBAVFORMAT_VERSION_MINOR, \
-- 
2.49.1

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2025-10-23 10:57 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-10-23 10:57 [FFmpeg-devel] [PATCH] avformat: Combine hlsenc/dashenc codec string generation, make public API (PR #20739) Martin Storsjö via ffmpeg-devel

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