xref: /linux/drivers/staging/media/atomisp/pci/ia_css_frame_public.h (revision 6fd600d742744dc7ef7fc65ca26daa2b1163158a)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Support for Intel Camera Imaging ISP subsystem.
4  * Copyright (c) 2015, Intel Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms and conditions of the GNU General Public License,
8  * version 2, as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
13  * more details.
14  */
15 
16 #ifndef __IA_CSS_FRAME_PUBLIC_H
17 #define __IA_CSS_FRAME_PUBLIC_H
18 
19 /* @file
20  * This file contains structs to describe various frame-formats supported by the ISP.
21  */
22 
23 #include <media/videobuf2-v4l2.h>
24 #include <type_support.h>
25 #include "ia_css_err.h"
26 #include "ia_css_types.h"
27 #include "ia_css_frame_format.h"
28 #include "ia_css_buffer.h"
29 
30 /* For RAW input, the bayer order needs to be specified separately. There
31  *  are 4 possible orders. The name is constructed by taking the first two
32  *  colors on the first line and the first two colors from the second line.
33  */
34 enum ia_css_bayer_order {
35 	IA_CSS_BAYER_ORDER_GRBG, /** GRGRGRGRGR .. BGBGBGBGBG */
36 	IA_CSS_BAYER_ORDER_RGGB, /** RGRGRGRGRG .. GBGBGBGBGB */
37 	IA_CSS_BAYER_ORDER_BGGR, /** BGBGBGBGBG .. GRGRGRGRGR */
38 	IA_CSS_BAYER_ORDER_GBRG, /** GBGBGBGBGB .. RGRGRGRGRG */
39 };
40 
41 #define IA_CSS_BAYER_ORDER_NUM (IA_CSS_BAYER_ORDER_GBRG + 1)
42 
43 /* Frame plane structure. This describes one plane in an image
44  *  frame buffer.
45  */
46 struct ia_css_frame_plane {
47 	unsigned int height; /** height of a plane in lines */
48 	unsigned int width;  /** width of a line, in DMA elements, note that
49 				  for RGB565 the three subpixels are stored in
50 				  one element. For all other formats this is
51 				  the number of subpixels per line. */
52 	unsigned int stride; /** stride of a line in bytes */
53 	unsigned int offset; /** offset in bytes to start of frame data.
54 				  offset is wrt data field in ia_css_frame */
55 };
56 
57 /* Binary "plane". This is used to story binary streams such as jpeg
58  *  images. This is not actually a real plane.
59  */
60 struct ia_css_frame_binary_plane {
61 	unsigned int		  size; /** number of bytes in the stream */
62 	struct ia_css_frame_plane data; /** plane */
63 };
64 
65 /* Container for planar YUV frames. This contains 3 planes.
66  */
67 struct ia_css_frame_yuv_planes {
68 	struct ia_css_frame_plane y; /** Y plane */
69 	struct ia_css_frame_plane u; /** U plane */
70 	struct ia_css_frame_plane v; /** V plane */
71 };
72 
73 /* Container for semi-planar YUV frames.
74   */
75 struct ia_css_frame_nv_planes {
76 	struct ia_css_frame_plane y;  /** Y plane */
77 	struct ia_css_frame_plane uv; /** UV plane */
78 };
79 
80 /* Container for planar RGB frames. Each color has its own plane.
81  */
82 struct ia_css_frame_rgb_planes {
83 	struct ia_css_frame_plane r; /** Red plane */
84 	struct ia_css_frame_plane g; /** Green plane */
85 	struct ia_css_frame_plane b; /** Blue plane */
86 };
87 
88 /* Container for 6-plane frames. These frames are used internally
89  *  in the advanced ISP only.
90  */
91 struct ia_css_frame_plane6_planes {
92 	struct ia_css_frame_plane r;	  /** Red plane */
93 	struct ia_css_frame_plane r_at_b; /** Red at blue plane */
94 	struct ia_css_frame_plane gr;	  /** Red-green plane */
95 	struct ia_css_frame_plane gb;	  /** Blue-green plane */
96 	struct ia_css_frame_plane b;	  /** Blue plane */
97 	struct ia_css_frame_plane b_at_r; /** Blue at red plane */
98 };
99 
100 /* Crop info struct - stores the lines to be cropped in isp */
101 struct ia_css_crop_info {
102 	/* the final start column and start line
103 	 * sum of lines to be cropped + bayer offset
104 	 */
105 	unsigned int start_column;
106 	unsigned int start_line;
107 };
108 
109 /* Frame info struct. This describes the contents of an image frame buffer.
110   */
111 struct ia_css_frame_info {
112 	struct ia_css_resolution res; /** Frame resolution (valid data) */
113 	unsigned int padded_width; /** stride of line in memory (in pixels) */
114 	enum ia_css_frame_format format; /** format of the frame data */
115 	unsigned int raw_bit_depth; /** number of valid bits per pixel,
116 					 only valid for RAW bayer frames */
117 	enum ia_css_bayer_order raw_bayer_order; /** bayer order, only valid
118 						      for RAW bayer frames */
119 	/* the params below are computed based on bayer_order
120 	 * we can remove the raw_bayer_order if it is redundant
121 	 * keeping it for now as bxt and fpn code seem to use it
122 	 */
123 	struct ia_css_crop_info crop_info;
124 };
125 
126 #define IA_CSS_BINARY_DEFAULT_FRAME_INFO { \
127 	.format			= IA_CSS_FRAME_FORMAT_NUM,  \
128 	.raw_bayer_order	= IA_CSS_BAYER_ORDER_NUM, \
129 }
130 
131 /**
132  *  Specifies the DVS loop delay in "frame periods"
133  */
134 enum ia_css_frame_delay {
135 	IA_CSS_FRAME_DELAY_0, /** Frame delay = 0 */
136 	IA_CSS_FRAME_DELAY_1, /** Frame delay = 1 */
137 	IA_CSS_FRAME_DELAY_2  /** Frame delay = 2 */
138 };
139 
140 /* Frame structure. This structure describes an image buffer or frame.
141  *  This is the main structure used for all input and output images.
142  */
143 struct ia_css_frame {
144 	/*
145 	 * The videobuf2 core will allocate buffers including room for private
146 	 * data (the rest of struct ia_css_frame). The vb2_v4l2_buffer must
147 	 * be the first member for this to work!
148 	 * Note the atomisp code also uses ia_css_frame-s which are not used
149 	 * as v4l2-buffers in some places. In this case the vb2 member will
150 	 * be unused.
151 	 */
152 	struct vb2_v4l2_buffer vb;
153 	/* List-head for linking into the activeq or buffers_waiting_for_param list */
154 	struct list_head queue;
155 	struct ia_css_frame_info frame_info; /** info struct describing the frame */
156 	ia_css_ptr   data;	       /** pointer to start of image data */
157 	unsigned int data_bytes;       /** size of image data in bytes */
158 	/* LA: move this to ia_css_buffer */
159 	/*
160 	 * -1 if data address is static during life time of pipeline
161 	 * >=0 if data address can change per pipeline/frame iteration
162 	 *     index to dynamic data: ia_css_frame_in, ia_css_frame_out
163 	 *                            ia_css_frame_out_vf
164 	 *     index to host-sp queue id: queue_0, queue_1 etc.
165 	 */
166 	int dynamic_queue_id;
167 	/*
168 	 * if it is dynamic frame, buf_type indicates which buffer type it
169 	 * should use for event generation. we have this because in vf_pp
170 	 * binary, we use output port, but we expect VF_OUTPUT_DONE event
171 	 */
172 	enum ia_css_buffer_type buf_type;
173 	unsigned int exp_id;
174 	/** exposure id, see ia_css_event_public.h for more detail */
175 	u32 isp_config_id; /** Unique ID to track which config was actually applied to a particular frame */
176 	bool valid; /** First video output frame is not valid */
177 	union {
178 		unsigned int	_initialisation_dummy;
179 		struct ia_css_frame_plane raw;
180 		struct ia_css_frame_plane rgb;
181 		struct ia_css_frame_rgb_planes planar_rgb;
182 		struct ia_css_frame_plane yuyv;
183 		struct ia_css_frame_yuv_planes yuv;
184 		struct ia_css_frame_nv_planes nv;
185 		struct ia_css_frame_plane6_planes plane6;
186 		struct ia_css_frame_binary_plane binary;
187 	} planes; /** frame planes, select the right one based on
188 		       info.format */
189 };
190 
191 #define vb_to_frame(vb2) \
192 	container_of(to_vb2_v4l2_buffer(vb2), struct ia_css_frame, vb)
193 
194 #define DEFAULT_FRAME { \
195 	.frame_info		= IA_CSS_BINARY_DEFAULT_FRAME_INFO, \
196 	.dynamic_queue_id	= SH_CSS_INVALID_QUEUE_ID, \
197 	.buf_type		= IA_CSS_BUFFER_TYPE_INVALID, \
198 }
199 
200 /* @brief Allocate a CSS frame structure
201  *
202  * @param	frame		The allocated frame.
203  * @param	width		The width (in pixels) of the frame.
204  * @param	height		The height (in lines) of the frame.
205  * @param	format		The frame format.
206  * @param	stride		The padded stride, in pixels.
207  * @param	raw_bit_depth	The raw bit depth, in bits.
208  * @return			The error code.
209  *
210  * Allocate a CSS frame structure. The memory for the frame data will be
211  * allocated in the CSS address space.
212  */
213 int
214 ia_css_frame_allocate(struct ia_css_frame **frame,
215 		      unsigned int width,
216 		      unsigned int height,
217 		      enum ia_css_frame_format format,
218 		      unsigned int stride,
219 		      unsigned int raw_bit_depth);
220 
221 /* @brief Initialize a CSS frame structure using a frame info structure.
222  *
223  * @param	frame	The allocated frame.
224  * @param[in]	info	The frame info structure.
225  * @return		The error code.
226  *
227  * Initialize a frame using the resolution and format from a frame info struct.
228  */
229 int ia_css_frame_init_from_info(struct ia_css_frame *frame,
230 				const struct ia_css_frame_info *info);
231 
232 /* @brief Allocate a CSS frame structure using a frame info structure.
233  *
234  * @param	frame	The allocated frame.
235  * @param[in]	info	The frame info structure.
236  * @return		The error code.
237  *
238  * Allocate a frame using the resolution and format from a frame info struct.
239  * This is a convenience function, implemented on top of
240  * ia_css_frame_allocate().
241  */
242 int
243 ia_css_frame_allocate_from_info(struct ia_css_frame **frame,
244 				const struct ia_css_frame_info *info);
245 /* @brief Free a CSS frame structure.
246  *
247  * @param[in]	frame	Pointer to the frame.
248  * @return	None
249  *
250  * Free a CSS frame structure. This will free both the frame structure
251  * and the pixel data pointer contained within the frame structure.
252  */
253 void
254 ia_css_frame_free(struct ia_css_frame *frame);
255 
256 static inline const struct ia_css_frame_info *
ia_css_frame_get_info(const struct ia_css_frame * frame)257 ia_css_frame_get_info(const struct ia_css_frame *frame)
258 {
259 	return frame ? &frame->frame_info : NULL;
260 }
261 
262 #endif /* __IA_CSS_FRAME_PUBLIC_H */
263