blob: 505fc68322d7b2ee53336404b6374e2e6e164b56 [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
zengliang.lidcd41462024-06-19 16:05:12 +080039#define GST_AML_V4L2_CC_IMPORT_QUARK gst_aml_v4l2_buffer_pool_cc_import_quark ()
40
hanghang.luo36df2852022-08-24 15:02:27 +080041#ifndef ABSDIFF
42#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
43#endif
44
xuesong.jiang61ea8012022-05-12 15:38:17 +080045#if GST_IMPORT_LGE_PROP
46typedef struct _GstAmlResourceInfo
47{
48 gchar *coretype;
49 gint videoport;
50 gint audioport;
51 gint maxwidth;
52 gint maxheight;
53 gint mixerport;
54} GstAmlResourceInfo;
55
56struct _GstAmlV4l2VideoDecLgeCtxt
57{
58 GstAmlResourceInfo res_info;
59 guint64 dec_size;
60 guint64 undec_size;
61 gchar *app_type;
62 gboolean clip_mode;
63};
64#endif
65
xuesong.jiangae1548e2022-05-06 16:38:46 +080066typedef struct
67{
68 gchar *device;
69 GstCaps *sink_caps;
70 GstCaps *src_caps;
71 const gchar *longname;
72 const gchar *description;
73} GstAmlV4l2VideoDecCData;
74
75enum
76{
77 PROP_0,
xuesong.jiang61ea8012022-05-12 15:38:17 +080078 V4L2_STD_OBJECT_PROPS,
79#if GST_IMPORT_LGE_PROP
80 LGE_RESOURCE_INFO,
81 LGE_DECODE_SIZE,
82 LGE_UNDECODE_SIZE,
83 LGE_APP_TYPE,
84 LGE_CLIP_MODE
85#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +080086};
87
xuesong.jiang406ee302023-06-28 03:45:22 +000088enum
89{
90 SIGNAL_DECODED_PTS,
le.han7e9c27d2024-08-08 08:10:46 +000091 SIGNAL_DECODED_ERROR_PTS,
xuesong.jiang406ee302023-06-28 03:45:22 +000092 MAX_SIGNAL
93};
94
95static guint g_signals[MAX_SIGNAL]= {0};
96
xuesong.jiangae1548e2022-05-06 16:38:46 +080097#define gst_aml_v4l2_video_dec_parent_class parent_class
98G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec,
le.han02c38f02024-08-16 02:35:36 +000099 GST_AML_TYPE_VIDEO_DECODER);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800100
le.han02c38f02024-08-16 02:35:36 +0000101static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder);
xuesong.jiang61ea8012022-05-12 15:38:17 +0800102#if GST_IMPORT_LGE_PROP
103static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class);
104#endif
le.han44125c32024-09-03 07:55:57 +0000105static GstClockTime gst_aml_v4l2_video_dec_calc_output_buffer_pts(GstAmlVideoDecoder *decoder);
106static GstClockTime gst_aml_v4l2_video_dec_calc_duration(GstAmlVideoDecoder *decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800107
108static void
109gst_aml_v4l2_video_dec_set_property(GObject *object,
110 guint prop_id, const GValue *value, GParamSpec *pspec)
111{
112 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
113
114 switch (prop_id)
115 {
116 case PROP_CAPTURE_IO_MODE:
xuesong.jiangae1548e2022-05-06 16:38:46 +0800117 case PROP_DUMP_FRAME_LOCATION:
zengliang.lidcd41462024-06-19 16:05:12 +0800118 case PROP_CC_DATA:
119 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
120 prop_id, value, pspec))
121 {
122 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
123 }
124 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800125#if GST_IMPORT_LGE_PROP
126 case LGE_RESOURCE_INFO:
127 {
128 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
129 GstStructure *r_info = g_value_get_object(value);
130 if (r_info)
131 {
132 if (gst_structure_has_field(r_info, "coretype"))
133 {
134 if (self->lge_ctxt->res_info.coretype)
135 g_free(self->lge_ctxt->res_info.coretype);
136 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
137 }
138 if (gst_structure_has_field(r_info, "videoport"))
139 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
140 if (gst_structure_has_field(r_info, "audioport"))
141 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
142 if (gst_structure_has_field(r_info, "maxwidth"))
143 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
144 if (gst_structure_has_field(r_info, "maxheight"))
145 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
146 if (gst_structure_has_field(r_info, "mixerport"))
147 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
148 }
149 break;
150 }
151 case LGE_APP_TYPE:
152 {
153 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
154 if (self->lge_ctxt->app_type)
155 g_free(self->lge_ctxt->app_type);
156 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
157 break;
158 }
159 case LGE_CLIP_MODE:
160 {
161 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
162 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
163 break;
164 }
165#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800166 /* By default, only set on output */
167 default:
168 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output,
169 prop_id, value, pspec))
170 {
171 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
172 }
173 break;
174 }
175}
176
177static void
178gst_aml_v4l2_video_dec_get_property(GObject *object,
179 guint prop_id, GValue *value, GParamSpec *pspec)
180{
181 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
182
183 switch (prop_id)
184 {
185 case PROP_CAPTURE_IO_MODE:
zengliang.lidcd41462024-06-19 16:05:12 +0800186 case PROP_CC_DATA:
fei.dengaf682762024-06-24 19:06:03 +0800187 case PROP_DECODING_ERROR_FRAMES:
zengliang.lidcd41462024-06-19 16:05:12 +0800188 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
189 prop_id, value, pspec))
190 {
191 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
192 }
193 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800194#if GST_IMPORT_LGE_PROP
195 case LGE_DECODE_SIZE:
196 {
197 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
198 self->lge_ctxt->dec_size = -1;
199 g_value_set_int(value, self->lge_ctxt->dec_size);
200 break;
201 }
202 case LGE_UNDECODE_SIZE:
203 {
204 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
205 self->lge_ctxt->undec_size = -1;
206 g_value_set_int(value, self->lge_ctxt->undec_size);
207 break;
208 }
209#endif
210
xuesong.jiangae1548e2022-05-06 16:38:46 +0800211 /* By default read from output */
212 default:
213 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output,
214 prop_id, value, pspec))
215 {
216 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
217 }
218 break;
219 }
220}
221
222static gboolean
le.han02c38f02024-08-16 02:35:36 +0000223gst_aml_v4l2_video_dec_open(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800224{
225 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
226 GstCaps *codec_caps;
227
228 GST_DEBUG_OBJECT(self, "Opening");
229
230 if (!gst_aml_v4l2_object_open(self->v4l2output))
231 goto failure;
232
233 if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output))
234 goto failure;
235
236 codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad);
237 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output,
238 codec_caps);
239 gst_caps_unref(codec_caps);
240
241 if (gst_caps_is_empty(self->probed_sinkcaps))
242 goto no_encoded_format;
243
244 return TRUE;
245
246no_encoded_format:
247 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
248 (_("Decoder on device %s has no supported input format"),
249 self->v4l2output->videodev),
250 (NULL));
251 goto failure;
252
253failure:
254 if (GST_AML_V4L2_IS_OPEN(self->v4l2output))
255 gst_aml_v4l2_object_close(self->v4l2output);
256
257 if (GST_AML_V4L2_IS_OPEN(self->v4l2capture))
258 gst_aml_v4l2_object_close(self->v4l2capture);
259
260 gst_caps_replace(&self->probed_srccaps, NULL);
261 gst_caps_replace(&self->probed_sinkcaps, NULL);
262
263 return FALSE;
264}
265
266static gboolean
le.han02c38f02024-08-16 02:35:36 +0000267gst_aml_v4l2_video_dec_close(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800268{
269 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
270
271 GST_DEBUG_OBJECT(self, "Closing");
272
273 gst_aml_v4l2_object_close(self->v4l2output);
274 gst_aml_v4l2_object_close(self->v4l2capture);
275 gst_caps_replace(&self->probed_srccaps, NULL);
276 gst_caps_replace(&self->probed_sinkcaps, NULL);
277
278 return TRUE;
279}
280
281static gboolean
le.han02c38f02024-08-16 02:35:36 +0000282gst_aml_v4l2_video_dec_start(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800283{
284 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
285
286 GST_DEBUG_OBJECT(self, "Starting");
287
fei.deng6bccf822024-08-22 10:05:44 +0800288 gst_aml_v4l2_object_flush_start(self->v4l2output);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800289 g_atomic_int_set(&self->active, TRUE);
290 self->output_flow = GST_FLOW_OK;
291
292 return TRUE;
293}
294
295static gboolean
le.han02c38f02024-08-16 02:35:36 +0000296gst_aml_v4l2_video_dec_stop(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800297{
298 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
299
300 GST_DEBUG_OBJECT(self, "Stopping");
301
fei.deng6bccf822024-08-22 10:05:44 +0800302 gst_aml_v4l2_object_flush_start(self->v4l2output);
303 gst_aml_v4l2_object_flush_start(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800304
305 /* Wait for capture thread to stop */
306 gst_pad_stop_task(decoder->srcpad);
307
le.han02c38f02024-08-16 02:35:36 +0000308 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800309 self->output_flow = GST_FLOW_OK;
le.han02c38f02024-08-16 02:35:36 +0000310 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800311
312 /* Should have been flushed already */
313 g_assert(g_atomic_int_get(&self->active) == FALSE);
314
315 gst_aml_v4l2_object_stop(self->v4l2output);
316 gst_aml_v4l2_object_stop(self->v4l2capture);
317
318 if (self->input_state)
319 {
le.han02c38f02024-08-16 02:35:36 +0000320 gst_aml_video_codec_state_unref(self->input_state);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800321 self->input_state = NULL;
322 }
323
324 GST_DEBUG_OBJECT(self, "Stopped");
325
326 return TRUE;
327}
328
329static gboolean
le.han02c38f02024-08-16 02:35:36 +0000330gst_aml_v4l2_video_dec_codec_chg(GstAmlVideoDecoder *decoder,
331 GstAmlVideoCodecState *state)
hanghang.luo8e1225b2023-10-10 08:54:28 +0000332{
333 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
334 GstStructure *s_old = NULL;
335 GstStructure *s_new = NULL;
336
337 // first play, must set foramt;
338 if (!self->input_state)
339 return TRUE;
340
341 if (self->input_state->caps)
342 s_old = gst_caps_get_structure(self->input_state->caps,0);
343 if (state->caps)
344 s_new = gst_caps_get_structure(state->caps,0);
345
346 if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old)))
347 return TRUE;
348 return FALSE;
349}
350
351static gboolean
le.han02c38f02024-08-16 02:35:36 +0000352gst_aml_v4l2_video_dec_res_chg(GstAmlVideoDecoder *decoder,
353 GstAmlVideoCodecState *state)
hanghang.luo8e1225b2023-10-10 08:54:28 +0000354{
355 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
356 gboolean ret = FALSE;
357 gint width_new = -1,height_new = -1,width_old = -1,height_old = -1;
358 GstStructure *s_old = NULL;
359 GstStructure *s_new = NULL;
360
361 // first play, must set foramt;
362 if (!self->input_state)
363 {
364 ret = TRUE;
365 goto done;
366 }
367
368 if (self->input_state->caps)
369 s_old = gst_caps_get_structure(self->input_state->caps,0);
370 if (state->caps)
371 s_new = gst_caps_get_structure(state->caps,0);
372
373 if (s_new && gst_structure_has_field(s_new,"width") && gst_structure_has_field(s_new,"height"))
374 {
375 gst_structure_get_int(s_new,"width",&width_new);
376 gst_structure_get_int(s_new,"height",&height_new);
377 }
378 if (s_old && gst_structure_has_field(s_old,"width") && gst_structure_has_field(s_old,"height"))
379 {
380 gst_structure_get_int(s_old,"width",&width_old);
381 gst_structure_get_int(s_old,"height",&height_old);
382 }
383
384 if (width_new != width_old || height_new != height_old)
385 ret = TRUE;
386
387done:
388 GST_DEBUG_OBJECT(self, "ret is %d",ret);
389 return ret;
390}
391
392static gboolean
le.han02c38f02024-08-16 02:35:36 +0000393gst_aml_v4l2_video_dec_set_format(GstAmlVideoDecoder *decoder,
394 GstAmlVideoCodecState *state)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800395{
396 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
397 gboolean ret = TRUE;
398 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
399 GstCaps *caps;
400
401 GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps);
hanghang.luo8e1225b2023-10-10 08:54:28 +0000402 if (self->input_state)
403 {
404 if (gst_aml_v4l2_video_dec_res_chg(decoder,state) || gst_aml_v4l2_video_dec_codec_chg(decoder,state))
405 GST_DEBUG_OBJECT(self, "resolution or codec changed");
406 else
407 goto done;
408 }
409
xuesong.jiangae1548e2022-05-06 16:38:46 +0800410 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
hanghang.luoc54208e2023-09-22 02:43:54 +0000411 GstStructure *s = gst_caps_get_structure(state->caps,0);
412 if (s && gst_structure_has_field(s,"format"))
413 {
414 if (!strcmp("XVID",gst_structure_get_string(s,"format")))
415 {
416 GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support");
417 ret = FALSE;
418 goto done;
419 }
420 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800421
422 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
423 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
424
425 if (self->input_state)
426 {
427 if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps))
428 {
429 GST_DEBUG_OBJECT(self, "Compatible caps");
430 goto done;
431 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800432
433 gst_aml_v4l2_video_dec_finish(decoder);
434 gst_aml_v4l2_object_stop(self->v4l2output);
435
le.han02c38f02024-08-16 02:35:36 +0000436 gst_aml_video_codec_state_unref(self->input_state);
fei.dengf78f7122024-07-15 14:44:04 +0800437 self->input_state = NULL;
438
xuesong.jiangae1548e2022-05-06 16:38:46 +0800439 /* The renegotiation flow don't blend with the base class flow. To properly
440 * stop the capture pool, if the buffers can't be orphaned, we need to
441 * reclaim our buffers, which will happend through the allocation query.
le.han02c38f02024-08-16 02:35:36 +0000442 * The allocation query is triggered by gst_aml_video_decoder_negotiate() which
xuesong.jiangae1548e2022-05-06 16:38:46 +0800443 * requires the output caps to be set, but we can't know this information
444 * as we rely on the decoder, which requires the capture queue to be
445 * stopped.
446 *
447 * To workaround this issue, we simply run an allocation query with the
448 * old negotiated caps in order to drain/reclaim our buffers. That breaks
449 * the complexity and should not have much impact in performance since the
450 * following allocation query will happen on a drained pipeline and won't
451 * block. */
452 if (self->v4l2capture->pool &&
453 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
454 {
455 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
456 if (caps)
457 {
458 GstQuery *query = gst_query_new_allocation(caps, FALSE);
459 gst_pad_peer_query(decoder->srcpad, query);
460 gst_query_unref(query);
461 gst_caps_unref(caps);
462 }
463 }
464
465 gst_aml_v4l2_object_stop(self->v4l2capture);
466 self->output_flow = GST_FLOW_OK;
467 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800468 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
469 {
470 GST_ERROR_OBJECT(self, "config output drm mode error");
471 goto done;
472 }
473
xuesong.jiang22a9b112023-05-24 09:01:59 +0000474 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
475 {
476 GST_ERROR_OBJECT(self, "config output stream mode error");
477 goto done;
478 }
479
hanghang.luoac6df002024-07-11 10:54:17 +0800480 if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error))
481 {
482 GST_ERROR_OBJECT(self, "set format error");
483 goto done;
484 }
485
486 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
487 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
488 {
489 GST_ERROR_OBJECT(self, "config I frame mode error");
490 goto done;
491 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800492
493 gst_caps_replace(&self->probed_srccaps, NULL);
494 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
495 gst_aml_v4l2_object_get_raw_caps());
496
497 if (gst_caps_is_empty(self->probed_srccaps))
498 goto no_raw_format;
499
500 caps = gst_caps_copy(self->probed_srccaps);
501 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
502 gst_caps_append(self->probed_srccaps, caps);
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800503
xuesong.jiangae1548e2022-05-06 16:38:46 +0800504 if (ret)
le.han02c38f02024-08-16 02:35:36 +0000505 self->input_state = gst_aml_video_codec_state_ref(state);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800506 else
507 gst_aml_v4l2_error(self, &error);
508
509done:
510 return ret;
511
512no_raw_format:
513 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
514 (_("Decoder on device %s has no supported output format"),
515 self->v4l2output->videodev),
516 (NULL));
517 return GST_FLOW_ERROR;
518}
519
520static gboolean
le.han02c38f02024-08-16 02:35:36 +0000521gst_aml_v4l2_video_dec_flush(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800522{
523 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
524
525 GST_DEBUG_OBJECT(self, "Flushed");
526
527 /* Ensure the processing thread has stopped for the reverse playback
528 * discount case */
529 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
530 {
le.han02c38f02024-08-16 02:35:36 +0000531 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800532
fei.deng6bccf822024-08-22 10:05:44 +0800533 gst_aml_v4l2_object_flush_start(self->v4l2output);
534 gst_aml_v4l2_object_flush_start(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800535 gst_pad_stop_task(decoder->srcpad);
le.han02c38f02024-08-16 02:35:36 +0000536 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800537 }
538
539 self->output_flow = GST_FLOW_OK;
540
fei.deng6bccf822024-08-22 10:05:44 +0800541 gst_aml_v4l2_object_flush_stop(self->v4l2output);
542 gst_aml_v4l2_object_flush_stop(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800543
544 if (self->v4l2output->pool)
545 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
546
547 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
fei.deng6bccf822024-08-22 10:05:44 +0800548 * called after gst_aml_v4l2_object_flush_stop() stopped flushing the buffer
xuesong.jiangae1548e2022-05-06 16:38:46 +0800549 * pool. */
550 if (self->v4l2capture->pool)
551 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
552
553 return TRUE;
554}
555
556static gboolean
le.han02c38f02024-08-16 02:35:36 +0000557gst_aml_v4l2_video_dec_negotiate(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800558{
559 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
560
xuesong.jiang681d3602022-06-24 21:23:35 +0800561 if (TRUE == self->v4l2output->is_svp)
562 {
563 GstStructure *s;
564 GstEvent *event;
565
566 s = gst_structure_new_empty ("IS_SVP");
567 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
568 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
569 gst_pad_push_event (decoder->srcpad, event);
570 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
571 }
572
xuesong.jiangae1548e2022-05-06 16:38:46 +0800573 /* We don't allow renegotiation without carefull disabling the pool */
574 if (self->v4l2capture->pool &&
575 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
576 return TRUE;
577
le.han02c38f02024-08-16 02:35:36 +0000578 return GST_AML_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800579}
580
581static gboolean
582gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
583{
584 struct v4l2_decoder_cmd dcmd = {
585 0,
586 };
587
588 GST_DEBUG_OBJECT(v4l2object->element,
589 "sending v4l2 decoder command %u with flags %u", cmd, flags);
590
591 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
592 return FALSE;
593
594 dcmd.cmd = cmd;
595 dcmd.flags = flags;
596 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
597 goto dcmd_failed;
598
599 return TRUE;
600
601dcmd_failed:
602 if (errno == ENOTTY)
603 {
604 GST_INFO_OBJECT(v4l2object->element,
605 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
606 cmd, flags, v4l2object->videodev, g_strerror(errno));
607 }
608 else
609 {
610 GST_ERROR_OBJECT(v4l2object->element,
611 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
612 cmd, flags, v4l2object->videodev, g_strerror(errno));
613 }
614 return FALSE;
615}
616
617static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +0000618gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800619{
620 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
621 GstFlowReturn ret = GST_FLOW_OK;
622 GstBuffer *buffer;
623
624 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
625 goto done;
626
627 GST_DEBUG_OBJECT(self, "Finishing decoding");
628
le.han02c38f02024-08-16 02:35:36 +0000629 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800630
631 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
632 {
633 GstTask *task = decoder->srcpad->task;
634
635 /* If the decoder stop command succeeded, just wait until processing is
636 * finished */
637 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
638 GST_OBJECT_LOCK(task);
639 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
640 GST_TASK_WAIT(task);
641 GST_OBJECT_UNLOCK(task);
642 ret = GST_FLOW_FLUSHING;
643 }
644 else
645 {
646 /* otherwise keep queuing empty buffers until the processing thread has
647 * stopped, _pool_process() will return FLUSHING when that happened */
648 while (ret == GST_FLOW_OK)
649 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800650 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800651 buffer = gst_buffer_new();
652 ret =
653 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
654 gst_buffer_unref(buffer);
655 }
656 }
657
658 /* and ensure the processing thread has stopped in case another error
659 * occured. */
fei.deng6bccf822024-08-22 10:05:44 +0800660 gst_aml_v4l2_object_flush_start(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800661 gst_pad_stop_task(decoder->srcpad);
le.han02c38f02024-08-16 02:35:36 +0000662 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800663
664 if (ret == GST_FLOW_FLUSHING)
665 ret = self->output_flow;
666
fei.deng4244a262024-07-31 14:07:14 +0800667 /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop.
668 should reset need_wait_event=true to wait source change event*/
669 self->v4l2capture->need_wait_event = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800670 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
671
672 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
673
674done:
675 return ret;
676}
677
678static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +0000679gst_aml_v4l2_video_dec_drain(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800680{
681 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
682
683 GST_DEBUG_OBJECT(self, "Draining...");
684 gst_aml_v4l2_video_dec_finish(decoder);
685 gst_aml_v4l2_video_dec_flush(decoder);
686
687 return GST_FLOW_OK;
688}
689
le.han02c38f02024-08-16 02:35:36 +0000690static GstAmlVideoCodecFrame *
691gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstAmlVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800692{
le.han02c38f02024-08-16 02:35:36 +0000693 GstAmlVideoCodecFrame *frame = NULL;
fei.dengbee20862022-06-14 14:59:48 +0800694 GList *frames, *l;
695 gint count = 0;
696
xuesong.jiange24aef92023-06-16 06:39:10 +0000697 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
698
le.han02c38f02024-08-16 02:35:36 +0000699 frames = gst_aml_video_decoder_get_frames(decoder);
fei.dengbee20862022-06-14 14:59:48 +0800700
701 for (l = frames; l != NULL; l = l->next)
702 {
le.han02c38f02024-08-16 02:35:36 +0000703 GstAmlVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000704
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800705 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800706 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800707 }
fei.dengbee20862022-06-14 14:59:48 +0800708 count++;
709 }
710
zengliang.liddee2da2023-07-14 07:27:05 +0000711 if (!frame)
712 {
713 for (l = frames; l != NULL; l = l->next)
714 {
le.han02c38f02024-08-16 02:35:36 +0000715 GstAmlVideoCodecFrame *f = l->data;
zengliang.liddee2da2023-07-14 07:27:05 +0000716 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
717 {
718 frame = f;
fei.dengf78f7122024-07-15 14:44:04 +0800719 GST_DEBUG("The pts of the expected output frame is invalid");
720 break;
zengliang.liddee2da2023-07-14 07:27:05 +0000721 }
zengliang.liddee2da2023-07-14 07:27:05 +0000722 }
723 }
724
fei.dengbee20862022-06-14 14:59:48 +0800725 if (frame)
726 {
727 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800728 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
729 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
le.han02c38f02024-08-16 02:35:36 +0000730 gst_aml_video_codec_frame_ref(frame);
fei.dengbee20862022-06-14 14:59:48 +0800731 }
le.han5ab880d2024-09-03 08:55:11 +0000732 else
733 {
734 GST_LOG_OBJECT(decoder,
735 "buffer %" GST_TIME_FORMAT " unmatch, create new frame", GST_TIME_ARGS(pts));
736 frame = gst_aml_video_decoder_v4l2_new_frame (decoder);
737 }
fei.dengbee20862022-06-14 14:59:48 +0800738
le.han02c38f02024-08-16 02:35:36 +0000739 g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref);
fei.dengbee20862022-06-14 14:59:48 +0800740
741 return frame;
742}
743
le.han02c38f02024-08-16 02:35:36 +0000744static GstAmlVideoCodecFrame *
745gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstAmlVideoDecoder *decoder, GstClockTime pts)
xuesong.jiange24aef92023-06-16 06:39:10 +0000746{
le.han02c38f02024-08-16 02:35:36 +0000747 GstAmlVideoCodecFrame *frame = NULL;
xuesong.jiange24aef92023-06-16 06:39:10 +0000748 GList *frames, *l;
hanghang.luo4486d7d2024-07-24 10:06:35 +0800749 guint frames_len = 0;
xuesong.jiange24aef92023-06-16 06:39:10 +0000750 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
751
le.han02c38f02024-08-16 02:35:36 +0000752 if (!(frames = gst_aml_video_decoder_get_frames(decoder)))
hanghang.luo4486d7d2024-07-24 10:06:35 +0800753 goto done;
754
xuesong.jiange24aef92023-06-16 06:39:10 +0000755 frames_len = g_list_length(frames);
756 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
757
xuesong.jiange24aef92023-06-16 06:39:10 +0000758 for (l = frames; l != NULL; l = l->next)
759 {
le.han02c38f02024-08-16 02:35:36 +0000760 GstAmlVideoCodecFrame *f = l->data;
xuesong.jiange24aef92023-06-16 06:39:10 +0000761
762 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
763 {
764 /* found the right frame */
bo.xiao8e3054d2024-07-31 17:13:57 +0800765 GST_LOG_OBJECT(decoder,
766 "found frame %" GST_TIME_FORMAT "with pts %" GST_TIME_FORMAT,
767 GST_TIME_ARGS(f->pts), GST_TIME_ARGS(pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000768 frame = f;
769 break;
770 }
771 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
772 {
773 GST_LOG_OBJECT(decoder,
774 "stream mode drop frame %d %" GST_TIME_FORMAT,
hanghang.luoa0626d22024-07-30 16:10:10 +0800775 f->system_frame_number, GST_TIME_ARGS(f->pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000776
le.han02c38f02024-08-16 02:35:36 +0000777 gst_aml_video_codec_frame_ref(f);
778 // gst_aml_video_decoder_drop_frame(decoder, f);
779 gst_aml_video_decoder_release_frame(decoder, f);
xuesong.jiange24aef92023-06-16 06:39:10 +0000780 }
781 else
782 {
783 GST_LOG_OBJECT (decoder, "dbg");
784 }
785 }
786
787 if (frame)
788 {
789 guint l_len = 0;
le.han02c38f02024-08-16 02:35:36 +0000790 l = gst_aml_video_decoder_get_frames(decoder);
xuesong.jiange24aef92023-06-16 06:39:10 +0000791 l_len = g_list_length(l);
le.han02c38f02024-08-16 02:35:36 +0000792 g_list_free_full(l, (GDestroyNotify)gst_aml_video_codec_frame_unref);
xuesong.jiange24aef92023-06-16 06:39:10 +0000793
794 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800795 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
796 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
le.han02c38f02024-08-16 02:35:36 +0000797 gst_aml_video_codec_frame_ref(frame);
xuesong.jiange24aef92023-06-16 06:39:10 +0000798 }
799
le.han02c38f02024-08-16 02:35:36 +0000800 g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref);
xuesong.jiange24aef92023-06-16 06:39:10 +0000801
hanghang.luo4486d7d2024-07-24 10:06:35 +0800802done:
xuesong.jiange24aef92023-06-16 06:39:10 +0000803 return frame;
804}
805
le.han02c38f02024-08-16 02:35:36 +0000806static GstAmlVideoCodecFrame *
807gst_aml_v4l2_video_dec_get_right_frame(GstAmlVideoDecoder *decoder, GstClockTime pts)
xuesong.jiange24aef92023-06-16 06:39:10 +0000808{
809 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
810 if (self->v4l2output->stream_mode)
811 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
812 else
813 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
814}
815
xuesong.jiangae1548e2022-05-06 16:38:46 +0800816static gboolean
817gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
818 GstStructure *structure, gpointer user_data)
819{
820 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
821 GstVideoAlignment *align = &self->v4l2capture->align;
822 GstVideoInfo *info = &self->v4l2capture->info;
823 int width, height;
824
825 if (!gst_structure_get_int(structure, "width", &width))
826 return TRUE;
827
828 if (!gst_structure_get_int(structure, "height", &height))
829 return TRUE;
830
831 if (align->padding_left != 0 || align->padding_top != 0 ||
832 height != info->height + align->padding_bottom)
833 return TRUE;
834
835 if (height == info->height + align->padding_bottom)
836 {
837 /* Some drivers may round up width to the padded with */
838 if (width == info->width + align->padding_right)
839 gst_structure_set(structure,
840 "width", G_TYPE_INT, width - align->padding_right,
841 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
842 /* Some drivers may keep visible width and only round up bytesperline */
843 else if (width == info->width)
844 gst_structure_set(structure,
845 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
846 }
847
848 return TRUE;
849}
850
851static void
sheng.liubcf036c2022-06-21 15:55:42 +0800852gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800853{
854 struct v4l2_event evt;
855 gint ret;
856
857 memset (&evt, 0x00, sizeof (struct v4l2_event));
858 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
859 if (ret < 0)
860 {
861 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
862 return;
863 }
864
865 switch (evt.type)
866 {
867 case V4L2_EVENT_SOURCE_CHANGE:
868 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
869 break;
870 case V4L2_EVENT_EOS:
871 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
872 break;
873 default:
874 break;
875 }
876
877 return;
878}
879
880static void
le.han02c38f02024-08-16 02:35:36 +0000881gst_aml_v4l2_video_dec_set_output_status(GstAmlVideoDecoder *decoder,GstVideoInfo info)
hanghang.luo2eec4892023-07-18 06:44:42 +0000882{
883 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
le.han02c38f02024-08-16 02:35:36 +0000884 GstAmlVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000885 struct v4l2_selection sel;
886 struct v4l2_rect *r = NULL;
887 GstStructure *s;
888 gint width = 0;
889 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800890 GST_DEBUG("%d %d",info.width, info.height);
le.han02c38f02024-08-16 02:35:36 +0000891 output_state = gst_aml_video_decoder_set_output_state(decoder,
hanghang.luo2eec4892023-07-18 06:44:42 +0000892 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000893 memset(&sel, 0, sizeof(struct v4l2_selection));
894 sel.type = self->v4l2capture->type;
895 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
896 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
897 {
898 r = &sel.r;
899 width = (r->width/2)*2;
900 height = (r->height/2)*2;
901 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
902 }
903 else
904 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000905 if (output_state)
906 {
907 output_state->info.interlace_mode = info.interlace_mode;
908 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000909 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000910 s = gst_caps_get_structure(output_state->caps, 0);
911 if (s)
912 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800913 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
914 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
915 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
916 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000917 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
le.han02c38f02024-08-16 02:35:36 +0000918 gst_aml_video_codec_state_unref(output_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000919 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000920 }
921}
922
zengliang.lidcd41462024-06-19 16:05:12 +0800923static GQuark
924gst_aml_v4l2_buffer_pool_cc_import_quark (void)
925{
926 static GQuark quark = 0;
927
928 if (quark == 0)
929 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
930
931 return quark;
932}
933
934static gboolean
le.han02c38f02024-08-16 02:35:36 +0000935foreach_cc_buffer_list_match_pts_func(GList *list , GstAmlVideoCodecFrame *frame)
zengliang.lidcd41462024-06-19 16:05:12 +0800936{
937 GList *l;
938 if (g_list_length (list) > 0)
939 {
940 for (l = list; l != NULL; l = l->next)
941 {
942 GstBuffer *cc_buffer = l->data;
943 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
944 {
945 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
946 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
947 #if 0
948 //Debug code:dump cc data
949 GstMapInfo gst_map;
950 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
951 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
952 if (gst_map.size>0)
953 write(fd,gst_map.data,gst_map.size);
954 close(fd);
955 gst_buffer_unmap(cc_buffer,&gst_map);
956 #endif
957 GST_DEBUG("match success");
958 return TRUE;
959 }
960 else
961 {
962 GST_DEBUG("match fail");
963 }
964 }
965 GST_WARNING("no match frame in the bufferlist");
966 }
967 else
968 {
969 GST_WARNING("list is null,can not foreach");
970 }
971 return FALSE;
972}
973
bo.xiao8e3054d2024-07-31 17:13:57 +0800974static GstClockTime
le.han44125c32024-09-03 07:55:57 +0000975gst_aml_v4l2_video_dec_calc_output_buffer_pts(GstAmlVideoDecoder *decoder)
bo.xiao8e3054d2024-07-31 17:13:57 +0800976{
977 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
978 GstClockTime pts = GST_CLOCK_TIME_NONE;
979
980 if (GST_CLOCK_TIME_IS_VALID (self->last_out_pts) && GST_CLOCK_TIME_IS_VALID(self->frame_duration)) {
981 pts = self->last_out_pts + self->frame_duration;
982 GST_LOG_OBJECT (decoder,
983 "calculate PTS %" GST_TIME_FORMAT " by duration: %" GST_TIME_FORMAT,
984 GST_TIME_ARGS (pts), GST_TIME_ARGS (self->frame_duration));
985 }
986 else
987 {
988 pts = 0;
989 GST_INFO_OBJECT (decoder,"Set PTS=0");
990 }
991 return pts;
992}
993
994static GstClockTime
le.han44125c32024-09-03 07:55:57 +0000995gst_aml_v4l2_video_dec_calc_duration(GstAmlVideoDecoder *decoder)
bo.xiao8e3054d2024-07-31 17:13:57 +0800996{
997 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
998 GstClockTime duration = GST_CLOCK_TIME_NONE;
999 gint fps_n, fps_d;
1000 fps_n = gst_value_get_fraction_numerator(self->v4l2capture->fps);
1001 fps_d = gst_value_get_fraction_denominator(self->v4l2capture->fps);
1002
1003 if (fps_n != 0 && fps_d != 0)
1004 {
1005 duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
1006
1007 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1008 {
1009 duration = duration / 2;
1010 }
1011 }
1012
1013 GST_INFO_OBJECT (decoder,"framerate(fps_d: %d, fps_n: %d) -> frame_duration = %" GST_TIME_FORMAT, fps_d, fps_n, GST_TIME_ARGS(duration));
1014
1015 return duration;
1016}
1017
hanghang.luo2eec4892023-07-18 06:44:42 +00001018static void
le.han02c38f02024-08-16 02:35:36 +00001019gst_aml_v4l2_video_dec_loop(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001020{
1021 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1022 GstAmlV4l2BufferPool *v4l2_pool;
1023 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1024 GstBufferPool *pool;
le.han02c38f02024-08-16 02:35:36 +00001025 GstAmlVideoCodecFrame *frame;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001026 GstBuffer *buffer = NULL;
1027 GstFlowReturn ret;
1028
1029 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1030 {
1031 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001032 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001033 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001034 GST_DEBUG_OBJECT(self, "waitting source change event");
1035 /* Wait until received SOURCE_CHANGE event to get right video format */
1036 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1037 {
1038 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1039 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1040 {
fei.deng2a06e042023-10-10 03:09:45 +00001041 //let flush start event blocked until capture buffer pool actived
1042 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001043 GST_DEBUG_OBJECT(self, "Received source change event");
1044 break;
1045 }
1046 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1047 {
1048 GST_DEBUG_OBJECT(self, "Received eos event");
1049 goto beach;
1050 }
1051 else if (ret != GST_FLOW_OK)
1052 {
1053 GST_ERROR_OBJECT(self, "dqevent error");
1054 goto beach;
1055 }
1056 }
1057 self->v4l2capture->need_wait_event = FALSE;
1058
sheng.liu0c77f6c2022-06-17 21:33:20 +08001059 if (TRUE == self->v4l2output->is_svp)
1060 {
1061 GstPad *peer;
1062 GstStructure *s;
1063 GstEvent *event;
1064
1065 peer = gst_pad_get_peer (decoder->srcpad);
1066 if (peer)
1067 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001068 s = gst_structure_new_empty ("IS_SVP");
1069 if (s)
1070 {
1071 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1072 gst_pad_send_event (peer, event);
1073 GST_DEBUG_OBJECT(self, "Send SVP Event");
1074 }
1075 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001076 }
1077 }
1078
sheng.liub56bbc52022-06-21 11:02:33 +08001079 if (self->v4l2capture->need_drop_event)
1080 {
1081 // drop V4L2_EVENT_SOURCE_CHANGE
1082 gst_v4l2_drop_event(self->v4l2capture);
1083 self->v4l2capture->need_drop_event = FALSE;
1084 }
1085
xuesong.jiangae1548e2022-05-06 16:38:46 +08001086 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1087 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001088
xuesong.jiangae1548e2022-05-06 16:38:46 +08001089 /* Create caps from the acquired format, remove the format field */
1090 acquired_caps = gst_video_info_to_caps(&info);
1091 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1092 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001093 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1094
1095 /* Probe currently available pixel formats */
1096 available_caps = gst_caps_copy(self->probed_srccaps);
1097 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1098
1099 /* Replace coded size with visible size, we want to negotiate visible size
1100 * with downstream, not coded size. */
1101 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1102
1103 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001104 caps = gst_caps_copy(filter);
1105 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1106 gst_caps_append(filter, caps);
1107
1108 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1109 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001110 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001111 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1112 gst_caps_unref(filter);
1113
1114 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1115 if (gst_caps_is_empty(caps))
1116 {
1117 gst_caps_unref(caps);
1118 goto not_negotiated;
1119 }
1120
1121 /* Fixate pixel format */
1122 caps = gst_caps_fixate(caps);
1123
1124 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1125
1126 /* Try to set negotiated format, on success replace acquired format */
1127 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1128 gst_video_info_from_caps(&info, caps);
1129 else
1130 gst_aml_v4l2_clear_error(&error);
1131 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001132 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
le.han02c38f02024-08-16 02:35:36 +00001133 if (!gst_aml_video_decoder_negotiate(decoder))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001134 {
1135 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1136 goto flushing;
1137 else
1138 goto not_negotiated;
1139 }
1140
1141 /* Ensure our internal pool is activated */
1142 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1143 TRUE))
1144 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001145
bo.xiao8e3054d2024-07-31 17:13:57 +08001146 //cal duration when got resolution event
le.han44125c32024-09-03 07:55:57 +00001147 self->frame_duration = gst_aml_v4l2_video_dec_calc_duration(decoder);
bo.xiao8e3054d2024-07-31 17:13:57 +08001148
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001149 g_mutex_lock(&self->res_chg_lock);
1150 GST_LOG_OBJECT(decoder, "signal resolution changed");
1151 self->is_res_chg = FALSE;
1152 g_cond_signal(&self->res_chg_cond);
1153 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001154 }
1155
1156 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1157
1158 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1159
1160 self->output_flow = GST_FLOW_OK;
1161 do
1162 {
1163 /* We cannot use the base class allotate helper since it taking the internal
1164 * stream lock. we know that the acquire may need to poll until more frames
1165 * comes in and holding this lock would prevent that.
1166 */
le.han02c38f02024-08-16 02:35:36 +00001167 pool = gst_aml_video_decoder_get_buffer_pool(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001168
1169 /* Pool may be NULL if we started going to READY state */
1170 if (pool == NULL)
1171 {
le.han02c38f02024-08-16 02:35:36 +00001172 GST_WARNING_OBJECT(decoder, "gst_aml_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001173 ret = GST_FLOW_FLUSHING;
1174 goto beach;
1175 }
1176
1177 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001178
xuesong.jiangae1548e2022-05-06 16:38:46 +08001179 g_object_unref(pool);
1180
fei.deng9a5cd6e2023-06-30 12:09:18 +00001181 if (ret == GST_FLOW_OK && GST_BUFFER_FLAG_IS_SET(buffer,GST_AML_V4L2_BUFFER_FLAG_LAST_EMPTY)) {
fei.deng990965a2023-11-15 07:03:15 +00001182 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1183 self->v4l2capture->need_drop_event = TRUE;
1184 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1185 if (self->is_res_chg) {
1186 //we must release last buffer
1187 gst_buffer_unref(buffer);
1188 //if resolution changed event received,we should set need_drop_event to false
1189 self->v4l2capture->need_drop_event = FALSE;
1190 gst_aml_v4l2_object_stop(self->v4l2capture);
1191 //unblock flush start event
1192 g_mutex_lock(&self->res_chg_lock);
1193 self->is_res_chg = FALSE;
1194 g_cond_signal(&self->res_chg_cond);
1195 g_mutex_unlock(&self->res_chg_lock);
1196 return;
1197 } else {
1198 goto beach;
1199 }
sheng.liub56bbc52022-06-21 11:02:33 +08001200 }
1201
zengliang.lidcd41462024-06-19 16:05:12 +08001202 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1203 {
bo.xiaoe978c3b2024-08-01 20:57:15 +08001204 GST_DEBUG_OBJECT(decoder, "already got cc data, just continue.");
1205 continue;
1206 }
1207
bo.xiaob6afda52024-08-02 16:08:30 +08001208 if (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT)
bo.xiaoe978c3b2024-08-01 20:57:15 +08001209 {
1210 GST_DEBUG_OBJECT(decoder, "unknow event, just continue.");
zengliang.lidcd41462024-06-19 16:05:12 +08001211 continue;
1212 }
1213
sheng.liu8d18ed22022-05-26 17:28:15 +08001214 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1215 {
1216 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001217
1218 g_mutex_lock (&self->res_chg_lock);
1219 self->is_res_chg = TRUE;
1220 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001221 return;
1222 }
hanghang.luo474440d2024-08-16 10:18:43 +08001223
fei.dengaf682762024-06-24 19:06:03 +08001224 //decoding error happened
1225 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1226 {
le.han5967dcf2024-08-21 08:50:56 +00001227 GST_DEBUG("send error pts:%llu - %" GST_TIME_FORMAT, v4l2_pool->obj->error_frame_pts, GST_TIME_ARGS(v4l2_pool->obj->error_frame_pts));
1228 g_signal_emit (self, g_signals[SIGNAL_DECODED_ERROR_PTS], 0, v4l2_pool->obj->error_frame_pts, NULL);
hanghang.luo474440d2024-08-16 10:18:43 +08001229 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, v4l2_pool->obj->error_frame_pts);
fei.dengaf682762024-06-24 19:06:03 +08001230 continue;
1231 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001232
fei.dengbee20862022-06-14 14:59:48 +08001233 if (ret != GST_FLOW_OK) {
1234 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001235 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001236 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001237
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001238 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001239 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001240
bo.xiao8e3054d2024-07-31 17:13:57 +08001241 GST_DEBUG_OBJECT(decoder, "decoded pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
xuesong.jiang406ee302023-06-28 03:45:22 +00001242 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1243
xuesong.jiangae1548e2022-05-06 16:38:46 +08001244 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1245 {
1246 gst_aml_v4l2_object_stop(self->v4l2capture);
1247 return;
1248 }
1249
fei.dengaf682762024-06-24 19:06:03 +08001250 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1251 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
bo.xiaob6afda52024-08-02 16:08:30 +08001252 (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT) ||
fei.dengaf682762024-06-24 19:06:03 +08001253 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001254
1255 if (ret != GST_FLOW_OK)
1256 goto beach;
1257
le.han44125c32024-09-03 07:55:57 +00001258 if (!GST_BUFFER_PTS_IS_VALID (buffer)
1259 || (GST_BUFFER_TIMESTAMP(buffer) == 0 && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED))
bo.xiao8e3054d2024-07-31 17:13:57 +08001260 {
le.han44125c32024-09-03 07:55:57 +00001261 GST_BUFFER_TIMESTAMP(buffer) = gst_aml_v4l2_video_dec_calc_output_buffer_pts(decoder);
bo.xiao8e3054d2024-07-31 17:13:57 +08001262 }
1263
1264 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1265 {
1266 GST_BUFFER_DURATION(buffer) = self->frame_duration; // got at resolution event.
1267 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1268 }
1269
fei.dengbee20862022-06-14 14:59:48 +08001270 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001271 if (frame)
1272 {
fei.dengccc89632022-07-15 19:10:17 +08001273 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001274 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001275 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1276 frame->duration = GST_BUFFER_DURATION(buffer);
bo.xiao8e3054d2024-07-31 17:13:57 +08001277
xuesong.jiangae1548e2022-05-06 16:38:46 +08001278 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001279
zengliang.lidcd41462024-06-19 16:05:12 +08001280 if (self->v4l2capture->enable_cc_data)
1281 {
1282 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1283 {
1284 GST_DEBUG("cc buffer and frame bind success");
1285 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1286 GST_AML_V4L2_CC_IMPORT_QUARK);
1287 #if 0
1288 //Debug code:dump cc data
1289 GstMapInfo gst_map;
1290 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1291 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1292 if (gst_map.size>0)
1293 write(fd,gst_map.data,gst_map.size);
1294 close(fd);
1295 gst_buffer_unmap(cc_buffer,&gst_map);
1296 #endif
1297 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1298 gst_buffer_unref(cc_buffer);
1299 }
1300 else
1301 {
1302 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1303 }
1304 }
le.han02c38f02024-08-16 02:35:36 +00001305 ret = gst_aml_video_decoder_finish_frame(decoder, frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001306
1307 if (ret != GST_FLOW_OK)
1308 goto beach;
1309 }
1310 else
1311 {
bo.xiao8e3054d2024-07-31 17:13:57 +08001312 GST_WARNING_OBJECT(decoder, "Unmatch buffer, should be push, need refine");
1313 //gst_pad_push (decoder->srcpad, buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001314 gst_buffer_unref(buffer);
1315 }
1316
1317 return;
1318 /* ERRORS */
1319not_negotiated:
1320{
1321 GST_ERROR_OBJECT(self, "not negotiated");
1322 ret = GST_FLOW_NOT_NEGOTIATED;
1323 goto beach;
1324}
1325activate_failed:
1326{
1327 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1328 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1329 (_("Failed to allocate required memory.")),
1330 ("Buffer pool activation failed"));
1331 ret = GST_FLOW_ERROR;
1332 goto beach;
1333}
1334flushing:
1335{
1336 ret = GST_FLOW_FLUSHING;
1337 goto beach;
1338}
1339beach:
1340 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1341 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001342 if (self->is_res_chg) {
1343 //unblock flush start event
1344 g_mutex_lock(&self->res_chg_lock);
1345 self->is_res_chg = FALSE;
1346 g_cond_signal(&self->res_chg_cond);
1347 g_mutex_unlock(&self->res_chg_lock);
1348 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001349 gst_buffer_replace(&buffer, NULL);
1350 self->output_flow = ret;
fei.deng6bccf822024-08-22 10:05:44 +08001351 gst_aml_v4l2_object_flush_start(self->v4l2output);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001352 gst_pad_pause_task(decoder->srcpad);
1353}
1354
1355static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +00001356gst_aml_v4l2_video_dec_handle_frame(GstAmlVideoDecoder *decoder,
1357 GstAmlVideoCodecFrame *frame)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001358{
1359 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1360 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1361 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1362 GstFlowReturn ret = GST_FLOW_OK;
1363 gboolean processed = FALSE;
1364 GstBuffer *tmp;
1365 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001366
1367 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1368
1369 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1370 goto flushing;
1371
le.han44125c32024-09-03 07:55:57 +00001372 if (G_UNLIKELY(!GST_CLOCK_TIME_IS_VALID(self->frame_duration)))
1373 self->frame_duration = frame->duration;
1374
xuesong.jiangae1548e2022-05-06 16:38:46 +08001375 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1376 {
1377 if (!self->input_state)
1378 goto not_negotiated;
1379 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1380 &error))
1381 goto not_negotiated;
1382 }
1383
1384 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1385 {
1386 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001387 GstCapsFeatures *features = NULL;
1388
1389 features = gst_caps_get_features(self->input_state->caps, 0);
xuesong.jiang26749f32024-07-24 09:40:47 +08001390 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
sheng.liu0c77f6c2022-06-17 21:33:20 +08001391 {
1392 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang26749f32024-07-24 09:40:47 +08001393 //TODO:need rm is_svp flag and just using secure_es flag
sheng.liu0c77f6c2022-06-17 21:33:20 +08001394 self->v4l2output->is_svp = TRUE;
1395 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001396
1397 GST_DEBUG_OBJECT(self, "Sending header");
1398
1399 codec_data = self->input_state->codec_data;
1400
1401 /* We are running in byte-stream mode, so we don't know the headers, but
1402 * we need to send something, otherwise the decoder will refuse to
1403 * intialize.
1404 */
1405 if (codec_data)
1406 {
1407 gst_buffer_ref(codec_data);
1408 }
1409 else
1410 {
1411 codec_data = gst_buffer_ref(frame->input_buffer);
1412 processed = TRUE;
1413 }
1414
1415 /* Ensure input internal pool is active */
1416 if (!gst_buffer_pool_is_active(pool))
1417 {
1418 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001419 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1420 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001421 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1422 // self->v4l2output->info.size, min, max);
1423 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1424
1425 /* There is no reason to refuse this config */
1426 if (!gst_buffer_pool_set_config(pool, config))
1427 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001428 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001429
1430 if (!gst_buffer_pool_set_active(pool, TRUE))
1431 goto activate_failed;
1432 }
1433
le.han02c38f02024-08-16 02:35:36 +00001434 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001435 ret =
1436 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001437 self->codec_data_inject = TRUE;
le.han02c38f02024-08-16 02:35:36 +00001438 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001439
1440 gst_buffer_unref(codec_data);
1441
1442 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1443 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1444 * and returns the visible size as with/height and the coded size as
1445 * padding. */
1446 }
1447
le.han02c38f02024-08-16 02:35:36 +00001448 task_state = gst_pad_get_task_state(GST_AML_VIDEO_DECODER_SRC_PAD(self));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001449 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1450 {
1451 /* It's possible that the processing thread stopped due to an error */
1452 if (self->output_flow != GST_FLOW_OK &&
1453 self->output_flow != GST_FLOW_FLUSHING)
1454 {
1455 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1456 ret = self->output_flow;
1457 goto drop;
1458 }
1459
1460 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1461
1462 /* Start the processing task, when it quits, the task will disable input
1463 * processing to unlock input if draining, or prevent potential block */
1464 self->output_flow = GST_FLOW_FLUSHING;
fei.deng6bccf822024-08-22 10:05:44 +08001465 /*reset poll and need_drop_event before start decoding loop thread*/
1466 self->v4l2capture->need_drop_event = FALSE;
1467 gst_poll_set_flushing(self->v4l2capture->poll, FALSE);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001468 if (!gst_pad_start_task(decoder->srcpad,
1469 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1470 goto start_task_failed;
1471 }
1472
1473 if (!processed)
1474 {
le.han02c38f02024-08-16 02:35:36 +00001475 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001476 if (!self->codec_data_inject && self->input_state->codec_data)
1477 {
1478 ret = gst_aml_v4l2_buffer_pool_process
1479 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1480 self->codec_data_inject = TRUE;
1481 if (ret != GST_FLOW_OK)
1482 goto send_codec_failed;
1483 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001484 ret =
1485 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
le.han02c38f02024-08-16 02:35:36 +00001486 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001487
1488 if (ret == GST_FLOW_FLUSHING)
1489 {
le.han02c38f02024-08-16 02:35:36 +00001490 if (gst_pad_get_task_state(GST_AML_VIDEO_DECODER_SRC_PAD(self)) !=
xuesong.jiangae1548e2022-05-06 16:38:46 +08001491 GST_TASK_STARTED)
1492 ret = self->output_flow;
1493 goto drop;
1494 }
1495 else if (ret != GST_FLOW_OK)
1496 {
1497 goto process_failed;
1498 }
1499 }
1500
1501 /* No need to keep input arround */
1502 tmp = frame->input_buffer;
1503 frame->input_buffer = gst_buffer_new();
1504 gst_buffer_copy_into(frame->input_buffer, tmp,
1505 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1506 GST_BUFFER_COPY_META,
1507 0, 0);
1508 gst_buffer_unref(tmp);
1509
le.han02c38f02024-08-16 02:35:36 +00001510 gst_aml_video_codec_frame_unref(frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001511 return ret;
1512
1513 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001514send_codec_failed:
1515 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1516 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001517not_negotiated:
1518{
1519 GST_ERROR_OBJECT(self, "not negotiated");
1520 ret = GST_FLOW_NOT_NEGOTIATED;
1521 gst_aml_v4l2_error(self, &error);
1522 goto drop;
1523}
1524activate_failed:
1525{
1526 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1527 (_("Failed to allocate required memory.")),
1528 ("Buffer pool activation failed"));
1529 ret = GST_FLOW_ERROR;
1530 goto drop;
1531}
1532flushing:
1533{
1534 ret = GST_FLOW_FLUSHING;
1535 goto drop;
1536}
1537
1538start_task_failed:
1539{
1540 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1541 (_("Failed to start decoding thread.")), (NULL));
1542 ret = GST_FLOW_ERROR;
1543 goto drop;
1544}
1545process_failed:
1546{
1547 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1548 (_("Failed to process frame.")),
1549 ("Maybe be due to not enough memory or failing driver"));
1550 ret = GST_FLOW_ERROR;
1551 goto drop;
1552}
1553drop:
1554{
le.han02c38f02024-08-16 02:35:36 +00001555 gst_aml_video_decoder_drop_frame(decoder, frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001556 return ret;
1557}
1558}
1559
1560static gboolean
le.han02c38f02024-08-16 02:35:36 +00001561gst_aml_v4l2_video_dec_decide_allocation(GstAmlVideoDecoder *decoder,
xuesong.jiangae1548e2022-05-06 16:38:46 +08001562 GstQuery *query)
1563{
1564 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1565 GstClockTime latency;
1566 gboolean ret = FALSE;
1567
1568 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
le.han02c38f02024-08-16 02:35:36 +00001569 ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001570
1571 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1572 {
1573 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1574 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1575 self->v4l2capture->min_buffers, self->v4l2capture->duration);
le.han02c38f02024-08-16 02:35:36 +00001576 gst_aml_video_decoder_set_latency(decoder, latency, latency);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001577 }
1578 else
1579 {
1580 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1581 }
1582
1583 return ret;
1584}
1585
1586static gboolean
le.han02c38f02024-08-16 02:35:36 +00001587gst_aml_v4l2_video_dec_src_query(GstAmlVideoDecoder *decoder, GstQuery *query)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001588{
1589 gboolean ret = TRUE;
1590 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1591
1592 switch (GST_QUERY_TYPE(query))
1593 {
1594 case GST_QUERY_CAPS:
1595 {
1596 GstCaps *filter, *result = NULL;
le.han02c38f02024-08-16 02:35:36 +00001597 GstPad *pad = GST_AML_VIDEO_DECODER_SRC_PAD(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001598
1599 gst_query_parse_caps(query, &filter);
1600
1601 if (self->probed_srccaps)
1602 result = gst_caps_ref(self->probed_srccaps);
1603 else
1604 result = gst_pad_get_pad_template_caps(pad);
1605
1606 if (filter)
1607 {
1608 GstCaps *tmp = result;
1609 result =
1610 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1611 gst_caps_unref(tmp);
1612 }
1613
1614 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1615
1616 gst_query_set_caps_result(query, result);
1617 gst_caps_unref(result);
1618 break;
1619 }
1620
1621 default:
le.han02c38f02024-08-16 02:35:36 +00001622 ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001623 break;
1624 }
1625
1626 return ret;
1627}
1628
1629static GstCaps *
le.han02c38f02024-08-16 02:35:36 +00001630gst_aml_v4l2_video_dec_sink_getcaps(GstAmlVideoDecoder *decoder, GstCaps *filter)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001631{
1632 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1633 GstCaps *result;
1634
le.han02c38f02024-08-16 02:35:36 +00001635 result = gst_aml_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
xuesong.jiangae1548e2022-05-06 16:38:46 +08001636 filter);
1637
1638 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1639
1640 return result;
1641}
1642
1643static gboolean
le.han02c38f02024-08-16 02:35:36 +00001644gst_aml_v4l2_video_dec_sink_event(GstAmlVideoDecoder *decoder, GstEvent *event)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001645{
1646 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1647 gboolean ret;
1648 GstEventType type = GST_EVENT_TYPE(event);
bo.xiao8e3054d2024-07-31 17:13:57 +08001649 GST_DEBUG_OBJECT (self, "received event %p %" GST_PTR_FORMAT, event, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001650
1651 switch (type)
1652 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001653 case GST_EVENT_STREAM_START:
1654 {
1655 GstStructure *s;
1656 GstEvent *event;
xuesong.jiang406ee302023-06-28 03:45:22 +00001657 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1658 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1659 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1660 gst_pad_push_event (decoder->sinkpad, event);
1661 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1662 break;
1663 }
zengliang.li51f54b62023-10-10 12:14:49 +00001664 case GST_EVENT_CAPS:
1665 {
1666 GstCaps *caps;
1667 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001668 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001669
1670 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001671
zengliang.li51f54b62023-10-10 12:14:49 +00001672 structure= gst_caps_get_structure(caps, 0);
1673 if ( gst_structure_has_field(structure, "parsed") )
1674 {
1675 gboolean parsed = TRUE;
1676 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1677 {
1678 self->v4l2output->stream_mode = !parsed;
1679 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1680 }
1681 }
sheng.liu641aa422023-12-26 07:05:59 +00001682
xuesong.jiang26749f32024-07-24 09:40:47 +08001683 if ( gst_structure_has_field(structure, "secure") )
1684 {
1685 gboolean is_secure = FALSE;
1686 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1687 {
1688 self->v4l2output->secure_es = is_secure;
1689 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1690 }
1691 }
xuesong.jiang578add22024-07-25 15:11:30 +08001692 else
1693 {
1694 GstCapsFeatures *const features = gst_caps_get_features(caps, 0);
1695 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1696 {
1697 self->v4l2output->secure_es = TRUE;
1698 GST_DEBUG("If there is no secure field in caps, consider dma es is secure");
1699 }
1700 }
xuesong.jiang26749f32024-07-24 09:40:47 +08001701
sheng.liudb26f7d2024-01-22 11:24:23 +00001702 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1703 {
1704 if ( denom == 0 ) denom= 1;
1705
1706 if (self->v4l2capture->fps)
1707 {
1708 g_value_unset(self->v4l2capture->fps);
1709 g_free(self->v4l2capture->fps);
1710 }
1711
1712 self->v4l2capture->fps = g_new0(GValue, 1);
1713 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1714 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1715
1716 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1717 }
1718
sheng.liu641aa422023-12-26 07:05:59 +00001719 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1720 ( !self->v4l2capture->have_set_par ) )
1721 {
1722 if ( (num <= 0) || (denom <= 0) )
1723 {
1724 num= denom= 1;
1725 }
1726
1727 if ( self->v4l2capture->par )
1728 {
1729 g_value_unset(self->v4l2capture->par);
1730 g_free(self->v4l2capture->par);
1731 }
1732
1733 self->v4l2capture->par = g_new0(GValue, 1);
1734 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1735 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1736 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1737 }
zengliang.li51f54b62023-10-10 12:14:49 +00001738 break;
1739 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001740 case GST_EVENT_FLUSH_START:
1741 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001742
1743 g_mutex_lock (&self->res_chg_lock);
1744 while (self->is_res_chg)
1745 {
1746 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1747 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1748 }
1749 g_mutex_unlock (&self->res_chg_lock);
1750
zengliang.li92ff6822023-06-06 07:12:52 +00001751 self->last_out_pts = GST_CLOCK_TIME_NONE;
fei.deng6bccf822024-08-22 10:05:44 +08001752 gst_aml_v4l2_object_flush_start(self->v4l2output);
1753 gst_aml_v4l2_object_flush_start(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001754 break;
1755 default:
1756 break;
1757 }
1758
le.han02c38f02024-08-16 02:35:36 +00001759 ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001760
1761 switch (type)
1762 {
1763 case GST_EVENT_FLUSH_START:
1764 /* The processing thread should stop now, wait for it */
1765 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001766 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001767 GST_DEBUG_OBJECT(self, "flush start done");
1768 break;
1769 default:
1770 break;
1771 }
1772
1773 return ret;
1774}
1775
1776static GstStateChangeReturn
1777gst_aml_v4l2_video_dec_change_state(GstElement *element,
1778 GstStateChange transition)
1779{
1780 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
le.han02c38f02024-08-16 02:35:36 +00001781 GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(element);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001782
bo.xiao8e3054d2024-07-31 17:13:57 +08001783 GST_DEBUG_OBJECT(element, "change state from %s to %s",
1784 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1785 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1786
xuesong.jiangae1548e2022-05-06 16:38:46 +08001787 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1788 {
1789 g_atomic_int_set(&self->active, FALSE);
fei.deng6bccf822024-08-22 10:05:44 +08001790 gst_aml_v4l2_object_flush_start(self->v4l2output);
1791 gst_aml_v4l2_object_flush_start(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001792 gst_pad_stop_task(decoder->srcpad);
1793 }
1794
1795 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1796}
1797
1798static void
1799gst_aml_v4l2_video_dec_dispose(GObject *object)
1800{
1801 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1802
1803 gst_caps_replace(&self->probed_sinkcaps, NULL);
1804 gst_caps_replace(&self->probed_srccaps, NULL);
1805
1806 G_OBJECT_CLASS(parent_class)->dispose(object);
1807}
1808
1809static void
1810gst_aml_v4l2_video_dec_finalize(GObject *object)
1811{
1812 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1813
1814 gst_aml_v4l2_object_destroy(self->v4l2capture);
1815 gst_aml_v4l2_object_destroy(self->v4l2output);
1816
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001817 g_mutex_clear(&self->res_chg_lock);
1818 g_cond_clear(&self->res_chg_cond);
1819
xuesong.jiang61ea8012022-05-12 15:38:17 +08001820#if GST_IMPORT_LGE_PROP
1821 if (self->lge_ctxt)
1822 {
1823 if (self->lge_ctxt->app_type)
1824 g_free(self->lge_ctxt->app_type);
1825 if (self->lge_ctxt->res_info.coretype)
1826 g_free(self->lge_ctxt->res_info.coretype);
1827 free(self->lge_ctxt);
1828 }
1829
1830#endif
1831
xuesong.jiangae1548e2022-05-06 16:38:46 +08001832 G_OBJECT_CLASS(parent_class)->finalize(object);
1833}
1834
1835static void
1836gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1837{
1838 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001839 self->last_out_pts = GST_CLOCK_TIME_NONE;
bo.xiao8e3054d2024-07-31 17:13:57 +08001840 self->frame_duration = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001841 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001842 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001843 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001844 g_mutex_init(&self->res_chg_lock);
1845 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001846#if GST_IMPORT_LGE_PROP
1847 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1848 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1849#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001850}
1851
1852static void
1853gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1854{
1855 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1856 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
le.han02c38f02024-08-16 02:35:36 +00001857 GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(instance);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001858
le.han02c38f02024-08-16 02:35:36 +00001859 gst_aml_video_decoder_set_packetized(decoder, TRUE);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001860
1861 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
le.han02c38f02024-08-16 02:35:36 +00001862 GST_OBJECT(GST_AML_VIDEO_DECODER_SINK_PAD(self)),
xuesong.jiangae1548e2022-05-06 16:38:46 +08001863 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1864 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1865 self->v4l2output->no_initial_format = TRUE;
1866 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001867 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001868
1869 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
le.han02c38f02024-08-16 02:35:36 +00001870 GST_OBJECT(GST_AML_VIDEO_DECODER_SRC_PAD(self)),
xuesong.jiangae1548e2022-05-06 16:38:46 +08001871 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1872 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1873 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001874 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001875}
1876
1877static void
1878gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1879{
1880 GstElementClass *element_class;
1881 GObjectClass *gobject_class;
le.han02c38f02024-08-16 02:35:36 +00001882 GstAmlVideoDecoderClass *video_decoder_class;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001883
1884 parent_class = g_type_class_peek_parent(klass);
1885
1886 element_class = (GstElementClass *)klass;
1887 gobject_class = (GObjectClass *)klass;
le.han02c38f02024-08-16 02:35:36 +00001888 video_decoder_class = (GstAmlVideoDecoderClass *)klass;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001889
1890 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1891 "AML V4L2 Video Decoder");
1892
1893 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1894 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1895 gobject_class->set_property =
1896 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1897 gobject_class->get_property =
1898 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1899
1900 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1901 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1902 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1903 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1904 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1905 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1906 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1907 video_decoder_class->set_format =
1908 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1909 video_decoder_class->negotiate =
1910 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1911 video_decoder_class->decide_allocation =
1912 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1913 /* FIXME propose_allocation or not ? */
1914 video_decoder_class->handle_frame =
1915 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1916 video_decoder_class->getcaps =
1917 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1918 video_decoder_class->src_query =
1919 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1920 video_decoder_class->sink_event =
1921 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1922
1923 element_class->change_state =
1924 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1925
xuesong.jiang406ee302023-06-28 03:45:22 +00001926 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1927 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1928 G_SIGNAL_RUN_LAST,
1929 0, /* class offset */
1930 NULL, /* accumulator */
1931 NULL, /* accu data */
1932 g_cclosure_marshal_generic,
1933 G_TYPE_NONE,
1934 1,
1935 G_TYPE_UINT64);
le.han7e9c27d2024-08-08 08:10:46 +00001936 g_signals[SIGNAL_DECODED_ERROR_PTS] = g_signal_new ("decoded-error-pts",
1937 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1938 G_SIGNAL_RUN_LAST,
1939 0, /* class offset */
1940 NULL, /* accumulator */
1941 NULL, /* accu data */
1942 g_cclosure_marshal_generic,
1943 G_TYPE_NONE,
1944 1,
1945 G_TYPE_UINT64);
xuesong.jiang406ee302023-06-28 03:45:22 +00001946
xuesong.jiangae1548e2022-05-06 16:38:46 +08001947 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001948#if GST_IMPORT_LGE_PROP
1949 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1950#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001951}
1952
1953static void
1954gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1955{
1956 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1957 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1958 GstAmlV4l2VideoDecCData *cdata = data;
1959
1960 klass->default_device = cdata->device;
1961
1962 /* Note: gst_pad_template_new() take the floating ref from the caps */
1963 gst_element_class_add_pad_template(element_class,
1964 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1965 cdata->sink_caps));
1966 gst_element_class_add_pad_template(element_class,
1967 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1968 cdata->src_caps));
1969
1970 gst_element_class_set_metadata(element_class, cdata->longname,
1971 "Codec/Decoder/Video/Hardware", cdata->description,
1972 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1973
1974 gst_caps_unref(cdata->sink_caps);
1975 gst_caps_unref(cdata->src_caps);
1976 g_free(cdata);
1977}
1978
1979/* Probing functions */
1980gboolean
1981gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1982{
1983 gboolean ret = FALSE;
1984
1985 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()))
1986 ret = TRUE;
1987
1988 return ret;
1989}
1990
1991static gchar *
1992gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
1993 const gchar *basename)
1994{
1995 gchar *codec_name = NULL;
1996 gchar *type_name = NULL;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001997
1998#define SET_META(codec) \
1999 G_STMT_START \
2000 { \
2001 cdata->longname = "AML V4L2 " codec " Decoder"; \
2002 cdata->description = "Decodes " codec " streams via V4L2 API"; \
2003 codec_name = g_ascii_strdown(codec, -1); \
2004 } \
2005 G_STMT_END
2006
bo.xiaoce116a42024-07-16 16:21:53 +08002007 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08002008 {
bo.xiaoce116a42024-07-16 16:21:53 +08002009 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002010 }
2011 else if (gst_structure_has_name(s, "video/mpeg"))
2012 {
bo.xiaob6afda52024-08-02 16:08:30 +08002013 //include mpeg1, mpeg2, mpeg4
2014 SET_META("MPEG4");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002015 }
2016 else if (gst_structure_has_name(s, "video/x-h263"))
2017 {
2018 SET_META("H263");
2019 }
2020 else if (gst_structure_has_name(s, "video/x-fwht"))
2021 {
2022 SET_META("FWHT");
2023 }
2024 else if (gst_structure_has_name(s, "video/x-h264"))
2025 {
2026 SET_META("H264");
2027 }
2028 else if (gst_structure_has_name(s, "video/x-h265"))
2029 {
2030 SET_META("H265");
2031 }
2032 else if (gst_structure_has_name(s, "video/x-wmv"))
2033 {
2034 SET_META("VC1");
2035 }
2036 else if (gst_structure_has_name(s, "video/x-vp8"))
2037 {
2038 SET_META("VP8");
2039 }
2040 else if (gst_structure_has_name(s, "video/x-vp9"))
2041 {
2042 SET_META("VP9");
2043 }
2044 else if (gst_structure_has_name(s, "video/x-av1"))
2045 {
2046 SET_META("AV1");
2047 }
zengliang.li51f54b62023-10-10 12:14:49 +00002048 else if (gst_structure_has_name(s, "video/x-avs"))
2049 {
2050 SET_META("AVS");
2051 }
2052 else if (gst_structure_has_name(s, "video/x-avs2"))
2053 {
2054 SET_META("AVS2");
2055 }
2056 else if (gst_structure_has_name(s, "video/x-avs3"))
2057 {
2058 SET_META("AVS3");
2059 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002060 else if (gst_structure_has_name(s, "video/x-bayer"))
2061 {
2062 SET_META("BAYER");
2063 }
2064 else if (gst_structure_has_name(s, "video/x-sonix"))
2065 {
2066 SET_META("SONIX");
2067 }
2068 else if (gst_structure_has_name(s, "video/x-pwc1"))
2069 {
2070 SET_META("PWC1");
2071 }
2072 else if (gst_structure_has_name(s, "video/x-pwc2"))
2073 {
2074 SET_META("PWC2");
2075 }
2076 else
2077 {
2078 /* This code should be kept on sync with the exposed CODEC type of format
2079 * from gstamlv4l2object.c. This warning will only occure in case we forget
2080 * to also add a format here. */
2081 gchar *s_str = gst_structure_to_string(s);
2082 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2083 "bug, please report at https://bugs.gnome.org",
2084 s_str);
2085 g_free(s_str);
2086 }
2087
2088 if (codec_name)
2089 {
2090 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2091 if (g_type_from_name(type_name) != 0)
2092 {
2093 g_free(type_name);
2094 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2095 }
2096
2097 g_free(codec_name);
2098 }
2099
2100 return type_name;
2101#undef SET_META
2102}
2103
2104void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2105 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2106{
2107 gint i;
2108
2109 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2110 {
2111 GstAmlV4l2VideoDecCData *cdata;
2112 GstStructure *s;
2113 GTypeQuery type_query;
2114 GTypeInfo type_info = {
2115 0,
2116 };
2117 GType type, subtype;
2118 gchar *type_name;
2119
2120 s = gst_caps_get_structure(sink_caps, i);
2121
2122 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2123 cdata->device = g_strdup(device_path);
2124 cdata->sink_caps = gst_caps_new_empty();
2125 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2126 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2127 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2128 cdata->src_caps = gst_caps_copy(src_caps);
2129 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2130 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2131 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2132
2133 /* Skip over if we hit an unmapped type */
2134 if (!type_name)
2135 {
2136 g_free(cdata);
2137 continue;
2138 }
2139
2140 type = gst_aml_v4l2_video_dec_get_type();
2141 g_type_query(type, &type_query);
2142 memset(&type_info, 0, sizeof(type_info));
2143 type_info.class_size = type_query.class_size;
2144 type_info.instance_size = type_query.instance_size;
2145 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2146 type_info.class_data = cdata;
2147 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2148
2149 subtype = g_type_register_static(type, type_name, &type_info, 0);
2150 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2151 subtype))
2152 GST_WARNING("Failed to register plugin '%s'", type_name);
2153
2154 g_free(type_name);
2155 }
2156}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002157
2158#if GST_IMPORT_LGE_PROP
2159static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2160{
2161 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2162 g_param_spec_object("resource-info", "resource-info",
2163 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2164 GST_TYPE_STRUCTURE,
2165 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2166
2167 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2168 g_param_spec_uint64("decoded-size", "decoded-size",
2169 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2170 0, G_MAXUINT64,
2171 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2172
2173 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2174 g_param_spec_uint64("undecoded-size", "undecoded-size",
2175 "video decoder element's total undecoded data update unit is byte.",
2176 0, G_MAXUINT64,
2177 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2178
2179 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2180 g_param_spec_string("app-type", "app-type",
2181 "set application type.",
2182 "default_app",
2183 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2184
2185 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2186 g_param_spec_boolean("clip-mode", "clip-mode",
2187 "When seeking, Content is moving faster for a while to skip frames.",
2188 FALSE,
2189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2190}
2191#endif