blob: f4762305d8aca6064049a6b4e9fcc1e100fc2958 [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 <unistd.h>
28#include <string.h>
29
30#include "gstamlv4l2object.h"
31#include "gstamlv4l2videodec.h"
32
33#include <string.h>
34#include <gst/gst-i18n-plugin.h>
35#include <gst/allocators/gstdmabuf.h>
36
37GST_DEBUG_CATEGORY_STATIC(gst_aml_v4l2_video_dec_debug);
38#define GST_CAT_DEFAULT gst_aml_v4l2_video_dec_debug
39
40#ifdef GST_VIDEO_DECODER_STREAM_LOCK
41#undef GST_VIDEO_DECODER_STREAM_LOCK
42#define GST_VIDEO_DECODER_STREAM_LOCK(decoder) \
43 { \
fei.denge9458472023-04-18 02:05:48 +000044 GST_TRACE("aml v4l2 dec locking"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080045 g_rec_mutex_lock(&GST_VIDEO_DECODER(decoder)->stream_lock); \
fei.denge9458472023-04-18 02:05:48 +000046 GST_TRACE("aml v4l2 dec locked"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080047 }
48#endif
49
50#ifdef GST_VIDEO_DECODER_STREAM_UNLOCK
51#undef GST_VIDEO_DECODER_STREAM_UNLOCK
52#define GST_VIDEO_DECODER_STREAM_UNLOCK(decoder) \
53 { \
fei.denge9458472023-04-18 02:05:48 +000054 GST_TRACE("aml v4l2 dec unlocking"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080055 g_rec_mutex_unlock(&GST_VIDEO_DECODER(decoder)->stream_lock); \
fei.denge9458472023-04-18 02:05:48 +000056 GST_TRACE("aml v4l2 dec unlocked"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080057 }
58#endif
xuesong.jiang61ea8012022-05-12 15:38:17 +080059
zengliang.lidcd41462024-06-19 16:05:12 +080060#define GST_AML_V4L2_CC_IMPORT_QUARK gst_aml_v4l2_buffer_pool_cc_import_quark ()
61
hanghang.luo36df2852022-08-24 15:02:27 +080062#ifndef ABSDIFF
63#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
64#endif
65
xuesong.jiang61ea8012022-05-12 15:38:17 +080066#if GST_IMPORT_LGE_PROP
67typedef struct _GstAmlResourceInfo
68{
69 gchar *coretype;
70 gint videoport;
71 gint audioport;
72 gint maxwidth;
73 gint maxheight;
74 gint mixerport;
75} GstAmlResourceInfo;
76
77struct _GstAmlV4l2VideoDecLgeCtxt
78{
79 GstAmlResourceInfo res_info;
80 guint64 dec_size;
81 guint64 undec_size;
82 gchar *app_type;
83 gboolean clip_mode;
84};
85#endif
86
xuesong.jiangae1548e2022-05-06 16:38:46 +080087typedef struct
88{
89 gchar *device;
90 GstCaps *sink_caps;
91 GstCaps *src_caps;
92 const gchar *longname;
93 const gchar *description;
94} GstAmlV4l2VideoDecCData;
95
96enum
97{
98 PROP_0,
xuesong.jiang61ea8012022-05-12 15:38:17 +080099 V4L2_STD_OBJECT_PROPS,
100#if GST_IMPORT_LGE_PROP
101 LGE_RESOURCE_INFO,
102 LGE_DECODE_SIZE,
103 LGE_UNDECODE_SIZE,
104 LGE_APP_TYPE,
105 LGE_CLIP_MODE
106#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800107};
108
xuesong.jiang406ee302023-06-28 03:45:22 +0000109enum
110{
111 SIGNAL_DECODED_PTS,
112 MAX_SIGNAL
113};
114
115static guint g_signals[MAX_SIGNAL]= {0};
116
xuesong.jiangae1548e2022-05-06 16:38:46 +0800117#define gst_aml_v4l2_video_dec_parent_class parent_class
118G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec,
119 GST_TYPE_VIDEO_DECODER);
120
121static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder);
xuesong.jiang61ea8012022-05-12 15:38:17 +0800122#if GST_IMPORT_LGE_PROP
123static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class);
124#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800125
126static void
127gst_aml_v4l2_video_dec_set_property(GObject *object,
128 guint prop_id, const GValue *value, GParamSpec *pspec)
129{
130 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
131
132 switch (prop_id)
133 {
134 case PROP_CAPTURE_IO_MODE:
xuesong.jiangae1548e2022-05-06 16:38:46 +0800135 case PROP_DUMP_FRAME_LOCATION:
zengliang.lidcd41462024-06-19 16:05:12 +0800136 case PROP_CC_DATA:
137 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
138 prop_id, value, pspec))
139 {
140 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
141 }
142 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800143#if GST_IMPORT_LGE_PROP
144 case LGE_RESOURCE_INFO:
145 {
146 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
147 GstStructure *r_info = g_value_get_object(value);
148 if (r_info)
149 {
150 if (gst_structure_has_field(r_info, "coretype"))
151 {
152 if (self->lge_ctxt->res_info.coretype)
153 g_free(self->lge_ctxt->res_info.coretype);
154 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
155 }
156 if (gst_structure_has_field(r_info, "videoport"))
157 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
158 if (gst_structure_has_field(r_info, "audioport"))
159 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
160 if (gst_structure_has_field(r_info, "maxwidth"))
161 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
162 if (gst_structure_has_field(r_info, "maxheight"))
163 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
164 if (gst_structure_has_field(r_info, "mixerport"))
165 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
166 }
167 break;
168 }
169 case LGE_APP_TYPE:
170 {
171 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
172 if (self->lge_ctxt->app_type)
173 g_free(self->lge_ctxt->app_type);
174 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
175 break;
176 }
177 case LGE_CLIP_MODE:
178 {
179 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
180 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
181 break;
182 }
183#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800184 /* By default, only set on output */
185 default:
186 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output,
187 prop_id, value, pspec))
188 {
189 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
190 }
191 break;
192 }
193}
194
195static void
196gst_aml_v4l2_video_dec_get_property(GObject *object,
197 guint prop_id, GValue *value, GParamSpec *pspec)
198{
199 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
200
201 switch (prop_id)
202 {
203 case PROP_CAPTURE_IO_MODE:
zengliang.lidcd41462024-06-19 16:05:12 +0800204 case PROP_CC_DATA:
fei.dengaf682762024-06-24 19:06:03 +0800205 case PROP_DECODING_ERROR_FRAMES:
zengliang.lidcd41462024-06-19 16:05:12 +0800206 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
207 prop_id, value, pspec))
208 {
209 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
210 }
211 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800212#if GST_IMPORT_LGE_PROP
213 case LGE_DECODE_SIZE:
214 {
215 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
216 self->lge_ctxt->dec_size = -1;
217 g_value_set_int(value, self->lge_ctxt->dec_size);
218 break;
219 }
220 case LGE_UNDECODE_SIZE:
221 {
222 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
223 self->lge_ctxt->undec_size = -1;
224 g_value_set_int(value, self->lge_ctxt->undec_size);
225 break;
226 }
227#endif
228
xuesong.jiangae1548e2022-05-06 16:38:46 +0800229 /* By default read from output */
230 default:
231 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output,
232 prop_id, value, pspec))
233 {
234 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
235 }
236 break;
237 }
238}
239
240static gboolean
241gst_aml_v4l2_video_dec_open(GstVideoDecoder *decoder)
242{
243 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
244 GstCaps *codec_caps;
245
246 GST_DEBUG_OBJECT(self, "Opening");
247
248 if (!gst_aml_v4l2_object_open(self->v4l2output))
249 goto failure;
250
251 if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output))
252 goto failure;
253
254 codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad);
255 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output,
256 codec_caps);
257 gst_caps_unref(codec_caps);
258
259 if (gst_caps_is_empty(self->probed_sinkcaps))
260 goto no_encoded_format;
261
262 return TRUE;
263
264no_encoded_format:
265 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
266 (_("Decoder on device %s has no supported input format"),
267 self->v4l2output->videodev),
268 (NULL));
269 goto failure;
270
271failure:
272 if (GST_AML_V4L2_IS_OPEN(self->v4l2output))
273 gst_aml_v4l2_object_close(self->v4l2output);
274
275 if (GST_AML_V4L2_IS_OPEN(self->v4l2capture))
276 gst_aml_v4l2_object_close(self->v4l2capture);
277
278 gst_caps_replace(&self->probed_srccaps, NULL);
279 gst_caps_replace(&self->probed_sinkcaps, NULL);
280
281 return FALSE;
282}
283
284static gboolean
285gst_aml_v4l2_video_dec_close(GstVideoDecoder *decoder)
286{
287 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
288
289 GST_DEBUG_OBJECT(self, "Closing");
290
291 gst_aml_v4l2_object_close(self->v4l2output);
292 gst_aml_v4l2_object_close(self->v4l2capture);
293 gst_caps_replace(&self->probed_srccaps, NULL);
294 gst_caps_replace(&self->probed_sinkcaps, NULL);
295
296 return TRUE;
297}
298
299static gboolean
300gst_aml_v4l2_video_dec_start(GstVideoDecoder *decoder)
301{
302 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
303
304 GST_DEBUG_OBJECT(self, "Starting");
305
306 gst_aml_v4l2_object_unlock(self->v4l2output);
307 g_atomic_int_set(&self->active, TRUE);
308 self->output_flow = GST_FLOW_OK;
309
310 return TRUE;
311}
312
313static gboolean
314gst_aml_v4l2_video_dec_stop(GstVideoDecoder *decoder)
315{
316 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
317
318 GST_DEBUG_OBJECT(self, "Stopping");
319
320 gst_aml_v4l2_object_unlock(self->v4l2output);
321 gst_aml_v4l2_object_unlock(self->v4l2capture);
322
323 /* Wait for capture thread to stop */
324 gst_pad_stop_task(decoder->srcpad);
325
326 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
327 self->output_flow = GST_FLOW_OK;
328 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
329
330 /* Should have been flushed already */
331 g_assert(g_atomic_int_get(&self->active) == FALSE);
332
333 gst_aml_v4l2_object_stop(self->v4l2output);
334 gst_aml_v4l2_object_stop(self->v4l2capture);
335
336 if (self->input_state)
337 {
338 gst_video_codec_state_unref(self->input_state);
339 self->input_state = NULL;
340 }
341
342 GST_DEBUG_OBJECT(self, "Stopped");
343
344 return TRUE;
345}
346
347static gboolean
hanghang.luo8e1225b2023-10-10 08:54:28 +0000348gst_aml_v4l2_video_dec_codec_chg(GstVideoDecoder *decoder,
349 GstVideoCodecState *state)
350{
351 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
352 GstStructure *s_old = NULL;
353 GstStructure *s_new = NULL;
354
355 // first play, must set foramt;
356 if (!self->input_state)
357 return TRUE;
358
359 if (self->input_state->caps)
360 s_old = gst_caps_get_structure(self->input_state->caps,0);
361 if (state->caps)
362 s_new = gst_caps_get_structure(state->caps,0);
363
364 if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old)))
365 return TRUE;
366 return FALSE;
367}
368
369static gboolean
370gst_aml_v4l2_video_dec_res_chg(GstVideoDecoder *decoder,
371 GstVideoCodecState *state)
372{
373 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
374 gboolean ret = FALSE;
375 gint width_new = -1,height_new = -1,width_old = -1,height_old = -1;
376 GstStructure *s_old = NULL;
377 GstStructure *s_new = NULL;
378
379 // first play, must set foramt;
380 if (!self->input_state)
381 {
382 ret = TRUE;
383 goto done;
384 }
385
386 if (self->input_state->caps)
387 s_old = gst_caps_get_structure(self->input_state->caps,0);
388 if (state->caps)
389 s_new = gst_caps_get_structure(state->caps,0);
390
391 if (s_new && gst_structure_has_field(s_new,"width") && gst_structure_has_field(s_new,"height"))
392 {
393 gst_structure_get_int(s_new,"width",&width_new);
394 gst_structure_get_int(s_new,"height",&height_new);
395 }
396 if (s_old && gst_structure_has_field(s_old,"width") && gst_structure_has_field(s_old,"height"))
397 {
398 gst_structure_get_int(s_old,"width",&width_old);
399 gst_structure_get_int(s_old,"height",&height_old);
400 }
401
402 if (width_new != width_old || height_new != height_old)
403 ret = TRUE;
404
405done:
406 GST_DEBUG_OBJECT(self, "ret is %d",ret);
407 return ret;
408}
409
410static gboolean
xuesong.jiangae1548e2022-05-06 16:38:46 +0800411gst_aml_v4l2_video_dec_set_format(GstVideoDecoder *decoder,
412 GstVideoCodecState *state)
413{
414 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
415 gboolean ret = TRUE;
416 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
417 GstCaps *caps;
418
419 GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps);
hanghang.luo8e1225b2023-10-10 08:54:28 +0000420 if (self->input_state)
421 {
422 if (gst_aml_v4l2_video_dec_res_chg(decoder,state) || gst_aml_v4l2_video_dec_codec_chg(decoder,state))
423 GST_DEBUG_OBJECT(self, "resolution or codec changed");
424 else
425 goto done;
426 }
427
xuesong.jiangae1548e2022-05-06 16:38:46 +0800428 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
hanghang.luoc54208e2023-09-22 02:43:54 +0000429 GstStructure *s = gst_caps_get_structure(state->caps,0);
430 if (s && gst_structure_has_field(s,"format"))
431 {
432 if (!strcmp("XVID",gst_structure_get_string(s,"format")))
433 {
434 GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support");
435 ret = FALSE;
436 goto done;
437 }
438 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800439
440 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
441 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
442
443 if (self->input_state)
444 {
445 if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps))
446 {
447 GST_DEBUG_OBJECT(self, "Compatible caps");
448 goto done;
449 }
450 gst_video_codec_state_unref(self->input_state);
451 self->input_state = NULL;
452
453 gst_aml_v4l2_video_dec_finish(decoder);
454 gst_aml_v4l2_object_stop(self->v4l2output);
455
456 /* The renegotiation flow don't blend with the base class flow. To properly
457 * stop the capture pool, if the buffers can't be orphaned, we need to
458 * reclaim our buffers, which will happend through the allocation query.
459 * The allocation query is triggered by gst_video_decoder_negotiate() which
460 * requires the output caps to be set, but we can't know this information
461 * as we rely on the decoder, which requires the capture queue to be
462 * stopped.
463 *
464 * To workaround this issue, we simply run an allocation query with the
465 * old negotiated caps in order to drain/reclaim our buffers. That breaks
466 * the complexity and should not have much impact in performance since the
467 * following allocation query will happen on a drained pipeline and won't
468 * block. */
469 if (self->v4l2capture->pool &&
470 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
471 {
472 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
473 if (caps)
474 {
475 GstQuery *query = gst_query_new_allocation(caps, FALSE);
476 gst_pad_peer_query(decoder->srcpad, query);
477 gst_query_unref(query);
478 gst_caps_unref(caps);
479 }
480 }
481
482 gst_aml_v4l2_object_stop(self->v4l2capture);
483 self->output_flow = GST_FLOW_OK;
484 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800485 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
486 {
487 GST_ERROR_OBJECT(self, "config output drm mode error");
488 goto done;
489 }
490
xuesong.jiang22a9b112023-05-24 09:01:59 +0000491 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
492 {
493 GST_ERROR_OBJECT(self, "config output stream mode error");
494 goto done;
495 }
496
hanghang.luoac6df002024-07-11 10:54:17 +0800497 if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error))
498 {
499 GST_ERROR_OBJECT(self, "set format error");
500 goto done;
501 }
502
503 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
504 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
505 {
506 GST_ERROR_OBJECT(self, "config I frame mode error");
507 goto done;
508 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800509
510 gst_caps_replace(&self->probed_srccaps, NULL);
511 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
512 gst_aml_v4l2_object_get_raw_caps());
513
514 if (gst_caps_is_empty(self->probed_srccaps))
515 goto no_raw_format;
516
517 caps = gst_caps_copy(self->probed_srccaps);
518 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
519 gst_caps_append(self->probed_srccaps, caps);
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800520
xuesong.jiangae1548e2022-05-06 16:38:46 +0800521 if (ret)
522 self->input_state = gst_video_codec_state_ref(state);
523 else
524 gst_aml_v4l2_error(self, &error);
525
526done:
527 return ret;
528
529no_raw_format:
530 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
531 (_("Decoder on device %s has no supported output format"),
532 self->v4l2output->videodev),
533 (NULL));
534 return GST_FLOW_ERROR;
535}
536
537static gboolean
538gst_aml_v4l2_video_dec_flush(GstVideoDecoder *decoder)
539{
540 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
541
542 GST_DEBUG_OBJECT(self, "Flushed");
543
544 /* Ensure the processing thread has stopped for the reverse playback
545 * discount case */
546 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
547 {
548 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
549
550 gst_aml_v4l2_object_unlock(self->v4l2output);
551 gst_aml_v4l2_object_unlock(self->v4l2capture);
552 gst_pad_stop_task(decoder->srcpad);
553 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
554 }
555
556 self->output_flow = GST_FLOW_OK;
557
558 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
559 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
560
561 if (self->v4l2output->pool)
562 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
563
564 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
565 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
566 * pool. */
567 if (self->v4l2capture->pool)
568 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
569
570 return TRUE;
571}
572
573static gboolean
574gst_aml_v4l2_video_dec_negotiate(GstVideoDecoder *decoder)
575{
576 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
577
xuesong.jiang681d3602022-06-24 21:23:35 +0800578 if (TRUE == self->v4l2output->is_svp)
579 {
580 GstStructure *s;
581 GstEvent *event;
582
583 s = gst_structure_new_empty ("IS_SVP");
584 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
585 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
586 gst_pad_push_event (decoder->srcpad, event);
587 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
588 }
589
xuesong.jiangae1548e2022-05-06 16:38:46 +0800590 /* We don't allow renegotiation without carefull disabling the pool */
591 if (self->v4l2capture->pool &&
592 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
593 return TRUE;
594
595 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
596}
597
598static gboolean
599gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
600{
601 struct v4l2_decoder_cmd dcmd = {
602 0,
603 };
604
605 GST_DEBUG_OBJECT(v4l2object->element,
606 "sending v4l2 decoder command %u with flags %u", cmd, flags);
607
608 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
609 return FALSE;
610
611 dcmd.cmd = cmd;
612 dcmd.flags = flags;
613 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
614 goto dcmd_failed;
615
616 return TRUE;
617
618dcmd_failed:
619 if (errno == ENOTTY)
620 {
621 GST_INFO_OBJECT(v4l2object->element,
622 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
623 cmd, flags, v4l2object->videodev, g_strerror(errno));
624 }
625 else
626 {
627 GST_ERROR_OBJECT(v4l2object->element,
628 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
629 cmd, flags, v4l2object->videodev, g_strerror(errno));
630 }
631 return FALSE;
632}
633
634static GstFlowReturn
635gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
636{
637 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
638 GstFlowReturn ret = GST_FLOW_OK;
639 GstBuffer *buffer;
640
641 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
642 goto done;
643
644 GST_DEBUG_OBJECT(self, "Finishing decoding");
645
646 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
647
648 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
649 {
650 GstTask *task = decoder->srcpad->task;
651
652 /* If the decoder stop command succeeded, just wait until processing is
653 * finished */
654 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
655 GST_OBJECT_LOCK(task);
656 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
657 GST_TASK_WAIT(task);
658 GST_OBJECT_UNLOCK(task);
659 ret = GST_FLOW_FLUSHING;
660 }
661 else
662 {
663 /* otherwise keep queuing empty buffers until the processing thread has
664 * stopped, _pool_process() will return FLUSHING when that happened */
665 while (ret == GST_FLOW_OK)
666 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800667 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800668 buffer = gst_buffer_new();
669 ret =
670 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
671 gst_buffer_unref(buffer);
672 }
673 }
674
675 /* and ensure the processing thread has stopped in case another error
676 * occured. */
677 gst_aml_v4l2_object_unlock(self->v4l2capture);
678 gst_pad_stop_task(decoder->srcpad);
679 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
680
681 if (ret == GST_FLOW_FLUSHING)
682 ret = self->output_flow;
683
684 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
685
686 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
687
688done:
689 return ret;
690}
691
692static GstFlowReturn
693gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
694{
695 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
696
697 GST_DEBUG_OBJECT(self, "Draining...");
698 gst_aml_v4l2_video_dec_finish(decoder);
699 gst_aml_v4l2_video_dec_flush(decoder);
700
701 return GST_FLOW_OK;
702}
703
704static GstVideoCodecFrame *
705gst_aml_v4l2_video_dec_get_oldest_frame(GstVideoDecoder *decoder)
706{
707 GstVideoCodecFrame *frame = NULL;
708 GList *frames, *l;
709 gint count = 0;
710
711 frames = gst_video_decoder_get_frames(decoder);
712
713 for (l = frames; l != NULL; l = l->next)
714 {
715 GstVideoCodecFrame *f = l->data;
716
717 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
718 frame = f;
719
720 count++;
721 }
722
723 if (frame)
724 {
725 GST_LOG_OBJECT(decoder,
726 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
727 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
728 gst_video_codec_frame_ref(frame);
729 }
730
731 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
732
733 return frame;
734}
735
fei.dengbee20862022-06-14 14:59:48 +0800736static GstVideoCodecFrame *
xuesong.jiange24aef92023-06-16 06:39:10 +0000737gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800738{
739 GstVideoCodecFrame *frame = NULL;
740 GList *frames, *l;
741 gint count = 0;
742
xuesong.jiange24aef92023-06-16 06:39:10 +0000743 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
744
fei.dengbee20862022-06-14 14:59:48 +0800745 frames = gst_video_decoder_get_frames(decoder);
746
747 for (l = frames; l != NULL; l = l->next)
748 {
749 GstVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000750
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800751 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800752 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800753 }
fei.dengbee20862022-06-14 14:59:48 +0800754 count++;
755 }
756
zengliang.liddee2da2023-07-14 07:27:05 +0000757 if (!frame)
758 {
759 for (l = frames; l != NULL; l = l->next)
760 {
761 GstVideoCodecFrame *f = l->data;
762 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
763 {
764 frame = f;
765 }
766 GST_DEBUG("The pts of the expected output frame is invalid");
767 }
768 }
769
fei.dengbee20862022-06-14 14:59:48 +0800770 if (frame)
771 {
772 GST_LOG_OBJECT(decoder,
773 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
774 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
775 gst_video_codec_frame_ref(frame);
776 }
777
778 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
779
xuesong.jiange24aef92023-06-16 06:39:10 +0000780 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
fei.dengbee20862022-06-14 14:59:48 +0800781 return frame;
782}
783
xuesong.jiange24aef92023-06-16 06:39:10 +0000784static GstVideoCodecFrame *
785gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstVideoDecoder *decoder, GstClockTime pts)
786{
787 GstVideoCodecFrame *frame = NULL;
788 GList *frames, *l;
xuesong.jiange24aef92023-06-16 06:39:10 +0000789
790 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
791
792 frames = gst_video_decoder_get_frames(decoder);
793 guint frames_len = 0;
794 frames_len = g_list_length(frames);
795 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
796
797 frame = frames->data;
798
799 for (l = frames; l != NULL; l = l->next)
800 {
801 GstVideoCodecFrame *f = l->data;
802
803 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
804 {
805 /* found the right frame */
806 frame = f;
807 break;
808 }
809 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
810 {
811 GST_LOG_OBJECT(decoder,
812 "stream mode drop frame %d %" GST_TIME_FORMAT,
813 frame->system_frame_number, GST_TIME_ARGS(frame->pts));
814
815 gst_video_codec_frame_ref(f);
816 // gst_video_decoder_drop_frame(decoder, f);
817 gst_video_decoder_release_frame(decoder, f);
818 }
819 else
820 {
821 GST_LOG_OBJECT (decoder, "dbg");
822 }
823 }
824
825 if (frame)
826 {
827 guint l_len = 0;
828 l = gst_video_decoder_get_frames(decoder);
829 l_len = g_list_length(l);
830 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
831
832 GST_LOG_OBJECT(decoder,
833 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
834 frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
835 gst_video_codec_frame_ref(frame);
836 }
837
838 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
839
840 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
841 return frame;
842}
843
844static GstVideoCodecFrame *
845gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
846{
847 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
848 if (self->v4l2output->stream_mode)
849 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
850 else
851 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
852}
853
xuesong.jiangae1548e2022-05-06 16:38:46 +0800854static gboolean
855gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
856 GstStructure *structure, gpointer user_data)
857{
858 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
859 GstVideoAlignment *align = &self->v4l2capture->align;
860 GstVideoInfo *info = &self->v4l2capture->info;
861 int width, height;
862
863 if (!gst_structure_get_int(structure, "width", &width))
864 return TRUE;
865
866 if (!gst_structure_get_int(structure, "height", &height))
867 return TRUE;
868
869 if (align->padding_left != 0 || align->padding_top != 0 ||
870 height != info->height + align->padding_bottom)
871 return TRUE;
872
873 if (height == info->height + align->padding_bottom)
874 {
875 /* Some drivers may round up width to the padded with */
876 if (width == info->width + align->padding_right)
877 gst_structure_set(structure,
878 "width", G_TYPE_INT, width - align->padding_right,
879 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
880 /* Some drivers may keep visible width and only round up bytesperline */
881 else if (width == info->width)
882 gst_structure_set(structure,
883 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
884 }
885
886 return TRUE;
887}
888
889static void
sheng.liubcf036c2022-06-21 15:55:42 +0800890gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800891{
892 struct v4l2_event evt;
893 gint ret;
894
895 memset (&evt, 0x00, sizeof (struct v4l2_event));
896 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
897 if (ret < 0)
898 {
899 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
900 return;
901 }
902
903 switch (evt.type)
904 {
905 case V4L2_EVENT_SOURCE_CHANGE:
906 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
907 break;
908 case V4L2_EVENT_EOS:
909 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
910 break;
911 default:
912 break;
913 }
914
915 return;
916}
917
918static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000919gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
920{
921 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
922 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000923 struct v4l2_selection sel;
924 struct v4l2_rect *r = NULL;
925 GstStructure *s;
926 gint width = 0;
927 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800928 GST_DEBUG("%d %d",info.width, info.height);
hanghang.luo2eec4892023-07-18 06:44:42 +0000929 output_state = gst_video_decoder_set_output_state(decoder,
930 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000931 memset(&sel, 0, sizeof(struct v4l2_selection));
932 sel.type = self->v4l2capture->type;
933 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
934 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
935 {
936 r = &sel.r;
937 width = (r->width/2)*2;
938 height = (r->height/2)*2;
939 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
940 }
941 else
942 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000943 if (output_state)
944 {
945 output_state->info.interlace_mode = info.interlace_mode;
946 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000947 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000948 s = gst_caps_get_structure(output_state->caps, 0);
949 if (s)
950 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800951 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
952 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
953 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
954 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000955 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
956 gst_video_codec_state_unref(output_state);
957 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000958 }
959}
960
zengliang.lidcd41462024-06-19 16:05:12 +0800961static GQuark
962gst_aml_v4l2_buffer_pool_cc_import_quark (void)
963{
964 static GQuark quark = 0;
965
966 if (quark == 0)
967 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
968
969 return quark;
970}
971
972static gboolean
973foreach_cc_buffer_list_match_pts_func(GList *list , GstVideoCodecFrame *frame)
974{
975 GList *l;
976 if (g_list_length (list) > 0)
977 {
978 for (l = list; l != NULL; l = l->next)
979 {
980 GstBuffer *cc_buffer = l->data;
981 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
982 {
983 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
984 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
985 #if 0
986 //Debug code:dump cc data
987 GstMapInfo gst_map;
988 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
989 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
990 if (gst_map.size>0)
991 write(fd,gst_map.data,gst_map.size);
992 close(fd);
993 gst_buffer_unmap(cc_buffer,&gst_map);
994 #endif
995 GST_DEBUG("match success");
996 return TRUE;
997 }
998 else
999 {
1000 GST_DEBUG("match fail");
1001 }
1002 }
1003 GST_WARNING("no match frame in the bufferlist");
1004 }
1005 else
1006 {
1007 GST_WARNING("list is null,can not foreach");
1008 }
1009 return FALSE;
1010}
1011
hanghang.luo2eec4892023-07-18 06:44:42 +00001012static void
xuesong.jiangae1548e2022-05-06 16:38:46 +08001013gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
1014{
1015 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1016 GstAmlV4l2BufferPool *v4l2_pool;
1017 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1018 GstBufferPool *pool;
1019 GstVideoCodecFrame *frame;
1020 GstBuffer *buffer = NULL;
1021 GstFlowReturn ret;
1022
1023 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1024 {
1025 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001026 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001027 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001028 GST_DEBUG_OBJECT(self, "waitting source change event");
1029 /* Wait until received SOURCE_CHANGE event to get right video format */
1030 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1031 {
1032 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1033 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1034 {
fei.deng2a06e042023-10-10 03:09:45 +00001035 //let flush start event blocked until capture buffer pool actived
1036 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001037 GST_DEBUG_OBJECT(self, "Received source change event");
1038 break;
1039 }
1040 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1041 {
1042 GST_DEBUG_OBJECT(self, "Received eos event");
1043 goto beach;
1044 }
1045 else if (ret != GST_FLOW_OK)
1046 {
1047 GST_ERROR_OBJECT(self, "dqevent error");
1048 goto beach;
1049 }
1050 }
1051 self->v4l2capture->need_wait_event = FALSE;
1052
sheng.liu0c77f6c2022-06-17 21:33:20 +08001053 if (TRUE == self->v4l2output->is_svp)
1054 {
1055 GstPad *peer;
1056 GstStructure *s;
1057 GstEvent *event;
1058
1059 peer = gst_pad_get_peer (decoder->srcpad);
1060 if (peer)
1061 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001062 s = gst_structure_new_empty ("IS_SVP");
1063 if (s)
1064 {
1065 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1066 gst_pad_send_event (peer, event);
1067 GST_DEBUG_OBJECT(self, "Send SVP Event");
1068 }
1069 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001070 }
1071 }
1072
sheng.liub56bbc52022-06-21 11:02:33 +08001073 if (self->v4l2capture->need_drop_event)
1074 {
1075 // drop V4L2_EVENT_SOURCE_CHANGE
1076 gst_v4l2_drop_event(self->v4l2capture);
1077 self->v4l2capture->need_drop_event = FALSE;
1078 }
1079
xuesong.jiangae1548e2022-05-06 16:38:46 +08001080 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1081 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001082
xuesong.jiangae1548e2022-05-06 16:38:46 +08001083 /* Create caps from the acquired format, remove the format field */
1084 acquired_caps = gst_video_info_to_caps(&info);
1085 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1086 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001087 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1088
1089 /* Probe currently available pixel formats */
1090 available_caps = gst_caps_copy(self->probed_srccaps);
1091 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1092
1093 /* Replace coded size with visible size, we want to negotiate visible size
1094 * with downstream, not coded size. */
1095 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1096
1097 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001098 caps = gst_caps_copy(filter);
1099 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1100 gst_caps_append(filter, caps);
1101
1102 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1103 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001104 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001105 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1106 gst_caps_unref(filter);
1107
1108 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1109 if (gst_caps_is_empty(caps))
1110 {
1111 gst_caps_unref(caps);
1112 goto not_negotiated;
1113 }
1114
1115 /* Fixate pixel format */
1116 caps = gst_caps_fixate(caps);
1117
1118 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1119
1120 /* Try to set negotiated format, on success replace acquired format */
1121 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1122 gst_video_info_from_caps(&info, caps);
1123 else
1124 gst_aml_v4l2_clear_error(&error);
1125 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001126 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001127 if (!gst_video_decoder_negotiate(decoder))
1128 {
1129 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1130 goto flushing;
1131 else
1132 goto not_negotiated;
1133 }
1134
1135 /* Ensure our internal pool is activated */
1136 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1137 TRUE))
1138 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001139
1140 g_mutex_lock(&self->res_chg_lock);
1141 GST_LOG_OBJECT(decoder, "signal resolution changed");
1142 self->is_res_chg = FALSE;
1143 g_cond_signal(&self->res_chg_cond);
1144 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001145 }
1146
1147 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1148
1149 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1150
1151 self->output_flow = GST_FLOW_OK;
1152 do
1153 {
1154 /* We cannot use the base class allotate helper since it taking the internal
1155 * stream lock. we know that the acquire may need to poll until more frames
1156 * comes in and holding this lock would prevent that.
1157 */
1158 pool = gst_video_decoder_get_buffer_pool(decoder);
1159
1160 /* Pool may be NULL if we started going to READY state */
1161 if (pool == NULL)
1162 {
fei.dengbee20862022-06-14 14:59:48 +08001163 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001164 ret = GST_FLOW_FLUSHING;
1165 goto beach;
1166 }
1167
1168 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001169
fei.dengccc89632022-07-15 19:10:17 +08001170 //calculate a new pts for interlace stream
hanghang.luo8ec04e92023-10-09 08:14:24 +00001171 if (ret == GST_FLOW_OK && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
fei.dengccc89632022-07-15 19:10:17 +08001172 {
1173 //if buffer pts is valid, reduce 1/2 duration
1174 if (GST_BUFFER_DURATION_IS_VALID(buffer))
1175 {
1176 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
1177 }
1178 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1179 //reset pts
hanghang.luo6a5bdff2024-04-15 06:29:43 +00001180 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL)
fei.dengccc89632022-07-15 19:10:17 +08001181 {
1182 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
1183 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1184 }
1185 }
1186
xuesong.jiangae1548e2022-05-06 16:38:46 +08001187 g_object_unref(pool);
1188
fei.deng9a5cd6e2023-06-30 12:09:18 +00001189 if (ret == GST_FLOW_OK && GST_BUFFER_FLAG_IS_SET(buffer,GST_AML_V4L2_BUFFER_FLAG_LAST_EMPTY)) {
fei.deng990965a2023-11-15 07:03:15 +00001190 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1191 self->v4l2capture->need_drop_event = TRUE;
1192 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1193 if (self->is_res_chg) {
1194 //we must release last buffer
1195 gst_buffer_unref(buffer);
1196 //if resolution changed event received,we should set need_drop_event to false
1197 self->v4l2capture->need_drop_event = FALSE;
1198 gst_aml_v4l2_object_stop(self->v4l2capture);
1199 //unblock flush start event
1200 g_mutex_lock(&self->res_chg_lock);
1201 self->is_res_chg = FALSE;
1202 g_cond_signal(&self->res_chg_cond);
1203 g_mutex_unlock(&self->res_chg_lock);
1204 return;
1205 } else {
1206 goto beach;
1207 }
sheng.liub56bbc52022-06-21 11:02:33 +08001208 }
1209
zengliang.lidcd41462024-06-19 16:05:12 +08001210 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1211 {
1212 GST_DEBUG_OBJECT(decoder, "continue get cc data ");
1213 continue;
1214 }
1215
sheng.liu8d18ed22022-05-26 17:28:15 +08001216 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1217 {
1218 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001219
1220 g_mutex_lock (&self->res_chg_lock);
1221 self->is_res_chg = TRUE;
1222 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001223 return;
1224 }
fei.dengaf682762024-06-24 19:06:03 +08001225 //decoding error happened
1226 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1227 {
1228 continue;
1229 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001230
fei.dengbee20862022-06-14 14:59:48 +08001231 if (ret != GST_FLOW_OK) {
1232 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001233 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001234 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001235
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001236 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001237 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001238
1239 GST_DEBUG_OBJECT(decoder, "send pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1240 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1241
xuesong.jiangae1548e2022-05-06 16:38:46 +08001242 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1243 {
1244 gst_aml_v4l2_object_stop(self->v4l2capture);
1245 return;
1246 }
1247
fei.dengaf682762024-06-24 19:06:03 +08001248 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1249 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
1250 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001251
1252 if (ret != GST_FLOW_OK)
1253 goto beach;
1254
fei.dengbee20862022-06-14 14:59:48 +08001255 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001256 if (frame)
1257 {
zengliang.li32cb11e2022-11-24 12:10:26 +08001258 if (!GST_CLOCK_TIME_IS_VALID(frame->pts))
1259 {
zengliang.li92ff6822023-06-06 07:12:52 +00001260 if (!GST_CLOCK_TIME_IS_VALID(self->last_out_pts))
1261 {
1262 if (GST_CLOCK_TIME_IS_VALID(frame->dts))
1263 {
1264 GST_BUFFER_TIMESTAMP(buffer) = frame->dts;
1265 }
1266 else
1267 {
1268 GST_WARNING_OBJECT (decoder,"sorry,we have no baseline to calculate pts");
1269 goto beach;
1270 }
1271 }
1272 else
1273 {
1274 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d);
1275 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1276 }
zengliang.li32cb11e2022-11-24 12:10:26 +08001277 }
fei.dengccc89632022-07-15 19:10:17 +08001278 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001279 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001280 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1281 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001282 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001283
1284 GST_DEBUG_OBJECT (decoder,"enable_cc_data:%d",self->v4l2capture->enable_cc_data);
1285 if (self->v4l2capture->enable_cc_data)
1286 {
1287 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1288 {
1289 GST_DEBUG("cc buffer and frame bind success");
1290 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1291 GST_AML_V4L2_CC_IMPORT_QUARK);
1292 #if 0
1293 //Debug code:dump cc data
1294 GstMapInfo gst_map;
1295 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1296 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1297 if (gst_map.size>0)
1298 write(fd,gst_map.data,gst_map.size);
1299 close(fd);
1300 gst_buffer_unmap(cc_buffer,&gst_map);
1301 #endif
1302 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1303 gst_buffer_unref(cc_buffer);
1304 }
1305 else
1306 {
1307 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1308 }
1309 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001310 ret = gst_video_decoder_finish_frame(decoder, frame);
1311
1312 if (ret != GST_FLOW_OK)
1313 goto beach;
1314 }
1315 else
1316 {
1317 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
1318 gst_buffer_unref(buffer);
1319 }
1320
1321 return;
1322 /* ERRORS */
1323not_negotiated:
1324{
1325 GST_ERROR_OBJECT(self, "not negotiated");
1326 ret = GST_FLOW_NOT_NEGOTIATED;
1327 goto beach;
1328}
1329activate_failed:
1330{
1331 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1332 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1333 (_("Failed to allocate required memory.")),
1334 ("Buffer pool activation failed"));
1335 ret = GST_FLOW_ERROR;
1336 goto beach;
1337}
1338flushing:
1339{
1340 ret = GST_FLOW_FLUSHING;
1341 goto beach;
1342}
1343beach:
1344 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1345 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001346 if (self->is_res_chg) {
1347 //unblock flush start event
1348 g_mutex_lock(&self->res_chg_lock);
1349 self->is_res_chg = FALSE;
1350 g_cond_signal(&self->res_chg_cond);
1351 g_mutex_unlock(&self->res_chg_lock);
1352 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001353 gst_buffer_replace(&buffer, NULL);
1354 self->output_flow = ret;
1355 gst_aml_v4l2_object_unlock(self->v4l2output);
1356 gst_pad_pause_task(decoder->srcpad);
1357}
1358
1359static GstFlowReturn
1360gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1361 GstVideoCodecFrame *frame)
1362{
1363 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1364 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1365 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1366 GstFlowReturn ret = GST_FLOW_OK;
1367 gboolean processed = FALSE;
1368 GstBuffer *tmp;
1369 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001370
1371 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1372
1373 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1374 goto flushing;
1375
1376 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1377 {
1378 if (!self->input_state)
1379 goto not_negotiated;
1380 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1381 &error))
1382 goto not_negotiated;
1383 }
1384
1385 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1386 {
1387 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001388 GstCapsFeatures *features = NULL;
1389
1390 features = gst_caps_get_features(self->input_state->caps, 0);
1391 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1392 {
1393 GST_DEBUG_OBJECT(self, "Is SVP");
1394 self->v4l2output->is_svp = TRUE;
1395 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001396
1397 GST_DEBUG_OBJECT(self, "Sending header");
1398
1399 codec_data = self->input_state->codec_data;
1400
1401 /* We are running in byte-stream mode, so we don't know the headers, but
1402 * we need to send something, otherwise the decoder will refuse to
1403 * intialize.
1404 */
1405 if (codec_data)
1406 {
1407 gst_buffer_ref(codec_data);
1408 }
1409 else
1410 {
1411 codec_data = gst_buffer_ref(frame->input_buffer);
1412 processed = TRUE;
1413 }
1414
1415 /* Ensure input internal pool is active */
1416 if (!gst_buffer_pool_is_active(pool))
1417 {
1418 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001419 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1420 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001421 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1422 // self->v4l2output->info.size, min, max);
1423 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1424
1425 /* There is no reason to refuse this config */
1426 if (!gst_buffer_pool_set_config(pool, config))
1427 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001428 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001429
1430 if (!gst_buffer_pool_set_active(pool, TRUE))
1431 goto activate_failed;
1432 }
1433
1434 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1435 ret =
1436 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001437 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001438 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1439
1440 gst_buffer_unref(codec_data);
1441
1442 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1443 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1444 * and returns the visible size as with/height and the coded size as
1445 * padding. */
1446 }
1447
1448 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1449 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1450 {
1451 /* It's possible that the processing thread stopped due to an error */
1452 if (self->output_flow != GST_FLOW_OK &&
1453 self->output_flow != GST_FLOW_FLUSHING)
1454 {
1455 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1456 ret = self->output_flow;
1457 goto drop;
1458 }
1459
1460 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1461
1462 /* Start the processing task, when it quits, the task will disable input
1463 * processing to unlock input if draining, or prevent potential block */
1464 self->output_flow = GST_FLOW_FLUSHING;
1465 if (!gst_pad_start_task(decoder->srcpad,
1466 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1467 goto start_task_failed;
1468 }
1469
1470 if (!processed)
1471 {
1472 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001473 if (!self->codec_data_inject && self->input_state->codec_data)
1474 {
1475 ret = gst_aml_v4l2_buffer_pool_process
1476 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1477 self->codec_data_inject = TRUE;
1478 if (ret != GST_FLOW_OK)
1479 goto send_codec_failed;
1480 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001481 ret =
1482 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1483 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1484
1485 if (ret == GST_FLOW_FLUSHING)
1486 {
1487 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1488 GST_TASK_STARTED)
1489 ret = self->output_flow;
1490 goto drop;
1491 }
1492 else if (ret != GST_FLOW_OK)
1493 {
1494 goto process_failed;
1495 }
1496 }
1497
1498 /* No need to keep input arround */
1499 tmp = frame->input_buffer;
1500 frame->input_buffer = gst_buffer_new();
1501 gst_buffer_copy_into(frame->input_buffer, tmp,
1502 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1503 GST_BUFFER_COPY_META,
1504 0, 0);
1505 gst_buffer_unref(tmp);
1506
1507 gst_video_codec_frame_unref(frame);
1508 return ret;
1509
1510 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001511send_codec_failed:
1512 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1513 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001514not_negotiated:
1515{
1516 GST_ERROR_OBJECT(self, "not negotiated");
1517 ret = GST_FLOW_NOT_NEGOTIATED;
1518 gst_aml_v4l2_error(self, &error);
1519 goto drop;
1520}
1521activate_failed:
1522{
1523 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1524 (_("Failed to allocate required memory.")),
1525 ("Buffer pool activation failed"));
1526 ret = GST_FLOW_ERROR;
1527 goto drop;
1528}
1529flushing:
1530{
1531 ret = GST_FLOW_FLUSHING;
1532 goto drop;
1533}
1534
1535start_task_failed:
1536{
1537 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1538 (_("Failed to start decoding thread.")), (NULL));
1539 ret = GST_FLOW_ERROR;
1540 goto drop;
1541}
1542process_failed:
1543{
1544 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1545 (_("Failed to process frame.")),
1546 ("Maybe be due to not enough memory or failing driver"));
1547 ret = GST_FLOW_ERROR;
1548 goto drop;
1549}
1550drop:
1551{
1552 gst_video_decoder_drop_frame(decoder, frame);
1553 return ret;
1554}
1555}
1556
1557static gboolean
1558gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1559 GstQuery *query)
1560{
1561 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1562 GstClockTime latency;
1563 gboolean ret = FALSE;
1564
1565 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1566 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1567
1568 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1569 {
1570 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1571 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1572 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1573 gst_video_decoder_set_latency(decoder, latency, latency);
1574 }
1575 else
1576 {
1577 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1578 }
1579
1580 return ret;
1581}
1582
1583static gboolean
1584gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1585{
1586 gboolean ret = TRUE;
1587 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1588
1589 switch (GST_QUERY_TYPE(query))
1590 {
1591 case GST_QUERY_CAPS:
1592 {
1593 GstCaps *filter, *result = NULL;
1594 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1595
1596 gst_query_parse_caps(query, &filter);
1597
1598 if (self->probed_srccaps)
1599 result = gst_caps_ref(self->probed_srccaps);
1600 else
1601 result = gst_pad_get_pad_template_caps(pad);
1602
1603 if (filter)
1604 {
1605 GstCaps *tmp = result;
1606 result =
1607 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1608 gst_caps_unref(tmp);
1609 }
1610
1611 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1612
1613 gst_query_set_caps_result(query, result);
1614 gst_caps_unref(result);
1615 break;
1616 }
1617
1618 default:
1619 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1620 break;
1621 }
1622
1623 return ret;
1624}
1625
1626static GstCaps *
1627gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1628{
1629 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1630 GstCaps *result;
1631
1632 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1633 filter);
1634
1635 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1636
1637 return result;
1638}
1639
1640static gboolean
1641gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1642{
1643 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1644 gboolean ret;
1645 GstEventType type = GST_EVENT_TYPE(event);
1646
1647 switch (type)
1648 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001649 case GST_EVENT_STREAM_START:
1650 {
1651 GstStructure *s;
1652 GstEvent *event;
1653 GST_DEBUG_OBJECT(self, "new private event");
1654 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1655 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1656 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1657 gst_pad_push_event (decoder->sinkpad, event);
1658 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1659 break;
1660 }
zengliang.li51f54b62023-10-10 12:14:49 +00001661 case GST_EVENT_CAPS:
1662 {
1663 GstCaps *caps;
1664 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001665 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001666
1667 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001668
1669 GST_DEBUG_OBJECT(self, "event caps %" GST_PTR_FORMAT, caps);
1670
zengliang.li51f54b62023-10-10 12:14:49 +00001671 structure= gst_caps_get_structure(caps, 0);
1672 if ( gst_structure_has_field(structure, "parsed") )
1673 {
1674 gboolean parsed = TRUE;
1675 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1676 {
1677 self->v4l2output->stream_mode = !parsed;
1678 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1679 }
1680 }
sheng.liu641aa422023-12-26 07:05:59 +00001681
sheng.liudb26f7d2024-01-22 11:24:23 +00001682 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1683 {
1684 if ( denom == 0 ) denom= 1;
1685
1686 if (self->v4l2capture->fps)
1687 {
1688 g_value_unset(self->v4l2capture->fps);
1689 g_free(self->v4l2capture->fps);
1690 }
1691
1692 self->v4l2capture->fps = g_new0(GValue, 1);
1693 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1694 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1695
1696 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1697 }
1698
sheng.liu641aa422023-12-26 07:05:59 +00001699 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1700 ( !self->v4l2capture->have_set_par ) )
1701 {
1702 if ( (num <= 0) || (denom <= 0) )
1703 {
1704 num= denom= 1;
1705 }
1706
1707 if ( self->v4l2capture->par )
1708 {
1709 g_value_unset(self->v4l2capture->par);
1710 g_free(self->v4l2capture->par);
1711 }
1712
1713 self->v4l2capture->par = g_new0(GValue, 1);
1714 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1715 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1716 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1717 }
zengliang.li51f54b62023-10-10 12:14:49 +00001718 break;
1719 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001720 case GST_EVENT_FLUSH_START:
1721 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001722
1723 g_mutex_lock (&self->res_chg_lock);
1724 while (self->is_res_chg)
1725 {
1726 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1727 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1728 }
1729 g_mutex_unlock (&self->res_chg_lock);
1730
zengliang.li92ff6822023-06-06 07:12:52 +00001731 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001732 gst_aml_v4l2_object_unlock(self->v4l2output);
1733 gst_aml_v4l2_object_unlock(self->v4l2capture);
1734 break;
1735 default:
1736 break;
1737 }
1738
1739 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1740
1741 switch (type)
1742 {
1743 case GST_EVENT_FLUSH_START:
1744 /* The processing thread should stop now, wait for it */
1745 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001746 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001747 GST_DEBUG_OBJECT(self, "flush start done");
1748 break;
1749 default:
1750 break;
1751 }
1752
1753 return ret;
1754}
1755
1756static GstStateChangeReturn
1757gst_aml_v4l2_video_dec_change_state(GstElement *element,
1758 GstStateChange transition)
1759{
1760 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1761 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1762
1763 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1764 {
1765 g_atomic_int_set(&self->active, FALSE);
1766 gst_aml_v4l2_object_unlock(self->v4l2output);
1767 gst_aml_v4l2_object_unlock(self->v4l2capture);
1768 gst_pad_stop_task(decoder->srcpad);
1769 }
1770
1771 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1772}
1773
1774static void
1775gst_aml_v4l2_video_dec_dispose(GObject *object)
1776{
1777 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1778
1779 gst_caps_replace(&self->probed_sinkcaps, NULL);
1780 gst_caps_replace(&self->probed_srccaps, NULL);
1781
1782 G_OBJECT_CLASS(parent_class)->dispose(object);
1783}
1784
1785static void
1786gst_aml_v4l2_video_dec_finalize(GObject *object)
1787{
1788 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1789
1790 gst_aml_v4l2_object_destroy(self->v4l2capture);
1791 gst_aml_v4l2_object_destroy(self->v4l2output);
1792
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001793 g_mutex_clear(&self->res_chg_lock);
1794 g_cond_clear(&self->res_chg_cond);
1795
xuesong.jiang61ea8012022-05-12 15:38:17 +08001796#if GST_IMPORT_LGE_PROP
1797 if (self->lge_ctxt)
1798 {
1799 if (self->lge_ctxt->app_type)
1800 g_free(self->lge_ctxt->app_type);
1801 if (self->lge_ctxt->res_info.coretype)
1802 g_free(self->lge_ctxt->res_info.coretype);
1803 free(self->lge_ctxt);
1804 }
1805
1806#endif
1807
xuesong.jiangae1548e2022-05-06 16:38:46 +08001808 G_OBJECT_CLASS(parent_class)->finalize(object);
1809}
1810
1811static void
1812gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1813{
1814 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001815 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001816 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001817 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001818 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001819 g_mutex_init(&self->res_chg_lock);
1820 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001821#if GST_IMPORT_LGE_PROP
1822 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1823 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1824#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001825}
1826
1827static void
1828gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1829{
1830 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1831 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1832 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1833
1834 gst_video_decoder_set_packetized(decoder, TRUE);
1835
1836 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1837 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1838 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1839 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1840 self->v4l2output->no_initial_format = TRUE;
1841 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001842 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001843
1844 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1845 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1846 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1847 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1848 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001849 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001850}
1851
1852static void
1853gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1854{
1855 GstElementClass *element_class;
1856 GObjectClass *gobject_class;
1857 GstVideoDecoderClass *video_decoder_class;
1858
1859 parent_class = g_type_class_peek_parent(klass);
1860
1861 element_class = (GstElementClass *)klass;
1862 gobject_class = (GObjectClass *)klass;
1863 video_decoder_class = (GstVideoDecoderClass *)klass;
1864
1865 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1866 "AML V4L2 Video Decoder");
1867
1868 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1869 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1870 gobject_class->set_property =
1871 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1872 gobject_class->get_property =
1873 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1874
1875 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1876 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1877 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1878 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1879 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1880 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1881 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1882 video_decoder_class->set_format =
1883 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1884 video_decoder_class->negotiate =
1885 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1886 video_decoder_class->decide_allocation =
1887 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1888 /* FIXME propose_allocation or not ? */
1889 video_decoder_class->handle_frame =
1890 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1891 video_decoder_class->getcaps =
1892 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1893 video_decoder_class->src_query =
1894 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1895 video_decoder_class->sink_event =
1896 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1897
1898 element_class->change_state =
1899 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1900
xuesong.jiang406ee302023-06-28 03:45:22 +00001901 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1902 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1903 G_SIGNAL_RUN_LAST,
1904 0, /* class offset */
1905 NULL, /* accumulator */
1906 NULL, /* accu data */
1907 g_cclosure_marshal_generic,
1908 G_TYPE_NONE,
1909 1,
1910 G_TYPE_UINT64);
1911
xuesong.jiangae1548e2022-05-06 16:38:46 +08001912 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001913#if GST_IMPORT_LGE_PROP
1914 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1915#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001916}
1917
1918static void
1919gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1920{
1921 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1922 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1923 GstAmlV4l2VideoDecCData *cdata = data;
1924
1925 klass->default_device = cdata->device;
1926
1927 /* Note: gst_pad_template_new() take the floating ref from the caps */
1928 gst_element_class_add_pad_template(element_class,
1929 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1930 cdata->sink_caps));
1931 gst_element_class_add_pad_template(element_class,
1932 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1933 cdata->src_caps));
1934
1935 gst_element_class_set_metadata(element_class, cdata->longname,
1936 "Codec/Decoder/Video/Hardware", cdata->description,
1937 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1938
1939 gst_caps_unref(cdata->sink_caps);
1940 gst_caps_unref(cdata->src_caps);
1941 g_free(cdata);
1942}
1943
1944/* Probing functions */
1945gboolean
1946gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1947{
1948 gboolean ret = FALSE;
1949
1950 if (gst_caps_is_subset(sink_caps, gst_aml_v4l2_object_get_codec_caps()) && gst_caps_is_subset(src_caps, gst_aml_v4l2_object_get_raw_caps()))
1951 ret = TRUE;
1952
1953 return ret;
1954}
1955
1956static gchar *
1957gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1958 const gchar *basename)
1959{
1960 gchar *codec_name = NULL;
1961 gchar *type_name = NULL;
1962 gboolean got_value = FALSE;
1963
1964#define SET_META(codec) \
1965 G_STMT_START \
1966 { \
1967 cdata->longname = "AML V4L2 " codec " Decoder"; \
1968 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1969 codec_name = g_ascii_strdown(codec, -1); \
1970 } \
1971 G_STMT_END
1972
bo.xiaoce116a42024-07-16 16:21:53 +08001973 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001974 {
bo.xiaoce116a42024-07-16 16:21:53 +08001975 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001976 }
1977 else if (gst_structure_has_name(s, "video/mpeg"))
1978 {
1979 gint mpegversion = 0;
1980 gint *list = NULL;
1981 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1982 if (FALSE == got_value)
1983 {
1984 got_value = gst_structure_get_list(s, "mpegversion", &list);
1985 if (TRUE == got_value && (1 == *list || 2 == *list))
1986 {
1987 SET_META("MPEG2");
1988 }
1989 else
1990 {
1991 SET_META("MPEG4");
1992 }
1993 }
1994 else
1995 {
1996 SET_META("MPEG4");
1997 }
1998 }
1999 else if (gst_structure_has_name(s, "video/x-h263"))
2000 {
2001 SET_META("H263");
2002 }
2003 else if (gst_structure_has_name(s, "video/x-fwht"))
2004 {
2005 SET_META("FWHT");
2006 }
2007 else if (gst_structure_has_name(s, "video/x-h264"))
2008 {
2009 SET_META("H264");
2010 }
2011 else if (gst_structure_has_name(s, "video/x-h265"))
2012 {
2013 SET_META("H265");
2014 }
2015 else if (gst_structure_has_name(s, "video/x-wmv"))
2016 {
2017 SET_META("VC1");
2018 }
2019 else if (gst_structure_has_name(s, "video/x-vp8"))
2020 {
2021 SET_META("VP8");
2022 }
2023 else if (gst_structure_has_name(s, "video/x-vp9"))
2024 {
2025 SET_META("VP9");
2026 }
2027 else if (gst_structure_has_name(s, "video/x-av1"))
2028 {
2029 SET_META("AV1");
2030 }
zengliang.li51f54b62023-10-10 12:14:49 +00002031 else if (gst_structure_has_name(s, "video/x-avs"))
2032 {
2033 SET_META("AVS");
2034 }
2035 else if (gst_structure_has_name(s, "video/x-avs2"))
2036 {
2037 SET_META("AVS2");
2038 }
2039 else if (gst_structure_has_name(s, "video/x-avs3"))
2040 {
2041 SET_META("AVS3");
2042 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002043 else if (gst_structure_has_name(s, "video/x-bayer"))
2044 {
2045 SET_META("BAYER");
2046 }
2047 else if (gst_structure_has_name(s, "video/x-sonix"))
2048 {
2049 SET_META("SONIX");
2050 }
2051 else if (gst_structure_has_name(s, "video/x-pwc1"))
2052 {
2053 SET_META("PWC1");
2054 }
2055 else if (gst_structure_has_name(s, "video/x-pwc2"))
2056 {
2057 SET_META("PWC2");
2058 }
2059 else
2060 {
2061 /* This code should be kept on sync with the exposed CODEC type of format
2062 * from gstamlv4l2object.c. This warning will only occure in case we forget
2063 * to also add a format here. */
2064 gchar *s_str = gst_structure_to_string(s);
2065 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2066 "bug, please report at https://bugs.gnome.org",
2067 s_str);
2068 g_free(s_str);
2069 }
2070
2071 if (codec_name)
2072 {
2073 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2074 if (g_type_from_name(type_name) != 0)
2075 {
2076 g_free(type_name);
2077 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2078 }
2079
2080 g_free(codec_name);
2081 }
2082
2083 return type_name;
2084#undef SET_META
2085}
2086
2087void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2088 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2089{
2090 gint i;
2091
2092 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2093 {
2094 GstAmlV4l2VideoDecCData *cdata;
2095 GstStructure *s;
2096 GTypeQuery type_query;
2097 GTypeInfo type_info = {
2098 0,
2099 };
2100 GType type, subtype;
2101 gchar *type_name;
2102
2103 s = gst_caps_get_structure(sink_caps, i);
2104
2105 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2106 cdata->device = g_strdup(device_path);
2107 cdata->sink_caps = gst_caps_new_empty();
2108 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2109 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2110 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2111 cdata->src_caps = gst_caps_copy(src_caps);
2112 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2113 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2114 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2115
2116 /* Skip over if we hit an unmapped type */
2117 if (!type_name)
2118 {
2119 g_free(cdata);
2120 continue;
2121 }
2122
2123 type = gst_aml_v4l2_video_dec_get_type();
2124 g_type_query(type, &type_query);
2125 memset(&type_info, 0, sizeof(type_info));
2126 type_info.class_size = type_query.class_size;
2127 type_info.instance_size = type_query.instance_size;
2128 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2129 type_info.class_data = cdata;
2130 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2131
2132 subtype = g_type_register_static(type, type_name, &type_info, 0);
2133 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2134 subtype))
2135 GST_WARNING("Failed to register plugin '%s'", type_name);
2136
2137 g_free(type_name);
2138 }
2139}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002140
2141#if GST_IMPORT_LGE_PROP
2142static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2143{
2144 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2145 g_param_spec_object("resource-info", "resource-info",
2146 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2147 GST_TYPE_STRUCTURE,
2148 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2149
2150 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2151 g_param_spec_uint64("decoded-size", "decoded-size",
2152 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2153 0, G_MAXUINT64,
2154 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2155
2156 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2157 g_param_spec_uint64("undecoded-size", "undecoded-size",
2158 "video decoder element's total undecoded data update unit is byte.",
2159 0, G_MAXUINT64,
2160 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2161
2162 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2163 g_param_spec_string("app-type", "app-type",
2164 "set application type.",
2165 "default_app",
2166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2167
2168 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2169 g_param_spec_boolean("clip-mode", "clip-mode",
2170 "When seeking, Content is moving faster for a while to skip frames.",
2171 FALSE,
2172 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2173}
2174#endif