Chromium Code Reviews| 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 "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 { | |
| 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 DLOG(ERROR) << "Unable to set OpenGL context."; | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
s/DLOG(ERROR)/DVLOG(1)/
here and everywhere below
sail
2012/05/29 03:45:01
Done.
| |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 IOSurfaceSupport* io_surface_support = IOSurfaceSupport::Initialize(); | |
| 56 DCHECK(io_surface_support); | |
| 57 | |
| 58 CFTypeRef io_surface = | |
| 59 io_surface_support->CVPixelBufferGetIOSurface(image); | |
| 60 if (!io_surface) { | |
| 61 DLOG(ERROR) << "Unable to get IOSurface for CVPixelBuffer."; | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
I'm afraid I wasn't clear before. The problem wit
sail
2012/05/29 03:45:01
Done.
To mark the MacVDA instance unusable I'm ju
sail
2012/05/29 23:22:57
Actually, I changed this to check for a client_ in
| |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 glEnable(GL_TEXTURE_RECTANGLE_ARB); | |
| 66 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id); | |
| 67 if (io_surface_support->CGLTexImageIOSurface2D( | |
| 68 context, | |
| 69 GL_TEXTURE_RECTANGLE_ARB, | |
| 70 GL_RGB, | |
| 71 io_surface_support->IOSurfaceGetWidth(io_surface), | |
| 72 io_surface_support->IOSurfaceGetHeight(io_surface), | |
| 73 GL_YCBCR_422_APPLE, | |
| 74 GL_UNSIGNED_SHORT_8_8_APPLE, | |
| 75 io_surface, | |
| 76 0) != kCGLNoError) { | |
| 77 DLOG(ERROR) << "Failed to bind image to texture."; | |
| 78 return false; | |
| 79 } | |
| 80 return glGetError() == GL_NO_ERROR; | |
| 81 } | |
| 82 | |
| 83 MacVideoDecodeAccelerator::MacVideoDecodeAccelerator( | |
| 84 media::VideoDecodeAccelerator::Client* client) | |
| 85 : client_(client), | |
| 86 cgl_context_(NULL), | |
| 87 nalu_len_field_size_(0), | |
| 88 did_request_pictures_(false) { | |
| 89 } | |
| 90 | |
| 91 void MacVideoDecodeAccelerator::SetGLContext(void* gl_context) { | |
| 92 DCHECK(CalledOnValidThread()); | |
| 93 cgl_context_ = static_cast<CGLContextObj>(gl_context); | |
| 94 } | |
| 95 | |
| 96 bool MacVideoDecodeAccelerator::SetConfigInfo( | |
| 97 uint32_t frame_width, | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
This config info includes width & height, and the
sail
2012/05/29 03:45:01
So taking a step back.
All the Mac video API clie
Ami GONE FROM CHROMIUM
2012/05/30 00:29:58
Presumably you need to request different textures,
sail
2012/05/30 20:13:46
Sounds good. I've filed bug 130352 to track this.
| |
| 98 uint32_t frame_height, | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
Is your plan to land this first and then immediate
sail
2012/05/29 03:45:01
I'm planning to land the various CLs one after ano
| |
| 99 const std::vector<uint8_t>& avc_data) { | |
| 100 DCHECK(CalledOnValidThread()); | |
| 101 frame_size_ = gfx::Size(frame_width, frame_height); | |
| 102 nalu_len_field_size_ = (avc_data[4] & 0x03) + 1; | |
| 103 | |
| 104 DCHECK(!vda_support_.get()); | |
| 105 vda_support_ = new gfx::VideoDecodeAccelerationSupport(); | |
| 106 return vda_support_->Create(frame_size_.width(), frame_size_.height(), | |
| 107 kCVPixelFormatType_422YpCbCr8, &avc_data.front(), avc_data.size()) == | |
| 108 gfx::VideoDecodeAccelerationSupport::SUCCESS; | |
| 109 } | |
| 110 | |
| 111 bool MacVideoDecodeAccelerator::Initialize(media::VideoCodecProfile profile) { | |
| 112 DCHECK(CalledOnValidThread()); | |
| 113 | |
| 114 IOSurfaceSupport* io_surface_support = IOSurfaceSupport::Initialize(); | |
| 115 if (!io_surface_support) | |
| 116 return false; | |
| 117 | |
| 118 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 119 &MacVideoDecodeAccelerator::NotifyInitializeDone, this)); | |
| 120 return true; | |
| 121 } | |
| 122 | |
| 123 void MacVideoDecodeAccelerator::Decode( | |
| 124 const media::BitstreamBuffer& bitstream_buffer) { | |
| 125 DCHECK(CalledOnValidThread()); | |
| 126 base::SharedMemory memory(bitstream_buffer.handle(), true); | |
| 127 if (!memory.Map(bitstream_buffer.size())) { | |
| 128 LOG(ERROR) << "Failed to SharedMemory::Map()."; | |
| 129 if (client_) | |
| 130 client_->NotifyError(UNREADABLE_INPUT); | |
| 131 return; | |
| 132 } | |
| 133 | |
| 134 size_t buffer_size = bitstream_buffer.size(); | |
| 135 if (buffer_size < nalu_len_field_size_ + 1) { | |
| 136 LOG(ERROR) << "Bitstream contains invalid data."; | |
| 137 if (client_) | |
| 138 client_->NotifyError(INVALID_ARGUMENT); | |
| 139 return; | |
| 140 } | |
| 141 | |
| 142 // The decoder can only handle slice types 1-5. | |
| 143 const uint8_t* buffer = static_cast<const uint8_t*>(memory.memory()); | |
| 144 uint8_t nalu_type = buffer[nalu_len_field_size_] & 0x1f; | |
| 145 if (nalu_type < 1 || nalu_type > 5) { | |
| 146 if (client_) | |
| 147 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id()); | |
| 148 return; | |
| 149 } | |
| 150 | |
| 151 // Keep a ref counted copy of the buffer. | |
| 152 std::vector<uint8_t> vbuffer(buffer, buffer + buffer_size); | |
| 153 scoped_refptr<base::RefCountedBytes> bytes( | |
| 154 base::RefCountedBytes::TakeVector(&vbuffer)); | |
| 155 | |
| 156 // Store the buffer size at the beginning of the buffer as the decoder | |
| 157 // expects. | |
| 158 size_t frame_buffer_size = buffer_size - nalu_len_field_size_; | |
| 159 DCHECK(nalu_len_field_size_ <= 4); | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
DCHECK_LE
sail
2012/05/29 03:45:01
Done.
| |
| 160 uint64_t max_frame_buffer_size = (1llu << (nalu_len_field_size_ * 8)) - 1; | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
s/uint64/int64/
s/llu/LL/
sail
2012/05/29 03:45:01
Done.
| |
| 161 if (frame_buffer_size > max_frame_buffer_size) { | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
FWIW, writing this as
if (frame_buffer_size >> (n
sail
2012/05/29 03:45:01
Unfortunately that overflows when nalu_len_field_s
Ami GONE FROM CHROMIUM
2012/05/30 00:29:58
WDYM "overflows"?
sail
2012/05/30 20:13:46
Sorry, not sure what the correct term is. If I do:
| |
| 162 LOG(ERROR) << "Bitstream is too large."; | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
s/is/buffer is/
sail
2012/05/29 03:45:01
Done.
| |
| 163 if (client_) | |
| 164 client_->NotifyError(INVALID_ARGUMENT); | |
| 165 return; | |
| 166 } | |
| 167 for (size_t i = 0; i < nalu_len_field_size_; ++i) { | |
| 168 size_t shift = nalu_len_field_size_ * 8 - (i + 1) * 8; | |
| 169 bytes->data()[i] = (frame_buffer_size >> shift) & 0xff; | |
| 170 } | |
| 171 | |
| 172 if (vda_support_) { | |
| 173 vda_support_->Decode(bytes->front(), bytes->size(), | |
| 174 base::Bind(&MacVideoDecodeAccelerator::OnFrameReady, | |
| 175 this, bitstream_buffer.id(), bytes)); | |
| 176 } | |
| 177 | |
| 178 if (!did_request_pictures_) { | |
| 179 did_request_pictures_ = true; | |
| 180 if (client_) | |
| 181 client_->ProvidePictureBuffers(kNumPictureBuffers, frame_size_); | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
Again needs to be out-of-line to avoid re-entrancy
sail
2012/05/29 03:45:01
Done.
| |
| 182 } | |
| 183 } | |
| 184 | |
| 185 void MacVideoDecodeAccelerator::AssignPictureBuffers( | |
| 186 const std::vector<media::PictureBuffer>& buffers) { | |
| 187 DCHECK(CalledOnValidThread()); | |
| 188 available_pictures_.insert( | |
| 189 available_pictures_.end(), buffers.begin(), buffers.end()); | |
| 190 SendImages(); | |
| 191 } | |
| 192 | |
| 193 void MacVideoDecodeAccelerator::ReusePictureBuffer(int32 picture_buffer_id) { | |
| 194 DCHECK(CalledOnValidThread()); | |
| 195 std::map<int32, UsedPictureInfo>::iterator it = | |
| 196 used_pictures_.find(picture_buffer_id); | |
| 197 if (it == used_pictures_.end()) { | |
| 198 LOG(ERROR) << "Missing picture buffer id: " << picture_buffer_id; | |
| 199 if (client_) | |
| 200 client_->NotifyError(INVALID_ARGUMENT); | |
| 201 return; | |
| 202 } | |
| 203 UsedPictureInfo info = it->second; | |
| 204 used_pictures_.erase(it); | |
| 205 available_pictures_.push_back(info.picture_buffer); | |
| 206 SendImages(); | |
| 207 } | |
| 208 | |
| 209 void MacVideoDecodeAccelerator::Flush() { | |
| 210 DCHECK(CalledOnValidThread()); | |
| 211 if (vda_support_) | |
| 212 vda_support_->Flush(true); | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
I'm surprised this can be done synchronously.
In
sail
2012/05/29 03:45:01
Even though the flush is synchronous the decoded i
| |
| 213 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 214 &MacVideoDecodeAccelerator::NotifyFlushDone, this)); | |
| 215 } | |
| 216 | |
| 217 void MacVideoDecodeAccelerator::Reset() { | |
| 218 DCHECK(CalledOnValidThread()); | |
| 219 if (vda_support_) | |
| 220 vda_support_->Flush(false); | |
| 221 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 222 &MacVideoDecodeAccelerator::NotifyResetDone, this)); | |
| 223 } | |
| 224 | |
| 225 void MacVideoDecodeAccelerator::Destroy() { | |
| 226 DCHECK(CalledOnValidThread()); | |
| 227 if (vda_support_) { | |
| 228 vda_support_->Destroy(); | |
| 229 vda_support_ = NULL; | |
| 230 } | |
| 231 client_ = NULL; | |
| 232 } | |
| 233 | |
| 234 MacVideoDecodeAccelerator::~MacVideoDecodeAccelerator() { | |
| 235 DCHECK(CalledOnValidThread()); | |
| 236 Destroy(); | |
| 237 } | |
| 238 | |
| 239 void MacVideoDecodeAccelerator::OnFrameReady( | |
| 240 int32 bitstream_buffer_id, | |
| 241 scoped_refptr<base::RefCountedBytes> bytes, | |
| 242 CVImageBufferRef image, | |
| 243 int status) { | |
| 244 DCHECK(CalledOnValidThread()); | |
| 245 if (image) { | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
Is this failing not a cause for notifying the clie
sail
2012/05/29 03:45:01
The image can be NULL if Reset() is called.
I adde
| |
| 246 DecodedImageInfo info; | |
| 247 info.image.reset(image, base::mac::RETAIN); | |
| 248 info.bitstream_buffer_id = bitstream_buffer_id; | |
| 249 decoded_images_.push_back(info); | |
| 250 SendImages(); | |
| 251 } | |
| 252 if (client_) | |
| 253 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer_id); | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
Add
// TODO(sail): this assumes Decode() is handed
sail
2012/05/29 03:45:01
Done.
| |
| 254 } | |
| 255 | |
| 256 void MacVideoDecodeAccelerator::SendImages() { | |
| 257 if (!client_) | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
In this case do you not want to clear the queues?
sail
2012/05/29 03:45:01
I added code to clear the decoded image queue in D
| |
| 258 return; | |
| 259 | |
| 260 while (available_pictures_.size() && decoded_images_.size()) { | |
| 261 DecodedImageInfo info = decoded_images_.front(); | |
| 262 decoded_images_.pop_front(); | |
| 263 media::PictureBuffer picture_buffer = available_pictures_.front(); | |
| 264 available_pictures_.pop_front(); | |
| 265 | |
| 266 if (!BindImageToTexture(cgl_context_, info.image, | |
| 267 picture_buffer.texture_id())) { | |
| 268 LOG(ERROR) << "Error binding image to texture."; | |
| 269 client_->NotifyError(PLATFORM_FAILURE); | |
| 270 return; | |
| 271 } | |
| 272 | |
| 273 used_pictures_.insert(std::pair<int, UsedPictureInfo>( | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
what's the problem if you replace
std::pair<int, U
sail
2012/05/29 03:45:01
Done.
| |
| 274 picture_buffer.id(), UsedPictureInfo(picture_buffer, info.image))); | |
| 275 client_->PictureReady( | |
| 276 media::Picture(picture_buffer.id(), info.bitstream_buffer_id)); | |
| 277 } | |
| 278 } | |
| 279 | |
| 280 void MacVideoDecodeAccelerator::NotifyInitializeDone() { | |
| 281 if (client_) | |
| 282 client_->NotifyInitializeDone(); | |
| 283 } | |
| 284 | |
| 285 void MacVideoDecodeAccelerator::NotifyFlushDone() { | |
| 286 if (client_) | |
| 287 client_->NotifyFlushDone(); | |
|
Ami GONE FROM CHROMIUM
2012/05/24 21:30:30
In that case in what sense is the flush "done"???
sail
2012/05/29 03:45:01
So it looks like there are a few solutions:
#1
Ami GONE FROM CHROMIUM
2012/05/30 00:29:58
This is the only option that's compatible with the
sail
2012/05/30 20:13:46
Sounds good. I've filed bug 130357 to track this w
| |
| 288 } | |
| 289 | |
| 290 void MacVideoDecodeAccelerator::NotifyResetDone() { | |
| 291 decoded_images_.clear(); | |
| 292 if (client_) | |
| 293 client_->NotifyResetDone(); | |
| 294 } | |
| 295 | |
| 296 MacVideoDecodeAccelerator::UsedPictureInfo::UsedPictureInfo( | |
| 297 const media::PictureBuffer& pic, | |
| 298 const base::mac::ScopedCFTypeRef<CVImageBufferRef>& image) | |
| 299 : picture_buffer(pic), | |
| 300 image(image, base::mac::RETAIN) { | |
| 301 } | |
| 302 | |
| 303 MacVideoDecodeAccelerator::UsedPictureInfo::~UsedPictureInfo() { | |
| 304 } | |
| 305 | |
| 306 MacVideoDecodeAccelerator::DecodedImageInfo::DecodedImageInfo() { | |
| 307 } | |
| 308 | |
| 309 MacVideoDecodeAccelerator::DecodedImageInfo::~DecodedImageInfo() { | |
| 310 } | |
| OLD | NEW |