Index: webkit/media/crypto/ppapi/video_frame.cc |
diff --git a/webkit/media/crypto/ppapi/video_frame.cc b/webkit/media/crypto/ppapi/video_frame.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..6b45d6e04dedf50c0d17ceb57aed45ab977e6f85 |
--- /dev/null |
+++ b/webkit/media/crypto/ppapi/video_frame.cc |
@@ -0,0 +1,127 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "webkit/media/crypto/ppapi/video_frame.h" |
+ |
+#include "base/logging.h" |
+#include "base/memory/scoped_ptr.h" |
+#include "media/base/limits.h" |
+#include "webkit/media/crypto/ppapi/ffmpeg_video_decoder.h" |
+ |
+namespace { |
ddorwin
2012/10/13 03:54:42
This should be inside the other namespace.
media/
Tom Finegan
2012/10/13 23:47:26
Done.
|
+ |
+inline size_t RoundUp(size_t value, size_t alignment) { |
+ // Check that |alignment| is a power of 2. |
+ DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); |
+ return ((value + (alignment - 1)) & ~(alignment-1)); |
+} |
+ |
+int RowBytes(cdm::VideoFormat format, |
+ cdm::VideoFrame::VideoPlane plane, |
+ int width) { |
+ switch (format) { |
+ case cdm::kYv12: |
+ case cdm::kI420: |
+ if (plane == cdm::VideoFrame::kYPlane) |
+ return width; |
+ return RoundUp(width, 2) / 2; |
+ |
+ default: |
+ break; |
+ } |
+ |
+ NOTREACHED() << "Unsupported video frame format: " << format; |
ddorwin
2012/10/13 03:54:42
Put these lines in default.
Tom Finegan
2012/10/13 23:47:26
Done.
|
+ return 0; |
+} |
+ |
+} // anonymous namespace |
+ |
+namespace webkit_media { |
+ |
+// static |
+VideoFrame* VideoFrame::Create(cdm::Allocator* allocator, |
+ cdm::VideoFormat format, |
+ const cdm::Size& size, |
+ int64_t timestamp) { |
+ if (!IsValidConfig(format, size)) { |
+ LOG(ERROR) << "VideoFrame::Create invalid config."; |
+ return NULL; |
+ } |
+ |
+ scoped_ptr<VideoFrame> frame(new VideoFrame(format, size, timestamp)); |
+ |
+ if (!frame->AllocateYUV(allocator)) |
+ return NULL; |
+ |
+ return frame.release(); |
+} |
+ |
+VideoFrame::VideoFrame(cdm::VideoFormat format, |
+ const cdm::Size& size, |
+ int64_t timestamp) |
+ : size_(size), |
+ format_(format), |
+ frame_(NULL) { |
ddorwin
2012/10/13 03:54:42
Where is frame_ set? It must be requested from the
Tom Finegan
2012/10/13 23:47:26
I removed the cdm::VideoFrame abuse. What I had wo
|
+} |
+ |
+VideoFrame::~VideoFrame() { |
+ if (frame_ && frame_->frame_buffer()) |
+ frame_->frame_buffer()->Destroy(); |
ddorwin
2012/10/13 03:54:42
Does cdm::~VideoFrameImpl() not do this?
Tom Finegan
2012/10/13 23:47:26
This class now creates a cdm::Buffer that it owns
|
+} |
+ |
+cdm::Size VideoFrame::size() const { |
+ return size_; |
+} |
+ |
+cdm::VideoFrame* VideoFrame::frame() { |
+ return frame_; |
+} |
+ |
+cdm::VideoFormat VideoFrame::format() const { |
+ return format_; |
+} |
+ |
+void VideoFrame::ReleaseCdmVideoFrame() { |
+ frame_ = NULL; |
+} |
+ |
+bool VideoFrame::AllocateYUV(cdm::Allocator* allocator) { |
+ DCHECK(allocator); |
+ DCHECK(format_ == cdm::kYv12 || format_ == cdm::kI420); |
+ |
+ // Align Y rows at least at 32 byte boundaries for safety on all platforms. |
+ DVLOG(1) << "VideoFrame::AllocateYUV: width=" << size_.width |
+ << " height=" << size_.height; |
+ size_t y_stride = RoundUp( |
+ RowBytes(format_, cdm::VideoFrame::kYPlane, size_.width), |
+ kBufferAlignment); |
+ size_t uv_stride = RoundUp( |
+ RowBytes(format_, cdm::VideoFrame::kUPlane, size_.width), |
+ kBufferAlignment); |
+ |
+ size_t y_height = RoundUp(size_.height, kBufferAlignment); |
+ size_t uv_height = format_ == cdm::kYv12 ? y_height / 2 : y_height; |
+ size_t y_bytes = y_height * y_stride; |
+ size_t uv_bytes = uv_height * uv_stride; |
+ |
+ frame_->set_frame_buffer(allocator->Allocate(y_bytes + (uv_bytes * 2))); |
+ if (!frame_->frame_buffer()) { |
+ LOG(ERROR) << "VideoFrame::AllocateYUV Allocate failed."; |
+ return false; |
+ } |
+ |
+ COMPILE_ASSERT(0 == cdm::VideoFrame::kYPlane, y_plane_data_must_be_index_0); |
+ uint8_t* data = frame_->frame_buffer()->data(); |
+ DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(data) & (kBufferAlignment - 1)); |
+ |
+ frame_->set_plane_offset(cdm::VideoFrame::kYPlane, 0); |
+ frame_->set_plane_offset(cdm::VideoFrame::kUPlane, y_bytes); |
+ frame_->set_plane_offset(cdm::VideoFrame::kVPlane, y_bytes + uv_bytes); |
+ frame_->set_stride(cdm::VideoFrame::kYPlane, y_stride); |
+ frame_->set_stride(cdm::VideoFrame::kUPlane, uv_stride); |
+ frame_->set_stride(cdm::VideoFrame::kVPlane, uv_stride); |
+ return true; |
+} |
+ |
+} // namespace webkit_media |