SoftMPEG4.cpp revision e75d53abb2f986cf07476d1430eb9981a79b2d2f
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 "SoftMPEG4" 19#include <utils/Log.h> 20 21#include "SoftMPEG4.h" 22 23#include <media/stagefright/foundation/ADebug.h> 24#include <media/stagefright/MediaDefs.h> 25#include <media/stagefright/MediaErrors.h> 26#include <media/IOMX.h> 27 28#include "mp4dec_api.h" 29 30namespace android { 31 32static const CodecProfileLevel kM4VProfileLevels[] = { 33 { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level0 }, 34 { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level0b }, 35 { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level1 }, 36 { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level2 }, 37 { OMX_VIDEO_MPEG4ProfileSimple, OMX_VIDEO_MPEG4Level3 }, 38}; 39 40static const CodecProfileLevel kH263ProfileLevels[] = { 41 { OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level10 }, 42 { OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level20 }, 43 { OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level30 }, 44 { OMX_VIDEO_H263ProfileBaseline, OMX_VIDEO_H263Level45 }, 45 { OMX_VIDEO_H263ProfileISWV2, OMX_VIDEO_H263Level10 }, 46 { OMX_VIDEO_H263ProfileISWV2, OMX_VIDEO_H263Level20 }, 47 { OMX_VIDEO_H263ProfileISWV2, OMX_VIDEO_H263Level30 }, 48 { OMX_VIDEO_H263ProfileISWV2, OMX_VIDEO_H263Level45 }, 49}; 50 51SoftMPEG4::SoftMPEG4( 52 const char *name, 53 const char *componentRole, 54 OMX_VIDEO_CODINGTYPE codingType, 55 const CodecProfileLevel *profileLevels, 56 size_t numProfileLevels, 57 const OMX_CALLBACKTYPE *callbacks, 58 OMX_PTR appData, 59 OMX_COMPONENTTYPE **component) 60 : SoftVideoDecoderOMXComponent( 61 name, componentRole, codingType, profileLevels, numProfileLevels, 62 352 /* width */, 288 /* height */, callbacks, appData, component), 63 mMode(codingType == OMX_VIDEO_CodingH263 ? MODE_H263 : MODE_MPEG4), 64 mHandle(new tagvideoDecControls), 65 mInputBufferCount(0), 66 mSignalledError(false), 67 mInitialized(false), 68 mFramesConfigured(false), 69 mNumSamplesOutput(0), 70 mPvTime(0) { 71 initPorts( 72 kNumInputBuffers, 73 8192 /* inputBufferSize */, 74 kNumOutputBuffers, 75 (mMode == MODE_MPEG4) 76 ? MEDIA_MIMETYPE_VIDEO_MPEG4 : MEDIA_MIMETYPE_VIDEO_H263); 77 CHECK_EQ(initDecoder(), (status_t)OK); 78} 79 80SoftMPEG4::~SoftMPEG4() { 81 if (mInitialized) { 82 PVCleanUpVideoDecoder(mHandle); 83 } 84 85 delete mHandle; 86 mHandle = NULL; 87} 88 89status_t SoftMPEG4::initDecoder() { 90 memset(mHandle, 0, sizeof(tagvideoDecControls)); 91 return OK; 92} 93 94void SoftMPEG4::onQueueFilled(OMX_U32 portIndex) { 95 if (mSignalledError || mOutputPortSettingsChange != NONE) { 96 return; 97 } 98 99 List<BufferInfo *> &inQueue = getPortQueue(0); 100 List<BufferInfo *> &outQueue = getPortQueue(1); 101 102 while (!inQueue.empty() && outQueue.size() == kNumOutputBuffers) { 103 BufferInfo *inInfo = *inQueue.begin(); 104 OMX_BUFFERHEADERTYPE *inHeader = inInfo->mHeader; 105 106 PortInfo *port = editPortInfo(1); 107 108 OMX_BUFFERHEADERTYPE *outHeader = 109 port->mBuffers.editItemAt(mNumSamplesOutput & 1).mHeader; 110 111 if ((inHeader->nFlags & OMX_BUFFERFLAG_EOS) && inHeader->nFilledLen == 0) { 112 inQueue.erase(inQueue.begin()); 113 inInfo->mOwnedByUs = false; 114 notifyEmptyBufferDone(inHeader); 115 116 ++mInputBufferCount; 117 118 outHeader->nFilledLen = 0; 119 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 120 121 List<BufferInfo *>::iterator it = outQueue.begin(); 122 while ((*it)->mHeader != outHeader) { 123 ++it; 124 } 125 126 BufferInfo *outInfo = *it; 127 outInfo->mOwnedByUs = false; 128 outQueue.erase(it); 129 outInfo = NULL; 130 131 notifyFillBufferDone(outHeader); 132 outHeader = NULL; 133 return; 134 } 135 136 uint8_t *bitstream = inHeader->pBuffer + inHeader->nOffset; 137 138 if (!mInitialized) { 139 uint8_t *vol_data[1]; 140 int32_t vol_size = 0; 141 142 vol_data[0] = NULL; 143 144 if (inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) { 145 vol_data[0] = bitstream; 146 vol_size = inHeader->nFilledLen; 147 } 148 149 MP4DecodingMode mode = 150 (mMode == MODE_MPEG4) ? MPEG4_MODE : H263_MODE; 151 152 Bool success = PVInitVideoDecoder( 153 mHandle, vol_data, &vol_size, 1, mWidth, mHeight, mode); 154 155 if (!success) { 156 ALOGW("PVInitVideoDecoder failed. Unsupported content?"); 157 158 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 159 mSignalledError = true; 160 return; 161 } 162 163 MP4DecodingMode actualMode = PVGetDecBitstreamMode(mHandle); 164 if (mode != actualMode) { 165 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 166 mSignalledError = true; 167 return; 168 } 169 170 PVSetPostProcType((VideoDecControls *) mHandle, 0); 171 172 if (inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) { 173 inInfo->mOwnedByUs = false; 174 inQueue.erase(inQueue.begin()); 175 inInfo = NULL; 176 notifyEmptyBufferDone(inHeader); 177 inHeader = NULL; 178 } 179 180 mInitialized = true; 181 182 if (mode == MPEG4_MODE && portSettingsChanged()) { 183 return; 184 } 185 186 continue; 187 } 188 189 if (!mFramesConfigured) { 190 PortInfo *port = editPortInfo(1); 191 OMX_BUFFERHEADERTYPE *outHeader = port->mBuffers.editItemAt(1).mHeader; 192 193 PVSetReferenceYUV(mHandle, outHeader->pBuffer); 194 195 mFramesConfigured = true; 196 } 197 198 uint32_t useExtTimestamp = (inHeader->nOffset == 0); 199 200 // decoder deals in ms (int32_t), OMX in us (int64_t) 201 // so use fake timestamp instead 202 uint32_t timestamp = 0xFFFFFFFF; 203 if (useExtTimestamp) { 204 mPvToOmxTimeMap.add(mPvTime, inHeader->nTimeStamp); 205 timestamp = mPvTime; 206 mPvTime++; 207 } 208 209 int32_t bufferSize = inHeader->nFilledLen; 210 int32_t tmp = bufferSize; 211 212 OMX_U32 frameSize = (mWidth * mHeight * 3) / 2; 213 if (outHeader->nAllocLen < frameSize) { 214 android_errorWriteLog(0x534e4554, "27833616"); 215 ALOGE("Insufficient output buffer size"); 216 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 217 mSignalledError = true; 218 return; 219 } 220 // The PV decoder is lying to us, sometimes it'll claim to only have 221 // consumed a subset of the buffer when it clearly consumed all of it. 222 // ignore whatever it says... 223 if (PVDecodeVideoFrame( 224 mHandle, &bitstream, ×tamp, &tmp, 225 &useExtTimestamp, 226 outHeader->pBuffer) != PV_TRUE) { 227 ALOGE("failed to decode video frame."); 228 229 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 230 mSignalledError = true; 231 return; 232 } 233 234 if (portSettingsChanged()) { 235 return; 236 } 237 238 // decoder deals in ms, OMX in us. 239 outHeader->nTimeStamp = mPvToOmxTimeMap.valueFor(timestamp); 240 mPvToOmxTimeMap.removeItem(timestamp); 241 242 inHeader->nOffset += bufferSize; 243 inHeader->nFilledLen = 0; 244 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 245 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 246 } else { 247 outHeader->nFlags = 0; 248 } 249 250 if (inHeader->nFilledLen == 0) { 251 inInfo->mOwnedByUs = false; 252 inQueue.erase(inQueue.begin()); 253 inInfo = NULL; 254 notifyEmptyBufferDone(inHeader); 255 inHeader = NULL; 256 } 257 258 ++mInputBufferCount; 259 260 outHeader->nOffset = 0; 261 outHeader->nFilledLen = frameSize; 262 263 List<BufferInfo *>::iterator it = outQueue.begin(); 264 while ((*it)->mHeader != outHeader) { 265 ++it; 266 } 267 268 BufferInfo *outInfo = *it; 269 outInfo->mOwnedByUs = false; 270 outQueue.erase(it); 271 outInfo = NULL; 272 273 notifyFillBufferDone(outHeader); 274 outHeader = NULL; 275 276 ++mNumSamplesOutput; 277 } 278} 279 280bool SoftMPEG4::portSettingsChanged() { 281 uint32_t disp_width, disp_height; 282 PVGetVideoDimensions(mHandle, (int32 *)&disp_width, (int32 *)&disp_height); 283 284 uint32_t buf_width, buf_height; 285 PVGetBufferDimensions(mHandle, (int32 *)&buf_width, (int32 *)&buf_height); 286 287 CHECK_LE(disp_width, buf_width); 288 CHECK_LE(disp_height, buf_height); 289 290 ALOGV("disp_width = %d, disp_height = %d, buf_width = %d, buf_height = %d", 291 disp_width, disp_height, buf_width, buf_height); 292 293 if (mCropWidth != disp_width 294 || mCropHeight != disp_height) { 295 mCropLeft = 0; 296 mCropTop = 0; 297 mCropWidth = disp_width; 298 mCropHeight = disp_height; 299 300 notify(OMX_EventPortSettingsChanged, 301 1, 302 OMX_IndexConfigCommonOutputCrop, 303 NULL); 304 } 305 306 if (buf_width != mWidth || buf_height != mHeight) { 307 mWidth = buf_width; 308 mHeight = buf_height; 309 310 updatePortDefinitions(); 311 312 if (mMode == MODE_H263) { 313 PVCleanUpVideoDecoder(mHandle); 314 315 uint8_t *vol_data[1]; 316 int32_t vol_size = 0; 317 318 vol_data[0] = NULL; 319 if (!PVInitVideoDecoder( 320 mHandle, vol_data, &vol_size, 1, mWidth, mHeight, 321 H263_MODE)) { 322 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 323 mSignalledError = true; 324 return true; 325 } 326 } 327 328 mFramesConfigured = false; 329 330 notify(OMX_EventPortSettingsChanged, 1, 0, NULL); 331 mOutputPortSettingsChange = AWAITING_DISABLED; 332 return true; 333 } 334 335 return false; 336} 337 338void SoftMPEG4::onPortFlushCompleted(OMX_U32 portIndex) { 339 if (portIndex == 0 && mInitialized) { 340 CHECK_EQ((int)PVResetVideoDecoder(mHandle), (int)PV_TRUE); 341 } 342} 343 344void SoftMPEG4::onReset() { 345 SoftVideoDecoderOMXComponent::onReset(); 346 mPvToOmxTimeMap.clear(); 347 mSignalledError = false; 348 mFramesConfigured = false; 349 if (mInitialized) { 350 PVCleanUpVideoDecoder(mHandle); 351 mInitialized = false; 352 } 353} 354 355void SoftMPEG4::updatePortDefinitions() { 356 SoftVideoDecoderOMXComponent::updatePortDefinitions(); 357 358 /* We have to align our width and height - this should affect stride! */ 359 OMX_PARAM_PORTDEFINITIONTYPE *def = &editPortInfo(kOutputPortIndex)->mDef; 360 def->nBufferSize = 361 (((def->format.video.nFrameWidth + 15) & -16) 362 * ((def->format.video.nFrameHeight + 15) & -16) * 3) / 2; 363} 364 365} // namespace android 366 367android::SoftOMXComponent *createSoftOMXComponent( 368 const char *name, const OMX_CALLBACKTYPE *callbacks, 369 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 370 using namespace android; 371 if (!strcmp(name, "OMX.google.h263.decoder")) { 372 return new android::SoftMPEG4( 373 name, "video_decoder.h263", OMX_VIDEO_CodingH263, 374 kH263ProfileLevels, ARRAY_SIZE(kH263ProfileLevels), 375 callbacks, appData, component); 376 } else if (!strcmp(name, "OMX.google.mpeg4.decoder")) { 377 return new android::SoftMPEG4( 378 name, "video_decoder.mpeg4", OMX_VIDEO_CodingMPEG4, 379 kM4VProfileLevels, ARRAY_SIZE(kM4VProfileLevels), 380 callbacks, appData, component); 381 } else { 382 CHECK(!"Unknown component"); 383 } 384} 385 386