SoftMPEG4.cpp revision a694dd0ce2caaf921f7bc894df87a5d52594b4eb
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 uint32_t *start_code = (uint32_t *)bitstream; 138 bool volHeader = *start_code == 0xB0010000; 139 if (volHeader) { 140 PVCleanUpVideoDecoder(mHandle); 141 mInitialized = false; 142 } 143 144 if (!mInitialized) { 145 uint8_t *vol_data[1]; 146 int32_t vol_size = 0; 147 148 vol_data[0] = NULL; 149 150 if ((inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) || volHeader) { 151 vol_data[0] = bitstream; 152 vol_size = inHeader->nFilledLen; 153 } 154 155 MP4DecodingMode mode = 156 (mMode == MODE_MPEG4) ? MPEG4_MODE : H263_MODE; 157 158 Bool success = PVInitVideoDecoder( 159 mHandle, vol_data, &vol_size, 1, mWidth, mHeight, mode); 160 161 if (!success) { 162 ALOGW("PVInitVideoDecoder failed. Unsupported content?"); 163 164 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 165 mSignalledError = true; 166 return; 167 } 168 169 MP4DecodingMode actualMode = PVGetDecBitstreamMode(mHandle); 170 if (mode != actualMode) { 171 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 172 mSignalledError = true; 173 return; 174 } 175 176 PVSetPostProcType((VideoDecControls *) mHandle, 0); 177 178 bool hasFrameData = false; 179 if (inHeader->nFlags & OMX_BUFFERFLAG_CODECCONFIG) { 180 inInfo->mOwnedByUs = false; 181 inQueue.erase(inQueue.begin()); 182 inInfo = NULL; 183 notifyEmptyBufferDone(inHeader); 184 inHeader = NULL; 185 } else if (volHeader) { 186 hasFrameData = true; 187 } 188 189 mInitialized = true; 190 191 if (mode == MPEG4_MODE && handlePortSettingsChange()) { 192 return; 193 } 194 195 if (!hasFrameData) { 196 continue; 197 } 198 } 199 200 if (!mFramesConfigured) { 201 PortInfo *port = editPortInfo(1); 202 OMX_BUFFERHEADERTYPE *outHeader = port->mBuffers.editItemAt(1).mHeader; 203 204 PVSetReferenceYUV(mHandle, outHeader->pBuffer); 205 206 mFramesConfigured = true; 207 } 208 209 uint32_t useExtTimestamp = (inHeader->nOffset == 0); 210 211 // decoder deals in ms (int32_t), OMX in us (int64_t) 212 // so use fake timestamp instead 213 uint32_t timestamp = 0xFFFFFFFF; 214 if (useExtTimestamp) { 215 mPvToOmxTimeMap.add(mPvTime, inHeader->nTimeStamp); 216 timestamp = mPvTime; 217 mPvTime++; 218 } 219 220 int32_t bufferSize = inHeader->nFilledLen; 221 int32_t tmp = bufferSize; 222 223 // The PV decoder is lying to us, sometimes it'll claim to only have 224 // consumed a subset of the buffer when it clearly consumed all of it. 225 // ignore whatever it says... 226 if (PVDecodeVideoFrame( 227 mHandle, &bitstream, ×tamp, &tmp, 228 &useExtTimestamp, 229 outHeader->pBuffer) != PV_TRUE) { 230 ALOGE("failed to decode video frame."); 231 232 notify(OMX_EventError, OMX_ErrorUndefined, 0, NULL); 233 mSignalledError = true; 234 return; 235 } 236 237 // H263 doesn't have VOL header, the frame size information is in short header, i.e. the 238 // decoder may detect size change after PVDecodeVideoFrame. 239 if (handlePortSettingsChange()) { 240 return; 241 } 242 243 // decoder deals in ms, OMX in us. 244 outHeader->nTimeStamp = mPvToOmxTimeMap.valueFor(timestamp); 245 mPvToOmxTimeMap.removeItem(timestamp); 246 247 inHeader->nOffset += bufferSize; 248 inHeader->nFilledLen = 0; 249 if (inHeader->nFlags & OMX_BUFFERFLAG_EOS) { 250 outHeader->nFlags = OMX_BUFFERFLAG_EOS; 251 } else { 252 outHeader->nFlags = 0; 253 } 254 255 if (inHeader->nFilledLen == 0) { 256 inInfo->mOwnedByUs = false; 257 inQueue.erase(inQueue.begin()); 258 inInfo = NULL; 259 notifyEmptyBufferDone(inHeader); 260 inHeader = NULL; 261 } 262 263 ++mInputBufferCount; 264 265 outHeader->nOffset = 0; 266 outHeader->nFilledLen = (mWidth * mHeight * 3) / 2; 267 268 List<BufferInfo *>::iterator it = outQueue.begin(); 269 while ((*it)->mHeader != outHeader) { 270 ++it; 271 } 272 273 BufferInfo *outInfo = *it; 274 outInfo->mOwnedByUs = false; 275 outQueue.erase(it); 276 outInfo = NULL; 277 278 notifyFillBufferDone(outHeader); 279 outHeader = NULL; 280 281 ++mNumSamplesOutput; 282 } 283} 284 285bool SoftMPEG4::handlePortSettingsChange() { 286 uint32_t disp_width, disp_height; 287 PVGetVideoDimensions(mHandle, (int32 *)&disp_width, (int32 *)&disp_height); 288 289 uint32_t buf_width, buf_height; 290 PVGetBufferDimensions(mHandle, (int32 *)&buf_width, (int32 *)&buf_height); 291 292 CHECK_LE(disp_width, buf_width); 293 CHECK_LE(disp_height, buf_height); 294 295 ALOGV("disp_width = %d, disp_height = %d, buf_width = %d, buf_height = %d", 296 disp_width, disp_height, buf_width, buf_height); 297 298 bool cropChanged = false; 299 if (mCropWidth != disp_width || mCropHeight != disp_height) { 300 mCropLeft = 0; 301 mCropTop = 0; 302 mCropWidth = disp_width; 303 mCropHeight = disp_height; 304 cropChanged = true; 305 } 306 307 bool portWillReset = false; 308 const bool fakeStride = true; 309 SoftVideoDecoderOMXComponent::handlePortSettingsChange( 310 &portWillReset, buf_width, buf_height, cropChanged, fakeStride); 311 if (portWillReset) { 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 331 return portWillReset; 332} 333 334void SoftMPEG4::onPortFlushCompleted(OMX_U32 portIndex) { 335 if (portIndex == 0 && mInitialized) { 336 CHECK_EQ((int)PVResetVideoDecoder(mHandle), (int)PV_TRUE); 337 } 338} 339 340void SoftMPEG4::onReset() { 341 SoftVideoDecoderOMXComponent::onReset(); 342 mPvToOmxTimeMap.clear(); 343 mSignalledError = false; 344 mFramesConfigured = false; 345 if (mInitialized) { 346 PVCleanUpVideoDecoder(mHandle); 347 mInitialized = false; 348 } 349} 350 351void SoftMPEG4::updatePortDefinitions() { 352 SoftVideoDecoderOMXComponent::updatePortDefinitions(); 353 354 /* We have to align our width and height - this should affect stride! */ 355 OMX_PARAM_PORTDEFINITIONTYPE *def = &editPortInfo(kOutputPortIndex)->mDef; 356 def->nBufferSize = 357 (((def->format.video.nFrameWidth + 15) & -16) 358 * ((def->format.video.nFrameHeight + 15) & -16) * 3) / 2; 359} 360 361} // namespace android 362 363android::SoftOMXComponent *createSoftOMXComponent( 364 const char *name, const OMX_CALLBACKTYPE *callbacks, 365 OMX_PTR appData, OMX_COMPONENTTYPE **component) { 366 using namespace android; 367 if (!strcmp(name, "OMX.google.h263.decoder")) { 368 return new android::SoftMPEG4( 369 name, "video_decoder.h263", OMX_VIDEO_CodingH263, 370 kH263ProfileLevels, ARRAY_SIZE(kH263ProfileLevels), 371 callbacks, appData, component); 372 } else if (!strcmp(name, "OMX.google.mpeg4.decoder")) { 373 return new android::SoftMPEG4( 374 name, "video_decoder.mpeg4", OMX_VIDEO_CodingMPEG4, 375 kM4VProfileLevels, ARRAY_SIZE(kM4VProfileLevels), 376 callbacks, appData, component); 377 } else { 378 CHECK(!"Unknown component"); 379 } 380} 381 382