blob: b31af561b74a7e95ff3d3220d8ff5ae85dafc28e [file] [log] [blame]
xuesong.jiangae1548e2022-05-06 16:38:46 +08001/* GStreamer
2 * Copyright (C) 2022 <xuesong.jiang@amlogic.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library 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 GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Suite 500,
17 * Boston, MA 02110-1335, USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24#include <sys/stat.h>
25#include <fcntl.h>
26#include <errno.h>
27#include <string.h>
28#include <sys/mman.h>
29#include <sys/ioctl.h>
xuesong.jiange1a19662022-06-21 20:30:22 +080030#include <stdio.h>
zengliang.lic9f869d2023-02-15 08:32:32 +000031#include <sys/utsname.h>
xuesong.jiangae1548e2022-05-06 16:38:46 +080032
33#ifdef HAVE_GUDEV
34#include <gudev/gudev.h>
35#endif
36
37#include "ext/videodev2.h"
38#include "gstamlv4l2object.h"
39
40#include "gst/gst-i18n-plugin.h"
41
42#include <gst/video/video.h>
43#include <gst/allocators/gstdmabuf.h>
44
45GST_DEBUG_CATEGORY_EXTERN(aml_v4l2_debug);
46#define GST_CAT_DEFAULT aml_v4l2_debug
47
48#define DEFAULT_PROP_DEVICE_NAME NULL
49#define DEFAULT_PROP_DEVICE_FD -1
50#define DEFAULT_PROP_FLAGS 0
51#define DEFAULT_PROP_TV_NORM 0
52#define DEFAULT_PROP_IO_MODE GST_V4L2_IO_AUTO
53
le.handd21c802024-06-13 09:17:41 +000054#define ENCODED_BUFFER_SIZE (4 * 1024 * 1024)
xuesong.jiangae1548e2022-05-06 16:38:46 +080055
xuesong.jiange1a19662022-06-21 20:30:22 +080056#define V4L2_CONFIG_PARM_DECODE_CFGINFO (1 << 0)
57#define V4L2_CONFIG_PARM_DECODE_PSINFO (1 << 1)
58#define V4L2_CONFIG_PARM_DECODE_HDRINFO (1 << 2)
59#define V4L2_CONFIG_PARM_DECODE_CNTINFO (1 << 3)
60
zengliang.lic9f869d2023-02-15 08:32:32 +000061#define V4L2_CID_USER_AMLOGIC_BASE (V4L2_CID_USER_BASE + 0x1100)
62#define AML_V4L2_SET_DRMMODE (V4L2_CID_USER_AMLOGIC_BASE + 0)
hanghang.luo6a5bdff2024-04-15 06:29:43 +000063#define AML_V4L2_GET_FILMGRAIN_INFO (V4L2_CID_USER_AMLOGIC_BASE + 3)
zengliang.lic9f869d2023-02-15 08:32:32 +000064#define AML_V4L2_DEC_PARMS_CONFIG (V4L2_CID_USER_AMLOGIC_BASE + 7)
xuesong.jiang22a9b112023-05-24 09:01:59 +000065#define AML_V4L2_SET_STREAM_MODE (V4L2_CID_USER_AMLOGIC_BASE + 9)
zengliang.lic9f869d2023-02-15 08:32:32 +000066
xuesong.jiangae1548e2022-05-06 16:38:46 +080067enum
68{
69 PROP_0,
70 V4L2_STD_OBJECT_PROPS,
71};
72
73/*
74 * common format / caps utilities:
75 */
76typedef enum
77{
78 GST_V4L2_RAW = 1 << 0,
79 GST_V4L2_CODEC = 1 << 1,
80 GST_V4L2_TRANSPORT = 1 << 2,
81 GST_V4L2_NO_PARSE = 1 << 3,
82 GST_V4L2_ALL = 0xffff
83} GstAmlV4L2FormatFlags;
84
85typedef struct
86{
87 guint32 format;
88 gboolean dimensions;
89 GstAmlV4L2FormatFlags flags;
90} GstAmlV4L2FormatDesc;
91
92static const GstAmlV4L2FormatDesc gst_aml_v4l2_formats[] = {
93 /* RGB formats */
94 {V4L2_PIX_FMT_RGB332, TRUE, GST_V4L2_RAW},
95 {V4L2_PIX_FMT_ARGB555, TRUE, GST_V4L2_RAW},
96 {V4L2_PIX_FMT_XRGB555, TRUE, GST_V4L2_RAW},
97 {V4L2_PIX_FMT_ARGB555X, TRUE, GST_V4L2_RAW},
98 {V4L2_PIX_FMT_XRGB555X, TRUE, GST_V4L2_RAW},
99 {V4L2_PIX_FMT_RGB565, TRUE, GST_V4L2_RAW},
100 {V4L2_PIX_FMT_RGB565X, TRUE, GST_V4L2_RAW},
101 {V4L2_PIX_FMT_BGR666, TRUE, GST_V4L2_RAW},
102 {V4L2_PIX_FMT_BGR24, TRUE, GST_V4L2_RAW},
103 {V4L2_PIX_FMT_RGB24, TRUE, GST_V4L2_RAW},
104 {V4L2_PIX_FMT_ABGR32, TRUE, GST_V4L2_RAW},
105 {V4L2_PIX_FMT_XBGR32, TRUE, GST_V4L2_RAW},
106 {V4L2_PIX_FMT_ARGB32, TRUE, GST_V4L2_RAW},
107 {V4L2_PIX_FMT_XRGB32, TRUE, GST_V4L2_RAW},
108
109 /* Deprecated Packed RGB Image Formats (alpha ambiguity) */
110 {V4L2_PIX_FMT_RGB444, TRUE, GST_V4L2_RAW},
111 {V4L2_PIX_FMT_RGB555, TRUE, GST_V4L2_RAW},
112 {V4L2_PIX_FMT_RGB555X, TRUE, GST_V4L2_RAW},
113 {V4L2_PIX_FMT_BGR32, TRUE, GST_V4L2_RAW},
114 {V4L2_PIX_FMT_RGB32, TRUE, GST_V4L2_RAW},
115
116 /* Grey formats */
117 {V4L2_PIX_FMT_GREY, TRUE, GST_V4L2_RAW},
118 {V4L2_PIX_FMT_Y4, TRUE, GST_V4L2_RAW},
119 {V4L2_PIX_FMT_Y6, TRUE, GST_V4L2_RAW},
120 {V4L2_PIX_FMT_Y10, TRUE, GST_V4L2_RAW},
121 {V4L2_PIX_FMT_Y12, TRUE, GST_V4L2_RAW},
122 {V4L2_PIX_FMT_Y16, TRUE, GST_V4L2_RAW},
123 {V4L2_PIX_FMT_Y16_BE, TRUE, GST_V4L2_RAW},
124 {V4L2_PIX_FMT_Y10BPACK, TRUE, GST_V4L2_RAW},
125
126 /* Palette formats */
127 {V4L2_PIX_FMT_PAL8, TRUE, GST_V4L2_RAW},
128
129 /* Chrominance formats */
130 {V4L2_PIX_FMT_UV8, TRUE, GST_V4L2_RAW},
131
132 /* Luminance+Chrominance formats */
133 {V4L2_PIX_FMT_YVU410, TRUE, GST_V4L2_RAW},
134 {V4L2_PIX_FMT_YVU420, TRUE, GST_V4L2_RAW},
135 {V4L2_PIX_FMT_YVU420M, TRUE, GST_V4L2_RAW},
136 {V4L2_PIX_FMT_YUYV, TRUE, GST_V4L2_RAW},
137 {V4L2_PIX_FMT_YYUV, TRUE, GST_V4L2_RAW},
138 {V4L2_PIX_FMT_YVYU, TRUE, GST_V4L2_RAW},
139 {V4L2_PIX_FMT_UYVY, TRUE, GST_V4L2_RAW},
140 {V4L2_PIX_FMT_VYUY, TRUE, GST_V4L2_RAW},
141 {V4L2_PIX_FMT_YUV422P, TRUE, GST_V4L2_RAW},
142 {V4L2_PIX_FMT_YUV411P, TRUE, GST_V4L2_RAW},
143 {V4L2_PIX_FMT_Y41P, TRUE, GST_V4L2_RAW},
144 {V4L2_PIX_FMT_YUV444, TRUE, GST_V4L2_RAW},
145 {V4L2_PIX_FMT_YUV555, TRUE, GST_V4L2_RAW},
146 {V4L2_PIX_FMT_YUV565, TRUE, GST_V4L2_RAW},
147 {V4L2_PIX_FMT_YUV32, TRUE, GST_V4L2_RAW},
148 {V4L2_PIX_FMT_YUV410, TRUE, GST_V4L2_RAW},
149 {V4L2_PIX_FMT_YUV420, TRUE, GST_V4L2_RAW},
150 {V4L2_PIX_FMT_YUV420M, TRUE, GST_V4L2_RAW},
151 {V4L2_PIX_FMT_HI240, TRUE, GST_V4L2_RAW},
152 {V4L2_PIX_FMT_HM12, TRUE, GST_V4L2_RAW},
153 {V4L2_PIX_FMT_M420, TRUE, GST_V4L2_RAW},
154
155 /* two planes -- one Y, one Cr + Cb interleaved */
156 {V4L2_PIX_FMT_NV12, TRUE, GST_V4L2_RAW},
157 {V4L2_PIX_FMT_NV12M, TRUE, GST_V4L2_RAW},
158 {V4L2_PIX_FMT_NV12MT, TRUE, GST_V4L2_RAW},
159 {V4L2_PIX_FMT_NV12MT_16X16, TRUE, GST_V4L2_RAW},
160 {V4L2_PIX_FMT_NV21, TRUE, GST_V4L2_RAW},
161 {V4L2_PIX_FMT_NV21M, TRUE, GST_V4L2_RAW},
162 {V4L2_PIX_FMT_NV16, TRUE, GST_V4L2_RAW},
163 {V4L2_PIX_FMT_NV16M, TRUE, GST_V4L2_RAW},
164 {V4L2_PIX_FMT_NV61, TRUE, GST_V4L2_RAW},
165 {V4L2_PIX_FMT_NV61M, TRUE, GST_V4L2_RAW},
166 {V4L2_PIX_FMT_NV24, TRUE, GST_V4L2_RAW},
167 {V4L2_PIX_FMT_NV42, TRUE, GST_V4L2_RAW},
168
169 /* Bayer formats - see http://www.siliconimaging.com/RGB%20Bayer.htm */
170 {V4L2_PIX_FMT_SBGGR8, TRUE, GST_V4L2_RAW},
171 {V4L2_PIX_FMT_SGBRG8, TRUE, GST_V4L2_RAW},
172 {V4L2_PIX_FMT_SGRBG8, TRUE, GST_V4L2_RAW},
173 {V4L2_PIX_FMT_SRGGB8, TRUE, GST_V4L2_RAW},
174
175 /* compressed formats */
176 {V4L2_PIX_FMT_MJPEG, FALSE, GST_V4L2_CODEC},
177 {V4L2_PIX_FMT_JPEG, FALSE, GST_V4L2_CODEC},
178 {V4L2_PIX_FMT_PJPG, FALSE, GST_V4L2_CODEC},
179 {V4L2_PIX_FMT_DV, FALSE, GST_V4L2_TRANSPORT},
180 {V4L2_PIX_FMT_MPEG, FALSE, GST_V4L2_TRANSPORT},
181 {V4L2_PIX_FMT_FWHT, FALSE, GST_V4L2_CODEC},
xuesong.jiang26fa9792023-09-21 11:34:40 +0000182 {V4L2_PIX_FMT_H264, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
183 {V4L2_PIX_FMT_H264_NO_SC, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
xuesong.jiangae1548e2022-05-06 16:38:46 +0800184 {V4L2_PIX_FMT_H264_MVC, FALSE, GST_V4L2_CODEC},
185 {V4L2_PIX_FMT_HEVC, FALSE, GST_V4L2_CODEC},
186 {V4L2_PIX_FMT_H263, FALSE, GST_V4L2_CODEC},
187 {V4L2_PIX_FMT_MPEG1, FALSE, GST_V4L2_CODEC},
188 {V4L2_PIX_FMT_MPEG2, FALSE, GST_V4L2_CODEC},
189 {V4L2_PIX_FMT_MPEG4, FALSE, GST_V4L2_CODEC},
190 {V4L2_PIX_FMT_XVID, FALSE, GST_V4L2_CODEC},
191 {V4L2_PIX_FMT_VC1_ANNEX_G, FALSE, GST_V4L2_CODEC},
192 {V4L2_PIX_FMT_VC1_ANNEX_L, FALSE, GST_V4L2_CODEC},
193 {V4L2_PIX_FMT_VP8, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
194 {V4L2_PIX_FMT_VP9, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
195 {V4L2_PIX_FMT_AV1, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
zengliang.li51f54b62023-10-10 12:14:49 +0000196 {V4L2_PIX_FMT_AVS, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
197 {V4L2_PIX_FMT_AVS2, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
198 {V4L2_PIX_FMT_AVS3, FALSE, GST_V4L2_CODEC | GST_V4L2_NO_PARSE},
xuesong.jiangae1548e2022-05-06 16:38:46 +0800199
200 /* Vendor-specific formats */
201 {V4L2_PIX_FMT_WNVA, TRUE, GST_V4L2_CODEC},
202 {V4L2_PIX_FMT_SN9C10X, TRUE, GST_V4L2_CODEC},
203 {V4L2_PIX_FMT_PWC1, TRUE, GST_V4L2_CODEC},
204 {V4L2_PIX_FMT_PWC2, TRUE, GST_V4L2_CODEC},
205};
206
207#define GST_AML_V4L2_FORMAT_COUNT (G_N_ELEMENTS(gst_aml_v4l2_formats))
208
209static GSList *gst_aml_v4l2_object_get_format_list(GstAmlV4l2Object *v4l2object);
xuesong.jiang22a9b112023-05-24 09:01:59 +0000210static gboolean gst_aml_v4l2_set_control(GstAmlV4l2Object *v4l2object, guint ctl);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800211
212#define GST_TYPE_AML_V4L2_DEVICE_FLAGS (gst_aml_v4l2_device_get_type())
213static GType
214gst_aml_v4l2_device_get_type(void)
215{
216 static GType v4l2_device_type = 0;
217
218 if (v4l2_device_type == 0)
219 {
220 static const GFlagsValue values[] = {
221 {V4L2_CAP_VIDEO_CAPTURE, "Device supports video capture", "capture"},
222 {V4L2_CAP_VIDEO_OUTPUT, "Device supports video playback", "output"},
223 {V4L2_CAP_VIDEO_OVERLAY, "Device supports video overlay", "overlay"},
224
225 {V4L2_CAP_VBI_CAPTURE, "Device supports the VBI capture", "vbi-capture"},
226 {V4L2_CAP_VBI_OUTPUT, "Device supports the VBI output", "vbi-output"},
227
228 {V4L2_CAP_TUNER, "Device has a tuner or modulator", "tuner"},
229 {V4L2_CAP_AUDIO, "Device has audio inputs or outputs", "audio"},
230
231 {0, NULL, NULL}};
232
233 v4l2_device_type =
234 g_flags_register_static("GstAmlV4l2DeviceTypeFlags", values);
235 }
236
237 return v4l2_device_type;
238}
239
240GType gst_aml_v4l2_io_mode_get_type(void)
241{
242 static GType v4l2_io_mode = 0;
243
244 if (!v4l2_io_mode)
245 {
246 static const GEnumValue io_modes[] = {
247 {GST_V4L2_IO_AUTO, "GST_V4L2_IO_AUTO", "auto"},
248 {GST_V4L2_IO_RW, "GST_V4L2_IO_RW", "rw"},
249 {GST_V4L2_IO_MMAP, "GST_V4L2_IO_MMAP", "mmap"},
250 {GST_V4L2_IO_USERPTR, "GST_V4L2_IO_USERPTR", "userptr"},
251 {GST_V4L2_IO_DMABUF, "GST_V4L2_IO_DMABUF", "dmabuf"},
252 {GST_V4L2_IO_DMABUF_IMPORT, "GST_V4L2_IO_DMABUF_IMPORT",
253 "dmabuf-import"},
254
255 {0, NULL, NULL}};
256 v4l2_io_mode = g_enum_register_static("GstAmlV4l2IOMode", io_modes);
257 }
258 return v4l2_io_mode;
259}
260
261void gst_aml_v4l2_object_install_properties_helper(GObjectClass *gobject_class,
262 const char *default_device)
263{
264 g_object_class_install_property(gobject_class, PROP_DEVICE,
265 g_param_spec_string("device", "Device", "Device location",
266 default_device, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
267 g_object_class_install_property(gobject_class, PROP_DEVICE_NAME,
268 g_param_spec_string("device-name", "Device name",
269 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
270 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
271 g_object_class_install_property(gobject_class, PROP_DEVICE_FD,
272 g_param_spec_int("device-fd", "File descriptor",
273 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
274 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
275 g_object_class_install_property(gobject_class, PROP_FLAGS,
276 g_param_spec_flags("flags", "Flags", "Device type flags",
277 GST_TYPE_AML_V4L2_DEVICE_FLAGS, DEFAULT_PROP_FLAGS,
278 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
279
280 /**
281 * GstV4l2Src:brightness:
282 *
283 * Picture brightness, or more precisely, the black level
284 */
285 g_object_class_install_property(gobject_class, PROP_BRIGHTNESS,
286 g_param_spec_int("brightness", "Brightness",
287 "Picture brightness, or more precisely, the black level", G_MININT,
288 G_MAXINT, 0,
289 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
290 /**
291 * GstV4l2Src:contrast:
292 *
293 * Picture contrast or luma gain
294 */
295 g_object_class_install_property(gobject_class, PROP_CONTRAST,
296 g_param_spec_int("contrast", "Contrast",
297 "Picture contrast or luma gain", G_MININT,
298 G_MAXINT, 0,
299 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
300 /**
301 * GstV4l2Src:saturation:
302 *
303 * Picture color saturation or chroma gain
304 */
305 g_object_class_install_property(gobject_class, PROP_SATURATION,
306 g_param_spec_int("saturation", "Saturation",
307 "Picture color saturation or chroma gain", G_MININT,
308 G_MAXINT, 0,
309 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
310 /**
311 * GstV4l2Src:hue:
312 *
313 * Hue or color balance
314 */
315 g_object_class_install_property(gobject_class, PROP_HUE,
316 g_param_spec_int("hue", "Hue",
317 "Hue or color balance", G_MININT,
318 G_MAXINT, 0,
319 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS | GST_PARAM_CONTROLLABLE));
320
321 /**
322 * GstV4l2Src:io-mode:
323 *
324 * IO Mode
325 */
326 g_object_class_install_property(gobject_class, PROP_IO_MODE,
327 g_param_spec_enum("io-mode", "IO mode",
328 "I/O mode",
329 GST_TYPE_AML_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
330 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
331
332 /**
333 * GstV4l2Src:extra-controls:
334 *
335 * Additional v4l2 controls for the device. The controls are identified
336 * by the control name (lowercase with '_' for any non-alphanumeric
337 * characters).
338 *
339 * Since: 1.2
340 */
341 g_object_class_install_property(gobject_class, PROP_EXTRA_CONTROLS,
342 g_param_spec_boxed("extra-controls", "Extra Controls",
343 "Extra v4l2 controls (CIDs) for the device",
344 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
345
346 /**
347 * GstV4l2Src:pixel-aspect-ratio:
348 *
349 * The pixel aspect ratio of the device. This overwrites the pixel aspect
350 * ratio queried from the device.
351 *
352 * Since: 1.2
353 */
354 g_object_class_install_property(gobject_class, PROP_PIXEL_ASPECT_RATIO,
355 g_param_spec_string("pixel-aspect-ratio", "Pixel Aspect Ratio",
356 "Overwrite the pixel aspect ratio of the device", "1/1",
357 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
358
359 /**
360 * GstV4l2Src:force-aspect-ratio:
361 *
362 * When enabled, the pixel aspect ratio queried from the device or set
363 * with the pixel-aspect-ratio property will be enforced.
364 *
365 * Since: 1.2
366 */
367 g_object_class_install_property(gobject_class, PROP_FORCE_ASPECT_RATIO,
368 g_param_spec_boolean("force-aspect-ratio", "Force aspect ratio",
369 "When enabled, the pixel aspect ratio will be enforced", TRUE,
370 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
371}
372
373void gst_aml_v4l2_object_install_m2m_properties_helper(GObjectClass *gobject_class)
374{
375 g_object_class_install_property(gobject_class, PROP_DEVICE,
376 g_param_spec_string("device", "Device", "Device location",
377 NULL, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
378
379 g_object_class_install_property(gobject_class, PROP_DEVICE_NAME,
380 g_param_spec_string("device-name", "Device name",
381 "Name of the device", DEFAULT_PROP_DEVICE_NAME,
382 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
383
384 g_object_class_install_property(gobject_class, PROP_DEVICE_FD,
385 g_param_spec_int("device-fd", "File descriptor",
386 "File descriptor of the device", -1, G_MAXINT, DEFAULT_PROP_DEVICE_FD,
387 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
388
389 g_object_class_install_property(gobject_class, PROP_OUTPUT_IO_MODE,
390 g_param_spec_enum("output-io-mode", "Output IO mode",
391 "Output side I/O mode (matches sink pad)",
392 GST_TYPE_AML_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
393 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
394
395 g_object_class_install_property(gobject_class, PROP_CAPTURE_IO_MODE,
396 g_param_spec_enum("capture-io-mode", "Capture IO mode",
397 "Capture I/O mode (matches src pad)",
398 GST_TYPE_AML_V4L2_IO_MODE, DEFAULT_PROP_IO_MODE,
399 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
400
401 g_object_class_install_property(gobject_class, PROP_EXTRA_CONTROLS,
402 g_param_spec_boxed("extra-controls", "Extra Controls",
403 "Extra v4l2 controls (CIDs) for the device",
404 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
405
406 g_object_class_install_property(gobject_class, PROP_DUMP_FRAME_LOCATION,
407 g_param_spec_string("dump-frame-location", "dump frame location",
408 "Location of the file to write decoder frames", NULL,
409 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
xuesong.jiang22a9b112023-05-24 09:01:59 +0000410
411 g_object_class_install_property(gobject_class, PROP_STREAM_MODE,
412 g_param_spec_boolean("stream-mode", "Configure v4l2 stream mode",
413 "TRUE for stream mode, FALSE for frame mode",
414 FALSE,
415 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
hanghang.luoc54208e2023-09-22 02:43:54 +0000416 g_object_class_install_property(gobject_class, PROP_LOW_LATENCY_MODE,
417 g_param_spec_boolean("low-latency-mode", "set low latency mode",
418 "enable is TURE, disable is FALSE, default is disable",
419 FALSE,
420 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
xuesong.jiangae1548e2022-05-06 16:38:46 +0800421}
422
423/* Support for 32bit off_t, this wrapper is casting off_t to gint64 */
424#ifdef HAVE_LIBV4L2
425#if SIZEOF_OFF_T < 8
426
427static gpointer
428v4l2_mmap_wrapper(gpointer start, gsize length, gint prot, gint flags, gint fd,
429 off_t offset)
430{
431 return v4l2_mmap(start, length, prot, flags, fd, (gint64)offset);
432}
433
434#define v4l2_mmap v4l2_mmap_wrapper
435
436#endif /* SIZEOF_OFF_T < 8 */
437#endif /* HAVE_LIBV4L2 */
438
439GstAmlV4l2Object *
440gst_aml_v4l2_object_new(GstElement *element,
441 GstObject *debug_object,
442 enum v4l2_buf_type type,
443 const char *default_device,
444 GstAmlV4l2GetInOutFunction get_in_out_func,
445 GstAmlV4l2SetInOutFunction set_in_out_func,
446 GstAmlV4l2UpdateFpsFunction update_fps_func)
447{
448 GstAmlV4l2Object *v4l2object;
449
450 /*
451 * some default values
452 */
453 v4l2object = g_new0(GstAmlV4l2Object, 1);
454
455 if ((V4L2_BUF_TYPE_VIDEO_CAPTURE == type || V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE == type))
456 {
457 const char *default_mode = getenv("GST_DEFAULT_V4L2_BUF_MODE");
458 GST_DEBUG("amlmodbuf GST_AML_DEFAULT_V4L2_BUF_MODE:%s", default_mode);
fei.denge9458472023-04-18 02:05:48 +0000459 //v4l2object->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800460 if (default_mode)
461 {
462 if (strcmp(default_mode, "DMA_BUF_IMPORT") == 0)
463 v4l2object->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
464 else if (strcmp(default_mode, "DMA_BUF") == 0)
465 v4l2object->req_mode = GST_V4L2_IO_DMABUF;
466 GST_DEBUG("amlmodbuf set default buf default_mode:%d", v4l2object->req_mode);
467 }
468 }
469
470 v4l2object->type = type;
471 v4l2object->formats = NULL;
472
473 v4l2object->element = element;
474 v4l2object->dbg_obj = debug_object;
475 v4l2object->get_in_out_func = get_in_out_func;
476 v4l2object->set_in_out_func = set_in_out_func;
477 v4l2object->update_fps_func = update_fps_func;
478
479 v4l2object->video_fd = -1;
480 v4l2object->active = FALSE;
481 v4l2object->videodev = g_strdup(default_device);
482
483 v4l2object->norms = NULL;
484 v4l2object->channels = NULL;
485 v4l2object->colors = NULL;
486
487 v4l2object->keep_aspect = TRUE;
xuesong.jiang22a9b112023-05-24 09:01:59 +0000488 v4l2object->stream_mode = FALSE;
sheng.liu641aa422023-12-26 07:05:59 +0000489 v4l2object->have_set_par = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800490
491 v4l2object->n_v4l2_planes = 0;
492
493 v4l2object->no_initial_format = FALSE;
494
495 /* We now disable libv4l2 by default, but have an env to enable it. */
496#ifdef HAVE_LIBV4L2
497 if (g_getenv("GST_V4L2_USE_LIBV4L2"))
498 {
499 v4l2object->fd_open = v4l2_fd_open;
500 v4l2object->close = v4l2_close;
501 v4l2object->dup = v4l2_dup;
502 v4l2object->ioctl = v4l2_ioctl;
503 v4l2object->read = v4l2_read;
504 v4l2object->mmap = v4l2_mmap;
505 v4l2object->munmap = v4l2_munmap;
506 }
507 else
508#endif
509 {
510 v4l2object->fd_open = NULL;
511 v4l2object->close = close;
512 v4l2object->dup = dup;
513 v4l2object->ioctl = ioctl;
514 v4l2object->read = read;
515 v4l2object->mmap = mmap;
516 v4l2object->munmap = munmap;
517 }
518 v4l2object->poll = gst_poll_new(TRUE);
519 v4l2object->can_wait_event = FALSE;
520 v4l2object->can_poll_device = TRUE;
521 v4l2object->tvin_port = -1;
522
523 v4l2object->dumpframefile = NULL;
524
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800525 /* jxsdbg resolution switching */
526 v4l2object->old_other_pool = NULL;
527 v4l2object->old_old_other_pool = NULL;
528 v4l2object->outstanding_buf_num = 0;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800529 return v4l2object;
530}
531
532static gboolean gst_aml_v4l2_object_clear_format_list(GstAmlV4l2Object *v4l2object);
533
534void gst_aml_v4l2_object_destroy(GstAmlV4l2Object *v4l2object)
535{
536 g_return_if_fail(v4l2object != NULL);
537
538 g_free(v4l2object->videodev);
539
540 g_free(v4l2object->channel);
541
542 if (v4l2object->formats)
543 {
544 gst_aml_v4l2_object_clear_format_list(v4l2object);
545 }
546
547 if (v4l2object->probed_caps)
548 {
549 gst_caps_unref(v4l2object->probed_caps);
550 }
551
552 if (v4l2object->extra_controls)
553 {
554 gst_structure_free(v4l2object->extra_controls);
555 }
556
557 gst_poll_free(v4l2object->poll);
558
559 g_free(v4l2object->dumpframefile);
560
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800561 /* jxsdbg resolution switching */
562 if (v4l2object->old_other_pool)
563 {
564 gst_object_unref(v4l2object->old_other_pool);
565 v4l2object->old_other_pool = NULL;
566 }
567 if (v4l2object->old_old_other_pool)
568 {
569 gst_object_unref(v4l2object->old_old_other_pool);
570 v4l2object->old_old_other_pool = NULL;
571 }
572 v4l2object->outstanding_buf_num = 0;
573
xuesong.jiangae1548e2022-05-06 16:38:46 +0800574 g_free(v4l2object);
575}
576
577static gboolean
578gst_aml_v4l2_object_clear_format_list(GstAmlV4l2Object *v4l2object)
579{
580 g_slist_foreach(v4l2object->formats, (GFunc)g_free, NULL);
581 g_slist_free(v4l2object->formats);
582 v4l2object->formats = NULL;
583
584 return TRUE;
585}
586
587static gint
588gst_aml_v4l2_object_prop_to_cid(guint prop_id)
589{
590 gint cid = -1;
591
592 switch (prop_id)
593 {
594 case PROP_BRIGHTNESS:
595 cid = V4L2_CID_BRIGHTNESS;
596 break;
597 case PROP_CONTRAST:
598 cid = V4L2_CID_CONTRAST;
599 break;
600 case PROP_SATURATION:
601 cid = V4L2_CID_SATURATION;
602 break;
603 case PROP_HUE:
604 cid = V4L2_CID_HUE;
605 break;
606 default:
607 GST_WARNING("unmapped property id: %d", prop_id);
608 }
609 return cid;
610}
611
612gboolean
613gst_aml_v4l2_object_set_property_helper(GstAmlV4l2Object *v4l2object,
614 guint prop_id, const GValue *value, GParamSpec *pspec)
615{
616 switch (prop_id)
617 {
618 case PROP_DEVICE:
619 g_free(v4l2object->videodev);
620 v4l2object->videodev = g_value_dup_string(value);
621 break;
622 case PROP_BRIGHTNESS:
623 case PROP_CONTRAST:
624 case PROP_SATURATION:
625 case PROP_HUE:
626 {
627 gint cid = gst_aml_v4l2_object_prop_to_cid(prop_id);
628
629 if (cid != -1)
630 {
631 if (GST_AML_V4L2_IS_OPEN(v4l2object))
632 {
633 gst_aml_v4l2_set_attribute(v4l2object, cid, g_value_get_int(value));
634 }
635 }
636 return TRUE;
637 }
638 break;
639 case PROP_IO_MODE:
640 v4l2object->req_mode = g_value_get_enum(value);
641 break;
642 case PROP_CAPTURE_IO_MODE:
643 g_return_val_if_fail(!V4L2_TYPE_IS_OUTPUT(v4l2object->type), FALSE);
644 v4l2object->req_mode = g_value_get_enum(value);
645 break;
646 case PROP_OUTPUT_IO_MODE:
647 g_return_val_if_fail(V4L2_TYPE_IS_OUTPUT(v4l2object->type), FALSE);
648 v4l2object->req_mode = g_value_get_enum(value);
649 break;
650 case PROP_EXTRA_CONTROLS:
651 {
652 const GstStructure *s = gst_value_get_structure(value);
653
654 if (v4l2object->extra_controls)
655 gst_structure_free(v4l2object->extra_controls);
656
657 v4l2object->extra_controls = s ? gst_structure_copy(s) : NULL;
658 if (GST_AML_V4L2_IS_OPEN(v4l2object))
659 gst_aml_v4l2_set_controls(v4l2object, v4l2object->extra_controls);
660 break;
661 }
662 case PROP_PIXEL_ASPECT_RATIO:
663 if (v4l2object->par)
664 {
665 g_value_unset(v4l2object->par);
666 g_free(v4l2object->par);
667 }
668 v4l2object->par = g_new0(GValue, 1);
669 g_value_init(v4l2object->par, GST_TYPE_FRACTION);
670 if (!g_value_transform(value, v4l2object->par))
671 {
672 g_warning("Could not transform string to aspect ratio");
673 gst_value_set_fraction(v4l2object->par, 1, 1);
674 }
675
sheng.liu641aa422023-12-26 07:05:59 +0000676 v4l2object->have_set_par = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800677 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set PAR to %d/%d",
678 gst_value_get_fraction_numerator(v4l2object->par),
679 gst_value_get_fraction_denominator(v4l2object->par));
680 break;
681 case PROP_FORCE_ASPECT_RATIO:
682 v4l2object->keep_aspect = g_value_get_boolean(value);
683 break;
684 case PROP_DUMP_FRAME_LOCATION:
685 g_free(v4l2object->dumpframefile);
686 v4l2object->dumpframefile = g_value_dup_string(value);
687 break;
xuesong.jiang22a9b112023-05-24 09:01:59 +0000688 case PROP_STREAM_MODE:
689 v4l2object->stream_mode = g_value_get_boolean(value);
690 break;
hanghang.luoc54208e2023-09-22 02:43:54 +0000691 case PROP_LOW_LATENCY_MODE:
692 v4l2object->low_latency_mode = g_value_get_boolean(value);
693 GST_DEBUG_OBJECT(v4l2object, "set low latency: %d",v4l2object->low_latency_mode);
694 break;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800695 default:
696 return FALSE;
697 break;
698 }
699 return TRUE;
700}
701
702gboolean
703gst_aml_v4l2_object_get_property_helper(GstAmlV4l2Object *v4l2object,
704 guint prop_id, GValue *value, GParamSpec *pspec)
705{
706 switch (prop_id)
707 {
708 case PROP_DEVICE:
709 g_value_set_string(value, v4l2object->videodev);
710 break;
711 case PROP_DEVICE_NAME:
712 {
713 const guchar *name = NULL;
714
715 if (GST_AML_V4L2_IS_OPEN(v4l2object))
716 name = v4l2object->vcap.card;
717
718 g_value_set_string(value, (gchar *)name);
719 break;
720 }
721 case PROP_DEVICE_FD:
722 {
723 if (GST_AML_V4L2_IS_OPEN(v4l2object))
724 g_value_set_int(value, v4l2object->video_fd);
725 else
726 g_value_set_int(value, DEFAULT_PROP_DEVICE_FD);
727 break;
728 }
729 case PROP_FLAGS:
730 {
731 guint flags = 0;
732
733 if (GST_AML_V4L2_IS_OPEN(v4l2object))
734 {
735 flags |= v4l2object->device_caps &
736 (V4L2_CAP_VIDEO_CAPTURE |
737 V4L2_CAP_VIDEO_OUTPUT |
738 V4L2_CAP_VIDEO_OVERLAY |
739 V4L2_CAP_VBI_CAPTURE |
740 V4L2_CAP_VBI_OUTPUT | V4L2_CAP_TUNER | V4L2_CAP_AUDIO);
741
742 if (v4l2object->device_caps & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
743 flags |= V4L2_CAP_VIDEO_CAPTURE;
744
745 if (v4l2object->device_caps & V4L2_CAP_VIDEO_OUTPUT_MPLANE)
746 flags |= V4L2_CAP_VIDEO_OUTPUT;
747 }
748 g_value_set_flags(value, flags);
749 break;
750 }
751 case PROP_BRIGHTNESS:
752 case PROP_CONTRAST:
753 case PROP_SATURATION:
754 case PROP_HUE:
755 {
756 gint cid = gst_aml_v4l2_object_prop_to_cid(prop_id);
757
758 if (cid != -1)
759 {
760 if (GST_AML_V4L2_IS_OPEN(v4l2object))
761 {
762 gint v;
763 if (gst_aml_v4l2_get_attribute(v4l2object, cid, &v))
764 {
765 g_value_set_int(value, v);
766 }
767 }
768 }
769 return TRUE;
770 }
771 break;
772 case PROP_IO_MODE:
773 g_value_set_enum(value, v4l2object->req_mode);
774 break;
775 case PROP_CAPTURE_IO_MODE:
776 g_return_val_if_fail(!V4L2_TYPE_IS_OUTPUT(v4l2object->type), FALSE);
777 g_value_set_enum(value, v4l2object->req_mode);
778 break;
779 case PROP_OUTPUT_IO_MODE:
780 g_return_val_if_fail(V4L2_TYPE_IS_OUTPUT(v4l2object->type), FALSE);
781 g_value_set_enum(value, v4l2object->req_mode);
782 break;
783 case PROP_EXTRA_CONTROLS:
784 gst_value_set_structure(value, v4l2object->extra_controls);
785 break;
786 case PROP_PIXEL_ASPECT_RATIO:
787 if (v4l2object->par)
788 g_value_transform(v4l2object->par, value);
789 break;
790 case PROP_FORCE_ASPECT_RATIO:
791 g_value_set_boolean(value, v4l2object->keep_aspect);
792 break;
793 case PROP_DUMP_FRAME_LOCATION:
794 g_value_set_string(value, v4l2object->dumpframefile);
795 break;
xuesong.jiang22a9b112023-05-24 09:01:59 +0000796 case PROP_STREAM_MODE:
797 g_value_set_boolean(value, v4l2object->stream_mode);
798 break;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800799 default:
800 return FALSE;
801 break;
802 }
803 return TRUE;
804}
805
806static void
807gst_aml_v4l2_get_driver_min_buffers(GstAmlV4l2Object *v4l2object)
808{
809 struct v4l2_control control = {
810 0,
811 };
812
813 g_return_if_fail(GST_AML_V4L2_IS_OPEN(v4l2object));
814
815 if (V4L2_TYPE_IS_OUTPUT(v4l2object->type))
816 control.id = V4L2_CID_MIN_BUFFERS_FOR_OUTPUT;
817 else
818 control.id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE;
819
820 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_CTRL, &control) == 0)
821 {
822 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
823 "driver requires a minimum of %d buffers", control.value);
824 v4l2object->min_buffers = control.value;
825 }
826 else
827 {
828 v4l2object->min_buffers = 0;
829 }
830}
831
832gboolean
833gst_aml_v4l2_object_open(GstAmlV4l2Object *v4l2object)
834{
835 if (!gst_aml_v4l2_open(v4l2object))
836 return FALSE;
837
838 return TRUE;
839}
840
841gboolean
842gst_aml_v4l2_object_open_shared(GstAmlV4l2Object *v4l2object, GstAmlV4l2Object *other)
843{
844 gboolean ret;
845
846 ret = gst_aml_v4l2_dup(v4l2object, other);
847
848 if (ret && !V4L2_TYPE_IS_OUTPUT(v4l2object->type))
849 {
850 gst_poll_fd_init(&v4l2object->pollfd);
851 v4l2object->pollfd.fd = v4l2object->video_fd;
852 gst_poll_add_fd(v4l2object->poll, &v4l2object->pollfd);
853 /* used for dequeue event */
854 gst_poll_fd_ctl_read(v4l2object->poll, &v4l2object->pollfd, TRUE);
855 gst_poll_fd_ctl_pri(v4l2object->poll, &v4l2object->pollfd, TRUE);
856 }
857
858 return ret;
859}
860
861gboolean
862gst_aml_v4l2_object_close(GstAmlV4l2Object *v4l2object)
863{
864 if (!gst_aml_v4l2_close(v4l2object))
865 return FALSE;
866
867 gst_caps_replace(&v4l2object->probed_caps, NULL);
868
869 /* reset our copy of the device caps */
870 v4l2object->device_caps = 0;
871
872 if (v4l2object->formats)
873 {
874 gst_aml_v4l2_object_clear_format_list(v4l2object);
875 }
876
877 if (v4l2object->par)
878 {
879 g_value_unset(v4l2object->par);
880 g_free(v4l2object->par);
881 v4l2object->par = NULL;
882 }
883
sheng.liudb26f7d2024-01-22 11:24:23 +0000884 if (v4l2object->fps)
885 {
886 g_value_unset(v4l2object->fps);
887 g_free(v4l2object->fps);
888 v4l2object->fps = NULL;
889 }
890
xuesong.jiangae1548e2022-05-06 16:38:46 +0800891 if (v4l2object->channel)
892 {
893 g_free(v4l2object->channel);
894 v4l2object->channel = NULL;
895 }
896
897 return TRUE;
898}
899
900static struct v4l2_fmtdesc *
901gst_aml_v4l2_object_get_format_from_fourcc(GstAmlV4l2Object *v4l2object,
902 guint32 fourcc)
903{
904 struct v4l2_fmtdesc *fmt;
905 GSList *walk;
906
907 if (fourcc == 0)
908 return NULL;
909
910 walk = gst_aml_v4l2_object_get_format_list(v4l2object);
911 while (walk)
912 {
913 fmt = (struct v4l2_fmtdesc *)walk->data;
914 if (fmt->pixelformat == fourcc)
915 return fmt;
916 /* special case for jpeg */
917 if (fmt->pixelformat == V4L2_PIX_FMT_MJPEG ||
918 fmt->pixelformat == V4L2_PIX_FMT_JPEG ||
919 fmt->pixelformat == V4L2_PIX_FMT_PJPG)
920 {
921 if (fourcc == V4L2_PIX_FMT_JPEG || fourcc == V4L2_PIX_FMT_MJPEG ||
922 fourcc == V4L2_PIX_FMT_PJPG)
923 {
924 return fmt;
925 }
926 }
927 walk = g_slist_next(walk);
928 }
929
930 return NULL;
931}
932
933/* complete made up ranking, the values themselves are meaningless */
934/* These ranks MUST be X such that X<<15 fits on a signed int - see
935 the comment at the end of gst_aml_v4l2_object_format_get_rank. */
936#define YUV_BASE_RANK 1000
937#define JPEG_BASE_RANK 500
938#define DV_BASE_RANK 200
939#define RGB_BASE_RANK 100
940#define YUV_ODD_BASE_RANK 50
941#define RGB_ODD_BASE_RANK 25
942#define BAYER_BASE_RANK 15
943#define S910_BASE_RANK 10
944#define GREY_BASE_RANK 5
945#define PWC_BASE_RANK 1
946
947static gint
948gst_aml_v4l2_object_format_get_rank(const struct v4l2_fmtdesc *fmt)
949{
950 guint32 fourcc = fmt->pixelformat;
951 gboolean emulated = ((fmt->flags & V4L2_FMT_FLAG_EMULATED) != 0);
952 gint rank = 0;
953
954 switch (fourcc)
955 {
956 case V4L2_PIX_FMT_MJPEG:
957 case V4L2_PIX_FMT_PJPG:
958 rank = JPEG_BASE_RANK;
959 break;
960 case V4L2_PIX_FMT_JPEG:
961 rank = JPEG_BASE_RANK + 1;
962 break;
963 case V4L2_PIX_FMT_MPEG: /* MPEG */
964 rank = JPEG_BASE_RANK + 2;
965 break;
966
967 case V4L2_PIX_FMT_RGB332:
968 case V4L2_PIX_FMT_ARGB555:
969 case V4L2_PIX_FMT_XRGB555:
970 case V4L2_PIX_FMT_RGB555:
971 case V4L2_PIX_FMT_ARGB555X:
972 case V4L2_PIX_FMT_XRGB555X:
973 case V4L2_PIX_FMT_RGB555X:
974 case V4L2_PIX_FMT_BGR666:
975 case V4L2_PIX_FMT_RGB565:
976 case V4L2_PIX_FMT_RGB565X:
977 case V4L2_PIX_FMT_RGB444:
978 case V4L2_PIX_FMT_Y4:
979 case V4L2_PIX_FMT_Y6:
980 case V4L2_PIX_FMT_Y10:
981 case V4L2_PIX_FMT_Y12:
982 case V4L2_PIX_FMT_Y10BPACK:
983 case V4L2_PIX_FMT_YUV555:
984 case V4L2_PIX_FMT_YUV565:
985 case V4L2_PIX_FMT_YUV32:
986 case V4L2_PIX_FMT_NV12MT_16X16:
987 case V4L2_PIX_FMT_NV42:
988 case V4L2_PIX_FMT_H264_MVC:
989 rank = RGB_ODD_BASE_RANK;
990 break;
991
992 case V4L2_PIX_FMT_RGB24:
993 case V4L2_PIX_FMT_BGR24:
994 rank = RGB_BASE_RANK - 1;
995 break;
996
997 case V4L2_PIX_FMT_RGB32:
998 case V4L2_PIX_FMT_BGR32:
999 case V4L2_PIX_FMT_ABGR32:
1000 case V4L2_PIX_FMT_XBGR32:
1001 case V4L2_PIX_FMT_ARGB32:
1002 case V4L2_PIX_FMT_XRGB32:
1003 rank = RGB_BASE_RANK;
1004 break;
1005
1006 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1007 rank = GREY_BASE_RANK;
1008 break;
1009
1010 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1011 case V4L2_PIX_FMT_NV12M: /* Same as NV12 */
1012 case V4L2_PIX_FMT_NV12MT: /* NV12 64x32 tile */
1013 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1014 case V4L2_PIX_FMT_NV21M: /* Same as NV21 */
1015 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1016 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1017 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1018 case V4L2_PIX_FMT_NV16M: /* Same as NV16 */
1019 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1020 case V4L2_PIX_FMT_NV61M: /* Same as NV61 */
1021 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1022 rank = YUV_ODD_BASE_RANK;
1023 break;
1024
1025 case V4L2_PIX_FMT_YVU410: /* YVU9, 9 bits per pixel */
1026 rank = YUV_BASE_RANK + 3;
1027 break;
1028 case V4L2_PIX_FMT_YUV410: /* YUV9, 9 bits per pixel */
1029 rank = YUV_BASE_RANK + 2;
1030 break;
1031 case V4L2_PIX_FMT_YUV420: /* I420, 12 bits per pixel */
1032 case V4L2_PIX_FMT_YUV420M:
1033 rank = YUV_BASE_RANK + 7;
1034 break;
1035 case V4L2_PIX_FMT_YUYV: /* YUY2, 16 bits per pixel */
1036 rank = YUV_BASE_RANK + 10;
1037 break;
1038 case V4L2_PIX_FMT_YVU420: /* YV12, 12 bits per pixel */
1039 rank = YUV_BASE_RANK + 6;
1040 break;
1041 case V4L2_PIX_FMT_UYVY: /* UYVY, 16 bits per pixel */
1042 rank = YUV_BASE_RANK + 9;
1043 break;
1044 case V4L2_PIX_FMT_YUV444:
1045 rank = YUV_BASE_RANK + 6;
1046 break;
1047 case V4L2_PIX_FMT_Y41P: /* Y41P, 12 bits per pixel */
1048 rank = YUV_BASE_RANK + 5;
1049 break;
1050 case V4L2_PIX_FMT_YUV411P: /* Y41B, 12 bits per pixel */
1051 rank = YUV_BASE_RANK + 4;
1052 break;
1053 case V4L2_PIX_FMT_YUV422P: /* Y42B, 16 bits per pixel */
1054 rank = YUV_BASE_RANK + 8;
1055 break;
1056
1057 case V4L2_PIX_FMT_DV:
1058 rank = DV_BASE_RANK;
1059 break;
1060
1061 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1062 rank = 0;
1063 break;
1064
1065 case V4L2_PIX_FMT_SBGGR8:
1066 case V4L2_PIX_FMT_SGBRG8:
1067 case V4L2_PIX_FMT_SGRBG8:
1068 case V4L2_PIX_FMT_SRGGB8:
1069 rank = BAYER_BASE_RANK;
1070 break;
1071
1072 case V4L2_PIX_FMT_SN9C10X:
1073 rank = S910_BASE_RANK;
1074 break;
1075
1076 case V4L2_PIX_FMT_PWC1:
1077 rank = PWC_BASE_RANK;
1078 break;
1079 case V4L2_PIX_FMT_PWC2:
1080 rank = PWC_BASE_RANK;
1081 break;
1082
1083 default:
1084 rank = 0;
1085 break;
1086 }
1087
1088 /* All ranks are below 1<<15 so a shift by 15
1089 * will a) make all non-emulated formats larger
1090 * than emulated and b) will not overflow
1091 */
1092 if (!emulated)
1093 rank <<= 15;
1094
1095 return rank;
1096}
1097
1098static gint
1099format_cmp_func(gconstpointer a, gconstpointer b)
1100{
1101 const struct v4l2_fmtdesc *fa = a;
1102 const struct v4l2_fmtdesc *fb = b;
1103
1104 if (fa->pixelformat == fb->pixelformat)
1105 return 0;
1106
1107 return gst_aml_v4l2_object_format_get_rank(fb) -
1108 gst_aml_v4l2_object_format_get_rank(fa);
1109}
1110
1111/******************************************************
1112 * gst_aml_v4l2_object_fill_format_list():
1113 * create list of supported capture formats
1114 * return value: TRUE on success, FALSE on error
1115 ******************************************************/
1116static gboolean
1117gst_aml_v4l2_object_fill_format_list(GstAmlV4l2Object *v4l2object,
1118 enum v4l2_buf_type type)
1119{
1120 gint n;
1121 struct v4l2_fmtdesc *format;
1122
1123 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "getting src format enumerations");
1124
1125 /* format enumeration */
1126 for (n = 0;; n++)
1127 {
1128 format = g_new0(struct v4l2_fmtdesc, 1);
1129
1130 format->index = n;
1131 format->type = type;
1132
1133 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_ENUM_FMT, format) < 0)
1134 {
1135 if (errno == EINVAL)
1136 {
1137 g_free(format);
1138 break; /* end of enumeration */
1139 }
1140 else
1141 {
1142 goto failed;
1143 }
1144 }
1145
1146 GST_LOG_OBJECT(v4l2object->dbg_obj, "index: %u", format->index);
1147 GST_LOG_OBJECT(v4l2object->dbg_obj, "type: %d", format->type);
1148 GST_LOG_OBJECT(v4l2object->dbg_obj, "flags: %08x", format->flags);
1149 GST_LOG_OBJECT(v4l2object->dbg_obj, "description: '%s'",
1150 format->description);
1151 GST_LOG_OBJECT(v4l2object->dbg_obj, "pixelformat: %" GST_FOURCC_FORMAT,
1152 GST_FOURCC_ARGS(format->pixelformat));
1153
xuesong.jiang282ca572023-05-05 09:03:32 +00001154
1155 if (V4L2_PIX_FMT_YUV420M == format->pixelformat || V4L2_PIX_FMT_YUV420 == format->pixelformat)
1156 {
1157 GST_LOG_OBJECT(v4l2object->dbg_obj, "aml v4l2 driver didn't real support YU12 and YM12, ignore it");
1158 continue;
1159 }
1160
xuesong.jiangae1548e2022-05-06 16:38:46 +08001161 /* sort formats according to our preference; we do this, because caps
1162 * are probed in the order the formats are in the list, and the order of
1163 * formats in the final probed caps matters for things like fixation */
1164 v4l2object->formats = g_slist_insert_sorted(v4l2object->formats, format,
1165 (GCompareFunc)format_cmp_func);
1166 }
1167
1168#ifndef GST_DISABLE_GST_DEBUG
1169 {
1170 GSList *l;
1171
1172 GST_INFO_OBJECT(v4l2object->dbg_obj, "got %d format(s):", n);
1173 for (l = v4l2object->formats; l != NULL; l = l->next)
1174 {
1175 format = l->data;
1176
1177 GST_INFO_OBJECT(v4l2object->dbg_obj,
1178 " %" GST_FOURCC_FORMAT "%s", GST_FOURCC_ARGS(format->pixelformat),
1179 ((format->flags & V4L2_FMT_FLAG_EMULATED)) ? " (emulated)" : "");
1180 }
1181 }
1182#endif
1183
1184 return TRUE;
1185
1186 /* ERRORS */
1187failed:
1188{
1189 g_free(format);
1190
1191 if (v4l2object->element)
1192 return FALSE;
1193
1194 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
1195 (_("Failed to enumerate possible video formats device '%s' can work "
1196 "with"),
1197 v4l2object->videodev),
1198 ("Failed to get number %d in pixelformat enumeration for %s. (%d - %s)",
1199 n, v4l2object->videodev, errno, g_strerror(errno)));
1200
1201 return FALSE;
1202}
1203}
1204
1205/*
1206 * Get the list of supported capture formats, a list of
1207 * <code>struct v4l2_fmtdesc</code>.
1208 */
1209static GSList *
1210gst_aml_v4l2_object_get_format_list(GstAmlV4l2Object *v4l2object)
1211{
1212 if (!v4l2object->formats)
1213 {
1214
1215 /* check usual way */
1216 gst_aml_v4l2_object_fill_format_list(v4l2object, v4l2object->type);
1217
1218 /* if our driver supports multi-planar
1219 * and if formats are still empty then we can workaround driver bug
1220 * by also looking up formats as if our device was not supporting
1221 * multiplanar */
1222 if (!v4l2object->formats)
1223 {
1224 switch (v4l2object->type)
1225 {
1226 case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1227 gst_aml_v4l2_object_fill_format_list(v4l2object,
1228 V4L2_BUF_TYPE_VIDEO_CAPTURE);
1229 break;
1230
1231 case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1232 gst_aml_v4l2_object_fill_format_list(v4l2object,
1233 V4L2_BUF_TYPE_VIDEO_OUTPUT);
1234 break;
1235
1236 default:
1237 break;
1238 }
1239 }
1240 }
1241 return v4l2object->formats;
1242}
1243
1244static GstVideoFormat
1245gst_aml_v4l2_object_v4l2fourcc_to_video_format(guint32 fourcc)
1246{
1247 GstVideoFormat format;
1248
1249 switch (fourcc)
1250 {
1251 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1252 format = GST_VIDEO_FORMAT_GRAY8;
1253 break;
1254 case V4L2_PIX_FMT_Y16:
1255 format = GST_VIDEO_FORMAT_GRAY16_LE;
1256 break;
1257 case V4L2_PIX_FMT_Y16_BE:
1258 format = GST_VIDEO_FORMAT_GRAY16_BE;
1259 break;
1260 case V4L2_PIX_FMT_XRGB555:
1261 case V4L2_PIX_FMT_RGB555:
1262 format = GST_VIDEO_FORMAT_RGB15;
1263 break;
1264 case V4L2_PIX_FMT_XRGB555X:
1265 case V4L2_PIX_FMT_RGB555X:
1266 format = GST_VIDEO_FORMAT_BGR15;
1267 break;
1268 case V4L2_PIX_FMT_RGB565:
1269 format = GST_VIDEO_FORMAT_RGB16;
1270 break;
1271 case V4L2_PIX_FMT_RGB24:
1272 format = GST_VIDEO_FORMAT_RGB;
1273 break;
1274 case V4L2_PIX_FMT_BGR24:
1275 format = GST_VIDEO_FORMAT_BGR;
1276 break;
1277 case V4L2_PIX_FMT_XRGB32:
1278 case V4L2_PIX_FMT_RGB32:
1279 format = GST_VIDEO_FORMAT_xRGB;
1280 break;
1281 case V4L2_PIX_FMT_XBGR32:
1282 case V4L2_PIX_FMT_BGR32:
1283 format = GST_VIDEO_FORMAT_BGRx;
1284 break;
1285 case V4L2_PIX_FMT_ABGR32:
1286 format = GST_VIDEO_FORMAT_BGRA;
1287 break;
1288 case V4L2_PIX_FMT_ARGB32:
1289 format = GST_VIDEO_FORMAT_ARGB;
1290 break;
1291 case V4L2_PIX_FMT_NV12:
1292 case V4L2_PIX_FMT_NV12M:
1293 format = GST_VIDEO_FORMAT_NV12;
1294 break;
1295 case V4L2_PIX_FMT_NV12MT:
1296 format = GST_VIDEO_FORMAT_NV12_64Z32;
1297 break;
1298 case V4L2_PIX_FMT_NV21:
1299 case V4L2_PIX_FMT_NV21M:
1300 format = GST_VIDEO_FORMAT_NV21;
1301 break;
1302 case V4L2_PIX_FMT_YVU410:
1303 format = GST_VIDEO_FORMAT_YVU9;
1304 break;
1305 case V4L2_PIX_FMT_YUV410:
1306 format = GST_VIDEO_FORMAT_YUV9;
1307 break;
1308 case V4L2_PIX_FMT_YUV420:
1309 case V4L2_PIX_FMT_YUV420M:
1310 format = GST_VIDEO_FORMAT_I420;
1311 break;
1312 case V4L2_PIX_FMT_YUYV:
1313 format = GST_VIDEO_FORMAT_YUY2;
1314 break;
1315 case V4L2_PIX_FMT_YVU420:
1316 format = GST_VIDEO_FORMAT_YV12;
1317 break;
1318 case V4L2_PIX_FMT_UYVY:
1319 format = GST_VIDEO_FORMAT_UYVY;
1320 break;
1321 case V4L2_PIX_FMT_YUV411P:
1322 format = GST_VIDEO_FORMAT_Y41B;
1323 break;
1324 case V4L2_PIX_FMT_YUV422P:
1325 format = GST_VIDEO_FORMAT_Y42B;
1326 break;
1327 case V4L2_PIX_FMT_YVYU:
1328 format = GST_VIDEO_FORMAT_YVYU;
1329 break;
1330 case V4L2_PIX_FMT_NV16:
1331 case V4L2_PIX_FMT_NV16M:
1332 format = GST_VIDEO_FORMAT_NV16;
1333 break;
1334 case V4L2_PIX_FMT_NV61:
1335 case V4L2_PIX_FMT_NV61M:
1336 format = GST_VIDEO_FORMAT_NV61;
1337 break;
1338 case V4L2_PIX_FMT_NV24:
1339 format = GST_VIDEO_FORMAT_NV24;
1340 break;
1341 default:
1342 format = GST_VIDEO_FORMAT_UNKNOWN;
1343 break;
1344 }
1345
1346 return format;
1347}
1348
1349static gboolean
1350gst_amL_v4l2_object_v4l2fourcc_is_rgb(guint32 fourcc)
1351{
1352 gboolean ret = FALSE;
1353
1354 switch (fourcc)
1355 {
1356 case V4L2_PIX_FMT_XRGB555:
1357 case V4L2_PIX_FMT_RGB555:
1358 case V4L2_PIX_FMT_XRGB555X:
1359 case V4L2_PIX_FMT_RGB555X:
1360 case V4L2_PIX_FMT_RGB565:
1361 case V4L2_PIX_FMT_RGB24:
1362 case V4L2_PIX_FMT_BGR24:
1363 case V4L2_PIX_FMT_XRGB32:
1364 case V4L2_PIX_FMT_RGB32:
1365 case V4L2_PIX_FMT_XBGR32:
1366 case V4L2_PIX_FMT_BGR32:
1367 case V4L2_PIX_FMT_ABGR32:
1368 case V4L2_PIX_FMT_ARGB32:
1369 case V4L2_PIX_FMT_SBGGR8:
1370 case V4L2_PIX_FMT_SGBRG8:
1371 case V4L2_PIX_FMT_SGRBG8:
1372 case V4L2_PIX_FMT_SRGGB8:
1373 ret = TRUE;
1374 break;
1375 default:
1376 break;
1377 }
1378
1379 return ret;
1380}
1381
1382static GstStructure *
1383gst_aml_v4l2_object_v4l2fourcc_to_bare_struct(guint32 fourcc)
1384{
1385 GstStructure *structure = NULL;
1386
1387 switch (fourcc)
1388 {
1389 case V4L2_PIX_FMT_MJPEG: /* Motion-JPEG */
1390 case V4L2_PIX_FMT_PJPG: /* Progressive-JPEG */
1391 case V4L2_PIX_FMT_JPEG: /* JFIF JPEG */
1392 structure = gst_structure_new_empty("image/jpeg");
1393 break;
1394 case V4L2_PIX_FMT_MPEG1:
1395 structure = gst_structure_new("video/mpeg",
1396 "mpegversion", G_TYPE_INT, 1, NULL);
fei.dengb5bfaa82022-07-12 17:27:13 +08001397 gst_structure_set(structure, "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1398 GST_DEBUG("aml set mpeg1 systemstream to false");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001399 break;
1400 case V4L2_PIX_FMT_MPEG2:
1401 structure = gst_structure_new("video/mpeg",
1402 "mpegversion", G_TYPE_INT, 2, NULL);
1403 gst_structure_set(structure, "systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
1404 GST_DEBUG("aml set mpeg2 systemstream to false");
1405 break;
1406 case V4L2_PIX_FMT_MPEG4:
1407 case V4L2_PIX_FMT_XVID:
1408 structure = gst_structure_new("video/mpeg",
1409 "mpegversion", G_TYPE_INT, 4, "systemstream",
1410 G_TYPE_BOOLEAN, FALSE, NULL);
1411 break;
1412 case V4L2_PIX_FMT_FWHT:
1413 structure = gst_structure_new_empty("video/x-fwht");
1414 break;
1415 case V4L2_PIX_FMT_H263:
1416 structure = gst_structure_new("video/x-h263",
1417 "variant", G_TYPE_STRING, "itu", NULL);
1418 break;
1419 case V4L2_PIX_FMT_H264: /* H.264 */
1420 structure = gst_structure_new("video/x-h264",
1421 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1422 G_TYPE_STRING, "au", NULL);
1423 break;
1424 case V4L2_PIX_FMT_H264_NO_SC:
1425 structure = gst_structure_new("video/x-h264",
1426 "stream-format", G_TYPE_STRING, "avc", "alignment",
1427 G_TYPE_STRING, "au", NULL);
1428 break;
1429 case V4L2_PIX_FMT_HEVC: /* H.265 */
1430 structure = gst_structure_new("video/x-h265",
1431 "stream-format", G_TYPE_STRING, "byte-stream", "alignment",
1432 G_TYPE_STRING, "au", NULL);
1433 break;
1434 case V4L2_PIX_FMT_VC1_ANNEX_G:
1435 case V4L2_PIX_FMT_VC1_ANNEX_L:
1436 structure = gst_structure_new("video/x-wmv",
1437 "wmvversion", G_TYPE_INT, 3, "format", G_TYPE_STRING, "WVC1", NULL);
1438 break;
1439 case V4L2_PIX_FMT_VP8:
1440 structure = gst_structure_new_empty("video/x-vp8");
1441 break;
1442 case V4L2_PIX_FMT_VP9:
1443 structure = gst_structure_new_empty("video/x-vp9");
1444 break;
1445 case V4L2_PIX_FMT_AV1:
1446 structure = gst_structure_new_empty("video/x-av1");
1447 break;
zengliang.li51f54b62023-10-10 12:14:49 +00001448 case V4L2_PIX_FMT_AVS:
1449 structure = gst_structure_new_empty("video/x-avs");
1450 break;
1451 case V4L2_PIX_FMT_AVS2:
1452 structure = gst_structure_new_empty("video/x-avs2");
1453 break;
1454 case V4L2_PIX_FMT_AVS3:
1455 structure = gst_structure_new_empty("video/x-avs3");
1456 break;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001457 case V4L2_PIX_FMT_GREY: /* 8 Greyscale */
1458 case V4L2_PIX_FMT_Y16:
1459 case V4L2_PIX_FMT_Y16_BE:
1460 case V4L2_PIX_FMT_XRGB555:
1461 case V4L2_PIX_FMT_RGB555:
1462 case V4L2_PIX_FMT_XRGB555X:
1463 case V4L2_PIX_FMT_RGB555X:
1464 case V4L2_PIX_FMT_RGB565:
1465 case V4L2_PIX_FMT_RGB24:
1466 case V4L2_PIX_FMT_BGR24:
1467 case V4L2_PIX_FMT_RGB32:
1468 case V4L2_PIX_FMT_XRGB32:
1469 case V4L2_PIX_FMT_ARGB32:
1470 case V4L2_PIX_FMT_BGR32:
1471 case V4L2_PIX_FMT_XBGR32:
1472 case V4L2_PIX_FMT_ABGR32:
1473 case V4L2_PIX_FMT_NV12: /* 12 Y/CbCr 4:2:0 */
1474 case V4L2_PIX_FMT_NV12M:
1475 case V4L2_PIX_FMT_NV12MT:
1476 case V4L2_PIX_FMT_NV21: /* 12 Y/CrCb 4:2:0 */
1477 case V4L2_PIX_FMT_NV21M:
1478 case V4L2_PIX_FMT_NV16: /* 16 Y/CbCr 4:2:2 */
1479 case V4L2_PIX_FMT_NV16M:
1480 case V4L2_PIX_FMT_NV61: /* 16 Y/CrCb 4:2:2 */
1481 case V4L2_PIX_FMT_NV61M:
1482 case V4L2_PIX_FMT_NV24: /* 24 Y/CrCb 4:4:4 */
1483 case V4L2_PIX_FMT_YVU410:
1484 case V4L2_PIX_FMT_YUV410:
1485 case V4L2_PIX_FMT_YUV420: /* I420/IYUV */
1486 case V4L2_PIX_FMT_YUV420M:
1487 case V4L2_PIX_FMT_YUYV:
1488 case V4L2_PIX_FMT_YVU420:
1489 case V4L2_PIX_FMT_UYVY:
1490 case V4L2_PIX_FMT_YUV422P:
1491 case V4L2_PIX_FMT_YVYU:
1492 case V4L2_PIX_FMT_YUV411P:
1493 {
1494 GstVideoFormat format;
1495 format = gst_aml_v4l2_object_v4l2fourcc_to_video_format(fourcc);
1496 if (format != GST_VIDEO_FORMAT_UNKNOWN)
1497 structure = gst_structure_new("video/x-raw",
1498 "format", G_TYPE_STRING, gst_video_format_to_string(format), NULL);
1499 break;
1500 }
1501 case V4L2_PIX_FMT_DV:
1502 structure =
1503 gst_structure_new("video/x-dv", "systemstream", G_TYPE_BOOLEAN, TRUE,
1504 NULL);
1505 break;
1506 case V4L2_PIX_FMT_MPEG: /* MPEG */
1507 structure = gst_structure_new("video/mpegts",
1508 "systemstream", G_TYPE_BOOLEAN, TRUE, NULL);
1509 break;
1510 case V4L2_PIX_FMT_WNVA: /* Winnov hw compres */
1511 break;
1512 case V4L2_PIX_FMT_SBGGR8:
1513 case V4L2_PIX_FMT_SGBRG8:
1514 case V4L2_PIX_FMT_SGRBG8:
1515 case V4L2_PIX_FMT_SRGGB8:
1516 structure = gst_structure_new("video/x-bayer", "format", G_TYPE_STRING,
1517 fourcc == V4L2_PIX_FMT_SBGGR8 ? "bggr" : fourcc == V4L2_PIX_FMT_SGBRG8 ? "gbrg"
1518 : fourcc == V4L2_PIX_FMT_SGRBG8 ? "grbg"
1519 :
1520 /* fourcc == V4L2_PIX_FMT_SRGGB8 ? */ "rggb",
1521 NULL);
1522 break;
1523 case V4L2_PIX_FMT_SN9C10X:
1524 structure = gst_structure_new_empty("video/x-sonix");
1525 break;
1526 case V4L2_PIX_FMT_PWC1:
1527 structure = gst_structure_new_empty("video/x-pwc1");
1528 break;
1529 case V4L2_PIX_FMT_PWC2:
1530 structure = gst_structure_new_empty("video/x-pwc2");
1531 break;
1532 case V4L2_PIX_FMT_RGB332:
1533 case V4L2_PIX_FMT_BGR666:
1534 case V4L2_PIX_FMT_ARGB555X:
1535 case V4L2_PIX_FMT_RGB565X:
1536 case V4L2_PIX_FMT_RGB444:
1537 case V4L2_PIX_FMT_YYUV: /* 16 YUV 4:2:2 */
1538 case V4L2_PIX_FMT_HI240: /* 8 8-bit color */
1539 case V4L2_PIX_FMT_Y4:
1540 case V4L2_PIX_FMT_Y6:
1541 case V4L2_PIX_FMT_Y10:
1542 case V4L2_PIX_FMT_Y12:
1543 case V4L2_PIX_FMT_Y10BPACK:
1544 case V4L2_PIX_FMT_YUV444:
1545 case V4L2_PIX_FMT_YUV555:
1546 case V4L2_PIX_FMT_YUV565:
1547 case V4L2_PIX_FMT_Y41P:
1548 case V4L2_PIX_FMT_YUV32:
1549 case V4L2_PIX_FMT_NV12MT_16X16:
1550 case V4L2_PIX_FMT_NV42:
1551 case V4L2_PIX_FMT_H264_MVC:
1552 default:
1553 GST_DEBUG("Unsupported fourcc 0x%08x %" GST_FOURCC_FORMAT,
1554 fourcc, GST_FOURCC_ARGS(fourcc));
1555 break;
1556 }
1557
1558 return structure;
1559}
1560
1561GstStructure *
1562gst_aml_v4l2_object_v4l2fourcc_to_structure(guint32 fourcc)
1563{
1564 GstStructure *template;
1565 gint i;
1566
1567 template = gst_aml_v4l2_object_v4l2fourcc_to_bare_struct(fourcc);
1568
1569 if (template == NULL)
1570 goto done;
1571
1572 for (i = 0; i < GST_AML_V4L2_FORMAT_COUNT; i++)
1573 {
1574 if (gst_aml_v4l2_formats[i].format != fourcc)
1575 continue;
1576
1577 if (gst_aml_v4l2_formats[i].dimensions)
1578 {
1579 gst_structure_set(template,
1580 "width", GST_TYPE_INT_RANGE, 1, GST_AML_V4L2_MAX_SIZE,
1581 "height", GST_TYPE_INT_RANGE, 1, GST_AML_V4L2_MAX_SIZE,
1582 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1583 }
1584 break;
1585 }
1586
1587done:
1588 return template;
1589}
1590
1591static GstCaps *
1592gst_aml_v4l2_object_get_caps_helper(GstAmlV4L2FormatFlags flags)
1593{
1594 GstStructure *structure;
1595 GstCaps *caps;
1596 guint i;
1597
1598 caps = gst_caps_new_empty();
1599 for (i = 0; i < GST_AML_V4L2_FORMAT_COUNT; i++)
1600 {
1601
1602 if ((gst_aml_v4l2_formats[i].flags & flags) == 0)
1603 continue;
1604
1605 structure =
1606 gst_aml_v4l2_object_v4l2fourcc_to_bare_struct(gst_aml_v4l2_formats[i].format);
1607
1608 if (structure)
1609 {
1610 GstStructure *alt_s = NULL;
1611
1612 if (gst_aml_v4l2_formats[i].dimensions)
1613 {
1614 gst_structure_set(structure,
1615 "width", GST_TYPE_INT_RANGE, 1, GST_AML_V4L2_MAX_SIZE,
1616 "height", GST_TYPE_INT_RANGE, 1, GST_AML_V4L2_MAX_SIZE,
1617 "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT, 1, NULL);
1618 }
1619
1620 switch (gst_aml_v4l2_formats[i].format)
1621 {
1622 case V4L2_PIX_FMT_RGB32:
1623 alt_s = gst_structure_copy(structure);
1624 gst_structure_set(alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
1625 break;
1626 case V4L2_PIX_FMT_BGR32:
1627 alt_s = gst_structure_copy(structure);
1628 gst_structure_set(alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
1629 default:
1630 break;
1631 }
1632
1633 gst_caps_append_structure(caps, structure);
1634
1635 if (alt_s)
1636 gst_caps_append_structure(caps, alt_s);
1637 }
1638 }
1639
1640 return gst_caps_simplify(caps);
1641}
1642
1643GstCaps *
1644gst_aml_v4l2_object_get_all_caps(void)
1645{
1646 static GstCaps *caps = NULL;
1647
1648 if (g_once_init_enter(&caps))
1649 {
1650 GstCaps *all_caps = gst_aml_v4l2_object_get_caps_helper(GST_V4L2_ALL);
1651 GST_MINI_OBJECT_FLAG_SET(all_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1652 g_once_init_leave(&caps, all_caps);
1653 }
1654
1655 return caps;
1656}
1657
1658GstCaps *
1659gst_aml_v4l2_object_get_raw_caps(void)
1660{
1661 static GstCaps *caps = NULL;
1662
1663 if (g_once_init_enter(&caps))
1664 {
1665 GstCaps *raw_caps = gst_aml_v4l2_object_get_caps_helper(GST_V4L2_RAW);
1666 GST_MINI_OBJECT_FLAG_SET(raw_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1667 g_once_init_leave(&caps, raw_caps);
1668 }
1669
1670 return caps;
1671}
1672
1673GstCaps *
1674gst_aml_v4l2_object_get_codec_caps(void)
1675{
1676 static GstCaps *caps = NULL;
1677
1678 if (g_once_init_enter(&caps))
1679 {
1680 GstCaps *codec_caps = gst_aml_v4l2_object_get_caps_helper(GST_V4L2_CODEC);
1681 GST_MINI_OBJECT_FLAG_SET(codec_caps, GST_MINI_OBJECT_FLAG_MAY_BE_LEAKED);
1682 g_once_init_leave(&caps, codec_caps);
1683 }
1684
1685 return caps;
1686}
1687
1688/* collect data for the given caps
1689 * @caps: given input caps
1690 * @format: location for the v4l format
1691 * @w/@h: location for width and height
1692 * @fps_n/@fps_d: location for framerate
1693 * @size: location for expected size of the frame or 0 if unknown
1694 */
1695static gboolean
1696gst_aml_v4l2_object_get_caps_info(GstAmlV4l2Object *v4l2object, GstCaps *caps,
1697 struct v4l2_fmtdesc **format, GstVideoInfo *info)
1698{
1699 GstStructure *structure;
1700 guint32 fourcc = 0, fourcc_nc = 0;
1701 const gchar *mimetype;
1702 struct v4l2_fmtdesc *fmt = NULL;
1703
fei.denge9458472023-04-18 02:05:48 +00001704 GST_DEBUG_OBJECT(v4l2object, "got caps: %" GST_PTR_FORMAT, caps);
1705
xuesong.jiangae1548e2022-05-06 16:38:46 +08001706 structure = gst_caps_get_structure(caps, 0);
1707
1708 mimetype = gst_structure_get_name(structure);
1709
1710 if (!gst_video_info_from_caps(info, caps))
1711 goto invalid_format;
1712
1713 if (g_str_equal(mimetype, "video/x-raw"))
1714 {
1715 switch (GST_VIDEO_INFO_FORMAT(info))
1716 {
1717 case GST_VIDEO_FORMAT_I420:
1718 fourcc = V4L2_PIX_FMT_YUV420;
1719 fourcc_nc = V4L2_PIX_FMT_YUV420M;
1720 break;
1721 case GST_VIDEO_FORMAT_YUY2:
1722 fourcc = V4L2_PIX_FMT_YUYV;
1723 break;
1724 case GST_VIDEO_FORMAT_UYVY:
1725 fourcc = V4L2_PIX_FMT_UYVY;
1726 break;
1727 case GST_VIDEO_FORMAT_YV12:
1728 fourcc = V4L2_PIX_FMT_YVU420;
1729 break;
1730 case GST_VIDEO_FORMAT_Y41B:
1731 fourcc = V4L2_PIX_FMT_YUV411P;
1732 break;
1733 case GST_VIDEO_FORMAT_Y42B:
1734 fourcc = V4L2_PIX_FMT_YUV422P;
1735 break;
1736 case GST_VIDEO_FORMAT_NV12:
1737 fourcc = V4L2_PIX_FMT_NV12;
1738 fourcc_nc = V4L2_PIX_FMT_NV12M;
1739 break;
1740 case GST_VIDEO_FORMAT_NV12_64Z32:
1741 fourcc_nc = V4L2_PIX_FMT_NV12MT;
1742 break;
1743 case GST_VIDEO_FORMAT_NV21:
1744 fourcc = V4L2_PIX_FMT_NV21;
1745 fourcc_nc = V4L2_PIX_FMT_NV21M;
1746 break;
1747 case GST_VIDEO_FORMAT_NV16:
1748 fourcc = V4L2_PIX_FMT_NV16;
1749 fourcc_nc = V4L2_PIX_FMT_NV16M;
1750 break;
1751 case GST_VIDEO_FORMAT_NV61:
1752 fourcc = V4L2_PIX_FMT_NV61;
1753 fourcc_nc = V4L2_PIX_FMT_NV61M;
1754 break;
1755 case GST_VIDEO_FORMAT_NV24:
1756 fourcc = V4L2_PIX_FMT_NV24;
1757 break;
1758 case GST_VIDEO_FORMAT_YVYU:
1759 fourcc = V4L2_PIX_FMT_YVYU;
1760 break;
1761 case GST_VIDEO_FORMAT_RGB15:
1762 fourcc = V4L2_PIX_FMT_RGB555;
1763 fourcc_nc = V4L2_PIX_FMT_XRGB555;
1764 break;
1765 case GST_VIDEO_FORMAT_RGB16:
1766 fourcc = V4L2_PIX_FMT_RGB565;
1767 break;
1768 case GST_VIDEO_FORMAT_RGB:
1769 fourcc = V4L2_PIX_FMT_RGB24;
1770 break;
1771 case GST_VIDEO_FORMAT_BGR:
1772 fourcc = V4L2_PIX_FMT_BGR24;
1773 break;
1774 case GST_VIDEO_FORMAT_xRGB:
1775 fourcc = V4L2_PIX_FMT_RGB32;
1776 fourcc_nc = V4L2_PIX_FMT_XRGB32;
1777 break;
1778 case GST_VIDEO_FORMAT_ARGB:
1779 fourcc = V4L2_PIX_FMT_RGB32;
1780 fourcc_nc = V4L2_PIX_FMT_ARGB32;
1781 break;
1782 case GST_VIDEO_FORMAT_BGRx:
1783 fourcc = V4L2_PIX_FMT_BGR32;
1784 fourcc_nc = V4L2_PIX_FMT_XBGR32;
1785 break;
1786 case GST_VIDEO_FORMAT_BGRA:
1787 fourcc = V4L2_PIX_FMT_BGR32;
1788 fourcc_nc = V4L2_PIX_FMT_ABGR32;
1789 break;
1790 case GST_VIDEO_FORMAT_GRAY8:
1791 fourcc = V4L2_PIX_FMT_GREY;
1792 break;
1793 case GST_VIDEO_FORMAT_GRAY16_LE:
1794 fourcc = V4L2_PIX_FMT_Y16;
1795 break;
1796 case GST_VIDEO_FORMAT_GRAY16_BE:
1797 fourcc = V4L2_PIX_FMT_Y16_BE;
1798 break;
1799 case GST_VIDEO_FORMAT_BGR15:
1800 fourcc = V4L2_PIX_FMT_RGB555X;
1801 fourcc_nc = V4L2_PIX_FMT_XRGB555X;
1802 break;
1803 default:
1804 break;
1805 }
1806 }
1807 else
1808 {
1809 if (g_str_equal(mimetype, "video/mpegts"))
1810 {
1811 fourcc = V4L2_PIX_FMT_MPEG;
1812 }
1813 else if (g_str_equal(mimetype, "video/x-dv"))
1814 {
1815 fourcc = V4L2_PIX_FMT_DV;
1816 }
1817 else if (g_str_equal(mimetype, "image/jpeg"))
1818 {
1819 fourcc = V4L2_PIX_FMT_JPEG;
1820 }
1821 else if (g_str_equal(mimetype, "video/mpeg"))
1822 {
1823 gint version;
1824 if (gst_structure_get_int(structure, "mpegversion", &version))
1825 {
1826 switch (version)
1827 {
1828 case 1:
1829 fourcc = V4L2_PIX_FMT_MPEG1;
1830 break;
1831 case 2:
1832 fourcc = V4L2_PIX_FMT_MPEG2;
1833 break;
1834 case 4:
1835 fourcc = V4L2_PIX_FMT_MPEG4;
1836 fourcc_nc = V4L2_PIX_FMT_XVID;
1837 break;
1838 default:
1839 break;
1840 }
1841 }
1842 }
1843 else if (g_str_equal(mimetype, "video/x-fwht"))
1844 {
1845 fourcc = V4L2_PIX_FMT_FWHT;
1846 }
1847 else if (g_str_equal(mimetype, "video/x-h263"))
1848 {
1849 fourcc = V4L2_PIX_FMT_H263;
1850 }
1851 else if (g_str_equal(mimetype, "video/x-h264"))
1852 {
1853 const gchar *stream_format =
1854 gst_structure_get_string(structure, "stream-format");
1855 if (g_str_equal(stream_format, "avc"))
1856 fourcc = V4L2_PIX_FMT_H264_NO_SC;
1857 else
1858 fourcc = V4L2_PIX_FMT_H264;
1859 }
1860 else if (g_str_equal(mimetype, "video/x-h265"))
1861 {
1862 fourcc = V4L2_PIX_FMT_HEVC;
1863 }
1864 else if (g_str_equal(mimetype, "video/x-vp8"))
1865 {
1866 fourcc = V4L2_PIX_FMT_VP8;
1867 }
1868 else if (g_str_equal(mimetype, "video/x-vp9"))
1869 {
1870 fourcc = V4L2_PIX_FMT_VP9;
1871 }
1872 else if (g_str_equal(mimetype, "video/x-av1"))
1873 {
1874 fourcc = V4L2_PIX_FMT_AV1;
1875 }
zengliang.li51f54b62023-10-10 12:14:49 +00001876 else if (g_str_equal(mimetype, "video/x-avs"))
1877 {
1878 fourcc = V4L2_PIX_FMT_AVS;
1879 }
1880 else if (g_str_equal(mimetype, "video/x-avs2"))
1881 {
1882 fourcc = V4L2_PIX_FMT_AVS2;
1883 }
1884 else if (g_str_equal(mimetype, "video/x-avs3"))
1885 {
1886 fourcc = V4L2_PIX_FMT_AVS3;
1887 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001888 else if (g_str_equal(mimetype, "video/x-bayer"))
1889 {
hanghang.luo3128f102022-08-18 10:36:19 +08001890 const gchar *vformat = gst_structure_get_string(structure, "format");
1891 if (vformat)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001892 {
1893 if (!g_ascii_strcasecmp(format, "bggr"))
1894 fourcc = V4L2_PIX_FMT_SBGGR8;
1895 else if (!g_ascii_strcasecmp(format, "gbrg"))
1896 fourcc = V4L2_PIX_FMT_SGBRG8;
1897 else if (!g_ascii_strcasecmp(format, "grbg"))
1898 fourcc = V4L2_PIX_FMT_SGRBG8;
1899 else if (!g_ascii_strcasecmp(format, "rggb"))
1900 fourcc = V4L2_PIX_FMT_SRGGB8;
1901 }
1902 }
1903 else if (g_str_equal(mimetype, "video/x-sonix"))
1904 {
1905 fourcc = V4L2_PIX_FMT_SN9C10X;
1906 }
1907 else if (g_str_equal(mimetype, "video/x-pwc1"))
1908 {
1909 fourcc = V4L2_PIX_FMT_PWC1;
1910 }
1911 else if (g_str_equal(mimetype, "video/x-pwc2"))
1912 {
1913 fourcc = V4L2_PIX_FMT_PWC2;
1914 }
1915 }
1916
1917 /* Prefer the non-contiguous if supported */
1918 v4l2object->prefered_non_contiguous = TRUE;
1919
1920 if (fourcc_nc)
1921 fmt = gst_aml_v4l2_object_get_format_from_fourcc(v4l2object, fourcc_nc);
1922 else if (fourcc == 0)
1923 goto unhandled_format;
1924
1925 if (fmt == NULL)
1926 {
1927 fmt = gst_aml_v4l2_object_get_format_from_fourcc(v4l2object, fourcc);
1928 v4l2object->prefered_non_contiguous = FALSE;
1929 }
1930
1931 if (fmt == NULL)
1932 goto unsupported_format;
1933
1934 *format = fmt;
1935
1936 return TRUE;
1937
1938 /* ERRORS */
1939invalid_format:
1940{
1941 GST_DEBUG_OBJECT(v4l2object, "invalid format");
1942 return FALSE;
1943}
1944unhandled_format:
1945{
1946 GST_DEBUG_OBJECT(v4l2object, "unhandled format");
1947 return FALSE;
1948}
1949unsupported_format:
1950{
1951 GST_DEBUG_OBJECT(v4l2object, "unsupported format");
1952 return FALSE;
1953}
1954}
1955
1956static gboolean
1957gst_aml_v4l2_object_get_nearest_size(GstAmlV4l2Object *v4l2object,
1958 guint32 pixelformat, gint *width, gint *height);
1959
1960static void
1961gst_aml_v4l2_object_add_aspect_ratio(GstAmlV4l2Object *v4l2object, GstStructure *s)
1962{
1963 if (v4l2object->keep_aspect && v4l2object->par)
1964 gst_structure_set_value(s, "pixel-aspect-ratio", v4l2object->par);
1965}
1966
1967/* returns TRUE if the value was changed in place, otherwise FALSE */
1968static gboolean
1969gst_aml_v4l2src_value_simplify(GValue *val)
1970{
1971 /* simplify list of one value to one value */
1972 if (GST_VALUE_HOLDS_LIST(val) && gst_value_list_get_size(val) == 1)
1973 {
1974 const GValue *list_val;
1975 GValue new_val = G_VALUE_INIT;
1976
1977 list_val = gst_value_list_get_value(val, 0);
1978 g_value_init(&new_val, G_VALUE_TYPE(list_val));
1979 g_value_copy(list_val, &new_val);
1980 g_value_unset(val);
1981 *val = new_val;
1982 return TRUE;
1983 }
1984
1985 return FALSE;
1986}
1987
1988static gboolean
1989gst_aml_v4l2_object_get_interlace_mode(enum v4l2_field field,
1990 GstVideoInterlaceMode *interlace_mode)
1991{
1992 switch (field)
1993 {
1994 case V4L2_FIELD_ANY:
1995 GST_ERROR("Driver bug detected - check driver with v4l2-compliance from http://git.linuxtv.org/v4l-utils.git\n");
1996 /* fallthrough */
1997 case V4L2_FIELD_NONE:
1998 *interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
1999 return TRUE;
2000 case V4L2_FIELD_INTERLACED:
2001 case V4L2_FIELD_INTERLACED_TB:
2002 case V4L2_FIELD_INTERLACED_BT:
2003 *interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
2004 return TRUE;
2005 default:
2006 GST_ERROR("Unknown enum v4l2_field %d", field);
2007 return FALSE;
2008 }
2009}
2010
2011static gboolean
2012gst_aml_v4l2_object_get_colorspace(struct v4l2_format *fmt,
2013 GstVideoColorimetry *cinfo)
2014{
2015 gboolean is_rgb =
2016 gst_amL_v4l2_object_v4l2fourcc_is_rgb(fmt->fmt.pix.pixelformat);
2017 enum v4l2_colorspace colorspace;
2018 enum v4l2_quantization range;
2019 enum v4l2_ycbcr_encoding matrix;
2020 enum v4l2_xfer_func transfer;
2021 gboolean ret = TRUE;
2022
2023 if (V4L2_TYPE_IS_MULTIPLANAR(fmt->type))
2024 {
2025 colorspace = fmt->fmt.pix_mp.colorspace;
2026 range = fmt->fmt.pix_mp.quantization;
2027 matrix = fmt->fmt.pix_mp.ycbcr_enc;
2028 transfer = fmt->fmt.pix_mp.xfer_func;
2029 }
2030 else
2031 {
2032 colorspace = fmt->fmt.pix.colorspace;
2033 range = fmt->fmt.pix.quantization;
2034 matrix = fmt->fmt.pix.ycbcr_enc;
2035 transfer = fmt->fmt.pix.xfer_func;
2036 }
xuesong.jiange1a19662022-06-21 20:30:22 +08002037 GST_DEBUG("colorspace:%d, range:%d, matrix:%d, transfer:%d", colorspace, range, matrix, transfer);
2038 GST_DEBUG("cinfo update 1 time | range:%d, matrix:%d, transfer:%d, primaries:%d", cinfo->range, cinfo->matrix, cinfo->transfer, cinfo->primaries);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002039
2040 /* First step, set the defaults for each primaries */
2041 switch (colorspace)
2042 {
2043 case V4L2_COLORSPACE_SMPTE170M:
2044 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2045 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2046 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2047 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE170M;
2048 break;
2049 case V4L2_COLORSPACE_REC709:
2050 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2051 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2052 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2053 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2054 break;
2055 case V4L2_COLORSPACE_SRGB:
2056 case V4L2_COLORSPACE_JPEG:
2057 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2058 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2059 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2060 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT709;
2061 break;
2062 case V4L2_COLORSPACE_OPRGB:
2063 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2064 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2065 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2066 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_ADOBERGB;
2067 break;
2068 case V4L2_COLORSPACE_BT2020:
2069 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2070 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2071 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2072 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT2020;
2073 break;
2074 case V4L2_COLORSPACE_SMPTE240M:
2075 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2076 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2077 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2078 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_SMPTE240M;
2079 break;
2080 case V4L2_COLORSPACE_470_SYSTEM_M:
2081 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2082 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2083 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2084 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470M;
2085 break;
2086 case V4L2_COLORSPACE_470_SYSTEM_BG:
2087 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2088 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2089 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2090 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_BT470BG;
2091 break;
2092 case V4L2_COLORSPACE_RAW:
2093 /* Explicitly unknown */
2094 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2095 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2096 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2097 cinfo->primaries = GST_VIDEO_COLOR_PRIMARIES_UNKNOWN;
2098 break;
2099 default:
2100 GST_DEBUG("Unknown enum v4l2_colorspace %d", colorspace);
2101 ret = FALSE;
2102 break;
2103 }
xuesong.jiange1a19662022-06-21 20:30:22 +08002104 GST_DEBUG("cinfo update 2 time | range:%d, matrix:%d, transfer:%d, primaries:%d", cinfo->range, cinfo->matrix, cinfo->transfer, cinfo->primaries);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002105
2106 if (!ret)
2107 goto done;
2108
2109 /* Second step, apply any custom variation */
2110 switch (range)
2111 {
2112 case V4L2_QUANTIZATION_FULL_RANGE:
2113 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2114 break;
2115 case V4L2_QUANTIZATION_LIM_RANGE:
2116 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2117 break;
2118 case V4L2_QUANTIZATION_DEFAULT:
2119 /* replicated V4L2_MAP_QUANTIZATION_DEFAULT macro behavior */
2120 if (is_rgb && colorspace == V4L2_COLORSPACE_BT2020)
2121 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2122 else if (is_rgb || matrix == V4L2_YCBCR_ENC_XV601 || matrix == V4L2_YCBCR_ENC_XV709 || colorspace == V4L2_COLORSPACE_JPEG)
2123 cinfo->range = GST_VIDEO_COLOR_RANGE_0_255;
2124 else
2125 cinfo->range = GST_VIDEO_COLOR_RANGE_16_235;
2126 break;
2127 default:
2128 GST_WARNING("Unknown enum v4l2_quantization value %d", range);
2129 cinfo->range = GST_VIDEO_COLOR_RANGE_UNKNOWN;
2130 break;
2131 }
xuesong.jiange1a19662022-06-21 20:30:22 +08002132 GST_DEBUG("cinfo update 3 time | range:%d, matrix:%d, transfer:%d, primaries:%d", cinfo->range, cinfo->matrix, cinfo->transfer, cinfo->primaries);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002133
2134 switch (matrix)
2135 {
2136 case V4L2_YCBCR_ENC_XV601:
2137 case V4L2_YCBCR_ENC_SYCC:
2138 GST_FIXME("XV601 and SYCC not defined, assuming 601");
2139 /* fallthrough */
2140 case V4L2_YCBCR_ENC_601:
2141 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT601;
2142 break;
2143 case V4L2_YCBCR_ENC_XV709:
2144 GST_FIXME("XV709 not defined, assuming 709");
2145 /* fallthrough */
2146 case V4L2_YCBCR_ENC_709:
2147 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT709;
2148 break;
2149 case V4L2_YCBCR_ENC_BT2020_CONST_LUM:
2150 GST_FIXME("BT2020 with constant luma is not defined, assuming BT2020");
2151 /* fallthrough */
2152 case V4L2_YCBCR_ENC_BT2020:
2153 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_BT2020;
2154 break;
2155 case V4L2_YCBCR_ENC_SMPTE240M:
2156 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_SMPTE240M;
2157 break;
2158 case V4L2_YCBCR_ENC_DEFAULT:
2159 /* nothing, just use defaults for colorspace */
2160 break;
2161 default:
2162 GST_WARNING("Unknown enum v4l2_ycbcr_encoding value %d", matrix);
2163 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_UNKNOWN;
2164 break;
2165 }
xuesong.jiange1a19662022-06-21 20:30:22 +08002166 GST_DEBUG("cinfo update 4 time | range:%d, matrix:%d, transfer:%d, primaries:%d", cinfo->range, cinfo->matrix, cinfo->transfer, cinfo->primaries);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002167
2168 /* Set identity matrix for R'G'B' formats to avoid creating
2169 * confusion. This though is cosmetic as it's now properly ignored by
2170 * the video info API and videoconvert. */
2171 if (is_rgb)
2172 cinfo->matrix = GST_VIDEO_COLOR_MATRIX_RGB;
2173
2174 switch (transfer)
2175 {
2176 case V4L2_XFER_FUNC_709:
2177 if (colorspace == V4L2_COLORSPACE_BT2020 && fmt->fmt.pix.height >= 2160)
2178 cinfo->transfer = GST_VIDEO_TRANSFER_BT2020_12;
2179 else
2180 cinfo->transfer = GST_VIDEO_TRANSFER_BT709;
2181 break;
2182 case V4L2_XFER_FUNC_SRGB:
2183 cinfo->transfer = GST_VIDEO_TRANSFER_SRGB;
2184 break;
2185 case V4L2_XFER_FUNC_OPRGB:
2186 cinfo->transfer = GST_VIDEO_TRANSFER_ADOBERGB;
2187 break;
2188 case V4L2_XFER_FUNC_SMPTE240M:
2189 cinfo->transfer = GST_VIDEO_TRANSFER_SMPTE240M;
2190 break;
2191 case V4L2_XFER_FUNC_NONE:
2192 cinfo->transfer = GST_VIDEO_TRANSFER_GAMMA10;
2193 break;
2194 case V4L2_XFER_FUNC_DEFAULT:
2195 /* nothing, just use defaults for colorspace */
2196 break;
2197 default:
2198 GST_WARNING("Unknown enum v4l2_xfer_func value %d", transfer);
2199 cinfo->transfer = GST_VIDEO_TRANSFER_UNKNOWN;
2200 break;
2201 }
xuesong.jiange1a19662022-06-21 20:30:22 +08002202 GST_DEBUG("cinfo update 5 time | range:%d, matrix:%d, transfer:%d, primaries:%d", cinfo->range, cinfo->matrix, cinfo->transfer, cinfo->primaries);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002203
2204done:
2205 return ret;
2206}
2207
2208static int
2209gst_aml_v4l2_object_try_fmt(GstAmlV4l2Object *v4l2object,
2210 struct v4l2_format *try_fmt)
2211{
2212 int fd = v4l2object->video_fd;
2213 struct v4l2_format fmt;
2214 int r;
2215
2216 memcpy(&fmt, try_fmt, sizeof(fmt));
2217 r = v4l2object->ioctl(fd, VIDIOC_TRY_FMT, &fmt);
2218
2219 if (r < 0 && errno == ENOTTY)
2220 {
2221 /* The driver might not implement TRY_FMT, in which case we will try
2222 S_FMT to probe */
2223 if (GST_AML_V4L2_IS_ACTIVE(v4l2object))
2224 goto error;
2225
2226 memcpy(&fmt, try_fmt, sizeof(fmt));
2227 r = v4l2object->ioctl(fd, VIDIOC_S_FMT, &fmt);
2228 }
2229 memcpy(try_fmt, &fmt, sizeof(fmt));
2230
2231 return r;
2232
2233error:
2234 memcpy(try_fmt, &fmt, sizeof(fmt));
2235 GST_WARNING_OBJECT(v4l2object->dbg_obj,
2236 "Unable to try format: %s", g_strerror(errno));
2237 return r;
2238}
2239
2240static void
2241gst_aml_v4l2_object_add_interlace_mode(GstAmlV4l2Object *v4l2object,
2242 GstStructure *s, guint32 width, guint32 height, guint32 pixelformat)
2243{
2244 struct v4l2_format fmt;
2245 GValue interlace_formats = {
2246 0,
2247 };
2248 enum v4l2_field formats[] = {V4L2_FIELD_NONE, V4L2_FIELD_INTERLACED};
2249 gsize i;
2250 GstVideoInterlaceMode interlace_mode, prev = -1;
2251
2252 if (!g_str_equal(gst_structure_get_name(s), "video/x-raw"))
2253 return;
2254
2255 if (v4l2object->never_interlaced)
2256 {
2257 gst_structure_set(s, "interlace-mode", G_TYPE_STRING, "progressive", NULL);
2258 return;
2259 }
2260
2261 g_value_init(&interlace_formats, GST_TYPE_LIST);
2262
2263 /* Try twice - once for NONE, once for INTERLACED. */
2264 for (i = 0; i < G_N_ELEMENTS(formats); i++)
2265 {
2266 memset(&fmt, 0, sizeof(fmt));
2267 fmt.type = v4l2object->type;
2268 fmt.fmt.pix.width = width;
2269 fmt.fmt.pix.height = height;
2270 fmt.fmt.pix.pixelformat = pixelformat;
2271 fmt.fmt.pix.field = formats[i];
2272
2273 if (gst_aml_v4l2_object_try_fmt(v4l2object, &fmt) == 0 &&
2274 gst_aml_v4l2_object_get_interlace_mode(fmt.fmt.pix.field, &interlace_mode) && prev != interlace_mode)
2275 {
2276 GValue interlace_enum = {
2277 0,
2278 };
2279 const gchar *mode_string;
2280 g_value_init(&interlace_enum, G_TYPE_STRING);
2281 mode_string = gst_video_interlace_mode_to_string(interlace_mode);
2282 g_value_set_string(&interlace_enum, mode_string);
2283 gst_value_list_append_and_take_value(&interlace_formats,
2284 &interlace_enum);
2285 prev = interlace_mode;
2286 }
2287 }
2288
2289 if (gst_aml_v4l2src_value_simplify(&interlace_formats) || gst_value_list_get_size(&interlace_formats) > 0)
2290 gst_structure_take_value(s, "interlace-mode", &interlace_formats);
2291 else
2292 GST_WARNING_OBJECT(v4l2object, "Failed to determine interlace mode");
2293
2294 return;
2295}
2296
2297static void
2298gst_aml_v4l2_object_fill_colorimetry_list(GValue *list,
2299 GstVideoColorimetry *cinfo)
2300{
2301 GValue colorimetry = G_VALUE_INIT;
2302 guint size;
2303 guint i;
2304 gboolean found = FALSE;
2305
2306 g_value_init(&colorimetry, G_TYPE_STRING);
2307 g_value_take_string(&colorimetry, gst_video_colorimetry_to_string(cinfo));
xuesong.jiange1a19662022-06-21 20:30:22 +08002308 GST_DEBUG("fill colorimetry:%s into list", gst_video_colorimetry_to_string(cinfo));
xuesong.jiangae1548e2022-05-06 16:38:46 +08002309
2310 /* only insert if no duplicate */
2311 size = gst_value_list_get_size(list);
2312 for (i = 0; i < size; i++)
2313 {
2314 const GValue *tmp;
2315
2316 tmp = gst_value_list_get_value(list, i);
2317 if (gst_value_compare(&colorimetry, tmp) == GST_VALUE_EQUAL)
2318 {
2319 found = TRUE;
2320 break;
2321 }
2322 }
2323
2324 if (!found)
2325 gst_value_list_append_and_take_value(list, &colorimetry);
2326 else
2327 g_value_unset(&colorimetry);
2328}
2329
2330static void
2331gst_aml_v4l2_object_add_colorspace(GstAmlV4l2Object *v4l2object, GstStructure *s,
2332 guint32 width, guint32 height, guint32 pixelformat)
2333{
2334 struct v4l2_format fmt;
2335 GValue list = G_VALUE_INIT;
2336 GstVideoColorimetry cinfo;
2337 enum v4l2_colorspace req_cspace;
2338
2339 memset(&fmt, 0, sizeof(fmt));
2340 fmt.type = v4l2object->type;
2341 fmt.fmt.pix.width = width;
2342 fmt.fmt.pix.height = height;
2343 fmt.fmt.pix.pixelformat = pixelformat;
2344
2345 g_value_init(&list, GST_TYPE_LIST);
2346
2347 /* step 1: get device default colorspace and insert it first as
2348 * it should be the preferred one */
xuesong.jiange1a19662022-06-21 20:30:22 +08002349 GST_DEBUG("try for pixl format");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002350 if (gst_aml_v4l2_object_try_fmt(v4l2object, &fmt) == 0)
2351 {
2352 if (gst_aml_v4l2_object_get_colorspace(&fmt, &cinfo))
2353 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2354 }
2355
2356 /* step 2: probe all colorspace other than default
2357 * We don't probe all colorspace, range, matrix and transfer combination to
2358 * avoid ioctl flooding which could greatly increase initialization time
2359 * with low-speed devices (UVC...) */
2360 for (req_cspace = V4L2_COLORSPACE_SMPTE170M;
2361 req_cspace <= V4L2_COLORSPACE_RAW; req_cspace++)
2362 {
xuesong.jiange1a19662022-06-21 20:30:22 +08002363 GST_DEBUG("try for pixl format in while loop :%d", req_cspace);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002364 /* V4L2_COLORSPACE_BT878 is deprecated and shall not be used, so skip */
2365 if (req_cspace == V4L2_COLORSPACE_BT878)
2366 continue;
2367
2368 if (V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type))
2369 fmt.fmt.pix_mp.colorspace = req_cspace;
2370 else
2371 fmt.fmt.pix.colorspace = req_cspace;
2372
2373 if (gst_aml_v4l2_object_try_fmt(v4l2object, &fmt) == 0)
2374 {
xuesong.jiange1a19662022-06-21 20:30:22 +08002375 GST_DEBUG("try for pixl format in while loop :%d tried ok", req_cspace);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002376 enum v4l2_colorspace colorspace;
2377
2378 if (V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type))
2379 colorspace = fmt.fmt.pix_mp.colorspace;
2380 else
2381 colorspace = fmt.fmt.pix.colorspace;
2382
2383 if (colorspace == req_cspace)
2384 {
2385 if (gst_aml_v4l2_object_get_colorspace(&fmt, &cinfo))
2386 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2387 }
2388 }
2389 }
2390
xuesong.jiang7b0882c2022-06-22 14:10:30 +08002391 GST_DEBUG("deal: caps with colorimetry 2,3,14,7");
xuesong.jiange1a19662022-06-21 20:30:22 +08002392 cinfo.range = 2;
2393 cinfo.matrix = 3;
2394 cinfo.transfer = 14;
2395 cinfo.primaries = 7;
2396 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2397
xuesong.jiang7b0882c2022-06-22 14:10:30 +08002398 GST_DEBUG("deal: caps with colorimetry 2,6,13,7");
2399 cinfo.range = 2;
2400 cinfo.matrix = 6;
2401 cinfo.transfer = 13;
2402 cinfo.primaries = 7;
2403 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2404
xuesong.jiang5c9aca72022-07-12 16:29:24 +08002405 GST_DEBUG("deal: caps with colorimetry 2,6,14,7");
2406 cinfo.range = 2;
2407 cinfo.matrix = 6;
2408 cinfo.transfer = 14;
2409 cinfo.primaries = 7;
2410 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2411
fei.dengccc89632022-07-15 19:10:17 +08002412 GST_DEBUG("deal: caps with colorimetry 2,6,0,7");
2413 cinfo.range = 2;
2414 cinfo.matrix = 6;
2415 cinfo.transfer = 0;
2416 cinfo.primaries = 7;
2417 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2418
fei.dengca85b052022-07-19 14:49:23 +08002419 GST_DEBUG("deal: caps with colorimetry 0,6,0,7");
2420 cinfo.range = 0;
2421 cinfo.matrix = 6;
2422 cinfo.transfer = 0;
2423 cinfo.primaries = 7;
2424 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2425
sheng.liua326d202022-07-20 14:15:34 +08002426 GST_DEBUG("deal: caps with colorimetry 2,3,0,0");
2427 cinfo.range = 2;
2428 cinfo.matrix = 3;
2429 cinfo.transfer = 0;
2430 cinfo.primaries = 0;
2431 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2432
hanghang.luoc54208e2023-09-22 02:43:54 +00002433 GST_DEBUG("deal: caps with colorimetry 2,6,14,0");
2434 cinfo.range = 2;
2435 cinfo.matrix = 6;
2436 cinfo.transfer = 14;
2437 cinfo.primaries = 0;
2438 gst_aml_v4l2_object_fill_colorimetry_list(&list, &cinfo);
2439
xuesong.jiangae1548e2022-05-06 16:38:46 +08002440 if (gst_value_list_get_size(&list) > 0)
2441 gst_structure_take_value(s, "colorimetry", &list);
2442 else
2443 g_value_unset(&list);
2444
2445 return;
2446}
2447
2448/* The frame interval enumeration code first appeared in Linux 2.6.19. */
2449static GstStructure *
2450gst_aml_v4l2_object_probe_caps_for_format_and_size(GstAmlV4l2Object *v4l2object,
2451 guint32 pixelformat,
2452 guint32 width, guint32 height, const GstStructure *template)
2453{
2454 gint fd = v4l2object->video_fd;
2455 struct v4l2_frmivalenum ival;
2456 guint32 num, denom;
2457 GstStructure *s;
2458 GValue rates = {
2459 0,
2460 };
2461
2462 memset(&ival, 0, sizeof(struct v4l2_frmivalenum));
2463 ival.index = 0;
2464 ival.pixel_format = pixelformat;
2465 ival.width = width;
2466 ival.height = height;
2467
2468 GST_LOG_OBJECT(v4l2object->dbg_obj,
2469 "get frame interval for %ux%u, %" GST_FOURCC_FORMAT, width, height,
2470 GST_FOURCC_ARGS(pixelformat));
2471
2472 /* keep in mind that v4l2 gives us frame intervals (durations); we invert the
2473 * fraction to get framerate */
2474 if (v4l2object->ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) < 0)
2475 goto enum_frameintervals_failed;
2476
2477 if (ival.type == V4L2_FRMIVAL_TYPE_DISCRETE)
2478 {
2479 GValue rate = {
2480 0,
2481 };
2482
2483 g_value_init(&rates, GST_TYPE_LIST);
2484 g_value_init(&rate, GST_TYPE_FRACTION);
2485
2486 do
2487 {
2488 num = ival.discrete.numerator;
2489 denom = ival.discrete.denominator;
2490
2491 if (num > G_MAXINT || denom > G_MAXINT)
2492 {
2493 /* let us hope we don't get here... */
2494 num >>= 1;
2495 denom >>= 1;
2496 }
2497
2498 GST_LOG_OBJECT(v4l2object->dbg_obj, "adding discrete framerate: %d/%d",
2499 denom, num);
2500
2501 /* swap to get the framerate */
2502 gst_value_set_fraction(&rate, denom, num);
2503 gst_value_list_append_value(&rates, &rate);
2504
2505 ival.index++;
2506 } while (v4l2object->ioctl(fd, VIDIOC_ENUM_FRAMEINTERVALS, &ival) >= 0);
2507 }
2508 else if (ival.type == V4L2_FRMIVAL_TYPE_STEPWISE)
2509 {
2510 GValue min = {
2511 0,
2512 };
2513 GValue step = {
2514 0,
2515 };
2516 GValue max = {
2517 0,
2518 };
2519 gboolean added = FALSE;
2520 guint32 minnum, mindenom;
2521 guint32 maxnum, maxdenom;
2522
2523 g_value_init(&rates, GST_TYPE_LIST);
2524
2525 g_value_init(&min, GST_TYPE_FRACTION);
2526 g_value_init(&step, GST_TYPE_FRACTION);
2527 g_value_init(&max, GST_TYPE_FRACTION);
2528
2529 /* get the min */
2530 minnum = ival.stepwise.min.numerator;
2531 mindenom = ival.stepwise.min.denominator;
2532 if (minnum > G_MAXINT || mindenom > G_MAXINT)
2533 {
2534 minnum >>= 1;
2535 mindenom >>= 1;
2536 }
2537 GST_LOG_OBJECT(v4l2object->dbg_obj, "stepwise min frame interval: %d/%d",
2538 minnum, mindenom);
2539 gst_value_set_fraction(&min, minnum, mindenom);
2540
2541 /* get the max */
2542 maxnum = ival.stepwise.max.numerator;
2543 maxdenom = ival.stepwise.max.denominator;
2544 if (maxnum > G_MAXINT || maxdenom > G_MAXINT)
2545 {
2546 maxnum >>= 1;
2547 maxdenom >>= 1;
2548 }
2549
2550 GST_LOG_OBJECT(v4l2object->dbg_obj, "stepwise max frame interval: %d/%d",
2551 maxnum, maxdenom);
2552 gst_value_set_fraction(&max, maxnum, maxdenom);
2553
2554 /* get the step */
2555 num = ival.stepwise.step.numerator;
2556 denom = ival.stepwise.step.denominator;
2557 if (num > G_MAXINT || denom > G_MAXINT)
2558 {
2559 num >>= 1;
2560 denom >>= 1;
2561 }
2562
2563 if (num == 0 || denom == 0)
2564 {
2565 /* in this case we have a wrong fraction or no step, set the step to max
2566 * so that we only add the min value in the loop below */
2567 num = maxnum;
2568 denom = maxdenom;
2569 }
2570
2571 /* since we only have gst_value_fraction_subtract and not add, negate the
2572 * numerator */
2573 GST_LOG_OBJECT(v4l2object->dbg_obj, "stepwise step frame interval: %d/%d",
2574 num, denom);
2575 gst_value_set_fraction(&step, -num, denom);
2576
2577 while (gst_value_compare(&min, &max) != GST_VALUE_GREATER_THAN)
2578 {
2579 GValue rate = {
2580 0,
2581 };
2582
2583 num = gst_value_get_fraction_numerator(&min);
2584 denom = gst_value_get_fraction_denominator(&min);
2585 GST_LOG_OBJECT(v4l2object->dbg_obj, "adding stepwise framerate: %d/%d",
2586 denom, num);
2587
2588 /* invert to get the framerate */
2589 g_value_init(&rate, GST_TYPE_FRACTION);
2590 gst_value_set_fraction(&rate, denom, num);
2591 gst_value_list_append_value(&rates, &rate);
2592 added = TRUE;
2593
2594 /* we're actually adding because step was negated above. This is because
2595 * there is no _add function... */
2596 if (!gst_value_fraction_subtract(&min, &min, &step))
2597 {
2598 GST_WARNING_OBJECT(v4l2object->dbg_obj, "could not step fraction!");
2599 break;
2600 }
2601 }
2602 if (!added)
2603 {
2604 /* no range was added, leave the default range from the template */
2605 GST_WARNING_OBJECT(v4l2object->dbg_obj,
2606 "no range added, leaving default");
2607 g_value_unset(&rates);
2608 }
2609 }
2610 else if (ival.type == V4L2_FRMIVAL_TYPE_CONTINUOUS)
2611 {
2612 guint32 maxnum, maxdenom;
2613
2614 g_value_init(&rates, GST_TYPE_FRACTION_RANGE);
2615
2616 num = ival.stepwise.min.numerator;
2617 denom = ival.stepwise.min.denominator;
2618 if (num > G_MAXINT || denom > G_MAXINT)
2619 {
2620 num >>= 1;
2621 denom >>= 1;
2622 }
2623
2624 maxnum = ival.stepwise.max.numerator;
2625 maxdenom = ival.stepwise.max.denominator;
2626 if (maxnum > G_MAXINT || maxdenom > G_MAXINT)
2627 {
2628 maxnum >>= 1;
2629 maxdenom >>= 1;
2630 }
2631
2632 GST_LOG_OBJECT(v4l2object->dbg_obj,
2633 "continuous frame interval %d/%d to %d/%d", maxdenom, maxnum, denom,
2634 num);
2635
2636 gst_value_set_fraction_range_full(&rates, maxdenom, maxnum, denom, num);
2637 }
2638 else
2639 {
2640 goto unknown_type;
2641 }
2642
2643return_data:
2644 s = gst_structure_copy(template);
2645 gst_structure_set(s, "width", G_TYPE_INT, (gint)width,
2646 "height", G_TYPE_INT, (gint)height, NULL);
2647
2648 gst_aml_v4l2_object_add_aspect_ratio(v4l2object, s);
2649
2650 if (!v4l2object->skip_try_fmt_probes)
2651 {
2652 gst_aml_v4l2_object_add_interlace_mode(v4l2object, s, width, height,
2653 pixelformat);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08002654 // gst_aml_v4l2_object_add_colorspace(v4l2object, s, width, height, pixelformat);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002655 }
2656
2657 if (G_IS_VALUE(&rates))
2658 {
2659 gst_aml_v4l2src_value_simplify(&rates);
2660 /* only change the framerate on the template when we have a valid probed new
2661 * value */
2662 gst_structure_take_value(s, "framerate", &rates);
2663 }
2664 else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2665 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
2666 {
2667 gst_structure_set(s, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1, G_MAXINT,
2668 1, NULL);
2669 }
2670 return s;
2671
2672 /* ERRORS */
2673enum_frameintervals_failed:
2674{
2675 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
2676 "Unable to enumerate intervals for %" GST_FOURCC_FORMAT "@%ux%u",
2677 GST_FOURCC_ARGS(pixelformat), width, height);
2678 goto return_data;
2679}
2680unknown_type:
2681{
2682 /* I don't see how this is actually an error, we ignore the format then */
2683 GST_WARNING_OBJECT(v4l2object->dbg_obj,
2684 "Unknown frame interval type at %" GST_FOURCC_FORMAT "@%ux%u: %u",
2685 GST_FOURCC_ARGS(pixelformat), width, height, ival.type);
2686 return NULL;
2687}
2688}
2689
2690static gint
2691sort_by_frame_size(GstStructure *s1, GstStructure *s2)
2692{
2693 int w1, h1, w2, h2;
2694
2695 gst_structure_get_int(s1, "width", &w1);
2696 gst_structure_get_int(s1, "height", &h1);
2697 gst_structure_get_int(s2, "width", &w2);
2698 gst_structure_get_int(s2, "height", &h2);
2699
2700 /* I think it's safe to assume that this won't overflow for a while */
2701 return ((w2 * h2) - (w1 * h1));
2702}
2703
2704static void
2705gst_aml_v4l2_object_update_and_append(GstAmlV4l2Object *v4l2object,
2706 guint32 format, GstCaps *caps, GstStructure *s)
2707{
2708 GstStructure *alt_s = NULL;
2709
2710 /* Encoded stream on output buffer need to be parsed */
2711 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT ||
2712 v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
2713 {
2714 gint i = 0;
2715
2716 for (; i < GST_AML_V4L2_FORMAT_COUNT; i++)
2717 {
2718 if (format == gst_aml_v4l2_formats[i].format &&
2719 gst_aml_v4l2_formats[i].flags & GST_V4L2_CODEC &&
2720 !(gst_aml_v4l2_formats[i].flags & GST_V4L2_NO_PARSE))
2721 {
2722 gst_structure_set(s, "parsed", G_TYPE_BOOLEAN, TRUE, NULL);
2723 break;
2724 }
2725 }
2726 }
2727
2728 if (v4l2object->has_alpha_component &&
2729 (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2730 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE))
2731 {
2732 switch (format)
2733 {
2734 case V4L2_PIX_FMT_RGB32:
2735 alt_s = gst_structure_copy(s);
2736 gst_structure_set(alt_s, "format", G_TYPE_STRING, "ARGB", NULL);
2737 break;
2738 case V4L2_PIX_FMT_BGR32:
2739 alt_s = gst_structure_copy(s);
2740 gst_structure_set(alt_s, "format", G_TYPE_STRING, "BGRA", NULL);
2741 break;
2742 default:
2743 break;
2744 }
2745 }
2746
2747 gst_caps_append_structure(caps, s);
2748
2749 if (alt_s)
2750 gst_caps_append_structure(caps, alt_s);
2751}
2752
2753static GstCaps *
2754gst_aml_v4l2_object_probe_caps_for_format(GstAmlV4l2Object *v4l2object,
2755 guint32 pixelformat, const GstStructure *template)
2756{
2757 GstCaps *ret = gst_caps_new_empty();
2758 GstStructure *tmp;
2759 gint fd = v4l2object->video_fd;
2760 struct v4l2_frmsizeenum size;
2761 GList *results = NULL;
2762 guint32 w, h;
2763
2764 if (pixelformat == GST_MAKE_FOURCC('M', 'P', 'E', 'G'))
2765 {
2766 gst_caps_append_structure(ret, gst_structure_copy(template));
2767 return ret;
2768 }
2769
2770 memset(&size, 0, sizeof(struct v4l2_frmsizeenum));
2771 size.index = 0;
2772 size.pixel_format = pixelformat;
2773
2774 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
2775 "Enumerating frame sizes for %" GST_FOURCC_FORMAT,
2776 GST_FOURCC_ARGS(pixelformat));
2777
2778 if (v4l2object->ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
2779 goto enum_framesizes_failed;
2780
2781 if (size.type == V4L2_FRMSIZE_TYPE_DISCRETE)
2782 {
2783 do
2784 {
2785 GST_LOG_OBJECT(v4l2object->dbg_obj, "got discrete frame size %dx%d",
2786 size.discrete.width, size.discrete.height);
2787
2788 w = MIN(size.discrete.width, G_MAXINT);
2789 h = MIN(size.discrete.height, G_MAXINT);
2790
2791 if (w && h)
2792 {
2793 tmp =
2794 gst_aml_v4l2_object_probe_caps_for_format_and_size(v4l2object,
2795 pixelformat, w, h, template);
2796
2797 if (tmp)
2798 results = g_list_prepend(results, tmp);
2799 }
2800
2801 size.index++;
2802 } while (v4l2object->ioctl(fd, VIDIOC_ENUM_FRAMESIZES, &size) >= 0);
2803 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
2804 "done iterating discrete frame sizes");
2805 }
2806 else if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE)
2807 {
2808 guint32 maxw, maxh, step_w, step_h;
2809
2810 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "we have stepwise frame sizes:");
2811 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "min width: %d",
2812 size.stepwise.min_width);
2813 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "min height: %d",
2814 size.stepwise.min_height);
2815 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "max width: %d",
2816 size.stepwise.max_width);
2817 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "min height: %d",
2818 size.stepwise.max_height);
2819 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "step width: %d",
2820 size.stepwise.step_width);
2821 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "step height: %d",
2822 size.stepwise.step_height);
2823
2824 w = MAX(size.stepwise.min_width, 1);
2825 h = MAX(size.stepwise.min_height, 1);
2826 maxw = MIN(size.stepwise.max_width, G_MAXINT);
2827 maxh = MIN(size.stepwise.max_height, G_MAXINT);
2828
hanghang.luo9edfc7d2023-05-17 07:01:05 +00002829 /* in this position,updating resolution only to pass the negotiation
2830 * actually, the details about resolution refer to function:
2831 * gst_aml_v4l2_object_set_format_full for checking.
2832 */
2833 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "update maxw_maxh to MAX(maxw,maxh)_MAX(maxw,maxh)");
2834 maxh = MAX (maxw, maxh);
2835 maxw = maxh;
2836
xuesong.jiangae1548e2022-05-06 16:38:46 +08002837 step_w = MAX(size.stepwise.step_width, 1);
2838 step_h = MAX(size.stepwise.step_height, 1);
2839
2840 /* FIXME: check for sanity and that min/max are multiples of the steps */
2841
2842 /* we only query details for the max width/height since it's likely the
2843 * most restricted if there are any resolution-dependent restrictions */
2844 tmp = gst_aml_v4l2_object_probe_caps_for_format_and_size(v4l2object,
2845 pixelformat, maxw, maxh, template);
2846
2847 if (tmp)
2848 {
2849 GValue step_range = G_VALUE_INIT;
2850
2851 g_value_init(&step_range, GST_TYPE_INT_RANGE);
2852 gst_value_set_int_range_step(&step_range, w, maxw, step_w);
2853 gst_structure_set_value(tmp, "width", &step_range);
2854
2855 gst_value_set_int_range_step(&step_range, h, maxh, step_h);
2856 gst_structure_take_value(tmp, "height", &step_range);
2857
2858 /* no point using the results list here, since there's only one struct */
2859 gst_aml_v4l2_object_update_and_append(v4l2object, pixelformat, ret, tmp);
2860 }
2861 }
2862 else if (size.type == V4L2_FRMSIZE_TYPE_CONTINUOUS)
2863 {
2864 guint32 maxw, maxh;
2865
2866 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "we have continuous frame sizes:");
2867 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "min width: %d",
2868 size.stepwise.min_width);
2869 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "min height: %d",
2870 size.stepwise.min_height);
2871 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "max width: %d",
2872 size.stepwise.max_width);
2873 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "min height: %d",
2874 size.stepwise.max_height);
2875
2876 w = MAX(size.stepwise.min_width, 1);
2877 h = MAX(size.stepwise.min_height, 1);
2878 maxw = MIN(size.stepwise.max_width, G_MAXINT);
2879 maxh = MIN(size.stepwise.max_height, G_MAXINT);
2880
2881 tmp =
2882 gst_aml_v4l2_object_probe_caps_for_format_and_size(v4l2object, pixelformat,
2883 w, h, template);
2884 if (tmp)
2885 {
2886 gst_structure_set(tmp, "width", GST_TYPE_INT_RANGE, (gint)w,
2887 (gint)maxw, "height", GST_TYPE_INT_RANGE, (gint)h, (gint)maxh,
2888 NULL);
2889
2890 /* no point using the results list here, since there's only one struct */
2891 gst_aml_v4l2_object_update_and_append(v4l2object, pixelformat, ret, tmp);
2892 }
2893 }
2894 else
2895 {
2896 goto unknown_type;
2897 }
2898
2899 /* we use an intermediary list to store and then sort the results of the
2900 * probing because we can't make any assumptions about the order in which
2901 * the driver will give us the sizes, but we want the final caps to contain
2902 * the results starting with the highest resolution and having the lowest
2903 * resolution last, since order in caps matters for things like fixation. */
2904 results = g_list_sort(results, (GCompareFunc)sort_by_frame_size);
2905 while (results != NULL)
2906 {
2907 gst_aml_v4l2_object_update_and_append(v4l2object, pixelformat, ret,
2908 results->data);
2909 results = g_list_delete_link(results, results);
2910 }
2911
2912 if (gst_caps_is_empty(ret))
2913 goto enum_framesizes_no_results;
2914
2915 return ret;
2916
2917 /* ERRORS */
2918enum_framesizes_failed:
2919{
2920 /* I don't see how this is actually an error */
2921 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
2922 "Failed to enumerate frame sizes for pixelformat %" GST_FOURCC_FORMAT
2923 " (%s)",
2924 GST_FOURCC_ARGS(pixelformat), g_strerror(errno));
2925 goto default_frame_sizes;
2926}
2927enum_framesizes_no_results:
2928{
2929 /* it's possible that VIDIOC_ENUM_FRAMESIZES is defined but the driver in
2930 * question doesn't actually support it yet */
2931 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
2932 "No results for pixelformat %" GST_FOURCC_FORMAT
2933 " enumerating frame sizes, trying fallback",
2934 GST_FOURCC_ARGS(pixelformat));
2935 goto default_frame_sizes;
2936}
2937unknown_type:
2938{
2939 GST_WARNING_OBJECT(v4l2object->dbg_obj,
2940 "Unknown frame sizeenum type for pixelformat %" GST_FOURCC_FORMAT
2941 ": %u",
2942 GST_FOURCC_ARGS(pixelformat), size.type);
2943 goto default_frame_sizes;
2944}
2945
2946default_frame_sizes:
2947{
2948 gint min_w, max_w, min_h, max_h, fix_num = 0, fix_denom = 0;
2949
2950 /* This code is for Linux < 2.6.19 */
2951 min_w = min_h = 1;
2952 max_w = max_h = GST_AML_V4L2_MAX_SIZE;
2953 if (!gst_aml_v4l2_object_get_nearest_size(v4l2object, pixelformat, &min_w,
2954 &min_h))
2955 {
2956 GST_WARNING_OBJECT(v4l2object->dbg_obj,
2957 "Could not probe minimum capture size for pixelformat %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(pixelformat));
2958 }
2959 if (!gst_aml_v4l2_object_get_nearest_size(v4l2object, pixelformat, &max_w,
2960 &max_h))
2961 {
2962 GST_WARNING_OBJECT(v4l2object->dbg_obj,
2963 "Could not probe maximum capture size for pixelformat %" GST_FOURCC_FORMAT, GST_FOURCC_ARGS(pixelformat));
2964 }
2965
2966 tmp = gst_structure_copy(template);
hanghang.luo3128f102022-08-18 10:36:19 +08002967#ifdef DELETE_FOR_LGE
xuesong.jiangae1548e2022-05-06 16:38:46 +08002968 if (fix_num)
2969 {
2970 gst_structure_set(tmp, "framerate", GST_TYPE_FRACTION, fix_num,
2971 fix_denom, NULL);
2972 }
hanghang.luo3128f102022-08-18 10:36:19 +08002973 else
2974#endif
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08002975 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
2976 v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
xuesong.jiangae1548e2022-05-06 16:38:46 +08002977 {
2978 /* if norm can't be used, copy the template framerate */
2979 gst_structure_set(tmp, "framerate", GST_TYPE_FRACTION_RANGE, 0, 1,
2980 G_MAXINT, 1, NULL);
2981 }
2982
2983 if (min_w == max_w)
2984 gst_structure_set(tmp, "width", G_TYPE_INT, max_w, NULL);
2985 else
2986 gst_structure_set(tmp, "width", GST_TYPE_INT_RANGE, min_w, max_w, NULL);
2987
2988 if (min_h == max_h)
2989 gst_structure_set(tmp, "height", G_TYPE_INT, max_h, NULL);
2990 else
2991 gst_structure_set(tmp, "height", GST_TYPE_INT_RANGE, min_h, max_h, NULL);
2992
2993 gst_aml_v4l2_object_add_aspect_ratio(v4l2object, tmp);
2994
2995 if (!v4l2object->skip_try_fmt_probes)
2996 {
2997 /* We could consider setting interlace mode from min and max. */
2998 gst_aml_v4l2_object_add_interlace_mode(v4l2object, tmp, max_w, max_h,
2999 pixelformat);
3000 /* We could consider to check colorspace for min too, in case it depends on
3001 * the size. But in this case, min and max could not be enough */
3002 gst_aml_v4l2_object_add_colorspace(v4l2object, tmp, max_w, max_h,
3003 pixelformat);
3004 }
3005
3006 gst_aml_v4l2_object_update_and_append(v4l2object, pixelformat, ret, tmp);
3007 return ret;
3008}
3009}
3010
3011static gboolean
3012gst_aml_v4l2_object_get_nearest_size(GstAmlV4l2Object *v4l2object,
3013 guint32 pixelformat, gint *width, gint *height)
3014{
3015 struct v4l2_format fmt;
3016 gboolean ret = FALSE;
3017 GstVideoInterlaceMode interlace_mode;
3018
3019 g_return_val_if_fail(width != NULL, FALSE);
3020 g_return_val_if_fail(height != NULL, FALSE);
3021
3022 GST_LOG_OBJECT(v4l2object->dbg_obj,
3023 "getting nearest size to %dx%d with format %" GST_FOURCC_FORMAT,
3024 *width, *height, GST_FOURCC_ARGS(pixelformat));
3025
3026 memset(&fmt, 0, sizeof(struct v4l2_format));
3027
3028 /* get size delimiters */
3029 memset(&fmt, 0, sizeof(fmt));
3030 fmt.type = v4l2object->type;
3031 fmt.fmt.pix.width = *width;
3032 fmt.fmt.pix.height = *height;
3033 fmt.fmt.pix.pixelformat = pixelformat;
3034 fmt.fmt.pix.field = V4L2_FIELD_ANY;
3035
3036 if (gst_aml_v4l2_object_try_fmt(v4l2object, &fmt) < 0)
3037 goto error;
3038
3039 GST_LOG_OBJECT(v4l2object->dbg_obj,
3040 "got nearest size %dx%d", fmt.fmt.pix.width, fmt.fmt.pix.height);
3041
3042 *width = fmt.fmt.pix.width;
3043 *height = fmt.fmt.pix.height;
3044
3045 if (!gst_aml_v4l2_object_get_interlace_mode(fmt.fmt.pix.field, &interlace_mode))
3046 {
3047 GST_WARNING_OBJECT(v4l2object->dbg_obj,
3048 "Unsupported field type for %" GST_FOURCC_FORMAT "@%ux%u: %u",
3049 GST_FOURCC_ARGS(pixelformat), *width, *height, fmt.fmt.pix.field);
3050 goto error;
3051 }
3052
3053 ret = TRUE;
3054
3055error:
3056 if (!ret)
3057 {
3058 GST_WARNING_OBJECT(v4l2object->dbg_obj,
3059 "Unable to try format: %s", g_strerror(errno));
3060 }
3061
3062 return ret;
3063}
3064
3065static gboolean
3066gst_aml_v4l2_object_is_dmabuf_supported(GstAmlV4l2Object *v4l2object)
3067{
3068 gboolean ret = TRUE;
3069 struct v4l2_exportbuffer expbuf = {
3070 .type = v4l2object->type,
3071 .index = -1,
3072 .plane = -1,
3073 .flags = O_CLOEXEC | O_RDWR,
3074 };
3075
3076 if (v4l2object->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED)
3077 {
3078 GST_WARNING_OBJECT(v4l2object->dbg_obj,
3079 "libv4l2 converter detected, disabling DMABuf");
3080 ret = FALSE;
3081 }
3082
3083 /* Expected to fail, but ENOTTY tells us that it is not implemented. */
3084 v4l2object->ioctl(v4l2object->video_fd, VIDIOC_EXPBUF, &expbuf);
3085 if (errno == ENOTTY)
3086 ret = FALSE;
3087
3088 return ret;
3089}
3090
3091static gboolean
3092gst_aml_v4l2_object_setup_pool(GstAmlV4l2Object *v4l2object, GstCaps *caps)
3093{
3094 GstAmlV4l2IOMode mode;
3095
3096 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "initializing the %s system",
3097 V4L2_TYPE_IS_OUTPUT(v4l2object->type) ? "output" : "capture");
3098
3099 GST_AML_V4L2_CHECK_OPEN(v4l2object);
3100 GST_AML_V4L2_CHECK_NOT_ACTIVE(v4l2object);
3101
3102 /* find transport */
3103 mode = v4l2object->req_mode;
3104
3105 if (v4l2object->device_caps & V4L2_CAP_READWRITE)
3106 {
3107 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
3108 mode = GST_V4L2_IO_RW;
3109 }
3110 else if (v4l2object->req_mode == GST_V4L2_IO_RW)
3111 goto method_not_supported;
3112
3113 if (v4l2object->device_caps & V4L2_CAP_STREAMING)
3114 {
3115 if (v4l2object->req_mode == GST_V4L2_IO_AUTO)
3116 {
3117 if (!V4L2_TYPE_IS_OUTPUT(v4l2object->type) &&
3118 gst_aml_v4l2_object_is_dmabuf_supported(v4l2object))
3119 {
3120 mode = GST_V4L2_IO_DMABUF;
3121 }
3122 else
3123 {
3124 mode = GST_V4L2_IO_MMAP;
3125 }
3126 }
3127 }
3128 else if (v4l2object->req_mode == GST_V4L2_IO_MMAP ||
3129 v4l2object->req_mode == GST_V4L2_IO_DMABUF)
3130 goto method_not_supported;
3131
3132 /* if still no transport selected, error out */
3133 if (mode == GST_V4L2_IO_AUTO)
3134 goto no_supported_capture_method;
3135
3136 GST_INFO_OBJECT(v4l2object->dbg_obj, "accessing buffers via mode %d", mode);
3137 v4l2object->mode = mode;
3138
3139 /* If min_buffers is not set, the driver either does not support the control or
3140 it has not been asked yet via propose_allocation/decide_allocation. */
3141 if (!v4l2object->min_buffers)
3142 gst_aml_v4l2_get_driver_min_buffers(v4l2object);
3143
3144 /* Map the buffers */
3145 GST_LOG_OBJECT(v4l2object->dbg_obj, "initiating buffer pool");
3146
3147 if (!(v4l2object->pool = gst_aml_v4l2_buffer_pool_new(v4l2object, caps)))
3148 goto buffer_pool_new_failed;
3149
3150 GST_AML_V4L2_SET_ACTIVE(v4l2object);
3151
3152 return TRUE;
3153
3154 /* ERRORS */
3155buffer_pool_new_failed:
3156{
3157 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, READ,
3158 (_("Could not map buffers from device '%s'"),
3159 v4l2object->videodev),
3160 ("Failed to create buffer pool: %s", g_strerror(errno)));
3161 return FALSE;
3162}
3163method_not_supported:
3164{
3165 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, READ,
3166 (_("The driver of device '%s' does not support the IO method %d"),
3167 v4l2object->videodev, mode),
3168 (NULL));
3169 return FALSE;
3170}
3171no_supported_capture_method:
3172{
3173 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, READ,
3174 (_("The driver of device '%s' does not support any known IO "
3175 "method."),
3176 v4l2object->videodev),
3177 (NULL));
3178 return FALSE;
3179}
3180}
3181
3182static void
3183gst_aml_v4l2_object_set_stride(GstVideoInfo *info, GstVideoAlignment *align,
3184 gint plane, gint stride)
3185{
3186 const GstVideoFormatInfo *finfo = info->finfo;
3187
3188 if (GST_VIDEO_FORMAT_INFO_IS_TILED(finfo))
3189 {
3190 gint x_tiles, y_tiles, ws, hs, tile_height, padded_height;
3191
3192 ws = GST_VIDEO_FORMAT_INFO_TILE_WS(finfo);
3193 hs = GST_VIDEO_FORMAT_INFO_TILE_HS(finfo);
3194 tile_height = 1 << hs;
3195
3196 padded_height = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT(finfo, plane,
3197 info->height + align->padding_top + align->padding_bottom);
3198 padded_height = GST_ROUND_UP_N(padded_height, tile_height);
3199
3200 x_tiles = stride >> ws;
3201 y_tiles = padded_height >> hs;
3202 info->stride[plane] = GST_VIDEO_TILE_MAKE_STRIDE(x_tiles, y_tiles);
3203 }
3204 else
3205 {
3206 info->stride[plane] = stride;
3207 }
3208}
3209
3210static void
3211gst_aml_v4l2_object_extrapolate_info(GstAmlV4l2Object *v4l2object,
3212 GstVideoInfo *info, GstVideoAlignment *align, gint stride)
3213{
3214 const GstVideoFormatInfo *finfo = info->finfo;
3215 gint i, estride, padded_height;
3216 gsize offs = 0;
3217
3218 g_return_if_fail(v4l2object->n_v4l2_planes == 1);
3219
3220 padded_height = info->height + align->padding_top + align->padding_bottom;
3221
3222 for (i = 0; i < finfo->n_planes; i++)
3223 {
3224 estride = gst_aml_v4l2_object_extrapolate_stride(finfo, i, stride);
3225
3226 gst_aml_v4l2_object_set_stride(info, align, i, estride);
3227
3228 info->offset[i] = offs;
3229 offs += estride *
3230 GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT(finfo, i, padded_height);
3231
3232 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
3233 "Extrapolated for plane %d with base stride %d: "
3234 "stride %d, offset %" G_GSIZE_FORMAT,
3235 i, stride, info->stride[i],
3236 info->offset[i]);
3237 }
3238
3239 /* Update the image size according the amount of data we are going to
3240 * read/write. This workaround bugs in driver where the sizeimage provided
3241 * by TRY/S_FMT represent the buffer length (maximum size) rather then the expected
3242 * bytesused (buffer size). */
3243 if (offs < info->size)
3244 info->size = offs;
3245}
3246
3247static void
3248gst_aml_v4l2_object_save_format(GstAmlV4l2Object *v4l2object,
3249 struct v4l2_fmtdesc *fmtdesc, struct v4l2_format *format,
3250 GstVideoInfo *info, GstVideoAlignment *align)
3251{
3252 const GstVideoFormatInfo *finfo = info->finfo;
3253 gboolean standard_stride = TRUE;
3254 gint stride, pstride, padded_width, padded_height, i;
3255
3256 if (GST_VIDEO_INFO_FORMAT(info) == GST_VIDEO_FORMAT_ENCODED)
3257 {
3258 v4l2object->n_v4l2_planes = 1;
3259 info->size = format->fmt.pix.sizeimage;
3260 goto store_info;
3261 }
3262
3263 /* adjust right padding */
3264 if (V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type))
3265 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3266 else
3267 stride = format->fmt.pix.bytesperline;
3268
3269 pstride = GST_VIDEO_FORMAT_INFO_PSTRIDE(finfo, 0);
3270 if (pstride)
3271 {
3272 padded_width = stride / pstride;
3273 }
3274 else
3275 {
3276 /* pstride can be 0 for complex formats */
3277 GST_WARNING_OBJECT(v4l2object->element,
3278 "format %s has a pstride of 0, cannot compute padded with",
3279 gst_video_format_to_string(GST_VIDEO_INFO_FORMAT(info)));
3280 padded_width = stride;
3281 }
3282
3283 if (padded_width < format->fmt.pix.width)
3284 GST_WARNING_OBJECT(v4l2object->dbg_obj,
3285 "Driver bug detected, stride (%d) is too small for the width (%d)",
3286 padded_width, format->fmt.pix.width);
3287
3288 align->padding_right = padded_width - info->width - align->padding_left;
3289
3290 /* adjust bottom padding */
3291 padded_height = format->fmt.pix.height;
3292
3293 if (GST_VIDEO_FORMAT_INFO_IS_TILED(finfo))
3294 {
3295 guint hs, tile_height;
3296
3297 hs = GST_VIDEO_FORMAT_INFO_TILE_HS(finfo);
3298 tile_height = 1 << hs;
3299
3300 padded_height = GST_ROUND_UP_N(padded_height, tile_height);
3301 }
3302
3303 align->padding_bottom = padded_height - info->height - align->padding_top;
3304
3305 /* setup the strides and offset */
3306 if (V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type))
3307 {
3308 struct v4l2_pix_format_mplane *pix_mp = &format->fmt.pix_mp;
3309
3310 /* figure out the frame layout */
3311 v4l2object->n_v4l2_planes = MAX(1, pix_mp->num_planes);
3312 info->size = 0;
3313 for (i = 0; i < v4l2object->n_v4l2_planes; i++)
3314 {
3315 stride = pix_mp->plane_fmt[i].bytesperline;
3316
3317 if (info->stride[i] != stride)
3318 standard_stride = FALSE;
3319
3320 gst_aml_v4l2_object_set_stride(info, align, i, stride);
3321 info->offset[i] = info->size;
3322 info->size += pix_mp->plane_fmt[i].sizeimage;
3323 }
3324
3325 /* Extrapolate stride if planar format are being set in 1 v4l2 plane */
3326 if (v4l2object->n_v4l2_planes < finfo->n_planes)
3327 {
3328 stride = format->fmt.pix_mp.plane_fmt[0].bytesperline;
3329 gst_aml_v4l2_object_extrapolate_info(v4l2object, info, align, stride);
3330 }
3331 }
3332 else
3333 {
3334 /* only one plane in non-MPLANE mode */
3335 v4l2object->n_v4l2_planes = 1;
3336 info->size = format->fmt.pix.sizeimage;
3337 stride = format->fmt.pix.bytesperline;
3338
3339 if (info->stride[0] != stride)
3340 standard_stride = FALSE;
3341
3342 gst_aml_v4l2_object_extrapolate_info(v4l2object, info, align, stride);
3343 }
3344
3345 /* adjust the offset to take into account left and top */
3346 if (GST_VIDEO_FORMAT_INFO_IS_TILED(finfo))
3347 {
3348 if ((align->padding_left + align->padding_top) > 0)
3349 GST_WARNING_OBJECT(v4l2object->dbg_obj,
3350 "Left and top padding is not permitted for tiled formats");
3351 }
3352 else
3353 {
3354 for (i = 0; i < finfo->n_planes; i++)
3355 {
3356 gint vedge, hedge;
3357
3358 /* FIXME we assume plane as component as this is true for all supported
3359 * format we support. */
3360
3361 hedge = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH(finfo, i, align->padding_left);
3362 vedge = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT(finfo, i, align->padding_top);
3363
3364 info->offset[i] += (vedge * info->stride[i]) +
3365 (hedge * GST_VIDEO_INFO_COMP_PSTRIDE(info, i));
3366 }
3367 }
3368
3369store_info:
3370 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got sizeimage %" G_GSIZE_FORMAT,
3371 info->size);
3372
3373 /* to avoid copies we need video meta if there is padding */
3374 v4l2object->need_video_meta =
3375 ((align->padding_top + align->padding_left + align->padding_right +
3376 align->padding_bottom) != 0);
3377
3378 /* ... or if stride is non "standard" */
3379 if (!standard_stride)
3380 v4l2object->need_video_meta = TRUE;
3381
3382 /* ... or also video meta if we use multiple, non-contiguous, planes */
3383 if (v4l2object->n_v4l2_planes > 1)
3384 v4l2object->need_video_meta = TRUE;
3385
3386 v4l2object->info = *info;
3387 v4l2object->align = *align;
3388 v4l2object->format = *format;
3389 v4l2object->fmtdesc = fmtdesc;
3390
3391 /* if we have a framerate pre-calculate duration */
3392 if (info->fps_n > 0 && info->fps_d > 0)
3393 {
3394 v4l2object->duration = gst_util_uint64_scale_int(GST_SECOND, info->fps_d,
3395 info->fps_n);
3396 }
3397 else
3398 {
3399 v4l2object->duration = GST_CLOCK_TIME_NONE;
3400 }
3401}
3402
3403gint gst_aml_v4l2_object_extrapolate_stride(const GstVideoFormatInfo *finfo,
3404 gint plane, gint stride)
3405{
3406 gint estride;
3407
3408 switch (finfo->format)
3409 {
3410 case GST_VIDEO_FORMAT_NV12:
3411 case GST_VIDEO_FORMAT_NV12_64Z32:
3412 case GST_VIDEO_FORMAT_NV21:
3413 case GST_VIDEO_FORMAT_NV16:
3414 case GST_VIDEO_FORMAT_NV61:
3415 case GST_VIDEO_FORMAT_NV24:
3416 estride = (plane == 0 ? 1 : 2) *
3417 GST_VIDEO_FORMAT_INFO_SCALE_WIDTH(finfo, plane, stride);
3418 break;
3419 default:
3420 estride = GST_VIDEO_FORMAT_INFO_SCALE_WIDTH(finfo, plane, stride);
3421 break;
3422 }
3423
3424 return estride;
3425}
3426
3427static gboolean
3428gst_aml_v4l2_video_colorimetry_matches(const GstVideoColorimetry *cinfo,
3429 const gchar *color)
3430{
3431 GstVideoColorimetry ci;
3432 static const GstVideoColorimetry ci_likely_jpeg = {
3433 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3434 GST_VIDEO_TRANSFER_UNKNOWN, GST_VIDEO_COLOR_PRIMARIES_UNKNOWN};
3435 static const GstVideoColorimetry ci_jpeg = {
3436 GST_VIDEO_COLOR_RANGE_0_255, GST_VIDEO_COLOR_MATRIX_BT601,
3437 GST_VIDEO_TRANSFER_SRGB, GST_VIDEO_COLOR_PRIMARIES_BT709};
3438
3439 if (!gst_video_colorimetry_from_string(&ci, color))
3440 return FALSE;
3441
3442 if (gst_video_colorimetry_is_equal(&ci, cinfo))
3443 return TRUE;
3444
3445 /* Allow 1:4:0:0 (produced by jpegdec) if the device expects 1:4:7:1 */
3446 if (gst_video_colorimetry_is_equal(&ci, &ci_likely_jpeg) && gst_video_colorimetry_is_equal(cinfo, &ci_jpeg))
3447 return TRUE;
3448
3449 return FALSE;
3450}
3451
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003452static gboolean needSpecConfigForFg(GstAmlV4l2Object *v4l2object)
3453{
3454 gboolean result= FALSE;
3455 int fd= -1;
3456 char valstr[64];
3457 const char* path= "/sys/class/video/film_grain_support";
3458 uint32_t val= 0;
3459 int rc;
3460 struct v4l2_control ctl;
3461
3462 GST_LOG("configForFilmGrain: enter");
3463 fd= open(path, O_RDONLY|O_CLOEXEC);
3464 if ( fd < 0 )
3465 {
3466 GST_DEBUG("unable to open file (%s)", path);
3467 goto exit;
3468 }
3469
3470 memset(valstr, 0, sizeof(valstr));
3471 read(fd, valstr, sizeof(valstr) - 1);
3472 valstr[strlen(valstr)] = '\0';
3473
3474 if ( sscanf(valstr, "%u", &val) < 1)
3475 {
3476 GST_DEBUG("unable to get flag from: (%s)", valstr);
3477 goto exit;
3478 }
3479
3480 GST_LOG("got film_grain_support:%d from node", val);
3481 if (val != 0)
3482 {
3483 goto exit;
3484 }
3485
3486 memset( &ctl, 0, sizeof(ctl));
3487 ctl.id= AML_V4L2_GET_FILMGRAIN_INFO;
3488 v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_CTRL, ctl);
3489 GST_LOG("got VIDIOC_G_CTRL value: %d", ctl.value);
3490 if (ctl.value == 0)
3491 {
3492 goto exit;
3493 }
3494
3495 result= TRUE;
3496
3497exit:
3498 if ( fd >= 0 )
3499 {
3500 close(fd);
3501 }
3502 GST_LOG("configForFilmGrain: exit: result %d", result);
3503 return result;
3504}
xuesong.jiangae1548e2022-05-06 16:38:46 +08003505static void
fei.deng7c3d67f2022-11-09 11:06:05 +08003506set_amlogic_vdec_parm(GstAmlV4l2Object *v4l2object, struct v4l2_streamparm *streamparm, GstCaps *caps, guint32 pixFormat)
xuesong.jiangae1548e2022-05-06 16:38:46 +08003507{
3508 struct aml_dec_params *decParm = (struct aml_dec_params *)streamparm->parm.raw_data;
3509 const char *env;
zengliang.lic9f869d2023-02-15 08:32:32 +00003510 struct v4l2_ext_control control;
3511 struct v4l2_ext_controls ctrls;
3512 gboolean use_ext_config = FALSE;
3513 int major = 0,minor = 0;
3514 struct utsname info;
xuesong.jiangae1548e2022-05-06 16:38:46 +08003515 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
3516 {
fei.dengccc89632022-07-15 19:10:17 +08003517 /*set bit12 value to 1,
3518 *v4l2 output 0 pts of second interlace field frame */
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003519 decParm->cfg.metadata_config_flag |= (1 << 12);
fei.deng7c3d67f2022-11-09 11:06:05 +08003520 decParm->parms_status = V4L2_CONFIG_PARM_DECODE_CFGINFO;
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003521
3522 decParm->cfg.metadata_config_flag |= 1 << 13;
3523
3524 /*set bit18 value to 1
3525 *release vpp in advance */
3526 decParm->cfg.metadata_config_flag |= (1 << 18);
3527 decParm->cfg.low_latency_mode = v4l2object->low_latency_mode;
3528
fei.deng7c3d67f2022-11-09 11:06:05 +08003529 switch (pixFormat)
3530 {
3531 default:
3532 case V4L2_PIX_FMT_MPEG:
3533 case V4L2_PIX_FMT_H264:
3534 decParm->cfg.double_write_mode= VDEC_DW_NO_AFBC;
3535 break;
3536 case V4L2_PIX_FMT_HEVC:
3537 case V4L2_PIX_FMT_VP9:
3538 case V4L2_PIX_FMT_AV1:
zengliang.lib1725ae2023-03-08 03:20:21 +00003539 decParm->cfg.double_write_mode= VDEC_DW_AFBC_AUTO_1_4;
fei.deng7c3d67f2022-11-09 11:06:05 +08003540 break;
3541 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08003542 env = getenv("V4L2_SET_AMLOGIC_DW_MODE");
3543 if (env)
3544 {
3545 int dwMode = atoi(env);
3546 switch (dwMode)
3547 {
3548 case 0:
3549 case 1:
3550 case 2:
3551 case 3:
3552 case 4:
3553 case 16:
zengliang.lib1725ae2023-03-08 03:20:21 +00003554 case 256:
3555 case 512:
xuesong.jiangae1548e2022-05-06 16:38:46 +08003556 decParm->cfg.double_write_mode = dwMode;
xuesong.jiangae1548e2022-05-06 16:38:46 +08003557 break;
3558 }
3559 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08003560 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "cfg dw mode to %d", decParm->cfg.double_write_mode);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08003561 decParm->cfg.ref_buf_margin = GST_AML_V4L2_DEFAULT_CAP_BUF_MARGIN;
xuesong.jiangae1548e2022-05-06 16:38:46 +08003562
xuesong.jiange1a19662022-06-21 20:30:22 +08003563 GstStructure *structure= gst_caps_get_structure(caps, 0);
3564 if (structure == NULL)
3565 {
3566 return;
3567 }
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003568
3569 // dv
3570 gboolean dv_bl_present_flag, dv_el_present_flag;
3571 int dvBaseLayerPresent = -1;
3572 int dvEnhancementLayerPresent = -1;
3573 /* have base and enhancement layers both, that means its dual layer,
3574 dv two layer flag will be true */
3575 if (gst_structure_get_boolean( structure, "dv_bl_present_flag", &dv_bl_present_flag))
3576 {
3577 dvBaseLayerPresent= dv_bl_present_flag?1:0;
3578 }
3579 if (gst_structure_get_boolean( structure, "dv_el_present_flag", &dv_el_present_flag))
3580 {
3581 dvEnhancementLayerPresent= dv_el_present_flag?1:0;
3582 }
3583
3584 /* have base and enhancement layers both, that means its dual layer, dv two layer flag will be true */
3585 if ( (dvBaseLayerPresent == 1) && (dvEnhancementLayerPresent == 1) )
3586 {
3587 decParm->cfg.metadata_config_flag |= (1 << 0);
3588 }
3589 else
3590 {
3591 decParm->cfg.metadata_config_flag |= (0 << 0);
3592 }
3593
3594 /* have one of then, it's standard dv stream, Non-standard dv flag will be false */
3595 if ( (dvBaseLayerPresent == 0) && (dvEnhancementLayerPresent == 0) )
3596 {
3597 decParm->cfg.metadata_config_flag |= (1 << 1);
3598 }
3599 else
3600 {
3601 decParm->cfg.metadata_config_flag |= (0 << 1);
3602 }
3603
3604 // HDR
xuesong.jiange1a19662022-06-21 20:30:22 +08003605 if ( gst_structure_has_field(structure, "colorimetry") )
3606 {
3607 const char *colorimetry= gst_structure_get_string(structure,"colorimetry");
3608 GstVideoColorimetry vci = {0};
3609 if ( colorimetry && gst_video_colorimetry_from_string( &vci, colorimetry ))
3610 {
3611 decParm->parms_status |= V4L2_CONFIG_PARM_DECODE_HDRINFO;
3612 decParm->hdr.signal_type= (1<<29); /* present flag */
3613 /*set default value, this is to keep up with driver hdr info synchronization*/
3614 decParm->hdr.signal_type |= (5<<26) | (1<<24);
3615
3616 gint hdrColorimetry[4] = {0};
3617 hdrColorimetry[0]= (int)vci.range;
3618 hdrColorimetry[1]= (int)vci.matrix;
3619 hdrColorimetry[2]= (int)vci.transfer;
3620 hdrColorimetry[3]= (int)vci.primaries;
3621 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "colorimetry: [%d,%d,%d,%d]",
3622 hdrColorimetry[0],
3623 hdrColorimetry[1],
3624 hdrColorimetry[2],
3625 hdrColorimetry[3] );
3626 /* range */
3627 switch ( hdrColorimetry[0] )
3628 {
le.han8d28eb82024-06-05 08:11:12 +00003629 case GST_VIDEO_COLOR_RANGE_0_255:
3630 case GST_VIDEO_COLOR_RANGE_16_235:
xuesong.jiange1a19662022-06-21 20:30:22 +08003631 decParm->hdr.signal_type |= ((hdrColorimetry[0] % 2)<<25);
3632 break;
3633 default:
3634 break;
3635 }
3636 /* matrix coefficient */
3637 switch ( hdrColorimetry[1] )
3638 {
le.han8d28eb82024-06-05 08:11:12 +00003639 case GST_VIDEO_COLOR_MATRIX_RGB: /* RGB */
xuesong.jiange1a19662022-06-21 20:30:22 +08003640 decParm->hdr.signal_type |= 0;
3641 break;
le.han8d28eb82024-06-05 08:11:12 +00003642 case GST_VIDEO_COLOR_MATRIX_FCC: /* FCC */
xuesong.jiange1a19662022-06-21 20:30:22 +08003643 decParm->hdr.signal_type |= 4;
3644 break;
le.han8d28eb82024-06-05 08:11:12 +00003645 case GST_VIDEO_COLOR_MATRIX_BT709: /* BT709 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003646 decParm->hdr.signal_type |= 1;
3647 break;
le.han8d28eb82024-06-05 08:11:12 +00003648 case GST_VIDEO_COLOR_MATRIX_BT601: /* BT601 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003649 decParm->hdr.signal_type |= 3;
3650 break;
le.han8d28eb82024-06-05 08:11:12 +00003651 case GST_VIDEO_COLOR_MATRIX_SMPTE240M: /* SMPTE240M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003652 decParm->hdr.signal_type |= 7;
3653 break;
le.han8d28eb82024-06-05 08:11:12 +00003654 case GST_VIDEO_COLOR_MATRIX_BT2020: /* BT2020 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003655 decParm->hdr.signal_type |= 9;
3656 break;
3657 default: /* unknown */
3658 decParm->hdr.signal_type |= 2;
3659 break;
3660 }
3661 /* transfer function */
3662 switch ( hdrColorimetry[2] )
3663 {
le.han8d28eb82024-06-05 08:11:12 +00003664 case GST_VIDEO_TRANSFER_BT709: /* BT709 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003665 decParm->hdr.signal_type |= (1<<8);
3666 break;
le.han8d28eb82024-06-05 08:11:12 +00003667 case GST_VIDEO_TRANSFER_SMPTE240M: /* SMPTE240M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003668 decParm->hdr.signal_type |= (7<<8);
3669 break;
le.han8d28eb82024-06-05 08:11:12 +00003670 case GST_VIDEO_TRANSFER_LOG100: /* LOG100 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003671 decParm->hdr.signal_type |= (9<<8);
3672 break;
le.han8d28eb82024-06-05 08:11:12 +00003673 case GST_VIDEO_TRANSFER_LOG316: /* LOG316 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003674 decParm->hdr.signal_type |= (10<<8);
3675 break;
le.han8d28eb82024-06-05 08:11:12 +00003676 case GST_VIDEO_TRANSFER_BT2020_12: /* BT2020_12 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003677 decParm->hdr.signal_type |= (15<<8);
3678 break;
le.han8d28eb82024-06-05 08:11:12 +00003679 case GST_VIDEO_TRANSFER_BT2020_10: /* BT2020_10 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003680 decParm->hdr.signal_type |= (14<<8);
3681 break;
le.han8d28eb82024-06-05 08:11:12 +00003682 #if ((GST_VERSION_MAJOR == 1) && (GST_VERSION_MINOR >= 18))
3683 case GST_VIDEO_TRANSFER_SMPTE2084: /* SMPTE2084 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003684 decParm->hdr.signal_type |= (16<<8);
3685 break;
le.han8d28eb82024-06-05 08:11:12 +00003686 #else
3687 case GST_VIDEO_TRANSFER_SMPTE_ST_2084: /* SMPTE2084 */
3688 decParm->hdr.signal_type |= (16<<8);
3689 break;
3690 #endif
3691 case GST_VIDEO_TRANSFER_ARIB_STD_B67: /* ARIB_STD_B67 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003692 decParm->hdr.signal_type |= (18<<8);
3693 break;
3694 #if ((GST_VERSION_MAJOR == 1) && (GST_VERSION_MINOR >= 18))
le.han8d28eb82024-06-05 08:11:12 +00003695 case GST_VIDEO_TRANSFER_BT601: /* BT601 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003696 decParm->hdr.signal_type |= (3<<8);
3697 break;
3698 #endif
le.han8d28eb82024-06-05 08:11:12 +00003699 case GST_VIDEO_TRANSFER_GAMMA10: /* GAMMA10 */
3700 case GST_VIDEO_TRANSFER_GAMMA18: /* GAMMA18 */
3701 case GST_VIDEO_TRANSFER_GAMMA20: /* GAMMA20 */
3702 case GST_VIDEO_TRANSFER_GAMMA22: /* GAMMA22 */
3703 case GST_VIDEO_TRANSFER_SRGB: /* SRGB */
3704 case GST_VIDEO_TRANSFER_GAMMA28: /* GAMMA28 */
3705 case GST_VIDEO_TRANSFER_ADOBERGB: /* ADOBERGB */
xuesong.jiange1a19662022-06-21 20:30:22 +08003706 default:
3707 break;
3708 }
3709 /* primaries */
3710 switch ( hdrColorimetry[3] )
3711 {
le.han8d28eb82024-06-05 08:11:12 +00003712 case GST_VIDEO_COLOR_PRIMARIES_BT709: /* BT709 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003713 decParm->hdr.signal_type |= ((1<<24)|(1<<16));
3714 break;
le.han8d28eb82024-06-05 08:11:12 +00003715 case GST_VIDEO_COLOR_PRIMARIES_BT470M: /* BT470M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003716 decParm->hdr.signal_type |= ((1<<24)|(4<<16));
3717 break;
le.han8d28eb82024-06-05 08:11:12 +00003718 case GST_VIDEO_COLOR_PRIMARIES_BT470BG: /* BT470BG */
xuesong.jiange1a19662022-06-21 20:30:22 +08003719 decParm->hdr.signal_type |= ((1<<24)|(5<<16));
3720 break;
le.han8d28eb82024-06-05 08:11:12 +00003721 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M: /* SMPTE170M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003722 decParm->hdr.signal_type |= ((1<<24)|(6<<16));
3723 break;
le.han8d28eb82024-06-05 08:11:12 +00003724 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M: /* SMPTE240M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003725 decParm->hdr.signal_type |= ((1<<24)|(7<<16));
3726 break;
le.han8d28eb82024-06-05 08:11:12 +00003727 case GST_VIDEO_COLOR_PRIMARIES_FILM: /* FILM */
xuesong.jiange1a19662022-06-21 20:30:22 +08003728 decParm->hdr.signal_type |= ((1<<24)|(8<<16));
3729 break;
le.han8d28eb82024-06-05 08:11:12 +00003730 case GST_VIDEO_COLOR_PRIMARIES_BT2020: /* BT2020 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003731 decParm->hdr.signal_type |= ((1<<24)|(9<<16));
3732 break;
le.han8d28eb82024-06-05 08:11:12 +00003733 case GST_VIDEO_COLOR_PRIMARIES_ADOBERGB: /* ADOBERGB */
xuesong.jiange1a19662022-06-21 20:30:22 +08003734 default:
3735 break;
3736 }
3737 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR signal_type %X", decParm->hdr.signal_type);
3738 }
3739
3740 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "got caps %" GST_PTR_FORMAT, caps);
3741 GstStructure *st = gst_caps_get_structure(caps, 0);
3742 GstCapsFeatures *features = gst_caps_get_features(caps, 0);
3743
3744 if (gst_structure_has_field(st, "colorimetry"))
3745 {
3746 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "have colorimetry");
3747 }
3748
3749 if (st && features)
3750 {
3751 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "trace in remove colorimetry");
3752 gst_structure_remove_field(st, "colorimetry");
3753 gst_caps_features_remove(features, "colorimetry");
3754 }
3755 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "caps after remove colorimetry %" GST_PTR_FORMAT, caps);
3756 }
3757
le.han7d0af792024-06-05 08:44:18 +00003758 const char * field = NULL;
3759 if (gst_structure_has_field(structure, "mastering-display-metadata")) {
3760 field = "mastering-display-metadata";
3761 } else if (gst_structure_has_field(structure, "mastering-display-info")) {
3762 field = "mastering-display-info";
3763 }
3764
3765 if ( field )
xuesong.jiange1a19662022-06-21 20:30:22 +08003766 {
le.han7d0af792024-06-05 08:44:18 +00003767 const char *masteringDisplay= gst_structure_get_string(structure, field);
xuesong.jiange1a19662022-06-21 20:30:22 +08003768 float hdrMasteringDisplay[10];
3769 if ( masteringDisplay && sscanf( masteringDisplay, "%f:%f:%f:%f:%f:%f:%f:%f:%f:%f",
3770 &hdrMasteringDisplay[0],
3771 &hdrMasteringDisplay[1],
3772 &hdrMasteringDisplay[2],
3773 &hdrMasteringDisplay[3],
3774 &hdrMasteringDisplay[4],
3775 &hdrMasteringDisplay[5],
3776 &hdrMasteringDisplay[6],
3777 &hdrMasteringDisplay[7],
3778 &hdrMasteringDisplay[8],
3779 &hdrMasteringDisplay[9] ) == 10 )
3780 {
3781 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "mastering display [%f,%f,%f,%f,%f,%f,%f,%f,%f,%f]",
3782 hdrMasteringDisplay[0],
3783 hdrMasteringDisplay[1],
3784 hdrMasteringDisplay[2],
3785 hdrMasteringDisplay[3],
3786 hdrMasteringDisplay[4],
3787 hdrMasteringDisplay[5],
3788 hdrMasteringDisplay[6],
3789 hdrMasteringDisplay[7],
3790 hdrMasteringDisplay[8],
3791 hdrMasteringDisplay[9] );
3792
3793 decParm->hdr.color_parms.present_flag= 1;
3794 decParm->hdr.color_parms.primaries[2][0]= (uint32_t)(hdrMasteringDisplay[0]*50000); /* R.x */
3795 decParm->hdr.color_parms.primaries[2][1]= (uint32_t)(hdrMasteringDisplay[1]*50000); /* R.y */
3796 decParm->hdr.color_parms.primaries[0][0]= (uint32_t)(hdrMasteringDisplay[2]*50000); /* G.x */
3797 decParm->hdr.color_parms.primaries[0][1]= (uint32_t)(hdrMasteringDisplay[3]*50000); /* G.y */
3798 decParm->hdr.color_parms.primaries[1][0]= (uint32_t)(hdrMasteringDisplay[4]*50000); /* B.x */
3799 decParm->hdr.color_parms.primaries[1][1]= (uint32_t)(hdrMasteringDisplay[5]*50000); /* B.y */
3800 decParm->hdr.color_parms.white_point[0]= (uint32_t)(hdrMasteringDisplay[6]*50000);
3801 decParm->hdr.color_parms.white_point[1]= (uint32_t)(hdrMasteringDisplay[7]*50000);
3802 decParm->hdr.color_parms.luminance[0]= (uint32_t)(hdrMasteringDisplay[8]);
3803 decParm->hdr.color_parms.luminance[1]= (uint32_t)(hdrMasteringDisplay[9]);
3804 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: primaries %X %X %X %X %X %X",
3805 decParm->hdr.color_parms.primaries[2][0],
3806 decParm->hdr.color_parms.primaries[2][1],
3807 decParm->hdr.color_parms.primaries[0][0],
3808 decParm->hdr.color_parms.primaries[0][1],
3809 decParm->hdr.color_parms.primaries[1][0],
3810 decParm->hdr.color_parms.primaries[1][1] );
3811 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: white point: %X %X",
3812 decParm->hdr.color_parms.white_point[0],
3813 decParm->hdr.color_parms.white_point[1] );
3814 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: luminance: %X %X",
3815 decParm->hdr.color_parms.luminance[0],
3816 decParm->hdr.color_parms.luminance[1] );
3817 }
3818
3819 GstStructure *st = gst_caps_get_structure(caps, 0);
3820 GstCapsFeatures * features = gst_caps_get_features(caps, 0);
3821 if (st && features)
3822 {
le.han7d0af792024-06-05 08:44:18 +00003823 gst_structure_remove_fields(st, field, NULL);
3824 gst_caps_features_remove(features, field);
xuesong.jiange1a19662022-06-21 20:30:22 +08003825 }
le.han7d0af792024-06-05 08:44:18 +00003826 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "caps after remove %s %" GST_PTR_FORMAT, field, caps);
xuesong.jiange1a19662022-06-21 20:30:22 +08003827 }
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003828
3829 if (uname(&info) || sscanf(info.release, "%d.%d", &major, &minor) <= 0)
3830 {
3831 GST_DEBUG("get linux version failed");
3832 }
3833 GST_DEBUG("linux major version %d %d", major,minor);
3834
3835 use_ext_config = ((major == 5 && minor >= 15) || major >= 6) ? TRUE: FALSE;
3836
3837 if (use_ext_config)
3838 {
3839 memset(&ctrls, 0, sizeof(ctrls));
3840 memset(&control, 0, sizeof(control));
3841 control.id = AML_V4L2_DEC_PARMS_CONFIG;
3842 control.ptr = decParm;
3843 control.size = sizeof(struct aml_dec_params);
3844 ctrls.count = 1;
3845 ctrls.controls = &control;
3846 if (v4l2object->ioctl( v4l2object->video_fd, VIDIOC_S_EXT_CTRLS, &ctrls ) <0)
3847 {
3848 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set vdec parm fail");
3849 }
3850 else
3851 {
3852 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set dwMode to %d, margin to %d", decParm->cfg.double_write_mode, decParm->cfg.ref_buf_margin);
3853 }
3854 }
3855 else
3856 {
3857 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_PARM, streamparm) < 0)
3858 {
3859 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set vdec parm fail");
3860 }
3861 else
3862 {
3863 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Set dwMode to %d, margin to %d", decParm->cfg.double_write_mode, decParm->cfg.ref_buf_margin);
3864 }
3865 }
3866 }
3867 else if (v4l2object->type == V4L2_BUF_TYPE_VBI_CAPTURE || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
3868 {
3869 if (needSpecConfigForFg(v4l2object))
3870 {
3871 decParm->cfg.double_write_mode= VDEC_DW_MMU_1;
3872 GST_DEBUG_OBJECT(v4l2object->dbg_obj,"set fg dw mode %d", decParm->cfg.double_write_mode);
3873 }
3874 }
3875 else
3876 {
3877 GST_ERROR_OBJECT(v4l2object->dbg_obj,"can't deal with. please check flow");
xuesong.jiangae1548e2022-05-06 16:38:46 +08003878 }
3879}
3880
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08003881static gint gst_aml_v4l2_object_get_dw_mode(GstAmlV4l2Object *v4l2object)
3882{
3883 struct v4l2_streamparm streamparm;
3884 struct aml_dec_params *decParm = (struct aml_dec_params *)(&streamparm.parm.raw_data);
3885 memset(&streamparm, 0x00, sizeof(struct v4l2_streamparm));
3886
3887 streamparm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3888 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_PARM, &streamparm) >= 0)
3889 {
3890 GST_DEBUG_OBJECT(v4l2object, "get dw mode:%d in type V4L2_BUF_TYPE_VIDEO_OUTPUT", decParm->cfg.double_write_mode);
3891 return decParm->cfg.double_write_mode;
3892 }
3893
3894 streamparm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
3895 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_PARM, &streamparm) >= 0)
3896 {
3897 GST_DEBUG_OBJECT(v4l2object, "get dw mode:%d in type V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE", decParm->cfg.double_write_mode);
3898 return decParm->cfg.double_write_mode;
3899 }
3900
3901 GST_ERROR_OBJECT(v4l2object, "can't get dw mode in type V4L2_BUF_TYPE_VIDEO_OUTPUT or V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE ret -1");
3902 return -1;
3903}
3904
xuesong.jiangae1548e2022-05-06 16:38:46 +08003905static gboolean
3906gst_aml_v4l2_object_set_format_full(GstAmlV4l2Object *v4l2object, GstCaps *caps,
3907 gboolean try_only, GstAmlV4l2Error *error)
3908{
3909 gint fd = v4l2object->video_fd;
3910 struct v4l2_format format;
3911 struct v4l2_streamparm streamparm;
3912 enum v4l2_field field;
3913 guint32 pixelformat;
3914 struct v4l2_fmtdesc *fmtdesc;
3915 GstVideoInfo info;
3916 GstVideoAlignment align;
3917 gint width, height, fps_n, fps_d;
3918 gint n_v4l_planes;
3919 gint i = 0;
3920 gboolean is_mplane;
3921 enum v4l2_colorspace colorspace = 0;
3922 enum v4l2_quantization range = 0;
3923 enum v4l2_ycbcr_encoding matrix = 0;
3924 enum v4l2_xfer_func transfer = 0;
3925 GstStructure *s;
3926 gboolean disable_colorimetry = FALSE;
3927
3928 g_return_val_if_fail(!v4l2object->skip_try_fmt_probes ||
3929 gst_caps_is_writable(caps),
3930 FALSE);
3931
3932 GST_AML_V4L2_CHECK_OPEN(v4l2object);
3933 if (!try_only)
3934 GST_AML_V4L2_CHECK_NOT_ACTIVE(v4l2object);
3935
xuesong.jiangae1548e2022-05-06 16:38:46 +08003936 is_mplane = V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type);
3937
3938 gst_video_info_init(&info);
3939 gst_video_alignment_reset(&align);
3940
3941 if (!gst_aml_v4l2_object_get_caps_info(v4l2object, caps, &fmtdesc, &info))
3942 goto invalid_caps;
3943
3944 pixelformat = fmtdesc->pixelformat;
3945 width = GST_VIDEO_INFO_WIDTH(&info);
3946 height = GST_VIDEO_INFO_HEIGHT(&info);
3947 fps_n = GST_VIDEO_INFO_FPS_N(&info);
3948 fps_d = GST_VIDEO_INFO_FPS_D(&info);
3949
hanghang.luo9edfc7d2023-05-17 07:01:05 +00003950 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Check image size");
3951 struct v4l2_frmsizeenum size;
3952 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
3953 size.index = 0;
3954 size.pixel_format = pixelformat;
3955 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
3956 return FALSE;
3957 if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE)
3958 {
3959 guint32 maxw, maxh;
3960 maxw = MIN (size.stepwise.max_width, G_MAXINT);
3961 maxh = MIN (size.stepwise.max_height, G_MAXINT);
3962 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "image from caps w_h:%d_%d", width, height);
3963 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "v4l2 support max w_h:%d_%d", maxw, maxh);
3964 if (width*height > maxw*maxh)
3965 return FALSE;
3966 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Check image size ok");
3967 }
3968
fei.deng7c3d67f2022-11-09 11:06:05 +08003969 //set amlogic params here,because we need pix format to set dw mode
3970 memset(&streamparm, 0x00, sizeof(struct v4l2_streamparm));
3971 streamparm.type = v4l2object->type;
3972 set_amlogic_vdec_parm(v4l2object, &streamparm, caps, pixelformat);
3973
xuesong.jiangae1548e2022-05-06 16:38:46 +08003974 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3975 * or if contiguous is prefered */
3976 n_v4l_planes = GST_VIDEO_INFO_N_PLANES(&info);
3977 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3978 n_v4l_planes = 1;
3979
3980 if (GST_VIDEO_INFO_IS_INTERLACED(&info))
3981 {
3982 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "interlaced video");
3983 /* ideally we would differentiate between types of interlaced video
3984 * but there is not sufficient information in the caps..
3985 */
3986 field = V4L2_FIELD_INTERLACED;
3987 }
3988 else
3989 {
3990 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "progressive video");
3991 field = V4L2_FIELD_NONE;
3992 }
3993
3994 /* We first pick the main colorspace from the primaries */
3995 switch (info.colorimetry.primaries)
3996 {
3997 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3998 /* There is two colorspaces using these primaries, use the range to
3999 * differentiate */
4000 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
4001 colorspace = V4L2_COLORSPACE_REC709;
4002 else
4003 colorspace = V4L2_COLORSPACE_SRGB;
4004 break;
4005 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
4006 colorspace = V4L2_COLORSPACE_BT2020;
4007 break;
4008 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
4009 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
4010 break;
4011 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4012 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
4013 break;
4014 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4015 colorspace = V4L2_COLORSPACE_SMPTE170M;
4016 break;
4017 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4018 colorspace = V4L2_COLORSPACE_SMPTE240M;
4019 break;
4020
4021 case GST_VIDEO_COLOR_PRIMARIES_FILM:
4022 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4023 /* We don't know, we will guess */
4024 break;
4025
4026 default:
4027 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4028 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
4029 break;
4030 }
4031
4032 switch (info.colorimetry.range)
4033 {
4034 case GST_VIDEO_COLOR_RANGE_0_255:
4035 range = V4L2_QUANTIZATION_FULL_RANGE;
4036 break;
4037 case GST_VIDEO_COLOR_RANGE_16_235:
4038 range = V4L2_QUANTIZATION_LIM_RANGE;
4039 break;
4040 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
4041 /* We let the driver pick a default one */
4042 break;
4043 default:
4044 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4045 "Unknown colorimetry range %d", info.colorimetry.range);
4046 break;
4047 }
4048
4049 switch (info.colorimetry.matrix)
4050 {
4051 case GST_VIDEO_COLOR_MATRIX_RGB:
4052 /* Unspecified, leave to default */
4053 break;
4054 /* FCC is about the same as BT601 with less digit */
4055 case GST_VIDEO_COLOR_MATRIX_FCC:
4056 case GST_VIDEO_COLOR_MATRIX_BT601:
4057 matrix = V4L2_YCBCR_ENC_601;
4058 break;
4059 case GST_VIDEO_COLOR_MATRIX_BT709:
4060 matrix = V4L2_YCBCR_ENC_709;
4061 break;
4062 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4063 matrix = V4L2_YCBCR_ENC_SMPTE240M;
4064 break;
4065 case GST_VIDEO_COLOR_MATRIX_BT2020:
4066 matrix = V4L2_YCBCR_ENC_BT2020;
4067 break;
4068 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4069 /* We let the driver pick a default one */
4070 break;
4071 default:
4072 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4073 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
4074 break;
4075 }
4076
4077 switch (info.colorimetry.transfer)
4078 {
4079 case GST_VIDEO_TRANSFER_GAMMA18:
4080 case GST_VIDEO_TRANSFER_GAMMA20:
4081 case GST_VIDEO_TRANSFER_GAMMA22:
4082 case GST_VIDEO_TRANSFER_GAMMA28:
4083 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4084 "GAMMA 18, 20, 22, 28 transfer functions not supported");
4085 /* fallthrough */
4086 case GST_VIDEO_TRANSFER_GAMMA10:
4087 transfer = V4L2_XFER_FUNC_NONE;
4088 break;
4089 case GST_VIDEO_TRANSFER_BT2020_12:
4090 case GST_VIDEO_TRANSFER_BT709:
4091 transfer = V4L2_XFER_FUNC_709;
4092 break;
4093 case GST_VIDEO_TRANSFER_SMPTE240M:
4094 transfer = V4L2_XFER_FUNC_SMPTE240M;
4095 break;
4096 case GST_VIDEO_TRANSFER_SRGB:
4097 transfer = V4L2_XFER_FUNC_SRGB;
4098 break;
4099 case GST_VIDEO_TRANSFER_LOG100:
4100 case GST_VIDEO_TRANSFER_LOG316:
4101 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4102 "LOG 100, 316 transfer functions not supported");
4103 /* FIXME No known sensible default, maybe AdobeRGB ? */
4104 break;
4105 case GST_VIDEO_TRANSFER_UNKNOWN:
4106 /* We let the driver pick a default one */
4107 break;
4108 default:
4109 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4110 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
4111 break;
4112 }
4113
4114 if (colorspace == 0)
4115 {
4116 /* Try to guess colorspace according to pixelformat and size */
4117 if (GST_VIDEO_INFO_IS_YUV(&info))
4118 {
4119 if (range == V4L2_QUANTIZATION_FULL_RANGE && matrix == V4L2_YCBCR_ENC_601 && transfer == 0)
4120 {
4121 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
4122 * function most likely is JPEG */
4123 colorspace = V4L2_COLORSPACE_JPEG;
4124 transfer = V4L2_XFER_FUNC_SRGB;
4125 }
4126 else
4127 {
4128 /* SD streams likely use SMPTE170M and HD streams REC709 */
4129 if (width <= 720 && height <= 576)
4130 colorspace = V4L2_COLORSPACE_SMPTE170M;
4131 else
4132 colorspace = V4L2_COLORSPACE_REC709;
4133 }
4134 }
4135 else if (GST_VIDEO_INFO_IS_RGB(&info))
4136 {
4137 colorspace = V4L2_COLORSPACE_SRGB;
4138 transfer = V4L2_XFER_FUNC_NONE;
4139 }
4140 }
4141
4142 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired format %dx%d, format "
4143 "%" GST_FOURCC_FORMAT " stride: %d",
4144 width, height,
4145 GST_FOURCC_ARGS(pixelformat), GST_VIDEO_INFO_PLANE_STRIDE(&info, 0));
4146
4147 memset(&format, 0x00, sizeof(struct v4l2_format));
4148 format.type = v4l2object->type;
4149
4150 if (is_mplane)
4151 {
4152 format.type = v4l2object->type;
4153 format.fmt.pix_mp.pixelformat = pixelformat;
4154 format.fmt.pix_mp.width = width;
4155 format.fmt.pix_mp.height = height;
4156 format.fmt.pix_mp.field = field;
4157 format.fmt.pix_mp.num_planes = n_v4l_planes;
4158
4159 /* try to ask our prefered stride but it's not a failure if not
4160 * accepted */
4161 for (i = 0; i < n_v4l_planes; i++)
4162 {
4163 gint stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, i);
4164
4165 if (GST_VIDEO_FORMAT_INFO_IS_TILED(info.finfo))
4166 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(info.finfo);
4167
4168 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
4169 }
4170
4171 if (GST_VIDEO_INFO_FORMAT(&info) == GST_VIDEO_FORMAT_ENCODED)
4172 {
4173 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
4174 format.fmt.pix_mp.plane_fmt[0].sizeimage = 1;
4175 else
4176 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
4177 }
4178 }
4179 else
4180 {
4181 gint stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, 0);
4182
4183 format.type = v4l2object->type;
4184
4185 format.fmt.pix.width = width;
4186 format.fmt.pix.height = height;
4187 format.fmt.pix.pixelformat = pixelformat;
4188 format.fmt.pix.field = field;
4189
4190 if (GST_VIDEO_FORMAT_INFO_IS_TILED(info.finfo))
4191 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(info.finfo);
4192
4193 /* try to ask our prefered stride */
4194 format.fmt.pix.bytesperline = stride;
4195
4196 if (GST_VIDEO_INFO_FORMAT(&info) == GST_VIDEO_FORMAT_ENCODED)
4197 {
4198 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
4199 format.fmt.pix_mp.plane_fmt[0].sizeimage = 1;
4200 else
4201 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
4202 }
4203 }
4204
4205 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired format is %dx%d, format "
4206 "%" GST_FOURCC_FORMAT ", nb planes %d",
4207 format.fmt.pix.width,
4208 format.fmt.pix_mp.height,
4209 GST_FOURCC_ARGS(format.fmt.pix.pixelformat),
4210 is_mplane ? format.fmt.pix_mp.num_planes : 1);
4211
4212#ifndef GST_DISABLE_GST_DEBUG
4213 if (is_mplane)
4214 {
4215 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
4216 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d",
4217 format.fmt.pix_mp.plane_fmt[i].bytesperline);
4218 }
4219 else
4220 {
4221 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d",
4222 format.fmt.pix.bytesperline);
4223 }
4224#endif
4225
4226 if (is_mplane)
4227 {
4228 format.fmt.pix_mp.colorspace = colorspace;
4229 format.fmt.pix_mp.quantization = range;
4230 format.fmt.pix_mp.ycbcr_enc = matrix;
4231 format.fmt.pix_mp.xfer_func = transfer;
4232 }
4233 else
4234 {
4235 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
4236 format.fmt.pix.colorspace = colorspace;
4237 format.fmt.pix.quantization = range;
4238 format.fmt.pix.ycbcr_enc = matrix;
4239 format.fmt.pix.xfer_func = transfer;
4240 }
4241
4242 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
4243 colorspace, range, matrix, transfer);
4244
4245 if (try_only)
4246 {
4247 if (v4l2object->ioctl(fd, VIDIOC_TRY_FMT, &format) < 0)
4248 goto try_fmt_failed;
4249 }
4250 else
4251 {
4252 if (v4l2object->ioctl(fd, VIDIOC_S_FMT, &format) < 0)
4253 goto set_fmt_failed;
4254 }
4255
4256 if (is_mplane)
4257 {
4258 colorspace = format.fmt.pix_mp.colorspace;
4259 range = format.fmt.pix_mp.quantization;
4260 matrix = format.fmt.pix_mp.ycbcr_enc;
4261 transfer = format.fmt.pix_mp.xfer_func;
4262 }
4263 else
4264 {
4265 colorspace = format.fmt.pix.colorspace;
4266 range = format.fmt.pix.quantization;
4267 matrix = format.fmt.pix.ycbcr_enc;
4268 transfer = format.fmt.pix.xfer_func;
4269 }
4270
4271 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got format of %dx%d, format "
4272 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d",
4273 format.fmt.pix.width, format.fmt.pix_mp.height,
4274 GST_FOURCC_ARGS(format.fmt.pix.pixelformat),
4275 is_mplane ? format.fmt.pix_mp.num_planes : 1,
4276 colorspace, range, matrix, transfer);
4277
4278#ifndef GST_DISABLE_GST_DEBUG
4279 if (is_mplane)
4280 {
4281 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
4282 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d, sizeimage %d",
4283 format.fmt.pix_mp.plane_fmt[i].bytesperline,
4284 format.fmt.pix_mp.plane_fmt[i].sizeimage);
4285 }
4286 else
4287 {
4288 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d, sizeimage %d",
4289 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
4290 }
4291#endif
4292
4293 if (format.fmt.pix.pixelformat != pixelformat)
4294 goto invalid_pixelformat;
4295
4296 /* Only negotiate size with raw data.
4297 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
4298 * in ASF mode for example, there is also not reason for a driver to
4299 * change the size. */
4300 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED)
4301 {
4302 /* We can crop larger images */
4303 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
4304 goto invalid_dimensions;
4305
4306 /* Note, this will be adjusted if upstream has non-centered cropping. */
4307 align.padding_top = 0;
4308 align.padding_bottom = format.fmt.pix.height - height;
4309 align.padding_left = 0;
4310 align.padding_right = format.fmt.pix.width - width;
4311 }
4312
4313 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
4314 goto invalid_planes;
4315
4316 /* used to check colorimetry and interlace mode fields presence */
4317 s = gst_caps_get_structure(caps, 0);
4318
4319 if (!gst_aml_v4l2_object_get_interlace_mode(format.fmt.pix.field,
4320 &info.interlace_mode))
4321 goto invalid_field;
4322 if (gst_structure_has_field(s, "interlace-mode"))
4323 {
4324 if (format.fmt.pix.field != field)
4325 goto invalid_field;
4326 }
4327
4328 if (gst_aml_v4l2_object_get_colorspace(&format, &info.colorimetry))
4329 {
4330 if (gst_structure_has_field(s, "colorimetry"))
4331 {
xuesong.jiange1a19662022-06-21 20:30:22 +08004332 if (!gst_aml_v4l2_video_colorimetry_matches(&info.colorimetry, gst_structure_get_string(s, "colorimetry")))
4333 {
4334 // goto invalid_colorimetry;
4335 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004336 }
4337 }
4338 else
4339 {
4340 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
4341 * the TRY_FMT */
4342 disable_colorimetry = TRUE;
4343 if (gst_structure_has_field(s, "colorimetry"))
4344 gst_structure_remove_field(s, "colorimetry");
4345 }
4346
4347 /* In case we have skipped the try_fmt probes, we'll need to set the
4348 * colorimetry and interlace-mode back into the caps. */
4349 if (v4l2object->skip_try_fmt_probes)
4350 {
4351 if (!disable_colorimetry && !gst_structure_has_field(s, "colorimetry"))
4352 {
4353 gchar *str = gst_video_colorimetry_to_string(&info.colorimetry);
4354 gst_structure_set(s, "colorimetry", G_TYPE_STRING, str, NULL);
4355 g_free(str);
4356 }
4357
4358 if (!gst_structure_has_field(s, "interlace-mode"))
4359 gst_structure_set(s, "interlace-mode", G_TYPE_STRING,
4360 gst_video_interlace_mode_to_string(info.interlace_mode), NULL);
4361 }
4362
4363 if (try_only) /* good enough for trying only */
4364 return TRUE;
4365
4366 if (GST_VIDEO_INFO_HAS_ALPHA(&info))
4367 {
4368 struct v4l2_control ctl = {
4369 0,
4370 };
4371 ctl.id = V4L2_CID_ALPHA_COMPONENT;
4372 ctl.value = 0xff;
4373
4374 if (v4l2object->ioctl(fd, VIDIOC_S_CTRL, &ctl) < 0)
4375 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4376 "Failed to set alpha component value");
4377 }
4378
4379 /* Is there a reason we require the caller to always specify a framerate? */
4380 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
4381 fps_d);
4382
4383 if (v4l2object->ioctl(fd, VIDIOC_G_PARM, &streamparm) < 0)
4384 goto get_parm_failed;
4385
4386 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
4387 {
4388 GST_VIDEO_INFO_FPS_N(&info) =
4389 streamparm.parm.capture.timeperframe.denominator;
4390 GST_VIDEO_INFO_FPS_D(&info) =
4391 streamparm.parm.capture.timeperframe.numerator;
4392
4393 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got capture framerate: %u/%u",
4394 streamparm.parm.capture.timeperframe.denominator,
4395 streamparm.parm.capture.timeperframe.numerator);
4396
4397 /* We used to skip frame rate setup if the camera was already setup
4398 * with the requested frame rate. This breaks some cameras though,
4399 * causing them to not output data (several models of Thinkpad cameras
4400 * have this problem at least).
4401 * So, don't skip. */
4402 GST_LOG_OBJECT(v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
4403 fps_n, fps_d);
4404 /* We want to change the frame rate, so check whether we can. Some cheap USB
4405 * cameras don't have the capability */
4406 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0)
4407 {
4408 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
4409 "Not setting capture framerate (not supported)");
4410 goto done;
4411 }
4412
4413 /* Note: V4L2 wants the frame interval, we have the frame rate */
4414 streamparm.parm.capture.timeperframe.numerator = fps_d;
4415 streamparm.parm.capture.timeperframe.denominator = fps_n;
4416
zengliang.lic9f869d2023-02-15 08:32:32 +00004417 /* Amlogic sets parameters to the decoder and only supports delivery of private structures */
4418 //some cheap USB cam's won't accept any change */
4419 //if (v4l2object->ioctl(fd, VIDIOC_S_PARM, &streamparm) < 0)
4420 //goto set_parm_failed;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004421
4422 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
4423 streamparm.parm.capture.timeperframe.denominator > 0)
4424 {
4425 /* get new values */
4426 fps_d = streamparm.parm.capture.timeperframe.numerator;
4427 fps_n = streamparm.parm.capture.timeperframe.denominator;
4428
4429 GST_INFO_OBJECT(v4l2object->dbg_obj, "Set capture framerate to %u/%u",
4430 fps_n, fps_d);
4431 }
4432 else
4433 {
4434 /* fix v4l2 capture driver to provide framerate values */
4435 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4436 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
4437 }
4438
4439 GST_VIDEO_INFO_FPS_N(&info) = fps_n;
4440 GST_VIDEO_INFO_FPS_D(&info) = fps_d;
4441 }
4442 else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
4443 {
4444 GST_VIDEO_INFO_FPS_N(&info) =
4445 streamparm.parm.output.timeperframe.denominator;
4446 GST_VIDEO_INFO_FPS_D(&info) =
4447 streamparm.parm.output.timeperframe.numerator;
4448
4449 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got output framerate: %u/%u",
4450 streamparm.parm.output.timeperframe.denominator,
4451 streamparm.parm.output.timeperframe.numerator);
4452
4453 GST_LOG_OBJECT(v4l2object->dbg_obj, "Setting output framerate to %u/%u",
4454 fps_n, fps_d);
4455 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0)
4456 {
4457 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
4458 "Not setting output framerate (not supported)");
4459 goto done;
4460 }
4461
4462 /* Note: V4L2 wants the frame interval, we have the frame rate */
4463 streamparm.parm.output.timeperframe.numerator = fps_d;
4464 streamparm.parm.output.timeperframe.denominator = fps_n;
4465
zengliang.lic9f869d2023-02-15 08:32:32 +00004466 /*Amlogic sets parameters to the decoder and only supports delivery of private structures*/
4467 //if (v4l2object->ioctl(fd, VIDIOC_S_PARM, &streamparm) < 0)
4468 //goto set_parm_failed;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004469
4470 if (streamparm.parm.output.timeperframe.numerator > 0 &&
4471 streamparm.parm.output.timeperframe.denominator > 0)
4472 {
4473 /* get new values */
4474 fps_d = streamparm.parm.output.timeperframe.numerator;
4475 fps_n = streamparm.parm.output.timeperframe.denominator;
4476
4477 GST_INFO_OBJECT(v4l2object->dbg_obj, "Set output framerate to %u/%u",
4478 fps_n, fps_d);
4479 }
4480 else
4481 {
4482 /* fix v4l2 output driver to provide framerate values */
4483 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4484 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
4485 }
4486
4487 GST_VIDEO_INFO_FPS_N(&info) = fps_n;
4488 GST_VIDEO_INFO_FPS_D(&info) = fps_d;
4489 }
4490
4491done:
4492 /* add boolean return, so we can fail on drivers bugs */
4493 gst_aml_v4l2_object_save_format(v4l2object, fmtdesc, &format, &info, &align);
4494
4495 /* now configure the pool */
4496 if (!gst_aml_v4l2_object_setup_pool(v4l2object, caps))
4497 goto pool_failed;
4498
4499 return TRUE;
4500
4501 /* ERRORS */
4502invalid_caps:
4503{
4504 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
4505 caps);
4506
4507 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4508 (_("Invalid caps")), ("Can't parse caps %" GST_PTR_FORMAT, caps));
4509 return FALSE;
4510}
4511try_fmt_failed:
4512{
4513 if (errno == EINVAL)
4514 {
4515 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4516 (_("Device '%s' has no supported format"), v4l2object->videodev),
4517 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4518 GST_FOURCC_ARGS(pixelformat), width, height,
4519 g_strerror(errno)));
4520 }
4521 else
4522 {
4523 GST_AML_V4L2_ERROR(error, RESOURCE, FAILED,
4524 (_("Device '%s' failed during initialization"),
4525 v4l2object->videodev),
4526 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4527 GST_FOURCC_ARGS(pixelformat), width, height,
4528 g_strerror(errno)));
4529 }
4530 return FALSE;
4531}
4532set_fmt_failed:
4533{
4534 if (errno == EBUSY)
4535 {
4536 GST_AML_V4L2_ERROR(error, RESOURCE, BUSY,
4537 (_("Device '%s' is busy"), v4l2object->videodev),
4538 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4539 GST_FOURCC_ARGS(pixelformat), width, height,
4540 g_strerror(errno)));
4541 }
4542 else if (errno == EINVAL)
4543 {
4544 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4545 (_("Device '%s' has no supported format"), v4l2object->videodev),
4546 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4547 GST_FOURCC_ARGS(pixelformat), width, height,
4548 g_strerror(errno)));
4549 }
4550 else
4551 {
4552 GST_AML_V4L2_ERROR(error, RESOURCE, FAILED,
4553 (_("Device '%s' failed during initialization"),
4554 v4l2object->videodev),
4555 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4556 GST_FOURCC_ARGS(pixelformat), width, height,
4557 g_strerror(errno)));
4558 }
4559 return FALSE;
4560}
4561invalid_dimensions:
4562{
4563 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4564 (_("Device '%s' cannot capture at %dx%d"),
4565 v4l2object->videodev, width, height),
4566 ("Tried to capture at %dx%d, but device returned size %dx%d",
4567 width, height, format.fmt.pix.width, format.fmt.pix.height));
4568 return FALSE;
4569}
4570invalid_pixelformat:
4571{
4572 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4573 (_("Device '%s' cannot capture in the specified format"),
4574 v4l2object->videodev),
4575 ("Tried to capture in %" GST_FOURCC_FORMAT
4576 ", but device returned format"
4577 " %" GST_FOURCC_FORMAT,
4578 GST_FOURCC_ARGS(pixelformat),
4579 GST_FOURCC_ARGS(format.fmt.pix.pixelformat)));
4580 return FALSE;
4581}
4582invalid_planes:
4583{
4584 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4585 (_("Device '%s' does support non-contiguous planes"),
4586 v4l2object->videodev),
4587 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
4588 return FALSE;
4589}
4590invalid_field:
4591{
4592 enum v4l2_field wanted_field;
4593
4594 if (is_mplane)
4595 wanted_field = format.fmt.pix_mp.field;
4596 else
4597 wanted_field = format.fmt.pix.field;
4598
4599 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4600 (_("Device '%s' does not support %s interlacing"),
4601 v4l2object->videodev,
4602 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
4603 ("Device wants %s interlacing",
4604 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
4605 return FALSE;
4606}
hanghang.luo3128f102022-08-18 10:36:19 +08004607#ifdef DELETE_FOR_LGE
xuesong.jiangae1548e2022-05-06 16:38:46 +08004608invalid_colorimetry:
4609{
4610 gchar *wanted_colorimetry;
4611
4612 wanted_colorimetry = gst_video_colorimetry_to_string(&info.colorimetry);
4613
4614 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4615 (_("Device '%s' does not support %s colorimetry"),
4616 v4l2object->videodev, gst_structure_get_string(s, "colorimetry")),
4617 ("Device wants %s colorimetry", wanted_colorimetry));
4618
4619 g_free(wanted_colorimetry);
4620 return FALSE;
4621}
hanghang.luo3128f102022-08-18 10:36:19 +08004622#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08004623get_parm_failed:
4624{
4625 /* it's possible that this call is not supported */
4626 if (errno != EINVAL && errno != ENOTTY)
4627 {
4628 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4629 (_("Could not get parameters on device '%s'"),
4630 v4l2object->videodev),
4631 GST_ERROR_SYSTEM);
4632 }
4633 goto done;
4634}
4635set_parm_failed:
4636{
4637 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4638 (_("Video device did not accept new frame rate setting.")),
4639 GST_ERROR_SYSTEM);
4640 goto done;
4641}
4642pool_failed:
4643{
4644 /* setup_pool already send the error */
4645 return FALSE;
4646}
4647}
4648
4649gboolean
4650gst_aml_v4l2_object_set_format(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4651 GstAmlV4l2Error *error)
4652{
4653 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
4654 caps);
4655 return gst_aml_v4l2_object_set_format_full(v4l2object, caps, FALSE, error);
4656}
4657
4658gboolean
4659gst_aml_v4l2_object_try_format(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4660 GstAmlV4l2Error *error)
4661{
4662 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
4663 caps);
4664 return gst_aml_v4l2_object_set_format_full(v4l2object, caps, TRUE, error);
4665}
4666
4667GstFlowReturn
4668gst_aml_v4l2_object_poll(GstAmlV4l2Object *v4l2object)
4669{
4670 gint ret;
4671
4672 if (!v4l2object->can_poll_device)
4673 goto done;
4674
4675 GST_LOG_OBJECT(v4l2object, "polling device");
4676
4677again:
4678 ret = gst_poll_wait(v4l2object->poll, GST_CLOCK_TIME_NONE);
4679 if (G_UNLIKELY(ret < 0))
4680 {
4681 switch (errno)
4682 {
4683 case EBUSY:
4684 goto stopped;
4685 case EAGAIN:
4686 case EINTR:
4687 goto again;
4688 case ENXIO:
4689 GST_WARNING_OBJECT(v4l2object,
4690 "v4l2 device doesn't support polling. Disabling"
4691 " using libv4l2 in this case may cause deadlocks");
4692 v4l2object->can_poll_device = FALSE;
4693 goto done;
4694 default:
4695 goto select_error;
4696 }
4697 }
4698
4699done:
4700 return GST_FLOW_OK;
4701
4702 /* ERRORS */
4703stopped:
4704{
4705 GST_DEBUG_OBJECT(v4l2object, "stop called");
4706 return GST_FLOW_FLUSHING;
4707}
4708select_error:
4709{
4710 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, READ, (NULL),
4711 ("poll error %d: %s (%d)", ret, g_strerror(errno), errno));
4712 return GST_FLOW_ERROR;
4713}
4714}
4715
4716GstFlowReturn
4717gst_aml_v4l2_object_dqevent(GstAmlV4l2Object *v4l2object)
4718{
4719 GstFlowReturn res;
4720 struct v4l2_event evt;
4721
4722 if ((res = gst_aml_v4l2_object_poll(v4l2object)) != GST_FLOW_OK)
4723 goto poll_failed;
4724
4725 memset(&evt, 0x00, sizeof(struct v4l2_event));
4726 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DQEVENT, &evt) < 0)
4727 goto dqevent_failed;
4728
4729 switch (evt.type)
4730 {
4731 case V4L2_EVENT_SOURCE_CHANGE:
4732 return GST_AML_V4L2_FLOW_SOURCE_CHANGE;
4733 break;
4734 case V4L2_EVENT_EOS:
4735 return GST_AML_V4L2_FLOW_LAST_BUFFER;
4736 break;
4737 default:
4738 break;
4739 }
4740
4741 return GST_FLOW_OK;
4742
4743 /* ERRORS */
4744poll_failed:
4745{
4746 GST_DEBUG_OBJECT(v4l2object, "poll error %s", gst_flow_get_name(res));
4747 return res;
4748}
4749dqevent_failed:
4750{
4751 return GST_FLOW_ERROR;
4752}
4753}
4754
4755/**
4756 * gst_aml_v4l2_object_acquire_format:
4757 * @v4l2object the object
4758 * @info a GstVideoInfo to be filled
4759 *
4760 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
4761 * the output format is choosen by the HW.
4762 *
4763 * Returns: %TRUE on success, %FALSE on failure.
4764 */
4765gboolean
4766gst_aml_v4l2_object_acquire_format(GstAmlV4l2Object *v4l2object, GstVideoInfo *info)
4767{
4768 struct v4l2_fmtdesc *fmtdesc;
4769 struct v4l2_format fmt;
4770 struct v4l2_crop crop;
4771 struct v4l2_selection sel;
sheng.liu641aa422023-12-26 07:05:59 +00004772 struct v4l2_cropcap cropcap;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004773 struct v4l2_rect *r = NULL;
4774 GstVideoFormat format;
4775 guint width, height;
4776 GstVideoAlignment align;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004777 gint dw_mode;
sheng.liu641aa422023-12-26 07:05:59 +00004778 gdouble pixelAspectRatio = 0.0;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004779
4780 gst_video_info_init(info);
4781 gst_video_alignment_reset(&align);
4782
4783 memset(&fmt, 0x00, sizeof(struct v4l2_format));
4784 fmt.type = v4l2object->type;
fei.denge9458472023-04-18 02:05:48 +00004785 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "fmt.type:%d", fmt.type);
xuesong.jiangae1548e2022-05-06 16:38:46 +08004786 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
4787 goto get_fmt_failed;
4788
4789 fmtdesc = gst_aml_v4l2_object_get_format_from_fourcc(v4l2object,
4790 fmt.fmt.pix.pixelformat);
4791 if (fmtdesc == NULL)
4792 goto unsupported_format;
4793
4794 /* No need to care about mplane, the four first params are the same */
4795 format = gst_aml_v4l2_object_v4l2fourcc_to_video_format(fmt.fmt.pix.pixelformat);
4796
4797 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
4798 if (format == GST_VIDEO_FORMAT_UNKNOWN)
4799 goto unsupported_format;
4800
4801 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
4802 goto invalid_dimensions;
4803
4804 width = fmt.fmt.pix.width;
4805 height = fmt.fmt.pix.height;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004806 /* Use the default compose rectangle */
4807 memset(&sel, 0, sizeof(struct v4l2_selection));
4808 sel.type = v4l2object->type;
4809 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
4810 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
4811 {
4812 r = &sel.r;
4813 }
4814 else
4815 {
4816 /* For ancient kernels, fall back to G_CROP */
4817 memset(&crop, 0, sizeof(struct v4l2_crop));
4818 crop.type = v4l2object->type;
4819 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
4820 r = &crop.c;
4821 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004822
4823 dw_mode = gst_aml_v4l2_object_get_dw_mode(v4l2object);
sheng.liu07337c82023-12-01 03:21:57 +00004824 if (r)
xuesong.jiangae1548e2022-05-06 16:38:46 +08004825 {
4826 align.padding_left = r->left;
4827 align.padding_top = r->top;
4828 align.padding_right = width - r->width - r->left;
4829 align.padding_bottom = height - r->height - r->top;
4830 width = r->width;
4831 height = r->height;
sheng.liu07337c82023-12-01 03:21:57 +00004832 if (dw_mode >= 0 && dw_mode != 16)
4833 {
4834 width = (width/2) *2; /* align for dw*/
4835 height = (height/2) *2; /* align for dw*/
4836 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004837 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004838 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "final w:%d, h:%d", width, height);
xuesong.jiangae1548e2022-05-06 16:38:46 +08004839
4840 gst_video_info_set_format(info, format, width, height);
4841
4842 switch (fmt.fmt.pix.field)
4843 {
4844 case V4L2_FIELD_ANY:
4845 case V4L2_FIELD_NONE:
4846 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
4847 break;
4848 case V4L2_FIELD_INTERLACED:
4849 case V4L2_FIELD_INTERLACED_TB:
4850 case V4L2_FIELD_INTERLACED_BT:
4851 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
4852 break;
4853 default:
4854 goto unsupported_field;
4855 }
4856
4857 gst_aml_v4l2_object_get_colorspace(&fmt, &info->colorimetry);
4858
4859 gst_aml_v4l2_object_save_format(v4l2object, fmtdesc, &fmt, info, &align);
4860
sheng.liu641aa422023-12-26 07:05:59 +00004861 if (v4l2object->par)
4862 {
4863 width = gst_value_get_fraction_numerator(v4l2object->par);
4864 height = gst_value_get_fraction_denominator(v4l2object->par);
4865 pixelAspectRatio = (gdouble)width/(gdouble)height;
4866 }
4867
4868 if (!v4l2object->par || pixelAspectRatio == 1.0)
4869 {
4870 memset(&cropcap, 0, sizeof(cropcap));
4871 width= height= 1;
4872 cropcap.type = v4l2object->type;
4873 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) >= 0)
4874 {
4875 width= cropcap.pixelaspect.denominator;
4876 height= cropcap.pixelaspect.numerator;
4877 GST_DEBUG("cropcap: pixel aspect ratio %d:%d", width, height);
4878 if ( !width || !height )
4879 {
4880 GST_DEBUG("force pixel aspect of 1:1");
4881 width= height= 1;
4882 }
4883 }
sheng.liu641aa422023-12-26 07:05:59 +00004884 }
sheng.liudb26f7d2024-01-22 11:24:23 +00004885
sheng.liud9027ca2024-01-24 09:21:49 +00004886 GST_VIDEO_INFO_PAR_N(info) = width;
4887 GST_VIDEO_INFO_PAR_D(info) = height;
4888
sheng.liudb26f7d2024-01-22 11:24:23 +00004889 if (v4l2object->fps)
4890 {
4891 GST_VIDEO_INFO_FPS_N(info) = gst_value_get_fraction_numerator(v4l2object->fps);
4892 GST_VIDEO_INFO_FPS_D(info) = gst_value_get_fraction_denominator(v4l2object->fps);
4893 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004894 /* Shall we setup the pool ? */
4895
4896 return TRUE;
4897
4898get_fmt_failed:
4899{
4900 GST_ELEMENT_WARNING(v4l2object->element, RESOURCE, SETTINGS,
4901 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
4902 return FALSE;
4903}
4904invalid_dimensions:
4905{
4906 GST_ELEMENT_WARNING(v4l2object->element, RESOURCE, SETTINGS,
4907 (_("Video device returned invalid dimensions.")),
4908 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
4909 fmt.fmt.pix.height));
4910 return FALSE;
4911}
4912unsupported_field:
4913{
4914 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
4915 (_("Video device uses an unsupported interlacing method.")),
4916 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4917 return FALSE;
4918}
4919unsupported_format:
4920{
4921 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
4922 (_("Video device uses an unsupported pixel format.")),
4923 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4924 GST_FOURCC_ARGS(fmt.fmt.pix.pixelformat)));
4925 return FALSE;
4926}
4927}
4928
4929gboolean
4930gst_aml_v4l2_object_set_crop(GstAmlV4l2Object *obj)
4931{
4932 struct v4l2_selection sel = {0};
4933 struct v4l2_crop crop = {0};
4934
4935 sel.type = obj->type;
4936 sel.target = V4L2_SEL_TGT_CROP;
4937 sel.flags = 0;
4938 sel.r.left = obj->align.padding_left;
4939 sel.r.top = obj->align.padding_top;
4940 sel.r.width = obj->info.width;
4941 sel.r.height = obj->info.height;
4942
4943 crop.type = obj->type;
4944 crop.c = sel.r;
4945
4946 if (obj->align.padding_left + obj->align.padding_top +
4947 obj->align.padding_right + obj->align.padding_bottom ==
4948 0)
4949 {
4950 GST_DEBUG_OBJECT(obj->dbg_obj, "no cropping needed");
4951 return TRUE;
4952 }
4953
4954 GST_DEBUG_OBJECT(obj->dbg_obj,
4955 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4956 crop.c.width, crop.c.height);
4957
4958 if (obj->ioctl(obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0)
4959 {
4960 if (errno != ENOTTY)
4961 {
4962 GST_WARNING_OBJECT(obj->dbg_obj,
4963 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4964 g_strerror(errno));
4965 return FALSE;
4966 }
4967 else
4968 {
4969 if (obj->ioctl(obj->video_fd, VIDIOC_S_CROP, &crop) < 0)
4970 {
4971 GST_WARNING_OBJECT(obj->dbg_obj, "VIDIOC_S_CROP failed");
4972 return FALSE;
4973 }
4974
4975 if (obj->ioctl(obj->video_fd, VIDIOC_G_CROP, &crop) < 0)
4976 {
4977 GST_WARNING_OBJECT(obj->dbg_obj, "VIDIOC_G_CROP failed");
4978 return FALSE;
4979 }
4980
4981 sel.r = crop.c;
4982 }
4983 }
4984
4985 GST_DEBUG_OBJECT(obj->dbg_obj,
4986 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4987 crop.c.width, crop.c.height);
4988
4989 return TRUE;
4990}
4991
4992gboolean
4993gst_aml_v4l2_object_caps_equal(GstAmlV4l2Object *v4l2object, GstCaps *caps)
4994{
4995 GstStructure *config;
4996 GstCaps *oldcaps;
4997 gboolean ret;
4998
4999 if (!v4l2object->pool)
5000 return FALSE;
5001
5002 config = gst_buffer_pool_get_config(v4l2object->pool);
5003 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5004
5005 ret = oldcaps && gst_caps_is_equal(caps, oldcaps);
5006
5007 gst_structure_free(config);
5008
5009 return ret;
5010}
5011
5012gboolean
5013gst_aml_v4l2_object_caps_is_subset(GstAmlV4l2Object *v4l2object, GstCaps *caps)
5014{
5015 GstStructure *config;
5016 GstCaps *oldcaps;
5017 gboolean ret;
5018
5019 if (!v4l2object->pool)
5020 return FALSE;
5021
5022 config = gst_buffer_pool_get_config(v4l2object->pool);
5023 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5024
5025 ret = oldcaps && gst_caps_is_subset(oldcaps, caps);
5026
5027 gst_structure_free(config);
5028
5029 return ret;
5030}
5031
5032GstCaps *
5033gst_aml_v4l2_object_get_current_caps(GstAmlV4l2Object *v4l2object)
5034{
5035 GstStructure *config;
5036 GstCaps *oldcaps;
5037
5038 if (!v4l2object->pool)
5039 return NULL;
5040
5041 config = gst_buffer_pool_get_config(v4l2object->pool);
5042 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5043
5044 if (oldcaps)
5045 gst_caps_ref(oldcaps);
5046
5047 gst_structure_free(config);
5048
5049 return oldcaps;
5050}
5051
5052gboolean
5053gst_aml_v4l2_object_unlock(GstAmlV4l2Object *v4l2object)
5054{
5055 gboolean ret = TRUE;
5056
5057 GST_LOG_OBJECT(v4l2object->dbg_obj, "start flushing");
5058
5059 gst_poll_set_flushing(v4l2object->poll, TRUE);
5060
5061 if (v4l2object->pool && gst_buffer_pool_is_active(v4l2object->pool))
5062 gst_buffer_pool_set_flushing(v4l2object->pool, TRUE);
5063
5064 return ret;
5065}
5066
5067gboolean
5068gst_aml_v4l2_object_unlock_stop(GstAmlV4l2Object *v4l2object)
5069{
5070 gboolean ret = TRUE;
5071
5072 GST_LOG_OBJECT(v4l2object->dbg_obj, "stop flushing");
5073
5074 if (v4l2object->pool && gst_buffer_pool_is_active(v4l2object->pool))
5075 gst_buffer_pool_set_flushing(v4l2object->pool, FALSE);
5076
5077 gst_poll_set_flushing(v4l2object->poll, FALSE);
5078
5079 return ret;
5080}
5081
5082gboolean
5083gst_aml_v4l2_object_stop(GstAmlV4l2Object *v4l2object)
5084{
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005085 GstAmlV4l2BufferPool *bpool = GST_AML_V4L2_BUFFER_POOL(v4l2object->pool);
5086
xuesong.jiangae1548e2022-05-06 16:38:46 +08005087 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "stopping");
5088
5089 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
5090 goto done;
5091 if (!GST_AML_V4L2_IS_ACTIVE(v4l2object))
5092 goto done;
5093
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005094 if (bpool && bpool->other_pool) /* jxsdbg for resolution switch */
5095 {
5096 if (v4l2object->old_other_pool)
5097 {
5098 /* this case indicate 1st switch did not wait all old pool buf recycle and 2nd switch is coming.
5099 so save 1st old pool */
5100 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "switching occurs during last switching buf recycle flow");
5101 v4l2object->old_old_other_pool = v4l2object->old_other_pool;
5102 }
5103
5104 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "switching flow, ref old drmbufferpool");
5105 v4l2object->old_other_pool = bpool->other_pool;
5106 gst_object_ref(v4l2object->old_other_pool);
5107 }
5108
xuesong.jiangae1548e2022-05-06 16:38:46 +08005109 if (v4l2object->pool)
5110 {
5111 if (!gst_aml_v4l2_buffer_pool_orphan(&v4l2object->pool))
5112 {
5113 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "deactivating pool");
5114 gst_buffer_pool_set_active(v4l2object->pool, FALSE);
5115 gst_object_unref(v4l2object->pool);
5116 }
5117 v4l2object->pool = NULL;
5118 }
5119
5120 GST_AML_V4L2_SET_INACTIVE(v4l2object);
5121
5122done:
5123 return TRUE;
5124}
5125
5126GstCaps *
5127gst_aml_v4l2_object_probe_caps(GstAmlV4l2Object *v4l2object, GstCaps *filter)
5128{
5129 GstCaps *ret;
5130 GSList *walk;
5131 GSList *formats;
5132
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005133 GST_INFO_OBJECT(v4l2object->dbg_obj, "filter caps: %" GST_PTR_FORMAT, filter);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005134 formats = gst_aml_v4l2_object_get_format_list(v4l2object);
5135
5136 ret = gst_caps_new_empty();
5137
sheng.liu641aa422023-12-26 07:05:59 +00005138// At this time, decoder will return defult aspect, and it is not usful.
5139// so, do not probe cropcap at this time and do this action after decoding.
5140#if 0
xuesong.jiangae1548e2022-05-06 16:38:46 +08005141 if (v4l2object->keep_aspect && !v4l2object->par)
5142 {
5143 struct v4l2_cropcap cropcap;
5144
5145 memset(&cropcap, 0, sizeof(cropcap));
5146
5147 cropcap.type = v4l2object->type;
5148 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
5149 {
5150 if (errno != ENOTTY)
5151 GST_WARNING_OBJECT(v4l2object->dbg_obj,
5152 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
5153 g_strerror(errno));
5154 }
5155 else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator)
5156 {
5157 v4l2object->par = g_new0(GValue, 1);
5158 g_value_init(v4l2object->par, GST_TYPE_FRACTION);
5159 gst_value_set_fraction(v4l2object->par, cropcap.pixelaspect.numerator,
5160 cropcap.pixelaspect.denominator);
5161 }
5162 }
sheng.liu641aa422023-12-26 07:05:59 +00005163#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08005164
5165 for (walk = formats; walk; walk = walk->next)
5166 {
5167 struct v4l2_fmtdesc *format;
5168 GstStructure *template;
5169 GstCaps *tmp, *tmp2;
5170
5171 format = (struct v4l2_fmtdesc *)walk->data;
5172
5173 template = gst_aml_v4l2_object_v4l2fourcc_to_bare_struct(format->pixelformat);
5174
5175 if (!template)
5176 {
5177 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
5178 "unknown format %" GST_FOURCC_FORMAT,
5179 GST_FOURCC_ARGS(format->pixelformat));
5180 continue;
5181 }
5182
5183 /* If we have a filter, check if we need to probe this format or not */
5184 if (filter)
5185 {
5186 GstCaps *format_caps = gst_caps_new_empty();
5187
5188 gst_caps_append_structure(format_caps, gst_structure_copy(template));
xuesong.jiange1a19662022-06-21 20:30:22 +08005189 GST_INFO_OBJECT(v4l2object->dbg_obj, "format_caps: %" GST_PTR_FORMAT, format_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005190
5191 if (!gst_caps_can_intersect(format_caps, filter))
5192 {
5193 gst_caps_unref(format_caps);
5194 gst_structure_free(template);
5195 continue;
5196 }
5197
5198 gst_caps_unref(format_caps);
5199 }
5200
5201 tmp = gst_aml_v4l2_object_probe_caps_for_format(v4l2object,
5202 format->pixelformat, template);
xuesong.jiange1a19662022-06-21 20:30:22 +08005203 GST_INFO_OBJECT(v4l2object->dbg_obj, "tmp caps: %" GST_PTR_FORMAT, tmp);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005204
5205 if (tmp)
5206 {
5207 tmp2 = gst_caps_copy(tmp);
5208 gst_caps_set_features_simple(tmp2, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
5209 gst_caps_append(ret, tmp);
5210 gst_caps_append(ret, tmp2);
5211 }
5212
5213 gst_structure_free(template);
5214 }
5215
5216 if (filter)
5217 {
5218 GstCaps *tmp;
5219
5220 tmp = ret;
5221 ret = gst_caps_intersect_full(filter, ret, GST_CAPS_INTERSECT_FIRST);
5222 gst_caps_unref(tmp);
5223 }
5224
xuesong.jiang22a9b112023-05-24 09:01:59 +00005225 if (v4l2object->stream_mode)
5226 {
5227 GST_INFO_OBJECT(v4l2object->dbg_obj, "ret caps: %" GST_PTR_FORMAT, ret);
5228 for (guint i = 0; i < gst_caps_get_size(ret); i++)
5229 {
5230 GstStructure *s = gst_caps_get_structure(ret, i);
5231 if (s)
5232 gst_structure_remove_field(s, "alignment");
5233
5234 GST_DEBUG("i:%d, s:%p", i, s);
5235 }
5236 GST_INFO_OBJECT(v4l2object->dbg_obj, "new ret caps: %" GST_PTR_FORMAT, ret);
5237 }
5238
xuesong.jiangae1548e2022-05-06 16:38:46 +08005239 GST_INFO_OBJECT(v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
5240
5241 return ret;
5242}
5243
5244GstCaps *
5245gst_aml_v4l2_object_get_caps(GstAmlV4l2Object *v4l2object, GstCaps *filter)
5246{
5247 GstCaps *ret;
5248
5249 if (v4l2object->probed_caps == NULL)
5250 v4l2object->probed_caps = gst_aml_v4l2_object_probe_caps(v4l2object, NULL);
5251
5252 if (filter)
5253 {
5254 ret = gst_caps_intersect_full(filter, v4l2object->probed_caps,
5255 GST_CAPS_INTERSECT_FIRST);
5256 }
5257 else
5258 {
5259 ret = gst_caps_ref(v4l2object->probed_caps);
5260 }
5261
5262 return ret;
5263}
5264
5265gboolean
5266gst_aml_v4l2_object_decide_allocation(GstAmlV4l2Object *obj, GstQuery *query)
5267{
5268 GstCaps *caps;
5269 GstBufferPool *pool = NULL, *other_pool = NULL;
5270 GstStructure *config;
5271 guint size, min, max, own_min = 0;
5272 gboolean update;
5273 gboolean has_video_meta;
5274 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
5275 GstAllocator *allocator = NULL;
5276 GstAllocationParams params = {0};
5277
5278 GST_DEBUG_OBJECT(obj->dbg_obj, "decide allocation");
5279
5280 g_return_val_if_fail(obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
5281 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
5282 FALSE);
5283
5284 gst_query_parse_allocation(query, &caps, NULL);
5285
5286 if (obj->pool == NULL)
5287 {
5288 if (!gst_aml_v4l2_object_setup_pool(obj, caps))
5289 goto pool_failed;
5290 }
5291
5292 if (gst_query_get_n_allocation_params(query) > 0)
5293 gst_query_parse_nth_allocation_param(query, 0, &allocator, &params);
5294
5295 if (gst_query_get_n_allocation_pools(query) > 0)
5296 {
5297 gst_query_parse_nth_allocation_pool(query, 0, &pool, &size, &min, &max);
5298 update = TRUE;
5299 }
5300 else
5301 {
5302 pool = NULL;
5303 min = max = 0;
5304 size = 0;
5305 update = FALSE;
5306 }
5307
5308 GST_DEBUG_OBJECT(obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%" GST_PTR_FORMAT, size, min, max, pool);
5309
5310 has_video_meta =
5311 gst_query_find_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
5312
5313 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
5314
5315 gst_aml_v4l2_get_driver_min_buffers(obj);
5316 /* We can't share our own pool, if it exceed V4L2 capacity */
5317 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
5318 can_share_own_pool = FALSE;
5319
5320 /* select a pool */
5321 switch (obj->mode)
5322 {
5323 case GST_V4L2_IO_RW:
5324 if (pool)
5325 {
5326 /* in READ/WRITE mode, prefer a downstream pool because our own pool
5327 * doesn't help much, we have to write to it as well */
5328 GST_DEBUG_OBJECT(obj->dbg_obj,
5329 "read/write mode: using downstream pool");
5330 /* use the bigest size, when we use our own pool we can't really do any
5331 * other size than what the hardware gives us but for downstream pools
5332 * we can try */
5333 size = MAX(size, obj->info.size);
5334 }
5335 else if (can_share_own_pool)
5336 {
5337 /* no downstream pool, use our own then */
5338 GST_DEBUG_OBJECT(obj->dbg_obj,
5339 "read/write mode: no downstream pool, using our own");
5340 pool = gst_object_ref(obj->pool);
5341 size = obj->info.size;
5342 pushing_from_our_pool = TRUE;
5343 }
5344 break;
5345
5346 case GST_V4L2_IO_USERPTR:
5347 case GST_V4L2_IO_DMABUF_IMPORT:
5348 /* in importing mode, prefer our own pool, and pass the other pool to
5349 * our own, so it can serve itself */
5350 if (pool == NULL)
5351 goto no_downstream_pool;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005352 gst_aml_v4l2_buffer_pool_set_other_pool(GST_AML_V4L2_BUFFER_POOL(obj->pool), pool);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005353 other_pool = pool;
5354 gst_object_unref(pool);
5355 pool = gst_object_ref(obj->pool);
5356 size = obj->info.size;
5357 break;
5358
5359 case GST_V4L2_IO_MMAP:
5360 case GST_V4L2_IO_DMABUF:
5361 /* in streaming mode, prefer our own pool */
5362 /* Check if we can use it ... */
5363 if (can_share_own_pool)
5364 {
5365 if (pool)
5366 gst_object_unref(pool);
5367 pool = gst_object_ref(obj->pool);
5368 size = obj->info.size;
5369 GST_DEBUG_OBJECT(obj->dbg_obj,
5370 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
5371 pushing_from_our_pool = TRUE;
5372 }
5373 else if (pool)
5374 {
5375 GST_DEBUG_OBJECT(obj->dbg_obj,
5376 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
5377 pool);
5378 }
5379 else
5380 {
5381 GST_DEBUG_OBJECT(obj->dbg_obj,
5382 "streaming mode: no usable pool, copying to generic pool");
5383 size = MAX(size, obj->info.size);
5384 }
5385 break;
5386 case GST_V4L2_IO_AUTO:
5387 default:
5388 GST_WARNING_OBJECT(obj->dbg_obj, "unhandled mode");
5389 break;
5390 }
5391
5392 if (size == 0)
5393 goto no_size;
5394
5395 /* If pushing from our own pool, configure it with queried minimum,
5396 * otherwise use the minimum required */
5397 if (pushing_from_our_pool)
5398 {
5399 /* When pushing from our own pool, we need what downstream one, to be able
5400 * to fill the pipeline, the minimum required to decoder according to the
5401 * driver and 2 more, so we don't endup up with everything downstream or
5402 * held by the decoder. We account 2 buffers for v4l2 so when one is being
5403 * pushed downstream the other one can already be queued for the next
5404 * frame. */
5405 own_min = min + obj->min_buffers + 2;
5406
5407 /* If no allocation parameters where provided, allow for a little more
5408 * buffers and enable copy threshold */
5409 if (!update)
5410 {
5411 own_min += 2;
5412 gst_aml_v4l2_buffer_pool_copy_at_threshold(GST_AML_V4L2_BUFFER_POOL(pool),
5413 TRUE);
5414 }
5415 else
5416 {
5417 gst_aml_v4l2_buffer_pool_copy_at_threshold(GST_AML_V4L2_BUFFER_POOL(pool),
5418 FALSE);
5419 }
5420 }
5421 else
5422 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005423 min = obj->min_buffers;
5424 max = min;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005425 }
5426
5427 /* Request a bigger max, if one was suggested but it's too small */
5428 if (max != 0)
5429 max = MAX(min, max);
5430
5431 /* First step, configure our own pool */
5432 config = gst_buffer_pool_get_config(obj->pool);
5433
5434 if (obj->need_video_meta || has_video_meta)
5435 {
5436 GST_DEBUG_OBJECT(obj->dbg_obj, "activate Video Meta");
5437 gst_buffer_pool_config_add_option(config,
5438 GST_BUFFER_POOL_OPTION_VIDEO_META);
5439 }
5440
5441 gst_buffer_pool_config_set_allocator(config, allocator, &params);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005442 gst_buffer_pool_config_set_params(config, caps, size, min, max);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005443
5444 GST_DEBUG_OBJECT(obj->dbg_obj, "setting own pool config to %" GST_PTR_FORMAT, config);
5445
5446 /* Our pool often need to adjust the value */
5447 if (!gst_buffer_pool_set_config(obj->pool, config))
5448 {
5449 config = gst_buffer_pool_get_config(obj->pool);
5450
5451 GST_DEBUG_OBJECT(obj->dbg_obj, "own pool config changed to %" GST_PTR_FORMAT, config);
5452
5453 /* our pool will adjust the maximum buffer, which we are fine with */
5454 if (!gst_buffer_pool_set_config(obj->pool, config))
5455 goto config_failed;
5456 }
5457
5458 /* Now configure the other pool if different */
5459 if (obj->pool != pool)
5460 other_pool = pool;
5461
5462 if (other_pool)
5463 {
5464 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)obj->element;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005465 guint other_min = min;
5466 guint other_max = max;
5467
5468 if (obj->old_other_pool || obj->old_old_other_pool) //jxsdbg for switching
5469 {
5470 obj->outstanding_buf_num = gst_aml_v4l2_object_get_outstanding_capture_buf_num(obj);
5471 other_min = min - obj->outstanding_buf_num;
5472 other_max = max - obj->outstanding_buf_num;
5473 GST_DEBUG_OBJECT(obj, "oop:%p, ooop:%p, outstanding buf num:%d, set min, max to %d,%d",
5474 obj->old_other_pool, obj->old_old_other_pool,
5475 obj->outstanding_buf_num, other_min, other_max);
5476 }
5477
xuesong.jiangae1548e2022-05-06 16:38:46 +08005478 if (self->is_secure_path)
5479 {
5480 params.flags |= GST_MEMORY_FLAG_LAST << 1; // in drmallocator GST_MEMORY_FLAG_LAST << 1 represent GST_MEMORY_FLAG_SECURE
5481 GST_DEBUG_OBJECT(obj, "set secure flag for drmbufferpool flag:0x%x", params.flags);
5482 }
5483 config = gst_buffer_pool_get_config(other_pool);
5484 gst_buffer_pool_config_set_allocator(config, allocator, &params);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005485 gst_buffer_pool_config_set_params (config, caps, size, other_min, other_max);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005486 gst_buffer_pool_config_set_video_alignment(config, &obj->align);
5487
5488 GST_DEBUG_OBJECT(obj->dbg_obj, "setting other pool config to %" GST_PTR_FORMAT, config);
5489
5490 /* if downstream supports video metadata, add this to the pool config */
5491 if (has_video_meta)
5492 {
5493 GST_DEBUG_OBJECT(obj->dbg_obj, "activate Video Meta");
5494 gst_buffer_pool_config_add_option(config,
5495 GST_BUFFER_POOL_OPTION_VIDEO_META);
5496 }
5497
5498 if (!gst_buffer_pool_set_config(other_pool, config))
5499 {
5500 config = gst_buffer_pool_get_config(other_pool);
5501
5502 if (!gst_buffer_pool_config_validate_params(config, caps, size, min,
5503 max))
5504 {
5505 gst_structure_free(config);
5506 goto config_failed;
5507 }
5508
5509 if (!gst_buffer_pool_set_config(other_pool, config))
5510 goto config_failed;
5511 }
5512 }
5513
5514 if (pool)
5515 {
5516 /* For simplicity, simply read back the active configuration, so our base
5517 * class get the right information */
5518 config = gst_buffer_pool_get_config(pool);
5519 gst_buffer_pool_config_get_params(config, NULL, &size, &min, &max);
5520 gst_structure_free(config);
5521 }
5522
5523 if (update)
5524 gst_query_set_nth_allocation_pool(query, 0, pool, size, min, max);
5525 else
5526 gst_query_add_allocation_pool(query, pool, size, min, max);
5527
5528 if (allocator)
5529 gst_object_unref(allocator);
5530
5531 if (pool)
5532 gst_object_unref(pool);
5533
5534 return TRUE;
5535
5536pool_failed:
5537{
5538 /* setup_pool already send the error */
5539 goto cleanup;
5540}
5541config_failed:
5542{
5543 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5544 (_("Failed to configure internal buffer pool.")), (NULL));
5545 goto cleanup;
5546}
5547no_size:
5548{
5549 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5550 (_("Video device did not suggest any buffer size.")), (NULL));
5551 goto cleanup;
5552}
5553cleanup:
5554{
5555 if (allocator)
5556 gst_object_unref(allocator);
5557
5558 if (pool)
5559 gst_object_unref(pool);
5560 return FALSE;
5561}
5562no_downstream_pool:
5563{
5564 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5565 (_("No downstream pool to import from.")),
5566 ("When importing DMABUF or USERPTR, we need a pool to import from"));
5567 return FALSE;
5568}
5569}
5570
5571gboolean
5572gst_aml_v4l2_object_propose_allocation(GstAmlV4l2Object *obj, GstQuery *query)
5573{
5574 GstBufferPool *pool;
5575 /* we need at least 2 buffers to operate */
5576 guint size, min, max;
5577 GstCaps *caps;
5578 gboolean need_pool;
5579
5580 /* Set defaults allocation parameters */
5581 size = obj->info.size;
5582 min = GST_AML_V4L2_MIN_BUFFERS;
5583 max = VIDEO_MAX_FRAME;
5584
5585 gst_query_parse_allocation(query, &caps, &need_pool);
5586
5587 if (caps == NULL)
5588 goto no_caps;
5589
5590 switch (obj->mode)
5591 {
5592 case GST_V4L2_IO_MMAP:
5593 case GST_V4L2_IO_DMABUF:
5594 if ((pool = obj->pool))
5595 gst_object_ref(pool);
5596 break;
5597 default:
5598 pool = NULL;
5599 break;
5600 }
5601
5602 if (pool != NULL)
5603 {
5604 GstCaps *pcaps;
5605 GstStructure *config;
5606
5607 /* we had a pool, check caps */
5608 config = gst_buffer_pool_get_config(pool);
5609 gst_buffer_pool_config_get_params(config, &pcaps, NULL, NULL, NULL);
5610
5611 GST_DEBUG_OBJECT(obj->dbg_obj,
5612 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
5613 if (!gst_caps_is_equal(caps, pcaps))
5614 {
5615 gst_structure_free(config);
5616 gst_object_unref(pool);
5617 goto different_caps;
5618 }
5619 gst_structure_free(config);
5620 }
5621 gst_aml_v4l2_get_driver_min_buffers(obj);
5622
5623 min = MAX(obj->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
5624
5625 gst_query_add_allocation_pool(query, pool, size, min, max);
5626
5627 /* we also support various metadata */
5628 gst_query_add_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
5629
5630 if (pool)
5631 gst_object_unref(pool);
5632
5633 return TRUE;
5634
5635 /* ERRORS */
5636no_caps:
5637{
5638 GST_DEBUG_OBJECT(obj->dbg_obj, "no caps specified");
5639 return FALSE;
5640}
5641different_caps:
5642{
5643 /* different caps, we can't use this pool */
5644 GST_DEBUG_OBJECT(obj->dbg_obj, "pool has different caps");
5645 return FALSE;
5646}
5647}
5648
5649gboolean
5650gst_aml_v4l2_object_try_import(GstAmlV4l2Object *obj, GstBuffer *buffer)
5651{
5652 GstVideoMeta *vmeta;
5653 guint n_mem = gst_buffer_n_memory(buffer);
5654
5655 /* only import if requested */
5656 switch (obj->mode)
5657 {
5658 case GST_V4L2_IO_USERPTR:
5659 case GST_V4L2_IO_DMABUF_IMPORT:
5660 break;
5661 default:
5662 GST_DEBUG_OBJECT(obj->dbg_obj,
5663 "The io-mode does not enable importation");
5664 return FALSE;
5665 }
5666
5667 vmeta = gst_buffer_get_video_meta(buffer);
5668 if (!vmeta && obj->need_video_meta)
5669 {
5670 GST_DEBUG_OBJECT(obj->dbg_obj, "Downstream buffer uses standard "
5671 "stride/offset while the driver does not.");
5672 return FALSE;
5673 }
5674
5675 /* we need matching strides/offsets and size */
5676 if (vmeta)
5677 {
5678 guint p;
5679 gboolean need_fmt_update = FALSE;
5680
5681 if (vmeta->n_planes != GST_VIDEO_INFO_N_PLANES(&obj->info))
5682 {
5683 GST_WARNING_OBJECT(obj->dbg_obj,
5684 "Cannot import buffers with different number planes");
5685 return FALSE;
5686 }
5687
5688 for (p = 0; p < vmeta->n_planes; p++)
5689 {
5690 if (vmeta->stride[p] < obj->info.stride[p])
5691 {
5692 GST_DEBUG_OBJECT(obj->dbg_obj,
5693 "Not importing as remote stride %i is smaller then %i on plane %u",
5694 vmeta->stride[p], obj->info.stride[p], p);
5695 return FALSE;
5696 }
5697 else if (vmeta->stride[p] > obj->info.stride[p])
5698 {
5699 need_fmt_update = TRUE;
5700 }
5701
5702 if (vmeta->offset[p] < obj->info.offset[p])
5703 {
5704 GST_DEBUG_OBJECT(obj->dbg_obj,
5705 "Not importing as offset %" G_GSIZE_FORMAT
5706 " is smaller then %" G_GSIZE_FORMAT " on plane %u",
5707 vmeta->offset[p], obj->info.offset[p], p);
5708 return FALSE;
5709 }
5710 else if (vmeta->offset[p] > obj->info.offset[p])
5711 {
5712 need_fmt_update = TRUE;
5713 }
5714 }
5715
5716 if (need_fmt_update)
5717 {
5718 struct v4l2_format format;
5719 gint wanted_stride[GST_VIDEO_MAX_PLANES] = {
5720 0,
5721 };
5722
5723 format = obj->format;
5724
5725 /* update the current format with the stride we want to import from */
5726 if (V4L2_TYPE_IS_MULTIPLANAR(obj->type))
5727 {
5728 guint i;
5729
5730 GST_DEBUG_OBJECT(obj->dbg_obj, "Wanted strides:");
5731
5732 for (i = 0; i < obj->n_v4l2_planes; i++)
5733 {
5734 gint stride = vmeta->stride[i];
5735
5736 if (GST_VIDEO_FORMAT_INFO_IS_TILED(obj->info.finfo))
5737 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(obj->info.finfo);
5738
5739 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
5740 wanted_stride[i] = stride;
5741 GST_DEBUG_OBJECT(obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
5742 }
5743 }
5744 else
5745 {
5746 gint stride = vmeta->stride[0];
5747
5748 GST_DEBUG_OBJECT(obj->dbg_obj, "Wanted stride: %i", stride);
5749
5750 if (GST_VIDEO_FORMAT_INFO_IS_TILED(obj->info.finfo))
5751 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(obj->info.finfo);
5752
5753 format.fmt.pix.bytesperline = stride;
5754 wanted_stride[0] = stride;
5755 }
5756
5757 if (obj->ioctl(obj->video_fd, VIDIOC_S_FMT, &format) < 0)
5758 {
5759 GST_WARNING_OBJECT(obj->dbg_obj,
5760 "Something went wrong trying to update current format: %s",
5761 g_strerror(errno));
5762 return FALSE;
5763 }
5764
5765 gst_aml_v4l2_object_save_format(obj, obj->fmtdesc, &format, &obj->info,
5766 &obj->align);
5767
5768 if (V4L2_TYPE_IS_MULTIPLANAR(obj->type))
5769 {
5770 guint i;
5771
5772 for (i = 0; i < obj->n_v4l2_planes; i++)
5773 {
5774 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i])
5775 {
5776 GST_DEBUG_OBJECT(obj->dbg_obj,
5777 "[%i] Driver did not accept the new stride (wants %i, got %i)",
5778 i, format.fmt.pix_mp.plane_fmt[i].bytesperline,
5779 wanted_stride[i]);
5780 return FALSE;
5781 }
5782 }
5783 }
5784 else
5785 {
5786 if (format.fmt.pix.bytesperline != wanted_stride[0])
5787 {
5788 GST_DEBUG_OBJECT(obj->dbg_obj,
5789 "Driver did not accept the new stride (wants %i, got %i)",
5790 format.fmt.pix.bytesperline, wanted_stride[0]);
5791 return FALSE;
5792 }
5793 }
5794 }
5795 }
5796
5797 /* we can always import single memory buffer, but otherwise we need the same
5798 * amount of memory object. */
5799 if (n_mem != 1 && n_mem != obj->n_v4l2_planes)
5800 {
5801 GST_DEBUG_OBJECT(obj->dbg_obj, "Can only import %i memory, "
5802 "buffers contains %u memory",
5803 obj->n_v4l2_planes, n_mem);
5804 return FALSE;
5805 }
5806
5807 /* For DMABuf importation we need DMABuf of course */
5808 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT)
5809 {
5810 guint i;
5811
5812 for (i = 0; i < n_mem; i++)
5813 {
5814 GstMemory *mem = gst_buffer_peek_memory(buffer, i);
5815
5816 if (!gst_is_dmabuf_memory(mem))
5817 {
5818 GST_DEBUG_OBJECT(obj->dbg_obj, "Cannot import non-DMABuf memory.");
5819 return FALSE;
5820 }
5821 }
5822 }
5823
5824 /* for the remaining, only the kernel driver can tell */
5825 return TRUE;
5826}
5827
xuesong.jiang22a9b112023-05-24 09:01:59 +00005828static gboolean gst_aml_v4l2_set_control(GstAmlV4l2Object *v4l2object, guint ctl)
5829{
5830 int rc;
5831 struct v4l2_queryctrl queryctrl;
5832 struct v4l2_control control;
5833
5834 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)v4l2object->element;
5835 self->is_secure_path = TRUE;
5836
5837 memset(&queryctrl, 0, sizeof(queryctrl));
5838 queryctrl.id = ctl;
5839
5840 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_QUERYCTRL, &queryctrl);
5841 if (rc == 0)
5842 {
5843 if (!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED))
5844 {
5845 memset(&control, 0, sizeof(control));
5846 control.id = ctl;
5847 control.value = 1;
5848 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_CTRL, &control);
5849 if (rc != 0)
5850 {
5851 GST_ERROR_OBJECT(v4l2object->dbg_obj, "set ctl:0x%x fail rc %d", ctl, rc);
5852 return FALSE;
5853 }
5854 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set ctl:0x%x succ", ctl);
5855 return TRUE;
5856 }
5857 else
5858 {
5859 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "ctl:0x%x is disabled", ctl);
5860 return TRUE;
5861 }
5862 }
5863 else
5864 {
5865 GST_ERROR_OBJECT(v4l2object->dbg_obj, "VIDIOC_QUERYCTRL for 0x:%x fail", ctl);
5866 return FALSE;
5867 }
5868}
5869
5870
xuesong.jiangae1548e2022-05-06 16:38:46 +08005871gboolean gst_aml_v4l2_set_drm_mode(GstAmlV4l2Object *v4l2object)
5872{
5873 /* On AmLogic, output obj use of GST_V4L2_IO_DMABUF_IMPORT implies secure memory */
5874 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
5875 {
xuesong.jiangae1548e2022-05-06 16:38:46 +08005876
xuesong.jiang22a9b112023-05-24 09:01:59 +00005877 if (gst_aml_v4l2_set_control(v4l2object, AML_V4L2_SET_DRMMODE))
xuesong.jiangae1548e2022-05-06 16:38:46 +08005878 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00005879 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_DRMMODE set succ");
5880 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005881 }
5882 else
5883 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00005884 GST_ERROR_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_DRMMODE set fail");
xuesong.jiangae1548e2022-05-06 16:38:46 +08005885 return FALSE;
5886 }
5887 }
5888 else
5889 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00005890 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "req mode is not GST_V4L2_IO_DMABUF_IMPORT, DRM mode does not need to be configured");
5891 return TRUE;
5892 }
5893}
5894
5895gboolean gst_aml_v4l2_set_stream_mode(GstAmlV4l2Object *v4l2object)
5896{
5897 if (v4l2object->stream_mode)
5898 {
5899 if (gst_aml_v4l2_set_control(v4l2object, AML_V4L2_SET_STREAM_MODE))
5900 {
5901 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_STREAM_MODE set succ");
5902 return TRUE;
5903 }
5904 else
5905 {
5906 GST_ERROR_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_STREAM_MODE set fail");
5907 return FALSE;
5908 }
5909 }
5910 else
5911 {
5912 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "req mode is not stream mode, frame mode in configured by default");
xuesong.jiangae1548e2022-05-06 16:38:46 +08005913 return TRUE;
5914 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005915}
5916
5917gint gst_aml_v4l2_object_get_outstanding_capture_buf_num(GstAmlV4l2Object *obj)
5918{
5919 gint ret = 0;
5920 gint count = 0;
5921
5922 if (obj->old_other_pool)
5923 {
5924 count = gst_buffer_pool_get_outstanding_num(obj->old_other_pool);
5925 if (count)
5926 {
5927 ret += count;
5928 }
5929 else
5930 {
5931 gst_object_unref(obj->old_other_pool);
5932 obj->old_other_pool = NULL;
5933 }
5934 }
5935
5936 count = 0;
5937 if (obj->old_old_other_pool)
5938 {
5939 count = gst_buffer_pool_get_outstanding_num(obj->old_old_other_pool);
5940 if (count)
5941 {
5942 ret += count;
5943 }
5944 else
5945 {
5946 gst_object_unref(obj->old_old_other_pool);
5947 obj->old_old_other_pool = NULL;
5948 }
5949 }
5950
5951 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005952}