blob: b1db3e286df72679fcca31457ff06da356890cc5 [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.luo419c4a92023-07-14 07:36:07 +0000911gst_aml_v4l2_video_dec_set_fence(GstVideoDecoder *decoder)
hanghang.luo70f07ef2023-07-13 02:23:06 +0000912{
913 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
914 GstStructure *s;
915 GstEvent *event;
916
hanghang.luo419c4a92023-07-14 07:36:07 +0000917 guint fence_num = self->v4l2capture->min_buffers-2;
hanghang.luo70f07ef2023-07-13 02:23:06 +0000918 s = gst_structure_new ("video_fence","fence_num",G_TYPE_UINT,fence_num,NULL);
919 if (s)
920 {
921 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
922 GST_DEBUG_OBJECT(self,"Send video_fence Event: %"GST_PTR_FORMAT,event);
923 gst_pad_push_event (decoder->srcpad, event);
924 }
925}
926
927static void
hanghang.luo2eec4892023-07-18 06:44:42 +0000928gst_aml_v4l2_video_dec_set_output_status(GstVideoDecoder *decoder,GstVideoInfo info)
929{
930 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
931 GstVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000932 struct v4l2_selection sel;
933 struct v4l2_rect *r = NULL;
934 GstStructure *s;
935 gint width = 0;
936 gint height = 0;
hanghang.luo2eec4892023-07-18 06:44:42 +0000937 output_state = gst_video_decoder_set_output_state(decoder,
938 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000939 memset(&sel, 0, sizeof(struct v4l2_selection));
940 sel.type = self->v4l2capture->type;
941 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
942 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
943 {
944 r = &sel.r;
945 width = (r->width/2)*2;
946 height = (r->height/2)*2;
947 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
948 }
949 else
950 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000951 if (output_state)
952 {
953 output_state->info.interlace_mode = info.interlace_mode;
954 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000955 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000956 s = gst_caps_get_structure(output_state->caps, 0);
957 if (s)
958 {
959 gst_structure_set(s,"coded_width",G_TYPE_INT,info.width,NULL);
960 gst_structure_set(s,"coded_height",G_TYPE_INT,info.height,NULL);
961 gst_structure_set(s,"width",G_TYPE_INT,width,NULL);
962 gst_structure_set(s,"height",G_TYPE_INT,height,NULL);
963 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
964 gst_video_codec_state_unref(output_state);
965 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000966 }
967}
968
969static void
xuesong.jiangae1548e2022-05-06 16:38:46 +0800970gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
971{
972 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
973 GstAmlV4l2BufferPool *v4l2_pool;
974 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
975 GstBufferPool *pool;
976 GstVideoCodecFrame *frame;
977 GstBuffer *buffer = NULL;
978 GstFlowReturn ret;
979
980 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
981 {
982 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +0000983 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800984 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800985 GST_DEBUG_OBJECT(self, "waitting source change event");
986 /* Wait until received SOURCE_CHANGE event to get right video format */
987 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
988 {
989 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
990 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
991 {
fei.deng2a06e042023-10-10 03:09:45 +0000992 //let flush start event blocked until capture buffer pool actived
993 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800994 GST_DEBUG_OBJECT(self, "Received source change event");
995 break;
996 }
997 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
998 {
999 GST_DEBUG_OBJECT(self, "Received eos event");
1000 goto beach;
1001 }
1002 else if (ret != GST_FLOW_OK)
1003 {
1004 GST_ERROR_OBJECT(self, "dqevent error");
1005 goto beach;
1006 }
1007 }
1008 self->v4l2capture->need_wait_event = FALSE;
1009
sheng.liu0c77f6c2022-06-17 21:33:20 +08001010 if (TRUE == self->v4l2output->is_svp)
1011 {
1012 GstPad *peer;
1013 GstStructure *s;
1014 GstEvent *event;
1015
1016 peer = gst_pad_get_peer (decoder->srcpad);
1017 if (peer)
1018 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001019 s = gst_structure_new_empty ("IS_SVP");
1020 if (s)
1021 {
1022 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1023 gst_pad_send_event (peer, event);
1024 GST_DEBUG_OBJECT(self, "Send SVP Event");
1025 }
1026 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001027 }
1028 }
1029
sheng.liub56bbc52022-06-21 11:02:33 +08001030 if (self->v4l2capture->need_drop_event)
1031 {
1032 // drop V4L2_EVENT_SOURCE_CHANGE
1033 gst_v4l2_drop_event(self->v4l2capture);
1034 self->v4l2capture->need_drop_event = FALSE;
1035 }
1036
xuesong.jiangae1548e2022-05-06 16:38:46 +08001037 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1038 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001039
xuesong.jiangae1548e2022-05-06 16:38:46 +08001040 /* Create caps from the acquired format, remove the format field */
1041 acquired_caps = gst_video_info_to_caps(&info);
1042 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1043 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001044 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1045
1046 /* Probe currently available pixel formats */
1047 available_caps = gst_caps_copy(self->probed_srccaps);
1048 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1049
1050 /* Replace coded size with visible size, we want to negotiate visible size
1051 * with downstream, not coded size. */
1052 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1053
1054 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001055 caps = gst_caps_copy(filter);
1056 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1057 gst_caps_append(filter, caps);
1058
1059 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1060 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001061 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001062 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1063 gst_caps_unref(filter);
1064
1065 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1066 if (gst_caps_is_empty(caps))
1067 {
1068 gst_caps_unref(caps);
1069 goto not_negotiated;
1070 }
1071
1072 /* Fixate pixel format */
1073 caps = gst_caps_fixate(caps);
1074
1075 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1076
1077 /* Try to set negotiated format, on success replace acquired format */
1078 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1079 gst_video_info_from_caps(&info, caps);
1080 else
1081 gst_aml_v4l2_clear_error(&error);
1082 gst_caps_unref(caps);
hanghang.luo419c4a92023-07-14 07:36:07 +00001083 gst_aml_v4l2_video_dec_set_fence(decoder);
hanghang.luo2eec4892023-07-18 06:44:42 +00001084 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001085 if (!gst_video_decoder_negotiate(decoder))
1086 {
1087 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1088 goto flushing;
1089 else
1090 goto not_negotiated;
1091 }
1092
1093 /* Ensure our internal pool is activated */
1094 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1095 TRUE))
1096 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001097
1098 g_mutex_lock(&self->res_chg_lock);
1099 GST_LOG_OBJECT(decoder, "signal resolution changed");
1100 self->is_res_chg = FALSE;
1101 g_cond_signal(&self->res_chg_cond);
1102 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001103 }
1104
1105 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1106
1107 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1108
1109 self->output_flow = GST_FLOW_OK;
1110 do
1111 {
1112 /* We cannot use the base class allotate helper since it taking the internal
1113 * stream lock. we know that the acquire may need to poll until more frames
1114 * comes in and holding this lock would prevent that.
1115 */
1116 pool = gst_video_decoder_get_buffer_pool(decoder);
1117
1118 /* Pool may be NULL if we started going to READY state */
1119 if (pool == NULL)
1120 {
fei.dengbee20862022-06-14 14:59:48 +08001121 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001122 ret = GST_FLOW_FLUSHING;
1123 goto beach;
1124 }
1125
1126 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
fei.dengccc89632022-07-15 19:10:17 +08001127 //calculate a new pts for interlace stream
hanghang.luo8ec04e92023-10-09 08:14:24 +00001128 if (ret == GST_FLOW_OK && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
fei.dengccc89632022-07-15 19:10:17 +08001129 {
1130 //if buffer pts is valid, reduce 1/2 duration
1131 if (GST_BUFFER_DURATION_IS_VALID(buffer))
1132 {
1133 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
1134 }
1135 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1136 //reset pts
fei.denga6ae3282022-07-15 19:50:30 +08001137 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL || self->last_out_pts == GST_BUFFER_TIMESTAMP (buffer))
fei.dengccc89632022-07-15 19:10:17 +08001138 {
1139 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
1140 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1141 }
1142 }
1143
xuesong.jiangae1548e2022-05-06 16:38:46 +08001144 g_object_unref(pool);
1145
fei.deng9a5cd6e2023-06-30 12:09:18 +00001146 if (ret == GST_FLOW_OK && GST_BUFFER_FLAG_IS_SET(buffer,GST_AML_V4L2_BUFFER_FLAG_LAST_EMPTY)) {
sheng.liubcf036c2022-06-21 15:55:42 +08001147 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
sheng.liub56bbc52022-06-21 11:02:33 +08001148 self->v4l2capture->need_drop_event = TRUE;
fei.deng594df4b2023-06-26 07:03:29 +00001149 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
sheng.liub56bbc52022-06-21 11:02:33 +08001150 goto beach;
1151 }
1152
sheng.liu8d18ed22022-05-26 17:28:15 +08001153 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1154 {
1155 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001156
1157 g_mutex_lock (&self->res_chg_lock);
1158 self->is_res_chg = TRUE;
1159 g_mutex_unlock (&self->res_chg_lock);
1160
sheng.liu8d18ed22022-05-26 17:28:15 +08001161 gst_aml_v4l2_object_stop(self->v4l2capture);
1162 return;
1163 }
1164
fei.dengbee20862022-06-14 14:59:48 +08001165 if (ret != GST_FLOW_OK) {
1166 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001167 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001168 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001169
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001170 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001171 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001172
1173 GST_DEBUG_OBJECT(decoder, "send pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1174 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1175
xuesong.jiangae1548e2022-05-06 16:38:46 +08001176 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1177 {
1178 gst_aml_v4l2_object_stop(self->v4l2capture);
1179 return;
1180 }
1181
1182 } while (ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER);
1183
1184 if (ret != GST_FLOW_OK)
1185 goto beach;
1186
fei.dengbee20862022-06-14 14:59:48 +08001187 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001188 if (frame)
1189 {
zengliang.li32cb11e2022-11-24 12:10:26 +08001190 if (!GST_CLOCK_TIME_IS_VALID(frame->pts))
1191 {
zengliang.li92ff6822023-06-06 07:12:52 +00001192 if (!GST_CLOCK_TIME_IS_VALID(self->last_out_pts))
1193 {
1194 if (GST_CLOCK_TIME_IS_VALID(frame->dts))
1195 {
1196 GST_BUFFER_TIMESTAMP(buffer) = frame->dts;
1197 }
1198 else
1199 {
1200 GST_WARNING_OBJECT (decoder,"sorry,we have no baseline to calculate pts");
1201 goto beach;
1202 }
1203 }
1204 else
1205 {
1206 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d);
1207 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
1208 }
zengliang.li32cb11e2022-11-24 12:10:26 +08001209 }
fei.dengccc89632022-07-15 19:10:17 +08001210 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001211 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001212 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1213 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001214 buffer = NULL;
1215 ret = gst_video_decoder_finish_frame(decoder, frame);
1216
1217 if (ret != GST_FLOW_OK)
1218 goto beach;
1219 }
1220 else
1221 {
1222 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
1223 gst_buffer_unref(buffer);
1224 }
1225
1226 return;
1227 /* ERRORS */
1228not_negotiated:
1229{
1230 GST_ERROR_OBJECT(self, "not negotiated");
1231 ret = GST_FLOW_NOT_NEGOTIATED;
1232 goto beach;
1233}
1234activate_failed:
1235{
1236 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1237 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1238 (_("Failed to allocate required memory.")),
1239 ("Buffer pool activation failed"));
1240 ret = GST_FLOW_ERROR;
1241 goto beach;
1242}
1243flushing:
1244{
1245 ret = GST_FLOW_FLUSHING;
1246 goto beach;
1247}
1248beach:
1249 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1250 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001251 if (self->is_res_chg) {
1252 //unblock flush start event
1253 g_mutex_lock(&self->res_chg_lock);
1254 self->is_res_chg = FALSE;
1255 g_cond_signal(&self->res_chg_cond);
1256 g_mutex_unlock(&self->res_chg_lock);
1257 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001258 gst_buffer_replace(&buffer, NULL);
1259 self->output_flow = ret;
1260 gst_aml_v4l2_object_unlock(self->v4l2output);
1261 gst_pad_pause_task(decoder->srcpad);
1262}
1263
1264static GstFlowReturn
1265gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
1266 GstVideoCodecFrame *frame)
1267{
1268 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1269 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1270 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1271 GstFlowReturn ret = GST_FLOW_OK;
1272 gboolean processed = FALSE;
1273 GstBuffer *tmp;
1274 GstTaskState task_state;
1275 GstCaps *caps;
1276
1277 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1278
1279 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1280 goto flushing;
1281
1282 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1283 {
1284 if (!self->input_state)
1285 goto not_negotiated;
1286 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1287 &error))
1288 goto not_negotiated;
1289 }
1290
1291 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1292 {
1293 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001294 GstCapsFeatures *features = NULL;
1295
1296 features = gst_caps_get_features(self->input_state->caps, 0);
1297 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1298 {
1299 GST_DEBUG_OBJECT(self, "Is SVP");
1300 self->v4l2output->is_svp = TRUE;
1301 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001302
1303 GST_DEBUG_OBJECT(self, "Sending header");
1304
1305 codec_data = self->input_state->codec_data;
1306
1307 /* We are running in byte-stream mode, so we don't know the headers, but
1308 * we need to send something, otherwise the decoder will refuse to
1309 * intialize.
1310 */
1311 if (codec_data)
1312 {
1313 gst_buffer_ref(codec_data);
1314 }
1315 else
1316 {
1317 codec_data = gst_buffer_ref(frame->input_buffer);
1318 processed = TRUE;
1319 }
1320
1321 /* Ensure input internal pool is active */
1322 if (!gst_buffer_pool_is_active(pool))
1323 {
1324 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001325 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1326 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001327 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1328 // self->v4l2output->info.size, min, max);
1329 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1330
1331 /* There is no reason to refuse this config */
1332 if (!gst_buffer_pool_set_config(pool, config))
1333 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001334 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001335
1336 if (!gst_buffer_pool_set_active(pool, TRUE))
1337 goto activate_failed;
1338 }
1339
1340 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1341 ret =
1342 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001343 self->codec_data_inject = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001344 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1345
1346 gst_buffer_unref(codec_data);
1347
1348 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1349 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1350 * and returns the visible size as with/height and the coded size as
1351 * padding. */
1352 }
1353
1354 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1355 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1356 {
1357 /* It's possible that the processing thread stopped due to an error */
1358 if (self->output_flow != GST_FLOW_OK &&
1359 self->output_flow != GST_FLOW_FLUSHING)
1360 {
1361 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1362 ret = self->output_flow;
1363 goto drop;
1364 }
1365
1366 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1367
1368 /* Start the processing task, when it quits, the task will disable input
1369 * processing to unlock input if draining, or prevent potential block */
1370 self->output_flow = GST_FLOW_FLUSHING;
1371 if (!gst_pad_start_task(decoder->srcpad,
1372 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1373 goto start_task_failed;
1374 }
1375
1376 if (!processed)
1377 {
1378 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001379 if (!self->codec_data_inject && self->input_state->codec_data)
1380 {
1381 ret = gst_aml_v4l2_buffer_pool_process
1382 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1383 self->codec_data_inject = TRUE;
1384 if (ret != GST_FLOW_OK)
1385 goto send_codec_failed;
1386 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001387 ret =
1388 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1389 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1390
1391 if (ret == GST_FLOW_FLUSHING)
1392 {
1393 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1394 GST_TASK_STARTED)
1395 ret = self->output_flow;
1396 goto drop;
1397 }
1398 else if (ret != GST_FLOW_OK)
1399 {
1400 goto process_failed;
1401 }
1402 }
1403
1404 /* No need to keep input arround */
1405 tmp = frame->input_buffer;
1406 frame->input_buffer = gst_buffer_new();
1407 gst_buffer_copy_into(frame->input_buffer, tmp,
1408 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1409 GST_BUFFER_COPY_META,
1410 0, 0);
1411 gst_buffer_unref(tmp);
1412
1413 gst_video_codec_frame_unref(frame);
1414 return ret;
1415
1416 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001417send_codec_failed:
1418 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1419 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001420not_negotiated:
1421{
1422 GST_ERROR_OBJECT(self, "not negotiated");
1423 ret = GST_FLOW_NOT_NEGOTIATED;
1424 gst_aml_v4l2_error(self, &error);
1425 goto drop;
1426}
1427activate_failed:
1428{
1429 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1430 (_("Failed to allocate required memory.")),
1431 ("Buffer pool activation failed"));
1432 ret = GST_FLOW_ERROR;
1433 goto drop;
1434}
1435flushing:
1436{
1437 ret = GST_FLOW_FLUSHING;
1438 goto drop;
1439}
1440
1441start_task_failed:
1442{
1443 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1444 (_("Failed to start decoding thread.")), (NULL));
1445 ret = GST_FLOW_ERROR;
1446 goto drop;
1447}
1448process_failed:
1449{
1450 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1451 (_("Failed to process frame.")),
1452 ("Maybe be due to not enough memory or failing driver"));
1453 ret = GST_FLOW_ERROR;
1454 goto drop;
1455}
1456drop:
1457{
1458 gst_video_decoder_drop_frame(decoder, frame);
1459 return ret;
1460}
1461}
1462
1463static gboolean
1464gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1465 GstQuery *query)
1466{
1467 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1468 GstClockTime latency;
1469 gboolean ret = FALSE;
1470
1471 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1472 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1473
1474 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1475 {
1476 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1477 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1478 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1479 gst_video_decoder_set_latency(decoder, latency, latency);
1480 }
1481 else
1482 {
1483 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1484 }
1485
1486 return ret;
1487}
1488
1489static gboolean
1490gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1491{
1492 gboolean ret = TRUE;
1493 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1494
1495 switch (GST_QUERY_TYPE(query))
1496 {
1497 case GST_QUERY_CAPS:
1498 {
1499 GstCaps *filter, *result = NULL;
1500 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1501
1502 gst_query_parse_caps(query, &filter);
1503
1504 if (self->probed_srccaps)
1505 result = gst_caps_ref(self->probed_srccaps);
1506 else
1507 result = gst_pad_get_pad_template_caps(pad);
1508
1509 if (filter)
1510 {
1511 GstCaps *tmp = result;
1512 result =
1513 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1514 gst_caps_unref(tmp);
1515 }
1516
1517 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1518
1519 gst_query_set_caps_result(query, result);
1520 gst_caps_unref(result);
1521 break;
1522 }
1523
1524 default:
1525 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1526 break;
1527 }
1528
1529 return ret;
1530}
1531
1532static GstCaps *
1533gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1534{
1535 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1536 GstCaps *result;
1537
1538 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1539 filter);
1540
1541 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1542
1543 return result;
1544}
1545
1546static gboolean
1547gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1548{
1549 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1550 gboolean ret;
1551 GstEventType type = GST_EVENT_TYPE(event);
1552
1553 switch (type)
1554 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001555 case GST_EVENT_STREAM_START:
1556 {
1557 GstStructure *s;
1558 GstEvent *event;
1559 GST_DEBUG_OBJECT(self, "new private event");
1560 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1561 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1562 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1563 gst_pad_push_event (decoder->sinkpad, event);
1564 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1565 break;
1566 }
zengliang.li51f54b62023-10-10 12:14:49 +00001567 case GST_EVENT_CAPS:
1568 {
1569 GstCaps *caps;
1570 GstStructure *structure;
1571
1572 gst_event_parse_caps (event, &caps);
1573 structure= gst_caps_get_structure(caps, 0);
1574 if ( gst_structure_has_field(structure, "parsed") )
1575 {
1576 gboolean parsed = TRUE;
1577 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1578 {
1579 self->v4l2output->stream_mode = !parsed;
1580 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1581 }
1582 }
1583 break;
1584 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001585 case GST_EVENT_FLUSH_START:
1586 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001587
1588 g_mutex_lock (&self->res_chg_lock);
1589 while (self->is_res_chg)
1590 {
1591 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1592 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1593 }
1594 g_mutex_unlock (&self->res_chg_lock);
1595
zengliang.li92ff6822023-06-06 07:12:52 +00001596 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001597 gst_aml_v4l2_object_unlock(self->v4l2output);
1598 gst_aml_v4l2_object_unlock(self->v4l2capture);
1599 break;
1600 default:
1601 break;
1602 }
1603
1604 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1605
1606 switch (type)
1607 {
1608 case GST_EVENT_FLUSH_START:
1609 /* The processing thread should stop now, wait for it */
1610 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001611 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001612 GST_DEBUG_OBJECT(self, "flush start done");
1613 break;
1614 default:
1615 break;
1616 }
1617
1618 return ret;
1619}
1620
1621static GstStateChangeReturn
1622gst_aml_v4l2_video_dec_change_state(GstElement *element,
1623 GstStateChange transition)
1624{
1625 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1626 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1627
1628 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1629 {
1630 g_atomic_int_set(&self->active, FALSE);
1631 gst_aml_v4l2_object_unlock(self->v4l2output);
1632 gst_aml_v4l2_object_unlock(self->v4l2capture);
1633 gst_pad_stop_task(decoder->srcpad);
1634 }
1635
1636 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1637}
1638
1639static void
1640gst_aml_v4l2_video_dec_dispose(GObject *object)
1641{
1642 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1643
1644 gst_caps_replace(&self->probed_sinkcaps, NULL);
1645 gst_caps_replace(&self->probed_srccaps, NULL);
1646
1647 G_OBJECT_CLASS(parent_class)->dispose(object);
1648}
1649
1650static void
1651gst_aml_v4l2_video_dec_finalize(GObject *object)
1652{
1653 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1654
1655 gst_aml_v4l2_object_destroy(self->v4l2capture);
1656 gst_aml_v4l2_object_destroy(self->v4l2output);
1657
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001658 g_mutex_clear(&self->res_chg_lock);
1659 g_cond_clear(&self->res_chg_cond);
1660
xuesong.jiang61ea8012022-05-12 15:38:17 +08001661#if GST_IMPORT_LGE_PROP
1662 if (self->lge_ctxt)
1663 {
1664 if (self->lge_ctxt->app_type)
1665 g_free(self->lge_ctxt->app_type);
1666 if (self->lge_ctxt->res_info.coretype)
1667 g_free(self->lge_ctxt->res_info.coretype);
1668 free(self->lge_ctxt);
1669 }
1670
1671#endif
1672
xuesong.jiangae1548e2022-05-06 16:38:46 +08001673 G_OBJECT_CLASS(parent_class)->finalize(object);
1674}
1675
1676static void
1677gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1678{
1679 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001680 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001681 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001682 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001683 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001684 g_mutex_init(&self->res_chg_lock);
1685 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001686#if GST_IMPORT_LGE_PROP
1687 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1688 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1689#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001690}
1691
1692static void
1693gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1694{
1695 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1696 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1697 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1698
1699 gst_video_decoder_set_packetized(decoder, TRUE);
1700
1701 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1702 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1703 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1704 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1705 self->v4l2output->no_initial_format = TRUE;
1706 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001707 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001708
1709 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1710 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1711 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1712 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1713 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001714 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001715}
1716
1717static void
1718gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1719{
1720 GstElementClass *element_class;
1721 GObjectClass *gobject_class;
1722 GstVideoDecoderClass *video_decoder_class;
1723
1724 parent_class = g_type_class_peek_parent(klass);
1725
1726 element_class = (GstElementClass *)klass;
1727 gobject_class = (GObjectClass *)klass;
1728 video_decoder_class = (GstVideoDecoderClass *)klass;
1729
1730 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1731 "AML V4L2 Video Decoder");
1732
1733 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1734 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1735 gobject_class->set_property =
1736 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1737 gobject_class->get_property =
1738 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1739
1740 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1741 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1742 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1743 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1744 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1745 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1746 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1747 video_decoder_class->set_format =
1748 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1749 video_decoder_class->negotiate =
1750 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1751 video_decoder_class->decide_allocation =
1752 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1753 /* FIXME propose_allocation or not ? */
1754 video_decoder_class->handle_frame =
1755 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1756 video_decoder_class->getcaps =
1757 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1758 video_decoder_class->src_query =
1759 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1760 video_decoder_class->sink_event =
1761 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1762
1763 element_class->change_state =
1764 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1765
xuesong.jiang406ee302023-06-28 03:45:22 +00001766 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1767 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1768 G_SIGNAL_RUN_LAST,
1769 0, /* class offset */
1770 NULL, /* accumulator */
1771 NULL, /* accu data */
1772 g_cclosure_marshal_generic,
1773 G_TYPE_NONE,
1774 1,
1775 G_TYPE_UINT64);
1776
xuesong.jiangae1548e2022-05-06 16:38:46 +08001777 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001778#if GST_IMPORT_LGE_PROP
1779 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1780#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001781}
1782
1783static void
1784gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1785{
1786 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1787 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1788 GstAmlV4l2VideoDecCData *cdata = data;
1789
1790 klass->default_device = cdata->device;
1791
1792 /* Note: gst_pad_template_new() take the floating ref from the caps */
1793 gst_element_class_add_pad_template(element_class,
1794 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1795 cdata->sink_caps));
1796 gst_element_class_add_pad_template(element_class,
1797 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1798 cdata->src_caps));
1799
1800 gst_element_class_set_metadata(element_class, cdata->longname,
1801 "Codec/Decoder/Video/Hardware", cdata->description,
1802 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1803
1804 gst_caps_unref(cdata->sink_caps);
1805 gst_caps_unref(cdata->src_caps);
1806 g_free(cdata);
1807}
1808
1809/* Probing functions */
1810gboolean
1811gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1812{
1813 gboolean ret = FALSE;
1814
1815 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()))
1816 ret = TRUE;
1817
1818 return ret;
1819}
1820
1821static gchar *
1822gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1823 const gchar *basename)
1824{
1825 gchar *codec_name = NULL;
1826 gchar *type_name = NULL;
1827 gboolean got_value = FALSE;
1828
1829#define SET_META(codec) \
1830 G_STMT_START \
1831 { \
1832 cdata->longname = "AML V4L2 " codec " Decoder"; \
1833 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1834 codec_name = g_ascii_strdown(codec, -1); \
1835 } \
1836 G_STMT_END
1837
1838 if (gst_structure_has_name(s, "image/jpeg"))
1839 {
1840 SET_META("JPEG");
1841 }
1842 else if (gst_structure_has_name(s, "video/mpeg"))
1843 {
1844 gint mpegversion = 0;
1845 gint *list = NULL;
1846 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1847 if (FALSE == got_value)
1848 {
1849 got_value = gst_structure_get_list(s, "mpegversion", &list);
1850 if (TRUE == got_value && (1 == *list || 2 == *list))
1851 {
1852 SET_META("MPEG2");
1853 }
1854 else
1855 {
1856 SET_META("MPEG4");
1857 }
1858 }
1859 else
1860 {
1861 SET_META("MPEG4");
1862 }
1863 }
1864 else if (gst_structure_has_name(s, "video/x-h263"))
1865 {
1866 SET_META("H263");
1867 }
1868 else if (gst_structure_has_name(s, "video/x-fwht"))
1869 {
1870 SET_META("FWHT");
1871 }
1872 else if (gst_structure_has_name(s, "video/x-h264"))
1873 {
1874 SET_META("H264");
1875 }
1876 else if (gst_structure_has_name(s, "video/x-h265"))
1877 {
1878 SET_META("H265");
1879 }
1880 else if (gst_structure_has_name(s, "video/x-wmv"))
1881 {
1882 SET_META("VC1");
1883 }
1884 else if (gst_structure_has_name(s, "video/x-vp8"))
1885 {
1886 SET_META("VP8");
1887 }
1888 else if (gst_structure_has_name(s, "video/x-vp9"))
1889 {
1890 SET_META("VP9");
1891 }
1892 else if (gst_structure_has_name(s, "video/x-av1"))
1893 {
1894 SET_META("AV1");
1895 }
zengliang.li51f54b62023-10-10 12:14:49 +00001896 else if (gst_structure_has_name(s, "video/x-avs"))
1897 {
1898 SET_META("AVS");
1899 }
1900 else if (gst_structure_has_name(s, "video/x-avs2"))
1901 {
1902 SET_META("AVS2");
1903 }
1904 else if (gst_structure_has_name(s, "video/x-avs3"))
1905 {
1906 SET_META("AVS3");
1907 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001908 else if (gst_structure_has_name(s, "video/x-bayer"))
1909 {
1910 SET_META("BAYER");
1911 }
1912 else if (gst_structure_has_name(s, "video/x-sonix"))
1913 {
1914 SET_META("SONIX");
1915 }
1916 else if (gst_structure_has_name(s, "video/x-pwc1"))
1917 {
1918 SET_META("PWC1");
1919 }
1920 else if (gst_structure_has_name(s, "video/x-pwc2"))
1921 {
1922 SET_META("PWC2");
1923 }
1924 else
1925 {
1926 /* This code should be kept on sync with the exposed CODEC type of format
1927 * from gstamlv4l2object.c. This warning will only occure in case we forget
1928 * to also add a format here. */
1929 gchar *s_str = gst_structure_to_string(s);
1930 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
1931 "bug, please report at https://bugs.gnome.org",
1932 s_str);
1933 g_free(s_str);
1934 }
1935
1936 if (codec_name)
1937 {
1938 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
1939 if (g_type_from_name(type_name) != 0)
1940 {
1941 g_free(type_name);
1942 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
1943 }
1944
1945 g_free(codec_name);
1946 }
1947
1948 return type_name;
1949#undef SET_META
1950}
1951
1952void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
1953 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
1954{
1955 gint i;
1956
1957 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
1958 {
1959 GstAmlV4l2VideoDecCData *cdata;
1960 GstStructure *s;
1961 GTypeQuery type_query;
1962 GTypeInfo type_info = {
1963 0,
1964 };
1965 GType type, subtype;
1966 gchar *type_name;
1967
1968 s = gst_caps_get_structure(sink_caps, i);
1969
1970 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
1971 cdata->device = g_strdup(device_path);
1972 cdata->sink_caps = gst_caps_new_empty();
1973 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1974 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1975 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1976 cdata->src_caps = gst_caps_copy(src_caps);
1977 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1978 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
1979 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
1980
1981 /* Skip over if we hit an unmapped type */
1982 if (!type_name)
1983 {
1984 g_free(cdata);
1985 continue;
1986 }
1987
1988 type = gst_aml_v4l2_video_dec_get_type();
1989 g_type_query(type, &type_query);
1990 memset(&type_info, 0, sizeof(type_info));
1991 type_info.class_size = type_query.class_size;
1992 type_info.instance_size = type_query.instance_size;
1993 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
1994 type_info.class_data = cdata;
1995 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
1996
1997 subtype = g_type_register_static(type, type_name, &type_info, 0);
1998 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
1999 subtype))
2000 GST_WARNING("Failed to register plugin '%s'", type_name);
2001
2002 g_free(type_name);
2003 }
2004}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002005
2006#if GST_IMPORT_LGE_PROP
2007static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2008{
2009 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2010 g_param_spec_object("resource-info", "resource-info",
2011 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2012 GST_TYPE_STRUCTURE,
2013 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2014
2015 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2016 g_param_spec_uint64("decoded-size", "decoded-size",
2017 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2018 0, G_MAXUINT64,
2019 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2020
2021 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2022 g_param_spec_uint64("undecoded-size", "undecoded-size",
2023 "video decoder element's total undecoded data update unit is byte.",
2024 0, G_MAXUINT64,
2025 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2026
2027 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2028 g_param_spec_string("app-type", "app-type",
2029 "set application type.",
2030 "default_app",
2031 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2032
2033 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2034 g_param_spec_boolean("clip-mode", "clip-mode",
2035 "When seeking, Content is moving faster for a while to skip frames.",
2036 FALSE,
2037 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2038}
2039#endif