--- ./libavcodec/hevc/refs.c	2026-03-16 18:13:04.000000000 +0000
+++ ./libavcodec/hevc/refs.c.new	2026-05-15 02:05:54.400611783 +0000
@@ -189,7 +189,7 @@
             goto fail;
 
         frame->pps = av_refstruct_ref_c(s->pps);
-        if (l != &s->layers[0] && ff_hevc_is_alpha_video(s)) {
+        if (l != &s->layers[0] && ff_hevc_is_alpha_video(s) && &s->layers[0].cur_frame->f != 0 ) {
             AVFrame *alpha = frame->f;
             AVFrame *base = s->layers[0].cur_frame->f;
             ret = replace_alpha_plane(alpha, base);
@@ -463,20 +463,20 @@
 static HEVCFrame *generate_missing_ref(HEVCContext *s, HEVCLayerContext *l, int poc)
 {
     HEVCFrame *frame;
-    int i, y;
 
     frame = alloc_frame(s, l);
     if (!frame)
         return NULL;
 
     if (!s->avctx->hwaccel) {
+        int nb_planes = l->sps->chroma_format_idc ? 3 : 1;
         if (!l->sps->pixel_shift) {
-            for (i = 0; frame->f->data[i]; i++)
+            for (int i = 0; i < nb_planes; i++)
                 memset(frame->f->data[i], 1 << (l->sps->bit_depth - 1),
                        frame->f->linesize[i] * AV_CEIL_RSHIFT(l->sps->height, l->sps->vshift[i]));
         } else {
-            for (i = 0; frame->f->data[i]; i++)
-                for (y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) {
+            for (int i = 0; i < nb_planes; i++)
+                for (int y = 0; y < (l->sps->height >> l->sps->vshift[i]); y++) {
                     uint8_t *dst = frame->f->data[i] + y * frame->f->linesize[i];
                     AV_WN16(dst, 1 << (l->sps->bit_depth - 1));
                     av_memcpy_backptr(dst + 2, 2, 2*(l->sps->width >> l->sps->hshift[i]) - 2);
