xref: /OK3568_Linux_fs/external/mpp/mpp/codec/dec/avs2/avs2d_ps.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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     mpp_err_f("video resolution %dx%d\n", vsh->horizontal_size, vsh->vertical_size);
308     if (vsh->horizontal_size < 16 || vsh->vertical_size < 16) {
309         ret = MPP_NOK;
310         mpp_err_f("invalid sequence width(%d), height(%d).\n",
311                   vsh->horizontal_size, vsh->vertical_size);
312         goto __FAILED;
313     }
314 
315     READ_BITS(bitctx, 2,  &vsh->chroma_format);
316     if (vsh->chroma_format != CHROMA_420) {
317         ret = MPP_NOK;
318         mpp_err_f("chroma_format 0x%02x is not supported.\n", vsh->chroma_format);
319         goto __FAILED;
320     }
321     READ_BITS(bitctx, 3, &vsh->sample_precision);
322     if (vsh->profile_id == MAIN10_PROFILE) {
323         READ_BITS(bitctx, 3, &vsh->encoding_precision);
324     } else {
325         vsh->encoding_precision = 1;
326     }
327     vsh->bit_depth = 6 + (vsh->encoding_precision << 1);
328 
329     if (vsh->sample_precision < 1 || vsh->sample_precision > 2) {
330         ret = MPP_NOK;
331         mpp_err_f("sample_precision 0x%02x is not supported.\n", vsh->sample_precision);
332         goto __FAILED;
333     }
334     if (vsh->encoding_precision < 1 || vsh->encoding_precision > 2) {
335         ret = MPP_NOK;
336         mpp_err_f("encoding_precision 0x%02x is not supported.\n", vsh->encoding_precision);
337         goto __FAILED;
338     }
339 
340     READ_BITS(bitctx, 4, &vsh->aspect_ratio);
341     READ_BITS(bitctx, 4, &vsh->frame_rate_code);
342     READ_BITS(bitctx, 18, &val_temp); //!< bit_rate_lower_18
343     vsh->bit_rate = val_temp << 12;
344     READ_MARKER_BIT(bitctx);
345     READ_BITS(bitctx, 12, &val_temp); //!< bit_rate_upper_12
346     vsh->bit_rate += val_temp;
347     READ_ONEBIT(bitctx, &vsh->low_delay);
348     READ_MARKER_BIT(bitctx);
349     READ_ONEBIT(bitctx, &vsh->enable_temporal_id);
350     READ_BITS(bitctx, 18, &vsh->bbv_buffer_size);
351     READ_BITS(bitctx, 3, &vsh->lcu_size);
352     if (vsh->lcu_size < 4 || vsh->lcu_size > 6) {
353         ret = MPP_NOK;
354         mpp_err_f("invalid lcu size: %d\n", vsh->lcu_size);
355         goto __FAILED;
356     }
357 
358     READ_ONEBIT(bitctx, &vsh->enable_weighted_quant);
359     if (vsh->enable_weighted_quant) {
360         parse_sequence_wqm(bitctx, vsh);
361     }
362 
363     READ_ONEBIT(bitctx, &val_temp);
364     vsh->enable_background_picture = val_temp ^ 0x01;
365     READ_ONEBIT(bitctx, &vsh->enable_mhp_skip);
366     READ_ONEBIT(bitctx, &vsh->enable_dhp);
367     READ_ONEBIT(bitctx, &vsh->enable_wsm);
368     READ_ONEBIT(bitctx, &vsh->enable_amp);
369     READ_ONEBIT(bitctx, &vsh->enable_nsqt);
370     READ_ONEBIT(bitctx, &vsh->enable_nsip);
371     READ_ONEBIT(bitctx, &vsh->enable_2nd_transform);
372     READ_ONEBIT(bitctx, &vsh->enable_sao);
373     READ_ONEBIT(bitctx, &vsh->enable_alf);
374     READ_ONEBIT(bitctx, &vsh->enable_pmvr);
375     READ_MARKER_BIT(bitctx);
376 
377     //!< parse rps
378     READ_BITS(bitctx, 6, &vsh->num_of_rps);
379     for (i = 0; i < vsh->num_of_rps; i++) {
380         rps = &vsh->seq_rps[i];
381         AVS2D_PARSE_TRACE("--------rcs[%d]--------", i);
382         FUN_CHECK(ret = parse_one_rps(bitctx, rps));
383     }
384 
385     if (vsh->low_delay == 0) {
386         READ_BITS(bitctx, 5, &vsh->picture_reorder_delay);
387     } else {
388         vsh->picture_reorder_delay = 0;
389     }
390     READ_ONEBIT(bitctx, &vsh->enable_clf);
391     READ_BITS(bitctx, 2, &val_temp); //!< reserved 2bits 00
392     if (val_temp) {
393         AVS2D_DBG(AVS2D_DBG_WARNNING, "reserver bits error.\n");
394     }
395     return ret;
396 __BITREAD_ERR:
397     return ret = bitctx->ret;
398 __FAILED:
399     return ret;
400 }
401 
read_pic_alf_coeff(BitReadCtx_t * bitctx,RK_S32 * alf_coeff)402 static MPP_RET read_pic_alf_coeff(BitReadCtx_t *bitctx, RK_S32 *alf_coeff)
403 {
404     MPP_RET ret = MPP_OK;
405     RK_U32 j;
406     RK_S32 alf_sum = 0;
407 
408     for (j = 0; j < ALF_MAX_COEFS; j++) {
409         READ_SE(bitctx, &alf_coeff[j]);
410         if ((j <= 7 && (alf_coeff[j] < -64 || alf_coeff[j] > 63)) ||
411             (j == 8 && (alf_coeff[j] < -1088 || alf_coeff[j] > 1071))) {
412             ret = MPP_NOK;
413             mpp_err_f("invalid alf coeff(%d).\n", alf_coeff[j]);
414             goto __FAILED;
415         }
416 
417         if (j <= 7) {
418             alf_sum += (2 * alf_coeff[j]); //!< avs2 9.12.2
419         }
420     }
421     alf_coeff[8] += (64 - alf_sum);
422 
423 __BITREAD_ERR:
424     return ret = bitctx->ret;
425 __FAILED:
426     return ret;
427 }
428 
parse_pic_alf_params(BitReadCtx_t * bitctx,Avs2dPicHeader_t * ph)429 static MPP_RET parse_pic_alf_params(BitReadCtx_t *bitctx, Avs2dPicHeader_t *ph)
430 {
431     MPP_RET ret = MPP_OK;
432     RK_U32 i;
433 
434     READ_ONEBIT(bitctx, &ph->enable_pic_alf_y);
435     READ_ONEBIT(bitctx, &ph->enable_pic_alf_cb);
436     READ_ONEBIT(bitctx, &ph->enable_pic_alf_cr);
437 
438     if (ph->enable_pic_alf_y) {
439         RK_U32 pre_symbole = 0;
440         RK_U32 symbol = 0;
441 
442         READ_UE(bitctx, &ph->alf_filter_num);
443         ph->alf_filter_num += 1;
444         if (ph->alf_filter_num > ALF_MAX_FILTERS) {
445             ret = MPP_NOK;
446             mpp_err_f("invalid alf filter num(%d).\n", ph->alf_filter_num);
447             goto __FAILED;
448         }
449 
450         for (i = 0; i < ph->alf_filter_num; i++) {
451             if (i > 0) {
452                 if (ph->alf_filter_num != 16) {
453                     READ_UE(bitctx, &symbol);
454                 } else {
455                     symbol = 1;
456                 }
457                 ph->alf_filter_pattern[symbol + pre_symbole] = 1;
458                 pre_symbole += symbol;
459             }
460 
461             FUN_CHECK(ret = read_pic_alf_coeff(bitctx, ph->alf_coeff_y[i]));
462         }
463 
464         if (pre_symbole > 15) {
465             ret = MPP_NOK;
466             mpp_err_f("invalid alf region distance.\n");
467             goto __FAILED;
468         }
469 
470         //!< @see avs2 9.12.2 eg: 0000111222223333
471         ph->alf_coeff_idx_tab[0] = 0;
472         for (i = 1; i < ALF_MAX_FILTERS; i++) {
473             ph->alf_coeff_idx_tab[i] = (ph->alf_filter_pattern[i]) ?
474                                        (ph->alf_coeff_idx_tab[i - 1] + 1) : ph->alf_coeff_idx_tab[i - 1];
475         }
476     }
477 
478     if (ph->enable_pic_alf_cb) {
479         FUN_CHECK(ret = read_pic_alf_coeff(bitctx, ph->alf_coeff_cb));
480     }
481 
482     if (ph->enable_pic_alf_cr) {
483         FUN_CHECK(ret = read_pic_alf_coeff(bitctx, ph->alf_coeff_cr));
484     }
485 
486 __BITREAD_ERR:
487     return ret = bitctx->ret;
488 __FAILED:
489     return ret;
490 }
491 
parse_picture_header_comm(Avs2dCtx_t * p_dec)492 static MPP_RET parse_picture_header_comm(Avs2dCtx_t *p_dec)
493 {
494     MPP_RET ret = MPP_OK;
495     RK_U32 i, j;
496     RK_U32 val_temp = 0;
497 
498     BitReadCtx_t *bitctx  = &p_dec->bitctx;
499     Avs2dSeqHeader_t *vsh = &p_dec->vsh;
500     Avs2dPicHeader_t *ph  = &p_dec->ph;
501 
502     AVS2D_PARSE_TRACE("In");
503     READ_ONEBIT(bitctx, &val_temp);
504     ph->enable_loop_filter = val_temp ^ 0x01;
505     if (ph->enable_loop_filter) {
506         READ_ONEBIT(bitctx, &ph->loop_filter_parameter_flag);
507         if (ph->loop_filter_parameter_flag) {
508             READ_SE(bitctx, &ph->alpha_c_offset);
509             READ_SE(bitctx, &ph->beta_offset);
510             if (ph->alpha_c_offset < -8 || ph->alpha_c_offset > 8 ||
511                 ph->beta_offset < -8 || ph->beta_offset > 8) {
512                 ret = MPP_NOK;
513                 mpp_err_f("invalid alpha_c_offset/beta_offset.\n");
514                 goto __FAILED;
515             }
516         } else {
517             ph->alpha_c_offset = 0;
518             ph->beta_offset    = 0;
519         }
520     }
521 
522     READ_ONEBIT(bitctx, &val_temp);
523     ph->enable_chroma_quant_param = val_temp ^ 0x01;
524     if (ph->enable_chroma_quant_param) {
525         READ_SE(bitctx, &ph->chroma_quant_param_delta_cb);
526         READ_SE(bitctx, &ph->chroma_quant_param_delta_cr);
527     } else {
528         ph->chroma_quant_param_delta_cb = 0;
529         ph->chroma_quant_param_delta_cr = 0;
530     }
531     if (ph->chroma_quant_param_delta_cb < -16 || ph->chroma_quant_param_delta_cb > 16 ||
532         ph->chroma_quant_param_delta_cr < -16 || ph->chroma_quant_param_delta_cr > 16) {
533         ret = MPP_NOK;
534         mpp_err_f("invalid chrome quant param delta.\n");
535         goto __FAILED;
536     }
537 
538     p_dec->enable_wq = 0;
539     if (vsh->enable_weighted_quant) {
540         READ_ONEBIT(bitctx, &ph->enable_pic_weight_quant);
541         if (ph->enable_pic_weight_quant) {
542             p_dec->enable_wq = 1;
543             READ_BITS(bitctx, 2, &ph->pic_wq_data_index);
544             if (ph->pic_wq_data_index == 1) {
545                 SKIP_BITS(bitctx, 1);
546                 READ_BITS(bitctx, 2, &ph->wq_param_index);
547                 READ_BITS(bitctx, 2, &ph->wq_model);
548                 if (ph->wq_param_index == 1) {
549                     for (i = 0; i < 6; i++) {
550                         READ_SE(bitctx, &ph->wq_param_delta1[i]);
551                     }
552                 } else if (ph->wq_param_index == 2) {
553                     for (i = 0; i < 6; i++) {
554                         READ_SE(bitctx, &ph->wq_param_delta2[i]);
555                     }
556                 }
557             } else if (ph->pic_wq_data_index == 2) {
558                 RK_U32 size_id, wqm_size;
559                 for (size_id = 0; size_id < 2; size_id++) {
560                     wqm_size = MPP_MIN(1 << (size_id + 2), 8);
561                     for (j = 0; j < wqm_size; j++) {
562                         for (i = 0; i < wqm_size; i++) {
563                             READ_UE(bitctx, &ph->pic_wq_matrix[size_id][j * wqm_size + i]);
564                         }
565                     }
566                 }
567             }
568 
569         }
570     }
571 
572     if (vsh->enable_alf) {
573         FUN_CHECK(ret = parse_pic_alf_params(bitctx, ph));
574     }
575 
576     AVS2D_PARSE_TRACE("Out, ret %d\n", ret);
577     return ret;
578 __BITREAD_ERR:
579     AVS2D_PARSE_TRACE("Out, Bit read Err");
580     return ret = bitctx->ret;
581 __FAILED:
582     AVS2D_PARSE_TRACE("Out, Failed");
583     return ret;
584 }
585 
parse_picture_header_intra(Avs2dCtx_t * p_dec)586 static MPP_RET parse_picture_header_intra(Avs2dCtx_t *p_dec)
587 {
588     MPP_RET ret = MPP_OK;
589     RK_U32 predict = 0;
590 
591     BitReadCtx_t *bitctx  = &p_dec->bitctx;
592     Avs2dSeqHeader_t *vsh = &p_dec->vsh;
593     Avs2dPicHeader_t *ph  = &p_dec->ph;
594     RK_U32 last_pic_out_delay = ph->picture_output_delay;
595 
596     AVS2D_PARSE_TRACE("In.");
597     memset(ph, 0, sizeof(Avs2dPicHeader_t));
598     ph->picture_type = I_PICTURE;
599 
600     AVS2D_PARSE_TRACE("Bitread buf %p, data %p, buf_len %d, left %d\n", bitctx->buf, bitctx->data_, bitctx->buf_len, bitctx->bytes_left_);
601     READ_BITS_LONG(bitctx, 32, &ph->bbv_delay);
602     AVS2D_PARSE_TRACE("bbv_delay %d\n", ph->bbv_delay);
603     READ_ONEBIT(bitctx, &ph->time_code_flag);
604     AVS2D_PARSE_TRACE("time_code_flag %d\n", ph->time_code_flag);
605     if (ph->time_code_flag) {
606         READ_BITS(bitctx, 24, &ph->time_code);
607         AVS2D_PARSE_TRACE("time_code %d\n", ph->time_code);
608     }
609 
610     if (vsh->enable_background_picture) {
611         READ_ONEBIT(bitctx, &ph->background_picture_flag);
612         AVS2D_PARSE_TRACE("background_picture_flag %d\n", ph->background_picture_flag);
613         if (ph->background_picture_flag) {
614             READ_ONEBIT(bitctx, &ph->background_picture_output_flag);
615             AVS2D_PARSE_TRACE("background_picture_output_flag %d\n", ph->background_picture_output_flag);
616             if (ph->background_picture_output_flag) {
617                 ph->picture_type = G_PICTURE;
618             } else {
619                 ph->picture_type = GB_PICTURE;
620             }
621         }
622     }
623 
624     READ_BITS(bitctx, 8, &ph->doi);
625     AVS2D_PARSE_TRACE("Picture type %d doi %d\n", ph->picture_type, ph->doi);
626     if (vsh->enable_temporal_id) {
627         READ_BITS(bitctx, 3, &ph->temporal_id);
628         AVS2D_PARSE_TRACE("temporal_id %d\n", ph->temporal_id);
629     }
630 
631     if (vsh->low_delay == 0 && !(ph->background_picture_flag && !ph->background_picture_output_flag)) {
632         READ_UE(bitctx, &ph->picture_output_delay);
633         AVS2D_PARSE_TRACE("picture_output_delay %d\n", ph->picture_output_delay);
634         if (ph->picture_output_delay >= 64) {
635             ret = MPP_NOK;
636             mpp_err_f("invalid picture output delay(%d) intra.\n", ph->picture_output_delay);
637             goto __FAILED;
638         }
639     } else {
640         ph->picture_output_delay = last_pic_out_delay;
641     }
642 
643     READ_ONEBIT(bitctx, &predict);
644     AVS2D_PARSE_TRACE("predict %d\n", predict);
645     if (predict) {
646         RK_U8 rcs_index = 0;
647         READ_BITS(bitctx, 5, &rcs_index);
648         AVS2D_PARSE_TRACE("rcs_index %d\n", rcs_index);
649         p_dec->frm_mgr.cur_rps = vsh->seq_rps[rcs_index];
650     } else {
651         FUN_CHECK(ret = parse_one_rps(bitctx, &p_dec->frm_mgr.cur_rps));
652     }
653     AVS2D_PARSE_TRACE("num_of_remove %d", p_dec->frm_mgr.cur_rps.num_to_remove);
654 
655     if (vsh->low_delay) {
656         READ_UE(bitctx, &ph->bbv_check_times);
657         AVS2D_PARSE_TRACE("bbv_check_time %d\n", ph->bbv_check_times);
658     }
659 
660     READ_ONEBIT(bitctx, &ph->progressive_frame);
661     AVS2D_PARSE_TRACE("progressive_frame %d\n", ph->progressive_frame);
662     if (!ph->progressive_frame) {
663         READ_ONEBIT(bitctx, &ph->picture_structure);
664         AVS2D_PARSE_TRACE("picture_structure %d\n", ph->picture_structure);
665     } else {
666         ph->picture_structure = 1; //!< frame picture
667     }
668     READ_ONEBIT(bitctx, &ph->top_field_first);
669     AVS2D_PARSE_TRACE("top_field_first %d\n", ph->top_field_first);
670     READ_ONEBIT(bitctx, &ph->repeat_first_field);
671     AVS2D_PARSE_TRACE("repeat_first_field %d\n", ph->repeat_first_field);
672     if (vsh->field_coded_sequence == 1) { //!< field picture
673         READ_ONEBIT(bitctx, &ph->is_top_field);
674         AVS2D_PARSE_TRACE("is_top_field %d\n", ph->is_top_field);
675         SKIP_BITS(bitctx, 1);
676         AVS2D_PARSE_TRACE("skip bits\n");
677     }
678     READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
679     AVS2D_PARSE_TRACE("fixed_picture_qp %d\n", ph->fixed_picture_qp);
680     READ_BITS(bitctx, 7, &ph->picture_qp);
681     AVS2D_PARSE_TRACE("picture_qp %d\n", ph->picture_qp);
682     if (ph->picture_qp > (63 + 8 * (vsh->bit_depth - 8))) {
683         ret = MPP_NOK;
684         mpp_err_f("invalid picture qp(%d).\n", ph->picture_qp);
685         goto __FAILED;
686     }
687 
688     FUN_CHECK(ret = parse_picture_header_comm(p_dec));
689 
690     AVS2D_PARSE_TRACE("Out. ret %d", ret);
691     return ret;
692 __BITREAD_ERR:
693     AVS2D_PARSE_TRACE("Out. Bit read ERR");
694     return ret = bitctx->ret;
695 __FAILED:
696     AVS2D_PARSE_TRACE("Out. Failed");
697     return ret;
698 }
699 
parse_picture_header_inter(Avs2dCtx_t * p_dec)700 static MPP_RET parse_picture_header_inter(Avs2dCtx_t *p_dec)
701 {
702     MPP_RET ret = MPP_OK;
703     RK_U32 predict = 0;
704 
705     BitReadCtx_t *bitctx  = &p_dec->bitctx;
706     Avs2dSeqHeader_t *vsh = &p_dec->vsh;
707     Avs2dPicHeader_t *ph  = &p_dec->ph;
708 
709     memset(ph, 0, sizeof(Avs2dPicHeader_t));
710     READ_BITS_LONG(bitctx, 32, &ph->bbv_delay);
711     READ_BITS(bitctx, 2, &ph->picture_coding_type);
712     if (vsh->enable_background_picture) {
713         if (ph->picture_coding_type == 1) {
714             READ_ONEBIT(bitctx, &ph->background_pred_flag);
715         }
716 
717         if (ph->picture_coding_type != 2 && !ph->background_pred_flag) {
718             READ_ONEBIT(bitctx, &ph->background_reference_flag); //!< P/F ref G/GB
719         }
720     }
721 
722     if (ph->picture_coding_type == 1) {
723         ph->picture_type = ph->background_pred_flag ? S_PICTURE : P_PICTURE;
724     } else if (ph->picture_coding_type == 2) {
725         ph->picture_type = B_PICTURE;
726     } else if (ph->picture_coding_type == 3) {
727         ph->picture_type = F_PICTURE;
728     } else {
729         ret = MPP_NOK;
730         mpp_err_f("invalid picture coding type(0).\n");
731         goto __FAILED;
732     }
733 
734     READ_BITS(bitctx, 8, &ph->doi);
735     AVS2D_PARSE_TRACE("Picture type %d doi %d\n", ph->picture_type, ph->doi);
736     if (vsh->enable_temporal_id) {
737         READ_BITS(bitctx, 3, &ph->temporal_id);
738     }
739 
740     if (vsh->low_delay == 0) {
741         READ_UE(bitctx, &ph->picture_output_delay);
742         AVS2D_PARSE_TRACE("picture_output_delay %d\n", ph->picture_output_delay);
743         if (ph->picture_output_delay >= 64) {
744             ret = MPP_NOK;
745             mpp_err_f("invalid picture output delay(%d) intra.\n", ph->picture_output_delay);
746             goto __FAILED;
747         }
748     } else {
749         ph->picture_output_delay = 0;
750     }
751 
752     READ_ONEBIT(bitctx, &predict);
753     AVS2D_PARSE_TRACE("predict %d\n", predict);
754     if (predict) {
755         RK_U8 rcs_index = 0;
756         READ_BITS(bitctx, 5, &rcs_index);
757         p_dec->frm_mgr.cur_rps = vsh->seq_rps[rcs_index];
758     } else {
759         FUN_CHECK(ret = parse_one_rps(bitctx, &p_dec->frm_mgr.cur_rps));
760     }
761     AVS2D_PARSE_TRACE("num_of_remove %d", p_dec->frm_mgr.cur_rps.num_to_remove);
762 
763     if (vsh->low_delay) {
764         READ_UE(bitctx, &ph->bbv_check_times);
765     }
766 
767     READ_ONEBIT(bitctx, &ph->progressive_frame);
768     if (!ph->progressive_frame) {
769         READ_ONEBIT(bitctx, &ph->picture_structure);
770     } else {
771         ph->picture_structure = 1; //!< frame picture
772     }
773     READ_ONEBIT(bitctx, &ph->top_field_first);
774     READ_ONEBIT(bitctx, &ph->repeat_first_field);
775     if (vsh->field_coded_sequence == 1) { //!< field picture
776         READ_ONEBIT(bitctx, &ph->is_top_field);
777         SKIP_BITS(bitctx, 1);
778     }
779     READ_ONEBIT(bitctx, &ph->fixed_picture_qp);
780     READ_BITS(bitctx, 7, &ph->picture_qp);
781     if (ph->picture_qp > (63 + 8 * (vsh->bit_depth - 8))) {
782         ret = MPP_NOK;
783         mpp_err_f("invalid picture qp(%d).\n", ph->picture_qp);
784         goto __FAILED;
785     }
786     if (ph->picture_coding_type != 2 || ph->picture_structure == 0) {
787         SKIP_BITS(bitctx, 1);
788     }
789     READ_ONEBIT(bitctx, &ph->enable_random_decodable);
790 
791     FUN_CHECK(ret = parse_picture_header_comm(p_dec));
792 
793     return ret;
794 __BITREAD_ERR:
795     return ret = bitctx->ret;
796 __FAILED:
797     return ret;
798 }
799 
avs2d_parse_picture_header(Avs2dCtx_t * p_dec,RK_U32 startcode)800 MPP_RET avs2d_parse_picture_header(Avs2dCtx_t *p_dec, RK_U32 startcode)
801 {
802     MPP_RET ret = MPP_OK;
803 
804     AVS2D_PARSE_TRACE("In.");
805     switch (startcode) {
806     case AVS2_I_PICTURE_START_CODE:
807         ret = parse_picture_header_intra(p_dec);
808         break;
809     case AVS2_PB_PICTURE_START_CODE:
810         ret = parse_picture_header_inter(p_dec);
811         break;
812     default:
813         ret = MPP_NOK;
814         mpp_err_f("invalid picture startcode(%d).\n", startcode);
815         return ret;
816     }
817 
818     wq_init_frame_quant_param(p_dec);
819     wq_update_frame_matrix(p_dec);
820 
821     AVS2D_PARSE_TRACE("Out.");
822     return ret;
823 }
824 
825 
826