blob: b1fb1f39f4e6c94ad974eba888a42a9941217dbc [file] [log] [blame]
niklase@google.com470e71d2011-07-07 08:21:251/*
mikhal@webrtc.orga2031d52012-07-31 15:53:442 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
niklase@google.com470e71d2011-07-07 08:21:253 *
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 04:47:3111#ifndef MODULES_VIDEO_CODING_GENERIC_DECODER_H_
12#define MODULES_VIDEO_CODING_GENERIC_DECODER_H_
niklase@google.com470e71d2011-07-07 08:21:2513
Evan Shrubsole1c51ec42022-08-08 11:21:2614#include <cstdint>
15#include <deque>
Ilya Nikolaevskiy43c108b2020-05-15 10:24:2916#include <string>
Evan Shrubsole1c51ec42022-08-08 11:21:2617#include <utility>
tommi5b7fc8c2017-07-05 23:45:5718
Jonas Orelande62c2f22022-03-29 09:04:4819#include "api/field_trials_view.h"
Artem Titovd15a5752021-02-10 13:31:2420#include "api/sequence_checker.h"
Tony Herre5f14f9e2023-08-25 12:53:4421#include "api/video/encoded_frame.h"
Danil Chapovalov355b8d22021-08-13 14:50:3722#include "api/video_codecs/video_decoder.h"
Mirko Bonadei92ea95e2017-09-15 04:47:3123#include "modules/video_coding/encoded_frame.h"
Rasmus Brandtc4d253c2022-05-25 10:03:3524#include "modules/video_coding/timing/timing.h"
Markus Handell6deec382020-07-07 10:17:1225#include "rtc_base/synchronization/mutex.h"
niklase@google.com470e71d2011-07-07 08:21:2526
philipel9d3ab612015-12-21 12:12:3927namespace webrtc {
niklase@google.com470e71d2011-07-07 08:21:2528
29class VCMReceiveCallback;
30
Evan Shrubsole1c51ec42022-08-08 11:21:2631struct FrameInfo {
32 FrameInfo() = default;
33 FrameInfo(const FrameInfo&) = delete;
34 FrameInfo& operator=(const FrameInfo&) = delete;
35 FrameInfo(FrameInfo&&) = default;
36 FrameInfo& operator=(FrameInfo&&) = default;
37
38 uint32_t rtp_timestamp;
39 // This is likely not optional, but some inputs seem to sometimes be negative.
40 // TODO(bugs.webrtc.org/13756): See if this can be replaced with Timestamp
41 // once all inputs to this field use Timestamp instead of an integer.
42 absl::optional<Timestamp> render_time;
43 absl::optional<Timestamp> decode_start;
44 VideoRotation rotation;
45 VideoContentType content_type;
46 EncodedImage::Timing timing;
47 int64_t ntp_time_ms;
48 RtpPacketInfos packet_infos;
49 // ColorSpace is not stored here, as it might be modified by decoders.
Philipp Hancke51657432023-08-15 08:20:5050 VideoFrameType frame_type;
Evan Shrubsole1c51ec42022-08-08 11:21:2651};
niklase@google.com470e71d2011-07-07 08:21:2552
philipel9d3ab612015-12-21 12:12:3953class VCMDecodedFrameCallback : public DecodedImageCallback {
54 public:
Jonas Orelande02f9ee2022-03-25 11:43:1455 VCMDecodedFrameCallback(VCMTiming* timing,
56 Clock* clock,
Jonas Orelande62c2f22022-03-29 09:04:4857 const FieldTrialsView& field_trials);
tommid0a71ba2017-03-14 11:16:2058 ~VCMDecodedFrameCallback() override;
59 void SetUserReceiveCallback(VCMReceiveCallback* receiveCallback);
60 VCMReceiveCallback* UserReceiveCallback();
niklase@google.com470e71d2011-07-07 08:21:2561
tommid0a71ba2017-03-14 11:16:2062 int32_t Decoded(VideoFrame& decodedImage) override;
63 int32_t Decoded(VideoFrame& decodedImage, int64_t decode_time_ms) override;
64 void Decoded(VideoFrame& decodedImage,
Danil Chapovalov0040b662018-06-18 08:48:1665 absl::optional<int32_t> decode_time_ms,
66 absl::optional<uint8_t> qp) override;
niklase@google.com470e71d2011-07-07 08:21:2567
Evan Shrubsole09da10e2022-10-14 14:38:3168 void OnDecoderInfoChanged(const VideoDecoder::DecoderInfo& decoder_info);
niklase@google.com470e71d2011-07-07 08:21:2569
Evan Shrubsole1c51ec42022-08-08 11:21:2670 void Map(FrameInfo frameInfo);
Johannes Kronfc5d2762021-04-09 14:03:2271 void ClearTimestampMap();
niklase@google.com470e71d2011-07-07 08:21:2572
philipel9d3ab612015-12-21 12:12:3973 private:
Evan Shrubsole1c51ec42022-08-08 11:21:2674 std::pair<absl::optional<FrameInfo>, size_t> FindFrameInfo(
75 uint32_t rtp_timestamp) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_);
76
Artem Titovc8421c42021-02-02 09:57:1977 SequenceChecker construction_thread_;
tommid0a71ba2017-03-14 11:16:2078 Clock* const _clock;
79 // This callback must be set before the decoder thread starts running
80 // and must only be unset when external threads (e.g decoder thread)
81 // have been stopped. Due to that, the variable should regarded as const
82 // while there are more than one threads involved, it must be set
83 // from the same thread, and therfore a lock is not required to access it.
84 VCMReceiveCallback* _receiveCallback = nullptr;
Lu Liu352314a2018-02-21 19:38:5985 VCMTiming* _timing;
Markus Handell6deec382020-07-07 10:17:1286 Mutex lock_;
Evan Shrubsole1c51ec42022-08-08 11:21:2687 std::deque<FrameInfo> frame_infos_ RTC_GUARDED_BY(lock_);
ilnik04f4d122017-06-19 14:18:5588 int64_t ntp_offset_;
niklase@google.com470e71d2011-07-07 08:21:2589};
90
philipel9d3ab612015-12-21 12:12:3991class VCMGenericDecoder {
philipel9d3ab612015-12-21 12:12:3992 public:
Danil Chapovalov7b78a312021-08-06 10:30:0293 explicit VCMGenericDecoder(VideoDecoder* decoder);
philipel9d3ab612015-12-21 12:12:3994 ~VCMGenericDecoder();
niklase@google.com470e71d2011-07-07 08:21:2595
philipel9d3ab612015-12-21 12:12:3996 /**
Danil Chapovalov355b8d22021-08-13 14:50:3797 * Initialize the decoder with the information from the `settings`
Yves Gerey665174f2018-06-19 13:03:0598 */
Danil Chapovalov355b8d22021-08-13 14:50:3799 bool Configure(const VideoDecoder::Settings& settings);
niklase@google.com470e71d2011-07-07 08:21:25100
philipel9d3ab612015-12-21 12:12:39101 /**
Yves Gerey665174f2018-06-19 13:03:05102 * Decode to a raw I420 frame,
103 *
104 * inputVideoBuffer reference to encoded video frame
105 */
Tony Herre5f14f9e2023-08-25 12:53:44106 // TODO(https://bugs.webrtc.org/9378): Remove VCMEncodedFrame variant
107 // once the usage from code in deprecated/ is gone.
Johannes Kron05f84872020-01-16 13:09:33108 int32_t Decode(const VCMEncodedFrame& inputFrame, Timestamp now);
Tony Herre5f14f9e2023-08-25 12:53:44109 int32_t Decode(const EncodedFrame& inputFrame, Timestamp now);
niklase@google.com470e71d2011-07-07 08:21:25110
philipel9d3ab612015-12-21 12:12:39111 /**
Yves Gerey665174f2018-06-19 13:03:05112 * Set decode callback. Deregistering while decoding is illegal.
113 */
philipel9d3ab612015-12-21 12:12:39114 int32_t RegisterDecodeCompleteCallback(VCMDecodedFrameCallback* callback);
niklase@google.com470e71d2011-07-07 08:21:25115
tommi5b7fc8c2017-07-05 23:45:57116 bool IsSameDecoder(VideoDecoder* decoder) const {
Danil Chapovalov7b78a312021-08-06 10:30:02117 return decoder_ == decoder;
tommi5b7fc8c2017-07-05 23:45:57118 }
niklase@google.com470e71d2011-07-07 08:21:25119
philipel9d3ab612015-12-21 12:12:39120 private:
Tony Herre5f14f9e2023-08-25 12:53:44121 int32_t Decode(const EncodedImage& frame,
122 Timestamp now,
Tony Herre55b593f2023-08-29 14:05:49123 int64_t render_time_ms);
Danil Chapovalov7b78a312021-08-06 10:30:02124 VCMDecodedFrameCallback* _callback = nullptr;
125 VideoDecoder* const decoder_;
ilnik00d802b2017-04-11 17:34:31126 VideoContentType _last_keyframe_content_type;
Erik Språngc12f6252021-01-13 20:49:59127 VideoDecoder::DecoderInfo decoder_info_;
niklase@google.com470e71d2011-07-07 08:21:25128};
129
pbos@webrtc.orgd900e8b2013-07-03 15:12:26130} // namespace webrtc
niklase@google.com470e71d2011-07-07 08:21:25131
Mirko Bonadei92ea95e2017-09-15 04:47:31132#endif // MODULES_VIDEO_CODING_GENERIC_DECODER_H_