blob: d7e1850abb917e6c6b2d1e97ca1337b98f84db78 [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
Evan Shrubsole1c51ec42022-08-08 11:21:26 +000014#include <cstdint>
15#include <deque>
Ilya Nikolaevskiy43c108b2020-05-15 12:24:29 +020016#include <string>
Evan Shrubsole1c51ec42022-08-08 11:21:26 +000017#include <utility>
tommi5b7fc8c2017-07-05 16:45:57 -070018
Jonas Orelande62c2f22022-03-29 11:04:48 +020019#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 14:31:24 +010020#include "api/sequence_checker.h"
Danil Chapovalov355b8d22021-08-13 16:50:37 +020021#include "api/video_codecs/video_decoder.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020022#include "modules/video_coding/encoded_frame.h"
Rasmus Brandtc4d253c2022-05-25 12:03:35 +020023#include "modules/video_coding/timing/timing.h"
Markus Handell6deec382020-07-07 12:17:12 +020024#include "rtc_base/synchronization/mutex.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
Evan Shrubsole1c51ec42022-08-08 11:21:26 +000030struct FrameInfo {
31 FrameInfo() = default;
32 FrameInfo(const FrameInfo&) = delete;
33 FrameInfo& operator=(const FrameInfo&) = delete;
34 FrameInfo(FrameInfo&&) = default;
35 FrameInfo& operator=(FrameInfo&&) = default;
36
37 uint32_t rtp_timestamp;
38 // This is likely not optional, but some inputs seem to sometimes be negative.
39 // TODO(bugs.webrtc.org/13756): See if this can be replaced with Timestamp
40 // once all inputs to this field use Timestamp instead of an integer.
41 absl::optional<Timestamp> render_time;
42 absl::optional<Timestamp> decode_start;
43 VideoRotation rotation;
44 VideoContentType content_type;
45 EncodedImage::Timing timing;
46 int64_t ntp_time_ms;
47 RtpPacketInfos packet_infos;
48 // ColorSpace is not stored here, as it might be modified by decoders.
49};
niklase@google.com470e71d2011-07-07 08:21:25 +000050
philipel9d3ab612015-12-21 04:12:39 -080051class VCMDecodedFrameCallback : public DecodedImageCallback {
52 public:
Jonas Orelande02f9ee2022-03-25 12:43:14 +010053 VCMDecodedFrameCallback(VCMTiming* timing,
54 Clock* clock,
Jonas Orelande62c2f22022-03-29 11:04:48 +020055 const FieldTrialsView& field_trials);
tommid0a71ba2017-03-14 04:16:20 -070056 ~VCMDecodedFrameCallback() override;
57 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
58 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:25 +000059
tommid0a71ba2017-03-14 04:16:20 -070060 int32_t Decoded(VideoFrame& decodedImage) override;
61 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
62 void Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 10:48:16 +020063 absl::optional<int32_t> decode_time_ms,
64 absl::optional<uint8_t> qp) override;
niklase@google.com470e71d2011-07-07 08:21:25 +000065
tommid0a71ba2017-03-14 04:16:20 -070066 void OnDecoderImplementationName(const char* implementation_name);
niklase@google.com470e71d2011-07-07 08:21:25 +000067
Evan Shrubsole1c51ec42022-08-08 11:21:26 +000068 void Map(FrameInfo frameInfo);
Johannes Kronfc5d2762021-04-09 16:03:22 +020069 void ClearTimestampMap();
niklase@google.com470e71d2011-07-07 08:21:25 +000070
philipel9d3ab612015-12-21 04:12:39 -080071 private:
Evan Shrubsole1c51ec42022-08-08 11:21:26 +000072 std::pair<absl::optional<FrameInfo>, size_t> FindFrameInfo(
73 uint32_t rtp_timestamp) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
74
Artem Titovc8421c42021-02-02 10:57:19 +010075 SequenceChecker construction_thread_;
tommid0a71ba2017-03-14 04:16:20 -070076 Clock* const _clock;
77 // This callback must be set before the decoder thread starts running
78 // and must only be unset when external threads (e.g decoder thread)
79 // have been stopped. Due to that, the variable should regarded as const
80 // while there are more than one threads involved, it must be set
81 // from the same thread, and therfore a lock is not required to access it.
82 VCMReceiveCallback* _receiveCallback = nullptr;
Lu Liu352314a2018-02-21 19:38:59 +000083 VCMTiming* _timing;
Markus Handell6deec382020-07-07 12:17:12 +020084 Mutex lock_;
Evan Shrubsole1c51ec42022-08-08 11:21:26 +000085 std::deque<FrameInfo> frame_infos_ RTC_GUARDED_BY(lock_);
ilnik04f4d122017-06-19 07:18:55 -070086 int64_t ntp_offset_;
niklase@google.com470e71d2011-07-07 08:21:25 +000087};
88
philipel9d3ab612015-12-21 04:12:39 -080089class VCMGenericDecoder {
philipel9d3ab612015-12-21 04:12:39 -080090 public:
Danil Chapovalov7b78a312021-08-06 12:30:02 +020091 explicit VCMGenericDecoder(VideoDecoder* decoder);
philipel9d3ab612015-12-21 04:12:39 -080092 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:25 +000093
philipel9d3ab612015-12-21 04:12:39 -080094 /**
Danil Chapovalov355b8d22021-08-13 16:50:37 +020095 * Initialize the decoder with the information from the `settings`
Yves Gerey665174f2018-06-19 15:03:05 +020096 */
Danil Chapovalov355b8d22021-08-13 16:50:37 +020097 bool Configure(const VideoDecoder::Settings& settings);
niklase@google.com470e71d2011-07-07 08:21:25 +000098
philipel9d3ab612015-12-21 04:12:39 -080099 /**
Yves Gerey665174f2018-06-19 15:03:05 +0200100 * Decode to a raw I420 frame,
101 *
102 * inputVideoBuffer reference to encoded video frame
103 */
Johannes Kron05f84872020-01-16 14:09:33 +0100104 int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now);
niklase@google.com470e71d2011-07-07 08:21:25 +0000105
philipel9d3ab612015-12-21 04:12:39 -0800106 /**
Yves Gerey665174f2018-06-19 15:03:05 +0200107 * Set decode callback. Deregistering while decoding is illegal.
108 */
philipel9d3ab612015-12-21 04:12:39 -0800109 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25 +0000110
tommi5b7fc8c2017-07-05 16:45:57 -0700111 bool IsSameDecoder(VideoDecoder* decoder) const {
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200112 return decoder_ == decoder;
tommi5b7fc8c2017-07-05 16:45:57 -0700113 }
niklase@google.com470e71d2011-07-07 08:21:25 +0000114
philipel9d3ab612015-12-21 04:12:39 -0800115 private:
Danil Chapovalov7b78a312021-08-06 12:30:02 +0200116 VCMDecodedFrameCallback* _callback = nullptr;
117 VideoDecoder* const decoder_;
ilnik00d802b2017-04-11 10:34:31 -0700118 VideoContentType _last_keyframe_content_type;
Erik Språngc12f6252021-01-13 21:49:59 +0100119 VideoDecoder::DecoderInfo decoder_info_;
niklase@google.com470e71d2011-07-07 08:21:25 +0000120};
121
pbos@webrtc.orgd900e8b2013-07-03 15:12:26 +0000122} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25 +0000123
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200124#endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_