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 | |
| 11 | #ifndef WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |
| 12 | #define WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |
| 13 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame^] | 14 | #include <memory> |
| 15 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 16 | #include "webrtc/base/thread_checker.h" |
Henrik Kjellander | ff761fb | 2015-11-04 08:31:52 +0100 | [diff] [blame] | 17 | #include "webrtc/modules/include/module_common_types.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 18 | #include "webrtc/modules/video_coding/encoded_frame.h" |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 19 | #include "webrtc/modules/video_coding/include/video_codec_interface.h" |
Henrik Kjellander | 2557b86 | 2015-11-18 22:00:21 +0100 | [diff] [blame] | 20 | #include "webrtc/modules/video_coding/timestamp_map.h" |
| 21 | #include "webrtc/modules/video_coding/timing.h" |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 22 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 23 | namespace webrtc { |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 24 | |
| 25 | class VCMReceiveCallback; |
| 26 | |
| 27 | enum { kDecoderFrameMemoryLength = 10 }; |
| 28 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 29 | struct VCMFrameInformation { |
| 30 | int64_t renderTimeMs; |
| 31 | int64_t decodeStartTimeMs; |
| 32 | void* userData; |
| 33 | VideoRotation rotation; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 34 | }; |
| 35 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 36 | class VCMDecodedFrameCallback : public DecodedImageCallback { |
| 37 | public: |
| 38 | VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock); |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 39 | ~VCMDecodedFrameCallback() override; |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame^] | 40 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 41 | void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback); |
| 42 | VCMReceiveCallback* UserReceiveCallback(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 43 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 44 | int32_t Decoded(VideoFrame& decodedImage) override; |
| 45 | int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override; |
| 46 | void Decoded(VideoFrame& decodedImage, |
| 47 | rtc::Optional<int32_t> decode_time_ms, |
| 48 | rtc::Optional<uint8_t> qp) override; |
| 49 | int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId) override; |
| 50 | int32_t ReceivedDecodedFrame(const uint64_t pictureId) override; |
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 | uint64_t LastReceivedPictureID() const; |
| 53 | void OnDecoderImplementationName(const char* implementation_name); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 54 | |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 55 | void Map(uint32_t timestamp, VCMFrameInformation* frameInfo); |
| 56 | int32_t Pop(uint32_t timestamp); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 57 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 58 | private: |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 59 | rtc::ThreadChecker construction_thread_; |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame^] | 60 | rtc::ThreadChecker decoder_thread_; |
tommi | d0a71ba | 2017-03-14 04:16:20 -0700 | [diff] [blame] | 61 | Clock* const _clock; |
| 62 | // This callback must be set before the decoder thread starts running |
| 63 | // and must only be unset when external threads (e.g decoder thread) |
| 64 | // have been stopped. Due to that, the variable should regarded as const |
| 65 | // while there are more than one threads involved, it must be set |
| 66 | // from the same thread, and therfore a lock is not required to access it. |
| 67 | VCMReceiveCallback* _receiveCallback = nullptr; |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame^] | 68 | VCMTiming* _timing ACCESS_ON(decoder_thread_); |
| 69 | VCMTimestampMap _timestampMap ACCESS_ON(decoder_thread_); |
| 70 | uint64_t _lastReceivedPictureID ACCESS_ON(decoder_thread_); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 71 | }; |
| 72 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 73 | class VCMGenericDecoder { |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 74 | public: |
| 75 | explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false); |
| 76 | ~VCMGenericDecoder(); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 77 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 78 | /** |
| 79 | * Initialize the decoder with the information from the VideoCodec |
| 80 | */ |
| 81 | int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 82 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 83 | /** |
| 84 | * Decode to a raw I420 frame, |
| 85 | * |
| 86 | * inputVideoBuffer reference to encoded video frame |
| 87 | */ |
| 88 | int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs); |
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 | /** |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 91 | * Set decode callback. Deregistering while decoding is illegal. |
| 92 | */ |
| 93 | int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback); |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 94 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 95 | bool PrefersLateDecoding() const; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 96 | |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame^] | 97 | #if defined(WEBRTC_ANDROID) |
| 98 | // See https://bugs.chromium.org/p/webrtc/issues/detail?id=7361 |
| 99 | void PollDecodedFrames(); |
| 100 | #endif |
| 101 | |
| 102 | bool IsSameDecoder(VideoDecoder* decoder) const { |
| 103 | return decoder_.get() == decoder; |
| 104 | } |
| 105 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 106 | private: |
tommi | e3aa88b | 2017-04-04 03:53:02 -0700 | [diff] [blame^] | 107 | rtc::ThreadChecker decoder_thread_; |
| 108 | VCMDecodedFrameCallback* _callback ACCESS_ON(decoder_thread_); |
| 109 | VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength] ACCESS_ON( |
| 110 | decoder_thread_); |
| 111 | uint32_t _nextFrameInfoIdx ACCESS_ON(decoder_thread_); |
| 112 | std::unique_ptr<VideoDecoder> decoder_; |
| 113 | VideoCodecType _codecType ACCESS_ON(decoder_thread_); |
| 114 | const bool _isExternal; |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
pbos@webrtc.org | d900e8b | 2013-07-03 15:12:26 +0000 | [diff] [blame] | 117 | } // namespace webrtc |
niklase@google.com | 470e71d | 2011-07-07 08:21:25 +0000 | [diff] [blame] | 118 | |
philipel | 9d3ab61 | 2015-12-21 04:12:39 -0800 | [diff] [blame] | 119 | #endif // WEBRTC_MODULES_VIDEO_CODING_GENERIC_DECODER_H_ |