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 "C2SoftVp9Enc"
19 #include <utils/Log.h>
20 #include <utils/misc.h>
21 
22 #include "C2SoftVp9Enc.h"
23 
24 namespace android {
25 
26 constexpr char COMPONENT_NAME[] = "c2.android.vp9.encoder";
27 
C2SoftVp9Enc(const char * name,c2_node_id_t id,const std::shared_ptr<IntfImpl> & intfImpl)28 C2SoftVp9Enc::C2SoftVp9Enc(const char* name, c2_node_id_t id,
29                            const std::shared_ptr<IntfImpl>& intfImpl)
30     : C2SoftVpxEnc(name, id, intfImpl),
31       mProfile(1),
32       mLevel(0),
33       mTileColumns(0),
34       mFrameParallelDecoding(false) {
35 }
36 
setCodecSpecificInterface()37 void C2SoftVp9Enc::setCodecSpecificInterface() {
38     mCodecInterface = vpx_codec_vp9_cx();
39 }
40 
setCodecSpecificConfiguration()41 void C2SoftVp9Enc::setCodecSpecificConfiguration() {
42     switch (mProfile) {
43         case 1:
44             mCodecConfiguration->g_profile = 0;
45             break;
46 
47         case 2:
48             mCodecConfiguration->g_profile = 1;
49             break;
50 
51         case 4:
52             mCodecConfiguration->g_profile = 2;
53             break;
54 
55         case 8:
56             mCodecConfiguration->g_profile = 3;
57             break;
58 
59         default:
60             mCodecConfiguration->g_profile = 0;
61     }
62 }
63 
setCodecSpecificControls()64 vpx_codec_err_t C2SoftVp9Enc::setCodecSpecificControls() {
65     vpx_codec_err_t codecReturn = vpx_codec_control(
66             mCodecContext, VP9E_SET_TILE_COLUMNS, mTileColumns);
67     if (codecReturn != VPX_CODEC_OK) {
68         ALOGE("Error setting VP9E_SET_TILE_COLUMNS to %d. vpx_codec_control() "
69               "returned %d", mTileColumns, codecReturn);
70         return codecReturn;
71     }
72     codecReturn = vpx_codec_control(
73             mCodecContext, VP9E_SET_FRAME_PARALLEL_DECODING,
74             mFrameParallelDecoding);
75     if (codecReturn != VPX_CODEC_OK) {
76         ALOGE("Error setting VP9E_SET_FRAME_PARALLEL_DECODING to %d."
77               "vpx_codec_control() returned %d", mFrameParallelDecoding,
78               codecReturn);
79         return codecReturn;
80     }
81     codecReturn = vpx_codec_control(mCodecContext, VP9E_SET_ROW_MT, 1);
82     if (codecReturn != VPX_CODEC_OK) {
83         ALOGE("Error setting VP9E_SET_ROW_MT to 1. vpx_codec_control() "
84               "returned %d", codecReturn);
85         return codecReturn;
86     }
87 
88     // For VP9, we always set CPU_USED to 8 (because the realtime default is 0
89     // which is too slow).
90     codecReturn = vpx_codec_control(mCodecContext, VP8E_SET_CPUUSED, 8);
91     if (codecReturn != VPX_CODEC_OK) {
92         ALOGE("Error setting VP8E_SET_CPUUSED to 8. vpx_codec_control() "
93               "returned %d", codecReturn);
94         return codecReturn;
95     }
96     return codecReturn;
97 }
98 
99 class C2SoftVp9EncFactory : public C2ComponentFactory {
100 public:
C2SoftVp9EncFactory()101     C2SoftVp9EncFactory()
102         : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
103               GetCodec2PlatformComponentStore()->getParamReflector())) {}
104 
createComponent(c2_node_id_t id,std::shared_ptr<C2Component> * const component,std::function<void (C2Component *)> deleter)105     virtual c2_status_t createComponent(
106             c2_node_id_t id,
107             std::shared_ptr<C2Component>* const component,
108             std::function<void(C2Component*)> deleter) override {
109         *component = std::shared_ptr<C2Component>(
110             new C2SoftVp9Enc(COMPONENT_NAME, id,
111                              std::make_shared<C2SoftVpxEnc::IntfImpl>(mHelper)),
112             deleter);
113         return C2_OK;
114     }
115 
createInterface(c2_node_id_t id,std::shared_ptr<C2ComponentInterface> * const interface,std::function<void (C2ComponentInterface *)> deleter)116     virtual c2_status_t createInterface(
117             c2_node_id_t id,
118             std::shared_ptr<C2ComponentInterface>* const interface,
119             std::function<void(C2ComponentInterface*)> deleter) override {
120         *interface = std::shared_ptr<C2ComponentInterface>(
121             new SimpleInterface<C2SoftVpxEnc::IntfImpl>(
122                 COMPONENT_NAME, id,
123                 std::make_shared<C2SoftVpxEnc::IntfImpl>(mHelper)),
124             deleter);
125         return C2_OK;
126     }
127 
128     virtual ~C2SoftVp9EncFactory() override = default;
129 
130 private:
131     std::shared_ptr<C2ReflectorHelper> mHelper;
132 };
133 
134 }  // namespace android
135 
CreateCodec2Factory()136 extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
137     ALOGV("in %s", __func__);
138     return new ::android::C2SoftVp9EncFactory();
139 }
140 
DestroyCodec2Factory(::C2ComponentFactory * factory)141 extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
142     ALOGV("in %s", __func__);
143     delete factory;
144 }
145