blob: 3a022b7517d8f99318c2dc033c79a629f9694b18 [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
470 /* We don't allow renegotiation without carefull disabling the pool */
471 if (self->v4l2capture->pool &&
472 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
473 return TRUE;
474
475 return GST_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
476}
477
478static gboolean
479gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
480{
481 struct v4l2_decoder_cmd dcmd = {
482 0,
483 };
484
485 GST_DEBUG_OBJECT(v4l2object->element,
486 "sending v4l2 decoder command %u with flags %u", cmd, flags);
487
488 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
489 return FALSE;
490
491 dcmd.cmd = cmd;
492 dcmd.flags = flags;
493 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
494 goto dcmd_failed;
495
496 return TRUE;
497
498dcmd_failed:
499 if (errno == ENOTTY)
500 {
501 GST_INFO_OBJECT(v4l2object->element,
502 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
503 cmd, flags, v4l2object->videodev, g_strerror(errno));
504 }
505 else
506 {
507 GST_ERROR_OBJECT(v4l2object->element,
508 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
509 cmd, flags, v4l2object->videodev, g_strerror(errno));
510 }
511 return FALSE;
512}
513
514static GstFlowReturn
515gst_aml_v4l2_video_dec_finish(GstVideoDecoder *decoder)
516{
517 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
518 GstFlowReturn ret = GST_FLOW_OK;
519 GstBuffer *buffer;
520
521 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
522 goto done;
523
524 GST_DEBUG_OBJECT(self, "Finishing decoding");
525
526 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
527
528 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
529 {
530 GstTask *task = decoder->srcpad->task;
531
532 /* If the decoder stop command succeeded, just wait until processing is
533 * finished */
534 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
535 GST_OBJECT_LOCK(task);
536 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
537 GST_TASK_WAIT(task);
538 GST_OBJECT_UNLOCK(task);
539 ret = GST_FLOW_FLUSHING;
540 }
541 else
542 {
543 /* otherwise keep queuing empty buffers until the processing thread has
544 * stopped, _pool_process() will return FLUSHING when that happened */
545 while (ret == GST_FLOW_OK)
546 {
547 buffer = gst_buffer_new();
548 ret =
549 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
550 gst_buffer_unref(buffer);
551 }
552 }
553
554 /* and ensure the processing thread has stopped in case another error
555 * occured. */
556 gst_aml_v4l2_object_unlock(self->v4l2capture);
557 gst_pad_stop_task(decoder->srcpad);
558 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
559
560 if (ret == GST_FLOW_FLUSHING)
561 ret = self->output_flow;
562
563 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
564
565 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
566
567done:
568 return ret;
569}
570
571static GstFlowReturn
572gst_aml_v4l2_video_dec_drain(GstVideoDecoder *decoder)
573{
574 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
575
576 GST_DEBUG_OBJECT(self, "Draining...");
577 gst_aml_v4l2_video_dec_finish(decoder);
578 gst_aml_v4l2_video_dec_flush(decoder);
579
580 return GST_FLOW_OK;
581}
582
583static GstVideoCodecFrame *
584gst_aml_v4l2_video_dec_get_oldest_frame(GstVideoDecoder *decoder)
585{
586 GstVideoCodecFrame *frame = NULL;
587 GList *frames, *l;
588 gint count = 0;
589
590 frames = gst_video_decoder_get_frames(decoder);
591
592 for (l = frames; l != NULL; l = l->next)
593 {
594 GstVideoCodecFrame *f = l->data;
595
596 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
597 frame = f;
598
599 count++;
600 }
601
602 if (frame)
603 {
604 GST_LOG_OBJECT(decoder,
605 "Oldest frame is %d %" GST_TIME_FORMAT " and %d frames left",
606 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
607 gst_video_codec_frame_ref(frame);
608 }
609
610 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
611
612 return frame;
613}
614
fei.dengbee20862022-06-14 14:59:48 +0800615static GstVideoCodecFrame *
616gst_aml_v4l2_video_dec_get_right_frame(GstVideoDecoder *decoder, GstClockTime pts)
617{
618 GstVideoCodecFrame *frame = NULL;
619 GList *frames, *l;
620 gint count = 0;
621
622 frames = gst_video_decoder_get_frames(decoder);
623
624 for (l = frames; l != NULL; l = l->next)
625 {
626 GstVideoCodecFrame *f = l->data;
627
628 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABS(f->pts - pts)) < 10) {
629 frame = f;
630 break;
631 } else {
632 if (!frame || (GST_CLOCK_TIME_IS_VALID(frame->pts) && GST_CLOCK_TIME_IS_VALID(f->pts) && (frame->pts > f->pts)))
633 frame = f;
634 }
635
636 count++;
637 }
638
639 if (frame)
640 {
641 GST_LOG_OBJECT(decoder,
642 "frame is %d %" GST_TIME_FORMAT " and %d frames left",
643 frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
644 gst_video_codec_frame_ref(frame);
645 }
646
647 g_list_free_full(frames, (GDestroyNotify)gst_video_codec_frame_unref);
648
649 return frame;
650}
651
xuesong.jiangae1548e2022-05-06 16:38:46 +0800652static gboolean
653gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
654 GstStructure *structure, gpointer user_data)
655{
656 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
657 GstVideoAlignment *align = &self->v4l2capture->align;
658 GstVideoInfo *info = &self->v4l2capture->info;
659 int width, height;
660
661 if (!gst_structure_get_int(structure, "width", &width))
662 return TRUE;
663
664 if (!gst_structure_get_int(structure, "height", &height))
665 return TRUE;
666
667 if (align->padding_left != 0 || align->padding_top != 0 ||
668 height != info->height + align->padding_bottom)
669 return TRUE;
670
671 if (height == info->height + align->padding_bottom)
672 {
673 /* Some drivers may round up width to the padded with */
674 if (width == info->width + align->padding_right)
675 gst_structure_set(structure,
676 "width", G_TYPE_INT, width - align->padding_right,
677 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
678 /* Some drivers may keep visible width and only round up bytesperline */
679 else if (width == info->width)
680 gst_structure_set(structure,
681 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
682 }
683
684 return TRUE;
685}
686
687static void
sheng.liub56bbc52022-06-21 11:02:33 +0800688gst_v4l2_drop_event (GstV4l2Object * v4l2object)
689{
690 struct v4l2_event evt;
691 gint ret;
692
693 memset (&evt, 0x00, sizeof (struct v4l2_event));
694 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
695 if (ret < 0)
696 {
697 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
698 return;
699 }
700
701 switch (evt.type)
702 {
703 case V4L2_EVENT_SOURCE_CHANGE:
704 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
705 break;
706 case V4L2_EVENT_EOS:
707 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
708 break;
709 default:
710 break;
711 }
712
713 return;
714}
715
716static void
xuesong.jiangae1548e2022-05-06 16:38:46 +0800717gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
718{
719 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
720 GstAmlV4l2BufferPool *v4l2_pool;
721 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
722 GstBufferPool *pool;
723 GstVideoCodecFrame *frame;
724 GstBuffer *buffer = NULL;
725 GstFlowReturn ret;
726
727 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
728 {
729 GstVideoInfo info;
730 GstVideoCodecState *output_state;
731 GstCaps *acquired_caps, *available_caps, *caps, *filter;
732 GstStructure *st;
733
734 GST_DEBUG_OBJECT(self, "waitting source change event");
735 /* Wait until received SOURCE_CHANGE event to get right video format */
736 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
737 {
738 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
739 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
740 {
741 GST_DEBUG_OBJECT(self, "Received source change event");
742 break;
743 }
744 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
745 {
746 GST_DEBUG_OBJECT(self, "Received eos event");
747 goto beach;
748 }
749 else if (ret != GST_FLOW_OK)
750 {
751 GST_ERROR_OBJECT(self, "dqevent error");
752 goto beach;
753 }
754 }
755 self->v4l2capture->need_wait_event = FALSE;
756
sheng.liu0c77f6c2022-06-17 21:33:20 +0800757 if (TRUE == self->v4l2output->is_svp)
758 {
759 GstPad *peer;
760 GstStructure *s;
761 GstEvent *event;
762
763 peer = gst_pad_get_peer (decoder->srcpad);
764 if (peer)
765 {
766 s = gst_structure_new_empty ("IS_SVP");
767 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
768 gst_pad_send_event (peer, event);
769 GST_DEBUG_OBJECT(self, "Send SVP Event");
770 gst_object_unref (peer);
771 }
772 }
773
sheng.liub56bbc52022-06-21 11:02:33 +0800774 if (self->v4l2capture->need_drop_event)
775 {
776 // drop V4L2_EVENT_SOURCE_CHANGE
777 gst_v4l2_drop_event(self->v4l2capture);
778 self->v4l2capture->need_drop_event = FALSE;
779 }
780
xuesong.jiangae1548e2022-05-06 16:38:46 +0800781 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
782 goto not_negotiated;
783
784 /* Create caps from the acquired format, remove the format field */
785 acquired_caps = gst_video_info_to_caps(&info);
786 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
787 st = gst_caps_get_structure(acquired_caps, 0);
788 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site",
789 NULL);
790
791 /* Probe currently available pixel formats */
792 available_caps = gst_caps_copy(self->probed_srccaps);
793 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
794
795 /* Replace coded size with visible size, we want to negotiate visible size
796 * with downstream, not coded size. */
797 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
798
799 filter = gst_caps_intersect_full(available_caps, acquired_caps,
800 GST_CAPS_INTERSECT_FIRST);
801 caps = gst_caps_copy(filter);
802 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
803 gst_caps_append(filter, caps);
804
805 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
806 gst_caps_unref(acquired_caps);
807 gst_caps_unref(available_caps);
808 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
809 gst_caps_unref(filter);
810
811 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
812 if (gst_caps_is_empty(caps))
813 {
814 gst_caps_unref(caps);
815 goto not_negotiated;
816 }
817
818 /* Fixate pixel format */
819 caps = gst_caps_fixate(caps);
820
821 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
822
823 /* Try to set negotiated format, on success replace acquired format */
824 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
825 gst_video_info_from_caps(&info, caps);
826 else
827 gst_aml_v4l2_clear_error(&error);
828 gst_caps_unref(caps);
829
830 output_state = gst_video_decoder_set_output_state(decoder,
831 info.finfo->format, info.width, info.height, self->input_state);
832
833 /* Copy the rest of the information, there might be more in the future */
834 output_state->info.interlace_mode = info.interlace_mode;
835 gst_video_codec_state_unref(output_state);
836
837 if (!gst_video_decoder_negotiate(decoder))
838 {
839 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
840 goto flushing;
841 else
842 goto not_negotiated;
843 }
844
845 /* Ensure our internal pool is activated */
846 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
847 TRUE))
848 goto activate_failed;
849 }
850
851 GST_LOG_OBJECT(decoder, "Allocate output buffer");
852
853 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
854
855 self->output_flow = GST_FLOW_OK;
856 do
857 {
858 /* We cannot use the base class allotate helper since it taking the internal
859 * stream lock. we know that the acquire may need to poll until more frames
860 * comes in and holding this lock would prevent that.
861 */
862 pool = gst_video_decoder_get_buffer_pool(decoder);
863
864 /* Pool may be NULL if we started going to READY state */
865 if (pool == NULL)
866 {
fei.dengbee20862022-06-14 14:59:48 +0800867 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800868 ret = GST_FLOW_FLUSHING;
869 goto beach;
870 }
871
872 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
873 g_object_unref(pool);
874
sheng.liub56bbc52022-06-21 11:02:33 +0800875 if (ret == GST_V4L2_FLOW_LAST_BUFFER) {
876 GST_LOG_OBJECT(decoder, "Get GST_V4L2_FLOW_LAST_BUFFER");
877 self->v4l2capture->need_drop_event = TRUE;
878 goto beach;
879 }
880
sheng.liu8d18ed22022-05-26 17:28:15 +0800881 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
882 {
883 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
884 gst_aml_v4l2_object_stop(self->v4l2capture);
885 return;
886 }
887
fei.dengbee20862022-06-14 14:59:48 +0800888 if (ret != GST_FLOW_OK) {
889 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800890 goto beach;
fei.dengbee20862022-06-14 14:59:48 +0800891 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800892
893 GST_LOG_OBJECT(decoder, "Process output buffer");
894 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
895 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
896 {
897 gst_aml_v4l2_object_stop(self->v4l2capture);
898 return;
899 }
900
901 } while (ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER);
902
903 if (ret != GST_FLOW_OK)
904 goto beach;
905
fei.dengbee20862022-06-14 14:59:48 +0800906 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +0800907
908 if (frame)
909 {
910 frame->output_buffer = buffer;
911 buffer = NULL;
912 ret = gst_video_decoder_finish_frame(decoder, frame);
913
914 if (ret != GST_FLOW_OK)
915 goto beach;
916 }
917 else
918 {
919 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
920 gst_buffer_unref(buffer);
921 }
922
923 return;
924 /* ERRORS */
925not_negotiated:
926{
927 GST_ERROR_OBJECT(self, "not negotiated");
928 ret = GST_FLOW_NOT_NEGOTIATED;
929 goto beach;
930}
931activate_failed:
932{
933 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
934 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
935 (_("Failed to allocate required memory.")),
936 ("Buffer pool activation failed"));
937 ret = GST_FLOW_ERROR;
938 goto beach;
939}
940flushing:
941{
942 ret = GST_FLOW_FLUSHING;
943 goto beach;
944}
945beach:
946 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
947 gst_flow_get_name(ret));
948
949 gst_buffer_replace(&buffer, NULL);
950 self->output_flow = ret;
951 gst_aml_v4l2_object_unlock(self->v4l2output);
952 gst_pad_pause_task(decoder->srcpad);
953}
954
955static GstFlowReturn
956gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
957 GstVideoCodecFrame *frame)
958{
959 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
960 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
961 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
962 GstFlowReturn ret = GST_FLOW_OK;
963 gboolean processed = FALSE;
964 GstBuffer *tmp;
965 GstTaskState task_state;
966 GstCaps *caps;
967
968 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
969
970 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
971 goto flushing;
972
973 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
974 {
975 if (!self->input_state)
976 goto not_negotiated;
977 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
978 &error))
979 goto not_negotiated;
980 }
981
982 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
983 {
984 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +0800985 GstCapsFeatures *features = NULL;
986
987 features = gst_caps_get_features(self->input_state->caps, 0);
988 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
989 {
990 GST_DEBUG_OBJECT(self, "Is SVP");
991 self->v4l2output->is_svp = TRUE;
992 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800993
994 GST_DEBUG_OBJECT(self, "Sending header");
995
996 codec_data = self->input_state->codec_data;
997
998 /* We are running in byte-stream mode, so we don't know the headers, but
999 * we need to send something, otherwise the decoder will refuse to
1000 * intialize.
1001 */
1002 if (codec_data)
1003 {
1004 gst_buffer_ref(codec_data);
1005 }
1006 else
1007 {
1008 codec_data = gst_buffer_ref(frame->input_buffer);
1009 processed = TRUE;
1010 }
1011
1012 /* Ensure input internal pool is active */
1013 if (!gst_buffer_pool_is_active(pool))
1014 {
1015 GstStructure *config = gst_buffer_pool_get_config(pool);
1016 guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1017 guint max = VIDEO_MAX_FRAME;
1018
1019 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1020 // self->v4l2output->info.size, min, max);
1021 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1022
1023 /* There is no reason to refuse this config */
1024 if (!gst_buffer_pool_set_config(pool, config))
1025 goto activate_failed;
1026
1027 if (!gst_buffer_pool_set_active(pool, TRUE))
1028 goto activate_failed;
1029 }
1030
1031 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1032 ret =
1033 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
1034 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1035
1036 gst_buffer_unref(codec_data);
1037
1038 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1039 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1040 * and returns the visible size as with/height and the coded size as
1041 * padding. */
1042 }
1043
1044 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1045 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1046 {
1047 /* It's possible that the processing thread stopped due to an error */
1048 if (self->output_flow != GST_FLOW_OK &&
1049 self->output_flow != GST_FLOW_FLUSHING)
1050 {
1051 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1052 ret = self->output_flow;
1053 goto drop;
1054 }
1055
1056 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1057
1058 /* Start the processing task, when it quits, the task will disable input
1059 * processing to unlock input if draining, or prevent potential block */
1060 self->output_flow = GST_FLOW_FLUSHING;
1061 if (!gst_pad_start_task(decoder->srcpad,
1062 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1063 goto start_task_failed;
1064 }
1065
1066 if (!processed)
1067 {
1068 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1069 ret =
1070 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1071 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1072
1073 if (ret == GST_FLOW_FLUSHING)
1074 {
1075 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1076 GST_TASK_STARTED)
1077 ret = self->output_flow;
1078 goto drop;
1079 }
1080 else if (ret != GST_FLOW_OK)
1081 {
1082 goto process_failed;
1083 }
1084 }
1085
1086 /* No need to keep input arround */
1087 tmp = frame->input_buffer;
1088 frame->input_buffer = gst_buffer_new();
1089 gst_buffer_copy_into(frame->input_buffer, tmp,
1090 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1091 GST_BUFFER_COPY_META,
1092 0, 0);
1093 gst_buffer_unref(tmp);
1094
1095 gst_video_codec_frame_unref(frame);
1096 return ret;
1097
1098 /* ERRORS */
1099not_negotiated:
1100{
1101 GST_ERROR_OBJECT(self, "not negotiated");
1102 ret = GST_FLOW_NOT_NEGOTIATED;
1103 gst_aml_v4l2_error(self, &error);
1104 goto drop;
1105}
1106activate_failed:
1107{
1108 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1109 (_("Failed to allocate required memory.")),
1110 ("Buffer pool activation failed"));
1111 ret = GST_FLOW_ERROR;
1112 goto drop;
1113}
1114flushing:
1115{
1116 ret = GST_FLOW_FLUSHING;
1117 goto drop;
1118}
1119
1120start_task_failed:
1121{
1122 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1123 (_("Failed to start decoding thread.")), (NULL));
1124 ret = GST_FLOW_ERROR;
1125 goto drop;
1126}
1127process_failed:
1128{
1129 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1130 (_("Failed to process frame.")),
1131 ("Maybe be due to not enough memory or failing driver"));
1132 ret = GST_FLOW_ERROR;
1133 goto drop;
1134}
1135drop:
1136{
1137 gst_video_decoder_drop_frame(decoder, frame);
1138 return ret;
1139}
1140}
1141
1142static gboolean
1143gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1144 GstQuery *query)
1145{
1146 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1147 GstClockTime latency;
1148 gboolean ret = FALSE;
1149
1150 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1151 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1152
1153 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1154 {
1155 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1156 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1157 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1158 gst_video_decoder_set_latency(decoder, latency, latency);
1159 }
1160 else
1161 {
1162 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1163 }
1164
1165 return ret;
1166}
1167
1168static gboolean
1169gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1170{
1171 gboolean ret = TRUE;
1172 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1173
1174 switch (GST_QUERY_TYPE(query))
1175 {
1176 case GST_QUERY_CAPS:
1177 {
1178 GstCaps *filter, *result = NULL;
1179 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1180
1181 gst_query_parse_caps(query, &filter);
1182
1183 if (self->probed_srccaps)
1184 result = gst_caps_ref(self->probed_srccaps);
1185 else
1186 result = gst_pad_get_pad_template_caps(pad);
1187
1188 if (filter)
1189 {
1190 GstCaps *tmp = result;
1191 result =
1192 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1193 gst_caps_unref(tmp);
1194 }
1195
1196 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1197
1198 gst_query_set_caps_result(query, result);
1199 gst_caps_unref(result);
1200 break;
1201 }
1202
1203 default:
1204 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1205 break;
1206 }
1207
1208 return ret;
1209}
1210
1211static GstCaps *
1212gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1213{
1214 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1215 GstCaps *result;
1216
1217 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1218 filter);
1219
1220 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1221
1222 return result;
1223}
1224
1225static gboolean
1226gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1227{
1228 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1229 gboolean ret;
1230 GstEventType type = GST_EVENT_TYPE(event);
1231
1232 switch (type)
1233 {
1234 case GST_EVENT_FLUSH_START:
1235 GST_DEBUG_OBJECT(self, "flush start");
1236 gst_aml_v4l2_object_unlock(self->v4l2output);
1237 gst_aml_v4l2_object_unlock(self->v4l2capture);
1238 break;
1239 default:
1240 break;
1241 }
1242
1243 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1244
1245 switch (type)
1246 {
1247 case GST_EVENT_FLUSH_START:
1248 /* The processing thread should stop now, wait for it */
1249 gst_pad_stop_task(decoder->srcpad);
1250 GST_DEBUG_OBJECT(self, "flush start done");
1251 break;
1252 default:
1253 break;
1254 }
1255
1256 return ret;
1257}
1258
1259static GstStateChangeReturn
1260gst_aml_v4l2_video_dec_change_state(GstElement *element,
1261 GstStateChange transition)
1262{
1263 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1264 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1265
1266 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1267 {
1268 g_atomic_int_set(&self->active, FALSE);
1269 gst_aml_v4l2_object_unlock(self->v4l2output);
1270 gst_aml_v4l2_object_unlock(self->v4l2capture);
1271 gst_pad_stop_task(decoder->srcpad);
1272 }
1273
1274 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1275}
1276
1277static void
1278gst_aml_v4l2_video_dec_dispose(GObject *object)
1279{
1280 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1281
1282 gst_caps_replace(&self->probed_sinkcaps, NULL);
1283 gst_caps_replace(&self->probed_srccaps, NULL);
1284
1285 G_OBJECT_CLASS(parent_class)->dispose(object);
1286}
1287
1288static void
1289gst_aml_v4l2_video_dec_finalize(GObject *object)
1290{
1291 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1292
1293 gst_aml_v4l2_object_destroy(self->v4l2capture);
1294 gst_aml_v4l2_object_destroy(self->v4l2output);
1295
xuesong.jiang61ea8012022-05-12 15:38:17 +08001296#if GST_IMPORT_LGE_PROP
1297 if (self->lge_ctxt)
1298 {
1299 if (self->lge_ctxt->app_type)
1300 g_free(self->lge_ctxt->app_type);
1301 if (self->lge_ctxt->res_info.coretype)
1302 g_free(self->lge_ctxt->res_info.coretype);
1303 free(self->lge_ctxt);
1304 }
1305
1306#endif
1307
xuesong.jiangae1548e2022-05-06 16:38:46 +08001308 G_OBJECT_CLASS(parent_class)->finalize(object);
1309}
1310
1311static void
1312gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1313{
1314 /* V4L2 object are created in subinstance_init */
1315 self->is_secure_path = FALSE;
xuesong.jiang61ea8012022-05-12 15:38:17 +08001316#if GST_IMPORT_LGE_PROP
1317 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1318 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1319#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001320}
1321
1322static void
1323gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1324{
1325 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1326 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1327 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1328
1329 gst_video_decoder_set_packetized(decoder, TRUE);
1330
1331 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1332 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1333 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1334 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1335 self->v4l2output->no_initial_format = TRUE;
1336 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001337 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001338
1339 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1340 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1341 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1342 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1343 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001344 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001345}
1346
1347static void
1348gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1349{
1350 GstElementClass *element_class;
1351 GObjectClass *gobject_class;
1352 GstVideoDecoderClass *video_decoder_class;
1353
1354 parent_class = g_type_class_peek_parent(klass);
1355
1356 element_class = (GstElementClass *)klass;
1357 gobject_class = (GObjectClass *)klass;
1358 video_decoder_class = (GstVideoDecoderClass *)klass;
1359
1360 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1361 "AML V4L2 Video Decoder");
1362
1363 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1364 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1365 gobject_class->set_property =
1366 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1367 gobject_class->get_property =
1368 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1369
1370 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1371 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1372 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1373 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1374 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1375 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1376 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1377 video_decoder_class->set_format =
1378 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1379 video_decoder_class->negotiate =
1380 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1381 video_decoder_class->decide_allocation =
1382 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1383 /* FIXME propose_allocation or not ? */
1384 video_decoder_class->handle_frame =
1385 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1386 video_decoder_class->getcaps =
1387 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1388 video_decoder_class->src_query =
1389 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1390 video_decoder_class->sink_event =
1391 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1392
1393 element_class->change_state =
1394 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1395
1396 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001397#if GST_IMPORT_LGE_PROP
1398 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1399#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001400}
1401
1402static void
1403gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1404{
1405 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1406 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1407 GstAmlV4l2VideoDecCData *cdata = data;
1408
1409 klass->default_device = cdata->device;
1410
1411 /* Note: gst_pad_template_new() take the floating ref from the caps */
1412 gst_element_class_add_pad_template(element_class,
1413 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1414 cdata->sink_caps));
1415 gst_element_class_add_pad_template(element_class,
1416 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1417 cdata->src_caps));
1418
1419 gst_element_class_set_metadata(element_class, cdata->longname,
1420 "Codec/Decoder/Video/Hardware", cdata->description,
1421 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1422
1423 gst_caps_unref(cdata->sink_caps);
1424 gst_caps_unref(cdata->src_caps);
1425 g_free(cdata);
1426}
1427
1428/* Probing functions */
1429gboolean
1430gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1431{
1432 gboolean ret = FALSE;
1433
1434 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()))
1435 ret = TRUE;
1436
1437 return ret;
1438}
1439
1440static gchar *
1441gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1442 const gchar *basename)
1443{
1444 gchar *codec_name = NULL;
1445 gchar *type_name = NULL;
1446 gboolean got_value = FALSE;
1447
1448#define SET_META(codec) \
1449 G_STMT_START \
1450 { \
1451 cdata->longname = "AML V4L2 " codec " Decoder"; \
1452 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1453 codec_name = g_ascii_strdown(codec, -1); \
1454 } \
1455 G_STMT_END
1456
1457 if (gst_structure_has_name(s, "image/jpeg"))
1458 {
1459 SET_META("JPEG");
1460 }
1461 else if (gst_structure_has_name(s, "video/mpeg"))
1462 {
1463 gint mpegversion = 0;
1464 gint *list = NULL;
1465 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1466 if (FALSE == got_value)
1467 {
1468 got_value = gst_structure_get_list(s, "mpegversion", &list);
1469 if (TRUE == got_value && (1 == *list || 2 == *list))
1470 {
1471 SET_META("MPEG2");
1472 }
1473 else
1474 {
1475 SET_META("MPEG4");
1476 }
1477 }
1478 else
1479 {
1480 SET_META("MPEG4");
1481 }
1482 }
1483 else if (gst_structure_has_name(s, "video/x-h263"))
1484 {
1485 SET_META("H263");
1486 }
1487 else if (gst_structure_has_name(s, "video/x-fwht"))
1488 {
1489 SET_META("FWHT");
1490 }
1491 else if (gst_structure_has_name(s, "video/x-h264"))
1492 {
1493 SET_META("H264");
1494 }
1495 else if (gst_structure_has_name(s, "video/x-h265"))
1496 {
1497 SET_META("H265");
1498 }
1499 else if (gst_structure_has_name(s, "video/x-wmv"))
1500 {
1501 SET_META("VC1");
1502 }
1503 else if (gst_structure_has_name(s, "video/x-vp8"))
1504 {
1505 SET_META("VP8");
1506 }
1507 else if (gst_structure_has_name(s, "video/x-vp9"))
1508 {
1509 SET_META("VP9");
1510 }
1511 else if (gst_structure_has_name(s, "video/x-av1"))
1512 {
1513 SET_META("AV1");
1514 }
1515 else if (gst_structure_has_name(s, "video/x-bayer"))
1516 {
1517 SET_META("BAYER");
1518 }
1519 else if (gst_structure_has_name(s, "video/x-sonix"))
1520 {
1521 SET_META("SONIX");
1522 }
1523 else if (gst_structure_has_name(s, "video/x-pwc1"))
1524 {
1525 SET_META("PWC1");
1526 }
1527 else if (gst_structure_has_name(s, "video/x-pwc2"))
1528 {
1529 SET_META("PWC2");
1530 }
1531 else
1532 {
1533 /* This code should be kept on sync with the exposed CODEC type of format
1534 * from gstamlv4l2object.c. This warning will only occure in case we forget
1535 * to also add a format here. */
1536 gchar *s_str = gst_structure_to_string(s);
1537 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
1538 "bug, please report at https://bugs.gnome.org",
1539 s_str);
1540 g_free(s_str);
1541 }
1542
1543 if (codec_name)
1544 {
1545 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
1546 if (g_type_from_name(type_name) != 0)
1547 {
1548 g_free(type_name);
1549 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
1550 }
1551
1552 g_free(codec_name);
1553 }
1554
1555 return type_name;
1556#undef SET_META
1557}
1558
1559void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
1560 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
1561{
1562 gint i;
1563
1564 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
1565 {
1566 GstAmlV4l2VideoDecCData *cdata;
1567 GstStructure *s;
1568 GTypeQuery type_query;
1569 GTypeInfo type_info = {
1570 0,
1571 };
1572 GType type, subtype;
1573 gchar *type_name;
1574
1575 s = gst_caps_get_structure(sink_caps, i);
1576
1577 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
1578 cdata->device = g_strdup(device_path);
1579 cdata->sink_caps = gst_caps_new_empty();
1580 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1581 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1582 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1583 cdata->src_caps = gst_caps_copy(src_caps);
1584 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1585 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
1586 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
1587
1588 /* Skip over if we hit an unmapped type */
1589 if (!type_name)
1590 {
1591 g_free(cdata);
1592 continue;
1593 }
1594
1595 type = gst_aml_v4l2_video_dec_get_type();
1596 g_type_query(type, &type_query);
1597 memset(&type_info, 0, sizeof(type_info));
1598 type_info.class_size = type_query.class_size;
1599 type_info.instance_size = type_query.instance_size;
1600 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
1601 type_info.class_data = cdata;
1602 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
1603
1604 subtype = g_type_register_static(type, type_name, &type_info, 0);
1605 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
1606 subtype))
1607 GST_WARNING("Failed to register plugin '%s'", type_name);
1608
1609 g_free(type_name);
1610 }
1611}
xuesong.jiang61ea8012022-05-12 15:38:17 +08001612
1613#if GST_IMPORT_LGE_PROP
1614static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
1615{
1616 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
1617 g_param_spec_object("resource-info", "resource-info",
1618 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
1619 GST_TYPE_STRUCTURE,
1620 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
1621
1622 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
1623 g_param_spec_uint64("decoded-size", "decoded-size",
1624 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
1625 0, G_MAXUINT64,
1626 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1627
1628 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
1629 g_param_spec_uint64("undecoded-size", "undecoded-size",
1630 "video decoder element's total undecoded data update unit is byte.",
1631 0, G_MAXUINT64,
1632 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1633
1634 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
1635 g_param_spec_string("app-type", "app-type",
1636 "set application type.",
1637 "default_app",
1638 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1639
1640 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
1641 g_param_spec_boolean("clip-mode", "clip-mode",
1642 "When seeking, Content is moving faster for a while to skip frames.",
1643 FALSE,
1644 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1645}
1646#endif