Exemplo n.º 1
0
static gboolean gst_imx_vpu_base_enc_set_bitrate (GstImxVpuBaseEnc *vpu_base_enc)
{
	VpuEncRetCode ret;

	if (vpu_base_enc->bitrate != 0)
	{
		GST_INFO_OBJECT(vpu_base_enc, "Configuring bitrate to %d", vpu_base_enc->bitrate);
		int param = vpu_base_enc->bitrate;
		ret = VPU_EncConfig(vpu_base_enc->handle, VPU_ENC_CONF_BIT_RATE, &param);
		if (ret != VPU_ENC_RET_SUCCESS)
		{
			GST_ERROR_OBJECT(vpu_base_enc, "could not configure bitrate: %s", gst_imx_vpu_strerror(ret));
			return FALSE;
		}
	}

	return TRUE;
}
Exemplo n.º 2
0
static gboolean gst_fsl_vpu_base_enc_set_format(GstVideoEncoder *encoder, GstVideoCodecState *state)
{
	VpuEncRetCode ret;
	GstVideoCodecState *output_state;
	GstFslVpuBaseEncClass *klass;
	GstFslVpuBaseEnc *vpu_base_enc;
	
	vpu_base_enc = GST_FSL_VPU_BASE_ENC(encoder);
	klass = GST_FSL_VPU_BASE_ENC_CLASS(G_OBJECT_GET_CLASS(vpu_base_enc));

	g_assert(klass->set_open_params != NULL);
	g_assert(klass->get_output_caps != NULL);

	/* Close old encoder instance */
	gst_fsl_vpu_base_enc_close_encoder(vpu_base_enc);

	/* Clean up existing framebuffers structure;
	 * if some previous and still existing buffer pools depend on this framebuffers
	 * structure, they will extend its lifetime, since they ref'd it
	 */
	if (vpu_base_enc->framebuffers != NULL)
	{
		gst_object_unref(vpu_base_enc->framebuffers);
		vpu_base_enc->framebuffers = NULL;
	}

	if (vpu_base_enc->output_phys_buffer != NULL)
	{
		gst_allocator_free(gst_fsl_vpu_enc_allocator_obtain(), (GstMemory *)(vpu_base_enc->output_phys_buffer));
		vpu_base_enc->output_phys_buffer = NULL;
	}

	memset(&(vpu_base_enc->open_param), 0, sizeof(VpuEncOpenParam));

	/* These params are usually not set by derived classes */
	vpu_base_enc->open_param.nPicWidth = GST_VIDEO_INFO_WIDTH(&(state->info));
	vpu_base_enc->open_param.nPicHeight = GST_VIDEO_INFO_HEIGHT(&(state->info));
	vpu_base_enc->open_param.nFrameRate = (GST_VIDEO_INFO_FPS_N(&(state->info)) & 0xffffUL) | (((GST_VIDEO_INFO_FPS_D(&(state->info)) - 1) & 0xffffUL) << 16);
	vpu_base_enc->open_param.sMirror = VPU_ENC_MIRDIR_NONE; /* don't use VPU rotation (IPU has better performance) */
	vpu_base_enc->open_param.nBitRate = vpu_base_enc->bitrate;
	vpu_base_enc->open_param.nGOPSize = vpu_base_enc->gop_size;
	vpu_base_enc->open_param.nUserGamma = (int)(32768 * vpu_base_enc->qp_smoothing);
	vpu_base_enc->open_param.nAvcIntra16x16OnlyModeEnable = vpu_base_enc->intra_16x16_only ? 1 : 0;
	vpu_base_enc->open_param.nRcIntervalMode = 1;
	/* These params are defaults, and are often overwritten by derived classes */
	vpu_base_enc->open_param.nUserQpMax = -1;
	vpu_base_enc->open_param.nUserQpMin = -1;
	vpu_base_enc->open_param.nRcIntraQp = -1;

	GST_DEBUG_OBJECT(vpu_base_enc, "setting bitrate to %u kbps and GOP size to %u", vpu_base_enc->open_param.nBitRate, vpu_base_enc->open_param.nGOPSize);

	/* Give the derived class a chance to set params */
	if (!klass->set_open_params(vpu_base_enc, &(vpu_base_enc->open_param)))
	{
		GST_ERROR_OBJECT(vpu_base_enc, "derived class could not set open params");
		return FALSE;
	}

	/* The actual initialization; requires bitstream information (such as the codec type), which
	 * is determined by the fill_param_set call before */
	ret = VPU_EncOpen(&(vpu_base_enc->handle), &(vpu_base_enc->mem_info), &(vpu_base_enc->open_param));
	if (ret != VPU_ENC_RET_SUCCESS)
	{
		GST_ERROR_OBJECT(vpu_base_enc, "opening new VPU handle failed: %s", gst_fsl_vpu_strerror(ret));
		return FALSE;
	}

	vpu_base_enc->vpu_inst_opened = TRUE;

	/* configure AFTER setting vpu_inst_opened to TRUE, to make sure that in case of
	   config failure the VPU handle is closed in the finalizer */

	ret = VPU_EncConfig(vpu_base_enc->handle, VPU_ENC_CONF_NONE, NULL);
	if (ret != VPU_ENC_RET_SUCCESS)
	{
		GST_ERROR_OBJECT(vpu_base_enc, "could not apply default configuration: %s", gst_fsl_vpu_strerror(ret));
		return FALSE;
	}

	ret = VPU_EncGetInitialInfo(vpu_base_enc->handle, &(vpu_base_enc->init_info));
	if (ret != VPU_ENC_RET_SUCCESS)
	{
		GST_ERROR_OBJECT(vpu_base_enc, "retrieving init info failed: %s", gst_fsl_vpu_strerror(ret));
		return FALSE;
	}

	/* Framebuffers are created in handle_frame(), to make sure the actual stride is used */

	/* Set the output state, using caps defined by the derived class */
	output_state = gst_video_encoder_set_output_state(
		encoder,
		klass->get_output_caps(vpu_base_enc),
		state
	);
	gst_video_codec_state_unref(output_state);

	vpu_base_enc->video_info = state->info;

	return TRUE;
}
Exemplo n.º 3
0
static gboolean gst_imx_vpu_base_enc_set_format(GstVideoEncoder *encoder, GstVideoCodecState *state)
{
	VpuEncRetCode ret;
	GstVideoCodecState *output_state;
	GstImxVpuBaseEncClass *klass;
	GstImxVpuBaseEnc *vpu_base_enc;
	int param;
	
	vpu_base_enc = GST_IMX_VPU_BASE_ENC(encoder);
	klass = GST_IMX_VPU_BASE_ENC_CLASS(G_OBJECT_GET_CLASS(vpu_base_enc));

	g_assert(klass->set_open_params != NULL);
	g_assert(klass->get_output_caps != NULL);

	/* Close old encoder instance */
	gst_imx_vpu_base_enc_close_encoder(vpu_base_enc);

	/* Clean up existing framebuffers structure;
	 * if some previous and still existing buffer pools depend on this framebuffers
	 * structure, they will extend its lifetime, since they ref'd it
	 */
	if (vpu_base_enc->framebuffers != NULL)
	{
		gst_object_unref(vpu_base_enc->framebuffers);
		vpu_base_enc->framebuffers = NULL;
	}

	if (vpu_base_enc->output_phys_buffer != NULL)
	{
		gst_allocator_free(gst_imx_vpu_enc_allocator_obtain(), (GstMemory *)(vpu_base_enc->output_phys_buffer));
		vpu_base_enc->output_phys_buffer = NULL;
	}

	memset(&(vpu_base_enc->open_param), 0, sizeof(VpuEncOpenParam));

	/* These params are usually not set by derived classes */
	vpu_base_enc->open_param.nPicWidth = GST_VIDEO_INFO_WIDTH(&(state->info));
	vpu_base_enc->open_param.nPicHeight = GST_VIDEO_INFO_HEIGHT(&(state->info));
	vpu_base_enc->open_param.nFrameRate = (GST_VIDEO_INFO_FPS_N(&(state->info)) & 0xffffUL) | (((GST_VIDEO_INFO_FPS_D(&(state->info)) - 1) & 0xffffUL) << 16);
	vpu_base_enc->open_param.sMirror = VPU_ENC_MIRDIR_NONE; /* don't use VPU mirroring (IPU has better performance) */
	vpu_base_enc->open_param.nBitRate = vpu_base_enc->bitrate;
	vpu_base_enc->open_param.nGOPSize = vpu_base_enc->gop_size;

	GST_INFO_OBJECT(vpu_base_enc, "setting bitrate to %u kbps and GOP size to %u", vpu_base_enc->open_param.nBitRate, vpu_base_enc->open_param.nGOPSize);

	/* These are default settings from VPU_EncOpenSimp */
	vpu_base_enc->open_param.sliceMode.sliceMode = 0; /* 1 slice per picture */
	vpu_base_enc->open_param.sliceMode.sliceSizeMode = 0; /* sliceSize is bits */
	vpu_base_enc->open_param.sliceMode.sliceSize = 4000;
	vpu_base_enc->open_param.nRcIntraQp = -1;
	vpu_base_enc->open_param.nUserGamma = 0.75 * 32768;

	if (vpu_base_enc->slice_size) {
		vpu_base_enc->open_param.sliceMode.sliceMode = 1;	/* 0: 1 slice per picture; 1: Multiple slices per picture */
		if (vpu_base_enc->slice_size < 0) {
			vpu_base_enc->open_param.sliceMode.sliceSizeMode = 1; /* 1: sliceSize defined by MB number*/
			vpu_base_enc->open_param.sliceMode.sliceSize = -vpu_base_enc->slice_size;  /* Size of a slice in MB numbers */
		} else {
			vpu_base_enc->open_param.sliceMode.sliceSizeMode = 0; /* 1: sliceSize defined by bits */
			vpu_base_enc->open_param.sliceMode.sliceSize = vpu_base_enc->slice_size;  /* Size of a slice in bits */
		}
	}

	vpu_base_enc->open_param.nIntraRefresh = vpu_base_enc->intra_refresh;

	/* Give the derived class a chance to set params */
	if (!klass->set_open_params(vpu_base_enc, state, &(vpu_base_enc->open_param)))
	{
		GST_ERROR_OBJECT(vpu_base_enc, "derived class could not set open params");
		return FALSE;
	}

	/* The actual initialization; requires bitstream information (such as the codec type), which
	 * is determined by the fill_param_set call before */
	ret = VPU_EncOpen(&(vpu_base_enc->handle), &(vpu_base_enc->mem_info), &(vpu_base_enc->open_param));
	if (ret != VPU_ENC_RET_SUCCESS)
	{
		GST_ERROR_OBJECT(vpu_base_enc, "opening new VPU handle failed: %s", gst_imx_vpu_strerror(ret));
		return FALSE;
	}

	vpu_base_enc->vpu_inst_opened = TRUE;

	/* configure AFTER setting vpu_inst_opened to TRUE, to make sure that in case of
	   config failure the VPU handle is closed in the finalizer */

	if (vpu_base_enc->bitrate != 0)
	{
		param = vpu_base_enc->bitrate;
		ret = VPU_EncConfig(vpu_base_enc->handle, VPU_ENC_CONF_BIT_RATE, &param);
		if (ret != VPU_ENC_RET_SUCCESS)
		{
			GST_ERROR_OBJECT(vpu_base_enc, "could not configure bitrate: %s", gst_imx_vpu_strerror(ret));
			return FALSE;
		}
	}

	if (vpu_base_enc->intra_refresh != 0)
	{
		param = vpu_base_enc->intra_refresh;
		ret = VPU_EncConfig(vpu_base_enc->handle, VPU_ENC_CONF_INTRA_REFRESH, &param);
		if (ret != VPU_ENC_RET_SUCCESS)
		{
			GST_ERROR_OBJECT(vpu_base_enc, "could not configure intra refresh period: %s", gst_imx_vpu_strerror(ret));
			return FALSE;
		}
	}

	ret = VPU_EncGetInitialInfo(vpu_base_enc->handle, &(vpu_base_enc->init_info));
	if (ret != VPU_ENC_RET_SUCCESS)
	{
		GST_ERROR_OBJECT(vpu_base_enc, "retrieving init info failed: %s", gst_imx_vpu_strerror(ret));
		return FALSE;
	}

	/* Framebuffers are created in handle_frame(), to make sure the actual stride is used */

	/* Set the output state, using caps defined by the derived class */
	output_state = gst_video_encoder_set_output_state(
		encoder,
		klass->get_output_caps(vpu_base_enc),
		state
	);
	gst_video_codec_state_unref(output_state);

	vpu_base_enc->video_info = state->info;

	return TRUE;
}
Exemplo n.º 4
0
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;
}