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