blob: 4bb65748812acb75eba040564617ea32285c37e1 [file] [log] [blame]
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00001/*
kjellander1afca732016-02-07 20:46:45 -08002 * Copyright (c) 2010 The WebRTC project authors. All Rights Reserved.
jlmiller@webrtc.org5f93d0a2015-01-20 21:36:13 +00003 *
kjellander1afca732016-02-07 20:46:45 -08004 * 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.org5f93d0a2015-01-20 21:36:13 +00009 */
henrike@webrtc.org28e20752013-07-10 00:45:36 +000010
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef MEDIA_BASE_VIDEOADAPTER_H_
12#define MEDIA_BASE_VIDEOADAPTER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
Danil Chapovalov00c71832018-06-15 15:58:38 +020014#include "absl/types/optional.h"
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020015#include "media/base/videocommon.h"
16#include "rtc_base/constructormagic.h"
17#include "rtc_base/criticalsection.h"
henrike@webrtc.org28e20752013-07-10 00:45:36 +000018
19namespace cricket {
20
henrike@webrtc.org28e20752013-07-10 00:45:36 +000021// VideoAdapter adapts an input video frame to an output frame based on the
22// specified input and output formats. The adaptation includes dropping frames
Per766ad3b2016-04-05 15:23:49 +020023// to reduce frame rate and scaling frames.
24// VideoAdapter is thread safe.
henrike@webrtc.org28e20752013-07-10 00:45:36 +000025class VideoAdapter {
26 public:
27 VideoAdapter();
sprangc5d62e22017-04-02 23:53:04 -070028 // The output frames will have height and width that is divisible by
29 // |required_resolution_alignment|.
30 explicit VideoAdapter(int required_resolution_alignment);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000031 virtual ~VideoAdapter();
32
nisse47ac4622016-05-25 08:47:01 -070033 // Return the adapted resolution and cropping parameters given the
34 // input resolution. The input frame should first be cropped, then
35 // scaled to the final output resolution. Returns true if the frame
36 // should be adapted, and false if it should be dropped.
37 bool AdaptFrameResolution(int in_width,
magjed709f73c2016-05-13 10:26:00 -070038 int in_height,
magjed604abe02016-05-19 06:05:40 -070039 int64_t in_timestamp_ns,
magjed709f73c2016-05-13 10:26:00 -070040 int* cropped_width,
41 int* cropped_height,
42 int* out_width,
43 int* out_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000044
Per766ad3b2016-04-05 15:23:49 +020045 // Requests the output frame size and frame interval from
magjed709f73c2016-05-13 10:26:00 -070046 // |AdaptFrameResolution| to not be larger than |format|. Also, the input
47 // frame size will be cropped to match the requested aspect ratio. The
48 // requested aspect ratio is orientation agnostic and will be adjusted to
49 // maintain the input orientation, so it doesn't matter if e.g. 1280x720 or
50 // 720x1280 is requested.
Danil Chapovalov00c71832018-06-15 15:58:38 +020051 void OnOutputFormatRequest(const absl::optional<VideoFormat>& format);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000052
sprang84a37592017-02-10 07:04:27 -080053 // Requests the output frame size from |AdaptFrameResolution| to have as close
sprangc5d62e22017-04-02 23:53:04 -070054 // as possible to |target_pixel_count| pixels (if set) but no more than
55 // |max_pixel_count|.
56 // |max_framerate_fps| is essentially analogous to |max_pixel_count|, but for
57 // framerate rather than resolution.
58 // Set |max_pixel_count| and/or |max_framerate_fps| to
59 // std::numeric_limit<int>::max() if no upper limit is desired.
60 void OnResolutionFramerateRequest(
Danil Chapovalov00c71832018-06-15 15:58:38 +020061 const absl::optional<int>& target_pixel_count,
sprangc5d62e22017-04-02 23:53:04 -070062 int max_pixel_count,
63 int max_framerate_fps);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +000064
henrike@webrtc.org28e20752013-07-10 00:45:36 +000065 private:
magjed604abe02016-05-19 06:05:40 -070066 // Determine if frame should be dropped based on input fps and requested fps.
67 bool KeepFrame(int64_t in_timestamp_ns);
68
Per766ad3b2016-04-05 15:23:49 +020069 int frames_in_; // Number of input frames.
70 int frames_out_; // Number of output frames.
71 int frames_scaled_; // Number of frames scaled.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000072 int adaption_changes_; // Number of changes in scale factor.
Per766ad3b2016-04-05 15:23:49 +020073 int previous_width_; // Previous adapter output width.
74 int previous_height_; // Previous adapter output height.
kthelgasonc8474172016-12-08 08:04:51 -080075 // Resolution must be divisible by this factor.
76 const int required_resolution_alignment_;
magjed604abe02016-05-19 06:05:40 -070077 // The target timestamp for the next frame based on requested format.
Danil Chapovalov00c71832018-06-15 15:58:38 +020078 absl::optional<int64_t> next_frame_timestamp_ns_
danilchapa37de392017-09-09 04:17:22 -070079 RTC_GUARDED_BY(critical_section_);
Per766ad3b2016-04-05 15:23:49 +020080
81 // Max number of pixels requested via calls to OnOutputFormatRequest,
82 // OnResolutionRequest respectively.
83 // The adapted output format is the minimum of these.
Danil Chapovalov00c71832018-06-15 15:58:38 +020084 absl::optional<VideoFormat> requested_format_
danilchapa37de392017-09-09 04:17:22 -070085 RTC_GUARDED_BY(critical_section_);
86 int resolution_request_target_pixel_count_ RTC_GUARDED_BY(critical_section_);
87 int resolution_request_max_pixel_count_ RTC_GUARDED_BY(critical_section_);
88 int max_framerate_request_ RTC_GUARDED_BY(critical_section_);
Per766ad3b2016-04-05 15:23:49 +020089
henrike@webrtc.org28e20752013-07-10 00:45:36 +000090 // The critical section to protect the above variables.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000091 rtc::CriticalSection critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000092
henrikg3c089d72015-09-16 05:37:44 -070093 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000094};
95
henrike@webrtc.org28e20752013-07-10 00:45:36 +000096} // namespace cricket
97
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020098#endif // MEDIA_BASE_VIDEOADAPTER_H_