blob: 31887f8647a46f45fa5a60b90a465154fa5a275d [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 { \
xuesong.jiang61ea8012022-05-12 15:38:17 +080044 GST_INFO("aml v4l2 dec locking"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080045 g_rec_mutex_lock(&GST_VIDEO_DECODER(decoder)->stream_lock); \
xuesong.jiang61ea8012022-05-12 15:38:17 +080046 GST_INFO("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 { \
xuesong.jiang252f6862022-05-07 14:53:41 +080054 GST_INFO("aml v4l2 dec unlocking"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080055 g_rec_mutex_unlock(&GST_VIDEO_DECODER(decoder)->stream_lock); \
xuesong.jiang61ea8012022-05-12 15:38:17 +080056 GST_INFO("aml v4l2 dec unlocked"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080057 }
58#endif
xuesong.jiang61ea8012022-05-12 15:38:17 +080059
60#if GST_IMPORT_LGE_PROP
61typedef struct _GstAmlResourceInfo
62{
63 gchar *coretype;
64 gint videoport;
65 gint audioport;
66 gint maxwidth;
67 gint maxheight;
68 gint mixerport;
69} GstAmlResourceInfo;
70
71struct _GstAmlV4l2VideoDecLgeCtxt
72{
73 GstAmlResourceInfo res_info;
74 guint64 dec_size;
75 guint64 undec_size;
76 gchar *app_type;
77 gboolean clip_mode;
78};
79#endif
80
xuesong.jiangae1548e2022-05-06 16:38:46 +080081typedef struct
82{
83 gchar *device;
84 GstCaps *sink_caps;
85 GstCaps *src_caps;
86 const gchar *longname;
87 const gchar *description;
88} GstAmlV4l2VideoDecCData;
89
90enum
91{
92 PROP_0,
xuesong.jiang61ea8012022-05-12 15:38:17 +080093 V4L2_STD_OBJECT_PROPS,
94#if GST_IMPORT_LGE_PROP
95 LGE_RESOURCE_INFO,
96 LGE_DECODE_SIZE,
97 LGE_UNDECODE_SIZE,
98 LGE_APP_TYPE,
99 LGE_CLIP_MODE
100#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800101};
102
103#define gst_aml_v4l2_video_dec_parent_class parent_class
104G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec,
105 GST_TYPE_VIDEO_DECODER);
106
107static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder);
xuesong.jiang61ea8012022-05-12 15:38:17 +0800108#if GST_IMPORT_LGE_PROP
109static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class);
110#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800111
112static void
113gst_aml_v4l2_video_dec_set_property(GObject *object,
114 guint prop_id, const GValue *value, GParamSpec *pspec)
115{
116 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
117
118 switch (prop_id)
119 {
120 case PROP_CAPTURE_IO_MODE:
121 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
122 prop_id, value, pspec))
123 {
124 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
125 }
126 break;
127 case PROP_DUMP_FRAME_LOCATION:
128 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
129 prop_id, value, pspec))
130 {
131 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
132 }
133 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800134#if GST_IMPORT_LGE_PROP
135 case LGE_RESOURCE_INFO:
136 {
137 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
138 GstStructure *r_info = g_value_get_object(value);
139 if (r_info)
140 {
141 if (gst_structure_has_field(r_info, "coretype"))
142 {
143 if (self->lge_ctxt->res_info.coretype)
144 g_free(self->lge_ctxt->res_info.coretype);
145 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
146 }
147 if (gst_structure_has_field(r_info, "videoport"))
148 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
149 if (gst_structure_has_field(r_info, "audioport"))
150 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
151 if (gst_structure_has_field(r_info, "maxwidth"))
152 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
153 if (gst_structure_has_field(r_info, "maxheight"))
154 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
155 if (gst_structure_has_field(r_info, "mixerport"))
156 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
157 }
158 break;
159 }
160 case LGE_APP_TYPE:
161 {
162 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
163 if (self->lge_ctxt->app_type)
164 g_free(self->lge_ctxt->app_type);
165 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
166 break;
167 }
168 case LGE_CLIP_MODE:
169 {
170 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
171 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
172 break;
173 }
174#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800175 /* By default, only set on output */
176 default:
177 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output,
178 prop_id, value, pspec))
179 {
180 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
181 }
182 break;
183 }
184}
185
186static void
187gst_aml_v4l2_video_dec_get_property(GObject *object,
188 guint prop_id, GValue *value, GParamSpec *pspec)
189{
190 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
191
192 switch (prop_id)
193 {
194 case PROP_CAPTURE_IO_MODE:
195 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
196 prop_id, value, pspec))
197 {
198 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
199 }
200 break;
201
xuesong.jiang61ea8012022-05-12 15:38:17 +0800202#if GST_IMPORT_LGE_PROP
203 case LGE_DECODE_SIZE:
204 {
205 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
206 self->lge_ctxt->dec_size = -1;
207 g_value_set_int(value, self->lge_ctxt->dec_size);
208 break;
209 }
210 case LGE_UNDECODE_SIZE:
211 {
212 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
213 self->lge_ctxt->undec_size = -1;
214 g_value_set_int(value, self->lge_ctxt->undec_size);
215 break;
216 }
217#endif
218
xuesong.jiangae1548e2022-05-06 16:38:46 +0800219 /* By default read from output */
220 default:
221 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output,
222 prop_id, value, pspec))
223 {
224 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
225 }
226 break;
227 }
228}
229
230static gboolean
231gst_aml_v4l2_video_dec_open(GstVideoDecoder *decoder)
232{
233 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
234 GstCaps *codec_caps;
235
236 GST_DEBUG_OBJECT(self, "Opening");
237
238 if (!gst_aml_v4l2_object_open(self->v4l2output))
239 goto failure;
240
241 if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output))
242 goto failure;
243
244 codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad);
245 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output,
246 codec_caps);
247 gst_caps_unref(codec_caps);
248
249 if (gst_caps_is_empty(self->probed_sinkcaps))
250 goto no_encoded_format;
251
252 return TRUE;
253
254no_encoded_format:
255 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
256 (_("Decoder on device %s has no supported input format"),
257 self->v4l2output->videodev),
258 (NULL));
259 goto failure;
260
261failure:
262 if (GST_AML_V4L2_IS_OPEN(self->v4l2output))
263 gst_aml_v4l2_object_close(self->v4l2output);
264
265 if (GST_AML_V4L2_IS_OPEN(self->v4l2capture))
266 gst_aml_v4l2_object_close(self->v4l2capture);
267
268 gst_caps_replace(&self->probed_srccaps, NULL);
269 gst_caps_replace(&self->probed_sinkcaps, NULL);
270
271 return FALSE;
272}
273
274static gboolean
275gst_aml_v4l2_video_dec_close(GstVideoDecoder *decoder)
276{
277 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
278
279 GST_DEBUG_OBJECT(self, "Closing");
280
281 gst_aml_v4l2_object_close(self->v4l2output);
282 gst_aml_v4l2_object_close(self->v4l2capture);
283 gst_caps_replace(&self->probed_srccaps, NULL);
284 gst_caps_replace(&self->probed_sinkcaps, NULL);
285
286 return TRUE;
287}
288
289static gboolean
290gst_aml_v4l2_video_dec_start(GstVideoDecoder *decoder)
291{
292 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
293
294 GST_DEBUG_OBJECT(self, "Starting");
295
296 gst_aml_v4l2_object_unlock(self->v4l2output);
297 g_atomic_int_set(&self->active, TRUE);
298 self->output_flow = GST_FLOW_OK;
299
300 return TRUE;
301}
302
303static gboolean
304gst_aml_v4l2_video_dec_stop(GstVideoDecoder *decoder)
305{
306 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
307
308 GST_DEBUG_OBJECT(self, "Stopping");
309
310 gst_aml_v4l2_object_unlock(self->v4l2output);
311 gst_aml_v4l2_object_unlock(self->v4l2capture);
312
313 /* Wait for capture thread to stop */
314 gst_pad_stop_task(decoder->srcpad);
315
316 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
317 self->output_flow = GST_FLOW_OK;
318 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
319
320 /* Should have been flushed already */
321 g_assert(g_atomic_int_get(&self->active) == FALSE);
322
323 gst_aml_v4l2_object_stop(self->v4l2output);
324 gst_aml_v4l2_object_stop(self->v4l2capture);
325
326 if (self->input_state)
327 {
328 gst_video_codec_state_unref(self->input_state);
329 self->input_state = NULL;
330 }
331
332 GST_DEBUG_OBJECT(self, "Stopped");
333
334 return TRUE;
335}
336
337static gboolean
338gst_aml_v4l2_video_dec_set_format(GstVideoDecoder *decoder,
339 GstVideoCodecState *state)
340{
341 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
342 gboolean ret = TRUE;
343 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
344 GstCaps *caps;
345
346 GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps);
347 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
348
349 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
350 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
351
352 if (self->input_state)
353 {
354 if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps))
355 {
356 GST_DEBUG_OBJECT(self, "Compatible caps");
357 goto done;
358 }
359 gst_video_codec_state_unref(self->input_state);
360 self->input_state = NULL;
361
362 gst_aml_v4l2_video_dec_finish(decoder);
363 gst_aml_v4l2_object_stop(self->v4l2output);
364
365 /* The renegotiation flow don't blend with the base class flow. To properly
366 * stop the capture pool, if the buffers can't be orphaned, we need to
367 * reclaim our buffers, which will happend through the allocation query.
368 * The allocation query is triggered by gst_video_decoder_negotiate() which
369 * requires the output caps to be set, but we can't know this information
370 * as we rely on the decoder, which requires the capture queue to be
371 * stopped.
372 *
373 * To workaround this issue, we simply run an allocation query with the
374 * old negotiated caps in order to drain/reclaim our buffers. That breaks
375 * the complexity and should not have much impact in performance since the
376 * following allocation query will happen on a drained pipeline and won't
377 * block. */
378 if (self->v4l2capture->pool &&
379 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
380 {
381 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
382 if (caps)
383 {
384 GstQuery *query = gst_query_new_allocation(caps, FALSE);
385 gst_pad_peer_query(decoder->srcpad, query);
386 gst_query_unref(query);
387 gst_caps_unref(caps);
388 }
389 }
390
391 gst_aml_v4l2_object_stop(self->v4l2capture);
392 self->output_flow = GST_FLOW_OK;
393 }
394
395 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
396 {
397 GST_ERROR_OBJECT(self, "config output drm mode error");
398 goto done;
399 }
400
401 ret = gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error);
402
403 gst_caps_replace(&self->probed_srccaps, NULL);
404 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
405 gst_aml_v4l2_object_get_raw_caps());
406
407 if (gst_caps_is_empty(self->probed_srccaps))
408 goto no_raw_format;
409
410 caps = gst_caps_copy(self->probed_srccaps);
411 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
412 gst_caps_append(self->probed_srccaps, caps);
413 if (ret)
414 self->input_state = gst_video_codec_state_ref(state);
415 else
416 gst_aml_v4l2_error(self, &error);
417
418done:
419 return ret;
420
421no_raw_format:
422 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
423 (_("Decoder on device %s has no supported output format"),
424 self->v4l2output->videodev),
425 (NULL));
426 return GST_FLOW_ERROR;
427}
428
429static gboolean
430gst_aml_v4l2_video_dec_flush(GstVideoDecoder *decoder)
431{
432 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
433
434 GST_DEBUG_OBJECT(self, "Flushed");
435
436 /* Ensure the processing thread has stopped for the reverse playback
437 * discount case */
438 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
439 {
440 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
441
442 gst_aml_v4l2_object_unlock(self->v4l2output);
443 gst_aml_v4l2_object_unlock(self->v4l2capture);
444 gst_pad_stop_task(decoder->srcpad);
445 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
446 }
447
448 self->output_flow = GST_FLOW_OK;
449
450 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
451 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
452
453 if (self->v4l2output->pool)
454 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
455
456 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
457 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
458 * pool. */
459 if (self->v4l2capture->pool)
460 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
461
462 return TRUE;
463}
464
465static gboolean
466gst_aml_v4l2_video_dec_negotiate(GstVideoDecoder *decoder)
467{
468 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
469
xuesong.jiang681d3602022-06-24 21:23:35 +0800470 if (TRUE == self->v4l2output->is_svp)
471 {
472 GstStructure *s;
473 GstEvent *event;
474
475 s = gst_structure_new_empty ("IS_SVP");
476 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
477 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
478 gst_pad_push_event (decoder->srcpad, event);
479 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
480 }
481
xuesong.jiangae1548e2022-05-06 16:38:46 +0800482 /* We don't allow renegotiation without carefull disabling the pool */
483 if (self->v4l2capture->pool &&
484 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
485 return TRUE;
486
487 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
488}
489
490static gboolean
491gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
492{
493 struct v4l2_decoder_cmd dcmd = {
494 0,
495 };
496
497 GST_DEBUG_OBJECT(v4l2object->element,
498 "sending v4l2 decoder command %u with flags %u", cmd, flags);
499
500 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
501 return FALSE;
502
503 dcmd.cmd = cmd;
504 dcmd.flags = flags;
505 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
506 goto dcmd_failed;
507
508 return TRUE;
509
510dcmd_failed:
511 if (errno == ENOTTY)
512 {
513 GST_INFO_OBJECT(v4l2object->element,
514 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
515 cmd, flags, v4l2object->videodev, g_strerror(errno));
516 }
517 else
518 {
519 GST_ERROR_OBJECT(v4l2object->element,
520 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
521 cmd, flags, v4l2object->videodev, g_strerror(errno));
522 }
523 return FALSE;
524}
525
526static GstFlowReturn
527gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
528{
529 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
530 GstFlowReturn ret = GST_FLOW_OK;
531 GstBuffer *buffer;
532
533 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
534 goto done;
535
536 GST_DEBUG_OBJECT(self, "Finishing decoding");
537
538 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
539
540 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
541 {
542 GstTask *task = decoder->srcpad->task;
543
544 /* If the decoder stop command succeeded, just wait until processing is
545 * finished */
546 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
547 GST_OBJECT_LOCK(task);
548 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
549 GST_TASK_WAIT(task);
550 GST_OBJECT_UNLOCK(task);
551 ret = GST_FLOW_FLUSHING;
552 }
553 else
554 {
555 /* otherwise keep queuing empty buffers until the processing thread has
556 * stopped, _pool_process() will return FLUSHING when that happened */
557 while (ret == GST_FLOW_OK)
558 {
559 buffer = gst_buffer_new();
560 ret =
561 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
562 gst_buffer_unref(buffer);
563 }
564 }
565
566 /* and ensure the processing thread has stopped in case another error
567 * occured. */
568 gst_aml_v4l2_object_unlock(self->v4l2capture);
569 gst_pad_stop_task(decoder->srcpad);
570 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
571
572 if (ret == GST_FLOW_FLUSHING)
573 ret = self->output_flow;
574
575 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
576
577 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
578
579done:
580 return ret;
581}
582
583static GstFlowReturn
584gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
585{
586 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
587
588 GST_DEBUG_OBJECT(self, "Draining...");
589 gst_aml_v4l2_video_dec_finish(decoder);
590 gst_aml_v4l2_video_dec_flush(decoder);
591
592 return GST_FLOW_OK;
593}
594
595static GstVideoCodecFrame *
596gst_aml_v4l2_video_dec_get_oldest_frame(GstVideoDecoder *decoder)
597{
598 GstVideoCodecFrame *frame = NULL;
599 GList *frames, *l;
600 gint count = 0;
601
602 frames = gst_video_decoder_get_frames(decoder);
603
604 for (l = frames; l != NULL; l = l->next)
605 {
606 GstVideoCodecFrame *f = l->data;
607
608 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
609 frame = f;
610
611 count++;
612 }
613
614 if (frame)
615 {
616 GST_LOG_OBJECT(decoder,
617 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
618 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
619 gst_video_codec_frame_ref(frame);
620 }
621
622 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
623
624 return frame;
625}
626
fei.dengbee20862022-06-14 14:59:48 +0800627static GstVideoCodecFrame *
628gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
629{
630 GstVideoCodecFrame *frame = NULL;
631 GList *frames, *l;
632 gint count = 0;
633
634 frames = gst_video_decoder_get_frames(decoder);
635
636 for (l = frames; l != NULL; l = l->next)
637 {
638 GstVideoCodecFrame *f = l->data;
639
640 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABS(f->pts - pts)) < 10) {
641 frame = f;
642 break;
643 } else {
644 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
645 frame = f;
646 }
647
648 count++;
649 }
650
651 if (frame)
652 {
653 GST_LOG_OBJECT(decoder,
654 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
655 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
656 gst_video_codec_frame_ref(frame);
657 }
658
659 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
660
661 return frame;
662}
663
xuesong.jiangae1548e2022-05-06 16:38:46 +0800664static gboolean
665gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
666 GstStructure *structure, gpointer user_data)
667{
668 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
669 GstVideoAlignment *align = &self->v4l2capture->align;
670 GstVideoInfo *info = &self->v4l2capture->info;
671 int width, height;
672
673 if (!gst_structure_get_int(structure, "width", &width))
674 return TRUE;
675
676 if (!gst_structure_get_int(structure, "height", &height))
677 return TRUE;
678
679 if (align->padding_left != 0 || align->padding_top != 0 ||
680 height != info->height + align->padding_bottom)
681 return TRUE;
682
683 if (height == info->height + align->padding_bottom)
684 {
685 /* Some drivers may round up width to the padded with */
686 if (width == info->width + align->padding_right)
687 gst_structure_set(structure,
688 "width", G_TYPE_INT, width - align->padding_right,
689 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
690 /* Some drivers may keep visible width and only round up bytesperline */
691 else if (width == info->width)
692 gst_structure_set(structure,
693 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
694 }
695
696 return TRUE;
697}
698
699static void
sheng.liubcf036c2022-06-21 15:55:42 +0800700gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800701{
702 struct v4l2_event evt;
703 gint ret;
704
705 memset (&evt, 0x00, sizeof (struct v4l2_event));
706 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
707 if (ret < 0)
708 {
709 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
710 return;
711 }
712
713 switch (evt.type)
714 {
715 case V4L2_EVENT_SOURCE_CHANGE:
716 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
717 break;
718 case V4L2_EVENT_EOS:
719 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
720 break;
721 default:
722 break;
723 }
724
725 return;
726}
727
728static void
xuesong.jiangae1548e2022-05-06 16:38:46 +0800729gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
730{
731 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
732 GstAmlV4l2BufferPool *v4l2_pool;
733 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
734 GstBufferPool *pool;
735 GstVideoCodecFrame *frame;
736 GstBuffer *buffer = NULL;
737 GstFlowReturn ret;
738
739 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
740 {
741 GstVideoInfo info;
742 GstVideoCodecState *output_state;
743 GstCaps *acquired_caps, *available_caps, *caps, *filter;
744 GstStructure *st;
745
746 GST_DEBUG_OBJECT(self, "waitting source change event");
747 /* Wait until received SOURCE_CHANGE event to get right video format */
748 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
749 {
750 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
751 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
752 {
753 GST_DEBUG_OBJECT(self, "Received source change event");
754 break;
755 }
756 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
757 {
758 GST_DEBUG_OBJECT(self, "Received eos event");
759 goto beach;
760 }
761 else if (ret != GST_FLOW_OK)
762 {
763 GST_ERROR_OBJECT(self, "dqevent error");
764 goto beach;
765 }
766 }
767 self->v4l2capture->need_wait_event = FALSE;
768
sheng.liu0c77f6c2022-06-17 21:33:20 +0800769 if (TRUE == self->v4l2output->is_svp)
770 {
771 GstPad *peer;
772 GstStructure *s;
773 GstEvent *event;
774
775 peer = gst_pad_get_peer (decoder->srcpad);
776 if (peer)
777 {
778 s = gst_structure_new_empty ("IS_SVP");
779 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
780 gst_pad_send_event (peer, event);
781 GST_DEBUG_OBJECT(self, "Send SVP Event");
782 gst_object_unref (peer);
783 }
784 }
785
sheng.liub56bbc52022-06-21 11:02:33 +0800786 if (self->v4l2capture->need_drop_event)
787 {
788 // drop V4L2_EVENT_SOURCE_CHANGE
789 gst_v4l2_drop_event(self->v4l2capture);
790 self->v4l2capture->need_drop_event = FALSE;
791 }
792
xuesong.jiangae1548e2022-05-06 16:38:46 +0800793 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
794 goto not_negotiated;
795
796 /* Create caps from the acquired format, remove the format field */
797 acquired_caps = gst_video_info_to_caps(&info);
798 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
799 st = gst_caps_get_structure(acquired_caps, 0);
800 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site",
801 NULL);
802
803 /* Probe currently available pixel formats */
804 available_caps = gst_caps_copy(self->probed_srccaps);
805 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
806
807 /* Replace coded size with visible size, we want to negotiate visible size
808 * with downstream, not coded size. */
809 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
810
811 filter = gst_caps_intersect_full(available_caps, acquired_caps,
812 GST_CAPS_INTERSECT_FIRST);
813 caps = gst_caps_copy(filter);
814 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
815 gst_caps_append(filter, caps);
816
817 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
818 gst_caps_unref(acquired_caps);
819 gst_caps_unref(available_caps);
820 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
821 gst_caps_unref(filter);
822
823 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
824 if (gst_caps_is_empty(caps))
825 {
826 gst_caps_unref(caps);
827 goto not_negotiated;
828 }
829
830 /* Fixate pixel format */
831 caps = gst_caps_fixate(caps);
832
833 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
834
835 /* Try to set negotiated format, on success replace acquired format */
836 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
837 gst_video_info_from_caps(&info, caps);
838 else
839 gst_aml_v4l2_clear_error(&error);
840 gst_caps_unref(caps);
841
842 output_state = gst_video_decoder_set_output_state(decoder,
843 info.finfo->format, info.width, info.height, self->input_state);
844
845 /* Copy the rest of the information, there might be more in the future */
846 output_state->info.interlace_mode = info.interlace_mode;
847 gst_video_codec_state_unref(output_state);
848
849 if (!gst_video_decoder_negotiate(decoder))
850 {
851 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
852 goto flushing;
853 else
854 goto not_negotiated;
855 }
856
857 /* Ensure our internal pool is activated */
858 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
859 TRUE))
860 goto activate_failed;
861 }
862
863 GST_LOG_OBJECT(decoder, "Allocate output buffer");
864
865 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
866
867 self->output_flow = GST_FLOW_OK;
868 do
869 {
870 /* We cannot use the base class allotate helper since it taking the internal
871 * stream lock. we know that the acquire may need to poll until more frames
872 * comes in and holding this lock would prevent that.
873 */
874 pool = gst_video_decoder_get_buffer_pool(decoder);
875
876 /* Pool may be NULL if we started going to READY state */
877 if (pool == NULL)
878 {
fei.dengbee20862022-06-14 14:59:48 +0800879 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800880 ret = GST_FLOW_FLUSHING;
881 goto beach;
882 }
883
884 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
885 g_object_unref(pool);
886
sheng.liu4e01a472022-06-21 15:38:25 +0800887 if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER) {
sheng.liubcf036c2022-06-21 15:55:42 +0800888 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
sheng.liub56bbc52022-06-21 11:02:33 +0800889 self->v4l2capture->need_drop_event = TRUE;
890 goto beach;
891 }
892
sheng.liu8d18ed22022-05-26 17:28:15 +0800893 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
894 {
895 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
896 gst_aml_v4l2_object_stop(self->v4l2capture);
897 return;
898 }
899
fei.dengbee20862022-06-14 14:59:48 +0800900 if (ret != GST_FLOW_OK) {
901 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800902 goto beach;
fei.dengbee20862022-06-14 14:59:48 +0800903 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800904
905 GST_LOG_OBJECT(decoder, "Process output buffer");
906 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
907 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
908 {
909 gst_aml_v4l2_object_stop(self->v4l2capture);
910 return;
911 }
912
913 } while (ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER);
914
915 if (ret != GST_FLOW_OK)
916 goto beach;
917
fei.dengbee20862022-06-14 14:59:48 +0800918 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +0800919
920 if (frame)
921 {
922 frame->output_buffer = buffer;
923 buffer = NULL;
924 ret = gst_video_decoder_finish_frame(decoder, frame);
925
926 if (ret != GST_FLOW_OK)
927 goto beach;
928 }
929 else
930 {
931 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
932 gst_buffer_unref(buffer);
933 }
934
935 return;
936 /* ERRORS */
937not_negotiated:
938{
939 GST_ERROR_OBJECT(self, "not negotiated");
940 ret = GST_FLOW_NOT_NEGOTIATED;
941 goto beach;
942}
943activate_failed:
944{
945 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
946 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
947 (_("Failed to allocate required memory.")),
948 ("Buffer pool activation failed"));
949 ret = GST_FLOW_ERROR;
950 goto beach;
951}
952flushing:
953{
954 ret = GST_FLOW_FLUSHING;
955 goto beach;
956}
957beach:
958 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
959 gst_flow_get_name(ret));
960
961 gst_buffer_replace(&buffer, NULL);
962 self->output_flow = ret;
963 gst_aml_v4l2_object_unlock(self->v4l2output);
964 gst_pad_pause_task(decoder->srcpad);
965}
966
967static GstFlowReturn
968gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
969 GstVideoCodecFrame *frame)
970{
971 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
972 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
973 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
974 GstFlowReturn ret = GST_FLOW_OK;
975 gboolean processed = FALSE;
976 GstBuffer *tmp;
977 GstTaskState task_state;
978 GstCaps *caps;
979
980 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
981
982 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
983 goto flushing;
984
985 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
986 {
987 if (!self->input_state)
988 goto not_negotiated;
989 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
990 &error))
991 goto not_negotiated;
992 }
993
994 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
995 {
996 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +0800997 GstCapsFeatures *features = NULL;
998
999 features = gst_caps_get_features(self->input_state->caps, 0);
1000 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1001 {
1002 GST_DEBUG_OBJECT(self, "Is SVP");
1003 self->v4l2output->is_svp = TRUE;
1004 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001005
1006 GST_DEBUG_OBJECT(self, "Sending header");
1007
1008 codec_data = self->input_state->codec_data;
1009
1010 /* We are running in byte-stream mode, so we don't know the headers, but
1011 * we need to send something, otherwise the decoder will refuse to
1012 * intialize.
1013 */
1014 if (codec_data)
1015 {
1016 gst_buffer_ref(codec_data);
1017 }
1018 else
1019 {
1020 codec_data = gst_buffer_ref(frame->input_buffer);
1021 processed = TRUE;
1022 }
1023
1024 /* Ensure input internal pool is active */
1025 if (!gst_buffer_pool_is_active(pool))
1026 {
1027 GstStructure *config = gst_buffer_pool_get_config(pool);
1028 guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1029 guint max = VIDEO_MAX_FRAME;
1030
1031 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1032 // self->v4l2output->info.size, min, max);
1033 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1034
1035 /* There is no reason to refuse this config */
1036 if (!gst_buffer_pool_set_config(pool, config))
1037 goto activate_failed;
1038
1039 if (!gst_buffer_pool_set_active(pool, TRUE))
1040 goto activate_failed;
1041 }
1042
1043 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1044 ret =
1045 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
1046 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1047
1048 gst_buffer_unref(codec_data);
1049
1050 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1051 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1052 * and returns the visible size as with/height and the coded size as
1053 * padding. */
1054 }
1055
1056 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1057 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1058 {
1059 /* It's possible that the processing thread stopped due to an error */
1060 if (self->output_flow != GST_FLOW_OK &&
1061 self->output_flow != GST_FLOW_FLUSHING)
1062 {
1063 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1064 ret = self->output_flow;
1065 goto drop;
1066 }
1067
1068 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1069
1070 /* Start the processing task, when it quits, the task will disable input
1071 * processing to unlock input if draining, or prevent potential block */
1072 self->output_flow = GST_FLOW_FLUSHING;
1073 if (!gst_pad_start_task(decoder->srcpad,
1074 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1075 goto start_task_failed;
1076 }
1077
1078 if (!processed)
1079 {
1080 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1081 ret =
1082 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1083 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1084
1085 if (ret == GST_FLOW_FLUSHING)
1086 {
1087 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1088 GST_TASK_STARTED)
1089 ret = self->output_flow;
1090 goto drop;
1091 }
1092 else if (ret != GST_FLOW_OK)
1093 {
1094 goto process_failed;
1095 }
1096 }
1097
1098 /* No need to keep input arround */
1099 tmp = frame->input_buffer;
1100 frame->input_buffer = gst_buffer_new();
1101 gst_buffer_copy_into(frame->input_buffer, tmp,
1102 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1103 GST_BUFFER_COPY_META,
1104 0, 0);
1105 gst_buffer_unref(tmp);
1106
1107 gst_video_codec_frame_unref(frame);
1108 return ret;
1109
1110 /* ERRORS */
1111not_negotiated:
1112{
1113 GST_ERROR_OBJECT(self, "not negotiated");
1114 ret = GST_FLOW_NOT_NEGOTIATED;
1115 gst_aml_v4l2_error(self, &error);
1116 goto drop;
1117}
1118activate_failed:
1119{
1120 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1121 (_("Failed to allocate required memory.")),
1122 ("Buffer pool activation failed"));
1123 ret = GST_FLOW_ERROR;
1124 goto drop;
1125}
1126flushing:
1127{
1128 ret = GST_FLOW_FLUSHING;
1129 goto drop;
1130}
1131
1132start_task_failed:
1133{
1134 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1135 (_("Failed to start decoding thread.")), (NULL));
1136 ret = GST_FLOW_ERROR;
1137 goto drop;
1138}
1139process_failed:
1140{
1141 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1142 (_("Failed to process frame.")),
1143 ("Maybe be due to not enough memory or failing driver"));
1144 ret = GST_FLOW_ERROR;
1145 goto drop;
1146}
1147drop:
1148{
1149 gst_video_decoder_drop_frame(decoder, frame);
1150 return ret;
1151}
1152}
1153
1154static gboolean
1155gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1156 GstQuery *query)
1157{
1158 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1159 GstClockTime latency;
1160 gboolean ret = FALSE;
1161
1162 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1163 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1164
1165 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1166 {
1167 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1168 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1169 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1170 gst_video_decoder_set_latency(decoder, latency, latency);
1171 }
1172 else
1173 {
1174 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1175 }
1176
1177 return ret;
1178}
1179
1180static gboolean
1181gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1182{
1183 gboolean ret = TRUE;
1184 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1185
1186 switch (GST_QUERY_TYPE(query))
1187 {
1188 case GST_QUERY_CAPS:
1189 {
1190 GstCaps *filter, *result = NULL;
1191 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1192
1193 gst_query_parse_caps(query, &filter);
1194
1195 if (self->probed_srccaps)
1196 result = gst_caps_ref(self->probed_srccaps);
1197 else
1198 result = gst_pad_get_pad_template_caps(pad);
1199
1200 if (filter)
1201 {
1202 GstCaps *tmp = result;
1203 result =
1204 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1205 gst_caps_unref(tmp);
1206 }
1207
1208 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1209
1210 gst_query_set_caps_result(query, result);
1211 gst_caps_unref(result);
1212 break;
1213 }
1214
1215 default:
1216 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1217 break;
1218 }
1219
1220 return ret;
1221}
1222
1223static GstCaps *
1224gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1225{
1226 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1227 GstCaps *result;
1228
1229 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1230 filter);
1231
1232 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1233
1234 return result;
1235}
1236
1237static gboolean
1238gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1239{
1240 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1241 gboolean ret;
1242 GstEventType type = GST_EVENT_TYPE(event);
1243
1244 switch (type)
1245 {
1246 case GST_EVENT_FLUSH_START:
1247 GST_DEBUG_OBJECT(self, "flush start");
1248 gst_aml_v4l2_object_unlock(self->v4l2output);
1249 gst_aml_v4l2_object_unlock(self->v4l2capture);
1250 break;
1251 default:
1252 break;
1253 }
1254
1255 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1256
1257 switch (type)
1258 {
1259 case GST_EVENT_FLUSH_START:
1260 /* The processing thread should stop now, wait for it */
1261 gst_pad_stop_task(decoder->srcpad);
1262 GST_DEBUG_OBJECT(self, "flush start done");
1263 break;
1264 default:
1265 break;
1266 }
1267
1268 return ret;
1269}
1270
1271static GstStateChangeReturn
1272gst_aml_v4l2_video_dec_change_state(GstElement *element,
1273 GstStateChange transition)
1274{
1275 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1276 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1277
1278 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1279 {
1280 g_atomic_int_set(&self->active, FALSE);
1281 gst_aml_v4l2_object_unlock(self->v4l2output);
1282 gst_aml_v4l2_object_unlock(self->v4l2capture);
1283 gst_pad_stop_task(decoder->srcpad);
1284 }
1285
1286 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1287}
1288
1289static void
1290gst_aml_v4l2_video_dec_dispose(GObject *object)
1291{
1292 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1293
1294 gst_caps_replace(&self->probed_sinkcaps, NULL);
1295 gst_caps_replace(&self->probed_srccaps, NULL);
1296
1297 G_OBJECT_CLASS(parent_class)->dispose(object);
1298}
1299
1300static void
1301gst_aml_v4l2_video_dec_finalize(GObject *object)
1302{
1303 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1304
1305 gst_aml_v4l2_object_destroy(self->v4l2capture);
1306 gst_aml_v4l2_object_destroy(self->v4l2output);
1307
xuesong.jiang61ea8012022-05-12 15:38:17 +08001308#if GST_IMPORT_LGE_PROP
1309 if (self->lge_ctxt)
1310 {
1311 if (self->lge_ctxt->app_type)
1312 g_free(self->lge_ctxt->app_type);
1313 if (self->lge_ctxt->res_info.coretype)
1314 g_free(self->lge_ctxt->res_info.coretype);
1315 free(self->lge_ctxt);
1316 }
1317
1318#endif
1319
xuesong.jiangae1548e2022-05-06 16:38:46 +08001320 G_OBJECT_CLASS(parent_class)->finalize(object);
1321}
1322
1323static void
1324gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1325{
1326 /* V4L2 object are created in subinstance_init */
1327 self->is_secure_path = FALSE;
xuesong.jiang61ea8012022-05-12 15:38:17 +08001328#if GST_IMPORT_LGE_PROP
1329 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1330 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1331#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001332}
1333
1334static void
1335gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1336{
1337 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1338 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1339 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1340
1341 gst_video_decoder_set_packetized(decoder, TRUE);
1342
1343 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1344 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1345 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1346 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1347 self->v4l2output->no_initial_format = TRUE;
1348 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001349 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001350
1351 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1352 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1353 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1354 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1355 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001356 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001357}
1358
1359static void
1360gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1361{
1362 GstElementClass *element_class;
1363 GObjectClass *gobject_class;
1364 GstVideoDecoderClass *video_decoder_class;
1365
1366 parent_class = g_type_class_peek_parent(klass);
1367
1368 element_class = (GstElementClass *)klass;
1369 gobject_class = (GObjectClass *)klass;
1370 video_decoder_class = (GstVideoDecoderClass *)klass;
1371
1372 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1373 "AML V4L2 Video Decoder");
1374
1375 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1376 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1377 gobject_class->set_property =
1378 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1379 gobject_class->get_property =
1380 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1381
1382 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1383 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1384 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1385 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1386 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1387 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1388 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1389 video_decoder_class->set_format =
1390 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1391 video_decoder_class->negotiate =
1392 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1393 video_decoder_class->decide_allocation =
1394 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1395 /* FIXME propose_allocation or not ? */
1396 video_decoder_class->handle_frame =
1397 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1398 video_decoder_class->getcaps =
1399 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1400 video_decoder_class->src_query =
1401 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1402 video_decoder_class->sink_event =
1403 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1404
1405 element_class->change_state =
1406 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1407
1408 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001409#if GST_IMPORT_LGE_PROP
1410 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1411#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001412}
1413
1414static void
1415gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1416{
1417 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1418 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1419 GstAmlV4l2VideoDecCData *cdata = data;
1420
1421 klass->default_device = cdata->device;
1422
1423 /* Note: gst_pad_template_new() take the floating ref from the caps */
1424 gst_element_class_add_pad_template(element_class,
1425 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1426 cdata->sink_caps));
1427 gst_element_class_add_pad_template(element_class,
1428 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1429 cdata->src_caps));
1430
1431 gst_element_class_set_metadata(element_class, cdata->longname,
1432 "Codec/Decoder/Video/Hardware", cdata->description,
1433 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1434
1435 gst_caps_unref(cdata->sink_caps);
1436 gst_caps_unref(cdata->src_caps);
1437 g_free(cdata);
1438}
1439
1440/* Probing functions */
1441gboolean
1442gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1443{
1444 gboolean ret = FALSE;
1445
1446 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()))
1447 ret = TRUE;
1448
1449 return ret;
1450}
1451
1452static gchar *
1453gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1454 const gchar *basename)
1455{
1456 gchar *codec_name = NULL;
1457 gchar *type_name = NULL;
1458 gboolean got_value = FALSE;
1459
1460#define SET_META(codec) \
1461 G_STMT_START \
1462 { \
1463 cdata->longname = "AML V4L2 " codec " Decoder"; \
1464 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1465 codec_name = g_ascii_strdown(codec, -1); \
1466 } \
1467 G_STMT_END
1468
1469 if (gst_structure_has_name(s, "image/jpeg"))
1470 {
1471 SET_META("JPEG");
1472 }
1473 else if (gst_structure_has_name(s, "video/mpeg"))
1474 {
1475 gint mpegversion = 0;
1476 gint *list = NULL;
1477 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1478 if (FALSE == got_value)
1479 {
1480 got_value = gst_structure_get_list(s, "mpegversion", &list);
1481 if (TRUE == got_value && (1 == *list || 2 == *list))
1482 {
1483 SET_META("MPEG2");
1484 }
1485 else
1486 {
1487 SET_META("MPEG4");
1488 }
1489 }
1490 else
1491 {
1492 SET_META("MPEG4");
1493 }
1494 }
1495 else if (gst_structure_has_name(s, "video/x-h263"))
1496 {
1497 SET_META("H263");
1498 }
1499 else if (gst_structure_has_name(s, "video/x-fwht"))
1500 {
1501 SET_META("FWHT");
1502 }
1503 else if (gst_structure_has_name(s, "video/x-h264"))
1504 {
1505 SET_META("H264");
1506 }
1507 else if (gst_structure_has_name(s, "video/x-h265"))
1508 {
1509 SET_META("H265");
1510 }
1511 else if (gst_structure_has_name(s, "video/x-wmv"))
1512 {
1513 SET_META("VC1");
1514 }
1515 else if (gst_structure_has_name(s, "video/x-vp8"))
1516 {
1517 SET_META("VP8");
1518 }
1519 else if (gst_structure_has_name(s, "video/x-vp9"))
1520 {
1521 SET_META("VP9");
1522 }
1523 else if (gst_structure_has_name(s, "video/x-av1"))
1524 {
1525 SET_META("AV1");
1526 }
1527 else if (gst_structure_has_name(s, "video/x-bayer"))
1528 {
1529 SET_META("BAYER");
1530 }
1531 else if (gst_structure_has_name(s, "video/x-sonix"))
1532 {
1533 SET_META("SONIX");
1534 }
1535 else if (gst_structure_has_name(s, "video/x-pwc1"))
1536 {
1537 SET_META("PWC1");
1538 }
1539 else if (gst_structure_has_name(s, "video/x-pwc2"))
1540 {
1541 SET_META("PWC2");
1542 }
1543 else
1544 {
1545 /* This code should be kept on sync with the exposed CODEC type of format
1546 * from gstamlv4l2object.c. This warning will only occure in case we forget
1547 * to also add a format here. */
1548 gchar *s_str = gst_structure_to_string(s);
1549 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
1550 "bug, please report at https://bugs.gnome.org",
1551 s_str);
1552 g_free(s_str);
1553 }
1554
1555 if (codec_name)
1556 {
1557 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
1558 if (g_type_from_name(type_name) != 0)
1559 {
1560 g_free(type_name);
1561 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
1562 }
1563
1564 g_free(codec_name);
1565 }
1566
1567 return type_name;
1568#undef SET_META
1569}
1570
1571void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
1572 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
1573{
1574 gint i;
1575
1576 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
1577 {
1578 GstAmlV4l2VideoDecCData *cdata;
1579 GstStructure *s;
1580 GTypeQuery type_query;
1581 GTypeInfo type_info = {
1582 0,
1583 };
1584 GType type, subtype;
1585 gchar *type_name;
1586
1587 s = gst_caps_get_structure(sink_caps, i);
1588
1589 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
1590 cdata->device = g_strdup(device_path);
1591 cdata->sink_caps = gst_caps_new_empty();
1592 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1593 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1594 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1595 cdata->src_caps = gst_caps_copy(src_caps);
1596 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1597 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
1598 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
1599
1600 /* Skip over if we hit an unmapped type */
1601 if (!type_name)
1602 {
1603 g_free(cdata);
1604 continue;
1605 }
1606
1607 type = gst_aml_v4l2_video_dec_get_type();
1608 g_type_query(type, &type_query);
1609 memset(&type_info, 0, sizeof(type_info));
1610 type_info.class_size = type_query.class_size;
1611 type_info.instance_size = type_query.instance_size;
1612 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
1613 type_info.class_data = cdata;
1614 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
1615
1616 subtype = g_type_register_static(type, type_name, &type_info, 0);
1617 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
1618 subtype))
1619 GST_WARNING("Failed to register plugin '%s'", type_name);
1620
1621 g_free(type_name);
1622 }
1623}
xuesong.jiang61ea8012022-05-12 15:38:17 +08001624
1625#if GST_IMPORT_LGE_PROP
1626static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
1627{
1628 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
1629 g_param_spec_object("resource-info", "resource-info",
1630 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
1631 GST_TYPE_STRUCTURE,
1632 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
1633
1634 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
1635 g_param_spec_uint64("decoded-size", "decoded-size",
1636 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
1637 0, G_MAXUINT64,
1638 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1639
1640 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
1641 g_param_spec_uint64("undecoded-size", "undecoded-size",
1642 "video decoder element's total undecoded data update unit is byte.",
1643 0, G_MAXUINT64,
1644 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1645
1646 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
1647 g_param_spec_string("app-type", "app-type",
1648 "set application type.",
1649 "default_app",
1650 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1651
1652 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
1653 g_param_spec_boolean("clip-mode", "clip-mode",
1654 "When seeking, Content is moving faster for a while to skip frames.",
1655 FALSE,
1656 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1657}
1658#endif