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