blob: 4608babf08356e866d2d7008d9d41bb689c26c5b [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
688gst_aml_v4l2_video_dec_loop(GstVideoDecoder *decoder)
689{
690 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
691 GstAmlV4l2BufferPool *v4l2_pool;
692 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
693 GstBufferPool *pool;
694 GstVideoCodecFrame *frame;
695 GstBuffer *buffer = NULL;
696 GstFlowReturn ret;
697
698 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
699 {
700 GstVideoInfo info;
701 GstVideoCodecState *output_state;
702 GstCaps *acquired_caps, *available_caps, *caps, *filter;
703 GstStructure *st;
704
705 GST_DEBUG_OBJECT(self, "waitting source change event");
706 /* Wait until received SOURCE_CHANGE event to get right video format */
707 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
708 {
709 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
710 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
711 {
712 GST_DEBUG_OBJECT(self, "Received source change event");
713 break;
714 }
715 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
716 {
717 GST_DEBUG_OBJECT(self, "Received eos event");
718 goto beach;
719 }
720 else if (ret != GST_FLOW_OK)
721 {
722 GST_ERROR_OBJECT(self, "dqevent error");
723 goto beach;
724 }
725 }
726 self->v4l2capture->need_wait_event = FALSE;
727
728 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
729 goto not_negotiated;
730
731 /* Create caps from the acquired format, remove the format field */
732 acquired_caps = gst_video_info_to_caps(&info);
733 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
734 st = gst_caps_get_structure(acquired_caps, 0);
735 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site",
736 NULL);
737
738 /* Probe currently available pixel formats */
739 available_caps = gst_caps_copy(self->probed_srccaps);
740 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
741
742 /* Replace coded size with visible size, we want to negotiate visible size
743 * with downstream, not coded size. */
744 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
745
746 filter = gst_caps_intersect_full(available_caps, acquired_caps,
747 GST_CAPS_INTERSECT_FIRST);
748 caps = gst_caps_copy(filter);
749 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
750 gst_caps_append(filter, caps);
751
752 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
753 gst_caps_unref(acquired_caps);
754 gst_caps_unref(available_caps);
755 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
756 gst_caps_unref(filter);
757
758 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
759 if (gst_caps_is_empty(caps))
760 {
761 gst_caps_unref(caps);
762 goto not_negotiated;
763 }
764
765 /* Fixate pixel format */
766 caps = gst_caps_fixate(caps);
767
768 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
769
770 /* Try to set negotiated format, on success replace acquired format */
771 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
772 gst_video_info_from_caps(&info, caps);
773 else
774 gst_aml_v4l2_clear_error(&error);
775 gst_caps_unref(caps);
776
777 output_state = gst_video_decoder_set_output_state(decoder,
778 info.finfo->format, info.width, info.height, self->input_state);
779
780 /* Copy the rest of the information, there might be more in the future */
781 output_state->info.interlace_mode = info.interlace_mode;
782 gst_video_codec_state_unref(output_state);
783
784 if (!gst_video_decoder_negotiate(decoder))
785 {
786 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
787 goto flushing;
788 else
789 goto not_negotiated;
790 }
791
792 /* Ensure our internal pool is activated */
793 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
794 TRUE))
795 goto activate_failed;
796 }
797
798 GST_LOG_OBJECT(decoder, "Allocate output buffer");
799
800 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
801
802 self->output_flow = GST_FLOW_OK;
803 do
804 {
805 /* We cannot use the base class allotate helper since it taking the internal
806 * stream lock. we know that the acquire may need to poll until more frames
807 * comes in and holding this lock would prevent that.
808 */
809 pool = gst_video_decoder_get_buffer_pool(decoder);
810
811 /* Pool may be NULL if we started going to READY state */
812 if (pool == NULL)
813 {
fei.dengbee20862022-06-14 14:59:48 +0800814 GST_WARNING_OBJECT(decoder, "gst_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800815 ret = GST_FLOW_FLUSHING;
816 goto beach;
817 }
818
819 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
820 g_object_unref(pool);
821
sheng.liu8d18ed22022-05-26 17:28:15 +0800822 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
823 {
824 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
825 gst_aml_v4l2_object_stop(self->v4l2capture);
826 return;
827 }
828
fei.dengbee20862022-06-14 14:59:48 +0800829 if (ret != GST_FLOW_OK) {
830 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800831 goto beach;
fei.dengbee20862022-06-14 14:59:48 +0800832 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800833
834 GST_LOG_OBJECT(decoder, "Process output buffer");
835 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
836 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
837 {
838 gst_aml_v4l2_object_stop(self->v4l2capture);
839 return;
840 }
841
842 } while (ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER);
843
844 if (ret != GST_FLOW_OK)
845 goto beach;
846
fei.dengbee20862022-06-14 14:59:48 +0800847 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +0800848
849 if (frame)
850 {
851 frame->output_buffer = buffer;
852 buffer = NULL;
853 ret = gst_video_decoder_finish_frame(decoder, frame);
854
855 if (ret != GST_FLOW_OK)
856 goto beach;
857 }
858 else
859 {
860 GST_WARNING_OBJECT(decoder, "Decoder is producing too many buffers");
861 gst_buffer_unref(buffer);
862 }
863
864 return;
865 /* ERRORS */
866not_negotiated:
867{
868 GST_ERROR_OBJECT(self, "not negotiated");
869 ret = GST_FLOW_NOT_NEGOTIATED;
870 goto beach;
871}
872activate_failed:
873{
874 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
875 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
876 (_("Failed to allocate required memory.")),
877 ("Buffer pool activation failed"));
878 ret = GST_FLOW_ERROR;
879 goto beach;
880}
881flushing:
882{
883 ret = GST_FLOW_FLUSHING;
884 goto beach;
885}
886beach:
887 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
888 gst_flow_get_name(ret));
889
890 gst_buffer_replace(&buffer, NULL);
891 self->output_flow = ret;
892 gst_aml_v4l2_object_unlock(self->v4l2output);
893 gst_pad_pause_task(decoder->srcpad);
894}
895
896static GstFlowReturn
897gst_aml_v4l2_video_dec_handle_frame(GstVideoDecoder *decoder,
898 GstVideoCodecFrame *frame)
899{
900 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
901 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
902 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
903 GstFlowReturn ret = GST_FLOW_OK;
904 gboolean processed = FALSE;
905 GstBuffer *tmp;
906 GstTaskState task_state;
907 GstCaps *caps;
908
909 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
910
911 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
912 goto flushing;
913
914 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
915 {
916 if (!self->input_state)
917 goto not_negotiated;
918 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
919 &error))
920 goto not_negotiated;
921 }
922
923 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
924 {
925 GstBuffer *codec_data;
926
927 GST_DEBUG_OBJECT(self, "Sending header");
928
929 codec_data = self->input_state->codec_data;
930
931 /* We are running in byte-stream mode, so we don't know the headers, but
932 * we need to send something, otherwise the decoder will refuse to
933 * intialize.
934 */
935 if (codec_data)
936 {
937 gst_buffer_ref(codec_data);
938 }
939 else
940 {
941 codec_data = gst_buffer_ref(frame->input_buffer);
942 processed = TRUE;
943 }
944
945 /* Ensure input internal pool is active */
946 if (!gst_buffer_pool_is_active(pool))
947 {
948 GstStructure *config = gst_buffer_pool_get_config(pool);
949 guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
950 guint max = VIDEO_MAX_FRAME;
951
952 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
953 // self->v4l2output->info.size, min, max);
954 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
955
956 /* There is no reason to refuse this config */
957 if (!gst_buffer_pool_set_config(pool, config))
958 goto activate_failed;
959
960 if (!gst_buffer_pool_set_active(pool, TRUE))
961 goto activate_failed;
962 }
963
964 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
965 ret =
966 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
967 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
968
969 gst_buffer_unref(codec_data);
970
971 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
972 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
973 * and returns the visible size as with/height and the coded size as
974 * padding. */
975 }
976
977 task_state = gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self));
978 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
979 {
980 /* It's possible that the processing thread stopped due to an error */
981 if (self->output_flow != GST_FLOW_OK &&
982 self->output_flow != GST_FLOW_FLUSHING)
983 {
984 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
985 ret = self->output_flow;
986 goto drop;
987 }
988
989 GST_DEBUG_OBJECT(self, "Starting decoding thread");
990
991 /* Start the processing task, when it quits, the task will disable input
992 * processing to unlock input if draining, or prevent potential block */
993 self->output_flow = GST_FLOW_FLUSHING;
994 if (!gst_pad_start_task(decoder->srcpad,
995 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
996 goto start_task_failed;
997 }
998
999 if (!processed)
1000 {
1001 GST_VIDEO_DECODER_STREAM_UNLOCK(decoder);
1002 ret =
1003 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1004 GST_VIDEO_DECODER_STREAM_LOCK(decoder);
1005
1006 if (ret == GST_FLOW_FLUSHING)
1007 {
1008 if (gst_pad_get_task_state(GST_VIDEO_DECODER_SRC_PAD(self)) !=
1009 GST_TASK_STARTED)
1010 ret = self->output_flow;
1011 goto drop;
1012 }
1013 else if (ret != GST_FLOW_OK)
1014 {
1015 goto process_failed;
1016 }
1017 }
1018
1019 /* No need to keep input arround */
1020 tmp = frame->input_buffer;
1021 frame->input_buffer = gst_buffer_new();
1022 gst_buffer_copy_into(frame->input_buffer, tmp,
1023 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1024 GST_BUFFER_COPY_META,
1025 0, 0);
1026 gst_buffer_unref(tmp);
1027
1028 gst_video_codec_frame_unref(frame);
1029 return ret;
1030
1031 /* ERRORS */
1032not_negotiated:
1033{
1034 GST_ERROR_OBJECT(self, "not negotiated");
1035 ret = GST_FLOW_NOT_NEGOTIATED;
1036 gst_aml_v4l2_error(self, &error);
1037 goto drop;
1038}
1039activate_failed:
1040{
1041 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1042 (_("Failed to allocate required memory.")),
1043 ("Buffer pool activation failed"));
1044 ret = GST_FLOW_ERROR;
1045 goto drop;
1046}
1047flushing:
1048{
1049 ret = GST_FLOW_FLUSHING;
1050 goto drop;
1051}
1052
1053start_task_failed:
1054{
1055 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1056 (_("Failed to start decoding thread.")), (NULL));
1057 ret = GST_FLOW_ERROR;
1058 goto drop;
1059}
1060process_failed:
1061{
1062 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1063 (_("Failed to process frame.")),
1064 ("Maybe be due to not enough memory or failing driver"));
1065 ret = GST_FLOW_ERROR;
1066 goto drop;
1067}
1068drop:
1069{
1070 gst_video_decoder_drop_frame(decoder, frame);
1071 return ret;
1072}
1073}
1074
1075static gboolean
1076gst_aml_v4l2_video_dec_decide_allocation(GstVideoDecoder *decoder,
1077 GstQuery *query)
1078{
1079 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1080 GstClockTime latency;
1081 gboolean ret = FALSE;
1082
1083 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
1084 ret = GST_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
1085
1086 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1087 {
1088 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1089 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1090 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1091 gst_video_decoder_set_latency(decoder, latency, latency);
1092 }
1093 else
1094 {
1095 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1096 }
1097
1098 return ret;
1099}
1100
1101static gboolean
1102gst_aml_v4l2_video_dec_src_query(GstVideoDecoder *decoder, GstQuery *query)
1103{
1104 gboolean ret = TRUE;
1105 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1106
1107 switch (GST_QUERY_TYPE(query))
1108 {
1109 case GST_QUERY_CAPS:
1110 {
1111 GstCaps *filter, *result = NULL;
1112 GstPad *pad = GST_VIDEO_DECODER_SRC_PAD(decoder);
1113
1114 gst_query_parse_caps(query, &filter);
1115
1116 if (self->probed_srccaps)
1117 result = gst_caps_ref(self->probed_srccaps);
1118 else
1119 result = gst_pad_get_pad_template_caps(pad);
1120
1121 if (filter)
1122 {
1123 GstCaps *tmp = result;
1124 result =
1125 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1126 gst_caps_unref(tmp);
1127 }
1128
1129 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1130
1131 gst_query_set_caps_result(query, result);
1132 gst_caps_unref(result);
1133 break;
1134 }
1135
1136 default:
1137 ret = GST_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
1138 break;
1139 }
1140
1141 return ret;
1142}
1143
1144static GstCaps *
1145gst_aml_v4l2_video_dec_sink_getcaps(GstVideoDecoder *decoder, GstCaps *filter)
1146{
1147 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1148 GstCaps *result;
1149
1150 result = gst_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
1151 filter);
1152
1153 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1154
1155 return result;
1156}
1157
1158static gboolean
1159gst_aml_v4l2_video_dec_sink_event(GstVideoDecoder *decoder, GstEvent *event)
1160{
1161 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1162 gboolean ret;
1163 GstEventType type = GST_EVENT_TYPE(event);
1164
1165 switch (type)
1166 {
1167 case GST_EVENT_FLUSH_START:
1168 GST_DEBUG_OBJECT(self, "flush start");
1169 gst_aml_v4l2_object_unlock(self->v4l2output);
1170 gst_aml_v4l2_object_unlock(self->v4l2capture);
1171 break;
1172 default:
1173 break;
1174 }
1175
1176 ret = GST_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
1177
1178 switch (type)
1179 {
1180 case GST_EVENT_FLUSH_START:
1181 /* The processing thread should stop now, wait for it */
1182 gst_pad_stop_task(decoder->srcpad);
1183 GST_DEBUG_OBJECT(self, "flush start done");
1184 break;
1185 default:
1186 break;
1187 }
1188
1189 return ret;
1190}
1191
1192static GstStateChangeReturn
1193gst_aml_v4l2_video_dec_change_state(GstElement *element,
1194 GstStateChange transition)
1195{
1196 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1197 GstVideoDecoder *decoder = GST_VIDEO_DECODER(element);
1198
1199 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1200 {
1201 g_atomic_int_set(&self->active, FALSE);
1202 gst_aml_v4l2_object_unlock(self->v4l2output);
1203 gst_aml_v4l2_object_unlock(self->v4l2capture);
1204 gst_pad_stop_task(decoder->srcpad);
1205 }
1206
1207 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1208}
1209
1210static void
1211gst_aml_v4l2_video_dec_dispose(GObject *object)
1212{
1213 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1214
1215 gst_caps_replace(&self->probed_sinkcaps, NULL);
1216 gst_caps_replace(&self->probed_srccaps, NULL);
1217
1218 G_OBJECT_CLASS(parent_class)->dispose(object);
1219}
1220
1221static void
1222gst_aml_v4l2_video_dec_finalize(GObject *object)
1223{
1224 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1225
1226 gst_aml_v4l2_object_destroy(self->v4l2capture);
1227 gst_aml_v4l2_object_destroy(self->v4l2output);
1228
xuesong.jiang61ea8012022-05-12 15:38:17 +08001229#if GST_IMPORT_LGE_PROP
1230 if (self->lge_ctxt)
1231 {
1232 if (self->lge_ctxt->app_type)
1233 g_free(self->lge_ctxt->app_type);
1234 if (self->lge_ctxt->res_info.coretype)
1235 g_free(self->lge_ctxt->res_info.coretype);
1236 free(self->lge_ctxt);
1237 }
1238
1239#endif
1240
xuesong.jiangae1548e2022-05-06 16:38:46 +08001241 G_OBJECT_CLASS(parent_class)->finalize(object);
1242}
1243
1244static void
1245gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1246{
1247 /* V4L2 object are created in subinstance_init */
1248 self->is_secure_path = FALSE;
xuesong.jiang61ea8012022-05-12 15:38:17 +08001249#if GST_IMPORT_LGE_PROP
1250 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1251 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1252#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001253}
1254
1255static void
1256gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1257{
1258 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1259 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
1260 GstVideoDecoder *decoder = GST_VIDEO_DECODER(instance);
1261
1262 gst_video_decoder_set_packetized(decoder, TRUE);
1263
1264 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1265 GST_OBJECT(GST_VIDEO_DECODER_SINK_PAD(self)),
1266 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1267 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1268 self->v4l2output->no_initial_format = TRUE;
1269 self->v4l2output->keep_aspect = FALSE;
1270
1271 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
1272 GST_OBJECT(GST_VIDEO_DECODER_SRC_PAD(self)),
1273 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1274 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1275 self->v4l2capture->need_wait_event = TRUE;
1276}
1277
1278static void
1279gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1280{
1281 GstElementClass *element_class;
1282 GObjectClass *gobject_class;
1283 GstVideoDecoderClass *video_decoder_class;
1284
1285 parent_class = g_type_class_peek_parent(klass);
1286
1287 element_class = (GstElementClass *)klass;
1288 gobject_class = (GObjectClass *)klass;
1289 video_decoder_class = (GstVideoDecoderClass *)klass;
1290
1291 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1292 "AML V4L2 Video Decoder");
1293
1294 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1295 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1296 gobject_class->set_property =
1297 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1298 gobject_class->get_property =
1299 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1300
1301 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1302 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1303 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1304 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1305 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1306 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1307 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1308 video_decoder_class->set_format =
1309 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1310 video_decoder_class->negotiate =
1311 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1312 video_decoder_class->decide_allocation =
1313 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1314 /* FIXME propose_allocation or not ? */
1315 video_decoder_class->handle_frame =
1316 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1317 video_decoder_class->getcaps =
1318 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1319 video_decoder_class->src_query =
1320 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1321 video_decoder_class->sink_event =
1322 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1323
1324 element_class->change_state =
1325 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1326
1327 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001328#if GST_IMPORT_LGE_PROP
1329 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1330#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001331}
1332
1333static void
1334gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1335{
1336 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1337 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1338 GstAmlV4l2VideoDecCData *cdata = data;
1339
1340 klass->default_device = cdata->device;
1341
1342 /* Note: gst_pad_template_new() take the floating ref from the caps */
1343 gst_element_class_add_pad_template(element_class,
1344 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1345 cdata->sink_caps));
1346 gst_element_class_add_pad_template(element_class,
1347 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1348 cdata->src_caps));
1349
1350 gst_element_class_set_metadata(element_class, cdata->longname,
1351 "Codec/Decoder/Video/Hardware", cdata->description,
1352 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1353
1354 gst_caps_unref(cdata->sink_caps);
1355 gst_caps_unref(cdata->src_caps);
1356 g_free(cdata);
1357}
1358
1359/* Probing functions */
1360gboolean
1361gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1362{
1363 gboolean ret = FALSE;
1364
1365 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()))
1366 ret = TRUE;
1367
1368 return ret;
1369}
1370
1371static gchar *
1372gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1373 const gchar *basename)
1374{
1375 gchar *codec_name = NULL;
1376 gchar *type_name = NULL;
1377 gboolean got_value = FALSE;
1378
1379#define SET_META(codec) \
1380 G_STMT_START \
1381 { \
1382 cdata->longname = "AML V4L2 " codec " Decoder"; \
1383 cdata->description = "Decodes " codec " streams via V4L2 API"; \
1384 codec_name = g_ascii_strdown(codec, -1); \
1385 } \
1386 G_STMT_END
1387
1388 if (gst_structure_has_name(s, "image/jpeg"))
1389 {
1390 SET_META("JPEG");
1391 }
1392 else if (gst_structure_has_name(s, "video/mpeg"))
1393 {
1394 gint mpegversion = 0;
1395 gint *list = NULL;
1396 got_value = gst_structure_get_int(s, "mpegversion", &mpegversion);
1397 if (FALSE == got_value)
1398 {
1399 got_value = gst_structure_get_list(s, "mpegversion", &list);
1400 if (TRUE == got_value && (1 == *list || 2 == *list))
1401 {
1402 SET_META("MPEG2");
1403 }
1404 else
1405 {
1406 SET_META("MPEG4");
1407 }
1408 }
1409 else
1410 {
1411 SET_META("MPEG4");
1412 }
1413 }
1414 else if (gst_structure_has_name(s, "video/x-h263"))
1415 {
1416 SET_META("H263");
1417 }
1418 else if (gst_structure_has_name(s, "video/x-fwht"))
1419 {
1420 SET_META("FWHT");
1421 }
1422 else if (gst_structure_has_name(s, "video/x-h264"))
1423 {
1424 SET_META("H264");
1425 }
1426 else if (gst_structure_has_name(s, "video/x-h265"))
1427 {
1428 SET_META("H265");
1429 }
1430 else if (gst_structure_has_name(s, "video/x-wmv"))
1431 {
1432 SET_META("VC1");
1433 }
1434 else if (gst_structure_has_name(s, "video/x-vp8"))
1435 {
1436 SET_META("VP8");
1437 }
1438 else if (gst_structure_has_name(s, "video/x-vp9"))
1439 {
1440 SET_META("VP9");
1441 }
1442 else if (gst_structure_has_name(s, "video/x-av1"))
1443 {
1444 SET_META("AV1");
1445 }
1446 else if (gst_structure_has_name(s, "video/x-bayer"))
1447 {
1448 SET_META("BAYER");
1449 }
1450 else if (gst_structure_has_name(s, "video/x-sonix"))
1451 {
1452 SET_META("SONIX");
1453 }
1454 else if (gst_structure_has_name(s, "video/x-pwc1"))
1455 {
1456 SET_META("PWC1");
1457 }
1458 else if (gst_structure_has_name(s, "video/x-pwc2"))
1459 {
1460 SET_META("PWC2");
1461 }
1462 else
1463 {
1464 /* This code should be kept on sync with the exposed CODEC type of format
1465 * from gstamlv4l2object.c. This warning will only occure in case we forget
1466 * to also add a format here. */
1467 gchar *s_str = gst_structure_to_string(s);
1468 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
1469 "bug, please report at https://bugs.gnome.org",
1470 s_str);
1471 g_free(s_str);
1472 }
1473
1474 if (codec_name)
1475 {
1476 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
1477 if (g_type_from_name(type_name) != 0)
1478 {
1479 g_free(type_name);
1480 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
1481 }
1482
1483 g_free(codec_name);
1484 }
1485
1486 return type_name;
1487#undef SET_META
1488}
1489
1490void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
1491 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
1492{
1493 gint i;
1494
1495 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
1496 {
1497 GstAmlV4l2VideoDecCData *cdata;
1498 GstStructure *s;
1499 GTypeQuery type_query;
1500 GTypeInfo type_info = {
1501 0,
1502 };
1503 GType type, subtype;
1504 gchar *type_name;
1505
1506 s = gst_caps_get_structure(sink_caps, i);
1507
1508 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
1509 cdata->device = g_strdup(device_path);
1510 cdata->sink_caps = gst_caps_new_empty();
1511 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1512 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
1513 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1514 cdata->src_caps = gst_caps_copy(src_caps);
1515 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1516 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
1517 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
1518
1519 /* Skip over if we hit an unmapped type */
1520 if (!type_name)
1521 {
1522 g_free(cdata);
1523 continue;
1524 }
1525
1526 type = gst_aml_v4l2_video_dec_get_type();
1527 g_type_query(type, &type_query);
1528 memset(&type_info, 0, sizeof(type_info));
1529 type_info.class_size = type_query.class_size;
1530 type_info.instance_size = type_query.instance_size;
1531 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
1532 type_info.class_data = cdata;
1533 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
1534
1535 subtype = g_type_register_static(type, type_name, &type_info, 0);
1536 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
1537 subtype))
1538 GST_WARNING("Failed to register plugin '%s'", type_name);
1539
1540 g_free(type_name);
1541 }
1542}
xuesong.jiang61ea8012022-05-12 15:38:17 +08001543
1544#if GST_IMPORT_LGE_PROP
1545static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
1546{
1547 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
1548 g_param_spec_object("resource-info", "resource-info",
1549 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
1550 GST_TYPE_STRUCTURE,
1551 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
1552
1553 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
1554 g_param_spec_uint64("decoded-size", "decoded-size",
1555 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
1556 0, G_MAXUINT64,
1557 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1558
1559 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
1560 g_param_spec_uint64("undecoded-size", "undecoded-size",
1561 "video decoder element's total undecoded data update unit is byte.",
1562 0, G_MAXUINT64,
1563 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1564
1565 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
1566 g_param_spec_string("app-type", "app-type",
1567 "set application type.",
1568 "default_app",
1569 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1570
1571 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
1572 g_param_spec_boolean("clip-mode", "clip-mode",
1573 "When seeking, Content is moving faster for a while to skip frames.",
1574 FALSE,
1575 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
1576}
1577#endif