OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "media/base/limits.h" |
| 10 #include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h" |
| 11 |
| 12 |
| 13 namespace webkit_media { |
| 14 |
| 15 namespace { |
| 16 |
| 17 inline size_t RoundUp(size_t value, size_t alignment) { |
| 18 // Check that |alignment| is a power of 2. |
| 19 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); |
| 20 return ((value + (alignment - 1)) & ~(alignment-1)); |
| 21 } |
| 22 |
| 23 int RowBytes(cdm::VideoFormat format, |
| 24 cdm::VideoFrame::VideoPlane plane, |
| 25 int width) { |
| 26 switch (format) { |
| 27 case cdm::kYv12: |
| 28 case cdm::kI420: |
| 29 if (plane == cdm::VideoFrame::kYPlane) |
| 30 return width; |
| 31 return RoundUp(width, 2) / 2; |
| 32 |
| 33 default: |
| 34 NOTREACHED() << "Unsupported video frame format: " << format; |
| 35 return 0; |
| 36 } |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 // static |
| 42 FFmpegCdmVideoFrame* FFmpegCdmVideoFrame::Create(cdm::Allocator* allocator, |
| 43 cdm::VideoFormat format, |
| 44 const cdm::Size& size) { |
| 45 if (!IsValidConfig(format, size)) { |
| 46 LOG(ERROR) << "Create() invalid config."; |
| 47 return NULL; |
| 48 } |
| 49 |
| 50 scoped_ptr<FFmpegCdmVideoFrame> frame(new FFmpegCdmVideoFrame(format, size)); |
| 51 |
| 52 if (!frame->AllocateYuv(allocator)) |
| 53 return NULL; |
| 54 |
| 55 return frame.release(); |
| 56 } |
| 57 |
| 58 FFmpegCdmVideoFrame::FFmpegCdmVideoFrame(cdm::VideoFormat format, |
| 59 const cdm::Size& size) |
| 60 : format_(format), |
| 61 size_(size), |
| 62 frame_buffer_(NULL) { |
| 63 for (int32_t i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) { |
| 64 plane_offsets_[i] = 0; |
| 65 strides_[i] = 0; |
| 66 } |
| 67 } |
| 68 |
| 69 FFmpegCdmVideoFrame::~FFmpegCdmVideoFrame() { |
| 70 if (frame_buffer_) |
| 71 frame_buffer_->Destroy(); |
| 72 } |
| 73 |
| 74 int32_t FFmpegCdmVideoFrame::plane_offset( |
| 75 cdm::VideoFrame::VideoPlane plane) const { |
| 76 DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes); |
| 77 return plane_offsets_[plane]; |
| 78 } |
| 79 |
| 80 int32_t FFmpegCdmVideoFrame::stride( |
| 81 cdm::VideoFrame::VideoPlane plane) const { |
| 82 DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes); |
| 83 return strides_[plane]; |
| 84 } |
| 85 |
| 86 // static |
| 87 bool FFmpegCdmVideoFrame::IsValidConfig(cdm::VideoFormat format, |
| 88 const cdm::Size& data_size) { |
| 89 return ((format == cdm::kYv12 || format == cdm::kI420) && |
| 90 data_size.width > 0 && data_size.height > 0 && |
| 91 data_size.width <= media::limits::kMaxDimension && |
| 92 data_size.height <= media::limits::kMaxDimension && |
| 93 data_size.width * data_size.height <= media::limits::kMaxCanvas); |
| 94 } |
| 95 |
| 96 void FFmpegCdmVideoFrame::MoveVideoFrame(cdm::VideoFrame* target) { |
| 97 DCHECK(frame_buffer_); |
| 98 DCHECK(target); |
| 99 target->set_format(format_); |
| 100 target->set_frame_buffer(frame_buffer_); |
| 101 frame_buffer_ = NULL; |
| 102 |
| 103 typedef cdm::VideoFrame::VideoPlane VideoPlane; |
| 104 const int kMaxPlanes = cdm::VideoFrame::kMaxPlanes; |
| 105 for (int i = 0; static_cast<VideoPlane>(i) < kMaxPlanes; ++i) { |
| 106 cdm::VideoFrame::VideoPlane plane = static_cast<VideoPlane>(i); |
| 107 target->set_plane_offset(plane, plane_offsets_[i]); |
| 108 target->set_stride(plane, strides_[i]); |
| 109 } |
| 110 } |
| 111 |
| 112 bool FFmpegCdmVideoFrame::AllocateYuv(cdm::Allocator* allocator) { |
| 113 DCHECK(allocator); |
| 114 DCHECK(format_ == cdm::kYv12 || format_ == cdm::kI420); |
| 115 |
| 116 // Align Y rows at least at 32 byte boundaries for safety on all platforms. |
| 117 DVLOG(3) << "AllocateYuv() width=" << size_.width << " height=" |
| 118 << size_.height; |
| 119 size_t y_stride = RoundUp( |
| 120 RowBytes(format_, cdm::VideoFrame::kYPlane, size_.width), |
| 121 kBufferAlignment); |
| 122 size_t uv_stride = RoundUp( |
| 123 RowBytes(format_, cdm::VideoFrame::kUPlane, size_.width), |
| 124 kBufferAlignment); |
| 125 |
| 126 size_t y_height = RoundUp(size_.height, kBufferAlignment); |
| 127 size_t uv_height = y_height / 2; |
| 128 size_t y_bytes = y_height * y_stride; |
| 129 size_t uv_bytes = uv_height * uv_stride; |
| 130 |
| 131 frame_buffer_ = allocator->Allocate(y_bytes + (uv_bytes * 2)); |
| 132 if (!frame_buffer_) { |
| 133 LOG(ERROR) << "AllocateYuv() cdm::Allocator::Allocate failed."; |
| 134 return false; |
| 135 } |
| 136 |
| 137 COMPILE_ASSERT(0 == cdm::VideoFrame::kYPlane, y_plane_data_must_be_index_0); |
| 138 uint8_t* data = frame_buffer_->data(); |
| 139 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(data) & (kBufferAlignment - 1)); |
| 140 |
| 141 plane_offsets_[cdm::VideoFrame::kYPlane] = 0; |
| 142 |
| 143 if (format_ == cdm::kYv12) { |
| 144 plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes; |
| 145 plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes + uv_bytes; |
| 146 } else { |
| 147 plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes; |
| 148 plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes + uv_bytes; |
| 149 } |
| 150 |
| 151 strides_[cdm::VideoFrame::kYPlane] = y_stride; |
| 152 strides_[cdm::VideoFrame::kUPlane] = uv_stride; |
| 153 strides_[cdm::VideoFrame::kVPlane] = uv_stride; |
| 154 |
| 155 return true; |
| 156 } |
| 157 |
| 158 } // namespace webkit_media |
OLD | NEW |