Path: blob/21.2-virgl/src/gallium/frontends/omx/bellagio/vid_dec_av1.c
4565 views
/**************************************************************************1*2* Copyright 2020 Advanced Micro Devices, Inc.3* All Rights Reserved.4*5* Permission is hereby granted, free of charge, to any person obtaining a6* copy of this software and associated documentation files (the7* "Software"), to deal in the Software without restriction, including8* without limitation the rights to use, copy, modify, merge, publish,9* distribute, sub license, and/or sell copies of the Software, and to10* permit persons to whom the Software is furnished to do so, subject to11* the following conditions:12*13* The above copyright notice and this permission notice (including the14* next paragraph) shall be included in all copies or substantial portions15* of the Software.16*17* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS18* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF19* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.20* IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR21* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,22* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE23* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.24*25**************************************************************************/2627#include "pipe/p_video_codec.h"28#include "util/u_memory.h"29#include "util/u_video.h"30#include "vl/vl_video_buffer.h"3132#include "entrypoint.h"33#include "vid_dec.h"34#include "vid_dec_av1.h"3536static unsigned av1_f(struct vl_vlc *vlc, unsigned n)37{38unsigned valid = vl_vlc_valid_bits(vlc);3940if (n == 0)41return 0;4243if (valid < 32)44vl_vlc_fillbits(vlc);4546return vl_vlc_get_uimsbf(vlc, n);47}4849static unsigned av1_uvlc(struct vl_vlc *vlc)50{51unsigned value;52unsigned leadingZeros = 0;5354while (1) {55bool done = av1_f(vlc, 1);56if (done)57break;58leadingZeros++;59}6061if (leadingZeros >= 32)62return 0xffffffff;6364value = av1_f(vlc, leadingZeros);6566return value + (1 << leadingZeros) - 1;67}6869static int av1_le(struct vl_vlc *vlc, const unsigned n)70{71unsigned byte, t = 0;72unsigned i;7374for (i = 0; i < n; ++i) {75byte = av1_f(vlc, 8);76t += (byte << (i * 8));77}7879return t;80}8182static unsigned av1_uleb128(struct vl_vlc *vlc)83{84unsigned value = 0;85unsigned leb128Bytes = 0;86unsigned i;8788for (i = 0; i < 8; ++i) {89leb128Bytes = av1_f(vlc, 8);90value |= ((leb128Bytes & 0x7f) << (i * 7));91if (!(leb128Bytes & 0x80))92break;93}9495return value;96}9798static int av1_su(struct vl_vlc *vlc, const unsigned n)99{100unsigned value = av1_f(vlc, n);101unsigned signMask = 1 << (n - 1);102103if (value && signMask)104value = value - 2 * signMask;105106return value;107}108109static unsigned FloorLog2(unsigned x)110{111unsigned s = 0;112unsigned x1 = x;113114while (x1 != 0) {115x1 = x1 >> 1;116s++;117}118119return s - 1;120}121122static unsigned av1_ns(struct vl_vlc *vlc, unsigned n)123{124unsigned w = FloorLog2(n) + 1;125unsigned m = (1 << w) - n;126unsigned v = av1_f(vlc, w - 1);127128if (v < m)129return v;130131bool extra_bit = av1_f(vlc, 1);132133return (v << 1) - m + extra_bit;134}135136static void av1_byte_alignment(struct vl_vlc *vlc)137{138vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);139}140141static void sequence_header_obu(vid_dec_PrivateType *priv, struct vl_vlc *vlc)142{143struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);144bool timing_info_present_flag;145bool initial_display_delay_present_flag;146uint8_t seq_level_idx;147bool initial_display_delay_present_for_this_op;148bool high_bitdepth;149bool twelve_bit;150bool color_description_present_flag;151uint8_t color_primaries;152uint8_t transfer_characteristics;153uint8_t matrix_coefficients;154int i;155156seq->seq_profile = av1_f(vlc, 3);157assert(seq->seq_profile < 3);158159av1_f(vlc, 1); /* still_picture */160seq->reduced_still_picture_header = av1_f(vlc, 1);161if (seq->reduced_still_picture_header) {162timing_info_present_flag = 0;163seq->decoder_model_info_present_flag = 0;164initial_display_delay_present_flag = 0;165seq->operating_points_cnt_minus_1 = 0;166seq->operating_point_idc[0] = 0;167seq_level_idx = av1_f(vlc, 5);168seq->decoder_model_present_for_this_op[0] = 0;169initial_display_delay_present_for_this_op = 0;170} else {171uint8_t buffer_delay_length_minus_1 = 0;172173timing_info_present_flag = av1_f(vlc, 1);174if (timing_info_present_flag) {175av1_f(vlc, 32); /* num_units_in_display_tick */176av1_f(vlc, 32); /* time_scale */177seq->timing_info.equal_picture_interval = av1_f(vlc, 1);178if (seq->timing_info.equal_picture_interval)179av1_uvlc(vlc); /* num_ticks_per_picture_minus_1 */180181seq->decoder_model_info_present_flag = av1_f(vlc, 1);182if (seq->decoder_model_info_present_flag) {183/* decoder_model_info */184buffer_delay_length_minus_1 = av1_f(vlc, 5);185seq->decoder_model_info.num_units_in_decoding_tick = av1_f(vlc, 32);186seq->decoder_model_info.buffer_removal_time_length_minus_1 = av1_f(vlc, 5);187seq->decoder_model_info.frame_presentation_time_length_minus_1 = av1_f(vlc, 5);188}189} else {190seq->decoder_model_info_present_flag = 0;191}192193initial_display_delay_present_flag = av1_f(vlc, 1);194seq->operating_points_cnt_minus_1 = av1_f(vlc, 5);195for (i = 0; i < seq->operating_points_cnt_minus_1 + 1; ++i) {196seq->operating_point_idc[i] = av1_f(vlc, 12);197seq_level_idx = av1_f(vlc, 5);198if (seq_level_idx > 7)199av1_f(vlc, 1); /* seq_tier */200201if (seq->decoder_model_info_present_flag) {202seq->decoder_model_present_for_this_op[i] = av1_f(vlc, 1);203if (seq->decoder_model_present_for_this_op[i]) {204uint8_t n = buffer_delay_length_minus_1 + 1;205av1_f(vlc, n); /* decoder_buffer_delay */206av1_f(vlc, n); /* encoder_buffer_delay */207av1_f(vlc, 1); /* low_delay_mode_flag */208}209} else {210seq->decoder_model_present_for_this_op[i] = 0;211}212213if (initial_display_delay_present_flag) {214initial_display_delay_present_for_this_op = av1_f(vlc, 1);215if (initial_display_delay_present_for_this_op)216av1_f(vlc, 4); /* initial_display_delay_minus_1 */217}218}219}220221seq->frame_width_bits_minus_1 = av1_f(vlc, 4);222seq->frame_height_bits_minus_1 = av1_f(vlc, 4);223seq->max_frame_width_minus_1 = av1_f(vlc, seq->frame_width_bits_minus_1 + 1);224seq->max_frame_height_minus_1 = av1_f(vlc, seq->frame_height_bits_minus_1 + 1);225226if (seq->reduced_still_picture_header)227seq->frame_id_numbers_present_flag = 0;228else229seq->frame_id_numbers_present_flag = av1_f(vlc, 1);230if (seq->frame_id_numbers_present_flag) {231seq->delta_frame_id_length_minus_2 = av1_f(vlc, 4);232seq->additional_frame_id_length_minus_1 = av1_f(vlc, 3);233}234235seq->use_128x128_superblock = av1_f(vlc, 1);236seq->enable_filter_intra = av1_f(vlc, 1);237seq->enable_intra_edge_filter = av1_f(vlc, 1);238if (seq->reduced_still_picture_header) {239seq->enable_interintra_compound = 0;240seq->enable_masked_compound = 0;241seq->enable_warped_motion = 0;242seq->enable_dual_filter = 0;243seq->enable_order_hint = 0;244seq->enable_jnt_comp = 0;245seq->enable_ref_frame_mvs = 0;246seq->seq_force_screen_content_tools = AV1_SELECT_SCREEN_CONTENT_TOOLS;247seq->seq_force_integer_mv = AV1_SELECT_INTEGER_MV;248seq->OrderHintBits = 0;249} else {250bool seq_choose_screen_content_tools;251seq->enable_interintra_compound = av1_f(vlc, 1);252seq->enable_masked_compound = av1_f(vlc, 1);253seq->enable_warped_motion = av1_f(vlc, 1);254seq->enable_dual_filter = av1_f(vlc, 1);255seq->enable_order_hint = av1_f(vlc, 1);256if (seq->enable_order_hint) {257seq->enable_jnt_comp = av1_f(vlc, 1);258seq->enable_ref_frame_mvs = av1_f(vlc, 1);259} else {260seq->enable_jnt_comp = 0;261seq->enable_ref_frame_mvs = 0;262}263264seq_choose_screen_content_tools = av1_f(vlc, 1);265seq->seq_force_screen_content_tools =266seq_choose_screen_content_tools ? AV1_SELECT_SCREEN_CONTENT_TOOLS : av1_f(vlc, 1);267268if (seq->seq_force_screen_content_tools > 0) {269bool seq_choose_integer_mv = av1_f(vlc, 1);270seq->seq_force_integer_mv =271seq_choose_integer_mv ? AV1_SELECT_INTEGER_MV : av1_f(vlc, 1);272} else {273seq->seq_force_integer_mv = AV1_SELECT_INTEGER_MV;274}275276if (seq->enable_order_hint) {277seq->order_hint_bits_minus_1 = av1_f(vlc, 3);278seq->OrderHintBits = seq->order_hint_bits_minus_1 + 1;279} else {280seq->OrderHintBits = 0;281}282}283284seq->enable_superres = av1_f(vlc, 1);285seq->enable_cdef = av1_f(vlc, 1);286seq->enable_restoration = av1_f(vlc, 1);287288high_bitdepth = av1_f(vlc, 1);289if (seq->seq_profile == 2 && high_bitdepth) {290twelve_bit = av1_f(vlc, 1);291seq->color_config.BitDepth = twelve_bit ? 12 : 10;292} else if (seq->seq_profile <= 2) {293seq->color_config.BitDepth = high_bitdepth ? 10 : 8;294}295296seq->color_config.mono_chrome = (seq->seq_profile == 1) ? 0 : av1_f(vlc, 1);297seq->color_config.NumPlanes = seq->color_config.mono_chrome ? 1 : 3;298299color_description_present_flag = av1_f(vlc, 1);300if (color_description_present_flag) {301color_primaries = av1_f(vlc, 8);302transfer_characteristics = av1_f(vlc, 8);303matrix_coefficients = av1_f(vlc, 8);304} else {305color_primaries = AV1_CP_UNSPECIFIED;306transfer_characteristics = AV1_TC_UNSPECIFIED;307matrix_coefficients = AV1_MC_UNSPECIFIED;308}309310if (seq->color_config.mono_chrome) {311av1_f(vlc, 1); /* color_range */312seq->color_config.subsampling_x = 1;313seq->color_config.subsampling_y = 1;314seq->color_config.separate_uv_delta_q = 0;315} else if (color_primaries == AV1_CP_BT_709 &&316transfer_characteristics == AV1_TC_SRGB &&317matrix_coefficients == AV1_MC_IDENTITY) {318seq->color_config.subsampling_x = 0;319seq->color_config.subsampling_y = 0;320} else {321av1_f(vlc, 1); /* color_range */322if (seq->seq_profile == 0) {323seq->color_config.subsampling_x = 1;324seq->color_config.subsampling_y = 1;325} else if (seq->seq_profile == 1 ) {326seq->color_config.subsampling_x = 0;327seq->color_config.subsampling_y = 0;328} else {329if (seq->color_config.BitDepth == 12) {330seq->color_config.subsampling_x = av1_f(vlc, 1);331if (seq->color_config.subsampling_x)332seq->color_config.subsampling_y = av1_f(vlc, 1);333else334seq->color_config.subsampling_y = 0;335} else {336seq->color_config.subsampling_x = 1;337seq->color_config.subsampling_y = 0;338}339}340if (seq->color_config.subsampling_x && seq->color_config.subsampling_y)341av1_f(vlc, 2); /* chroma_sample_position */342}343if (!seq->color_config.mono_chrome)344seq->color_config.separate_uv_delta_q = av1_f(vlc, 1);345346seq->film_grain_params_present = av1_f(vlc, 1);347348priv->picture.av1.picture_parameter.profile = seq->seq_profile;349priv->picture.av1.picture_parameter.seq_info_fields.use_128x128_superblock =350seq->use_128x128_superblock;351priv->picture.av1.picture_parameter.seq_info_fields.enable_filter_intra =352seq->enable_filter_intra;353priv->picture.av1.picture_parameter.seq_info_fields.enable_intra_edge_filter =354seq->enable_intra_edge_filter;355priv->picture.av1.picture_parameter.order_hint_bits_minus_1 =356seq->order_hint_bits_minus_1;357priv->picture.av1.picture_parameter.max_width = seq->max_frame_width_minus_1 + 1;358priv->picture.av1.picture_parameter.max_height = seq->max_frame_height_minus_1 + 1;359priv->picture.av1.picture_parameter.seq_info_fields.enable_interintra_compound =360seq->enable_interintra_compound;361priv->picture.av1.picture_parameter.seq_info_fields.enable_masked_compound =362seq->enable_masked_compound;363priv->picture.av1.picture_parameter.seq_info_fields.enable_dual_filter =364seq->enable_dual_filter;365priv->picture.av1.picture_parameter.seq_info_fields.enable_order_hint =366seq->enable_order_hint;367priv->picture.av1.picture_parameter.seq_info_fields.enable_jnt_comp =368seq->enable_jnt_comp;369priv->picture.av1.picture_parameter.seq_info_fields.ref_frame_mvs =370seq->enable_ref_frame_mvs;371priv->picture.av1.picture_parameter.bit_depth_idx =372(seq->color_config.BitDepth - 8) >> 1;373priv->picture.av1.picture_parameter.seq_info_fields.mono_chrome =374seq->color_config.mono_chrome;375}376377static void superres_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)378{379struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);380struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);381unsigned coded_denom;382383if (seq->enable_superres)384hdr->use_superres = av1_f(vlc, 1);385else386hdr->use_superres = 0;387388if (hdr->use_superres) {389coded_denom = av1_f(vlc, 3 /* SUPERRES_DENOM_BITS */);390hdr->SuperresDenom = coded_denom + 9 /* SUPERRES_DENOM_MIN */;391} else {392hdr->SuperresDenom = 8 /* SUPERRES_NUM */;393}394395hdr->UpscaledWidth = hdr->FrameWidth;396hdr->FrameWidth = (hdr->UpscaledWidth * 8 + (hdr->SuperresDenom / 2)) /397hdr->SuperresDenom;398}399400static void compute_image_size(vid_dec_PrivateType *priv)401{402struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);403404hdr->MiCols = 2 * ((hdr->FrameWidth + 7) >> 3);405hdr->MiRows = 2 * ((hdr->FrameHeight + 7) >> 3);406}407408static void frame_size(vid_dec_PrivateType *priv, struct vl_vlc *vlc)409{410struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);411struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);412unsigned frame_width_minus_1;413unsigned frame_height_minus_1;414415if (hdr->frame_size_override_flag) {416frame_width_minus_1 = av1_f(vlc, seq->frame_width_bits_minus_1 + 1);417frame_height_minus_1 = av1_f(vlc, seq->frame_height_bits_minus_1 + 1);418hdr->FrameWidth = frame_width_minus_1 + 1;419hdr->FrameHeight = frame_height_minus_1 + 1;420} else {421hdr->FrameWidth = seq->max_frame_width_minus_1 + 1;422hdr->FrameHeight = seq->max_frame_height_minus_1 + 1;423}424425superres_params(priv, vlc);426compute_image_size(priv);427}428429static void render_size(vid_dec_PrivateType *priv, struct vl_vlc *vlc)430{431struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);432bool render_and_frame_size_different;433unsigned render_width_minus_1;434unsigned render_height_minus_1;435436render_and_frame_size_different = av1_f(vlc, 1);437if (render_and_frame_size_different) {438render_width_minus_1 = av1_f(vlc, 16);439render_height_minus_1 = av1_f(vlc, 16);440hdr->RenderWidth = render_width_minus_1 + 1;441hdr->RenderHeight = render_height_minus_1 + 1;442} else {443hdr->RenderWidth = hdr->UpscaledWidth;444hdr->RenderHeight = hdr->FrameHeight;445}446}447448static int get_relative_dist(vid_dec_PrivateType *priv, int a, int b)449{450struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);451int diff;452unsigned m;453454if (!seq->enable_order_hint)455return 0;456457diff = a - b;458m = 1 << (seq->OrderHintBits - 1);459diff = (diff & (m - 1)) - (diff & m);460461return diff;462}463464static uint8_t find_latest_backward(vid_dec_PrivateType *priv)465{466struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);467uint8_t ref = 0xff;468unsigned latestOrderHint = 0;469int i;470471for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {472unsigned hint = hdr->shiftedOrderHints[i];473if (!hdr->usedFrame[i] &&474hint >= hdr->curFrameHint &&475(ref == 0xff || hint >= latestOrderHint)) {476ref = i;477latestOrderHint = hint;478}479}480481return ref;482}483484static uint8_t find_earliest_backward(vid_dec_PrivateType *priv)485{486struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);487uint8_t ref = 0xff;488unsigned earliestOrderHint = 0;489int i;490491for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {492unsigned hint = hdr->shiftedOrderHints[i];493if (!hdr->usedFrame[i] &&494hint >= hdr->curFrameHint &&495(ref == 0xff || hint < earliestOrderHint)) {496ref = i;497earliestOrderHint = hint;498}499}500501return ref;502}503504static uint8_t find_latest_forward(vid_dec_PrivateType *priv)505{506struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);507uint8_t ref = 0xff;508unsigned latestOrderHint = 0;509int i;510511for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {512unsigned hint = hdr->shiftedOrderHints[i];513if (!hdr->usedFrame[i] &&514hint < hdr->curFrameHint &&515(ref == 0xff || hint >= latestOrderHint)) {516ref = i;517latestOrderHint = hint;518}519}520521return ref;522}523524static void set_frame_refs(vid_dec_PrivateType *priv, struct vl_vlc *vlc)525{526struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);527struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);528uint8_t Ref_Frame_List[5] = { AV1_LAST2_FRAME , AV1_LAST3_FRAME, AV1_BWDREF_FRAME,529AV1_ALTREF2_FRAME, AV1_ALTREF_FRAME };530unsigned earliestOrderHint = 0;531uint8_t ref;532int i;533534for (i = 0; i < AV1_REFS_PER_FRAME; ++i)535hdr->ref_frame_idx[i] = 0xff;536537hdr->ref_frame_idx[0] = hdr->last_frame_idx;538hdr->ref_frame_idx[AV1_GOLDEN_FRAME - AV1_LAST_FRAME] = hdr->gold_frame_idx;539540for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)541hdr->usedFrame[i] = 0;542543hdr->usedFrame[hdr->last_frame_idx] = 1;544hdr->usedFrame[hdr->gold_frame_idx] = 1;545546hdr->curFrameHint = 1 << (seq->OrderHintBits - 1);547548for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)549hdr->shiftedOrderHints[i] =550hdr->curFrameHint +551get_relative_dist(priv, hdr->RefOrderHint[i], hdr->OrderHint);552553ref = find_latest_backward(priv);554if (ref != 0xff) {555hdr->ref_frame_idx[AV1_ALTREF_FRAME - AV1_LAST_FRAME] = ref;556hdr->usedFrame[ref] = 1;557}558559ref = find_earliest_backward(priv);560if (ref != 0xff) {561hdr->ref_frame_idx[AV1_BWDREF_FRAME - AV1_LAST_FRAME] = ref;562hdr->usedFrame[ref] = 1;563}564565ref = find_earliest_backward(priv);566if (ref != 0xff) {567hdr->ref_frame_idx[AV1_ALTREF2_FRAME - AV1_LAST_FRAME] = ref;568hdr->usedFrame[ref] = 1;569}570571for (i = 0; i < AV1_REFS_PER_FRAME - 2; ++i) {572uint8_t refFrame = Ref_Frame_List[i];573if (hdr->ref_frame_idx[refFrame - AV1_LAST_FRAME] == 0xff) {574ref = find_latest_forward(priv);575if (ref != 0xff) {576hdr->ref_frame_idx[refFrame - AV1_LAST_FRAME] = ref;577hdr->usedFrame[ref] = 1;578}579}580}581582ref = 0xff;583for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {584unsigned hint = hdr->shiftedOrderHints[i];585if (ref == 0xff || hint < earliestOrderHint) {586ref = i;587earliestOrderHint = hint;588}589}590591for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {592if (hdr->ref_frame_idx[i] == 0xff)593hdr->ref_frame_idx[i] = ref;594}595}596597static void frame_size_with_refs(vid_dec_PrivateType *priv, struct vl_vlc *vlc)598{599struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);600bool found_ref;601int i;602603for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {604found_ref = av1_f(vlc, 1);605if (found_ref) {606hdr->UpscaledWidth =607priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefUpscaledWidth;608hdr->FrameWidth = hdr->UpscaledWidth;609hdr->FrameHeight =610priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefFrameHeight;611hdr->RenderWidth =612priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefRenderWidth;613hdr->RenderHeight =614priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefRenderHeight;615break;616}617}618619if (!found_ref) {620frame_size(priv, vlc);621render_size(priv, vlc);622} else {623superres_params(priv, vlc);624compute_image_size(priv);625}626}627628static unsigned tile_log2(unsigned blkSize, unsigned target)629{630unsigned k = 0;631632for (k = 0; (blkSize << k) < target; k++);633634return k;635}636637static void tile_info(vid_dec_PrivateType *priv, struct vl_vlc *vlc)638{639struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);640struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);641struct tile_info *ti = &(priv->codec_data.av1.uncompressed_header.ti);642unsigned sbCols;643unsigned sbRows;644int width_sb;645int height_sb;646unsigned sbSize;647unsigned maxTileWidthSb;648unsigned minLog2TileCols;649unsigned maxLog2TileCols;650unsigned maxLog2TileRows;651unsigned minLog2Tiles;652bool uniform_tile_spacing_flag;653unsigned maxTileAreaSb;654unsigned startSb, i;655656sbCols = (seq->use_128x128_superblock) ?657((hdr->MiCols + 31) >> 5) : ((hdr->MiCols + 15) >> 4);658sbRows = (seq->use_128x128_superblock) ?659((hdr->MiRows + 31) >> 5) : ((hdr->MiRows + 15) >> 4);660width_sb = sbCols;661height_sb = sbRows;662sbSize = (seq->use_128x128_superblock ? 5 : 4) + 2;663maxTileWidthSb = AV1_MAX_TILE_WIDTH >> sbSize;664maxTileAreaSb = AV1_MAX_TILE_AREA >> (2 * sbSize);665minLog2TileCols = tile_log2(maxTileWidthSb, sbCols);666maxLog2TileCols = tile_log2(1, MIN2(sbCols, AV1_MAX_TILE_COLS));667maxLog2TileRows = tile_log2(1, MIN2(sbRows, AV1_MAX_TILE_ROWS));668minLog2Tiles = MAX2(minLog2TileCols, tile_log2(maxTileAreaSb, sbRows * sbCols));669670uniform_tile_spacing_flag = av1_f(vlc, 1);671if (uniform_tile_spacing_flag) {672unsigned tileWidthSb, tileHeightSb;673unsigned minLog2TileRows;674675ti->TileColsLog2 = minLog2TileCols;676while (ti->TileColsLog2 < maxLog2TileCols) {677bool increment_tile_cols_log2 = av1_f(vlc, 1);678if (increment_tile_cols_log2)679ti->TileColsLog2++;680else681break;682}683tileWidthSb = (sbCols + (1 << ti->TileColsLog2) - 1) >> ti->TileColsLog2;684i = 0;685for (startSb = 0; startSb < sbCols; startSb += tileWidthSb) {686ti->tile_col_start_sb[i] = startSb;687i++;688}689ti->tile_col_start_sb[i] = sbCols;690ti->TileCols = i;691692minLog2TileRows = (minLog2Tiles > ti->TileColsLog2)?693(minLog2Tiles - ti->TileColsLog2) : 0;694ti->TileRowsLog2 = minLog2TileRows;695while (ti->TileRowsLog2 < maxLog2TileRows) {696bool increment_tile_rows_log2 = av1_f(vlc, 1);697if (increment_tile_rows_log2)698ti->TileRowsLog2++;699else700break;701}702tileHeightSb = (sbRows + (1 << ti->TileRowsLog2) - 1) >> ti->TileRowsLog2;703i = 0;704for (startSb = 0; startSb < sbRows; startSb += tileHeightSb) {705ti->tile_row_start_sb[i] = startSb;706i++;707}708ti->tile_row_start_sb[i] = sbRows;709ti->TileRows = i;710} else {711unsigned widestTileSb = 0;712unsigned maxTileHeightSb;713714startSb = 0;715for (i = 0; startSb < sbCols; ++i) {716uint8_t maxWidth;717unsigned sizeSb;718unsigned width_in_sbs_minus_1;719720ti->tile_col_start_sb[i] = startSb;721maxWidth = MIN2(sbCols - startSb, maxTileWidthSb);722width_in_sbs_minus_1 = av1_ns(vlc, maxWidth);723sizeSb = width_in_sbs_minus_1 + 1;724widestTileSb = MAX2(sizeSb, widestTileSb);725startSb += sizeSb;726width_sb -= sizeSb;727}728ti->TileCols = i;729730ti->tile_col_start_sb[i] = startSb + width_sb;731ti->TileColsLog2 = tile_log2(1, ti->TileCols);732733if (minLog2Tiles > 0)734maxTileAreaSb = (sbRows * sbCols) >> (minLog2Tiles + 1);735else736maxTileAreaSb = (sbRows * sbCols);737maxTileHeightSb = MAX2(maxTileAreaSb / widestTileSb, 1);738739startSb = 0;740for (i = 0; startSb < sbRows; ++i) {741uint8_t maxHeight;742unsigned height_in_sbs_minus_1;743744maxHeight = MIN2(sbRows - startSb, maxTileHeightSb);745height_in_sbs_minus_1 = av1_ns(vlc, maxHeight);746ti->tile_row_start_sb[i] = startSb;747startSb += height_in_sbs_minus_1 + 1;748height_sb -= height_in_sbs_minus_1 + 1;749}750ti->TileRows = i;751ti->tile_row_start_sb[i] = startSb + height_sb;752ti->TileRowsLog2 = tile_log2(1, ti->TileRows);753}754755if (ti->TileColsLog2 > 0 || ti->TileRowsLog2 > 0) {756ti->context_update_tile_id =757av1_f(vlc, ti->TileRowsLog2 + ti->TileColsLog2);758uint8_t tile_size_bytes_minus_1 = av1_f(vlc, 2);759ti->TileSizeBytes = tile_size_bytes_minus_1 + 1;760} else {761ti->context_update_tile_id = 0;762}763}764765static int read_delta_q(struct vl_vlc *vlc)766{767bool delta_coded = av1_f(vlc, 1);768int delta_q = 0;769770if (delta_coded)771delta_q = av1_su(vlc, 7);772773return delta_q;774}775776static void quantization_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)777{778struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);779struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp);780bool using_qmatrix;781782qp->base_q_idx = av1_f(vlc, 8);783qp->DeltaQYDc = read_delta_q(vlc);784if (seq->color_config.NumPlanes > 1) {785bool diff_uv_delta =786(seq->color_config.separate_uv_delta_q) ? av1_f(vlc, 1) : 0;787788qp->DeltaQUDc = read_delta_q(vlc);789qp->DeltaQUAc = read_delta_q(vlc);790if (diff_uv_delta) {791qp->DeltaQVDc = read_delta_q(vlc);792qp->DeltaQVAc = read_delta_q(vlc);793} else {794qp->DeltaQVDc = qp->DeltaQUDc;795qp->DeltaQVAc = qp->DeltaQUAc;796}797} else {798qp->DeltaQVDc = 0;799qp->DeltaQVAc = 0;800qp->DeltaQUDc = 0;801qp->DeltaQUAc = 0;802}803804using_qmatrix = av1_f(vlc, 1);805if (using_qmatrix) {806qp->qm_y = av1_f(vlc, 4);807qp->qm_u = av1_f(vlc, 4);808if (!seq->color_config.separate_uv_delta_q)809qp->qm_v = qp->qm_u;810else811qp->qm_v = av1_f(vlc, 4);812} else {813qp->qm_y = 0xf;814qp->qm_u = 0xf;815qp->qm_v = 0xf;816}817}818819static void segmentation_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)820{821struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);822struct segmentation_params* sp = &(priv->codec_data.av1.uncompressed_header.sp);823int i, j;824825sp->segmentation_enabled = av1_f(vlc, 1);826if (sp->segmentation_enabled) {827bool segmentation_update_data;828829if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) {830sp->segmentation_update_map = 1;831sp->segmentation_temporal_update = 0;832segmentation_update_data = 1;833} else {834sp->segmentation_update_map = av1_f(vlc, 1);835if (sp->segmentation_update_map)836sp->segmentation_temporal_update = av1_f(vlc, 1);837else838sp->segmentation_temporal_update = 0;839segmentation_update_data = av1_f(vlc, 1);840}841842if (segmentation_update_data) {843uint8_t Segmentation_Feature_Bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };844bool Segmentation_Feature_Signed[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };845unsigned Segmentation_Feature_Max[AV1_SEG_LVL_MAX] = { 255, 63, 63, 63, 63, 7, 0, 0 };846847memset(sp->FeatureData, 0, sizeof(sp->FeatureData));848memset(sp->FeatureMask, 0, sizeof(sp->FeatureMask));849for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {850for (j = 0; j < AV1_SEG_LVL_MAX; ++j) {851int feature_value = 0;852bool feature_enabled = av1_f(vlc, 1);853854sp->FeatureEnabled[i][j] = feature_enabled;855int clippedValue = 0;856if (feature_enabled) {857uint8_t bitsToRead = Segmentation_Feature_Bits[j];858int limit = Segmentation_Feature_Max[j];859if (Segmentation_Feature_Signed[j]) {860feature_value = av1_su(vlc, 1 + bitsToRead);861clippedValue = CLAMP(feature_value, -limit, limit);862sp->FeatureMask[i] |= 1 << j;863} else {864feature_value = av1_f(vlc, bitsToRead);865clippedValue = CLAMP(feature_value, 0, limit);866sp->FeatureMask[i] |= 1 << j;867}868}869sp->FeatureData[i][j] = clippedValue;870}871}872} else {873int r = hdr->ref_frame_idx[hdr->primary_ref_frame];874memcpy(sp, &(priv->codec_data.av1.refs[r].sp), sizeof(*sp));875}876} else {877memset(sp, 0, sizeof(*sp));878}879}880881static void delta_q_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)882{883struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp);884struct delta_q_params * dqp = &(priv->codec_data.av1.uncompressed_header.dqp);885886dqp->delta_q_present = 0;887dqp->delta_q_res = 0;888if (qp->base_q_idx > 0)889dqp->delta_q_present = av1_f(vlc, 1);890if (dqp->delta_q_present)891dqp->delta_q_res = av1_f(vlc, 2);892}893894static void delta_lf_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)895{896struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);897struct delta_q_params * dqp = &(priv->codec_data.av1.uncompressed_header.dqp);898struct delta_lf_params* dlfp = &(priv->codec_data.av1.uncompressed_header.dlfp);899900dlfp->delta_lf_present = 0;901dlfp->delta_lf_res = 0;902dlfp->delta_lf_multi = 0;903if (dqp->delta_q_present) {904if (!hdr->allow_intrabc)905dlfp->delta_lf_present = av1_f(vlc, 1);906907if (dlfp->delta_lf_present) {908dlfp->delta_lf_res = av1_f(vlc, 2);909dlfp->delta_lf_multi = av1_f(vlc, 1);910}911}912}913914static unsigned get_qindex(vid_dec_PrivateType * priv, bool ignoreDeltaQ, unsigned segmentId)915{916struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);917struct segmentation_params* sp = &(priv->codec_data.av1.uncompressed_header.sp);918struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp);919unsigned qindex = 0;920921if (sp->segmentation_enabled && sp->FeatureEnabled[segmentId][AV1_SEG_LVL_ALT_Q]) {922unsigned data = sp->FeatureData[segmentId][AV1_SEG_LVL_ALT_Q];923qindex = qp->base_q_idx + data;924if (!ignoreDeltaQ && hdr->dqp.delta_q_present)925qindex = data;926927return CLAMP(qindex, 0, 255);928}929930if (!ignoreDeltaQ && hdr->dqp.delta_q_present)931return 0;932933return qp->base_q_idx;934}935936static void loop_filter_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)937{938struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);939struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);940struct loop_filter_params *lfp = &(priv->codec_data.av1.uncompressed_header.lfp);941int i;942943if (hdr->CodedLossless || hdr->allow_intrabc) {944lfp->loop_filter_level[0] = 0;945lfp->loop_filter_level[1] = 0;946lfp->loop_filter_ref_deltas[AV1_INTRA_FRAME] = 1;947lfp->loop_filter_ref_deltas[AV1_LAST_FRAME] = 0;948lfp->loop_filter_ref_deltas[AV1_LAST2_FRAME] = 0;949lfp->loop_filter_ref_deltas[AV1_LAST3_FRAME] = 0;950lfp->loop_filter_ref_deltas[AV1_BWDREF_FRAME] = 0;951lfp->loop_filter_ref_deltas[AV1_GOLDEN_FRAME] = -1;952lfp->loop_filter_ref_deltas[AV1_ALTREF2_FRAME] = -1;953lfp->loop_filter_ref_deltas[AV1_ALTREF_FRAME] = -1;954lfp->loop_filter_mode_deltas[0] = 0;955lfp->loop_filter_mode_deltas[1] = 0;956return;957}958959if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) {960lfp->loop_filter_ref_deltas[AV1_INTRA_FRAME] = 1;961lfp->loop_filter_ref_deltas[AV1_LAST_FRAME] = 0;962lfp->loop_filter_ref_deltas[AV1_LAST2_FRAME] = 0;963lfp->loop_filter_ref_deltas[AV1_LAST3_FRAME] = 0;964lfp->loop_filter_ref_deltas[AV1_BWDREF_FRAME] = 0;965lfp->loop_filter_ref_deltas[AV1_GOLDEN_FRAME] = -1;966lfp->loop_filter_ref_deltas[AV1_ALTREF2_FRAME] = -1;967lfp->loop_filter_ref_deltas[AV1_ALTREF_FRAME] = -1;968lfp->loop_filter_mode_deltas[0] = 0;969lfp->loop_filter_mode_deltas[1] = 0;970} else {971int r = hdr->ref_frame_idx[hdr->primary_ref_frame];972memcpy(lfp->loop_filter_ref_deltas,973priv->codec_data.av1.refs[r].lfp.loop_filter_ref_deltas, 8);974memcpy(lfp->loop_filter_mode_deltas,975priv->codec_data.av1.refs[r].lfp.loop_filter_mode_deltas, 2);976}977978lfp->loop_filter_level[0] = av1_f(vlc, 6);979lfp->loop_filter_level[1] = av1_f(vlc, 6);980if (seq->color_config.NumPlanes > 1) {981if (lfp->loop_filter_level[0] || lfp->loop_filter_level[1]) {982lfp->loop_filter_level[2] = av1_f(vlc, 6);983lfp->loop_filter_level[3] = av1_f(vlc, 6);984}985}986987lfp->loop_filter_sharpness = av1_f(vlc, 3);988lfp->loop_filter_delta_enabled = av1_f(vlc, 1);989if (lfp->loop_filter_delta_enabled) {990lfp->loop_filter_delta_update = av1_f(vlc, 1);991if (lfp->loop_filter_delta_update) {992for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {993int8_t update_ref_delta = av1_f(vlc, 1);994if (update_ref_delta)995lfp->loop_filter_ref_deltas[i] = av1_su(vlc, 7);996}997998for (i = 0; i < 2; ++i) {999int8_t update_mode_delta = av1_f(vlc, 1);1000if (update_mode_delta)1001lfp->loop_filter_mode_deltas[i] = av1_su(vlc, 7);1002}1003}1004}1005}10061007static void cdef_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1008{1009struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);1010struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1011struct cdef_params *cdefp = &(priv->codec_data.av1.uncompressed_header.cdefp);;1012int i;10131014if (hdr->CodedLossless || hdr->allow_intrabc || !seq->enable_cdef) {1015cdefp->cdef_bits = 0;1016cdefp->cdef_y_strengths[0] = 0;1017cdefp->cdef_uv_strengths[0] = 0;1018return;1019}10201021cdefp->cdef_damping_minus_3 = av1_f(vlc, 2);1022cdefp->cdef_bits = av1_f(vlc, 2);1023for (i = 0; i < (1 << cdefp->cdef_bits); ++i) {1024cdefp->cdef_y_strengths[i] = av1_f(vlc, 6);1025if (seq->color_config.NumPlanes > 1)1026cdefp->cdef_uv_strengths[i] = av1_f(vlc, 6);1027}1028}10291030static void lr_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1031{1032struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);1033struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1034struct loop_restoration_params *lrp = &(priv->codec_data.av1.uncompressed_header.lrp);1035uint8_t Remap_Lr_Type[4] =1036{ AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ };1037bool UsesLr = false;1038bool UsesChromaLr = false;1039uint8_t lr_unit_shift, lr_uv_shift;1040int i;10411042if (hdr->AllLossless || hdr->allow_intrabc || !seq->enable_restoration) {1043lrp->FrameRestorationType[0] = AV1_RESTORE_NONE;1044lrp->FrameRestorationType[1] = AV1_RESTORE_NONE;1045lrp->FrameRestorationType[2] = AV1_RESTORE_NONE;1046return;1047}10481049for (i = 0; i < seq->color_config.NumPlanes; ++i) {1050uint8_t lr_type = av1_f(vlc, 2);1051lrp->FrameRestorationType[i] = Remap_Lr_Type[lr_type];1052if (lrp->FrameRestorationType[i] != AV1_RESTORE_NONE) {1053UsesLr = true;1054if (i > 0)1055UsesChromaLr = true;1056}1057}10581059if (UsesLr) {1060if (seq->use_128x128_superblock) {1061lr_unit_shift = av1_f(vlc, 1) + 1;1062} else {1063lr_unit_shift = av1_f(vlc, 1);1064if (lr_unit_shift) {1065uint8_t lr_unit_extra_shift = av1_f(vlc, 1);1066lr_unit_shift += lr_unit_extra_shift;1067}1068}10691070lrp->LoopRestorationSize[0] = AV1_RESTORATION_TILESIZE >> (2 - lr_unit_shift);1071lr_uv_shift =1072(seq->color_config.subsampling_x && seq->color_config.subsampling_y && UsesChromaLr) ?1073av1_f(vlc, 1) : 0;10741075lrp->LoopRestorationSize[1] = lrp->LoopRestorationSize[0] >> lr_uv_shift;1076lrp->LoopRestorationSize[2] = lrp->LoopRestorationSize[0] >> lr_uv_shift;1077} else {1078lrp->LoopRestorationSize[0] = lrp->LoopRestorationSize[1] =1079lrp->LoopRestorationSize[2] = (1 << 8);1080}1081}10821083static void tx_mode(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1084{1085struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1086struct tx_mode_params *tm = &(priv->codec_data.av1.uncompressed_header.tm);10871088if (hdr->CodedLossless) {1089tm->TxMode = AV1_ONLY_4X4;1090} else {1091bool tx_mode_select = av1_f(vlc, 1);1092tm->TxMode = (tx_mode_select) ? AV1_TX_MODE_SELECT : AV1_TX_MODE_LARGEST;1093}1094}10951096static void frame_reference_mode(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1097{1098struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);10991100if (hdr->FrameIsIntra)1101hdr->reference_select = SINGLE_REFERENCE;1102else1103hdr->reference_select = av1_f(vlc, 1) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;1104}11051106static void skip_mode_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1107{1108struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);1109struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1110struct skip_mode_params *smp = &(priv->codec_data.av1.uncompressed_header.smp);;1111bool skipModeAllowed;1112int i;11131114if (hdr->FrameIsIntra || hdr->reference_select == SINGLE_REFERENCE ||1115!seq->enable_order_hint) {1116skipModeAllowed = 0;1117} else {1118int ref_frame_offset[2] = { -1, INT_MAX };1119int ref_idx[2] = { -1, -1 };11201121skipModeAllowed = 0;1122for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {1123unsigned ref_offset = priv->codec_data.av1.refs[hdr->ref_frame_idx[i]].OrderHint;1124if (get_relative_dist(priv, ref_offset, hdr->OrderHint) < 0) {1125if (ref_frame_offset[0] == -1 ||1126get_relative_dist(priv, ref_offset, ref_frame_offset[0]) > 0) {1127ref_frame_offset[0] = ref_offset;1128ref_idx[0] = i;1129}1130} else if (get_relative_dist(priv, ref_offset, hdr->OrderHint) > 0) {1131if (ref_frame_offset[1] == INT_MAX ||1132get_relative_dist(priv, ref_offset, ref_frame_offset[1]) < 0) {1133ref_frame_offset[1] = ref_offset;1134ref_idx[1] = i;1135}1136}1137}11381139if (ref_idx[0] != -1 && ref_idx[1] != -1) {1140skipModeAllowed = 1;1141} else if (ref_idx[0] != -1 && ref_idx[1] == -1) {1142ref_frame_offset[1] = -1;1143for (i = 0; i < AV1_ALTREF_FRAME - AV1_LAST_FRAME + 1; ++i) {1144unsigned ref_offset = priv->codec_data.av1.refs[hdr->ref_frame_idx[i]].OrderHint;1145if ((ref_frame_offset[0] != -1 &&1146get_relative_dist(priv, ref_offset, ref_frame_offset[0]) < 0) &&1147(ref_frame_offset[1] == -1 ||1148get_relative_dist(priv, ref_offset, ref_frame_offset[1]) > 0)) {1149ref_frame_offset[1] = ref_offset;1150ref_idx[1] = i;1151}1152}1153if (ref_frame_offset[1] != -1)1154skipModeAllowed = 1;1155}1156}11571158smp->skip_mode_present = skipModeAllowed ? av1_f(vlc, 1) : 0;1159}11601161static unsigned inverse_recenter(unsigned r, unsigned v)1162{1163if (v > (2 * r))1164return v;1165else if (v & 1)1166return (r - ((v + 1) >> 1));1167else1168return (r + (v >> 1));1169}11701171static unsigned decode_subexp(struct vl_vlc *vlc, unsigned numSyms)1172{1173unsigned i = 0;1174unsigned mk = 0;1175unsigned k = 3;11761177while (1) {1178unsigned b2 = (i) ? (k + i - 1) : k;1179unsigned a = 1 << b2;1180if (numSyms <= (mk + 3 * a)) {1181unsigned subexp_final_bits = av1_ns(vlc, (numSyms - mk));1182return (subexp_final_bits + mk);1183} else {1184bool subexp_more_bits = av1_f(vlc, 1);1185if (subexp_more_bits) {1186i++;1187mk += a;1188} else {1189unsigned subexp_bits = av1_f(vlc, b2);1190return (subexp_bits + mk);1191}1192}1193}1194}11951196static unsigned decode_unsigned_subexp_with_ref(struct vl_vlc *vlc,1197unsigned mx, unsigned r)1198{1199unsigned smart;1200unsigned v = decode_subexp(vlc, mx);12011202if ((r << 1) <= mx) {1203smart = inverse_recenter(r, v);1204return smart;1205} else {1206smart = inverse_recenter(mx - 1 - r, v);1207return (mx - 1 - smart);1208}1209}12101211static int decode_signed_subexp_with_ref(struct vl_vlc *vlc, int low, int high, int r)1212{1213int x = decode_unsigned_subexp_with_ref(vlc, high - low, r - low);12141215return (x + low);1216}12171218static void read_global_param(struct global_motion_params* global_params,1219struct global_motion_params* ref_params,1220vid_dec_PrivateType *priv, struct vl_vlc *vlc,1221uint8_t type, uint8_t ref, uint8_t idx)1222{1223struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1224uint8_t absBits = 12; /* GM_ABS_ALPHA_BITS */1225uint8_t precBits = 15; /* GM_ALPHA_PREC_BITS */1226int precDiff, round, sub, mx, r = 0;12271228if (idx < 2) {1229if (type == AV1_TRANSLATION) {1230absBits = 9 /* GM_ABS_TRANS_ONLY_BITS */ - !hdr->allow_high_precision_mv;1231precBits = 3 /* GM_TRANS_ONLY_PREC_BITS */ - !hdr->allow_high_precision_mv;1232} else {1233absBits = 12; /* GM_ABS_TRANS_BITS */1234precBits = 6; /* GM_TRANS_PREC_BITS */;1235}1236}12371238precDiff = AV1_WARPEDMODEL_PREC_BITS - precBits;1239round = ((idx % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;1240sub = ((idx % 3) == 2) ? (1 << precBits) : 0;1241mx = (int)(1 << absBits);1242if (ref_params)1243r = (ref_params->gm_params[ref][idx] >> precDiff) - sub;12441245global_params->gm_params[ref][idx] =1246(decode_signed_subexp_with_ref(vlc, -mx, mx + 1, r) << precDiff) + round;1247}12481249static void global_motion_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1250{1251struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1252struct global_motion_params *gmp = &(priv->codec_data.av1.uncompressed_header.gmp);1253struct global_motion_params *ref_gmp = NULL;1254unsigned ref, i;12551256if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) {1257for (ref = 0; ref < AV1_NUM_REF_FRAMES; ++ref) {1258gmp->GmType[ref] = AV1_IDENTITY;1259for (i = 0; i < 6; ++i)1260gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0);1261}1262} else {1263const int r = hdr->ref_frame_idx[hdr->primary_ref_frame];1264ref_gmp = &(priv->codec_data.av1.refs[r].gmp);1265}12661267for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ++ref) {1268gmp->GmType[ref] = AV1_IDENTITY;1269for (i = 0; i < 6; ++i)1270gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0);1271}12721273if (hdr->FrameIsIntra)1274return;12751276for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ++ref) {1277uint8_t type = AV1_IDENTITY;1278bool is_global;12791280gmp->GmType[ref] = AV1_IDENTITY;1281for (i = 0; i < 6; ++i)1282gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0);12831284is_global = av1_f(vlc, 1);1285if (is_global) {1286bool is_rot_zoom = av1_f(vlc, 1);1287if (is_rot_zoom) {1288type = AV1_ROTZOOM;1289} else {1290bool is_translation = av1_f(vlc, 1);1291type = is_translation ? AV1_TRANSLATION : AV1_AFFINE;1292}1293}12941295gmp->GmType[ref] = type;12961297if (type >= AV1_ROTZOOM) {1298read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 2);1299read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 3);1300if (type == AV1_AFFINE) {1301read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 4);1302read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 5);1303} else {1304gmp->gm_params[ref][4] = -gmp->gm_params[ref][3];1305gmp->gm_params[ref][5] = gmp->gm_params[ref][2];1306}1307}13081309if (type >= AV1_TRANSLATION) {1310read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 0);1311read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 1);1312}1313}1314}13151316static void film_grain_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1317{1318struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);1319struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1320struct film_grain_params *fgp = &(priv->codec_data.av1.uncompressed_header.fgp);13211322bool update_grain;1323uint8_t numPosLuma;1324uint8_t numPosChroma;1325unsigned i;13261327if (!seq->film_grain_params_present ||1328(!hdr->show_frame && !hdr->showable_frame)) {1329memset(fgp, 0, sizeof(*fgp));1330return;1331}13321333fgp->apply_grain = av1_f(vlc, 1);1334if (!fgp->apply_grain) {1335memset(fgp, 0, sizeof(*fgp));1336return;1337}13381339fgp->grain_seed = av1_f(vlc, 16);1340update_grain =1341(hdr->frame_type == AV1_INTER_FRAME) ? av1_f(vlc, 1) : 1;13421343if (!update_grain) {1344uint8_t film_grain_params_ref_idx = av1_f(vlc, 3);1345uint16_t tempGrainSeed = fgp->grain_seed;1346memcpy(fgp, &(priv->codec_data.av1.refs[film_grain_params_ref_idx].fgp),1347sizeof(*fgp));1348fgp->grain_seed = tempGrainSeed;1349return;1350}13511352fgp->num_y_points = av1_f(vlc, 4);1353for (i = 0; i < fgp->num_y_points; ++i) {1354fgp->point_y_value[i] = av1_f(vlc, 8);1355fgp->point_y_scaling[i] = av1_f(vlc, 8);1356}13571358fgp->chroma_scaling_from_luma =1359(seq->color_config.mono_chrome) ? 0 : av1_f(vlc, 1);1360if (seq->color_config.mono_chrome || fgp->chroma_scaling_from_luma ||1361(seq->color_config.subsampling_x && seq->color_config.subsampling_y &&1362(fgp->num_y_points == 0))) {1363fgp->num_cb_points = 0;1364fgp->num_cr_points = 0;1365} else {1366fgp->num_cb_points = av1_f(vlc, 4);1367for (i = 0; i < fgp->num_cb_points; ++i) {1368fgp->point_cb_value[i] = av1_f(vlc, 8);1369fgp->point_cb_scaling[i] = av1_f(vlc, 8);1370}1371fgp->num_cr_points = av1_f(vlc, 4);1372for (i = 0; i < fgp->num_cr_points; ++i) {1373fgp->point_cr_value[i] = av1_f(vlc, 8);1374fgp->point_cr_scaling[i] = av1_f(vlc, 8);1375}1376}13771378fgp->grain_scaling_minus_8 = av1_f(vlc, 2);1379fgp->ar_coeff_lag = av1_f(vlc, 2);1380numPosLuma = 2 * fgp->ar_coeff_lag * (fgp->ar_coeff_lag + 1);1381if (fgp->num_y_points) {1382numPosChroma = numPosLuma + 1;1383for (i = 0; i < numPosLuma; ++i) {1384uint8_t ar_coeffs_y_plus_128 = av1_f(vlc, 8);1385fgp->ar_coeffs_y[i] = ar_coeffs_y_plus_128 - 128;1386}1387} else {1388numPosChroma = numPosLuma;1389}13901391if (fgp->chroma_scaling_from_luma || fgp->num_cb_points) {1392for (i = 0; i < numPosChroma; ++i) {1393uint8_t ar_coeffs_cb_plus_128 = av1_f(vlc, 8);1394fgp->ar_coeffs_cb[i] = ar_coeffs_cb_plus_128 - 128;1395}1396}13971398if (fgp->chroma_scaling_from_luma || fgp->num_cr_points) {1399for (i = 0; i < numPosChroma; ++i) {1400uint8_t ar_coeffs_cr_plus_128 = av1_f(vlc, 8);1401fgp->ar_coeffs_cr[i] = ar_coeffs_cr_plus_128 - 128;1402}1403}14041405fgp->ar_coeff_shift_minus_6 = av1_f(vlc, 2);1406fgp->grain_scale_shift = av1_f(vlc, 2);1407if (fgp->num_cb_points) {1408fgp->cb_mult = av1_f(vlc, 8);1409fgp->cb_luma_mult = av1_f(vlc, 8);1410fgp->cb_offset = av1_f(vlc, 9);1411}14121413if (fgp->num_cr_points) {1414fgp->cr_mult = av1_f(vlc, 8);1415fgp->cr_luma_mult = av1_f(vlc, 8);1416fgp->cr_offset = av1_f(vlc, 9);1417}14181419fgp->overlap_flag = av1_f(vlc, 1);1420fgp->clip_to_restricted_range = av1_f(vlc, 1);1421}14221423static void frame_header_obu(vid_dec_PrivateType *priv, struct vl_vlc *vlc)1424{1425struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);1426struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);1427unsigned idLen = 0;1428unsigned allFrames;1429int i, j;14301431memset(hdr, 0, sizeof(*hdr));14321433if (seq->frame_id_numbers_present_flag)1434idLen = seq->additional_frame_id_length_minus_1 +1435seq->delta_frame_id_length_minus_2 + 3;14361437allFrames = (1 << AV1_NUM_REF_FRAMES) - 1;1438if (seq->reduced_still_picture_header) {1439hdr->show_existing_frame = 0;1440hdr->frame_type = AV1_KEY_FRAME;1441hdr->FrameIsIntra = 1;1442hdr->show_frame = 1;1443hdr->showable_frame = 0;1444} else {1445hdr->show_existing_frame = av1_f(vlc, 1);1446if (hdr->show_existing_frame) {1447hdr->frame_to_show_map_idx = av1_f(vlc, 3);1448if (seq->decoder_model_info_present_flag &&1449!seq->timing_info.equal_picture_interval)1450av1_f(vlc, seq->decoder_model_info.1451frame_presentation_time_length_minus_1 + 1);1452hdr->refresh_frame_flags = 0;1453if (seq->frame_id_numbers_present_flag)1454av1_f(vlc, idLen); /* display_frame_id */14551456hdr->frame_type =1457priv->codec_data.av1.RefFrames[priv->codec_data.av1.uncompressed_header.1458frame_to_show_map_idx].RefFrameType;14591460return;1461}14621463hdr->frame_type = av1_f(vlc, 2);1464hdr->FrameIsIntra = (hdr->frame_type == AV1_INTRA_ONLY_FRAME ||1465hdr->frame_type == AV1_KEY_FRAME);1466hdr->show_frame = av1_f(vlc, 1);14671468if (hdr->show_frame && seq->decoder_model_info_present_flag &&1469!seq->timing_info.equal_picture_interval)1470av1_f(vlc, seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1);14711472hdr->showable_frame =1473hdr->show_frame ? (hdr->frame_type != AV1_KEY_FRAME) : av1_f(vlc, 1);14741475hdr->error_resilient_mode = (hdr->frame_type == AV1_SWITCH_FRAME ||1476(hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame)) ? 1 : av1_f(vlc, 1);1477}14781479if (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame) {1480for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)1481hdr->RefOrderHint[i] = 0;1482}14831484hdr->disable_cdf_update = av1_f(vlc, 1);14851486hdr->allow_screen_content_tools =1487(seq->seq_force_screen_content_tools == AV1_SELECT_SCREEN_CONTENT_TOOLS) ?1488av1_f(vlc, 1) : seq->seq_force_screen_content_tools;14891490if (hdr->allow_screen_content_tools) {1491if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)1492hdr->force_integer_mv = av1_f(vlc, 1);1493else1494hdr->force_integer_mv = seq->seq_force_integer_mv;1495} else {1496hdr->force_integer_mv = 0;1497}14981499if (hdr->FrameIsIntra)1500hdr->force_integer_mv = 1;15011502hdr->current_frame_id =1503seq->frame_id_numbers_present_flag ? av1_f(vlc, idLen) : 0;15041505if (hdr->frame_type == AV1_SWITCH_FRAME)1506hdr->frame_size_override_flag = 1;1507else if (seq->reduced_still_picture_header)1508hdr->frame_size_override_flag = 0;1509else1510hdr->frame_size_override_flag = av1_f(vlc, 1);15111512hdr->OrderHint = av1_f(vlc, seq->OrderHintBits);15131514if (hdr->FrameIsIntra || hdr->error_resilient_mode)1515hdr->primary_ref_frame = AV1_PRIMARY_REF_NONE;1516else1517hdr->primary_ref_frame = av1_f(vlc, 3);15181519if (seq->decoder_model_info_present_flag) {1520bool buffer_removal_time_present_flag = av1_f(vlc, 1);1521if (buffer_removal_time_present_flag) {1522for (i = 0; i <= seq->operating_points_cnt_minus_1; ++i) {1523if (seq->decoder_model_present_for_this_op[i]) {1524unsigned opPtIdc;1525bool inTemporalLayer;1526bool inSpatialLayer;1527opPtIdc = seq->operating_point_idc[i];1528inTemporalLayer =1529(opPtIdc >> priv->codec_data.av1.ext.temporal_id) & 1;1530inSpatialLayer =1531(opPtIdc >> (priv->codec_data.av1.ext.spatial_id + 8)) & 1;1532if ((opPtIdc == 0) || (inTemporalLayer && inSpatialLayer))1533av1_f(vlc, seq->decoder_model_info.1534buffer_removal_time_length_minus_1 + 1);1535}1536}1537}1538}15391540hdr->allow_high_precision_mv = 0;1541hdr->use_ref_frame_mvs = 0;1542hdr->allow_intrabc = 0;15431544hdr->refresh_frame_flags = allFrames = (hdr->frame_type == AV1_SWITCH_FRAME ||1545(hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame)) ?1546allFrames : av1_f(vlc, AV1_NUM_REF_FRAMES);15471548if (!hdr->FrameIsIntra || hdr->refresh_frame_flags != allFrames) {1549if (hdr->error_resilient_mode && seq->enable_order_hint) {1550for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)1551av1_f(vlc, seq->OrderHintBits);1552}1553}15541555if (hdr->FrameIsIntra) {1556frame_size(priv, vlc);1557render_size(priv, vlc);1558if (hdr->allow_screen_content_tools && (hdr->UpscaledWidth == hdr->FrameWidth))1559hdr->allow_intrabc = av1_f(vlc, 1);1560} else {1561bool is_filter_switchable;1562bool frame_refs_short_signaling;15631564if (!seq->enable_order_hint) {1565frame_refs_short_signaling = 0;1566} else {1567frame_refs_short_signaling = av1_f(vlc, 1);1568if (frame_refs_short_signaling) {1569hdr->last_frame_idx = av1_f(vlc, 3);1570hdr->gold_frame_idx = av1_f(vlc, 3);1571set_frame_refs(priv, vlc);1572}1573}15741575for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {1576if (!frame_refs_short_signaling)1577hdr->ref_frame_idx[i] = av1_f(vlc, 3);1578if (seq->frame_id_numbers_present_flag)1579av1_f(vlc, seq->delta_frame_id_length_minus_2 + 2);1580}15811582if (hdr->frame_size_override_flag && !hdr->error_resilient_mode) {1583frame_size_with_refs(priv, vlc);1584} else {1585frame_size(priv, vlc);1586render_size(priv, vlc);1587}15881589hdr->allow_high_precision_mv = hdr->force_integer_mv ? 0 : av1_f(vlc, 1);15901591is_filter_switchable = av1_f(vlc, 1);1592hdr->interpolation_filter = is_filter_switchable ? 4 /* SWITCHABLE */ : av1_f(vlc, 2);15931594hdr->is_motion_mode_switchable = av1_f(vlc, 1);1595hdr->use_ref_frame_mvs =1596(hdr->error_resilient_mode || !seq->enable_ref_frame_mvs) ? 0 : av1_f(vlc, 1);1597}15981599hdr->disable_frame_end_update_cdf =1600(seq->reduced_still_picture_header || hdr->disable_cdf_update) ? 1 : av1_f(vlc, 1);16011602tile_info(priv, vlc);1603quantization_params(priv, vlc);1604segmentation_params(priv, vlc);1605delta_q_params(priv, vlc);1606delta_lf_params(priv, vlc);16071608hdr->CodedLossless = 1;1609for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {1610unsigned qindex = get_qindex(priv, 1, i);1611bool LosslessArray =1612(qindex == 0) && (hdr->qp.DeltaQYDc == 0) &&1613(hdr->qp.DeltaQUAc == 0) && (hdr->qp.DeltaQUDc == 0) &&1614(hdr->qp.DeltaQVAc == 0) && (hdr->qp.DeltaQVDc == 0);16151616if (!LosslessArray)1617hdr->CodedLossless = 0;1618}1619hdr->AllLossless = hdr->CodedLossless && (hdr->FrameWidth == hdr->UpscaledWidth);16201621loop_filter_params(priv, vlc);1622cdef_params(priv, vlc);1623lr_params(priv, vlc);1624tx_mode(priv, vlc);1625frame_reference_mode(priv, vlc);1626skip_mode_params(priv, vlc);16271628if (hdr->FrameIsIntra || hdr->error_resilient_mode || !seq->enable_warped_motion)1629hdr->allow_warped_motion = 0;1630else1631hdr->allow_warped_motion = av1_f(vlc, 1);1632hdr->reduced_tx_set = av1_f(vlc, 1);16331634global_motion_params(priv, vlc);16351636film_grain_params(priv, vlc);16371638priv->picture.av1.picture_parameter.pic_info_fields.frame_type = hdr->frame_type;1639priv->picture.av1.picture_parameter.pic_info_fields.show_frame = hdr->show_frame;1640priv->picture.av1.picture_parameter.pic_info_fields.error_resilient_mode =1641hdr->error_resilient_mode;1642priv->picture.av1.picture_parameter.pic_info_fields.disable_cdf_update =1643hdr->disable_cdf_update;1644priv->picture.av1.picture_parameter.pic_info_fields.allow_screen_content_tools =1645hdr->allow_screen_content_tools;1646priv->picture.av1.picture_parameter.pic_info_fields.force_integer_mv =1647hdr->force_integer_mv;1648priv->picture.av1.picture_parameter.current_frame_id = hdr->current_frame_id;1649priv->picture.av1.picture_parameter.order_hint = hdr->OrderHint;1650priv->picture.av1.picture_parameter.primary_ref_frame = hdr->primary_ref_frame;1651priv->picture.av1.picture_parameter.frame_width = hdr->FrameWidth;1652priv->picture.av1.picture_parameter.frame_height = hdr->FrameHeight;1653priv->picture.av1.picture_parameter.pic_info_fields.use_superres =1654hdr->use_superres;1655priv->picture.av1.picture_parameter.superres_scale_denominator =1656hdr->SuperresDenom;16571658for (i = 0; i < AV1_REFS_PER_FRAME; ++i)1659priv->picture.av1.picture_parameter.ref_frame_idx[i] = hdr->ref_frame_idx[i];16601661priv->picture.av1.picture_parameter.pic_info_fields.allow_high_precision_mv =1662hdr->allow_high_precision_mv;1663priv->picture.av1.picture_parameter.pic_info_fields.allow_intrabc = hdr->allow_intrabc;1664priv->picture.av1.picture_parameter.pic_info_fields.use_ref_frame_mvs =1665hdr->use_ref_frame_mvs;1666priv->picture.av1.picture_parameter.interp_filter = hdr->interpolation_filter;1667priv->picture.av1.picture_parameter.pic_info_fields.is_motion_mode_switchable =1668hdr->is_motion_mode_switchable;1669priv->picture.av1.picture_parameter.refresh_frame_flags =1670hdr->refresh_frame_flags;1671priv->picture.av1.picture_parameter.pic_info_fields.disable_frame_end_update_cdf =1672hdr->disable_frame_end_update_cdf;16731674/* Tile Info */1675priv->picture.av1.picture_parameter.tile_rows = hdr->ti.TileRows;1676priv->picture.av1.picture_parameter.tile_cols = hdr->ti.TileCols;1677priv->picture.av1.picture_parameter.context_update_tile_id =1678hdr->ti.context_update_tile_id;1679for (i = 0; i <AV1_MAX_TILE_ROWS; ++i)1680priv->picture.av1.picture_parameter.tile_row_start_sb[i] =1681hdr->ti.tile_row_start_sb[i];1682for (i = 0; i <AV1_MAX_TILE_COLS; ++i)1683priv->picture.av1.picture_parameter.tile_col_start_sb[i] =1684hdr->ti.tile_col_start_sb[i];16851686/* Quantization Params */1687priv->picture.av1.picture_parameter.base_qindex = hdr->qp.base_q_idx;1688priv->picture.av1.picture_parameter.y_dc_delta_q = hdr->qp.DeltaQYDc;1689priv->picture.av1.picture_parameter.u_dc_delta_q = hdr->qp.DeltaQUDc;1690priv->picture.av1.picture_parameter.u_ac_delta_q = hdr->qp.DeltaQUAc;1691priv->picture.av1.picture_parameter.v_dc_delta_q = hdr->qp.DeltaQVDc;1692priv->picture.av1.picture_parameter.v_ac_delta_q = hdr->qp.DeltaQVAc;1693priv->picture.av1.picture_parameter.qmatrix_fields.qm_y = hdr->qp.qm_y;1694priv->picture.av1.picture_parameter.qmatrix_fields.qm_u = hdr->qp.qm_u;1695priv->picture.av1.picture_parameter.qmatrix_fields.qm_v = hdr->qp.qm_v;16961697/* Segmentation Params */1698priv->picture.av1.picture_parameter.seg_info.segment_info_fields.enabled =1699hdr->sp.segmentation_enabled;1700priv->picture.av1.picture_parameter.seg_info.segment_info_fields.update_map =1701hdr->sp.segmentation_update_map;1702priv->picture.av1.picture_parameter.seg_info.segment_info_fields.temporal_update =1703hdr->sp.segmentation_temporal_update;1704for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {1705for (j = 0; j < AV1_SEG_LVL_MAX; ++j)1706priv->picture.av1.picture_parameter.seg_info.feature_data[i][j] =1707hdr->sp.FeatureData[i][j];1708priv->picture.av1.picture_parameter.seg_info.feature_mask[i] =1709hdr->sp.FeatureMask[i];1710}17111712/* Delta Q Params */1713priv->picture.av1.picture_parameter.mode_control_fields.delta_q_present_flag =1714hdr->dqp.delta_q_present;1715priv->picture.av1.picture_parameter.mode_control_fields.log2_delta_q_res =1716hdr->dqp.delta_q_res;17171718/* Delta LF Params */1719priv->picture.av1.picture_parameter.mode_control_fields.delta_lf_present_flag =1720hdr->dlfp.delta_lf_present;1721priv->picture.av1.picture_parameter.mode_control_fields.log2_delta_lf_res =1722hdr->dlfp.delta_lf_res;1723priv->picture.av1.picture_parameter.mode_control_fields.delta_lf_multi =1724hdr->dlfp.delta_lf_multi;17251726/* Loop Filter Params */1727for (i = 0; i < 2; ++i)1728priv->picture.av1.picture_parameter.filter_level[i] = hdr->lfp.loop_filter_level[i];1729priv->picture.av1.picture_parameter.filter_level_u = hdr->lfp.loop_filter_level[2];1730priv->picture.av1.picture_parameter.filter_level_v = hdr->lfp.loop_filter_level[3];1731priv->picture.av1.picture_parameter.loop_filter_info_fields.sharpness_level =1732hdr->lfp.loop_filter_sharpness;1733priv->picture.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_enabled =1734hdr->lfp.loop_filter_delta_enabled;1735priv->picture.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_update =1736hdr->lfp.loop_filter_delta_update;1737for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)1738priv->picture.av1.picture_parameter.ref_deltas[i] =1739hdr->lfp.loop_filter_ref_deltas[i];1740for (i = 0; i < 2; ++i)1741priv->picture.av1.picture_parameter.mode_deltas[i] =1742hdr->lfp.loop_filter_mode_deltas[i];17431744/* CDEF Params */1745priv->picture.av1.picture_parameter.cdef_damping_minus_3 =1746hdr->cdefp.cdef_damping_minus_3;1747priv->picture.av1.picture_parameter.cdef_bits = hdr->cdefp.cdef_bits;1748for (i = 0; i < AV1_MAX_CDEF_BITS_ARRAY; ++i) {1749priv->picture.av1.picture_parameter.cdef_y_strengths[i] =1750hdr->cdefp.cdef_y_strengths[i];1751priv->picture.av1.picture_parameter.cdef_uv_strengths[i] =1752hdr->cdefp.cdef_uv_strengths[i];1753}17541755/* Loop Restoration Params */1756priv->picture.av1.picture_parameter.loop_restoration_fields.yframe_restoration_type =1757hdr->lrp.FrameRestorationType[0];1758priv->picture.av1.picture_parameter.loop_restoration_fields.cbframe_restoration_type =1759hdr->lrp.FrameRestorationType[1];1760priv->picture.av1.picture_parameter.loop_restoration_fields.crframe_restoration_type =1761hdr->lrp.FrameRestorationType[2];1762for (i = 0; i < 3; ++i)1763priv->picture.av1.picture_parameter.lr_unit_size[i] = hdr->lrp.LoopRestorationSize[i];17641765priv->picture.av1.picture_parameter.mode_control_fields.tx_mode = hdr->tm.TxMode;1766priv->picture.av1.picture_parameter.mode_control_fields.reference_select =1767(hdr->reference_select == REFERENCE_MODE_SELECT) ? COMPOUND_REFERENCE : SINGLE_REFERENCE;1768priv->picture.av1.picture_parameter.mode_control_fields.skip_mode_present =1769hdr->smp.skip_mode_present;1770priv->picture.av1.picture_parameter.pic_info_fields.allow_warped_motion =1771hdr->allow_warped_motion;1772priv->picture.av1.picture_parameter.mode_control_fields.reduced_tx_set_used =1773hdr->reduced_tx_set;17741775/* Global Motion Params */1776for (i = 0; i < 7; ++i) {1777priv->picture.av1.picture_parameter.wm[i].wmtype = hdr->gmp.GmType[i + 1];1778for (j = 0; j < 6; ++j)1779priv->picture.av1.picture_parameter.wm[i].wmmat[j] = hdr->gmp.gm_params[i + 1][j];1780}17811782/* Film Grain Params */1783priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.apply_grain =1784hdr->fgp.apply_grain;1785priv->picture.av1.picture_parameter.film_grain_info.grain_seed =1786hdr->fgp.grain_seed;1787priv->picture.av1.picture_parameter.film_grain_info.num_y_points =1788hdr->fgp.num_y_points;1789for (i = 0; i < AV1_FG_MAX_NUM_Y_POINTS; ++i) {1790priv->picture.av1.picture_parameter.film_grain_info.point_y_value[i] =1791hdr->fgp.point_y_value[i];1792priv->picture.av1.picture_parameter.film_grain_info.point_y_scaling[i] =1793hdr->fgp.point_y_scaling[i];1794}1795priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1796chroma_scaling_from_luma = hdr->fgp.chroma_scaling_from_luma;1797priv->picture.av1.picture_parameter.film_grain_info.num_cb_points =1798hdr->fgp.num_cb_points;1799priv->picture.av1.picture_parameter.film_grain_info.num_cr_points =1800hdr->fgp.num_cr_points;1801for (i = 0; i < AV1_FG_MAX_NUM_CBR_POINTS; ++i) {1802priv->picture.av1.picture_parameter.film_grain_info.point_cb_value[i] =1803hdr->fgp.point_cb_value[i];1804priv->picture.av1.picture_parameter.film_grain_info.point_cb_scaling[i] =1805hdr->fgp.point_cb_scaling[i];1806priv->picture.av1.picture_parameter.film_grain_info.point_cr_value[i] =1807hdr->fgp.point_cr_value[i];1808priv->picture.av1.picture_parameter.film_grain_info.point_cr_scaling[i] =1809hdr->fgp.point_cr_scaling[i];1810}1811priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1812grain_scaling_minus_8 = hdr->fgp.grain_scaling_minus_8;1813priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1814ar_coeff_lag = hdr->fgp.ar_coeff_lag;1815for (i = 0; i < AV1_FG_MAX_NUM_POS_LUMA; ++i)1816priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_y[i] =1817hdr->fgp.ar_coeffs_y[i];1818for (i = 0; i < AV1_FG_MAX_NUM_POS_CHROMA; ++i) {1819priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_cb[i] =1820hdr->fgp.ar_coeffs_cb[i];1821priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_cr[i] =1822hdr->fgp.ar_coeffs_cr[i];1823}1824priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1825ar_coeff_shift_minus_6 = hdr->fgp.ar_coeff_shift_minus_6;1826priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1827grain_scale_shift = hdr->fgp.grain_scale_shift;1828priv->picture.av1.picture_parameter.film_grain_info.cb_mult = hdr->fgp.cb_mult;1829priv->picture.av1.picture_parameter.film_grain_info.cb_luma_mult = hdr->fgp.cb_luma_mult;1830priv->picture.av1.picture_parameter.film_grain_info.cb_offset = hdr->fgp.cb_offset;1831priv->picture.av1.picture_parameter.film_grain_info.cr_mult = hdr->fgp.cr_mult;1832priv->picture.av1.picture_parameter.film_grain_info.cr_luma_mult = hdr->fgp.cr_luma_mult;1833priv->picture.av1.picture_parameter.film_grain_info.cr_offset = hdr->fgp.cr_offset;1834priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1835overlap_flag = hdr->fgp.overlap_flag;1836priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.1837clip_to_restricted_range = hdr->fgp.clip_to_restricted_range;1838}18391840static void parse_tile_hdr(vid_dec_PrivateType *priv, struct vl_vlc *vlc,1841unsigned start_bits_pos, unsigned total_obu_len)1842{1843struct tile_info *ti = &(priv->codec_data.av1.uncompressed_header.ti);1844unsigned tg_start, tg_end;1845unsigned NumTiles, tileBits;1846bool tile_start_and_end_present_flag;1847unsigned size[AV1_MAX_NUM_TILES] = { 0 };1848unsigned offset[AV1_MAX_NUM_TILES] = { 0 };1849unsigned frame_header_size, left_size;1850unsigned i, j;18511852NumTiles = ti->TileCols * ti->TileRows;1853tile_start_and_end_present_flag = 0;1854if (NumTiles > 1)1855tile_start_and_end_present_flag = av1_f(vlc, 1);18561857if (NumTiles == 1 || !tile_start_and_end_present_flag) {1858tg_start = 0;1859tg_end = NumTiles - 1;1860} else {1861tileBits = ti->TileColsLog2 + ti->TileRowsLog2;1862tg_start = av1_f(vlc, tileBits);1863tg_end = av1_f(vlc, tileBits);1864}18651866av1_byte_alignment(vlc);18671868frame_header_size = (start_bits_pos - vl_vlc_bits_left(vlc)) / 8;1869left_size = total_obu_len - frame_header_size;1870for (i = tg_start; i <= tg_end; ++i) {1871if (i == tg_start) {1872offset[i] = priv->codec_data.av1.bs_obu_td_sz +1873priv->codec_data.av1.bs_obu_seq_sz + frame_header_size +1874ti->TileSizeBytes;1875if (tg_start == tg_end) {1876size[i] = left_size;1877for (j = 0; j < size[i]; ++j) {1878vl_vlc_fillbits(vlc);1879vl_vlc_eatbits(vlc, 8);1880}1881break;1882}1883} else {1884offset[i] = offset[i - 1] + ti->TileSizeBytes + size[i - 1];1885left_size -= ti->TileSizeBytes + size[i - 1];1886}18871888if (i != tg_end) {1889size[i] = av1_le(vlc, ti->TileSizeBytes) + 1;1890} else {1891offset[i] = offset[i - 1] + size[i - 1];1892size[i] = left_size;1893}18941895for (j = 0; j < size[i]; ++j) {1896vl_vlc_fillbits(vlc);1897vl_vlc_eatbits(vlc, 8);1898}1899}19001901for (i = tg_start; i <= tg_end; ++i) {1902priv->picture.av1.slice_parameter.slice_data_offset[i] = offset[i];1903priv->picture.av1.slice_parameter.slice_data_size[i] = size[i];1904}1905}19061907static struct dec_av1_task *dec_av1_NeedTask(vid_dec_PrivateType *priv)1908{1909struct pipe_video_buffer templat = {};1910struct dec_av1_task *task;1911struct vl_screen *omx_screen;1912struct pipe_screen *pscreen;19131914omx_screen = priv->screen;1915assert(omx_screen);19161917pscreen = omx_screen->pscreen;1918assert(pscreen);19191920if (!list_is_empty(&priv->codec_data.av1.free_tasks)) {1921task = LIST_ENTRY(struct dec_av1_task,1922priv->codec_data.av1.free_tasks.next, list);1923task->buf_ref_count = 1;1924list_del(&task->list);1925return task;1926}19271928task = CALLOC_STRUCT(dec_av1_task);1929if (!task)1930return NULL;19311932memset(&templat, 0, sizeof(templat));1933templat.width = priv->codec->width;1934templat.height = priv->codec->height;1935templat.buffer_format = pscreen->get_video_param(1936pscreen,1937PIPE_VIDEO_PROFILE_UNKNOWN,1938PIPE_VIDEO_ENTRYPOINT_BITSTREAM,1939PIPE_VIDEO_CAP_PREFERED_FORMAT1940);1941templat.interlaced = false;19421943task->buf = priv->pipe->create_video_buffer(priv->pipe, &templat);1944if (!task->buf) {1945FREE(task);1946return NULL;1947}1948task->buf_ref_count = 1;1949task->is_sef_task = false;19501951return task;1952}19531954static void dec_av1_ReleaseTask(vid_dec_PrivateType *priv,1955struct list_head *head)1956{1957if (!head || !head->next)1958return;19591960list_for_each_entry_safe(struct dec_av1_task, task, head, list) {1961task->buf->destroy(task->buf);1962FREE(task);1963}1964}19651966static void dec_av1_MoveTask(struct list_head *from,1967struct list_head *to)1968{1969to->prev->next = from->next;1970from->next->prev = to->prev;1971from->prev->next = to;1972to->prev = from->prev;1973list_inithead(from);1974}19751976static void dec_av1_SortTask(vid_dec_PrivateType *priv)1977{1978int i;19791980list_for_each_entry_safe(struct dec_av1_task, t,1981&priv->codec_data.av1.finished_tasks, list) {1982bool found = false;1983for (i = 0; i < 8; ++i) {1984if (t->buf == priv->picture.av1.ref[i]) {1985found = true;1986break;1987}1988}1989if (!found && t->buf_ref_count == 0) {1990list_del(&t->list);1991list_addtail(&t->list, &priv->codec_data.av1.free_tasks);1992}1993}1994}19951996static struct dec_av1_task *dec_av1_SearchTask(vid_dec_PrivateType *priv,1997struct list_head *tasks)1998{1999unsigned idx =2000priv->codec_data.av1.uncompressed_header.frame_to_show_map_idx;20012002list_for_each_entry_safe(struct dec_av1_task, t, tasks, list) {2003if (t->buf == priv->picture.av1.ref[idx])2004return t;2005}20062007return NULL;2008}20092010static bool dec_av1_GetStartedTask(vid_dec_PrivateType *priv,2011struct dec_av1_task *task, struct list_head *tasks)2012{2013struct dec_av1_task *started_task;20142015++priv->codec_data.av1.que_num;2016list_addtail(&task->list, &priv->codec_data.av1.started_tasks);2017if (priv->codec_data.av1.que_num <= 16)2018return false;20192020started_task = LIST_ENTRY(struct dec_av1_task,2021priv->codec_data.av1.started_tasks.next, list);2022list_del(&started_task->list);2023list_addtail(&started_task->list, tasks);2024--priv->codec_data.av1.que_num;20252026return true;2027}20282029static void dec_av1_ShowExistingframe(vid_dec_PrivateType *priv)2030{2031struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate;2032struct dec_av1_task *task, *existing_task;2033bool fnd;20342035task = CALLOC_STRUCT(dec_av1_task);2036if (!task)2037return;20382039task->is_sef_task = true;20402041mtx_lock(&priv->codec_data.av1.mutex);2042dec_av1_MoveTask(&inp->tasks, &priv->codec_data.av1.finished_tasks);2043dec_av1_SortTask(priv);2044existing_task = dec_av1_SearchTask(priv, &priv->codec_data.av1.started_tasks);2045if (existing_task) {2046++existing_task->buf_ref_count;2047task->buf = existing_task->buf;2048task->buf_ref = &existing_task->buf;2049task->buf_ref_count = 0;2050} else {2051existing_task = dec_av1_SearchTask(priv, &priv->codec_data.av1.finished_tasks);2052if (existing_task) {2053struct vl_screen *omx_screen;2054struct pipe_screen *pscreen;2055struct pipe_video_buffer templat = {};2056struct pipe_video_buffer *buf;2057struct pipe_box box={};20582059omx_screen = priv->screen;2060assert(omx_screen);20612062pscreen = omx_screen->pscreen;2063assert(pscreen);20642065memset(&templat, 0, sizeof(templat));2066templat.width = priv->codec->width;2067templat.height = priv->codec->height;2068templat.buffer_format = pscreen->get_video_param(2069pscreen,2070PIPE_VIDEO_PROFILE_UNKNOWN,2071PIPE_VIDEO_ENTRYPOINT_BITSTREAM,2072PIPE_VIDEO_CAP_PREFERED_FORMAT2073);2074templat.interlaced = false;2075buf = priv->pipe->create_video_buffer(priv->pipe, &templat);2076if (!buf) {2077FREE(task);2078mtx_unlock(&priv->codec_data.av1.mutex);2079return;2080}20812082box.width = priv->codec->width;2083box.height = priv->codec->height;2084box.depth = 1;2085priv->pipe->resource_copy_region(priv->pipe,2086((struct vl_video_buffer *)buf)->resources[0],20870, 0, 0, 0,2088((struct vl_video_buffer *)(existing_task->buf))->resources[0],20890, &box);2090box.width /= 2;2091box.height/= 2;2092priv->pipe->resource_copy_region(priv->pipe,2093((struct vl_video_buffer *)buf)->resources[1],20940, 0, 0, 0,2095((struct vl_video_buffer *)(existing_task->buf))->resources[1],20960, &box);2097priv->pipe->flush(priv->pipe, NULL, 0);2098existing_task->buf_ref_count = 0;2099task->buf = buf;2100task->buf_ref_count = 1;2101} else {2102FREE(task);2103mtx_unlock(&priv->codec_data.av1.mutex);2104return;2105}2106}2107dec_av1_SortTask(priv);21082109fnd = dec_av1_GetStartedTask(priv, task, &inp->tasks);2110mtx_unlock(&priv->codec_data.av1.mutex);2111if (fnd)2112priv->frame_finished = 1;2113}21142115static struct dec_av1_task *dec_av1_BeginFrame(vid_dec_PrivateType *priv)2116{2117struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate;2118struct dec_av1_task *task;21192120if (priv->frame_started)2121return NULL;21222123if (!priv->codec) {2124struct vl_screen *omx_screen;2125struct pipe_screen *pscreen;2126struct pipe_video_codec templat = {};2127bool supported;21282129omx_screen = priv->screen;2130assert(omx_screen);21312132pscreen = omx_screen->pscreen;2133assert(pscreen);21342135supported = pscreen->get_video_param(pscreen, priv->profile,2136PIPE_VIDEO_ENTRYPOINT_BITSTREAM, PIPE_VIDEO_CAP_SUPPORTED);2137assert(supported && "AV1 is not supported");21382139templat.profile = priv->profile;2140templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;2141templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;2142templat.max_references = AV1_NUM_REF_FRAMES;2143templat.expect_chunked_decode = true;2144omx_base_video_PortType *port;2145port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];2146templat.width = port->sPortParam.format.video.nFrameWidth;2147templat.height = port->sPortParam.format.video.nFrameHeight;21482149priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat);2150}21512152mtx_lock(&priv->codec_data.av1.mutex);2153dec_av1_MoveTask(&inp->tasks, &priv->codec_data.av1.finished_tasks);2154dec_av1_SortTask(priv);2155mtx_unlock(&priv->codec_data.av1.mutex);21562157task = dec_av1_NeedTask(priv);2158if (!task)2159return NULL;21602161priv->codec->begin_frame(priv->codec, task->buf, &priv->picture.base);2162priv->frame_started = true;21632164return task;2165}21662167static void dec_av1_EndFrame(vid_dec_PrivateType *priv, struct dec_av1_task *task)2168{2169struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate;2170unsigned refresh_frame_flags;2171bool fnd;2172unsigned i;21732174if (!priv->frame_started || ! task)2175return;21762177priv->codec->end_frame(priv->codec, task->buf, &priv->picture.base);2178priv->frame_started = false;21792180refresh_frame_flags = priv->codec_data.av1.uncompressed_header.refresh_frame_flags;2181for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {2182if (refresh_frame_flags & (1 << i)) {2183memcpy(&priv->codec_data.av1.refs[i], &priv->codec_data.av1.uncompressed_header,2184sizeof(struct av1_uncompressed_header_obu));2185priv->picture.av1.ref[i] = task->buf;2186priv->codec_data.av1.RefFrames[i].RefFrameType =2187priv->codec_data.av1.uncompressed_header.frame_type;2188priv->codec_data.av1.RefFrames[i].RefFrameId =2189priv->codec_data.av1.uncompressed_header.current_frame_id;2190priv->codec_data.av1.RefFrames[i].RefUpscaledWidth =2191priv->codec_data.av1.uncompressed_header.UpscaledWidth;2192priv->codec_data.av1.RefFrames[i].RefFrameWidth =2193priv->codec_data.av1.uncompressed_header.FrameWidth;2194priv->codec_data.av1.RefFrames[i].RefFrameHeight =2195priv->codec_data.av1.uncompressed_header.FrameHeight;2196priv->codec_data.av1.RefFrames[i].RefRenderWidth =2197priv->codec_data.av1.uncompressed_header.RenderWidth;2198priv->codec_data.av1.RefFrames[i].RefRenderHeight =2199priv->codec_data.av1.uncompressed_header.RenderHeight;2200}2201}2202if (!priv->picture.av1.picture_parameter.pic_info_fields.show_frame)2203task->no_show_frame = true;22042205mtx_lock(&priv->codec_data.av1.mutex);2206fnd = dec_av1_GetStartedTask(priv, task, &priv->codec_data.av1.decode_tasks);2207if (!fnd) {2208mtx_unlock(&priv->codec_data.av1.mutex);2209return;2210}2211if (!priv->codec_data.av1.stacked_frame)2212dec_av1_MoveTask(&priv->codec_data.av1.decode_tasks, &inp->tasks);2213mtx_unlock(&priv->codec_data.av1.mutex);2214priv->frame_finished = 1;2215}22162217static void dec_av1_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc,2218unsigned min_bits_left)2219{2220unsigned start_bits_pos = vl_vlc_bits_left(vlc);2221unsigned start_bits = vl_vlc_valid_bits(vlc);2222unsigned start_bytes = start_bits / 8;2223const void *obu_data = vlc->data;2224uint8_t start_buf[8];2225unsigned num_buffers = 0;2226void * const * buffers[4];2227unsigned sizes[4];2228unsigned obu_size = 0;2229unsigned total_obu_len;2230enum av1_obu_type type;2231bool obu_extension_flag;2232bool obu_has_size_field;2233unsigned i;22342235for (i = 0; i < start_bytes; ++i)2236start_buf[i] =2237vl_vlc_peekbits(vlc, start_bits) >> ((start_bytes - i - 1) * 8);22382239/* obu header */2240av1_f(vlc, 1); /* obu_forbidden_bit */2241type = av1_f(vlc, 4);2242obu_extension_flag = av1_f(vlc, 1);2243obu_has_size_field = av1_f(vlc, 1);2244av1_f(vlc, 1); /* obu_reserved_1bit */2245if (obu_extension_flag) {2246priv->codec_data.av1.ext.temporal_id = av1_f(vlc, 3);2247priv->codec_data.av1.ext.spatial_id = av1_f(vlc, 2);2248av1_f(vlc, 3); /* extension_header_reserved_3bits */2249}22502251obu_size = (obu_has_size_field) ? av1_uleb128(vlc) :2252(priv->sizes[0] - (unsigned)obu_extension_flag - 1);2253total_obu_len = (start_bits_pos - vl_vlc_bits_left(vlc)) / 8 + obu_size;22542255switch (type) {2256case AV1_OBU_SEQUENCE_HEADER: {2257sequence_header_obu(priv, vlc);2258av1_byte_alignment(vlc);2259priv->codec_data.av1.bs_obu_seq_sz = total_obu_len;2260memcpy(priv->codec_data.av1.bs_obu_seq_buf, start_buf, start_bytes);2261memcpy(priv->codec_data.av1.bs_obu_seq_buf + start_bytes, obu_data,2262total_obu_len - start_bytes);2263break;2264}2265case AV1_OBU_TEMPORAL_DELIMITER:2266av1_byte_alignment(vlc);2267priv->codec_data.av1.bs_obu_td_sz = total_obu_len;2268memcpy(priv->codec_data.av1.bs_obu_td_buf, start_buf, total_obu_len);2269break;2270case AV1_OBU_FRAME_HEADER:2271frame_header_obu(priv, vlc);2272if (priv->codec_data.av1.uncompressed_header.show_existing_frame)2273dec_av1_ShowExistingframe(priv);2274av1_byte_alignment(vlc);2275break;2276case AV1_OBU_FRAME: {2277struct dec_av1_task *task;22782279frame_header_obu(priv, vlc);2280av1_byte_alignment(vlc);22812282parse_tile_hdr(priv, vlc, start_bits_pos, total_obu_len);2283av1_byte_alignment(vlc);22842285task = dec_av1_BeginFrame(priv);2286if (!task)2287return;22882289if (priv->codec_data.av1.bs_obu_td_sz) {2290buffers[num_buffers] = (void *)priv->codec_data.av1.bs_obu_td_buf;2291sizes[num_buffers++] = priv->codec_data.av1.bs_obu_td_sz;2292priv->codec_data.av1.bs_obu_td_sz = 0;2293}2294if (priv->codec_data.av1.bs_obu_seq_sz) {2295buffers[num_buffers] = (void *)priv->codec_data.av1.bs_obu_seq_buf;2296sizes[num_buffers++] = priv->codec_data.av1.bs_obu_seq_sz;2297priv->codec_data.av1.bs_obu_seq_sz = 0;2298}2299buffers[num_buffers] = (void *)start_buf;2300sizes[num_buffers++] = start_bytes;2301buffers[num_buffers] = (void *)obu_data;2302sizes[num_buffers++] = total_obu_len - start_bytes;23032304priv->codec->decode_bitstream(priv->codec, priv->target,2305&priv->picture.base, num_buffers, (const void * const*)buffers, sizes);23062307priv->codec_data.av1.stacked_frame =2308(vl_vlc_bits_left(vlc) > min_bits_left) ? true : false;23092310dec_av1_EndFrame(priv, task);2311break;2312}2313default:2314av1_byte_alignment(vlc);2315break;2316}23172318return;2319}23202321OMX_ERRORTYPE vid_dec_av1_AllocateInBuffer(omx_base_PortType *port,2322OMX_INOUT OMX_BUFFERHEADERTYPE **buf, OMX_IN OMX_U32 idx,2323OMX_IN OMX_PTR private, OMX_IN OMX_U32 size)2324{2325struct input_buf_private *inp;2326OMX_ERRORTYPE r;23272328r = base_port_AllocateBuffer(port, buf, idx, private, size);2329if (r)2330return r;23312332inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);2333if (!inp) {2334base_port_FreeBuffer(port, idx, *buf);2335return OMX_ErrorInsufficientResources;2336}23372338list_inithead(&inp->tasks);23392340return OMX_ErrorNone;2341}23422343OMX_ERRORTYPE vid_dec_av1_UseInBuffer(omx_base_PortType *port,2344OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,2345OMX_PTR private, OMX_U32 size, OMX_U8 *mem)2346{2347struct input_buf_private *inp;2348OMX_ERRORTYPE r;23492350r = base_port_UseBuffer(port, buf, idx, private, size, mem);2351if (r)2352return r;23532354inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);2355if (!inp) {2356base_port_FreeBuffer(port, idx, *buf);2357return OMX_ErrorInsufficientResources;2358}23592360list_inithead(&inp->tasks);23612362return OMX_ErrorNone;2363}23642365void vid_dec_av1_FreeInputPortPrivate(vid_dec_PrivateType *priv,2366OMX_BUFFERHEADERTYPE *buf)2367{2368struct input_buf_private *inp = buf->pInputPortPrivate;23692370if (!inp || !inp->tasks.next)2371return;23722373list_for_each_entry_safe(struct dec_av1_task, task, &inp->tasks, list) {2374task->buf->destroy(task->buf);2375FREE(task);2376}2377}23782379void vid_dec_av1_ReleaseTasks(vid_dec_PrivateType *priv)2380{2381dec_av1_ReleaseTask(priv, &priv->codec_data.av1.free_tasks);2382dec_av1_ReleaseTask(priv, &priv->codec_data.av1.started_tasks);2383dec_av1_ReleaseTask(priv, &priv->codec_data.av1.decode_tasks);2384dec_av1_ReleaseTask(priv, &priv->codec_data.av1.finished_tasks);2385mtx_destroy(&priv->codec_data.av1.mutex);2386}23872388void vid_dec_av1_FrameDecoded(OMX_COMPONENTTYPE *comp,2389OMX_BUFFERHEADERTYPE* input,2390OMX_BUFFERHEADERTYPE* output)2391{2392vid_dec_PrivateType *priv = comp->pComponentPrivate;2393bool eos = !!(input->nFlags & OMX_BUFFERFLAG_EOS);2394struct input_buf_private *inp = input->pInputPortPrivate;2395struct dec_av1_task *task;2396bool stacked = false;23972398mtx_lock(&priv->codec_data.av1.mutex);2399if (list_length(&inp->tasks) > 1)2400stacked = true;24012402if (list_is_empty(&inp->tasks)) {2403task = LIST_ENTRY(struct dec_av1_task,2404priv->codec_data.av1.started_tasks.next, list);2405list_del(&task->list);2406list_addtail(&task->list, &inp->tasks);2407--priv->codec_data.av1.que_num;2408}24092410task = LIST_ENTRY(struct dec_av1_task, inp->tasks.next, list);24112412if (!task->no_show_frame) {2413vid_dec_FillOutput(priv, task->buf, output);2414output->nFilledLen = output->nAllocLen;2415output->nTimeStamp = input->nTimeStamp;2416} else {2417task->no_show_frame = false;2418output->nFilledLen = 0;2419}24202421if (task->is_sef_task) {2422if (task->buf_ref_count == 0) {2423struct dec_av1_task *t = container_of(task->buf_ref, struct dec_av1_task, buf);2424list_del(&task->list);2425t->buf_ref_count--;2426list_del(&t->list);2427list_addtail(&t->list, &priv->codec_data.av1.finished_tasks);2428} else if (task->buf_ref_count == 1) {2429list_del(&task->list);2430task->buf->destroy(task->buf);2431task->buf_ref_count--;2432}2433FREE(task);2434} else {2435if (task->buf_ref_count == 1) {2436list_del(&task->list);2437list_addtail(&task->list, &priv->codec_data.av1.finished_tasks);2438task->buf_ref_count--;2439} else if (task->buf_ref_count == 2) {2440list_del(&task->list);2441task->buf_ref_count--;2442list_addtail(&task->list, &priv->codec_data.av1.finished_tasks);2443}2444}24452446if (eos && input->pInputPortPrivate) {2447if (!priv->codec_data.av1.que_num)2448input->nFilledLen = 0;2449else2450vid_dec_av1_FreeInputPortPrivate(priv, input);2451}2452else {2453if (!stacked)2454input->nFilledLen = 0;2455}2456mtx_unlock(&priv->codec_data.av1.mutex);2457}24582459void vid_dec_av1_Init(vid_dec_PrivateType *priv)2460{2461priv->picture.base.profile = PIPE_VIDEO_PROFILE_AV1_MAIN;2462priv->Decode = dec_av1_Decode;2463list_inithead(&priv->codec_data.av1.free_tasks);2464list_inithead(&priv->codec_data.av1.started_tasks);2465list_inithead(&priv->codec_data.av1.decode_tasks);2466list_inithead(&priv->codec_data.av1.finished_tasks);2467(void)mtx_init(&priv->codec_data.av1.mutex, mtx_plain);2468}246924702471