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