blob: d8ec277b2f44a72afb937cf1c97a377789e6fb3e [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
hanghang.luo3128f102022-08-18 10:36:19 +0800640 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 10) {
fei.dengbee20862022-06-14 14:59:48 +0800641 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);
fei.dengccc89632022-07-15 19:10:17 +0800885 //calculate a new pts for interlace stream
886 if (ret == GST_FLOW_OK &&
887 self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
888 {
889 //if buffer pts is valid, reduce 1/2 duration
890 if (GST_BUFFER_DURATION_IS_VALID(buffer))
891 {
892 GST_BUFFER_DURATION(buffer) = GST_BUFFER_DURATION(buffer)/2;
893 }
894 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
895 //reset pts
fei.denga6ae3282022-07-15 19:50:30 +0800896 if (GST_BUFFER_TIMESTAMP (buffer) == 0LL || self->last_out_pts == GST_BUFFER_TIMESTAMP (buffer))
fei.dengccc89632022-07-15 19:10:17 +0800897 {
898 double rate = ((double)self->input_state->info.fps_n/(double)self->input_state->info.fps_d)*2;
899 GST_BUFFER_TIMESTAMP(buffer) = self->last_out_pts + 1000000000LL/rate;
900 }
901 }
902
xuesong.jiangae1548e2022-05-06 16:38:46 +0800903 g_object_unref(pool);
904
sheng.liu4e01a472022-06-21 15:38:25 +0800905 if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER) {
sheng.liubcf036c2022-06-21 15:55:42 +0800906 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
sheng.liub56bbc52022-06-21 11:02:33 +0800907 self->v4l2capture->need_drop_event = TRUE;
908 goto beach;
909 }
910
sheng.liu8d18ed22022-05-26 17:28:15 +0800911 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
912 {
913 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
914 gst_aml_v4l2_object_stop(self->v4l2capture);
915 return;
916 }
917
fei.dengbee20862022-06-14 14:59:48 +0800918 if (ret != GST_FLOW_OK) {
919 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800920 goto beach;
fei.dengbee20862022-06-14 14:59:48 +0800921 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800922
923 GST_LOG_OBJECT(decoder, "Process output buffer");
924 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
925 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
926 {
927 gst_aml_v4l2_object_stop(self->v4l2capture);
928 return;
929 }
930
931 } while (ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER);
932
933 if (ret != GST_FLOW_OK)
934 goto beach;
935
fei.dengbee20862022-06-14 14:59:48 +0800936 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +0800937 if (frame)
938 {
fei.dengccc89632022-07-15 19:10:17 +0800939 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800940 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +0800941 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
942 frame->duration = GST_BUFFER_DURATION(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800943 buffer = NULL;
944 ret = gst_video_decoder_finish_frame(decoder, frame);
945
946 if (ret != GST_FLOW_OK)
947 goto beach;
948 }
949 else
950 {
951 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
952 gst_buffer_unref(buffer);
953 }
954
955 return;
956 /* ERRORS */
957not_negotiated:
958{
959 GST_ERROR_OBJECT(self, "not negotiated");
960 ret = GST_FLOW_NOT_NEGOTIATED;
961 goto beach;
962}
963activate_failed:
964{
965 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
966 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
967 (_("Failed to allocate required memory.")),
968 ("Buffer pool activation failed"));
969 ret = GST_FLOW_ERROR;
970 goto beach;
971}
972flushing:
973{
974 ret = GST_FLOW_FLUSHING;
975 goto beach;
976}
977beach:
978 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
979 gst_flow_get_name(ret));
980
981 gst_buffer_replace(&buffer, NULL);
982 self->output_flow = ret;
983 gst_aml_v4l2_object_unlock(self->v4l2output);
984 gst_pad_pause_task(decoder->srcpad);
985}
986
987static GstFlowReturn
988gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
989 GstVideoCodecFrame *frame)
990{
991 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
992 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
993 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
994 GstFlowReturn ret = GST_FLOW_OK;
995 gboolean processed = FALSE;
996 GstBuffer *tmp;
997 GstTaskState task_state;
998 GstCaps *caps;
999
1000 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1001
1002 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1003 goto flushing;
1004
1005 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1006 {
1007 if (!self->input_state)
1008 goto not_negotiated;
1009 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1010 &error))
1011 goto not_negotiated;
1012 }
1013
1014 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1015 {
1016 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001017 GstCapsFeatures *features = NULL;
1018
1019 features = gst_caps_get_features(self->input_state->caps, 0);
1020 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1021 {
1022 GST_DEBUG_OBJECT(self, "Is SVP");
1023 self->v4l2output->is_svp = TRUE;
1024 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001025
1026 GST_DEBUG_OBJECT(self, "Sending header");
1027
1028 codec_data = self->input_state->codec_data;
1029
1030 /* We are running in byte-stream mode, so we don't know the headers, but
1031 * we need to send something, otherwise the decoder will refuse to
1032 * intialize.
1033 */
1034 if (codec_data)
1035 {
1036 gst_buffer_ref(codec_data);
1037 }
1038 else
1039 {
1040 codec_data = gst_buffer_ref(frame->input_buffer);
1041 processed = TRUE;
1042 }
1043
1044 /* Ensure input internal pool is active */
1045 if (!gst_buffer_pool_is_active(pool))
1046 {
1047 GstStructure *config = gst_buffer_pool_get_config(pool);
1048 guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1049 guint max = VIDEO_MAX_FRAME;
1050
1051 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1052 // self->v4l2output->info.size, min, max);
1053 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1054
1055 /* There is no reason to refuse this config */
1056 if (!gst_buffer_pool_set_config(pool, config))
1057 goto activate_failed;
1058
1059 if (!gst_buffer_pool_set_active(pool, TRUE))
1060 goto activate_failed;
1061 }
1062
1063 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1064 ret =
1065 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
1066 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1067
1068 gst_buffer_unref(codec_data);
1069
1070 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1071 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1072 * and returns the visible size as with/height and the coded size as
1073 * padding. */
1074 }
1075
1076 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
1077 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1078 {
1079 /* It's possible that the processing thread stopped due to an error */
1080 if (self->output_flow != GST_FLOW_OK &&
1081 self->output_flow != GST_FLOW_FLUSHING)
1082 {
1083 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1084 ret = self->output_flow;
1085 goto drop;
1086 }
1087
1088 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1089
1090 /* Start the processing task, when it quits, the task will disable input
1091 * processing to unlock input if draining, or prevent potential block */
1092 self->output_flow = GST_FLOW_FLUSHING;
1093 if (!gst_pad_start_task(decoder->srcpad,
1094 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1095 goto start_task_failed;
1096 }
1097
1098 if (!processed)
1099 {
1100 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1101 ret =
1102 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1103 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1104
1105 if (ret == GST_FLOW_FLUSHING)
1106 {
1107 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1108 GST_TASK_STARTED)
1109 ret = self->output_flow;
1110 goto drop;
1111 }
1112 else if (ret != GST_FLOW_OK)
1113 {
1114 goto process_failed;
1115 }
1116 }
1117
1118 /* No need to keep input arround */
1119 tmp = frame->input_buffer;
1120 frame->input_buffer = gst_buffer_new();
1121 gst_buffer_copy_into(frame->input_buffer, tmp,
1122 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1123 GST_BUFFER_COPY_META,
1124 0, 0);
1125 gst_buffer_unref(tmp);
1126
1127 gst_video_codec_frame_unref(frame);
1128 return ret;
1129
1130 /* ERRORS */
1131not_negotiated:
1132{
1133 GST_ERROR_OBJECT(self, "not negotiated");
1134 ret = GST_FLOW_NOT_NEGOTIATED;
1135 gst_aml_v4l2_error(self, &error);
1136 goto drop;
1137}
1138activate_failed:
1139{
1140 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1141 (_("Failed to allocate required memory.")),
1142 ("Buffer pool activation failed"));
1143 ret = GST_FLOW_ERROR;
1144 goto drop;
1145}
1146flushing:
1147{
1148 ret = GST_FLOW_FLUSHING;
1149 goto drop;
1150}
1151
1152start_task_failed:
1153{
1154 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1155 (_("Failed to start decoding thread.")), (NULL));
1156 ret = GST_FLOW_ERROR;
1157 goto drop;
1158}
1159process_failed:
1160{
1161 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1162 (_("Failed to process frame.")),
1163 ("Maybe be due to not enough memory or failing driver"));
1164 ret = GST_FLOW_ERROR;
1165 goto drop;
1166}
1167drop:
1168{
1169 gst_video_decoder_drop_frame(decoder, frame);
1170 return ret;
1171}
1172}
1173
1174static gboolean
1175gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1176 GstQuery *query)
1177{
1178 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1179 GstClockTime latency;
1180 gboolean ret = FALSE;
1181
1182 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1183 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1184
1185 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1186 {
1187 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1188 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1189 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1190 gst_video_decoder_set_latency(decoder, latency, latency);
1191 }
1192 else
1193 {
1194 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1195 }
1196
1197 return ret;
1198}
1199
1200static gboolean
1201gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1202{
1203 gboolean ret = TRUE;
1204 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1205
1206 switch (GST_QUERY_TYPE(query))
1207 {
1208 case GST_QUERY_CAPS:
1209 {
1210 GstCaps *filter, *result = NULL;
1211 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1212
1213 gst_query_parse_caps(query, &filter);
1214
1215 if (self->probed_srccaps)
1216 result = gst_caps_ref(self->probed_srccaps);
1217 else
1218 result = gst_pad_get_pad_template_caps(pad);
1219
1220 if (filter)
1221 {
1222 GstCaps *tmp = result;
1223 result =
1224 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1225 gst_caps_unref(tmp);
1226 }
1227
1228 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1229
1230 gst_query_set_caps_result(query, result);
1231 gst_caps_unref(result);
1232 break;
1233 }
1234
1235 default:
1236 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1237 break;
1238 }
1239
1240 return ret;
1241}
1242
1243static GstCaps *
1244gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1245{
1246 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1247 GstCaps *result;
1248
1249 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1250 filter);
1251
1252 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1253
1254 return result;
1255}
1256
1257static gboolean
1258gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1259{
1260 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1261 gboolean ret;
1262 GstEventType type = GST_EVENT_TYPE(event);
1263
1264 switch (type)
1265 {
1266 case GST_EVENT_FLUSH_START:
1267 GST_DEBUG_OBJECT(self, "flush start");
1268 gst_aml_v4l2_object_unlock(self->v4l2output);
1269 gst_aml_v4l2_object_unlock(self->v4l2capture);
1270 break;
1271 default:
1272 break;
1273 }
1274
1275 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1276
1277 switch (type)
1278 {
1279 case GST_EVENT_FLUSH_START:
1280 /* The processing thread should stop now, wait for it */
1281 gst_pad_stop_task(decoder->srcpad);
1282 GST_DEBUG_OBJECT(self, "flush start done");
1283 break;
1284 default:
1285 break;
1286 }
1287
1288 return ret;
1289}
1290
1291static GstStateChangeReturn
1292gst_aml_v4l2_video_dec_change_state(GstElement *element,
1293 GstStateChange transition)
1294{
1295 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1296 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1297
1298 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1299 {
1300 g_atomic_int_set(&self->active, FALSE);
1301 gst_aml_v4l2_object_unlock(self->v4l2output);
1302 gst_aml_v4l2_object_unlock(self->v4l2capture);
1303 gst_pad_stop_task(decoder->srcpad);
1304 }
1305
1306 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1307}
1308
1309static void
1310gst_aml_v4l2_video_dec_dispose(GObject *object)
1311{
1312 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1313
1314 gst_caps_replace(&self->probed_sinkcaps, NULL);
1315 gst_caps_replace(&self->probed_srccaps, NULL);
1316
1317 G_OBJECT_CLASS(parent_class)->dispose(object);
1318}
1319
1320static void
1321gst_aml_v4l2_video_dec_finalize(GObject *object)
1322{
1323 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1324
1325 gst_aml_v4l2_object_destroy(self->v4l2capture);
1326 gst_aml_v4l2_object_destroy(self->v4l2output);
1327
xuesong.jiang61ea8012022-05-12 15:38:17 +08001328#if GST_IMPORT_LGE_PROP
1329 if (self->lge_ctxt)
1330 {
1331 if (self->lge_ctxt->app_type)
1332 g_free(self->lge_ctxt->app_type);
1333 if (self->lge_ctxt->res_info.coretype)
1334 g_free(self->lge_ctxt->res_info.coretype);
1335 free(self->lge_ctxt);
1336 }
1337
1338#endif
1339
xuesong.jiangae1548e2022-05-06 16:38:46 +08001340 G_OBJECT_CLASS(parent_class)->finalize(object);
1341}
1342
1343static void
1344gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1345{
1346 /* V4L2 object are created in subinstance_init */
1347 self->is_secure_path = FALSE;
xuesong.jiang61ea8012022-05-12 15:38:17 +08001348#if GST_IMPORT_LGE_PROP
1349 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1350 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1351#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001352}
1353
1354static void
1355gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1356{
1357 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1358 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1359 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1360
1361 gst_video_decoder_set_packetized(decoder, TRUE);
1362
1363 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1364 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1365 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1366 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1367 self->v4l2output->no_initial_format = TRUE;
1368 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001369 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001370
1371 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1372 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1373 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1374 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1375 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001376 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001377}
1378
1379static void
1380gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1381{
1382 GstElementClass *element_class;
1383 GObjectClass *gobject_class;
1384 GstVideoDecoderClass *video_decoder_class;
1385
1386 parent_class = g_type_class_peek_parent(klass);
1387
1388 element_class = (GstElementClass *)klass;
1389 gobject_class = (GObjectClass *)klass;
1390 video_decoder_class = (GstVideoDecoderClass *)klass;
1391
1392 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1393 "AML V4L2 Video Decoder");
1394
1395 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1396 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1397 gobject_class->set_property =
1398 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1399 gobject_class->get_property =
1400 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1401
1402 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1403 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1404 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1405 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1406 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1407 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1408 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1409 video_decoder_class->set_format =
1410 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1411 video_decoder_class->negotiate =
1412 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1413 video_decoder_class->decide_allocation =
1414 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1415 /* FIXME propose_allocation or not ? */
1416 video_decoder_class->handle_frame =
1417 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1418 video_decoder_class->getcaps =
1419 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1420 video_decoder_class->src_query =
1421 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1422 video_decoder_class->sink_event =
1423 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1424
1425 element_class->change_state =
1426 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1427
1428 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001429#if GST_IMPORT_LGE_PROP
1430 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1431#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001432}
1433
1434static void
1435gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1436{
1437 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1438 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1439 GstAmlV4l2VideoDecCData *cdata = data;
1440
1441 klass->default_device = cdata->device;
1442
1443 /* Note: gst_pad_template_new() take the floating ref from the caps */
1444 gst_element_class_add_pad_template(element_class,
1445 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1446 cdata->sink_caps));
1447 gst_element_class_add_pad_template(element_class,
1448 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1449 cdata->src_caps));
1450
1451 gst_element_class_set_metadata(element_class, cdata->longname,
1452 "Codec/Decoder/Video/Hardware", cdata->description,
1453 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1454
1455 gst_caps_unref(cdata->sink_caps);
1456 gst_caps_unref(cdata->src_caps);
1457 g_free(cdata);
1458}
1459
1460/* Probing functions */
1461gboolean
1462gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1463{
1464 gboolean ret = FALSE;
1465
1466 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()))
1467 ret = TRUE;
1468
1469 return ret;
1470}
1471
1472static gchar *
1473gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1474 const gchar *basename)
1475{
1476 gchar *codec_name = NULL;
1477 gchar *type_name = NULL;
1478 gboolean got_value = FALSE;
1479
1480#define SET_META(codec) \
1481 G_STMT_START \
1482 { \
1483 cdata->longname = "AML V4L2 " codec " Decoder"; \
1484 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1485 codec_name = g_ascii_strdown(codec, -1); \
1486 } \
1487 G_STMT_END
1488
1489 if (gst_structure_has_name(s, "image/jpeg"))
1490 {
1491 SET_META("JPEG");
1492 }
1493 else if (gst_structure_has_name(s, "video/mpeg"))
1494 {
1495 gint mpegversion = 0;
1496 gint *list = NULL;
1497 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1498 if (FALSE == got_value)
1499 {
1500 got_value = gst_structure_get_list(s, "mpegversion", &list);
1501 if (TRUE == got_value && (1 == *list || 2 == *list))
1502 {
1503 SET_META("MPEG2");
1504 }
1505 else
1506 {
1507 SET_META("MPEG4");
1508 }
1509 }
1510 else
1511 {
1512 SET_META("MPEG4");
1513 }
1514 }
1515 else if (gst_structure_has_name(s, "video/x-h263"))
1516 {
1517 SET_META("H263");
1518 }
1519 else if (gst_structure_has_name(s, "video/x-fwht"))
1520 {
1521 SET_META("FWHT");
1522 }
1523 else if (gst_structure_has_name(s, "video/x-h264"))
1524 {
1525 SET_META("H264");
1526 }
1527 else if (gst_structure_has_name(s, "video/x-h265"))
1528 {
1529 SET_META("H265");
1530 }
1531 else if (gst_structure_has_name(s, "video/x-wmv"))
1532 {
1533 SET_META("VC1");
1534 }
1535 else if (gst_structure_has_name(s, "video/x-vp8"))
1536 {
1537 SET_META("VP8");
1538 }
1539 else if (gst_structure_has_name(s, "video/x-vp9"))
1540 {
1541 SET_META("VP9");
1542 }
1543 else if (gst_structure_has_name(s, "video/x-av1"))
1544 {
1545 SET_META("AV1");
1546 }
1547 else if (gst_structure_has_name(s, "video/x-bayer"))
1548 {
1549 SET_META("BAYER");
1550 }
1551 else if (gst_structure_has_name(s, "video/x-sonix"))
1552 {
1553 SET_META("SONIX");
1554 }
1555 else if (gst_structure_has_name(s, "video/x-pwc1"))
1556 {
1557 SET_META("PWC1");
1558 }
1559 else if (gst_structure_has_name(s, "video/x-pwc2"))
1560 {
1561 SET_META("PWC2");
1562 }
1563 else
1564 {
1565 /* This code should be kept on sync with the exposed CODEC type of format
1566 * from gstamlv4l2object.c. This warning will only occure in case we forget
1567 * to also add a format here. */
1568 gchar *s_str = gst_structure_to_string(s);
1569 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
1570 "bug, please report at https://bugs.gnome.org",
1571 s_str);
1572 g_free(s_str);
1573 }
1574
1575 if (codec_name)
1576 {
1577 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
1578 if (g_type_from_name(type_name) != 0)
1579 {
1580 g_free(type_name);
1581 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
1582 }
1583
1584 g_free(codec_name);
1585 }
1586
1587 return type_name;
1588#undef SET_META
1589}
1590
1591void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
1592 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
1593{
1594 gint i;
1595
1596 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
1597 {
1598 GstAmlV4l2VideoDecCData *cdata;
1599 GstStructure *s;
1600 GTypeQuery type_query;
1601 GTypeInfo type_info = {
1602 0,
1603 };
1604 GType type, subtype;
1605 gchar *type_name;
1606
1607 s = gst_caps_get_structure(sink_caps, i);
1608
1609 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
1610 cdata->device = g_strdup(device_path);
1611 cdata->sink_caps = gst_caps_new_empty();
1612 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1613 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1614 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1615 cdata->src_caps = gst_caps_copy(src_caps);
1616 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1617 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
1618 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
1619
1620 /* Skip over if we hit an unmapped type */
1621 if (!type_name)
1622 {
1623 g_free(cdata);
1624 continue;
1625 }
1626
1627 type = gst_aml_v4l2_video_dec_get_type();
1628 g_type_query(type, &type_query);
1629 memset(&type_info, 0, sizeof(type_info));
1630 type_info.class_size = type_query.class_size;
1631 type_info.instance_size = type_query.instance_size;
1632 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
1633 type_info.class_data = cdata;
1634 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
1635
1636 subtype = g_type_register_static(type, type_name, &type_info, 0);
1637 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
1638 subtype))
1639 GST_WARNING("Failed to register plugin '%s'", type_name);
1640
1641 g_free(type_name);
1642 }
1643}
xuesong.jiang61ea8012022-05-12 15:38:17 +08001644
1645#if GST_IMPORT_LGE_PROP
1646static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
1647{
1648 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
1649 g_param_spec_object("resource-info", "resource-info",
1650 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
1651 GST_TYPE_STRUCTURE,
1652 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
1653
1654 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
1655 g_param_spec_uint64("decoded-size", "decoded-size",
1656 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
1657 0, G_MAXUINT64,
1658 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1659
1660 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
1661 g_param_spec_uint64("undecoded-size", "undecoded-size",
1662 "video decoder element's total undecoded data update unit is byte.",
1663 0, G_MAXUINT64,
1664 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1665
1666 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
1667 g_param_spec_string("app-type", "app-type",
1668 "set application type.",
1669 "default_app",
1670 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1671
1672 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
1673 g_param_spec_boolean("clip-mode", "clip-mode",
1674 "When seeking, Content is moving faster for a while to skip frames.",
1675 FALSE,
1676 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1677}
1678#endif