generic_decoder.h (5352B)
1 /* 2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef MODULES_VIDEO_CODING_GENERIC_DECODER_H_ 12 #define MODULES_VIDEO_CODING_GENERIC_DECODER_H_ 13 14 #include <cstddef> 15 #include <cstdint> 16 #include <deque> 17 #include <optional> 18 #include <utility> 19 20 #include "api/field_trials_view.h" 21 #include "api/rtp_packet_infos.h" 22 #include "api/sequence_checker.h" 23 #include "api/units/timestamp.h" 24 #include "api/video/color_space.h" 25 #include "api/video/corruption_detection/frame_instrumentation_data.h" 26 #include "api/video/encoded_frame.h" 27 #include "api/video/encoded_image.h" 28 #include "api/video/video_content_type.h" 29 #include "api/video/video_frame.h" 30 #include "api/video/video_frame_type.h" 31 #include "api/video/video_rotation.h" 32 #include "api/video_codecs/video_decoder.h" 33 #include "common_video/include/corruption_score_calculator.h" 34 #include "modules/video_coding/encoded_frame.h" 35 #include "modules/video_coding/timing/timing.h" 36 #include "rtc_base/synchronization/mutex.h" 37 #include "rtc_base/thread_annotations.h" 38 #include "system_wrappers/include/clock.h" 39 40 namespace webrtc { 41 42 class VCMReceiveCallback; 43 44 struct FrameInfo { 45 FrameInfo() = default; 46 FrameInfo(const FrameInfo&) = delete; 47 FrameInfo& operator=(const FrameInfo&) = delete; 48 FrameInfo(FrameInfo&&) = default; 49 FrameInfo& operator=(FrameInfo&&) = default; 50 51 uint32_t rtp_timestamp; 52 // This is likely not optional, but some inputs seem to sometimes be negative. 53 // TODO(bugs.webrtc.org/13756): See if this can be replaced with Timestamp 54 // once all inputs to this field use Timestamp instead of an integer. 55 std::optional<Timestamp> render_time; 56 std::optional<Timestamp> decode_start; 57 VideoRotation rotation; 58 VideoContentType content_type; 59 EncodedImage::Timing timing; 60 int64_t ntp_time_ms; 61 RtpPacketInfos packet_infos; 62 // ColorSpace is not stored here, as it might be modified by decoders. 63 VideoFrameType frame_type; 64 std::optional<FrameInstrumentationData> frame_instrumentation_data; 65 std::optional<ColorSpace> color_space; 66 }; 67 68 class VCMDecodedFrameCallback : public DecodedImageCallback { 69 public: 70 VCMDecodedFrameCallback( 71 VCMTiming* timing, 72 Clock* clock, 73 const FieldTrialsView& field_trials, 74 CorruptionScoreCalculator* corruption_score_calculator); 75 ~VCMDecodedFrameCallback() override; 76 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback); 77 VCMReceiveCallback* UserReceiveCallback(); 78 79 int32_t Decoded(VideoFrame& decodedImage) override; 80 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; 81 void Decoded(VideoFrame& decodedImage, 82 std::optional<int32_t> decode_time_ms, 83 std::optional<uint8_t> qp) override; 84 85 void OnDecoderInfoChanged(const VideoDecoder::DecoderInfo& decoder_info); 86 87 void Map(FrameInfo frameInfo); 88 void ClearTimestampMap(); 89 90 private: 91 std::pair<std::optional<FrameInfo>, size_t> FindFrameInfo( 92 uint32_t rtp_timestamp) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); 93 94 SequenceChecker construction_thread_; 95 Clock* const _clock; 96 // This callback must be set before the decoder thread starts running 97 // and must only be unset when external threads (e.g decoder thread) 98 // have been stopped. Due to that, the variable should regarded as const 99 // while there are more than one threads involved, it must be set 100 // from the same thread, and therfore a lock is not required to access it. 101 VCMReceiveCallback* _receiveCallback = nullptr; 102 VCMTiming* _timing; 103 Mutex lock_; 104 std::deque<FrameInfo> frame_infos_ RTC_GUARDED_BY(lock_); 105 int64_t ntp_offset_; 106 CorruptionScoreCalculator* const corruption_score_calculator_; 107 }; 108 109 class VCMGenericDecoder { 110 public: 111 explicit VCMGenericDecoder(VideoDecoder* decoder); 112 ~VCMGenericDecoder(); 113 114 /** 115 * Initialize the decoder with the information from the `settings` 116 */ 117 bool Configure(const VideoDecoder::Settings& settings); 118 119 /** 120 * Decode to a raw I420 frame, 121 * 122 * inputVideoBuffer reference to encoded video frame 123 */ 124 // TODO(https://bugs.webrtc.org/9378): Remove VCMEncodedFrame variant 125 // once the usage from code in deprecated/ is gone. 126 int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now); 127 int32_t Decode(const EncodedFrame& inputFrame, Timestamp now); 128 129 /** 130 * Set decode callback. Deregistering while decoding is illegal. 131 */ 132 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback); 133 134 bool IsSameDecoder(VideoDecoder* decoder) const { 135 return decoder_ == decoder; 136 } 137 138 private: 139 int32_t Decode(const EncodedImage& frame, 140 Timestamp now, 141 int64_t render_time_ms, 142 const std::optional<FrameInstrumentationData>& 143 frame_instrumentation_data); 144 VCMDecodedFrameCallback* _callback = nullptr; 145 VideoDecoder* const decoder_; 146 VideoContentType _last_keyframe_content_type; 147 VideoDecoder::DecoderInfo decoder_info_; 148 }; 149 150 } // namespace webrtc 151 152 #endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_