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

Side by Side Diff: webrtc/common_video/libyuv/scaler.cc

Issue 2020593002: Refactor scaling. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: Address Per's comments. Add I420Buffer::Scale helper method. 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
OLDNEW
(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/common_video/libyuv/include/scaler.h"
12
13 #include <algorithm>
14
15 // NOTE(ajm): Path provided by gyp.
16 #include "libyuv.h" // NOLINT
17
18 namespace webrtc {
19
20 Scaler::Scaler()
21 : method_(kScaleBox),
22 src_width_(0),
23 src_height_(0),
24 dst_width_(0),
25 dst_height_(0),
26 set_(false) {}
27
28 Scaler::~Scaler() {}
29
30 int Scaler::Set(int src_width, int src_height,
31 int dst_width, int dst_height,
32 VideoType src_video_type, VideoType dst_video_type,
33 ScaleMethod method) {
34 set_ = false;
35 if (src_width < 1 || src_height < 1 || dst_width < 1 || dst_height < 1)
36 return -1;
37
38 if (!SupportedVideoType(src_video_type, dst_video_type))
39 return -1;
40
41 src_width_ = src_width;
42 src_height_ = src_height;
43 dst_width_ = dst_width;
44 dst_height_ = dst_height;
45 method_ = method;
46 set_ = true;
47 return 0;
48 }
49
50 // TODO(nisse): Should work with VideoFrameBuffer instead.
51 int Scaler::Scale(const VideoFrame& src_frame, VideoFrame* dst_frame) {
52 assert(dst_frame);
53 if (src_frame.IsZeroSize())
54 return -1;
55 if (!set_)
56 return -2;
57
58 // Making sure that destination frame is of sufficient size.
59 dst_frame->set_video_frame_buffer(
60 buffer_pool_.CreateBuffer(dst_width_, dst_height_));
61
62 // We want to preserve aspect ratio instead of stretching the frame.
63 // Therefore, we need to crop the source frame. Calculate the largest center
64 // aligned region of the source frame that can be used.
65 const int cropped_src_width =
66 std::min(src_width_, dst_width_ * src_height_ / dst_height_);
67 const int cropped_src_height =
68 std::min(src_height_, dst_height_ * src_width_ / dst_width_);
69 // Make sure the offsets are even to avoid rounding errors for the U/V planes.
70 const int src_offset_x = ((src_width_ - cropped_src_width) / 2) & ~1;
71 const int src_offset_y = ((src_height_ - cropped_src_height) / 2) & ~1;
72
73 const uint8_t* y_ptr =
74 src_frame.video_frame_buffer()->DataY() +
75 src_offset_y * src_frame.video_frame_buffer()->StrideY() +
76 src_offset_x;
77 const uint8_t* u_ptr =
78 src_frame.video_frame_buffer()->DataU() +
79 src_offset_y / 2 * src_frame.video_frame_buffer()->StrideU() +
80 src_offset_x / 2;
81 const uint8_t* v_ptr =
82 src_frame.video_frame_buffer()->DataV() +
83 src_offset_y / 2 * src_frame.video_frame_buffer()->StrideV() +
84 src_offset_x / 2;
85
86 return libyuv::I420Scale(
87 y_ptr,
88 src_frame.video_frame_buffer()->StrideY(),
89 u_ptr,
90 src_frame.video_frame_buffer()->StrideU(),
91 v_ptr,
92 src_frame.video_frame_buffer()->StrideV(),
93 cropped_src_width, cropped_src_height,
94 dst_frame->video_frame_buffer()->MutableDataY(),
95 dst_frame->video_frame_buffer()->StrideY(),
96 dst_frame->video_frame_buffer()->MutableDataU(),
97 dst_frame->video_frame_buffer()->StrideU(),
98 dst_frame->video_frame_buffer()->MutableDataV(),
99 dst_frame->video_frame_buffer()->StrideV(),
100 dst_width_, dst_height_,
101 libyuv::FilterMode(method_));
102 }
103
104 bool Scaler::SupportedVideoType(VideoType src_video_type,
105 VideoType dst_video_type) {
106 if (src_video_type != dst_video_type)
107 return false;
108
109 if ((src_video_type == kI420) || (src_video_type == kIYUV) ||
110 (src_video_type == kYV12))
111 return true;
112
113 return false;
114 }
115
116 } // namespace webrtc
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698