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 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 11 | #ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 12 | #define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 13 | |
kwiberg | 4485ffb | 2016-04-26 08:14:39 -0700 | [diff] [blame] | 14 | #include "webrtc/base/constructormagic.h" |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 15 | #include "webrtc/base/criticalsection.h" |
perkj | 2d5f091 | 2016-02-29 00:04:41 -0800 | [diff] [blame] | 16 | #include "webrtc/base/optional.h" |
kjellander | a96e2d7 | 2016-02-04 23:52:28 -0800 | [diff] [blame] | 17 | #include "webrtc/media/base/videocommon.h" |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 18 | |
| 19 | namespace cricket { |
| 20 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 21 | // VideoAdapter adapts an input video frame to an output frame based on the |
| 22 | // specified input and output formats. The adaptation includes dropping frames |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 23 | // to reduce frame rate and scaling frames. |
| 24 | // VideoAdapter is thread safe. |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 25 | class VideoAdapter { |
| 26 | public: |
| 27 | VideoAdapter(); |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 28 | VideoAdapter(int required_resolution_alignment); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 29 | virtual ~VideoAdapter(); |
| 30 | |
nisse | 47ac462 | 2016-05-25 08:47:01 -0700 | [diff] [blame] | 31 | // Return the adapted resolution and cropping parameters given the |
| 32 | // input resolution. The input frame should first be cropped, then |
| 33 | // scaled to the final output resolution. Returns true if the frame |
| 34 | // should be adapted, and false if it should be dropped. |
| 35 | bool AdaptFrameResolution(int in_width, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 36 | int in_height, |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 37 | int64_t in_timestamp_ns, |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 38 | int* cropped_width, |
| 39 | int* cropped_height, |
| 40 | int* out_width, |
| 41 | int* out_height); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 42 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 43 | // Requests the output frame size and frame interval from |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 44 | // |AdaptFrameResolution| to not be larger than |format|. Also, the input |
| 45 | // frame size will be cropped to match the requested aspect ratio. The |
| 46 | // requested aspect ratio is orientation agnostic and will be adjusted to |
| 47 | // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or |
| 48 | // 720x1280 is requested. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 49 | void OnOutputFormatRequest(const VideoFormat& format); |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 50 | |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame^] | 51 | // Requests the output frame size from |AdaptFrameResolution| to have as close |
| 52 | // as possible to |target_pixel_count|, but no more than |max_pixel_count| |
| 53 | // pixels. If |target_pixel_count| is not set, treat it as being equal to |
| 54 | // |max_pixel_count|. If |max_pixel_count| is not set, treat is as being the |
| 55 | // highest resolution available. |
| 56 | void OnResolutionRequest(const rtc::Optional<int>& target_pixel_count, |
| 57 | const rtc::Optional<int>& max_pixel_count); |
buildbot@webrtc.org | 71dffb7 | 2014-06-24 07:24:49 +0000 | [diff] [blame] | 58 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 59 | private: |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 60 | // Determine if frame should be dropped based on input fps and requested fps. |
| 61 | bool KeepFrame(int64_t in_timestamp_ns); |
| 62 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 63 | int frames_in_; // Number of input frames. |
| 64 | int frames_out_; // Number of output frames. |
| 65 | int frames_scaled_; // Number of frames scaled. |
wu@webrtc.org | cadf904 | 2013-08-30 21:24:16 +0000 | [diff] [blame] | 66 | int adaption_changes_; // Number of changes in scale factor. |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 67 | int previous_width_; // Previous adapter output width. |
| 68 | int previous_height_; // Previous adapter output height. |
kthelgason | c847417 | 2016-12-08 08:04:51 -0800 | [diff] [blame] | 69 | // Resolution must be divisible by this factor. |
| 70 | const int required_resolution_alignment_; |
magjed | 604abe0 | 2016-05-19 06:05:40 -0700 | [diff] [blame] | 71 | // The target timestamp for the next frame based on requested format. |
| 72 | rtc::Optional<int64_t> next_frame_timestamp_ns_ GUARDED_BY(critical_section_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 73 | |
| 74 | // Max number of pixels requested via calls to OnOutputFormatRequest, |
| 75 | // OnResolutionRequest respectively. |
| 76 | // The adapted output format is the minimum of these. |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 77 | rtc::Optional<VideoFormat> requested_format_ GUARDED_BY(critical_section_); |
sprang | 84a3759 | 2017-02-10 07:04:27 -0800 | [diff] [blame^] | 78 | int resolution_request_target_pixel_count_ GUARDED_BY(critical_section_); |
magjed | 709f73c | 2016-05-13 10:26:00 -0700 | [diff] [blame] | 79 | int resolution_request_max_pixel_count_ GUARDED_BY(critical_section_); |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 80 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 81 | // The critical section to protect the above variables. |
buildbot@webrtc.org | d4e598d | 2014-07-29 17:36:52 +0000 | [diff] [blame] | 82 | rtc::CriticalSection critical_section_; |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 83 | |
henrikg | 3c089d7 | 2015-09-16 05:37:44 -0700 | [diff] [blame] | 84 | RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter); |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
henrike@webrtc.org | 28e2075 | 2013-07-10 00:45:36 +0000 | [diff] [blame] | 87 | } // namespace cricket |
| 88 | |
Per | 766ad3b | 2016-04-05 15:23:49 +0200 | [diff] [blame] | 89 | #endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_ |