OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/renderer_host/media/video_capture_device_client.h" | 5 #include "content/browser/renderer_host/media/video_capture_device_client.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/strings/stringprintf.h" | 8 #include "base/strings/stringprintf.h" |
9 #include "base/trace_event/trace_event.h" | 9 #include "base/trace_event/trace_event.h" |
10 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" | 10 #include "content/browser/renderer_host/media/video_capture_buffer_pool.h" |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
66 | 66 |
67 if (last_captured_pixel_format_ != frame_format.pixel_format) { | 67 if (last_captured_pixel_format_ != frame_format.pixel_format) { |
68 OnLog("Pixel format: " + media::VideoCaptureFormat::PixelFormatToString( | 68 OnLog("Pixel format: " + media::VideoCaptureFormat::PixelFormatToString( |
69 frame_format.pixel_format)); | 69 frame_format.pixel_format)); |
70 last_captured_pixel_format_ = frame_format.pixel_format; | 70 last_captured_pixel_format_ = frame_format.pixel_format; |
71 } | 71 } |
72 | 72 |
73 if (!frame_format.IsValid()) | 73 if (!frame_format.IsValid()) |
74 return; | 74 return; |
75 | 75 |
76 // Chopped pixels in width/height in case video capture device has odd | 76 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest |
77 // numbers for width/height. | 77 // bit decomposition of {width, height}, grabbing the odd and even parts. |
78 int chopped_width = 0; | 78 const int chopped_width = frame_format.frame_size.width() & 1; |
79 int chopped_height = 0; | 79 const int chopped_height = frame_format.frame_size.height() & 1; |
80 int new_unrotated_width = frame_format.frame_size.width(); | 80 const int new_unrotated_width = frame_format.frame_size.width() & ~1; |
81 int new_unrotated_height = frame_format.frame_size.height(); | 81 const int new_unrotated_height = frame_format.frame_size.height() & ~1; |
82 | |
83 if (new_unrotated_width & 1) { | |
84 --new_unrotated_width; | |
85 chopped_width = 1; | |
86 } | |
87 if (new_unrotated_height & 1) { | |
88 --new_unrotated_height; | |
89 chopped_height = 1; | |
90 } | |
91 | 82 |
92 int destination_width = new_unrotated_width; | 83 int destination_width = new_unrotated_width; |
93 int destination_height = new_unrotated_height; | 84 int destination_height = new_unrotated_height; |
94 if (rotation == 90 || rotation == 270) { | 85 if (rotation == 90 || rotation == 270) { |
95 destination_width = new_unrotated_height; | 86 destination_width = new_unrotated_height; |
96 destination_height = new_unrotated_width; | 87 destination_height = new_unrotated_width; |
97 } | 88 } |
89 | |
90 DCHECK_EQ(rotation % 90, 0) | |
91 << " Rotation must be a multiple of 90, now: " << rotation; | |
92 libyuv::RotationMode rotation_mode = libyuv::kRotate0; | |
93 if (rotation == 90) | |
94 rotation_mode = libyuv::kRotate90; | |
95 else if (rotation == 180) | |
96 rotation_mode = libyuv::kRotate180; | |
97 else if (rotation == 270) | |
98 rotation_mode = libyuv::kRotate270; | |
99 | |
98 const gfx::Size dimensions(destination_width, destination_height); | 100 const gfx::Size dimensions(destination_width, destination_height); |
99 if (!VideoFrame::IsValidConfig(VideoFrame::I420, | 101 if (!VideoFrame::IsValidConfig(VideoFrame::I420, |
100 dimensions, | 102 dimensions, |
101 gfx::Rect(dimensions), | 103 gfx::Rect(dimensions), |
102 dimensions)) { | 104 dimensions)) { |
103 return; | 105 return; |
104 } | 106 } |
105 | 107 |
106 scoped_refptr<Buffer> buffer = ReserveOutputBuffer(VideoFrame::I420, | 108 scoped_refptr<Buffer> buffer = ReserveOutputBuffer(VideoFrame::I420, |
107 dimensions); | 109 dimensions); |
108 if (!buffer.get()) | 110 if (!buffer.get()) |
109 return; | 111 return; |
110 | 112 |
111 uint8* const yplane = reinterpret_cast<uint8*>(buffer->data()); | 113 uint8* const yplane = reinterpret_cast<uint8*>(buffer->data()); |
112 uint8* const uplane = | 114 uint8* const uplane = |
113 yplane + VideoFrame::PlaneAllocationSize(VideoFrame::I420, | 115 yplane + VideoFrame::PlaneAllocationSize(VideoFrame::I420, |
114 VideoFrame::kYPlane, dimensions); | 116 VideoFrame::kYPlane, dimensions); |
115 uint8* const vplane = | 117 uint8* const vplane = |
116 uplane + VideoFrame::PlaneAllocationSize(VideoFrame::I420, | 118 uplane + VideoFrame::PlaneAllocationSize(VideoFrame::I420, |
117 VideoFrame::kUPlane, dimensions); | 119 VideoFrame::kUPlane, dimensions); |
118 int yplane_stride = dimensions.width(); | 120 int yplane_stride = dimensions.width(); |
119 int uv_plane_stride = yplane_stride / 2; | 121 int uv_plane_stride = yplane_stride / 2; |
120 int crop_x = 0; | 122 int crop_x = 0; |
121 int crop_y = 0; | 123 int crop_y = 0; |
122 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; | 124 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; |
123 | 125 |
124 libyuv::RotationMode rotation_mode = libyuv::kRotate0; | |
125 if (rotation == 90) | |
126 rotation_mode = libyuv::kRotate90; | |
127 else if (rotation == 180) | |
128 rotation_mode = libyuv::kRotate180; | |
129 else if (rotation == 270) | |
130 rotation_mode = libyuv::kRotate270; | |
131 | |
132 bool flip = false; | 126 bool flip = false; |
133 switch (frame_format.pixel_format) { | 127 switch (frame_format.pixel_format) { |
134 case media::PIXEL_FORMAT_UNKNOWN: // Color format not set. | 128 case media::PIXEL_FORMAT_UNKNOWN: // Color format not set. |
135 break; | 129 break; |
136 case media::PIXEL_FORMAT_I420: | 130 case media::PIXEL_FORMAT_I420: |
137 DCHECK(!chopped_width && !chopped_height); | 131 DCHECK(!chopped_width && !chopped_height); |
138 origin_colorspace = libyuv::FOURCC_I420; | 132 origin_colorspace = libyuv::FOURCC_I420; |
139 break; | 133 break; |
140 case media::PIXEL_FORMAT_YV12: | 134 case media::PIXEL_FORMAT_YV12: |
141 DCHECK(!chopped_width && !chopped_height); | 135 DCHECK(!chopped_width && !chopped_height); |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
222 BrowserThread::IO, | 216 BrowserThread::IO, |
223 FROM_HERE, | 217 FROM_HERE, |
224 base::Bind( | 218 base::Bind( |
225 &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread, | 219 &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread, |
226 controller_, | 220 controller_, |
227 buffer, | 221 buffer, |
228 frame, | 222 frame, |
229 timestamp)); | 223 timestamp)); |
230 } | 224 } |
231 | 225 |
226 void | |
227 VideoCaptureDeviceClient::OnIncomingCapturedYuvData( | |
228 const uint8* y_data, | |
229 const uint8* u_data, | |
230 const uint8* v_data, | |
231 size_t y_stride, | |
232 size_t u_stride, | |
233 size_t v_stride, | |
234 const VideoCaptureFormat& frame_format, | |
235 int clockwise_rotation, | |
236 const base::TimeTicks& timestamp) { | |
237 TRACE_EVENT0("video", "VideoCaptureController::OnIncomingCapturedYuvData"); | |
238 DCHECK_EQ(frame_format.pixel_format, media::PIXEL_FORMAT_I420); | |
239 DCHECK_EQ(clockwise_rotation, 0) << "Rotation not supported"; | |
240 DCHECK_GE(y_stride, 1u * frame_format.frame_size.width()); | |
241 DCHECK_GE(u_stride, 1u * frame_format.frame_size.width() / 2); | |
magjed_chromium
2015/03/20 07:53:45
It would still be nice to have height + 1 here. Fo
mcasas
2015/03/24 14:24:50
Done.
| |
242 DCHECK_GE(v_stride, 1u * frame_format.frame_size.width() / 2); | |
243 | |
244 scoped_refptr<Buffer> buffer = ReserveOutputBuffer(VideoFrame::I420, | |
245 frame_format.frame_size); | |
246 if (!buffer.get()) | |
247 return; | |
248 | |
249 // Blit (copy) here from y,u,v into buffer.data()). Needed so we can return | |
250 // the parameter buffer synchronously to the driver. | |
251 const size_t y_plane_size = VideoFrame::PlaneAllocationSize(VideoFrame::I420, | |
252 VideoFrame::kYPlane, frame_format.frame_size); | |
253 const size_t u_plane_size = VideoFrame::PlaneAllocationSize(VideoFrame::I420, | |
254 VideoFrame::kUPlane, frame_format.frame_size); | |
255 uint8* const dst_y = reinterpret_cast<uint8*>(buffer->data()); | |
256 uint8* const dst_u = dst_y + y_plane_size; | |
257 uint8* const dst_v = dst_u + u_plane_size; | |
258 | |
259 if (libyuv::I420Copy(y_data, y_stride, | |
260 u_data, u_stride, | |
261 v_data, v_stride, | |
262 dst_y, y_stride, | |
magjed_chromium
2015/03/20 07:53:45
The destination strides are wrong. You calculate |
mcasas
2015/03/24 14:24:50
Changed to using those from VideoFrame::RowBytes()
| |
263 dst_u, u_stride, | |
264 dst_v, v_stride, | |
265 frame_format.frame_size.width(), | |
266 frame_format.frame_size.height())) { | |
267 DLOG(WARNING) << "Failed to copy buffer"; | |
268 return; | |
269 } | |
270 | |
271 scoped_refptr<VideoFrame> video_frame = VideoFrame::WrapExternalYuvData( | |
272 VideoFrame::I420, frame_format.frame_size, | |
273 gfx::Rect(frame_format.frame_size), frame_format.frame_size, y_stride, | |
274 u_stride, v_stride, dst_y, dst_u, dst_v, base::TimeDelta(), | |
275 base::Closure()); | |
276 DCHECK(video_frame.get()); | |
277 | |
278 BrowserThread::PostTask( | |
279 BrowserThread::IO, | |
280 FROM_HERE, | |
281 base::Bind( | |
282 &VideoCaptureController::DoIncomingCapturedVideoFrameOnIOThread, | |
283 controller_, | |
284 buffer, | |
285 video_frame, | |
286 timestamp)); | |
287 }; | |
288 | |
232 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> | 289 scoped_refptr<media::VideoCaptureDevice::Client::Buffer> |
233 VideoCaptureDeviceClient::ReserveOutputBuffer(VideoFrame::Format format, | 290 VideoCaptureDeviceClient::ReserveOutputBuffer(VideoFrame::Format format, |
234 const gfx::Size& dimensions) { | 291 const gfx::Size& dimensions) { |
235 const size_t frame_bytes = VideoFrame::AllocationSize(format, dimensions); | 292 const size_t frame_bytes = VideoFrame::AllocationSize(format, dimensions); |
236 if (format == VideoFrame::NATIVE_TEXTURE) { | 293 if (format == VideoFrame::NATIVE_TEXTURE) { |
237 DCHECK_EQ(dimensions.width(), 0); | 294 DCHECK_EQ(dimensions.width(), 0); |
238 DCHECK_EQ(dimensions.height(), 0); | 295 DCHECK_EQ(dimensions.height(), 0); |
239 } else { | 296 } else { |
240 DLOG_IF(ERROR, frame_bytes == 0) << "Error calculating allocation size"; | 297 DLOG_IF(ERROR, frame_bytes == 0) << "Error calculating allocation size"; |
241 } | 298 } |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
293 } | 350 } |
294 | 351 |
295 void VideoCaptureDeviceClient::OnLog( | 352 void VideoCaptureDeviceClient::OnLog( |
296 const std::string& message) { | 353 const std::string& message) { |
297 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 354 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
298 base::Bind(&VideoCaptureController::DoLogOnIOThread, | 355 base::Bind(&VideoCaptureController::DoLogOnIOThread, |
299 controller_, message)); | 356 controller_, message)); |
300 } | 357 } |
301 | 358 |
302 } // namespace content | 359 } // namespace content |
OLD | NEW |