1 /*
2 * xcam_buffer.cpp - video buffer standard version
3 *
4 * Copyright (c) 2016 Intel Corporation
5 *
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 *
18 * Author: Wind Yuan <feng.yuan@intel.com>
19 */
20
21 #include <base/xcam_buffer.h>
22 #include <xcam_log.h>
23
24 XCamReturn
xcam_video_buffer_info_reset(XCamVideoBufferInfo * info,uint32_t format,uint32_t width,uint32_t height,uint32_t aligned_width,uint32_t aligned_height,uint32_t size,bool compacted)25 xcam_video_buffer_info_reset (
26 XCamVideoBufferInfo *info,
27 uint32_t format,
28 uint32_t width, uint32_t height,
29 uint32_t aligned_width, uint32_t aligned_height, uint32_t size, bool compacted)
30 {
31 uint32_t image_size = 0;
32 uint32_t i = 0;
33
34 XCAM_ASSERT (info && format);
35 XCAM_ASSERT (!aligned_width || aligned_width >= width);
36 XCAM_ASSERT (!aligned_height || aligned_height >= height);
37
38 if (!aligned_width)
39 aligned_width = XCAM_ALIGN_UP (width, 4);
40 if (!aligned_height)
41 aligned_height = XCAM_ALIGN_UP (height, 2);
42
43 info->format = format;
44 info->width = width;
45 info->height = height;
46 info->aligned_width = aligned_width;
47 info->aligned_height = aligned_height;
48
49 switch (format) {
50 case V4L2_PIX_FMT_GREY:
51 info->color_bits = 8;
52 info->components = 1;
53 info->strides [0] = aligned_width;
54 info->offsets [0] = 0;
55 image_size = info->strides [0] * aligned_height;
56 break;
57 case V4L2_PIX_FMT_NV12:
58 case V4L2_PIX_FMT_NV21:
59 info->color_bits = 8;
60 info->components = 2;
61 info->strides [0] = aligned_width;
62 info->strides [1] = info->strides [0];
63 info->offsets [0] = 0;
64 info->offsets [1] = info->offsets [0] + info->strides [0] * aligned_height;
65 image_size = info->strides [0] * aligned_height + info->strides [1] * aligned_height / 2;
66 break;
67 case V4L2_PIX_FMT_YUYV:
68 info->color_bits = 8;
69 info->components = 1;
70 info->strides [0] = aligned_width * 2;
71 info->offsets [0] = 0;
72 image_size = info->strides [0] * aligned_height;
73 break;
74 case V4L2_PIX_FMT_RGB565:
75 info->color_bits = 16;
76 info->components = 1;
77 info->strides [0] = aligned_width * 2;
78 info->offsets [0] = 0;
79 image_size = info->strides [0] * aligned_height;
80 break;
81 case V4L2_PIX_FMT_RGB24:
82 info->color_bits = 8;
83 info->components = 1;
84 info->strides [0] = aligned_width * 3;
85 info->offsets [0] = 0;
86 image_size = info->strides [0] * aligned_height;
87 break;
88 // memory order RGBA 8-8-8-8
89 case V4L2_PIX_FMT_RGBA32:
90 // memory order: BGRA 8-8-8-8
91 case V4L2_PIX_FMT_XBGR32:
92 case V4L2_PIX_FMT_ABGR32:
93 case V4L2_PIX_FMT_BGR32:
94 // memory order: ARGB 8-8-8-8
95 case V4L2_PIX_FMT_RGB32:
96 case V4L2_PIX_FMT_ARGB32:
97 case V4L2_PIX_FMT_XRGB32:
98 info->color_bits = 8;
99 info->components = 1;
100 info->strides [0] = aligned_width * 4;
101 info->offsets [0] = 0;
102 image_size = info->strides [0] * aligned_height;
103 break;
104 case XCAM_PIX_FMT_RGB48:
105 info->color_bits = 16;
106 info->components = 1;
107 info->strides [0] = aligned_width * 3 * 2;
108 info->offsets [0] = 0;
109 image_size = info->strides [0] * aligned_height;
110 break;
111 case XCAM_PIX_FMT_RGBA64:
112 info->color_bits = 16;
113 info->components = 1;
114 info->strides [0] = aligned_width * 4 * 2;
115 info->offsets [0] = 0;
116 image_size = info->strides [0] * aligned_height;
117 break;
118
119 case V4L2_PIX_FMT_SBGGR8:
120 case V4L2_PIX_FMT_SGBRG8:
121 case V4L2_PIX_FMT_SGRBG8:
122 case V4L2_PIX_FMT_SRGGB8:
123 info->color_bits = 8;
124 info->components = 1;
125 if (compacted)
126 info->strides [0] = XCAM_ALIGN_UP(aligned_width, 256);
127 else
128 info->strides [0] = aligned_width;
129 info->offsets [0] = 0;
130 image_size = info->strides [0] * aligned_height;
131 break;
132
133 case V4L2_PIX_FMT_SBGGR10:
134 case V4L2_PIX_FMT_SGBRG10:
135 case V4L2_PIX_FMT_SGRBG10:
136 case V4L2_PIX_FMT_SRGGB10:
137 info->color_bits = 10;
138 info->components = 1;
139 if (compacted)
140 info->strides [0] = XCAM_ALIGN_UP((aligned_width * info->color_bits) / 8, 256);
141 else
142 info->strides [0] = aligned_width * 2;
143 info->offsets [0] = 0;
144 image_size = info->strides [0] * aligned_height;
145 break;
146
147 case V4L2_PIX_FMT_SBGGR12:
148 case V4L2_PIX_FMT_SGBRG12:
149 case V4L2_PIX_FMT_SGRBG12:
150 case V4L2_PIX_FMT_SRGGB12:
151 info->color_bits = 12;
152 info->components = 1;
153 if (compacted)
154 info->strides [0] = XCAM_ALIGN_UP((aligned_width * info->color_bits) / 8, 256);
155 else
156 info->strides [0] = aligned_width * 2;
157 info->offsets [0] = 0;
158 image_size = info->strides [0] * aligned_height;
159 break;
160
161 case V4L2_PIX_FMT_SBGGR16:
162 case XCAM_PIX_FMT_SGRBG16:
163 info->color_bits = 16;
164 info->components = 1;
165 if (compacted)
166 info->strides [0] = XCAM_ALIGN_UP((aligned_width * 2), 256);
167 else
168 info->strides [0] = aligned_width * 2;
169 info->offsets [0] = 0;
170 image_size = info->strides [0] * aligned_height;
171 break;
172
173 case XCAM_PIX_FMT_LAB:
174 info->color_bits = 8;
175 info->components = 1;
176 info->strides [0] = aligned_width * 3;
177 info->offsets [0] = 0;
178 image_size = info->strides [0] * aligned_height;
179 break;
180
181 case XCAM_PIX_FMT_RGB48_planar:
182 case XCAM_PIX_FMT_RGB24_planar:
183 if (XCAM_PIX_FMT_RGB48_planar == format)
184 info->color_bits = 16;
185 else
186 info->color_bits = 8;
187 info->components = 3;
188 info->strides [0] = info->strides [1] = info->strides [2] = aligned_width * (info->color_bits / 8);
189 info->offsets [0] = 0;
190 info->offsets [1] = info->offsets [0] + info->strides [0] * aligned_height;
191 info->offsets [2] = info->offsets [1] + info->strides [1] * aligned_height;
192 image_size = info->offsets [2] + info->strides [2] * aligned_height;
193 break;
194
195 case XCAM_PIX_FMT_SGRBG16_planar:
196 case XCAM_PIX_FMT_SGRBG8_planar:
197 if (XCAM_PIX_FMT_SGRBG16_planar == format)
198 info->color_bits = 16;
199 else
200 info->color_bits = 8;
201 info->components = 4;
202 for (i = 0; i < info->components; ++i) {
203 info->strides [i] = aligned_width * (info->color_bits / 8);
204 }
205 info->offsets [0] = 0;
206 for (i = 1; i < info->components; ++i) {
207 info->offsets [i] = info->offsets [i - 1] + info->strides [i - 1] * aligned_height;
208 }
209 image_size = info->offsets [info->components - 1] + info->strides [info->components - 1] * aligned_height;
210 break;
211
212 default:
213 XCAM_LOG_WARNING ("XCamVideoBufferInfo reset failed, unsupported format:%s", xcam_fourcc_to_string (format));
214 return XCAM_RETURN_ERROR_PARAM;
215 }
216
217 if (!size)
218 info->size = image_size;
219 else {
220 XCAM_ASSERT (size >= image_size);
221 info->size = size;
222 }
223
224 return XCAM_RETURN_NO_ERROR;
225 }
226
227 XCamReturn
xcam_video_buffer_get_planar_info(const XCamVideoBufferInfo * buf_info,XCamVideoBufferPlanarInfo * planar_info,const uint32_t index)228 xcam_video_buffer_get_planar_info (
229 const XCamVideoBufferInfo *buf_info, XCamVideoBufferPlanarInfo *planar_info, const uint32_t index)
230 {
231 XCAM_ASSERT (buf_info);
232 XCAM_ASSERT (planar_info);
233
234 planar_info->width = buf_info->width;
235 planar_info->height = buf_info->height;
236 planar_info->pixel_bytes = XCAM_ALIGN_UP (buf_info->color_bits, 8) / 8;
237
238 switch (buf_info->format) {
239 case V4L2_PIX_FMT_NV12:
240 XCAM_ASSERT (index <= 1);
241 if (index == 1) {
242 planar_info->height = buf_info->height / 2;
243 }
244 break;
245
246 case V4L2_PIX_FMT_GREY:
247 case V4L2_PIX_FMT_YUYV:
248 case V4L2_PIX_FMT_RGB565:
249 case V4L2_PIX_FMT_SBGGR8:
250 case V4L2_PIX_FMT_SGBRG8:
251 case V4L2_PIX_FMT_SGRBG8:
252 case V4L2_PIX_FMT_SRGGB8:
253 case V4L2_PIX_FMT_SBGGR10:
254 case V4L2_PIX_FMT_SGBRG10:
255 case V4L2_PIX_FMT_SGRBG10:
256 case V4L2_PIX_FMT_SRGGB10:
257 case V4L2_PIX_FMT_SBGGR12:
258 case V4L2_PIX_FMT_SGBRG12:
259 case V4L2_PIX_FMT_SGRBG12:
260 case V4L2_PIX_FMT_SRGGB12:
261 case V4L2_PIX_FMT_SBGGR16:
262 case XCAM_PIX_FMT_SGRBG16:
263 XCAM_ASSERT (index <= 0);
264 break;
265
266 case V4L2_PIX_FMT_RGB24:
267 XCAM_ASSERT (index <= 0);
268 planar_info->pixel_bytes = 3;
269 break;
270
271 case V4L2_PIX_FMT_RGBA32:
272 case V4L2_PIX_FMT_XBGR32:
273 case V4L2_PIX_FMT_ABGR32:
274 case V4L2_PIX_FMT_BGR32:
275 case V4L2_PIX_FMT_RGB32:
276 case V4L2_PIX_FMT_ARGB32:
277 case V4L2_PIX_FMT_XRGB32:
278 XCAM_ASSERT (index <= 0);
279 planar_info->pixel_bytes = 4;
280 break;
281
282 case XCAM_PIX_FMT_RGB48:
283 XCAM_ASSERT (index <= 0);
284 planar_info->pixel_bytes = 3 * 2;
285 break;
286
287 case XCAM_PIX_FMT_RGBA64:
288 planar_info->pixel_bytes = 4 * 2;
289 break;
290
291 case XCAM_PIX_FMT_LAB:
292 planar_info->pixel_bytes = 3;
293 break;
294
295 case XCAM_PIX_FMT_RGB48_planar:
296 case XCAM_PIX_FMT_RGB24_planar:
297 XCAM_ASSERT (index <= 2);
298 break;
299
300 case XCAM_PIX_FMT_SGRBG16_planar:
301 case XCAM_PIX_FMT_SGRBG8_planar:
302 XCAM_ASSERT (index <= 3);
303 break;
304
305 default:
306 XCAM_LOG_WARNING ("VideoBufferInfo get_planar_info failed, unsupported format:%s", xcam_fourcc_to_string (buf_info->format));
307 return XCAM_RETURN_ERROR_PARAM;
308 }
309
310 return XCAM_RETURN_NO_ERROR;
311 }
312