1 /*
2 *
3 * Copyright 2021 Rockchip Electronics Co. LTD
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 #define MODULE_TAG "avs2d_ps"
19
20 #include <string.h>
21 #include <stdlib.h>
22
23 #include "mpp_mem.h"
24 #include "mpp_log.h"
25 #include "mpp_debug.h"
26 #include "mpp_packet_impl.h"
27 #include "hal_task.h"
28
29 #include "avs2d_api.h"
30 #include "avs2d_ps.h"
31
32 static const RK_U32 wq_param_default[2][6] = {
33 { 67, 71, 71, 80, 80, 106},
34 { 64, 49, 53, 58, 58, 64 }
35 };
36
37 static const RK_U32 g_WqMDefault4x4[16] = {
38 64, 64, 64, 68,
39 64, 64, 68, 72,
40 64, 68, 76, 80,
41 72, 76, 84, 96
42 };
43
44 static const RK_U32 g_WqMDefault8x8[64] = {
45 64, 64, 64, 64, 68, 68, 72, 76,
46 64, 64, 64, 68, 72, 76, 84, 92,
47 64, 64, 68, 72, 76, 80, 88, 100,
48 64, 68, 72, 80, 84, 92, 100, 28,
49 68, 72, 80, 84, 92, 104, 112, 128,
50 76, 80, 84, 92, 104, 116, 132, 152,
51 96, 100, 104, 116, 124, 140, 164, 188,
52 104, 108, 116, 128, 152, 172, 192, 216
53 };
54
55 static const RK_U8 WeightQuantModel[4][64] = {
56 // l a b c d h
57 // 0 1 2 3 4 5
58 {
59 // Mode 0
60 0, 0, 0, 4, 4, 4, 5, 5,
61 0, 0, 3, 3, 3, 3, 5, 5,
62 0, 3, 2, 2, 1, 1, 5, 5,
63 4, 3, 2, 2, 1, 5, 5, 5,
64 4, 3, 1, 1, 5, 5, 5, 5,
65 4, 3, 1, 5, 5, 5, 5, 5,
66 5, 5, 5, 5, 5, 5, 5, 5,
67 5, 5, 5, 5, 5, 5, 5, 5
68 }, {
69 // Mode 1
70 0, 0, 0, 4, 4, 4, 5, 5,
71 0, 0, 4, 4, 4, 4, 5, 5,
72 0, 3, 2, 2, 2, 1, 5, 5,
73 3, 3, 2, 2, 1, 5, 5, 5,
74 3, 3, 2, 1, 5, 5, 5, 5,
75 3, 3, 1, 5, 5, 5, 5, 5,
76 5, 5, 5, 5, 5, 5, 5, 5,
77 5, 5, 5, 5, 5, 5, 5, 5
78 }, {
79 // Mode 2
80 0, 0, 0, 4, 4, 3, 5, 5,
81 0, 0, 4, 4, 3, 2, 5, 5,
82 0, 4, 4, 3, 2, 1, 5, 5,
83 4, 4, 3, 2, 1, 5, 5, 5,
84 4, 3, 2, 1, 5, 5, 5, 5,
85 3, 2, 1, 5, 5, 5, 5, 5,
86 5, 5, 5, 5, 5, 5, 5, 5,
87 5, 5, 5, 5, 5, 5, 5, 5
88 }, {
89 // Mode 3
90 0, 0, 0, 3, 2, 1, 5, 5,
91 0, 0, 4, 3, 2, 1, 5, 5,
92 0, 4, 4, 3, 2, 1, 5, 5,
93 3, 3, 3, 3, 2, 5, 5, 5,
94 2, 2, 2, 2, 5, 5, 5, 5,
95 1, 1, 1, 5, 5, 5, 5, 5,
96 5, 5, 5, 5, 5, 5, 5, 5,
97 5, 5, 5, 5, 5, 5, 5, 5
98 }
99 };
100
101 static const RK_U8 WeightQuantModel4x4[4][16] = {
102 // l a b c d h
103 // 0 1 2 3 4 5
104 {
105 // Mode 0
106 0, 4, 3, 5,
107 4, 2, 1, 5,
108 3, 1, 1, 5,
109 5, 5, 5, 5
110 }, {
111 // Mode 1
112 0, 4, 4, 5,
113 3, 2, 2, 5,
114 3, 2, 1, 5,
115 5, 5, 5, 5
116 }, {
117 // Mode 2
118 0, 4, 3, 5,
119 4, 3, 2, 5,
120 3, 2, 1, 5,
121 5, 5, 5, 5
122 }, {
123 // Mode 3
124 0, 3, 1, 5,
125 3, 4, 2, 5,
126 1, 2, 2, 5,
127 5, 5, 5, 5
128 }
129 };
130
wq_get_default_matrix(RK_U32 sizeId)131 static const RK_U32 *wq_get_default_matrix(RK_U32 sizeId)
132 {
133 return (sizeId == 0) ? g_WqMDefault4x4 : g_WqMDefault8x8;
134 }
135
wq_init_frame_quant_param(Avs2dCtx_t * p_dec)136 static void wq_init_frame_quant_param(Avs2dCtx_t *p_dec)
137 {
138 RK_U32 i, j, k;
139 Avs2dPicHeader_t *ph = &p_dec->ph;
140
141 if (!p_dec->enable_wq || ph->pic_wq_data_index != 1) {
142 return;
143 }
144
145 //<! determine weight quant param
146 if (ph->wq_param_index == 0) {
147 for (i = 0; i < WQP_SIZE; i++) {
148 ph->pic_wq_param[i] = wq_param_default[DETAILED][i];
149 }
150 } else if (ph->wq_param_index == 1) {
151 for (i = 0; i < WQP_SIZE; i++) {
152 ph->pic_wq_param[i] = ph->wq_param_delta1[i] + wq_param_default[UNDETAILED][i];
153 }
154 } else if (ph->wq_param_index == 2) {
155 for (i = 0; i < WQP_SIZE; i++) {
156 ph->pic_wq_param[i] = ph->wq_param_delta2[i] + wq_param_default[DETAILED][i];
157 }
158 }
159
160 //!< reconstruct the weighting matrix
161 for (j = 0; j < 8; j++) {
162 for (i = 0; i < 8; i++) {
163 k = WeightQuantModel[ph->wq_model][j * 8 + i];
164 ph->pic_wq_matrix[1][j * 8 + i] = ph->pic_wq_param[k];
165 }
166 }
167
168 for (j = 0; j < 4; j++) {
169 for (i = 0; i < 4; i++) {
170 k = WeightQuantModel4x4[ph->wq_model][j * 4 + i];
171 ph->pic_wq_matrix[0][j * 4 + i] = ph->pic_wq_param[k];
172 }
173 }
174 }
175
wq_update_frame_matrix(Avs2dCtx_t * p_dec)176 static void wq_update_frame_matrix(Avs2dCtx_t *p_dec)
177 {
178 RK_U32 i, j;
179 RK_U32 size_id, wqm_size;
180 RK_U32 (*frm_wqm)[64];
181
182 Avs2dSeqHeader_t *vsh = &p_dec->vsh;
183 Avs2dPicHeader_t *ph = &p_dec->ph;
184 RK_U32 (*cur_wqm)[64] = p_dec->cur_wq_matrix;
185
186 mpp_assert(ph->pic_wq_data_index <= 2);
187
188 if (!p_dec->enable_wq) {
189 for (size_id = 0; size_id < 2; size_id++) {
190 for (i = 0; i < 64; i++) {
191 cur_wqm[size_id][i] = 64;
192 }
193 }
194 } else {
195 if (ph->pic_wq_data_index == 0) {
196 frm_wqm = vsh->seq_wq_matrix;
197 } else {
198 frm_wqm = ph->pic_wq_matrix;
199 }
200
201 for (size_id = 0; size_id < 2; size_id++) {
202 wqm_size = MPP_MIN(1 << (size_id + 2), 8);
203 for (i = 0; i < wqm_size; i++) {
204 for (j = 0; j < wqm_size; j++) {
205 cur_wqm[size_id][i * wqm_size + j] = frm_wqm[size_id][i * wqm_size + j];
206 }
207 }
208 }
209 }
210 }
211
parse_sequence_wqm(BitReadCtx_t * bitctx,Avs2dSeqHeader_t * vsh)212 static MPP_RET parse_sequence_wqm(BitReadCtx_t *bitctx, Avs2dSeqHeader_t *vsh)
213 {
214 MPP_RET ret = MPP_OK;
215 RK_U8 load_seq_wquant_data_flag;
216 RK_U32 i, j, size_id, wqm_size;
217 const RK_U32 *seq_wqm;
218
219 READ_ONEBIT(bitctx, &load_seq_wquant_data_flag);
220
221 for (size_id = 0; size_id < 2; size_id++) {
222 wqm_size = MPP_MIN(1 << (size_id + 2), 8);
223 if (load_seq_wquant_data_flag == 1) {
224 for (j = 0; j < wqm_size; j++) {
225 for (i = 0; i < wqm_size; i++) {
226 READ_UE(bitctx, &vsh->seq_wq_matrix[size_id][j * wqm_size + i]);
227 }
228 }
229 } else if (load_seq_wquant_data_flag == 0) {
230 seq_wqm = wq_get_default_matrix(size_id);
231 for (i = 0; i < (wqm_size * wqm_size); i++) {
232 vsh->seq_wq_matrix[size_id][i] = seq_wqm[i];
233 }
234 }
235 }
236
237 return ret;
238 __BITREAD_ERR:
239 return ret = bitctx->ret;
240 }
241
parse_one_rps(BitReadCtx_t * bitctx,Avs2dRps_t * rps)242 static MPP_RET parse_one_rps(BitReadCtx_t *bitctx, Avs2dRps_t *rps)
243 {
244 MPP_RET ret = MPP_OK;
245 RK_U32 j;
246
247 AVS2D_PARSE_TRACE("In");
248 READ_ONEBIT(bitctx, &rps->refered_by_others);
249
250 READ_BITS(bitctx, 3, &rps->num_of_ref);
251 AVS2D_PARSE_TRACE("refered_by_others_flag %d, num_Of_ref %d", rps->refered_by_others, rps->num_of_ref);
252 if (rps->num_of_ref > AVS2_MAX_REFS) {
253 ret = MPP_NOK;
254 mpp_err_f("invalid ref num(%d).\n", rps->num_of_ref);
255 goto __FAILED;
256 }
257 for (j = 0; j < rps->num_of_ref; j++) {
258 READ_BITS(bitctx, 6, &rps->ref_pic[j]);
259 AVS2D_PARSE_TRACE("delta_doi_of_ref_pic[%d]=%d", j, rps->ref_pic[j]);
260 }
261
262 READ_BITS(bitctx, 3, &rps->num_to_remove);
263 for (j = 0; j < rps->num_to_remove; j++) {
264 READ_BITS(bitctx, 6, &rps->remove_pic[j]);
265 AVS2D_PARSE_TRACE("num_of_removed_pic[%d]=%d", j, rps->remove_pic[j]);
266 }
267
268 READ_MARKER_BIT(bitctx);
269 AVS2D_PARSE_TRACE("Out ret %d\n", ret);
270 return ret;
271 __BITREAD_ERR:
272 AVS2D_PARSE_TRACE("Out Bit read Err\n");
273 return ret = bitctx->ret;
274 __FAILED:
275 AVS2D_PARSE_TRACE("Out Failed\n");
276 return ret;
277 }
278
avs2d_parse_sequence_header(Avs2dCtx_t * p_dec)279 MPP_RET avs2d_parse_sequence_header(Avs2dCtx_t *p_dec)
280 {
281 MPP_RET ret = MPP_OK;
282 RK_U32 i;
283 RK_U32 val_temp = 0;
284 Avs2dRps_t *rps = NULL;
285 BitReadCtx_t *bitctx = &p_dec->bitctx;
286 Avs2dSeqHeader_t *vsh = &p_dec->vsh;
287
288 AVS2D_PARSE_TRACE("Bitread buf %p, data %p, buf_len %d, left %d\n", bitctx->buf, bitctx->data_, bitctx->buf_len, bitctx->bytes_left_);
289 memset(vsh, 0, sizeof(Avs2dSeqHeader_t));
290 READ_BITS(bitctx, 8, &vsh->profile_id);
291 //!< check profile_id
292 if (vsh->profile_id != 0x12 && vsh->profile_id != 0x20 && vsh->profile_id != 0x22) {
293 ret = MPP_NOK;
294 mpp_err_f("profile_id 0x%02x is not supported.\n", vsh->profile_id);
295 goto __FAILED;
296 }
297 READ_BITS(bitctx, 8, &vsh->level_id);
298 if (vsh->level_id > 0x6A) {
299 ret = MPP_NOK;
300 mpp_err_f("level_id 0x%02x is not supported.\n", vsh->level_id);
301 goto __FAILED;
302 }
303 READ_ONEBIT(bitctx, &vsh->progressive_sequence);
304 READ_ONEBIT(bitctx, &vsh->field_coded_sequence);
305 READ_BITS(bitctx, 14, &vsh->horizontal_size);
306 READ_BITS(bitctx, 14, &vsh->vertical_size);
307 if (vsh->horizontal_size < 16 || vsh->vertical_size < 16) {
308 ret = MPP_NOK;
309 mpp_err_f("invalid sequence width(%d), height(%d).\n",
310 vsh->horizontal_size, vsh->vertical_size);
311 goto __FAILED;
312 }
313
314 READ_BITS(bitctx, 2, &vsh->chroma_format);
315 if (vsh->chroma_format != CHROMA_420) {
316 ret = MPP_NOK;
317 mpp_err_f("chroma_format 0x%02x is not supported.\n", vsh->chroma_format);
318 goto __FAILED;
319 }
320 READ_BITS(bitctx, 3, &vsh->sample_precision);
321 if (vsh->profile_id == MAIN10_PROFILE) {
322 READ_BITS(bitctx, 3, &vsh->encoding_precision);
323 } else {
324 vsh->encoding_precision = 1;
325 }
326 vsh->bit_depth = 6 + (vsh->encoding_precision << 1);
327
328 if (vsh->sample_precision < 1 || vsh->sample_precision > 2) {
329 ret = MPP_NOK;
330 mpp_err_f("sample_precision 0x%02x is not supported.\n", vsh->sample_precision);
331 goto __FAILED;
332 }
333 if (vsh->encoding_precision < 1 || vsh->encoding_precision > 2) {
334 ret = MPP_NOK;
335 mpp_err_f("encoding_precision 0x%02x is not supported.\n", vsh->encoding_precision);
336 goto __FAILED;
337 }
338
339 READ_BITS(bitctx, 4, &vsh->aspect_ratio);
340 READ_BITS(bitctx, 4, &vsh->frame_rate_code);
341 READ_BITS(bitctx, 18, &val_temp); //!< bit_rate_lower_18
342 vsh->bit_rate = val_temp << 12;
343 READ_MARKER_BIT(bitctx);
344 READ_BITS(bitctx, 12, &val_temp); //!< bit_rate_upper_12
345 vsh->bit_rate += val_temp;
346 READ_ONEBIT(bitctx, &vsh->low_delay);
347 READ_MARKER_BIT(bitctx);
348 READ_ONEBIT(bitctx, &vsh->enable_temporal_id);
349 READ_BITS(bitctx, 18, &vsh->bbv_buffer_size);
350 READ_BITS(bitctx, 3, &vsh->lcu_size);
351 if (vsh->lcu_size < 4 || vsh->lcu_size > 6) {
352 ret = MPP_NOK;
353 mpp_err_f("invalid lcu size: %d\n", vsh->lcu_size);
354 goto __FAILED;
355 }
356
357 READ_ONEBIT(bitctx, &vsh->enable_weighted_quant);
358 if (vsh->enable_weighted_quant) {
359 parse_sequence_wqm(bitctx, vsh);
360 }
361
362 READ_ONEBIT(bitctx, &val_temp);
363 vsh->enable_background_picture = val_temp ^ 0x01;
364 READ_ONEBIT(bitctx, &vsh->enable_mhp_skip);
365 READ_ONEBIT(bitctx, &vsh->enable_dhp);
366 READ_ONEBIT(bitctx, &vsh->enable_wsm);
367 READ_ONEBIT(bitctx, &vsh->enable_amp);
368 READ_ONEBIT(bitctx, &vsh->enable_nsqt);
369 READ_ONEBIT(bitctx, &vsh->enable_nsip);
370 READ_ONEBIT(bitctx, &vsh->enable_2nd_transform);
371 READ_ONEBIT(bitctx, &vsh->enable_sao);
372 READ_ONEBIT(bitctx, &vsh->enable_alf);
373 READ_ONEBIT(bitctx, &vsh->enable_pmvr);
374 READ_MARKER_BIT(bitctx);
375
376 //!< parse rps
377 READ_BITS(bitctx, 6, &vsh->num_of_rps);
378 for (i = 0; i < vsh->num_of_rps; i++) {
379 rps = &vsh->seq_rps[i];
380 AVS2D_PARSE_TRACE("--------rcs[%d]--------", i);
381 FUN_CHECK(ret = parse_one_rps(bitctx, rps));
382 }
383
384 if (vsh->low_delay == 0) {
385 READ_BITS(bitctx, 5, &vsh->picture_reorder_delay);
386 } else {
387 vsh->picture_reorder_delay = 0;
388 }
389 READ_ONEBIT(bitctx, &vsh->enable_clf);
390 READ_BITS(bitctx, 2, &val_temp); //!< reserved 2bits 00
391 if (val_temp) {
392 AVS2D_DBG(AVS2D_DBG_WARNNING, "reserver bits error.\n");
393 }
394 return ret;
395 __BITREAD_ERR:
396 return ret = bitctx->ret;
397 __FAILED:
398 return ret;
399 }
400
read_pic_alf_coeff(BitReadCtx_t * bitctx,RK_S32 * alf_coeff)401 static MPP_RET read_pic_alf_coeff(BitReadCtx_t *bitctx, RK_S32 *alf_coeff)
402 {
403 MPP_RET ret = MPP_OK;
404 RK_U32 j;
405 RK_S32 alf_sum = 0;
406
407 for (j = 0; j < ALF_MAX_COEFS; j++) {
408 READ_SE(bitctx, &alf_coeff[j]);
409 if ((j <= 7 && (alf_coeff[j] < -64 || alf_coeff[j] > 63)) ||
410 (j == 8 && (alf_coeff[j] < -1088 || alf_coeff[j] > 1071))) {
411 ret = MPP_NOK;
412 mpp_err_f("invalid alf coeff(%d).\n", alf_coeff[j]);
413 goto __FAILED;
414 }
415
416 if (j <= 7) {
417 alf_sum += (2 * alf_coeff[j]); //!< avs2 9.12.2
418 }
419 }
420 alf_coeff[8] += (64 - alf_sum);
421
422 __BITREAD_ERR:
423 return ret = bitctx->ret;
424 __FAILED:
425 return ret;
426 }
427
parse_pic_alf_params(BitReadCtx_t * bitctx,Avs2dPicHeader_t * ph)428 static MPP_RET parse_pic_alf_params(BitReadCtx_t *bitctx, Avs2dPicHeader_t *ph)
429 {
430 MPP_RET ret = MPP_OK;
431 RK_U32 i;
432
433 READ_ONEBIT(bitctx, &ph->enable_pic_alf_y);
434 READ_ONEBIT(bitctx, &ph->enable_pic_alf_cb);
435 READ_ONEBIT(bitctx, &ph->enable_pic_alf_cr);
436
437 if (ph->enable_pic_alf_y) {
438 RK_U32 pre_symbole = 0;
439 RK_U32 symbol = 0;
440
441 READ_UE(bitctx, &ph->alf_filter_num);
442 ph->alf_filter_num += 1;
443 if (ph->alf_filter_num > ALF_MAX_FILTERS) {
444 ret = MPP_NOK;
445 mpp_err_f("invalid alf filter num(%d).\n", ph->alf_filter_num);
446 goto __FAILED;
447 }
448
449 for (i = 0; i < ph->alf_filter_num; i++) {
450 if (i > 0) {
451 if (ph->alf_filter_num != 16) {
452 READ_UE(bitctx, &symbol);
453 } else {
454 symbol = 1;
455 }
456 ph->alf_filter_pattern[symbol + pre_symbole] = 1;
457 pre_symbole += symbol;
458 }
459
460 FUN_CHECK(ret = read_pic_alf_coeff(bitctx, ph->alf_coeff_y[i]));
461 }
462
463 if (pre_symbole > 15) {
464 ret = MPP_NOK;
465 mpp_err_f("invalid alf region distance.\n");
466 goto __FAILED;
467 }
468
469 //!< @see avs2 9.12.2 eg: 0000111222223333
470 ph->alf_coeff_idx_tab[0] = 0;
471 for (i = 1; i < ALF_MAX_FILTERS; i++) {
472 ph->alf_coeff_idx_tab[i] = (ph->alf_filter_pattern[i]) ?
473 (ph->alf_coeff_idx_tab[i - 1] + 1) : ph->alf_coeff_idx_tab[i - 1];
474 }
475 }
476
477 if (ph->enable_pic_alf_cb) {
478 FUN_CHECK(ret = read_pic_alf_coeff(bitctx, ph->alf_coeff_cb));
479 }
480
481 if (ph->enable_pic_alf_cr) {
482 FUN_CHECK(ret = read_pic_alf_coeff(bitctx, ph->alf_coeff_cr));
483 }
484
485 __BITREAD_ERR:
486 return ret = bitctx->ret;
487 __FAILED:
488 return ret;
489 }
490
parse_picture_header_comm(Avs2dCtx_t * p_dec)491 static MPP_RET parse_picture_header_comm(Avs2dCtx_t *p_dec)
492 {
493 MPP_RET ret = MPP_OK;
494 RK_U32 i, j;
495 RK_U32 val_temp = 0;
496
497 BitReadCtx_t *bitctx = &p_dec->bitctx;
498 Avs2dSeqHeader_t *vsh = &p_dec->vsh;
499 Avs2dPicHeader_t *ph = &p_dec->ph;
500
501 AVS2D_PARSE_TRACE("In");
502 READ_ONEBIT(bitctx, &val_temp);
503 ph->enable_loop_filter = val_temp ^ 0x01;
504 if (ph->enable_loop_filter) {
505 READ_ONEBIT(bitctx, &ph->loop_filter_parameter_flag);
506 if (ph->loop_filter_parameter_flag) {
507 READ_SE(bitctx, &ph->alpha_c_offset);
508 READ_SE(bitctx, &ph->beta_offset);
509 if (ph->alpha_c_offset < -8 || ph->alpha_c_offset > 8 ||
510 ph->beta_offset < -8 || ph->beta_offset > 8) {
511 ret = MPP_NOK;
512 mpp_err_f("invalid alpha_c_offset/beta_offset.\n");
513 goto __FAILED;
514 }
515 } else {
516 ph->alpha_c_offset = 0;
517 ph->beta_offset = 0;
518 }
519 }
520
521 READ_ONEBIT(bitctx, &val_temp);
522 ph->enable_chroma_quant_param = val_temp ^ 0x01;
523 if (ph->enable_chroma_quant_param) {
524 READ_SE(bitctx, &ph->chroma_quant_param_delta_cb);
525 READ_SE(bitctx, &ph->chroma_quant_param_delta_cr);
526 } else {
527 ph->chroma_quant_param_delta_cb = 0;
528 ph->chroma_quant_param_delta_cr = 0;
529 }
530 if (ph->chroma_quant_param_delta_cb < -16 || ph->chroma_quant_param_delta_cb > 16 ||
531 ph->chroma_quant_param_delta_cr < -16 || ph->chroma_quant_param_delta_cr > 16) {
532 ret = MPP_NOK;
533 mpp_err_f("invalid chrome quant param delta.\n");
534 goto __FAILED;
535 }
536
537 p_dec->enable_wq = 0;
538 if (vsh->enable_weighted_quant) {
539 READ_ONEBIT(bitctx, &ph->enable_pic_weight_quant);
540 if (ph->enable_pic_weight_quant) {
541 p_dec->enable_wq = 1;
542 READ_BITS(bitctx, 2, &ph->pic_wq_data_index);
543 if (ph->pic_wq_data_index == 1) {
544 SKIP_BITS(bitctx, 1);
545 READ_BITS(bitctx, 2, &ph->wq_param_index);
546 READ_BITS(bitctx, 2, &ph->wq_model);
547 if (ph->wq_param_index == 1) {
548 for (i = 0; i < 6; i++) {
549 READ_SE(bitctx, &ph->wq_param_delta1[i]);
550 }
551 } else if (ph->wq_param_index == 2) {
552 for (i = 0; i < 6; i++) {
553 READ_SE(bitctx, &ph->wq_param_delta2[i]);
554 }
555 }
556 } else if (ph->pic_wq_data_index == 2) {
557 RK_U32 size_id, wqm_size;
558 for (size_id = 0; size_id < 2; size_id++) {
559 wqm_size = MPP_MIN(1 << (size_id + 2), 8);
560 for (j = 0; j < wqm_size; j++) {
561 for (i = 0; i < wqm_size; i++) {
562 READ_UE(bitctx, &ph->pic_wq_matrix[size_id][j * wqm_size + i]);
563 }
564 }
565 }
566 }
567
568 }
569 }
570
571 if (vsh->enable_alf) {
572 FUN_CHECK(ret = parse_pic_alf_params(bitctx, ph));
573 }
574
575 AVS2D_PARSE_TRACE("Out, ret %d\n", ret);
576 return ret;
577 __BITREAD_ERR:
578 AVS2D_PARSE_TRACE("Out, Bit read Err");
579 return ret = bitctx->ret;
580 __FAILED:
581 AVS2D_PARSE_TRACE("Out, Failed");
582 return ret;
583 }
584
parse_picture_header_intra(Avs2dCtx_t * p_dec)585 static MPP_RET parse_picture_header_intra(Avs2dCtx_t *p_dec)
586 {
587 MPP_RET ret = MPP_OK;
588 RK_U32 predict = 0;
589
590 BitReadCtx_t *bitctx = &p_dec->bitctx;
591 Avs2dSeqHeader_t *vsh = &p_dec->vsh;
592 Avs2dPicHeader_t *ph = &p_dec->ph;
593 RK_U32 last_pic_out_delay = ph->picture_output_delay;
594
595 AVS2D_PARSE_TRACE("In.");
596 memset(ph, 0, sizeof(Avs2dPicHeader_t));
597 ph->picture_type = I_PICTURE;
598
599 AVS2D_PARSE_TRACE("Bitread buf %p, data %p, buf_len %d, left %d\n", bitctx->buf, bitctx->data_, bitctx->buf_len, bitctx->bytes_left_);
600 READ_BITS_LONG(bitctx, 32, &ph->bbv_delay);
601 AVS2D_PARSE_TRACE("bbv_delay %d\n", ph->bbv_delay);
602 READ_ONEBIT(bitctx, &ph->time_code_flag);
603 AVS2D_PARSE_TRACE("time_code_flag %d\n", ph->time_code_flag);
604 if (ph->time_code_flag) {
605 READ_BITS(bitctx, 24, &ph->time_code);
606 AVS2D_PARSE_TRACE("time_code %d\n", ph->time_code);
607 }
608
609 if (vsh->enable_background_picture) {
610 READ_ONEBIT(bitctx, &ph->background_picture_flag);
611 AVS2D_PARSE_TRACE("background_picture_flag %d\n", ph->background_picture_flag);
612 if (ph->background_picture_flag) {
613 READ_ONEBIT(bitctx, &ph->background_picture_output_flag);
614 AVS2D_PARSE_TRACE("background_picture_output_flag %d\n", ph->background_picture_output_flag);
615 if (ph->background_picture_output_flag) {
616 ph->picture_type = G_PICTURE;
617 } else {
618 ph->picture_type = GB_PICTURE;
619 }
620 }
621 }
622
623 READ_BITS(bitctx, 8, &ph->doi);
624 AVS2D_PARSE_TRACE("Picture type %d doi %d\n", ph->picture_type, ph->doi);
625 if (vsh->enable_temporal_id) {
626 READ_BITS(bitctx, 3, &ph->temporal_id);
627 AVS2D_PARSE_TRACE("temporal_id %d\n", ph->temporal_id);
628 }
629
630 if (vsh->low_delay == 0 && !(ph->background_picture_flag && !ph->background_picture_output_flag)) {
631 READ_UE(bitctx, &ph->picture_output_delay);
632 AVS2D_PARSE_TRACE("picture_output_delay %d\n", ph->picture_output_delay);
633 if (ph->picture_output_delay >= 64) {
634 ret = MPP_NOK;
635 mpp_err_f("invalid picture output delay(%d) intra.\n", ph->picture_output_delay);
636 goto __FAILED;
637 }
638 } else {
639 ph->picture_output_delay = last_pic_out_delay;
640 }
641
642 READ_ONEBIT(bitctx, &predict);
643 AVS2D_PARSE_TRACE("predict %d\n", predict);
644 if (predict) {
645 RK_U8 rcs_index = 0;
646 READ_BITS(bitctx, 5, &rcs_index);
647 AVS2D_PARSE_TRACE("rcs_index %d\n", rcs_index);
648 p_dec->frm_mgr.cur_rps = vsh->seq_rps[rcs_index];
649 } else {
650 FUN_CHECK(ret = parse_one_rps(bitctx, &p_dec->frm_mgr.cur_rps));
651 }
652 AVS2D_PARSE_TRACE("num_of_remove %d", p_dec->frm_mgr.cur_rps.num_to_remove);
653
654 if (vsh->low_delay) {
655 READ_UE(bitctx, &ph->bbv_check_times);
656 AVS2D_PARSE_TRACE("bbv_check_time %d\n", ph->bbv_check_times);
657 }
658
659 READ_ONEBIT(bitctx, &ph->progressive_frame);
660 AVS2D_PARSE_TRACE("progressive_frame %d\n", ph->progressive_frame);
661 if (!ph->progressive_frame) {
662 READ_ONEBIT(bitctx, &ph->picture_structure);
663 AVS2D_PARSE_TRACE("picture_structure %d\n", ph->picture_structure);
664 } else {
665 ph->picture_structure = 1; //!< frame picture
666 }
667 READ_ONEBIT(bitctx, &ph->top_field_first);
668 AVS2D_PARSE_TRACE("top_field_first %d\n", ph->top_field_first);
669 READ_ONEBIT(bitctx, &ph->repeat_first_field);
670 AVS2D_PARSE_TRACE("repeat_first_field %d\n", ph->repeat_first_field);
671 if (vsh->field_coded_sequence == 1) { //!< field picture
672 READ_ONEBIT(bitctx, &ph->is_top_field);
673 AVS2D_PARSE_TRACE("is_top_field %d\n", ph->is_top_field);
674 SKIP_BITS(bitctx, 1);
675 AVS2D_PARSE_TRACE("skip bits\n");
676 }
677 READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
678 AVS2D_PARSE_TRACE("fixed_picture_qp %d\n", ph->fixed_picture_qp);
679 READ_BITS(bitctx, 7, &ph->picture_qp);
680 AVS2D_PARSE_TRACE("picture_qp %d\n", ph->picture_qp);
681 if (ph->picture_qp > (63 + 8 * (vsh->bit_depth - 8))) {
682 ret = MPP_NOK;
683 mpp_err_f("invalid picture qp(%d).\n", ph->picture_qp);
684 goto __FAILED;
685 }
686
687 FUN_CHECK(ret = parse_picture_header_comm(p_dec));
688
689 AVS2D_PARSE_TRACE("Out. ret %d", ret);
690 return ret;
691 __BITREAD_ERR:
692 AVS2D_PARSE_TRACE("Out. Bit read ERR");
693 return ret = bitctx->ret;
694 __FAILED:
695 AVS2D_PARSE_TRACE("Out. Failed");
696 return ret;
697 }
698
parse_picture_header_inter(Avs2dCtx_t * p_dec)699 static MPP_RET parse_picture_header_inter(Avs2dCtx_t *p_dec)
700 {
701 MPP_RET ret = MPP_OK;
702 RK_U32 predict = 0;
703
704 BitReadCtx_t *bitctx = &p_dec->bitctx;
705 Avs2dSeqHeader_t *vsh = &p_dec->vsh;
706 Avs2dPicHeader_t *ph = &p_dec->ph;
707
708 memset(ph, 0, sizeof(Avs2dPicHeader_t));
709 READ_BITS_LONG(bitctx, 32, &ph->bbv_delay);
710 READ_BITS(bitctx, 2, &ph->picture_coding_type);
711 if (vsh->enable_background_picture) {
712 if (ph->picture_coding_type == 1) {
713 READ_ONEBIT(bitctx, &ph->background_pred_flag);
714 }
715
716 if (ph->picture_coding_type != 2 && !ph->background_pred_flag) {
717 READ_ONEBIT(bitctx, &ph->background_reference_flag); //!< P/F ref G/GB
718 }
719 }
720
721 if (ph->picture_coding_type == 1) {
722 ph->picture_type = ph->background_pred_flag ? S_PICTURE : P_PICTURE;
723 } else if (ph->picture_coding_type == 2) {
724 ph->picture_type = B_PICTURE;
725 } else if (ph->picture_coding_type == 3) {
726 ph->picture_type = F_PICTURE;
727 } else {
728 ret = MPP_NOK;
729 mpp_err_f("invalid picture coding type(0).\n");
730 goto __FAILED;
731 }
732
733 READ_BITS(bitctx, 8, &ph->doi);
734 AVS2D_PARSE_TRACE("Picture type %d doi %d\n", ph->picture_type, ph->doi);
735 if (vsh->enable_temporal_id) {
736 READ_BITS(bitctx, 3, &ph->temporal_id);
737 }
738
739 if (vsh->low_delay == 0) {
740 READ_UE(bitctx, &ph->picture_output_delay);
741 AVS2D_PARSE_TRACE("picture_output_delay %d\n", ph->picture_output_delay);
742 if (ph->picture_output_delay >= 64) {
743 ret = MPP_NOK;
744 mpp_err_f("invalid picture output delay(%d) intra.\n", ph->picture_output_delay);
745 goto __FAILED;
746 }
747 } else {
748 ph->picture_output_delay = 0;
749 }
750
751 READ_ONEBIT(bitctx, &predict);
752 AVS2D_PARSE_TRACE("predict %d\n", predict);
753 if (predict) {
754 RK_U8 rcs_index = 0;
755 READ_BITS(bitctx, 5, &rcs_index);
756 p_dec->frm_mgr.cur_rps = vsh->seq_rps[rcs_index];
757 } else {
758 FUN_CHECK(ret = parse_one_rps(bitctx, &p_dec->frm_mgr.cur_rps));
759 }
760 AVS2D_PARSE_TRACE("num_of_remove %d", p_dec->frm_mgr.cur_rps.num_to_remove);
761
762 if (vsh->low_delay) {
763 READ_UE(bitctx, &ph->bbv_check_times);
764 }
765
766 READ_ONEBIT(bitctx, &ph->progressive_frame);
767 if (!ph->progressive_frame) {
768 READ_ONEBIT(bitctx, &ph->picture_structure);
769 } else {
770 ph->picture_structure = 1; //!< frame picture
771 }
772 READ_ONEBIT(bitctx, &ph->top_field_first);
773 READ_ONEBIT(bitctx, &ph->repeat_first_field);
774 if (vsh->field_coded_sequence == 1) { //!< field picture
775 READ_ONEBIT(bitctx, &ph->is_top_field);
776 SKIP_BITS(bitctx, 1);
777 }
778 READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
779 READ_BITS(bitctx, 7, &ph->picture_qp);
780 if (ph->picture_qp > (63 + 8 * (vsh->bit_depth - 8))) {
781 ret = MPP_NOK;
782 mpp_err_f("invalid picture qp(%d).\n", ph->picture_qp);
783 goto __FAILED;
784 }
785 if (ph->picture_coding_type != 2 || ph->picture_structure == 0) {
786 SKIP_BITS(bitctx, 1);
787 }
788 READ_ONEBIT(bitctx, &ph->enable_random_decodable);
789
790 FUN_CHECK(ret = parse_picture_header_comm(p_dec));
791
792 return ret;
793 __BITREAD_ERR:
794 return ret = bitctx->ret;
795 __FAILED:
796 return ret;
797 }
798
avs2d_parse_picture_header(Avs2dCtx_t * p_dec,RK_U32 startcode)799 MPP_RET avs2d_parse_picture_header(Avs2dCtx_t *p_dec, RK_U32 startcode)
800 {
801 MPP_RET ret = MPP_OK;
802
803 AVS2D_PARSE_TRACE("In.");
804 switch (startcode) {
805 case AVS2_I_PICTURE_START_CODE:
806 ret = parse_picture_header_intra(p_dec);
807 break;
808 case AVS2_PB_PICTURE_START_CODE:
809 ret = parse_picture_header_inter(p_dec);
810 break;
811 default:
812 ret = MPP_NOK;
813 mpp_err_f("invalid picture startcode(%d).\n", startcode);
814 return ret;
815 }
816
817 wq_init_frame_quant_param(p_dec);
818 wq_update_frame_matrix(p_dec);
819
820 AVS2D_PARSE_TRACE("Out.");
821 return ret;
822 }
823
824
825