From 38ee240962b31920fcf7198b6f60d571535d0ddb Mon Sep 17 00:00:00 2001
From: Andrew Randrianasulu <randrianasulu@gmail.com>
Date: Sat, 9 May 2026 05:36:13 +0000
Subject: [PATCH 1/6] Add two patches for decoding hevc-alpha

---
 .../thirdparty/src/ffmpeg-8.1.patch11         | 15 +++++
 .../thirdparty/src/ffmpeg-8.1.patch12         | 55 +++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch11
 create mode 100644 cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch12

diff --git a/cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch11 b/cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch11
new file mode 100644
index 00000000..1d44fb35
--- /dev/null
+++ b/cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch11
@@ -0,0 +1,15 @@
+--- ./libavcodec/hevc/hevcdec.c	2026-03-16 18:13:04.000000000 +0000
++++ ./libavcodec/hevc/hevcdec.c.new	2026-05-09 05:22:45.098799320 +0000
+@@ -712,9 +712,11 @@
+         break;
+     }
+ 
++        
+     if (alpha_fmt != AV_PIX_FMT_NONE)
+         *fmt++ = alpha_fmt;
+-    *fmt++ = sps->pix_fmt;
++    if(alpha_fmt == AV_PIX_FMT_NONE)
++        *fmt++ = sps->pix_fmt;
+     *fmt = AV_PIX_FMT_NONE;
+ 
+     // export multilayer information from active VPS to the caller,
diff --git a/cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch12 b/cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch12
new file mode 100644
index 00000000..a504e140
--- /dev/null
+++ b/cinelerra-5.1/thirdparty/src/ffmpeg-8.1.patch12
@@ -0,0 +1,55 @@
+X-Git-Url: http://source.ffmpeg.org/gitweb/ffmpeg.git/blobdiff_plain/28ab24b71717027ab67e6220bd0dad82a90f22c1..eedf8f0165fe6523be692c5a91ebf6744808335d:/libavcodec/hevc/ps.c
+
+diff --git a/libavcodec/hevc/ps.c b/libavcodec/hevc/ps.c
+index 46b38564d5..10c9a36102 100644
+--- a/libavcodec/hevc/ps.c
++++ b/libavcodec/hevc/ps.c
+@@ -610,8 +610,11 @@ static int decode_vps_ext(GetBitContext *gb, AVCodecContext *avctx, HEVCVPS *vps
+         }
+     }
+     vps->num_output_layer_sets = vps->vps_num_layer_sets + vps->num_add_layer_sets;
+-    if (vps->num_output_layer_sets != 2)
+-        return AVERROR_INVALIDDATA;
++    if (vps->num_output_layer_sets != 2) {
++        av_log(avctx, AV_LOG_WARNING,
++               "Unsupported num_output_layer_sets: %d\n", vps->num_output_layer_sets);
++        return AVERROR_PATCHWELCOME;
++    }
+ 
+     sub_layers_max_present = get_bits1(gb); // vps_sub_layers_max_minus1_present_flag
+     if (sub_layers_max_present) {
+@@ -677,7 +680,7 @@ static int decode_vps_ext(GetBitContext *gb, AVCodecContext *avctx, HEVCVPS *vps
+ 
+     if (get_ue_golomb_31(gb) != 0 /* vps_num_rep_formats_minus1 */) {
+         av_log(avctx, AV_LOG_ERROR, "Unexpected extra rep formats\n");
+-        return AVERROR_INVALIDDATA;
++        return AVERROR_PATCHWELCOME;
+     }
+ 
+     vps->rep_format.pic_width_in_luma_samples  = get_bits(gb, 16);
+@@ -895,8 +898,23 @@ int ff_hevc_decode_nal_vps(GetBitContext *gb, AVCodecContext *avctx,
+     if (vps->vps_max_layers > 1 && get_bits1(gb)) { /* vps_extension_flag */
+         int ret = decode_vps_ext(gb, avctx, vps, layer1_id_included);
+         if (ret == AVERROR_PATCHWELCOME) {
+-            vps->nb_layers = 1;
+-            av_log(avctx, AV_LOG_WARNING, "Ignoring unsupported VPS extension\n");
++            /* If alpha layer info was already parsed, preserve it for alpha decoding */
++            if (!(avctx->err_recognition & (AV_EF_BITSTREAM | AV_EF_COMPLIANT)) &&
++                vps->nb_layers == 2 &&
++                vps->layer_id_in_nuh[1] &&
++                (vps->scalability_mask_flag & HEVC_SCALABILITY_AUXILIARY)) {
++                av_log(avctx, AV_LOG_WARNING,
++                       "Broken VPS extension, treating as alpha video\n");
++                /* If alpha layer has no direct dependency on base layer,
++                 * assume poc_lsb_not_present for the alpha layer, so that
++                 * IDR slices on that layer won't read pic_order_cnt_lsb.
++                 * This matches the behavior of Apple VideoToolbox encoders. */
++                if (!vps->num_direct_ref_layers[1])
++                    vps->poc_lsb_not_present |= 1 << 1;
++            } else {
++                vps->nb_layers = 1;
++                av_log(avctx, AV_LOG_WARNING, "Ignoring unsupported VPS extension\n");
++            }
+             ret = 0;
+         } else if (ret < 0)
+             goto err;
-- 
2.29.0

