SoftVPXEncoder.h revision 336da16a12423c496efba6ca9813d5d42a1f70f6
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 // number of buffers allocated per port 95 static const uint32_t kNumBuffers = 4; 96 97 // OMX port indexes that refer to input and 98 // output ports respectively 99 static const uint32_t kInputPortIndex = 0; 100 static const uint32_t kOutputPortIndex = 1; 101 102 // Byte-alignment required for buffers 103 static const uint32_t kInputBufferAlignment = 1; 104 static const uint32_t kOutputBufferAlignment = 2; 105 106 // Max value supported for DCT partitions 107 static const uint32_t kMaxDCTPartitions = 3; 108 109 // Number of supported input color formats 110 static const uint32_t kNumberOfSupportedColorFormats = 3; 111 112 // vpx specific opaque data structure that 113 // stores encoder state 114 vpx_codec_ctx_t* mCodecContext; 115 116 // vpx specific data structure that 117 // stores encoder configuration 118 vpx_codec_enc_cfg_t* mCodecConfiguration; 119 120 // vpx specific read-only data structure 121 // that specifies algorithm interface (e.g. vp8) 122 vpx_codec_iface_t* mCodecInterface; 123 124 // Width of the input frames 125 int32_t mWidth; 126 127 // Height of the input frames 128 int32_t mHeight; 129 130 // Target bitrate set for the encoder, in bits per second. 131 int32_t mBitrate; 132 133 // Bitrate control mode, either constant or variable 134 vpx_rc_mode mBitrateControlMode; 135 136 // Frame duration is the reciprocal of framerate, denoted 137 // in microseconds 138 uint64_t mFrameDurationUs; 139 140 // vp8 specific configuration parameter 141 // that enables token partitioning of 142 // the stream into substreams 143 int32_t mDCTPartitions; 144 145 // Parameter that denotes whether error resilience 146 // is enabled in encoder 147 OMX_BOOL mErrorResilience; 148 149 // Color format for the input port 150 OMX_COLOR_FORMATTYPE mColorFormat; 151 152 // Encoder profile corresponding to OMX level parameter 153 // 154 // The inconsistency in the naming is caused by 155 // OMX spec referring vpx profiles (g_profile) 156 // as "levels" whereas using the name "profile" for 157 // something else. 158 OMX_VIDEO_VP8LEVELTYPE mLevel; 159 160 // Conversion buffer is needed to convert semi 161 // planar yuv420 to planar format 162 // It is only allocated if input format is 163 // indeed YUV420SemiPlanar. 164 uint8_t* mConversionBuffer; 165 166 bool mInputDataIsMeta; 167 const hw_module_t *mGrallocModule; 168 169 bool mKeyFrameRequested; 170 171 // Initializes input and output OMX ports with sensible 172 // default values. 173 void initPorts(); 174 175 // Initializes vpx encoder with available settings. 176 status_t initEncoder(); 177 178 // Releases vpx encoder instance, with it's associated 179 // data structures. 180 // 181 // Unless called earlier, this is handled by the 182 // dtor. 183 status_t releaseEncoder(); 184 185 // Handles port changes with respect to color formats 186 OMX_ERRORTYPE internalSetFormatParams( 187 const OMX_VIDEO_PARAM_PORTFORMATTYPE* format); 188 189 // Verifies the component role tried to be set to this OMX component is 190 // strictly video_encoder.vp8 191 OMX_ERRORTYPE internalSetRoleParams( 192 const OMX_PARAM_COMPONENTROLETYPE* role); 193 194 // Updates bitrate to reflect port settings. 195 OMX_ERRORTYPE internalSetBitrateParams( 196 const OMX_VIDEO_PARAM_BITRATETYPE* bitrate); 197 198 // Handles port definition changes. 199 OMX_ERRORTYPE internalSetPortParams( 200 const OMX_PARAM_PORTDEFINITIONTYPE* port); 201 202 // Handles vp8 specific parameters. 203 OMX_ERRORTYPE internalSetVp8Params( 204 const OMX_VIDEO_PARAM_VP8TYPE* vp8Params); 205 206 // Updates encoder profile 207 OMX_ERRORTYPE internalSetProfileLevel( 208 const OMX_VIDEO_PARAM_PROFILELEVELTYPE* profileAndLevel); 209 210 DISALLOW_EVIL_CONSTRUCTORS(SoftVPXEncoder); 211}; 212 213} // namespace android 214 215#endif // SOFT_VPX_ENCODER_H_ 216