#!/bin/bash
#
# create_lua.sh
#
# History:
#	2020/10/22 - [Qiangqiang Zhang] created file
#
# Copyright (c) 2020 Ambarella International LP
#
# This file and its contents ("Software") are protected by intellectual
# property rights including, without limitation, U.S. and/or foreign
# copyrights. This Software is also the confidential and proprietary
# information of Ambarella International LP and its licensors. You may not use, reproduce,
# disclose, distribute, modify, or otherwise prepare derivative works of this
# Software or any portion thereof except pursuant to a signed license agreement
# or nondisclosure agreement with Ambarella International LP or its authorized affiliates.
# In the absence of such an agreement, you agree to promptly notify and return
# this Software to Ambarella International LP.
#
# This file includes sample code and is only for internal testing and evaluation.  If you
# distribute this sample code (whether in source, object, or binary code form), it will be
# without any warranty or indemnity protection from Ambarella International LP or its affiliates.
#
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
# INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF NON-INFRINGEMENT,
# MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
# IN NO EVENT SHALL AMBARELLA INTERNATIONAL LP OR ITS AFFILIATES BE LIABLE FOR ANY DIRECT,
# INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; COMPUTER FAILURE OR MALFUNCTION; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#
#################################################################################################

#################################################################################################
# For shellcheck
# Introduce exclude types of warnings
# SC2140: Word is of the form "A"B"C" (B indicated). Did you mean "ABC" or "A\"B\"C"? (Ignore lua's comments)
# SC2154: <var> is referenced but not assigned. (Because variables may be defined in included files)
#
# shellcheck disable=SC2140,SC2154
#
# Execute the following command on the command line to check the shell script
# 	build $ shellcheck create_lua.sh
#################################################################################################

# Variables in the string need to be assigned before this
string_lua_vsrc_0="
vsrc_0 = {
	vsrc_id = 0,
	mode = \"${lua_vsrc_mode}\",
	hdr_mode = \"linear\", -- options: "linear", "2x" or "3x"
	fps = 30,
	bits= 0,
}
"

string_lua_chan_0_basic="
chan_0 = {
	id = 0,
	vsrc = vsrc_0,
	vsrc_ctx = 0,
	img_stats_src_chan = \"chan_0\",
	sensor_ctrl = 1,
	max_padding_width = 0,
	idsp_fps = 0,
	lens_warp = 0,
	max_main_input_width = 0, -- 0: VIN raw width
	blend_left = 0,
	blend_right = 0,
	mctf_cmpr = 1,
	mctf_10bit_ref = 0,
	main_burst_tile = 1,
	extra_downscale = 0,
"

string_cv5_lua_chan_0_basic="
chan_0 = {
	id = 0,
	vsrc = vsrc_0,
	vsrc_ctx = 0,
	img_stats_src_chan = \"chan_0\",
	sensor_ctrl = 1,
	max_padding_width = 0,
	idsp_fps = 0,
	lens_warp = 0,
	max_main_input_width = 0, -- 0: VIN raw width
	mctf_cmpr = 1,
	c2y_burst_tile = 1,
	extra_downscale = 0,
"

string_lua_chan_0_source_buffer="
	main = { -- As a Big buffer in the current script, for stream_A or pyramid
		max_output = {0, 0}, -- output width
		input      = {0, 0, 0, 0}, -- full VIN
		output     = {0, 0, ${lua_main_source_buf_w}, ${lua_main_source_buf_h}},
	},
	second = { -- As a Small buffer in the current script, for stream_A
		max_output = {0, 0}, -- output width
		input      = {0, 0, 0, 0}, -- full main
		output     = {0, 0, ${lua_second_source_buf_w}, ${lua_second_source_buf_h}},
	},
	third = { -- Only for vout in the current script, default is 1280x720
		max_output = {0, 0}, -- output width
		input      = {0, 0, 0, 0}, -- full main
		output     = {0, 0, ${lua_third_source_buf_w}, ${lua_third_source_buf_h}},
	},
	fourth = { -- As a Optional buffer in the current script, only for stream_B
		max_output = {0, 0},
		input      = {0, 0, 0, 0},
		output     = {0, 0, ${lua_fourth_source_buf_w}, ${lua_fourth_source_buf_h}},
	},
	fifth = { -- Only for pyramid in the current script
		max_output = {0, 0},
		input      = {0, 0, 0, 0}, -- full main
		output     = {0, 0, ${lua_fifth_source_buf_w}, ${lua_fifth_source_buf_h}},
	},
"

string_lua_chan_0_pyramid_buffer="
	pyramid = {
		input_buf_id = ${lua_pyramid_input_buf_id},	-- 0: Main, 1: Second, 2: Third, 3: Fourth, 4: Fifth
		scale_type = ${lua_pyramid_scale_type},		-- 0: 1/sqrt(2); 1: 1/2; 2: Arbitrary size
		buf_addr = 0x0,
		buf_size = 0x0,
		manual_feed = ${lua_pyramid_manual_feed},
		item_num = ${lua_pyramid_item_num},
		layer_map = ${lua_pyramid_layer_map},
		${lua_pyramid_layer_1_rescale_size}
		layers = {
			{
				crop_win = {0, 0, 0 ,0},
			},
			{
				crop_win = {0, 0, 0, 0},
			},
			{
				crop_win = {0, 0, 0 ,0},
			},
			{
				crop_win = {0, 0, 0 ,0},
			},
			{
				crop_win = {0, 0, 0 ,0},
			},
			{
				crop_win = {0, 0, 0 ,0},
			},
		},
	},
}
"

string_lua_stream_0_config="
stream_0 = {
	id = 0,
	max_size = {${lua_stream_0_max_w}, ${lua_stream_0_max_h}},
	max_M = 1,
	fast_seek_enable = 0,
	two_ref_enable = 0,
	max_svct_layers_minus_1 = 0,
	max_num_minus_1_ltrs = 0,
	codec_enable = 0, -- 0: H264/H265/MJPEG; 1: H265/MJPEG; 2: H264/MJPEG; 3: MJPEG
}
"

string_lua_stream_1_config="
stream_1 = {
	id = 1,
	max_size = {${lua_stream_1_max_w}, ${lua_stream_1_max_h}},
	max_M = 1,
	fast_seek_enable = 0,
	two_ref_enable = 0,
	max_svct_layers_minus_1 = 0,
	max_num_minus_1_ltrs = 0,
	codec_enable = 0, -- 0: H264/H265/MJPEG; 1: H265/MJPEG; 2: H264/MJPEG; 3: MJPEG
}
"

string_lua_resource_basic="
_resource_config_ = {
	version = 1,
	log_level = 0, -- 0: error; 1: warning; 2: info; 3: debug
	channels = {
		chan_0,
	},
	canvas = {"

string_lua_resource_canvas_buf_0="
		{
			type = \"encode\",
			size = {0, 0},
			source = {\"chan_0.main\",},
			extra_dram_buf = 0,
		},"

string_lua_resource_canvas_buf_1="
		{
			type = \"encode\",
			size = {0, 0},
			source = {\"chan_0.second\",},
			extra_dram_buf = 0,
		},"

string_lua_resource_canvas_buf_2="
		{
			type = \"prev\",
			size = {0, 0},
			source = {\"chan_0.third\",},
			${lua_canvas_buf_2_vout_id_param}
			extra_dram_buf = 0,
		},"

string_lua_resource_canvas_buf_3="
		{
			type = \"encode\",
			size = {0, 0},
			source = {\"chan_0.fourth\",},
			extra_dram_buf = 0,
		},"

string_lua_resource_stream_0="
	},
	streams = {
		stream_0,
	},
}
"

string_lua_resource_stream_0_1="
	},
	streams = {
		stream_0,
		stream_1,
	},
}
"

string_lua_resource_end="

return _resource_config_
"

### Only for vout_hdmi in cv5
string_lua_vout_hdmi_cfg="
vout_0 = {
    status = \"disable\",                                   --selection: 0:disable, 1:on, 2:off, 3:reset
                                                            --disable: do not use this vout controller
                                                            --on: use this vout controller and enable video display
                                                            --off: use this vout controller and disable video display
                                                            --reset: reset this vout controller
    mode = \"1200x1920p\",
    input_yuv422 = \"yes\",                                 ----selection: yes, no
    video_output_window = {0, 0, 1200, 1920},               --{offsetx, offsety, output_width, output_heigh}

    type = \"mipi_dsi\",                                    --selection: mipi_dsi, mipi_csi

    vout_from_image = \"disable\",
    default_img_format = \"yuv422\",                        --selection: yuv422, yuv420

    video_rotate = \"no\",                                  --selection: 0:no, 1:yes
    video_flip_mode = \"no\",                               --selection: 0:no, 1:hv, 2:h, 3:v

    own_mixer = \"yes\",                                    --selection: 0:no, 1:yes

    osd_output_window = {0, 0, 0, 0},                       --{offsetx, offsety, output_width, output_heigh}
    osd_flip_mode = \"no\",                                 --selection: 0:no, 1:hv, 2:h, 3:v
    osd_rescaler = \"disable\",                             --selection: 0:disable, 1:enable
    osd_rescaler_output_window = {0, 0, 0, 0},              --{offsetx, offsety, output_width, output_heigh}
}

vout_1 = {
    status = \"disable\",                                   --selection: 0:disable, 1:on, 2:off, 3:reset
                                                            --disable: do not use this vout controller
                                                            --on: use this vout controller and enable video display
                                                            --off: use this vout controller and disable video display
                                                            --reset: reset this vout controller
    mode = \"1200x1920p\",
    input_yuv422 = \"yes\",                                 ----selection: yes, no
    video_output_window = {0, 0, 1200, 1920},               --{offsetx, offsety, output_width, output_heigh}

    type = \"mipi_dsi\",                                    --selection: mipi_dsi, mipi_csi

    vout_from_image = \"disable\",
    default_img_format = \"yuv422\",                        --selection: yuv422, yuv420

    video_rotate = \"no\",                                  --selection: 0:no, 1:yes
    video_flip_mode = \"no\",                               --selection: 0:no, 1:hv, 2:h, 3:v

    own_mixer = \"yes\",                                    --selection: 0:no, 1:yes

    osd_output_window = {0, 0, 0, 0},                       --{offsetx, offsety, output_width, output_heigh}
    osd_flip_mode = \"no\",                                 --selection: 0:no, 1:hv, 2:h, 3:v
    osd_rescaler = \"disable\",                             --selection: 0:disable, 1:enable
    osd_rescaler_output_window = {0, 0, 0, 0},              --{offsetx, offsety, output_width, output_heigh}
}

vout_2 = {
    status = \"on\",                                        --selection: 0:disable, 1:on, 2:off, 3:reset
                                                            --disable: do not use this vout controller
                                                            --on: use this vout controller and enable video display
                                                            --off: use this vout controller and disable video display
                                                            --reset: reset this vout controller
    mode = \"${hdmi_res}\",
    input_yuv422 = \"yes\",
    video_output_window = {0, 0, ${hdmi_w}, ${hdmi_h}},     --{offsetx, offsety, output_width, output_heigh}

    type = \"hdmi\",                                        --selection: hdmi, cvbs,
                                                            --refer to voutc in ambarella/kernel/linux-5.10/arch/arm64/boot/dts/ambarella/ambarella-cv5.dtsi

    hdmi_output_format = \"rgb\",                           --selection: 0:rgb, 1:yuv422, 2:yuv444
                                                            --only for hdmi output mode
    vout_from_image = \"disable\",
    default_img_format = \"yuv422\",                        --selection: 0:yuv422, 1:yuv420

    video_rotate = \"no\",                                  --selection: 0:no, 1:yes
    video_flip_mode = \"no\",                               --selection: 0:no, 1:hv, 2:h, 3:v

    own_mixer = \"yes\",                                    --selection: 0:no, 1:yes

    osd_output_window = {0, 0, 0, 0},                       --{offsetx, offsety, output_width, output_heigh}
    osd_flip_mode = \"no\",                                 --selection: 0:no, 1:hv, 2:h, 3:v
    osd_rescaler = \"disable\",                             --selection: 0:disable, 1:enable
    osd_rescaler_output_window = {0, 0, 0, 0},              --{offsetx, offsety, output_width, output_heigh}
}


_vout_config_ = {
	version = 1,
	vouts = {
		vout_0,
		vout_1,
		vout_2,
	},
}

return _vout_config_

"


save_param_to_lua()
{
	echo -e -n "$1" >> "${RUN_IPC_LUA}"
}

save_param_to_vout_hdmi_lua()
{
	echo -e -n "$1" >> "${RUN_IPC_VOUT_HDMI_LUA}"
}

save_stream_config_params_to_lua()
{
	if [ "${stream_num}" = "0" ] || [ "${stream_num}" = "1" ]; then
		save_param_to_lua "${string_lua_stream_0_config}"
	elif [ "${stream_num}" = "2" ]; then
		save_param_to_lua "${string_lua_stream_0_config}"
		save_param_to_lua "${string_lua_stream_1_config}"
	else
		print_error "Unknown status for save_stream_config_params_to_lua !"
	fi
}

save_canvas_buffer_resource_params_to_lua()
{
	save_param_to_lua "${string_lua_resource_canvas_buf_0}"
	save_param_to_lua "${string_lua_resource_canvas_buf_1}"

	if [ "${customer_canvas_buffer_type}" = "main_second_third" ] || \
		[ "${customer_canvas_buffer_type}" = "main_second_third_fourth" ]; then
		save_param_to_lua "${string_lua_resource_canvas_buf_2}"
	fi

	if [ "${customer_canvas_buffer_type}" = "main_second_null_fourth" ] || \
		[ "${customer_canvas_buffer_type}" = "main_second_third_fourth" ]; then
		save_param_to_lua "${string_lua_resource_canvas_buf_3}"
	fi
}

save_stream_resource_to_lua()
{
	if [ "${stream_num}" = "0" ] || [ "${stream_num}" = "1" ]; then
		save_param_to_lua "${string_lua_resource_stream_0}"
	elif [ "${stream_num}" = "2" ]; then
		save_param_to_lua "${string_lua_resource_stream_0_1}"
	else
		print_error "Unknow stream_num:${stream_num} for save_stream_resource_to_lua"
	fi
}

main()
{
	# Only for vout_hdmi in cv5
	if [ "${need_vout_hdmi_lua_flag}" = "1" ]; then
		rm -rf "${RUN_IPC_VOUT_HDMI_LUA}"
		echo '-- This file is automatically generated by script' > "${RUN_IPC_VOUT_HDMI_LUA}"

		save_param_to_vout_hdmi_lua "${string_lua_vout_hdmi_cfg}"
	fi

	# For create lua
	rm -rf "${RUN_IPC_LUA}"
	echo '-- This file is automatically generated by script' > "${RUN_IPC_LUA}"

	save_param_to_lua "${string_lua_vsrc_0}"

	if [ "${system_board_bsp_name}" = "cv5_timn" ]; then
		save_param_to_lua "${string_cv5_lua_chan_0_basic}"
	else
		save_param_to_lua "${string_lua_chan_0_basic}"
	fi
	save_param_to_lua "${string_lua_chan_0_source_buffer}"
	save_param_to_lua "${string_lua_chan_0_pyramid_buffer}"

	save_stream_config_params_to_lua

	save_param_to_lua "${string_lua_resource_basic}"
	save_canvas_buffer_resource_params_to_lua
	save_stream_resource_to_lua

	save_param_to_lua "${string_lua_resource_end}"
}

main



