blob: 553c08577490c667a75a36b783a49c502eae09e8 [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
Per766ad3b2016-04-05 15:23:49 +020011#ifndef WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
kjellandera96e2d72016-02-04 23:52:28 -080012#define WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_
henrike@webrtc.org28e20752013-07-10 00:45:36 +000013
kwiberg4485ffb2016-04-26 08:14:39 -070014#include "webrtc/base/constructormagic.h"
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000015#include "webrtc/base/criticalsection.h"
perkj2d5f0912016-02-29 00:04:41 -080016#include "webrtc/base/optional.h"
kjellandera96e2d72016-02-04 23:52:28 -080017#include "webrtc/media/base/videocommon.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();
kthelgasonc8474172016-12-08 08:04:51 -080028 VideoAdapter(int required_resolution_alignment);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000029 virtual ~VideoAdapter();
30
nisse47ac4622016-05-25 08:47:01 -070031 // 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,
magjed709f73c2016-05-13 10:26:00 -070036 int in_height,
magjed604abe02016-05-19 06:05:40 -070037 int64_t in_timestamp_ns,
magjed709f73c2016-05-13 10:26:00 -070038 int* cropped_width,
39 int* cropped_height,
40 int* out_width,
41 int* out_height);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000042
Per766ad3b2016-04-05 15:23:49 +020043 // Requests the output frame size and frame interval from
magjed709f73c2016-05-13 10:26:00 -070044 // |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.
Per766ad3b2016-04-05 15:23:49 +020049 void OnOutputFormatRequest(const VideoFormat& format);
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000050
Per766ad3b2016-04-05 15:23:49 +020051 // Requests the output frame size from |AdaptFrameResolution| to not have
52 // more than |max_pixel_count| pixels and have "one step" up more pixels than
53 // max_pixel_count_step_up.
54 void OnResolutionRequest(rtc::Optional<int> max_pixel_count,
55 rtc::Optional<int> max_pixel_count_step_up);
buildbot@webrtc.org71dffb72014-06-24 07:24:49 +000056
henrike@webrtc.org28e20752013-07-10 00:45:36 +000057 private:
magjed604abe02016-05-19 06:05:40 -070058 // Determine if frame should be dropped based on input fps and requested fps.
59 bool KeepFrame(int64_t in_timestamp_ns);
60
Per766ad3b2016-04-05 15:23:49 +020061 int frames_in_; // Number of input frames.
62 int frames_out_; // Number of output frames.
63 int frames_scaled_; // Number of frames scaled.
wu@webrtc.orgcadf9042013-08-30 21:24:16 +000064 int adaption_changes_; // Number of changes in scale factor.
Per766ad3b2016-04-05 15:23:49 +020065 int previous_width_; // Previous adapter output width.
66 int previous_height_; // Previous adapter output height.
kthelgasonc8474172016-12-08 08:04:51 -080067 // Resolution must be divisible by this factor.
68 const int required_resolution_alignment_;
magjed604abe02016-05-19 06:05:40 -070069 // The target timestamp for the next frame based on requested format.
70 rtc::Optional<int64_t> next_frame_timestamp_ns_ GUARDED_BY(critical_section_);
Per766ad3b2016-04-05 15:23:49 +020071
72 // Max number of pixels requested via calls to OnOutputFormatRequest,
73 // OnResolutionRequest respectively.
74 // The adapted output format is the minimum of these.
magjed709f73c2016-05-13 10:26:00 -070075 rtc::Optional<VideoFormat> requested_format_ GUARDED_BY(critical_section_);
76 int resolution_request_max_pixel_count_ GUARDED_BY(critical_section_);
kthelgasonc8474172016-12-08 08:04:51 -080077 bool step_up_ GUARDED_BY(critical_section_);
Per766ad3b2016-04-05 15:23:49 +020078
henrike@webrtc.org28e20752013-07-10 00:45:36 +000079 // The critical section to protect the above variables.
buildbot@webrtc.orgd4e598d2014-07-29 17:36:52 +000080 rtc::CriticalSection critical_section_;
henrike@webrtc.org28e20752013-07-10 00:45:36 +000081
henrikg3c089d72015-09-16 05:37:44 -070082 RTC_DISALLOW_COPY_AND_ASSIGN(VideoAdapter);
henrike@webrtc.org28e20752013-07-10 00:45:36 +000083};
84
henrike@webrtc.org28e20752013-07-10 00:45:36 +000085} // namespace cricket
86
Per766ad3b2016-04-05 15:23:49 +020087#endif // WEBRTC_MEDIA_BASE_VIDEOADAPTER_H_