diff --git a/drivers/media/platform/apple/avd/.kunitconfig b/drivers/media/platform/apple/avd/.kunitconfig new file mode 100644 index 00000000000000..fde64597fe0f9f --- /dev/null +++ b/drivers/media/platform/apple/avd/.kunitconfig @@ -0,0 +1,11 @@ +CONFIG_KUNIT=y +CONFIG_ARCH_APPLE=y +CONFIG_MEDIA_SUPPORT=y +CONFIG_MEDIA_PLATFORM_SUPPORT=y +CONFIG_MEDIA_CONTROLLER=y +CONFIG_VIDEO_DEV=y +CONFIG_V4L_PLATFORM_DRIVERS=y +CONFIG_VIDEO_APPLE_AVD=y +CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST=y +CONFIG_KASAN=y +CONFIG_KASAN_GENERIC=y diff --git a/drivers/media/platform/apple/avd/Makefile b/drivers/media/platform/apple/avd/Makefile index 4d1877c414d41a..13b6e41fd0a43a 100644 --- a/drivers/media/platform/apple/avd/Makefile +++ b/drivers/media/platform/apple/avd/Makefile @@ -1,6 +1,6 @@ # SPDX-License-Identifier: GPL-2.0-only -apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-h264.o avd-vp9.o avd-hevc.o \ +apple-avd-y := avd-drv.o avd-v4l2.o avd-hw.o avd-h264.o avd-vp9.o avd-hevc.o avd-hevc-parse.o \ avd-av1.o avd-av1-entropymode.o -apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o +apple-avd-$(CONFIG_VIDEO_APPLE_AVD_KUNIT_TEST) += avd-h264-test.o avd-hevc-parse-test.o obj-$(CONFIG_VIDEO_APPLE_AVD) += apple-avd.o diff --git a/drivers/media/platform/apple/avd/avd-h264.c b/drivers/media/platform/apple/avd/avd-h264.c index e8bc8d0066eadb..e9264d1bb702ca 100644 --- a/drivers/media/platform/apple/avd/avd-h264.c +++ b/drivers/media/platform/apple/avd/avd-h264.c @@ -37,6 +37,9 @@ */ #define MAX_SLICES 4096 +/* no picture, or a DPB entry without a VP reference slot */ +#define AVD_H264_NO_REF V4L2_H264_NUM_DPB_ENTRIES + struct avd_h264_run { struct avd_run base; @@ -54,7 +57,12 @@ struct avd_h264_run { s32 cur_poc; u8 num_valid; - u32 slice_data_off; + /* VP reference slot of each DPB entry, see avd_h264_assign_slots() */ + u8 slot[V4L2_H264_NUM_DPB_ENTRIES]; + u8 num_slots; + /* coded slice data after the slice header */ + dma_addr_t slice_addr; + u32 slice_len; }; /* state */ @@ -66,6 +74,10 @@ struct avd_h264_ctx { struct v4l2_h264_reference b1[V4L2_H264_REF_LIST_LEN]; } reflists; + /* slice header and weights parsed in-kernel in frame-based mode */ + struct v4l2_ctrl_h264_slice_params fb_slice; + struct v4l2_ctrl_h264_pred_weights fb_weights; + struct avd_buf *slices; struct avd_buf *active_slice; size_t slice_num; @@ -127,7 +139,7 @@ static void stream_refs(struct avd_ctx *ctx, struct avd_h264_run *run) push(0, ""); for (int i = 0; i < ARRAY_SIZE(decode->dpb); i++) { - if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_VALID)) + if (run->slot[i] == AVD_H264_NO_REF) continue; ref = avd_get_ref_buf(ctx, &dst->base.vb, dpb[i].reference_ts); @@ -135,7 +147,7 @@ static void stream_refs(struct avd_ctx *ctx, struct avd_h264_run *run) addr = vb2_dma_contig_plane_dma_addr(&ref->base.vb.vb2_buf, 0) + ref->comp.start_offset; - push(AVD_REF_NUM(run->num_valid - 1) | AVD_REF_FLAG_CONST | + push(AVD_REF_NUM(run->num_slots - 1) | AVD_REF_FLAG_CONST | AVD_REF_FLAG_LONG( dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) | @@ -436,13 +448,9 @@ static void stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) const struct v4l2_ctrl_h264_pps *pps = run->pps; const struct v4l2_ctrl_h264_sps *sps = run->sps; const struct v4l2_ctrl_h264_slice_params *sl = run->slice_params; - struct avd_h264_ctx *h264_ctx = ctx->priv; - u32 payload_len = h264_ctx->active_slice->size; bool en_mode = (pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE) == 0; - u32 off = run->slice_data_off; - - dma_addr_t slc_a84 = h264_ctx->active_slice->addr + off; + dma_addr_t slc_a84 = run->slice_addr; push(AVD_OP_CODED_DATA | AVD_OP_CODED_DATA_BIT_OFF( @@ -451,8 +459,7 @@ static void stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) "slc_a7c_cmd_set_coded_slice"); push((u32)(slc_a84 & 0xffffffff), "slc_a84_slice_addr_low"); - /* should not include trailing 0? */ - push(payload_len - off, "slc_a88_slice_hdr_size"); + push(run->slice_len, "slc_a88_slice_hdr_size"); push(AVD_OP_SL_LOC | AVD_OP_SL_LOC_Y(sl->first_mb_in_slice / @@ -481,7 +488,7 @@ static void stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) push(AVD_OP_REF | AVD_OP_REF_LIST_IDX(0) | AVD_OP_REF_LOOP_IDX(i) | AVD_OP_REF_DBP_IDX( - sl->ref_pic_list0[i].index), + run->slot[sl->ref_pic_list0[i].index]), "slc_6e8_cmd_ref_list_0"); if (sl->slice_type == V4L2_H264_SLICE_TYPE_B) { @@ -491,7 +498,7 @@ static void stream_slice(struct avd_ctx *ctx, struct avd_h264_run *run) push(AVD_OP_REF | AVD_OP_REF_LIST_IDX(1) | AVD_OP_REF_LOOP_IDX(i) | AVD_OP_REF_DBP_IDX( - sl->ref_pic_list1[i].index), + run->slot[sl->ref_pic_list1[i].index]), "slc_6e8_cmd_ref_list_0"); } stream_weights(ctx, run); @@ -613,9 +620,6 @@ static int avd_h264_validate_sps(struct avd_ctx *ctx, if (sps->chroma_format_idc == 2 && sps->bit_depth_luma_minus8 == 2) /* 4:2:2 10 bit would need P210; P010 is too small for its chroma */ return -EINVAL; - if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)) - /* no interlaced support */ - return -EINVAL; return 0; } @@ -769,11 +773,32 @@ static int avd_h264_realloc_slices(struct avd_ctx *ctx, size_t max_slices) return 0; } +/* + * The VP holds the reference pictures in slots, numbered in the order that + * stream_refs() pushes them, and the slice's reference lists address those + * slots. Give a slot to every DPB entry that is a reference picture (VALID + * and ACTIVE), and to nothing else: Chromium also lists the pictures that + * only wait for output, and pushing those as well left the reference count + * in the header short of the references pushed, which stalled the VP on + * some P pictures. + */ +static void avd_h264_assign_slots(struct avd_h264_run *run) +{ + const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb; + const u32 ref = V4L2_H264_DPB_ENTRY_FLAG_VALID | + V4L2_H264_DPB_ENTRY_FLAG_ACTIVE; + + run->num_slots = 0; + for (int i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) + run->slot[i] = (dpb[i].flags & ref) == ref ? run->num_slots++ : + AVD_H264_NO_REF; +} + /* * Check that every reference in the slice's ref lists points at a DPB entry - * that is VALID. stream_refs() only pushes a reference header (and its RVRA - * addresses) for VALID entries and the slice command indexes those by DPB - * index, so a reference to any other entry makes the VP dereference an + * with a VP slot. stream_refs() only pushes a reference header (and its RVRA + * addresses) for those entries, so a reference to any other entry makes the + * VP dereference an * uninitialised reference slot: that ends in a DART translation fault, a * frame that never completes and a watchdog reset of the VP, which also * disturbs the other contexts. It happens whenever a client starts decoding @@ -785,7 +810,6 @@ static int avd_h264_realloc_slices(struct avd_ctx *ctx, size_t max_slices) static bool avd_h264_refs_valid(const struct avd_h264_run *run) { const struct v4l2_ctrl_h264_slice_params *sl = run->slice_params; - const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb; const struct v4l2_h264_reference *lists[2] = { sl->ref_pic_list0, sl->ref_pic_list1 }; @@ -809,7 +833,7 @@ static bool avd_h264_refs_valid(const struct avd_h264_run *run) u8 idx = lists[l][i].index; if (idx >= V4L2_H264_NUM_DPB_ENTRIES || - !(dpb[idx].flags & V4L2_H264_DPB_ENTRY_FLAG_VALID)) + run->slot[idx] == AVD_H264_NO_REF) return false; } } @@ -853,22 +877,631 @@ VISIBLE_IF_KUNIT bool avd_h264_slice_fits(const struct v4l2_ctrl_h264_sps *sps, avd_h264_slice_budget(decoded_size); } +/* + * Frame-based decoding: the client queues every slice of a picture in one + * OUTPUT buffer and sets no slice parameters, so find the slice NAL units in + * the buffer and parse their headers here (ITU-T H.264 7.3.3). + */ + +/* Reads RBSP bits from a NAL unit, skipping emulation prevention bytes. */ +struct avd_h264_bits { + const u8 *data; + u32 len; + u32 pos; + u32 zeros; + u32 consumed; + u8 cur; + u8 left; + bool err; +}; + +static u32 avd_h264_bit(struct avd_h264_bits *br) +{ + if (!br->left) { + if (br->pos < br->len && br->zeros >= 2 && + br->data[br->pos] == 0x03) { + br->pos++; + br->zeros = 0; + } + if (br->pos >= br->len) { + br->err = true; + return 0; + } + br->cur = br->data[br->pos++]; + br->zeros = br->cur ? 0 : br->zeros + 1; + br->left = 8; + } + + br->left--; + br->consumed++; + return (br->cur >> br->left) & 1; +} + +static u32 avd_h264_bits(struct avd_h264_bits *br, unsigned int n) +{ + u32 v = 0; + + while (n--) + v = (v << 1) | avd_h264_bit(br); + return v; +} + +static u32 avd_h264_ue(struct avd_h264_bits *br) +{ + unsigned int lz = 0; + + while (!avd_h264_bit(br)) { + if (br->err || ++lz > 31) { + br->err = true; + return 0; + } + } + + return lz ? (u32)((1ULL << lz) - 1) + avd_h264_bits(br, lz) : 0; +} + +static s32 avd_h264_se(struct avd_h264_bits *br) +{ + u32 k = avd_h264_ue(br); + + return (k & 1) ? (s32)((k >> 1) + 1) : -(s32)(k >> 1); +} + +/* + * Find the next NAL unit at or after *pos. With Annex B start codes the NAL + * unit runs up to the next start code, without the trailing zero bytes that + * belong to a four-byte start code: a NAL unit never ends in a zero byte, and + * the AVD never completes a CAVLC picture whose slice data has such a tail. + */ +static bool avd_h264_next_nal(const u8 *data, u32 len, u32 *pos, u32 *nal_off, + u32 *nal_len) +{ + u32 i = *pos, start, end; + + for (; i + 3 <= len; i++) + if (!data[i] && !data[i + 1] && data[i + 2] == 1) + break; + if (i + 3 > len) + return false; + + start = i + 3; + for (i = start; i + 3 <= len; i++) + if (!data[i] && !data[i + 1] && data[i + 2] == 1) + break; + end = i + 3 <= len ? i : len; + *pos = end; + + while (end > start && data[end - 1] == 0) + end--; + *nal_off = start; + *nal_len = end - start; + return true; +} + +static bool avd_h264_is_slice_nal(const u8 *data, u32 len) +{ + u8 type = len ? data[0] & 0x1f : 0; + + return type == 1 || type == 5; +} + +/* PicNum (8-28) of a short-term reference, or LongTermPicNum (8-29). */ +static s32 avd_h264_pic_num(const struct avd_h264_run *run, unsigned int i) +{ + const struct v4l2_h264_dpb_entry *e = &run->decode_params->dpb[i]; + u32 max_frame_num = 1 << (run->sps->log2_max_frame_num_minus4 + 4); + + if (e->flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) + return e->pic_num; + if (e->frame_num > run->decode_params->frame_num) + return (s32)e->frame_num - max_frame_num; + return e->frame_num; +} + +static u8 avd_h264_find_ref(const struct avd_h264_run *run, s32 pic_num, + bool long_term) +{ + const struct v4l2_h264_dpb_entry *dpb = run->decode_params->dpb; + + for (unsigned int i = 0; i < V4L2_H264_NUM_DPB_ENTRIES; i++) { + if (!(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_ACTIVE) || + !(dpb[i].flags & V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM) != + !long_term) + continue; + if (avd_h264_pic_num(run, i) == pic_num) + return i; + } + + return AVD_H264_NO_REF; +} + +/* + * Build one reference picture list of a frame: the initial list (8.2.4.2) + * cut to the active size, then ref_pic_list_modification() applied as in + * 8.2.4.3. + */ +static void avd_h264_ref_list(struct avd_h264_bits *br, + const struct avd_h264_run *run, + const struct v4l2_h264_reference *init, + unsigned int num_init, + struct v4l2_h264_reference *list, + unsigned int num_active) +{ + struct v4l2_h264_reference tmp[V4L2_H264_REF_LIST_LEN + 1]; + u32 max_pic_num = 1 << (run->sps->log2_max_frame_num_minus4 + 4); + s32 cur_pic_num = run->decode_params->frame_num; + s32 pred = cur_pic_num; + unsigned int ref_idx = 0, ops = 0; + + for (unsigned int i = 0; i <= num_active; i++) { + if (i < num_init && i < num_active) { + tmp[i] = init[i]; + } else { + tmp[i].index = AVD_H264_NO_REF; + tmp[i].fields = V4L2_H264_FRAME_REF; + } + } + + if (avd_h264_bit(br)) { + for (;;) { + u32 idc = avd_h264_ue(br); + struct v4l2_h264_reference ref = { + .fields = V4L2_H264_FRAME_REF, + }; + unsigned int n; + + if (idc == 3 || br->err) + break; + if (idc > 3 || ++ops > num_active + 1) { + br->err = true; + break; + } + + if (idc < 2) { + u32 abs_diff = avd_h264_ue(br) + 1; + s32 pic_num; + + if (abs_diff > max_pic_num) { + br->err = true; + break; + } + if (idc == 0) { + pred -= abs_diff; + if (pred < 0) + pred += max_pic_num; + } else { + pred += abs_diff; + if (pred >= max_pic_num) + pred -= max_pic_num; + } + pic_num = pred > cur_pic_num ? pred - max_pic_num : pred; + ref.index = avd_h264_find_ref(run, pic_num, false); + } else { + ref.index = avd_h264_find_ref(run, avd_h264_ue(br), + true); + } + + /* insert at ref_idx, then drop the later duplicate */ + for (unsigned int c = num_active; c > ref_idx; c--) + tmp[c] = tmp[c - 1]; + tmp[ref_idx++] = ref; + n = ref_idx; + for (unsigned int c = ref_idx; c <= num_active; c++) + if (ref.index == AVD_H264_NO_REF || + tmp[c].index != ref.index) + tmp[n++] = tmp[c]; + } + } + + /* + * A conforming slice uses no entry without a picture; point any such + * entry at the first reference so that the VP is never handed an + * unset DPB slot. + */ + for (unsigned int i = 0; i < num_active; i++) { + list[i] = tmp[i]; + if (list[i].index == AVD_H264_NO_REF) + list[i].index = tmp[0].index; + } +} + +/* pred_weight_table(), 7.3.3.2, with the inferred default factors filled in */ +static void avd_h264_pred_weights(struct avd_h264_bits *br, + const struct avd_h264_run *run, + const struct v4l2_ctrl_h264_slice_params *sl, + struct v4l2_ctrl_h264_pred_weights *pw) +{ + const struct v4l2_ctrl_h264_sps *sps = run->sps; + bool chroma = sps->chroma_format_idc && + !(sps->flags & V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE); + int lists = sl->slice_type == V4L2_H264_SLICE_TYPE_B ? 2 : 1; + + pw->luma_log2_weight_denom = avd_h264_ue(br); + if (chroma) + pw->chroma_log2_weight_denom = avd_h264_ue(br); + if (pw->luma_log2_weight_denom > 7 || pw->chroma_log2_weight_denom > 7) { + br->err = true; + return; + } + + for (int l = 0; l < lists; l++) { + struct v4l2_h264_weight_factors *f = &pw->weight_factors[l]; + unsigned int n = (l ? sl->num_ref_idx_l1_active_minus1 : + sl->num_ref_idx_l0_active_minus1) + 1; + + for (unsigned int i = 0; i < n; i++) { + f->luma_weight[i] = 1 << pw->luma_log2_weight_denom; + f->luma_offset[i] = 0; + if (avd_h264_bit(br)) { + f->luma_weight[i] = avd_h264_se(br); + f->luma_offset[i] = avd_h264_se(br); + } + + for (int j = 0; j < 2; j++) { + f->chroma_weight[i][j] = + 1 << pw->chroma_log2_weight_denom; + f->chroma_offset[i][j] = 0; + } + if (chroma && avd_h264_bit(br)) { + for (int j = 0; j < 2; j++) { + f->chroma_weight[i][j] = avd_h264_se(br); + f->chroma_offset[i][j] = avd_h264_se(br); + } + } + } + } +} + +/* dec_ref_pic_marking(), 7.3.3.3: nothing in it is needed, only its size */ +static void avd_h264_skip_ref_pic_marking(struct avd_h264_bits *br, bool idr) +{ + unsigned int ops = 0; + + if (idr) { + avd_h264_bits(br, 2); + return; + } + + if (!avd_h264_bit(br)) + return; + + for (;;) { + u32 op = avd_h264_ue(br); + + if (!op || br->err) + return; + if (op > 6 || ++ops > 2 * V4L2_H264_NUM_DPB_ENTRIES + 2) { + br->err = true; + return; + } + if (op == 1 || op == 3) + avd_h264_ue(br); /* difference_of_pic_nums_minus1 */ + if (op == 2) + avd_h264_ue(br); /* long_term_pic_num */ + if (op == 3 || op == 6) + avd_h264_ue(br); /* long_term_frame_idx */ + if (op == 4) + avd_h264_ue(br); /* max_long_term_frame_idx_plus1 */ + } +} + +/* + * Parse the header of one slice NAL unit into the slice parameters and + * prediction weights the client would set in slice-based mode. Returns 1 for + * a redundant slice, which the decoder may drop. + */ +static int avd_h264_parse_slice(const struct avd_h264_run *run, + const struct avd_h264_reflists *init, + const u8 *nal, u32 len, + struct v4l2_ctrl_h264_slice_params *sl, + struct v4l2_ctrl_h264_pred_weights *pw) +{ + const struct v4l2_ctrl_h264_sps *sps = run->sps; + const struct v4l2_ctrl_h264_pps *pps = run->pps; + u32 mbs = (sps->pic_width_in_mbs_minus1 + 1) * + (sps->pic_height_in_map_units_minus1 + 1); + struct avd_h264_bits br = { .data = nal, .len = len }; + u32 nal_ref_idc, slice_type, num_l0, num_l1, v; + bool idr, field_pic = false; + s32 qp_delta, alpha = 0, beta = 0; + + memset(sl, 0, sizeof(*sl)); + memset(pw, 0, sizeof(*pw)); + + avd_h264_bit(&br); /* forbidden_zero_bit */ + nal_ref_idc = avd_h264_bits(&br, 2); + idr = avd_h264_bits(&br, 5) == 5; + + sl->first_mb_in_slice = avd_h264_ue(&br); + slice_type = avd_h264_ue(&br); + if (sl->first_mb_in_slice >= mbs || slice_type > 9) + return -EINVAL; + sl->slice_type = slice_type % 5; + if (sl->slice_type == V4L2_H264_SLICE_TYPE_SP || + sl->slice_type == V4L2_H264_SLICE_TYPE_SI) + return -EOPNOTSUPP; + if (idr && sl->slice_type != V4L2_H264_SLICE_TYPE_I) + return -EINVAL; + + avd_h264_ue(&br); /* pic_parameter_set_id */ + if (sps->flags & V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE) + sl->colour_plane_id = avd_h264_bits(&br, 2); + avd_h264_bits(&br, sps->log2_max_frame_num_minus4 + 4); + if (!(sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)) { + field_pic = avd_h264_bit(&br); + if (field_pic) + return -EOPNOTSUPP; + } + if (idr) + avd_h264_ue(&br); /* idr_pic_id */ + + if (sps->pic_order_cnt_type == 0) { + avd_h264_bits(&br, sps->log2_max_pic_order_cnt_lsb_minus4 + 4); + if ((pps->flags & + V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT) && + !field_pic) + avd_h264_se(&br); /* delta_pic_order_cnt_bottom */ + } else if (sps->pic_order_cnt_type == 1 && + !(sps->flags & V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO)) { + avd_h264_se(&br); /* delta_pic_order_cnt[0] */ + if ((pps->flags & + V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT) && + !field_pic) + avd_h264_se(&br); /* delta_pic_order_cnt[1] */ + } + + if (pps->flags & V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT) { + v = avd_h264_ue(&br); + if (v > 127) + return -EINVAL; + sl->redundant_pic_cnt = v; + } + + if (sl->slice_type == V4L2_H264_SLICE_TYPE_B && avd_h264_bit(&br)) + sl->flags |= V4L2_H264_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED; + + if (sl->slice_type != V4L2_H264_SLICE_TYPE_I) { + num_l0 = pps->num_ref_idx_l0_default_active_minus1; + num_l1 = pps->num_ref_idx_l1_default_active_minus1; + if (avd_h264_bit(&br)) { /* num_ref_idx_active_override */ + num_l0 = avd_h264_ue(&br); + if (sl->slice_type == V4L2_H264_SLICE_TYPE_B) + num_l1 = avd_h264_ue(&br); + } + if (sl->slice_type != V4L2_H264_SLICE_TYPE_B) + num_l1 = 0; + /* the VP takes 16 entries a list, all a frame may use */ + if (num_l0 > 15 || num_l1 > 15) + return -EINVAL; + sl->num_ref_idx_l0_active_minus1 = num_l0; + sl->num_ref_idx_l1_active_minus1 = num_l1; + + if (sl->slice_type == V4L2_H264_SLICE_TYPE_P) { + avd_h264_ref_list(&br, run, init->p, run->num_valid, + sl->ref_pic_list0, num_l0 + 1); + } else { + avd_h264_ref_list(&br, run, init->b0, run->num_valid, + sl->ref_pic_list0, num_l0 + 1); + avd_h264_ref_list(&br, run, init->b1, run->num_valid, + sl->ref_pic_list1, num_l1 + 1); + } + + if (V4L2_H264_CTRL_PRED_WEIGHTS_REQUIRED(pps, sl)) + avd_h264_pred_weights(&br, run, sl, pw); + } + + if (nal_ref_idc) + avd_h264_skip_ref_pic_marking(&br, idr); + + if ((pps->flags & V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE) && + sl->slice_type != V4L2_H264_SLICE_TYPE_I) { + v = avd_h264_ue(&br); + if (v > 2) + return -EINVAL; + sl->cabac_init_idc = v; + } + + qp_delta = avd_h264_se(&br); + if (qp_delta < -87 || qp_delta > 77) + return -EINVAL; + sl->slice_qp_delta = qp_delta; + + if (pps->flags & V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT) { + v = avd_h264_ue(&br); + if (v > 2) + return -EINVAL; + sl->disable_deblocking_filter_idc = v; + if (v != 1) { + alpha = avd_h264_se(&br); + beta = avd_h264_se(&br); + if (alpha < -6 || alpha > 6 || beta < -6 || beta > 6) + return -EINVAL; + } + } + sl->slice_alpha_c0_offset_div2 = alpha; + sl->slice_beta_offset_div2 = beta; + + if (br.err) + return -EINVAL; + + sl->header_bit_size = br.consumed; + return sl->redundant_pic_cnt ? 1 : 0; +} + +static int avd_h264_run_frame(struct avd_ctx *ctx, struct avd_h264_run *run, + const u8 *data, u32 len) +{ + struct avd_h264_ctx *h264_ctx = ctx->priv; + struct v4l2_ctrl_h264_slice_params *sl = &h264_ctx->fb_slice; + u32 pos = 0, nal_off, nal_len, off; + size_t num = 0; + bool first = true, starts_picture; + int ret; + + while (avd_h264_next_nal(data, len, &pos, &nal_off, &nal_len)) + if (avd_h264_is_slice_nal(data + nal_off, nal_len)) + num++; + + if (!num || num > avd_h264_max_slices(run->sps)) { + dev_dbg_ratelimited(ctx->dev->dev, + "frame has %zu slices, dropping it\n", num); + return -EINVAL; + } + + /* the header segment plus one per slice */ + ret = avd_init_job(ctx, AVD_CODEC_H264, num + 1); + if (ret) + return ret; + + run->slice_params = sl; + run->pred_weights = &h264_ctx->fb_weights; + + pos = 0; + while (avd_h264_next_nal(data, len, &pos, &nal_off, &nal_len)) { + if (!avd_h264_is_slice_nal(data + nal_off, nal_len)) + continue; + + ret = avd_h264_parse_slice(run, &h264_ctx->reflists, + data + nal_off, nal_len, sl, + &h264_ctx->fb_weights); + if (ret == 1) + continue; + if (ret) + goto err_drop; + + /* + * The picture starts at its first macroblock: the dimensions + * go with that slice, and only with it. + */ + starts_picture = !sl->first_mb_in_slice; + if (first != starts_picture) { + ret = -EINVAL; + goto err_drop; + } + + if (!avd_h264_refs_valid(run) || + !avd_h264_find_slice_data(data + nal_off, nal_len, + avd_h264_header_len(run), &off)) { + ret = -EINVAL; + goto err_drop; + } + + /* The OUTPUT buffer stays queued until the picture completes. */ + run->slice_addr = run->base.coded_in + nal_off + off; + run->slice_len = nal_len - off; + + if (first) + stream_hdr(ctx, run); + first = false; + + ctx->job.num++; + stream_slice(ctx, run); + } + + if (first) { + ret = -EINVAL; + goto err_drop; + } + + return avd_submit_job(ctx); + +err_drop: + dev_dbg_ratelimited(ctx->dev->dev, "bad slice in frame: %d\n", ret); + kvfree(ctx->job.segments); + ctx->job.segments = NULL; + return ret; +} + static int avd_h264_run(struct avd_ctx *ctx) { struct avd_h264_ctx *h264_ctx = ctx->priv; struct v4l2_h264_reflist_builder reflist_builder; struct avd_h264_run run; + struct v4l2_ctrl *ctrl; + bool frame_based, annexb; + u32 nal_off, nal_len, pos = 0; int ret; avd_h264_run_preamble(ctx, &run); + /* + * No interlaced support. This is checked per picture rather than when + * the SPS is set: Chromium sets a placeholder SPS without + * frame_mbs_only_flag to learn the decoded formats before it has + * parsed the stream, and the stream's own SPS comes later. + */ + if (!(run.sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY)) { + dev_dbg_ratelimited(ctx->dev->dev, + "interlaced H.264 is not supported\n"); + avd_run_postamble(ctx, &run.base); + return -EINVAL; + } + + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_DECODE_MODE); + frame_based = ctrl && ctrl->cur.val == + V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED; + ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_H264_START_CODE); + annexb = ctrl && ctrl->cur.val == V4L2_STATELESS_H264_START_CODE_ANNEX_B; + struct vb2_v4l2_buffer *src = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx); u32 payload_len = vb2_get_plane_payload(&src->vb2_buf, 0); const u8 *data = vb2_plane_vaddr(&src->vb2_buf, 0); - if (!data || !avd_h264_find_slice_data(data, payload_len, + /* + * A client may send Annex B start codes and leave the start code + * control at its default (Chromium does). A NAL unit never begins + * with a zero byte, so a leading start code is unambiguous. + */ + if (data && payload_len >= 3 && !data[0] && !data[1] && + (data[2] == 1 || (payload_len >= 4 && !data[2] && data[3] == 1))) + annexb = true; + + if (frame_based) { + avd_run_postamble(ctx, &run.base); + if (!data) + return -EINVAL; + /* + * Without start codes nothing marks where one slice ends and + * the next begins, so a multi-slice picture cannot be split. + */ + if (!annexb) { + dev_dbg_ratelimited(ctx->dev->dev, + "frame-based decoding needs Annex B start codes\n"); + return -EINVAL; + } + + h264_ctx->monochrome = run.sps->chroma_format_idc == 0; + + /* Build the P/B{0,1} ref lists. */ + v4l2_h264_init_reflist_builder(&reflist_builder, + run.decode_params, run.sps, + run.decode_params->dpb); + run.num_valid = reflist_builder.num_valid; + run.cur_poc = reflist_builder.cur_pic_order_count; + avd_h264_assign_slots(&run); + v4l2_h264_build_p_ref_list(&reflist_builder, + h264_ctx->reflists.p); + v4l2_h264_build_b_ref_lists(&reflist_builder, + h264_ctx->reflists.b0, + h264_ctx->reflists.b1); + + return avd_h264_run_frame(ctx, &run, data, payload_len); + } + + /* A slice with a start code: skip to its NAL unit. */ + if (data && annexb) { + if (!avd_h264_next_nal(data, payload_len, &pos, &nal_off, &nal_len)) { + avd_run_postamble(ctx, &run.base); + return -EINVAL; + } + data += nal_off; + payload_len = nal_len; + } + + if (!run.slice_params || + !data || !avd_h264_find_slice_data(data, payload_len, avd_h264_header_len(&run), - &run.slice_data_off)) { + &nal_off)) { dev_dbg_ratelimited(ctx->dev->dev, "slice has no data after its header\n"); avd_run_postamble(ctx, &run.base); @@ -900,6 +1533,8 @@ static int avd_h264_run(struct avd_ctx *ctx) h264_ctx->slice_num++; h264_ctx->slice_bytes += PAGE_ALIGN((size_t)payload_len); h264_ctx->monochrome = run.sps->chroma_format_idc == 0; + run.slice_addr = h264_ctx->active_slice->addr + nal_off; + run.slice_len = payload_len - nal_off; /* Build the P/B{0,1} ref lists. */ v4l2_h264_init_reflist_builder(&reflist_builder, run.decode_params, @@ -907,6 +1542,7 @@ static int avd_h264_run(struct avd_ctx *ctx) run.num_valid = reflist_builder.num_valid; run.cur_poc = reflist_builder.cur_pic_order_count; + avd_h264_assign_slots(&run); v4l2_h264_build_p_ref_list(&reflist_builder, h264_ctx->reflists.p); v4l2_h264_build_b_ref_lists(&reflist_builder, h264_ctx->reflists.b0, diff --git a/drivers/media/platform/apple/avd/avd-hevc-parse-test.c b/drivers/media/platform/apple/avd/avd-hevc-parse-test.c new file mode 100644 index 00000000000000..bb7a60ed311ff5 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-hevc-parse-test.c @@ -0,0 +1,508 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * KUnit tests for the HEVC slice header parser of the Apple video decoder. + * + * The streams are built here bit by bit from the slice segment header syntax + * (H.265 7.3.6.1); the expected results are worked out by hand from that + * syntax, not from the parser. + */ + +#include +#include + +#include "avd-hevc-parse.h" + +#define HEVC_NAL_TRAIL_R 1 + +struct bitwriter { + u8 rbsp[256]; + size_t bits; +}; + +struct stream { + u8 buf[2048]; + size_t len; +}; + +static void put_bits(struct bitwriter *bw, u64 val, int n) +{ + while (n--) { + if ((val >> n) & 1) + bw->rbsp[bw->bits / 8] |= 0x80 >> (bw->bits % 8); + bw->bits++; + } +} + +static void put_ue(struct bitwriter *bw, u32 val) +{ + u64 x = (u64)val + 1; + int len = fls64(x) - 1; + + put_bits(bw, 0, len); + put_bits(bw, x, len + 1); +} + +/* rbsp_slice_segment_trailing_bits: a 1, then zeros to the byte boundary. */ +static void put_trailing_bits(struct bitwriter *bw) +{ + put_bits(bw, 1, 1); + while (bw->bits % 8) + put_bits(bw, 0, 1); +} + +/* Append a start code, a NAL unit header and the escaped RBSP, then data. */ +static void put_nal(struct stream *s, u8 type, const struct bitwriter *bw, + size_t data_len) +{ + int zeros = 0; + + s->buf[s->len++] = 0; + s->buf[s->len++] = 0; + s->buf[s->len++] = 1; + s->buf[s->len++] = type << 1; + s->buf[s->len++] = 1; /* nuh_temporal_id_plus1 */ + for (size_t i = 0; i < bw->bits / 8; i++) { + if (zeros >= 2 && bw->rbsp[i] <= 3) { + s->buf[s->len++] = 3; + zeros = 0; + } + s->buf[s->len++] = bw->rbsp[i]; + zeros = bw->rbsp[i] ? 0 : zeros + 1; + } + memset(s->buf + s->len, 0xaa, data_len); + s->len += data_len; +} + +/* + * 256x256, 64x64 CTBs: 16 CTBs, so slice_segment_address takes 4 bits. + * One short-term RPS in the SPS, so its index takes no bits. WPP on, so + * every slice carries entry points. + */ +static void test_params(struct v4l2_ctrl_hevc_sps *sps, struct v4l2_ctrl_hevc_pps *pps, + struct v4l2_ctrl_hevc_decode_params *dec) +{ + *sps = (struct v4l2_ctrl_hevc_sps){ + .pic_width_in_luma_samples = 256, + .pic_height_in_luma_samples = 256, + .chroma_format_idc = 1, + .log2_diff_max_min_luma_coding_block_size = 3, + .num_short_term_ref_pic_sets = 1, + }; + *pps = (struct v4l2_ctrl_hevc_pps){ + .flags = V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED, + }; + *dec = (struct v4l2_ctrl_hevc_decode_params){}; +} + +/* + * An I slice segment header of a TRAIL_R picture for test_params(), with + * @num_eps entry points of @offset_len bits whose entry_point_offset_minus1 + * values are @offsets (only the first @num_offsets are written). + */ +static void put_i_slice(struct bitwriter *bw, bool first, u32 addr, u32 num_eps, + int offset_len, const u32 *offsets, int num_offsets) +{ + memset(bw, 0, sizeof(*bw)); + put_bits(bw, first, 1); /* first_slice_segment_in_pic_flag */ + put_ue(bw, 0); /* slice_pic_parameter_set_id */ + if (!first) + put_bits(bw, addr, 4); /* slice_segment_address */ + put_ue(bw, 2); /* slice_type: I */ + put_bits(bw, 0, 4); /* slice_pic_order_cnt_lsb */ + put_bits(bw, 1, 1); /* short_term_ref_pic_set_sps_flag */ + put_ue(bw, 1); /* slice_qp_delta = 0, as se(v) */ + put_ue(bw, num_eps); /* num_entry_point_offsets */ + if (num_eps) { + put_ue(bw, offset_len - 1); + for (int i = 0; i < num_offsets; i++) + put_bits(bw, offsets[i], offset_len); + } + put_trailing_bits(bw); +} + +static void put_se(struct bitwriter *bw, s32 val) +{ + put_ue(bw, val > 0 ? 2 * val - 1 : -2 * val); +} + +struct p_slice { + u32 num_ref_idx_minus1; + bool temporal_mvp; /* needs the SPS temporal MVP flag */ + u32 collocated_ref_idx; + bool weights; /* needs the PPS weighted pred flag */ + u32 luma_log2_weight_denom; + s32 delta_chroma_log2_weight_denom; +}; + +/* + * The first P slice segment of a TRAIL_R picture for test_params(), with + * the reference count overridden; without entry points' data offsets + * (num_entry_point_offsets = 0). + */ +static void put_p_slice(struct bitwriter *bw, const struct p_slice *p) +{ + memset(bw, 0, sizeof(*bw)); + put_bits(bw, 1, 1); /* first_slice_segment_in_pic_flag */ + put_ue(bw, 0); /* slice_pic_parameter_set_id */ + put_ue(bw, 1); /* slice_type: P */ + put_bits(bw, 0, 4); /* slice_pic_order_cnt_lsb */ + put_bits(bw, 1, 1); /* short_term_ref_pic_set_sps_flag */ + if (p->temporal_mvp) + put_bits(bw, 1, 1); /* slice_temporal_mvp_enabled_flag */ + put_bits(bw, 1, 1); /* num_ref_idx_active_override_flag */ + put_ue(bw, p->num_ref_idx_minus1); + if (p->temporal_mvp && p->num_ref_idx_minus1) + put_ue(bw, p->collocated_ref_idx); + if (p->weights) { + put_ue(bw, p->luma_log2_weight_denom); + put_se(bw, p->delta_chroma_log2_weight_denom); + /* no luma or chroma weights for any entry */ + put_bits(bw, 0, 2 * (p->num_ref_idx_minus1 + 1)); + } + put_ue(bw, 0); /* five_minus_max_num_merge_cand */ + put_se(bw, 0); /* slice_qp_delta */ + put_ue(bw, 0); /* num_entry_point_offsets */ + put_trailing_bits(bw); +} + +/* Two short-term references before the current picture: DPB 0 and 1. */ +static void two_refs(struct v4l2_ctrl_hevc_decode_params *dec) +{ + dec->num_active_dpb_entries = 2; + dec->num_poc_st_curr_before = 2; + dec->poc_st_curr_before[0] = 0; + dec->poc_st_curr_before[1] = 1; +} + +struct parse_result { + int ret; + struct v4l2_ctrl_hevc_slice_params *slices; + int num_slices; + u32 *eps; + int num_eps; +}; + +static void parse(struct kunit *test, const struct stream *s, + const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *dec, + struct parse_result *r) +{ + *r = (struct parse_result){}; + r->ret = avd_hevc_parse_slices(s->buf, s->len, sps, pps, dec, &r->slices, + &r->num_slices, &r->eps, &r->num_eps); + if (!r->ret) { + kunit_add_action(test, (kunit_action_t *)kvfree, r->slices); + kunit_add_action(test, (kunit_action_t *)kvfree, r->eps); + } +} + +/* + * Two I slices of 2 entry points each. By hand: the first header is + * 37 bits plus trailing bits, 5 bytes, so its data starts 3 + 2 + 5 bytes + * after the buffer start; the second header (with a 4-bit address) is + * 6 bytes, its data 3 + 2 + 6 bytes after its own start code. Each slice + * NAL unit carries 64 data bytes. + */ +static void avd_hevc_parse_two_slices(struct kunit *test) +{ + static const u32 offsets[] = { 15, 31 }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + put_i_slice(&bw, true, 0, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + put_i_slice(&bw, false, 8, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_ASSERT_EQ(test, r.ret, 0); + KUNIT_ASSERT_EQ(test, r.num_slices, 2); + KUNIT_ASSERT_EQ(test, r.num_eps, 4); + KUNIT_EXPECT_EQ(test, r.eps[0], 16); + KUNIT_EXPECT_EQ(test, r.eps[1], 32); + KUNIT_EXPECT_EQ(test, r.eps[2], 16); + KUNIT_EXPECT_EQ(test, r.eps[3], 32); + KUNIT_EXPECT_EQ(test, r.slices[0].slice_type, V4L2_HEVC_SLICE_TYPE_I); + KUNIT_EXPECT_EQ(test, r.slices[0].num_entry_point_offsets, 2); + KUNIT_EXPECT_EQ(test, r.slices[0].data_byte_offset, 10); + KUNIT_EXPECT_EQ(test, r.slices[0].bit_size, 74 * 8); + KUNIT_EXPECT_EQ(test, r.slices[1].slice_segment_addr, 8); + KUNIT_EXPECT_EQ(test, r.slices[1].data_byte_offset, 11); + KUNIT_EXPECT_EQ(test, r.slices[1].bit_size, 75 * 8); +} + +/* + * A later slice whose entry point count wraps the table bound when added + * to the earlier slices' count (3 + 0xfffffffd == 0 in 32 bits). + */ +static void avd_hevc_parse_rejects_wrapping_entry_point_count(struct kunit *test) +{ + static const u32 offsets[] = { 15, 15, 15 }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + put_i_slice(&bw, true, 0, 3, 8, offsets, 3); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + put_i_slice(&bw, false, 8, 0xfffffffd, 8, offsets, 3); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* + * Entry points of 16 + 32 bytes, so the last substream starts 48 bytes into + * the slice data, but the slice carries only 40 data bytes. + */ +static void avd_hevc_parse_rejects_entry_points_past_slice_data(struct kunit *test) +{ + static const u32 offsets[] = { 15, 31 }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + put_i_slice(&bw, true, 0, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 40); + put_i_slice(&bw, false, 8, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* + * 192x256 has 3 x 4 = 12 CTBs; slice_segment_address still takes 4 bits, + * so it can name CTB 12, which does not exist. + */ +static void avd_hevc_parse_rejects_slice_address_past_picture(struct kunit *test) +{ + static const u32 offsets[] = { 15, 31 }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + sps.pic_width_in_luma_samples = 192; + put_i_slice(&bw, true, 0, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + put_i_slice(&bw, false, 12, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* Two active references, so collocated_ref_idx may be 0 or 1, not 2. */ +static void avd_hevc_parse_rejects_collocated_ref_past_list(struct kunit *test) +{ + const struct p_slice p = { + .num_ref_idx_minus1 = 1, + .temporal_mvp = true, + .collocated_ref_idx = 2, + }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + sps.flags |= V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED; + two_refs(&dec); + put_p_slice(&bw, &p); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* ChromaLog2WeightDenom = 7 + 1 = 8, outside 0..7 (7.4.7.3). */ +static void avd_hevc_parse_rejects_chroma_weight_denom_past_range(struct kunit *test) +{ + const struct p_slice p = { + .weights = true, + .luma_log2_weight_denom = 7, + .delta_chroma_log2_weight_denom = 1, + }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + pps.flags |= V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED; + two_refs(&dec); + put_p_slice(&bw, &p); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* poc_st_curr_before[] has 16 entries; a count of 17 reads past it. */ +static void avd_hevc_parse_rejects_reference_count_past_dpb(struct kunit *test) +{ + const struct p_slice p = {}; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + dec.num_poc_st_curr_before = V4L2_HEVC_DPB_ENTRIES_NUM_MAX + 1; + put_p_slice(&bw, &p); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* Two active DPB entries; a reference to DPB entry 5 names no picture. */ +static void avd_hevc_parse_rejects_reference_past_active_dpb(struct kunit *test) +{ + const struct p_slice p = { .num_ref_idx_minus1 = 1 }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + two_refs(&dec); + dec.num_active_dpb_entries = 2; + dec.poc_st_curr_before[1] = 5; + put_p_slice(&bw, &p); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +/* + * A valid P slice using everything the rejection tests above bend: two + * active references, a collocated picture, and weights at the denominators' + * upper bound. By 8.3.4, RefPicList0 = PocStCurrBefore = { 0, 1 }. + */ +static void avd_hevc_parse_p_slice(struct kunit *test) +{ + const struct p_slice p = { + .num_ref_idx_minus1 = 1, + .temporal_mvp = true, + .collocated_ref_idx = 1, + .weights = true, + .luma_log2_weight_denom = 7, + .delta_chroma_log2_weight_denom = 0, + }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + sps.flags |= V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED; + pps.flags |= V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED; + two_refs(&dec); + put_p_slice(&bw, &p); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_ASSERT_EQ(test, r.ret, 0); + KUNIT_ASSERT_EQ(test, r.num_slices, 1); + KUNIT_EXPECT_EQ(test, r.slices[0].slice_type, V4L2_HEVC_SLICE_TYPE_P); + KUNIT_EXPECT_EQ(test, r.slices[0].num_ref_idx_l0_active_minus1, 1); + KUNIT_EXPECT_EQ(test, r.slices[0].ref_idx_l0[0], 0); + KUNIT_EXPECT_EQ(test, r.slices[0].ref_idx_l0[1], 1); + KUNIT_EXPECT_EQ(test, r.slices[0].collocated_ref_idx, 1); + KUNIT_EXPECT_EQ(test, r.slices[0].pred_weight_table.luma_log2_weight_denom, 7); +} + +/* + * 4:2:2 is a Range Extensions format; its slice headers may carry fields + * (cu_chroma_qp_offset_enabled_flag) gated on PPS range extension flags + * that the V4L2 PPS does not carry, so the parser cannot read them. + */ +static void avd_hevc_parse_rejects_range_extensions_format(struct kunit *test) +{ + static const u32 offsets[] = { 15, 31 }; + struct v4l2_ctrl_hevc_decode_params dec; + struct v4l2_ctrl_hevc_sps sps; + struct v4l2_ctrl_hevc_pps pps; + struct parse_result r; + struct bitwriter bw; + struct stream *s; + + s = kunit_kzalloc(test, sizeof(*s), GFP_KERNEL); + KUNIT_ASSERT_NOT_NULL(test, s); + test_params(&sps, &pps, &dec); + sps.chroma_format_idc = 2; + put_i_slice(&bw, true, 0, 2, 8, offsets, 2); + put_nal(s, HEVC_NAL_TRAIL_R, &bw, 64); + + parse(test, s, &sps, &pps, &dec, &r); + KUNIT_EXPECT_EQ(test, r.ret, -EINVAL); +} + +static struct kunit_case avd_hevc_parse_cases[] = { + KUNIT_CASE(avd_hevc_parse_p_slice), + KUNIT_CASE(avd_hevc_parse_rejects_range_extensions_format), + KUNIT_CASE(avd_hevc_parse_rejects_reference_past_active_dpb), + KUNIT_CASE(avd_hevc_parse_rejects_reference_count_past_dpb), + KUNIT_CASE(avd_hevc_parse_rejects_chroma_weight_denom_past_range), + KUNIT_CASE(avd_hevc_parse_rejects_collocated_ref_past_list), + KUNIT_CASE(avd_hevc_parse_rejects_slice_address_past_picture), + KUNIT_CASE(avd_hevc_parse_two_slices), + KUNIT_CASE(avd_hevc_parse_rejects_wrapping_entry_point_count), + KUNIT_CASE(avd_hevc_parse_rejects_entry_points_past_slice_data), + {} +}; + +static struct kunit_suite avd_hevc_parse_suite = { + .name = "apple-avd-hevc-parse", + .test_cases = avd_hevc_parse_cases, +}; + +kunit_test_suite(avd_hevc_parse_suite); diff --git a/drivers/media/platform/apple/avd/avd-hevc-parse.c b/drivers/media/platform/apple/avd/avd-hevc-parse.c new file mode 100644 index 00000000000000..288a3ae1dcd9f5 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-hevc-parse.c @@ -0,0 +1,587 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* + * HEVC slice segment headers parsed from the bitstream, for clients that + * queue a whole picture in Annex B form and set no slice parameters + * (Chromium's V4L2 stateless decoder does this: it sends SPS, PPS, scaling + * matrix and decode parameters only, and prefixes each slice NAL unit with a + * 3-byte start code). + * + * The parser fills the same struct v4l2_ctrl_hevc_slice_params array and + * entry point table a slice-parameter client would, so the rest of the + * driver is unchanged. Byte offsets and sizes are in the escaped (raw) + * bitstream, counted from the slice's start code, which keeps the driver's + * "slices follow each other in the buffer" walk valid. + */ + +#include +#include +#include +#include + +#include "avd-hevc-parse.h" + +/* RBSP bit reader over an escaped NAL unit. */ +struct avd_br { + const u8 *buf; + size_t len; + size_t pos; /* next raw byte to load */ + u32 cur; /* loaded byte */ + int bits; /* bits left in cur */ + int zeros; /* consecutive 0x00 raw bytes just loaded */ + bool err; +}; + +static void br_init(struct avd_br *br, const u8 *buf, size_t len) +{ + *br = (struct avd_br){ .buf = buf, .len = len }; +} + +static bool br_load(struct avd_br *br) +{ + if (br->pos >= br->len) { + br->err = true; + return false; + } + /* 00 00 03: the 03 is an emulation prevention byte. */ + if (br->zeros >= 2 && br->buf[br->pos] == 0x03) { + br->pos++; + br->zeros = 0; + if (br->pos >= br->len) { + br->err = true; + return false; + } + } + br->cur = br->buf[br->pos++]; + br->zeros = br->cur ? 0 : br->zeros + 1; + br->bits = 8; + return true; +} + +static u32 br_u(struct avd_br *br, int n) +{ + u32 v = 0; + + while (n-- > 0) { + if (!br->bits && !br_load(br)) + return 0; + v = (v << 1) | ((br->cur >> --br->bits) & 1); + } + return v; +} + +static void br_skip(struct avd_br *br, u32 n) +{ + while (n--) { + br_u(br, 1); + if (br->err) + return; + } +} + +static u32 br_ue(struct avd_br *br) +{ + int lz = 0; + + while (!br_u(br, 1)) { + if (br->err || ++lz > 31) { + br->err = true; + return 0; + } + } + return ((1u << lz) - 1) + br_u(br, lz); +} + +static s32 br_se(struct avd_br *br) +{ + u32 k = br_ue(br); + + return (k & 1) ? (s32)((k + 1) / 2) : -(s32)(k / 2); +} + +/* Raw offset of the next unread bit, which must be byte aligned. */ +static size_t br_raw_pos(struct avd_br *br) +{ + return br->pos; +} + +static int ceil_log2(u32 v) +{ + return v > 1 ? order_base_2(v) : 0; +} + +#define HEVC_NAL_BLA_W_LP 16 +#define HEVC_NAL_IDR_W_RADL 19 +#define HEVC_NAL_IDR_N_LP 20 +#define HEVC_NAL_RSV_IRAP_23 23 + +static bool is_slice_nal(u8 type) +{ + return type <= 9 || (type >= 16 && type <= 21); +} + +static int parse_weights(struct avd_br *br, struct v4l2_ctrl_hevc_slice_params *sl, + int chroma_array_type) +{ + struct v4l2_hevc_pred_weight_table *pwt = &sl->pred_weight_table; + int lists = sl->slice_type == V4L2_HEVC_SLICE_TYPE_B ? 2 : 1; + int chroma_denom; + + pwt->luma_log2_weight_denom = br_ue(br); + chroma_denom = pwt->luma_log2_weight_denom; + if (chroma_array_type) { + pwt->delta_chroma_log2_weight_denom = br_se(br); + chroma_denom += pwt->delta_chroma_log2_weight_denom; + } + /* Both denominators are 0..7 (7.4.7.3). */ + if (pwt->luma_log2_weight_denom > 7 || chroma_denom < 0 || chroma_denom > 7) + return -EINVAL; + + for (int l = 0; l < lists; l++) { + int n = (l ? sl->num_ref_idx_l1_active_minus1 : + sl->num_ref_idx_l0_active_minus1) + 1; + s8 *dlw = l ? pwt->delta_luma_weight_l1 : pwt->delta_luma_weight_l0; + s8 *lo = l ? pwt->luma_offset_l1 : pwt->luma_offset_l0; + s8(*dcw)[2] = l ? pwt->delta_chroma_weight_l1 : pwt->delta_chroma_weight_l0; + s8(*co)[2] = l ? pwt->chroma_offset_l1 : pwt->chroma_offset_l0; + bool luma_flag[V4L2_HEVC_DPB_ENTRIES_NUM_MAX] = {}; + bool chroma_flag[V4L2_HEVC_DPB_ENTRIES_NUM_MAX] = {}; + + for (int i = 0; i < n; i++) + luma_flag[i] = br_u(br, 1); + if (chroma_array_type) + for (int i = 0; i < n; i++) + chroma_flag[i] = br_u(br, 1); + + for (int i = 0; i < n; i++) { + if (luma_flag[i]) { + dlw[i] = br_se(br); + lo[i] = br_se(br); + } + if (!chroma_flag[i]) + continue; + for (int j = 0; j < 2; j++) { + /* 7.4.7.3, without high precision offsets */ + const int half = 1 << 7; + int dw = br_se(br); + int doff = br_se(br); + int w = (1 << chroma_denom) + dw; + + dcw[i][j] = dw; + co[i][j] = clamp(half + doff - ((half * w) >> chroma_denom), + -half, half - 1); + } + } + } + return 0; +} + +/* 8.3.4: reference picture lists, as DPB indices. */ +static void build_ref_lists(struct v4l2_ctrl_hevc_slice_params *sl, + const struct v4l2_ctrl_hevc_decode_params *dec, + const u8 *entry_l0, bool mod_l0, + const u8 *entry_l1, bool mod_l1) +{ + int total = dec->num_poc_st_curr_before + dec->num_poc_st_curr_after + + dec->num_poc_lt_curr; + u8 temp[2 * V4L2_HEVC_DPB_ENTRIES_NUM_MAX]; + + if (!total) + return; + + for (int l = 0; l < 2; l++) { + int n = (l ? sl->num_ref_idx_l1_active_minus1 : + sl->num_ref_idx_l0_active_minus1) + 1; + int num_temp = max(n, total), r = 0; + const u8 *first = l ? dec->poc_st_curr_after : dec->poc_st_curr_before; + const u8 *second = l ? dec->poc_st_curr_before : dec->poc_st_curr_after; + int nfirst = l ? dec->num_poc_st_curr_after : dec->num_poc_st_curr_before; + int nsecond = l ? dec->num_poc_st_curr_before : dec->num_poc_st_curr_after; + u8 *out = l ? sl->ref_idx_l1 : sl->ref_idx_l0; + const u8 *entry = l ? entry_l1 : entry_l0; + bool mod = l ? mod_l1 : mod_l0; + + if (l && sl->slice_type != V4L2_HEVC_SLICE_TYPE_B) + break; + num_temp = min_t(int, num_temp, ARRAY_SIZE(temp)); + while (r < num_temp) { + for (int i = 0; i < nfirst && r < num_temp; i++) + temp[r++] = first[i]; + for (int i = 0; i < nsecond && r < num_temp; i++) + temp[r++] = second[i]; + for (int i = 0; i < dec->num_poc_lt_curr && r < num_temp; i++) + temp[r++] = dec->poc_lt_curr[i]; + } + for (int i = 0; i < n && i < V4L2_HEVC_DPB_ENTRIES_NUM_MAX; i++) + out[i] = temp[mod ? min_t(int, entry[i], num_temp - 1) : i]; + } +} + +/* + * Parse one slice segment header (7.3.6.1). @nal points at the NAL unit + * header, @start_off is the raw offset of the NAL unit from the slice's + * start code. @prev is the previous independent slice segment of the + * picture, for dependent slice segments. + */ +static int parse_slice(const u8 *nal, size_t nal_len, size_t start_off, + const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *dec, + const struct v4l2_ctrl_hevc_slice_params *prev, + struct v4l2_ctrl_hevc_slice_params *sl, + u32 *eps, int max_eps, int *num_eps) +{ + int chroma_array_type = (sps->flags & V4L2_HEVC_SPS_FLAG_SEPARATE_COLOUR_PLANE) ? + 0 : sps->chroma_format_idc; + int ctb_log2 = sps->log2_min_luma_coding_block_size_minus3 + 3 + + sps->log2_diff_max_min_luma_coding_block_size; + u32 ctbs = DIV_ROUND_UP(sps->pic_width_in_luma_samples, 1u << ctb_log2) * + DIV_ROUND_UP(sps->pic_height_in_luma_samples, 1u << ctb_log2); + int total = dec->num_poc_st_curr_before + dec->num_poc_st_curr_after + + dec->num_poc_lt_curr; + u8 entry_l0[V4L2_HEVC_DPB_ENTRIES_NUM_MAX] = {}; + u8 entry_l1[V4L2_HEVC_DPB_ENTRIES_NUM_MAX] = {}; + bool mod_l0 = false, mod_l1 = false, first, dependent = false; + bool temporal_mvp = false, dbk_disabled, lf_flag_present; + struct avd_br br; + u8 nal_type; + u32 i; + + if (nal_len < 3) + return -EINVAL; + nal_type = (nal[0] >> 1) & 0x3f; + br_init(&br, nal + 2, nal_len - 2); + + first = br_u(&br, 1); + if (nal_type >= HEVC_NAL_BLA_W_LP && nal_type <= HEVC_NAL_RSV_IRAP_23) + br_u(&br, 1); /* no_output_of_prior_pics_flag */ + br_ue(&br); /* slice_pic_parameter_set_id */ + + if (!first) { + if (pps->flags & V4L2_HEVC_PPS_FLAG_DEPENDENT_SLICE_SEGMENT_ENABLED) + dependent = br_u(&br, 1); + sl->slice_segment_addr = br_u(&br, ceil_log2(ctbs)); + if (sl->slice_segment_addr >= ctbs) + return -EINVAL; + } + + if (dependent) { + if (!prev) + return -EINVAL; + *sl = (struct v4l2_ctrl_hevc_slice_params){ + .slice_type = prev->slice_type, + .colour_plane_id = prev->colour_plane_id, + .num_ref_idx_l0_active_minus1 = prev->num_ref_idx_l0_active_minus1, + .num_ref_idx_l1_active_minus1 = prev->num_ref_idx_l1_active_minus1, + .collocated_ref_idx = prev->collocated_ref_idx, + .five_minus_max_num_merge_cand = prev->five_minus_max_num_merge_cand, + .slice_qp_delta = prev->slice_qp_delta, + .slice_cb_qp_offset = prev->slice_cb_qp_offset, + .slice_cr_qp_offset = prev->slice_cr_qp_offset, + .slice_beta_offset_div2 = prev->slice_beta_offset_div2, + .slice_tc_offset_div2 = prev->slice_tc_offset_div2, + .slice_segment_addr = sl->slice_segment_addr, + .pred_weight_table = prev->pred_weight_table, + .flags = prev->flags | V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT, + }; + memcpy(sl->ref_idx_l0, prev->ref_idx_l0, sizeof(sl->ref_idx_l0)); + memcpy(sl->ref_idx_l1, prev->ref_idx_l1, sizeof(sl->ref_idx_l1)); + goto entry_points; + } + + br_skip(&br, pps->num_extra_slice_header_bits); + sl->slice_type = br_ue(&br); + if (sl->slice_type > V4L2_HEVC_SLICE_TYPE_I) + return -EINVAL; + if (pps->flags & V4L2_HEVC_PPS_FLAG_OUTPUT_FLAG_PRESENT) + br_u(&br, 1); /* pic_output_flag */ + if (sps->flags & V4L2_HEVC_SPS_FLAG_SEPARATE_COLOUR_PLANE) + sl->colour_plane_id = br_u(&br, 2); + + if (nal_type != HEVC_NAL_IDR_W_RADL && nal_type != HEVC_NAL_IDR_N_LP) { + br_u(&br, sps->log2_max_pic_order_cnt_lsb_minus4 + 4); + if (!br_u(&br, 1)) /* short_term_ref_pic_set_sps_flag */ + br_skip(&br, dec->short_term_ref_pic_set_size); + else + br_skip(&br, ceil_log2(sps->num_short_term_ref_pic_sets)); + if (sps->flags & V4L2_HEVC_SPS_FLAG_LONG_TERM_REF_PICS_PRESENT) + br_skip(&br, dec->long_term_ref_pic_set_size); + if (sps->flags & V4L2_HEVC_SPS_FLAG_SPS_TEMPORAL_MVP_ENABLED) + temporal_mvp = br_u(&br, 1); + } + if (temporal_mvp) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_TEMPORAL_MVP_ENABLED; + + if (sps->flags & V4L2_HEVC_SPS_FLAG_SAMPLE_ADAPTIVE_OFFSET) { + if (br_u(&br, 1)) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_LUMA; + if (chroma_array_type && br_u(&br, 1)) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_CHROMA; + } + + if (sl->slice_type != V4L2_HEVC_SLICE_TYPE_I) { + bool b = sl->slice_type == V4L2_HEVC_SLICE_TYPE_B; + + sl->num_ref_idx_l0_active_minus1 = pps->num_ref_idx_l0_default_active_minus1; + sl->num_ref_idx_l1_active_minus1 = pps->num_ref_idx_l1_default_active_minus1; + if (br_u(&br, 1)) { /* num_ref_idx_active_override_flag */ + sl->num_ref_idx_l0_active_minus1 = br_ue(&br); + if (b) + sl->num_ref_idx_l1_active_minus1 = br_ue(&br); + } + if (sl->num_ref_idx_l0_active_minus1 >= V4L2_HEVC_DPB_ENTRIES_NUM_MAX || + sl->num_ref_idx_l1_active_minus1 >= V4L2_HEVC_DPB_ENTRIES_NUM_MAX) + return -EINVAL; + + if ((pps->flags & V4L2_HEVC_PPS_FLAG_LISTS_MODIFICATION_PRESENT) && + total > 1) { + int bits = ceil_log2(total); + + mod_l0 = br_u(&br, 1); + if (mod_l0) + for (i = 0; i <= sl->num_ref_idx_l0_active_minus1; i++) + entry_l0[i] = br_u(&br, bits); + if (b) { + mod_l1 = br_u(&br, 1); + if (mod_l1) + for (i = 0; i <= sl->num_ref_idx_l1_active_minus1; i++) + entry_l1[i] = br_u(&br, bits); + } + } + if (b && br_u(&br, 1)) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_MVD_L1_ZERO; + if ((pps->flags & V4L2_HEVC_PPS_FLAG_CABAC_INIT_PRESENT) && br_u(&br, 1)) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_CABAC_INIT; + if (temporal_mvp) { + bool from_l0 = true; + + if (b) + from_l0 = br_u(&br, 1); + if (from_l0) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_COLLOCATED_FROM_L0; + u32 max_idx = from_l0 ? sl->num_ref_idx_l0_active_minus1 : + sl->num_ref_idx_l1_active_minus1; + + if (max_idx) { + u32 idx = br_ue(&br); + + if (idx > max_idx) + return -EINVAL; + sl->collocated_ref_idx = idx; + } + } + if (((pps->flags & V4L2_HEVC_PPS_FLAG_WEIGHTED_PRED) && !b) || + ((pps->flags & V4L2_HEVC_PPS_FLAG_WEIGHTED_BIPRED) && b)) + if (parse_weights(&br, sl, chroma_array_type)) + return -EINVAL; + sl->five_minus_max_num_merge_cand = br_ue(&br); + build_ref_lists(sl, dec, entry_l0, mod_l0, entry_l1, mod_l1); + } + + sl->slice_qp_delta = br_se(&br); + if (pps->flags & V4L2_HEVC_PPS_FLAG_PPS_SLICE_CHROMA_QP_OFFSETS_PRESENT) { + sl->slice_cb_qp_offset = br_se(&br); + sl->slice_cr_qp_offset = br_se(&br); + } + + dbk_disabled = pps->flags & V4L2_HEVC_PPS_FLAG_PPS_DISABLE_DEBLOCKING_FILTER; + sl->slice_beta_offset_div2 = pps->pps_beta_offset_div2; + sl->slice_tc_offset_div2 = pps->pps_tc_offset_div2; + if ((pps->flags & V4L2_HEVC_PPS_FLAG_DEBLOCKING_FILTER_OVERRIDE_ENABLED) && + br_u(&br, 1)) { /* deblocking_filter_override_flag */ + dbk_disabled = br_u(&br, 1); + if (!dbk_disabled) { + sl->slice_beta_offset_div2 = br_se(&br); + sl->slice_tc_offset_div2 = br_se(&br); + } + } + if (dbk_disabled) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_DEBLOCKING_FILTER_DISABLED; + + /* slice_loop_filter_across_slices_enabled_flag, else as the PPS */ + lf_flag_present = (sl->flags & (V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_LUMA | + V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_SAO_CHROMA)) || + !dbk_disabled; + if ((pps->flags & V4L2_HEVC_PPS_FLAG_PPS_LOOP_FILTER_ACROSS_SLICES_ENABLED) && + (!lf_flag_present || br_u(&br, 1))) + sl->flags |= V4L2_HEVC_SLICE_PARAMS_FLAG_SLICE_LOOP_FILTER_ACROSS_SLICES_ENABLED; + +entry_points: + sl->nal_unit_type = nal_type; + sl->nuh_temporal_id_plus1 = nal[1] & 0x7; + sl->slice_pic_order_cnt = dec->pic_order_cnt_val; + sl->short_term_ref_pic_set_size = dec->short_term_ref_pic_set_size; + sl->long_term_ref_pic_set_size = dec->long_term_ref_pic_set_size; + + if (pps->flags & (V4L2_HEVC_PPS_FLAG_TILES_ENABLED | + V4L2_HEVC_PPS_FLAG_ENTROPY_CODING_SYNC_ENABLED)) { + u32 n = br_ue(&br); + + /* A slice has fewer entry points than the picture has CTBs. */ + if (br.err || n >= ctbs || n > (u32)(max_eps - *num_eps)) + return -EINVAL; + if (n) { + int len = br_ue(&br) + 1; + + if (br.err || len > 32) + return -EINVAL; + for (i = 0; i < n; i++) { + eps[(*num_eps)++] = br_u(&br, len) + 1; + if (br.err) + return -EINVAL; + } + } + sl->num_entry_point_offsets = n; + } + if (pps->flags & V4L2_HEVC_PPS_FLAG_SLICE_SEGMENT_HEADER_EXTENSION_PRESENT) + br_skip(&br, br_ue(&br) * 8); + + /* byte_alignment() */ + if (br_u(&br, 1) != 1) + return -EINVAL; + br_skip(&br, br.bits); + if (br.err) + return -EINVAL; + + sl->data_byte_offset = start_off + 2 + br_raw_pos(&br); + return 0; +} + +/* + * The entry points split the slice segment data (escaped bytes, 7.4.7.1), + * and the last substream must not be empty. + */ +static int check_entry_points(const struct v4l2_ctrl_hevc_slice_params *sl, + const u32 *eps) +{ + u32 data_len, i; + u64 sum = 0; + + if (sl->data_byte_offset >= sl->bit_size / 8) + return -EINVAL; + data_len = sl->bit_size / 8 - sl->data_byte_offset; + for (i = 0; i < sl->num_entry_point_offsets; i++) + sum += eps[i]; + return sum < data_len ? 0 : -EINVAL; +} + +static bool dpb_indices_valid(const u8 *idx, u8 n, u8 num_active) +{ + if (n > V4L2_HEVC_DPB_ENTRIES_NUM_MAX) + return false; + for (int i = 0; i < n; i++) + if (idx[i] >= num_active) + return false; + return true; +} + +/* + * The V4L2 core does not bound these; the reference lists are built from + * them and the driver indexes the DPB by the result. + */ +static bool dec_params_valid(const struct v4l2_ctrl_hevc_decode_params *dec) +{ + u8 active = dec->num_active_dpb_entries; + + return active <= V4L2_HEVC_DPB_ENTRIES_NUM_MAX && + dpb_indices_valid(dec->poc_st_curr_before, dec->num_poc_st_curr_before, active) && + dpb_indices_valid(dec->poc_st_curr_after, dec->num_poc_st_curr_after, active) && + dpb_indices_valid(dec->poc_lt_curr, dec->num_poc_lt_curr, active); +} + +static size_t next_start_code(const u8 *buf, size_t len, size_t from) +{ + for (size_t i = from; i + 3 <= len; i++) + if (!buf[i] && !buf[i + 1] && buf[i + 2] == 1) + return i; + return len; +} + +int avd_hevc_parse_slices(const u8 *buf, size_t len, + const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *dec, + struct v4l2_ctrl_hevc_slice_params **slices, int *num_slices, + u32 **entry_points, int *num_entry_points) +{ + const int max_slices = 600, max_eps = 4096; + struct v4l2_ctrl_hevc_slice_params *sl, *prev = NULL; + size_t sc = next_start_code(buf, len, 0), slice_start = 0, new_start; + int n = 0, neps = 0, ret = 0; + u32 *eps; + + /* + * Main and Main 10 only: Range Extensions slice headers may hold + * fields gated on PPS flags that the V4L2 PPS does not carry. + */ + if (sps->chroma_format_idc != 1 || sps->bit_depth_luma_minus8 > 2 || + sps->bit_depth_chroma_minus8 > 2) + return -EINVAL; + if (!dec_params_valid(dec)) + return -EINVAL; + + sl = kvcalloc(max_slices, sizeof(*sl), GFP_KERNEL); + eps = kvcalloc(max_eps, sizeof(*eps), GFP_KERNEL); + if (!sl || !eps) { + ret = -ENOMEM; + goto err; + } + + while (sc < len) { + size_t nal = sc + 3; + size_t next = next_start_code(buf, len, nal); + size_t nal_end = next; + u8 type; + + if (nal >= len) + break; + /* A 4-byte start code's leading zero belongs to the next unit. */ + while (nal_end > nal && !buf[nal_end - 1]) + nal_end--; + type = (buf[nal] >> 1) & 0x3f; + + if (is_slice_nal(type)) { + if (n == max_slices) { + ret = -EINVAL; + goto err; + } + /* + * The first slice starts at the buffer start, later + * ones at their start code: the driver walks slices + * as consecutive spans of bit_size / 8 bytes. + */ + new_start = n ? sc : 0; + ret = parse_slice(buf + nal, nal_end - nal, nal - new_start, + sps, pps, dec, prev, &sl[n], eps, max_eps, &neps); + if (ret) + goto err; + if (n) + sl[n - 1].bit_size = (new_start - slice_start) * 8; + slice_start = new_start; + if (!(sl[n].flags & V4L2_HEVC_SLICE_PARAMS_FLAG_DEPENDENT_SLICE_SEGMENT)) + prev = &sl[n]; + n++; + } + sc = next; + } + if (!n) { + ret = -EINVAL; + goto err; + } + sl[n - 1].bit_size = (len - slice_start) * 8; + + for (int i = 0, e = 0; i < n; e += sl[i].num_entry_point_offsets, i++) { + ret = check_entry_points(&sl[i], eps + e); + if (ret) + goto err; + } + + *slices = sl; + *num_slices = n; + *entry_points = eps; + *num_entry_points = neps; + return 0; +err: + kvfree(sl); + kvfree(eps); + return ret; +} diff --git a/drivers/media/platform/apple/avd/avd-hevc-parse.h b/drivers/media/platform/apple/avd/avd-hevc-parse.h new file mode 100644 index 00000000000000..f817fb8cc72bf3 --- /dev/null +++ b/drivers/media/platform/apple/avd/avd-hevc-parse.h @@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +#ifndef AVD_HEVC_PARSE_H +#define AVD_HEVC_PARSE_H + +#include +#include + +/* + * Build slice parameters and entry point offsets for a whole Annex B + * picture. On success *slices and *entry_points are owned by the caller, + * who frees them with kvfree(). + */ +int avd_hevc_parse_slices(const u8 *buf, size_t len, + const struct v4l2_ctrl_hevc_sps *sps, + const struct v4l2_ctrl_hevc_pps *pps, + const struct v4l2_ctrl_hevc_decode_params *dec, + struct v4l2_ctrl_hevc_slice_params **slices, int *num_slices, + u32 **entry_points, int *num_entry_points); + +#endif diff --git a/drivers/media/platform/apple/avd/avd-hevc.c b/drivers/media/platform/apple/avd/avd-hevc.c index 9dfa20ab719139..1677498a92d6a0 100644 --- a/drivers/media/platform/apple/avd/avd-hevc.c +++ b/drivers/media/platform/apple/avd/avd-hevc.c @@ -16,6 +16,7 @@ #include #include "avd.h" +#include "avd-hevc-parse.h" #include "avd-inst.h" #define NEW_TILE_ID BIT(0) @@ -1282,6 +1283,37 @@ static int avd_hevc_compute_tiles(struct avd_ctx *ctx, struct avd_hevc_run *run) return 0; } +/* + * A NAL unit header never starts with two zero bytes (its second byte holds + * nuh_temporal_id_plus1, which is not 0), so a leading 00 00 01 or + * 00 00 00 01 can only be a start code. + */ +static bool avd_hevc_starts_with_start_code(const u8 *buf, size_t len) +{ + if (len >= 3 && !buf[0] && !buf[1] && buf[2] == 1) + return true; + return len >= 4 && !buf[0] && !buf[1] && !buf[2] && buf[3] == 1; +} + +/* Whether this picture's request carries slice parameters. */ +static bool avd_hevc_request_has_slice_params(struct avd_ctx *ctx, + struct avd_hevc_run *run) +{ + struct media_request *req = run->base.bufs.src->vb2_buf.req_obj.req; + struct v4l2_ctrl_handler *hdl; + bool has; + + if (!req) + return true; + hdl = v4l2_ctrl_request_hdl_find(req, &ctx->ctrl_hdl); + if (!hdl) + return false; + has = v4l2_ctrl_request_hdl_ctrl_find(hdl, + V4L2_CID_STATELESS_HEVC_SLICE_PARAMS); + v4l2_ctrl_request_hdl_put(hdl); + return has; +} + static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) { struct v4l2_ctrl *ctrl, *sl, *ep; @@ -1315,6 +1347,39 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) ep = v4l2_ctrl_find(&ctx->ctrl_hdl, V4L2_CID_STATELESS_HEVC_ENTRY_POINT_OFFSETS); + /* + * A client that sets no slice parameters with the picture and queues + * it in Annex B form leaves the slice headers to the decoder + * (Chromium). Parse them from the bitstream. A client without start + * codes may have set the slice parameters once and kept them. + */ + if (!avd_hevc_request_has_slice_params(ctx, run)) { + struct vb2_buffer *src = &run->base.bufs.src->vb2_buf; + size_t len = vb2_get_plane_payload(src, 0); + const u8 *buf = vb2_plane_vaddr(src, 0); + int ret; + + if (!buf || !run->sps || !run->pps || !run->decode) { + avd_run_postamble(ctx, &run->base); + return -EINVAL; + } + if (!avd_hevc_starts_with_start_code(buf, len)) + goto persisted_slices; + ret = avd_hevc_parse_slices(buf, len, + run->sps, run->pps, run->decode, + (struct v4l2_ctrl_hevc_slice_params **)&run->sl, + &run->num_slices, + (u32 **)&run->entry_point_offsets, + &run->num_entry_point_offsets); + if (ret) { + dev_dbg(ctx->dev->dev, "hevc: bitstream slice parse failed: %d\n", ret); + avd_run_postamble(ctx, &run->base); + return ret; + } + goto have_slices; + } + +persisted_slices: /* * Setting a larger dynamic array on a queued request reallocates the * array's storage, which this run would otherwise keep using. Copy what @@ -1328,12 +1393,13 @@ static int avd_hevc_run_preamble(struct avd_ctx *ctx, struct avd_hevc_run *run) kmemdup(ep->p_cur.p, ep->elems * ep->elem_size, GFP_KERNEL); v4l2_ctrl_unlock(sl); if (!run->sl || !run->entry_point_offsets) { - kfree(run->sl); - kfree(run->entry_point_offsets); + kvfree(run->sl); + kvfree(run->entry_point_offsets); avd_run_postamble(ctx, &run->base); return -ENOMEM; } +have_slices: dst_len = run->base.bufs.dst->vb2_buf.planes[0].length; mv_color_len = mv_color_size(fmt_width(ctx), fmt_height(ctx)); @@ -1382,8 +1448,8 @@ static int avd_hevc_run(struct avd_ctx *ctx) done: kfree(run.tile_info.ctb_addr_rs_to_ts); kfree(run.tile_info.tile_ids); - kfree(run.sl); - kfree(run.entry_point_offsets); + kvfree(run.sl); + kvfree(run.entry_point_offsets); return ret; } diff --git a/drivers/media/platform/apple/avd/avd-v4l2.c b/drivers/media/platform/apple/avd/avd-v4l2.c index 4622e920554864..50a4c4bcaeb18f 100644 --- a/drivers/media/platform/apple/avd/avd-v4l2.c +++ b/drivers/media/platform/apple/avd/avd-v4l2.c @@ -123,6 +123,35 @@ static int avd_try_ctrl(struct v4l2_ctrl *ctrl) return 0; } +/* + * A client may pick the decoded format with S_FMT and allocate capture + * buffers before it sets any stream control (Chromium does). The first + * stream control then resolves the image format from ANY. The layout was + * sized for ANY (8-bit 4:2:0 compressed planes), so recompute it for the + * stream and keep the buffers only if they still fit. + */ +static int avd_resolve_busy_image_fmt(struct avd_ctx *ctx, + enum avd_image_fmt image_fmt) +{ + struct v4l2_pix_format_mplane pix_mp = ctx->decoded_fmt.fmt.pix_mp; + struct avd_comp comp = ctx->comp; + + if (ctx->image_fmt != AVD_IMG_FMT_ANY || + !avd_is_valid_fmt(ctx, pix_mp.pixelformat, image_fmt)) + return -EBUSY; + + ctx->image_fmt = image_fmt; + avd_fill_decoded_pixfmt(ctx, &pix_mp); + if (pix_mp.plane_fmt[0].sizeimage > + ctx->decoded_fmt.fmt.pix_mp.plane_fmt[0].sizeimage) { + ctx->image_fmt = AVD_IMG_FMT_ANY; + ctx->comp = comp; + return -EBUSY; + } + ctx->decoded_fmt.fmt.pix_mp = pix_mp; + return 0; +} + static int avd_s_ctrl(struct v4l2_ctrl *ctrl) { struct avd_ctx *ctx = @@ -140,7 +169,7 @@ static int avd_s_ctrl(struct v4l2_ctrl *ctrl) vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE); if (vb2_is_busy(vq)) - return -EBUSY; + return avd_resolve_busy_image_fmt(ctx, image_fmt); ctx->image_fmt = image_fmt; avd_reset_decoded_fmt(ctx); @@ -254,13 +283,13 @@ static const struct avd_ctrl_desc avd_h264_ctrl_descs[] = { { .cfg.id = V4L2_CID_STATELESS_H264_DECODE_MODE, .cfg.min = V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED, - .cfg.max = V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED, + .cfg.max = V4L2_STATELESS_H264_DECODE_MODE_FRAME_BASED, .cfg.def = V4L2_STATELESS_H264_DECODE_MODE_SLICE_BASED, }, { .cfg.id = V4L2_CID_STATELESS_H264_START_CODE, .cfg.min = V4L2_STATELESS_H264_START_CODE_NONE, - .cfg.max = V4L2_STATELESS_H264_START_CODE_NONE, + .cfg.max = V4L2_STATELESS_H264_START_CODE_ANNEX_B, .cfg.def = V4L2_STATELESS_H264_START_CODE_NONE, }, {