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

Side by Side Diff: media/video/gpu_memory_buffer_video_frame_pool.cc

Issue 1316493004: Add support for converting I420 software frames into NV12 hardware frames (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@snapshot
Patch Set: Fix win_x64 build Created 5 years, 4 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
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/video/gpu_memory_buffer_video_frame_pool.h" 5 #include "media/video/gpu_memory_buffer_video_frame_pool.h"
6 6
7 #include <GLES2/gl2.h> 7 #include <GLES2/gl2.h>
8 #include <GLES2/gl2ext.h> 8 #include <GLES2/gl2ext.h>
9 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 // output size is |kBytesPerCopyTarget| bytes and run in parallel. 153 // output size is |kBytesPerCopyTarget| bytes and run in parallel.
154 const size_t kBytesPerCopyTarget = 1024 * 1024; // 1MB 154 const size_t kBytesPerCopyTarget = 1024 * 1024; // 1MB
155 155
156 // Return the GpuMemoryBuffer format to use for a specific VideoPixelFormat 156 // Return the GpuMemoryBuffer format to use for a specific VideoPixelFormat
157 // and plane. 157 // and plane.
158 gfx::BufferFormat GpuMemoryBufferFormat(VideoPixelFormat format, size_t plane) { 158 gfx::BufferFormat GpuMemoryBufferFormat(VideoPixelFormat format, size_t plane) {
159 switch (format) { 159 switch (format) {
160 case PIXEL_FORMAT_I420: 160 case PIXEL_FORMAT_I420:
161 DCHECK_LE(plane, 2u); 161 DCHECK_LE(plane, 2u);
162 return gfx::BufferFormat::R_8; 162 return gfx::BufferFormat::R_8;
163 case PIXEL_FORMAT_NV12:
164 DCHECK_LE(plane, 1u);
165 return gfx::BufferFormat::YUV_420_BIPLANAR;
163 case PIXEL_FORMAT_UYVY: 166 case PIXEL_FORMAT_UYVY:
164 DCHECK_EQ(0u, plane); 167 DCHECK_EQ(0u, plane);
165 return gfx::BufferFormat::UYVY_422; 168 return gfx::BufferFormat::UYVY_422;
166 default: 169 default:
167 NOTREACHED(); 170 NOTREACHED();
168 return gfx::BufferFormat::BGRA_8888; 171 return gfx::BufferFormat::BGRA_8888;
169 } 172 }
170 } 173 }
171 174
172 unsigned ImageInternalFormat(VideoPixelFormat format, size_t plane) { 175 unsigned ImageInternalFormat(VideoPixelFormat format, size_t plane) {
173 switch (format) { 176 switch (format) {
174 case PIXEL_FORMAT_I420: 177 case PIXEL_FORMAT_I420:
175 DCHECK_LE(plane, 2u); 178 DCHECK_LE(plane, 2u);
176 return GL_R8_EXT; 179 return GL_R8_EXT;
180 case PIXEL_FORMAT_NV12:
181 DCHECK_LE(plane, 1u);
182 return GL_R8_EXT;
Daniele Castagna 2015/08/25 15:08:42 Why is the internal format R_8?
Andre 2015/08/25 18:19:57 I'm not sure actually, what do you think the forma
177 case PIXEL_FORMAT_UYVY: 183 case PIXEL_FORMAT_UYVY:
178 DCHECK_EQ(0u, plane); 184 DCHECK_EQ(0u, plane);
179 return GL_RGB; 185 return GL_RGB;
180 default: 186 default:
181 NOTREACHED(); 187 NOTREACHED();
182 return 0; 188 return 0;
183 } 189 }
184 } 190 }
185 191
186 void CopyRowsToI420Buffer(int first_row, 192 void CopyRowsToI420Buffer(int first_row,
187 int rows, 193 int rows,
188 int bytes_per_row, 194 int bytes_per_row,
189 const uint8* source, 195 const uint8* source,
190 int source_stride, 196 int source_stride,
191 uint8* output, 197 uint8* output,
192 int dest_stride, 198 int dest_stride,
193 const base::Closure& done) { 199 const base::Closure& done) {
194 TRACE_EVENT2("media", "CopyRowsToI420Buffer", "bytes_per_row", bytes_per_row, 200 TRACE_EVENT2("media", "CopyRowsToI420Buffer", "bytes_per_row", bytes_per_row,
195 "rows", rows); 201 "rows", rows);
196 DCHECK_NE(dest_stride, 0); 202 DCHECK_NE(dest_stride, 0);
197 DCHECK_LE(bytes_per_row, std::abs(dest_stride)); 203 DCHECK_LE(bytes_per_row, std::abs(dest_stride));
198 DCHECK_LE(bytes_per_row, source_stride); 204 DCHECK_LE(bytes_per_row, source_stride);
199 for (int row = first_row; row < first_row + rows; ++row) { 205 for (int row = first_row; row < first_row + rows; ++row) {
200 memcpy(output + dest_stride * row, source + source_stride * row, 206 memcpy(output + dest_stride * row, source + source_stride * row,
201 bytes_per_row); 207 bytes_per_row);
202 } 208 }
203 done.Run(); 209 done.Run();
204 } 210 }
205 211
212 void CopyRowsToNV12Buffer(int first_row,
213 int rows,
214 int bytes_per_row,
215 const scoped_refptr<VideoFrame>& source_frame,
216 uint8* dest_y,
217 int dest_stride_y,
218 uint8* dest_uv,
219 int dest_stride_uv,
220 const base::Closure& done) {
221 TRACE_EVENT2("media", "CopyRowsToNV12Buffer", "bytes_per_row", bytes_per_row,
222 "rows", rows);
223 DCHECK_NE(dest_stride_y, 0);
224 DCHECK_NE(dest_stride_uv, 0);
225 libyuv::I420ToNV12(
226 source_frame->data(VideoFrame::kYPlane) +
227 first_row * source_frame->stride(VideoFrame::kYPlane),
228 source_frame->stride(VideoFrame::kYPlane),
229 source_frame->data(VideoFrame::kUPlane) +
230 first_row * source_frame->stride(VideoFrame::kUPlane),
Daniele Castagna 2015/08/25 15:08:42 Shouldn't this start at first_row/2?
Andre 2015/08/25 21:04:33 You're right, fixed. There's also still an issue w
231 source_frame->stride(VideoFrame::kUPlane),
232 source_frame->data(VideoFrame::kVPlane) +
233 first_row * source_frame->stride(VideoFrame::kVPlane),
234 source_frame->stride(VideoFrame::kVPlane),
235 dest_y + first_row * dest_stride_y, dest_stride_y,
236 dest_uv + first_row * dest_stride_uv, dest_stride_uv,
Daniele Castagna 2015/08/25 15:08:42 Same here.
237 bytes_per_row, rows);
238 done.Run();
239 }
240
206 void CopyRowsToUYVYBuffer(int first_row, 241 void CopyRowsToUYVYBuffer(int first_row,
207 int rows, 242 int rows,
208 int width, 243 int width,
209 const scoped_refptr<VideoFrame>& source_frame, 244 const scoped_refptr<VideoFrame>& source_frame,
210 uint8* output, 245 uint8* output,
211 int dest_stride, 246 int dest_stride,
212 const base::Closure& done) { 247 const base::Closure& done) {
213 TRACE_EVENT2("media", "CopyRowsToUYVYBuffer", "bytes_per_row", width * 2, 248 TRACE_EVENT2("media", "CopyRowsToUYVYBuffer", "bytes_per_row", width * 2,
214 "rows", rows); 249 "rows", rows);
215 DCHECK_NE(dest_stride, 0); 250 DCHECK_NE(dest_stride, 0);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 311
277 worker_task_runner_->PostTask( 312 worker_task_runner_->PostTask(
278 FROM_HERE, base::Bind(&PoolImpl::CopyVideoFrameToGpuMemoryBuffers, this, 313 FROM_HERE, base::Bind(&PoolImpl::CopyVideoFrameToGpuMemoryBuffers, this,
279 video_frame, frame_resources, frame_ready_cb)); 314 video_frame, frame_resources, frame_ready_cb));
280 } 315 }
281 316
282 void GpuMemoryBufferVideoFramePool::PoolImpl::OnCopiesDone( 317 void GpuMemoryBufferVideoFramePool::PoolImpl::OnCopiesDone(
283 const scoped_refptr<VideoFrame>& video_frame, 318 const scoped_refptr<VideoFrame>& video_frame,
284 FrameResources* frame_resources, 319 FrameResources* frame_resources,
285 const FrameReadyCB& frame_ready_cb) { 320 const FrameReadyCB& frame_ready_cb) {
286 const size_t planes = VideoFrame::NumPlanes(output_format_); 321 for (const auto& plane_resource : frame_resources->plane_resources) {
287 for (size_t i = 0; i < planes; ++i) { 322 if (plane_resource.gpu_memory_buffer)
288 frame_resources->plane_resources[i].gpu_memory_buffer->Unmap(); 323 plane_resource.gpu_memory_buffer->Unmap();
289 } 324 }
290 325
291 media_task_runner_->PostTask( 326 media_task_runner_->PostTask(
292 FROM_HERE, 327 FROM_HERE,
293 base::Bind(&PoolImpl::BindAndCreateMailboxesHardwareFrameResources, this, 328 base::Bind(&PoolImpl::BindAndCreateMailboxesHardwareFrameResources, this,
294 video_frame, frame_resources, frame_ready_cb)); 329 video_frame, frame_resources, frame_ready_cb));
295 } 330 }
296 331
297 // Copies |video_frame| into |frame_resources| asynchronously, posting n tasks 332 // Copies |video_frame| into |frame_resources| asynchronously, posting n tasks
298 // that will be synchronized by a barrier. 333 // that will be synchronized by a barrier.
(...skipping 13 matching lines...) Expand all
312 int rows_per_copy = 347 int rows_per_copy =
313 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1); 348 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1);
314 copies += rows / rows_per_copy; 349 copies += rows / rows_per_copy;
315 if (rows % rows_per_copy) 350 if (rows % rows_per_copy)
316 ++copies; 351 ++copies;
317 } 352 }
318 base::Closure copies_done = 353 base::Closure copies_done =
319 base::Bind(&PoolImpl::OnCopiesDone, this, video_frame, frame_resources, 354 base::Bind(&PoolImpl::OnCopiesDone, this, video_frame, frame_resources,
320 frame_ready_cb); 355 frame_ready_cb);
321 base::Closure barrier = base::BarrierClosure(copies, copies_done); 356 base::Closure barrier = base::BarrierClosure(copies, copies_done);
357
322 // Post all the async tasks. 358 // Post all the async tasks.
323 for (size_t i = 0; i < dest_planes; ++i) { 359 for (size_t i = 0; i < dest_planes; ++i) {
360 gfx::GpuMemoryBuffer* buffer =
361 frame_resources->plane_resources[i].gpu_memory_buffer.get();
362 DCHECK(buffer);
363 const size_t buffer_planes =
364 gfx::NumberOfPlanesForBufferFormat(buffer->GetFormat());
365 scoped_ptr<uint8*[]> dest_buffers(new uint8*[buffer_planes]);
Daniele Castagna 2015/08/25 15:08:42 Can we avoid the allocation with an array of size
Andre 2015/08/25 18:19:57 Done. I suppose it's ok to use VideoFrame::kMaxPla
366 scoped_ptr<int[]> dest_strides(new int[buffer_planes]);
Daniele Castagna 2015/08/25 15:08:43 Same here.
Andre 2015/08/25 18:19:57 Done.
367 bool rv = buffer->Map(reinterpret_cast<void**>(dest_buffers.get()));
368 DCHECK(rv);
369 buffer->GetStride(dest_strides.get());
370
324 int rows = VideoFrame::Rows(i, output_format_, size.height()); 371 int rows = VideoFrame::Rows(i, output_format_, size.height());
325 int bytes_per_row = VideoFrame::RowBytes(i, output_format_, size.width()); 372 int bytes_per_row = VideoFrame::RowBytes(i, output_format_, size.width());
326 int rows_per_copy = 373 int rows_per_copy =
327 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1); 374 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1);
328 375
329 void* data = nullptr;
330 DCHECK_EQ(1u, gfx::NumberOfPlanesForBufferFormat(
331 GpuMemoryBufferFormat(output_format_, i)));
332 bool rv = frame_resources->plane_resources[i].gpu_memory_buffer->Map(&data);
333 DCHECK(rv);
334 uint8* mapped_buffer = static_cast<uint8*>(data);
335
336 int dest_stride = 0;
337 frame_resources->plane_resources[i].gpu_memory_buffer->GetStride(
338 &dest_stride);
339
340 for (int row = 0; row < rows; row += rows_per_copy) { 376 for (int row = 0; row < rows; row += rows_per_copy) {
341 switch (output_format_) { 377 switch (output_format_) {
342 case PIXEL_FORMAT_I420: 378 case PIXEL_FORMAT_I420:
379 DCHECK_EQ(1u, buffer_planes);
343 worker_task_runner_->PostTask( 380 worker_task_runner_->PostTask(
344 FROM_HERE, 381 FROM_HERE,
345 base::Bind(&CopyRowsToI420Buffer, row, 382 base::Bind(&CopyRowsToI420Buffer, row,
346 std::min(rows_per_copy, rows - row), bytes_per_row, 383 std::min(rows_per_copy, rows - row), bytes_per_row,
347 video_frame->data(i), video_frame->stride(i), 384 video_frame->data(i), video_frame->stride(i),
348 mapped_buffer, dest_stride, barrier)); 385 dest_buffers[0], dest_strides[0], barrier));
386 break;
387 case PIXEL_FORMAT_NV12:
388 DCHECK_EQ(2u, buffer_planes);
389 worker_task_runner_->PostTask(
390 FROM_HERE,
391 base::Bind(&CopyRowsToNV12Buffer, row,
392 std::min(rows_per_copy, rows - row), bytes_per_row,
Daniele Castagna 2015/08/25 15:08:43 nit: we could move std::min(rows_per_copy, rows -
Andre 2015/08/25 18:19:57 Done.
393 video_frame, dest_buffers[0], dest_strides[0],
394 dest_buffers[1], dest_strides[1], barrier));
395 ++i; // Consume an extra plane.
Daniele Castagna 2015/08/25 15:08:42 nit: I'd elaborate on the comment a little to expl
Andre 2015/08/25 18:19:57 Done.
349 break; 396 break;
350 case PIXEL_FORMAT_UYVY: 397 case PIXEL_FORMAT_UYVY:
398 DCHECK_EQ(1u, buffer_planes);
351 worker_task_runner_->PostTask( 399 worker_task_runner_->PostTask(
352 FROM_HERE, 400 FROM_HERE,
353 base::Bind(&CopyRowsToUYVYBuffer, row, 401 base::Bind(&CopyRowsToUYVYBuffer, row,
354 std::min(rows_per_copy, rows - row), size.width(), 402 std::min(rows_per_copy, rows - row), size.width(),
355 video_frame, mapped_buffer, dest_stride, barrier)); 403 video_frame, dest_buffers[0], dest_strides[0],
404 barrier));
356 break; 405 break;
357 default: 406 default:
358 NOTREACHED(); 407 NOTREACHED();
359 } 408 }
360 } 409 }
361 } 410 }
362 } 411 }
363 412
364 void GpuMemoryBufferVideoFramePool::PoolImpl:: 413 void GpuMemoryBufferVideoFramePool::PoolImpl::
365 BindAndCreateMailboxesHardwareFrameResources( 414 BindAndCreateMailboxesHardwareFrameResources(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 frame = VideoFrame::WrapYUV420NativeTextures( 460 frame = VideoFrame::WrapYUV420NativeTextures(
412 mailbox_holders[VideoFrame::kYPlane], 461 mailbox_holders[VideoFrame::kYPlane],
413 mailbox_holders[VideoFrame::kUPlane], 462 mailbox_holders[VideoFrame::kUPlane],
414 mailbox_holders[VideoFrame::kVPlane], 463 mailbox_holders[VideoFrame::kVPlane],
415 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources), 464 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources),
416 size, video_frame->visible_rect(), video_frame->natural_size(), 465 size, video_frame->visible_rect(), video_frame->natural_size(),
417 video_frame->timestamp()); 466 video_frame->timestamp());
418 if (video_frame->metadata()->IsTrue(VideoFrameMetadata::ALLOW_OVERLAY)) 467 if (video_frame->metadata()->IsTrue(VideoFrameMetadata::ALLOW_OVERLAY))
419 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true); 468 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true);
420 break; 469 break;
470 case PIXEL_FORMAT_NV12:
421 case PIXEL_FORMAT_UYVY: 471 case PIXEL_FORMAT_UYVY:
422 frame = VideoFrame::WrapNativeTexture( 472 frame = VideoFrame::WrapNativeTexture(
423 PIXEL_FORMAT_UYVY, mailbox_holders[VideoFrame::kYPlane], 473 output_format_, mailbox_holders[VideoFrame::kYPlane],
424 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources), 474 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources),
425 size, video_frame->visible_rect(), video_frame->natural_size(), 475 size, video_frame->visible_rect(), video_frame->natural_size(),
426 video_frame->timestamp()); 476 video_frame->timestamp());
427 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true); 477 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true);
428 break; 478 break;
429 default: 479 default:
430 NOTREACHED(); 480 NOTREACHED();
431 } 481 }
432 frame_ready_cb.Run(frame); 482 frame_ready_cb.Run(frame);
433 } 483 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 gles2->ActiveTexture(GL_TEXTURE0); 525 gles2->ActiveTexture(GL_TEXTURE0);
476 size_t planes = VideoFrame::NumPlanes(format); 526 size_t planes = VideoFrame::NumPlanes(format);
477 FrameResources* frame_resources = new FrameResources(size); 527 FrameResources* frame_resources = new FrameResources(size);
478 resources_pool_.push_back(frame_resources); 528 resources_pool_.push_back(frame_resources);
479 for (size_t i = 0; i < planes; ++i) { 529 for (size_t i = 0; i < planes; ++i) {
480 PlaneResource& plane_resource = frame_resources->plane_resources[i]; 530 PlaneResource& plane_resource = frame_resources->plane_resources[i];
481 const size_t width = VideoFrame::Columns(i, format, size.width()); 531 const size_t width = VideoFrame::Columns(i, format, size.width());
482 const size_t height = VideoFrame::Rows(i, format, size.height()); 532 const size_t height = VideoFrame::Rows(i, format, size.height());
483 const gfx::Size plane_size(width, height); 533 const gfx::Size plane_size(width, height);
484 534
535 const gfx::BufferFormat buffer_format = GpuMemoryBufferFormat(format, i);
485 plane_resource.gpu_memory_buffer = gpu_factories_->AllocateGpuMemoryBuffer( 536 plane_resource.gpu_memory_buffer = gpu_factories_->AllocateGpuMemoryBuffer(
486 plane_size, GpuMemoryBufferFormat(format, i), gfx::BufferUsage::MAP); 537 plane_size, buffer_format, gfx::BufferUsage::MAP);
487 538
488 gles2->GenTextures(1, &plane_resource.texture_id); 539 gles2->GenTextures(1, &plane_resource.texture_id);
489 gles2->BindTexture(texture_target_, plane_resource.texture_id); 540 gles2->BindTexture(texture_target_, plane_resource.texture_id);
490 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 541 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
491 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 542 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
492 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 543 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
493 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); 544 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
494 gles2->GenMailboxCHROMIUM(plane_resource.mailbox.name); 545 gles2->GenMailboxCHROMIUM(plane_resource.mailbox.name);
495 gles2->ProduceTextureCHROMIUM(texture_target_, plane_resource.mailbox.name); 546 gles2->ProduceTextureCHROMIUM(texture_target_, plane_resource.mailbox.name);
547
548 size_t buffer_planes = gfx::NumberOfPlanesForBufferFormat(buffer_format);
549 if (buffer_planes > 1) {
550 // Got a multi-planar buffer, assume all the planes fit in it.
551 DCHECK_EQ(planes, buffer_planes);
552 break;
553 }
496 } 554 }
497 return frame_resources; 555 return frame_resources;
498 } 556 }
499 557
500 // static 558 // static
501 void GpuMemoryBufferVideoFramePool::PoolImpl::DeleteFrameResources( 559 void GpuMemoryBufferVideoFramePool::PoolImpl::DeleteFrameResources(
502 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, 560 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories,
503 FrameResources* frame_resources) { 561 FrameResources* frame_resources) {
504 // TODO(dcastagna): As soon as the context lost is dealt with in media, 562 // TODO(dcastagna): As soon as the context lost is dealt with in media,
505 // make sure that we won't execute this callback (use a weak pointer to 563 // make sure that we won't execute this callback (use a weak pointer to
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
551 } 609 }
552 610
553 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( 611 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame(
554 const scoped_refptr<VideoFrame>& video_frame, 612 const scoped_refptr<VideoFrame>& video_frame,
555 const FrameReadyCB& frame_ready_cb) { 613 const FrameReadyCB& frame_ready_cb) {
556 DCHECK(video_frame); 614 DCHECK(video_frame);
557 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb); 615 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb);
558 } 616 }
559 617
560 } // namespace media 618 } // namespace media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698