1 // Copyright (C) 2022 Beken Corporation
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "jpeg_ll.h"
16 #include "jpeg_hal.h"
17 #include <driver/hal/hal_jpeg_types.h>
18 #include <driver/jpeg_enc_types.h>
19
20 #define JPEG_BITRATE_MAX_SIZE_320_240 (20 * 1024)
21 #define JPEG_BITRATE_MIN_SIZE_320_240 (5 * 1024)
22
23 #define JPEG_BITRATE_MAX_SIZE_640_480 (35 * 1024)//30
24 #define JPEG_BITRATE_MIN_SIZE_640_480 (20 * 1024)//10
25
26 #define JPEG_BITRATE_MAX_SIZE_1280_720 (50 * 1024)
27 #define JPEG_BITRATE_MIN_SIZE_1280_720 (30 * 1024)
28
29 #define JPEG_BITRATE_MAX_SIZE JPEG_BITRATE_MAX_SIZE_640_480
30 #define JPEG_BITRATE_MIN_SIZE JPEG_BITRATE_MIN_SIZE_640_480
31
32
33 __attribute__((section(".video_spec_data"))) uint8_t video_sram[40*1024];
34 #define JPEG_SHARE_MEM (&video_sram[0])
35
36
jpeg_hal_init(jpeg_hal_t * hal)37 bk_err_t jpeg_hal_init(jpeg_hal_t *hal)
38 {
39 hal->hw = (jpeg_hw_t *)JPEG_LL_REG_BASE(hal->id);
40 jpeg_ll_init(hal->hw);
41 return BK_OK;
42 }
43
jpeg_hal_set_target_bitrate(jpeg_hal_t * hal,uint32_t x_pixel)44 static void jpeg_hal_set_target_bitrate(jpeg_hal_t *hal, uint32_t x_pixel)
45 {
46 switch(x_pixel){
47 case X_PIXEL_320:
48 jpeg_ll_set_target_high_byte(hal->hw, JPEG_BITRATE_MAX_SIZE_320_240);
49 jpeg_ll_set_target_low_byte(hal->hw, JPEG_BITRATE_MIN_SIZE_320_240);
50 break;
51 case X_PIXEL_640:
52 jpeg_ll_set_target_high_byte(hal->hw, JPEG_BITRATE_MAX_SIZE_640_480);
53 jpeg_ll_set_target_low_byte(hal->hw, JPEG_BITRATE_MIN_SIZE_640_480);
54 break;
55 case X_PIXEL_1280:
56 case X_PIXEL_1600:
57 //jpeg_ll_enable_bitrate_ctrl(hal->hw, 1);
58 jpeg_ll_set_target_high_byte(hal->hw, JPEG_BITRATE_MAX_SIZE_1280_720);
59 jpeg_ll_set_target_low_byte(hal->hw, JPEG_BITRATE_MIN_SIZE_1280_720);
60 break;
61 default:
62 os_printf("Not adapt this image resolution\r\n");
63 jpeg_ll_set_target_high_byte(hal->hw, JPEG_BITRATE_MAX_SIZE);
64 jpeg_ll_set_target_low_byte(hal->hw, JPEG_BITRATE_MIN_SIZE);
65 break;
66 }
67 }
68
jpeg_hal_set_em_base_addr(jpeg_hal_t * hal,uint8_t * address)69 bk_err_t jpeg_hal_set_em_base_addr(jpeg_hal_t *hal, uint8_t *address)
70 {
71 jpeg_ll_set_em_base_addr(hal->hw, (uint32_t)address);
72 return BK_OK;
73 }
74
jpeg_hal_set_yuv_config(jpeg_hal_t * hal,const jpeg_config_t * config)75 bk_err_t jpeg_hal_set_yuv_config(jpeg_hal_t *hal, const jpeg_config_t *config)
76 {
77 jpeg_ll_init(hal->hw);
78 jpeg_ll_clear_config(hal->hw);
79
80 jpeg_ll_enable_end_yuv_int(hal->hw);
81 jpeg_ll_enable_vsync_negedge_int(hal->hw);
82 jpeg_ll_set_mclk_div(hal->hw, config->mclk_div);
83
84 jpeg_ll_enable_sync_edge_dect(hal->hw);
85
86 jpeg_ll_set_x_pixel(hal->hw, config->x_pixel);
87 jpeg_ll_set_y_pixel(hal->hw, config->y_pixel);
88 jpeg_ll_enable_yuv_word_reverse(hal->hw);
89 jpeg_ll_set_yuv_mode(hal->hw, 1);
90 jpeg_ll_set_em_base_addr(hal->hw, PSRAM_BASEADDR);//PSRAM_BASEADDR
91
92 return BK_OK;
93 }
94
jpeg_hal_set_encode_config(jpeg_hal_t * hal,const jpeg_config_t * config)95 bk_err_t jpeg_hal_set_encode_config(jpeg_hal_t *hal, const jpeg_config_t *config)
96 {
97 jpeg_ll_init(hal->hw);
98 jpeg_ll_clear_config(hal->hw);
99
100 jpeg_ll_enable_end_frame_int(hal->hw);
101 jpeg_ll_set_mclk_div(hal->hw, config->mclk_div);
102
103 jpeg_ll_enable_sync_edge_dect(hal->hw);
104
105 jpeg_ll_set_x_pixel(hal->hw, config->x_pixel);
106 jpeg_ll_set_y_pixel(hal->hw, config->y_pixel);
107 jpeg_ll_init_quant_table(hal->hw);
108 jpeg_hal_set_target_bitrate(hal, config->x_pixel);
109
110 jpeg_ll_set_default_bitrate_step(hal->hw);
111 jpeg_ll_enable_video_byte_reverse(hal->hw);
112 jpeg_ll_enable_enc_size(hal->hw);
113 jpeg_ll_set_em_base_addr(hal->hw, (uint32_t)JPEG_SHARE_MEM);
114 jpeg_ll_enable(hal->hw);
115
116 return BK_OK;
117 }
118
jpeg_hal_enable_clk(jpeg_hal_t * hal,uint32_t value)119 bk_err_t jpeg_hal_enable_clk(jpeg_hal_t *hal, uint32_t value)
120 {
121 jpeg_ll_set_mclk_div(hal->hw, value);
122 jpeg_ll_enable(hal->hw);
123 return BK_OK;
124 }
125
jpeg_hal_yuv_fmt_sel(jpeg_hal_t * hal,uint32_t value)126 bk_err_t jpeg_hal_yuv_fmt_sel(jpeg_hal_t *hal, uint32_t value)
127 {
128 jpeg_ll_yuv_fml_sel(hal->hw, value);
129 return BK_OK;
130 }
131
jpeg_hal_start_common(jpeg_hal_t * hal,uint8_t mode)132 bk_err_t jpeg_hal_start_common(jpeg_hal_t *hal, uint8_t mode)
133 {
134 if (mode)
135 jpeg_ll_set_yuv_mode(hal->hw, 1);
136 else
137 jpeg_ll_enable(hal->hw);
138 return BK_OK;
139 }
140
jpeg_hal_stop_common(jpeg_hal_t * hal,uint8_t mode)141 bk_err_t jpeg_hal_stop_common(jpeg_hal_t *hal, uint8_t mode)
142 {
143 if (mode)
144 jpeg_ll_set_yuv_mode(hal->hw, 0);
145 else
146 jpeg_ll_disable(hal->hw);
147 return BK_OK;
148 }
149
jpeg_hal_enable_partial_display(jpeg_hal_t * hal,uint32_t enable)150 bk_err_t jpeg_hal_enable_partial_display(jpeg_hal_t *hal, uint32_t enable)
151 {
152 if (enable) {
153 jpeg_ll_enable_partial_display(hal->hw);
154 } else {
155 jpeg_ll_disable_partial_display(hal->hw);
156 }
157
158 return BK_OK;
159 }
160
jpeg_hal_partial_display_offset_config(jpeg_hal_t * hal,const jpeg_partial_offset_config_t * offset_config)161 bk_err_t jpeg_hal_partial_display_offset_config(jpeg_hal_t *hal, const jpeg_partial_offset_config_t *offset_config)
162 {
163 jpeg_ll_set_x_partial_offset_l(hal->hw, offset_config->x_partial_offset_l);
164 jpeg_ll_set_x_partial_offset_r(hal->hw, offset_config->x_partial_offset_r);
165 jpeg_ll_set_y_partial_offset_l(hal->hw, offset_config->y_partial_offset_l);
166 jpeg_ll_set_y_partial_offset_r(hal->hw, offset_config->y_partial_offset_r);
167
168 return BK_OK;
169 }
170
jpeg_hal_enable_bitrate_ctrl(jpeg_hal_t * hal,uint8_t enable)171 bk_err_t jpeg_hal_enable_bitrate_ctrl(jpeg_hal_t *hal, uint8_t enable)
172 {
173 if (enable)
174 {
175 jpeg_ll_enable_bitrate_ctrl(hal->hw, 1);
176 }
177 else
178 {
179 jpeg_ll_enable_bitrate_ctrl(hal->hw, 0);
180 }
181
182 return BK_OK;
183 }
184
jpeg_hal_set_target_size(jpeg_hal_t * hal,uint32_t up_size,uint32_t low_size)185 bk_err_t jpeg_hal_set_target_size(jpeg_hal_t *hal, uint32_t up_size, uint32_t low_size)
186 {
187 jpeg_ll_set_target_high_byte(hal->hw, up_size);
188 jpeg_ll_set_target_low_byte(hal->hw, low_size);
189
190 return BK_OK;
191 }