summaryrefslogtreecommitdiff
path: root/media/codec2/components/vpx/C2SoftVp9Enc.cpp
blob: 740dbda6e564cc567bf6ed7bbc3744c9c2c5cb66 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
 * 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 "C2SoftVp9Enc"
#include <utils/Log.h>
#include <utils/misc.h>

#include "C2SoftVp9Enc.h"

namespace android {

C2SoftVp9Enc::C2SoftVp9Enc(const char* name, c2_node_id_t id,
                           const std::shared_ptr<IntfImpl>& intfImpl)
    : C2SoftVpxEnc(name, id, intfImpl),
      mProfile(1),
      mLevel(0),
      mTileColumns(0),
      mFrameParallelDecoding(false) {
}

void C2SoftVp9Enc::setCodecSpecificInterface() {
    mCodecInterface = vpx_codec_vp9_cx();
}

void C2SoftVp9Enc::setCodecSpecificConfiguration() {
    switch (mProfile) {
        case 1:
            mCodecConfiguration->g_profile = 0;
            break;

        case 2:
            mCodecConfiguration->g_profile = 1;
            break;

        case 4:
            mCodecConfiguration->g_profile = 2;
            break;

        case 8:
            mCodecConfiguration->g_profile = 3;
            break;

        default:
            mCodecConfiguration->g_profile = 0;
    }
}

vpx_codec_err_t C2SoftVp9Enc::setCodecSpecificControls() {
    vpx_codec_err_t codecReturn = vpx_codec_control(
            mCodecContext, VP9E_SET_TILE_COLUMNS, mTileColumns);
    if (codecReturn != VPX_CODEC_OK) {
        ALOGE("Error setting VP9E_SET_TILE_COLUMNS to %d. vpx_codec_control() "
              "returned %d", mTileColumns, codecReturn);
        return codecReturn;
    }
    codecReturn = vpx_codec_control(
            mCodecContext, VP9E_SET_FRAME_PARALLEL_DECODING,
            mFrameParallelDecoding);
    if (codecReturn != VPX_CODEC_OK) {
        ALOGE("Error setting VP9E_SET_FRAME_PARALLEL_DECODING to %d."
              "vpx_codec_control() returned %d", mFrameParallelDecoding,
              codecReturn);
        return codecReturn;
    }
    codecReturn = vpx_codec_control(mCodecContext, VP9E_SET_ROW_MT, 1);
    if (codecReturn != VPX_CODEC_OK) {
        ALOGE("Error setting VP9E_SET_ROW_MT to 1. vpx_codec_control() "
              "returned %d", codecReturn);
        return codecReturn;
    }

    // For VP9, we always set CPU_USED to 8 (because the realtime default is 0
    // which is too slow).
    codecReturn = vpx_codec_control(mCodecContext, VP8E_SET_CPUUSED, 8);
    if (codecReturn != VPX_CODEC_OK) {
        ALOGE("Error setting VP8E_SET_CPUUSED to 8. vpx_codec_control() "
              "returned %d", codecReturn);
        return codecReturn;
    }
    return codecReturn;
}

class C2SoftVp9EncFactory : public C2ComponentFactory {
public:
    C2SoftVp9EncFactory()
        : mHelper(std::static_pointer_cast<C2ReflectorHelper>(
              GetCodec2PlatformComponentStore()->getParamReflector())) {}

    virtual c2_status_t createComponent(
            c2_node_id_t id,
            std::shared_ptr<C2Component>* const component,
            std::function<void(C2Component*)> deleter) override {
        *component = std::shared_ptr<C2Component>(
            new C2SoftVp9Enc(COMPONENT_NAME, id,
                             std::make_shared<C2SoftVpxEnc::IntfImpl>(mHelper)),
            deleter);
        return C2_OK;
    }

    virtual c2_status_t createInterface(
            c2_node_id_t id,
            std::shared_ptr<C2ComponentInterface>* const interface,
            std::function<void(C2ComponentInterface*)> deleter) override {
        *interface = std::shared_ptr<C2ComponentInterface>(
            new SimpleInterface<C2SoftVpxEnc::IntfImpl>(
                COMPONENT_NAME, id,
                std::make_shared<C2SoftVpxEnc::IntfImpl>(mHelper)),
            deleter);
        return C2_OK;
    }

    virtual ~C2SoftVp9EncFactory() override = default;

private:
    std::shared_ptr<C2ReflectorHelper> mHelper;
};

}  // namespace android

extern "C" ::C2ComponentFactory* CreateCodec2Factory() {
    ALOGV("in %s", __func__);
    return new ::android::C2SoftVp9EncFactory();
}

extern "C" void DestroyCodec2Factory(::C2ComponentFactory* factory) {
    ALOGV("in %s", __func__);
    delete factory;
}