OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. | |
3 * | |
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. | |
9 */ | |
10 | |
11 #include "webrtc/modules/video_processing/main/source/frame_preprocessor.h" | |
12 | |
13 namespace webrtc { | |
14 | |
15 VPMFramePreprocessor::VPMFramePreprocessor() | |
16 : content_metrics_(NULL), | |
17 resampled_frame_(), | |
18 enable_ca_(false), | |
19 frame_cnt_(0) { | |
20 spatial_resampler_ = new VPMSimpleSpatialResampler(); | |
21 ca_ = new VPMContentAnalysis(true); | |
22 vd_ = new VPMVideoDecimator(); | |
23 } | |
24 | |
25 VPMFramePreprocessor::~VPMFramePreprocessor() { | |
26 Reset(); | |
27 delete spatial_resampler_; | |
28 delete ca_; | |
29 delete vd_; | |
30 } | |
31 | |
32 void VPMFramePreprocessor::Reset() { | |
33 ca_->Release(); | |
34 vd_->Reset(); | |
35 content_metrics_ = NULL; | |
36 spatial_resampler_->Reset(); | |
37 enable_ca_ = false; | |
38 frame_cnt_ = 0; | |
39 } | |
40 | |
41 void VPMFramePreprocessor::EnableTemporalDecimation(bool enable) { | |
42 vd_->EnableTemporalDecimation(enable); | |
43 } | |
44 | |
45 void VPMFramePreprocessor::EnableContentAnalysis(bool enable) { | |
46 enable_ca_ = enable; | |
47 } | |
48 | |
49 void VPMFramePreprocessor::SetInputFrameResampleMode( | |
50 VideoFrameResampling resampling_mode) { | |
51 spatial_resampler_->SetInputFrameResampleMode(resampling_mode); | |
52 } | |
53 | |
54 int32_t VPMFramePreprocessor::SetTargetResolution( | |
55 uint32_t width, uint32_t height, uint32_t frame_rate) { | |
56 if ( (width == 0) || (height == 0) || (frame_rate == 0)) { | |
57 return VPM_PARAMETER_ERROR; | |
58 } | |
59 int32_t ret_val = 0; | |
60 ret_val = spatial_resampler_->SetTargetFrameSize(width, height); | |
61 | |
62 if (ret_val < 0) return ret_val; | |
63 | |
64 vd_->SetTargetFramerate(frame_rate); | |
65 return VPM_OK; | |
66 } | |
67 | |
68 void VPMFramePreprocessor::SetTargetFramerate(int frame_rate) { | |
69 if (frame_rate == -1) { | |
70 vd_->EnableTemporalDecimation(false); | |
71 } else { | |
72 vd_->EnableTemporalDecimation(true); | |
73 vd_->SetTargetFramerate(frame_rate); | |
74 } | |
75 } | |
76 | |
77 void VPMFramePreprocessor::UpdateIncomingframe_rate() { | |
78 vd_->UpdateIncomingframe_rate(); | |
79 } | |
80 | |
81 uint32_t VPMFramePreprocessor::Decimatedframe_rate() { | |
82 return vd_->Decimatedframe_rate(); | |
83 } | |
84 | |
85 | |
86 uint32_t VPMFramePreprocessor::DecimatedWidth() const { | |
87 return spatial_resampler_->TargetWidth(); | |
88 } | |
89 | |
90 | |
91 uint32_t VPMFramePreprocessor::DecimatedHeight() const { | |
92 return spatial_resampler_->TargetHeight(); | |
93 } | |
94 | |
95 int32_t VPMFramePreprocessor::PreprocessFrame(const VideoFrame& frame, | |
96 VideoFrame** processed_frame) { | |
97 if (frame.IsZeroSize()) { | |
98 return VPM_PARAMETER_ERROR; | |
99 } | |
100 | |
101 vd_->UpdateIncomingframe_rate(); | |
102 | |
103 if (vd_->DropFrame()) { | |
104 return 1; // drop 1 frame | |
105 } | |
106 | |
107 // Resizing incoming frame if needed. Otherwise, remains NULL. | |
108 // We are not allowed to resample the input frame (must make a copy of it). | |
109 *processed_frame = NULL; | |
110 if (spatial_resampler_->ApplyResample(frame.width(), frame.height())) { | |
111 int32_t ret = spatial_resampler_->ResampleFrame(frame, &resampled_frame_); | |
112 if (ret != VPM_OK) return ret; | |
113 *processed_frame = &resampled_frame_; | |
114 } | |
115 | |
116 // Perform content analysis on the frame to be encoded. | |
117 if (enable_ca_) { | |
118 // Compute new metrics every |kSkipFramesCA| frames, starting with | |
119 // the first frame. | |
120 if (frame_cnt_ % kSkipFrameCA == 0) { | |
121 if (*processed_frame == NULL) { | |
122 content_metrics_ = ca_->ComputeContentMetrics(frame); | |
123 } else { | |
124 content_metrics_ = ca_->ComputeContentMetrics(resampled_frame_); | |
125 } | |
126 } | |
127 ++frame_cnt_; | |
128 } | |
129 return VPM_OK; | |
130 } | |
131 | |
132 VideoContentMetrics* VPMFramePreprocessor::ContentMetrics() const { | |
133 return content_metrics_; | |
134 } | |
135 | |
136 } // namespace | |
OLD | NEW |