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> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 16 | #include "modules/include/module_common_types.h" |
| 17 | #include "modules/video_coding/encoded_frame.h" |
| 18 | #include "modules/video_coding/include/video_codec_interface.h" |
| 19 | #include "modules/video_coding/timestamp_map.h" |
| 20 | #include "modules/video_coding/timing.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 21 | #include "rtc_base/thread_checker.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 23 | #if defined(WEBRTC_ANDROID) |
| 24 | #include "modules/video_coding/generic_decoder_android.h" // NOLINT |
| 25 | #else |
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; |
| 34 | int64_t decodeStartTimeMs; |
| 35 | void* userData; |
| 36 | VideoRotation rotation; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 37 | VideoContentType content_type; |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 38 | EncodedImage::Timing timing; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 41 | class VCMDecodedFrameCallback : public DecodedImageCallback { |
| 42 | public: |
| 43 | VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 44 | ~VCMDecodedFrameCallback() override; |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 45 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 46 | void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback); |
| 47 | VCMReceiveCallback* UserReceiveCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 48 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 49 | int32_t Decoded(VideoFrame& decodedImage) override; |
| 50 | int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; |
| 51 | void Decoded(VideoFrame& decodedImage, |
| 52 | rtc::Optional<int32_t> decode_time_ms, |
| 53 | rtc::Optional<uint8_t> qp) override; |
| 54 | int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId) override; |
| 55 | int32_t ReceivedDecodedFrame(const uint64_t pictureId) override; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 56 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 57 | uint64_t LastReceivedPictureID() const; |
| 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: |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 64 | rtc::ThreadChecker construction_thread_; |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 65 | rtc::ThreadChecker decoder_thread_; |
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; |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 73 | VCMTiming* _timing RTC_GUARDED_BY(decoder_thread_); |
| 74 | VCMTimestampMap _timestampMap RTC_GUARDED_BY(decoder_thread_); |
| 75 | uint64_t _lastReceivedPictureID RTC_GUARDED_BY(decoder_thread_); |
ilnik | 04f4d12 | 2017-06-19 07:18:55 -0700 | [diff] [blame] | 76 | int64_t ntp_offset_; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | }; |
| 78 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 79 | class VCMGenericDecoder { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 80 | public: |
Magnus Jedvert | 46a2765 | 2017-11-13 14:10:02 +0100 | [diff] [blame] | 81 | explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder); |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 82 | explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false); |
| 83 | ~VCMGenericDecoder(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 84 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 85 | /** |
| 86 | * Initialize the decoder with the information from the VideoCodec |
| 87 | */ |
| 88 | int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 89 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 90 | /** |
| 91 | * Decode to a raw I420 frame, |
| 92 | * |
| 93 | * inputVideoBuffer reference to encoded video frame |
| 94 | */ |
| 95 | int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs); |
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 | /** |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 98 | * Set decode callback. Deregistering while decoding is illegal. |
| 99 | */ |
| 100 | int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback); |
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 | bool PrefersLateDecoding() const; |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 103 | bool IsSameDecoder(VideoDecoder* decoder) const { |
| 104 | return decoder_.get() == decoder; |
| 105 | } |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 106 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 107 | private: |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 108 | rtc::ThreadChecker decoder_thread_; |
| 109 | VCMDecodedFrameCallback* _callback RTC_GUARDED_BY(decoder_thread_); |
| 110 | VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength] RTC_GUARDED_BY( |
| 111 | decoder_thread_); |
| 112 | uint32_t _nextFrameInfoIdx RTC_GUARDED_BY(decoder_thread_); |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 113 | std::unique_ptr<VideoDecoder> decoder_; |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 114 | VideoCodecType _codecType RTC_GUARDED_BY(decoder_thread_); |
tommi | 5b7fc8c | 2017-07-05 16:45:57 -0700 | [diff] [blame] | 115 | const bool _isExternal; |
ilnik | 00d802b | 2017-04-11 10:34:31 -0700 | [diff] [blame] | 116 | VideoContentType _last_keyframe_content_type; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 117 | }; |
| 118 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 119 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 120 | |
Tommi | a4e71b9 | 2018-02-21 09:37:11 +0100 | [diff] [blame] | 121 | #endif // defined(WEBRTC_ANDROID) |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 122 | #endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |