gboolean gst_imx_vpu_base_enc_load(void) { VpuEncRetCode ret; #define VPUINIT_ERR(RET, DESC, UNLOAD) \ if ((RET) != VPU_ENC_RET_SUCCESS) \ { \ g_mutex_unlock(&inst_counter_mutex); \ GST_ERROR("%s: %s", (DESC), gst_imx_vpu_strerror(RET)); \ if (UNLOAD) \ VPU_EncUnLoad(); \ return FALSE; \ } g_mutex_lock(&inst_counter_mutex); if (inst_counter == 0) { ret = VPU_EncLoad(); VPUINIT_ERR(ret, "loading VPU encoder failed", FALSE); { VpuVersionInfo version; VpuWrapperVersionInfo wrapper_version; ret = VPU_EncGetVersionInfo(&version); VPUINIT_ERR(ret, "getting version info failed", TRUE); ret = VPU_EncGetWrapperVersionInfo(&wrapper_version); VPUINIT_ERR(ret, "getting wrapper version info failed", TRUE); GST_INFO("VPU encoder loaded"); GST_INFO("VPU firmware version %d.%d.%d_r%d", version.nFwMajor, version.nFwMinor, version.nFwRelease, version.nFwCode); GST_INFO("VPU library version %d.%d.%d", version.nLibMajor, version.nLibMinor, version.nLibRelease); GST_INFO("VPU wrapper version %d.%d.%d %s", wrapper_version.nMajor, wrapper_version.nMinor, wrapper_version.nRelease, wrapper_version.pBinary); } } ++inst_counter; g_mutex_unlock(&inst_counter_mutex); #undef VPUINIT_ERR return TRUE; }
static gboolean gst_fsl_vpu_base_enc_start(GstVideoEncoder *encoder) { VpuEncRetCode ret; GstFslVpuBaseEnc *vpu_base_enc; GstFslVpuBaseEncClass *klass; vpu_base_enc = GST_FSL_VPU_BASE_ENC(encoder); klass = GST_FSL_VPU_BASE_ENC_CLASS(G_OBJECT_GET_CLASS(vpu_base_enc)); #define VPUINIT_ERR(RET, DESC, UNLOAD) \ if ((RET) != VPU_ENC_RET_SUCCESS) \ { \ g_mutex_unlock(&inst_counter_mutex); \ GST_ELEMENT_ERROR(vpu_base_enc, LIBRARY, INIT, ("%s: %s", (DESC), gst_fsl_vpu_strerror(RET)), (NULL)); \ if (UNLOAD) \ VPU_EncUnLoad(); \ return FALSE; \ } g_mutex_lock(&inst_counter_mutex); if (klass->inst_counter == 0) { ret = VPU_EncLoad(); VPUINIT_ERR(ret, "loading VPU encoder failed", FALSE); { VpuVersionInfo version; VpuWrapperVersionInfo wrapper_version; ret = VPU_EncGetVersionInfo(&version); VPUINIT_ERR(ret, "getting version info failed", TRUE); ret = VPU_EncGetWrapperVersionInfo(&wrapper_version); VPUINIT_ERR(ret, "getting wrapper version info failed", TRUE); GST_INFO_OBJECT(vpu_base_enc, "VPU encoder loaded"); GST_INFO_OBJECT(vpu_base_enc, "VPU firmware version %d.%d.%d_r%d", version.nFwMajor, version.nFwMinor, version.nFwRelease, version.nFwCode); GST_INFO_OBJECT(vpu_base_enc, "VPU library version %d.%d.%d", version.nLibMajor, version.nLibMinor, version.nLibRelease); GST_INFO_OBJECT(vpu_base_enc, "VPU wrapper version %d.%d.%d %s", wrapper_version.nMajor, wrapper_version.nMinor, wrapper_version.nRelease, wrapper_version.pBinary); } } ++klass->inst_counter; g_mutex_unlock(&inst_counter_mutex); /* mem_info contains information about how to set up memory blocks * the VPU uses as temporary storage (they are "work buffers") */ memset(&(vpu_base_enc->mem_info), 0, sizeof(VpuMemInfo)); ret = VPU_EncQueryMem(&(vpu_base_enc->mem_info)); if (ret != VPU_ENC_RET_SUCCESS) { GST_ERROR_OBJECT(vpu_base_enc, "could not get VPU memory information: %s", gst_fsl_vpu_strerror(ret)); return FALSE; } /* Allocate the work buffers * Note that these are independent of encoder instances, so they * are allocated before the VPU_EncOpen() call, and are not * recreated in set_format */ if (!gst_fsl_vpu_base_enc_alloc_enc_mem_blocks(vpu_base_enc)) return FALSE; #undef VPUINIT_ERR /* The encoder is initialized in set_format, not here, since only then the input bitstream * format is known (and this information is necessary for initialization). */ return TRUE; }
int enc_init(int w, int h, int fps, VpuCodStd std) { int tmp, nBufNum, nAlign, nSize; VpuEncRetCode res; VpuVersionInfo ver; VpuWrapperVersionInfo vver; VpuMemInfo sMemInfo; VpuEncOpenParam sEncOpenParam; VpuEncInitInfo sEncInitInfo; VpuFrameBuffer sFrameBuf[MAX_FRAME_NUM]; res = VPU_EncLoad(); if(res != VPU_ENC_RET_SUCCESS) { printf("VPU_ENC load error : %d\n", res); return -1; } res = VPU_EncGetVersionInfo(&ver); if (res != VPU_ENC_RET_SUCCESS) { printf("VPU_ENC get version error : %d\n", res); goto err; } res = VPU_EncGetWrapperVersionInfo(&vver); if (res != VPU_ENC_RET_SUCCESS) { printf("VPU_ENC get wrapper version error : %d\n", res); goto err; } printf("======= VPU ENC =======\n"); printf(" LIB : %d.%d.%d\n", ver.nLibMajor, ver.nLibMinor, ver.nLibRelease); printf(" FW : %d.%d.%d.%d\n", ver.nFwMajor, ver.nFwMinor, ver.nFwRelease, ver.nFwCode); printf(" WLIB: %d.%d.%d\n", vver.nMajor, vver.nMinor, vver.nRelease); printf("=======================\n"); res = VPU_EncQueryMem(&sMemInfo); if (res != VPU_ENC_RET_SUCCESS) { printf("VPU_ENC query memory error : %d\n", res); goto err; } tmp = enc_mem_alloc(&sMemInfo, &gMemInfo); if(tmp) { printf("enc_mem_alloc error\n"); goto err; } bzero(&sEncOpenParam, sizeof(VpuEncOpenParam)); sEncOpenParam.eFormat = std; sEncOpenParam.sMirror = VPU_ENC_MIRDIR_NONE; sEncOpenParam.nPicWidth = w; sEncOpenParam.nPicHeight = h; sEncOpenParam.nRotAngle = 0; sEncOpenParam.nFrameRate = fps; sEncOpenParam.nBitRate = 5000; sEncOpenParam.nGOPSize = 100; sEncOpenParam.nChromaInterleave = 0; sEncOpenParam.nMapType = 0; sEncOpenParam.nLinear2TiledEnable = 0; sEncOpenParam.eColorFormat = VPU_COLOR_420; sEncOpenParam.nInitialDelay = 0; sEncOpenParam.nVbvBufferSize = 0; sEncOpenParam.sliceMode.sliceMode = 0; /* 0: 1 slice per picture; 1: Multiple slices per picture */ sEncOpenParam.sliceMode.sliceSizeMode = 0; /* 0: silceSize defined by bits; 1: sliceSize defined by MB number*/ sEncOpenParam.sliceMode.sliceSize = 4000;//4000; /* Size of a slice in bits or MB numbers */ //sEncOpenParam.enableAutoSkip = 1; sEncOpenParam.nUserGamma = 0.75*32768; /* (0*32768 <= gamma <= 1*32768) */ sEncOpenParam.nRcIntervalMode = 0; //1; /* 0:normal, 1:frame_level, 2:slice_level, 3: user defined Mb_level */ sEncOpenParam.nMbInterval = 0; // sEncOpenParam.nAvcIntra16x16OnlyModeEnable = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_constrainedIntraPredFlag = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_disableDeblk = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_deblkFilterOffsetAlpha = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_deblkFilterOffsetBeta = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_chromaQpOffset = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_audEnable = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_fmoEnable = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_fmoType = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_fmoSliceNum = 0; // sEncOpenParam.VpuEncStdParam.avcParam.avc_fmoSliceSaveBufSize = 32; /* FMO_SLICE_SAVE_BUF_SIZE */ res = VPU_EncOpen(&gHandle, &sMemInfo, &sEncOpenParam); if (res != VPU_ENC_RET_SUCCESS) { printf("VPU_ENC open error : %d\n", res); goto err1; } res = VPU_EncConfig(gHandle, VPU_ENC_CONF_NONE, NULL); if(VPU_ENC_RET_SUCCESS != res) { printf("VPU_ENC config error : %d\n", res); goto err2; } //get initinfo res = VPU_EncGetInitialInfo(gHandle, &sEncInitInfo); if(VPU_ENC_RET_SUCCESS != res) { printf("VPU_ENC get Init Info error : %d\n", res); goto err2; } nBufNum = sEncInitInfo.nMinFrameBufferCount; nAlign = sEncInitInfo.nAddressAlignment; printf("Init OK: min buffer cnt: %d, alignment: %d\n", nBufNum, nAlign); tmp = enc_mem_frame(sEncOpenParam.eFormat, sFrameBuf, nBufNum, w, h, &gMemInfo, nAlign); if(tmp) { printf("enc_mem_frame error\n"); goto err2; } res = VPU_EncRegisterFrameBuffer(gHandle, sFrameBuf, nBufNum, w); if(VPU_ENC_RET_SUCCESS != res) { printf("VPU_ENC register frame buffer error : %d\n", res); goto err2; } nSize = w * h * 3 / 2; nSize += nAlign * 2; bzero(&sMemInfo, sizeof(VpuMemInfo)); sMemInfo.nSubBlockNum = 2; sMemInfo.MemSubBlock[0].MemType = VPU_MEM_PHY; sMemInfo.MemSubBlock[0].nAlignment = nAlign; sMemInfo.MemSubBlock[0].nSize = nSize; sMemInfo.MemSubBlock[1].MemType = VPU_MEM_PHY; sMemInfo.MemSubBlock[1].nAlignment = nAlign; sMemInfo.MemSubBlock[1].nSize = nSize; nSize = enc_mem_alloc(&sMemInfo, &gMemInfo); if(nSize) { printf("enc_mem_alloc error\n"); goto err2; } bzero(&gEncParam, sizeof(VpuEncEncParam)); gEncParam.eFormat = VPU_V_AVC; gEncParam.nPicWidth = w; gEncParam.nPicHeight = h; gEncParam.nFrameRate = sEncOpenParam.nFrameRate; gEncParam.nQuantParam = 0; gEncParam.nInPhyInput = (unsigned int)sMemInfo.MemSubBlock[0].pPhyAddr; gEncParam.nInVirtInput = (unsigned int)sMemInfo.MemSubBlock[0].pVirtAddr; gEncParam.nInInputSize = nSize; gEncParam.nInPhyOutput = (unsigned int)sMemInfo.MemSubBlock[1].pPhyAddr; gEncParam.nInVirtOutput=(unsigned int)sMemInfo.MemSubBlock[1].pVirtAddr; gEncParam.nInOutputBufLen = nSize; gEncParam.nForceIPicture = 0; gEncParam.nSkipPicture = 0; gEncParam.nEnableAutoSkip = 0; gEncParam.pInFrame = NULL; return 0; err2: VPU_EncClose(gHandle); err1: enc_mem_free(&gMemInfo); err: VPU_EncUnLoad(); return -1; }