ImxVpuDecReturnCodes imx_vpu_dec_get_decoded_frame(ImxVpuDecoder *decoder, ImxVpuDecodedFrame *decoded_frame) { VpuDecRetCode ret; VpuDecOutFrameInfo out_frame_info; int fb_index; void *user_data; ret = VPU_DecGetOutputFrame(decoder->handle, &out_frame_info); if (ret != VPU_DEC_RET_SUCCESS) { ImxVpuDecReturnCodes imxret = dec_convert_retcode(ret); IMX_VPU_ERROR("error getting decoded output frame: %s", imx_vpu_dec_error_string(imxret)); return imxret; } fb_index = dec_get_wrapper_framebuffer_index(decoder, out_frame_info.pDisplayFrameBuf); user_data = NULL; if (decoder->consumption_info_available) { if ((fb_index >= 0) && (fb_index < (int)(decoder->num_framebuffers))) { user_data = decoder->user_data_for_frames[fb_index]; IMX_VPU_LOG("framebuffer index %d for framebuffer %p and user data %p", fb_index, (void *)(out_frame_info.pDisplayFrameBuf), user_data); decoder->user_data_for_frames[fb_index] = NULL; } else IMX_VPU_ERROR("framebuffer index %d for framebuffer %p and user data %p out of bounds", fb_index, (void *)(out_frame_info.pDisplayFrameBuf), user_data); } else { if (decoder->num_user_data > 0) { user_data = decoder->user_data_for_frames[0]; decoder->user_data_for_frames[0] = NULL; IMX_VPU_LOG("framebuffer index %d user data %p retrieved as oldest", fb_index, user_data); memmove(decoder->user_data_for_frames, decoder->user_data_for_frames + 1, sizeof(void*) * (decoder->num_user_data - 1)); decoder->num_user_data--; } } decoded_frame->pic_type = convert_from_wrapper_pic_type(out_frame_info.ePicType); decoded_frame->user_data = user_data; /* XXX * This association assumes that the order of internal framebuffer entries * inside the VPU wrapper is the same as the order of the framebuffers here. * So, decoder->framebuffers[1] equals internal framebuffer entry with index 1 etc. */ decoded_frame->framebuffer = &(decoder->framebuffers[fb_index]); /* This is used in imx_vpu_dec_mark_framebuffer_as_displayed() to be able * to mark the vpuwrapper framebuffer as displayed */ decoded_frame->framebuffer->internal = out_frame_info.pDisplayFrameBuf; decoder->num_framebuffers_in_use++; return IMX_VPU_DEC_RETURN_CODE_OK; }
int CDVDVideoCodecIMX::Decode(BYTE *pData, int iSize, double dts, double pts) { VpuDecFrameLengthInfo frameLengthInfo; VpuBufferNode inData; VpuDecRetCode ret; int decRet = 0; int retStatus = 0; int demuxer_bytes = iSize; uint8_t *demuxer_content = pData; int retries = 0; int idx; #ifdef IMX_PROFILE static unsigned long long previous, current; unsigned long long before_dec; #endif if (!m_vpuHandle) { VpuOpen(); if (!m_vpuHandle) return VC_ERROR; } for (int i=0; i < m_vpuFrameBufferNum; i++) { if (m_outputBuffers[i]->Rendered()) { ret = m_outputBuffers[i]->ReleaseFramebuffer(&m_vpuHandle); if(ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s: vpu clear frame display failure: ret=%d \r\n",__FUNCTION__,ret); } } } #ifdef IMX_PROFILE current = XbmcThreads::SystemClockMillis(); CLog::Log(LOGDEBUG, "%s - delta time decode : %llu - demux size : %d dts : %f - pts : %f\n", __FUNCTION__, current - previous, iSize, dts, pts); previous = current; #endif if ((pData && iSize) || (m_bytesToBeConsumed)) { if ((m_convert_bitstream) && (iSize)) { // convert demuxer packet from bitstream to bytestream (AnnexB) if (m_converter->Convert(demuxer_content, demuxer_bytes)) { demuxer_content = m_converter->GetConvertBuffer(); demuxer_bytes = m_converter->GetConvertSize(); } else CLog::Log(LOGERROR,"%s - bitstream_convert error", __FUNCTION__); } inData.nSize = demuxer_bytes; inData.pPhyAddr = NULL; inData.pVirAddr = demuxer_content; if ((m_decOpenParam.CodecFormat == VPU_V_MPEG2) || (m_decOpenParam.CodecFormat == VPU_V_VC1_AP)|| (m_decOpenParam.CodecFormat == VPU_V_XVID)) { inData.sCodecData.pData = (unsigned char *)m_hints.extradata; inData.sCodecData.nSize = m_hints.extrasize; } else { inData.sCodecData.pData = NULL; inData.sCodecData.nSize = 0; } while (true) // Decode as long as the VPU consumes data { #ifdef IMX_PROFILE before_dec = XbmcThreads::SystemClockMillis(); #endif if (m_frameReported) m_bytesToBeConsumed += inData.nSize; ret = VPU_DecDecodeBuf(m_vpuHandle, &inData, &decRet); #ifdef IMX_PROFILE CLog::Log(LOGDEBUG, "%s - VPU dec 0x%x decode takes : %lld\n\n", __FUNCTION__, decRet, XbmcThreads::SystemClockMillis() - before_dec); #endif if (ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s - VPU decode failed with error code %d.\n", __FUNCTION__, ret); goto out_error; } if (decRet & VPU_DEC_INIT_OK) // VPU decoding init OK : We can retrieve stream info { ret = VPU_DecGetInitialInfo(m_vpuHandle, &m_initInfo); if (ret == VPU_DEC_RET_SUCCESS) { if (g_advancedSettings.CanLogComponent(LOGVIDEO)) { CLog::Log(LOGDEBUG, "%s - VPU Init Stream Info : %dx%d (interlaced : %d - Minframe : %d)"\ " - Align : %d bytes - crop : %d %d %d %d - Q16Ratio : %x\n", __FUNCTION__, m_initInfo.nPicWidth, m_initInfo.nPicHeight, m_initInfo.nInterlace, m_initInfo.nMinFrameBufferCount, m_initInfo.nAddressAlignment, m_initInfo.PicCropRect.nLeft, m_initInfo.PicCropRect.nTop, m_initInfo.PicCropRect.nRight, m_initInfo.PicCropRect.nBottom, m_initInfo.nQ16ShiftWidthDivHeightRatio); } if (VpuAllocFrameBuffers()) { ret = VPU_DecRegisterFrameBuffer(m_vpuHandle, m_vpuFrameBuffers, m_vpuFrameBufferNum); if (ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s - VPU error while registering frame buffers (%d).\n", __FUNCTION__, ret); goto out_error; } } else { goto out_error; } } else { CLog::Log(LOGERROR, "%s - VPU get initial info failed (%d).\n", __FUNCTION__, ret); goto out_error; } } //VPU_DEC_INIT_OK if (decRet & VPU_DEC_ONE_FRM_CONSUMED) { ret = VPU_DecGetConsumedFrameInfo(m_vpuHandle, &frameLengthInfo); if (ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s - VPU error retireving info about consummed frame (%d).\n", __FUNCTION__, ret); } m_bytesToBeConsumed -= (frameLengthInfo.nFrameLength + frameLengthInfo.nStuffLength); if (frameLengthInfo.pFrame) { idx = VpuFindBuffer(frameLengthInfo.pFrame->pbufY); if (m_bytesToBeConsumed < 50) m_bytesToBeConsumed = 0; if (idx != -1) { if (m_previousPts != DVD_NOPTS_VALUE) { m_outputBuffers[idx]->SetPts(m_previousPts); m_previousPts = DVD_NOPTS_VALUE; } else m_outputBuffers[idx]->SetPts(pts); } else CLog::Log(LOGERROR, "%s - could not find frame buffer\n", __FUNCTION__); } } //VPU_DEC_ONE_FRM_CONSUMED if (decRet & VPU_DEC_OUTPUT_DIS) // Frame ready to be displayed { if (retStatus & VC_PICTURE) CLog::Log(LOGERROR, "%s - Second picture in the same decode call !\n", __FUNCTION__); ret = VPU_DecGetOutputFrame(m_vpuHandle, &m_frameInfo); if(ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s - VPU Cannot get output frame(%d).\n", __FUNCTION__, ret); goto out_error; } // Some codecs (VC1?) lie about their frame size (mod 16). Adjust... m_frameInfo.pExtInfo->nFrmWidth = (((m_frameInfo.pExtInfo->nFrmWidth) + 15) & ~15); m_frameInfo.pExtInfo->nFrmHeight = (((m_frameInfo.pExtInfo->nFrmHeight) + 15) & ~15); retStatus |= VC_PICTURE; } //VPU_DEC_OUTPUT_DIS // According to libfslvpuwrap: If this flag is set then the frame should // be dropped. It is just returned to gather decoder information but not // for display. if (decRet & VPU_DEC_OUTPUT_MOSAIC_DIS) { ret = VPU_DecGetOutputFrame(m_vpuHandle, &m_frameInfo); if(ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s - VPU Cannot get output frame(%d).\n", __FUNCTION__, ret); goto out_error; } // Display frame ret = VPU_DecOutFrameDisplayed(m_vpuHandle, m_frameInfo.pDisplayFrameBuf); if(ret != VPU_DEC_RET_SUCCESS) { CLog::Log(LOGERROR, "%s: VPU Clear frame display failure(%d)\n",__FUNCTION__,ret); goto out_error; } } //VPU_DEC_OUTPUT_MOSAIC_DIS if (decRet & VPU_DEC_OUTPUT_REPEAT) { if (g_advancedSettings.CanLogComponent(LOGVIDEO)) CLog::Log(LOGDEBUG, "%s - Frame repeat.\n", __FUNCTION__); } if (decRet & VPU_DEC_OUTPUT_DROPPED) { if (g_advancedSettings.CanLogComponent(LOGVIDEO)) CLog::Log(LOGDEBUG, "%s - Frame dropped.\n", __FUNCTION__); } if (decRet & VPU_DEC_NO_ENOUGH_BUF) { CLog::Log(LOGERROR, "%s - No frame buffer available.\n", __FUNCTION__); } if (decRet & VPU_DEC_SKIP) { if (g_advancedSettings.CanLogComponent(LOGVIDEO)) CLog::Log(LOGDEBUG, "%s - Frame skipped.\n", __FUNCTION__); } if (decRet & VPU_DEC_FLUSH) { CLog::Log(LOGNOTICE, "%s - VPU requires a flush.\n", __FUNCTION__); Reset(); retStatus = VC_FLUSHED; } if (decRet & VPU_DEC_OUTPUT_EOS) { CLog::Log(LOGNOTICE, "%s - EOS encountered.\n", __FUNCTION__); } if ((decRet & VPU_DEC_NO_ENOUGH_INBUF) || (decRet & VPU_DEC_OUTPUT_DIS)) { // We are done with VPU decoder that time break; } retries++; if (retries >= m_maxVpuDecodeLoops) { CLog::Log(LOGERROR, "%s - Leaving VPU decoding loop after %d iterations\n", __FUNCTION__, m_maxVpuDecodeLoops); break; } if (!(decRet & VPU_DEC_INPUT_USED)) { CLog::Log(LOGERROR, "%s - input not used : addr %p size :%d!\n", __FUNCTION__, inData.pVirAddr, inData.nSize); } // Let's process again as VPU_DEC_NO_ENOUGH_INBUF was not set // and we don't have an image ready if we reach that point inData.pVirAddr = NULL; inData.nSize = 0; } // Decode loop } //(pData && iSize) if (retStatus == 0) { retStatus |= VC_BUFFER; } if (m_bytesToBeConsumed > 0) { // Remember the current pts because the data which has just // been sent to the VPU has not yet been consumed. // This pts is related to the frame that will be consumed // at next call... m_previousPts = pts; } // Store current dts (will be used only if VC_PICTURE is set) m_dts = dts; #ifdef IMX_PROFILE CLog::Log(LOGDEBUG, "%s - returns %x - duration %lld\n", __FUNCTION__, retStatus, XbmcThreads::SystemClockMillis() - previous); #endif return retStatus; out_error: return VC_ERROR; }