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

Side by Side Diff: content/common/gpu/media/mac_video_decode_accelerator.mm

Issue 10388108: Implement media::VideoDecodeAccelerator on Mac (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: address review comments Created 8 years, 7 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 "content/common/gpu/media/mac_video_decode_accelerator.h"
6
7 #include "base/bind.h"
8 #include "base/file_path.h"
9 #import "base/mac/foundation_util.h"
10 #import "base/memory/ref_counted_memory.h"
11 #import "base/message_loop.h"
12 #include "base/location.h"
13 #include "base/native_library.h"
14 #include "ui/surface/io_surface_support_mac.h"
15 #include "ui/gfx/video_decode_acceleration_support_mac.h"
16
17 namespace {
18
19 enum { kNumPictureBuffers = 4 };
20
21 class ScopedContextSetter {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 This seems like kind of overkill considering you u
sail 2012/05/23 23:02:29 I agree that this is pretty verbose but I really l
Ami GONE FROM CHROMIUM 2012/05/24 21:30:29 "simplicity" I don't think that word means what yo
22 public:
23 ScopedContextSetter(CGLContextObj context)
24 : old_context_(NULL),
25 did_succeed_(false) {
26 old_context_ = CGLGetCurrentContext();
27 did_succeed_ = CGLSetCurrentContext(context) == kCGLNoError;
28 }
29
30 ~ScopedContextSetter() {
31 if (did_succeed_)
32 CGLSetCurrentContext(old_context_);
33 }
34
35 bool did_succeed() const {
36 return did_succeed_;
37 }
38
39 private:
40 CGLContextObj old_context_;
41 bool did_succeed_;
42 };
43
44 } // namespace
45
46 static bool BindImageToTexture(CGLContextObj context,
47 CVImageBufferRef image,
48 uint32 texture_id) {
49 ScopedContextSetter scoped_context_setter(context);
50 if (!scoped_context_setter.did_succeed())
51 return false;
52
53 IOSurfaceSupport* io_surface_support = IOSurfaceSupport::Initialize();
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Get & check this before Initialize returns true?
sail 2012/05/23 23:02:29 Done.
54 if (!io_surface_support)
55 return false;
56
57 CFTypeRef io_surface =
58 io_surface_support->CVPixelBufferGetIOSurface(image);
59 if (!io_surface)
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 When can this happen? Do you want to log somethin
sail 2012/05/23 23:02:29 Done. Added logging.
60 return false;
61
62 glEnable(GL_TEXTURE_RECTANGLE_ARB);
63 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id);
64 if (io_surface_support->CGLTexImageIOSurface2D(
65 context,
66 GL_TEXTURE_RECTANGLE_ARB,
67 GL_RGB,
68 io_surface_support->IOSurfaceGetWidth(io_surface),
69 io_surface_support->IOSurfaceGetHeight(io_surface),
70 GL_YCBCR_422_APPLE,
71 GL_UNSIGNED_SHORT_8_8_APPLE,
72 io_surface,
73 0) != kCGLNoError) {
74 return false;
75 }
76 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Why this line & the next?
sail 2012/05/23 23:02:29 Hm.. it looks like we don't normally clear this in
77 glDisable(GL_TEXTURE_RECTANGLE_ARB);
78 if (glGetError() != GL_NO_ERROR)
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 if (foo != bar) return false; return true; is m
sail 2012/05/23 23:02:29 Done.
79 return false;
80
81 return true;
82 }
83
84 MacVideoDecodeAccelerator::MacVideoDecodeAccelerator(
85 media::VideoDecodeAccelerator::Client* client)
86 : client_(client),
87 cgl_context_(NULL),
88 nalu_len_field_size_(0),
89 frame_width_(0),
90 frame_height_(0),
91 did_request_pictures_(false) {
92 }
93
94 void MacVideoDecodeAccelerator::SetGLContext(void* gl_context) {
95 cgl_context_ = static_cast<CGLContextObj>(gl_context);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Why not type the param explicitly and avoid the ca
sail 2012/05/23 23:02:29 gfx::GLContext::GetHandle() returns void*. It seem
Ami GONE FROM CHROMIUM 2012/05/24 21:30:29 I disagree b/c the call-site is not cross-platform
sail 2012/05/29 03:45:01 Done.
96 }
97
98 bool MacVideoDecodeAccelerator::SetConfigInfo(
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 This at least needs a TODO to support mid-stream f
sail 2012/05/23 23:02:29 I'm not sure if this config info is related to mid
99 uint32_t frame_width,
100 uint32_t frame_height,
101 const std::vector<uint8_t>& avc_data) {
102 frame_width_ = frame_width;
103 frame_height_ = frame_height;
104 nalu_len_field_size_ = (avc_data[4] & 0x03) + 1;
105
106 DCHECK(!vda_.get());
107 vda_ = new gfx::VideoDecodeAccelerationSupport();
108 gfx::VideoDecodeAccelerationSupport::Status status = vda_->Create(
109 frame_width_, frame_height_,
110 kCVPixelFormatType_422YpCbCr8, &avc_data.front(), avc_data.size());
111 if (status != gfx::VideoDecodeAccelerationSupport::VDA_SUCCESS)
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 That's a funny name for the enum given it's alread
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 again shorter/clearer as: return status == gfx::Vi
sail 2012/05/23 23:02:29 Done.
sail 2012/05/23 23:02:29 Done.
112 return false;
113 return true;
114 }
115
116 bool MacVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile) {
117 if (client_)
118 client_->NotifyInitializeDone();
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 IIRC this can't be done in-line b/c of reentrancy
sail 2012/05/23 23:02:29 Done.
119 return true;
120 }
121
122 void MacVideoDecodeAccelerator::Decode(
123 const media::BitstreamBuffer& bitstream_buffer) {
124 base::SharedMemory memory(bitstream_buffer.handle(), true);
125 if (!memory.Map(bitstream_buffer.size())) {
126 CHECK(false);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 This isn't very cool ;)
sail 2012/05/23 23:02:29 Done.
127 return;
128 }
129
130 size_t buffer_size = bitstream_buffer.size();
131 if (buffer_size < nalu_len_field_size_ + 1) {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Why so forgiving? Shouldn't this be an invalid-st
sail 2012/05/23 23:02:29 Done.
132 if (client_)
133 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id());
134 return;
135 }
136
137 // The decoder can only handle slice types (1-5).
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Drop the parens?
sail 2012/05/23 23:02:29 Done.
138 const uint8_t* buffer = static_cast<const uint8_t*>(memory.memory());
139 uint8_t nalu_type = buffer[nalu_len_field_size_] & 0x1f;
140 if (nalu_type < 1 || nalu_type > 5) {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 are you sure about these bounds? H264Parser claim
sail 2012/05/23 23:02:29 Yea, the bounds are correct. 0 is unspecified whic
141 if (client_)
142 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id());
143 return;
144 }
145
146 // Keep a ref counted copy of the buffer.
147 std::vector<uint8_t> vector(buffer, buffer + buffer_size);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 That's quite the variable name.... vbuffer?
sail 2012/05/23 23:02:29 Done.
148 scoped_refptr<base::RefCountedBytes> bytes(
149 base::RefCountedBytes::TakeVector(&vector));
150
151 // Store the buffer size at the beginning of the buffer as the decoder
152 // expects.
153 size_t frame_buffer_size = buffer_size - nalu_len_field_size_;
154 for (size_t i = 0; i < nalu_len_field_size_; ++i) {
155 size_t shift = nalu_len_field_size_ * 8 - (i + 1) * 8;
156 bytes->data()[i] = (frame_buffer_size >> shift) & 0xff;
157 }
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Do you want to have any kind of error checking?
sail 2012/05/23 23:02:29 Done.
158
159 vda_->Decode(bytes->front(), bytes->size(),
160 base::Bind(&MacVideoDecodeAccelerator::OnFrameReady, this,
161 bitstream_buffer.id(), bytes));
162
163 if (!did_request_pictures_) {
164 did_request_pictures_ = true;
165 if (client_)
166 client_->ProvidePictureBuffers(
167 kNumPictureBuffers, gfx::Size(frame_width_, frame_height_));
168 }
169 }
170
171 void MacVideoDecodeAccelerator::AssignPictureBuffers(
172 const std::vector<media::PictureBuffer>& buffers) {
173 available_pictures_.insert(
174 available_pictures_.end(), buffers.begin(), buffers.end());
175 SendImages();
176 }
177
178 void MacVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) {
179 std::map<int32, PendingPictureInfo>::iterator it =
180 pending_pictures_.find(picture_buffer_id);
181 DCHECK(it != pending_pictures_.end());
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Do you really want to crash the GPU process if a b
sail 2012/05/23 23:02:29 Done.
182 PendingPictureInfo info = it->second;
183 pending_pictures_.erase(it);
184 available_pictures_.push_back(info.picture_buffer);
185 SendImages();
186 }
187
188 void MacVideoDecodeAccelerator::Flush() {
189 vda_->Flush(true);
190 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
191 &MacVideoDecodeAccelerator::NotifyFlushDone, this));
192 }
193
194 void MacVideoDecodeAccelerator::Reset() {
195 vda_->Flush(false);
196 MessageLoop::current()->PostTask(FROM_HERE, base::Bind(
197 &MacVideoDecodeAccelerator::NotifyResetDone, this));
198 }
199
200 void MacVideoDecodeAccelerator::Destroy() {
201 vda_->Destroy();
202 vda_ = NULL;
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 Don't all the other entry points want to check for
sail 2012/05/23 23:02:29 Done.
203 client_ = NULL;
204 }
205
206 MacVideoDecodeAccelerator::~MacVideoDecodeAccelerator() {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 move to below ctor call Destroy()?
sail 2012/05/23 23:02:29 I'd rather leave it here to match the declaration
207 }
208
209 void MacVideoDecodeAccelerator::OnFrameReady(
210 int32 bitstream_buffer_id,
211 scoped_refptr<base::RefCountedBytes> bytes,
212 CVImageBufferRef image,
213 int status) {
214 if (image) {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 how can this fail?
sail 2012/05/23 23:02:29 The decoder can send a NULL image if it failed to
215 DecodedImageInfo info;
216 info.image.reset(image, base::mac::RETAIN);
217 info.bitstream_buffer_id = bitstream_buffer_id;
218 decoded_images_.push_back(info);
219 SendImages();
220 }
221 if (client_)
222 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer_id);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 This is wrong if multiple frames are encoded in a
sail 2012/05/23 23:02:29 The Mac API can only handle a single NALU at a tim
223 }
224
225 void MacVideoDecodeAccelerator::SendImages() {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 This sends at most one image, so the name is misle
sail 2012/05/23 23:02:29 Done.
226 if (available_pictures_.size() == 0 || decoded_images_.size() == 0)
227 return;
228
229 DecodedImageInfo info = decoded_images_.front();
230 decoded_images_.pop_front();
231 media::PictureBuffer picture_buffer = available_pictures_.front();
232
233 if (!BindImageToTexture(cgl_context_, info.image,
234 picture_buffer.texture_id())) {
235 LOG(ERROR) << "Error binding image to texture.";
236 return;
237 }
238
239 available_pictures_.pop_front();
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 move this up to right below the .front() call?
sail 2012/05/23 23:02:29 Done.
240 PendingPictureInfo pending_info(picture_buffer);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 why use the intermediate?
sail 2012/05/23 23:02:29 Done.
241 pending_info.image = info.image;
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 why not pass both buffer & image to the PPI ctor?
sail 2012/05/23 23:02:29 Done.
242 pending_pictures_.insert(std::pair<int, PendingPictureInfo>(
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 std::make_pair lets you drop the template params.
sail 2012/05/23 23:02:29 The Mac compiler doesn't seem to allow this.
243 picture_buffer.id(), pending_info));
244 media::Picture picture(picture_buffer.id(), info.bitstream_buffer_id);
245 if (client_)
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 If !client_ early-return at the top of the functio
sail 2012/05/23 23:02:29 Done.
246 client_->PictureReady(picture);
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 can construct this inline: client_->PictureReady(m
sail 2012/05/23 23:02:29 Done.
247 }
248
249 void MacVideoDecodeAccelerator::NotifyFlushDone() {
250 if (client_)
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 assert decoded_images_ is empty?
sail 2012/05/23 23:02:29 Hm... it's possible that there are no available im
251 client_->NotifyFlushDone();
252 }
253
254 void MacVideoDecodeAccelerator::NotifyResetDone() {
255 decoded_images_.clear();
256 if (client_)
257 client_->NotifyResetDone();
258 }
259
260 MacVideoDecodeAccelerator::PendingPictureInfo::PendingPictureInfo(
261 media::PictureBuffer pic)
262 : picture_buffer(pic.id(), pic.size(), pic.texture_id()) {
Ami GONE FROM CHROMIUM 2012/05/17 21:44:47 what's wrong w/ the copy ctor?
sail 2012/05/23 23:02:29 Done.
263 }
264
265 MacVideoDecodeAccelerator::PendingPictureInfo::~PendingPictureInfo() {
266 }
267
268 MacVideoDecodeAccelerator::DecodedImageInfo::DecodedImageInfo() {
269 }
270
271 MacVideoDecodeAccelerator::DecodedImageInfo::~DecodedImageInfo() {
272 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698