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