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