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