OLD | NEW |
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 |
(...skipping 17 matching lines...) Expand all Loading... |
28 bool VideoFrame::CopyToPlanes(uint8_t* dst_y, | 28 bool VideoFrame::CopyToPlanes(uint8_t* dst_y, |
29 uint8_t* dst_u, | 29 uint8_t* dst_u, |
30 uint8_t* dst_v, | 30 uint8_t* dst_v, |
31 int32_t dst_pitch_y, | 31 int32_t dst_pitch_y, |
32 int32_t dst_pitch_u, | 32 int32_t dst_pitch_u, |
33 int32_t dst_pitch_v) const { | 33 int32_t dst_pitch_v) const { |
34 if (!GetYPlane() || !GetUPlane() || !GetVPlane()) { | 34 if (!GetYPlane() || !GetUPlane() || !GetVPlane()) { |
35 LOG(LS_ERROR) << "NULL plane pointer."; | 35 LOG(LS_ERROR) << "NULL plane pointer."; |
36 return false; | 36 return false; |
37 } | 37 } |
38 int32_t src_width = static_cast<int>(GetWidth()); | 38 int32_t src_width = width(); |
39 int32_t src_height = static_cast<int>(GetHeight()); | 39 int32_t src_height = height(); |
40 return libyuv::I420Copy(GetYPlane(), GetYPitch(), | 40 return libyuv::I420Copy(GetYPlane(), GetYPitch(), |
41 GetUPlane(), GetUPitch(), | 41 GetUPlane(), GetUPitch(), |
42 GetVPlane(), GetVPitch(), | 42 GetVPlane(), GetVPitch(), |
43 dst_y, dst_pitch_y, | 43 dst_y, dst_pitch_y, |
44 dst_u, dst_pitch_u, | 44 dst_u, dst_pitch_u, |
45 dst_v, dst_pitch_v, | 45 dst_v, dst_pitch_v, |
46 src_width, src_height) == 0; | 46 src_width, src_height) == 0; |
47 } | 47 } |
48 | 48 |
49 void VideoFrame::CopyToFrame(VideoFrame* dst) const { | 49 void VideoFrame::CopyToFrame(VideoFrame* dst) const { |
50 if (!dst) { | 50 if (!dst) { |
51 LOG(LS_ERROR) << "NULL dst pointer."; | 51 LOG(LS_ERROR) << "NULL dst pointer."; |
52 return; | 52 return; |
53 } | 53 } |
54 | 54 |
55 CopyToPlanes(dst->GetYPlane(), dst->GetUPlane(), dst->GetVPlane(), | 55 CopyToPlanes(dst->GetYPlane(), dst->GetUPlane(), dst->GetVPlane(), |
56 dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch()); | 56 dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch()); |
57 } | 57 } |
58 | 58 |
59 size_t VideoFrame::ConvertToRgbBuffer(uint32_t to_fourcc, | 59 size_t VideoFrame::ConvertToRgbBuffer(uint32_t to_fourcc, |
60 uint8_t* buffer, | 60 uint8_t* buffer, |
61 size_t size, | 61 size_t size, |
62 int stride_rgb) const { | 62 int stride_rgb) const { |
63 const size_t needed = std::abs(stride_rgb) * GetHeight(); | 63 const size_t needed = std::abs(stride_rgb) * static_cast<size_t>(height()); |
64 if (size < needed) { | 64 if (size < needed) { |
65 LOG(LS_WARNING) << "RGB buffer is not large enough"; | 65 LOG(LS_WARNING) << "RGB buffer is not large enough"; |
66 return needed; | 66 return needed; |
67 } | 67 } |
68 | 68 |
69 if (libyuv::ConvertFromI420(GetYPlane(), GetYPitch(), GetUPlane(), | 69 if (libyuv::ConvertFromI420(GetYPlane(), GetYPitch(), GetUPlane(), |
70 GetUPitch(), GetVPlane(), GetVPitch(), buffer, | 70 GetUPitch(), GetVPlane(), GetVPitch(), buffer, |
71 stride_rgb, static_cast<int>(GetWidth()), | 71 stride_rgb, width(), height(), to_fourcc)) { |
72 static_cast<int>(GetHeight()), to_fourcc)) { | |
73 LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; | 72 LOG(LS_ERROR) << "RGB type not supported: " << to_fourcc; |
74 return 0; // 0 indicates error | 73 return 0; // 0 indicates error |
75 } | 74 } |
76 return needed; | 75 return needed; |
77 } | 76 } |
78 | 77 |
79 // TODO(fbarchard): Handle odd width/height with rounding. | 78 // TODO(fbarchard): Handle odd width/height with rounding. |
| 79 // TODO(nisse): If method is kept, switch to using int instead of |
| 80 // size_t and int32_t. |
80 void VideoFrame::StretchToPlanes(uint8_t* dst_y, | 81 void VideoFrame::StretchToPlanes(uint8_t* dst_y, |
81 uint8_t* dst_u, | 82 uint8_t* dst_u, |
82 uint8_t* dst_v, | 83 uint8_t* dst_v, |
83 int32_t dst_pitch_y, | 84 int32_t dst_pitch_y, |
84 int32_t dst_pitch_u, | 85 int32_t dst_pitch_u, |
85 int32_t dst_pitch_v, | 86 int32_t dst_pitch_v, |
86 size_t width, | 87 size_t dst_width, |
87 size_t height, | 88 size_t dst_height, |
88 bool interpolate, | 89 bool interpolate, |
89 bool vert_crop) const { | 90 bool vert_crop) const { |
90 if (!GetYPlane() || !GetUPlane() || !GetVPlane()) { | 91 if (!GetYPlane() || !GetUPlane() || !GetVPlane()) { |
91 LOG(LS_ERROR) << "NULL plane pointer."; | 92 LOG(LS_ERROR) << "NULL plane pointer."; |
92 return; | 93 return; |
93 } | 94 } |
94 | 95 |
95 size_t src_width = GetWidth(); | 96 size_t src_width = width(); |
96 size_t src_height = GetHeight(); | 97 size_t src_height = height(); |
97 if (width == src_width && height == src_height) { | 98 if (dst_width == src_width && dst_height == src_height) { |
98 CopyToPlanes(dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v); | 99 CopyToPlanes(dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v); |
99 return; | 100 return; |
100 } | 101 } |
101 const uint8_t* src_y = GetYPlane(); | 102 const uint8_t* src_y = GetYPlane(); |
102 const uint8_t* src_u = GetUPlane(); | 103 const uint8_t* src_u = GetUPlane(); |
103 const uint8_t* src_v = GetVPlane(); | 104 const uint8_t* src_v = GetVPlane(); |
104 | 105 |
105 if (vert_crop) { | 106 if (vert_crop) { |
106 // Adjust the input width:height ratio to be the same as the output ratio. | 107 // Adjust the input width:height ratio to be the same as the output ratio. |
107 if (src_width * height > src_height * width) { | 108 if (src_width * dst_height > src_height * dst_width) { |
108 // Reduce the input width, but keep size/position aligned for YuvScaler | 109 // Reduce the input width, but keep size/position aligned for YuvScaler |
109 src_width = ROUNDTO2(src_height * width / height); | 110 src_width = ROUNDTO2(src_height * dst_width / dst_height); |
110 int32_t iwidth_offset = ROUNDTO2((GetWidth() - src_width) / 2); | 111 int32_t iwidth_offset = ROUNDTO2((width() - src_width) / 2); |
111 src_y += iwidth_offset; | 112 src_y += iwidth_offset; |
112 src_u += iwidth_offset / 2; | 113 src_u += iwidth_offset / 2; |
113 src_v += iwidth_offset / 2; | 114 src_v += iwidth_offset / 2; |
114 } else if (src_width * height < src_height * width) { | 115 } else if (src_width * dst_height < src_height * dst_width) { |
115 // Reduce the input height. | 116 // Reduce the input height. |
116 src_height = src_width * height / width; | 117 src_height = src_width * dst_height / dst_width; |
117 int32_t iheight_offset = | 118 int32_t iheight_offset = |
118 static_cast<int32_t>((GetHeight() - src_height) >> 2); | 119 static_cast<int32_t>((height() - src_height) >> 2); |
119 iheight_offset <<= 1; // Ensure that iheight_offset is even. | 120 iheight_offset <<= 1; // Ensure that iheight_offset is even. |
120 src_y += iheight_offset * GetYPitch(); | 121 src_y += iheight_offset * GetYPitch(); |
121 src_u += iheight_offset / 2 * GetUPitch(); | 122 src_u += iheight_offset / 2 * GetUPitch(); |
122 src_v += iheight_offset / 2 * GetVPitch(); | 123 src_v += iheight_offset / 2 * GetVPitch(); |
123 } | 124 } |
124 } | 125 } |
125 | 126 |
126 // Scale to the output I420 frame. | 127 // Scale to the output I420 frame. |
127 libyuv::Scale(src_y, src_u, src_v, | 128 libyuv::Scale(src_y, src_u, src_v, |
128 GetYPitch(), GetUPitch(), GetVPitch(), | 129 GetYPitch(), GetUPitch(), GetVPitch(), |
129 static_cast<int>(src_width), static_cast<int>(src_height), | 130 static_cast<int>(src_width), static_cast<int>(src_height), |
130 dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v, | 131 dst_y, dst_u, dst_v, dst_pitch_y, dst_pitch_u, dst_pitch_v, |
131 static_cast<int>(width), static_cast<int>(height), interpolate); | 132 static_cast<int>(dst_width), static_cast<int>(dst_height), |
| 133 interpolate); |
132 } | 134 } |
133 | 135 |
134 void VideoFrame::StretchToFrame(VideoFrame* dst, | 136 void VideoFrame::StretchToFrame(VideoFrame* dst, |
135 bool interpolate, bool vert_crop) const { | 137 bool interpolate, bool vert_crop) const { |
136 if (!dst) { | 138 if (!dst) { |
137 LOG(LS_ERROR) << "NULL dst pointer."; | 139 LOG(LS_ERROR) << "NULL dst pointer."; |
138 return; | 140 return; |
139 } | 141 } |
140 | 142 |
141 StretchToPlanes(dst->GetYPlane(), dst->GetUPlane(), dst->GetVPlane(), | 143 StretchToPlanes(dst->GetYPlane(), dst->GetUPlane(), dst->GetVPlane(), |
142 dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch(), | 144 dst->GetYPitch(), dst->GetUPitch(), dst->GetVPitch(), |
143 dst->GetWidth(), dst->GetHeight(), | 145 dst->width(), dst->height(), |
144 interpolate, vert_crop); | 146 interpolate, vert_crop); |
145 dst->SetTimeStamp(GetTimeStamp()); | 147 dst->SetTimeStamp(GetTimeStamp()); |
146 // Stretched frame should have the same rotation as the source. | 148 // Stretched frame should have the same rotation as the source. |
147 dst->SetRotation(GetVideoRotation()); | 149 dst->SetRotation(GetVideoRotation()); |
148 } | 150 } |
149 | 151 |
150 VideoFrame* VideoFrame::Stretch(size_t dst_width, size_t dst_height, | 152 VideoFrame* VideoFrame::Stretch(size_t dst_width, size_t dst_height, |
151 bool interpolate, bool vert_crop) const { | 153 bool interpolate, bool vert_crop) const { |
152 VideoFrame* dest = CreateEmptyFrame(static_cast<int>(dst_width), | 154 VideoFrame* dest = CreateEmptyFrame(static_cast<int>(dst_width), |
153 static_cast<int>(dst_height), | 155 static_cast<int>(dst_height), |
154 GetTimeStamp()); | 156 GetTimeStamp()); |
155 if (dest) { | 157 if (dest) { |
156 StretchToFrame(dest, interpolate, vert_crop); | 158 StretchToFrame(dest, interpolate, vert_crop); |
157 } | 159 } |
158 return dest; | 160 return dest; |
159 } | 161 } |
160 | 162 |
161 bool VideoFrame::SetToBlack() { | 163 bool VideoFrame::SetToBlack() { |
162 return libyuv::I420Rect(GetYPlane(), GetYPitch(), | 164 return libyuv::I420Rect(GetYPlane(), GetYPitch(), |
163 GetUPlane(), GetUPitch(), | 165 GetUPlane(), GetUPitch(), |
164 GetVPlane(), GetVPitch(), | 166 GetVPlane(), GetVPitch(), |
165 0, 0, | 167 0, 0, |
166 static_cast<int>(GetWidth()), | 168 width(), height(), |
167 static_cast<int>(GetHeight()), | |
168 16, 128, 128) == 0; | 169 16, 128, 128) == 0; |
169 } | 170 } |
170 | 171 |
171 static const size_t kMaxSampleSize = 1000000000u; | 172 static const size_t kMaxSampleSize = 1000000000u; |
172 // Returns whether a sample is valid. | 173 // Returns whether a sample is valid. |
173 bool VideoFrame::Validate(uint32_t fourcc, | 174 bool VideoFrame::Validate(uint32_t fourcc, |
174 int w, | 175 int w, |
175 int h, | 176 int h, |
176 const uint8_t* sample, | 177 const uint8_t* sample, |
177 size_t sample_size) { | 178 size_t sample_size) { |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
311 << " expected: " << expected_size | 312 << " expected: " << expected_size |
312 << " sample[0..3]: " << static_cast<int>(four_samples[0]) | 313 << " sample[0..3]: " << static_cast<int>(four_samples[0]) |
313 << ", " << static_cast<int>(four_samples[1]) | 314 << ", " << static_cast<int>(four_samples[1]) |
314 << ", " << static_cast<int>(four_samples[2]) | 315 << ", " << static_cast<int>(four_samples[2]) |
315 << ", " << static_cast<int>(four_samples[3]); | 316 << ", " << static_cast<int>(four_samples[3]); |
316 } | 317 } |
317 return true; | 318 return true; |
318 } | 319 } |
319 | 320 |
320 } // namespace cricket | 321 } // namespace cricket |
OLD | NEW |