blob: bd85206d8f32faf11a3c5ce07d0709fc2ced8604 [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
hanghang.luo36df2852022-08-24 15:02:27 +080060#ifndef ABSDIFF
61#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
62#endif
63
xuesong.jiang61ea8012022-05-12 15:38:17 +080064#if GST_IMPORT_LGE_PROP
65typedef struct _GstAmlResourceInfo
66{
67 gchar *coretype;
68 gint videoport;
69 gint audioport;
70 gint maxwidth;
71 gint maxheight;
72 gint mixerport;
73} GstAmlResourceInfo;
74
75struct _GstAmlV4l2VideoDecLgeCtxt
76{
77 GstAmlResourceInfo res_info;
78 guint64 dec_size;
79 guint64 undec_size;
80 gchar *app_type;
81 gboolean clip_mode;
82};
83#endif
84
xuesong.jiangae1548e2022-05-06 16:38:46 +080085typedef struct
86{
87 gchar *device;
88 GstCaps *sink_caps;
89 GstCaps *src_caps;
90 const gchar *longname;
91 const gchar *description;
92} GstAmlV4l2VideoDecCData;
93
94enum
95{
96 PROP_0,
xuesong.jiang61ea8012022-05-12 15:38:17 +080097 V4L2_STD_OBJECT_PROPS,
98#if GST_IMPORT_LGE_PROP
99 LGE_RESOURCE_INFO,
100 LGE_DECODE_SIZE,
101 LGE_UNDECODE_SIZE,
102 LGE_APP_TYPE,
103 LGE_CLIP_MODE
104#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800105};
106
xuesong.jiang406ee302023-06-28 03:45:22 +0000107enum
108{
109 SIGNAL_DECODED_PTS,
110 MAX_SIGNAL
111};
112
113static guint g_signals[MAX_SIGNAL]= {0};
114
xuesong.jiangae1548e2022-05-06 16:38:46 +0800115#define gst_aml_v4l2_video_dec_parent_class parent_class
116G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec,
117 GST_TYPE_VIDEO_DECODER);
118
119static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder);
xuesong.jiang61ea8012022-05-12 15:38:17 +0800120#if GST_IMPORT_LGE_PROP
121static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class);
122#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800123
124static void
125gst_aml_v4l2_video_dec_set_property(GObject *object,
126 guint prop_id, const GValue *value, GParamSpec *pspec)
127{
128 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
129
130 switch (prop_id)
131 {
132 case PROP_CAPTURE_IO_MODE:
133 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
134 prop_id, value, pspec))
135 {
136 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
137 }
138 break;
139 case PROP_DUMP_FRAME_LOCATION:
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:
207 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
208 prop_id, value, pspec))
209 {
210 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
211 }
212 break;
213
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 }
452 gst_video_codec_state_unref(self->input_state);
453 self->input_state = NULL;
454
455 gst_aml_v4l2_video_dec_finish(decoder);
456 gst_aml_v4l2_object_stop(self->v4l2output);
457
458 /* The renegotiation flow don't blend with the base class flow. To properly
459 * stop the capture pool, if the buffers can't be orphaned, we need to
460 * reclaim our buffers, which will happend through the allocation query.
461 * The allocation query is triggered by gst_video_decoder_negotiate() which
462 * requires the output caps to be set, but we can't know this information
463 * as we rely on the decoder, which requires the capture queue to be
464 * stopped.
465 *
466 * To workaround this issue, we simply run an allocation query with the
467 * old negotiated caps in order to drain/reclaim our buffers. That breaks
468 * the complexity and should not have much impact in performance since the
469 * following allocation query will happen on a drained pipeline and won't
470 * block. */
471 if (self->v4l2capture->pool &&
472 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
473 {
474 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
475 if (caps)
476 {
477 GstQuery *query = gst_query_new_allocation(caps, FALSE);
478 gst_pad_peer_query(decoder->srcpad, query);
479 gst_query_unref(query);
480 gst_caps_unref(caps);
481 }
482 }
483
484 gst_aml_v4l2_object_stop(self->v4l2capture);
485 self->output_flow = GST_FLOW_OK;
486 }
487
488 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
xuesong.jiangae1548e2022-05-06 16:38:46 +0800500 ret = gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error);
501
502 gst_caps_replace(&self->probed_srccaps, NULL);
503 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
504 gst_aml_v4l2_object_get_raw_caps());
505
506 if (gst_caps_is_empty(self->probed_srccaps))
507 goto no_raw_format;
508
509 caps = gst_caps_copy(self->probed_srccaps);
510 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
511 gst_caps_append(self->probed_srccaps, caps);
512 if (ret)
513 self->input_state = gst_video_codec_state_ref(state);
514 else
515 gst_aml_v4l2_error(self, &error);
516
517done:
518 return ret;
519
520no_raw_format:
521 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
522 (_("Decoder on device %s has no supported output format"),
523 self->v4l2output->videodev),
524 (NULL));
525 return GST_FLOW_ERROR;
526}
527
528static gboolean
529gst_aml_v4l2_video_dec_flush(GstVideoDecoder *decoder)
530{
531 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
532
533 GST_DEBUG_OBJECT(self, "Flushed");
534
535 /* Ensure the processing thread has stopped for the reverse playback
536 * discount case */
537 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
538 {
539 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
540
541 gst_aml_v4l2_object_unlock(self->v4l2output);
542 gst_aml_v4l2_object_unlock(self->v4l2capture);
543 gst_pad_stop_task(decoder->srcpad);
544 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
545 }
546
547 self->output_flow = GST_FLOW_OK;
548
549 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
550 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
551
552 if (self->v4l2output->pool)
553 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
554
555 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
556 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
557 * pool. */
558 if (self->v4l2capture->pool)
559 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
560
561 return TRUE;
562}
563
564static gboolean
565gst_aml_v4l2_video_dec_negotiate(GstVideoDecoder *decoder)
566{
567 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
568
xuesong.jiang681d3602022-06-24 21:23:35 +0800569 if (TRUE == self->v4l2output->is_svp)
570 {
571 GstStructure *s;
572 GstEvent *event;
573
574 s = gst_structure_new_empty ("IS_SVP");
575 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
576 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
577 gst_pad_push_event (decoder->srcpad, event);
578 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
579 }
580
xuesong.jiangae1548e2022-05-06 16:38:46 +0800581 /* We don't allow renegotiation without carefull disabling the pool */
582 if (self->v4l2capture->pool &&
583 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
584 return TRUE;
585
586 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
587}
588
589static gboolean
590gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
591{
592 struct v4l2_decoder_cmd dcmd = {
593 0,
594 };
595
596 GST_DEBUG_OBJECT(v4l2object->element,
597 "sending v4l2 decoder command %u with flags %u", cmd, flags);
598
599 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
600 return FALSE;
601
602 dcmd.cmd = cmd;
603 dcmd.flags = flags;
604 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
605 goto dcmd_failed;
606
607 return TRUE;
608
609dcmd_failed:
610 if (errno == ENOTTY)
611 {
612 GST_INFO_OBJECT(v4l2object->element,
613 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
614 cmd, flags, v4l2object->videodev, g_strerror(errno));
615 }
616 else
617 {
618 GST_ERROR_OBJECT(v4l2object->element,
619 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
620 cmd, flags, v4l2object->videodev, g_strerror(errno));
621 }
622 return FALSE;
623}
624
625static GstFlowReturn
626gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
627{
628 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
629 GstFlowReturn ret = GST_FLOW_OK;
630 GstBuffer *buffer;
631
632 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
633 goto done;
634
635 GST_DEBUG_OBJECT(self, "Finishing decoding");
636
637 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
638
639 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
640 {
641 GstTask *task = decoder->srcpad->task;
642
643 /* If the decoder stop command succeeded, just wait until processing is
644 * finished */
645 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
646 GST_OBJECT_LOCK(task);
647 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
648 GST_TASK_WAIT(task);
649 GST_OBJECT_UNLOCK(task);
650 ret = GST_FLOW_FLUSHING;
651 }
652 else
653 {
654 /* otherwise keep queuing empty buffers until the processing thread has
655 * stopped, _pool_process() will return FLUSHING when that happened */
656 while (ret == GST_FLOW_OK)
657 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800658 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800659 buffer = gst_buffer_new();
660 ret =
661 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
662 gst_buffer_unref(buffer);
663 }
664 }
665
666 /* and ensure the processing thread has stopped in case another error
667 * occured. */
668 gst_aml_v4l2_object_unlock(self->v4l2capture);
669 gst_pad_stop_task(decoder->srcpad);
670 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
671
672 if (ret == GST_FLOW_FLUSHING)
673 ret = self->output_flow;
674
675 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
676
677 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
678
679done:
680 return ret;
681}
682
683static GstFlowReturn
684gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
685{
686 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
687
688 GST_DEBUG_OBJECT(self, "Draining...");
689 gst_aml_v4l2_video_dec_finish(decoder);
690 gst_aml_v4l2_video_dec_flush(decoder);
691
692 return GST_FLOW_OK;
693}
694
695static GstVideoCodecFrame *
696gst_aml_v4l2_video_dec_get_oldest_frame(GstVideoDecoder *decoder)
697{
698 GstVideoCodecFrame *frame = NULL;
699 GList *frames, *l;
700 gint count = 0;
701
702 frames = gst_video_decoder_get_frames(decoder);
703
704 for (l = frames; l != NULL; l = l->next)
705 {
706 GstVideoCodecFrame *f = l->data;
707
708 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
709 frame = f;
710
711 count++;
712 }
713
714 if (frame)
715 {
716 GST_LOG_OBJECT(decoder,
717 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
718 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
719 gst_video_codec_frame_ref(frame);
720 }
721
722 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
723
724 return frame;
725}
726
fei.dengbee20862022-06-14 14:59:48 +0800727static GstVideoCodecFrame *
xuesong.jiange24aef92023-06-16 06:39:10 +0000728gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800729{
730 GstVideoCodecFrame *frame = NULL;
731 GList *frames, *l;
732 gint count = 0;
733
xuesong.jiange24aef92023-06-16 06:39:10 +0000734 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
735
fei.dengbee20862022-06-14 14:59:48 +0800736 frames = gst_video_decoder_get_frames(decoder);
737
738 for (l = frames; l != NULL; l = l->next)
739 {
740 GstVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000741
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800742 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800743 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800744 }
fei.dengbee20862022-06-14 14:59:48 +0800745 count++;
746 }
747
zengliang.liddee2da2023-07-14 07:27:05 +0000748 if (!frame)
749 {
750 for (l = frames; l != NULL; l = l->next)
751 {
752 GstVideoCodecFrame *f = l->data;
753 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
754 {
755 frame = f;
756 }
757 GST_DEBUG("The pts of the expected output frame is invalid");
758 }
759 }
760
fei.dengbee20862022-06-14 14:59:48 +0800761 if (frame)
762 {
763 GST_LOG_OBJECT(decoder,
764 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
765 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
766 gst_video_codec_frame_ref(frame);
767 }
768
769 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
770
xuesong.jiange24aef92023-06-16 06:39:10 +0000771 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
fei.dengbee20862022-06-14 14:59:48 +0800772 return frame;
773}
774
xuesong.jiange24aef92023-06-16 06:39:10 +0000775static GstVideoCodecFrame *
776gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstVideoDecoder *decoder, GstClockTime pts)
777{
778 GstVideoCodecFrame *frame = NULL;
779 GList *frames, *l;
780 gint count = 0;
781
782 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
783
784 frames = gst_video_decoder_get_frames(decoder);
785 guint frames_len = 0;
786 frames_len = g_list_length(frames);
787 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
788
789 frame = frames->data;
790
791 for (l = frames; l != NULL; l = l->next)
792 {
793 GstVideoCodecFrame *f = l->data;
794
795 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
796 {
797 /* found the right frame */
798 frame = f;
799 break;
800 }
801 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
802 {
803 GST_LOG_OBJECT(decoder,
804 "stream mode drop frame %d %" GST_TIME_FORMAT,
805 frame->system_frame_number, GST_TIME_ARGS(frame->pts));
806
807 gst_video_codec_frame_ref(f);
808 // gst_video_decoder_drop_frame(decoder, f);
809 gst_video_decoder_release_frame(decoder, f);
810 }
811 else
812 {
813 GST_LOG_OBJECT (decoder, "dbg");
814 }
815 }
816
817 if (frame)
818 {
819 guint l_len = 0;
820 l = gst_video_decoder_get_frames(decoder);
821 l_len = g_list_length(l);
822 g_list_free_full(l, (GDestroyNotify)gst_video_codec_frame_unref);
823
824 GST_LOG_OBJECT(decoder,
825 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
826 frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
827 gst_video_codec_frame_ref(frame);
828 }
829
830 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
831
832 GST_LOG_OBJECT (decoder, "trace out ret:%p", frame);
833 return frame;
834}
835
836static GstVideoCodecFrame *
837gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
838{
839 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
840 if (self->v4l2output->stream_mode)
841 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
842 else
843 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
844}
845
xuesong.jiangae1548e2022-05-06 16:38:46 +0800846static gboolean
847gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
848 GstStructure *structure, gpointer user_data)
849{
850 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
851 GstVideoAlignment *align = &self->v4l2capture->align;
852 GstVideoInfo *info = &self->v4l2capture->info;
853 int width, height;
854
855 if (!gst_structure_get_int(structure, "width", &width))
856 return TRUE;
857
858 if (!gst_structure_get_int(structure, "height", &height))
859 return TRUE;
860
861 if (align->padding_left != 0 || align->padding_top != 0 ||
862 height != info->height + align->padding_bottom)
863 return TRUE;
864
865 if (height == info->height + align->padding_bottom)
866 {
867 /* Some drivers may round up width to the padded with */
868 if (width == info->width + align->padding_right)
869 gst_structure_set(structure,
870 "width", G_TYPE_INT, width - align->padding_right,
871 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
872 /* Some drivers may keep visible width and only round up bytesperline */
873 else if (width == info->width)
874 gst_structure_set(structure,
875 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
876 }
877
878 return TRUE;
879}
880
881static void
sheng.liubcf036c2022-06-21 15:55:42 +0800882gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800883{
884 struct v4l2_event evt;
885 gint ret;
886
887 memset (&evt, 0x00, sizeof (struct v4l2_event));
888 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
889 if (ret < 0)
890 {
891 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
892 return;
893 }
894
895 switch (evt.type)
896 {
897 case V4L2_EVENT_SOURCE_CHANGE:
898 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
899 break;
900 case V4L2_EVENT_EOS:
901 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
902 break;
903 default:
904 break;
905 }
906
907 return;
908}
909
910static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000911gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
912{
913 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
914 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000915 struct v4l2_selection sel;
916 struct v4l2_rect *r = NULL;
917 GstStructure *s;
918 gint width = 0;
919 gint height = 0;
hanghang.luo2eec4892023-07-18 06:44:42 +0000920 output_state = gst_video_decoder_set_output_state(decoder,
921 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000922 memset(&sel, 0, sizeof(struct v4l2_selection));
923 sel.type = self->v4l2capture->type;
924 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
925 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
926 {
927 r = &sel.r;
928 width = (r->width/2)*2;
929 height = (r->height/2)*2;
930 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
931 }
932 else
933 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000934 if (output_state)
935 {
936 output_state->info.interlace_mode = info.interlace_mode;
937 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000938 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000939 s = gst_caps_get_structure(output_state->caps, 0);
940 if (s)
941 {
942 gst_structure_set(s,"coded_width",G_TYPE_INT,info.width,NULL);
943 gst_structure_set(s,"coded_height",G_TYPE_INT,info.height,NULL);
944 gst_structure_set(s,"width",G_TYPE_INT,width,NULL);
945 gst_structure_set(s,"height",G_TYPE_INT,height,NULL);
946 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
947 gst_video_codec_state_unref(output_state);
948 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000949 }
950}
951
952static void
xuesong.jiangae1548e2022-05-06 16:38:46 +0800953gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
954{
955 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
956 GstAmlV4l2BufferPool *v4l2_pool;
957 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
958 GstBufferPool *pool;
959 GstVideoCodecFrame *frame;
960 GstBuffer *buffer = NULL;
961 GstFlowReturn ret;
962
963 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
964 {
965 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +0000966 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800967 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800968 GST_DEBUG_OBJECT(self, "waitting source change event");
969 /* Wait until received SOURCE_CHANGE event to get right video format */
970 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
971 {
972 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
973 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
974 {
fei.deng2a06e042023-10-10 03:09:45 +0000975 //let flush start event blocked until capture buffer pool actived
976 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800977 GST_DEBUG_OBJECT(self, "Received source change event");
978 break;
979 }
980 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
981 {
982 GST_DEBUG_OBJECT(self, "Received eos event");
983 goto beach;
984 }
985 else if (ret != GST_FLOW_OK)
986 {
987 GST_ERROR_OBJECT(self, "dqevent error");
988 goto beach;
989 }
990 }
991 self->v4l2capture->need_wait_event = FALSE;
992
sheng.liu0c77f6c2022-06-17 21:33:20 +0800993 if (TRUE == self->v4l2output->is_svp)
994 {
995 GstPad *peer;
996 GstStructure *s;
997 GstEvent *event;
998
999 peer = gst_pad_get_peer (decoder->srcpad);
1000 if (peer)
1001 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001002 s = gst_structure_new_empty ("IS_SVP");
1003 if (s)
1004 {
1005 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1006 gst_pad_send_event (peer, event);
1007 GST_DEBUG_OBJECT(self, "Send SVP Event");
1008 }
1009 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001010 }
1011 }
1012
sheng.liub56bbc52022-06-21 11:02:33 +08001013 if (self->v4l2capture->need_drop_event)
1014 {
1015 // drop V4L2_EVENT_SOURCE_CHANGE
1016 gst_v4l2_drop_event(self->v4l2capture);
1017 self->v4l2capture->need_drop_event = FALSE;
1018 }
1019
xuesong.jiangae1548e2022-05-06 16:38:46 +08001020 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1021 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001022
xuesong.jiangae1548e2022-05-06 16:38:46 +08001023 /* Create caps from the acquired format, remove the format field */
1024 acquired_caps = gst_video_info_to_caps(&info);
1025 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1026 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001027 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1028
1029 /* Probe currently available pixel formats */
1030 available_caps = gst_caps_copy(self->probed_srccaps);
1031 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1032
1033 /* Replace coded size with visible size, we want to negotiate visible size
1034 * with downstream, not coded size. */
1035 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1036
1037 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001038 caps = gst_caps_copy(filter);
1039 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1040 gst_caps_append(filter, caps);
1041
1042 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1043 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001044 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001045 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1046 gst_caps_unref(filter);
1047
1048 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1049 if (gst_caps_is_empty(caps))
1050 {
1051 gst_caps_unref(caps);
1052 goto not_negotiated;
1053 }
1054
1055 /* Fixate pixel format */
1056 caps = gst_caps_fixate(caps);
1057
1058 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1059
1060 /* Try to set negotiated format, on success replace acquired format */
1061 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1062 gst_video_info_from_caps(&info, caps);
1063 else
1064 gst_aml_v4l2_clear_error(&error);
1065 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001066 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001067 if (!gst_video_decoder_negotiate(decoder))
1068 {
1069 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1070 goto flushing;
1071 else
1072 goto not_negotiated;
1073 }
1074
1075 /* Ensure our internal pool is activated */
1076 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1077 TRUE))
1078 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001079
1080 g_mutex_lock(&self->res_chg_lock);
1081 GST_LOG_OBJECT(decoder, "signal resolution changed");
1082 self->is_res_chg = FALSE;
1083 g_cond_signal(&self->res_chg_cond);
1084 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001085 }
1086
1087 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1088
1089 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1090
1091 self->output_flow = GST_FLOW_OK;
1092 do
1093 {
1094 /* We cannot use the base class allotate helper since it taking the internal
1095 * stream lock. we know that the acquire may need to poll until more frames
1096 * comes in and holding this lock would prevent that.
1097 */
1098 pool = gst_video_decoder_get_buffer_pool(decoder);
1099
1100 /* Pool may be NULL if we started going to READY state */
1101 if (pool == NULL)
1102 {
fei.dengbee20862022-06-14 14:59:48 +08001103 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001104 ret = GST_FLOW_FLUSHING;
1105 goto beach;
1106 }
1107
1108 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
fei.dengccc89632022-07-15 19:10:17 +08001109 //calculate a new pts for interlace stream
hanghang.luo8ec04e92023-10-09 08:14:24 +00001110 if (ret == GST_FLOW_OK && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
fei.dengccc89632022-07-15 19:10:17 +08001111 {
1112 //if buffer pts is valid, reduce 1/2 duration
1113 if (GST_BUFFER_DURATION_IS_VALID(buffer))
1114 {
1115 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
1116 }
1117 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1118 //reset pts
fei.denga6ae3282022-07-15 19:50:30 +08001119 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL || self->last_out_pts == GST_BUFFER_TIMESTAMP (buffer))
fei.dengccc89632022-07-15 19:10:17 +08001120 {
1121 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
1122 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1123 }
1124 }
1125
xuesong.jiangae1548e2022-05-06 16:38:46 +08001126 g_object_unref(pool);
1127
fei.deng9a5cd6e2023-06-30 12:09:18 +00001128 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 +00001129 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1130 self->v4l2capture->need_drop_event = TRUE;
1131 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1132 if (self->is_res_chg) {
1133 //we must release last buffer
1134 gst_buffer_unref(buffer);
1135 //if resolution changed event received,we should set need_drop_event to false
1136 self->v4l2capture->need_drop_event = FALSE;
1137 gst_aml_v4l2_object_stop(self->v4l2capture);
1138 //unblock flush start event
1139 g_mutex_lock(&self->res_chg_lock);
1140 self->is_res_chg = FALSE;
1141 g_cond_signal(&self->res_chg_cond);
1142 g_mutex_unlock(&self->res_chg_lock);
1143 return;
1144 } else {
1145 goto beach;
1146 }
sheng.liub56bbc52022-06-21 11:02:33 +08001147 }
1148
sheng.liu8d18ed22022-05-26 17:28:15 +08001149 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1150 {
1151 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001152
1153 g_mutex_lock (&self->res_chg_lock);
1154 self->is_res_chg = TRUE;
1155 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001156 return;
1157 }
1158
fei.dengbee20862022-06-14 14:59:48 +08001159 if (ret != GST_FLOW_OK) {
1160 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001161 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001162 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001163
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001164 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001165 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001166
1167 GST_DEBUG_OBJECT(decoder, "send pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1168 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1169
xuesong.jiangae1548e2022-05-06 16:38:46 +08001170 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1171 {
1172 gst_aml_v4l2_object_stop(self->v4l2capture);
1173 return;
1174 }
1175
1176 } while (ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER);
1177
1178 if (ret != GST_FLOW_OK)
1179 goto beach;
1180
fei.dengbee20862022-06-14 14:59:48 +08001181 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001182 if (frame)
1183 {
zengliang.li32cb11e2022-11-24 12:10:26 +08001184 if (!GST_CLOCK_TIME_IS_VALID(frame->pts))
1185 {
zengliang.li92ff6822023-06-06 07:12:52 +00001186 if (!GST_CLOCK_TIME_IS_VALID(self->last_out_pts))
1187 {
1188 if (GST_CLOCK_TIME_IS_VALID(frame->dts))
1189 {
1190 GST_BUFFER_TIMESTAMP(buffer) = frame->dts;
1191 }
1192 else
1193 {
1194 GST_WARNING_OBJECT (decoder,"sorry,we have no baseline to calculate pts");
1195 goto beach;
1196 }
1197 }
1198 else
1199 {
1200 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d);
1201 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1202 }
zengliang.li32cb11e2022-11-24 12:10:26 +08001203 }
fei.dengccc89632022-07-15 19:10:17 +08001204 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001205 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001206 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1207 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001208 buffer = NULL;
1209 ret = gst_video_decoder_finish_frame(decoder, frame);
1210
1211 if (ret != GST_FLOW_OK)
1212 goto beach;
1213 }
1214 else
1215 {
1216 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
1217 gst_buffer_unref(buffer);
1218 }
1219
1220 return;
1221 /* ERRORS */
1222not_negotiated:
1223{
1224 GST_ERROR_OBJECT(self, "not negotiated");
1225 ret = GST_FLOW_NOT_NEGOTIATED;
1226 goto beach;
1227}
1228activate_failed:
1229{
1230 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1231 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1232 (_("Failed to allocate required memory.")),
1233 ("Buffer pool activation failed"));
1234 ret = GST_FLOW_ERROR;
1235 goto beach;
1236}
1237flushing:
1238{
1239 ret = GST_FLOW_FLUSHING;
1240 goto beach;
1241}
1242beach:
1243 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1244 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001245 if (self->is_res_chg) {
1246 //unblock flush start event
1247 g_mutex_lock(&self->res_chg_lock);
1248 self->is_res_chg = FALSE;
1249 g_cond_signal(&self->res_chg_cond);
1250 g_mutex_unlock(&self->res_chg_lock);
1251 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001252 gst_buffer_replace(&buffer, NULL);
1253 self->output_flow = ret;
1254 gst_aml_v4l2_object_unlock(self->v4l2output);
1255 gst_pad_pause_task(decoder->srcpad);
1256}
1257
1258static GstFlowReturn
1259gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1260 GstVideoCodecFrame *frame)
1261{
1262 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1263 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1264 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1265 GstFlowReturn ret = GST_FLOW_OK;
1266 gboolean processed = FALSE;
1267 GstBuffer *tmp;
1268 GstTaskState task_state;
1269 GstCaps *caps;
1270
1271 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1272
1273 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1274 goto flushing;
1275
1276 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1277 {
1278 if (!self->input_state)
1279 goto not_negotiated;
1280 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1281 &error))
1282 goto not_negotiated;
1283 }
1284
1285 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1286 {
1287 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001288 GstCapsFeatures *features = NULL;
1289
1290 features = gst_caps_get_features(self->input_state->caps, 0);
1291 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1292 {
1293 GST_DEBUG_OBJECT(self, "Is SVP");
1294 self->v4l2output->is_svp = TRUE;
1295 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001296
1297 GST_DEBUG_OBJECT(self, "Sending header");
1298
1299 codec_data = self->input_state->codec_data;
1300
1301 /* We are running in byte-stream mode, so we don't know the headers, but
1302 * we need to send something, otherwise the decoder will refuse to
1303 * intialize.
1304 */
1305 if (codec_data)
1306 {
1307 gst_buffer_ref(codec_data);
1308 }
1309 else
1310 {
1311 codec_data = gst_buffer_ref(frame->input_buffer);
1312 processed = TRUE;
1313 }
1314
1315 /* Ensure input internal pool is active */
1316 if (!gst_buffer_pool_is_active(pool))
1317 {
1318 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001319 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1320 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001321 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1322 // self->v4l2output->info.size, min, max);
1323 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1324
1325 /* There is no reason to refuse this config */
1326 if (!gst_buffer_pool_set_config(pool, config))
1327 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001328 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001329
1330 if (!gst_buffer_pool_set_active(pool, TRUE))
1331 goto activate_failed;
1332 }
1333
1334 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1335 ret =
1336 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001337 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001338 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1339
1340 gst_buffer_unref(codec_data);
1341
1342 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1343 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1344 * and returns the visible size as with/height and the coded size as
1345 * padding. */
1346 }
1347
1348 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1349 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1350 {
1351 /* It's possible that the processing thread stopped due to an error */
1352 if (self->output_flow != GST_FLOW_OK &&
1353 self->output_flow != GST_FLOW_FLUSHING)
1354 {
1355 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1356 ret = self->output_flow;
1357 goto drop;
1358 }
1359
1360 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1361
1362 /* Start the processing task, when it quits, the task will disable input
1363 * processing to unlock input if draining, or prevent potential block */
1364 self->output_flow = GST_FLOW_FLUSHING;
1365 if (!gst_pad_start_task(decoder->srcpad,
1366 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1367 goto start_task_failed;
1368 }
1369
1370 if (!processed)
1371 {
1372 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001373 if (!self->codec_data_inject && self->input_state->codec_data)
1374 {
1375 ret = gst_aml_v4l2_buffer_pool_process
1376 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1377 self->codec_data_inject = TRUE;
1378 if (ret != GST_FLOW_OK)
1379 goto send_codec_failed;
1380 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001381 ret =
1382 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1383 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1384
1385 if (ret == GST_FLOW_FLUSHING)
1386 {
1387 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1388 GST_TASK_STARTED)
1389 ret = self->output_flow;
1390 goto drop;
1391 }
1392 else if (ret != GST_FLOW_OK)
1393 {
1394 goto process_failed;
1395 }
1396 }
1397
1398 /* No need to keep input arround */
1399 tmp = frame->input_buffer;
1400 frame->input_buffer = gst_buffer_new();
1401 gst_buffer_copy_into(frame->input_buffer, tmp,
1402 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1403 GST_BUFFER_COPY_META,
1404 0, 0);
1405 gst_buffer_unref(tmp);
1406
1407 gst_video_codec_frame_unref(frame);
1408 return ret;
1409
1410 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001411send_codec_failed:
1412 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1413 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001414not_negotiated:
1415{
1416 GST_ERROR_OBJECT(self, "not negotiated");
1417 ret = GST_FLOW_NOT_NEGOTIATED;
1418 gst_aml_v4l2_error(self, &error);
1419 goto drop;
1420}
1421activate_failed:
1422{
1423 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1424 (_("Failed to allocate required memory.")),
1425 ("Buffer pool activation failed"));
1426 ret = GST_FLOW_ERROR;
1427 goto drop;
1428}
1429flushing:
1430{
1431 ret = GST_FLOW_FLUSHING;
1432 goto drop;
1433}
1434
1435start_task_failed:
1436{
1437 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1438 (_("Failed to start decoding thread.")), (NULL));
1439 ret = GST_FLOW_ERROR;
1440 goto drop;
1441}
1442process_failed:
1443{
1444 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1445 (_("Failed to process frame.")),
1446 ("Maybe be due to not enough memory or failing driver"));
1447 ret = GST_FLOW_ERROR;
1448 goto drop;
1449}
1450drop:
1451{
1452 gst_video_decoder_drop_frame(decoder, frame);
1453 return ret;
1454}
1455}
1456
1457static gboolean
1458gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1459 GstQuery *query)
1460{
1461 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1462 GstClockTime latency;
1463 gboolean ret = FALSE;
1464
1465 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1466 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1467
1468 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1469 {
1470 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1471 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1472 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1473 gst_video_decoder_set_latency(decoder, latency, latency);
1474 }
1475 else
1476 {
1477 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1478 }
1479
1480 return ret;
1481}
1482
1483static gboolean
1484gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1485{
1486 gboolean ret = TRUE;
1487 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1488
1489 switch (GST_QUERY_TYPE(query))
1490 {
1491 case GST_QUERY_CAPS:
1492 {
1493 GstCaps *filter, *result = NULL;
1494 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1495
1496 gst_query_parse_caps(query, &filter);
1497
1498 if (self->probed_srccaps)
1499 result = gst_caps_ref(self->probed_srccaps);
1500 else
1501 result = gst_pad_get_pad_template_caps(pad);
1502
1503 if (filter)
1504 {
1505 GstCaps *tmp = result;
1506 result =
1507 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1508 gst_caps_unref(tmp);
1509 }
1510
1511 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1512
1513 gst_query_set_caps_result(query, result);
1514 gst_caps_unref(result);
1515 break;
1516 }
1517
1518 default:
1519 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1520 break;
1521 }
1522
1523 return ret;
1524}
1525
1526static GstCaps *
1527gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1528{
1529 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1530 GstCaps *result;
1531
1532 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1533 filter);
1534
1535 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1536
1537 return result;
1538}
1539
1540static gboolean
1541gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1542{
1543 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1544 gboolean ret;
1545 GstEventType type = GST_EVENT_TYPE(event);
1546
1547 switch (type)
1548 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001549 case GST_EVENT_STREAM_START:
1550 {
1551 GstStructure *s;
1552 GstEvent *event;
1553 GST_DEBUG_OBJECT(self, "new private event");
1554 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1555 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1556 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1557 gst_pad_push_event (decoder->sinkpad, event);
1558 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1559 break;
1560 }
zengliang.li51f54b62023-10-10 12:14:49 +00001561 case GST_EVENT_CAPS:
1562 {
1563 GstCaps *caps;
1564 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001565 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001566
1567 gst_event_parse_caps (event, &caps);
1568 structure= gst_caps_get_structure(caps, 0);
1569 if ( gst_structure_has_field(structure, "parsed") )
1570 {
1571 gboolean parsed = TRUE;
1572 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1573 {
1574 self->v4l2output->stream_mode = !parsed;
1575 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1576 }
1577 }
sheng.liu641aa422023-12-26 07:05:59 +00001578
sheng.liudb26f7d2024-01-22 11:24:23 +00001579 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1580 {
1581 if ( denom == 0 ) denom= 1;
1582
1583 if (self->v4l2capture->fps)
1584 {
1585 g_value_unset(self->v4l2capture->fps);
1586 g_free(self->v4l2capture->fps);
1587 }
1588
1589 self->v4l2capture->fps = g_new0(GValue, 1);
1590 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1591 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1592
1593 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1594 }
1595
sheng.liu641aa422023-12-26 07:05:59 +00001596 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1597 ( !self->v4l2capture->have_set_par ) )
1598 {
1599 if ( (num <= 0) || (denom <= 0) )
1600 {
1601 num= denom= 1;
1602 }
1603
1604 if ( self->v4l2capture->par )
1605 {
1606 g_value_unset(self->v4l2capture->par);
1607 g_free(self->v4l2capture->par);
1608 }
1609
1610 self->v4l2capture->par = g_new0(GValue, 1);
1611 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1612 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1613 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1614 }
zengliang.li51f54b62023-10-10 12:14:49 +00001615 break;
1616 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001617 case GST_EVENT_FLUSH_START:
1618 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001619
1620 g_mutex_lock (&self->res_chg_lock);
1621 while (self->is_res_chg)
1622 {
1623 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1624 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1625 }
1626 g_mutex_unlock (&self->res_chg_lock);
1627
zengliang.li92ff6822023-06-06 07:12:52 +00001628 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001629 gst_aml_v4l2_object_unlock(self->v4l2output);
1630 gst_aml_v4l2_object_unlock(self->v4l2capture);
1631 break;
1632 default:
1633 break;
1634 }
1635
1636 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1637
1638 switch (type)
1639 {
1640 case GST_EVENT_FLUSH_START:
1641 /* The processing thread should stop now, wait for it */
1642 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001643 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001644 GST_DEBUG_OBJECT(self, "flush start done");
1645 break;
1646 default:
1647 break;
1648 }
1649
1650 return ret;
1651}
1652
1653static GstStateChangeReturn
1654gst_aml_v4l2_video_dec_change_state(GstElement *element,
1655 GstStateChange transition)
1656{
1657 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1658 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1659
1660 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1661 {
1662 g_atomic_int_set(&self->active, FALSE);
1663 gst_aml_v4l2_object_unlock(self->v4l2output);
1664 gst_aml_v4l2_object_unlock(self->v4l2capture);
1665 gst_pad_stop_task(decoder->srcpad);
1666 }
1667
1668 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1669}
1670
1671static void
1672gst_aml_v4l2_video_dec_dispose(GObject *object)
1673{
1674 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1675
1676 gst_caps_replace(&self->probed_sinkcaps, NULL);
1677 gst_caps_replace(&self->probed_srccaps, NULL);
1678
1679 G_OBJECT_CLASS(parent_class)->dispose(object);
1680}
1681
1682static void
1683gst_aml_v4l2_video_dec_finalize(GObject *object)
1684{
1685 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1686
1687 gst_aml_v4l2_object_destroy(self->v4l2capture);
1688 gst_aml_v4l2_object_destroy(self->v4l2output);
1689
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001690 g_mutex_clear(&self->res_chg_lock);
1691 g_cond_clear(&self->res_chg_cond);
1692
xuesong.jiang61ea8012022-05-12 15:38:17 +08001693#if GST_IMPORT_LGE_PROP
1694 if (self->lge_ctxt)
1695 {
1696 if (self->lge_ctxt->app_type)
1697 g_free(self->lge_ctxt->app_type);
1698 if (self->lge_ctxt->res_info.coretype)
1699 g_free(self->lge_ctxt->res_info.coretype);
1700 free(self->lge_ctxt);
1701 }
1702
1703#endif
1704
xuesong.jiangae1548e2022-05-06 16:38:46 +08001705 G_OBJECT_CLASS(parent_class)->finalize(object);
1706}
1707
1708static void
1709gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1710{
1711 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001712 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001713 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001714 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001715 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001716 g_mutex_init(&self->res_chg_lock);
1717 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001718#if GST_IMPORT_LGE_PROP
1719 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1720 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1721#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001722}
1723
1724static void
1725gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1726{
1727 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1728 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1729 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1730
1731 gst_video_decoder_set_packetized(decoder, TRUE);
1732
1733 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1734 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1735 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1736 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1737 self->v4l2output->no_initial_format = TRUE;
1738 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001739 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001740
1741 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1742 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1743 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1744 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1745 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001746 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001747}
1748
1749static void
1750gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1751{
1752 GstElementClass *element_class;
1753 GObjectClass *gobject_class;
1754 GstVideoDecoderClass *video_decoder_class;
1755
1756 parent_class = g_type_class_peek_parent(klass);
1757
1758 element_class = (GstElementClass *)klass;
1759 gobject_class = (GObjectClass *)klass;
1760 video_decoder_class = (GstVideoDecoderClass *)klass;
1761
1762 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1763 "AML V4L2 Video Decoder");
1764
1765 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1766 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1767 gobject_class->set_property =
1768 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1769 gobject_class->get_property =
1770 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1771
1772 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1773 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1774 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1775 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1776 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1777 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1778 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1779 video_decoder_class->set_format =
1780 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1781 video_decoder_class->negotiate =
1782 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1783 video_decoder_class->decide_allocation =
1784 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1785 /* FIXME propose_allocation or not ? */
1786 video_decoder_class->handle_frame =
1787 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1788 video_decoder_class->getcaps =
1789 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1790 video_decoder_class->src_query =
1791 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1792 video_decoder_class->sink_event =
1793 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1794
1795 element_class->change_state =
1796 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1797
xuesong.jiang406ee302023-06-28 03:45:22 +00001798 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1799 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1800 G_SIGNAL_RUN_LAST,
1801 0, /* class offset */
1802 NULL, /* accumulator */
1803 NULL, /* accu data */
1804 g_cclosure_marshal_generic,
1805 G_TYPE_NONE,
1806 1,
1807 G_TYPE_UINT64);
1808
xuesong.jiangae1548e2022-05-06 16:38:46 +08001809 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001810#if GST_IMPORT_LGE_PROP
1811 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1812#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001813}
1814
1815static void
1816gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1817{
1818 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1819 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1820 GstAmlV4l2VideoDecCData *cdata = data;
1821
1822 klass->default_device = cdata->device;
1823
1824 /* Note: gst_pad_template_new() take the floating ref from the caps */
1825 gst_element_class_add_pad_template(element_class,
1826 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1827 cdata->sink_caps));
1828 gst_element_class_add_pad_template(element_class,
1829 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1830 cdata->src_caps));
1831
1832 gst_element_class_set_metadata(element_class, cdata->longname,
1833 "Codec/Decoder/Video/Hardware", cdata->description,
1834 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1835
1836 gst_caps_unref(cdata->sink_caps);
1837 gst_caps_unref(cdata->src_caps);
1838 g_free(cdata);
1839}
1840
1841/* Probing functions */
1842gboolean
1843gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1844{
1845 gboolean ret = FALSE;
1846
1847 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()))
1848 ret = TRUE;
1849
1850 return ret;
1851}
1852
1853static gchar *
1854gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1855 const gchar *basename)
1856{
1857 gchar *codec_name = NULL;
1858 gchar *type_name = NULL;
1859 gboolean got_value = FALSE;
1860
1861#define SET_META(codec) \
1862 G_STMT_START \
1863 { \
1864 cdata->longname = "AML V4L2 " codec " Decoder"; \
1865 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1866 codec_name = g_ascii_strdown(codec, -1); \
1867 } \
1868 G_STMT_END
1869
1870 if (gst_structure_has_name(s, "image/jpeg"))
1871 {
1872 SET_META("JPEG");
1873 }
1874 else if (gst_structure_has_name(s, "video/mpeg"))
1875 {
1876 gint mpegversion = 0;
1877 gint *list = NULL;
1878 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1879 if (FALSE == got_value)
1880 {
1881 got_value = gst_structure_get_list(s, "mpegversion", &list);
1882 if (TRUE == got_value && (1 == *list || 2 == *list))
1883 {
1884 SET_META("MPEG2");
1885 }
1886 else
1887 {
1888 SET_META("MPEG4");
1889 }
1890 }
1891 else
1892 {
1893 SET_META("MPEG4");
1894 }
1895 }
1896 else if (gst_structure_has_name(s, "video/x-h263"))
1897 {
1898 SET_META("H263");
1899 }
1900 else if (gst_structure_has_name(s, "video/x-fwht"))
1901 {
1902 SET_META("FWHT");
1903 }
1904 else if (gst_structure_has_name(s, "video/x-h264"))
1905 {
1906 SET_META("H264");
1907 }
1908 else if (gst_structure_has_name(s, "video/x-h265"))
1909 {
1910 SET_META("H265");
1911 }
1912 else if (gst_structure_has_name(s, "video/x-wmv"))
1913 {
1914 SET_META("VC1");
1915 }
1916 else if (gst_structure_has_name(s, "video/x-vp8"))
1917 {
1918 SET_META("VP8");
1919 }
1920 else if (gst_structure_has_name(s, "video/x-vp9"))
1921 {
1922 SET_META("VP9");
1923 }
1924 else if (gst_structure_has_name(s, "video/x-av1"))
1925 {
1926 SET_META("AV1");
1927 }
zengliang.li51f54b62023-10-10 12:14:49 +00001928 else if (gst_structure_has_name(s, "video/x-avs"))
1929 {
1930 SET_META("AVS");
1931 }
1932 else if (gst_structure_has_name(s, "video/x-avs2"))
1933 {
1934 SET_META("AVS2");
1935 }
1936 else if (gst_structure_has_name(s, "video/x-avs3"))
1937 {
1938 SET_META("AVS3");
1939 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001940 else if (gst_structure_has_name(s, "video/x-bayer"))
1941 {
1942 SET_META("BAYER");
1943 }
1944 else if (gst_structure_has_name(s, "video/x-sonix"))
1945 {
1946 SET_META("SONIX");
1947 }
1948 else if (gst_structure_has_name(s, "video/x-pwc1"))
1949 {
1950 SET_META("PWC1");
1951 }
1952 else if (gst_structure_has_name(s, "video/x-pwc2"))
1953 {
1954 SET_META("PWC2");
1955 }
1956 else
1957 {
1958 /* This code should be kept on sync with the exposed CODEC type of format
1959 * from gstamlv4l2object.c. This warning will only occure in case we forget
1960 * to also add a format here. */
1961 gchar *s_str = gst_structure_to_string(s);
1962 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
1963 "bug, please report at https://bugs.gnome.org",
1964 s_str);
1965 g_free(s_str);
1966 }
1967
1968 if (codec_name)
1969 {
1970 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
1971 if (g_type_from_name(type_name) != 0)
1972 {
1973 g_free(type_name);
1974 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
1975 }
1976
1977 g_free(codec_name);
1978 }
1979
1980 return type_name;
1981#undef SET_META
1982}
1983
1984void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
1985 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
1986{
1987 gint i;
1988
1989 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
1990 {
1991 GstAmlV4l2VideoDecCData *cdata;
1992 GstStructure *s;
1993 GTypeQuery type_query;
1994 GTypeInfo type_info = {
1995 0,
1996 };
1997 GType type, subtype;
1998 gchar *type_name;
1999
2000 s = gst_caps_get_structure(sink_caps, i);
2001
2002 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2003 cdata->device = g_strdup(device_path);
2004 cdata->sink_caps = gst_caps_new_empty();
2005 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2006 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2007 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2008 cdata->src_caps = gst_caps_copy(src_caps);
2009 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2010 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2011 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2012
2013 /* Skip over if we hit an unmapped type */
2014 if (!type_name)
2015 {
2016 g_free(cdata);
2017 continue;
2018 }
2019
2020 type = gst_aml_v4l2_video_dec_get_type();
2021 g_type_query(type, &type_query);
2022 memset(&type_info, 0, sizeof(type_info));
2023 type_info.class_size = type_query.class_size;
2024 type_info.instance_size = type_query.instance_size;
2025 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2026 type_info.class_data = cdata;
2027 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2028
2029 subtype = g_type_register_static(type, type_name, &type_info, 0);
2030 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2031 subtype))
2032 GST_WARNING("Failed to register plugin '%s'", type_name);
2033
2034 g_free(type_name);
2035 }
2036}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002037
2038#if GST_IMPORT_LGE_PROP
2039static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2040{
2041 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2042 g_param_spec_object("resource-info", "resource-info",
2043 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2044 GST_TYPE_STRUCTURE,
2045 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2046
2047 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2048 g_param_spec_uint64("decoded-size", "decoded-size",
2049 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2050 0, G_MAXUINT64,
2051 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2052
2053 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2054 g_param_spec_uint64("undecoded-size", "undecoded-size",
2055 "video decoder element's total undecoded data update unit is byte.",
2056 0, G_MAXUINT64,
2057 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2058
2059 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2060 g_param_spec_string("app-type", "app-type",
2061 "set application type.",
2062 "default_app",
2063 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2064
2065 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2066 g_param_spec_boolean("clip-mode", "clip-mode",
2067 "When seeking, Content is moving faster for a while to skip frames.",
2068 FALSE,
2069 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2070}
2071#endif