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 "media/capture/video/video_capture_device_client.h" | 5 #include "media/capture/video/video_capture_device_client.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/command_line.h" | 11 #include "base/command_line.h" |
12 #include "base/location.h" | 12 #include "base/location.h" |
13 #include "base/memory/ptr_util.h" | 13 #include "base/memory/ptr_util.h" |
14 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
15 #include "base/trace_event/trace_event.h" | 15 #include "base/trace_event/trace_event.h" |
16 #include "build/build_config.h" | 16 #include "build/build_config.h" |
17 #include "media/base/bind_to_current_loop.h" | 17 #include "media/base/bind_to_current_loop.h" |
18 #include "media/base/video_capture_types.h" | 18 #include "media/base/video_capture_types.h" |
19 #include "media/base/video_frame.h" | 19 #include "media/base/video_frame.h" |
20 #include "media/capture/video/video_capture_buffer_handle.h" | 20 #include "media/capture/video/video_capture_buffer_handle.h" |
21 #include "media/capture/video/video_capture_buffer_pool.h" | 21 #include "media/capture/video/video_capture_buffer_pool.h" |
22 #include "media/capture/video/video_capture_jpeg_decoder.h" | 22 #include "media/capture/video/video_capture_jpeg_decoder.h" |
23 #include "media/capture/video/video_frame_receiver.h" | 23 #include "media/capture/video/video_frame_receiver.h" |
24 #include "third_party/libyuv/include/libyuv.h" | 24 #include "third_party/libyuv/include/libyuv.h" |
25 | 25 |
26 using media::VideoCaptureFormat; | 26 using media::VideoCaptureFormat; |
27 using media::VideoFrame; | 27 using media::VideoFrame; |
28 using media::VideoFrameMetadata; | 28 using media::VideoFrameMetadata; |
29 | 29 |
| 30 namespace { |
| 31 |
| 32 bool isFormatSupported(media::VideoPixelFormat pixel_format) { |
| 33 // Currently, only I420 and Y16 pixel formats are supported. |
| 34 return (pixel_format == media::PIXEL_FORMAT_I420 || |
| 35 pixel_format == media::PIXEL_FORMAT_Y16); |
| 36 } |
| 37 } |
| 38 |
30 namespace media { | 39 namespace media { |
31 | 40 |
32 // Class combining a Client::Buffer interface implementation and a pool buffer | 41 // Class combining a Client::Buffer interface implementation and a pool buffer |
33 // implementation to guarantee proper cleanup on destruction on our side. | 42 // implementation to guarantee proper cleanup on destruction on our side. |
34 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer { | 43 class AutoReleaseBuffer : public media::VideoCaptureDevice::Client::Buffer { |
35 public: | 44 public: |
36 AutoReleaseBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool, | 45 AutoReleaseBuffer(const scoped_refptr<VideoCaptureBufferPool>& pool, |
37 int buffer_id) | 46 int buffer_id) |
38 : id_(buffer_id), | 47 : id_(buffer_id), |
39 pool_(pool), | 48 pool_(pool), |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
99 !external_jpeg_decoder_initialized_) { | 108 !external_jpeg_decoder_initialized_) { |
100 external_jpeg_decoder_initialized_ = true; | 109 external_jpeg_decoder_initialized_ = true; |
101 external_jpeg_decoder_ = jpeg_decoder_factory_callback_.Run(); | 110 external_jpeg_decoder_ = jpeg_decoder_factory_callback_.Run(); |
102 external_jpeg_decoder_->Initialize(); | 111 external_jpeg_decoder_->Initialize(); |
103 } | 112 } |
104 } | 113 } |
105 | 114 |
106 if (!frame_format.IsValid()) | 115 if (!frame_format.IsValid()) |
107 return; | 116 return; |
108 | 117 |
| 118 // The input |length| can be greater than the required buffer size because of |
| 119 // paddings and/or alignments, but it cannot be smaller. |
| 120 DCHECK_GE(static_cast<size_t>(length), frame_format.ImageAllocationSize()); |
| 121 |
| 122 const bool useFullSize = frame_format.pixel_format == media::PIXEL_FORMAT_Y16; |
| 123 |
109 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest | 124 // |chopped_{width,height} and |new_unrotated_{width,height}| are the lowest |
110 // bit decomposition of {width, height}, grabbing the odd and even parts. | 125 // bit decomposition of {width, height}, grabbing the odd and even parts. |
111 const int chopped_width = frame_format.frame_size.width() & 1; | 126 const int chopped_width = frame_format.frame_size.width() & 1; |
112 const int chopped_height = frame_format.frame_size.height() & 1; | 127 const int chopped_height = frame_format.frame_size.height() & 1; |
113 const int new_unrotated_width = frame_format.frame_size.width() & ~1; | 128 const int new_unrotated_width = (useFullSize) |
114 const int new_unrotated_height = frame_format.frame_size.height() & ~1; | 129 ? frame_format.frame_size.width() |
| 130 : (frame_format.frame_size.width() & ~1); |
| 131 const int new_unrotated_height = |
| 132 (useFullSize) ? frame_format.frame_size.height() |
| 133 : (frame_format.frame_size.height() & ~1); |
115 | 134 |
116 int destination_width = new_unrotated_width; | 135 int destination_width = new_unrotated_width; |
117 int destination_height = new_unrotated_height; | 136 int destination_height = new_unrotated_height; |
118 if (rotation == 90 || rotation == 270) | 137 if (rotation == 90 || rotation == 270) |
119 std::swap(destination_width, destination_height); | 138 std::swap(destination_width, destination_height); |
120 | 139 |
121 DCHECK_EQ(0, rotation % 90) << " Rotation must be a multiple of 90, now: " | 140 DCHECK_EQ(0, rotation % 90) << " Rotation must be a multiple of 90, now: " |
122 << rotation; | 141 << rotation; |
123 libyuv::RotationMode rotation_mode = libyuv::kRotate0; | 142 libyuv::RotationMode rotation_mode = libyuv::kRotate0; |
124 if (rotation == 90) | 143 if (rotation == 90) |
125 rotation_mode = libyuv::kRotate90; | 144 rotation_mode = libyuv::kRotate90; |
126 else if (rotation == 180) | 145 else if (rotation == 180) |
127 rotation_mode = libyuv::kRotate180; | 146 rotation_mode = libyuv::kRotate180; |
128 else if (rotation == 270) | 147 else if (rotation == 270) |
129 rotation_mode = libyuv::kRotate270; | 148 rotation_mode = libyuv::kRotate270; |
130 | 149 |
131 const gfx::Size dimensions(destination_width, destination_height); | 150 const gfx::Size dimensions(destination_width, destination_height); |
132 uint8_t *y_plane_data, *u_plane_data, *v_plane_data; | 151 uint8_t* y_plane_data = 0; |
| 152 uint8_t* u_plane_data = 0; |
| 153 uint8_t* v_plane_data = 0; |
| 154 |
133 std::unique_ptr<Buffer> buffer( | 155 std::unique_ptr<Buffer> buffer( |
134 ReserveI420OutputBuffer(dimensions, media::PIXEL_STORAGE_CPU, | 156 (frame_format.pixel_format == media::PIXEL_FORMAT_Y16) |
135 &y_plane_data, &u_plane_data, &v_plane_data)); | 157 ? ReserveOutputBuffer(dimensions, frame_format.pixel_format, |
| 158 media::PIXEL_STORAGE_CPU) |
| 159 : ReserveI420OutputBuffer(dimensions, media::PIXEL_STORAGE_CPU, |
| 160 &y_plane_data, &u_plane_data, |
| 161 &v_plane_data)); |
136 #if DCHECK_IS_ON() | 162 #if DCHECK_IS_ON() |
137 dropped_frame_counter_ = buffer.get() ? 0 : dropped_frame_counter_ + 1; | 163 dropped_frame_counter_ = buffer.get() ? 0 : dropped_frame_counter_ + 1; |
138 if (dropped_frame_counter_ >= kMaxDroppedFrames) | 164 if (dropped_frame_counter_ >= kMaxDroppedFrames) |
139 OnError(FROM_HERE, "Too many frames dropped"); | 165 OnError(FROM_HERE, "Too many frames dropped"); |
140 #endif | 166 #endif |
141 // Failed to reserve I420 output buffer, so drop the frame. | 167 // Failed to reserve output buffer, so drop the frame. |
142 if (!buffer.get()) | 168 if (!buffer.get()) |
143 return; | 169 return; |
144 | 170 |
| 171 if (frame_format.pixel_format == media::PIXEL_FORMAT_Y16) { |
| 172 memcpy(buffer->data(), data, length); |
| 173 const VideoCaptureFormat output_format = |
| 174 VideoCaptureFormat(dimensions, frame_format.frame_rate, |
| 175 frame_format.pixel_format, media::PIXEL_STORAGE_CPU); |
| 176 OnIncomingCapturedBuffer(std::move(buffer), output_format, reference_time, |
| 177 timestamp); |
| 178 return; |
| 179 } |
| 180 |
145 const int yplane_stride = dimensions.width(); | 181 const int yplane_stride = dimensions.width(); |
146 const int uv_plane_stride = yplane_stride / 2; | 182 const int uv_plane_stride = yplane_stride / 2; |
147 int crop_x = 0; | 183 int crop_x = 0; |
148 int crop_y = 0; | 184 int crop_y = 0; |
149 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; | 185 libyuv::FourCC origin_colorspace = libyuv::FOURCC_ANY; |
150 | 186 |
151 bool flip = false; | 187 bool flip = false; |
152 switch (frame_format.pixel_format) { | 188 switch (frame_format.pixel_format) { |
153 case media::PIXEL_FORMAT_UNKNOWN: // Color format not set. | 189 case media::PIXEL_FORMAT_UNKNOWN: // Color format not set. |
154 break; | 190 break; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 timestamp); | 286 timestamp); |
251 } | 287 } |
252 | 288 |
253 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> | 289 std::unique_ptr<media::VideoCaptureDevice::Client::Buffer> |
254 VideoCaptureDeviceClient::ReserveOutputBuffer( | 290 VideoCaptureDeviceClient::ReserveOutputBuffer( |
255 const gfx::Size& frame_size, | 291 const gfx::Size& frame_size, |
256 media::VideoPixelFormat pixel_format, | 292 media::VideoPixelFormat pixel_format, |
257 media::VideoPixelStorage pixel_storage) { | 293 media::VideoPixelStorage pixel_storage) { |
258 DCHECK_GT(frame_size.width(), 0); | 294 DCHECK_GT(frame_size.width(), 0); |
259 DCHECK_GT(frame_size.height(), 0); | 295 DCHECK_GT(frame_size.height(), 0); |
260 // Currently, only I420 pixel format is supported. | 296 DCHECK(isFormatSupported(pixel_format)); |
261 DCHECK_EQ(media::PIXEL_FORMAT_I420, pixel_format); | |
262 | 297 |
263 // TODO(mcasas): For PIXEL_STORAGE_GPUMEMORYBUFFER, find a way to indicate if | 298 // TODO(mcasas): For PIXEL_STORAGE_GPUMEMORYBUFFER, find a way to indicate if |
264 // it's a ShMem GMB or a DmaBuf GMB. | 299 // it's a ShMem GMB or a DmaBuf GMB. |
265 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; | 300 int buffer_id_to_drop = VideoCaptureBufferPool::kInvalidId; |
266 const int buffer_id = buffer_pool_->ReserveForProducer( | 301 const int buffer_id = buffer_pool_->ReserveForProducer( |
267 frame_size, pixel_format, pixel_storage, &buffer_id_to_drop); | 302 frame_size, pixel_format, pixel_storage, &buffer_id_to_drop); |
268 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) | 303 if (buffer_id_to_drop != VideoCaptureBufferPool::kInvalidId) |
269 receiver_->OnBufferDestroyed(buffer_id_to_drop); | 304 receiver_->OnBufferDestroyed(buffer_id_to_drop); |
270 if (buffer_id == VideoCaptureBufferPool::kInvalidId) | 305 if (buffer_id == VideoCaptureBufferPool::kInvalidId) |
271 return nullptr; | 306 return nullptr; |
272 return base::WrapUnique<Buffer>( | 307 return base::WrapUnique<Buffer>( |
273 new AutoReleaseBuffer(buffer_pool_, buffer_id)); | 308 new AutoReleaseBuffer(buffer_pool_, buffer_id)); |
274 } | 309 } |
275 | 310 |
276 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( | 311 void VideoCaptureDeviceClient::OnIncomingCapturedBuffer( |
277 std::unique_ptr<Buffer> buffer, | 312 std::unique_ptr<Buffer> buffer, |
278 const VideoCaptureFormat& frame_format, | 313 const VideoCaptureFormat& frame_format, |
279 base::TimeTicks reference_time, | 314 base::TimeTicks reference_time, |
280 base::TimeDelta timestamp) { | 315 base::TimeDelta timestamp) { |
281 // Currently, only I420 pixel format is supported. | 316 DCHECK(isFormatSupported(frame_format.pixel_format)); |
282 DCHECK_EQ(media::PIXEL_FORMAT_I420, frame_format.pixel_format); | |
283 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage); | 317 DCHECK_EQ(media::PIXEL_STORAGE_CPU, frame_format.pixel_storage); |
284 | 318 |
285 scoped_refptr<VideoFrame> frame; | 319 scoped_refptr<VideoFrame> frame; |
286 if (buffer->IsBackedByVideoFrame()) { | 320 if (buffer->IsBackedByVideoFrame()) { |
287 frame = buffer->GetVideoFrame(); | 321 frame = buffer->GetVideoFrame(); |
288 frame->set_timestamp(timestamp); | 322 frame->set_timestamp(timestamp); |
289 } else { | 323 } else { |
290 frame = VideoFrame::WrapExternalSharedMemory( | 324 frame = VideoFrame::WrapExternalSharedMemory( |
291 media::PIXEL_FORMAT_I420, frame_format.frame_size, | 325 frame_format.pixel_format, frame_format.frame_size, |
292 gfx::Rect(frame_format.frame_size), frame_format.frame_size, | 326 gfx::Rect(frame_format.frame_size), frame_format.frame_size, |
293 reinterpret_cast<uint8_t*>(buffer->data()), | 327 reinterpret_cast<uint8_t*>(buffer->data()), |
294 VideoFrame::AllocationSize(media::PIXEL_FORMAT_I420, | 328 VideoFrame::AllocationSize(frame_format.pixel_format, |
295 frame_format.frame_size), | 329 frame_format.frame_size), |
296 base::SharedMemory::NULLHandle(), 0u, timestamp); | 330 base::SharedMemory::NULLHandle(), 0u, timestamp); |
297 } | 331 } |
298 if (!frame) | 332 if (!frame) |
299 return; | 333 return; |
300 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, | 334 frame->metadata()->SetDouble(media::VideoFrameMetadata::FRAME_RATE, |
301 frame_format.frame_rate); | 335 frame_format.frame_rate); |
302 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, | 336 frame->metadata()->SetTimeTicks(media::VideoFrameMetadata::REFERENCE_TIME, |
303 reference_time); | 337 reference_time); |
304 OnIncomingCapturedVideoFrame(std::move(buffer), frame); | 338 OnIncomingCapturedVideoFrame(std::move(buffer), frame); |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
366 *u_plane_data = | 400 *u_plane_data = |
367 *y_plane_data + | 401 *y_plane_data + |
368 VideoFrame::PlaneSize(format, VideoFrame::kYPlane, dimensions).GetArea(); | 402 VideoFrame::PlaneSize(format, VideoFrame::kYPlane, dimensions).GetArea(); |
369 *v_plane_data = | 403 *v_plane_data = |
370 *u_plane_data + | 404 *u_plane_data + |
371 VideoFrame::PlaneSize(format, VideoFrame::kUPlane, dimensions).GetArea(); | 405 VideoFrame::PlaneSize(format, VideoFrame::kUPlane, dimensions).GetArea(); |
372 return buffer; | 406 return buffer; |
373 } | 407 } |
374 | 408 |
375 } // namespace media | 409 } // namespace media |
OLD | NEW |