SoftVPXEncoder.h revision 4154795d5526750b7aec5f774831a9e1ba0a3f15
1/* 2 * Copyright (C) 2013 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#ifndef SOFT_VPX_ENCODER_H_ 18 19#define SOFT_VPX_ENCODER_H_ 20 21#include "SimpleSoftOMXComponent.h" 22 23#include <OMX_VideoExt.h> 24#include <OMX_IndexExt.h> 25 26#include <hardware/gralloc.h> 27 28#include "vpx/vpx_encoder.h" 29#include "vpx/vpx_codec.h" 30#include "vpx/vp8cx.h" 31 32namespace android { 33 34// Exposes a vpx encoder as an OMX Component 35// 36// Boilerplate for callback bindings are taken care 37// by the base class SimpleSoftOMXComponent and its 38// parent SoftOMXComponent. 39// 40// Only following encoder settings are available 41// - target bitrate 42// - rate control (constant / variable) 43// - frame rate 44// - error resilience 45// - token partitioning 46// - reconstruction & loop filters (g_profile) 47// 48// Only following color formats are recognized 49// - YUV420Planar 50// - YUV420SemiPlanar 51// - AndroidOpaque 52// 53// Following settings are not configurable by the client 54// - encoding deadline is realtime 55// - multithreaded encoding utilizes a number of threads equal 56// to online cpu's available 57// - the algorithm interface for encoder is vp8 58// - fractional bits of frame rate is discarded 59// - OMX timestamps are in microseconds, therefore 60// encoder timebase is fixed to 1/1000000 61 62struct SoftVPXEncoder : public SimpleSoftOMXComponent { 63 SoftVPXEncoder(const char *name, 64 const OMX_CALLBACKTYPE *callbacks, 65 OMX_PTR appData, 66 OMX_COMPONENTTYPE **component); 67 68protected: 69 virtual ~SoftVPXEncoder(); 70 71 // Returns current values for requested OMX 72 // parameters 73 virtual OMX_ERRORTYPE internalGetParameter( 74 OMX_INDEXTYPE index, OMX_PTR param); 75 76 // Validates, extracts and stores relevant OMX 77 // parameters 78 virtual OMX_ERRORTYPE internalSetParameter( 79 OMX_INDEXTYPE index, const OMX_PTR param); 80 81 virtual OMX_ERRORTYPE setConfig( 82 OMX_INDEXTYPE index, const OMX_PTR params); 83 84 // OMX callback when buffers available 85 // Note that both an input and output buffer 86 // is expected to be available to carry out 87 // encoding of the frame 88 virtual void onQueueFilled(OMX_U32 portIndex); 89 90 virtual OMX_ERRORTYPE getExtensionIndex( 91 const char *name, OMX_INDEXTYPE *index); 92 93private: 94 enum { 95 kStoreMetaDataExtensionIndex = OMX_IndexVendorStartUnused + 1, 96 }; 97 98 enum TemporalReferences { 99 // For 1 layer case: reference all (last, golden, and alt ref), but only 100 // update last. 101 kTemporalUpdateLastRefAll = 12, 102 // First base layer frame for 3 temporal layers, which updates last and 103 // golden with alt ref dependency. 104 kTemporalUpdateLastAndGoldenRefAltRef = 11, 105 // First enhancement layer with alt ref dependency. 106 kTemporalUpdateGoldenRefAltRef = 10, 107 // First enhancement layer with alt ref dependency. 108 kTemporalUpdateGoldenWithoutDependencyRefAltRef = 9, 109 // Base layer with alt ref dependency. 110 kTemporalUpdateLastRefAltRef = 8, 111 // Highest enhacement layer without dependency on golden with alt ref 112 // dependency. 113 kTemporalUpdateNoneNoRefGoldenRefAltRef = 7, 114 // Second layer and last frame in cycle, for 2 layers. 115 kTemporalUpdateNoneNoRefAltref = 6, 116 // Highest enhancement layer. 117 kTemporalUpdateNone = 5, 118 // Second enhancement layer. 119 kTemporalUpdateAltref = 4, 120 // Second enhancement layer without dependency on previous frames in 121 // the second enhancement layer. 122 kTemporalUpdateAltrefWithoutDependency = 3, 123 // First enhancement layer. 124 kTemporalUpdateGolden = 2, 125 // First enhancement layer without dependency on previous frames in 126 // the first enhancement layer. 127 kTemporalUpdateGoldenWithoutDependency = 1, 128 // Base layer. 129 kTemporalUpdateLast = 0, 130 }; 131 enum { 132 kMaxTemporalPattern = 8 133 }; 134 135 // number of buffers allocated per port 136 static const uint32_t kNumBuffers = 4; 137 138 // OMX port indexes that refer to input and 139 // output ports respectively 140 static const uint32_t kInputPortIndex = 0; 141 static const uint32_t kOutputPortIndex = 1; 142 143 // Byte-alignment required for buffers 144 static const uint32_t kInputBufferAlignment = 1; 145 static const uint32_t kOutputBufferAlignment = 2; 146 147 // Max value supported for DCT partitions 148 static const uint32_t kMaxDCTPartitions = 3; 149 150 // Number of supported input color formats 151 static const uint32_t kNumberOfSupportedColorFormats = 3; 152 153 // vpx specific opaque data structure that 154 // stores encoder state 155 vpx_codec_ctx_t* mCodecContext; 156 157 // vpx specific data structure that 158 // stores encoder configuration 159 vpx_codec_enc_cfg_t* mCodecConfiguration; 160 161 // vpx specific read-only data structure 162 // that specifies algorithm interface (e.g. vp8) 163 vpx_codec_iface_t* mCodecInterface; 164 165 // Width of the input frames 166 int32_t mWidth; 167 168 // Height of the input frames 169 int32_t mHeight; 170 171 // Target bitrate set for the encoder, in bits per second. 172 uint32_t mBitrate; 173 174 // Target framerate set for the encoder. 175 uint32_t mFramerate; 176 177 // If a request for a change it bitrate has been received. 178 bool mBitrateUpdated; 179 180 // Bitrate control mode, either constant or variable 181 vpx_rc_mode mBitrateControlMode; 182 183 // vp8 specific configuration parameter 184 // that enables token partitioning of 185 // the stream into substreams 186 int32_t mDCTPartitions; 187 188 // Parameter that denotes whether error resilience 189 // is enabled in encoder 190 OMX_BOOL mErrorResilience; 191 192 // Color format for the input port 193 OMX_COLOR_FORMATTYPE mColorFormat; 194 195 // Encoder profile corresponding to OMX level parameter 196 // 197 // The inconsistency in the naming is caused by 198 // OMX spec referring vpx profiles (g_profile) 199 // as "levels" whereas using the name "profile" for 200 // something else. 201 OMX_VIDEO_VP8LEVELTYPE mLevel; 202 203 // Key frame interval in frames 204 uint32_t mKeyFrameInterval; 205 206 // Minimum (best quality) quantizer 207 uint32_t mMinQuantizer; 208 209 // Maximum (worst quality) quantizer 210 uint32_t mMaxQuantizer; 211 212 // Number of coding temporal layers to be used. 213 size_t mTemporalLayers; 214 215 // Temporal layer bitrare ratio in percentage 216 uint32_t mTemporalLayerBitrateRatio[OMX_VIDEO_ANDROID_MAXVP8TEMPORALLAYERS]; 217 218 // Temporal pattern type 219 OMX_VIDEO_ANDROID_VPXTEMPORALLAYERPATTERNTYPE mTemporalPatternType; 220 221 // Temporal pattern length 222 size_t mTemporalPatternLength; 223 224 // Temporal pattern current index 225 size_t mTemporalPatternIdx; 226 227 // Frame type temporal pattern 228 TemporalReferences mTemporalPattern[kMaxTemporalPattern]; 229 230 // Last input buffer timestamp 231 OMX_TICKS mLastTimestamp; 232 233 // Conversion buffer is needed to convert semi 234 // planar yuv420 to planar format 235 // It is only allocated if input format is 236 // indeed YUV420SemiPlanar. 237 uint8_t* mConversionBuffer; 238 239 bool mInputDataIsMeta; 240 const hw_module_t *mGrallocModule; 241 242 bool mKeyFrameRequested; 243 244 // Initializes input and output OMX ports with sensible 245 // default values. 246 void initPorts(); 247 248 // Initializes vpx encoder with available settings. 249 status_t initEncoder(); 250 251 // Releases vpx encoder instance, with it's associated 252 // data structures. 253 // 254 // Unless called earlier, this is handled by the 255 // dtor. 256 status_t releaseEncoder(); 257 258 // Get current encode flags 259 vpx_enc_frame_flags_t getEncodeFlags(); 260 261 // Handles port changes with respect to color formats 262 OMX_ERRORTYPE internalSetFormatParams( 263 const OMX_VIDEO_PARAM_PORTFORMATTYPE* format); 264 265 // Verifies the component role tried to be set to this OMX component is 266 // strictly video_encoder.vp8 267 OMX_ERRORTYPE internalSetRoleParams( 268 const OMX_PARAM_COMPONENTROLETYPE* role); 269 270 // Updates bitrate to reflect port settings. 271 OMX_ERRORTYPE internalSetBitrateParams( 272 const OMX_VIDEO_PARAM_BITRATETYPE* bitrate); 273 274 // Handles port definition changes. 275 OMX_ERRORTYPE internalSetPortParams( 276 const OMX_PARAM_PORTDEFINITIONTYPE* port); 277 278 // Handles vp8 specific parameters. 279 OMX_ERRORTYPE internalSetVp8Params( 280 const OMX_VIDEO_PARAM_VP8TYPE* vp8Params); 281 282 // Handles Android vp8 specific parameters. 283 OMX_ERRORTYPE internalSetAndroidVp8Params( 284 const OMX_VIDEO_PARAM_ANDROID_VP8ENCODERTYPE* vp8AndroidParams); 285 286 // Updates encoder profile 287 OMX_ERRORTYPE internalSetProfileLevel( 288 const OMX_VIDEO_PARAM_PROFILELEVELTYPE* profileAndLevel); 289 290 DISALLOW_EVIL_CONSTRUCTORS(SoftVPXEncoder); 291}; 292 293} // namespace android 294 295#endif // SOFT_VPX_ENCODER_H_ 296