blob: d694bd1aab0e50a8c9dfba37dd776876c05515a6 [file] [log] [blame]
Pin-chih Lin04947dd2019-08-08 11:38:17 +08001// Copyright 2019 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
David Staessense5c20ed2020-06-17 11:58:21 +09006#define LOG_TAG "FormatConverter"
Pin-chih Lin04947dd2019-08-08 11:38:17 +08007
David Staessense5c20ed2020-06-17 11:58:21 +09008#include <v4l2_codec2/common/FormatConverter.h>
Pin-chih Lin04947dd2019-08-08 11:38:17 +08009
10#include <inttypes.h>
Pin-chih Lin04947dd2019-08-08 11:38:17 +080011
12#include <memory>
13#include <string>
14
David Staessense5c20ed2020-06-17 11:58:21 +090015#include <C2AllocatorGralloc.h>
16#include <C2PlatformSupport.h>
17#include <android/hardware/graphics/common/1.0/types.h>
18#include <inttypes.h>
19#include <libyuv.h>
20#include <ui/GraphicBuffer.h>
21#include <utils/Log.h>
22
David Staessensefdbd3d2020-07-30 16:54:21 +090023#include <v4l2_codec2/common/VideoTypes.h> // for HalPixelFormat
David Staessense5c20ed2020-06-17 11:58:21 +090024
Hirokazu Honda49738432019-11-11 11:20:44 +090025using android::hardware::graphics::common::V1_0::BufferUsage;
26
Pin-chih Lin04947dd2019-08-08 11:38:17 +080027namespace android {
28
29namespace {
Pin-chih Linea218a62019-10-08 15:21:52 +080030// The constant expression of mapping the pixel format conversion pair (src, dst) to a unique
31// integer.
David Staessens669080c2021-03-25 14:29:40 +090032constexpr int convertMap(VideoPixelFormat src, VideoPixelFormat dst) {
33 return static_cast<int>(src) * (static_cast<int>(VideoPixelFormat::UNKNOWN) + 1) +
David Staessense5c20ed2020-06-17 11:58:21 +090034 static_cast<int>(dst);
Pin-chih Linea218a62019-10-08 15:21:52 +080035}
36
Pin-chih Lin04947dd2019-08-08 11:38:17 +080037// The helper function to copy a plane pixel by pixel. It assumes bytesPerPixel is 1.
38void copyPlaneByPixel(const uint8_t* src, int srcStride, int srcColInc, uint8_t* dst, int dstStride,
39 int dstColInc, int width, int height) {
40 for (int row = 0; row < height; row++) {
41 const uint8_t* srcRow = src;
42 uint8_t* dstRow = dst;
43 for (int col = 0; col < width; col++) {
44 memcpy(dstRow, srcRow, 1);
45 srcRow += srcColInc;
46 dstRow += dstColInc;
47 }
48 src += srcStride;
49 dst += dstStride;
50 }
51}
52
53} // namespace
54
Pin-chih Linfe73d242019-12-04 16:55:57 +080055ImplDefinedToRGBXMap::ImplDefinedToRGBXMap(sp<GraphicBuffer> buf, uint8_t* addr, int rowInc)
David Staessense5c20ed2020-06-17 11:58:21 +090056 : mBuffer(std::move(buf)), mAddr(addr), mRowInc(rowInc) {}
Pin-chih Linfe73d242019-12-04 16:55:57 +080057
58ImplDefinedToRGBXMap::~ImplDefinedToRGBXMap() {
59 mBuffer->unlock();
60}
61
62// static
63std::unique_ptr<ImplDefinedToRGBXMap> ImplDefinedToRGBXMap::Create(
64 const C2ConstGraphicBlock& block) {
65 uint32_t width, height, format, stride, igbpSlot, generation;
66 uint64_t usage, igbpId;
David Staessense5c20ed2020-06-17 11:58:21 +090067 android::_UnwrapNativeCodec2GrallocMetadata(block.handle(), &width, &height, &format, &usage,
68 &stride, &generation, &igbpId, &igbpSlot);
Pin-chih Linfe73d242019-12-04 16:55:57 +080069
70 if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
71 ALOGE("The original format (=%u) is not IMPLEMENTATION_DEFINED", format);
72 return nullptr;
73 }
74
75 native_handle_t* grallocHandle = android::UnwrapNativeCodec2GrallocHandle(block.handle());
76 sp<GraphicBuffer> buf = new GraphicBuffer(grallocHandle, GraphicBuffer::CLONE_HANDLE, width,
77 height, format, 1, usage, stride);
78 native_handle_delete(grallocHandle);
79
80 void* pointer = nullptr;
81 int32_t status = buf->lock(GRALLOC_USAGE_SW_READ_OFTEN, &pointer);
82 if (status != OK) {
83 ALOGE("Failed to lock buffer as IMPLEMENTATION_DEFINED format");
84 return nullptr;
85 }
86
87 uint8_t* addr = reinterpret_cast<uint8_t*>(pointer);
88 int rowInc = static_cast<int>(stride * 4); // RGBX 4-byte data per pixel
89 ALOGD("Parsed input format IMPLEMENTATION_DEFINED to RGBX_8888");
90 return std::unique_ptr<ImplDefinedToRGBXMap>(
91 new ImplDefinedToRGBXMap(std::move(buf), addr, rowInc));
92}
93
Pin-chih Lin04947dd2019-08-08 11:38:17 +080094// static
David Staessens669080c2021-03-25 14:29:40 +090095std::unique_ptr<FormatConverter> FormatConverter::Create(VideoPixelFormat outFormat,
David Staessensee231c72021-03-23 14:44:32 +090096 const ui::Size& visibleSize,
David Staessense5c20ed2020-06-17 11:58:21 +090097 uint32_t inputCount,
David Staessensee231c72021-03-23 14:44:32 +090098 const ui::Size& codedSize) {
David Staessens669080c2021-03-25 14:29:40 +090099 if (outFormat != VideoPixelFormat::I420 && outFormat != VideoPixelFormat::NV12) {
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800100 ALOGE("Unsupported output format: %d", static_cast<int32_t>(outFormat));
101 return nullptr;
102 }
103
David Staessense5c20ed2020-06-17 11:58:21 +0900104 std::unique_ptr<FormatConverter> converter(new FormatConverter);
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800105 if (converter->initialize(outFormat, visibleSize, inputCount, codedSize) != C2_OK) {
David Staessense5c20ed2020-06-17 11:58:21 +0900106 ALOGE("Failed to initialize FormatConverter");
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800107 return nullptr;
108 }
109 return converter;
110}
111
David Staessens669080c2021-03-25 14:29:40 +0900112c2_status_t FormatConverter::initialize(VideoPixelFormat outFormat, const ui::Size& visibleSize,
113 uint32_t inputCount, const ui::Size& codedSize) {
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800114 ALOGV("initialize(out_format=%s, visible_size=%dx%d, input_count=%u, coded_size=%dx%d)",
David Staessens669080c2021-03-25 14:29:40 +0900115 videoPixelFormatToString(outFormat).c_str(), visibleSize.width, visibleSize.height,
David Staessensee231c72021-03-23 14:44:32 +0900116 inputCount, codedSize.width, codedSize.height);
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800117
118 std::shared_ptr<C2BlockPool> pool;
119 c2_status_t status = GetCodec2BlockPool(C2BlockPool::BASIC_GRAPHIC, nullptr, &pool);
120 if (status != C2_OK) {
121 ALOGE("Failed to get basic graphic block pool (err=%d)", status);
122 return status;
123 }
124
125 HalPixelFormat halFormat;
David Staessens669080c2021-03-25 14:29:40 +0900126 if (outFormat == VideoPixelFormat::I420) {
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800127 // Android HAL format doesn't have I420, we use YV12 instead and swap U and V data while
128 // conversion to perform I420.
129 halFormat = HalPixelFormat::YV12;
130 } else {
David Staessensefdbd3d2020-07-30 16:54:21 +0900131 halFormat = HalPixelFormat::YCBCR_420_888; // will allocate NV12 by minigbm.
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800132 }
133
134 uint32_t bufferCount = std::max(inputCount, kMinInputBufferCount);
135 for (uint32_t i = 0; i < bufferCount; i++) {
136 std::shared_ptr<C2GraphicBlock> block;
David Staessensee231c72021-03-23 14:44:32 +0900137 status = pool->fetchGraphicBlock(codedSize.width, codedSize.height,
David Staessense5c20ed2020-06-17 11:58:21 +0900138 static_cast<uint32_t>(halFormat),
139 {(C2MemoryUsage::CPU_READ | C2MemoryUsage::CPU_WRITE),
140 static_cast<uint64_t>(BufferUsage::VIDEO_ENCODER)},
141 &block);
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800142 if (status != C2_OK) {
143 ALOGE("Failed to fetch graphic block (err=%d)", status);
144 return status;
145 }
146 mGraphicBlocks.emplace_back(new BlockEntry(std::move(block)));
147 mAvailableQueue.push(mGraphicBlocks.back().get());
148 }
149
150 mOutFormat = outFormat;
151 mVisibleSize = visibleSize;
152
David Staessensee231c72021-03-23 14:44:32 +0900153 mTempPlaneU =
154 std::unique_ptr<uint8_t[]>(new uint8_t[mVisibleSize.width * mVisibleSize.height / 4]);
155 mTempPlaneV =
156 std::unique_ptr<uint8_t[]>(new uint8_t[mVisibleSize.width * mVisibleSize.height / 4]);
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800157
158 return C2_OK;
159}
160
David Staessense5c20ed2020-06-17 11:58:21 +0900161C2ConstGraphicBlock FormatConverter::convertBlock(uint64_t frameIndex,
162 const C2ConstGraphicBlock& inputBlock,
163 c2_status_t* status) {
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800164 if (!isReady()) {
165 ALOGV("There is no available block for conversion");
166 *status = C2_NO_MEMORY;
167 return inputBlock; // This is actually redundant and should not be used.
168 }
169
170 BlockEntry* entry = mAvailableQueue.front();
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800171 std::shared_ptr<C2GraphicBlock> outputBlock = entry->mBlock;
172
173 const C2GraphicView& inputView = inputBlock.map().get();
174 C2PlanarLayout inputLayout = inputView.layout();
175
Pin-chih Linfe73d242019-12-04 16:55:57 +0800176 // The above layout() cannot fill layout information and memset 0 instead if the input format is
177 // IMPLEMENTATION_DEFINED and its backed format is RGB. We fill the layout by using
178 // ImplDefinedToRGBXMap in the case.
179 std::unique_ptr<ImplDefinedToRGBXMap> idMap;
180 if (static_cast<uint32_t>(inputLayout.type) == 0u) {
181 idMap = ImplDefinedToRGBXMap::Create(inputBlock);
182 if (idMap == nullptr) {
183 ALOGE("Unable to parse RGBX_8888 from IMPLEMENTATION_DEFINED");
184 *status = C2_CORRUPTED;
185 return inputBlock; // This is actually redundant and should not be used.
186 }
187 inputLayout.type = C2PlanarLayout::TYPE_RGB;
188 }
189
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800190 C2GraphicView outputView = outputBlock->map().get();
191 C2PlanarLayout outputLayout = outputView.layout();
192 uint8_t* dstY = outputView.data()[C2PlanarLayout::PLANE_Y];
193 uint8_t* dstU = outputView.data()[C2PlanarLayout::PLANE_V]; // only for I420
194 uint8_t* dstV = outputView.data()[C2PlanarLayout::PLANE_U]; // only for I420
195 uint8_t* dstUV = outputView.data()[C2PlanarLayout::PLANE_U]; // only for NV12
196 const int dstStrideY = outputLayout.planes[C2PlanarLayout::PLANE_Y].rowInc;
197 const int dstStrideU = outputLayout.planes[C2PlanarLayout::PLANE_V].rowInc; // only for I420
198 const int dstStrideV = outputLayout.planes[C2PlanarLayout::PLANE_U].rowInc; // only for I420
199 const int dstStrideUV = outputLayout.planes[C2PlanarLayout::PLANE_U].rowInc; // only for NV12
200
David Staessens669080c2021-03-25 14:29:40 +0900201 VideoPixelFormat inputFormat = VideoPixelFormat::UNKNOWN;
Pin-chih Linea218a62019-10-08 15:21:52 +0800202 *status = C2_OK;
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800203 if (inputLayout.type == C2PlanarLayout::TYPE_YUV) {
204 const uint8_t* srcY = inputView.data()[C2PlanarLayout::PLANE_Y];
205 const uint8_t* srcU = inputView.data()[C2PlanarLayout::PLANE_U];
206 const uint8_t* srcV = inputView.data()[C2PlanarLayout::PLANE_V];
207 const int srcStrideY = inputLayout.planes[C2PlanarLayout::PLANE_Y].rowInc;
208 const int srcStrideU = inputLayout.planes[C2PlanarLayout::PLANE_U].rowInc;
209 const int srcStrideV = inputLayout.planes[C2PlanarLayout::PLANE_V].rowInc;
210 if (inputLayout.rootPlanes == 3) {
David Staessens669080c2021-03-25 14:29:40 +0900211 inputFormat = VideoPixelFormat::YV12;
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800212 } else if (inputLayout.rootPlanes == 2) {
David Staessens669080c2021-03-25 14:29:40 +0900213 inputFormat = (srcV > srcU) ? VideoPixelFormat::NV12 : VideoPixelFormat::NV21;
Pin-chih Linea218a62019-10-08 15:21:52 +0800214 }
215
216 if (inputFormat == mOutFormat) {
217 ALOGV("Zero-Copy is applied");
218 mGraphicBlocks.emplace_back(new BlockEntry(frameIndex));
219 return inputBlock;
220 }
221
222 switch (convertMap(inputFormat, mOutFormat)) {
David Staessens669080c2021-03-25 14:29:40 +0900223 case convertMap(VideoPixelFormat::YV12, VideoPixelFormat::I420):
Pin-chih Linea218a62019-10-08 15:21:52 +0800224 libyuv::I420Copy(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY, dstStrideY,
David Staessensee231c72021-03-23 14:44:32 +0900225 dstU, dstStrideU, dstV, dstStrideV, mVisibleSize.width,
226 mVisibleSize.height);
Pin-chih Linea218a62019-10-08 15:21:52 +0800227 break;
David Staessens669080c2021-03-25 14:29:40 +0900228 case convertMap(VideoPixelFormat::YV12, VideoPixelFormat::NV12):
Pin-chih Linea218a62019-10-08 15:21:52 +0800229 libyuv::I420ToNV12(srcY, srcStrideY, srcU, srcStrideU, srcV, srcStrideV, dstY,
David Staessensee231c72021-03-23 14:44:32 +0900230 dstStrideY, dstUV, dstStrideUV, mVisibleSize.width,
231 mVisibleSize.height);
Pin-chih Linea218a62019-10-08 15:21:52 +0800232 break;
David Staessens669080c2021-03-25 14:29:40 +0900233 case convertMap(VideoPixelFormat::NV12, VideoPixelFormat::I420):
Pin-chih Linea218a62019-10-08 15:21:52 +0800234 libyuv::NV12ToI420(srcY, srcStrideY, srcU, srcStrideU, dstY, dstStrideY, dstU,
David Staessensee231c72021-03-23 14:44:32 +0900235 dstStrideU, dstV, dstStrideV, mVisibleSize.width,
236 mVisibleSize.height);
Pin-chih Linea218a62019-10-08 15:21:52 +0800237 break;
David Staessens669080c2021-03-25 14:29:40 +0900238 case convertMap(VideoPixelFormat::NV21, VideoPixelFormat::I420):
Pin-chih Linea218a62019-10-08 15:21:52 +0800239 libyuv::NV21ToI420(srcY, srcStrideY, srcV, srcStrideV, dstY, dstStrideY, dstU,
David Staessensee231c72021-03-23 14:44:32 +0900240 dstStrideU, dstV, dstStrideV, mVisibleSize.width,
241 mVisibleSize.height);
Pin-chih Linea218a62019-10-08 15:21:52 +0800242 break;
David Staessens669080c2021-03-25 14:29:40 +0900243 case convertMap(VideoPixelFormat::NV21, VideoPixelFormat::NV12):
244 ALOGV("%s(): Converting PIXEL_FORMAT_NV21 -> PIXEL_FORMAT_NV12", __func__);
David Staessensee231c72021-03-23 14:44:32 +0900245 libyuv::CopyPlane(srcY, srcStrideY, dstY, dstStrideY, mVisibleSize.width,
246 mVisibleSize.height);
247 copyPlaneByPixel(srcU, srcStrideU, 2, dstUV, dstStrideUV, 2, mVisibleSize.width / 2,
248 mVisibleSize.height / 2);
249 copyPlaneByPixel(srcV, srcStrideV, 2, dstUV + 1, dstStrideUV, 2, mVisibleSize.width / 2,
250 mVisibleSize.height / 2);
Pin-chih Linea218a62019-10-08 15:21:52 +0800251 break;
252 default:
253 ALOGE("Unsupported pixel format conversion from %s to %s",
David Staessens669080c2021-03-25 14:29:40 +0900254 videoPixelFormatToString(inputFormat).c_str(),
255 videoPixelFormatToString(mOutFormat).c_str());
Pin-chih Linea218a62019-10-08 15:21:52 +0800256 *status = C2_CORRUPTED;
257 return inputBlock; // This is actually redundant and should not be used.
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800258 }
259 } else if (inputLayout.type == C2PlanarLayout::TYPE_RGB) {
260 // There is only RGBA_8888 specified in C2AllocationGralloc::map(), no BGRA_8888. Maybe
261 // BGRA_8888 is not used now?
David Staessens669080c2021-03-25 14:29:40 +0900262 inputFormat = VideoPixelFormat::ABGR;
Pin-chih Linfe73d242019-12-04 16:55:57 +0800263
David Staessense5c20ed2020-06-17 11:58:21 +0900264 const uint8_t* srcRGB = (idMap) ? idMap->addr() : inputView.data()[C2PlanarLayout::PLANE_R];
265 const int srcStrideRGB =
266 (idMap) ? idMap->rowInc() : inputLayout.planes[C2PlanarLayout::PLANE_R].rowInc;
Pin-chih Linea218a62019-10-08 15:21:52 +0800267
268 switch (convertMap(inputFormat, mOutFormat)) {
David Staessens669080c2021-03-25 14:29:40 +0900269 case convertMap(VideoPixelFormat::ABGR, VideoPixelFormat::I420):
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800270 libyuv::ABGRToI420(srcRGB, srcStrideRGB, dstY, dstStrideY, dstU, dstStrideU, dstV,
David Staessensee231c72021-03-23 14:44:32 +0900271 dstStrideV, mVisibleSize.width, mVisibleSize.height);
Pin-chih Linea218a62019-10-08 15:21:52 +0800272 break;
David Staessens669080c2021-03-25 14:29:40 +0900273 case convertMap(VideoPixelFormat::ABGR, VideoPixelFormat::NV12): {
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800274 // There is no libyuv function to convert ABGR to NV12. Therefore, we first convert to
275 // I420 on dst-Y plane and temporary U/V plane. Then we copy U and V pixels from
276 // temporary planes to dst-UV interleavedly.
David Staessensee231c72021-03-23 14:44:32 +0900277 const int tempStride = mVisibleSize.width / 2;
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800278 libyuv::ABGRToI420(srcRGB, srcStrideRGB, dstY, dstStrideY, mTempPlaneU.get(),
David Staessensee231c72021-03-23 14:44:32 +0900279 tempStride, mTempPlaneV.get(), tempStride, mVisibleSize.width,
280 mVisibleSize.height);
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800281 libyuv::MergeUVPlane(mTempPlaneU.get(), tempStride, mTempPlaneV.get(), tempStride,
David Staessensee231c72021-03-23 14:44:32 +0900282 dstUV, dstStrideUV, mVisibleSize.width / 2,
283 mVisibleSize.height / 2);
Pin-chih Linea218a62019-10-08 15:21:52 +0800284 break;
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800285 }
Pin-chih Linea218a62019-10-08 15:21:52 +0800286 default:
287 ALOGE("Unsupported pixel format conversion from %s to %s",
David Staessens669080c2021-03-25 14:29:40 +0900288 videoPixelFormatToString(inputFormat).c_str(),
289 videoPixelFormatToString(mOutFormat).c_str());
Pin-chih Linea218a62019-10-08 15:21:52 +0800290 *status = C2_CORRUPTED;
291 return inputBlock; // This is actually redundant and should not be used.
292 }
293 } else {
294 ALOGE("Unsupported input layout type");
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800295 *status = C2_CORRUPTED;
296 return inputBlock; // This is actually redundant and should not be used.
297 }
298
299 ALOGV("convertBlock(frame_index=%" PRIu64 ", format=%s)", frameIndex,
David Staessens669080c2021-03-25 14:29:40 +0900300 videoPixelFormatToString(inputFormat).c_str());
Pin-chih Linea218a62019-10-08 15:21:52 +0800301 entry->mAssociatedFrameIndex = frameIndex;
302 mAvailableQueue.pop();
David Staessensee231c72021-03-23 14:44:32 +0900303 return outputBlock->share(C2Rect(mVisibleSize.width, mVisibleSize.height), C2Fence());
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800304}
305
David Staessense5c20ed2020-06-17 11:58:21 +0900306c2_status_t FormatConverter::returnBlock(uint64_t frameIndex) {
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800307 ALOGV("returnBlock(frame_index=%" PRIu64 ")", frameIndex);
308
David Staessense5c20ed2020-06-17 11:58:21 +0900309 auto iter = std::find_if(mGraphicBlocks.begin(), mGraphicBlocks.end(),
310 [frameIndex](const std::unique_ptr<BlockEntry>& be) {
311 return be->mAssociatedFrameIndex == frameIndex;
312 });
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800313 if (iter == mGraphicBlocks.end()) {
Pin-chih Linea218a62019-10-08 15:21:52 +0800314 ALOGE("Failed to find graphic block by converted/zero-copied frame index: %" PRIu64 "",
315 frameIndex);
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800316 return C2_BAD_INDEX;
317 }
318
Pin-chih Linea218a62019-10-08 15:21:52 +0800319 if ((*iter)->mBlock) {
320 // Returned block is format converted.
321 (*iter)->mAssociatedFrameIndex = kNoFrameAssociated;
322 mAvailableQueue.push(iter->get());
323 } else {
324 // Returned block is zero-copied.
325 mGraphicBlocks.erase(iter);
326 }
Pin-chih Lin04947dd2019-08-08 11:38:17 +0800327 return C2_OK;
328}
329
330} // namespace android