blob: 757ee73e5044188503203254cc9d3810845df15c [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
bo.xiao8e3054d2024-07-31 17:13:57 +0800125static GstClockTime calc_output_buffer_pts_func(GstVideoDecoder *decoder);
126static GstClockTime calc_duration_func(GstVideoDecoder *decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800127
128static void
129gst_aml_v4l2_video_dec_set_property(GObject *object,
130 guint prop_id, const GValue *value, GParamSpec *pspec)
131{
132 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
133
134 switch (prop_id)
135 {
136 case PROP_CAPTURE_IO_MODE:
xuesong.jiangae1548e2022-05-06 16:38:46 +0800137 case PROP_DUMP_FRAME_LOCATION:
zengliang.lidcd41462024-06-19 16:05:12 +0800138 case PROP_CC_DATA:
139 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
140 prop_id, value, pspec))
141 {
142 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
143 }
144 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800145#if GST_IMPORT_LGE_PROP
146 case LGE_RESOURCE_INFO:
147 {
148 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
149 GstStructure *r_info = g_value_get_object(value);
150 if (r_info)
151 {
152 if (gst_structure_has_field(r_info, "coretype"))
153 {
154 if (self->lge_ctxt->res_info.coretype)
155 g_free(self->lge_ctxt->res_info.coretype);
156 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
157 }
158 if (gst_structure_has_field(r_info, "videoport"))
159 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
160 if (gst_structure_has_field(r_info, "audioport"))
161 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
162 if (gst_structure_has_field(r_info, "maxwidth"))
163 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
164 if (gst_structure_has_field(r_info, "maxheight"))
165 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
166 if (gst_structure_has_field(r_info, "mixerport"))
167 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
168 }
169 break;
170 }
171 case LGE_APP_TYPE:
172 {
173 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
174 if (self->lge_ctxt->app_type)
175 g_free(self->lge_ctxt->app_type);
176 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
177 break;
178 }
179 case LGE_CLIP_MODE:
180 {
181 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
182 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
183 break;
184 }
185#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800186 /* By default, only set on output */
187 default:
188 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output,
189 prop_id, value, pspec))
190 {
191 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
192 }
193 break;
194 }
195}
196
197static void
198gst_aml_v4l2_video_dec_get_property(GObject *object,
199 guint prop_id, GValue *value, GParamSpec *pspec)
200{
201 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
202
203 switch (prop_id)
204 {
205 case PROP_CAPTURE_IO_MODE:
zengliang.lidcd41462024-06-19 16:05:12 +0800206 case PROP_CC_DATA:
fei.dengaf682762024-06-24 19:06:03 +0800207 case PROP_DECODING_ERROR_FRAMES:
zengliang.lidcd41462024-06-19 16:05:12 +0800208 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
209 prop_id, value, pspec))
210 {
211 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
212 }
213 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800214#if GST_IMPORT_LGE_PROP
215 case LGE_DECODE_SIZE:
216 {
217 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
218 self->lge_ctxt->dec_size = -1;
219 g_value_set_int(value, self->lge_ctxt->dec_size);
220 break;
221 }
222 case LGE_UNDECODE_SIZE:
223 {
224 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
225 self->lge_ctxt->undec_size = -1;
226 g_value_set_int(value, self->lge_ctxt->undec_size);
227 break;
228 }
229#endif
230
xuesong.jiangae1548e2022-05-06 16:38:46 +0800231 /* By default read from output */
232 default:
233 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output,
234 prop_id, value, pspec))
235 {
236 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
237 }
238 break;
239 }
240}
241
242static gboolean
243gst_aml_v4l2_video_dec_open(GstVideoDecoder *decoder)
244{
245 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
246 GstCaps *codec_caps;
247
248 GST_DEBUG_OBJECT(self, "Opening");
249
250 if (!gst_aml_v4l2_object_open(self->v4l2output))
251 goto failure;
252
253 if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output))
254 goto failure;
255
256 codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad);
257 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output,
258 codec_caps);
259 gst_caps_unref(codec_caps);
260
261 if (gst_caps_is_empty(self->probed_sinkcaps))
262 goto no_encoded_format;
263
264 return TRUE;
265
266no_encoded_format:
267 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
268 (_("Decoder on device %s has no supported input format"),
269 self->v4l2output->videodev),
270 (NULL));
271 goto failure;
272
273failure:
274 if (GST_AML_V4L2_IS_OPEN(self->v4l2output))
275 gst_aml_v4l2_object_close(self->v4l2output);
276
277 if (GST_AML_V4L2_IS_OPEN(self->v4l2capture))
278 gst_aml_v4l2_object_close(self->v4l2capture);
279
280 gst_caps_replace(&self->probed_srccaps, NULL);
281 gst_caps_replace(&self->probed_sinkcaps, NULL);
282
283 return FALSE;
284}
285
286static gboolean
287gst_aml_v4l2_video_dec_close(GstVideoDecoder *decoder)
288{
289 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
290
291 GST_DEBUG_OBJECT(self, "Closing");
292
293 gst_aml_v4l2_object_close(self->v4l2output);
294 gst_aml_v4l2_object_close(self->v4l2capture);
295 gst_caps_replace(&self->probed_srccaps, NULL);
296 gst_caps_replace(&self->probed_sinkcaps, NULL);
297
298 return TRUE;
299}
300
301static gboolean
302gst_aml_v4l2_video_dec_start(GstVideoDecoder *decoder)
303{
304 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
305
306 GST_DEBUG_OBJECT(self, "Starting");
307
308 gst_aml_v4l2_object_unlock(self->v4l2output);
309 g_atomic_int_set(&self->active, TRUE);
310 self->output_flow = GST_FLOW_OK;
311
312 return TRUE;
313}
314
315static gboolean
316gst_aml_v4l2_video_dec_stop(GstVideoDecoder *decoder)
317{
318 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
319
320 GST_DEBUG_OBJECT(self, "Stopping");
321
322 gst_aml_v4l2_object_unlock(self->v4l2output);
323 gst_aml_v4l2_object_unlock(self->v4l2capture);
324
325 /* Wait for capture thread to stop */
326 gst_pad_stop_task(decoder->srcpad);
327
328 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
329 self->output_flow = GST_FLOW_OK;
330 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
331
332 /* Should have been flushed already */
333 g_assert(g_atomic_int_get(&self->active) == FALSE);
334
335 gst_aml_v4l2_object_stop(self->v4l2output);
336 gst_aml_v4l2_object_stop(self->v4l2capture);
337
338 if (self->input_state)
339 {
340 gst_video_codec_state_unref(self->input_state);
341 self->input_state = NULL;
342 }
343
344 GST_DEBUG_OBJECT(self, "Stopped");
345
346 return TRUE;
347}
348
349static gboolean
hanghang.luo8e1225b2023-10-10 08:54:28 +0000350gst_aml_v4l2_video_dec_codec_chg(GstVideoDecoder *decoder,
351 GstVideoCodecState *state)
352{
353 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
354 GstStructure *s_old = NULL;
355 GstStructure *s_new = NULL;
356
357 // first play, must set foramt;
358 if (!self->input_state)
359 return TRUE;
360
361 if (self->input_state->caps)
362 s_old = gst_caps_get_structure(self->input_state->caps,0);
363 if (state->caps)
364 s_new = gst_caps_get_structure(state->caps,0);
365
366 if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old)))
367 return TRUE;
368 return FALSE;
369}
370
371static gboolean
372gst_aml_v4l2_video_dec_res_chg(GstVideoDecoder *decoder,
373 GstVideoCodecState *state)
374{
375 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
376 gboolean ret = FALSE;
377 gint width_new = -1,height_new = -1,width_old = -1,height_old = -1;
378 GstStructure *s_old = NULL;
379 GstStructure *s_new = NULL;
380
381 // first play, must set foramt;
382 if (!self->input_state)
383 {
384 ret = TRUE;
385 goto done;
386 }
387
388 if (self->input_state->caps)
389 s_old = gst_caps_get_structure(self->input_state->caps,0);
390 if (state->caps)
391 s_new = gst_caps_get_structure(state->caps,0);
392
393 if (s_new && gst_structure_has_field(s_new,"width") && gst_structure_has_field(s_new,"height"))
394 {
395 gst_structure_get_int(s_new,"width",&width_new);
396 gst_structure_get_int(s_new,"height",&height_new);
397 }
398 if (s_old && gst_structure_has_field(s_old,"width") && gst_structure_has_field(s_old,"height"))
399 {
400 gst_structure_get_int(s_old,"width",&width_old);
401 gst_structure_get_int(s_old,"height",&height_old);
402 }
403
404 if (width_new != width_old || height_new != height_old)
405 ret = TRUE;
406
407done:
408 GST_DEBUG_OBJECT(self, "ret is %d",ret);
409 return ret;
410}
411
412static gboolean
xuesong.jiangae1548e2022-05-06 16:38:46 +0800413gst_aml_v4l2_video_dec_set_format(GstVideoDecoder *decoder,
414 GstVideoCodecState *state)
415{
416 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
417 gboolean ret = TRUE;
418 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
419 GstCaps *caps;
420
421 GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps);
hanghang.luo8e1225b2023-10-10 08:54:28 +0000422 if (self->input_state)
423 {
424 if (gst_aml_v4l2_video_dec_res_chg(decoder,state) || gst_aml_v4l2_video_dec_codec_chg(decoder,state))
425 GST_DEBUG_OBJECT(self, "resolution or codec changed");
426 else
427 goto done;
428 }
429
xuesong.jiangae1548e2022-05-06 16:38:46 +0800430 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
hanghang.luoc54208e2023-09-22 02:43:54 +0000431 GstStructure *s = gst_caps_get_structure(state->caps,0);
432 if (s && gst_structure_has_field(s,"format"))
433 {
434 if (!strcmp("XVID",gst_structure_get_string(s,"format")))
435 {
436 GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support");
437 ret = FALSE;
438 goto done;
439 }
440 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800441
442 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
443 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
444
445 if (self->input_state)
446 {
447 if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps))
448 {
449 GST_DEBUG_OBJECT(self, "Compatible caps");
450 goto done;
451 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800452
453 gst_aml_v4l2_video_dec_finish(decoder);
454 gst_aml_v4l2_object_stop(self->v4l2output);
455
fei.dengf78f7122024-07-15 14:44:04 +0800456 gst_video_codec_state_unref(self->input_state);
457 self->input_state = NULL;
458
xuesong.jiangae1548e2022-05-06 16:38:46 +0800459 /* The renegotiation flow don't blend with the base class flow. To properly
460 * stop the capture pool, if the buffers can't be orphaned, we need to
461 * reclaim our buffers, which will happend through the allocation query.
462 * The allocation query is triggered by gst_video_decoder_negotiate() which
463 * requires the output caps to be set, but we can't know this information
464 * as we rely on the decoder, which requires the capture queue to be
465 * stopped.
466 *
467 * To workaround this issue, we simply run an allocation query with the
468 * old negotiated caps in order to drain/reclaim our buffers. That breaks
469 * the complexity and should not have much impact in performance since the
470 * following allocation query will happen on a drained pipeline and won't
471 * block. */
472 if (self->v4l2capture->pool &&
473 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
474 {
475 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
476 if (caps)
477 {
478 GstQuery *query = gst_query_new_allocation(caps, FALSE);
479 gst_pad_peer_query(decoder->srcpad, query);
480 gst_query_unref(query);
481 gst_caps_unref(caps);
482 }
483 }
484
485 gst_aml_v4l2_object_stop(self->v4l2capture);
486 self->output_flow = GST_FLOW_OK;
487 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800488 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
489 {
490 GST_ERROR_OBJECT(self, "config output drm mode error");
491 goto done;
492 }
493
xuesong.jiang22a9b112023-05-24 09:01:59 +0000494 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
495 {
496 GST_ERROR_OBJECT(self, "config output stream mode error");
497 goto done;
498 }
499
hanghang.luoac6df002024-07-11 10:54:17 +0800500 if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error))
501 {
502 GST_ERROR_OBJECT(self, "set format error");
503 goto done;
504 }
505
506 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
507 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
508 {
509 GST_ERROR_OBJECT(self, "config I frame mode error");
510 goto done;
511 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800512
513 gst_caps_replace(&self->probed_srccaps, NULL);
514 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
515 gst_aml_v4l2_object_get_raw_caps());
516
517 if (gst_caps_is_empty(self->probed_srccaps))
518 goto no_raw_format;
519
520 caps = gst_caps_copy(self->probed_srccaps);
521 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
522 gst_caps_append(self->probed_srccaps, caps);
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800523
xuesong.jiangae1548e2022-05-06 16:38:46 +0800524 if (ret)
525 self->input_state = gst_video_codec_state_ref(state);
526 else
527 gst_aml_v4l2_error(self, &error);
528
529done:
530 return ret;
531
532no_raw_format:
533 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
534 (_("Decoder on device %s has no supported output format"),
535 self->v4l2output->videodev),
536 (NULL));
537 return GST_FLOW_ERROR;
538}
539
540static gboolean
541gst_aml_v4l2_video_dec_flush(GstVideoDecoder *decoder)
542{
543 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
544
545 GST_DEBUG_OBJECT(self, "Flushed");
546
547 /* Ensure the processing thread has stopped for the reverse playback
548 * discount case */
549 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
550 {
551 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
552
553 gst_aml_v4l2_object_unlock(self->v4l2output);
554 gst_aml_v4l2_object_unlock(self->v4l2capture);
555 gst_pad_stop_task(decoder->srcpad);
556 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
557 }
558
559 self->output_flow = GST_FLOW_OK;
560
561 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
562 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
563
564 if (self->v4l2output->pool)
565 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
566
567 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
568 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
569 * pool. */
570 if (self->v4l2capture->pool)
571 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
572
573 return TRUE;
574}
575
576static gboolean
577gst_aml_v4l2_video_dec_negotiate(GstVideoDecoder *decoder)
578{
579 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
580
xuesong.jiang681d3602022-06-24 21:23:35 +0800581 if (TRUE == self->v4l2output->is_svp)
582 {
583 GstStructure *s;
584 GstEvent *event;
585
586 s = gst_structure_new_empty ("IS_SVP");
587 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
588 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
589 gst_pad_push_event (decoder->srcpad, event);
590 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
591 }
592
xuesong.jiangae1548e2022-05-06 16:38:46 +0800593 /* We don't allow renegotiation without carefull disabling the pool */
594 if (self->v4l2capture->pool &&
595 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
596 return TRUE;
597
598 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
599}
600
601static gboolean
602gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
603{
604 struct v4l2_decoder_cmd dcmd = {
605 0,
606 };
607
608 GST_DEBUG_OBJECT(v4l2object->element,
609 "sending v4l2 decoder command %u with flags %u", cmd, flags);
610
611 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
612 return FALSE;
613
614 dcmd.cmd = cmd;
615 dcmd.flags = flags;
616 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
617 goto dcmd_failed;
618
619 return TRUE;
620
621dcmd_failed:
622 if (errno == ENOTTY)
623 {
624 GST_INFO_OBJECT(v4l2object->element,
625 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
626 cmd, flags, v4l2object->videodev, g_strerror(errno));
627 }
628 else
629 {
630 GST_ERROR_OBJECT(v4l2object->element,
631 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
632 cmd, flags, v4l2object->videodev, g_strerror(errno));
633 }
634 return FALSE;
635}
636
637static GstFlowReturn
638gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
639{
640 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
641 GstFlowReturn ret = GST_FLOW_OK;
642 GstBuffer *buffer;
643
644 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
645 goto done;
646
647 GST_DEBUG_OBJECT(self, "Finishing decoding");
648
649 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
650
651 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
652 {
653 GstTask *task = decoder->srcpad->task;
654
655 /* If the decoder stop command succeeded, just wait until processing is
656 * finished */
657 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
658 GST_OBJECT_LOCK(task);
659 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
660 GST_TASK_WAIT(task);
661 GST_OBJECT_UNLOCK(task);
662 ret = GST_FLOW_FLUSHING;
663 }
664 else
665 {
666 /* otherwise keep queuing empty buffers until the processing thread has
667 * stopped, _pool_process() will return FLUSHING when that happened */
668 while (ret == GST_FLOW_OK)
669 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800670 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800671 buffer = gst_buffer_new();
672 ret =
673 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
674 gst_buffer_unref(buffer);
675 }
676 }
677
678 /* and ensure the processing thread has stopped in case another error
679 * occured. */
680 gst_aml_v4l2_object_unlock(self->v4l2capture);
681 gst_pad_stop_task(decoder->srcpad);
682 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
683
684 if (ret == GST_FLOW_FLUSHING)
685 ret = self->output_flow;
686
fei.deng4244a262024-07-31 14:07:14 +0800687 /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop.
688 should reset need_wait_event=true to wait source change event*/
689 self->v4l2capture->need_wait_event = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800690 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
691
692 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
693
694done:
695 return ret;
696}
697
698static GstFlowReturn
699gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
700{
701 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
702
703 GST_DEBUG_OBJECT(self, "Draining...");
704 gst_aml_v4l2_video_dec_finish(decoder);
705 gst_aml_v4l2_video_dec_flush(decoder);
706
707 return GST_FLOW_OK;
708}
709
710static GstVideoCodecFrame *
xuesong.jiange24aef92023-06-16 06:39:10 +0000711gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800712{
713 GstVideoCodecFrame *frame = NULL;
714 GList *frames, *l;
715 gint count = 0;
716
xuesong.jiange24aef92023-06-16 06:39:10 +0000717 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
718
fei.dengbee20862022-06-14 14:59:48 +0800719 frames = gst_video_decoder_get_frames(decoder);
720
721 for (l = frames; l != NULL; l = l->next)
722 {
723 GstVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000724
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800725 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800726 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800727 }
fei.dengbee20862022-06-14 14:59:48 +0800728 count++;
729 }
730
zengliang.liddee2da2023-07-14 07:27:05 +0000731 if (!frame)
732 {
733 for (l = frames; l != NULL; l = l->next)
734 {
735 GstVideoCodecFrame *f = l->data;
736 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
737 {
738 frame = f;
fei.dengf78f7122024-07-15 14:44:04 +0800739 GST_DEBUG("The pts of the expected output frame is invalid");
740 break;
zengliang.liddee2da2023-07-14 07:27:05 +0000741 }
zengliang.liddee2da2023-07-14 07:27:05 +0000742 }
743 }
744
fei.dengbee20862022-06-14 14:59:48 +0800745 if (frame)
746 {
747 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800748 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
749 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
fei.dengbee20862022-06-14 14:59:48 +0800750 gst_video_codec_frame_ref(frame);
751 }
752
753 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
754
755 return frame;
756}
757
xuesong.jiange24aef92023-06-16 06:39:10 +0000758static GstVideoCodecFrame *
759gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstVideoDecoder *decoder, GstClockTime pts)
760{
761 GstVideoCodecFrame *frame = NULL;
762 GList *frames, *l;
hanghang.luo4486d7d2024-07-24 10:06:35 +0800763 guint frames_len = 0;
xuesong.jiange24aef92023-06-16 06:39:10 +0000764 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
765
hanghang.luo4486d7d2024-07-24 10:06:35 +0800766 if (!(frames = gst_video_decoder_get_frames(decoder)))
767 goto done;
768
xuesong.jiange24aef92023-06-16 06:39:10 +0000769 frames_len = g_list_length(frames);
770 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
771
xuesong.jiange24aef92023-06-16 06:39:10 +0000772 for (l = frames; l != NULL; l = l->next)
773 {
774 GstVideoCodecFrame *f = l->data;
775
776 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
777 {
778 /* found the right frame */
bo.xiao8e3054d2024-07-31 17:13:57 +0800779 GST_LOG_OBJECT(decoder,
780 "found frame %" GST_TIME_FORMAT "with pts %" GST_TIME_FORMAT,
781 GST_TIME_ARGS(f->pts), GST_TIME_ARGS(pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000782 frame = f;
783 break;
784 }
785 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
786 {
787 GST_LOG_OBJECT(decoder,
788 "stream mode drop frame %d %" GST_TIME_FORMAT,
hanghang.luoa0626d22024-07-30 16:10:10 +0800789 f->system_frame_number, GST_TIME_ARGS(f->pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000790
791 gst_video_codec_frame_ref(f);
792 // gst_video_decoder_drop_frame(decoder, f);
793 gst_video_decoder_release_frame(decoder, f);
794 }
795 else
796 {
797 GST_LOG_OBJECT (decoder, "dbg");
798 }
799 }
800
801 if (frame)
802 {
803 guint l_len = 0;
804 l = gst_video_decoder_get_frames(decoder);
805 l_len = g_list_length(l);
806 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
807
808 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800809 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
810 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
xuesong.jiange24aef92023-06-16 06:39:10 +0000811 gst_video_codec_frame_ref(frame);
812 }
813
814 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
815
hanghang.luo4486d7d2024-07-24 10:06:35 +0800816done:
xuesong.jiange24aef92023-06-16 06:39:10 +0000817 return frame;
818}
819
820static GstVideoCodecFrame *
821gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
822{
823 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
824 if (self->v4l2output->stream_mode)
825 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
826 else
827 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
828}
829
xuesong.jiangae1548e2022-05-06 16:38:46 +0800830static gboolean
831gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
832 GstStructure *structure, gpointer user_data)
833{
834 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
835 GstVideoAlignment *align = &self->v4l2capture->align;
836 GstVideoInfo *info = &self->v4l2capture->info;
837 int width, height;
838
839 if (!gst_structure_get_int(structure, "width", &width))
840 return TRUE;
841
842 if (!gst_structure_get_int(structure, "height", &height))
843 return TRUE;
844
845 if (align->padding_left != 0 || align->padding_top != 0 ||
846 height != info->height + align->padding_bottom)
847 return TRUE;
848
849 if (height == info->height + align->padding_bottom)
850 {
851 /* Some drivers may round up width to the padded with */
852 if (width == info->width + align->padding_right)
853 gst_structure_set(structure,
854 "width", G_TYPE_INT, width - align->padding_right,
855 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
856 /* Some drivers may keep visible width and only round up bytesperline */
857 else if (width == info->width)
858 gst_structure_set(structure,
859 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
860 }
861
862 return TRUE;
863}
864
865static void
sheng.liubcf036c2022-06-21 15:55:42 +0800866gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800867{
868 struct v4l2_event evt;
869 gint ret;
870
871 memset (&evt, 0x00, sizeof (struct v4l2_event));
872 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
873 if (ret < 0)
874 {
875 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
876 return;
877 }
878
879 switch (evt.type)
880 {
881 case V4L2_EVENT_SOURCE_CHANGE:
882 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
883 break;
884 case V4L2_EVENT_EOS:
885 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
886 break;
887 default:
888 break;
889 }
890
891 return;
892}
893
894static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000895gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
896{
897 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
898 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000899 struct v4l2_selection sel;
900 struct v4l2_rect *r = NULL;
901 GstStructure *s;
902 gint width = 0;
903 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800904 GST_DEBUG("%d %d",info.width, info.height);
hanghang.luo2eec4892023-07-18 06:44:42 +0000905 output_state = gst_video_decoder_set_output_state(decoder,
906 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000907 memset(&sel, 0, sizeof(struct v4l2_selection));
908 sel.type = self->v4l2capture->type;
909 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
910 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
911 {
912 r = &sel.r;
913 width = (r->width/2)*2;
914 height = (r->height/2)*2;
915 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
916 }
917 else
918 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000919 if (output_state)
920 {
921 output_state->info.interlace_mode = info.interlace_mode;
922 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000923 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000924 s = gst_caps_get_structure(output_state->caps, 0);
925 if (s)
926 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800927 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
928 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
929 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
930 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000931 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
932 gst_video_codec_state_unref(output_state);
933 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000934 }
935}
936
zengliang.lidcd41462024-06-19 16:05:12 +0800937static GQuark
938gst_aml_v4l2_buffer_pool_cc_import_quark (void)
939{
940 static GQuark quark = 0;
941
942 if (quark == 0)
943 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
944
945 return quark;
946}
947
948static gboolean
949foreach_cc_buffer_list_match_pts_func(GList *list , GstVideoCodecFrame *frame)
950{
951 GList *l;
952 if (g_list_length (list) > 0)
953 {
954 for (l = list; l != NULL; l = l->next)
955 {
956 GstBuffer *cc_buffer = l->data;
957 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
958 {
959 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
960 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
961 #if 0
962 //Debug code:dump cc data
963 GstMapInfo gst_map;
964 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
965 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
966 if (gst_map.size>0)
967 write(fd,gst_map.data,gst_map.size);
968 close(fd);
969 gst_buffer_unmap(cc_buffer,&gst_map);
970 #endif
971 GST_DEBUG("match success");
972 return TRUE;
973 }
974 else
975 {
976 GST_DEBUG("match fail");
977 }
978 }
979 GST_WARNING("no match frame in the bufferlist");
980 }
981 else
982 {
983 GST_WARNING("list is null,can not foreach");
984 }
985 return FALSE;
986}
987
bo.xiao8e3054d2024-07-31 17:13:57 +0800988static GstClockTime
989calc_output_buffer_pts_func(GstVideoDecoder *decoder)
990{
991 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
992 GstClockTime pts = GST_CLOCK_TIME_NONE;
993
994 if (GST_CLOCK_TIME_IS_VALID (self->last_out_pts) && GST_CLOCK_TIME_IS_VALID(self->frame_duration)) {
995 pts = self->last_out_pts + self->frame_duration;
996 GST_LOG_OBJECT (decoder,
997 "calculate PTS %" GST_TIME_FORMAT " by duration: %" GST_TIME_FORMAT,
998 GST_TIME_ARGS (pts), GST_TIME_ARGS (self->frame_duration));
999 }
1000 else
1001 {
1002 pts = 0;
1003 GST_INFO_OBJECT (decoder,"Set PTS=0");
1004 }
1005 return pts;
1006}
1007
1008static GstClockTime
1009calc_duration_func(GstVideoDecoder *decoder)
1010{
1011 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1012 GstClockTime duration = GST_CLOCK_TIME_NONE;
1013 gint fps_n, fps_d;
1014 fps_n = gst_value_get_fraction_numerator(self->v4l2capture->fps);
1015 fps_d = gst_value_get_fraction_denominator(self->v4l2capture->fps);
1016
1017 if (fps_n != 0 && fps_d != 0)
1018 {
1019 duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
1020
1021 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1022 {
1023 duration = duration / 2;
1024 }
1025 }
1026
1027 GST_INFO_OBJECT (decoder,"framerate(fps_d: %d, fps_n: %d) -> frame_duration = %" GST_TIME_FORMAT, fps_d, fps_n, GST_TIME_ARGS(duration));
1028
1029 return duration;
1030}
1031
hanghang.luo2eec4892023-07-18 06:44:42 +00001032static void
xuesong.jiangae1548e2022-05-06 16:38:46 +08001033gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
1034{
1035 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1036 GstAmlV4l2BufferPool *v4l2_pool;
1037 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1038 GstBufferPool *pool;
1039 GstVideoCodecFrame *frame;
1040 GstBuffer *buffer = NULL;
1041 GstFlowReturn ret;
1042
1043 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1044 {
1045 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001046 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001047 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001048 GST_DEBUG_OBJECT(self, "waitting source change event");
1049 /* Wait until received SOURCE_CHANGE event to get right video format */
1050 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1051 {
1052 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1053 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1054 {
fei.deng2a06e042023-10-10 03:09:45 +00001055 //let flush start event blocked until capture buffer pool actived
1056 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001057 GST_DEBUG_OBJECT(self, "Received source change event");
1058 break;
1059 }
1060 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1061 {
1062 GST_DEBUG_OBJECT(self, "Received eos event");
1063 goto beach;
1064 }
1065 else if (ret != GST_FLOW_OK)
1066 {
1067 GST_ERROR_OBJECT(self, "dqevent error");
1068 goto beach;
1069 }
1070 }
1071 self->v4l2capture->need_wait_event = FALSE;
1072
sheng.liu0c77f6c2022-06-17 21:33:20 +08001073 if (TRUE == self->v4l2output->is_svp)
1074 {
1075 GstPad *peer;
1076 GstStructure *s;
1077 GstEvent *event;
1078
1079 peer = gst_pad_get_peer (decoder->srcpad);
1080 if (peer)
1081 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001082 s = gst_structure_new_empty ("IS_SVP");
1083 if (s)
1084 {
1085 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1086 gst_pad_send_event (peer, event);
1087 GST_DEBUG_OBJECT(self, "Send SVP Event");
1088 }
1089 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001090 }
1091 }
1092
sheng.liub56bbc52022-06-21 11:02:33 +08001093 if (self->v4l2capture->need_drop_event)
1094 {
1095 // drop V4L2_EVENT_SOURCE_CHANGE
1096 gst_v4l2_drop_event(self->v4l2capture);
1097 self->v4l2capture->need_drop_event = FALSE;
1098 }
1099
xuesong.jiangae1548e2022-05-06 16:38:46 +08001100 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1101 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001102
xuesong.jiangae1548e2022-05-06 16:38:46 +08001103 /* Create caps from the acquired format, remove the format field */
1104 acquired_caps = gst_video_info_to_caps(&info);
1105 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1106 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001107 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1108
1109 /* Probe currently available pixel formats */
1110 available_caps = gst_caps_copy(self->probed_srccaps);
1111 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1112
1113 /* Replace coded size with visible size, we want to negotiate visible size
1114 * with downstream, not coded size. */
1115 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1116
1117 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001118 caps = gst_caps_copy(filter);
1119 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1120 gst_caps_append(filter, caps);
1121
1122 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1123 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001124 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001125 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1126 gst_caps_unref(filter);
1127
1128 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1129 if (gst_caps_is_empty(caps))
1130 {
1131 gst_caps_unref(caps);
1132 goto not_negotiated;
1133 }
1134
1135 /* Fixate pixel format */
1136 caps = gst_caps_fixate(caps);
1137
1138 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1139
1140 /* Try to set negotiated format, on success replace acquired format */
1141 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1142 gst_video_info_from_caps(&info, caps);
1143 else
1144 gst_aml_v4l2_clear_error(&error);
1145 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001146 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001147 if (!gst_video_decoder_negotiate(decoder))
1148 {
1149 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1150 goto flushing;
1151 else
1152 goto not_negotiated;
1153 }
1154
1155 /* Ensure our internal pool is activated */
1156 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1157 TRUE))
1158 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001159
bo.xiao8e3054d2024-07-31 17:13:57 +08001160 //cal duration when got resolution event
1161 self->frame_duration = calc_duration_func(decoder);
1162
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001163 g_mutex_lock(&self->res_chg_lock);
1164 GST_LOG_OBJECT(decoder, "signal resolution changed");
1165 self->is_res_chg = FALSE;
1166 g_cond_signal(&self->res_chg_cond);
1167 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001168 }
1169
1170 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1171
1172 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1173
1174 self->output_flow = GST_FLOW_OK;
1175 do
1176 {
1177 /* We cannot use the base class allotate helper since it taking the internal
1178 * stream lock. we know that the acquire may need to poll until more frames
1179 * comes in and holding this lock would prevent that.
1180 */
1181 pool = gst_video_decoder_get_buffer_pool(decoder);
1182
1183 /* Pool may be NULL if we started going to READY state */
1184 if (pool == NULL)
1185 {
fei.dengbee20862022-06-14 14:59:48 +08001186 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001187 ret = GST_FLOW_FLUSHING;
1188 goto beach;
1189 }
1190
1191 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001192
xuesong.jiangae1548e2022-05-06 16:38:46 +08001193 g_object_unref(pool);
1194
fei.deng9a5cd6e2023-06-30 12:09:18 +00001195 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 +00001196 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1197 self->v4l2capture->need_drop_event = TRUE;
1198 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1199 if (self->is_res_chg) {
1200 //we must release last buffer
1201 gst_buffer_unref(buffer);
1202 //if resolution changed event received,we should set need_drop_event to false
1203 self->v4l2capture->need_drop_event = FALSE;
1204 gst_aml_v4l2_object_stop(self->v4l2capture);
1205 //unblock flush start event
1206 g_mutex_lock(&self->res_chg_lock);
1207 self->is_res_chg = FALSE;
1208 g_cond_signal(&self->res_chg_cond);
1209 g_mutex_unlock(&self->res_chg_lock);
1210 return;
1211 } else {
1212 goto beach;
1213 }
sheng.liub56bbc52022-06-21 11:02:33 +08001214 }
1215
zengliang.lidcd41462024-06-19 16:05:12 +08001216 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1217 {
1218 GST_DEBUG_OBJECT(decoder, "continue get cc data ");
1219 continue;
1220 }
1221
sheng.liu8d18ed22022-05-26 17:28:15 +08001222 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1223 {
1224 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001225
1226 g_mutex_lock (&self->res_chg_lock);
1227 self->is_res_chg = TRUE;
1228 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001229 return;
1230 }
fei.dengaf682762024-06-24 19:06:03 +08001231 //decoding error happened
1232 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1233 {
1234 continue;
1235 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001236
fei.dengbee20862022-06-14 14:59:48 +08001237 if (ret != GST_FLOW_OK) {
1238 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001239 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001240 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001241
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001242 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001243 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001244
bo.xiao8e3054d2024-07-31 17:13:57 +08001245 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 +00001246 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1247
xuesong.jiangae1548e2022-05-06 16:38:46 +08001248 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1249 {
1250 gst_aml_v4l2_object_stop(self->v4l2capture);
1251 return;
1252 }
1253
fei.dengaf682762024-06-24 19:06:03 +08001254 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1255 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
1256 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001257
1258 if (ret != GST_FLOW_OK)
1259 goto beach;
1260
bo.xiao8e3054d2024-07-31 17:13:57 +08001261 if (!GST_BUFFER_PTS_IS_VALID (buffer))
1262 {
1263 GST_BUFFER_TIMESTAMP(buffer) = calc_output_buffer_pts_func(decoder);
1264 }
1265
1266 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1267 {
1268 GST_BUFFER_DURATION(buffer) = self->frame_duration; // got at resolution event.
1269 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1270 }
1271
fei.dengbee20862022-06-14 14:59:48 +08001272 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001273 if (frame)
1274 {
fei.dengccc89632022-07-15 19:10:17 +08001275 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001276 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001277 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1278 frame->duration = GST_BUFFER_DURATION(buffer);
bo.xiao8e3054d2024-07-31 17:13:57 +08001279
xuesong.jiangae1548e2022-05-06 16:38:46 +08001280 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001281
zengliang.lidcd41462024-06-19 16:05:12 +08001282 if (self->v4l2capture->enable_cc_data)
1283 {
1284 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1285 {
1286 GST_DEBUG("cc buffer and frame bind success");
1287 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1288 GST_AML_V4L2_CC_IMPORT_QUARK);
1289 #if 0
1290 //Debug code:dump cc data
1291 GstMapInfo gst_map;
1292 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1293 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1294 if (gst_map.size>0)
1295 write(fd,gst_map.data,gst_map.size);
1296 close(fd);
1297 gst_buffer_unmap(cc_buffer,&gst_map);
1298 #endif
1299 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1300 gst_buffer_unref(cc_buffer);
1301 }
1302 else
1303 {
1304 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1305 }
1306 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001307 ret = gst_video_decoder_finish_frame(decoder, frame);
1308
1309 if (ret != GST_FLOW_OK)
1310 goto beach;
1311 }
1312 else
1313 {
bo.xiao8e3054d2024-07-31 17:13:57 +08001314 GST_WARNING_OBJECT(decoder, "Unmatch buffer, should be push, need refine");
1315 //gst_pad_push (decoder->srcpad, buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001316 gst_buffer_unref(buffer);
1317 }
1318
1319 return;
1320 /* ERRORS */
1321not_negotiated:
1322{
1323 GST_ERROR_OBJECT(self, "not negotiated");
1324 ret = GST_FLOW_NOT_NEGOTIATED;
1325 goto beach;
1326}
1327activate_failed:
1328{
1329 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1330 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1331 (_("Failed to allocate required memory.")),
1332 ("Buffer pool activation failed"));
1333 ret = GST_FLOW_ERROR;
1334 goto beach;
1335}
1336flushing:
1337{
1338 ret = GST_FLOW_FLUSHING;
1339 goto beach;
1340}
1341beach:
1342 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1343 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001344 if (self->is_res_chg) {
1345 //unblock flush start event
1346 g_mutex_lock(&self->res_chg_lock);
1347 self->is_res_chg = FALSE;
1348 g_cond_signal(&self->res_chg_cond);
1349 g_mutex_unlock(&self->res_chg_lock);
1350 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001351 gst_buffer_replace(&buffer, NULL);
1352 self->output_flow = ret;
1353 gst_aml_v4l2_object_unlock(self->v4l2output);
1354 gst_pad_pause_task(decoder->srcpad);
1355}
1356
1357static GstFlowReturn
1358gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1359 GstVideoCodecFrame *frame)
1360{
1361 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1362 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1363 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1364 GstFlowReturn ret = GST_FLOW_OK;
1365 gboolean processed = FALSE;
1366 GstBuffer *tmp;
1367 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001368
1369 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1370
1371 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1372 goto flushing;
1373
1374 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1375 {
1376 if (!self->input_state)
1377 goto not_negotiated;
1378 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1379 &error))
1380 goto not_negotiated;
1381 }
1382
1383 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1384 {
1385 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001386 GstCapsFeatures *features = NULL;
1387
1388 features = gst_caps_get_features(self->input_state->caps, 0);
xuesong.jiang26749f32024-07-24 09:40:47 +08001389 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
sheng.liu0c77f6c2022-06-17 21:33:20 +08001390 {
1391 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang26749f32024-07-24 09:40:47 +08001392 //TODO:need rm is_svp flag and just using secure_es flag
sheng.liu0c77f6c2022-06-17 21:33:20 +08001393 self->v4l2output->is_svp = TRUE;
1394 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001395
1396 GST_DEBUG_OBJECT(self, "Sending header");
1397
1398 codec_data = self->input_state->codec_data;
1399
1400 /* We are running in byte-stream mode, so we don't know the headers, but
1401 * we need to send something, otherwise the decoder will refuse to
1402 * intialize.
1403 */
1404 if (codec_data)
1405 {
1406 gst_buffer_ref(codec_data);
1407 }
1408 else
1409 {
1410 codec_data = gst_buffer_ref(frame->input_buffer);
1411 processed = TRUE;
1412 }
1413
1414 /* Ensure input internal pool is active */
1415 if (!gst_buffer_pool_is_active(pool))
1416 {
1417 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001418 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1419 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001420 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1421 // self->v4l2output->info.size, min, max);
1422 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1423
1424 /* There is no reason to refuse this config */
1425 if (!gst_buffer_pool_set_config(pool, config))
1426 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001427 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001428
1429 if (!gst_buffer_pool_set_active(pool, TRUE))
1430 goto activate_failed;
1431 }
1432
1433 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1434 ret =
1435 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001436 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001437 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1438
1439 gst_buffer_unref(codec_data);
1440
1441 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1442 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1443 * and returns the visible size as with/height and the coded size as
1444 * padding. */
1445 }
1446
1447 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1448 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1449 {
1450 /* It's possible that the processing thread stopped due to an error */
1451 if (self->output_flow != GST_FLOW_OK &&
1452 self->output_flow != GST_FLOW_FLUSHING)
1453 {
1454 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1455 ret = self->output_flow;
1456 goto drop;
1457 }
1458
1459 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1460
1461 /* Start the processing task, when it quits, the task will disable input
1462 * processing to unlock input if draining, or prevent potential block */
1463 self->output_flow = GST_FLOW_FLUSHING;
1464 if (!gst_pad_start_task(decoder->srcpad,
1465 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1466 goto start_task_failed;
1467 }
1468
1469 if (!processed)
1470 {
1471 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001472 if (!self->codec_data_inject && self->input_state->codec_data)
1473 {
1474 ret = gst_aml_v4l2_buffer_pool_process
1475 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1476 self->codec_data_inject = TRUE;
1477 if (ret != GST_FLOW_OK)
1478 goto send_codec_failed;
1479 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001480 ret =
1481 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1482 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1483
1484 if (ret == GST_FLOW_FLUSHING)
1485 {
1486 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1487 GST_TASK_STARTED)
1488 ret = self->output_flow;
1489 goto drop;
1490 }
1491 else if (ret != GST_FLOW_OK)
1492 {
1493 goto process_failed;
1494 }
1495 }
1496
1497 /* No need to keep input arround */
1498 tmp = frame->input_buffer;
1499 frame->input_buffer = gst_buffer_new();
1500 gst_buffer_copy_into(frame->input_buffer, tmp,
1501 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1502 GST_BUFFER_COPY_META,
1503 0, 0);
1504 gst_buffer_unref(tmp);
1505
1506 gst_video_codec_frame_unref(frame);
1507 return ret;
1508
1509 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001510send_codec_failed:
1511 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1512 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001513not_negotiated:
1514{
1515 GST_ERROR_OBJECT(self, "not negotiated");
1516 ret = GST_FLOW_NOT_NEGOTIATED;
1517 gst_aml_v4l2_error(self, &error);
1518 goto drop;
1519}
1520activate_failed:
1521{
1522 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1523 (_("Failed to allocate required memory.")),
1524 ("Buffer pool activation failed"));
1525 ret = GST_FLOW_ERROR;
1526 goto drop;
1527}
1528flushing:
1529{
1530 ret = GST_FLOW_FLUSHING;
1531 goto drop;
1532}
1533
1534start_task_failed:
1535{
1536 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1537 (_("Failed to start decoding thread.")), (NULL));
1538 ret = GST_FLOW_ERROR;
1539 goto drop;
1540}
1541process_failed:
1542{
1543 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1544 (_("Failed to process frame.")),
1545 ("Maybe be due to not enough memory or failing driver"));
1546 ret = GST_FLOW_ERROR;
1547 goto drop;
1548}
1549drop:
1550{
1551 gst_video_decoder_drop_frame(decoder, frame);
1552 return ret;
1553}
1554}
1555
1556static gboolean
1557gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1558 GstQuery *query)
1559{
1560 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1561 GstClockTime latency;
1562 gboolean ret = FALSE;
1563
1564 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1565 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1566
1567 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1568 {
1569 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1570 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1571 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1572 gst_video_decoder_set_latency(decoder, latency, latency);
1573 }
1574 else
1575 {
1576 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1577 }
1578
1579 return ret;
1580}
1581
1582static gboolean
1583gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1584{
1585 gboolean ret = TRUE;
1586 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1587
1588 switch (GST_QUERY_TYPE(query))
1589 {
1590 case GST_QUERY_CAPS:
1591 {
1592 GstCaps *filter, *result = NULL;
1593 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1594
1595 gst_query_parse_caps(query, &filter);
1596
1597 if (self->probed_srccaps)
1598 result = gst_caps_ref(self->probed_srccaps);
1599 else
1600 result = gst_pad_get_pad_template_caps(pad);
1601
1602 if (filter)
1603 {
1604 GstCaps *tmp = result;
1605 result =
1606 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1607 gst_caps_unref(tmp);
1608 }
1609
1610 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1611
1612 gst_query_set_caps_result(query, result);
1613 gst_caps_unref(result);
1614 break;
1615 }
1616
1617 default:
1618 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1619 break;
1620 }
1621
1622 return ret;
1623}
1624
1625static GstCaps *
1626gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1627{
1628 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1629 GstCaps *result;
1630
1631 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1632 filter);
1633
1634 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1635
1636 return result;
1637}
1638
1639static gboolean
1640gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1641{
1642 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1643 gboolean ret;
1644 GstEventType type = GST_EVENT_TYPE(event);
bo.xiao8e3054d2024-07-31 17:13:57 +08001645 GST_DEBUG_OBJECT (self, "received event %p %" GST_PTR_FORMAT, event, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001646
1647 switch (type)
1648 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001649 case GST_EVENT_STREAM_START:
1650 {
1651 GstStructure *s;
1652 GstEvent *event;
xuesong.jiang406ee302023-06-28 03:45:22 +00001653 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1654 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1655 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1656 gst_pad_push_event (decoder->sinkpad, event);
1657 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1658 break;
1659 }
zengliang.li51f54b62023-10-10 12:14:49 +00001660 case GST_EVENT_CAPS:
1661 {
1662 GstCaps *caps;
1663 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001664 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001665
1666 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001667
zengliang.li51f54b62023-10-10 12:14:49 +00001668 structure= gst_caps_get_structure(caps, 0);
1669 if ( gst_structure_has_field(structure, "parsed") )
1670 {
1671 gboolean parsed = TRUE;
1672 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1673 {
1674 self->v4l2output->stream_mode = !parsed;
1675 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1676 }
1677 }
sheng.liu641aa422023-12-26 07:05:59 +00001678
xuesong.jiang26749f32024-07-24 09:40:47 +08001679 if ( gst_structure_has_field(structure, "secure") )
1680 {
1681 gboolean is_secure = FALSE;
1682 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1683 {
1684 self->v4l2output->secure_es = is_secure;
1685 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1686 }
1687 }
xuesong.jiang578add22024-07-25 15:11:30 +08001688 else
1689 {
1690 GstCapsFeatures *const features = gst_caps_get_features(caps, 0);
1691 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1692 {
1693 self->v4l2output->secure_es = TRUE;
1694 GST_DEBUG("If there is no secure field in caps, consider dma es is secure");
1695 }
1696 }
xuesong.jiang26749f32024-07-24 09:40:47 +08001697
sheng.liudb26f7d2024-01-22 11:24:23 +00001698 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1699 {
1700 if ( denom == 0 ) denom= 1;
1701
1702 if (self->v4l2capture->fps)
1703 {
1704 g_value_unset(self->v4l2capture->fps);
1705 g_free(self->v4l2capture->fps);
1706 }
1707
1708 self->v4l2capture->fps = g_new0(GValue, 1);
1709 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1710 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1711
1712 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1713 }
1714
sheng.liu641aa422023-12-26 07:05:59 +00001715 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1716 ( !self->v4l2capture->have_set_par ) )
1717 {
1718 if ( (num <= 0) || (denom <= 0) )
1719 {
1720 num= denom= 1;
1721 }
1722
1723 if ( self->v4l2capture->par )
1724 {
1725 g_value_unset(self->v4l2capture->par);
1726 g_free(self->v4l2capture->par);
1727 }
1728
1729 self->v4l2capture->par = g_new0(GValue, 1);
1730 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1731 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1732 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1733 }
zengliang.li51f54b62023-10-10 12:14:49 +00001734 break;
1735 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001736 case GST_EVENT_FLUSH_START:
1737 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001738
1739 g_mutex_lock (&self->res_chg_lock);
1740 while (self->is_res_chg)
1741 {
1742 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1743 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1744 }
1745 g_mutex_unlock (&self->res_chg_lock);
1746
zengliang.li92ff6822023-06-06 07:12:52 +00001747 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001748 gst_aml_v4l2_object_unlock(self->v4l2output);
1749 gst_aml_v4l2_object_unlock(self->v4l2capture);
1750 break;
1751 default:
1752 break;
1753 }
1754
1755 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1756
1757 switch (type)
1758 {
1759 case GST_EVENT_FLUSH_START:
1760 /* The processing thread should stop now, wait for it */
1761 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001762 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001763 GST_DEBUG_OBJECT(self, "flush start done");
1764 break;
1765 default:
1766 break;
1767 }
1768
1769 return ret;
1770}
1771
1772static GstStateChangeReturn
1773gst_aml_v4l2_video_dec_change_state(GstElement *element,
1774 GstStateChange transition)
1775{
1776 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1777 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1778
bo.xiao8e3054d2024-07-31 17:13:57 +08001779 GST_DEBUG_OBJECT(element, "change state from %s to %s",
1780 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1781 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1782
xuesong.jiangae1548e2022-05-06 16:38:46 +08001783 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1784 {
1785 g_atomic_int_set(&self->active, FALSE);
1786 gst_aml_v4l2_object_unlock(self->v4l2output);
1787 gst_aml_v4l2_object_unlock(self->v4l2capture);
1788 gst_pad_stop_task(decoder->srcpad);
1789 }
1790
1791 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1792}
1793
1794static void
1795gst_aml_v4l2_video_dec_dispose(GObject *object)
1796{
1797 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1798
1799 gst_caps_replace(&self->probed_sinkcaps, NULL);
1800 gst_caps_replace(&self->probed_srccaps, NULL);
1801
1802 G_OBJECT_CLASS(parent_class)->dispose(object);
1803}
1804
1805static void
1806gst_aml_v4l2_video_dec_finalize(GObject *object)
1807{
1808 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1809
1810 gst_aml_v4l2_object_destroy(self->v4l2capture);
1811 gst_aml_v4l2_object_destroy(self->v4l2output);
1812
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001813 g_mutex_clear(&self->res_chg_lock);
1814 g_cond_clear(&self->res_chg_cond);
1815
xuesong.jiang61ea8012022-05-12 15:38:17 +08001816#if GST_IMPORT_LGE_PROP
1817 if (self->lge_ctxt)
1818 {
1819 if (self->lge_ctxt->app_type)
1820 g_free(self->lge_ctxt->app_type);
1821 if (self->lge_ctxt->res_info.coretype)
1822 g_free(self->lge_ctxt->res_info.coretype);
1823 free(self->lge_ctxt);
1824 }
1825
1826#endif
1827
xuesong.jiangae1548e2022-05-06 16:38:46 +08001828 G_OBJECT_CLASS(parent_class)->finalize(object);
1829}
1830
1831static void
1832gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1833{
1834 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001835 self->last_out_pts = GST_CLOCK_TIME_NONE;
bo.xiao8e3054d2024-07-31 17:13:57 +08001836 self->frame_duration = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001837 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001838 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001839 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001840 g_mutex_init(&self->res_chg_lock);
1841 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001842#if GST_IMPORT_LGE_PROP
1843 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1844 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1845#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001846}
1847
1848static void
1849gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1850{
1851 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1852 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1853 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1854
1855 gst_video_decoder_set_packetized(decoder, TRUE);
1856
1857 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1858 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1859 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1860 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1861 self->v4l2output->no_initial_format = TRUE;
1862 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001863 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001864
1865 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1866 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1867 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1868 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1869 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001870 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001871}
1872
1873static void
1874gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1875{
1876 GstElementClass *element_class;
1877 GObjectClass *gobject_class;
1878 GstVideoDecoderClass *video_decoder_class;
1879
1880 parent_class = g_type_class_peek_parent(klass);
1881
1882 element_class = (GstElementClass *)klass;
1883 gobject_class = (GObjectClass *)klass;
1884 video_decoder_class = (GstVideoDecoderClass *)klass;
1885
1886 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1887 "AML V4L2 Video Decoder");
1888
1889 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1890 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1891 gobject_class->set_property =
1892 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1893 gobject_class->get_property =
1894 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1895
1896 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1897 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1898 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1899 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1900 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1901 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1902 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1903 video_decoder_class->set_format =
1904 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1905 video_decoder_class->negotiate =
1906 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1907 video_decoder_class->decide_allocation =
1908 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1909 /* FIXME propose_allocation or not ? */
1910 video_decoder_class->handle_frame =
1911 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1912 video_decoder_class->getcaps =
1913 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1914 video_decoder_class->src_query =
1915 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1916 video_decoder_class->sink_event =
1917 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1918
1919 element_class->change_state =
1920 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1921
xuesong.jiang406ee302023-06-28 03:45:22 +00001922 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1923 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1924 G_SIGNAL_RUN_LAST,
1925 0, /* class offset */
1926 NULL, /* accumulator */
1927 NULL, /* accu data */
1928 g_cclosure_marshal_generic,
1929 G_TYPE_NONE,
1930 1,
1931 G_TYPE_UINT64);
1932
xuesong.jiangae1548e2022-05-06 16:38:46 +08001933 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001934#if GST_IMPORT_LGE_PROP
1935 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1936#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001937}
1938
1939static void
1940gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1941{
1942 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1943 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1944 GstAmlV4l2VideoDecCData *cdata = data;
1945
1946 klass->default_device = cdata->device;
1947
1948 /* Note: gst_pad_template_new() take the floating ref from the caps */
1949 gst_element_class_add_pad_template(element_class,
1950 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1951 cdata->sink_caps));
1952 gst_element_class_add_pad_template(element_class,
1953 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1954 cdata->src_caps));
1955
1956 gst_element_class_set_metadata(element_class, cdata->longname,
1957 "Codec/Decoder/Video/Hardware", cdata->description,
1958 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1959
1960 gst_caps_unref(cdata->sink_caps);
1961 gst_caps_unref(cdata->src_caps);
1962 g_free(cdata);
1963}
1964
1965/* Probing functions */
1966gboolean
1967gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1968{
1969 gboolean ret = FALSE;
1970
1971 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()))
1972 ret = TRUE;
1973
1974 return ret;
1975}
1976
1977static gchar *
1978gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1979 const gchar *basename)
1980{
1981 gchar *codec_name = NULL;
1982 gchar *type_name = NULL;
1983 gboolean got_value = FALSE;
1984
1985#define SET_META(codec) \
1986 G_STMT_START \
1987 { \
1988 cdata->longname = "AML V4L2 " codec " Decoder"; \
1989 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1990 codec_name = g_ascii_strdown(codec, -1); \
1991 } \
1992 G_STMT_END
1993
bo.xiaoce116a42024-07-16 16:21:53 +08001994 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001995 {
bo.xiaoce116a42024-07-16 16:21:53 +08001996 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001997 }
1998 else if (gst_structure_has_name(s, "video/mpeg"))
1999 {
2000 gint mpegversion = 0;
2001 gint *list = NULL;
2002 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
2003 if (FALSE == got_value)
2004 {
2005 got_value = gst_structure_get_list(s, "mpegversion", &list);
2006 if (TRUE == got_value && (1 == *list || 2 == *list))
2007 {
2008 SET_META("MPEG2");
2009 }
2010 else
2011 {
2012 SET_META("MPEG4");
2013 }
2014 }
2015 else
2016 {
2017 SET_META("MPEG4");
2018 }
2019 }
2020 else if (gst_structure_has_name(s, "video/x-h263"))
2021 {
2022 SET_META("H263");
2023 }
2024 else if (gst_structure_has_name(s, "video/x-fwht"))
2025 {
2026 SET_META("FWHT");
2027 }
2028 else if (gst_structure_has_name(s, "video/x-h264"))
2029 {
2030 SET_META("H264");
2031 }
2032 else if (gst_structure_has_name(s, "video/x-h265"))
2033 {
2034 SET_META("H265");
2035 }
2036 else if (gst_structure_has_name(s, "video/x-wmv"))
2037 {
2038 SET_META("VC1");
2039 }
2040 else if (gst_structure_has_name(s, "video/x-vp8"))
2041 {
2042 SET_META("VP8");
2043 }
2044 else if (gst_structure_has_name(s, "video/x-vp9"))
2045 {
2046 SET_META("VP9");
2047 }
2048 else if (gst_structure_has_name(s, "video/x-av1"))
2049 {
2050 SET_META("AV1");
2051 }
zengliang.li51f54b62023-10-10 12:14:49 +00002052 else if (gst_structure_has_name(s, "video/x-avs"))
2053 {
2054 SET_META("AVS");
2055 }
2056 else if (gst_structure_has_name(s, "video/x-avs2"))
2057 {
2058 SET_META("AVS2");
2059 }
2060 else if (gst_structure_has_name(s, "video/x-avs3"))
2061 {
2062 SET_META("AVS3");
2063 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002064 else if (gst_structure_has_name(s, "video/x-bayer"))
2065 {
2066 SET_META("BAYER");
2067 }
2068 else if (gst_structure_has_name(s, "video/x-sonix"))
2069 {
2070 SET_META("SONIX");
2071 }
2072 else if (gst_structure_has_name(s, "video/x-pwc1"))
2073 {
2074 SET_META("PWC1");
2075 }
2076 else if (gst_structure_has_name(s, "video/x-pwc2"))
2077 {
2078 SET_META("PWC2");
2079 }
2080 else
2081 {
2082 /* This code should be kept on sync with the exposed CODEC type of format
2083 * from gstamlv4l2object.c. This warning will only occure in case we forget
2084 * to also add a format here. */
2085 gchar *s_str = gst_structure_to_string(s);
2086 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2087 "bug, please report at https://bugs.gnome.org",
2088 s_str);
2089 g_free(s_str);
2090 }
2091
2092 if (codec_name)
2093 {
2094 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2095 if (g_type_from_name(type_name) != 0)
2096 {
2097 g_free(type_name);
2098 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2099 }
2100
2101 g_free(codec_name);
2102 }
2103
2104 return type_name;
2105#undef SET_META
2106}
2107
2108void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2109 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2110{
2111 gint i;
2112
2113 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2114 {
2115 GstAmlV4l2VideoDecCData *cdata;
2116 GstStructure *s;
2117 GTypeQuery type_query;
2118 GTypeInfo type_info = {
2119 0,
2120 };
2121 GType type, subtype;
2122 gchar *type_name;
2123
2124 s = gst_caps_get_structure(sink_caps, i);
2125
2126 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2127 cdata->device = g_strdup(device_path);
2128 cdata->sink_caps = gst_caps_new_empty();
2129 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2130 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2131 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2132 cdata->src_caps = gst_caps_copy(src_caps);
2133 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2134 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2135 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2136
2137 /* Skip over if we hit an unmapped type */
2138 if (!type_name)
2139 {
2140 g_free(cdata);
2141 continue;
2142 }
2143
2144 type = gst_aml_v4l2_video_dec_get_type();
2145 g_type_query(type, &type_query);
2146 memset(&type_info, 0, sizeof(type_info));
2147 type_info.class_size = type_query.class_size;
2148 type_info.instance_size = type_query.instance_size;
2149 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2150 type_info.class_data = cdata;
2151 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2152
2153 subtype = g_type_register_static(type, type_name, &type_info, 0);
2154 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2155 subtype))
2156 GST_WARNING("Failed to register plugin '%s'", type_name);
2157
2158 g_free(type_name);
2159 }
2160}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002161
2162#if GST_IMPORT_LGE_PROP
2163static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2164{
2165 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2166 g_param_spec_object("resource-info", "resource-info",
2167 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2168 GST_TYPE_STRUCTURE,
2169 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2170
2171 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2172 g_param_spec_uint64("decoded-size", "decoded-size",
2173 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2174 0, G_MAXUINT64,
2175 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2176
2177 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2178 g_param_spec_uint64("undecoded-size", "undecoded-size",
2179 "video decoder element's total undecoded data update unit is byte.",
2180 0, G_MAXUINT64,
2181 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2182
2183 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2184 g_param_spec_string("app-type", "app-type",
2185 "set application type.",
2186 "default_app",
2187 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2188
2189 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2190 g_param_spec_boolean("clip-mode", "clip-mode",
2191 "When seeking, Content is moving faster for a while to skip frames.",
2192 FALSE,
2193 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2194}
2195#endif