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