blob: 9524bab99b5568830144cdf036c4dd4cf33bb25c [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>
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +020015#include <string>
tommi5b7fc8c2017-07-05 16:45:57 -070016
Johannes Kron7ddea572019-09-11 12:00:22 +020017#include "api/units/time_delta.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "modules/video_coding/encoded_frame.h"
19#include "modules/video_coding/include/video_codec_interface.h"
20#include "modules/video_coding/timestamp_map.h"
21#include "modules/video_coding/timing.h"
Johannes Kron7ddea572019-09-11 12:00:22 +020022#include "rtc_base/experiments/field_trial_parser.h"
Markus Handell6deec382020-07-07 12:17:12 +020023#include "rtc_base/synchronization/mutex.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020024#include "rtc_base/thread_checker.h"
niklase@google.com470e71d2011-07-07 08:21:25 +000025
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;
Johannes Kron05f84872020-01-16 14:09:33 +010034 absl::optional<Timestamp> decodeStart;
philipel9d3ab612015-12-21 04:12:39 -080035 void* userData;
36 VideoRotation rotation;
ilnik00d802b2017-04-11 10:34:31 -070037 VideoContentType content_type;
Johannes Kron111e9812020-10-26 13:54:40 +010038 PlayoutDelay playout_delay;
ilnik04f4d122017-06-19 07:18:55 -070039 EncodedImage::Timing timing;
Ilya Nikolaevskiy2ebf5232019-05-13 16:13:36 +020040 int64_t ntp_time_ms;
Chen Xingf00bf422019-06-20 10:05:55 +020041 RtpPacketInfos packet_infos;
Johannes Kron05f84872020-01-16 14:09:33 +010042 // ColorSpace is not stored here, as it might be modified by decoders.
niklase@google.com470e71d2011-07-07 08:21:25 +000043};
44
philipel9d3ab612015-12-21 04:12:39 -080045class VCMDecodedFrameCallback : public DecodedImageCallback {
46 public:
47 VCMDecodedFrameCallback(VCMTiming* timing, Clock* clock);
tommid0a71ba2017-03-14 04:16:20 -070048 ~VCMDecodedFrameCallback() override;
49 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
50 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000051
tommid0a71ba2017-03-14 04:16:20 -070052 int32_t Decoded(VideoFrame& decodedImage) override;
53 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
54 void Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020055 absl::optional<int32_t> decode_time_ms,
56 absl::optional<uint8_t> qp) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000057
tommid0a71ba2017-03-14 04:16:20 -070058 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_;
Lu Liu352314a2018-02-21 19:38:59 +000065 // Protect |_timestampMap|.
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;
Lu Liu352314a2018-02-21 19:38:59 +000073 VCMTiming* _timing;
Markus Handell6deec382020-07-07 12:17:12 +020074 Mutex lock_;
Lu Liu352314a2018-02-21 19:38:59 +000075 VCMTimestampMap _timestampMap RTC_GUARDED_BY(lock_);
ilnik04f4d122017-06-19 07:18:55 -070076 int64_t ntp_offset_;
Johannes Kron7ddea572019-09-11 12:00:22 +020077 // Set by the field trial WebRTC-SlowDownDecoder to simulate a slow decoder.
78 FieldTrialOptional<TimeDelta> _extra_decode_time;
Johannes Kron111e9812020-10-26 13:54:40 +010079
80 // Set by the field trial WebRTC-LowLatencyRenderer. The parameter |enabled|
81 // determines if the low-latency renderer algorithm should be used for the
82 // case min playout delay=0 and max playout delay>0.
83 FieldTrialParameter<bool> low_latency_renderer_enabled_;
84 // Set by the field trial WebRTC-LowLatencyRenderer. The parameter
85 // |include_predecode_buffer| determines if the predecode buffer should be
86 // taken into account when calculating maximum number of frames in composition
87 // queue.
88 FieldTrialParameter<bool> low_latency_renderer_include_predecode_buffer_;
niklase@google.com470e71d2011-07-07 08:21:25 +000089};
90
philipel9d3ab612015-12-21 04:12:39 -080091class VCMGenericDecoder {
philipel9d3ab612015-12-21 04:12:39 -080092 public:
Magnus Jedvert46a27652017-11-13 14:10:02 +010093 explicit VCMGenericDecoder(std::unique_ptr<VideoDecoder> decoder);
philipel9d3ab612015-12-21 04:12:39 -080094 explicit VCMGenericDecoder(VideoDecoder* decoder, bool isExternal = false);
95 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000096
philipel9d3ab612015-12-21 04:12:39 -080097 /**
Yves Gerey665174f2018-06-19 15:03:05 +020098 * Initialize the decoder with the information from the VideoCodec
99 */
philipel9d3ab612015-12-21 04:12:39 -0800100 int32_t InitDecode(const VideoCodec* settings, int32_t numberOfCores);
niklase@google.com470e71d2011-07-07 08:21:25 +0000101
philipel9d3ab612015-12-21 04:12:39 -0800102 /**
Yves Gerey665174f2018-06-19 15:03:05 +0200103 * Decode to a raw I420 frame,
104 *
105 * inputVideoBuffer reference to encoded video frame
106 */
Johannes Kron05f84872020-01-16 14:09:33 +0100107 int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000108
philipel9d3ab612015-12-21 04:12:39 -0800109 /**
Yves Gerey665174f2018-06-19 15:03:05 +0200110 * Set decode callback. Deregistering while decoding is illegal.
111 */
philipel9d3ab612015-12-21 04:12:39 -0800112 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000113
tommi5b7fc8c2017-07-05 16:45:57 -0700114 bool IsSameDecoder(VideoDecoder* decoder) const {
115 return decoder_.get() == decoder;
116 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000117
philipel9d3ab612015-12-21 04:12:39 -0800118 private:
Lu Liu352314a2018-02-21 19:38:59 +0000119 VCMDecodedFrameCallback* _callback;
120 VCMFrameInformation _frameInfos[kDecoderFrameMemoryLength];
121 uint32_t _nextFrameInfoIdx;
tommi5b7fc8c2017-07-05 16:45:57 -0700122 std::unique_ptr<VideoDecoder> decoder_;
Lu Liu352314a2018-02-21 19:38:59 +0000123 VideoCodecType _codecType;
tommi5b7fc8c2017-07-05 16:45:57 -0700124 const bool _isExternal;
ilnik00d802b2017-04-11 10:34:31 -0700125 VideoContentType _last_keyframe_content_type;
Erik Språngc12f6252021-01-13 21:49:59 +0100126 VideoDecoder::DecoderInfo decoder_info_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000127};
128
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000129} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000130
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200131#endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_