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/trace_event/trace_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 VaapiJpegDecodeAccelerator::DecodeRequest::DecodeRequest( |
| 27 media::BitstreamBuffer bitstream_buffer, |
| 28 scoped_refptr<media::VideoFrame> video_frame) |
| 29 : bitstream_buffer(bitstream_buffer), video_frame(video_frame) { |
| 30 } |
| 31 |
| 32 VaapiJpegDecodeAccelerator::DecodeRequest::~DecodeRequest() { |
| 33 } |
| 34 |
| 35 void VaapiJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, |
| 36 Error error) { |
| 37 if (message_loop_ != base::MessageLoop::current()) { |
| 38 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 39 message_loop_->PostTask(FROM_HERE, |
| 40 base::Bind(&VaapiJpegDecodeAccelerator::NotifyError, |
| 41 weak_this_, bitstream_buffer_id, error)); |
| 42 return; |
| 43 } |
| 44 |
| 45 Cleanup(); |
| 46 |
| 47 LOG(ERROR) << "Notifying of error " << error; |
| 48 if (client_) { |
| 49 client_->NotifyError(bitstream_buffer_id, error); |
| 50 client_ptr_factory_.reset(); |
| 51 } |
| 52 } |
| 53 |
| 54 VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator( |
| 55 const scoped_refptr<base::MessageLoopProxy>& io_message_loop) |
| 56 : state_(kUninitialized), |
| 57 message_loop_(base::MessageLoop::current()), |
| 58 io_message_loop_(io_message_loop), |
| 59 decoder_thread_("VaapiJpegDecoderThread"), |
| 60 va_surface_(VA_INVALID_SURFACE), |
| 61 weak_this_factory_(this) { |
| 62 weak_this_ = weak_this_factory_.GetWeakPtr(); |
| 63 } |
| 64 |
| 65 VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() { |
| 66 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 67 } |
| 68 |
| 69 bool VaapiJpegDecodeAccelerator::Initialize(Client* client) { |
| 70 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 71 |
| 72 client_ptr_factory_.reset(new base::WeakPtrFactory<Client>(client)); |
| 73 client_ = client_ptr_factory_->GetWeakPtr(); |
| 74 |
| 75 base::AutoLock auto_lock(lock_); |
| 76 DCHECK_EQ(state_, kUninitialized); |
| 77 |
| 78 vaapi_wrapper_ = |
| 79 VaapiWrapper::Create(VaapiWrapper::kDecode, VAProfileJPEGBaseline, |
| 80 base::Bind(&ReportVaapiError)); |
| 81 |
| 82 if (!vaapi_wrapper_.get()) { |
| 83 DLOG(ERROR) << "Failed initializing VAAPI"; |
| 84 return false; |
| 85 } |
| 86 |
| 87 CHECK(decoder_thread_.Start()); |
| 88 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy(); |
| 89 |
| 90 state_ = kInitialized; |
| 91 return true; |
| 92 } |
| 93 |
| 94 bool VaapiJpegDecodeAccelerator::OutputPicture( |
| 95 VASurfaceID va_surface_id, |
| 96 int32_t input_id, |
| 97 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 98 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 99 |
| 100 TRACE_EVENT1("jpeg", "VaapiJpegDecodeAccelerator::OutputPicture", "input_id", |
| 101 input_id); |
| 102 |
| 103 DVLOG(3) << "Outputting VASurface " << va_surface_id |
| 104 << " into video_frame associate to input buffer id " << input_id; |
| 105 |
| 106 VAImage image; |
| 107 VAImageFormat format; |
| 108 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0'); |
| 109 memset(&image, 0, sizeof(image)); |
| 110 memset(&format, 0, sizeof(format)); |
| 111 format.fourcc = kI420Fourcc; |
| 112 format.byte_order = VA_LSB_FIRST; |
| 113 format.bits_per_pixel = 12; // 12 for I420 |
| 114 |
| 115 void* mem; |
| 116 gfx::Size coded_size = video_frame->coded_size(); |
| 117 if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image, |
| 118 &mem)) { |
| 119 DLOG(ERROR) << "Cannot get VAImage"; |
| 120 return false; |
| 121 } |
| 122 |
| 123 uint8* frame_mem = video_frame->data(media::VideoFrame::kYPlane); |
| 124 size_t frame_buffer_size = |
| 125 media::VideoFrame::AllocationSize(media::VideoFrame::I420, coded_size); |
| 126 memcpy(frame_mem, mem, frame_buffer_size); |
| 127 |
| 128 vaapi_wrapper_->ReturnVaImage(&image); |
| 129 |
| 130 if (client_) |
| 131 client_->VideoFrameReady(input_id); |
| 132 |
| 133 return true; |
| 134 } |
| 135 |
| 136 void VaapiJpegDecodeAccelerator::QueueNewDecodeRequest( |
| 137 const media::BitstreamBuffer& bitstream_buffer, |
| 138 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 139 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 140 |
| 141 // Set up a new decode request and queue it for later. |
| 142 linked_ptr<DecodeRequest> input_buffer( |
| 143 new DecodeRequest(bitstream_buffer, video_frame)); |
| 144 base::AutoLock auto_lock(lock_); |
| 145 decode_requests_.push(input_buffer); |
| 146 } |
| 147 |
| 148 void VaapiJpegDecodeAccelerator::DecodeTask() { |
| 149 DVLOG(3) << __func__; |
| 150 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 151 TRACE_EVENT0("jpeg", "DecodeTask"); |
| 152 linked_ptr<DecodeRequest> request; |
| 153 { |
| 154 base::AutoLock auto_lock(lock_); |
| 155 |
| 156 DCHECK(!decode_requests_.empty()); |
| 157 request = decode_requests_.front(); |
| 158 decode_requests_.pop(); |
| 159 } |
| 160 |
| 161 do { |
| 162 DVLOG(4) << "Mapping new input buffer id: " |
| 163 << request->bitstream_buffer.id() |
| 164 << " size: " << (int)request->bitstream_buffer.size(); |
| 165 |
| 166 scoped_ptr<base::SharedMemory> shm( |
| 167 new base::SharedMemory(request->bitstream_buffer.handle(), true)); |
| 168 if (!shm->Map(request->bitstream_buffer.size())) { |
| 169 LOG(ERROR) << "Failed to map input buffer"; |
| 170 NotifyError(request->bitstream_buffer.id(), UNREADABLE_INPUT); |
| 171 break; |
| 172 } |
| 173 |
| 174 media::JpegParseResult parse_result; |
| 175 |
| 176 if (!media::ParseJpegPicture( |
| 177 reinterpret_cast<const uint8_t*>(shm->memory()), |
| 178 request->bitstream_buffer.size(), &parse_result)) { |
| 179 DLOG(ERROR) << "ParseJpegPicture failed"; |
| 180 NotifyError(request->bitstream_buffer.id(), |
| 181 media::JpegDecodeAccelerator::PARSE_JPEG_FAILED); |
| 182 break; |
| 183 } |
| 184 |
| 185 // Reuse VASurface if size doesn't change. |
| 186 // This is not only optimization, but also to avoid libva resoruce leak. |
| 187 // crosbug.com/p/39584 |
| 188 gfx::Size coded_size(parse_result.frame_header.coded_width, |
| 189 parse_result.frame_header.coded_height); |
| 190 if (coded_size != last_coded_size_ || va_surface_ == VA_INVALID_SURFACE) { |
| 191 vaapi_wrapper_->DestroySurfaces(); |
| 192 std::vector<VASurfaceID> va_surfaces; |
| 193 if (!vaapi_wrapper_->CreateSurfaces(coded_size, 1, &va_surfaces)) |
| 194 break; |
| 195 va_surface_ = va_surfaces[0]; |
| 196 last_coded_size_ = coded_size; |
| 197 } |
| 198 |
| 199 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result, |
| 200 va_surface_)) { |
| 201 LOG(ERROR) << "Decode failed"; |
| 202 NotifyError(request->bitstream_buffer.id(), |
| 203 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 204 break; |
| 205 } |
| 206 |
| 207 if (!OutputPicture(va_surface_, request->bitstream_buffer.id(), |
| 208 request->video_frame)) { |
| 209 LOG(ERROR) << "Output failed"; |
| 210 NotifyError(request->bitstream_buffer.id(), |
| 211 media::JpegDecodeAccelerator::PLATFORM_FAILURE); |
| 212 break; |
| 213 } |
| 214 } while (0); |
| 215 } |
| 216 |
| 217 void VaapiJpegDecodeAccelerator::Decode( |
| 218 const media::BitstreamBuffer& bitstream_buffer, |
| 219 const scoped_refptr<media::VideoFrame>& video_frame) { |
| 220 DVLOG(3) << __func__; |
| 221 DCHECK(io_message_loop_->BelongsToCurrentThread()); |
| 222 |
| 223 TRACE_EVENT1("jpeg", "Decode", "input_id", bitstream_buffer.id()); |
| 224 QueueNewDecodeRequest(bitstream_buffer, video_frame); |
| 225 |
| 226 decoder_thread_proxy_->PostTask( |
| 227 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask, |
| 228 base::Unretained(this))); |
| 229 } |
| 230 |
| 231 void VaapiJpegDecodeAccelerator::Cleanup() { |
| 232 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 233 |
| 234 base::AutoLock auto_lock(lock_); |
| 235 if (state_ == kUninitialized) |
| 236 return; |
| 237 |
| 238 DVLOG(1) << "Destroying VaapiJpegDecodeAccelerator"; |
| 239 |
| 240 client_ptr_factory_.reset(); |
| 241 weak_this_factory_.InvalidateWeakPtrs(); |
| 242 |
| 243 { |
| 244 base::AutoUnlock auto_unlock(lock_); |
| 245 decoder_thread_.Stop(); |
| 246 } |
| 247 |
| 248 state_ = kUninitialized; |
| 249 } |
| 250 |
| 251 void VaapiJpegDecodeAccelerator::Destroy() { |
| 252 DCHECK_EQ(message_loop_, base::MessageLoop::current()); |
| 253 Cleanup(); |
| 254 delete this; |
| 255 } |
| 256 |
| 257 } // namespace content |
OLD | NEW |