Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 94 additions & 0 deletions drivers/media/platform/apple/avd/avd-av1-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,99 @@ static void avd_av1_try_frame_size_test(struct kunit *test)
v4l2_ctrl_handler_free(&ctx->ctrl_hdl);
}

struct avd_av1_tile_case {
const char *desc;
u16 width, height;
bool sb128;
u8 cols, rows;
/* mi_*_starts and *_in_sbs_minus_1 of the first tiles */
u32 col_start[3], col_sbs[3];
u32 row_start[3], row_sbs[3];
u8 context_tile;
int ret;
};

/*
* 1920x1080 is 480x270 MI units: 30x17 superblocks of 64, 15x9 of 128.
* Two uniform tiles per dimension are 15+15 columns and 9+8 rows.
*/
static const struct avd_av1_tile_case avd_av1_tile_cases[] = {
{ "64x64, one tile", 64, 64, false, 1, 1, {0}, {0}, {0}, {0}, 0, 0 },
{ "64x64, 4096x4096 superblock tile", 64, 64, false, 1, 1,
{0}, {4095}, {0}, {4095}, 0, -EINVAL },
{ "64x64, tile one superblock too wide", 64, 64, false, 1, 1,
{0}, {1}, {0}, {0}, 0, -EINVAL },
{ "64x64, tile one superblock too high", 64, 64, false, 1, 1,
{0}, {0}, {0}, {1}, 0, -EINVAL },
{ "66x66, two partial superblocks", 66, 66, false, 2, 2,
{0, 16}, {0, 0}, {0, 16}, {0, 0}, 0, 0 },
{ "1080p, one tile", 1920, 1080, false, 1, 1, {0}, {29}, {0}, {16}, 0, 0 },
{ "1080p, 2x2 tiles", 1920, 1080, false, 2, 2,
{0, 240}, {14, 14}, {0, 144}, {8, 7}, 0, 0 },
{ "1080p, 128x128 superblocks", 1920, 1080, true, 1, 1,
{0}, {14}, {0}, {8}, 0, 0 },
{ "1080p, 64x64 layout with 128x128 superblocks", 1920, 1080, true, 1, 1,
{0}, {29}, {0}, {16}, 0, -EINVAL },
{ "1080p, second column one MI early", 1920, 1080, false, 2, 1,
{0, 239}, {14, 14}, {0}, {16}, 0, -EINVAL },
{ "1080p, second row one superblock late", 1920, 1080, false, 1, 2,
{0}, {29}, {0, 160}, {8, 7}, 0, -EINVAL },
{ "1080p, first column not at 0", 1920, 1080, false, 1, 1,
{16}, {29}, {0}, {16}, 0, -EINVAL },
{ "1080p, columns short of the frame", 1920, 1080, false, 2, 1,
{0, 240}, {14, 13}, {0}, {16}, 0, -EINVAL },
{ "1080p, columns past the frame", 1920, 1080, false, 2, 1,
{0, 240}, {14, 15}, {0}, {16}, 0, -EINVAL },
{ "1080p, third column past the frame", 1920, 1080, false, 3, 1,
{0, 240, 480}, {14, 14, 0}, {0}, {16}, 0, -EINVAL },
{ "1080p, largest width", 1920, 1080, false, 1, 1,
{0}, {U32_MAX}, {0}, {16}, 0, -EINVAL },
{ "1080p, width wrapping to the next column", 1920, 1080, false, 2, 1,
{0, 0}, {U32_MAX, 29}, {0}, {16}, 0, -EINVAL },
{ "1080p, largest height", 1920, 1080, false, 1, 1,
{0}, {29}, {0}, {U32_MAX}, 0, -EINVAL },
{ "1080p, no tiles", 1920, 1080, false, 0, 0, {}, {}, {}, {}, 0, -EINVAL },
{ "1080p, no columns", 1920, 1080, false, 0, 1, {}, {}, {0}, {16}, 0, -EINVAL },
{ "1080p, more columns than tile_info has", 1920, 1080, false,
V4L2_AV1_MAX_TILE_COLS + 1, 1, {0}, {0}, {0}, {16}, 0, -EINVAL },
{ "1080p, CDFs from the last tile", 1920, 1080, false, 2, 2,
{0, 240}, {14, 14}, {0, 144}, {8, 7}, 3, 0 },
{ "1080p, CDFs from past the tiles", 1920, 1080, false, 2, 2,
{0, 240}, {14, 14}, {0, 144}, {8, 7}, 4, -EINVAL },
};

KUNIT_ARRAY_PARAM_DESC(avd_av1_tile, avd_av1_tile_cases, desc);

/* The tiles cover the frame's superblock grid, no more and no less. */
static void avd_av1_tile_info_test(struct kunit *test)
{
const struct avd_av1_tile_case *c = test->param_value;
struct v4l2_ctrl_av1_sequence seq = {
.flags = c->sb128 ? V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK : 0,
};
struct v4l2_ctrl_av1_frame *frame;
struct v4l2_av1_tile_info *t;
unsigned int i;

frame = kunit_kzalloc(test, sizeof(*frame), GFP_KERNEL);
KUNIT_ASSERT_NOT_NULL(test, frame);
frame->frame_width_minus_1 = c->width - 1;
frame->frame_height_minus_1 = c->height - 1;
frame->upscaled_width = c->width;
t = &frame->tile_info;
t->tile_cols = c->cols;
t->tile_rows = c->rows;
t->context_update_tile_id = c->context_tile;
for (i = 0; i < ARRAY_SIZE(c->col_start); i++) {
t->mi_col_starts[i] = c->col_start[i];
t->width_in_sbs_minus_1[i] = c->col_sbs[i];
t->mi_row_starts[i] = c->row_start[i];
t->height_in_sbs_minus_1[i] = c->row_sbs[i];
}

KUNIT_EXPECT_EQ(test, avd_av1_check_tile_info(NULL, &seq, frame), c->ret);
}

static struct kunit_case avd_av1_test_cases[] = {
KUNIT_CASE(avd_av1_tiles_valid_test),
KUNIT_CASE(avd_av1_tiles_count_test),
Expand All @@ -282,6 +375,7 @@ static struct kunit_case avd_av1_test_cases[] = {
KUNIT_CASE(avd_av1_copy_tile_group_test),
KUNIT_CASE_PARAM(avd_av1_frame_size_test, avd_av1_size_gen_params),
KUNIT_CASE(avd_av1_try_frame_size_test),
KUNIT_CASE_PARAM(avd_av1_tile_info_test, avd_av1_tile_gen_params),
{}
};

Expand Down
66 changes: 66 additions & 0 deletions drivers/media/platform/apple/avd/avd-av1.c
Original file line number Diff line number Diff line change
Expand Up @@ -1453,6 +1453,68 @@ avd_av1_check_tiles(struct device *dev, unsigned int tile_rows,
}
EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tiles);

/*
* The tiles of one dimension must cover the frame's superblocks the way
* tile_info() lays them out (AV1 5.9.15): at least one tile, tile i starting
* at the superblocks of the tiles before it, << sbShift, in MI units, and
* the tile sizes adding up to the frame's superblocks. set_tiles() programs
* each tile's start and end from these, and the work buffers are sized from
* the tile sizes.
*/
static int avd_av1_check_tile_dim(const u32 *mi_starts, const u32 *sbs_minus_1,
unsigned int tiles, unsigned int max_tiles,
u32 mi_size, unsigned int sb_shift)
{
u32 sbs = (mi_size + (1 << sb_shift) - 1) >> sb_shift;
u32 start = 0;
unsigned int i;

if (!tiles || tiles > max_tiles)
return -EINVAL;
for (i = 0; i < tiles; i++) {
if (start >= sbs || mi_starts[i] != start << sb_shift ||
sbs_minus_1[i] >= sbs - start)
return -EINVAL;
start += sbs_minus_1[i] + 1;
}
return start == sbs ? 0 : -EINVAL;
}

/*
* The frame is MiCols x MiRows 4x4 blocks (AV1 5.9.9, compute_image_size()),
* in superblocks of 16 or 32 of them (sbShift 4 or 5, AV1 5.9.15). The tile
* whose CDFs are kept is one of the frame's tiles (AV1 6.8.14).
*/
VISIBLE_IF_KUNIT int
avd_av1_check_tile_info(struct device *dev,
const struct v4l2_ctrl_av1_sequence *seq,
const struct v4l2_ctrl_av1_frame *frame)
{
const struct v4l2_av1_tile_info *t = &frame->tile_info;
unsigned int sb_shift =
seq->flags & V4L2_AV1_SEQUENCE_FLAG_USE_128X128_SUPERBLOCK ? 5 : 4;
u32 mi_cols = 2 * (((u32)frame->frame_width_minus_1 + 1 + 7) >> 3);
u32 mi_rows = 2 * (((u32)frame->frame_height_minus_1 + 1 + 7) >> 3);

if (avd_av1_check_tile_dim(t->mi_col_starts, t->width_in_sbs_minus_1,
t->tile_cols, V4L2_AV1_MAX_TILE_COLS,
mi_cols, sb_shift) ||
avd_av1_check_tile_dim(t->mi_row_starts, t->height_in_sbs_minus_1,
t->tile_rows, V4L2_AV1_MAX_TILE_ROWS,
mi_rows, sb_shift)) {
dev_err_ratelimited(dev, "AV1: %ux%u tiles do not cover the %ux%u frame\n",
t->tile_cols, t->tile_rows,
frame->frame_width_minus_1 + 1,
frame->frame_height_minus_1 + 1);
return -EINVAL;
}
if (t->context_update_tile_id >= t->tile_cols * t->tile_rows)
return -EINVAL;

return 0;
}
EXPORT_SYMBOL_IF_KUNIT(avd_av1_check_tile_info);

/*
* Setting a larger tile-group array reallocates the control's storage and
* frees the old one under the control lock, which the run does not otherwise
Expand Down Expand Up @@ -1600,6 +1662,10 @@ static int avd_av1_run_preamble(struct avd_ctx *ctx, struct avd_av1_run *run)
return -EINVAL;
}

ret = avd_av1_check_tile_info(ctx->dev->dev, run->seq, run->frame);
if (ret)
return ret;

ctrl = v4l2_ctrl_find(&ctx->ctrl_hdl,
V4L2_CID_STATELESS_AV1_TILE_GROUP_ENTRY);
if (WARN_ON(!ctrl))
Expand Down
50 changes: 50 additions & 0 deletions drivers/media/platform/apple/avd/avd-h264-params.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,56 @@ avd_h264_slice_type_supported(const struct v4l2_ctrl_h264_slice_params *sl)
sl->slice_type == V4L2_H264_SLICE_TYPE_B;
}

/*
* The picture a slice belongs to, in macroblocks (H.264 7.4.2.1.1 and the
* field_pic_flag semantics in 7.4.3): PicWidthInMbs, and PicSizeInMbs =
* PicWidthInMbs * FrameHeightInMbs / (1 + field_pic_flag), with
* FrameHeightInMbs = (2 - frame_mbs_only_flag) * PicHeightInMapUnits.
*/
struct avd_h264_pic_size {
u32 width_mbs;
u64 mbs;
};

static inline struct avd_h264_pic_size
avd_h264_pic_size(const struct v4l2_ctrl_h264_sps *sps,
const struct v4l2_ctrl_h264_decode_params *decode)
{
bool field = decode->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC;
u32 frame_height = (sps->flags & V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY ? 1 : 2) *
((u32)sps->pic_height_in_map_units_minus1 + 1);
struct avd_h264_pic_size pic = {
.width_mbs = (u32)sps->pic_width_in_mbs_minus1 + 1,
};

pic.mbs = (u64)pic.width_mbs * (frame_height / (1 + field));
return pic;
}

static inline bool avd_h264_same_pic_size(const struct avd_h264_pic_size *a,
const struct avd_h264_pic_size *b)
{
return a->width_mbs == b->width_mbs && a->mbs == b->mbs;
}

/*
* H.264 7.4.3: first_mb_in_slice * (1 + MbaffFrameFlag) is less than
* PicSizeInMbs. The slice start is programmed as a macroblock column and
* row of @pic, so a slice outside it would send the decoder outside the
* picture's buffers.
*/
static inline bool
avd_h264_slice_in_picture(const struct avd_h264_pic_size *pic,
const struct v4l2_ctrl_h264_sps *sps,
const struct v4l2_ctrl_h264_decode_params *decode,
u32 first_mb)
{
bool mbaff = (sps->flags & V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD) &&
!(decode->flags & V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC);

return ((u64)first_mb << mbaff) < pic->mbs;
}

/* Pictures are held until their final slice; ASO is not implemented. */
static inline bool avd_h264_slice_order_supported(unsigned int held,
unsigned int last_mb,
Expand Down
89 changes: 89 additions & 0 deletions drivers/media/platform/apple/avd/avd-h264-test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <media/v4l2-h264.h>

#include "avd.h"
#include "avd-h264-params.h"
#include "avd-h264-parse.h"

struct avd_slice_data_case {
Expand Down Expand Up @@ -312,6 +313,92 @@ static void avd_scaling_flat_test(struct kunit *test)
KUNIT_EXPECT_FALSE(test, avd_h264_scaling_is_flat(&pps, m));
}

struct avd_slice_start_case {
const char *desc;
u32 width_mbs;
u32 height_map_units;
u32 sps_flags;
u32 decode_flags;
u32 first_mb;
bool in_picture;
};

#define AVD_TEST_FRAME V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY
#define AVD_TEST_MBAFF V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD
#define AVD_TEST_FIELD V4L2_H264_DECODE_PARAM_FLAG_FIELD_PIC

static const struct avd_slice_start_case avd_slice_start_cases[] = {
{ "64x64, first macroblock", 4, 4, AVD_TEST_FRAME, 0, 0, true },
{ "64x64, last macroblock", 4, 4, AVD_TEST_FRAME, 0, 15, true },
{ "64x64, PicSizeInMbs", 4, 4, AVD_TEST_FRAME, 0, 16 },
{ "64x64, macroblock 16383", 4, 4, AVD_TEST_FRAME, 0, 16383 },
{ "64x64, U32_MAX", 4, 4, AVD_TEST_FRAME, 0, U32_MAX },
{ "1920x1088, last macroblock", 120, 68, AVD_TEST_FRAME, 0, 8159, true },
{ "1920x1088, PicSizeInMbs", 120, 68, AVD_TEST_FRAME, 0, 8160 },
{ "4096x4096, last macroblock", 256, 256, AVD_TEST_FRAME, 0, 65535, true },
{ "4096x4096, PicSizeInMbs", 256, 256, AVD_TEST_FRAME, 0, 65536 },
/* PicSizeInMbs is 2^32 here: no 32-bit wrap to zero */
{ "largest SPS size, U32_MAX", U16_MAX + 1, U16_MAX + 1, AVD_TEST_FRAME,
0, U32_MAX, true },
{ "interlaced frame, last macroblock", 4, 4, 0, 0, 31, true },
{ "interlaced frame, PicSizeInMbs", 4, 4, 0, 0, 32 },
{ "field, last macroblock", 4, 4, 0, AVD_TEST_FIELD, 15, true },
{ "field, PicSizeInMbs", 4, 4, 0, AVD_TEST_FIELD, 16 },
{ "MBAFF frame, last pair", 4, 4, AVD_TEST_MBAFF, 0, 15, true },
{ "MBAFF frame, half PicSizeInMbs", 4, 4, AVD_TEST_MBAFF, 0, 16 },
{ "MBAFF SPS, field", 4, 4, AVD_TEST_MBAFF, AVD_TEST_FIELD, 15, true },
};

KUNIT_ARRAY_PARAM_DESC(avd_slice_start, avd_slice_start_cases, desc);

/* H.264 7.4.3: a slice starts inside its picture. */
static void avd_slice_start_test(struct kunit *test)
{
const struct avd_slice_start_case *c = test->param_value;
struct v4l2_ctrl_h264_sps sps = {
.pic_width_in_mbs_minus1 = c->width_mbs - 1,
.pic_height_in_map_units_minus1 = c->height_map_units - 1,
.flags = c->sps_flags,
};
struct v4l2_ctrl_h264_decode_params decode = { .flags = c->decode_flags };
struct avd_h264_pic_size pic = avd_h264_pic_size(&sps, &decode);

KUNIT_EXPECT_EQ(test, avd_h264_slice_in_picture(&pic, &sps, &decode,
c->first_mb),
c->in_picture);
}

/* A held picture's later slices keep the size its first slice set. */
static void avd_slice_pic_size_test(struct kunit *test)
{
struct v4l2_ctrl_h264_sps sps = {
.pic_width_in_mbs_minus1 = 3,
.pic_height_in_map_units_minus1 = 3,
.flags = V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY,
};
struct v4l2_ctrl_h264_decode_params decode = {};
struct avd_h264_pic_size first = avd_h264_pic_size(&sps, &decode);
struct avd_h264_pic_size pic;

KUNIT_EXPECT_EQ(test, first.width_mbs, 4U);
KUNIT_EXPECT_EQ(test, first.mbs, 16ULL);
pic = avd_h264_pic_size(&sps, &decode);
KUNIT_EXPECT_TRUE(test, avd_h264_same_pic_size(&first, &pic));

/* As many macroblocks in another shape */
sps.pic_width_in_mbs_minus1 = 1;
sps.pic_height_in_map_units_minus1 = 7;
pic = avd_h264_pic_size(&sps, &decode);
KUNIT_EXPECT_EQ(test, pic.mbs, 16ULL);
KUNIT_EXPECT_FALSE(test, avd_h264_same_pic_size(&first, &pic));

/* A taller picture of the same width */
sps.pic_width_in_mbs_minus1 = 3;
sps.pic_height_in_map_units_minus1 = 255;
pic = avd_h264_pic_size(&sps, &decode);
KUNIT_EXPECT_FALSE(test, avd_h264_same_pic_size(&first, &pic));
}

/* Simple bit writer for synthesizing H.264 slice header bitstreams in tests. */
struct avd_test_bw {
u8 buf[256];
Expand Down Expand Up @@ -1544,6 +1631,8 @@ static struct kunit_case avd_h264_test_cases[] = {
KUNIT_CASE(avd_sps_fmt_init_test),
KUNIT_CASE(avd_sps_range_test),
KUNIT_CASE(avd_start_slices_test),
KUNIT_CASE_PARAM(avd_slice_start_test, avd_slice_start_gen_params),
KUNIT_CASE(avd_slice_pic_size_test),
KUNIT_CASE(avd_annexb_scanner_test),
KUNIT_CASE(avd_frame_mode_framing_test),
KUNIT_CASE(avd_bitreader_epb_test),
Expand Down
16 changes: 16 additions & 0 deletions drivers/media/platform/apple/avd/avd-h264.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ struct avd_bank {
/* allocated size of the held slices */
size_t slice_bytes;
u32 last_mb;
/* the size of the held picture, set by its first slice */
struct avd_h264_pic_size pic;

struct avd_h264_bufs {
/* VP to PP instruction FIFO for pictures of this bank */
Expand Down Expand Up @@ -1263,6 +1265,7 @@ 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 avd_h264_pic_size pic;
size_t held = h264_ctx->b->busy && !h264_ctx->b->submitted ?
h264_ctx->b->slice_num : 0;
int ret;
Expand Down Expand Up @@ -1309,10 +1312,23 @@ static int avd_h264_run(struct avd_ctx *ctx)
goto err_free_jobs;
}

/* The slice starts in its picture; later slices keep the held picture's size. */
pic = avd_h264_pic_size(run.sps, run.decode_params);
if (!avd_h264_slice_in_picture(&pic, run.sps, run.decode_params,
run.slice_params->first_mb_in_slice) ||
(held && !avd_h264_same_pic_size(&pic, &h264_ctx->b->pic))) {
dev_err_ratelimited(ctx->dev->dev,
"H.264 slice at macroblock %u is outside its %llu macroblock picture\n",
run.slice_params->first_mb_in_slice, pic.mbs);
ret = -EINVAL;
goto err_free_jobs;
}

if (is_new_frame(run.slice_params)) {
ret = avd_h264_pick_bank(ctx, run.base.bufs.dst);
if (ret)
goto err_free_jobs;
h264_ctx->b->pic = pic;
}

if (!h264_ctx->b->busy || h264_ctx->b->submitted ||
Expand Down
Loading
Loading