1/*
2 * Copyright 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//#define LOG_NDEBUG 0
18#define LOG_TAG "C2SoftVp8Enc"
19#include <utils/Log.h>
20#include <utils/misc.h>
21
22#include "C2SoftVp8Enc.h"
23
24namespace android {
25
26constexpr char COMPONENT_NAME[] = "c2.android.vp8.encoder";
27
28C2SoftVp8Enc::C2SoftVp8Enc(const char* name, c2_node_id_t id,
29                           const std::shared_ptr<IntfImpl>& intfImpl)
30    : C2SoftVpxEnc(name, id, intfImpl), mDCTPartitions(0), mProfile(1) {}
31
32void C2SoftVp8Enc::setCodecSpecificInterface() {
33    mCodecInterface = vpx_codec_vp8_cx();
34}
35
36void C2SoftVp8Enc::setCodecSpecificConfiguration() {
37    switch (mProfile) {
38        case 1:
39            mCodecConfiguration->g_profile = 0;
40            break;
41
42        case 2:
43            mCodecConfiguration->g_profile = 1;
44            break;
45
46        case 4:
47            mCodecConfiguration->g_profile = 2;
48            break;
49
50        case 8:
51            mCodecConfiguration->g_profile = 3;
52            break;
53
54        default:
55            mCodecConfiguration->g_profile = 0;
56    }
57}
58
59vpx_codec_err_t C2SoftVp8Enc::setCodecSpecificControls() {
60    vpx_codec_err_t codec_return = vpx_codec_control(mCodecContext,
61                                                     VP8E_SET_TOKEN_PARTITIONS,
62                                                     mDCTPartitions);
63    if (codec_return != VPX_CODEC_OK) {
64        ALOGE("Error setting dct partitions for vpx encoder.");
65    }
66    return codec_return;
67}
68
69class C2SoftVp8EncFactory : public C2ComponentFactory {
70public:
71    C2SoftVp8EncFactory()
72        : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
73              GetCodec2PlatformComponentStore()->getParamReflector())) {}
74
75    virtual c2_status_t createComponent(
76            c2_node_id_t id,
77            std::shared_ptr<C2Component>* const component,
78            std::function<void(C2Component*)> deleter) override {
79        *component = std::shared_ptr<C2Component>(
80            new C2SoftVp8Enc(COMPONENT_NAME, id,
81                             std::make_shared<C2SoftVpxEnc::IntfImpl>(mHelper)),
82            deleter);
83        return C2_OK;
84    }
85
86    virtual c2_status_t createInterface(
87            c2_node_id_t id,
88            std::shared_ptr<C2ComponentInterface>* const interface,
89            std::function<void(C2ComponentInterface*)> deleter) override {
90        *interface = std::shared_ptr<C2ComponentInterface>(
91            new SimpleInterface<C2SoftVpxEnc::IntfImpl>(
92                COMPONENT_NAME, id,
93                std::make_shared<C2SoftVpxEnc::IntfImpl>(mHelper)),
94            deleter);
95        return C2_OK;
96    }
97
98    virtual ~C2SoftVp8EncFactory() override = default;
99
100private:
101    std::shared_ptr<C2ReflectorHelper> mHelper;
102};
103
104}  // namespace android
105
106extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
107    ALOGV("in %s", __func__);
108    return new ::android::C2SoftVp8EncFactory();
109}
110
111extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
112    ALOGV("in %s", __func__);
113    delete factory;
114}
115