OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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/vaapi_jpeg_decode_accelerator.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/logging.h" |
| 9 #include "base/metrics/histogram.h" |
| 10 #include "base/stl_util.h" |
| 11 #include "base/strings/string_util.h" |
| 12 #include "base/synchronization/waitable_event.h" |
| 13 #include "content/common/gpu/gpu_channel.h" |
| 14 #include "content/common/gpu/media/vaapi_picture.h" |
| 15 #include "media/base/bind_to_current_loop.h" |
| 16 #include "media/base/video_frame.h" |
| 17 #include "media/filters/jpeg_parser.h" |
| 18 #include "media/video/picture.h" |
| 19 #include "ui/gl/gl_bindings.h" |
| 20 |
| 21 static void ReportVaapiError() { |
| 22 } |
| 23 |
| 24 namespace content { |
| 25 |
| 26 #define RETURN_AND_NOTIFY_ON_FAILURE(result, log, buffer_id, error_code, ret) \ |
| 27 do { \ |
| 28 if (!(result)) { \ |
| 29 LOG(ERROR) << log; \ |
| 30 NotifyError(buffer_id, error_code); \ |
| 31 return ret; \ |
| 32 } \ |
| 33 } while (0) |
| 34 |
| 35 VaapiJpegDecodeAccelerator::InputBuffer::InputBuffer() : id(0), size(0) { |
| 36 } |
| 37 |
| 38 VaapiJpegDecodeAccelerator::InputBuffer::~InputBuffer() { |
| 39 } |
| 40 |
| 41 void VaapiJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, |
| 42 Error error) { |
| 43 if (message_loop_ != base::MessageLoop::current()) { |
| 44 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 45 message_loop_->PostTask(FROM_HERE, |
| 46 base::Bind(&VaapiJpegDecodeAccelerator::NotifyError, |
| 47 weak_this_, |
| 48 bitstream_buffer_id, |
| 49 error)); |
| 50 return; |
| 51 } |
| 52 |
| 53 // Post Cleanup() as a task so we don't recursively acquire lock_. |
| 54 message_loop_->PostTask(FROM_HERE, base::Bind( |
| 55 &VaapiJpegDecodeAccelerator::Cleanup, weak_this_)); |
| 56 |
| 57 LOG(ERROR) << "Notifying of error " << error; |
| 58 if (client_) { |
| 59 client_->NotifyError(bitstream_buffer_id, error); |
| 60 client_ptr_factory_.reset(); |
| 61 } |
| 62 } |
| 63 |
| 64 VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator() |
| 65 : state_(kUninitialized), |
| 66 input_ready_(&lock_), |
| 67 message_loop_(base::MessageLoop::current()), |
| 68 decoder_thread_("VaapiDecoderThread"), |
| 69 weak_this_factory_(this) { |
| 70 weak_this_ = weak_this_factory_.GetWeakPtr(); |
| 71 } |
| 72 |
| 73 VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() { |
| 74 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 75 } |
| 76 |
| 77 bool VaapiJpegDecodeAccelerator::Initialize(Client* client) { |
| 78 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 79 |
| 80 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client)); |
| 81 client_ = client_ptr_factory_->GetWeakPtr(); |
| 82 |
| 83 base::AutoLock auto_lock(lock_); |
| 84 DCHECK_EQ(state_, kUninitialized); |
| 85 |
| 86 vaapi_wrapper_ = VaapiWrapper::Create( |
| 87 VaapiWrapper::kDecode, VAProfileJPEGBaseline, |
| 88 base::Bind(&ReportVaapiError)); |
| 89 |
| 90 if (!vaapi_wrapper_.get()) { |
| 91 LOG(ERROR) << "Failed initializing VAAPI"; |
| 92 return false; |
| 93 } |
| 94 |
| 95 CHECK(decoder_thread_.Start()); |
| 96 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy(); |
| 97 |
| 98 state_ = kIdle; |
| 99 return true; |
| 100 } |
| 101 |
| 102 bool VaapiJpegDecodeAccelerator::OutputPicture( |
| 103 VASurfaceID va_surface_id, |
| 104 int32_t input_id, |
| 105 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 106 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 107 |
| 108 DVLOG(3) << "Outputting VASurface " << va_surface_id |
| 109 << " into video_frame associate to input buffer id " << input_id; |
| 110 |
| 111 VAImage image; |
| 112 VAImageFormat format; |
| 113 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0'); |
| 114 memset(&image, 0, sizeof(image)); |
| 115 memset(&format, 0, sizeof(format)); |
| 116 format.fourcc = kI420Fourcc; |
| 117 format.byte_order = VA_LSB_FIRST; |
| 118 format.bits_per_pixel = 12; // 12 for I420 |
| 119 |
| 120 void* mem; |
| 121 gfx::Size coded_size = video_frame->coded_size(); |
| 122 if (!vaapi_wrapper_->GetVaImage( |
| 123 va_surface_id, &format, coded_size, &image, &mem)) { |
| 124 LOG(ERROR) << "Cannot get VAImage"; |
| 125 return false; |
| 126 } |
| 127 |
| 128 uint8* frame_mem = video_frame->data(media::VideoFrame::kYPlane); |
| 129 size_t frame_buffer_size = |
| 130 media::VideoFrame::AllocationSize(media::VideoFrame::I420, coded_size); |
| 131 memcpy(frame_mem, mem, frame_buffer_size); |
| 132 |
| 133 vaapi_wrapper_->ReturnVaImage(&image); |
| 134 |
| 135 if (client_) |
| 136 client_->VideoFrameReady(input_id); |
| 137 |
| 138 return true; |
| 139 } |
| 140 |
| 141 void VaapiJpegDecodeAccelerator::MapAndQueueNewInputBuffer( |
| 142 const media::BitstreamBuffer& bitstream_buffer, |
| 143 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 144 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id() |
| 145 << " size: " << (int)bitstream_buffer.size(); |
| 146 |
| 147 scoped_ptr<base::SharedMemory> shm( |
| 148 new base::SharedMemory(bitstream_buffer.handle(), true)); |
| 149 RETURN_AND_NOTIFY_ON_FAILURE(shm->Map(bitstream_buffer.size()), |
| 150 "Failed to map input buffer", |
| 151 bitstream_buffer.id(), |
| 152 UNREADABLE_INPUT,); |
| 153 |
| 154 base::AutoLock auto_lock(lock_); |
| 155 |
| 156 // Set up a new input buffer and queue it for later. |
| 157 linked_ptr<InputBuffer> input_buffer(new InputBuffer()); |
| 158 input_buffer->shm.reset(shm.release()); |
| 159 input_buffer->id = bitstream_buffer.id(); |
| 160 input_buffer->size = bitstream_buffer.size(); |
| 161 input_buffer->video_frame = video_frame; |
| 162 |
| 163 input_buffers_.push(input_buffer); |
| 164 input_ready_.Signal(); |
| 165 } |
| 166 |
| 167 void VaapiJpegDecodeAccelerator::DecodeTask() { |
| 168 DVLOG(3) << __func__; |
| 169 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 170 base::AutoLock auto_lock(lock_); |
| 171 |
| 172 if (state_ != kIdle) { |
| 173 LOG(ERROR) << "not idle state"; |
| 174 return; |
| 175 } |
| 176 |
| 177 state_ = kDecoding; |
| 178 |
| 179 DCHECK(!input_buffers_.empty()); |
| 180 curr_input_buffer_ = input_buffers_.front(); |
| 181 input_buffers_.pop(); |
| 182 DVLOG(4) << "New current bitstream buffer, id: " |
| 183 << curr_input_buffer_->id |
| 184 << " size: " << curr_input_buffer_->size; |
| 185 |
| 186 do { |
| 187 base::AutoUnlock auto_unlock(lock_); |
| 188 |
| 189 media::JpegParseResult parse_result; |
| 190 |
| 191 if (!media::ParseJpegPicture( |
| 192 reinterpret_cast<const uint8_t*>(curr_input_buffer_->shm->memory()), |
| 193 curr_input_buffer_->size, |
| 194 &parse_result)) { |
| 195 NotifyError(curr_input_buffer_->id, |
| 196 media::JpegDecodeAccelerator::PARSE_JPEG_FAILED); |
| 197 break; |
| 198 } |
| 199 |
| 200 gfx::Size coded_size(parse_result.frame_header.coded_width, |
| 201 parse_result.frame_header.coded_height); |
| 202 |
| 203 vaapi_wrapper_->DestroySurfaces(); // XXX assume one frame at a time |
| 204 std::vector<VASurfaceID> va_surfaces; |
| 205 if (!vaapi_wrapper_->CreateSurfaces(coded_size, 1, &va_surfaces)) |
| 206 break; |
| 207 |
| 208 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result, |
| 209 va_surfaces[0])) { |
| 210 // TODO UNSUPPORTED_JPEG ? |
| 211 DLOG(ERROR) << "Decode failed"; |
| 212 NotifyError(curr_input_buffer_->id, |
| 213 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 214 break; |
| 215 } |
| 216 |
| 217 if (!OutputPicture(va_surfaces[0], curr_input_buffer_->id, |
| 218 curr_input_buffer_->video_frame)) { |
| 219 DLOG(ERROR) << "Output failed"; |
| 220 NotifyError(curr_input_buffer_->id, |
| 221 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 222 break; |
| 223 } |
| 224 vaapi_wrapper_->DestroySurfaces(); |
| 225 } while (0); |
| 226 curr_input_buffer_.reset(); |
| 227 |
| 228 state_ = kIdle; |
| 229 } |
| 230 |
| 231 void VaapiJpegDecodeAccelerator::Decode( |
| 232 const media::BitstreamBuffer& bitstream_buffer, |
| 233 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 234 DVLOG(3) << __func__; |
| 235 // We got a new input buffer from the client, map it and queue for later use. |
| 236 MapAndQueueNewInputBuffer(bitstream_buffer, video_frame); |
| 237 |
| 238 base::AutoLock auto_lock(lock_); |
| 239 decoder_thread_proxy_->PostTask(FROM_HERE, base::Bind( |
| 240 &VaapiJpegDecodeAccelerator::DecodeTask, |
| 241 base::Unretained(this))); |
| 242 } |
| 243 |
| 244 void VaapiJpegDecodeAccelerator::Cleanup() { |
| 245 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 246 |
| 247 base::AutoLock auto_lock(lock_); |
| 248 if (state_ == kUninitialized || state_ == kDestroying) |
| 249 return; |
| 250 |
| 251 DVLOG(1) << "Destroying VAJDA"; |
| 252 state_ = kDestroying; |
| 253 |
| 254 client_ptr_factory_.reset(); |
| 255 weak_this_factory_.InvalidateWeakPtrs(); |
| 256 |
| 257 // Signal all potential waiters on the decoder_thread_, let them early-exit, |
| 258 // as we've just moved to the kDestroying state, and wait for all tasks |
| 259 // to finish. |
| 260 input_ready_.Signal(); |
| 261 { |
| 262 base::AutoUnlock auto_unlock(lock_); |
| 263 decoder_thread_.Stop(); |
| 264 } |
| 265 |
| 266 state_ = kUninitialized; |
| 267 } |
| 268 |
| 269 void VaapiJpegDecodeAccelerator::Destroy() { |
| 270 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 271 Cleanup(); |
| 272 delete this; |
| 273 } |
| 274 |
| 275 } // namespace content |
OLD | NEW |