niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 1 | /* |
mikhal@webrtc.org | a2031d5 | 2012-07-31 15:53:44 +0000 | [diff] [blame] | 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |
| 12 | #define MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 13 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 14 | #include <memory> |
Ilya Nikolaevskiy | 43c108b | 2020-05-15 12:24:29 +0200 | [diff] [blame] | 15 | #include <string> |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 16 | |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 17 | #include "api/units/time_delta.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 18 | #include "modules/video_coding/encoded_frame.h" |
| 19 | #include "modules/video_coding/include/video_codec_interface.h" |
| 20 | #include "modules/video_coding/timestamp_map.h" |
| 21 | #include "modules/video_coding/timing.h" |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 22 | #include "rtc_base/experiments/field_trial_parser.h" |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 23 | #include "rtc_base/synchronization/mutex.h" |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame^] | 24 | #include "rtc_base/synchronization/sequence_checker.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 25 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 26 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 27 | |
| 28 | class VCMReceiveCallback; |
| 29 | |
| 30 | enum { kDecoderFrameMemoryLength = 10 }; |
| 31 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 32 | struct VCMFrameInformation { |
| 33 | int64_t renderTimeMs; |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 34 | absl::optional<Timestamp> decodeStart; |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 35 | void* userData; |
| 36 | VideoRotation rotation; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 37 | VideoContentType content_type; |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 38 | PlayoutDelay playout_delay; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 39 | EncodedImage::Timing timing; |
Ilya Nikolaevskiy | 2ebf523 | 2019-05-13 16:13:36 +0200 | [diff] [blame] | 40 | int64_t ntp_time_ms; |
Chen Xing | f00bf42 | 2019-06-20 10:05:55 +0200 | [diff] [blame] | 41 | RtpPacketInfos packet_infos; |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 42 | // ColorSpace is not stored here, as it might be modified by decoders. |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | }; |
| 44 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 45 | class VCMDecodedFrameCallback : public DecodedImageCallback { |
| 46 | public: |
| 47 | VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 48 | ~VCMDecodedFrameCallback() override; |
| 49 | void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback); |
| 50 | VCMReceiveCallback* UserReceiveCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 51 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 52 | int32_t Decoded(VideoFrame& decodedImage) override; |
| 53 | int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; |
| 54 | void Decoded(VideoFrame& decodedImage, |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 55 | absl::optional<int32_t> decode_time_ms, |
| 56 | absl::optional<uint8_t> qp) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 58 | void OnDecoderImplementationName(const char* implementation_name); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 59 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 60 | void Map(uint32_t timestamp, VCMFrameInformation* frameInfo); |
| 61 | int32_t Pop(uint32_t timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 62 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 63 | private: |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame^] | 64 | SequenceChecker construction_thread_; |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 65 | // Protect |_timestampMap|. |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 66 | Clock* const _clock; |
| 67 | // This callback must be set before the decoder thread starts running |
| 68 | // and must only be unset when external threads (e.g decoder thread) |
| 69 | // have been stopped. Due to that, the variable should regarded as const |
| 70 | // while there are more than one threads involved, it must be set |
| 71 | // from the same thread, and therfore a lock is not required to access it. |
| 72 | VCMReceiveCallback* _receiveCallback = nullptr; |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 73 | VCMTiming* _timing; |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 74 | Mutex lock_; |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 75 | VCMTimestampMap _timestampMap RTC_GUARDED_BY(lock_); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 76 | int64_t ntp_offset_; |
Johannes Kron | 7ddea57 | 2019-09-11 12:00:22 +0200 | [diff] [blame] | 77 | // Set by the field trial WebRTC-SlowDownDecoder to simulate a slow decoder. |
| 78 | FieldTrialOptional<TimeDelta> _extra_decode_time; |
Johannes Kron | 111e981 | 2020-10-26 13:54:40 +0100 | [diff] [blame] | 79 | |
| 80 | // Set by the field trial WebRTC-LowLatencyRenderer. The parameter |enabled| |
| 81 | // determines if the low-latency renderer algorithm should be used for the |
| 82 | // case min playout delay=0 and max playout delay>0. |
| 83 | FieldTrialParameter<bool> low_latency_renderer_enabled_; |
| 84 | // Set by the field trial WebRTC-LowLatencyRenderer. The parameter |
| 85 | // |include_predecode_buffer| determines if the predecode buffer should be |
| 86 | // taken into account when calculating maximum number of frames in composition |
| 87 | // queue. |
| 88 | FieldTrialParameter<bool> low_latency_renderer_include_predecode_buffer_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | }; |
| 90 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 91 | class VCMGenericDecoder { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 92 | public: |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 93 | explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 94 | explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false); |
| 95 | ~VCMGenericDecoder(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 97 | /** |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 98 | * Initialize the decoder with the information from the VideoCodec |
| 99 | */ |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 100 | int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 101 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 102 | /** |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 103 | * Decode to a raw I420 frame, |
| 104 | * |
| 105 | * inputVideoBuffer reference to encoded video frame |
| 106 | */ |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 107 | int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 108 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | /** |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 110 | * Set decode callback. Deregistering while decoding is illegal. |
| 111 | */ |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 112 | int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 113 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 114 | bool IsSameDecoder(VideoDecoder* decoder) const { |
| 115 | return decoder_.get() == decoder; |
| 116 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 118 | private: |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 119 | VCMDecodedFrameCallback* _callback; |
| 120 | VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength]; |
| 121 | uint32_t _nextFrameInfoIdx; |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 122 | std::unique_ptr<VideoDecoder> decoder_; |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 123 | VideoCodecType _codecType; |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 124 | const bool _isExternal; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 125 | VideoContentType _last_keyframe_content_type; |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 126 | VideoDecoder::DecoderInfo decoder_info_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 127 | }; |
| 128 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 129 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 130 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 131 | #endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |