@@ -40,25 +40,25 @@ struct video_sw_generator_data {
40
40
uint32_t frame_rate ;
41
41
};
42
42
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
+
43
54
static 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 ),
62
62
{0 },
63
63
};
64
64
@@ -117,51 +117,122 @@ static int video_sw_generator_set_stream(const struct device *dev, bool enable)
117
117
return 0 ;
118
118
}
119
119
120
- /* Black, Blue, Red, Purple, Green, Aqua, Yellow, White */
121
- uint16_t rgb565_colorbar_value [] = {
122
- 0x0000 , 0x001F , 0xF800 , 0xF81F , 0x07E0 , 0x07FF , 0xFFE0 , 0xFFFF ,
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 };
124
+
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 },
123
130
};
124
131
125
- uint32_t xrgb32_colorbar_value [ ] = {
126
- 0xFF000000 , 0xFF0000FF , 0xFFFF0000 , 0xFFFF00FF ,
127
- 0xFF00FF00 , 0xFF00FFFF , 0xFFFFFF00 , 0xFFFFFFFF ,
132
+ static const uint16_t pattern_8bars_rgb [ 8 ][ 3 ] = {
133
+ { 0xFF , 0xFF , 0xFF }, { 0xFF , 0xFF , 0x00 }, { 0x00 , 0xFF , 0xFF }, { 0x00 , 0xFF , 0x00 } ,
134
+ { 0xFF , 0x00 , 0xFF }, { 0xFF , 0x00 , 0x00 }, { 0x00 , 0x00 , 0xFF }, { 0x00 , 0x00 , 0x00 } ,
128
135
};
129
136
130
- static void video_sw_generator_fill_colorbar (struct video_sw_generator_data * data ,
131
- struct video_buffer * vbuf )
137
+ static int video_sw_generator_fill_yuyv (uint8_t * buffer , uint16_t pitch )
138
+ {
139
+ for (size_t i = 0 ; i + 4 <= pitch ; i += 4 ) {
140
+ buffer [i + 0 ] = pattern_8bars_yuv [8 * i / pitch ][0 ];
141
+ buffer [i + 1 ] = pattern_8bars_yuv [8 * i / pitch ][1 ];
142
+ buffer [i + 2 ] = pattern_8bars_yuv [8 * i / pitch ][0 ];
143
+ buffer [i + 3 ] = pattern_8bars_yuv [8 * i / pitch ][2 ];
144
+ }
145
+ return 1 ;
146
+ }
147
+
148
+ static int video_sw_generator_fill_xrgb32 (uint8_t * buffer , uint16_t pitch )
149
+ {
150
+ for (size_t i = 0 ; i < pitch ; i += 4 ) {
151
+ buffer [i + 0 ] = 0xff ;
152
+ buffer [i + 1 ] = pattern_8bars_rgb [8 * i / pitch ][0 ];
153
+ buffer [i + 2 ] = pattern_8bars_rgb [8 * i / pitch ][1 ];
154
+ buffer [i + 3 ] = pattern_8bars_rgb [8 * i / pitch ][2 ];
155
+ }
156
+ return 1 ;
157
+ }
158
+
159
+ static int video_sw_generator_fill_rgb565 (uint8_t * buffer , uint16_t pitch )
160
+ {
161
+ for (size_t i = 0 ; i < pitch ; i += 1 ) {
162
+ uint8_t r = pattern_8bars_rgb [16 * i / pitch ][0 ] >> (8 - 5 );
163
+ uint8_t g = pattern_8bars_rgb [16 * i / pitch ][1 ] >> (8 - 6 );
164
+ uint8_t b = pattern_8bars_rgb [16 * i / pitch ][2 ] >> (8 - 5 );
165
+
166
+ ((uint16_t * )buffer )[i ] = sys_cpu_to_le16 ((r << 11 ) | (g << 6 ) | (b << 0 ));
167
+ }
168
+ return 1 ;
169
+ }
170
+
171
+ static int video_sw_generator_fill_bayer (uint8_t * buffer , uint16_t pitch , const uint8_t * idx )
132
172
{
133
- int bw = data -> fmt .width / 8 ;
173
+ uint8_t * row0 = buffer + 0 ;
174
+ uint8_t * row1 = buffer + pitch ;
175
+
176
+ for (size_t i = 0 ; i + 2 <= pitch ; i += 2 ) {
177
+ row0 [i + 0 ] = pattern_8bars_rgb [8 * i / pitch ][idx [0 ]];
178
+ row0 [i + 1 ] = pattern_8bars_rgb [8 * i / pitch ][idx [1 ]];
179
+ row1 [i + 0 ] = pattern_8bars_rgb [8 * i / pitch ][idx [2 ]];
180
+ row1 [i + 1 ] = pattern_8bars_rgb [8 * i / pitch ][idx [3 ]];
181
+ }
182
+ return 2 ;
183
+ }
134
184
135
- vbuf -> bytesused = 0 ;
185
+ static void video_sw_generator_fill (const struct device * const dev , struct video_buffer * vbuf )
186
+ {
187
+ struct video_sw_generator_data * data = dev -> data ;
188
+ struct video_format * fmt = & data -> fmt ;
189
+ int lines = 1 ;
136
190
137
- if (vbuf -> size < data -> fmt .pitch ) {
138
- LOG_WRN ("Buffer %p has only %u bytes, shorter than pitch %u" ,
191
+ if (vbuf -> size < data -> fmt .pitch * 2 ) {
192
+ LOG_WRN ("Buffer %p has only %u bytes, shorter than twice the pitch %u" ,
139
193
vbuf , vbuf -> size , data -> fmt .pitch );
140
194
return ;
141
195
}
142
196
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 ;
197
+ /* Fill the first row of the emulated framebuffer */
198
+ switch (data -> fmt .pixelformat ) {
199
+ case VIDEO_PIX_FMT_YUYV :
200
+ lines = video_sw_generator_fill_yuyv (vbuf -> buffer , fmt -> pitch );
201
+ break ;
202
+ case VIDEO_PIX_FMT_XRGB32 :
203
+ lines = video_sw_generator_fill_xrgb32 (vbuf -> buffer , fmt -> pitch );
204
+ break ;
205
+ case VIDEO_PIX_FMT_RGB565 :
206
+ lines = video_sw_generator_fill_rgb565 (vbuf -> buffer , fmt -> pitch );
207
+ break ;
208
+ case VIDEO_PIX_FMT_RGGB8 :
209
+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_rggb8_idx );
210
+ break ;
211
+ case VIDEO_PIX_FMT_GBRG8 :
212
+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_gbrg8_idx );
213
+ break ;
214
+ case VIDEO_PIX_FMT_BGGR8 :
215
+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_bggr8_idx );
216
+ break ;
217
+ case VIDEO_PIX_FMT_GRBG8 :
218
+ lines = video_sw_generator_fill_bayer (vbuf -> buffer , fmt -> pitch , pattern_grbg8_idx );
219
+ break ;
220
+ default :
221
+ LOG_WRN ("Unsupported pixel format %x, filling with 0x55" , data -> fmt .pixelformat );
222
+ memset (vbuf -> buffer , 0x55 , data -> fmt .pitch );
223
+ break ;
155
224
}
156
225
226
+ /* How much was filled insofar */
227
+ vbuf -> bytesused = data -> fmt .pitch * lines ;
228
+
157
229
/* 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 ) {
230
+ for (int h = lines ; h < data -> fmt .height ; h += lines ) {
231
+ if (vbuf -> size < vbuf -> bytesused + data -> fmt .pitch * lines ) {
160
232
break ;
161
233
}
162
-
163
- memcpy (vbuf -> buffer + h * data -> fmt .pitch , vbuf -> buffer , data -> fmt .pitch );
164
- vbuf -> bytesused += data -> fmt .pitch ;
234
+ memcpy (vbuf -> buffer + h * data -> fmt .pitch , vbuf -> buffer , data -> fmt .pitch * lines );
235
+ vbuf -> bytesused += data -> fmt .pitch * lines ;
165
236
}
166
237
167
238
vbuf -> timestamp = k_uptime_get_32 ();
@@ -185,7 +256,7 @@ static void video_sw_generator_worker(struct k_work *work)
185
256
186
257
switch (data -> pattern ) {
187
258
case VIDEO_PATTERN_COLOR_BAR :
188
- video_sw_generator_fill_colorbar (data , vbuf );
259
+ video_sw_generator_fill (data -> dev , vbuf );
189
260
break ;
190
261
}
191
262
0 commit comments