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