SoftVPX.cpp revision bbba88cb1bdc34705d1477208990a06904c022e7
1/* 2 * Copyright (C) 2011 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 "SoftVPX" 19#include <utils/Log.h> 20 21#include "SoftVPX.h" 22 23#include <media/stagefright/foundation/ADebug.h> 24#include <media/stagefright/MediaDefs.h> 25 26#include "vpx/vpx_decoder.h" 27#include "vpx/vpx_codec.h" 28#include "vpx/vp8dx.h" 29 30namespace android { 31 32template<class T> 33static void InitOMXParams(T *params) { 34 params->nSize = sizeof(T); 35 params->nVersion.s.nVersionMajor = 1; 36 params->nVersion.s.nVersionMinor = 0; 37 params->nVersion.s.nRevision = 0; 38 params->nVersion.s.nStep = 0; 39} 40 41SoftVPX::SoftVPX( 42 const char *name, 43 const OMX_CALLBACKTYPE *callbacks, 44 OMX_PTR appData, 45 OMX_COMPONENTTYPE **component) 46 : SimpleSoftOMXComponent(name, callbacks, appData, component), 47 mCtx(NULL), 48 mWidth(320), 49 mHeight(240), 50 mOutputPortSettingsChange(NONE) { 51 initPorts(); 52 CHECK_EQ(initDecoder(), (status_t)OK); 53} 54 55SoftVPX::~SoftVPX() { 56 vpx_codec_destroy((vpx_codec_ctx_t *)mCtx); 57 delete (vpx_codec_ctx_t *)mCtx; 58 mCtx = NULL; 59} 60 61void SoftVPX::initPorts() { 62 OMX_PARAM_PORTDEFINITIONTYPE def; 63 InitOMXParams(&def); 64 65 def.nPortIndex = 0; 66 def.eDir = OMX_DirInput; 67 def.nBufferCountMin = kNumBuffers; 68 def.nBufferCountActual = def.nBufferCountMin; 69 def.nBufferSize = 8192; 70 def.bEnabled = OMX_TRUE; 71 def.bPopulated = OMX_FALSE; 72 def.eDomain = OMX_PortDomainVideo; 73 def.bBuffersContiguous = OMX_FALSE; 74 def.nBufferAlignment = 1; 75 76 def.format.video.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_VIDEO_VPX); 77 def.format.video.pNativeRender = NULL; 78 def.format.video.nFrameWidth = mWidth; 79 def.format.video.nFrameHeight = mHeight; 80 def.format.video.nStride = def.format.video.nFrameWidth; 81 def.format.video.nSliceHeight = def.format.video.nFrameHeight; 82 def.format.video.nBitrate = 0; 83 def.format.video.xFramerate = 0; 84 def.format.video.bFlagErrorConcealment = OMX_FALSE; 85 def.format.video.eCompressionFormat = OMX_VIDEO_CodingVPX; 86 def.format.video.eColorFormat = OMX_COLOR_FormatUnused; 87 def.format.video.pNativeWindow = NULL; 88 89 addPort(def); 90 91 def.nPortIndex = 1; 92 def.eDir = OMX_DirOutput; 93 def.nBufferCountMin = kNumBuffers; 94 def.nBufferCountActual = def.nBufferCountMin; 95 def.bEnabled = OMX_TRUE; 96 def.bPopulated = OMX_FALSE; 97 def.eDomain = OMX_PortDomainVideo; 98 def.bBuffersContiguous = OMX_FALSE; 99 def.nBufferAlignment = 2; 100 101 def.format.video.cMIMEType = const_cast<char *>(MEDIA_MIMETYPE_VIDEO_RAW); 102 def.format.video.pNativeRender = NULL; 103 def.format.video.nFrameWidth = mWidth; 104 def.format.video.nFrameHeight = mHeight; 105 def.format.video.nStride = def.format.video.nFrameWidth; 106 def.format.video.nSliceHeight = def.format.video.nFrameHeight; 107 def.format.video.nBitrate = 0; 108 def.format.video.xFramerate = 0; 109 def.format.video.bFlagErrorConcealment = OMX_FALSE; 110 def.format.video.eCompressionFormat = OMX_VIDEO_CodingUnused; 111 def.format.video.eColorFormat = OMX_COLOR_FormatYUV420Planar; 112 def.format.video.pNativeWindow = NULL; 113 114 def.nBufferSize = 115 (def.format.video.nFrameWidth * def.format.video.nFrameHeight * 3) / 2; 116 117 addPort(def); 118} 119 120status_t SoftVPX::initDecoder() { 121 mCtx = new vpx_codec_ctx_t; 122 vpx_codec_err_t vpx_err; 123 if ((vpx_err = vpx_codec_dec_init( 124 (vpx_codec_ctx_t *)mCtx, &vpx_codec_vp8_dx_algo, NULL, 0))) { 125 LOGE("on2 decoder failed to initialize. (%d)", vpx_err); 126 return UNKNOWN_ERROR; 127 } 128 129 return OK; 130} 131 132OMX_ERRORTYPE SoftVPX::internalGetParameter( 133 OMX_INDEXTYPE index, OMX_PTR params) { 134 switch (index) { 135 case OMX_IndexParamVideoPortFormat: 136 { 137 OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams = 138 (OMX_VIDEO_PARAM_PORTFORMATTYPE *)params; 139 140 if (formatParams->nPortIndex > 1) { 141 return OMX_ErrorUndefined; 142 } 143 144 if (formatParams->nIndex != 0) { 145 return OMX_ErrorNoMore; 146 } 147 148 if (formatParams->nPortIndex == 0) { 149 formatParams->eCompressionFormat = OMX_VIDEO_CodingVPX; 150 formatParams->eColorFormat = OMX_COLOR_FormatUnused; 151 formatParams->xFramerate = 0; 152 } else { 153 CHECK_EQ(formatParams->nPortIndex, 1u); 154 155 formatParams->eCompressionFormat = OMX_VIDEO_CodingUnused; 156 formatParams->eColorFormat = OMX_COLOR_FormatYUV420Planar; 157 formatParams->xFramerate = 0; 158 } 159 160 return OMX_ErrorNone; 161 } 162 163 default: 164 return SimpleSoftOMXComponent::internalGetParameter(index, params); 165 } 166} 167 168OMX_ERRORTYPE SoftVPX::internalSetParameter( 169 OMX_INDEXTYPE index, const OMX_PTR params) { 170 switch (index) { 171 case OMX_IndexParamStandardComponentRole: 172 { 173 const OMX_PARAM_COMPONENTROLETYPE *roleParams = 174 (const OMX_PARAM_COMPONENTROLETYPE *)params; 175 176 if (strncmp((const char *)roleParams->cRole, 177 "video_decoder.vpx", 178 OMX_MAX_STRINGNAME_SIZE - 1)) { 179 return OMX_ErrorUndefined; 180 } 181 182 return OMX_ErrorNone; 183 } 184 185 case OMX_IndexParamVideoPortFormat: 186 { 187 OMX_VIDEO_PARAM_PORTFORMATTYPE *formatParams = 188 (OMX_VIDEO_PARAM_PORTFORMATTYPE *)params; 189 190 if (formatParams->nPortIndex > 1) { 191 return OMX_ErrorUndefined; 192 } 193 194 if (formatParams->nIndex != 0) { 195 return OMX_ErrorNoMore; 196 } 197 198 return OMX_ErrorNone; 199 } 200 201 default: 202 return SimpleSoftOMXComponent::internalSetParameter(index, params); 203 } 204} 205 206void SoftVPX::onQueueFilled(OMX_U32 portIndex) { 207 if (mOutputPortSettingsChange != NONE) { 208 return; 209 } 210 211 List<BufferInfo *> &inQueue = getPortQueue(0); 212 List<BufferInfo *> &outQueue = getPortQueue(1); 213 214 while (!inQueue.empty() && !outQueue.empty()) { 215 BufferInfo *inInfo = *inQueue.begin(); 216 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 217 218 BufferInfo *outInfo = *outQueue.begin(); 219 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 220 221 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 222 inQueue.erase(inQueue.begin()); 223 inInfo->mOwnedByUs = false; 224 notifyEmptyBufferDone(inHeader); 225 226 outHeader->nFilledLen = 0; 227 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 228 229 outQueue.erase(outQueue.begin()); 230 outInfo->mOwnedByUs = false; 231 notifyFillBufferDone(outHeader); 232 return; 233 } 234 235 if (vpx_codec_decode( 236 (vpx_codec_ctx_t *)mCtx, 237 inHeader->pBuffer + inHeader->nOffset, 238 inHeader->nFilledLen, 239 NULL, 240 0)) { 241 LOGE("on2 decoder failed to decode frame."); 242 243 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 244 return; 245 } 246 247 vpx_codec_iter_t iter = NULL; 248 vpx_image_t *img = vpx_codec_get_frame((vpx_codec_ctx_t *)mCtx, &iter); 249 250 if (img != NULL) { 251 CHECK_EQ(img->fmt, IMG_FMT_I420); 252 253 int32_t width = img->d_w; 254 int32_t height = img->d_h; 255 256 if (width != mWidth || height != mHeight) { 257 mWidth = width; 258 mHeight = height; 259 260 updatePortDefinitions(); 261 262 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 263 mOutputPortSettingsChange = AWAITING_DISABLED; 264 return; 265 } 266 267 outHeader->nOffset = 0; 268 outHeader->nFilledLen = (width * height * 3) / 2; 269 outHeader->nFlags = 0; 270 outHeader->nTimeStamp = inHeader->nTimeStamp; 271 272 const uint8_t *srcLine = (const uint8_t *)img->planes[PLANE_Y]; 273 uint8_t *dst = outHeader->pBuffer; 274 for (size_t i = 0; i < img->d_h; ++i) { 275 memcpy(dst, srcLine, img->d_w); 276 277 srcLine += img->stride[PLANE_Y]; 278 dst += img->d_w; 279 } 280 281 srcLine = (const uint8_t *)img->planes[PLANE_U]; 282 for (size_t i = 0; i < img->d_h / 2; ++i) { 283 memcpy(dst, srcLine, img->d_w / 2); 284 285 srcLine += img->stride[PLANE_U]; 286 dst += img->d_w / 2; 287 } 288 289 srcLine = (const uint8_t *)img->planes[PLANE_V]; 290 for (size_t i = 0; i < img->d_h / 2; ++i) { 291 memcpy(dst, srcLine, img->d_w / 2); 292 293 srcLine += img->stride[PLANE_V]; 294 dst += img->d_w / 2; 295 } 296 297 outInfo->mOwnedByUs = false; 298 outQueue.erase(outQueue.begin()); 299 outInfo = NULL; 300 notifyFillBufferDone(outHeader); 301 outHeader = NULL; 302 } 303 304 inInfo->mOwnedByUs = false; 305 inQueue.erase(inQueue.begin()); 306 inInfo = NULL; 307 notifyEmptyBufferDone(inHeader); 308 inHeader = NULL; 309 } 310} 311 312void SoftVPX::onPortFlushCompleted(OMX_U32 portIndex) { 313} 314 315void SoftVPX::onPortEnableCompleted(OMX_U32 portIndex, bool enabled) { 316 if (portIndex != 1) { 317 return; 318 } 319 320 switch (mOutputPortSettingsChange) { 321 case NONE: 322 break; 323 324 case AWAITING_DISABLED: 325 { 326 CHECK(!enabled); 327 mOutputPortSettingsChange = AWAITING_ENABLED; 328 break; 329 } 330 331 default: 332 { 333 CHECK_EQ((int)mOutputPortSettingsChange, (int)AWAITING_ENABLED); 334 CHECK(enabled); 335 mOutputPortSettingsChange = NONE; 336 break; 337 } 338 } 339} 340 341void SoftVPX::updatePortDefinitions() { 342 OMX_PARAM_PORTDEFINITIONTYPE *def = &editPortInfo(0)->mDef; 343 def->format.video.nFrameWidth = mWidth; 344 def->format.video.nFrameHeight = mHeight; 345 def->format.video.nStride = def->format.video.nFrameWidth; 346 def->format.video.nSliceHeight = def->format.video.nFrameHeight; 347 348 def = &editPortInfo(1)->mDef; 349 def->format.video.nFrameWidth = mWidth; 350 def->format.video.nFrameHeight = mHeight; 351 def->format.video.nStride = def->format.video.nFrameWidth; 352 def->format.video.nSliceHeight = def->format.video.nFrameHeight; 353 354 def->nBufferSize = 355 (def->format.video.nFrameWidth 356 * def->format.video.nFrameHeight * 3) / 2; 357} 358 359} // namespace android 360 361android::SoftOMXComponent *createSoftOMXComponent( 362 const char *name, const OMX_CALLBACKTYPE *callbacks, 363 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 364 return new android::SoftVPX(name, callbacks, appData, component); 365} 366 367