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

Unified Diff: webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.cc

Issue 10899021: Add CDM video decoder. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Address more comments. Created 8 years, 2 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 side-by-side diff with in-line comments
Download patch
Index: webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.cc
diff --git a/webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.cc b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e0f53ee9988030c69ac0fd9af03d14c443b92b3e
--- /dev/null
+++ b/webkit/media/crypto/ppapi/ffmpeg_cdm_video_frame.cc
@@ -0,0 +1,158 @@
+// 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/ffmpeg_cdm_video_frame.h"
+
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "media/base/limits.h"
+#include "webkit/media/crypto/ppapi/ffmpeg_cdm_video_decoder.h"
+
+
+namespace webkit_media {
+
+namespace {
+
+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:
+ NOTREACHED() << "Unsupported video frame format: " << format;
+ return 0;
+ }
+}
+
+} // namespace
+
+// static
+FFmpegCdmVideoFrame* FFmpegCdmVideoFrame::Create(cdm::Allocator* allocator,
+ cdm::VideoFormat format,
+ const cdm::Size& size) {
+ if (!IsValidConfig(format, size)) {
+ LOG(ERROR) << "Create() invalid config.";
+ return NULL;
+ }
+
+ scoped_ptr<FFmpegCdmVideoFrame> frame(new FFmpegCdmVideoFrame(format, size));
+
+ if (!frame->AllocateYuv(allocator))
+ return NULL;
+
+ return frame.release();
+}
+
+FFmpegCdmVideoFrame::FFmpegCdmVideoFrame(cdm::VideoFormat format,
+ const cdm::Size& size)
+ : format_(format),
+ size_(size),
+ frame_buffer_(NULL) {
+ for (int32_t i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) {
+ plane_offsets_[i] = 0;
+ strides_[i] = 0;
+ }
+}
+
+FFmpegCdmVideoFrame::~FFmpegCdmVideoFrame() {
+ if (frame_buffer_)
+ frame_buffer_->Destroy();
+}
+
+int32_t FFmpegCdmVideoFrame::plane_offset(
+ cdm::VideoFrame::VideoPlane plane) const {
+ DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes);
+ return plane_offsets_[plane];
+}
+
+int32_t FFmpegCdmVideoFrame::stride(
+ cdm::VideoFrame::VideoPlane plane) const {
+ DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes);
+ return strides_[plane];
+}
+
+// static
+bool FFmpegCdmVideoFrame::IsValidConfig(cdm::VideoFormat format,
+ const cdm::Size& data_size) {
+ return ((format == cdm::kYv12 || format == cdm::kI420) &&
+ data_size.width > 0 && data_size.height > 0 &&
+ data_size.width <= media::limits::kMaxDimension &&
+ data_size.height <= media::limits::kMaxDimension &&
+ data_size.width * data_size.height <= media::limits::kMaxCanvas);
+}
+
+void FFmpegCdmVideoFrame::MoveVideoFrame(cdm::VideoFrame* target) {
+ DCHECK(frame_buffer_);
+ DCHECK(target);
+ target->set_format(format_);
+ target->set_frame_buffer(frame_buffer_);
+ frame_buffer_ = NULL;
+
+ typedef cdm::VideoFrame::VideoPlane VideoPlane;
+ const int kMaxPlanes = cdm::VideoFrame::kMaxPlanes;
+ for (int i = 0; static_cast<VideoPlane>(i) < kMaxPlanes; ++i) {
+ cdm::VideoFrame::VideoPlane plane = static_cast<VideoPlane>(i);
+ target->set_plane_offset(plane, plane_offsets_[i]);
+ target->set_stride(plane, strides_[i]);
+ }
+}
+
+bool FFmpegCdmVideoFrame::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(3) << "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 = y_height / 2;
+ size_t y_bytes = y_height * y_stride;
+ size_t uv_bytes = uv_height * uv_stride;
+
+ frame_buffer_ = allocator->Allocate(y_bytes + (uv_bytes * 2));
+ if (!frame_buffer_) {
+ LOG(ERROR) << "AllocateYuv() cdm::Allocator::Allocate failed.";
+ return false;
+ }
+
+ COMPILE_ASSERT(0 == cdm::VideoFrame::kYPlane, y_plane_data_must_be_index_0);
+ uint8_t* data = frame_buffer_->data();
+ DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(data) & (kBufferAlignment - 1));
+
+ plane_offsets_[cdm::VideoFrame::kYPlane] = 0;
+
+ if (format_ == cdm::kYv12) {
+ plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes;
+ plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes + uv_bytes;
+ } else {
+ plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes;
+ plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes + uv_bytes;
+ }
+
+ strides_[cdm::VideoFrame::kYPlane] = y_stride;
+ strides_[cdm::VideoFrame::kUPlane] = uv_stride;
+ strides_[cdm::VideoFrame::kVPlane] = uv_stride;
+
+ return true;
+}
+
+} // namespace webkit_media

Powered by Google App Engine
This is Rietveld 408576698