jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 1 | /* |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 2 | * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 3 | * |
kjellander | 1afca73 | 2016-02-07 20:46:45 -0800 | [diff] [blame] | 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. |
jlmiller@webrtc.org | 5f93d0a | 2015-01-20 21:36:13 +0000 | [diff] [blame] | 9 | */ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #ifndef MEDIA_BASE_VIDEOADAPTER_H_ |
| 12 | #define MEDIA_BASE_VIDEOADAPTER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 14 | #include <utility> |
| 15 | |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 16 | #include "absl/types/optional.h" |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 17 | #include "media/base/videocommon.h" |
| 18 | #include "rtc_base/constructormagic.h" |
| 19 | #include "rtc_base/criticalsection.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 20 | |
| 21 | namespace cricket { |
| 22 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 23 | // VideoAdapter adapts an input video frame to an output frame based on the |
| 24 | // specified input and output formats. The adaptation includes dropping frames |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 25 | // to reduce frame rate and scaling frames. |
| 26 | // VideoAdapter is thread safe. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 27 | class VideoAdapter { |
| 28 | public: |
| 29 | VideoAdapter(); |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 30 | // The output frames will have height and width that is divisible by |
| 31 | // |required_resolution_alignment|. |
| 32 | explicit VideoAdapter(int required_resolution_alignment); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 33 | virtual ~VideoAdapter(); |
| 34 | |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 35 | // Return the adapted resolution and cropping parameters given the |
| 36 | // input resolution. The input frame should first be cropped, then |
| 37 | // scaled to the final output resolution. Returns true if the frame |
| 38 | // should be adapted, and false if it should be dropped. |
| 39 | bool AdaptFrameResolution(int in_width, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 40 | int in_height, |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 41 | int64_t in_timestamp_ns, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 42 | int* cropped_width, |
| 43 | int* cropped_height, |
| 44 | int* out_width, |
| 45 | int* out_height); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 46 | |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 47 | // DEPRECATED. Please use OnOutputFormatRequest below. |
| 48 | // TODO(asapersson): Remove this once it is no longer used. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 49 | // Requests the output frame size and frame interval from |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 50 | // |AdaptFrameResolution| to not be larger than |format|. Also, the input |
| 51 | // frame size will be cropped to match the requested aspect ratio. The |
| 52 | // requested aspect ratio is orientation agnostic and will be adjusted to |
| 53 | // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or |
| 54 | // 720x1280 is requested. |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 55 | // Note: Should be called from the source only. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 56 | void OnOutputFormatRequest(const absl::optional<VideoFormat>& format); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 57 | |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 58 | // Requests output frame size and frame interval from |AdaptFrameResolution|. |
| 59 | // |target_aspect_ratio|: The input frame size will be cropped to match the |
| 60 | // requested aspect ratio. The aspect ratio is orientation agnostic and will |
| 61 | // be adjusted to maintain the input orientation (i.e. it doesn't matter if |
| 62 | // e.g. <1280,720> or <720,1280> is requested). |
| 63 | // |max_pixel_count|: The maximum output frame size. |
| 64 | // |max_fps|: The maximum output framerate. |
| 65 | // Note: Should be called from the source only. |
| 66 | void OnOutputFormatRequest( |
| 67 | const absl::optional<std::pair<int, int>>& target_aspect_ratio, |
| 68 | const absl::optional<int>& max_pixel_count, |
| 69 | const absl::optional<int>& max_fps); |
| 70 | |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame] | 71 | // Requests the output frame size from |AdaptFrameResolution| to have as close |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 72 | // as possible to |target_pixel_count| pixels (if set) but no more than |
| 73 | // |max_pixel_count|. |
| 74 | // |max_framerate_fps| is essentially analogous to |max_pixel_count|, but for |
| 75 | // framerate rather than resolution. |
| 76 | // Set |max_pixel_count| and/or |max_framerate_fps| to |
| 77 | // std::numeric_limit<int>::max() if no upper limit is desired. |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 78 | // Note: Should be called from the sink only. |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 79 | void OnResolutionFramerateRequest( |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 80 | const absl::optional<int>& target_pixel_count, |
sprang | c5d62e2 | 2017-04-02 23:53:04 -0700 | [diff] [blame] | 81 | int max_pixel_count, |
| 82 | int max_framerate_fps); |
buildbot@webrtc.org | 71dffb7 | 2014-06-24 07:24:49 +0000 | [diff] [blame] | 83 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 84 | private: |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 85 | // Determine if frame should be dropped based on input fps and requested fps. |
| 86 | bool KeepFrame(int64_t in_timestamp_ns); |
| 87 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 88 | int frames_in_; // Number of input frames. |
| 89 | int frames_out_; // Number of output frames. |
| 90 | int frames_scaled_; // Number of frames scaled. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 91 | int adaption_changes_; // Number of changes in scale factor. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 92 | int previous_width_; // Previous adapter output width. |
| 93 | int previous_height_; // Previous adapter output height. |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 94 | // Resolution must be divisible by this factor. |
| 95 | const int required_resolution_alignment_; |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 96 | // The target timestamp for the next frame based on requested format. |
Danil Chapovalov | 00c7183 | 2018-06-15 15:58:38 +0200 | [diff] [blame] | 97 | absl::optional<int64_t> next_frame_timestamp_ns_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 98 | RTC_GUARDED_BY(critical_section_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 99 | |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 100 | // Max number of pixels/fps requested via calls to OnOutputFormatRequest, |
| 101 | // OnResolutionFramerateRequest respectively. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 102 | // The adapted output format is the minimum of these. |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 103 | absl::optional<std::pair<int, int>> target_aspect_ratio_ |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 104 | RTC_GUARDED_BY(critical_section_); |
Åsa Persson | 2e4419e | 2018-09-06 15:02:55 +0200 | [diff] [blame^] | 105 | absl::optional<int> max_pixel_count_ RTC_GUARDED_BY(critical_section_); |
| 106 | absl::optional<int> max_fps_ RTC_GUARDED_BY(critical_section_); |
danilchap | a37de39 | 2017-09-09 04:17:22 -0700 | [diff] [blame] | 107 | int resolution_request_target_pixel_count_ RTC_GUARDED_BY(critical_section_); |
| 108 | int resolution_request_max_pixel_count_ RTC_GUARDED_BY(critical_section_); |
| 109 | int max_framerate_request_ RTC_GUARDED_BY(critical_section_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 110 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 111 | // The critical section to protect the above variables. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 112 | rtc::CriticalSection critical_section_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 113 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 114 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 115 | }; |
| 116 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 117 | } // namespace cricket |
| 118 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 119 | #endif // MEDIA_BASE_VIDEOADAPTER_H_ |