1 /*
2 #             (C) 2008 Hans de Goede <hdegoede@redhat.com>
3 
4 # This program is free software; you can redistribute it and/or modify
5 # it under the terms of the GNU Lesser General Public License as published by
6 # the Free Software Foundation; either version 2.1 of the License, or
7 # (at your option) any later version.
8 #
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 # GNU Lesser General Public License for more details.
13 #
14 # You should have received a copy of the GNU Lesser General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA  02110-1335  USA
17  */
18 
19 #ifndef __LIBV4LCONVERT_PRIV_H
20 #define __LIBV4LCONVERT_PRIV_H
21 
22 #ifdef ANDROID
23 #include <android-config.h>
24 #else
25 #include <config.h>
26 #endif
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <sys/types.h>
30 #ifdef HAVE_JPEG
31 #include <jpeglib.h>
32 #endif
33 #include <setjmp.h>
34 #include "libv4l-plugin.h"
35 #include "libv4lconvert.h"
36 #include "control/libv4lcontrol.h"
37 #include "processing/libv4lprocessing.h"
38 #include "tinyjpeg.h"
39 
40 #define ARRAY_SIZE(x) ((int)sizeof(x)/(int)sizeof((x)[0]))
41 #define BITS_PER_LONG (8 * sizeof(long))
42 
43 #define V4LCONVERT_ERROR_MSG_SIZE 256
44 #define V4LCONVERT_MAX_FRAMESIZES 256
45 
46 #define V4LCONVERT_ERR(...) \
47 	snprintf(data->error_msg, V4LCONVERT_ERROR_MSG_SIZE, \
48 			"v4l-convert: error " __VA_ARGS__)
49 
50 /* Card flags */
51 #define V4LCONVERT_IS_UVC                0x01
52 #define V4LCONVERT_USE_TINYJPEG          0x02
53 
54 struct v4lconvert_data {
55 	int fd;
56 	int flags; /* bitfield */
57 	int control_flags; /* bitfield */
58 	unsigned int no_formats;
59 	unsigned long supported_src_formats[128 / BITS_PER_LONG];
60 	char error_msg[V4LCONVERT_ERROR_MSG_SIZE];
61 	struct jdec_private *tinyjpeg;
62 #ifdef HAVE_JPEG
63 	struct jpeg_error_mgr jerr;
64 	int jerr_errno;
65 	jmp_buf jerr_jmp_state;
66 	struct jpeg_decompress_struct cinfo;
67 	int cinfo_initialized;
68 #endif // HAVE_JPEG
69 	struct v4l2_frmsizeenum framesizes[V4LCONVERT_MAX_FRAMESIZES];
70 	/* Bitmask of all supported src_formats which can do for a size */
71 	int64_t framesize_supported_src_formats[V4LCONVERT_MAX_FRAMESIZES];
72 	unsigned int no_framesizes;
73 	int bandwidth;
74 	int fps;
75 	int convert1_buf_size;
76 	int convert2_buf_size;
77 	int rotate90_buf_size;
78 	int flip_buf_size;
79 	int convert_pixfmt_buf_size;
80 	unsigned char *convert1_buf;
81 	unsigned char *convert2_buf;
82 	unsigned char *rotate90_buf;
83 	unsigned char *flip_buf;
84 	unsigned char *convert_pixfmt_buf;
85 	struct v4lcontrol_data *control;
86 	struct v4lprocessing_data *processing;
87 	void *dev_ops_priv;
88 	const struct libv4l_dev_ops *dev_ops;
89 
90 	/* Data for external decompression helpers code */
91 	pid_t decompress_pid;
92 	int decompress_in_pipe[2];  /* Data from helper to us */
93 	int decompress_out_pipe[2]; /* Data from us to helper */
94 
95 	/* For mr97310a decoder */
96 	int frames_dropped;
97 
98 	/* For cpia1 decoder */
99 	unsigned char *previous_frame;
100 };
101 
102 struct v4lconvert_pixfmt {
103 	unsigned int fmt;	/* v4l2 fourcc */
104 	int bpp;		/* bits per pixel, 0 for compressed formats */
105 	int rgb_rank;		/* rank for converting to rgb32 / bgr32 */
106 	int yuv_rank;		/* rank for converting to yuv420 / yvu420 */
107 	int needs_conversion;
108 };
109 
110 void v4lconvert_fixup_fmt(struct v4l2_format *fmt);
111 
112 unsigned char *v4lconvert_alloc_buffer(int needed,
113 		unsigned char **buf, int *buf_size);
114 
115 int v4lconvert_oom_error(struct v4lconvert_data *data);
116 
117 void v4lconvert_rgb24_to_yuv420(const unsigned char *src, unsigned char *dest,
118 		const struct v4l2_format *src_fmt, int bgr, int yvu, int bpp);
119 
120 void v4lconvert_yuv420_to_rgb24(const unsigned char *src, unsigned char *dst,
121 		int width, int height, int yvu);
122 
123 void v4lconvert_yuv420_to_bgr24(const unsigned char *src, unsigned char *dst,
124 		int width, int height, int yvu);
125 
126 void v4lconvert_yuyv_to_rgb24(const unsigned char *src, unsigned char *dst,
127 		int width, int height, int stride);
128 
129 void v4lconvert_yuyv_to_bgr24(const unsigned char *src, unsigned char *dst,
130 		int width, int height, int stride);
131 
132 void v4lconvert_yuyv_to_yuv420(const unsigned char *src, unsigned char *dst,
133 		int width, int height, int stride, int yvu);
134 
135 void v4lconvert_nv16_to_yuyv(const unsigned char *src, unsigned char *dest,
136 		int width, int height);
137 
138 void v4lconvert_yvyu_to_rgb24(const unsigned char *src, unsigned char *dst,
139 		int width, int height, int stride);
140 
141 void v4lconvert_yvyu_to_bgr24(const unsigned char *src, unsigned char *dst,
142 		int width, int height, int stride);
143 
144 void v4lconvert_uyvy_to_rgb24(const unsigned char *src, unsigned char *dst,
145 		int width, int height, int stride);
146 
147 void v4lconvert_uyvy_to_bgr24(const unsigned char *src, unsigned char *dst,
148 		int width, int height, int stride);
149 
150 void v4lconvert_uyvy_to_yuv420(const unsigned char *src, unsigned char *dst,
151 		int width, int height, int stride, int yvu);
152 
153 void v4lconvert_swap_rgb(const unsigned char *src, unsigned char *dst,
154 		int width, int height);
155 
156 void v4lconvert_swap_uv(const unsigned char *src, unsigned char *dst,
157 		const struct v4l2_format *src_fmt);
158 
159 void v4lconvert_grey_to_rgb24(const unsigned char *src, unsigned char *dest,
160 		int width, int height);
161 
162 void v4lconvert_grey_to_yuv420(const unsigned char *src, unsigned char *dest,
163 		const struct v4l2_format *src_fmt);
164 
165 void v4lconvert_y16_to_rgb24(const unsigned char *src, unsigned char *dest,
166 		int width, int height, int little_endian);
167 
168 void v4lconvert_y16_to_yuv420(const unsigned char *src, unsigned char *dest,
169 		const struct v4l2_format *src_fmt, int little_endian);
170 
171 void v4lconvert_rgb32_to_rgb24(const unsigned char *src, unsigned char *dest,
172 		int width, int height, int bgr);
173 
174 int v4lconvert_y10b_to_rgb24(struct v4lconvert_data *data,
175 	const unsigned char *src, unsigned char *dest, int width, int height);
176 
177 int v4lconvert_y10b_to_yuv420(struct v4lconvert_data *data,
178 	const unsigned char *src, unsigned char *dest, int width, int height);
179 
180 void v4lconvert_rgb565_to_rgb24(const unsigned char *src, unsigned char *dest,
181 		int width, int height);
182 
183 void v4lconvert_rgb565_to_bgr24(const unsigned char *src, unsigned char *dest,
184 		int width, int height);
185 
186 void v4lconvert_rgb565_to_yuv420(const unsigned char *src, unsigned char *dest,
187 		const struct v4l2_format *src_fmt, int yvu);
188 
189 void v4lconvert_spca501_to_yuv420(const unsigned char *src, unsigned char *dst,
190 		int width, int height, int yvu);
191 
192 void v4lconvert_spca505_to_yuv420(const unsigned char *src, unsigned char *dst,
193 		int width, int height, int yvu);
194 
195 void v4lconvert_spca508_to_yuv420(const unsigned char *src, unsigned char *dst,
196 		int width, int height, int yvu);
197 
198 void v4lconvert_cit_yyvyuy_to_yuv420(const unsigned char *src,
199 		unsigned char *ydest,
200 		int width, int height, int yvu);
201 
202 void v4lconvert_konica_yuv420_to_yuv420(const unsigned char *src,
203 		unsigned char *ydest,
204 		int width, int height, int yvu);
205 
206 void v4lconvert_m420_to_yuv420(const unsigned char *src,
207 		unsigned char *ydest,
208 		int width, int height, int yvu);
209 
210 int v4lconvert_cpia1_to_yuv420(struct v4lconvert_data *data,
211 		const unsigned char *src, int src_size,
212 		unsigned char *dst, int width, int height, int yvu);
213 
214 void v4lconvert_sn9c20x_to_yuv420(const unsigned char *src, unsigned char *dst,
215 		int width, int height, int yvu);
216 
217 int v4lconvert_se401_to_rgb24(struct v4lconvert_data *data,
218 		const unsigned char *src, int src_size,
219 		unsigned char *dest, int width, int height);
220 
221 int v4lconvert_decode_jpeg_tinyjpeg(struct v4lconvert_data *data,
222 	unsigned char *src, int src_size, unsigned char *dest,
223 	struct v4l2_format *fmt, unsigned int dest_pix_fmt, int flags);
224 
225 int v4lconvert_decode_jpeg_libjpeg(struct v4lconvert_data *data,
226 	unsigned char *src, int src_size, unsigned char *dest,
227 	struct v4l2_format *fmt, unsigned int dest_pix_fmt);
228 
229 int v4lconvert_decode_jpgl(const unsigned char *src, int src_size,
230 	unsigned int dest_pix_fmt, unsigned char *dest, int width, int height);
231 
232 void v4lconvert_decode_spca561(const unsigned char *src, unsigned char *dst,
233 		int width, int height);
234 
235 void v4lconvert_decode_sn9c10x(const unsigned char *src, unsigned char *dst,
236 		int width, int height);
237 
238 int v4lconvert_decode_pac207(struct v4lconvert_data *data,
239 		const unsigned char *inp, int src_size, unsigned char *outp,
240 		int width, int height);
241 
242 int v4lconvert_decode_mr97310a(struct v4lconvert_data *data,
243 		const unsigned char *src, int src_size, unsigned char *dst,
244 		int width, int height);
245 
246 int v4lconvert_decode_jl2005bcd(struct v4lconvert_data *data,
247 		const unsigned char *src, int src_size,
248 		unsigned char *dest, int width, int height);
249 
250 void v4lconvert_decode_sn9c2028(const unsigned char *src, unsigned char *dst,
251 		int width, int height);
252 
253 void v4lconvert_decode_sq905c(const unsigned char *src, unsigned char *dst,
254 		int width, int height);
255 
256 void v4lconvert_decode_stv0680(const unsigned char *src, unsigned char *dst,
257 		int width, int height);
258 
259 void v4lconvert_bayer_to_rgb24(const unsigned char *bayer,
260 		unsigned char *rgb, int width, int height, const unsigned int stride, unsigned int pixfmt);
261 
262 void v4lconvert_bayer_to_bgr24(const unsigned char *bayer,
263 		unsigned char *rgb, int width, int height, const unsigned int stride, unsigned int pixfmt);
264 
265 void v4lconvert_bayer_to_yuv420(const unsigned char *bayer, unsigned char *yuv,
266 		int width, int height, const unsigned int stride, unsigned int src_pixfmt, int yvu);
267 
268 void v4lconvert_bayer10_to_bayer8(void *bayer10,
269 		unsigned char *bayer8, int width, int height);
270 
271 void v4lconvert_bayer10p_to_bayer8(unsigned char *bayer10p,
272 		unsigned char *bayer8, int width, int height);
273 
274 void v4lconvert_bayer16_to_bayer8(unsigned char *bayer16,
275 		unsigned char *bayer8, int width, int height);
276 
277 void v4lconvert_hm12_to_rgb24(const unsigned char *src,
278 		unsigned char *dst, int width, int height);
279 
280 void v4lconvert_hm12_to_bgr24(const unsigned char *src,
281 		unsigned char *dst, int width, int height);
282 
283 void v4lconvert_hm12_to_yuv420(const unsigned char *src,
284 		unsigned char *dst, int width, int height, int yvu);
285 
286 void v4lconvert_hsv_to_rgb24(const unsigned char *src, unsigned char *dest,
287 		int width, int height, int bgr, int Xin, unsigned char hsv_enc);
288 
289 void v4lconvert_nv12_to_rgb24(const unsigned char *src, unsigned char *dest,
290 		int width, int height, int bgr);
291 
292 void v4lconvert_nv12_to_yuv420(const unsigned char *src, unsigned char *dest,
293 		int width, int height, int yvu);
294 
295 void v4lconvert_rotate90(unsigned char *src, unsigned char *dest,
296 		struct v4l2_format *fmt);
297 
298 void v4lconvert_flip(unsigned char *src, unsigned char *dest,
299 		struct v4l2_format *fmt, int hflip, int vflip);
300 
301 void v4lconvert_crop(unsigned char *src, unsigned char *dest,
302 		const struct v4l2_format *src_fmt, const struct v4l2_format *dest_fmt);
303 
304 int v4lconvert_helper_decompress(struct v4lconvert_data *data,
305 		const char *helper, const unsigned char *src, int src_size,
306 		unsigned char *dest, int dest_size, int width, int height, int command);
307 
308 void v4lconvert_helper_cleanup(struct v4lconvert_data *data);
309 
310 #endif
311