| 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 <stddef.h> | |
| 8 #include <string.h> | |
| 9 | |
| 10 #include <memory> | |
| 11 #include <utility> | |
| 12 | |
| 13 #include "base/bind.h" | |
| 14 #include "base/logging.h" | |
| 15 #include "base/metrics/histogram.h" | |
| 16 #include "base/thread_task_runner_handle.h" | |
| 17 #include "base/trace_event/trace_event.h" | |
| 18 #include "content/common/gpu/media/shared_memory_region.h" | |
| 19 #include "content/common/gpu/media/vaapi_picture.h" | |
| 20 #include "gpu/ipc/service/gpu_channel.h" | |
| 21 #include "media/base/video_frame.h" | |
| 22 #include "media/filters/jpeg_parser.h" | |
| 23 #include "third_party/libyuv/include/libyuv.h" | |
| 24 | |
| 25 namespace content { | |
| 26 | |
| 27 namespace { | |
| 28 // UMA errors that the VaapiJpegDecodeAccelerator class reports. | |
| 29 enum VAJDADecoderFailure { | |
| 30 VAAPI_ERROR = 0, | |
| 31 // UMA requires that max must be greater than 1. | |
| 32 VAJDA_DECODER_FAILURES_MAX = 2, | |
| 33 }; | |
| 34 | |
| 35 static void ReportToUMA(VAJDADecoderFailure failure) { | |
| 36 UMA_HISTOGRAM_ENUMERATION("Media.VAJDA.DecoderFailure", failure, | |
| 37 VAJDA_DECODER_FAILURES_MAX); | |
| 38 } | |
| 39 | |
| 40 static unsigned int VaSurfaceFormatForJpeg( | |
| 41 const media::JpegFrameHeader& frame_header) { | |
| 42 // The range of sampling factor is [1, 4]. Pack them into integer to make the | |
| 43 // matching code simpler. For example, 0x211 means the sampling factor are 2, | |
| 44 // 1, 1 for 3 components. | |
| 45 unsigned int h = 0, v = 0; | |
| 46 for (int i = 0; i < frame_header.num_components; i++) { | |
| 47 DCHECK_LE(frame_header.components[i].horizontal_sampling_factor, 4); | |
| 48 DCHECK_LE(frame_header.components[i].vertical_sampling_factor, 4); | |
| 49 h = h << 4 | frame_header.components[i].horizontal_sampling_factor; | |
| 50 v = v << 4 | frame_header.components[i].vertical_sampling_factor; | |
| 51 } | |
| 52 | |
| 53 switch (frame_header.num_components) { | |
| 54 case 1: // Grey image | |
| 55 return VA_RT_FORMAT_YUV400; | |
| 56 | |
| 57 case 3: // Y Cb Cr color image | |
| 58 // See https://en.wikipedia.org/wiki/Chroma_subsampling for the | |
| 59 // definition of these numbers. | |
| 60 if (h == 0x211 && v == 0x211) | |
| 61 return VA_RT_FORMAT_YUV420; | |
| 62 | |
| 63 if (h == 0x211 && v == 0x111) | |
| 64 return VA_RT_FORMAT_YUV422; | |
| 65 | |
| 66 if (h == 0x111 && v == 0x111) | |
| 67 return VA_RT_FORMAT_YUV444; | |
| 68 | |
| 69 if (h == 0x411 && v == 0x111) | |
| 70 return VA_RT_FORMAT_YUV411; | |
| 71 } | |
| 72 DVLOG(1) << "Unsupported sampling factor: num_components=" | |
| 73 << frame_header.num_components << ", h=" << std::hex << h | |
| 74 << ", v=" << v; | |
| 75 | |
| 76 return 0; | |
| 77 } | |
| 78 | |
| 79 } // namespace | |
| 80 | |
| 81 VaapiJpegDecodeAccelerator::DecodeRequest::DecodeRequest( | |
| 82 int32_t bitstream_buffer_id, | |
| 83 std::unique_ptr<SharedMemoryRegion> shm, | |
| 84 const scoped_refptr<media::VideoFrame>& video_frame) | |
| 85 : bitstream_buffer_id(bitstream_buffer_id), | |
| 86 shm(std::move(shm)), | |
| 87 video_frame(video_frame) {} | |
| 88 | |
| 89 VaapiJpegDecodeAccelerator::DecodeRequest::~DecodeRequest() { | |
| 90 } | |
| 91 | |
| 92 void VaapiJpegDecodeAccelerator::NotifyError(int32_t bitstream_buffer_id, | |
| 93 Error error) { | |
| 94 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 95 DLOG(ERROR) << "Notifying of error " << error; | |
| 96 DCHECK(client_); | |
| 97 client_->NotifyError(bitstream_buffer_id, error); | |
| 98 } | |
| 99 | |
| 100 void VaapiJpegDecodeAccelerator::NotifyErrorFromDecoderThread( | |
| 101 int32_t bitstream_buffer_id, | |
| 102 Error error) { | |
| 103 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
| 104 task_runner_->PostTask(FROM_HERE, | |
| 105 base::Bind(&VaapiJpegDecodeAccelerator::NotifyError, | |
| 106 weak_this_, bitstream_buffer_id, error)); | |
| 107 } | |
| 108 | |
| 109 void VaapiJpegDecodeAccelerator::VideoFrameReady(int32_t bitstream_buffer_id) { | |
| 110 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 111 client_->VideoFrameReady(bitstream_buffer_id); | |
| 112 } | |
| 113 | |
| 114 VaapiJpegDecodeAccelerator::VaapiJpegDecodeAccelerator( | |
| 115 const scoped_refptr<base::SingleThreadTaskRunner>& io_task_runner) | |
| 116 : task_runner_(base::ThreadTaskRunnerHandle::Get()), | |
| 117 io_task_runner_(io_task_runner), | |
| 118 decoder_thread_("VaapiJpegDecoderThread"), | |
| 119 va_surface_id_(VA_INVALID_SURFACE), | |
| 120 weak_this_factory_(this) { | |
| 121 weak_this_ = weak_this_factory_.GetWeakPtr(); | |
| 122 } | |
| 123 | |
| 124 VaapiJpegDecodeAccelerator::~VaapiJpegDecodeAccelerator() { | |
| 125 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 126 DVLOG(1) << "Destroying VaapiJpegDecodeAccelerator"; | |
| 127 | |
| 128 weak_this_factory_.InvalidateWeakPtrs(); | |
| 129 decoder_thread_.Stop(); | |
| 130 } | |
| 131 | |
| 132 bool VaapiJpegDecodeAccelerator::Initialize(Client* client) { | |
| 133 DCHECK(task_runner_->BelongsToCurrentThread()); | |
| 134 | |
| 135 client_ = client; | |
| 136 | |
| 137 vaapi_wrapper_ = | |
| 138 VaapiWrapper::Create(VaapiWrapper::kDecode, VAProfileJPEGBaseline, | |
| 139 base::Bind(&ReportToUMA, VAAPI_ERROR)); | |
| 140 | |
| 141 if (!vaapi_wrapper_.get()) { | |
| 142 DLOG(ERROR) << "Failed initializing VAAPI"; | |
| 143 return false; | |
| 144 } | |
| 145 | |
| 146 if (!decoder_thread_.Start()) { | |
| 147 DLOG(ERROR) << "Failed to start decoding thread."; | |
| 148 return false; | |
| 149 } | |
| 150 decoder_task_runner_ = decoder_thread_.task_runner(); | |
| 151 | |
| 152 return true; | |
| 153 } | |
| 154 | |
| 155 bool VaapiJpegDecodeAccelerator::OutputPicture( | |
| 156 VASurfaceID va_surface_id, | |
| 157 int32_t input_buffer_id, | |
| 158 const scoped_refptr<media::VideoFrame>& video_frame) { | |
| 159 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
| 160 | |
| 161 TRACE_EVENT1("jpeg", "VaapiJpegDecodeAccelerator::OutputPicture", | |
| 162 "input_buffer_id", input_buffer_id); | |
| 163 | |
| 164 DVLOG(3) << "Outputting VASurface " << va_surface_id | |
| 165 << " into video_frame associated with input buffer id " | |
| 166 << input_buffer_id; | |
| 167 | |
| 168 VAImage image; | |
| 169 VAImageFormat format; | |
| 170 const uint32_t kI420Fourcc = VA_FOURCC('I', '4', '2', '0'); | |
| 171 memset(&image, 0, sizeof(image)); | |
| 172 memset(&format, 0, sizeof(format)); | |
| 173 format.fourcc = kI420Fourcc; | |
| 174 format.byte_order = VA_LSB_FIRST; | |
| 175 format.bits_per_pixel = 12; // 12 for I420 | |
| 176 | |
| 177 uint8_t* mem = nullptr; | |
| 178 gfx::Size coded_size = video_frame->coded_size(); | |
| 179 if (!vaapi_wrapper_->GetVaImage(va_surface_id, &format, coded_size, &image, | |
| 180 reinterpret_cast<void**>(&mem))) { | |
| 181 DLOG(ERROR) << "Cannot get VAImage"; | |
| 182 return false; | |
| 183 } | |
| 184 | |
| 185 // Copy image content from VAImage to VideoFrame. | |
| 186 // The component order of VAImage I420 are Y, U, and V. | |
| 187 DCHECK_EQ(image.num_planes, 3u); | |
| 188 DCHECK_GE(image.width, coded_size.width()); | |
| 189 DCHECK_GE(image.height, coded_size.height()); | |
| 190 const uint8_t* src_y = mem + image.offsets[0]; | |
| 191 const uint8_t* src_u = mem + image.offsets[1]; | |
| 192 const uint8_t* src_v = mem + image.offsets[2]; | |
| 193 size_t src_y_stride = image.pitches[0]; | |
| 194 size_t src_u_stride = image.pitches[1]; | |
| 195 size_t src_v_stride = image.pitches[2]; | |
| 196 uint8_t* dst_y = video_frame->data(media::VideoFrame::kYPlane); | |
| 197 uint8_t* dst_u = video_frame->data(media::VideoFrame::kUPlane); | |
| 198 uint8_t* dst_v = video_frame->data(media::VideoFrame::kVPlane); | |
| 199 size_t dst_y_stride = video_frame->stride(media::VideoFrame::kYPlane); | |
| 200 size_t dst_u_stride = video_frame->stride(media::VideoFrame::kUPlane); | |
| 201 size_t dst_v_stride = video_frame->stride(media::VideoFrame::kVPlane); | |
| 202 | |
| 203 if (libyuv::I420Copy(src_y, src_y_stride, // Y | |
| 204 src_u, src_u_stride, // U | |
| 205 src_v, src_v_stride, // V | |
| 206 dst_y, dst_y_stride, // Y | |
| 207 dst_u, dst_u_stride, // U | |
| 208 dst_v, dst_v_stride, // V | |
| 209 coded_size.width(), coded_size.height())) { | |
| 210 DLOG(ERROR) << "I420Copy failed"; | |
| 211 return false; | |
| 212 } | |
| 213 | |
| 214 vaapi_wrapper_->ReturnVaImage(&image); | |
| 215 | |
| 216 task_runner_->PostTask( | |
| 217 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::VideoFrameReady, | |
| 218 weak_this_, input_buffer_id)); | |
| 219 | |
| 220 return true; | |
| 221 } | |
| 222 | |
| 223 void VaapiJpegDecodeAccelerator::DecodeTask( | |
| 224 const std::unique_ptr<DecodeRequest>& request) { | |
| 225 DVLOG(3) << __func__; | |
| 226 DCHECK(decoder_task_runner_->BelongsToCurrentThread()); | |
| 227 TRACE_EVENT0("jpeg", "DecodeTask"); | |
| 228 | |
| 229 media::JpegParseResult parse_result; | |
| 230 if (!media::ParseJpegPicture( | |
| 231 reinterpret_cast<const uint8_t*>(request->shm->memory()), | |
| 232 request->shm->size(), &parse_result)) { | |
| 233 DLOG(ERROR) << "ParseJpegPicture failed"; | |
| 234 NotifyErrorFromDecoderThread(request->bitstream_buffer_id, | |
| 235 PARSE_JPEG_FAILED); | |
| 236 return; | |
| 237 } | |
| 238 | |
| 239 unsigned int new_va_rt_format = | |
| 240 VaSurfaceFormatForJpeg(parse_result.frame_header); | |
| 241 if (!new_va_rt_format) { | |
| 242 DLOG(ERROR) << "Unsupported subsampling"; | |
| 243 NotifyErrorFromDecoderThread(request->bitstream_buffer_id, | |
| 244 UNSUPPORTED_JPEG); | |
| 245 return; | |
| 246 } | |
| 247 | |
| 248 // Reuse VASurface if size doesn't change. | |
| 249 gfx::Size new_coded_size(parse_result.frame_header.coded_width, | |
| 250 parse_result.frame_header.coded_height); | |
| 251 if (new_coded_size != coded_size_ || va_surface_id_ == VA_INVALID_SURFACE || | |
| 252 new_va_rt_format != va_rt_format_) { | |
| 253 vaapi_wrapper_->DestroySurfaces(); | |
| 254 va_surface_id_ = VA_INVALID_SURFACE; | |
| 255 va_rt_format_ = new_va_rt_format; | |
| 256 | |
| 257 std::vector<VASurfaceID> va_surfaces; | |
| 258 if (!vaapi_wrapper_->CreateSurfaces(va_rt_format_, new_coded_size, 1, | |
| 259 &va_surfaces)) { | |
| 260 LOG(ERROR) << "Create VA surface failed"; | |
| 261 NotifyErrorFromDecoderThread(request->bitstream_buffer_id, | |
| 262 PLATFORM_FAILURE); | |
| 263 return; | |
| 264 } | |
| 265 va_surface_id_ = va_surfaces[0]; | |
| 266 coded_size_ = new_coded_size; | |
| 267 } | |
| 268 | |
| 269 if (!VaapiJpegDecoder::Decode(vaapi_wrapper_.get(), parse_result, | |
| 270 va_surface_id_)) { | |
| 271 LOG(ERROR) << "Decode JPEG failed"; | |
| 272 NotifyErrorFromDecoderThread(request->bitstream_buffer_id, | |
| 273 PLATFORM_FAILURE); | |
| 274 return; | |
| 275 } | |
| 276 | |
| 277 if (!OutputPicture(va_surface_id_, request->bitstream_buffer_id, | |
| 278 request->video_frame)) { | |
| 279 LOG(ERROR) << "Output picture failed"; | |
| 280 NotifyErrorFromDecoderThread(request->bitstream_buffer_id, | |
| 281 PLATFORM_FAILURE); | |
| 282 return; | |
| 283 } | |
| 284 } | |
| 285 | |
| 286 void VaapiJpegDecodeAccelerator::Decode( | |
| 287 const media::BitstreamBuffer& bitstream_buffer, | |
| 288 const scoped_refptr<media::VideoFrame>& video_frame) { | |
| 289 DVLOG(3) << __func__; | |
| 290 DCHECK(io_task_runner_->BelongsToCurrentThread()); | |
| 291 TRACE_EVENT1("jpeg", "Decode", "input_id", bitstream_buffer.id()); | |
| 292 | |
| 293 DVLOG(4) << "Mapping new input buffer id: " << bitstream_buffer.id() | |
| 294 << " size: " << bitstream_buffer.size(); | |
| 295 | |
| 296 // SharedMemoryRegion will take over the |bitstream_buffer.handle()|. | |
| 297 std::unique_ptr<SharedMemoryRegion> shm( | |
| 298 new SharedMemoryRegion(bitstream_buffer, true)); | |
| 299 | |
| 300 if (bitstream_buffer.id() < 0) { | |
| 301 LOG(ERROR) << "Invalid bitstream_buffer, id: " << bitstream_buffer.id(); | |
| 302 NotifyErrorFromDecoderThread(bitstream_buffer.id(), INVALID_ARGUMENT); | |
| 303 return; | |
| 304 } | |
| 305 | |
| 306 if (!shm->Map()) { | |
| 307 LOG(ERROR) << "Failed to map input buffer"; | |
| 308 NotifyErrorFromDecoderThread(bitstream_buffer.id(), UNREADABLE_INPUT); | |
| 309 return; | |
| 310 } | |
| 311 | |
| 312 std::unique_ptr<DecodeRequest> request( | |
| 313 new DecodeRequest(bitstream_buffer.id(), std::move(shm), video_frame)); | |
| 314 | |
| 315 decoder_task_runner_->PostTask( | |
| 316 FROM_HERE, base::Bind(&VaapiJpegDecodeAccelerator::DecodeTask, | |
| 317 base::Unretained(this), base::Passed(&request))); | |
| 318 } | |
| 319 | |
| 320 bool VaapiJpegDecodeAccelerator::IsSupported() { | |
| 321 return VaapiWrapper::IsJpegDecodeSupported(); | |
| 322 } | |
| 323 | |
| 324 } // namespace content | |
| OLD | NEW |