Level 4.2/6.2 streams may signal chroma_format=2 (4:2:2) in the
sequence header. Implement the full 4:2:2 decoding chain after the
GDM4.2 reference decoder:

- parse chroma_format from the sequence header, select YUV422P
- MB geometry: chroma MB is 8x16 (full vertical resolution)
- CBP422: a second ue(v) coded block pattern right after CBP carries
  the lower chroma 8x8 blocks (bit0=Cb2, bit1=Cr2); the delta-quant
  is signalled when either CBP is non-zero
- intra: a second chroma intra prediction mode
  (intra_chroma_pred_mode_422) for the lower blocks; its top
  neighbour is the reconstructed bottom line of the upper block, so
  prediction and residual decoding are interleaved in bitstream order
  Cb1,Cr1,Cb2,Cr2
- inter: chroma motion compensation with doubled vertical
  displacement (1/8 chroma pel = 1/4 luma pel vertically)
- deblocking: left chroma edge filtered as two 8-row halves with
  bs[0]/bs[1], plus the internal horizontal chroma edge between the
  two chroma blocks (bs[6]/bs[7])

Fixes decoding of the AVS1-P2_JZP09/FINAL 30/31_Hisilicon
conformance streams (9 streams, all bit-exact vs GDM4.2 output).

Co-Authored-By: kimi-k3 with kimi-code
Signed-off-by: jianfeng.zheng <[email protected]>
---
 libavcodec/cavs.c    | 122 +++++++++++++++++++++++++++++++------
 libavcodec/cavs.h    |   7 ++-
 libavcodec/cavsdec.c | 141 +++++++++++++++++++++++++++++++++++++------
 3 files changed, 232 insertions(+), 38 deletions(-)

diff --git a/libavcodec/cavs.c b/libavcodec/cavs.c
index 72a3ce70ed..399b25d6b4 100644
--- a/libavcodec/cavs.c
+++ b/libavcodec/cavs.c
@@ -121,17 +121,21 @@ void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type)
     uint8_t bs[8];
     int qp_avg, alpha, beta, tc;
     int i;
+    /* chroma macroblock height in samples: 8 for 4:2:0, 16 for 4:2:2 */
+    const int cm_h = h->chroma_format == 2 ? 16 : 8;
 
     /* save un-deblocked lines */
     h->topleft_border_y = h->top_border_y[h->mbx * 16 + 15];
     h->topleft_border_u = h->top_border_u[h->mbx * 10 + 8];
     h->topleft_border_v = h->top_border_v[h->mbx * 10 + 8];
     memcpy(&h->top_border_y[h->mbx * 16],     h->cy + 15 * h->l_stride, 16);
-    memcpy(&h->top_border_u[h->mbx * 10 + 1], h->cu +  7 * h->c_stride, 8);
-    memcpy(&h->top_border_v[h->mbx * 10 + 1], h->cv +  7 * h->c_stride, 8);
+    memcpy(&h->top_border_u[h->mbx * 10 + 1], h->cu + (cm_h - 1) * 
h->c_stride, 8);
+    memcpy(&h->top_border_v[h->mbx * 10 + 1], h->cv + (cm_h - 1) * 
h->c_stride, 8);
     for (i = 0; i < 8; i++) {
         h->left_border_y[i * 2 + 1] = *(h->cy + 15 + (i * 2 + 0) * 
h->l_stride);
         h->left_border_y[i * 2 + 2] = *(h->cy + 15 + (i * 2 + 1) * 
h->l_stride);
+    }
+    for (i = 0; i < cm_h; i++) {
         h->left_border_u[i + 1]     = *(h->cu + 7  +  i          * 
h->c_stride);
         h->left_border_v[i + 1]     = *(h->cv + 7  +  i          * 
h->c_stride);
     }
@@ -161,13 +165,39 @@ void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type)
                 h->cdsp.cavs_filter_lv(h->cy, h->l_stride, alpha, beta, tc, 
bs[0], bs[1]);
                 qp_avg = (ff_cavs_chroma_qp[h->qp] + 
ff_cavs_chroma_qp[h->left_qp] + 1) >> 1;
                 SET_PARAMS;
-                h->cdsp.cavs_filter_cv(h->cu, h->c_stride, alpha, beta, tc, 
bs[0], bs[1]);
-                h->cdsp.cavs_filter_cv(h->cv, h->c_stride, alpha, beta, tc, 
bs[0], bs[1]);
+                if (h->chroma_format == 2) {
+                    /* 4:2:2: the left chroma edge is 16 samples high, the
+                     * top half takes bs[0], the bottom half bs[1] (GDM
+                     * DeblockMb422) */
+                    h->cdsp.cavs_filter_cv(h->cu, h->c_stride, alpha, beta, 
tc, bs[0], bs[0]);
+                    h->cdsp.cavs_filter_cv(h->cu + 8 * h->c_stride, 
h->c_stride, alpha, beta, tc, bs[1], bs[1]);
+                } else {
+                    h->cdsp.cavs_filter_cv(h->cu, h->c_stride, alpha, beta, 
tc, bs[0], bs[1]);
+                }
+                qp_avg = (ff_cavs_chroma_qp[h->qp] +
+                          ff_cavs_chroma_qp[h->left_qp] + 1) >> 1;
+                SET_PARAMS;
+                if (h->chroma_format == 2) {
+                    h->cdsp.cavs_filter_cv(h->cv, h->c_stride, alpha, beta, 
tc, bs[0], bs[0]);
+                    h->cdsp.cavs_filter_cv(h->cv + 8 * h->c_stride, 
h->c_stride, alpha, beta, tc, bs[1], bs[1]);
+                } else {
+                    h->cdsp.cavs_filter_cv(h->cv, h->c_stride, alpha, beta, 
tc, bs[0], bs[1]);
+                }
             }
             qp_avg = h->qp;
             SET_PARAMS;
             h->cdsp.cavs_filter_lv(h->cy + 8,               h->l_stride, 
alpha, beta, tc, bs[2], bs[3]);
             h->cdsp.cavs_filter_lh(h->cy + 8 * h->l_stride, h->l_stride, 
alpha, beta, tc, bs[6], bs[7]);
+            if (h->chroma_format == 2) {
+                /* 4:2:2: the internal horizontal luma edge coincides with
+                 * the edge between the two chroma 8x8 blocks */
+                qp_avg = ff_cavs_chroma_qp[h->qp];
+                SET_PARAMS;
+                h->cdsp.cavs_filter_ch(h->cu + 8 * h->c_stride, h->c_stride, 
alpha, beta, tc, bs[6], bs[7]);
+                qp_avg = ff_cavs_chroma_qp[h->qp];
+                SET_PARAMS;
+                h->cdsp.cavs_filter_ch(h->cv + 8 * h->c_stride, h->c_stride, 
alpha, beta, tc, bs[6], bs[7]);
+            }
 
             if (h->flags & B_AVAIL) {
                 qp_avg = (h->qp + h->top_qp[h->mbx] + 1) >> 1;
@@ -246,8 +276,15 @@ void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t 
*top,
 void ff_cavs_load_intra_pred_chroma(AVSContext *h)
 {
     /* extend borders by one pixel */
-    h->left_border_u[9]              = h->left_border_u[8];
-    h->left_border_v[9]              = h->left_border_v[8];
+    if (h->chroma_format == 2) {
+        /* 4:2:2: left_border_u/v[1..16] hold the full 16-sample column
+         * of the left MB; the lowpass of the first 8x8 block reads the
+         * real sample below it (index 9), the second 8x8 block extends
+         * its own local copy in ff_cavs_intra_pred_chroma_422() */
+    } else {
+        h->left_border_u[9]              = h->left_border_u[8];
+        h->left_border_v[9]              = h->left_border_v[8];
+    }
     if(h->flags & C_AVAIL) {
         h->top_border_u[h->mbx*10 + 9] = h->top_border_u[h->mbx*10 + 11];
         h->top_border_v[h->mbx*10 + 9] = h->top_border_v[h->mbx*10 + 11];
@@ -391,6 +428,39 @@ void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv)
     }
 }
 
+/**
+ * intra prediction of the second chroma 8x8 block of a 4:2:2 macroblock
+ * (GDM "block 6&7 intra decoding"): its top neighbour is the just decoded
+ * bottom line of the first chroma block of the same macroblock (always
+ * available), its left neighbours are the bottom half of the left
+ * macroblock's chroma column
+ * @param dc        destination (top-left sample of the second block)
+ * @param left_border  left border column of the whole 8x16 chroma area
+ * @param pred_mode raw intra_chroma_pred_mode_422 from the bitstream
+ */
+void ff_cavs_intra_pred_chroma_422(AVSContext *h, uint8_t *dc,
+                                   uint8_t *left_border, int pred_mode)
+{
+    uint8_t top[18];
+    uint8_t left[10];
+
+    memcpy(&top[1], dc - h->c_stride, 8);
+    /* the top-right samples belong to the not yet coded right MB */
+    memset(&top[9], top[8], 9);
+    if (h->flags & A_AVAIL) {
+        /* the corner sample is the left column sample of the line above */
+        top[0] = left_border[8];
+    } else {
+        top[0] = top[1];
+        modify_pred(left_modifier_c, &pred_mode);
+    }
+    left[0] = top[0];
+    memcpy(&left[1], &left_border[9], 8);
+    left[9] = left[8];
+
+    h->intra_pred_c[pred_mode](dc, top, left, h->c_stride);
+}
+
 /*****************************************************************************
  *
  * motion compensation
@@ -410,13 +480,17 @@ static inline void mc_dir_part(AVSContext *h, AVFrame 
*pic, int parity,
 
     const int mx         = mv->x + src_x_offset * 8;
     const int my         = mv->y + src_y_offset * 8;
+    /* chroma displacement in 1/8 chroma pel units; for 4:2:2 the chroma
+     * vertical resolution is the full luma resolution, so the vertical
+     * displacement doubles (GDM: j1 = pix_c_y * 8 + 2 * mvy) */
+    const int my_c       = h->chroma_format == 2 ? 2 * my : my;
     const int luma_xy    = (mx & 3) + ((my & 3) << 2);
     uint8_t *src_y       = pic->data[0] + parity * pic->linesize[0] +
                            (mx >> 2) + (my >> 2) * h->l_stride;
     uint8_t *src_cb      = pic->data[1] + parity * pic->linesize[1] +
-                           (mx >> 3) + (my >> 3) * h->c_stride;
+                           (mx >> 3) + (my_c >> 3) * h->c_stride;
     uint8_t *src_cr      = pic->data[2] + parity * pic->linesize[2] +
-                           (mx >> 3) + (my >> 3) * h->c_stride;
+                           (mx >> 3) + (my_c >> 3) * h->c_stride;
     int extra_width      = 0;
     int extra_height     = extra_width;
     const int full_mx    = mx >> 2;
@@ -425,8 +499,12 @@ static inline void mc_dir_part(AVSContext *h, AVFrame 
*pic, int parity,
     /* motion vectors address a field in quarter field lines (9.9.2.1) */
     const int pic_height = h->pic_structure ? 16 * h->mb_height
                                             :  8 * h->mb_height;
+    /* chroma vertical resolution: half of luma for 4:2:0, full for 4:2:2 */
+    const int pic_height_c = h->chroma_format == 2 ? pic_height
+                                                   : pic_height >> 1;
     int emu = 0;
 
+
     if (mx & 7)
         extra_width  -= 3;
     if (my & 7)
@@ -452,22 +530,22 @@ static inline void mc_dir_part(AVSContext *h, AVFrame 
*pic, int parity,
     if (emu) {
         h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cb,
                                  h->c_stride, h->c_stride,
-                                 9, 9 /* FIXME */,
-                                 mx >> 3, my >> 3,
-                                 pic_width >> 1, pic_height >> 1);
+                                 9, chroma_height + 1,
+                                 mx >> 3, my_c >> 3,
+                                 pic_width >> 1, pic_height_c);
         src_cb = h->edge_emu_buffer;
     }
-    chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx & 7, my & 7);
+    chroma_op(dest_cb, src_cb, h->c_stride, chroma_height, mx & 7, my_c & 7);
 
     if (emu) {
         h->vdsp.emulated_edge_mc(h->edge_emu_buffer, src_cr,
                                  h->c_stride, h->c_stride,
-                                 9, 9 /* FIXME */,
-                                 mx >> 3, my >> 3,
-                                 pic_width >> 1, pic_height >> 1);
+                                 9, chroma_height + 1,
+                                 mx >> 3, my_c >> 3,
+                                 pic_width >> 1, pic_height_c);
         src_cr = h->edge_emu_buffer;
     }
-    chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx & 7, my & 7);
+    chroma_op(dest_cr, src_cr, h->c_stride, chroma_height, mx & 7, my_c & 7);
 }
 
 static inline void mc_part_std(AVSContext *h, int chroma_height,
@@ -483,10 +561,12 @@ static inline void mc_part_std(AVSContext *h, int 
chroma_height,
 {
     qpel_mc_func *qpix_op =  qpix_put;
     h264_chroma_mc_func chroma_op = chroma_put;
+    /* for 4:2:2 the chroma block keeps the luma block's height */
+    chroma_height <<= (h->chroma_format == 2);
 
     dest_y   += x_offset * 2 + y_offset * h->l_stride * 2;
-    dest_cb  += x_offset     + y_offset * h->c_stride;
-    dest_cr  += x_offset     + y_offset * h->c_stride;
+    dest_cb  += x_offset     + (y_offset << (h->chroma_format == 2)) * 
h->c_stride;
+    dest_cr  += x_offset     + (y_offset << (h->chroma_format == 2)) * 
h->c_stride;
     x_offset += 8 * h->mbx;
     /* the row is counted inside the current field (clause 3.24) */
     y_offset += 8 * h->field_mby;
@@ -676,6 +756,8 @@ void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum 
cavs_mv_loc nC,
 void ff_cavs_set_mb_row(AVSContext *h)
 {
     int parity = 0;
+    /* chroma macroblock height: 8 samples for 4:2:0, 16 for 4:2:2 */
+    const int cm_h = h->chroma_format == 2 ? 16 : 8;
 
     h->field     = !h->pic_structure && h->mby >= h->mb_height / 2;
     h->field_mby = h->mby - h->field * (h->mb_height / 2);
@@ -688,9 +770,9 @@ void ff_cavs_set_mb_row(AVSContext *h)
     h->cy = h->cur.f->data[0] + parity * h->cur.f->linesize[0] +
             h->field_mby * 16 * h->l_stride;
     h->cu = h->cur.f->data[1] + parity * h->cur.f->linesize[1] +
-            h->field_mby *  8 * h->c_stride;
+            h->field_mby * cm_h * h->c_stride;
     h->cv = h->cur.f->data[2] + parity * h->cur.f->linesize[2] +
-            h->field_mby *  8 * h->c_stride;
+            h->field_mby * cm_h * h->c_stride;
 }
 
 /**
diff --git a/libavcodec/cavs.h b/libavcodec/cavs.h
index acc33b0793..d40c8fe3e3 100644
--- a/libavcodec/cavs.h
+++ b/libavcodec/cavs.h
@@ -197,6 +197,7 @@ typedef struct AVSContext {
     int progressive_seq;
     int profile, level;
     int aspect_ratio;
+    int chroma_format; ///< 1: 4:2:0, 2: 4:2:2 (level 4.2/6.2 streams)
     int mb_width, mb_height;
     int width, height;
     int stream_revision; ///<0 for samples from 2006, 1 for rm52j encoder
@@ -259,6 +260,8 @@ typedef struct AVSContext {
     int qp_fixed;
     int pic_qp_fixed;
     int cbp;
+    int cbp422;        ///< coded block pattern of the second chroma 8x8
+                       ///< blocks (4:2:2): bit0 = U, bit1 = V
     DECLARE_ALIGNED(32, int16_t, block)[64];
     uint8_t permutated_scantable[64];
     /** inverse block scan method 2 of clause 9.5.3 c), for field pictures */
@@ -269,7 +272,7 @@ typedef struct AVSContext {
     /** intra prediction is done with un-deblocked samples
      they are saved here before deblocking the MB  */
     uint8_t *top_border_y, *top_border_u, *top_border_v;
-    uint8_t left_border_y[26], left_border_u[10], left_border_v[10];
+    uint8_t left_border_y[26], left_border_u[17], left_border_v[17];
     uint8_t intern_border_y[26];
     uint8_t topleft_border_y, topleft_border_u, topleft_border_v;
 
@@ -350,6 +353,8 @@ void ff_cavs_filter(AVSContext *h, enum cavs_mb mb_type);
 void ff_cavs_load_intra_pred_luma(AVSContext *h, uint8_t *top, uint8_t **left,
                                   int block);
 void ff_cavs_load_intra_pred_chroma(AVSContext *h);
+void ff_cavs_intra_pred_chroma_422(AVSContext *h, uint8_t *dc,
+                                   uint8_t *left_border, int pred_mode);
 void ff_cavs_modify_mb_i(AVSContext *h, int *pred_mode_uv);
 void ff_cavs_inter(AVSContext *h, enum cavs_mb mb_type);
 void ff_cavs_mv(AVSContext *h, enum cavs_mv_loc nP, enum cavs_mv_loc nC,
diff --git a/libavcodec/cavsdec.c b/libavcodec/cavsdec.c
index ab168ba8ca..706b1efb65 100644
--- a/libavcodec/cavsdec.c
+++ b/libavcodec/cavsdec.c
@@ -1120,7 +1120,7 @@ static int decode_mb_qp_delta(AVSContext *h)
 {
     int delta = 0;
 
-    if (h->cbp && !h->qp_fixed) {
+    if ((h->cbp | h->cbp422) && !h->qp_fixed) {
         if (h->aec_enable) {
             int ret = aec_mb_qp_delta(h, &delta);
             if (ret < 0)
@@ -1145,21 +1145,55 @@ static int decode_residual(AVSContext *h, const struct 
dec_2dvlc *r,
                                  stride);
 }
 
+static inline void get_chroma_qp(AVSContext *h, int *qp_cb, int *qp_cr)
+{
+    *qp_cb = *qp_cr = ff_cavs_chroma_qp[h->qp];
+}
+
+/** decode the residual of one chroma 8x8 block
+ * @param plane  0: Cb, 1: Cr
+ * @param second 1: the lower 8x8 block of a 4:2:2 macroblock */
+static inline int decode_residual_chroma_block(AVSContext *h, int plane,
+                                               int second)
+{
+    int qp_cb, qp_cr;
+    uint8_t *dst = plane ? h->cv : h->cu;
+
+    if (second)
+        dst += 8 * h->c_stride;
+    get_chroma_qp(h, &qp_cb, &qp_cr);
+    return decode_residual(h, chroma_dec, 0, 1,
+                           plane ? qp_cr : qp_cb, dst, h->c_stride);
+}
+
+/* chroma residuals are stored in the bitstream in the order Cb1, Cr1 for
+ * 4:2:0 and Cb1, Cr1, Cb2, Cr2 (CBP then CBP422) for 4:2:2 */
 static inline int decode_residual_chroma(AVSContext *h)
 {
-    const int qp = ff_cavs_chroma_qp[h->qp];
     int ret;
 
     if (h->cbp & (1 << 4)) {
-        ret = decode_residual(h, chroma_dec, 0, 1, qp, h->cu, h->c_stride);
+        ret = decode_residual_chroma_block(h, 0, 0);
         if (ret < 0)
             return ret;
     }
     if (h->cbp & (1 << 5)) {
-        ret = decode_residual(h, chroma_dec, 0, 1, qp, h->cv, h->c_stride);
+        ret = decode_residual_chroma_block(h, 1, 0);
         if (ret < 0)
             return ret;
     }
+    if (h->chroma_format == 2) {
+        if (h->cbp422 & 1) {
+            ret = decode_residual_chroma_block(h, 0, 1);
+            if (ret < 0)
+                return ret;
+        }
+        if (h->cbp422 & 2) {
+            ret = decode_residual_chroma_block(h, 1, 1);
+            if (ret < 0)
+                return ret;
+        }
+    }
     return 0;
 }
 
@@ -1178,6 +1212,16 @@ static inline int decode_residual_inter(AVSContext *h)
         }
         h->cbp = cbp_tab[cbp][1];
     }
+    /* 4:2:2 streams carry a second coded block pattern for the lower
+     * chroma 8x8 blocks right after CBP (GDM CBP422); the reference
+     * decoder reads it with the UVLC reader even in AEC streams */
+    if (h->chroma_format == 2) {
+        h->cbp422 = get_ue_golomb(&h->gb);
+        if (h->cbp422 > 3U) {
+            av_log(h->avctx, AV_LOG_ERROR, "illegal cbp422 %d\n", h->cbp422);
+            return AVERROR_INVALIDDATA;
+        }
+    }
     ret = decode_mb_qp_delta(h);
     if (ret < 0)
         return ret;
@@ -1212,6 +1256,7 @@ static int decode_mb_i(AVSContext *h, int cbp_code)
 {
     GetBitContext *gb = &h->gb;
     unsigned pred_mode_uv;
+    unsigned pred_mode_uv_2 = 0; ///< intra_chroma_pred_mode_422 (4:2:2)
     int block;
     uint8_t top[18];
     uint8_t *left = NULL;
@@ -1249,6 +1294,14 @@ static int decode_mb_i(AVSContext *h, int cbp_code)
             return AVERROR_INVALIDDATA;
         }
     }
+    if (h->chroma_format == 2) {
+        /* 4:2:2: prediction mode of the lower chroma 8x8 blocks */
+        pred_mode_uv_2 = get_ue_golomb_31(gb);
+        if (pred_mode_uv_2 > 6) {
+            av_log(h->avctx, AV_LOG_ERROR, "illegal intra chroma pred mode 
422\n");
+            return AVERROR_INVALIDDATA;
+        }
+    }
     ff_cavs_modify_mb_i(h, &pred_mode_uv);
 
     /* get coded block pattern */
@@ -1265,6 +1318,16 @@ static int decode_mb_i(AVSContext *h, int cbp_code)
         }
         h->cbp = cbp_tab[cbp_code][0];
     }
+    /* 4:2:2 streams carry a second coded block pattern for the lower
+     * chroma 8x8 blocks right after CBP (GDM CBP422); the reference
+     * decoder reads it with the UVLC reader even in AEC streams */
+    if (h->chroma_format == 2) {
+        h->cbp422 = get_ue_golomb(gb);
+        if (h->cbp422 > 3U) {
+            av_log(h->avctx, AV_LOG_ERROR, "illegal cbp422 %d\n", h->cbp422);
+            return AVERROR_INVALIDDATA;
+        }
+    }
     ret = decode_mb_qp_delta(h);
     if (ret < 0)
         return ret;
@@ -1289,9 +1352,39 @@ static int decode_mb_i(AVSContext *h, int cbp_code)
     h->intra_pred_c[pred_mode_uv](h->cv, &h->top_border_v[h->mbx * 10],
                                   h->left_border_v, h->c_stride);
 
-    ret = decode_residual_chroma(h);
-    if (ret < 0)
-        return ret;
+    if (h->chroma_format == 2) {
+        /* the chroma residuals are stored Cb1, Cr1, Cb2, Cr2 while the
+         * prediction of the second 8x8 blocks needs the reconstructed
+         * first blocks: interleave prediction and residual decoding */
+        if (h->cbp & (1 << 4)) {
+            ret = decode_residual_chroma_block(h, 0, 0);
+            if (ret < 0)
+                return ret;
+        }
+        if (h->cbp & (1 << 5)) {
+            ret = decode_residual_chroma_block(h, 1, 0);
+            if (ret < 0)
+                return ret;
+        }
+        ff_cavs_intra_pred_chroma_422(h, h->cu + 8 * h->c_stride,
+                                      h->left_border_u, pred_mode_uv_2);
+        if (h->cbp422 & 1) {
+            ret = decode_residual_chroma_block(h, 0, 1);
+            if (ret < 0)
+                return ret;
+        }
+        ff_cavs_intra_pred_chroma_422(h, h->cv + 8 * h->c_stride,
+                                      h->left_border_v, pred_mode_uv_2);
+        if (h->cbp422 & 2) {
+            ret = decode_residual_chroma_block(h, 1, 1);
+            if (ret < 0)
+                return ret;
+        }
+    } else {
+        ret = decode_residual_chroma(h);
+        if (ret < 0)
+            return ret;
+    }
     ff_cavs_filter(h, I_8X8);
     set_mv_intra(h);
     return 0;
@@ -2102,23 +2195,27 @@ static int decode_seq_header(AVSContext *h)
     }
     chroma_format    = get_bits(&h->gb, 2);
     sample_precision = get_bits(&h->gb, 3);
-    /* the Broadcasting macroblock layer is written for 8 bit 4:2:0 */
+    /* the Broadcasting macroblock layer is written for 8 bit */
     if (profile == AV_PROFILE_CAVS_BROADCASTING) {
         if (sample_precision != 1) {
             av_log(h->avctx, AV_LOG_ERROR,
                    "sample_precision %d is not 8 bit\n", sample_precision);
             return AVERROR_INVALIDDATA;
         }
-        if (chroma_format == 2) {
-            avpriv_request_sample(h->avctx, "4:2:2 chroma");
-            return AVERROR_PATCHWELCOME;
-        }
-        if (chroma_format != 1) {
-            av_log(h->avctx, AV_LOG_ERROR,
-                   "chroma_format %d is reserved\n", chroma_format);
-            return AVERROR_INVALIDDATA;
-        }
     }
+    /* legacy encoder clips (e.g. cavs.mpg) signal the reserved value 0
+     * while carrying 4:2:0 content; FFmpeg historically decoded those
+     * by ignoring the field.  In the AVS1-P16 spec 0 means 4:0:0
+     * (monochrome), which is not implemented - reject it for the
+     * Broadcasting profile rather than misdecoding it as 4:2:0. */
+    if (chroma_format == 3 ||
+        !chroma_format && profile == AV_PROFILE_CAVS_BROADCASTING) {
+        avpriv_report_missing_feature(h->avctx, "chroma_format %d",
+                                      chroma_format);
+        return AVERROR_PATCHWELCOME;
+    }
+    if (chroma_format != 2)
+        chroma_format = 1;
     aspect_ratio = get_bits(&h->gb, 4);
     frame_rate_code = get_bits(&h->gb, 4);
     if (frame_rate_code == 0 || frame_rate_code > 13) {
@@ -2141,6 +2238,13 @@ static int decode_seq_header(AVSContext *h)
         avpriv_report_missing_feature(h->avctx, "Macroblock grid changing");
         return AVERROR_PATCHWELCOME;
     }
+    /* the DPB references share the current chroma geometry; a mid-stream
+     * chroma_format change would let motion compensation read the old
+     * references with the new plane heights */
+    if (h->top_qp && chroma_format != h->chroma_format) {
+        avpriv_report_missing_feature(h->avctx, "chroma_format changing in 
CAVS");
+        return AVERROR_PATCHWELCOME;
+    }
 
     /* horizontal_size and vertical_size are only the displayed part */
     ret = ff_set_dimensions(h->avctx, mb_width * 16, mb_height * 16);
@@ -2158,6 +2262,7 @@ static int decode_seq_header(AVSContext *h)
     h->mb_width        = mb_width;
     h->mb_height       = mb_height;
     h->low_delay       = low_delay;
+    h->chroma_format   = chroma_format;
     h->avctx->profile  = profile;
     h->progressive_seq = progressive_seq;
     h->avctx->framerate = ff_mpeg12_frame_rate_tab[frame_rate_code];
@@ -2355,6 +2460,8 @@ static int cavs_decode_frame(AVCodecContext *avctx, 
AVFrame *rframe,
             ret = decode_seq_header(h);
             if (ret < 0)
                 return ret;
+            avctx->pix_fmt = h->chroma_format == 2 ? AV_PIX_FMT_YUV422P
+                                                   : AV_PIX_FMT_YUV420P;
             break;
         case PIC_I_START_CODE:
             if (!h->got_keyframe) {
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to