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

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: Created 5 years, 3 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;
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 DCHECK_LE(bytes_per_row, std::abs(dest_stride_y));
226 DCHECK_LE(bytes_per_row, std::abs(dest_stride_uv));
227 DCHECK_EQ(0, first_row % 2);
228 libyuv::I420ToNV12(
229 source_frame->data(VideoFrame::kYPlane) +
230 first_row * source_frame->stride(VideoFrame::kYPlane),
231 source_frame->stride(VideoFrame::kYPlane),
232 source_frame->data(VideoFrame::kUPlane) +
233 first_row / 2 * source_frame->stride(VideoFrame::kUPlane),
234 source_frame->stride(VideoFrame::kUPlane),
235 source_frame->data(VideoFrame::kVPlane) +
236 first_row / 2 * source_frame->stride(VideoFrame::kVPlane),
237 source_frame->stride(VideoFrame::kVPlane),
238 dest_y + first_row * dest_stride_y, dest_stride_y,
239 dest_uv + first_row / 2 * dest_stride_uv, dest_stride_uv,
240 bytes_per_row, rows);
241 done.Run();
242 done.Run();
243 }
244
206 void CopyRowsToUYVYBuffer(int first_row, 245 void CopyRowsToUYVYBuffer(int first_row,
207 int rows, 246 int rows,
208 int width, 247 int width,
209 const scoped_refptr<VideoFrame>& source_frame, 248 const scoped_refptr<VideoFrame>& source_frame,
210 uint8* output, 249 uint8* output,
211 int dest_stride, 250 int dest_stride,
212 const base::Closure& done) { 251 const base::Closure& done) {
213 TRACE_EVENT2("media", "CopyRowsToUYVYBuffer", "bytes_per_row", width * 2, 252 TRACE_EVENT2("media", "CopyRowsToUYVYBuffer", "bytes_per_row", width * 2,
214 "rows", rows); 253 "rows", rows);
215 DCHECK_NE(dest_stride, 0); 254 DCHECK_NE(dest_stride, 0);
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
276 315
277 worker_task_runner_->PostTask( 316 worker_task_runner_->PostTask(
278 FROM_HERE, base::Bind(&PoolImpl::CopyVideoFrameToGpuMemoryBuffers, this, 317 FROM_HERE, base::Bind(&PoolImpl::CopyVideoFrameToGpuMemoryBuffers, this,
279 video_frame, frame_resources, frame_ready_cb)); 318 video_frame, frame_resources, frame_ready_cb));
280 } 319 }
281 320
282 void GpuMemoryBufferVideoFramePool::PoolImpl::OnCopiesDone( 321 void GpuMemoryBufferVideoFramePool::PoolImpl::OnCopiesDone(
283 const scoped_refptr<VideoFrame>& video_frame, 322 const scoped_refptr<VideoFrame>& video_frame,
284 FrameResources* frame_resources, 323 FrameResources* frame_resources,
285 const FrameReadyCB& frame_ready_cb) { 324 const FrameReadyCB& frame_ready_cb) {
286 const size_t planes = VideoFrame::NumPlanes(output_format_); 325 for (const auto& plane_resource : frame_resources->plane_resources) {
287 for (size_t i = 0; i < planes; ++i) { 326 if (plane_resource.gpu_memory_buffer)
288 frame_resources->plane_resources[i].gpu_memory_buffer->Unmap(); 327 plane_resource.gpu_memory_buffer->Unmap();
289 } 328 }
290 329
291 media_task_runner_->PostTask( 330 media_task_runner_->PostTask(
292 FROM_HERE, 331 FROM_HERE,
293 base::Bind(&PoolImpl::BindAndCreateMailboxesHardwareFrameResources, this, 332 base::Bind(&PoolImpl::BindAndCreateMailboxesHardwareFrameResources, this,
294 video_frame, frame_resources, frame_ready_cb)); 333 video_frame, frame_resources, frame_ready_cb));
295 } 334 }
296 335
297 // Copies |video_frame| into |frame_resources| asynchronously, posting n tasks 336 // Copies |video_frame| into |frame_resources| asynchronously, posting n tasks
298 // that will be synchronized by a barrier. 337 // that will be synchronized by a barrier.
(...skipping 13 matching lines...) Expand all
312 int rows_per_copy = 351 int rows_per_copy =
313 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1); 352 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1);
314 copies += rows / rows_per_copy; 353 copies += rows / rows_per_copy;
315 if (rows % rows_per_copy) 354 if (rows % rows_per_copy)
316 ++copies; 355 ++copies;
317 } 356 }
318 base::Closure copies_done = 357 base::Closure copies_done =
319 base::Bind(&PoolImpl::OnCopiesDone, this, video_frame, frame_resources, 358 base::Bind(&PoolImpl::OnCopiesDone, this, video_frame, frame_resources,
320 frame_ready_cb); 359 frame_ready_cb);
321 base::Closure barrier = base::BarrierClosure(copies, copies_done); 360 base::Closure barrier = base::BarrierClosure(copies, copies_done);
361
322 // Post all the async tasks. 362 // Post all the async tasks.
323 for (size_t i = 0; i < dest_planes; ++i) { 363 for (size_t i = 0; i < dest_planes; ++i) {
364 gfx::GpuMemoryBuffer* buffer =
365 frame_resources->plane_resources[i].gpu_memory_buffer.get();
366 DCHECK(buffer);
367 const size_t buffer_planes =
368 gfx::NumberOfPlanesForBufferFormat(buffer->GetFormat());
369 DCHECK_LE(buffer_planes, VideoFrame::kMaxPlanes);
370 uint8* dest_buffers[VideoFrame::kMaxPlanes];
371 int dest_strides[VideoFrame::kMaxPlanes];
372 bool rv = buffer->Map(reinterpret_cast<void**>(dest_buffers));
373 DCHECK(rv);
374 buffer->GetStride(dest_strides);
375
324 int rows = VideoFrame::Rows(i, output_format_, size.height()); 376 int rows = VideoFrame::Rows(i, output_format_, size.height());
325 int bytes_per_row = VideoFrame::RowBytes(i, output_format_, size.width()); 377 int bytes_per_row = VideoFrame::RowBytes(i, output_format_, size.width());
326 int rows_per_copy = 378 int rows_per_copy =
327 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1); 379 std::max<size_t>((kBytesPerCopyTarget / bytes_per_row) & ~1, 1);
328 380
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) { 381 for (int row = 0; row < rows; row += rows_per_copy) {
382 const int rows_to_copy = std::min(rows_per_copy, rows - row);
341 switch (output_format_) { 383 switch (output_format_) {
342 case PIXEL_FORMAT_I420: 384 case PIXEL_FORMAT_I420:
385 DCHECK_EQ(1u, buffer_planes);
343 worker_task_runner_->PostTask( 386 worker_task_runner_->PostTask(
344 FROM_HERE, 387 FROM_HERE,
345 base::Bind(&CopyRowsToI420Buffer, row, 388 base::Bind(&CopyRowsToI420Buffer, row, rows_to_copy,
346 std::min(rows_per_copy, rows - row), bytes_per_row, 389 bytes_per_row, video_frame->data(i),
347 video_frame->data(i), video_frame->stride(i), 390 video_frame->stride(i), dest_buffers[0],
348 mapped_buffer, dest_stride, barrier)); 391 dest_strides[0], barrier));
392 break;
393 case PIXEL_FORMAT_NV12:
394 DCHECK_EQ(2u, buffer_planes);
395 worker_task_runner_->PostTask(
396 FROM_HERE,
397 base::Bind(&CopyRowsToNV12Buffer, row, rows_to_copy,
398 bytes_per_row, video_frame, dest_buffers[0],
399 dest_strides[0], dest_buffers[1], dest_strides[1],
400 barrier));
401 ++i; // Consume an extra plane since we copied 2 planes into 1 GMB.
349 break; 402 break;
350 case PIXEL_FORMAT_UYVY: 403 case PIXEL_FORMAT_UYVY:
404 DCHECK_EQ(1u, buffer_planes);
351 worker_task_runner_->PostTask( 405 worker_task_runner_->PostTask(
352 FROM_HERE, 406 FROM_HERE,
353 base::Bind(&CopyRowsToUYVYBuffer, row, 407 base::Bind(&CopyRowsToUYVYBuffer, row, rows_to_copy, size.width(),
354 std::min(rows_per_copy, rows - row), size.width(), 408 video_frame, dest_buffers[0], dest_strides[0],
355 video_frame, mapped_buffer, dest_stride, barrier)); 409 barrier));
356 break; 410 break;
357 default: 411 default:
358 NOTREACHED(); 412 NOTREACHED();
359 } 413 }
360 } 414 }
361 } 415 }
362 } 416 }
363 417
364 void GpuMemoryBufferVideoFramePool::PoolImpl:: 418 void GpuMemoryBufferVideoFramePool::PoolImpl::
365 BindAndCreateMailboxesHardwareFrameResources( 419 BindAndCreateMailboxesHardwareFrameResources(
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 frame = VideoFrame::WrapYUV420NativeTextures( 465 frame = VideoFrame::WrapYUV420NativeTextures(
412 mailbox_holders[VideoFrame::kYPlane], 466 mailbox_holders[VideoFrame::kYPlane],
413 mailbox_holders[VideoFrame::kUPlane], 467 mailbox_holders[VideoFrame::kUPlane],
414 mailbox_holders[VideoFrame::kVPlane], 468 mailbox_holders[VideoFrame::kVPlane],
415 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources), 469 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources),
416 size, video_frame->visible_rect(), video_frame->natural_size(), 470 size, video_frame->visible_rect(), video_frame->natural_size(),
417 video_frame->timestamp()); 471 video_frame->timestamp());
418 if (video_frame->metadata()->IsTrue(VideoFrameMetadata::ALLOW_OVERLAY)) 472 if (video_frame->metadata()->IsTrue(VideoFrameMetadata::ALLOW_OVERLAY))
419 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true); 473 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true);
420 break; 474 break;
475 case PIXEL_FORMAT_NV12:
421 case PIXEL_FORMAT_UYVY: 476 case PIXEL_FORMAT_UYVY:
422 frame = VideoFrame::WrapNativeTexture( 477 frame = VideoFrame::WrapNativeTexture(
423 PIXEL_FORMAT_UYVY, mailbox_holders[VideoFrame::kYPlane], 478 output_format_, mailbox_holders[VideoFrame::kYPlane],
424 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources), 479 base::Bind(&PoolImpl::MailboxHoldersReleased, this, frame_resources),
425 size, video_frame->visible_rect(), video_frame->natural_size(), 480 size, video_frame->visible_rect(), video_frame->natural_size(),
426 video_frame->timestamp()); 481 video_frame->timestamp());
427 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true); 482 frame->metadata()->SetBoolean(VideoFrameMetadata::ALLOW_OVERLAY, true);
428 break; 483 break;
429 default: 484 default:
430 NOTREACHED(); 485 NOTREACHED();
431 } 486 }
432 frame_ready_cb.Run(frame); 487 frame_ready_cb.Run(frame);
433 } 488 }
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
475 gles2->ActiveTexture(GL_TEXTURE0); 530 gles2->ActiveTexture(GL_TEXTURE0);
476 size_t planes = VideoFrame::NumPlanes(format); 531 size_t planes = VideoFrame::NumPlanes(format);
477 FrameResources* frame_resources = new FrameResources(size); 532 FrameResources* frame_resources = new FrameResources(size);
478 resources_pool_.push_back(frame_resources); 533 resources_pool_.push_back(frame_resources);
479 for (size_t i = 0; i < planes; ++i) { 534 for (size_t i = 0; i < planes; ++i) {
480 PlaneResource& plane_resource = frame_resources->plane_resources[i]; 535 PlaneResource& plane_resource = frame_resources->plane_resources[i];
481 const size_t width = VideoFrame::Columns(i, format, size.width()); 536 const size_t width = VideoFrame::Columns(i, format, size.width());
482 const size_t height = VideoFrame::Rows(i, format, size.height()); 537 const size_t height = VideoFrame::Rows(i, format, size.height());
483 const gfx::Size plane_size(width, height); 538 const gfx::Size plane_size(width, height);
484 539
540 const gfx::BufferFormat buffer_format = GpuMemoryBufferFormat(format, i);
485 plane_resource.gpu_memory_buffer = gpu_factories_->AllocateGpuMemoryBuffer( 541 plane_resource.gpu_memory_buffer = gpu_factories_->AllocateGpuMemoryBuffer(
486 plane_size, GpuMemoryBufferFormat(format, i), gfx::BufferUsage::MAP); 542 plane_size, buffer_format, gfx::BufferUsage::MAP);
487 543
488 gles2->GenTextures(1, &plane_resource.texture_id); 544 gles2->GenTextures(1, &plane_resource.texture_id);
489 gles2->BindTexture(texture_target_, plane_resource.texture_id); 545 gles2->BindTexture(texture_target_, plane_resource.texture_id);
490 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 546 gles2->TexParameteri(texture_target_, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
491 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR); 547 gles2->TexParameteri(texture_target_, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
492 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 548 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); 549 gles2->TexParameteri(texture_target_, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
494 gles2->GenMailboxCHROMIUM(plane_resource.mailbox.name); 550 gles2->GenMailboxCHROMIUM(plane_resource.mailbox.name);
495 gles2->ProduceTextureCHROMIUM(texture_target_, plane_resource.mailbox.name); 551 gles2->ProduceTextureCHROMIUM(texture_target_, plane_resource.mailbox.name);
552
553 size_t buffer_planes = gfx::NumberOfPlanesForBufferFormat(buffer_format);
554 if (buffer_planes > 1) {
555 // Got a multi-planar buffer, assume all the planes fit in it.
556 DCHECK_EQ(planes, buffer_planes);
557 break;
558 }
496 } 559 }
497 return frame_resources; 560 return frame_resources;
498 } 561 }
499 562
500 // static 563 // static
501 void GpuMemoryBufferVideoFramePool::PoolImpl::DeleteFrameResources( 564 void GpuMemoryBufferVideoFramePool::PoolImpl::DeleteFrameResources(
502 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories, 565 const scoped_refptr<GpuVideoAcceleratorFactories>& gpu_factories,
503 FrameResources* frame_resources) { 566 FrameResources* frame_resources) {
504 // TODO(dcastagna): As soon as the context lost is dealt with in media, 567 // 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 568 // 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 } 614 }
552 615
553 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame( 616 void GpuMemoryBufferVideoFramePool::MaybeCreateHardwareFrame(
554 const scoped_refptr<VideoFrame>& video_frame, 617 const scoped_refptr<VideoFrame>& video_frame,
555 const FrameReadyCB& frame_ready_cb) { 618 const FrameReadyCB& frame_ready_cb) {
556 DCHECK(video_frame); 619 DCHECK(video_frame);
557 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb); 620 pool_impl_->CreateHardwareFrame(video_frame, frame_ready_cb);
558 } 621 }
559 622
560 } // namespace media 623 } // namespace media
OLDNEW
« no previous file with comments | « media/renderers/mock_gpu_video_accelerator_factories.cc ('k') | media/video/gpu_memory_buffer_video_frame_pool_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698