SoftMPEG2.cpp revision 8f81045b9219cada36c7be7e6f0541cfa7bd94c8
1/* 2 * Copyright 2015 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 "SoftMPEG2" 19#include <utils/Log.h> 20 21#include "iv_datatypedef.h" 22#include "iv.h" 23#include "ivd.h" 24#include "ithread.h" 25#include "impeg2d.h" 26#include "SoftMPEG2.h" 27 28#include <media/stagefright/foundation/ADebug.h> 29#include <media/stagefright/MediaDefs.h> 30#include <OMX_VideoExt.h> 31 32namespace android { 33 34#define componentName "video_decoder.mpeg2" 35#define codingType OMX_VIDEO_CodingMPEG2 36#define CODEC_MIME_TYPE MEDIA_MIMETYPE_VIDEO_MPEG2 37 38/** Function and structure definitions to keep code similar for each codec */ 39#define ivdec_api_function impeg2d_api_function 40#define ivdext_init_ip_t impeg2d_init_ip_t 41#define ivdext_init_op_t impeg2d_init_op_t 42#define ivdext_fill_mem_rec_ip_t impeg2d_fill_mem_rec_ip_t 43#define ivdext_fill_mem_rec_op_t impeg2d_fill_mem_rec_op_t 44#define ivdext_ctl_set_num_cores_ip_t impeg2d_ctl_set_num_cores_ip_t 45#define ivdext_ctl_set_num_cores_op_t impeg2d_ctl_set_num_cores_op_t 46 47#define IVDEXT_CMD_CTL_SET_NUM_CORES \ 48 (IVD_CONTROL_API_COMMAND_TYPE_T)IMPEG2D_CMD_CTL_SET_NUM_CORES 49 50static const CodecProfileLevel kProfileLevels[] = { 51 { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelLL }, 52 { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelML }, 53 { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelH14 }, 54 { OMX_VIDEO_MPEG2ProfileSimple, OMX_VIDEO_MPEG2LevelHL }, 55 56 { OMX_VIDEO_MPEG2ProfileMain , OMX_VIDEO_MPEG2LevelLL }, 57 { OMX_VIDEO_MPEG2ProfileMain , OMX_VIDEO_MPEG2LevelML }, 58 { OMX_VIDEO_MPEG2ProfileMain , OMX_VIDEO_MPEG2LevelH14 }, 59 { OMX_VIDEO_MPEG2ProfileMain , OMX_VIDEO_MPEG2LevelHL }, 60}; 61 62SoftMPEG2::SoftMPEG2( 63 const char *name, 64 const OMX_CALLBACKTYPE *callbacks, 65 OMX_PTR appData, 66 OMX_COMPONENTTYPE **component) 67 : SoftVideoDecoderOMXComponent( 68 name, componentName, codingType, 69 kProfileLevels, ARRAY_SIZE(kProfileLevels), 70 320 /* width */, 240 /* height */, callbacks, 71 appData, component), 72 mMemRecords(NULL), 73 mFlushOutBuffer(NULL), 74 mOmxColorFormat(OMX_COLOR_FormatYUV420Planar), 75 mIvColorFormat(IV_YUV_420P), 76 mNewWidth(mWidth), 77 mNewHeight(mHeight), 78 mChangingResolution(false) { 79 initPorts(kNumBuffers, INPUT_BUF_SIZE, kNumBuffers, CODEC_MIME_TYPE); 80 81 // If input dump is enabled, then open create an empty file 82 GENERATE_FILE_NAMES(); 83 CREATE_DUMP_FILE(mInFile); 84 85 CHECK_EQ(initDecoder(), (status_t)OK); 86} 87 88SoftMPEG2::~SoftMPEG2() { 89 CHECK_EQ(deInitDecoder(), (status_t)OK); 90} 91 92 93static size_t getMinTimestampIdx(OMX_S64 *pNTimeStamp, bool *pIsTimeStampValid) { 94 OMX_S64 minTimeStamp = LLONG_MAX; 95 int idx = -1; 96 for (size_t i = 0; i < MAX_TIME_STAMPS; i++) { 97 if (pIsTimeStampValid[i]) { 98 if (pNTimeStamp[i] < minTimeStamp) { 99 minTimeStamp = pNTimeStamp[i]; 100 idx = i; 101 } 102 } 103 } 104 return idx; 105} 106 107static size_t GetCPUCoreCount() { 108 long cpuCoreCount = 1; 109#if defined(_SC_NPROCESSORS_ONLN) 110 cpuCoreCount = sysconf(_SC_NPROCESSORS_ONLN); 111#else 112 // _SC_NPROC_ONLN must be defined... 113 cpuCoreCount = sysconf(_SC_NPROC_ONLN); 114#endif 115 CHECK(cpuCoreCount >= 1); 116 ALOGV("Number of CPU cores: %ld", cpuCoreCount); 117 return (size_t)cpuCoreCount; 118} 119 120void SoftMPEG2::logVersion() { 121 ivd_ctl_getversioninfo_ip_t s_ctl_ip; 122 ivd_ctl_getversioninfo_op_t s_ctl_op; 123 UWORD8 au1_buf[512]; 124 IV_API_CALL_STATUS_T status; 125 126 s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL; 127 s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_GETVERSION; 128 s_ctl_ip.u4_size = sizeof(ivd_ctl_getversioninfo_ip_t); 129 s_ctl_op.u4_size = sizeof(ivd_ctl_getversioninfo_op_t); 130 s_ctl_ip.pv_version_buffer = au1_buf; 131 s_ctl_ip.u4_version_buffer_size = sizeof(au1_buf); 132 133 status = ivdec_api_function(mCodecCtx, (void *)&s_ctl_ip, (void *)&s_ctl_op); 134 135 if (status != IV_SUCCESS) { 136 ALOGE("Error in getting version number: 0x%x", 137 s_ctl_op.u4_error_code); 138 } else { 139 ALOGV("Ittiam decoder version number: %s", 140 (char *)s_ctl_ip.pv_version_buffer); 141 } 142 return; 143} 144 145status_t SoftMPEG2::setParams(size_t stride) { 146 ivd_ctl_set_config_ip_t s_ctl_ip; 147 ivd_ctl_set_config_op_t s_ctl_op; 148 IV_API_CALL_STATUS_T status; 149 s_ctl_ip.u4_disp_wd = (UWORD32)stride; 150 s_ctl_ip.e_frm_skip_mode = IVD_SKIP_NONE; 151 152 s_ctl_ip.e_frm_out_mode = IVD_DISPLAY_FRAME_OUT; 153 s_ctl_ip.e_vid_dec_mode = IVD_DECODE_FRAME; 154 s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL; 155 s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_SETPARAMS; 156 s_ctl_ip.u4_size = sizeof(ivd_ctl_set_config_ip_t); 157 s_ctl_op.u4_size = sizeof(ivd_ctl_set_config_op_t); 158 159 ALOGV("Set the run-time (dynamic) parameters stride = %u", stride); 160 status = ivdec_api_function(mCodecCtx, (void *)&s_ctl_ip, (void *)&s_ctl_op); 161 162 if (status != IV_SUCCESS) { 163 ALOGE("Error in setting the run-time parameters: 0x%x", 164 s_ctl_op.u4_error_code); 165 166 return UNKNOWN_ERROR; 167 } 168 return OK; 169} 170 171status_t SoftMPEG2::resetPlugin() { 172 mIsInFlush = false; 173 mReceivedEOS = false; 174 memset(mTimeStamps, 0, sizeof(mTimeStamps)); 175 memset(mTimeStampsValid, 0, sizeof(mTimeStampsValid)); 176 177 /* Initialize both start and end times */ 178 gettimeofday(&mTimeStart, NULL); 179 gettimeofday(&mTimeEnd, NULL); 180 181 return OK; 182} 183 184status_t SoftMPEG2::resetDecoder() { 185 ivd_ctl_reset_ip_t s_ctl_ip; 186 ivd_ctl_reset_op_t s_ctl_op; 187 IV_API_CALL_STATUS_T status; 188 189 s_ctl_ip.e_cmd = IVD_CMD_VIDEO_CTL; 190 s_ctl_ip.e_sub_cmd = IVD_CMD_CTL_RESET; 191 s_ctl_ip.u4_size = sizeof(ivd_ctl_reset_ip_t); 192 s_ctl_op.u4_size = sizeof(ivd_ctl_reset_op_t); 193 194 status = ivdec_api_function(mCodecCtx, (void *)&s_ctl_ip, (void *)&s_ctl_op); 195 if (IV_SUCCESS != status) { 196 ALOGE("Error in reset: 0x%x", s_ctl_op.u4_error_code); 197 return UNKNOWN_ERROR; 198 } 199 200 /* Set the run-time (dynamic) parameters */ 201 setParams(outputBufferWidth()); 202 203 /* Set number of cores/threads to be used by the codec */ 204 setNumCores(); 205 206 return OK; 207} 208 209status_t SoftMPEG2::setNumCores() { 210 ivdext_ctl_set_num_cores_ip_t s_set_cores_ip; 211 ivdext_ctl_set_num_cores_op_t s_set_cores_op; 212 IV_API_CALL_STATUS_T status; 213 s_set_cores_ip.e_cmd = IVD_CMD_VIDEO_CTL; 214 s_set_cores_ip.e_sub_cmd = IVDEXT_CMD_CTL_SET_NUM_CORES; 215 s_set_cores_ip.u4_num_cores = MIN(mNumCores, CODEC_MAX_NUM_CORES); 216 s_set_cores_ip.u4_size = sizeof(ivdext_ctl_set_num_cores_ip_t); 217 s_set_cores_op.u4_size = sizeof(ivdext_ctl_set_num_cores_op_t); 218 219 status = ivdec_api_function(mCodecCtx, (void *)&s_set_cores_ip, (void *)&s_set_cores_op); 220 if (IV_SUCCESS != status) { 221 ALOGE("Error in setting number of cores: 0x%x", 222 s_set_cores_op.u4_error_code); 223 return UNKNOWN_ERROR; 224 } 225 return OK; 226} 227 228status_t SoftMPEG2::setFlushMode() { 229 IV_API_CALL_STATUS_T status; 230 ivd_ctl_flush_ip_t s_video_flush_ip; 231 ivd_ctl_flush_op_t s_video_flush_op; 232 233 s_video_flush_ip.e_cmd = IVD_CMD_VIDEO_CTL; 234 s_video_flush_ip.e_sub_cmd = IVD_CMD_CTL_FLUSH; 235 s_video_flush_ip.u4_size = sizeof(ivd_ctl_flush_ip_t); 236 s_video_flush_op.u4_size = sizeof(ivd_ctl_flush_op_t); 237 238 /* Set the decoder in Flush mode, subsequent decode() calls will flush */ 239 status = ivdec_api_function( 240 mCodecCtx, (void *)&s_video_flush_ip, (void *)&s_video_flush_op); 241 242 if (status != IV_SUCCESS) { 243 ALOGE("Error in setting the decoder in flush mode: (%d) 0x%x", status, 244 s_video_flush_op.u4_error_code); 245 return UNKNOWN_ERROR; 246 } 247 248 mWaitForI = true; 249 mIsInFlush = true; 250 return OK; 251} 252 253status_t SoftMPEG2::initDecoder() { 254 IV_API_CALL_STATUS_T status; 255 256 UWORD32 u4_num_reorder_frames; 257 UWORD32 u4_num_ref_frames; 258 UWORD32 u4_share_disp_buf; 259 260 mNumCores = GetCPUCoreCount(); 261 mWaitForI = true; 262 263 /* Initialize number of ref and reorder modes (for MPEG2) */ 264 u4_num_reorder_frames = 16; 265 u4_num_ref_frames = 16; 266 u4_share_disp_buf = 0; 267 268 uint32_t displayStride = outputBufferWidth(); 269 uint32_t displayHeight = outputBufferHeight(); 270 uint32_t displaySizeY = displayStride * displayHeight; 271 272 { 273 iv_num_mem_rec_ip_t s_num_mem_rec_ip; 274 iv_num_mem_rec_op_t s_num_mem_rec_op; 275 276 s_num_mem_rec_ip.u4_size = sizeof(s_num_mem_rec_ip); 277 s_num_mem_rec_op.u4_size = sizeof(s_num_mem_rec_op); 278 s_num_mem_rec_ip.e_cmd = IV_CMD_GET_NUM_MEM_REC; 279 280 status = ivdec_api_function( 281 mCodecCtx, (void *)&s_num_mem_rec_ip, (void *)&s_num_mem_rec_op); 282 if (IV_SUCCESS != status) { 283 ALOGE("Error in getting mem records: 0x%x", 284 s_num_mem_rec_op.u4_error_code); 285 return UNKNOWN_ERROR; 286 } 287 288 mNumMemRecords = s_num_mem_rec_op.u4_num_mem_rec; 289 } 290 291 mMemRecords = (iv_mem_rec_t *)ivd_aligned_malloc( 292 128, mNumMemRecords * sizeof(iv_mem_rec_t)); 293 if (mMemRecords == NULL) { 294 ALOGE("Allocation failure"); 295 return NO_MEMORY; 296 } 297 298 memset(mMemRecords, 0, mNumMemRecords * sizeof(iv_mem_rec_t)); 299 300 { 301 size_t i; 302 ivdext_fill_mem_rec_ip_t s_fill_mem_ip; 303 ivdext_fill_mem_rec_op_t s_fill_mem_op; 304 iv_mem_rec_t *ps_mem_rec; 305 306 s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_size = 307 sizeof(ivdext_fill_mem_rec_ip_t); 308 309 s_fill_mem_ip.u4_share_disp_buf = u4_share_disp_buf; 310 s_fill_mem_ip.e_output_format = mIvColorFormat; 311 312 s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.e_cmd = IV_CMD_FILL_NUM_MEM_REC; 313 s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.pv_mem_rec_location = mMemRecords; 314 s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_wd = displayStride; 315 s_fill_mem_ip.s_ivd_fill_mem_rec_ip_t.u4_max_frm_ht = displayHeight; 316 s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_size = 317 sizeof(ivdext_fill_mem_rec_op_t); 318 319 ps_mem_rec = mMemRecords; 320 for (i = 0; i < mNumMemRecords; i++) { 321 ps_mem_rec[i].u4_size = sizeof(iv_mem_rec_t); 322 } 323 324 status = ivdec_api_function( 325 mCodecCtx, (void *)&s_fill_mem_ip, (void *)&s_fill_mem_op); 326 327 if (IV_SUCCESS != status) { 328 ALOGE("Error in filling mem records: 0x%x", 329 s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_error_code); 330 return UNKNOWN_ERROR; 331 } 332 mNumMemRecords = 333 s_fill_mem_op.s_ivd_fill_mem_rec_op_t.u4_num_mem_rec_filled; 334 335 ps_mem_rec = mMemRecords; 336 337 for (i = 0; i < mNumMemRecords; i++) { 338 ps_mem_rec->pv_base = ivd_aligned_malloc( 339 ps_mem_rec->u4_mem_alignment, ps_mem_rec->u4_mem_size); 340 if (ps_mem_rec->pv_base == NULL) { 341 ALOGE("Allocation failure for memory record #%zu of size %u", 342 i, ps_mem_rec->u4_mem_size); 343 status = IV_FAIL; 344 return NO_MEMORY; 345 } 346 347 ps_mem_rec++; 348 } 349 } 350 351 /* Initialize the decoder */ 352 { 353 ivdext_init_ip_t s_init_ip; 354 ivdext_init_op_t s_init_op; 355 356 void *dec_fxns = (void *)ivdec_api_function; 357 358 s_init_ip.s_ivd_init_ip_t.u4_size = sizeof(ivdext_init_ip_t); 359 s_init_ip.s_ivd_init_ip_t.e_cmd = (IVD_API_COMMAND_TYPE_T)IV_CMD_INIT; 360 s_init_ip.s_ivd_init_ip_t.pv_mem_rec_location = mMemRecords; 361 s_init_ip.s_ivd_init_ip_t.u4_frm_max_wd = displayStride; 362 s_init_ip.s_ivd_init_ip_t.u4_frm_max_ht = displayHeight; 363 364 s_init_ip.u4_share_disp_buf = u4_share_disp_buf; 365 366 s_init_op.s_ivd_init_op_t.u4_size = sizeof(s_init_op); 367 368 s_init_ip.s_ivd_init_ip_t.u4_num_mem_rec = mNumMemRecords; 369 s_init_ip.s_ivd_init_ip_t.e_output_format = mIvColorFormat; 370 371 mCodecCtx = (iv_obj_t *)mMemRecords[0].pv_base; 372 mCodecCtx->pv_fxns = dec_fxns; 373 mCodecCtx->u4_size = sizeof(iv_obj_t); 374 375 status = ivdec_api_function(mCodecCtx, (void *)&s_init_ip, (void *)&s_init_op); 376 if (status != IV_SUCCESS) { 377 ALOGE("Error in init: 0x%x", 378 s_init_op.s_ivd_init_op_t.u4_error_code); 379 return UNKNOWN_ERROR; 380 } 381 } 382 383 /* Reset the plugin state */ 384 resetPlugin(); 385 386 /* Set the run time (dynamic) parameters */ 387 setParams(displayStride); 388 389 /* Set number of cores/threads to be used by the codec */ 390 setNumCores(); 391 392 /* Get codec version */ 393 logVersion(); 394 395 /* Allocate internal picture buffer */ 396 uint32_t bufferSize = displaySizeY * 3 / 2; 397 mFlushOutBuffer = (uint8_t *)ivd_aligned_malloc(128, bufferSize); 398 if (NULL == mFlushOutBuffer) { 399 ALOGE("Could not allocate flushOutputBuffer of size %zu", bufferSize); 400 return NO_MEMORY; 401 } 402 403 mInitNeeded = false; 404 mFlushNeeded = false; 405 return OK; 406} 407 408status_t SoftMPEG2::deInitDecoder() { 409 size_t i; 410 411 if (mMemRecords) { 412 iv_mem_rec_t *ps_mem_rec; 413 414 ps_mem_rec = mMemRecords; 415 for (i = 0; i < mNumMemRecords; i++) { 416 if (ps_mem_rec->pv_base) { 417 ivd_aligned_free(ps_mem_rec->pv_base); 418 } 419 ps_mem_rec++; 420 } 421 ivd_aligned_free(mMemRecords); 422 mMemRecords = NULL; 423 } 424 425 if (mFlushOutBuffer) { 426 ivd_aligned_free(mFlushOutBuffer); 427 mFlushOutBuffer = NULL; 428 } 429 430 mInitNeeded = true; 431 mChangingResolution = false; 432 433 return OK; 434} 435 436status_t SoftMPEG2::reInitDecoder() { 437 status_t ret; 438 439 deInitDecoder(); 440 441 ret = initDecoder(); 442 if (OK != ret) { 443 ALOGE("Create failure"); 444 deInitDecoder(); 445 return NO_MEMORY; 446 } 447 return OK; 448} 449 450void SoftMPEG2::onReset() { 451 SoftVideoDecoderOMXComponent::onReset(); 452 453 mWaitForI = true; 454 455 resetDecoder(); 456 resetPlugin(); 457} 458 459OMX_ERRORTYPE SoftMPEG2::internalSetParameter(OMX_INDEXTYPE index, const OMX_PTR params) { 460 const uint32_t oldWidth = mWidth; 461 const uint32_t oldHeight = mHeight; 462 OMX_ERRORTYPE ret = SoftVideoDecoderOMXComponent::internalSetParameter(index, params); 463 if (mWidth != oldWidth || mHeight != oldHeight) { 464 reInitDecoder(); 465 } 466 return ret; 467} 468 469void SoftMPEG2::setDecodeArgs( 470 ivd_video_decode_ip_t *ps_dec_ip, 471 ivd_video_decode_op_t *ps_dec_op, 472 OMX_BUFFERHEADERTYPE *inHeader, 473 OMX_BUFFERHEADERTYPE *outHeader, 474 size_t timeStampIx) { 475 size_t sizeY = outputBufferWidth() * outputBufferHeight(); 476 size_t sizeUV; 477 uint8_t *pBuf; 478 479 ps_dec_ip->u4_size = sizeof(ivd_video_decode_ip_t); 480 ps_dec_op->u4_size = sizeof(ivd_video_decode_op_t); 481 482 ps_dec_ip->e_cmd = IVD_CMD_VIDEO_DECODE; 483 484 /* When in flush and after EOS with zero byte input, 485 * inHeader is set to zero. Hence check for non-null */ 486 if (inHeader) { 487 ps_dec_ip->u4_ts = timeStampIx; 488 ps_dec_ip->pv_stream_buffer = inHeader->pBuffer 489 + inHeader->nOffset; 490 ps_dec_ip->u4_num_Bytes = inHeader->nFilledLen; 491 } else { 492 ps_dec_ip->u4_ts = 0; 493 ps_dec_ip->pv_stream_buffer = NULL; 494 ps_dec_ip->u4_num_Bytes = 0; 495 } 496 497 if (outHeader) { 498 pBuf = outHeader->pBuffer; 499 } else { 500 pBuf = mFlushOutBuffer; 501 } 502 503 sizeUV = sizeY / 4; 504 ps_dec_ip->s_out_buffer.u4_min_out_buf_size[0] = sizeY; 505 ps_dec_ip->s_out_buffer.u4_min_out_buf_size[1] = sizeUV; 506 ps_dec_ip->s_out_buffer.u4_min_out_buf_size[2] = sizeUV; 507 508 ps_dec_ip->s_out_buffer.pu1_bufs[0] = pBuf; 509 ps_dec_ip->s_out_buffer.pu1_bufs[1] = pBuf + sizeY; 510 ps_dec_ip->s_out_buffer.pu1_bufs[2] = pBuf + sizeY + sizeUV; 511 ps_dec_ip->s_out_buffer.u4_num_bufs = 3; 512 return; 513} 514void SoftMPEG2::onPortFlushCompleted(OMX_U32 portIndex) { 515 /* Once the output buffers are flushed, ignore any buffers that are held in decoder */ 516 if (kOutputPortIndex == portIndex) { 517 setFlushMode(); 518 519 while (true) { 520 ivd_video_decode_ip_t s_dec_ip; 521 ivd_video_decode_op_t s_dec_op; 522 IV_API_CALL_STATUS_T status; 523 size_t sizeY, sizeUV; 524 525 setDecodeArgs(&s_dec_ip, &s_dec_op, NULL, NULL, 0); 526 527 status = ivdec_api_function(mCodecCtx, (void *)&s_dec_ip, (void *)&s_dec_op); 528 if (0 == s_dec_op.u4_output_present) { 529 resetPlugin(); 530 break; 531 } 532 } 533 } 534} 535 536void SoftMPEG2::onQueueFilled(OMX_U32 portIndex) { 537 UNUSED(portIndex); 538 539 if (mOutputPortSettingsChange != NONE) { 540 return; 541 } 542 543 List<BufferInfo *> &inQueue = getPortQueue(kInputPortIndex); 544 List<BufferInfo *> &outQueue = getPortQueue(kOutputPortIndex); 545 546 /* If input EOS is seen and decoder is not in flush mode, 547 * set the decoder in flush mode. 548 * There can be a case where EOS is sent along with last picture data 549 * In that case, only after decoding that input data, decoder has to be 550 * put in flush. This case is handled here */ 551 552 if (mReceivedEOS && !mIsInFlush) { 553 setFlushMode(); 554 } 555 556 while (!outQueue.empty()) { 557 BufferInfo *inInfo; 558 OMX_BUFFERHEADERTYPE *inHeader; 559 560 BufferInfo *outInfo; 561 OMX_BUFFERHEADERTYPE *outHeader; 562 size_t timeStampIx; 563 564 inInfo = NULL; 565 inHeader = NULL; 566 567 if (!mIsInFlush) { 568 if (!inQueue.empty()) { 569 inInfo = *inQueue.begin(); 570 inHeader = inInfo->mHeader; 571 } else { 572 break; 573 } 574 } 575 576 outInfo = *outQueue.begin(); 577 outHeader = outInfo->mHeader; 578 outHeader->nFlags = 0; 579 outHeader->nTimeStamp = 0; 580 outHeader->nOffset = 0; 581 582 if (inHeader != NULL && (inHeader->nFlags & OMX_BUFFERFLAG_EOS)) { 583 mReceivedEOS = true; 584 if (inHeader->nFilledLen == 0) { 585 inQueue.erase(inQueue.begin()); 586 inInfo->mOwnedByUs = false; 587 notifyEmptyBufferDone(inHeader); 588 inHeader = NULL; 589 setFlushMode(); 590 } 591 } 592 593 // When there is an init required and the decoder is not in flush mode, 594 // update output port's definition and reinitialize decoder. 595 if (mInitNeeded && !mIsInFlush) { 596 bool portWillReset = false; 597 handlePortSettingsChange(&portWillReset, mNewWidth, mNewHeight); 598 599 CHECK_EQ(reInitDecoder(), (status_t)OK); 600 return; 601 } 602 603 /* Get a free slot in timestamp array to hold input timestamp */ 604 { 605 size_t i; 606 timeStampIx = 0; 607 for (i = 0; i < MAX_TIME_STAMPS; i++) { 608 if (!mTimeStampsValid[i]) { 609 timeStampIx = i; 610 break; 611 } 612 } 613 if (inHeader != NULL) { 614 mTimeStampsValid[timeStampIx] = true; 615 mTimeStamps[timeStampIx] = inHeader->nTimeStamp; 616 } 617 } 618 619 { 620 ivd_video_decode_ip_t s_dec_ip; 621 ivd_video_decode_op_t s_dec_op; 622 WORD32 timeDelay, timeTaken; 623 size_t sizeY, sizeUV; 624 625 setDecodeArgs(&s_dec_ip, &s_dec_op, inHeader, outHeader, timeStampIx); 626 // If input dump is enabled, then write to file 627 DUMP_TO_FILE(mInFile, s_dec_ip.pv_stream_buffer, s_dec_ip.u4_num_Bytes); 628 629 if (s_dec_ip.u4_num_Bytes > 0) { 630 char *ptr = (char *)s_dec_ip.pv_stream_buffer; 631 } 632 633 GETTIME(&mTimeStart, NULL); 634 /* Compute time elapsed between end of previous decode() 635 * to start of current decode() */ 636 TIME_DIFF(mTimeEnd, mTimeStart, timeDelay); 637 638 IV_API_CALL_STATUS_T status; 639 status = ivdec_api_function(mCodecCtx, (void *)&s_dec_ip, (void *)&s_dec_op); 640 641 bool unsupportedDimensions = (IMPEG2D_UNSUPPORTED_DIMENSIONS == s_dec_op.u4_error_code); 642 bool resChanged = (IVD_RES_CHANGED == (s_dec_op.u4_error_code & 0xFF)); 643 644 GETTIME(&mTimeEnd, NULL); 645 /* Compute time taken for decode() */ 646 TIME_DIFF(mTimeStart, mTimeEnd, timeTaken); 647 648 ALOGV("timeTaken=%6d delay=%6d numBytes=%6d", timeTaken, timeDelay, 649 s_dec_op.u4_num_bytes_consumed); 650 if (s_dec_op.u4_frame_decoded_flag && !mFlushNeeded) { 651 mFlushNeeded = true; 652 } 653 654 if ((inHeader != NULL) && (1 != s_dec_op.u4_frame_decoded_flag)) { 655 /* If the input did not contain picture data, then ignore 656 * the associated timestamp */ 657 mTimeStampsValid[timeStampIx] = false; 658 } 659 660 // This is needed to handle CTS DecoderTest testCodecResetsMPEG2WithoutSurface, 661 // which is not sending SPS/PPS after port reconfiguration and flush to the codec. 662 if (unsupportedDimensions && !mFlushNeeded) { 663 bool portWillReset = false; 664 handlePortSettingsChange(&portWillReset, s_dec_op.u4_pic_wd, s_dec_op.u4_pic_ht); 665 666 CHECK_EQ(reInitDecoder(), (status_t)OK); 667 668 setDecodeArgs(&s_dec_ip, &s_dec_op, inHeader, outHeader, timeStampIx); 669 670 ivdec_api_function(mCodecCtx, (void *)&s_dec_ip, (void *)&s_dec_op); 671 return; 672 } 673 674 // If the decoder is in the changing resolution mode and there is no output present, 675 // that means the switching is done and it's ready to reset the decoder and the plugin. 676 if (mChangingResolution && !s_dec_op.u4_output_present) { 677 mChangingResolution = false; 678 resetDecoder(); 679 resetPlugin(); 680 continue; 681 } 682 683 if (unsupportedDimensions || resChanged) { 684 mChangingResolution = true; 685 if (mFlushNeeded) { 686 setFlushMode(); 687 } 688 689 if (unsupportedDimensions) { 690 mNewWidth = s_dec_op.u4_pic_wd; 691 mNewHeight = s_dec_op.u4_pic_ht; 692 mInitNeeded = true; 693 } 694 continue; 695 } 696 697 if ((0 < s_dec_op.u4_pic_wd) && (0 < s_dec_op.u4_pic_ht)) { 698 uint32_t width = s_dec_op.u4_pic_wd; 699 uint32_t height = s_dec_op.u4_pic_ht; 700 bool portWillReset = false; 701 handlePortSettingsChange(&portWillReset, width, height); 702 703 if (portWillReset) { 704 resetDecoder(); 705 return; 706 } 707 } 708 709 if (s_dec_op.u4_output_present) { 710 size_t timeStampIdx; 711 outHeader->nFilledLen = (mWidth * mHeight * 3) / 2; 712 713 timeStampIdx = getMinTimestampIdx(mTimeStamps, mTimeStampsValid); 714 outHeader->nTimeStamp = mTimeStamps[timeStampIdx]; 715 mTimeStampsValid[timeStampIdx] = false; 716 717 /* mWaitForI waits for the first I picture. Once made FALSE, it 718 has to remain false till explicitly set to TRUE. */ 719 mWaitForI = mWaitForI && !(IV_I_FRAME == s_dec_op.e_pic_type); 720 721 if (mWaitForI) { 722 s_dec_op.u4_output_present = false; 723 } else { 724 ALOGV("Output timestamp: %lld, res: %ux%u", 725 (long long)outHeader->nTimeStamp, mWidth, mHeight); 726 DUMP_TO_FILE(mOutFile, outHeader->pBuffer, outHeader->nFilledLen); 727 outInfo->mOwnedByUs = false; 728 outQueue.erase(outQueue.begin()); 729 outInfo = NULL; 730 notifyFillBufferDone(outHeader); 731 outHeader = NULL; 732 } 733 } else { 734 /* If in flush mode and no output is returned by the codec, 735 * then come out of flush mode */ 736 mIsInFlush = false; 737 738 /* If EOS was recieved on input port and there is no output 739 * from the codec, then signal EOS on output port */ 740 if (mReceivedEOS) { 741 outHeader->nFilledLen = 0; 742 outHeader->nFlags |= OMX_BUFFERFLAG_EOS; 743 744 outInfo->mOwnedByUs = false; 745 outQueue.erase(outQueue.begin()); 746 outInfo = NULL; 747 notifyFillBufferDone(outHeader); 748 outHeader = NULL; 749 resetPlugin(); 750 } 751 } 752 } 753 754 // TODO: Handle more than one picture data 755 if (inHeader != NULL) { 756 inInfo->mOwnedByUs = false; 757 inQueue.erase(inQueue.begin()); 758 inInfo = NULL; 759 notifyEmptyBufferDone(inHeader); 760 inHeader = NULL; 761 } 762 } 763} 764 765} // namespace android 766 767android::SoftOMXComponent *createSoftOMXComponent( 768 const char *name, const OMX_CALLBACKTYPE *callbacks, OMX_PTR appData, 769 OMX_COMPONENTTYPE **component) { 770 return new android::SoftMPEG2(name, callbacks, appData, component); 771} 772