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