Move Codec2-related code from hardware/google/av
Test: None
Bug: 112362730
Change-Id: Ie2f8ff431d65c40333f267ab9877d47089adeea4
diff --git a/media/codec2/sfplugin/utils/Android.bp b/media/codec2/sfplugin/utils/Android.bp
new file mode 100644
index 0000000..3dc6060
--- /dev/null
+++ b/media/codec2/sfplugin/utils/Android.bp
@@ -0,0 +1,39 @@
+cc_library_shared {
+ name: "libstagefright_ccodec_utils",
+ vendor_available: true,
+
+ srcs: [
+ "Codec2BufferUtils.cpp",
+ "Codec2Mapper.cpp",
+ ],
+
+ cflags: [
+ "-Werror",
+ "-Wall",
+ ],
+
+ export_include_dirs: [
+ ".",
+ ],
+
+ shared_libs: [
+ "libbase",
+ "libcutils",
+ "liblog",
+ "libstagefright_codec2",
+ "libstagefright_codec2_vndk",
+ "libstagefright_foundation",
+ "libutils",
+ ],
+
+ sanitize: {
+ cfi: true,
+ misc_undefined: [
+ "unsigned-integer-overflow",
+ "signed-integer-overflow",
+ ],
+ diag: {
+ cfi: true,
+ },
+ },
+}
diff --git a/media/codec2/sfplugin/utils/Codec2BufferUtils.cpp b/media/codec2/sfplugin/utils/Codec2BufferUtils.cpp
new file mode 100644
index 0000000..b7519da
--- /dev/null
+++ b/media/codec2/sfplugin/utils/Codec2BufferUtils.cpp
@@ -0,0 +1,400 @@
+/*
+ * Copyright 2018, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "Codec2BufferUtils"
+#include <utils/Log.h>
+
+#include <list>
+#include <mutex>
+
+#include <media/hardware/HardwareAPI.h>
+#include <media/stagefright/foundation/AUtils.h>
+
+#include <C2Debug.h>
+
+#include "Codec2BufferUtils.h"
+
+namespace android {
+
+namespace {
+
+/**
+ * A flippable, optimizable memcpy. Constructs such as (from ? src : dst) do not work as the results are
+ * always const.
+ */
+template<bool ToA, size_t S>
+struct MemCopier {
+ template<typename A, typename B>
+ inline static void copy(A *a, const B *b, size_t size) {
+ __builtin_memcpy(a, b, size);
+ }
+};
+
+template<size_t S>
+struct MemCopier<false, S> {
+ template<typename A, typename B>
+ inline static void copy(const A *a, B *b, size_t size) {
+ MemCopier<true, S>::copy(b, a, size);
+ }
+};
+
+/**
+ * Copies between a MediaImage and a graphic view.
+ *
+ * \param ToMediaImage whether to copy to (or from) the MediaImage
+ * \param view graphic view (could be ConstGraphicView or GraphicView depending on direction)
+ * \param img MediaImage data
+ * \param imgBase base of MediaImage (could be const uint8_t* or uint8_t* depending on direction)
+ */
+template<bool ToMediaImage, typename View, typename ImagePixel>
+static status_t _ImageCopy(View &view, const MediaImage2 *img, ImagePixel *imgBase) {
+ // TODO: more efficient copying --- e.g. one row at a time, copying
+ // interleaved planes together, etc.
+ const C2PlanarLayout &layout = view.layout();
+ const size_t bpp = divUp(img->mBitDepthAllocated, 8u);
+ if (view.width() != img->mWidth
+ || view.height() != img->mHeight) {
+ return BAD_VALUE;
+ }
+ for (uint32_t i = 0; i < layout.numPlanes; ++i) {
+ typename std::conditional<ToMediaImage, uint8_t, const uint8_t>::type *imgRow =
+ imgBase + img->mPlane[i].mOffset;
+ typename std::conditional<ToMediaImage, const uint8_t, uint8_t>::type *viewRow =
+ viewRow = view.data()[i];
+ const C2PlaneInfo &plane = layout.planes[i];
+ if (plane.colSampling != img->mPlane[i].mHorizSubsampling
+ || plane.rowSampling != img->mPlane[i].mVertSubsampling
+ || plane.allocatedDepth != img->mBitDepthAllocated
+ || plane.allocatedDepth < plane.bitDepth
+ // MediaImage only supports MSB values
+ || plane.rightShift != plane.allocatedDepth - plane.bitDepth
+ || (bpp > 1 && plane.endianness != plane.NATIVE)) {
+ return BAD_VALUE;
+ }
+
+ uint32_t planeW = img->mWidth / plane.colSampling;
+ uint32_t planeH = img->mHeight / plane.rowSampling;
+ for (uint32_t row = 0; row < planeH; ++row) {
+ decltype(imgRow) imgPtr = imgRow;
+ decltype(viewRow) viewPtr = viewRow;
+ for (uint32_t col = 0; col < planeW; ++col) {
+ MemCopier<ToMediaImage, 0>::copy(imgPtr, viewPtr, bpp);
+ imgPtr += img->mPlane[i].mColInc;
+ viewPtr += plane.colInc;
+ }
+ imgRow += img->mPlane[i].mRowInc;
+ viewRow += plane.rowInc;
+ }
+ }
+ return OK;
+}
+
+} // namespace
+
+status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view) {
+ return _ImageCopy<true>(view, img, imgBase);
+}
+
+status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img) {
+ return _ImageCopy<false>(view, img, imgBase);
+}
+
+bool IsYUV420(const C2GraphicView &view) {
+ const C2PlanarLayout &layout = view.layout();
+ return (layout.numPlanes == 3
+ && layout.type == C2PlanarLayout::TYPE_YUV
+ && layout.planes[layout.PLANE_Y].channel == C2PlaneInfo::CHANNEL_Y
+ && layout.planes[layout.PLANE_Y].allocatedDepth == 8
+ && layout.planes[layout.PLANE_Y].bitDepth == 8
+ && layout.planes[layout.PLANE_Y].rightShift == 0
+ && layout.planes[layout.PLANE_Y].colSampling == 1
+ && layout.planes[layout.PLANE_Y].rowSampling == 1
+ && layout.planes[layout.PLANE_U].channel == C2PlaneInfo::CHANNEL_CB
+ && layout.planes[layout.PLANE_U].allocatedDepth == 8
+ && layout.planes[layout.PLANE_U].bitDepth == 8
+ && layout.planes[layout.PLANE_U].rightShift == 0
+ && layout.planes[layout.PLANE_U].colSampling == 2
+ && layout.planes[layout.PLANE_U].rowSampling == 2
+ && layout.planes[layout.PLANE_V].channel == C2PlaneInfo::CHANNEL_CR
+ && layout.planes[layout.PLANE_V].allocatedDepth == 8
+ && layout.planes[layout.PLANE_V].bitDepth == 8
+ && layout.planes[layout.PLANE_V].rightShift == 0
+ && layout.planes[layout.PLANE_V].colSampling == 2
+ && layout.planes[layout.PLANE_V].rowSampling == 2);
+}
+
+MediaImage2 CreateYUV420PlanarMediaImage2(
+ uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
+ return MediaImage2 {
+ .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
+ .mNumPlanes = 3,
+ .mWidth = width,
+ .mHeight = height,
+ .mBitDepth = 8,
+ .mBitDepthAllocated = 8,
+ .mPlane = {
+ {
+ .mOffset = 0,
+ .mColInc = 1,
+ .mRowInc = (int32_t)stride,
+ .mHorizSubsampling = 1,
+ .mVertSubsampling = 1,
+ },
+ {
+ .mOffset = stride * vstride,
+ .mColInc = 1,
+ .mRowInc = (int32_t)stride / 2,
+ .mHorizSubsampling = 2,
+ .mVertSubsampling = 2,
+ },
+ {
+ .mOffset = stride * vstride * 5 / 4,
+ .mColInc = 1,
+ .mRowInc = (int32_t)stride / 2,
+ .mHorizSubsampling = 2,
+ .mVertSubsampling = 2,
+ }
+ },
+ };
+}
+
+MediaImage2 CreateYUV420SemiPlanarMediaImage2(
+ uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride) {
+ return MediaImage2 {
+ .mType = MediaImage2::MEDIA_IMAGE_TYPE_YUV,
+ .mNumPlanes = 3,
+ .mWidth = width,
+ .mHeight = height,
+ .mBitDepth = 8,
+ .mBitDepthAllocated = 8,
+ .mPlane = {
+ {
+ .mOffset = 0,
+ .mColInc = 1,
+ .mRowInc = (int32_t)stride,
+ .mHorizSubsampling = 1,
+ .mVertSubsampling = 1,
+ },
+ {
+ .mOffset = stride * vstride,
+ .mColInc = 2,
+ .mRowInc = (int32_t)stride,
+ .mHorizSubsampling = 2,
+ .mVertSubsampling = 2,
+ },
+ {
+ .mOffset = stride * vstride + 1,
+ .mColInc = 2,
+ .mRowInc = (int32_t)stride,
+ .mHorizSubsampling = 2,
+ .mVertSubsampling = 2,
+ }
+ },
+ };
+}
+
+status_t ConvertRGBToPlanarYUV(
+ uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
+ const C2GraphicView &src) {
+ CHECK(dstY != nullptr);
+ CHECK((src.width() & 1) == 0);
+ CHECK((src.height() & 1) == 0);
+
+ if (dstStride * dstVStride * 3 / 2 > bufferSize) {
+ ALOGD("conversion buffer is too small for converting from RGB to YUV");
+ return NO_MEMORY;
+ }
+
+ uint8_t *dstU = dstY + dstStride * dstVStride;
+ uint8_t *dstV = dstU + (dstStride >> 1) * (dstVStride >> 1);
+
+ const C2PlanarLayout &layout = src.layout();
+ const uint8_t *pRed = src.data()[C2PlanarLayout::PLANE_R];
+ const uint8_t *pGreen = src.data()[C2PlanarLayout::PLANE_G];
+ const uint8_t *pBlue = src.data()[C2PlanarLayout::PLANE_B];
+
+#define CLIP3(x,y,z) (((z) < (x)) ? (x) : (((z) > (y)) ? (y) : (z)))
+ for (size_t y = 0; y < src.height(); ++y) {
+ for (size_t x = 0; x < src.width(); ++x) {
+ uint8_t red = *pRed;
+ uint8_t green = *pGreen;
+ uint8_t blue = *pBlue;
+
+ // using ITU-R BT.601 conversion matrix
+ unsigned luma =
+ CLIP3(0, (((red * 66 + green * 129 + blue * 25) >> 8) + 16), 255);
+
+ dstY[x] = luma;
+
+ if ((x & 1) == 0 && (y & 1) == 0) {
+ unsigned U =
+ CLIP3(0, (((-red * 38 - green * 74 + blue * 112) >> 8) + 128), 255);
+
+ unsigned V =
+ CLIP3(0, (((red * 112 - green * 94 - blue * 18) >> 8) + 128), 255);
+
+ dstU[x >> 1] = U;
+ dstV[x >> 1] = V;
+ }
+ pRed += layout.planes[C2PlanarLayout::PLANE_R].colInc;
+ pGreen += layout.planes[C2PlanarLayout::PLANE_G].colInc;
+ pBlue += layout.planes[C2PlanarLayout::PLANE_B].colInc;
+ }
+
+ if ((y & 1) == 0) {
+ dstU += dstStride >> 1;
+ dstV += dstStride >> 1;
+ }
+
+ pRed -= layout.planes[C2PlanarLayout::PLANE_R].colInc * src.width();
+ pGreen -= layout.planes[C2PlanarLayout::PLANE_G].colInc * src.width();
+ pBlue -= layout.planes[C2PlanarLayout::PLANE_B].colInc * src.width();
+ pRed += layout.planes[C2PlanarLayout::PLANE_R].rowInc;
+ pGreen += layout.planes[C2PlanarLayout::PLANE_G].rowInc;
+ pBlue += layout.planes[C2PlanarLayout::PLANE_B].rowInc;
+
+ dstY += dstStride;
+ }
+ return OK;
+}
+
+namespace {
+
+/**
+ * A block of raw allocated memory.
+ */
+struct MemoryBlockPoolBlock {
+ MemoryBlockPoolBlock(size_t size)
+ : mData(new uint8_t[size]), mSize(mData ? size : 0) { }
+
+ ~MemoryBlockPoolBlock() {
+ delete[] mData;
+ }
+
+ const uint8_t *data() const {
+ return mData;
+ }
+
+ size_t size() const {
+ return mSize;
+ }
+
+ C2_DO_NOT_COPY(MemoryBlockPoolBlock);
+
+private:
+ uint8_t *mData;
+ size_t mSize;
+};
+
+/**
+ * A simple raw memory block pool implementation.
+ */
+struct MemoryBlockPoolImpl {
+ void release(std::list<MemoryBlockPoolBlock>::const_iterator block) {
+ std::lock_guard<std::mutex> lock(mMutex);
+ // return block to free blocks if it is the current size; otherwise, discard
+ if (block->size() == mCurrentSize) {
+ mFreeBlocks.splice(mFreeBlocks.begin(), mBlocksInUse, block);
+ } else {
+ mBlocksInUse.erase(block);
+ }
+ }
+
+ std::list<MemoryBlockPoolBlock>::const_iterator fetch(size_t size) {
+ std::lock_guard<std::mutex> lock(mMutex);
+ mFreeBlocks.remove_if([size](const MemoryBlockPoolBlock &block) -> bool {
+ return block.size() != size;
+ });
+ mCurrentSize = size;
+ if (mFreeBlocks.empty()) {
+ mBlocksInUse.emplace_front(size);
+ } else {
+ mBlocksInUse.splice(mBlocksInUse.begin(), mFreeBlocks, mFreeBlocks.begin());
+ }
+ return mBlocksInUse.begin();
+ }
+
+ MemoryBlockPoolImpl() = default;
+
+ C2_DO_NOT_COPY(MemoryBlockPoolImpl);
+
+private:
+ std::mutex mMutex;
+ std::list<MemoryBlockPoolBlock> mFreeBlocks;
+ std::list<MemoryBlockPoolBlock> mBlocksInUse;
+ size_t mCurrentSize;
+};
+
+} // namespace
+
+struct MemoryBlockPool::Impl : MemoryBlockPoolImpl {
+};
+
+struct MemoryBlock::Impl {
+ Impl(std::list<MemoryBlockPoolBlock>::const_iterator block,
+ std::shared_ptr<MemoryBlockPoolImpl> pool)
+ : mBlock(block), mPool(pool) {
+ }
+
+ ~Impl() {
+ mPool->release(mBlock);
+ }
+
+ const uint8_t *data() const {
+ return mBlock->data();
+ }
+
+ size_t size() const {
+ return mBlock->size();
+ }
+
+private:
+ std::list<MemoryBlockPoolBlock>::const_iterator mBlock;
+ std::shared_ptr<MemoryBlockPoolImpl> mPool;
+};
+
+MemoryBlock MemoryBlockPool::fetch(size_t size) {
+ std::list<MemoryBlockPoolBlock>::const_iterator poolBlock = mImpl->fetch(size);
+ return MemoryBlock(std::make_shared<MemoryBlock::Impl>(
+ poolBlock, std::static_pointer_cast<MemoryBlockPoolImpl>(mImpl)));
+}
+
+MemoryBlockPool::MemoryBlockPool()
+ : mImpl(std::make_shared<MemoryBlockPool::Impl>()) {
+}
+
+MemoryBlock::MemoryBlock(std::shared_ptr<MemoryBlock::Impl> impl)
+ : mImpl(impl) {
+}
+
+MemoryBlock::MemoryBlock() = default;
+
+MemoryBlock::~MemoryBlock() = default;
+
+const uint8_t* MemoryBlock::data() const {
+ return mImpl ? mImpl->data() : nullptr;
+}
+
+size_t MemoryBlock::size() const {
+ return mImpl ? mImpl->size() : 0;
+}
+
+MemoryBlock MemoryBlock::Allocate(size_t size) {
+ return MemoryBlockPool().fetch(size);
+}
+
+} // namespace android
diff --git a/media/codec2/sfplugin/utils/Codec2BufferUtils.h b/media/codec2/sfplugin/utils/Codec2BufferUtils.h
new file mode 100644
index 0000000..eaf6776
--- /dev/null
+++ b/media/codec2/sfplugin/utils/Codec2BufferUtils.h
@@ -0,0 +1,141 @@
+/*
+ * Copyright 2018, The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef CODEC2_BUFFER_UTILS_H_
+#define CODEC2_BUFFER_UTILS_H_
+
+#include <C2Buffer.h>
+#include <C2ParamDef.h>
+
+#include <media/hardware/VideoAPI.h>
+#include <utils/Errors.h>
+
+namespace android {
+
+/**
+ * Converts an RGB view to planar YUV 420 media image.
+ *
+ * \param dstY pointer to media image buffer
+ * \param dstStride stride in bytes
+ * \param dstVStride vertical stride in pixels
+ * \param bufferSize media image buffer size
+ * \param src source image
+ *
+ * \retval NO_MEMORY media image is too small
+ * \retval OK on success
+ */
+status_t ConvertRGBToPlanarYUV(
+ uint8_t *dstY, size_t dstStride, size_t dstVStride, size_t bufferSize,
+ const C2GraphicView &src);
+
+/**
+ * Returns a planar YUV 420 8-bit media image descriptor.
+ *
+ * \param width width of image in pixels
+ * \param height height of image in pixels
+ * \param stride stride of image in pixels
+ * \param vstride vertical stride of image in pixels
+ */
+MediaImage2 CreateYUV420PlanarMediaImage2(
+ uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride);
+
+/**
+ * Returns a semiplanar YUV 420 8-bit media image descriptor.
+ *
+ * \param width width of image in pixels
+ * \param height height of image in pixels
+ * \param stride stride of image in pixels
+ * \param vstride vertical stride of image in pixels
+ */
+MediaImage2 CreateYUV420SemiPlanarMediaImage2(
+ uint32_t width, uint32_t height, uint32_t stride, uint32_t vstride);
+
+/**
+ * Copies a graphic view into a media image.
+ *
+ * \param imgBase base of MediaImage
+ * \param img MediaImage data
+ * \param view graphic view
+ *
+ * \return OK on success
+ */
+status_t ImageCopy(uint8_t *imgBase, const MediaImage2 *img, const C2GraphicView &view);
+
+/**
+ * Copies a media image into a graphic view.
+ *
+ * \param view graphic view
+ * \param imgBase base of MediaImage
+ * \param img MediaImage data
+ *
+ * \return OK on success
+ */
+status_t ImageCopy(C2GraphicView &view, const uint8_t *imgBase, const MediaImage2 *img);
+
+/**
+ * Returns true iff a view has a YUV 420 888 layout.
+ */
+bool IsYUV420(const C2GraphicView &view);
+
+/**
+ * A raw memory block to use for internal buffers.
+ *
+ * TODO: replace this with C2LinearBlocks from a private C2BlockPool
+ */
+struct MemoryBlock : public C2MemoryBlock<uint8_t> {
+ virtual const uint8_t* data() const override;
+ virtual size_t size() const override;
+
+ inline uint8_t *data() {
+ return const_cast<uint8_t*>(const_cast<const MemoryBlock*>(this)->data());
+ }
+
+ // allocates an unmanaged block (not in a pool)
+ static MemoryBlock Allocate(size_t);
+
+ // memory block with no actual memory (size is 0, data is null)
+ MemoryBlock();
+
+ struct Impl;
+ MemoryBlock(std::shared_ptr<Impl> impl);
+ virtual ~MemoryBlock();
+
+private:
+ std::shared_ptr<Impl> mImpl;
+};
+
+/**
+ * A raw memory mini-pool.
+ */
+struct MemoryBlockPool {
+ /**
+ * Fetches a block with a given size.
+ *
+ * \param size size in bytes
+ */
+ MemoryBlock fetch(size_t size);
+
+ MemoryBlockPool();
+ ~MemoryBlockPool() = default;
+
+private:
+ struct Impl;
+ std::shared_ptr<Impl> mImpl;
+};
+
+} // namespace android
+
+#endif // CODEC2_BUFFER_UTILS_H_
diff --git a/media/codec2/sfplugin/utils/Codec2Mapper.cpp b/media/codec2/sfplugin/utils/Codec2Mapper.cpp
new file mode 100644
index 0000000..97e17e8
--- /dev/null
+++ b/media/codec2/sfplugin/utils/Codec2Mapper.cpp
@@ -0,0 +1,815 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//#define LOG_NDEBUG 0
+#define LOG_TAG "Codec2Mapper"
+#include <utils/Log.h>
+
+#include <media/stagefright/MediaCodecConstants.h>
+#include <media/stagefright/SurfaceUtils.h>
+#include <media/stagefright/foundation/ALookup.h>
+#include <media/stagefright/foundation/ColorUtils.h>
+#include <media/stagefright/foundation/MediaDefs.h>
+
+#include <stdint.h> // for INT32_MAX
+
+#include "Codec2Mapper.h"
+
+using namespace android;
+
+namespace {
+
+ALookup<C2Config::profile_t, int32_t> sAacProfiles = {
+ { C2Config::PROFILE_AAC_LC, AACObjectLC },
+ { C2Config::PROFILE_AAC_MAIN, AACObjectMain },
+ { C2Config::PROFILE_AAC_SSR, AACObjectSSR },
+ { C2Config::PROFILE_AAC_LTP, AACObjectLTP },
+ { C2Config::PROFILE_AAC_HE, AACObjectHE },
+ { C2Config::PROFILE_AAC_SCALABLE, AACObjectScalable },
+ { C2Config::PROFILE_AAC_ER_LC, AACObjectERLC },
+ { C2Config::PROFILE_AAC_ER_SCALABLE, AACObjectERScalable },
+ { C2Config::PROFILE_AAC_LD, AACObjectLD },
+ { C2Config::PROFILE_AAC_HE_PS, AACObjectHE_PS },
+ { C2Config::PROFILE_AAC_ELD, AACObjectELD },
+ { C2Config::PROFILE_AAC_XHE, AACObjectXHE },
+};
+
+ALookup<C2Config::level_t, int32_t> sAvcLevels = {
+ { C2Config::LEVEL_AVC_1, AVCLevel1 },
+ { C2Config::LEVEL_AVC_1B, AVCLevel1b },
+ { C2Config::LEVEL_AVC_1_1, AVCLevel11 },
+ { C2Config::LEVEL_AVC_1_2, AVCLevel12 },
+ { C2Config::LEVEL_AVC_1_3, AVCLevel13 },
+ { C2Config::LEVEL_AVC_2, AVCLevel2 },
+ { C2Config::LEVEL_AVC_2_1, AVCLevel21 },
+ { C2Config::LEVEL_AVC_2_2, AVCLevel22 },
+ { C2Config::LEVEL_AVC_3, AVCLevel3 },
+ { C2Config::LEVEL_AVC_3_1, AVCLevel31 },
+ { C2Config::LEVEL_AVC_3_2, AVCLevel32 },
+ { C2Config::LEVEL_AVC_4, AVCLevel4 },
+ { C2Config::LEVEL_AVC_4_1, AVCLevel41 },
+ { C2Config::LEVEL_AVC_4_2, AVCLevel42 },
+ { C2Config::LEVEL_AVC_5, AVCLevel5 },
+ { C2Config::LEVEL_AVC_5_1, AVCLevel51 },
+ { C2Config::LEVEL_AVC_5_2, AVCLevel52 },
+
+};
+
+ALookup<C2Config::profile_t, int32_t> sAvcProfiles = {
+ // treat restricted profiles as full profile if there is no equivalent - which works for
+ // decoders, but not for encoders
+ { C2Config::PROFILE_AVC_BASELINE, AVCProfileBaseline },
+ { C2Config::PROFILE_AVC_CONSTRAINED_BASELINE, AVCProfileConstrainedBaseline },
+ { C2Config::PROFILE_AVC_MAIN, AVCProfileMain },
+ { C2Config::PROFILE_AVC_EXTENDED, AVCProfileExtended },
+ { C2Config::PROFILE_AVC_HIGH, AVCProfileHigh },
+ { C2Config::PROFILE_AVC_PROGRESSIVE_HIGH, AVCProfileHigh },
+ { C2Config::PROFILE_AVC_CONSTRAINED_HIGH, AVCProfileConstrainedHigh },
+ { C2Config::PROFILE_AVC_HIGH_10, AVCProfileHigh10 },
+ { C2Config::PROFILE_AVC_PROGRESSIVE_HIGH_10, AVCProfileHigh10 },
+ { C2Config::PROFILE_AVC_HIGH_422, AVCProfileHigh422 },
+ { C2Config::PROFILE_AVC_HIGH_444_PREDICTIVE, AVCProfileHigh444 },
+ { C2Config::PROFILE_AVC_HIGH_10_INTRA, AVCProfileHigh10 },
+ { C2Config::PROFILE_AVC_HIGH_422_INTRA, AVCProfileHigh422 },
+ { C2Config::PROFILE_AVC_HIGH_444_INTRA, AVCProfileHigh444 },
+ { C2Config::PROFILE_AVC_CAVLC_444_INTRA, AVCProfileHigh444 },
+};
+
+ALookup<C2Config::bitrate_mode_t, int32_t> sBitrateModes = {
+ { C2Config::BITRATE_CONST, BITRATE_MODE_CBR },
+ { C2Config::BITRATE_VARIABLE, BITRATE_MODE_VBR },
+ { C2Config::BITRATE_IGNORE, BITRATE_MODE_CQ },
+};
+
+ALookup<C2Color::matrix_t, ColorAspects::MatrixCoeffs> sColorMatricesSf = {
+ { C2Color::MATRIX_UNSPECIFIED, ColorAspects::MatrixUnspecified },
+ { C2Color::MATRIX_BT709, ColorAspects::MatrixBT709_5 },
+ { C2Color::MATRIX_FCC47_73_682, ColorAspects::MatrixBT470_6M },
+ { C2Color::MATRIX_BT601, ColorAspects::MatrixBT601_6 },
+ { C2Color::MATRIX_SMPTE240M, ColorAspects::MatrixSMPTE240M },
+ { C2Color::MATRIX_BT2020, ColorAspects::MatrixBT2020 },
+ { C2Color::MATRIX_BT2020_CONSTANT, ColorAspects::MatrixBT2020Constant },
+ { C2Color::MATRIX_OTHER, ColorAspects::MatrixOther },
+};
+
+ALookup<C2Color::primaries_t, ColorAspects::Primaries> sColorPrimariesSf = {
+ { C2Color::PRIMARIES_UNSPECIFIED, ColorAspects::PrimariesUnspecified },
+ { C2Color::PRIMARIES_BT709, ColorAspects::PrimariesBT709_5 },
+ { C2Color::PRIMARIES_BT470_M, ColorAspects::PrimariesBT470_6M },
+ { C2Color::PRIMARIES_BT601_625, ColorAspects::PrimariesBT601_6_625 },
+ { C2Color::PRIMARIES_BT601_525, ColorAspects::PrimariesBT601_6_525 },
+ { C2Color::PRIMARIES_GENERIC_FILM, ColorAspects::PrimariesGenericFilm },
+ { C2Color::PRIMARIES_BT2020, ColorAspects::PrimariesBT2020 },
+// { C2Color::PRIMARIES_RP431, ColorAspects::Primaries... },
+// { C2Color::PRIMARIES_EG432, ColorAspects::Primaries... },
+// { C2Color::PRIMARIES_EBU3213, ColorAspects::Primaries... },
+ { C2Color::PRIMARIES_OTHER, ColorAspects::PrimariesOther },
+};
+
+ALookup<C2Color::range_t, int32_t> sColorRanges = {
+ { C2Color::RANGE_FULL, COLOR_RANGE_FULL },
+ { C2Color::RANGE_LIMITED, COLOR_RANGE_LIMITED },
+};
+
+ALookup<C2Color::range_t, ColorAspects::Range> sColorRangesSf = {
+ { C2Color::RANGE_UNSPECIFIED, ColorAspects::RangeUnspecified },
+ { C2Color::RANGE_FULL, ColorAspects::RangeFull },
+ { C2Color::RANGE_LIMITED, ColorAspects::RangeLimited },
+ { C2Color::RANGE_OTHER, ColorAspects::RangeOther },
+};
+
+ALookup<C2Color::transfer_t, int32_t> sColorTransfers = {
+ { C2Color::TRANSFER_LINEAR, COLOR_TRANSFER_LINEAR },
+ { C2Color::TRANSFER_170M, COLOR_TRANSFER_SDR_VIDEO },
+ { C2Color::TRANSFER_ST2084, COLOR_TRANSFER_ST2084 },
+ { C2Color::TRANSFER_HLG, COLOR_TRANSFER_HLG },
+};
+
+ALookup<C2Color::transfer_t, ColorAspects::Transfer> sColorTransfersSf = {
+ { C2Color::TRANSFER_UNSPECIFIED, ColorAspects::TransferUnspecified },
+ { C2Color::TRANSFER_LINEAR, ColorAspects::TransferLinear },
+ { C2Color::TRANSFER_SRGB, ColorAspects::TransferSRGB },
+ { C2Color::TRANSFER_170M, ColorAspects::TransferSMPTE170M },
+ { C2Color::TRANSFER_GAMMA22, ColorAspects::TransferGamma22 },
+ { C2Color::TRANSFER_GAMMA28, ColorAspects::TransferGamma28 },
+ { C2Color::TRANSFER_ST2084, ColorAspects::TransferST2084 },
+ { C2Color::TRANSFER_HLG, ColorAspects::TransferHLG },
+ { C2Color::TRANSFER_240M, ColorAspects::TransferSMPTE240M },
+ { C2Color::TRANSFER_XVYCC, ColorAspects::TransferXvYCC },
+ { C2Color::TRANSFER_BT1361, ColorAspects::TransferBT1361 },
+ { C2Color::TRANSFER_ST428, ColorAspects::TransferST428 },
+ { C2Color::TRANSFER_OTHER, ColorAspects::TransferOther },
+};
+
+ALookup<C2Config::level_t, int32_t> sDolbyVisionLevels = {
+ { C2Config::LEVEL_DV_MAIN_HD_24, DolbyVisionLevelHd24 },
+ { C2Config::LEVEL_DV_MAIN_HD_30, DolbyVisionLevelHd30 },
+ { C2Config::LEVEL_DV_MAIN_FHD_24, DolbyVisionLevelFhd24 },
+ { C2Config::LEVEL_DV_MAIN_FHD_30, DolbyVisionLevelFhd30 },
+ { C2Config::LEVEL_DV_MAIN_FHD_60, DolbyVisionLevelFhd60 },
+ { C2Config::LEVEL_DV_MAIN_UHD_24, DolbyVisionLevelUhd24 },
+ { C2Config::LEVEL_DV_MAIN_UHD_30, DolbyVisionLevelUhd30 },
+ { C2Config::LEVEL_DV_MAIN_UHD_48, DolbyVisionLevelUhd48 },
+ { C2Config::LEVEL_DV_MAIN_UHD_60, DolbyVisionLevelUhd60 },
+
+ // high tiers are not yet supported on android, for now map them to main tier
+ { C2Config::LEVEL_DV_HIGH_HD_24, DolbyVisionLevelHd24 },
+ { C2Config::LEVEL_DV_HIGH_HD_30, DolbyVisionLevelHd30 },
+ { C2Config::LEVEL_DV_HIGH_FHD_24, DolbyVisionLevelFhd24 },
+ { C2Config::LEVEL_DV_HIGH_FHD_30, DolbyVisionLevelFhd30 },
+ { C2Config::LEVEL_DV_HIGH_FHD_60, DolbyVisionLevelFhd60 },
+ { C2Config::LEVEL_DV_HIGH_UHD_24, DolbyVisionLevelUhd24 },
+ { C2Config::LEVEL_DV_HIGH_UHD_30, DolbyVisionLevelUhd30 },
+ { C2Config::LEVEL_DV_HIGH_UHD_48, DolbyVisionLevelUhd48 },
+ { C2Config::LEVEL_DV_HIGH_UHD_60, DolbyVisionLevelUhd60 },
+};
+
+ALookup<C2Config::profile_t, int32_t> sDolbyVisionProfiles = {
+ { C2Config::PROFILE_DV_AV_PER, DolbyVisionProfileDvavPer },
+ { C2Config::PROFILE_DV_AV_PEN, DolbyVisionProfileDvavPen },
+ { C2Config::PROFILE_DV_HE_DER, DolbyVisionProfileDvheDer },
+ { C2Config::PROFILE_DV_HE_DEN, DolbyVisionProfileDvheDen },
+ { C2Config::PROFILE_DV_HE_04, DolbyVisionProfileDvheDtr },
+ { C2Config::PROFILE_DV_HE_05, DolbyVisionProfileDvheStn },
+ { C2Config::PROFILE_DV_HE_DTH, DolbyVisionProfileDvheDth },
+ { C2Config::PROFILE_DV_HE_07, DolbyVisionProfileDvheDtb },
+ { C2Config::PROFILE_DV_HE_08, DolbyVisionProfileDvheSt },
+ { C2Config::PROFILE_DV_AV_09, DolbyVisionProfileDvavSe },
+};
+
+ALookup<C2Config::level_t, int32_t> sH263Levels = {
+ { C2Config::LEVEL_H263_10, H263Level10 },
+ { C2Config::LEVEL_H263_20, H263Level20 },
+ { C2Config::LEVEL_H263_30, H263Level30 },
+ { C2Config::LEVEL_H263_40, H263Level40 },
+ { C2Config::LEVEL_H263_45, H263Level45 },
+ { C2Config::LEVEL_H263_50, H263Level50 },
+ { C2Config::LEVEL_H263_60, H263Level60 },
+ { C2Config::LEVEL_H263_70, H263Level70 },
+};
+
+ALookup<C2Config::profile_t, int32_t> sH263Profiles = {
+ { C2Config::PROFILE_H263_BASELINE, H263ProfileBaseline },
+ { C2Config::PROFILE_H263_H320, H263ProfileH320Coding },
+ { C2Config::PROFILE_H263_V1BC, H263ProfileBackwardCompatible },
+ { C2Config::PROFILE_H263_ISWV2, H263ProfileISWV2 },
+ { C2Config::PROFILE_H263_ISWV3, H263ProfileISWV3 },
+ { C2Config::PROFILE_H263_HIGH_COMPRESSION, H263ProfileHighCompression },
+ { C2Config::PROFILE_H263_INTERNET, H263ProfileInternet },
+ { C2Config::PROFILE_H263_INTERLACE, H263ProfileInterlace },
+ { C2Config::PROFILE_H263_HIGH_LATENCY, H263ProfileHighLatency },
+};
+
+ALookup<C2Config::level_t, int32_t> sHevcLevels = {
+ { C2Config::LEVEL_HEVC_MAIN_1, HEVCMainTierLevel1 },
+ { C2Config::LEVEL_HEVC_MAIN_2, HEVCMainTierLevel2 },
+ { C2Config::LEVEL_HEVC_MAIN_2_1, HEVCMainTierLevel21 },
+ { C2Config::LEVEL_HEVC_MAIN_3, HEVCMainTierLevel3 },
+ { C2Config::LEVEL_HEVC_MAIN_3_1, HEVCMainTierLevel31 },
+ { C2Config::LEVEL_HEVC_MAIN_4, HEVCMainTierLevel4 },
+ { C2Config::LEVEL_HEVC_MAIN_4_1, HEVCMainTierLevel41 },
+ { C2Config::LEVEL_HEVC_MAIN_5, HEVCMainTierLevel5 },
+ { C2Config::LEVEL_HEVC_MAIN_5_1, HEVCMainTierLevel51 },
+ { C2Config::LEVEL_HEVC_MAIN_5_2, HEVCMainTierLevel52 },
+ { C2Config::LEVEL_HEVC_MAIN_6, HEVCMainTierLevel6 },
+ { C2Config::LEVEL_HEVC_MAIN_6_1, HEVCMainTierLevel61 },
+ { C2Config::LEVEL_HEVC_MAIN_6_2, HEVCMainTierLevel62 },
+
+ { C2Config::LEVEL_HEVC_HIGH_4, HEVCHighTierLevel4 },
+ { C2Config::LEVEL_HEVC_HIGH_4_1, HEVCHighTierLevel41 },
+ { C2Config::LEVEL_HEVC_HIGH_5, HEVCHighTierLevel5 },
+ { C2Config::LEVEL_HEVC_HIGH_5_1, HEVCHighTierLevel51 },
+ { C2Config::LEVEL_HEVC_HIGH_5_2, HEVCHighTierLevel52 },
+ { C2Config::LEVEL_HEVC_HIGH_6, HEVCHighTierLevel6 },
+ { C2Config::LEVEL_HEVC_HIGH_6_1, HEVCHighTierLevel61 },
+ { C2Config::LEVEL_HEVC_HIGH_6_2, HEVCHighTierLevel62 },
+
+ // map high tier levels below 4 to main tier
+ { C2Config::LEVEL_HEVC_MAIN_1, HEVCHighTierLevel1 },
+ { C2Config::LEVEL_HEVC_MAIN_2, HEVCHighTierLevel2 },
+ { C2Config::LEVEL_HEVC_MAIN_2_1, HEVCHighTierLevel21 },
+ { C2Config::LEVEL_HEVC_MAIN_3, HEVCHighTierLevel3 },
+ { C2Config::LEVEL_HEVC_MAIN_3_1, HEVCHighTierLevel31 },
+};
+
+ALookup<C2Config::profile_t, int32_t> sHevcProfiles = {
+ { C2Config::PROFILE_HEVC_MAIN, HEVCProfileMain },
+ { C2Config::PROFILE_HEVC_MAIN_10, HEVCProfileMain10 },
+ { C2Config::PROFILE_HEVC_MAIN_STILL, HEVCProfileMainStill },
+ { C2Config::PROFILE_HEVC_MAIN_INTRA, HEVCProfileMain },
+ { C2Config::PROFILE_HEVC_MAIN_10_INTRA, HEVCProfileMain10 },
+};
+
+ALookup<C2Config::level_t, int32_t> sMpeg2Levels = {
+ { C2Config::LEVEL_MP2V_LOW, MPEG2LevelLL },
+ { C2Config::LEVEL_MP2V_MAIN, MPEG2LevelML },
+ { C2Config::LEVEL_MP2V_HIGH_1440, MPEG2LevelH14 },
+ { C2Config::LEVEL_MP2V_HIGH, MPEG2LevelHL },
+ { C2Config::LEVEL_MP2V_HIGHP, MPEG2LevelHP },
+};
+
+ALookup<C2Config::profile_t, int32_t> sMpeg2Profiles = {
+ { C2Config::PROFILE_MP2V_SIMPLE, MPEG2ProfileSimple },
+ { C2Config::PROFILE_MP2V_MAIN, MPEG2ProfileMain },
+ { C2Config::PROFILE_MP2V_SNR_SCALABLE, MPEG2ProfileSNR },
+ { C2Config::PROFILE_MP2V_SPATIALLY_SCALABLE, MPEG2ProfileSpatial },
+ { C2Config::PROFILE_MP2V_HIGH, MPEG2ProfileHigh },
+ { C2Config::PROFILE_MP2V_422, MPEG2Profile422 },
+};
+
+ALookup<C2Config::level_t, int32_t> sMpeg4Levels = {
+ { C2Config::LEVEL_MP4V_0, MPEG4Level0 },
+ { C2Config::LEVEL_MP4V_0B, MPEG4Level0b },
+ { C2Config::LEVEL_MP4V_1, MPEG4Level1 },
+ { C2Config::LEVEL_MP4V_2, MPEG4Level2 },
+ { C2Config::LEVEL_MP4V_3, MPEG4Level3 },
+ { C2Config::LEVEL_MP4V_3B, MPEG4Level3b },
+ { C2Config::LEVEL_MP4V_4, MPEG4Level4 },
+ { C2Config::LEVEL_MP4V_4A, MPEG4Level4a },
+ { C2Config::LEVEL_MP4V_5, MPEG4Level5 },
+ { C2Config::LEVEL_MP4V_6, MPEG4Level6 },
+};
+
+ALookup<C2Config::profile_t, int32_t> sMpeg4Profiles = {
+ { C2Config::PROFILE_MP4V_SIMPLE, MPEG4ProfileSimple },
+ { C2Config::PROFILE_MP4V_SIMPLE_SCALABLE, MPEG4ProfileSimpleScalable },
+ { C2Config::PROFILE_MP4V_CORE, MPEG4ProfileCore },
+ { C2Config::PROFILE_MP4V_MAIN, MPEG4ProfileMain },
+ { C2Config::PROFILE_MP4V_NBIT, MPEG4ProfileNbit },
+ { C2Config::PROFILE_MP4V_ARTS, MPEG4ProfileAdvancedRealTime },
+ { C2Config::PROFILE_MP4V_CORE_SCALABLE, MPEG4ProfileCoreScalable },
+ { C2Config::PROFILE_MP4V_ACE, MPEG4ProfileAdvancedCoding },
+ { C2Config::PROFILE_MP4V_ADVANCED_CORE, MPEG4ProfileAdvancedCore },
+ { C2Config::PROFILE_MP4V_ADVANCED_SIMPLE, MPEG4ProfileAdvancedSimple },
+};
+
+ALookup<C2Config::pcm_encoding_t, int32_t> sPcmEncodings = {
+ { C2Config::PCM_8, kAudioEncodingPcm8bit },
+ { C2Config::PCM_16, kAudioEncodingPcm16bit },
+ { C2Config::PCM_FLOAT, kAudioEncodingPcmFloat },
+};
+
+ALookup<C2Config::level_t, int32_t> sVp9Levels = {
+ { C2Config::LEVEL_VP9_1, VP9Level1 },
+ { C2Config::LEVEL_VP9_1_1, VP9Level11 },
+ { C2Config::LEVEL_VP9_2, VP9Level2 },
+ { C2Config::LEVEL_VP9_2_1, VP9Level21 },
+ { C2Config::LEVEL_VP9_3, VP9Level3 },
+ { C2Config::LEVEL_VP9_3_1, VP9Level31 },
+ { C2Config::LEVEL_VP9_4, VP9Level4 },
+ { C2Config::LEVEL_VP9_4_1, VP9Level41 },
+ { C2Config::LEVEL_VP9_5, VP9Level5 },
+ { C2Config::LEVEL_VP9_5_1, VP9Level51 },
+ { C2Config::LEVEL_VP9_5_2, VP9Level52 },
+ { C2Config::LEVEL_VP9_6, VP9Level6 },
+ { C2Config::LEVEL_VP9_6_1, VP9Level61 },
+ { C2Config::LEVEL_VP9_6_2, VP9Level62 },
+};
+
+ALookup<C2Config::profile_t, int32_t> sVp9Profiles = {
+ { C2Config::PROFILE_VP9_0, VP9Profile0 },
+ { C2Config::PROFILE_VP9_1, VP9Profile1 },
+ { C2Config::PROFILE_VP9_2, VP9Profile2 },
+ { C2Config::PROFILE_VP9_3, VP9Profile3 },
+};
+
+/**
+ * A helper that passes through vendor extension profile and level values.
+ */
+struct ProfileLevelMapperHelper : C2Mapper::ProfileLevelMapper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) = 0;
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) = 0;
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) = 0;
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) = 0;
+
+ template<typename T, typename U>
+ bool passThroughMap(T from, U *to) {
+ // allow (and pass through) vendor extensions
+ if (from >= (T)C2_PROFILE_LEVEL_VENDOR_START && from < (T)INT32_MAX) {
+ *to = (U)from;
+ return true;
+ }
+ return simpleMap(from, to);
+ }
+
+ virtual bool mapLevel(C2Config::level_t from, int32_t *to) {
+ return passThroughMap(from, to);
+ }
+
+ virtual bool mapLevel(int32_t from, C2Config::level_t *to) {
+ return passThroughMap(from, to);
+ }
+
+ virtual bool mapProfile(C2Config::profile_t from, int32_t *to) {
+ return passThroughMap(from, to);
+ }
+
+ virtual bool mapProfile(int32_t from, C2Config::profile_t *to) {
+ return passThroughMap(from, to);
+ }
+};
+
+// AAC only uses profiles, map all levels to unused or 0
+struct AacProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t, int32_t *to) {
+ *to = 0;
+ return true;
+ }
+ virtual bool simpleMap(int32_t, C2Config::level_t *to) {
+ *to = C2Config::LEVEL_UNUSED;
+ return true;
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sAacProfiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sAacProfiles.map(from, to);
+ }
+};
+
+struct AvcProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sAvcLevels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sAvcLevels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sAvcProfiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sAvcProfiles.map(from, to);
+ }
+};
+
+struct DolbyVisionProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sDolbyVisionLevels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sDolbyVisionLevels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sDolbyVisionProfiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sDolbyVisionProfiles.map(from, to);
+ }
+};
+
+struct H263ProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sH263Levels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sH263Levels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sH263Profiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sH263Profiles.map(from, to);
+ }
+};
+
+struct HevcProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sHevcLevels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sHevcLevels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sHevcProfiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sHevcProfiles.map(from, to);
+ }
+};
+
+struct Mpeg2ProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sMpeg2Levels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sMpeg2Levels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sMpeg2Profiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sMpeg2Profiles.map(from, to);
+ }
+};
+
+struct Mpeg4ProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sMpeg4Levels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sMpeg4Levels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sMpeg4Profiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sMpeg4Profiles.map(from, to);
+ }
+};
+
+// VP8 has no profiles and levels in Codec 2.0, but we use main profile and level 0 in MediaCodec
+// map all profiles and levels to that.
+struct Vp8ProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t, int32_t *to) {
+ *to = VP8Level_Version0;
+ return true;
+ }
+ virtual bool simpleMap(int32_t, C2Config::level_t *to) {
+ *to = C2Config::LEVEL_UNUSED;
+ return true;
+ }
+ virtual bool simpleMap(C2Config::profile_t, int32_t *to) {
+ *to = VP8ProfileMain;
+ return true;
+ }
+ virtual bool simpleMap(int32_t, C2Config::profile_t *to) {
+ *to = C2Config::PROFILE_UNUSED;
+ return true;
+ }
+};
+
+struct Vp9ProfileLevelMapper : ProfileLevelMapperHelper {
+ virtual bool simpleMap(C2Config::level_t from, int32_t *to) {
+ return sVp9Levels.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::level_t *to) {
+ return sVp9Levels.map(from, to);
+ }
+ virtual bool simpleMap(C2Config::profile_t from, int32_t *to) {
+ return sVp9Profiles.map(from, to);
+ }
+ virtual bool simpleMap(int32_t from, C2Config::profile_t *to) {
+ return sVp9Profiles.map(from, to);
+ }
+};
+
+} // namespace
+
+// static
+std::shared_ptr<C2Mapper::ProfileLevelMapper>
+C2Mapper::GetProfileLevelMapper(std::string mediaType) {
+ std::transform(mediaType.begin(), mediaType.begin(), mediaType.end(), ::tolower);
+ if (mediaType == MIMETYPE_AUDIO_AAC) {
+ return std::make_shared<AacProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_AVC) {
+ return std::make_shared<AvcProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_DOLBY_VISION) {
+ return std::make_shared<DolbyVisionProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_H263) {
+ return std::make_shared<H263ProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_HEVC) {
+ return std::make_shared<HevcProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_MPEG2) {
+ return std::make_shared<Mpeg2ProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_MPEG4) {
+ return std::make_shared<Mpeg4ProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_VP8) {
+ return std::make_shared<Vp8ProfileLevelMapper>();
+ } else if (mediaType == MIMETYPE_VIDEO_VP9) {
+ return std::make_shared<Vp9ProfileLevelMapper>();
+ }
+ return nullptr;
+}
+
+// static
+bool C2Mapper::map(C2Config::bitrate_mode_t from, int32_t *to) {
+ return sBitrateModes.map(from, to);
+}
+
+// static
+bool C2Mapper::map(int32_t from, C2Config::bitrate_mode_t *to) {
+ return sBitrateModes.map(from, to);
+}
+
+// static
+bool C2Mapper::map(C2Config::pcm_encoding_t from, int32_t *to) {
+ return sPcmEncodings.map(from, to);
+}
+
+// static
+bool C2Mapper::map(int32_t from, C2Config::pcm_encoding_t *to) {
+ return sPcmEncodings.map(from, to);
+}
+
+// static
+bool C2Mapper::map(C2Color::range_t from, int32_t *to) {
+ bool res = true;
+ // map SDK defined values directly. For other values, use wrapping from ColorUtils.
+ if (!sColorRanges.map(from, to)) {
+ ColorAspects::Range sfRange;
+
+ // map known constants and keep vendor extensions. all other values are mapped to 'Other'
+ if (!sColorRangesSf.map(from, &sfRange)) {
+ // use static cast and ensure it is in the extension range
+ if (from < C2Color::RANGE_VENDOR_START || from > C2Color::RANGE_OTHER) {
+ sfRange = ColorAspects::RangeOther;
+ res = false;
+ }
+ }
+
+ *to = ColorUtils::wrapColorAspectsIntoColorRange(sfRange);
+ }
+ return res;
+}
+
+// static
+bool C2Mapper::map(int32_t from, C2Color::range_t *to) {
+ // map SDK defined values directly. For other values, use wrapping from ColorUtils.
+ if (!sColorRanges.map(from, to)) {
+ ColorAspects::Range sfRange;
+ (void)ColorUtils::unwrapColorAspectsFromColorRange(from, &sfRange);
+
+ // map known constants and keep vendor extensions. all other values are mapped to 'Other'
+ if (!sColorRangesSf.map(sfRange, to)) {
+ // use static cast and ensure it is in the extension range
+ *to = (C2Color::range_t)sfRange;
+ if (*to < C2Color::RANGE_VENDOR_START || *to > C2Color::RANGE_OTHER) {
+ *to = C2Color::RANGE_OTHER;
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+// static
+bool C2Mapper::map(C2Color::range_t from, ColorAspects::Range *to) {
+ return sColorRangesSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(ColorAspects::Range from, C2Color::range_t *to) {
+ return sColorRangesSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(C2Color::primaries_t primaries, C2Color::matrix_t matrix, int32_t *standard) {
+ ColorAspects::Primaries sfPrimaries;
+ ColorAspects::MatrixCoeffs sfMatrix;
+ bool res = true;
+
+ // map known constants and keep vendor extensions. all other values are mapped to 'Other'
+ if (!sColorPrimariesSf.map(primaries, &sfPrimaries)) {
+ // ensure it is in the extension range and use static cast
+ if (primaries < C2Color::PRIMARIES_VENDOR_START || primaries > C2Color::PRIMARIES_OTHER) {
+ // undefined non-extension values map to 'Other'
+ sfPrimaries = ColorAspects::PrimariesOther;
+ res = false;
+ } else {
+ sfPrimaries = (ColorAspects::Primaries)primaries;
+ }
+ }
+
+ if (!sColorMatricesSf.map(matrix, &sfMatrix)) {
+ // use static cast and ensure it is in the extension range
+ if (matrix < C2Color::MATRIX_VENDOR_START || matrix > C2Color::MATRIX_OTHER) {
+ // undefined non-extension values map to 'Other'
+ sfMatrix = ColorAspects::MatrixOther;
+ res = false;
+ } else {
+ sfMatrix = (ColorAspects::MatrixCoeffs)matrix;
+ }
+ }
+
+ *standard = ColorUtils::wrapColorAspectsIntoColorStandard(sfPrimaries, sfMatrix);
+
+ return res;
+}
+
+// static
+bool C2Mapper::map(int32_t standard, C2Color::primaries_t *primaries, C2Color::matrix_t *matrix) {
+ // first map to stagefright foundation aspects => these actually map nearly 1:1 to
+ // Codec 2.0 aspects
+ ColorAspects::Primaries sfPrimaries;
+ ColorAspects::MatrixCoeffs sfMatrix;
+ bool res = true;
+ (void)ColorUtils::unwrapColorAspectsFromColorStandard(standard, &sfPrimaries, &sfMatrix);
+
+ // map known constants and keep vendor extensions. all other values are mapped to 'Other'
+ if (!sColorPrimariesSf.map(sfPrimaries, primaries)) {
+ // use static cast and ensure it is in the extension range
+ *primaries = (C2Color::primaries_t)sfPrimaries;
+ if (*primaries < C2Color::PRIMARIES_VENDOR_START || *primaries > C2Color::PRIMARIES_OTHER) {
+ *primaries = C2Color::PRIMARIES_OTHER;
+ res = false;
+ }
+ }
+
+ if (!sColorMatricesSf.map(sfMatrix, matrix)) {
+ // use static cast and ensure it is in the extension range
+ *matrix = (C2Color::matrix_t)sfMatrix;
+ if (*matrix < C2Color::MATRIX_VENDOR_START || *matrix > C2Color::MATRIX_OTHER) {
+ *matrix = C2Color::MATRIX_OTHER;
+ res = false;
+ }
+ }
+
+ return res;
+}
+
+// static
+bool C2Mapper::map(C2Color::primaries_t from, ColorAspects::Primaries *to) {
+ return sColorPrimariesSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(ColorAspects::Primaries from, C2Color::primaries_t *to) {
+ return sColorPrimariesSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(C2Color::matrix_t from, ColorAspects::MatrixCoeffs *to) {
+ return sColorMatricesSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(ColorAspects::MatrixCoeffs from, C2Color::matrix_t *to) {
+ return sColorMatricesSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(C2Color::transfer_t from, int32_t *to) {
+ bool res = true;
+ // map SDK defined values directly. For other values, use wrapping from ColorUtils.
+ if (!sColorTransfers.map(from, to)) {
+ ColorAspects::Transfer sfTransfer;
+
+ // map known constants and keep vendor extensions. all other values are mapped to 'Other'
+ if (!sColorTransfersSf.map(from, &sfTransfer)) {
+ // use static cast and ensure it is in the extension range
+ if (from < C2Color::TRANSFER_VENDOR_START || from > C2Color::TRANSFER_OTHER) {
+ sfTransfer = ColorAspects::TransferOther;
+ res = false;
+ }
+ }
+
+ *to = ColorUtils::wrapColorAspectsIntoColorTransfer(sfTransfer);
+ }
+ return res;
+}
+
+// static
+bool C2Mapper::map(int32_t from, C2Color::transfer_t *to) {
+ // map SDK defined values directly. For other values, use wrapping from ColorUtils.
+ if (!sColorTransfers.map(from, to)) {
+ ColorAspects::Transfer sfTransfer;
+ (void)ColorUtils::unwrapColorAspectsFromColorTransfer(from, &sfTransfer);
+
+ // map known constants and keep vendor extensions. all other values are mapped to 'Other'
+ if (!sColorTransfersSf.map(sfTransfer, to)) {
+ // use static cast and ensure it is in the extension range
+ *to = (C2Color::transfer_t)sfTransfer;
+ if (*to < C2Color::TRANSFER_VENDOR_START || *to > C2Color::TRANSFER_OTHER) {
+ *to = C2Color::TRANSFER_OTHER;
+ return false;
+ }
+ }
+ }
+
+ return true;
+}
+
+// static
+bool C2Mapper::map(
+ C2Color::range_t range, C2Color::primaries_t primaries,
+ C2Color::matrix_t matrix, C2Color::transfer_t transfer, uint32_t *dataSpace) {
+#if 0
+ // pure reimplementation
+ *dataSpace = HAL_DATASPACE_UNKNOWN; // this is 0
+
+ switch (range) {
+ case C2Color::RANGE_FULL: *dataSpace |= HAL_DATASPACE_RANGE_FULL; break;
+ case C2Color::RANGE_LIMITED: *dataSpace |= HAL_DATASPACE_RANGE_LIMITED; break;
+ default: break;
+ }
+
+ switch (transfer) {
+ case C2Color::TRANSFER_LINEAR: *dataSpace |= HAL_DATASPACE_TRANSFER_LINEAR; break;
+ case C2Color::TRANSFER_SRGB: *dataSpace |= HAL_DATASPACE_TRANSFER_SRGB; break;
+ case C2Color::TRANSFER_170M: *dataSpace |= HAL_DATASPACE_TRANSFER_SMPTE_170M; break;
+ case C2Color::TRANSFER_GAMMA22: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_2; break;
+ case C2Color::TRANSFER_GAMMA28: *dataSpace |= HAL_DATASPACE_TRANSFER_GAMMA2_8; break;
+ case C2Color::TRANSFER_ST2084: *dataSpace |= HAL_DATASPACE_TRANSFER_ST2084; break;
+ case C2Color::TRANSFER_HLG: *dataSpace |= HAL_DATASPACE_TRANSFER_HLG; break;
+ default: break;
+ }
+
+ switch (primaries) {
+ case C2Color::PRIMARIES_BT601_525:
+ *dataSpace |= (matrix == C2Color::MATRIX_SMPTE240M
+ || matrix == C2Color::MATRIX_BT709)
+ ? HAL_DATASPACE_STANDARD_BT601_525_UNADJUSTED
+ : HAL_DATASPACE_STANDARD_BT601_525;
+ break;
+ case C2Color::PRIMARIES_BT601_625:
+ *dataSpace |= (matrix == C2Color::MATRIX_SMPTE240M
+ || matrix == C2Color::MATRIX_BT709)
+ ? HAL_DATASPACE_STANDARD_BT601_625_UNADJUSTED
+ : HAL_DATASPACE_STANDARD_BT601_625;
+ break;
+ case C2Color::PRIMARIES_BT2020:
+ *dataSpace |= (matrix == C2Color::MATRIX_BT2020CONSTANT
+ ? HAL_DATASPACE_STANDARD_BT2020_CONSTANT_LUMINANCE
+ : HAL_DATASPACE_STANDARD_BT2020);
+ break;
+ case C2Color::PRIMARIES_BT470_M:
+ *dataSpace |= HAL_DATASPACE_STANDARD_BT470M;
+ break;
+ case C2Color::PRIMARIES_BT709:
+ *dataSpace |= HAL_DATASPACE_STANDARD_BT709;
+ break;
+ default: break;
+ }
+#else
+ // for now use legacy implementation
+ ColorAspects aspects;
+ if (!sColorRangesSf.map(range, &aspects.mRange)) {
+ aspects.mRange = ColorAspects::RangeUnspecified;
+ }
+ if (!sColorPrimariesSf.map(primaries, &aspects.mPrimaries)) {
+ aspects.mPrimaries = ColorAspects::PrimariesUnspecified;
+ }
+ if (!sColorMatricesSf.map(matrix, &aspects.mMatrixCoeffs)) {
+ aspects.mMatrixCoeffs = ColorAspects::MatrixUnspecified;
+ }
+ if (!sColorTransfersSf.map(transfer, &aspects.mTransfer)) {
+ aspects.mTransfer = ColorAspects::TransferUnspecified;
+ }
+ *dataSpace = ColorUtils::getDataSpaceForColorAspects(aspects, true /* mayExpand */);
+#endif
+ return true;
+}
+
+// static
+bool C2Mapper::map(C2Color::transfer_t from, ColorAspects::Transfer *to) {
+ return sColorTransfersSf.map(from, to);
+}
+
+// static
+bool C2Mapper::map(ColorAspects::Transfer from, C2Color::transfer_t *to) {
+ return sColorTransfersSf.map(from, to);
+}
diff --git a/media/codec2/sfplugin/utils/Codec2Mapper.h b/media/codec2/sfplugin/utils/Codec2Mapper.h
new file mode 100644
index 0000000..1eeb92e
--- /dev/null
+++ b/media/codec2/sfplugin/utils/Codec2Mapper.h
@@ -0,0 +1,78 @@
+/*
+ * Copyright 2018 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef ANDROID_CODEC2_MAPPER_H_
+#define ANDROID_CODEC2_MAPPER_H_
+
+#include <C2Config.h>
+
+#include <media/stagefright/foundation/ColorUtils.h>
+
+#include <memory>
+
+namespace android {
+
+ /**
+ * Utility class to map Codec 2.0 values to android values.
+ */
+ struct C2Mapper {
+ struct ProfileLevelMapper {
+ virtual bool mapProfile(C2Config::profile_t, int32_t*) = 0;
+ virtual bool mapProfile(int32_t, C2Config::profile_t*) = 0;
+ virtual bool mapLevel(C2Config::level_t, int32_t*) = 0;
+ virtual bool mapLevel(int32_t, C2Config::level_t*) = 0;
+ virtual ~ProfileLevelMapper() = default;
+ };
+
+ static std::shared_ptr<ProfileLevelMapper>
+ GetProfileLevelMapper(std::string mediaType);
+
+ // convert between bitrates
+ static bool map(C2Config::bitrate_mode_t, int32_t*);
+ static bool map(int32_t, C2Config::bitrate_mode_t*);
+
+ // convert between pcm encodings
+ static bool map(C2Config::pcm_encoding_t, int32_t*);
+ static bool map(int32_t, C2Config::pcm_encoding_t*);
+
+ // convert between picture types
+ static bool map(C2Config::picture_type_t, int32_t*);
+ static bool map(int32_t, C2Config::picture_type_t*);
+
+ // convert between color aspects
+ static bool map(C2Color::range_t, int32_t*);
+ static bool map(int32_t, C2Color::range_t*);
+ static bool map(C2Color::primaries_t, C2Color::matrix_t, int32_t*);
+ static bool map(int32_t, C2Color::primaries_t*, C2Color::matrix_t*);
+ static bool map(C2Color::transfer_t, int32_t*);
+ static bool map(int32_t, C2Color::transfer_t*);
+
+ static bool map(
+ C2Color::range_t, C2Color::primaries_t, C2Color::matrix_t, C2Color::transfer_t,
+ uint32_t *dataSpace);
+
+ static bool map(C2Color::range_t, ColorAspects::Range*);
+ static bool map(ColorAspects::Range, C2Color::range_t*);
+ static bool map(C2Color::primaries_t, ColorAspects::Primaries*);
+ static bool map(ColorAspects::Primaries, C2Color::primaries_t*);
+ static bool map(C2Color::matrix_t, ColorAspects::MatrixCoeffs*);
+ static bool map(ColorAspects::MatrixCoeffs, C2Color::matrix_t*);
+ static bool map(C2Color::transfer_t, ColorAspects::Transfer*);
+ static bool map(ColorAspects::Transfer, C2Color::transfer_t*);
+ };
+}
+
+#endif // ANDROID_CODEC2_MAPPER_H_