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 | |
|
Ami GONE FROM CHROMIUM
2012/05/15 17:55:22
I don't know anything about Objective-C so I'm goi
sail
2012/05/15 23:53:31
It would be great if you could look at the way I'm
| |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "content/common/gpu/media/video_decode_accelerator_mac.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 bool BindImageToTexture(CGLContextObj context, | |
| 45 CVImageBufferRef image, | |
| 46 uint32 texture_id) { | |
| 47 ScopedContextSetter scoped_context_setter(context); | |
| 48 if (!scoped_context_setter.did_succeed()) | |
| 49 return false; | |
| 50 | |
| 51 IOSurfaceSupport* io_surface_support = IOSurfaceSupport::Initialize(); | |
| 52 if (!io_surface_support) | |
| 53 return false; | |
| 54 | |
| 55 CFTypeRef io_surface = | |
| 56 io_surface_support->CVPixelBufferGetIOSurface(image); | |
| 57 if (!io_surface) | |
| 58 return false; | |
| 59 | |
| 60 glEnable(GL_TEXTURE_RECTANGLE_ARB); | |
| 61 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, texture_id); | |
| 62 if (io_surface_support->CGLTexImageIOSurface2D( | |
| 63 context, | |
| 64 GL_TEXTURE_RECTANGLE_ARB, | |
| 65 GL_RGB, | |
| 66 io_surface_support->IOSurfaceGetWidth(io_surface), | |
| 67 io_surface_support->IOSurfaceGetHeight(io_surface), | |
| 68 GL_YCBCR_422_APPLE, | |
| 69 GL_UNSIGNED_SHORT_8_8_APPLE, | |
| 70 io_surface, | |
| 71 0) != kCGLNoError) { | |
| 72 return false; | |
| 73 } | |
| 74 glBindTexture(GL_TEXTURE_RECTANGLE_ARB, 0); | |
| 75 glDisable(GL_TEXTURE_RECTANGLE_ARB); | |
| 76 if (glGetError() != GL_NO_ERROR) | |
| 77 return false; | |
| 78 | |
| 79 return true; | |
| 80 } | |
| 81 | |
| 82 } // namespace | |
| 83 | |
| 84 VideoDecodeAcceleratorMac::VideoDecodeAcceleratorMac( | |
| 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 VideoDecodeAcceleratorMac::SetGLContext(void* gl_context) { | |
| 95 cgl_context_ = static_cast<CGLContextObj>(gl_context); | |
| 96 } | |
| 97 | |
| 98 bool VideoDecodeAcceleratorMac::SetConfigInfo( | |
| 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) | |
| 112 return false; | |
| 113 return true; | |
| 114 } | |
| 115 | |
| 116 bool VideoDecodeAcceleratorMac::Initialize(media::VideoCodecProfile profile) { | |
| 117 if (client_) | |
| 118 client_->NotifyInitializeDone(); | |
| 119 return true; | |
| 120 } | |
| 121 | |
| 122 void VideoDecodeAcceleratorMac::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); | |
| 127 return; | |
| 128 } | |
| 129 | |
| 130 size_t buffer_size = bitstream_buffer.size(); | |
| 131 if (buffer_size < nalu_len_field_size_ + 1) { | |
| 132 if (client_) | |
| 133 client_->NotifyEndOfBitstreamBuffer(bitstream_buffer.id()); | |
| 134 return; | |
| 135 } | |
| 136 | |
| 137 // The decoder can only handle slice types (1-5). | |
| 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) { | |
| 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); | |
| 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 } | |
| 158 | |
| 159 vda_->Decode(bytes->front(), bytes->size(), | |
| 160 base::Bind(&VideoDecodeAcceleratorMac::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 VideoDecodeAcceleratorMac::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 VideoDecodeAcceleratorMac::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()); | |
| 182 PendingPictureInfo info = it->second; | |
| 183 pending_pictures_.erase(it); | |
| 184 available_pictures_.push_back(info.picture_buffer); | |
| 185 SendImages(); | |
| 186 } | |
| 187 | |
| 188 void VideoDecodeAcceleratorMac::Flush() { | |
| 189 vda_->Flush(true); | |
| 190 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 191 &VideoDecodeAcceleratorMac::NotifyFlushDone, this)); | |
| 192 } | |
| 193 | |
| 194 void VideoDecodeAcceleratorMac::Reset() { | |
| 195 vda_->Flush(false); | |
| 196 MessageLoop::current()->PostTask(FROM_HERE, base::Bind( | |
| 197 &VideoDecodeAcceleratorMac::NotifyResetDone, this)); | |
| 198 } | |
| 199 | |
| 200 void VideoDecodeAcceleratorMac::Destroy() { | |
| 201 vda_->Destroy(); | |
| 202 vda_ = NULL; | |
| 203 client_ = NULL; | |
| 204 } | |
| 205 | |
| 206 VideoDecodeAcceleratorMac::~VideoDecodeAcceleratorMac() { | |
| 207 } | |
| 208 | |
| 209 void VideoDecodeAcceleratorMac::OnFrameReady( | |
| 210 int32 bitstream_buffer_id, | |
| 211 scoped_refptr<base::RefCountedBytes> bytes, | |
| 212 CVImageBufferRef image, | |
| 213 int status) { | |
| 214 if (image) { | |
| 215 DecodedImageInfo info; | |
| 216 info.image = image; | |
| 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); | |
| 223 } | |
| 224 | |
| 225 void VideoDecodeAcceleratorMac::SendImages() { | |
| 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(); | |
| 240 PendingPictureInfo pending_info(picture_buffer, info.image); | |
| 241 pending_pictures_.insert(std::pair<int, PendingPictureInfo>( | |
| 242 picture_buffer.id(), pending_info)); | |
| 243 media::Picture picture(picture_buffer.id(), info.bitstream_buffer_id); | |
| 244 if (client_) | |
| 245 client_->PictureReady(picture); | |
| 246 } | |
| 247 | |
| 248 void VideoDecodeAcceleratorMac::NotifyFlushDone() { | |
| 249 if (client_) | |
| 250 client_->NotifyFlushDone(); | |
| 251 } | |
| 252 | |
| 253 void VideoDecodeAcceleratorMac::NotifyResetDone() { | |
| 254 decoded_images_.clear(); | |
| 255 if (client_) | |
| 256 client_->NotifyResetDone(); | |
| 257 } | |
| 258 | |
| 259 VideoDecodeAcceleratorMac::PendingPictureInfo::PendingPictureInfo( | |
| 260 media::PictureBuffer pic, | |
| 261 CVImageBufferRef image) | |
| 262 : picture_buffer(pic.id(), pic.size(), pic.texture_id()), | |
| 263 image(image) { | |
| 264 } | |
| 265 | |
| 266 VideoDecodeAcceleratorMac::PendingPictureInfo::~PendingPictureInfo() { | |
| 267 } | |
| 268 | |
| 269 VideoDecodeAcceleratorMac::DecodedImageInfo::DecodedImageInfo() { | |
| 270 } | |
| 271 | |
| 272 VideoDecodeAcceleratorMac::DecodedImageInfo::~DecodedImageInfo() { | |
| 273 } | |
| OLD | NEW |