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/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_video_decoder.h" | |
11 | |
12 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.
| |
13 | |
14 inline size_t RoundUp(size_t value, size_t alignment) { | |
15 // Check that |alignment| is a power of 2. | |
16 DCHECK((alignment + (alignment - 1)) == (alignment | (alignment - 1))); | |
17 return ((value + (alignment - 1)) & ~(alignment-1)); | |
18 } | |
19 | |
20 int RowBytes(cdm::VideoFormat format, | |
21 cdm::VideoFrame::VideoPlane plane, | |
22 int width) { | |
23 switch (format) { | |
24 case cdm::kYv12: | |
25 case cdm::kI420: | |
26 if (plane == cdm::VideoFrame::kYPlane) | |
27 return width; | |
28 return RoundUp(width, 2) / 2; | |
29 | |
30 default: | |
31 break; | |
32 } | |
33 | |
34 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.
| |
35 return 0; | |
36 } | |
37 | |
38 } // anonymous namespace | |
39 | |
40 namespace webkit_media { | |
41 | |
42 // static | |
43 VideoFrame* VideoFrame::Create(cdm::Allocator* allocator, | |
44 cdm::VideoFormat format, | |
45 const cdm::Size& size, | |
46 int64_t timestamp) { | |
47 if (!IsValidConfig(format, size)) { | |
48 LOG(ERROR) << "VideoFrame::Create invalid config."; | |
49 return NULL; | |
50 } | |
51 | |
52 scoped_ptr<VideoFrame> frame(new VideoFrame(format, size, timestamp)); | |
53 | |
54 if (!frame->AllocateYUV(allocator)) | |
55 return NULL; | |
56 | |
57 return frame.release(); | |
58 } | |
59 | |
60 VideoFrame::VideoFrame(cdm::VideoFormat format, | |
61 const cdm::Size& size, | |
62 int64_t timestamp) | |
63 : size_(size), | |
64 format_(format), | |
65 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
| |
66 } | |
67 | |
68 VideoFrame::~VideoFrame() { | |
69 if (frame_ && frame_->frame_buffer()) | |
70 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
| |
71 } | |
72 | |
73 cdm::Size VideoFrame::size() const { | |
74 return size_; | |
75 } | |
76 | |
77 cdm::VideoFrame* VideoFrame::frame() { | |
78 return frame_; | |
79 } | |
80 | |
81 cdm::VideoFormat VideoFrame::format() const { | |
82 return format_; | |
83 } | |
84 | |
85 void VideoFrame::ReleaseCdmVideoFrame() { | |
86 frame_ = NULL; | |
87 } | |
88 | |
89 bool VideoFrame::AllocateYUV(cdm::Allocator* allocator) { | |
90 DCHECK(allocator); | |
91 DCHECK(format_ == cdm::kYv12 || format_ == cdm::kI420); | |
92 | |
93 // Align Y rows at least at 32 byte boundaries for safety on all platforms. | |
94 DVLOG(1) << "VideoFrame::AllocateYUV: width=" << size_.width | |
95 << " height=" << size_.height; | |
96 size_t y_stride = RoundUp( | |
97 RowBytes(format_, cdm::VideoFrame::kYPlane, size_.width), | |
98 kBufferAlignment); | |
99 size_t uv_stride = RoundUp( | |
100 RowBytes(format_, cdm::VideoFrame::kUPlane, size_.width), | |
101 kBufferAlignment); | |
102 | |
103 size_t y_height = RoundUp(size_.height, kBufferAlignment); | |
104 size_t uv_height = format_ == cdm::kYv12 ? y_height / 2 : y_height; | |
105 size_t y_bytes = y_height * y_stride; | |
106 size_t uv_bytes = uv_height * uv_stride; | |
107 | |
108 frame_->set_frame_buffer(allocator->Allocate(y_bytes + (uv_bytes * 2))); | |
109 if (!frame_->frame_buffer()) { | |
110 LOG(ERROR) << "VideoFrame::AllocateYUV Allocate failed."; | |
111 return false; | |
112 } | |
113 | |
114 COMPILE_ASSERT(0 == cdm::VideoFrame::kYPlane, y_plane_data_must_be_index_0); | |
115 uint8_t* data = frame_->frame_buffer()->data(); | |
116 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(data) & (kBufferAlignment - 1)); | |
117 | |
118 frame_->set_plane_offset(cdm::VideoFrame::kYPlane, 0); | |
119 frame_->set_plane_offset(cdm::VideoFrame::kUPlane, y_bytes); | |
120 frame_->set_plane_offset(cdm::VideoFrame::kVPlane, y_bytes + uv_bytes); | |
121 frame_->set_stride(cdm::VideoFrame::kYPlane, y_stride); | |
122 frame_->set_stride(cdm::VideoFrame::kUPlane, uv_stride); | |
123 frame_->set_stride(cdm::VideoFrame::kVPlane, uv_stride); | |
124 return true; | |
125 } | |
126 | |
127 } // namespace webkit_media | |
OLD | NEW |