homerHEVC代码阅读(33)——计算残差

发布时间:2017-2-26 11:36:05编辑:www.fx114.net 分享查询网我要评论
本篇文章主要介绍了"homerHEVC代码阅读(33)——计算残差",主要涉及到homerHEVC代码阅读(33)——计算残差方面的内容,对于homerHEVC代码阅读(33)——计算残差感兴趣的同学可以参考一下。

预测之后,得到预测块,我们需要用预测块减去原始块,得到残差块,然后才能进行变换量化等操作

int predict_inter(henc_thread_t* et, ctu_info_t* ctu, int gcnt, int depth, int part_position, PartSize part_size_type)
{
	double distortion = 0.;

	slice_t *currslice = &et->enc_engine->current_pict.slice;
	ctu_info_t *ctu_rd = et->ctu_rd;
	/* 原始数据、预测数据、残差数据、参考数据的buffer和步幅 */
	int pred_buff_stride, orig_buff_stride, reference_buff_stride, residual_buff_stride;
	int pred_buff_stride_chroma, orig_buff_stride_chroma, reference_buff_stride_chroma, residual_buff_stride_chroma;
	int16_t  *orig_buff, *orig_buff_u, *orig_buff_v;
	int16_t  *reference_buff_cu_position, *reference_buff_cu_position_u, *reference_buff_cu_position_v;
	int16_t *pred_buff, *pred_buff_u, *pred_buff_v, *residual_buff, *residual_buff_u, *residual_buff_v;
	/* 辅助的buffer */
	int16_t *pred_aux_buffs[NUM_REF_LISTS][NUM_PICT_COMPONENTS];
	/* 参考窗口 */
	wnd_t *reference_wnd = NULL;//, *resi_wnd = NULL;
	int curr_part_size, curr_part_size_shift;
	int curr_part_size_chroma, curr_part_size_shift_chroma;
	int curr_part_x, curr_part_y, curr_part_global_x, curr_part_global_y;
	int curr_part_x_chroma, curr_part_y_chroma, curr_part_global_x_chroma, curr_part_global_y_chroma;
	int curr_depth = depth;
	/* 父亲partition的信息 */
	cu_partition_info_t *parent_part_info;
	/* 当前partition的信息 */
	cu_partition_info_t *curr_cu_info;
	int num_partitions, npart;
	int mv_cost = 0;

	curr_cu_info = &ctu->partition_list[et->partition_depth_start[curr_depth]] + part_position;

	/* 2Nx2N的划分类型,实质就是当前的CU不进行划分*/
	if (part_size_type == SIZE_2Nx2N)
	{
		parent_part_info = curr_cu_info->parent;
		num_partitions = 1;
	}
	/* NxN的划分类型,实际就是把一个CU划分成4个子CU */
	else if (part_size_type == SIZE_NxN)
	{
		parent_part_info = curr_cu_info->parent;
		curr_cu_info = parent_part_info->children[0];
		num_partitions = 4;
	}

	/* 遍历当前CU中的所有的partition  */
	for (npart = 0; npart < num_partitions; npart++)
	{
		//set mvs and ref_idx
		motion_vector_t mv;

		int ref_list = REF_PIC_LIST_0, list_init = REF_PIC_LIST_0, list_last = REF_PIC_LIST_1;

		/* 是否需要双向预测 */
		int uni_prediction = check_unidirectional_motion(currslice, curr_cu_info);

		if (uni_prediction)
		{
			list_init = list_last = ref_list = curr_cu_info->inter_mode == 0x1 ? REF_PIC_LIST_0 : REF_PIC_LIST_1;
		}

		curr_depth = curr_cu_info->depth;
		curr_part_x = curr_cu_info->x_position;
		curr_part_y = curr_cu_info->y_position;
		curr_part_global_x = ctu->x[Y_COMP] + curr_part_x;
		curr_part_global_y = ctu->y[Y_COMP] + curr_part_y;
		curr_part_size = curr_cu_info->size;
		curr_part_size_shift = et->max_cu_size_shift - curr_depth;
		curr_part_x_chroma = curr_cu_info->x_position_chroma;
		curr_part_y_chroma = curr_cu_info->y_position_chroma;
		curr_part_global_x_chroma = ctu->x[CHR_COMP] + curr_part_x_chroma;
		curr_part_global_y_chroma = ctu->y[CHR_COMP] + curr_part_y_chroma;
		curr_part_size_chroma = curr_cu_info->size_chroma;
		curr_part_size_shift_chroma = et->max_cu_size_shift - curr_depth - 1;//420

		pred_buff_stride = WND_STRIDE_2D(et->prediction_wnd[0], Y_COMP);
		pred_buff = WND_POSITION_2D(int16_t *, et->prediction_wnd[0], Y_COMP, curr_part_x, curr_part_y, gcnt, et->ctu_width);
		pred_buff_stride_chroma = WND_STRIDE_2D(et->prediction_wnd[0], CHR_COMP);
		pred_buff_u = WND_POSITION_2D(int16_t *, et->prediction_wnd[0], U_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);
		pred_buff_v = WND_POSITION_2D(int16_t *, et->prediction_wnd[0], V_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);

		/* 遍历参考图像列表 */
		for (ref_list = list_init; ref_list <= list_last; ref_list++)
		{
			/* 获取参考帧索引 */
			int ref_idx = curr_cu_info->inter_ref_index[ref_list];
			/* 获取参考的MV */
			mv = curr_cu_info->inter_mv[ref_list];

			if (uni_prediction)
			{
				pred_aux_buffs[ref_list][Y_COMP] = pred_buff;
				pred_aux_buffs[ref_list][U_COMP] = pred_buff_u;
				pred_aux_buffs[ref_list][V_COMP] = pred_buff_v;
			}
			else
			{
				pred_aux_buffs[ref_list][Y_COMP] = WND_POSITION_2D(int16_t *, et->prediction_wnd[1 + ref_list], Y_COMP, curr_part_x, curr_part_y, gcnt, et->ctu_width);
				pred_aux_buffs[ref_list][U_COMP] = WND_POSITION_2D(int16_t *, et->prediction_wnd[1 + ref_list], U_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);
				pred_aux_buffs[ref_list][V_COMP] = WND_POSITION_2D(int16_t *, et->prediction_wnd[1 + ref_list], V_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);
			}

			orig_buff_stride = WND_STRIDE_2D(et->curr_mbs_wnd, Y_COMP);
			orig_buff = WND_POSITION_2D(int16_t *, et->curr_mbs_wnd, Y_COMP, curr_part_x, curr_part_y, gcnt, et->ctu_width);
			orig_buff_stride_chroma = WND_STRIDE_2D(et->curr_mbs_wnd, CHR_COMP);
			orig_buff_u = WND_POSITION_2D(int16_t *, et->curr_mbs_wnd, U_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);
			orig_buff_v = WND_POSITION_2D(int16_t *, et->curr_mbs_wnd, V_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);

			residual_buff_stride = WND_STRIDE_2D(et->residual_wnd, Y_COMP);
			residual_buff = WND_POSITION_2D(int16_t *, et->residual_wnd, Y_COMP, curr_part_x, curr_part_y, gcnt, et->ctu_width);
			residual_buff_stride_chroma = WND_STRIDE_2D(et->residual_wnd, CHR_COMP);
			residual_buff_u = WND_POSITION_2D(int16_t *, et->residual_wnd, U_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);
			residual_buff_v = WND_POSITION_2D(int16_t *, et->residual_wnd, V_COMP, curr_part_x_chroma, curr_part_y_chroma, gcnt, et->ctu_width);

			reference_wnd = &currslice->ref_pic_list[ref_list][ref_idx]->img;//[0] up to now we only use one reference	
			reference_buff_stride = WND_STRIDE_2D(*reference_wnd, Y_COMP);
			reference_buff_cu_position = WND_POSITION_2D(int16_t *, *reference_wnd, Y_COMP, curr_part_global_x, curr_part_global_y, gcnt, et->ctu_width);
			reference_buff_stride_chroma = WND_STRIDE_2D(*reference_wnd, CHR_COMP);
			reference_buff_cu_position_u = WND_POSITION_2D(int16_t *, *reference_wnd, U_COMP, curr_part_global_x_chroma, curr_part_global_y_chroma, gcnt, et->ctu_width);
			reference_buff_cu_position_v = WND_POSITION_2D(int16_t *, *reference_wnd, V_COMP, curr_part_global_x_chroma, curr_part_global_y_chroma, gcnt, et->ctu_width);

			/* 根据某一个指定的顺序根据CU的邻居的MV计算AMVP */
			get_amvp_candidates(et, currslice, ctu, curr_cu_info, &et->amvp_candidates[ref_list], ref_list, ref_idx, part_size_type);//get candidates for motion search from the neigbour CUs
#ifdef COMPUTE_AS_HM
			mv_cost += select_mv_candidate(et, curr_cu_info, &et->amvp_candidates[ref_list], &mv, &curr_cu_info->best_candidate_idx[ref_list]);
#else
			mv_cost += select_mv_candidate(et, curr_cu_info, &et->amvp_candidates[ref_list], &mv, &curr_cu_info->best_candidate_idx[ref_list]);
#endif
			curr_cu_info->best_dif_mv[ref_list].hor_vector = mv.hor_vector - et->amvp_candidates[ref_list].mv_candidates[curr_cu_info->best_candidate_idx[ref_list]].mv.hor_vector;
			curr_cu_info->best_dif_mv[ref_list].ver_vector = mv.ver_vector - et->amvp_candidates[ref_list].mv_candidates[curr_cu_info->best_candidate_idx[ref_list]].mv.ver_vector;


			//##################################################### this should be moved somewhere in an upper herarchy ##################################################
			SET_INTER_MV_BUFFS(et, ctu, curr_cu_info, curr_cu_info->abs_index, curr_cu_info->num_part_in_cu);
			memset(&ctu->mv_ref_idx[REF_PIC_LIST_0][curr_cu_info->abs_index], curr_cu_info->inter_ref_index[REF_PIC_LIST_0], curr_cu_info->num_part_in_cu*sizeof(ctu->mv_ref_idx[0][0]));
			memset(&ctu->mv_ref_idx[REF_PIC_LIST_1][curr_cu_info->abs_index], curr_cu_info->inter_ref_index[REF_PIC_LIST_1], curr_cu_info->num_part_in_cu*sizeof(ctu->mv_ref_idx[0][0]));
			//##########################################################################################################################################################################
			/* 运动补偿*/
			hmr_motion_compensation_luma(et, curr_cu_info, reference_buff_cu_position, reference_buff_stride, pred_aux_buffs[ref_list][Y_COMP], pred_buff_stride, curr_part_size, curr_part_size, curr_part_size_shift, &mv, !uni_prediction);
			hmr_motion_compensation_chroma(et, reference_buff_cu_position_u, reference_buff_stride_chroma, pred_aux_buffs[ref_list][U_COMP], pred_buff_stride_chroma, curr_part_size_chroma, curr_part_size_shift_chroma, &mv, !uni_prediction);
			hmr_motion_compensation_chroma(et, reference_buff_cu_position_v, reference_buff_stride_chroma, pred_aux_buffs[ref_list][V_COMP], pred_buff_stride_chroma, curr_part_size_chroma, curr_part_size_shift_chroma, &mv, !uni_prediction);
		}

		/* 不是单向预测*/
		if (!uni_prediction)
		{
			/* 权重预测 */
			et->funcs->weighted_average_motion(pred_aux_buffs[REF_PIC_LIST_0][Y_COMP], pred_buff_stride, pred_aux_buffs[REF_PIC_LIST_1][Y_COMP], pred_buff_stride, pred_buff, pred_buff_stride, curr_cu_info->size, curr_cu_info->size, et->bit_depth);
			et->funcs->weighted_average_motion(pred_aux_buffs[REF_PIC_LIST_0][U_COMP], pred_buff_stride_chroma, pred_aux_buffs[REF_PIC_LIST_1][U_COMP], pred_buff_stride_chroma, pred_buff_u, pred_buff_stride_chroma, curr_cu_info->size_chroma, curr_cu_info->size_chroma, et->bit_depth);
			et->funcs->weighted_average_motion(pred_aux_buffs[REF_PIC_LIST_0][V_COMP], pred_buff_stride_chroma, pred_aux_buffs[REF_PIC_LIST_1][V_COMP], pred_buff_stride_chroma, pred_buff_v, pred_buff_stride_chroma, curr_cu_info->size_chroma, curr_cu_info->size_chroma, et->bit_depth);
		}

		/* 计算残差 */
		et->funcs->predict(orig_buff, orig_buff_stride, pred_buff, pred_buff_stride, residual_buff, residual_buff_stride, curr_part_size);
		et->funcs->predict(orig_buff_u, orig_buff_stride_chroma, pred_buff_u, pred_buff_stride_chroma, residual_buff_u, residual_buff_stride_chroma, curr_part_size_chroma);
		et->funcs->predict(orig_buff_v, orig_buff_stride_chroma, pred_buff_v, pred_buff_stride_chroma, residual_buff_v, residual_buff_stride_chroma, curr_part_size_chroma);

		curr_cu_info++;
	}

	return mv_cost;
}


计算块的残差:

void predict(int16_t * orig_auxptr, int orig_buff_stride, int16_t *pred_auxptr, int pred_buff_stride, int16_t * residual_auxptr, int residual_buff_stride, int curr_part_size)
{
	int i,j;
	for(j=0;j<curr_part_size;j++)
	{
		for(i=0;i<curr_part_size;i++)
		{
			residual_auxptr[i] = 	orig_auxptr[i]-pred_auxptr[i];
		}
		residual_auxptr += residual_buff_stride;
		orig_auxptr += orig_buff_stride;
		pred_auxptr += pred_buff_stride;
	}
}



上一篇:墙内+墙外,医疗健康行业催生出三个万亿级的大市场#创新中国2016#
下一篇:为什么做java的web开发我们会使用struts2,springMVC和spring这样的框架?

相关文章

相关评论

本站评论功能暂时取消,后续此功能例行通知。

一、不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益,不得利用本站制作、复制和传播不法有害信息!

二、互相尊重,对自己的言论和行为负责。

好贷网好贷款