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 | |
Evan Shrubsole | 1c51ec4 | 2022-08-08 11:21:26 +0000 | [diff] [blame^] | 14 | #include <cstdint> |
| 15 | #include <deque> |
Ilya Nikolaevskiy | 43c108b | 2020-05-15 12:24:29 +0200 | [diff] [blame] | 16 | #include <string> |
Evan Shrubsole | 1c51ec4 | 2022-08-08 11:21:26 +0000 | [diff] [blame^] | 17 | #include <utility> |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 18 | |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 19 | #include "api/field_trials_view.h" |
Artem Titov | d15a575 | 2021-02-10 14:31:24 +0100 | [diff] [blame] | 20 | #include "api/sequence_checker.h" |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame] | 21 | #include "api/video_codecs/video_decoder.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 22 | #include "modules/video_coding/encoded_frame.h" |
Rasmus Brandt | c4d253c | 2022-05-25 12:03:35 +0200 | [diff] [blame] | 23 | #include "modules/video_coding/timing/timing.h" |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 24 | #include "rtc_base/synchronization/mutex.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 | |
Evan Shrubsole | 1c51ec4 | 2022-08-08 11:21:26 +0000 | [diff] [blame^] | 30 | struct FrameInfo { |
| 31 | FrameInfo() = default; |
| 32 | FrameInfo(const FrameInfo&) = delete; |
| 33 | FrameInfo& operator=(const FrameInfo&) = delete; |
| 34 | FrameInfo(FrameInfo&&) = default; |
| 35 | FrameInfo& operator=(FrameInfo&&) = default; |
| 36 | |
| 37 | uint32_t rtp_timestamp; |
| 38 | // This is likely not optional, but some inputs seem to sometimes be negative. |
| 39 | // TODO(bugs.webrtc.org/13756): See if this can be replaced with Timestamp |
| 40 | // once all inputs to this field use Timestamp instead of an integer. |
| 41 | absl::optional<Timestamp> render_time; |
| 42 | absl::optional<Timestamp> decode_start; |
| 43 | VideoRotation rotation; |
| 44 | VideoContentType content_type; |
| 45 | EncodedImage::Timing timing; |
| 46 | int64_t ntp_time_ms; |
| 47 | RtpPacketInfos packet_infos; |
| 48 | // ColorSpace is not stored here, as it might be modified by decoders. |
| 49 | }; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 50 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 51 | class VCMDecodedFrameCallback : public DecodedImageCallback { |
| 52 | public: |
Jonas Oreland | e02f9ee | 2022-03-25 12:43:14 +0100 | [diff] [blame] | 53 | VCMDecodedFrameCallback(VCMTiming* timing, |
| 54 | Clock* clock, |
Jonas Oreland | e62c2f2 | 2022-03-29 11:04:48 +0200 | [diff] [blame] | 55 | const FieldTrialsView& field_trials); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 56 | ~VCMDecodedFrameCallback() override; |
| 57 | void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback); |
| 58 | VCMReceiveCallback* UserReceiveCallback(); |
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 | int32_t Decoded(VideoFrame& decodedImage) override; |
| 61 | int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; |
| 62 | void Decoded(VideoFrame& decodedImage, |
Danil Chapovalov | 0040b66 | 2018-06-18 10:48:16 +0200 | [diff] [blame] | 63 | absl::optional<int32_t> decode_time_ms, |
| 64 | absl::optional<uint8_t> qp) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 65 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 66 | void OnDecoderImplementationName(const char* implementation_name); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 67 | |
Evan Shrubsole | 1c51ec4 | 2022-08-08 11:21:26 +0000 | [diff] [blame^] | 68 | void Map(FrameInfo frameInfo); |
Johannes Kron | fc5d276 | 2021-04-09 16:03:22 +0200 | [diff] [blame] | 69 | void ClearTimestampMap(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 70 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 71 | private: |
Evan Shrubsole | 1c51ec4 | 2022-08-08 11:21:26 +0000 | [diff] [blame^] | 72 | std::pair<absl::optional<FrameInfo>, size_t> FindFrameInfo( |
| 73 | uint32_t rtp_timestamp) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); |
| 74 | |
Artem Titov | c8421c4 | 2021-02-02 10:57:19 +0100 | [diff] [blame] | 75 | SequenceChecker construction_thread_; |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 76 | Clock* const _clock; |
| 77 | // This callback must be set before the decoder thread starts running |
| 78 | // and must only be unset when external threads (e.g decoder thread) |
| 79 | // have been stopped. Due to that, the variable should regarded as const |
| 80 | // while there are more than one threads involved, it must be set |
| 81 | // from the same thread, and therfore a lock is not required to access it. |
| 82 | VCMReceiveCallback* _receiveCallback = nullptr; |
Lu Liu | 352314a | 2018-02-21 19:38:59 +0000 | [diff] [blame] | 83 | VCMTiming* _timing; |
Markus Handell | 6deec38 | 2020-07-07 12:17:12 +0200 | [diff] [blame] | 84 | Mutex lock_; |
Evan Shrubsole | 1c51ec4 | 2022-08-08 11:21:26 +0000 | [diff] [blame^] | 85 | std::deque<FrameInfo> frame_infos_ RTC_GUARDED_BY(lock_); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 86 | int64_t ntp_offset_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 87 | }; |
| 88 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 89 | class VCMGenericDecoder { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 90 | public: |
Danil Chapovalov | 7b78a31 | 2021-08-06 12:30:02 +0200 | [diff] [blame] | 91 | explicit VCMGenericDecoder(VideoDecoder* decoder); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 92 | ~VCMGenericDecoder(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 93 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 94 | /** |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame] | 95 | * Initialize the decoder with the information from the `settings` |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 96 | */ |
Danil Chapovalov | 355b8d2 | 2021-08-13 16:50:37 +0200 | [diff] [blame] | 97 | bool Configure(const VideoDecoder::Settings& settings); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 98 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 99 | /** |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 100 | * Decode to a raw I420 frame, |
| 101 | * |
| 102 | * inputVideoBuffer reference to encoded video frame |
| 103 | */ |
Johannes Kron | 05f8487 | 2020-01-16 14:09:33 +0100 | [diff] [blame] | 104 | int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 105 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 106 | /** |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 107 | * Set decode callback. Deregistering while decoding is illegal. |
| 108 | */ |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 109 | int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 110 | |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 111 | bool IsSameDecoder(VideoDecoder* decoder) const { |
Danil Chapovalov | 7b78a31 | 2021-08-06 12:30:02 +0200 | [diff] [blame] | 112 | return decoder_ == decoder; |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 113 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 114 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 115 | private: |
Danil Chapovalov | 7b78a31 | 2021-08-06 12:30:02 +0200 | [diff] [blame] | 116 | VCMDecodedFrameCallback* _callback = nullptr; |
| 117 | VideoDecoder* const decoder_; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 118 | VideoContentType _last_keyframe_content_type; |
Erik Språng | c12f625 | 2021-01-13 21:49:59 +0100 | [diff] [blame] | 119 | VideoDecoder::DecoderInfo decoder_info_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 122 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 123 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 124 | #endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |