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 <fcntl.h> |
| 6 #include <linux/videodev2.h> |
| 7 #include <poll.h> |
| 8 #include <sys/eventfd.h> |
| 9 #include <sys/ioctl.h> |
| 10 #include <sys/mman.h> |
| 11 |
| 12 #include "base/bind.h" |
| 13 #include "base/bind_helpers.h" |
| 14 #include "base/callback.h" |
| 15 #include "base/callback_helpers.h" |
| 16 #include "base/command_line.h" |
| 17 #include "base/message_loop/message_loop_proxy.h" |
| 18 #include "base/numerics/safe_conversions.h" |
| 19 #include "base/strings/stringprintf.h" |
| 20 #include "content/common/gpu/media/v4l2_slice_video_decode_accelerator.h" |
| 21 #include "media/base/bind_to_current_loop.h" |
| 22 #include "media/base/media_switches.h" |
| 23 #include "ui/gl/scoped_binders.h" |
| 24 |
| 25 #define LOGF(level) LOG(level) << __FUNCTION__ << "(): " |
| 26 #define DVLOGF(level) DVLOG(level) << __FUNCTION__ << "(): " |
| 27 |
| 28 #define NOTIFY_ERROR(x) \ |
| 29 do { \ |
| 30 LOG(ERROR) << "Setting error state:" << x; \ |
| 31 SetErrorState(x); \ |
| 32 } while (0) |
| 33 |
| 34 #define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value) \ |
| 35 do { \ |
| 36 if (device_->Ioctl(type, arg) != 0) { \ |
| 37 PLOG(ERROR) << __FUNCTION__ << "(): ioctl() failed: " << #type; \ |
| 38 return value; \ |
| 39 } \ |
| 40 } while (0) |
| 41 |
| 42 #define IOCTL_OR_ERROR_RETURN(type, arg) \ |
| 43 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0)) |
| 44 |
| 45 #define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \ |
| 46 IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false) |
| 47 |
| 48 #define IOCTL_OR_LOG_ERROR(type, arg) \ |
| 49 do { \ |
| 50 if (device_->Ioctl(type, arg) != 0) \ |
| 51 PLOG(ERROR) << __FUNCTION__ << "(): ioctl() failed: " << #type; \ |
| 52 } while (0) |
| 53 |
| 54 namespace content { |
| 55 |
| 56 class V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface |
| 57 : public base::RefCounted<V4L2DecodeSurface> { |
| 58 public: |
| 59 using ReleaseCB = base::Callback<void(int)>; |
| 60 |
| 61 V4L2DecodeSurface(int32 bitstream_id, |
| 62 int input_record, |
| 63 int output_record, |
| 64 const ReleaseCB& release_cb); |
| 65 |
| 66 // Mark the surface as decoded. This will also release all references, as |
| 67 // they are not needed anymore. |
| 68 void SetDecoded(); |
| 69 bool decoded() const { return decoded_; } |
| 70 |
| 71 int32 bitstream_id() const { return bitstream_id_; } |
| 72 int input_record() const { return input_record_; } |
| 73 int output_record() const { return output_record_; } |
| 74 uint32_t config_store() const { return config_store_; } |
| 75 |
| 76 // Take references to each reference surface and keep them until the |
| 77 // target surface is decoded. |
| 78 void SetReferenceSurfaces( |
| 79 const std::vector<scoped_refptr<V4L2DecodeSurface>>& ref_surfaces); |
| 80 |
| 81 std::string ToString() const; |
| 82 |
| 83 private: |
| 84 friend class base::RefCounted<V4L2DecodeSurface>; |
| 85 ~V4L2DecodeSurface(); |
| 86 |
| 87 int32 bitstream_id_; |
| 88 int input_record_; |
| 89 int output_record_; |
| 90 uint32_t config_store_; |
| 91 |
| 92 bool decoded_; |
| 93 ReleaseCB release_cb_; |
| 94 |
| 95 std::vector<scoped_refptr<V4L2DecodeSurface>> reference_surfaces_; |
| 96 |
| 97 DISALLOW_COPY_AND_ASSIGN(V4L2DecodeSurface); |
| 98 }; |
| 99 |
| 100 V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::V4L2DecodeSurface( |
| 101 int32 bitstream_id, |
| 102 int input_record, |
| 103 int output_record, |
| 104 const ReleaseCB& release_cb) |
| 105 : bitstream_id_(bitstream_id), |
| 106 input_record_(input_record), |
| 107 output_record_(output_record), |
| 108 config_store_(input_record + 1), |
| 109 decoded_(false), |
| 110 release_cb_(release_cb) { |
| 111 } |
| 112 |
| 113 V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::~V4L2DecodeSurface() { |
| 114 DVLOGF(5) << "Releasing output record id=" << output_record_; |
| 115 release_cb_.Run(output_record_); |
| 116 } |
| 117 |
| 118 void V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::SetReferenceSurfaces( |
| 119 const std::vector<scoped_refptr<V4L2DecodeSurface>>& ref_surfaces) { |
| 120 DCHECK(reference_surfaces_.empty()); |
| 121 reference_surfaces_ = ref_surfaces; |
| 122 } |
| 123 |
| 124 void V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::SetDecoded() { |
| 125 DCHECK(!decoded_); |
| 126 decoded_ = true; |
| 127 |
| 128 // We can now drop references to all reference surfaces for this surface |
| 129 // as we are done with decoding. |
| 130 reference_surfaces_.clear(); |
| 131 } |
| 132 |
| 133 std::string V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::ToString() |
| 134 const { |
| 135 std::string out; |
| 136 base::StringAppendF(&out, "Buffer %d -> %d. ", input_record_, output_record_); |
| 137 base::StringAppendF(&out, "Reference surfaces:"); |
| 138 for (const auto& ref : reference_surfaces_) { |
| 139 DCHECK_NE(ref->output_record(), output_record_); |
| 140 base::StringAppendF(&out, " %d", ref->output_record()); |
| 141 } |
| 142 return out; |
| 143 } |
| 144 |
| 145 V4L2SliceVideoDecodeAccelerator::InputRecord::InputRecord() |
| 146 : input_id(-1), |
| 147 address(nullptr), |
| 148 length(0), |
| 149 bytes_used(0), |
| 150 at_device(false) { |
| 151 } |
| 152 |
| 153 V4L2SliceVideoDecodeAccelerator::OutputRecord::OutputRecord() |
| 154 : at_device(false), |
| 155 at_client(false), |
| 156 picture_id(-1), |
| 157 egl_image(EGL_NO_IMAGE_KHR), |
| 158 egl_sync(EGL_NO_SYNC_KHR), |
| 159 cleared(false) { |
| 160 } |
| 161 |
| 162 struct V4L2SliceVideoDecodeAccelerator::BitstreamBufferRef { |
| 163 BitstreamBufferRef( |
| 164 base::WeakPtr<VideoDecodeAccelerator::Client>& client, |
| 165 const scoped_refptr<base::MessageLoopProxy>& client_message_loop_proxy, |
| 166 base::SharedMemory* shm, |
| 167 size_t size, |
| 168 int32 input_id); |
| 169 ~BitstreamBufferRef(); |
| 170 const base::WeakPtr<VideoDecodeAccelerator::Client> client; |
| 171 const scoped_refptr<base::MessageLoopProxy> client_message_loop_proxy; |
| 172 const scoped_ptr<base::SharedMemory> shm; |
| 173 const size_t size; |
| 174 off_t bytes_used; |
| 175 const int32 input_id; |
| 176 }; |
| 177 |
| 178 V4L2SliceVideoDecodeAccelerator::BitstreamBufferRef::BitstreamBufferRef( |
| 179 base::WeakPtr<VideoDecodeAccelerator::Client>& client, |
| 180 const scoped_refptr<base::MessageLoopProxy>& client_message_loop_proxy, |
| 181 base::SharedMemory* shm, |
| 182 size_t size, |
| 183 int32 input_id) |
| 184 : client(client), |
| 185 client_message_loop_proxy(client_message_loop_proxy), |
| 186 shm(shm), |
| 187 size(size), |
| 188 bytes_used(0), |
| 189 input_id(input_id) { |
| 190 } |
| 191 |
| 192 V4L2SliceVideoDecodeAccelerator::BitstreamBufferRef::~BitstreamBufferRef() { |
| 193 if (input_id >= 0) { |
| 194 DVLOGF(5) << "returning input_id: " << input_id; |
| 195 client_message_loop_proxy->PostTask( |
| 196 FROM_HERE, |
| 197 base::Bind(&VideoDecodeAccelerator::Client::NotifyEndOfBitstreamBuffer, |
| 198 client, input_id)); |
| 199 } |
| 200 } |
| 201 |
| 202 struct V4L2SliceVideoDecodeAccelerator::EGLSyncKHRRef { |
| 203 EGLSyncKHRRef(EGLDisplay egl_display, EGLSyncKHR egl_sync); |
| 204 ~EGLSyncKHRRef(); |
| 205 EGLDisplay const egl_display; |
| 206 EGLSyncKHR egl_sync; |
| 207 }; |
| 208 |
| 209 V4L2SliceVideoDecodeAccelerator::EGLSyncKHRRef::EGLSyncKHRRef( |
| 210 EGLDisplay egl_display, |
| 211 EGLSyncKHR egl_sync) |
| 212 : egl_display(egl_display), egl_sync(egl_sync) { |
| 213 } |
| 214 |
| 215 V4L2SliceVideoDecodeAccelerator::EGLSyncKHRRef::~EGLSyncKHRRef() { |
| 216 // We don't check for eglDestroySyncKHR failures, because if we get here |
| 217 // with a valid sync object, something went wrong and we are getting |
| 218 // destroyed anyway. |
| 219 if (egl_sync != EGL_NO_SYNC_KHR) |
| 220 eglDestroySyncKHR(egl_display, egl_sync); |
| 221 } |
| 222 |
| 223 struct V4L2SliceVideoDecodeAccelerator::PictureRecord { |
| 224 PictureRecord(bool cleared, const media::Picture& picture); |
| 225 ~PictureRecord(); |
| 226 bool cleared; // Whether the texture is cleared and safe to render from. |
| 227 media::Picture picture; // The decoded picture. |
| 228 }; |
| 229 |
| 230 V4L2SliceVideoDecodeAccelerator::PictureRecord::PictureRecord( |
| 231 bool cleared, |
| 232 const media::Picture& picture) |
| 233 : cleared(cleared), picture(picture) { |
| 234 } |
| 235 |
| 236 V4L2SliceVideoDecodeAccelerator::PictureRecord::~PictureRecord() { |
| 237 } |
| 238 |
| 239 class V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator |
| 240 : public H264Decoder::H264Accelerator { |
| 241 public: |
| 242 V4L2H264Accelerator(V4L2SliceVideoDecodeAccelerator* v4l2_dec); |
| 243 virtual ~V4L2H264Accelerator() override; |
| 244 |
| 245 // H264Decoder::H264Accelerator implementation. |
| 246 scoped_refptr<H264Picture> CreateH264Picture() override; |
| 247 |
| 248 bool SubmitFrameMetadata(const media::H264SPS* sps, |
| 249 const media::H264PPS* pps, |
| 250 const H264DPB& dpb, |
| 251 const H264Picture::Vector& ref_pic_listp0, |
| 252 const H264Picture::Vector& ref_pic_listb0, |
| 253 const H264Picture::Vector& ref_pic_listb1, |
| 254 const scoped_refptr<H264Picture>& pic) override; |
| 255 |
| 256 bool SubmitSlice(const media::H264PPS* pps, |
| 257 const media::H264SliceHeader* slice_hdr, |
| 258 const H264Picture::Vector& ref_pic_list0, |
| 259 const H264Picture::Vector& ref_pic_list1, |
| 260 const scoped_refptr<H264Picture>& pic, |
| 261 const uint8_t* data, |
| 262 size_t size) override; |
| 263 |
| 264 bool SubmitDecode(const scoped_refptr<H264Picture>& pic) override; |
| 265 bool OutputPicture(const scoped_refptr<H264Picture>& pic) override; |
| 266 |
| 267 private: |
| 268 // Max size of reference list. |
| 269 static const size_t kDPBIndicesListSize = 32; |
| 270 void H264PictureListToDPBIndicesList(const H264Picture::Vector& src_pic_list, |
| 271 uint8_t dst_list[kDPBIndicesListSize]); |
| 272 |
| 273 void H264DPBToV4L2DPB( |
| 274 const H264DPB& dpb, |
| 275 std::vector<scoped_refptr<V4L2DecodeSurface>>* ref_surfaces); |
| 276 |
| 277 scoped_refptr<V4L2DecodeSurface> H264PictureToV4L2DecodeSurface( |
| 278 const scoped_refptr<H264Picture>& pic); |
| 279 |
| 280 size_t num_slices_; |
| 281 V4L2SliceVideoDecodeAccelerator* v4l2_dec_; |
| 282 |
| 283 // TODO(posciak): This should be queried from hardware once supported. |
| 284 static const size_t kMaxSlices = 16; |
| 285 struct v4l2_ctrl_h264_slice_param v4l2_slice_params_[kMaxSlices]; |
| 286 struct v4l2_ctrl_h264_decode_param v4l2_decode_param_; |
| 287 |
| 288 DISALLOW_COPY_AND_ASSIGN(V4L2H264Accelerator); |
| 289 }; |
| 290 |
| 291 class V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator |
| 292 : public VP8Decoder::VP8Accelerator { |
| 293 public: |
| 294 V4L2VP8Accelerator(V4L2SliceVideoDecodeAccelerator* v4l2_dec); |
| 295 ~V4L2VP8Accelerator() override; |
| 296 |
| 297 // H264Decoder::VP8Accelerator implementation. |
| 298 scoped_refptr<VP8Picture> CreateVP8Picture() override; |
| 299 |
| 300 bool SubmitDecode(const scoped_refptr<VP8Picture>& pic, |
| 301 const media::Vp8FrameHeader* frame_hdr, |
| 302 const scoped_refptr<VP8Picture>& last_frame, |
| 303 const scoped_refptr<VP8Picture>& golden_frame, |
| 304 const scoped_refptr<VP8Picture>& alt_frame) override; |
| 305 |
| 306 bool OutputPicture(const scoped_refptr<VP8Picture>& pic) override; |
| 307 |
| 308 private: |
| 309 scoped_refptr<V4L2DecodeSurface> VP8PictureToV4L2DecodeSurface( |
| 310 const scoped_refptr<VP8Picture>& pic); |
| 311 |
| 312 V4L2SliceVideoDecodeAccelerator* v4l2_dec_; |
| 313 |
| 314 DISALLOW_COPY_AND_ASSIGN(V4L2VP8Accelerator); |
| 315 }; |
| 316 |
| 317 // Codec-specific subclasses of software decoder picture classes. |
| 318 // This allows us to keep decoders oblivious of our implementation details. |
| 319 class V4L2H264Picture : public H264Picture { |
| 320 public: |
| 321 V4L2H264Picture(const scoped_refptr< |
| 322 V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>& dec_surface); |
| 323 virtual ~V4L2H264Picture() override; |
| 324 |
| 325 V4L2H264Picture* AsV4L2H264Picture() override { return this; } |
| 326 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 327 dec_surface() { |
| 328 return dec_surface_; |
| 329 } |
| 330 |
| 331 private: |
| 332 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 333 dec_surface_; |
| 334 |
| 335 DISALLOW_COPY_AND_ASSIGN(V4L2H264Picture); |
| 336 }; |
| 337 |
| 338 V4L2H264Picture::V4L2H264Picture(const scoped_refptr< |
| 339 V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>& dec_surface) |
| 340 : dec_surface_(dec_surface) { |
| 341 } |
| 342 |
| 343 V4L2H264Picture::~V4L2H264Picture() { |
| 344 } |
| 345 |
| 346 class V4L2VP8Picture : public VP8Picture { |
| 347 public: |
| 348 V4L2VP8Picture(const scoped_refptr< |
| 349 V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>& dec_surface); |
| 350 virtual ~V4L2VP8Picture() override; |
| 351 |
| 352 V4L2VP8Picture* AsV4L2VP8Picture() override { return this; } |
| 353 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 354 dec_surface() { |
| 355 return dec_surface_; |
| 356 } |
| 357 |
| 358 private: |
| 359 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 360 dec_surface_; |
| 361 |
| 362 DISALLOW_COPY_AND_ASSIGN(V4L2VP8Picture); |
| 363 }; |
| 364 |
| 365 V4L2VP8Picture::V4L2VP8Picture(const scoped_refptr< |
| 366 V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>& dec_surface) |
| 367 : dec_surface_(dec_surface) { |
| 368 } |
| 369 |
| 370 V4L2VP8Picture::~V4L2VP8Picture() { |
| 371 } |
| 372 |
| 373 V4L2SliceVideoDecodeAccelerator::V4L2SliceVideoDecodeAccelerator( |
| 374 const scoped_refptr<V4L2Device>& device, |
| 375 EGLDisplay egl_display, |
| 376 EGLContext egl_context, |
| 377 const base::WeakPtr<Client>& io_client, |
| 378 const base::Callback<bool(void)>& make_context_current, |
| 379 const scoped_refptr<base::MessageLoopProxy>& io_message_loop_proxy) |
| 380 : input_planes_count_(0), |
| 381 output_planes_count_(0), |
| 382 child_message_loop_proxy_(base::MessageLoopProxy::current()), |
| 383 io_message_loop_proxy_(io_message_loop_proxy), |
| 384 io_client_(io_client), |
| 385 device_(device), |
| 386 decoder_thread_("V4L2SliceVideoDecodeAcceleratorThread"), |
| 387 device_poll_thread_("V4L2SliceVideoDecodeAcceleratorDevicePollThread"), |
| 388 input_streamon_(false), |
| 389 input_buffer_queued_count_(0), |
| 390 output_streamon_(false), |
| 391 output_buffer_queued_count_(0), |
| 392 video_profile_(media::VIDEO_CODEC_PROFILE_UNKNOWN), |
| 393 output_format_fourcc_(0), |
| 394 output_dpb_size_(0), |
| 395 state_(kUninitialized), |
| 396 decoder_flushing_(false), |
| 397 decoder_resetting_(false), |
| 398 surface_set_change_pending_(false), |
| 399 picture_clearing_count_(0), |
| 400 pictures_assigned_(false, false), |
| 401 make_context_current_(make_context_current), |
| 402 egl_display_(egl_display), |
| 403 egl_context_(egl_context), |
| 404 weak_this_factory_(this) { |
| 405 weak_this_ = weak_this_factory_.GetWeakPtr(); |
| 406 } |
| 407 |
| 408 V4L2SliceVideoDecodeAccelerator::~V4L2SliceVideoDecodeAccelerator() { |
| 409 DVLOGF(2); |
| 410 |
| 411 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 412 DCHECK(!decoder_thread_.IsRunning()); |
| 413 DCHECK(!device_poll_thread_.IsRunning()); |
| 414 |
| 415 DCHECK(input_buffer_map_.empty()); |
| 416 DCHECK(output_buffer_map_.empty()); |
| 417 } |
| 418 |
| 419 void V4L2SliceVideoDecodeAccelerator::NotifyError(Error error) { |
| 420 if (!child_message_loop_proxy_->BelongsToCurrentThread()) { |
| 421 child_message_loop_proxy_->PostTask( |
| 422 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::NotifyError, |
| 423 weak_this_, error)); |
| 424 return; |
| 425 } |
| 426 |
| 427 if (client_) { |
| 428 client_->NotifyError(error); |
| 429 client_ptr_factory_.reset(); |
| 430 } |
| 431 } |
| 432 |
| 433 bool V4L2SliceVideoDecodeAccelerator::Initialize( |
| 434 media::VideoCodecProfile profile, |
| 435 VideoDecodeAccelerator::Client* client) { |
| 436 DVLOGF(3) << "profile: " << profile; |
| 437 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 438 DCHECK_EQ(state_, kUninitialized); |
| 439 |
| 440 client_ptr_factory_.reset( |
| 441 new base::WeakPtrFactory<VideoDecodeAccelerator::Client>(client)); |
| 442 client_ = client_ptr_factory_->GetWeakPtr(); |
| 443 |
| 444 video_profile_ = profile; |
| 445 |
| 446 if (video_profile_ >= media::H264PROFILE_MIN && |
| 447 video_profile_ <= media::H264PROFILE_MAX) { |
| 448 h264_accelerator_.reset(new V4L2H264Accelerator(this)); |
| 449 decoder_.reset(new H264Decoder(h264_accelerator_.get())); |
| 450 } else if (video_profile_ >= media::VP8PROFILE_MIN && |
| 451 video_profile_ <= media::VP8PROFILE_MAX) { |
| 452 vp8_accelerator_.reset(new V4L2VP8Accelerator(this)); |
| 453 decoder_.reset(new VP8Decoder(vp8_accelerator_.get())); |
| 454 } else { |
| 455 DLOG(ERROR) << "Unsupported profile " << video_profile_; |
| 456 return false; |
| 457 } |
| 458 |
| 459 // TODO(posciak): This needs to be queried once supported. |
| 460 input_planes_count_ = 1; |
| 461 output_planes_count_ = 1; |
| 462 |
| 463 if (egl_display_ == EGL_NO_DISPLAY) { |
| 464 LOG(ERROR) << "Initialize(): could not get EGLDisplay"; |
| 465 return false; |
| 466 } |
| 467 |
| 468 // We need the context to be initialized to query extensions. |
| 469 if (!make_context_current_.Run()) { |
| 470 LOG(ERROR) << "Initialize(): could not make context current"; |
| 471 return false; |
| 472 } |
| 473 |
| 474 if (!gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync) { |
| 475 LOG(ERROR) << "Initialize(): context does not have EGL_KHR_fence_sync"; |
| 476 return false; |
| 477 } |
| 478 |
| 479 // Capabilities check. |
| 480 struct v4l2_capability caps; |
| 481 const __u32 kCapsRequired = |
| 482 V4L2_CAP_VIDEO_CAPTURE_MPLANE | |
| 483 V4L2_CAP_VIDEO_OUTPUT_MPLANE | |
| 484 V4L2_CAP_STREAMING; |
| 485 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps); |
| 486 if ((caps.capabilities & kCapsRequired) != kCapsRequired) { |
| 487 DLOG(ERROR) << "Initialize(): ioctl() failed: VIDIOC_QUERYCAP" |
| 488 ", caps check failed: 0x" << std::hex << caps.capabilities; |
| 489 return false; |
| 490 } |
| 491 |
| 492 if (!SetupFormats()) |
| 493 return false; |
| 494 |
| 495 if (!decoder_thread_.Start()) { |
| 496 DLOG(ERROR) << "Initialize(): device thread failed to start"; |
| 497 return false; |
| 498 } |
| 499 decoder_thread_proxy_ = decoder_thread_.message_loop_proxy(); |
| 500 |
| 501 state_ = kInitialized; |
| 502 |
| 503 // InitializeTask will NOTIFY_ERROR on failure. |
| 504 decoder_thread_proxy_->PostTask( |
| 505 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::InitializeTask, |
| 506 base::Unretained(this))); |
| 507 |
| 508 DVLOGF(1) << "V4L2SliceVideoDecodeAccelerator initialized"; |
| 509 return true; |
| 510 } |
| 511 |
| 512 void V4L2SliceVideoDecodeAccelerator::InitializeTask() { |
| 513 DVLOGF(3); |
| 514 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 515 DCHECK_EQ(state_, kInitialized); |
| 516 |
| 517 if (!CreateInputBuffers()) |
| 518 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 519 |
| 520 // Output buffers will be created once decoder gives us information |
| 521 // about their size and required count. |
| 522 state_ = kDecoding; |
| 523 } |
| 524 |
| 525 void V4L2SliceVideoDecodeAccelerator::Destroy() { |
| 526 DVLOGF(3); |
| 527 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 528 |
| 529 if (decoder_thread_.IsRunning()) { |
| 530 decoder_thread_proxy_->PostTask( |
| 531 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DestroyTask, |
| 532 base::Unretained(this))); |
| 533 |
| 534 // Wait for tasks to finish/early-exit. |
| 535 decoder_thread_.Stop(); |
| 536 } |
| 537 |
| 538 delete this; |
| 539 DVLOGF(3) << "Destroyed"; |
| 540 } |
| 541 |
| 542 void V4L2SliceVideoDecodeAccelerator::DestroyTask() { |
| 543 DVLOGF(3); |
| 544 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 545 |
| 546 state_ = kError; |
| 547 |
| 548 decoder_->Reset(); |
| 549 |
| 550 decoder_current_bitstream_buffer_.reset(); |
| 551 while (!decoder_input_queue_.empty()) |
| 552 decoder_input_queue_.pop(); |
| 553 |
| 554 // Stop streaming and the device_poll_thread_. |
| 555 StopDevicePoll(false); |
| 556 |
| 557 DestroyInputBuffers(); |
| 558 DestroyOutputs(false); |
| 559 |
| 560 DCHECK(surfaces_at_device_.empty()); |
| 561 DCHECK(surfaces_at_display_.empty()); |
| 562 DCHECK(decoder_display_queue_.empty()); |
| 563 } |
| 564 |
| 565 bool V4L2SliceVideoDecodeAccelerator::SetupFormats() { |
| 566 DCHECK_EQ(state_, kUninitialized); |
| 567 |
| 568 __u32 input_format_fourcc = |
| 569 V4L2Device::VideoCodecProfileToV4L2PixFmt(video_profile_, true); |
| 570 if (!input_format_fourcc) { |
| 571 NOTREACHED(); |
| 572 return false; |
| 573 } |
| 574 |
| 575 size_t input_size; |
| 576 if (base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 577 switches::kIgnoreResolutionLimitsForAcceleratedVideoDecode)) |
| 578 input_size = kInputBufferMaxSizeFor4k; |
| 579 else |
| 580 input_size = kInputBufferMaxSizeFor1080p; |
| 581 |
| 582 struct v4l2_format format; |
| 583 memset(&format, 0, sizeof(format)); |
| 584 format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 585 format.fmt.pix_mp.pixelformat = input_format_fourcc; |
| 586 format.fmt.pix_mp.plane_fmt[0].sizeimage = input_size; |
| 587 format.fmt.pix_mp.num_planes = input_planes_count_; |
| 588 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); |
| 589 |
| 590 // We have to set up the format for output, because the driver may not allow |
| 591 // changing it once we start streaming; whether it can support our chosen |
| 592 // output format or not may depend on the input format. |
| 593 struct v4l2_fmtdesc fmtdesc; |
| 594 memset(&fmtdesc, 0, sizeof(fmtdesc)); |
| 595 fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 596 output_format_fourcc_ = 0; |
| 597 while (device_->Ioctl(VIDIOC_ENUM_FMT, &fmtdesc) == 0) { |
| 598 if (device_->CanCreateEGLImageFrom(fmtdesc.pixelformat)) { |
| 599 output_format_fourcc_ = fmtdesc.pixelformat; |
| 600 break; |
| 601 } |
| 602 ++fmtdesc.index; |
| 603 } |
| 604 |
| 605 if (output_format_fourcc_ == 0) { |
| 606 LOG(ERROR) << "Could not find a usable output format"; |
| 607 return false; |
| 608 } |
| 609 |
| 610 // Only set fourcc for output; resolution, etc., will come from the |
| 611 // driver once it extracts it from the stream. |
| 612 memset(&format, 0, sizeof(format)); |
| 613 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 614 format.fmt.pix_mp.pixelformat = output_format_fourcc_; |
| 615 format.fmt.pix_mp.num_planes = output_planes_count_; |
| 616 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format); |
| 617 |
| 618 return true; |
| 619 } |
| 620 |
| 621 bool V4L2SliceVideoDecodeAccelerator::CreateInputBuffers() { |
| 622 DVLOGF(3); |
| 623 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 624 DCHECK(!input_streamon_); |
| 625 DCHECK(input_buffer_map_.empty()); |
| 626 |
| 627 struct v4l2_requestbuffers reqbufs; |
| 628 memset(&reqbufs, 0, sizeof(reqbufs)); |
| 629 reqbufs.count = kNumInputBuffers; |
| 630 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 631 reqbufs.memory = V4L2_MEMORY_MMAP; |
| 632 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); |
| 633 if (reqbufs.count < kNumInputBuffers) { |
| 634 PLOG(ERROR) << "Could not allocate enough output buffers"; |
| 635 return false; |
| 636 } |
| 637 input_buffer_map_.resize(reqbufs.count); |
| 638 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { |
| 639 free_input_buffers_.push_back(i); |
| 640 |
| 641 // Query for the MEMORY_MMAP pointer. |
| 642 struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
| 643 struct v4l2_buffer buffer; |
| 644 memset(&buffer, 0, sizeof(buffer)); |
| 645 memset(planes, 0, sizeof(planes)); |
| 646 buffer.index = i; |
| 647 buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 648 buffer.memory = V4L2_MEMORY_MMAP; |
| 649 buffer.m.planes = planes; |
| 650 buffer.length = input_planes_count_; |
| 651 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYBUF, &buffer); |
| 652 void* address = device_->Mmap(nullptr, |
| 653 buffer.m.planes[0].length, |
| 654 PROT_READ | PROT_WRITE, |
| 655 MAP_SHARED, |
| 656 buffer.m.planes[0].m.mem_offset); |
| 657 if (address == MAP_FAILED) { |
| 658 PLOG(ERROR) << "CreateInputBuffers(): mmap() failed"; |
| 659 return false; |
| 660 } |
| 661 input_buffer_map_[i].address = address; |
| 662 input_buffer_map_[i].length = buffer.m.planes[0].length; |
| 663 } |
| 664 |
| 665 return true; |
| 666 } |
| 667 |
| 668 bool V4L2SliceVideoDecodeAccelerator::CreateOutputBuffers() { |
| 669 DVLOGF(3); |
| 670 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 671 DCHECK(!output_streamon_); |
| 672 DCHECK(output_buffer_map_.empty()); |
| 673 DCHECK(surfaces_at_display_.empty()); |
| 674 DCHECK(surfaces_at_device_.empty()); |
| 675 |
| 676 frame_buffer_size_ = decoder_->GetPicSize(); |
| 677 output_dpb_size_ = decoder_->GetRequiredNumOfPictures(); |
| 678 |
| 679 DCHECK_GT(output_dpb_size_, 0u); |
| 680 DCHECK(!frame_buffer_size_.IsEmpty()); |
| 681 |
| 682 struct v4l2_format format; |
| 683 memset(&format, 0, sizeof(format)); |
| 684 format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 685 format.fmt.pix_mp.pixelformat = output_format_fourcc_; |
| 686 format.fmt.pix_mp.width = frame_buffer_size_.width(); |
| 687 format.fmt.pix_mp.height = frame_buffer_size_.height(); |
| 688 format.fmt.pix_mp.num_planes = input_planes_count_; |
| 689 |
| 690 if (device_->Ioctl(VIDIOC_S_FMT, &format) != 0) { |
| 691 PLOG(ERROR) << "Failed setting format to: " << output_format_fourcc_; |
| 692 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 693 return false; |
| 694 } |
| 695 |
| 696 struct v4l2_requestbuffers reqbufs; |
| 697 memset(&reqbufs, 0, sizeof(reqbufs)); |
| 698 reqbufs.count = output_dpb_size_; |
| 699 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 700 reqbufs.memory = V4L2_MEMORY_MMAP; |
| 701 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); |
| 702 |
| 703 if (reqbufs.count < output_dpb_size_) { |
| 704 PLOG(ERROR) << "Could not allocate enough output buffers"; |
| 705 return false; |
| 706 } |
| 707 |
| 708 output_buffer_map_.resize(reqbufs.count); |
| 709 |
| 710 DVLOGF(3) << "buffer_count=" << output_buffer_map_.size() |
| 711 << ", size=" << frame_buffer_size_.ToString(); |
| 712 |
| 713 child_message_loop_proxy_->PostTask( |
| 714 FROM_HERE, |
| 715 base::Bind(&VideoDecodeAccelerator::Client::ProvidePictureBuffers, |
| 716 client_, output_buffer_map_.size(), frame_buffer_size_, |
| 717 device_->GetTextureTarget())); |
| 718 |
| 719 // Wait for the client to call AssignPictureBuffers() on the Child thread. |
| 720 // We do this, because if we continue decoding without finishing buffer |
| 721 // allocation, we may end up Resetting before AssignPictureBuffers arrives, |
| 722 // resulting in unnecessary complications and subtle bugs. |
| 723 pictures_assigned_.Wait(); |
| 724 |
| 725 return true; |
| 726 } |
| 727 |
| 728 void V4L2SliceVideoDecodeAccelerator::DestroyInputBuffers() { |
| 729 DVLOGF(3); |
| 730 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread() || |
| 731 !decoder_thread_.IsRunning()); |
| 732 DCHECK(!input_streamon_); |
| 733 |
| 734 for (auto& input_record : input_buffer_map_) { |
| 735 if (input_record.address != nullptr) |
| 736 device_->Munmap(input_record.address, input_record.length); |
| 737 } |
| 738 |
| 739 struct v4l2_requestbuffers reqbufs; |
| 740 memset(&reqbufs, 0, sizeof(reqbufs)); |
| 741 reqbufs.count = 0; |
| 742 reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 743 reqbufs.memory = V4L2_MEMORY_MMAP; |
| 744 IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs); |
| 745 |
| 746 input_buffer_map_.clear(); |
| 747 free_input_buffers_.clear(); |
| 748 } |
| 749 |
| 750 void V4L2SliceVideoDecodeAccelerator::DismissPictures( |
| 751 std::vector<int32> picture_buffer_ids, |
| 752 base::WaitableEvent* done) { |
| 753 DVLOGF(3); |
| 754 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 755 |
| 756 for (auto picture_buffer_id : picture_buffer_ids) { |
| 757 DVLOGF(1) << "dismissing PictureBuffer id=" << picture_buffer_id; |
| 758 client_->DismissPictureBuffer(picture_buffer_id); |
| 759 } |
| 760 |
| 761 done->Signal(); |
| 762 } |
| 763 |
| 764 void V4L2SliceVideoDecodeAccelerator::DevicePollTask(bool poll_device) { |
| 765 DVLOGF(4); |
| 766 DCHECK_EQ(device_poll_thread_.message_loop(), base::MessageLoop::current()); |
| 767 |
| 768 bool event_pending; |
| 769 if (!device_->Poll(poll_device, &event_pending)) { |
| 770 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 771 return; |
| 772 } |
| 773 |
| 774 // All processing should happen on ServiceDeviceTask(), since we shouldn't |
| 775 // touch encoder state from this thread. |
| 776 decoder_thread_proxy_->PostTask( |
| 777 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::ServiceDeviceTask, |
| 778 base::Unretained(this))); |
| 779 } |
| 780 |
| 781 void V4L2SliceVideoDecodeAccelerator::ServiceDeviceTask() { |
| 782 DVLOGF(4); |
| 783 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 784 |
| 785 // ServiceDeviceTask() should only ever be scheduled from DevicePollTask(). |
| 786 |
| 787 Dequeue(); |
| 788 SchedulePollIfNeeded(); |
| 789 } |
| 790 |
| 791 void V4L2SliceVideoDecodeAccelerator::SchedulePollIfNeeded() { |
| 792 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 793 |
| 794 if (!device_poll_thread_.IsRunning()) { |
| 795 DVLOGF(2) << "Device poll thread stopped, will not schedule poll"; |
| 796 return; |
| 797 } |
| 798 |
| 799 DCHECK(input_streamon_ || output_streamon_); |
| 800 |
| 801 if (input_buffer_queued_count_ + output_buffer_queued_count_ == 0) { |
| 802 DVLOGF(4) << "No buffers queued, will not schedule poll"; |
| 803 return; |
| 804 } |
| 805 |
| 806 DVLOGF(4) << "Scheduling device poll task"; |
| 807 |
| 808 device_poll_thread_.message_loop()->PostTask( |
| 809 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DevicePollTask, |
| 810 base::Unretained(this), true)); |
| 811 |
| 812 DVLOGF(2) << "buffer counts: " |
| 813 << "INPUT[" << decoder_input_queue_.size() << "]" |
| 814 << " => DEVICE[" |
| 815 << free_input_buffers_.size() << "+" |
| 816 << input_buffer_queued_count_ << "/" |
| 817 << input_buffer_map_.size() << "]->[" |
| 818 << free_output_buffers_.size() << "+" |
| 819 << output_buffer_queued_count_ << "/" |
| 820 << output_buffer_map_.size() << "]" |
| 821 << " => DISPLAYQ[" << decoder_display_queue_.size() << "]" |
| 822 << " => CLIENT[" << surfaces_at_display_.size() << "]"; |
| 823 } |
| 824 |
| 825 void V4L2SliceVideoDecodeAccelerator::Enqueue( |
| 826 const scoped_refptr<V4L2DecodeSurface>& dec_surface) { |
| 827 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 828 |
| 829 const int old_inputs_queued = input_buffer_queued_count_; |
| 830 const int old_outputs_queued = output_buffer_queued_count_; |
| 831 |
| 832 if (!EnqueueInputRecord(dec_surface->input_record(), |
| 833 dec_surface->config_store())) { |
| 834 DVLOGF(1) << "Failed queueing an input buffer"; |
| 835 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 836 return; |
| 837 } |
| 838 |
| 839 if (!EnqueueOutputRecord(dec_surface->output_record())) { |
| 840 DVLOGF(1) << "Failed queueing an output buffer"; |
| 841 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 842 return; |
| 843 } |
| 844 |
| 845 bool inserted = |
| 846 surfaces_at_device_.insert(std::make_pair(dec_surface->output_record(), |
| 847 dec_surface)).second; |
| 848 DCHECK(inserted); |
| 849 |
| 850 if (old_inputs_queued == 0 && old_outputs_queued == 0) |
| 851 SchedulePollIfNeeded(); |
| 852 } |
| 853 |
| 854 void V4L2SliceVideoDecodeAccelerator::Dequeue() { |
| 855 DVLOGF(3); |
| 856 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 857 |
| 858 struct v4l2_buffer dqbuf; |
| 859 struct v4l2_plane planes[VIDEO_MAX_PLANES]; |
| 860 while (input_buffer_queued_count_ > 0) { |
| 861 DCHECK(input_streamon_); |
| 862 memset(&dqbuf, 0, sizeof(dqbuf)); |
| 863 memset(&planes, 0, sizeof(planes)); |
| 864 dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 865 dqbuf.memory = V4L2_MEMORY_USERPTR; |
| 866 dqbuf.m.planes = planes; |
| 867 dqbuf.length = input_planes_count_; |
| 868 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { |
| 869 if (errno == EAGAIN) { |
| 870 // EAGAIN if we're just out of buffers to dequeue. |
| 871 break; |
| 872 } |
| 873 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; |
| 874 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 875 return; |
| 876 } |
| 877 InputRecord& input_record = input_buffer_map_[dqbuf.index]; |
| 878 DCHECK(input_record.at_device); |
| 879 input_record.at_device = false; |
| 880 input_record.input_id = -1; |
| 881 input_record.bytes_used = 0; |
| 882 free_input_buffers_.push_back(dqbuf.index); |
| 883 input_buffer_queued_count_--; |
| 884 DVLOGF(4) << "Dequeued input=" << dqbuf.index |
| 885 << " count: " << input_buffer_queued_count_; |
| 886 } |
| 887 |
| 888 while (output_buffer_queued_count_ > 0) { |
| 889 DCHECK(output_streamon_); |
| 890 memset(&dqbuf, 0, sizeof(dqbuf)); |
| 891 memset(&planes, 0, sizeof(planes)); |
| 892 dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 893 dqbuf.memory = V4L2_MEMORY_MMAP; |
| 894 dqbuf.m.planes = planes; |
| 895 dqbuf.length = output_planes_count_; |
| 896 if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) { |
| 897 if (errno == EAGAIN) { |
| 898 // EAGAIN if we're just out of buffers to dequeue. |
| 899 break; |
| 900 } |
| 901 PLOG(ERROR) << "ioctl() failed: VIDIOC_DQBUF"; |
| 902 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 903 return; |
| 904 } |
| 905 OutputRecord& output_record = output_buffer_map_[dqbuf.index]; |
| 906 DCHECK(output_record.at_device); |
| 907 output_record.at_device = false; |
| 908 DCHECK_NE(output_record.picture_id, -1); |
| 909 output_buffer_queued_count_--; |
| 910 DVLOGF(3) << "Dequeued output=" << dqbuf.index |
| 911 << " count " << output_buffer_queued_count_; |
| 912 |
| 913 V4L2DecodeSurfaceByOutputId::iterator it = |
| 914 surfaces_at_device_.find(dqbuf.index); |
| 915 if (it == surfaces_at_device_.end()) { |
| 916 DLOG(ERROR) << "Got invalid surface from device."; |
| 917 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 918 } |
| 919 |
| 920 it->second->SetDecoded(); |
| 921 surfaces_at_device_.erase(it); |
| 922 } |
| 923 |
| 924 // A frame was decoded, see if we can output it. |
| 925 TryOutputSurfaces(); |
| 926 |
| 927 ProcessPendingEventsIfNeeded(); |
| 928 } |
| 929 |
| 930 void V4L2SliceVideoDecodeAccelerator::ProcessPendingEventsIfNeeded() { |
| 931 // Process pending events, if any, in the correct order. |
| 932 // We always first process the surface set change, as it is an internal |
| 933 // event from the decoder and interleaving it with external requests would |
| 934 // put the decoder in an undefined state. |
| 935 FinishSurfaceSetChangeIfNeeded(); |
| 936 |
| 937 // Process external (client) requests. |
| 938 FinishFlushIfNeeded(); |
| 939 FinishResetIfNeeded(); |
| 940 } |
| 941 |
| 942 void V4L2SliceVideoDecodeAccelerator::ReuseOutputBuffer(int index) { |
| 943 DCHECK_LT(index, static_cast<int>(output_buffer_map_.size())); |
| 944 DVLOGF(4) << "Reusing output buffer, index=" << index; |
| 945 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 946 |
| 947 OutputRecord& output_record = output_buffer_map_[index]; |
| 948 DCHECK(!output_record.at_device); |
| 949 output_record.at_client = false; |
| 950 |
| 951 free_output_buffers_.push_back(index); |
| 952 |
| 953 ScheduleDecodeBufferTaskIfNeeded(); |
| 954 } |
| 955 |
| 956 bool V4L2SliceVideoDecodeAccelerator::EnqueueInputRecord( |
| 957 int index, |
| 958 uint32_t config_store) { |
| 959 DVLOGF(3); |
| 960 DCHECK_LT(index, static_cast<int>(input_buffer_map_.size())); |
| 961 DCHECK_GT(config_store, 0u); |
| 962 |
| 963 // Enqueue an input (VIDEO_OUTPUT) buffer for an input video frame. |
| 964 InputRecord& input_record = input_buffer_map_[index]; |
| 965 DCHECK(!input_record.at_device); |
| 966 struct v4l2_buffer qbuf; |
| 967 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; |
| 968 memset(&qbuf, 0, sizeof(qbuf)); |
| 969 memset(qbuf_planes, 0, sizeof(qbuf_planes)); |
| 970 qbuf.index = index; |
| 971 qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 972 qbuf.memory = V4L2_MEMORY_MMAP; |
| 973 qbuf.m.planes = qbuf_planes; |
| 974 qbuf.m.planes[0].bytesused = input_record.bytes_used; |
| 975 qbuf.length = input_planes_count_; |
| 976 qbuf.config_store = config_store; |
| 977 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); |
| 978 input_record.at_device = true; |
| 979 input_buffer_queued_count_++; |
| 980 DVLOGF(4) << "Enqueued input=" << qbuf.index |
| 981 << " count: " << input_buffer_queued_count_; |
| 982 |
| 983 return true; |
| 984 } |
| 985 |
| 986 bool V4L2SliceVideoDecodeAccelerator::EnqueueOutputRecord(int index) { |
| 987 DVLOGF(3); |
| 988 DCHECK_LT(index, static_cast<int>(output_buffer_map_.size())); |
| 989 |
| 990 // Enqueue an output (VIDEO_CAPTURE) buffer. |
| 991 OutputRecord& output_record = output_buffer_map_[index]; |
| 992 DCHECK(!output_record.at_device); |
| 993 DCHECK(!output_record.at_client); |
| 994 DCHECK_NE(output_record.egl_image, EGL_NO_IMAGE_KHR); |
| 995 DCHECK_NE(output_record.picture_id, -1); |
| 996 if (output_record.egl_sync != EGL_NO_SYNC_KHR) { |
| 997 // If we have to wait for completion, wait. Note that |
| 998 // free_output_buffers_ is a FIFO queue, so we always wait on the |
| 999 // buffer that has been in the queue the longest. |
| 1000 if (eglClientWaitSyncKHR(egl_display_, output_record.egl_sync, 0, |
| 1001 EGL_FOREVER_KHR) == EGL_FALSE) { |
| 1002 // This will cause tearing, but is safe otherwise. |
| 1003 DVLOGF(1) << "eglClientWaitSyncKHR failed!"; |
| 1004 } |
| 1005 if (eglDestroySyncKHR(egl_display_, output_record.egl_sync) != EGL_TRUE) { |
| 1006 LOGF(ERROR) << "eglDestroySyncKHR failed!"; |
| 1007 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1008 return false; |
| 1009 } |
| 1010 output_record.egl_sync = EGL_NO_SYNC_KHR; |
| 1011 } |
| 1012 |
| 1013 struct v4l2_buffer qbuf; |
| 1014 struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES]; |
| 1015 memset(&qbuf, 0, sizeof(qbuf)); |
| 1016 memset(qbuf_planes, 0, sizeof(qbuf_planes)); |
| 1017 qbuf.index = index; |
| 1018 qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 1019 qbuf.memory = V4L2_MEMORY_MMAP; |
| 1020 qbuf.m.planes = qbuf_planes; |
| 1021 qbuf.length = output_planes_count_; |
| 1022 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf); |
| 1023 output_record.at_device = true; |
| 1024 output_buffer_queued_count_++; |
| 1025 DVLOGF(4) << "Enqueued output=" << qbuf.index |
| 1026 << " count: " << output_buffer_queued_count_; |
| 1027 |
| 1028 return true; |
| 1029 } |
| 1030 |
| 1031 bool V4L2SliceVideoDecodeAccelerator::StartDevicePoll() { |
| 1032 DVLOGF(3) << "Starting device poll"; |
| 1033 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1034 DCHECK(!device_poll_thread_.IsRunning()); |
| 1035 |
| 1036 // Start up the device poll thread and schedule its first DevicePollTask(). |
| 1037 if (!device_poll_thread_.Start()) { |
| 1038 DLOG(ERROR) << "StartDevicePoll(): Device thread failed to start"; |
| 1039 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1040 return false; |
| 1041 } |
| 1042 if (!input_streamon_) { |
| 1043 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 1044 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMON, &type); |
| 1045 input_streamon_ = true; |
| 1046 } |
| 1047 |
| 1048 if (!output_streamon_) { |
| 1049 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 1050 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMON, &type); |
| 1051 output_streamon_ = true; |
| 1052 } |
| 1053 |
| 1054 device_poll_thread_.message_loop()->PostTask( |
| 1055 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DevicePollTask, |
| 1056 base::Unretained(this), true)); |
| 1057 |
| 1058 return true; |
| 1059 } |
| 1060 |
| 1061 bool V4L2SliceVideoDecodeAccelerator::StopDevicePoll(bool keep_input_state) { |
| 1062 DVLOGF(3) << "Stopping device poll"; |
| 1063 if (decoder_thread_.IsRunning()) |
| 1064 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1065 |
| 1066 // Signal the DevicePollTask() to stop, and stop the device poll thread. |
| 1067 if (!device_->SetDevicePollInterrupt()) { |
| 1068 PLOG(ERROR) << "SetDevicePollInterrupt(): failed"; |
| 1069 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1070 return false; |
| 1071 } |
| 1072 device_poll_thread_.Stop(); |
| 1073 DVLOGF(3) << "Device poll thread stopped"; |
| 1074 |
| 1075 // Clear the interrupt now, to be sure. |
| 1076 if (!device_->ClearDevicePollInterrupt()) { |
| 1077 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1078 return false; |
| 1079 } |
| 1080 |
| 1081 if (!keep_input_state) { |
| 1082 if (input_streamon_) { |
| 1083 __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE; |
| 1084 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type); |
| 1085 } |
| 1086 input_streamon_ = false; |
| 1087 } |
| 1088 |
| 1089 if (output_streamon_) { |
| 1090 __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 1091 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type); |
| 1092 } |
| 1093 output_streamon_ = false; |
| 1094 |
| 1095 if (!keep_input_state) { |
| 1096 free_input_buffers_.clear(); |
| 1097 for (size_t i = 0; i < input_buffer_map_.size(); ++i) { |
| 1098 InputRecord& input_record = input_buffer_map_[i]; |
| 1099 input_record.at_device = false; |
| 1100 input_record.bytes_used = 0; |
| 1101 input_record.input_id = -1; |
| 1102 free_input_buffers_.push_back(i); |
| 1103 } |
| 1104 input_buffer_queued_count_ = 0; |
| 1105 } |
| 1106 |
| 1107 surfaces_at_device_.clear(); |
| 1108 |
| 1109 free_output_buffers_.clear(); |
| 1110 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { |
| 1111 OutputRecord& output_record = output_buffer_map_[i]; |
| 1112 DCHECK(!(output_record.at_client && output_record.at_device)); |
| 1113 output_record.at_device = false; |
| 1114 if (!output_record.at_client) |
| 1115 free_output_buffers_.push_back(i); |
| 1116 } |
| 1117 output_buffer_queued_count_ = 0; |
| 1118 |
| 1119 DVLOGF(3) << "Device poll stopped"; |
| 1120 return true; |
| 1121 } |
| 1122 |
| 1123 void V4L2SliceVideoDecodeAccelerator::Decode( |
| 1124 const media::BitstreamBuffer& bitstream_buffer) { |
| 1125 DVLOGF(3) << "input_id=" << bitstream_buffer.id() |
| 1126 << ", size=" << bitstream_buffer.size(); |
| 1127 DCHECK(io_message_loop_proxy_->BelongsToCurrentThread()); |
| 1128 |
| 1129 decoder_thread_proxy_->PostTask( |
| 1130 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DecodeTask, |
| 1131 base::Unretained(this), bitstream_buffer)); |
| 1132 } |
| 1133 |
| 1134 void V4L2SliceVideoDecodeAccelerator::DecodeTask( |
| 1135 const media::BitstreamBuffer& bitstream_buffer) { |
| 1136 DVLOGF(3) << "input_id=" << bitstream_buffer.id() |
| 1137 << " size=" << bitstream_buffer.size(); |
| 1138 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1139 |
| 1140 scoped_ptr<BitstreamBufferRef> bitstream_record(new BitstreamBufferRef( |
| 1141 io_client_, io_message_loop_proxy_, |
| 1142 new base::SharedMemory(bitstream_buffer.handle(), true), |
| 1143 bitstream_buffer.size(), bitstream_buffer.id())); |
| 1144 if (!bitstream_record->shm->Map(bitstream_buffer.size())) { |
| 1145 LOGF(ERROR) << "Could not map bitstream_buffer"; |
| 1146 NOTIFY_ERROR(UNREADABLE_INPUT); |
| 1147 return; |
| 1148 } |
| 1149 DVLOGF(3) << "mapped at=" << bitstream_record->shm->memory(); |
| 1150 |
| 1151 decoder_input_queue_.push( |
| 1152 linked_ptr<BitstreamBufferRef>(bitstream_record.release())); |
| 1153 |
| 1154 ScheduleDecodeBufferTaskIfNeeded(); |
| 1155 } |
| 1156 |
| 1157 bool V4L2SliceVideoDecodeAccelerator::TrySetNewBistreamBuffer() { |
| 1158 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1159 DCHECK(!decoder_current_bitstream_buffer_); |
| 1160 |
| 1161 if (decoder_input_queue_.empty()) |
| 1162 return false; |
| 1163 |
| 1164 decoder_current_bitstream_buffer_.reset( |
| 1165 decoder_input_queue_.front().release()); |
| 1166 decoder_input_queue_.pop(); |
| 1167 |
| 1168 if (decoder_current_bitstream_buffer_->input_id == kFlushBufferId) { |
| 1169 // This is a buffer we queued for ourselves to trigger flush at this time. |
| 1170 InitiateFlush(); |
| 1171 return false; |
| 1172 } |
| 1173 |
| 1174 const uint8_t* const data = reinterpret_cast<const uint8_t*>( |
| 1175 decoder_current_bitstream_buffer_->shm->memory()); |
| 1176 const size_t data_size = decoder_current_bitstream_buffer_->size; |
| 1177 decoder_->SetStream(data, data_size); |
| 1178 |
| 1179 return true; |
| 1180 } |
| 1181 |
| 1182 void V4L2SliceVideoDecodeAccelerator::ScheduleDecodeBufferTaskIfNeeded() { |
| 1183 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1184 if (state_ == kDecoding) { |
| 1185 decoder_thread_proxy_->PostTask( |
| 1186 FROM_HERE, |
| 1187 base::Bind(&V4L2SliceVideoDecodeAccelerator::DecodeBufferTask, |
| 1188 base::Unretained(this))); |
| 1189 } |
| 1190 } |
| 1191 |
| 1192 void V4L2SliceVideoDecodeAccelerator::DecodeBufferTask() { |
| 1193 DVLOGF(3); |
| 1194 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1195 |
| 1196 if (state_ != kDecoding) { |
| 1197 DVLOGF(3) << "Early exit, not in kDecoding"; |
| 1198 return; |
| 1199 } |
| 1200 |
| 1201 while (true) { |
| 1202 AcceleratedVideoDecoder::DecodeResult res; |
| 1203 res = decoder_->Decode(); |
| 1204 switch (res) { |
| 1205 case AcceleratedVideoDecoder::kAllocateNewSurfaces: |
| 1206 DVLOGF(2) << "Decoder requesting a new set of surfaces"; |
| 1207 InitiateSurfaceSetChange(); |
| 1208 return; |
| 1209 |
| 1210 case AcceleratedVideoDecoder::kRanOutOfStreamData: |
| 1211 decoder_current_bitstream_buffer_.reset(); |
| 1212 if (!TrySetNewBistreamBuffer()) |
| 1213 return; |
| 1214 |
| 1215 break; |
| 1216 |
| 1217 case AcceleratedVideoDecoder::kRanOutOfSurfaces: |
| 1218 // No more surfaces for the decoder, we'll come back once we have more. |
| 1219 DVLOGF(4) << "Ran out of surfaces"; |
| 1220 return; |
| 1221 |
| 1222 case AcceleratedVideoDecoder::kDecodeError: |
| 1223 DVLOGF(1) << "Error decoding stream"; |
| 1224 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1225 return; |
| 1226 } |
| 1227 } |
| 1228 } |
| 1229 |
| 1230 void V4L2SliceVideoDecodeAccelerator::InitiateSurfaceSetChange() { |
| 1231 DVLOGF(2); |
| 1232 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1233 |
| 1234 DCHECK_EQ(state_, kDecoding); |
| 1235 state_ = kIdle; |
| 1236 |
| 1237 DCHECK(!surface_set_change_pending_); |
| 1238 surface_set_change_pending_ = true; |
| 1239 |
| 1240 FinishSurfaceSetChangeIfNeeded(); |
| 1241 } |
| 1242 |
| 1243 void V4L2SliceVideoDecodeAccelerator::FinishSurfaceSetChangeIfNeeded() { |
| 1244 DVLOGF(2); |
| 1245 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1246 |
| 1247 if (!surface_set_change_pending_ || !surfaces_at_device_.empty()) |
| 1248 return; |
| 1249 |
| 1250 DCHECK_EQ(state_, kIdle); |
| 1251 DCHECK(decoder_display_queue_.empty()); |
| 1252 |
| 1253 // Keep input queue running while we switch outputs. |
| 1254 if (!StopDevicePoll(true)) { |
| 1255 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1256 return; |
| 1257 } |
| 1258 |
| 1259 // This will return only once all buffers are dismissed and destroyed. |
| 1260 // This does not wait until they are displayed however, as display retains |
| 1261 // references to the buffers bound to textures and will release them |
| 1262 // after displaying. |
| 1263 if (!DestroyOutputs(true)) { |
| 1264 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1265 return; |
| 1266 } |
| 1267 |
| 1268 if (!CreateOutputBuffers()) { |
| 1269 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1270 return; |
| 1271 } |
| 1272 |
| 1273 if (!StartDevicePoll()) { |
| 1274 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1275 return; |
| 1276 } |
| 1277 |
| 1278 DVLOGF(3) << "Surface set change finished"; |
| 1279 |
| 1280 surface_set_change_pending_ = false; |
| 1281 state_ = kDecoding; |
| 1282 ScheduleDecodeBufferTaskIfNeeded(); |
| 1283 } |
| 1284 |
| 1285 bool V4L2SliceVideoDecodeAccelerator::DestroyOutputs(bool dismiss) { |
| 1286 DVLOGF(3); |
| 1287 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1288 std::vector<EGLImageKHR> egl_images_to_destroy; |
| 1289 std::vector<int32> picture_buffers_to_dismiss; |
| 1290 |
| 1291 if (output_buffer_map_.empty()) |
| 1292 return true; |
| 1293 |
| 1294 for (auto output_record : output_buffer_map_) { |
| 1295 DCHECK(!output_record.at_device); |
| 1296 output_record.at_client = false; |
| 1297 |
| 1298 if (output_record.egl_sync != EGL_NO_SYNC_KHR) { |
| 1299 if (eglDestroySyncKHR(egl_display_, output_record.egl_sync) != EGL_TRUE) |
| 1300 DVLOGF(1) << "eglDestroySyncKHR failed."; |
| 1301 } |
| 1302 |
| 1303 if (output_record.egl_image != EGL_NO_IMAGE_KHR) { |
| 1304 child_message_loop_proxy_->PostTask( |
| 1305 FROM_HERE, |
| 1306 base::Bind(base::IgnoreResult(&V4L2Device::DestroyEGLImage), device_, |
| 1307 egl_display_, output_record.egl_image)); |
| 1308 } |
| 1309 |
| 1310 picture_buffers_to_dismiss.push_back(output_record.picture_id); |
| 1311 } |
| 1312 |
| 1313 if (dismiss) { |
| 1314 DVLOGF(2) << "Scheduling picture dismissal"; |
| 1315 base::WaitableEvent done(false, false); |
| 1316 child_message_loop_proxy_->PostTask( |
| 1317 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DismissPictures, |
| 1318 weak_this_, picture_buffers_to_dismiss, &done)); |
| 1319 done.Wait(); |
| 1320 } |
| 1321 |
| 1322 // At this point client can't call ReusePictureBuffer on any of the pictures |
| 1323 // anymore, so it's safe to destroy. |
| 1324 return DestroyOutputBuffers(); |
| 1325 } |
| 1326 |
| 1327 bool V4L2SliceVideoDecodeAccelerator::DestroyOutputBuffers() { |
| 1328 DVLOGF(3); |
| 1329 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread() || |
| 1330 !decoder_thread_.IsRunning()); |
| 1331 DCHECK(!output_streamon_); |
| 1332 DCHECK(surfaces_at_device_.empty()); |
| 1333 DCHECK(decoder_display_queue_.empty()); |
| 1334 DCHECK_EQ(surfaces_at_display_.size() + free_output_buffers_.size(), |
| 1335 output_buffer_map_.size()); |
| 1336 |
| 1337 if (output_buffer_map_.empty()) |
| 1338 return true; |
| 1339 |
| 1340 // It's ok to do this, client will retain references to textures, but we are |
| 1341 // not interested in reusing the surfaces anymore. |
| 1342 // This will prevent us from reusing old surfaces in case we have some |
| 1343 // ReusePictureBuffer() pending on ChildThread already. It's ok to ignore |
| 1344 // them, because we have already dismissed them (in DestroyOutputs()). |
| 1345 surfaces_at_display_.clear(); |
| 1346 DCHECK_EQ(free_output_buffers_.size(), output_buffer_map_.size()); |
| 1347 |
| 1348 free_output_buffers_.clear(); |
| 1349 output_buffer_map_.clear(); |
| 1350 |
| 1351 struct v4l2_requestbuffers reqbufs; |
| 1352 memset(&reqbufs, 0, sizeof(reqbufs)); |
| 1353 reqbufs.count = 0; |
| 1354 reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE; |
| 1355 reqbufs.memory = V4L2_MEMORY_MMAP; |
| 1356 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs); |
| 1357 |
| 1358 return true; |
| 1359 } |
| 1360 |
| 1361 void V4L2SliceVideoDecodeAccelerator::AssignPictureBuffers( |
| 1362 const std::vector<media::PictureBuffer>& buffers) { |
| 1363 DVLOGF(3); |
| 1364 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 1365 |
| 1366 if (buffers.size() != output_buffer_map_.size()) { |
| 1367 DLOG(ERROR) << "Failed to provide requested picture buffers. " |
| 1368 << "(Got " << buffers.size() |
| 1369 << ", requested " << output_buffer_map_.size() << ")"; |
| 1370 NOTIFY_ERROR(INVALID_ARGUMENT); |
| 1371 return; |
| 1372 } |
| 1373 |
| 1374 if (!make_context_current_.Run()) { |
| 1375 DLOG(ERROR) << "could not make context current"; |
| 1376 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1377 return; |
| 1378 } |
| 1379 |
| 1380 gfx::ScopedTextureBinder bind_restore(GL_TEXTURE_EXTERNAL_OES, 0); |
| 1381 |
| 1382 // It's safe to manipulate all the buffer state here, because the decoder |
| 1383 // thread is waiting on pictures_assigned_. |
| 1384 DCHECK(free_output_buffers_.empty()); |
| 1385 for (size_t i = 0; i < output_buffer_map_.size(); ++i) { |
| 1386 DCHECK(buffers[i].size() == frame_buffer_size_); |
| 1387 |
| 1388 OutputRecord& output_record = output_buffer_map_[i]; |
| 1389 DCHECK(!output_record.at_device); |
| 1390 DCHECK(!output_record.at_client); |
| 1391 DCHECK_EQ(output_record.egl_image, EGL_NO_IMAGE_KHR); |
| 1392 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); |
| 1393 DCHECK_EQ(output_record.picture_id, -1); |
| 1394 DCHECK_EQ(output_record.cleared, false); |
| 1395 |
| 1396 EGLImageKHR egl_image = device_->CreateEGLImage(egl_display_, |
| 1397 egl_context_, |
| 1398 buffers[i].texture_id(), |
| 1399 frame_buffer_size_, |
| 1400 i, |
| 1401 output_format_fourcc_, |
| 1402 output_planes_count_); |
| 1403 if (egl_image == EGL_NO_IMAGE_KHR) { |
| 1404 LOGF(ERROR) << "Could not create EGLImageKHR"; |
| 1405 // Ownership of EGLImages allocated in previous iterations of this loop |
| 1406 // has been transferred to output_buffer_map_. After we error-out here |
| 1407 // the destructor will handle their cleanup. |
| 1408 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1409 return; |
| 1410 } |
| 1411 |
| 1412 output_record.egl_image = egl_image; |
| 1413 output_record.picture_id = buffers[i].id(); |
| 1414 free_output_buffers_.push_back(i); |
| 1415 DVLOGF(3) << "buffer[" << i << "]: picture_id=" << output_record.picture_id; |
| 1416 } |
| 1417 |
| 1418 pictures_assigned_.Signal(); |
| 1419 } |
| 1420 |
| 1421 void V4L2SliceVideoDecodeAccelerator::ReusePictureBuffer( |
| 1422 int32 picture_buffer_id) { |
| 1423 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 1424 DVLOGF(4) << "picture_buffer_id=" << picture_buffer_id; |
| 1425 |
| 1426 if (!make_context_current_.Run()) { |
| 1427 LOGF(ERROR) << "could not make context current"; |
| 1428 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1429 return; |
| 1430 } |
| 1431 |
| 1432 EGLSyncKHR egl_sync = |
| 1433 eglCreateSyncKHR(egl_display_, EGL_SYNC_FENCE_KHR, NULL); |
| 1434 if (egl_sync == EGL_NO_SYNC_KHR) { |
| 1435 LOGF(ERROR) << "eglCreateSyncKHR() failed"; |
| 1436 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1437 return; |
| 1438 } |
| 1439 |
| 1440 scoped_ptr<EGLSyncKHRRef> egl_sync_ref( |
| 1441 new EGLSyncKHRRef(egl_display_, egl_sync)); |
| 1442 decoder_thread_proxy_->PostTask( |
| 1443 FROM_HERE, |
| 1444 base::Bind(&V4L2SliceVideoDecodeAccelerator::ReusePictureBufferTask, |
| 1445 base::Unretained(this), picture_buffer_id, |
| 1446 base::Passed(&egl_sync_ref))); |
| 1447 } |
| 1448 |
| 1449 void V4L2SliceVideoDecodeAccelerator::ReusePictureBufferTask( |
| 1450 int32 picture_buffer_id, |
| 1451 scoped_ptr<EGLSyncKHRRef> egl_sync_ref) { |
| 1452 DVLOGF(3) << "picture_buffer_id=" << picture_buffer_id; |
| 1453 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1454 |
| 1455 V4L2DecodeSurfaceByPictureBufferId::iterator it = |
| 1456 surfaces_at_display_.find(picture_buffer_id); |
| 1457 if (it == surfaces_at_display_.end()) { |
| 1458 // It's possible that we've already posted a DismissPictureBuffer for this |
| 1459 // picture, but it has not yet executed when this ReusePictureBuffer was |
| 1460 // posted to us by the client. In that case just ignore this (we've already |
| 1461 // dismissed it and accounted for that) and let the sync object get |
| 1462 // destroyed. |
| 1463 DVLOGF(3) << "got picture id=" << picture_buffer_id |
| 1464 << " not in use (anymore?)."; |
| 1465 return; |
| 1466 } |
| 1467 |
| 1468 OutputRecord& output_record = output_buffer_map_[it->second->output_record()]; |
| 1469 if (output_record.at_device || !output_record.at_client) { |
| 1470 DVLOGF(1) << "picture_buffer_id not reusable"; |
| 1471 NOTIFY_ERROR(INVALID_ARGUMENT); |
| 1472 return; |
| 1473 } |
| 1474 |
| 1475 DCHECK_EQ(output_record.egl_sync, EGL_NO_SYNC_KHR); |
| 1476 DCHECK(!output_record.at_device); |
| 1477 output_record.at_client = false; |
| 1478 output_record.egl_sync = egl_sync_ref->egl_sync; |
| 1479 // Take ownership of the EGLSync. |
| 1480 egl_sync_ref->egl_sync = EGL_NO_SYNC_KHR; |
| 1481 surfaces_at_display_.erase(it); |
| 1482 } |
| 1483 |
| 1484 void V4L2SliceVideoDecodeAccelerator::Flush() { |
| 1485 DVLOGF(3); |
| 1486 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 1487 |
| 1488 decoder_thread_proxy_->PostTask( |
| 1489 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::FlushTask, |
| 1490 base::Unretained(this))); |
| 1491 } |
| 1492 |
| 1493 void V4L2SliceVideoDecodeAccelerator::FlushTask() { |
| 1494 DVLOGF(3); |
| 1495 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1496 |
| 1497 if (!decoder_input_queue_.empty()) { |
| 1498 // We are not done with pending inputs, so queue an empty buffer, |
| 1499 // which - when reached - will trigger flush sequence. |
| 1500 decoder_input_queue_.push( |
| 1501 linked_ptr<BitstreamBufferRef>(new BitstreamBufferRef( |
| 1502 io_client_, io_message_loop_proxy_, nullptr, 0, kFlushBufferId))); |
| 1503 return; |
| 1504 } |
| 1505 |
| 1506 // No more inputs pending, so just finish flushing here. |
| 1507 InitiateFlush(); |
| 1508 } |
| 1509 |
| 1510 void V4L2SliceVideoDecodeAccelerator::InitiateFlush() { |
| 1511 DVLOGF(3); |
| 1512 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1513 |
| 1514 DCHECK(!decoder_flushing_); |
| 1515 DCHECK_EQ(state_, kDecoding); |
| 1516 state_ = kIdle; |
| 1517 |
| 1518 // This will trigger output for all remaining surfaces in the decoder. |
| 1519 // However, not all of them may be decoded yet (they would be queued |
| 1520 // in hardware then). |
| 1521 if (!decoder_->Flush()) { |
| 1522 DVLOGF(1) << "Failed flushing the decoder."; |
| 1523 NOTIFY_ERROR(PLATFORM_FAILURE); |
| 1524 return; |
| 1525 } |
| 1526 |
| 1527 // Put the decoder in an idle state, ready to resume. |
| 1528 decoder_->Reset(); |
| 1529 |
| 1530 decoder_flushing_ = true; |
| 1531 |
| 1532 decoder_thread_proxy_->PostTask( |
| 1533 FROM_HERE, |
| 1534 base::Bind(&V4L2SliceVideoDecodeAccelerator::FinishFlushIfNeeded, |
| 1535 base::Unretained(this))); |
| 1536 } |
| 1537 |
| 1538 void V4L2SliceVideoDecodeAccelerator::FinishFlushIfNeeded() { |
| 1539 DVLOGF(3); |
| 1540 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1541 |
| 1542 if (!decoder_flushing_ || !surfaces_at_device_.empty()) |
| 1543 return; |
| 1544 |
| 1545 DCHECK_EQ(state_, kIdle); |
| 1546 |
| 1547 // At this point, all remaining surfaces are decoded and dequeued, and since |
| 1548 // we have already scheduled output for them in InitiateFlush(), their |
| 1549 // respective PictureReady calls have been posted (or they have been queued on |
| 1550 // pending_picture_ready_). So at this time, once we SendPictureReady(), |
| 1551 // we will have all remaining PictureReady() posted to the client and we |
| 1552 // can post NotifyFlushDone(). |
| 1553 DCHECK(decoder_display_queue_.empty()); |
| 1554 SendPictureReady(); |
| 1555 |
| 1556 child_message_loop_proxy_->PostTask( |
| 1557 FROM_HERE, base::Bind(&Client::NotifyFlushDone, client_)); |
| 1558 |
| 1559 decoder_flushing_ = false; |
| 1560 |
| 1561 DVLOGF(3) << "Flush finished"; |
| 1562 state_ = kDecoding; |
| 1563 ScheduleDecodeBufferTaskIfNeeded(); |
| 1564 } |
| 1565 |
| 1566 void V4L2SliceVideoDecodeAccelerator::Reset() { |
| 1567 DVLOGF(3); |
| 1568 DCHECK(child_message_loop_proxy_->BelongsToCurrentThread()); |
| 1569 |
| 1570 decoder_thread_proxy_->PostTask( |
| 1571 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::ResetTask, |
| 1572 base::Unretained(this))); |
| 1573 } |
| 1574 |
| 1575 void V4L2SliceVideoDecodeAccelerator::ResetTask() { |
| 1576 DVLOGF(3); |
| 1577 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1578 |
| 1579 if (decoder_resetting_) { |
| 1580 // This is a bug in the client, multiple Reset()s before NotifyResetDone() |
| 1581 // are not allowed. |
| 1582 NOTREACHED() << "Client should not be requesting multiple Reset()s"; |
| 1583 return; |
| 1584 } |
| 1585 |
| 1586 DCHECK_EQ(state_, kDecoding); |
| 1587 state_ = kIdle; |
| 1588 |
| 1589 // Put the decoder in an idle state, ready to resume. |
| 1590 decoder_->Reset(); |
| 1591 |
| 1592 decoder_resetting_ = true; |
| 1593 |
| 1594 // Drop all remaining inputs. |
| 1595 decoder_current_bitstream_buffer_.reset(); |
| 1596 while (!decoder_input_queue_.empty()) |
| 1597 decoder_input_queue_.pop(); |
| 1598 |
| 1599 FinishResetIfNeeded(); |
| 1600 } |
| 1601 |
| 1602 void V4L2SliceVideoDecodeAccelerator::FinishResetIfNeeded() { |
| 1603 DVLOGF(3); |
| 1604 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1605 |
| 1606 if (!decoder_resetting_ || !surfaces_at_device_.empty()) |
| 1607 return; |
| 1608 |
| 1609 DCHECK_EQ(state_, kIdle); |
| 1610 DCHECK(!decoder_flushing_); |
| 1611 SendPictureReady(); |
| 1612 |
| 1613 // Drop any pending outputs. |
| 1614 while (!decoder_display_queue_.empty()) |
| 1615 decoder_display_queue_.pop(); |
| 1616 |
| 1617 decoder_resetting_ = false; |
| 1618 |
| 1619 child_message_loop_proxy_->PostTask( |
| 1620 FROM_HERE, base::Bind(&Client::NotifyResetDone, client_)); |
| 1621 |
| 1622 DVLOGF(3) << "Reset finished"; |
| 1623 |
| 1624 state_ = kDecoding; |
| 1625 ScheduleDecodeBufferTaskIfNeeded(); |
| 1626 } |
| 1627 |
| 1628 void V4L2SliceVideoDecodeAccelerator::SetErrorState(Error error) { |
| 1629 // We can touch decoder_state_ only if this is the decoder thread or the |
| 1630 // decoder thread isn't running. |
| 1631 if (decoder_thread_.IsRunning() && |
| 1632 !decoder_thread_proxy_->BelongsToCurrentThread()) { |
| 1633 decoder_thread_proxy_->PostTask( |
| 1634 FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::SetErrorState, |
| 1635 base::Unretained(this), error)); |
| 1636 return; |
| 1637 } |
| 1638 |
| 1639 // Post NotifyError only if we are already initialized, as the API does |
| 1640 // not allow doing so before that. |
| 1641 if (state_ != kError && state_ != kUninitialized) |
| 1642 NotifyError(error); |
| 1643 |
| 1644 state_ = kError; |
| 1645 } |
| 1646 |
| 1647 V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::V4L2H264Accelerator( |
| 1648 V4L2SliceVideoDecodeAccelerator* v4l2_dec) |
| 1649 : num_slices_(0), v4l2_dec_(v4l2_dec) { |
| 1650 DCHECK(v4l2_dec_); |
| 1651 } |
| 1652 |
| 1653 V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::~V4L2H264Accelerator() { |
| 1654 } |
| 1655 |
| 1656 scoped_refptr<H264Picture> |
| 1657 V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::CreateH264Picture() { |
| 1658 scoped_refptr<V4L2DecodeSurface> dec_surface = v4l2_dec_->CreateSurface(); |
| 1659 if (!dec_surface) |
| 1660 return nullptr; |
| 1661 |
| 1662 return new V4L2H264Picture(dec_surface); |
| 1663 } |
| 1664 |
| 1665 void V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator:: |
| 1666 H264PictureListToDPBIndicesList(const H264Picture::Vector& src_pic_list, |
| 1667 uint8_t dst_list[kDPBIndicesListSize]) { |
| 1668 size_t i; |
| 1669 for (i = 0; i < src_pic_list.size() && i < kDPBIndicesListSize; ++i) { |
| 1670 const scoped_refptr<H264Picture>& pic = src_pic_list[i]; |
| 1671 dst_list[i] = pic ? pic->dpb_position : VIDEO_MAX_FRAME; |
| 1672 } |
| 1673 |
| 1674 while (i < kDPBIndicesListSize) |
| 1675 dst_list[i++] = VIDEO_MAX_FRAME; |
| 1676 } |
| 1677 |
| 1678 void V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::H264DPBToV4L2DPB( |
| 1679 const H264DPB& dpb, |
| 1680 std::vector<scoped_refptr<V4L2DecodeSurface>>* ref_surfaces) { |
| 1681 memset(v4l2_decode_param_.dpb, 0, sizeof(v4l2_decode_param_.dpb)); |
| 1682 size_t i = 0; |
| 1683 for (const auto& pic : dpb) { |
| 1684 if (i >= arraysize(v4l2_decode_param_.dpb)) { |
| 1685 DVLOG(1) << "Invalid DPB size"; |
| 1686 break; |
| 1687 } |
| 1688 struct v4l2_h264_dpb_entry& entry = v4l2_decode_param_.dpb[i++]; |
| 1689 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 1690 H264PictureToV4L2DecodeSurface(pic); |
| 1691 entry.buf_index = dec_surface->output_record(); |
| 1692 entry.frame_num = pic->frame_num; |
| 1693 entry.pic_num = pic->pic_num; |
| 1694 entry.top_field_order_cnt = pic->top_field_order_cnt; |
| 1695 entry.bottom_field_order_cnt = pic->bottom_field_order_cnt; |
| 1696 entry.flags = (pic->ref ? V4L2_H264_DPB_ENTRY_FLAG_ACTIVE : 0) | |
| 1697 (pic->long_term ? V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM : 0); |
| 1698 |
| 1699 ref_surfaces->push_back(dec_surface); |
| 1700 } |
| 1701 } |
| 1702 |
| 1703 bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::SubmitFrameMetadata( |
| 1704 const media::H264SPS* sps, |
| 1705 const media::H264PPS* pps, |
| 1706 const H264DPB& dpb, |
| 1707 const H264Picture::Vector& ref_pic_listp0, |
| 1708 const H264Picture::Vector& ref_pic_listb0, |
| 1709 const H264Picture::Vector& ref_pic_listb1, |
| 1710 const scoped_refptr<H264Picture>& pic) { |
| 1711 struct v4l2_ext_control ctrl; |
| 1712 std::vector<struct v4l2_ext_control> ctrls; |
| 1713 |
| 1714 struct v4l2_ctrl_h264_sps v4l2_sps; |
| 1715 memset(&v4l2_sps, 0, sizeof(v4l2_sps)); |
| 1716 v4l2_sps.constraint_set_flags = |
| 1717 sps->constraint_set0_flag ? V4L2_H264_SPS_CONSTRAINT_SET0_FLAG : 0 | |
| 1718 sps->constraint_set1_flag ? V4L2_H264_SPS_CONSTRAINT_SET1_FLAG : 0 | |
| 1719 sps->constraint_set2_flag ? V4L2_H264_SPS_CONSTRAINT_SET2_FLAG : 0 | |
| 1720 sps->constraint_set3_flag ? V4L2_H264_SPS_CONSTRAINT_SET3_FLAG : 0 | |
| 1721 sps->constraint_set4_flag ? V4L2_H264_SPS_CONSTRAINT_SET4_FLAG : 0 | |
| 1722 sps->constraint_set5_flag ? V4L2_H264_SPS_CONSTRAINT_SET5_FLAG : 0; |
| 1723 #define SPS_TO_V4L2SPS(a) v4l2_sps.a = sps->a |
| 1724 SPS_TO_V4L2SPS(profile_idc); |
| 1725 SPS_TO_V4L2SPS(level_idc); |
| 1726 SPS_TO_V4L2SPS(seq_parameter_set_id); |
| 1727 SPS_TO_V4L2SPS(chroma_format_idc); |
| 1728 SPS_TO_V4L2SPS(bit_depth_luma_minus8); |
| 1729 SPS_TO_V4L2SPS(bit_depth_chroma_minus8); |
| 1730 SPS_TO_V4L2SPS(log2_max_frame_num_minus4); |
| 1731 SPS_TO_V4L2SPS(pic_order_cnt_type); |
| 1732 SPS_TO_V4L2SPS(log2_max_pic_order_cnt_lsb_minus4); |
| 1733 SPS_TO_V4L2SPS(offset_for_non_ref_pic); |
| 1734 SPS_TO_V4L2SPS(offset_for_top_to_bottom_field); |
| 1735 SPS_TO_V4L2SPS(num_ref_frames_in_pic_order_cnt_cycle); |
| 1736 |
| 1737 static_assert(arraysize(v4l2_sps.offset_for_ref_frame) == |
| 1738 arraysize(sps->offset_for_ref_frame), |
| 1739 "offset_for_ref_frame arrays must be same size"); |
| 1740 for (size_t i = 0; i < arraysize(v4l2_sps.offset_for_ref_frame); ++i) |
| 1741 v4l2_sps.offset_for_ref_frame[i] = sps->offset_for_ref_frame[i]; |
| 1742 SPS_TO_V4L2SPS(max_num_ref_frames); |
| 1743 SPS_TO_V4L2SPS(pic_width_in_mbs_minus1); |
| 1744 SPS_TO_V4L2SPS(pic_height_in_map_units_minus1); |
| 1745 #undef SPS_TO_V4L2SPS |
| 1746 |
| 1747 #define SET_V4L2_SPS_FLAG_IF(cond, flag) \ |
| 1748 v4l2_sps.flags |= ((sps->cond) ? (flag) : 0) |
| 1749 SET_V4L2_SPS_FLAG_IF(separate_colour_plane_flag, |
| 1750 V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE); |
| 1751 SET_V4L2_SPS_FLAG_IF(qpprime_y_zero_transform_bypass_flag, |
| 1752 V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS); |
| 1753 SET_V4L2_SPS_FLAG_IF(delta_pic_order_always_zero_flag, |
| 1754 V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO); |
| 1755 SET_V4L2_SPS_FLAG_IF(gaps_in_frame_num_value_allowed_flag, |
| 1756 V4L2_H264_SPS_FLAG_GAPS_IN_FRAME_NUM_VALUE_ALLOWED); |
| 1757 SET_V4L2_SPS_FLAG_IF(frame_mbs_only_flag, V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY); |
| 1758 SET_V4L2_SPS_FLAG_IF(mb_adaptive_frame_field_flag, |
| 1759 V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD); |
| 1760 SET_V4L2_SPS_FLAG_IF(direct_8x8_inference_flag, |
| 1761 V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE); |
| 1762 #undef SET_FLAG |
| 1763 memset(&ctrl, 0, sizeof(ctrl)); |
| 1764 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SPS; |
| 1765 ctrl.size = sizeof(v4l2_sps); |
| 1766 ctrl.p_h264_sps = &v4l2_sps; |
| 1767 ctrls.push_back(ctrl); |
| 1768 |
| 1769 struct v4l2_ctrl_h264_pps v4l2_pps; |
| 1770 memset(&v4l2_pps, 0, sizeof(v4l2_pps)); |
| 1771 #define PPS_TO_V4L2PPS(a) v4l2_pps.a = pps->a |
| 1772 PPS_TO_V4L2PPS(pic_parameter_set_id); |
| 1773 PPS_TO_V4L2PPS(seq_parameter_set_id); |
| 1774 PPS_TO_V4L2PPS(num_slice_groups_minus1); |
| 1775 PPS_TO_V4L2PPS(num_ref_idx_l0_default_active_minus1); |
| 1776 PPS_TO_V4L2PPS(num_ref_idx_l1_default_active_minus1); |
| 1777 PPS_TO_V4L2PPS(weighted_bipred_idc); |
| 1778 PPS_TO_V4L2PPS(pic_init_qp_minus26); |
| 1779 PPS_TO_V4L2PPS(pic_init_qs_minus26); |
| 1780 PPS_TO_V4L2PPS(chroma_qp_index_offset); |
| 1781 PPS_TO_V4L2PPS(second_chroma_qp_index_offset); |
| 1782 #undef PPS_TO_V4L2PPS |
| 1783 |
| 1784 #define SET_V4L2_PPS_FLAG_IF(cond, flag) \ |
| 1785 v4l2_pps.flags |= ((pps->cond) ? (flag) : 0) |
| 1786 SET_V4L2_PPS_FLAG_IF(entropy_coding_mode_flag, |
| 1787 V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE); |
| 1788 SET_V4L2_PPS_FLAG_IF( |
| 1789 bottom_field_pic_order_in_frame_present_flag, |
| 1790 V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT); |
| 1791 SET_V4L2_PPS_FLAG_IF(weighted_pred_flag, V4L2_H264_PPS_FLAG_WEIGHTED_PRED); |
| 1792 SET_V4L2_PPS_FLAG_IF(deblocking_filter_control_present_flag, |
| 1793 V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT); |
| 1794 SET_V4L2_PPS_FLAG_IF(constrained_intra_pred_flag, |
| 1795 V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED); |
| 1796 SET_V4L2_PPS_FLAG_IF(redundant_pic_cnt_present_flag, |
| 1797 V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT); |
| 1798 SET_V4L2_PPS_FLAG_IF(transform_8x8_mode_flag, |
| 1799 V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE); |
| 1800 SET_V4L2_PPS_FLAG_IF(pic_scaling_matrix_present_flag, |
| 1801 V4L2_H264_PPS_FLAG_PIC_SCALING_MATRIX_PRESENT); |
| 1802 #undef SET_V4L2_PPS_FLAG_IF |
| 1803 memset(&ctrl, 0, sizeof(ctrl)); |
| 1804 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_PPS; |
| 1805 ctrl.size = sizeof(v4l2_pps); |
| 1806 ctrl.p_h264_pps = &v4l2_pps; |
| 1807 ctrls.push_back(ctrl); |
| 1808 |
| 1809 struct v4l2_ctrl_h264_scaling_matrix v4l2_scaling_matrix; |
| 1810 memset(&v4l2_scaling_matrix, 0, sizeof(v4l2_scaling_matrix)); |
| 1811 static_assert(arraysize(v4l2_scaling_matrix.scaling_list_4x4) <= |
| 1812 arraysize(pps->scaling_list4x4) && |
| 1813 arraysize(v4l2_scaling_matrix.scaling_list_4x4[0]) <= |
| 1814 arraysize(pps->scaling_list4x4[0]) && |
| 1815 arraysize(v4l2_scaling_matrix.scaling_list_8x8) <= |
| 1816 arraysize(pps->scaling_list8x8) && |
| 1817 arraysize(v4l2_scaling_matrix.scaling_list_8x8[0]) <= |
| 1818 arraysize(pps->scaling_list8x8[0]), |
| 1819 "scaling_lists must be of correct size"); |
| 1820 for (size_t i = 0; i < arraysize(v4l2_scaling_matrix.scaling_list_4x4); ++i) { |
| 1821 for (size_t j = 0; j < arraysize(v4l2_scaling_matrix.scaling_list_4x4[i]); |
| 1822 ++j) { |
| 1823 v4l2_scaling_matrix.scaling_list_4x4[i][j] = pps->scaling_list4x4[i][j]; |
| 1824 } |
| 1825 } |
| 1826 for (size_t i = 0; i < arraysize(v4l2_scaling_matrix.scaling_list_8x8); ++i) { |
| 1827 for (size_t j = 0; j < arraysize(v4l2_scaling_matrix.scaling_list_8x8[i]); |
| 1828 ++j) { |
| 1829 v4l2_scaling_matrix.scaling_list_8x8[i][j] = pps->scaling_list8x8[i][j]; |
| 1830 } |
| 1831 } |
| 1832 memset(&ctrl, 0, sizeof(ctrl)); |
| 1833 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX; |
| 1834 ctrl.size = sizeof(v4l2_scaling_matrix); |
| 1835 ctrl.p_h264_scal_mtrx = &v4l2_scaling_matrix; |
| 1836 ctrls.push_back(ctrl); |
| 1837 |
| 1838 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 1839 H264PictureToV4L2DecodeSurface(pic); |
| 1840 |
| 1841 struct v4l2_ext_controls ext_ctrls; |
| 1842 memset(&ext_ctrls, 0, sizeof(ext_ctrls)); |
| 1843 ext_ctrls.count = ctrls.size(); |
| 1844 ext_ctrls.controls = &ctrls[0]; |
| 1845 ext_ctrls.config_store = dec_surface->config_store(); |
| 1846 v4l2_dec_->SubmitExtControls(&ext_ctrls); |
| 1847 |
| 1848 H264PictureListToDPBIndicesList(ref_pic_listp0, |
| 1849 v4l2_decode_param_.ref_pic_list_p0); |
| 1850 H264PictureListToDPBIndicesList(ref_pic_listb0, |
| 1851 v4l2_decode_param_.ref_pic_list_b0); |
| 1852 H264PictureListToDPBIndicesList(ref_pic_listb1, |
| 1853 v4l2_decode_param_.ref_pic_list_b1); |
| 1854 |
| 1855 std::vector<scoped_refptr<V4L2DecodeSurface>> ref_surfaces; |
| 1856 H264DPBToV4L2DPB(dpb, &ref_surfaces); |
| 1857 dec_surface->SetReferenceSurfaces(ref_surfaces); |
| 1858 |
| 1859 return true; |
| 1860 } |
| 1861 |
| 1862 bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::SubmitSlice( |
| 1863 const media::H264PPS* pps, |
| 1864 const media::H264SliceHeader* slice_hdr, |
| 1865 const H264Picture::Vector& ref_pic_list0, |
| 1866 const H264Picture::Vector& ref_pic_list1, |
| 1867 const scoped_refptr<H264Picture>& pic, |
| 1868 const uint8_t* data, |
| 1869 size_t size) { |
| 1870 if (num_slices_ == kMaxSlices) { |
| 1871 LOGF(ERROR) << "Over limit of supported slices per frame"; |
| 1872 return false; |
| 1873 } |
| 1874 |
| 1875 struct v4l2_ctrl_h264_slice_param& v4l2_slice_param = |
| 1876 v4l2_slice_params_[num_slices_++]; |
| 1877 memset(&v4l2_slice_param, 0, sizeof(v4l2_slice_param)); |
| 1878 |
| 1879 v4l2_slice_param.size = size; |
| 1880 #define SHDR_TO_V4L2SPARM(a) v4l2_slice_param.a = slice_hdr->a |
| 1881 SHDR_TO_V4L2SPARM(header_bit_size); |
| 1882 SHDR_TO_V4L2SPARM(first_mb_in_slice); |
| 1883 SHDR_TO_V4L2SPARM(slice_type); |
| 1884 SHDR_TO_V4L2SPARM(pic_parameter_set_id); |
| 1885 SHDR_TO_V4L2SPARM(colour_plane_id); |
| 1886 SHDR_TO_V4L2SPARM(frame_num); |
| 1887 SHDR_TO_V4L2SPARM(idr_pic_id); |
| 1888 SHDR_TO_V4L2SPARM(pic_order_cnt_lsb); |
| 1889 SHDR_TO_V4L2SPARM(delta_pic_order_cnt_bottom); |
| 1890 SHDR_TO_V4L2SPARM(delta_pic_order_cnt0); |
| 1891 SHDR_TO_V4L2SPARM(delta_pic_order_cnt1); |
| 1892 SHDR_TO_V4L2SPARM(redundant_pic_cnt); |
| 1893 SHDR_TO_V4L2SPARM(dec_ref_pic_marking_bit_size); |
| 1894 SHDR_TO_V4L2SPARM(cabac_init_idc); |
| 1895 SHDR_TO_V4L2SPARM(slice_qp_delta); |
| 1896 SHDR_TO_V4L2SPARM(slice_qs_delta); |
| 1897 SHDR_TO_V4L2SPARM(disable_deblocking_filter_idc); |
| 1898 SHDR_TO_V4L2SPARM(slice_alpha_c0_offset_div2); |
| 1899 SHDR_TO_V4L2SPARM(slice_beta_offset_div2); |
| 1900 SHDR_TO_V4L2SPARM(num_ref_idx_l0_active_minus1); |
| 1901 SHDR_TO_V4L2SPARM(num_ref_idx_l1_active_minus1); |
| 1902 SHDR_TO_V4L2SPARM(pic_order_cnt_bit_size); |
| 1903 #undef SHDR_TO_V4L2SPARM |
| 1904 |
| 1905 #define SET_V4L2_SPARM_FLAG_IF(cond, flag) \ |
| 1906 v4l2_slice_param.flags |= ((slice_hdr->cond) ? (flag) : 0) |
| 1907 SET_V4L2_SPARM_FLAG_IF(field_pic_flag, V4L2_SLICE_FLAG_FIELD_PIC); |
| 1908 SET_V4L2_SPARM_FLAG_IF(bottom_field_flag, V4L2_SLICE_FLAG_BOTTOM_FIELD); |
| 1909 SET_V4L2_SPARM_FLAG_IF(direct_spatial_mv_pred_flag, |
| 1910 V4L2_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED); |
| 1911 SET_V4L2_SPARM_FLAG_IF(sp_for_switch_flag, V4L2_SLICE_FLAG_SP_FOR_SWITCH); |
| 1912 #undef SET_V4L2_SPARM_FLAG_IF |
| 1913 |
| 1914 struct v4l2_h264_pred_weight_table* pred_weight_table = |
| 1915 &v4l2_slice_param.pred_weight_table; |
| 1916 |
| 1917 if (((slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) && |
| 1918 pps->weighted_pred_flag) || |
| 1919 (slice_hdr->IsBSlice() && pps->weighted_bipred_idc == 1)) { |
| 1920 pred_weight_table->luma_log2_weight_denom = |
| 1921 slice_hdr->luma_log2_weight_denom; |
| 1922 pred_weight_table->chroma_log2_weight_denom = |
| 1923 slice_hdr->chroma_log2_weight_denom; |
| 1924 |
| 1925 struct v4l2_h264_weight_factors* factorsl0 = |
| 1926 &pred_weight_table->weight_factors[0]; |
| 1927 |
| 1928 for (int i = 0; i < 32; ++i) { |
| 1929 factorsl0->luma_weight[i] = |
| 1930 slice_hdr->pred_weight_table_l0.luma_weight[i]; |
| 1931 factorsl0->luma_offset[i] = |
| 1932 slice_hdr->pred_weight_table_l0.luma_offset[i]; |
| 1933 |
| 1934 for (int j = 0; j < 2; ++j) { |
| 1935 factorsl0->chroma_weight[i][j] = |
| 1936 slice_hdr->pred_weight_table_l0.chroma_weight[i][j]; |
| 1937 factorsl0->chroma_offset[i][j] = |
| 1938 slice_hdr->pred_weight_table_l0.chroma_offset[i][j]; |
| 1939 } |
| 1940 } |
| 1941 |
| 1942 if (slice_hdr->IsBSlice()) { |
| 1943 struct v4l2_h264_weight_factors* factorsl1 = |
| 1944 &pred_weight_table->weight_factors[1]; |
| 1945 |
| 1946 for (int i = 0; i < 32; ++i) { |
| 1947 factorsl1->luma_weight[i] = |
| 1948 slice_hdr->pred_weight_table_l1.luma_weight[i]; |
| 1949 factorsl1->luma_offset[i] = |
| 1950 slice_hdr->pred_weight_table_l1.luma_offset[i]; |
| 1951 |
| 1952 for (int j = 0; j < 2; ++j) { |
| 1953 factorsl1->chroma_weight[i][j] = |
| 1954 slice_hdr->pred_weight_table_l1.chroma_weight[i][j]; |
| 1955 factorsl1->chroma_offset[i][j] = |
| 1956 slice_hdr->pred_weight_table_l1.chroma_offset[i][j]; |
| 1957 } |
| 1958 } |
| 1959 } |
| 1960 } |
| 1961 |
| 1962 H264PictureListToDPBIndicesList(ref_pic_list0, |
| 1963 v4l2_slice_param.ref_pic_list0); |
| 1964 H264PictureListToDPBIndicesList(ref_pic_list1, |
| 1965 v4l2_slice_param.ref_pic_list1); |
| 1966 |
| 1967 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 1968 H264PictureToV4L2DecodeSurface(pic); |
| 1969 |
| 1970 v4l2_decode_param_.nal_ref_idc = slice_hdr->nal_ref_idc; |
| 1971 |
| 1972 // TODO(posciak): Don't add start code back here, but have it passed from |
| 1973 // the parser. |
| 1974 size_t data_copy_size = size + 3; |
| 1975 scoped_ptr<uint8_t[]> data_copy(new uint8_t[data_copy_size]); |
| 1976 memset(data_copy.get(), 0, data_copy_size); |
| 1977 data_copy[2] = 0x01; |
| 1978 memcpy(data_copy.get() + 3, data, size); |
| 1979 return v4l2_dec_->SubmitSlice(dec_surface->input_record(), data_copy.get(), |
| 1980 data_copy_size); |
| 1981 } |
| 1982 |
| 1983 bool V4L2SliceVideoDecodeAccelerator::SubmitSlice(int index, |
| 1984 const uint8_t* data, |
| 1985 size_t size) { |
| 1986 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 1987 |
| 1988 InputRecord& input_record = input_buffer_map_[index]; |
| 1989 |
| 1990 if (input_record.bytes_used + size > input_record.length) { |
| 1991 DVLOGF(1) << "Input buffer too small"; |
| 1992 return false; |
| 1993 } |
| 1994 |
| 1995 memcpy(static_cast<uint8_t*>(input_record.address) + input_record.bytes_used, |
| 1996 data, size); |
| 1997 input_record.bytes_used += size; |
| 1998 |
| 1999 return true; |
| 2000 } |
| 2001 |
| 2002 bool V4L2SliceVideoDecodeAccelerator::SubmitExtControls( |
| 2003 struct v4l2_ext_controls* ext_ctrls) { |
| 2004 DCHECK_GT(ext_ctrls->config_store, 0u); |
| 2005 IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_EXT_CTRLS, ext_ctrls); |
| 2006 return true; |
| 2007 } |
| 2008 |
| 2009 bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::SubmitDecode( |
| 2010 const scoped_refptr<H264Picture>& pic) { |
| 2011 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 2012 H264PictureToV4L2DecodeSurface(pic); |
| 2013 |
| 2014 v4l2_decode_param_.num_slices = num_slices_; |
| 2015 v4l2_decode_param_.idr_pic_flag = pic->idr; |
| 2016 v4l2_decode_param_.top_field_order_cnt = pic->top_field_order_cnt; |
| 2017 v4l2_decode_param_.bottom_field_order_cnt = pic->bottom_field_order_cnt; |
| 2018 |
| 2019 struct v4l2_ext_control ctrl; |
| 2020 std::vector<struct v4l2_ext_control> ctrls; |
| 2021 |
| 2022 memset(&ctrl, 0, sizeof(ctrl)); |
| 2023 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM; |
| 2024 ctrl.size = sizeof(v4l2_slice_params_); |
| 2025 ctrl.p_h264_slice_param = v4l2_slice_params_; |
| 2026 ctrls.push_back(ctrl); |
| 2027 |
| 2028 memset(&ctrl, 0, sizeof(ctrl)); |
| 2029 ctrl.id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM; |
| 2030 ctrl.size = sizeof(v4l2_decode_param_); |
| 2031 ctrl.p_h264_decode_param = &v4l2_decode_param_; |
| 2032 ctrls.push_back(ctrl); |
| 2033 |
| 2034 struct v4l2_ext_controls ext_ctrls; |
| 2035 memset(&ext_ctrls, 0, sizeof(ext_ctrls)); |
| 2036 ext_ctrls.count = ctrls.size(); |
| 2037 ext_ctrls.controls = &ctrls[0]; |
| 2038 ext_ctrls.config_store = dec_surface->config_store(); |
| 2039 v4l2_dec_->SubmitExtControls(&ext_ctrls); |
| 2040 |
| 2041 num_slices_ = 0; |
| 2042 memset(&v4l2_decode_param_, 0, sizeof(v4l2_decode_param_)); |
| 2043 memset(&v4l2_slice_params_, 0, sizeof(v4l2_slice_params_)); |
| 2044 |
| 2045 v4l2_dec_->DecodeSurface(dec_surface); |
| 2046 return true; |
| 2047 } |
| 2048 |
| 2049 bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::OutputPicture( |
| 2050 const scoped_refptr<H264Picture>& pic) { |
| 2051 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 2052 H264PictureToV4L2DecodeSurface(pic); |
| 2053 v4l2_dec_->SurfaceReady(dec_surface); |
| 2054 return true; |
| 2055 } |
| 2056 |
| 2057 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 2058 V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator:: |
| 2059 H264PictureToV4L2DecodeSurface(const scoped_refptr<H264Picture>& pic) { |
| 2060 V4L2H264Picture* v4l2_pic = pic->AsV4L2H264Picture(); |
| 2061 CHECK(v4l2_pic); |
| 2062 return v4l2_pic->dec_surface(); |
| 2063 } |
| 2064 |
| 2065 V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::V4L2VP8Accelerator( |
| 2066 V4L2SliceVideoDecodeAccelerator* v4l2_dec) |
| 2067 : v4l2_dec_(v4l2_dec) { |
| 2068 DCHECK(v4l2_dec_); |
| 2069 } |
| 2070 |
| 2071 V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::~V4L2VP8Accelerator() { |
| 2072 } |
| 2073 |
| 2074 scoped_refptr<VP8Picture> |
| 2075 V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::CreateVP8Picture() { |
| 2076 scoped_refptr<V4L2DecodeSurface> dec_surface = v4l2_dec_->CreateSurface(); |
| 2077 if (!dec_surface) |
| 2078 return nullptr; |
| 2079 |
| 2080 return new V4L2VP8Picture(dec_surface); |
| 2081 } |
| 2082 |
| 2083 #define ARRAY_MEMCPY_CHECKED(to, from) \ |
| 2084 do { \ |
| 2085 static_assert(sizeof(to) == sizeof(from), \ |
| 2086 #from " and " #to " arrays must be of same size"); \ |
| 2087 memcpy(to, from, sizeof(to)); \ |
| 2088 } while (0) |
| 2089 |
| 2090 static void FillV4L2SegmentationHeader( |
| 2091 const media::Vp8SegmentationHeader& vp8_sgmnt_hdr, |
| 2092 struct v4l2_vp8_sgmnt_hdr* v4l2_sgmnt_hdr) { |
| 2093 #define SET_V4L2_SGMNT_HDR_FLAG_IF(cond, flag) \ |
| 2094 v4l2_sgmnt_hdr->flags |= ((vp8_sgmnt_hdr.cond) ? (flag) : 0) |
| 2095 SET_V4L2_SGMNT_HDR_FLAG_IF(segmentation_enabled, |
| 2096 V4L2_VP8_SEGMNT_HDR_FLAG_ENABLED); |
| 2097 SET_V4L2_SGMNT_HDR_FLAG_IF(update_mb_segmentation_map, |
| 2098 V4L2_VP8_SEGMNT_HDR_FLAG_UPDATE_MAP); |
| 2099 SET_V4L2_SGMNT_HDR_FLAG_IF(update_segment_feature_data, |
| 2100 V4L2_VP8_SEGMNT_HDR_FLAG_UPDATE_FEATURE_DATA); |
| 2101 #undef SET_V4L2_SPARM_FLAG_IF |
| 2102 v4l2_sgmnt_hdr->segment_feature_mode = vp8_sgmnt_hdr.segment_feature_mode; |
| 2103 |
| 2104 ARRAY_MEMCPY_CHECKED(v4l2_sgmnt_hdr->quant_update, |
| 2105 vp8_sgmnt_hdr.quantizer_update_value); |
| 2106 ARRAY_MEMCPY_CHECKED(v4l2_sgmnt_hdr->lf_update, |
| 2107 vp8_sgmnt_hdr.lf_update_value); |
| 2108 ARRAY_MEMCPY_CHECKED(v4l2_sgmnt_hdr->segment_probs, |
| 2109 vp8_sgmnt_hdr.segment_prob); |
| 2110 } |
| 2111 |
| 2112 static void FillV4L2LoopfilterHeader( |
| 2113 const media::Vp8LoopFilterHeader& vp8_loopfilter_hdr, |
| 2114 struct v4l2_vp8_loopfilter_hdr* v4l2_lf_hdr) { |
| 2115 #define SET_V4L2_LF_HDR_FLAG_IF(cond, flag) \ |
| 2116 v4l2_lf_hdr->flags |= ((vp8_loopfilter_hdr.cond) ? (flag) : 0) |
| 2117 SET_V4L2_LF_HDR_FLAG_IF(loop_filter_adj_enable, V4L2_VP8_LF_HDR_ADJ_ENABLE); |
| 2118 SET_V4L2_LF_HDR_FLAG_IF(mode_ref_lf_delta_update, |
| 2119 V4L2_VP8_LF_HDR_DELTA_UPDATE); |
| 2120 #undef SET_V4L2_SGMNT_HDR_FLAG_IF |
| 2121 |
| 2122 #define LF_HDR_TO_V4L2_LF_HDR(a) v4l2_lf_hdr->a = vp8_loopfilter_hdr.a; |
| 2123 LF_HDR_TO_V4L2_LF_HDR(type); |
| 2124 LF_HDR_TO_V4L2_LF_HDR(level); |
| 2125 LF_HDR_TO_V4L2_LF_HDR(sharpness_level); |
| 2126 #undef LF_HDR_TO_V4L2_LF_HDR |
| 2127 |
| 2128 ARRAY_MEMCPY_CHECKED(v4l2_lf_hdr->ref_frm_delta_magnitude, |
| 2129 vp8_loopfilter_hdr.ref_frame_delta); |
| 2130 ARRAY_MEMCPY_CHECKED(v4l2_lf_hdr->mb_mode_delta_magnitude, |
| 2131 vp8_loopfilter_hdr.mb_mode_delta); |
| 2132 } |
| 2133 |
| 2134 static void FillV4L2QuantizationHeader( |
| 2135 const media::Vp8QuantizationHeader& vp8_quant_hdr, |
| 2136 struct v4l2_vp8_quantization_hdr* v4l2_quant_hdr) { |
| 2137 v4l2_quant_hdr->y_ac_qi = vp8_quant_hdr.y_ac_qi; |
| 2138 v4l2_quant_hdr->y_dc_delta = vp8_quant_hdr.y_dc_delta; |
| 2139 v4l2_quant_hdr->y2_dc_delta = vp8_quant_hdr.y2_dc_delta; |
| 2140 v4l2_quant_hdr->y2_ac_delta = vp8_quant_hdr.y2_ac_delta; |
| 2141 v4l2_quant_hdr->uv_dc_delta = vp8_quant_hdr.uv_dc_delta; |
| 2142 v4l2_quant_hdr->uv_ac_delta = vp8_quant_hdr.uv_ac_delta; |
| 2143 } |
| 2144 |
| 2145 static void FillV4L2EntropyHeader( |
| 2146 const media::Vp8EntropyHeader& vp8_entropy_hdr, |
| 2147 struct v4l2_vp8_entropy_hdr* v4l2_entropy_hdr) { |
| 2148 ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->coeff_probs, |
| 2149 vp8_entropy_hdr.coeff_probs); |
| 2150 ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->y_mode_probs, |
| 2151 vp8_entropy_hdr.y_mode_probs); |
| 2152 ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->uv_mode_probs, |
| 2153 vp8_entropy_hdr.uv_mode_probs); |
| 2154 ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->mv_probs, |
| 2155 vp8_entropy_hdr.mv_probs); |
| 2156 } |
| 2157 |
| 2158 bool V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::SubmitDecode( |
| 2159 const scoped_refptr<VP8Picture>& pic, |
| 2160 const media::Vp8FrameHeader* frame_hdr, |
| 2161 const scoped_refptr<VP8Picture>& last_frame, |
| 2162 const scoped_refptr<VP8Picture>& golden_frame, |
| 2163 const scoped_refptr<VP8Picture>& alt_frame) { |
| 2164 struct v4l2_ctrl_vp8_frame_hdr v4l2_frame_hdr; |
| 2165 memset(&v4l2_frame_hdr, 0, sizeof(v4l2_frame_hdr)); |
| 2166 |
| 2167 #define FHDR_TO_V4L2_FHDR(a) v4l2_frame_hdr.a = frame_hdr->a |
| 2168 FHDR_TO_V4L2_FHDR(key_frame); |
| 2169 FHDR_TO_V4L2_FHDR(version); |
| 2170 FHDR_TO_V4L2_FHDR(width); |
| 2171 FHDR_TO_V4L2_FHDR(horizontal_scale); |
| 2172 FHDR_TO_V4L2_FHDR(height); |
| 2173 FHDR_TO_V4L2_FHDR(vertical_scale); |
| 2174 FHDR_TO_V4L2_FHDR(sign_bias_golden); |
| 2175 FHDR_TO_V4L2_FHDR(sign_bias_alternate); |
| 2176 FHDR_TO_V4L2_FHDR(prob_skip_false); |
| 2177 FHDR_TO_V4L2_FHDR(prob_intra); |
| 2178 FHDR_TO_V4L2_FHDR(prob_last); |
| 2179 FHDR_TO_V4L2_FHDR(prob_gf); |
| 2180 FHDR_TO_V4L2_FHDR(bool_dec_range); |
| 2181 FHDR_TO_V4L2_FHDR(bool_dec_value); |
| 2182 FHDR_TO_V4L2_FHDR(bool_dec_count); |
| 2183 #undef FHDR_TO_V4L2_FHDR |
| 2184 |
| 2185 #define SET_V4L2_FRM_HDR_FLAG_IF(cond, flag) \ |
| 2186 v4l2_frame_hdr.flags |= ((frame_hdr->cond) ? (flag) : 0) |
| 2187 SET_V4L2_FRM_HDR_FLAG_IF(is_experimental, |
| 2188 V4L2_VP8_FRAME_HDR_FLAG_EXPERIMENTAL); |
| 2189 SET_V4L2_FRM_HDR_FLAG_IF(show_frame, V4L2_VP8_FRAME_HDR_FLAG_SHOW_FRAME); |
| 2190 SET_V4L2_FRM_HDR_FLAG_IF(mb_no_skip_coeff, |
| 2191 V4L2_VP8_FRAME_HDR_FLAG_MB_NO_SKIP_COEFF); |
| 2192 #undef SET_V4L2_FRM_HDR_FLAG_IF |
| 2193 |
| 2194 FillV4L2SegmentationHeader(frame_hdr->segmentation_hdr, |
| 2195 &v4l2_frame_hdr.sgmnt_hdr); |
| 2196 |
| 2197 FillV4L2LoopfilterHeader(frame_hdr->loopfilter_hdr, &v4l2_frame_hdr.lf_hdr); |
| 2198 |
| 2199 FillV4L2QuantizationHeader(frame_hdr->quantization_hdr, |
| 2200 &v4l2_frame_hdr.quant_hdr); |
| 2201 |
| 2202 FillV4L2EntropyHeader(frame_hdr->entropy_hdr, &v4l2_frame_hdr.entropy_hdr); |
| 2203 |
| 2204 v4l2_frame_hdr.first_part_size = |
| 2205 base::checked_cast<__u32>(frame_hdr->first_part_size); |
| 2206 v4l2_frame_hdr.first_part_offset = |
| 2207 base::checked_cast<__u32>(frame_hdr->first_part_offset); |
| 2208 v4l2_frame_hdr.macroblock_bit_offset = |
| 2209 base::checked_cast<__u32>(frame_hdr->macroblock_bit_offset); |
| 2210 v4l2_frame_hdr.num_dct_parts = frame_hdr->num_of_dct_partitions; |
| 2211 |
| 2212 static_assert(arraysize(v4l2_frame_hdr.dct_part_sizes) == |
| 2213 arraysize(frame_hdr->dct_partition_sizes), |
| 2214 "DCT partition size arrays must have equal number of elements"); |
| 2215 for (size_t i = 0; i < frame_hdr->num_of_dct_partitions && |
| 2216 i < arraysize(v4l2_frame_hdr.dct_part_sizes); ++i) |
| 2217 v4l2_frame_hdr.dct_part_sizes[i] = frame_hdr->dct_partition_sizes[i]; |
| 2218 |
| 2219 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 2220 VP8PictureToV4L2DecodeSurface(pic); |
| 2221 std::vector<scoped_refptr<V4L2DecodeSurface>> ref_surfaces; |
| 2222 |
| 2223 if (last_frame) { |
| 2224 scoped_refptr<V4L2DecodeSurface> last_frame_surface = |
| 2225 VP8PictureToV4L2DecodeSurface(last_frame); |
| 2226 v4l2_frame_hdr.last_frame = last_frame_surface->output_record(); |
| 2227 ref_surfaces.push_back(last_frame_surface); |
| 2228 } else { |
| 2229 v4l2_frame_hdr.last_frame = VIDEO_MAX_FRAME; |
| 2230 } |
| 2231 |
| 2232 if (golden_frame) { |
| 2233 scoped_refptr<V4L2DecodeSurface> golden_frame_surface = |
| 2234 VP8PictureToV4L2DecodeSurface(golden_frame); |
| 2235 v4l2_frame_hdr.golden_frame = golden_frame_surface->output_record(); |
| 2236 ref_surfaces.push_back(golden_frame_surface); |
| 2237 } else { |
| 2238 v4l2_frame_hdr.golden_frame = VIDEO_MAX_FRAME; |
| 2239 } |
| 2240 |
| 2241 if (alt_frame) { |
| 2242 scoped_refptr<V4L2DecodeSurface> alt_frame_surface = |
| 2243 VP8PictureToV4L2DecodeSurface(alt_frame); |
| 2244 v4l2_frame_hdr.alt_frame = alt_frame_surface->output_record(); |
| 2245 ref_surfaces.push_back(alt_frame_surface); |
| 2246 } else { |
| 2247 v4l2_frame_hdr.alt_frame = VIDEO_MAX_FRAME; |
| 2248 } |
| 2249 |
| 2250 struct v4l2_ext_control ctrl; |
| 2251 memset(&ctrl, 0, sizeof(ctrl)); |
| 2252 ctrl.id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR; |
| 2253 ctrl.size = sizeof(v4l2_frame_hdr); |
| 2254 ctrl.p_vp8_frame_hdr = &v4l2_frame_hdr; |
| 2255 |
| 2256 struct v4l2_ext_controls ext_ctrls; |
| 2257 memset(&ext_ctrls, 0, sizeof(ext_ctrls)); |
| 2258 ext_ctrls.count = 1; |
| 2259 ext_ctrls.controls = &ctrl; |
| 2260 ext_ctrls.config_store = dec_surface->config_store(); |
| 2261 |
| 2262 if (!v4l2_dec_->SubmitExtControls(&ext_ctrls)) |
| 2263 return false; |
| 2264 |
| 2265 dec_surface->SetReferenceSurfaces(ref_surfaces); |
| 2266 |
| 2267 if (!v4l2_dec_->SubmitSlice(dec_surface->input_record(), frame_hdr->data, |
| 2268 frame_hdr->frame_size)) |
| 2269 return false; |
| 2270 |
| 2271 v4l2_dec_->DecodeSurface(dec_surface); |
| 2272 return true; |
| 2273 } |
| 2274 |
| 2275 bool V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::OutputPicture( |
| 2276 const scoped_refptr<VP8Picture>& pic) { |
| 2277 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 2278 VP8PictureToV4L2DecodeSurface(pic); |
| 2279 |
| 2280 v4l2_dec_->SurfaceReady(dec_surface); |
| 2281 return true; |
| 2282 } |
| 2283 |
| 2284 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 2285 V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator:: |
| 2286 VP8PictureToV4L2DecodeSurface(const scoped_refptr<VP8Picture>& pic) { |
| 2287 V4L2VP8Picture* v4l2_pic = pic->AsV4L2VP8Picture(); |
| 2288 CHECK(v4l2_pic); |
| 2289 return v4l2_pic->dec_surface(); |
| 2290 } |
| 2291 |
| 2292 void V4L2SliceVideoDecodeAccelerator::DecodeSurface( |
| 2293 const scoped_refptr<V4L2DecodeSurface>& dec_surface) { |
| 2294 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 2295 |
| 2296 DVLOGF(3) << "Submitting decode for surface: " << dec_surface->ToString(); |
| 2297 Enqueue(dec_surface); |
| 2298 } |
| 2299 |
| 2300 void V4L2SliceVideoDecodeAccelerator::SurfaceReady( |
| 2301 const scoped_refptr<V4L2DecodeSurface>& dec_surface) { |
| 2302 DVLOGF(3); |
| 2303 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 2304 |
| 2305 decoder_display_queue_.push(dec_surface); |
| 2306 TryOutputSurfaces(); |
| 2307 } |
| 2308 |
| 2309 void V4L2SliceVideoDecodeAccelerator::TryOutputSurfaces() { |
| 2310 while (!decoder_display_queue_.empty()) { |
| 2311 scoped_refptr<V4L2DecodeSurface> dec_surface = |
| 2312 decoder_display_queue_.front(); |
| 2313 |
| 2314 if (!dec_surface->decoded()) |
| 2315 break; |
| 2316 |
| 2317 decoder_display_queue_.pop(); |
| 2318 OutputSurface(dec_surface); |
| 2319 } |
| 2320 } |
| 2321 |
| 2322 void V4L2SliceVideoDecodeAccelerator::OutputSurface( |
| 2323 const scoped_refptr<V4L2DecodeSurface>& dec_surface) { |
| 2324 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 2325 |
| 2326 OutputRecord& output_record = |
| 2327 output_buffer_map_[dec_surface->output_record()]; |
| 2328 |
| 2329 bool inserted = |
| 2330 surfaces_at_display_.insert(std::make_pair(output_record.picture_id, |
| 2331 dec_surface)).second; |
| 2332 DCHECK(inserted); |
| 2333 |
| 2334 DCHECK(!output_record.at_client); |
| 2335 DCHECK(!output_record.at_device); |
| 2336 DCHECK_NE(output_record.egl_image, EGL_NO_IMAGE_KHR); |
| 2337 DCHECK_NE(output_record.picture_id, -1); |
| 2338 output_record.at_client = true; |
| 2339 |
| 2340 media::Picture picture(output_record.picture_id, dec_surface->bitstream_id(), |
| 2341 gfx::Rect(frame_buffer_size_)); |
| 2342 DVLOGF(3) << dec_surface->ToString() |
| 2343 << ", bitstream_id: " << picture.bitstream_buffer_id() |
| 2344 << ", picture_id: " << picture.picture_buffer_id(); |
| 2345 pending_picture_ready_.push(PictureRecord(output_record.cleared, picture)); |
| 2346 SendPictureReady(); |
| 2347 output_record.cleared = true; |
| 2348 } |
| 2349 |
| 2350 scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface> |
| 2351 V4L2SliceVideoDecodeAccelerator::CreateSurface() { |
| 2352 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 2353 |
| 2354 if (free_input_buffers_.empty() || free_output_buffers_.empty()) |
| 2355 return nullptr; |
| 2356 |
| 2357 int input = free_input_buffers_.front(); |
| 2358 free_input_buffers_.pop_front(); |
| 2359 int output = free_output_buffers_.front(); |
| 2360 free_output_buffers_.pop_front(); |
| 2361 |
| 2362 InputRecord& input_record = input_buffer_map_[input]; |
| 2363 DCHECK_EQ(input_record.bytes_used, 0u); |
| 2364 DCHECK_EQ(input_record.input_id, -1); |
| 2365 DCHECK(decoder_current_bitstream_buffer_ != nullptr); |
| 2366 input_record.input_id = decoder_current_bitstream_buffer_->input_id; |
| 2367 |
| 2368 scoped_refptr<V4L2DecodeSurface> dec_surface = new V4L2DecodeSurface( |
| 2369 decoder_current_bitstream_buffer_->input_id, input, output, |
| 2370 base::Bind(&V4L2SliceVideoDecodeAccelerator::ReuseOutputBuffer, |
| 2371 base::Unretained(this))); |
| 2372 |
| 2373 DVLOGF(4) << "Created surface " << input << " -> " << output; |
| 2374 return dec_surface; |
| 2375 } |
| 2376 |
| 2377 void V4L2SliceVideoDecodeAccelerator::SendPictureReady() { |
| 2378 DVLOGF(3); |
| 2379 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 2380 bool resetting_or_flushing = (decoder_resetting_ || decoder_flushing_); |
| 2381 while (!pending_picture_ready_.empty()) { |
| 2382 bool cleared = pending_picture_ready_.front().cleared; |
| 2383 const media::Picture& picture = pending_picture_ready_.front().picture; |
| 2384 if (cleared && picture_clearing_count_ == 0) { |
| 2385 DVLOGF(4) << "Posting picture ready to IO for: " |
| 2386 << picture.picture_buffer_id(); |
| 2387 // This picture is cleared. Post it to IO thread to reduce latency. This |
| 2388 // should be the case after all pictures are cleared at the beginning. |
| 2389 io_message_loop_proxy_->PostTask( |
| 2390 FROM_HERE, base::Bind(&Client::PictureReady, io_client_, picture)); |
| 2391 pending_picture_ready_.pop(); |
| 2392 } else if (!cleared || resetting_or_flushing) { |
| 2393 DVLOGF(3) << "cleared=" << pending_picture_ready_.front().cleared |
| 2394 << ", decoder_resetting_=" << decoder_resetting_ |
| 2395 << ", decoder_flushing_=" << decoder_flushing_ |
| 2396 << ", picture_clearing_count_=" << picture_clearing_count_; |
| 2397 DVLOGF(4) << "Posting picture ready to GPU for: " |
| 2398 << picture.picture_buffer_id(); |
| 2399 // If the picture is not cleared, post it to the child thread because it |
| 2400 // has to be cleared in the child thread. A picture only needs to be |
| 2401 // cleared once. If the decoder is resetting or flushing, send all |
| 2402 // pictures to ensure PictureReady arrive before reset or flush done. |
| 2403 child_message_loop_proxy_->PostTaskAndReply( |
| 2404 FROM_HERE, base::Bind(&Client::PictureReady, client_, picture), |
| 2405 // Unretained is safe. If Client::PictureReady gets to run, |this| is |
| 2406 // alive. Destroy() will wait the decode thread to finish. |
| 2407 base::Bind(&V4L2SliceVideoDecodeAccelerator::PictureCleared, |
| 2408 base::Unretained(this))); |
| 2409 picture_clearing_count_++; |
| 2410 pending_picture_ready_.pop(); |
| 2411 } else { |
| 2412 // This picture is cleared. But some pictures are about to be cleared on |
| 2413 // the child thread. To preserve the order, do not send this until those |
| 2414 // pictures are cleared. |
| 2415 break; |
| 2416 } |
| 2417 } |
| 2418 } |
| 2419 |
| 2420 void V4L2SliceVideoDecodeAccelerator::PictureCleared() { |
| 2421 DVLOGF(3) << "clearing count=" << picture_clearing_count_; |
| 2422 DCHECK(decoder_thread_proxy_->BelongsToCurrentThread()); |
| 2423 DCHECK_GT(picture_clearing_count_, 0); |
| 2424 picture_clearing_count_--; |
| 2425 SendPictureReady(); |
| 2426 } |
| 2427 |
| 2428 bool V4L2SliceVideoDecodeAccelerator::CanDecodeOnIOThread() { |
| 2429 return true; |
| 2430 } |
| 2431 |
| 2432 } // namespace content |
OLD | NEW |