blob: 7d3a7ede072d36dec9cc78d54f77dad2cbdddbe2 [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;
789 gint count = 0;
790
791 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
792
793 frames = gst_video_decoder_get_frames(decoder);
794 guint frames_len = 0;
795 frames_len = g_list_length(frames);
796 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
797
798 frame = frames->data;
799
800 for (l = frames; l != NULL; l = l->next)
801 {
802 GstVideoCodecFrame *f = l->data;
803
804 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
805 {
806 /* found the right frame */
807 frame = f;
808 break;
809 }
810 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
811 {
812 GST_LOG_OBJECT(decoder,
813 "stream mode drop frame %d %" GST_TIME_FORMAT,
814 frame->system_frame_number, GST_TIME_ARGS(frame->pts));
815
816 gst_video_codec_frame_ref(f);
817 // gst_video_decoder_drop_frame(decoder, f);
818 gst_video_decoder_release_frame(decoder, f);
819 }
820 else
821 {
822 GST_LOG_OBJECT (decoder, "dbg");
823 }
824 }
825
826 if (frame)
827 {
828 guint l_len = 0;
829 l = gst_video_decoder_get_frames(decoder);
830 l_len = g_list_length(l);
831 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
832
833 GST_LOG_OBJECT(decoder,
834 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
835 frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
836 gst_video_codec_frame_ref(frame);
837 }
838
839 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
840
841 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
842 return frame;
843}
844
845static GstVideoCodecFrame *
846gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
847{
848 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
849 if (self->v4l2output->stream_mode)
850 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
851 else
852 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
853}
854
xuesong.jiangae1548e2022-05-06 16:38:46 +0800855static gboolean
856gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
857 GstStructure *structure, gpointer user_data)
858{
859 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
860 GstVideoAlignment *align = &self->v4l2capture->align;
861 GstVideoInfo *info = &self->v4l2capture->info;
862 int width, height;
863
864 if (!gst_structure_get_int(structure, "width", &width))
865 return TRUE;
866
867 if (!gst_structure_get_int(structure, "height", &height))
868 return TRUE;
869
870 if (align->padding_left != 0 || align->padding_top != 0 ||
871 height != info->height + align->padding_bottom)
872 return TRUE;
873
874 if (height == info->height + align->padding_bottom)
875 {
876 /* Some drivers may round up width to the padded with */
877 if (width == info->width + align->padding_right)
878 gst_structure_set(structure,
879 "width", G_TYPE_INT, width - align->padding_right,
880 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
881 /* Some drivers may keep visible width and only round up bytesperline */
882 else if (width == info->width)
883 gst_structure_set(structure,
884 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
885 }
886
887 return TRUE;
888}
889
890static void
sheng.liubcf036c2022-06-21 15:55:42 +0800891gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800892{
893 struct v4l2_event evt;
894 gint ret;
895
896 memset (&evt, 0x00, sizeof (struct v4l2_event));
897 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
898 if (ret < 0)
899 {
900 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
901 return;
902 }
903
904 switch (evt.type)
905 {
906 case V4L2_EVENT_SOURCE_CHANGE:
907 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
908 break;
909 case V4L2_EVENT_EOS:
910 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
911 break;
912 default:
913 break;
914 }
915
916 return;
917}
918
919static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000920gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
921{
922 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
923 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000924 struct v4l2_selection sel;
925 struct v4l2_rect *r = NULL;
926 GstStructure *s;
927 gint width = 0;
928 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800929 GST_DEBUG("%d %d",info.width, info.height);
hanghang.luo2eec4892023-07-18 06:44:42 +0000930 output_state = gst_video_decoder_set_output_state(decoder,
931 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000932 memset(&sel, 0, sizeof(struct v4l2_selection));
933 sel.type = self->v4l2capture->type;
934 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
935 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
936 {
937 r = &sel.r;
938 width = (r->width/2)*2;
939 height = (r->height/2)*2;
940 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
941 }
942 else
943 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000944 if (output_state)
945 {
946 output_state->info.interlace_mode = info.interlace_mode;
947 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000948 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000949 s = gst_caps_get_structure(output_state->caps, 0);
950 if (s)
951 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800952 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
953 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
954 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
955 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000956 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
957 gst_video_codec_state_unref(output_state);
958 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000959 }
960}
961
zengliang.lidcd41462024-06-19 16:05:12 +0800962static GQuark
963gst_aml_v4l2_buffer_pool_cc_import_quark (void)
964{
965 static GQuark quark = 0;
966
967 if (quark == 0)
968 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
969
970 return quark;
971}
972
973static gboolean
974foreach_cc_buffer_list_match_pts_func(GList *list , GstVideoCodecFrame *frame)
975{
976 GList *l;
977 if (g_list_length (list) > 0)
978 {
979 for (l = list; l != NULL; l = l->next)
980 {
981 GstBuffer *cc_buffer = l->data;
982 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
983 {
984 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
985 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
986 #if 0
987 //Debug code:dump cc data
988 GstMapInfo gst_map;
989 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
990 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
991 if (gst_map.size>0)
992 write(fd,gst_map.data,gst_map.size);
993 close(fd);
994 gst_buffer_unmap(cc_buffer,&gst_map);
995 #endif
996 GST_DEBUG("match success");
997 return TRUE;
998 }
999 else
1000 {
1001 GST_DEBUG("match fail");
1002 }
1003 }
1004 GST_WARNING("no match frame in the bufferlist");
1005 }
1006 else
1007 {
1008 GST_WARNING("list is null,can not foreach");
1009 }
1010 return FALSE;
1011}
1012
hanghang.luo2eec4892023-07-18 06:44:42 +00001013static void
xuesong.jiangae1548e2022-05-06 16:38:46 +08001014gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
1015{
1016 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1017 GstAmlV4l2BufferPool *v4l2_pool;
1018 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1019 GstBufferPool *pool;
1020 GstVideoCodecFrame *frame;
1021 GstBuffer *buffer = NULL;
1022 GstFlowReturn ret;
1023
1024 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1025 {
1026 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001027 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001028 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001029 GST_DEBUG_OBJECT(self, "waitting source change event");
1030 /* Wait until received SOURCE_CHANGE event to get right video format */
1031 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1032 {
1033 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1034 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1035 {
fei.deng2a06e042023-10-10 03:09:45 +00001036 //let flush start event blocked until capture buffer pool actived
1037 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001038 GST_DEBUG_OBJECT(self, "Received source change event");
1039 break;
1040 }
1041 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1042 {
1043 GST_DEBUG_OBJECT(self, "Received eos event");
1044 goto beach;
1045 }
1046 else if (ret != GST_FLOW_OK)
1047 {
1048 GST_ERROR_OBJECT(self, "dqevent error");
1049 goto beach;
1050 }
1051 }
1052 self->v4l2capture->need_wait_event = FALSE;
1053
sheng.liu0c77f6c2022-06-17 21:33:20 +08001054 if (TRUE == self->v4l2output->is_svp)
1055 {
1056 GstPad *peer;
1057 GstStructure *s;
1058 GstEvent *event;
1059
1060 peer = gst_pad_get_peer (decoder->srcpad);
1061 if (peer)
1062 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001063 s = gst_structure_new_empty ("IS_SVP");
1064 if (s)
1065 {
1066 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1067 gst_pad_send_event (peer, event);
1068 GST_DEBUG_OBJECT(self, "Send SVP Event");
1069 }
1070 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001071 }
1072 }
1073
sheng.liub56bbc52022-06-21 11:02:33 +08001074 if (self->v4l2capture->need_drop_event)
1075 {
1076 // drop V4L2_EVENT_SOURCE_CHANGE
1077 gst_v4l2_drop_event(self->v4l2capture);
1078 self->v4l2capture->need_drop_event = FALSE;
1079 }
1080
xuesong.jiangae1548e2022-05-06 16:38:46 +08001081 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1082 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001083
xuesong.jiangae1548e2022-05-06 16:38:46 +08001084 /* Create caps from the acquired format, remove the format field */
1085 acquired_caps = gst_video_info_to_caps(&info);
1086 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1087 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001088 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1089
1090 /* Probe currently available pixel formats */
1091 available_caps = gst_caps_copy(self->probed_srccaps);
1092 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1093
1094 /* Replace coded size with visible size, we want to negotiate visible size
1095 * with downstream, not coded size. */
1096 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1097
1098 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001099 caps = gst_caps_copy(filter);
1100 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1101 gst_caps_append(filter, caps);
1102
1103 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1104 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001105 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001106 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1107 gst_caps_unref(filter);
1108
1109 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1110 if (gst_caps_is_empty(caps))
1111 {
1112 gst_caps_unref(caps);
1113 goto not_negotiated;
1114 }
1115
1116 /* Fixate pixel format */
1117 caps = gst_caps_fixate(caps);
1118
1119 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1120
1121 /* Try to set negotiated format, on success replace acquired format */
1122 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1123 gst_video_info_from_caps(&info, caps);
1124 else
1125 gst_aml_v4l2_clear_error(&error);
1126 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001127 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001128 if (!gst_video_decoder_negotiate(decoder))
1129 {
1130 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1131 goto flushing;
1132 else
1133 goto not_negotiated;
1134 }
1135
1136 /* Ensure our internal pool is activated */
1137 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1138 TRUE))
1139 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001140
1141 g_mutex_lock(&self->res_chg_lock);
1142 GST_LOG_OBJECT(decoder, "signal resolution changed");
1143 self->is_res_chg = FALSE;
1144 g_cond_signal(&self->res_chg_cond);
1145 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001146 }
1147
1148 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1149
1150 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1151
1152 self->output_flow = GST_FLOW_OK;
1153 do
1154 {
1155 /* We cannot use the base class allotate helper since it taking the internal
1156 * stream lock. we know that the acquire may need to poll until more frames
1157 * comes in and holding this lock would prevent that.
1158 */
1159 pool = gst_video_decoder_get_buffer_pool(decoder);
1160
1161 /* Pool may be NULL if we started going to READY state */
1162 if (pool == NULL)
1163 {
fei.dengbee20862022-06-14 14:59:48 +08001164 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001165 ret = GST_FLOW_FLUSHING;
1166 goto beach;
1167 }
1168
1169 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001170
fei.dengccc89632022-07-15 19:10:17 +08001171 //calculate a new pts for interlace stream
hanghang.luo8ec04e92023-10-09 08:14:24 +00001172 if (ret == GST_FLOW_OK && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
fei.dengccc89632022-07-15 19:10:17 +08001173 {
1174 //if buffer pts is valid, reduce 1/2 duration
1175 if (GST_BUFFER_DURATION_IS_VALID(buffer))
1176 {
1177 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
1178 }
1179 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1180 //reset pts
hanghang.luo6a5bdff2024-04-15 06:29:43 +00001181 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL)
fei.dengccc89632022-07-15 19:10:17 +08001182 {
1183 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
1184 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1185 }
1186 }
1187
xuesong.jiangae1548e2022-05-06 16:38:46 +08001188 g_object_unref(pool);
1189
fei.deng9a5cd6e2023-06-30 12:09:18 +00001190 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 +00001191 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1192 self->v4l2capture->need_drop_event = TRUE;
1193 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1194 if (self->is_res_chg) {
1195 //we must release last buffer
1196 gst_buffer_unref(buffer);
1197 //if resolution changed event received,we should set need_drop_event to false
1198 self->v4l2capture->need_drop_event = FALSE;
1199 gst_aml_v4l2_object_stop(self->v4l2capture);
1200 //unblock flush start event
1201 g_mutex_lock(&self->res_chg_lock);
1202 self->is_res_chg = FALSE;
1203 g_cond_signal(&self->res_chg_cond);
1204 g_mutex_unlock(&self->res_chg_lock);
1205 return;
1206 } else {
1207 goto beach;
1208 }
sheng.liub56bbc52022-06-21 11:02:33 +08001209 }
1210
zengliang.lidcd41462024-06-19 16:05:12 +08001211 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1212 {
1213 GST_DEBUG_OBJECT(decoder, "continue get cc data ");
1214 continue;
1215 }
1216
sheng.liu8d18ed22022-05-26 17:28:15 +08001217 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1218 {
1219 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001220
1221 g_mutex_lock (&self->res_chg_lock);
1222 self->is_res_chg = TRUE;
1223 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001224 return;
1225 }
fei.dengaf682762024-06-24 19:06:03 +08001226 //decoding error happened
1227 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1228 {
1229 continue;
1230 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001231
fei.dengbee20862022-06-14 14:59:48 +08001232 if (ret != GST_FLOW_OK) {
1233 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001234 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001235 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001236
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001237 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001238 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001239
1240 GST_DEBUG_OBJECT(decoder, "send pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1241 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1242
xuesong.jiangae1548e2022-05-06 16:38:46 +08001243 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1244 {
1245 gst_aml_v4l2_object_stop(self->v4l2capture);
1246 return;
1247 }
1248
fei.dengaf682762024-06-24 19:06:03 +08001249 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1250 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
1251 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001252
1253 if (ret != GST_FLOW_OK)
1254 goto beach;
1255
fei.dengbee20862022-06-14 14:59:48 +08001256 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001257 if (frame)
1258 {
zengliang.li32cb11e2022-11-24 12:10:26 +08001259 if (!GST_CLOCK_TIME_IS_VALID(frame->pts))
1260 {
zengliang.li92ff6822023-06-06 07:12:52 +00001261 if (!GST_CLOCK_TIME_IS_VALID(self->last_out_pts))
1262 {
1263 if (GST_CLOCK_TIME_IS_VALID(frame->dts))
1264 {
1265 GST_BUFFER_TIMESTAMP(buffer) = frame->dts;
1266 }
1267 else
1268 {
1269 GST_WARNING_OBJECT (decoder,"sorry,we have no baseline to calculate pts");
1270 goto beach;
1271 }
1272 }
1273 else
1274 {
1275 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d);
1276 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1277 }
zengliang.li32cb11e2022-11-24 12:10:26 +08001278 }
fei.dengccc89632022-07-15 19:10:17 +08001279 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001280 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001281 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1282 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001283 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001284
1285 GST_DEBUG_OBJECT (decoder,"enable_cc_data:%d",self->v4l2capture->enable_cc_data);
1286 if (self->v4l2capture->enable_cc_data)
1287 {
1288 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1289 {
1290 GST_DEBUG("cc buffer and frame bind success");
1291 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1292 GST_AML_V4L2_CC_IMPORT_QUARK);
1293 #if 0
1294 //Debug code:dump cc data
1295 GstMapInfo gst_map;
1296 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1297 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1298 if (gst_map.size>0)
1299 write(fd,gst_map.data,gst_map.size);
1300 close(fd);
1301 gst_buffer_unmap(cc_buffer,&gst_map);
1302 #endif
1303 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1304 gst_buffer_unref(cc_buffer);
1305 }
1306 else
1307 {
1308 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1309 }
1310 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001311 ret = gst_video_decoder_finish_frame(decoder, frame);
1312
1313 if (ret != GST_FLOW_OK)
1314 goto beach;
1315 }
1316 else
1317 {
1318 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
1319 gst_buffer_unref(buffer);
1320 }
1321
1322 return;
1323 /* ERRORS */
1324not_negotiated:
1325{
1326 GST_ERROR_OBJECT(self, "not negotiated");
1327 ret = GST_FLOW_NOT_NEGOTIATED;
1328 goto beach;
1329}
1330activate_failed:
1331{
1332 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1333 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1334 (_("Failed to allocate required memory.")),
1335 ("Buffer pool activation failed"));
1336 ret = GST_FLOW_ERROR;
1337 goto beach;
1338}
1339flushing:
1340{
1341 ret = GST_FLOW_FLUSHING;
1342 goto beach;
1343}
1344beach:
1345 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1346 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001347 if (self->is_res_chg) {
1348 //unblock flush start event
1349 g_mutex_lock(&self->res_chg_lock);
1350 self->is_res_chg = FALSE;
1351 g_cond_signal(&self->res_chg_cond);
1352 g_mutex_unlock(&self->res_chg_lock);
1353 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001354 gst_buffer_replace(&buffer, NULL);
1355 self->output_flow = ret;
1356 gst_aml_v4l2_object_unlock(self->v4l2output);
1357 gst_pad_pause_task(decoder->srcpad);
1358}
1359
1360static GstFlowReturn
1361gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1362 GstVideoCodecFrame *frame)
1363{
1364 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1365 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1366 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1367 GstFlowReturn ret = GST_FLOW_OK;
1368 gboolean processed = FALSE;
1369 GstBuffer *tmp;
1370 GstTaskState task_state;
1371 GstCaps *caps;
1372
1373 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1374
1375 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1376 goto flushing;
1377
1378 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1379 {
1380 if (!self->input_state)
1381 goto not_negotiated;
1382 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1383 &error))
1384 goto not_negotiated;
1385 }
1386
1387 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1388 {
1389 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001390 GstCapsFeatures *features = NULL;
1391
1392 features = gst_caps_get_features(self->input_state->caps, 0);
1393 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1394 {
1395 GST_DEBUG_OBJECT(self, "Is SVP");
1396 self->v4l2output->is_svp = TRUE;
1397 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001398
1399 GST_DEBUG_OBJECT(self, "Sending header");
1400
1401 codec_data = self->input_state->codec_data;
1402
1403 /* We are running in byte-stream mode, so we don't know the headers, but
1404 * we need to send something, otherwise the decoder will refuse to
1405 * intialize.
1406 */
1407 if (codec_data)
1408 {
1409 gst_buffer_ref(codec_data);
1410 }
1411 else
1412 {
1413 codec_data = gst_buffer_ref(frame->input_buffer);
1414 processed = TRUE;
1415 }
1416
1417 /* Ensure input internal pool is active */
1418 if (!gst_buffer_pool_is_active(pool))
1419 {
1420 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001421 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1422 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001423 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1424 // self->v4l2output->info.size, min, max);
1425 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1426
1427 /* There is no reason to refuse this config */
1428 if (!gst_buffer_pool_set_config(pool, config))
1429 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001430 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001431
1432 if (!gst_buffer_pool_set_active(pool, TRUE))
1433 goto activate_failed;
1434 }
1435
1436 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1437 ret =
1438 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001439 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001440 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1441
1442 gst_buffer_unref(codec_data);
1443
1444 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1445 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1446 * and returns the visible size as with/height and the coded size as
1447 * padding. */
1448 }
1449
1450 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1451 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1452 {
1453 /* It's possible that the processing thread stopped due to an error */
1454 if (self->output_flow != GST_FLOW_OK &&
1455 self->output_flow != GST_FLOW_FLUSHING)
1456 {
1457 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1458 ret = self->output_flow;
1459 goto drop;
1460 }
1461
1462 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1463
1464 /* Start the processing task, when it quits, the task will disable input
1465 * processing to unlock input if draining, or prevent potential block */
1466 self->output_flow = GST_FLOW_FLUSHING;
1467 if (!gst_pad_start_task(decoder->srcpad,
1468 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1469 goto start_task_failed;
1470 }
1471
1472 if (!processed)
1473 {
1474 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001475 if (!self->codec_data_inject && self->input_state->codec_data)
1476 {
1477 ret = gst_aml_v4l2_buffer_pool_process
1478 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1479 self->codec_data_inject = TRUE;
1480 if (ret != GST_FLOW_OK)
1481 goto send_codec_failed;
1482 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001483 ret =
1484 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1485 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1486
1487 if (ret == GST_FLOW_FLUSHING)
1488 {
1489 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1490 GST_TASK_STARTED)
1491 ret = self->output_flow;
1492 goto drop;
1493 }
1494 else if (ret != GST_FLOW_OK)
1495 {
1496 goto process_failed;
1497 }
1498 }
1499
1500 /* No need to keep input arround */
1501 tmp = frame->input_buffer;
1502 frame->input_buffer = gst_buffer_new();
1503 gst_buffer_copy_into(frame->input_buffer, tmp,
1504 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1505 GST_BUFFER_COPY_META,
1506 0, 0);
1507 gst_buffer_unref(tmp);
1508
1509 gst_video_codec_frame_unref(frame);
1510 return ret;
1511
1512 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001513send_codec_failed:
1514 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1515 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001516not_negotiated:
1517{
1518 GST_ERROR_OBJECT(self, "not negotiated");
1519 ret = GST_FLOW_NOT_NEGOTIATED;
1520 gst_aml_v4l2_error(self, &error);
1521 goto drop;
1522}
1523activate_failed:
1524{
1525 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1526 (_("Failed to allocate required memory.")),
1527 ("Buffer pool activation failed"));
1528 ret = GST_FLOW_ERROR;
1529 goto drop;
1530}
1531flushing:
1532{
1533 ret = GST_FLOW_FLUSHING;
1534 goto drop;
1535}
1536
1537start_task_failed:
1538{
1539 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1540 (_("Failed to start decoding thread.")), (NULL));
1541 ret = GST_FLOW_ERROR;
1542 goto drop;
1543}
1544process_failed:
1545{
1546 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1547 (_("Failed to process frame.")),
1548 ("Maybe be due to not enough memory or failing driver"));
1549 ret = GST_FLOW_ERROR;
1550 goto drop;
1551}
1552drop:
1553{
1554 gst_video_decoder_drop_frame(decoder, frame);
1555 return ret;
1556}
1557}
1558
1559static gboolean
1560gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1561 GstQuery *query)
1562{
1563 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1564 GstClockTime latency;
1565 gboolean ret = FALSE;
1566
1567 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1568 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1569
1570 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1571 {
1572 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1573 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1574 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1575 gst_video_decoder_set_latency(decoder, latency, latency);
1576 }
1577 else
1578 {
1579 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1580 }
1581
1582 return ret;
1583}
1584
1585static gboolean
1586gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1587{
1588 gboolean ret = TRUE;
1589 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1590
1591 switch (GST_QUERY_TYPE(query))
1592 {
1593 case GST_QUERY_CAPS:
1594 {
1595 GstCaps *filter, *result = NULL;
1596 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1597
1598 gst_query_parse_caps(query, &filter);
1599
1600 if (self->probed_srccaps)
1601 result = gst_caps_ref(self->probed_srccaps);
1602 else
1603 result = gst_pad_get_pad_template_caps(pad);
1604
1605 if (filter)
1606 {
1607 GstCaps *tmp = result;
1608 result =
1609 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1610 gst_caps_unref(tmp);
1611 }
1612
1613 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1614
1615 gst_query_set_caps_result(query, result);
1616 gst_caps_unref(result);
1617 break;
1618 }
1619
1620 default:
1621 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1622 break;
1623 }
1624
1625 return ret;
1626}
1627
1628static GstCaps *
1629gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1630{
1631 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1632 GstCaps *result;
1633
1634 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1635 filter);
1636
1637 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1638
1639 return result;
1640}
1641
1642static gboolean
1643gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1644{
1645 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1646 gboolean ret;
1647 GstEventType type = GST_EVENT_TYPE(event);
1648
1649 switch (type)
1650 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001651 case GST_EVENT_STREAM_START:
1652 {
1653 GstStructure *s;
1654 GstEvent *event;
1655 GST_DEBUG_OBJECT(self, "new private event");
1656 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1657 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1658 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1659 gst_pad_push_event (decoder->sinkpad, event);
1660 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1661 break;
1662 }
zengliang.li51f54b62023-10-10 12:14:49 +00001663 case GST_EVENT_CAPS:
1664 {
1665 GstCaps *caps;
1666 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001667 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001668
1669 gst_event_parse_caps (event, &caps);
1670 structure= gst_caps_get_structure(caps, 0);
1671 if ( gst_structure_has_field(structure, "parsed") )
1672 {
1673 gboolean parsed = TRUE;
1674 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1675 {
1676 self->v4l2output->stream_mode = !parsed;
1677 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1678 }
1679 }
sheng.liu641aa422023-12-26 07:05:59 +00001680
sheng.liudb26f7d2024-01-22 11:24:23 +00001681 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1682 {
1683 if ( denom == 0 ) denom= 1;
1684
1685 if (self->v4l2capture->fps)
1686 {
1687 g_value_unset(self->v4l2capture->fps);
1688 g_free(self->v4l2capture->fps);
1689 }
1690
1691 self->v4l2capture->fps = g_new0(GValue, 1);
1692 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1693 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1694
1695 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1696 }
1697
sheng.liu641aa422023-12-26 07:05:59 +00001698 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1699 ( !self->v4l2capture->have_set_par ) )
1700 {
1701 if ( (num <= 0) || (denom <= 0) )
1702 {
1703 num= denom= 1;
1704 }
1705
1706 if ( self->v4l2capture->par )
1707 {
1708 g_value_unset(self->v4l2capture->par);
1709 g_free(self->v4l2capture->par);
1710 }
1711
1712 self->v4l2capture->par = g_new0(GValue, 1);
1713 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1714 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1715 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1716 }
zengliang.li51f54b62023-10-10 12:14:49 +00001717 break;
1718 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001719 case GST_EVENT_FLUSH_START:
1720 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001721
1722 g_mutex_lock (&self->res_chg_lock);
1723 while (self->is_res_chg)
1724 {
1725 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1726 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1727 }
1728 g_mutex_unlock (&self->res_chg_lock);
1729
zengliang.li92ff6822023-06-06 07:12:52 +00001730 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001731 gst_aml_v4l2_object_unlock(self->v4l2output);
1732 gst_aml_v4l2_object_unlock(self->v4l2capture);
1733 break;
1734 default:
1735 break;
1736 }
1737
1738 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1739
1740 switch (type)
1741 {
1742 case GST_EVENT_FLUSH_START:
1743 /* The processing thread should stop now, wait for it */
1744 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001745 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001746 GST_DEBUG_OBJECT(self, "flush start done");
1747 break;
1748 default:
1749 break;
1750 }
1751
1752 return ret;
1753}
1754
1755static GstStateChangeReturn
1756gst_aml_v4l2_video_dec_change_state(GstElement *element,
1757 GstStateChange transition)
1758{
1759 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1760 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1761
1762 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1763 {
1764 g_atomic_int_set(&self->active, FALSE);
1765 gst_aml_v4l2_object_unlock(self->v4l2output);
1766 gst_aml_v4l2_object_unlock(self->v4l2capture);
1767 gst_pad_stop_task(decoder->srcpad);
1768 }
1769
1770 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1771}
1772
1773static void
1774gst_aml_v4l2_video_dec_dispose(GObject *object)
1775{
1776 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1777
1778 gst_caps_replace(&self->probed_sinkcaps, NULL);
1779 gst_caps_replace(&self->probed_srccaps, NULL);
1780
1781 G_OBJECT_CLASS(parent_class)->dispose(object);
1782}
1783
1784static void
1785gst_aml_v4l2_video_dec_finalize(GObject *object)
1786{
1787 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1788
1789 gst_aml_v4l2_object_destroy(self->v4l2capture);
1790 gst_aml_v4l2_object_destroy(self->v4l2output);
1791
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001792 g_mutex_clear(&self->res_chg_lock);
1793 g_cond_clear(&self->res_chg_cond);
1794
xuesong.jiang61ea8012022-05-12 15:38:17 +08001795#if GST_IMPORT_LGE_PROP
1796 if (self->lge_ctxt)
1797 {
1798 if (self->lge_ctxt->app_type)
1799 g_free(self->lge_ctxt->app_type);
1800 if (self->lge_ctxt->res_info.coretype)
1801 g_free(self->lge_ctxt->res_info.coretype);
1802 free(self->lge_ctxt);
1803 }
1804
1805#endif
1806
xuesong.jiangae1548e2022-05-06 16:38:46 +08001807 G_OBJECT_CLASS(parent_class)->finalize(object);
1808}
1809
1810static void
1811gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1812{
1813 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001814 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001815 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001816 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001817 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001818 g_mutex_init(&self->res_chg_lock);
1819 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001820#if GST_IMPORT_LGE_PROP
1821 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1822 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1823#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001824}
1825
1826static void
1827gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1828{
1829 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1830 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1831 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1832
1833 gst_video_decoder_set_packetized(decoder, TRUE);
1834
1835 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1836 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1837 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1838 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1839 self->v4l2output->no_initial_format = TRUE;
1840 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001841 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001842
1843 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1844 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1845 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1846 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1847 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001848 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001849}
1850
1851static void
1852gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1853{
1854 GstElementClass *element_class;
1855 GObjectClass *gobject_class;
1856 GstVideoDecoderClass *video_decoder_class;
1857
1858 parent_class = g_type_class_peek_parent(klass);
1859
1860 element_class = (GstElementClass *)klass;
1861 gobject_class = (GObjectClass *)klass;
1862 video_decoder_class = (GstVideoDecoderClass *)klass;
1863
1864 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1865 "AML V4L2 Video Decoder");
1866
1867 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1868 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1869 gobject_class->set_property =
1870 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1871 gobject_class->get_property =
1872 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1873
1874 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1875 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1876 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1877 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1878 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1879 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1880 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1881 video_decoder_class->set_format =
1882 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1883 video_decoder_class->negotiate =
1884 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1885 video_decoder_class->decide_allocation =
1886 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1887 /* FIXME propose_allocation or not ? */
1888 video_decoder_class->handle_frame =
1889 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1890 video_decoder_class->getcaps =
1891 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1892 video_decoder_class->src_query =
1893 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1894 video_decoder_class->sink_event =
1895 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1896
1897 element_class->change_state =
1898 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1899
xuesong.jiang406ee302023-06-28 03:45:22 +00001900 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1901 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1902 G_SIGNAL_RUN_LAST,
1903 0, /* class offset */
1904 NULL, /* accumulator */
1905 NULL, /* accu data */
1906 g_cclosure_marshal_generic,
1907 G_TYPE_NONE,
1908 1,
1909 G_TYPE_UINT64);
1910
xuesong.jiangae1548e2022-05-06 16:38:46 +08001911 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001912#if GST_IMPORT_LGE_PROP
1913 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1914#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001915}
1916
1917static void
1918gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1919{
1920 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1921 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1922 GstAmlV4l2VideoDecCData *cdata = data;
1923
1924 klass->default_device = cdata->device;
1925
1926 /* Note: gst_pad_template_new() take the floating ref from the caps */
1927 gst_element_class_add_pad_template(element_class,
1928 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1929 cdata->sink_caps));
1930 gst_element_class_add_pad_template(element_class,
1931 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1932 cdata->src_caps));
1933
1934 gst_element_class_set_metadata(element_class, cdata->longname,
1935 "Codec/Decoder/Video/Hardware", cdata->description,
1936 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1937
1938 gst_caps_unref(cdata->sink_caps);
1939 gst_caps_unref(cdata->src_caps);
1940 g_free(cdata);
1941}
1942
1943/* Probing functions */
1944gboolean
1945gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1946{
1947 gboolean ret = FALSE;
1948
1949 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()))
1950 ret = TRUE;
1951
1952 return ret;
1953}
1954
1955static gchar *
1956gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1957 const gchar *basename)
1958{
1959 gchar *codec_name = NULL;
1960 gchar *type_name = NULL;
1961 gboolean got_value = FALSE;
1962
1963#define SET_META(codec) \
1964 G_STMT_START \
1965 { \
1966 cdata->longname = "AML V4L2 " codec " Decoder"; \
1967 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1968 codec_name = g_ascii_strdown(codec, -1); \
1969 } \
1970 G_STMT_END
1971
bo.xiaoce116a42024-07-16 16:21:53 +08001972 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001973 {
bo.xiaoce116a42024-07-16 16:21:53 +08001974 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001975 }
1976 else if (gst_structure_has_name(s, "video/mpeg"))
1977 {
1978 gint mpegversion = 0;
1979 gint *list = NULL;
1980 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1981 if (FALSE == got_value)
1982 {
1983 got_value = gst_structure_get_list(s, "mpegversion", &list);
1984 if (TRUE == got_value && (1 == *list || 2 == *list))
1985 {
1986 SET_META("MPEG2");
1987 }
1988 else
1989 {
1990 SET_META("MPEG4");
1991 }
1992 }
1993 else
1994 {
1995 SET_META("MPEG4");
1996 }
1997 }
1998 else if (gst_structure_has_name(s, "video/x-h263"))
1999 {
2000 SET_META("H263");
2001 }
2002 else if (gst_structure_has_name(s, "video/x-fwht"))
2003 {
2004 SET_META("FWHT");
2005 }
2006 else if (gst_structure_has_name(s, "video/x-h264"))
2007 {
2008 SET_META("H264");
2009 }
2010 else if (gst_structure_has_name(s, "video/x-h265"))
2011 {
2012 SET_META("H265");
2013 }
2014 else if (gst_structure_has_name(s, "video/x-wmv"))
2015 {
2016 SET_META("VC1");
2017 }
2018 else if (gst_structure_has_name(s, "video/x-vp8"))
2019 {
2020 SET_META("VP8");
2021 }
2022 else if (gst_structure_has_name(s, "video/x-vp9"))
2023 {
2024 SET_META("VP9");
2025 }
2026 else if (gst_structure_has_name(s, "video/x-av1"))
2027 {
2028 SET_META("AV1");
2029 }
zengliang.li51f54b62023-10-10 12:14:49 +00002030 else if (gst_structure_has_name(s, "video/x-avs"))
2031 {
2032 SET_META("AVS");
2033 }
2034 else if (gst_structure_has_name(s, "video/x-avs2"))
2035 {
2036 SET_META("AVS2");
2037 }
2038 else if (gst_structure_has_name(s, "video/x-avs3"))
2039 {
2040 SET_META("AVS3");
2041 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002042 else if (gst_structure_has_name(s, "video/x-bayer"))
2043 {
2044 SET_META("BAYER");
2045 }
2046 else if (gst_structure_has_name(s, "video/x-sonix"))
2047 {
2048 SET_META("SONIX");
2049 }
2050 else if (gst_structure_has_name(s, "video/x-pwc1"))
2051 {
2052 SET_META("PWC1");
2053 }
2054 else if (gst_structure_has_name(s, "video/x-pwc2"))
2055 {
2056 SET_META("PWC2");
2057 }
2058 else
2059 {
2060 /* This code should be kept on sync with the exposed CODEC type of format
2061 * from gstamlv4l2object.c. This warning will only occure in case we forget
2062 * to also add a format here. */
2063 gchar *s_str = gst_structure_to_string(s);
2064 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2065 "bug, please report at https://bugs.gnome.org",
2066 s_str);
2067 g_free(s_str);
2068 }
2069
2070 if (codec_name)
2071 {
2072 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2073 if (g_type_from_name(type_name) != 0)
2074 {
2075 g_free(type_name);
2076 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2077 }
2078
2079 g_free(codec_name);
2080 }
2081
2082 return type_name;
2083#undef SET_META
2084}
2085
2086void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2087 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2088{
2089 gint i;
2090
2091 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2092 {
2093 GstAmlV4l2VideoDecCData *cdata;
2094 GstStructure *s;
2095 GTypeQuery type_query;
2096 GTypeInfo type_info = {
2097 0,
2098 };
2099 GType type, subtype;
2100 gchar *type_name;
2101
2102 s = gst_caps_get_structure(sink_caps, i);
2103
2104 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2105 cdata->device = g_strdup(device_path);
2106 cdata->sink_caps = gst_caps_new_empty();
2107 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2108 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2109 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2110 cdata->src_caps = gst_caps_copy(src_caps);
2111 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2112 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2113 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2114
2115 /* Skip over if we hit an unmapped type */
2116 if (!type_name)
2117 {
2118 g_free(cdata);
2119 continue;
2120 }
2121
2122 type = gst_aml_v4l2_video_dec_get_type();
2123 g_type_query(type, &type_query);
2124 memset(&type_info, 0, sizeof(type_info));
2125 type_info.class_size = type_query.class_size;
2126 type_info.instance_size = type_query.instance_size;
2127 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2128 type_info.class_data = cdata;
2129 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2130
2131 subtype = g_type_register_static(type, type_name, &type_info, 0);
2132 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2133 subtype))
2134 GST_WARNING("Failed to register plugin '%s'", type_name);
2135
2136 g_free(type_name);
2137 }
2138}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002139
2140#if GST_IMPORT_LGE_PROP
2141static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2142{
2143 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2144 g_param_spec_object("resource-info", "resource-info",
2145 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2146 GST_TYPE_STRUCTURE,
2147 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2148
2149 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2150 g_param_spec_uint64("decoded-size", "decoded-size",
2151 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2152 0, G_MAXUINT64,
2153 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2154
2155 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2156 g_param_spec_uint64("undecoded-size", "undecoded-size",
2157 "video decoder element's total undecoded data update unit is byte.",
2158 0, G_MAXUINT64,
2159 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2160
2161 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2162 g_param_spec_string("app-type", "app-type",
2163 "set application type.",
2164 "default_app",
2165 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2166
2167 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2168 g_param_spec_boolean("clip-mode", "clip-mode",
2169 "When seeking, Content is moving faster for a while to skip frames.",
2170 FALSE,
2171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2172}
2173#endif