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 v4 1/6] avformat/mp4: add muxer support for H266/VVC
       [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
@ 2024-01-29 14:54 ` Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 2/6] avformat/mpegtsenc: refact mpegts_check_bitstream to loop up table Nuo Mi
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: Nuo Mi @ 2024-01-29 14:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Nuo Mi, Thomas Siedel

From: Thomas Siedel <thomas.ff@spin-digital.com>

Add muxer for vvcc byte stream format.
Add AV_CODEC_ID_VVC to ff_mp4_obj_type.
Add AV_CODEC_ID_VVC to ISO Media codec (VvcConfigurationBox vvi1,
vvc1 defined in ISO/IEC 14496-15:2021).
Add VvcConfigurationBox vvcC which extends FullBox type in
ISO/IEC 14496-15:2021.

Tested with:
    ffmpeg -i NovosobornayaSquare_1920x1080.mp4 -c:v libvvenc test.mp4 && ffmpeg -i test.mp4 -f null -
    ffmpeg -i NovosobornayaSquare_1920x1080.mp4 -c:v copy test.mp4     && ffmpeg -i test.mp4 -f md5 -

Signed-off-by: Thomas Siedel <thomas.ff@spin-digital.com>
Co-Authored-By: Nuo Mi <nuomi2021@gmail.com>
---
 libavformat/Makefile |   6 +-
 libavformat/isom.c   |   1 +
 libavformat/mov.c    |   6 +
 libavformat/movenc.c |  41 +-
 libavformat/vvc.c    | 971 +++++++++++++++++++++++++++++++++++++++++++
 libavformat/vvc.h    |  99 +++++
 6 files changed, 1120 insertions(+), 4 deletions(-)
 create mode 100644 libavformat/vvc.c
 create mode 100644 libavformat/vvc.h

diff --git a/libavformat/Makefile b/libavformat/Makefile
index dcc99eeac4..05b9b8a115 100644
--- a/libavformat/Makefile
+++ b/libavformat/Makefile
@@ -343,7 +343,7 @@ OBJS-$(CONFIG_MATROSKA_DEMUXER)          += matroskadec.o matroska.o  \
                                             oggparsevorbis.o vorbiscomment.o \
                                             qtpalette.o replaygain.o dovi_isom.o
 OBJS-$(CONFIG_MATROSKA_MUXER)            += matroskaenc.o matroska.o \
-                                            av1.o avc.o hevc.o \
+                                            av1.o avc.o hevc.o vvc.o\
                                             flacenc_header.o avlanguage.o \
                                             vorbiscomment.o wv.o dovi_isom.o
 OBJS-$(CONFIG_MCA_DEMUXER)               += mca.o
@@ -365,7 +365,7 @@ OBJS-$(CONFIG_MODS_DEMUXER)              += mods.o
 OBJS-$(CONFIG_MOFLEX_DEMUXER)            += moflex.o
 OBJS-$(CONFIG_MOV_DEMUXER)               += mov.o mov_chan.o mov_esds.o \
                                             qtpalette.o replaygain.o dovi_isom.o
-OBJS-$(CONFIG_MOV_MUXER)                 += movenc.o av1.o avc.o hevc.o vpcc.o \
+OBJS-$(CONFIG_MOV_MUXER)                 += movenc.o av1.o avc.o hevc.o vvc.o vpcc.o \
                                             movenchint.o mov_chan.o rtp.o \
                                             movenccenc.o movenc_ttml.o rawutils.o \
                                             dovi_isom.o evc.o
@@ -520,7 +520,7 @@ OBJS-$(CONFIG_RTP_MUXER)                 += rtp.o         \
                                             rtpenc_vp8.o  \
                                             rtpenc_vp9.o                \
                                             rtpenc_xiph.o \
-                                            avc.o hevc.o
+                                            avc.o hevc.o vvc.o
 OBJS-$(CONFIG_RTSP_DEMUXER)              += rtsp.o rtspdec.o httpauth.o \
                                             urldecode.o
 OBJS-$(CONFIG_RTSP_MUXER)                += rtsp.o rtspenc.o httpauth.o \
diff --git a/libavformat/isom.c b/libavformat/isom.c
index 6d019881e5..9fbccd4437 100644
--- a/libavformat/isom.c
+++ b/libavformat/isom.c
@@ -36,6 +36,7 @@ const AVCodecTag ff_mp4_obj_type[] = {
     { AV_CODEC_ID_MPEG4       , 0x20 },
     { AV_CODEC_ID_H264        , 0x21 },
     { AV_CODEC_ID_HEVC        , 0x23 },
+    { AV_CODEC_ID_VVC         , 0x33 },
     { AV_CODEC_ID_AAC         , 0x40 },
     { AV_CODEC_ID_MP4ALS      , 0x40 }, /* 14496-3 ALS */
     { AV_CODEC_ID_MPEG2VIDEO  , 0x61 }, /* MPEG-2 Main */
diff --git a/libavformat/mov.c b/libavformat/mov.c
index 4cffd6c7db..cf931d4594 100644
--- a/libavformat/mov.c
+++ b/libavformat/mov.c
@@ -2123,6 +2123,11 @@ static int mov_read_glbl(MOVContext *c, AVIOContext *pb, MOVAtom atom)
     if ((uint64_t)atom.size > (1<<30))
         return AVERROR_INVALIDDATA;
 
+    if (atom.type == MKTAG('v','v','c','C')) {
+        avio_skip(pb, 4);
+        atom.size -= 4;
+    }
+
     if (atom.size >= 10) {
         // Broken files created by legacy versions of libavformat will
         // wrap a whole fiel atom inside of a glbl atom.
@@ -8129,6 +8134,7 @@ static const MOVParseTableEntry mov_default_parse_table[] = {
 { MKTAG('s','g','p','d'), mov_read_sgpd },
 { MKTAG('s','b','g','p'), mov_read_sbgp },
 { MKTAG('h','v','c','C'), mov_read_glbl },
+{ MKTAG('v','v','c','C'), mov_read_glbl },
 { MKTAG('u','u','i','d'), mov_read_uuid },
 { MKTAG('C','i','n', 0x8e), mov_read_targa_y216 },
 { MKTAG('f','r','e','e'), mov_read_free },
diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 8a27afbc57..611a2a26f5 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -68,6 +68,7 @@
 #include "ttmlenc.h"
 #include "version.h"
 #include "vpcc.h"
+#include "vvc.h"
 
 static const AVOption options[] = {
     { "brand",    "Override major brand", offsetof(MOVMuxContext, major_brand),   AV_OPT_TYPE_STRING, {.str = NULL}, .flags = AV_OPT_FLAG_ENCODING_PARAM },
@@ -1473,6 +1474,23 @@ static int mov_write_evcc_tag(AVIOContext *pb, MOVTrack *track)
     return update_size(pb, pos);
 }
 
+static int mov_write_vvcc_tag(AVIOContext *pb, MOVTrack *track)
+{
+    int64_t pos = avio_tell(pb);
+
+    avio_wb32(pb, 0);
+    ffio_wfourcc(pb, "vvcC");
+
+    avio_w8  (pb, 0); /* version */
+    avio_wb24(pb, 0); /* flags */
+
+    if (track->tag == MKTAG('v','v','c','1'))
+        ff_isom_write_vvcc(pb, track->vos_data, track->vos_len, 1);
+    else
+        ff_isom_write_vvcc(pb, track->vos_data, track->vos_len, 0);
+    return update_size(pb, pos);
+}
+
 /* also used by all avid codecs (dv, imx, meridien) and their variants */
 static int mov_write_avid_tag(AVIOContext *pb, MOVTrack *track)
 {
@@ -2382,6 +2400,8 @@ static int mov_write_video_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex
         avid = 1;
     } else if (track->par->codec_id == AV_CODEC_ID_HEVC)
         mov_write_hvcc_tag(pb, track);
+    else if (track->par->codec_id == AV_CODEC_ID_VVC)
+        mov_write_vvcc_tag(pb, track);
     else if (track->par->codec_id == AV_CODEC_ID_H264 && !TAG_IS_AVCI(track->tag)) {
         mov_write_avcc_tag(pb, track);
         if (track->mode == MODE_IPOD)
@@ -6170,6 +6190,7 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
     if ((par->codec_id == AV_CODEC_ID_DNXHD ||
          par->codec_id == AV_CODEC_ID_H264 ||
          par->codec_id == AV_CODEC_ID_HEVC ||
+         par->codec_id == AV_CODEC_ID_VVC ||
          par->codec_id == AV_CODEC_ID_VP9 ||
          par->codec_id == AV_CODEC_ID_EVC ||
          par->codec_id == AV_CODEC_ID_TRUEHD) && !trk->vos_len &&
@@ -6235,6 +6256,18 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
                 size = ff_hevc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
             }
         }
+    } else if (par->codec_id == AV_CODEC_ID_VVC && trk->vos_len > 6 &&
+             (AV_RB24(trk->vos_data) == 1 || AV_RB32(trk->vos_data) == 1)) {
+        /* extradata is Annex B, assume the bitstream is too and convert it */
+        if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
+            ret = ff_vvc_annexb2mp4_buf(pkt->data, &reformatted_data,
+                                        &size, 0, NULL);
+            if (ret < 0)
+                return ret;
+            avio_write(pb, reformatted_data, size);
+        } else {
+            size = ff_vvc_annexb2mp4(pb, pkt->data, pkt->size, 0, NULL);
+        }
     } else if (par->codec_id == AV_CODEC_ID_AV1) {
         if (trk->hint_track >= 0 && trk->hint_track < mov->nb_tracks) {
             ret = ff_av1_filter_obus_buf(pkt->data, &reformatted_data,
@@ -6281,6 +6314,9 @@ int ff_mov_write_packet(AVFormatContext *s, AVPacket *pkt)
             } else if(par->codec_id == AV_CODEC_ID_HEVC && par->extradata_size > 21) {
                 int nal_size_length = (par->extradata[21] & 0x3) + 1;
                 ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
+            } else if(par->codec_id == AV_CODEC_ID_VVC && par->extradata_size > 4) {
+                int nal_size_length = ((par->extradata[4] >> 1) & 0x3) + 1;
+                ret = ff_mov_cenc_avc_write_nal_units(s, &trk->cenc, nal_size_length, pb, pkt->data, size);
             } else {
                 ret = ff_mov_cenc_write_packet(&trk->cenc, pb, pkt->data, size);
             }
@@ -7363,7 +7399,8 @@ static int mov_init(AVFormatContext *s)
 
         if (mov->encryption_scheme == MOV_ENC_CENC_AES_CTR) {
             ret = ff_mov_cenc_init(&track->cenc, mov->encryption_key,
-                (track->par->codec_id == AV_CODEC_ID_H264 || track->par->codec_id == AV_CODEC_ID_HEVC),
+                (track->par->codec_id == AV_CODEC_ID_H264 || track->par->codec_id == AV_CODEC_ID_HEVC ||
+                 track->par->codec_id == AV_CODEC_ID_VVC),
                 s->flags & AVFMT_FLAG_BITEXACT);
             if (ret)
                 return ret;
@@ -7832,6 +7869,8 @@ static const AVCodecTag codec_mp4_tags[] = {
     { AV_CODEC_ID_HEVC,            MKTAG('h', 'e', 'v', '1') },
     { AV_CODEC_ID_HEVC,            MKTAG('h', 'v', 'c', '1') },
     { AV_CODEC_ID_HEVC,            MKTAG('d', 'v', 'h', '1') },
+    { AV_CODEC_ID_VVC,             MKTAG('v', 'v', 'c', '1') },
+    { AV_CODEC_ID_VVC,             MKTAG('v', 'v', 'i', '1') },
     { AV_CODEC_ID_EVC,             MKTAG('e', 'v', 'c', '1') },
     { AV_CODEC_ID_MPEG2VIDEO,      MKTAG('m', 'p', '4', 'v') },
     { AV_CODEC_ID_MPEG1VIDEO,      MKTAG('m', 'p', '4', 'v') },
diff --git a/libavformat/vvc.c b/libavformat/vvc.c
new file mode 100644
index 0000000000..14a4c0a2f3
--- /dev/null
+++ b/libavformat/vvc.c
@@ -0,0 +1,971 @@
+/*
+ * H.266/VVC helper functions for muxers
+ *
+ * Copyright (C) 2022, Thomas Siedel
+ *
+ * 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 "libavcodec/get_bits.h"
+#include "libavcodec/golomb.h"
+#include "libavcodec/vvc.h"
+#include "libavutil/intreadwrite.h"
+#include "avc.h"
+#include "avio.h"
+#include "avio_internal.h"
+#include "vvc.h"
+
+typedef struct VVCCNALUnitArray {
+    uint8_t array_completeness;
+    uint8_t NAL_unit_type;
+    uint16_t num_nalus;
+    uint16_t *nal_unit_length;
+    uint8_t **nal_unit;
+} VVCCNALUnitArray;
+
+typedef struct VVCPTLRecord {
+    uint8_t num_bytes_constraint_info;
+    uint8_t general_profile_idc;
+    uint8_t general_tier_flag;
+    uint8_t general_level_idc;
+    uint8_t ptl_frame_only_constraint_flag;
+    uint8_t ptl_multilayer_enabled_flag;
+    uint8_t general_constraint_info[9];
+    uint8_t ptl_sublayer_level_present_flag[VVC_MAX_SUBLAYERS - 1];
+    uint8_t sublayer_level_idc[VVC_MAX_SUBLAYERS - 1];
+    uint8_t ptl_num_sub_profiles;
+    uint32_t general_sub_profile_idc[VVC_MAX_SUB_PROFILES];
+} VVCPTLRecord;
+
+typedef struct VVCDecoderConfigurationRecord {
+    uint8_t lengthSizeMinusOne;
+    uint8_t ptl_present_flag;
+    uint16_t ols_idx;
+    uint8_t num_sublayers;
+    uint8_t constant_frame_rate;
+    uint8_t chroma_format_idc;
+    uint8_t bit_depth_minus8;
+    VVCPTLRecord ptl;
+    uint16_t max_picture_width;
+    uint16_t max_picture_height;
+    uint16_t avg_frame_rate;
+    uint8_t num_of_arrays;
+    VVCCNALUnitArray *array;
+} VVCDecoderConfigurationRecord;
+
+typedef struct VVCCProfileTierLevel {
+    uint8_t profile_idc;
+    uint8_t tier_flag;
+    uint8_t general_level_idc;
+    uint8_t ptl_frame_only_constraint_flag;
+    uint8_t ptl_multilayer_enabled_flag;
+// general_constraint_info
+    uint8_t gci_present_flag;
+    uint8_t gci_general_constraints[9];
+    uint8_t gci_num_reserved_bits;
+// end general_constraint_info
+    uint8_t ptl_sublayer_level_present_flag[VVC_MAX_SUBLAYERS - 1];
+    uint8_t sublayer_level_idc[VVC_MAX_SUBLAYERS - 1];
+    uint8_t ptl_num_sub_profiles;
+    uint32_t general_sub_profile_idc[VVC_MAX_SUB_PROFILES];
+} VVCCProfileTierLevel;
+
+static void vvcc_update_ptl(VVCDecoderConfigurationRecord *vvcc,
+                            VVCCProfileTierLevel *ptl)
+{
+    /*
+     * The level indication general_level_idc must indicate a level of
+     * capability equal to or greater than the highest level indicated for the
+     * highest tier in all the parameter sets.
+     */
+    if (vvcc->ptl.general_tier_flag < ptl->tier_flag)
+        vvcc->ptl.general_level_idc = ptl->general_level_idc;
+    else
+        vvcc->ptl.general_level_idc =
+            FFMAX(vvcc->ptl.general_level_idc, ptl->general_level_idc);
+
+    /*
+     * The tier indication general_tier_flag must indicate a tier equal to or
+     * greater than the highest tier indicated in all the parameter sets.
+     */
+    vvcc->ptl.general_tier_flag =
+        FFMAX(vvcc->ptl.general_tier_flag, ptl->tier_flag);
+
+    /*
+     * The profile indication general_profile_idc must indicate a profile to
+     * which the stream associated with this configuration record conforms.
+     *
+     * If the sequence parameter sets are marked with different profiles, then
+     * the stream may need examination to determine which profile, if any, the
+     * entire stream conforms to. If the entire stream is not examined, or the
+     * examination reveals that there is no profile to which the entire stream
+     * conforms, then the entire stream must be split into two or more
+     * sub-streams with separate configuration records in which these rules can
+     * be met.
+     *
+     * Note: set the profile to the highest value for the sake of simplicity.
+     */
+    vvcc->ptl.general_profile_idc =
+        FFMAX(vvcc->ptl.general_profile_idc, ptl->profile_idc);
+
+    /*
+     * Each bit in flags may only be set if all
+     * the parameter sets set that bit.
+     */
+    vvcc->ptl.ptl_frame_only_constraint_flag &=
+        ptl->ptl_frame_only_constraint_flag;
+    vvcc->ptl.ptl_multilayer_enabled_flag &= ptl->ptl_multilayer_enabled_flag;
+
+    /*
+     * Constraints Info
+     */
+    if (ptl->gci_present_flag) {
+        vvcc->ptl.num_bytes_constraint_info = 9;
+        memcpy(&vvcc->ptl.general_constraint_info[0],
+               &ptl->gci_general_constraints[0], sizeof(uint8_t) * 9);
+
+    } else {
+        vvcc->ptl.num_bytes_constraint_info = 1;
+        memset(&vvcc->ptl.general_constraint_info[0], 0, sizeof(uint8_t) * 9);
+    }
+
+    /*
+     * Each bit in flags may only be set if one of
+     * the parameter sets set that bit.
+     */
+    memset(vvcc->ptl.ptl_sublayer_level_present_flag, 0,
+           sizeof(uint8_t) * vvcc->num_sublayers - 1);
+    memset(vvcc->ptl.sublayer_level_idc, 0,
+           sizeof(uint8_t) * vvcc->num_sublayers - 1);
+
+    for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
+        vvcc->ptl.ptl_sublayer_level_present_flag[i] |=
+            ptl->ptl_sublayer_level_present_flag[i];
+        if (vvcc->ptl.ptl_sublayer_level_present_flag[i]) {
+            vvcc->ptl.sublayer_level_idc[i] =
+                FFMAX(vvcc->ptl.sublayer_level_idc[i],
+                      ptl->sublayer_level_idc[i]);
+        } else {
+            if (i == vvcc->num_sublayers - 1) {
+                vvcc->ptl.sublayer_level_idc[i] = vvcc->ptl.general_level_idc;
+            } else {
+                vvcc->ptl.sublayer_level_idc[i] =
+                    vvcc->ptl.sublayer_level_idc[i + 1];
+            }
+        }
+    }
+
+    vvcc->ptl.ptl_num_sub_profiles =
+        FFMAX(vvcc->ptl.ptl_num_sub_profiles, ptl->ptl_num_sub_profiles);
+    if (vvcc->ptl.ptl_num_sub_profiles) {
+        for (int i = 0; i < vvcc->ptl.ptl_num_sub_profiles; i++) {
+            vvcc->ptl.general_sub_profile_idc[i] =
+                ptl->general_sub_profile_idc[i];
+        }
+    }
+}
+
+static void vvcc_parse_ptl(GetBitContext *gb,
+                           VVCDecoderConfigurationRecord *vvcc,
+                           unsigned int profileTierPresentFlag,
+                           unsigned int max_sub_layers_minus1)
+{
+    VVCCProfileTierLevel general_ptl;
+    int j;
+
+    if (profileTierPresentFlag) {
+        general_ptl.profile_idc = get_bits(gb, 7);
+        general_ptl.tier_flag = get_bits1(gb);
+    }
+    general_ptl.general_level_idc = get_bits(gb, 8);
+
+    general_ptl.ptl_frame_only_constraint_flag = get_bits1(gb);
+    general_ptl.ptl_multilayer_enabled_flag = get_bits1(gb);
+    if (profileTierPresentFlag) {       // parse constraint info
+        general_ptl.gci_present_flag = get_bits1(gb);
+        if (general_ptl.gci_present_flag) {
+            for (j = 0; j < 8; j++)
+                general_ptl.gci_general_constraints[j] = get_bits(gb, 8);
+            general_ptl.gci_general_constraints[8] = get_bits(gb, 7);
+
+            general_ptl.gci_num_reserved_bits = get_bits(gb, 8);
+            skip_bits(gb, general_ptl.gci_num_reserved_bits);
+        }
+        while (gb->index % 8 != 0)
+            skip_bits1(gb);
+    }
+
+    for (int i = max_sub_layers_minus1 - 1; i >= 0; i--)
+        general_ptl.ptl_sublayer_level_present_flag[i] = get_bits1(gb);
+
+    while (gb->index % 8 != 0)
+        skip_bits1(gb);
+
+    for (int i = max_sub_layers_minus1 - 1; i >= 0; i--) {
+        if (general_ptl.ptl_sublayer_level_present_flag[i])
+            general_ptl.sublayer_level_idc[i] = get_bits(gb, 8);
+    }
+
+    if (profileTierPresentFlag) {
+        general_ptl.ptl_num_sub_profiles = get_bits(gb, 8);
+        if (general_ptl.ptl_num_sub_profiles) {
+            for (int i = 0; i < general_ptl.ptl_num_sub_profiles; i++)
+                general_ptl.general_sub_profile_idc[i] = get_bits_long(gb, 32);
+        }
+    }
+
+    vvcc_update_ptl(vvcc, &general_ptl);
+}
+
+static int vvcc_parse_vps(GetBitContext *gb,
+                          VVCDecoderConfigurationRecord *vvcc)
+{
+    unsigned int vps_max_layers_minus1;
+    unsigned int vps_max_sublayers_minus1;
+    unsigned int vps_default_ptl_dpb_hrd_max_tid_flag;
+    unsigned int vps_all_independent_layers_flag;
+    unsigned int vps_each_layer_is_an_ols_flag;
+    unsigned int vps_ols_mode_idc;
+
+    unsigned int vps_pt_present_flag[VVC_MAX_PTLS];
+    unsigned int vps_ptl_max_tid[VVC_MAX_PTLS];
+    unsigned int vps_num_ptls_minus1 = 0;
+
+    /*
+     * vps_video_parameter_set_id u(4)
+     */
+    skip_bits(gb, 4);
+
+    vps_max_layers_minus1 = get_bits(gb, 6);
+    vps_max_sublayers_minus1 = get_bits(gb, 3);
+
+    /*
+     * numTemporalLayers greater than 1 indicates that the stream to which this
+     * configuration record applies is temporally scalable and the contained
+     * number of temporal layers (also referred to as temporal sub-layer or
+     * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
+     * indicates that the stream is not temporally scalable. Value 0 indicates
+     * that it is unknown whether the stream is temporally scalable.
+     */
+    vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
+                                vps_max_sublayers_minus1 + 1);
+
+    if (vps_max_layers_minus1 > 0 && vps_max_sublayers_minus1 > 0)
+        vps_default_ptl_dpb_hrd_max_tid_flag = get_bits1(gb);
+    if (vps_max_layers_minus1 > 0)
+        vps_all_independent_layers_flag = get_bits1(gb);
+    else
+        vps_all_independent_layers_flag = 1;
+
+    for (int i = 0; i <= vps_max_layers_minus1; i++) {
+        skip_bits(gb, 6);    //vps_layer_id[i]
+        if (i > 0 && !vps_all_independent_layers_flag) {
+            if (get_bits1(gb)) {    // vps_independent_layer_flag[i]
+                unsigned int vps_max_tid_ref_present_flag = get_bits1(gb);
+                for (int j = 0; j < i; j++) {
+                    if (vps_max_tid_ref_present_flag && get_bits1(gb))  // vps_direct_ref_layer_flag[i][j]
+                        skip_bits(gb, 3);                               // vps_max_tid_il_ref_pics_plus1
+                }
+            }
+        }
+    }
+
+    if (vps_max_layers_minus1 > 0) {
+        if (vps_all_independent_layers_flag)
+            vps_each_layer_is_an_ols_flag = get_bits1(gb);
+        else
+            vps_each_layer_is_an_ols_flag = 0;
+        if (!vps_each_layer_is_an_ols_flag) {
+            if (!vps_all_independent_layers_flag)
+                vps_ols_mode_idc = get_bits(gb, 2);
+            else
+                vps_ols_mode_idc = 2;
+            if (vps_ols_mode_idc == 2) {
+                unsigned int vps_num_output_layer_sets_minus2 = get_bits(gb, 8);
+                for (int i = 1; i <= vps_num_output_layer_sets_minus2 + 1; i++) {
+                    for (int j = 0; j <= vps_max_layers_minus1; j++) {
+                        skip_bits1(gb); // vps_ols_output_layer_flag[i][j]
+                    }
+                }
+            }
+        }
+        vps_num_ptls_minus1 = get_bits(gb, 8);
+    } else {
+        vps_each_layer_is_an_ols_flag = 0;
+    }
+
+    for (int i = 0; i <= vps_num_ptls_minus1; i++) {
+        if (i > 0)
+            vps_pt_present_flag[i] = get_bits1(gb);
+        else
+            vps_pt_present_flag[i] = 1;
+
+        if (!vps_default_ptl_dpb_hrd_max_tid_flag)
+            vps_ptl_max_tid[i] = get_bits(gb, 3);
+        else
+            vps_ptl_max_tid[i] = vps_max_sublayers_minus1;
+    }
+
+    while (gb->index % 8 != 0)
+        skip_bits1(gb);
+
+    for (int i = 0; i <= vps_num_ptls_minus1; i++)
+        vvcc_parse_ptl(gb, vvcc, vps_pt_present_flag[i], vps_ptl_max_tid[i]);
+
+    /* nothing useful for vvcc past this point */
+    return 0;
+}
+
+static int vvcc_parse_sps(GetBitContext *gb,
+                          VVCDecoderConfigurationRecord *vvcc)
+{
+    unsigned int sps_max_sublayers_minus1, sps_log2_ctu_size_minus5;
+    unsigned int sps_subpic_same_size_flag, sps_pic_height_max_in_luma_samples,
+        sps_pic_width_max_in_luma_samples;
+    unsigned int sps_independent_subpics_flag;
+
+    skip_bits(gb, 8);  // sps_seq_parameter_set_id && sps_video_parameter_set_id
+    sps_max_sublayers_minus1 = get_bits(gb, 3);
+
+    /*
+     * numTemporalLayers greater than 1 indicates that the stream to which this
+     * configuration record applies is temporally scalable and the contained
+     * number of temporal layers (also referred to as temporal sub-layer or
+     * sub-layer in ISO/IEC 23008-2) is equal to numTemporalLayers. Value 1
+     * indicates that the stream is not temporally scalable. Value 0 indicates
+     * that it is unknown whether the stream is temporally scalable.
+     */
+    vvcc->num_sublayers = FFMAX(vvcc->num_sublayers,
+                                sps_max_sublayers_minus1 + 1);
+
+    vvcc->chroma_format_idc = get_bits(gb, 2);
+    sps_log2_ctu_size_minus5 = get_bits(gb, 2);
+
+    if (get_bits1(gb))          // sps_ptl_dpb_hrd_params_present_flag
+        vvcc_parse_ptl(gb, vvcc, 1, sps_max_sublayers_minus1);
+
+    skip_bits1(gb);             // sps_gdr_enabled_flag
+    if (get_bits(gb, 1))        // sps_ref_pic_resampling_enabled_flag
+        skip_bits1(gb);         // sps_res_change_in_clvs_allowed_flag
+
+    sps_pic_width_max_in_luma_samples = get_ue_golomb_long(gb);
+    vvcc->max_picture_width =
+        FFMAX(vvcc->max_picture_width, sps_pic_width_max_in_luma_samples);
+    sps_pic_height_max_in_luma_samples = get_ue_golomb_long(gb);
+    vvcc->max_picture_height =
+        FFMAX(vvcc->max_picture_height, sps_pic_height_max_in_luma_samples);
+
+    if (get_bits1(gb)) {
+        get_ue_golomb_long(gb); // sps_conf_win_left_offset
+        get_ue_golomb_long(gb); // sps_conf_win_right_offset
+        get_ue_golomb_long(gb); // sps_conf_win_top_offset
+        get_ue_golomb_long(gb); // sps_conf_win_bottom_offset
+    }
+
+    if (get_bits1(gb)) {        // sps_subpic_info_present_flag
+        const unsigned int sps_num_subpics_minus1 = get_ue_golomb_long(gb);
+        const int ctb_log2_size_y = sps_log2_ctu_size_minus5 + 5;
+        const int ctb_size_y      = 1 << ctb_log2_size_y;
+        const int tmp_width_val   = AV_CEIL_RSHIFT(sps_pic_width_max_in_luma_samples,  ctb_log2_size_y);
+        const int tmp_height_val  = AV_CEIL_RSHIFT(sps_pic_height_max_in_luma_samples, ctb_log2_size_y);
+        const int wlen            = av_ceil_log2(tmp_width_val);
+        const int hlen            = av_ceil_log2(tmp_height_val);
+        if (sps_num_subpics_minus1 > 0) {       // sps_num_subpics_minus1
+            sps_independent_subpics_flag = get_bits1(gb);
+            sps_subpic_same_size_flag = get_bits1(gb);
+        }
+        for (int i = 0; sps_num_subpics_minus1 > 0 && i <= sps_num_subpics_minus1; i++) {
+            if (!sps_subpic_same_size_flag || i == 0) {
+                if (i > 0 && sps_pic_width_max_in_luma_samples > ctb_size_y)
+                    skip_bits(gb, wlen);
+                if (i > 0 && sps_pic_height_max_in_luma_samples > ctb_size_y)
+                    skip_bits(gb, hlen);
+                if (i < sps_num_subpics_minus1 && sps_pic_width_max_in_luma_samples > ctb_size_y)
+                    skip_bits(gb, wlen);
+                if (i < sps_num_subpics_minus1 && sps_pic_height_max_in_luma_samples > ctb_size_y)
+                    skip_bits(gb, hlen);
+            }
+            if (!sps_independent_subpics_flag) {
+                skip_bits(gb, 2);       // sps_subpic_treated_as_pic_flag && sps_loop_filter_across_subpic_enabled_flag
+            }
+        }
+        get_ue_golomb_long(gb); // sps_subpic_id_len_minus1
+        if (get_bits1(gb)) {    // sps_subpic_id_mapping_explicitly_signalled_flag
+            if (get_bits1(gb))  // sps_subpic_id_mapping_present_flag
+                for (int i = 0; i <= sps_num_subpics_minus1; i++) {
+                    skip_bits1(gb);     // sps_subpic_id[i]
+                }
+        }
+    }
+    vvcc->bit_depth_minus8 = get_ue_golomb_long(gb);
+
+    /* nothing useful for vvcc past this point */
+    return 0;
+}
+
+static int vvcc_parse_pps(GetBitContext *gb,
+                          VVCDecoderConfigurationRecord *vvcc)
+{
+
+    // Nothing of importance to parse in PPS
+    /* nothing useful for vvcc past this point */
+    return 0;
+}
+
+static void nal_unit_parse_header(GetBitContext *gb, uint8_t *nal_type)
+{
+    /*
+     * forbidden_zero_bit    u(1)
+     * nuh_reserved_zero_bit u(1)
+     * nuh_layer_id          u(6)
+     */
+    skip_bits(gb, 8);
+    *nal_type = get_bits(gb, 5);
+
+    /*
+     * nuh_temporal_id_plus1 u(3)
+     */
+    skip_bits(gb, 3);
+}
+
+static int vvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
+                                   uint8_t nal_type, int ps_array_completeness,
+                                   VVCDecoderConfigurationRecord *vvcc)
+{
+    int ret;
+    uint8_t index;
+    uint16_t num_nalus;
+    VVCCNALUnitArray *array;
+
+    for (index = 0; index < vvcc->num_of_arrays; index++)
+        if (vvcc->array[index].NAL_unit_type == nal_type)
+            break;
+
+    if (index >= vvcc->num_of_arrays) {
+        uint8_t i;
+
+        ret =
+            av_reallocp_array(&vvcc->array, index + 1,
+                              sizeof(VVCCNALUnitArray));
+        if (ret < 0)
+            return ret;
+
+        for (i = vvcc->num_of_arrays; i <= index; i++)
+            memset(&vvcc->array[i], 0, sizeof(VVCCNALUnitArray));
+        vvcc->num_of_arrays = index + 1;
+    }
+
+    array = &vvcc->array[index];
+    num_nalus = array->num_nalus;
+
+    ret = av_reallocp_array(&array->nal_unit, num_nalus + 1, sizeof(uint8_t *));
+    if (ret < 0)
+        return ret;
+
+    ret =
+        av_reallocp_array(&array->nal_unit_length, num_nalus + 1,
+                          sizeof(uint16_t));
+    if (ret < 0)
+        return ret;
+
+    array->nal_unit[num_nalus] = nal_buf;
+    array->nal_unit_length[num_nalus] = nal_size;
+    array->NAL_unit_type = nal_type;
+    array->num_nalus++;
+
+    /*
+    * When the sample entry name is 'vvc1', the following applies:
+    * • The value of array_completeness shall be equal to 1 for arrays of SPS,
+    *   and PPS NAL units.
+    * • If a VVC bitstream includes DCI NAL unit(s), the value of
+    *   array_completeness shall be equal to 1 for the array of DCI units.
+    *   Otherwise, NAL_unit_type shall not indicate DCI NAL units.
+    * • If a VVC bitstream includes VPS NAL unit(s), the value of
+    *   array_completeness shall be equal to 1 for the array of VPS NAL units.
+    *   Otherwise, NAL_unit_type shall not indicate VPS NAL units.
+    * When the value of array_completeness is equal to 1 for an array of a
+    * particular NAL_unit_type value, NAL units of that NAL_unit_type value
+    * cannot be updated without causing a different sample entry to be used.
+    * When the sample entry name is 'vvi1', the value of array_completeness
+    * of at least one of the following arrays shall be equal to 0:
+      • The array of DCI NAL units, if present.
+      • The array of VPS NAL units, if present.
+      • The array of SPS NAL units
+      • The array of PPS NAL units.
+    */
+    if (nal_type == VVC_VPS_NUT || nal_type == VVC_SPS_NUT ||
+        nal_type == VVC_PPS_NUT || nal_type == VVC_DCI_NUT )
+        array->array_completeness = ps_array_completeness;
+
+    return 0;
+}
+
+static int vvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size,
+                             int ps_array_completeness,
+                             VVCDecoderConfigurationRecord *vvcc)
+{
+    int ret = 0;
+    GetBitContext gbc;
+    uint8_t nal_type;
+    uint8_t *rbsp_buf;
+    uint32_t rbsp_size;
+
+    rbsp_buf = ff_nal_unit_extract_rbsp(nal_buf, nal_size, &rbsp_size, 2);
+    if (!rbsp_buf) {
+        ret = AVERROR(ENOMEM);
+        goto end;
+    }
+
+    ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size);
+    if (ret < 0)
+        goto end;
+
+    nal_unit_parse_header(&gbc, &nal_type);
+
+    /*
+     * Note: only 'declarative' SEI messages are allowed in
+     * vvcc. Perhaps the SEI playload type should be checked
+     * and non-declarative SEI messages discarded?
+     */
+    switch (nal_type) {
+    case VVC_OPI_NUT:
+    case VVC_VPS_NUT:
+    case VVC_SPS_NUT:
+    case VVC_PPS_NUT:
+    case VVC_PREFIX_SEI_NUT:
+    case VVC_SUFFIX_SEI_NUT:
+        ret = vvcc_array_add_nal_unit(nal_buf, nal_size, nal_type,
+                                      ps_array_completeness, vvcc);
+        if (ret < 0)
+            goto end;
+        else if (nal_type == VVC_VPS_NUT)
+            ret = vvcc_parse_vps(&gbc, vvcc);
+        else if (nal_type == VVC_SPS_NUT)
+            ret = vvcc_parse_sps(&gbc, vvcc);
+        else if (nal_type == VVC_PPS_NUT)
+            ret = vvcc_parse_pps(&gbc, vvcc);
+        else if (nal_type == VVC_OPI_NUT) {
+            // not yet supported
+        }
+        if (ret < 0)
+            goto end;
+        break;
+    default:
+        ret = AVERROR_INVALIDDATA;
+        goto end;
+    }
+
+  end:
+    av_free(rbsp_buf);
+    return ret;
+}
+
+static void vvcc_init(VVCDecoderConfigurationRecord *vvcc)
+{
+    memset(vvcc, 0, sizeof(VVCDecoderConfigurationRecord));
+    vvcc->lengthSizeMinusOne = 3;       // 4 bytes
+
+    vvcc->ptl.num_bytes_constraint_info = 1;
+
+    vvcc->ptl_present_flag = 1;
+}
+
+static void vvcc_close(VVCDecoderConfigurationRecord *vvcc)
+{
+    uint8_t i;
+
+    for (i = 0; i < vvcc->num_of_arrays; i++) {
+        vvcc->array[i].num_nalus = 0;
+        av_freep(&vvcc->array[i].nal_unit);
+        av_freep(&vvcc->array[i].nal_unit_length);
+    }
+
+    vvcc->num_of_arrays = 0;
+    av_freep(&vvcc->array);
+}
+
+static int vvcc_write(AVIOContext *pb, VVCDecoderConfigurationRecord *vvcc)
+{
+    uint8_t i;
+    uint16_t j, vps_count = 0, sps_count = 0, pps_count = 0;
+    unsigned char *buf = NULL;
+    /*
+     * It's unclear how to properly compute these fields, so
+     * let's always set them to values meaning 'unspecified'.
+     */
+    vvcc->avg_frame_rate = 0;
+    vvcc->constant_frame_rate = 1;
+
+    av_log(NULL, AV_LOG_TRACE,
+           "lengthSizeMinusOne:                  %" PRIu8 "\n",
+           vvcc->lengthSizeMinusOne);
+    av_log(NULL, AV_LOG_TRACE,
+           "ptl_present_flag:                    %" PRIu8 "\n",
+           vvcc->ptl_present_flag);
+    av_log(NULL, AV_LOG_TRACE,
+           "ols_idx:                             %" PRIu16 "\n", vvcc->ols_idx);
+    av_log(NULL, AV_LOG_TRACE,
+           "num_sublayers:                       %" PRIu8 "\n",
+           vvcc->num_sublayers);
+    av_log(NULL, AV_LOG_TRACE,
+           "constant_frame_rate:                 %" PRIu8 "\n",
+           vvcc->constant_frame_rate);
+    av_log(NULL, AV_LOG_TRACE,
+           "chroma_format_idc:                   %" PRIu8 "\n",
+           vvcc->chroma_format_idc);
+
+    av_log(NULL, AV_LOG_TRACE,
+           "bit_depth_minus8:                    %" PRIu8 "\n",
+           vvcc->bit_depth_minus8);
+    av_log(NULL, AV_LOG_TRACE,
+           "num_bytes_constraint_info:           %" PRIu8 "\n",
+           vvcc->ptl.num_bytes_constraint_info);
+    av_log(NULL, AV_LOG_TRACE,
+           "general_profile_idc:                 %" PRIu8 "\n",
+           vvcc->ptl.general_profile_idc);
+    av_log(NULL, AV_LOG_TRACE,
+           "general_tier_flag:                   %" PRIu8 "\n",
+           vvcc->ptl.general_tier_flag);
+    av_log(NULL, AV_LOG_TRACE,
+           "general_level_idc:                   %" PRIu8 "\n",
+           vvcc->ptl.general_level_idc);
+    av_log(NULL, AV_LOG_TRACE,
+           "ptl_frame_only_constraint_flag:      %" PRIu8 "\n",
+           vvcc->ptl.ptl_frame_only_constraint_flag);
+    av_log(NULL, AV_LOG_TRACE,
+           "ptl_multilayer_enabled_flag:         %" PRIu8 "\n",
+           vvcc->ptl.ptl_multilayer_enabled_flag);
+    for (i = 0; i < vvcc->ptl.num_bytes_constraint_info; i++) {
+        av_log(NULL, AV_LOG_TRACE,
+               "general_constraint_info[%d]:          %" PRIu8 "\n", i,
+               vvcc->ptl.general_constraint_info[i]);
+    }
+
+    for (i = 0; i < vvcc->num_sublayers - 1; i++) {
+        av_log(NULL, AV_LOG_TRACE,
+               "ptl_sublayer_level_present_flag[%" PRIu8 "]:  %" PRIu8 "\n", i,
+               vvcc->ptl.ptl_sublayer_level_present_flag[i]);
+        av_log(NULL, AV_LOG_TRACE,
+               "sublayer_level_idc[%" PRIu8 "]: %" PRIu8 "\n", i,
+               vvcc->ptl.sublayer_level_idc[i]);
+    }
+
+    av_log(NULL, AV_LOG_TRACE,
+           "num_sub_profiles:                    %" PRIu8 "\n",
+           vvcc->ptl.ptl_num_sub_profiles);
+
+    for (i = 0; i < vvcc->ptl.ptl_num_sub_profiles; i++) {
+        av_log(NULL, AV_LOG_TRACE,
+               "general_sub_profile_idc[%" PRIu8 "]:         %" PRIx32 "\n", i,
+               vvcc->ptl.general_sub_profile_idc[i]);
+    }
+
+    av_log(NULL, AV_LOG_TRACE,
+           "max_picture_width:                   %" PRIu16 "\n",
+           vvcc->max_picture_width);
+    av_log(NULL, AV_LOG_TRACE,
+           "max_picture_height:                  %" PRIu16 "\n",
+           vvcc->max_picture_height);
+    av_log(NULL, AV_LOG_TRACE,
+           "avg_frame_rate:                      %" PRIu16 "\n",
+           vvcc->avg_frame_rate);
+
+    av_log(NULL, AV_LOG_TRACE,
+           "num_of_arrays:                       %" PRIu8 "\n",
+           vvcc->num_of_arrays);
+    for (i = 0; i < vvcc->num_of_arrays; i++) {
+        av_log(NULL, AV_LOG_TRACE,
+               "array_completeness[%" PRIu8 "]:               %" PRIu8 "\n", i,
+               vvcc->array[i].array_completeness);
+        av_log(NULL, AV_LOG_TRACE,
+               "NAL_unit_type[%" PRIu8 "]:                    %" PRIu8 "\n", i,
+               vvcc->array[i].NAL_unit_type);
+        av_log(NULL, AV_LOG_TRACE,
+               "num_nalus[%" PRIu8 "]:                        %" PRIu16 "\n", i,
+               vvcc->array[i].num_nalus);
+        for (j = 0; j < vvcc->array[i].num_nalus; j++)
+            av_log(NULL, AV_LOG_TRACE,
+                   "nal_unit_length[%" PRIu8 "][%" PRIu16 "]:               %"
+                   PRIu16 "\n", i, j, vvcc->array[i].nal_unit_length[j]);
+    }
+
+    /*
+     * We need at least one of each: VPS and SPS.
+     */
+    for (i = 0; i < vvcc->num_of_arrays; i++)
+        switch (vvcc->array[i].NAL_unit_type) {
+        case VVC_VPS_NUT:
+            vps_count += vvcc->array[i].num_nalus;
+            break;
+        case VVC_SPS_NUT:
+            sps_count += vvcc->array[i].num_nalus;
+            break;
+        case VVC_PPS_NUT:
+            pps_count += vvcc->array[i].num_nalus;
+            break;
+        default:
+            break;
+        }
+
+    if (vps_count > VVC_MAX_VPS_COUNT)
+        return AVERROR_INVALIDDATA;
+    if (!sps_count || sps_count > VVC_MAX_SPS_COUNT)
+        return AVERROR_INVALIDDATA;
+    if (!pps_count || pps_count > VVC_MAX_PPS_COUNT)
+        return AVERROR_INVALIDDATA;
+
+    /* bit(5) reserved = ‘11111’b;
+       unsigned int (2) LengthSizeMinusOne
+       unsigned int (1) ptl_present_flag */
+    avio_w8(pb, vvcc->lengthSizeMinusOne << 1 | vvcc->ptl_present_flag | 0xf8);
+
+    if (vvcc->ptl_present_flag) {
+        /*
+         * unsigned int(9) ols_idx;
+         * unsigned int(3) num_sublayers;
+         * unsigned int(2) constant_frame_rate;
+         * unsigned int(2) chroma_format_idc;     */
+        avio_wb16(pb,
+                  vvcc->ols_idx << 7 | vvcc->num_sublayers << 4 | vvcc->
+                  constant_frame_rate << 2 | vvcc->chroma_format_idc);
+
+        /* unsigned int(3) bit_depth_minus8;
+           bit(5) reserved = ‘11111’b; */
+        avio_w8(pb, vvcc->bit_depth_minus8 << 5 | 0x1f);
+
+        //VVCPTLRecord
+
+        /* bit(2) reserved = ‘00’b;
+           unsigned int (6) num_bytes_constraint_info */
+        avio_w8(pb, vvcc->ptl.num_bytes_constraint_info & 0x3f);
+
+        /* unsigned int (7) general_profile_idc
+           unsigned int (1) general_tier_flag */
+        avio_w8(pb,
+                vvcc->ptl.general_profile_idc << 1 | vvcc->ptl.general_tier_flag);
+
+        /* unsigned int (8) general_level_idc */
+        avio_w8(pb, vvcc->ptl.general_level_idc);
+
+        /*
+         * unsigned int (1) ptl_frame_only_constraint_flag
+         * unsigned int (1) ptl_multilayer_enabled_flag
+         * unsigned int (8*num_bytes_constraint_info -2) general_constraint_info */
+        buf =
+            (unsigned char *) malloc(sizeof(unsigned char) *
+                                     vvcc->ptl.num_bytes_constraint_info);
+        *buf = vvcc->ptl.ptl_frame_only_constraint_flag << vvcc->ptl.
+            num_bytes_constraint_info * 8 - 1 | vvcc->ptl.
+            ptl_multilayer_enabled_flag << vvcc->ptl.num_bytes_constraint_info *
+            8 - 2 | *vvcc->ptl.general_constraint_info >> 2;
+        avio_write(pb, buf, vvcc->ptl.num_bytes_constraint_info);
+        free(buf);
+
+        if (vvcc->num_sublayers > 1) {
+            uint8_t ptl_sublayer_level_present_flags = 0;
+            for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
+                ptl_sublayer_level_present_flags =
+                    (ptl_sublayer_level_present_flags << 1 | vvcc->ptl.
+                     ptl_sublayer_level_present_flag[i]);
+            }
+            avio_w8(pb, ptl_sublayer_level_present_flags);
+        }
+
+        for (int i = vvcc->num_sublayers - 2; i >= 0; i--) {
+            if (vvcc->ptl.ptl_sublayer_level_present_flag[i])
+                avio_w8(pb, vvcc->ptl.sublayer_level_idc[i]);
+        }
+
+        /* unsigned int(8) num_sub_profiles; */
+        avio_w8(pb, vvcc->ptl.ptl_num_sub_profiles);
+
+        for (int j = 0; j < vvcc->ptl.ptl_num_sub_profiles; j++) {
+            /* unsigned int(32) general_sub_profile_idc[j]; */
+            avio_wb32(pb, vvcc->ptl.general_sub_profile_idc[j]);
+        }
+
+        //End of VvcPTLRecord
+
+        /*
+         * unsigned int(16) max_picture_width;*/
+        avio_wb16(pb, vvcc->max_picture_width);
+
+        /*
+         * unsigned int(16) max_picture_height;*/
+        avio_wb16(pb, vvcc->max_picture_height);
+
+        /*
+         * unsigned int(16) avg_frame_rate; */
+        avio_wb16(pb, vvcc->avg_frame_rate);
+    }
+
+    /* unsigned int(8) num_of_arrays; */
+    avio_w8(pb, vvcc->num_of_arrays);
+
+    for (i = 0; i < vvcc->num_of_arrays; i++) {
+        /*
+         * bit(1) array_completeness;
+         * unsigned int(2) reserved = 0;
+         * unsigned int(5) NAL_unit_type;
+         */
+        avio_w8(pb, vvcc->array[i].array_completeness << 7 |
+                vvcc->array[i].NAL_unit_type & 0x1f);
+        /* unsigned int(16) num_nalus; */
+        if (vvcc->array[i].NAL_unit_type != VVC_DCI_NUT &&
+            vvcc->array[i].NAL_unit_type != VVC_OPI_NUT)
+            avio_wb16(pb, vvcc->array[i].num_nalus);
+        for (j = 0; j < vvcc->array[i].num_nalus; j++) {
+            /* unsigned int(16) nal_unit_length; */
+            avio_wb16(pb, vvcc->array[i].nal_unit_length[j]);
+
+            /* bit(8*nal_unit_length) nal_unit; */
+            avio_write(pb, vvcc->array[i].nal_unit[j],
+                       vvcc->array[i].nal_unit_length[j]);
+        }
+    }
+
+    return 0;
+}
+
+int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
+                      int size, int filter_ps, int *ps_count)
+{
+    int num_ps = 0, ret = 0;
+    uint8_t *buf, *end, *start = NULL;
+
+    if (!filter_ps) {
+        ret = ff_avc_parse_nal_units(pb, buf_in, size);
+        goto end;
+    }
+
+    ret = ff_avc_parse_nal_units_buf(buf_in, &start, &size);
+    if (ret < 0)
+        goto end;
+
+    ret = 0;
+    buf = start;
+    end = start + size;
+
+    while (end - buf > 4) {
+        uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
+        uint8_t type = (buf[5] >> 3);
+
+        buf += 4;
+
+        switch (type) {
+        case VVC_VPS_NUT:
+        case VVC_SPS_NUT:
+        case VVC_PPS_NUT:
+            num_ps++;
+            break;
+        default:
+            ret += 4 + len;
+            avio_wb32(pb, len);
+            avio_write(pb, buf, len);
+            break;
+        }
+
+        buf += len;
+    }
+
+  end:
+    av_free(start);
+    if (ps_count)
+        *ps_count = num_ps;
+    return ret;
+}
+
+int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
+                          int *size, int filter_ps, int *ps_count)
+{
+    AVIOContext *pb;
+    int ret;
+
+    ret = avio_open_dyn_buf(&pb);
+    if (ret < 0)
+        return ret;
+
+    ret = ff_vvc_annexb2mp4(pb, buf_in, *size, filter_ps, ps_count);
+    if (ret < 0) {
+        ffio_free_dyn_buf(&pb);
+        return ret;
+    }
+
+    *size = avio_close_dyn_buf(pb, buf_out);
+
+    return 0;
+}
+
+int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data,
+                       int size, int ps_array_completeness)
+{
+    VVCDecoderConfigurationRecord vvcc;
+    uint8_t *buf, *end, *start;
+    int ret;
+
+    if (size < 6) {
+        /* We can't write a valid vvcc from the provided data */
+        return AVERROR_INVALIDDATA;
+    } else if ((*data & 0xf8) == 0xf8) {
+        /* Data is already vvcc-formatted */
+        avio_write(pb, data, size);
+        return 0;
+    } else if (!(AV_RB24(data) == 1 || AV_RB32(data) == 1)) {
+        /* Not a valid Annex B start code prefix */
+        return AVERROR_INVALIDDATA;
+    }
+
+    ret = ff_avc_parse_nal_units_buf(data, &start, &size);
+    if (ret < 0)
+        return ret;
+
+    vvcc_init(&vvcc);
+
+    buf = start;
+    end = start + size;
+
+    while (end - buf > 4) {
+        uint32_t len = FFMIN(AV_RB32(buf), end - buf - 4);
+        uint8_t type = (buf[5] >> 3);
+
+        buf += 4;
+
+        switch (type) {
+        case VVC_OPI_NUT:
+        case VVC_VPS_NUT:
+        case VVC_SPS_NUT:
+        case VVC_PPS_NUT:
+        case VVC_PREFIX_SEI_NUT:
+        case VVC_SUFFIX_SEI_NUT:
+            ret = vvcc_add_nal_unit(buf, len, ps_array_completeness, &vvcc);
+            if (ret < 0)
+                goto end;
+            break;
+        default:
+            break;
+        }
+
+        buf += len;
+    }
+
+    ret = vvcc_write(pb, &vvcc);
+
+  end:
+    vvcc_close(&vvcc);
+    av_free(start);
+    return ret;
+}
diff --git a/libavformat/vvc.h b/libavformat/vvc.h
new file mode 100644
index 0000000000..11c700540f
--- /dev/null
+++ b/libavformat/vvc.h
@@ -0,0 +1,99 @@
+/*
+ * H.266 / VVC helper functions for muxers
+ *
+ * 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
+ */
+
+/**
+ * @file
+ * internal header for H.266/VVC (de)muxer utilities
+ */
+
+#ifndef AVFORMAT_VVC_H
+#define AVFORMAT_VVC_H
+
+#include <stdint.h>
+#include "avio.h"
+
+/**
+ * Writes Annex B formatted H.266/VVC NAL units to the provided AVIOContext.
+ *
+ * The NAL units are converted to an MP4-compatible format (start code prefixes
+ * are replaced by 4-byte size fields, as per ISO/IEC 14496-15).
+ *
+ * If filter_ps is non-zero, any VVC parameter sets found in the input will be
+ * discarded, and *ps_count will be set to the number of discarded PS NAL units.
+ *
+ * @param pb address of the AVIOContext where the data shall be written
+ * @param buf_in address of the buffer holding the input data
+ * @param size size (in bytes) of the input buffer
+ * @param filter_ps whether to write parameter set NAL units to the output (0)
+ *        or to discard them (non-zero)
+ * @param ps_count address of the variable where the number of discarded
+ *        parameter set NAL units shall be written, may be NULL
+ * @return the amount (in bytes) of data written in case of success, a negative
+ *         value corresponding to an AVERROR code in case of failure
+ */
+int ff_vvc_annexb2mp4(AVIOContext *pb, const uint8_t *buf_in,
+                      int size, int filter_ps, int *ps_count);
+
+/**
+ * Writes Annex B formatted H.266/VVC NAL units to a data buffer.
+ *
+ * The NAL units are converted to an MP4-compatible format (start code prefixes
+ * are replaced by 4-byte size fields, as per ISO/IEC 14496-15).
+ *
+ * If filter_ps is non-zero, any VVC parameter sets found in the input will be
+ * discarded, and *ps_count will be set to the number of discarded PS NAL units.
+ *
+ * On success, *size holds the size (in bytes) of the output data buffer.
+ *
+ * @param buf_in address of the buffer holding the input data
+ * @param size address of the variable holding the size (in bytes) of the input
+ *        buffer (on input) and of the output buffer (on success)
+ * @param buf_out on success, address of the variable holding the address of
+ *        the output buffer
+ * @param filter_ps whether to write parameter set NAL units to the output (0)
+ *        or to discard them (non-zero)
+ * @param ps_count address of the variable where the number of discarded
+ *        parameter set NAL units shall be written, may be NULL
+ * @return 0 in case of success, a negative value corresponding to an AVERROR
+ *         code in case of failure
+ * @note *buf_out will be treated as uninitialized on input and won't be freed.
+ */
+int ff_vvc_annexb2mp4_buf(const uint8_t *buf_in, uint8_t **buf_out,
+                          int *size, int filter_ps, int *ps_count);
+
+/**
+ * Writes H.266/VVC extradata (parameter sets, declarative SEI NAL units) to
+ * the provided AVIOContext.
+ *
+ * If the extradata is Annex B format, it gets converted to vvcC format before
+ * writing.
+ *
+ * @param pb address of the AVIOContext where the vvcC shall be written
+ * @param data address of the buffer holding the data needed to write the vvcC
+ * @param size size (in bytes) of the data buffer
+ * @param ps_array_completeness whether all parameter sets are in the vvcC (1)
+ *        or there may be additional parameter sets in the bitstream (0)
+ * @return >=0 in case of success, a negative value corresponding to an AVERROR
+ *         code in case of failure
+ */
+int ff_isom_write_vvcc(AVIOContext *pb, const uint8_t *data,
+                       int size, int ps_array_completeness);
+
+#endif /* AVFORMAT_VVC_H */
-- 
2.25.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH v4 2/6] avformat/mpegtsenc: refact mpegts_check_bitstream to loop up table
       [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 1/6] avformat/mp4: add muxer support for H266/VVC Nuo Mi
@ 2024-01-29 14:54 ` Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 3/6] avformat/mpegtsenc: refact, move h264, hevc startcode checking to check_h26x_startcode Nuo Mi
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: Nuo Mi @ 2024-01-29 14:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Nuo Mi

---
 libavformat/mpegtsenc.c | 33 ++++++++++++++++++---------------
 1 file changed, 18 insertions(+), 15 deletions(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 84edd418f0..4e5c264d2a 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -2257,23 +2257,26 @@ static void mpegts_deinit(AVFormatContext *s)
 static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st,
                                   const AVPacket *pkt)
 {
-    int ret = 1;
+    const struct Entry {
+        enum AVCodecID id;
+        const char *bsf_name;
+        uint8_t mask;
+        uint8_t value;
+    } list[] = {
+        { AV_CODEC_ID_H264, "h264_mp4toannexb", 0xff, 0x01 /* configurationVersion in AVCDecoderConfigurationRecord  */},
+        { AV_CODEC_ID_HEVC, "hevc_mp4toannexb", 0xff, 0x01 /* configurationVersion in HEVCDecoderConfigurationRecord */},
+    };
 
-    if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
-        if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
-                             (AV_RB24(pkt->data) != 0x000001 ||
-                              (st->codecpar->extradata_size > 0 &&
-                               st->codecpar->extradata[0] == 1)))
-            ret = ff_stream_add_bitstream_filter(st, "h264_mp4toannexb", NULL);
-    } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
-        if (pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
-                             (AV_RB24(pkt->data) != 0x000001 ||
-                              (st->codecpar->extradata_size > 0 &&
-                               st->codecpar->extradata[0] == 1)))
-            ret = ff_stream_add_bitstream_filter(st, "hevc_mp4toannexb", NULL);
+    for (int i = 0; i < FF_ARRAY_ELEMS(list); i++) {
+        const struct Entry *e = list + i;
+        if (e->id == st->codecpar->codec_id &&
+                pkt->size >= 5 && AV_RB32(pkt->data) != 0x0000001 &&
+                (AV_RB24(pkt->data) != 0x000001 ||
+                    (st->codecpar->extradata_size > 0 &&
+                        (st->codecpar->extradata[0] & e->mask == e->value))))
+            return ff_stream_add_bitstream_filter(st, e->bsf_name, NULL);
     }
-
-    return ret;
+    return 1;
 }
 
 #define OFFSET(x) offsetof(MpegTSWrite, x)
-- 
2.25.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH v4 3/6] avformat/mpegtsenc: refact, move h264, hevc startcode checking to check_h26x_startcode
       [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 1/6] avformat/mp4: add muxer support for H266/VVC Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 2/6] avformat/mpegtsenc: refact mpegts_check_bitstream to loop up table Nuo Mi
@ 2024-01-29 14:54 ` Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 4/6] avformat/mpegtsenc: refact, remove h264, hevc magic numbers for nal_type Nuo Mi
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 6+ messages in thread
From: Nuo Mi @ 2024-01-29 14:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Nuo Mi

---
 libavformat/mpegtsenc.c | 26 ++++++++------------------
 1 file changed, 8 insertions(+), 18 deletions(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 4e5c264d2a..5e089f2866 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -1759,16 +1759,16 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
     ts_st->prev_payload_key = key;
 }
 
-int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
+static int check_h26x_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt, const char *codec)
 {
     if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
         if (!st->nb_frames) {
-            av_log(s, AV_LOG_ERROR, "H.264 bitstream malformed, "
-                   "no startcode found, use the video bitstream filter 'h264_mp4toannexb' to fix it "
-                   "('-bsf:v h264_mp4toannexb' option with ffmpeg)\n");
+            av_log(s, AV_LOG_ERROR, "%s bitstream malformed, "
+                   "no startcode found, use the video bitstream filter '%s_mp4toannexb' to fix it "
+                   "('-bsf:v %s_mp4toannexb' option with ffmpeg)\n", codec, codec, codec);
             return AVERROR_INVALIDDATA;
         }
-        av_log(s, AV_LOG_WARNING, "H.264 bitstream error, startcode missing, size %d", pkt->size);
+        av_log(s, AV_LOG_WARNING, "%s bitstream error, startcode missing, size %d", codec, pkt->size);
         if (pkt->size)
             av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
         av_log(s, AV_LOG_WARNING, "\n");
@@ -1776,19 +1776,9 @@ int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPack
     return 0;
 }
 
-static int check_hevc_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
+int ff_check_h264_startcode(AVFormatContext *s, const AVStream *st, const AVPacket *pkt)
 {
-    if (pkt->size < 5 || AV_RB32(pkt->data) != 0x0000001 && AV_RB24(pkt->data) != 0x000001) {
-        if (!st->nb_frames) {
-            av_log(s, AV_LOG_ERROR, "HEVC bitstream malformed, no startcode found\n");
-            return AVERROR_PATCHWELCOME;
-        }
-        av_log(s, AV_LOG_WARNING, "HEVC bitstream error, startcode missing, size %d", pkt->size);
-        if (pkt->size)
-            av_log(s, AV_LOG_WARNING, " data %08"PRIX32, AV_RB32(pkt->data));
-        av_log(s, AV_LOG_WARNING, "\n");
-    }
-    return 0;
+    return check_h26x_startcode(s, st, pkt, "h264");
 }
 
 /* Based on GStreamer's gst-plugins-base/ext/ogg/gstoggstream.c
@@ -1985,7 +1975,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
         const uint8_t *p = buf, *buf_end = p + size;
         uint32_t state = -1;
         int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
-        int ret = check_hevc_startcode(s, st, pkt);
+        int ret = check_h26x_startcode(s, st, pkt, "hevc");
         if (ret < 0)
             return ret;
 
-- 
2.25.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH v4 4/6] avformat/mpegtsenc: refact, remove h264, hevc magic numbers for nal_type
       [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
                   ` (2 preceding siblings ...)
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 3/6] avformat/mpegtsenc: refact, move h264, hevc startcode checking to check_h26x_startcode Nuo Mi
@ 2024-01-29 14:54 ` Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 5/6] avformat/mpegtsenc: refact out h26x_prefix_aud Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 6/6] avformat: add ts stream types for H266/VVC Nuo Mi
  5 siblings, 0 replies; 6+ messages in thread
From: Nuo Mi @ 2024-01-29 14:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Nuo Mi

---
 libavformat/mpegtsenc.c | 32 +++++++++++++++++++-------------
 1 file changed, 19 insertions(+), 13 deletions(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 5e089f2866..3872be0f46 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -31,6 +31,7 @@
 #include "libavcodec/bytestream.h"
 #include "libavcodec/defs.h"
 #include "libavcodec/h264.h"
+#include "libavcodec/hevc.h"
 #include "libavcodec/startcode.h"
 
 #include "avformat.h"
@@ -1833,6 +1834,8 @@ static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
     return duration;
 }
 
+#define H264_NAL_TYPE(state) (state & 0x1f)
+#define HEVC_NAL_TYPE(state) ((state & 0x7e) >> 1)
 static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 {
     AVStream *st = s->streams[pkt->stream_index];
@@ -1876,6 +1879,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
     if (st->codecpar->codec_id == AV_CODEC_ID_H264) {
         const uint8_t *p = buf, *buf_end = p + size;
         const uint8_t *found_aud = NULL, *found_aud_end = NULL;
+        int nal_type;
         uint32_t state = -1;
         int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
         int ret = ff_check_h264_startcode(s, st, pkt);
@@ -1890,10 +1894,11 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
          * are assumed to be available in 'extradata' if not found in-band. */
         do {
             p = avpriv_find_start_code(p, buf_end, &state);
-            av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", state & 0x1f);
-            if ((state & 0x1f) == H264_NAL_SPS)
+            nal_type = H264_NAL_TYPE(state);
+            av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", nal_type);
+            if (nal_type == H264_NAL_SPS)
                 extradd = 0;
-            if ((state & 0x1f) == H264_NAL_AUD) {
+            if (nal_type == H264_NAL_AUD) {
                 found_aud = p - 4;     // start of the 0x000001 start code.
                 found_aud_end = p + 1; // first byte past the AUD.
                 if (found_aud < buf)
@@ -1902,10 +1907,10 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
                     found_aud_end = buf_end;
             }
         } while (p < buf_end
-                 && (state & 0x1f) != H264_NAL_IDR_SLICE
-                 && (state & 0x1f) != H264_NAL_SLICE
+                 && nal_type != H264_NAL_IDR_SLICE
+                 && nal_type != H264_NAL_SLICE
                  && (extradd > 0 || !found_aud));
-        if ((state & 0x1f) != H264_NAL_IDR_SLICE)
+        if (nal_type != H264_NAL_IDR_SLICE)
             extradd = 0;
 
         if (!found_aud) {
@@ -1974,6 +1979,7 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
     } else if (st->codecpar->codec_id == AV_CODEC_ID_HEVC) {
         const uint8_t *p = buf, *buf_end = p + size;
         uint32_t state = -1;
+        int nal_type;
         int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
         int ret = check_h26x_startcode(s, st, pkt, "hevc");
         if (ret < 0)
@@ -1984,22 +1990,22 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 
         do {
             p = avpriv_find_start_code(p, buf_end, &state);
-            av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", (state & 0x7e)>>1);
-            if ((state & 0x7e) == 2*32)
+            nal_type = HEVC_NAL_TYPE(state);
+            av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", nal_type);
+            if (nal_type == HEVC_NAL_VPS)
                 extradd = 0;
-        } while (p < buf_end && (state & 0x7e) != 2*35 &&
-                 (state & 0x7e) >= 2*32);
+        } while (p < buf_end && nal_type != HEVC_NAL_AUD && nal_type >= HEVC_NAL_VPS);
 
-        if ((state & 0x7e) < 2*16 || (state & 0x7e) >= 2*24)
+        if (nal_type < HEVC_NAL_BLA_W_LP || nal_type >= HEVC_NAL_RSV_VCL24)
             extradd = 0;
-        if ((state & 0x7e) != 2*35) { // AUD NAL
+        if (nal_type != HEVC_NAL_AUD) { // AUD NAL
             data = av_malloc(pkt->size + 7 + extradd);
             if (!data)
                 return AVERROR(ENOMEM);
             memcpy(data + 7, st->codecpar->extradata, extradd);
             memcpy(data + 7 + extradd, pkt->data, pkt->size);
             AV_WB32(data, 0x00000001);
-            data[4] = 2*35;
+            data[4] = (HEVC_NAL_AUD << 1);
             data[5] = 1;
             data[6] = 0x50; // any slice type (0x4) + rbsp stop one bit
             buf     = data;
-- 
2.25.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH v4 5/6] avformat/mpegtsenc: refact out h26x_prefix_aud
       [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
                   ` (3 preceding siblings ...)
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 4/6] avformat/mpegtsenc: refact, remove h264, hevc magic numbers for nal_type Nuo Mi
@ 2024-01-29 14:54 ` Nuo Mi
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 6/6] avformat: add ts stream types for H266/VVC Nuo Mi
  5 siblings, 0 replies; 6+ messages in thread
From: Nuo Mi @ 2024-01-29 14:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Nuo Mi

---
 libavformat/mpegtsenc.c | 45 +++++++++++++++++++++++++----------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 3872be0f46..7bc3feaef1 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -1834,6 +1834,21 @@ static int opus_get_packet_samples(AVFormatContext *s, AVPacket *pkt)
     return duration;
 }
 
+static uint8_t *h26x_prefix_aud(const uint8_t *aud, const int aud_size,
+    const uint8_t *extra_data, const int extra_size, AVPacket *pkt, int *size)
+{
+    const int sz = 4;       //start code size
+    uint8_t *data = av_malloc(pkt->size + sz + aud_size + extra_size);
+    if (!data)
+        return NULL;
+    AV_WB32(data, 0x00000001);
+    memcpy(data + sz, aud, aud_size);
+    memcpy(data + sz + aud_size, extra_data, extra_size);
+    memcpy(data + sz + aud_size + extra_size, pkt->data, pkt->size);
+    *size = pkt->size + sz + aud_size + extra_size;
+    return data;
+}
+
 #define H264_NAL_TYPE(state) (state & 0x1f)
 #define HEVC_NAL_TYPE(state) ((state & 0x7e) >> 1)
 static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
@@ -1915,16 +1930,14 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 
         if (!found_aud) {
             /* Prefix 'buf' with the missing AUD, and extradata if needed. */
-            data = av_malloc(pkt->size + 6 + extradd);
+            const uint8_t aud[] = {
+                H264_NAL_AUD,
+                0xf0, // any slice type (0xe) + rbsp stop one bit
+            };
+            buf = data = h26x_prefix_aud(aud, FF_ARRAY_ELEMS(aud),
+                st->codecpar->extradata, extradd, pkt, &size);
             if (!data)
                 return AVERROR(ENOMEM);
-            memcpy(data + 6, st->codecpar->extradata, extradd);
-            memcpy(data + 6 + extradd, pkt->data, pkt->size);
-            AV_WB32(data, 0x00000001);
-            data[4] = H264_NAL_AUD;
-            data[5] = 0xf0; // any slice type (0xe) + rbsp stop one bit
-            buf     = data;
-            size    = pkt->size + 6 + extradd;
         } else if (extradd != 0) {
             /* Move the AUD up to the beginning of the frame, where the H.264
              * spec requires it to appear. Emit the extradata after it. */
@@ -1999,17 +2012,15 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
         if (nal_type < HEVC_NAL_BLA_W_LP || nal_type >= HEVC_NAL_RSV_VCL24)
             extradd = 0;
         if (nal_type != HEVC_NAL_AUD) { // AUD NAL
-            data = av_malloc(pkt->size + 7 + extradd);
+            const uint8_t aud[] = {
+                (HEVC_NAL_AUD << 1),
+                0x01,
+                0x50, // any slice type (0x4) + rbsp stop one bit
+            };
+            buf = data = h26x_prefix_aud(aud, FF_ARRAY_ELEMS(aud),
+                st->codecpar->extradata, extradd, pkt, &size);
             if (!data)
                 return AVERROR(ENOMEM);
-            memcpy(data + 7, st->codecpar->extradata, extradd);
-            memcpy(data + 7 + extradd, pkt->data, pkt->size);
-            AV_WB32(data, 0x00000001);
-            data[4] = (HEVC_NAL_AUD << 1);
-            data[5] = 1;
-            data[6] = 0x50; // any slice type (0x4) + rbsp stop one bit
-            buf     = data;
-            size    = pkt->size + 7 + extradd;
         }
     } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
         if (pkt->size < 2) {
-- 
2.25.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] 6+ messages in thread

* [FFmpeg-devel] [PATCH v4 6/6] avformat: add ts stream types for H266/VVC
       [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
                   ` (4 preceding siblings ...)
  2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 5/6] avformat/mpegtsenc: refact out h26x_prefix_aud Nuo Mi
@ 2024-01-29 14:54 ` Nuo Mi
  5 siblings, 0 replies; 6+ messages in thread
From: Nuo Mi @ 2024-01-29 14:54 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: Nuo Mi, Thomas Siedel

From: Thomas Siedel <thomas.ff@spin-digital.com>

Add transport stream stream type 0x33 for vvc.
Add STREAM_TYPE_VIDEO_VVC to MPEG-1/2 and MPEG-2 transport stream.
Add basic transport stream support for TS mux/demux.

Tested with:
ffmpeg -i NovosobornayaSquare_1920x1080.mp4 -c:v libvvenc test.ts && ffmpeg -i test.ts -f null -
ffmpeg -i NovosobornayaSquare_1920x1080.mp4 -c:v copy test.ts     && ffmpeg -i test.ts -f md5 -

Signed-off-by: Thomas Siedel <thomas.ff@spin-digital.com>
Co-Authored-By: Nuo Mi <nuomi2021@gmail.com>
---
 configure               |  2 +-
 libavformat/mpeg.c      |  3 +++
 libavformat/mpeg.h      |  1 +
 libavformat/mpegts.c    |  2 ++
 libavformat/mpegts.h    |  1 +
 libavformat/mpegtsenc.c | 39 +++++++++++++++++++++++++++++++++++++++
 6 files changed, 47 insertions(+), 1 deletion(-)

diff --git a/configure b/configure
index 21663000f8..68f675a4bc 100755
--- a/configure
+++ b/configure
@@ -3552,7 +3552,7 @@ mp3_demuxer_select="mpegaudio_parser"
 mp3_muxer_select="mpegaudioheader"
 mp4_muxer_select="mov_muxer"
 mpegts_demuxer_select="iso_media"
-mpegts_muxer_select="ac3_parser adts_muxer latm_muxer h264_mp4toannexb_bsf hevc_mp4toannexb_bsf"
+mpegts_muxer_select="ac3_parser adts_muxer latm_muxer h264_mp4toannexb_bsf hevc_mp4toannexb_bsf vvc_mp4toannexb_bsf"
 mpegtsraw_demuxer_select="mpegts_demuxer"
 mxf_muxer_select="pcm_rechunk_bsf rangecoder"
 mxf_d10_muxer_select="mxf_muxer"
diff --git a/libavformat/mpeg.c b/libavformat/mpeg.c
index 781c3162d6..a0f2c6da05 100644
--- a/libavformat/mpeg.c
+++ b/libavformat/mpeg.c
@@ -546,6 +546,9 @@ redo:
     } else if (es_type == STREAM_TYPE_VIDEO_HEVC) {
         codec_id = AV_CODEC_ID_HEVC;
         type     = AVMEDIA_TYPE_VIDEO;
+    } else if (es_type == STREAM_TYPE_VIDEO_VVC) {
+        codec_id = AV_CODEC_ID_VVC;
+        type     = AVMEDIA_TYPE_VIDEO;
     } else if (es_type == STREAM_TYPE_AUDIO_AC3) {
         codec_id = AV_CODEC_ID_AC3;
         type     = AVMEDIA_TYPE_AUDIO;
diff --git a/libavformat/mpeg.h b/libavformat/mpeg.h
index b635295776..20592eb184 100644
--- a/libavformat/mpeg.h
+++ b/libavformat/mpeg.h
@@ -56,6 +56,7 @@
 #define STREAM_TYPE_VIDEO_MPEG4     0x10
 #define STREAM_TYPE_VIDEO_H264      0x1b
 #define STREAM_TYPE_VIDEO_HEVC      0x24
+#define STREAM_TYPE_VIDEO_VVC       0x33
 #define STREAM_TYPE_VIDEO_CAVS      0x42
 
 #define STREAM_TYPE_AUDIO_AC3       0x81
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index c7fd1f5d1f..bef00c88e7 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -812,6 +812,7 @@ static const StreamType ISO_types[] = {
     { 0x20, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_H264       },
     { 0x21, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_JPEG2000   },
     { 0x24, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC       },
+    { 0x33, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VVC        },
     { 0x42, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_CAVS       },
     { 0xd1, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_DIRAC      },
     { 0xd2, AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_AVS2       },
@@ -867,6 +868,7 @@ static const StreamType REGD_types[] = {
     { MKTAG('D', 'T', 'S', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_DTS   },
     { MKTAG('E', 'A', 'C', '3'), AVMEDIA_TYPE_AUDIO, AV_CODEC_ID_EAC3  },
     { MKTAG('H', 'E', 'V', 'C'), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_HEVC  },
+    { MKTAG('V', 'V', 'C', ' '), AVMEDIA_TYPE_VIDEO, AV_CODEC_ID_VVC   },
     { MKTAG('K', 'L', 'V', 'A'), AVMEDIA_TYPE_DATA,  AV_CODEC_ID_SMPTE_KLV },
     { MKTAG('V', 'A', 'N', 'C'), AVMEDIA_TYPE_DATA,  AV_CODEC_ID_SMPTE_2038 },
     { MKTAG('I', 'D', '3', ' '), AVMEDIA_TYPE_DATA,  AV_CODEC_ID_TIMED_ID3 },
diff --git a/libavformat/mpegts.h b/libavformat/mpegts.h
index a48f14e768..14ae312c50 100644
--- a/libavformat/mpegts.h
+++ b/libavformat/mpegts.h
@@ -128,6 +128,7 @@
 #define STREAM_TYPE_METADATA        0x15
 #define STREAM_TYPE_VIDEO_H264      0x1b
 #define STREAM_TYPE_VIDEO_HEVC      0x24
+#define STREAM_TYPE_VIDEO_VVC       0x33
 #define STREAM_TYPE_VIDEO_CAVS      0x42
 #define STREAM_TYPE_VIDEO_AVS2      0xd2
 #define STREAM_TYPE_VIDEO_AVS3      0xd4
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 7bc3feaef1..db3f5b64e6 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -32,6 +32,7 @@
 #include "libavcodec/defs.h"
 #include "libavcodec/h264.h"
 #include "libavcodec/hevc.h"
+#include "libavcodec/vvc.h"
 #include "libavcodec/startcode.h"
 
 #include "avformat.h"
@@ -369,6 +370,9 @@ static int get_dvb_stream_type(AVFormatContext *s, AVStream *st)
     case AV_CODEC_ID_HEVC:
         stream_type = STREAM_TYPE_VIDEO_HEVC;
         break;
+    case AV_CODEC_ID_VVC:
+        stream_type = STREAM_TYPE_VIDEO_VVC;
+        break;
     case AV_CODEC_ID_CAVS:
         stream_type = STREAM_TYPE_VIDEO_CAVS;
         break;
@@ -1851,6 +1855,7 @@ static uint8_t *h26x_prefix_aud(const uint8_t *aud, const int aud_size,
 
 #define H264_NAL_TYPE(state) (state & 0x1f)
 #define HEVC_NAL_TYPE(state) ((state & 0x7e) >> 1)
+#define VVC_NAL_TYPE(state)  ((state >> 11) & 0x1f)
 static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
 {
     AVStream *st = s->streams[pkt->stream_index];
@@ -2022,6 +2027,39 @@ static int mpegts_write_packet_internal(AVFormatContext *s, AVPacket *pkt)
             if (!data)
                 return AVERROR(ENOMEM);
         }
+    } else if (st->codecpar->codec_id == AV_CODEC_ID_VVC) {
+      const uint8_t *p = buf, *buf_end = p + size;
+      uint32_t state = -1;
+      uint32_t nal_type = -1;
+      int extradd = (pkt->flags & AV_PKT_FLAG_KEY) ? st->codecpar->extradata_size : 0;
+      int ret = check_h26x_startcode(s, st, pkt, "vvc");
+      if (ret < 0)
+          return ret;
+
+      if (extradd && AV_RB24(st->codecpar->extradata) > 1)
+          extradd = 0;
+
+      do {
+          p = avpriv_find_start_code(p, buf_end, &state);
+          // state contains byte behind start code, p points 2 bytes behind start code
+          nal_type = VVC_NAL_TYPE(state);
+          av_log(s, AV_LOG_TRACE, "nal %"PRId32"\n", nal_type );
+          if (nal_type == VVC_VPS_NUT)
+              extradd = 0;
+      } while (p < buf_end && nal_type != VVC_AUD_NUT && nal_type >= VVC_OPI_NUT);
+
+      if (nal_type >= VVC_OPI_NUT)
+          extradd = 0;
+      if (nal_type != VVC_AUD_NUT) { // AUD NAL
+            const uint8_t aud[] = {
+                0,                                          // forbidden_zero_bit, nuh_reserved_zero_bit, nuh_layer_id
+                (VVC_AUD_NUT << 3) | 1,                     // nal_unit_type, nuh_temporal_id_plus1(1)
+                (pkt->flags & AV_PKT_FLAG_KEY) << 7 | 0x28, // aud_irap_or_gdr_flag, aud_pic_type(2), rbsp_stop_one_bit
+            };
+            buf = data = h26x_prefix_aud(aud, FF_ARRAY_ELEMS(aud), st->codecpar->extradata, extradd, pkt, &size);
+            if (!data)
+                return AVERROR(ENOMEM);
+      }
     } else if (st->codecpar->codec_id == AV_CODEC_ID_OPUS) {
         if (pkt->size < 2) {
             av_log(s, AV_LOG_ERROR, "Opus packet too short\n");
@@ -2272,6 +2310,7 @@ static int mpegts_check_bitstream(AVFormatContext *s, AVStream *st,
     } list[] = {
         { AV_CODEC_ID_H264, "h264_mp4toannexb", 0xff, 0x01 /* configurationVersion in AVCDecoderConfigurationRecord  */},
         { AV_CODEC_ID_HEVC, "hevc_mp4toannexb", 0xff, 0x01 /* configurationVersion in HEVCDecoderConfigurationRecord */},
+        { AV_CODEC_ID_VVC,  "vvc_mp4toannexb",  0xf8, 0xf8 /* reserved '11111'b    in VVCDecoderConfigurationRecord  */},
     };
 
     for (int i = 0; i < FF_ARRAY_ELEMS(list); i++) {
-- 
2.25.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] 6+ messages in thread

end of thread, other threads:[~2024-01-29 14:56 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <20240129145432.9053-1-nuomi2021@gmail.com>
2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 1/6] avformat/mp4: add muxer support for H266/VVC Nuo Mi
2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 2/6] avformat/mpegtsenc: refact mpegts_check_bitstream to loop up table Nuo Mi
2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 3/6] avformat/mpegtsenc: refact, move h264, hevc startcode checking to check_h26x_startcode Nuo Mi
2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 4/6] avformat/mpegtsenc: refact, remove h264, hevc magic numbers for nal_type Nuo Mi
2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 5/6] avformat/mpegtsenc: refact out h26x_prefix_aud Nuo Mi
2024-01-29 14:54 ` [FFmpeg-devel] [PATCH v4 6/6] avformat: add ts stream types for H266/VVC Nuo Mi

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