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