From 835d8b3e96d33c8f7a4bca8c94492b1a6689cd22 Mon Sep 17 00:00:00 2001 From: Yandong Lin Date: Mon, 27 Feb 2023 16:09:01 +0800 Subject: [PATCH] [av1d_parser]: fix gm_params update issue Signed-off-by: Yandong Lin Change-Id: Id00be71023fb1ab3c4a67c290d0255f5021b4b71 --- mpp/codec/dec/av1/av1d_cbs.c | 120 -------------------------------- mpp/codec/dec/av1/av1d_parser.c | 120 ++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+), 120 deletions(-) diff --git a/mpp/codec/dec/av1/av1d_cbs.c b/mpp/codec/dec/av1/av1d_cbs.c index 30c879cd..39220889 100644 --- a/mpp/codec/dec/av1/av1d_cbs.c +++ b/mpp/codec/dec/av1/av1d_cbs.c @@ -38,33 +38,6 @@ #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1) #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1))) -static const RK_S16 div_lut[AV1_DIV_LUT_NUM] = { - 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768, - 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142, - 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564, - 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028, - 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530, - 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066, - 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633, - 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228, - 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848, - 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491, - 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155, - 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838, - 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538, - 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255, - 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986, - 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732, - 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489, - 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259, - 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039, - 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830, - 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630, - 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439, - 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257, - 8240, 8224, 8208, 8192 -}; - /** * Clip a signed integer into the -(2^p),(2^p-1) range. * @param a value to clip @@ -1564,76 +1537,6 @@ static RK_S32 mpp_av1_global_motion_param(AV1Context *ctx, BitReadCtx_t *gb, return 0; } - -static RK_U16 round_two(RK_U64 x, RK_U16 n) -{ - if (n == 0) - return x; - return ((x + ((RK_U64)1 << (n - 1))) >> n); -} - -static RK_S64 round_two_signed(RK_S64 x, RK_U16 n) -{ - return ((x < 0) ? -((RK_S64)round_two(-x, n)) : (RK_S64)round_two(x, n)); -} - -/** - * Resolve divisor process. - * see spec 7.11.3.7 - */ -static RK_S16 resolve_divisor(RK_U32 d, RK_S16 *shift) -{ - RK_S32 e, f; - - *shift = mpp_log2(d); - e = d - (1 << (*shift)); - if (*shift > AV1_DIV_LUT_BITS) - f = round_two(e, *shift - AV1_DIV_LUT_BITS); - else - f = e << (AV1_DIV_LUT_BITS - (*shift)); - - *shift += AV1_DIV_LUT_PREC_BITS; - - return div_lut[f]; -} - -/** - * check if global motion params is valid. - * see spec 7.11.3.6 - */ -static uint8_t get_shear_params_valid(AV1Context *s, RK_S32 idx) -{ - RK_S16 alpha, beta, gamma, delta, divf, divs; - RK_S64 v, w; - RK_S32 *param = &s->cur_frame.gm_params[idx].wmmat[0]; - - if (param[2] < 0) - return 0; - - alpha = MPP_CLIP3(INT16_MIN, INT16_MAX, param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS)); - beta = MPP_CLIP3(INT16_MIN, INT16_MAX, param[3]); - divf = resolve_divisor(MPP_ABS(param[2]), &divs) * (param[2] < 0 ? -1 : 1); - v = (RK_S64)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS); - w = (RK_S64)param[3] * param[4]; - gamma = MPP_CLIP3(INT16_MIN, INT16_MAX, (RK_S32)round_two_signed((v * divf), divs)); - delta = MPP_CLIP3(INT16_MIN, INT16_MAX, param[5] - (RK_S32)round_two_signed((w * divf), divs) - (1 << AV1_WARPEDMODEL_PREC_BITS)); - - alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; - beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; - gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; - delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; - - if ((4 * MPP_ABS(alpha) + 7 * MPP_ABS(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) || - (4 * MPP_ABS(gamma) + 4 * MPP_ABS(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS)) - return 0; - - s->cur_frame.gm_params[idx].alpha = alpha; - s->cur_frame.gm_params[idx].beta = beta; - s->cur_frame.gm_params[idx].gamma = gamma; - s->cur_frame.gm_params[idx].delta = delta; - return 1; -} - static RK_S32 mpp_av1_global_motion_params(AV1Context *ctx, BitReadCtx_t *gb, AV1RawFrameHeader *current) { @@ -1645,13 +1548,6 @@ static RK_S32 mpp_av1_global_motion_params(AV1Context *ctx, BitReadCtx_t *gb, return 0; for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) { - RK_U32 ref_width = ctx->ref_s[ref].frame_width; - RK_U32 ref_height = ctx->ref_s[ref].frame_height; - RK_U32 cur_width = ctx->frame_width; - RK_U32 cur_height = ctx->frame_height; - RK_U8 ref_uses_scaling = cur_width != ref_width || cur_height != ref_height; - RK_U32 shear_params_valid; - flags(is_global[ref], 1, ref); if (current->is_global[ref]) { flags(is_rot_zoom[ref], 1, ref); @@ -1681,22 +1577,6 @@ static RK_S32 mpp_av1_global_motion_params(AV1Context *ctx, BitReadCtx_t *gb, CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 0)); CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 1)); } - - if (type <= AV1_WARP_MODEL_AFFINE) - shear_params_valid = get_shear_params_valid(ctx, ref); - - if (!shear_params_valid || ref_uses_scaling) { - ctx->cur_frame.gm_params[ref].alpha = -32768; - ctx->cur_frame.gm_params[ref].beta = -32768; - ctx->cur_frame.gm_params[ref].gamma = -32768; - ctx->cur_frame.gm_params[ref].delta = -32768; - } - av1d_dbg(AV1D_DBG_HEADER, "ref %d alpa %d beta %d gamma %d delta %d\n", - ref, - ctx->cur_frame.gm_params[ref].alpha, - ctx->cur_frame.gm_params[ref].beta, - ctx->cur_frame.gm_params[ref].gamma, - ctx->cur_frame.gm_params[ref].delta); } return 0; diff --git a/mpp/codec/dec/av1/av1d_parser.c b/mpp/codec/dec/av1/av1d_parser.c index 733bfe31..7169bb06 100644 --- a/mpp/codec/dec/av1/av1d_parser.c +++ b/mpp/codec/dec/av1/av1d_parser.c @@ -34,6 +34,34 @@ #include "mpp_frame_impl.h" RK_U32 av1d_debug = 0; + +static const RK_S16 div_lut[AV1_DIV_LUT_NUM] = { + 16384, 16320, 16257, 16194, 16132, 16070, 16009, 15948, 15888, 15828, 15768, + 15709, 15650, 15592, 15534, 15477, 15420, 15364, 15308, 15252, 15197, 15142, + 15087, 15033, 14980, 14926, 14873, 14821, 14769, 14717, 14665, 14614, 14564, + 14513, 14463, 14413, 14364, 14315, 14266, 14218, 14170, 14122, 14075, 14028, + 13981, 13935, 13888, 13843, 13797, 13752, 13707, 13662, 13618, 13574, 13530, + 13487, 13443, 13400, 13358, 13315, 13273, 13231, 13190, 13148, 13107, 13066, + 13026, 12985, 12945, 12906, 12866, 12827, 12788, 12749, 12710, 12672, 12633, + 12596, 12558, 12520, 12483, 12446, 12409, 12373, 12336, 12300, 12264, 12228, + 12193, 12157, 12122, 12087, 12053, 12018, 11984, 11950, 11916, 11882, 11848, + 11815, 11782, 11749, 11716, 11683, 11651, 11619, 11586, 11555, 11523, 11491, + 11460, 11429, 11398, 11367, 11336, 11305, 11275, 11245, 11215, 11185, 11155, + 11125, 11096, 11067, 11038, 11009, 10980, 10951, 10923, 10894, 10866, 10838, + 10810, 10782, 10755, 10727, 10700, 10673, 10645, 10618, 10592, 10565, 10538, + 10512, 10486, 10460, 10434, 10408, 10382, 10356, 10331, 10305, 10280, 10255, + 10230, 10205, 10180, 10156, 10131, 10107, 10082, 10058, 10034, 10010, 9986, + 9963, 9939, 9916, 9892, 9869, 9846, 9823, 9800, 9777, 9754, 9732, + 9709, 9687, 9664, 9642, 9620, 9598, 9576, 9554, 9533, 9511, 9489, + 9468, 9447, 9425, 9404, 9383, 9362, 9341, 9321, 9300, 9279, 9259, + 9239, 9218, 9198, 9178, 9158, 9138, 9118, 9098, 9079, 9059, 9039, + 9020, 9001, 8981, 8962, 8943, 8924, 8905, 8886, 8867, 8849, 8830, + 8812, 8793, 8775, 8756, 8738, 8720, 8702, 8684, 8666, 8648, 8630, + 8613, 8595, 8577, 8560, 8542, 8525, 8508, 8490, 8473, 8456, 8439, + 8422, 8405, 8389, 8372, 8355, 8339, 8322, 8306, 8289, 8273, 8257, + 8240, 8224, 8208, 8192 +}; + /** * Clip a signed integer into the -(2^p),(2^p-1) range. * @param a value to clip @@ -181,6 +209,77 @@ static void read_global_param(AV1Context *s, RK_S32 type, RK_S32 ref, RK_S32 idx -mx, mx + 1, r) << prec_diff) + round; } +static RK_U16 round_two(RK_U64 x, RK_U16 n) +{ + if (n == 0) + return x; + return ((x + ((RK_U64)1 << (n - 1))) >> n); +} + +static RK_S64 round_two_signed(RK_S64 x, RK_U16 n) +{ + return ((x < 0) ? -((RK_S64)round_two(-x, n)) : (RK_S64)round_two(x, n)); +} + +/** + * Resolve divisor process. + * see spec 7.11.3.7 + */ +static RK_S16 resolve_divisor(RK_U32 d, RK_S16 *shift) +{ + RK_S32 e, f; + + *shift = mpp_log2(d); + e = d - (1 << (*shift)); + if (*shift > AV1_DIV_LUT_BITS) + f = round_two(e, *shift - AV1_DIV_LUT_BITS); + else + f = e << (AV1_DIV_LUT_BITS - (*shift)); + + *shift += AV1_DIV_LUT_PREC_BITS; + + return div_lut[f]; +} + +/** + * check if global motion params is valid. + * see spec 7.11.3.6 + */ +static RK_U8 get_shear_params_valid(AV1Context *s, RK_S32 idx) +{ + RK_S16 alpha, beta, gamma, delta, divf, divs; + RK_S64 v, w; + RK_S32 *param = &s->cur_frame.gm_params[idx].wmmat[0]; + + if (param[2] < 0) + return 0; + + alpha = MPP_CLIP3(INT16_MIN, INT16_MAX, param[2] - (1 << AV1_WARPEDMODEL_PREC_BITS)); + beta = MPP_CLIP3(INT16_MIN, INT16_MAX, param[3]); + divf = resolve_divisor(MPP_ABS(param[2]), &divs) * (param[2] < 0 ? -1 : 1); + v = (RK_S64)param[4] * (1 << AV1_WARPEDMODEL_PREC_BITS); + w = (RK_S64)param[3] * param[4]; + gamma = MPP_CLIP3(INT16_MIN, INT16_MAX, (RK_S32)round_two_signed((v * divf), divs)); + delta = MPP_CLIP3(INT16_MIN, INT16_MAX, param[5] - (RK_S32)round_two_signed((w * divf), divs) - + (1 << AV1_WARPEDMODEL_PREC_BITS)); + + alpha = round_two_signed(alpha, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + beta = round_two_signed(beta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + gamma = round_two_signed(gamma, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + delta = round_two_signed(delta, AV1_WARP_PARAM_REDUCE_BITS) << AV1_WARP_PARAM_REDUCE_BITS; + + s->cur_frame.gm_params[idx].alpha = alpha; + s->cur_frame.gm_params[idx].beta = beta; + s->cur_frame.gm_params[idx].gamma = gamma; + s->cur_frame.gm_params[idx].delta = delta; + + if ((4 * MPP_ABS(alpha) + 7 * MPP_ABS(beta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS) || + (4 * MPP_ABS(gamma) + 4 * MPP_ABS(delta)) >= (1 << AV1_WARPEDMODEL_PREC_BITS)) + return 0; + + return 1; +} + /** * update gm type/params, since cbs already implemented part of this funcation, * so we don't need to full implement spec. @@ -190,6 +289,7 @@ static void global_motion_params(AV1Context *s) const AV1RawFrameHeader *header = s->raw_frame_header; RK_S32 type, ref; RK_S32 i = 0; + AV1Context *ctx = s; for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) { s->cur_frame.gm_params[ref].wmtype = AV1_WARP_MODEL_IDENTITY; @@ -202,6 +302,10 @@ static void global_motion_params(AV1Context *s) return; for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) { + RK_U8 ref_uses_scaling = ctx->frame_width != ctx->ref_s[ref].frame_width || + ctx->frame_height != ctx->ref_s[ref].frame_height; + RK_U32 shear_params_valid; + if (header->is_global[ref]) { if (header->is_rot_zoom[ref]) { type = AV1_WARP_MODEL_ROTZOOM; @@ -229,6 +333,22 @@ static void global_motion_params(AV1Context *s) read_global_param(s, type, ref, 0); read_global_param(s, type, ref, 1); } + + if (type <= AV1_WARP_MODEL_AFFINE) + shear_params_valid = get_shear_params_valid(ctx, ref); + + if (!shear_params_valid || ref_uses_scaling) { + ctx->cur_frame.gm_params[ref].alpha = -32768; + ctx->cur_frame.gm_params[ref].beta = -32768; + ctx->cur_frame.gm_params[ref].gamma = -32768; + ctx->cur_frame.gm_params[ref].delta = -32768; + } + av1d_dbg(AV1D_DBG_HEADER, "ref %d alpa %d beta %d gamma %d delta %d\n", + ref, + ctx->cur_frame.gm_params[ref].alpha, + ctx->cur_frame.gm_params[ref].beta, + ctx->cur_frame.gm_params[ref].gamma, + ctx->cur_frame.gm_params[ref].delta); } }