blob: 44aaae550d1ee0f4c77ce9e55629737f6c6fca6b [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
fei.deng17693b62024-08-28 20:13:17 +08003745 if (v4l2object->low_memory_mode) {
3746 decParm->cfg.ref_buf_margin = GST_AML_V4L2_LOW_MEMORY_CAP_BUF_MARGIN;
3747 } else {
3748 decParm->cfg.ref_buf_margin = GST_AML_V4L2_DEFAULT_CAP_BUF_MARGIN;
3749 }
3750
le.hancd3f2842024-06-26 09:37:50 +00003751 env = getenv("V4L2_SET_AMLOGIC_MARGIN_NUM");
3752 if (env)
3753 {
3754 decParm->cfg.ref_buf_margin = atoi(env);
bo.xiao7659cda2024-07-18 16:16:50 +08003755 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "output cfg margin to %d", decParm->cfg.ref_buf_margin);
le.hancd3f2842024-06-26 09:37:50 +00003756 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08003757
bo.xiaof42b0082024-08-22 14:08:53 +08003758 // d v
3759 gboolean bl_present_flag, el_present_flag;
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003760 int dvBaseLayerPresent = -1;
3761 int dvEnhancementLayerPresent = -1;
3762 /* have base and enhancement layers both, that means its dual layer,
bo.xiaof42b0082024-08-22 14:08:53 +08003763 d v two layer flag will be true */
3764 if (gst_structure_get_boolean( structure, "bl_present_flag", &bl_present_flag))
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003765 {
bo.xiaof42b0082024-08-22 14:08:53 +08003766 dvBaseLayerPresent= bl_present_flag?1:0;
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003767 }
bo.xiaof42b0082024-08-22 14:08:53 +08003768 if (gst_structure_get_boolean( structure, "el_present_flag", &el_present_flag))
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003769 {
bo.xiaof42b0082024-08-22 14:08:53 +08003770 dvEnhancementLayerPresent= el_present_flag?1:0;
hanghang.luo6a5bdff2024-04-15 06:29:43 +00003771 }
3772
3773 /* have base and enhancement layers both, that means its dual layer, dv two layer flag will be true */
3774 if ( (dvBaseLayerPresent == 1) && (dvEnhancementLayerPresent == 1) )
3775 {
3776 decParm->cfg.metadata_config_flag |= (1 << 0);
3777 }
3778 else
3779 {
3780 decParm->cfg.metadata_config_flag |= (0 << 0);
3781 }
3782
3783 /* have one of then, it's standard dv stream, Non-standard dv flag will be false */
3784 if ( (dvBaseLayerPresent == 0) && (dvEnhancementLayerPresent == 0) )
3785 {
3786 decParm->cfg.metadata_config_flag |= (1 << 1);
3787 }
3788 else
3789 {
3790 decParm->cfg.metadata_config_flag |= (0 << 1);
3791 }
3792
3793 // HDR
xuesong.jiange1a19662022-06-21 20:30:22 +08003794 if ( gst_structure_has_field(structure, "colorimetry") )
3795 {
3796 const char *colorimetry= gst_structure_get_string(structure,"colorimetry");
3797 GstVideoColorimetry vci = {0};
3798 if ( colorimetry && gst_video_colorimetry_from_string( &vci, colorimetry ))
3799 {
3800 decParm->parms_status |= V4L2_CONFIG_PARM_DECODE_HDRINFO;
3801 decParm->hdr.signal_type= (1<<29); /* present flag */
3802 /*set default value, this is to keep up with driver hdr info synchronization*/
3803 decParm->hdr.signal_type |= (5<<26) | (1<<24);
3804
3805 gint hdrColorimetry[4] = {0};
3806 hdrColorimetry[0]= (int)vci.range;
3807 hdrColorimetry[1]= (int)vci.matrix;
3808 hdrColorimetry[2]= (int)vci.transfer;
3809 hdrColorimetry[3]= (int)vci.primaries;
3810 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "colorimetry: [%d,%d,%d,%d]",
3811 hdrColorimetry[0],
3812 hdrColorimetry[1],
3813 hdrColorimetry[2],
3814 hdrColorimetry[3] );
3815 /* range */
3816 switch ( hdrColorimetry[0] )
3817 {
le.han8d28eb82024-06-05 08:11:12 +00003818 case GST_VIDEO_COLOR_RANGE_0_255:
3819 case GST_VIDEO_COLOR_RANGE_16_235:
xuesong.jiange1a19662022-06-21 20:30:22 +08003820 decParm->hdr.signal_type |= ((hdrColorimetry[0] % 2)<<25);
3821 break;
3822 default:
3823 break;
3824 }
3825 /* matrix coefficient */
3826 switch ( hdrColorimetry[1] )
3827 {
le.han8d28eb82024-06-05 08:11:12 +00003828 case GST_VIDEO_COLOR_MATRIX_RGB: /* RGB */
xuesong.jiange1a19662022-06-21 20:30:22 +08003829 decParm->hdr.signal_type |= 0;
3830 break;
le.han8d28eb82024-06-05 08:11:12 +00003831 case GST_VIDEO_COLOR_MATRIX_FCC: /* FCC */
xuesong.jiange1a19662022-06-21 20:30:22 +08003832 decParm->hdr.signal_type |= 4;
3833 break;
le.han8d28eb82024-06-05 08:11:12 +00003834 case GST_VIDEO_COLOR_MATRIX_BT709: /* BT709 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003835 decParm->hdr.signal_type |= 1;
3836 break;
le.han8d28eb82024-06-05 08:11:12 +00003837 case GST_VIDEO_COLOR_MATRIX_BT601: /* BT601 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003838 decParm->hdr.signal_type |= 3;
3839 break;
le.han8d28eb82024-06-05 08:11:12 +00003840 case GST_VIDEO_COLOR_MATRIX_SMPTE240M: /* SMPTE240M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003841 decParm->hdr.signal_type |= 7;
3842 break;
le.han8d28eb82024-06-05 08:11:12 +00003843 case GST_VIDEO_COLOR_MATRIX_BT2020: /* BT2020 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003844 decParm->hdr.signal_type |= 9;
3845 break;
3846 default: /* unknown */
3847 decParm->hdr.signal_type |= 2;
3848 break;
3849 }
3850 /* transfer function */
3851 switch ( hdrColorimetry[2] )
3852 {
le.han8d28eb82024-06-05 08:11:12 +00003853 case GST_VIDEO_TRANSFER_BT709: /* BT709 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003854 decParm->hdr.signal_type |= (1<<8);
3855 break;
le.han8d28eb82024-06-05 08:11:12 +00003856 case GST_VIDEO_TRANSFER_SMPTE240M: /* SMPTE240M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003857 decParm->hdr.signal_type |= (7<<8);
3858 break;
le.han8d28eb82024-06-05 08:11:12 +00003859 case GST_VIDEO_TRANSFER_LOG100: /* LOG100 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003860 decParm->hdr.signal_type |= (9<<8);
3861 break;
le.han8d28eb82024-06-05 08:11:12 +00003862 case GST_VIDEO_TRANSFER_LOG316: /* LOG316 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003863 decParm->hdr.signal_type |= (10<<8);
3864 break;
le.han8d28eb82024-06-05 08:11:12 +00003865 case GST_VIDEO_TRANSFER_BT2020_12: /* BT2020_12 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003866 decParm->hdr.signal_type |= (15<<8);
3867 break;
le.han8d28eb82024-06-05 08:11:12 +00003868 case GST_VIDEO_TRANSFER_BT2020_10: /* BT2020_10 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003869 decParm->hdr.signal_type |= (14<<8);
3870 break;
le.han8d28eb82024-06-05 08:11:12 +00003871 #if ((GST_VERSION_MAJOR == 1) && (GST_VERSION_MINOR >= 18))
3872 case GST_VIDEO_TRANSFER_SMPTE2084: /* SMPTE2084 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003873 decParm->hdr.signal_type |= (16<<8);
3874 break;
le.han8d28eb82024-06-05 08:11:12 +00003875 #else
3876 case GST_VIDEO_TRANSFER_SMPTE_ST_2084: /* SMPTE2084 */
3877 decParm->hdr.signal_type |= (16<<8);
3878 break;
3879 #endif
3880 case GST_VIDEO_TRANSFER_ARIB_STD_B67: /* ARIB_STD_B67 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003881 decParm->hdr.signal_type |= (18<<8);
3882 break;
3883 #if ((GST_VERSION_MAJOR == 1) && (GST_VERSION_MINOR >= 18))
le.han8d28eb82024-06-05 08:11:12 +00003884 case GST_VIDEO_TRANSFER_BT601: /* BT601 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003885 decParm->hdr.signal_type |= (3<<8);
3886 break;
3887 #endif
le.han8d28eb82024-06-05 08:11:12 +00003888 case GST_VIDEO_TRANSFER_GAMMA10: /* GAMMA10 */
3889 case GST_VIDEO_TRANSFER_GAMMA18: /* GAMMA18 */
3890 case GST_VIDEO_TRANSFER_GAMMA20: /* GAMMA20 */
3891 case GST_VIDEO_TRANSFER_GAMMA22: /* GAMMA22 */
3892 case GST_VIDEO_TRANSFER_SRGB: /* SRGB */
3893 case GST_VIDEO_TRANSFER_GAMMA28: /* GAMMA28 */
3894 case GST_VIDEO_TRANSFER_ADOBERGB: /* ADOBERGB */
xuesong.jiange1a19662022-06-21 20:30:22 +08003895 default:
3896 break;
3897 }
3898 /* primaries */
3899 switch ( hdrColorimetry[3] )
3900 {
le.han8d28eb82024-06-05 08:11:12 +00003901 case GST_VIDEO_COLOR_PRIMARIES_BT709: /* BT709 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003902 decParm->hdr.signal_type |= ((1<<24)|(1<<16));
3903 break;
le.han8d28eb82024-06-05 08:11:12 +00003904 case GST_VIDEO_COLOR_PRIMARIES_BT470M: /* BT470M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003905 decParm->hdr.signal_type |= ((1<<24)|(4<<16));
3906 break;
le.han8d28eb82024-06-05 08:11:12 +00003907 case GST_VIDEO_COLOR_PRIMARIES_BT470BG: /* BT470BG */
xuesong.jiange1a19662022-06-21 20:30:22 +08003908 decParm->hdr.signal_type |= ((1<<24)|(5<<16));
3909 break;
le.han8d28eb82024-06-05 08:11:12 +00003910 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M: /* SMPTE170M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003911 decParm->hdr.signal_type |= ((1<<24)|(6<<16));
3912 break;
le.han8d28eb82024-06-05 08:11:12 +00003913 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M: /* SMPTE240M */
xuesong.jiange1a19662022-06-21 20:30:22 +08003914 decParm->hdr.signal_type |= ((1<<24)|(7<<16));
3915 break;
le.han8d28eb82024-06-05 08:11:12 +00003916 case GST_VIDEO_COLOR_PRIMARIES_FILM: /* FILM */
xuesong.jiange1a19662022-06-21 20:30:22 +08003917 decParm->hdr.signal_type |= ((1<<24)|(8<<16));
3918 break;
le.han8d28eb82024-06-05 08:11:12 +00003919 case GST_VIDEO_COLOR_PRIMARIES_BT2020: /* BT2020 */
xuesong.jiange1a19662022-06-21 20:30:22 +08003920 decParm->hdr.signal_type |= ((1<<24)|(9<<16));
3921 break;
le.han8d28eb82024-06-05 08:11:12 +00003922 case GST_VIDEO_COLOR_PRIMARIES_ADOBERGB: /* ADOBERGB */
xuesong.jiange1a19662022-06-21 20:30:22 +08003923 default:
3924 break;
3925 }
3926 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR signal_type %X", decParm->hdr.signal_type);
3927 }
3928
bo.xiao7659cda2024-07-18 16:16:50 +08003929 //why remove this colorimetry?
xuesong.jiange1a19662022-06-21 20:30:22 +08003930 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "got caps %" GST_PTR_FORMAT, caps);
3931 GstStructure *st = gst_caps_get_structure(caps, 0);
3932 GstCapsFeatures *features = gst_caps_get_features(caps, 0);
3933
3934 if (gst_structure_has_field(st, "colorimetry"))
3935 {
3936 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "have colorimetry");
3937 }
3938
3939 if (st && features)
3940 {
3941 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "trace in remove colorimetry");
3942 gst_structure_remove_field(st, "colorimetry");
3943 gst_caps_features_remove(features, "colorimetry");
3944 }
3945 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "caps after remove colorimetry %" GST_PTR_FORMAT, caps);
3946 }
3947
le.han7d0af792024-06-05 08:44:18 +00003948 const char * field = NULL;
3949 if (gst_structure_has_field(structure, "mastering-display-metadata")) {
3950 field = "mastering-display-metadata";
3951 } else if (gst_structure_has_field(structure, "mastering-display-info")) {
3952 field = "mastering-display-info";
3953 }
3954
3955 if ( field )
xuesong.jiange1a19662022-06-21 20:30:22 +08003956 {
le.han7d0af792024-06-05 08:44:18 +00003957 const char *masteringDisplay= gst_structure_get_string(structure, field);
xuesong.jiange1a19662022-06-21 20:30:22 +08003958 float hdrMasteringDisplay[10];
3959 if ( masteringDisplay && sscanf( masteringDisplay, "%f:%f:%f:%f:%f:%f:%f:%f:%f:%f",
3960 &hdrMasteringDisplay[0],
3961 &hdrMasteringDisplay[1],
3962 &hdrMasteringDisplay[2],
3963 &hdrMasteringDisplay[3],
3964 &hdrMasteringDisplay[4],
3965 &hdrMasteringDisplay[5],
3966 &hdrMasteringDisplay[6],
3967 &hdrMasteringDisplay[7],
3968 &hdrMasteringDisplay[8],
3969 &hdrMasteringDisplay[9] ) == 10 )
3970 {
3971 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "mastering display [%f,%f,%f,%f,%f,%f,%f,%f,%f,%f]",
3972 hdrMasteringDisplay[0],
3973 hdrMasteringDisplay[1],
3974 hdrMasteringDisplay[2],
3975 hdrMasteringDisplay[3],
3976 hdrMasteringDisplay[4],
3977 hdrMasteringDisplay[5],
3978 hdrMasteringDisplay[6],
3979 hdrMasteringDisplay[7],
3980 hdrMasteringDisplay[8],
3981 hdrMasteringDisplay[9] );
3982
3983 decParm->hdr.color_parms.present_flag= 1;
3984 decParm->hdr.color_parms.primaries[2][0]= (uint32_t)(hdrMasteringDisplay[0]*50000); /* R.x */
3985 decParm->hdr.color_parms.primaries[2][1]= (uint32_t)(hdrMasteringDisplay[1]*50000); /* R.y */
3986 decParm->hdr.color_parms.primaries[0][0]= (uint32_t)(hdrMasteringDisplay[2]*50000); /* G.x */
3987 decParm->hdr.color_parms.primaries[0][1]= (uint32_t)(hdrMasteringDisplay[3]*50000); /* G.y */
3988 decParm->hdr.color_parms.primaries[1][0]= (uint32_t)(hdrMasteringDisplay[4]*50000); /* B.x */
3989 decParm->hdr.color_parms.primaries[1][1]= (uint32_t)(hdrMasteringDisplay[5]*50000); /* B.y */
3990 decParm->hdr.color_parms.white_point[0]= (uint32_t)(hdrMasteringDisplay[6]*50000);
3991 decParm->hdr.color_parms.white_point[1]= (uint32_t)(hdrMasteringDisplay[7]*50000);
3992 decParm->hdr.color_parms.luminance[0]= (uint32_t)(hdrMasteringDisplay[8]);
3993 decParm->hdr.color_parms.luminance[1]= (uint32_t)(hdrMasteringDisplay[9]);
3994 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: primaries %X %X %X %X %X %X",
3995 decParm->hdr.color_parms.primaries[2][0],
3996 decParm->hdr.color_parms.primaries[2][1],
3997 decParm->hdr.color_parms.primaries[0][0],
3998 decParm->hdr.color_parms.primaries[0][1],
3999 decParm->hdr.color_parms.primaries[1][0],
4000 decParm->hdr.color_parms.primaries[1][1] );
4001 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: white point: %X %X",
4002 decParm->hdr.color_parms.white_point[0],
4003 decParm->hdr.color_parms.white_point[1] );
4004 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "HDR mastering: luminance: %X %X",
4005 decParm->hdr.color_parms.luminance[0],
4006 decParm->hdr.color_parms.luminance[1] );
4007 }
4008
4009 GstStructure *st = gst_caps_get_structure(caps, 0);
4010 GstCapsFeatures * features = gst_caps_get_features(caps, 0);
4011 if (st && features)
4012 {
le.han7d0af792024-06-05 08:44:18 +00004013 gst_structure_remove_fields(st, field, NULL);
4014 gst_caps_features_remove(features, field);
xuesong.jiange1a19662022-06-21 20:30:22 +08004015 }
le.han7d0af792024-06-05 08:44:18 +00004016 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "caps after remove %s %" GST_PTR_FORMAT, field, caps);
xuesong.jiange1a19662022-06-21 20:30:22 +08004017 }
hanghang.luo6a5bdff2024-04-15 06:29:43 +00004018 }
hanghang.luo75664712024-07-01 19:28:10 +08004019 else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
hanghang.luo6a5bdff2024-04-15 06:29:43 +00004020 {
hanghang.luo75664712024-07-01 19:28:10 +08004021 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec*) v4l2object->element;
bo.xiao7659cda2024-07-18 16:16:50 +08004022
4023 if (!get_amlogic_vdec_parm(v4l2object, streamparm))
hanghang.luobfc63f82024-07-05 11:04:56 +08004024 {
4025 GST_ERROR_OBJECT(v4l2object->dbg_obj, "can't get vdec parm");
4026 return;
4027 }
hanghang.luo75664712024-07-01 19:28:10 +08004028
bo.xiao7659cda2024-07-18 16:16:50 +08004029 decParm->cfg.double_write_mode = gst_aml_config_dw(v4l2object, self->v4l2output->fmtdesc->pixelformat,
bo.xiao34e36202024-07-17 16:04:01 +08004030 width, height, is_interlaced);
bo.xiao7659cda2024-07-18 16:16:50 +08004031 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "capture cfg dw mode to %d", decParm->cfg.double_write_mode);
hanghang.luo75664712024-07-01 19:28:10 +08004032
4033 if (needSpecConfigForFg(v4l2object))
4034 {
bo.xiao7659cda2024-07-18 16:16:50 +08004035 decParm->cfg.double_write_mode = VDEC_DW_MMU_1;
4036 GST_DEBUG_OBJECT(v4l2object->dbg_obj,"set fg dw mode %d", decParm->cfg.double_write_mode);
hanghang.luo75664712024-07-01 19:28:10 +08004037 }
bo.xiao7659cda2024-07-18 16:16:50 +08004038 v4l2object->dw_mode = decParm->cfg.double_write_mode;
hanghang.luo6a5bdff2024-04-15 06:29:43 +00004039 }
4040 else
4041 {
hanghang.luo75664712024-07-01 19:28:10 +08004042 GST_ERROR_OBJECT(v4l2object->dbg_obj,"can't deal with. please check buffer type.");
bo.xiao7659cda2024-07-18 16:16:50 +08004043 return;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004044 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004045
bo.xiao7659cda2024-07-18 16:16:50 +08004046 if (use_ext_config)
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004047 {
bo.xiao7659cda2024-07-18 16:16:50 +08004048 memset(&ctrls, 0, sizeof(ctrls));
4049 memset(&control, 0, sizeof(control));
4050 control.id = AML_V4L2_DEC_PARMS_CONFIG;
4051 control.ptr = decParm;
4052 control.size = sizeof(struct aml_dec_params);
4053 ctrls.count = 1;
4054 ctrls.controls = &control;
4055 if (v4l2object->ioctl( v4l2object->video_fd, VIDIOC_S_EXT_CTRLS, &ctrls ) < 0)
4056 {
4057 GST_ERROR_OBJECT(v4l2object->dbg_obj, "set vdec parm fail");
4058 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004059 }
bo.xiao7659cda2024-07-18 16:16:50 +08004060 else
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004061 {
bo.xiao7659cda2024-07-18 16:16:50 +08004062 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_PARM, streamparm) < 0)
4063 {
4064 GST_ERROR_OBJECT(v4l2object->dbg_obj, "set vdec parm fail");
4065 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004066 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004067}
4068
xuesong.jiangae1548e2022-05-06 16:38:46 +08004069static gboolean
4070gst_aml_v4l2_object_set_format_full(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4071 gboolean try_only, GstAmlV4l2Error *error)
4072{
4073 gint fd = v4l2object->video_fd;
4074 struct v4l2_format format;
4075 struct v4l2_streamparm streamparm;
4076 enum v4l2_field field;
4077 guint32 pixelformat;
4078 struct v4l2_fmtdesc *fmtdesc;
4079 GstVideoInfo info;
4080 GstVideoAlignment align;
4081 gint width, height, fps_n, fps_d;
4082 gint n_v4l_planes;
4083 gint i = 0;
4084 gboolean is_mplane;
4085 enum v4l2_colorspace colorspace = 0;
4086 enum v4l2_quantization range = 0;
4087 enum v4l2_ycbcr_encoding matrix = 0;
4088 enum v4l2_xfer_func transfer = 0;
4089 GstStructure *s;
4090 gboolean disable_colorimetry = FALSE;
4091
4092 g_return_val_if_fail(!v4l2object->skip_try_fmt_probes ||
4093 gst_caps_is_writable(caps),
4094 FALSE);
4095
4096 GST_AML_V4L2_CHECK_OPEN(v4l2object);
4097 if (!try_only)
4098 GST_AML_V4L2_CHECK_NOT_ACTIVE(v4l2object);
4099
xuesong.jiangae1548e2022-05-06 16:38:46 +08004100 is_mplane = V4L2_TYPE_IS_MULTIPLANAR(v4l2object->type);
4101
4102 gst_video_info_init(&info);
4103 gst_video_alignment_reset(&align);
4104
4105 if (!gst_aml_v4l2_object_get_caps_info(v4l2object, caps, &fmtdesc, &info))
4106 goto invalid_caps;
4107
4108 pixelformat = fmtdesc->pixelformat;
4109 width = GST_VIDEO_INFO_WIDTH(&info);
4110 height = GST_VIDEO_INFO_HEIGHT(&info);
4111 fps_n = GST_VIDEO_INFO_FPS_N(&info);
4112 fps_d = GST_VIDEO_INFO_FPS_D(&info);
4113
hanghang.luo9edfc7d2023-05-17 07:01:05 +00004114 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Check image size");
4115 struct v4l2_frmsizeenum size;
4116 memset (&size, 0, sizeof (struct v4l2_frmsizeenum));
4117 size.index = 0;
4118 size.pixel_format = pixelformat;
4119 if (v4l2object->ioctl (fd, VIDIOC_ENUM_FRAMESIZES, &size) < 0)
4120 return FALSE;
4121 if (size.type == V4L2_FRMSIZE_TYPE_STEPWISE)
4122 {
4123 guint32 maxw, maxh;
4124 maxw = MIN (size.stepwise.max_width, G_MAXINT);
4125 maxh = MIN (size.stepwise.max_height, G_MAXINT);
4126 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "image from caps w_h:%d_%d", width, height);
4127 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "v4l2 support max w_h:%d_%d", maxw, maxh);
4128 if (width*height > maxw*maxh)
4129 return FALSE;
4130 GST_DEBUG_OBJECT (v4l2object->dbg_obj, "Check image size ok");
4131 }
4132
fei.deng7c3d67f2022-11-09 11:06:05 +08004133 //set amlogic params here,because we need pix format to set dw mode
4134 memset(&streamparm, 0x00, sizeof(struct v4l2_streamparm));
fei.deng7c3d67f2022-11-09 11:06:05 +08004135 set_amlogic_vdec_parm(v4l2object, &streamparm, caps, pixelformat);
4136
xuesong.jiangae1548e2022-05-06 16:38:46 +08004137 /* if encoded format (GST_VIDEO_INFO_N_PLANES return 0)
4138 * or if contiguous is prefered */
4139 n_v4l_planes = GST_VIDEO_INFO_N_PLANES(&info);
4140 if (!n_v4l_planes || !v4l2object->prefered_non_contiguous)
4141 n_v4l_planes = 1;
4142
4143 if (GST_VIDEO_INFO_IS_INTERLACED(&info))
4144 {
4145 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "interlaced video");
4146 /* ideally we would differentiate between types of interlaced video
4147 * but there is not sufficient information in the caps..
4148 */
4149 field = V4L2_FIELD_INTERLACED;
4150 }
4151 else
4152 {
4153 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "progressive video");
4154 field = V4L2_FIELD_NONE;
4155 }
4156
4157 /* We first pick the main colorspace from the primaries */
4158 switch (info.colorimetry.primaries)
4159 {
4160 case GST_VIDEO_COLOR_PRIMARIES_BT709:
4161 /* There is two colorspaces using these primaries, use the range to
4162 * differentiate */
4163 if (info.colorimetry.range == GST_VIDEO_COLOR_RANGE_16_235)
4164 colorspace = V4L2_COLORSPACE_REC709;
4165 else
4166 colorspace = V4L2_COLORSPACE_SRGB;
4167 break;
4168 case GST_VIDEO_COLOR_PRIMARIES_BT2020:
4169 colorspace = V4L2_COLORSPACE_BT2020;
4170 break;
4171 case GST_VIDEO_COLOR_PRIMARIES_BT470M:
4172 colorspace = V4L2_COLORSPACE_470_SYSTEM_M;
4173 break;
4174 case GST_VIDEO_COLOR_PRIMARIES_BT470BG:
4175 colorspace = V4L2_COLORSPACE_470_SYSTEM_BG;
4176 break;
4177 case GST_VIDEO_COLOR_PRIMARIES_SMPTE170M:
4178 colorspace = V4L2_COLORSPACE_SMPTE170M;
4179 break;
4180 case GST_VIDEO_COLOR_PRIMARIES_SMPTE240M:
4181 colorspace = V4L2_COLORSPACE_SMPTE240M;
4182 break;
4183
4184 case GST_VIDEO_COLOR_PRIMARIES_FILM:
4185 case GST_VIDEO_COLOR_PRIMARIES_UNKNOWN:
4186 /* We don't know, we will guess */
4187 break;
4188
4189 default:
4190 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4191 "Unknown colorimetry primaries %d", info.colorimetry.primaries);
4192 break;
4193 }
4194
4195 switch (info.colorimetry.range)
4196 {
4197 case GST_VIDEO_COLOR_RANGE_0_255:
4198 range = V4L2_QUANTIZATION_FULL_RANGE;
4199 break;
4200 case GST_VIDEO_COLOR_RANGE_16_235:
4201 range = V4L2_QUANTIZATION_LIM_RANGE;
4202 break;
4203 case GST_VIDEO_COLOR_RANGE_UNKNOWN:
4204 /* We let the driver pick a default one */
4205 break;
4206 default:
4207 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4208 "Unknown colorimetry range %d", info.colorimetry.range);
4209 break;
4210 }
4211
4212 switch (info.colorimetry.matrix)
4213 {
4214 case GST_VIDEO_COLOR_MATRIX_RGB:
4215 /* Unspecified, leave to default */
4216 break;
4217 /* FCC is about the same as BT601 with less digit */
4218 case GST_VIDEO_COLOR_MATRIX_FCC:
4219 case GST_VIDEO_COLOR_MATRIX_BT601:
4220 matrix = V4L2_YCBCR_ENC_601;
4221 break;
4222 case GST_VIDEO_COLOR_MATRIX_BT709:
4223 matrix = V4L2_YCBCR_ENC_709;
4224 break;
4225 case GST_VIDEO_COLOR_MATRIX_SMPTE240M:
4226 matrix = V4L2_YCBCR_ENC_SMPTE240M;
4227 break;
4228 case GST_VIDEO_COLOR_MATRIX_BT2020:
4229 matrix = V4L2_YCBCR_ENC_BT2020;
4230 break;
4231 case GST_VIDEO_COLOR_MATRIX_UNKNOWN:
4232 /* We let the driver pick a default one */
4233 break;
4234 default:
4235 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4236 "Unknown colorimetry matrix %d", info.colorimetry.matrix);
4237 break;
4238 }
4239
4240 switch (info.colorimetry.transfer)
4241 {
4242 case GST_VIDEO_TRANSFER_GAMMA18:
4243 case GST_VIDEO_TRANSFER_GAMMA20:
4244 case GST_VIDEO_TRANSFER_GAMMA22:
4245 case GST_VIDEO_TRANSFER_GAMMA28:
4246 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4247 "GAMMA 18, 20, 22, 28 transfer functions not supported");
4248 /* fallthrough */
4249 case GST_VIDEO_TRANSFER_GAMMA10:
4250 transfer = V4L2_XFER_FUNC_NONE;
4251 break;
4252 case GST_VIDEO_TRANSFER_BT2020_12:
4253 case GST_VIDEO_TRANSFER_BT709:
4254 transfer = V4L2_XFER_FUNC_709;
4255 break;
4256 case GST_VIDEO_TRANSFER_SMPTE240M:
4257 transfer = V4L2_XFER_FUNC_SMPTE240M;
4258 break;
4259 case GST_VIDEO_TRANSFER_SRGB:
4260 transfer = V4L2_XFER_FUNC_SRGB;
4261 break;
4262 case GST_VIDEO_TRANSFER_LOG100:
4263 case GST_VIDEO_TRANSFER_LOG316:
4264 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4265 "LOG 100, 316 transfer functions not supported");
4266 /* FIXME No known sensible default, maybe AdobeRGB ? */
4267 break;
4268 case GST_VIDEO_TRANSFER_UNKNOWN:
4269 /* We let the driver pick a default one */
4270 break;
4271 default:
4272 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4273 "Unknown colorimetry tranfer %d", info.colorimetry.transfer);
4274 break;
4275 }
4276
4277 if (colorspace == 0)
4278 {
4279 /* Try to guess colorspace according to pixelformat and size */
4280 if (GST_VIDEO_INFO_IS_YUV(&info))
4281 {
4282 if (range == V4L2_QUANTIZATION_FULL_RANGE && matrix == V4L2_YCBCR_ENC_601 && transfer == 0)
4283 {
4284 /* Full range BT.601 YCbCr encoding with unknown primaries and transfer
4285 * function most likely is JPEG */
4286 colorspace = V4L2_COLORSPACE_JPEG;
4287 transfer = V4L2_XFER_FUNC_SRGB;
4288 }
4289 else
4290 {
4291 /* SD streams likely use SMPTE170M and HD streams REC709 */
4292 if (width <= 720 && height <= 576)
4293 colorspace = V4L2_COLORSPACE_SMPTE170M;
4294 else
4295 colorspace = V4L2_COLORSPACE_REC709;
4296 }
4297 }
4298 else if (GST_VIDEO_INFO_IS_RGB(&info))
4299 {
4300 colorspace = V4L2_COLORSPACE_SRGB;
4301 transfer = V4L2_XFER_FUNC_NONE;
4302 }
4303 }
4304
4305 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired format %dx%d, format "
4306 "%" GST_FOURCC_FORMAT " stride: %d",
4307 width, height,
4308 GST_FOURCC_ARGS(pixelformat), GST_VIDEO_INFO_PLANE_STRIDE(&info, 0));
4309
4310 memset(&format, 0x00, sizeof(struct v4l2_format));
4311 format.type = v4l2object->type;
4312
4313 if (is_mplane)
4314 {
4315 format.type = v4l2object->type;
4316 format.fmt.pix_mp.pixelformat = pixelformat;
4317 format.fmt.pix_mp.width = width;
4318 format.fmt.pix_mp.height = height;
4319 format.fmt.pix_mp.field = field;
4320 format.fmt.pix_mp.num_planes = n_v4l_planes;
4321
4322 /* try to ask our prefered stride but it's not a failure if not
4323 * accepted */
4324 for (i = 0; i < n_v4l_planes; i++)
4325 {
4326 gint stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, i);
4327
4328 if (GST_VIDEO_FORMAT_INFO_IS_TILED(info.finfo))
4329 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(info.finfo);
4330
4331 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
4332 }
4333
4334 if (GST_VIDEO_INFO_FORMAT(&info) == GST_VIDEO_FORMAT_ENCODED)
4335 {
4336 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
4337 format.fmt.pix_mp.plane_fmt[0].sizeimage = 1;
4338 else
hanghang.luo75664712024-07-01 19:28:10 +08004339 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 +08004340 }
4341 }
4342 else
4343 {
4344 gint stride = GST_VIDEO_INFO_PLANE_STRIDE(&info, 0);
4345
4346 format.type = v4l2object->type;
4347
4348 format.fmt.pix.width = width;
4349 format.fmt.pix.height = height;
4350 format.fmt.pix.pixelformat = pixelformat;
4351 format.fmt.pix.field = field;
4352
4353 if (GST_VIDEO_FORMAT_INFO_IS_TILED(info.finfo))
4354 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(info.finfo);
4355
4356 /* try to ask our prefered stride */
4357 format.fmt.pix.bytesperline = stride;
4358
4359 if (GST_VIDEO_INFO_FORMAT(&info) == GST_VIDEO_FORMAT_ENCODED)
4360 {
4361 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT)
4362 format.fmt.pix_mp.plane_fmt[0].sizeimage = 1;
4363 else
hanghang.luo75664712024-07-01 19:28:10 +08004364 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 +08004365 }
4366 }
4367
4368 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired format is %dx%d, format "
4369 "%" GST_FOURCC_FORMAT ", nb planes %d",
4370 format.fmt.pix.width,
4371 format.fmt.pix_mp.height,
4372 GST_FOURCC_ARGS(format.fmt.pix.pixelformat),
4373 is_mplane ? format.fmt.pix_mp.num_planes : 1);
4374
4375#ifndef GST_DISABLE_GST_DEBUG
4376 if (is_mplane)
4377 {
4378 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
4379 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d",
4380 format.fmt.pix_mp.plane_fmt[i].bytesperline);
4381 }
4382 else
4383 {
4384 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d",
4385 format.fmt.pix.bytesperline);
4386 }
4387#endif
4388
4389 if (is_mplane)
4390 {
4391 format.fmt.pix_mp.colorspace = colorspace;
4392 format.fmt.pix_mp.quantization = range;
4393 format.fmt.pix_mp.ycbcr_enc = matrix;
4394 format.fmt.pix_mp.xfer_func = transfer;
4395 }
4396 else
4397 {
4398 format.fmt.pix.priv = V4L2_PIX_FMT_PRIV_MAGIC;
4399 format.fmt.pix.colorspace = colorspace;
4400 format.fmt.pix.quantization = range;
4401 format.fmt.pix.ycbcr_enc = matrix;
4402 format.fmt.pix.xfer_func = transfer;
4403 }
4404
4405 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired colorspace is %d:%d:%d:%d",
4406 colorspace, range, matrix, transfer);
4407
4408 if (try_only)
4409 {
4410 if (v4l2object->ioctl(fd, VIDIOC_TRY_FMT, &format) < 0)
4411 goto try_fmt_failed;
4412 }
4413 else
4414 {
4415 if (v4l2object->ioctl(fd, VIDIOC_S_FMT, &format) < 0)
4416 goto set_fmt_failed;
4417 }
4418
4419 if (is_mplane)
4420 {
4421 colorspace = format.fmt.pix_mp.colorspace;
4422 range = format.fmt.pix_mp.quantization;
4423 matrix = format.fmt.pix_mp.ycbcr_enc;
4424 transfer = format.fmt.pix_mp.xfer_func;
4425 }
4426 else
4427 {
4428 colorspace = format.fmt.pix.colorspace;
4429 range = format.fmt.pix.quantization;
4430 matrix = format.fmt.pix.ycbcr_enc;
4431 transfer = format.fmt.pix.xfer_func;
4432 }
4433
4434 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got format of %dx%d, format "
4435 "%" GST_FOURCC_FORMAT ", nb planes %d, colorspace %d:%d:%d:%d",
4436 format.fmt.pix.width, format.fmt.pix_mp.height,
4437 GST_FOURCC_ARGS(format.fmt.pix.pixelformat),
4438 is_mplane ? format.fmt.pix_mp.num_planes : 1,
4439 colorspace, range, matrix, transfer);
4440
4441#ifndef GST_DISABLE_GST_DEBUG
4442 if (is_mplane)
4443 {
4444 for (i = 0; i < format.fmt.pix_mp.num_planes; i++)
4445 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d, sizeimage %d",
4446 format.fmt.pix_mp.plane_fmt[i].bytesperline,
4447 format.fmt.pix_mp.plane_fmt[i].sizeimage);
4448 }
4449 else
4450 {
4451 GST_DEBUG_OBJECT(v4l2object->dbg_obj, " stride %d, sizeimage %d",
4452 format.fmt.pix.bytesperline, format.fmt.pix.sizeimage);
4453 }
4454#endif
4455
4456 if (format.fmt.pix.pixelformat != pixelformat)
4457 goto invalid_pixelformat;
4458
4459 /* Only negotiate size with raw data.
4460 * For some codecs the dimensions are *not* in the bitstream, IIRC VC1
4461 * in ASF mode for example, there is also not reason for a driver to
4462 * change the size. */
4463 if (info.finfo->format != GST_VIDEO_FORMAT_ENCODED)
4464 {
4465 /* We can crop larger images */
4466 if (format.fmt.pix.width < width || format.fmt.pix.height < height)
4467 goto invalid_dimensions;
4468
4469 /* Note, this will be adjusted if upstream has non-centered cropping. */
4470 align.padding_top = 0;
4471 align.padding_bottom = format.fmt.pix.height - height;
4472 align.padding_left = 0;
4473 align.padding_right = format.fmt.pix.width - width;
4474 }
4475
4476 if (is_mplane && format.fmt.pix_mp.num_planes != n_v4l_planes)
4477 goto invalid_planes;
4478
4479 /* used to check colorimetry and interlace mode fields presence */
4480 s = gst_caps_get_structure(caps, 0);
4481
4482 if (!gst_aml_v4l2_object_get_interlace_mode(format.fmt.pix.field,
4483 &info.interlace_mode))
4484 goto invalid_field;
4485 if (gst_structure_has_field(s, "interlace-mode"))
4486 {
4487 if (format.fmt.pix.field != field)
4488 goto invalid_field;
4489 }
4490
4491 if (gst_aml_v4l2_object_get_colorspace(&format, &info.colorimetry))
4492 {
4493 if (gst_structure_has_field(s, "colorimetry"))
4494 {
xuesong.jiange1a19662022-06-21 20:30:22 +08004495 if (!gst_aml_v4l2_video_colorimetry_matches(&info.colorimetry, gst_structure_get_string(s, "colorimetry")))
4496 {
4497 // goto invalid_colorimetry;
4498 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004499 }
4500 }
4501 else
4502 {
4503 /* The driver (or libv4l2) is miss-behaving, just ignore colorimetry from
4504 * the TRY_FMT */
4505 disable_colorimetry = TRUE;
4506 if (gst_structure_has_field(s, "colorimetry"))
4507 gst_structure_remove_field(s, "colorimetry");
4508 }
4509
4510 /* In case we have skipped the try_fmt probes, we'll need to set the
4511 * colorimetry and interlace-mode back into the caps. */
4512 if (v4l2object->skip_try_fmt_probes)
4513 {
4514 if (!disable_colorimetry && !gst_structure_has_field(s, "colorimetry"))
4515 {
4516 gchar *str = gst_video_colorimetry_to_string(&info.colorimetry);
4517 gst_structure_set(s, "colorimetry", G_TYPE_STRING, str, NULL);
4518 g_free(str);
4519 }
4520
4521 if (!gst_structure_has_field(s, "interlace-mode"))
4522 gst_structure_set(s, "interlace-mode", G_TYPE_STRING,
4523 gst_video_interlace_mode_to_string(info.interlace_mode), NULL);
4524 }
4525
4526 if (try_only) /* good enough for trying only */
4527 return TRUE;
4528
4529 if (GST_VIDEO_INFO_HAS_ALPHA(&info))
4530 {
4531 struct v4l2_control ctl = {
4532 0,
4533 };
4534 ctl.id = V4L2_CID_ALPHA_COMPONENT;
4535 ctl.value = 0xff;
4536
4537 if (v4l2object->ioctl(fd, VIDIOC_S_CTRL, &ctl) < 0)
4538 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4539 "Failed to set alpha component value");
4540 }
4541
4542 /* Is there a reason we require the caller to always specify a framerate? */
4543 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Desired framerate: %u/%u", fps_n,
4544 fps_d);
4545
4546 if (v4l2object->ioctl(fd, VIDIOC_G_PARM, &streamparm) < 0)
4547 goto get_parm_failed;
4548
4549 if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || v4l2object->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
4550 {
4551 GST_VIDEO_INFO_FPS_N(&info) =
4552 streamparm.parm.capture.timeperframe.denominator;
4553 GST_VIDEO_INFO_FPS_D(&info) =
4554 streamparm.parm.capture.timeperframe.numerator;
4555
4556 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got capture framerate: %u/%u",
4557 streamparm.parm.capture.timeperframe.denominator,
4558 streamparm.parm.capture.timeperframe.numerator);
4559
4560 /* We used to skip frame rate setup if the camera was already setup
4561 * with the requested frame rate. This breaks some cameras though,
4562 * causing them to not output data (several models of Thinkpad cameras
4563 * have this problem at least).
4564 * So, don't skip. */
4565 GST_LOG_OBJECT(v4l2object->dbg_obj, "Setting capture framerate to %u/%u",
4566 fps_n, fps_d);
4567 /* We want to change the frame rate, so check whether we can. Some cheap USB
4568 * cameras don't have the capability */
4569 if ((streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) == 0)
4570 {
4571 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
4572 "Not setting capture framerate (not supported)");
4573 goto done;
4574 }
4575
4576 /* Note: V4L2 wants the frame interval, we have the frame rate */
4577 streamparm.parm.capture.timeperframe.numerator = fps_d;
4578 streamparm.parm.capture.timeperframe.denominator = fps_n;
4579
xuesong.jiangae1548e2022-05-06 16:38:46 +08004580 if (streamparm.parm.capture.timeperframe.numerator > 0 &&
4581 streamparm.parm.capture.timeperframe.denominator > 0)
4582 {
4583 /* get new values */
4584 fps_d = streamparm.parm.capture.timeperframe.numerator;
4585 fps_n = streamparm.parm.capture.timeperframe.denominator;
4586
4587 GST_INFO_OBJECT(v4l2object->dbg_obj, "Set capture framerate to %u/%u",
4588 fps_n, fps_d);
4589 }
4590 else
4591 {
4592 /* fix v4l2 capture driver to provide framerate values */
4593 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4594 "Reuse caps framerate %u/%u - fix v4l2 capture driver", fps_n, fps_d);
4595 }
4596
4597 GST_VIDEO_INFO_FPS_N(&info) = fps_n;
4598 GST_VIDEO_INFO_FPS_D(&info) = fps_d;
4599 }
4600 else if (v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT || v4l2object->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
4601 {
4602 GST_VIDEO_INFO_FPS_N(&info) =
4603 streamparm.parm.output.timeperframe.denominator;
4604 GST_VIDEO_INFO_FPS_D(&info) =
4605 streamparm.parm.output.timeperframe.numerator;
4606
4607 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Got output framerate: %u/%u",
4608 streamparm.parm.output.timeperframe.denominator,
4609 streamparm.parm.output.timeperframe.numerator);
4610
4611 GST_LOG_OBJECT(v4l2object->dbg_obj, "Setting output framerate to %u/%u",
4612 fps_n, fps_d);
4613 if ((streamparm.parm.output.capability & V4L2_CAP_TIMEPERFRAME) == 0)
4614 {
4615 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
4616 "Not setting output framerate (not supported)");
4617 goto done;
4618 }
4619
4620 /* Note: V4L2 wants the frame interval, we have the frame rate */
4621 streamparm.parm.output.timeperframe.numerator = fps_d;
4622 streamparm.parm.output.timeperframe.denominator = fps_n;
4623
xuesong.jiangae1548e2022-05-06 16:38:46 +08004624 if (streamparm.parm.output.timeperframe.numerator > 0 &&
4625 streamparm.parm.output.timeperframe.denominator > 0)
4626 {
4627 /* get new values */
4628 fps_d = streamparm.parm.output.timeperframe.numerator;
4629 fps_n = streamparm.parm.output.timeperframe.denominator;
4630
4631 GST_INFO_OBJECT(v4l2object->dbg_obj, "Set output framerate to %u/%u",
4632 fps_n, fps_d);
4633 }
4634 else
4635 {
4636 /* fix v4l2 output driver to provide framerate values */
4637 GST_WARNING_OBJECT(v4l2object->dbg_obj,
4638 "Reuse caps framerate %u/%u - fix v4l2 output driver", fps_n, fps_d);
4639 }
4640
4641 GST_VIDEO_INFO_FPS_N(&info) = fps_n;
4642 GST_VIDEO_INFO_FPS_D(&info) = fps_d;
4643 }
4644
4645done:
4646 /* add boolean return, so we can fail on drivers bugs */
4647 gst_aml_v4l2_object_save_format(v4l2object, fmtdesc, &format, &info, &align);
4648
4649 /* now configure the pool */
4650 if (!gst_aml_v4l2_object_setup_pool(v4l2object, caps))
4651 goto pool_failed;
4652
4653 return TRUE;
4654
4655 /* ERRORS */
4656invalid_caps:
4657{
4658 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "can't parse caps %" GST_PTR_FORMAT,
4659 caps);
4660
4661 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4662 (_("Invalid caps")), ("Can't parse caps %" GST_PTR_FORMAT, caps));
4663 return FALSE;
4664}
4665try_fmt_failed:
4666{
4667 if (errno == EINVAL)
4668 {
4669 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4670 (_("Device '%s' has no supported format"), v4l2object->videodev),
4671 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4672 GST_FOURCC_ARGS(pixelformat), width, height,
4673 g_strerror(errno)));
4674 }
4675 else
4676 {
4677 GST_AML_V4L2_ERROR(error, RESOURCE, FAILED,
4678 (_("Device '%s' failed during initialization"),
4679 v4l2object->videodev),
4680 ("Call to TRY_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4681 GST_FOURCC_ARGS(pixelformat), width, height,
4682 g_strerror(errno)));
4683 }
4684 return FALSE;
4685}
4686set_fmt_failed:
4687{
4688 if (errno == EBUSY)
4689 {
4690 GST_AML_V4L2_ERROR(error, RESOURCE, BUSY,
4691 (_("Device '%s' is busy"), v4l2object->videodev),
4692 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4693 GST_FOURCC_ARGS(pixelformat), width, height,
4694 g_strerror(errno)));
4695 }
4696 else if (errno == EINVAL)
4697 {
4698 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4699 (_("Device '%s' has no supported format"), v4l2object->videodev),
4700 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4701 GST_FOURCC_ARGS(pixelformat), width, height,
4702 g_strerror(errno)));
4703 }
4704 else
4705 {
4706 GST_AML_V4L2_ERROR(error, RESOURCE, FAILED,
4707 (_("Device '%s' failed during initialization"),
4708 v4l2object->videodev),
4709 ("Call to S_FMT failed for %" GST_FOURCC_FORMAT " @ %dx%d: %s",
4710 GST_FOURCC_ARGS(pixelformat), width, height,
4711 g_strerror(errno)));
4712 }
4713 return FALSE;
4714}
4715invalid_dimensions:
4716{
4717 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4718 (_("Device '%s' cannot capture at %dx%d"),
4719 v4l2object->videodev, width, height),
4720 ("Tried to capture at %dx%d, but device returned size %dx%d",
4721 width, height, format.fmt.pix.width, format.fmt.pix.height));
4722 return FALSE;
4723}
4724invalid_pixelformat:
4725{
4726 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4727 (_("Device '%s' cannot capture in the specified format"),
4728 v4l2object->videodev),
4729 ("Tried to capture in %" GST_FOURCC_FORMAT
4730 ", but device returned format"
4731 " %" GST_FOURCC_FORMAT,
4732 GST_FOURCC_ARGS(pixelformat),
4733 GST_FOURCC_ARGS(format.fmt.pix.pixelformat)));
4734 return FALSE;
4735}
4736invalid_planes:
4737{
4738 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4739 (_("Device '%s' does support non-contiguous planes"),
4740 v4l2object->videodev),
4741 ("Device wants %d planes", format.fmt.pix_mp.num_planes));
4742 return FALSE;
4743}
4744invalid_field:
4745{
4746 enum v4l2_field wanted_field;
4747
4748 if (is_mplane)
4749 wanted_field = format.fmt.pix_mp.field;
4750 else
4751 wanted_field = format.fmt.pix.field;
4752
4753 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4754 (_("Device '%s' does not support %s interlacing"),
4755 v4l2object->videodev,
4756 field == V4L2_FIELD_NONE ? "progressive" : "interleaved"),
4757 ("Device wants %s interlacing",
4758 wanted_field == V4L2_FIELD_NONE ? "progressive" : "interleaved"));
4759 return FALSE;
4760}
hanghang.luo3128f102022-08-18 10:36:19 +08004761#ifdef DELETE_FOR_LGE
xuesong.jiangae1548e2022-05-06 16:38:46 +08004762invalid_colorimetry:
4763{
4764 gchar *wanted_colorimetry;
4765
4766 wanted_colorimetry = gst_video_colorimetry_to_string(&info.colorimetry);
4767
4768 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4769 (_("Device '%s' does not support %s colorimetry"),
4770 v4l2object->videodev, gst_structure_get_string(s, "colorimetry")),
4771 ("Device wants %s colorimetry", wanted_colorimetry));
4772
4773 g_free(wanted_colorimetry);
4774 return FALSE;
4775}
hanghang.luo3128f102022-08-18 10:36:19 +08004776#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08004777get_parm_failed:
4778{
4779 /* it's possible that this call is not supported */
4780 if (errno != EINVAL && errno != ENOTTY)
4781 {
4782 GST_AML_V4L2_ERROR(error, RESOURCE, SETTINGS,
4783 (_("Could not get parameters on device '%s'"),
4784 v4l2object->videodev),
4785 GST_ERROR_SYSTEM);
4786 }
4787 goto done;
4788}
xuesong.jiangae1548e2022-05-06 16:38:46 +08004789pool_failed:
4790{
4791 /* setup_pool already send the error */
4792 return FALSE;
4793}
4794}
4795
4796gboolean
4797gst_aml_v4l2_object_set_format(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4798 GstAmlV4l2Error *error)
4799{
4800 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Setting format to %" GST_PTR_FORMAT,
4801 caps);
4802 return gst_aml_v4l2_object_set_format_full(v4l2object, caps, FALSE, error);
4803}
4804
4805gboolean
4806gst_aml_v4l2_object_try_format(GstAmlV4l2Object *v4l2object, GstCaps *caps,
4807 GstAmlV4l2Error *error)
4808{
4809 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "Trying format %" GST_PTR_FORMAT,
4810 caps);
4811 return gst_aml_v4l2_object_set_format_full(v4l2object, caps, TRUE, error);
4812}
4813
4814GstFlowReturn
4815gst_aml_v4l2_object_poll(GstAmlV4l2Object *v4l2object)
4816{
4817 gint ret;
4818
4819 if (!v4l2object->can_poll_device)
4820 goto done;
4821
4822 GST_LOG_OBJECT(v4l2object, "polling device");
4823
4824again:
4825 ret = gst_poll_wait(v4l2object->poll, GST_CLOCK_TIME_NONE);
4826 if (G_UNLIKELY(ret < 0))
4827 {
4828 switch (errno)
4829 {
4830 case EBUSY:
4831 goto stopped;
4832 case EAGAIN:
4833 case EINTR:
4834 goto again;
4835 case ENXIO:
4836 GST_WARNING_OBJECT(v4l2object,
4837 "v4l2 device doesn't support polling. Disabling"
4838 " using libv4l2 in this case may cause deadlocks");
4839 v4l2object->can_poll_device = FALSE;
4840 goto done;
4841 default:
4842 goto select_error;
4843 }
4844 }
4845
4846done:
4847 return GST_FLOW_OK;
4848
4849 /* ERRORS */
4850stopped:
4851{
4852 GST_DEBUG_OBJECT(v4l2object, "stop called");
4853 return GST_FLOW_FLUSHING;
4854}
4855select_error:
4856{
4857 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, READ, (NULL),
4858 ("poll error %d: %s (%d)", ret, g_strerror(errno), errno));
4859 return GST_FLOW_ERROR;
4860}
4861}
4862
4863GstFlowReturn
4864gst_aml_v4l2_object_dqevent(GstAmlV4l2Object *v4l2object)
4865{
4866 GstFlowReturn res;
4867 struct v4l2_event evt;
4868
4869 if ((res = gst_aml_v4l2_object_poll(v4l2object)) != GST_FLOW_OK)
4870 goto poll_failed;
4871
fei.deng678f7052024-07-10 20:11:26 +08004872 //only read v4l2 pri event
4873 if (!gst_poll_fd_can_read_pri(v4l2object->poll, &v4l2object->pollfd))
4874 {
4875 GST_DEBUG_OBJECT(v4l2object, "not v4l2 pri event");
4876 return GST_FLOW_OK;
4877 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08004878 memset(&evt, 0x00, sizeof(struct v4l2_event));
4879 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DQEVENT, &evt) < 0)
4880 goto dqevent_failed;
4881
4882 switch (evt.type)
4883 {
4884 case V4L2_EVENT_SOURCE_CHANGE:
4885 return GST_AML_V4L2_FLOW_SOURCE_CHANGE;
4886 break;
4887 case V4L2_EVENT_EOS:
4888 return GST_AML_V4L2_FLOW_LAST_BUFFER;
4889 break;
4890 default:
4891 break;
4892 }
4893
4894 return GST_FLOW_OK;
4895
4896 /* ERRORS */
4897poll_failed:
4898{
4899 GST_DEBUG_OBJECT(v4l2object, "poll error %s", gst_flow_get_name(res));
4900 return res;
4901}
4902dqevent_failed:
4903{
fei.deng678f7052024-07-10 20:11:26 +08004904 GST_DEBUG_OBJECT(v4l2object, "dqevent failed");
xuesong.jiangae1548e2022-05-06 16:38:46 +08004905 return GST_FLOW_ERROR;
4906}
4907}
4908
4909/**
4910 * gst_aml_v4l2_object_acquire_format:
4911 * @v4l2object the object
4912 * @info a GstVideoInfo to be filled
4913 *
4914 * Acquire the driver choosen format. This is useful in decoder or encoder elements where
4915 * the output format is choosen by the HW.
4916 *
4917 * Returns: %TRUE on success, %FALSE on failure.
4918 */
4919gboolean
4920gst_aml_v4l2_object_acquire_format(GstAmlV4l2Object *v4l2object, GstVideoInfo *info)
4921{
4922 struct v4l2_fmtdesc *fmtdesc;
4923 struct v4l2_format fmt;
4924 struct v4l2_crop crop;
4925 struct v4l2_selection sel;
sheng.liu641aa422023-12-26 07:05:59 +00004926 struct v4l2_cropcap cropcap;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004927 struct v4l2_rect *r = NULL;
4928 GstVideoFormat format;
4929 guint width, height;
4930 GstVideoAlignment align;
sheng.liu641aa422023-12-26 07:05:59 +00004931 gdouble pixelAspectRatio = 0.0;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004932
4933 gst_video_info_init(info);
4934 gst_video_alignment_reset(&align);
4935
4936 memset(&fmt, 0x00, sizeof(struct v4l2_format));
4937 fmt.type = v4l2object->type;
fei.denge9458472023-04-18 02:05:48 +00004938 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "fmt.type:%d", fmt.type);
xuesong.jiangae1548e2022-05-06 16:38:46 +08004939 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_FMT, &fmt) < 0)
4940 goto get_fmt_failed;
4941
4942 fmtdesc = gst_aml_v4l2_object_get_format_from_fourcc(v4l2object,
4943 fmt.fmt.pix.pixelformat);
4944 if (fmtdesc == NULL)
4945 goto unsupported_format;
4946
4947 /* No need to care about mplane, the four first params are the same */
4948 format = gst_aml_v4l2_object_v4l2fourcc_to_video_format(fmt.fmt.pix.pixelformat);
4949
4950 /* fails if we do no translate the fmt.pix.pixelformat to GstVideoFormat */
4951 if (format == GST_VIDEO_FORMAT_UNKNOWN)
4952 goto unsupported_format;
4953
4954 if (fmt.fmt.pix.width == 0 || fmt.fmt.pix.height == 0)
4955 goto invalid_dimensions;
4956
4957 width = fmt.fmt.pix.width;
4958 height = fmt.fmt.pix.height;
xuesong.jiangae1548e2022-05-06 16:38:46 +08004959 /* Use the default compose rectangle */
4960 memset(&sel, 0, sizeof(struct v4l2_selection));
4961 sel.type = v4l2object->type;
4962 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
4963 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
4964 {
4965 r = &sel.r;
4966 }
4967 else
4968 {
4969 /* For ancient kernels, fall back to G_CROP */
4970 memset(&crop, 0, sizeof(struct v4l2_crop));
4971 crop.type = v4l2object->type;
4972 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_G_CROP, &crop) >= 0)
4973 r = &crop.c;
4974 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004975
sheng.liu07337c82023-12-01 03:21:57 +00004976 if (r)
xuesong.jiangae1548e2022-05-06 16:38:46 +08004977 {
bo.xiao7659cda2024-07-18 16:16:50 +08004978 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec*) v4l2object->element;
4979
xuesong.jiangae1548e2022-05-06 16:38:46 +08004980 align.padding_left = r->left;
4981 align.padding_top = r->top;
4982 align.padding_right = width - r->width - r->left;
4983 align.padding_bottom = height - r->height - r->top;
4984 width = r->width;
4985 height = r->height;
bo.xiao7659cda2024-07-18 16:16:50 +08004986
4987 if (self->v4l2output->dw_mode >= 0 && self->v4l2output->dw_mode != VDEC_DW_NO_AFBC)
sheng.liu07337c82023-12-01 03:21:57 +00004988 {
bo.xiao7659cda2024-07-18 16:16:50 +08004989 width = (width/2) *2; // align for dw
4990 height = (height/2) *2; // align for dw
sheng.liu07337c82023-12-01 03:21:57 +00004991 }
bo.xiao7659cda2024-07-18 16:16:50 +08004992
4993 if (self->v4l2output->dw_mode == VDEC_DW_AFBC_ONLY)
4994 height = width = 64; //because driver return src w,h when AFBC_ONLY
xuesong.jiangae1548e2022-05-06 16:38:46 +08004995 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08004996 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "final w:%d, h:%d", width, height);
xuesong.jiangae1548e2022-05-06 16:38:46 +08004997
4998 gst_video_info_set_format(info, format, width, height);
4999
5000 switch (fmt.fmt.pix.field)
5001 {
5002 case V4L2_FIELD_ANY:
5003 case V4L2_FIELD_NONE:
5004 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_PROGRESSIVE;
5005 break;
5006 case V4L2_FIELD_INTERLACED:
5007 case V4L2_FIELD_INTERLACED_TB:
5008 case V4L2_FIELD_INTERLACED_BT:
5009 info->interlace_mode = GST_VIDEO_INTERLACE_MODE_INTERLEAVED;
5010 break;
5011 default:
5012 goto unsupported_field;
5013 }
5014
5015 gst_aml_v4l2_object_get_colorspace(&fmt, &info->colorimetry);
5016
5017 gst_aml_v4l2_object_save_format(v4l2object, fmtdesc, &fmt, info, &align);
5018
sheng.liu641aa422023-12-26 07:05:59 +00005019 if (v4l2object->par)
5020 {
5021 width = gst_value_get_fraction_numerator(v4l2object->par);
5022 height = gst_value_get_fraction_denominator(v4l2object->par);
5023 pixelAspectRatio = (gdouble)width/(gdouble)height;
5024 }
5025
5026 if (!v4l2object->par || pixelAspectRatio == 1.0)
5027 {
5028 memset(&cropcap, 0, sizeof(cropcap));
5029 width= height= 1;
5030 cropcap.type = v4l2object->type;
5031 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) >= 0)
5032 {
5033 width= cropcap.pixelaspect.denominator;
5034 height= cropcap.pixelaspect.numerator;
5035 GST_DEBUG("cropcap: pixel aspect ratio %d:%d", width, height);
5036 if ( !width || !height )
5037 {
5038 GST_DEBUG("force pixel aspect of 1:1");
5039 width= height= 1;
5040 }
5041 }
sheng.liu641aa422023-12-26 07:05:59 +00005042 }
sheng.liudb26f7d2024-01-22 11:24:23 +00005043
sheng.liud9027ca2024-01-24 09:21:49 +00005044 GST_VIDEO_INFO_PAR_N(info) = width;
5045 GST_VIDEO_INFO_PAR_D(info) = height;
5046
sheng.liudb26f7d2024-01-22 11:24:23 +00005047 if (v4l2object->fps)
5048 {
5049 GST_VIDEO_INFO_FPS_N(info) = gst_value_get_fraction_numerator(v4l2object->fps);
5050 GST_VIDEO_INFO_FPS_D(info) = gst_value_get_fraction_denominator(v4l2object->fps);
5051 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08005052 /* Shall we setup the pool ? */
5053
5054 return TRUE;
5055
5056get_fmt_failed:
5057{
5058 GST_ELEMENT_WARNING(v4l2object->element, RESOURCE, SETTINGS,
5059 (_("Video device did not provide output format.")), GST_ERROR_SYSTEM);
5060 return FALSE;
5061}
5062invalid_dimensions:
5063{
5064 GST_ELEMENT_WARNING(v4l2object->element, RESOURCE, SETTINGS,
5065 (_("Video device returned invalid dimensions.")),
5066 ("Expected non 0 dimensions, got %dx%d", fmt.fmt.pix.width,
5067 fmt.fmt.pix.height));
5068 return FALSE;
5069}
5070unsupported_field:
5071{
5072 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
5073 (_("Video device uses an unsupported interlacing method.")),
5074 ("V4L2 field type %d not supported", fmt.fmt.pix.field));
5075 return FALSE;
5076}
5077unsupported_format:
5078{
5079 GST_ELEMENT_ERROR(v4l2object->element, RESOURCE, SETTINGS,
5080 (_("Video device uses an unsupported pixel format.")),
5081 ("V4L2 format %" GST_FOURCC_FORMAT " not supported",
5082 GST_FOURCC_ARGS(fmt.fmt.pix.pixelformat)));
5083 return FALSE;
5084}
5085}
5086
5087gboolean
5088gst_aml_v4l2_object_set_crop(GstAmlV4l2Object *obj)
5089{
5090 struct v4l2_selection sel = {0};
5091 struct v4l2_crop crop = {0};
5092
5093 sel.type = obj->type;
5094 sel.target = V4L2_SEL_TGT_CROP;
5095 sel.flags = 0;
5096 sel.r.left = obj->align.padding_left;
5097 sel.r.top = obj->align.padding_top;
5098 sel.r.width = obj->info.width;
5099 sel.r.height = obj->info.height;
5100
5101 crop.type = obj->type;
5102 crop.c = sel.r;
5103
5104 if (obj->align.padding_left + obj->align.padding_top +
5105 obj->align.padding_right + obj->align.padding_bottom ==
5106 0)
5107 {
5108 GST_DEBUG_OBJECT(obj->dbg_obj, "no cropping needed");
5109 return TRUE;
5110 }
5111
5112 GST_DEBUG_OBJECT(obj->dbg_obj,
5113 "Desired cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
5114 crop.c.width, crop.c.height);
5115
5116 if (obj->ioctl(obj->video_fd, VIDIOC_S_SELECTION, &sel) < 0)
5117 {
5118 if (errno != ENOTTY)
5119 {
5120 GST_WARNING_OBJECT(obj->dbg_obj,
5121 "Failed to set crop rectangle with VIDIOC_S_SELECTION: %s",
5122 g_strerror(errno));
5123 return FALSE;
5124 }
5125 else
5126 {
5127 if (obj->ioctl(obj->video_fd, VIDIOC_S_CROP, &crop) < 0)
5128 {
5129 GST_WARNING_OBJECT(obj->dbg_obj, "VIDIOC_S_CROP failed");
5130 return FALSE;
5131 }
5132
5133 if (obj->ioctl(obj->video_fd, VIDIOC_G_CROP, &crop) < 0)
5134 {
5135 GST_WARNING_OBJECT(obj->dbg_obj, "VIDIOC_G_CROP failed");
5136 return FALSE;
5137 }
5138
5139 sel.r = crop.c;
5140 }
5141 }
5142
5143 GST_DEBUG_OBJECT(obj->dbg_obj,
5144 "Got cropping left %u, top %u, size %ux%u", crop.c.left, crop.c.top,
5145 crop.c.width, crop.c.height);
5146
5147 return TRUE;
5148}
5149
5150gboolean
5151gst_aml_v4l2_object_caps_equal(GstAmlV4l2Object *v4l2object, GstCaps *caps)
5152{
5153 GstStructure *config;
5154 GstCaps *oldcaps;
5155 gboolean ret;
5156
5157 if (!v4l2object->pool)
5158 return FALSE;
5159
5160 config = gst_buffer_pool_get_config(v4l2object->pool);
5161 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5162
5163 ret = oldcaps && gst_caps_is_equal(caps, oldcaps);
5164
5165 gst_structure_free(config);
5166
5167 return ret;
5168}
5169
5170gboolean
5171gst_aml_v4l2_object_caps_is_subset(GstAmlV4l2Object *v4l2object, GstCaps *caps)
5172{
5173 GstStructure *config;
5174 GstCaps *oldcaps;
5175 gboolean ret;
5176
5177 if (!v4l2object->pool)
5178 return FALSE;
5179
5180 config = gst_buffer_pool_get_config(v4l2object->pool);
5181 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5182
5183 ret = oldcaps && gst_caps_is_subset(oldcaps, caps);
5184
5185 gst_structure_free(config);
5186
5187 return ret;
5188}
5189
5190GstCaps *
5191gst_aml_v4l2_object_get_current_caps(GstAmlV4l2Object *v4l2object)
5192{
5193 GstStructure *config;
5194 GstCaps *oldcaps;
5195
5196 if (!v4l2object->pool)
5197 return NULL;
5198
5199 config = gst_buffer_pool_get_config(v4l2object->pool);
5200 gst_buffer_pool_config_get_params(config, &oldcaps, NULL, NULL, NULL);
5201
5202 if (oldcaps)
5203 gst_caps_ref(oldcaps);
5204
5205 gst_structure_free(config);
5206
5207 return oldcaps;
5208}
5209
5210gboolean
fei.deng6bccf822024-08-22 10:05:44 +08005211gst_aml_v4l2_object_flush_start(GstAmlV4l2Object *v4l2object)
xuesong.jiangae1548e2022-05-06 16:38:46 +08005212{
5213 gboolean ret = TRUE;
5214
5215 GST_LOG_OBJECT(v4l2object->dbg_obj, "start flushing");
5216
5217 gst_poll_set_flushing(v4l2object->poll, TRUE);
5218
5219 if (v4l2object->pool && gst_buffer_pool_is_active(v4l2object->pool))
5220 gst_buffer_pool_set_flushing(v4l2object->pool, TRUE);
5221
5222 return ret;
5223}
5224
5225gboolean
fei.deng6bccf822024-08-22 10:05:44 +08005226gst_aml_v4l2_object_flush_stop(GstAmlV4l2Object *v4l2object)
xuesong.jiangae1548e2022-05-06 16:38:46 +08005227{
5228 gboolean ret = TRUE;
5229
5230 GST_LOG_OBJECT(v4l2object->dbg_obj, "stop flushing");
5231
5232 if (v4l2object->pool && gst_buffer_pool_is_active(v4l2object->pool))
5233 gst_buffer_pool_set_flushing(v4l2object->pool, FALSE);
5234
5235 gst_poll_set_flushing(v4l2object->poll, FALSE);
5236
5237 return ret;
5238}
5239
5240gboolean
5241gst_aml_v4l2_object_stop(GstAmlV4l2Object *v4l2object)
5242{
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005243 GstAmlV4l2BufferPool *bpool = GST_AML_V4L2_BUFFER_POOL(v4l2object->pool);
5244
xuesong.jiangae1548e2022-05-06 16:38:46 +08005245 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "stopping");
5246
5247 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
5248 goto done;
5249 if (!GST_AML_V4L2_IS_ACTIVE(v4l2object))
5250 goto done;
5251
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005252 if (bpool && bpool->other_pool) /* jxsdbg for resolution switch */
5253 {
5254 if (v4l2object->old_other_pool)
5255 {
5256 /* this case indicate 1st switch did not wait all old pool buf recycle and 2nd switch is coming.
5257 so save 1st old pool */
5258 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "switching occurs during last switching buf recycle flow");
5259 v4l2object->old_old_other_pool = v4l2object->old_other_pool;
5260 }
5261
5262 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "switching flow, ref old drmbufferpool");
5263 v4l2object->old_other_pool = bpool->other_pool;
5264 gst_object_ref(v4l2object->old_other_pool);
5265 }
5266
xuesong.jiangae1548e2022-05-06 16:38:46 +08005267 if (v4l2object->pool)
5268 {
5269 if (!gst_aml_v4l2_buffer_pool_orphan(&v4l2object->pool))
5270 {
5271 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "deactivating pool");
5272 gst_buffer_pool_set_active(v4l2object->pool, FALSE);
5273 gst_object_unref(v4l2object->pool);
5274 }
5275 v4l2object->pool = NULL;
5276 }
5277
5278 GST_AML_V4L2_SET_INACTIVE(v4l2object);
fei.deng6bccf822024-08-22 10:05:44 +08005279 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "stopped");
xuesong.jiangae1548e2022-05-06 16:38:46 +08005280done:
5281 return TRUE;
5282}
5283
5284GstCaps *
5285gst_aml_v4l2_object_probe_caps(GstAmlV4l2Object *v4l2object, GstCaps *filter)
5286{
5287 GstCaps *ret;
5288 GSList *walk;
5289 GSList *formats;
5290
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005291 GST_INFO_OBJECT(v4l2object->dbg_obj, "filter caps: %" GST_PTR_FORMAT, filter);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005292 formats = gst_aml_v4l2_object_get_format_list(v4l2object);
5293
5294 ret = gst_caps_new_empty();
5295
sheng.liu641aa422023-12-26 07:05:59 +00005296// At this time, decoder will return defult aspect, and it is not usful.
5297// so, do not probe cropcap at this time and do this action after decoding.
5298#if 0
xuesong.jiangae1548e2022-05-06 16:38:46 +08005299 if (v4l2object->keep_aspect && !v4l2object->par)
5300 {
5301 struct v4l2_cropcap cropcap;
5302
5303 memset(&cropcap, 0, sizeof(cropcap));
5304
5305 cropcap.type = v4l2object->type;
5306 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_CROPCAP, &cropcap) < 0)
5307 {
5308 if (errno != ENOTTY)
5309 GST_WARNING_OBJECT(v4l2object->dbg_obj,
5310 "Failed to probe pixel aspect ratio with VIDIOC_CROPCAP: %s",
5311 g_strerror(errno));
5312 }
5313 else if (cropcap.pixelaspect.numerator && cropcap.pixelaspect.denominator)
5314 {
5315 v4l2object->par = g_new0(GValue, 1);
5316 g_value_init(v4l2object->par, GST_TYPE_FRACTION);
5317 gst_value_set_fraction(v4l2object->par, cropcap.pixelaspect.numerator,
5318 cropcap.pixelaspect.denominator);
5319 }
5320 }
sheng.liu641aa422023-12-26 07:05:59 +00005321#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08005322
5323 for (walk = formats; walk; walk = walk->next)
5324 {
5325 struct v4l2_fmtdesc *format;
5326 GstStructure *template;
5327 GstCaps *tmp, *tmp2;
5328
5329 format = (struct v4l2_fmtdesc *)walk->data;
5330
5331 template = gst_aml_v4l2_object_v4l2fourcc_to_bare_struct(format->pixelformat);
5332
5333 if (!template)
5334 {
5335 GST_DEBUG_OBJECT(v4l2object->dbg_obj,
5336 "unknown format %" GST_FOURCC_FORMAT,
5337 GST_FOURCC_ARGS(format->pixelformat));
5338 continue;
5339 }
5340
5341 /* If we have a filter, check if we need to probe this format or not */
5342 if (filter)
5343 {
5344 GstCaps *format_caps = gst_caps_new_empty();
5345
5346 gst_caps_append_structure(format_caps, gst_structure_copy(template));
xuesong.jiange1a19662022-06-21 20:30:22 +08005347 GST_INFO_OBJECT(v4l2object->dbg_obj, "format_caps: %" GST_PTR_FORMAT, format_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005348
5349 if (!gst_caps_can_intersect(format_caps, filter))
5350 {
5351 gst_caps_unref(format_caps);
5352 gst_structure_free(template);
5353 continue;
5354 }
5355
5356 gst_caps_unref(format_caps);
5357 }
5358
5359 tmp = gst_aml_v4l2_object_probe_caps_for_format(v4l2object,
5360 format->pixelformat, template);
xuesong.jiange1a19662022-06-21 20:30:22 +08005361 GST_INFO_OBJECT(v4l2object->dbg_obj, "tmp caps: %" GST_PTR_FORMAT, tmp);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005362
5363 if (tmp)
5364 {
5365 tmp2 = gst_caps_copy(tmp);
5366 gst_caps_set_features_simple(tmp2, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
5367 gst_caps_append(ret, tmp);
5368 gst_caps_append(ret, tmp2);
5369 }
5370
5371 gst_structure_free(template);
5372 }
5373
5374 if (filter)
5375 {
5376 GstCaps *tmp;
5377
5378 tmp = ret;
5379 ret = gst_caps_intersect_full(filter, ret, GST_CAPS_INTERSECT_FIRST);
5380 gst_caps_unref(tmp);
5381 }
5382
xuesong.jiang22a9b112023-05-24 09:01:59 +00005383 if (v4l2object->stream_mode)
5384 {
5385 GST_INFO_OBJECT(v4l2object->dbg_obj, "ret caps: %" GST_PTR_FORMAT, ret);
5386 for (guint i = 0; i < gst_caps_get_size(ret); i++)
5387 {
5388 GstStructure *s = gst_caps_get_structure(ret, i);
5389 if (s)
5390 gst_structure_remove_field(s, "alignment");
5391
5392 GST_DEBUG("i:%d, s:%p", i, s);
5393 }
5394 GST_INFO_OBJECT(v4l2object->dbg_obj, "new ret caps: %" GST_PTR_FORMAT, ret);
5395 }
5396
xuesong.jiangae1548e2022-05-06 16:38:46 +08005397 GST_INFO_OBJECT(v4l2object->dbg_obj, "probed caps: %" GST_PTR_FORMAT, ret);
5398
5399 return ret;
5400}
5401
5402GstCaps *
5403gst_aml_v4l2_object_get_caps(GstAmlV4l2Object *v4l2object, GstCaps *filter)
5404{
5405 GstCaps *ret;
5406
5407 if (v4l2object->probed_caps == NULL)
5408 v4l2object->probed_caps = gst_aml_v4l2_object_probe_caps(v4l2object, NULL);
5409
5410 if (filter)
5411 {
5412 ret = gst_caps_intersect_full(filter, v4l2object->probed_caps,
5413 GST_CAPS_INTERSECT_FIRST);
5414 }
5415 else
5416 {
5417 ret = gst_caps_ref(v4l2object->probed_caps);
5418 }
5419
5420 return ret;
5421}
5422
5423gboolean
5424gst_aml_v4l2_object_decide_allocation(GstAmlV4l2Object *obj, GstQuery *query)
5425{
5426 GstCaps *caps;
5427 GstBufferPool *pool = NULL, *other_pool = NULL;
5428 GstStructure *config;
5429 guint size, min, max, own_min = 0;
5430 gboolean update;
5431 gboolean has_video_meta;
5432 gboolean can_share_own_pool, pushing_from_our_pool = FALSE;
5433 GstAllocator *allocator = NULL;
5434 GstAllocationParams params = {0};
5435
5436 GST_DEBUG_OBJECT(obj->dbg_obj, "decide allocation");
5437
5438 g_return_val_if_fail(obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE ||
5439 obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE,
5440 FALSE);
5441
5442 gst_query_parse_allocation(query, &caps, NULL);
5443
5444 if (obj->pool == NULL)
5445 {
5446 if (!gst_aml_v4l2_object_setup_pool(obj, caps))
5447 goto pool_failed;
5448 }
5449
5450 if (gst_query_get_n_allocation_params(query) > 0)
5451 gst_query_parse_nth_allocation_param(query, 0, &allocator, &params);
5452
5453 if (gst_query_get_n_allocation_pools(query) > 0)
5454 {
5455 gst_query_parse_nth_allocation_pool(query, 0, &pool, &size, &min, &max);
5456 update = TRUE;
5457 }
5458 else
5459 {
5460 pool = NULL;
5461 min = max = 0;
5462 size = 0;
5463 update = FALSE;
5464 }
5465
5466 GST_DEBUG_OBJECT(obj->dbg_obj, "allocation: size:%u min:%u max:%u pool:%" GST_PTR_FORMAT, size, min, max, pool);
5467
5468 has_video_meta =
5469 gst_query_find_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
5470
5471 can_share_own_pool = (has_video_meta || !obj->need_video_meta);
5472
5473 gst_aml_v4l2_get_driver_min_buffers(obj);
5474 /* We can't share our own pool, if it exceed V4L2 capacity */
5475 if (min + obj->min_buffers + 1 > VIDEO_MAX_FRAME)
5476 can_share_own_pool = FALSE;
5477
5478 /* select a pool */
5479 switch (obj->mode)
5480 {
5481 case GST_V4L2_IO_RW:
5482 if (pool)
5483 {
5484 /* in READ/WRITE mode, prefer a downstream pool because our own pool
5485 * doesn't help much, we have to write to it as well */
5486 GST_DEBUG_OBJECT(obj->dbg_obj,
5487 "read/write mode: using downstream pool");
5488 /* use the bigest size, when we use our own pool we can't really do any
5489 * other size than what the hardware gives us but for downstream pools
5490 * we can try */
5491 size = MAX(size, obj->info.size);
5492 }
5493 else if (can_share_own_pool)
5494 {
5495 /* no downstream pool, use our own then */
5496 GST_DEBUG_OBJECT(obj->dbg_obj,
5497 "read/write mode: no downstream pool, using our own");
5498 pool = gst_object_ref(obj->pool);
5499 size = obj->info.size;
5500 pushing_from_our_pool = TRUE;
5501 }
5502 break;
5503
5504 case GST_V4L2_IO_USERPTR:
5505 case GST_V4L2_IO_DMABUF_IMPORT:
5506 /* in importing mode, prefer our own pool, and pass the other pool to
5507 * our own, so it can serve itself */
5508 if (pool == NULL)
5509 goto no_downstream_pool;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005510 gst_aml_v4l2_buffer_pool_set_other_pool(GST_AML_V4L2_BUFFER_POOL(obj->pool), pool);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005511 other_pool = pool;
5512 gst_object_unref(pool);
5513 pool = gst_object_ref(obj->pool);
5514 size = obj->info.size;
5515 break;
5516
5517 case GST_V4L2_IO_MMAP:
5518 case GST_V4L2_IO_DMABUF:
5519 /* in streaming mode, prefer our own pool */
5520 /* Check if we can use it ... */
5521 if (can_share_own_pool)
5522 {
5523 if (pool)
5524 gst_object_unref(pool);
5525 pool = gst_object_ref(obj->pool);
5526 size = obj->info.size;
5527 GST_DEBUG_OBJECT(obj->dbg_obj,
5528 "streaming mode: using our own pool %" GST_PTR_FORMAT, pool);
5529 pushing_from_our_pool = TRUE;
5530 }
5531 else if (pool)
5532 {
5533 GST_DEBUG_OBJECT(obj->dbg_obj,
5534 "streaming mode: copying to downstream pool %" GST_PTR_FORMAT,
5535 pool);
5536 }
5537 else
5538 {
5539 GST_DEBUG_OBJECT(obj->dbg_obj,
5540 "streaming mode: no usable pool, copying to generic pool");
5541 size = MAX(size, obj->info.size);
5542 }
5543 break;
5544 case GST_V4L2_IO_AUTO:
5545 default:
5546 GST_WARNING_OBJECT(obj->dbg_obj, "unhandled mode");
5547 break;
5548 }
5549
5550 if (size == 0)
5551 goto no_size;
5552
5553 /* If pushing from our own pool, configure it with queried minimum,
5554 * otherwise use the minimum required */
5555 if (pushing_from_our_pool)
5556 {
5557 /* When pushing from our own pool, we need what downstream one, to be able
5558 * to fill the pipeline, the minimum required to decoder according to the
5559 * driver and 2 more, so we don't endup up with everything downstream or
5560 * held by the decoder. We account 2 buffers for v4l2 so when one is being
5561 * pushed downstream the other one can already be queued for the next
5562 * frame. */
5563 own_min = min + obj->min_buffers + 2;
5564
5565 /* If no allocation parameters where provided, allow for a little more
5566 * buffers and enable copy threshold */
5567 if (!update)
5568 {
5569 own_min += 2;
5570 gst_aml_v4l2_buffer_pool_copy_at_threshold(GST_AML_V4L2_BUFFER_POOL(pool),
5571 TRUE);
5572 }
5573 else
5574 {
5575 gst_aml_v4l2_buffer_pool_copy_at_threshold(GST_AML_V4L2_BUFFER_POOL(pool),
5576 FALSE);
5577 }
5578 }
5579 else
5580 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005581 min = obj->min_buffers;
5582 max = min;
xuesong.jiangae1548e2022-05-06 16:38:46 +08005583 }
5584
5585 /* Request a bigger max, if one was suggested but it's too small */
5586 if (max != 0)
5587 max = MAX(min, max);
5588
5589 /* First step, configure our own pool */
5590 config = gst_buffer_pool_get_config(obj->pool);
5591
5592 if (obj->need_video_meta || has_video_meta)
5593 {
5594 GST_DEBUG_OBJECT(obj->dbg_obj, "activate Video Meta");
5595 gst_buffer_pool_config_add_option(config,
5596 GST_BUFFER_POOL_OPTION_VIDEO_META);
5597 }
5598
5599 gst_buffer_pool_config_set_allocator(config, allocator, &params);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005600 gst_buffer_pool_config_set_params(config, caps, size, min, max);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005601
5602 GST_DEBUG_OBJECT(obj->dbg_obj, "setting own pool config to %" GST_PTR_FORMAT, config);
5603
5604 /* Our pool often need to adjust the value */
5605 if (!gst_buffer_pool_set_config(obj->pool, config))
5606 {
5607 config = gst_buffer_pool_get_config(obj->pool);
5608
5609 GST_DEBUG_OBJECT(obj->dbg_obj, "own pool config changed to %" GST_PTR_FORMAT, config);
5610
5611 /* our pool will adjust the maximum buffer, which we are fine with */
5612 if (!gst_buffer_pool_set_config(obj->pool, config))
5613 goto config_failed;
5614 }
5615
5616 /* Now configure the other pool if different */
5617 if (obj->pool != pool)
5618 other_pool = pool;
5619
5620 if (other_pool)
5621 {
5622 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)obj->element;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005623 guint other_min = min;
5624 guint other_max = max;
5625
5626 if (obj->old_other_pool || obj->old_old_other_pool) //jxsdbg for switching
5627 {
5628 obj->outstanding_buf_num = gst_aml_v4l2_object_get_outstanding_capture_buf_num(obj);
fei.deng7f79b0e2024-07-26 15:18:22 +08005629 if (obj->outstanding_buf_num > 0) {
hanghang.luodc626902024-08-23 14:59:27 +08005630 if (obj->outstanding_buf_num >= obj->min_buffers)
5631 {
5632 other_min = 1;
5633 }
5634 else
5635 {
5636 other_min = other_max = obj->min_buffers - obj->outstanding_buf_num;
5637 }
fei.deng7f79b0e2024-07-26 15:18:22 +08005638 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005639 GST_DEBUG_OBJECT(obj, "oop:%p, ooop:%p, outstanding buf num:%d, set min, max to %d,%d",
5640 obj->old_other_pool, obj->old_old_other_pool,
5641 obj->outstanding_buf_num, other_min, other_max);
5642 }
5643
xuesong.jiangae1548e2022-05-06 16:38:46 +08005644 if (self->is_secure_path)
5645 {
5646 params.flags |= GST_MEMORY_FLAG_LAST << 1; // in drmallocator GST_MEMORY_FLAG_LAST << 1 represent GST_MEMORY_FLAG_SECURE
5647 GST_DEBUG_OBJECT(obj, "set secure flag for drmbufferpool flag:0x%x", params.flags);
5648 }
5649 config = gst_buffer_pool_get_config(other_pool);
5650 gst_buffer_pool_config_set_allocator(config, allocator, &params);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08005651 gst_buffer_pool_config_set_params (config, caps, size, other_min, other_max);
xuesong.jiangae1548e2022-05-06 16:38:46 +08005652 gst_buffer_pool_config_set_video_alignment(config, &obj->align);
5653
5654 GST_DEBUG_OBJECT(obj->dbg_obj, "setting other pool config to %" GST_PTR_FORMAT, config);
5655
5656 /* if downstream supports video metadata, add this to the pool config */
5657 if (has_video_meta)
5658 {
5659 GST_DEBUG_OBJECT(obj->dbg_obj, "activate Video Meta");
5660 gst_buffer_pool_config_add_option(config,
5661 GST_BUFFER_POOL_OPTION_VIDEO_META);
5662 }
5663
5664 if (!gst_buffer_pool_set_config(other_pool, config))
5665 {
5666 config = gst_buffer_pool_get_config(other_pool);
5667
5668 if (!gst_buffer_pool_config_validate_params(config, caps, size, min,
5669 max))
5670 {
5671 gst_structure_free(config);
5672 goto config_failed;
5673 }
5674
5675 if (!gst_buffer_pool_set_config(other_pool, config))
5676 goto config_failed;
5677 }
5678 }
5679
5680 if (pool)
5681 {
5682 /* For simplicity, simply read back the active configuration, so our base
5683 * class get the right information */
5684 config = gst_buffer_pool_get_config(pool);
5685 gst_buffer_pool_config_get_params(config, NULL, &size, &min, &max);
5686 gst_structure_free(config);
5687 }
5688
5689 if (update)
5690 gst_query_set_nth_allocation_pool(query, 0, pool, size, min, max);
5691 else
5692 gst_query_add_allocation_pool(query, pool, size, min, max);
5693
5694 if (allocator)
5695 gst_object_unref(allocator);
5696
5697 if (pool)
5698 gst_object_unref(pool);
5699
5700 return TRUE;
5701
5702pool_failed:
5703{
5704 /* setup_pool already send the error */
5705 goto cleanup;
5706}
5707config_failed:
5708{
5709 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5710 (_("Failed to configure internal buffer pool.")), (NULL));
5711 goto cleanup;
5712}
5713no_size:
5714{
5715 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5716 (_("Video device did not suggest any buffer size.")), (NULL));
5717 goto cleanup;
5718}
5719cleanup:
5720{
5721 if (allocator)
5722 gst_object_unref(allocator);
5723
5724 if (pool)
5725 gst_object_unref(pool);
5726 return FALSE;
5727}
5728no_downstream_pool:
5729{
5730 GST_ELEMENT_ERROR(obj->element, RESOURCE, SETTINGS,
5731 (_("No downstream pool to import from.")),
5732 ("When importing DMABUF or USERPTR, we need a pool to import from"));
5733 return FALSE;
5734}
5735}
5736
5737gboolean
5738gst_aml_v4l2_object_propose_allocation(GstAmlV4l2Object *obj, GstQuery *query)
5739{
5740 GstBufferPool *pool;
5741 /* we need at least 2 buffers to operate */
5742 guint size, min, max;
5743 GstCaps *caps;
5744 gboolean need_pool;
5745
5746 /* Set defaults allocation parameters */
5747 size = obj->info.size;
5748 min = GST_AML_V4L2_MIN_BUFFERS;
5749 max = VIDEO_MAX_FRAME;
5750
5751 gst_query_parse_allocation(query, &caps, &need_pool);
5752
5753 if (caps == NULL)
5754 goto no_caps;
5755
5756 switch (obj->mode)
5757 {
5758 case GST_V4L2_IO_MMAP:
5759 case GST_V4L2_IO_DMABUF:
5760 if ((pool = obj->pool))
5761 gst_object_ref(pool);
5762 break;
5763 default:
5764 pool = NULL;
5765 break;
5766 }
5767
5768 if (pool != NULL)
5769 {
5770 GstCaps *pcaps;
5771 GstStructure *config;
5772
5773 /* we had a pool, check caps */
5774 config = gst_buffer_pool_get_config(pool);
5775 gst_buffer_pool_config_get_params(config, &pcaps, NULL, NULL, NULL);
5776
5777 GST_DEBUG_OBJECT(obj->dbg_obj,
5778 "we had a pool with caps %" GST_PTR_FORMAT, pcaps);
5779 if (!gst_caps_is_equal(caps, pcaps))
5780 {
5781 gst_structure_free(config);
5782 gst_object_unref(pool);
5783 goto different_caps;
5784 }
5785 gst_structure_free(config);
5786 }
5787 gst_aml_v4l2_get_driver_min_buffers(obj);
5788
5789 min = MAX(obj->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
5790
5791 gst_query_add_allocation_pool(query, pool, size, min, max);
5792
5793 /* we also support various metadata */
5794 gst_query_add_allocation_meta(query, GST_VIDEO_META_API_TYPE, NULL);
5795
5796 if (pool)
5797 gst_object_unref(pool);
5798
5799 return TRUE;
5800
5801 /* ERRORS */
5802no_caps:
5803{
5804 GST_DEBUG_OBJECT(obj->dbg_obj, "no caps specified");
5805 return FALSE;
5806}
5807different_caps:
5808{
5809 /* different caps, we can't use this pool */
5810 GST_DEBUG_OBJECT(obj->dbg_obj, "pool has different caps");
5811 return FALSE;
5812}
5813}
5814
5815gboolean
5816gst_aml_v4l2_object_try_import(GstAmlV4l2Object *obj, GstBuffer *buffer)
5817{
5818 GstVideoMeta *vmeta;
5819 guint n_mem = gst_buffer_n_memory(buffer);
5820
5821 /* only import if requested */
5822 switch (obj->mode)
5823 {
5824 case GST_V4L2_IO_USERPTR:
5825 case GST_V4L2_IO_DMABUF_IMPORT:
5826 break;
5827 default:
5828 GST_DEBUG_OBJECT(obj->dbg_obj,
5829 "The io-mode does not enable importation");
5830 return FALSE;
5831 }
5832
5833 vmeta = gst_buffer_get_video_meta(buffer);
5834 if (!vmeta && obj->need_video_meta)
5835 {
5836 GST_DEBUG_OBJECT(obj->dbg_obj, "Downstream buffer uses standard "
5837 "stride/offset while the driver does not.");
5838 return FALSE;
5839 }
5840
5841 /* we need matching strides/offsets and size */
5842 if (vmeta)
5843 {
5844 guint p;
5845 gboolean need_fmt_update = FALSE;
5846
5847 if (vmeta->n_planes != GST_VIDEO_INFO_N_PLANES(&obj->info))
5848 {
5849 GST_WARNING_OBJECT(obj->dbg_obj,
5850 "Cannot import buffers with different number planes");
5851 return FALSE;
5852 }
5853
5854 for (p = 0; p < vmeta->n_planes; p++)
5855 {
5856 if (vmeta->stride[p] < obj->info.stride[p])
5857 {
5858 GST_DEBUG_OBJECT(obj->dbg_obj,
5859 "Not importing as remote stride %i is smaller then %i on plane %u",
5860 vmeta->stride[p], obj->info.stride[p], p);
5861 return FALSE;
5862 }
5863 else if (vmeta->stride[p] > obj->info.stride[p])
5864 {
5865 need_fmt_update = TRUE;
5866 }
5867
5868 if (vmeta->offset[p] < obj->info.offset[p])
5869 {
5870 GST_DEBUG_OBJECT(obj->dbg_obj,
5871 "Not importing as offset %" G_GSIZE_FORMAT
5872 " is smaller then %" G_GSIZE_FORMAT " on plane %u",
5873 vmeta->offset[p], obj->info.offset[p], p);
5874 return FALSE;
5875 }
5876 else if (vmeta->offset[p] > obj->info.offset[p])
5877 {
5878 need_fmt_update = TRUE;
5879 }
5880 }
5881
5882 if (need_fmt_update)
5883 {
5884 struct v4l2_format format;
5885 gint wanted_stride[GST_VIDEO_MAX_PLANES] = {
5886 0,
5887 };
5888
5889 format = obj->format;
5890
5891 /* update the current format with the stride we want to import from */
5892 if (V4L2_TYPE_IS_MULTIPLANAR(obj->type))
5893 {
5894 guint i;
5895
5896 GST_DEBUG_OBJECT(obj->dbg_obj, "Wanted strides:");
5897
5898 for (i = 0; i < obj->n_v4l2_planes; i++)
5899 {
5900 gint stride = vmeta->stride[i];
5901
5902 if (GST_VIDEO_FORMAT_INFO_IS_TILED(obj->info.finfo))
5903 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(obj->info.finfo);
5904
5905 format.fmt.pix_mp.plane_fmt[i].bytesperline = stride;
5906 wanted_stride[i] = stride;
5907 GST_DEBUG_OBJECT(obj->dbg_obj, " [%u] %i", i, wanted_stride[i]);
5908 }
5909 }
5910 else
5911 {
5912 gint stride = vmeta->stride[0];
5913
5914 GST_DEBUG_OBJECT(obj->dbg_obj, "Wanted stride: %i", stride);
5915
5916 if (GST_VIDEO_FORMAT_INFO_IS_TILED(obj->info.finfo))
5917 stride = GST_VIDEO_TILE_X_TILES(stride) << GST_VIDEO_FORMAT_INFO_TILE_WS(obj->info.finfo);
5918
5919 format.fmt.pix.bytesperline = stride;
5920 wanted_stride[0] = stride;
5921 }
5922
5923 if (obj->ioctl(obj->video_fd, VIDIOC_S_FMT, &format) < 0)
5924 {
5925 GST_WARNING_OBJECT(obj->dbg_obj,
5926 "Something went wrong trying to update current format: %s",
5927 g_strerror(errno));
5928 return FALSE;
5929 }
5930
5931 gst_aml_v4l2_object_save_format(obj, obj->fmtdesc, &format, &obj->info,
5932 &obj->align);
5933
5934 if (V4L2_TYPE_IS_MULTIPLANAR(obj->type))
5935 {
5936 guint i;
5937
5938 for (i = 0; i < obj->n_v4l2_planes; i++)
5939 {
5940 if (format.fmt.pix_mp.plane_fmt[i].bytesperline != wanted_stride[i])
5941 {
5942 GST_DEBUG_OBJECT(obj->dbg_obj,
5943 "[%i] Driver did not accept the new stride (wants %i, got %i)",
5944 i, format.fmt.pix_mp.plane_fmt[i].bytesperline,
5945 wanted_stride[i]);
5946 return FALSE;
5947 }
5948 }
5949 }
5950 else
5951 {
5952 if (format.fmt.pix.bytesperline != wanted_stride[0])
5953 {
5954 GST_DEBUG_OBJECT(obj->dbg_obj,
5955 "Driver did not accept the new stride (wants %i, got %i)",
5956 format.fmt.pix.bytesperline, wanted_stride[0]);
5957 return FALSE;
5958 }
5959 }
5960 }
5961 }
5962
5963 /* we can always import single memory buffer, but otherwise we need the same
5964 * amount of memory object. */
5965 if (n_mem != 1 && n_mem != obj->n_v4l2_planes)
5966 {
5967 GST_DEBUG_OBJECT(obj->dbg_obj, "Can only import %i memory, "
5968 "buffers contains %u memory",
5969 obj->n_v4l2_planes, n_mem);
5970 return FALSE;
5971 }
5972
5973 /* For DMABuf importation we need DMABuf of course */
5974 if (obj->mode == GST_V4L2_IO_DMABUF_IMPORT)
5975 {
5976 guint i;
5977
5978 for (i = 0; i < n_mem; i++)
5979 {
5980 GstMemory *mem = gst_buffer_peek_memory(buffer, i);
5981
5982 if (!gst_is_dmabuf_memory(mem))
5983 {
5984 GST_DEBUG_OBJECT(obj->dbg_obj, "Cannot import non-DMABuf memory.");
5985 return FALSE;
5986 }
5987 }
5988 }
5989
5990 /* for the remaining, only the kernel driver can tell */
5991 return TRUE;
5992}
5993
xuesong.jiang22a9b112023-05-24 09:01:59 +00005994static gboolean gst_aml_v4l2_set_control(GstAmlV4l2Object *v4l2object, guint ctl)
5995{
5996 int rc;
5997 struct v4l2_queryctrl queryctrl;
5998 struct v4l2_control control;
5999
xuesong.jiang22a9b112023-05-24 09:01:59 +00006000 memset(&queryctrl, 0, sizeof(queryctrl));
6001 queryctrl.id = ctl;
6002
6003 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_QUERYCTRL, &queryctrl);
6004 if (rc == 0)
6005 {
6006 if (!(queryctrl.flags & V4L2_CTRL_FLAG_DISABLED))
6007 {
6008 memset(&control, 0, sizeof(control));
6009 control.id = ctl;
6010 control.value = 1;
6011 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_CTRL, &control);
6012 if (rc != 0)
6013 {
6014 GST_ERROR_OBJECT(v4l2object->dbg_obj, "set ctl:0x%x fail rc %d", ctl, rc);
6015 return FALSE;
6016 }
6017 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "set ctl:0x%x succ", ctl);
6018 return TRUE;
6019 }
6020 else
6021 {
6022 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "ctl:0x%x is disabled", ctl);
6023 return TRUE;
6024 }
6025 }
6026 else
6027 {
6028 GST_ERROR_OBJECT(v4l2object->dbg_obj, "VIDIOC_QUERYCTRL for 0x:%x fail", ctl);
6029 return FALSE;
6030 }
6031}
6032
hanghang.luo7f403102024-07-04 10:33:01 +08006033gboolean gst_aml_v4l2_set_I_frame_mode(GstAmlV4l2Object *v4l2object)
6034{
6035 if (v4l2object->iframe_mode)
6036 {
6037 int rc;
6038 struct v4l2_control control;
6039 memset(&control, 0, sizeof(control));
6040 control.id = AML_V4L2_SET_I_FRAME;
6041 control.value = 1;
6042 rc = v4l2object->ioctl(v4l2object->video_fd, VIDIOC_S_CTRL, &control);
6043 if (rc != 0)
6044 {
6045 GST_ERROR_OBJECT(v4l2object->dbg_obj, "rc: %d", rc);
6046 return FALSE;
6047 }
6048 }
hanghang.luoac6df002024-07-11 10:54:17 +08006049 GST_DEBUG("set I frame ok");
hanghang.luo7f403102024-07-04 10:33:01 +08006050 return TRUE;
6051}
xuesong.jiang22a9b112023-05-24 09:01:59 +00006052
xuesong.jiangae1548e2022-05-06 16:38:46 +08006053gboolean gst_aml_v4l2_set_drm_mode(GstAmlV4l2Object *v4l2object)
6054{
6055 /* On AmLogic, output obj use of GST_V4L2_IO_DMABUF_IMPORT implies secure memory */
xuesong.jiang26749f32024-07-24 09:40:47 +08006056 if (v4l2object->req_mode == GST_V4L2_IO_DMABUF_IMPORT && v4l2object->secure_es)
xuesong.jiangae1548e2022-05-06 16:38:46 +08006057 {
xuesong.jiang26749f32024-07-24 09:40:47 +08006058 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)v4l2object->element;
6059 self->is_secure_path = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08006060
xuesong.jiang22a9b112023-05-24 09:01:59 +00006061 if (gst_aml_v4l2_set_control(v4l2object, AML_V4L2_SET_DRMMODE))
xuesong.jiangae1548e2022-05-06 16:38:46 +08006062 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00006063 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_DRMMODE set succ");
6064 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08006065 }
6066 else
6067 {
xuesong.jiang22a9b112023-05-24 09:01:59 +00006068 GST_ERROR_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_DRMMODE set fail");
xuesong.jiangae1548e2022-05-06 16:38:46 +08006069 return FALSE;
6070 }
6071 }
xuesong.jiang26749f32024-07-24 09:40:47 +08006072 return TRUE;
xuesong.jiang22a9b112023-05-24 09:01:59 +00006073}
6074
6075gboolean gst_aml_v4l2_set_stream_mode(GstAmlV4l2Object *v4l2object)
6076{
6077 if (v4l2object->stream_mode)
6078 {
6079 if (gst_aml_v4l2_set_control(v4l2object, AML_V4L2_SET_STREAM_MODE))
6080 {
6081 GST_DEBUG_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_STREAM_MODE set succ");
6082 return TRUE;
6083 }
6084 else
6085 {
6086 GST_ERROR_OBJECT(v4l2object->dbg_obj, "AML_V4L2_SET_STREAM_MODE set fail");
6087 return FALSE;
6088 }
6089 }
6090 else
6091 {
6092 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 +08006093 return TRUE;
6094 }
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08006095}
6096
6097gint gst_aml_v4l2_object_get_outstanding_capture_buf_num(GstAmlV4l2Object *obj)
6098{
6099 gint ret = 0;
6100 gint count = 0;
6101
6102 if (obj->old_other_pool)
6103 {
6104 count = gst_buffer_pool_get_outstanding_num(obj->old_other_pool);
6105 if (count)
6106 {
6107 ret += count;
6108 }
6109 else
6110 {
6111 gst_object_unref(obj->old_other_pool);
6112 obj->old_other_pool = NULL;
6113 }
6114 }
6115
6116 count = 0;
6117 if (obj->old_old_other_pool)
6118 {
6119 count = gst_buffer_pool_get_outstanding_num(obj->old_old_other_pool);
6120 if (count)
6121 {
6122 ret += count;
6123 }
6124 else
6125 {
6126 gst_object_unref(obj->old_old_other_pool);
6127 obj->old_old_other_pool = NULL;
6128 }
6129 }
6130
6131 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08006132}