1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright 2021 Rockchip Electronics Co. LTD
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Licensed under the Apache License, Version 2.0 (the "License");
5*4882a593Smuzhiyun * you may not use this file except in compliance with the License.
6*4882a593Smuzhiyun * You may obtain a copy of the License at
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun * http://www.apache.org/licenses/LICENSE-2.0
9*4882a593Smuzhiyun *
10*4882a593Smuzhiyun * Unless required by applicable law or agreed to in writing, software
11*4882a593Smuzhiyun * distributed under the License is distributed on an "AS IS" BASIS,
12*4882a593Smuzhiyun * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*4882a593Smuzhiyun * See the License for the specific language governing permissions and
14*4882a593Smuzhiyun * limitations under the License.
15*4882a593Smuzhiyun */
16*4882a593Smuzhiyun #define MODULE_TAG "av1d_cbs"
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun #include <string.h>
19*4882a593Smuzhiyun
20*4882a593Smuzhiyun #include "mpp_mem.h"
21*4882a593Smuzhiyun #include "mpp_debug.h"
22*4882a593Smuzhiyun #include "mpp_bitread.h"
23*4882a593Smuzhiyun #include "mpp_bitwrite.h"
24*4882a593Smuzhiyun #include "rk_hdr_meta_com.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include "av1d_parser.h"
27*4882a593Smuzhiyun
28*4882a593Smuzhiyun #ifndef UINT32_MAX
29*4882a593Smuzhiyun #define UINT32_MAX 0xFFFFFFFF
30*4882a593Smuzhiyun #endif
31*4882a593Smuzhiyun
32*4882a593Smuzhiyun #ifndef INT_MAX
33*4882a593Smuzhiyun #define INT_MAX 2147483647 /* maximum (signed) int value */
34*4882a593Smuzhiyun #endif
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun #define BUFFER_PADDING_SIZE 64
37*4882a593Smuzhiyun #define MAX_UINT_BITS(length) ((UINT64_C(1) << (length)) - 1)
38*4882a593Smuzhiyun #define MAX_INT_BITS(length) ((INT64_C(1) << ((length) - 1)) - 1)
39*4882a593Smuzhiyun #define MIN_INT_BITS(length) (-(INT64_C(1) << ((length) - 1)))
40*4882a593Smuzhiyun
41*4882a593Smuzhiyun /**
42*4882a593Smuzhiyun * Clip a signed integer into the -(2^p),(2^p-1) range.
43*4882a593Smuzhiyun * @param a value to clip
44*4882a593Smuzhiyun * @param p bit position to clip at
45*4882a593Smuzhiyun * @return clipped value
46*4882a593Smuzhiyun */
mpp_clip_uintp2(RK_S32 a,RK_S32 p)47*4882a593Smuzhiyun static RK_U32 mpp_clip_uintp2(RK_S32 a, RK_S32 p)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun if (a & ~((1 << p) - 1)) return -a >> 31 & ((1 << p) - 1);
50*4882a593Smuzhiyun else return a;
51*4882a593Smuzhiyun }
52*4882a593Smuzhiyun
mpp_av1_read_uvlc(BitReadCtx_t * gbc,const char * name,RK_U32 * write_to,RK_U32 range_min,RK_U32 range_max)53*4882a593Smuzhiyun static RK_S32 mpp_av1_read_uvlc(BitReadCtx_t *gbc, const char *name, RK_U32 *write_to,
54*4882a593Smuzhiyun RK_U32 range_min, RK_U32 range_max)
55*4882a593Smuzhiyun {
56*4882a593Smuzhiyun RK_U32 value;
57*4882a593Smuzhiyun
58*4882a593Smuzhiyun mpp_read_ue(gbc, &value);
59*4882a593Smuzhiyun
60*4882a593Smuzhiyun if (value < range_min || value > range_max) {
61*4882a593Smuzhiyun mpp_err_f("%s out of range: "
62*4882a593Smuzhiyun "%d, but must be in [%d,%d].\n",
63*4882a593Smuzhiyun name, value, range_min, range_max);
64*4882a593Smuzhiyun return MPP_NOK;
65*4882a593Smuzhiyun }
66*4882a593Smuzhiyun *write_to = value;
67*4882a593Smuzhiyun return MPP_OK;
68*4882a593Smuzhiyun }
69*4882a593Smuzhiyun
70*4882a593Smuzhiyun
mpp_av1_read_leb128(BitReadCtx_t * gbc,RK_U64 * write_to)71*4882a593Smuzhiyun static RK_S32 mpp_av1_read_leb128(BitReadCtx_t *gbc, RK_U64 *write_to)
72*4882a593Smuzhiyun {
73*4882a593Smuzhiyun RK_U64 value;
74*4882a593Smuzhiyun RK_S32 err = 0, i;
75*4882a593Smuzhiyun
76*4882a593Smuzhiyun value = 0;
77*4882a593Smuzhiyun for (i = 0; i < 8; i++) {
78*4882a593Smuzhiyun RK_U32 byte;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun READ_BITS(gbc, 8, &byte);
81*4882a593Smuzhiyun
82*4882a593Smuzhiyun if (err < 0)
83*4882a593Smuzhiyun return err;
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun value |= (RK_U64)(byte & 0x7f) << (i * 7);
86*4882a593Smuzhiyun if (!(byte & 0x80))
87*4882a593Smuzhiyun break;
88*4882a593Smuzhiyun }
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun if (value > UINT32_MAX)
91*4882a593Smuzhiyun return MPP_NOK;
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun *write_to = value;
95*4882a593Smuzhiyun return MPP_OK;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun __bitread_error:
98*4882a593Smuzhiyun return MPP_NOK;
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
mpp_av1_read_ns(BitReadCtx_t * gbc,const char * name,RK_U32 n,RK_U32 * write_to)102*4882a593Smuzhiyun static RK_S32 mpp_av1_read_ns(BitReadCtx_t *gbc, const char *name,
103*4882a593Smuzhiyun RK_U32 n, RK_U32 *write_to)
104*4882a593Smuzhiyun {
105*4882a593Smuzhiyun RK_U32 m, v, extra_bit, value;
106*4882a593Smuzhiyun RK_S32 w;
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun w = mpp_log2(n) + 1;
109*4882a593Smuzhiyun m = (1 << w) - n;
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun if (mpp_get_bits_left(gbc) < w) {
112*4882a593Smuzhiyun mpp_err_f("Invalid non-symmetric value at "
113*4882a593Smuzhiyun "%s: bitstream ended.\n", name);
114*4882a593Smuzhiyun return MPP_NOK;
115*4882a593Smuzhiyun }
116*4882a593Smuzhiyun if (w - 1 > 0)
117*4882a593Smuzhiyun READ_BITS(gbc, w - 1, &v);
118*4882a593Smuzhiyun else
119*4882a593Smuzhiyun v = 0;
120*4882a593Smuzhiyun
121*4882a593Smuzhiyun if (v < m) {
122*4882a593Smuzhiyun value = v;
123*4882a593Smuzhiyun } else {
124*4882a593Smuzhiyun READ_ONEBIT(gbc, &extra_bit);
125*4882a593Smuzhiyun value = (v << 1) - m + extra_bit;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun
128*4882a593Smuzhiyun *write_to = value;
129*4882a593Smuzhiyun return MPP_OK;
130*4882a593Smuzhiyun
131*4882a593Smuzhiyun __bitread_error:
132*4882a593Smuzhiyun return MPP_NOK;
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun }
135*4882a593Smuzhiyun
mpp_av1_read_increment(BitReadCtx_t * gbc,RK_U32 range_min,RK_U32 range_max,const char * name,RK_U32 * write_to)136*4882a593Smuzhiyun static RK_S32 mpp_av1_read_increment(BitReadCtx_t *gbc, RK_U32 range_min,
137*4882a593Smuzhiyun RK_U32 range_max, const char *name,
138*4882a593Smuzhiyun RK_U32 *write_to)
139*4882a593Smuzhiyun {
140*4882a593Smuzhiyun RK_U32 value;
141*4882a593Smuzhiyun RK_S32 i;
142*4882a593Smuzhiyun RK_S8 bits[33];
143*4882a593Smuzhiyun
144*4882a593Smuzhiyun mpp_assert(range_min <= range_max && range_max - range_min < sizeof(bits) - 1);
145*4882a593Smuzhiyun
146*4882a593Smuzhiyun for (i = 0, value = range_min; value < range_max;) {
147*4882a593Smuzhiyun RK_U8 tmp = 0;
148*4882a593Smuzhiyun if (mpp_get_bits_left(gbc) < 1) {
149*4882a593Smuzhiyun mpp_err_f("Invalid increment value at "
150*4882a593Smuzhiyun "%s: bitstream ended.\n", name);
151*4882a593Smuzhiyun return MPP_NOK;
152*4882a593Smuzhiyun }
153*4882a593Smuzhiyun READ_ONEBIT(gbc, &tmp);
154*4882a593Smuzhiyun if (tmp) {
155*4882a593Smuzhiyun bits[i++] = '1';
156*4882a593Smuzhiyun ++value;
157*4882a593Smuzhiyun } else {
158*4882a593Smuzhiyun bits[i++] = '0';
159*4882a593Smuzhiyun break;
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun }
162*4882a593Smuzhiyun *write_to = value;
163*4882a593Smuzhiyun return MPP_OK;
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun __bitread_error:
166*4882a593Smuzhiyun return MPP_NOK;
167*4882a593Smuzhiyun }
168*4882a593Smuzhiyun
mpp_av1_read_unsigned(BitReadCtx_t * gbc,RK_S32 width,const char * name,RK_U32 * write_to,RK_U32 range_min,RK_U32 range_max)169*4882a593Smuzhiyun RK_S32 mpp_av1_read_unsigned(BitReadCtx_t *gbc,
170*4882a593Smuzhiyun RK_S32 width, const char *name,
171*4882a593Smuzhiyun RK_U32 *write_to, RK_U32 range_min,
172*4882a593Smuzhiyun RK_U32 range_max)
173*4882a593Smuzhiyun {
174*4882a593Smuzhiyun RK_U32 value;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun mpp_assert(width > 0 && width <= 32);
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if (mpp_get_bits_left(gbc) < width) {
179*4882a593Smuzhiyun mpp_err_f("Invalid value at "
180*4882a593Smuzhiyun "%s: bitstream ended.\n", name);
181*4882a593Smuzhiyun return MPP_NOK;
182*4882a593Smuzhiyun }
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun READ_BITS(gbc, width, &value);
185*4882a593Smuzhiyun
186*4882a593Smuzhiyun if (value < range_min || value > range_max) {
187*4882a593Smuzhiyun mpp_err_f("%s out of range: "
188*4882a593Smuzhiyun "%d, but must be in [%d,%d].\n",
189*4882a593Smuzhiyun name, value, range_min, range_max);
190*4882a593Smuzhiyun return MPP_NOK;
191*4882a593Smuzhiyun }
192*4882a593Smuzhiyun
193*4882a593Smuzhiyun *write_to = value;
194*4882a593Smuzhiyun return 0;
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun __bitread_error:
197*4882a593Smuzhiyun return MPP_NOK;
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun }
200*4882a593Smuzhiyun
sign_extend(RK_S32 val,RK_U8 bits)201*4882a593Smuzhiyun static RK_S32 sign_extend(RK_S32 val, RK_U8 bits)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun RK_U8 shift = 8 * sizeof(RK_S32) - bits;
204*4882a593Smuzhiyun union { RK_U8 u; RK_S32 s; } v = { (RK_U8) val << shift };
205*4882a593Smuzhiyun return v.s >> shift;
206*4882a593Smuzhiyun }
207*4882a593Smuzhiyun
mpp_av1_read_signed(BitReadCtx_t * gbc,RK_S32 width,const char * name,RK_S32 * write_to,RK_S32 range_min,RK_S32 range_max)208*4882a593Smuzhiyun RK_S32 mpp_av1_read_signed(BitReadCtx_t *gbc,
209*4882a593Smuzhiyun RK_S32 width, const char *name,
210*4882a593Smuzhiyun RK_S32 *write_to, RK_S32 range_min,
211*4882a593Smuzhiyun RK_S32 range_max)
212*4882a593Smuzhiyun {
213*4882a593Smuzhiyun RK_S32 value;
214*4882a593Smuzhiyun
215*4882a593Smuzhiyun mpp_assert(width > 0 && width <= 32);
216*4882a593Smuzhiyun
217*4882a593Smuzhiyun if (mpp_get_bits_left(gbc) < width) {
218*4882a593Smuzhiyun mpp_err_f("Invalid value at "
219*4882a593Smuzhiyun "%s: bitstream ended.\n", name);
220*4882a593Smuzhiyun return MPP_NOK;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
223*4882a593Smuzhiyun READ_BITS(gbc, width, &value);
224*4882a593Smuzhiyun value = sign_extend(value, width);
225*4882a593Smuzhiyun if (value < range_min || value > range_max) {
226*4882a593Smuzhiyun mpp_err_f("%s out of range: "
227*4882a593Smuzhiyun "%d, but must be in [%d,%d].\n",
228*4882a593Smuzhiyun name, value, range_min, range_max);
229*4882a593Smuzhiyun return MPP_NOK;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun *write_to = value;
233*4882a593Smuzhiyun return 0;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun __bitread_error:
236*4882a593Smuzhiyun return MPP_NOK;
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun }
239*4882a593Smuzhiyun
mpp_av1_read_subexp(BitReadCtx_t * gbc,RK_U32 range_max,RK_U32 * write_to)240*4882a593Smuzhiyun static RK_S32 mpp_av1_read_subexp(BitReadCtx_t *gbc,
241*4882a593Smuzhiyun RK_U32 range_max, RK_U32 *write_to)
242*4882a593Smuzhiyun {
243*4882a593Smuzhiyun RK_U32 value;
244*4882a593Smuzhiyun RK_S32 err;
245*4882a593Smuzhiyun RK_U32 max_len, len, range_offset, range_bits;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun max_len = mpp_log2(range_max - 1) - 3;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun err = mpp_av1_read_increment(gbc, 0, max_len, "subexp_more_bits", &len);
250*4882a593Smuzhiyun if (err < 0)
251*4882a593Smuzhiyun return err;
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun if (len) {
254*4882a593Smuzhiyun range_bits = 2 + len;
255*4882a593Smuzhiyun range_offset = 1 << range_bits;
256*4882a593Smuzhiyun } else {
257*4882a593Smuzhiyun range_bits = 3;
258*4882a593Smuzhiyun range_offset = 0;
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun if (len < max_len) {
262*4882a593Smuzhiyun err = mpp_av1_read_unsigned(gbc, range_bits,
263*4882a593Smuzhiyun "subexp_bits", &value,
264*4882a593Smuzhiyun 0, MAX_UINT_BITS(range_bits));
265*4882a593Smuzhiyun if (err < 0)
266*4882a593Smuzhiyun return err;
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun } else {
269*4882a593Smuzhiyun err = mpp_av1_read_ns(gbc, "subexp_final_bits", range_max - range_offset,
270*4882a593Smuzhiyun &value);
271*4882a593Smuzhiyun if (err < 0)
272*4882a593Smuzhiyun return err;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun value += range_offset;
275*4882a593Smuzhiyun
276*4882a593Smuzhiyun *write_to = value;
277*4882a593Smuzhiyun return err;
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun
mpp_av1_tile_log2(RK_S32 blksize,RK_S32 target)281*4882a593Smuzhiyun static RK_S32 mpp_av1_tile_log2(RK_S32 blksize, RK_S32 target)
282*4882a593Smuzhiyun {
283*4882a593Smuzhiyun RK_S32 k;
284*4882a593Smuzhiyun for (k = 0; (blksize << k) < target; k++);
285*4882a593Smuzhiyun return k;
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
mpp_av1_get_relative_dist(const AV1RawSequenceHeader * seq,RK_U32 a,RK_U32 b)288*4882a593Smuzhiyun static RK_S32 mpp_av1_get_relative_dist(const AV1RawSequenceHeader *seq,
289*4882a593Smuzhiyun RK_U32 a, RK_U32 b)
290*4882a593Smuzhiyun {
291*4882a593Smuzhiyun RK_U32 diff, m;
292*4882a593Smuzhiyun if (!seq->enable_order_hint)
293*4882a593Smuzhiyun return 0;
294*4882a593Smuzhiyun diff = a - b;
295*4882a593Smuzhiyun m = 1 << seq->order_hint_bits_minus_1;
296*4882a593Smuzhiyun diff = (diff & (m - 1)) - (diff & m);
297*4882a593Smuzhiyun return diff;
298*4882a593Smuzhiyun }
299*4882a593Smuzhiyun
mpp_av1_get_payload_bytes_left(BitReadCtx_t * gbc)300*4882a593Smuzhiyun static size_t mpp_av1_get_payload_bytes_left(BitReadCtx_t *gbc)
301*4882a593Smuzhiyun {
302*4882a593Smuzhiyun size_t size = 0;
303*4882a593Smuzhiyun RK_U8 value = 0;
304*4882a593Smuzhiyun RK_S32 i = 0;
305*4882a593Smuzhiyun
306*4882a593Smuzhiyun for (i = 0; mpp_get_bits_left(gbc) >= 8; i++) {
307*4882a593Smuzhiyun READ_BITS(gbc, 8, &value);
308*4882a593Smuzhiyun if (value)
309*4882a593Smuzhiyun size = i;
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun return size;
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun __bitread_error:
314*4882a593Smuzhiyun return MPP_NOK;
315*4882a593Smuzhiyun
316*4882a593Smuzhiyun }
317*4882a593Smuzhiyun
318*4882a593Smuzhiyun #define CHECK(call) do { \
319*4882a593Smuzhiyun err = (call); \
320*4882a593Smuzhiyun if (err < 0) \
321*4882a593Smuzhiyun return err; \
322*4882a593Smuzhiyun } while (0)
323*4882a593Smuzhiyun
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun #define SUBSCRIPTS(subs, ...) (subs > 0 ? ((RK_S32[subs + 1]){ subs, __VA_ARGS__ }) : NULL)
326*4882a593Smuzhiyun #define fb(width, name) \
327*4882a593Smuzhiyun xf(width, name, current->name, 0, MAX_UINT_BITS(width), 0, )
328*4882a593Smuzhiyun #define fc(width, name, range_min, range_max) \
329*4882a593Smuzhiyun xf(width, name, current->name, range_min, range_max, 0, )
330*4882a593Smuzhiyun #define flag(name) fb(1, name)
331*4882a593Smuzhiyun #define su(width, name) \
332*4882a593Smuzhiyun xsu(width, name, current->name, 0, )
333*4882a593Smuzhiyun
334*4882a593Smuzhiyun #define fbs(width, name, subs, ...) \
335*4882a593Smuzhiyun xf(width, name, current->name, 0, MAX_UINT_BITS(width), subs, __VA_ARGS__)
336*4882a593Smuzhiyun #define fcs(width, name, range_min, range_max, subs, ...) \
337*4882a593Smuzhiyun xf(width, name, current->name, range_min, range_max, subs, __VA_ARGS__)
338*4882a593Smuzhiyun #define flags(name, subs, ...) \
339*4882a593Smuzhiyun xf(1, name, current->name, 0, 1, subs, __VA_ARGS__)
340*4882a593Smuzhiyun #define sus(width, name, subs, ...) \
341*4882a593Smuzhiyun xsu(width, name, current->name, subs, __VA_ARGS__)
342*4882a593Smuzhiyun
343*4882a593Smuzhiyun #define xf(width, name, var, range_min, range_max, subs, ...) do { \
344*4882a593Smuzhiyun RK_U32 value; \
345*4882a593Smuzhiyun CHECK(mpp_av1_read_unsigned(gb, width, #name, \
346*4882a593Smuzhiyun &value, range_min, range_max)); \
347*4882a593Smuzhiyun var = value; \
348*4882a593Smuzhiyun } while (0)
349*4882a593Smuzhiyun
350*4882a593Smuzhiyun #define xsu(width, name, var, subs, ...) do { \
351*4882a593Smuzhiyun RK_S32 value; \
352*4882a593Smuzhiyun CHECK(mpp_av1_read_signed(gb, width, #name, \
353*4882a593Smuzhiyun &value, \
354*4882a593Smuzhiyun MIN_INT_BITS(width), \
355*4882a593Smuzhiyun MAX_INT_BITS(width))); \
356*4882a593Smuzhiyun var = value; \
357*4882a593Smuzhiyun } while (0)
358*4882a593Smuzhiyun
359*4882a593Smuzhiyun #define uvlc(name, range_min, range_max) do { \
360*4882a593Smuzhiyun RK_U32 value; \
361*4882a593Smuzhiyun CHECK(mpp_av1_read_uvlc(gb, #name, \
362*4882a593Smuzhiyun &value, range_min, range_max)); \
363*4882a593Smuzhiyun current->name = value; \
364*4882a593Smuzhiyun } while (0)
365*4882a593Smuzhiyun
366*4882a593Smuzhiyun #define ns(max_value, name) do { \
367*4882a593Smuzhiyun RK_U32 value; \
368*4882a593Smuzhiyun CHECK(mpp_av1_read_ns(gb, #name, max_value, \
369*4882a593Smuzhiyun &value)); \
370*4882a593Smuzhiyun current->name = value; \
371*4882a593Smuzhiyun } while (0)
372*4882a593Smuzhiyun
373*4882a593Smuzhiyun #define increment(name, min, max) do { \
374*4882a593Smuzhiyun RK_U32 value; \
375*4882a593Smuzhiyun CHECK(mpp_av1_read_increment(gb, min, max, #name, &value)); \
376*4882a593Smuzhiyun current->name = value; \
377*4882a593Smuzhiyun } while (0)
378*4882a593Smuzhiyun
379*4882a593Smuzhiyun #define subexp(name, max) do { \
380*4882a593Smuzhiyun RK_U32 value = 0; \
381*4882a593Smuzhiyun CHECK(mpp_av1_read_subexp(gb, max, \
382*4882a593Smuzhiyun &value)); \
383*4882a593Smuzhiyun current->name = value; \
384*4882a593Smuzhiyun } while (0)
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun #define delta_q(name) do { \
387*4882a593Smuzhiyun RK_U8 delta_coded; \
388*4882a593Smuzhiyun RK_S8 delta_q; \
389*4882a593Smuzhiyun xf(1, name.delta_coded, delta_coded, 0, 1, 0, ); \
390*4882a593Smuzhiyun if (delta_coded) \
391*4882a593Smuzhiyun xsu(1 + 6, name.delta_q, delta_q, 0, ); \
392*4882a593Smuzhiyun else \
393*4882a593Smuzhiyun delta_q = 0; \
394*4882a593Smuzhiyun current->name = delta_q; \
395*4882a593Smuzhiyun } while (0)
396*4882a593Smuzhiyun
397*4882a593Smuzhiyun #define leb128(name) do { \
398*4882a593Smuzhiyun RK_U64 value; \
399*4882a593Smuzhiyun CHECK(mpp_av1_read_leb128(gb, &value)); \
400*4882a593Smuzhiyun current->name = value; \
401*4882a593Smuzhiyun } while (0)
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun #define infer(name, value) do { \
404*4882a593Smuzhiyun current->name = value; \
405*4882a593Smuzhiyun } while (0)
406*4882a593Smuzhiyun
407*4882a593Smuzhiyun #define byte_alignment(gb) (mpp_get_bits_count(gb) % 8)
408*4882a593Smuzhiyun
mpp_av1_read_obu_header(AV1Context * ctx,BitReadCtx_t * gb,AV1RawOBUHeader * current)409*4882a593Smuzhiyun static RK_S32 mpp_av1_read_obu_header(AV1Context *ctx, BitReadCtx_t *gb,
410*4882a593Smuzhiyun AV1RawOBUHeader *current)
411*4882a593Smuzhiyun {
412*4882a593Smuzhiyun RK_S32 err;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun fc(1, obu_forbidden_bit, 0, 0);
415*4882a593Smuzhiyun
416*4882a593Smuzhiyun fc(4, obu_type, 0, AV1_OBU_PADDING);
417*4882a593Smuzhiyun flag(obu_extension_flag);
418*4882a593Smuzhiyun flag(obu_has_size_field);
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun fc(1, obu_reserved_1bit, 0, 0);
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun if (current->obu_extension_flag) {
423*4882a593Smuzhiyun fb(3, temporal_id);
424*4882a593Smuzhiyun fb(2, spatial_id);
425*4882a593Smuzhiyun fc(3, extension_header_reserved_3bits, 0, 0);
426*4882a593Smuzhiyun } else {
427*4882a593Smuzhiyun infer(temporal_id, 0);
428*4882a593Smuzhiyun infer(spatial_id, 0);
429*4882a593Smuzhiyun }
430*4882a593Smuzhiyun
431*4882a593Smuzhiyun ctx->temporal_id = current->temporal_id;
432*4882a593Smuzhiyun ctx->spatial_id = current->spatial_id;
433*4882a593Smuzhiyun
434*4882a593Smuzhiyun return 0;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
mpp_av1_trailing_bits(AV1Context * ctx,BitReadCtx_t * gb,RK_S32 nb_bits)437*4882a593Smuzhiyun static RK_S32 mpp_av1_trailing_bits(AV1Context *ctx, BitReadCtx_t *gb, RK_S32 nb_bits)
438*4882a593Smuzhiyun {
439*4882a593Smuzhiyun (void)ctx;
440*4882a593Smuzhiyun mpp_assert(nb_bits > 0);
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun // fixed(1, trailing_one_bit, 1);
443*4882a593Smuzhiyun mpp_skip_bits(gb, 1);
444*4882a593Smuzhiyun
445*4882a593Smuzhiyun --nb_bits;
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun while (nb_bits > 0) {
448*4882a593Smuzhiyun // fixed(1, trailing_zero_bit, 0);
449*4882a593Smuzhiyun mpp_skip_bits(gb, 1);
450*4882a593Smuzhiyun --nb_bits;
451*4882a593Smuzhiyun }
452*4882a593Smuzhiyun
453*4882a593Smuzhiyun return 0;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun
mpp_av1_byte_alignment(AV1Context * ctx,BitReadCtx_t * gb)456*4882a593Smuzhiyun static RK_S32 mpp_av1_byte_alignment(AV1Context *ctx, BitReadCtx_t *gb)
457*4882a593Smuzhiyun {
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun (void)ctx;
460*4882a593Smuzhiyun
461*4882a593Smuzhiyun while (byte_alignment(gb) != 0)
462*4882a593Smuzhiyun mpp_skip_bits(gb, 1);
463*4882a593Smuzhiyun //fixed(1, zero_bit, 0);
464*4882a593Smuzhiyun
465*4882a593Smuzhiyun return 0;
466*4882a593Smuzhiyun }
467*4882a593Smuzhiyun
mpp_av1_color_config(AV1Context * ctx,BitReadCtx_t * gb,AV1RawColorConfig * current,RK_S32 seq_profile)468*4882a593Smuzhiyun static RK_S32 mpp_av1_color_config(AV1Context *ctx, BitReadCtx_t *gb,
469*4882a593Smuzhiyun AV1RawColorConfig *current, RK_S32 seq_profile)
470*4882a593Smuzhiyun {
471*4882a593Smuzhiyun RK_S32 err;
472*4882a593Smuzhiyun
473*4882a593Smuzhiyun flag(high_bitdepth);
474*4882a593Smuzhiyun
475*4882a593Smuzhiyun if (seq_profile == PROFILE_AV1_PROFESSIONAL &&
476*4882a593Smuzhiyun current->high_bitdepth) {
477*4882a593Smuzhiyun flag(twelve_bit);
478*4882a593Smuzhiyun ctx->bit_depth = current->twelve_bit ? 12 : 10;
479*4882a593Smuzhiyun } else {
480*4882a593Smuzhiyun ctx->bit_depth = current->high_bitdepth ? 10 : 8;
481*4882a593Smuzhiyun }
482*4882a593Smuzhiyun
483*4882a593Smuzhiyun if (seq_profile == PROFILE_AV1_HIGH)
484*4882a593Smuzhiyun infer(mono_chrome, 0);
485*4882a593Smuzhiyun else
486*4882a593Smuzhiyun flag(mono_chrome);
487*4882a593Smuzhiyun ctx->num_planes = current->mono_chrome ? 1 : 3;
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun flag(color_description_present_flag);
490*4882a593Smuzhiyun if (current->color_description_present_flag) {
491*4882a593Smuzhiyun fb(8, color_primaries);
492*4882a593Smuzhiyun fb(8, transfer_characteristics);
493*4882a593Smuzhiyun fb(8, matrix_coefficients);
494*4882a593Smuzhiyun } else {
495*4882a593Smuzhiyun infer(color_primaries, MPP_FRAME_PRI_UNSPECIFIED);
496*4882a593Smuzhiyun infer(transfer_characteristics, MPP_FRAME_TRC_UNSPECIFIED);
497*4882a593Smuzhiyun infer(matrix_coefficients, MPP_FRAME_SPC_UNSPECIFIED);
498*4882a593Smuzhiyun }
499*4882a593Smuzhiyun
500*4882a593Smuzhiyun if (current->mono_chrome) {
501*4882a593Smuzhiyun flag(color_range);
502*4882a593Smuzhiyun
503*4882a593Smuzhiyun infer(subsampling_x, 1);
504*4882a593Smuzhiyun infer(subsampling_y, 1);
505*4882a593Smuzhiyun infer(chroma_sample_position, AV1_CSP_UNKNOWN);
506*4882a593Smuzhiyun infer(separate_uv_delta_q, 0);
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun } else if (current->color_primaries == MPP_FRAME_PRI_BT709 &&
509*4882a593Smuzhiyun current->transfer_characteristics == MPP_FRAME_TRC_IEC61966_2_1 &&
510*4882a593Smuzhiyun current->matrix_coefficients == MPP_FRAME_SPC_RGB) {
511*4882a593Smuzhiyun infer(color_range, 1);
512*4882a593Smuzhiyun infer(subsampling_x, 0);
513*4882a593Smuzhiyun infer(subsampling_y, 0);
514*4882a593Smuzhiyun flag(separate_uv_delta_q);
515*4882a593Smuzhiyun
516*4882a593Smuzhiyun } else {
517*4882a593Smuzhiyun flag(color_range);
518*4882a593Smuzhiyun
519*4882a593Smuzhiyun if (seq_profile == PROFILE_AV1_MAIN) {
520*4882a593Smuzhiyun infer(subsampling_x, 1);
521*4882a593Smuzhiyun infer(subsampling_y, 1);
522*4882a593Smuzhiyun } else if (seq_profile == PROFILE_AV1_HIGH) {
523*4882a593Smuzhiyun infer(subsampling_x, 0);
524*4882a593Smuzhiyun infer(subsampling_y, 0);
525*4882a593Smuzhiyun } else {
526*4882a593Smuzhiyun if (ctx->bit_depth == 12) {
527*4882a593Smuzhiyun fb(1, subsampling_x);
528*4882a593Smuzhiyun if (current->subsampling_x)
529*4882a593Smuzhiyun fb(1, subsampling_y);
530*4882a593Smuzhiyun else
531*4882a593Smuzhiyun infer(subsampling_y, 0);
532*4882a593Smuzhiyun } else {
533*4882a593Smuzhiyun infer(subsampling_x, 1);
534*4882a593Smuzhiyun infer(subsampling_y, 0);
535*4882a593Smuzhiyun }
536*4882a593Smuzhiyun }
537*4882a593Smuzhiyun if (current->subsampling_x && current->subsampling_y) {
538*4882a593Smuzhiyun fc(2, chroma_sample_position, AV1_CSP_UNKNOWN,
539*4882a593Smuzhiyun AV1_CSP_COLOCATED);
540*4882a593Smuzhiyun }
541*4882a593Smuzhiyun
542*4882a593Smuzhiyun flag(separate_uv_delta_q);
543*4882a593Smuzhiyun }
544*4882a593Smuzhiyun
545*4882a593Smuzhiyun return 0;
546*4882a593Smuzhiyun }
547*4882a593Smuzhiyun
mpp_av1_timing_info(AV1Context * ctx,BitReadCtx_t * gb,AV1RawTimingInfo * current)548*4882a593Smuzhiyun static RK_S32 mpp_av1_timing_info(AV1Context *ctx, BitReadCtx_t *gb,
549*4882a593Smuzhiyun AV1RawTimingInfo *current)
550*4882a593Smuzhiyun {
551*4882a593Smuzhiyun (void)ctx;
552*4882a593Smuzhiyun RK_S32 err;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun fc(32, num_units_in_display_tick, 1, MAX_UINT_BITS(32));
555*4882a593Smuzhiyun fc(32, time_scale, 1, MAX_UINT_BITS(32));
556*4882a593Smuzhiyun
557*4882a593Smuzhiyun flag(equal_picture_interval);
558*4882a593Smuzhiyun if (current->equal_picture_interval)
559*4882a593Smuzhiyun uvlc(num_ticks_per_picture_minus_1, 0, MAX_UINT_BITS(32) - 1);
560*4882a593Smuzhiyun
561*4882a593Smuzhiyun return 0;
562*4882a593Smuzhiyun }
563*4882a593Smuzhiyun
mpp_av1_decoder_model_info(AV1Context * ctx,BitReadCtx_t * gb,AV1RawDecoderModelInfo * current)564*4882a593Smuzhiyun static RK_S32 mpp_av1_decoder_model_info(AV1Context *ctx, BitReadCtx_t *gb,
565*4882a593Smuzhiyun AV1RawDecoderModelInfo *current)
566*4882a593Smuzhiyun {
567*4882a593Smuzhiyun RK_S32 err;
568*4882a593Smuzhiyun (void)ctx;
569*4882a593Smuzhiyun fb(5, buffer_delay_length_minus_1);
570*4882a593Smuzhiyun fb(32, num_units_in_decoding_tick);
571*4882a593Smuzhiyun fb(5, buffer_removal_time_length_minus_1);
572*4882a593Smuzhiyun fb(5, frame_presentation_time_length_minus_1);
573*4882a593Smuzhiyun
574*4882a593Smuzhiyun return 0;
575*4882a593Smuzhiyun }
576*4882a593Smuzhiyun
mpp_av1_sequence_header_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawSequenceHeader * current)577*4882a593Smuzhiyun static RK_S32 mpp_av1_sequence_header_obu(AV1Context *ctx, BitReadCtx_t *gb,
578*4882a593Smuzhiyun AV1RawSequenceHeader *current)
579*4882a593Smuzhiyun {
580*4882a593Smuzhiyun RK_S32 i, err;
581*4882a593Smuzhiyun
582*4882a593Smuzhiyun fc(3, seq_profile, PROFILE_AV1_MAIN,
583*4882a593Smuzhiyun PROFILE_AV1_PROFESSIONAL);
584*4882a593Smuzhiyun flag(still_picture);
585*4882a593Smuzhiyun flag(reduced_still_picture_header);
586*4882a593Smuzhiyun
587*4882a593Smuzhiyun if (current->reduced_still_picture_header) {
588*4882a593Smuzhiyun infer(timing_info_present_flag, 0);
589*4882a593Smuzhiyun infer(decoder_model_info_present_flag, 0);
590*4882a593Smuzhiyun infer(initial_display_delay_present_flag, 0);
591*4882a593Smuzhiyun infer(operating_points_cnt_minus_1, 0);
592*4882a593Smuzhiyun infer(operating_point_idc[0], 0);
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun fb(5, seq_level_idx[0]);
595*4882a593Smuzhiyun
596*4882a593Smuzhiyun infer(seq_tier[0], 0);
597*4882a593Smuzhiyun infer(decoder_model_present_for_this_op[0], 0);
598*4882a593Smuzhiyun infer(initial_display_delay_present_for_this_op[0], 0);
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun } else {
601*4882a593Smuzhiyun flag(timing_info_present_flag);
602*4882a593Smuzhiyun if (current->timing_info_present_flag) {
603*4882a593Smuzhiyun CHECK(mpp_av1_timing_info(ctx, gb, ¤t->timing_info));
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun flag(decoder_model_info_present_flag);
606*4882a593Smuzhiyun if (current->decoder_model_info_present_flag) {
607*4882a593Smuzhiyun CHECK(mpp_av1_decoder_model_info
608*4882a593Smuzhiyun (ctx, gb, ¤t->decoder_model_info));
609*4882a593Smuzhiyun }
610*4882a593Smuzhiyun } else {
611*4882a593Smuzhiyun infer(decoder_model_info_present_flag, 0);
612*4882a593Smuzhiyun }
613*4882a593Smuzhiyun
614*4882a593Smuzhiyun flag(initial_display_delay_present_flag);
615*4882a593Smuzhiyun
616*4882a593Smuzhiyun fb(5, operating_points_cnt_minus_1);
617*4882a593Smuzhiyun for (i = 0; i <= current->operating_points_cnt_minus_1; i++) {
618*4882a593Smuzhiyun fbs(12, operating_point_idc[i], 1, i);
619*4882a593Smuzhiyun fbs(5, seq_level_idx[i], 1, i);
620*4882a593Smuzhiyun
621*4882a593Smuzhiyun if (current->seq_level_idx[i] > 7)
622*4882a593Smuzhiyun flags(seq_tier[i], 1, i);
623*4882a593Smuzhiyun else
624*4882a593Smuzhiyun infer(seq_tier[i], 0);
625*4882a593Smuzhiyun
626*4882a593Smuzhiyun if (current->decoder_model_info_present_flag) {
627*4882a593Smuzhiyun flags(decoder_model_present_for_this_op[i], 1, i);
628*4882a593Smuzhiyun if (current->decoder_model_present_for_this_op[i]) {
629*4882a593Smuzhiyun RK_S32 n = current->decoder_model_info.buffer_delay_length_minus_1 + 1;
630*4882a593Smuzhiyun fbs(n, decoder_buffer_delay[i], 1, i);
631*4882a593Smuzhiyun fbs(n, encoder_buffer_delay[i], 1, i);
632*4882a593Smuzhiyun flags(low_delay_mode_flag[i], 1, i);
633*4882a593Smuzhiyun }
634*4882a593Smuzhiyun } else {
635*4882a593Smuzhiyun infer(decoder_model_present_for_this_op[i], 0);
636*4882a593Smuzhiyun }
637*4882a593Smuzhiyun
638*4882a593Smuzhiyun if (current->initial_display_delay_present_flag) {
639*4882a593Smuzhiyun flags(initial_display_delay_present_for_this_op[i], 1, i);
640*4882a593Smuzhiyun if (current->initial_display_delay_present_for_this_op[i])
641*4882a593Smuzhiyun fbs(4, initial_display_delay_minus_1[i], 1, i);
642*4882a593Smuzhiyun }
643*4882a593Smuzhiyun }
644*4882a593Smuzhiyun }
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun fb(4, frame_width_bits_minus_1);
647*4882a593Smuzhiyun fb(4, frame_height_bits_minus_1);
648*4882a593Smuzhiyun
649*4882a593Smuzhiyun fb(current->frame_width_bits_minus_1 + 1, max_frame_width_minus_1);
650*4882a593Smuzhiyun fb(current->frame_height_bits_minus_1 + 1, max_frame_height_minus_1);
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun if (current->reduced_still_picture_header)
653*4882a593Smuzhiyun infer(frame_id_numbers_present_flag, 0);
654*4882a593Smuzhiyun else
655*4882a593Smuzhiyun flag(frame_id_numbers_present_flag);
656*4882a593Smuzhiyun if (current->frame_id_numbers_present_flag) {
657*4882a593Smuzhiyun fb(4, delta_frame_id_length_minus_2);
658*4882a593Smuzhiyun fb(3, additional_frame_id_length_minus_1);
659*4882a593Smuzhiyun }
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun flag(use_128x128_superblock);
662*4882a593Smuzhiyun flag(enable_filter_intra);
663*4882a593Smuzhiyun flag(enable_intra_edge_filter);
664*4882a593Smuzhiyun
665*4882a593Smuzhiyun if (current->reduced_still_picture_header) {
666*4882a593Smuzhiyun infer(enable_interintra_compound, 0);
667*4882a593Smuzhiyun infer(enable_masked_compound, 0);
668*4882a593Smuzhiyun infer(enable_warped_motion, 0);
669*4882a593Smuzhiyun infer(enable_dual_filter, 0);
670*4882a593Smuzhiyun infer(enable_order_hint, 0);
671*4882a593Smuzhiyun infer(enable_jnt_comp, 0);
672*4882a593Smuzhiyun infer(enable_ref_frame_mvs, 0);
673*4882a593Smuzhiyun
674*4882a593Smuzhiyun infer(seq_force_screen_content_tools,
675*4882a593Smuzhiyun AV1_SELECT_SCREEN_CONTENT_TOOLS);
676*4882a593Smuzhiyun infer(seq_force_integer_mv,
677*4882a593Smuzhiyun AV1_SELECT_INTEGER_MV);
678*4882a593Smuzhiyun } else {
679*4882a593Smuzhiyun flag(enable_interintra_compound);
680*4882a593Smuzhiyun flag(enable_masked_compound);
681*4882a593Smuzhiyun flag(enable_warped_motion);
682*4882a593Smuzhiyun flag(enable_dual_filter);
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun flag(enable_order_hint);
685*4882a593Smuzhiyun if (current->enable_order_hint) {
686*4882a593Smuzhiyun flag(enable_jnt_comp);
687*4882a593Smuzhiyun flag(enable_ref_frame_mvs);
688*4882a593Smuzhiyun } else {
689*4882a593Smuzhiyun infer(enable_jnt_comp, 0);
690*4882a593Smuzhiyun infer(enable_ref_frame_mvs, 0);
691*4882a593Smuzhiyun }
692*4882a593Smuzhiyun
693*4882a593Smuzhiyun flag(seq_choose_screen_content_tools);
694*4882a593Smuzhiyun if (current->seq_choose_screen_content_tools)
695*4882a593Smuzhiyun infer(seq_force_screen_content_tools,
696*4882a593Smuzhiyun AV1_SELECT_SCREEN_CONTENT_TOOLS);
697*4882a593Smuzhiyun else
698*4882a593Smuzhiyun fb(1, seq_force_screen_content_tools);
699*4882a593Smuzhiyun if (current->seq_force_screen_content_tools > 0) {
700*4882a593Smuzhiyun flag(seq_choose_integer_mv);
701*4882a593Smuzhiyun if (current->seq_choose_integer_mv)
702*4882a593Smuzhiyun infer(seq_force_integer_mv,
703*4882a593Smuzhiyun AV1_SELECT_INTEGER_MV);
704*4882a593Smuzhiyun else
705*4882a593Smuzhiyun fb(1, seq_force_integer_mv);
706*4882a593Smuzhiyun } else {
707*4882a593Smuzhiyun infer(seq_force_integer_mv, AV1_SELECT_INTEGER_MV);
708*4882a593Smuzhiyun }
709*4882a593Smuzhiyun
710*4882a593Smuzhiyun if (current->enable_order_hint)
711*4882a593Smuzhiyun fb(3, order_hint_bits_minus_1);
712*4882a593Smuzhiyun }
713*4882a593Smuzhiyun
714*4882a593Smuzhiyun flag(enable_superres);
715*4882a593Smuzhiyun flag(enable_cdef);
716*4882a593Smuzhiyun flag(enable_restoration);
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun CHECK(mpp_av1_color_config(ctx, gb, ¤t->color_config,
719*4882a593Smuzhiyun current->seq_profile));
720*4882a593Smuzhiyun
721*4882a593Smuzhiyun flag(film_grain_params_present);
722*4882a593Smuzhiyun
723*4882a593Smuzhiyun return 0;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun
mpp_av1_temporal_delimiter_obu(AV1Context * ctx,BitReadCtx_t * gb)726*4882a593Smuzhiyun static RK_S32 mpp_av1_temporal_delimiter_obu(AV1Context *ctx, BitReadCtx_t *gb)
727*4882a593Smuzhiyun {
728*4882a593Smuzhiyun (void)gb;
729*4882a593Smuzhiyun ctx->seen_frame_header = 0;
730*4882a593Smuzhiyun
731*4882a593Smuzhiyun return 0;
732*4882a593Smuzhiyun }
733*4882a593Smuzhiyun
734*4882a593Smuzhiyun /* spec 7.8 */
mpp_av1_set_frame_refs(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)735*4882a593Smuzhiyun static RK_S32 mpp_av1_set_frame_refs(AV1Context *ctx, BitReadCtx_t *gb,
736*4882a593Smuzhiyun AV1RawFrameHeader *current)
737*4882a593Smuzhiyun {
738*4882a593Smuzhiyun (void)gb;
739*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
740*4882a593Smuzhiyun static const RK_U8 ref_frame_list[AV1_NUM_REF_FRAMES - 2] = {
741*4882a593Smuzhiyun AV1_REF_FRAME_LAST2, AV1_REF_FRAME_LAST3, AV1_REF_FRAME_BWDREF,
742*4882a593Smuzhiyun AV1_REF_FRAME_ALTREF2, AV1_REF_FRAME_ALTREF
743*4882a593Smuzhiyun };
744*4882a593Smuzhiyun RK_S8 ref_frame_idx[AV1_REFS_PER_FRAME], used_frame[AV1_NUM_REF_FRAMES];
745*4882a593Smuzhiyun RK_S8 shifted_order_hints[AV1_NUM_REF_FRAMES];
746*4882a593Smuzhiyun RK_S32 cur_frame_hint, latest_order_hint, earliest_order_hint, ref;
747*4882a593Smuzhiyun RK_S32 i, j;
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME; i++)
750*4882a593Smuzhiyun ref_frame_idx[i] = -1;
751*4882a593Smuzhiyun ref_frame_idx[AV1_REF_FRAME_LAST - AV1_REF_FRAME_LAST] = current->last_frame_idx;
752*4882a593Smuzhiyun ref_frame_idx[AV1_REF_FRAME_GOLDEN - AV1_REF_FRAME_LAST] = current->golden_frame_idx;
753*4882a593Smuzhiyun
754*4882a593Smuzhiyun /*
755*4882a593Smuzhiyun * An array usedFrame marking which reference frames
756*4882a593Smuzhiyun * have been used is prepared as follows:
757*4882a593Smuzhiyun */
758*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
759*4882a593Smuzhiyun used_frame[i] = 0;
760*4882a593Smuzhiyun used_frame[current->last_frame_idx] = 1;
761*4882a593Smuzhiyun used_frame[current->golden_frame_idx] = 1;
762*4882a593Smuzhiyun
763*4882a593Smuzhiyun /*
764*4882a593Smuzhiyun * An array shiftedOrderHints (containing the expected output order shifted
765*4882a593Smuzhiyun * such that the current frame has hint equal to curFrameHint) is prepared as follows:
766*4882a593Smuzhiyun */
767*4882a593Smuzhiyun cur_frame_hint = 1 << (seq->order_hint_bits_minus_1);
768*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++)
769*4882a593Smuzhiyun shifted_order_hints[i] = cur_frame_hint +
770*4882a593Smuzhiyun mpp_av1_get_relative_dist(seq, ctx->ref_s[i].order_hint,
771*4882a593Smuzhiyun ctx->order_hint);
772*4882a593Smuzhiyun
773*4882a593Smuzhiyun latest_order_hint = shifted_order_hints[current->last_frame_idx];
774*4882a593Smuzhiyun earliest_order_hint = shifted_order_hints[current->golden_frame_idx];
775*4882a593Smuzhiyun
776*4882a593Smuzhiyun /* find_latest_backward */
777*4882a593Smuzhiyun ref = -1;
778*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
779*4882a593Smuzhiyun RK_S32 hint = shifted_order_hints[i];
780*4882a593Smuzhiyun if (!used_frame[i] && hint >= cur_frame_hint &&
781*4882a593Smuzhiyun (ref < 0 || hint >= latest_order_hint)) {
782*4882a593Smuzhiyun ref = i;
783*4882a593Smuzhiyun latest_order_hint = hint;
784*4882a593Smuzhiyun }
785*4882a593Smuzhiyun }
786*4882a593Smuzhiyun /*
787*4882a593Smuzhiyun * The ALTREF_FRAME reference is set to be a backward reference to the frame
788*4882a593Smuzhiyun * with highest output order as follows:
789*4882a593Smuzhiyun */
790*4882a593Smuzhiyun if (ref >= 0) {
791*4882a593Smuzhiyun ref_frame_idx[AV1_REF_FRAME_ALTREF - AV1_REF_FRAME_LAST] = ref;
792*4882a593Smuzhiyun used_frame[ref] = 1;
793*4882a593Smuzhiyun }
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun /* find_earliest_backward */
796*4882a593Smuzhiyun ref = -1;
797*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
798*4882a593Smuzhiyun RK_S32 hint = shifted_order_hints[i];
799*4882a593Smuzhiyun if (!used_frame[i] && hint >= cur_frame_hint &&
800*4882a593Smuzhiyun (ref < 0 || hint < earliest_order_hint)) {
801*4882a593Smuzhiyun ref = i;
802*4882a593Smuzhiyun earliest_order_hint = hint;
803*4882a593Smuzhiyun }
804*4882a593Smuzhiyun }
805*4882a593Smuzhiyun /*
806*4882a593Smuzhiyun * The BWDREF_FRAME reference is set to be a backward reference to
807*4882a593Smuzhiyun * the closest frame as follows:
808*4882a593Smuzhiyun */
809*4882a593Smuzhiyun if (ref >= 0) {
810*4882a593Smuzhiyun ref_frame_idx[AV1_REF_FRAME_BWDREF - AV1_REF_FRAME_LAST] = ref;
811*4882a593Smuzhiyun used_frame[ref] = 1;
812*4882a593Smuzhiyun }
813*4882a593Smuzhiyun
814*4882a593Smuzhiyun ref = -1;
815*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
816*4882a593Smuzhiyun RK_S32 hint = shifted_order_hints[i];
817*4882a593Smuzhiyun if (!used_frame[i] && hint >= cur_frame_hint &&
818*4882a593Smuzhiyun (ref < 0 || hint < earliest_order_hint)) {
819*4882a593Smuzhiyun ref = i;
820*4882a593Smuzhiyun earliest_order_hint = hint;
821*4882a593Smuzhiyun }
822*4882a593Smuzhiyun }
823*4882a593Smuzhiyun
824*4882a593Smuzhiyun /*
825*4882a593Smuzhiyun * The ALTREF2_FRAME reference is set to the next closest
826*4882a593Smuzhiyun * backward reference as follows:
827*4882a593Smuzhiyun */
828*4882a593Smuzhiyun if (ref >= 0) {
829*4882a593Smuzhiyun ref_frame_idx[AV1_REF_FRAME_ALTREF2 - AV1_REF_FRAME_LAST] = ref;
830*4882a593Smuzhiyun used_frame[ref] = 1;
831*4882a593Smuzhiyun }
832*4882a593Smuzhiyun
833*4882a593Smuzhiyun /*
834*4882a593Smuzhiyun * The remaining references are set to be forward references
835*4882a593Smuzhiyun * in anti-chronological order as follows:
836*4882a593Smuzhiyun */
837*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME - 2; i++) {
838*4882a593Smuzhiyun RK_S32 ref_frame = ref_frame_list[i];
839*4882a593Smuzhiyun if (ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] < 0 ) {
840*4882a593Smuzhiyun /* find_latest_forward */
841*4882a593Smuzhiyun ref = -1;
842*4882a593Smuzhiyun for (j = 0; j < AV1_NUM_REF_FRAMES; j++) {
843*4882a593Smuzhiyun RK_S32 hint = shifted_order_hints[j];
844*4882a593Smuzhiyun if (!used_frame[j] && hint < cur_frame_hint &&
845*4882a593Smuzhiyun (ref < 0 || hint >= latest_order_hint)) {
846*4882a593Smuzhiyun ref = j;
847*4882a593Smuzhiyun latest_order_hint = hint;
848*4882a593Smuzhiyun }
849*4882a593Smuzhiyun }
850*4882a593Smuzhiyun if (ref >= 0) {
851*4882a593Smuzhiyun ref_frame_idx[ref_frame - AV1_REF_FRAME_LAST] = ref;
852*4882a593Smuzhiyun used_frame[ref] = 1;
853*4882a593Smuzhiyun }
854*4882a593Smuzhiyun }
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun /*
858*4882a593Smuzhiyun * Finally, any remaining references are set to the reference
859*4882a593Smuzhiyun * frame with smallest output order as follows:
860*4882a593Smuzhiyun */
861*4882a593Smuzhiyun ref = -1;
862*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
863*4882a593Smuzhiyun RK_S32 hint = shifted_order_hints[i];
864*4882a593Smuzhiyun if (ref < 0 || hint < earliest_order_hint) {
865*4882a593Smuzhiyun ref = i;
866*4882a593Smuzhiyun earliest_order_hint = hint;
867*4882a593Smuzhiyun }
868*4882a593Smuzhiyun }
869*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
870*4882a593Smuzhiyun if (ref_frame_idx[i] < 0)
871*4882a593Smuzhiyun ref_frame_idx[i] = ref;
872*4882a593Smuzhiyun infer(ref_frame_idx[i], ref_frame_idx[i]);
873*4882a593Smuzhiyun }
874*4882a593Smuzhiyun
875*4882a593Smuzhiyun return 0;
876*4882a593Smuzhiyun }
877*4882a593Smuzhiyun
mpp_av1_superres_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)878*4882a593Smuzhiyun static RK_S32 mpp_av1_superres_params(AV1Context *ctx, BitReadCtx_t *gb,
879*4882a593Smuzhiyun AV1RawFrameHeader *current)
880*4882a593Smuzhiyun {
881*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
882*4882a593Smuzhiyun RK_S32 denom, err;
883*4882a593Smuzhiyun
884*4882a593Smuzhiyun if (seq->enable_superres)
885*4882a593Smuzhiyun flag(use_superres);
886*4882a593Smuzhiyun else
887*4882a593Smuzhiyun infer(use_superres, 0);
888*4882a593Smuzhiyun
889*4882a593Smuzhiyun if (current->use_superres) {
890*4882a593Smuzhiyun fb(3, coded_denom);
891*4882a593Smuzhiyun denom = current->coded_denom + AV1_SUPERRES_DENOM_MIN;
892*4882a593Smuzhiyun } else {
893*4882a593Smuzhiyun denom = AV1_SUPERRES_NUM;
894*4882a593Smuzhiyun }
895*4882a593Smuzhiyun
896*4882a593Smuzhiyun ctx->upscaled_width = ctx->frame_width;
897*4882a593Smuzhiyun ctx->frame_width = (ctx->upscaled_width * AV1_SUPERRES_NUM +
898*4882a593Smuzhiyun denom / 2) / denom;
899*4882a593Smuzhiyun return 0;
900*4882a593Smuzhiyun }
901*4882a593Smuzhiyun
mpp_av1_frame_size(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)902*4882a593Smuzhiyun static RK_S32 mpp_av1_frame_size(AV1Context *ctx, BitReadCtx_t *gb,
903*4882a593Smuzhiyun AV1RawFrameHeader *current)
904*4882a593Smuzhiyun {
905*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
906*4882a593Smuzhiyun RK_S32 err;
907*4882a593Smuzhiyun
908*4882a593Smuzhiyun if (current->frame_size_override_flag) {
909*4882a593Smuzhiyun fb(seq->frame_width_bits_minus_1 + 1, frame_width_minus_1);
910*4882a593Smuzhiyun fb(seq->frame_height_bits_minus_1 + 1, frame_height_minus_1);
911*4882a593Smuzhiyun } else {
912*4882a593Smuzhiyun infer(frame_width_minus_1, seq->max_frame_width_minus_1);
913*4882a593Smuzhiyun infer(frame_height_minus_1, seq->max_frame_height_minus_1);
914*4882a593Smuzhiyun }
915*4882a593Smuzhiyun
916*4882a593Smuzhiyun ctx->frame_width = current->frame_width_minus_1 + 1;
917*4882a593Smuzhiyun ctx->frame_height = current->frame_height_minus_1 + 1;
918*4882a593Smuzhiyun
919*4882a593Smuzhiyun CHECK(mpp_av1_superres_params(ctx, gb, current));
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun return 0;
922*4882a593Smuzhiyun }
923*4882a593Smuzhiyun
mpp_av1_render_size(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)924*4882a593Smuzhiyun static RK_S32 mpp_av1_render_size(AV1Context *ctx, BitReadCtx_t *gb,
925*4882a593Smuzhiyun AV1RawFrameHeader *current)
926*4882a593Smuzhiyun {
927*4882a593Smuzhiyun RK_S32 err;
928*4882a593Smuzhiyun
929*4882a593Smuzhiyun flag(render_and_frame_size_different);
930*4882a593Smuzhiyun
931*4882a593Smuzhiyun if (current->render_and_frame_size_different) {
932*4882a593Smuzhiyun fb(16, render_width_minus_1);
933*4882a593Smuzhiyun fb(16, render_height_minus_1);
934*4882a593Smuzhiyun } else {
935*4882a593Smuzhiyun infer(render_width_minus_1, current->frame_width_minus_1);
936*4882a593Smuzhiyun infer(render_height_minus_1, current->frame_height_minus_1);
937*4882a593Smuzhiyun }
938*4882a593Smuzhiyun
939*4882a593Smuzhiyun ctx->render_width = current->render_width_minus_1 + 1;
940*4882a593Smuzhiyun ctx->render_height = current->render_height_minus_1 + 1;
941*4882a593Smuzhiyun
942*4882a593Smuzhiyun return 0;
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun
mpp_av1_frame_size_with_refs(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)945*4882a593Smuzhiyun static RK_S32 mpp_av1_frame_size_with_refs(AV1Context *ctx, BitReadCtx_t *gb,
946*4882a593Smuzhiyun AV1RawFrameHeader *current)
947*4882a593Smuzhiyun {
948*4882a593Smuzhiyun RK_S32 i, err;
949*4882a593Smuzhiyun
950*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
951*4882a593Smuzhiyun flags(found_ref[i], 1, i);
952*4882a593Smuzhiyun if (current->found_ref[i]) {
953*4882a593Smuzhiyun AV1ReferenceFrameState *ref =
954*4882a593Smuzhiyun &ctx->ref_s[current->ref_frame_idx[i]];
955*4882a593Smuzhiyun
956*4882a593Smuzhiyun if (!ref->valid) {
957*4882a593Smuzhiyun mpp_err_f("Missing reference frame needed for frame size "
958*4882a593Smuzhiyun "(ref = %d, ref_frame_idx = %d).\n",
959*4882a593Smuzhiyun i, current->ref_frame_idx[i]);
960*4882a593Smuzhiyun return MPP_ERR_PROTOL;
961*4882a593Smuzhiyun }
962*4882a593Smuzhiyun
963*4882a593Smuzhiyun infer(frame_width_minus_1, ref->upscaled_width - 1);
964*4882a593Smuzhiyun infer(frame_height_minus_1, ref->frame_height - 1);
965*4882a593Smuzhiyun infer(render_width_minus_1, ref->render_width - 1);
966*4882a593Smuzhiyun infer(render_height_minus_1, ref->render_height - 1);
967*4882a593Smuzhiyun
968*4882a593Smuzhiyun ctx->upscaled_width = ref->upscaled_width;
969*4882a593Smuzhiyun ctx->frame_width = ctx->upscaled_width;
970*4882a593Smuzhiyun ctx->frame_height = ref->frame_height;
971*4882a593Smuzhiyun ctx->render_width = ref->render_width;
972*4882a593Smuzhiyun ctx->render_height = ref->render_height;
973*4882a593Smuzhiyun break;
974*4882a593Smuzhiyun }
975*4882a593Smuzhiyun }
976*4882a593Smuzhiyun
977*4882a593Smuzhiyun if (i >= AV1_REFS_PER_FRAME) {
978*4882a593Smuzhiyun CHECK(mpp_av1_frame_size(ctx, gb, current));
979*4882a593Smuzhiyun CHECK(mpp_av1_render_size(ctx, gb, current));
980*4882a593Smuzhiyun } else {
981*4882a593Smuzhiyun CHECK(mpp_av1_superres_params(ctx, gb, current));
982*4882a593Smuzhiyun }
983*4882a593Smuzhiyun
984*4882a593Smuzhiyun return 0;
985*4882a593Smuzhiyun }
986*4882a593Smuzhiyun
mpp_av1_interpolation_filter(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)987*4882a593Smuzhiyun static RK_S32 mpp_av1_interpolation_filter(AV1Context *ctx, BitReadCtx_t *gb,
988*4882a593Smuzhiyun AV1RawFrameHeader *current)
989*4882a593Smuzhiyun {
990*4882a593Smuzhiyun RK_S32 err;
991*4882a593Smuzhiyun (void)ctx;
992*4882a593Smuzhiyun flag(is_filter_switchable);
993*4882a593Smuzhiyun if (current->is_filter_switchable)
994*4882a593Smuzhiyun infer(interpolation_filter,
995*4882a593Smuzhiyun AV1_INTERPOLATION_FILTER_SWITCHABLE);
996*4882a593Smuzhiyun else
997*4882a593Smuzhiyun fb(2, interpolation_filter);
998*4882a593Smuzhiyun
999*4882a593Smuzhiyun return 0;
1000*4882a593Smuzhiyun }
1001*4882a593Smuzhiyun
mpp_av1_tile_info(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1002*4882a593Smuzhiyun static RK_S32 mpp_av1_tile_info(AV1Context *ctx, BitReadCtx_t *gb,
1003*4882a593Smuzhiyun AV1RawFrameHeader *current)
1004*4882a593Smuzhiyun {
1005*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
1006*4882a593Smuzhiyun RK_S32 mi_cols, mi_rows, sb_cols, sb_rows, sb_shift, sb_size;
1007*4882a593Smuzhiyun RK_S32 max_tile_width_sb, max_tile_height_sb, max_tile_area_sb;
1008*4882a593Smuzhiyun RK_S32 min_log2_tile_cols, max_log2_tile_cols, max_log2_tile_rows;
1009*4882a593Smuzhiyun RK_S32 min_log2_tiles, min_log2_tile_rows;
1010*4882a593Smuzhiyun RK_S32 i, err;
1011*4882a593Smuzhiyun
1012*4882a593Smuzhiyun mi_cols = 2 * ((ctx->frame_width + 7) >> 3);
1013*4882a593Smuzhiyun mi_rows = 2 * ((ctx->frame_height + 7) >> 3);
1014*4882a593Smuzhiyun
1015*4882a593Smuzhiyun sb_cols = seq->use_128x128_superblock ? ((mi_cols + 31) >> 5)
1016*4882a593Smuzhiyun : ((mi_cols + 15) >> 4);
1017*4882a593Smuzhiyun sb_rows = seq->use_128x128_superblock ? ((mi_rows + 31) >> 5)
1018*4882a593Smuzhiyun : ((mi_rows + 15) >> 4);
1019*4882a593Smuzhiyun
1020*4882a593Smuzhiyun sb_shift = seq->use_128x128_superblock ? 5 : 4;
1021*4882a593Smuzhiyun sb_size = sb_shift + 2;
1022*4882a593Smuzhiyun
1023*4882a593Smuzhiyun max_tile_width_sb = AV1_MAX_TILE_WIDTH >> sb_size;
1024*4882a593Smuzhiyun max_tile_area_sb = AV1_MAX_TILE_AREA >> (2 * sb_size);
1025*4882a593Smuzhiyun
1026*4882a593Smuzhiyun min_log2_tile_cols = mpp_av1_tile_log2(max_tile_width_sb, sb_cols);
1027*4882a593Smuzhiyun max_log2_tile_cols = mpp_av1_tile_log2(1, MPP_MIN(sb_cols, AV1_MAX_TILE_COLS));
1028*4882a593Smuzhiyun max_log2_tile_rows = mpp_av1_tile_log2(1, MPP_MIN(sb_rows, AV1_MAX_TILE_ROWS));
1029*4882a593Smuzhiyun min_log2_tiles = MPP_MAX(min_log2_tile_cols,
1030*4882a593Smuzhiyun mpp_av1_tile_log2(max_tile_area_sb, sb_rows * sb_cols));
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun flag(uniform_tile_spacing_flag);
1033*4882a593Smuzhiyun
1034*4882a593Smuzhiyun if (current->uniform_tile_spacing_flag) {
1035*4882a593Smuzhiyun RK_S32 tile_width_sb, tile_height_sb;
1036*4882a593Smuzhiyun
1037*4882a593Smuzhiyun increment(tile_cols_log2, min_log2_tile_cols, max_log2_tile_cols);
1038*4882a593Smuzhiyun
1039*4882a593Smuzhiyun tile_width_sb = (sb_cols + (1 << current->tile_cols_log2) - 1) >>
1040*4882a593Smuzhiyun current->tile_cols_log2;
1041*4882a593Smuzhiyun current->tile_cols = (sb_cols + tile_width_sb - 1) / tile_width_sb;
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun min_log2_tile_rows = MPP_MAX(min_log2_tiles - current->tile_cols_log2, 0);
1044*4882a593Smuzhiyun
1045*4882a593Smuzhiyun increment(tile_rows_log2, min_log2_tile_rows, max_log2_tile_rows);
1046*4882a593Smuzhiyun
1047*4882a593Smuzhiyun tile_height_sb = (sb_rows + (1 << current->tile_rows_log2) - 1) >>
1048*4882a593Smuzhiyun current->tile_rows_log2;
1049*4882a593Smuzhiyun current->tile_rows = (sb_rows + tile_height_sb - 1) / tile_height_sb;
1050*4882a593Smuzhiyun
1051*4882a593Smuzhiyun for (i = 0; i < current->tile_cols - 1; i++)
1052*4882a593Smuzhiyun infer(width_in_sbs_minus_1[i], tile_width_sb - 1);
1053*4882a593Smuzhiyun infer(width_in_sbs_minus_1[i],
1054*4882a593Smuzhiyun sb_cols - (current->tile_cols - 1) * tile_width_sb - 1);
1055*4882a593Smuzhiyun for (i = 0; i < current->tile_rows - 1; i++)
1056*4882a593Smuzhiyun infer(height_in_sbs_minus_1[i], tile_height_sb - 1);
1057*4882a593Smuzhiyun infer(height_in_sbs_minus_1[i],
1058*4882a593Smuzhiyun sb_rows - (current->tile_rows - 1) * tile_height_sb - 1);
1059*4882a593Smuzhiyun
1060*4882a593Smuzhiyun } else {
1061*4882a593Smuzhiyun RK_S32 widest_tile_sb, start_sb, size_sb, max_width, max_height;
1062*4882a593Smuzhiyun
1063*4882a593Smuzhiyun widest_tile_sb = 0;
1064*4882a593Smuzhiyun
1065*4882a593Smuzhiyun start_sb = 0;
1066*4882a593Smuzhiyun for (i = 0; start_sb < sb_cols && i < AV1_MAX_TILE_COLS; i++) {
1067*4882a593Smuzhiyun max_width = MPP_MIN(sb_cols - start_sb, max_tile_width_sb);
1068*4882a593Smuzhiyun ns(max_width, width_in_sbs_minus_1[i]);
1069*4882a593Smuzhiyun //ns(max_width, width_in_sbs_minus_1[i]);
1070*4882a593Smuzhiyun size_sb = current->width_in_sbs_minus_1[i] + 1;
1071*4882a593Smuzhiyun widest_tile_sb = MPP_MAX(size_sb, widest_tile_sb);
1072*4882a593Smuzhiyun start_sb += size_sb;
1073*4882a593Smuzhiyun }
1074*4882a593Smuzhiyun current->tile_cols_log2 = mpp_av1_tile_log2(1, i);
1075*4882a593Smuzhiyun current->tile_cols = i;
1076*4882a593Smuzhiyun
1077*4882a593Smuzhiyun if (min_log2_tiles > 0)
1078*4882a593Smuzhiyun max_tile_area_sb = (sb_rows * sb_cols) >> (min_log2_tiles + 1);
1079*4882a593Smuzhiyun else
1080*4882a593Smuzhiyun max_tile_area_sb = sb_rows * sb_cols;
1081*4882a593Smuzhiyun max_tile_height_sb = MPP_MAX(max_tile_area_sb / widest_tile_sb, 1);
1082*4882a593Smuzhiyun
1083*4882a593Smuzhiyun start_sb = 0;
1084*4882a593Smuzhiyun for (i = 0; start_sb < sb_rows && i < AV1_MAX_TILE_ROWS; i++) {
1085*4882a593Smuzhiyun max_height = MPP_MIN(sb_rows - start_sb, max_tile_height_sb);
1086*4882a593Smuzhiyun ns(max_height, height_in_sbs_minus_1[i]);
1087*4882a593Smuzhiyun size_sb = current->height_in_sbs_minus_1[i] + 1;
1088*4882a593Smuzhiyun start_sb += size_sb;
1089*4882a593Smuzhiyun }
1090*4882a593Smuzhiyun current->tile_rows_log2 = mpp_av1_tile_log2(1, i);
1091*4882a593Smuzhiyun current->tile_rows = i;
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun
1094*4882a593Smuzhiyun if (current->tile_cols_log2 > 0 ||
1095*4882a593Smuzhiyun current->tile_rows_log2 > 0) {
1096*4882a593Smuzhiyun fb(current->tile_cols_log2 + current->tile_rows_log2,
1097*4882a593Smuzhiyun context_update_tile_id);
1098*4882a593Smuzhiyun fb(2, tile_size_bytes_minus1);
1099*4882a593Smuzhiyun } else {
1100*4882a593Smuzhiyun infer(context_update_tile_id, 0);
1101*4882a593Smuzhiyun current->tile_size_bytes_minus1 = 3;
1102*4882a593Smuzhiyun }
1103*4882a593Smuzhiyun
1104*4882a593Smuzhiyun ctx->tile_cols = current->tile_cols;
1105*4882a593Smuzhiyun ctx->tile_rows = current->tile_rows;
1106*4882a593Smuzhiyun
1107*4882a593Smuzhiyun return 0;
1108*4882a593Smuzhiyun }
1109*4882a593Smuzhiyun
mpp_av1_quantization_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1110*4882a593Smuzhiyun static RK_S32 mpp_av1_quantization_params(AV1Context *ctx, BitReadCtx_t *gb,
1111*4882a593Smuzhiyun AV1RawFrameHeader *current)
1112*4882a593Smuzhiyun {
1113*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
1114*4882a593Smuzhiyun RK_S32 err;
1115*4882a593Smuzhiyun
1116*4882a593Smuzhiyun fb(8, base_q_idx);
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun delta_q(delta_q_y_dc);
1119*4882a593Smuzhiyun
1120*4882a593Smuzhiyun if (ctx->num_planes > 1) {
1121*4882a593Smuzhiyun if (seq->color_config.separate_uv_delta_q)
1122*4882a593Smuzhiyun flag(diff_uv_delta);
1123*4882a593Smuzhiyun else
1124*4882a593Smuzhiyun infer(diff_uv_delta, 0);
1125*4882a593Smuzhiyun
1126*4882a593Smuzhiyun delta_q(delta_q_u_dc);
1127*4882a593Smuzhiyun delta_q(delta_q_u_ac);
1128*4882a593Smuzhiyun
1129*4882a593Smuzhiyun if (current->diff_uv_delta) {
1130*4882a593Smuzhiyun delta_q(delta_q_v_dc);
1131*4882a593Smuzhiyun delta_q(delta_q_v_ac);
1132*4882a593Smuzhiyun } else {
1133*4882a593Smuzhiyun infer(delta_q_v_dc, current->delta_q_u_dc);
1134*4882a593Smuzhiyun infer(delta_q_v_ac, current->delta_q_u_ac);
1135*4882a593Smuzhiyun }
1136*4882a593Smuzhiyun } else {
1137*4882a593Smuzhiyun infer(delta_q_u_dc, 0);
1138*4882a593Smuzhiyun infer(delta_q_u_ac, 0);
1139*4882a593Smuzhiyun infer(delta_q_v_dc, 0);
1140*4882a593Smuzhiyun infer(delta_q_v_ac, 0);
1141*4882a593Smuzhiyun }
1142*4882a593Smuzhiyun
1143*4882a593Smuzhiyun flag(using_qmatrix);
1144*4882a593Smuzhiyun if (current->using_qmatrix) {
1145*4882a593Smuzhiyun fb(4, qm_y);
1146*4882a593Smuzhiyun fb(4, qm_u);
1147*4882a593Smuzhiyun if (seq->color_config.separate_uv_delta_q)
1148*4882a593Smuzhiyun fb(4, qm_v);
1149*4882a593Smuzhiyun else
1150*4882a593Smuzhiyun infer(qm_v, current->qm_u);
1151*4882a593Smuzhiyun }
1152*4882a593Smuzhiyun
1153*4882a593Smuzhiyun return 0;
1154*4882a593Smuzhiyun }
1155*4882a593Smuzhiyun
mpp_av1_segmentation_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1156*4882a593Smuzhiyun static RK_S32 mpp_av1_segmentation_params(AV1Context *ctx, BitReadCtx_t *gb,
1157*4882a593Smuzhiyun AV1RawFrameHeader *current)
1158*4882a593Smuzhiyun {
1159*4882a593Smuzhiyun static const RK_U8 bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
1160*4882a593Smuzhiyun static const RK_U8 sign[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
1161*4882a593Smuzhiyun static const RK_U8 default_feature_enabled[AV1_SEG_LVL_MAX] = { 0 };
1162*4882a593Smuzhiyun static const RK_S16 default_feature_value[AV1_SEG_LVL_MAX] = { 0 };
1163*4882a593Smuzhiyun RK_S32 i, j, err;
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun flag(segmentation_enabled);
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun if (current->segmentation_enabled) {
1168*4882a593Smuzhiyun if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1169*4882a593Smuzhiyun infer(segmentation_update_map, 1);
1170*4882a593Smuzhiyun infer(segmentation_temporal_update, 0);
1171*4882a593Smuzhiyun infer(segmentation_update_data, 1);
1172*4882a593Smuzhiyun } else {
1173*4882a593Smuzhiyun flag(segmentation_update_map);
1174*4882a593Smuzhiyun if (current->segmentation_update_map)
1175*4882a593Smuzhiyun flag(segmentation_temporal_update);
1176*4882a593Smuzhiyun else
1177*4882a593Smuzhiyun infer(segmentation_temporal_update, 0);
1178*4882a593Smuzhiyun flag(segmentation_update_data);
1179*4882a593Smuzhiyun }
1180*4882a593Smuzhiyun
1181*4882a593Smuzhiyun for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1182*4882a593Smuzhiyun const RK_U8 *ref_feature_enabled;
1183*4882a593Smuzhiyun const RK_S16 *ref_feature_value;
1184*4882a593Smuzhiyun
1185*4882a593Smuzhiyun if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1186*4882a593Smuzhiyun ref_feature_enabled = default_feature_enabled;
1187*4882a593Smuzhiyun ref_feature_value = default_feature_value;
1188*4882a593Smuzhiyun } else {
1189*4882a593Smuzhiyun ref_feature_enabled =
1190*4882a593Smuzhiyun ctx->ref_s[current->ref_frame_idx[current->primary_ref_frame]].feature_enabled[i];
1191*4882a593Smuzhiyun ref_feature_value =
1192*4882a593Smuzhiyun ctx->ref_s[current->ref_frame_idx[current->primary_ref_frame]].feature_value[i];
1193*4882a593Smuzhiyun }
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
1196*4882a593Smuzhiyun if (current->segmentation_update_data) {
1197*4882a593Smuzhiyun flags(feature_enabled[i][j], 2, i, j);
1198*4882a593Smuzhiyun if (current->feature_enabled[i][j] && bits[j] > 0) {
1199*4882a593Smuzhiyun if (sign[j]) {
1200*4882a593Smuzhiyun RK_S32 sign_, data;
1201*4882a593Smuzhiyun
1202*4882a593Smuzhiyun READ_ONEBIT(gb, &sign_);
1203*4882a593Smuzhiyun READ_BITS(gb, bits[j], &data);
1204*4882a593Smuzhiyun if (sign_) data -= (1 << bits[j]);
1205*4882a593Smuzhiyun current->feature_value[i][j] = data;
1206*4882a593Smuzhiyun } else
1207*4882a593Smuzhiyun fbs(bits[j], feature_value[i][j], 2, i, j);
1208*4882a593Smuzhiyun } else {
1209*4882a593Smuzhiyun infer(feature_value[i][j], 0);
1210*4882a593Smuzhiyun }
1211*4882a593Smuzhiyun } else {
1212*4882a593Smuzhiyun infer(feature_enabled[i][j], ref_feature_enabled[j]);
1213*4882a593Smuzhiyun infer(feature_value[i][j], ref_feature_value[j]);
1214*4882a593Smuzhiyun }
1215*4882a593Smuzhiyun }
1216*4882a593Smuzhiyun }
1217*4882a593Smuzhiyun } else {
1218*4882a593Smuzhiyun for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
1219*4882a593Smuzhiyun for (j = 0; j < AV1_SEG_LVL_MAX; j++) {
1220*4882a593Smuzhiyun infer(feature_enabled[i][j], 0);
1221*4882a593Smuzhiyun infer(feature_value[i][j], 0);
1222*4882a593Smuzhiyun }
1223*4882a593Smuzhiyun }
1224*4882a593Smuzhiyun }
1225*4882a593Smuzhiyun
1226*4882a593Smuzhiyun return 0;
1227*4882a593Smuzhiyun __BITREAD_ERR:
1228*4882a593Smuzhiyun return MPP_ERR_STREAM;
1229*4882a593Smuzhiyun }
1230*4882a593Smuzhiyun
mpp_av1_delta_q_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1231*4882a593Smuzhiyun static RK_S32 mpp_av1_delta_q_params(AV1Context *ctx, BitReadCtx_t *gb,
1232*4882a593Smuzhiyun AV1RawFrameHeader *current)
1233*4882a593Smuzhiyun {
1234*4882a593Smuzhiyun RK_S32 err;
1235*4882a593Smuzhiyun (void)ctx;
1236*4882a593Smuzhiyun if (current->base_q_idx > 0)
1237*4882a593Smuzhiyun flag(delta_q_present);
1238*4882a593Smuzhiyun else
1239*4882a593Smuzhiyun infer(delta_q_present, 0);
1240*4882a593Smuzhiyun
1241*4882a593Smuzhiyun if (current->delta_q_present)
1242*4882a593Smuzhiyun fb(2, delta_q_res);
1243*4882a593Smuzhiyun
1244*4882a593Smuzhiyun return 0;
1245*4882a593Smuzhiyun }
1246*4882a593Smuzhiyun
mpp_av1_delta_lf_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1247*4882a593Smuzhiyun static RK_S32 mpp_av1_delta_lf_params(AV1Context *ctx, BitReadCtx_t *gb,
1248*4882a593Smuzhiyun AV1RawFrameHeader *current)
1249*4882a593Smuzhiyun {
1250*4882a593Smuzhiyun RK_S32 err;
1251*4882a593Smuzhiyun (void)ctx;
1252*4882a593Smuzhiyun if (current->delta_q_present) {
1253*4882a593Smuzhiyun if (!current->allow_intrabc)
1254*4882a593Smuzhiyun flag(delta_lf_present);
1255*4882a593Smuzhiyun else
1256*4882a593Smuzhiyun infer(delta_lf_present, 0);
1257*4882a593Smuzhiyun if (current->delta_lf_present) {
1258*4882a593Smuzhiyun fb(2, delta_lf_res);
1259*4882a593Smuzhiyun flag(delta_lf_multi);
1260*4882a593Smuzhiyun } else {
1261*4882a593Smuzhiyun infer(delta_lf_res, 0);
1262*4882a593Smuzhiyun infer(delta_lf_multi, 0);
1263*4882a593Smuzhiyun }
1264*4882a593Smuzhiyun } else {
1265*4882a593Smuzhiyun infer(delta_lf_present, 0);
1266*4882a593Smuzhiyun infer(delta_lf_res, 0);
1267*4882a593Smuzhiyun infer(delta_lf_multi, 0);
1268*4882a593Smuzhiyun }
1269*4882a593Smuzhiyun
1270*4882a593Smuzhiyun return 0;
1271*4882a593Smuzhiyun }
1272*4882a593Smuzhiyun
mpp_av1_loop_filter_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1273*4882a593Smuzhiyun static RK_S32 mpp_av1_loop_filter_params(AV1Context *ctx, BitReadCtx_t *gb,
1274*4882a593Smuzhiyun AV1RawFrameHeader *current)
1275*4882a593Smuzhiyun {
1276*4882a593Smuzhiyun static const RK_S8 default_loop_filter_ref_deltas[AV1_TOTAL_REFS_PER_FRAME] =
1277*4882a593Smuzhiyun { 1, 0, 0, 0, -1, 0, -1, -1 };
1278*4882a593Smuzhiyun static const RK_S8 default_loop_filter_mode_deltas[2] = { 0, 0 };
1279*4882a593Smuzhiyun RK_S32 i, err;
1280*4882a593Smuzhiyun
1281*4882a593Smuzhiyun if (ctx->coded_lossless || current->allow_intrabc) {
1282*4882a593Smuzhiyun infer(loop_filter_level[0], 0);
1283*4882a593Smuzhiyun infer(loop_filter_level[1], 0);
1284*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_INTRA], 1);
1285*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST], 0);
1286*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST2], 0);
1287*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_LAST3], 0);
1288*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_BWDREF], 0);
1289*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_GOLDEN], -1);
1290*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF], -1);
1291*4882a593Smuzhiyun infer(loop_filter_ref_deltas[AV1_REF_FRAME_ALTREF2], -1);
1292*4882a593Smuzhiyun for (i = 0; i < 2; i++)
1293*4882a593Smuzhiyun infer(loop_filter_mode_deltas[i], 0);
1294*4882a593Smuzhiyun return 0;
1295*4882a593Smuzhiyun }
1296*4882a593Smuzhiyun
1297*4882a593Smuzhiyun fb(6, loop_filter_level[0]);
1298*4882a593Smuzhiyun fb(6, loop_filter_level[1]);
1299*4882a593Smuzhiyun
1300*4882a593Smuzhiyun if (ctx->num_planes > 1) {
1301*4882a593Smuzhiyun if (current->loop_filter_level[0] ||
1302*4882a593Smuzhiyun current->loop_filter_level[1]) {
1303*4882a593Smuzhiyun fb(6, loop_filter_level[2]);
1304*4882a593Smuzhiyun fb(6, loop_filter_level[3]);
1305*4882a593Smuzhiyun }
1306*4882a593Smuzhiyun }
1307*4882a593Smuzhiyun
1308*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "orderhint %d loop_filter_level %d %d %d %d\n",
1309*4882a593Smuzhiyun current->order_hint,
1310*4882a593Smuzhiyun current->loop_filter_level[0], current->loop_filter_level[1],
1311*4882a593Smuzhiyun current->loop_filter_level[2], current->loop_filter_level[3]);
1312*4882a593Smuzhiyun fb(3, loop_filter_sharpness);
1313*4882a593Smuzhiyun
1314*4882a593Smuzhiyun flag(loop_filter_delta_enabled);
1315*4882a593Smuzhiyun if (current->loop_filter_delta_enabled) {
1316*4882a593Smuzhiyun const RK_S8 *ref_loop_filter_ref_deltas, *ref_loop_filter_mode_deltas;
1317*4882a593Smuzhiyun
1318*4882a593Smuzhiyun if (current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1319*4882a593Smuzhiyun ref_loop_filter_ref_deltas = default_loop_filter_ref_deltas;
1320*4882a593Smuzhiyun ref_loop_filter_mode_deltas = default_loop_filter_mode_deltas;
1321*4882a593Smuzhiyun } else {
1322*4882a593Smuzhiyun ref_loop_filter_ref_deltas =
1323*4882a593Smuzhiyun ctx->ref_s[current->ref_frame_idx[current->primary_ref_frame]].loop_filter_ref_deltas;
1324*4882a593Smuzhiyun ref_loop_filter_mode_deltas =
1325*4882a593Smuzhiyun ctx->ref_s[current->ref_frame_idx[current->primary_ref_frame]].loop_filter_mode_deltas;
1326*4882a593Smuzhiyun }
1327*4882a593Smuzhiyun
1328*4882a593Smuzhiyun flag(loop_filter_delta_update);
1329*4882a593Smuzhiyun for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++) {
1330*4882a593Smuzhiyun if (current->loop_filter_delta_update)
1331*4882a593Smuzhiyun flags(update_ref_delta[i], 1, i);
1332*4882a593Smuzhiyun else
1333*4882a593Smuzhiyun infer(update_ref_delta[i], 0);
1334*4882a593Smuzhiyun if (current->update_ref_delta[i])
1335*4882a593Smuzhiyun sus(1 + 6, loop_filter_ref_deltas[i], 1, i);
1336*4882a593Smuzhiyun else
1337*4882a593Smuzhiyun infer(loop_filter_ref_deltas[i], ref_loop_filter_ref_deltas[i]);
1338*4882a593Smuzhiyun }
1339*4882a593Smuzhiyun for (i = 0; i < 2; i++) {
1340*4882a593Smuzhiyun if (current->loop_filter_delta_update)
1341*4882a593Smuzhiyun flags(update_mode_delta[i], 1, i);
1342*4882a593Smuzhiyun else
1343*4882a593Smuzhiyun infer(update_mode_delta[i], 0);
1344*4882a593Smuzhiyun if (current->update_mode_delta[i])
1345*4882a593Smuzhiyun sus(1 + 6, loop_filter_mode_deltas[i], 1, i);
1346*4882a593Smuzhiyun else
1347*4882a593Smuzhiyun infer(loop_filter_mode_deltas[i], ref_loop_filter_mode_deltas[i]);
1348*4882a593Smuzhiyun }
1349*4882a593Smuzhiyun } else {
1350*4882a593Smuzhiyun for (i = 0; i < AV1_TOTAL_REFS_PER_FRAME; i++)
1351*4882a593Smuzhiyun infer(loop_filter_ref_deltas[i], default_loop_filter_ref_deltas[i]);
1352*4882a593Smuzhiyun for (i = 0; i < 2; i++)
1353*4882a593Smuzhiyun infer(loop_filter_mode_deltas[i], default_loop_filter_mode_deltas[i]);
1354*4882a593Smuzhiyun }
1355*4882a593Smuzhiyun
1356*4882a593Smuzhiyun return 0;
1357*4882a593Smuzhiyun }
1358*4882a593Smuzhiyun
mpp_av1_cdef_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1359*4882a593Smuzhiyun static RK_S32 mpp_av1_cdef_params(AV1Context *ctx, BitReadCtx_t *gb,
1360*4882a593Smuzhiyun AV1RawFrameHeader *current)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
1363*4882a593Smuzhiyun RK_S32 i, err;
1364*4882a593Smuzhiyun if (ctx->coded_lossless || current->allow_intrabc ||
1365*4882a593Smuzhiyun !seq->enable_cdef) {
1366*4882a593Smuzhiyun infer(cdef_damping_minus_3, 0);
1367*4882a593Smuzhiyun infer(cdef_bits, 0);
1368*4882a593Smuzhiyun infer(cdef_y_pri_strength[0], 0);
1369*4882a593Smuzhiyun infer(cdef_y_sec_strength[0], 0);
1370*4882a593Smuzhiyun infer(cdef_uv_pri_strength[0], 0);
1371*4882a593Smuzhiyun infer(cdef_uv_sec_strength[0], 0);
1372*4882a593Smuzhiyun
1373*4882a593Smuzhiyun return 0;
1374*4882a593Smuzhiyun }
1375*4882a593Smuzhiyun
1376*4882a593Smuzhiyun fb(2, cdef_damping_minus_3);
1377*4882a593Smuzhiyun fb(2, cdef_bits);
1378*4882a593Smuzhiyun
1379*4882a593Smuzhiyun for (i = 0; i < (1 << current->cdef_bits); i++) {
1380*4882a593Smuzhiyun fbs(4, cdef_y_pri_strength[i], 1, i);
1381*4882a593Smuzhiyun fbs(2, cdef_y_sec_strength[i], 1, i);
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun if (ctx->num_planes > 1) {
1384*4882a593Smuzhiyun fbs(4, cdef_uv_pri_strength[i], 1, i);
1385*4882a593Smuzhiyun fbs(2, cdef_uv_sec_strength[i], 1, i);
1386*4882a593Smuzhiyun }
1387*4882a593Smuzhiyun }
1388*4882a593Smuzhiyun
1389*4882a593Smuzhiyun return 0;
1390*4882a593Smuzhiyun }
1391*4882a593Smuzhiyun
mpp_av1_lr_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1392*4882a593Smuzhiyun static RK_S32 mpp_av1_lr_params(AV1Context *ctx, BitReadCtx_t *gb,
1393*4882a593Smuzhiyun AV1RawFrameHeader *current)
1394*4882a593Smuzhiyun {
1395*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
1396*4882a593Smuzhiyun RK_S32 uses_lr, uses_chroma_lr;
1397*4882a593Smuzhiyun RK_S32 i, err;
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun if (ctx->all_lossless || current->allow_intrabc ||
1400*4882a593Smuzhiyun !seq->enable_restoration) {
1401*4882a593Smuzhiyun return 0;
1402*4882a593Smuzhiyun }
1403*4882a593Smuzhiyun
1404*4882a593Smuzhiyun uses_lr = uses_chroma_lr = 0;
1405*4882a593Smuzhiyun for (i = 0; i < ctx->num_planes; i++) {
1406*4882a593Smuzhiyun fbs(2, lr_type[i], 1, i);
1407*4882a593Smuzhiyun
1408*4882a593Smuzhiyun if (current->lr_type[i] != AV1_RESTORE_NONE) {
1409*4882a593Smuzhiyun uses_lr = 1;
1410*4882a593Smuzhiyun if (i > 0)
1411*4882a593Smuzhiyun uses_chroma_lr = 1;
1412*4882a593Smuzhiyun }
1413*4882a593Smuzhiyun }
1414*4882a593Smuzhiyun
1415*4882a593Smuzhiyun if (uses_lr) {
1416*4882a593Smuzhiyun if (seq->use_128x128_superblock)
1417*4882a593Smuzhiyun increment(lr_unit_shift, 1, 2);
1418*4882a593Smuzhiyun else
1419*4882a593Smuzhiyun increment(lr_unit_shift, 0, 2);
1420*4882a593Smuzhiyun
1421*4882a593Smuzhiyun if (seq->color_config.subsampling_x &&
1422*4882a593Smuzhiyun seq->color_config.subsampling_y && uses_chroma_lr) {
1423*4882a593Smuzhiyun fb(1, lr_uv_shift);
1424*4882a593Smuzhiyun } else {
1425*4882a593Smuzhiyun infer(lr_uv_shift, 0);
1426*4882a593Smuzhiyun }
1427*4882a593Smuzhiyun }
1428*4882a593Smuzhiyun
1429*4882a593Smuzhiyun return 0;
1430*4882a593Smuzhiyun }
1431*4882a593Smuzhiyun
mpp_av1_read_tx_mode(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1432*4882a593Smuzhiyun static RK_S32 mpp_av1_read_tx_mode(AV1Context *ctx, BitReadCtx_t *gb,
1433*4882a593Smuzhiyun AV1RawFrameHeader *current)
1434*4882a593Smuzhiyun {
1435*4882a593Smuzhiyun RK_S32 err;
1436*4882a593Smuzhiyun
1437*4882a593Smuzhiyun if (ctx->coded_lossless)
1438*4882a593Smuzhiyun infer(tx_mode, 0);
1439*4882a593Smuzhiyun else {
1440*4882a593Smuzhiyun flag(tx_mode);
1441*4882a593Smuzhiyun current->tx_mode = current->tx_mode ? 4 : 3;
1442*4882a593Smuzhiyun }
1443*4882a593Smuzhiyun
1444*4882a593Smuzhiyun return 0;
1445*4882a593Smuzhiyun }
1446*4882a593Smuzhiyun
mpp_av1_frame_reference_mode(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1447*4882a593Smuzhiyun static RK_S32 mpp_av1_frame_reference_mode(AV1Context *ctx, BitReadCtx_t *gb,
1448*4882a593Smuzhiyun AV1RawFrameHeader *current)
1449*4882a593Smuzhiyun {
1450*4882a593Smuzhiyun RK_S32 err;
1451*4882a593Smuzhiyun (void)ctx;
1452*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1453*4882a593Smuzhiyun current->frame_type == AV1_FRAME_KEY)
1454*4882a593Smuzhiyun infer(reference_select, 0);
1455*4882a593Smuzhiyun else
1456*4882a593Smuzhiyun flag(reference_select);
1457*4882a593Smuzhiyun
1458*4882a593Smuzhiyun return 0;
1459*4882a593Smuzhiyun }
1460*4882a593Smuzhiyun
mpp_av1_skip_mode_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1461*4882a593Smuzhiyun static RK_S32 mpp_av1_skip_mode_params(AV1Context *ctx, BitReadCtx_t *gb,
1462*4882a593Smuzhiyun AV1RawFrameHeader *current)
1463*4882a593Smuzhiyun {
1464*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
1465*4882a593Smuzhiyun RK_S32 skip_mode_allowed;
1466*4882a593Smuzhiyun RK_S32 err;
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_KEY ||
1469*4882a593Smuzhiyun current->frame_type == AV1_FRAME_INTRA_ONLY ||
1470*4882a593Smuzhiyun !current->reference_select || !seq->enable_order_hint) {
1471*4882a593Smuzhiyun skip_mode_allowed = 0;
1472*4882a593Smuzhiyun } else {
1473*4882a593Smuzhiyun RK_S32 forward_idx, backward_idx;
1474*4882a593Smuzhiyun RK_S32 forward_hint, backward_hint;
1475*4882a593Smuzhiyun RK_S32 ref_hint, dist, i;
1476*4882a593Smuzhiyun
1477*4882a593Smuzhiyun forward_idx = -1;
1478*4882a593Smuzhiyun backward_idx = -1;
1479*4882a593Smuzhiyun forward_hint = -1;
1480*4882a593Smuzhiyun backward_hint = -1;
1481*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1482*4882a593Smuzhiyun ref_hint = ctx->ref_s[current->ref_frame_idx[i]].order_hint;
1483*4882a593Smuzhiyun dist = mpp_av1_get_relative_dist(seq, ref_hint,
1484*4882a593Smuzhiyun ctx->order_hint);
1485*4882a593Smuzhiyun if (dist < 0) {
1486*4882a593Smuzhiyun if (forward_idx < 0 ||
1487*4882a593Smuzhiyun mpp_av1_get_relative_dist(seq, ref_hint,
1488*4882a593Smuzhiyun forward_hint) > 0) {
1489*4882a593Smuzhiyun forward_idx = i;
1490*4882a593Smuzhiyun forward_hint = ref_hint;
1491*4882a593Smuzhiyun }
1492*4882a593Smuzhiyun } else if (dist > 0) {
1493*4882a593Smuzhiyun if (backward_idx < 0 ||
1494*4882a593Smuzhiyun mpp_av1_get_relative_dist(seq, ref_hint,
1495*4882a593Smuzhiyun backward_hint) < 0) {
1496*4882a593Smuzhiyun backward_idx = i;
1497*4882a593Smuzhiyun backward_hint = ref_hint;
1498*4882a593Smuzhiyun }
1499*4882a593Smuzhiyun }
1500*4882a593Smuzhiyun }
1501*4882a593Smuzhiyun
1502*4882a593Smuzhiyun if (forward_idx < 0) {
1503*4882a593Smuzhiyun skip_mode_allowed = 0;
1504*4882a593Smuzhiyun } else if (backward_idx >= 0) {
1505*4882a593Smuzhiyun skip_mode_allowed = 1;
1506*4882a593Smuzhiyun ctx->skip_ref0 = MPP_MIN(forward_idx, backward_idx) + 1;
1507*4882a593Smuzhiyun ctx->skip_ref1 = MPP_MAX(forward_idx, backward_idx) + 1;
1508*4882a593Smuzhiyun // Frames for skip mode are forward_idx and backward_idx.
1509*4882a593Smuzhiyun } else {
1510*4882a593Smuzhiyun RK_S32 second_forward_idx;
1511*4882a593Smuzhiyun RK_S32 second_forward_hint;
1512*4882a593Smuzhiyun second_forward_idx = -1;
1513*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1514*4882a593Smuzhiyun ref_hint = ctx->ref_s[current->ref_frame_idx[i]].order_hint;
1515*4882a593Smuzhiyun if (mpp_av1_get_relative_dist(seq, ref_hint,
1516*4882a593Smuzhiyun forward_hint) < 0) {
1517*4882a593Smuzhiyun if (second_forward_idx < 0 ||
1518*4882a593Smuzhiyun mpp_av1_get_relative_dist(seq, ref_hint,
1519*4882a593Smuzhiyun second_forward_hint) > 0) {
1520*4882a593Smuzhiyun second_forward_idx = i;
1521*4882a593Smuzhiyun second_forward_hint = ref_hint;
1522*4882a593Smuzhiyun }
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun }
1525*4882a593Smuzhiyun
1526*4882a593Smuzhiyun if (second_forward_idx < 0) {
1527*4882a593Smuzhiyun skip_mode_allowed = 0;
1528*4882a593Smuzhiyun } else {
1529*4882a593Smuzhiyun ctx->skip_ref0 = MPP_MIN(forward_idx, second_forward_idx) + 1;
1530*4882a593Smuzhiyun ctx->skip_ref1 = MPP_MAX(forward_idx, second_forward_idx) + 1;
1531*4882a593Smuzhiyun skip_mode_allowed = 1;
1532*4882a593Smuzhiyun // Frames for skip mode are forward_idx and second_forward_idx.
1533*4882a593Smuzhiyun }
1534*4882a593Smuzhiyun }
1535*4882a593Smuzhiyun }
1536*4882a593Smuzhiyun
1537*4882a593Smuzhiyun if (skip_mode_allowed)
1538*4882a593Smuzhiyun flag(skip_mode_present);
1539*4882a593Smuzhiyun else
1540*4882a593Smuzhiyun infer(skip_mode_present, 0);
1541*4882a593Smuzhiyun
1542*4882a593Smuzhiyun return 0;
1543*4882a593Smuzhiyun }
1544*4882a593Smuzhiyun
mpp_av1_global_motion_param(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current,RK_S32 type,RK_S32 ref,RK_S32 idx)1545*4882a593Smuzhiyun static RK_S32 mpp_av1_global_motion_param(AV1Context *ctx, BitReadCtx_t *gb,
1546*4882a593Smuzhiyun AV1RawFrameHeader *current,
1547*4882a593Smuzhiyun RK_S32 type, RK_S32 ref, RK_S32 idx)
1548*4882a593Smuzhiyun {
1549*4882a593Smuzhiyun RK_U32 abs_bits, prec_bits, num_syms;
1550*4882a593Smuzhiyun RK_S32 err;
1551*4882a593Smuzhiyun (void)ctx;
1552*4882a593Smuzhiyun if (idx < 2) {
1553*4882a593Smuzhiyun if (type == AV1_WARP_MODEL_TRANSLATION) {
1554*4882a593Smuzhiyun abs_bits = AV1_GM_ABS_TRANS_ONLY_BITS - !current->allow_high_precision_mv;
1555*4882a593Smuzhiyun prec_bits = AV1_GM_TRANS_ONLY_PREC_BITS - !current->allow_high_precision_mv;
1556*4882a593Smuzhiyun } else {
1557*4882a593Smuzhiyun abs_bits = AV1_GM_ABS_TRANS_BITS;
1558*4882a593Smuzhiyun prec_bits = AV1_GM_TRANS_PREC_BITS;
1559*4882a593Smuzhiyun }
1560*4882a593Smuzhiyun } else {
1561*4882a593Smuzhiyun abs_bits = AV1_GM_ABS_ALPHA_BITS;
1562*4882a593Smuzhiyun prec_bits = AV1_GM_ALPHA_PREC_BITS;
1563*4882a593Smuzhiyun }
1564*4882a593Smuzhiyun
1565*4882a593Smuzhiyun num_syms = 2 * (1 << abs_bits) + 1;
1566*4882a593Smuzhiyun subexp(gm_params[ref][idx], num_syms);// 2, ref, idx);
1567*4882a593Smuzhiyun
1568*4882a593Smuzhiyun // Actual gm_params value is not reconstructed here.
1569*4882a593Smuzhiyun (void)prec_bits;
1570*4882a593Smuzhiyun
1571*4882a593Smuzhiyun return 0;
1572*4882a593Smuzhiyun }
1573*4882a593Smuzhiyun
1574*4882a593Smuzhiyun /*
1575*4882a593Smuzhiyun * Actual gm_params value is not reconstructed here.
1576*4882a593Smuzhiyun * Real gm_params update in av1d_parser.c->global_motion_params()
1577*4882a593Smuzhiyun */
mpp_av1_global_motion_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1578*4882a593Smuzhiyun static RK_S32 mpp_av1_global_motion_params(AV1Context *ctx, BitReadCtx_t *gb,
1579*4882a593Smuzhiyun AV1RawFrameHeader *current)
1580*4882a593Smuzhiyun {
1581*4882a593Smuzhiyun RK_S32 ref, type;
1582*4882a593Smuzhiyun RK_S32 err;
1583*4882a593Smuzhiyun
1584*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_KEY ||
1585*4882a593Smuzhiyun current->frame_type == AV1_FRAME_INTRA_ONLY)
1586*4882a593Smuzhiyun return 0;
1587*4882a593Smuzhiyun
1588*4882a593Smuzhiyun for (ref = AV1_REF_FRAME_LAST; ref <= AV1_REF_FRAME_ALTREF; ref++) {
1589*4882a593Smuzhiyun flags(is_global[ref], 1, ref);
1590*4882a593Smuzhiyun if (current->is_global[ref]) {
1591*4882a593Smuzhiyun flags(is_rot_zoom[ref], 1, ref);
1592*4882a593Smuzhiyun if (current->is_rot_zoom[ref]) {
1593*4882a593Smuzhiyun type = AV1_WARP_MODEL_ROTZOOM;
1594*4882a593Smuzhiyun } else {
1595*4882a593Smuzhiyun flags(is_translation[ref], 1, ref);
1596*4882a593Smuzhiyun type = current->is_translation[ref] ? AV1_WARP_MODEL_TRANSLATION
1597*4882a593Smuzhiyun : AV1_WARP_MODEL_AFFINE;
1598*4882a593Smuzhiyun }
1599*4882a593Smuzhiyun } else {
1600*4882a593Smuzhiyun type = AV1_WARP_MODEL_IDENTITY;
1601*4882a593Smuzhiyun }
1602*4882a593Smuzhiyun
1603*4882a593Smuzhiyun if (type >= AV1_WARP_MODEL_ROTZOOM) {
1604*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 2));
1605*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 3));
1606*4882a593Smuzhiyun if (type == AV1_WARP_MODEL_AFFINE) {
1607*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 4));
1608*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 5));
1609*4882a593Smuzhiyun } else {
1610*4882a593Smuzhiyun current->gm_params[ref][4] = -current->gm_params[ref][3];
1611*4882a593Smuzhiyun current->gm_params[ref][5] = current->gm_params[ref][2];
1612*4882a593Smuzhiyun }
1613*4882a593Smuzhiyun }
1614*4882a593Smuzhiyun if (type >= AV1_WARP_MODEL_TRANSLATION) {
1615*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 0));
1616*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_param(ctx, gb, current, type, ref, 1));
1617*4882a593Smuzhiyun }
1618*4882a593Smuzhiyun }
1619*4882a593Smuzhiyun
1620*4882a593Smuzhiyun return 0;
1621*4882a593Smuzhiyun }
1622*4882a593Smuzhiyun
mpp_av1_film_grain_params(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFilmGrainParams * current,AV1RawFrameHeader * frame_header)1623*4882a593Smuzhiyun static RK_S32 mpp_av1_film_grain_params(AV1Context *ctx, BitReadCtx_t *gb,
1624*4882a593Smuzhiyun AV1RawFilmGrainParams *current,
1625*4882a593Smuzhiyun AV1RawFrameHeader *frame_header)
1626*4882a593Smuzhiyun {
1627*4882a593Smuzhiyun const AV1RawSequenceHeader *seq = ctx->sequence_header;
1628*4882a593Smuzhiyun RK_S32 num_pos_luma, num_pos_chroma;
1629*4882a593Smuzhiyun RK_S32 i, err;
1630*4882a593Smuzhiyun
1631*4882a593Smuzhiyun if (!seq->film_grain_params_present ||
1632*4882a593Smuzhiyun (!frame_header->show_frame && !frame_header->showable_frame))
1633*4882a593Smuzhiyun return 0;
1634*4882a593Smuzhiyun
1635*4882a593Smuzhiyun flag(apply_grain);
1636*4882a593Smuzhiyun
1637*4882a593Smuzhiyun if (!current->apply_grain)
1638*4882a593Smuzhiyun return 0;
1639*4882a593Smuzhiyun
1640*4882a593Smuzhiyun fb(16, grain_seed);
1641*4882a593Smuzhiyun
1642*4882a593Smuzhiyun if (frame_header->frame_type == AV1_FRAME_INTER)
1643*4882a593Smuzhiyun flag(update_grain);
1644*4882a593Smuzhiyun else
1645*4882a593Smuzhiyun infer(update_grain, 1);
1646*4882a593Smuzhiyun
1647*4882a593Smuzhiyun if (!current->update_grain) {
1648*4882a593Smuzhiyun fb(3, film_grain_params_ref_idx);
1649*4882a593Smuzhiyun return 0;
1650*4882a593Smuzhiyun }
1651*4882a593Smuzhiyun
1652*4882a593Smuzhiyun fc(4, num_y_points, 0, 14);
1653*4882a593Smuzhiyun for (i = 0; i < current->num_y_points; i++) {
1654*4882a593Smuzhiyun fcs(8, point_y_value[i],
1655*4882a593Smuzhiyun i ? current->point_y_value[i - 1] + 1 : 0,
1656*4882a593Smuzhiyun MAX_UINT_BITS(8) - (current->num_y_points - i - 1),
1657*4882a593Smuzhiyun 1, i);
1658*4882a593Smuzhiyun fbs(8, point_y_scaling[i], 1, i);
1659*4882a593Smuzhiyun }
1660*4882a593Smuzhiyun
1661*4882a593Smuzhiyun if (seq->color_config.mono_chrome)
1662*4882a593Smuzhiyun infer(chroma_scaling_from_luma, 0);
1663*4882a593Smuzhiyun else
1664*4882a593Smuzhiyun flag(chroma_scaling_from_luma);
1665*4882a593Smuzhiyun
1666*4882a593Smuzhiyun if (seq->color_config.mono_chrome ||
1667*4882a593Smuzhiyun current->chroma_scaling_from_luma ||
1668*4882a593Smuzhiyun (seq->color_config.subsampling_x == 1 &&
1669*4882a593Smuzhiyun seq->color_config.subsampling_y == 1 &&
1670*4882a593Smuzhiyun current->num_y_points == 0)) {
1671*4882a593Smuzhiyun infer(num_cb_points, 0);
1672*4882a593Smuzhiyun infer(num_cr_points, 0);
1673*4882a593Smuzhiyun } else {
1674*4882a593Smuzhiyun fc(4, num_cb_points, 0, 10);
1675*4882a593Smuzhiyun for (i = 0; i < current->num_cb_points; i++) {
1676*4882a593Smuzhiyun fcs(8, point_cb_value[i],
1677*4882a593Smuzhiyun i ? current->point_cb_value[i - 1] + 1 : 0,
1678*4882a593Smuzhiyun MAX_UINT_BITS(8) - (current->num_cb_points - i - 1),
1679*4882a593Smuzhiyun 1, i);
1680*4882a593Smuzhiyun fbs(8, point_cb_scaling[i], 1, i);
1681*4882a593Smuzhiyun }
1682*4882a593Smuzhiyun fc(4, num_cr_points, 0, 10);
1683*4882a593Smuzhiyun for (i = 0; i < current->num_cr_points; i++) {
1684*4882a593Smuzhiyun fcs(8, point_cr_value[i],
1685*4882a593Smuzhiyun i ? current->point_cr_value[i - 1] + 1 : 0,
1686*4882a593Smuzhiyun MAX_UINT_BITS(8) - (current->num_cr_points - i - 1),
1687*4882a593Smuzhiyun 1, i);
1688*4882a593Smuzhiyun fbs(8, point_cr_scaling[i], 1, i);
1689*4882a593Smuzhiyun }
1690*4882a593Smuzhiyun }
1691*4882a593Smuzhiyun
1692*4882a593Smuzhiyun fb(2, grain_scaling_minus_8);
1693*4882a593Smuzhiyun fb(2, ar_coeff_lag);
1694*4882a593Smuzhiyun num_pos_luma = 2 * current->ar_coeff_lag * (current->ar_coeff_lag + 1);
1695*4882a593Smuzhiyun if (current->num_y_points) {
1696*4882a593Smuzhiyun num_pos_chroma = num_pos_luma + 1;
1697*4882a593Smuzhiyun for (i = 0; i < num_pos_luma; i++)
1698*4882a593Smuzhiyun fbs(8, ar_coeffs_y_plus_128[i], 1, i);
1699*4882a593Smuzhiyun } else {
1700*4882a593Smuzhiyun num_pos_chroma = num_pos_luma;
1701*4882a593Smuzhiyun }
1702*4882a593Smuzhiyun if (current->chroma_scaling_from_luma || current->num_cb_points) {
1703*4882a593Smuzhiyun for (i = 0; i < num_pos_chroma; i++)
1704*4882a593Smuzhiyun fbs(8, ar_coeffs_cb_plus_128[i], 1, i);
1705*4882a593Smuzhiyun }
1706*4882a593Smuzhiyun if (current->chroma_scaling_from_luma || current->num_cr_points) {
1707*4882a593Smuzhiyun for (i = 0; i < num_pos_chroma; i++)
1708*4882a593Smuzhiyun fbs(8, ar_coeffs_cr_plus_128[i], 1, i);
1709*4882a593Smuzhiyun }
1710*4882a593Smuzhiyun fb(2, ar_coeff_shift_minus_6);
1711*4882a593Smuzhiyun fb(2, grain_scale_shift);
1712*4882a593Smuzhiyun if (current->num_cb_points) {
1713*4882a593Smuzhiyun fb(8, cb_mult);
1714*4882a593Smuzhiyun fb(8, cb_luma_mult);
1715*4882a593Smuzhiyun fb(9, cb_offset);
1716*4882a593Smuzhiyun }
1717*4882a593Smuzhiyun if (current->num_cr_points) {
1718*4882a593Smuzhiyun fb(8, cr_mult);
1719*4882a593Smuzhiyun fb(8, cr_luma_mult);
1720*4882a593Smuzhiyun fb(9, cr_offset);
1721*4882a593Smuzhiyun }
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun flag(overlap_flag);
1724*4882a593Smuzhiyun flag(clip_to_restricted_range);
1725*4882a593Smuzhiyun
1726*4882a593Smuzhiyun return 0;
1727*4882a593Smuzhiyun }
1728*4882a593Smuzhiyun
mpp_av1_uncompressed_header(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current)1729*4882a593Smuzhiyun static RK_S32 mpp_av1_uncompressed_header(AV1Context *ctx, BitReadCtx_t *gb,
1730*4882a593Smuzhiyun AV1RawFrameHeader *current)
1731*4882a593Smuzhiyun {
1732*4882a593Smuzhiyun const AV1RawSequenceHeader *seq;
1733*4882a593Smuzhiyun RK_S32 id_len, diff_len, all_frames, frame_is_intra, order_hint_bits;
1734*4882a593Smuzhiyun RK_S32 i, err;
1735*4882a593Smuzhiyun
1736*4882a593Smuzhiyun if (!ctx->sequence_header) {
1737*4882a593Smuzhiyun mpp_err_f("No sequence header available: "
1738*4882a593Smuzhiyun "unable to decode frame header.\n");
1739*4882a593Smuzhiyun return MPP_ERR_UNKNOW;
1740*4882a593Smuzhiyun }
1741*4882a593Smuzhiyun seq = ctx->sequence_header;
1742*4882a593Smuzhiyun
1743*4882a593Smuzhiyun id_len = seq->additional_frame_id_length_minus_1 +
1744*4882a593Smuzhiyun seq->delta_frame_id_length_minus_2 + 3;
1745*4882a593Smuzhiyun all_frames = (1 << AV1_NUM_REF_FRAMES) - 1;
1746*4882a593Smuzhiyun
1747*4882a593Smuzhiyun if (seq->reduced_still_picture_header) {
1748*4882a593Smuzhiyun infer(show_existing_frame, 0);
1749*4882a593Smuzhiyun infer(frame_type, AV1_FRAME_KEY);
1750*4882a593Smuzhiyun infer(show_frame, 1);
1751*4882a593Smuzhiyun infer(showable_frame, 0);
1752*4882a593Smuzhiyun frame_is_intra = 1;
1753*4882a593Smuzhiyun
1754*4882a593Smuzhiyun } else {
1755*4882a593Smuzhiyun flag(show_existing_frame);
1756*4882a593Smuzhiyun
1757*4882a593Smuzhiyun if (current->show_existing_frame) {
1758*4882a593Smuzhiyun AV1ReferenceFrameState *ref;
1759*4882a593Smuzhiyun
1760*4882a593Smuzhiyun fb(3, frame_to_show_map_idx);
1761*4882a593Smuzhiyun ref = &ctx->ref_s[current->frame_to_show_map_idx];
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun if (!ref->valid) {
1764*4882a593Smuzhiyun mpp_err_f("Missing reference frame needed for "
1765*4882a593Smuzhiyun "show_existing_frame (frame_to_show_map_idx = %d).\n",
1766*4882a593Smuzhiyun current->frame_to_show_map_idx);
1767*4882a593Smuzhiyun return MPP_ERR_UNKNOW;
1768*4882a593Smuzhiyun }
1769*4882a593Smuzhiyun
1770*4882a593Smuzhiyun if (seq->decoder_model_info_present_flag &&
1771*4882a593Smuzhiyun !seq->timing_info.equal_picture_interval) {
1772*4882a593Smuzhiyun fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1773*4882a593Smuzhiyun frame_presentation_time);
1774*4882a593Smuzhiyun }
1775*4882a593Smuzhiyun
1776*4882a593Smuzhiyun if (seq->frame_id_numbers_present_flag)
1777*4882a593Smuzhiyun fb(id_len, display_frame_id);
1778*4882a593Smuzhiyun
1779*4882a593Smuzhiyun infer(frame_type, ref->frame_type);
1780*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_KEY) {
1781*4882a593Smuzhiyun infer(refresh_frame_flags, all_frames);
1782*4882a593Smuzhiyun
1783*4882a593Smuzhiyun // Section 7.21
1784*4882a593Smuzhiyun infer(current_frame_id, ref->frame_id);
1785*4882a593Smuzhiyun ctx->upscaled_width = ref->upscaled_width;
1786*4882a593Smuzhiyun ctx->frame_width = ref->frame_width;
1787*4882a593Smuzhiyun ctx->frame_height = ref->frame_height;
1788*4882a593Smuzhiyun ctx->render_width = ref->render_width;
1789*4882a593Smuzhiyun ctx->render_height = ref->render_height;
1790*4882a593Smuzhiyun ctx->bit_depth = ref->bit_depth;
1791*4882a593Smuzhiyun ctx->order_hint = ref->order_hint;
1792*4882a593Smuzhiyun } else
1793*4882a593Smuzhiyun infer(refresh_frame_flags, 0);
1794*4882a593Smuzhiyun
1795*4882a593Smuzhiyun infer(frame_width_minus_1, ref->upscaled_width - 1);
1796*4882a593Smuzhiyun infer(frame_height_minus_1, ref->frame_height - 1);
1797*4882a593Smuzhiyun infer(render_width_minus_1, ref->render_width - 1);
1798*4882a593Smuzhiyun infer(render_height_minus_1, ref->render_height - 1);
1799*4882a593Smuzhiyun
1800*4882a593Smuzhiyun // Section 7.20
1801*4882a593Smuzhiyun goto update_refs;
1802*4882a593Smuzhiyun }
1803*4882a593Smuzhiyun
1804*4882a593Smuzhiyun fb(2, frame_type);
1805*4882a593Smuzhiyun frame_is_intra = (current->frame_type == AV1_FRAME_INTRA_ONLY ||
1806*4882a593Smuzhiyun current->frame_type == AV1_FRAME_KEY);
1807*4882a593Smuzhiyun
1808*4882a593Smuzhiyun ctx->frame_is_intra = frame_is_intra;
1809*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_KEY) {
1810*4882a593Smuzhiyun RK_U32 refresh_frame_flags = (1 << NUM_REF_FRAMES) - 1;
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun Av1GetCDFs(ctx, current->frame_to_show_map_idx);
1813*4882a593Smuzhiyun Av1StoreCDFs(ctx, refresh_frame_flags);
1814*4882a593Smuzhiyun }
1815*4882a593Smuzhiyun
1816*4882a593Smuzhiyun flag(show_frame);
1817*4882a593Smuzhiyun if (current->show_frame &&
1818*4882a593Smuzhiyun seq->decoder_model_info_present_flag &&
1819*4882a593Smuzhiyun !seq->timing_info.equal_picture_interval) {
1820*4882a593Smuzhiyun fb(seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1,
1821*4882a593Smuzhiyun frame_presentation_time);
1822*4882a593Smuzhiyun }
1823*4882a593Smuzhiyun if (current->show_frame)
1824*4882a593Smuzhiyun infer(showable_frame, current->frame_type != AV1_FRAME_KEY);
1825*4882a593Smuzhiyun else
1826*4882a593Smuzhiyun flag(showable_frame);
1827*4882a593Smuzhiyun
1828*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_SWITCH ||
1829*4882a593Smuzhiyun (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1830*4882a593Smuzhiyun infer(error_resilient_mode, 1);
1831*4882a593Smuzhiyun else
1832*4882a593Smuzhiyun flag(error_resilient_mode);
1833*4882a593Smuzhiyun }
1834*4882a593Smuzhiyun
1835*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_KEY && current->show_frame) {
1836*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1837*4882a593Smuzhiyun ctx->ref_s[i].valid = 0;
1838*4882a593Smuzhiyun ctx->ref_s[i].order_hint = 0;
1839*4882a593Smuzhiyun }
1840*4882a593Smuzhiyun }
1841*4882a593Smuzhiyun
1842*4882a593Smuzhiyun flag(disable_cdf_update);
1843*4882a593Smuzhiyun
1844*4882a593Smuzhiyun if (seq->seq_force_screen_content_tools ==
1845*4882a593Smuzhiyun AV1_SELECT_SCREEN_CONTENT_TOOLS) {
1846*4882a593Smuzhiyun flag(allow_screen_content_tools);
1847*4882a593Smuzhiyun } else {
1848*4882a593Smuzhiyun infer(allow_screen_content_tools,
1849*4882a593Smuzhiyun seq->seq_force_screen_content_tools);
1850*4882a593Smuzhiyun }
1851*4882a593Smuzhiyun if (current->allow_screen_content_tools) {
1852*4882a593Smuzhiyun if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1853*4882a593Smuzhiyun flag(force_integer_mv);
1854*4882a593Smuzhiyun else
1855*4882a593Smuzhiyun infer(force_integer_mv, seq->seq_force_integer_mv);
1856*4882a593Smuzhiyun } else {
1857*4882a593Smuzhiyun infer(force_integer_mv, 0);
1858*4882a593Smuzhiyun }
1859*4882a593Smuzhiyun
1860*4882a593Smuzhiyun if (seq->frame_id_numbers_present_flag) {
1861*4882a593Smuzhiyun fb(id_len, current_frame_id);
1862*4882a593Smuzhiyun
1863*4882a593Smuzhiyun diff_len = seq->delta_frame_id_length_minus_2 + 2;
1864*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1865*4882a593Smuzhiyun if (current->current_frame_id > (RK_S32)(1 << diff_len)) {
1866*4882a593Smuzhiyun if (ctx->ref_s[i].frame_id > current->current_frame_id ||
1867*4882a593Smuzhiyun ctx->ref_s[i].frame_id < (current->current_frame_id -
1868*4882a593Smuzhiyun (RK_S32)(1 << diff_len)))
1869*4882a593Smuzhiyun ctx->ref_s[i].valid = 0;
1870*4882a593Smuzhiyun } else {
1871*4882a593Smuzhiyun if (ctx->ref_s[i].frame_id > current->current_frame_id &&
1872*4882a593Smuzhiyun ctx->ref_s[i].frame_id < ((RK_S32)(1 << id_len) +
1873*4882a593Smuzhiyun current->current_frame_id -
1874*4882a593Smuzhiyun (RK_S32)(1 << diff_len)))
1875*4882a593Smuzhiyun ctx->ref_s[i].valid = 0;
1876*4882a593Smuzhiyun }
1877*4882a593Smuzhiyun }
1878*4882a593Smuzhiyun } else {
1879*4882a593Smuzhiyun infer(current_frame_id, 0);
1880*4882a593Smuzhiyun }
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_SWITCH)
1883*4882a593Smuzhiyun infer(frame_size_override_flag, 1);
1884*4882a593Smuzhiyun else if (seq->reduced_still_picture_header)
1885*4882a593Smuzhiyun infer(frame_size_override_flag, 0);
1886*4882a593Smuzhiyun else
1887*4882a593Smuzhiyun flag(frame_size_override_flag);
1888*4882a593Smuzhiyun
1889*4882a593Smuzhiyun order_hint_bits =
1890*4882a593Smuzhiyun seq->enable_order_hint ? seq->order_hint_bits_minus_1 + 1 : 0;
1891*4882a593Smuzhiyun if (order_hint_bits > 0)
1892*4882a593Smuzhiyun fb(order_hint_bits, order_hint);
1893*4882a593Smuzhiyun else
1894*4882a593Smuzhiyun infer(order_hint, 0);
1895*4882a593Smuzhiyun ctx->order_hint = current->order_hint;
1896*4882a593Smuzhiyun
1897*4882a593Smuzhiyun if (frame_is_intra || current->error_resilient_mode)
1898*4882a593Smuzhiyun infer(primary_ref_frame, AV1_PRIMARY_REF_NONE);
1899*4882a593Smuzhiyun else
1900*4882a593Smuzhiyun fb(3, primary_ref_frame);
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun if (seq->decoder_model_info_present_flag) {
1903*4882a593Smuzhiyun flag(buffer_removal_time_present_flag);
1904*4882a593Smuzhiyun if (current->buffer_removal_time_present_flag) {
1905*4882a593Smuzhiyun for (i = 0; i <= seq->operating_points_cnt_minus_1; i++) {
1906*4882a593Smuzhiyun if (seq->decoder_model_present_for_this_op[i]) {
1907*4882a593Smuzhiyun RK_S32 op_pt_idc = seq->operating_point_idc[i];
1908*4882a593Smuzhiyun RK_S32 in_temporal_layer = (op_pt_idc >> ctx->temporal_id ) & 1;
1909*4882a593Smuzhiyun RK_S32 in_spatial_layer = (op_pt_idc >> (ctx->spatial_id + 8)) & 1;
1910*4882a593Smuzhiyun if (seq->operating_point_idc[i] == 0 ||
1911*4882a593Smuzhiyun (in_temporal_layer && in_spatial_layer)) {
1912*4882a593Smuzhiyun fbs(seq->decoder_model_info.buffer_removal_time_length_minus_1 + 1,
1913*4882a593Smuzhiyun buffer_removal_time[i], 1, i);
1914*4882a593Smuzhiyun }
1915*4882a593Smuzhiyun }
1916*4882a593Smuzhiyun }
1917*4882a593Smuzhiyun }
1918*4882a593Smuzhiyun }
1919*4882a593Smuzhiyun
1920*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_SWITCH ||
1921*4882a593Smuzhiyun (current->frame_type == AV1_FRAME_KEY && current->show_frame))
1922*4882a593Smuzhiyun infer(refresh_frame_flags, all_frames);
1923*4882a593Smuzhiyun else
1924*4882a593Smuzhiyun fb(8, refresh_frame_flags);
1925*4882a593Smuzhiyun
1926*4882a593Smuzhiyun ctx->refresh_frame_flags = current->refresh_frame_flags;
1927*4882a593Smuzhiyun if (!frame_is_intra || current->refresh_frame_flags != all_frames) {
1928*4882a593Smuzhiyun if (seq->enable_order_hint) {
1929*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
1930*4882a593Smuzhiyun if (current->error_resilient_mode)
1931*4882a593Smuzhiyun fbs(order_hint_bits, ref_order_hint[i], 1, i);
1932*4882a593Smuzhiyun else
1933*4882a593Smuzhiyun infer(ref_order_hint[i], ctx->ref_s[i].order_hint);
1934*4882a593Smuzhiyun if (current->ref_order_hint[i] != ctx->ref_s[i].order_hint)
1935*4882a593Smuzhiyun ctx->ref_s[i].valid = 0;
1936*4882a593Smuzhiyun }
1937*4882a593Smuzhiyun }
1938*4882a593Smuzhiyun }
1939*4882a593Smuzhiyun
1940*4882a593Smuzhiyun if (current->frame_type == AV1_FRAME_KEY ||
1941*4882a593Smuzhiyun current->frame_type == AV1_FRAME_INTRA_ONLY) {
1942*4882a593Smuzhiyun CHECK(mpp_av1_frame_size(ctx, gb, current));
1943*4882a593Smuzhiyun CHECK(mpp_av1_render_size(ctx, gb, current));
1944*4882a593Smuzhiyun
1945*4882a593Smuzhiyun if (current->allow_screen_content_tools &&
1946*4882a593Smuzhiyun ctx->upscaled_width == ctx->frame_width)
1947*4882a593Smuzhiyun flag(allow_intrabc);
1948*4882a593Smuzhiyun else
1949*4882a593Smuzhiyun infer(allow_intrabc, 0);
1950*4882a593Smuzhiyun
1951*4882a593Smuzhiyun } else {
1952*4882a593Smuzhiyun if (!seq->enable_order_hint) {
1953*4882a593Smuzhiyun infer(frame_refs_short_signaling, 0);
1954*4882a593Smuzhiyun } else {
1955*4882a593Smuzhiyun flag(frame_refs_short_signaling);
1956*4882a593Smuzhiyun if (current->frame_refs_short_signaling) {
1957*4882a593Smuzhiyun fb(3, last_frame_idx);
1958*4882a593Smuzhiyun fb(3, golden_frame_idx);
1959*4882a593Smuzhiyun CHECK(mpp_av1_set_frame_refs(ctx, gb, current));
1960*4882a593Smuzhiyun }
1961*4882a593Smuzhiyun }
1962*4882a593Smuzhiyun
1963*4882a593Smuzhiyun for (i = 0; i < AV1_REFS_PER_FRAME; i++) {
1964*4882a593Smuzhiyun if (!current->frame_refs_short_signaling)
1965*4882a593Smuzhiyun fbs(3, ref_frame_idx[i], 1, i);
1966*4882a593Smuzhiyun if (seq->frame_id_numbers_present_flag) {
1967*4882a593Smuzhiyun fbs(seq->delta_frame_id_length_minus_2 + 2,
1968*4882a593Smuzhiyun delta_frame_id_minus1[i], 1, i);
1969*4882a593Smuzhiyun }
1970*4882a593Smuzhiyun }
1971*4882a593Smuzhiyun
1972*4882a593Smuzhiyun if (current->frame_size_override_flag &&
1973*4882a593Smuzhiyun !current->error_resilient_mode) {
1974*4882a593Smuzhiyun CHECK(mpp_av1_frame_size_with_refs(ctx, gb, current));
1975*4882a593Smuzhiyun } else {
1976*4882a593Smuzhiyun CHECK(mpp_av1_frame_size(ctx, gb, current));
1977*4882a593Smuzhiyun CHECK(mpp_av1_render_size(ctx, gb, current));
1978*4882a593Smuzhiyun }
1979*4882a593Smuzhiyun
1980*4882a593Smuzhiyun if (current->force_integer_mv)
1981*4882a593Smuzhiyun infer(allow_high_precision_mv, 0);
1982*4882a593Smuzhiyun else
1983*4882a593Smuzhiyun flag(allow_high_precision_mv);
1984*4882a593Smuzhiyun
1985*4882a593Smuzhiyun CHECK(mpp_av1_interpolation_filter(ctx, gb, current));
1986*4882a593Smuzhiyun
1987*4882a593Smuzhiyun flag(is_motion_mode_switchable);
1988*4882a593Smuzhiyun
1989*4882a593Smuzhiyun if (current->error_resilient_mode ||
1990*4882a593Smuzhiyun !seq->enable_ref_frame_mvs)
1991*4882a593Smuzhiyun infer(use_ref_frame_mvs, 0);
1992*4882a593Smuzhiyun else
1993*4882a593Smuzhiyun flag(use_ref_frame_mvs);
1994*4882a593Smuzhiyun
1995*4882a593Smuzhiyun infer(allow_intrabc, 0);
1996*4882a593Smuzhiyun }
1997*4882a593Smuzhiyun
1998*4882a593Smuzhiyun if (!frame_is_intra) {
1999*4882a593Smuzhiyun // Derive reference frame sign biases.
2000*4882a593Smuzhiyun }
2001*4882a593Smuzhiyun
2002*4882a593Smuzhiyun if (seq->reduced_still_picture_header || current->disable_cdf_update)
2003*4882a593Smuzhiyun infer(disable_frame_end_update_cdf, 1);
2004*4882a593Smuzhiyun else
2005*4882a593Smuzhiyun flag(disable_frame_end_update_cdf);
2006*4882a593Smuzhiyun
2007*4882a593Smuzhiyun ctx->disable_frame_end_update_cdf = current->disable_frame_end_update_cdf;
2008*4882a593Smuzhiyun
2009*4882a593Smuzhiyun if (current->use_ref_frame_mvs) {
2010*4882a593Smuzhiyun // Perform motion field estimation process.
2011*4882a593Smuzhiyun }
2012*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "ptile_info in %d", mpp_get_bits_count(gb));
2013*4882a593Smuzhiyun CHECK(mpp_av1_tile_info(ctx, gb, current));
2014*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "ptile_info out %d", mpp_get_bits_count(gb));
2015*4882a593Smuzhiyun
2016*4882a593Smuzhiyun CHECK(mpp_av1_quantization_params(ctx, gb, current));
2017*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "quantization out %d", mpp_get_bits_count(gb));
2018*4882a593Smuzhiyun
2019*4882a593Smuzhiyun CHECK(mpp_av1_segmentation_params(ctx, gb, current));
2020*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "segmentation out %d", mpp_get_bits_count(gb));
2021*4882a593Smuzhiyun
2022*4882a593Smuzhiyun CHECK(mpp_av1_delta_q_params(ctx, gb, current));
2023*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "delta_q out %d", mpp_get_bits_count(gb));
2024*4882a593Smuzhiyun
2025*4882a593Smuzhiyun CHECK(mpp_av1_delta_lf_params(ctx, gb, current));
2026*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "lf out %d", mpp_get_bits_count(gb));
2027*4882a593Smuzhiyun
2028*4882a593Smuzhiyun // Init coeff CDFs / load previous segments.
2029*4882a593Smuzhiyun if (current->error_resilient_mode || frame_is_intra || current->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
2030*4882a593Smuzhiyun // Init non-coeff CDFs.
2031*4882a593Smuzhiyun // Setup past independence.
2032*4882a593Smuzhiyun ctx->cdfs = &ctx->default_cdfs;
2033*4882a593Smuzhiyun ctx->cdfs_ndvc = &ctx->default_cdfs_ndvc;
2034*4882a593Smuzhiyun Av1DefaultCoeffProbs(current->base_q_idx, ctx->cdfs);
2035*4882a593Smuzhiyun } else {
2036*4882a593Smuzhiyun // Load CDF tables from previous frame.
2037*4882a593Smuzhiyun // Load params from previous frame.
2038*4882a593Smuzhiyun RK_U32 idx = current->ref_frame_idx[current->primary_ref_frame];
2039*4882a593Smuzhiyun
2040*4882a593Smuzhiyun Av1GetCDFs(ctx, idx);
2041*4882a593Smuzhiyun }
2042*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "show_existing_frame_index %d primary_ref_frame %d %d (%d) refresh_frame_flags %d base_q_idx %d\n",
2043*4882a593Smuzhiyun current->frame_to_show_map_idx,
2044*4882a593Smuzhiyun current->ref_frame_idx[current->primary_ref_frame],
2045*4882a593Smuzhiyun ctx->ref[current->ref_frame_idx[current->primary_ref_frame]].slot_index,
2046*4882a593Smuzhiyun current->primary_ref_frame,
2047*4882a593Smuzhiyun current->refresh_frame_flags,
2048*4882a593Smuzhiyun current->base_q_idx);
2049*4882a593Smuzhiyun Av1StoreCDFs(ctx, current->refresh_frame_flags);
2050*4882a593Smuzhiyun
2051*4882a593Smuzhiyun ctx->coded_lossless = 1;
2052*4882a593Smuzhiyun for (i = 0; i < AV1_MAX_SEGMENTS; i++) {
2053*4882a593Smuzhiyun RK_S32 qindex;
2054*4882a593Smuzhiyun if (current->feature_enabled[i][AV1_SEG_LVL_ALT_Q]) {
2055*4882a593Smuzhiyun qindex = (current->base_q_idx +
2056*4882a593Smuzhiyun current->feature_value[i][AV1_SEG_LVL_ALT_Q]);
2057*4882a593Smuzhiyun } else {
2058*4882a593Smuzhiyun qindex = current->base_q_idx;
2059*4882a593Smuzhiyun }
2060*4882a593Smuzhiyun qindex = mpp_clip_uintp2(qindex, 8);
2061*4882a593Smuzhiyun
2062*4882a593Smuzhiyun if (qindex || current->delta_q_y_dc ||
2063*4882a593Smuzhiyun current->delta_q_u_ac || current->delta_q_u_dc ||
2064*4882a593Smuzhiyun current->delta_q_v_ac || current->delta_q_v_dc) {
2065*4882a593Smuzhiyun ctx->coded_lossless = 0;
2066*4882a593Smuzhiyun }
2067*4882a593Smuzhiyun }
2068*4882a593Smuzhiyun ctx->all_lossless = ctx->coded_lossless &&
2069*4882a593Smuzhiyun ctx->frame_width == ctx->upscaled_width;
2070*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "filter in %d", mpp_get_bits_count(gb));
2071*4882a593Smuzhiyun
2072*4882a593Smuzhiyun CHECK(mpp_av1_loop_filter_params(ctx, gb, current));
2073*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "cdef in %d", mpp_get_bits_count(gb));
2074*4882a593Smuzhiyun
2075*4882a593Smuzhiyun CHECK(mpp_av1_cdef_params(ctx, gb, current));
2076*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "lr in %d", mpp_get_bits_count(gb));
2077*4882a593Smuzhiyun
2078*4882a593Smuzhiyun CHECK(mpp_av1_lr_params(ctx, gb, current));
2079*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "read_tx in %d", mpp_get_bits_count(gb));
2080*4882a593Smuzhiyun
2081*4882a593Smuzhiyun CHECK(mpp_av1_read_tx_mode(ctx, gb, current));
2082*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "reference in%d", mpp_get_bits_count(gb));
2083*4882a593Smuzhiyun
2084*4882a593Smuzhiyun CHECK(mpp_av1_frame_reference_mode(ctx, gb, current));
2085*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "kip_mode in %d", mpp_get_bits_count(gb));
2086*4882a593Smuzhiyun
2087*4882a593Smuzhiyun CHECK(mpp_av1_skip_mode_params(ctx, gb, current));
2088*4882a593Smuzhiyun
2089*4882a593Smuzhiyun if (frame_is_intra || current->error_resilient_mode ||
2090*4882a593Smuzhiyun !seq->enable_warped_motion)
2091*4882a593Smuzhiyun infer(allow_warped_motion, 0);
2092*4882a593Smuzhiyun else
2093*4882a593Smuzhiyun flag(allow_warped_motion);
2094*4882a593Smuzhiyun
2095*4882a593Smuzhiyun flag(reduced_tx_set);
2096*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "motion in%d", mpp_get_bits_count(gb));
2097*4882a593Smuzhiyun
2098*4882a593Smuzhiyun CHECK(mpp_av1_global_motion_params(ctx, gb, current));
2099*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "grain in %d", mpp_get_bits_count(gb));
2100*4882a593Smuzhiyun CHECK(mpp_av1_film_grain_params(ctx, gb, ¤t->film_grain, current));
2101*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "film_grain out %d", mpp_get_bits_count(gb));
2102*4882a593Smuzhiyun
2103*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_REF, "Frame %d: size %dx%d "
2104*4882a593Smuzhiyun "upscaled %d render %dx%d subsample %dx%d "
2105*4882a593Smuzhiyun "bitdepth %d tiles %dx%d.\n", ctx->order_hint,
2106*4882a593Smuzhiyun ctx->frame_width, ctx->frame_height, ctx->upscaled_width,
2107*4882a593Smuzhiyun ctx->render_width, ctx->render_height,
2108*4882a593Smuzhiyun seq->color_config.subsampling_x + 1,
2109*4882a593Smuzhiyun seq->color_config.subsampling_y + 1, ctx->bit_depth,
2110*4882a593Smuzhiyun ctx->tile_rows, ctx->tile_cols);
2111*4882a593Smuzhiyun
2112*4882a593Smuzhiyun update_refs:
2113*4882a593Smuzhiyun for (i = 0; i < AV1_NUM_REF_FRAMES; i++) {
2114*4882a593Smuzhiyun if (current->refresh_frame_flags & (1 << i)) {
2115*4882a593Smuzhiyun ctx->ref_s[i] = (AV1ReferenceFrameState) {
2116*4882a593Smuzhiyun .valid = 1,
2117*4882a593Smuzhiyun .frame_id = current->current_frame_id,
2118*4882a593Smuzhiyun .upscaled_width = ctx->upscaled_width,
2119*4882a593Smuzhiyun .frame_width = ctx->frame_width,
2120*4882a593Smuzhiyun .frame_height = ctx->frame_height,
2121*4882a593Smuzhiyun .render_width = ctx->render_width,
2122*4882a593Smuzhiyun .render_height = ctx->render_height,
2123*4882a593Smuzhiyun .frame_type = current->frame_type,
2124*4882a593Smuzhiyun .subsampling_x = seq->color_config.subsampling_x,
2125*4882a593Smuzhiyun .subsampling_y = seq->color_config.subsampling_y,
2126*4882a593Smuzhiyun .bit_depth = ctx->bit_depth,
2127*4882a593Smuzhiyun .order_hint = ctx->order_hint,
2128*4882a593Smuzhiyun };
2129*4882a593Smuzhiyun memcpy(ctx->ref_s[i].loop_filter_ref_deltas, current->loop_filter_ref_deltas,
2130*4882a593Smuzhiyun sizeof(current->loop_filter_ref_deltas));
2131*4882a593Smuzhiyun memcpy(ctx->ref_s[i].loop_filter_mode_deltas, current->loop_filter_mode_deltas,
2132*4882a593Smuzhiyun sizeof(current->loop_filter_mode_deltas));
2133*4882a593Smuzhiyun memcpy(ctx->ref_s[i].feature_enabled, current->feature_enabled,
2134*4882a593Smuzhiyun sizeof(current->feature_enabled));
2135*4882a593Smuzhiyun memcpy(ctx->ref_s[i].feature_value, current->feature_value,
2136*4882a593Smuzhiyun sizeof(current->feature_value));
2137*4882a593Smuzhiyun }
2138*4882a593Smuzhiyun }
2139*4882a593Smuzhiyun
2140*4882a593Smuzhiyun return 0;
2141*4882a593Smuzhiyun }
2142*4882a593Smuzhiyun
mpp_av1_frame_header_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrameHeader * current,RK_S32 redundant,void * rw_buffer_ref)2143*4882a593Smuzhiyun static RK_S32 mpp_av1_frame_header_obu(AV1Context *ctx, BitReadCtx_t *gb,
2144*4882a593Smuzhiyun AV1RawFrameHeader *current, RK_S32 redundant,
2145*4882a593Smuzhiyun void *rw_buffer_ref)
2146*4882a593Smuzhiyun {
2147*4882a593Smuzhiyun RK_S32 start_pos, fh_bits, fh_bytes, err;
2148*4882a593Smuzhiyun RK_U8 *fh_start;
2149*4882a593Smuzhiyun (void)rw_buffer_ref;
2150*4882a593Smuzhiyun if (ctx->seen_frame_header) {
2151*4882a593Smuzhiyun if (!redundant) {
2152*4882a593Smuzhiyun mpp_err_f("Invalid repeated "
2153*4882a593Smuzhiyun "frame header OBU.\n");
2154*4882a593Smuzhiyun return MPP_ERR_UNKNOW;
2155*4882a593Smuzhiyun } else {
2156*4882a593Smuzhiyun BitReadCtx_t fh;
2157*4882a593Smuzhiyun size_t i, b;
2158*4882a593Smuzhiyun RK_U32 val;
2159*4882a593Smuzhiyun
2160*4882a593Smuzhiyun // mpp_assert(ctx->frame_header_ref && ctx->frame_header);
2161*4882a593Smuzhiyun
2162*4882a593Smuzhiyun mpp_set_bitread_ctx(&fh, ctx->frame_header,
2163*4882a593Smuzhiyun ctx->frame_header_size);
2164*4882a593Smuzhiyun
2165*4882a593Smuzhiyun for (i = 0; i < ctx->frame_header_size; i += 8) {
2166*4882a593Smuzhiyun b = MPP_MIN(ctx->frame_header_size - i, 8);
2167*4882a593Smuzhiyun mpp_read_bits(&fh, b, (RK_S32*)&val);
2168*4882a593Smuzhiyun xf(b, frame_header_copy[i],
2169*4882a593Smuzhiyun val, val, val, 1, i / 8);
2170*4882a593Smuzhiyun }
2171*4882a593Smuzhiyun }
2172*4882a593Smuzhiyun } else {
2173*4882a593Smuzhiyun
2174*4882a593Smuzhiyun start_pos = mpp_get_bits_count(gb);
2175*4882a593Smuzhiyun
2176*4882a593Smuzhiyun CHECK(mpp_av1_uncompressed_header(ctx, gb, current));
2177*4882a593Smuzhiyun
2178*4882a593Smuzhiyun ctx->tile_num = 0;
2179*4882a593Smuzhiyun
2180*4882a593Smuzhiyun if (current->show_existing_frame) {
2181*4882a593Smuzhiyun ctx->seen_frame_header = 0;
2182*4882a593Smuzhiyun } else {
2183*4882a593Smuzhiyun ctx->seen_frame_header = 1;
2184*4882a593Smuzhiyun
2185*4882a593Smuzhiyun fh_bits = mpp_get_bits_count(gb) - start_pos;
2186*4882a593Smuzhiyun fh_start = (RK_U8*)gb->buf + start_pos / 8;
2187*4882a593Smuzhiyun
2188*4882a593Smuzhiyun fh_bytes = (fh_bits + 7) / 8;
2189*4882a593Smuzhiyun ctx->frame_header_size = fh_bits;
2190*4882a593Smuzhiyun MPP_FREE(ctx->frame_header);
2191*4882a593Smuzhiyun ctx->frame_header =
2192*4882a593Smuzhiyun mpp_malloc(RK_U8, fh_bytes + BUFFER_PADDING_SIZE);
2193*4882a593Smuzhiyun if (!ctx->frame_header) {
2194*4882a593Smuzhiyun mpp_err_f("frame header malloc failed\n");
2195*4882a593Smuzhiyun return MPP_ERR_NOMEM;
2196*4882a593Smuzhiyun }
2197*4882a593Smuzhiyun memcpy(ctx->frame_header, fh_start, fh_bytes);
2198*4882a593Smuzhiyun }
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun
2201*4882a593Smuzhiyun return 0;
2202*4882a593Smuzhiyun }
2203*4882a593Smuzhiyun
mpp_av1_tile_group_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawTileGroup * current)2204*4882a593Smuzhiyun static RK_S32 mpp_av1_tile_group_obu(AV1Context *ctx, BitReadCtx_t *gb,
2205*4882a593Smuzhiyun AV1RawTileGroup *current)
2206*4882a593Smuzhiyun {
2207*4882a593Smuzhiyun RK_S32 num_tiles, tile_bits;
2208*4882a593Smuzhiyun RK_S32 err;
2209*4882a593Smuzhiyun
2210*4882a593Smuzhiyun num_tiles = ctx->tile_cols * ctx->tile_rows;
2211*4882a593Smuzhiyun if (num_tiles > 1)
2212*4882a593Smuzhiyun flag(tile_start_and_end_present_flag);
2213*4882a593Smuzhiyun else
2214*4882a593Smuzhiyun infer(tile_start_and_end_present_flag, 0);
2215*4882a593Smuzhiyun
2216*4882a593Smuzhiyun if (num_tiles == 1 || !current->tile_start_and_end_present_flag) {
2217*4882a593Smuzhiyun infer(tg_start, 0);
2218*4882a593Smuzhiyun infer(tg_end, num_tiles - 1);
2219*4882a593Smuzhiyun } else {
2220*4882a593Smuzhiyun tile_bits = mpp_av1_tile_log2(1, ctx->tile_cols) +
2221*4882a593Smuzhiyun mpp_av1_tile_log2(1, ctx->tile_rows);
2222*4882a593Smuzhiyun fc(tile_bits, tg_start, ctx->tile_num, num_tiles - 1);
2223*4882a593Smuzhiyun fc(tile_bits, tg_end, current->tg_start, num_tiles - 1);
2224*4882a593Smuzhiyun }
2225*4882a593Smuzhiyun
2226*4882a593Smuzhiyun ctx->tile_num = current->tg_end + 1;
2227*4882a593Smuzhiyun
2228*4882a593Smuzhiyun CHECK(mpp_av1_byte_alignment(ctx, gb));
2229*4882a593Smuzhiyun
2230*4882a593Smuzhiyun // Reset header for next frame.
2231*4882a593Smuzhiyun if (current->tg_end == num_tiles - 1)
2232*4882a593Smuzhiyun ctx->seen_frame_header = 0;
2233*4882a593Smuzhiyun // Tile data follows.
2234*4882a593Smuzhiyun
2235*4882a593Smuzhiyun return 0;
2236*4882a593Smuzhiyun }
2237*4882a593Smuzhiyun
mpp_av1_frame_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawFrame * current,void * rw_buffer_ref)2238*4882a593Smuzhiyun static RK_S32 mpp_av1_frame_obu(AV1Context *ctx, BitReadCtx_t *gb,
2239*4882a593Smuzhiyun AV1RawFrame *current,
2240*4882a593Smuzhiyun void *rw_buffer_ref)
2241*4882a593Smuzhiyun {
2242*4882a593Smuzhiyun RK_S32 err;
2243*4882a593Smuzhiyun RK_U32 start_pos = mpp_get_bits_count(gb);
2244*4882a593Smuzhiyun
2245*4882a593Smuzhiyun CHECK(mpp_av1_frame_header_obu(ctx, gb, ¤t->header,
2246*4882a593Smuzhiyun 0, rw_buffer_ref));
2247*4882a593Smuzhiyun
2248*4882a593Smuzhiyun CHECK(mpp_av1_byte_alignment(ctx, gb));
2249*4882a593Smuzhiyun
2250*4882a593Smuzhiyun CHECK(mpp_av1_tile_group_obu(ctx, gb, ¤t->tile_group));
2251*4882a593Smuzhiyun ctx->frame_tag_size += (mpp_get_bits_count(gb) - start_pos + 7) >> 3;
2252*4882a593Smuzhiyun
2253*4882a593Smuzhiyun return 0;
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun
mpp_av1_tile_list_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawTileList * current)2256*4882a593Smuzhiyun static RK_S32 mpp_av1_tile_list_obu(AV1Context *ctx, BitReadCtx_t *gb,
2257*4882a593Smuzhiyun AV1RawTileList *current)
2258*4882a593Smuzhiyun {
2259*4882a593Smuzhiyun RK_S32 err;
2260*4882a593Smuzhiyun (void)ctx;
2261*4882a593Smuzhiyun fb(8, output_frame_width_in_tiles_minus_1);
2262*4882a593Smuzhiyun fb(8, output_frame_height_in_tiles_minus_1);
2263*4882a593Smuzhiyun
2264*4882a593Smuzhiyun fb(16, tile_count_minus_1);
2265*4882a593Smuzhiyun
2266*4882a593Smuzhiyun // Tile data follows.
2267*4882a593Smuzhiyun
2268*4882a593Smuzhiyun return 0;
2269*4882a593Smuzhiyun }
2270*4882a593Smuzhiyun
mpp_av1_metadata_hdr_cll(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadataHDRCLL * current)2271*4882a593Smuzhiyun static RK_S32 mpp_av1_metadata_hdr_cll(AV1Context *ctx, BitReadCtx_t *gb,
2272*4882a593Smuzhiyun AV1RawMetadataHDRCLL *current)
2273*4882a593Smuzhiyun {
2274*4882a593Smuzhiyun RK_S32 err;
2275*4882a593Smuzhiyun (void)ctx;
2276*4882a593Smuzhiyun fb(16, max_cll);
2277*4882a593Smuzhiyun fb(16, max_fall);
2278*4882a593Smuzhiyun
2279*4882a593Smuzhiyun return 0;
2280*4882a593Smuzhiyun }
2281*4882a593Smuzhiyun
mpp_av1_metadata_hdr_mdcv(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadataHDRMDCV * current)2282*4882a593Smuzhiyun static RK_S32 mpp_av1_metadata_hdr_mdcv(AV1Context *ctx, BitReadCtx_t *gb,
2283*4882a593Smuzhiyun AV1RawMetadataHDRMDCV *current)
2284*4882a593Smuzhiyun {
2285*4882a593Smuzhiyun RK_S32 err, i;
2286*4882a593Smuzhiyun (void)ctx;
2287*4882a593Smuzhiyun for (i = 0; i < 3; i++) {
2288*4882a593Smuzhiyun fbs(16, primary_chromaticity_x[i], 1, i);
2289*4882a593Smuzhiyun fbs(16, primary_chromaticity_y[i], 1, i);
2290*4882a593Smuzhiyun }
2291*4882a593Smuzhiyun
2292*4882a593Smuzhiyun fb(16, white_point_chromaticity_x);
2293*4882a593Smuzhiyun fb(16, white_point_chromaticity_y);
2294*4882a593Smuzhiyun
2295*4882a593Smuzhiyun fc(32, luminance_max, 1, MAX_UINT_BITS(32));
2296*4882a593Smuzhiyun // luminance_min must be lower than luminance_max. Convert luminance_max from
2297*4882a593Smuzhiyun // 24.8 fixed point to 18.14 fixed point in order to compare them.
2298*4882a593Smuzhiyun fc(32, luminance_min, 0, MPP_MIN(((RK_U64)current->luminance_max << 6) - 1,
2299*4882a593Smuzhiyun MAX_UINT_BITS(32)));
2300*4882a593Smuzhiyun
2301*4882a593Smuzhiyun return 0;
2302*4882a593Smuzhiyun }
2303*4882a593Smuzhiyun
mpp_av1_scalability_structure(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadataScalability * current)2304*4882a593Smuzhiyun static RK_S32 mpp_av1_scalability_structure(AV1Context *ctx, BitReadCtx_t *gb,
2305*4882a593Smuzhiyun AV1RawMetadataScalability *current)
2306*4882a593Smuzhiyun {
2307*4882a593Smuzhiyun const AV1RawSequenceHeader *seq;
2308*4882a593Smuzhiyun RK_S32 err, i, j;
2309*4882a593Smuzhiyun
2310*4882a593Smuzhiyun if (!ctx->sequence_header) {
2311*4882a593Smuzhiyun mpp_err_f("No sequence header available: "
2312*4882a593Smuzhiyun "unable to parse scalability metadata.\n");
2313*4882a593Smuzhiyun return MPP_ERR_UNKNOW;
2314*4882a593Smuzhiyun }
2315*4882a593Smuzhiyun seq = ctx->sequence_header;
2316*4882a593Smuzhiyun
2317*4882a593Smuzhiyun fb(2, spatial_layers_cnt_minus_1);
2318*4882a593Smuzhiyun flag(spatial_layer_dimensions_present_flag);
2319*4882a593Smuzhiyun flag(spatial_layer_description_present_flag);
2320*4882a593Smuzhiyun flag(temporal_group_description_present_flag);
2321*4882a593Smuzhiyun fc(3, scalability_structure_reserved_3bits, 0, 0);
2322*4882a593Smuzhiyun if (current->spatial_layer_dimensions_present_flag) {
2323*4882a593Smuzhiyun for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++) {
2324*4882a593Smuzhiyun fcs(16, spatial_layer_max_width[i],
2325*4882a593Smuzhiyun 0, seq->max_frame_width_minus_1 + 1, 1, i);
2326*4882a593Smuzhiyun fcs(16, spatial_layer_max_height[i],
2327*4882a593Smuzhiyun 0, seq->max_frame_height_minus_1 + 1, 1, i);
2328*4882a593Smuzhiyun }
2329*4882a593Smuzhiyun }
2330*4882a593Smuzhiyun if (current->spatial_layer_description_present_flag) {
2331*4882a593Smuzhiyun for (i = 0; i <= current->spatial_layers_cnt_minus_1; i++)
2332*4882a593Smuzhiyun fbs(8, spatial_layer_ref_id[i], 1, i);
2333*4882a593Smuzhiyun }
2334*4882a593Smuzhiyun if (current->temporal_group_description_present_flag) {
2335*4882a593Smuzhiyun fb(8, temporal_group_size);
2336*4882a593Smuzhiyun for (i = 0; i < current->temporal_group_size; i++) {
2337*4882a593Smuzhiyun fbs(3, temporal_group_temporal_id[i], 1, i);
2338*4882a593Smuzhiyun flags(temporal_group_temporal_switching_up_point_flag[i], 1, i);
2339*4882a593Smuzhiyun flags(temporal_group_spatial_switching_up_point_flag[i], 1, i);
2340*4882a593Smuzhiyun fbs(3, temporal_group_ref_cnt[i], 1, i);
2341*4882a593Smuzhiyun for (j = 0; j < current->temporal_group_ref_cnt[i]; j++) {
2342*4882a593Smuzhiyun fbs(8, temporal_group_ref_pic_diff[i][j], 2, i, j);
2343*4882a593Smuzhiyun }
2344*4882a593Smuzhiyun }
2345*4882a593Smuzhiyun }
2346*4882a593Smuzhiyun
2347*4882a593Smuzhiyun return 0;
2348*4882a593Smuzhiyun }
2349*4882a593Smuzhiyun
mpp_av1_metadata_scalability(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadataScalability * current)2350*4882a593Smuzhiyun static RK_S32 mpp_av1_metadata_scalability(AV1Context *ctx, BitReadCtx_t *gb,
2351*4882a593Smuzhiyun AV1RawMetadataScalability *current)
2352*4882a593Smuzhiyun {
2353*4882a593Smuzhiyun RK_S32 err;
2354*4882a593Smuzhiyun
2355*4882a593Smuzhiyun fb(8, scalability_mode_idc);
2356*4882a593Smuzhiyun
2357*4882a593Smuzhiyun if (current->scalability_mode_idc == AV1_SCALABILITY_SS)
2358*4882a593Smuzhiyun CHECK(mpp_av1_scalability_structure(ctx, gb, current));
2359*4882a593Smuzhiyun
2360*4882a593Smuzhiyun return 0;
2361*4882a593Smuzhiyun }
2362*4882a593Smuzhiyun
mpp_av1_get_dolby_rpu(AV1Context * ctx,BitReadCtx_t * gb)2363*4882a593Smuzhiyun static RK_S32 mpp_av1_get_dolby_rpu(AV1Context *ctx, BitReadCtx_t *gb)
2364*4882a593Smuzhiyun {
2365*4882a593Smuzhiyun MppFrameHdrDynamicMeta *hdr_dynamic_meta = ctx->hdr_dynamic_meta;
2366*4882a593Smuzhiyun RK_U32 emdf_payload_size = 0;
2367*4882a593Smuzhiyun
2368*4882a593Smuzhiyun /* skip emdf_container{} */
2369*4882a593Smuzhiyun SKIP_BITS(gb, 3);
2370*4882a593Smuzhiyun SKIP_BITS(gb, 2);
2371*4882a593Smuzhiyun SKIP_BITS(gb, 5);
2372*4882a593Smuzhiyun SKIP_BITS(gb, 5);
2373*4882a593Smuzhiyun SKIP_BITS(gb, 1);
2374*4882a593Smuzhiyun SKIP_BITS(gb, 5);
2375*4882a593Smuzhiyun SKIP_BITS(gb, 1);
2376*4882a593Smuzhiyun /* skip emdf_payload_config{} */
2377*4882a593Smuzhiyun SKIP_BITS(gb, 5);
2378*4882a593Smuzhiyun
2379*4882a593Smuzhiyun /* get payload size */
2380*4882a593Smuzhiyun #define VARIABLE_BITS8(gb, value) \
2381*4882a593Smuzhiyun for (;;) { \
2382*4882a593Smuzhiyun RK_U32 tmp, flag; \
2383*4882a593Smuzhiyun \
2384*4882a593Smuzhiyun READ_BITS(gb, 8, &tmp); \
2385*4882a593Smuzhiyun value += tmp; \
2386*4882a593Smuzhiyun READ_ONEBIT(gb, &flag); \
2387*4882a593Smuzhiyun if (!flag) break; \
2388*4882a593Smuzhiyun value <<= 8; \
2389*4882a593Smuzhiyun value += (1 << 8); \
2390*4882a593Smuzhiyun }
2391*4882a593Smuzhiyun
2392*4882a593Smuzhiyun VARIABLE_BITS8(gb, emdf_payload_size);
2393*4882a593Smuzhiyun if (!hdr_dynamic_meta) {
2394*4882a593Smuzhiyun hdr_dynamic_meta = mpp_calloc_size(MppFrameHdrDynamicMeta,
2395*4882a593Smuzhiyun sizeof(MppFrameHdrDynamicMeta) + SZ_1K);
2396*4882a593Smuzhiyun if (!hdr_dynamic_meta) {
2397*4882a593Smuzhiyun mpp_err_f("malloc hdr dynamic data failed!\n");
2398*4882a593Smuzhiyun return MPP_ERR_NOMEM;
2399*4882a593Smuzhiyun }
2400*4882a593Smuzhiyun }
2401*4882a593Smuzhiyun
2402*4882a593Smuzhiyun RK_U32 i;
2403*4882a593Smuzhiyun MppWriteCtx bit_ctx;
2404*4882a593Smuzhiyun
2405*4882a593Smuzhiyun mpp_writer_init(&bit_ctx, hdr_dynamic_meta->data, SZ_1K);
2406*4882a593Smuzhiyun
2407*4882a593Smuzhiyun mpp_writer_put_raw_bits(&bit_ctx, 0, 24);
2408*4882a593Smuzhiyun mpp_writer_put_raw_bits(&bit_ctx, 1, 8);
2409*4882a593Smuzhiyun mpp_writer_put_raw_bits(&bit_ctx, 0x19, 8);
2410*4882a593Smuzhiyun for (i = 0; i < emdf_payload_size; i++) {
2411*4882a593Smuzhiyun RK_U8 data;
2412*4882a593Smuzhiyun
2413*4882a593Smuzhiyun READ_BITS(gb, 8, &data);
2414*4882a593Smuzhiyun mpp_writer_put_bits(&bit_ctx, data, 8);
2415*4882a593Smuzhiyun }
2416*4882a593Smuzhiyun
2417*4882a593Smuzhiyun hdr_dynamic_meta->size = mpp_writer_bytes(&bit_ctx);
2418*4882a593Smuzhiyun hdr_dynamic_meta->hdr_fmt = DOLBY;
2419*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN, "dolby rpu size %d -> %d\n",
2420*4882a593Smuzhiyun emdf_payload_size, hdr_dynamic_meta->size);
2421*4882a593Smuzhiyun
2422*4882a593Smuzhiyun ctx->hdr_dynamic_meta = hdr_dynamic_meta;
2423*4882a593Smuzhiyun ctx->hdr_dynamic = 1;
2424*4882a593Smuzhiyun ctx->is_hdr = 1;
2425*4882a593Smuzhiyun
2426*4882a593Smuzhiyun if (av1d_debug & AV1D_DBG_DUMP_RPU) {
2427*4882a593Smuzhiyun RK_U8 *p = hdr_dynamic_meta->data;
2428*4882a593Smuzhiyun char fname[128];
2429*4882a593Smuzhiyun FILE *fp_in = NULL;
2430*4882a593Smuzhiyun static RK_U32 g_frame_no = 0;
2431*4882a593Smuzhiyun
2432*4882a593Smuzhiyun sprintf(fname, "/data/video/meta_%d.txt", g_frame_no++);
2433*4882a593Smuzhiyun fp_in = fopen(fname, "wb");
2434*4882a593Smuzhiyun mpp_err("open %s %p\n", fname, fp_in);
2435*4882a593Smuzhiyun if (fp_in)
2436*4882a593Smuzhiyun fwrite(p, 1, hdr_dynamic_meta->size, fp_in);
2437*4882a593Smuzhiyun fflush(fp_in);
2438*4882a593Smuzhiyun fclose(fp_in);
2439*4882a593Smuzhiyun }
2440*4882a593Smuzhiyun
2441*4882a593Smuzhiyun return 0;
2442*4882a593Smuzhiyun
2443*4882a593Smuzhiyun __BITREAD_ERR:
2444*4882a593Smuzhiyun return MPP_ERR_STREAM;
2445*4882a593Smuzhiyun }
2446*4882a593Smuzhiyun
mpp_av1_metadata_itut_t35(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadataITUTT35 * current)2447*4882a593Smuzhiyun static RK_S32 mpp_av1_metadata_itut_t35(AV1Context *ctx, BitReadCtx_t *gb,
2448*4882a593Smuzhiyun AV1RawMetadataITUTT35 *current)
2449*4882a593Smuzhiyun {
2450*4882a593Smuzhiyun RK_S32 err;
2451*4882a593Smuzhiyun
2452*4882a593Smuzhiyun fb(8, itu_t_t35_country_code);
2453*4882a593Smuzhiyun if (current->itu_t_t35_country_code == 0xff)
2454*4882a593Smuzhiyun fb(8, itu_t_t35_country_code_extension_byte);
2455*4882a593Smuzhiyun
2456*4882a593Smuzhiyun current->payload_size = mpp_get_bits_left(gb) / 8 - 1;
2457*4882a593Smuzhiyun
2458*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN, "%s itu_t_t35_country_code %d payload_size %d\n",
2459*4882a593Smuzhiyun __func__, current->itu_t_t35_country_code, current->payload_size);
2460*4882a593Smuzhiyun
2461*4882a593Smuzhiyun fb(16, itu_t_t35_terminal_provider_code);
2462*4882a593Smuzhiyun READ_BITS_LONG(gb, 32, ¤t->itu_t_t35_terminal_provider_oriented_code);
2463*4882a593Smuzhiyun
2464*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN, "itu_t_t35_country_code 0x%x\n",
2465*4882a593Smuzhiyun current->itu_t_t35_country_code);
2466*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN, "itu_t_t35_terminal_provider_code 0x%x\n",
2467*4882a593Smuzhiyun current->itu_t_t35_terminal_provider_code);
2468*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN, "itu_t_t35_terminal_provider_oriented_code 0x%x\n",
2469*4882a593Smuzhiyun current->itu_t_t35_terminal_provider_oriented_code);
2470*4882a593Smuzhiyun
2471*4882a593Smuzhiyun if (current->itu_t_t35_terminal_provider_code == 0x3B &&
2472*4882a593Smuzhiyun current->itu_t_t35_terminal_provider_oriented_code == 0x800)
2473*4882a593Smuzhiyun mpp_av1_get_dolby_rpu(ctx, gb);
2474*4882a593Smuzhiyun
2475*4882a593Smuzhiyun return 0;
2476*4882a593Smuzhiyun __BITREAD_ERR:
2477*4882a593Smuzhiyun return 0;
2478*4882a593Smuzhiyun }
2479*4882a593Smuzhiyun
mpp_av1_metadata_timecode(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadataTimecode * current)2480*4882a593Smuzhiyun static RK_S32 mpp_av1_metadata_timecode(AV1Context *ctx, BitReadCtx_t *gb,
2481*4882a593Smuzhiyun AV1RawMetadataTimecode *current)
2482*4882a593Smuzhiyun {
2483*4882a593Smuzhiyun RK_S32 err;
2484*4882a593Smuzhiyun (void)ctx;
2485*4882a593Smuzhiyun
2486*4882a593Smuzhiyun fb(5, counting_type);
2487*4882a593Smuzhiyun flag(full_timestamp_flag);
2488*4882a593Smuzhiyun flag(discontinuity_flag);
2489*4882a593Smuzhiyun flag(cnt_dropped_flag);
2490*4882a593Smuzhiyun fb(9, n_frames);
2491*4882a593Smuzhiyun
2492*4882a593Smuzhiyun if (current->full_timestamp_flag) {
2493*4882a593Smuzhiyun fc(6, seconds_value, 0, 59);
2494*4882a593Smuzhiyun fc(6, minutes_value, 0, 59);
2495*4882a593Smuzhiyun fc(5, hours_value, 0, 23);
2496*4882a593Smuzhiyun } else {
2497*4882a593Smuzhiyun flag(seconds_flag);
2498*4882a593Smuzhiyun if (current->seconds_flag) {
2499*4882a593Smuzhiyun fc(6, seconds_value, 0, 59);
2500*4882a593Smuzhiyun flag(minutes_flag);
2501*4882a593Smuzhiyun if (current->minutes_flag) {
2502*4882a593Smuzhiyun fc(6, minutes_value, 0, 59);
2503*4882a593Smuzhiyun flag(hours_flag);
2504*4882a593Smuzhiyun if (current->hours_flag)
2505*4882a593Smuzhiyun fc(5, hours_value, 0, 23);
2506*4882a593Smuzhiyun }
2507*4882a593Smuzhiyun }
2508*4882a593Smuzhiyun }
2509*4882a593Smuzhiyun
2510*4882a593Smuzhiyun fb(5, time_offset_length);
2511*4882a593Smuzhiyun if (current->time_offset_length > 0)
2512*4882a593Smuzhiyun fb(current->time_offset_length, time_offset_value);
2513*4882a593Smuzhiyun else
2514*4882a593Smuzhiyun infer(time_offset_length, 0);
2515*4882a593Smuzhiyun
2516*4882a593Smuzhiyun return 0;
2517*4882a593Smuzhiyun }
2518*4882a593Smuzhiyun
mpp_av1_metadata_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawMetadata * current)2519*4882a593Smuzhiyun static RK_S32 mpp_av1_metadata_obu(AV1Context *ctx, BitReadCtx_t *gb,
2520*4882a593Smuzhiyun AV1RawMetadata *current)
2521*4882a593Smuzhiyun {
2522*4882a593Smuzhiyun RK_S32 err;
2523*4882a593Smuzhiyun
2524*4882a593Smuzhiyun leb128(metadata_type);
2525*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN, "%s meta type %d\n", __func__, current->metadata_type);
2526*4882a593Smuzhiyun switch (current->metadata_type) {
2527*4882a593Smuzhiyun case AV1_METADATA_TYPE_HDR_CLL:
2528*4882a593Smuzhiyun CHECK(mpp_av1_metadata_hdr_cll(ctx, gb, ¤t->metadata.hdr_cll));
2529*4882a593Smuzhiyun break;
2530*4882a593Smuzhiyun case AV1_METADATA_TYPE_HDR_MDCV:
2531*4882a593Smuzhiyun CHECK(mpp_av1_metadata_hdr_mdcv(ctx, gb, ¤t->metadata.hdr_mdcv));
2532*4882a593Smuzhiyun break;
2533*4882a593Smuzhiyun case AV1_METADATA_TYPE_SCALABILITY:
2534*4882a593Smuzhiyun CHECK(mpp_av1_metadata_scalability(ctx, gb, ¤t->metadata.scalability));
2535*4882a593Smuzhiyun break;
2536*4882a593Smuzhiyun case AV1_METADATA_TYPE_ITUT_T35:
2537*4882a593Smuzhiyun CHECK(mpp_av1_metadata_itut_t35(ctx, gb, ¤t->metadata.itut_t35));
2538*4882a593Smuzhiyun break;
2539*4882a593Smuzhiyun case AV1_METADATA_TYPE_TIMECODE:
2540*4882a593Smuzhiyun CHECK(mpp_av1_metadata_timecode(ctx, gb, ¤t->metadata.timecode));
2541*4882a593Smuzhiyun break;
2542*4882a593Smuzhiyun default:
2543*4882a593Smuzhiyun // Unknown metadata type.
2544*4882a593Smuzhiyun return MPP_OK;
2545*4882a593Smuzhiyun }
2546*4882a593Smuzhiyun
2547*4882a593Smuzhiyun return 0;
2548*4882a593Smuzhiyun }
2549*4882a593Smuzhiyun
mpp_av1_padding_obu(AV1Context * ctx,BitReadCtx_t * gb,AV1RawPadding * current)2550*4882a593Smuzhiyun static RK_S32 mpp_av1_padding_obu(AV1Context *ctx, BitReadCtx_t *gb,
2551*4882a593Smuzhiyun AV1RawPadding *current)
2552*4882a593Smuzhiyun {
2553*4882a593Smuzhiyun RK_S32 err;
2554*4882a593Smuzhiyun RK_U32 i;
2555*4882a593Smuzhiyun (void)ctx;
2556*4882a593Smuzhiyun current->payload_size = mpp_av1_get_payload_bytes_left(gb);
2557*4882a593Smuzhiyun
2558*4882a593Smuzhiyun current->payload = mpp_malloc(RK_U8, current->payload_size);
2559*4882a593Smuzhiyun if (!current->payload )
2560*4882a593Smuzhiyun return MPP_ERR_NOMEM;
2561*4882a593Smuzhiyun
2562*4882a593Smuzhiyun for (i = 0; i < current->payload_size; i++)
2563*4882a593Smuzhiyun xf(8, obu_padding_byte[i], current->payload[i], 0x00, 0xff, 1, i);
2564*4882a593Smuzhiyun
2565*4882a593Smuzhiyun return 0;
2566*4882a593Smuzhiyun }
2567*4882a593Smuzhiyun
2568*4882a593Smuzhiyun
2569*4882a593Smuzhiyun
mpp_insert_unit(Av1UnitFragment * frag,RK_S32 position)2570*4882a593Smuzhiyun static MPP_RET mpp_insert_unit(Av1UnitFragment *frag, RK_S32 position)
2571*4882a593Smuzhiyun {
2572*4882a593Smuzhiyun Av1ObuUnit *units;
2573*4882a593Smuzhiyun
2574*4882a593Smuzhiyun if (frag->nb_units < frag->nb_units_allocated) {
2575*4882a593Smuzhiyun units = frag->units;
2576*4882a593Smuzhiyun
2577*4882a593Smuzhiyun if (position < frag->nb_units)
2578*4882a593Smuzhiyun memmove(units + position + 1, units + position,
2579*4882a593Smuzhiyun (frag->nb_units - position) * sizeof(*units));
2580*4882a593Smuzhiyun } else {
2581*4882a593Smuzhiyun units = mpp_malloc(Av1ObuUnit, frag->nb_units * 2 + 1);
2582*4882a593Smuzhiyun if (!units)
2583*4882a593Smuzhiyun return MPP_ERR_NOMEM;
2584*4882a593Smuzhiyun
2585*4882a593Smuzhiyun frag->nb_units_allocated = 2 * frag->nb_units_allocated + 1;
2586*4882a593Smuzhiyun
2587*4882a593Smuzhiyun if (position > 0)
2588*4882a593Smuzhiyun memcpy(units, frag->units, position * sizeof(*units));
2589*4882a593Smuzhiyun
2590*4882a593Smuzhiyun if (position < frag->nb_units)
2591*4882a593Smuzhiyun memcpy(units + position + 1, frag->units + position,
2592*4882a593Smuzhiyun (frag->nb_units - position) * sizeof(*units));
2593*4882a593Smuzhiyun }
2594*4882a593Smuzhiyun
2595*4882a593Smuzhiyun memset(units + position, 0, sizeof(*units));
2596*4882a593Smuzhiyun
2597*4882a593Smuzhiyun if (units != frag->units) {
2598*4882a593Smuzhiyun mpp_free(frag->units);
2599*4882a593Smuzhiyun frag->units = units;
2600*4882a593Smuzhiyun }
2601*4882a593Smuzhiyun
2602*4882a593Smuzhiyun ++frag->nb_units;
2603*4882a593Smuzhiyun
2604*4882a593Smuzhiyun return MPP_OK;
2605*4882a593Smuzhiyun }
2606*4882a593Smuzhiyun
mpp_insert_unit_data(Av1UnitFragment * frag,RK_S32 position,Av1UnitType type,RK_U8 * data,size_t data_size)2607*4882a593Smuzhiyun static MPP_RET mpp_insert_unit_data(Av1UnitFragment *frag,
2608*4882a593Smuzhiyun RK_S32 position,
2609*4882a593Smuzhiyun Av1UnitType type,
2610*4882a593Smuzhiyun RK_U8 *data, size_t data_size)
2611*4882a593Smuzhiyun {
2612*4882a593Smuzhiyun Av1ObuUnit *unit;
2613*4882a593Smuzhiyun MPP_RET ret;
2614*4882a593Smuzhiyun
2615*4882a593Smuzhiyun if (position == -1)
2616*4882a593Smuzhiyun position = frag->nb_units;
2617*4882a593Smuzhiyun
2618*4882a593Smuzhiyun mpp_assert(position >= 0 && position <= frag->nb_units);
2619*4882a593Smuzhiyun ret = mpp_insert_unit(frag, position);
2620*4882a593Smuzhiyun if (ret < 0) {
2621*4882a593Smuzhiyun return ret;
2622*4882a593Smuzhiyun }
2623*4882a593Smuzhiyun
2624*4882a593Smuzhiyun unit = &frag->units[position];
2625*4882a593Smuzhiyun unit->type = type;
2626*4882a593Smuzhiyun unit->data = data;
2627*4882a593Smuzhiyun unit->data_size = data_size;
2628*4882a593Smuzhiyun
2629*4882a593Smuzhiyun return MPP_OK;
2630*4882a593Smuzhiyun }
2631*4882a593Smuzhiyun
mpp_av1_split_fragment(AV1Context * ctx,Av1UnitFragment * frag,RK_S32 header_flag)2632*4882a593Smuzhiyun RK_S32 mpp_av1_split_fragment(AV1Context *ctx, Av1UnitFragment *frag, RK_S32 header_flag)
2633*4882a593Smuzhiyun {
2634*4882a593Smuzhiyun BitReadCtx_t gbc;
2635*4882a593Smuzhiyun RK_U8 *data;
2636*4882a593Smuzhiyun size_t size;
2637*4882a593Smuzhiyun RK_U64 obu_length;
2638*4882a593Smuzhiyun RK_S32 pos, err;
2639*4882a593Smuzhiyun
2640*4882a593Smuzhiyun data = frag->data;
2641*4882a593Smuzhiyun size = frag->data_size;
2642*4882a593Smuzhiyun
2643*4882a593Smuzhiyun if (INT_MAX / 8 < size) {
2644*4882a593Smuzhiyun mpp_err( "Invalid fragment: "
2645*4882a593Smuzhiyun "too large (%d bytes).\n", size);
2646*4882a593Smuzhiyun err = MPP_NOK;
2647*4882a593Smuzhiyun goto fail;
2648*4882a593Smuzhiyun }
2649*4882a593Smuzhiyun
2650*4882a593Smuzhiyun if (header_flag && size && data[0] & 0x80) {
2651*4882a593Smuzhiyun // first bit is nonzero, the extradata does not consist purely of
2652*4882a593Smuzhiyun // OBUs. Expect MP4/Matroska AV1CodecConfigurationRecord
2653*4882a593Smuzhiyun RK_S32 config_record_version = data[0] & 0x7f;
2654*4882a593Smuzhiyun
2655*4882a593Smuzhiyun if (config_record_version != 1) {
2656*4882a593Smuzhiyun mpp_err(
2657*4882a593Smuzhiyun "Unknown version %d of AV1CodecConfigurationRecord "
2658*4882a593Smuzhiyun "found!\n",
2659*4882a593Smuzhiyun config_record_version);
2660*4882a593Smuzhiyun err = MPP_NOK;
2661*4882a593Smuzhiyun goto fail;
2662*4882a593Smuzhiyun }
2663*4882a593Smuzhiyun
2664*4882a593Smuzhiyun if (size <= 4) {
2665*4882a593Smuzhiyun if (size < 4) {
2666*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_STRMIN,
2667*4882a593Smuzhiyun "Undersized AV1CodecConfigurationRecord v%d found!\n",
2668*4882a593Smuzhiyun config_record_version);
2669*4882a593Smuzhiyun err = MPP_NOK;
2670*4882a593Smuzhiyun goto fail;
2671*4882a593Smuzhiyun }
2672*4882a593Smuzhiyun
2673*4882a593Smuzhiyun goto success;
2674*4882a593Smuzhiyun }
2675*4882a593Smuzhiyun
2676*4882a593Smuzhiyun // In AV1CodecConfigurationRecord v1, actual OBUs start after
2677*4882a593Smuzhiyun // four bytes. Thus set the offset as required for properly
2678*4882a593Smuzhiyun // parsing them.
2679*4882a593Smuzhiyun data += 4;
2680*4882a593Smuzhiyun size -= 4;
2681*4882a593Smuzhiyun }
2682*4882a593Smuzhiyun
2683*4882a593Smuzhiyun while (size > 0) {
2684*4882a593Smuzhiyun AV1RawOBUHeader header;
2685*4882a593Smuzhiyun RK_U64 obu_size = 0;
2686*4882a593Smuzhiyun
2687*4882a593Smuzhiyun mpp_set_bitread_ctx(&gbc, data, size);
2688*4882a593Smuzhiyun
2689*4882a593Smuzhiyun err = mpp_av1_read_obu_header(ctx, &gbc, &header);
2690*4882a593Smuzhiyun if (err < 0)
2691*4882a593Smuzhiyun goto fail;
2692*4882a593Smuzhiyun
2693*4882a593Smuzhiyun if (header.obu_has_size_field) {
2694*4882a593Smuzhiyun if (mpp_get_bits_left(&gbc) < 8) {
2695*4882a593Smuzhiyun mpp_err( "Invalid OBU: fragment "
2696*4882a593Smuzhiyun "too short (%d bytes).\n", size);
2697*4882a593Smuzhiyun err = MPP_NOK;
2698*4882a593Smuzhiyun goto fail;
2699*4882a593Smuzhiyun }
2700*4882a593Smuzhiyun err = mpp_av1_read_leb128(&gbc, &obu_size);
2701*4882a593Smuzhiyun if (err < 0)
2702*4882a593Smuzhiyun goto fail;
2703*4882a593Smuzhiyun } else
2704*4882a593Smuzhiyun obu_size = size - 1 - header.obu_extension_flag;
2705*4882a593Smuzhiyun
2706*4882a593Smuzhiyun pos = mpp_get_bits_count(&gbc);
2707*4882a593Smuzhiyun
2708*4882a593Smuzhiyun mpp_assert(pos % 8 == 0 && pos / 8 <= (RK_S32)size);
2709*4882a593Smuzhiyun obu_length = pos / 8 + obu_size;
2710*4882a593Smuzhiyun
2711*4882a593Smuzhiyun if (size < obu_length) {
2712*4882a593Smuzhiyun mpp_err( "Invalid OBU length: "
2713*4882a593Smuzhiyun "%lld, but only %d bytes remaining in fragment.\n",
2714*4882a593Smuzhiyun obu_length, size);
2715*4882a593Smuzhiyun err = MPP_NOK;
2716*4882a593Smuzhiyun goto fail;
2717*4882a593Smuzhiyun }
2718*4882a593Smuzhiyun err = mpp_insert_unit_data(frag, -1, header.obu_type,
2719*4882a593Smuzhiyun data, obu_length);
2720*4882a593Smuzhiyun if (err < 0)
2721*4882a593Smuzhiyun goto fail;
2722*4882a593Smuzhiyun
2723*4882a593Smuzhiyun data += obu_length;
2724*4882a593Smuzhiyun size -= obu_length;
2725*4882a593Smuzhiyun }
2726*4882a593Smuzhiyun
2727*4882a593Smuzhiyun success:
2728*4882a593Smuzhiyun err = 0;
2729*4882a593Smuzhiyun fail:
2730*4882a593Smuzhiyun return err;
2731*4882a593Smuzhiyun }
2732*4882a593Smuzhiyun
mpp_av1_ref_tile_data(Av1ObuUnit * unit,BitReadCtx_t * gbc,AV1RawTileData * td)2733*4882a593Smuzhiyun static RK_S32 mpp_av1_ref_tile_data(Av1ObuUnit *unit,
2734*4882a593Smuzhiyun BitReadCtx_t *gbc,
2735*4882a593Smuzhiyun AV1RawTileData *td)
2736*4882a593Smuzhiyun {
2737*4882a593Smuzhiyun RK_S32 pos;
2738*4882a593Smuzhiyun
2739*4882a593Smuzhiyun pos = mpp_get_bits_count(gbc);
2740*4882a593Smuzhiyun if (pos >= (RK_S32)(8 * unit->data_size)) {
2741*4882a593Smuzhiyun mpp_err( "Bitstream ended before "
2742*4882a593Smuzhiyun "any data in tile group (%d bits read).\n", pos);
2743*4882a593Smuzhiyun return MPP_NOK;
2744*4882a593Smuzhiyun }
2745*4882a593Smuzhiyun // Must be byte-aligned at this point.
2746*4882a593Smuzhiyun mpp_assert(pos % 8 == 0);
2747*4882a593Smuzhiyun
2748*4882a593Smuzhiyun td->offset = pos / 8;
2749*4882a593Smuzhiyun td->data = unit->data + pos / 8;
2750*4882a593Smuzhiyun td->data_size = unit->data_size - pos / 8;
2751*4882a593Smuzhiyun
2752*4882a593Smuzhiyun return 0;
2753*4882a593Smuzhiyun }
2754*4882a593Smuzhiyun
mpp_av1_alloc_unit_content(Av1ObuUnit * unit)2755*4882a593Smuzhiyun static MPP_RET mpp_av1_alloc_unit_content(Av1ObuUnit *unit)
2756*4882a593Smuzhiyun {
2757*4882a593Smuzhiyun (void)unit;
2758*4882a593Smuzhiyun MPP_FREE(unit->content);
2759*4882a593Smuzhiyun unit->content = mpp_calloc(AV1RawOBU, 1);
2760*4882a593Smuzhiyun if (!unit->content) {
2761*4882a593Smuzhiyun return MPP_ERR_NOMEM; // drop_obu()
2762*4882a593Smuzhiyun }
2763*4882a593Smuzhiyun return MPP_OK;
2764*4882a593Smuzhiyun }
2765*4882a593Smuzhiyun
mpp_av1_read_unit(AV1Context * ctx,Av1ObuUnit * unit)2766*4882a593Smuzhiyun MPP_RET mpp_av1_read_unit(AV1Context *ctx, Av1ObuUnit *unit)
2767*4882a593Smuzhiyun {
2768*4882a593Smuzhiyun AV1RawOBU *obu;
2769*4882a593Smuzhiyun BitReadCtx_t gbc;
2770*4882a593Smuzhiyun RK_S32 err = 0, start_pos, end_pos, hdr_start_pos;
2771*4882a593Smuzhiyun
2772*4882a593Smuzhiyun err = mpp_av1_alloc_unit_content(unit);
2773*4882a593Smuzhiyun
2774*4882a593Smuzhiyun if (err < 0)
2775*4882a593Smuzhiyun return err;
2776*4882a593Smuzhiyun
2777*4882a593Smuzhiyun obu = unit->content;
2778*4882a593Smuzhiyun
2779*4882a593Smuzhiyun mpp_set_bitread_ctx(&gbc, unit->data, unit->data_size);
2780*4882a593Smuzhiyun
2781*4882a593Smuzhiyun hdr_start_pos = mpp_get_bits_count(&gbc);
2782*4882a593Smuzhiyun
2783*4882a593Smuzhiyun err = mpp_av1_read_obu_header(ctx, &gbc, &obu->header);
2784*4882a593Smuzhiyun if (err < 0)
2785*4882a593Smuzhiyun return err;
2786*4882a593Smuzhiyun mpp_assert(obu->header.obu_type == unit->type);
2787*4882a593Smuzhiyun
2788*4882a593Smuzhiyun if (obu->header.obu_has_size_field) {
2789*4882a593Smuzhiyun RK_U64 obu_size = 0;
2790*4882a593Smuzhiyun err = mpp_av1_read_leb128(&gbc, &obu_size);
2791*4882a593Smuzhiyun if (err < 0)
2792*4882a593Smuzhiyun return err;
2793*4882a593Smuzhiyun obu->obu_size = obu_size;
2794*4882a593Smuzhiyun } else {
2795*4882a593Smuzhiyun if (unit->data_size < (RK_U32)(1 + obu->header.obu_extension_flag)) {
2796*4882a593Smuzhiyun mpp_err( "Invalid OBU length: "
2797*4882a593Smuzhiyun "unit too short (%d).\n", unit->data_size);
2798*4882a593Smuzhiyun return MPP_NOK;
2799*4882a593Smuzhiyun }
2800*4882a593Smuzhiyun obu->obu_size = unit->data_size - 1 - obu->header.obu_extension_flag;
2801*4882a593Smuzhiyun }
2802*4882a593Smuzhiyun
2803*4882a593Smuzhiyun start_pos = mpp_get_bits_count(&gbc);
2804*4882a593Smuzhiyun if (!ctx->fist_tile_group)
2805*4882a593Smuzhiyun ctx->frame_tag_size += ((start_pos - hdr_start_pos + 7) >> 3);
2806*4882a593Smuzhiyun if (obu->header.obu_extension_flag) {
2807*4882a593Smuzhiyun if (obu->header.obu_type != AV1_OBU_SEQUENCE_HEADER &&
2808*4882a593Smuzhiyun obu->header.obu_type != AV1_OBU_TEMPORAL_DELIMITER &&
2809*4882a593Smuzhiyun ctx->operating_point_idc) {
2810*4882a593Smuzhiyun RK_S32 in_temporal_layer =
2811*4882a593Smuzhiyun (ctx->operating_point_idc >> ctx->temporal_id ) & 1;
2812*4882a593Smuzhiyun RK_S32 in_spatial_layer =
2813*4882a593Smuzhiyun (ctx->operating_point_idc >> (ctx->spatial_id + 8)) & 1;
2814*4882a593Smuzhiyun if (!in_temporal_layer || !in_spatial_layer) {
2815*4882a593Smuzhiyun return MPP_ERR_PROTOL; // drop_obu()
2816*4882a593Smuzhiyun }
2817*4882a593Smuzhiyun }
2818*4882a593Smuzhiyun }
2819*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "obu type %d size %d\n",
2820*4882a593Smuzhiyun obu->header.obu_type, obu->obu_size);
2821*4882a593Smuzhiyun switch (obu->header.obu_type) {
2822*4882a593Smuzhiyun case AV1_OBU_SEQUENCE_HEADER: {
2823*4882a593Smuzhiyun err = mpp_av1_sequence_header_obu(ctx, &gbc,
2824*4882a593Smuzhiyun &obu->obu.sequence_header);
2825*4882a593Smuzhiyun if (err < 0)
2826*4882a593Smuzhiyun return err;
2827*4882a593Smuzhiyun ctx->frame_tag_size += obu->obu_size;
2828*4882a593Smuzhiyun if (ctx->operating_point >= 0) {
2829*4882a593Smuzhiyun AV1RawSequenceHeader *sequence_header = &obu->obu.sequence_header;
2830*4882a593Smuzhiyun
2831*4882a593Smuzhiyun if (ctx->operating_point > sequence_header->operating_points_cnt_minus_1) {
2832*4882a593Smuzhiyun mpp_err("Invalid Operating Point %d requested. "
2833*4882a593Smuzhiyun "Must not be higher than %u.\n",
2834*4882a593Smuzhiyun ctx->operating_point, sequence_header->operating_points_cnt_minus_1);
2835*4882a593Smuzhiyun return MPP_ERR_PROTOL;
2836*4882a593Smuzhiyun }
2837*4882a593Smuzhiyun ctx->operating_point_idc = sequence_header->operating_point_idc[ctx->operating_point];
2838*4882a593Smuzhiyun }
2839*4882a593Smuzhiyun
2840*4882a593Smuzhiyun ctx->sequence_header = NULL;
2841*4882a593Smuzhiyun ctx->sequence_header = &obu->obu.sequence_header;
2842*4882a593Smuzhiyun } break;
2843*4882a593Smuzhiyun case AV1_OBU_TEMPORAL_DELIMITER: {
2844*4882a593Smuzhiyun err = mpp_av1_temporal_delimiter_obu(ctx, &gbc);
2845*4882a593Smuzhiyun if (err < 0)
2846*4882a593Smuzhiyun return err;
2847*4882a593Smuzhiyun } break;
2848*4882a593Smuzhiyun case AV1_OBU_FRAME_HEADER:
2849*4882a593Smuzhiyun case AV1_OBU_REDUNDANT_FRAME_HEADER: {
2850*4882a593Smuzhiyun err = mpp_av1_frame_header_obu(ctx, &gbc,
2851*4882a593Smuzhiyun &obu->obu.frame_header,
2852*4882a593Smuzhiyun obu->header.obu_type ==
2853*4882a593Smuzhiyun AV1_OBU_REDUNDANT_FRAME_HEADER,
2854*4882a593Smuzhiyun NULL);
2855*4882a593Smuzhiyun if (err < 0)
2856*4882a593Smuzhiyun return err;
2857*4882a593Smuzhiyun ctx->frame_tag_size += obu->obu_size;
2858*4882a593Smuzhiyun } break;
2859*4882a593Smuzhiyun case AV1_OBU_TILE_GROUP: {
2860*4882a593Smuzhiyun RK_U32 cur_pos = mpp_get_bits_count(&gbc);
2861*4882a593Smuzhiyun
2862*4882a593Smuzhiyun err = mpp_av1_tile_group_obu(ctx, &gbc, &obu->obu.tile_group);
2863*4882a593Smuzhiyun if (err < 0)
2864*4882a593Smuzhiyun return err;
2865*4882a593Smuzhiyun if (!ctx->fist_tile_group)
2866*4882a593Smuzhiyun ctx->frame_tag_size += MPP_ALIGN(mpp_get_bits_count(&gbc) - cur_pos, 8) / 8;
2867*4882a593Smuzhiyun ctx->fist_tile_group = 1;
2868*4882a593Smuzhiyun err = mpp_av1_ref_tile_data(unit, &gbc,
2869*4882a593Smuzhiyun &obu->obu.tile_group.tile_data);
2870*4882a593Smuzhiyun if (err < 0)
2871*4882a593Smuzhiyun return err;
2872*4882a593Smuzhiyun } break;
2873*4882a593Smuzhiyun case AV1_OBU_FRAME: {
2874*4882a593Smuzhiyun err = mpp_av1_frame_obu(ctx, &gbc, &obu->obu.frame,
2875*4882a593Smuzhiyun NULL);
2876*4882a593Smuzhiyun if (err < 0)
2877*4882a593Smuzhiyun return err;
2878*4882a593Smuzhiyun
2879*4882a593Smuzhiyun err = mpp_av1_ref_tile_data(unit, &gbc,
2880*4882a593Smuzhiyun &obu->obu.frame.tile_group.tile_data);
2881*4882a593Smuzhiyun if (err < 0)
2882*4882a593Smuzhiyun return err;
2883*4882a593Smuzhiyun } break;
2884*4882a593Smuzhiyun case AV1_OBU_TILE_LIST: {
2885*4882a593Smuzhiyun err = mpp_av1_tile_list_obu(ctx, &gbc, &obu->obu.tile_list);
2886*4882a593Smuzhiyun if (err < 0)
2887*4882a593Smuzhiyun return err;
2888*4882a593Smuzhiyun
2889*4882a593Smuzhiyun err = mpp_av1_ref_tile_data(unit, &gbc,
2890*4882a593Smuzhiyun &obu->obu.tile_list.tile_data);
2891*4882a593Smuzhiyun if (err < 0)
2892*4882a593Smuzhiyun return err;
2893*4882a593Smuzhiyun } break;
2894*4882a593Smuzhiyun case AV1_OBU_METADATA: {
2895*4882a593Smuzhiyun ctx->frame_tag_size += obu->obu_size;
2896*4882a593Smuzhiyun err = mpp_av1_metadata_obu(ctx, &gbc, &obu->obu.metadata);
2897*4882a593Smuzhiyun if (err < 0)
2898*4882a593Smuzhiyun return err;
2899*4882a593Smuzhiyun } break;
2900*4882a593Smuzhiyun case AV1_OBU_PADDING: {
2901*4882a593Smuzhiyun err = mpp_av1_padding_obu(ctx, &gbc, &obu->obu.padding);
2902*4882a593Smuzhiyun if (err < 0)
2903*4882a593Smuzhiyun return err;
2904*4882a593Smuzhiyun } break;
2905*4882a593Smuzhiyun default:
2906*4882a593Smuzhiyun return MPP_ERR_VALUE;
2907*4882a593Smuzhiyun }
2908*4882a593Smuzhiyun
2909*4882a593Smuzhiyun end_pos = mpp_get_bits_count(&gbc);
2910*4882a593Smuzhiyun mpp_assert(end_pos <= (RK_S32)(unit->data_size * 8));
2911*4882a593Smuzhiyun
2912*4882a593Smuzhiyun if (obu->obu_size > 0 &&
2913*4882a593Smuzhiyun obu->header.obu_type != AV1_OBU_TILE_GROUP &&
2914*4882a593Smuzhiyun obu->header.obu_type != AV1_OBU_TILE_LIST &&
2915*4882a593Smuzhiyun obu->header.obu_type != AV1_OBU_FRAME) {
2916*4882a593Smuzhiyun RK_S32 nb_bits = obu->obu_size * 8 + start_pos - end_pos;
2917*4882a593Smuzhiyun
2918*4882a593Smuzhiyun if (nb_bits <= 0)
2919*4882a593Smuzhiyun return MPP_NOK;
2920*4882a593Smuzhiyun
2921*4882a593Smuzhiyun err = mpp_av1_trailing_bits(ctx, &gbc, nb_bits);
2922*4882a593Smuzhiyun if (err < 0)
2923*4882a593Smuzhiyun return err;
2924*4882a593Smuzhiyun }
2925*4882a593Smuzhiyun
2926*4882a593Smuzhiyun return 0;
2927*4882a593Smuzhiyun }
2928*4882a593Smuzhiyun
mpp_av1_read_fragment_content(AV1Context * ctx,Av1UnitFragment * frag)2929*4882a593Smuzhiyun RK_S32 mpp_av1_read_fragment_content(AV1Context *ctx, Av1UnitFragment *frag)
2930*4882a593Smuzhiyun {
2931*4882a593Smuzhiyun int err, i, j;
2932*4882a593Smuzhiyun AV1RawOBU *obu;
2933*4882a593Smuzhiyun
2934*4882a593Smuzhiyun ctx->frame_tag_size = 0;
2935*4882a593Smuzhiyun ctx->fist_tile_group = 0;
2936*4882a593Smuzhiyun for (i = 0; i < frag->nb_units; i++) {
2937*4882a593Smuzhiyun Av1ObuUnit *unit = &frag->units[i];
2938*4882a593Smuzhiyun if (ctx->unit_types) {
2939*4882a593Smuzhiyun for (j = 0; j < ctx->nb_unit_types; j++) {
2940*4882a593Smuzhiyun if (ctx->unit_types[j] == unit->type)
2941*4882a593Smuzhiyun break;
2942*4882a593Smuzhiyun }
2943*4882a593Smuzhiyun if (j >= ctx->nb_unit_types)
2944*4882a593Smuzhiyun continue;
2945*4882a593Smuzhiyun }
2946*4882a593Smuzhiyun MPP_FREE(unit->content);
2947*4882a593Smuzhiyun mpp_assert(unit->data);
2948*4882a593Smuzhiyun err = mpp_av1_read_unit(ctx, unit);
2949*4882a593Smuzhiyun
2950*4882a593Smuzhiyun if (err == MPP_ERR_VALUE) {
2951*4882a593Smuzhiyun mpp_err_f("Decomposition unimplemented for unit %d "
2952*4882a593Smuzhiyun "(type %d).\n", i, unit->type);
2953*4882a593Smuzhiyun } else if (err == MPP_ERR_PROTOL) {
2954*4882a593Smuzhiyun mpp_err_f("Skipping decomposition of"
2955*4882a593Smuzhiyun "unit %d (type %d).\n", i, unit->type);
2956*4882a593Smuzhiyun MPP_FREE(unit->content);
2957*4882a593Smuzhiyun unit->content = NULL;
2958*4882a593Smuzhiyun } else if (err < 0) {
2959*4882a593Smuzhiyun mpp_err_f("Failed to read unit %d (type %d).\n", i, unit->type);
2960*4882a593Smuzhiyun return err;
2961*4882a593Smuzhiyun }
2962*4882a593Smuzhiyun obu = unit->content;
2963*4882a593Smuzhiyun av1d_dbg(AV1D_DBG_HEADER, "obu->header.obu_type %d, obu->obu_size = %d ctx->frame_tag_size %d",
2964*4882a593Smuzhiyun obu->header.obu_type, obu->obu_size, ctx->frame_tag_size);
2965*4882a593Smuzhiyun }
2966*4882a593Smuzhiyun return 0;
2967*4882a593Smuzhiyun }
2968*4882a593Smuzhiyun
mpp_av1_set_context_with_sequence(Av1CodecContext * ctx,const AV1RawSequenceHeader * seq)2969*4882a593Smuzhiyun int mpp_av1_set_context_with_sequence(Av1CodecContext *ctx,
2970*4882a593Smuzhiyun const AV1RawSequenceHeader *seq)
2971*4882a593Smuzhiyun {
2972*4882a593Smuzhiyun int width = seq->max_frame_width_minus_1 + 1;
2973*4882a593Smuzhiyun int height = seq->max_frame_height_minus_1 + 1;
2974*4882a593Smuzhiyun
2975*4882a593Smuzhiyun ctx->profile = seq->seq_profile;
2976*4882a593Smuzhiyun ctx->level = seq->seq_level_idx[0];
2977*4882a593Smuzhiyun
2978*4882a593Smuzhiyun ctx->color_range =
2979*4882a593Smuzhiyun seq->color_config.color_range ? MPP_FRAME_RANGE_JPEG : MPP_FRAME_RANGE_MPEG;
2980*4882a593Smuzhiyun ctx->color_primaries = seq->color_config.color_primaries;
2981*4882a593Smuzhiyun ctx->colorspace = seq->color_config.color_primaries;
2982*4882a593Smuzhiyun ctx->color_trc = seq->color_config.transfer_characteristics;
2983*4882a593Smuzhiyun
2984*4882a593Smuzhiyun switch (seq->color_config.chroma_sample_position) {
2985*4882a593Smuzhiyun case AV1_CSP_VERTICAL:
2986*4882a593Smuzhiyun ctx->chroma_sample_location = MPP_CHROMA_LOC_LEFT;
2987*4882a593Smuzhiyun break;
2988*4882a593Smuzhiyun case AV1_CSP_COLOCATED:
2989*4882a593Smuzhiyun ctx->chroma_sample_location = MPP_CHROMA_LOC_TOPLEFT;
2990*4882a593Smuzhiyun break;
2991*4882a593Smuzhiyun }
2992*4882a593Smuzhiyun
2993*4882a593Smuzhiyun if (ctx->width != width || ctx->height != height) {
2994*4882a593Smuzhiyun ctx->width = width;
2995*4882a593Smuzhiyun ctx->height = height;
2996*4882a593Smuzhiyun }
2997*4882a593Smuzhiyun return 0;
2998*4882a593Smuzhiyun }
2999*4882a593Smuzhiyun
mpp_av1_fragment_reset(Av1UnitFragment * frag)3000*4882a593Smuzhiyun void mpp_av1_fragment_reset(Av1UnitFragment *frag)
3001*4882a593Smuzhiyun {
3002*4882a593Smuzhiyun int i;
3003*4882a593Smuzhiyun
3004*4882a593Smuzhiyun for (i = 0; i < frag->nb_units; i++) {
3005*4882a593Smuzhiyun Av1ObuUnit *unit = &frag->units[i];
3006*4882a593Smuzhiyun MPP_FREE(unit->content);
3007*4882a593Smuzhiyun unit->data = NULL;
3008*4882a593Smuzhiyun unit->data_size = 0;
3009*4882a593Smuzhiyun }
3010*4882a593Smuzhiyun frag->nb_units = 0;
3011*4882a593Smuzhiyun frag->data = NULL;
3012*4882a593Smuzhiyun frag->data_size = 0;
3013*4882a593Smuzhiyun }
3014*4882a593Smuzhiyun
mpp_av1_assemble_fragment(AV1Context * ctx,Av1UnitFragment * frag)3015*4882a593Smuzhiyun RK_S32 mpp_av1_assemble_fragment(AV1Context *ctx, Av1UnitFragment *frag)
3016*4882a593Smuzhiyun {
3017*4882a593Smuzhiyun size_t size, pos;
3018*4882a593Smuzhiyun RK_S32 i;
3019*4882a593Smuzhiyun (void)ctx;
3020*4882a593Smuzhiyun size = 0;
3021*4882a593Smuzhiyun for (i = 0; i < frag->nb_units; i++)
3022*4882a593Smuzhiyun size += frag->units[i].data_size;
3023*4882a593Smuzhiyun
3024*4882a593Smuzhiyun frag->data = mpp_malloc(RK_U8, size + BUFFER_PADDING_SIZE);
3025*4882a593Smuzhiyun if (!frag->data)
3026*4882a593Smuzhiyun return MPP_ERR_NOMEM;
3027*4882a593Smuzhiyun
3028*4882a593Smuzhiyun memset(frag->data + size, 0, BUFFER_PADDING_SIZE);
3029*4882a593Smuzhiyun
3030*4882a593Smuzhiyun pos = 0;
3031*4882a593Smuzhiyun for (i = 0; i < frag->nb_units; i++) {
3032*4882a593Smuzhiyun memcpy(frag->data + pos, frag->units[i].data,
3033*4882a593Smuzhiyun frag->units[i].data_size);
3034*4882a593Smuzhiyun pos += frag->units[i].data_size;
3035*4882a593Smuzhiyun }
3036*4882a593Smuzhiyun mpp_assert(pos == size);
3037*4882a593Smuzhiyun frag->data_size = size;
3038*4882a593Smuzhiyun
3039*4882a593Smuzhiyun return 0;
3040*4882a593Smuzhiyun }
3041*4882a593Smuzhiyun
mpp_av1_flush(AV1Context * ctx)3042*4882a593Smuzhiyun void mpp_av1_flush(AV1Context *ctx)
3043*4882a593Smuzhiyun {
3044*4882a593Smuzhiyun // ctx->sequencframe_headere_header = NULL;
3045*4882a593Smuzhiyun // ctx-> = NULL;
3046*4882a593Smuzhiyun
3047*4882a593Smuzhiyun memset(ctx->ref_s, 0, sizeof(ctx->ref_s));
3048*4882a593Smuzhiyun ctx->operating_point_idc = 0;
3049*4882a593Smuzhiyun ctx->seen_frame_header = 0;
3050*4882a593Smuzhiyun ctx->tile_num = 0;
3051*4882a593Smuzhiyun }
3052*4882a593Smuzhiyun
mpp_av1_close(AV1Context * ctx)3053*4882a593Smuzhiyun void mpp_av1_close(AV1Context *ctx)
3054*4882a593Smuzhiyun {
3055*4882a593Smuzhiyun MPP_FREE(ctx->frame_header);
3056*4882a593Smuzhiyun MPP_FREE(ctx->sequence_header);
3057*4882a593Smuzhiyun MPP_FREE(ctx->raw_frame_header);
3058*4882a593Smuzhiyun }
3059*4882a593Smuzhiyun
mpp_av1_free_metadata(void * unit,RK_U8 * content)3060*4882a593Smuzhiyun void mpp_av1_free_metadata(void *unit, RK_U8 *content)
3061*4882a593Smuzhiyun {
3062*4882a593Smuzhiyun AV1RawOBU *obu = (AV1RawOBU*)content;
3063*4882a593Smuzhiyun (void)unit;
3064*4882a593Smuzhiyun mpp_assert(obu->header.obu_type == AV1_OBU_METADATA);
3065*4882a593Smuzhiyun MPP_FREE(content);
3066*4882a593Smuzhiyun }
3067