@@ -40,25 +40,25 @@ struct video_sw_generator_data {
4040 uint32_t frame_rate ;
4141};
4242
43+ #define VIDEO_SW_GENERATOR_FORMAT_CAP (pixfmt ) \
44+ { \
45+ .pixelformat = pixfmt, \
46+ .width_min = 64, \
47+ .width_max = 1920, \
48+ .height_min = 64, \
49+ .height_max = 1080, \
50+ .width_step = 1, \
51+ .height_step = 1, \
52+ }
53+
4354static const struct video_format_cap fmts [] = {
44- {
45- .pixelformat = VIDEO_PIX_FMT_RGB565 ,
46- .width_min = 64 ,
47- .width_max = 1920 ,
48- .height_min = 64 ,
49- .height_max = 1080 ,
50- .width_step = 1 ,
51- .height_step = 1 ,
52- },
53- {
54- .pixelformat = VIDEO_PIX_FMT_XRGB32 ,
55- .width_min = 64 ,
56- .width_max = 1920 ,
57- .height_min = 64 ,
58- .height_max = 1080 ,
59- .width_step = 1 ,
60- .height_step = 1 ,
61- },
55+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_YUYV ),
56+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_RGB565 ),
57+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_XRGB32 ),
58+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_RGGB8 ),
59+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_GRBG8 ),
60+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_BGGR8 ),
61+ VIDEO_SW_GENERATOR_FORMAT_CAP (VIDEO_PIX_FMT_GBRG8 ),
6262 {0 },
6363};
6464
@@ -117,51 +117,118 @@ static int video_sw_generator_set_stream(const struct device *dev, bool enable)
117117 return 0 ;
118118}
119119
120- /* Black, Blue, Red, Purple, Green, Aqua, Yellow, White */
121- uint16_t rgb565_colorbar_value [] = {
122- 0x0000 , 0x001F , 0xF800 , 0xF81F , 0x07E0 , 0x07FF , 0xFFE0 , 0xFFFF ,
123- };
120+ static const uint8_t pattern_rggb8_idx [] = { 0 , 1 , 1 , 2 };
121+ static const uint8_t pattern_bggr8_idx [] = {2 , 1 , 1 , 0 };
122+ static const uint8_t pattern_gbrg8_idx [] = { 1 , 2 , 0 , 1 };
123+ static const uint8_t pattern_grbg8_idx [] = { 1 , 0 , 2 , 1 };
124124
125- uint32_t xrgb32_colorbar_value [] = {
126- 0xFF000000 , 0xFF0000FF , 0xFFFF0000 , 0xFFFF00FF ,
127- 0xFF00FF00 , 0xFF00FFFF , 0xFFFFFF00 , 0xFFFFFFFF ,
128- };
125+ /* White, Yellow, Cyan, Green, Magenta, Red, Blue, Black */
126+
127+ static const uint16_t pattern_8bars_yuv [8 ][3 ] = {
128+ {0xFF , 0x7F , 0x7F }, {0xFF , 0x00 , 0xFF }, {0xFF , 0xFF , 0x00 }, {0x7F , 0x00 , 0x00 },
129+ {0x00 , 0xFF , 0xFF }, {0x00 , 0x00 , 0xFF }, {0x00 , 0xFF , 0x00 }, {0x00 , 0x7F , 0x7F }};
130+
131+ static const uint16_t pattern_8bars_rgb [8 ][3 ] = {
132+ {0xFF , 0xFF , 0xFF }, {0xFF , 0xFF , 0x00 }, {0x00 , 0xFF , 0xFF }, {0x00 , 0xFF , 0x00 },
133+ {0xFF , 0x00 , 0xFF }, {0xFF , 0x00 , 0x00 }, {0x00 , 0x00 , 0xFF }, {0x00 , 0x00 , 0x00 }};
134+
135+ static int video_sw_generator_fill_yuyv (uint8_t * buffer , uint16_t pitch )
136+ {
137+ for (size_t i = 0 ; i < pitch ; i += 2 ) {
138+ buffer [i + 0 ] = pattern_8bars_yuv [8 * i / pitch ][0 ];
139+ buffer [i + 1 ] = pattern_8bars_yuv [8 * i / pitch ][1 + i % 2 ];
140+ }
141+ return 1 ;
142+ }
143+
144+ static int video_sw_generator_fill_xrgb32 (uint8_t * buffer , uint16_t pitch )
145+ {
146+ for (size_t i = 0 ; i < pitch ; i += 4 ) {
147+ buffer [i + 0 ] = 0xff ;
148+ buffer [i + 1 ] = pattern_8bars_rgb [8 * i / pitch ][0 ];
149+ buffer [i + 2 ] = pattern_8bars_rgb [8 * i / pitch ][1 ];
150+ buffer [i + 3 ] = pattern_8bars_rgb [8 * i / pitch ][2 ];
151+ }
152+ return 1 ;
153+ }
154+
155+ static int video_sw_generator_fill_rgb565 (uint8_t * buffer , uint16_t pitch )
156+ {
157+ for (size_t i = 0 ; i < pitch ; i += 1 ) {
158+ uint8_t r = pattern_8bars_rgb [16 * i / pitch ][0 ] >> (8 - 5 );
159+ uint8_t g = pattern_8bars_rgb [16 * i / pitch ][1 ] >> (8 - 6 );
160+ uint8_t b = pattern_8bars_rgb [16 * i / pitch ][2 ] >> (8 - 5 );
161+
162+ ((uint16_t * )buffer )[i ] = sys_cpu_to_le16 ((r << 11 ) | (g << 6 ) | (b << 0 ));
163+ }
164+ return 1 ;
165+ }
129166
130- static void video_sw_generator_fill_colorbar (struct video_sw_generator_data * data ,
131- struct video_buffer * vbuf )
167+ static int video_sw_generator_fill_bayer (uint8_t * buffer , uint16_t pitch , const uint8_t * idx )
132168{
133- int bw = data -> fmt .width / 8 ;
169+ uint8_t * row0 = buffer + 0 ;
170+ uint8_t * row1 = buffer + pitch ;
171+
172+ for (size_t i = 0 ; i + 2 <= pitch ; i += 2 ) {
173+ row0 [i + 0 ] = pattern_8bars_rgb [8 * i / pitch ][idx [0 ]];
174+ row0 [i + 1 ] = pattern_8bars_rgb [8 * i / pitch ][idx [1 ]];
175+ row1 [i + 0 ] = pattern_8bars_rgb [8 * i / pitch ][idx [2 ]];
176+ row1 [i + 1 ] = pattern_8bars_rgb [8 * i / pitch ][idx [3 ]];
177+ }
178+ return 2 ;
179+ }
134180
135- vbuf -> bytesused = 0 ;
181+ static void video_sw_generator_fill (const struct device * const dev , struct video_buffer * vbuf )
182+ {
183+ struct video_sw_generator_data * data = dev -> data ;
184+ struct video_format * fmt = & data -> fmt ;
185+ int lines = 1 ;
136186
137- if (vbuf -> size < data -> fmt .pitch ) {
138- LOG_WRN ("Buffer %p has only %u bytes, shorter than pitch %u" ,
187+ if (vbuf -> size < data -> fmt .pitch * 2 ) {
188+ LOG_WRN ("Buffer %p has only %u bytes, shorter than twice the pitch %u" ,
139189 vbuf , vbuf -> size , data -> fmt .pitch );
140190 return ;
141191 }
142192
143- /* Generate the first line of data */
144- for (int w = 0 , i = 0 ; w < data -> fmt .width ; w ++ ) {
145- int color_idx = data -> ctrl_vflip ? 7 - w / bw : w / bw ;
146-
147- if (data -> fmt .pixelformat == VIDEO_PIX_FMT_RGB565 ) {
148- uint16_t * pixel = (uint16_t * )& vbuf -> buffer [i ];
149- * pixel = sys_cpu_to_le16 (rgb565_colorbar_value [color_idx ]);
150- } else if (data -> fmt .pixelformat == VIDEO_PIX_FMT_XRGB32 ) {
151- uint32_t * pixel = (uint32_t * )& vbuf -> buffer [i ];
152- * pixel = sys_cpu_to_le32 (xrgb32_colorbar_value [color_idx ]);
153- }
154- i += video_bits_per_pixel (data -> fmt .pixelformat ) / BITS_PER_BYTE ;
193+ /* Fill the first row of the emulated framebuffer */
194+ switch (data -> fmt .pixelformat ) {
195+ case VIDEO_PIX_FMT_YUYV :
196+ lines = video_sw_generator_fill_yuyv (vbuf -> buffer , fmt -> pitch );
197+ break ;
198+ case VIDEO_PIX_FMT_XRGB32 :
199+ lines = video_sw_generator_fill_xrgb32 (vbuf -> buffer , fmt -> pitch );
200+ break ;
201+ case VIDEO_PIX_FMT_RGB565 :
202+ lines = video_sw_generator_fill_rgb565 (vbuf -> buffer , fmt -> pitch );
203+ break ;
204+ case VIDEO_PIX_FMT_RGGB8 :
205+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_rggb8_idx );
206+ break ;
207+ case VIDEO_PIX_FMT_GBRG8 :
208+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_gbrg8_idx );
209+ break ;
210+ case VIDEO_PIX_FMT_BGGR8 :
211+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_bggr8_idx );
212+ break ;
213+ case VIDEO_PIX_FMT_GRBG8 :
214+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_grbg8_idx );
215+ break ;
216+ default :
217+ LOG_WRN ("Unsupported pixel format %x, filling with 0x55" , data -> fmt .pixelformat );
218+ memset (vbuf -> buffer , 0x55 , data -> fmt .pitch );
219+ break ;
155220 }
156221
222+ /* How much was filled insofar */
223+ vbuf -> bytesused = data -> fmt .pitch * lines ;
224+
157225 /* Duplicate the first line all over the buffer */
158- for (int h = 1 ; h < data -> fmt .height ; h ++ ) {
159- if (vbuf -> size < vbuf -> bytesused + data -> fmt .pitch ) {
226+ for (int h = lines ; h < data -> fmt .height ; h += lines ) {
227+ if (vbuf -> size < vbuf -> bytesused + data -> fmt .pitch * lines ) {
160228 break ;
161229 }
162-
163- memcpy (vbuf -> buffer + h * data -> fmt .pitch , vbuf -> buffer , data -> fmt .pitch );
164- vbuf -> bytesused += data -> fmt .pitch ;
230+ memcpy (vbuf -> buffer + h * data -> fmt .pitch , vbuf -> buffer , data -> fmt .pitch * lines );
231+ vbuf -> bytesused += data -> fmt .pitch * lines ;
165232 }
166233
167234 vbuf -> timestamp = k_uptime_get_32 ();
@@ -185,7 +252,7 @@ static void video_sw_generator_worker(struct k_work *work)
185252
186253 switch (data -> pattern ) {
187254 case VIDEO_PATTERN_COLOR_BAR :
188- video_sw_generator_fill_colorbar (data , vbuf );
255+ video_sw_generator_fill (data -> dev , vbuf );
189256 break ;
190257 }
191258
0 commit comments