blob: 210a63b50c8db042512313c88329351565e1ac2b [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:25 +00001/*
mikhal@webrtc.orga2031d52012-07-31 15:53:44 +00002 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:25 +00003 *
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MODULES_VIDEO_CODING_GENERIC_DECODER_H_
12#define MODULES_VIDEO_CODING_GENERIC_DECODER_H_
niklase@google.com470e71d2011-07-07 08:21:25 +000013
tommi5b7fc8c2017-07-05 16:45:57 -070014#include <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#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 Bonadei92ea95e2017-09-15 06:47:31 +020021#include "rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000022
Tommia4e71b92018-02-21 09:37:11 +010023#if defined(WEBRTC_ANDROID)
24#include "modules/video_coding/generic_decoder_android.h" // NOLINT
25#else
philipel9d3ab612015-12-21 04:12:39 -080026namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000027
28class VCMReceiveCallback;
29
30enum { kDecoderFrameMemoryLength = 10 };
31
philipel9d3ab612015-12-21 04:12:39 -080032struct VCMFrameInformation {
33 int64_t renderTimeMs;
34 int64_t decodeStartTimeMs;
35 void* userData;
36 VideoRotation rotation;
ilnik00d802b2017-04-11 10:34:31 -070037 VideoContentType content_type;
ilnik04f4d122017-06-19 07:18:55 -070038 EncodedImage::Timing timing;
niklase@google.com470e71d2011-07-07 08:21:25 +000039};
40
philipel9d3ab612015-12-21 04:12:39 -080041class VCMDecodedFrameCallback : public DecodedImageCallback {
42 public:
43 VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
tommid0a71ba2017-03-14 04:16:20 -070044 ~VCMDecodedFrameCallback() override;
Tommia4e71b92018-02-21 09:37:11 +010045
tommid0a71ba2017-03-14 04:16:20 -070046 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
47 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000048
tommid0a71ba2017-03-14 04:16:20 -070049 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.com470e71d2011-07-07 08:21:25 +000056
tommid0a71ba2017-03-14 04:16:20 -070057 uint64_t LastReceivedPictureID() const;
58 void OnDecoderImplementationName(const char* implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000059
tommid0a71ba2017-03-14 04:16:20 -070060 void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
61 int32_t Pop(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000062
philipel9d3ab612015-12-21 04:12:39 -080063 private:
tommid0a71ba2017-03-14 04:16:20 -070064 rtc::ThreadChecker construction_thread_;
Tommia4e71b92018-02-21 09:37:11 +010065 rtc::ThreadChecker decoder_thread_;
tommid0a71ba2017-03-14 04:16:20 -070066 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;
Tommia4e71b92018-02-21 09:37:11 +010073 VCMTiming* _timing RTC_GUARDED_BY(decoder_thread_);
74 VCMTimestampMap _timestampMap RTC_GUARDED_BY(decoder_thread_);
75 uint64_t _lastReceivedPictureID RTC_GUARDED_BY(decoder_thread_);
ilnik04f4d122017-06-19 07:18:55 -070076 int64_t ntp_offset_;
niklase@google.com470e71d2011-07-07 08:21:25 +000077};
78
philipel9d3ab612015-12-21 04:12:39 -080079class VCMGenericDecoder {
philipel9d3ab612015-12-21 04:12:39 -080080 public:
Magnus Jedvert46a27652017-11-13 14:10:02 +010081 explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder);
philipel9d3ab612015-12-21 04:12:39 -080082 explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
83 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000084
philipel9d3ab612015-12-21 04:12:39 -080085 /**
86 * Initialize the decoder with the information from the VideoCodec
87 */
88 int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +000089
philipel9d3ab612015-12-21 04:12:39 -080090 /**
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.com470e71d2011-07-07 08:21:25 +000096
philipel9d3ab612015-12-21 04:12:39 -080097 /**
philipel9d3ab612015-12-21 04:12:39 -080098 * Set decode callback. Deregistering while decoding is illegal.
99 */
100 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
philipel9d3ab612015-12-21 04:12:39 -0800102 bool PrefersLateDecoding() const;
tommi5b7fc8c2017-07-05 16:45:57 -0700103 bool IsSameDecoder(VideoDecoder* decoder) const {
104 return decoder_.get() == decoder;
105 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000106
philipel9d3ab612015-12-21 04:12:39 -0800107 private:
Tommia4e71b92018-02-21 09:37:11 +0100108 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_);
tommi5b7fc8c2017-07-05 16:45:57 -0700113 std::unique_ptr<VideoDecoder> decoder_;
Tommia4e71b92018-02-21 09:37:11 +0100114 VideoCodecType _codecType RTC_GUARDED_BY(decoder_thread_);
tommi5b7fc8c2017-07-05 16:45:57 -0700115 const bool _isExternal;
ilnik00d802b2017-04-11 10:34:31 -0700116 VideoContentType _last_keyframe_content_type;
niklase@google.com470e71d2011-07-07 08:21:25 +0000117};
118
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000119} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000120
Tommia4e71b92018-02-21 09:37:11 +0100121#endif // defined(WEBRTC_ANDROID)
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200122#endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_