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> |
11 | 11 |
12 #include "media/base/mac/corevideo_glue.h" | |
13 #include "media/base/video_frame.h" | 12 #include "media/base/video_frame.h" |
14 | 13 |
15 namespace media { | 14 namespace media { |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 // Maximum number of planes supported by this implementation. | 18 // Maximum number of planes supported by this implementation. |
20 const int kMaxPlanes = 3; | 19 const int kMaxPlanes = 3; |
21 | 20 |
22 // CVPixelBuffer release callback. See |GetCvPixelBufferRepresentation()|. | 21 // CVPixelBuffer release callback. See |GetCvPixelBufferRepresentation()|. |
(...skipping 21 matching lines...) Expand all Loading... |
44 // VideoFrame only supports YUV formats and most of them are 'YVU' ordered, | 43 // VideoFrame only supports YUV formats and most of them are 'YVU' ordered, |
45 // which CVPixelBuffer does not support. This means we effectively can only | 44 // which CVPixelBuffer does not support. This means we effectively can only |
46 // represent I420 and NV12 frames. In addition, VideoFrame does not carry | 45 // represent I420 and NV12 frames. In addition, VideoFrame does not carry |
47 // colorimetric information, so this function assumes standard video range | 46 // colorimetric information, so this function assumes standard video range |
48 // and ITU Rec 709 primaries. | 47 // and ITU Rec 709 primaries. |
49 const VideoPixelFormat video_frame_format = frame.format(); | 48 const VideoPixelFormat video_frame_format = frame.format(); |
50 OSType cv_format; | 49 OSType cv_format; |
51 if (video_frame_format == PIXEL_FORMAT_I420) { | 50 if (video_frame_format == PIXEL_FORMAT_I420) { |
52 cv_format = kCVPixelFormatType_420YpCbCr8Planar; | 51 cv_format = kCVPixelFormatType_420YpCbCr8Planar; |
53 } else if (video_frame_format == PIXEL_FORMAT_NV12) { | 52 } else if (video_frame_format == PIXEL_FORMAT_NV12) { |
54 cv_format = CoreVideoGlue::kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; | 53 cv_format = kCVPixelFormatType_420YpCbCr8BiPlanarVideoRange; |
55 } else { | 54 } else { |
56 DLOG(ERROR) << " unsupported frame format: " << video_frame_format; | 55 DLOG(ERROR) << " unsupported frame format: " << video_frame_format; |
57 return pixel_buffer; | 56 return pixel_buffer; |
58 } | 57 } |
59 | 58 |
60 int num_planes = VideoFrame::NumPlanes(video_frame_format); | 59 int num_planes = VideoFrame::NumPlanes(video_frame_format); |
61 DCHECK_LE(num_planes, kMaxPlanes); | 60 DCHECK_LE(num_planes, kMaxPlanes); |
62 const gfx::Rect& visible_rect = frame.visible_rect(); | 61 const gfx::Rect& visible_rect = frame.visible_rect(); |
63 | 62 |
64 // Build arrays for each plane's data pointer, dimensions and byte alignment. | 63 // Build arrays for each plane's data pointer, dimensions and byte alignment. |
65 void* plane_ptrs[kMaxPlanes]; | 64 void* plane_ptrs[kMaxPlanes]; |
66 size_t plane_widths[kMaxPlanes]; | 65 size_t plane_widths[kMaxPlanes]; |
67 size_t plane_heights[kMaxPlanes]; | 66 size_t plane_heights[kMaxPlanes]; |
68 size_t plane_bytes_per_row[kMaxPlanes]; | 67 size_t plane_bytes_per_row[kMaxPlanes]; |
69 for (int plane_i = 0; plane_i < num_planes; ++plane_i) { | 68 for (int plane_i = 0; plane_i < num_planes; ++plane_i) { |
70 plane_ptrs[plane_i] = const_cast<uint8_t*>(frame.visible_data(plane_i)); | 69 plane_ptrs[plane_i] = const_cast<uint8_t*>(frame.visible_data(plane_i)); |
71 gfx::Size plane_size = | 70 gfx::Size plane_size = |
72 VideoFrame::PlaneSize(video_frame_format, plane_i, visible_rect.size()); | 71 VideoFrame::PlaneSize(video_frame_format, plane_i, visible_rect.size()); |
73 plane_widths[plane_i] = plane_size.width(); | 72 plane_widths[plane_i] = plane_size.width(); |
74 plane_heights[plane_i] = plane_size.height(); | 73 plane_heights[plane_i] = plane_size.height(); |
75 plane_bytes_per_row[plane_i] = frame.stride(plane_i); | 74 plane_bytes_per_row[plane_i] = frame.stride(plane_i); |
76 } | 75 } |
77 | 76 |
78 // CVPixelBufferCreateWithPlanarBytes needs a dummy plane descriptor or the | 77 // CVPixelBufferCreateWithPlanarBytes needs a dummy plane descriptor or the |
79 // release callback will not execute. The descriptor is freed in the callback. | 78 // release callback will not execute. The descriptor is freed in the callback. |
80 void* descriptor = calloc( | 79 void* descriptor = |
81 1, | 80 calloc(1, std::max(sizeof(CVPlanarPixelBufferInfo_YCbCrPlanar), |
82 std::max(sizeof(CVPlanarPixelBufferInfo_YCbCrPlanar), | 81 sizeof(CVPlanarPixelBufferInfo_YCbCrBiPlanar))); |
83 sizeof(CoreVideoGlue::CVPlanarPixelBufferInfo_YCbCrBiPlanar))); | |
84 | 82 |
85 // Wrap the frame's data in a CVPixelBuffer. Because this is a C API, we can't | 83 // Wrap the frame's data in a CVPixelBuffer. Because this is a C API, we can't |
86 // give it a smart pointer to the frame, so instead pass a raw pointer and | 84 // give it a smart pointer to the frame, so instead pass a raw pointer and |
87 // increment the frame's reference count manually. | 85 // increment the frame's reference count manually. |
88 CVReturn result = CVPixelBufferCreateWithPlanarBytes( | 86 CVReturn result = CVPixelBufferCreateWithPlanarBytes( |
89 kCFAllocatorDefault, visible_rect.width(), visible_rect.height(), | 87 kCFAllocatorDefault, visible_rect.width(), visible_rect.height(), |
90 cv_format, descriptor, 0, num_planes, plane_ptrs, plane_widths, | 88 cv_format, descriptor, 0, num_planes, plane_ptrs, plane_widths, |
91 plane_heights, plane_bytes_per_row, &CvPixelBufferReleaseCallback, | 89 plane_heights, plane_bytes_per_row, &CvPixelBufferReleaseCallback, |
92 const_cast<VideoFrame*>(&frame), nullptr, pixel_buffer.InitializeInto()); | 90 const_cast<VideoFrame*>(&frame), nullptr, pixel_buffer.InitializeInto()); |
93 if (result != kCVReturnSuccess) { | 91 if (result != kCVReturnSuccess) { |
(...skipping 14 matching lines...) Expand all Loading... |
108 kCVImageBufferTransferFunction_ITU_R_709_2, | 106 kCVImageBufferTransferFunction_ITU_R_709_2, |
109 kCVAttachmentMode_ShouldPropagate); | 107 kCVAttachmentMode_ShouldPropagate); |
110 CVBufferSetAttachment(pixel_buffer, kCVImageBufferYCbCrMatrixKey, | 108 CVBufferSetAttachment(pixel_buffer, kCVImageBufferYCbCrMatrixKey, |
111 kCVImageBufferYCbCrMatrix_ITU_R_709_2, | 109 kCVImageBufferYCbCrMatrix_ITU_R_709_2, |
112 kCVAttachmentMode_ShouldPropagate); | 110 kCVAttachmentMode_ShouldPropagate); |
113 | 111 |
114 return pixel_buffer; | 112 return pixel_buffer; |
115 } | 113 } |
116 | 114 |
117 } // namespace media | 115 } // namespace media |
OLD | NEW |