blob: 66a12e54d68d1df182b57606918bc51dfb02c01a [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,
le.han7e9c27d2024-08-08 08:10:46 +0000112 SIGNAL_DECODED_ERROR_PTS,
xuesong.jiang406ee302023-06-28 03:45:22 +0000113 MAX_SIGNAL
114};
115
116static guint g_signals[MAX_SIGNAL]= {0};
117
xuesong.jiangae1548e2022-05-06 16:38:46 +0800118#define gst_aml_v4l2_video_dec_parent_class parent_class
119G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec,
120 GST_TYPE_VIDEO_DECODER);
121
122static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder);
xuesong.jiang61ea8012022-05-12 15:38:17 +0800123#if GST_IMPORT_LGE_PROP
124static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class);
125#endif
bo.xiao8e3054d2024-07-31 17:13:57 +0800126static GstClockTime calc_output_buffer_pts_func(GstVideoDecoder *decoder);
127static GstClockTime calc_duration_func(GstVideoDecoder *decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800128
129static void
130gst_aml_v4l2_video_dec_set_property(GObject *object,
131 guint prop_id, const GValue *value, GParamSpec *pspec)
132{
133 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
134
135 switch (prop_id)
136 {
137 case PROP_CAPTURE_IO_MODE:
xuesong.jiangae1548e2022-05-06 16:38:46 +0800138 case PROP_DUMP_FRAME_LOCATION:
zengliang.lidcd41462024-06-19 16:05:12 +0800139 case PROP_CC_DATA:
140 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
141 prop_id, value, pspec))
142 {
143 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
144 }
145 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800146#if GST_IMPORT_LGE_PROP
147 case LGE_RESOURCE_INFO:
148 {
149 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
150 GstStructure *r_info = g_value_get_object(value);
151 if (r_info)
152 {
153 if (gst_structure_has_field(r_info, "coretype"))
154 {
155 if (self->lge_ctxt->res_info.coretype)
156 g_free(self->lge_ctxt->res_info.coretype);
157 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
158 }
159 if (gst_structure_has_field(r_info, "videoport"))
160 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
161 if (gst_structure_has_field(r_info, "audioport"))
162 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
163 if (gst_structure_has_field(r_info, "maxwidth"))
164 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
165 if (gst_structure_has_field(r_info, "maxheight"))
166 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
167 if (gst_structure_has_field(r_info, "mixerport"))
168 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
169 }
170 break;
171 }
172 case LGE_APP_TYPE:
173 {
174 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
175 if (self->lge_ctxt->app_type)
176 g_free(self->lge_ctxt->app_type);
177 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
178 break;
179 }
180 case LGE_CLIP_MODE:
181 {
182 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
183 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
184 break;
185 }
186#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800187 /* By default, only set on output */
188 default:
189 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output,
190 prop_id, value, pspec))
191 {
192 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
193 }
194 break;
195 }
196}
197
198static void
199gst_aml_v4l2_video_dec_get_property(GObject *object,
200 guint prop_id, GValue *value, GParamSpec *pspec)
201{
202 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
203
204 switch (prop_id)
205 {
206 case PROP_CAPTURE_IO_MODE:
zengliang.lidcd41462024-06-19 16:05:12 +0800207 case PROP_CC_DATA:
fei.dengaf682762024-06-24 19:06:03 +0800208 case PROP_DECODING_ERROR_FRAMES:
zengliang.lidcd41462024-06-19 16:05:12 +0800209 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
210 prop_id, value, pspec))
211 {
212 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
213 }
214 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800215#if GST_IMPORT_LGE_PROP
216 case LGE_DECODE_SIZE:
217 {
218 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
219 self->lge_ctxt->dec_size = -1;
220 g_value_set_int(value, self->lge_ctxt->dec_size);
221 break;
222 }
223 case LGE_UNDECODE_SIZE:
224 {
225 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
226 self->lge_ctxt->undec_size = -1;
227 g_value_set_int(value, self->lge_ctxt->undec_size);
228 break;
229 }
230#endif
231
xuesong.jiangae1548e2022-05-06 16:38:46 +0800232 /* By default read from output */
233 default:
234 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output,
235 prop_id, value, pspec))
236 {
237 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
238 }
239 break;
240 }
241}
242
243static gboolean
244gst_aml_v4l2_video_dec_open(GstVideoDecoder *decoder)
245{
246 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
247 GstCaps *codec_caps;
248
249 GST_DEBUG_OBJECT(self, "Opening");
250
251 if (!gst_aml_v4l2_object_open(self->v4l2output))
252 goto failure;
253
254 if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output))
255 goto failure;
256
257 codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad);
258 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output,
259 codec_caps);
260 gst_caps_unref(codec_caps);
261
262 if (gst_caps_is_empty(self->probed_sinkcaps))
263 goto no_encoded_format;
264
265 return TRUE;
266
267no_encoded_format:
268 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
269 (_("Decoder on device %s has no supported input format"),
270 self->v4l2output->videodev),
271 (NULL));
272 goto failure;
273
274failure:
275 if (GST_AML_V4L2_IS_OPEN(self->v4l2output))
276 gst_aml_v4l2_object_close(self->v4l2output);
277
278 if (GST_AML_V4L2_IS_OPEN(self->v4l2capture))
279 gst_aml_v4l2_object_close(self->v4l2capture);
280
281 gst_caps_replace(&self->probed_srccaps, NULL);
282 gst_caps_replace(&self->probed_sinkcaps, NULL);
283
284 return FALSE;
285}
286
287static gboolean
288gst_aml_v4l2_video_dec_close(GstVideoDecoder *decoder)
289{
290 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
291
292 GST_DEBUG_OBJECT(self, "Closing");
293
294 gst_aml_v4l2_object_close(self->v4l2output);
295 gst_aml_v4l2_object_close(self->v4l2capture);
296 gst_caps_replace(&self->probed_srccaps, NULL);
297 gst_caps_replace(&self->probed_sinkcaps, NULL);
298
299 return TRUE;
300}
301
302static gboolean
303gst_aml_v4l2_video_dec_start(GstVideoDecoder *decoder)
304{
305 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
306
307 GST_DEBUG_OBJECT(self, "Starting");
308
309 gst_aml_v4l2_object_unlock(self->v4l2output);
310 g_atomic_int_set(&self->active, TRUE);
311 self->output_flow = GST_FLOW_OK;
312
313 return TRUE;
314}
315
316static gboolean
317gst_aml_v4l2_video_dec_stop(GstVideoDecoder *decoder)
318{
319 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
320
321 GST_DEBUG_OBJECT(self, "Stopping");
322
323 gst_aml_v4l2_object_unlock(self->v4l2output);
324 gst_aml_v4l2_object_unlock(self->v4l2capture);
325
326 /* Wait for capture thread to stop */
327 gst_pad_stop_task(decoder->srcpad);
328
329 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
330 self->output_flow = GST_FLOW_OK;
331 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
332
333 /* Should have been flushed already */
334 g_assert(g_atomic_int_get(&self->active) == FALSE);
335
336 gst_aml_v4l2_object_stop(self->v4l2output);
337 gst_aml_v4l2_object_stop(self->v4l2capture);
338
339 if (self->input_state)
340 {
341 gst_video_codec_state_unref(self->input_state);
342 self->input_state = NULL;
343 }
344
345 GST_DEBUG_OBJECT(self, "Stopped");
346
347 return TRUE;
348}
349
350static gboolean
hanghang.luo8e1225b2023-10-10 08:54:28 +0000351gst_aml_v4l2_video_dec_codec_chg(GstVideoDecoder *decoder,
352 GstVideoCodecState *state)
353{
354 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
355 GstStructure *s_old = NULL;
356 GstStructure *s_new = NULL;
357
358 // first play, must set foramt;
359 if (!self->input_state)
360 return TRUE;
361
362 if (self->input_state->caps)
363 s_old = gst_caps_get_structure(self->input_state->caps,0);
364 if (state->caps)
365 s_new = gst_caps_get_structure(state->caps,0);
366
367 if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old)))
368 return TRUE;
369 return FALSE;
370}
371
372static gboolean
373gst_aml_v4l2_video_dec_res_chg(GstVideoDecoder *decoder,
374 GstVideoCodecState *state)
375{
376 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
377 gboolean ret = FALSE;
378 gint width_new = -1,height_new = -1,width_old = -1,height_old = -1;
379 GstStructure *s_old = NULL;
380 GstStructure *s_new = NULL;
381
382 // first play, must set foramt;
383 if (!self->input_state)
384 {
385 ret = TRUE;
386 goto done;
387 }
388
389 if (self->input_state->caps)
390 s_old = gst_caps_get_structure(self->input_state->caps,0);
391 if (state->caps)
392 s_new = gst_caps_get_structure(state->caps,0);
393
394 if (s_new && gst_structure_has_field(s_new,"width") && gst_structure_has_field(s_new,"height"))
395 {
396 gst_structure_get_int(s_new,"width",&width_new);
397 gst_structure_get_int(s_new,"height",&height_new);
398 }
399 if (s_old && gst_structure_has_field(s_old,"width") && gst_structure_has_field(s_old,"height"))
400 {
401 gst_structure_get_int(s_old,"width",&width_old);
402 gst_structure_get_int(s_old,"height",&height_old);
403 }
404
405 if (width_new != width_old || height_new != height_old)
406 ret = TRUE;
407
408done:
409 GST_DEBUG_OBJECT(self, "ret is %d",ret);
410 return ret;
411}
412
413static gboolean
xuesong.jiangae1548e2022-05-06 16:38:46 +0800414gst_aml_v4l2_video_dec_set_format(GstVideoDecoder *decoder,
415 GstVideoCodecState *state)
416{
417 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
418 gboolean ret = TRUE;
419 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
420 GstCaps *caps;
421
422 GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps);
hanghang.luo8e1225b2023-10-10 08:54:28 +0000423 if (self->input_state)
424 {
425 if (gst_aml_v4l2_video_dec_res_chg(decoder,state) || gst_aml_v4l2_video_dec_codec_chg(decoder,state))
426 GST_DEBUG_OBJECT(self, "resolution or codec changed");
427 else
428 goto done;
429 }
430
xuesong.jiangae1548e2022-05-06 16:38:46 +0800431 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
hanghang.luoc54208e2023-09-22 02:43:54 +0000432 GstStructure *s = gst_caps_get_structure(state->caps,0);
433 if (s && gst_structure_has_field(s,"format"))
434 {
435 if (!strcmp("XVID",gst_structure_get_string(s,"format")))
436 {
437 GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support");
438 ret = FALSE;
439 goto done;
440 }
441 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800442
443 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
444 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
445
446 if (self->input_state)
447 {
448 if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps))
449 {
450 GST_DEBUG_OBJECT(self, "Compatible caps");
451 goto done;
452 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800453
454 gst_aml_v4l2_video_dec_finish(decoder);
455 gst_aml_v4l2_object_stop(self->v4l2output);
456
fei.dengf78f7122024-07-15 14:44:04 +0800457 gst_video_codec_state_unref(self->input_state);
458 self->input_state = NULL;
459
xuesong.jiangae1548e2022-05-06 16:38:46 +0800460 /* The renegotiation flow don't blend with the base class flow. To properly
461 * stop the capture pool, if the buffers can't be orphaned, we need to
462 * reclaim our buffers, which will happend through the allocation query.
463 * The allocation query is triggered by gst_video_decoder_negotiate() which
464 * requires the output caps to be set, but we can't know this information
465 * as we rely on the decoder, which requires the capture queue to be
466 * stopped.
467 *
468 * To workaround this issue, we simply run an allocation query with the
469 * old negotiated caps in order to drain/reclaim our buffers. That breaks
470 * the complexity and should not have much impact in performance since the
471 * following allocation query will happen on a drained pipeline and won't
472 * block. */
473 if (self->v4l2capture->pool &&
474 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
475 {
476 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
477 if (caps)
478 {
479 GstQuery *query = gst_query_new_allocation(caps, FALSE);
480 gst_pad_peer_query(decoder->srcpad, query);
481 gst_query_unref(query);
482 gst_caps_unref(caps);
483 }
484 }
485
486 gst_aml_v4l2_object_stop(self->v4l2capture);
487 self->output_flow = GST_FLOW_OK;
488 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800489 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
490 {
491 GST_ERROR_OBJECT(self, "config output drm mode error");
492 goto done;
493 }
494
xuesong.jiang22a9b112023-05-24 09:01:59 +0000495 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
496 {
497 GST_ERROR_OBJECT(self, "config output stream mode error");
498 goto done;
499 }
500
hanghang.luoac6df002024-07-11 10:54:17 +0800501 if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error))
502 {
503 GST_ERROR_OBJECT(self, "set format error");
504 goto done;
505 }
506
507 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
508 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
509 {
510 GST_ERROR_OBJECT(self, "config I frame mode error");
511 goto done;
512 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800513
514 gst_caps_replace(&self->probed_srccaps, NULL);
515 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
516 gst_aml_v4l2_object_get_raw_caps());
517
518 if (gst_caps_is_empty(self->probed_srccaps))
519 goto no_raw_format;
520
521 caps = gst_caps_copy(self->probed_srccaps);
522 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
523 gst_caps_append(self->probed_srccaps, caps);
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800524
xuesong.jiangae1548e2022-05-06 16:38:46 +0800525 if (ret)
526 self->input_state = gst_video_codec_state_ref(state);
527 else
528 gst_aml_v4l2_error(self, &error);
529
530done:
531 return ret;
532
533no_raw_format:
534 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
535 (_("Decoder on device %s has no supported output format"),
536 self->v4l2output->videodev),
537 (NULL));
538 return GST_FLOW_ERROR;
539}
540
541static gboolean
542gst_aml_v4l2_video_dec_flush(GstVideoDecoder *decoder)
543{
544 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
545
546 GST_DEBUG_OBJECT(self, "Flushed");
547
548 /* Ensure the processing thread has stopped for the reverse playback
549 * discount case */
550 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
551 {
552 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
553
554 gst_aml_v4l2_object_unlock(self->v4l2output);
555 gst_aml_v4l2_object_unlock(self->v4l2capture);
556 gst_pad_stop_task(decoder->srcpad);
557 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
558 }
559
560 self->output_flow = GST_FLOW_OK;
561
562 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
563 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
564
565 if (self->v4l2output->pool)
566 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
567
568 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
569 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
570 * pool. */
571 if (self->v4l2capture->pool)
572 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
573
574 return TRUE;
575}
576
577static gboolean
578gst_aml_v4l2_video_dec_negotiate(GstVideoDecoder *decoder)
579{
580 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
581
xuesong.jiang681d3602022-06-24 21:23:35 +0800582 if (TRUE == self->v4l2output->is_svp)
583 {
584 GstStructure *s;
585 GstEvent *event;
586
587 s = gst_structure_new_empty ("IS_SVP");
588 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
589 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
590 gst_pad_push_event (decoder->srcpad, event);
591 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
592 }
593
xuesong.jiangae1548e2022-05-06 16:38:46 +0800594 /* We don't allow renegotiation without carefull disabling the pool */
595 if (self->v4l2capture->pool &&
596 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
597 return TRUE;
598
599 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
600}
601
602static gboolean
603gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
604{
605 struct v4l2_decoder_cmd dcmd = {
606 0,
607 };
608
609 GST_DEBUG_OBJECT(v4l2object->element,
610 "sending v4l2 decoder command %u with flags %u", cmd, flags);
611
612 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
613 return FALSE;
614
615 dcmd.cmd = cmd;
616 dcmd.flags = flags;
617 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
618 goto dcmd_failed;
619
620 return TRUE;
621
622dcmd_failed:
623 if (errno == ENOTTY)
624 {
625 GST_INFO_OBJECT(v4l2object->element,
626 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
627 cmd, flags, v4l2object->videodev, g_strerror(errno));
628 }
629 else
630 {
631 GST_ERROR_OBJECT(v4l2object->element,
632 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
633 cmd, flags, v4l2object->videodev, g_strerror(errno));
634 }
635 return FALSE;
636}
637
638static GstFlowReturn
639gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
640{
641 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
642 GstFlowReturn ret = GST_FLOW_OK;
643 GstBuffer *buffer;
644
645 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
646 goto done;
647
648 GST_DEBUG_OBJECT(self, "Finishing decoding");
649
650 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
651
652 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
653 {
654 GstTask *task = decoder->srcpad->task;
655
656 /* If the decoder stop command succeeded, just wait until processing is
657 * finished */
658 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
659 GST_OBJECT_LOCK(task);
660 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
661 GST_TASK_WAIT(task);
662 GST_OBJECT_UNLOCK(task);
663 ret = GST_FLOW_FLUSHING;
664 }
665 else
666 {
667 /* otherwise keep queuing empty buffers until the processing thread has
668 * stopped, _pool_process() will return FLUSHING when that happened */
669 while (ret == GST_FLOW_OK)
670 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800671 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800672 buffer = gst_buffer_new();
673 ret =
674 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
675 gst_buffer_unref(buffer);
676 }
677 }
678
679 /* and ensure the processing thread has stopped in case another error
680 * occured. */
681 gst_aml_v4l2_object_unlock(self->v4l2capture);
682 gst_pad_stop_task(decoder->srcpad);
683 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
684
685 if (ret == GST_FLOW_FLUSHING)
686 ret = self->output_flow;
687
fei.deng4244a262024-07-31 14:07:14 +0800688 /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop.
689 should reset need_wait_event=true to wait source change event*/
690 self->v4l2capture->need_wait_event = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800691 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
692
693 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
694
695done:
696 return ret;
697}
698
699static GstFlowReturn
700gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
701{
702 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
703
704 GST_DEBUG_OBJECT(self, "Draining...");
705 gst_aml_v4l2_video_dec_finish(decoder);
706 gst_aml_v4l2_video_dec_flush(decoder);
707
708 return GST_FLOW_OK;
709}
710
711static GstVideoCodecFrame *
xuesong.jiange24aef92023-06-16 06:39:10 +0000712gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800713{
714 GstVideoCodecFrame *frame = NULL;
715 GList *frames, *l;
716 gint count = 0;
717
xuesong.jiange24aef92023-06-16 06:39:10 +0000718 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
719
fei.dengbee20862022-06-14 14:59:48 +0800720 frames = gst_video_decoder_get_frames(decoder);
721
722 for (l = frames; l != NULL; l = l->next)
723 {
724 GstVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000725
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800726 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800727 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800728 }
fei.dengbee20862022-06-14 14:59:48 +0800729 count++;
730 }
731
zengliang.liddee2da2023-07-14 07:27:05 +0000732 if (!frame)
733 {
734 for (l = frames; l != NULL; l = l->next)
735 {
736 GstVideoCodecFrame *f = l->data;
737 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
738 {
739 frame = f;
fei.dengf78f7122024-07-15 14:44:04 +0800740 GST_DEBUG("The pts of the expected output frame is invalid");
741 break;
zengliang.liddee2da2023-07-14 07:27:05 +0000742 }
zengliang.liddee2da2023-07-14 07:27:05 +0000743 }
744 }
745
fei.dengbee20862022-06-14 14:59:48 +0800746 if (frame)
747 {
748 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800749 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
750 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
fei.dengbee20862022-06-14 14:59:48 +0800751 gst_video_codec_frame_ref(frame);
752 }
753
754 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
755
756 return frame;
757}
758
xuesong.jiange24aef92023-06-16 06:39:10 +0000759static GstVideoCodecFrame *
760gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstVideoDecoder *decoder, GstClockTime pts)
761{
762 GstVideoCodecFrame *frame = NULL;
763 GList *frames, *l;
hanghang.luo4486d7d2024-07-24 10:06:35 +0800764 guint frames_len = 0;
xuesong.jiange24aef92023-06-16 06:39:10 +0000765 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
766
hanghang.luo4486d7d2024-07-24 10:06:35 +0800767 if (!(frames = gst_video_decoder_get_frames(decoder)))
768 goto done;
769
xuesong.jiange24aef92023-06-16 06:39:10 +0000770 frames_len = g_list_length(frames);
771 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
772
xuesong.jiange24aef92023-06-16 06:39:10 +0000773 for (l = frames; l != NULL; l = l->next)
774 {
775 GstVideoCodecFrame *f = l->data;
776
777 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
778 {
779 /* found the right frame */
bo.xiao8e3054d2024-07-31 17:13:57 +0800780 GST_LOG_OBJECT(decoder,
781 "found frame %" GST_TIME_FORMAT "with pts %" GST_TIME_FORMAT,
782 GST_TIME_ARGS(f->pts), GST_TIME_ARGS(pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000783 frame = f;
784 break;
785 }
786 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
787 {
788 GST_LOG_OBJECT(decoder,
789 "stream mode drop frame %d %" GST_TIME_FORMAT,
hanghang.luoa0626d22024-07-30 16:10:10 +0800790 f->system_frame_number, GST_TIME_ARGS(f->pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000791
792 gst_video_codec_frame_ref(f);
793 // gst_video_decoder_drop_frame(decoder, f);
794 gst_video_decoder_release_frame(decoder, f);
795 }
796 else
797 {
798 GST_LOG_OBJECT (decoder, "dbg");
799 }
800 }
801
802 if (frame)
803 {
804 guint l_len = 0;
805 l = gst_video_decoder_get_frames(decoder);
806 l_len = g_list_length(l);
807 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
808
809 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800810 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
811 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
xuesong.jiange24aef92023-06-16 06:39:10 +0000812 gst_video_codec_frame_ref(frame);
813 }
814
815 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
816
hanghang.luo4486d7d2024-07-24 10:06:35 +0800817done:
xuesong.jiange24aef92023-06-16 06:39:10 +0000818 return frame;
819}
820
821static GstVideoCodecFrame *
822gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
823{
824 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
825 if (self->v4l2output->stream_mode)
826 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
827 else
828 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
829}
830
xuesong.jiangae1548e2022-05-06 16:38:46 +0800831static gboolean
832gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
833 GstStructure *structure, gpointer user_data)
834{
835 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
836 GstVideoAlignment *align = &self->v4l2capture->align;
837 GstVideoInfo *info = &self->v4l2capture->info;
838 int width, height;
839
840 if (!gst_structure_get_int(structure, "width", &width))
841 return TRUE;
842
843 if (!gst_structure_get_int(structure, "height", &height))
844 return TRUE;
845
846 if (align->padding_left != 0 || align->padding_top != 0 ||
847 height != info->height + align->padding_bottom)
848 return TRUE;
849
850 if (height == info->height + align->padding_bottom)
851 {
852 /* Some drivers may round up width to the padded with */
853 if (width == info->width + align->padding_right)
854 gst_structure_set(structure,
855 "width", G_TYPE_INT, width - align->padding_right,
856 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
857 /* Some drivers may keep visible width and only round up bytesperline */
858 else if (width == info->width)
859 gst_structure_set(structure,
860 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
861 }
862
863 return TRUE;
864}
865
866static void
sheng.liubcf036c2022-06-21 15:55:42 +0800867gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800868{
869 struct v4l2_event evt;
870 gint ret;
871
872 memset (&evt, 0x00, sizeof (struct v4l2_event));
873 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
874 if (ret < 0)
875 {
876 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
877 return;
878 }
879
880 switch (evt.type)
881 {
882 case V4L2_EVENT_SOURCE_CHANGE:
883 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
884 break;
885 case V4L2_EVENT_EOS:
886 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
887 break;
888 default:
889 break;
890 }
891
892 return;
893}
894
895static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000896gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
897{
898 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
899 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000900 struct v4l2_selection sel;
901 struct v4l2_rect *r = NULL;
902 GstStructure *s;
903 gint width = 0;
904 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800905 GST_DEBUG("%d %d",info.width, info.height);
hanghang.luo2eec4892023-07-18 06:44:42 +0000906 output_state = gst_video_decoder_set_output_state(decoder,
907 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000908 memset(&sel, 0, sizeof(struct v4l2_selection));
909 sel.type = self->v4l2capture->type;
910 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
911 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
912 {
913 r = &sel.r;
914 width = (r->width/2)*2;
915 height = (r->height/2)*2;
916 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
917 }
918 else
919 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000920 if (output_state)
921 {
922 output_state->info.interlace_mode = info.interlace_mode;
923 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000924 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000925 s = gst_caps_get_structure(output_state->caps, 0);
926 if (s)
927 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800928 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
929 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
930 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
931 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000932 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
933 gst_video_codec_state_unref(output_state);
934 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000935 }
936}
937
zengliang.lidcd41462024-06-19 16:05:12 +0800938static GQuark
939gst_aml_v4l2_buffer_pool_cc_import_quark (void)
940{
941 static GQuark quark = 0;
942
943 if (quark == 0)
944 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
945
946 return quark;
947}
948
949static gboolean
950foreach_cc_buffer_list_match_pts_func(GList *list , GstVideoCodecFrame *frame)
951{
952 GList *l;
953 if (g_list_length (list) > 0)
954 {
955 for (l = list; l != NULL; l = l->next)
956 {
957 GstBuffer *cc_buffer = l->data;
958 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
959 {
960 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
961 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
962 #if 0
963 //Debug code:dump cc data
964 GstMapInfo gst_map;
965 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
966 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
967 if (gst_map.size>0)
968 write(fd,gst_map.data,gst_map.size);
969 close(fd);
970 gst_buffer_unmap(cc_buffer,&gst_map);
971 #endif
972 GST_DEBUG("match success");
973 return TRUE;
974 }
975 else
976 {
977 GST_DEBUG("match fail");
978 }
979 }
980 GST_WARNING("no match frame in the bufferlist");
981 }
982 else
983 {
984 GST_WARNING("list is null,can not foreach");
985 }
986 return FALSE;
987}
988
bo.xiao8e3054d2024-07-31 17:13:57 +0800989static GstClockTime
990calc_output_buffer_pts_func(GstVideoDecoder *decoder)
991{
992 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
993 GstClockTime pts = GST_CLOCK_TIME_NONE;
994
995 if (GST_CLOCK_TIME_IS_VALID (self->last_out_pts) && GST_CLOCK_TIME_IS_VALID(self->frame_duration)) {
996 pts = self->last_out_pts + self->frame_duration;
997 GST_LOG_OBJECT (decoder,
998 "calculate PTS %" GST_TIME_FORMAT " by duration: %" GST_TIME_FORMAT,
999 GST_TIME_ARGS (pts), GST_TIME_ARGS (self->frame_duration));
1000 }
1001 else
1002 {
1003 pts = 0;
1004 GST_INFO_OBJECT (decoder,"Set PTS=0");
1005 }
1006 return pts;
1007}
1008
1009static GstClockTime
1010calc_duration_func(GstVideoDecoder *decoder)
1011{
1012 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1013 GstClockTime duration = GST_CLOCK_TIME_NONE;
1014 gint fps_n, fps_d;
1015 fps_n = gst_value_get_fraction_numerator(self->v4l2capture->fps);
1016 fps_d = gst_value_get_fraction_denominator(self->v4l2capture->fps);
1017
1018 if (fps_n != 0 && fps_d != 0)
1019 {
1020 duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
1021
1022 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1023 {
1024 duration = duration / 2;
1025 }
1026 }
1027
1028 GST_INFO_OBJECT (decoder,"framerate(fps_d: %d, fps_n: %d) -> frame_duration = %" GST_TIME_FORMAT, fps_d, fps_n, GST_TIME_ARGS(duration));
1029
1030 return duration;
1031}
1032
hanghang.luo2eec4892023-07-18 06:44:42 +00001033static void
xuesong.jiangae1548e2022-05-06 16:38:46 +08001034gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
1035{
1036 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1037 GstAmlV4l2BufferPool *v4l2_pool;
1038 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1039 GstBufferPool *pool;
1040 GstVideoCodecFrame *frame;
1041 GstBuffer *buffer = NULL;
1042 GstFlowReturn ret;
1043
1044 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1045 {
1046 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001047 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001048 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001049 GST_DEBUG_OBJECT(self, "waitting source change event");
1050 /* Wait until received SOURCE_CHANGE event to get right video format */
1051 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1052 {
1053 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1054 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1055 {
fei.deng2a06e042023-10-10 03:09:45 +00001056 //let flush start event blocked until capture buffer pool actived
1057 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001058 GST_DEBUG_OBJECT(self, "Received source change event");
1059 break;
1060 }
1061 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1062 {
1063 GST_DEBUG_OBJECT(self, "Received eos event");
1064 goto beach;
1065 }
1066 else if (ret != GST_FLOW_OK)
1067 {
1068 GST_ERROR_OBJECT(self, "dqevent error");
1069 goto beach;
1070 }
1071 }
1072 self->v4l2capture->need_wait_event = FALSE;
1073
sheng.liu0c77f6c2022-06-17 21:33:20 +08001074 if (TRUE == self->v4l2output->is_svp)
1075 {
1076 GstPad *peer;
1077 GstStructure *s;
1078 GstEvent *event;
1079
1080 peer = gst_pad_get_peer (decoder->srcpad);
1081 if (peer)
1082 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001083 s = gst_structure_new_empty ("IS_SVP");
1084 if (s)
1085 {
1086 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1087 gst_pad_send_event (peer, event);
1088 GST_DEBUG_OBJECT(self, "Send SVP Event");
1089 }
1090 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001091 }
1092 }
1093
sheng.liub56bbc52022-06-21 11:02:33 +08001094 if (self->v4l2capture->need_drop_event)
1095 {
1096 // drop V4L2_EVENT_SOURCE_CHANGE
1097 gst_v4l2_drop_event(self->v4l2capture);
1098 self->v4l2capture->need_drop_event = FALSE;
1099 }
1100
xuesong.jiangae1548e2022-05-06 16:38:46 +08001101 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1102 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001103
xuesong.jiangae1548e2022-05-06 16:38:46 +08001104 /* Create caps from the acquired format, remove the format field */
1105 acquired_caps = gst_video_info_to_caps(&info);
1106 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1107 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001108 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1109
1110 /* Probe currently available pixel formats */
1111 available_caps = gst_caps_copy(self->probed_srccaps);
1112 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1113
1114 /* Replace coded size with visible size, we want to negotiate visible size
1115 * with downstream, not coded size. */
1116 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1117
1118 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001119 caps = gst_caps_copy(filter);
1120 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1121 gst_caps_append(filter, caps);
1122
1123 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1124 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001125 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001126 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1127 gst_caps_unref(filter);
1128
1129 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1130 if (gst_caps_is_empty(caps))
1131 {
1132 gst_caps_unref(caps);
1133 goto not_negotiated;
1134 }
1135
1136 /* Fixate pixel format */
1137 caps = gst_caps_fixate(caps);
1138
1139 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1140
1141 /* Try to set negotiated format, on success replace acquired format */
1142 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1143 gst_video_info_from_caps(&info, caps);
1144 else
1145 gst_aml_v4l2_clear_error(&error);
1146 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001147 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001148 if (!gst_video_decoder_negotiate(decoder))
1149 {
1150 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1151 goto flushing;
1152 else
1153 goto not_negotiated;
1154 }
1155
1156 /* Ensure our internal pool is activated */
1157 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1158 TRUE))
1159 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001160
bo.xiao8e3054d2024-07-31 17:13:57 +08001161 //cal duration when got resolution event
1162 self->frame_duration = calc_duration_func(decoder);
1163
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001164 g_mutex_lock(&self->res_chg_lock);
1165 GST_LOG_OBJECT(decoder, "signal resolution changed");
1166 self->is_res_chg = FALSE;
1167 g_cond_signal(&self->res_chg_cond);
1168 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001169 }
1170
1171 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1172
1173 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1174
1175 self->output_flow = GST_FLOW_OK;
1176 do
1177 {
1178 /* We cannot use the base class allotate helper since it taking the internal
1179 * stream lock. we know that the acquire may need to poll until more frames
1180 * comes in and holding this lock would prevent that.
1181 */
1182 pool = gst_video_decoder_get_buffer_pool(decoder);
1183
1184 /* Pool may be NULL if we started going to READY state */
1185 if (pool == NULL)
1186 {
fei.dengbee20862022-06-14 14:59:48 +08001187 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001188 ret = GST_FLOW_FLUSHING;
1189 goto beach;
1190 }
1191
1192 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001193
xuesong.jiangae1548e2022-05-06 16:38:46 +08001194 g_object_unref(pool);
1195
fei.deng9a5cd6e2023-06-30 12:09:18 +00001196 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 +00001197 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1198 self->v4l2capture->need_drop_event = TRUE;
1199 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1200 if (self->is_res_chg) {
1201 //we must release last buffer
1202 gst_buffer_unref(buffer);
1203 //if resolution changed event received,we should set need_drop_event to false
1204 self->v4l2capture->need_drop_event = FALSE;
1205 gst_aml_v4l2_object_stop(self->v4l2capture);
1206 //unblock flush start event
1207 g_mutex_lock(&self->res_chg_lock);
1208 self->is_res_chg = FALSE;
1209 g_cond_signal(&self->res_chg_cond);
1210 g_mutex_unlock(&self->res_chg_lock);
1211 return;
1212 } else {
1213 goto beach;
1214 }
sheng.liub56bbc52022-06-21 11:02:33 +08001215 }
1216
zengliang.lidcd41462024-06-19 16:05:12 +08001217 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1218 {
bo.xiaoe978c3b2024-08-01 20:57:15 +08001219 GST_DEBUG_OBJECT(decoder, "already got cc data, just continue.");
1220 continue;
1221 }
1222
bo.xiaob6afda52024-08-02 16:08:30 +08001223 if (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT)
bo.xiaoe978c3b2024-08-01 20:57:15 +08001224 {
1225 GST_DEBUG_OBJECT(decoder, "unknow event, just continue.");
zengliang.lidcd41462024-06-19 16:05:12 +08001226 continue;
1227 }
1228
sheng.liu8d18ed22022-05-26 17:28:15 +08001229 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1230 {
1231 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001232
1233 g_mutex_lock (&self->res_chg_lock);
1234 self->is_res_chg = TRUE;
1235 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001236 return;
1237 }
fei.dengaf682762024-06-24 19:06:03 +08001238 //decoding error happened
1239 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1240 {
le.han7e9c27d2024-08-08 08:10:46 +00001241 g_signal_emit (self, g_signals[SIGNAL_DECODED_ERROR_PTS], v4l2_pool->obj->error_frame_pts, NULL);
fei.dengaf682762024-06-24 19:06:03 +08001242 continue;
1243 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001244
fei.dengbee20862022-06-14 14:59:48 +08001245 if (ret != GST_FLOW_OK) {
1246 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001247 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001248 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001249
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001250 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001251 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001252
bo.xiao8e3054d2024-07-31 17:13:57 +08001253 GST_DEBUG_OBJECT(decoder, "decoded pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
xuesong.jiang406ee302023-06-28 03:45:22 +00001254 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1255
xuesong.jiangae1548e2022-05-06 16:38:46 +08001256 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1257 {
1258 gst_aml_v4l2_object_stop(self->v4l2capture);
1259 return;
1260 }
1261
fei.dengaf682762024-06-24 19:06:03 +08001262 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1263 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
bo.xiaob6afda52024-08-02 16:08:30 +08001264 (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT) ||
fei.dengaf682762024-06-24 19:06:03 +08001265 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001266
1267 if (ret != GST_FLOW_OK)
1268 goto beach;
1269
bo.xiao8e3054d2024-07-31 17:13:57 +08001270 if (!GST_BUFFER_PTS_IS_VALID (buffer))
1271 {
1272 GST_BUFFER_TIMESTAMP(buffer) = calc_output_buffer_pts_func(decoder);
1273 }
1274
1275 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1276 {
1277 GST_BUFFER_DURATION(buffer) = self->frame_duration; // got at resolution event.
1278 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1279 }
1280
fei.dengbee20862022-06-14 14:59:48 +08001281 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001282 if (frame)
1283 {
fei.dengccc89632022-07-15 19:10:17 +08001284 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001285 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001286 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1287 frame->duration = GST_BUFFER_DURATION(buffer);
bo.xiao8e3054d2024-07-31 17:13:57 +08001288
xuesong.jiangae1548e2022-05-06 16:38:46 +08001289 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001290
zengliang.lidcd41462024-06-19 16:05:12 +08001291 if (self->v4l2capture->enable_cc_data)
1292 {
1293 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1294 {
1295 GST_DEBUG("cc buffer and frame bind success");
1296 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1297 GST_AML_V4L2_CC_IMPORT_QUARK);
1298 #if 0
1299 //Debug code:dump cc data
1300 GstMapInfo gst_map;
1301 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1302 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1303 if (gst_map.size>0)
1304 write(fd,gst_map.data,gst_map.size);
1305 close(fd);
1306 gst_buffer_unmap(cc_buffer,&gst_map);
1307 #endif
1308 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1309 gst_buffer_unref(cc_buffer);
1310 }
1311 else
1312 {
1313 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1314 }
1315 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001316 ret = gst_video_decoder_finish_frame(decoder, frame);
1317
1318 if (ret != GST_FLOW_OK)
1319 goto beach;
1320 }
1321 else
1322 {
bo.xiao8e3054d2024-07-31 17:13:57 +08001323 GST_WARNING_OBJECT(decoder, "Unmatch buffer, should be push, need refine");
1324 //gst_pad_push (decoder->srcpad, buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001325 gst_buffer_unref(buffer);
1326 }
1327
1328 return;
1329 /* ERRORS */
1330not_negotiated:
1331{
1332 GST_ERROR_OBJECT(self, "not negotiated");
1333 ret = GST_FLOW_NOT_NEGOTIATED;
1334 goto beach;
1335}
1336activate_failed:
1337{
1338 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1339 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1340 (_("Failed to allocate required memory.")),
1341 ("Buffer pool activation failed"));
1342 ret = GST_FLOW_ERROR;
1343 goto beach;
1344}
1345flushing:
1346{
1347 ret = GST_FLOW_FLUSHING;
1348 goto beach;
1349}
1350beach:
1351 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1352 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001353 if (self->is_res_chg) {
1354 //unblock flush start event
1355 g_mutex_lock(&self->res_chg_lock);
1356 self->is_res_chg = FALSE;
1357 g_cond_signal(&self->res_chg_cond);
1358 g_mutex_unlock(&self->res_chg_lock);
1359 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001360 gst_buffer_replace(&buffer, NULL);
1361 self->output_flow = ret;
1362 gst_aml_v4l2_object_unlock(self->v4l2output);
1363 gst_pad_pause_task(decoder->srcpad);
1364}
1365
1366static GstFlowReturn
1367gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1368 GstVideoCodecFrame *frame)
1369{
1370 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1371 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1372 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1373 GstFlowReturn ret = GST_FLOW_OK;
1374 gboolean processed = FALSE;
1375 GstBuffer *tmp;
1376 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001377
1378 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1379
1380 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1381 goto flushing;
1382
1383 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1384 {
1385 if (!self->input_state)
1386 goto not_negotiated;
1387 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1388 &error))
1389 goto not_negotiated;
1390 }
1391
1392 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1393 {
1394 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001395 GstCapsFeatures *features = NULL;
1396
1397 features = gst_caps_get_features(self->input_state->caps, 0);
xuesong.jiang26749f32024-07-24 09:40:47 +08001398 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
sheng.liu0c77f6c2022-06-17 21:33:20 +08001399 {
1400 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang26749f32024-07-24 09:40:47 +08001401 //TODO:need rm is_svp flag and just using secure_es flag
sheng.liu0c77f6c2022-06-17 21:33:20 +08001402 self->v4l2output->is_svp = TRUE;
1403 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001404
1405 GST_DEBUG_OBJECT(self, "Sending header");
1406
1407 codec_data = self->input_state->codec_data;
1408
1409 /* We are running in byte-stream mode, so we don't know the headers, but
1410 * we need to send something, otherwise the decoder will refuse to
1411 * intialize.
1412 */
1413 if (codec_data)
1414 {
1415 gst_buffer_ref(codec_data);
1416 }
1417 else
1418 {
1419 codec_data = gst_buffer_ref(frame->input_buffer);
1420 processed = TRUE;
1421 }
1422
1423 /* Ensure input internal pool is active */
1424 if (!gst_buffer_pool_is_active(pool))
1425 {
1426 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001427 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1428 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001429 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1430 // self->v4l2output->info.size, min, max);
1431 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1432
1433 /* There is no reason to refuse this config */
1434 if (!gst_buffer_pool_set_config(pool, config))
1435 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001436 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001437
1438 if (!gst_buffer_pool_set_active(pool, TRUE))
1439 goto activate_failed;
1440 }
1441
1442 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1443 ret =
1444 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001445 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001446 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1447
1448 gst_buffer_unref(codec_data);
1449
1450 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1451 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1452 * and returns the visible size as with/height and the coded size as
1453 * padding. */
1454 }
1455
1456 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1457 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1458 {
1459 /* It's possible that the processing thread stopped due to an error */
1460 if (self->output_flow != GST_FLOW_OK &&
1461 self->output_flow != GST_FLOW_FLUSHING)
1462 {
1463 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1464 ret = self->output_flow;
1465 goto drop;
1466 }
1467
1468 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1469
1470 /* Start the processing task, when it quits, the task will disable input
1471 * processing to unlock input if draining, or prevent potential block */
1472 self->output_flow = GST_FLOW_FLUSHING;
1473 if (!gst_pad_start_task(decoder->srcpad,
1474 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1475 goto start_task_failed;
1476 }
1477
1478 if (!processed)
1479 {
1480 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001481 if (!self->codec_data_inject && self->input_state->codec_data)
1482 {
1483 ret = gst_aml_v4l2_buffer_pool_process
1484 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1485 self->codec_data_inject = TRUE;
1486 if (ret != GST_FLOW_OK)
1487 goto send_codec_failed;
1488 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001489 ret =
1490 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1491 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1492
1493 if (ret == GST_FLOW_FLUSHING)
1494 {
1495 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1496 GST_TASK_STARTED)
1497 ret = self->output_flow;
1498 goto drop;
1499 }
1500 else if (ret != GST_FLOW_OK)
1501 {
1502 goto process_failed;
1503 }
1504 }
1505
1506 /* No need to keep input arround */
1507 tmp = frame->input_buffer;
1508 frame->input_buffer = gst_buffer_new();
1509 gst_buffer_copy_into(frame->input_buffer, tmp,
1510 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1511 GST_BUFFER_COPY_META,
1512 0, 0);
1513 gst_buffer_unref(tmp);
1514
1515 gst_video_codec_frame_unref(frame);
1516 return ret;
1517
1518 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001519send_codec_failed:
1520 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1521 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001522not_negotiated:
1523{
1524 GST_ERROR_OBJECT(self, "not negotiated");
1525 ret = GST_FLOW_NOT_NEGOTIATED;
1526 gst_aml_v4l2_error(self, &error);
1527 goto drop;
1528}
1529activate_failed:
1530{
1531 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1532 (_("Failed to allocate required memory.")),
1533 ("Buffer pool activation failed"));
1534 ret = GST_FLOW_ERROR;
1535 goto drop;
1536}
1537flushing:
1538{
1539 ret = GST_FLOW_FLUSHING;
1540 goto drop;
1541}
1542
1543start_task_failed:
1544{
1545 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1546 (_("Failed to start decoding thread.")), (NULL));
1547 ret = GST_FLOW_ERROR;
1548 goto drop;
1549}
1550process_failed:
1551{
1552 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1553 (_("Failed to process frame.")),
1554 ("Maybe be due to not enough memory or failing driver"));
1555 ret = GST_FLOW_ERROR;
1556 goto drop;
1557}
1558drop:
1559{
1560 gst_video_decoder_drop_frame(decoder, frame);
1561 return ret;
1562}
1563}
1564
1565static gboolean
1566gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1567 GstQuery *query)
1568{
1569 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1570 GstClockTime latency;
1571 gboolean ret = FALSE;
1572
1573 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1574 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1575
1576 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1577 {
1578 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1579 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1580 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1581 gst_video_decoder_set_latency(decoder, latency, latency);
1582 }
1583 else
1584 {
1585 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1586 }
1587
1588 return ret;
1589}
1590
1591static gboolean
1592gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1593{
1594 gboolean ret = TRUE;
1595 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1596
1597 switch (GST_QUERY_TYPE(query))
1598 {
1599 case GST_QUERY_CAPS:
1600 {
1601 GstCaps *filter, *result = NULL;
1602 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1603
1604 gst_query_parse_caps(query, &filter);
1605
1606 if (self->probed_srccaps)
1607 result = gst_caps_ref(self->probed_srccaps);
1608 else
1609 result = gst_pad_get_pad_template_caps(pad);
1610
1611 if (filter)
1612 {
1613 GstCaps *tmp = result;
1614 result =
1615 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1616 gst_caps_unref(tmp);
1617 }
1618
1619 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1620
1621 gst_query_set_caps_result(query, result);
1622 gst_caps_unref(result);
1623 break;
1624 }
1625
1626 default:
1627 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1628 break;
1629 }
1630
1631 return ret;
1632}
1633
1634static GstCaps *
1635gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1636{
1637 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1638 GstCaps *result;
1639
1640 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1641 filter);
1642
1643 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1644
1645 return result;
1646}
1647
1648static gboolean
1649gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1650{
1651 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1652 gboolean ret;
1653 GstEventType type = GST_EVENT_TYPE(event);
bo.xiao8e3054d2024-07-31 17:13:57 +08001654 GST_DEBUG_OBJECT (self, "received event %p %" GST_PTR_FORMAT, event, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001655
1656 switch (type)
1657 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001658 case GST_EVENT_STREAM_START:
1659 {
1660 GstStructure *s;
1661 GstEvent *event;
xuesong.jiang406ee302023-06-28 03:45:22 +00001662 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1663 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1664 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1665 gst_pad_push_event (decoder->sinkpad, event);
1666 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1667 break;
1668 }
zengliang.li51f54b62023-10-10 12:14:49 +00001669 case GST_EVENT_CAPS:
1670 {
1671 GstCaps *caps;
1672 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001673 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001674
1675 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001676
zengliang.li51f54b62023-10-10 12:14:49 +00001677 structure= gst_caps_get_structure(caps, 0);
1678 if ( gst_structure_has_field(structure, "parsed") )
1679 {
1680 gboolean parsed = TRUE;
1681 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1682 {
1683 self->v4l2output->stream_mode = !parsed;
1684 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1685 }
1686 }
sheng.liu641aa422023-12-26 07:05:59 +00001687
xuesong.jiang26749f32024-07-24 09:40:47 +08001688 if ( gst_structure_has_field(structure, "secure") )
1689 {
1690 gboolean is_secure = FALSE;
1691 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1692 {
1693 self->v4l2output->secure_es = is_secure;
1694 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1695 }
1696 }
xuesong.jiang578add22024-07-25 15:11:30 +08001697 else
1698 {
1699 GstCapsFeatures *const features = gst_caps_get_features(caps, 0);
1700 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1701 {
1702 self->v4l2output->secure_es = TRUE;
1703 GST_DEBUG("If there is no secure field in caps, consider dma es is secure");
1704 }
1705 }
xuesong.jiang26749f32024-07-24 09:40:47 +08001706
sheng.liudb26f7d2024-01-22 11:24:23 +00001707 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1708 {
1709 if ( denom == 0 ) denom= 1;
1710
1711 if (self->v4l2capture->fps)
1712 {
1713 g_value_unset(self->v4l2capture->fps);
1714 g_free(self->v4l2capture->fps);
1715 }
1716
1717 self->v4l2capture->fps = g_new0(GValue, 1);
1718 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1719 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1720
1721 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1722 }
1723
sheng.liu641aa422023-12-26 07:05:59 +00001724 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1725 ( !self->v4l2capture->have_set_par ) )
1726 {
1727 if ( (num <= 0) || (denom <= 0) )
1728 {
1729 num= denom= 1;
1730 }
1731
1732 if ( self->v4l2capture->par )
1733 {
1734 g_value_unset(self->v4l2capture->par);
1735 g_free(self->v4l2capture->par);
1736 }
1737
1738 self->v4l2capture->par = g_new0(GValue, 1);
1739 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1740 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1741 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1742 }
zengliang.li51f54b62023-10-10 12:14:49 +00001743 break;
1744 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001745 case GST_EVENT_FLUSH_START:
1746 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001747
1748 g_mutex_lock (&self->res_chg_lock);
1749 while (self->is_res_chg)
1750 {
1751 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1752 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1753 }
1754 g_mutex_unlock (&self->res_chg_lock);
1755
zengliang.li92ff6822023-06-06 07:12:52 +00001756 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001757 gst_aml_v4l2_object_unlock(self->v4l2output);
1758 gst_aml_v4l2_object_unlock(self->v4l2capture);
1759 break;
1760 default:
1761 break;
1762 }
1763
1764 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1765
1766 switch (type)
1767 {
1768 case GST_EVENT_FLUSH_START:
1769 /* The processing thread should stop now, wait for it */
1770 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001771 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001772 GST_DEBUG_OBJECT(self, "flush start done");
1773 break;
1774 default:
1775 break;
1776 }
1777
1778 return ret;
1779}
1780
1781static GstStateChangeReturn
1782gst_aml_v4l2_video_dec_change_state(GstElement *element,
1783 GstStateChange transition)
1784{
1785 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1786 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1787
bo.xiao8e3054d2024-07-31 17:13:57 +08001788 GST_DEBUG_OBJECT(element, "change state from %s to %s",
1789 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1790 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1791
xuesong.jiangae1548e2022-05-06 16:38:46 +08001792 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1793 {
1794 g_atomic_int_set(&self->active, FALSE);
1795 gst_aml_v4l2_object_unlock(self->v4l2output);
1796 gst_aml_v4l2_object_unlock(self->v4l2capture);
1797 gst_pad_stop_task(decoder->srcpad);
1798 }
1799
1800 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1801}
1802
1803static void
1804gst_aml_v4l2_video_dec_dispose(GObject *object)
1805{
1806 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1807
1808 gst_caps_replace(&self->probed_sinkcaps, NULL);
1809 gst_caps_replace(&self->probed_srccaps, NULL);
1810
1811 G_OBJECT_CLASS(parent_class)->dispose(object);
1812}
1813
1814static void
1815gst_aml_v4l2_video_dec_finalize(GObject *object)
1816{
1817 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1818
1819 gst_aml_v4l2_object_destroy(self->v4l2capture);
1820 gst_aml_v4l2_object_destroy(self->v4l2output);
1821
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001822 g_mutex_clear(&self->res_chg_lock);
1823 g_cond_clear(&self->res_chg_cond);
1824
xuesong.jiang61ea8012022-05-12 15:38:17 +08001825#if GST_IMPORT_LGE_PROP
1826 if (self->lge_ctxt)
1827 {
1828 if (self->lge_ctxt->app_type)
1829 g_free(self->lge_ctxt->app_type);
1830 if (self->lge_ctxt->res_info.coretype)
1831 g_free(self->lge_ctxt->res_info.coretype);
1832 free(self->lge_ctxt);
1833 }
1834
1835#endif
1836
xuesong.jiangae1548e2022-05-06 16:38:46 +08001837 G_OBJECT_CLASS(parent_class)->finalize(object);
1838}
1839
1840static void
1841gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1842{
1843 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001844 self->last_out_pts = GST_CLOCK_TIME_NONE;
bo.xiao8e3054d2024-07-31 17:13:57 +08001845 self->frame_duration = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001846 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001847 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001848 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001849 g_mutex_init(&self->res_chg_lock);
1850 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001851#if GST_IMPORT_LGE_PROP
1852 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1853 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1854#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001855}
1856
1857static void
1858gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1859{
1860 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1861 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1862 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1863
1864 gst_video_decoder_set_packetized(decoder, TRUE);
1865
1866 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1867 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1868 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1869 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1870 self->v4l2output->no_initial_format = TRUE;
1871 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001872 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001873
1874 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1875 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1876 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1877 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1878 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001879 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001880}
1881
1882static void
1883gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1884{
1885 GstElementClass *element_class;
1886 GObjectClass *gobject_class;
1887 GstVideoDecoderClass *video_decoder_class;
1888
1889 parent_class = g_type_class_peek_parent(klass);
1890
1891 element_class = (GstElementClass *)klass;
1892 gobject_class = (GObjectClass *)klass;
1893 video_decoder_class = (GstVideoDecoderClass *)klass;
1894
1895 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1896 "AML V4L2 Video Decoder");
1897
1898 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1899 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1900 gobject_class->set_property =
1901 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1902 gobject_class->get_property =
1903 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1904
1905 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1906 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1907 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1908 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1909 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1910 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1911 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1912 video_decoder_class->set_format =
1913 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1914 video_decoder_class->negotiate =
1915 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1916 video_decoder_class->decide_allocation =
1917 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1918 /* FIXME propose_allocation or not ? */
1919 video_decoder_class->handle_frame =
1920 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1921 video_decoder_class->getcaps =
1922 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1923 video_decoder_class->src_query =
1924 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1925 video_decoder_class->sink_event =
1926 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1927
1928 element_class->change_state =
1929 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1930
xuesong.jiang406ee302023-06-28 03:45:22 +00001931 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1932 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1933 G_SIGNAL_RUN_LAST,
1934 0, /* class offset */
1935 NULL, /* accumulator */
1936 NULL, /* accu data */
1937 g_cclosure_marshal_generic,
1938 G_TYPE_NONE,
1939 1,
1940 G_TYPE_UINT64);
le.han7e9c27d2024-08-08 08:10:46 +00001941 g_signals[SIGNAL_DECODED_ERROR_PTS] = g_signal_new ("decoded-error-pts",
1942 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1943 G_SIGNAL_RUN_LAST,
1944 0, /* class offset */
1945 NULL, /* accumulator */
1946 NULL, /* accu data */
1947 g_cclosure_marshal_generic,
1948 G_TYPE_NONE,
1949 1,
1950 G_TYPE_UINT64);
xuesong.jiang406ee302023-06-28 03:45:22 +00001951
xuesong.jiangae1548e2022-05-06 16:38:46 +08001952 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001953#if GST_IMPORT_LGE_PROP
1954 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1955#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001956}
1957
1958static void
1959gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1960{
1961 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1962 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1963 GstAmlV4l2VideoDecCData *cdata = data;
1964
1965 klass->default_device = cdata->device;
1966
1967 /* Note: gst_pad_template_new() take the floating ref from the caps */
1968 gst_element_class_add_pad_template(element_class,
1969 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1970 cdata->sink_caps));
1971 gst_element_class_add_pad_template(element_class,
1972 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1973 cdata->src_caps));
1974
1975 gst_element_class_set_metadata(element_class, cdata->longname,
1976 "Codec/Decoder/Video/Hardware", cdata->description,
1977 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1978
1979 gst_caps_unref(cdata->sink_caps);
1980 gst_caps_unref(cdata->src_caps);
1981 g_free(cdata);
1982}
1983
1984/* Probing functions */
1985gboolean
1986gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1987{
1988 gboolean ret = FALSE;
1989
1990 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()))
1991 ret = TRUE;
1992
1993 return ret;
1994}
1995
1996static gchar *
1997gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1998 const gchar *basename)
1999{
2000 gchar *codec_name = NULL;
2001 gchar *type_name = NULL;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002002
2003#define SET_META(codec) \
2004 G_STMT_START \
2005 { \
2006 cdata->longname = "AML V4L2 " codec " Decoder"; \
2007 cdata->description = "Decodes " codec " streams via V4L2 API"; \
2008 codec_name = g_ascii_strdown(codec, -1); \
2009 } \
2010 G_STMT_END
2011
bo.xiaoce116a42024-07-16 16:21:53 +08002012 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08002013 {
bo.xiaoce116a42024-07-16 16:21:53 +08002014 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002015 }
2016 else if (gst_structure_has_name(s, "video/mpeg"))
2017 {
bo.xiaob6afda52024-08-02 16:08:30 +08002018 //include mpeg1, mpeg2, mpeg4
2019 SET_META("MPEG4");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002020 }
2021 else if (gst_structure_has_name(s, "video/x-h263"))
2022 {
2023 SET_META("H263");
2024 }
2025 else if (gst_structure_has_name(s, "video/x-fwht"))
2026 {
2027 SET_META("FWHT");
2028 }
2029 else if (gst_structure_has_name(s, "video/x-h264"))
2030 {
2031 SET_META("H264");
2032 }
2033 else if (gst_structure_has_name(s, "video/x-h265"))
2034 {
2035 SET_META("H265");
2036 }
2037 else if (gst_structure_has_name(s, "video/x-wmv"))
2038 {
2039 SET_META("VC1");
2040 }
2041 else if (gst_structure_has_name(s, "video/x-vp8"))
2042 {
2043 SET_META("VP8");
2044 }
2045 else if (gst_structure_has_name(s, "video/x-vp9"))
2046 {
2047 SET_META("VP9");
2048 }
2049 else if (gst_structure_has_name(s, "video/x-av1"))
2050 {
2051 SET_META("AV1");
2052 }
zengliang.li51f54b62023-10-10 12:14:49 +00002053 else if (gst_structure_has_name(s, "video/x-avs"))
2054 {
2055 SET_META("AVS");
2056 }
2057 else if (gst_structure_has_name(s, "video/x-avs2"))
2058 {
2059 SET_META("AVS2");
2060 }
2061 else if (gst_structure_has_name(s, "video/x-avs3"))
2062 {
2063 SET_META("AVS3");
2064 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002065 else if (gst_structure_has_name(s, "video/x-bayer"))
2066 {
2067 SET_META("BAYER");
2068 }
2069 else if (gst_structure_has_name(s, "video/x-sonix"))
2070 {
2071 SET_META("SONIX");
2072 }
2073 else if (gst_structure_has_name(s, "video/x-pwc1"))
2074 {
2075 SET_META("PWC1");
2076 }
2077 else if (gst_structure_has_name(s, "video/x-pwc2"))
2078 {
2079 SET_META("PWC2");
2080 }
2081 else
2082 {
2083 /* This code should be kept on sync with the exposed CODEC type of format
2084 * from gstamlv4l2object.c. This warning will only occure in case we forget
2085 * to also add a format here. */
2086 gchar *s_str = gst_structure_to_string(s);
2087 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2088 "bug, please report at https://bugs.gnome.org",
2089 s_str);
2090 g_free(s_str);
2091 }
2092
2093 if (codec_name)
2094 {
2095 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2096 if (g_type_from_name(type_name) != 0)
2097 {
2098 g_free(type_name);
2099 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2100 }
2101
2102 g_free(codec_name);
2103 }
2104
2105 return type_name;
2106#undef SET_META
2107}
2108
2109void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2110 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2111{
2112 gint i;
2113
2114 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2115 {
2116 GstAmlV4l2VideoDecCData *cdata;
2117 GstStructure *s;
2118 GTypeQuery type_query;
2119 GTypeInfo type_info = {
2120 0,
2121 };
2122 GType type, subtype;
2123 gchar *type_name;
2124
2125 s = gst_caps_get_structure(sink_caps, i);
2126
2127 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2128 cdata->device = g_strdup(device_path);
2129 cdata->sink_caps = gst_caps_new_empty();
2130 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2131 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2132 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2133 cdata->src_caps = gst_caps_copy(src_caps);
2134 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2135 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2136 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2137
2138 /* Skip over if we hit an unmapped type */
2139 if (!type_name)
2140 {
2141 g_free(cdata);
2142 continue;
2143 }
2144
2145 type = gst_aml_v4l2_video_dec_get_type();
2146 g_type_query(type, &type_query);
2147 memset(&type_info, 0, sizeof(type_info));
2148 type_info.class_size = type_query.class_size;
2149 type_info.instance_size = type_query.instance_size;
2150 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2151 type_info.class_data = cdata;
2152 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2153
2154 subtype = g_type_register_static(type, type_name, &type_info, 0);
2155 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2156 subtype))
2157 GST_WARNING("Failed to register plugin '%s'", type_name);
2158
2159 g_free(type_name);
2160 }
2161}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002162
2163#if GST_IMPORT_LGE_PROP
2164static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2165{
2166 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2167 g_param_spec_object("resource-info", "resource-info",
2168 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2169 GST_TYPE_STRUCTURE,
2170 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2171
2172 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2173 g_param_spec_uint64("decoded-size", "decoded-size",
2174 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2175 0, G_MAXUINT64,
2176 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2177
2178 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2179 g_param_spec_uint64("undecoded-size", "undecoded-size",
2180 "video decoder element's total undecoded data 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_APP_TYPE,
2185 g_param_spec_string("app-type", "app-type",
2186 "set application type.",
2187 "default_app",
2188 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2189
2190 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2191 g_param_spec_boolean("clip-mode", "clip-mode",
2192 "When seeking, Content is moving faster for a while to skip frames.",
2193 FALSE,
2194 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2195}
2196#endif