1 /* 2 * Copyright (C) 2016 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 "SoftVP8Encoder" 19 #include "SoftVP8Encoder.h" 20 21 #include <utils/Log.h> 22 #include <utils/misc.h> 23 24 #include <media/hardware/HardwareAPI.h> 25 #include <media/hardware/MetadataBufferType.h> 26 #include <media/stagefright/foundation/ADebug.h> 27 #include <media/stagefright/MediaDefs.h> 28 29 #ifndef INT32_MAX 30 #define INT32_MAX 2147483647 31 #endif 32 33 namespace android { 34 35 static const CodecProfileLevel kVp8ProfileLevels[] = { 36 { OMX_VIDEO_VP8ProfileMain, OMX_VIDEO_VP8Level_Version0 }, 37 { OMX_VIDEO_VP8ProfileMain, OMX_VIDEO_VP8Level_Version1 }, 38 { OMX_VIDEO_VP8ProfileMain, OMX_VIDEO_VP8Level_Version2 }, 39 { OMX_VIDEO_VP8ProfileMain, OMX_VIDEO_VP8Level_Version3 }, 40 }; 41 42 SoftVP8Encoder::SoftVP8Encoder(const char *name, 43 const OMX_CALLBACKTYPE *callbacks, 44 OMX_PTR appData, 45 OMX_COMPONENTTYPE **component) 46 : SoftVPXEncoder( 47 name, callbacks, appData, component, "video_encoder.vp8", 48 OMX_VIDEO_CodingVP8, MEDIA_MIMETYPE_VIDEO_VP8, 2, 49 kVp8ProfileLevels, NELEM(kVp8ProfileLevels)), 50 mDCTPartitions(0), 51 mLevel(OMX_VIDEO_VP8Level_Version0) { 52 } 53 54 void SoftVP8Encoder::setCodecSpecificInterface() { 55 mCodecInterface = vpx_codec_vp8_cx(); 56 } 57 58 void SoftVP8Encoder::setCodecSpecificConfiguration() { 59 switch (mLevel) { 60 case OMX_VIDEO_VP8Level_Version0: 61 mCodecConfiguration->g_profile = 0; 62 break; 63 64 case OMX_VIDEO_VP8Level_Version1: 65 mCodecConfiguration->g_profile = 1; 66 break; 67 68 case OMX_VIDEO_VP8Level_Version2: 69 mCodecConfiguration->g_profile = 2; 70 break; 71 72 case OMX_VIDEO_VP8Level_Version3: 73 mCodecConfiguration->g_profile = 3; 74 break; 75 76 default: 77 mCodecConfiguration->g_profile = 0; 78 } 79 } 80 81 vpx_codec_err_t SoftVP8Encoder::setCodecSpecificControls() { 82 vpx_codec_err_t codec_return = vpx_codec_control(mCodecContext, 83 VP8E_SET_TOKEN_PARTITIONS, 84 mDCTPartitions); 85 if (codec_return != VPX_CODEC_OK) { 86 ALOGE("Error setting dct partitions for vpx encoder."); 87 } 88 return codec_return; 89 } 90 91 OMX_ERRORTYPE SoftVP8Encoder::internalGetParameter(OMX_INDEXTYPE index, 92 OMX_PTR param) { 93 // can include extension index OMX_INDEXEXTTYPE 94 const int32_t indexFull = index; 95 96 switch (indexFull) { 97 case OMX_IndexParamVideoVp8: 98 return internalGetVp8Params( 99 (OMX_VIDEO_PARAM_VP8TYPE *)param); 100 101 default: 102 return SoftVPXEncoder::internalGetParameter(index, param); 103 } 104 } 105 106 OMX_ERRORTYPE SoftVP8Encoder::internalSetParameter(OMX_INDEXTYPE index, 107 const OMX_PTR param) { 108 // can include extension index OMX_INDEXEXTTYPE 109 const int32_t indexFull = index; 110 111 switch (indexFull) { 112 case OMX_IndexParamVideoVp8: 113 return internalSetVp8Params( 114 (const OMX_VIDEO_PARAM_VP8TYPE *)param); 115 116 default: 117 return SoftVPXEncoder::internalSetParameter(index, param); 118 } 119 } 120 121 OMX_ERRORTYPE SoftVP8Encoder::internalGetVp8Params( 122 OMX_VIDEO_PARAM_VP8TYPE* vp8Params) { 123 if (vp8Params->nPortIndex != kOutputPortIndex) { 124 return OMX_ErrorUnsupportedIndex; 125 } 126 127 vp8Params->eProfile = OMX_VIDEO_VP8ProfileMain; 128 vp8Params->eLevel = mLevel; 129 vp8Params->bErrorResilientMode = mErrorResilience; 130 vp8Params->nDCTPartitions = mDCTPartitions; 131 return OMX_ErrorNone; 132 } 133 134 OMX_ERRORTYPE SoftVP8Encoder::internalSetVp8Params( 135 const OMX_VIDEO_PARAM_VP8TYPE* vp8Params) { 136 if (vp8Params->nPortIndex != kOutputPortIndex) { 137 return OMX_ErrorUnsupportedIndex; 138 } 139 140 if (vp8Params->eProfile != OMX_VIDEO_VP8ProfileMain) { 141 return OMX_ErrorBadParameter; 142 } 143 144 if (vp8Params->eLevel == OMX_VIDEO_VP8Level_Version0 || 145 vp8Params->eLevel == OMX_VIDEO_VP8Level_Version1 || 146 vp8Params->eLevel == OMX_VIDEO_VP8Level_Version2 || 147 vp8Params->eLevel == OMX_VIDEO_VP8Level_Version3) { 148 mLevel = vp8Params->eLevel; 149 } else { 150 return OMX_ErrorBadParameter; 151 } 152 153 mErrorResilience = vp8Params->bErrorResilientMode; 154 if (vp8Params->nDCTPartitions <= kMaxDCTPartitions) { 155 mDCTPartitions = vp8Params->nDCTPartitions; 156 } else { 157 return OMX_ErrorBadParameter; 158 } 159 return OMX_ErrorNone; 160 } 161 162 } // namespace android 163