PR #26067 opened by JuliusBairaktaris URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26067 Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/26067.patch
`hevc_mp4toannexb` inserts the extradata's VPS, SPS and PPS before every IRAP picture that does not carry them. When a stream replaces a parameter set in-band, once, and does not repeat it at the following IRAP pictures, the superseded set from the extradata is inserted again and the decoder activates it. A three-access-unit Matroska stream (832x480 IDR with VPS/SPS/PPS, a 416x240 IDR with replacement sets using the same ids, then a 416x240 IDR without sets) decodes to two pictures through the filter and three without it (reproduced by Forgejo_Fairy in #25646). The filter now replaces the extradata's set of the same type and id with each one it sees in a packet, and appends sets with new ids, so later insertions use the latest ones. A new case in `api-hevc-mp4toannexb-test` sends a replacement PPS 0 and then a packet without sets, and checks that the second packet carries the replacement. `fate-api-hevc-mp4toannexb` passes. Needed by #25646, which makes the filter mandatory for `hevc_amf`. Written with Claude Sonnet 5.5 in Claude Code; `Assisted-by` trailer on the commit. >From d3b25586df7797b0593d2a71a1ead62de445808f Mon Sep 17 00:00:00 2001 From: Julius Bairaktaris <[email protected]> Date: Sat, 14 Nov 2026 18:59:02 +0100 Subject: [PATCH] avcodec/bsf/hevc_mp4toannexb: keep in-band parameter sets current The filter inserts the VPS, SPS and PPS of the extradata before every IRAP picture that does not carry them itself. A stream that replaces a parameter set in-band, once, and does not repeat it at the following IRAP pictures, therefore got the superseded set from the extradata inserted again, and a decoder activating it at the next IRAP could not decode the picture: with a different resolution the slice no longer matched the active SPS. Replace the extradata's parameter set with the same type and id by each one found in a packet, and append it when the id is new, so that later insertions use the latest sets. The ids are read from the NAL units: the VPS id, the SPS id after the profile_tier_level structure and the PPS id. The packet that carries the new set is not affected. A new API test case sends a packet with a replacement PPS 0, then one without parameter sets, and checks that the second gets the replacement. Found by Forgejo_Fairy in the review of #25646. Assisted-by: Claude Sonnet 5.5 Signed-off-by: Julius Bairaktaris <[email protected]> --- libavcodec/bsf/hevc_mp4toannexb.c | 186 +++++++++++++++++++++++++- tests/api/api-hevc-mp4toannexb-test.c | 68 ++++++++++ tests/ref/fate/api-hevc-mp4toannexb | 3 + 3 files changed, 256 insertions(+), 1 deletion(-) diff --git a/libavcodec/bsf/hevc_mp4toannexb.c b/libavcodec/bsf/hevc_mp4toannexb.c index 345a7ebbc08..d263aeeec9e 100644 --- a/libavcodec/bsf/hevc_mp4toannexb.c +++ b/libavcodec/bsf/hevc_mp4toannexb.c @@ -30,6 +30,8 @@ #include "libavcodec/bsf_internal.h" #include "libavcodec/bytestream.h" #include "libavcodec/defs.h" +#include "libavcodec/get_bits.h" +#include "libavcodec/golomb.h" #include "libavcodec/hevc/hevc.h" @@ -56,6 +58,7 @@ typedef struct HEVCNALUnit { size_t offset; size_t size; int type; + int id; uint32_t hash; int matched; int emit; @@ -77,6 +80,53 @@ typedef struct HEVCBSFContext { int sps_seen; } HEVCBSFContext; +static int hevc_ps_id(const uint8_t *nal, int size, int type) +{ + uint8_t rbsp[160 + AV_INPUT_BUFFER_PADDING_SIZE] = { 0 }; + GetBitContext gb; + int n = 0, zeros = 0, max_sub_layers, profile_present = 0, level_present = 0; + + if (type < HEVC_NAL_VPS || type > HEVC_NAL_PPS || size <= 2) + return -1; + + for (int i = 2; i < size && n < 160; i++) { + if (zeros >= 2 && nal[i] == 3) { + zeros = 0; + continue; + } + zeros = nal[i] ? 0 : zeros + 1; + rbsp[n++] = nal[i]; + } + if (init_get_bits8(&gb, rbsp, n) < 0) + return -1; + + switch (type) { + case HEVC_NAL_VPS: + return get_bits(&gb, 4); + case HEVC_NAL_PPS: + return get_ue_golomb_long(&gb) & 0xff; + } + + skip_bits(&gb, 4); + max_sub_layers = get_bits(&gb, 3); + skip_bits(&gb, 1 + 88 + 8); + if (max_sub_layers > 0) { + int flags = 0; + + for (int i = 0; i < max_sub_layers; i++) + flags |= get_bits(&gb, 2) << (2 * i); + skip_bits(&gb, 2 * (8 - max_sub_layers)); + for (int i = 0; i < max_sub_layers; i++) { + profile_present = (flags >> (2 * i)) & 2; + level_present = (flags >> (2 * i)) & 1; + skip_bits(&gb, 88 * !!profile_present + 8 * level_present); + } + } + if (get_bits_left(&gb) <= 0) + return -1; + return get_ue_golomb_long(&gb) & 0xff; +} + static int hevc_extradata_to_annexb(AVBSFContext *ctx, const uint8_t *extradata, size_t extradata_size, uint8_t **out_extradata, int *out_extradata_size) @@ -187,6 +237,7 @@ static int hevc_extradata_to_annexb(AVBSFContext *ctx, new_nals[nb_nals].offset = new_extradata_size; new_nals[nb_nals].size = 4 + nalu_len; new_nals[nb_nals].type = type; + new_nals[nb_nals].id = -1; new_nals[nb_nals].matched = 0; new_nals[nb_nals].emit = 1; new_nals[nb_nals].hash = 0; @@ -195,6 +246,8 @@ static int hevc_extradata_to_annexb(AVBSFContext *ctx, AV_WB32(new_extradata + new_extradata_size, 1); // add the startcode bytestream2_get_buffer(&gb, new_extradata + new_extradata_size + 4, nalu_len); + new_nals[nb_nals - 1].id = hevc_ps_id(new_extradata + new_extradata_size + 4, + nalu_len, type); new_extradata_size += 4 + nalu_len; } } @@ -292,6 +345,133 @@ static int hevc_mp4toannexb_init(AVBSFContext *ctx) return 0; } +static int hevc_store_ps(AVBSFContext *ctx, const uint8_t *nal, int size, int type) +{ + HEVCBSFContext *s = ctx->priv_data; + int id = hevc_ps_id(nal, size, type); + int idx = -1, nb_nals, nb_ps = 0, ps_table_size; + size_t new_size, off = 0; + uint8_t *extradata; + HEVCNALUnit *nals; + int *buckets; + + if (id < 0) + return 0; + + for (int i = 0; i < s->nb_nals; i++) { + if (s->nals[i].type == type && s->nals[i].id == id) { + idx = i; + break; + } + } + if (idx >= 0 && s->nals[idx].size == 4 + (size_t)size && + !memcmp(s->extradata + s->nals[idx].offset + 4, nal, size)) + return 0; + + nb_nals = s->nb_nals + (idx < 0); + if (nb_nals > MAX_EXTRADATA_NALS) + return 0; + new_size = s->extradata_size + 4 + size - (idx >= 0 ? s->nals[idx].size : 0); + if (new_size > INT_MAX) + return 0; + + extradata = av_malloc(new_size + AV_INPUT_BUFFER_PADDING_SIZE); + nals = av_malloc_array(nb_nals, sizeof(*nals)); + if (!extradata || !nals) { + av_free(extradata); + av_free(nals); + return AVERROR(ENOMEM); + } + + for (int i = 0; i < nb_nals; i++) { + const uint8_t *src; + size_t len; + + if (i < s->nb_nals) + nals[i] = s->nals[i]; + if (i == idx || i == s->nb_nals) { + nals[i].type = type; + nals[i].id = id; + nals[i].matched = 0; + nals[i].emit = 0; + nals[i].next = -1; + nals[i].hash = 0; + src = nal; + len = size; + AV_WB32(extradata + off, 1); + memcpy(extradata + off + 4, src, len); + nals[i].offset = off; + nals[i].size = 4 + len; + } else { + memcpy(extradata + off, s->extradata + nals[i].offset, nals[i].size); + nals[i].offset = off; + } + off += nals[i].size; + } + memset(extradata + off, 0, AV_INPUT_BUFFER_PADDING_SIZE); + + for (int i = 0; i < nb_nals; i++) + nb_ps += nals[i].type >= HEVC_NAL_VPS && nals[i].type <= HEVC_NAL_PPS; + ps_table_size = 1 << av_ceil_log2(FFMAX(32, nb_ps * 2)); + buckets = av_malloc_array(ps_table_size, sizeof(*buckets)); + if (!buckets) { + av_free(extradata); + av_free(nals); + return AVERROR(ENOMEM); + } + for (int i = 0; i < ps_table_size; i++) + buckets[i] = -1; + for (int i = 0; i < nb_nals; i++) { + HEVCNALUnit *n = &nals[i]; + + if (n->type >= HEVC_NAL_VPS && n->type <= HEVC_NAL_PPS) { + int bucket; + + n->hash = hash_ps(extradata + n->offset + 4, n->size - 4); + bucket = n->hash & (ps_table_size - 1); + n->next = buckets[bucket]; + buckets[bucket] = i; + } + } + + av_freep(&s->nals); + av_freep(&s->extradata); + av_freep(&s->ps_buckets); + s->nals = nals; + s->nb_nals = nb_nals; + s->nb_ps = nb_ps; + s->extradata = extradata; + s->extradata_size = new_size; + s->ps_buckets = buckets; + s->ps_table_size = ps_table_size; + return 0; +} + +static int hevc_store_inband_ps(AVBSFContext *ctx, const AVPacket *in) +{ + HEVCBSFContext *s = ctx->priv_data; + GetByteContext gb; + + bytestream2_init(&gb, in->data, in->size); + while (bytestream2_get_bytes_left(&gb) >= s->length_size) { + uint32_t nalu_size = 0; + int type, ret; + + for (int i = 0; i < s->length_size; i++) + nalu_size = (nalu_size << 8) | bytestream2_get_byte(&gb); + if (nalu_size > bytestream2_get_bytes_left(&gb)) + break; + type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f; + if (type >= HEVC_NAL_VPS && type <= HEVC_NAL_PPS) { + ret = hevc_store_ps(ctx, gb.buffer, nalu_size, type); + if (ret < 0) + return ret; + } + bytestream2_skip(&gb, nalu_size); + } + return 0; +} + static void emit_extradata_type(HEVCBSFContext *s, uint8_t *dst, size_t *out_offset, int type) { unsigned bit = 1U << (type - HEVC_NAL_VPS); @@ -362,7 +542,7 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) AVPacket *in; GetByteContext gb; - int got_irap = 0, got_ps = 0, seen_irap_ps = 0; + int got_irap = 0, got_ps = 0, seen_irap_ps = 0, any_ps = 0; int i, ret = 0; size_t extradata_size = 0; size_t in_annexb_size = 0; @@ -421,6 +601,7 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) } nalu_type = (bytestream2_peek_byte(&gb) >> 1) & 0x3f; + any_ps |= nalu_type >= HEVC_NAL_VPS && nalu_type <= HEVC_NAL_PPS; /* Prefix SEI can flush dependent extradata before later VPS/SPS. */ if (!got_irap && nalu_type == HEVC_NAL_SEI_PREFIX) max_inband_ps_level = HEVC_NAL_PPS - HEVC_NAL_VPS; @@ -541,6 +722,9 @@ static int hevc_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out) if (ret < 0) goto fail; + if (any_ps) + ret = hevc_store_inband_ps(ctx, in); + fail: if (ret < 0) av_packet_unref(out); diff --git a/tests/api/api-hevc-mp4toannexb-test.c b/tests/api/api-hevc-mp4toannexb-test.c index c6db4e93f66..1698e122523 100644 --- a/tests/api/api-hevc-mp4toannexb-test.c +++ b/tests/api/api-hevc-mp4toannexb-test.c @@ -61,6 +61,11 @@ static const uint8_t pps0_bytes[] = { 0x44, 0x01, 0xc0, 0x73, 0xc0, 0x89 }; +/* PPS 0 replacement: same pps_pic_parameter_set_id, different payload */ +static const uint8_t pps0b_bytes[] = { + 0x44, 0x01, 0xc0, 0x73, 0xc0, 0x8a +}; + /* PPS with pps_pic_parameter_set_id = 1 */ static const uint8_t pps1_bytes[] = { 0x44, 0x01, 0x50, 0x1c, 0xf0, 0x22, 0x40 @@ -322,6 +327,48 @@ end: return ret; } +static int run_two_packets(const char *case_name, + const uint8_t *hvcc, size_t hvcc_size, + AVPacket *first, AVPacket *second) +{ + const AVBitStreamFilter *bsf = av_bsf_get_by_name("hevc_mp4toannexb"); + AVBSFContext *ctx = NULL; + AVPacket *out = av_packet_alloc(); + AVPacket *in[2] = { first, second }; + int ret = out ? 0 : AVERROR(ENOMEM); + + printf("=== Test: %s ===\n", case_name); + if (ret >= 0) + ret = av_bsf_alloc(bsf, &ctx); + if (ret < 0) + goto end; + + ctx->par_in->codec_type = AVMEDIA_TYPE_VIDEO; + ctx->par_in->codec_id = AV_CODEC_ID_HEVC; + ctx->par_in->extradata = av_memdup(hvcc, hvcc_size + AV_INPUT_BUFFER_PADDING_SIZE); + if (!ctx->par_in->extradata) { + ret = AVERROR(ENOMEM); + goto end; + } + ctx->par_in->extradata_size = hvcc_size; + + ret = av_bsf_init(ctx); + for (int i = 0; ret >= 0 && i < 2; i++) { + ret = av_bsf_send_packet(ctx, in[i]); + if (ret >= 0) + ret = av_bsf_receive_packet(ctx, out); + if (ret >= 0) { + print_packet_hex("Output", out); + av_packet_unref(out); + } + } + +end: + av_packet_free(&out); + av_bsf_free(&ctx); + return ret; +} + int main(void) { uint8_t *hvcc_std = NULL, *hvcc_sei = NULL, *hvcc_dup = NULL, *hvcc_limit = NULL; @@ -552,6 +599,27 @@ int main(void) av_packet_unref(pkt); } + /* Case 12: In-band replacement of PPS 0 stays in effect for a later IRAP without parameter sets */ + { + AVPacket *second = av_packet_alloc(); + const uint8_t *first_nalus[] = { pps0b_bytes, idr_slice_bytes }; + const size_t first_lens[] = { sizeof(pps0b_bytes), sizeof(idr_slice_bytes) }; + const uint8_t *second_nalus[] = { idr_slice_bytes }; + const size_t second_lens[] = { sizeof(idr_slice_bytes) }; + + if (!second || build_packet(pkt, first_nalus, first_lens, FF_ARRAY_ELEMS(first_nalus)) < 0 || + build_packet(second, second_nalus, second_lens, FF_ARRAY_ELEMS(second_nalus)) < 0) { + av_packet_free(&second); + ret = 1; + goto cleanup; + } + err = run_two_packets("Case 12 (In-band Replacement Persists)", hvcc_std, sz_std, pkt, second); + if (err < 0) + ret = 1; + av_packet_free(&second); + av_packet_unref(pkt); + } + cleanup: av_freep(&hvcc_std); av_freep(&hvcc_sei); diff --git a/tests/ref/fate/api-hevc-mp4toannexb b/tests/ref/fate/api-hevc-mp4toannexb index 88f345b99db..76b7ef5303e 100644 --- a/tests/ref/fate/api-hevc-mp4toannexb +++ b/tests/ref/fate/api-hevc-mp4toannexb @@ -30,3 +30,6 @@ Decoded frames: 1 === Test: Case 11 (Multiple SPS IDs Across Prefix SEI) === Output: size 191, hex: 0000000140010c01ffff03700000030090000003000003001e9594090000000142010103700000030090000003000003001ea0208105965654a4c2e01000003e8000003e80800000000142010103700000030090000003000003001e480820416595952930b80400000fa000000fa020000000014401c073c089000000014e010301ff800000000142010103700000030090000003000003001ea0208105965654a4c2e01000003e8000003e8080000000012801ac4ed71ffff5de9cafeaf8 Decoded frames: 1 +=== Test: Case 12 (In-band Replacement Persists) === +Output: size 107, hex: 0000000140010c01ffff03700000030090000003000003001e9594090000000142010103700000030090000003000003001ea0208105965654a4c2e01000003e8000003e8080000000014401c073c089000000014401c073c08a000000012801ac4ed71ffff5de9cafeaf8 +Output: size 97, hex: 0000000140010c01ffff03700000030090000003000003001e9594090000000142010103700000030090000003000003001ea0208105965654a4c2e01000003e8000003e8080000000014401c073c08a000000012801ac4ed71ffff5de9cafeaf8 -- 2.54.0 _______________________________________________ ffmpeg-devel mailing list -- [email protected] To unsubscribe send an email to [email protected]
