| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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/base/mac/video_frame_mac.h" | 5 #include "media/base/mac/video_frame_mac.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <algorithm> | 10 #include <algorithm> |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 52 cv_format = kCVPixelFormatType_420YpCbCr8Planar; | 52 cv_format = kCVPixelFormatType_420YpCbCr8Planar; |
| 53 } else if (video_frame_format == PIXEL_FORMAT_NV12) { | 53 } else if (video_frame_format == PIXEL_FORMAT_NV12) { |
| 54 cv_format = CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; | 54 cv_format = CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; |
| 55 } else { | 55 } else { |
| 56 DLOG(ERROR) << " unsupported frame format: " << video_frame_format; | 56 DLOG(ERROR) << " unsupported frame format: " << video_frame_format; |
| 57 return pixel_buffer; | 57 return pixel_buffer; |
| 58 } | 58 } |
| 59 | 59 |
| 60 int num_planes = VideoFrame::NumPlanes(video_frame_format); | 60 int num_planes = VideoFrame::NumPlanes(video_frame_format); |
| 61 DCHECK_LE(num_planes, kMaxPlanes); | 61 DCHECK_LE(num_planes, kMaxPlanes); |
| 62 gfx::Size coded_size = frame.coded_size(); | 62 const gfx::Rect& visible_rect = frame.visible_rect(); |
| 63 | |
| 64 // TODO(jfroy): Support extended pixels (i.e. padding). | |
| 65 if (coded_size != frame.visible_rect().size()) { | |
| 66 DLOG(ERROR) << " frame with extended pixels not supported: " | |
| 67 << " coded_size: " << coded_size.ToString() | |
| 68 << ", visible_rect: " << frame.visible_rect().ToString(); | |
| 69 return pixel_buffer; | |
| 70 } | |
| 71 | 63 |
| 72 // Build arrays for each plane's data pointer, dimensions and byte alignment. | 64 // Build arrays for each plane's data pointer, dimensions and byte alignment. |
| 73 void* plane_ptrs[kMaxPlanes]; | 65 void* plane_ptrs[kMaxPlanes]; |
| 74 size_t plane_widths[kMaxPlanes]; | 66 size_t plane_widths[kMaxPlanes]; |
| 75 size_t plane_heights[kMaxPlanes]; | 67 size_t plane_heights[kMaxPlanes]; |
| 76 size_t plane_bytes_per_row[kMaxPlanes]; | 68 size_t plane_bytes_per_row[kMaxPlanes]; |
| 77 for (int plane_i = 0; plane_i < num_planes; ++plane_i) { | 69 for (int plane_i = 0; plane_i < num_planes; ++plane_i) { |
| 78 plane_ptrs[plane_i] = const_cast<uint8_t*>(frame.data(plane_i)); | 70 plane_ptrs[plane_i] = const_cast<uint8_t*>(frame.visible_data(plane_i)); |
| 79 gfx::Size plane_size = | 71 gfx::Size plane_size = |
| 80 VideoFrame::PlaneSize(video_frame_format, plane_i, coded_size); | 72 VideoFrame::PlaneSize(video_frame_format, plane_i, visible_rect.size()); |
| 81 plane_widths[plane_i] = plane_size.width(); | 73 plane_widths[plane_i] = plane_size.width(); |
| 82 plane_heights[plane_i] = plane_size.height(); | 74 plane_heights[plane_i] = plane_size.height(); |
| 83 plane_bytes_per_row[plane_i] = frame.stride(plane_i); | 75 plane_bytes_per_row[plane_i] = frame.stride(plane_i); |
| 84 } | 76 } |
| 85 | 77 |
| 86 // CVPixelBufferCreateWithPlanarBytes needs a dummy plane descriptor or the | 78 // CVPixelBufferCreateWithPlanarBytes needs a dummy plane descriptor or the |
| 87 // release callback will not execute. The descriptor is freed in the callback. | 79 // release callback will not execute. The descriptor is freed in the callback. |
| 88 void* descriptor = calloc( | 80 void* descriptor = calloc( |
| 89 1, | 81 1, |
| 90 std::max(sizeof(CVPlanarPixelBufferInfo_YCbCrPlanar), | 82 std::max(sizeof(CVPlanarPixelBufferInfo_YCbCrPlanar), |
| 91 sizeof(CoreVideoGlue::CVPlanarPixelBufferInfo_YCbCrBiPlanar))); | 83 sizeof(CoreVideoGlue::CVPlanarPixelBufferInfo_YCbCrBiPlanar))); |
| 92 | 84 |
| 93 // Wrap the frame's data in a CVPixelBuffer. Because this is a C API, we can't | 85 // Wrap the frame's data in a CVPixelBuffer. Because this is a C API, we can't |
| 94 // give it a smart pointer to the frame, so instead pass a raw pointer and | 86 // give it a smart pointer to the frame, so instead pass a raw pointer and |
| 95 // increment the frame's reference count manually. | 87 // increment the frame's reference count manually. |
| 96 CVReturn result = CVPixelBufferCreateWithPlanarBytes( | 88 CVReturn result = CVPixelBufferCreateWithPlanarBytes( |
| 97 kCFAllocatorDefault, coded_size.width(), coded_size.height(), cv_format, | 89 kCFAllocatorDefault, visible_rect.width(), visible_rect.height(), |
| 98 descriptor, 0, num_planes, plane_ptrs, plane_widths, plane_heights, | 90 cv_format, descriptor, 0, num_planes, plane_ptrs, plane_widths, |
| 99 plane_bytes_per_row, &CvPixelBufferReleaseCallback, | 91 plane_heights, plane_bytes_per_row, &CvPixelBufferReleaseCallback, |
| 100 const_cast<VideoFrame*>(&frame), nullptr, pixel_buffer.InitializeInto()); | 92 const_cast<VideoFrame*>(&frame), nullptr, pixel_buffer.InitializeInto()); |
| 101 if (result != kCVReturnSuccess) { | 93 if (result != kCVReturnSuccess) { |
| 102 DLOG(ERROR) << " CVPixelBufferCreateWithPlanarBytes failed: " << result; | 94 DLOG(ERROR) << " CVPixelBufferCreateWithPlanarBytes failed: " << result; |
| 103 return base::ScopedCFTypeRef<CVPixelBufferRef>(nullptr); | 95 return base::ScopedCFTypeRef<CVPixelBufferRef>(nullptr); |
| 104 } | 96 } |
| 105 | 97 |
| 106 // The CVPixelBuffer now references the data of the frame, so increment its | 98 // The CVPixelBuffer now references the data of the frame, so increment its |
| 107 // reference count manually. The release callback set on the pixel buffer will | 99 // reference count manually. The release callback set on the pixel buffer will |
| 108 // release the frame. | 100 // release the frame. |
| 109 frame.AddRef(); | 101 frame.AddRef(); |
| 110 | 102 |
| 111 // Apply required colorimetric attachments. | 103 // Apply required colorimetric attachments. |
| 112 CVBufferSetAttachment(pixel_buffer, kCVImageBufferColorPrimariesKey, | 104 CVBufferSetAttachment(pixel_buffer, kCVImageBufferColorPrimariesKey, |
| 113 kCVImageBufferColorPrimaries_ITU_R_709_2, | 105 kCVImageBufferColorPrimaries_ITU_R_709_2, |
| 114 kCVAttachmentMode_ShouldPropagate); | 106 kCVAttachmentMode_ShouldPropagate); |
| 115 CVBufferSetAttachment(pixel_buffer, kCVImageBufferTransferFunctionKey, | 107 CVBufferSetAttachment(pixel_buffer, kCVImageBufferTransferFunctionKey, |
| 116 kCVImageBufferTransferFunction_ITU_R_709_2, | 108 kCVImageBufferTransferFunction_ITU_R_709_2, |
| 117 kCVAttachmentMode_ShouldPropagate); | 109 kCVAttachmentMode_ShouldPropagate); |
| 118 CVBufferSetAttachment(pixel_buffer, kCVImageBufferYCbCrMatrixKey, | 110 CVBufferSetAttachment(pixel_buffer, kCVImageBufferYCbCrMatrixKey, |
| 119 kCVImageBufferYCbCrMatrix_ITU_R_709_2, | 111 kCVImageBufferYCbCrMatrix_ITU_R_709_2, |
| 120 kCVAttachmentMode_ShouldPropagate); | 112 kCVAttachmentMode_ShouldPropagate); |
| 121 | 113 |
| 122 return pixel_buffer; | 114 return pixel_buffer; |
| 123 } | 115 } |
| 124 | 116 |
| 125 } // namespace media | 117 } // namespace media |
| OLD | NEW |