xref: /aosp_15_r20/external/libgav1/src/decoder_state.h (revision 095378508e87ed692bf8dfeb34008b65b3735891)
1*09537850SAkhilesh Sanikop /*
2*09537850SAkhilesh Sanikop  * Copyright 2020 The libgav1 Authors
3*09537850SAkhilesh Sanikop  *
4*09537850SAkhilesh Sanikop  * Licensed under the Apache License, Version 2.0 (the "License");
5*09537850SAkhilesh Sanikop  * you may not use this file except in compliance with the License.
6*09537850SAkhilesh Sanikop  * You may obtain a copy of the License at
7*09537850SAkhilesh Sanikop  *
8*09537850SAkhilesh Sanikop  *      http://www.apache.org/licenses/LICENSE-2.0
9*09537850SAkhilesh Sanikop  *
10*09537850SAkhilesh Sanikop  * Unless required by applicable law or agreed to in writing, software
11*09537850SAkhilesh Sanikop  * distributed under the License is distributed on an "AS IS" BASIS,
12*09537850SAkhilesh Sanikop  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*09537850SAkhilesh Sanikop  * See the License for the specific language governing permissions and
14*09537850SAkhilesh Sanikop  * limitations under the License.
15*09537850SAkhilesh Sanikop  */
16*09537850SAkhilesh Sanikop 
17*09537850SAkhilesh Sanikop #ifndef LIBGAV1_SRC_DECODER_STATE_H_
18*09537850SAkhilesh Sanikop #define LIBGAV1_SRC_DECODER_STATE_H_
19*09537850SAkhilesh Sanikop 
20*09537850SAkhilesh Sanikop #include <array>
21*09537850SAkhilesh Sanikop #include <cstdint>
22*09537850SAkhilesh Sanikop 
23*09537850SAkhilesh Sanikop #include "src/buffer_pool.h"
24*09537850SAkhilesh Sanikop #include "src/utils/constants.h"
25*09537850SAkhilesh Sanikop 
26*09537850SAkhilesh Sanikop namespace libgav1 {
27*09537850SAkhilesh Sanikop 
28*09537850SAkhilesh Sanikop struct DecoderState {
29*09537850SAkhilesh Sanikop   // Section 7.20. Updates frames in the reference_frame array with
30*09537850SAkhilesh Sanikop   // |current_frame|, based on the |refresh_frame_flags| bitmask.
UpdateReferenceFramesDecoderState31*09537850SAkhilesh Sanikop   void UpdateReferenceFrames(const RefCountedBufferPtr& current_frame,
32*09537850SAkhilesh Sanikop                              int refresh_frame_flags) {
33*09537850SAkhilesh Sanikop     for (int ref_index = 0, mask = refresh_frame_flags; mask != 0;
34*09537850SAkhilesh Sanikop          ++ref_index, mask >>= 1) {
35*09537850SAkhilesh Sanikop       if ((mask & 1) != 0) {
36*09537850SAkhilesh Sanikop         reference_frame_id[ref_index] = current_frame_id;
37*09537850SAkhilesh Sanikop         reference_frame[ref_index] = current_frame;
38*09537850SAkhilesh Sanikop         reference_order_hint[ref_index] = order_hint;
39*09537850SAkhilesh Sanikop       }
40*09537850SAkhilesh Sanikop     }
41*09537850SAkhilesh Sanikop   }
42*09537850SAkhilesh Sanikop 
43*09537850SAkhilesh Sanikop   // Clears all the reference frames.
ClearReferenceFramesDecoderState44*09537850SAkhilesh Sanikop   void ClearReferenceFrames() {
45*09537850SAkhilesh Sanikop     reference_frame_id = {};
46*09537850SAkhilesh Sanikop     reference_order_hint = {};
47*09537850SAkhilesh Sanikop     for (int ref_index = 0; ref_index < kNumReferenceFrameTypes; ++ref_index) {
48*09537850SAkhilesh Sanikop       reference_frame[ref_index] = nullptr;
49*09537850SAkhilesh Sanikop     }
50*09537850SAkhilesh Sanikop   }
51*09537850SAkhilesh Sanikop 
52*09537850SAkhilesh Sanikop   // reference_frame_id and current_frame_id have meaningful values and are used
53*09537850SAkhilesh Sanikop   // in checks only if sequence_header_.frame_id_numbers_present is true. If
54*09537850SAkhilesh Sanikop   // sequence_header_.frame_id_numbers_present is false, reference_frame_id and
55*09537850SAkhilesh Sanikop   // current_frame_id are assigned the default value 0 and are not used in
56*09537850SAkhilesh Sanikop   // checks.
57*09537850SAkhilesh Sanikop   std::array<uint16_t, kNumReferenceFrameTypes> reference_frame_id = {};
58*09537850SAkhilesh Sanikop   // A valid value of current_frame_id is an unsigned integer of at most 16
59*09537850SAkhilesh Sanikop   // bits. -1 indicates current_frame_id is not initialized.
60*09537850SAkhilesh Sanikop   int current_frame_id = -1;
61*09537850SAkhilesh Sanikop   // The RefOrderHint array variable in the spec.
62*09537850SAkhilesh Sanikop   std::array<uint8_t, kNumReferenceFrameTypes> reference_order_hint = {};
63*09537850SAkhilesh Sanikop   // The OrderHint variable in the spec. Its value comes from either the
64*09537850SAkhilesh Sanikop   // order_hint syntax element in the uncompressed header (if
65*09537850SAkhilesh Sanikop   // show_existing_frame is false) or RefOrderHint[ frame_to_show_map_idx ]
66*09537850SAkhilesh Sanikop   // (if show_existing_frame is true and frame_type is KEY_FRAME). See Section
67*09537850SAkhilesh Sanikop   // 5.9.2 and Section 7.4.
68*09537850SAkhilesh Sanikop   //
69*09537850SAkhilesh Sanikop   // NOTE: When show_existing_frame is false, it is often more convenient to
70*09537850SAkhilesh Sanikop   // just use the order_hint field of the frame header as OrderHint. So this
71*09537850SAkhilesh Sanikop   // field is mainly used to update the reference_order_hint array in
72*09537850SAkhilesh Sanikop   // UpdateReferenceFrames().
73*09537850SAkhilesh Sanikop   uint8_t order_hint = 0;
74*09537850SAkhilesh Sanikop   // reference_frame_sign_bias[i] (a boolean) specifies the intended direction
75*09537850SAkhilesh Sanikop   // of the motion vector in time for each reference frame.
76*09537850SAkhilesh Sanikop   // * |false| indicates that the reference frame is a forwards reference (i.e.
77*09537850SAkhilesh Sanikop   //   the reference frame is expected to be output before the current frame);
78*09537850SAkhilesh Sanikop   // * |true| indicates that the reference frame is a backwards reference.
79*09537850SAkhilesh Sanikop   // Note: reference_frame_sign_bias[0] (for kReferenceFrameIntra) is not used.
80*09537850SAkhilesh Sanikop   std::array<bool, kNumReferenceFrameTypes> reference_frame_sign_bias = {};
81*09537850SAkhilesh Sanikop   // The RefValid[i] variable in the spec does not need to be stored explicitly.
82*09537850SAkhilesh Sanikop   // If the RefValid[i] variable in the spec is 0, then reference_frame[i] is a
83*09537850SAkhilesh Sanikop   // null pointer. (Whenever the spec sets the RefValid[i] variable to 0, we set
84*09537850SAkhilesh Sanikop   // reference_frame[i] to a null pointer.) If the RefValid[i] variable in the
85*09537850SAkhilesh Sanikop   // spec is 1, then reference_frame[i] contains a frame buffer pointer.
86*09537850SAkhilesh Sanikop   std::array<RefCountedBufferPtr, kNumReferenceFrameTypes> reference_frame;
87*09537850SAkhilesh Sanikop };
88*09537850SAkhilesh Sanikop 
89*09537850SAkhilesh Sanikop }  // namespace libgav1
90*09537850SAkhilesh Sanikop 
91*09537850SAkhilesh Sanikop #endif  // LIBGAV1_SRC_DECODER_STATE_H_
92