xref: /rockchip-linux_mpp/mpp/codec/dec/av1/av1d_parser2_syntax.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /* SPDX-License-Identifier: Apache-2.0 OR MIT */
2 /*
3  * Copyright (c) 2024 Rockchip Electronics Co., Ltd.
4  */
5 
6 #define MODULE_TAG "av1d_parser2syntax"
7 
8 #include <string.h>
9 
10 #include "av1d_parser.h"
11 #include "av1d_syntax.h"
12 
av1d_fill_picparams(Av1CodecContext * ctx,DXVA_PicParams_AV1 * pp)13 static int av1d_fill_picparams(Av1CodecContext *ctx, DXVA_PicParams_AV1 *pp)
14 {
15     int i, j, loop_cnt, uses_lr;
16     RK_U8 is_rk3588;
17     AV1Context *h = ctx->priv_data;
18     const AV1RawSequenceHeader *seq = h->sequence_header;
19     const AV1RawFrameHeader *frame_header = h->raw_frame_header;
20     const AV1RawFilmGrainParams *film_grain = &h->cur_frame.film_grain;
21 
22     unsigned char remap_lr_type[4] = { AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ };
23     // int apply_grain = !(ctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN) && film_grain->apply_grain;
24     int apply_grain = film_grain->apply_grain;
25 
26     memset(pp, 0, sizeof(*pp));
27 
28     pp->width  = h->frame_width;
29     pp->height = h->frame_height;
30 
31     pp->max_width  = seq->max_frame_width_minus_1 + 1;
32     pp->max_height = seq->max_frame_height_minus_1 + 1;
33 
34     pp->CurrPic.Index7Bits  = h->cur_frame.slot_index;
35     pp->CurrPicTextureIndex = h->cur_frame.slot_index;
36     pp->superres_denom      = frame_header->use_superres ? frame_header->coded_denom : AV1_SUPERRES_NUM;
37     pp->bitdepth            = h->bit_depth;
38     pp->seq_profile         = seq->seq_profile;
39     pp->frame_header_size   = h->frame_header_size;
40 
41     /* Tiling info */
42     pp->tiles.cols = frame_header->tile_cols;
43     pp->tiles.rows = frame_header->tile_rows;
44     pp->tiles.context_update_id = frame_header->context_update_tile_id;
45 
46     for (i = 0; i < pp->tiles.cols; i++)
47         pp->tiles.widths[i] = frame_header->width_in_sbs_minus_1[i] + 1;
48 
49     for (i = 0; i < pp->tiles.rows; i++)
50         pp->tiles.heights[i] = frame_header->height_in_sbs_minus_1[i] + 1;
51 
52     for (i = 0; i < AV1_MAX_TILES; i++) {
53         pp->tiles.tile_offset_start[i] = h->tile_offset_start[i];
54         pp->tiles.tile_offset_end[i] = h->tile_offset_end[i];
55     }
56 
57     pp->tiles.tile_sz_mag = h->raw_frame_header->tile_size_bytes_minus1;
58     /* Coding tools */
59     pp->coding.current_operating_point      = seq->operating_point_idc[h->operating_point_idc];
60     pp->coding.use_128x128_superblock       = seq->use_128x128_superblock;
61     pp->coding.intra_edge_filter            = seq->enable_intra_edge_filter;
62     pp->coding.interintra_compound          = seq->enable_interintra_compound;
63     pp->coding.masked_compound              = seq->enable_masked_compound;
64     pp->coding.warped_motion                = frame_header->allow_warped_motion;
65     pp->coding.dual_filter                  = seq->enable_dual_filter;
66     pp->coding.jnt_comp                     = seq->enable_jnt_comp;
67     pp->coding.screen_content_tools         = frame_header->allow_screen_content_tools;
68     pp->coding.integer_mv                   = frame_header->force_integer_mv;
69 
70     pp->coding.cdef_en                      = seq->enable_cdef;
71     pp->coding.restoration                  = seq->enable_restoration;
72     pp->coding.film_grain_en                = seq->film_grain_params_present  ;//&& !(avctx->export_side_data & AV_CODEC_EXPORT_DATA_FILM_GRAIN);
73     pp->coding.intrabc                      = frame_header->allow_intrabc;
74     pp->coding.high_precision_mv            = frame_header->allow_high_precision_mv;
75     pp->coding.switchable_motion_mode       = frame_header->is_motion_mode_switchable;
76     pp->coding.filter_intra                 = seq->enable_filter_intra;
77     pp->coding.disable_frame_end_update_cdf = frame_header->disable_frame_end_update_cdf;
78     pp->coding.disable_cdf_update           = frame_header->disable_cdf_update;
79     pp->coding.reference_mode               = frame_header->reference_select;
80     pp->coding.skip_mode                    = frame_header->skip_mode_present;
81     pp->coding.reduced_tx_set               = frame_header->reduced_tx_set;
82     pp->coding.superres                     = frame_header->use_superres;
83     pp->coding.tx_mode                      = frame_header->tx_mode;
84     pp->coding.use_ref_frame_mvs            = frame_header->use_ref_frame_mvs;
85     pp->coding.enable_ref_frame_mvs         = seq->enable_ref_frame_mvs;
86     pp->coding.reference_frame_update       = 1; // 0 for show_existing_frame with key frames, but those are not passed to the hwaccel
87     pp->coding.error_resilient_mode         = frame_header->error_resilient_mode;
88 
89     /* Format & Picture Info flags */
90     pp->format.frame_type     = frame_header->frame_type;
91     pp->format.show_frame     = frame_header->show_frame;
92     pp->format.showable_frame = frame_header->showable_frame;
93     pp->format.subsampling_x  = seq->color_config.subsampling_x;
94     pp->format.subsampling_y  = seq->color_config.subsampling_y;
95     pp->format.mono_chrome    = seq->color_config.mono_chrome;
96     pp->coded_lossless        = h->cur_frame.coded_lossless;
97     pp->all_lossless          = h->all_lossless;
98     /* References */
99     pp->primary_ref_frame = frame_header->primary_ref_frame;
100     pp->enable_order_hint = seq->enable_order_hint;
101     pp->order_hint        = frame_header->order_hint;
102     pp->order_hint_bits   = seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
103 
104     pp->ref_frame_valued = frame_header->ref_frame_valued;
105     for (i = 0; i < AV1_REFS_PER_FRAME; i++)
106         pp->ref_frame_idx[i] = frame_header->ref_frame_idx[i];
107 
108     memset(pp->RefFrameMapTextureIndex, 0xFF, sizeof(pp->RefFrameMapTextureIndex));
109     is_rk3588 = mpp_get_soc_type() == ROCKCHIP_SOC_RK3588;
110     loop_cnt = is_rk3588 ? AV1_REFS_PER_FRAME : AV1_NUM_REF_FRAMES;
111     for (i = 0; i < loop_cnt; i++) {
112         int8_t ref_idx = frame_header->ref_frame_idx[i];
113         AV1Frame *ref_frame;
114         RefInfo *ref_i;
115 
116         if (is_rk3588)
117             ref_frame = &h->ref[ref_idx];
118         else
119             ref_frame = &h->ref[i];
120         ref_i = ref_frame->ref;
121 
122         if (ref_frame->f) {
123             pp->frame_refs[i].width  = mpp_frame_get_width(ref_frame->f);
124             pp->frame_refs[i].height = mpp_frame_get_height(ref_frame->f);;
125         }
126         pp->frame_refs[i].Index  = ref_frame->slot_index;
127         pp->frame_refs[i].order_hint = ref_frame->order_hint;
128         if (ref_i) {
129             pp->frame_refs[i].lst_frame_offset = ref_i->lst_frame_offset;
130             pp->frame_refs[i].lst2_frame_offset = ref_i->lst2_frame_offset;
131             pp->frame_refs[i].lst3_frame_offset = ref_i->lst3_frame_offset;
132             pp->frame_refs[i].gld_frame_offset = ref_i->gld_frame_offset;
133             pp->frame_refs[i].bwd_frame_offset = ref_i->bwd_frame_offset;
134             pp->frame_refs[i].alt2_frame_offset = ref_i->alt2_frame_offset;
135             pp->frame_refs[i].alt_frame_offset = ref_i->alt_frame_offset ;
136             pp->frame_refs[i].is_intra_frame = ref_i->is_intra_frame;
137             pp->frame_refs[i].intra_only = ref_i->intra_only;
138         }
139         /* Global Motion */
140         pp->frame_refs[i].wminvalid = (h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].wmtype == AV1_WARP_MODEL_IDENTITY);
141         pp->frame_refs[i].wmtype    = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].wmtype;
142         for (j = 0; j < 6; ++j) {
143             pp->frame_refs[i].wmmat[j] = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].wmmat[j];
144             pp->frame_refs[i].wmmat_val[j] = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].wmmat_val[j];
145         }
146         pp->frame_refs[i].alpha = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].alpha;
147         pp->frame_refs[i].beta = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].beta;
148         pp->frame_refs[i].gamma = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].gamma;
149         pp->frame_refs[i].delta = h->cur_frame.gm_params[AV1_REF_FRAME_LAST + i].delta;
150     }
151     for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
152         AV1Frame *ref_frame = &h->ref[i];
153 
154         pp->frame_ref_state[i].valid           = h->ref_s[i].valid         ;
155         pp->frame_ref_state[i].frame_id        = h->ref_s[i].frame_id      ;
156         pp->frame_ref_state[i].upscaled_width  = h->ref_s[i].upscaled_width;
157         pp->frame_ref_state[i].frame_width     = h->ref_s[i].frame_width   ;
158         pp->frame_ref_state[i].frame_height    = h->ref_s[i].frame_height  ;
159         pp->frame_ref_state[i].render_width    = h->ref_s[i].render_width  ;
160         pp->frame_ref_state[i].render_height   = h->ref_s[i].render_height ;
161         pp->frame_ref_state[i].frame_type      = h->ref_s[i].frame_type    ;
162         pp->frame_ref_state[i].subsampling_x   = h->ref_s[i].subsampling_x ;
163         pp->frame_ref_state[i].subsampling_y   = h->ref_s[i].subsampling_y ;
164         pp->frame_ref_state[i].bit_depth       = h->ref_s[i].bit_depth     ;
165         pp->frame_ref_state[i].order_hint      = h->ref_s[i].order_hint    ;
166 
167         pp->ref_order_hint[i] = frame_header->ref_order_hint[i];
168 
169         if (ref_frame->slot_index < 0x7f)
170             pp->RefFrameMapTextureIndex[i] = ref_frame->slot_index;
171         else
172             pp->RefFrameMapTextureIndex[i] = 0xff;
173     }
174 
175     /* Loop filter parameters */
176     pp->loop_filter.filter_level[0]        = frame_header->loop_filter_level[0];
177     pp->loop_filter.filter_level[1]        = frame_header->loop_filter_level[1];
178     pp->loop_filter.filter_level_u         = frame_header->loop_filter_level[2];
179     pp->loop_filter.filter_level_v         = frame_header->loop_filter_level[3];
180     pp->loop_filter.sharpness_level        = frame_header->loop_filter_sharpness;
181     pp->loop_filter.mode_ref_delta_enabled = frame_header->loop_filter_delta_enabled;
182     pp->loop_filter.mode_ref_delta_update  = frame_header->loop_filter_delta_update;
183     pp->loop_filter.delta_lf_multi         = frame_header->delta_lf_multi;
184     pp->loop_filter.delta_lf_present       = frame_header->delta_lf_present;
185     pp->loop_filter.delta_lf_res           = frame_header->delta_lf_res;
186 
187     for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
188         pp->loop_filter.ref_deltas[i] = frame_header->loop_filter_ref_deltas[i];
189     }
190 
191     pp->loop_filter.mode_deltas[0]                = frame_header->loop_filter_mode_deltas[0];
192     pp->loop_filter.mode_deltas[1]                = frame_header->loop_filter_mode_deltas[1];
193     pp->loop_filter.frame_restoration_type[0]     = remap_lr_type[frame_header->lr_type[0]];
194     pp->loop_filter.frame_restoration_type[1]     = remap_lr_type[frame_header->lr_type[1]];
195     pp->loop_filter.frame_restoration_type[2]     = remap_lr_type[frame_header->lr_type[2]];
196     uses_lr = frame_header->lr_type[0] || frame_header->lr_type[1] || frame_header->lr_type[2];
197     pp->loop_filter.log2_restoration_unit_size[0] = uses_lr ? (1 + frame_header->lr_unit_shift) : 3;
198     pp->loop_filter.log2_restoration_unit_size[1] = uses_lr ? (1 + frame_header->lr_unit_shift - frame_header->lr_uv_shift) : 3;
199     pp->loop_filter.log2_restoration_unit_size[2] = uses_lr ? (1 + frame_header->lr_unit_shift - frame_header->lr_uv_shift) : 3;
200 
201     /* Quantization */
202     pp->quantization.delta_q_present = frame_header->delta_q_present;
203     pp->quantization.delta_q_res     = frame_header->delta_q_res;
204     pp->quantization.base_qindex     = frame_header->base_q_idx;
205     pp->quantization.y_dc_delta_q    = frame_header->delta_q_y_dc;
206     pp->quantization.u_dc_delta_q    = frame_header->delta_q_u_dc;
207     pp->quantization.v_dc_delta_q    = frame_header->delta_q_v_dc;
208     pp->quantization.u_ac_delta_q    = frame_header->delta_q_u_ac;
209     pp->quantization.v_ac_delta_q    = frame_header->delta_q_v_ac;
210     pp->quantization.using_qmatrix   = frame_header->using_qmatrix;
211     pp->quantization.qm_y            = frame_header->using_qmatrix ? frame_header->qm_y : 0xFF;
212     pp->quantization.qm_u            = frame_header->using_qmatrix ? frame_header->qm_u : 0xFF;
213     pp->quantization.qm_v            = frame_header->using_qmatrix ? frame_header->qm_v : 0xFF;
214 
215     /* Cdef parameters */
216     pp->cdef.damping = frame_header->cdef_damping_minus_3;
217     pp->cdef.bits    = frame_header->cdef_bits;
218     for (i = 0; i < 8; i++) {
219         pp->cdef.y_strengths[i].primary    = frame_header->cdef_y_pri_strength[i];
220         pp->cdef.y_strengths[i].secondary  = frame_header->cdef_y_sec_strength[i];
221         pp->cdef.uv_strengths[i].primary   = frame_header->cdef_uv_pri_strength[i];
222         pp->cdef.uv_strengths[i].secondary = frame_header->cdef_uv_sec_strength[i];
223     }
224 
225     /* Misc flags */
226     pp->interp_filter = frame_header->interpolation_filter;
227 
228     /* Segmentation */
229     pp->segmentation.enabled         = frame_header->segmentation_enabled;
230     pp->segmentation.update_map      = frame_header->segmentation_update_map;
231     pp->segmentation.update_data     = frame_header->segmentation_update_data;
232     pp->segmentation.temporal_update = frame_header->segmentation_temporal_update;
233     for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
234         for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
235             pp->segmentation.feature_mask[i]      |= frame_header->feature_enabled[i][j] << j;
236             pp->segmentation.feature_data[i][j]    = frame_header->feature_value[i][j];
237         }
238     }
239     pp->segmentation.last_active     = frame_header->segmentation_id_last_active;
240     pp->segmentation.preskip         = frame_header->segmentation_id_preskip;
241 
242     /* Film grain */
243     pp->film_grain.matrix_coefficients      = seq->color_config.matrix_coefficients;
244     if (apply_grain) {
245         pp->film_grain.apply_grain              = 1;
246         pp->film_grain.scaling_shift_minus8     = film_grain->grain_scaling_minus_8;
247         pp->film_grain.chroma_scaling_from_luma = film_grain->chroma_scaling_from_luma;
248         pp->film_grain.ar_coeff_lag             = film_grain->ar_coeff_lag;
249         pp->film_grain.ar_coeff_shift_minus6    = film_grain->ar_coeff_shift_minus_6;
250         pp->film_grain.grain_scale_shift        = film_grain->grain_scale_shift;
251         pp->film_grain.overlap_flag             = film_grain->overlap_flag;
252         pp->film_grain.clip_to_restricted_range = film_grain->clip_to_restricted_range;
253         pp->film_grain.matrix_coeff_is_identity = (seq->color_config.matrix_coefficients == MPP_FRAME_SPC_RGB);
254 
255         pp->film_grain.grain_seed               = film_grain->grain_seed;
256         pp->film_grain.update_grain             = film_grain->update_grain;
257         pp->film_grain.num_y_points             = film_grain->num_y_points;
258         for (i = 0; i < film_grain->num_y_points; i++) {
259             pp->film_grain.scaling_points_y[i][0] = film_grain->point_y_value[i];
260             pp->film_grain.scaling_points_y[i][1] = film_grain->point_y_scaling[i];
261         }
262         pp->film_grain.num_cb_points            = film_grain->num_cb_points;
263         for (i = 0; i < film_grain->num_cb_points; i++) {
264             pp->film_grain.scaling_points_cb[i][0] = film_grain->point_cb_value[i];
265             pp->film_grain.scaling_points_cb[i][1] = film_grain->point_cb_scaling[i];
266         }
267         pp->film_grain.num_cr_points            = film_grain->num_cr_points;
268         for (i = 0; i < film_grain->num_cr_points; i++) {
269             pp->film_grain.scaling_points_cr[i][0] = film_grain->point_cr_value[i];
270             pp->film_grain.scaling_points_cr[i][1] = film_grain->point_cr_scaling[i];
271         }
272         for (i = 0; i < 24; i++) {
273             pp->film_grain.ar_coeffs_y[i] = film_grain->ar_coeffs_y_plus_128[i];
274         }
275         for (i = 0; i < 25; i++) {
276             pp->film_grain.ar_coeffs_cb[i] = film_grain->ar_coeffs_cb_plus_128[i];
277             pp->film_grain.ar_coeffs_cr[i] = film_grain->ar_coeffs_cr_plus_128[i];
278         }
279         pp->film_grain.cb_mult      = film_grain->cb_mult;
280         pp->film_grain.cb_luma_mult = film_grain->cb_luma_mult;
281         pp->film_grain.cr_mult      = film_grain->cr_mult;
282         pp->film_grain.cr_luma_mult = film_grain->cr_luma_mult;
283         pp->film_grain.cb_offset    = film_grain->cb_offset;
284         pp->film_grain.cr_offset    = film_grain->cr_offset;
285         pp->film_grain.cr_offset    = film_grain->cr_offset;
286     }
287     pp->upscaled_width = h->upscaled_width;
288     pp->frame_to_show_map_idx = frame_header->frame_to_show_map_idx;
289     pp->show_existing_frame = frame_header->show_existing_frame;
290     pp->frame_tag_size = h->frame_tag_size;
291     pp->skip_ref0      = h->skip_ref0;
292     pp->skip_ref1      = h->skip_ref1;
293     pp->refresh_frame_flags = frame_header->refresh_frame_flags;
294 
295     pp->cdfs = h->cdfs;
296     pp->cdfs_ndvc = h->cdfs_ndvc;
297     pp->tile_cols_log2 = frame_header->tile_cols_log2;
298     pp->tile_rows_log2 = frame_header->tile_rows_log2;
299     // XXX: Setting the StatusReportFeedbackNumber breaks decoding on some drivers (tested on NVIDIA 457.09)
300     // Status Reporting is not used by FF, hence not providing a number does not cause any issues
301     //pp->StatusReportFeedbackNumber = 1 + DXVA_CONTEXT_REPORT_ID(avctx, ctx)++;
302     return 0;
303 }
304 
av1d_fill_counts(Av1CodecContext * ctx)305 void av1d_fill_counts(Av1CodecContext *ctx)
306 {
307     //AV1Context *s = ctx->priv_data;
308     (void) ctx;
309     // memcpy(&ctx->pic_params.counts, &s->counts, sizeof(s->counts));
310 }
311 
av1d_parser2_syntax(Av1CodecContext * ctx)312 RK_S32 av1d_parser2_syntax(Av1CodecContext *ctx)
313 {
314     av1d_fill_picparams(ctx, &ctx->pic_params);
315     return 0;
316 }
317