Johny Lin | 4869017 | 2017-08-04 11:11:50 +0800 | [diff] [blame] | 1 | // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | //#define LOG_NDEBUG 0 |
| 6 | #define LOG_TAG "C2VDAAdaptor" |
| 7 | |
| 8 | #include "C2VDAAdaptor.h" |
| 9 | #include "bitstream_buffer.h" |
| 10 | #include "native_pixmap_handle.h" |
Johny Lin | 7c4cb52 | 2017-06-26 16:10:24 +0800 | [diff] [blame] | 11 | #include "v4l2_device.h" |
Johny Lin | 4869017 | 2017-08-04 11:11:50 +0800 | [diff] [blame] | 12 | #include "v4l2_slice_video_decode_accelerator.h" |
Johny Lin | 7c4cb52 | 2017-06-26 16:10:24 +0800 | [diff] [blame] | 13 | #include "videodev2.h" |
Johny Lin | 4869017 | 2017-08-04 11:11:50 +0800 | [diff] [blame] | 14 | |
| 15 | #include <system/graphics.h> |
| 16 | #include <utils/Log.h> |
| 17 | |
| 18 | namespace android { |
| 19 | |
| 20 | C2VDAAdaptor::C2VDAAdaptor() : mNumOutputBuffers(0u) { |
| 21 | } |
| 22 | |
| 23 | C2VDAAdaptor::~C2VDAAdaptor() { |
| 24 | if (mVDA) { |
| 25 | destroy(); |
| 26 | } |
| 27 | } |
| 28 | |
| 29 | VideoDecodeAcceleratorAdaptor::Result C2VDAAdaptor::initialize( |
Hirokazu Honda | ce27506 | 2017-10-30 11:34:57 +0900 | [diff] [blame] | 30 | media::VideoCodecProfile profile, bool secureMode, |
| 31 | VideoDecodeAcceleratorAdaptor::Client* client) { |
| 32 | // TODO: use secureMode here, or ignore? |
Johny Lin | 4869017 | 2017-08-04 11:11:50 +0800 | [diff] [blame] | 33 | if (mVDA) { |
| 34 | ALOGE("Re-initialize() is not allowed"); |
| 35 | return ILLEGAL_STATE; |
| 36 | } |
| 37 | |
| 38 | media::VideoDecodeAccelerator::Config config; |
| 39 | config.profile = profile; |
| 40 | config.output_mode = media::VideoDecodeAccelerator::Config::OutputMode::IMPORT; |
| 41 | |
| 42 | // TODO(johnylin): may need to implement factory to create VDA if there are multiple VDA |
| 43 | // implementations in the future. |
| 44 | scoped_refptr<media::V4L2Device> device = new media::V4L2Device(); |
| 45 | std::unique_ptr<media::VideoDecodeAccelerator> vda( |
| 46 | new media::V4L2SliceVideoDecodeAccelerator(device)); |
| 47 | if (!vda->Initialize(config, this)) { |
| 48 | ALOGE("Failed to initialize VDA"); |
| 49 | return PLATFORM_FAILURE; |
| 50 | } |
| 51 | |
| 52 | mVDA = std::move(vda); |
| 53 | mClient = client; |
| 54 | |
| 55 | return SUCCESS; |
| 56 | } |
| 57 | |
| 58 | void C2VDAAdaptor::decode(int32_t bitstreamId, int ashmemFd, off_t offset, uint32_t bytesUsed) { |
| 59 | CHECK(mVDA); |
| 60 | mVDA->Decode(media::BitstreamBuffer( |
| 61 | bitstreamId, base::SharedMemoryHandle(ashmemFd, true), bytesUsed, offset)); |
| 62 | } |
| 63 | |
| 64 | void C2VDAAdaptor::assignPictureBuffers(uint32_t numOutputBuffers) { |
| 65 | CHECK(mVDA); |
| 66 | std::vector<media::PictureBuffer> buffers; |
| 67 | for (uint32_t id = 0; id < numOutputBuffers; ++id) { |
| 68 | buffers.push_back( |
| 69 | media::PictureBuffer(static_cast<int32_t>(id), mPictureSize)); |
| 70 | } |
| 71 | mVDA->AssignPictureBuffers(buffers); |
| 72 | mNumOutputBuffers = numOutputBuffers; |
| 73 | } |
| 74 | |
| 75 | void C2VDAAdaptor::importBufferForPicture(int32_t pictureBufferId, int dmabufFd, |
| 76 | const std::vector<VideoFramePlane>& planes) { |
| 77 | CHECK(mVDA); |
| 78 | CHECK_LT(pictureBufferId, static_cast<int32_t>(mNumOutputBuffers)); |
| 79 | |
| 80 | media::NativePixmapHandle handle; |
| 81 | handle.fds.emplace_back(base::FileDescriptor(dmabufFd, true)); |
| 82 | for (const auto& plane : planes) { |
| 83 | handle.planes.emplace_back(plane.stride, plane.offset, 0, 0); |
| 84 | } |
| 85 | mVDA->ImportBufferForPicture(pictureBufferId, handle); |
| 86 | } |
| 87 | |
| 88 | void C2VDAAdaptor::reusePictureBuffer(int32_t pictureBufferId) { |
| 89 | CHECK(mVDA); |
| 90 | CHECK_LT(pictureBufferId, static_cast<int32_t>(mNumOutputBuffers)); |
| 91 | |
| 92 | mVDA->ReusePictureBuffer(pictureBufferId); |
| 93 | } |
| 94 | |
| 95 | void C2VDAAdaptor::flush() { |
| 96 | CHECK(mVDA); |
| 97 | mVDA->Flush(); |
| 98 | } |
| 99 | |
| 100 | void C2VDAAdaptor::reset() { |
| 101 | CHECK(mVDA); |
| 102 | mVDA->Reset(); |
| 103 | } |
| 104 | |
| 105 | void C2VDAAdaptor::destroy() { |
| 106 | mVDA.reset(nullptr); |
| 107 | mNumOutputBuffers = 0u; |
| 108 | mPictureSize = media::Size(); |
| 109 | } |
| 110 | |
Johny Lin | 7c4cb52 | 2017-06-26 16:10:24 +0800 | [diff] [blame] | 111 | //static |
| 112 | media::VideoDecodeAccelerator::SupportedProfiles C2VDAAdaptor::GetSupportedProfiles( |
| 113 | uint32_t inputFormatFourcc) { |
| 114 | media::VideoDecodeAccelerator::SupportedProfiles supportedProfiles; |
| 115 | auto allProfiles = media::V4L2SliceVideoDecodeAccelerator::GetSupportedProfiles(); |
| 116 | bool isSliceBased = (inputFormatFourcc == V4L2_PIX_FMT_H264_SLICE) || |
| 117 | (inputFormatFourcc == V4L2_PIX_FMT_VP8_FRAME) || |
| 118 | (inputFormatFourcc == V4L2_PIX_FMT_VP9_FRAME); |
| 119 | for (const auto& profile : allProfiles) { |
| 120 | if (inputFormatFourcc == media::V4L2Device::VideoCodecProfileToV4L2PixFmt( |
| 121 | profile.profile, isSliceBased)) { |
| 122 | supportedProfiles.push_back(profile); |
| 123 | } |
| 124 | } |
| 125 | return supportedProfiles; |
| 126 | } |
Johny Lin | 4869017 | 2017-08-04 11:11:50 +0800 | [diff] [blame] | 127 | |
| 128 | void C2VDAAdaptor::ProvidePictureBuffers(uint32_t requested_num_of_buffers, |
| 129 | media::VideoPixelFormat output_format, |
| 130 | const media::Size& dimensions) { |
| 131 | uint32_t pixelFormat; |
| 132 | switch (output_format) { |
| 133 | case media::PIXEL_FORMAT_I420: |
| 134 | case media::PIXEL_FORMAT_YV12: |
| 135 | case media::PIXEL_FORMAT_NV12: |
| 136 | case media::PIXEL_FORMAT_NV21: |
| 137 | // HAL_PIXEL_FORMAT_YCbCr_420_888 is the flexible pixel format in Android |
| 138 | // which handles all 420 formats, with both orderings of chroma (CbCr and |
| 139 | // CrCb) as well as planar and semi-planar layouts. |
| 140 | pixelFormat = HAL_PIXEL_FORMAT_YCbCr_420_888; |
| 141 | break; |
| 142 | case media::PIXEL_FORMAT_ARGB: |
| 143 | pixelFormat = HAL_PIXEL_FORMAT_BGRA_8888; |
| 144 | break; |
| 145 | default: |
| 146 | ALOGE("Format not supported: %d", output_format); |
| 147 | mClient->notifyError(PLATFORM_FAILURE); |
| 148 | return; |
| 149 | } |
| 150 | |
| 151 | mClient->providePictureBuffers(pixelFormat, requested_num_of_buffers, dimensions); |
| 152 | mPictureSize = dimensions; |
| 153 | } |
| 154 | |
| 155 | void C2VDAAdaptor::DismissPictureBuffer(int32_t picture_buffer_id) { |
| 156 | mClient->dismissPictureBuffer(picture_buffer_id); |
| 157 | } |
| 158 | |
| 159 | void C2VDAAdaptor::PictureReady(const media::Picture& picture) { |
| 160 | mClient->pictureReady(picture.picture_buffer_id(), |
| 161 | picture.bitstream_buffer_id(), |
| 162 | picture.visible_rect()); |
| 163 | } |
| 164 | |
| 165 | void C2VDAAdaptor::NotifyEndOfBitstreamBuffer(int32_t bitstream_buffer_id) { |
| 166 | mClient->notifyEndOfBitstreamBuffer(bitstream_buffer_id); |
| 167 | } |
| 168 | |
| 169 | void C2VDAAdaptor::NotifyFlushDone() { |
| 170 | mClient->notifyFlushDone(); |
| 171 | } |
| 172 | |
| 173 | void C2VDAAdaptor::NotifyResetDone() { |
| 174 | mClient->notifyResetDone(); |
| 175 | } |
| 176 | |
| 177 | static VideoDecodeAcceleratorAdaptor::Result convertErrorCode( |
| 178 | media::VideoDecodeAccelerator::Error error) { |
| 179 | switch (error) { |
| 180 | case media::VideoDecodeAccelerator::ILLEGAL_STATE: |
| 181 | return VideoDecodeAcceleratorAdaptor::ILLEGAL_STATE; |
| 182 | case media::VideoDecodeAccelerator::INVALID_ARGUMENT: |
| 183 | return VideoDecodeAcceleratorAdaptor::INVALID_ARGUMENT; |
| 184 | case media::VideoDecodeAccelerator::UNREADABLE_INPUT: |
| 185 | return VideoDecodeAcceleratorAdaptor::UNREADABLE_INPUT; |
| 186 | case media::VideoDecodeAccelerator::PLATFORM_FAILURE: |
| 187 | return VideoDecodeAcceleratorAdaptor::PLATFORM_FAILURE; |
| 188 | default: |
Johny Lin | 7c4cb52 | 2017-06-26 16:10:24 +0800 | [diff] [blame] | 189 | ALOGE("Unknown error code: %d", static_cast<int>(error)); |
Johny Lin | 4869017 | 2017-08-04 11:11:50 +0800 | [diff] [blame] | 190 | return VideoDecodeAcceleratorAdaptor::PLATFORM_FAILURE; |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | void C2VDAAdaptor::NotifyError(media::VideoDecodeAccelerator::Error error) { |
| 195 | mClient->notifyError(convertErrorCode(error)); |
| 196 | } |
| 197 | |
| 198 | } // namespace android |