DO NOT MERGE - Merge Android 10 into master

Bug: 139893257
Change-Id: Ib11313bef8f129e109d11e656b29c9b753137265
diff --git a/Android.mk b/Android.mk
index 32656ff..541323c 100644
--- a/Android.mk
+++ b/Android.mk
@@ -31,6 +31,7 @@
                           libstagefright \
                           libstagefright_bufferqueue_helper \
                           libstagefright_ccodec_ext \
+                          libstagefright_ccodec_utils \
                           libstagefright_codec2 \
                           libstagefright_codec2_vndk \
                           libstagefright_simple_c2component \
@@ -41,7 +42,7 @@
                           libvda_c2componentstore \
 
 # -Wno-unused-parameter is needed for libchrome/base codes
-LOCAL_CFLAGS += -Werror -Wall -Wno-unused-parameter -std=c++14
+LOCAL_CFLAGS += -Werror -Wall -Wno-unused-parameter
 LOCAL_CFLAGS += -Wno-unused-lambda-capture -Wno-unknown-warning-option
 LOCAL_CLANG := true
 LOCAL_SANITIZE := unsigned-integer-overflow signed-integer-overflow
diff --git a/C2VDAAdaptor.cpp b/C2VDAAdaptor.cpp
index 2e837a9..55a98a4 100644
--- a/C2VDAAdaptor.cpp
+++ b/C2VDAAdaptor.cpp
@@ -10,9 +10,8 @@
 #include <bitstream_buffer.h>
 #include <native_pixmap_handle.h>
 #include <v4l2_device.h>
-#include <v4l2_slice_video_decode_accelerator.h>
+#include <v4l2_video_decode_accelerator.h>
 #include <video_pixel_format.h>
-#include <videodev2_custom.h>
 
 #include <utils/Log.h>
 
@@ -43,7 +42,7 @@
     // implementations in the future.
     scoped_refptr<media::V4L2Device> device = new media::V4L2Device();
     std::unique_ptr<media::VideoDecodeAccelerator> vda(
-            new media::V4L2SliceVideoDecodeAccelerator(device));
+            new media::V4L2VideoDecodeAccelerator(device));
     if (!vda->Initialize(config, this)) {
         ALOGE("Failed to initialize VDA");
         return PLATFORM_FAILURE;
@@ -123,15 +122,22 @@
 
 //static
 media::VideoDecodeAccelerator::SupportedProfiles C2VDAAdaptor::GetSupportedProfiles(
-        uint32_t inputFormatFourcc) {
+        InputCodec inputCodec) {
+    // TODO(johnylin): use factory function to determine whether V4L2 stream or slice API is.
+    uint32_t inputFormatFourcc;
+    if (inputCodec == InputCodec::H264) {
+        inputFormatFourcc = V4L2_PIX_FMT_H264;
+    } else if (inputCodec == InputCodec::VP8) {
+        inputFormatFourcc = V4L2_PIX_FMT_VP8;
+    } else {  // InputCodec::VP9
+        inputFormatFourcc = V4L2_PIX_FMT_VP9;
+    }
+
     media::VideoDecodeAccelerator::SupportedProfiles supportedProfiles;
-    auto allProfiles = media::V4L2SliceVideoDecodeAccelerator::GetSupportedProfiles();
-    bool isSliceBased = (inputFormatFourcc == V4L2_PIX_FMT_H264_SLICE) ||
-                        (inputFormatFourcc == V4L2_PIX_FMT_VP8_FRAME) ||
-                        (inputFormatFourcc == V4L2_PIX_FMT_VP9_FRAME);
+    auto allProfiles = media::V4L2VideoDecodeAccelerator::GetSupportedProfiles();
     for (const auto& profile : allProfiles) {
         if (inputFormatFourcc ==
-            media::V4L2Device::VideoCodecProfileToV4L2PixFmt(profile.profile, isSliceBased)) {
+            media::V4L2Device::VideoCodecProfileToV4L2PixFmt(profile.profile)) {
             supportedProfiles.push_back(profile);
         }
     }
diff --git a/C2VDAAdaptorProxy.cpp b/C2VDAAdaptorProxy.cpp
index 5b1558b..ec1915f 100644
--- a/C2VDAAdaptorProxy.cpp
+++ b/C2VDAAdaptorProxy.cpp
@@ -8,14 +8,14 @@
 #include <C2ArcVideoAcceleratorFactory.h>
 #include <C2VDAAdaptorProxy.h>
 
-#include <videodev2_custom.h>
-
 #include <arc/MojoProcessSupport.h>
 #include <arc/MojoThread.h>
 #include <base/bind.h>
+#include <base/files/scoped_file.h>
+#include <mojo/public/cpp/platform/platform_handle.h>
+#include <mojo/public/cpp/system/platform_handle.h>
+
 #include <binder/IServiceManager.h>
-#include <mojo/edk/embedder/embedder.h>
-#include <mojo/public/cpp/system/handle.h>
 #include <utils/Log.h>
 
 namespace mojo {
@@ -51,8 +51,8 @@
     ALOGV("establishChannel");
     auto future = ::arc::Future<bool>::make_shared(mRelay);
     mMojoTaskRunner->PostTask(FROM_HERE,
-                              base::Bind(&C2VDAAdaptorProxy::establishChannelOnMojoThread,
-                                         base::Unretained(this), future));
+                              ::base::Bind(&C2VDAAdaptorProxy::establishChannelOnMojoThread,
+                                         ::base::Unretained(this), future));
     return future->wait() && future->get();
 }
 
@@ -63,10 +63,10 @@
         future->set(false);
         return;
     }
-    mVDAPtr.set_connection_error_handler(base::Bind(&C2VDAAdaptorProxy::onConnectionError,
-                                                    base::Unretained(this),
+    mVDAPtr.set_connection_error_handler(::base::Bind(&C2VDAAdaptorProxy::onConnectionError,
+                                                    ::base::Unretained(this),
                                                     std::string("mVDAPtr (vda pipe)")));
-    mVDAPtr.QueryVersion(base::Bind(&C2VDAAdaptorProxy::onVersionReady, base::Unretained(this),
+    mVDAPtr.QueryVersion(::base::Bind(&C2VDAAdaptorProxy::onVersionReady, ::base::Unretained(this),
                                     std::move(future)));
 }
 
@@ -121,10 +121,12 @@
 
 void C2VDAAdaptorProxy::NotifyResetDone(::arc::mojom::VideoDecodeAccelerator::Result result) {
     ALOGV("NotifyResetDone");
+    // Always notify reset done to component even if result is not success. On shutdown, MediaCodec
+    // will wait on shutdown complete notification despite any error. If no notification, it will be
+    // hanging until timeout and force release.
     if (result != ::arc::mojom::VideoDecodeAccelerator::Result::SUCCESS) {
         ALOGE("Reset is done incorrectly.");
         NotifyError(result);
-        return;
     }
     mClient->notifyResetDone();
 }
@@ -146,25 +148,22 @@
 
 //static
 media::VideoDecodeAccelerator::SupportedProfiles C2VDAAdaptorProxy::GetSupportedProfiles(
-        uint32_t inputFormatFourcc) {
+        InputCodec inputCodec) {
     media::VideoDecodeAccelerator::SupportedProfiles profiles(1);
     profiles[0].min_resolution = media::Size(16, 16);
     profiles[0].max_resolution = media::Size(4096, 4096);
-    switch (inputFormatFourcc) {
-    case V4L2_PIX_FMT_H264:
-    case V4L2_PIX_FMT_H264_SLICE:
+    switch (inputCodec) {
+    case InputCodec::H264:
         profiles[0].profile = media::H264PROFILE_MAIN;
         break;
-    case V4L2_PIX_FMT_VP8:
-    case V4L2_PIX_FMT_VP8_FRAME:
+    case InputCodec::VP8:
         profiles[0].profile = media::VP8PROFILE_ANY;
         break;
-    case V4L2_PIX_FMT_VP9:
-    case V4L2_PIX_FMT_VP9_FRAME:
+    case InputCodec::VP9:
         profiles[0].profile = media::VP9PROFILE_PROFILE0;
         break;
     default:
-        ALOGE("Unknown formatfourcc: %d", inputFormatFourcc);
+        ALOGE("Unknown input codec: %d", inputCodec);
         return {};
     }
     return profiles;
@@ -185,8 +184,8 @@
     }
 
     auto future = ::arc::Future<::arc::mojom::VideoDecodeAccelerator::Result>::make_shared(mRelay);
-    mMojoTaskRunner->PostTask(FROM_HERE, base::Bind(&C2VDAAdaptorProxy::initializeOnMojoThread,
-                                                    base::Unretained(this), profile, secureMode,
+    mMojoTaskRunner->PostTask(FROM_HERE, ::base::Bind(&C2VDAAdaptorProxy::initializeOnMojoThread,
+                                                    ::base::Unretained(this), profile, secureMode,
                                                     ::arc::FutureCallback(future)));
 
     if (!future->wait()) {
@@ -200,9 +199,10 @@
         const media::VideoCodecProfile profile, const bool secureMode,
         const ::arc::mojom::VideoDecodeAccelerator::InitializeCallback& cb) {
     // base::Unretained is safe because we own |mBinding|.
-    auto client = mBinding.CreateInterfacePtrAndBind();
-    mBinding.set_connection_error_handler(base::Bind(&C2VDAAdaptorProxy::onConnectionError,
-                                                     base::Unretained(this),
+    mojo::InterfacePtr<::arc::mojom::VideoDecodeClient> client;
+    mBinding.Bind(mojo::MakeRequest(&client));
+    mBinding.set_connection_error_handler(::base::Bind(&C2VDAAdaptorProxy::onConnectionError,
+                                                     ::base::Unretained(this),
                                                      std::string("mBinding (client pipe)")));
 
     ::arc::mojom::VideoDecodeAcceleratorConfigPtr arcConfig =
@@ -215,23 +215,22 @@
 void C2VDAAdaptorProxy::decode(int32_t bitstreamId, int handleFd, off_t offset, uint32_t size) {
     ALOGV("decode");
     mMojoTaskRunner->PostTask(
-            FROM_HERE, base::Bind(&C2VDAAdaptorProxy::decodeOnMojoThread, base::Unretained(this),
+            FROM_HERE, ::base::Bind(&C2VDAAdaptorProxy::decodeOnMojoThread, ::base::Unretained(this),
                                   bitstreamId, handleFd, offset, size));
 }
 
 void C2VDAAdaptorProxy::decodeOnMojoThread(int32_t bitstreamId, int handleFd, off_t offset,
                                            uint32_t size) {
-    MojoHandle wrappedHandle;
-    MojoResult wrapResult = mojo::edk::CreatePlatformHandleWrapper(
-            mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(handleFd)), &wrappedHandle);
-    if (wrapResult != MOJO_RESULT_OK) {
-        ALOGE("failed to wrap handle: %d", static_cast<int>(wrapResult));
+    mojo::ScopedHandle wrappedHandle =
+            mojo::WrapPlatformHandle(mojo::PlatformHandle(::base::ScopedFD(handleFd)));
+    if (!wrappedHandle.is_valid()) {
+        ALOGE("failed to wrap handle");
         NotifyError(::arc::mojom::VideoDecodeAccelerator::Result::PLATFORM_FAILURE);
         return;
     }
     auto bufferPtr = ::arc::mojom::BitstreamBuffer::New();
     bufferPtr->bitstream_id = bitstreamId;
-    bufferPtr->handle_fd = mojo::ScopedHandle(mojo::Handle(wrappedHandle));
+    bufferPtr->handle_fd = std::move(wrappedHandle);
     bufferPtr->offset = offset;
     bufferPtr->bytes_used = size;
     mVDAPtr->Decode(std::move(bufferPtr));
@@ -240,8 +239,8 @@
 void C2VDAAdaptorProxy::assignPictureBuffers(uint32_t numOutputBuffers) {
     ALOGV("assignPictureBuffers: %d", numOutputBuffers);
     mMojoTaskRunner->PostTask(FROM_HERE,
-                              base::Bind(&C2VDAAdaptorProxy::assignPictureBuffersOnMojoThread,
-                                         base::Unretained(this), numOutputBuffers));
+                              ::base::Bind(&C2VDAAdaptorProxy::assignPictureBuffersOnMojoThread,
+                                         ::base::Unretained(this), numOutputBuffers));
 }
 
 void C2VDAAdaptorProxy::assignPictureBuffersOnMojoThread(uint32_t numOutputBuffers) {
@@ -254,33 +253,32 @@
     ALOGV("importBufferForPicture");
     mMojoTaskRunner->PostTask(
             FROM_HERE,
-            base::Bind(&C2VDAAdaptorProxy::importBufferForPictureOnMojoThread,
-                       base::Unretained(this), pictureBufferId, format, handleFd, planes));
+            ::base::Bind(&C2VDAAdaptorProxy::importBufferForPictureOnMojoThread,
+                       ::base::Unretained(this), pictureBufferId, format, handleFd, planes));
 }
 
 void C2VDAAdaptorProxy::importBufferForPictureOnMojoThread(
         int32_t pictureBufferId, HalPixelFormat format, int handleFd,
         const std::vector<VideoFramePlane>& planes) {
-    MojoHandle wrappedHandle;
-    MojoResult wrapResult = mojo::edk::CreatePlatformHandleWrapper(
-            mojo::edk::ScopedPlatformHandle(mojo::edk::PlatformHandle(handleFd)), &wrappedHandle);
-    if (wrapResult != MOJO_RESULT_OK) {
-        ALOGE("failed to wrap handle: %d", static_cast<int>(wrapResult));
+    mojo::ScopedHandle wrappedHandle =
+            mojo::WrapPlatformHandle(mojo::PlatformHandle(::base::ScopedFD(handleFd)));
+    if (!wrappedHandle.is_valid()) {
+        ALOGE("failed to wrap handle");
         NotifyError(::arc::mojom::VideoDecodeAccelerator::Result::PLATFORM_FAILURE);
         return;
     }
 
     mVDAPtr->ImportBufferForPicture(pictureBufferId,
                                     static_cast<::arc::mojom::HalPixelFormat>(format),
-                                    mojo::ScopedHandle(mojo::Handle(wrappedHandle)),
+                                    std::move(wrappedHandle),
                                     mojo::ConvertTo<std::vector<::arc::VideoFramePlane>>(planes));
 }
 
 void C2VDAAdaptorProxy::reusePictureBuffer(int32_t pictureBufferId) {
     ALOGV("reusePictureBuffer: %d", pictureBufferId);
     mMojoTaskRunner->PostTask(FROM_HERE,
-                              base::Bind(&C2VDAAdaptorProxy::reusePictureBufferOnMojoThread,
-                                         base::Unretained(this), pictureBufferId));
+                              ::base::Bind(&C2VDAAdaptorProxy::reusePictureBufferOnMojoThread,
+                                         ::base::Unretained(this), pictureBufferId));
 }
 
 void C2VDAAdaptorProxy::reusePictureBufferOnMojoThread(int32_t pictureBufferId) {
@@ -290,21 +288,21 @@
 void C2VDAAdaptorProxy::flush() {
     ALOGV("flush");
     mMojoTaskRunner->PostTask(
-            FROM_HERE, base::Bind(&C2VDAAdaptorProxy::flushOnMojoThread, base::Unretained(this)));
+            FROM_HERE, ::base::Bind(&C2VDAAdaptorProxy::flushOnMojoThread, ::base::Unretained(this)));
 }
 
 void C2VDAAdaptorProxy::flushOnMojoThread() {
-    mVDAPtr->Flush(base::Bind(&C2VDAAdaptorProxy::NotifyFlushDone, base::Unretained(this)));
+    mVDAPtr->Flush(::base::Bind(&C2VDAAdaptorProxy::NotifyFlushDone, ::base::Unretained(this)));
 }
 
 void C2VDAAdaptorProxy::reset() {
     ALOGV("reset");
     mMojoTaskRunner->PostTask(
-            FROM_HERE, base::Bind(&C2VDAAdaptorProxy::resetOnMojoThread, base::Unretained(this)));
+            FROM_HERE, ::base::Bind(&C2VDAAdaptorProxy::resetOnMojoThread, ::base::Unretained(this)));
 }
 
 void C2VDAAdaptorProxy::resetOnMojoThread() {
-    mVDAPtr->Reset(base::Bind(&C2VDAAdaptorProxy::NotifyResetDone, base::Unretained(this)));
+    mVDAPtr->Reset(::base::Bind(&C2VDAAdaptorProxy::NotifyResetDone, ::base::Unretained(this)));
 }
 
 void C2VDAAdaptorProxy::destroy() {
@@ -312,7 +310,7 @@
     ::arc::Future<void> future;
     ::arc::PostTaskAndSetFutureWithResult(
             mMojoTaskRunner.get(), FROM_HERE,
-            base::Bind(&C2VDAAdaptorProxy::closeChannelOnMojoThread, base::Unretained(this)),
+            ::base::Bind(&C2VDAAdaptorProxy::closeChannelOnMojoThread, ::base::Unretained(this)),
             &future);
     future.get();
 }
diff --git a/C2VDAComponent.cpp b/C2VDAComponent.cpp
index fd7b313..8685fce 100644
--- a/C2VDAComponent.cpp
+++ b/C2VDAComponent.cpp
@@ -19,16 +19,18 @@
 #include <C2VdaBqBlockPool.h>
 #include <C2VdaPooledBlockPool.h>
 
-#include <videodev2_custom.h>
+#include <h264_parser.h>
 
 #include <C2AllocatorGralloc.h>
 #include <C2ComponentFactory.h>
 #include <C2PlatformSupport.h>
+#include <Codec2Mapper.h>
 
 #include <base/bind.h>
 #include <base/bind_helpers.h>
 
 #include <media/stagefright/MediaDefs.h>
+#include <media/stagefright/foundation/ColorUtils.h>
 #include <utils/Log.h>
 #include <utils/misc.h>
 
@@ -66,22 +68,140 @@
 const int32_t kAllocateBufferMaxRetries = 10;  // Max retry time for fetchGraphicBlock timeout.
 }  // namespace
 
+static c2_status_t adaptorResultToC2Status(VideoDecodeAcceleratorAdaptor::Result result) {
+    switch (result) {
+    case VideoDecodeAcceleratorAdaptor::Result::SUCCESS:
+        return C2_OK;
+    case VideoDecodeAcceleratorAdaptor::Result::ILLEGAL_STATE:
+        ALOGE("Got error: ILLEGAL_STATE");
+        return C2_BAD_STATE;
+    case VideoDecodeAcceleratorAdaptor::Result::INVALID_ARGUMENT:
+        ALOGE("Got error: INVALID_ARGUMENT");
+        return C2_BAD_VALUE;
+    case VideoDecodeAcceleratorAdaptor::Result::UNREADABLE_INPUT:
+        ALOGE("Got error: UNREADABLE_INPUT");
+        return C2_BAD_VALUE;
+    case VideoDecodeAcceleratorAdaptor::Result::PLATFORM_FAILURE:
+        ALOGE("Got error: PLATFORM_FAILURE");
+        return C2_CORRUPTED;
+    case VideoDecodeAcceleratorAdaptor::Result::INSUFFICIENT_RESOURCES:
+        ALOGE("Got error: INSUFFICIENT_RESOURCES");
+        return C2_NO_MEMORY;
+    default:
+        ALOGE("Unrecognizable adaptor result (value = %d)...", result);
+        return C2_CORRUPTED;
+    }
+}
+
+// static
+C2R C2VDAComponent::IntfImpl::ProfileLevelSetter(bool mayBlock,
+                                                 C2P<C2StreamProfileLevelInfo::input>& info) {
+    (void)mayBlock;
+    return info.F(info.v.profile)
+            .validatePossible(info.v.profile)
+            .plus(info.F(info.v.level).validatePossible(info.v.level));
+}
+
+// static
+C2R C2VDAComponent::IntfImpl::SizeSetter(bool mayBlock,
+                                         C2P<C2StreamPictureSizeInfo::output>& videoSize) {
+    (void)mayBlock;
+    // TODO: maybe apply block limit?
+    return videoSize.F(videoSize.v.width)
+            .validatePossible(videoSize.v.width)
+            .plus(videoSize.F(videoSize.v.height).validatePossible(videoSize.v.height));
+}
+
+// static
+template <typename T>
+C2R C2VDAComponent::IntfImpl::DefaultColorAspectsSetter(bool mayBlock, C2P<T>& def) {
+    (void)mayBlock;
+    if (def.v.range > C2Color::RANGE_OTHER) {
+        def.set().range = C2Color::RANGE_OTHER;
+    }
+    if (def.v.primaries > C2Color::PRIMARIES_OTHER) {
+        def.set().primaries = C2Color::PRIMARIES_OTHER;
+    }
+    if (def.v.transfer > C2Color::TRANSFER_OTHER) {
+        def.set().transfer = C2Color::TRANSFER_OTHER;
+    }
+    if (def.v.matrix > C2Color::MATRIX_OTHER) {
+        def.set().matrix = C2Color::MATRIX_OTHER;
+    }
+    return C2R::Ok();
+}
+
+// static
+C2R C2VDAComponent::IntfImpl::MergedColorAspectsSetter(
+        bool mayBlock, C2P<C2StreamColorAspectsInfo::output>& merged,
+        const C2P<C2StreamColorAspectsTuning::output>& def,
+        const C2P<C2StreamColorAspectsInfo::input>& coded) {
+    (void)mayBlock;
+    // Take coded values for all specified fields, and default values for unspecified ones.
+    merged.set().range = coded.v.range == RANGE_UNSPECIFIED ? def.v.range : coded.v.range;
+    merged.set().primaries =
+            coded.v.primaries == PRIMARIES_UNSPECIFIED ? def.v.primaries : coded.v.primaries;
+    merged.set().transfer =
+            coded.v.transfer == TRANSFER_UNSPECIFIED ? def.v.transfer : coded.v.transfer;
+    merged.set().matrix = coded.v.matrix == MATRIX_UNSPECIFIED ? def.v.matrix : coded.v.matrix;
+    return C2R::Ok();
+}
+
 C2VDAComponent::IntfImpl::IntfImpl(C2String name, const std::shared_ptr<C2ReflectorHelper>& helper)
       : C2InterfaceHelper(helper), mInitStatus(C2_OK) {
     setDerivedInstance(this);
 
     // TODO(johnylin): use factory function to determine whether V4L2 stream or slice API is.
-    uint32_t inputFormatFourcc;
     char inputMime[128];
     if (name == kH264DecoderName || name == kH264SecureDecoderName) {
         strcpy(inputMime, MEDIA_MIMETYPE_VIDEO_AVC);
-        inputFormatFourcc = V4L2_PIX_FMT_H264_SLICE;
+        mInputCodec = InputCodec::H264;
+        addParameter(
+                DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
+                        .withDefault(new C2StreamProfileLevelInfo::input(
+                                0u, C2Config::PROFILE_AVC_MAIN, C2Config::LEVEL_AVC_4))
+                        .withFields(
+                                {C2F(mProfileLevel, profile)
+                                         .oneOf({C2Config::PROFILE_AVC_BASELINE,
+                                                 C2Config::PROFILE_AVC_CONSTRAINED_BASELINE,
+                                                 C2Config::PROFILE_AVC_MAIN,
+                                                 C2Config::PROFILE_AVC_HIGH,
+                                                 C2Config::PROFILE_AVC_CONSTRAINED_HIGH}),
+                                 C2F(mProfileLevel, level)
+                                         .oneOf({C2Config::LEVEL_AVC_1, C2Config::LEVEL_AVC_1B,
+                                                 C2Config::LEVEL_AVC_1_1, C2Config::LEVEL_AVC_1_2,
+                                                 C2Config::LEVEL_AVC_1_3, C2Config::LEVEL_AVC_2,
+                                                 C2Config::LEVEL_AVC_2_1, C2Config::LEVEL_AVC_2_2,
+                                                 C2Config::LEVEL_AVC_3, C2Config::LEVEL_AVC_3_1,
+                                                 C2Config::LEVEL_AVC_3_2, C2Config::LEVEL_AVC_4,
+                                                 C2Config::LEVEL_AVC_4_1, C2Config::LEVEL_AVC_4_2,
+                                                 C2Config::LEVEL_AVC_5, C2Config::LEVEL_AVC_5_1,
+                                                 C2Config::LEVEL_AVC_5_2})})
+                        .withSetter(ProfileLevelSetter)
+                        .build());
     } else if (name == kVP8DecoderName || name == kVP8SecureDecoderName) {
         strcpy(inputMime, MEDIA_MIMETYPE_VIDEO_VP8);
-        inputFormatFourcc = V4L2_PIX_FMT_VP8_FRAME;
+        mInputCodec = InputCodec::VP8;
+        addParameter(DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
+                             .withConstValue(new C2StreamProfileLevelInfo::input(
+                                     0u, C2Config::PROFILE_UNUSED, C2Config::LEVEL_UNUSED))
+                             .build());
     } else if (name == kVP9DecoderName || name == kVP9SecureDecoderName) {
         strcpy(inputMime, MEDIA_MIMETYPE_VIDEO_VP9);
-        inputFormatFourcc = V4L2_PIX_FMT_VP9_FRAME;
+        mInputCodec = InputCodec::VP9;
+        addParameter(
+                DefineParam(mProfileLevel, C2_PARAMKEY_PROFILE_LEVEL)
+                        .withDefault(new C2StreamProfileLevelInfo::input(
+                                0u, C2Config::PROFILE_VP9_0, C2Config::LEVEL_VP9_5))
+                        .withFields({C2F(mProfileLevel, profile).oneOf({C2Config::PROFILE_VP9_0}),
+                                     C2F(mProfileLevel, level)
+                                             .oneOf({C2Config::LEVEL_VP9_1, C2Config::LEVEL_VP9_1_1,
+                                                     C2Config::LEVEL_VP9_2, C2Config::LEVEL_VP9_2_1,
+                                                     C2Config::LEVEL_VP9_3, C2Config::LEVEL_VP9_3_1,
+                                                     C2Config::LEVEL_VP9_4, C2Config::LEVEL_VP9_4_1,
+                                                     C2Config::LEVEL_VP9_5})})
+                        .withSetter(ProfileLevelSetter)
+                        .build());
     } else {
         ALOGE("Invalid component name: %s", name.c_str());
         mInitStatus = C2_BAD_VALUE;
@@ -92,12 +212,12 @@
     //       ARC++.
     media::VideoDecodeAccelerator::SupportedProfiles supportedProfiles;
 #ifdef V4L2_CODEC2_ARC
-    supportedProfiles = arc::C2VDAAdaptorProxy::GetSupportedProfiles(inputFormatFourcc);
+    supportedProfiles = arc::C2VDAAdaptorProxy::GetSupportedProfiles(mInputCodec);
 #else
-    supportedProfiles = C2VDAAdaptor::GetSupportedProfiles(inputFormatFourcc);
+    supportedProfiles = C2VDAAdaptor::GetSupportedProfiles(mInputCodec);
 #endif
     if (supportedProfiles.empty()) {
-        ALOGE("No supported profile from input format: %u", inputFormatFourcc);
+        ALOGE("No supported profile from input codec: %d", mInputCodec);
         mInitStatus = C2_BAD_VALUE;
         return;
     }
@@ -126,23 +246,42 @@
                                  MEDIA_MIMETYPE_VIDEO_RAW))
                          .build());
 
-    struct LocalSetter {
-        static C2R SizeSetter(bool mayBlock, C2P<C2StreamPictureSizeInfo::output>& videoSize) {
-            (void)mayBlock;
-            // TODO: maybe apply block limit?
-            return videoSize.F(videoSize.v.width)
-                    .validatePossible(videoSize.v.width)
-                    .plus(videoSize.F(videoSize.v.height).validatePossible(videoSize.v.height));
-        }
-    };
-
     addParameter(DefineParam(mSize, C2_PARAMKEY_STREAM_PICTURE_SIZE)
                          .withDefault(new C2StreamPictureSizeInfo::output(0u, 176, 144))
                          .withFields({
                                  C2F(mSize, width).inRange(minSize.width(), maxSize.width(), 16),
                                  C2F(mSize, height).inRange(minSize.height(), maxSize.height(), 16),
                          })
-                         .withSetter(LocalSetter::SizeSetter)
+                         .withSetter(SizeSetter)
+                         .build());
+
+    // App may set a smaller value for maximum of input buffer size than actually required
+    // by mistake. C2VDAComponent overrides it if the value specified by app is smaller than
+    // the calculated value in MaxSizeCalculator().
+    // This value is the default maximum of linear buffer size (kLinearBufferSize) in
+    // CCodecBufferChannel.cpp.
+    constexpr static size_t kLinearBufferSize = 1048576;
+    struct LocalCalculator {
+        static C2R MaxSizeCalculator(bool mayBlock, C2P<C2StreamMaxBufferSizeInfo::input>& me,
+                                     const C2P<C2StreamPictureSizeInfo::output>& size) {
+            (void)mayBlock;
+            // TODO: Need larger size?
+            me.set().value = kLinearBufferSize;
+            const uint32_t width = size.v.width;
+            const uint32_t height = size.v.height;
+            // Enlarge the input buffer for 4k video
+            if ((width > 1920 && height > 1080)) {
+                me.set().value = 4 * kLinearBufferSize;
+            }
+            return C2R::Ok();
+        }
+    };
+    addParameter(DefineParam(mMaxInputSize, C2_PARAMKEY_INPUT_MAX_BUFFER_SIZE)
+                         .withDefault(new C2StreamMaxBufferSizeInfo::input(0u, kLinearBufferSize))
+                         .withFields({
+                                 C2F(mMaxInputSize, value).any(),
+                         })
+                         .calculatedAs(LocalCalculator::MaxSizeCalculator, mSize)
                          .build());
 
     bool secureMode = name.find(".secure") != std::string::npos;
@@ -177,6 +316,63 @@
                                  C2F(mOutputBlockPoolIds, m.values).inRange(0, 1)})
                     .withSetter(Setter<C2PortBlockPoolsTuning::output>::NonStrictValuesWithNoDeps)
                     .build());
+
+    addParameter(
+            DefineParam(mDefaultColorAspects, C2_PARAMKEY_DEFAULT_COLOR_ASPECTS)
+                    .withDefault(new C2StreamColorAspectsTuning::output(
+                            0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
+                            C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
+                    .withFields(
+                            {C2F(mDefaultColorAspects, range)
+                                     .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
+                             C2F(mDefaultColorAspects, primaries)
+                                     .inRange(C2Color::PRIMARIES_UNSPECIFIED,
+                                              C2Color::PRIMARIES_OTHER),
+                             C2F(mDefaultColorAspects, transfer)
+                                     .inRange(C2Color::TRANSFER_UNSPECIFIED,
+                                              C2Color::TRANSFER_OTHER),
+                             C2F(mDefaultColorAspects, matrix)
+                                     .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)})
+                    .withSetter(DefaultColorAspectsSetter)
+                    .build());
+
+    addParameter(
+            DefineParam(mCodedColorAspects, C2_PARAMKEY_VUI_COLOR_ASPECTS)
+                    .withDefault(new C2StreamColorAspectsInfo::input(
+                            0u, C2Color::RANGE_LIMITED, C2Color::PRIMARIES_UNSPECIFIED,
+                            C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
+                    .withFields(
+                            {C2F(mCodedColorAspects, range)
+                                     .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
+                             C2F(mCodedColorAspects, primaries)
+                                     .inRange(C2Color::PRIMARIES_UNSPECIFIED,
+                                              C2Color::PRIMARIES_OTHER),
+                             C2F(mCodedColorAspects, transfer)
+                                     .inRange(C2Color::TRANSFER_UNSPECIFIED,
+                                              C2Color::TRANSFER_OTHER),
+                             C2F(mCodedColorAspects, matrix)
+                                     .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)})
+                    .withSetter(DefaultColorAspectsSetter)
+                    .build());
+
+    addParameter(
+            DefineParam(mColorAspects, C2_PARAMKEY_COLOR_ASPECTS)
+                    .withDefault(new C2StreamColorAspectsInfo::output(
+                            0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
+                            C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED))
+                    .withFields(
+                            {C2F(mColorAspects, range)
+                                     .inRange(C2Color::RANGE_UNSPECIFIED, C2Color::RANGE_OTHER),
+                             C2F(mColorAspects, primaries)
+                                     .inRange(C2Color::PRIMARIES_UNSPECIFIED,
+                                              C2Color::PRIMARIES_OTHER),
+                             C2F(mColorAspects, transfer)
+                                     .inRange(C2Color::TRANSFER_UNSPECIFIED,
+                                              C2Color::TRANSFER_OTHER),
+                             C2F(mColorAspects, matrix)
+                                     .inRange(C2Color::MATRIX_UNSPECIFIED, C2Color::MATRIX_OTHER)})
+                    .withSetter(MergedColorAspectsSetter, mDefaultColorAspects, mCodedColorAspects)
+                    .build());
 }
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -208,6 +404,8 @@
         mVDAInitResult(VideoDecodeAcceleratorAdaptor::Result::ILLEGAL_STATE),
         mComponentState(ComponentState::UNINITIALIZED),
         mPendingOutputEOS(false),
+        mPendingColorAspectsChange(false),
+        mPendingColorAspectsChangeFrameIndex(0),
         mCodecProfile(media::VIDEO_CODEC_PROFILE_UNKNOWN),
         mState(State::UNLOADED),
         mWeakThisFactory(this) {
@@ -227,8 +425,6 @@
 }
 
 C2VDAComponent::~C2VDAComponent() {
-    CHECK_EQ(mState.load(), State::LOADED);
-
     if (mThread.IsRunning()) {
         mTaskRunner->PostTask(FROM_HERE,
                               ::base::Bind(&C2VDAComponent::onDestroy, ::base::Unretained(this)));
@@ -262,6 +458,12 @@
         mComponentState = ComponentState::STARTED;
     }
 
+    if (!mSecureMode && mIntfImpl->getInputCodec() == InputCodec::H264) {
+        // Get default color aspects on start.
+        updateColorAspects();
+        mPendingColorAspectsChange = false;
+    }
+
     done->Signal();
 }
 
@@ -305,10 +507,14 @@
     mQueue.pop();
 
     CHECK_LE(work->input.buffers.size(), 1u);
+    bool isEmptyCSDWork = false;
+    // Use frameIndex as bitstreamId.
+    int32_t bitstreamId = frameIndexToBitstreamId(work->input.ordinal.frameIndex);
     if (work->input.buffers.empty()) {
         // Client may queue a work with no input buffer for either it's EOS or empty CSD, otherwise
         // every work must have one input buffer.
-        CHECK(drainMode != NO_DRAIN || work->input.flags & C2FrameData::FLAG_CODEC_CONFIG);
+        isEmptyCSDWork = work->input.flags & C2FrameData::FLAG_CODEC_CONFIG;
+        CHECK(drainMode != NO_DRAIN || isEmptyCSDWork);
         // Emplace a nullptr to unify the check for work done.
         ALOGV("Got a work with no input buffer! Emplace a nullptr inside.");
         work->input.buffers.emplace_back(nullptr);
@@ -316,9 +522,21 @@
         // If input.buffers is not empty, the buffer should have meaningful content inside.
         C2ConstLinearBlock linearBlock = work->input.buffers.front()->data().linearBlocks().front();
         CHECK_GT(linearBlock.size(), 0u);
+
+        // Call parseCodedColorAspects() to try to parse color aspects from bitstream only if:
+        // 1) This is non-secure decoding.
+        // 2) This is H264 codec.
+        // 3) This input is CSD buffer (with flags FLAG_CODEC_CONFIG).
+        if (!mSecureMode && (mIntfImpl->getInputCodec() == InputCodec::H264) &&
+            (work->input.flags & C2FrameData::FLAG_CODEC_CONFIG)) {
+            if (parseCodedColorAspects(linearBlock)) {
+                // Record current frame index, color aspects should be updated only for output
+                // buffers whose frame indices are not less than this one.
+                mPendingColorAspectsChange = true;
+                mPendingColorAspectsChangeFrameIndex = work->input.ordinal.frameIndex.peeku();
+            }
+        }
         // Send input buffer to VDA for decode.
-        // Use frameIndex as bitstreamId.
-        int32_t bitstreamId = frameIndexToBitstreamId(work->input.ordinal.frameIndex);
         sendInputBufferToAccelerator(linearBlock, bitstreamId);
     }
 
@@ -335,6 +553,10 @@
 
     // Put work to mPendingWorks.
     mPendingWorks.emplace_back(std::move(work));
+    if (isEmptyCSDWork) {
+        // Directly report the empty CSD work as finished.
+        reportWorkIfFinished(bitstreamId);
+    }
 
     if (!mQueue.empty()) {
         mTaskRunner->PostTask(FROM_HERE, ::base::Bind(&C2VDAComponent::onDequeueWork,
@@ -356,7 +578,7 @@
     // When the work is done, the input buffer shall be reset by component.
     work->input.buffers.front().reset();
 
-    reportFinishedWorkIfAny();
+    reportWorkIfFinished(bitstreamId);
 }
 
 void C2VDAComponent::onOutputBufferReturned(std::shared_ptr<C2GraphicBlock> block,
@@ -389,7 +611,14 @@
     if (mPendingOutputFormat) {
         tryChangeOutputFormat();
     } else {
-        sendOutputBufferToAccelerator(info);
+        // Do not pass the ownership to accelerator if this buffer will still be reused under
+        // |mPendingBuffersToWork|.
+        auto existingFrame = std::find_if(
+                mPendingBuffersToWork.begin(), mPendingBuffersToWork.end(),
+                [id = info->mBlockId](const OutputBufferInfo& o) { return o.mBlockId == id; });
+        bool ownByAccelerator = existingFrame == mPendingBuffersToWork.end();
+        sendOutputBufferToAccelerator(info, ownByAccelerator);
+        sendOutputBufferToWorkIfAny(false /* dropIfUnavailable */);
     }
 }
 
@@ -398,31 +627,79 @@
     ALOGV("onOutputBufferDone: picture id=%d, bitstream id=%d", pictureBufferId, bitstreamId);
     EXPECT_RUNNING_OR_RETURN_ON_ERROR();
 
-    C2Work* work = getPendingWorkByBitstreamId(bitstreamId);
-    if (!work) {
-        reportError(C2_CORRUPTED);
-        return;
-    }
     GraphicBlockInfo* info = getGraphicBlockById(pictureBufferId);
     if (!info) {
         reportError(C2_CORRUPTED);
         return;
     }
-    CHECK_EQ(info->mState, GraphicBlockInfo::State::OWNED_BY_ACCELERATOR);
-    // Output buffer will be passed to client soon along with mListener->onWorkDone_nb().
-    info->mState = GraphicBlockInfo::State::OWNED_BY_CLIENT;
-    mBuffersInClient++;
 
-    // Attach output buffer to the work corresponded to bitstreamId.
-    C2ConstGraphicBlock constBlock = info->mGraphicBlock->share(
-            C2Rect(mOutputFormat.mVisibleRect.width(), mOutputFormat.mVisibleRect.height()),
-            C2Fence());
-    MarkBlockPoolDataAsShared(constBlock);
-    work->worklets.front()->output.buffers.emplace_back(
-            C2Buffer::CreateGraphicBuffer(std::move(constBlock)));
-    info->mGraphicBlock.reset();
+    if (info->mState == GraphicBlockInfo::State::OWNED_BY_ACCELERATOR) {
+        info->mState = GraphicBlockInfo::State::OWNED_BY_COMPONENT;
+    }
+    mPendingBuffersToWork.push_back({bitstreamId, pictureBufferId});
+    sendOutputBufferToWorkIfAny(false /* dropIfUnavailable */);
+}
 
-    reportFinishedWorkIfAny();
+void C2VDAComponent::sendOutputBufferToWorkIfAny(bool dropIfUnavailable) {
+    DCHECK(mTaskRunner->BelongsToCurrentThread());
+
+    while (!mPendingBuffersToWork.empty()) {
+        auto nextBuffer = mPendingBuffersToWork.front();
+        GraphicBlockInfo* info = getGraphicBlockById(nextBuffer.mBlockId);
+        CHECK_NE(info->mState, GraphicBlockInfo::State::OWNED_BY_ACCELERATOR);
+
+        C2Work* work = getPendingWorkByBitstreamId(nextBuffer.mBitstreamId);
+        if (!work) {
+            reportError(C2_CORRUPTED);
+            return;
+        }
+
+        if (info->mState == GraphicBlockInfo::State::OWNED_BY_CLIENT) {
+            // This buffer is the existing frame and still owned by client.
+            if (!dropIfUnavailable &&
+                std::find(mUndequeuedBlockIds.begin(), mUndequeuedBlockIds.end(),
+                          nextBuffer.mBlockId) == mUndequeuedBlockIds.end()) {
+                ALOGV("Still waiting for existing frame returned from client...");
+                return;
+            }
+            ALOGV("Drop this frame...");
+            sendOutputBufferToAccelerator(info, false /* ownByAccelerator */);
+            work->worklets.front()->output.flags = C2FrameData::FLAG_DROP_FRAME;
+        } else {
+            // This buffer is ready to push into the corresponding work.
+            // Output buffer will be passed to client soon along with mListener->onWorkDone_nb().
+            info->mState = GraphicBlockInfo::State::OWNED_BY_CLIENT;
+            mBuffersInClient++;
+            updateUndequeuedBlockIds(info->mBlockId);
+
+            // Attach output buffer to the work corresponded to bitstreamId.
+            C2ConstGraphicBlock constBlock = info->mGraphicBlock->share(
+                    C2Rect(mOutputFormat.mVisibleRect.width(),
+                           mOutputFormat.mVisibleRect.height()),
+                    C2Fence());
+            MarkBlockPoolDataAsShared(constBlock);
+
+            std::shared_ptr<C2Buffer> buffer = C2Buffer::CreateGraphicBuffer(std::move(constBlock));
+            if (mPendingColorAspectsChange &&
+                work->input.ordinal.frameIndex.peeku() >= mPendingColorAspectsChangeFrameIndex) {
+                updateColorAspects();
+                mPendingColorAspectsChange = false;
+            }
+            if (mCurrentColorAspects) {
+                buffer->setInfo(mCurrentColorAspects);
+            }
+            work->worklets.front()->output.buffers.emplace_back(std::move(buffer));
+            info->mGraphicBlock.reset();
+        }
+        reportWorkIfFinished(nextBuffer.mBitstreamId);
+        mPendingBuffersToWork.pop_front();
+    }
+}
+
+void C2VDAComponent::updateUndequeuedBlockIds(int32_t blockId) {
+    // The size of |mUndequedBlockIds| will always be the minimum buffer count for display.
+    mUndequeuedBlockIds.push_back(blockId);
+    mUndequeuedBlockIds.pop_front();
 }
 
 void C2VDAComponent::onDrain(uint32_t drainMode) {
@@ -468,6 +745,10 @@
         return;
     }
 
+    // Drop all pending existing frames and return all finished works before drain done.
+    sendOutputBufferToWorkIfAny(true /* dropIfUnavailable */);
+    CHECK(mPendingBuffersToWork.empty());
+
     if (mPendingOutputEOS) {
         // Return EOS work.
         reportEOSWork();
@@ -536,6 +817,7 @@
 void C2VDAComponent::onFlushDone() {
     ALOGV("onFlushDone");
     reportAbandonedWorks();
+    mPendingBuffersToWork.clear();
     mComponentState = ComponentState::STARTED;
 
     // Work dequeueing was stopped while component flushing. Restart it.
@@ -551,14 +833,14 @@
     // do something for them?
     reportAbandonedWorks();
     mPendingOutputFormat.reset();
+    mPendingBuffersToWork.clear();
     if (mVDAAdaptor.get()) {
         mVDAAdaptor->destroy();
         mVDAAdaptor.reset(nullptr);
     }
 
-    mGraphicBlocks.clear();
-
     stopDequeueThread();
+    mGraphicBlocks.clear();
 
     mStopDoneEvent->Signal();
     mStopDoneEvent = nullptr;
@@ -592,13 +874,17 @@
     mVDAAdaptor->decode(bitstreamId, dupFd, input.offset(), input.size());
 }
 
-C2Work* C2VDAComponent::getPendingWorkByBitstreamId(int32_t bitstreamId) {
-    auto workIter = std::find_if(mPendingWorks.begin(), mPendingWorks.end(),
-                                 [bitstreamId](const std::unique_ptr<C2Work>& w) {
-                                     return frameIndexToBitstreamId(w->input.ordinal.frameIndex) ==
-                                            bitstreamId;
-                                 });
+std::deque<std::unique_ptr<C2Work>>::iterator C2VDAComponent::findPendingWorkByBitstreamId(
+        int32_t bitstreamId) {
+    return std::find_if(mPendingWorks.begin(), mPendingWorks.end(),
+                        [bitstreamId](const std::unique_ptr<C2Work>& w) {
+                            return frameIndexToBitstreamId(w->input.ordinal.frameIndex) ==
+                                   bitstreamId;
+                        });
+}
 
+C2Work* C2VDAComponent::getPendingWorkByBitstreamId(int32_t bitstreamId) {
+    auto workIter = findPendingWorkByBitstreamId(bitstreamId);
     if (workIter == mPendingWorks.end()) {
         ALOGE("Can't find pending work by bitstream ID: %d", bitstreamId);
         return nullptr;
@@ -662,6 +948,10 @@
         CHECK(info.mState != GraphicBlockInfo::State::OWNED_BY_ACCELERATOR);
     }
 
+    // Drop all pending existing frames and return all finished works before changing output format.
+    sendOutputBufferToWorkIfAny(true /* dropIfUnavailable */);
+    CHECK(mPendingBuffersToWork.empty());
+
     CHECK_EQ(mPendingOutputFormat->mPixelFormat, HalPixelFormat::YCbCr_420_888);
 
     mOutputFormat.mPixelFormat = mPendingOutputFormat->mPixelFormat;
@@ -679,7 +969,7 @@
     }
 
     for (auto& info : mGraphicBlocks) {
-        sendOutputBufferToAccelerator(&info);
+        sendOutputBufferToAccelerator(&info, true /* ownByAccelerator */);
     }
     mPendingOutputFormat.reset();
 }
@@ -709,6 +999,7 @@
     mGraphicBlocks.clear();
 
     bool useBufferQueue = blockPool->getAllocatorId() == C2PlatformAllocatorStore::BUFFERQUEUE;
+    size_t minBuffersForDisplay = 0;
     if (useBufferQueue) {
         ALOGV("Bufferqueue-backed block pool is used.");
         // Set requested buffer count to C2VdaBqBlockPool.
@@ -721,6 +1012,12 @@
                 reportError(err);
                 return err;
             }
+            err = bqPool->getMinBuffersForDisplay(&minBuffersForDisplay);
+            if (err != C2_OK) {
+                ALOGE("failed to query minimum undequeued buffer count from block pool: %d", err);
+                reportError(err);
+                return err;
+            }
         } else {
             ALOGE("static_pointer_cast C2VdaBqBlockPool failed...");
             reportError(C2_CORRUPTED);
@@ -738,6 +1035,7 @@
                 reportError(err);
                 return err;
             }
+            minBuffersForDisplay = 0;  // no undequeued buffer restriction for bufferpool.
         } else {
             ALOGE("static_pointer_cast C2VdaPooledBlockPool failed...");
             reportError(C2_CORRUPTED);
@@ -745,6 +1043,9 @@
         }
     }
 
+    ALOGV("Minimum undequeued buffer count = %zu", minBuffersForDisplay);
+    mUndequeuedBlockIds.resize(minBuffersForDisplay, -1);
+
     for (size_t i = 0; i < bufferCount; ++i) {
         std::shared_ptr<C2GraphicBlock> block;
         C2MemoryUsage usage = {
@@ -786,7 +1087,8 @@
     }
     mOutputFormat.mMinNumBuffers = bufferCount;
 
-    if (!startDequeueThread(size, pixelFormat, std::move(blockPool))) {
+    if (!startDequeueThread(size, pixelFormat, std::move(blockPool),
+                            true /* resetBuffersInClient */)) {
         reportError(C2_CORRUPTED);
         return C2_CORRUPTED;
     }
@@ -894,10 +1196,15 @@
 #endif // V4L2_CODEC2_ARC
 }
 
-void C2VDAComponent::sendOutputBufferToAccelerator(GraphicBlockInfo* info) {
-    ALOGV("sendOutputBufferToAccelerator index=%d", info->mBlockId);
-    CHECK_EQ(info->mState, GraphicBlockInfo::State::OWNED_BY_COMPONENT);
-    info->mState = GraphicBlockInfo::State::OWNED_BY_ACCELERATOR;
+void C2VDAComponent::sendOutputBufferToAccelerator(GraphicBlockInfo* info, bool ownByAccelerator) {
+    DCHECK(mTaskRunner->BelongsToCurrentThread());
+    ALOGV("sendOutputBufferToAccelerator index=%d ownByAccelerator=%d", info->mBlockId,
+          ownByAccelerator);
+
+    if (ownByAccelerator) {
+        CHECK_EQ(info->mState, GraphicBlockInfo::State::OWNED_BY_COMPONENT);
+        info->mState = GraphicBlockInfo::State::OWNED_BY_ACCELERATOR;
+    }
 
     // is_valid() is true for the first time the buffer is passed to VDA. In that case, VDA needs to
     // import the buffer first.
@@ -909,6 +1216,88 @@
     }
 }
 
+bool C2VDAComponent::parseCodedColorAspects(const C2ConstLinearBlock& input) {
+    C2ReadView view = input.map().get();
+    const uint8_t* data = view.data();
+    const uint32_t size = view.capacity();
+
+    std::unique_ptr<media::H264Parser> h264Parser = std::make_unique<media::H264Parser>();
+    h264Parser->SetStream(data, static_cast<off_t>(size));
+    media::H264NALU nalu;
+    media::H264Parser::Result parRes = h264Parser->AdvanceToNextNALU(&nalu);
+    if (parRes != media::H264Parser::kEOStream && parRes != media::H264Parser::kOk) {
+        ALOGE("H264 AdvanceToNextNALU error: %d", static_cast<int>(parRes));
+        return false;
+    }
+    if (nalu.nal_unit_type != media::H264NALU::kSPS) {
+        ALOGV("NALU is not SPS");
+        return false;
+    }
+
+    int spsId;
+    parRes = h264Parser->ParseSPS(&spsId);
+    if (parRes != media::H264Parser::kEOStream && parRes != media::H264Parser::kOk) {
+        ALOGE("H264 ParseSPS error: %d", static_cast<int>(parRes));
+        return false;
+    }
+
+    // Parse ISO color aspects from H264 SPS bitstream.
+    const media::H264SPS* sps = h264Parser->GetSPS(spsId);
+    if (!sps->colour_description_present_flag) {
+        ALOGV("No Color Description in SPS");
+        return false;
+    }
+    int32_t primaries = sps->colour_primaries;
+    int32_t transfer = sps->transfer_characteristics;
+    int32_t coeffs = sps->matrix_coefficients;
+    bool fullRange = sps->video_full_range_flag;
+
+    // Convert ISO color aspects to ColorUtils::ColorAspects.
+    ColorAspects colorAspects;
+    ColorUtils::convertIsoColorAspectsToCodecAspects(primaries, transfer, coeffs, fullRange,
+                                                     colorAspects);
+    ALOGV("Parsed ColorAspects from bitstream: (R:%d, P:%d, M:%d, T:%d)", colorAspects.mRange,
+          colorAspects.mPrimaries, colorAspects.mMatrixCoeffs, colorAspects.mTransfer);
+
+    // Map ColorUtils::ColorAspects to C2StreamColorAspectsInfo::input parameter.
+    C2StreamColorAspectsInfo::input codedAspects = {0u};
+    if (!C2Mapper::map(colorAspects.mPrimaries, &codedAspects.primaries)) {
+        codedAspects.primaries = C2Color::PRIMARIES_UNSPECIFIED;
+    }
+    if (!C2Mapper::map(colorAspects.mRange, &codedAspects.range)) {
+        codedAspects.range = C2Color::RANGE_UNSPECIFIED;
+    }
+    if (!C2Mapper::map(colorAspects.mMatrixCoeffs, &codedAspects.matrix)) {
+        codedAspects.matrix = C2Color::MATRIX_UNSPECIFIED;
+    }
+    if (!C2Mapper::map(colorAspects.mTransfer, &codedAspects.transfer)) {
+        codedAspects.transfer = C2Color::TRANSFER_UNSPECIFIED;
+    }
+    // Configure to interface.
+    std::vector<std::unique_ptr<C2SettingResult>> failures;
+    c2_status_t status = mIntfImpl->config({&codedAspects}, C2_MAY_BLOCK, &failures);
+    if (status != C2_OK) {
+        ALOGE("Failed to config color aspects to interface, error: %d", status);
+        return false;
+    }
+    return true;
+}
+
+c2_status_t C2VDAComponent::updateColorAspects() {
+    ALOGV("updateColorAspects");
+    std::unique_ptr<C2StreamColorAspectsInfo::output> colorAspects =
+            std::make_unique<C2StreamColorAspectsInfo::output>(
+                    0u, C2Color::RANGE_UNSPECIFIED, C2Color::PRIMARIES_UNSPECIFIED,
+                    C2Color::TRANSFER_UNSPECIFIED, C2Color::MATRIX_UNSPECIFIED);
+    c2_status_t status = mIntfImpl->query({colorAspects.get()}, {}, C2_DONT_BLOCK, nullptr);
+    if (status != C2_OK) {
+        ALOGE("Failed to query color aspects, error: %d", status);
+        return status;
+    }
+    mCurrentColorAspects = std::move(colorAspects);
+    return C2_OK;
+}
+
 void C2VDAComponent::onVisibleRectChanged(const media::Rect& cropRect) {
     DCHECK(mTaskRunner->BelongsToCurrentThread());
     ALOGV("onVisibleRectChanged");
@@ -927,6 +1316,87 @@
     mOutputFormat.mVisibleRect = cropRect;
 }
 
+void C2VDAComponent::onSurfaceChanged() {
+    DCHECK(mTaskRunner->BelongsToCurrentThread());
+    ALOGV("onSurfaceChanged");
+
+    if (mComponentState == ComponentState::UNINITIALIZED) {
+        return;  // Component is already stopped, no need to update graphic blocks.
+    }
+
+    stopDequeueThread();
+
+    // Get block pool ID configured from the client.
+    std::shared_ptr<C2BlockPool> blockPool;
+    auto blockPoolId = mIntfImpl->getBlockPoolId();
+    ALOGI("Retrieving C2BlockPool ID = %" PRIu64 " for updating output buffers", blockPoolId);
+    auto err = GetCodec2BlockPool(blockPoolId, shared_from_this(), &blockPool);
+    if (err != C2_OK) {
+        ALOGE("Graphic block allocator is invalid");
+        reportError(err);
+        return;
+    }
+    if (blockPool->getAllocatorId() != C2PlatformAllocatorStore::BUFFERQUEUE) {
+        ALOGE("Only Bufferqueue-backed block pool would need to change surface.");
+        reportError(C2_CORRUPTED);
+        return;
+    }
+
+    std::shared_ptr<C2VdaBqBlockPool> bqPool =
+            std::static_pointer_cast<C2VdaBqBlockPool>(blockPool);
+    if (!bqPool) {
+        ALOGE("static_pointer_cast C2VdaBqBlockPool failed...");
+        reportError(C2_CORRUPTED);
+        return;
+    }
+
+    size_t minBuffersForDisplay = 0;
+    err = bqPool->getMinBuffersForDisplay(&minBuffersForDisplay);
+    if (err != C2_OK) {
+        ALOGE("failed to query minimum undequeued buffer count from block pool: %d", err);
+        reportError(err);
+        return;
+    }
+    ALOGV("Minimum undequeued buffer count = %zu", minBuffersForDisplay);
+    mUndequeuedBlockIds.resize(minBuffersForDisplay, -1);
+
+    for (auto& info : mGraphicBlocks) {
+        bool willCancel = (info.mGraphicBlock == nullptr);
+        uint32_t oldSlot = info.mPoolId;
+        ALOGV("Updating graphic block #%d: slot = %u, willCancel = %d", info.mBlockId, oldSlot,
+              willCancel);
+        uint32_t newSlot;
+        std::shared_ptr<C2GraphicBlock> block;
+        err = bqPool->updateGraphicBlock(willCancel, oldSlot, &newSlot, &block);
+        if (err == C2_CANCELED) {
+            // There may be a chance that a task in task runner before onSurfaceChange triggers
+            // output format change. If so, block pool will return C2_CANCELED and no need to
+            // updateGraphicBlock anymore.
+            return;
+        }
+        if (err != C2_OK) {
+            ALOGE("failed to update graphic block from block pool: %d", err);
+            reportError(err);
+            return;
+        }
+
+        // Update slot index.
+        info.mPoolId = newSlot;
+        // Update C2GraphicBlock if |willCancel| is false. Note that although the old C2GraphicBlock
+        // will be released, the block pool data destructor won't do detachBuffer to new surface
+        // because the producer ID is not matched.
+        if (!willCancel) {
+            info.mGraphicBlock = std::move(block);
+        }
+    }
+
+    if (!startDequeueThread(mOutputFormat.mCodedSize,
+                            static_cast<uint32_t>(mOutputFormat.mPixelFormat), std::move(blockPool),
+                            false /* resetBuffersInClient */)) {
+        reportError(C2_CORRUPTED);
+    }
+}
+
 c2_status_t C2VDAComponent::queue_nb(std::list<std::unique_ptr<C2Work>>* const items) {
     if (mState.load() != State::RUNNING) {
         return C2_BAD_STATE;
@@ -989,9 +1459,10 @@
                           ::base::Bind(&C2VDAComponent::onStart, ::base::Unretained(this),
                                        mCodecProfile, &done));
     done.Wait();
-    if (mVDAInitResult != VideoDecodeAcceleratorAdaptor::Result::SUCCESS) {
-        ALOGE("Failed to start component due to VDA error: %d", static_cast<int>(mVDAInitResult));
-        return C2_CORRUPTED;
+    c2_status_t c2Status = adaptorResultToC2Status(mVDAInitResult);
+    if (c2Status != C2_OK) {
+        ALOGE("Failed to start component due to VDA error...");
+        return c2Status;
     }
     mState.store(State::RUNNING);
     return C2_OK;
@@ -1080,49 +1551,41 @@
 }
 
 void C2VDAComponent::notifyError(VideoDecodeAcceleratorAdaptor::Result error) {
-    ALOGE("Got notifyError from VDA error=%d", error);
-    c2_status_t err;
-    switch (error) {
-    case VideoDecodeAcceleratorAdaptor::Result::ILLEGAL_STATE:
-        err = C2_BAD_STATE;
-        break;
-    case VideoDecodeAcceleratorAdaptor::Result::INVALID_ARGUMENT:
-    case VideoDecodeAcceleratorAdaptor::Result::UNREADABLE_INPUT:
-        err = C2_BAD_VALUE;
-        break;
-    case VideoDecodeAcceleratorAdaptor::Result::PLATFORM_FAILURE:
-        err = C2_CORRUPTED;
-        break;
-    case VideoDecodeAcceleratorAdaptor::Result::INSUFFICIENT_RESOURCES:
-        err = C2_NO_MEMORY;
-        break;
-    case VideoDecodeAcceleratorAdaptor::Result::SUCCESS:
-        ALOGE("Shouldn't get SUCCESS err code in NotifyError(). Skip it...");
+    ALOGE("Got notifyError from VDA...");
+    c2_status_t err = adaptorResultToC2Status(error);
+    if (err == C2_OK) {
+        ALOGW("Shouldn't get SUCCESS err code in NotifyError(). Skip it...");
         return;
     }
     reportError(err);
 }
 
-void C2VDAComponent::reportFinishedWorkIfAny() {
+void C2VDAComponent::reportWorkIfFinished(int32_t bitstreamId) {
     DCHECK(mTaskRunner->BelongsToCurrentThread());
-    std::list<std::unique_ptr<C2Work>> finishedWorks;
 
-    // Work should be reported as done if both input and output buffer are returned by VDA.
-    // EOS work will not be reported here. reportEOSWork() does it.
-    auto iter = mPendingWorks.begin();
-    while (iter != mPendingWorks.end()) {
-        if (isWorkDone(iter->get())) {
-            iter->get()->result = C2_OK;
-            iter->get()->workletsProcessed = static_cast<uint32_t>(iter->get()->worklets.size());
-            finishedWorks.emplace_back(std::move(*iter));
-            iter = mPendingWorks.erase(iter);
-        } else {
-            ++iter;
-        }
+    auto workIter = findPendingWorkByBitstreamId(bitstreamId);
+    if (workIter == mPendingWorks.end()) {
+        reportError(C2_CORRUPTED);
+        return;
     }
 
-    if (!finishedWorks.empty()) {
+    // EOS work will not be reported here. reportEOSWork() does it.
+    auto work = workIter->get();
+    if (isWorkDone(work)) {
+        if (work->worklets.front()->output.flags & C2FrameData::FLAG_DROP_FRAME) {
+            // TODO: actually framework does not handle FLAG_DROP_FRAME, use C2_NOT_FOUND result to
+            //       let framework treat this as flushed work.
+            work->result = C2_NOT_FOUND;
+        } else {
+            work->result = C2_OK;
+        }
+        work->workletsProcessed = static_cast<uint32_t>(work->worklets.size());
+
+        ALOGV("Reported finished work index=%llu", work->input.ordinal.frameIndex.peekull());
+        std::list<std::unique_ptr<C2Work>> finishedWorks;
+        finishedWorks.emplace_back(std::move(*workIter));
         mListener->onWorkDone_nb(shared_from_this(), std::move(finishedWorks));
+        mPendingWorks.erase(workIter);
     }
 }
 
@@ -1141,11 +1604,13 @@
         return false;
     }
     if (!(work->input.flags & C2FrameData::FLAG_CODEC_CONFIG) &&
+        !(work->worklets.front()->output.flags & C2FrameData::FLAG_DROP_FRAME) &&
         work->worklets.front()->output.buffers.empty()) {
-        // Output buffer is not returned from VDA yet.
+        // Unless the input is CSD or the output is dropped, this work is not done because the
+        // output buffer is not returned from VDA yet.
         return false;
     }
-    return true;  // Output buffer is returned, or it has no related output buffer (CSD work).
+    return true;  // This work is done.
 }
 
 void C2VDAComponent::reportEOSWork() {
@@ -1215,14 +1680,17 @@
 }
 
 bool C2VDAComponent::startDequeueThread(const media::Size& size, uint32_t pixelFormat,
-                                        std::shared_ptr<C2BlockPool> blockPool) {
+                                        std::shared_ptr<C2BlockPool> blockPool,
+                                        bool resetBuffersInClient) {
     CHECK(!mDequeueThread.IsRunning());
     if (!mDequeueThread.Start()) {
         ALOGE("failed to start dequeue thread!!");
         return false;
     }
     mDequeueLoopStop.store(false);
-    mBuffersInClient.store(0u);
+    if (resetBuffersInClient) {
+        mBuffersInClient.store(0u);
+    }
     mDequeueThread.task_runner()->PostTask(
             FROM_HERE, ::base::Bind(&C2VDAComponent::dequeueThreadLoop, ::base::Unretained(this),
                                     size, pixelFormat, std::move(blockPool)));
@@ -1252,8 +1720,19 @@
         auto err = blockPool->fetchGraphicBlock(size.width(), size.height(), pixelFormat, usage,
                                                 &block);
         if (err == C2_TIMED_OUT) {
+            // Mutexes often do not care for FIFO. Practically the thread who is locking the mutex
+            // usually will be granted to lock again right thereafter. To make this loop not too
+            // bossy, the simpliest way is to add a short delay to the next time acquiring the
+            // lock. TODO (b/118354314): replace this if there is better solution.
+            ::usleep(1);
             continue;  // wait for retry
         }
+        if (err == C2_BAD_STATE) {
+            ALOGV("Got informed from block pool surface is changed.");
+            mTaskRunner->PostTask(FROM_HERE, ::base::Bind(&C2VDAComponent::onSurfaceChanged,
+                                                          ::base::Unretained(this)));
+            break;  // terminate the loop, will be resumed after onSurfaceChanged().
+        }
         if (err == C2_OK) {
             uint32_t poolId;
             if (blockPool->getAllocatorId() == C2PlatformAllocatorStore::BUFFERQUEUE) {
diff --git a/cmds/Android.mk b/cmds/Android.mk
index 6cc84ae..34d9af6 100644
--- a/cmds/Android.mk
+++ b/cmds/Android.mk
@@ -24,7 +24,6 @@
                           libgui \
                           liblog \
                           libmedia \
-                          libmediaextractor \
                           libstagefright \
                           libstagefright_codec2 \
                           libstagefright_foundation \
diff --git a/cmds/codec2.cpp b/cmds/codec2.cpp
index 9458bd3..fc84c29 100644
--- a/cmds/codec2.cpp
+++ b/cmds/codec2.cpp
@@ -23,11 +23,11 @@
 #include <media/DataSource.h>
 #include <media/ICrypto.h>
 #include <media/IMediaHTTPService.h>
-#include <media/MediaExtractor.h>
 #include <media/MediaSource.h>
 #include <media/stagefright/DataSourceFactory.h>
 #include <media/stagefright/MediaDefs.h>
 #include <media/stagefright/MediaErrors.h>
+#include <media/stagefright/MediaExtractor.h>
 #include <media/stagefright/MediaExtractorFactory.h>
 #include <media/stagefright/MetaData.h>
 #include <media/stagefright/Utils.h>
diff --git a/include/C2VDAAdaptor.h b/include/C2VDAAdaptor.h
index 3ba85f2..a1acf51 100644
--- a/include/C2VDAAdaptor.h
+++ b/include/C2VDAAdaptor.h
@@ -34,7 +34,7 @@
     void destroy() override;
 
     static media::VideoDecodeAccelerator::SupportedProfiles GetSupportedProfiles(
-            uint32_t inputFormatFourcc);
+            InputCodec inputCodec);
 
     // Implementation of the media::VideoDecodeAccelerator::Client interface.
     void ProvidePictureBuffers(uint32_t requested_num_of_buffers,
diff --git a/include/C2VDAAdaptorProxy.h b/include/C2VDAAdaptorProxy.h
index eab6882..df2adad 100644
--- a/include/C2VDAAdaptorProxy.h
+++ b/include/C2VDAAdaptorProxy.h
@@ -58,7 +58,7 @@
     void NotifyFlushDone(::arc::mojom::VideoDecodeAccelerator::Result result);
 
     static media::VideoDecodeAccelerator::SupportedProfiles GetSupportedProfiles(
-            uint32_t inputFormatFourcc);
+            InputCodec inputCodec);
 
 private:
     void onConnectionError(const std::string& pipeName);
@@ -85,7 +85,7 @@
     VideoDecodeAcceleratorAdaptor::Client* mClient;
 
     // Task runner for mojom functions.
-    const scoped_refptr<base::SingleThreadTaskRunner> mMojoTaskRunner;
+    const scoped_refptr<::base::SingleThreadTaskRunner> mMojoTaskRunner;
 
     // |mVDAPtr| and |mBinding| should only be called on |mMojoTaskRunner| after bound.
     ::arc::mojom::VideoDecodeAcceleratorPtr mVDAPtr;
diff --git a/include/C2VDACommon.h b/include/C2VDACommon.h
index 510a96c..2807af0 100644
--- a/include/C2VDACommon.h
+++ b/include/C2VDACommon.h
@@ -15,5 +15,11 @@
     YV12 = 0x32315659,
     NV12 = 0x3231564e,
 };
+
+enum class InputCodec {
+    H264,
+    VP8,
+    VP9,
+};
 } // namespace android
 #endif  // ANDROID_C2_VDA_COMMON_H
diff --git a/include/C2VDAComponent.h b/include/C2VDAComponent.h
index 477bc1c..e8f5bc0 100644
--- a/include/C2VDAComponent.h
+++ b/include/C2VDAComponent.h
@@ -5,6 +5,7 @@
 #ifndef ANDROID_C2_VDA_COMPONENT_H
 #define ANDROID_C2_VDA_COMPONENT_H
 
+#include <C2VDACommon.h>
 #include <VideoDecodeAcceleratorAdaptor.h>
 
 #include <rect.h>
@@ -47,8 +48,22 @@
         c2_status_t status() const { return mInitStatus; }
         media::VideoCodecProfile getCodecProfile() const { return mCodecProfile; }
         C2BlockPool::local_id_t getBlockPoolId() const { return mOutputBlockPoolIds->m.values[0]; }
+        InputCodec getInputCodec() const { return mInputCodec; }
 
     private:
+        // Configurable parameter setters.
+        static C2R ProfileLevelSetter(bool mayBlock, C2P<C2StreamProfileLevelInfo::input>& info);
+
+        static C2R SizeSetter(bool mayBlock, C2P<C2StreamPictureSizeInfo::output>& videoSize);
+
+        template <typename T>
+        static C2R DefaultColorAspectsSetter(bool mayBlock, C2P<T>& def);
+
+        static C2R MergedColorAspectsSetter(bool mayBlock,
+                                            C2P<C2StreamColorAspectsInfo::output>& merged,
+                                            const C2P<C2StreamColorAspectsTuning::output>& def,
+                                            const C2P<C2StreamColorAspectsInfo::input>& coded);
+
         // The input format kind; should be C2FormatCompressed.
         std::shared_ptr<C2StreamBufferTypeSetting::input> mInputFormat;
         // The output format kind; should be C2FormatVideo.
@@ -57,8 +72,15 @@
         std::shared_ptr<C2PortMediaTypeSetting::input> mInputMediaType;
         // The MIME type of output port; should be MEDIA_MIMETYPE_VIDEO_RAW.
         std::shared_ptr<C2PortMediaTypeSetting::output> mOutputMediaType;
+        // The input codec profile and level. For now configuring this parameter is useless since
+        // the component always uses fixed codec profile to initialize accelerator. It is only used
+        // for the client to query supported profile and level values.
+        // TODO: use configured profile/level to initialize accelerator.
+        std::shared_ptr<C2StreamProfileLevelInfo::input> mProfileLevel;
         // Decoded video size for output.
         std::shared_ptr<C2StreamPictureSizeInfo::output> mSize;
+        // Maximum size of one input buffer.
+        std::shared_ptr<C2StreamMaxBufferSizeInfo::input> mMaxInputSize;
         // The suggested usage of input buffer allocator ID.
         std::shared_ptr<C2PortAllocatorsTuning::input> mInputAllocatorIds;
         // The suggested usage of output buffer allocator ID.
@@ -67,9 +89,20 @@
         std::shared_ptr<C2PortSurfaceAllocatorTuning::output> mOutputSurfaceAllocatorId;
         // Compnent uses this ID to fetch corresponding output block pool from platform.
         std::shared_ptr<C2PortBlockPoolsTuning::output> mOutputBlockPoolIds;
+        // The color aspects parsed from input bitstream. This parameter should be configured by
+        // component while decoding.
+        std::shared_ptr<C2StreamColorAspectsInfo::input> mCodedColorAspects;
+        // The default color aspects specified by requested output format. This parameter should be
+        // configured by client.
+        std::shared_ptr<C2StreamColorAspectsTuning::output> mDefaultColorAspects;
+        // The combined color aspects by |mCodedColorAspects| and |mDefaultColorAspects|, and the
+        // former has higher priority. This parameter is used for component to provide color aspects
+        // as C2Info in decoded output buffers.
+        std::shared_ptr<C2StreamColorAspectsInfo::output> mColorAspects;
 
         c2_status_t mInitStatus;
         media::VideoCodecProfile mCodecProfile;
+        InputCodec mInputCodec;
     };
 
     C2VDAComponent(C2String name, c2_node_id_t id,
@@ -181,6 +214,12 @@
                     media::Rect visibleRect);
     };
 
+    // Internal struct for the information of output buffer returned from the accelerator.
+    struct OutputBufferInfo {
+        int32_t mBitstreamId;
+        int32_t mBlockId;
+    };
+
     // These tasks should be run on the component thread |mThread|.
     void onDestroy();
     void onStart(media::VideoCodecProfile profile, ::base::WaitableEvent* done);
@@ -198,42 +237,58 @@
     void onOutputFormatChanged(std::unique_ptr<VideoFormat> format);
     void onVisibleRectChanged(const media::Rect& cropRect);
     void onOutputBufferReturned(std::shared_ptr<C2GraphicBlock> block, uint32_t poolId);
+    void onSurfaceChanged();
 
     // Send input buffer to accelerator with specified bitstream id.
     void sendInputBufferToAccelerator(const C2ConstLinearBlock& input, int32_t bitstreamId);
-    // Send output buffer to accelerator.
-    void sendOutputBufferToAccelerator(GraphicBlockInfo* info);
+    // Send output buffer to accelerator. If |passToAccelerator|, change the ownership to
+    // OWNED_BY_ACCELERATOR of this buffer.
+    void sendOutputBufferToAccelerator(GraphicBlockInfo* info, bool passToAccelerator);
     // Set crop rectangle infomation to output format.
     void setOutputFormatCrop(const media::Rect& cropRect);
     // Helper function to get the specified GraphicBlockInfo object by its id.
     GraphicBlockInfo* getGraphicBlockById(int32_t blockId);
     // Helper function to get the specified GraphicBlockInfo object by its pool id.
     GraphicBlockInfo* getGraphicBlockByPoolId(uint32_t poolId);
-    // Helper function to get the specified work in mPendingWorks by bitstream id.
+    // Helper function to find the work iterator in |mPendingWorks| by bitstream id.
+    std::deque<std::unique_ptr<C2Work>>::iterator findPendingWorkByBitstreamId(int32_t bitstreamId);
+    // Helper function to get the specified work in |mPendingWorks| by bitstream id.
     C2Work* getPendingWorkByBitstreamId(int32_t bitstreamId);
     // Try to apply the output format change.
     void tryChangeOutputFormat();
     // Allocate output buffers (graphic blocks) from block allocator.
     c2_status_t allocateBuffersFromBlockAllocator(const media::Size& size, uint32_t pixelFormat);
-    // Append allocated buffer (graphic block) to mGraphicBlocks.
+    // Append allocated buffer (graphic block) to |mGraphicBlocks|.
     void appendOutputBuffer(std::shared_ptr<C2GraphicBlock> block, uint32_t poolId);
-    // Append allocated buffer (graphic block) to mGraphicBlocks in secure mode.
+    // Append allocated buffer (graphic block) to |mGraphicBlocks| in secure mode.
     void appendSecureOutputBuffer(std::shared_ptr<C2GraphicBlock> block, uint32_t poolId);
+    // Parse coded color aspects from bitstream and configs parameter if applicable.
+    bool parseCodedColorAspects(const C2ConstLinearBlock& input);
+    // Update color aspects for current output buffer.
+    c2_status_t updateColorAspects();
+    // Dequeue |mPendingBuffersToWork| to put output buffer to corresponding work and report if
+    // finished as many as possible. If |dropIfUnavailable|, drop all pending existing frames
+    // without blocking.
+    void sendOutputBufferToWorkIfAny(bool dropIfUnavailable);
+    // Update |mUndequeuedBlockIds| FIFO by pushing |blockId|.
+    void updateUndequeuedBlockIds(int32_t blockId);
 
-    // Check for finished works in mPendingWorks. If any, make onWorkDone call to listener.
-    void reportFinishedWorkIfAny();
-    // Make onWorkDone call to listener for reporting EOS work in mPendingWorks.
+    // Check if the corresponding work is finished by |bitstreamId|. If yes, make onWorkDone call to
+    // listener and erase the work from |mPendingWorks|.
+    void reportWorkIfFinished(int32_t bitstreamId);
+    // Make onWorkDone call to listener for reporting EOS work in |mPendingWorks|.
     void reportEOSWork();
-    // Abandon all works in mPendingWorks and mAbandonedWorks.
+    // Abandon all works in |mPendingWorks| and |mAbandonedWorks|.
     void reportAbandonedWorks();
     // Make onError call to listener for reporting errors.
     void reportError(c2_status_t error);
     // Helper function to determine if the work is finished.
     bool isWorkDone(const C2Work* work) const;
 
-    // Start dequeue thread, return true on success.
+    // Start dequeue thread, return true on success. If |resetBuffersInClient|, reset the counter
+    // |mBuffersInClient| on start.
     bool startDequeueThread(const media::Size& size, uint32_t pixelFormat,
-                            std::shared_ptr<C2BlockPool> blockPool);
+                            std::shared_ptr<C2BlockPool> blockPool, bool resetBuffersInClient);
     // Stop dequeue thread.
     void stopDequeueThread();
     // The rountine task running on dequeue thread.
@@ -293,6 +348,22 @@
     // The pending output format. We need to wait until all buffers are returned back to apply the
     // format change.
     std::unique_ptr<VideoFormat> mPendingOutputFormat;
+    // The color aspects parameter for current decoded output buffers.
+    std::shared_ptr<C2StreamColorAspectsInfo::output> mCurrentColorAspects;
+    // The flag of pending color aspects change. This should be set once we have parsed color
+    // aspects from bitstream by parseCodedColorAspects(), at the same time recorded input frame
+    // index into |mPendingColorAspectsChangeFrameIndex|.
+    // When this flag is true and the corresponding frame index is not less than
+    // |mPendingColorAspectsChangeFrameIndex| for the output buffer in onOutputBufferDone(), update
+    // |mCurrentColorAspects| from component interface and reset the flag.
+    bool mPendingColorAspectsChange;
+    // The record of frame index to update color aspects. Details as above.
+    uint64_t mPendingColorAspectsChangeFrameIndex;
+    // The record of bitstream and block ID of pending output buffers returned from accelerator.
+    std::deque<OutputBufferInfo> mPendingBuffersToWork;
+    // A FIFO queue to record the block IDs which are currently undequequed for display. The size
+    // of this queue will be equal to the minimum number of undequeued buffers.
+    std::deque<int32_t> mUndequeuedBlockIds;
 
     // The indicator of whether component is in secure mode.
     bool mSecureMode;
diff --git a/tests/Android.mk b/tests/Android.mk
index 3b384b4..309fccc 100644
--- a/tests/Android.mk
+++ b/tests/Android.mk
@@ -51,7 +51,6 @@
   libcutils \
   liblog \
   libmedia \
-  libmediaextractor \
   libstagefright \
   libstagefright_codec2 \
   libstagefright_codec2_vndk \
@@ -62,6 +61,7 @@
   android.hardware.media.bufferpool@1.0 \
 
 LOCAL_C_INCLUDES += \
+  $(TOP)/device/google/cheets2/codec2/vdastore/include \
   $(TOP)/external/libchrome \
   $(TOP)/external/v4l2_codec2/include \
   $(TOP)/external/v4l2_codec2/vda \
diff --git a/tests/C2VDAComponent_test.cpp b/tests/C2VDAComponent_test.cpp
index 5e597c8..4d387d4 100644
--- a/tests/C2VDAComponent_test.cpp
+++ b/tests/C2VDAComponent_test.cpp
@@ -5,6 +5,7 @@
 //#define LOG_NDEBUG 0
 #define LOG_TAG "C2VDAComponent_test"
 
+#include <C2VDAAllocatorStore.h>
 #include <C2VDAComponent.h>
 
 #include <C2Buffer.h>
@@ -23,11 +24,11 @@
 #include <gtest/gtest.h>
 #include <media/DataSource.h>
 #include <media/IMediaHTTPService.h>
-#include <media/MediaExtractor.h>
 #include <media/MediaSource.h>
 #include <media/stagefright/DataSourceFactory.h>
 #include <media/stagefright/MediaDefs.h>
 #include <media/stagefright/MediaErrors.h>
+#include <media/stagefright/MediaExtractor.h>
 #include <media/stagefright/MediaExtractorFactory.h>
 #include <media/stagefright/MetaData.h>
 #include <media/stagefright/Utils.h>
@@ -134,12 +135,6 @@
           : C2Buffer({block->share(block->offset(), block->size(), C2Fence())}) {}
 };
 
-class C2VDADummyLinearBuffer : public C2Buffer {
-public:
-    explicit C2VDADummyLinearBuffer(const std::shared_ptr<C2LinearBlock>& block)
-          : C2Buffer({block->share(0, 0, C2Fence())}) {}
-};
-
 class Listener;
 
 class C2VDAComponentTest : public ::testing::Test {
@@ -165,10 +160,6 @@
 
     std::shared_ptr<Listener> mListener;
 
-    // Allocators
-    std::shared_ptr<C2Allocator> mLinearAlloc;
-    std::shared_ptr<C2BlockPool> mLinearBlockPool;
-
     // The array of output video frame counters which will be counted in listenerThread. The array
     // length equals to iteration time of stream play.
     std::vector<int> mOutputFrameCounts;
@@ -221,12 +212,7 @@
     C2VDAComponentTest* const mThis;
 };
 
-C2VDAComponentTest::C2VDAComponentTest() : mListener(new Listener(this)) {
-    std::shared_ptr<C2AllocatorStore> store = GetCodec2PlatformAllocatorStore();
-    CHECK_EQ(store->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &mLinearAlloc), C2_OK);
-
-    mLinearBlockPool = std::make_shared<C2BasicLinearBlockPool>(mLinearAlloc);
-}
+C2VDAComponentTest::C2VDAComponentTest() : mListener(new Listener(this)) {}
 
 void C2VDAComponentTest::onWorkDone(std::weak_ptr<C2Component> component,
                                     std::list<std::unique_ptr<C2Work>> workItems) {
@@ -436,6 +422,40 @@
     std::shared_ptr<C2Component> component(std::make_shared<C2VDAComponent>(
             mTestVideoFile->mComponentName, 0, std::make_shared<C2ReflectorHelper>()));
 
+    // Get input allocator & block pool.
+    std::shared_ptr<C2AllocatorStore> store = GetCodec2PlatformAllocatorStore();
+    std::shared_ptr<C2Allocator> inputAllocator;
+    std::shared_ptr<C2BlockPool> inputBlockPool;
+
+    CHECK_EQ(store->fetchAllocator(C2AllocatorStore::DEFAULT_LINEAR, &inputAllocator), C2_OK);
+    inputBlockPool = std::make_shared<C2BasicLinearBlockPool>(inputAllocator);
+
+    // Setup output block pool (bufferpool-backed).
+    std::vector<std::unique_ptr<C2Param>> params;
+    ASSERT_EQ(component->intf()->query_vb({}, {C2PortAllocatorsTuning::output::PARAM_TYPE},
+                                          C2_DONT_BLOCK, &params),
+              C2_OK);
+    ASSERT_EQ(params.size(), 1u);
+    C2PortAllocatorsTuning::output* outputAllocators =
+            C2PortAllocatorsTuning::output::From(params[0].get());
+    C2Allocator::id_t outputAllocatorId = outputAllocators->m.values[0];
+    ALOGV("output allocator ID = %u", outputAllocatorId);
+
+    // Check bufferpool-backed block pool is used.
+    ASSERT_EQ(outputAllocatorId, C2VDAAllocatorStore::V4L2_BUFFERPOOL);
+
+    std::shared_ptr<C2BlockPool> outputBlockPool;
+    ASSERT_EQ(CreateCodec2BlockPool(outputAllocatorId, component, &outputBlockPool), C2_OK);
+    C2BlockPool::local_id_t outputPoolId = outputBlockPool->getLocalId();
+    ALOGV("output block pool ID = %" PRIu64 "", outputPoolId);
+
+    std::unique_ptr<C2PortBlockPoolsTuning::output> poolIdsTuning =
+            C2PortBlockPoolsTuning::output::AllocUnique({outputPoolId});
+
+    std::vector<std::unique_ptr<C2SettingResult>> failures;
+    ASSERT_EQ(component->intf()->config_vb({poolIdsTuning.get()}, C2_MAY_BLOCK, &failures), C2_OK);
+
+    // Set listener and start.
     ASSERT_EQ(component->setListener_vb(mListener, C2_DONT_BLOCK), C2_OK);
     ASSERT_EQ(component->start(), C2_OK);
 
@@ -472,8 +492,11 @@
                   work->worklets.front()->output.flags,
                   work->worklets.front()->output.buffers.size());
 
+            // Don't check output buffer and flags for flushed works.
+            bool flushed = work->result == C2_NOT_FOUND;
+
             ASSERT_EQ(work->worklets.size(), 1u);
-            if (work->worklets.front()->output.buffers.size() == 1u) {
+            if (!flushed && work->worklets.front()->output.buffers.size() == 1u) {
                 std::shared_ptr<C2Buffer> output = work->worklets.front()->output.buffers[0];
                 C2ConstGraphicBlock graphicBlock = output->data().graphicBlocks().front();
 
@@ -514,8 +537,8 @@
                 mOutputFrameCounts[iteration]++;
             }
 
-            bool iteration_end =
-                    work->worklets.front()->output.flags & C2FrameData::FLAG_END_OF_STREAM;
+            bool iteration_end = !flushed && (work->worklets.front()->output.flags &
+                                              C2FrameData::FLAG_END_OF_STREAM);
 
             // input buffer should be reset in component side.
             ASSERT_EQ(work->input.buffers.size(), 1u);
@@ -574,12 +597,14 @@
             int64_t timestamp = 0u;
             MediaBufferBase* buffer = nullptr;
             sp<ABuffer> csd;
+            C2FrameData::flags_t inputFlag = static_cast<C2FrameData::flags_t>(0);
             bool queueDummyEOSWork = false;
             if (!csds.empty()) {
                 csd = std::move(csds.front());
                 csds.pop_front();
                 size = csd->size();
                 data = csd->data();
+                inputFlag = C2FrameData::FLAG_CODEC_CONFIG;
             } else {
                 if (mTestVideoFile->mData->read(&buffer) != OK) {
                     ASSERT_TRUE(buffer == nullptr);
@@ -613,25 +638,22 @@
                 }
             }
 
+            work->input.flags = inputFlag;
             work->input.ordinal.frameIndex = static_cast<uint64_t>(numWorks);
             work->input.buffers.clear();
 
             std::shared_ptr<C2LinearBlock> block;
             if (queueDummyEOSWork) {
-                work->input.flags = C2FrameData::FLAG_END_OF_STREAM;
+                // Create the dummy EOS work with no input buffer inside.
+                work->input.flags = static_cast<C2FrameData::flags_t>(
+                        work->input.flags | C2FrameData::FLAG_END_OF_STREAM);
                 work->input.ordinal.timestamp = 0;  // timestamp is invalid for dummy EOS work
-
-                // Create a dummy input buffer by allocating minimal size of buffer from block pool.
-                mLinearBlockPool->fetchLinearBlock(
-                        1, {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}, &block);
-                work->input.buffers.emplace_back(new C2VDADummyLinearBuffer(std::move(block)));
                 ALOGV("Input: (Dummy EOS) id: %llu", work->input.ordinal.frameIndex.peekull());
             } else {
-                work->input.flags = static_cast<C2FrameData::flags_t>(0);
                 work->input.ordinal.timestamp = static_cast<uint64_t>(timestamp);
 
                 // Allocate an input buffer with data size.
-                mLinearBlockPool->fetchLinearBlock(
+                inputBlockPool->fetchLinearBlock(
                         size, {C2MemoryUsage::CPU_READ, C2MemoryUsage::CPU_WRITE}, &block);
                 C2WriteView view = block->map().get();
                 ASSERT_EQ(view.error(), C2_OK);
diff --git a/tests/data/bear-vp8.webm.md5 b/tests/data/bear-vp8.webm.md5
index 25d983c..ca3c179 100644
--- a/tests/data/bear-vp8.webm.md5
+++ b/tests/data/bear-vp8.webm.md5
@@ -2,4 +2,4 @@
 # ARM - Mali
 056a2484b34bc78637b37b36481027c6
 # Intel
-fdc9d348b06a77e65a8aa0ccc120c6f9
+0f6e37e68359ba1eef1cb998a084db01
diff --git a/tests/data/bear-vp9.webm.md5 b/tests/data/bear-vp9.webm.md5
index 99810d5..cf5ff81 100644
--- a/tests/data/bear-vp9.webm.md5
+++ b/tests/data/bear-vp9.webm.md5
@@ -2,4 +2,4 @@
 # ARM - Mali
 7228c16473724e4dff2fc55edcf94683
 # Intel
-058213ed7a7e119838564001b7ee8004
+971d6cdd8d75480749b1c78f5ac9dbd8
diff --git a/vda/Android.bp b/vda/Android.bp
index adba0ef..ffb38d8 100644
--- a/vda/Android.bp
+++ b/vda/Android.bp
@@ -13,7 +13,6 @@
         "ranges.cc",
         "shared_memory_region.cc",
         "v4l2_device.cc",
-        "v4l2_slice_video_decode_accelerator.cc",
         "v4l2_video_decode_accelerator.cc",
         "video_codecs.cc",
         "video_decode_accelerator.cc",
diff --git a/vda/v4l2_controls_custom.h b/vda/v4l2_controls_custom.h
deleted file mode 100644
index c34e417..0000000
--- a/vda/v4l2_controls_custom.h
+++ /dev/null
@@ -1,411 +0,0 @@
-// Copyright 2018 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Note:
-// This header file has extracted some new structs and definitions in videodev2.h from ChromeOS
-// which is not upstreamed in Linux mainline. This should be removed once they are upstreamed.
-// TODO(johnylin): remove this file once it is upstreamed.
-
-#ifndef V4L2_CONTROLS_CUSTOM_H_
-#define V4L2_CONTROLS_CUSTOM_H_
-
-#include <linux/v4l2-controls.h>
-
-#define V4L2_CID_MPEG_VIDEO_H264_SPS (V4L2_CID_MPEG_BASE+383)
-#define V4L2_CID_MPEG_VIDEO_H264_PPS (V4L2_CID_MPEG_BASE+384)
-#define V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX (V4L2_CID_MPEG_BASE+385)
-#define V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM (V4L2_CID_MPEG_BASE+386)
-#define V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM (V4L2_CID_MPEG_BASE+387)
-#define V4L2_CID_MPEG_VIDEO_H264_SPS_PPS_BEFORE_IDR (V4L2_CID_MPEG_BASE+388)
-
-#define V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR (V4L2_CID_MPEG_BASE+512)
-
-#define V4L2_CID_MPEG_VIDEO_VP9_FRAME_HDR (V4L2_CID_MPEG_BASE+513)
-#define V4L2_CID_MPEG_VIDEO_VP9_DECODE_PARAM (V4L2_CID_MPEG_BASE+514)
-#define V4L2_CID_MPEG_VIDEO_VP9_ENTROPY (V4L2_CID_MPEG_BASE+515)
-
-/* Complex controls */
-
-#define V4L2_H264_SPS_CONSTRAINT_SET0_FLAG 0x01
-#define V4L2_H264_SPS_CONSTRAINT_SET1_FLAG 0x02
-#define V4L2_H264_SPS_CONSTRAINT_SET2_FLAG 0x04
-#define V4L2_H264_SPS_CONSTRAINT_SET3_FLAG 0x08
-#define V4L2_H264_SPS_CONSTRAINT_SET4_FLAG 0x10
-#define V4L2_H264_SPS_CONSTRAINT_SET5_FLAG 0x20
-
-#define V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE 0x01
-#define V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS 0x02
-#define V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO 0x04
-#define V4L2_H264_SPS_FLAG_GAPS_IN_FRAME_NUM_VALUE_ALLOWED 0x08
-#define V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY 0x10
-#define V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD 0x20
-#define V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE 0x40
-struct v4l2_ctrl_h264_sps {
-  __u8 profile_idc;
-  __u8 constraint_set_flags;
-  __u8 level_idc;
-  __u8 seq_parameter_set_id;
-  __u8 chroma_format_idc;
-  __u8 bit_depth_luma_minus8;
-  __u8 bit_depth_chroma_minus8;
-  __u8 log2_max_frame_num_minus4;
-  __u8 pic_order_cnt_type;
-  __u8 log2_max_pic_order_cnt_lsb_minus4;
-  __s32 offset_for_non_ref_pic;
-  __s32 offset_for_top_to_bottom_field;
-  __u8 num_ref_frames_in_pic_order_cnt_cycle;
-  __s32 offset_for_ref_frame[255];
-  __u8 max_num_ref_frames;
-  __u16 pic_width_in_mbs_minus1;
-  __u16 pic_height_in_map_units_minus1;
-  __u8 flags;
-};
-
-#define V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE 0x0001
-#define V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT 0x0002
-#define V4L2_H264_PPS_FLAG_WEIGHTED_PRED 0x0004
-#define V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT 0x0008
-#define V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED 0x0010
-#define V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT 0x0020
-#define V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE 0x0040
-#define V4L2_H264_PPS_FLAG_PIC_SCALING_MATRIX_PRESENT 0x0080
-struct v4l2_ctrl_h264_pps {
-  __u8 pic_parameter_set_id;
-  __u8 seq_parameter_set_id;
-  __u8 num_slice_groups_minus1;
-  __u8 num_ref_idx_l0_default_active_minus1;
-  __u8 num_ref_idx_l1_default_active_minus1;
-  __u8 weighted_bipred_idc;
-  __s8 pic_init_qp_minus26;
-  __s8 pic_init_qs_minus26;
-  __s8 chroma_qp_index_offset;
-  __s8 second_chroma_qp_index_offset;
-  __u8 flags;
-};
-
-struct v4l2_ctrl_h264_scaling_matrix {
-  __u8 scaling_list_4x4[6][16];
-  __u8 scaling_list_8x8[6][64];
-};
-
-struct v4l2_h264_weight_factors {
-  __s8 luma_weight[32];
-  __s8 luma_offset[32];
-  __s8 chroma_weight[32][2];
-  __s8 chroma_offset[32][2];
-};
-
-struct v4l2_h264_pred_weight_table {
-  __u8 luma_log2_weight_denom;
-  __u8 chroma_log2_weight_denom;
-  struct v4l2_h264_weight_factors weight_factors[2];
-};
-
-#define V4L2_SLICE_FLAG_FIELD_PIC 0x01
-#define V4L2_SLICE_FLAG_BOTTOM_FIELD 0x02
-#define V4L2_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED 0x04
-#define V4L2_SLICE_FLAG_SP_FOR_SWITCH 0x08
-struct v4l2_ctrl_h264_slice_param {
-  /* Size in bytes, including header */
-  __u32 size;
-  /* Offset in bits to slice_data() from the beginning of this slice. */
-  __u32 header_bit_size;
-
-  __u16 first_mb_in_slice;
-  __u8 slice_type;
-  __u8 pic_parameter_set_id;
-  __u8 colour_plane_id;
-  __u16 frame_num;
-  __u16 idr_pic_id;
-  __u16 pic_order_cnt_lsb;
-  __s32 delta_pic_order_cnt_bottom;
-  __s32 delta_pic_order_cnt0;
-  __s32 delta_pic_order_cnt1;
-  __u8 redundant_pic_cnt;
-
-  struct v4l2_h264_pred_weight_table pred_weight_table;
-  /* Size in bits of dec_ref_pic_marking() syntax element. */
-  __u32 dec_ref_pic_marking_bit_size;
-  /* Size in bits of pic order count syntax. */
-  __u32 pic_order_cnt_bit_size;
-
-  __u8 cabac_init_idc;
-  __s8 slice_qp_delta;
-  __s8 slice_qs_delta;
-  __u8 disable_deblocking_filter_idc;
-  __s8 slice_alpha_c0_offset_div2;
-  __s8 slice_beta_offset_div2;
-  __u32 slice_group_change_cycle;
-
-  __u8 num_ref_idx_l0_active_minus1;
-  __u8 num_ref_idx_l1_active_minus1;
-  /*  Entries on each list are indices
-   *  into v4l2_ctrl_h264_decode_param.dpb[]. */
-  __u8 ref_pic_list0[32];
-  __u8 ref_pic_list1[32];
-
-  __u8 flags;
-};
-
-/* If not set, this entry is unused for reference. */
-#define V4L2_H264_DPB_ENTRY_FLAG_ACTIVE 0x01
-#define V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM 0x02
-struct v4l2_h264_dpb_entry {
-  __u32 buf_index; /* v4l2_buffer index */
-  __u16 frame_num;
-  __u16 pic_num;
-  /* Note that field is indicated by v4l2_buffer.field */
-  __s32 top_field_order_cnt;
-  __s32 bottom_field_order_cnt;
-  __u8 flags; /* V4L2_H264_DPB_ENTRY_FLAG_* */
-};
-
-struct v4l2_ctrl_h264_decode_param {
-  __u32 num_slices;
-  __u8 idr_pic_flag;
-  __u8 nal_ref_idc;
-  __s32 top_field_order_cnt;
-  __s32 bottom_field_order_cnt;
-  __u8 ref_pic_list_p0[32];
-  __u8 ref_pic_list_b0[32];
-  __u8 ref_pic_list_b1[32];
-  struct v4l2_h264_dpb_entry dpb[16];
-};
-
-#define V4L2_VP8_SEGMNT_HDR_FLAG_ENABLED 0x01
-#define V4L2_VP8_SEGMNT_HDR_FLAG_UPDATE_MAP 0x02
-#define V4L2_VP8_SEGMNT_HDR_FLAG_UPDATE_FEATURE_DATA 0x04
-struct v4l2_vp8_sgmnt_hdr {
-  __u8 segment_feature_mode;
-
-  __s8 quant_update[4];
-  __s8 lf_update[4];
-  __u8 segment_probs[3];
-
-  __u8 flags;
-};
-
-#define V4L2_VP8_LF_HDR_ADJ_ENABLE 0x01
-#define V4L2_VP8_LF_HDR_DELTA_UPDATE 0x02
-struct v4l2_vp8_loopfilter_hdr {
-  __u8 type;
-  __u8 level;
-  __u8 sharpness_level;
-  __s8 ref_frm_delta_magnitude[4];
-  __s8 mb_mode_delta_magnitude[4];
-
-  __u8 flags;
-};
-
-struct v4l2_vp8_quantization_hdr {
-  __u8 y_ac_qi;
-  __s8 y_dc_delta;
-  __s8 y2_dc_delta;
-  __s8 y2_ac_delta;
-  __s8 uv_dc_delta;
-  __s8 uv_ac_delta;
-  __u16 dequant_factors[4][3][2];
-};
-
-struct v4l2_vp8_entropy_hdr {
-  __u8 coeff_probs[4][8][3][11];
-  __u8 y_mode_probs[4];
-  __u8 uv_mode_probs[3];
-  __u8 mv_probs[2][19];
-};
-
-#define V4L2_VP8_FRAME_HDR_FLAG_EXPERIMENTAL 0x01
-#define V4L2_VP8_FRAME_HDR_FLAG_SHOW_FRAME 0x02
-#define V4L2_VP8_FRAME_HDR_FLAG_MB_NO_SKIP_COEFF 0x04
-struct v4l2_ctrl_vp8_frame_hdr {
-  /* 0: keyframe, 1: not a keyframe */
-  __u8 key_frame;
-  __u8 version;
-
-  /* Populated also if not a key frame */
-  __u16 width;
-  __u8 horizontal_scale;
-  __u16 height;
-  __u8 vertical_scale;
-
-  struct v4l2_vp8_sgmnt_hdr sgmnt_hdr;
-  struct v4l2_vp8_loopfilter_hdr lf_hdr;
-  struct v4l2_vp8_quantization_hdr quant_hdr;
-  struct v4l2_vp8_entropy_hdr entropy_hdr;
-
-  __u8 sign_bias_golden;
-  __u8 sign_bias_alternate;
-
-  __u8 prob_skip_false;
-  __u8 prob_intra;
-  __u8 prob_last;
-  __u8 prob_gf;
-
-  __u32 first_part_size;
-  __u32 first_part_offset;
-  /*
-   * Offset in bits of MB data in first partition,
-   * i.e. bit offset starting from first_part_offset.
-   */
-  __u32 macroblock_bit_offset;
-
-  __u8 num_dct_parts;
-  __u32 dct_part_sizes[8];
-
-  __u8 bool_dec_range;
-  __u8 bool_dec_value;
-  __u8 bool_dec_count;
-
-  /* v4l2_buffer indices of reference frames */
-  __u32 last_frame;
-  __u32 golden_frame;
-  __u32 alt_frame;
-
-  __u8 flags;
-};
-
-#define V4L2_VP9_SGMNT_PARAM_FLAG_ENABLED 0x01
-#define V4L2_VP9_SGMNT_PARAM_FLAG_UPDATE_MAP 0x02
-#define V4L2_VP9_SGMNT_PARAM_FLAG_TEMPORAL_UPDATE 0x04
-#define V4L2_VP9_SGMNT_PARAM_FLAG_UPDATE_DATA 0x08
-#define V4L2_VP9_SGMNT_PARAM_FLAG_ABS_OR_DELTA_UPDATE 0x10
-struct v4l2_vp9_segmentation_params {
-  __u8 tree_probs[7];
-  __u8 pred_probs[3];
-  __u8 feature_enabled[8][4];
-  __s16 feature_data[8][4];
-
-  __u8 flags;
-};
-
-#define V4L2_VP9_LOOP_FLTR_FLAG_DELTA_ENABLED 0x01
-#define V4L2_VP9_LOOP_FLTR_FLAG_DELTA_UPDATE 0x02
-struct v4l2_vp9_loop_filter_params {
-  __u8 level;
-  __u8 sharpness;
-  __s8 deltas[4];
-  __s8 mode_deltas[2];
-  __u8 lvl_lookup[8][4][2];
-
-  __u8 flags;
-};
-
-#define V4L2_VP9_QUANT_PARAMS_FLAG_LOSSLESS 0x01
-struct v4l2_vp9_quantization_params {
-  __u8 base_q_idx;
-  __s8 delta_q_y_dc;
-  __s8 delta_q_uv_dc;
-  __s8 delta_q_uv_ac;
-
-  __u8 flags;
-};
-
-#define V4L2_VP9_FRAME_HDR_FLAG_SHOW_FRAME 0x01
-/* Error resilient mode enabled flag */
-#define V4L2_VP9_FRAME_HDR_FLAG_ERR_RES	0x02
-#define V4L2_VP9_FRAME_HDR_FLAG_FRAME_INTRA 0x04
-#define V4L2_VP9_FRAME_HDR_ALLOW_HIGH_PREC_MV 0x08
-#define V4L2_VP9_FRAME_HDR_REFRESH_FRAME_CTX 0x10
-#define V4L2_VP9_FRAME_HDR_PARALLEL_DEC_MODE 0x20
-struct v4l2_ctrl_vp9_frame_hdr {
-  __u8 profile;
-  /* 0: keyframe, 1: non-keyframe */
-  __u8 frame_type;
-
-  __u8 bit_depth;
-  __u8 color_space;
-  __u8 color_range;
-  __u8 subsampling_x;
-  __u8 subsampling_y;
-
-  __u32 frame_width;
-  __u32 frame_height;
-  __u32 render_width;
-  __u32 render_height;
-
-  __u8 reset_frame_context;
-
-  __u8 interpolation_filter;
-  __u8 frame_context_idx;
-
-  struct v4l2_vp9_loop_filter_params lf_params;
-  struct v4l2_vp9_quantization_params quant_params;
-  struct v4l2_vp9_segmentation_params sgmnt_params;
-
-  __u8 tile_cols_log2;
-  __u8 tile_rows_log2;
-
-  __u16 header_size_in_bytes;
-
-  __u8 flags;
-};
-
-struct v4l2_vp9_reference_frame {
-  /* v4l2_buffer index */
-  __u32 buf_index;
-
-  __u32 frame_width;
-  __u32 frame_height;
-  __u8 bit_depth;
-  __u8 subsampling_x;
-  __u8 subsampling_y;
-};
-
-struct v4l2_ctrl_vp9_decode_param {
-  /* v4l2_buffer index for all reference frames (frame slots). */
-  __u32 ref_frames[8];
-
-  /* Active reference frames, [0]: last, [1]: golden, [2]: altref */
-  struct v4l2_vp9_reference_frame active_ref_frames[3];
-};
-
-struct v4l2_vp9_entropy_ctx {
-  __u8 tx_probs_8x8[2][1];
-  __u8 tx_probs_16x16[2][2];
-  __u8 tx_probs_32x32[2][3];
-
-  __u8 coef_probs[4][2][2][6][6][3];
-  __u8 skip_prob[3];
-  __u8 inter_mode_probs[7][3];
-  __u8 interp_filter_probs[4][2];
-  __u8 is_inter_prob[4];
-
-  __u8 comp_mode_prob[5];
-  __u8 single_ref_prob[5][2];
-  __u8 comp_ref_prob[5];
-
-  __u8 y_mode_probs[4][9];
-  __u8 uv_mode_probs[10][9];
-
-  __u8 partition_probs[16][3];
-
-  __u8 mv_joint_probs[3];
-  __u8 mv_sign_prob[2];
-  __u8 mv_class_probs[2][10];
-  __u8 mv_class0_bit_prob[2];
-  __u8 mv_bits_prob[2][10];
-  __u8 mv_class0_fr_probs[2][2][3];
-  __u8 mv_fr_probs[2][3];
-  __u8 mv_class0_hp_prob[2];
-  __u8 mv_hp_prob[2];
-};
-
-/* Entropy context state for current frame (frame_context_idx). */
-struct v4l2_ctrl_vp9_entropy {
-  __u8 tx_mode;
-  __u8 reference_mode;
-
-  /* Entropy context after load_probs2(). */
-  struct v4l2_vp9_entropy_ctx initial_entropy_ctx;
-
-  /*
-   * Entropy context for the current decoding state: when passed to the
-   * driver, contains the state of initial_entropy_ctx after parsing the
-   * compressed header. After decoding is done (after vb2_buffer_done() is
-   * called on the associated buffer), state as after refresh_probs().
-   */
-  struct v4l2_vp9_entropy_ctx current_entropy_ctx;
-};
-
-#endif  // V4L2_CONTROLS_CUSTOM_H_
diff --git a/vda/v4l2_device.cc b/vda/v4l2_device.cc
index 78ef474..956b2a7 100644
--- a/vda/v4l2_device.cc
+++ b/vda/v4l2_device.cc
@@ -80,22 +80,12 @@
 }
 
 // static
-uint32_t V4L2Device::VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile,
-                                                   bool slice_based) {
+uint32_t V4L2Device::VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile) {
   if (profile >= H264PROFILE_MIN && profile <= H264PROFILE_MAX) {
-    if (slice_based)
-      return V4L2_PIX_FMT_H264_SLICE;
-    else
       return V4L2_PIX_FMT_H264;
   } else if (profile >= VP8PROFILE_MIN && profile <= VP8PROFILE_MAX) {
-    if (slice_based)
-      return V4L2_PIX_FMT_VP8_FRAME;
-    else
       return V4L2_PIX_FMT_VP8;
   } else if (profile >= VP9PROFILE_MIN && profile <= VP9PROFILE_MAX) {
-    if (slice_based)
-      return V4L2_PIX_FMT_VP9_FRAME;
-    else
       return V4L2_PIX_FMT_VP9;
   } else {
     LOG(FATAL) << "Add more cases as needed";
@@ -112,7 +102,6 @@
 
   switch (pix_fmt) {
     case V4L2_PIX_FMT_H264:
-    case V4L2_PIX_FMT_H264_SLICE:
       if (is_encoder) {
         // TODO(posciak): need to query the device for supported H.264 profiles,
         // for now choose Main as a sensible default.
@@ -125,13 +114,11 @@
       break;
 
     case V4L2_PIX_FMT_VP8:
-    case V4L2_PIX_FMT_VP8_FRAME:
       min_profile = VP8PROFILE_MIN;
       max_profile = VP8PROFILE_MAX;
       break;
 
     case V4L2_PIX_FMT_VP9:
-    case V4L2_PIX_FMT_VP9_FRAME:
       min_profile = VP9PROFILE_MIN;
       max_profile = VP9PROFILE_MAX;
       break;
diff --git a/vda/v4l2_device.h b/vda/v4l2_device.h
index e54a1e7..a4fa15b 100644
--- a/vda/v4l2_device.h
+++ b/vda/v4l2_device.h
@@ -11,6 +11,7 @@
 #ifndef V4L2_DEVICE_H_
 #define V4L2_DEVICE_H_
 
+#include <linux/videodev2.h>
 #include <map>
 #include <stddef.h>
 #include <stdint.h>
@@ -21,7 +22,6 @@
 #include "video_codecs.h"
 #include "video_decode_accelerator.h"
 #include "video_pixel_format.h"
-#include "videodev2_custom.h"
 
 // TODO(posciak): remove this once V4L2 headers are updated.
 #define V4L2_PIX_FMT_MT21 v4l2_fourcc('M', 'T', '2', '1')
@@ -40,8 +40,7 @@
   // Utility format conversion functions
   static VideoPixelFormat V4L2PixFmtToVideoPixelFormat(uint32_t format);
   static uint32_t VideoPixelFormatToV4L2PixFmt(VideoPixelFormat format);
-  static uint32_t VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile,
-                                                bool slice_based);
+  static uint32_t VideoCodecProfileToV4L2PixFmt(VideoCodecProfile profile);
   std::vector<VideoCodecProfile> V4L2PixFmtToVideoCodecProfiles(
       uint32_t pix_fmt,
       bool is_encoder);
diff --git a/vda/v4l2_slice_video_decode_accelerator.cc b/vda/v4l2_slice_video_decode_accelerator.cc
deleted file mode 100644
index 4e82dbd..0000000
--- a/vda/v4l2_slice_video_decode_accelerator.cc
+++ /dev/null
@@ -1,3099 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-// Note: ported from Chromium commit head: 85fdf90
-
-#include "v4l2_slice_video_decode_accelerator.h"
-
-#include <errno.h>
-#include <fcntl.h>
-#include <poll.h>
-#include <string.h>
-#include <sys/eventfd.h>
-#include <sys/ioctl.h>
-#include <sys/mman.h>
-
-#include <memory>
-
-#include "base/bind.h"
-#include "base/bind_helpers.h"
-#include "base/callback.h"
-#include "base/callback_helpers.h"
-#include "base/command_line.h"
-#include "base/macros.h"
-#include "base/memory/ptr_util.h"
-#include "base/numerics/safe_conversions.h"
-#include "base/single_thread_task_runner.h"
-#include "base/strings/stringprintf.h"
-#include "base/threading/thread_task_runner_handle.h"
-#include "shared_memory_region.h"
-
-#define DVLOGF(level) DVLOG(level) << __func__ << "(): "
-#define VLOGF(level) VLOG(level) << __func__ << "(): "
-#define VPLOGF(level) VPLOG(level) << __func__ << "(): "
-
-#define NOTIFY_ERROR(x)                       \
-  do {                                        \
-    VLOGF(1) << "Setting error state: " << x; \
-    SetErrorState(x);                         \
-  } while (0)
-
-#define IOCTL_OR_ERROR_RETURN_VALUE(type, arg, value, type_str) \
-  do {                                                          \
-    if (device_->Ioctl(type, arg) != 0) {                       \
-      VPLOGF(1) << "ioctl() failed: " << type_str;              \
-      return value;                                             \
-    }                                                           \
-  } while (0)
-
-#define IOCTL_OR_ERROR_RETURN(type, arg) \
-  IOCTL_OR_ERROR_RETURN_VALUE(type, arg, ((void)0), #type)
-
-#define IOCTL_OR_ERROR_RETURN_FALSE(type, arg) \
-  IOCTL_OR_ERROR_RETURN_VALUE(type, arg, false, #type)
-
-#define IOCTL_OR_LOG_ERROR(type, arg)           \
-  do {                                          \
-    if (device_->Ioctl(type, arg) != 0)         \
-      VPLOGF(1) << "ioctl() failed: " << #type; \
-  } while (0)
-
-namespace media {
-
-// static
-const uint32_t V4L2SliceVideoDecodeAccelerator::supported_input_fourccs_[] = {
-    V4L2_PIX_FMT_H264_SLICE, V4L2_PIX_FMT_VP8_FRAME, V4L2_PIX_FMT_VP9_FRAME,
-};
-
-class V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface
-    : public base::RefCounted<V4L2DecodeSurface> {
- public:
-  using ReleaseCB = base::Callback<void(int)>;
-
-  V4L2DecodeSurface(int32_t bitstream_id,
-                    int input_record,
-                    int output_record,
-                    const ReleaseCB& release_cb);
-
-  // Mark the surface as decoded. This will also release all references, as
-  // they are not needed anymore and execute the done callback, if not null.
-  void SetDecoded();
-  bool decoded() const { return decoded_; }
-
-  int32_t bitstream_id() const { return bitstream_id_; }
-  int input_record() const { return input_record_; }
-  int output_record() const { return output_record_; }
-  uint32_t config_store() const { return config_store_; }
-  Rect visible_rect() const { return visible_rect_; }
-
-  void set_visible_rect(const Rect& visible_rect) {
-    visible_rect_ = visible_rect;
-  }
-
-  // Take references to each reference surface and keep them until the
-  // target surface is decoded.
-  void SetReferenceSurfaces(
-      const std::vector<scoped_refptr<V4L2DecodeSurface>>& ref_surfaces);
-
-  // If provided via this method, |done_cb| callback will be executed after
-  // decoding into this surface is finished. The callback is reset afterwards,
-  // so it needs to be set again before each decode operation.
-  void SetDecodeDoneCallback(const base::Closure& done_cb) {
-    DCHECK(done_cb_.is_null());
-    done_cb_ = done_cb;
-  }
-
-  std::string ToString() const;
-
- private:
-  friend class base::RefCounted<V4L2DecodeSurface>;
-  ~V4L2DecodeSurface();
-
-  int32_t bitstream_id_;
-  int input_record_;
-  int output_record_;
-  uint32_t config_store_;
-  Rect visible_rect_;
-
-  bool decoded_;
-  ReleaseCB release_cb_;
-  base::Closure done_cb_;
-
-  std::vector<scoped_refptr<V4L2DecodeSurface>> reference_surfaces_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2DecodeSurface);
-};
-
-V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::V4L2DecodeSurface(
-    int32_t bitstream_id,
-    int input_record,
-    int output_record,
-    const ReleaseCB& release_cb)
-    : bitstream_id_(bitstream_id),
-      input_record_(input_record),
-      output_record_(output_record),
-      config_store_(input_record + 1),
-      decoded_(false),
-      release_cb_(release_cb) {}
-
-V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::~V4L2DecodeSurface() {
-  DVLOGF(5) << "Releasing output record id=" << output_record_;
-  release_cb_.Run(output_record_);
-}
-
-void V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::SetReferenceSurfaces(
-    const std::vector<scoped_refptr<V4L2DecodeSurface>>& ref_surfaces) {
-  DCHECK(reference_surfaces_.empty());
-  reference_surfaces_ = ref_surfaces;
-}
-
-void V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::SetDecoded() {
-  DCHECK(!decoded_);
-  decoded_ = true;
-
-  // We can now drop references to all reference surfaces for this surface
-  // as we are done with decoding.
-  reference_surfaces_.clear();
-
-  // And finally execute and drop the decode done callback, if set.
-  if (!done_cb_.is_null())
-    base::ResetAndReturn(&done_cb_).Run();
-}
-
-std::string V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface::ToString()
-    const {
-  std::string out;
-  base::StringAppendF(&out, "Buffer %d -> %d. ", input_record_, output_record_);
-  base::StringAppendF(&out, "Reference surfaces:");
-  for (const auto& ref : reference_surfaces_) {
-    DCHECK_NE(ref->output_record(), output_record_);
-    base::StringAppendF(&out, " %d", ref->output_record());
-  }
-  return out;
-}
-
-V4L2SliceVideoDecodeAccelerator::InputRecord::InputRecord()
-    : input_id(-1),
-      address(nullptr),
-      length(0),
-      bytes_used(0),
-      at_device(false) {}
-
-V4L2SliceVideoDecodeAccelerator::OutputRecord::OutputRecord()
-    : at_device(false),
-      at_client(false),
-      picture_id(-1),
-      cleared(false) {}
-
-struct V4L2SliceVideoDecodeAccelerator::BitstreamBufferRef {
-  BitstreamBufferRef(
-      base::WeakPtr<VideoDecodeAccelerator::Client>& client,
-      const scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner,
-      SharedMemoryRegion* shm,
-      int32_t input_id);
-  ~BitstreamBufferRef();
-  const base::WeakPtr<VideoDecodeAccelerator::Client> client;
-  const scoped_refptr<base::SingleThreadTaskRunner> client_task_runner;
-  const std::unique_ptr<SharedMemoryRegion> shm;
-  off_t bytes_used;
-  const int32_t input_id;
-};
-
-V4L2SliceVideoDecodeAccelerator::BitstreamBufferRef::BitstreamBufferRef(
-    base::WeakPtr<VideoDecodeAccelerator::Client>& client,
-    const scoped_refptr<base::SingleThreadTaskRunner>& client_task_runner,
-    SharedMemoryRegion* shm,
-    int32_t input_id)
-    : client(client),
-      client_task_runner(client_task_runner),
-      shm(shm),
-      bytes_used(0),
-      input_id(input_id) {}
-
-V4L2SliceVideoDecodeAccelerator::BitstreamBufferRef::~BitstreamBufferRef() {
-  if (input_id >= 0) {
-    DVLOGF(5) << "returning input_id: " << input_id;
-    client_task_runner->PostTask(
-        FROM_HERE,
-        base::Bind(&VideoDecodeAccelerator::Client::NotifyEndOfBitstreamBuffer,
-                   client, input_id));
-  }
-}
-
-V4L2SliceVideoDecodeAccelerator::PictureRecord::PictureRecord(
-    bool cleared,
-    const Picture& picture)
-    : cleared(cleared), picture(picture) {}
-
-V4L2SliceVideoDecodeAccelerator::PictureRecord::~PictureRecord() {}
-
-class V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator
-    : public H264Decoder::H264Accelerator {
- public:
-  explicit V4L2H264Accelerator(V4L2SliceVideoDecodeAccelerator* v4l2_dec);
-  ~V4L2H264Accelerator() override;
-
-  // H264Decoder::H264Accelerator implementation.
-  scoped_refptr<H264Picture> CreateH264Picture() override;
-
-  bool SubmitFrameMetadata(const H264SPS* sps,
-                           const H264PPS* pps,
-                           const H264DPB& dpb,
-                           const H264Picture::Vector& ref_pic_listp0,
-                           const H264Picture::Vector& ref_pic_listb0,
-                           const H264Picture::Vector& ref_pic_listb1,
-                           const scoped_refptr<H264Picture>& pic) override;
-
-  bool SubmitSlice(const H264PPS* pps,
-                   const H264SliceHeader* slice_hdr,
-                   const H264Picture::Vector& ref_pic_list0,
-                   const H264Picture::Vector& ref_pic_list1,
-                   const scoped_refptr<H264Picture>& pic,
-                   const uint8_t* data,
-                   size_t size) override;
-
-  bool SubmitDecode(const scoped_refptr<H264Picture>& pic) override;
-  bool OutputPicture(const scoped_refptr<H264Picture>& pic) override;
-
-  void Reset() override;
-
- private:
-  // Max size of reference list.
-  static const size_t kDPBIndicesListSize = 32;
-  void H264PictureListToDPBIndicesList(const H264Picture::Vector& src_pic_list,
-                                       uint8_t dst_list[kDPBIndicesListSize]);
-
-  void H264DPBToV4L2DPB(
-      const H264DPB& dpb,
-      std::vector<scoped_refptr<V4L2DecodeSurface>>* ref_surfaces);
-
-  scoped_refptr<V4L2DecodeSurface> H264PictureToV4L2DecodeSurface(
-      const scoped_refptr<H264Picture>& pic);
-
-  size_t num_slices_;
-  V4L2SliceVideoDecodeAccelerator* v4l2_dec_;
-
-  // TODO(posciak): This should be queried from hardware once supported.
-  static const size_t kMaxSlices = 16;
-  struct v4l2_ctrl_h264_slice_param v4l2_slice_params_[kMaxSlices];
-  struct v4l2_ctrl_h264_decode_param v4l2_decode_param_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2H264Accelerator);
-};
-
-class V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator
-    : public VP8Decoder::VP8Accelerator {
- public:
-  explicit V4L2VP8Accelerator(V4L2SliceVideoDecodeAccelerator* v4l2_dec);
-  ~V4L2VP8Accelerator() override;
-
-  // VP8Decoder::VP8Accelerator implementation.
-  scoped_refptr<VP8Picture> CreateVP8Picture() override;
-
-  bool SubmitDecode(const scoped_refptr<VP8Picture>& pic,
-                    const Vp8FrameHeader* frame_hdr,
-                    const scoped_refptr<VP8Picture>& last_frame,
-                    const scoped_refptr<VP8Picture>& golden_frame,
-                    const scoped_refptr<VP8Picture>& alt_frame) override;
-
-  bool OutputPicture(const scoped_refptr<VP8Picture>& pic) override;
-
- private:
-  scoped_refptr<V4L2DecodeSurface> VP8PictureToV4L2DecodeSurface(
-      const scoped_refptr<VP8Picture>& pic);
-
-  V4L2SliceVideoDecodeAccelerator* v4l2_dec_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2VP8Accelerator);
-};
-
-class V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator
-    : public VP9Decoder::VP9Accelerator {
- public:
-  explicit V4L2VP9Accelerator(V4L2SliceVideoDecodeAccelerator* v4l2_dec);
-  ~V4L2VP9Accelerator() override;
-
-  // VP9Decoder::VP9Accelerator implementation.
-  scoped_refptr<VP9Picture> CreateVP9Picture() override;
-
-  bool SubmitDecode(const scoped_refptr<VP9Picture>& pic,
-                    const Vp9SegmentationParams& segm_params,
-                    const Vp9LoopFilterParams& lf_params,
-                    const std::vector<scoped_refptr<VP9Picture>>& ref_pictures,
-                    const base::Closure& done_cb) override;
-
-  bool OutputPicture(const scoped_refptr<VP9Picture>& pic) override;
-
-  bool GetFrameContext(const scoped_refptr<VP9Picture>& pic,
-                       Vp9FrameContext* frame_ctx) override;
-
-  bool IsFrameContextRequired() const override {
-    return device_needs_frame_context_;
-  }
-
- private:
-  scoped_refptr<V4L2DecodeSurface> VP9PictureToV4L2DecodeSurface(
-      const scoped_refptr<VP9Picture>& pic);
-
-  bool device_needs_frame_context_;
-
-  V4L2SliceVideoDecodeAccelerator* v4l2_dec_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2VP9Accelerator);
-};
-
-// Codec-specific subclasses of software decoder picture classes.
-// This allows us to keep decoders oblivious of our implementation details.
-class V4L2H264Picture : public H264Picture {
- public:
-  explicit V4L2H264Picture(
-      const scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>&
-          dec_surface);
-
-  V4L2H264Picture* AsV4L2H264Picture() override { return this; }
-  scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-  dec_surface() {
-    return dec_surface_;
-  }
-
- private:
-  ~V4L2H264Picture() override;
-
-  scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-      dec_surface_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2H264Picture);
-};
-
-V4L2H264Picture::V4L2H264Picture(
-    const scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>&
-        dec_surface)
-    : dec_surface_(dec_surface) {}
-
-V4L2H264Picture::~V4L2H264Picture() {}
-
-class V4L2VP8Picture : public VP8Picture {
- public:
-  explicit V4L2VP8Picture(
-      const scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>&
-          dec_surface);
-
-  V4L2VP8Picture* AsV4L2VP8Picture() override { return this; }
-  scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-  dec_surface() {
-    return dec_surface_;
-  }
-
- private:
-  ~V4L2VP8Picture() override;
-
-  scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-      dec_surface_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2VP8Picture);
-};
-
-V4L2VP8Picture::V4L2VP8Picture(
-    const scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>&
-        dec_surface)
-    : dec_surface_(dec_surface) {}
-
-V4L2VP8Picture::~V4L2VP8Picture() {}
-
-class V4L2VP9Picture : public VP9Picture {
- public:
-  explicit V4L2VP9Picture(
-      const scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>&
-          dec_surface);
-
-  V4L2VP9Picture* AsV4L2VP9Picture() override { return this; }
-  scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-  dec_surface() {
-    return dec_surface_;
-  }
-
- private:
-  ~V4L2VP9Picture() override;
-
-  scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-      dec_surface_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2VP9Picture);
-};
-
-V4L2VP9Picture::V4L2VP9Picture(
-    const scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>&
-        dec_surface)
-    : dec_surface_(dec_surface) {}
-
-V4L2VP9Picture::~V4L2VP9Picture() {}
-
-V4L2SliceVideoDecodeAccelerator::V4L2SliceVideoDecodeAccelerator(
-    const scoped_refptr<V4L2Device>& device)
-    : input_planes_count_(0),
-      output_planes_count_(0),
-      child_task_runner_(base::ThreadTaskRunnerHandle::Get()),
-      device_(device),
-      decoder_thread_("V4L2SliceVideoDecodeAcceleratorThread"),
-      device_poll_thread_("V4L2SliceVideoDecodeAcceleratorDevicePollThread"),
-      input_streamon_(false),
-      input_buffer_queued_count_(0),
-      output_streamon_(false),
-      output_buffer_queued_count_(0),
-      video_profile_(VIDEO_CODEC_PROFILE_UNKNOWN),
-      input_format_fourcc_(0),
-      output_format_fourcc_(0),
-      state_(kUninitialized),
-      output_mode_(Config::OutputMode::ALLOCATE),
-      decoder_flushing_(false),
-      decoder_resetting_(false),
-      surface_set_change_pending_(false),
-      picture_clearing_count_(0),
-      weak_this_factory_(this) {
-  weak_this_ = weak_this_factory_.GetWeakPtr();
-}
-
-V4L2SliceVideoDecodeAccelerator::~V4L2SliceVideoDecodeAccelerator() {
-  DVLOGF(2);
-
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-  DCHECK(!decoder_thread_.IsRunning());
-  DCHECK(!device_poll_thread_.IsRunning());
-
-  DCHECK(input_buffer_map_.empty());
-  DCHECK(output_buffer_map_.empty());
-}
-
-void V4L2SliceVideoDecodeAccelerator::NotifyError(Error error) {
-  if (!child_task_runner_->BelongsToCurrentThread()) {
-    child_task_runner_->PostTask(
-        FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::NotifyError,
-                              weak_this_, error));
-    return;
-  }
-
-  if (client_) {
-    client_->NotifyError(error);
-    client_ptr_factory_.reset();
-  }
-}
-
-bool V4L2SliceVideoDecodeAccelerator::Initialize(const Config& config,
-                                                 Client* client) {
-  VLOGF(3) << "profile: " << config.profile;
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-  DCHECK_EQ(state_, kUninitialized);
-
-  if (config.output_mode != Config::OutputMode::ALLOCATE &&
-      config.output_mode != Config::OutputMode::IMPORT) {
-    NOTREACHED() << "Only ALLOCATE and IMPORT OutputModes are supported";
-    return false;
-  }
-
-  client_ptr_factory_.reset(
-      new base::WeakPtrFactory<VideoDecodeAccelerator::Client>(client));
-  client_ = client_ptr_factory_->GetWeakPtr();
-  // If we haven't been set up to decode on separate thread via
-  // TryToSetupDecodeOnSeparateThread(), use the main thread/client for
-  // decode tasks.
-  if (!decode_task_runner_) {
-    decode_task_runner_ = child_task_runner_;
-    DCHECK(!decode_client_);
-    decode_client_ = client_;
-  }
-
-  video_profile_ = config.profile;
-
-  // TODO(posciak): This needs to be queried once supported.
-  input_planes_count_ = 1;
-  output_planes_count_ = 1;
-
-  input_format_fourcc_ =
-      V4L2Device::VideoCodecProfileToV4L2PixFmt(video_profile_, true);
-
-  if (!device_->Open(V4L2Device::Type::kDecoder, input_format_fourcc_)) {
-    VLOGF(1) << "Failed to open device for profile: " << config.profile
-             << " fourcc: " << std::hex << "0x" << input_format_fourcc_;
-    return false;
-  }
-
-  if (video_profile_ >= H264PROFILE_MIN && video_profile_ <= H264PROFILE_MAX) {
-    h264_accelerator_.reset(new V4L2H264Accelerator(this));
-    decoder_.reset(new H264Decoder(h264_accelerator_.get()));
-  } else if (video_profile_ >= VP8PROFILE_MIN &&
-             video_profile_ <= VP8PROFILE_MAX) {
-    vp8_accelerator_.reset(new V4L2VP8Accelerator(this));
-    decoder_.reset(new VP8Decoder(vp8_accelerator_.get()));
-  } else if (video_profile_ >= VP9PROFILE_MIN &&
-             video_profile_ <= VP9PROFILE_MAX) {
-    vp9_accelerator_.reset(new V4L2VP9Accelerator(this));
-    decoder_.reset(new VP9Decoder(vp9_accelerator_.get()));
-  } else {
-    NOTREACHED() << "Unsupported profile " << video_profile_;
-    return false;
-  }
-
-  // Capabilities check.
-  struct v4l2_capability caps;
-  const __u32 kCapsRequired = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYCAP, &caps);
-  if ((caps.capabilities & kCapsRequired) != kCapsRequired) {
-    VLOGF(1) << "ioctl() failed: VIDIOC_QUERYCAP"
-             << ", caps check failed: 0x" << std::hex << caps.capabilities;
-    return false;
-  }
-
-  if (!SetupFormats())
-    return false;
-
-  if (!decoder_thread_.Start()) {
-    VLOGF(1) << "device thread failed to start";
-    return false;
-  }
-  decoder_thread_task_runner_ = decoder_thread_.task_runner();
-
-  state_ = kInitialized;
-  output_mode_ = config.output_mode;
-
-  // InitializeTask will NOTIFY_ERROR on failure.
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::InitializeTask,
-                            base::Unretained(this)));
-
-  VLOGF(2) << "V4L2SliceVideoDecodeAccelerator initialized";
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::InitializeTask() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_EQ(state_, kInitialized);
-
-  if (!CreateInputBuffers())
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-
-  // Output buffers will be created once decoder gives us information
-  // about their size and required count.
-  state_ = kDecoding;
-}
-
-void V4L2SliceVideoDecodeAccelerator::Destroy() {
-  VLOGF(2);
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-
-  if (decoder_thread_.IsRunning()) {
-    decoder_thread_task_runner_->PostTask(
-        FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DestroyTask,
-                              base::Unretained(this)));
-
-    // Wait for tasks to finish/early-exit.
-    decoder_thread_.Stop();
-  }
-
-  delete this;
-  VLOGF(2) << "Destroyed";
-}
-
-void V4L2SliceVideoDecodeAccelerator::DestroyTask() {
-  DVLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  state_ = kError;
-
-  decoder_->Reset();
-
-  decoder_current_bitstream_buffer_.reset();
-  while (!decoder_input_queue_.empty())
-    decoder_input_queue_.pop();
-
-  // Stop streaming and the device_poll_thread_.
-  StopDevicePoll(false);
-
-  DestroyInputBuffers();
-  DestroyOutputs(false);
-
-  DCHECK(surfaces_at_device_.empty());
-  DCHECK(surfaces_at_display_.empty());
-  DCHECK(decoder_display_queue_.empty());
-}
-
-static bool IsSupportedOutputFormat(uint32_t v4l2_format) {
-  // Only support V4L2_PIX_FMT_NV12 output format for now.
-  // TODO(johnylin): add more supported format if necessary.
-  uint32_t kSupportedOutputFmtFourcc[] = { V4L2_PIX_FMT_NV12 };
-  return std::find(
-      kSupportedOutputFmtFourcc,
-      kSupportedOutputFmtFourcc + arraysize(kSupportedOutputFmtFourcc),
-      v4l2_format) !=
-          kSupportedOutputFmtFourcc + arraysize(kSupportedOutputFmtFourcc);
-}
-
-bool V4L2SliceVideoDecodeAccelerator::SetupFormats() {
-  DCHECK_EQ(state_, kUninitialized);
-
-  size_t input_size;
-  Size max_resolution, min_resolution;
-  device_->GetSupportedResolution(input_format_fourcc_, &min_resolution,
-                                  &max_resolution);
-  if (max_resolution.width() > 1920 && max_resolution.height() > 1088)
-    input_size = kInputBufferMaxSizeFor4k;
-  else
-    input_size = kInputBufferMaxSizeFor1080p;
-
-  struct v4l2_fmtdesc fmtdesc;
-  memset(&fmtdesc, 0, sizeof(fmtdesc));
-  fmtdesc.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-  bool is_format_supported = false;
-  while (device_->Ioctl(VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
-    if (fmtdesc.pixelformat == input_format_fourcc_) {
-      is_format_supported = true;
-      break;
-    }
-    ++fmtdesc.index;
-  }
-
-  if (!is_format_supported) {
-    DVLOGF(1) << "Input fourcc " << input_format_fourcc_
-              << " not supported by device.";
-    return false;
-  }
-
-  struct v4l2_format format;
-  memset(&format, 0, sizeof(format));
-  format.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-  format.fmt.pix_mp.pixelformat = input_format_fourcc_;
-  format.fmt.pix_mp.plane_fmt[0].sizeimage = input_size;
-  format.fmt.pix_mp.num_planes = input_planes_count_;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format);
-
-  // We have to set up the format for output, because the driver may not allow
-  // changing it once we start streaming; whether it can support our chosen
-  // output format or not may depend on the input format.
-  memset(&fmtdesc, 0, sizeof(fmtdesc));
-  fmtdesc.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-  output_format_fourcc_ = 0;
-  while (device_->Ioctl(VIDIOC_ENUM_FMT, &fmtdesc) == 0) {
-    if (IsSupportedOutputFormat(fmtdesc.pixelformat)) {
-      output_format_fourcc_ = fmtdesc.pixelformat;
-      break;
-    }
-    ++fmtdesc.index;
-  }
-
-  if (output_format_fourcc_ == 0) {
-    VLOGF(1) << "Could not find a usable output format";
-    return false;
-  }
-
-  // Only set fourcc for output; resolution, etc., will come from the
-  // driver once it extracts it from the stream.
-  memset(&format, 0, sizeof(format));
-  format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-  format.fmt.pix_mp.pixelformat = output_format_fourcc_;
-  format.fmt.pix_mp.num_planes = output_planes_count_;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_FMT, &format);
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::CreateInputBuffers() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK(!input_streamon_);
-  DCHECK(input_buffer_map_.empty());
-
-  struct v4l2_requestbuffers reqbufs;
-  memset(&reqbufs, 0, sizeof(reqbufs));
-  reqbufs.count = kNumInputBuffers;
-  reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-  reqbufs.memory = V4L2_MEMORY_MMAP;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs);
-  if (reqbufs.count < kNumInputBuffers) {
-    VLOGF(1) << "Could not allocate enough output buffers";
-    return false;
-  }
-  input_buffer_map_.resize(reqbufs.count);
-  for (size_t i = 0; i < input_buffer_map_.size(); ++i) {
-    free_input_buffers_.push_back(i);
-
-    // Query for the MEMORY_MMAP pointer.
-    struct v4l2_plane planes[VIDEO_MAX_PLANES];
-    struct v4l2_buffer_custom buffer;
-    memset(&buffer, 0, sizeof(buffer));
-    memset(planes, 0, sizeof(planes));
-    buffer.index = i;
-    buffer.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-    buffer.memory = V4L2_MEMORY_MMAP;
-    buffer.m.planes = planes;
-    buffer.length = input_planes_count_;
-    IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QUERYBUF, &buffer);
-    void* address = device_->Mmap(nullptr,
-                                  buffer.m.planes[0].length,
-                                  PROT_READ | PROT_WRITE,
-                                  MAP_SHARED,
-                                  buffer.m.planes[0].m.mem_offset);
-    if (address == MAP_FAILED) {
-      VLOGF(1) << "mmap() failed";
-      return false;
-    }
-    input_buffer_map_[i].address = address;
-    input_buffer_map_[i].length = buffer.m.planes[0].length;
-  }
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::CreateOutputBuffers() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK(!output_streamon_);
-  DCHECK(output_buffer_map_.empty());
-  DCHECK(surfaces_at_display_.empty());
-  DCHECK(surfaces_at_device_.empty());
-
-  Size pic_size = decoder_->GetPicSize();
-  size_t num_pictures = decoder_->GetRequiredNumOfPictures();
-
-  DCHECK_GT(num_pictures, 0u);
-  DCHECK(!pic_size.IsEmpty());
-
-  struct v4l2_format format;
-  memset(&format, 0, sizeof(format));
-  format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-  format.fmt.pix_mp.pixelformat = output_format_fourcc_;
-  format.fmt.pix_mp.width = pic_size.width();
-  format.fmt.pix_mp.height = pic_size.height();
-  format.fmt.pix_mp.num_planes = input_planes_count_;
-
-  if (device_->Ioctl(VIDIOC_S_FMT, &format) != 0) {
-    VPLOGF(1) << "Failed setting format to: " << output_format_fourcc_;
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-
-  coded_size_.SetSize(base::checked_cast<int>(format.fmt.pix_mp.width),
-                      base::checked_cast<int>(format.fmt.pix_mp.height));
-  DCHECK_EQ(coded_size_.width() % 16, 0);
-  DCHECK_EQ(coded_size_.height() % 16, 0);
-
-  if (!Rect(coded_size_).Contains(Rect(pic_size))) {
-    VLOGF(1) << "Got invalid adjusted coded size: " << coded_size_.ToString();
-    return false;
-  }
-
-  DVLOGF(3) << "buffer_count=" << num_pictures
-            << ", pic size=" << pic_size.ToString()
-            << ", coded size=" << coded_size_.ToString();
-
-  VideoPixelFormat pixel_format =
-      V4L2Device::V4L2PixFmtToVideoPixelFormat(output_format_fourcc_);
-
-  child_task_runner_->PostTask(
-      FROM_HERE,
-      base::Bind(&VideoDecodeAccelerator::Client::ProvidePictureBuffers,
-                 client_, num_pictures, pixel_format, coded_size_));
-
-  // Go into kAwaitingPictureBuffers to prevent us from doing any more decoding
-  // or event handling while we are waiting for AssignPictureBuffers(). Not
-  // having Pictures available would not have prevented us from making decoding
-  // progress entirely e.g. in the case of H.264 where we could further decode
-  // non-slice NALUs and could even get another resolution change before we were
-  // done with this one. After we get the buffers, we'll go back into kIdle and
-  // kick off further event processing, and eventually go back into kDecoding
-  // once no more events are pending (if any).
-  state_ = kAwaitingPictureBuffers;
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::DestroyInputBuffers() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread() ||
-         !decoder_thread_.IsRunning());
-  DCHECK(!input_streamon_);
-
-  if (input_buffer_map_.empty())
-    return;
-
-  for (auto& input_record : input_buffer_map_) {
-    if (input_record.address != nullptr)
-      device_->Munmap(input_record.address, input_record.length);
-  }
-
-  struct v4l2_requestbuffers reqbufs;
-  memset(&reqbufs, 0, sizeof(reqbufs));
-  reqbufs.count = 0;
-  reqbufs.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-  reqbufs.memory = V4L2_MEMORY_MMAP;
-  IOCTL_OR_LOG_ERROR(VIDIOC_REQBUFS, &reqbufs);
-
-  input_buffer_map_.clear();
-  free_input_buffers_.clear();
-}
-
-void V4L2SliceVideoDecodeAccelerator::DismissPictures(
-    const std::vector<int32_t>& picture_buffer_ids,
-    base::WaitableEvent* done) {
-  DVLOGF(3);
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-
-  for (auto picture_buffer_id : picture_buffer_ids) {
-    DVLOGF(4) << "dismissing PictureBuffer id=" << picture_buffer_id;
-    client_->DismissPictureBuffer(picture_buffer_id);
-  }
-
-  done->Signal();
-}
-
-void V4L2SliceVideoDecodeAccelerator::DevicePollTask(bool poll_device) {
-  DVLOGF(3);
-  DCHECK(device_poll_thread_.task_runner()->BelongsToCurrentThread());
-
-  bool event_pending;
-  if (!device_->Poll(poll_device, &event_pending)) {
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return;
-  }
-
-  // All processing should happen on ServiceDeviceTask(), since we shouldn't
-  // touch encoder state from this thread.
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::ServiceDeviceTask,
-                            base::Unretained(this)));
-}
-
-void V4L2SliceVideoDecodeAccelerator::ServiceDeviceTask() {
-  DVLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  // ServiceDeviceTask() should only ever be scheduled from DevicePollTask().
-
-  Dequeue();
-  SchedulePollIfNeeded();
-}
-
-void V4L2SliceVideoDecodeAccelerator::SchedulePollIfNeeded() {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  if (!device_poll_thread_.IsRunning()) {
-    DVLOGF(4) << "Device poll thread stopped, will not schedule poll";
-    return;
-  }
-
-  DCHECK(input_streamon_ || output_streamon_);
-
-  if (input_buffer_queued_count_ + output_buffer_queued_count_ == 0) {
-    DVLOGF(4) << "No buffers queued, will not schedule poll";
-    return;
-  }
-
-  DVLOGF(4) << "Scheduling device poll task";
-
-  device_poll_thread_.task_runner()->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DevicePollTask,
-                            base::Unretained(this), true));
-
-  DVLOGF(3) << "buffer counts: "
-            << "INPUT[" << decoder_input_queue_.size() << "]"
-            << " => DEVICE["
-            << free_input_buffers_.size() << "+"
-            << input_buffer_queued_count_ << "/"
-            << input_buffer_map_.size() << "]->["
-            << free_output_buffers_.size() << "+"
-            << output_buffer_queued_count_ << "/"
-            << output_buffer_map_.size() << "]"
-            << " => DISPLAYQ[" << decoder_display_queue_.size() << "]"
-            << " => CLIENT[" << surfaces_at_display_.size() << "]";
-}
-
-void V4L2SliceVideoDecodeAccelerator::Enqueue(
-    const scoped_refptr<V4L2DecodeSurface>& dec_surface) {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  const int old_inputs_queued = input_buffer_queued_count_;
-  const int old_outputs_queued = output_buffer_queued_count_;
-
-  if (!EnqueueInputRecord(dec_surface->input_record(),
-                          dec_surface->config_store())) {
-    VLOGF(1) << "Failed queueing an input buffer";
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return;
-  }
-
-  if (!EnqueueOutputRecord(dec_surface->output_record())) {
-    VLOGF(1) << "Failed queueing an output buffer";
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return;
-  }
-
-  bool inserted =
-      surfaces_at_device_
-          .insert(std::make_pair(dec_surface->output_record(), dec_surface))
-          .second;
-  DCHECK(inserted);
-
-  if (old_inputs_queued == 0 && old_outputs_queued == 0)
-    SchedulePollIfNeeded();
-}
-
-void V4L2SliceVideoDecodeAccelerator::Dequeue() {
-  DVLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  struct v4l2_buffer_custom dqbuf;
-  struct v4l2_plane planes[VIDEO_MAX_PLANES];
-  while (input_buffer_queued_count_ > 0) {
-    DCHECK(input_streamon_);
-    memset(&dqbuf, 0, sizeof(dqbuf));
-    memset(&planes, 0, sizeof(planes));
-    dqbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-    dqbuf.memory = V4L2_MEMORY_MMAP;
-    dqbuf.m.planes = planes;
-    dqbuf.length = input_planes_count_;
-    if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) {
-      if (errno == EAGAIN) {
-        // EAGAIN if we're just out of buffers to dequeue.
-        break;
-      }
-      VPLOGF(1) << "ioctl() failed: VIDIOC_DQBUF";
-      NOTIFY_ERROR(PLATFORM_FAILURE);
-      return;
-    }
-    InputRecord& input_record = input_buffer_map_[dqbuf.index];
-    DCHECK(input_record.at_device);
-    input_record.at_device = false;
-    ReuseInputBuffer(dqbuf.index);
-    input_buffer_queued_count_--;
-    DVLOGF(4) << "Dequeued input=" << dqbuf.index
-              << " count: " << input_buffer_queued_count_;
-  }
-
-  while (output_buffer_queued_count_ > 0) {
-    DCHECK(output_streamon_);
-    memset(&dqbuf, 0, sizeof(dqbuf));
-    memset(&planes, 0, sizeof(planes));
-    dqbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-    dqbuf.memory =
-        (output_mode_ == Config::OutputMode::ALLOCATE ? V4L2_MEMORY_MMAP
-                                                      : V4L2_MEMORY_DMABUF);
-    dqbuf.m.planes = planes;
-    dqbuf.length = output_planes_count_;
-    if (device_->Ioctl(VIDIOC_DQBUF, &dqbuf) != 0) {
-      if (errno == EAGAIN) {
-        // EAGAIN if we're just out of buffers to dequeue.
-        break;
-      }
-      VPLOGF(1) << "ioctl() failed: VIDIOC_DQBUF";
-      NOTIFY_ERROR(PLATFORM_FAILURE);
-      return;
-    }
-    OutputRecord& output_record = output_buffer_map_[dqbuf.index];
-    DCHECK(output_record.at_device);
-    output_record.at_device = false;
-    output_buffer_queued_count_--;
-    DVLOGF(4) << "Dequeued output=" << dqbuf.index << " count "
-              << output_buffer_queued_count_;
-
-    V4L2DecodeSurfaceByOutputId::iterator it =
-        surfaces_at_device_.find(dqbuf.index);
-    if (it == surfaces_at_device_.end()) {
-      VLOGF(1) << "Got invalid surface from device.";
-      NOTIFY_ERROR(PLATFORM_FAILURE);
-      return;
-    }
-
-    it->second->SetDecoded();
-    surfaces_at_device_.erase(it);
-  }
-
-  // A frame was decoded, see if we can output it.
-  TryOutputSurfaces();
-
-  ProcessPendingEventsIfNeeded();
-  ScheduleDecodeBufferTaskIfNeeded();
-}
-
-void V4L2SliceVideoDecodeAccelerator::NewEventPending() {
-  // Switch to event processing mode if we are decoding. Otherwise we are either
-  // already in it, or we will potentially switch to it later, after finishing
-  // other tasks.
-  if (state_ == kDecoding)
-    state_ = kIdle;
-
-  ProcessPendingEventsIfNeeded();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::FinishEventProcessing() {
-  DCHECK_EQ(state_, kIdle);
-
-  state_ = kDecoding;
-  ScheduleDecodeBufferTaskIfNeeded();
-
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::ProcessPendingEventsIfNeeded() {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  // Process pending events, if any, in the correct order.
-  // We always first process the surface set change, as it is an internal
-  // event from the decoder and interleaving it with external requests would
-  // put the decoder in an undefined state.
-  using ProcessFunc = bool (V4L2SliceVideoDecodeAccelerator::*)();
-  const ProcessFunc process_functions[] = {
-      &V4L2SliceVideoDecodeAccelerator::FinishSurfaceSetChange,
-      &V4L2SliceVideoDecodeAccelerator::FinishFlush,
-      &V4L2SliceVideoDecodeAccelerator::FinishReset,
-      &V4L2SliceVideoDecodeAccelerator::FinishEventProcessing,
-  };
-
-  for (const auto& fn : process_functions) {
-    if (state_ != kIdle)
-      return;
-
-    if (!(this->*fn)())
-      return;
-  }
-}
-
-void V4L2SliceVideoDecodeAccelerator::ReuseInputBuffer(int index) {
-  DVLOGF(4) << "Reusing input buffer, index=" << index;
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  DCHECK_LT(index, static_cast<int>(input_buffer_map_.size()));
-  InputRecord& input_record = input_buffer_map_[index];
-
-  DCHECK(!input_record.at_device);
-  input_record.input_id = -1;
-  input_record.bytes_used = 0;
-
-  DCHECK_EQ(
-      std::count(free_input_buffers_.begin(), free_input_buffers_.end(), index),
-      0);
-  free_input_buffers_.push_back(index);
-}
-
-void V4L2SliceVideoDecodeAccelerator::ReuseOutputBuffer(int index) {
-  DVLOGF(4) << "Reusing output buffer, index=" << index;
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  DCHECK_LT(index, static_cast<int>(output_buffer_map_.size()));
-  OutputRecord& output_record = output_buffer_map_[index];
-  DCHECK(!output_record.at_device);
-  DCHECK(!output_record.at_client);
-
-  DCHECK_EQ(std::count(free_output_buffers_.begin(), free_output_buffers_.end(),
-                       index),
-            0);
-  free_output_buffers_.push_back(index);
-
-  ScheduleDecodeBufferTaskIfNeeded();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::EnqueueInputRecord(
-    int index,
-    uint32_t config_store) {
-  DVLOGF(4);
-  DCHECK_LT(index, static_cast<int>(input_buffer_map_.size()));
-  DCHECK_GT(config_store, 0u);
-
-  // Enqueue an input (VIDEO_OUTPUT) buffer for an input video frame.
-  InputRecord& input_record = input_buffer_map_[index];
-  DCHECK(!input_record.at_device);
-  struct v4l2_buffer_custom qbuf;
-  struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES];
-  memset(&qbuf, 0, sizeof(qbuf));
-  memset(qbuf_planes, 0, sizeof(qbuf_planes));
-  qbuf.index = index;
-  qbuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-  qbuf.memory = V4L2_MEMORY_MMAP;
-  qbuf.m.planes = qbuf_planes;
-  qbuf.m.planes[0].bytesused = input_record.bytes_used;
-  qbuf.length = input_planes_count_;
-  qbuf.config_store = config_store;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf);
-  input_record.at_device = true;
-  input_buffer_queued_count_++;
-  DVLOGF(4) << "Enqueued input=" << qbuf.index
-            << " count: " << input_buffer_queued_count_;
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::EnqueueOutputRecord(int index) {
-  DVLOGF(4);
-  DCHECK_LT(index, static_cast<int>(output_buffer_map_.size()));
-
-  // Enqueue an output (VIDEO_CAPTURE) buffer.
-  OutputRecord& output_record = output_buffer_map_[index];
-  DCHECK(!output_record.at_device);
-  DCHECK(!output_record.at_client);
-  DCHECK_NE(output_record.picture_id, -1);
-
-  struct v4l2_buffer_custom qbuf;
-  struct v4l2_plane qbuf_planes[VIDEO_MAX_PLANES];
-  memset(&qbuf, 0, sizeof(qbuf));
-  memset(qbuf_planes, 0, sizeof(qbuf_planes));
-  qbuf.index = index;
-  qbuf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-  if (output_mode_ == Config::OutputMode::ALLOCATE) {
-    qbuf.memory = V4L2_MEMORY_MMAP;
-  } else {
-    qbuf.memory = V4L2_MEMORY_DMABUF;
-    DCHECK_EQ(output_planes_count_, output_record.dmabuf_fds.size());
-    for (size_t i = 0; i < output_record.dmabuf_fds.size(); ++i) {
-      DCHECK(output_record.dmabuf_fds[i].is_valid());
-      qbuf_planes[i].m.fd = output_record.dmabuf_fds[i].get();
-    }
-  }
-  qbuf.m.planes = qbuf_planes;
-  qbuf.length = output_planes_count_;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_QBUF, &qbuf);
-  output_record.at_device = true;
-  output_buffer_queued_count_++;
-  DVLOGF(4) << "Enqueued output=" << qbuf.index
-            << " count: " << output_buffer_queued_count_;
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::StartDevicePoll() {
-  DVLOGF(3) << "Starting device poll";
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK(!device_poll_thread_.IsRunning());
-
-  // Start up the device poll thread and schedule its first DevicePollTask().
-  if (!device_poll_thread_.Start()) {
-    VLOGF(1) << "Device thread failed to start";
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-  if (!input_streamon_) {
-    __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-    IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMON, &type);
-    input_streamon_ = true;
-  }
-
-  if (!output_streamon_) {
-    __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-    IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMON, &type);
-    output_streamon_ = true;
-  }
-
-  device_poll_thread_.task_runner()->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DevicePollTask,
-                            base::Unretained(this), true));
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::StopDevicePoll(bool keep_input_state) {
-  DVLOGF(3) << "Stopping device poll";
-  if (decoder_thread_.IsRunning())
-    DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  // Signal the DevicePollTask() to stop, and stop the device poll thread.
-  if (!device_->SetDevicePollInterrupt()) {
-    VPLOGF(1) << "SetDevicePollInterrupt(): failed";
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-  device_poll_thread_.Stop();
-  DVLOGF(3) << "Device poll thread stopped";
-
-  // Clear the interrupt now, to be sure.
-  if (!device_->ClearDevicePollInterrupt()) {
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-
-  if (!keep_input_state) {
-    if (input_streamon_) {
-      __u32 type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
-      IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type);
-    }
-    input_streamon_ = false;
-  }
-
-  if (output_streamon_) {
-    __u32 type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-    IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_STREAMOFF, &type);
-  }
-  output_streamon_ = false;
-
-  if (!keep_input_state) {
-    for (size_t i = 0; i < input_buffer_map_.size(); ++i) {
-      InputRecord& input_record = input_buffer_map_[i];
-      if (input_record.at_device) {
-        input_record.at_device = false;
-        ReuseInputBuffer(i);
-        input_buffer_queued_count_--;
-      }
-    }
-    DCHECK_EQ(input_buffer_queued_count_, 0);
-  }
-
-  // STREAMOFF makes the driver drop all buffers without decoding and DQBUFing,
-  // so we mark them all as at_device = false and clear surfaces_at_device_.
-  for (size_t i = 0; i < output_buffer_map_.size(); ++i) {
-    OutputRecord& output_record = output_buffer_map_[i];
-    if (output_record.at_device) {
-      output_record.at_device = false;
-      output_buffer_queued_count_--;
-    }
-  }
-  surfaces_at_device_.clear();
-  DCHECK_EQ(output_buffer_queued_count_, 0);
-
-  // Drop all surfaces that were awaiting decode before being displayed,
-  // since we've just cancelled all outstanding decodes.
-  while (!decoder_display_queue_.empty())
-    decoder_display_queue_.pop();
-
-  DVLOGF(3) << "Device poll stopped";
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::Decode(
-    const BitstreamBuffer& bitstream_buffer) {
-  DVLOGF(4) << "input_id=" << bitstream_buffer.id()
-            << ", size=" << bitstream_buffer.size();
-  DCHECK(decode_task_runner_->BelongsToCurrentThread());
-
-  if (bitstream_buffer.id() < 0) {
-    VLOGF(1) << "Invalid bitstream_buffer, id: " << bitstream_buffer.id();
-    if (base::SharedMemory::IsHandleValid(bitstream_buffer.handle()))
-      base::SharedMemory::CloseHandle(bitstream_buffer.handle());
-    NOTIFY_ERROR(INVALID_ARGUMENT);
-    return;
-  }
-
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DecodeTask,
-                            base::Unretained(this), bitstream_buffer));
-}
-
-void V4L2SliceVideoDecodeAccelerator::DecodeTask(
-    const BitstreamBuffer& bitstream_buffer) {
-  DVLOGF(4) << "input_id=" << bitstream_buffer.id()
-            << " size=" << bitstream_buffer.size();
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  std::unique_ptr<BitstreamBufferRef> bitstream_record(new BitstreamBufferRef(
-      decode_client_, decode_task_runner_,
-      new SharedMemoryRegion(bitstream_buffer, true), bitstream_buffer.id()));
-
-  // Skip empty buffer.
-  if (bitstream_buffer.size() == 0)
-    return;
-
-  if (!bitstream_record->shm->Map()) {
-    VLOGF(1) << "Could not map bitstream_buffer";
-    NOTIFY_ERROR(UNREADABLE_INPUT);
-    return;
-  }
-  DVLOGF(4) << "mapped at=" << bitstream_record->shm->memory();
-
-  decoder_input_queue_.push(
-      linked_ptr<BitstreamBufferRef>(bitstream_record.release()));
-
-  ScheduleDecodeBufferTaskIfNeeded();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::TrySetNewBistreamBuffer() {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK(!decoder_current_bitstream_buffer_);
-
-  if (decoder_input_queue_.empty())
-    return false;
-
-  decoder_current_bitstream_buffer_.reset(
-      decoder_input_queue_.front().release());
-  decoder_input_queue_.pop();
-
-  if (decoder_current_bitstream_buffer_->input_id == kFlushBufferId) {
-    // This is a buffer we queued for ourselves to trigger flush at this time.
-    InitiateFlush();
-    return false;
-  }
-
-  const uint8_t* const data = reinterpret_cast<const uint8_t*>(
-      decoder_current_bitstream_buffer_->shm->memory());
-  const size_t data_size = decoder_current_bitstream_buffer_->shm->size();
-  decoder_->SetStream(data, data_size);
-
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::ScheduleDecodeBufferTaskIfNeeded() {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  if (state_ == kDecoding) {
-    decoder_thread_task_runner_->PostTask(
-        FROM_HERE,
-        base::Bind(&V4L2SliceVideoDecodeAccelerator::DecodeBufferTask,
-                   base::Unretained(this)));
-  }
-}
-
-void V4L2SliceVideoDecodeAccelerator::DecodeBufferTask() {
-  DVLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  if (state_ != kDecoding) {
-    DVLOGF(3) << "Early exit, not in kDecoding";
-    return;
-  }
-
-  while (true) {
-    AcceleratedVideoDecoder::DecodeResult res;
-    res = decoder_->Decode();
-    switch (res) {
-      case AcceleratedVideoDecoder::kAllocateNewSurfaces:
-        VLOGF(2) << "Decoder requesting a new set of surfaces";
-        InitiateSurfaceSetChange();
-        return;
-
-      case AcceleratedVideoDecoder::kRanOutOfStreamData:
-        decoder_current_bitstream_buffer_.reset();
-        if (!TrySetNewBistreamBuffer())
-          return;
-
-        break;
-
-      case AcceleratedVideoDecoder::kRanOutOfSurfaces:
-        // No more surfaces for the decoder, we'll come back once we have more.
-        DVLOGF(4) << "Ran out of surfaces";
-        return;
-
-      case AcceleratedVideoDecoder::kNeedContextUpdate:
-        DVLOGF(4) << "Awaiting context update";
-        return;
-
-      case AcceleratedVideoDecoder::kDecodeError:
-        VLOGF(1) << "Error decoding stream";
-        NOTIFY_ERROR(PLATFORM_FAILURE);
-        return;
-    }
-  }
-}
-
-void V4L2SliceVideoDecodeAccelerator::InitiateSurfaceSetChange() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_EQ(state_, kDecoding);
-
-  DCHECK(!surface_set_change_pending_);
-  surface_set_change_pending_ = true;
-  NewEventPending();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::FinishSurfaceSetChange() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  if (!surface_set_change_pending_)
-    return true;
-
-  if (!surfaces_at_device_.empty())
-    return false;
-
-  DCHECK_EQ(state_, kIdle);
-  DCHECK(decoder_display_queue_.empty());
-  // All output buffers should've been returned from decoder and device by now.
-  // The only remaining owner of surfaces may be display (client), and we will
-  // dismiss them when destroying output buffers below.
-  DCHECK_EQ(free_output_buffers_.size() + surfaces_at_display_.size(),
-            output_buffer_map_.size());
-
-  // Keep input queue running while we switch outputs.
-  if (!StopDevicePoll(true)) {
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-
-  // Dequeued decoded surfaces may be pended in pending_picture_ready_ if they
-  // are waiting for some pictures to be cleared. We should post them right away
-  // because they are about to be dismissed and destroyed for surface set
-  // change.
-  SendPictureReady();
-
-  // This will return only once all buffers are dismissed and destroyed.
-  // This does not wait until they are displayed however, as display retains
-  // references to the buffers bound to textures and will release them
-  // after displaying.
-  if (!DestroyOutputs(true)) {
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-
-  if (!CreateOutputBuffers()) {
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return false;
-  }
-
-  surface_set_change_pending_ = false;
-  VLOGF(2) << "Surface set change finished";
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::DestroyOutputs(bool dismiss) {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  std::vector<int32_t> picture_buffers_to_dismiss;
-
-  if (output_buffer_map_.empty())
-    return true;
-
-  for (const auto& output_record : output_buffer_map_) {
-    DCHECK(!output_record.at_device);
-    picture_buffers_to_dismiss.push_back(output_record.picture_id);
-  }
-
-  if (dismiss) {
-    VLOGF(2) << "Scheduling picture dismissal";
-    base::WaitableEvent done(base::WaitableEvent::ResetPolicy::AUTOMATIC,
-                             base::WaitableEvent::InitialState::NOT_SIGNALED);
-    child_task_runner_->PostTask(
-        FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::DismissPictures,
-                              weak_this_, picture_buffers_to_dismiss, &done));
-    done.Wait();
-  }
-
-  // At this point client can't call ReusePictureBuffer on any of the pictures
-  // anymore, so it's safe to destroy.
-  return DestroyOutputBuffers();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::DestroyOutputBuffers() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread() ||
-         !decoder_thread_.IsRunning());
-  DCHECK(!output_streamon_);
-  DCHECK(surfaces_at_device_.empty());
-  DCHECK(decoder_display_queue_.empty());
-  DCHECK_EQ(surfaces_at_display_.size() + free_output_buffers_.size(),
-            output_buffer_map_.size());
-
-  if (output_buffer_map_.empty())
-    return true;
-
-  // It's ok to do this, client will retain references to textures, but we are
-  // not interested in reusing the surfaces anymore.
-  // This will prevent us from reusing old surfaces in case we have some
-  // ReusePictureBuffer() pending on ChildThread already. It's ok to ignore
-  // them, because we have already dismissed them (in DestroyOutputs()).
-  for (const auto& surface_at_display : surfaces_at_display_) {
-    size_t index = surface_at_display.second->output_record();
-    DCHECK_LT(index, output_buffer_map_.size());
-    OutputRecord& output_record = output_buffer_map_[index];
-    DCHECK(output_record.at_client);
-    output_record.at_client = false;
-  }
-  surfaces_at_display_.clear();
-  DCHECK_EQ(free_output_buffers_.size(), output_buffer_map_.size());
-
-  free_output_buffers_.clear();
-  output_buffer_map_.clear();
-
-  struct v4l2_requestbuffers reqbufs;
-  memset(&reqbufs, 0, sizeof(reqbufs));
-  reqbufs.count = 0;
-  reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-  reqbufs.memory = V4L2_MEMORY_MMAP;
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_REQBUFS, &reqbufs);
-
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::AssignPictureBuffers(
-    const std::vector<PictureBuffer>& buffers) {
-  VLOGF(2);
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE,
-      base::Bind(&V4L2SliceVideoDecodeAccelerator::AssignPictureBuffersTask,
-                 base::Unretained(this), buffers));
-}
-
-void V4L2SliceVideoDecodeAccelerator::AssignPictureBuffersTask(
-    const std::vector<PictureBuffer>& buffers) {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_EQ(state_, kAwaitingPictureBuffers);
-
-  const uint32_t req_buffer_count = decoder_->GetRequiredNumOfPictures();
-
-  if (buffers.size() < req_buffer_count) {
-    VLOGF(1) << "Failed to provide requested picture buffers. "
-             << "(Got " << buffers.size() << ", requested " << req_buffer_count
-             << ")";
-    NOTIFY_ERROR(INVALID_ARGUMENT);
-    return;
-  }
-
-  // Allocate the output buffers.
-  struct v4l2_requestbuffers reqbufs;
-  memset(&reqbufs, 0, sizeof(reqbufs));
-  reqbufs.count = buffers.size();
-  reqbufs.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
-  reqbufs.memory =
-      (output_mode_ == Config::OutputMode::ALLOCATE ? V4L2_MEMORY_MMAP
-                                                    : V4L2_MEMORY_DMABUF);
-  IOCTL_OR_ERROR_RETURN(VIDIOC_REQBUFS, &reqbufs);
-
-  if (reqbufs.count != buffers.size()) {
-    VLOGF(1) << "Could not allocate enough output buffers";
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return;
-  }
-
-  DCHECK(free_output_buffers_.empty());
-  DCHECK(output_buffer_map_.empty());
-  output_buffer_map_.resize(buffers.size());
-  for (size_t i = 0; i < output_buffer_map_.size(); ++i) {
-    DCHECK(buffers[i].size() == coded_size_);
-
-    OutputRecord& output_record = output_buffer_map_[i];
-    DCHECK(!output_record.at_device);
-    DCHECK(!output_record.at_client);
-    DCHECK_EQ(output_record.picture_id, -1);
-    DCHECK(output_record.dmabuf_fds.empty());
-    DCHECK_EQ(output_record.cleared, false);
-
-    output_record.picture_id = buffers[i].id();
-
-    // This will remain true until ImportBufferForPicture is called, either by
-    // the client, or by ourselves, if we are allocating.
-    output_record.at_client = true;
-    if (output_mode_ == Config::OutputMode::ALLOCATE) {
-      std::vector<base::ScopedFD> dmabuf_fds = device_->GetDmabufsForV4L2Buffer(
-          i, output_planes_count_, V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE);
-      if (dmabuf_fds.empty()) {
-        NOTIFY_ERROR(PLATFORM_FAILURE);
-        return;
-      }
-
-      auto passed_dmabuf_fds(base::WrapUnique(
-          new std::vector<base::ScopedFD>(std::move(dmabuf_fds))));
-      ImportBufferForPictureTask(output_record.picture_id,
-                                 std::move(passed_dmabuf_fds));
-    }  // else we'll get triggered via ImportBufferForPicture() from client.
-    DVLOGF(3) << "buffer[" << i << "]: picture_id=" << output_record.picture_id;
-  }
-
-  if (!StartDevicePoll()) {
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return;
-  }
-
-  // Put us in kIdle to allow further event processing.
-  // ProcessPendingEventsIfNeeded() will put us back into kDecoding after all
-  // other pending events are processed successfully.
-  state_ = kIdle;
-  ProcessPendingEventsIfNeeded();
-}
-
-void V4L2SliceVideoDecodeAccelerator::ImportBufferForPicture(
-    int32_t picture_buffer_id,
-    VideoPixelFormat pixel_format,
-    const NativePixmapHandle& native_pixmap_handle) {
-  DVLOGF(3) << "picture_buffer_id=" << picture_buffer_id;
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-
-  auto passed_dmabuf_fds(base::WrapUnique(new std::vector<base::ScopedFD>()));
-  for (const auto& fd : native_pixmap_handle.fds) {
-    DCHECK_NE(fd.fd, -1);
-    passed_dmabuf_fds->push_back(base::ScopedFD(fd.fd));
-  }
-
-  if (output_mode_ != Config::OutputMode::IMPORT) {
-    VLOGF(1) << "Cannot import in non-import mode";
-    NOTIFY_ERROR(INVALID_ARGUMENT);
-    return;
-  }
-
-  if (pixel_format !=
-      V4L2Device::V4L2PixFmtToVideoPixelFormat(output_format_fourcc_)) {
-    VLOGF(1) << "Unsupported import format: " << pixel_format;
-    NOTIFY_ERROR(INVALID_ARGUMENT);
-    return;
-  }
-
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE,
-      base::Bind(&V4L2SliceVideoDecodeAccelerator::ImportBufferForPictureTask,
-                 base::Unretained(this), picture_buffer_id,
-                 base::Passed(&passed_dmabuf_fds)));
-}
-
-void V4L2SliceVideoDecodeAccelerator::ImportBufferForPictureTask(
-    int32_t picture_buffer_id,
-    std::unique_ptr<std::vector<base::ScopedFD>> passed_dmabuf_fds) {
-  DVLOGF(3) << "picture_buffer_id=" << picture_buffer_id;
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  const auto iter =
-      std::find_if(output_buffer_map_.begin(), output_buffer_map_.end(),
-                   [picture_buffer_id](const OutputRecord& output_record) {
-                     return output_record.picture_id == picture_buffer_id;
-                   });
-  if (iter == output_buffer_map_.end()) {
-    // It's possible that we've already posted a DismissPictureBuffer for this
-    // picture, but it has not yet executed when this ImportBufferForPicture was
-    // posted to us by the client. In that case just ignore this (we've already
-    // dismissed it and accounted for that).
-    DVLOGF(3) << "got picture id=" << picture_buffer_id
-              << " not in use (anymore?).";
-    return;
-  }
-
-  if (!iter->at_client) {
-    VLOGF(1) << "Cannot import buffer that not owned by client";
-    NOTIFY_ERROR(INVALID_ARGUMENT);
-    return;
-  }
-
-  size_t index = iter - output_buffer_map_.begin();
-  DCHECK_EQ(std::count(free_output_buffers_.begin(), free_output_buffers_.end(),
-                       index),
-            0);
-
-  DCHECK(!iter->at_device);
-  iter->at_client = false;
-
-  DCHECK_EQ(output_planes_count_, passed_dmabuf_fds->size());
-  iter->dmabuf_fds.swap(*passed_dmabuf_fds);
-  free_output_buffers_.push_back(index);
-  ScheduleDecodeBufferTaskIfNeeded();
-}
-
-void V4L2SliceVideoDecodeAccelerator::ReusePictureBuffer(
-    int32_t picture_buffer_id) {
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-  DVLOGF(4) << "picture_buffer_id=" << picture_buffer_id;
-
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE,
-      base::Bind(&V4L2SliceVideoDecodeAccelerator::ReusePictureBufferTask,
-                 base::Unretained(this), picture_buffer_id));
-}
-
-void V4L2SliceVideoDecodeAccelerator::ReusePictureBufferTask(
-    int32_t picture_buffer_id) {
-  DVLOGF(4) << "picture_buffer_id=" << picture_buffer_id;
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  V4L2DecodeSurfaceByPictureBufferId::iterator it =
-      surfaces_at_display_.find(picture_buffer_id);
-  if (it == surfaces_at_display_.end()) {
-    // It's possible that we've already posted a DismissPictureBuffer for this
-    // picture, but it has not yet executed when this ReusePictureBuffer was
-    // posted to us by the client. In that case just ignore this (we've already
-    // dismissed it and accounted for that) and let the sync object get
-    // destroyed.
-    DVLOGF(3) << "got picture id=" << picture_buffer_id
-              << " not in use (anymore?).";
-    return;
-  }
-
-  OutputRecord& output_record = output_buffer_map_[it->second->output_record()];
-  if (output_record.at_device || !output_record.at_client) {
-    VLOGF(1) << "picture_buffer_id not reusable";
-    NOTIFY_ERROR(INVALID_ARGUMENT);
-    return;
-  }
-
-  DCHECK(!output_record.at_device);
-  output_record.at_client = false;
-
-  surfaces_at_display_.erase(it);
-}
-
-void V4L2SliceVideoDecodeAccelerator::Flush() {
-  VLOGF(2);
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::FlushTask,
-                            base::Unretained(this)));
-}
-
-void V4L2SliceVideoDecodeAccelerator::FlushTask() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  // Queue an empty buffer which - when reached - will trigger flush sequence.
-  decoder_input_queue_.push(
-      linked_ptr<BitstreamBufferRef>(new BitstreamBufferRef(
-          decode_client_, decode_task_runner_, nullptr, kFlushBufferId)));
-
-  ScheduleDecodeBufferTaskIfNeeded();
-}
-
-void V4L2SliceVideoDecodeAccelerator::InitiateFlush() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  // This will trigger output for all remaining surfaces in the decoder.
-  // However, not all of them may be decoded yet (they would be queued
-  // in hardware then).
-  if (!decoder_->Flush()) {
-    DVLOGF(1) << "Failed flushing the decoder.";
-    NOTIFY_ERROR(PLATFORM_FAILURE);
-    return;
-  }
-
-  // Put the decoder in an idle state, ready to resume.
-  decoder_->Reset();
-
-  DCHECK(!decoder_flushing_);
-  decoder_flushing_ = true;
-  NewEventPending();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::FinishFlush() {
-  VLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  if (!decoder_flushing_)
-    return true;
-
-  if (!surfaces_at_device_.empty())
-    return false;
-
-  DCHECK_EQ(state_, kIdle);
-
-  // At this point, all remaining surfaces are decoded and dequeued, and since
-  // we have already scheduled output for them in InitiateFlush(), their
-  // respective PictureReady calls have been posted (or they have been queued on
-  // pending_picture_ready_). So at this time, once we SendPictureReady(),
-  // we will have all remaining PictureReady() posted to the client and we
-  // can post NotifyFlushDone().
-  DCHECK(decoder_display_queue_.empty());
-
-  // Decoder should have already returned all surfaces and all surfaces are
-  // out of hardware. There can be no other owners of input buffers.
-  DCHECK_EQ(free_input_buffers_.size(), input_buffer_map_.size());
-
-  SendPictureReady();
-
-  decoder_flushing_ = false;
-  VLOGF(2) << "Flush finished";
-
-  child_task_runner_->PostTask(FROM_HERE,
-                               base::Bind(&Client::NotifyFlushDone, client_));
-
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::Reset() {
-  VLOGF(2);
-  DCHECK(child_task_runner_->BelongsToCurrentThread());
-
-  decoder_thread_task_runner_->PostTask(
-      FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::ResetTask,
-                            base::Unretained(this)));
-}
-
-void V4L2SliceVideoDecodeAccelerator::ResetTask() {
-  VLOGF(2);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  if (decoder_resetting_) {
-    // This is a bug in the client, multiple Reset()s before NotifyResetDone()
-    // are not allowed.
-    NOTREACHED() << "Client should not be requesting multiple Reset()s";
-    return;
-  }
-
-  // Put the decoder in an idle state, ready to resume.
-  decoder_->Reset();
-
-  // Drop all remaining inputs.
-  decoder_current_bitstream_buffer_.reset();
-  while (!decoder_input_queue_.empty())
-    decoder_input_queue_.pop();
-
-  decoder_resetting_ = true;
-  NewEventPending();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::FinishReset() {
-  VLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  if (!decoder_resetting_)
-    return true;
-
-  if (!surfaces_at_device_.empty())
-    return false;
-
-  DCHECK_EQ(state_, kIdle);
-  DCHECK(!decoder_flushing_);
-  SendPictureReady();
-
-  // Drop any pending outputs.
-  while (!decoder_display_queue_.empty())
-    decoder_display_queue_.pop();
-
-  // At this point we can have no input buffers in the decoder, because we
-  // Reset()ed it in ResetTask(), and have not scheduled any new Decode()s
-  // having been in kIdle since. We don't have any surfaces in the HW either -
-  // we just checked that surfaces_at_device_.empty(), and inputs are tied
-  // to surfaces. Since there can be no other owners of input buffers, we can
-  // simply mark them all as available.
-  DCHECK_EQ(input_buffer_queued_count_, 0);
-  free_input_buffers_.clear();
-  for (size_t i = 0; i < input_buffer_map_.size(); ++i) {
-    DCHECK(!input_buffer_map_[i].at_device);
-    ReuseInputBuffer(i);
-  }
-
-  decoder_resetting_ = false;
-  VLOGF(2) << "Reset finished";
-
-  child_task_runner_->PostTask(FROM_HERE,
-                               base::Bind(&Client::NotifyResetDone, client_));
-
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::SetErrorState(Error error) {
-  // We can touch decoder_state_ only if this is the decoder thread or the
-  // decoder thread isn't running.
-  if (decoder_thread_.IsRunning() &&
-      !decoder_thread_task_runner_->BelongsToCurrentThread()) {
-    decoder_thread_task_runner_->PostTask(
-        FROM_HERE, base::Bind(&V4L2SliceVideoDecodeAccelerator::SetErrorState,
-                              base::Unretained(this), error));
-    return;
-  }
-
-  // Post NotifyError only if we are already initialized, as the API does
-  // not allow doing so before that.
-  if (state_ != kError && state_ != kUninitialized)
-    NotifyError(error);
-
-  state_ = kError;
-}
-
-V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::V4L2H264Accelerator(
-    V4L2SliceVideoDecodeAccelerator* v4l2_dec)
-    : num_slices_(0), v4l2_dec_(v4l2_dec) {
-  DCHECK(v4l2_dec_);
-}
-
-V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::~V4L2H264Accelerator() {}
-
-scoped_refptr<H264Picture>
-V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::CreateH264Picture() {
-  scoped_refptr<V4L2DecodeSurface> dec_surface = v4l2_dec_->CreateSurface();
-  if (!dec_surface)
-    return nullptr;
-
-  return new V4L2H264Picture(dec_surface);
-}
-
-void V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::
-    H264PictureListToDPBIndicesList(const H264Picture::Vector& src_pic_list,
-                                    uint8_t dst_list[kDPBIndicesListSize]) {
-  size_t i;
-  for (i = 0; i < src_pic_list.size() && i < kDPBIndicesListSize; ++i) {
-    const scoped_refptr<H264Picture>& pic = src_pic_list[i];
-    dst_list[i] = pic ? pic->dpb_position : VIDEO_MAX_FRAME;
-  }
-
-  while (i < kDPBIndicesListSize)
-    dst_list[i++] = VIDEO_MAX_FRAME;
-}
-
-void V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::H264DPBToV4L2DPB(
-    const H264DPB& dpb,
-    std::vector<scoped_refptr<V4L2DecodeSurface>>* ref_surfaces) {
-  memset(v4l2_decode_param_.dpb, 0, sizeof(v4l2_decode_param_.dpb));
-  size_t i = 0;
-  for (const auto& pic : dpb) {
-    if (i >= arraysize(v4l2_decode_param_.dpb)) {
-      VLOGF(1) << "Invalid DPB size";
-      break;
-    }
-
-    int index = VIDEO_MAX_FRAME;
-    if (!pic->nonexisting) {
-      scoped_refptr<V4L2DecodeSurface> dec_surface =
-          H264PictureToV4L2DecodeSurface(pic);
-      index = dec_surface->output_record();
-      ref_surfaces->push_back(dec_surface);
-    }
-
-    struct v4l2_h264_dpb_entry& entry = v4l2_decode_param_.dpb[i++];
-    entry.buf_index = index;
-    entry.frame_num = pic->frame_num;
-    entry.pic_num = pic->pic_num;
-    entry.top_field_order_cnt = pic->top_field_order_cnt;
-    entry.bottom_field_order_cnt = pic->bottom_field_order_cnt;
-    entry.flags = (pic->ref ? V4L2_H264_DPB_ENTRY_FLAG_ACTIVE : 0) |
-                  (pic->long_term ? V4L2_H264_DPB_ENTRY_FLAG_LONG_TERM : 0);
-  }
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::SubmitFrameMetadata(
-    const H264SPS* sps,
-    const H264PPS* pps,
-    const H264DPB& dpb,
-    const H264Picture::Vector& ref_pic_listp0,
-    const H264Picture::Vector& ref_pic_listb0,
-    const H264Picture::Vector& ref_pic_listb1,
-    const scoped_refptr<H264Picture>& pic) {
-  struct v4l2_ext_control_custom ctrl;
-  std::vector<struct v4l2_ext_control_custom> ctrls;
-
-  struct v4l2_ctrl_h264_sps v4l2_sps;
-  memset(&v4l2_sps, 0, sizeof(v4l2_sps));
-  v4l2_sps.constraint_set_flags =
-      (sps->constraint_set0_flag ? V4L2_H264_SPS_CONSTRAINT_SET0_FLAG : 0) |
-      (sps->constraint_set1_flag ? V4L2_H264_SPS_CONSTRAINT_SET1_FLAG : 0) |
-      (sps->constraint_set2_flag ? V4L2_H264_SPS_CONSTRAINT_SET2_FLAG : 0) |
-      (sps->constraint_set3_flag ? V4L2_H264_SPS_CONSTRAINT_SET3_FLAG : 0) |
-      (sps->constraint_set4_flag ? V4L2_H264_SPS_CONSTRAINT_SET4_FLAG : 0) |
-      (sps->constraint_set5_flag ? V4L2_H264_SPS_CONSTRAINT_SET5_FLAG : 0);
-#define SPS_TO_V4L2SPS(a) v4l2_sps.a = sps->a
-  SPS_TO_V4L2SPS(profile_idc);
-  SPS_TO_V4L2SPS(level_idc);
-  SPS_TO_V4L2SPS(seq_parameter_set_id);
-  SPS_TO_V4L2SPS(chroma_format_idc);
-  SPS_TO_V4L2SPS(bit_depth_luma_minus8);
-  SPS_TO_V4L2SPS(bit_depth_chroma_minus8);
-  SPS_TO_V4L2SPS(log2_max_frame_num_minus4);
-  SPS_TO_V4L2SPS(pic_order_cnt_type);
-  SPS_TO_V4L2SPS(log2_max_pic_order_cnt_lsb_minus4);
-  SPS_TO_V4L2SPS(offset_for_non_ref_pic);
-  SPS_TO_V4L2SPS(offset_for_top_to_bottom_field);
-  SPS_TO_V4L2SPS(num_ref_frames_in_pic_order_cnt_cycle);
-
-  static_assert(arraysize(v4l2_sps.offset_for_ref_frame) ==
-                    arraysize(sps->offset_for_ref_frame),
-                "offset_for_ref_frame arrays must be same size");
-  for (size_t i = 0; i < arraysize(v4l2_sps.offset_for_ref_frame); ++i)
-    v4l2_sps.offset_for_ref_frame[i] = sps->offset_for_ref_frame[i];
-  SPS_TO_V4L2SPS(max_num_ref_frames);
-  SPS_TO_V4L2SPS(pic_width_in_mbs_minus1);
-  SPS_TO_V4L2SPS(pic_height_in_map_units_minus1);
-#undef SPS_TO_V4L2SPS
-
-#define SET_V4L2_SPS_FLAG_IF(cond, flag) \
-  v4l2_sps.flags |= ((sps->cond) ? (flag) : 0)
-  SET_V4L2_SPS_FLAG_IF(separate_colour_plane_flag,
-                       V4L2_H264_SPS_FLAG_SEPARATE_COLOUR_PLANE);
-  SET_V4L2_SPS_FLAG_IF(qpprime_y_zero_transform_bypass_flag,
-                       V4L2_H264_SPS_FLAG_QPPRIME_Y_ZERO_TRANSFORM_BYPASS);
-  SET_V4L2_SPS_FLAG_IF(delta_pic_order_always_zero_flag,
-                       V4L2_H264_SPS_FLAG_DELTA_PIC_ORDER_ALWAYS_ZERO);
-  SET_V4L2_SPS_FLAG_IF(gaps_in_frame_num_value_allowed_flag,
-                       V4L2_H264_SPS_FLAG_GAPS_IN_FRAME_NUM_VALUE_ALLOWED);
-  SET_V4L2_SPS_FLAG_IF(frame_mbs_only_flag, V4L2_H264_SPS_FLAG_FRAME_MBS_ONLY);
-  SET_V4L2_SPS_FLAG_IF(mb_adaptive_frame_field_flag,
-                       V4L2_H264_SPS_FLAG_MB_ADAPTIVE_FRAME_FIELD);
-  SET_V4L2_SPS_FLAG_IF(direct_8x8_inference_flag,
-                       V4L2_H264_SPS_FLAG_DIRECT_8X8_INFERENCE);
-#undef SET_V4L2_SPS_FLAG_IF
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SPS;
-  ctrl.size = sizeof(v4l2_sps);
-  ctrl.p_h264_sps = &v4l2_sps;
-  ctrls.push_back(ctrl);
-
-  struct v4l2_ctrl_h264_pps v4l2_pps;
-  memset(&v4l2_pps, 0, sizeof(v4l2_pps));
-#define PPS_TO_V4L2PPS(a) v4l2_pps.a = pps->a
-  PPS_TO_V4L2PPS(pic_parameter_set_id);
-  PPS_TO_V4L2PPS(seq_parameter_set_id);
-  PPS_TO_V4L2PPS(num_slice_groups_minus1);
-  PPS_TO_V4L2PPS(num_ref_idx_l0_default_active_minus1);
-  PPS_TO_V4L2PPS(num_ref_idx_l1_default_active_minus1);
-  PPS_TO_V4L2PPS(weighted_bipred_idc);
-  PPS_TO_V4L2PPS(pic_init_qp_minus26);
-  PPS_TO_V4L2PPS(pic_init_qs_minus26);
-  PPS_TO_V4L2PPS(chroma_qp_index_offset);
-  PPS_TO_V4L2PPS(second_chroma_qp_index_offset);
-#undef PPS_TO_V4L2PPS
-
-#define SET_V4L2_PPS_FLAG_IF(cond, flag) \
-  v4l2_pps.flags |= ((pps->cond) ? (flag) : 0)
-  SET_V4L2_PPS_FLAG_IF(entropy_coding_mode_flag,
-                       V4L2_H264_PPS_FLAG_ENTROPY_CODING_MODE);
-  SET_V4L2_PPS_FLAG_IF(
-      bottom_field_pic_order_in_frame_present_flag,
-      V4L2_H264_PPS_FLAG_BOTTOM_FIELD_PIC_ORDER_IN_FRAME_PRESENT);
-  SET_V4L2_PPS_FLAG_IF(weighted_pred_flag, V4L2_H264_PPS_FLAG_WEIGHTED_PRED);
-  SET_V4L2_PPS_FLAG_IF(deblocking_filter_control_present_flag,
-                       V4L2_H264_PPS_FLAG_DEBLOCKING_FILTER_CONTROL_PRESENT);
-  SET_V4L2_PPS_FLAG_IF(constrained_intra_pred_flag,
-                       V4L2_H264_PPS_FLAG_CONSTRAINED_INTRA_PRED);
-  SET_V4L2_PPS_FLAG_IF(redundant_pic_cnt_present_flag,
-                       V4L2_H264_PPS_FLAG_REDUNDANT_PIC_CNT_PRESENT);
-  SET_V4L2_PPS_FLAG_IF(transform_8x8_mode_flag,
-                       V4L2_H264_PPS_FLAG_TRANSFORM_8X8_MODE);
-  SET_V4L2_PPS_FLAG_IF(pic_scaling_matrix_present_flag,
-                       V4L2_H264_PPS_FLAG_PIC_SCALING_MATRIX_PRESENT);
-#undef SET_V4L2_PPS_FLAG_IF
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_H264_PPS;
-  ctrl.size = sizeof(v4l2_pps);
-  ctrl.p_h264_pps = &v4l2_pps;
-  ctrls.push_back(ctrl);
-
-  struct v4l2_ctrl_h264_scaling_matrix v4l2_scaling_matrix;
-  memset(&v4l2_scaling_matrix, 0, sizeof(v4l2_scaling_matrix));
-
-  static_assert(arraysize(v4l2_scaling_matrix.scaling_list_4x4) <=
-                        arraysize(pps->scaling_list4x4) &&
-                    arraysize(v4l2_scaling_matrix.scaling_list_4x4[0]) <=
-                        arraysize(pps->scaling_list4x4[0]) &&
-                    arraysize(v4l2_scaling_matrix.scaling_list_8x8) <=
-                        arraysize(pps->scaling_list8x8) &&
-                    arraysize(v4l2_scaling_matrix.scaling_list_8x8[0]) <=
-                        arraysize(pps->scaling_list8x8[0]),
-                "scaling_lists must be of correct size");
-  static_assert(arraysize(v4l2_scaling_matrix.scaling_list_4x4) <=
-                        arraysize(sps->scaling_list4x4) &&
-                    arraysize(v4l2_scaling_matrix.scaling_list_4x4[0]) <=
-                        arraysize(sps->scaling_list4x4[0]) &&
-                    arraysize(v4l2_scaling_matrix.scaling_list_8x8) <=
-                        arraysize(sps->scaling_list8x8) &&
-                    arraysize(v4l2_scaling_matrix.scaling_list_8x8[0]) <=
-                        arraysize(sps->scaling_list8x8[0]),
-                "scaling_lists must be of correct size");
-
-  const auto* scaling_list4x4 = &sps->scaling_list4x4[0];
-  const auto* scaling_list8x8 = &sps->scaling_list8x8[0];
-  if (pps->pic_scaling_matrix_present_flag) {
-    scaling_list4x4 = &pps->scaling_list4x4[0];
-    scaling_list8x8 = &pps->scaling_list8x8[0];
-  }
-
-  for (size_t i = 0; i < arraysize(v4l2_scaling_matrix.scaling_list_4x4); ++i) {
-    for (size_t j = 0; j < arraysize(v4l2_scaling_matrix.scaling_list_4x4[i]);
-         ++j) {
-      v4l2_scaling_matrix.scaling_list_4x4[i][j] = scaling_list4x4[i][j];
-    }
-  }
-  for (size_t i = 0; i < arraysize(v4l2_scaling_matrix.scaling_list_8x8); ++i) {
-    for (size_t j = 0; j < arraysize(v4l2_scaling_matrix.scaling_list_8x8[i]);
-         ++j) {
-      v4l2_scaling_matrix.scaling_list_8x8[i][j] = scaling_list8x8[i][j];
-    }
-  }
-
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SCALING_MATRIX;
-  ctrl.size = sizeof(v4l2_scaling_matrix);
-  ctrl.p_h264_scal_mtrx = &v4l2_scaling_matrix;
-  ctrls.push_back(ctrl);
-
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      H264PictureToV4L2DecodeSurface(pic);
-
-  struct v4l2_ext_controls_custom ext_ctrls;
-  memset(&ext_ctrls, 0, sizeof(ext_ctrls));
-  ext_ctrls.count = ctrls.size();
-  ext_ctrls.controls = &ctrls[0];
-  ext_ctrls.config_store = dec_surface->config_store();
-  v4l2_dec_->SubmitExtControls(&ext_ctrls);
-
-  H264PictureListToDPBIndicesList(ref_pic_listp0,
-                                  v4l2_decode_param_.ref_pic_list_p0);
-  H264PictureListToDPBIndicesList(ref_pic_listb0,
-                                  v4l2_decode_param_.ref_pic_list_b0);
-  H264PictureListToDPBIndicesList(ref_pic_listb1,
-                                  v4l2_decode_param_.ref_pic_list_b1);
-
-  std::vector<scoped_refptr<V4L2DecodeSurface>> ref_surfaces;
-  H264DPBToV4L2DPB(dpb, &ref_surfaces);
-  dec_surface->SetReferenceSurfaces(ref_surfaces);
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::SubmitSlice(
-    const H264PPS* pps,
-    const H264SliceHeader* slice_hdr,
-    const H264Picture::Vector& ref_pic_list0,
-    const H264Picture::Vector& ref_pic_list1,
-    const scoped_refptr<H264Picture>& pic,
-    const uint8_t* data,
-    size_t size) {
-  if (num_slices_ == kMaxSlices) {
-    VLOGF(1) << "Over limit of supported slices per frame";
-    return false;
-  }
-
-  struct v4l2_ctrl_h264_slice_param& v4l2_slice_param =
-      v4l2_slice_params_[num_slices_++];
-  memset(&v4l2_slice_param, 0, sizeof(v4l2_slice_param));
-
-  v4l2_slice_param.size = size;
-#define SHDR_TO_V4L2SPARM(a) v4l2_slice_param.a = slice_hdr->a
-  SHDR_TO_V4L2SPARM(header_bit_size);
-  SHDR_TO_V4L2SPARM(first_mb_in_slice);
-  SHDR_TO_V4L2SPARM(slice_type);
-  SHDR_TO_V4L2SPARM(pic_parameter_set_id);
-  SHDR_TO_V4L2SPARM(colour_plane_id);
-  SHDR_TO_V4L2SPARM(frame_num);
-  SHDR_TO_V4L2SPARM(idr_pic_id);
-  SHDR_TO_V4L2SPARM(pic_order_cnt_lsb);
-  SHDR_TO_V4L2SPARM(delta_pic_order_cnt_bottom);
-  SHDR_TO_V4L2SPARM(delta_pic_order_cnt0);
-  SHDR_TO_V4L2SPARM(delta_pic_order_cnt1);
-  SHDR_TO_V4L2SPARM(redundant_pic_cnt);
-  SHDR_TO_V4L2SPARM(dec_ref_pic_marking_bit_size);
-  SHDR_TO_V4L2SPARM(cabac_init_idc);
-  SHDR_TO_V4L2SPARM(slice_qp_delta);
-  SHDR_TO_V4L2SPARM(slice_qs_delta);
-  SHDR_TO_V4L2SPARM(disable_deblocking_filter_idc);
-  SHDR_TO_V4L2SPARM(slice_alpha_c0_offset_div2);
-  SHDR_TO_V4L2SPARM(slice_beta_offset_div2);
-  SHDR_TO_V4L2SPARM(num_ref_idx_l0_active_minus1);
-  SHDR_TO_V4L2SPARM(num_ref_idx_l1_active_minus1);
-  SHDR_TO_V4L2SPARM(pic_order_cnt_bit_size);
-#undef SHDR_TO_V4L2SPARM
-
-#define SET_V4L2_SPARM_FLAG_IF(cond, flag) \
-  v4l2_slice_param.flags |= ((slice_hdr->cond) ? (flag) : 0)
-  SET_V4L2_SPARM_FLAG_IF(field_pic_flag, V4L2_SLICE_FLAG_FIELD_PIC);
-  SET_V4L2_SPARM_FLAG_IF(bottom_field_flag, V4L2_SLICE_FLAG_BOTTOM_FIELD);
-  SET_V4L2_SPARM_FLAG_IF(direct_spatial_mv_pred_flag,
-                         V4L2_SLICE_FLAG_DIRECT_SPATIAL_MV_PRED);
-  SET_V4L2_SPARM_FLAG_IF(sp_for_switch_flag, V4L2_SLICE_FLAG_SP_FOR_SWITCH);
-#undef SET_V4L2_SPARM_FLAG_IF
-
-  struct v4l2_h264_pred_weight_table* pred_weight_table =
-      &v4l2_slice_param.pred_weight_table;
-
-  if (((slice_hdr->IsPSlice() || slice_hdr->IsSPSlice()) &&
-       pps->weighted_pred_flag) ||
-      (slice_hdr->IsBSlice() && pps->weighted_bipred_idc == 1)) {
-    pred_weight_table->luma_log2_weight_denom =
-        slice_hdr->luma_log2_weight_denom;
-    pred_weight_table->chroma_log2_weight_denom =
-        slice_hdr->chroma_log2_weight_denom;
-
-    struct v4l2_h264_weight_factors* factorsl0 =
-        &pred_weight_table->weight_factors[0];
-
-    for (int i = 0; i < 32; ++i) {
-      factorsl0->luma_weight[i] =
-          slice_hdr->pred_weight_table_l0.luma_weight[i];
-      factorsl0->luma_offset[i] =
-          slice_hdr->pred_weight_table_l0.luma_offset[i];
-
-      for (int j = 0; j < 2; ++j) {
-        factorsl0->chroma_weight[i][j] =
-            slice_hdr->pred_weight_table_l0.chroma_weight[i][j];
-        factorsl0->chroma_offset[i][j] =
-            slice_hdr->pred_weight_table_l0.chroma_offset[i][j];
-      }
-    }
-
-    if (slice_hdr->IsBSlice()) {
-      struct v4l2_h264_weight_factors* factorsl1 =
-          &pred_weight_table->weight_factors[1];
-
-      for (int i = 0; i < 32; ++i) {
-        factorsl1->luma_weight[i] =
-            slice_hdr->pred_weight_table_l1.luma_weight[i];
-        factorsl1->luma_offset[i] =
-            slice_hdr->pred_weight_table_l1.luma_offset[i];
-
-        for (int j = 0; j < 2; ++j) {
-          factorsl1->chroma_weight[i][j] =
-              slice_hdr->pred_weight_table_l1.chroma_weight[i][j];
-          factorsl1->chroma_offset[i][j] =
-              slice_hdr->pred_weight_table_l1.chroma_offset[i][j];
-        }
-      }
-    }
-  }
-
-  H264PictureListToDPBIndicesList(ref_pic_list0,
-                                  v4l2_slice_param.ref_pic_list0);
-  H264PictureListToDPBIndicesList(ref_pic_list1,
-                                  v4l2_slice_param.ref_pic_list1);
-
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      H264PictureToV4L2DecodeSurface(pic);
-
-  v4l2_decode_param_.nal_ref_idc = slice_hdr->nal_ref_idc;
-
-  // TODO(posciak): Don't add start code back here, but have it passed from
-  // the parser.
-  size_t data_copy_size = size + 3;
-  std::unique_ptr<uint8_t[]> data_copy(new uint8_t[data_copy_size]);
-  memset(data_copy.get(), 0, data_copy_size);
-  data_copy[2] = 0x01;
-  memcpy(data_copy.get() + 3, data, size);
-  return v4l2_dec_->SubmitSlice(dec_surface->input_record(), data_copy.get(),
-                                data_copy_size);
-}
-
-bool V4L2SliceVideoDecodeAccelerator::SubmitSlice(int index,
-                                                  const uint8_t* data,
-                                                  size_t size) {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  InputRecord& input_record = input_buffer_map_[index];
-
-  if (input_record.bytes_used + size > input_record.length) {
-    VLOGF(1) << "Input buffer too small";
-    return false;
-  }
-
-  memcpy(static_cast<uint8_t*>(input_record.address) + input_record.bytes_used,
-         data, size);
-  input_record.bytes_used += size;
-
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::SubmitExtControls(
-    struct v4l2_ext_controls_custom* ext_ctrls) {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_GT(ext_ctrls->config_store, 0u);
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_S_EXT_CTRLS, ext_ctrls);
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::GetExtControls(
-    struct v4l2_ext_controls_custom* ext_ctrls) {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_GT(ext_ctrls->config_store, 0u);
-  IOCTL_OR_ERROR_RETURN_FALSE(VIDIOC_G_EXT_CTRLS, ext_ctrls);
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::IsCtrlExposed(uint32_t ctrl_id) {
-  struct v4l2_queryctrl query_ctrl;
-  memset(&query_ctrl, 0, sizeof(query_ctrl));
-  query_ctrl.id = ctrl_id;
-
-  return (device_->Ioctl(VIDIOC_QUERYCTRL, &query_ctrl) == 0);
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::SubmitDecode(
-    const scoped_refptr<H264Picture>& pic) {
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      H264PictureToV4L2DecodeSurface(pic);
-
-  v4l2_decode_param_.num_slices = num_slices_;
-  v4l2_decode_param_.idr_pic_flag = pic->idr;
-  v4l2_decode_param_.top_field_order_cnt = pic->top_field_order_cnt;
-  v4l2_decode_param_.bottom_field_order_cnt = pic->bottom_field_order_cnt;
-
-  struct v4l2_ext_control_custom ctrl;
-  std::vector<struct v4l2_ext_control_custom> ctrls;
-
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_H264_SLICE_PARAM;
-  ctrl.size = sizeof(v4l2_slice_params_);
-  ctrl.p_h264_slice_param = v4l2_slice_params_;
-  ctrls.push_back(ctrl);
-
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_H264_DECODE_PARAM;
-  ctrl.size = sizeof(v4l2_decode_param_);
-  ctrl.p_h264_decode_param = &v4l2_decode_param_;
-  ctrls.push_back(ctrl);
-
-  struct v4l2_ext_controls_custom ext_ctrls;
-  memset(&ext_ctrls, 0, sizeof(ext_ctrls));
-  ext_ctrls.count = ctrls.size();
-  ext_ctrls.controls = &ctrls[0];
-  ext_ctrls.config_store = dec_surface->config_store();
-  if (!v4l2_dec_->SubmitExtControls(&ext_ctrls))
-    return false;
-
-  Reset();
-
-  v4l2_dec_->DecodeSurface(dec_surface);
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::OutputPicture(
-    const scoped_refptr<H264Picture>& pic) {
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      H264PictureToV4L2DecodeSurface(pic);
-  dec_surface->set_visible_rect(pic->visible_rect);
-  v4l2_dec_->SurfaceReady(dec_surface);
-  return true;
-}
-
-void V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::Reset() {
-  num_slices_ = 0;
-  memset(&v4l2_decode_param_, 0, sizeof(v4l2_decode_param_));
-  memset(&v4l2_slice_params_, 0, sizeof(v4l2_slice_params_));
-}
-
-scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-V4L2SliceVideoDecodeAccelerator::V4L2H264Accelerator::
-    H264PictureToV4L2DecodeSurface(const scoped_refptr<H264Picture>& pic) {
-  V4L2H264Picture* v4l2_pic = pic->AsV4L2H264Picture();
-  CHECK(v4l2_pic);
-  return v4l2_pic->dec_surface();
-}
-
-V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::V4L2VP8Accelerator(
-    V4L2SliceVideoDecodeAccelerator* v4l2_dec)
-    : v4l2_dec_(v4l2_dec) {
-  DCHECK(v4l2_dec_);
-}
-
-V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::~V4L2VP8Accelerator() {}
-
-scoped_refptr<VP8Picture>
-V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::CreateVP8Picture() {
-  scoped_refptr<V4L2DecodeSurface> dec_surface = v4l2_dec_->CreateSurface();
-  if (!dec_surface)
-    return nullptr;
-
-  return new V4L2VP8Picture(dec_surface);
-}
-
-#define ARRAY_MEMCPY_CHECKED(to, from)                               \
-  do {                                                               \
-    static_assert(sizeof(to) == sizeof(from),                        \
-                  #from " and " #to " arrays must be of same size"); \
-    memcpy(to, from, sizeof(to));                                    \
-  } while (0)
-
-static void FillV4L2SegmentationHeader(
-    const Vp8SegmentationHeader& vp8_sgmnt_hdr,
-    struct v4l2_vp8_sgmnt_hdr* v4l2_sgmnt_hdr) {
-#define SET_V4L2_SGMNT_HDR_FLAG_IF(cond, flag) \
-  v4l2_sgmnt_hdr->flags |= ((vp8_sgmnt_hdr.cond) ? (flag) : 0)
-  SET_V4L2_SGMNT_HDR_FLAG_IF(segmentation_enabled,
-                             V4L2_VP8_SEGMNT_HDR_FLAG_ENABLED);
-  SET_V4L2_SGMNT_HDR_FLAG_IF(update_mb_segmentation_map,
-                             V4L2_VP8_SEGMNT_HDR_FLAG_UPDATE_MAP);
-  SET_V4L2_SGMNT_HDR_FLAG_IF(update_segment_feature_data,
-                             V4L2_VP8_SEGMNT_HDR_FLAG_UPDATE_FEATURE_DATA);
-#undef SET_V4L2_SPARM_FLAG_IF
-  v4l2_sgmnt_hdr->segment_feature_mode = vp8_sgmnt_hdr.segment_feature_mode;
-
-  ARRAY_MEMCPY_CHECKED(v4l2_sgmnt_hdr->quant_update,
-                       vp8_sgmnt_hdr.quantizer_update_value);
-  ARRAY_MEMCPY_CHECKED(v4l2_sgmnt_hdr->lf_update,
-                       vp8_sgmnt_hdr.lf_update_value);
-  ARRAY_MEMCPY_CHECKED(v4l2_sgmnt_hdr->segment_probs,
-                       vp8_sgmnt_hdr.segment_prob);
-}
-
-static void FillV4L2LoopfilterHeader(
-    const Vp8LoopFilterHeader& vp8_loopfilter_hdr,
-    struct v4l2_vp8_loopfilter_hdr* v4l2_lf_hdr) {
-#define SET_V4L2_LF_HDR_FLAG_IF(cond, flag) \
-  v4l2_lf_hdr->flags |= ((vp8_loopfilter_hdr.cond) ? (flag) : 0)
-  SET_V4L2_LF_HDR_FLAG_IF(loop_filter_adj_enable, V4L2_VP8_LF_HDR_ADJ_ENABLE);
-  SET_V4L2_LF_HDR_FLAG_IF(mode_ref_lf_delta_update,
-                          V4L2_VP8_LF_HDR_DELTA_UPDATE);
-#undef SET_V4L2_SGMNT_HDR_FLAG_IF
-
-#define LF_HDR_TO_V4L2_LF_HDR(a) v4l2_lf_hdr->a = vp8_loopfilter_hdr.a;
-  LF_HDR_TO_V4L2_LF_HDR(type);
-  LF_HDR_TO_V4L2_LF_HDR(level);
-  LF_HDR_TO_V4L2_LF_HDR(sharpness_level);
-#undef LF_HDR_TO_V4L2_LF_HDR
-
-  ARRAY_MEMCPY_CHECKED(v4l2_lf_hdr->ref_frm_delta_magnitude,
-                       vp8_loopfilter_hdr.ref_frame_delta);
-  ARRAY_MEMCPY_CHECKED(v4l2_lf_hdr->mb_mode_delta_magnitude,
-                       vp8_loopfilter_hdr.mb_mode_delta);
-}
-
-static void FillV4L2QuantizationHeader(
-    const Vp8QuantizationHeader& vp8_quant_hdr,
-    struct v4l2_vp8_quantization_hdr* v4l2_quant_hdr) {
-  v4l2_quant_hdr->y_ac_qi = vp8_quant_hdr.y_ac_qi;
-  v4l2_quant_hdr->y_dc_delta = vp8_quant_hdr.y_dc_delta;
-  v4l2_quant_hdr->y2_dc_delta = vp8_quant_hdr.y2_dc_delta;
-  v4l2_quant_hdr->y2_ac_delta = vp8_quant_hdr.y2_ac_delta;
-  v4l2_quant_hdr->uv_dc_delta = vp8_quant_hdr.uv_dc_delta;
-  v4l2_quant_hdr->uv_ac_delta = vp8_quant_hdr.uv_ac_delta;
-}
-
-static void FillV4L2Vp8EntropyHeader(
-    const Vp8EntropyHeader& vp8_entropy_hdr,
-    struct v4l2_vp8_entropy_hdr* v4l2_entropy_hdr) {
-  ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->coeff_probs,
-                       vp8_entropy_hdr.coeff_probs);
-  ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->y_mode_probs,
-                       vp8_entropy_hdr.y_mode_probs);
-  ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->uv_mode_probs,
-                       vp8_entropy_hdr.uv_mode_probs);
-  ARRAY_MEMCPY_CHECKED(v4l2_entropy_hdr->mv_probs, vp8_entropy_hdr.mv_probs);
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::SubmitDecode(
-    const scoped_refptr<VP8Picture>& pic,
-    const Vp8FrameHeader* frame_hdr,
-    const scoped_refptr<VP8Picture>& last_frame,
-    const scoped_refptr<VP8Picture>& golden_frame,
-    const scoped_refptr<VP8Picture>& alt_frame) {
-  struct v4l2_ctrl_vp8_frame_hdr v4l2_frame_hdr;
-  memset(&v4l2_frame_hdr, 0, sizeof(v4l2_frame_hdr));
-
-#define FHDR_TO_V4L2_FHDR(a) v4l2_frame_hdr.a = frame_hdr->a
-  FHDR_TO_V4L2_FHDR(key_frame);
-  FHDR_TO_V4L2_FHDR(version);
-  FHDR_TO_V4L2_FHDR(width);
-  FHDR_TO_V4L2_FHDR(horizontal_scale);
-  FHDR_TO_V4L2_FHDR(height);
-  FHDR_TO_V4L2_FHDR(vertical_scale);
-  FHDR_TO_V4L2_FHDR(sign_bias_golden);
-  FHDR_TO_V4L2_FHDR(sign_bias_alternate);
-  FHDR_TO_V4L2_FHDR(prob_skip_false);
-  FHDR_TO_V4L2_FHDR(prob_intra);
-  FHDR_TO_V4L2_FHDR(prob_last);
-  FHDR_TO_V4L2_FHDR(prob_gf);
-  FHDR_TO_V4L2_FHDR(bool_dec_range);
-  FHDR_TO_V4L2_FHDR(bool_dec_value);
-  FHDR_TO_V4L2_FHDR(bool_dec_count);
-#undef FHDR_TO_V4L2_FHDR
-
-#define SET_V4L2_FRM_HDR_FLAG_IF(cond, flag) \
-  v4l2_frame_hdr.flags |= ((frame_hdr->cond) ? (flag) : 0)
-  SET_V4L2_FRM_HDR_FLAG_IF(is_experimental,
-                           V4L2_VP8_FRAME_HDR_FLAG_EXPERIMENTAL);
-  SET_V4L2_FRM_HDR_FLAG_IF(show_frame, V4L2_VP8_FRAME_HDR_FLAG_SHOW_FRAME);
-  SET_V4L2_FRM_HDR_FLAG_IF(mb_no_skip_coeff,
-                           V4L2_VP8_FRAME_HDR_FLAG_MB_NO_SKIP_COEFF);
-#undef SET_V4L2_FRM_HDR_FLAG_IF
-
-  FillV4L2SegmentationHeader(frame_hdr->segmentation_hdr,
-                             &v4l2_frame_hdr.sgmnt_hdr);
-
-  FillV4L2LoopfilterHeader(frame_hdr->loopfilter_hdr, &v4l2_frame_hdr.lf_hdr);
-
-  FillV4L2QuantizationHeader(frame_hdr->quantization_hdr,
-                             &v4l2_frame_hdr.quant_hdr);
-
-  FillV4L2Vp8EntropyHeader(frame_hdr->entropy_hdr, &v4l2_frame_hdr.entropy_hdr);
-
-  v4l2_frame_hdr.first_part_size =
-      base::checked_cast<__u32>(frame_hdr->first_part_size);
-  v4l2_frame_hdr.first_part_offset =
-      base::checked_cast<__u32>(frame_hdr->first_part_offset);
-  v4l2_frame_hdr.macroblock_bit_offset =
-      base::checked_cast<__u32>(frame_hdr->macroblock_bit_offset);
-  v4l2_frame_hdr.num_dct_parts = frame_hdr->num_of_dct_partitions;
-
-  static_assert(arraysize(v4l2_frame_hdr.dct_part_sizes) ==
-                    arraysize(frame_hdr->dct_partition_sizes),
-                "DCT partition size arrays must have equal number of elements");
-  for (size_t i = 0; i < frame_hdr->num_of_dct_partitions &&
-                     i < arraysize(v4l2_frame_hdr.dct_part_sizes);
-       ++i)
-    v4l2_frame_hdr.dct_part_sizes[i] = frame_hdr->dct_partition_sizes[i];
-
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      VP8PictureToV4L2DecodeSurface(pic);
-  std::vector<scoped_refptr<V4L2DecodeSurface>> ref_surfaces;
-
-  if (last_frame) {
-    scoped_refptr<V4L2DecodeSurface> last_frame_surface =
-        VP8PictureToV4L2DecodeSurface(last_frame);
-    v4l2_frame_hdr.last_frame = last_frame_surface->output_record();
-    ref_surfaces.push_back(last_frame_surface);
-  } else {
-    v4l2_frame_hdr.last_frame = VIDEO_MAX_FRAME;
-  }
-
-  if (golden_frame) {
-    scoped_refptr<V4L2DecodeSurface> golden_frame_surface =
-        VP8PictureToV4L2DecodeSurface(golden_frame);
-    v4l2_frame_hdr.golden_frame = golden_frame_surface->output_record();
-    ref_surfaces.push_back(golden_frame_surface);
-  } else {
-    v4l2_frame_hdr.golden_frame = VIDEO_MAX_FRAME;
-  }
-
-  if (alt_frame) {
-    scoped_refptr<V4L2DecodeSurface> alt_frame_surface =
-        VP8PictureToV4L2DecodeSurface(alt_frame);
-    v4l2_frame_hdr.alt_frame = alt_frame_surface->output_record();
-    ref_surfaces.push_back(alt_frame_surface);
-  } else {
-    v4l2_frame_hdr.alt_frame = VIDEO_MAX_FRAME;
-  }
-
-  struct v4l2_ext_control_custom ctrl;
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_VP8_FRAME_HDR;
-  ctrl.size = sizeof(v4l2_frame_hdr);
-  ctrl.p_vp8_frame_hdr = &v4l2_frame_hdr;
-
-  struct v4l2_ext_controls_custom ext_ctrls;
-  memset(&ext_ctrls, 0, sizeof(ext_ctrls));
-  ext_ctrls.count = 1;
-  ext_ctrls.controls = &ctrl;
-  ext_ctrls.config_store = dec_surface->config_store();
-
-  if (!v4l2_dec_->SubmitExtControls(&ext_ctrls))
-    return false;
-
-  dec_surface->SetReferenceSurfaces(ref_surfaces);
-
-  if (!v4l2_dec_->SubmitSlice(dec_surface->input_record(), frame_hdr->data,
-                              frame_hdr->frame_size))
-    return false;
-
-  v4l2_dec_->DecodeSurface(dec_surface);
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::OutputPicture(
-    const scoped_refptr<VP8Picture>& pic) {
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      VP8PictureToV4L2DecodeSurface(pic);
-  dec_surface->set_visible_rect(pic->visible_rect);
-  v4l2_dec_->SurfaceReady(dec_surface);
-  return true;
-}
-
-scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-V4L2SliceVideoDecodeAccelerator::V4L2VP8Accelerator::
-    VP8PictureToV4L2DecodeSurface(const scoped_refptr<VP8Picture>& pic) {
-  V4L2VP8Picture* v4l2_pic = pic->AsV4L2VP8Picture();
-  CHECK(v4l2_pic);
-  return v4l2_pic->dec_surface();
-}
-
-V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::V4L2VP9Accelerator(
-    V4L2SliceVideoDecodeAccelerator* v4l2_dec)
-    : v4l2_dec_(v4l2_dec) {
-  DCHECK(v4l2_dec_);
-
-  device_needs_frame_context_ =
-      v4l2_dec_->IsCtrlExposed(V4L2_CID_MPEG_VIDEO_VP9_ENTROPY);
-  DVLOG_IF(1, device_needs_frame_context_)
-      << "Device requires frame context parsing";
-}
-
-V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::~V4L2VP9Accelerator() {}
-
-scoped_refptr<VP9Picture>
-V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::CreateVP9Picture() {
-  scoped_refptr<V4L2DecodeSurface> dec_surface = v4l2_dec_->CreateSurface();
-  if (!dec_surface)
-    return nullptr;
-
-  return new V4L2VP9Picture(dec_surface);
-}
-
-static void FillV4L2VP9LoopFilterParams(
-    const Vp9LoopFilterParams& vp9_lf_params,
-    struct v4l2_vp9_loop_filter_params* v4l2_lf_params) {
-#define SET_LF_PARAMS_FLAG_IF(cond, flag) \
-  v4l2_lf_params->flags |= ((vp9_lf_params.cond) ? (flag) : 0)
-  SET_LF_PARAMS_FLAG_IF(delta_enabled, V4L2_VP9_LOOP_FLTR_FLAG_DELTA_ENABLED);
-  SET_LF_PARAMS_FLAG_IF(delta_update, V4L2_VP9_LOOP_FLTR_FLAG_DELTA_UPDATE);
-#undef SET_LF_PARAMS_FLAG_IF
-
-  v4l2_lf_params->level = vp9_lf_params.level;
-  v4l2_lf_params->sharpness = vp9_lf_params.sharpness;
-
-  ARRAY_MEMCPY_CHECKED(v4l2_lf_params->deltas, vp9_lf_params.ref_deltas);
-  ARRAY_MEMCPY_CHECKED(v4l2_lf_params->mode_deltas, vp9_lf_params.mode_deltas);
-  ARRAY_MEMCPY_CHECKED(v4l2_lf_params->lvl_lookup, vp9_lf_params.lvl);
-}
-
-static void FillV4L2VP9QuantizationParams(
-    const Vp9QuantizationParams& vp9_quant_params,
-    struct v4l2_vp9_quantization_params* v4l2_q_params) {
-#define SET_Q_PARAMS_FLAG_IF(cond, flag) \
-  v4l2_q_params->flags |= ((vp9_quant_params.cond) ? (flag) : 0)
-  SET_Q_PARAMS_FLAG_IF(IsLossless(), V4L2_VP9_QUANT_PARAMS_FLAG_LOSSLESS);
-#undef SET_Q_PARAMS_FLAG_IF
-
-#define Q_PARAMS_TO_V4L2_Q_PARAMS(a) v4l2_q_params->a = vp9_quant_params.a
-  Q_PARAMS_TO_V4L2_Q_PARAMS(base_q_idx);
-  Q_PARAMS_TO_V4L2_Q_PARAMS(delta_q_y_dc);
-  Q_PARAMS_TO_V4L2_Q_PARAMS(delta_q_uv_dc);
-  Q_PARAMS_TO_V4L2_Q_PARAMS(delta_q_uv_ac);
-#undef Q_PARAMS_TO_V4L2_Q_PARAMS
-}
-
-static void FillV4L2VP9SegmentationParams(
-    const Vp9SegmentationParams& vp9_segm_params,
-    struct v4l2_vp9_segmentation_params* v4l2_segm_params) {
-#define SET_SEG_PARAMS_FLAG_IF(cond, flag) \
-  v4l2_segm_params->flags |= ((vp9_segm_params.cond) ? (flag) : 0)
-  SET_SEG_PARAMS_FLAG_IF(enabled, V4L2_VP9_SGMNT_PARAM_FLAG_ENABLED);
-  SET_SEG_PARAMS_FLAG_IF(update_map, V4L2_VP9_SGMNT_PARAM_FLAG_UPDATE_MAP);
-  SET_SEG_PARAMS_FLAG_IF(temporal_update,
-                         V4L2_VP9_SGMNT_PARAM_FLAG_TEMPORAL_UPDATE);
-  SET_SEG_PARAMS_FLAG_IF(update_data, V4L2_VP9_SGMNT_PARAM_FLAG_UPDATE_DATA);
-  SET_SEG_PARAMS_FLAG_IF(abs_or_delta_update,
-                         V4L2_VP9_SGMNT_PARAM_FLAG_ABS_OR_DELTA_UPDATE);
-#undef SET_SEG_PARAMS_FLAG_IF
-
-  ARRAY_MEMCPY_CHECKED(v4l2_segm_params->tree_probs,
-                       vp9_segm_params.tree_probs);
-  ARRAY_MEMCPY_CHECKED(v4l2_segm_params->pred_probs,
-                       vp9_segm_params.pred_probs);
-  ARRAY_MEMCPY_CHECKED(v4l2_segm_params->feature_data,
-                       vp9_segm_params.feature_data);
-
-  static_assert(arraysize(v4l2_segm_params->feature_enabled) ==
-                        arraysize(vp9_segm_params.feature_enabled) &&
-                    arraysize(v4l2_segm_params->feature_enabled[0]) ==
-                        arraysize(vp9_segm_params.feature_enabled[0]),
-                "feature_enabled arrays must be of same size");
-  for (size_t i = 0; i < arraysize(v4l2_segm_params->feature_enabled); ++i) {
-    for (size_t j = 0; j < arraysize(v4l2_segm_params->feature_enabled[i]);
-         ++j) {
-      v4l2_segm_params->feature_enabled[i][j] =
-          vp9_segm_params.feature_enabled[i][j];
-    }
-  }
-}
-
-static void FillV4L2Vp9EntropyContext(
-    const Vp9FrameContext& vp9_frame_ctx,
-    struct v4l2_vp9_entropy_ctx* v4l2_entropy_ctx) {
-#define ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(a) \
-  ARRAY_MEMCPY_CHECKED(v4l2_entropy_ctx->a, vp9_frame_ctx.a)
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(tx_probs_8x8);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(tx_probs_16x16);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(tx_probs_32x32);
-
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(coef_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(skip_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(inter_mode_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(interp_filter_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(is_inter_prob);
-
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(comp_mode_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(single_ref_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(comp_ref_prob);
-
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(y_mode_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(uv_mode_probs);
-
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(partition_probs);
-
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_joint_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_sign_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_class_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_class0_bit_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_bits_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_class0_fr_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_fr_probs);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_class0_hp_prob);
-  ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR(mv_hp_prob);
-#undef ARRAY_MEMCPY_CHECKED_FRM_CTX_TO_V4L2_ENTR
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::SubmitDecode(
-    const scoped_refptr<VP9Picture>& pic,
-    const Vp9SegmentationParams& segm_params,
-    const Vp9LoopFilterParams& lf_params,
-    const std::vector<scoped_refptr<VP9Picture>>& ref_pictures,
-    const base::Closure& done_cb) {
-  const Vp9FrameHeader* frame_hdr = pic->frame_hdr.get();
-  DCHECK(frame_hdr);
-
-  struct v4l2_ctrl_vp9_frame_hdr v4l2_frame_hdr;
-  memset(&v4l2_frame_hdr, 0, sizeof(v4l2_frame_hdr));
-
-#define FHDR_TO_V4L2_FHDR(a) v4l2_frame_hdr.a = frame_hdr->a
-  FHDR_TO_V4L2_FHDR(profile);
-  FHDR_TO_V4L2_FHDR(frame_type);
-
-  FHDR_TO_V4L2_FHDR(bit_depth);
-  FHDR_TO_V4L2_FHDR(color_range);
-  FHDR_TO_V4L2_FHDR(subsampling_x);
-  FHDR_TO_V4L2_FHDR(subsampling_y);
-
-  FHDR_TO_V4L2_FHDR(frame_width);
-  FHDR_TO_V4L2_FHDR(frame_height);
-  FHDR_TO_V4L2_FHDR(render_width);
-  FHDR_TO_V4L2_FHDR(render_height);
-
-  FHDR_TO_V4L2_FHDR(reset_frame_context);
-
-  FHDR_TO_V4L2_FHDR(interpolation_filter);
-  FHDR_TO_V4L2_FHDR(frame_context_idx);
-
-  FHDR_TO_V4L2_FHDR(tile_cols_log2);
-  FHDR_TO_V4L2_FHDR(tile_rows_log2);
-
-  FHDR_TO_V4L2_FHDR(header_size_in_bytes);
-#undef FHDR_TO_V4L2_FHDR
-  v4l2_frame_hdr.color_space = static_cast<uint8_t>(frame_hdr->color_space);
-
-  FillV4L2VP9QuantizationParams(frame_hdr->quant_params,
-                                &v4l2_frame_hdr.quant_params);
-
-#define SET_V4L2_FRM_HDR_FLAG_IF(cond, flag) \
-  v4l2_frame_hdr.flags |= ((frame_hdr->cond) ? (flag) : 0)
-  SET_V4L2_FRM_HDR_FLAG_IF(show_frame, V4L2_VP9_FRAME_HDR_FLAG_SHOW_FRAME);
-  SET_V4L2_FRM_HDR_FLAG_IF(error_resilient_mode,
-                           V4L2_VP9_FRAME_HDR_FLAG_ERR_RES);
-  SET_V4L2_FRM_HDR_FLAG_IF(intra_only, V4L2_VP9_FRAME_HDR_FLAG_FRAME_INTRA);
-  SET_V4L2_FRM_HDR_FLAG_IF(allow_high_precision_mv,
-                           V4L2_VP9_FRAME_HDR_ALLOW_HIGH_PREC_MV);
-  SET_V4L2_FRM_HDR_FLAG_IF(refresh_frame_context,
-                           V4L2_VP9_FRAME_HDR_REFRESH_FRAME_CTX);
-  SET_V4L2_FRM_HDR_FLAG_IF(frame_parallel_decoding_mode,
-                           V4L2_VP9_FRAME_HDR_PARALLEL_DEC_MODE);
-#undef SET_V4L2_FRM_HDR_FLAG_IF
-
-  FillV4L2VP9LoopFilterParams(lf_params, &v4l2_frame_hdr.lf_params);
-  FillV4L2VP9SegmentationParams(segm_params, &v4l2_frame_hdr.sgmnt_params);
-
-  std::vector<struct v4l2_ext_control_custom> ctrls;
-
-  struct v4l2_ext_control_custom ctrl;
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_VP9_FRAME_HDR;
-  ctrl.size = sizeof(v4l2_frame_hdr);
-  ctrl.p_vp9_frame_hdr = &v4l2_frame_hdr;
-  ctrls.push_back(ctrl);
-
-  struct v4l2_ctrl_vp9_decode_param v4l2_decode_param;
-  memset(&v4l2_decode_param, 0, sizeof(v4l2_decode_param));
-  DCHECK_EQ(ref_pictures.size(), arraysize(v4l2_decode_param.ref_frames));
-
-  std::vector<scoped_refptr<V4L2DecodeSurface>> ref_surfaces;
-  for (size_t i = 0; i < ref_pictures.size(); ++i) {
-    if (ref_pictures[i]) {
-      scoped_refptr<V4L2DecodeSurface> ref_surface =
-          VP9PictureToV4L2DecodeSurface(ref_pictures[i]);
-
-      v4l2_decode_param.ref_frames[i] = ref_surface->output_record();
-      ref_surfaces.push_back(ref_surface);
-    } else {
-      v4l2_decode_param.ref_frames[i] = VIDEO_MAX_FRAME;
-    }
-  }
-
-  static_assert(arraysize(v4l2_decode_param.active_ref_frames) ==
-                    arraysize(frame_hdr->ref_frame_idx),
-                "active reference frame array sizes mismatch");
-
-  for (size_t i = 0; i < arraysize(frame_hdr->ref_frame_idx); ++i) {
-    uint8_t idx = frame_hdr->ref_frame_idx[i];
-    if (idx >= ref_pictures.size())
-      return false;
-
-    struct v4l2_vp9_reference_frame* v4l2_ref_frame =
-        &v4l2_decode_param.active_ref_frames[i];
-
-    scoped_refptr<VP9Picture> ref_pic = ref_pictures[idx];
-    if (ref_pic) {
-      scoped_refptr<V4L2DecodeSurface> ref_surface =
-          VP9PictureToV4L2DecodeSurface(ref_pic);
-      v4l2_ref_frame->buf_index = ref_surface->output_record();
-#define REF_TO_V4L2_REF(a) v4l2_ref_frame->a = ref_pic->frame_hdr->a
-      REF_TO_V4L2_REF(frame_width);
-      REF_TO_V4L2_REF(frame_height);
-      REF_TO_V4L2_REF(bit_depth);
-      REF_TO_V4L2_REF(subsampling_x);
-      REF_TO_V4L2_REF(subsampling_y);
-#undef REF_TO_V4L2_REF
-    } else {
-      v4l2_ref_frame->buf_index = VIDEO_MAX_FRAME;
-    }
-  }
-
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_VP9_DECODE_PARAM;
-  ctrl.size = sizeof(v4l2_decode_param);
-  ctrl.p_vp9_decode_param = &v4l2_decode_param;
-  ctrls.push_back(ctrl);
-
-  // Defined outside of the if() clause below as it must remain valid until
-  // the call to SubmitExtControls().
-  struct v4l2_ctrl_vp9_entropy v4l2_entropy;
-  if (device_needs_frame_context_) {
-    memset(&v4l2_entropy, 0, sizeof(v4l2_entropy));
-    FillV4L2Vp9EntropyContext(frame_hdr->initial_frame_context,
-                              &v4l2_entropy.initial_entropy_ctx);
-    FillV4L2Vp9EntropyContext(frame_hdr->frame_context,
-                              &v4l2_entropy.current_entropy_ctx);
-    v4l2_entropy.tx_mode = frame_hdr->compressed_header.tx_mode;
-    v4l2_entropy.reference_mode = frame_hdr->compressed_header.reference_mode;
-
-    memset(&ctrl, 0, sizeof(ctrl));
-    ctrl.id = V4L2_CID_MPEG_VIDEO_VP9_ENTROPY;
-    ctrl.size = sizeof(v4l2_entropy);
-    ctrl.p_vp9_entropy = &v4l2_entropy;
-    ctrls.push_back(ctrl);
-  }
-
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      VP9PictureToV4L2DecodeSurface(pic);
-
-  struct v4l2_ext_controls_custom ext_ctrls;
-  memset(&ext_ctrls, 0, sizeof(ext_ctrls));
-  ext_ctrls.count = ctrls.size();
-  ext_ctrls.controls = &ctrls[0];
-  ext_ctrls.config_store = dec_surface->config_store();
-  if (!v4l2_dec_->SubmitExtControls(&ext_ctrls))
-    return false;
-
-  dec_surface->SetReferenceSurfaces(ref_surfaces);
-  dec_surface->SetDecodeDoneCallback(done_cb);
-
-  if (!v4l2_dec_->SubmitSlice(dec_surface->input_record(), frame_hdr->data,
-                              frame_hdr->frame_size))
-    return false;
-
-  v4l2_dec_->DecodeSurface(dec_surface);
-  return true;
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::OutputPicture(
-    const scoped_refptr<VP9Picture>& pic) {
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      VP9PictureToV4L2DecodeSurface(pic);
-  dec_surface->set_visible_rect(pic->visible_rect);
-  v4l2_dec_->SurfaceReady(dec_surface);
-  return true;
-}
-
-static void FillVp9FrameContext(struct v4l2_vp9_entropy_ctx& v4l2_entropy_ctx,
-                                Vp9FrameContext* vp9_frame_ctx) {
-#define ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(a) \
-  ARRAY_MEMCPY_CHECKED(vp9_frame_ctx->a, v4l2_entropy_ctx.a)
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(tx_probs_8x8);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(tx_probs_16x16);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(tx_probs_32x32);
-
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(coef_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(skip_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(inter_mode_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(interp_filter_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(is_inter_prob);
-
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(comp_mode_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(single_ref_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(comp_ref_prob);
-
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(y_mode_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(uv_mode_probs);
-
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(partition_probs);
-
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_joint_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_sign_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_class_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_class0_bit_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_bits_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_class0_fr_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_fr_probs);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_class0_hp_prob);
-  ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX(mv_hp_prob);
-#undef ARRAY_MEMCPY_CHECKED_V4L2_ENTR_TO_FRM_CTX
-}
-
-bool V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::GetFrameContext(
-    const scoped_refptr<VP9Picture>& pic,
-    Vp9FrameContext* frame_ctx) {
-  struct v4l2_ctrl_vp9_entropy v4l2_entropy;
-  memset(&v4l2_entropy, 0, sizeof(v4l2_entropy));
-
-  struct v4l2_ext_control_custom ctrl;
-  memset(&ctrl, 0, sizeof(ctrl));
-  ctrl.id = V4L2_CID_MPEG_VIDEO_VP9_ENTROPY;
-  ctrl.size = sizeof(v4l2_entropy);
-  ctrl.p_vp9_entropy = &v4l2_entropy;
-
-  scoped_refptr<V4L2DecodeSurface> dec_surface =
-      VP9PictureToV4L2DecodeSurface(pic);
-
-  struct v4l2_ext_controls_custom ext_ctrls;
-  memset(&ext_ctrls, 0, sizeof(ext_ctrls));
-  ext_ctrls.count = 1;
-  ext_ctrls.controls = &ctrl;
-  ext_ctrls.config_store = dec_surface->config_store();
-
-  if (!v4l2_dec_->GetExtControls(&ext_ctrls))
-    return false;
-
-  FillVp9FrameContext(v4l2_entropy.current_entropy_ctx, frame_ctx);
-  return true;
-}
-
-scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-V4L2SliceVideoDecodeAccelerator::V4L2VP9Accelerator::
-    VP9PictureToV4L2DecodeSurface(const scoped_refptr<VP9Picture>& pic) {
-  V4L2VP9Picture* v4l2_pic = pic->AsV4L2VP9Picture();
-  CHECK(v4l2_pic);
-  return v4l2_pic->dec_surface();
-}
-
-void V4L2SliceVideoDecodeAccelerator::DecodeSurface(
-    const scoped_refptr<V4L2DecodeSurface>& dec_surface) {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  DVLOGF(4) << "Submitting decode for surface: " << dec_surface->ToString();
-  Enqueue(dec_surface);
-}
-
-void V4L2SliceVideoDecodeAccelerator::SurfaceReady(
-    const scoped_refptr<V4L2DecodeSurface>& dec_surface) {
-  DVLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  decoder_display_queue_.push(dec_surface);
-  TryOutputSurfaces();
-}
-
-void V4L2SliceVideoDecodeAccelerator::TryOutputSurfaces() {
-  while (!decoder_display_queue_.empty()) {
-    scoped_refptr<V4L2DecodeSurface> dec_surface =
-        decoder_display_queue_.front();
-
-    if (!dec_surface->decoded())
-      break;
-
-    decoder_display_queue_.pop();
-    OutputSurface(dec_surface);
-  }
-}
-
-void V4L2SliceVideoDecodeAccelerator::OutputSurface(
-    const scoped_refptr<V4L2DecodeSurface>& dec_surface) {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-
-  OutputRecord& output_record =
-      output_buffer_map_[dec_surface->output_record()];
-
-  bool inserted =
-      surfaces_at_display_
-          .insert(std::make_pair(output_record.picture_id, dec_surface))
-          .second;
-  DCHECK(inserted);
-
-  DCHECK(!output_record.at_client);
-  DCHECK(!output_record.at_device);
-  DCHECK_NE(output_record.picture_id, -1);
-  output_record.at_client = true;
-
-  Picture picture(output_record.picture_id, dec_surface->bitstream_id(),
-                  dec_surface->visible_rect(), true /* allow_overlay */);
-  DVLOGF(4) << dec_surface->ToString()
-            << ", bitstream_id: " << picture.bitstream_buffer_id()
-            << ", picture_id: " << picture.picture_buffer_id()
-            << ", visible_rect: " << picture.visible_rect().ToString();
-  pending_picture_ready_.push(PictureRecord(output_record.cleared, picture));
-  SendPictureReady();
-  output_record.cleared = true;
-}
-
-scoped_refptr<V4L2SliceVideoDecodeAccelerator::V4L2DecodeSurface>
-V4L2SliceVideoDecodeAccelerator::CreateSurface() {
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_EQ(state_, kDecoding);
-
-  if (free_input_buffers_.empty() || free_output_buffers_.empty())
-    return nullptr;
-
-  int input = free_input_buffers_.front();
-  free_input_buffers_.pop_front();
-  int output = free_output_buffers_.front();
-  free_output_buffers_.pop_front();
-
-  InputRecord& input_record = input_buffer_map_[input];
-  DCHECK_EQ(input_record.bytes_used, 0u);
-  DCHECK_EQ(input_record.input_id, -1);
-  DCHECK(decoder_current_bitstream_buffer_ != nullptr);
-  input_record.input_id = decoder_current_bitstream_buffer_->input_id;
-
-  scoped_refptr<V4L2DecodeSurface> dec_surface = new V4L2DecodeSurface(
-      decoder_current_bitstream_buffer_->input_id, input, output,
-      base::Bind(&V4L2SliceVideoDecodeAccelerator::ReuseOutputBuffer,
-                 base::Unretained(this)));
-
-  DVLOGF(4) << "Created surface " << input << " -> " << output;
-  return dec_surface;
-}
-
-void V4L2SliceVideoDecodeAccelerator::SendPictureReady() {
-  DVLOGF(4);
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  bool send_now =
-      (decoder_resetting_ || decoder_flushing_ || surface_set_change_pending_);
-  while (!pending_picture_ready_.empty()) {
-    bool cleared = pending_picture_ready_.front().cleared;
-    const Picture& picture = pending_picture_ready_.front().picture;
-    if (cleared && picture_clearing_count_ == 0) {
-      DVLOGF(4) << "Posting picture ready to decode task runner for: "
-                << picture.picture_buffer_id();
-      // This picture is cleared. It can be posted to a thread different than
-      // the main GPU thread to reduce latency. This should be the case after
-      // all pictures are cleared at the beginning.
-      decode_task_runner_->PostTask(
-          FROM_HERE,
-          base::Bind(&Client::PictureReady, decode_client_, picture));
-      pending_picture_ready_.pop();
-    } else if (!cleared || send_now) {
-      DVLOGF(4) << "cleared=" << pending_picture_ready_.front().cleared
-                << ", decoder_resetting_=" << decoder_resetting_
-                << ", decoder_flushing_=" << decoder_flushing_
-                << ", surface_set_change_pending_="
-                << surface_set_change_pending_
-                << ", picture_clearing_count_=" << picture_clearing_count_;
-      DVLOGF(4) << "Posting picture ready to GPU for: "
-                << picture.picture_buffer_id();
-      // If the picture is not cleared, post it to the child thread because it
-      // has to be cleared in the child thread. A picture only needs to be
-      // cleared once. If the decoder is resetting or flushing or changing
-      // resolution, send all pictures to ensure PictureReady arrive before
-      // reset done, flush done, or picture dismissed.
-      child_task_runner_->PostTaskAndReply(
-          FROM_HERE, base::Bind(&Client::PictureReady, client_, picture),
-          // Unretained is safe. If Client::PictureReady gets to run, |this| is
-          // alive. Destroy() will wait the decode thread to finish.
-          base::Bind(&V4L2SliceVideoDecodeAccelerator::PictureCleared,
-                     base::Unretained(this)));
-      picture_clearing_count_++;
-      pending_picture_ready_.pop();
-    } else {
-      // This picture is cleared. But some pictures are about to be cleared on
-      // the child thread. To preserve the order, do not send this until those
-      // pictures are cleared.
-      break;
-    }
-  }
-}
-
-void V4L2SliceVideoDecodeAccelerator::PictureCleared() {
-  DVLOGF(4) << "clearing count=" << picture_clearing_count_;
-  DCHECK(decoder_thread_task_runner_->BelongsToCurrentThread());
-  DCHECK_GT(picture_clearing_count_, 0);
-  picture_clearing_count_--;
-  SendPictureReady();
-}
-
-bool V4L2SliceVideoDecodeAccelerator::TryToSetupDecodeOnSeparateThread(
-    const base::WeakPtr<Client>& decode_client,
-    const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner) {
-  decode_client_ = decode_client;
-  decode_task_runner_ = decode_task_runner;
-  return true;
-}
-
-// static
-VideoDecodeAccelerator::SupportedProfiles
-V4L2SliceVideoDecodeAccelerator::GetSupportedProfiles() {
-  scoped_refptr<V4L2Device> device(new V4L2Device());
-  if (!device)
-    return SupportedProfiles();
-
-  return device->GetSupportedDecodeProfiles(arraysize(supported_input_fourccs_),
-                                            supported_input_fourccs_);
-}
-
-}  // namespace media
diff --git a/vda/v4l2_slice_video_decode_accelerator.h b/vda/v4l2_slice_video_decode_accelerator.h
deleted file mode 100644
index a85dfa1..0000000
--- a/vda/v4l2_slice_video_decode_accelerator.h
+++ /dev/null
@@ -1,437 +0,0 @@
-// Copyright 2015 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-// Note: ported from Chromium commit head: 85fdf90
-
-#ifndef V4L2_SLICE_VIDEO_DECODE_ACCELERATOR_H_
-#define V4L2_SLICE_VIDEO_DECODE_ACCELERATOR_H_
-
-#include <stddef.h>
-#include <stdint.h>
-
-#include <memory>
-#include <queue>
-#include <utility>
-#include <vector>
-
-#include "base/macros.h"
-#include "base/memory/linked_ptr.h"
-#include "base/memory/ref_counted.h"
-#include "base/memory/weak_ptr.h"
-#include "base/synchronization/waitable_event.h"
-#include "base/threading/thread.h"
-#include "h264_decoder.h"
-#include "v4l2_device.h"
-#include "video_decode_accelerator.h"
-#include "videodev2_custom.h"
-#include "vp8_decoder.h"
-#include "vp9_decoder.h"
-
-namespace media {
-
-// An implementation of VideoDecodeAccelerator that utilizes the V4L2 slice
-// level codec API for decoding. The slice level API provides only a low-level
-// decoding functionality and requires userspace to provide support for parsing
-// the input stream and managing decoder state across frames.
-class V4L2SliceVideoDecodeAccelerator
-    : public VideoDecodeAccelerator {
- public:
-  class V4L2DecodeSurface;
-
-  V4L2SliceVideoDecodeAccelerator(
-      const scoped_refptr<V4L2Device>& device);
-  ~V4L2SliceVideoDecodeAccelerator() override;
-
-  // VideoDecodeAccelerator implementation.
-  bool Initialize(const Config& config, Client* client) override;
-  void Decode(const BitstreamBuffer& bitstream_buffer) override;
-  void AssignPictureBuffers(const std::vector<PictureBuffer>& buffers) override;
-  void ImportBufferForPicture(
-      int32_t picture_buffer_id,
-      VideoPixelFormat pixel_format,
-      const NativePixmapHandle& native_pixmap_handle) override;
-  void ReusePictureBuffer(int32_t picture_buffer_id) override;
-  void Flush() override;
-  void Reset() override;
-  void Destroy() override;
-  bool TryToSetupDecodeOnSeparateThread(
-      const base::WeakPtr<Client>& decode_client,
-      const scoped_refptr<base::SingleThreadTaskRunner>& decode_task_runner)
-      override;
-
-  static VideoDecodeAccelerator::SupportedProfiles GetSupportedProfiles();
-
- private:
-  class V4L2H264Accelerator;
-  class V4L2VP8Accelerator;
-  class V4L2VP9Accelerator;
-
-  // Record for input buffers.
-  struct InputRecord {
-    InputRecord();
-    int32_t input_id;
-    void* address;
-    size_t length;
-    size_t bytes_used;
-    bool at_device;
-  };
-
-  // Record for output buffers.
-  struct OutputRecord {
-    OutputRecord();
-    OutputRecord(OutputRecord&&) = default;
-    bool at_device;
-    bool at_client;
-    int32_t picture_id;
-    std::vector<base::ScopedFD> dmabuf_fds;
-    bool cleared;
-  };
-
-  // See http://crbug.com/255116.
-  // Input bitstream buffer size for up to 1080p streams.
-  const size_t kInputBufferMaxSizeFor1080p = 1024 * 1024;
-  // Input bitstream buffer size for up to 4k streams.
-  const size_t kInputBufferMaxSizeFor4k = 4 * kInputBufferMaxSizeFor1080p;
-  const size_t kNumInputBuffers = 16;
-
-  // Input format V4L2 fourccs this class supports.
-  static const uint32_t supported_input_fourccs_[];
-
-  //
-  // Below methods are used by accelerator implementations.
-  //
-  // Append slice data in |data| of size |size| to pending hardware
-  // input buffer with |index|. This buffer will be submitted for decode
-  // on the next DecodeSurface(). Return true on success.
-  bool SubmitSlice(int index, const uint8_t* data, size_t size);
-
-  // Submit controls in |ext_ctrls| to hardware. Return true on success.
-  bool SubmitExtControls(struct v4l2_ext_controls_custom* ext_ctrls);
-
-  // Gets current control values for controls in |ext_ctrls| from the driver.
-  // Return true on success.
-  bool GetExtControls(struct v4l2_ext_controls_custom* ext_ctrls);
-
-  // Return true if the driver exposes V4L2 control |ctrl_id|, false otherwise.
-  bool IsCtrlExposed(uint32_t ctrl_id);
-
-  // Decode of |dec_surface| is ready to be submitted and all codec-specific
-  // settings are set in hardware.
-  void DecodeSurface(const scoped_refptr<V4L2DecodeSurface>& dec_surface);
-
-  // |dec_surface| is ready to be outputted once decode is finished.
-  // This can be called before decode is actually done in hardware, and this
-  // method is responsible for maintaining the ordering, i.e. the surfaces will
-  // be outputted in the same order as SurfaceReady calls. To do so, the
-  // surfaces are put on decoder_display_queue_ and sent to output in that
-  // order once all preceding surfaces are sent.
-  void SurfaceReady(const scoped_refptr<V4L2DecodeSurface>& dec_surface);
-
-  //
-  // Internal methods of this class.
-  //
-  // Recycle a V4L2 input buffer with |index| after dequeuing from device.
-  void ReuseInputBuffer(int index);
-
-  // Recycle V4L2 output buffer with |index|. Used as surface release callback.
-  void ReuseOutputBuffer(int index);
-
-  // Queue a |dec_surface| to device for decoding.
-  void Enqueue(const scoped_refptr<V4L2DecodeSurface>& dec_surface);
-
-  // Dequeue any V4L2 buffers available and process.
-  void Dequeue();
-
-  // V4L2 QBUF helpers.
-  bool EnqueueInputRecord(int index, uint32_t config_store);
-  bool EnqueueOutputRecord(int index);
-
-  // Set input and output formats in hardware.
-  bool SetupFormats();
-
-  // Create input and output buffers.
-  bool CreateInputBuffers();
-  bool CreateOutputBuffers();
-
-  // Destroy input buffers.
-  void DestroyInputBuffers();
-
-  // Destroy output buffers. If |dismiss| is true, also dismissing the
-  // associated PictureBuffers.
-  bool DestroyOutputs(bool dismiss);
-
-  // Used by DestroyOutputs.
-  bool DestroyOutputBuffers();
-
-  // Dismiss all |picture_buffer_ids| via Client::DismissPictureBuffer()
-  // and signal |done| after finishing.
-  void DismissPictures(const std::vector<int32_t>& picture_buffer_ids,
-                       base::WaitableEvent* done);
-
-  // Task to finish initialization on decoder_thread_.
-  void InitializeTask();
-
-  void NotifyError(Error error);
-  void DestroyTask();
-
-  // Sets the state to kError and notifies client if needed.
-  void SetErrorState(Error error);
-
-  // Event handling. Events include flush, reset and resolution change and are
-  // processed while in kIdle state.
-
-  // Surface set change (resolution change) flow.
-  // If we have no surfaces allocated, start it immediately, otherwise mark
-  // ourselves as pending for surface set change.
-  void InitiateSurfaceSetChange();
-  // If a surface set change is pending and we are ready, stop the device,
-  // destroy outputs, releasing resources and dismissing pictures as required,
-  // followed by starting the flow to allocate a new set for the current
-  // resolution/DPB size, as provided by decoder.
-  bool FinishSurfaceSetChange();
-
-  // Flush flow when requested by client.
-  // When Flush() is called, it posts a FlushTask, which checks the input queue.
-  // If nothing is pending for decode on decoder_input_queue_, we call
-  // InitiateFlush() directly. Otherwise, we push a dummy BitstreamBufferRef
-  // onto the decoder_input_queue_ to schedule a flush. When we reach it later
-  // on, we call InitiateFlush() to perform it at the correct time.
-  void FlushTask();
-  // Tell the decoder to flush all frames, reset it and mark us as scheduled
-  // for flush, so that we can finish it once all pending decodes are finished.
-  void InitiateFlush();
-  // To be called if decoder_flushing_ is true. If not all pending frames are
-  // decoded, return false, requesting the caller to try again later.
-  // Otherwise perform flush by sending all pending pictures to the client,
-  // notify it that flush is finished and return true, informing the caller
-  // that further progress can be made.
-  bool FinishFlush();
-
-  // Reset flow when requested by client.
-  // Drop all inputs, reset the decoder and mark us as pending for reset.
-  void ResetTask();
-  // To be called if decoder_resetting_ is true. If not all pending frames are
-  // decoded, return false, requesting the caller to try again later.
-  // Otherwise perform reset by dropping all pending outputs (client is not
-  // interested anymore), notifying it that reset is finished, and return true,
-  // informing the caller that further progress can be made.
-  bool FinishReset();
-
-  // Called when a new event is pended. Transitions us into kIdle state (if not
-  // already in it), if possible. Also starts processing events.
-  void NewEventPending();
-
-  // Called after all events are processed successfully (i.e. all Finish*()
-  // methods return true) to return to decoding state.
-  bool FinishEventProcessing();
-
-  // Process pending events, if any.
-  void ProcessPendingEventsIfNeeded();
-
-  // Allocate V4L2 buffers and assign them to |buffers| provided by the client
-  // via AssignPictureBuffers() on decoder thread.
-  void AssignPictureBuffersTask(const std::vector<PictureBuffer>& buffers);
-
-  // Use buffer backed by dmabuf file descriptors in |passed_dmabuf_fds| for the
-  // OutputRecord associated with |picture_buffer_id|, taking ownership of the
-  // file descriptors.
-  void ImportBufferForPictureTask(
-      int32_t picture_buffer_id,
-      // TODO(posciak): (https://crbug.com/561749) we should normally be able to
-      // pass the vector by itself via std::move, but it's not possible to do
-      // this if this method is used as a callback.
-      std::unique_ptr<std::vector<base::ScopedFD>> passed_dmabuf_fds);
-
-  // Performed on decoder_thread_ as a consequence of poll() on decoder_thread_
-  // returning an event.
-  void ServiceDeviceTask();
-
-  // Schedule poll if we have any buffers queued and the poll thread
-  // is not stopped (on surface set change).
-  void SchedulePollIfNeeded();
-
-  // Attempt to start/stop device_poll_thread_.
-  bool StartDevicePoll();
-  bool StopDevicePoll(bool keep_input_state);
-
-  // Ran on device_poll_thread_ to wait for device events.
-  void DevicePollTask(bool poll_device);
-
-  enum State {
-    // We are in this state until Initialize() returns successfully.
-    // We can't post errors to the client in this state yet.
-    kUninitialized,
-    // Initialize() returned successfully.
-    kInitialized,
-    // This state allows making progress decoding more input stream.
-    kDecoding,
-    // Transitional state when we are not decoding any more stream, but are
-    // performing flush, reset, resolution change or are destroying ourselves.
-    kIdle,
-    // Requested new PictureBuffers via ProvidePictureBuffers(), awaiting
-    // AssignPictureBuffers().
-    kAwaitingPictureBuffers,
-    // Error state, set when sending NotifyError to client.
-    kError,
-  };
-
-  // Buffer id for flush buffer, queued by FlushTask().
-  const int kFlushBufferId = -2;
-
-  // Handler for Decode() on decoder_thread_.
-  void DecodeTask(const BitstreamBuffer& bitstream_buffer);
-
-  // Schedule a new DecodeBufferTask if we are decoding.
-  void ScheduleDecodeBufferTaskIfNeeded();
-
-  // Main decoder loop. Keep decoding the current buffer in decoder_, asking
-  // for more stream via TrySetNewBistreamBuffer() if decoder_ requests so,
-  // and handle other returns from it appropriately.
-  void DecodeBufferTask();
-
-  // Check decoder_input_queue_ for any available buffers to decode and
-  // set the decoder_current_bitstream_buffer_ to the next buffer if one is
-  // available, taking it off the queue. Also set the current stream pointer
-  // in decoder_, and return true.
-  // Return false if no buffers are pending on decoder_input_queue_.
-  bool TrySetNewBistreamBuffer();
-
-  // Auto-destruction reference for EGLSync (for message-passing).
-  void ReusePictureBufferTask(int32_t picture_buffer_id);
-
-  // Called to actually send |dec_surface| to the client, after it is decoded
-  // preserving the order in which it was scheduled via SurfaceReady().
-  void OutputSurface(const scoped_refptr<V4L2DecodeSurface>& dec_surface);
-
-  // Goes over the |decoder_display_queue_| and sends all buffers from the
-  // front of the queue that are already decoded to the client, in order.
-  void TryOutputSurfaces();
-
-  // Creates a new decode surface or returns nullptr if one is not available.
-  scoped_refptr<V4L2DecodeSurface> CreateSurface();
-
-  // Send decoded pictures to PictureReady.
-  void SendPictureReady();
-
-  // Callback that indicates a picture has been cleared.
-  void PictureCleared();
-
-  size_t input_planes_count_;
-  size_t output_planes_count_;
-
-  // GPU Child thread task runner.
-  const scoped_refptr<base::SingleThreadTaskRunner> child_task_runner_;
-
-  // Task runner Decode() and PictureReady() run on.
-  scoped_refptr<base::SingleThreadTaskRunner> decode_task_runner_;
-
-  // WeakPtr<> pointing to |this| for use in posting tasks from the decoder or
-  // device worker threads back to the child thread.
-  base::WeakPtr<V4L2SliceVideoDecodeAccelerator> weak_this_;
-
-  // To expose client callbacks from VideoDecodeAccelerator.
-  // NOTE: all calls to these objects *MUST* be executed on
-  // child_task_runner_.
-  std::unique_ptr<base::WeakPtrFactory<VideoDecodeAccelerator::Client>>
-      client_ptr_factory_;
-  base::WeakPtr<VideoDecodeAccelerator::Client> client_;
-  // Callbacks to |decode_client_| must be executed on |decode_task_runner_|.
-  base::WeakPtr<Client> decode_client_;
-
-  // V4L2 device in use.
-  scoped_refptr<V4L2Device> device_;
-
-  // Thread to communicate with the device on.
-  base::Thread decoder_thread_;
-  scoped_refptr<base::SingleThreadTaskRunner> decoder_thread_task_runner_;
-
-  // Thread used to poll the device for events.
-  base::Thread device_poll_thread_;
-
-  // Input queue state.
-  bool input_streamon_;
-  // Number of input buffers enqueued to the device.
-  int input_buffer_queued_count_;
-  // Input buffers ready to use; LIFO since we don't care about ordering.
-  std::list<int> free_input_buffers_;
-  // Mapping of int index to an input buffer record.
-  std::vector<InputRecord> input_buffer_map_;
-
-  // Output queue state.
-  bool output_streamon_;
-  // Number of output buffers enqueued to the device.
-  int output_buffer_queued_count_;
-  // Output buffers ready to use.
-  std::list<int> free_output_buffers_;
-  // Mapping of int index to an output buffer record.
-  std::vector<OutputRecord> output_buffer_map_;
-
-  VideoCodecProfile video_profile_;
-  uint32_t input_format_fourcc_;
-  uint32_t output_format_fourcc_;
-  Size coded_size_;
-
-  struct BitstreamBufferRef;
-  // Input queue of stream buffers coming from the client.
-  std::queue<linked_ptr<BitstreamBufferRef>> decoder_input_queue_;
-  // BitstreamBuffer currently being processed.
-  std::unique_ptr<BitstreamBufferRef> decoder_current_bitstream_buffer_;
-
-  // Queue storing decode surfaces ready to be output as soon as they are
-  // decoded. The surfaces must be output in order they are queued.
-  std::queue<scoped_refptr<V4L2DecodeSurface>> decoder_display_queue_;
-
-  // Decoder state.
-  State state_;
-
-  Config::OutputMode output_mode_;
-
-  // If any of these are true, we are waiting for the device to finish decoding
-  // all previously-queued frames, so we can finish the flush/reset/surface
-  // change flows. These can stack.
-  bool decoder_flushing_;
-  bool decoder_resetting_;
-  bool surface_set_change_pending_;
-
-  // Hardware accelerators.
-  // TODO(posciak): Try to have a superclass here if possible.
-  std::unique_ptr<V4L2H264Accelerator> h264_accelerator_;
-  std::unique_ptr<V4L2VP8Accelerator> vp8_accelerator_;
-  std::unique_ptr<V4L2VP9Accelerator> vp9_accelerator_;
-
-  // Codec-specific software decoder in use.
-  std::unique_ptr<AcceleratedVideoDecoder> decoder_;
-
-  // Surfaces queued to device to keep references to them while decoded.
-  using V4L2DecodeSurfaceByOutputId =
-      std::map<int, scoped_refptr<V4L2DecodeSurface>>;
-  V4L2DecodeSurfaceByOutputId surfaces_at_device_;
-
-  // Surfaces sent to client to keep references to them while displayed.
-  using V4L2DecodeSurfaceByPictureBufferId =
-      std::map<int32_t, scoped_refptr<V4L2DecodeSurface>>;
-  V4L2DecodeSurfaceByPictureBufferId surfaces_at_display_;
-
-  // Record for decoded pictures that can be sent to PictureReady.
-  struct PictureRecord {
-    PictureRecord(bool cleared, const Picture& picture);
-    ~PictureRecord();
-    bool cleared;  // Whether the texture is cleared and safe to render from.
-    Picture picture;  // The decoded picture.
-  };
-
-  // Pictures that are ready but not sent to PictureReady yet.
-  std::queue<PictureRecord> pending_picture_ready_;
-
-  // The number of pictures that are sent to PictureReady and will be cleared.
-  int picture_clearing_count_;
-
-  // The WeakPtrFactory for |weak_this_|.
-  base::WeakPtrFactory<V4L2SliceVideoDecodeAccelerator> weak_this_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(V4L2SliceVideoDecodeAccelerator);
-};
-
-}  // namespace media
-
-#endif  // V4L2_SLICE_VIDEO_DECODE_ACCELERATOR_H_
diff --git a/vda/v4l2_video_decode_accelerator.cc b/vda/v4l2_video_decode_accelerator.cc
index 19b5b38..5346a33 100644
--- a/vda/v4l2_video_decode_accelerator.cc
+++ b/vda/v4l2_video_decode_accelerator.cc
@@ -9,6 +9,7 @@
 #include <dlfcn.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <linux/videodev2.h>
 #include <poll.h>
 #include <string.h>
 #include <sys/eventfd.h>
@@ -27,7 +28,6 @@
 #include "h264_parser.h"
 #include "rect.h"
 #include "shared_memory_region.h"
-#include "videodev2_custom.h"
 
 #define DVLOGF(level) DVLOG(level) << __func__ << "(): "
 #define VLOGF(level) VLOG(level) << __func__ << "(): "
@@ -186,7 +186,7 @@
   video_profile_ = config.profile;
 
   input_format_fourcc_ =
-      V4L2Device::VideoCodecProfileToV4L2PixFmt(video_profile_, false);
+      V4L2Device::VideoCodecProfileToV4L2PixFmt(video_profile_);
 
   if (!device_->Open(V4L2Device::Type::kDecoder, input_format_fourcc_)) {
     VLOGF(1) << "Failed to open device for profile: " << config.profile
@@ -1084,7 +1084,7 @@
 
   // Dequeue a completed input (VIDEO_OUTPUT) buffer, and recycle to the free
   // list.
-  struct v4l2_buffer_custom dqbuf;
+  struct v4l2_buffer dqbuf;
   struct v4l2_plane planes[1];
   memset(&dqbuf, 0, sizeof(dqbuf));
   memset(planes, 0, sizeof(planes));
@@ -1119,7 +1119,7 @@
 
   // Dequeue a completed output (VIDEO_CAPTURE) buffer, and queue to the
   // completed queue.
-  struct v4l2_buffer_custom dqbuf;
+  struct v4l2_buffer dqbuf;
   std::unique_ptr<struct v4l2_plane[]> planes(
       new v4l2_plane[output_planes_count_]);
   memset(&dqbuf, 0, sizeof(dqbuf));
@@ -1184,7 +1184,7 @@
   const int buffer = input_ready_queue_.front();
   InputRecord& input_record = input_buffer_map_[buffer];
   DCHECK(!input_record.at_device);
-  struct v4l2_buffer_custom qbuf;
+  struct v4l2_buffer qbuf;
   struct v4l2_plane qbuf_plane;
   memset(&qbuf, 0, sizeof(qbuf));
   memset(&qbuf_plane, 0, sizeof(qbuf_plane));
@@ -1213,7 +1213,7 @@
   OutputRecord& output_record = output_buffer_map_[buffer];
   DCHECK_EQ(output_record.state, kFree);
   DCHECK_NE(output_record.picture_id, -1);
-  struct v4l2_buffer_custom qbuf;
+  struct v4l2_buffer qbuf;
   std::unique_ptr<struct v4l2_plane[]> qbuf_planes(
       new v4l2_plane[output_planes_count_]);
   memset(&qbuf, 0, sizeof(qbuf));
@@ -1819,7 +1819,7 @@
 
     // Query for the MEMORY_MMAP pointer.
     struct v4l2_plane planes[1];
-    struct v4l2_buffer_custom buffer;
+    struct v4l2_buffer buffer;
     memset(&buffer, 0, sizeof(buffer));
     memset(planes, 0, sizeof(planes));
     buffer.index = i;
diff --git a/vda/videodev2_custom.h b/vda/videodev2_custom.h
deleted file mode 100644
index 258b8fe..0000000
--- a/vda/videodev2_custom.h
+++ /dev/null
@@ -1,111 +0,0 @@
-// Copyright 2018 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// Note:
-// This header file has extracted some new structs and definitions in videodev2.h from ChromeOS
-// which is not upstreamed in Linux mainline. This should be removed once they are upstreamed.
-// TODO(johnylin): remove this file once it is upstreamed.
-
-#ifndef VIDEODEV2_CUSTOM_H_
-#define VIDEODEV2_CUSTOM_H_
-
-#include "v4l2_controls_custom.h"
-
-#include <linux/videodev2.h>
-
-/* compressed formats */
-#define V4L2_PIX_FMT_H264_SLICE v4l2_fourcc('S', '2', '6', '4') /* H264 parsed slices */
-#define V4L2_PIX_FMT_VP8_FRAME v4l2_fourcc('V', 'P', '8', 'F') /* VP8 parsed frames */
-#define V4L2_PIX_FMT_VP9_FRAME v4l2_fourcc('V', 'P', '9', 'F') /* VP9 parsed frames */
-
-struct v4l2_ext_control_custom {
-  __u32 id;
-  __u32 size;
-  __u32 reserved2[1];
-  union {
-    __s32 value;
-    __s64 value64;
-    char __user *string;
-    __u8 __user *p_u8;
-    __u16 __user *p_u16;
-    __u32 __user *p_u32;
-    struct v4l2_ctrl_h264_sps __user *p_h264_sps;
-    struct v4l2_ctrl_h264_pps __user *p_h264_pps;
-    struct v4l2_ctrl_h264_scaling_matrix __user *p_h264_scal_mtrx;
-    struct v4l2_ctrl_h264_slice_param __user *p_h264_slice_param;
-    struct v4l2_ctrl_h264_decode_param __user *p_h264_decode_param;
-    struct v4l2_ctrl_vp8_frame_hdr __user *p_vp8_frame_hdr;
-    struct v4l2_ctrl_vp9_frame_hdr __user *p_vp9_frame_hdr;
-    struct v4l2_ctrl_vp9_decode_param __user *p_vp9_decode_param;
-    struct v4l2_ctrl_vp9_entropy __user *p_vp9_entropy;
-    void __user *ptr;
-  };
-} __attribute__ ((packed));
-
-struct v4l2_ext_controls_custom {
-  union {
-    __u32 ctrl_class;
-    __u32 config_store;
-  };
-  __u32 count;
-  __u32 error_idx;
-  __u32 reserved[2];
-  struct v4l2_ext_control_custom *controls;
-};
-
-/**
- * struct v4l2_buffer - video buffer info
- * @index:	id number of the buffer
- * @type:	enum v4l2_buf_type; buffer type (type == *_MPLANE for
- *		multiplanar buffers);
- * @bytesused:	number of bytes occupied by data in the buffer (payload);
- *		unused (set to 0) for multiplanar buffers
- * @flags:	buffer informational flags
- * @field:	enum v4l2_field; field order of the image in the buffer
- * @timestamp:	frame timestamp
- * @timecode:	frame timecode
- * @sequence:	sequence count of this frame
- * @memory:	enum v4l2_memory; the method, in which the actual video data is
- *		passed
- * @offset:	for non-multiplanar buffers with memory == V4L2_MEMORY_MMAP;
- *		offset from the start of the device memory for this plane,
- *		(or a "cookie" that should be passed to mmap() as offset)
- * @userptr:	for non-multiplanar buffers with memory == V4L2_MEMORY_USERPTR;
- *		a userspace pointer pointing to this buffer
- * @fd:		for non-multiplanar buffers with memory == V4L2_MEMORY_DMABUF;
- *		a userspace file descriptor associated with this buffer
- * @planes:	for multiplanar buffers; userspace pointer to the array of plane
- *		info structs for this buffer
- * @length:	size in bytes of the buffer (NOT its payload) for single-plane
- *		buffers (when type != *_MPLANE); number of elements in the
- *		planes array for multi-plane buffers
- * @config_store: this buffer should use this configuration store
- *
- * Contains data exchanged by application and driver using one of the Streaming
- * I/O methods.
- */
-struct v4l2_buffer_custom {
-  __u32	index;
-  __u32	type;
-  __u32	bytesused;
-  __u32	flags;
-  __u32	field;
-  struct timeval timestamp;
-  struct v4l2_timecode timecode;
-  __u32 sequence;
-
-  /* memory location */
-  __u32 memory;
-  union {
-    __u32 offset;
-    unsigned long userptr;
-    struct v4l2_plane *planes;
-    __s32 fd;
-  } m;
-  __u32	length;
-  __u32	config_store;
-  __u32 reserved;
-};
-
-#endif  // VIDEODEV2_CUSTOM_H_