static enum ia_css_err frame_allocate_with_data(struct ia_css_frame **frame,
	unsigned int width,
	unsigned int height,
	enum ia_css_frame_format format,
	unsigned int padded_width,
	unsigned int raw_bit_depth,
	bool contiguous)
{
	enum ia_css_err err;
	struct ia_css_frame *me = frame_create(width,
		height,
		format,
		padded_width,
		raw_bit_depth,
		contiguous,
		true);

	if (me == NULL)
		return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;

	err = ia_css_frame_init_planes(me);

	if (err == IA_CSS_SUCCESS)
		err = frame_allocate_buffer_data(me);

	if (err != IA_CSS_SUCCESS) {
		sh_css_free(me);
		return err;
	}

	*frame = me;

	return err;
}
enum ia_css_err ia_css_frame_create_from_info(struct ia_css_frame **frame,
	const struct ia_css_frame_info *info)
{
	enum ia_css_err err = IA_CSS_SUCCESS;
	struct ia_css_frame *me;
	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
		"ia_css_frame_create_from_info() enter:\n");
	if (frame == NULL || info == NULL) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
			"ia_css_frame_create_from_info() leave:"
			" invalid arguments\n");
		return IA_CSS_ERR_INVALID_ARGUMENTS;
	}

	me = frame_create(info->res.width,
		info->res.height,
		info->format,
		info->padded_width,
		info->raw_bit_depth,
		false,
		false);
	if (me == NULL) {
		ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE,
			"ia_css_frame_create_from_info() leave:"
			" frame create failed\n");
		return IA_CSS_ERR_CANNOT_ALLOCATE_MEMORY;
	}

	err = ia_css_frame_init_planes(me);

	if (err == IA_CSS_SUCCESS)
		*frame = me;
	else
		sh_css_free(me);

	ia_css_debug_dtrace(IA_CSS_DEBUG_TRACE, "ia_css_frame_create_from_info() leave:\n");

	return err;
}
static void
copy_buffer_to_spframe(
	struct sh_css_sp_stage *sp_stage,
	struct ia_css_frame_sp *sp_frame_out,
	const struct ia_css_frame_info *frame_info_in,
	const hrt_vaddress data,
	enum ia_css_buffer_type type)
{
	struct ia_css_frame frame_local;
	assert(sp_stage != NULL && sp_frame_out != NULL && data != mmgr_NULL);

	sp_frame_out->buf_attr.buf_src.xmem_addr = data;
	sp_frame_out->buf_attr.buf_type = type;

	/* frame_info(css)->sp_frame_info(css) */
	ia_css_frame_info_to_frame_sp_info(&sp_frame_out->info, frame_info_in);

	/* only to get plane offsets */
	frame_local.info = *frame_info_in;
	ia_css_frame_init_planes(&frame_local);

	switch (frame_local.info.format) {
	case IA_CSS_FRAME_FORMAT_RAW_PACKED:
	case IA_CSS_FRAME_FORMAT_RAW:
		sp_frame_out->planes.raw.offset = frame_local.planes.raw.offset;
		break;
	case IA_CSS_FRAME_FORMAT_RGB565:
	case IA_CSS_FRAME_FORMAT_RGBA888:
		sp_frame_out->planes.rgb.offset = frame_local.planes.rgb.offset;
		break;
	case IA_CSS_FRAME_FORMAT_PLANAR_RGB888:
		sp_frame_out->planes.planar_rgb.r.offset =
			frame_local.planes.planar_rgb.r.offset;
		sp_frame_out->planes.planar_rgb.g.offset =
			frame_local.planes.planar_rgb.g.offset;
		sp_frame_out->planes.planar_rgb.b.offset =
			frame_local.planes.planar_rgb.b.offset;
		break;
	case IA_CSS_FRAME_FORMAT_YUYV:
	case IA_CSS_FRAME_FORMAT_UYVY:
	case IA_CSS_FRAME_FORMAT_CSI_MIPI_YUV420_8:
	case IA_CSS_FRAME_FORMAT_YUV_LINE:
		sp_frame_out->planes.yuyv.offset = frame_local.planes.yuyv.offset;
		break;
	case IA_CSS_FRAME_FORMAT_NV11:
	case IA_CSS_FRAME_FORMAT_NV12:
	case IA_CSS_FRAME_FORMAT_NV21:
	case IA_CSS_FRAME_FORMAT_NV16:
	case IA_CSS_FRAME_FORMAT_NV61:
		sp_frame_out->planes.nv.y.offset =
			frame_local.planes.nv.y.offset;
		sp_frame_out->planes.nv.uv.offset =
			frame_local.planes.nv.uv.offset;
		break;
	case IA_CSS_FRAME_FORMAT_YUV420:
	case IA_CSS_FRAME_FORMAT_YUV422:
	case IA_CSS_FRAME_FORMAT_YUV444:
	case IA_CSS_FRAME_FORMAT_YUV420_16:
	case IA_CSS_FRAME_FORMAT_YUV422_16:
	case IA_CSS_FRAME_FORMAT_YV12:
	case IA_CSS_FRAME_FORMAT_YV16:
		sp_frame_out->planes.yuv.y.offset =
			frame_local.planes.yuv.y.offset;
		sp_frame_out->planes.yuv.u.offset =
			frame_local.planes.yuv.u.offset;
		sp_frame_out->planes.yuv.v.offset =
			frame_local.planes.yuv.v.offset;
		break;
	case IA_CSS_FRAME_FORMAT_QPLANE6:
		sp_frame_out->planes.plane6.r.offset =
			frame_local.planes.plane6.r.offset;
		sp_frame_out->planes.plane6.r_at_b.offset =
			frame_local.planes.plane6.r_at_b.offset;
		sp_frame_out->planes.plane6.gr.offset =
			frame_local.planes.plane6.gr.offset;
		sp_frame_out->planes.plane6.gb.offset =
			frame_local.planes.plane6.gb.offset;
		sp_frame_out->planes.plane6.b.offset =
			frame_local.planes.plane6.b.offset;
		sp_frame_out->planes.plane6.b_at_r.offset =
			frame_local.planes.plane6.b_at_r.offset;
		break;
	case IA_CSS_FRAME_FORMAT_BINARY_8:
		sp_frame_out->planes.binary.data.offset =
			frame_local.planes.binary.data.offset;
		break;
	default:
		/* This should not happen, but in case it does,
		 * nullify the planes
		 */
		memset(&sp_frame_out->planes, 0, sizeof(sp_frame_out->planes));
		break;
	}
}