blob: a24a363f92522a81f2e925784b3d3ee9a690c058 [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
xuesong.jiangc5dac0f2023-02-01 14:42:24 +080054#define ENCODED_BUFFER_SIZE (3 * 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
3758 if ( gst_structure_has_field(structure, "mastering-display-metadata") )
3759 {
3760 const char *masteringDisplay= gst_structure_get_string(structure,"mastering-display-metadata");
3761 float hdrMasteringDisplay[10];
3762 if ( masteringDisplay && sscanf( masteringDisplay, "%f:%f:%f:%f:%f:%f:%f:%f:%f:%f",
3763 &hdrMasteringDisplay[0],
3764 &hdrMasteringDisplay[1],
3765 &hdrMasteringDisplay[2],
3766 &hdrMasteringDisplay[3],
3767 &hdrMasteringDisplay[4],
3768 &hdrMasteringDisplay[5],
3769 &hdrMasteringDisplay[6],
3770 &hdrMasteringDisplay[7],
3771 &hdrMasteringDisplay[8],
3772 &hdrMasteringDisplay[9] ) == 10 )
3773 {
3774 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "mastering display [%f,%f,%f,%f,%f,%f,%f,%f,%f,%f]",
3775 hdrMasteringDisplay[0],
3776 hdrMasteringDisplay[1],
3777 hdrMasteringDisplay[2],
3778 hdrMasteringDisplay[3],
3779 hdrMasteringDisplay[4],
3780 hdrMasteringDisplay[5],
3781 hdrMasteringDisplay[6],
3782 hdrMasteringDisplay[7],
3783 hdrMasteringDisplay[8],
3784 hdrMasteringDisplay[9] );
3785
3786 decParm->hdr.color_parms.present_flag= 1;
3787 decParm->hdr.color_parms.primaries[2][0]= (uint32_t)(hdrMasteringDisplay[0]*50000); /* R.x */
3788 decParm->hdr.color_parms.primaries[2][1]= (uint32_t)(hdrMasteringDisplay[1]*50000); /* R.y */
3789 decParm->hdr.color_parms.primaries[0][0]= (uint32_t)(hdrMasteringDisplay[2]*50000); /* G.x */
3790 decParm->hdr.color_parms.primaries[0][1]= (uint32_t)(hdrMasteringDisplay[3]*50000); /* G.y */
3791 decParm->hdr.color_parms.primaries[1][0]= (uint32_t)(hdrMasteringDisplay[4]*50000); /* B.x */
3792 decParm->hdr.color_parms.primaries[1][1]= (uint32_t)(hdrMasteringDisplay[5]*50000); /* B.y */
3793 decParm->hdr.color_parms.white_point[0]= (uint32_t)(hdrMasteringDisplay[6]*50000);
3794 decParm->hdr.color_parms.white_point[1]= (uint32_t)(hdrMasteringDisplay[7]*50000);
3795 decParm->hdr.color_parms.luminance[0]= (uint32_t)(hdrMasteringDisplay[8]);
3796 decParm->hdr.color_parms.luminance[1]= (uint32_t)(hdrMasteringDisplay[9]);
3797 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: primaries %X %X %X %X %X %X",
3798 decParm->hdr.color_parms.primaries[2][0],
3799 decParm->hdr.color_parms.primaries[2][1],
3800 decParm->hdr.color_parms.primaries[0][0],
3801 decParm->hdr.color_parms.primaries[0][1],
3802 decParm->hdr.color_parms.primaries[1][0],
3803 decParm->hdr.color_parms.primaries[1][1] );
3804 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: white point: %X %X",
3805 decParm->hdr.color_parms.white_point[0],
3806 decParm->hdr.color_parms.white_point[1] );
3807 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: luminance: %X %X",
3808 decParm->hdr.color_parms.luminance[0],
3809 decParm->hdr.color_parms.luminance[1] );
3810 }
3811
3812 GstStructure *st = gst_caps_get_structure(caps, 0);
3813 GstCapsFeatures * features = gst_caps_get_features(caps, 0);
3814 if (st && features)
3815 {
3816 gst_structure_remove_fields(st, "mastering-display-metadata", NULL);
3817 gst_caps_features_remove(features, "mastering-display-metadata");
3818 }
3819 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "caps after remove mastering-display-metadata %" GST_PTR_FORMAT, caps);
3820 }
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003821
3822 if (uname(&info) || sscanf(info.release, "%d.%d", &major, &minor) <= 0)
3823 {
3824 GST_DEBUG("get linux version failed");
3825 }
3826 GST_DEBUG("linux major version %d %d", major,minor);
3827
3828 use_ext_config = ((major == 5 && minor >= 15) || major >= 6) ? TRUE: FALSE;
3829
3830 if (use_ext_config)
3831 {
3832 memset(&ctrls, 0, sizeof(ctrls));
3833 memset(&control, 0, sizeof(control));
3834 control.id = AML_V4L2_DEC_PARMS_CONFIG;
3835 control.ptr = decParm;
3836 control.size = sizeof(struct aml_dec_params);
3837 ctrls.count = 1;
3838 ctrls.controls = &control;
3839 if (v4l2object->ioctl( v4l2object->video_fd, VIDIOC_S_EXT_CTRLS, &ctrls ) <0)
3840 {
3841 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set vdec parm fail");
3842 }
3843 else
3844 {
3845 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set dwMode to %d, margin to %d", decParm->cfg.double_write_mode, decParm->cfg.ref_buf_margin);
3846 }
3847 }
3848 else
3849 {
3850 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_PARM, streamparm) < 0)
3851 {
3852 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set vdec parm fail");
3853 }
3854 else
3855 {
3856 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Set dwMode to %d, margin to %d", decParm->cfg.double_write_mode, decParm->cfg.ref_buf_margin);
3857 }
3858 }
3859 }
3860 else if (v4l2object->type == V4L2_BUF_TYPE_VBI_CAPTURE || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
3861 {
3862 if (needSpecConfigForFg(v4l2object))
3863 {
3864 decParm->cfg.double_write_mode= VDEC_DW_MMU_1;
3865 GST_DEBUG_OBJECT(v4l2object->dbg_obj,"set fg dw mode %d", decParm->cfg.double_write_mode);
3866 }
3867 }
3868 else
3869 {
3870 GST_ERROR_OBJECT(v4l2object->dbg_obj,"can't deal with. please check flow");
xuesong.jiangae1548e2022-05-06 16:38:46 +08003871 }
3872}
3873
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08003874static gint gst_aml_v4l2_object_get_dw_mode(GstAmlV4l2Object *v4l2object)
3875{
3876 struct v4l2_streamparm streamparm;
3877 struct aml_dec_params *decParm = (struct aml_dec_params *)(&streamparm.parm.raw_data);
3878 memset(&streamparm, 0x00, sizeof(struct v4l2_streamparm));
3879
3880 streamparm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
3881 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_PARM, &streamparm) >= 0)
3882 {
3883 GST_DEBUG_OBJECT(v4l2object, "get dw mode:%d in type V4L2_BUF_TYPE_VIDEO_OUTPUT", decParm->cfg.double_write_mode);
3884 return decParm->cfg.double_write_mode;
3885 }
3886
3887 streamparm.type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
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_MPLANE", decParm->cfg.double_write_mode);
3891 return decParm->cfg.double_write_mode;
3892 }
3893
3894 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");
3895 return -1;
3896}
3897
xuesong.jiangae1548e2022-05-06 16:38:46 +08003898static gboolean
3899gst_aml_v4l2_object_set_format_full(GstAmlV4l2Object *v4l2object, GstCaps *caps,
3900 gboolean try_only, GstAmlV4l2Error *error)
3901{
3902 gint fd = v4l2object->video_fd;
3903 struct v4l2_format format;
3904 struct v4l2_streamparm streamparm;
3905 enum v4l2_field field;
3906 guint32 pixelformat;
3907 struct v4l2_fmtdesc *fmtdesc;
3908 GstVideoInfo info;
3909 GstVideoAlignment align;
3910 gint width, height, fps_n, fps_d;
3911 gint n_v4l_planes;
3912 gint i = 0;
3913 gboolean is_mplane;
3914 enum v4l2_colorspace colorspace = 0;
3915 enum v4l2_quantization range = 0;
3916 enum v4l2_ycbcr_encoding matrix = 0;
3917 enum v4l2_xfer_func transfer = 0;
3918 GstStructure *s;
3919 gboolean disable_colorimetry = FALSE;
3920
3921 g_return_val_if_fail(!v4l2object->skip_try_fmt_probes ||
3922 gst_caps_is_writable(caps),
3923 FALSE);
3924
3925 GST_AML_V4L2_CHECK_OPEN(v4l2object);
3926 if (!try_only)
3927 GST_AML_V4L2_CHECK_NOT_ACTIVE(v4l2object);
3928
xuesong.jiangae1548e2022-05-06 16:38:46 +08003929 is_mplane = V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type);
3930
3931 gst_video_info_init(&info);
3932 gst_video_alignment_reset(&align);
3933
3934 if (!gst_aml_v4l2_object_get_caps_info(v4l2object, caps, &fmtdesc, &info))
3935 goto invalid_caps;
3936
3937 pixelformat = fmtdesc->pixelformat;
3938 width = GST_VIDEO_INFO_WIDTH(&info);
3939 height = GST_VIDEO_INFO_HEIGHT(&info);
3940 fps_n = GST_VIDEO_INFO_FPS_N(&info);
3941 fps_d = GST_VIDEO_INFO_FPS_D(&info);
3942
hanghang.luo9edfc7d2023-05-17 07:01:05 +00003943 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Check image size");
3944 struct v4l2_frmsizeenum size;
3945 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
3946 size.index = 0;
3947 size.pixel_format = pixelformat;
3948 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
3949 return FALSE;
3950 if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE)
3951 {
3952 guint32 maxw, maxh;
3953 maxw = MIN (size.stepwise.max_width, G_MAXINT);
3954 maxh = MIN (size.stepwise.max_height, G_MAXINT);
3955 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "image from caps w_h:%d_%d", width, height);
3956 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "v4l2 support max w_h:%d_%d", maxw, maxh);
3957 if (width*height > maxw*maxh)
3958 return FALSE;
3959 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Check image size ok");
3960 }
3961
fei.deng7c3d67f2022-11-09 11:06:05 +08003962 //set amlogic params here,because we need pix format to set dw mode
3963 memset(&streamparm, 0x00, sizeof(struct v4l2_streamparm));
3964 streamparm.type = v4l2object->type;
3965 set_amlogic_vdec_parm(v4l2object, &streamparm, caps, pixelformat);
3966
xuesong.jiangae1548e2022-05-06 16:38:46 +08003967 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
3968 * or if contiguous is prefered */
3969 n_v4l_planes = GST_VIDEO_INFO_N_PLANES(&info);
3970 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
3971 n_v4l_planes = 1;
3972
3973 if (GST_VIDEO_INFO_IS_INTERLACED(&info))
3974 {
3975 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "interlaced video");
3976 /* ideally we would differentiate between types of interlaced video
3977 * but there is not sufficient information in the caps..
3978 */
3979 field = V4L2_FIELD_INTERLACED;
3980 }
3981 else
3982 {
3983 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "progressive video");
3984 field = V4L2_FIELD_NONE;
3985 }
3986
3987 /* We first pick the main colorspace from the primaries */
3988 switch (info.colorimetry.primaries)
3989 {
3990 case GST_VIDEO_COLOR_PRIMARIES_BT709:
3991 /* There is two colorspaces using these primaries, use the range to
3992 * differentiate */
3993 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
3994 colorspace = V4L2_COLORSPACE_REC709;
3995 else
3996 colorspace = V4L2_COLORSPACE_SRGB;
3997 break;
3998 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
3999 colorspace = V4L2_COLORSPACE_BT2020;
4000 break;
4001 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
4002 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
4003 break;
4004 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4005 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
4006 break;
4007 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4008 colorspace = V4L2_COLORSPACE_SMPTE170M;
4009 break;
4010 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4011 colorspace = V4L2_COLORSPACE_SMPTE240M;
4012 break;
4013
4014 case GST_VIDEO_COLOR_PRIMARIES_FILM:
4015 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4016 /* We don't know, we will guess */
4017 break;
4018
4019 default:
4020 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4021 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
4022 break;
4023 }
4024
4025 switch (info.colorimetry.range)
4026 {
4027 case GST_VIDEO_COLOR_RANGE_0_255:
4028 range = V4L2_QUANTIZATION_FULL_RANGE;
4029 break;
4030 case GST_VIDEO_COLOR_RANGE_16_235:
4031 range = V4L2_QUANTIZATION_LIM_RANGE;
4032 break;
4033 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
4034 /* We let the driver pick a default one */
4035 break;
4036 default:
4037 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4038 "Unknown colorimetry range %d", info.colorimetry.range);
4039 break;
4040 }
4041
4042 switch (info.colorimetry.matrix)
4043 {
4044 case GST_VIDEO_COLOR_MATRIX_RGB:
4045 /* Unspecified, leave to default */
4046 break;
4047 /* FCC is about the same as BT601 with less digit */
4048 case GST_VIDEO_COLOR_MATRIX_FCC:
4049 case GST_VIDEO_COLOR_MATRIX_BT601:
4050 matrix = V4L2_YCBCR_ENC_601;
4051 break;
4052 case GST_VIDEO_COLOR_MATRIX_BT709:
4053 matrix = V4L2_YCBCR_ENC_709;
4054 break;
4055 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4056 matrix = V4L2_YCBCR_ENC_SMPTE240M;
4057 break;
4058 case GST_VIDEO_COLOR_MATRIX_BT2020:
4059 matrix = V4L2_YCBCR_ENC_BT2020;
4060 break;
4061 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4062 /* We let the driver pick a default one */
4063 break;
4064 default:
4065 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4066 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
4067 break;
4068 }
4069
4070 switch (info.colorimetry.transfer)
4071 {
4072 case GST_VIDEO_TRANSFER_GAMMA18:
4073 case GST_VIDEO_TRANSFER_GAMMA20:
4074 case GST_VIDEO_TRANSFER_GAMMA22:
4075 case GST_VIDEO_TRANSFER_GAMMA28:
4076 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4077 "GAMMA 18, 20, 22, 28 transfer functions not supported");
4078 /* fallthrough */
4079 case GST_VIDEO_TRANSFER_GAMMA10:
4080 transfer = V4L2_XFER_FUNC_NONE;
4081 break;
4082 case GST_VIDEO_TRANSFER_BT2020_12:
4083 case GST_VIDEO_TRANSFER_BT709:
4084 transfer = V4L2_XFER_FUNC_709;
4085 break;
4086 case GST_VIDEO_TRANSFER_SMPTE240M:
4087 transfer = V4L2_XFER_FUNC_SMPTE240M;
4088 break;
4089 case GST_VIDEO_TRANSFER_SRGB:
4090 transfer = V4L2_XFER_FUNC_SRGB;
4091 break;
4092 case GST_VIDEO_TRANSFER_LOG100:
4093 case GST_VIDEO_TRANSFER_LOG316:
4094 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4095 "LOG 100, 316 transfer functions not supported");
4096 /* FIXME No known sensible default, maybe AdobeRGB ? */
4097 break;
4098 case GST_VIDEO_TRANSFER_UNKNOWN:
4099 /* We let the driver pick a default one */
4100 break;
4101 default:
4102 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4103 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
4104 break;
4105 }
4106
4107 if (colorspace == 0)
4108 {
4109 /* Try to guess colorspace according to pixelformat and size */
4110 if (GST_VIDEO_INFO_IS_YUV(&info))
4111 {
4112 if (range == V4L2_QUANTIZATION_FULL_RANGE && matrix == V4L2_YCBCR_ENC_601 && transfer == 0)
4113 {
4114 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
4115 * function most likely is JPEG */
4116 colorspace = V4L2_COLORSPACE_JPEG;
4117 transfer = V4L2_XFER_FUNC_SRGB;
4118 }
4119 else
4120 {
4121 /* SD streams likely use SMPTE170M and HD streams REC709 */
4122 if (width <= 720 && height <= 576)
4123 colorspace = V4L2_COLORSPACE_SMPTE170M;
4124 else
4125 colorspace = V4L2_COLORSPACE_REC709;
4126 }
4127 }
4128 else if (GST_VIDEO_INFO_IS_RGB(&info))
4129 {
4130 colorspace = V4L2_COLORSPACE_SRGB;
4131 transfer = V4L2_XFER_FUNC_NONE;
4132 }
4133 }
4134
4135 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired format %dx%d, format "
4136 "%" GST_FOURCC_FORMAT " stride: %d",
4137 width, height,
4138 GST_FOURCC_ARGS(pixelformat), GST_VIDEO_INFO_PLANE_STRIDE(&info, 0));
4139
4140 memset(&format, 0x00, sizeof(struct v4l2_format));
4141 format.type = v4l2object->type;
4142
4143 if (is_mplane)
4144 {
4145 format.type = v4l2object->type;
4146 format.fmt.pix_mp.pixelformat = pixelformat;
4147 format.fmt.pix_mp.width = width;
4148 format.fmt.pix_mp.height = height;
4149 format.fmt.pix_mp.field = field;
4150 format.fmt.pix_mp.num_planes = n_v4l_planes;
4151
4152 /* try to ask our prefered stride but it's not a failure if not
4153 * accepted */
4154 for (i = 0; i < n_v4l_planes; i++)
4155 {
4156 gint stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, i);
4157
4158 if (GST_VIDEO_FORMAT_INFO_IS_TILED(info.finfo))
4159 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(info.finfo);
4160
4161 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
4162 }
4163
4164 if (GST_VIDEO_INFO_FORMAT(&info) == GST_VIDEO_FORMAT_ENCODED)
4165 {
4166 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
4167 format.fmt.pix_mp.plane_fmt[0].sizeimage = 1;
4168 else
4169 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
4170 }
4171 }
4172 else
4173 {
4174 gint stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, 0);
4175
4176 format.type = v4l2object->type;
4177
4178 format.fmt.pix.width = width;
4179 format.fmt.pix.height = height;
4180 format.fmt.pix.pixelformat = pixelformat;
4181 format.fmt.pix.field = field;
4182
4183 if (GST_VIDEO_FORMAT_INFO_IS_TILED(info.finfo))
4184 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(info.finfo);
4185
4186 /* try to ask our prefered stride */
4187 format.fmt.pix.bytesperline = stride;
4188
4189 if (GST_VIDEO_INFO_FORMAT(&info) == GST_VIDEO_FORMAT_ENCODED)
4190 {
4191 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
4192 format.fmt.pix_mp.plane_fmt[0].sizeimage = 1;
4193 else
4194 format.fmt.pix_mp.plane_fmt[0].sizeimage = ENCODED_BUFFER_SIZE;
4195 }
4196 }
4197
4198 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired format is %dx%d, format "
4199 "%" GST_FOURCC_FORMAT ", nb planes %d",
4200 format.fmt.pix.width,
4201 format.fmt.pix_mp.height,
4202 GST_FOURCC_ARGS(format.fmt.pix.pixelformat),
4203 is_mplane ? format.fmt.pix_mp.num_planes : 1);
4204
4205#ifndef GST_DISABLE_GST_DEBUG
4206 if (is_mplane)
4207 {
4208 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
4209 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d",
4210 format.fmt.pix_mp.plane_fmt[i].bytesperline);
4211 }
4212 else
4213 {
4214 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d",
4215 format.fmt.pix.bytesperline);
4216 }
4217#endif
4218
4219 if (is_mplane)
4220 {
4221 format.fmt.pix_mp.colorspace = colorspace;
4222 format.fmt.pix_mp.quantization = range;
4223 format.fmt.pix_mp.ycbcr_enc = matrix;
4224 format.fmt.pix_mp.xfer_func = transfer;
4225 }
4226 else
4227 {
4228 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
4229 format.fmt.pix.colorspace = colorspace;
4230 format.fmt.pix.quantization = range;
4231 format.fmt.pix.ycbcr_enc = matrix;
4232 format.fmt.pix.xfer_func = transfer;
4233 }
4234
4235 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
4236 colorspace, range, matrix, transfer);
4237
4238 if (try_only)
4239 {
4240 if (v4l2object->ioctl(fd, VIDIOC_TRY_FMT, &format) < 0)
4241 goto try_fmt_failed;
4242 }
4243 else
4244 {
4245 if (v4l2object->ioctl(fd, VIDIOC_S_FMT, &format) < 0)
4246 goto set_fmt_failed;
4247 }
4248
4249 if (is_mplane)
4250 {
4251 colorspace = format.fmt.pix_mp.colorspace;
4252 range = format.fmt.pix_mp.quantization;
4253 matrix = format.fmt.pix_mp.ycbcr_enc;
4254 transfer = format.fmt.pix_mp.xfer_func;
4255 }
4256 else
4257 {
4258 colorspace = format.fmt.pix.colorspace;
4259 range = format.fmt.pix.quantization;
4260 matrix = format.fmt.pix.ycbcr_enc;
4261 transfer = format.fmt.pix.xfer_func;
4262 }
4263
4264 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got format of %dx%d, format "
4265 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d",
4266 format.fmt.pix.width, format.fmt.pix_mp.height,
4267 GST_FOURCC_ARGS(format.fmt.pix.pixelformat),
4268 is_mplane ? format.fmt.pix_mp.num_planes : 1,
4269 colorspace, range, matrix, transfer);
4270
4271#ifndef GST_DISABLE_GST_DEBUG
4272 if (is_mplane)
4273 {
4274 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
4275 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d, sizeimage %d",
4276 format.fmt.pix_mp.plane_fmt[i].bytesperline,
4277 format.fmt.pix_mp.plane_fmt[i].sizeimage);
4278 }
4279 else
4280 {
4281 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d, sizeimage %d",
4282 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
4283 }
4284#endif
4285
4286 if (format.fmt.pix.pixelformat != pixelformat)
4287 goto invalid_pixelformat;
4288
4289 /* Only negotiate size with raw data.
4290 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
4291 * in ASF mode for example, there is also not reason for a driver to
4292 * change the size. */
4293 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED)
4294 {
4295 /* We can crop larger images */
4296 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
4297 goto invalid_dimensions;
4298
4299 /* Note, this will be adjusted if upstream has non-centered cropping. */
4300 align.padding_top = 0;
4301 align.padding_bottom = format.fmt.pix.height - height;
4302 align.padding_left = 0;
4303 align.padding_right = format.fmt.pix.width - width;
4304 }
4305
4306 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
4307 goto invalid_planes;
4308
4309 /* used to check colorimetry and interlace mode fields presence */
4310 s = gst_caps_get_structure(caps, 0);
4311
4312 if (!gst_aml_v4l2_object_get_interlace_mode(format.fmt.pix.field,
4313 &info.interlace_mode))
4314 goto invalid_field;
4315 if (gst_structure_has_field(s, "interlace-mode"))
4316 {
4317 if (format.fmt.pix.field != field)
4318 goto invalid_field;
4319 }
4320
4321 if (gst_aml_v4l2_object_get_colorspace(&format, &info.colorimetry))
4322 {
4323 if (gst_structure_has_field(s, "colorimetry"))
4324 {
xuesong.jiange1a19662022-06-21 20:30:22 +08004325 if (!gst_aml_v4l2_video_colorimetry_matches(&info.colorimetry, gst_structure_get_string(s, "colorimetry")))
4326 {
4327 // goto invalid_colorimetry;
4328 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004329 }
4330 }
4331 else
4332 {
4333 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
4334 * the TRY_FMT */
4335 disable_colorimetry = TRUE;
4336 if (gst_structure_has_field(s, "colorimetry"))
4337 gst_structure_remove_field(s, "colorimetry");
4338 }
4339
4340 /* In case we have skipped the try_fmt probes, we'll need to set the
4341 * colorimetry and interlace-mode back into the caps. */
4342 if (v4l2object->skip_try_fmt_probes)
4343 {
4344 if (!disable_colorimetry && !gst_structure_has_field(s, "colorimetry"))
4345 {
4346 gchar *str = gst_video_colorimetry_to_string(&info.colorimetry);
4347 gst_structure_set(s, "colorimetry", G_TYPE_STRING, str, NULL);
4348 g_free(str);
4349 }
4350
4351 if (!gst_structure_has_field(s, "interlace-mode"))
4352 gst_structure_set(s, "interlace-mode", G_TYPE_STRING,
4353 gst_video_interlace_mode_to_string(info.interlace_mode), NULL);
4354 }
4355
4356 if (try_only) /* good enough for trying only */
4357 return TRUE;
4358
4359 if (GST_VIDEO_INFO_HAS_ALPHA(&info))
4360 {
4361 struct v4l2_control ctl = {
4362 0,
4363 };
4364 ctl.id = V4L2_CID_ALPHA_COMPONENT;
4365 ctl.value = 0xff;
4366
4367 if (v4l2object->ioctl(fd, VIDIOC_S_CTRL, &ctl) < 0)
4368 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4369 "Failed to set alpha component value");
4370 }
4371
4372 /* Is there a reason we require the caller to always specify a framerate? */
4373 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
4374 fps_d);
4375
4376 if (v4l2object->ioctl(fd, VIDIOC_G_PARM, &streamparm) < 0)
4377 goto get_parm_failed;
4378
4379 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
4380 {
4381 GST_VIDEO_INFO_FPS_N(&info) =
4382 streamparm.parm.capture.timeperframe.denominator;
4383 GST_VIDEO_INFO_FPS_D(&info) =
4384 streamparm.parm.capture.timeperframe.numerator;
4385
4386 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got capture framerate: %u/%u",
4387 streamparm.parm.capture.timeperframe.denominator,
4388 streamparm.parm.capture.timeperframe.numerator);
4389
4390 /* We used to skip frame rate setup if the camera was already setup
4391 * with the requested frame rate. This breaks some cameras though,
4392 * causing them to not output data (several models of Thinkpad cameras
4393 * have this problem at least).
4394 * So, don't skip. */
4395 GST_LOG_OBJECT(v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
4396 fps_n, fps_d);
4397 /* We want to change the frame rate, so check whether we can. Some cheap USB
4398 * cameras don't have the capability */
4399 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0)
4400 {
4401 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
4402 "Not setting capture framerate (not supported)");
4403 goto done;
4404 }
4405
4406 /* Note: V4L2 wants the frame interval, we have the frame rate */
4407 streamparm.parm.capture.timeperframe.numerator = fps_d;
4408 streamparm.parm.capture.timeperframe.denominator = fps_n;
4409
zengliang.lic9f869d2023-02-15 08:32:32 +00004410 /* Amlogic sets parameters to the decoder and only supports delivery of private structures */
4411 //some cheap USB cam's won't accept any change */
4412 //if (v4l2object->ioctl(fd, VIDIOC_S_PARM, &streamparm) < 0)
4413 //goto set_parm_failed;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004414
4415 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
4416 streamparm.parm.capture.timeperframe.denominator > 0)
4417 {
4418 /* get new values */
4419 fps_d = streamparm.parm.capture.timeperframe.numerator;
4420 fps_n = streamparm.parm.capture.timeperframe.denominator;
4421
4422 GST_INFO_OBJECT(v4l2object->dbg_obj, "Set capture framerate to %u/%u",
4423 fps_n, fps_d);
4424 }
4425 else
4426 {
4427 /* fix v4l2 capture driver to provide framerate values */
4428 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4429 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
4430 }
4431
4432 GST_VIDEO_INFO_FPS_N(&info) = fps_n;
4433 GST_VIDEO_INFO_FPS_D(&info) = fps_d;
4434 }
4435 else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
4436 {
4437 GST_VIDEO_INFO_FPS_N(&info) =
4438 streamparm.parm.output.timeperframe.denominator;
4439 GST_VIDEO_INFO_FPS_D(&info) =
4440 streamparm.parm.output.timeperframe.numerator;
4441
4442 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got output framerate: %u/%u",
4443 streamparm.parm.output.timeperframe.denominator,
4444 streamparm.parm.output.timeperframe.numerator);
4445
4446 GST_LOG_OBJECT(v4l2object->dbg_obj, "Setting output framerate to %u/%u",
4447 fps_n, fps_d);
4448 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0)
4449 {
4450 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
4451 "Not setting output framerate (not supported)");
4452 goto done;
4453 }
4454
4455 /* Note: V4L2 wants the frame interval, we have the frame rate */
4456 streamparm.parm.output.timeperframe.numerator = fps_d;
4457 streamparm.parm.output.timeperframe.denominator = fps_n;
4458
zengliang.lic9f869d2023-02-15 08:32:32 +00004459 /*Amlogic sets parameters to the decoder and only supports delivery of private structures*/
4460 //if (v4l2object->ioctl(fd, VIDIOC_S_PARM, &streamparm) < 0)
4461 //goto set_parm_failed;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004462
4463 if (streamparm.parm.output.timeperframe.numerator > 0 &&
4464 streamparm.parm.output.timeperframe.denominator > 0)
4465 {
4466 /* get new values */
4467 fps_d = streamparm.parm.output.timeperframe.numerator;
4468 fps_n = streamparm.parm.output.timeperframe.denominator;
4469
4470 GST_INFO_OBJECT(v4l2object->dbg_obj, "Set output framerate to %u/%u",
4471 fps_n, fps_d);
4472 }
4473 else
4474 {
4475 /* fix v4l2 output driver to provide framerate values */
4476 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4477 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
4478 }
4479
4480 GST_VIDEO_INFO_FPS_N(&info) = fps_n;
4481 GST_VIDEO_INFO_FPS_D(&info) = fps_d;
4482 }
4483
4484done:
4485 /* add boolean return, so we can fail on drivers bugs */
4486 gst_aml_v4l2_object_save_format(v4l2object, fmtdesc, &format, &info, &align);
4487
4488 /* now configure the pool */
4489 if (!gst_aml_v4l2_object_setup_pool(v4l2object, caps))
4490 goto pool_failed;
4491
4492 return TRUE;
4493
4494 /* ERRORS */
4495invalid_caps:
4496{
4497 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
4498 caps);
4499
4500 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4501 (_("Invalid caps")), ("Can't parse caps %" GST_PTR_FORMAT, caps));
4502 return FALSE;
4503}
4504try_fmt_failed:
4505{
4506 if (errno == EINVAL)
4507 {
4508 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4509 (_("Device '%s' has no supported format"), v4l2object->videodev),
4510 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4511 GST_FOURCC_ARGS(pixelformat), width, height,
4512 g_strerror(errno)));
4513 }
4514 else
4515 {
4516 GST_AML_V4L2_ERROR(error, RESOURCE, FAILED,
4517 (_("Device '%s' failed during initialization"),
4518 v4l2object->videodev),
4519 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4520 GST_FOURCC_ARGS(pixelformat), width, height,
4521 g_strerror(errno)));
4522 }
4523 return FALSE;
4524}
4525set_fmt_failed:
4526{
4527 if (errno == EBUSY)
4528 {
4529 GST_AML_V4L2_ERROR(error, RESOURCE, BUSY,
4530 (_("Device '%s' is busy"), v4l2object->videodev),
4531 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4532 GST_FOURCC_ARGS(pixelformat), width, height,
4533 g_strerror(errno)));
4534 }
4535 else if (errno == EINVAL)
4536 {
4537 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4538 (_("Device '%s' has no supported format"), v4l2object->videodev),
4539 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4540 GST_FOURCC_ARGS(pixelformat), width, height,
4541 g_strerror(errno)));
4542 }
4543 else
4544 {
4545 GST_AML_V4L2_ERROR(error, RESOURCE, FAILED,
4546 (_("Device '%s' failed during initialization"),
4547 v4l2object->videodev),
4548 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4549 GST_FOURCC_ARGS(pixelformat), width, height,
4550 g_strerror(errno)));
4551 }
4552 return FALSE;
4553}
4554invalid_dimensions:
4555{
4556 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4557 (_("Device '%s' cannot capture at %dx%d"),
4558 v4l2object->videodev, width, height),
4559 ("Tried to capture at %dx%d, but device returned size %dx%d",
4560 width, height, format.fmt.pix.width, format.fmt.pix.height));
4561 return FALSE;
4562}
4563invalid_pixelformat:
4564{
4565 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4566 (_("Device '%s' cannot capture in the specified format"),
4567 v4l2object->videodev),
4568 ("Tried to capture in %" GST_FOURCC_FORMAT
4569 ", but device returned format"
4570 " %" GST_FOURCC_FORMAT,
4571 GST_FOURCC_ARGS(pixelformat),
4572 GST_FOURCC_ARGS(format.fmt.pix.pixelformat)));
4573 return FALSE;
4574}
4575invalid_planes:
4576{
4577 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4578 (_("Device '%s' does support non-contiguous planes"),
4579 v4l2object->videodev),
4580 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
4581 return FALSE;
4582}
4583invalid_field:
4584{
4585 enum v4l2_field wanted_field;
4586
4587 if (is_mplane)
4588 wanted_field = format.fmt.pix_mp.field;
4589 else
4590 wanted_field = format.fmt.pix.field;
4591
4592 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4593 (_("Device '%s' does not support %s interlacing"),
4594 v4l2object->videodev,
4595 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
4596 ("Device wants %s interlacing",
4597 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
4598 return FALSE;
4599}
hanghang.luo3128f102022-08-18 10:36:19 +08004600#ifdef DELETE_FOR_LGE
xuesong.jiangae1548e2022-05-06 16:38:46 +08004601invalid_colorimetry:
4602{
4603 gchar *wanted_colorimetry;
4604
4605 wanted_colorimetry = gst_video_colorimetry_to_string(&info.colorimetry);
4606
4607 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4608 (_("Device '%s' does not support %s colorimetry"),
4609 v4l2object->videodev, gst_structure_get_string(s, "colorimetry")),
4610 ("Device wants %s colorimetry", wanted_colorimetry));
4611
4612 g_free(wanted_colorimetry);
4613 return FALSE;
4614}
hanghang.luo3128f102022-08-18 10:36:19 +08004615#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08004616get_parm_failed:
4617{
4618 /* it's possible that this call is not supported */
4619 if (errno != EINVAL && errno != ENOTTY)
4620 {
4621 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4622 (_("Could not get parameters on device '%s'"),
4623 v4l2object->videodev),
4624 GST_ERROR_SYSTEM);
4625 }
4626 goto done;
4627}
4628set_parm_failed:
4629{
4630 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4631 (_("Video device did not accept new frame rate setting.")),
4632 GST_ERROR_SYSTEM);
4633 goto done;
4634}
4635pool_failed:
4636{
4637 /* setup_pool already send the error */
4638 return FALSE;
4639}
4640}
4641
4642gboolean
4643gst_aml_v4l2_object_set_format(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4644 GstAmlV4l2Error *error)
4645{
4646 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
4647 caps);
4648 return gst_aml_v4l2_object_set_format_full(v4l2object, caps, FALSE, error);
4649}
4650
4651gboolean
4652gst_aml_v4l2_object_try_format(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4653 GstAmlV4l2Error *error)
4654{
4655 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
4656 caps);
4657 return gst_aml_v4l2_object_set_format_full(v4l2object, caps, TRUE, error);
4658}
4659
4660GstFlowReturn
4661gst_aml_v4l2_object_poll(GstAmlV4l2Object *v4l2object)
4662{
4663 gint ret;
4664
4665 if (!v4l2object->can_poll_device)
4666 goto done;
4667
4668 GST_LOG_OBJECT(v4l2object, "polling device");
4669
4670again:
4671 ret = gst_poll_wait(v4l2object->poll, GST_CLOCK_TIME_NONE);
4672 if (G_UNLIKELY(ret < 0))
4673 {
4674 switch (errno)
4675 {
4676 case EBUSY:
4677 goto stopped;
4678 case EAGAIN:
4679 case EINTR:
4680 goto again;
4681 case ENXIO:
4682 GST_WARNING_OBJECT(v4l2object,
4683 "v4l2 device doesn't support polling. Disabling"
4684 " using libv4l2 in this case may cause deadlocks");
4685 v4l2object->can_poll_device = FALSE;
4686 goto done;
4687 default:
4688 goto select_error;
4689 }
4690 }
4691
4692done:
4693 return GST_FLOW_OK;
4694
4695 /* ERRORS */
4696stopped:
4697{
4698 GST_DEBUG_OBJECT(v4l2object, "stop called");
4699 return GST_FLOW_FLUSHING;
4700}
4701select_error:
4702{
4703 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, READ, (NULL),
4704 ("poll error %d: %s (%d)", ret, g_strerror(errno), errno));
4705 return GST_FLOW_ERROR;
4706}
4707}
4708
4709GstFlowReturn
4710gst_aml_v4l2_object_dqevent(GstAmlV4l2Object *v4l2object)
4711{
4712 GstFlowReturn res;
4713 struct v4l2_event evt;
4714
4715 if ((res = gst_aml_v4l2_object_poll(v4l2object)) != GST_FLOW_OK)
4716 goto poll_failed;
4717
4718 memset(&evt, 0x00, sizeof(struct v4l2_event));
4719 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DQEVENT, &evt) < 0)
4720 goto dqevent_failed;
4721
4722 switch (evt.type)
4723 {
4724 case V4L2_EVENT_SOURCE_CHANGE:
4725 return GST_AML_V4L2_FLOW_SOURCE_CHANGE;
4726 break;
4727 case V4L2_EVENT_EOS:
4728 return GST_AML_V4L2_FLOW_LAST_BUFFER;
4729 break;
4730 default:
4731 break;
4732 }
4733
4734 return GST_FLOW_OK;
4735
4736 /* ERRORS */
4737poll_failed:
4738{
4739 GST_DEBUG_OBJECT(v4l2object, "poll error %s", gst_flow_get_name(res));
4740 return res;
4741}
4742dqevent_failed:
4743{
4744 return GST_FLOW_ERROR;
4745}
4746}
4747
4748/**
4749 * gst_aml_v4l2_object_acquire_format:
4750 * @v4l2object the object
4751 * @info a GstVideoInfo to be filled
4752 *
4753 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
4754 * the output format is choosen by the HW.
4755 *
4756 * Returns: %TRUE on success, %FALSE on failure.
4757 */
4758gboolean
4759gst_aml_v4l2_object_acquire_format(GstAmlV4l2Object *v4l2object, GstVideoInfo *info)
4760{
4761 struct v4l2_fmtdesc *fmtdesc;
4762 struct v4l2_format fmt;
4763 struct v4l2_crop crop;
4764 struct v4l2_selection sel;
sheng.liu641aa422023-12-26 07:05:59 +00004765 struct v4l2_cropcap cropcap;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004766 struct v4l2_rect *r = NULL;
4767 GstVideoFormat format;
4768 guint width, height;
4769 GstVideoAlignment align;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004770 gint dw_mode;
sheng.liu641aa422023-12-26 07:05:59 +00004771 gdouble pixelAspectRatio = 0.0;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004772
4773 gst_video_info_init(info);
4774 gst_video_alignment_reset(&align);
4775
4776 memset(&fmt, 0x00, sizeof(struct v4l2_format));
4777 fmt.type = v4l2object->type;
fei.denge9458472023-04-18 02:05:48 +00004778 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "fmt.type:%d", fmt.type);
xuesong.jiangae1548e2022-05-06 16:38:46 +08004779 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
4780 goto get_fmt_failed;
4781
4782 fmtdesc = gst_aml_v4l2_object_get_format_from_fourcc(v4l2object,
4783 fmt.fmt.pix.pixelformat);
4784 if (fmtdesc == NULL)
4785 goto unsupported_format;
4786
4787 /* No need to care about mplane, the four first params are the same */
4788 format = gst_aml_v4l2_object_v4l2fourcc_to_video_format(fmt.fmt.pix.pixelformat);
4789
4790 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
4791 if (format == GST_VIDEO_FORMAT_UNKNOWN)
4792 goto unsupported_format;
4793
4794 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
4795 goto invalid_dimensions;
4796
4797 width = fmt.fmt.pix.width;
4798 height = fmt.fmt.pix.height;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004799 /* Use the default compose rectangle */
4800 memset(&sel, 0, sizeof(struct v4l2_selection));
4801 sel.type = v4l2object->type;
4802 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
4803 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
4804 {
4805 r = &sel.r;
4806 }
4807 else
4808 {
4809 /* For ancient kernels, fall back to G_CROP */
4810 memset(&crop, 0, sizeof(struct v4l2_crop));
4811 crop.type = v4l2object->type;
4812 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
4813 r = &crop.c;
4814 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004815
4816 dw_mode = gst_aml_v4l2_object_get_dw_mode(v4l2object);
sheng.liu07337c82023-12-01 03:21:57 +00004817 if (r)
xuesong.jiangae1548e2022-05-06 16:38:46 +08004818 {
4819 align.padding_left = r->left;
4820 align.padding_top = r->top;
4821 align.padding_right = width - r->width - r->left;
4822 align.padding_bottom = height - r->height - r->top;
4823 width = r->width;
4824 height = r->height;
sheng.liu07337c82023-12-01 03:21:57 +00004825 if (dw_mode >= 0 && dw_mode != 16)
4826 {
4827 width = (width/2) *2; /* align for dw*/
4828 height = (height/2) *2; /* align for dw*/
4829 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004830 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004831 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "final w:%d, h:%d", width, height);
xuesong.jiangae1548e2022-05-06 16:38:46 +08004832
4833 gst_video_info_set_format(info, format, width, height);
4834
4835 switch (fmt.fmt.pix.field)
4836 {
4837 case V4L2_FIELD_ANY:
4838 case V4L2_FIELD_NONE:
4839 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
4840 break;
4841 case V4L2_FIELD_INTERLACED:
4842 case V4L2_FIELD_INTERLACED_TB:
4843 case V4L2_FIELD_INTERLACED_BT:
4844 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
4845 break;
4846 default:
4847 goto unsupported_field;
4848 }
4849
4850 gst_aml_v4l2_object_get_colorspace(&fmt, &info->colorimetry);
4851
4852 gst_aml_v4l2_object_save_format(v4l2object, fmtdesc, &fmt, info, &align);
4853
sheng.liu641aa422023-12-26 07:05:59 +00004854 if (v4l2object->par)
4855 {
4856 width = gst_value_get_fraction_numerator(v4l2object->par);
4857 height = gst_value_get_fraction_denominator(v4l2object->par);
4858 pixelAspectRatio = (gdouble)width/(gdouble)height;
4859 }
4860
4861 if (!v4l2object->par || pixelAspectRatio == 1.0)
4862 {
4863 memset(&cropcap, 0, sizeof(cropcap));
4864 width= height= 1;
4865 cropcap.type = v4l2object->type;
4866 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) >= 0)
4867 {
4868 width= cropcap.pixelaspect.denominator;
4869 height= cropcap.pixelaspect.numerator;
4870 GST_DEBUG("cropcap: pixel aspect ratio %d:%d", width, height);
4871 if ( !width || !height )
4872 {
4873 GST_DEBUG("force pixel aspect of 1:1");
4874 width= height= 1;
4875 }
4876 }
sheng.liu641aa422023-12-26 07:05:59 +00004877 }
sheng.liudb26f7d2024-01-22 11:24:23 +00004878
sheng.liud9027ca2024-01-24 09:21:49 +00004879 GST_VIDEO_INFO_PAR_N(info) = width;
4880 GST_VIDEO_INFO_PAR_D(info) = height;
4881
sheng.liudb26f7d2024-01-22 11:24:23 +00004882 if (v4l2object->fps)
4883 {
4884 GST_VIDEO_INFO_FPS_N(info) = gst_value_get_fraction_numerator(v4l2object->fps);
4885 GST_VIDEO_INFO_FPS_D(info) = gst_value_get_fraction_denominator(v4l2object->fps);
4886 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004887 /* Shall we setup the pool ? */
4888
4889 return TRUE;
4890
4891get_fmt_failed:
4892{
4893 GST_ELEMENT_WARNING(v4l2object->element, RESOURCE, SETTINGS,
4894 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
4895 return FALSE;
4896}
4897invalid_dimensions:
4898{
4899 GST_ELEMENT_WARNING(v4l2object->element, RESOURCE, SETTINGS,
4900 (_("Video device returned invalid dimensions.")),
4901 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
4902 fmt.fmt.pix.height));
4903 return FALSE;
4904}
4905unsupported_field:
4906{
4907 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
4908 (_("Video device uses an unsupported interlacing method.")),
4909 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
4910 return FALSE;
4911}
4912unsupported_format:
4913{
4914 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
4915 (_("Video device uses an unsupported pixel format.")),
4916 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
4917 GST_FOURCC_ARGS(fmt.fmt.pix.pixelformat)));
4918 return FALSE;
4919}
4920}
4921
4922gboolean
4923gst_aml_v4l2_object_set_crop(GstAmlV4l2Object *obj)
4924{
4925 struct v4l2_selection sel = {0};
4926 struct v4l2_crop crop = {0};
4927
4928 sel.type = obj->type;
4929 sel.target = V4L2_SEL_TGT_CROP;
4930 sel.flags = 0;
4931 sel.r.left = obj->align.padding_left;
4932 sel.r.top = obj->align.padding_top;
4933 sel.r.width = obj->info.width;
4934 sel.r.height = obj->info.height;
4935
4936 crop.type = obj->type;
4937 crop.c = sel.r;
4938
4939 if (obj->align.padding_left + obj->align.padding_top +
4940 obj->align.padding_right + obj->align.padding_bottom ==
4941 0)
4942 {
4943 GST_DEBUG_OBJECT(obj->dbg_obj, "no cropping needed");
4944 return TRUE;
4945 }
4946
4947 GST_DEBUG_OBJECT(obj->dbg_obj,
4948 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4949 crop.c.width, crop.c.height);
4950
4951 if (obj->ioctl(obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0)
4952 {
4953 if (errno != ENOTTY)
4954 {
4955 GST_WARNING_OBJECT(obj->dbg_obj,
4956 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
4957 g_strerror(errno));
4958 return FALSE;
4959 }
4960 else
4961 {
4962 if (obj->ioctl(obj->video_fd, VIDIOC_S_CROP, &crop) < 0)
4963 {
4964 GST_WARNING_OBJECT(obj->dbg_obj, "VIDIOC_S_CROP failed");
4965 return FALSE;
4966 }
4967
4968 if (obj->ioctl(obj->video_fd, VIDIOC_G_CROP, &crop) < 0)
4969 {
4970 GST_WARNING_OBJECT(obj->dbg_obj, "VIDIOC_G_CROP failed");
4971 return FALSE;
4972 }
4973
4974 sel.r = crop.c;
4975 }
4976 }
4977
4978 GST_DEBUG_OBJECT(obj->dbg_obj,
4979 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
4980 crop.c.width, crop.c.height);
4981
4982 return TRUE;
4983}
4984
4985gboolean
4986gst_aml_v4l2_object_caps_equal(GstAmlV4l2Object *v4l2object, GstCaps *caps)
4987{
4988 GstStructure *config;
4989 GstCaps *oldcaps;
4990 gboolean ret;
4991
4992 if (!v4l2object->pool)
4993 return FALSE;
4994
4995 config = gst_buffer_pool_get_config(v4l2object->pool);
4996 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
4997
4998 ret = oldcaps && gst_caps_is_equal(caps, oldcaps);
4999
5000 gst_structure_free(config);
5001
5002 return ret;
5003}
5004
5005gboolean
5006gst_aml_v4l2_object_caps_is_subset(GstAmlV4l2Object *v4l2object, GstCaps *caps)
5007{
5008 GstStructure *config;
5009 GstCaps *oldcaps;
5010 gboolean ret;
5011
5012 if (!v4l2object->pool)
5013 return FALSE;
5014
5015 config = gst_buffer_pool_get_config(v4l2object->pool);
5016 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5017
5018 ret = oldcaps && gst_caps_is_subset(oldcaps, caps);
5019
5020 gst_structure_free(config);
5021
5022 return ret;
5023}
5024
5025GstCaps *
5026gst_aml_v4l2_object_get_current_caps(GstAmlV4l2Object *v4l2object)
5027{
5028 GstStructure *config;
5029 GstCaps *oldcaps;
5030
5031 if (!v4l2object->pool)
5032 return NULL;
5033
5034 config = gst_buffer_pool_get_config(v4l2object->pool);
5035 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5036
5037 if (oldcaps)
5038 gst_caps_ref(oldcaps);
5039
5040 gst_structure_free(config);
5041
5042 return oldcaps;
5043}
5044
5045gboolean
5046gst_aml_v4l2_object_unlock(GstAmlV4l2Object *v4l2object)
5047{
5048 gboolean ret = TRUE;
5049
5050 GST_LOG_OBJECT(v4l2object->dbg_obj, "start flushing");
5051
5052 gst_poll_set_flushing(v4l2object->poll, TRUE);
5053
5054 if (v4l2object->pool && gst_buffer_pool_is_active(v4l2object->pool))
5055 gst_buffer_pool_set_flushing(v4l2object->pool, TRUE);
5056
5057 return ret;
5058}
5059
5060gboolean
5061gst_aml_v4l2_object_unlock_stop(GstAmlV4l2Object *v4l2object)
5062{
5063 gboolean ret = TRUE;
5064
5065 GST_LOG_OBJECT(v4l2object->dbg_obj, "stop flushing");
5066
5067 if (v4l2object->pool && gst_buffer_pool_is_active(v4l2object->pool))
5068 gst_buffer_pool_set_flushing(v4l2object->pool, FALSE);
5069
5070 gst_poll_set_flushing(v4l2object->poll, FALSE);
5071
5072 return ret;
5073}
5074
5075gboolean
5076gst_aml_v4l2_object_stop(GstAmlV4l2Object *v4l2object)
5077{
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005078 GstAmlV4l2BufferPool *bpool = GST_AML_V4L2_BUFFER_POOL(v4l2object->pool);
5079
xuesong.jiangae1548e2022-05-06 16:38:46 +08005080 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "stopping");
5081
5082 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
5083 goto done;
5084 if (!GST_AML_V4L2_IS_ACTIVE(v4l2object))
5085 goto done;
5086
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005087 if (bpool && bpool->other_pool) /* jxsdbg for resolution switch */
5088 {
5089 if (v4l2object->old_other_pool)
5090 {
5091 /* this case indicate 1st switch did not wait all old pool buf recycle and 2nd switch is coming.
5092 so save 1st old pool */
5093 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "switching occurs during last switching buf recycle flow");
5094 v4l2object->old_old_other_pool = v4l2object->old_other_pool;
5095 }
5096
5097 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "switching flow, ref old drmbufferpool");
5098 v4l2object->old_other_pool = bpool->other_pool;
5099 gst_object_ref(v4l2object->old_other_pool);
5100 }
5101
xuesong.jiangae1548e2022-05-06 16:38:46 +08005102 if (v4l2object->pool)
5103 {
5104 if (!gst_aml_v4l2_buffer_pool_orphan(&v4l2object->pool))
5105 {
5106 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "deactivating pool");
5107 gst_buffer_pool_set_active(v4l2object->pool, FALSE);
5108 gst_object_unref(v4l2object->pool);
5109 }
5110 v4l2object->pool = NULL;
5111 }
5112
5113 GST_AML_V4L2_SET_INACTIVE(v4l2object);
5114
5115done:
5116 return TRUE;
5117}
5118
5119GstCaps *
5120gst_aml_v4l2_object_probe_caps(GstAmlV4l2Object *v4l2object, GstCaps *filter)
5121{
5122 GstCaps *ret;
5123 GSList *walk;
5124 GSList *formats;
5125
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005126 GST_INFO_OBJECT(v4l2object->dbg_obj, "filter caps: %" GST_PTR_FORMAT, filter);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005127 formats = gst_aml_v4l2_object_get_format_list(v4l2object);
5128
5129 ret = gst_caps_new_empty();
5130
sheng.liu641aa422023-12-26 07:05:59 +00005131// At this time, decoder will return defult aspect, and it is not usful.
5132// so, do not probe cropcap at this time and do this action after decoding.
5133#if 0
xuesong.jiangae1548e2022-05-06 16:38:46 +08005134 if (v4l2object->keep_aspect && !v4l2object->par)
5135 {
5136 struct v4l2_cropcap cropcap;
5137
5138 memset(&cropcap, 0, sizeof(cropcap));
5139
5140 cropcap.type = v4l2object->type;
5141 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
5142 {
5143 if (errno != ENOTTY)
5144 GST_WARNING_OBJECT(v4l2object->dbg_obj,
5145 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
5146 g_strerror(errno));
5147 }
5148 else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator)
5149 {
5150 v4l2object->par = g_new0(GValue, 1);
5151 g_value_init(v4l2object->par, GST_TYPE_FRACTION);
5152 gst_value_set_fraction(v4l2object->par, cropcap.pixelaspect.numerator,
5153 cropcap.pixelaspect.denominator);
5154 }
5155 }
sheng.liu641aa422023-12-26 07:05:59 +00005156#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08005157
5158 for (walk = formats; walk; walk = walk->next)
5159 {
5160 struct v4l2_fmtdesc *format;
5161 GstStructure *template;
5162 GstCaps *tmp, *tmp2;
5163
5164 format = (struct v4l2_fmtdesc *)walk->data;
5165
5166 template = gst_aml_v4l2_object_v4l2fourcc_to_bare_struct(format->pixelformat);
5167
5168 if (!template)
5169 {
5170 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
5171 "unknown format %" GST_FOURCC_FORMAT,
5172 GST_FOURCC_ARGS(format->pixelformat));
5173 continue;
5174 }
5175
5176 /* If we have a filter, check if we need to probe this format or not */
5177 if (filter)
5178 {
5179 GstCaps *format_caps = gst_caps_new_empty();
5180
5181 gst_caps_append_structure(format_caps, gst_structure_copy(template));
xuesong.jiange1a19662022-06-21 20:30:22 +08005182 GST_INFO_OBJECT(v4l2object->dbg_obj, "format_caps: %" GST_PTR_FORMAT, format_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005183
5184 if (!gst_caps_can_intersect(format_caps, filter))
5185 {
5186 gst_caps_unref(format_caps);
5187 gst_structure_free(template);
5188 continue;
5189 }
5190
5191 gst_caps_unref(format_caps);
5192 }
5193
5194 tmp = gst_aml_v4l2_object_probe_caps_for_format(v4l2object,
5195 format->pixelformat, template);
xuesong.jiange1a19662022-06-21 20:30:22 +08005196 GST_INFO_OBJECT(v4l2object->dbg_obj, "tmp caps: %" GST_PTR_FORMAT, tmp);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005197
5198 if (tmp)
5199 {
5200 tmp2 = gst_caps_copy(tmp);
5201 gst_caps_set_features_simple(tmp2, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
5202 gst_caps_append(ret, tmp);
5203 gst_caps_append(ret, tmp2);
5204 }
5205
5206 gst_structure_free(template);
5207 }
5208
5209 if (filter)
5210 {
5211 GstCaps *tmp;
5212
5213 tmp = ret;
5214 ret = gst_caps_intersect_full(filter, ret, GST_CAPS_INTERSECT_FIRST);
5215 gst_caps_unref(tmp);
5216 }
5217
xuesong.jiang22a9b112023-05-24 09:01:59 +00005218 if (v4l2object->stream_mode)
5219 {
5220 GST_INFO_OBJECT(v4l2object->dbg_obj, "ret caps: %" GST_PTR_FORMAT, ret);
5221 for (guint i = 0; i < gst_caps_get_size(ret); i++)
5222 {
5223 GstStructure *s = gst_caps_get_structure(ret, i);
5224 if (s)
5225 gst_structure_remove_field(s, "alignment");
5226
5227 GST_DEBUG("i:%d, s:%p", i, s);
5228 }
5229 GST_INFO_OBJECT(v4l2object->dbg_obj, "new ret caps: %" GST_PTR_FORMAT, ret);
5230 }
5231
xuesong.jiangae1548e2022-05-06 16:38:46 +08005232 GST_INFO_OBJECT(v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
5233
5234 return ret;
5235}
5236
5237GstCaps *
5238gst_aml_v4l2_object_get_caps(GstAmlV4l2Object *v4l2object, GstCaps *filter)
5239{
5240 GstCaps *ret;
5241
5242 if (v4l2object->probed_caps == NULL)
5243 v4l2object->probed_caps = gst_aml_v4l2_object_probe_caps(v4l2object, NULL);
5244
5245 if (filter)
5246 {
5247 ret = gst_caps_intersect_full(filter, v4l2object->probed_caps,
5248 GST_CAPS_INTERSECT_FIRST);
5249 }
5250 else
5251 {
5252 ret = gst_caps_ref(v4l2object->probed_caps);
5253 }
5254
5255 return ret;
5256}
5257
5258gboolean
5259gst_aml_v4l2_object_decide_allocation(GstAmlV4l2Object *obj, GstQuery *query)
5260{
5261 GstCaps *caps;
5262 GstBufferPool *pool = NULL, *other_pool = NULL;
5263 GstStructure *config;
5264 guint size, min, max, own_min = 0;
5265 gboolean update;
5266 gboolean has_video_meta;
5267 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
5268 GstAllocator *allocator = NULL;
5269 GstAllocationParams params = {0};
5270
5271 GST_DEBUG_OBJECT(obj->dbg_obj, "decide allocation");
5272
5273 g_return_val_if_fail(obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
5274 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
5275 FALSE);
5276
5277 gst_query_parse_allocation(query, &caps, NULL);
5278
5279 if (obj->pool == NULL)
5280 {
5281 if (!gst_aml_v4l2_object_setup_pool(obj, caps))
5282 goto pool_failed;
5283 }
5284
5285 if (gst_query_get_n_allocation_params(query) > 0)
5286 gst_query_parse_nth_allocation_param(query, 0, &allocator, &params);
5287
5288 if (gst_query_get_n_allocation_pools(query) > 0)
5289 {
5290 gst_query_parse_nth_allocation_pool(query, 0, &pool, &size, &min, &max);
5291 update = TRUE;
5292 }
5293 else
5294 {
5295 pool = NULL;
5296 min = max = 0;
5297 size = 0;
5298 update = FALSE;
5299 }
5300
5301 GST_DEBUG_OBJECT(obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%" GST_PTR_FORMAT, size, min, max, pool);
5302
5303 has_video_meta =
5304 gst_query_find_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
5305
5306 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
5307
5308 gst_aml_v4l2_get_driver_min_buffers(obj);
5309 /* We can't share our own pool, if it exceed V4L2 capacity */
5310 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
5311 can_share_own_pool = FALSE;
5312
5313 /* select a pool */
5314 switch (obj->mode)
5315 {
5316 case GST_V4L2_IO_RW:
5317 if (pool)
5318 {
5319 /* in READ/WRITE mode, prefer a downstream pool because our own pool
5320 * doesn't help much, we have to write to it as well */
5321 GST_DEBUG_OBJECT(obj->dbg_obj,
5322 "read/write mode: using downstream pool");
5323 /* use the bigest size, when we use our own pool we can't really do any
5324 * other size than what the hardware gives us but for downstream pools
5325 * we can try */
5326 size = MAX(size, obj->info.size);
5327 }
5328 else if (can_share_own_pool)
5329 {
5330 /* no downstream pool, use our own then */
5331 GST_DEBUG_OBJECT(obj->dbg_obj,
5332 "read/write mode: no downstream pool, using our own");
5333 pool = gst_object_ref(obj->pool);
5334 size = obj->info.size;
5335 pushing_from_our_pool = TRUE;
5336 }
5337 break;
5338
5339 case GST_V4L2_IO_USERPTR:
5340 case GST_V4L2_IO_DMABUF_IMPORT:
5341 /* in importing mode, prefer our own pool, and pass the other pool to
5342 * our own, so it can serve itself */
5343 if (pool == NULL)
5344 goto no_downstream_pool;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005345 gst_aml_v4l2_buffer_pool_set_other_pool(GST_AML_V4L2_BUFFER_POOL(obj->pool), pool);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005346 other_pool = pool;
5347 gst_object_unref(pool);
5348 pool = gst_object_ref(obj->pool);
5349 size = obj->info.size;
5350 break;
5351
5352 case GST_V4L2_IO_MMAP:
5353 case GST_V4L2_IO_DMABUF:
5354 /* in streaming mode, prefer our own pool */
5355 /* Check if we can use it ... */
5356 if (can_share_own_pool)
5357 {
5358 if (pool)
5359 gst_object_unref(pool);
5360 pool = gst_object_ref(obj->pool);
5361 size = obj->info.size;
5362 GST_DEBUG_OBJECT(obj->dbg_obj,
5363 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
5364 pushing_from_our_pool = TRUE;
5365 }
5366 else if (pool)
5367 {
5368 GST_DEBUG_OBJECT(obj->dbg_obj,
5369 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
5370 pool);
5371 }
5372 else
5373 {
5374 GST_DEBUG_OBJECT(obj->dbg_obj,
5375 "streaming mode: no usable pool, copying to generic pool");
5376 size = MAX(size, obj->info.size);
5377 }
5378 break;
5379 case GST_V4L2_IO_AUTO:
5380 default:
5381 GST_WARNING_OBJECT(obj->dbg_obj, "unhandled mode");
5382 break;
5383 }
5384
5385 if (size == 0)
5386 goto no_size;
5387
5388 /* If pushing from our own pool, configure it with queried minimum,
5389 * otherwise use the minimum required */
5390 if (pushing_from_our_pool)
5391 {
5392 /* When pushing from our own pool, we need what downstream one, to be able
5393 * to fill the pipeline, the minimum required to decoder according to the
5394 * driver and 2 more, so we don't endup up with everything downstream or
5395 * held by the decoder. We account 2 buffers for v4l2 so when one is being
5396 * pushed downstream the other one can already be queued for the next
5397 * frame. */
5398 own_min = min + obj->min_buffers + 2;
5399
5400 /* If no allocation parameters where provided, allow for a little more
5401 * buffers and enable copy threshold */
5402 if (!update)
5403 {
5404 own_min += 2;
5405 gst_aml_v4l2_buffer_pool_copy_at_threshold(GST_AML_V4L2_BUFFER_POOL(pool),
5406 TRUE);
5407 }
5408 else
5409 {
5410 gst_aml_v4l2_buffer_pool_copy_at_threshold(GST_AML_V4L2_BUFFER_POOL(pool),
5411 FALSE);
5412 }
5413 }
5414 else
5415 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005416 min = obj->min_buffers;
5417 max = min;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005418 }
5419
5420 /* Request a bigger max, if one was suggested but it's too small */
5421 if (max != 0)
5422 max = MAX(min, max);
5423
5424 /* First step, configure our own pool */
5425 config = gst_buffer_pool_get_config(obj->pool);
5426
5427 if (obj->need_video_meta || has_video_meta)
5428 {
5429 GST_DEBUG_OBJECT(obj->dbg_obj, "activate Video Meta");
5430 gst_buffer_pool_config_add_option(config,
5431 GST_BUFFER_POOL_OPTION_VIDEO_META);
5432 }
5433
5434 gst_buffer_pool_config_set_allocator(config, allocator, &params);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005435 gst_buffer_pool_config_set_params(config, caps, size, min, max);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005436
5437 GST_DEBUG_OBJECT(obj->dbg_obj, "setting own pool config to %" GST_PTR_FORMAT, config);
5438
5439 /* Our pool often need to adjust the value */
5440 if (!gst_buffer_pool_set_config(obj->pool, config))
5441 {
5442 config = gst_buffer_pool_get_config(obj->pool);
5443
5444 GST_DEBUG_OBJECT(obj->dbg_obj, "own pool config changed to %" GST_PTR_FORMAT, config);
5445
5446 /* our pool will adjust the maximum buffer, which we are fine with */
5447 if (!gst_buffer_pool_set_config(obj->pool, config))
5448 goto config_failed;
5449 }
5450
5451 /* Now configure the other pool if different */
5452 if (obj->pool != pool)
5453 other_pool = pool;
5454
5455 if (other_pool)
5456 {
5457 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)obj->element;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005458 guint other_min = min;
5459 guint other_max = max;
5460
5461 if (obj->old_other_pool || obj->old_old_other_pool) //jxsdbg for switching
5462 {
5463 obj->outstanding_buf_num = gst_aml_v4l2_object_get_outstanding_capture_buf_num(obj);
5464 other_min = min - obj->outstanding_buf_num;
5465 other_max = max - obj->outstanding_buf_num;
5466 GST_DEBUG_OBJECT(obj, "oop:%p, ooop:%p, outstanding buf num:%d, set min, max to %d,%d",
5467 obj->old_other_pool, obj->old_old_other_pool,
5468 obj->outstanding_buf_num, other_min, other_max);
5469 }
5470
xuesong.jiangae1548e2022-05-06 16:38:46 +08005471 if (self->is_secure_path)
5472 {
5473 params.flags |= GST_MEMORY_FLAG_LAST << 1; // in drmallocator GST_MEMORY_FLAG_LAST << 1 represent GST_MEMORY_FLAG_SECURE
5474 GST_DEBUG_OBJECT(obj, "set secure flag for drmbufferpool flag:0x%x", params.flags);
5475 }
5476 config = gst_buffer_pool_get_config(other_pool);
5477 gst_buffer_pool_config_set_allocator(config, allocator, &params);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005478 gst_buffer_pool_config_set_params (config, caps, size, other_min, other_max);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005479 gst_buffer_pool_config_set_video_alignment(config, &obj->align);
5480
5481 GST_DEBUG_OBJECT(obj->dbg_obj, "setting other pool config to %" GST_PTR_FORMAT, config);
5482
5483 /* if downstream supports video metadata, add this to the pool config */
5484 if (has_video_meta)
5485 {
5486 GST_DEBUG_OBJECT(obj->dbg_obj, "activate Video Meta");
5487 gst_buffer_pool_config_add_option(config,
5488 GST_BUFFER_POOL_OPTION_VIDEO_META);
5489 }
5490
5491 if (!gst_buffer_pool_set_config(other_pool, config))
5492 {
5493 config = gst_buffer_pool_get_config(other_pool);
5494
5495 if (!gst_buffer_pool_config_validate_params(config, caps, size, min,
5496 max))
5497 {
5498 gst_structure_free(config);
5499 goto config_failed;
5500 }
5501
5502 if (!gst_buffer_pool_set_config(other_pool, config))
5503 goto config_failed;
5504 }
5505 }
5506
5507 if (pool)
5508 {
5509 /* For simplicity, simply read back the active configuration, so our base
5510 * class get the right information */
5511 config = gst_buffer_pool_get_config(pool);
5512 gst_buffer_pool_config_get_params(config, NULL, &size, &min, &max);
5513 gst_structure_free(config);
5514 }
5515
5516 if (update)
5517 gst_query_set_nth_allocation_pool(query, 0, pool, size, min, max);
5518 else
5519 gst_query_add_allocation_pool(query, pool, size, min, max);
5520
5521 if (allocator)
5522 gst_object_unref(allocator);
5523
5524 if (pool)
5525 gst_object_unref(pool);
5526
5527 return TRUE;
5528
5529pool_failed:
5530{
5531 /* setup_pool already send the error */
5532 goto cleanup;
5533}
5534config_failed:
5535{
5536 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5537 (_("Failed to configure internal buffer pool.")), (NULL));
5538 goto cleanup;
5539}
5540no_size:
5541{
5542 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5543 (_("Video device did not suggest any buffer size.")), (NULL));
5544 goto cleanup;
5545}
5546cleanup:
5547{
5548 if (allocator)
5549 gst_object_unref(allocator);
5550
5551 if (pool)
5552 gst_object_unref(pool);
5553 return FALSE;
5554}
5555no_downstream_pool:
5556{
5557 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5558 (_("No downstream pool to import from.")),
5559 ("When importing DMABUF or USERPTR, we need a pool to import from"));
5560 return FALSE;
5561}
5562}
5563
5564gboolean
5565gst_aml_v4l2_object_propose_allocation(GstAmlV4l2Object *obj, GstQuery *query)
5566{
5567 GstBufferPool *pool;
5568 /* we need at least 2 buffers to operate */
5569 guint size, min, max;
5570 GstCaps *caps;
5571 gboolean need_pool;
5572
5573 /* Set defaults allocation parameters */
5574 size = obj->info.size;
5575 min = GST_AML_V4L2_MIN_BUFFERS;
5576 max = VIDEO_MAX_FRAME;
5577
5578 gst_query_parse_allocation(query, &caps, &need_pool);
5579
5580 if (caps == NULL)
5581 goto no_caps;
5582
5583 switch (obj->mode)
5584 {
5585 case GST_V4L2_IO_MMAP:
5586 case GST_V4L2_IO_DMABUF:
5587 if ((pool = obj->pool))
5588 gst_object_ref(pool);
5589 break;
5590 default:
5591 pool = NULL;
5592 break;
5593 }
5594
5595 if (pool != NULL)
5596 {
5597 GstCaps *pcaps;
5598 GstStructure *config;
5599
5600 /* we had a pool, check caps */
5601 config = gst_buffer_pool_get_config(pool);
5602 gst_buffer_pool_config_get_params(config, &pcaps, NULL, NULL, NULL);
5603
5604 GST_DEBUG_OBJECT(obj->dbg_obj,
5605 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
5606 if (!gst_caps_is_equal(caps, pcaps))
5607 {
5608 gst_structure_free(config);
5609 gst_object_unref(pool);
5610 goto different_caps;
5611 }
5612 gst_structure_free(config);
5613 }
5614 gst_aml_v4l2_get_driver_min_buffers(obj);
5615
5616 min = MAX(obj->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
5617
5618 gst_query_add_allocation_pool(query, pool, size, min, max);
5619
5620 /* we also support various metadata */
5621 gst_query_add_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
5622
5623 if (pool)
5624 gst_object_unref(pool);
5625
5626 return TRUE;
5627
5628 /* ERRORS */
5629no_caps:
5630{
5631 GST_DEBUG_OBJECT(obj->dbg_obj, "no caps specified");
5632 return FALSE;
5633}
5634different_caps:
5635{
5636 /* different caps, we can't use this pool */
5637 GST_DEBUG_OBJECT(obj->dbg_obj, "pool has different caps");
5638 return FALSE;
5639}
5640}
5641
5642gboolean
5643gst_aml_v4l2_object_try_import(GstAmlV4l2Object *obj, GstBuffer *buffer)
5644{
5645 GstVideoMeta *vmeta;
5646 guint n_mem = gst_buffer_n_memory(buffer);
5647
5648 /* only import if requested */
5649 switch (obj->mode)
5650 {
5651 case GST_V4L2_IO_USERPTR:
5652 case GST_V4L2_IO_DMABUF_IMPORT:
5653 break;
5654 default:
5655 GST_DEBUG_OBJECT(obj->dbg_obj,
5656 "The io-mode does not enable importation");
5657 return FALSE;
5658 }
5659
5660 vmeta = gst_buffer_get_video_meta(buffer);
5661 if (!vmeta && obj->need_video_meta)
5662 {
5663 GST_DEBUG_OBJECT(obj->dbg_obj, "Downstream buffer uses standard "
5664 "stride/offset while the driver does not.");
5665 return FALSE;
5666 }
5667
5668 /* we need matching strides/offsets and size */
5669 if (vmeta)
5670 {
5671 guint p;
5672 gboolean need_fmt_update = FALSE;
5673
5674 if (vmeta->n_planes != GST_VIDEO_INFO_N_PLANES(&obj->info))
5675 {
5676 GST_WARNING_OBJECT(obj->dbg_obj,
5677 "Cannot import buffers with different number planes");
5678 return FALSE;
5679 }
5680
5681 for (p = 0; p < vmeta->n_planes; p++)
5682 {
5683 if (vmeta->stride[p] < obj->info.stride[p])
5684 {
5685 GST_DEBUG_OBJECT(obj->dbg_obj,
5686 "Not importing as remote stride %i is smaller then %i on plane %u",
5687 vmeta->stride[p], obj->info.stride[p], p);
5688 return FALSE;
5689 }
5690 else if (vmeta->stride[p] > obj->info.stride[p])
5691 {
5692 need_fmt_update = TRUE;
5693 }
5694
5695 if (vmeta->offset[p] < obj->info.offset[p])
5696 {
5697 GST_DEBUG_OBJECT(obj->dbg_obj,
5698 "Not importing as offset %" G_GSIZE_FORMAT
5699 " is smaller then %" G_GSIZE_FORMAT " on plane %u",
5700 vmeta->offset[p], obj->info.offset[p], p);
5701 return FALSE;
5702 }
5703 else if (vmeta->offset[p] > obj->info.offset[p])
5704 {
5705 need_fmt_update = TRUE;
5706 }
5707 }
5708
5709 if (need_fmt_update)
5710 {
5711 struct v4l2_format format;
5712 gint wanted_stride[GST_VIDEO_MAX_PLANES] = {
5713 0,
5714 };
5715
5716 format = obj->format;
5717
5718 /* update the current format with the stride we want to import from */
5719 if (V4L2_TYPE_IS_MULTIPLANAR(obj->type))
5720 {
5721 guint i;
5722
5723 GST_DEBUG_OBJECT(obj->dbg_obj, "Wanted strides:");
5724
5725 for (i = 0; i < obj->n_v4l2_planes; i++)
5726 {
5727 gint stride = vmeta->stride[i];
5728
5729 if (GST_VIDEO_FORMAT_INFO_IS_TILED(obj->info.finfo))
5730 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(obj->info.finfo);
5731
5732 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
5733 wanted_stride[i] = stride;
5734 GST_DEBUG_OBJECT(obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
5735 }
5736 }
5737 else
5738 {
5739 gint stride = vmeta->stride[0];
5740
5741 GST_DEBUG_OBJECT(obj->dbg_obj, "Wanted stride: %i", stride);
5742
5743 if (GST_VIDEO_FORMAT_INFO_IS_TILED(obj->info.finfo))
5744 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(obj->info.finfo);
5745
5746 format.fmt.pix.bytesperline = stride;
5747 wanted_stride[0] = stride;
5748 }
5749
5750 if (obj->ioctl(obj->video_fd, VIDIOC_S_FMT, &format) < 0)
5751 {
5752 GST_WARNING_OBJECT(obj->dbg_obj,
5753 "Something went wrong trying to update current format: %s",
5754 g_strerror(errno));
5755 return FALSE;
5756 }
5757
5758 gst_aml_v4l2_object_save_format(obj, obj->fmtdesc, &format, &obj->info,
5759 &obj->align);
5760
5761 if (V4L2_TYPE_IS_MULTIPLANAR(obj->type))
5762 {
5763 guint i;
5764
5765 for (i = 0; i < obj->n_v4l2_planes; i++)
5766 {
5767 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i])
5768 {
5769 GST_DEBUG_OBJECT(obj->dbg_obj,
5770 "[%i] Driver did not accept the new stride (wants %i, got %i)",
5771 i, format.fmt.pix_mp.plane_fmt[i].bytesperline,
5772 wanted_stride[i]);
5773 return FALSE;
5774 }
5775 }
5776 }
5777 else
5778 {
5779 if (format.fmt.pix.bytesperline != wanted_stride[0])
5780 {
5781 GST_DEBUG_OBJECT(obj->dbg_obj,
5782 "Driver did not accept the new stride (wants %i, got %i)",
5783 format.fmt.pix.bytesperline, wanted_stride[0]);
5784 return FALSE;
5785 }
5786 }
5787 }
5788 }
5789
5790 /* we can always import single memory buffer, but otherwise we need the same
5791 * amount of memory object. */
5792 if (n_mem != 1 && n_mem != obj->n_v4l2_planes)
5793 {
5794 GST_DEBUG_OBJECT(obj->dbg_obj, "Can only import %i memory, "
5795 "buffers contains %u memory",
5796 obj->n_v4l2_planes, n_mem);
5797 return FALSE;
5798 }
5799
5800 /* For DMABuf importation we need DMABuf of course */
5801 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT)
5802 {
5803 guint i;
5804
5805 for (i = 0; i < n_mem; i++)
5806 {
5807 GstMemory *mem = gst_buffer_peek_memory(buffer, i);
5808
5809 if (!gst_is_dmabuf_memory(mem))
5810 {
5811 GST_DEBUG_OBJECT(obj->dbg_obj, "Cannot import non-DMABuf memory.");
5812 return FALSE;
5813 }
5814 }
5815 }
5816
5817 /* for the remaining, only the kernel driver can tell */
5818 return TRUE;
5819}
5820
xuesong.jiang22a9b112023-05-24 09:01:59 +00005821static gboolean gst_aml_v4l2_set_control(GstAmlV4l2Object *v4l2object, guint ctl)
5822{
5823 int rc;
5824 struct v4l2_queryctrl queryctrl;
5825 struct v4l2_control control;
5826
5827 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)v4l2object->element;
5828 self->is_secure_path = TRUE;
5829
5830 memset(&queryctrl, 0, sizeof(queryctrl));
5831 queryctrl.id = ctl;
5832
5833 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_QUERYCTRL, &queryctrl);
5834 if (rc == 0)
5835 {
5836 if (!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED))
5837 {
5838 memset(&control, 0, sizeof(control));
5839 control.id = ctl;
5840 control.value = 1;
5841 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_CTRL, &control);
5842 if (rc != 0)
5843 {
5844 GST_ERROR_OBJECT(v4l2object->dbg_obj, "set ctl:0x%x fail rc %d", ctl, rc);
5845 return FALSE;
5846 }
5847 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set ctl:0x%x succ", ctl);
5848 return TRUE;
5849 }
5850 else
5851 {
5852 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "ctl:0x%x is disabled", ctl);
5853 return TRUE;
5854 }
5855 }
5856 else
5857 {
5858 GST_ERROR_OBJECT(v4l2object->dbg_obj, "VIDIOC_QUERYCTRL for 0x:%x fail", ctl);
5859 return FALSE;
5860 }
5861}
5862
5863
xuesong.jiangae1548e2022-05-06 16:38:46 +08005864gboolean gst_aml_v4l2_set_drm_mode(GstAmlV4l2Object *v4l2object)
5865{
5866 /* On AmLogic, output obj use of GST_V4L2_IO_DMABUF_IMPORT implies secure memory */
5867 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
5868 {
xuesong.jiangae1548e2022-05-06 16:38:46 +08005869
xuesong.jiang22a9b112023-05-24 09:01:59 +00005870 if (gst_aml_v4l2_set_control(v4l2object, AML_V4L2_SET_DRMMODE))
xuesong.jiangae1548e2022-05-06 16:38:46 +08005871 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00005872 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_DRMMODE set succ");
5873 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005874 }
5875 else
5876 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00005877 GST_ERROR_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_DRMMODE set fail");
xuesong.jiangae1548e2022-05-06 16:38:46 +08005878 return FALSE;
5879 }
5880 }
5881 else
5882 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00005883 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "req mode is not GST_V4L2_IO_DMABUF_IMPORT, DRM mode does not need to be configured");
5884 return TRUE;
5885 }
5886}
5887
5888gboolean gst_aml_v4l2_set_stream_mode(GstAmlV4l2Object *v4l2object)
5889{
5890 if (v4l2object->stream_mode)
5891 {
5892 if (gst_aml_v4l2_set_control(v4l2object, AML_V4L2_SET_STREAM_MODE))
5893 {
5894 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_STREAM_MODE set succ");
5895 return TRUE;
5896 }
5897 else
5898 {
5899 GST_ERROR_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_STREAM_MODE set fail");
5900 return FALSE;
5901 }
5902 }
5903 else
5904 {
5905 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 +08005906 return TRUE;
5907 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005908}
5909
5910gint gst_aml_v4l2_object_get_outstanding_capture_buf_num(GstAmlV4l2Object *obj)
5911{
5912 gint ret = 0;
5913 gint count = 0;
5914
5915 if (obj->old_other_pool)
5916 {
5917 count = gst_buffer_pool_get_outstanding_num(obj->old_other_pool);
5918 if (count)
5919 {
5920 ret += count;
5921 }
5922 else
5923 {
5924 gst_object_unref(obj->old_other_pool);
5925 obj->old_other_pool = NULL;
5926 }
5927 }
5928
5929 count = 0;
5930 if (obj->old_old_other_pool)
5931 {
5932 count = gst_buffer_pool_get_outstanding_num(obj->old_old_other_pool);
5933 if (count)
5934 {
5935 ret += count;
5936 }
5937 else
5938 {
5939 gst_object_unref(obj->old_old_other_pool);
5940 obj->old_old_other_pool = NULL;
5941 }
5942 }
5943
5944 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005945}