xref: /aosp_15_r20/external/webrtc/media/base/video_adapter.h (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1*d9f75844SAndroid Build Coastguard Worker /*
2*d9f75844SAndroid Build Coastguard Worker  *  Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
3*d9f75844SAndroid Build Coastguard Worker  *
4*d9f75844SAndroid Build Coastguard Worker  *  Use of this source code is governed by a BSD-style license
5*d9f75844SAndroid Build Coastguard Worker  *  that can be found in the LICENSE file in the root of the source
6*d9f75844SAndroid Build Coastguard Worker  *  tree. An additional intellectual property rights grant can be found
7*d9f75844SAndroid Build Coastguard Worker  *  in the file PATENTS.  All contributing project authors may
8*d9f75844SAndroid Build Coastguard Worker  *  be found in the AUTHORS file in the root of the source tree.
9*d9f75844SAndroid Build Coastguard Worker  */
10*d9f75844SAndroid Build Coastguard Worker 
11*d9f75844SAndroid Build Coastguard Worker #ifndef MEDIA_BASE_VIDEO_ADAPTER_H_
12*d9f75844SAndroid Build Coastguard Worker #define MEDIA_BASE_VIDEO_ADAPTER_H_
13*d9f75844SAndroid Build Coastguard Worker 
14*d9f75844SAndroid Build Coastguard Worker #include <stdint.h>
15*d9f75844SAndroid Build Coastguard Worker 
16*d9f75844SAndroid Build Coastguard Worker #include <string>
17*d9f75844SAndroid Build Coastguard Worker #include <utility>
18*d9f75844SAndroid Build Coastguard Worker 
19*d9f75844SAndroid Build Coastguard Worker #include "absl/types/optional.h"
20*d9f75844SAndroid Build Coastguard Worker #include "api/video/video_source_interface.h"
21*d9f75844SAndroid Build Coastguard Worker #include "common_video/framerate_controller.h"
22*d9f75844SAndroid Build Coastguard Worker #include "media/base/video_common.h"
23*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/synchronization/mutex.h"
24*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/system/rtc_export.h"
25*d9f75844SAndroid Build Coastguard Worker #include "rtc_base/thread_annotations.h"
26*d9f75844SAndroid Build Coastguard Worker 
27*d9f75844SAndroid Build Coastguard Worker namespace cricket {
28*d9f75844SAndroid Build Coastguard Worker 
29*d9f75844SAndroid Build Coastguard Worker // VideoAdapter adapts an input video frame to an output frame based on the
30*d9f75844SAndroid Build Coastguard Worker // specified input and output formats. The adaptation includes dropping frames
31*d9f75844SAndroid Build Coastguard Worker // to reduce frame rate and scaling frames.
32*d9f75844SAndroid Build Coastguard Worker // VideoAdapter is thread safe.
33*d9f75844SAndroid Build Coastguard Worker class RTC_EXPORT VideoAdapter {
34*d9f75844SAndroid Build Coastguard Worker  public:
35*d9f75844SAndroid Build Coastguard Worker   VideoAdapter();
36*d9f75844SAndroid Build Coastguard Worker   // The source requests output frames whose width and height are divisible
37*d9f75844SAndroid Build Coastguard Worker   // by `source_resolution_alignment`.
38*d9f75844SAndroid Build Coastguard Worker   explicit VideoAdapter(int source_resolution_alignment);
39*d9f75844SAndroid Build Coastguard Worker   virtual ~VideoAdapter();
40*d9f75844SAndroid Build Coastguard Worker 
41*d9f75844SAndroid Build Coastguard Worker   VideoAdapter(const VideoAdapter&) = delete;
42*d9f75844SAndroid Build Coastguard Worker   VideoAdapter& operator=(const VideoAdapter&) = delete;
43*d9f75844SAndroid Build Coastguard Worker 
44*d9f75844SAndroid Build Coastguard Worker   // Return the adapted resolution and cropping parameters given the
45*d9f75844SAndroid Build Coastguard Worker   // input resolution. The input frame should first be cropped, then
46*d9f75844SAndroid Build Coastguard Worker   // scaled to the final output resolution. Returns true if the frame
47*d9f75844SAndroid Build Coastguard Worker   // should be adapted, and false if it should be dropped.
48*d9f75844SAndroid Build Coastguard Worker   bool AdaptFrameResolution(int in_width,
49*d9f75844SAndroid Build Coastguard Worker                             int in_height,
50*d9f75844SAndroid Build Coastguard Worker                             int64_t in_timestamp_ns,
51*d9f75844SAndroid Build Coastguard Worker                             int* cropped_width,
52*d9f75844SAndroid Build Coastguard Worker                             int* cropped_height,
53*d9f75844SAndroid Build Coastguard Worker                             int* out_width,
54*d9f75844SAndroid Build Coastguard Worker                             int* out_height) RTC_LOCKS_EXCLUDED(mutex_);
55*d9f75844SAndroid Build Coastguard Worker 
56*d9f75844SAndroid Build Coastguard Worker   // DEPRECATED. Please use OnOutputFormatRequest below.
57*d9f75844SAndroid Build Coastguard Worker   // TODO(asapersson): Remove this once it is no longer used.
58*d9f75844SAndroid Build Coastguard Worker   // Requests the output frame size and frame interval from
59*d9f75844SAndroid Build Coastguard Worker   // `AdaptFrameResolution` to not be larger than `format`. Also, the input
60*d9f75844SAndroid Build Coastguard Worker   // frame size will be cropped to match the requested aspect ratio. The
61*d9f75844SAndroid Build Coastguard Worker   // requested aspect ratio is orientation agnostic and will be adjusted to
62*d9f75844SAndroid Build Coastguard Worker   // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or
63*d9f75844SAndroid Build Coastguard Worker   // 720x1280 is requested.
64*d9f75844SAndroid Build Coastguard Worker   // Note: Should be called from the source only.
65*d9f75844SAndroid Build Coastguard Worker   void OnOutputFormatRequest(const absl::optional<VideoFormat>& format)
66*d9f75844SAndroid Build Coastguard Worker       RTC_LOCKS_EXCLUDED(mutex_);
67*d9f75844SAndroid Build Coastguard Worker 
68*d9f75844SAndroid Build Coastguard Worker   // Requests output frame size and frame interval from `AdaptFrameResolution`.
69*d9f75844SAndroid Build Coastguard Worker   // `target_aspect_ratio`: The input frame size will be cropped to match the
70*d9f75844SAndroid Build Coastguard Worker   // requested aspect ratio. The aspect ratio is orientation agnostic and will
71*d9f75844SAndroid Build Coastguard Worker   // be adjusted to maintain the input orientation (i.e. it doesn't matter if
72*d9f75844SAndroid Build Coastguard Worker   // e.g. <1280,720> or <720,1280> is requested).
73*d9f75844SAndroid Build Coastguard Worker   // `max_pixel_count`: The maximum output frame size.
74*d9f75844SAndroid Build Coastguard Worker   // `max_fps`: The maximum output framerate.
75*d9f75844SAndroid Build Coastguard Worker   // Note: Should be called from the source only.
76*d9f75844SAndroid Build Coastguard Worker   void OnOutputFormatRequest(
77*d9f75844SAndroid Build Coastguard Worker       const absl::optional<std::pair<int, int>>& target_aspect_ratio,
78*d9f75844SAndroid Build Coastguard Worker       const absl::optional<int>& max_pixel_count,
79*d9f75844SAndroid Build Coastguard Worker       const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
80*d9f75844SAndroid Build Coastguard Worker 
81*d9f75844SAndroid Build Coastguard Worker   // Same as above, but allows setting two different target aspect ratios
82*d9f75844SAndroid Build Coastguard Worker   // depending on incoming frame orientation. This gives more fine-grained
83*d9f75844SAndroid Build Coastguard Worker   // control and can e.g. be used to force landscape video to be cropped to
84*d9f75844SAndroid Build Coastguard Worker   // portrait video.
85*d9f75844SAndroid Build Coastguard Worker   void OnOutputFormatRequest(
86*d9f75844SAndroid Build Coastguard Worker       const absl::optional<std::pair<int, int>>& target_landscape_aspect_ratio,
87*d9f75844SAndroid Build Coastguard Worker       const absl::optional<int>& max_landscape_pixel_count,
88*d9f75844SAndroid Build Coastguard Worker       const absl::optional<std::pair<int, int>>& target_portrait_aspect_ratio,
89*d9f75844SAndroid Build Coastguard Worker       const absl::optional<int>& max_portrait_pixel_count,
90*d9f75844SAndroid Build Coastguard Worker       const absl::optional<int>& max_fps) RTC_LOCKS_EXCLUDED(mutex_);
91*d9f75844SAndroid Build Coastguard Worker 
92*d9f75844SAndroid Build Coastguard Worker   // Requests the output frame size from `AdaptFrameResolution` to have as close
93*d9f75844SAndroid Build Coastguard Worker   // as possible to `sink_wants.target_pixel_count` pixels (if set)
94*d9f75844SAndroid Build Coastguard Worker   // but no more than `sink_wants.max_pixel_count`.
95*d9f75844SAndroid Build Coastguard Worker   // `sink_wants.max_framerate_fps` is essentially analogous to
96*d9f75844SAndroid Build Coastguard Worker   // `sink_wants.max_pixel_count`, but for framerate rather than resolution.
97*d9f75844SAndroid Build Coastguard Worker   // Set `sink_wants.max_pixel_count` and/or `sink_wants.max_framerate_fps` to
98*d9f75844SAndroid Build Coastguard Worker   // std::numeric_limit<int>::max() if no upper limit is desired.
99*d9f75844SAndroid Build Coastguard Worker   // The sink resolution alignment requirement is given by
100*d9f75844SAndroid Build Coastguard Worker   // `sink_wants.resolution_alignment`.
101*d9f75844SAndroid Build Coastguard Worker   // Note: Should be called from the sink only.
102*d9f75844SAndroid Build Coastguard Worker   void OnSinkWants(const rtc::VideoSinkWants& sink_wants)
103*d9f75844SAndroid Build Coastguard Worker       RTC_LOCKS_EXCLUDED(mutex_);
104*d9f75844SAndroid Build Coastguard Worker 
105*d9f75844SAndroid Build Coastguard Worker   // Returns maximum image area, which shouldn't impose any adaptations.
106*d9f75844SAndroid Build Coastguard Worker   // Can return `numeric_limits<int>::max()` if no limit is set.
107*d9f75844SAndroid Build Coastguard Worker   int GetTargetPixels() const;
108*d9f75844SAndroid Build Coastguard Worker 
109*d9f75844SAndroid Build Coastguard Worker   // Returns current frame-rate limit.
110*d9f75844SAndroid Build Coastguard Worker   // Can return `numeric_limits<float>::infinity()` if no limit is set.
111*d9f75844SAndroid Build Coastguard Worker   float GetMaxFramerate() const;
112*d9f75844SAndroid Build Coastguard Worker 
113*d9f75844SAndroid Build Coastguard Worker  private:
114*d9f75844SAndroid Build Coastguard Worker   // Determine if frame should be dropped based on input fps and requested fps.
115*d9f75844SAndroid Build Coastguard Worker   bool DropFrame(int64_t in_timestamp_ns) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
116*d9f75844SAndroid Build Coastguard Worker 
117*d9f75844SAndroid Build Coastguard Worker   int frames_in_ RTC_GUARDED_BY(mutex_);      // Number of input frames.
118*d9f75844SAndroid Build Coastguard Worker   int frames_out_ RTC_GUARDED_BY(mutex_);     // Number of output frames.
119*d9f75844SAndroid Build Coastguard Worker   int frames_scaled_ RTC_GUARDED_BY(mutex_);  // Number of frames scaled.
120*d9f75844SAndroid Build Coastguard Worker   int adaption_changes_
121*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(mutex_);  // Number of changes in scale factor.
122*d9f75844SAndroid Build Coastguard Worker   int previous_width_ RTC_GUARDED_BY(mutex_);  // Previous adapter output width.
123*d9f75844SAndroid Build Coastguard Worker   int previous_height_
124*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(mutex_);  // Previous adapter output height.
125*d9f75844SAndroid Build Coastguard Worker   const bool variable_start_scale_factor_;
126*d9f75844SAndroid Build Coastguard Worker 
127*d9f75844SAndroid Build Coastguard Worker   // The fixed source resolution alignment requirement.
128*d9f75844SAndroid Build Coastguard Worker   const int source_resolution_alignment_;
129*d9f75844SAndroid Build Coastguard Worker   // The currently applied resolution alignment, as given by the requirements:
130*d9f75844SAndroid Build Coastguard Worker   //  - the fixed `source_resolution_alignment_`; and
131*d9f75844SAndroid Build Coastguard Worker   //  - the latest `sink_wants.resolution_alignment`.
132*d9f75844SAndroid Build Coastguard Worker   int resolution_alignment_ RTC_GUARDED_BY(mutex_);
133*d9f75844SAndroid Build Coastguard Worker 
134*d9f75844SAndroid Build Coastguard Worker   // Max number of pixels/fps requested via calls to OnOutputFormatRequest,
135*d9f75844SAndroid Build Coastguard Worker   // OnResolutionFramerateRequest respectively.
136*d9f75844SAndroid Build Coastguard Worker   // The adapted output format is the minimum of these.
137*d9f75844SAndroid Build Coastguard Worker   struct OutputFormatRequest {
138*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::pair<int, int>> target_landscape_aspect_ratio;
139*d9f75844SAndroid Build Coastguard Worker     absl::optional<int> max_landscape_pixel_count;
140*d9f75844SAndroid Build Coastguard Worker     absl::optional<std::pair<int, int>> target_portrait_aspect_ratio;
141*d9f75844SAndroid Build Coastguard Worker     absl::optional<int> max_portrait_pixel_count;
142*d9f75844SAndroid Build Coastguard Worker     absl::optional<int> max_fps;
143*d9f75844SAndroid Build Coastguard Worker 
144*d9f75844SAndroid Build Coastguard Worker     // For logging.
145*d9f75844SAndroid Build Coastguard Worker     std::string ToString() const;
146*d9f75844SAndroid Build Coastguard Worker   };
147*d9f75844SAndroid Build Coastguard Worker 
148*d9f75844SAndroid Build Coastguard Worker   OutputFormatRequest output_format_request_ RTC_GUARDED_BY(mutex_);
149*d9f75844SAndroid Build Coastguard Worker   int resolution_request_target_pixel_count_ RTC_GUARDED_BY(mutex_);
150*d9f75844SAndroid Build Coastguard Worker   int resolution_request_max_pixel_count_ RTC_GUARDED_BY(mutex_);
151*d9f75844SAndroid Build Coastguard Worker   int max_framerate_request_ RTC_GUARDED_BY(mutex_);
152*d9f75844SAndroid Build Coastguard Worker 
153*d9f75844SAndroid Build Coastguard Worker   // Stashed OutputFormatRequest that is used to save value of
154*d9f75844SAndroid Build Coastguard Worker   // OnOutputFormatRequest in case all active encoders are using
155*d9f75844SAndroid Build Coastguard Worker   // requested_resolution. I.e when all active encoders are using
156*d9f75844SAndroid Build Coastguard Worker   // requested_resolution, the call to OnOutputFormatRequest is ignored
157*d9f75844SAndroid Build Coastguard Worker   // and the value from requested_resolution is used instead (to scale/crop
158*d9f75844SAndroid Build Coastguard Worker   // frame). This allows for an application to only use
159*d9f75844SAndroid Build Coastguard Worker   // RtpEncodingParameters::request_resolution and get the same behavior as if
160*d9f75844SAndroid Build Coastguard Worker   // it had used VideoAdapter::OnOutputFormatRequest.
161*d9f75844SAndroid Build Coastguard Worker   absl::optional<OutputFormatRequest> stashed_output_format_request_
162*d9f75844SAndroid Build Coastguard Worker       RTC_GUARDED_BY(mutex_);
163*d9f75844SAndroid Build Coastguard Worker 
164*d9f75844SAndroid Build Coastguard Worker   webrtc::FramerateController framerate_controller_ RTC_GUARDED_BY(mutex_);
165*d9f75844SAndroid Build Coastguard Worker 
166*d9f75844SAndroid Build Coastguard Worker   // The critical section to protect the above variables.
167*d9f75844SAndroid Build Coastguard Worker   mutable webrtc::Mutex mutex_;
168*d9f75844SAndroid Build Coastguard Worker };
169*d9f75844SAndroid Build Coastguard Worker 
170*d9f75844SAndroid Build Coastguard Worker }  // namespace cricket
171*d9f75844SAndroid Build Coastguard Worker 
172*d9f75844SAndroid Build Coastguard Worker #endif  // MEDIA_BASE_VIDEO_ADAPTER_H_
173