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

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: Allocate PP_DCHECK, rename video buffer callbacks, stop leaking video frames when FFmpeg releases t… 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 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) << "VideoFrame::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 const cdm::Size& FFmpegCdmVideoFrame::size() const {
75 return size_;
76 }
77
78 cdm::VideoFormat FFmpegCdmVideoFrame::format() const {
79 return format_;
xhwang 2012/10/16 21:59:53 accessors can be defined in header files
Tom Finegan 2012/10/17 02:39:10 Done. Moved the single line ones into the header f
80 }
81
82 cdm::Buffer* FFmpegCdmVideoFrame::frame_buffer() {
83 return frame_buffer_;
84 }
85
86 int32_t FFmpegCdmVideoFrame::plane_offset(cdm::VideoFrame::VideoPlane plane) {
87 DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes);
88 return plane_offsets_[plane];
89 }
90
91 int32_t FFmpegCdmVideoFrame::stride(cdm::VideoFrame::VideoPlane plane) {
92 DCHECK(0 <= plane && plane < cdm::VideoFrame::kMaxPlanes);
93 return strides_[plane];
94 }
95
96
xhwang 2012/10/16 21:59:53 remove extra empty line
Tom Finegan 2012/10/17 02:39:10 Done.
97 cdm::Buffer* FFmpegCdmVideoFrame::ReleaseCdmBuffer() {
98 cdm::Buffer* buffer = frame_buffer_;
99 frame_buffer_ = NULL;
100 return buffer;
101 }
102
103 bool FFmpegCdmVideoFrame::AllocateYUV(cdm::Allocator* allocator) {
xhwang 2012/10/16 21:59:53 Bikeshedding: Not sure if it should be YUV or Yuv
Tom Finegan 2012/10/17 02:39:10 Done. I prefer Yuv anyway. :)
104 DCHECK(allocator);
105 DCHECK(format_ == cdm::kYv12 || format_ == cdm::kI420);
106
107 // Align Y rows at least at 32 byte boundaries for safety on all platforms.
108 DVLOG(1) << "VideoFrame::AllocateYUV: width=" << size_.width
xhwang 2012/10/16 21:59:53 s/VideoFame/FFmpegCdmVideoFrame
Tom Finegan 2012/10/17 02:39:10 Removed class name from the comment. File name and
109 << " height=" << size_.height;
110 size_t y_stride = RoundUp(
111 RowBytes(format_, cdm::VideoFrame::kYPlane, size_.width),
112 kBufferAlignment);
113 size_t uv_stride = RoundUp(
114 RowBytes(format_, cdm::VideoFrame::kUPlane, size_.width),
115 kBufferAlignment);
116
117 size_t y_height = RoundUp(size_.height, kBufferAlignment);
118 size_t uv_height = format_ == cdm::kYv12 ? y_height / 2 : y_height;
xhwang 2012/10/16 21:59:53 The difference b/w YV12 and I420 is that YV12 is Y
Tom Finegan 2012/10/17 02:39:10 Thanks for catching this-- this is based on media:
119 size_t y_bytes = y_height * y_stride;
120 size_t uv_bytes = uv_height * uv_stride;
121
122 frame_buffer_ = allocator->Allocate(y_bytes + (uv_bytes * 2));
123 if (!frame_buffer_) {
124 LOG(ERROR) << "cdm::Allocator::Allocate failed.";
125 return false;
126 }
127
128 COMPILE_ASSERT(0 == cdm::VideoFrame::kYPlane, y_plane_data_must_be_index_0);
129 uint8_t* data = frame_buffer_->data();
130 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(data) & (kBufferAlignment - 1));
131
132 plane_offsets_[cdm::VideoFrame::kYPlane] = 0;
133
134 if (format_ == cdm::kYv12) {
135 plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes;
136 plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes + uv_bytes;
137 } else {
138 plane_offsets_[cdm::VideoFrame::kUPlane] = y_bytes;
139 plane_offsets_[cdm::VideoFrame::kVPlane] = y_bytes + uv_bytes;
140 }
141
142 strides_[cdm::VideoFrame::kYPlane] = y_stride;
143 strides_[cdm::VideoFrame::kUPlane] = uv_stride;
144 strides_[cdm::VideoFrame::kVPlane] = uv_stride;
145
146 return true;
147 }
148
149 } // namespace webkit_media
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698