SoftVPX.cpp revision dba83c1cb1bef03bc5d1760c2639d06ff71c0fa7
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 27namespace android { 28 29SoftVPX::SoftVPX( 30 const char *name, 31 const char *componentRole, 32 OMX_VIDEO_CODINGTYPE codingType, 33 const OMX_CALLBACKTYPE *callbacks, 34 OMX_PTR appData, 35 OMX_COMPONENTTYPE **component) 36 : SoftVideoDecoderOMXComponent( 37 name, componentRole, codingType, 38 NULL /* profileLevels */, 0 /* numProfileLevels */, 39 320 /* width */, 240 /* height */, callbacks, appData, component), 40 mMode(codingType == OMX_VIDEO_CodingVP8 ? MODE_VP8 : MODE_VP9), 41 mCtx(NULL), 42 mImg(NULL) { 43 initPorts(kNumBuffers, 768 * 1024 /* inputBufferSize */, 44 kNumBuffers, 45 codingType == OMX_VIDEO_CodingVP8 ? MEDIA_MIMETYPE_VIDEO_VP8 : MEDIA_MIMETYPE_VIDEO_VP9); 46 47 CHECK_EQ(initDecoder(), (status_t)OK); 48} 49 50SoftVPX::~SoftVPX() { 51 vpx_codec_destroy((vpx_codec_ctx_t *)mCtx); 52 delete (vpx_codec_ctx_t *)mCtx; 53 mCtx = NULL; 54} 55 56static int GetCPUCoreCount() { 57 int cpuCoreCount = 1; 58#if defined(_SC_NPROCESSORS_ONLN) 59 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); 60#else 61 // _SC_NPROC_ONLN must be defined... 62 cpuCoreCount = sysconf(_SC_NPROC_ONLN); 63#endif 64 CHECK(cpuCoreCount >= 1); 65 ALOGV("Number of CPU cores: %d", cpuCoreCount); 66 return cpuCoreCount; 67} 68 69status_t SoftVPX::initDecoder() { 70 mCtx = new vpx_codec_ctx_t; 71 vpx_codec_err_t vpx_err; 72 vpx_codec_dec_cfg_t cfg; 73 memset(&cfg, 0, sizeof(vpx_codec_dec_cfg_t)); 74 cfg.threads = GetCPUCoreCount(); 75 if ((vpx_err = vpx_codec_dec_init( 76 (vpx_codec_ctx_t *)mCtx, 77 mMode == MODE_VP8 ? &vpx_codec_vp8_dx_algo : &vpx_codec_vp9_dx_algo, 78 &cfg, 0))) { 79 ALOGE("on2 decoder failed to initialize. (%d)", vpx_err); 80 return UNKNOWN_ERROR; 81 } 82 83 return OK; 84} 85 86void SoftVPX::onQueueFilled(OMX_U32 /* portIndex */) { 87 if (mOutputPortSettingsChange != NONE) { 88 return; 89 } 90 91 List<BufferInfo *> &inQueue = getPortQueue(0); 92 List<BufferInfo *> &outQueue = getPortQueue(1); 93 bool EOSseen = false; 94 95 while (!inQueue.empty() && !outQueue.empty()) { 96 BufferInfo *inInfo = *inQueue.begin(); 97 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 98 99 BufferInfo *outInfo = *outQueue.begin(); 100 OMX_BUFFERHEADERTYPE *outHeader = outInfo->mHeader; 101 102 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 103 EOSseen = true; 104 if (inHeader->nFilledLen == 0) { 105 inQueue.erase(inQueue.begin()); 106 inInfo->mOwnedByUs = false; 107 notifyEmptyBufferDone(inHeader); 108 109 outHeader->nFilledLen = 0; 110 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 111 112 outQueue.erase(outQueue.begin()); 113 outInfo->mOwnedByUs = false; 114 notifyFillBufferDone(outHeader); 115 return; 116 } 117 } 118 119 if (mImg == NULL) { 120 if (vpx_codec_decode( 121 (vpx_codec_ctx_t *)mCtx, 122 inHeader->pBuffer + inHeader->nOffset, 123 inHeader->nFilledLen, 124 NULL, 125 0)) { 126 ALOGE("on2 decoder failed to decode frame."); 127 128 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 129 return; 130 } 131 vpx_codec_iter_t iter = NULL; 132 mImg = vpx_codec_get_frame((vpx_codec_ctx_t *)mCtx, &iter); 133 } 134 135 if (mImg != NULL) { 136 CHECK_EQ(mImg->fmt, IMG_FMT_I420); 137 138 uint32_t width = mImg->d_w; 139 uint32_t height = mImg->d_h; 140 bool portWillReset = false; 141 handlePortSettingsChange(&portWillReset, width, height); 142 if (portWillReset) { 143 return; 144 } 145 146 outHeader->nOffset = 0; 147 outHeader->nFilledLen = (width * height * 3) / 2; 148 outHeader->nFlags = EOSseen ? OMX_BUFFERFLAG_EOS : 0; 149 outHeader->nTimeStamp = inHeader->nTimeStamp; 150 151 uint8_t *dst = outHeader->pBuffer; 152 const uint8_t *srcY = (const uint8_t *)mImg->planes[PLANE_Y]; 153 const uint8_t *srcU = (const uint8_t *)mImg->planes[PLANE_U]; 154 const uint8_t *srcV = (const uint8_t *)mImg->planes[PLANE_V]; 155 size_t srcYStride = mImg->stride[PLANE_Y]; 156 size_t srcUStride = mImg->stride[PLANE_U]; 157 size_t srcVStride = mImg->stride[PLANE_V]; 158 copyYV12FrameToOutputBuffer(dst, srcY, srcU, srcV, srcYStride, srcUStride, srcVStride); 159 160 mImg = NULL; 161 outInfo->mOwnedByUs = false; 162 outQueue.erase(outQueue.begin()); 163 outInfo = NULL; 164 notifyFillBufferDone(outHeader); 165 outHeader = NULL; 166 } 167 168 inInfo->mOwnedByUs = false; 169 inQueue.erase(inQueue.begin()); 170 inInfo = NULL; 171 notifyEmptyBufferDone(inHeader); 172 inHeader = NULL; 173 } 174} 175 176} // namespace android 177 178android::SoftOMXComponent *createSoftOMXComponent( 179 const char *name, const OMX_CALLBACKTYPE *callbacks, 180 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 181 if (!strcmp(name, "OMX.google.vp8.decoder")) { 182 return new android::SoftVPX( 183 name, "video_decoder.vp8", OMX_VIDEO_CodingVP8, 184 callbacks, appData, component); 185 } else if (!strcmp(name, "OMX.google.vp9.decoder")) { 186 return new android::SoftVPX( 187 name, "video_decoder.vp9", OMX_VIDEO_CodingVP9, 188 callbacks, appData, component); 189 } else { 190 CHECK(!"Unknown component"); 191 } 192 return NULL; 193} 194