blob: 8ae49c5a810cff46094374497e6d1aac04792f54 [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 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800450
451 gst_aml_v4l2_video_dec_finish(decoder);
452 gst_aml_v4l2_object_stop(self->v4l2output);
453
fei.dengf78f7122024-07-15 14:44:04 +0800454 gst_video_codec_state_unref(self->input_state);
455 self->input_state = NULL;
456
xuesong.jiangae1548e2022-05-06 16:38:46 +0800457 /* The renegotiation flow don't blend with the base class flow. To properly
458 * stop the capture pool, if the buffers can't be orphaned, we need to
459 * reclaim our buffers, which will happend through the allocation query.
460 * The allocation query is triggered by gst_video_decoder_negotiate() which
461 * requires the output caps to be set, but we can't know this information
462 * as we rely on the decoder, which requires the capture queue to be
463 * stopped.
464 *
465 * To workaround this issue, we simply run an allocation query with the
466 * old negotiated caps in order to drain/reclaim our buffers. That breaks
467 * the complexity and should not have much impact in performance since the
468 * following allocation query will happen on a drained pipeline and won't
469 * block. */
470 if (self->v4l2capture->pool &&
471 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
472 {
473 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
474 if (caps)
475 {
476 GstQuery *query = gst_query_new_allocation(caps, FALSE);
477 gst_pad_peer_query(decoder->srcpad, query);
478 gst_query_unref(query);
479 gst_caps_unref(caps);
480 }
481 }
482
483 gst_aml_v4l2_object_stop(self->v4l2capture);
484 self->output_flow = GST_FLOW_OK;
485 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800486 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
487 {
488 GST_ERROR_OBJECT(self, "config output drm mode error");
489 goto done;
490 }
491
xuesong.jiang22a9b112023-05-24 09:01:59 +0000492 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
493 {
494 GST_ERROR_OBJECT(self, "config output stream mode error");
495 goto done;
496 }
497
hanghang.luoac6df002024-07-11 10:54:17 +0800498 if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error))
499 {
500 GST_ERROR_OBJECT(self, "set format error");
501 goto done;
502 }
503
504 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
505 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
506 {
507 GST_ERROR_OBJECT(self, "config I frame mode error");
508 goto done;
509 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800510
511 gst_caps_replace(&self->probed_srccaps, NULL);
512 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
513 gst_aml_v4l2_object_get_raw_caps());
514
515 if (gst_caps_is_empty(self->probed_srccaps))
516 goto no_raw_format;
517
518 caps = gst_caps_copy(self->probed_srccaps);
519 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
520 gst_caps_append(self->probed_srccaps, caps);
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800521
xuesong.jiangae1548e2022-05-06 16:38:46 +0800522 if (ret)
523 self->input_state = gst_video_codec_state_ref(state);
524 else
525 gst_aml_v4l2_error(self, &error);
526
527done:
528 return ret;
529
530no_raw_format:
531 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
532 (_("Decoder on device %s has no supported output format"),
533 self->v4l2output->videodev),
534 (NULL));
535 return GST_FLOW_ERROR;
536}
537
538static gboolean
539gst_aml_v4l2_video_dec_flush(GstVideoDecoder *decoder)
540{
541 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
542
543 GST_DEBUG_OBJECT(self, "Flushed");
544
545 /* Ensure the processing thread has stopped for the reverse playback
546 * discount case */
547 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
548 {
549 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
550
551 gst_aml_v4l2_object_unlock(self->v4l2output);
552 gst_aml_v4l2_object_unlock(self->v4l2capture);
553 gst_pad_stop_task(decoder->srcpad);
554 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
555 }
556
557 self->output_flow = GST_FLOW_OK;
558
559 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
560 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
561
562 if (self->v4l2output->pool)
563 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
564
565 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
566 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
567 * pool. */
568 if (self->v4l2capture->pool)
569 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
570
571 return TRUE;
572}
573
574static gboolean
575gst_aml_v4l2_video_dec_negotiate(GstVideoDecoder *decoder)
576{
577 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
578
xuesong.jiang681d3602022-06-24 21:23:35 +0800579 if (TRUE == self->v4l2output->is_svp)
580 {
581 GstStructure *s;
582 GstEvent *event;
583
584 s = gst_structure_new_empty ("IS_SVP");
585 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
586 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
587 gst_pad_push_event (decoder->srcpad, event);
588 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
589 }
590
xuesong.jiangae1548e2022-05-06 16:38:46 +0800591 /* We don't allow renegotiation without carefull disabling the pool */
592 if (self->v4l2capture->pool &&
593 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
594 return TRUE;
595
596 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
597}
598
599static gboolean
600gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
601{
602 struct v4l2_decoder_cmd dcmd = {
603 0,
604 };
605
606 GST_DEBUG_OBJECT(v4l2object->element,
607 "sending v4l2 decoder command %u with flags %u", cmd, flags);
608
609 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
610 return FALSE;
611
612 dcmd.cmd = cmd;
613 dcmd.flags = flags;
614 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
615 goto dcmd_failed;
616
617 return TRUE;
618
619dcmd_failed:
620 if (errno == ENOTTY)
621 {
622 GST_INFO_OBJECT(v4l2object->element,
623 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
624 cmd, flags, v4l2object->videodev, g_strerror(errno));
625 }
626 else
627 {
628 GST_ERROR_OBJECT(v4l2object->element,
629 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
630 cmd, flags, v4l2object->videodev, g_strerror(errno));
631 }
632 return FALSE;
633}
634
635static GstFlowReturn
636gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
637{
638 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
639 GstFlowReturn ret = GST_FLOW_OK;
640 GstBuffer *buffer;
641
642 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
643 goto done;
644
645 GST_DEBUG_OBJECT(self, "Finishing decoding");
646
647 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
648
649 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
650 {
651 GstTask *task = decoder->srcpad->task;
652
653 /* If the decoder stop command succeeded, just wait until processing is
654 * finished */
655 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
656 GST_OBJECT_LOCK(task);
657 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
658 GST_TASK_WAIT(task);
659 GST_OBJECT_UNLOCK(task);
660 ret = GST_FLOW_FLUSHING;
661 }
662 else
663 {
664 /* otherwise keep queuing empty buffers until the processing thread has
665 * stopped, _pool_process() will return FLUSHING when that happened */
666 while (ret == GST_FLOW_OK)
667 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800668 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800669 buffer = gst_buffer_new();
670 ret =
671 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
672 gst_buffer_unref(buffer);
673 }
674 }
675
676 /* and ensure the processing thread has stopped in case another error
677 * occured. */
678 gst_aml_v4l2_object_unlock(self->v4l2capture);
679 gst_pad_stop_task(decoder->srcpad);
680 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
681
682 if (ret == GST_FLOW_FLUSHING)
683 ret = self->output_flow;
684
685 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
686
687 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
688
689done:
690 return ret;
691}
692
693static GstFlowReturn
694gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
695{
696 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
697
698 GST_DEBUG_OBJECT(self, "Draining...");
699 gst_aml_v4l2_video_dec_finish(decoder);
700 gst_aml_v4l2_video_dec_flush(decoder);
701
702 return GST_FLOW_OK;
703}
704
705static GstVideoCodecFrame *
706gst_aml_v4l2_video_dec_get_oldest_frame(GstVideoDecoder *decoder)
707{
708 GstVideoCodecFrame *frame = NULL;
709 GList *frames, *l;
710 gint count = 0;
711
712 frames = gst_video_decoder_get_frames(decoder);
713
714 for (l = frames; l != NULL; l = l->next)
715 {
716 GstVideoCodecFrame *f = l->data;
717
718 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
719 frame = f;
720
721 count++;
722 }
723
724 if (frame)
725 {
726 GST_LOG_OBJECT(decoder,
727 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
728 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
729 gst_video_codec_frame_ref(frame);
730 }
731
732 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
733
734 return frame;
735}
736
fei.dengbee20862022-06-14 14:59:48 +0800737static GstVideoCodecFrame *
xuesong.jiange24aef92023-06-16 06:39:10 +0000738gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800739{
740 GstVideoCodecFrame *frame = NULL;
741 GList *frames, *l;
742 gint count = 0;
743
xuesong.jiange24aef92023-06-16 06:39:10 +0000744 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
745
fei.dengbee20862022-06-14 14:59:48 +0800746 frames = gst_video_decoder_get_frames(decoder);
747
748 for (l = frames; l != NULL; l = l->next)
749 {
750 GstVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000751
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800752 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800753 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800754 }
fei.dengbee20862022-06-14 14:59:48 +0800755 count++;
756 }
757
zengliang.liddee2da2023-07-14 07:27:05 +0000758 if (!frame)
759 {
760 for (l = frames; l != NULL; l = l->next)
761 {
762 GstVideoCodecFrame *f = l->data;
763 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
764 {
765 frame = f;
fei.dengf78f7122024-07-15 14:44:04 +0800766 GST_DEBUG("The pts of the expected output frame is invalid");
767 break;
zengliang.liddee2da2023-07-14 07:27:05 +0000768 }
zengliang.liddee2da2023-07-14 07:27:05 +0000769 }
770 }
771
fei.dengbee20862022-06-14 14:59:48 +0800772 if (frame)
773 {
774 GST_LOG_OBJECT(decoder,
775 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
776 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
777 gst_video_codec_frame_ref(frame);
778 }
779
780 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
781
xuesong.jiange24aef92023-06-16 06:39:10 +0000782 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
fei.dengbee20862022-06-14 14:59:48 +0800783 return frame;
784}
785
xuesong.jiange24aef92023-06-16 06:39:10 +0000786static GstVideoCodecFrame *
787gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstVideoDecoder *decoder, GstClockTime pts)
788{
789 GstVideoCodecFrame *frame = NULL;
790 GList *frames, *l;
xuesong.jiange24aef92023-06-16 06:39:10 +0000791
792 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
793
794 frames = gst_video_decoder_get_frames(decoder);
795 guint frames_len = 0;
796 frames_len = g_list_length(frames);
797 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
798
799 frame = frames->data;
800
801 for (l = frames; l != NULL; l = l->next)
802 {
803 GstVideoCodecFrame *f = l->data;
804
805 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
806 {
807 /* found the right frame */
808 frame = f;
809 break;
810 }
811 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
812 {
813 GST_LOG_OBJECT(decoder,
814 "stream mode drop frame %d %" GST_TIME_FORMAT,
815 frame->system_frame_number, GST_TIME_ARGS(frame->pts));
816
817 gst_video_codec_frame_ref(f);
818 // gst_video_decoder_drop_frame(decoder, f);
819 gst_video_decoder_release_frame(decoder, f);
820 }
821 else
822 {
823 GST_LOG_OBJECT (decoder, "dbg");
824 }
825 }
826
827 if (frame)
828 {
829 guint l_len = 0;
830 l = gst_video_decoder_get_frames(decoder);
831 l_len = g_list_length(l);
832 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
833
834 GST_LOG_OBJECT(decoder,
835 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
836 frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
837 gst_video_codec_frame_ref(frame);
838 }
839
840 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
841
842 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
843 return frame;
844}
845
846static GstVideoCodecFrame *
847gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
848{
849 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
850 if (self->v4l2output->stream_mode)
851 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
852 else
853 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
854}
855
xuesong.jiangae1548e2022-05-06 16:38:46 +0800856static gboolean
857gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
858 GstStructure *structure, gpointer user_data)
859{
860 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
861 GstVideoAlignment *align = &self->v4l2capture->align;
862 GstVideoInfo *info = &self->v4l2capture->info;
863 int width, height;
864
865 if (!gst_structure_get_int(structure, "width", &width))
866 return TRUE;
867
868 if (!gst_structure_get_int(structure, "height", &height))
869 return TRUE;
870
871 if (align->padding_left != 0 || align->padding_top != 0 ||
872 height != info->height + align->padding_bottom)
873 return TRUE;
874
875 if (height == info->height + align->padding_bottom)
876 {
877 /* Some drivers may round up width to the padded with */
878 if (width == info->width + align->padding_right)
879 gst_structure_set(structure,
880 "width", G_TYPE_INT, width - align->padding_right,
881 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
882 /* Some drivers may keep visible width and only round up bytesperline */
883 else if (width == info->width)
884 gst_structure_set(structure,
885 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
886 }
887
888 return TRUE;
889}
890
891static void
sheng.liubcf036c2022-06-21 15:55:42 +0800892gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800893{
894 struct v4l2_event evt;
895 gint ret;
896
897 memset (&evt, 0x00, sizeof (struct v4l2_event));
898 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
899 if (ret < 0)
900 {
901 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
902 return;
903 }
904
905 switch (evt.type)
906 {
907 case V4L2_EVENT_SOURCE_CHANGE:
908 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
909 break;
910 case V4L2_EVENT_EOS:
911 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
912 break;
913 default:
914 break;
915 }
916
917 return;
918}
919
920static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000921gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
922{
923 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
924 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000925 struct v4l2_selection sel;
926 struct v4l2_rect *r = NULL;
927 GstStructure *s;
928 gint width = 0;
929 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800930 GST_DEBUG("%d %d",info.width, info.height);
hanghang.luo2eec4892023-07-18 06:44:42 +0000931 output_state = gst_video_decoder_set_output_state(decoder,
932 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000933 memset(&sel, 0, sizeof(struct v4l2_selection));
934 sel.type = self->v4l2capture->type;
935 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
936 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
937 {
938 r = &sel.r;
939 width = (r->width/2)*2;
940 height = (r->height/2)*2;
941 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
942 }
943 else
944 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000945 if (output_state)
946 {
947 output_state->info.interlace_mode = info.interlace_mode;
948 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000949 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000950 s = gst_caps_get_structure(output_state->caps, 0);
951 if (s)
952 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800953 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
954 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
955 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
956 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000957 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
958 gst_video_codec_state_unref(output_state);
959 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000960 }
961}
962
zengliang.lidcd41462024-06-19 16:05:12 +0800963static GQuark
964gst_aml_v4l2_buffer_pool_cc_import_quark (void)
965{
966 static GQuark quark = 0;
967
968 if (quark == 0)
969 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
970
971 return quark;
972}
973
974static gboolean
975foreach_cc_buffer_list_match_pts_func(GList *list , GstVideoCodecFrame *frame)
976{
977 GList *l;
978 if (g_list_length (list) > 0)
979 {
980 for (l = list; l != NULL; l = l->next)
981 {
982 GstBuffer *cc_buffer = l->data;
983 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
984 {
985 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
986 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
987 #if 0
988 //Debug code:dump cc data
989 GstMapInfo gst_map;
990 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
991 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
992 if (gst_map.size>0)
993 write(fd,gst_map.data,gst_map.size);
994 close(fd);
995 gst_buffer_unmap(cc_buffer,&gst_map);
996 #endif
997 GST_DEBUG("match success");
998 return TRUE;
999 }
1000 else
1001 {
1002 GST_DEBUG("match fail");
1003 }
1004 }
1005 GST_WARNING("no match frame in the bufferlist");
1006 }
1007 else
1008 {
1009 GST_WARNING("list is null,can not foreach");
1010 }
1011 return FALSE;
1012}
1013
hanghang.luo2eec4892023-07-18 06:44:42 +00001014static void
xuesong.jiangae1548e2022-05-06 16:38:46 +08001015gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
1016{
1017 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1018 GstAmlV4l2BufferPool *v4l2_pool;
1019 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1020 GstBufferPool *pool;
1021 GstVideoCodecFrame *frame;
1022 GstBuffer *buffer = NULL;
1023 GstFlowReturn ret;
1024
1025 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1026 {
1027 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001028 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001029 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001030 GST_DEBUG_OBJECT(self, "waitting source change event");
1031 /* Wait until received SOURCE_CHANGE event to get right video format */
1032 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1033 {
1034 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1035 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1036 {
fei.deng2a06e042023-10-10 03:09:45 +00001037 //let flush start event blocked until capture buffer pool actived
1038 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001039 GST_DEBUG_OBJECT(self, "Received source change event");
1040 break;
1041 }
1042 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1043 {
1044 GST_DEBUG_OBJECT(self, "Received eos event");
1045 goto beach;
1046 }
1047 else if (ret != GST_FLOW_OK)
1048 {
1049 GST_ERROR_OBJECT(self, "dqevent error");
1050 goto beach;
1051 }
1052 }
1053 self->v4l2capture->need_wait_event = FALSE;
1054
sheng.liu0c77f6c2022-06-17 21:33:20 +08001055 if (TRUE == self->v4l2output->is_svp)
1056 {
1057 GstPad *peer;
1058 GstStructure *s;
1059 GstEvent *event;
1060
1061 peer = gst_pad_get_peer (decoder->srcpad);
1062 if (peer)
1063 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001064 s = gst_structure_new_empty ("IS_SVP");
1065 if (s)
1066 {
1067 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1068 gst_pad_send_event (peer, event);
1069 GST_DEBUG_OBJECT(self, "Send SVP Event");
1070 }
1071 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001072 }
1073 }
1074
sheng.liub56bbc52022-06-21 11:02:33 +08001075 if (self->v4l2capture->need_drop_event)
1076 {
1077 // drop V4L2_EVENT_SOURCE_CHANGE
1078 gst_v4l2_drop_event(self->v4l2capture);
1079 self->v4l2capture->need_drop_event = FALSE;
1080 }
1081
xuesong.jiangae1548e2022-05-06 16:38:46 +08001082 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1083 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001084
xuesong.jiangae1548e2022-05-06 16:38:46 +08001085 /* Create caps from the acquired format, remove the format field */
1086 acquired_caps = gst_video_info_to_caps(&info);
1087 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1088 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001089 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1090
1091 /* Probe currently available pixel formats */
1092 available_caps = gst_caps_copy(self->probed_srccaps);
1093 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1094
1095 /* Replace coded size with visible size, we want to negotiate visible size
1096 * with downstream, not coded size. */
1097 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1098
1099 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001100 caps = gst_caps_copy(filter);
1101 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1102 gst_caps_append(filter, caps);
1103
1104 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1105 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001106 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001107 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1108 gst_caps_unref(filter);
1109
1110 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1111 if (gst_caps_is_empty(caps))
1112 {
1113 gst_caps_unref(caps);
1114 goto not_negotiated;
1115 }
1116
1117 /* Fixate pixel format */
1118 caps = gst_caps_fixate(caps);
1119
1120 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1121
1122 /* Try to set negotiated format, on success replace acquired format */
1123 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1124 gst_video_info_from_caps(&info, caps);
1125 else
1126 gst_aml_v4l2_clear_error(&error);
1127 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001128 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001129 if (!gst_video_decoder_negotiate(decoder))
1130 {
1131 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1132 goto flushing;
1133 else
1134 goto not_negotiated;
1135 }
1136
1137 /* Ensure our internal pool is activated */
1138 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1139 TRUE))
1140 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001141
1142 g_mutex_lock(&self->res_chg_lock);
1143 GST_LOG_OBJECT(decoder, "signal resolution changed");
1144 self->is_res_chg = FALSE;
1145 g_cond_signal(&self->res_chg_cond);
1146 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001147 }
1148
1149 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1150
1151 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1152
1153 self->output_flow = GST_FLOW_OK;
1154 do
1155 {
1156 /* We cannot use the base class allotate helper since it taking the internal
1157 * stream lock. we know that the acquire may need to poll until more frames
1158 * comes in and holding this lock would prevent that.
1159 */
1160 pool = gst_video_decoder_get_buffer_pool(decoder);
1161
1162 /* Pool may be NULL if we started going to READY state */
1163 if (pool == NULL)
1164 {
fei.dengbee20862022-06-14 14:59:48 +08001165 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001166 ret = GST_FLOW_FLUSHING;
1167 goto beach;
1168 }
1169
1170 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001171
fei.dengccc89632022-07-15 19:10:17 +08001172 //calculate a new pts for interlace stream
hanghang.luo8ec04e92023-10-09 08:14:24 +00001173 if (ret == GST_FLOW_OK && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
fei.dengccc89632022-07-15 19:10:17 +08001174 {
1175 //if buffer pts is valid, reduce 1/2 duration
1176 if (GST_BUFFER_DURATION_IS_VALID(buffer))
1177 {
1178 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
1179 }
1180 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1181 //reset pts
hanghang.luo6a5bdff2024-04-15 06:29:43 +00001182 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL)
fei.dengccc89632022-07-15 19:10:17 +08001183 {
1184 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
1185 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1186 }
1187 }
1188
xuesong.jiangae1548e2022-05-06 16:38:46 +08001189 g_object_unref(pool);
1190
fei.deng9a5cd6e2023-06-30 12:09:18 +00001191 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 +00001192 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1193 self->v4l2capture->need_drop_event = TRUE;
1194 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1195 if (self->is_res_chg) {
1196 //we must release last buffer
1197 gst_buffer_unref(buffer);
1198 //if resolution changed event received,we should set need_drop_event to false
1199 self->v4l2capture->need_drop_event = FALSE;
1200 gst_aml_v4l2_object_stop(self->v4l2capture);
1201 //unblock flush start event
1202 g_mutex_lock(&self->res_chg_lock);
1203 self->is_res_chg = FALSE;
1204 g_cond_signal(&self->res_chg_cond);
1205 g_mutex_unlock(&self->res_chg_lock);
1206 return;
1207 } else {
1208 goto beach;
1209 }
sheng.liub56bbc52022-06-21 11:02:33 +08001210 }
1211
zengliang.lidcd41462024-06-19 16:05:12 +08001212 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1213 {
1214 GST_DEBUG_OBJECT(decoder, "continue get cc data ");
1215 continue;
1216 }
1217
sheng.liu8d18ed22022-05-26 17:28:15 +08001218 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1219 {
1220 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001221
1222 g_mutex_lock (&self->res_chg_lock);
1223 self->is_res_chg = TRUE;
1224 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001225 return;
1226 }
fei.dengaf682762024-06-24 19:06:03 +08001227 //decoding error happened
1228 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1229 {
1230 continue;
1231 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001232
fei.dengbee20862022-06-14 14:59:48 +08001233 if (ret != GST_FLOW_OK) {
1234 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001235 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001236 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001237
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001238 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001239 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001240
1241 GST_DEBUG_OBJECT(decoder, "send pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1242 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1243
xuesong.jiangae1548e2022-05-06 16:38:46 +08001244 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1245 {
1246 gst_aml_v4l2_object_stop(self->v4l2capture);
1247 return;
1248 }
1249
fei.dengaf682762024-06-24 19:06:03 +08001250 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1251 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
1252 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001253
1254 if (ret != GST_FLOW_OK)
1255 goto beach;
1256
fei.dengbee20862022-06-14 14:59:48 +08001257 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001258 if (frame)
1259 {
zengliang.li32cb11e2022-11-24 12:10:26 +08001260 if (!GST_CLOCK_TIME_IS_VALID(frame->pts))
1261 {
zengliang.li92ff6822023-06-06 07:12:52 +00001262 if (!GST_CLOCK_TIME_IS_VALID(self->last_out_pts))
1263 {
1264 if (GST_CLOCK_TIME_IS_VALID(frame->dts))
1265 {
1266 GST_BUFFER_TIMESTAMP(buffer) = frame->dts;
1267 }
1268 else
1269 {
1270 GST_WARNING_OBJECT (decoder,"sorry,we have no baseline to calculate pts");
1271 goto beach;
1272 }
1273 }
1274 else
1275 {
1276 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d);
1277 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1278 }
zengliang.li32cb11e2022-11-24 12:10:26 +08001279 }
fei.dengccc89632022-07-15 19:10:17 +08001280 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001281 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001282 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1283 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001284 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001285
1286 GST_DEBUG_OBJECT (decoder,"enable_cc_data:%d",self->v4l2capture->enable_cc_data);
1287 if (self->v4l2capture->enable_cc_data)
1288 {
1289 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1290 {
1291 GST_DEBUG("cc buffer and frame bind success");
1292 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1293 GST_AML_V4L2_CC_IMPORT_QUARK);
1294 #if 0
1295 //Debug code:dump cc data
1296 GstMapInfo gst_map;
1297 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1298 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1299 if (gst_map.size>0)
1300 write(fd,gst_map.data,gst_map.size);
1301 close(fd);
1302 gst_buffer_unmap(cc_buffer,&gst_map);
1303 #endif
1304 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1305 gst_buffer_unref(cc_buffer);
1306 }
1307 else
1308 {
1309 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1310 }
1311 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001312 ret = gst_video_decoder_finish_frame(decoder, frame);
1313
1314 if (ret != GST_FLOW_OK)
1315 goto beach;
1316 }
1317 else
1318 {
1319 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
1320 gst_buffer_unref(buffer);
1321 }
1322
1323 return;
1324 /* ERRORS */
1325not_negotiated:
1326{
1327 GST_ERROR_OBJECT(self, "not negotiated");
1328 ret = GST_FLOW_NOT_NEGOTIATED;
1329 goto beach;
1330}
1331activate_failed:
1332{
1333 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1334 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1335 (_("Failed to allocate required memory.")),
1336 ("Buffer pool activation failed"));
1337 ret = GST_FLOW_ERROR;
1338 goto beach;
1339}
1340flushing:
1341{
1342 ret = GST_FLOW_FLUSHING;
1343 goto beach;
1344}
1345beach:
1346 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1347 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001348 if (self->is_res_chg) {
1349 //unblock flush start event
1350 g_mutex_lock(&self->res_chg_lock);
1351 self->is_res_chg = FALSE;
1352 g_cond_signal(&self->res_chg_cond);
1353 g_mutex_unlock(&self->res_chg_lock);
1354 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001355 gst_buffer_replace(&buffer, NULL);
1356 self->output_flow = ret;
1357 gst_aml_v4l2_object_unlock(self->v4l2output);
1358 gst_pad_pause_task(decoder->srcpad);
1359}
1360
1361static GstFlowReturn
1362gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1363 GstVideoCodecFrame *frame)
1364{
1365 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1366 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1367 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1368 GstFlowReturn ret = GST_FLOW_OK;
1369 gboolean processed = FALSE;
1370 GstBuffer *tmp;
1371 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001372
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);
xuesong.jiang26749f32024-07-24 09:40:47 +08001393 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
sheng.liu0c77f6c2022-06-17 21:33:20 +08001394 {
1395 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang26749f32024-07-24 09:40:47 +08001396 //TODO:need rm is_svp flag and just using secure_es flag
sheng.liu0c77f6c2022-06-17 21:33:20 +08001397 self->v4l2output->is_svp = TRUE;
1398 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001399
1400 GST_DEBUG_OBJECT(self, "Sending header");
1401
1402 codec_data = self->input_state->codec_data;
1403
1404 /* We are running in byte-stream mode, so we don't know the headers, but
1405 * we need to send something, otherwise the decoder will refuse to
1406 * intialize.
1407 */
1408 if (codec_data)
1409 {
1410 gst_buffer_ref(codec_data);
1411 }
1412 else
1413 {
1414 codec_data = gst_buffer_ref(frame->input_buffer);
1415 processed = TRUE;
1416 }
1417
1418 /* Ensure input internal pool is active */
1419 if (!gst_buffer_pool_is_active(pool))
1420 {
1421 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001422 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1423 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001424 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1425 // self->v4l2output->info.size, min, max);
1426 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1427
1428 /* There is no reason to refuse this config */
1429 if (!gst_buffer_pool_set_config(pool, config))
1430 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001431 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001432
1433 if (!gst_buffer_pool_set_active(pool, TRUE))
1434 goto activate_failed;
1435 }
1436
1437 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1438 ret =
1439 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001440 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001441 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1442
1443 gst_buffer_unref(codec_data);
1444
1445 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1446 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1447 * and returns the visible size as with/height and the coded size as
1448 * padding. */
1449 }
1450
1451 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1452 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1453 {
1454 /* It's possible that the processing thread stopped due to an error */
1455 if (self->output_flow != GST_FLOW_OK &&
1456 self->output_flow != GST_FLOW_FLUSHING)
1457 {
1458 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1459 ret = self->output_flow;
1460 goto drop;
1461 }
1462
1463 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1464
1465 /* Start the processing task, when it quits, the task will disable input
1466 * processing to unlock input if draining, or prevent potential block */
1467 self->output_flow = GST_FLOW_FLUSHING;
1468 if (!gst_pad_start_task(decoder->srcpad,
1469 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1470 goto start_task_failed;
1471 }
1472
1473 if (!processed)
1474 {
1475 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001476 if (!self->codec_data_inject && self->input_state->codec_data)
1477 {
1478 ret = gst_aml_v4l2_buffer_pool_process
1479 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1480 self->codec_data_inject = TRUE;
1481 if (ret != GST_FLOW_OK)
1482 goto send_codec_failed;
1483 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001484 ret =
1485 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1486 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1487
1488 if (ret == GST_FLOW_FLUSHING)
1489 {
1490 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1491 GST_TASK_STARTED)
1492 ret = self->output_flow;
1493 goto drop;
1494 }
1495 else if (ret != GST_FLOW_OK)
1496 {
1497 goto process_failed;
1498 }
1499 }
1500
1501 /* No need to keep input arround */
1502 tmp = frame->input_buffer;
1503 frame->input_buffer = gst_buffer_new();
1504 gst_buffer_copy_into(frame->input_buffer, tmp,
1505 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1506 GST_BUFFER_COPY_META,
1507 0, 0);
1508 gst_buffer_unref(tmp);
1509
1510 gst_video_codec_frame_unref(frame);
1511 return ret;
1512
1513 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001514send_codec_failed:
1515 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1516 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001517not_negotiated:
1518{
1519 GST_ERROR_OBJECT(self, "not negotiated");
1520 ret = GST_FLOW_NOT_NEGOTIATED;
1521 gst_aml_v4l2_error(self, &error);
1522 goto drop;
1523}
1524activate_failed:
1525{
1526 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1527 (_("Failed to allocate required memory.")),
1528 ("Buffer pool activation failed"));
1529 ret = GST_FLOW_ERROR;
1530 goto drop;
1531}
1532flushing:
1533{
1534 ret = GST_FLOW_FLUSHING;
1535 goto drop;
1536}
1537
1538start_task_failed:
1539{
1540 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1541 (_("Failed to start decoding thread.")), (NULL));
1542 ret = GST_FLOW_ERROR;
1543 goto drop;
1544}
1545process_failed:
1546{
1547 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1548 (_("Failed to process frame.")),
1549 ("Maybe be due to not enough memory or failing driver"));
1550 ret = GST_FLOW_ERROR;
1551 goto drop;
1552}
1553drop:
1554{
1555 gst_video_decoder_drop_frame(decoder, frame);
1556 return ret;
1557}
1558}
1559
1560static gboolean
1561gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1562 GstQuery *query)
1563{
1564 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1565 GstClockTime latency;
1566 gboolean ret = FALSE;
1567
1568 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1569 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1570
1571 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1572 {
1573 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1574 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1575 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1576 gst_video_decoder_set_latency(decoder, latency, latency);
1577 }
1578 else
1579 {
1580 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1581 }
1582
1583 return ret;
1584}
1585
1586static gboolean
1587gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1588{
1589 gboolean ret = TRUE;
1590 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1591
1592 switch (GST_QUERY_TYPE(query))
1593 {
1594 case GST_QUERY_CAPS:
1595 {
1596 GstCaps *filter, *result = NULL;
1597 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1598
1599 gst_query_parse_caps(query, &filter);
1600
1601 if (self->probed_srccaps)
1602 result = gst_caps_ref(self->probed_srccaps);
1603 else
1604 result = gst_pad_get_pad_template_caps(pad);
1605
1606 if (filter)
1607 {
1608 GstCaps *tmp = result;
1609 result =
1610 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1611 gst_caps_unref(tmp);
1612 }
1613
1614 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1615
1616 gst_query_set_caps_result(query, result);
1617 gst_caps_unref(result);
1618 break;
1619 }
1620
1621 default:
1622 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1623 break;
1624 }
1625
1626 return ret;
1627}
1628
1629static GstCaps *
1630gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1631{
1632 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1633 GstCaps *result;
1634
1635 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1636 filter);
1637
1638 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1639
1640 return result;
1641}
1642
1643static gboolean
1644gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1645{
1646 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1647 gboolean ret;
1648 GstEventType type = GST_EVENT_TYPE(event);
1649
1650 switch (type)
1651 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001652 case GST_EVENT_STREAM_START:
1653 {
1654 GstStructure *s;
1655 GstEvent *event;
1656 GST_DEBUG_OBJECT(self, "new private event");
1657 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1658 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1659 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1660 gst_pad_push_event (decoder->sinkpad, event);
1661 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1662 break;
1663 }
zengliang.li51f54b62023-10-10 12:14:49 +00001664 case GST_EVENT_CAPS:
1665 {
1666 GstCaps *caps;
1667 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001668 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001669
1670 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001671
1672 GST_DEBUG_OBJECT(self, "event caps %" GST_PTR_FORMAT, caps);
1673
zengliang.li51f54b62023-10-10 12:14:49 +00001674 structure= gst_caps_get_structure(caps, 0);
1675 if ( gst_structure_has_field(structure, "parsed") )
1676 {
1677 gboolean parsed = TRUE;
1678 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1679 {
1680 self->v4l2output->stream_mode = !parsed;
1681 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1682 }
1683 }
sheng.liu641aa422023-12-26 07:05:59 +00001684
xuesong.jiang26749f32024-07-24 09:40:47 +08001685 if ( gst_structure_has_field(structure, "secure") )
1686 {
1687 gboolean is_secure = FALSE;
1688 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1689 {
1690 self->v4l2output->secure_es = is_secure;
1691 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1692 }
1693 }
1694
sheng.liudb26f7d2024-01-22 11:24:23 +00001695 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1696 {
1697 if ( denom == 0 ) denom= 1;
1698
1699 if (self->v4l2capture->fps)
1700 {
1701 g_value_unset(self->v4l2capture->fps);
1702 g_free(self->v4l2capture->fps);
1703 }
1704
1705 self->v4l2capture->fps = g_new0(GValue, 1);
1706 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1707 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1708
1709 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1710 }
1711
sheng.liu641aa422023-12-26 07:05:59 +00001712 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1713 ( !self->v4l2capture->have_set_par ) )
1714 {
1715 if ( (num <= 0) || (denom <= 0) )
1716 {
1717 num= denom= 1;
1718 }
1719
1720 if ( self->v4l2capture->par )
1721 {
1722 g_value_unset(self->v4l2capture->par);
1723 g_free(self->v4l2capture->par);
1724 }
1725
1726 self->v4l2capture->par = g_new0(GValue, 1);
1727 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1728 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1729 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1730 }
zengliang.li51f54b62023-10-10 12:14:49 +00001731 break;
1732 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001733 case GST_EVENT_FLUSH_START:
1734 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001735
1736 g_mutex_lock (&self->res_chg_lock);
1737 while (self->is_res_chg)
1738 {
1739 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1740 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1741 }
1742 g_mutex_unlock (&self->res_chg_lock);
1743
zengliang.li92ff6822023-06-06 07:12:52 +00001744 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001745 gst_aml_v4l2_object_unlock(self->v4l2output);
1746 gst_aml_v4l2_object_unlock(self->v4l2capture);
1747 break;
1748 default:
1749 break;
1750 }
1751
1752 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1753
1754 switch (type)
1755 {
1756 case GST_EVENT_FLUSH_START:
1757 /* The processing thread should stop now, wait for it */
1758 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001759 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001760 GST_DEBUG_OBJECT(self, "flush start done");
1761 break;
1762 default:
1763 break;
1764 }
1765
1766 return ret;
1767}
1768
1769static GstStateChangeReturn
1770gst_aml_v4l2_video_dec_change_state(GstElement *element,
1771 GstStateChange transition)
1772{
1773 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1774 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1775
1776 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1777 {
1778 g_atomic_int_set(&self->active, FALSE);
1779 gst_aml_v4l2_object_unlock(self->v4l2output);
1780 gst_aml_v4l2_object_unlock(self->v4l2capture);
1781 gst_pad_stop_task(decoder->srcpad);
1782 }
1783
1784 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1785}
1786
1787static void
1788gst_aml_v4l2_video_dec_dispose(GObject *object)
1789{
1790 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1791
1792 gst_caps_replace(&self->probed_sinkcaps, NULL);
1793 gst_caps_replace(&self->probed_srccaps, NULL);
1794
1795 G_OBJECT_CLASS(parent_class)->dispose(object);
1796}
1797
1798static void
1799gst_aml_v4l2_video_dec_finalize(GObject *object)
1800{
1801 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1802
1803 gst_aml_v4l2_object_destroy(self->v4l2capture);
1804 gst_aml_v4l2_object_destroy(self->v4l2output);
1805
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001806 g_mutex_clear(&self->res_chg_lock);
1807 g_cond_clear(&self->res_chg_cond);
1808
xuesong.jiang61ea8012022-05-12 15:38:17 +08001809#if GST_IMPORT_LGE_PROP
1810 if (self->lge_ctxt)
1811 {
1812 if (self->lge_ctxt->app_type)
1813 g_free(self->lge_ctxt->app_type);
1814 if (self->lge_ctxt->res_info.coretype)
1815 g_free(self->lge_ctxt->res_info.coretype);
1816 free(self->lge_ctxt);
1817 }
1818
1819#endif
1820
xuesong.jiangae1548e2022-05-06 16:38:46 +08001821 G_OBJECT_CLASS(parent_class)->finalize(object);
1822}
1823
1824static void
1825gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1826{
1827 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001828 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001829 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001830 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001831 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001832 g_mutex_init(&self->res_chg_lock);
1833 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001834#if GST_IMPORT_LGE_PROP
1835 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1836 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1837#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001838}
1839
1840static void
1841gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1842{
1843 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1844 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1845 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1846
1847 gst_video_decoder_set_packetized(decoder, TRUE);
1848
1849 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1850 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1851 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1852 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1853 self->v4l2output->no_initial_format = TRUE;
1854 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001855 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001856
1857 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1858 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1859 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1860 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1861 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001862 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001863}
1864
1865static void
1866gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1867{
1868 GstElementClass *element_class;
1869 GObjectClass *gobject_class;
1870 GstVideoDecoderClass *video_decoder_class;
1871
1872 parent_class = g_type_class_peek_parent(klass);
1873
1874 element_class = (GstElementClass *)klass;
1875 gobject_class = (GObjectClass *)klass;
1876 video_decoder_class = (GstVideoDecoderClass *)klass;
1877
1878 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1879 "AML V4L2 Video Decoder");
1880
1881 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1882 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1883 gobject_class->set_property =
1884 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1885 gobject_class->get_property =
1886 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1887
1888 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1889 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1890 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1891 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1892 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1893 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1894 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1895 video_decoder_class->set_format =
1896 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1897 video_decoder_class->negotiate =
1898 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1899 video_decoder_class->decide_allocation =
1900 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1901 /* FIXME propose_allocation or not ? */
1902 video_decoder_class->handle_frame =
1903 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1904 video_decoder_class->getcaps =
1905 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1906 video_decoder_class->src_query =
1907 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1908 video_decoder_class->sink_event =
1909 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1910
1911 element_class->change_state =
1912 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1913
xuesong.jiang406ee302023-06-28 03:45:22 +00001914 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1915 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1916 G_SIGNAL_RUN_LAST,
1917 0, /* class offset */
1918 NULL, /* accumulator */
1919 NULL, /* accu data */
1920 g_cclosure_marshal_generic,
1921 G_TYPE_NONE,
1922 1,
1923 G_TYPE_UINT64);
1924
xuesong.jiangae1548e2022-05-06 16:38:46 +08001925 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001926#if GST_IMPORT_LGE_PROP
1927 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1928#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001929}
1930
1931static void
1932gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1933{
1934 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1935 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1936 GstAmlV4l2VideoDecCData *cdata = data;
1937
1938 klass->default_device = cdata->device;
1939
1940 /* Note: gst_pad_template_new() take the floating ref from the caps */
1941 gst_element_class_add_pad_template(element_class,
1942 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1943 cdata->sink_caps));
1944 gst_element_class_add_pad_template(element_class,
1945 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1946 cdata->src_caps));
1947
1948 gst_element_class_set_metadata(element_class, cdata->longname,
1949 "Codec/Decoder/Video/Hardware", cdata->description,
1950 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1951
1952 gst_caps_unref(cdata->sink_caps);
1953 gst_caps_unref(cdata->src_caps);
1954 g_free(cdata);
1955}
1956
1957/* Probing functions */
1958gboolean
1959gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1960{
1961 gboolean ret = FALSE;
1962
1963 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()))
1964 ret = TRUE;
1965
1966 return ret;
1967}
1968
1969static gchar *
1970gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1971 const gchar *basename)
1972{
1973 gchar *codec_name = NULL;
1974 gchar *type_name = NULL;
1975 gboolean got_value = FALSE;
1976
1977#define SET_META(codec) \
1978 G_STMT_START \
1979 { \
1980 cdata->longname = "AML V4L2 " codec " Decoder"; \
1981 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1982 codec_name = g_ascii_strdown(codec, -1); \
1983 } \
1984 G_STMT_END
1985
bo.xiaoce116a42024-07-16 16:21:53 +08001986 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001987 {
bo.xiaoce116a42024-07-16 16:21:53 +08001988 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001989 }
1990 else if (gst_structure_has_name(s, "video/mpeg"))
1991 {
1992 gint mpegversion = 0;
1993 gint *list = NULL;
1994 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1995 if (FALSE == got_value)
1996 {
1997 got_value = gst_structure_get_list(s, "mpegversion", &list);
1998 if (TRUE == got_value && (1 == *list || 2 == *list))
1999 {
2000 SET_META("MPEG2");
2001 }
2002 else
2003 {
2004 SET_META("MPEG4");
2005 }
2006 }
2007 else
2008 {
2009 SET_META("MPEG4");
2010 }
2011 }
2012 else if (gst_structure_has_name(s, "video/x-h263"))
2013 {
2014 SET_META("H263");
2015 }
2016 else if (gst_structure_has_name(s, "video/x-fwht"))
2017 {
2018 SET_META("FWHT");
2019 }
2020 else if (gst_structure_has_name(s, "video/x-h264"))
2021 {
2022 SET_META("H264");
2023 }
2024 else if (gst_structure_has_name(s, "video/x-h265"))
2025 {
2026 SET_META("H265");
2027 }
2028 else if (gst_structure_has_name(s, "video/x-wmv"))
2029 {
2030 SET_META("VC1");
2031 }
2032 else if (gst_structure_has_name(s, "video/x-vp8"))
2033 {
2034 SET_META("VP8");
2035 }
2036 else if (gst_structure_has_name(s, "video/x-vp9"))
2037 {
2038 SET_META("VP9");
2039 }
2040 else if (gst_structure_has_name(s, "video/x-av1"))
2041 {
2042 SET_META("AV1");
2043 }
zengliang.li51f54b62023-10-10 12:14:49 +00002044 else if (gst_structure_has_name(s, "video/x-avs"))
2045 {
2046 SET_META("AVS");
2047 }
2048 else if (gst_structure_has_name(s, "video/x-avs2"))
2049 {
2050 SET_META("AVS2");
2051 }
2052 else if (gst_structure_has_name(s, "video/x-avs3"))
2053 {
2054 SET_META("AVS3");
2055 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002056 else if (gst_structure_has_name(s, "video/x-bayer"))
2057 {
2058 SET_META("BAYER");
2059 }
2060 else if (gst_structure_has_name(s, "video/x-sonix"))
2061 {
2062 SET_META("SONIX");
2063 }
2064 else if (gst_structure_has_name(s, "video/x-pwc1"))
2065 {
2066 SET_META("PWC1");
2067 }
2068 else if (gst_structure_has_name(s, "video/x-pwc2"))
2069 {
2070 SET_META("PWC2");
2071 }
2072 else
2073 {
2074 /* This code should be kept on sync with the exposed CODEC type of format
2075 * from gstamlv4l2object.c. This warning will only occure in case we forget
2076 * to also add a format here. */
2077 gchar *s_str = gst_structure_to_string(s);
2078 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2079 "bug, please report at https://bugs.gnome.org",
2080 s_str);
2081 g_free(s_str);
2082 }
2083
2084 if (codec_name)
2085 {
2086 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2087 if (g_type_from_name(type_name) != 0)
2088 {
2089 g_free(type_name);
2090 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2091 }
2092
2093 g_free(codec_name);
2094 }
2095
2096 return type_name;
2097#undef SET_META
2098}
2099
2100void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2101 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2102{
2103 gint i;
2104
2105 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2106 {
2107 GstAmlV4l2VideoDecCData *cdata;
2108 GstStructure *s;
2109 GTypeQuery type_query;
2110 GTypeInfo type_info = {
2111 0,
2112 };
2113 GType type, subtype;
2114 gchar *type_name;
2115
2116 s = gst_caps_get_structure(sink_caps, i);
2117
2118 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2119 cdata->device = g_strdup(device_path);
2120 cdata->sink_caps = gst_caps_new_empty();
2121 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2122 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2123 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2124 cdata->src_caps = gst_caps_copy(src_caps);
2125 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2126 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2127 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2128
2129 /* Skip over if we hit an unmapped type */
2130 if (!type_name)
2131 {
2132 g_free(cdata);
2133 continue;
2134 }
2135
2136 type = gst_aml_v4l2_video_dec_get_type();
2137 g_type_query(type, &type_query);
2138 memset(&type_info, 0, sizeof(type_info));
2139 type_info.class_size = type_query.class_size;
2140 type_info.instance_size = type_query.instance_size;
2141 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2142 type_info.class_data = cdata;
2143 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2144
2145 subtype = g_type_register_static(type, type_name, &type_info, 0);
2146 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2147 subtype))
2148 GST_WARNING("Failed to register plugin '%s'", type_name);
2149
2150 g_free(type_name);
2151 }
2152}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002153
2154#if GST_IMPORT_LGE_PROP
2155static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2156{
2157 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2158 g_param_spec_object("resource-info", "resource-info",
2159 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2160 GST_TYPE_STRUCTURE,
2161 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2162
2163 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2164 g_param_spec_uint64("decoded-size", "decoded-size",
2165 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2166 0, G_MAXUINT64,
2167 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2168
2169 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2170 g_param_spec_uint64("undecoded-size", "undecoded-size",
2171 "video decoder element's total undecoded data update unit is byte.",
2172 0, G_MAXUINT64,
2173 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2174
2175 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2176 g_param_spec_string("app-type", "app-type",
2177 "set application type.",
2178 "default_app",
2179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2180
2181 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2182 g_param_spec_boolean("clip-mode", "clip-mode",
2183 "When seeking, Content is moving faster for a while to skip frames.",
2184 FALSE,
2185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2186}
2187#endif