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