blob: d4a15d9d9e1f60d17e4882a182cff68e40e6907d [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"
Steve Anton10542f22019-01-11 09:11:00 -080021#include "rtc_base/critical_section.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000023
philipel9d3ab612015-12-21 04:12:39 -080024namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:25 +000025
26class VCMReceiveCallback;
27
28enum { kDecoderFrameMemoryLength = 10 };
29
philipel9d3ab612015-12-21 04:12:39 -080030struct VCMFrameInformation {
31 int64_t renderTimeMs;
32 int64_t decodeStartTimeMs;
33 void* userData;
34 VideoRotation rotation;
ilnik00d802b2017-04-11 10:34:31 -070035 VideoContentType content_type;
ilnik04f4d122017-06-19 07:18:55 -070036 EncodedImage::Timing timing;
niklase@google.com470e71d2011-07-07 08:21:25 +000037};
38
philipel9d3ab612015-12-21 04:12:39 -080039class VCMDecodedFrameCallback : public DecodedImageCallback {
40 public:
41 VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
tommid0a71ba2017-03-14 04:16:20 -070042 ~VCMDecodedFrameCallback() override;
43 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
44 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000045
tommid0a71ba2017-03-14 04:16:20 -070046 int32_t Decoded(VideoFrame& decodedImage) override;
47 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
48 void Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020049 absl::optional<int32_t> decode_time_ms,
50 absl::optional<uint8_t> qp) override;
tommid0a71ba2017-03-14 04:16:20 -070051 int32_t ReceivedDecodedReferenceFrame(const uint64_t pictureId) override;
52 int32_t ReceivedDecodedFrame(const uint64_t pictureId) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000053
tommid0a71ba2017-03-14 04:16:20 -070054 uint64_t LastReceivedPictureID() const;
55 void OnDecoderImplementationName(const char* implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000056
tommid0a71ba2017-03-14 04:16:20 -070057 void Map(uint32_t timestamp, VCMFrameInformation* frameInfo);
58 int32_t Pop(uint32_t timestamp);
niklase@google.com470e71d2011-07-07 08:21:25 +000059
philipel9d3ab612015-12-21 04:12:39 -080060 private:
tommid0a71ba2017-03-14 04:16:20 -070061 rtc::ThreadChecker construction_thread_;
Lu Liu352314a2018-02-21 19:38:59 +000062 // Protect |_timestampMap|.
tommid0a71ba2017-03-14 04:16:20 -070063 Clock* const _clock;
64 // This callback must be set before the decoder thread starts running
65 // and must only be unset when external threads (e.g decoder thread)
66 // have been stopped. Due to that, the variable should regarded as const
67 // while there are more than one threads involved, it must be set
68 // from the same thread, and therfore a lock is not required to access it.
69 VCMReceiveCallback* _receiveCallback = nullptr;
Lu Liu352314a2018-02-21 19:38:59 +000070 VCMTiming* _timing;
71 rtc::CriticalSection lock_;
72 VCMTimestampMap _timestampMap RTC_GUARDED_BY(lock_);
73 uint64_t _lastReceivedPictureID;
ilnik04f4d122017-06-19 07:18:55 -070074 int64_t ntp_offset_;
niklase@google.com470e71d2011-07-07 08:21:25 +000075};
76
philipel9d3ab612015-12-21 04:12:39 -080077class VCMGenericDecoder {
philipel9d3ab612015-12-21 04:12:39 -080078 public:
Magnus Jedvert46a27652017-11-13 14:10:02 +010079 explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder);
philipel9d3ab612015-12-21 04:12:39 -080080 explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
81 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000082
philipel9d3ab612015-12-21 04:12:39 -080083 /**
Yves Gerey665174f2018-06-19 15:03:05 +020084 * Initialize the decoder with the information from the VideoCodec
85 */
philipel9d3ab612015-12-21 04:12:39 -080086 int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +000087
philipel9d3ab612015-12-21 04:12:39 -080088 /**
Yves Gerey665174f2018-06-19 15:03:05 +020089 * Decode to a raw I420 frame,
90 *
91 * inputVideoBuffer reference to encoded video frame
92 */
philipel9d3ab612015-12-21 04:12:39 -080093 int32_t Decode(const VCMEncodedFrame& inputFrame, int64_t nowMs);
niklase@google.com470e71d2011-07-07 08:21:25 +000094
philipel9d3ab612015-12-21 04:12:39 -080095 /**
Yves Gerey665174f2018-06-19 15:03:05 +020096 * Set decode callback. Deregistering while decoding is illegal.
97 */
philipel9d3ab612015-12-21 04:12:39 -080098 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +000099
philipel9d3ab612015-12-21 04:12:39 -0800100 bool PrefersLateDecoding() const;
tommi5b7fc8c2017-07-05 16:45:57 -0700101 bool IsSameDecoder(VideoDecoder* decoder) const {
102 return decoder_.get() == decoder;
103 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000104
philipel9d3ab612015-12-21 04:12:39 -0800105 private:
Lu Liu352314a2018-02-21 19:38:59 +0000106 VCMDecodedFrameCallback* _callback;
107 VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
108 uint32_t _nextFrameInfoIdx;
tommi5b7fc8c2017-07-05 16:45:57 -0700109 std::unique_ptr<VideoDecoder> decoder_;
Lu Liu352314a2018-02-21 19:38:59 +0000110 VideoCodecType _codecType;
tommi5b7fc8c2017-07-05 16:45:57 -0700111 const bool _isExternal;
ilnik00d802b2017-04-11 10:34:31 -0700112 VideoContentType _last_keyframe_content_type;
niklase@google.com470e71d2011-07-07 08:21:25 +0000113};
114
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000115} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000116
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200117#endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_