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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 static const uint32_t kBufferAlignment = 32;
16
17 static inline int RoundUp(int value, int 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 static 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 // static
40 FFmpegCdmVideoFrame* FFmpegCdmVideoFrame::Create(cdm::Allocator* allocator,
41 cdm::VideoFormat format,
42 const cdm::Size& size) {
43 if (!IsValidConfig(format, size)) {
44 LOG(ERROR) << "Create() invalid config.";
45 return NULL;
46 }
47
48 scoped_ptr<FFmpegCdmVideoFrame> frame(new FFmpegCdmVideoFrame(format, size));
49
50 if (!frame->AllocateYuv(allocator))
51 return NULL;
52
53 return frame.release();
54 }
55
56 FFmpegCdmVideoFrame::FFmpegCdmVideoFrame(cdm::VideoFormat format,
57 const cdm::Size& size)
58 : format_(format),
59 size_(size),
60 frame_buffer_(NULL) {
61 for (int32_t i = 0; i < cdm::VideoFrame::kMaxPlanes; ++i) {
62 plane_offsets_[i] = 0;
63 strides_[i] = 0;
64 }
65 }
66
67 FFmpegCdmVideoFrame::~FFmpegCdmVideoFrame() {
68 if (frame_buffer_)
69 frame_buffer_->Destroy();
70 }
71
72 int32_t FFmpegCdmVideoFrame::plane_offset(
73 cdm::VideoFrame::VideoPlane plane) const {
74 DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes);
75 return plane_offsets_[plane];
76 }
77
78 int32_t FFmpegCdmVideoFrame::stride(
79 cdm::VideoFrame::VideoPlane plane) const {
80 DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes);
81 return strides_[plane];
82 }
83
84 // static
85 bool FFmpegCdmVideoFrame::IsValidConfig(cdm::VideoFormat format,
86 const cdm::Size& data_size) {
87 return ((format == cdm::kYv12 || format == cdm::kI420) &&
88 data_size.width > 0 && data_size.height > 0 &&
89 data_size.width <= media::limits::kMaxDimension &&
90 data_size.height <= media::limits::kMaxDimension &&
91 data_size.width * data_size.height <= media::limits::kMaxCanvas);
92 }
93
94 void FFmpegCdmVideoFrame::PassVideoFrame(cdm::VideoFrame* target) {
95 DCHECK(frame_buffer_);
96 DCHECK(target);
97 target->set_format(format_);
98 target->set_size(size_);
99 target->set_frame_buffer(frame_buffer_);
100
101 const int kMaxPlanes = cdm::VideoFrame::kMaxPlanes;
102 for (int i = 0; i < kMaxPlanes; ++i) {
103 cdm::VideoFrame::VideoPlane plane =
104 static_cast<cdm::VideoFrame::VideoPlane>(i);
105 target->set_plane_offset(plane, plane_offsets_[i]);
106 target->set_stride(plane, strides_[i]);
107 plane_offsets_[i] = 0;
108 strides_[i] = 0;
109 }
110
111 format_ = cdm::kEmptyVideoFrame;
112 size_.width = 0;
113 size_.height = 0;
114 frame_buffer_ = NULL;
115 }
116
117 bool FFmpegCdmVideoFrame::AllocateYuv(cdm::Allocator* allocator) {
118 DCHECK(allocator);
119 DCHECK(format_ == cdm::kYv12 || format_ == cdm::kI420);
120
121 // Align Y rows at least at 32 byte boundaries for safety on all platforms.
122 DVLOG(3) << "AllocateYuv() width=" << size_.width << " height="
123 << size_.height;
124 size_t y_stride = RoundUp(
125 RowBytes(format_, cdm::VideoFrame::kYPlane, size_.width),
126 kBufferAlignment);
127 size_t uv_stride = RoundUp(
128 RowBytes(format_, cdm::VideoFrame::kUPlane, size_.width),
129 kBufferAlignment);
130
131 // The RoundUp() here is because some formats (e.g. h264) allow interlaced
132 // coding, and then the size needs to be a multiple of two macroblocks
133 // (vertically). See libavcodec/utils.c:avcodec_align_dimensions2().
xhwang 2012/10/17 20:23:10 good to know! Thanks.
134 size_t y_height = RoundUp(size_.height, kBufferAlignment);
135 size_t uv_height = y_height / 2;
136 size_t y_bytes = y_height * y_stride;
137 size_t uv_bytes = uv_height * uv_stride;
138
139 frame_buffer_ = allocator->Allocate(y_bytes + (uv_bytes * 2));
140 if (!frame_buffer_) {
141 LOG(ERROR) << "AllocateYuv() cdm::Allocator::Allocate failed.";
142 return false;
143 }
144
145 COMPILE_ASSERT(0 == cdm::VideoFrame::kYPlane, y_plane_data_must_be_index_0);
146 uint8_t* data = frame_buffer_->data();
147 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(data) & (kBufferAlignment - 1));
148
149 plane_offsets_[cdm::VideoFrame::kYPlane] = 0;
150
151 if (format_ == cdm::kYv12) {
152 plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes;
153 plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes + uv_bytes;
154 } else {
155 plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes;
156 plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes + uv_bytes;
157 }
158
159 strides_[cdm::VideoFrame::kYPlane] = y_stride;
160 strides_[cdm::VideoFrame::kUPlane] = uv_stride;
161 strides_[cdm::VideoFrame::kVPlane] = uv_stride;
162
163 return true;
164 }
165
166 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698