Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(110)

Side by Side Diff: webrtc/media/base/videoframe.cc

Issue 2020593002: Refactor scaling. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Trivial rebase. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « webrtc/media/base/videoframe.h ('k') | webrtc/media/base/videoframe_unittest.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved. 2 * Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
3 * 3 *
4 * Use of this source code is governed by a BSD-style license 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 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 6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may 7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree. 8 * be found in the AUTHORS file in the root of the source tree.
9 */ 9 */
10 10
11 #include "webrtc/media/base/videoframe.h" 11 #include "webrtc/media/base/videoframe.h"
12 12
13 #include <string.h> 13 #include <string.h>
14 14
15 #include "libyuv/compare.h" 15 #include "libyuv/compare.h"
16 #include "libyuv/planar_functions.h" 16 #include "libyuv/planar_functions.h"
17 #include "libyuv/scale.h" 17 #include "libyuv/scale.h"
18 #include "webrtc/base/arraysize.h" 18 #include "webrtc/base/arraysize.h"
19 #include "webrtc/base/checks.h" 19 #include "webrtc/base/checks.h"
20 #include "webrtc/base/logging.h" 20 #include "webrtc/base/logging.h"
21 #include "webrtc/media/base/videocommon.h" 21 #include "webrtc/media/base/videocommon.h"
22 22
23 namespace cricket { 23 namespace cricket {
24 24
25 // Round to 2 pixels because Chroma channels are half size.
26 #define ROUNDTO2(v) (v & ~1)
27
28 bool VideoFrame::CopyToPlanes(uint8_t* dst_y,
29 uint8_t* dst_u,
30 uint8_t* dst_v,
31 int32_t dst_pitch_y,
32 int32_t dst_pitch_u,
33 int32_t dst_pitch_v) const {
34 const rtc::scoped_refptr<webrtc::VideoFrameBuffer>& buffer =
35 video_frame_buffer();
36 if (!buffer) {
37 LOG(LS_ERROR) << "NULL video buffer.";
38 return false;
39 }
40 int32_t src_width = width();
41 int32_t src_height = height();
42 return libyuv::I420Copy(buffer->DataY(), buffer->StrideY(),
43 buffer->DataU(), buffer->StrideU(),
44 buffer->DataV(), buffer->StrideV(),
45 dst_y, dst_pitch_y,
46 dst_u, dst_pitch_u,
47 dst_v, dst_pitch_v,
48 src_width, src_height) == 0;
49 }
50
51 size_t VideoFrame::ConvertToRgbBuffer(uint32_t to_fourcc, 25 size_t VideoFrame::ConvertToRgbBuffer(uint32_t to_fourcc,
52 uint8_t* buffer, 26 uint8_t* buffer,
53 size_t size, 27 size_t size,
54 int stride_rgb) const { 28 int stride_rgb) const {
55 const size_t needed = std::abs(stride_rgb) * static_cast<size_t>(height()); 29 const size_t needed = std::abs(stride_rgb) * static_cast<size_t>(height());
56 if (size < needed) { 30 if (size < needed) {
57 LOG(LS_WARNING) << "RGB buffer is not large enough"; 31 LOG(LS_WARNING) << "RGB buffer is not large enough";
58 return needed; 32 return needed;
59 } 33 }
60 34
61 if (libyuv::ConvertFromI420( 35 if (libyuv::ConvertFromI420(
62 video_frame_buffer()->DataY(), video_frame_buffer()->StrideY(), 36 video_frame_buffer()->DataY(), video_frame_buffer()->StrideY(),
63 video_frame_buffer()->DataU(), video_frame_buffer()->StrideU(), 37 video_frame_buffer()->DataU(), video_frame_buffer()->StrideU(),
64 video_frame_buffer()->DataV(), video_frame_buffer()->StrideV(), 38 video_frame_buffer()->DataV(), video_frame_buffer()->StrideV(),
65 buffer, stride_rgb, width(), height(), to_fourcc)) { 39 buffer, stride_rgb, width(), height(), to_fourcc)) {
66 LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; 40 LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc;
67 return 0; // 0 indicates error 41 return 0; // 0 indicates error
68 } 42 }
69 return needed; 43 return needed;
70 } 44 }
71 45
72 // TODO(fbarchard): Handle odd width/height with rounding.
73 // TODO(nisse): If method is kept, switch to using int instead of
74 // size_t and int32_t.
75 void VideoFrame::StretchToPlanes(uint8_t* dst_y,
76 uint8_t* dst_u,
77 uint8_t* dst_v,
78 int32_t dst_pitch_y,
79 int32_t dst_pitch_u,
80 int32_t dst_pitch_v,
81 size_t dst_width,
82 size_t dst_height,
83 bool interpolate,
84 bool vert_crop) const {
85 if (!video_frame_buffer()) {
86 LOG(LS_ERROR) << "NULL frame buffer.";
87 return;
88 }
89
90 size_t src_width = width();
91 size_t src_height = height();
92 if (dst_width == src_width && dst_height == src_height) {
93 CopyToPlanes(dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v);
94 return;
95 }
96 const uint8_t* src_y = video_frame_buffer()->DataY();
97 const uint8_t* src_u = video_frame_buffer()->DataU();
98 const uint8_t* src_v = video_frame_buffer()->DataV();
99
100 if (vert_crop) {
101 // Adjust the input width:height ratio to be the same as the output ratio.
102 if (src_width * dst_height > src_height * dst_width) {
103 // Reduce the input width, but keep size/position aligned for YuvScaler
104 src_width = ROUNDTO2(src_height * dst_width / dst_height);
105 int32_t iwidth_offset = ROUNDTO2((width() - src_width) / 2);
106 src_y += iwidth_offset;
107 src_u += iwidth_offset / 2;
108 src_v += iwidth_offset / 2;
109 } else if (src_width * dst_height < src_height * dst_width) {
110 // Reduce the input height.
111 src_height = src_width * dst_height / dst_width;
112 int32_t iheight_offset =
113 static_cast<int32_t>((height() - src_height) >> 2);
114 iheight_offset <<= 1; // Ensure that iheight_offset is even.
115 src_y += iheight_offset * video_frame_buffer()->StrideY();
116 src_u += iheight_offset / 2 * video_frame_buffer()->StrideU();
117 src_v += iheight_offset / 2 * video_frame_buffer()->StrideV();
118 }
119 }
120
121 // Scale to the output I420 frame.
122 libyuv::Scale(src_y, src_u, src_v, video_frame_buffer()->StrideY(),
123 video_frame_buffer()->StrideU(),
124 video_frame_buffer()->StrideV(),
125 static_cast<int>(src_width), static_cast<int>(src_height),
126 dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v,
127 static_cast<int>(dst_width), static_cast<int>(dst_height),
128 interpolate);
129 }
130
131 void VideoFrame::StretchToFrame(VideoFrame* dst,
132 bool interpolate, bool vert_crop) const {
133 if (!dst) {
134 LOG(LS_ERROR) << "NULL dst pointer.";
135 return;
136 }
137
138 StretchToPlanes(dst->video_frame_buffer()->MutableDataY(),
139 dst->video_frame_buffer()->MutableDataU(),
140 dst->video_frame_buffer()->MutableDataV(),
141 dst->video_frame_buffer()->StrideY(),
142 dst->video_frame_buffer()->StrideU(),
143 dst->video_frame_buffer()->StrideV(),
144 dst->width(), dst->height(),
145 interpolate, vert_crop);
146 dst->SetTimeStamp(GetTimeStamp());
147 // Stretched frame should have the same rotation as the source.
148 dst->set_rotation(rotation());
149 }
150
151 static const size_t kMaxSampleSize = 1000000000u; 46 static const size_t kMaxSampleSize = 1000000000u;
152 // Returns whether a sample is valid. 47 // Returns whether a sample is valid.
153 bool VideoFrame::Validate(uint32_t fourcc, 48 bool VideoFrame::Validate(uint32_t fourcc,
154 int w, 49 int w,
155 int h, 50 int h,
156 const uint8_t* sample, 51 const uint8_t* sample,
157 size_t sample_size) { 52 size_t sample_size) {
158 if (h < 0) { 53 if (h < 0) {
159 h = -h; 54 h = -h;
160 } 55 }
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 << " expected: " << expected_size 186 << " expected: " << expected_size
292 << " sample[0..3]: " << static_cast<int>(four_samples[0]) 187 << " sample[0..3]: " << static_cast<int>(four_samples[0])
293 << ", " << static_cast<int>(four_samples[1]) 188 << ", " << static_cast<int>(four_samples[1])
294 << ", " << static_cast<int>(four_samples[2]) 189 << ", " << static_cast<int>(four_samples[2])
295 << ", " << static_cast<int>(four_samples[3]); 190 << ", " << static_cast<int>(four_samples[3]);
296 } 191 }
297 return true; 192 return true;
298 } 193 }
299 194
300 } // namespace cricket 195 } // namespace cricket
OLDNEW
« no previous file with comments | « webrtc/media/base/videoframe.h ('k') | webrtc/media/base/videoframe_unittest.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698