• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**************************************************************************
2  *
3  * Copyright 2020 Advanced Micro Devices, Inc.
4  * All Rights Reserved.
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sub license, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice (including the
15  * next paragraph) shall be included in all copies or substantial portions
16  * of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21  * IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR
22  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25  *
26  **************************************************************************/
27 
28 #include "pipe/p_video_codec.h"
29 #include "util/u_memory.h"
30 #include "util/u_video.h"
31 #include "vl/vl_video_buffer.h"
32 
33 #include "vl/vl_codec.h"
34 
35 #include "entrypoint.h"
36 #include "vid_dec.h"
37 #include "vid_dec_av1.h"
38 
av1_f(struct vl_vlc * vlc,unsigned n)39 static unsigned av1_f(struct vl_vlc *vlc, unsigned n)
40 {
41    unsigned valid = vl_vlc_valid_bits(vlc);
42 
43    if (n == 0)
44       return 0;
45 
46    if (valid < 32)
47       vl_vlc_fillbits(vlc);
48 
49    return vl_vlc_get_uimsbf(vlc, n);
50 }
51 
av1_uvlc(struct vl_vlc * vlc)52 static unsigned av1_uvlc(struct vl_vlc *vlc)
53 {
54    unsigned value;
55    unsigned leadingZeros = 0;
56 
57    while (1) {
58       bool done = av1_f(vlc, 1);
59       if (done)
60          break;
61       leadingZeros++;
62    }
63 
64    if (leadingZeros >= 32)
65       return 0xffffffff;
66 
67    value = av1_f(vlc, leadingZeros);
68 
69    return value + (1 << leadingZeros) - 1;
70 }
71 
av1_le(struct vl_vlc * vlc,const unsigned n)72 static int av1_le(struct vl_vlc *vlc, const unsigned n)
73 {
74    unsigned byte, t = 0;
75    unsigned i;
76 
77    for (i = 0; i < n; ++i) {
78       byte = av1_f(vlc, 8);
79       t += (byte << (i * 8));
80    }
81 
82    return t;
83 }
84 
av1_uleb128(struct vl_vlc * vlc)85 static unsigned av1_uleb128(struct vl_vlc *vlc)
86 {
87    unsigned value = 0;
88    unsigned leb128Bytes = 0;
89    unsigned i;
90 
91    for (i = 0; i < 8; ++i) {
92       leb128Bytes = av1_f(vlc, 8);
93       value |= ((leb128Bytes & 0x7f) << (i * 7));
94       if (!(leb128Bytes & 0x80))
95          break;
96    }
97 
98    return value;
99 }
100 
av1_su(struct vl_vlc * vlc,const unsigned n)101 static int av1_su(struct vl_vlc *vlc, const unsigned n)
102 {
103    unsigned value = av1_f(vlc, n);
104    unsigned signMask = 1 << (n - 1);
105 
106    if (value && signMask)
107       value = value - 2 * signMask;
108 
109    return value;
110 }
111 
FloorLog2(unsigned x)112 static unsigned FloorLog2(unsigned x)
113 {
114    unsigned s = 0;
115    unsigned x1 = x;
116 
117    while (x1 != 0) {
118       x1 = x1 >> 1;
119       s++;
120    }
121 
122    return s - 1;
123 }
124 
av1_ns(struct vl_vlc * vlc,unsigned n)125 static unsigned av1_ns(struct vl_vlc *vlc, unsigned n)
126 {
127    unsigned w = FloorLog2(n) + 1;
128    unsigned m = (1 << w) - n;
129    unsigned v = av1_f(vlc, w - 1);
130 
131    if (v < m)
132       return v;
133 
134    bool extra_bit = av1_f(vlc, 1);
135 
136    return (v << 1) - m + extra_bit;
137 }
138 
av1_byte_alignment(struct vl_vlc * vlc)139 static void av1_byte_alignment(struct vl_vlc *vlc)
140 {
141    vl_vlc_eatbits(vlc, vl_vlc_valid_bits(vlc) % 8);
142 }
143 
sequence_header_obu(vid_dec_PrivateType * priv,struct vl_vlc * vlc)144 static void sequence_header_obu(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
145 {
146    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
147    bool timing_info_present_flag;
148    bool initial_display_delay_present_flag;
149    uint8_t seq_level_idx;
150    bool initial_display_delay_present_for_this_op;
151    bool high_bitdepth;
152    bool twelve_bit;
153    bool color_description_present_flag;
154    uint8_t color_primaries;
155    uint8_t transfer_characteristics;
156    uint8_t matrix_coefficients;
157    int i;
158 
159    seq->seq_profile = av1_f(vlc, 3);
160    assert(seq->seq_profile < 3);
161 
162    av1_f(vlc, 1); /* still_picture */
163    seq->reduced_still_picture_header = av1_f(vlc, 1);
164    if (seq->reduced_still_picture_header) {
165       timing_info_present_flag = 0;
166       seq->decoder_model_info_present_flag = 0;
167       initial_display_delay_present_flag = 0;
168       seq->operating_points_cnt_minus_1 = 0;
169       seq->operating_point_idc[0] = 0;
170       seq_level_idx = av1_f(vlc, 5);
171       seq->decoder_model_present_for_this_op[0] = 0;
172       initial_display_delay_present_for_this_op = 0;
173    } else {
174       uint8_t buffer_delay_length_minus_1 = 0;
175 
176       timing_info_present_flag = av1_f(vlc, 1);
177       if (timing_info_present_flag) {
178          av1_f(vlc, 32); /* num_units_in_display_tick */
179          av1_f(vlc, 32); /* time_scale */
180          seq->timing_info.equal_picture_interval = av1_f(vlc, 1);
181          if (seq->timing_info.equal_picture_interval)
182             av1_uvlc(vlc); /* num_ticks_per_picture_minus_1 */
183 
184          seq->decoder_model_info_present_flag = av1_f(vlc, 1);
185          if (seq->decoder_model_info_present_flag) {
186              /* decoder_model_info */
187             buffer_delay_length_minus_1 = av1_f(vlc, 5);
188             seq->decoder_model_info.num_units_in_decoding_tick = av1_f(vlc, 32);
189             seq->decoder_model_info.buffer_removal_time_length_minus_1 = av1_f(vlc, 5);
190             seq->decoder_model_info.frame_presentation_time_length_minus_1 = av1_f(vlc, 5);
191          }
192       } else {
193          seq->decoder_model_info_present_flag = 0;
194       }
195 
196       initial_display_delay_present_flag = av1_f(vlc, 1);
197       seq->operating_points_cnt_minus_1 = av1_f(vlc, 5);
198       for (i = 0; i < seq->operating_points_cnt_minus_1 + 1; ++i) {
199          seq->operating_point_idc[i] = av1_f(vlc, 12);
200          seq_level_idx = av1_f(vlc, 5);
201          if (seq_level_idx > 7)
202             av1_f(vlc, 1); /* seq_tier */
203 
204          if (seq->decoder_model_info_present_flag) {
205             seq->decoder_model_present_for_this_op[i] = av1_f(vlc, 1);
206             if (seq->decoder_model_present_for_this_op[i]) {
207                uint8_t n = buffer_delay_length_minus_1 + 1;
208                av1_f(vlc, n); /* decoder_buffer_delay */
209                av1_f(vlc, n); /* encoder_buffer_delay */
210                av1_f(vlc, 1); /* low_delay_mode_flag */
211             }
212          } else {
213             seq->decoder_model_present_for_this_op[i] = 0;
214          }
215 
216          if (initial_display_delay_present_flag) {
217             initial_display_delay_present_for_this_op = av1_f(vlc, 1);
218             if (initial_display_delay_present_for_this_op)
219                av1_f(vlc, 4); /* initial_display_delay_minus_1 */
220          }
221       }
222    }
223 
224    seq->frame_width_bits_minus_1 = av1_f(vlc, 4);
225    seq->frame_height_bits_minus_1 = av1_f(vlc, 4);
226    seq->max_frame_width_minus_1 = av1_f(vlc, seq->frame_width_bits_minus_1 + 1);
227    seq->max_frame_height_minus_1 = av1_f(vlc, seq->frame_height_bits_minus_1 + 1);
228 
229    if (seq->reduced_still_picture_header)
230       seq->frame_id_numbers_present_flag = 0;
231    else
232       seq->frame_id_numbers_present_flag = av1_f(vlc, 1);
233    if (seq->frame_id_numbers_present_flag) {
234       seq->delta_frame_id_length_minus_2 = av1_f(vlc, 4);
235       seq->additional_frame_id_length_minus_1 = av1_f(vlc, 3);
236    }
237 
238    seq->use_128x128_superblock = av1_f(vlc, 1);
239    seq->enable_filter_intra = av1_f(vlc, 1);
240    seq->enable_intra_edge_filter = av1_f(vlc, 1);
241    if (seq->reduced_still_picture_header) {
242       seq->enable_interintra_compound = 0;
243       seq->enable_masked_compound = 0;
244       seq->enable_warped_motion = 0;
245       seq->enable_dual_filter = 0;
246       seq->enable_order_hint = 0;
247       seq->enable_jnt_comp = 0;
248       seq->enable_ref_frame_mvs = 0;
249       seq->seq_force_screen_content_tools = AV1_SELECT_SCREEN_CONTENT_TOOLS;
250       seq->seq_force_integer_mv = AV1_SELECT_INTEGER_MV;
251       seq->OrderHintBits = 0;
252   } else {
253       bool seq_choose_screen_content_tools;
254       seq->enable_interintra_compound = av1_f(vlc, 1);
255       seq->enable_masked_compound = av1_f(vlc, 1);
256       seq->enable_warped_motion = av1_f(vlc, 1);
257       seq->enable_dual_filter = av1_f(vlc, 1);
258       seq->enable_order_hint = av1_f(vlc, 1);
259       if (seq->enable_order_hint) {
260          seq->enable_jnt_comp = av1_f(vlc, 1);
261          seq->enable_ref_frame_mvs = av1_f(vlc, 1);
262       } else {
263          seq->enable_jnt_comp = 0;
264          seq->enable_ref_frame_mvs = 0;
265       }
266 
267       seq_choose_screen_content_tools = av1_f(vlc, 1);
268       seq->seq_force_screen_content_tools =
269          seq_choose_screen_content_tools ? AV1_SELECT_SCREEN_CONTENT_TOOLS : av1_f(vlc, 1);
270 
271       if (seq->seq_force_screen_content_tools > 0) {
272          bool seq_choose_integer_mv = av1_f(vlc, 1);
273          seq->seq_force_integer_mv =
274             seq_choose_integer_mv ? AV1_SELECT_INTEGER_MV : av1_f(vlc, 1);
275       } else {
276          seq->seq_force_integer_mv = AV1_SELECT_INTEGER_MV;
277       }
278 
279       if (seq->enable_order_hint) {
280          seq->order_hint_bits_minus_1 = av1_f(vlc, 3);
281          seq->OrderHintBits = seq->order_hint_bits_minus_1 + 1;
282       } else {
283          seq->OrderHintBits = 0;
284       }
285    }
286 
287    seq->enable_superres = av1_f(vlc, 1);
288    seq->enable_cdef = av1_f(vlc, 1);
289    seq->enable_restoration = av1_f(vlc, 1);
290 
291    high_bitdepth = av1_f(vlc, 1);
292    if (seq->seq_profile == 2 && high_bitdepth) {
293       twelve_bit = av1_f(vlc, 1);
294       seq->color_config.BitDepth = twelve_bit ? 12 : 10;
295    } else if (seq->seq_profile <= 2) {
296       seq->color_config.BitDepth = high_bitdepth ? 10 : 8;
297    }
298 
299    seq->color_config.mono_chrome = (seq->seq_profile == 1) ? 0 : av1_f(vlc, 1);
300    seq->color_config.NumPlanes = seq->color_config.mono_chrome ? 1 : 3;
301 
302    color_description_present_flag = av1_f(vlc, 1);
303    if (color_description_present_flag) {
304       color_primaries = av1_f(vlc, 8);
305       transfer_characteristics = av1_f(vlc, 8);
306       matrix_coefficients = av1_f(vlc, 8);
307    } else {
308       color_primaries = AV1_CP_UNSPECIFIED;
309       transfer_characteristics = AV1_TC_UNSPECIFIED;
310       matrix_coefficients = AV1_MC_UNSPECIFIED;
311    }
312 
313    if (seq->color_config.mono_chrome) {
314       av1_f(vlc, 1); /* color_range */
315       seq->color_config.subsampling_x = 1;
316       seq->color_config.subsampling_y = 1;
317       seq->color_config.separate_uv_delta_q = 0;
318    } else if (color_primaries == AV1_CP_BT_709 &&
319               transfer_characteristics == AV1_TC_SRGB &&
320               matrix_coefficients == AV1_MC_IDENTITY) {
321       seq->color_config.subsampling_x = 0;
322       seq->color_config.subsampling_y = 0;
323    } else {
324       av1_f(vlc, 1); /* color_range */
325       if (seq->seq_profile == 0) {
326          seq->color_config.subsampling_x = 1;
327          seq->color_config.subsampling_y = 1;
328       } else if (seq->seq_profile == 1 ) {
329          seq->color_config.subsampling_x = 0;
330          seq->color_config.subsampling_y = 0;
331       } else {
332          if (seq->color_config.BitDepth == 12) {
333             seq->color_config.subsampling_x = av1_f(vlc, 1);
334             if (seq->color_config.subsampling_x)
335                seq->color_config.subsampling_y = av1_f(vlc, 1);
336             else
337                seq->color_config.subsampling_y = 0;
338          } else {
339             seq->color_config.subsampling_x = 1;
340             seq->color_config.subsampling_y = 0;
341          }
342       }
343       if (seq->color_config.subsampling_x && seq->color_config.subsampling_y)
344          av1_f(vlc, 2); /* chroma_sample_position */
345    }
346    if (!seq->color_config.mono_chrome)
347       seq->color_config.separate_uv_delta_q = av1_f(vlc, 1);
348 
349    seq->film_grain_params_present = av1_f(vlc, 1);
350 
351    priv->picture.av1.picture_parameter.profile = seq->seq_profile;
352    priv->picture.av1.picture_parameter.seq_info_fields.use_128x128_superblock =
353       seq->use_128x128_superblock;
354    priv->picture.av1.picture_parameter.seq_info_fields.enable_filter_intra =
355       seq->enable_filter_intra;
356    priv->picture.av1.picture_parameter.seq_info_fields.enable_intra_edge_filter =
357       seq->enable_intra_edge_filter;
358    priv->picture.av1.picture_parameter.order_hint_bits_minus_1 =
359       seq->order_hint_bits_minus_1;
360    priv->picture.av1.picture_parameter.max_width = seq->max_frame_width_minus_1 + 1;
361    priv->picture.av1.picture_parameter.max_height = seq->max_frame_height_minus_1 + 1;
362    priv->picture.av1.picture_parameter.seq_info_fields.enable_interintra_compound =
363       seq->enable_interintra_compound;
364    priv->picture.av1.picture_parameter.seq_info_fields.enable_masked_compound =
365       seq->enable_masked_compound;
366    priv->picture.av1.picture_parameter.seq_info_fields.enable_dual_filter =
367       seq->enable_dual_filter;
368    priv->picture.av1.picture_parameter.seq_info_fields.enable_order_hint =
369       seq->enable_order_hint;
370    priv->picture.av1.picture_parameter.seq_info_fields.enable_jnt_comp =
371       seq->enable_jnt_comp;
372    priv->picture.av1.picture_parameter.seq_info_fields.ref_frame_mvs =
373       seq->enable_ref_frame_mvs;
374    priv->picture.av1.picture_parameter.bit_depth_idx =
375       (seq->color_config.BitDepth - 8) >> 1;
376    priv->picture.av1.picture_parameter.seq_info_fields.mono_chrome =
377       seq->color_config.mono_chrome;
378 }
379 
superres_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)380 static void superres_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
381 {
382    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
383    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
384    unsigned coded_denom;
385 
386    if (seq->enable_superres)
387       hdr->use_superres = av1_f(vlc, 1);
388    else
389       hdr->use_superres = 0;
390 
391    if (hdr->use_superres) {
392       coded_denom = av1_f(vlc, 3 /* SUPERRES_DENOM_BITS */);
393       hdr->SuperresDenom = coded_denom + 9 /* SUPERRES_DENOM_MIN */;
394    } else {
395       hdr->SuperresDenom = 8 /* SUPERRES_NUM */;
396    }
397 
398    hdr->UpscaledWidth = hdr->FrameWidth;
399    hdr->FrameWidth = (hdr->UpscaledWidth * 8 + (hdr->SuperresDenom / 2)) /
400       hdr->SuperresDenom;
401 }
402 
compute_image_size(vid_dec_PrivateType * priv)403 static void compute_image_size(vid_dec_PrivateType *priv)
404 {
405    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
406 
407    hdr->MiCols = 2 * ((hdr->FrameWidth + 7) >> 3);
408    hdr->MiRows = 2 * ((hdr->FrameHeight + 7) >> 3);
409 }
410 
frame_size(vid_dec_PrivateType * priv,struct vl_vlc * vlc)411 static void frame_size(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
412 {
413    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
414    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
415    unsigned frame_width_minus_1;
416    unsigned frame_height_minus_1;
417 
418    if (hdr->frame_size_override_flag) {
419       frame_width_minus_1 = av1_f(vlc, seq->frame_width_bits_minus_1 + 1);
420       frame_height_minus_1 = av1_f(vlc, seq->frame_height_bits_minus_1 + 1);
421       hdr->FrameWidth = frame_width_minus_1 + 1;
422       hdr->FrameHeight = frame_height_minus_1 + 1;
423    } else {
424       hdr->FrameWidth = seq->max_frame_width_minus_1 + 1;
425       hdr->FrameHeight = seq->max_frame_height_minus_1 + 1;
426    }
427 
428    superres_params(priv, vlc);
429    compute_image_size(priv);
430 }
431 
render_size(vid_dec_PrivateType * priv,struct vl_vlc * vlc)432 static void render_size(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
433 {
434    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
435    bool render_and_frame_size_different;
436    unsigned render_width_minus_1;
437    unsigned render_height_minus_1;
438 
439    render_and_frame_size_different = av1_f(vlc, 1);
440    if (render_and_frame_size_different) {
441       render_width_minus_1 = av1_f(vlc, 16);
442       render_height_minus_1 = av1_f(vlc, 16);
443       hdr->RenderWidth = render_width_minus_1 + 1;
444       hdr->RenderHeight = render_height_minus_1 + 1;
445    } else {
446       hdr->RenderWidth = hdr->UpscaledWidth;
447       hdr->RenderHeight = hdr->FrameHeight;
448    }
449 }
450 
get_relative_dist(vid_dec_PrivateType * priv,int a,int b)451 static int get_relative_dist(vid_dec_PrivateType *priv, int a, int b)
452 {
453    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
454    int diff;
455    unsigned m;
456 
457    if (!seq->enable_order_hint)
458       return 0;
459 
460    diff = a - b;
461    m = 1 << (seq->OrderHintBits - 1);
462    diff = (diff & (m - 1)) - (diff & m);
463 
464    return diff;
465 }
466 
find_latest_backward(vid_dec_PrivateType * priv)467 static uint8_t find_latest_backward(vid_dec_PrivateType *priv)
468 {
469    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
470    uint8_t ref = 0xff;
471    unsigned latestOrderHint = 0;
472    int i;
473 
474    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
475       unsigned hint = hdr->shiftedOrderHints[i];
476       if (!hdr->usedFrame[i] &&
477           hint >= hdr->curFrameHint &&
478           (ref == 0xff || hint >= latestOrderHint)) {
479          ref = i;
480          latestOrderHint = hint;
481       }
482    }
483 
484    return ref;
485 }
486 
find_earliest_backward(vid_dec_PrivateType * priv)487 static uint8_t find_earliest_backward(vid_dec_PrivateType *priv)
488 {
489    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
490    uint8_t ref = 0xff;
491    unsigned earliestOrderHint = 0;
492    int i;
493 
494    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
495       unsigned hint = hdr->shiftedOrderHints[i];
496       if (!hdr->usedFrame[i] &&
497           hint >= hdr->curFrameHint &&
498           (ref == 0xff || hint < earliestOrderHint)) {
499          ref = i;
500          earliestOrderHint = hint;
501       }
502    }
503 
504    return ref;
505 }
506 
find_latest_forward(vid_dec_PrivateType * priv)507 static uint8_t find_latest_forward(vid_dec_PrivateType *priv)
508 {
509    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
510    uint8_t ref = 0xff;
511    unsigned latestOrderHint = 0;
512    int i;
513 
514    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
515       unsigned hint = hdr->shiftedOrderHints[i];
516       if (!hdr->usedFrame[i] &&
517           hint < hdr->curFrameHint &&
518           (ref == 0xff || hint >= latestOrderHint)) {
519          ref = i;
520          latestOrderHint = hint;
521       }
522    }
523 
524    return ref;
525 }
526 
set_frame_refs(vid_dec_PrivateType * priv,struct vl_vlc * vlc)527 static void set_frame_refs(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
528 {
529    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
530    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
531    uint8_t Ref_Frame_List[5] = { AV1_LAST2_FRAME , AV1_LAST3_FRAME, AV1_BWDREF_FRAME,
532       AV1_ALTREF2_FRAME, AV1_ALTREF_FRAME };
533    unsigned earliestOrderHint = 0;
534    uint8_t ref;
535    int i;
536 
537    for (i = 0; i < AV1_REFS_PER_FRAME; ++i)
538       hdr->ref_frame_idx[i] = 0xff;
539 
540    hdr->ref_frame_idx[0] = hdr->last_frame_idx;
541    hdr->ref_frame_idx[AV1_GOLDEN_FRAME - AV1_LAST_FRAME] = hdr->gold_frame_idx;
542 
543    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
544       hdr->usedFrame[i] = 0;
545 
546    hdr->usedFrame[hdr->last_frame_idx] = 1;
547    hdr->usedFrame[hdr->gold_frame_idx] = 1;
548 
549    hdr->curFrameHint = 1 << (seq->OrderHintBits - 1);
550 
551    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
552       hdr->shiftedOrderHints[i] =
553          hdr->curFrameHint +
554          get_relative_dist(priv, hdr->RefOrderHint[i], hdr->OrderHint);
555 
556    ref = find_latest_backward(priv);
557    if (ref != 0xff) {
558       hdr->ref_frame_idx[AV1_ALTREF_FRAME - AV1_LAST_FRAME] = ref;
559       hdr->usedFrame[ref] = 1;
560    }
561 
562    ref = find_earliest_backward(priv);
563    if (ref != 0xff) {
564       hdr->ref_frame_idx[AV1_BWDREF_FRAME - AV1_LAST_FRAME] = ref;
565       hdr->usedFrame[ref] = 1;
566    }
567 
568    ref = find_earliest_backward(priv);
569    if (ref != 0xff) {
570       hdr->ref_frame_idx[AV1_ALTREF2_FRAME - AV1_LAST_FRAME] = ref;
571       hdr->usedFrame[ref] = 1;
572    }
573 
574    for (i = 0; i < AV1_REFS_PER_FRAME - 2; ++i) {
575       uint8_t refFrame = Ref_Frame_List[i];
576       if (hdr->ref_frame_idx[refFrame - AV1_LAST_FRAME] == 0xff) {
577          ref = find_latest_forward(priv);
578          if (ref != 0xff) {
579             hdr->ref_frame_idx[refFrame - AV1_LAST_FRAME] = ref;
580             hdr->usedFrame[ref] = 1;
581          }
582       }
583    }
584 
585    ref = 0xff;
586    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
587       unsigned hint = hdr->shiftedOrderHints[i];
588       if (ref == 0xff || hint < earliestOrderHint) {
589          ref = i;
590          earliestOrderHint = hint;
591       }
592    }
593 
594    for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {
595      if (hdr->ref_frame_idx[i] == 0xff)
596         hdr->ref_frame_idx[i] = ref;
597    }
598 }
599 
frame_size_with_refs(vid_dec_PrivateType * priv,struct vl_vlc * vlc)600 static void frame_size_with_refs(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
601 {
602    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
603    bool found_ref;
604    int i;
605 
606    for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {
607       found_ref = av1_f(vlc, 1);
608       if (found_ref) {
609          hdr->UpscaledWidth =
610             priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefUpscaledWidth;
611          hdr->FrameWidth = hdr->UpscaledWidth;
612          hdr->FrameHeight =
613             priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefFrameHeight;
614          hdr->RenderWidth =
615             priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefRenderWidth;
616          hdr->RenderHeight =
617             priv->codec_data.av1.RefFrames[hdr->ref_frame_idx[i]].RefRenderHeight;
618          break;
619       }
620    }
621 
622    if (!found_ref) {
623       frame_size(priv, vlc);
624       render_size(priv, vlc);
625    } else {
626       superres_params(priv, vlc);
627       compute_image_size(priv);
628    }
629 }
630 
tile_log2(unsigned blkSize,unsigned target)631 static unsigned tile_log2(unsigned blkSize, unsigned target)
632 {
633    unsigned k = 0;
634 
635    for (k = 0; (blkSize << k) < target; k++);
636 
637    return k;
638 }
639 
tile_info(vid_dec_PrivateType * priv,struct vl_vlc * vlc)640 static void tile_info(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
641 {
642    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
643    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
644    struct tile_info *ti = &(priv->codec_data.av1.uncompressed_header.ti);
645    unsigned sbCols;
646    unsigned sbRows;
647    int width_sb;
648    int height_sb;
649    unsigned sbSize;
650    unsigned maxTileWidthSb;
651    unsigned minLog2TileCols;
652    unsigned maxLog2TileCols;
653    unsigned maxLog2TileRows;
654    unsigned minLog2Tiles;
655    bool uniform_tile_spacing_flag;
656    unsigned maxTileAreaSb;
657    unsigned startSb, i;
658 
659    sbCols = (seq->use_128x128_superblock) ?
660       ((hdr->MiCols + 31) >> 5) : ((hdr->MiCols + 15) >> 4);
661    sbRows = (seq->use_128x128_superblock) ?
662       ((hdr->MiRows + 31) >> 5) : ((hdr->MiRows + 15) >> 4);
663    width_sb = sbCols;
664    height_sb = sbRows;
665    sbSize = (seq->use_128x128_superblock ? 5 : 4) + 2;
666    maxTileWidthSb = AV1_MAX_TILE_WIDTH >> sbSize;
667    maxTileAreaSb = AV1_MAX_TILE_AREA >> (2 * sbSize);
668    minLog2TileCols = tile_log2(maxTileWidthSb, sbCols);
669    maxLog2TileCols = tile_log2(1, MIN2(sbCols, AV1_MAX_TILE_COLS));
670    maxLog2TileRows = tile_log2(1, MIN2(sbRows, AV1_MAX_TILE_ROWS));
671    minLog2Tiles = MAX2(minLog2TileCols, tile_log2(maxTileAreaSb, sbRows * sbCols));
672 
673    uniform_tile_spacing_flag = av1_f(vlc, 1);
674    if (uniform_tile_spacing_flag) {
675       unsigned tileWidthSb, tileHeightSb;
676       unsigned minLog2TileRows;
677 
678       ti->TileColsLog2 = minLog2TileCols;
679       while (ti->TileColsLog2 < maxLog2TileCols) {
680          bool increment_tile_cols_log2 = av1_f(vlc, 1);
681          if (increment_tile_cols_log2)
682             ti->TileColsLog2++;
683          else
684             break;
685       }
686       tileWidthSb = (sbCols + (1 << ti->TileColsLog2) - 1) >> ti->TileColsLog2;
687       i = 0;
688       for (startSb = 0; startSb < sbCols; startSb += tileWidthSb) {
689          ti->tile_col_start_sb[i] = startSb;
690          i++;
691       }
692       ti->tile_col_start_sb[i] = sbCols;
693       ti->TileCols = i;
694 
695       minLog2TileRows = (minLog2Tiles > ti->TileColsLog2)?
696          (minLog2Tiles - ti->TileColsLog2) : 0;
697       ti->TileRowsLog2 = minLog2TileRows;
698       while (ti->TileRowsLog2 < maxLog2TileRows) {
699          bool increment_tile_rows_log2 = av1_f(vlc, 1);
700          if (increment_tile_rows_log2)
701             ti->TileRowsLog2++;
702          else
703             break;
704       }
705       tileHeightSb = (sbRows + (1 << ti->TileRowsLog2) - 1) >> ti->TileRowsLog2;
706       i = 0;
707       for (startSb = 0; startSb < sbRows; startSb += tileHeightSb) {
708          ti->tile_row_start_sb[i] = startSb;
709          i++;
710       }
711       ti->tile_row_start_sb[i] = sbRows;
712       ti->TileRows = i;
713    } else {
714       unsigned widestTileSb = 0;
715       unsigned maxTileHeightSb;
716 
717       startSb = 0;
718       for (i = 0; startSb < sbCols; ++i) {
719          uint8_t maxWidth;
720          unsigned sizeSb;
721          unsigned width_in_sbs_minus_1;
722 
723          ti->tile_col_start_sb[i] = startSb;
724          maxWidth = MIN2(sbCols - startSb, maxTileWidthSb);
725          width_in_sbs_minus_1 = av1_ns(vlc, maxWidth);
726          sizeSb = width_in_sbs_minus_1 + 1;
727          widestTileSb = MAX2(sizeSb, widestTileSb);
728          startSb += sizeSb;
729          width_sb -= sizeSb;
730       }
731       ti->TileCols = i;
732 
733       ti->tile_col_start_sb[i] = startSb + width_sb;
734       ti->TileColsLog2 = tile_log2(1, ti->TileCols);
735 
736       if (minLog2Tiles > 0)
737          maxTileAreaSb = (sbRows * sbCols) >> (minLog2Tiles + 1);
738       else
739          maxTileAreaSb = (sbRows * sbCols);
740       maxTileHeightSb = MAX2(maxTileAreaSb / widestTileSb, 1);
741 
742       startSb = 0;
743       for (i = 0; startSb < sbRows; ++i) {
744          uint8_t maxHeight;
745          unsigned height_in_sbs_minus_1;
746 
747          maxHeight = MIN2(sbRows - startSb, maxTileHeightSb);
748          height_in_sbs_minus_1 = av1_ns(vlc, maxHeight);
749          ti->tile_row_start_sb[i] = startSb;
750          startSb += height_in_sbs_minus_1 + 1;
751          height_sb -= height_in_sbs_minus_1 + 1;
752       }
753       ti->TileRows = i;
754       ti->tile_row_start_sb[i] = startSb + height_sb;
755       ti->TileRowsLog2 = tile_log2(1, ti->TileRows);
756    }
757 
758    if (ti->TileColsLog2 > 0 || ti->TileRowsLog2 > 0) {
759       ti->context_update_tile_id =
760          av1_f(vlc, ti->TileRowsLog2 + ti->TileColsLog2);
761       uint8_t tile_size_bytes_minus_1 = av1_f(vlc, 2);
762       ti->TileSizeBytes = tile_size_bytes_minus_1 + 1;
763    } else {
764       ti->context_update_tile_id = 0;
765    }
766 }
767 
read_delta_q(struct vl_vlc * vlc)768 static int read_delta_q(struct vl_vlc *vlc)
769 {
770    bool delta_coded = av1_f(vlc, 1);
771    int delta_q = 0;
772 
773    if (delta_coded)
774       delta_q = av1_su(vlc, 7);
775 
776    return delta_q;
777 }
778 
quantization_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)779 static void quantization_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
780 {
781    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
782    struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp);
783    bool using_qmatrix;
784 
785    qp->base_q_idx = av1_f(vlc, 8);
786    qp->DeltaQYDc = read_delta_q(vlc);
787    if (seq->color_config.NumPlanes > 1) {
788       bool diff_uv_delta =
789          (seq->color_config.separate_uv_delta_q) ? av1_f(vlc, 1) : 0;
790 
791       qp->DeltaQUDc = read_delta_q(vlc);
792       qp->DeltaQUAc = read_delta_q(vlc);
793       if (diff_uv_delta) {
794          qp->DeltaQVDc = read_delta_q(vlc);
795          qp->DeltaQVAc = read_delta_q(vlc);
796       } else {
797          qp->DeltaQVDc = qp->DeltaQUDc;
798          qp->DeltaQVAc = qp->DeltaQUAc;
799       }
800    } else {
801       qp->DeltaQVDc = 0;
802       qp->DeltaQVAc = 0;
803       qp->DeltaQUDc = 0;
804       qp->DeltaQUAc = 0;
805    }
806 
807    using_qmatrix = av1_f(vlc, 1);
808    if (using_qmatrix) {
809       qp->qm_y = av1_f(vlc, 4);
810       qp->qm_u = av1_f(vlc, 4);
811       if (!seq->color_config.separate_uv_delta_q)
812          qp->qm_v = qp->qm_u;
813       else
814          qp->qm_v = av1_f(vlc, 4);
815    } else {
816       qp->qm_y = 0xf;
817       qp->qm_u = 0xf;
818       qp->qm_v = 0xf;
819    }
820 }
821 
segmentation_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)822 static void segmentation_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
823 {
824    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
825    struct segmentation_params* sp = &(priv->codec_data.av1.uncompressed_header.sp);
826    int i, j;
827 
828    sp->segmentation_enabled = av1_f(vlc, 1);
829    if (sp->segmentation_enabled) {
830       bool segmentation_update_data;
831 
832       if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
833          sp->segmentation_update_map = 1;
834          sp->segmentation_temporal_update = 0;
835          segmentation_update_data = 1;
836       } else {
837          sp->segmentation_update_map = av1_f(vlc, 1);
838          if (sp->segmentation_update_map)
839             sp->segmentation_temporal_update = av1_f(vlc, 1);
840          else
841             sp->segmentation_temporal_update = 0;
842          segmentation_update_data = av1_f(vlc, 1);
843       }
844 
845       if (segmentation_update_data) {
846          uint8_t Segmentation_Feature_Bits[AV1_SEG_LVL_MAX] = { 8, 6, 6, 6, 6, 3, 0, 0 };
847          bool Segmentation_Feature_Signed[AV1_SEG_LVL_MAX] = { 1, 1, 1, 1, 1, 0, 0, 0 };
848          unsigned Segmentation_Feature_Max[AV1_SEG_LVL_MAX] = { 255, 63, 63, 63, 63, 7, 0, 0 };
849 
850          memset(sp->FeatureData, 0, sizeof(sp->FeatureData));
851          memset(sp->FeatureMask, 0, sizeof(sp->FeatureMask));
852          for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {
853             for (j = 0; j < AV1_SEG_LVL_MAX; ++j) {
854                int feature_value = 0;
855                bool feature_enabled = av1_f(vlc, 1);
856 
857                sp->FeatureEnabled[i][j] = feature_enabled;
858                int clippedValue = 0;
859                if (feature_enabled) {
860                   uint8_t bitsToRead = Segmentation_Feature_Bits[j];
861                   int limit = Segmentation_Feature_Max[j];
862                   if (Segmentation_Feature_Signed[j]) {
863                      feature_value = av1_su(vlc, 1 + bitsToRead);
864                      clippedValue = CLAMP(feature_value, -limit, limit);
865                      sp->FeatureMask[i] |= 1 << j;
866                   } else {
867                      feature_value = av1_f(vlc, bitsToRead);
868                      clippedValue = CLAMP(feature_value, 0, limit);
869                      sp->FeatureMask[i] |= 1 << j;
870                   }
871                }
872                sp->FeatureData[i][j] = clippedValue;
873             }
874          }
875       } else {
876          int r = hdr->ref_frame_idx[hdr->primary_ref_frame];
877          memcpy(sp, &(priv->codec_data.av1.refs[r].sp), sizeof(*sp));
878       }
879    } else {
880       memset(sp, 0, sizeof(*sp));
881    }
882 }
883 
delta_q_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)884 static void delta_q_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
885 {
886    struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp);
887    struct delta_q_params * dqp = &(priv->codec_data.av1.uncompressed_header.dqp);
888 
889    dqp->delta_q_present = 0;
890    dqp->delta_q_res = 0;
891    if (qp->base_q_idx > 0)
892       dqp->delta_q_present = av1_f(vlc, 1);
893    if (dqp->delta_q_present)
894       dqp->delta_q_res = av1_f(vlc, 2);
895 }
896 
delta_lf_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)897 static void delta_lf_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
898 {
899    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
900    struct delta_q_params * dqp = &(priv->codec_data.av1.uncompressed_header.dqp);
901    struct delta_lf_params* dlfp = &(priv->codec_data.av1.uncompressed_header.dlfp);
902 
903    dlfp->delta_lf_present = 0;
904    dlfp->delta_lf_res = 0;
905    dlfp->delta_lf_multi = 0;
906    if (dqp->delta_q_present) {
907       if (!hdr->allow_intrabc)
908          dlfp->delta_lf_present = av1_f(vlc, 1);
909 
910       if (dlfp->delta_lf_present) {
911          dlfp->delta_lf_res = av1_f(vlc, 2);
912          dlfp->delta_lf_multi = av1_f(vlc, 1);
913       }
914    }
915 }
916 
get_qindex(vid_dec_PrivateType * priv,bool ignoreDeltaQ,unsigned segmentId)917 static unsigned get_qindex(vid_dec_PrivateType * priv, bool ignoreDeltaQ, unsigned segmentId)
918 {
919    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
920    struct segmentation_params* sp = &(priv->codec_data.av1.uncompressed_header.sp);
921    struct quantization_params* qp = &(priv->codec_data.av1.uncompressed_header.qp);
922    unsigned qindex = 0;
923 
924    if (sp->segmentation_enabled && sp->FeatureEnabled[segmentId][AV1_SEG_LVL_ALT_Q]) {
925       unsigned data = sp->FeatureData[segmentId][AV1_SEG_LVL_ALT_Q];
926       qindex = qp->base_q_idx + data;
927       if (!ignoreDeltaQ && hdr->dqp.delta_q_present)
928          qindex = data;
929 
930       return CLAMP(qindex, 0, 255);
931    }
932 
933    if (!ignoreDeltaQ && hdr->dqp.delta_q_present)
934       return 0;
935 
936    return qp->base_q_idx;
937 }
938 
loop_filter_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)939 static void loop_filter_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
940 {
941    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
942    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
943    struct loop_filter_params *lfp = &(priv->codec_data.av1.uncompressed_header.lfp);
944    int i;
945 
946    if (hdr->CodedLossless || hdr->allow_intrabc) {
947       lfp->loop_filter_level[0] = 0;
948       lfp->loop_filter_level[1] = 0;
949       lfp->loop_filter_ref_deltas[AV1_INTRA_FRAME] = 1;
950       lfp->loop_filter_ref_deltas[AV1_LAST_FRAME] = 0;
951       lfp->loop_filter_ref_deltas[AV1_LAST2_FRAME] = 0;
952       lfp->loop_filter_ref_deltas[AV1_LAST3_FRAME] = 0;
953       lfp->loop_filter_ref_deltas[AV1_BWDREF_FRAME] = 0;
954       lfp->loop_filter_ref_deltas[AV1_GOLDEN_FRAME] = -1;
955       lfp->loop_filter_ref_deltas[AV1_ALTREF2_FRAME] = -1;
956       lfp->loop_filter_ref_deltas[AV1_ALTREF_FRAME] = -1;
957       lfp->loop_filter_mode_deltas[0] = 0;
958       lfp->loop_filter_mode_deltas[1] = 0;
959       return;
960    }
961 
962    if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
963       lfp->loop_filter_ref_deltas[AV1_INTRA_FRAME] = 1;
964       lfp->loop_filter_ref_deltas[AV1_LAST_FRAME] = 0;
965       lfp->loop_filter_ref_deltas[AV1_LAST2_FRAME] = 0;
966       lfp->loop_filter_ref_deltas[AV1_LAST3_FRAME] = 0;
967       lfp->loop_filter_ref_deltas[AV1_BWDREF_FRAME] = 0;
968       lfp->loop_filter_ref_deltas[AV1_GOLDEN_FRAME] = -1;
969       lfp->loop_filter_ref_deltas[AV1_ALTREF2_FRAME] = -1;
970       lfp->loop_filter_ref_deltas[AV1_ALTREF_FRAME] = -1;
971       lfp->loop_filter_mode_deltas[0] = 0;
972       lfp->loop_filter_mode_deltas[1] = 0;
973    } else {
974       int r = hdr->ref_frame_idx[hdr->primary_ref_frame];
975       memcpy(lfp->loop_filter_ref_deltas,
976             priv->codec_data.av1.refs[r].lfp.loop_filter_ref_deltas, 8);
977       memcpy(lfp->loop_filter_mode_deltas,
978             priv->codec_data.av1.refs[r].lfp.loop_filter_mode_deltas, 2);
979    }
980 
981    lfp->loop_filter_level[0] = av1_f(vlc, 6);
982    lfp->loop_filter_level[1] = av1_f(vlc, 6);
983    if (seq->color_config.NumPlanes > 1) {
984       if (lfp->loop_filter_level[0] || lfp->loop_filter_level[1]) {
985          lfp->loop_filter_level[2] = av1_f(vlc, 6);
986          lfp->loop_filter_level[3] = av1_f(vlc, 6);
987       }
988    }
989 
990    lfp->loop_filter_sharpness = av1_f(vlc, 3);
991    lfp->loop_filter_delta_enabled = av1_f(vlc, 1);
992    if (lfp->loop_filter_delta_enabled) {
993       lfp->loop_filter_delta_update = av1_f(vlc, 1);
994       if (lfp->loop_filter_delta_update) {
995          for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
996             int8_t update_ref_delta = av1_f(vlc, 1);
997             if (update_ref_delta)
998                lfp->loop_filter_ref_deltas[i] = av1_su(vlc, 7);
999          }
1000 
1001          for (i = 0; i < 2; ++i) {
1002             int8_t update_mode_delta = av1_f(vlc, 1);
1003             if (update_mode_delta)
1004                lfp->loop_filter_mode_deltas[i] = av1_su(vlc, 7);
1005          }
1006       }
1007    }
1008 }
1009 
cdef_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1010 static void cdef_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1011 {
1012    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
1013    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1014    struct cdef_params *cdefp = &(priv->codec_data.av1.uncompressed_header.cdefp);;
1015    int i;
1016 
1017    if (hdr->CodedLossless || hdr->allow_intrabc || !seq->enable_cdef) {
1018       cdefp->cdef_bits = 0;
1019       cdefp->cdef_y_strengths[0] = 0;
1020       cdefp->cdef_uv_strengths[0] = 0;
1021       return;
1022    }
1023 
1024    cdefp->cdef_damping_minus_3 = av1_f(vlc, 2);
1025    cdefp->cdef_bits = av1_f(vlc, 2);
1026    for (i = 0; i < (1 << cdefp->cdef_bits); ++i) {
1027       cdefp->cdef_y_strengths[i] = av1_f(vlc, 6);
1028       if (seq->color_config.NumPlanes > 1)
1029          cdefp->cdef_uv_strengths[i] = av1_f(vlc, 6);
1030    }
1031 }
1032 
lr_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1033 static void lr_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1034 {
1035    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
1036    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1037    struct loop_restoration_params *lrp = &(priv->codec_data.av1.uncompressed_header.lrp);
1038    uint8_t Remap_Lr_Type[4] =
1039       { AV1_RESTORE_NONE, AV1_RESTORE_SWITCHABLE, AV1_RESTORE_WIENER, AV1_RESTORE_SGRPROJ };
1040    bool UsesLr = false;
1041    bool UsesChromaLr = false;
1042    uint8_t lr_unit_shift, lr_uv_shift;
1043    int i;
1044 
1045    if (hdr->AllLossless || hdr->allow_intrabc || !seq->enable_restoration) {
1046       lrp->FrameRestorationType[0] = AV1_RESTORE_NONE;
1047       lrp->FrameRestorationType[1] = AV1_RESTORE_NONE;
1048       lrp->FrameRestorationType[2] = AV1_RESTORE_NONE;
1049       return;
1050    }
1051 
1052    for (i = 0; i < seq->color_config.NumPlanes; ++i) {
1053       uint8_t lr_type = av1_f(vlc, 2);
1054       lrp->FrameRestorationType[i] = Remap_Lr_Type[lr_type];
1055       if (lrp->FrameRestorationType[i] != AV1_RESTORE_NONE) {
1056          UsesLr = true;
1057          if (i > 0)
1058             UsesChromaLr = true;
1059       }
1060    }
1061 
1062    if (UsesLr) {
1063       if (seq->use_128x128_superblock) {
1064          lr_unit_shift = av1_f(vlc, 1) + 1;
1065       } else {
1066          lr_unit_shift = av1_f(vlc, 1);
1067          if (lr_unit_shift) {
1068             uint8_t lr_unit_extra_shift = av1_f(vlc, 1);
1069             lr_unit_shift += lr_unit_extra_shift;
1070          }
1071       }
1072 
1073       lrp->LoopRestorationSize[0] = AV1_RESTORATION_TILESIZE >> (2 - lr_unit_shift);
1074       lr_uv_shift =
1075          (seq->color_config.subsampling_x && seq->color_config.subsampling_y && UsesChromaLr) ?
1076          av1_f(vlc, 1) : 0;
1077 
1078       lrp->LoopRestorationSize[1] = lrp->LoopRestorationSize[0] >> lr_uv_shift;
1079       lrp->LoopRestorationSize[2] = lrp->LoopRestorationSize[0] >> lr_uv_shift;
1080    } else {
1081       lrp->LoopRestorationSize[0] = lrp->LoopRestorationSize[1] =
1082          lrp->LoopRestorationSize[2] = (1 << 8);
1083    }
1084 }
1085 
tx_mode(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1086 static void tx_mode(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1087 {
1088    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1089    struct tx_mode_params *tm = &(priv->codec_data.av1.uncompressed_header.tm);
1090 
1091    if (hdr->CodedLossless) {
1092       tm->TxMode = AV1_ONLY_4X4;
1093    } else {
1094       bool tx_mode_select = av1_f(vlc, 1);
1095       tm->TxMode = (tx_mode_select) ? AV1_TX_MODE_SELECT : AV1_TX_MODE_LARGEST;
1096    }
1097 }
1098 
frame_reference_mode(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1099 static void frame_reference_mode(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1100 {
1101    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1102 
1103    if (hdr->FrameIsIntra)
1104       hdr->reference_select = SINGLE_REFERENCE;
1105    else
1106       hdr->reference_select = av1_f(vlc, 1) ? REFERENCE_MODE_SELECT : SINGLE_REFERENCE;
1107 }
1108 
skip_mode_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1109 static void skip_mode_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1110 {
1111    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
1112    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1113    struct skip_mode_params *smp = &(priv->codec_data.av1.uncompressed_header.smp);;
1114    bool skipModeAllowed;
1115    int i;
1116 
1117    if (hdr->FrameIsIntra || hdr->reference_select == SINGLE_REFERENCE ||
1118          !seq->enable_order_hint) {
1119       skipModeAllowed = 0;
1120    } else {
1121       int ref_frame_offset[2] = { -1, INT_MAX };
1122       int ref_idx[2] = { -1, -1 };
1123 
1124       skipModeAllowed = 0;
1125       for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {
1126          unsigned ref_offset = priv->codec_data.av1.refs[hdr->ref_frame_idx[i]].OrderHint;
1127          if (get_relative_dist(priv, ref_offset, hdr->OrderHint) < 0) {
1128             if (ref_frame_offset[0] == -1 ||
1129                get_relative_dist(priv, ref_offset, ref_frame_offset[0]) > 0) {
1130                ref_frame_offset[0] = ref_offset;
1131                ref_idx[0] = i;
1132             }
1133          } else if (get_relative_dist(priv, ref_offset, hdr->OrderHint) > 0) {
1134             if (ref_frame_offset[1] == INT_MAX ||
1135                get_relative_dist(priv, ref_offset, ref_frame_offset[1]) < 0) {
1136                ref_frame_offset[1] = ref_offset;
1137                ref_idx[1] = i;
1138             }
1139          }
1140       }
1141 
1142       if (ref_idx[0] != -1 && ref_idx[1] != -1) {
1143          skipModeAllowed = 1;
1144       } else if (ref_idx[0] != -1 && ref_idx[1] == -1) {
1145          ref_frame_offset[1] = -1;
1146          for (i = 0; i < AV1_ALTREF_FRAME - AV1_LAST_FRAME + 1; ++i) {
1147             unsigned ref_offset = priv->codec_data.av1.refs[hdr->ref_frame_idx[i]].OrderHint;
1148             if ((ref_frame_offset[0] != -1 &&
1149                 get_relative_dist(priv, ref_offset, ref_frame_offset[0]) < 0) &&
1150                 (ref_frame_offset[1] == -1 ||
1151                 get_relative_dist(priv, ref_offset, ref_frame_offset[1]) > 0)) {
1152                ref_frame_offset[1] = ref_offset;
1153                ref_idx[1] = i;
1154             }
1155          }
1156          if (ref_frame_offset[1] != -1)
1157             skipModeAllowed = 1;
1158       }
1159    }
1160 
1161    smp->skip_mode_present = skipModeAllowed ? av1_f(vlc, 1) : 0;
1162 }
1163 
inverse_recenter(unsigned r,unsigned v)1164 static unsigned inverse_recenter(unsigned r, unsigned v)
1165 {
1166    if (v > (2 * r))
1167       return v;
1168    else if (v & 1)
1169       return (r - ((v + 1) >> 1));
1170    else
1171       return (r + (v >> 1));
1172 }
1173 
decode_subexp(struct vl_vlc * vlc,unsigned numSyms)1174 static unsigned decode_subexp(struct vl_vlc *vlc, unsigned numSyms)
1175 {
1176    unsigned i = 0;
1177    unsigned mk = 0;
1178    unsigned k = 3;
1179 
1180    while (1) {
1181       unsigned b2 = (i) ? (k + i - 1) : k;
1182       unsigned a = 1 << b2;
1183       if (numSyms <= (mk + 3 * a)) {
1184          unsigned subexp_final_bits = av1_ns(vlc, (numSyms - mk));
1185          return (subexp_final_bits + mk);
1186       } else {
1187          bool subexp_more_bits = av1_f(vlc, 1);
1188          if (subexp_more_bits) {
1189             i++;
1190             mk += a;
1191          } else {
1192             unsigned subexp_bits = av1_f(vlc, b2);
1193             return (subexp_bits + mk);
1194          }
1195       }
1196    }
1197 }
1198 
decode_unsigned_subexp_with_ref(struct vl_vlc * vlc,unsigned mx,unsigned r)1199 static unsigned decode_unsigned_subexp_with_ref(struct vl_vlc *vlc,
1200       unsigned mx, unsigned r)
1201 {
1202    unsigned smart;
1203    unsigned v = decode_subexp(vlc, mx);
1204 
1205    if ((r << 1) <= mx) {
1206       smart = inverse_recenter(r, v);
1207       return smart;
1208    } else {
1209       smart = inverse_recenter(mx - 1 - r, v);
1210       return (mx - 1 - smart);
1211    }
1212 }
1213 
decode_signed_subexp_with_ref(struct vl_vlc * vlc,int low,int high,int r)1214 static int decode_signed_subexp_with_ref(struct vl_vlc *vlc, int low, int high, int r)
1215 {
1216    int x = decode_unsigned_subexp_with_ref(vlc, high - low, r - low);
1217 
1218    return (x + low);
1219 }
1220 
read_global_param(struct global_motion_params * global_params,struct global_motion_params * ref_params,vid_dec_PrivateType * priv,struct vl_vlc * vlc,uint8_t type,uint8_t ref,uint8_t idx)1221 static void read_global_param(struct global_motion_params* global_params,
1222                               struct global_motion_params* ref_params,
1223                               vid_dec_PrivateType *priv, struct vl_vlc *vlc,
1224                               uint8_t type, uint8_t ref, uint8_t idx)
1225 {
1226    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1227    uint8_t absBits = 12; /* GM_ABS_ALPHA_BITS */
1228    uint8_t precBits = 15; /* GM_ALPHA_PREC_BITS */
1229    int precDiff, round, sub, mx, r = 0;
1230 
1231    if (idx < 2) {
1232       if (type == AV1_TRANSLATION) {
1233          absBits = 9 /* GM_ABS_TRANS_ONLY_BITS */ - !hdr->allow_high_precision_mv;
1234          precBits = 3 /* GM_TRANS_ONLY_PREC_BITS */ - !hdr->allow_high_precision_mv;
1235       } else {
1236          absBits = 12; /* GM_ABS_TRANS_BITS */
1237          precBits = 6; /* GM_TRANS_PREC_BITS */;
1238       }
1239    }
1240 
1241    precDiff = AV1_WARPEDMODEL_PREC_BITS - precBits;
1242    round = ((idx % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0;
1243    sub = ((idx % 3) == 2) ? (1 << precBits) : 0;
1244    mx = (int)(1 << absBits);
1245    if (ref_params)
1246       r = (ref_params->gm_params[ref][idx] >> precDiff) - sub;
1247 
1248    global_params->gm_params[ref][idx] =
1249       (decode_signed_subexp_with_ref(vlc, -mx, mx + 1, r) << precDiff) + round;
1250 }
1251 
global_motion_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1252 static void global_motion_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1253 {
1254    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1255    struct global_motion_params *gmp = &(priv->codec_data.av1.uncompressed_header.gmp);
1256    struct global_motion_params *ref_gmp = NULL;
1257    unsigned ref, i;
1258 
1259    if (hdr->primary_ref_frame == AV1_PRIMARY_REF_NONE) {
1260       for (ref = 0; ref < AV1_NUM_REF_FRAMES; ++ref) {
1261          gmp->GmType[ref] = AV1_IDENTITY;
1262          for (i = 0; i < 6; ++i)
1263             gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0);
1264       }
1265    } else {
1266       const int r = hdr->ref_frame_idx[hdr->primary_ref_frame];
1267       ref_gmp = &(priv->codec_data.av1.refs[r].gmp);
1268    }
1269 
1270    for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ++ref) {
1271       gmp->GmType[ref] = AV1_IDENTITY;
1272       for (i = 0; i < 6; ++i)
1273          gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0);
1274    }
1275 
1276    if (hdr->FrameIsIntra)
1277       return;
1278 
1279    for (ref = AV1_LAST_FRAME; ref <= AV1_ALTREF_FRAME; ++ref) {
1280       uint8_t type = AV1_IDENTITY;
1281       bool is_global;
1282 
1283       gmp->GmType[ref] = AV1_IDENTITY;
1284       for (i = 0; i < 6; ++i)
1285          gmp->gm_params[ref][i] = (((i % 3) == 2) ? (1 << AV1_WARPEDMODEL_PREC_BITS) : 0);
1286 
1287       is_global = av1_f(vlc, 1);
1288       if (is_global) {
1289          bool is_rot_zoom = av1_f(vlc, 1);
1290          if (is_rot_zoom) {
1291             type = AV1_ROTZOOM;
1292          } else {
1293             bool is_translation = av1_f(vlc, 1);
1294             type = is_translation ? AV1_TRANSLATION : AV1_AFFINE;
1295          }
1296       }
1297 
1298       gmp->GmType[ref] = type;
1299 
1300       if (type >= AV1_ROTZOOM) {
1301          read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 2);
1302          read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 3);
1303          if (type == AV1_AFFINE) {
1304             read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 4);
1305             read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 5);
1306          } else {
1307             gmp->gm_params[ref][4] = -gmp->gm_params[ref][3];
1308             gmp->gm_params[ref][5] = gmp->gm_params[ref][2];
1309          }
1310       }
1311 
1312       if (type >= AV1_TRANSLATION) {
1313          read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 0);
1314          read_global_param(gmp, ref_gmp, priv, vlc, type, ref, 1);
1315       }
1316    }
1317 }
1318 
film_grain_params(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1319 static void film_grain_params(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1320 {
1321    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
1322    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1323    struct film_grain_params *fgp = &(priv->codec_data.av1.uncompressed_header.fgp);
1324 
1325    bool update_grain;
1326    uint8_t numPosLuma;
1327    uint8_t numPosChroma;
1328    unsigned i;
1329 
1330    if (!seq->film_grain_params_present ||
1331          (!hdr->show_frame && !hdr->showable_frame)) {
1332       memset(fgp, 0, sizeof(*fgp));
1333       return;
1334    }
1335 
1336    fgp->apply_grain = av1_f(vlc, 1);
1337    if (!fgp->apply_grain) {
1338       memset(fgp, 0, sizeof(*fgp));
1339       return;
1340    }
1341 
1342    fgp->grain_seed = av1_f(vlc, 16);
1343    update_grain =
1344       (hdr->frame_type == AV1_INTER_FRAME) ? av1_f(vlc, 1) : 1;
1345 
1346    if (!update_grain) {
1347       uint8_t film_grain_params_ref_idx = av1_f(vlc, 3);
1348       uint16_t tempGrainSeed = fgp->grain_seed;
1349       memcpy(fgp, &(priv->codec_data.av1.refs[film_grain_params_ref_idx].fgp),
1350             sizeof(*fgp));
1351       fgp->grain_seed = tempGrainSeed;
1352       return;
1353    }
1354 
1355    fgp->num_y_points = av1_f(vlc, 4);
1356    for (i = 0; i < fgp->num_y_points; ++i) {
1357       fgp->point_y_value[i] = av1_f(vlc, 8);
1358       fgp->point_y_scaling[i] = av1_f(vlc, 8);
1359    }
1360 
1361    fgp->chroma_scaling_from_luma =
1362       (seq->color_config.mono_chrome) ? 0 : av1_f(vlc, 1);
1363    if (seq->color_config.mono_chrome || fgp->chroma_scaling_from_luma ||
1364        (seq->color_config.subsampling_x && seq->color_config.subsampling_y &&
1365        (fgp->num_y_points == 0))) {
1366       fgp->num_cb_points = 0;
1367       fgp->num_cr_points = 0;
1368    } else {
1369       fgp->num_cb_points = av1_f(vlc, 4);
1370       for (i = 0; i < fgp->num_cb_points; ++i) {
1371          fgp->point_cb_value[i] = av1_f(vlc, 8);
1372          fgp->point_cb_scaling[i] = av1_f(vlc, 8);
1373       }
1374       fgp->num_cr_points = av1_f(vlc, 4);
1375       for (i = 0; i < fgp->num_cr_points; ++i) {
1376          fgp->point_cr_value[i] = av1_f(vlc, 8);
1377          fgp->point_cr_scaling[i] = av1_f(vlc, 8);
1378       }
1379    }
1380 
1381    fgp->grain_scaling_minus_8 = av1_f(vlc, 2);
1382    fgp->ar_coeff_lag = av1_f(vlc, 2);
1383    numPosLuma = 2 * fgp->ar_coeff_lag * (fgp->ar_coeff_lag + 1);
1384    if (fgp->num_y_points) {
1385       numPosChroma = numPosLuma + 1;
1386       for (i = 0; i < numPosLuma; ++i) {
1387          uint8_t ar_coeffs_y_plus_128 = av1_f(vlc, 8);
1388          fgp->ar_coeffs_y[i] = ar_coeffs_y_plus_128 - 128;
1389       }
1390    } else {
1391       numPosChroma = numPosLuma;
1392    }
1393 
1394    if (fgp->chroma_scaling_from_luma || fgp->num_cb_points) {
1395       for (i = 0; i < numPosChroma; ++i) {
1396          uint8_t ar_coeffs_cb_plus_128 = av1_f(vlc, 8);
1397          fgp->ar_coeffs_cb[i] = ar_coeffs_cb_plus_128 - 128;
1398       }
1399    }
1400 
1401    if (fgp->chroma_scaling_from_luma || fgp->num_cr_points) {
1402       for (i = 0; i < numPosChroma; ++i) {
1403          uint8_t ar_coeffs_cr_plus_128 = av1_f(vlc, 8);
1404          fgp->ar_coeffs_cr[i] = ar_coeffs_cr_plus_128 - 128;
1405       }
1406    }
1407 
1408    fgp->ar_coeff_shift_minus_6 = av1_f(vlc, 2);
1409    fgp->grain_scale_shift = av1_f(vlc, 2);
1410    if (fgp->num_cb_points) {
1411       fgp->cb_mult = av1_f(vlc, 8);
1412       fgp->cb_luma_mult = av1_f(vlc, 8);
1413       fgp->cb_offset = av1_f(vlc, 9);
1414    }
1415 
1416    if (fgp->num_cr_points) {
1417       fgp->cr_mult = av1_f(vlc, 8);
1418       fgp->cr_luma_mult = av1_f(vlc, 8);
1419       fgp->cr_offset = av1_f(vlc, 9);
1420    }
1421 
1422    fgp->overlap_flag = av1_f(vlc, 1);
1423    fgp->clip_to_restricted_range = av1_f(vlc, 1);
1424 }
1425 
frame_header_obu(vid_dec_PrivateType * priv,struct vl_vlc * vlc)1426 static void frame_header_obu(vid_dec_PrivateType *priv, struct vl_vlc *vlc)
1427 {
1428    struct av1_sequence_header_obu *seq = &(priv->codec_data.av1.seq);
1429    struct av1_uncompressed_header_obu *hdr = &(priv->codec_data.av1.uncompressed_header);
1430    unsigned idLen = 0;
1431    unsigned allFrames;
1432    int i, j;
1433 
1434    memset(hdr, 0, sizeof(*hdr));
1435 
1436    if (seq->frame_id_numbers_present_flag)
1437       idLen = seq->additional_frame_id_length_minus_1 +
1438          seq->delta_frame_id_length_minus_2 + 3;
1439 
1440    allFrames = (1 << AV1_NUM_REF_FRAMES) - 1;
1441    if (seq->reduced_still_picture_header) {
1442       hdr->show_existing_frame = 0;
1443       hdr->frame_type = AV1_KEY_FRAME;
1444       hdr->FrameIsIntra = 1;
1445       hdr->show_frame = 1;
1446       hdr->showable_frame = 0;
1447    } else {
1448       hdr->show_existing_frame = av1_f(vlc, 1);
1449       if (hdr->show_existing_frame) {
1450          hdr->frame_to_show_map_idx = av1_f(vlc, 3);
1451          if (seq->decoder_model_info_present_flag &&
1452                !seq->timing_info.equal_picture_interval)
1453             av1_f(vlc, seq->decoder_model_info.
1454                   frame_presentation_time_length_minus_1 + 1);
1455          hdr->refresh_frame_flags  = 0;
1456          if (seq->frame_id_numbers_present_flag)
1457             av1_f(vlc, idLen); /* display_frame_id */
1458 
1459          hdr->frame_type =
1460             priv->codec_data.av1.RefFrames[priv->codec_data.av1.uncompressed_header.
1461                frame_to_show_map_idx].RefFrameType;
1462 
1463          return;
1464       }
1465 
1466       hdr->frame_type = av1_f(vlc, 2);
1467       hdr->FrameIsIntra = (hdr->frame_type == AV1_INTRA_ONLY_FRAME ||
1468             hdr->frame_type == AV1_KEY_FRAME);
1469       hdr->show_frame = av1_f(vlc, 1);
1470 
1471       if (hdr->show_frame && seq->decoder_model_info_present_flag &&
1472             !seq->timing_info.equal_picture_interval)
1473          av1_f(vlc, seq->decoder_model_info.frame_presentation_time_length_minus_1 + 1);
1474 
1475       hdr->showable_frame =
1476          hdr->show_frame ? (hdr->frame_type != AV1_KEY_FRAME) : av1_f(vlc, 1);
1477 
1478       hdr->error_resilient_mode = (hdr->frame_type == AV1_SWITCH_FRAME ||
1479           (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame)) ? 1 : av1_f(vlc, 1);
1480    }
1481 
1482    if (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame) {
1483       for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
1484          hdr->RefOrderHint[i] = 0;
1485    }
1486 
1487    hdr->disable_cdf_update = av1_f(vlc, 1);
1488 
1489    hdr->allow_screen_content_tools =
1490       (seq->seq_force_screen_content_tools == AV1_SELECT_SCREEN_CONTENT_TOOLS) ?
1491       av1_f(vlc, 1) : seq->seq_force_screen_content_tools;
1492 
1493    if (hdr->allow_screen_content_tools) {
1494       if (seq->seq_force_integer_mv == AV1_SELECT_INTEGER_MV)
1495          hdr->force_integer_mv = av1_f(vlc, 1);
1496       else
1497          hdr->force_integer_mv = seq->seq_force_integer_mv;
1498    } else {
1499       hdr->force_integer_mv = 0;
1500    }
1501 
1502    if (hdr->FrameIsIntra)
1503       hdr->force_integer_mv = 1;
1504 
1505    hdr->current_frame_id =
1506       seq->frame_id_numbers_present_flag ? av1_f(vlc, idLen) : 0;
1507 
1508    if (hdr->frame_type == AV1_SWITCH_FRAME)
1509       hdr->frame_size_override_flag = 1;
1510    else if (seq->reduced_still_picture_header)
1511       hdr->frame_size_override_flag = 0;
1512    else
1513       hdr->frame_size_override_flag = av1_f(vlc, 1);
1514 
1515    hdr->OrderHint = av1_f(vlc, seq->OrderHintBits);
1516 
1517    if (hdr->FrameIsIntra || hdr->error_resilient_mode)
1518       hdr->primary_ref_frame = AV1_PRIMARY_REF_NONE;
1519    else
1520       hdr->primary_ref_frame = av1_f(vlc, 3);
1521 
1522    if (seq->decoder_model_info_present_flag) {
1523       bool buffer_removal_time_present_flag = av1_f(vlc, 1);
1524       if (buffer_removal_time_present_flag) {
1525          for (i = 0; i <= seq->operating_points_cnt_minus_1; ++i) {
1526             if (seq->decoder_model_present_for_this_op[i]) {
1527                unsigned opPtIdc;
1528                bool inTemporalLayer;
1529                bool inSpatialLayer;
1530                opPtIdc = seq->operating_point_idc[i];
1531                inTemporalLayer =
1532                   (opPtIdc >> priv->codec_data.av1.ext.temporal_id) & 1;
1533                inSpatialLayer =
1534                   (opPtIdc >> (priv->codec_data.av1.ext.spatial_id + 8)) & 1;
1535                if ((opPtIdc == 0) || (inTemporalLayer && inSpatialLayer))
1536                   av1_f(vlc, seq->decoder_model_info.
1537                         buffer_removal_time_length_minus_1 + 1);
1538             }
1539          }
1540       }
1541    }
1542 
1543    hdr->allow_high_precision_mv = 0;
1544    hdr->use_ref_frame_mvs = 0;
1545    hdr->allow_intrabc = 0;
1546 
1547    hdr->refresh_frame_flags = allFrames = (hdr->frame_type == AV1_SWITCH_FRAME ||
1548          (hdr->frame_type == AV1_KEY_FRAME && hdr->show_frame)) ?
1549       allFrames : av1_f(vlc, AV1_NUM_REF_FRAMES);
1550 
1551    if (!hdr->FrameIsIntra || hdr->refresh_frame_flags != allFrames) {
1552       if (hdr->error_resilient_mode && seq->enable_order_hint) {
1553          for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
1554             av1_f(vlc, seq->OrderHintBits);
1555       }
1556    }
1557 
1558    if (hdr->FrameIsIntra) {
1559       frame_size(priv, vlc);
1560       render_size(priv, vlc);
1561       if (hdr->allow_screen_content_tools && (hdr->UpscaledWidth == hdr->FrameWidth))
1562          hdr->allow_intrabc = av1_f(vlc, 1);
1563    } else {
1564       bool is_filter_switchable;
1565       bool frame_refs_short_signaling;
1566 
1567       if (!seq->enable_order_hint) {
1568          frame_refs_short_signaling = 0;
1569       } else {
1570          frame_refs_short_signaling = av1_f(vlc, 1);
1571          if (frame_refs_short_signaling) {
1572             hdr->last_frame_idx = av1_f(vlc, 3);
1573             hdr->gold_frame_idx = av1_f(vlc, 3);
1574             set_frame_refs(priv, vlc);
1575          }
1576       }
1577 
1578       for (i = 0; i < AV1_REFS_PER_FRAME; ++i) {
1579          if (!frame_refs_short_signaling)
1580             hdr->ref_frame_idx[i] = av1_f(vlc, 3);
1581          if (seq->frame_id_numbers_present_flag)
1582             av1_f(vlc, seq->delta_frame_id_length_minus_2 + 2);
1583       }
1584 
1585       if (hdr->frame_size_override_flag && !hdr->error_resilient_mode) {
1586          frame_size_with_refs(priv, vlc);
1587       } else {
1588          frame_size(priv, vlc);
1589          render_size(priv, vlc);
1590       }
1591 
1592       hdr->allow_high_precision_mv = hdr->force_integer_mv ? 0 : av1_f(vlc, 1);
1593 
1594       is_filter_switchable = av1_f(vlc, 1);
1595       hdr->interpolation_filter = is_filter_switchable ? 4 /* SWITCHABLE */ : av1_f(vlc, 2);
1596 
1597       hdr->is_motion_mode_switchable = av1_f(vlc, 1);
1598       hdr->use_ref_frame_mvs =
1599          (hdr->error_resilient_mode || !seq->enable_ref_frame_mvs) ? 0 : av1_f(vlc, 1);
1600    }
1601 
1602    hdr->disable_frame_end_update_cdf =
1603       (seq->reduced_still_picture_header || hdr->disable_cdf_update) ? 1 : av1_f(vlc, 1);
1604 
1605    tile_info(priv, vlc);
1606    quantization_params(priv, vlc);
1607    segmentation_params(priv, vlc);
1608    delta_q_params(priv, vlc);
1609    delta_lf_params(priv, vlc);
1610 
1611    hdr->CodedLossless = 1;
1612    for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {
1613       unsigned qindex = get_qindex(priv, 1, i);
1614       bool LosslessArray =
1615          (qindex == 0) && (hdr->qp.DeltaQYDc == 0) &&
1616          (hdr->qp.DeltaQUAc == 0) && (hdr->qp.DeltaQUDc == 0) &&
1617          (hdr->qp.DeltaQVAc == 0) && (hdr->qp.DeltaQVDc == 0);
1618 
1619       if (!LosslessArray)
1620          hdr->CodedLossless = 0;
1621    }
1622    hdr->AllLossless = hdr->CodedLossless && (hdr->FrameWidth == hdr->UpscaledWidth);
1623 
1624    loop_filter_params(priv, vlc);
1625    cdef_params(priv, vlc);
1626    lr_params(priv, vlc);
1627    tx_mode(priv, vlc);
1628    frame_reference_mode(priv, vlc);
1629    skip_mode_params(priv, vlc);
1630 
1631    if (hdr->FrameIsIntra || hdr->error_resilient_mode || !seq->enable_warped_motion)
1632       hdr->allow_warped_motion = 0;
1633    else
1634       hdr->allow_warped_motion = av1_f(vlc, 1);
1635    hdr->reduced_tx_set = av1_f(vlc, 1);
1636 
1637    global_motion_params(priv, vlc);
1638 
1639    film_grain_params(priv, vlc);
1640 
1641    priv->picture.av1.picture_parameter.pic_info_fields.frame_type = hdr->frame_type;
1642    priv->picture.av1.picture_parameter.pic_info_fields.show_frame = hdr->show_frame;
1643    priv->picture.av1.picture_parameter.pic_info_fields.error_resilient_mode =
1644       hdr->error_resilient_mode;
1645    priv->picture.av1.picture_parameter.pic_info_fields.disable_cdf_update =
1646       hdr->disable_cdf_update;
1647    priv->picture.av1.picture_parameter.pic_info_fields.allow_screen_content_tools =
1648       hdr->allow_screen_content_tools;
1649    priv->picture.av1.picture_parameter.pic_info_fields.force_integer_mv =
1650       hdr->force_integer_mv;
1651    priv->picture.av1.picture_parameter.current_frame_id = hdr->current_frame_id;
1652    priv->picture.av1.picture_parameter.order_hint = hdr->OrderHint;
1653    priv->picture.av1.picture_parameter.primary_ref_frame = hdr->primary_ref_frame;
1654    priv->picture.av1.picture_parameter.frame_width = hdr->FrameWidth;
1655    priv->picture.av1.picture_parameter.frame_height = hdr->FrameHeight;
1656    priv->picture.av1.picture_parameter.pic_info_fields.use_superres =
1657       hdr->use_superres;
1658    priv->picture.av1.picture_parameter.superres_scale_denominator =
1659       hdr->SuperresDenom;
1660 
1661    for (i = 0; i < AV1_REFS_PER_FRAME; ++i)
1662       priv->picture.av1.picture_parameter.ref_frame_idx[i] = hdr->ref_frame_idx[i];
1663 
1664    priv->picture.av1.picture_parameter.pic_info_fields.allow_high_precision_mv =
1665       hdr->allow_high_precision_mv;
1666    priv->picture.av1.picture_parameter.pic_info_fields.allow_intrabc = hdr->allow_intrabc;
1667    priv->picture.av1.picture_parameter.pic_info_fields.use_ref_frame_mvs =
1668       hdr->use_ref_frame_mvs;
1669    priv->picture.av1.picture_parameter.interp_filter = hdr->interpolation_filter;
1670    priv->picture.av1.picture_parameter.pic_info_fields.is_motion_mode_switchable =
1671       hdr->is_motion_mode_switchable;
1672    priv->picture.av1.picture_parameter.refresh_frame_flags =
1673       hdr->refresh_frame_flags;
1674    priv->picture.av1.picture_parameter.pic_info_fields.disable_frame_end_update_cdf =
1675       hdr->disable_frame_end_update_cdf;
1676 
1677    /* Tile Info */
1678    priv->picture.av1.picture_parameter.tile_rows = hdr->ti.TileRows;
1679    priv->picture.av1.picture_parameter.tile_cols = hdr->ti.TileCols;
1680    priv->picture.av1.picture_parameter.context_update_tile_id =
1681       hdr->ti.context_update_tile_id;
1682    for (i = 0; i <AV1_MAX_TILE_ROWS; ++i)
1683       priv->picture.av1.picture_parameter.tile_row_start_sb[i] =
1684          hdr->ti.tile_row_start_sb[i];
1685    for (i = 0; i <AV1_MAX_TILE_COLS; ++i)
1686       priv->picture.av1.picture_parameter.tile_col_start_sb[i] =
1687          hdr->ti.tile_col_start_sb[i];
1688 
1689    /* Quantization Params */
1690    priv->picture.av1.picture_parameter.base_qindex =  hdr->qp.base_q_idx;
1691    priv->picture.av1.picture_parameter.y_dc_delta_q = hdr->qp.DeltaQYDc;
1692    priv->picture.av1.picture_parameter.u_dc_delta_q = hdr->qp.DeltaQUDc;
1693    priv->picture.av1.picture_parameter.u_ac_delta_q = hdr->qp.DeltaQUAc;
1694    priv->picture.av1.picture_parameter.v_dc_delta_q = hdr->qp.DeltaQVDc;
1695    priv->picture.av1.picture_parameter.v_ac_delta_q = hdr->qp.DeltaQVAc;
1696    priv->picture.av1.picture_parameter.qmatrix_fields.qm_y = hdr->qp.qm_y;
1697    priv->picture.av1.picture_parameter.qmatrix_fields.qm_u = hdr->qp.qm_u;
1698    priv->picture.av1.picture_parameter.qmatrix_fields.qm_v = hdr->qp.qm_v;
1699 
1700    /* Segmentation Params */
1701    priv->picture.av1.picture_parameter.seg_info.segment_info_fields.enabled =
1702       hdr->sp.segmentation_enabled;
1703    priv->picture.av1.picture_parameter.seg_info.segment_info_fields.update_map =
1704       hdr->sp.segmentation_update_map;
1705    priv->picture.av1.picture_parameter.seg_info.segment_info_fields.temporal_update =
1706       hdr->sp.segmentation_temporal_update;
1707    for (i = 0; i < AV1_MAX_SEGMENTS; ++i) {
1708       for (j = 0; j < AV1_SEG_LVL_MAX; ++j)
1709          priv->picture.av1.picture_parameter.seg_info.feature_data[i][j] =
1710             hdr->sp.FeatureData[i][j];
1711       priv->picture.av1.picture_parameter.seg_info.feature_mask[i] =
1712          hdr->sp.FeatureMask[i];
1713    }
1714 
1715    /* Delta Q Params */
1716    priv->picture.av1.picture_parameter.mode_control_fields.delta_q_present_flag =
1717       hdr->dqp.delta_q_present;
1718    priv->picture.av1.picture_parameter.mode_control_fields.log2_delta_q_res =
1719       hdr->dqp.delta_q_res;
1720 
1721    /* Delta LF Params */
1722    priv->picture.av1.picture_parameter.mode_control_fields.delta_lf_present_flag =
1723       hdr->dlfp.delta_lf_present;
1724    priv->picture.av1.picture_parameter.mode_control_fields.log2_delta_lf_res =
1725       hdr->dlfp.delta_lf_res;
1726    priv->picture.av1.picture_parameter.mode_control_fields.delta_lf_multi =
1727       hdr->dlfp.delta_lf_multi;
1728 
1729    /* Loop Filter Params */
1730    for (i = 0; i < 2; ++i)
1731       priv->picture.av1.picture_parameter.filter_level[i] = hdr->lfp.loop_filter_level[i];
1732    priv->picture.av1.picture_parameter.filter_level_u = hdr->lfp.loop_filter_level[2];
1733    priv->picture.av1.picture_parameter.filter_level_v = hdr->lfp.loop_filter_level[3];
1734    priv->picture.av1.picture_parameter.loop_filter_info_fields.sharpness_level =
1735       hdr->lfp.loop_filter_sharpness;
1736    priv->picture.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_enabled =
1737       hdr->lfp.loop_filter_delta_enabled;
1738    priv->picture.av1.picture_parameter.loop_filter_info_fields.mode_ref_delta_update =
1739       hdr->lfp.loop_filter_delta_update;
1740    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i)
1741       priv->picture.av1.picture_parameter.ref_deltas[i] =
1742          hdr->lfp.loop_filter_ref_deltas[i];
1743    for (i = 0; i < 2; ++i)
1744       priv->picture.av1.picture_parameter.mode_deltas[i] =
1745          hdr->lfp.loop_filter_mode_deltas[i];
1746 
1747    /* CDEF Params */
1748    priv->picture.av1.picture_parameter.cdef_damping_minus_3 =
1749       hdr->cdefp.cdef_damping_minus_3;
1750    priv->picture.av1.picture_parameter.cdef_bits = hdr->cdefp.cdef_bits;
1751    for (i = 0; i < AV1_MAX_CDEF_BITS_ARRAY; ++i) {
1752       priv->picture.av1.picture_parameter.cdef_y_strengths[i] =
1753          hdr->cdefp.cdef_y_strengths[i];
1754       priv->picture.av1.picture_parameter.cdef_uv_strengths[i] =
1755          hdr->cdefp.cdef_uv_strengths[i];
1756    }
1757 
1758    /* Loop Restoration Params */
1759    priv->picture.av1.picture_parameter.loop_restoration_fields.yframe_restoration_type =
1760       hdr->lrp.FrameRestorationType[0];
1761    priv->picture.av1.picture_parameter.loop_restoration_fields.cbframe_restoration_type =
1762       hdr->lrp.FrameRestorationType[1];
1763    priv->picture.av1.picture_parameter.loop_restoration_fields.crframe_restoration_type =
1764       hdr->lrp.FrameRestorationType[2];
1765    for (i = 0; i < 3; ++i)
1766       priv->picture.av1.picture_parameter.lr_unit_size[i] = hdr->lrp.LoopRestorationSize[i];
1767 
1768    priv->picture.av1.picture_parameter.mode_control_fields.tx_mode = hdr->tm.TxMode;
1769    priv->picture.av1.picture_parameter.mode_control_fields.reference_select =
1770       (hdr->reference_select == REFERENCE_MODE_SELECT) ? COMPOUND_REFERENCE : SINGLE_REFERENCE;
1771    priv->picture.av1.picture_parameter.mode_control_fields.skip_mode_present =
1772       hdr->smp.skip_mode_present;
1773    priv->picture.av1.picture_parameter.pic_info_fields.allow_warped_motion =
1774       hdr->allow_warped_motion;
1775    priv->picture.av1.picture_parameter.mode_control_fields.reduced_tx_set_used =
1776       hdr->reduced_tx_set;
1777 
1778    /* Global Motion Params */
1779    for (i = 0; i < 7; ++i) {
1780       priv->picture.av1.picture_parameter.wm[i].wmtype = hdr->gmp.GmType[i + 1];
1781       for (j = 0; j < 6; ++j)
1782          priv->picture.av1.picture_parameter.wm[i].wmmat[j] = hdr->gmp.gm_params[i + 1][j];
1783    }
1784 
1785    /* Film Grain Params */
1786    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.apply_grain =
1787       hdr->fgp.apply_grain;
1788    priv->picture.av1.picture_parameter.film_grain_info.grain_seed =
1789       hdr->fgp.grain_seed;
1790    priv->picture.av1.picture_parameter.film_grain_info.num_y_points =
1791       hdr->fgp.num_y_points;
1792    for (i = 0; i < AV1_FG_MAX_NUM_Y_POINTS; ++i) {
1793       priv->picture.av1.picture_parameter.film_grain_info.point_y_value[i] =
1794          hdr->fgp.point_y_value[i];
1795       priv->picture.av1.picture_parameter.film_grain_info.point_y_scaling[i] =
1796          hdr->fgp.point_y_scaling[i];
1797    }
1798    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1799       chroma_scaling_from_luma = hdr->fgp.chroma_scaling_from_luma;
1800    priv->picture.av1.picture_parameter.film_grain_info.num_cb_points =
1801       hdr->fgp.num_cb_points;
1802    priv->picture.av1.picture_parameter.film_grain_info.num_cr_points =
1803       hdr->fgp.num_cr_points;
1804    for (i = 0; i < AV1_FG_MAX_NUM_CBR_POINTS; ++i) {
1805       priv->picture.av1.picture_parameter.film_grain_info.point_cb_value[i] =
1806          hdr->fgp.point_cb_value[i];
1807       priv->picture.av1.picture_parameter.film_grain_info.point_cb_scaling[i] =
1808          hdr->fgp.point_cb_scaling[i];
1809       priv->picture.av1.picture_parameter.film_grain_info.point_cr_value[i] =
1810          hdr->fgp.point_cr_value[i];
1811       priv->picture.av1.picture_parameter.film_grain_info.point_cr_scaling[i] =
1812          hdr->fgp.point_cr_scaling[i];
1813    }
1814    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1815       grain_scaling_minus_8 = hdr->fgp.grain_scaling_minus_8;
1816    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1817       ar_coeff_lag = hdr->fgp.ar_coeff_lag;
1818    for (i = 0; i < AV1_FG_MAX_NUM_POS_LUMA; ++i)
1819       priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_y[i] =
1820          hdr->fgp.ar_coeffs_y[i];
1821    for (i = 0; i < AV1_FG_MAX_NUM_POS_CHROMA; ++i) {
1822       priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_cb[i] =
1823          hdr->fgp.ar_coeffs_cb[i];
1824       priv->picture.av1.picture_parameter.film_grain_info.ar_coeffs_cr[i] =
1825          hdr->fgp.ar_coeffs_cr[i];
1826    }
1827    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1828       ar_coeff_shift_minus_6 = hdr->fgp.ar_coeff_shift_minus_6;
1829    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1830       grain_scale_shift = hdr->fgp.grain_scale_shift;
1831    priv->picture.av1.picture_parameter.film_grain_info.cb_mult = hdr->fgp.cb_mult;
1832    priv->picture.av1.picture_parameter.film_grain_info.cb_luma_mult = hdr->fgp.cb_luma_mult;
1833    priv->picture.av1.picture_parameter.film_grain_info.cb_offset = hdr->fgp.cb_offset;
1834    priv->picture.av1.picture_parameter.film_grain_info.cr_mult = hdr->fgp.cr_mult;
1835    priv->picture.av1.picture_parameter.film_grain_info.cr_luma_mult = hdr->fgp.cr_luma_mult;
1836    priv->picture.av1.picture_parameter.film_grain_info.cr_offset = hdr->fgp.cr_offset;
1837    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1838       overlap_flag = hdr->fgp.overlap_flag;
1839    priv->picture.av1.picture_parameter.film_grain_info.film_grain_info_fields.
1840       clip_to_restricted_range = hdr->fgp.clip_to_restricted_range;
1841 }
1842 
parse_tile_hdr(vid_dec_PrivateType * priv,struct vl_vlc * vlc,unsigned start_bits_pos,unsigned total_obu_len)1843 static void parse_tile_hdr(vid_dec_PrivateType *priv, struct vl_vlc *vlc,
1844       unsigned start_bits_pos, unsigned total_obu_len)
1845 {
1846    struct tile_info *ti = &(priv->codec_data.av1.uncompressed_header.ti);
1847    unsigned tg_start, tg_end;
1848    unsigned NumTiles, tileBits;
1849    bool tile_start_and_end_present_flag;
1850    unsigned size[AV1_MAX_NUM_TILES] = { 0 };
1851    unsigned offset[AV1_MAX_NUM_TILES] = { 0 };
1852    unsigned frame_header_size, left_size;
1853    unsigned i, j;
1854 
1855    NumTiles = ti->TileCols * ti->TileRows;
1856    tile_start_and_end_present_flag = 0;
1857    if (NumTiles > 1)
1858       tile_start_and_end_present_flag = av1_f(vlc, 1);
1859 
1860    if (NumTiles == 1 || !tile_start_and_end_present_flag) {
1861       tg_start = 0;
1862       tg_end = NumTiles - 1;
1863    } else {
1864       tileBits = ti->TileColsLog2 + ti->TileRowsLog2;
1865       tg_start = av1_f(vlc, tileBits);
1866       tg_end = av1_f(vlc, tileBits);
1867    }
1868 
1869    av1_byte_alignment(vlc);
1870 
1871    frame_header_size = (start_bits_pos - vl_vlc_bits_left(vlc)) / 8;
1872    left_size = total_obu_len - frame_header_size;
1873    for (i = tg_start; i <= tg_end; ++i) {
1874       if (i == tg_start) {
1875          offset[i] = priv->codec_data.av1.bs_obu_td_sz +
1876             priv->codec_data.av1.bs_obu_seq_sz + frame_header_size +
1877             ti->TileSizeBytes;
1878          if (tg_start == tg_end) {
1879             size[i] = left_size;
1880             for (j = 0; j < size[i]; ++j) {
1881                vl_vlc_fillbits(vlc);
1882                vl_vlc_eatbits(vlc, 8);
1883             }
1884             break;
1885          }
1886       } else {
1887          offset[i] = offset[i - 1] + ti->TileSizeBytes + size[i - 1];
1888          left_size -= ti->TileSizeBytes + size[i - 1];
1889       }
1890 
1891       if (i != tg_end) {
1892          size[i] = av1_le(vlc, ti->TileSizeBytes) + 1;
1893       } else {
1894          offset[i] = offset[i - 1] + size[i - 1];
1895          size[i] = left_size;
1896       }
1897 
1898       for (j = 0; j < size[i]; ++j) {
1899          vl_vlc_fillbits(vlc);
1900          vl_vlc_eatbits(vlc, 8);
1901       }
1902    }
1903 
1904    for (i = tg_start; i <= tg_end; ++i) {
1905       priv->picture.av1.slice_parameter.slice_data_offset[i] = offset[i];
1906       priv->picture.av1.slice_parameter.slice_data_size[i] = size[i];
1907    }
1908 }
1909 
dec_av1_NeedTask(vid_dec_PrivateType * priv)1910 static struct dec_av1_task *dec_av1_NeedTask(vid_dec_PrivateType *priv)
1911 {
1912    struct pipe_video_buffer templat = {};
1913    struct dec_av1_task *task;
1914    struct vl_screen *omx_screen;
1915    struct pipe_screen *pscreen;
1916 
1917    omx_screen = priv->screen;
1918    assert(omx_screen);
1919 
1920    pscreen = omx_screen->pscreen;
1921    assert(pscreen);
1922 
1923    if (!list_is_empty(&priv->codec_data.av1.free_tasks)) {
1924       task = list_entry(priv->codec_data.av1.free_tasks.next,
1925                         struct dec_av1_task, list);
1926       task->buf_ref_count = 1;
1927       list_del(&task->list);
1928       return task;
1929    }
1930 
1931    task = CALLOC_STRUCT(dec_av1_task);
1932    if (!task)
1933       return NULL;
1934 
1935    memset(&templat, 0, sizeof(templat));
1936    templat.width = priv->codec->width;
1937    templat.height = priv->codec->height;
1938    templat.buffer_format = pscreen->get_video_param(
1939          pscreen,
1940          PIPE_VIDEO_PROFILE_UNKNOWN,
1941          PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
1942          PIPE_VIDEO_CAP_PREFERED_FORMAT
1943    );
1944    templat.interlaced = false;
1945 
1946    task->buf = priv->pipe->create_video_buffer(priv->pipe, &templat);
1947    if (!task->buf) {
1948       FREE(task);
1949       return NULL;
1950    }
1951    task->buf_ref_count = 1;
1952    task->is_sef_task = false;
1953 
1954    return task;
1955 }
1956 
dec_av1_ReleaseTask(vid_dec_PrivateType * priv,struct list_head * head)1957 static void dec_av1_ReleaseTask(vid_dec_PrivateType *priv,
1958       struct list_head *head)
1959 {
1960    if (!head || !head->next)
1961       return;
1962 
1963    list_for_each_entry_safe(struct dec_av1_task, task, head, list) {
1964       task->buf->destroy(task->buf);
1965       FREE(task);
1966    }
1967 }
1968 
dec_av1_MoveTask(struct list_head * from,struct list_head * to)1969 static void dec_av1_MoveTask(struct list_head *from,
1970       struct list_head *to)
1971 {
1972    to->prev->next = from->next;
1973    from->next->prev = to->prev;
1974    from->prev->next = to;
1975    to->prev = from->prev;
1976    list_inithead(from);
1977 }
1978 
dec_av1_SortTask(vid_dec_PrivateType * priv)1979 static void dec_av1_SortTask(vid_dec_PrivateType *priv)
1980 {
1981    int i;
1982 
1983    list_for_each_entry_safe(struct dec_av1_task, t,
1984          &priv->codec_data.av1.finished_tasks, list) {
1985       bool found = false;
1986       for (i = 0; i < 8; ++i) {
1987          if (t->buf == priv->picture.av1.ref[i]) {
1988             found = true;
1989             break;
1990          }
1991       }
1992       if (!found && t->buf_ref_count == 0) {
1993          list_del(&t->list);
1994          list_addtail(&t->list, &priv->codec_data.av1.free_tasks);
1995       }
1996    }
1997 }
1998 
dec_av1_SearchTask(vid_dec_PrivateType * priv,struct list_head * tasks)1999 static struct dec_av1_task *dec_av1_SearchTask(vid_dec_PrivateType *priv,
2000       struct list_head *tasks)
2001 {
2002    unsigned idx =
2003       priv->codec_data.av1.uncompressed_header.frame_to_show_map_idx;
2004 
2005    list_for_each_entry_safe(struct dec_av1_task, t, tasks, list) {
2006       if (t->buf == priv->picture.av1.ref[idx])
2007          return t;
2008    }
2009 
2010    return NULL;
2011 }
2012 
dec_av1_GetStartedTask(vid_dec_PrivateType * priv,struct dec_av1_task * task,struct list_head * tasks)2013 static bool dec_av1_GetStartedTask(vid_dec_PrivateType *priv,
2014       struct dec_av1_task *task, struct list_head *tasks)
2015 {
2016    struct dec_av1_task *started_task;
2017 
2018    ++priv->codec_data.av1.que_num;
2019    list_addtail(&task->list, &priv->codec_data.av1.started_tasks);
2020    if (priv->codec_data.av1.que_num <= 16)
2021       return false;
2022 
2023    started_task = list_entry(priv->codec_data.av1.started_tasks.next,
2024                              struct dec_av1_task, list);
2025    list_del(&started_task->list);
2026    list_addtail(&started_task->list, tasks);
2027    --priv->codec_data.av1.que_num;
2028 
2029    return true;
2030 }
2031 
dec_av1_ShowExistingframe(vid_dec_PrivateType * priv)2032 static void dec_av1_ShowExistingframe(vid_dec_PrivateType *priv)
2033 {
2034    struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate;
2035    struct dec_av1_task *task, *existing_task;
2036    bool fnd;
2037 
2038    task = CALLOC_STRUCT(dec_av1_task);
2039    if (!task)
2040       return;
2041 
2042    task->is_sef_task = true;
2043 
2044    mtx_lock(&priv->codec_data.av1.mutex);
2045    dec_av1_MoveTask(&inp->tasks, &priv->codec_data.av1.finished_tasks);
2046    dec_av1_SortTask(priv);
2047    existing_task = dec_av1_SearchTask(priv, &priv->codec_data.av1.started_tasks);
2048    if (existing_task) {
2049       ++existing_task->buf_ref_count;
2050       task->buf = existing_task->buf;
2051       task->buf_ref = &existing_task->buf;
2052       task->buf_ref_count = 0;
2053    } else {
2054       existing_task = dec_av1_SearchTask(priv, &priv->codec_data.av1.finished_tasks);
2055       if (existing_task) {
2056          struct vl_screen *omx_screen;
2057          struct pipe_screen *pscreen;
2058          struct pipe_video_buffer templat = {};
2059          struct pipe_video_buffer *buf;
2060          struct pipe_box box={};
2061 
2062          omx_screen = priv->screen;
2063          assert(omx_screen);
2064 
2065          pscreen = omx_screen->pscreen;
2066          assert(pscreen);
2067 
2068          memset(&templat, 0, sizeof(templat));
2069          templat.width = priv->codec->width;
2070          templat.height = priv->codec->height;
2071          templat.buffer_format = pscreen->get_video_param(
2072             pscreen,
2073             PIPE_VIDEO_PROFILE_UNKNOWN,
2074             PIPE_VIDEO_ENTRYPOINT_BITSTREAM,
2075             PIPE_VIDEO_CAP_PREFERED_FORMAT
2076          );
2077          templat.interlaced = false;
2078          buf = priv->pipe->create_video_buffer(priv->pipe, &templat);
2079          if (!buf) {
2080             FREE(task);
2081             mtx_unlock(&priv->codec_data.av1.mutex);
2082             return;
2083          }
2084 
2085          box.width = priv->codec->width;
2086          box.height = priv->codec->height;
2087          box.depth = 1;
2088          priv->pipe->resource_copy_region(priv->pipe,
2089                ((struct vl_video_buffer *)buf)->resources[0],
2090                0, 0, 0, 0,
2091                ((struct vl_video_buffer *)(existing_task->buf))->resources[0],
2092                0, &box);
2093          box.width /= 2;
2094          box.height/= 2;
2095          priv->pipe->resource_copy_region(priv->pipe,
2096                ((struct vl_video_buffer *)buf)->resources[1],
2097                0, 0, 0, 0,
2098                ((struct vl_video_buffer *)(existing_task->buf))->resources[1],
2099                0, &box);
2100          priv->pipe->flush(priv->pipe, NULL, 0);
2101          existing_task->buf_ref_count = 0;
2102          task->buf = buf;
2103          task->buf_ref_count = 1;
2104       } else {
2105          FREE(task);
2106          mtx_unlock(&priv->codec_data.av1.mutex);
2107          return;
2108       }
2109    }
2110    dec_av1_SortTask(priv);
2111 
2112    fnd = dec_av1_GetStartedTask(priv, task, &inp->tasks);
2113    mtx_unlock(&priv->codec_data.av1.mutex);
2114    if (fnd)
2115       priv->frame_finished = 1;
2116 }
2117 
dec_av1_BeginFrame(vid_dec_PrivateType * priv)2118 static struct dec_av1_task *dec_av1_BeginFrame(vid_dec_PrivateType *priv)
2119 {
2120    struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate;
2121    struct dec_av1_task *task;
2122 
2123    if (priv->frame_started)
2124       return NULL;
2125 
2126    if (!priv->codec) {
2127       struct vl_screen *omx_screen;
2128       struct pipe_screen *pscreen;
2129       struct pipe_video_codec templat = {};
2130       bool supported;
2131 
2132       omx_screen = priv->screen;
2133       assert(omx_screen);
2134 
2135       pscreen = omx_screen->pscreen;
2136       assert(pscreen);
2137 
2138       supported = vl_codec_supported(pscreen, priv->profile, false);
2139       assert(supported && "AV1 is not supported");
2140 
2141       templat.profile = priv->profile;
2142       templat.entrypoint = PIPE_VIDEO_ENTRYPOINT_BITSTREAM;
2143       templat.chroma_format = PIPE_VIDEO_CHROMA_FORMAT_420;
2144       templat.max_references = AV1_NUM_REF_FRAMES;
2145       templat.expect_chunked_decode = true;
2146       omx_base_video_PortType *port;
2147       port = (omx_base_video_PortType *)priv->ports[OMX_BASE_FILTER_INPUTPORT_INDEX];
2148       templat.width = port->sPortParam.format.video.nFrameWidth;
2149       templat.height = port->sPortParam.format.video.nFrameHeight;
2150 
2151       priv->codec = priv->pipe->create_video_codec(priv->pipe, &templat);
2152    }
2153 
2154    mtx_lock(&priv->codec_data.av1.mutex);
2155    dec_av1_MoveTask(&inp->tasks, &priv->codec_data.av1.finished_tasks);
2156    dec_av1_SortTask(priv);
2157    mtx_unlock(&priv->codec_data.av1.mutex);
2158 
2159    task = dec_av1_NeedTask(priv);
2160    if (!task)
2161       return NULL;
2162 
2163    priv->codec->begin_frame(priv->codec, task->buf, &priv->picture.base);
2164    priv->frame_started = true;
2165 
2166    return task;
2167 }
2168 
dec_av1_EndFrame(vid_dec_PrivateType * priv,struct dec_av1_task * task)2169 static void dec_av1_EndFrame(vid_dec_PrivateType *priv, struct dec_av1_task *task)
2170 {
2171    struct input_buf_private *inp = priv->in_buffers[0]->pInputPortPrivate;
2172    unsigned refresh_frame_flags;
2173    bool fnd;
2174    unsigned i;
2175 
2176    if (!priv->frame_started || ! task)
2177       return;
2178 
2179    priv->codec->end_frame(priv->codec, task->buf, &priv->picture.base);
2180    priv->frame_started = false;
2181 
2182    refresh_frame_flags = priv->codec_data.av1.uncompressed_header.refresh_frame_flags;
2183    for (i = 0; i < AV1_NUM_REF_FRAMES; ++i) {
2184       if (refresh_frame_flags & (1 << i)) {
2185          memcpy(&priv->codec_data.av1.refs[i], &priv->codec_data.av1.uncompressed_header,
2186             sizeof(struct av1_uncompressed_header_obu));
2187          priv->picture.av1.ref[i] = task->buf;
2188          priv->codec_data.av1.RefFrames[i].RefFrameType =
2189             priv->codec_data.av1.uncompressed_header.frame_type;
2190          priv->codec_data.av1.RefFrames[i].RefFrameId =
2191             priv->codec_data.av1.uncompressed_header.current_frame_id;
2192          priv->codec_data.av1.RefFrames[i].RefUpscaledWidth =
2193             priv->codec_data.av1.uncompressed_header.UpscaledWidth;
2194          priv->codec_data.av1.RefFrames[i].RefFrameWidth =
2195             priv->codec_data.av1.uncompressed_header.FrameWidth;
2196          priv->codec_data.av1.RefFrames[i].RefFrameHeight =
2197             priv->codec_data.av1.uncompressed_header.FrameHeight;
2198          priv->codec_data.av1.RefFrames[i].RefRenderWidth =
2199             priv->codec_data.av1.uncompressed_header.RenderWidth;
2200          priv->codec_data.av1.RefFrames[i].RefRenderHeight =
2201             priv->codec_data.av1.uncompressed_header.RenderHeight;
2202       }
2203    }
2204    if (!priv->picture.av1.picture_parameter.pic_info_fields.show_frame)
2205        task->no_show_frame = true;
2206 
2207    mtx_lock(&priv->codec_data.av1.mutex);
2208    fnd = dec_av1_GetStartedTask(priv, task, &priv->codec_data.av1.decode_tasks);
2209    if (!fnd) {
2210       mtx_unlock(&priv->codec_data.av1.mutex);
2211       return;
2212    }
2213    if (!priv->codec_data.av1.stacked_frame)
2214       dec_av1_MoveTask(&priv->codec_data.av1.decode_tasks, &inp->tasks);
2215    mtx_unlock(&priv->codec_data.av1.mutex);
2216    priv->frame_finished = 1;
2217 }
2218 
dec_av1_Decode(vid_dec_PrivateType * priv,struct vl_vlc * vlc,unsigned min_bits_left)2219 static void dec_av1_Decode(vid_dec_PrivateType *priv, struct vl_vlc *vlc,
2220                            unsigned min_bits_left)
2221 {
2222    unsigned start_bits_pos = vl_vlc_bits_left(vlc);
2223    unsigned start_bits = vl_vlc_valid_bits(vlc);
2224    unsigned start_bytes = start_bits / 8;
2225    const void *obu_data = vlc->data;
2226    uint8_t start_buf[8];
2227    unsigned num_buffers = 0;
2228    void * const * buffers[4];
2229    unsigned sizes[4];
2230    unsigned obu_size = 0;
2231    unsigned total_obu_len;
2232    enum av1_obu_type type;
2233    bool obu_extension_flag;
2234    bool obu_has_size_field;
2235    unsigned i;
2236 
2237    for (i = 0; i < start_bytes; ++i)
2238       start_buf[i] =
2239          vl_vlc_peekbits(vlc, start_bits) >> ((start_bytes - i - 1) * 8);
2240 
2241    /* obu header */
2242    av1_f(vlc, 1); /* obu_forbidden_bit */
2243    type = av1_f(vlc, 4);
2244    obu_extension_flag = av1_f(vlc, 1);
2245    obu_has_size_field = av1_f(vlc, 1);
2246    av1_f(vlc, 1); /* obu_reserved_1bit */
2247    if (obu_extension_flag) {
2248       priv->codec_data.av1.ext.temporal_id = av1_f(vlc, 3);
2249       priv->codec_data.av1.ext.spatial_id = av1_f(vlc, 2);
2250       av1_f(vlc, 3); /* extension_header_reserved_3bits */
2251    }
2252 
2253    obu_size = (obu_has_size_field) ? av1_uleb128(vlc) :
2254               (priv->sizes[0] - (unsigned)obu_extension_flag - 1);
2255    total_obu_len = (start_bits_pos - vl_vlc_bits_left(vlc)) / 8 + obu_size;
2256 
2257    switch (type) {
2258    case AV1_OBU_SEQUENCE_HEADER: {
2259       sequence_header_obu(priv, vlc);
2260       av1_byte_alignment(vlc);
2261       priv->codec_data.av1.bs_obu_seq_sz = total_obu_len;
2262       memcpy(priv->codec_data.av1.bs_obu_seq_buf, start_buf, start_bytes);
2263       memcpy(priv->codec_data.av1.bs_obu_seq_buf + start_bytes, obu_data,
2264              total_obu_len - start_bytes);
2265       break;
2266    }
2267    case AV1_OBU_TEMPORAL_DELIMITER:
2268       av1_byte_alignment(vlc);
2269       priv->codec_data.av1.bs_obu_td_sz = total_obu_len;
2270       memcpy(priv->codec_data.av1.bs_obu_td_buf, start_buf, total_obu_len);
2271       break;
2272    case AV1_OBU_FRAME_HEADER:
2273       frame_header_obu(priv, vlc);
2274       if (priv->codec_data.av1.uncompressed_header.show_existing_frame)
2275          dec_av1_ShowExistingframe(priv);
2276       av1_byte_alignment(vlc);
2277       break;
2278    case AV1_OBU_FRAME: {
2279       struct dec_av1_task *task;
2280 
2281       frame_header_obu(priv, vlc);
2282       av1_byte_alignment(vlc);
2283 
2284       parse_tile_hdr(priv, vlc, start_bits_pos, total_obu_len);
2285       av1_byte_alignment(vlc);
2286 
2287       task = dec_av1_BeginFrame(priv);
2288       if (!task)
2289          return;
2290 
2291       if (priv->codec_data.av1.bs_obu_td_sz) {
2292          buffers[num_buffers] = (void *)priv->codec_data.av1.bs_obu_td_buf;
2293          sizes[num_buffers++] = priv->codec_data.av1.bs_obu_td_sz;
2294          priv->codec_data.av1.bs_obu_td_sz = 0;
2295       }
2296       if (priv->codec_data.av1.bs_obu_seq_sz) {
2297          buffers[num_buffers] = (void *)priv->codec_data.av1.bs_obu_seq_buf;
2298          sizes[num_buffers++] = priv->codec_data.av1.bs_obu_seq_sz;
2299          priv->codec_data.av1.bs_obu_seq_sz = 0;
2300       }
2301       buffers[num_buffers] = (void *)start_buf;
2302       sizes[num_buffers++] = start_bytes;
2303       buffers[num_buffers] = (void *)obu_data;
2304       sizes[num_buffers++] = total_obu_len - start_bytes;
2305 
2306       priv->codec->decode_bitstream(priv->codec, priv->target,
2307          &priv->picture.base, num_buffers, (const void * const*)buffers, sizes);
2308 
2309       priv->codec_data.av1.stacked_frame =
2310             (vl_vlc_bits_left(vlc) > min_bits_left) ? true : false;
2311 
2312       dec_av1_EndFrame(priv, task);
2313       break;
2314    }
2315    default:
2316       av1_byte_alignment(vlc);
2317       break;
2318    }
2319 
2320    return;
2321 }
2322 
vid_dec_av1_AllocateInBuffer(omx_base_PortType * port,OMX_INOUT OMX_BUFFERHEADERTYPE ** buf,OMX_IN OMX_U32 idx,OMX_IN OMX_PTR private,OMX_IN OMX_U32 size)2323 OMX_ERRORTYPE vid_dec_av1_AllocateInBuffer(omx_base_PortType *port,
2324              OMX_INOUT OMX_BUFFERHEADERTYPE **buf, OMX_IN OMX_U32 idx,
2325              OMX_IN OMX_PTR private, OMX_IN OMX_U32 size)
2326 {
2327    struct input_buf_private *inp;
2328    OMX_ERRORTYPE r;
2329 
2330    r = base_port_AllocateBuffer(port, buf, idx, private, size);
2331    if (r)
2332       return r;
2333 
2334    inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
2335    if (!inp) {
2336       base_port_FreeBuffer(port, idx, *buf);
2337       return OMX_ErrorInsufficientResources;
2338    }
2339 
2340    list_inithead(&inp->tasks);
2341 
2342    return OMX_ErrorNone;
2343 }
2344 
vid_dec_av1_UseInBuffer(omx_base_PortType * port,OMX_BUFFERHEADERTYPE ** buf,OMX_U32 idx,OMX_PTR private,OMX_U32 size,OMX_U8 * mem)2345 OMX_ERRORTYPE vid_dec_av1_UseInBuffer(omx_base_PortType *port,
2346              OMX_BUFFERHEADERTYPE **buf, OMX_U32 idx,
2347              OMX_PTR private, OMX_U32 size, OMX_U8 *mem)
2348 {
2349    struct input_buf_private *inp;
2350    OMX_ERRORTYPE r;
2351 
2352    r = base_port_UseBuffer(port, buf, idx, private, size, mem);
2353    if (r)
2354       return r;
2355 
2356    inp = (*buf)->pInputPortPrivate = CALLOC_STRUCT(input_buf_private);
2357    if (!inp) {
2358       base_port_FreeBuffer(port, idx, *buf);
2359       return OMX_ErrorInsufficientResources;
2360    }
2361 
2362    list_inithead(&inp->tasks);
2363 
2364    return OMX_ErrorNone;
2365 }
2366 
vid_dec_av1_FreeInputPortPrivate(vid_dec_PrivateType * priv,OMX_BUFFERHEADERTYPE * buf)2367 void vid_dec_av1_FreeInputPortPrivate(vid_dec_PrivateType *priv,
2368                                       OMX_BUFFERHEADERTYPE *buf)
2369 {
2370    struct input_buf_private *inp = buf->pInputPortPrivate;
2371 
2372    if (!inp || !inp->tasks.next)
2373       return;
2374 
2375    list_for_each_entry_safe(struct dec_av1_task, task, &inp->tasks, list) {
2376       task->buf->destroy(task->buf);
2377       FREE(task);
2378    }
2379 }
2380 
vid_dec_av1_ReleaseTasks(vid_dec_PrivateType * priv)2381 void vid_dec_av1_ReleaseTasks(vid_dec_PrivateType *priv)
2382 {
2383    dec_av1_ReleaseTask(priv, &priv->codec_data.av1.free_tasks);
2384    dec_av1_ReleaseTask(priv, &priv->codec_data.av1.started_tasks);
2385    dec_av1_ReleaseTask(priv, &priv->codec_data.av1.decode_tasks);
2386    dec_av1_ReleaseTask(priv, &priv->codec_data.av1.finished_tasks);
2387    mtx_destroy(&priv->codec_data.av1.mutex);
2388 }
2389 
vid_dec_av1_FrameDecoded(OMX_COMPONENTTYPE * comp,OMX_BUFFERHEADERTYPE * input,OMX_BUFFERHEADERTYPE * output)2390 void vid_dec_av1_FrameDecoded(OMX_COMPONENTTYPE *comp,
2391                               OMX_BUFFERHEADERTYPE* input,
2392                               OMX_BUFFERHEADERTYPE* output)
2393 {
2394    vid_dec_PrivateType *priv = comp->pComponentPrivate;
2395    bool eos = !!(input->nFlags & OMX_BUFFERFLAG_EOS);
2396    struct input_buf_private *inp = input->pInputPortPrivate;
2397    struct dec_av1_task *task;
2398    bool stacked = false;
2399 
2400    mtx_lock(&priv->codec_data.av1.mutex);
2401    if (list_length(&inp->tasks) > 1)
2402       stacked = true;
2403 
2404    if (list_is_empty(&inp->tasks)) {
2405       task = list_entry(priv->codec_data.av1.started_tasks.next,
2406                         struct dec_av1_task, list);
2407       list_del(&task->list);
2408       list_addtail(&task->list, &inp->tasks);
2409       --priv->codec_data.av1.que_num;
2410    }
2411 
2412    task = list_entry(inp->tasks.next, struct dec_av1_task, list);
2413 
2414    if (!task->no_show_frame) {
2415       vid_dec_FillOutput(priv, task->buf, output);
2416       output->nFilledLen = output->nAllocLen;
2417       output->nTimeStamp = input->nTimeStamp;
2418    } else {
2419       task->no_show_frame = false;
2420       output->nFilledLen = 0;
2421    }
2422 
2423    if (task->is_sef_task) {
2424       if (task->buf_ref_count == 0) {
2425          struct dec_av1_task *t = container_of(task->buf_ref, struct dec_av1_task, buf);
2426          list_del(&task->list);
2427          t->buf_ref_count--;
2428          list_del(&t->list);
2429          list_addtail(&t->list, &priv->codec_data.av1.finished_tasks);
2430       } else if (task->buf_ref_count == 1) {
2431          list_del(&task->list);
2432          task->buf->destroy(task->buf);
2433          task->buf_ref_count--;
2434       }
2435       FREE(task);
2436    } else {
2437       if (task->buf_ref_count == 1) {
2438          list_del(&task->list);
2439          list_addtail(&task->list, &priv->codec_data.av1.finished_tasks);
2440          task->buf_ref_count--;
2441       } else if (task->buf_ref_count == 2) {
2442          list_del(&task->list);
2443          task->buf_ref_count--;
2444          list_addtail(&task->list, &priv->codec_data.av1.finished_tasks);
2445       }
2446    }
2447 
2448    if (eos && input->pInputPortPrivate) {
2449       if (!priv->codec_data.av1.que_num)
2450          input->nFilledLen = 0;
2451       else
2452          vid_dec_av1_FreeInputPortPrivate(priv, input);
2453    }
2454    else {
2455       if (!stacked)
2456          input->nFilledLen = 0;
2457    }
2458    mtx_unlock(&priv->codec_data.av1.mutex);
2459 }
2460 
vid_dec_av1_Init(vid_dec_PrivateType * priv)2461 void vid_dec_av1_Init(vid_dec_PrivateType *priv)
2462 {
2463    priv->picture.base.profile = PIPE_VIDEO_PROFILE_AV1_MAIN;
2464    priv->Decode = dec_av1_Decode;
2465    list_inithead(&priv->codec_data.av1.free_tasks);
2466    list_inithead(&priv->codec_data.av1.started_tasks);
2467    list_inithead(&priv->codec_data.av1.decode_tasks);
2468    list_inithead(&priv->codec_data.av1.finished_tasks);
2469    (void)mtx_init(&priv->codec_data.av1.mutex, mtx_plain);
2470 }
2471