blob: 4e2cd6203ee3374d6650506dcf833e8d8d563c55 [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
fei.deng4244a262024-07-31 14:07:14 +0800685 /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop.
686 should reset need_wait_event=true to wait source change event*/
687 self->v4l2capture->need_wait_event = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800688 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
689
690 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
691
692done:
693 return ret;
694}
695
696static GstFlowReturn
697gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
698{
699 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
700
701 GST_DEBUG_OBJECT(self, "Draining...");
702 gst_aml_v4l2_video_dec_finish(decoder);
703 gst_aml_v4l2_video_dec_flush(decoder);
704
705 return GST_FLOW_OK;
706}
707
708static GstVideoCodecFrame *
709gst_aml_v4l2_video_dec_get_oldest_frame(GstVideoDecoder *decoder)
710{
711 GstVideoCodecFrame *frame = NULL;
712 GList *frames, *l;
713 gint count = 0;
714
715 frames = gst_video_decoder_get_frames(decoder);
716
717 for (l = frames; l != NULL; l = l->next)
718 {
719 GstVideoCodecFrame *f = l->data;
720
721 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
722 frame = f;
723
724 count++;
725 }
726
727 if (frame)
728 {
729 GST_LOG_OBJECT(decoder,
730 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
731 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
732 gst_video_codec_frame_ref(frame);
733 }
734
735 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
736
737 return frame;
738}
739
fei.dengbee20862022-06-14 14:59:48 +0800740static GstVideoCodecFrame *
xuesong.jiange24aef92023-06-16 06:39:10 +0000741gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800742{
743 GstVideoCodecFrame *frame = NULL;
744 GList *frames, *l;
745 gint count = 0;
746
xuesong.jiange24aef92023-06-16 06:39:10 +0000747 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
748
fei.dengbee20862022-06-14 14:59:48 +0800749 frames = gst_video_decoder_get_frames(decoder);
750
751 for (l = frames; l != NULL; l = l->next)
752 {
753 GstVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000754
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800755 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800756 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800757 }
fei.dengbee20862022-06-14 14:59:48 +0800758 count++;
759 }
760
zengliang.liddee2da2023-07-14 07:27:05 +0000761 if (!frame)
762 {
763 for (l = frames; l != NULL; l = l->next)
764 {
765 GstVideoCodecFrame *f = l->data;
766 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
767 {
768 frame = f;
fei.dengf78f7122024-07-15 14:44:04 +0800769 GST_DEBUG("The pts of the expected output frame is invalid");
770 break;
zengliang.liddee2da2023-07-14 07:27:05 +0000771 }
zengliang.liddee2da2023-07-14 07:27:05 +0000772 }
773 }
774
fei.dengbee20862022-06-14 14:59:48 +0800775 if (frame)
776 {
777 GST_LOG_OBJECT(decoder,
778 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
779 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
780 gst_video_codec_frame_ref(frame);
781 }
782
783 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
784
xuesong.jiange24aef92023-06-16 06:39:10 +0000785 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
fei.dengbee20862022-06-14 14:59:48 +0800786 return frame;
787}
788
xuesong.jiange24aef92023-06-16 06:39:10 +0000789static GstVideoCodecFrame *
790gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstVideoDecoder *decoder, GstClockTime pts)
791{
792 GstVideoCodecFrame *frame = NULL;
793 GList *frames, *l;
hanghang.luo4486d7d2024-07-24 10:06:35 +0800794 guint frames_len = 0;
xuesong.jiange24aef92023-06-16 06:39:10 +0000795 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
796
hanghang.luo4486d7d2024-07-24 10:06:35 +0800797 if (!(frames = gst_video_decoder_get_frames(decoder)))
798 goto done;
799
xuesong.jiange24aef92023-06-16 06:39:10 +0000800 frames_len = g_list_length(frames);
801 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
802
xuesong.jiange24aef92023-06-16 06:39:10 +0000803 for (l = frames; l != NULL; l = l->next)
804 {
805 GstVideoCodecFrame *f = l->data;
806
807 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
808 {
809 /* found the right frame */
810 frame = f;
811 break;
812 }
813 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
814 {
815 GST_LOG_OBJECT(decoder,
816 "stream mode drop frame %d %" GST_TIME_FORMAT,
hanghang.luoa0626d22024-07-30 16:10:10 +0800817 f->system_frame_number, GST_TIME_ARGS(f->pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000818
819 gst_video_codec_frame_ref(f);
820 // gst_video_decoder_drop_frame(decoder, f);
821 gst_video_decoder_release_frame(decoder, f);
822 }
823 else
824 {
825 GST_LOG_OBJECT (decoder, "dbg");
826 }
827 }
828
829 if (frame)
830 {
831 guint l_len = 0;
832 l = gst_video_decoder_get_frames(decoder);
833 l_len = g_list_length(l);
834 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
835
836 GST_LOG_OBJECT(decoder,
837 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
838 frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
839 gst_video_codec_frame_ref(frame);
840 }
841
842 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
843
hanghang.luo4486d7d2024-07-24 10:06:35 +0800844done:
xuesong.jiange24aef92023-06-16 06:39:10 +0000845 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
846 return frame;
847}
848
849static GstVideoCodecFrame *
850gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
851{
852 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
853 if (self->v4l2output->stream_mode)
854 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
855 else
856 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
857}
858
xuesong.jiangae1548e2022-05-06 16:38:46 +0800859static gboolean
860gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
861 GstStructure *structure, gpointer user_data)
862{
863 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
864 GstVideoAlignment *align = &self->v4l2capture->align;
865 GstVideoInfo *info = &self->v4l2capture->info;
866 int width, height;
867
868 if (!gst_structure_get_int(structure, "width", &width))
869 return TRUE;
870
871 if (!gst_structure_get_int(structure, "height", &height))
872 return TRUE;
873
874 if (align->padding_left != 0 || align->padding_top != 0 ||
875 height != info->height + align->padding_bottom)
876 return TRUE;
877
878 if (height == info->height + align->padding_bottom)
879 {
880 /* Some drivers may round up width to the padded with */
881 if (width == info->width + align->padding_right)
882 gst_structure_set(structure,
883 "width", G_TYPE_INT, width - align->padding_right,
884 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
885 /* Some drivers may keep visible width and only round up bytesperline */
886 else if (width == info->width)
887 gst_structure_set(structure,
888 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
889 }
890
891 return TRUE;
892}
893
894static void
sheng.liubcf036c2022-06-21 15:55:42 +0800895gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800896{
897 struct v4l2_event evt;
898 gint ret;
899
900 memset (&evt, 0x00, sizeof (struct v4l2_event));
901 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
902 if (ret < 0)
903 {
904 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
905 return;
906 }
907
908 switch (evt.type)
909 {
910 case V4L2_EVENT_SOURCE_CHANGE:
911 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
912 break;
913 case V4L2_EVENT_EOS:
914 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
915 break;
916 default:
917 break;
918 }
919
920 return;
921}
922
923static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000924gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
925{
926 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
927 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000928 struct v4l2_selection sel;
929 struct v4l2_rect *r = NULL;
930 GstStructure *s;
931 gint width = 0;
932 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800933 GST_DEBUG("%d %d",info.width, info.height);
hanghang.luo2eec4892023-07-18 06:44:42 +0000934 output_state = gst_video_decoder_set_output_state(decoder,
935 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000936 memset(&sel, 0, sizeof(struct v4l2_selection));
937 sel.type = self->v4l2capture->type;
938 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
939 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
940 {
941 r = &sel.r;
942 width = (r->width/2)*2;
943 height = (r->height/2)*2;
944 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
945 }
946 else
947 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000948 if (output_state)
949 {
950 output_state->info.interlace_mode = info.interlace_mode;
951 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000952 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000953 s = gst_caps_get_structure(output_state->caps, 0);
954 if (s)
955 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800956 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
957 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
958 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
959 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000960 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
961 gst_video_codec_state_unref(output_state);
962 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000963 }
964}
965
zengliang.lidcd41462024-06-19 16:05:12 +0800966static GQuark
967gst_aml_v4l2_buffer_pool_cc_import_quark (void)
968{
969 static GQuark quark = 0;
970
971 if (quark == 0)
972 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
973
974 return quark;
975}
976
977static gboolean
978foreach_cc_buffer_list_match_pts_func(GList *list , GstVideoCodecFrame *frame)
979{
980 GList *l;
981 if (g_list_length (list) > 0)
982 {
983 for (l = list; l != NULL; l = l->next)
984 {
985 GstBuffer *cc_buffer = l->data;
986 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
987 {
988 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
989 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
990 #if 0
991 //Debug code:dump cc data
992 GstMapInfo gst_map;
993 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
994 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
995 if (gst_map.size>0)
996 write(fd,gst_map.data,gst_map.size);
997 close(fd);
998 gst_buffer_unmap(cc_buffer,&gst_map);
999 #endif
1000 GST_DEBUG("match success");
1001 return TRUE;
1002 }
1003 else
1004 {
1005 GST_DEBUG("match fail");
1006 }
1007 }
1008 GST_WARNING("no match frame in the bufferlist");
1009 }
1010 else
1011 {
1012 GST_WARNING("list is null,can not foreach");
1013 }
1014 return FALSE;
1015}
1016
hanghang.luo2eec4892023-07-18 06:44:42 +00001017static void
xuesong.jiangae1548e2022-05-06 16:38:46 +08001018gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
1019{
1020 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1021 GstAmlV4l2BufferPool *v4l2_pool;
1022 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1023 GstBufferPool *pool;
1024 GstVideoCodecFrame *frame;
1025 GstBuffer *buffer = NULL;
1026 GstFlowReturn ret;
1027
1028 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1029 {
1030 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001031 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001032 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001033 GST_DEBUG_OBJECT(self, "waitting source change event");
1034 /* Wait until received SOURCE_CHANGE event to get right video format */
1035 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1036 {
1037 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1038 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1039 {
fei.deng2a06e042023-10-10 03:09:45 +00001040 //let flush start event blocked until capture buffer pool actived
1041 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001042 GST_DEBUG_OBJECT(self, "Received source change event");
1043 break;
1044 }
1045 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1046 {
1047 GST_DEBUG_OBJECT(self, "Received eos event");
1048 goto beach;
1049 }
1050 else if (ret != GST_FLOW_OK)
1051 {
1052 GST_ERROR_OBJECT(self, "dqevent error");
1053 goto beach;
1054 }
1055 }
1056 self->v4l2capture->need_wait_event = FALSE;
1057
sheng.liu0c77f6c2022-06-17 21:33:20 +08001058 if (TRUE == self->v4l2output->is_svp)
1059 {
1060 GstPad *peer;
1061 GstStructure *s;
1062 GstEvent *event;
1063
1064 peer = gst_pad_get_peer (decoder->srcpad);
1065 if (peer)
1066 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001067 s = gst_structure_new_empty ("IS_SVP");
1068 if (s)
1069 {
1070 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1071 gst_pad_send_event (peer, event);
1072 GST_DEBUG_OBJECT(self, "Send SVP Event");
1073 }
1074 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001075 }
1076 }
1077
sheng.liub56bbc52022-06-21 11:02:33 +08001078 if (self->v4l2capture->need_drop_event)
1079 {
1080 // drop V4L2_EVENT_SOURCE_CHANGE
1081 gst_v4l2_drop_event(self->v4l2capture);
1082 self->v4l2capture->need_drop_event = FALSE;
1083 }
1084
xuesong.jiangae1548e2022-05-06 16:38:46 +08001085 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1086 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001087
xuesong.jiangae1548e2022-05-06 16:38:46 +08001088 /* Create caps from the acquired format, remove the format field */
1089 acquired_caps = gst_video_info_to_caps(&info);
1090 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1091 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001092 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1093
1094 /* Probe currently available pixel formats */
1095 available_caps = gst_caps_copy(self->probed_srccaps);
1096 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1097
1098 /* Replace coded size with visible size, we want to negotiate visible size
1099 * with downstream, not coded size. */
1100 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1101
1102 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001103 caps = gst_caps_copy(filter);
1104 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1105 gst_caps_append(filter, caps);
1106
1107 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1108 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001109 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001110 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1111 gst_caps_unref(filter);
1112
1113 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1114 if (gst_caps_is_empty(caps))
1115 {
1116 gst_caps_unref(caps);
1117 goto not_negotiated;
1118 }
1119
1120 /* Fixate pixel format */
1121 caps = gst_caps_fixate(caps);
1122
1123 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1124
1125 /* Try to set negotiated format, on success replace acquired format */
1126 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1127 gst_video_info_from_caps(&info, caps);
1128 else
1129 gst_aml_v4l2_clear_error(&error);
1130 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001131 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001132 if (!gst_video_decoder_negotiate(decoder))
1133 {
1134 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1135 goto flushing;
1136 else
1137 goto not_negotiated;
1138 }
1139
1140 /* Ensure our internal pool is activated */
1141 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1142 TRUE))
1143 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001144
1145 g_mutex_lock(&self->res_chg_lock);
1146 GST_LOG_OBJECT(decoder, "signal resolution changed");
1147 self->is_res_chg = FALSE;
1148 g_cond_signal(&self->res_chg_cond);
1149 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001150 }
1151
1152 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1153
1154 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1155
1156 self->output_flow = GST_FLOW_OK;
1157 do
1158 {
1159 /* We cannot use the base class allotate helper since it taking the internal
1160 * stream lock. we know that the acquire may need to poll until more frames
1161 * comes in and holding this lock would prevent that.
1162 */
1163 pool = gst_video_decoder_get_buffer_pool(decoder);
1164
1165 /* Pool may be NULL if we started going to READY state */
1166 if (pool == NULL)
1167 {
fei.dengbee20862022-06-14 14:59:48 +08001168 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001169 ret = GST_FLOW_FLUSHING;
1170 goto beach;
1171 }
1172
1173 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001174
fei.dengccc89632022-07-15 19:10:17 +08001175 //calculate a new pts for interlace stream
hanghang.luo8ec04e92023-10-09 08:14:24 +00001176 if (ret == GST_FLOW_OK && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
fei.dengccc89632022-07-15 19:10:17 +08001177 {
1178 //if buffer pts is valid, reduce 1/2 duration
1179 if (GST_BUFFER_DURATION_IS_VALID(buffer))
1180 {
1181 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
1182 }
1183 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1184 //reset pts
hanghang.luo6a5bdff2024-04-15 06:29:43 +00001185 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL)
fei.dengccc89632022-07-15 19:10:17 +08001186 {
hanghang.luoa0626d22024-07-30 16:10:10 +08001187 if (self->input_state->info.fps_n !=0 && self->input_state->info.fps_d !=0)
1188 {
1189 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
1190 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1191 }
fei.dengccc89632022-07-15 19:10:17 +08001192 }
1193 }
1194
xuesong.jiangae1548e2022-05-06 16:38:46 +08001195 g_object_unref(pool);
1196
fei.deng9a5cd6e2023-06-30 12:09:18 +00001197 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 +00001198 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1199 self->v4l2capture->need_drop_event = TRUE;
1200 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1201 if (self->is_res_chg) {
1202 //we must release last buffer
1203 gst_buffer_unref(buffer);
1204 //if resolution changed event received,we should set need_drop_event to false
1205 self->v4l2capture->need_drop_event = FALSE;
1206 gst_aml_v4l2_object_stop(self->v4l2capture);
1207 //unblock flush start event
1208 g_mutex_lock(&self->res_chg_lock);
1209 self->is_res_chg = FALSE;
1210 g_cond_signal(&self->res_chg_cond);
1211 g_mutex_unlock(&self->res_chg_lock);
1212 return;
1213 } else {
1214 goto beach;
1215 }
sheng.liub56bbc52022-06-21 11:02:33 +08001216 }
1217
zengliang.lidcd41462024-06-19 16:05:12 +08001218 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1219 {
1220 GST_DEBUG_OBJECT(decoder, "continue get cc data ");
1221 continue;
1222 }
1223
sheng.liu8d18ed22022-05-26 17:28:15 +08001224 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1225 {
1226 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001227
1228 g_mutex_lock (&self->res_chg_lock);
1229 self->is_res_chg = TRUE;
1230 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001231 return;
1232 }
fei.dengaf682762024-06-24 19:06:03 +08001233 //decoding error happened
1234 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1235 {
1236 continue;
1237 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001238
fei.dengbee20862022-06-14 14:59:48 +08001239 if (ret != GST_FLOW_OK) {
1240 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001241 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001242 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001243
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001244 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001245 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001246
1247 GST_DEBUG_OBJECT(decoder, "send pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1248 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1249
xuesong.jiangae1548e2022-05-06 16:38:46 +08001250 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1251 {
1252 gst_aml_v4l2_object_stop(self->v4l2capture);
1253 return;
1254 }
1255
fei.dengaf682762024-06-24 19:06:03 +08001256 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1257 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
1258 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001259
1260 if (ret != GST_FLOW_OK)
1261 goto beach;
1262
fei.dengbee20862022-06-14 14:59:48 +08001263 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001264 if (frame)
1265 {
zengliang.li32cb11e2022-11-24 12:10:26 +08001266 if (!GST_CLOCK_TIME_IS_VALID(frame->pts))
1267 {
zengliang.li92ff6822023-06-06 07:12:52 +00001268 if (!GST_CLOCK_TIME_IS_VALID(self->last_out_pts))
1269 {
1270 if (GST_CLOCK_TIME_IS_VALID(frame->dts))
1271 {
1272 GST_BUFFER_TIMESTAMP(buffer) = frame->dts;
1273 }
1274 else
1275 {
1276 GST_WARNING_OBJECT (decoder,"sorry,we have no baseline to calculate pts");
1277 goto beach;
1278 }
1279 }
1280 else
1281 {
1282 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d);
1283 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1284 }
zengliang.li32cb11e2022-11-24 12:10:26 +08001285 }
fei.dengccc89632022-07-15 19:10:17 +08001286 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001287 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001288 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1289 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001290 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001291
1292 GST_DEBUG_OBJECT (decoder,"enable_cc_data:%d",self->v4l2capture->enable_cc_data);
1293 if (self->v4l2capture->enable_cc_data)
1294 {
1295 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1296 {
1297 GST_DEBUG("cc buffer and frame bind success");
1298 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1299 GST_AML_V4L2_CC_IMPORT_QUARK);
1300 #if 0
1301 //Debug code:dump cc data
1302 GstMapInfo gst_map;
1303 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1304 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1305 if (gst_map.size>0)
1306 write(fd,gst_map.data,gst_map.size);
1307 close(fd);
1308 gst_buffer_unmap(cc_buffer,&gst_map);
1309 #endif
1310 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1311 gst_buffer_unref(cc_buffer);
1312 }
1313 else
1314 {
1315 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1316 }
1317 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001318 ret = gst_video_decoder_finish_frame(decoder, frame);
1319
1320 if (ret != GST_FLOW_OK)
1321 goto beach;
1322 }
1323 else
1324 {
1325 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
1326 gst_buffer_unref(buffer);
1327 }
1328
1329 return;
1330 /* ERRORS */
1331not_negotiated:
1332{
1333 GST_ERROR_OBJECT(self, "not negotiated");
1334 ret = GST_FLOW_NOT_NEGOTIATED;
1335 goto beach;
1336}
1337activate_failed:
1338{
1339 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1340 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1341 (_("Failed to allocate required memory.")),
1342 ("Buffer pool activation failed"));
1343 ret = GST_FLOW_ERROR;
1344 goto beach;
1345}
1346flushing:
1347{
1348 ret = GST_FLOW_FLUSHING;
1349 goto beach;
1350}
1351beach:
1352 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1353 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001354 if (self->is_res_chg) {
1355 //unblock flush start event
1356 g_mutex_lock(&self->res_chg_lock);
1357 self->is_res_chg = FALSE;
1358 g_cond_signal(&self->res_chg_cond);
1359 g_mutex_unlock(&self->res_chg_lock);
1360 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001361 gst_buffer_replace(&buffer, NULL);
1362 self->output_flow = ret;
1363 gst_aml_v4l2_object_unlock(self->v4l2output);
1364 gst_pad_pause_task(decoder->srcpad);
1365}
1366
1367static GstFlowReturn
1368gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1369 GstVideoCodecFrame *frame)
1370{
1371 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1372 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1373 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1374 GstFlowReturn ret = GST_FLOW_OK;
1375 gboolean processed = FALSE;
1376 GstBuffer *tmp;
1377 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001378
1379 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1380
1381 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1382 goto flushing;
1383
1384 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1385 {
1386 if (!self->input_state)
1387 goto not_negotiated;
1388 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1389 &error))
1390 goto not_negotiated;
1391 }
1392
1393 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1394 {
1395 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001396 GstCapsFeatures *features = NULL;
1397
1398 features = gst_caps_get_features(self->input_state->caps, 0);
xuesong.jiang26749f32024-07-24 09:40:47 +08001399 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
sheng.liu0c77f6c2022-06-17 21:33:20 +08001400 {
1401 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang26749f32024-07-24 09:40:47 +08001402 //TODO:need rm is_svp flag and just using secure_es flag
sheng.liu0c77f6c2022-06-17 21:33:20 +08001403 self->v4l2output->is_svp = TRUE;
1404 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001405
1406 GST_DEBUG_OBJECT(self, "Sending header");
1407
1408 codec_data = self->input_state->codec_data;
1409
1410 /* We are running in byte-stream mode, so we don't know the headers, but
1411 * we need to send something, otherwise the decoder will refuse to
1412 * intialize.
1413 */
1414 if (codec_data)
1415 {
1416 gst_buffer_ref(codec_data);
1417 }
1418 else
1419 {
1420 codec_data = gst_buffer_ref(frame->input_buffer);
1421 processed = TRUE;
1422 }
1423
1424 /* Ensure input internal pool is active */
1425 if (!gst_buffer_pool_is_active(pool))
1426 {
1427 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001428 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1429 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001430 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1431 // self->v4l2output->info.size, min, max);
1432 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1433
1434 /* There is no reason to refuse this config */
1435 if (!gst_buffer_pool_set_config(pool, config))
1436 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001437 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001438
1439 if (!gst_buffer_pool_set_active(pool, TRUE))
1440 goto activate_failed;
1441 }
1442
1443 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1444 ret =
1445 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001446 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001447 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1448
1449 gst_buffer_unref(codec_data);
1450
1451 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1452 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1453 * and returns the visible size as with/height and the coded size as
1454 * padding. */
1455 }
1456
1457 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1458 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1459 {
1460 /* It's possible that the processing thread stopped due to an error */
1461 if (self->output_flow != GST_FLOW_OK &&
1462 self->output_flow != GST_FLOW_FLUSHING)
1463 {
1464 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1465 ret = self->output_flow;
1466 goto drop;
1467 }
1468
1469 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1470
1471 /* Start the processing task, when it quits, the task will disable input
1472 * processing to unlock input if draining, or prevent potential block */
1473 self->output_flow = GST_FLOW_FLUSHING;
1474 if (!gst_pad_start_task(decoder->srcpad,
1475 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1476 goto start_task_failed;
1477 }
1478
1479 if (!processed)
1480 {
1481 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001482 if (!self->codec_data_inject && self->input_state->codec_data)
1483 {
1484 ret = gst_aml_v4l2_buffer_pool_process
1485 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1486 self->codec_data_inject = TRUE;
1487 if (ret != GST_FLOW_OK)
1488 goto send_codec_failed;
1489 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001490 ret =
1491 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1492 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1493
1494 if (ret == GST_FLOW_FLUSHING)
1495 {
1496 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1497 GST_TASK_STARTED)
1498 ret = self->output_flow;
1499 goto drop;
1500 }
1501 else if (ret != GST_FLOW_OK)
1502 {
1503 goto process_failed;
1504 }
1505 }
1506
1507 /* No need to keep input arround */
1508 tmp = frame->input_buffer;
1509 frame->input_buffer = gst_buffer_new();
1510 gst_buffer_copy_into(frame->input_buffer, tmp,
1511 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1512 GST_BUFFER_COPY_META,
1513 0, 0);
1514 gst_buffer_unref(tmp);
1515
1516 gst_video_codec_frame_unref(frame);
1517 return ret;
1518
1519 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001520send_codec_failed:
1521 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1522 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001523not_negotiated:
1524{
1525 GST_ERROR_OBJECT(self, "not negotiated");
1526 ret = GST_FLOW_NOT_NEGOTIATED;
1527 gst_aml_v4l2_error(self, &error);
1528 goto drop;
1529}
1530activate_failed:
1531{
1532 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1533 (_("Failed to allocate required memory.")),
1534 ("Buffer pool activation failed"));
1535 ret = GST_FLOW_ERROR;
1536 goto drop;
1537}
1538flushing:
1539{
1540 ret = GST_FLOW_FLUSHING;
1541 goto drop;
1542}
1543
1544start_task_failed:
1545{
1546 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1547 (_("Failed to start decoding thread.")), (NULL));
1548 ret = GST_FLOW_ERROR;
1549 goto drop;
1550}
1551process_failed:
1552{
1553 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1554 (_("Failed to process frame.")),
1555 ("Maybe be due to not enough memory or failing driver"));
1556 ret = GST_FLOW_ERROR;
1557 goto drop;
1558}
1559drop:
1560{
1561 gst_video_decoder_drop_frame(decoder, frame);
1562 return ret;
1563}
1564}
1565
1566static gboolean
1567gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1568 GstQuery *query)
1569{
1570 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1571 GstClockTime latency;
1572 gboolean ret = FALSE;
1573
1574 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1575 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1576
1577 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1578 {
1579 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1580 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1581 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1582 gst_video_decoder_set_latency(decoder, latency, latency);
1583 }
1584 else
1585 {
1586 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1587 }
1588
1589 return ret;
1590}
1591
1592static gboolean
1593gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1594{
1595 gboolean ret = TRUE;
1596 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1597
1598 switch (GST_QUERY_TYPE(query))
1599 {
1600 case GST_QUERY_CAPS:
1601 {
1602 GstCaps *filter, *result = NULL;
1603 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1604
1605 gst_query_parse_caps(query, &filter);
1606
1607 if (self->probed_srccaps)
1608 result = gst_caps_ref(self->probed_srccaps);
1609 else
1610 result = gst_pad_get_pad_template_caps(pad);
1611
1612 if (filter)
1613 {
1614 GstCaps *tmp = result;
1615 result =
1616 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1617 gst_caps_unref(tmp);
1618 }
1619
1620 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1621
1622 gst_query_set_caps_result(query, result);
1623 gst_caps_unref(result);
1624 break;
1625 }
1626
1627 default:
1628 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1629 break;
1630 }
1631
1632 return ret;
1633}
1634
1635static GstCaps *
1636gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1637{
1638 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1639 GstCaps *result;
1640
1641 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1642 filter);
1643
1644 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1645
1646 return result;
1647}
1648
1649static gboolean
1650gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1651{
1652 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1653 gboolean ret;
1654 GstEventType type = GST_EVENT_TYPE(event);
1655
1656 switch (type)
1657 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001658 case GST_EVENT_STREAM_START:
1659 {
1660 GstStructure *s;
1661 GstEvent *event;
1662 GST_DEBUG_OBJECT(self, "new private event");
1663 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1664 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1665 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1666 gst_pad_push_event (decoder->sinkpad, event);
1667 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1668 break;
1669 }
zengliang.li51f54b62023-10-10 12:14:49 +00001670 case GST_EVENT_CAPS:
1671 {
1672 GstCaps *caps;
1673 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001674 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001675
1676 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001677
1678 GST_DEBUG_OBJECT(self, "event caps %" GST_PTR_FORMAT, caps);
1679
zengliang.li51f54b62023-10-10 12:14:49 +00001680 structure= gst_caps_get_structure(caps, 0);
1681 if ( gst_structure_has_field(structure, "parsed") )
1682 {
1683 gboolean parsed = TRUE;
1684 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1685 {
1686 self->v4l2output->stream_mode = !parsed;
1687 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1688 }
1689 }
sheng.liu641aa422023-12-26 07:05:59 +00001690
xuesong.jiang26749f32024-07-24 09:40:47 +08001691 if ( gst_structure_has_field(structure, "secure") )
1692 {
1693 gboolean is_secure = FALSE;
1694 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1695 {
1696 self->v4l2output->secure_es = is_secure;
1697 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1698 }
1699 }
xuesong.jiang578add22024-07-25 15:11:30 +08001700 else
1701 {
1702 GstCapsFeatures *const features = gst_caps_get_features(caps, 0);
1703 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1704 {
1705 self->v4l2output->secure_es = TRUE;
1706 GST_DEBUG("If there is no secure field in caps, consider dma es is secure");
1707 }
1708 }
xuesong.jiang26749f32024-07-24 09:40:47 +08001709
sheng.liudb26f7d2024-01-22 11:24:23 +00001710 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1711 {
1712 if ( denom == 0 ) denom= 1;
1713
1714 if (self->v4l2capture->fps)
1715 {
1716 g_value_unset(self->v4l2capture->fps);
1717 g_free(self->v4l2capture->fps);
1718 }
1719
1720 self->v4l2capture->fps = g_new0(GValue, 1);
1721 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1722 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1723
1724 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1725 }
1726
sheng.liu641aa422023-12-26 07:05:59 +00001727 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1728 ( !self->v4l2capture->have_set_par ) )
1729 {
1730 if ( (num <= 0) || (denom <= 0) )
1731 {
1732 num= denom= 1;
1733 }
1734
1735 if ( self->v4l2capture->par )
1736 {
1737 g_value_unset(self->v4l2capture->par);
1738 g_free(self->v4l2capture->par);
1739 }
1740
1741 self->v4l2capture->par = g_new0(GValue, 1);
1742 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1743 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1744 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1745 }
zengliang.li51f54b62023-10-10 12:14:49 +00001746 break;
1747 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001748 case GST_EVENT_FLUSH_START:
1749 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001750
1751 g_mutex_lock (&self->res_chg_lock);
1752 while (self->is_res_chg)
1753 {
1754 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1755 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1756 }
1757 g_mutex_unlock (&self->res_chg_lock);
1758
zengliang.li92ff6822023-06-06 07:12:52 +00001759 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001760 gst_aml_v4l2_object_unlock(self->v4l2output);
1761 gst_aml_v4l2_object_unlock(self->v4l2capture);
1762 break;
1763 default:
1764 break;
1765 }
1766
1767 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1768
1769 switch (type)
1770 {
1771 case GST_EVENT_FLUSH_START:
1772 /* The processing thread should stop now, wait for it */
1773 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001774 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001775 GST_DEBUG_OBJECT(self, "flush start done");
1776 break;
1777 default:
1778 break;
1779 }
1780
1781 return ret;
1782}
1783
1784static GstStateChangeReturn
1785gst_aml_v4l2_video_dec_change_state(GstElement *element,
1786 GstStateChange transition)
1787{
1788 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1789 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1790
1791 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1792 {
1793 g_atomic_int_set(&self->active, FALSE);
1794 gst_aml_v4l2_object_unlock(self->v4l2output);
1795 gst_aml_v4l2_object_unlock(self->v4l2capture);
1796 gst_pad_stop_task(decoder->srcpad);
1797 }
1798
1799 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1800}
1801
1802static void
1803gst_aml_v4l2_video_dec_dispose(GObject *object)
1804{
1805 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1806
1807 gst_caps_replace(&self->probed_sinkcaps, NULL);
1808 gst_caps_replace(&self->probed_srccaps, NULL);
1809
1810 G_OBJECT_CLASS(parent_class)->dispose(object);
1811}
1812
1813static void
1814gst_aml_v4l2_video_dec_finalize(GObject *object)
1815{
1816 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1817
1818 gst_aml_v4l2_object_destroy(self->v4l2capture);
1819 gst_aml_v4l2_object_destroy(self->v4l2output);
1820
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001821 g_mutex_clear(&self->res_chg_lock);
1822 g_cond_clear(&self->res_chg_cond);
1823
xuesong.jiang61ea8012022-05-12 15:38:17 +08001824#if GST_IMPORT_LGE_PROP
1825 if (self->lge_ctxt)
1826 {
1827 if (self->lge_ctxt->app_type)
1828 g_free(self->lge_ctxt->app_type);
1829 if (self->lge_ctxt->res_info.coretype)
1830 g_free(self->lge_ctxt->res_info.coretype);
1831 free(self->lge_ctxt);
1832 }
1833
1834#endif
1835
xuesong.jiangae1548e2022-05-06 16:38:46 +08001836 G_OBJECT_CLASS(parent_class)->finalize(object);
1837}
1838
1839static void
1840gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1841{
1842 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001843 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001844 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001845 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001846 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001847 g_mutex_init(&self->res_chg_lock);
1848 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001849#if GST_IMPORT_LGE_PROP
1850 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1851 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1852#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001853}
1854
1855static void
1856gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1857{
1858 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1859 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1860 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1861
1862 gst_video_decoder_set_packetized(decoder, TRUE);
1863
1864 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1865 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1866 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1867 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1868 self->v4l2output->no_initial_format = TRUE;
1869 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001870 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001871
1872 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1873 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1874 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1875 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1876 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001877 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001878}
1879
1880static void
1881gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1882{
1883 GstElementClass *element_class;
1884 GObjectClass *gobject_class;
1885 GstVideoDecoderClass *video_decoder_class;
1886
1887 parent_class = g_type_class_peek_parent(klass);
1888
1889 element_class = (GstElementClass *)klass;
1890 gobject_class = (GObjectClass *)klass;
1891 video_decoder_class = (GstVideoDecoderClass *)klass;
1892
1893 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1894 "AML V4L2 Video Decoder");
1895
1896 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1897 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1898 gobject_class->set_property =
1899 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1900 gobject_class->get_property =
1901 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1902
1903 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1904 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1905 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1906 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1907 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1908 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1909 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1910 video_decoder_class->set_format =
1911 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1912 video_decoder_class->negotiate =
1913 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1914 video_decoder_class->decide_allocation =
1915 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1916 /* FIXME propose_allocation or not ? */
1917 video_decoder_class->handle_frame =
1918 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1919 video_decoder_class->getcaps =
1920 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1921 video_decoder_class->src_query =
1922 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1923 video_decoder_class->sink_event =
1924 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1925
1926 element_class->change_state =
1927 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1928
xuesong.jiang406ee302023-06-28 03:45:22 +00001929 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1930 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1931 G_SIGNAL_RUN_LAST,
1932 0, /* class offset */
1933 NULL, /* accumulator */
1934 NULL, /* accu data */
1935 g_cclosure_marshal_generic,
1936 G_TYPE_NONE,
1937 1,
1938 G_TYPE_UINT64);
1939
xuesong.jiangae1548e2022-05-06 16:38:46 +08001940 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001941#if GST_IMPORT_LGE_PROP
1942 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1943#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001944}
1945
1946static void
1947gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1948{
1949 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1950 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1951 GstAmlV4l2VideoDecCData *cdata = data;
1952
1953 klass->default_device = cdata->device;
1954
1955 /* Note: gst_pad_template_new() take the floating ref from the caps */
1956 gst_element_class_add_pad_template(element_class,
1957 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1958 cdata->sink_caps));
1959 gst_element_class_add_pad_template(element_class,
1960 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1961 cdata->src_caps));
1962
1963 gst_element_class_set_metadata(element_class, cdata->longname,
1964 "Codec/Decoder/Video/Hardware", cdata->description,
1965 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1966
1967 gst_caps_unref(cdata->sink_caps);
1968 gst_caps_unref(cdata->src_caps);
1969 g_free(cdata);
1970}
1971
1972/* Probing functions */
1973gboolean
1974gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1975{
1976 gboolean ret = FALSE;
1977
1978 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()))
1979 ret = TRUE;
1980
1981 return ret;
1982}
1983
1984static gchar *
1985gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1986 const gchar *basename)
1987{
1988 gchar *codec_name = NULL;
1989 gchar *type_name = NULL;
1990 gboolean got_value = FALSE;
1991
1992#define SET_META(codec) \
1993 G_STMT_START \
1994 { \
1995 cdata->longname = "AML V4L2 " codec " Decoder"; \
1996 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1997 codec_name = g_ascii_strdown(codec, -1); \
1998 } \
1999 G_STMT_END
2000
bo.xiaoce116a42024-07-16 16:21:53 +08002001 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08002002 {
bo.xiaoce116a42024-07-16 16:21:53 +08002003 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002004 }
2005 else if (gst_structure_has_name(s, "video/mpeg"))
2006 {
2007 gint mpegversion = 0;
2008 gint *list = NULL;
2009 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
2010 if (FALSE == got_value)
2011 {
2012 got_value = gst_structure_get_list(s, "mpegversion", &list);
2013 if (TRUE == got_value && (1 == *list || 2 == *list))
2014 {
2015 SET_META("MPEG2");
2016 }
2017 else
2018 {
2019 SET_META("MPEG4");
2020 }
2021 }
2022 else
2023 {
2024 SET_META("MPEG4");
2025 }
2026 }
2027 else if (gst_structure_has_name(s, "video/x-h263"))
2028 {
2029 SET_META("H263");
2030 }
2031 else if (gst_structure_has_name(s, "video/x-fwht"))
2032 {
2033 SET_META("FWHT");
2034 }
2035 else if (gst_structure_has_name(s, "video/x-h264"))
2036 {
2037 SET_META("H264");
2038 }
2039 else if (gst_structure_has_name(s, "video/x-h265"))
2040 {
2041 SET_META("H265");
2042 }
2043 else if (gst_structure_has_name(s, "video/x-wmv"))
2044 {
2045 SET_META("VC1");
2046 }
2047 else if (gst_structure_has_name(s, "video/x-vp8"))
2048 {
2049 SET_META("VP8");
2050 }
2051 else if (gst_structure_has_name(s, "video/x-vp9"))
2052 {
2053 SET_META("VP9");
2054 }
2055 else if (gst_structure_has_name(s, "video/x-av1"))
2056 {
2057 SET_META("AV1");
2058 }
zengliang.li51f54b62023-10-10 12:14:49 +00002059 else if (gst_structure_has_name(s, "video/x-avs"))
2060 {
2061 SET_META("AVS");
2062 }
2063 else if (gst_structure_has_name(s, "video/x-avs2"))
2064 {
2065 SET_META("AVS2");
2066 }
2067 else if (gst_structure_has_name(s, "video/x-avs3"))
2068 {
2069 SET_META("AVS3");
2070 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002071 else if (gst_structure_has_name(s, "video/x-bayer"))
2072 {
2073 SET_META("BAYER");
2074 }
2075 else if (gst_structure_has_name(s, "video/x-sonix"))
2076 {
2077 SET_META("SONIX");
2078 }
2079 else if (gst_structure_has_name(s, "video/x-pwc1"))
2080 {
2081 SET_META("PWC1");
2082 }
2083 else if (gst_structure_has_name(s, "video/x-pwc2"))
2084 {
2085 SET_META("PWC2");
2086 }
2087 else
2088 {
2089 /* This code should be kept on sync with the exposed CODEC type of format
2090 * from gstamlv4l2object.c. This warning will only occure in case we forget
2091 * to also add a format here. */
2092 gchar *s_str = gst_structure_to_string(s);
2093 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2094 "bug, please report at https://bugs.gnome.org",
2095 s_str);
2096 g_free(s_str);
2097 }
2098
2099 if (codec_name)
2100 {
2101 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2102 if (g_type_from_name(type_name) != 0)
2103 {
2104 g_free(type_name);
2105 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2106 }
2107
2108 g_free(codec_name);
2109 }
2110
2111 return type_name;
2112#undef SET_META
2113}
2114
2115void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2116 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2117{
2118 gint i;
2119
2120 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2121 {
2122 GstAmlV4l2VideoDecCData *cdata;
2123 GstStructure *s;
2124 GTypeQuery type_query;
2125 GTypeInfo type_info = {
2126 0,
2127 };
2128 GType type, subtype;
2129 gchar *type_name;
2130
2131 s = gst_caps_get_structure(sink_caps, i);
2132
2133 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2134 cdata->device = g_strdup(device_path);
2135 cdata->sink_caps = gst_caps_new_empty();
2136 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2137 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2138 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2139 cdata->src_caps = gst_caps_copy(src_caps);
2140 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2141 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2142 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2143
2144 /* Skip over if we hit an unmapped type */
2145 if (!type_name)
2146 {
2147 g_free(cdata);
2148 continue;
2149 }
2150
2151 type = gst_aml_v4l2_video_dec_get_type();
2152 g_type_query(type, &type_query);
2153 memset(&type_info, 0, sizeof(type_info));
2154 type_info.class_size = type_query.class_size;
2155 type_info.instance_size = type_query.instance_size;
2156 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2157 type_info.class_data = cdata;
2158 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2159
2160 subtype = g_type_register_static(type, type_name, &type_info, 0);
2161 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2162 subtype))
2163 GST_WARNING("Failed to register plugin '%s'", type_name);
2164
2165 g_free(type_name);
2166 }
2167}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002168
2169#if GST_IMPORT_LGE_PROP
2170static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2171{
2172 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2173 g_param_spec_object("resource-info", "resource-info",
2174 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2175 GST_TYPE_STRUCTURE,
2176 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2177
2178 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2179 g_param_spec_uint64("decoded-size", "decoded-size",
2180 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2181 0, G_MAXUINT64,
2182 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2183
2184 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2185 g_param_spec_uint64("undecoded-size", "undecoded-size",
2186 "video decoder element's total undecoded data update unit is byte.",
2187 0, G_MAXUINT64,
2188 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2189
2190 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2191 g_param_spec_string("app-type", "app-type",
2192 "set application type.",
2193 "default_app",
2194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2195
2196 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2197 g_param_spec_boolean("clip-mode", "clip-mode",
2198 "When seeking, Content is moving faster for a while to skip frames.",
2199 FALSE,
2200 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2201}
2202#endif