blob: f35590f2c943fa4189d91fe24d99b72bd60bba85 [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
le.han02c38f02024-08-16 02:35:36 +000040#ifdef GST_AML_VIDEO_DECODER_STREAM_LOCK
41#undef GST_AML_VIDEO_DECODER_STREAM_LOCK
42#define GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder) \
xuesong.jiangae1548e2022-05-06 16:38:46 +080043 { \
fei.denge9458472023-04-18 02:05:48 +000044 GST_TRACE("aml v4l2 dec locking"); \
le.han02c38f02024-08-16 02:35:36 +000045 g_rec_mutex_lock(&GST_AML_VIDEO_DECODER(decoder)->stream_lock); \
fei.denge9458472023-04-18 02:05:48 +000046 GST_TRACE("aml v4l2 dec locked"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080047 }
48#endif
49
le.han02c38f02024-08-16 02:35:36 +000050#ifdef GST_AML_VIDEO_DECODER_STREAM_UNLOCK
51#undef GST_AML_VIDEO_DECODER_STREAM_UNLOCK
52#define GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder) \
xuesong.jiangae1548e2022-05-06 16:38:46 +080053 { \
fei.denge9458472023-04-18 02:05:48 +000054 GST_TRACE("aml v4l2 dec unlocking"); \
le.han02c38f02024-08-16 02:35:36 +000055 g_rec_mutex_unlock(&GST_AML_VIDEO_DECODER(decoder)->stream_lock); \
fei.denge9458472023-04-18 02:05:48 +000056 GST_TRACE("aml v4l2 dec unlocked"); \
xuesong.jiangae1548e2022-05-06 16:38:46 +080057 }
58#endif
xuesong.jiang61ea8012022-05-12 15:38:17 +080059
zengliang.lidcd41462024-06-19 16:05:12 +080060#define GST_AML_V4L2_CC_IMPORT_QUARK gst_aml_v4l2_buffer_pool_cc_import_quark ()
61
hanghang.luo36df2852022-08-24 15:02:27 +080062#ifndef ABSDIFF
63#define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a)))
64#endif
65
xuesong.jiang61ea8012022-05-12 15:38:17 +080066#if GST_IMPORT_LGE_PROP
67typedef struct _GstAmlResourceInfo
68{
69 gchar *coretype;
70 gint videoport;
71 gint audioport;
72 gint maxwidth;
73 gint maxheight;
74 gint mixerport;
75} GstAmlResourceInfo;
76
77struct _GstAmlV4l2VideoDecLgeCtxt
78{
79 GstAmlResourceInfo res_info;
80 guint64 dec_size;
81 guint64 undec_size;
82 gchar *app_type;
83 gboolean clip_mode;
84};
85#endif
86
xuesong.jiangae1548e2022-05-06 16:38:46 +080087typedef struct
88{
89 gchar *device;
90 GstCaps *sink_caps;
91 GstCaps *src_caps;
92 const gchar *longname;
93 const gchar *description;
94} GstAmlV4l2VideoDecCData;
95
96enum
97{
98 PROP_0,
xuesong.jiang61ea8012022-05-12 15:38:17 +080099 V4L2_STD_OBJECT_PROPS,
100#if GST_IMPORT_LGE_PROP
101 LGE_RESOURCE_INFO,
102 LGE_DECODE_SIZE,
103 LGE_UNDECODE_SIZE,
104 LGE_APP_TYPE,
105 LGE_CLIP_MODE
106#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800107};
108
xuesong.jiang406ee302023-06-28 03:45:22 +0000109enum
110{
111 SIGNAL_DECODED_PTS,
le.han7e9c27d2024-08-08 08:10:46 +0000112 SIGNAL_DECODED_ERROR_PTS,
xuesong.jiang406ee302023-06-28 03:45:22 +0000113 MAX_SIGNAL
114};
115
116static guint g_signals[MAX_SIGNAL]= {0};
117
xuesong.jiangae1548e2022-05-06 16:38:46 +0800118#define gst_aml_v4l2_video_dec_parent_class parent_class
119G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec,
le.han02c38f02024-08-16 02:35:36 +0000120 GST_AML_TYPE_VIDEO_DECODER);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800121
le.han02c38f02024-08-16 02:35:36 +0000122static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder);
xuesong.jiang61ea8012022-05-12 15:38:17 +0800123#if GST_IMPORT_LGE_PROP
124static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class);
125#endif
le.han02c38f02024-08-16 02:35:36 +0000126static GstClockTime calc_output_buffer_pts_func(GstAmlVideoDecoder *decoder);
127static GstClockTime calc_duration_func(GstAmlVideoDecoder *decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800128
129static void
130gst_aml_v4l2_video_dec_set_property(GObject *object,
131 guint prop_id, const GValue *value, GParamSpec *pspec)
132{
133 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
134
135 switch (prop_id)
136 {
137 case PROP_CAPTURE_IO_MODE:
xuesong.jiangae1548e2022-05-06 16:38:46 +0800138 case PROP_DUMP_FRAME_LOCATION:
zengliang.lidcd41462024-06-19 16:05:12 +0800139 case PROP_CC_DATA:
140 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
141 prop_id, value, pspec))
142 {
143 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
144 }
145 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800146#if GST_IMPORT_LGE_PROP
147 case LGE_RESOURCE_INFO:
148 {
149 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
150 GstStructure *r_info = g_value_get_object(value);
151 if (r_info)
152 {
153 if (gst_structure_has_field(r_info, "coretype"))
154 {
155 if (self->lge_ctxt->res_info.coretype)
156 g_free(self->lge_ctxt->res_info.coretype);
157 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
158 }
159 if (gst_structure_has_field(r_info, "videoport"))
160 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
161 if (gst_structure_has_field(r_info, "audioport"))
162 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
163 if (gst_structure_has_field(r_info, "maxwidth"))
164 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
165 if (gst_structure_has_field(r_info, "maxheight"))
166 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
167 if (gst_structure_has_field(r_info, "mixerport"))
168 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
169 }
170 break;
171 }
172 case LGE_APP_TYPE:
173 {
174 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
175 if (self->lge_ctxt->app_type)
176 g_free(self->lge_ctxt->app_type);
177 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
178 break;
179 }
180 case LGE_CLIP_MODE:
181 {
182 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
183 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
184 break;
185 }
186#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +0800187 /* By default, only set on output */
188 default:
189 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output,
190 prop_id, value, pspec))
191 {
192 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
193 }
194 break;
195 }
196}
197
198static void
199gst_aml_v4l2_video_dec_get_property(GObject *object,
200 guint prop_id, GValue *value, GParamSpec *pspec)
201{
202 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
203
204 switch (prop_id)
205 {
206 case PROP_CAPTURE_IO_MODE:
zengliang.lidcd41462024-06-19 16:05:12 +0800207 case PROP_CC_DATA:
fei.dengaf682762024-06-24 19:06:03 +0800208 case PROP_DECODING_ERROR_FRAMES:
zengliang.lidcd41462024-06-19 16:05:12 +0800209 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture,
210 prop_id, value, pspec))
211 {
212 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
213 }
214 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800215#if GST_IMPORT_LGE_PROP
216 case LGE_DECODE_SIZE:
217 {
218 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
219 self->lge_ctxt->dec_size = -1;
220 g_value_set_int(value, self->lge_ctxt->dec_size);
221 break;
222 }
223 case LGE_UNDECODE_SIZE:
224 {
225 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
226 self->lge_ctxt->undec_size = -1;
227 g_value_set_int(value, self->lge_ctxt->undec_size);
228 break;
229 }
230#endif
231
xuesong.jiangae1548e2022-05-06 16:38:46 +0800232 /* By default read from output */
233 default:
234 if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output,
235 prop_id, value, pspec))
236 {
237 G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec);
238 }
239 break;
240 }
241}
242
243static gboolean
le.han02c38f02024-08-16 02:35:36 +0000244gst_aml_v4l2_video_dec_open(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800245{
246 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
247 GstCaps *codec_caps;
248
249 GST_DEBUG_OBJECT(self, "Opening");
250
251 if (!gst_aml_v4l2_object_open(self->v4l2output))
252 goto failure;
253
254 if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output))
255 goto failure;
256
257 codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad);
258 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output,
259 codec_caps);
260 gst_caps_unref(codec_caps);
261
262 if (gst_caps_is_empty(self->probed_sinkcaps))
263 goto no_encoded_format;
264
265 return TRUE;
266
267no_encoded_format:
268 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
269 (_("Decoder on device %s has no supported input format"),
270 self->v4l2output->videodev),
271 (NULL));
272 goto failure;
273
274failure:
275 if (GST_AML_V4L2_IS_OPEN(self->v4l2output))
276 gst_aml_v4l2_object_close(self->v4l2output);
277
278 if (GST_AML_V4L2_IS_OPEN(self->v4l2capture))
279 gst_aml_v4l2_object_close(self->v4l2capture);
280
281 gst_caps_replace(&self->probed_srccaps, NULL);
282 gst_caps_replace(&self->probed_sinkcaps, NULL);
283
284 return FALSE;
285}
286
287static gboolean
le.han02c38f02024-08-16 02:35:36 +0000288gst_aml_v4l2_video_dec_close(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800289{
290 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
291
292 GST_DEBUG_OBJECT(self, "Closing");
293
294 gst_aml_v4l2_object_close(self->v4l2output);
295 gst_aml_v4l2_object_close(self->v4l2capture);
296 gst_caps_replace(&self->probed_srccaps, NULL);
297 gst_caps_replace(&self->probed_sinkcaps, NULL);
298
299 return TRUE;
300}
301
302static gboolean
le.han02c38f02024-08-16 02:35:36 +0000303gst_aml_v4l2_video_dec_start(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800304{
305 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
306
307 GST_DEBUG_OBJECT(self, "Starting");
308
309 gst_aml_v4l2_object_unlock(self->v4l2output);
310 g_atomic_int_set(&self->active, TRUE);
311 self->output_flow = GST_FLOW_OK;
312
313 return TRUE;
314}
315
316static gboolean
le.han02c38f02024-08-16 02:35:36 +0000317gst_aml_v4l2_video_dec_stop(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800318{
319 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
320
321 GST_DEBUG_OBJECT(self, "Stopping");
322
323 gst_aml_v4l2_object_unlock(self->v4l2output);
324 gst_aml_v4l2_object_unlock(self->v4l2capture);
325
326 /* Wait for capture thread to stop */
327 gst_pad_stop_task(decoder->srcpad);
328
le.han02c38f02024-08-16 02:35:36 +0000329 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800330 self->output_flow = GST_FLOW_OK;
le.han02c38f02024-08-16 02:35:36 +0000331 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800332
333 /* Should have been flushed already */
334 g_assert(g_atomic_int_get(&self->active) == FALSE);
335
336 gst_aml_v4l2_object_stop(self->v4l2output);
337 gst_aml_v4l2_object_stop(self->v4l2capture);
338
339 if (self->input_state)
340 {
le.han02c38f02024-08-16 02:35:36 +0000341 gst_aml_video_codec_state_unref(self->input_state);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800342 self->input_state = NULL;
343 }
344
345 GST_DEBUG_OBJECT(self, "Stopped");
346
347 return TRUE;
348}
349
350static gboolean
le.han02c38f02024-08-16 02:35:36 +0000351gst_aml_v4l2_video_dec_codec_chg(GstAmlVideoDecoder *decoder,
352 GstAmlVideoCodecState *state)
hanghang.luo8e1225b2023-10-10 08:54:28 +0000353{
354 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
355 GstStructure *s_old = NULL;
356 GstStructure *s_new = NULL;
357
358 // first play, must set foramt;
359 if (!self->input_state)
360 return TRUE;
361
362 if (self->input_state->caps)
363 s_old = gst_caps_get_structure(self->input_state->caps,0);
364 if (state->caps)
365 s_new = gst_caps_get_structure(state->caps,0);
366
367 if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old)))
368 return TRUE;
369 return FALSE;
370}
371
372static gboolean
le.han02c38f02024-08-16 02:35:36 +0000373gst_aml_v4l2_video_dec_res_chg(GstAmlVideoDecoder *decoder,
374 GstAmlVideoCodecState *state)
hanghang.luo8e1225b2023-10-10 08:54:28 +0000375{
376 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
377 gboolean ret = FALSE;
378 gint width_new = -1,height_new = -1,width_old = -1,height_old = -1;
379 GstStructure *s_old = NULL;
380 GstStructure *s_new = NULL;
381
382 // first play, must set foramt;
383 if (!self->input_state)
384 {
385 ret = TRUE;
386 goto done;
387 }
388
389 if (self->input_state->caps)
390 s_old = gst_caps_get_structure(self->input_state->caps,0);
391 if (state->caps)
392 s_new = gst_caps_get_structure(state->caps,0);
393
394 if (s_new && gst_structure_has_field(s_new,"width") && gst_structure_has_field(s_new,"height"))
395 {
396 gst_structure_get_int(s_new,"width",&width_new);
397 gst_structure_get_int(s_new,"height",&height_new);
398 }
399 if (s_old && gst_structure_has_field(s_old,"width") && gst_structure_has_field(s_old,"height"))
400 {
401 gst_structure_get_int(s_old,"width",&width_old);
402 gst_structure_get_int(s_old,"height",&height_old);
403 }
404
405 if (width_new != width_old || height_new != height_old)
406 ret = TRUE;
407
408done:
409 GST_DEBUG_OBJECT(self, "ret is %d",ret);
410 return ret;
411}
412
413static gboolean
le.han02c38f02024-08-16 02:35:36 +0000414gst_aml_v4l2_video_dec_set_format(GstAmlVideoDecoder *decoder,
415 GstAmlVideoCodecState *state)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800416{
417 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
418 gboolean ret = TRUE;
419 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
420 GstCaps *caps;
421
422 GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps);
hanghang.luo8e1225b2023-10-10 08:54:28 +0000423 if (self->input_state)
424 {
425 if (gst_aml_v4l2_video_dec_res_chg(decoder,state) || gst_aml_v4l2_video_dec_codec_chg(decoder,state))
426 GST_DEBUG_OBJECT(self, "resolution or codec changed");
427 else
428 goto done;
429 }
430
xuesong.jiangae1548e2022-05-06 16:38:46 +0800431 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
hanghang.luoc54208e2023-09-22 02:43:54 +0000432 GstStructure *s = gst_caps_get_structure(state->caps,0);
433 if (s && gst_structure_has_field(s,"format"))
434 {
435 if (!strcmp("XVID",gst_structure_get_string(s,"format")))
436 {
437 GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support");
438 ret = FALSE;
439 goto done;
440 }
441 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800442
443 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
444 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
445
446 if (self->input_state)
447 {
448 if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps))
449 {
450 GST_DEBUG_OBJECT(self, "Compatible caps");
451 goto done;
452 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800453
454 gst_aml_v4l2_video_dec_finish(decoder);
455 gst_aml_v4l2_object_stop(self->v4l2output);
456
le.han02c38f02024-08-16 02:35:36 +0000457 gst_aml_video_codec_state_unref(self->input_state);
fei.dengf78f7122024-07-15 14:44:04 +0800458 self->input_state = NULL;
459
xuesong.jiangae1548e2022-05-06 16:38:46 +0800460 /* The renegotiation flow don't blend with the base class flow. To properly
461 * stop the capture pool, if the buffers can't be orphaned, we need to
462 * reclaim our buffers, which will happend through the allocation query.
le.han02c38f02024-08-16 02:35:36 +0000463 * The allocation query is triggered by gst_aml_video_decoder_negotiate() which
xuesong.jiangae1548e2022-05-06 16:38:46 +0800464 * requires the output caps to be set, but we can't know this information
465 * as we rely on the decoder, which requires the capture queue to be
466 * stopped.
467 *
468 * To workaround this issue, we simply run an allocation query with the
469 * old negotiated caps in order to drain/reclaim our buffers. That breaks
470 * the complexity and should not have much impact in performance since the
471 * following allocation query will happen on a drained pipeline and won't
472 * block. */
473 if (self->v4l2capture->pool &&
474 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
475 {
476 GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad);
477 if (caps)
478 {
479 GstQuery *query = gst_query_new_allocation(caps, FALSE);
480 gst_pad_peer_query(decoder->srcpad, query);
481 gst_query_unref(query);
482 gst_caps_unref(caps);
483 }
484 }
485
486 gst_aml_v4l2_object_stop(self->v4l2capture);
487 self->output_flow = GST_FLOW_OK;
488 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800489 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
490 {
491 GST_ERROR_OBJECT(self, "config output drm mode error");
492 goto done;
493 }
494
xuesong.jiang22a9b112023-05-24 09:01:59 +0000495 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
496 {
497 GST_ERROR_OBJECT(self, "config output stream mode error");
498 goto done;
499 }
500
hanghang.luoac6df002024-07-11 10:54:17 +0800501 if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error))
502 {
503 GST_ERROR_OBJECT(self, "set format error");
504 goto done;
505 }
506
507 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
508 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
509 {
510 GST_ERROR_OBJECT(self, "config I frame mode error");
511 goto done;
512 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800513
514 gst_caps_replace(&self->probed_srccaps, NULL);
515 self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture,
516 gst_aml_v4l2_object_get_raw_caps());
517
518 if (gst_caps_is_empty(self->probed_srccaps))
519 goto no_raw_format;
520
521 caps = gst_caps_copy(self->probed_srccaps);
522 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
523 gst_caps_append(self->probed_srccaps, caps);
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800524
xuesong.jiangae1548e2022-05-06 16:38:46 +0800525 if (ret)
le.han02c38f02024-08-16 02:35:36 +0000526 self->input_state = gst_aml_video_codec_state_ref(state);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800527 else
528 gst_aml_v4l2_error(self, &error);
529
530done:
531 return ret;
532
533no_raw_format:
534 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
535 (_("Decoder on device %s has no supported output format"),
536 self->v4l2output->videodev),
537 (NULL));
538 return GST_FLOW_ERROR;
539}
540
541static gboolean
le.han02c38f02024-08-16 02:35:36 +0000542gst_aml_v4l2_video_dec_flush(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800543{
544 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
545
546 GST_DEBUG_OBJECT(self, "Flushed");
547
548 /* Ensure the processing thread has stopped for the reverse playback
549 * discount case */
550 if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED)
551 {
le.han02c38f02024-08-16 02:35:36 +0000552 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800553
554 gst_aml_v4l2_object_unlock(self->v4l2output);
555 gst_aml_v4l2_object_unlock(self->v4l2capture);
556 gst_pad_stop_task(decoder->srcpad);
le.han02c38f02024-08-16 02:35:36 +0000557 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800558 }
559
560 self->output_flow = GST_FLOW_OK;
561
562 gst_aml_v4l2_object_unlock_stop(self->v4l2output);
563 gst_aml_v4l2_object_unlock_stop(self->v4l2capture);
564
565 if (self->v4l2output->pool)
566 gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool);
567
568 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
569 * called after gst_aml_v4l2_object_unlock_stop() stopped flushing the buffer
570 * pool. */
571 if (self->v4l2capture->pool)
572 gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool);
573
574 return TRUE;
575}
576
577static gboolean
le.han02c38f02024-08-16 02:35:36 +0000578gst_aml_v4l2_video_dec_negotiate(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800579{
580 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
581
xuesong.jiang681d3602022-06-24 21:23:35 +0800582 if (TRUE == self->v4l2output->is_svp)
583 {
584 GstStructure *s;
585 GstEvent *event;
586
587 s = gst_structure_new_empty ("IS_SVP");
588 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
589 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
590 gst_pad_push_event (decoder->srcpad, event);
591 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
592 }
593
xuesong.jiangae1548e2022-05-06 16:38:46 +0800594 /* We don't allow renegotiation without carefull disabling the pool */
595 if (self->v4l2capture->pool &&
596 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
597 return TRUE;
598
le.han02c38f02024-08-16 02:35:36 +0000599 return GST_AML_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800600}
601
602static gboolean
603gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
604{
605 struct v4l2_decoder_cmd dcmd = {
606 0,
607 };
608
609 GST_DEBUG_OBJECT(v4l2object->element,
610 "sending v4l2 decoder command %u with flags %u", cmd, flags);
611
612 if (!GST_AML_V4L2_IS_OPEN(v4l2object))
613 return FALSE;
614
615 dcmd.cmd = cmd;
616 dcmd.flags = flags;
617 if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
618 goto dcmd_failed;
619
620 return TRUE;
621
622dcmd_failed:
623 if (errno == ENOTTY)
624 {
625 GST_INFO_OBJECT(v4l2object->element,
626 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
627 cmd, flags, v4l2object->videodev, g_strerror(errno));
628 }
629 else
630 {
631 GST_ERROR_OBJECT(v4l2object->element,
632 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
633 cmd, flags, v4l2object->videodev, g_strerror(errno));
634 }
635 return FALSE;
636}
637
638static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +0000639gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800640{
641 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
642 GstFlowReturn ret = GST_FLOW_OK;
643 GstBuffer *buffer;
644
645 if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED)
646 goto done;
647
648 GST_DEBUG_OBJECT(self, "Finishing decoding");
649
le.han02c38f02024-08-16 02:35:36 +0000650 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800651
652 if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0))
653 {
654 GstTask *task = decoder->srcpad->task;
655
656 /* If the decoder stop command succeeded, just wait until processing is
657 * finished */
658 GST_DEBUG_OBJECT(self, "Waiting for decoder stop");
659 GST_OBJECT_LOCK(task);
660 while (GST_TASK_STATE(task) == GST_TASK_STARTED)
661 GST_TASK_WAIT(task);
662 GST_OBJECT_UNLOCK(task);
663 ret = GST_FLOW_FLUSHING;
664 }
665 else
666 {
667 /* otherwise keep queuing empty buffers until the processing thread has
668 * stopped, _pool_process() will return FLUSHING when that happened */
669 while (ret == GST_FLOW_OK)
670 {
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800671 GST_DEBUG_OBJECT(self, "queue empty output buf");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800672 buffer = gst_buffer_new();
673 ret =
674 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
675 gst_buffer_unref(buffer);
676 }
677 }
678
679 /* and ensure the processing thread has stopped in case another error
680 * occured. */
681 gst_aml_v4l2_object_unlock(self->v4l2capture);
682 gst_pad_stop_task(decoder->srcpad);
le.han02c38f02024-08-16 02:35:36 +0000683 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800684
685 if (ret == GST_FLOW_FLUSHING)
686 ret = self->output_flow;
687
fei.deng4244a262024-07-31 14:07:14 +0800688 /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop.
689 should reset need_wait_event=true to wait source change event*/
690 self->v4l2capture->need_wait_event = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800691 GST_DEBUG_OBJECT(decoder, "Done draining buffers");
692
693 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
694
695done:
696 return ret;
697}
698
699static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +0000700gst_aml_v4l2_video_dec_drain(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800701{
702 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
703
704 GST_DEBUG_OBJECT(self, "Draining...");
705 gst_aml_v4l2_video_dec_finish(decoder);
706 gst_aml_v4l2_video_dec_flush(decoder);
707
708 return GST_FLOW_OK;
709}
710
le.han02c38f02024-08-16 02:35:36 +0000711static GstAmlVideoCodecFrame *
712gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstAmlVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800713{
le.han02c38f02024-08-16 02:35:36 +0000714 GstAmlVideoCodecFrame *frame = NULL;
fei.dengbee20862022-06-14 14:59:48 +0800715 GList *frames, *l;
716 gint count = 0;
717
xuesong.jiange24aef92023-06-16 06:39:10 +0000718 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
719
le.han02c38f02024-08-16 02:35:36 +0000720 frames = gst_aml_video_decoder_get_frames(decoder);
fei.dengbee20862022-06-14 14:59:48 +0800721
722 for (l = frames; l != NULL; l = l->next)
723 {
le.han02c38f02024-08-16 02:35:36 +0000724 GstAmlVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000725
xuesong.jiangc5dac0f2023-02-01 14:42:24 +0800726 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
fei.dengbee20862022-06-14 14:59:48 +0800727 frame = f;
fei.dengbee20862022-06-14 14:59:48 +0800728 }
fei.dengbee20862022-06-14 14:59:48 +0800729 count++;
730 }
731
zengliang.liddee2da2023-07-14 07:27:05 +0000732 if (!frame)
733 {
734 for (l = frames; l != NULL; l = l->next)
735 {
le.han02c38f02024-08-16 02:35:36 +0000736 GstAmlVideoCodecFrame *f = l->data;
zengliang.liddee2da2023-07-14 07:27:05 +0000737 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
738 {
739 frame = f;
fei.dengf78f7122024-07-15 14:44:04 +0800740 GST_DEBUG("The pts of the expected output frame is invalid");
741 break;
zengliang.liddee2da2023-07-14 07:27:05 +0000742 }
zengliang.liddee2da2023-07-14 07:27:05 +0000743 }
744 }
745
fei.dengbee20862022-06-14 14:59:48 +0800746 if (frame)
747 {
748 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800749 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
750 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
le.han02c38f02024-08-16 02:35:36 +0000751 gst_aml_video_codec_frame_ref(frame);
fei.dengbee20862022-06-14 14:59:48 +0800752 }
753
le.han02c38f02024-08-16 02:35:36 +0000754 g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref);
fei.dengbee20862022-06-14 14:59:48 +0800755
756 return frame;
757}
758
le.han02c38f02024-08-16 02:35:36 +0000759static GstAmlVideoCodecFrame *
760gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstAmlVideoDecoder *decoder, GstClockTime pts)
xuesong.jiange24aef92023-06-16 06:39:10 +0000761{
le.han02c38f02024-08-16 02:35:36 +0000762 GstAmlVideoCodecFrame *frame = NULL;
xuesong.jiange24aef92023-06-16 06:39:10 +0000763 GList *frames, *l;
hanghang.luo4486d7d2024-07-24 10:06:35 +0800764 guint frames_len = 0;
xuesong.jiange24aef92023-06-16 06:39:10 +0000765 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
766
le.han02c38f02024-08-16 02:35:36 +0000767 if (!(frames = gst_aml_video_decoder_get_frames(decoder)))
hanghang.luo4486d7d2024-07-24 10:06:35 +0800768 goto done;
769
xuesong.jiange24aef92023-06-16 06:39:10 +0000770 frames_len = g_list_length(frames);
771 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
772
xuesong.jiange24aef92023-06-16 06:39:10 +0000773 for (l = frames; l != NULL; l = l->next)
774 {
le.han02c38f02024-08-16 02:35:36 +0000775 GstAmlVideoCodecFrame *f = l->data;
xuesong.jiange24aef92023-06-16 06:39:10 +0000776
777 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
778 {
779 /* found the right frame */
bo.xiao8e3054d2024-07-31 17:13:57 +0800780 GST_LOG_OBJECT(decoder,
781 "found frame %" GST_TIME_FORMAT "with pts %" GST_TIME_FORMAT,
782 GST_TIME_ARGS(f->pts), GST_TIME_ARGS(pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000783 frame = f;
784 break;
785 }
786 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
787 {
788 GST_LOG_OBJECT(decoder,
789 "stream mode drop frame %d %" GST_TIME_FORMAT,
hanghang.luoa0626d22024-07-30 16:10:10 +0800790 f->system_frame_number, GST_TIME_ARGS(f->pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000791
le.han02c38f02024-08-16 02:35:36 +0000792 gst_aml_video_codec_frame_ref(f);
793 // gst_aml_video_decoder_drop_frame(decoder, f);
794 gst_aml_video_decoder_release_frame(decoder, f);
xuesong.jiange24aef92023-06-16 06:39:10 +0000795 }
796 else
797 {
798 GST_LOG_OBJECT (decoder, "dbg");
799 }
800 }
801
802 if (frame)
803 {
804 guint l_len = 0;
le.han02c38f02024-08-16 02:35:36 +0000805 l = gst_aml_video_decoder_get_frames(decoder);
xuesong.jiange24aef92023-06-16 06:39:10 +0000806 l_len = g_list_length(l);
le.han02c38f02024-08-16 02:35:36 +0000807 g_list_free_full(l, (GDestroyNotify)gst_aml_video_codec_frame_unref);
xuesong.jiange24aef92023-06-16 06:39:10 +0000808
809 GST_LOG_OBJECT(decoder,
bo.xiao8e3054d2024-07-31 17:13:57 +0800810 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
811 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
le.han02c38f02024-08-16 02:35:36 +0000812 gst_aml_video_codec_frame_ref(frame);
xuesong.jiange24aef92023-06-16 06:39:10 +0000813 }
814
le.han02c38f02024-08-16 02:35:36 +0000815 g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref);
xuesong.jiange24aef92023-06-16 06:39:10 +0000816
hanghang.luo4486d7d2024-07-24 10:06:35 +0800817done:
xuesong.jiange24aef92023-06-16 06:39:10 +0000818 return frame;
819}
820
le.han02c38f02024-08-16 02:35:36 +0000821static GstAmlVideoCodecFrame *
822gst_aml_v4l2_video_dec_get_right_frame(GstAmlVideoDecoder *decoder, GstClockTime pts)
xuesong.jiange24aef92023-06-16 06:39:10 +0000823{
824 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
825 if (self->v4l2output->stream_mode)
826 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
827 else
828 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
829}
830
xuesong.jiangae1548e2022-05-06 16:38:46 +0800831static gboolean
832gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
833 GstStructure *structure, gpointer user_data)
834{
835 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
836 GstVideoAlignment *align = &self->v4l2capture->align;
837 GstVideoInfo *info = &self->v4l2capture->info;
838 int width, height;
839
840 if (!gst_structure_get_int(structure, "width", &width))
841 return TRUE;
842
843 if (!gst_structure_get_int(structure, "height", &height))
844 return TRUE;
845
846 if (align->padding_left != 0 || align->padding_top != 0 ||
847 height != info->height + align->padding_bottom)
848 return TRUE;
849
850 if (height == info->height + align->padding_bottom)
851 {
852 /* Some drivers may round up width to the padded with */
853 if (width == info->width + align->padding_right)
854 gst_structure_set(structure,
855 "width", G_TYPE_INT, width - align->padding_right,
856 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
857 /* Some drivers may keep visible width and only round up bytesperline */
858 else if (width == info->width)
859 gst_structure_set(structure,
860 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
861 }
862
863 return TRUE;
864}
865
866static void
sheng.liubcf036c2022-06-21 15:55:42 +0800867gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800868{
869 struct v4l2_event evt;
870 gint ret;
871
872 memset (&evt, 0x00, sizeof (struct v4l2_event));
873 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
874 if (ret < 0)
875 {
876 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
877 return;
878 }
879
880 switch (evt.type)
881 {
882 case V4L2_EVENT_SOURCE_CHANGE:
883 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
884 break;
885 case V4L2_EVENT_EOS:
886 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
887 break;
888 default:
889 break;
890 }
891
892 return;
893}
894
895static void
le.han02c38f02024-08-16 02:35:36 +0000896gst_aml_v4l2_video_dec_set_output_status(GstAmlVideoDecoder *decoder,GstVideoInfo info)
hanghang.luo2eec4892023-07-18 06:44:42 +0000897{
898 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
le.han02c38f02024-08-16 02:35:36 +0000899 GstAmlVideoCodecState *output_state;
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000900 struct v4l2_selection sel;
901 struct v4l2_rect *r = NULL;
902 GstStructure *s;
903 gint width = 0;
904 gint height = 0;
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800905 GST_DEBUG("%d %d",info.width, info.height);
le.han02c38f02024-08-16 02:35:36 +0000906 output_state = gst_aml_video_decoder_set_output_state(decoder,
hanghang.luo2eec4892023-07-18 06:44:42 +0000907 info.finfo->format, info.width, info.height, self->input_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000908 memset(&sel, 0, sizeof(struct v4l2_selection));
909 sel.type = self->v4l2capture->type;
910 sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT;
911 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
912 {
913 r = &sel.r;
914 width = (r->width/2)*2;
915 height = (r->height/2)*2;
916 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
917 }
918 else
919 GST_DEBUG_OBJECT(self, "iotcl error");
hanghang.luo2eec4892023-07-18 06:44:42 +0000920 if (output_state)
921 {
922 output_state->info.interlace_mode = info.interlace_mode;
923 output_state->allocation_caps =gst_video_info_to_caps(&info);
hanghang.luo2eec4892023-07-18 06:44:42 +0000924 output_state->caps =gst_video_info_to_caps(&info);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000925 s = gst_caps_get_structure(output_state->caps, 0);
926 if (s)
927 {
hanghang.luo2dfa0ac2024-07-09 11:33:39 +0800928 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
929 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
930 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
931 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000932 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
le.han02c38f02024-08-16 02:35:36 +0000933 gst_aml_video_codec_state_unref(output_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000934 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000935 }
936}
937
zengliang.lidcd41462024-06-19 16:05:12 +0800938static GQuark
939gst_aml_v4l2_buffer_pool_cc_import_quark (void)
940{
941 static GQuark quark = 0;
942
943 if (quark == 0)
944 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
945
946 return quark;
947}
948
949static gboolean
le.han02c38f02024-08-16 02:35:36 +0000950foreach_cc_buffer_list_match_pts_func(GList *list , GstAmlVideoCodecFrame *frame)
zengliang.lidcd41462024-06-19 16:05:12 +0800951{
952 GList *l;
953 if (g_list_length (list) > 0)
954 {
955 for (l = list; l != NULL; l = l->next)
956 {
957 GstBuffer *cc_buffer = l->data;
958 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
959 {
960 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
961 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
962 #if 0
963 //Debug code:dump cc data
964 GstMapInfo gst_map;
965 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
966 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
967 if (gst_map.size>0)
968 write(fd,gst_map.data,gst_map.size);
969 close(fd);
970 gst_buffer_unmap(cc_buffer,&gst_map);
971 #endif
972 GST_DEBUG("match success");
973 return TRUE;
974 }
975 else
976 {
977 GST_DEBUG("match fail");
978 }
979 }
980 GST_WARNING("no match frame in the bufferlist");
981 }
982 else
983 {
984 GST_WARNING("list is null,can not foreach");
985 }
986 return FALSE;
987}
988
bo.xiao8e3054d2024-07-31 17:13:57 +0800989static GstClockTime
le.han02c38f02024-08-16 02:35:36 +0000990calc_output_buffer_pts_func(GstAmlVideoDecoder *decoder)
bo.xiao8e3054d2024-07-31 17:13:57 +0800991{
992 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
993 GstClockTime pts = GST_CLOCK_TIME_NONE;
994
995 if (GST_CLOCK_TIME_IS_VALID (self->last_out_pts) && GST_CLOCK_TIME_IS_VALID(self->frame_duration)) {
996 pts = self->last_out_pts + self->frame_duration;
997 GST_LOG_OBJECT (decoder,
998 "calculate PTS %" GST_TIME_FORMAT " by duration: %" GST_TIME_FORMAT,
999 GST_TIME_ARGS (pts), GST_TIME_ARGS (self->frame_duration));
1000 }
1001 else
1002 {
1003 pts = 0;
1004 GST_INFO_OBJECT (decoder,"Set PTS=0");
1005 }
1006 return pts;
1007}
1008
1009static GstClockTime
le.han02c38f02024-08-16 02:35:36 +00001010calc_duration_func(GstAmlVideoDecoder *decoder)
bo.xiao8e3054d2024-07-31 17:13:57 +08001011{
1012 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1013 GstClockTime duration = GST_CLOCK_TIME_NONE;
1014 gint fps_n, fps_d;
1015 fps_n = gst_value_get_fraction_numerator(self->v4l2capture->fps);
1016 fps_d = gst_value_get_fraction_denominator(self->v4l2capture->fps);
1017
1018 if (fps_n != 0 && fps_d != 0)
1019 {
1020 duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
1021
1022 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1023 {
1024 duration = duration / 2;
1025 }
1026 }
1027
1028 GST_INFO_OBJECT (decoder,"framerate(fps_d: %d, fps_n: %d) -> frame_duration = %" GST_TIME_FORMAT, fps_d, fps_n, GST_TIME_ARGS(duration));
1029
1030 return duration;
1031}
1032
hanghang.luo2eec4892023-07-18 06:44:42 +00001033static void
le.han02c38f02024-08-16 02:35:36 +00001034gst_aml_v4l2_video_dec_loop(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001035{
1036 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1037 GstAmlV4l2BufferPool *v4l2_pool;
1038 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1039 GstBufferPool *pool;
le.han02c38f02024-08-16 02:35:36 +00001040 GstAmlVideoCodecFrame *frame;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001041 GstBuffer *buffer = NULL;
1042 GstFlowReturn ret;
1043
1044 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1045 {
1046 GstVideoInfo info;
xuesong.jiang282ca572023-05-05 09:03:32 +00001047 GstCaps *acquired_caps, *available_caps, *caps, *filter;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001048 GstStructure *st;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001049 GST_DEBUG_OBJECT(self, "waitting source change event");
1050 /* Wait until received SOURCE_CHANGE event to get right video format */
1051 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
1052 {
1053 ret = gst_aml_v4l2_object_dqevent(self->v4l2capture);
1054 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1055 {
fei.deng2a06e042023-10-10 03:09:45 +00001056 //let flush start event blocked until capture buffer pool actived
1057 self->is_res_chg = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001058 GST_DEBUG_OBJECT(self, "Received source change event");
1059 break;
1060 }
1061 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1062 {
1063 GST_DEBUG_OBJECT(self, "Received eos event");
1064 goto beach;
1065 }
1066 else if (ret != GST_FLOW_OK)
1067 {
1068 GST_ERROR_OBJECT(self, "dqevent error");
1069 goto beach;
1070 }
1071 }
1072 self->v4l2capture->need_wait_event = FALSE;
1073
sheng.liu0c77f6c2022-06-17 21:33:20 +08001074 if (TRUE == self->v4l2output->is_svp)
1075 {
1076 GstPad *peer;
1077 GstStructure *s;
1078 GstEvent *event;
1079
1080 peer = gst_pad_get_peer (decoder->srcpad);
1081 if (peer)
1082 {
hanghang.luo70f07ef2023-07-13 02:23:06 +00001083 s = gst_structure_new_empty ("IS_SVP");
1084 if (s)
1085 {
1086 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1087 gst_pad_send_event (peer, event);
1088 GST_DEBUG_OBJECT(self, "Send SVP Event");
1089 }
1090 gst_object_unref (peer);
sheng.liu0c77f6c2022-06-17 21:33:20 +08001091 }
1092 }
1093
sheng.liub56bbc52022-06-21 11:02:33 +08001094 if (self->v4l2capture->need_drop_event)
1095 {
1096 // drop V4L2_EVENT_SOURCE_CHANGE
1097 gst_v4l2_drop_event(self->v4l2capture);
1098 self->v4l2capture->need_drop_event = FALSE;
1099 }
1100
xuesong.jiangae1548e2022-05-06 16:38:46 +08001101 if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info))
1102 goto not_negotiated;
hanghang.luo8ec04e92023-10-09 08:14:24 +00001103
xuesong.jiangae1548e2022-05-06 16:38:46 +08001104 /* Create caps from the acquired format, remove the format field */
1105 acquired_caps = gst_video_info_to_caps(&info);
1106 GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1107 st = gst_caps_get_structure(acquired_caps, 0);
xuesong.jiang282ca572023-05-05 09:03:32 +00001108 gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL);
1109
1110 /* Probe currently available pixel formats */
1111 available_caps = gst_caps_copy(self->probed_srccaps);
1112 GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1113
1114 /* Replace coded size with visible size, we want to negotiate visible size
1115 * with downstream, not coded size. */
1116 gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self);
1117
1118 filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001119 caps = gst_caps_copy(filter);
1120 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1121 gst_caps_append(filter, caps);
1122
1123 GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1124 gst_caps_unref(acquired_caps);
xuesong.jiang282ca572023-05-05 09:03:32 +00001125 gst_caps_unref(available_caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001126 caps = gst_pad_peer_query_caps(decoder->srcpad, filter);
1127 gst_caps_unref(filter);
1128
1129 GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1130 if (gst_caps_is_empty(caps))
1131 {
1132 gst_caps_unref(caps);
1133 goto not_negotiated;
1134 }
1135
1136 /* Fixate pixel format */
1137 caps = gst_caps_fixate(caps);
1138
1139 GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
1140
1141 /* Try to set negotiated format, on success replace acquired format */
1142 if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error))
1143 gst_video_info_from_caps(&info, caps);
1144 else
1145 gst_aml_v4l2_clear_error(&error);
1146 gst_caps_unref(caps);
hanghang.luo2eec4892023-07-18 06:44:42 +00001147 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
le.han02c38f02024-08-16 02:35:36 +00001148 if (!gst_aml_video_decoder_negotiate(decoder))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001149 {
1150 if (GST_PAD_IS_FLUSHING(decoder->srcpad))
1151 goto flushing;
1152 else
1153 goto not_negotiated;
1154 }
1155
1156 /* Ensure our internal pool is activated */
1157 if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool),
1158 TRUE))
1159 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001160
bo.xiao8e3054d2024-07-31 17:13:57 +08001161 //cal duration when got resolution event
1162 self->frame_duration = calc_duration_func(decoder);
1163
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001164 g_mutex_lock(&self->res_chg_lock);
1165 GST_LOG_OBJECT(decoder, "signal resolution changed");
1166 self->is_res_chg = FALSE;
1167 g_cond_signal(&self->res_chg_cond);
1168 g_mutex_unlock(&self->res_chg_lock);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001169 }
1170
1171 GST_LOG_OBJECT(decoder, "Allocate output buffer");
1172
1173 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1174
1175 self->output_flow = GST_FLOW_OK;
1176 do
1177 {
1178 /* We cannot use the base class allotate helper since it taking the internal
1179 * stream lock. we know that the acquire may need to poll until more frames
1180 * comes in and holding this lock would prevent that.
1181 */
le.han02c38f02024-08-16 02:35:36 +00001182 pool = gst_aml_video_decoder_get_buffer_pool(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001183
1184 /* Pool may be NULL if we started going to READY state */
1185 if (pool == NULL)
1186 {
le.han02c38f02024-08-16 02:35:36 +00001187 GST_WARNING_OBJECT(decoder, "gst_aml_video_decoder_get_buffer_pool goto beach");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001188 ret = GST_FLOW_FLUSHING;
1189 goto beach;
1190 }
1191
1192 ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL);
zengliang.lidcd41462024-06-19 16:05:12 +08001193
xuesong.jiangae1548e2022-05-06 16:38:46 +08001194 g_object_unref(pool);
1195
fei.deng9a5cd6e2023-06-30 12:09:18 +00001196 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 +00001197 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1198 self->v4l2capture->need_drop_event = TRUE;
1199 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1200 if (self->is_res_chg) {
1201 //we must release last buffer
1202 gst_buffer_unref(buffer);
1203 //if resolution changed event received,we should set need_drop_event to false
1204 self->v4l2capture->need_drop_event = FALSE;
1205 gst_aml_v4l2_object_stop(self->v4l2capture);
1206 //unblock flush start event
1207 g_mutex_lock(&self->res_chg_lock);
1208 self->is_res_chg = FALSE;
1209 g_cond_signal(&self->res_chg_cond);
1210 g_mutex_unlock(&self->res_chg_lock);
1211 return;
1212 } else {
1213 goto beach;
1214 }
sheng.liub56bbc52022-06-21 11:02:33 +08001215 }
1216
zengliang.lidcd41462024-06-19 16:05:12 +08001217 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1218 {
bo.xiaoe978c3b2024-08-01 20:57:15 +08001219 GST_DEBUG_OBJECT(decoder, "already got cc data, just continue.");
1220 continue;
1221 }
1222
bo.xiaob6afda52024-08-02 16:08:30 +08001223 if (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT)
bo.xiaoe978c3b2024-08-01 20:57:15 +08001224 {
1225 GST_DEBUG_OBJECT(decoder, "unknow event, just continue.");
zengliang.lidcd41462024-06-19 16:05:12 +08001226 continue;
1227 }
1228
sheng.liu8d18ed22022-05-26 17:28:15 +08001229 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1230 {
1231 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001232
1233 g_mutex_lock (&self->res_chg_lock);
1234 self->is_res_chg = TRUE;
1235 g_mutex_unlock (&self->res_chg_lock);
sheng.liu8d18ed22022-05-26 17:28:15 +08001236 return;
1237 }
hanghang.luo474440d2024-08-16 10:18:43 +08001238
fei.dengaf682762024-06-24 19:06:03 +08001239 //decoding error happened
1240 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1241 {
le.han7e9c27d2024-08-08 08:10:46 +00001242 g_signal_emit (self, g_signals[SIGNAL_DECODED_ERROR_PTS], v4l2_pool->obj->error_frame_pts, NULL);
hanghang.luo474440d2024-08-16 10:18:43 +08001243 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, v4l2_pool->obj->error_frame_pts);
fei.dengaf682762024-06-24 19:06:03 +08001244 continue;
1245 }
sheng.liu8d18ed22022-05-26 17:28:15 +08001246
fei.dengbee20862022-06-14 14:59:48 +08001247 if (ret != GST_FLOW_OK) {
1248 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001249 goto beach;
fei.dengbee20862022-06-14 14:59:48 +08001250 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001251
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001252 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001253 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
xuesong.jiang406ee302023-06-28 03:45:22 +00001254
bo.xiao8e3054d2024-07-31 17:13:57 +08001255 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 +00001256 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1257
xuesong.jiangae1548e2022-05-06 16:38:46 +08001258 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1259 {
1260 gst_aml_v4l2_object_stop(self->v4l2capture);
1261 return;
1262 }
1263
fei.dengaf682762024-06-24 19:06:03 +08001264 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1265 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
bo.xiaob6afda52024-08-02 16:08:30 +08001266 (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT) ||
fei.dengaf682762024-06-24 19:06:03 +08001267 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001268
1269 if (ret != GST_FLOW_OK)
1270 goto beach;
1271
bo.xiao8e3054d2024-07-31 17:13:57 +08001272 if (!GST_BUFFER_PTS_IS_VALID (buffer))
1273 {
1274 GST_BUFFER_TIMESTAMP(buffer) = calc_output_buffer_pts_func(decoder);
1275 }
1276
1277 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1278 {
1279 GST_BUFFER_DURATION(buffer) = self->frame_duration; // got at resolution event.
1280 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1281 }
1282
fei.dengbee20862022-06-14 14:59:48 +08001283 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001284 if (frame)
1285 {
fei.dengccc89632022-07-15 19:10:17 +08001286 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001287 frame->output_buffer = buffer;
fei.dengccc89632022-07-15 19:10:17 +08001288 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1289 frame->duration = GST_BUFFER_DURATION(buffer);
bo.xiao8e3054d2024-07-31 17:13:57 +08001290
xuesong.jiangae1548e2022-05-06 16:38:46 +08001291 buffer = NULL;
zengliang.lidcd41462024-06-19 16:05:12 +08001292
zengliang.lidcd41462024-06-19 16:05:12 +08001293 if (self->v4l2capture->enable_cc_data)
1294 {
1295 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1296 {
1297 GST_DEBUG("cc buffer and frame bind success");
1298 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1299 GST_AML_V4L2_CC_IMPORT_QUARK);
1300 #if 0
1301 //Debug code:dump cc data
1302 GstMapInfo gst_map;
1303 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1304 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1305 if (gst_map.size>0)
1306 write(fd,gst_map.data,gst_map.size);
1307 close(fd);
1308 gst_buffer_unmap(cc_buffer,&gst_map);
1309 #endif
1310 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1311 gst_buffer_unref(cc_buffer);
1312 }
1313 else
1314 {
1315 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1316 }
1317 }
le.han02c38f02024-08-16 02:35:36 +00001318 ret = gst_aml_video_decoder_finish_frame(decoder, frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001319
1320 if (ret != GST_FLOW_OK)
1321 goto beach;
1322 }
1323 else
1324 {
bo.xiao8e3054d2024-07-31 17:13:57 +08001325 GST_WARNING_OBJECT(decoder, "Unmatch buffer, should be push, need refine");
1326 //gst_pad_push (decoder->srcpad, buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001327 gst_buffer_unref(buffer);
1328 }
1329
1330 return;
1331 /* ERRORS */
1332not_negotiated:
1333{
1334 GST_ERROR_OBJECT(self, "not negotiated");
1335 ret = GST_FLOW_NOT_NEGOTIATED;
1336 goto beach;
1337}
1338activate_failed:
1339{
1340 GST_ERROR_OBJECT(self, "Buffer pool activation failed");
1341 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1342 (_("Failed to allocate required memory.")),
1343 ("Buffer pool activation failed"));
1344 ret = GST_FLOW_ERROR;
1345 goto beach;
1346}
1347flushing:
1348{
1349 ret = GST_FLOW_FLUSHING;
1350 goto beach;
1351}
1352beach:
1353 GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s",
1354 gst_flow_get_name(ret));
fei.deng2a06e042023-10-10 03:09:45 +00001355 if (self->is_res_chg) {
1356 //unblock flush start event
1357 g_mutex_lock(&self->res_chg_lock);
1358 self->is_res_chg = FALSE;
1359 g_cond_signal(&self->res_chg_cond);
1360 g_mutex_unlock(&self->res_chg_lock);
1361 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001362 gst_buffer_replace(&buffer, NULL);
1363 self->output_flow = ret;
1364 gst_aml_v4l2_object_unlock(self->v4l2output);
1365 gst_pad_pause_task(decoder->srcpad);
1366}
1367
1368static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +00001369gst_aml_v4l2_video_dec_handle_frame(GstAmlVideoDecoder *decoder,
1370 GstAmlVideoCodecFrame *frame)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001371{
1372 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1373 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1374 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1375 GstFlowReturn ret = GST_FLOW_OK;
1376 gboolean processed = FALSE;
1377 GstBuffer *tmp;
1378 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001379
1380 GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number);
1381
1382 if (G_UNLIKELY(!g_atomic_int_get(&self->active)))
1383 goto flushing;
1384
1385 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output)))
1386 {
1387 if (!self->input_state)
1388 goto not_negotiated;
1389 if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps,
1390 &error))
1391 goto not_negotiated;
1392 }
1393
1394 if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture)))
1395 {
1396 GstBuffer *codec_data;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001397 GstCapsFeatures *features = NULL;
1398
1399 features = gst_caps_get_features(self->input_state->caps, 0);
xuesong.jiang26749f32024-07-24 09:40:47 +08001400 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
sheng.liu0c77f6c2022-06-17 21:33:20 +08001401 {
1402 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang26749f32024-07-24 09:40:47 +08001403 //TODO:need rm is_svp flag and just using secure_es flag
sheng.liu0c77f6c2022-06-17 21:33:20 +08001404 self->v4l2output->is_svp = TRUE;
1405 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001406
1407 GST_DEBUG_OBJECT(self, "Sending header");
1408
1409 codec_data = self->input_state->codec_data;
1410
1411 /* We are running in byte-stream mode, so we don't know the headers, but
1412 * we need to send something, otherwise the decoder will refuse to
1413 * intialize.
1414 */
1415 if (codec_data)
1416 {
1417 gst_buffer_ref(codec_data);
1418 }
1419 else
1420 {
1421 codec_data = gst_buffer_ref(frame->input_buffer);
1422 processed = TRUE;
1423 }
1424
1425 /* Ensure input internal pool is active */
1426 if (!gst_buffer_pool_is_active(pool))
1427 {
1428 GstStructure *config = gst_buffer_pool_get_config(pool);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001429 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1430 // guint max = VIDEO_MAX_FRAME;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001431 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1432 // self->v4l2output->info.size, min, max);
1433 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1434
1435 /* There is no reason to refuse this config */
1436 if (!gst_buffer_pool_set_config(pool, config))
1437 goto activate_failed;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001438 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001439
1440 if (!gst_buffer_pool_set_active(pool, TRUE))
1441 goto activate_failed;
1442 }
1443
le.han02c38f02024-08-16 02:35:36 +00001444 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001445 ret =
1446 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
hanghang.luoc54208e2023-09-22 02:43:54 +00001447 self->codec_data_inject = TRUE;
le.han02c38f02024-08-16 02:35:36 +00001448 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001449
1450 gst_buffer_unref(codec_data);
1451
1452 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1453 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1454 * and returns the visible size as with/height and the coded size as
1455 * padding. */
1456 }
1457
le.han02c38f02024-08-16 02:35:36 +00001458 task_state = gst_pad_get_task_state(GST_AML_VIDEO_DECODER_SRC_PAD(self));
xuesong.jiangae1548e2022-05-06 16:38:46 +08001459 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1460 {
1461 /* It's possible that the processing thread stopped due to an error */
1462 if (self->output_flow != GST_FLOW_OK &&
1463 self->output_flow != GST_FLOW_FLUSHING)
1464 {
1465 GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving");
1466 ret = self->output_flow;
1467 goto drop;
1468 }
1469
1470 GST_DEBUG_OBJECT(self, "Starting decoding thread");
1471
1472 /* Start the processing task, when it quits, the task will disable input
1473 * processing to unlock input if draining, or prevent potential block */
1474 self->output_flow = GST_FLOW_FLUSHING;
1475 if (!gst_pad_start_task(decoder->srcpad,
1476 (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL))
1477 goto start_task_failed;
1478 }
1479
1480 if (!processed)
1481 {
le.han02c38f02024-08-16 02:35:36 +00001482 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
hanghang.luoc54208e2023-09-22 02:43:54 +00001483 if (!self->codec_data_inject && self->input_state->codec_data)
1484 {
1485 ret = gst_aml_v4l2_buffer_pool_process
1486 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1487 self->codec_data_inject = TRUE;
1488 if (ret != GST_FLOW_OK)
1489 goto send_codec_failed;
1490 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001491 ret =
1492 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
le.han02c38f02024-08-16 02:35:36 +00001493 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001494
1495 if (ret == GST_FLOW_FLUSHING)
1496 {
le.han02c38f02024-08-16 02:35:36 +00001497 if (gst_pad_get_task_state(GST_AML_VIDEO_DECODER_SRC_PAD(self)) !=
xuesong.jiangae1548e2022-05-06 16:38:46 +08001498 GST_TASK_STARTED)
1499 ret = self->output_flow;
1500 goto drop;
1501 }
1502 else if (ret != GST_FLOW_OK)
1503 {
1504 goto process_failed;
1505 }
1506 }
1507
1508 /* No need to keep input arround */
1509 tmp = frame->input_buffer;
1510 frame->input_buffer = gst_buffer_new();
1511 gst_buffer_copy_into(frame->input_buffer, tmp,
1512 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1513 GST_BUFFER_COPY_META,
1514 0, 0);
1515 gst_buffer_unref(tmp);
1516
le.han02c38f02024-08-16 02:35:36 +00001517 gst_aml_video_codec_frame_unref(frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001518 return ret;
1519
1520 /* ERRORS */
hanghang.luoc54208e2023-09-22 02:43:54 +00001521send_codec_failed:
1522 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1523 goto drop;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001524not_negotiated:
1525{
1526 GST_ERROR_OBJECT(self, "not negotiated");
1527 ret = GST_FLOW_NOT_NEGOTIATED;
1528 gst_aml_v4l2_error(self, &error);
1529 goto drop;
1530}
1531activate_failed:
1532{
1533 GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS,
1534 (_("Failed to allocate required memory.")),
1535 ("Buffer pool activation failed"));
1536 ret = GST_FLOW_ERROR;
1537 goto drop;
1538}
1539flushing:
1540{
1541 ret = GST_FLOW_FLUSHING;
1542 goto drop;
1543}
1544
1545start_task_failed:
1546{
1547 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1548 (_("Failed to start decoding thread.")), (NULL));
1549 ret = GST_FLOW_ERROR;
1550 goto drop;
1551}
1552process_failed:
1553{
1554 GST_ELEMENT_ERROR(self, RESOURCE, FAILED,
1555 (_("Failed to process frame.")),
1556 ("Maybe be due to not enough memory or failing driver"));
1557 ret = GST_FLOW_ERROR;
1558 goto drop;
1559}
1560drop:
1561{
le.han02c38f02024-08-16 02:35:36 +00001562 gst_aml_video_decoder_drop_frame(decoder, frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001563 return ret;
1564}
1565}
1566
1567static gboolean
le.han02c38f02024-08-16 02:35:36 +00001568gst_aml_v4l2_video_dec_decide_allocation(GstAmlVideoDecoder *decoder,
xuesong.jiangae1548e2022-05-06 16:38:46 +08001569 GstQuery *query)
1570{
1571 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1572 GstClockTime latency;
1573 gboolean ret = FALSE;
1574
1575 if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query))
le.han02c38f02024-08-16 02:35:36 +00001576 ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001577
1578 if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration))
1579 {
1580 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1581 GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency),
1582 self->v4l2capture->min_buffers, self->v4l2capture->duration);
le.han02c38f02024-08-16 02:35:36 +00001583 gst_aml_video_decoder_set_latency(decoder, latency, latency);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001584 }
1585 else
1586 {
1587 GST_WARNING_OBJECT(self, "Duration invalid, not setting latency");
1588 }
1589
1590 return ret;
1591}
1592
1593static gboolean
le.han02c38f02024-08-16 02:35:36 +00001594gst_aml_v4l2_video_dec_src_query(GstAmlVideoDecoder *decoder, GstQuery *query)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001595{
1596 gboolean ret = TRUE;
1597 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1598
1599 switch (GST_QUERY_TYPE(query))
1600 {
1601 case GST_QUERY_CAPS:
1602 {
1603 GstCaps *filter, *result = NULL;
le.han02c38f02024-08-16 02:35:36 +00001604 GstPad *pad = GST_AML_VIDEO_DECODER_SRC_PAD(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001605
1606 gst_query_parse_caps(query, &filter);
1607
1608 if (self->probed_srccaps)
1609 result = gst_caps_ref(self->probed_srccaps);
1610 else
1611 result = gst_pad_get_pad_template_caps(pad);
1612
1613 if (filter)
1614 {
1615 GstCaps *tmp = result;
1616 result =
1617 gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST);
1618 gst_caps_unref(tmp);
1619 }
1620
1621 GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result);
1622
1623 gst_query_set_caps_result(query, result);
1624 gst_caps_unref(result);
1625 break;
1626 }
1627
1628 default:
le.han02c38f02024-08-16 02:35:36 +00001629 ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001630 break;
1631 }
1632
1633 return ret;
1634}
1635
1636static GstCaps *
le.han02c38f02024-08-16 02:35:36 +00001637gst_aml_v4l2_video_dec_sink_getcaps(GstAmlVideoDecoder *decoder, GstCaps *filter)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001638{
1639 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1640 GstCaps *result;
1641
le.han02c38f02024-08-16 02:35:36 +00001642 result = gst_aml_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps,
xuesong.jiangae1548e2022-05-06 16:38:46 +08001643 filter);
1644
1645 GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result);
1646
1647 return result;
1648}
1649
1650static gboolean
le.han02c38f02024-08-16 02:35:36 +00001651gst_aml_v4l2_video_dec_sink_event(GstAmlVideoDecoder *decoder, GstEvent *event)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001652{
1653 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1654 gboolean ret;
1655 GstEventType type = GST_EVENT_TYPE(event);
bo.xiao8e3054d2024-07-31 17:13:57 +08001656 GST_DEBUG_OBJECT (self, "received event %p %" GST_PTR_FORMAT, event, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001657
1658 switch (type)
1659 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001660 case GST_EVENT_STREAM_START:
1661 {
1662 GstStructure *s;
1663 GstEvent *event;
xuesong.jiang406ee302023-06-28 03:45:22 +00001664 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1665 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1666 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1667 gst_pad_push_event (decoder->sinkpad, event);
1668 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1669 break;
1670 }
zengliang.li51f54b62023-10-10 12:14:49 +00001671 case GST_EVENT_CAPS:
1672 {
1673 GstCaps *caps;
1674 GstStructure *structure;
sheng.liu641aa422023-12-26 07:05:59 +00001675 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001676
1677 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001678
zengliang.li51f54b62023-10-10 12:14:49 +00001679 structure= gst_caps_get_structure(caps, 0);
1680 if ( gst_structure_has_field(structure, "parsed") )
1681 {
1682 gboolean parsed = TRUE;
1683 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
1684 {
1685 self->v4l2output->stream_mode = !parsed;
1686 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1687 }
1688 }
sheng.liu641aa422023-12-26 07:05:59 +00001689
xuesong.jiang26749f32024-07-24 09:40:47 +08001690 if ( gst_structure_has_field(structure, "secure") )
1691 {
1692 gboolean is_secure = FALSE;
1693 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1694 {
1695 self->v4l2output->secure_es = is_secure;
1696 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1697 }
1698 }
xuesong.jiang578add22024-07-25 15:11:30 +08001699 else
1700 {
1701 GstCapsFeatures *const features = gst_caps_get_features(caps, 0);
1702 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1703 {
1704 self->v4l2output->secure_es = TRUE;
1705 GST_DEBUG("If there is no secure field in caps, consider dma es is secure");
1706 }
1707 }
xuesong.jiang26749f32024-07-24 09:40:47 +08001708
sheng.liudb26f7d2024-01-22 11:24:23 +00001709 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1710 {
1711 if ( denom == 0 ) denom= 1;
1712
1713 if (self->v4l2capture->fps)
1714 {
1715 g_value_unset(self->v4l2capture->fps);
1716 g_free(self->v4l2capture->fps);
1717 }
1718
1719 self->v4l2capture->fps = g_new0(GValue, 1);
1720 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1721 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1722
1723 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1724 }
1725
sheng.liu641aa422023-12-26 07:05:59 +00001726 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1727 ( !self->v4l2capture->have_set_par ) )
1728 {
1729 if ( (num <= 0) || (denom <= 0) )
1730 {
1731 num= denom= 1;
1732 }
1733
1734 if ( self->v4l2capture->par )
1735 {
1736 g_value_unset(self->v4l2capture->par);
1737 g_free(self->v4l2capture->par);
1738 }
1739
1740 self->v4l2capture->par = g_new0(GValue, 1);
1741 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1742 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1743 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1744 }
zengliang.li51f54b62023-10-10 12:14:49 +00001745 break;
1746 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001747 case GST_EVENT_FLUSH_START:
1748 GST_DEBUG_OBJECT(self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001749
1750 g_mutex_lock (&self->res_chg_lock);
1751 while (self->is_res_chg)
1752 {
1753 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1754 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1755 }
1756 g_mutex_unlock (&self->res_chg_lock);
1757
zengliang.li92ff6822023-06-06 07:12:52 +00001758 self->last_out_pts = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001759 gst_aml_v4l2_object_unlock(self->v4l2output);
1760 gst_aml_v4l2_object_unlock(self->v4l2capture);
1761 break;
1762 default:
1763 break;
1764 }
1765
le.han02c38f02024-08-16 02:35:36 +00001766 ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001767
1768 switch (type)
1769 {
1770 case GST_EVENT_FLUSH_START:
1771 /* The processing thread should stop now, wait for it */
1772 gst_pad_stop_task(decoder->srcpad);
hanghang.luoc54208e2023-09-22 02:43:54 +00001773 self->codec_data_inject = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001774 GST_DEBUG_OBJECT(self, "flush start done");
1775 break;
1776 default:
1777 break;
1778 }
1779
1780 return ret;
1781}
1782
1783static GstStateChangeReturn
1784gst_aml_v4l2_video_dec_change_state(GstElement *element,
1785 GstStateChange transition)
1786{
1787 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
le.han02c38f02024-08-16 02:35:36 +00001788 GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(element);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001789
bo.xiao8e3054d2024-07-31 17:13:57 +08001790 GST_DEBUG_OBJECT(element, "change state from %s to %s",
1791 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1792 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
1793
xuesong.jiangae1548e2022-05-06 16:38:46 +08001794 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1795 {
1796 g_atomic_int_set(&self->active, FALSE);
1797 gst_aml_v4l2_object_unlock(self->v4l2output);
1798 gst_aml_v4l2_object_unlock(self->v4l2capture);
1799 gst_pad_stop_task(decoder->srcpad);
1800 }
1801
1802 return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition);
1803}
1804
1805static void
1806gst_aml_v4l2_video_dec_dispose(GObject *object)
1807{
1808 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1809
1810 gst_caps_replace(&self->probed_sinkcaps, NULL);
1811 gst_caps_replace(&self->probed_srccaps, NULL);
1812
1813 G_OBJECT_CLASS(parent_class)->dispose(object);
1814}
1815
1816static void
1817gst_aml_v4l2_video_dec_finalize(GObject *object)
1818{
1819 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
1820
1821 gst_aml_v4l2_object_destroy(self->v4l2capture);
1822 gst_aml_v4l2_object_destroy(self->v4l2output);
1823
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001824 g_mutex_clear(&self->res_chg_lock);
1825 g_cond_clear(&self->res_chg_cond);
1826
xuesong.jiang61ea8012022-05-12 15:38:17 +08001827#if GST_IMPORT_LGE_PROP
1828 if (self->lge_ctxt)
1829 {
1830 if (self->lge_ctxt->app_type)
1831 g_free(self->lge_ctxt->app_type);
1832 if (self->lge_ctxt->res_info.coretype)
1833 g_free(self->lge_ctxt->res_info.coretype);
1834 free(self->lge_ctxt);
1835 }
1836
1837#endif
1838
xuesong.jiangae1548e2022-05-06 16:38:46 +08001839 G_OBJECT_CLASS(parent_class)->finalize(object);
1840}
1841
1842static void
1843gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self)
1844{
1845 /* V4L2 object are created in subinstance_init */
zengliang.li92ff6822023-06-06 07:12:52 +00001846 self->last_out_pts = GST_CLOCK_TIME_NONE;
bo.xiao8e3054d2024-07-31 17:13:57 +08001847 self->frame_duration = GST_CLOCK_TIME_NONE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001848 self->is_secure_path = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001849 self->is_res_chg = FALSE;
hanghang.luoc54208e2023-09-22 02:43:54 +00001850 self->codec_data_inject = FALSE;
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001851 g_mutex_init(&self->res_chg_lock);
1852 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001853#if GST_IMPORT_LGE_PROP
1854 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1855 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
1856#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001857}
1858
1859static void
1860gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class)
1861{
1862 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1863 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance);
le.han02c38f02024-08-16 02:35:36 +00001864 GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(instance);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001865
le.han02c38f02024-08-16 02:35:36 +00001866 gst_aml_video_decoder_set_packetized(decoder, TRUE);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001867
1868 self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self),
le.han02c38f02024-08-16 02:35:36 +00001869 GST_OBJECT(GST_AML_VIDEO_DECODER_SINK_PAD(self)),
xuesong.jiangae1548e2022-05-06 16:38:46 +08001870 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1871 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1872 self->v4l2output->no_initial_format = TRUE;
1873 self->v4l2output->keep_aspect = FALSE;
sheng.liu0c77f6c2022-06-17 21:33:20 +08001874 self->v4l2output->is_svp = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001875
1876 self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self),
le.han02c38f02024-08-16 02:35:36 +00001877 GST_OBJECT(GST_AML_VIDEO_DECODER_SRC_PAD(self)),
xuesong.jiangae1548e2022-05-06 16:38:46 +08001878 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1879 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1880 self->v4l2capture->need_wait_event = TRUE;
sheng.liub56bbc52022-06-21 11:02:33 +08001881 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001882}
1883
1884static void
1885gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass)
1886{
1887 GstElementClass *element_class;
1888 GObjectClass *gobject_class;
le.han02c38f02024-08-16 02:35:36 +00001889 GstAmlVideoDecoderClass *video_decoder_class;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001890
1891 parent_class = g_type_class_peek_parent(klass);
1892
1893 element_class = (GstElementClass *)klass;
1894 gobject_class = (GObjectClass *)klass;
le.han02c38f02024-08-16 02:35:36 +00001895 video_decoder_class = (GstAmlVideoDecoderClass *)klass;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001896
1897 GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1898 "AML V4L2 Video Decoder");
1899
1900 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1901 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1902 gobject_class->set_property =
1903 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1904 gobject_class->get_property =
1905 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
1906
1907 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1908 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1909 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1910 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1911 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1912 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1913 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1914 video_decoder_class->set_format =
1915 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1916 video_decoder_class->negotiate =
1917 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1918 video_decoder_class->decide_allocation =
1919 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1920 /* FIXME propose_allocation or not ? */
1921 video_decoder_class->handle_frame =
1922 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1923 video_decoder_class->getcaps =
1924 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1925 video_decoder_class->src_query =
1926 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1927 video_decoder_class->sink_event =
1928 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
1929
1930 element_class->change_state =
1931 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
1932
xuesong.jiang406ee302023-06-28 03:45:22 +00001933 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
1934 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1935 G_SIGNAL_RUN_LAST,
1936 0, /* class offset */
1937 NULL, /* accumulator */
1938 NULL, /* accu data */
1939 g_cclosure_marshal_generic,
1940 G_TYPE_NONE,
1941 1,
1942 G_TYPE_UINT64);
le.han7e9c27d2024-08-08 08:10:46 +00001943 g_signals[SIGNAL_DECODED_ERROR_PTS] = g_signal_new ("decoded-error-pts",
1944 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1945 G_SIGNAL_RUN_LAST,
1946 0, /* class offset */
1947 NULL, /* accumulator */
1948 NULL, /* accu data */
1949 g_cclosure_marshal_generic,
1950 G_TYPE_NONE,
1951 1,
1952 G_TYPE_UINT64);
xuesong.jiang406ee302023-06-28 03:45:22 +00001953
xuesong.jiangae1548e2022-05-06 16:38:46 +08001954 gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001955#if GST_IMPORT_LGE_PROP
1956 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
1957#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001958}
1959
1960static void
1961gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data)
1962{
1963 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class);
1964 GstElementClass *element_class = GST_ELEMENT_CLASS(g_class);
1965 GstAmlV4l2VideoDecCData *cdata = data;
1966
1967 klass->default_device = cdata->device;
1968
1969 /* Note: gst_pad_template_new() take the floating ref from the caps */
1970 gst_element_class_add_pad_template(element_class,
1971 gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
1972 cdata->sink_caps));
1973 gst_element_class_add_pad_template(element_class,
1974 gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS,
1975 cdata->src_caps));
1976
1977 gst_element_class_set_metadata(element_class, cdata->longname,
1978 "Codec/Decoder/Video/Hardware", cdata->description,
1979 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
1980
1981 gst_caps_unref(cdata->sink_caps);
1982 gst_caps_unref(cdata->src_caps);
1983 g_free(cdata);
1984}
1985
1986/* Probing functions */
1987gboolean
1988gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps)
1989{
1990 gboolean ret = FALSE;
1991
1992 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()))
1993 ret = TRUE;
1994
1995 return ret;
1996}
1997
1998static gchar *
1999gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata,
2000 const gchar *basename)
2001{
2002 gchar *codec_name = NULL;
2003 gchar *type_name = NULL;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002004
2005#define SET_META(codec) \
2006 G_STMT_START \
2007 { \
2008 cdata->longname = "AML V4L2 " codec " Decoder"; \
2009 cdata->description = "Decodes " codec " streams via V4L2 API"; \
2010 codec_name = g_ascii_strdown(codec, -1); \
2011 } \
2012 G_STMT_END
2013
bo.xiaoce116a42024-07-16 16:21:53 +08002014 if (gst_structure_has_name(s, "video/mjpeg"))
xuesong.jiangae1548e2022-05-06 16:38:46 +08002015 {
bo.xiaoce116a42024-07-16 16:21:53 +08002016 SET_META("MJPEG");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002017 }
2018 else if (gst_structure_has_name(s, "video/mpeg"))
2019 {
bo.xiaob6afda52024-08-02 16:08:30 +08002020 //include mpeg1, mpeg2, mpeg4
2021 SET_META("MPEG4");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002022 }
2023 else if (gst_structure_has_name(s, "video/x-h263"))
2024 {
2025 SET_META("H263");
2026 }
2027 else if (gst_structure_has_name(s, "video/x-fwht"))
2028 {
2029 SET_META("FWHT");
2030 }
2031 else if (gst_structure_has_name(s, "video/x-h264"))
2032 {
2033 SET_META("H264");
2034 }
2035 else if (gst_structure_has_name(s, "video/x-h265"))
2036 {
2037 SET_META("H265");
2038 }
2039 else if (gst_structure_has_name(s, "video/x-wmv"))
2040 {
2041 SET_META("VC1");
2042 }
2043 else if (gst_structure_has_name(s, "video/x-vp8"))
2044 {
2045 SET_META("VP8");
2046 }
2047 else if (gst_structure_has_name(s, "video/x-vp9"))
2048 {
2049 SET_META("VP9");
2050 }
2051 else if (gst_structure_has_name(s, "video/x-av1"))
2052 {
2053 SET_META("AV1");
2054 }
zengliang.li51f54b62023-10-10 12:14:49 +00002055 else if (gst_structure_has_name(s, "video/x-avs"))
2056 {
2057 SET_META("AVS");
2058 }
2059 else if (gst_structure_has_name(s, "video/x-avs2"))
2060 {
2061 SET_META("AVS2");
2062 }
2063 else if (gst_structure_has_name(s, "video/x-avs3"))
2064 {
2065 SET_META("AVS3");
2066 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002067 else if (gst_structure_has_name(s, "video/x-bayer"))
2068 {
2069 SET_META("BAYER");
2070 }
2071 else if (gst_structure_has_name(s, "video/x-sonix"))
2072 {
2073 SET_META("SONIX");
2074 }
2075 else if (gst_structure_has_name(s, "video/x-pwc1"))
2076 {
2077 SET_META("PWC1");
2078 }
2079 else if (gst_structure_has_name(s, "video/x-pwc2"))
2080 {
2081 SET_META("PWC2");
2082 }
2083 else
2084 {
2085 /* This code should be kept on sync with the exposed CODEC type of format
2086 * from gstamlv4l2object.c. This warning will only occure in case we forget
2087 * to also add a format here. */
2088 gchar *s_str = gst_structure_to_string(s);
2089 g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer "
2090 "bug, please report at https://bugs.gnome.org",
2091 s_str);
2092 g_free(s_str);
2093 }
2094
2095 if (codec_name)
2096 {
2097 type_name = g_strdup_printf("amlv4l2%sdec", codec_name);
2098 if (g_type_from_name(type_name) != 0)
2099 {
2100 g_free(type_name);
2101 type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name);
2102 }
2103
2104 g_free(codec_name);
2105 }
2106
2107 return type_name;
2108#undef SET_META
2109}
2110
2111void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
2112 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2113{
2114 gint i;
2115
2116 for (i = 0; i < gst_caps_get_size(sink_caps); i++)
2117 {
2118 GstAmlV4l2VideoDecCData *cdata;
2119 GstStructure *s;
2120 GTypeQuery type_query;
2121 GTypeInfo type_info = {
2122 0,
2123 };
2124 GType type, subtype;
2125 gchar *type_name;
2126
2127 s = gst_caps_get_structure(sink_caps, i);
2128
2129 cdata = g_new0(GstAmlV4l2VideoDecCData, 1);
2130 cdata->device = g_strdup(device_path);
2131 cdata->sink_caps = gst_caps_new_empty();
2132 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2133 gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s));
2134 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2135 cdata->src_caps = gst_caps_copy(src_caps);
2136 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2137 gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps));
2138 type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename);
2139
2140 /* Skip over if we hit an unmapped type */
2141 if (!type_name)
2142 {
2143 g_free(cdata);
2144 continue;
2145 }
2146
2147 type = gst_aml_v4l2_video_dec_get_type();
2148 g_type_query(type, &type_query);
2149 memset(&type_info, 0, sizeof(type_info));
2150 type_info.class_size = type_query.class_size;
2151 type_info.instance_size = type_query.instance_size;
2152 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2153 type_info.class_data = cdata;
2154 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2155
2156 subtype = g_type_register_static(type, type_name, &type_info, 0);
2157 if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1,
2158 subtype))
2159 GST_WARNING("Failed to register plugin '%s'", type_name);
2160
2161 g_free(type_name);
2162 }
2163}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002164
2165#if GST_IMPORT_LGE_PROP
2166static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2167{
2168 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2169 g_param_spec_object("resource-info", "resource-info",
2170 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2171 GST_TYPE_STRUCTURE,
2172 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2173
2174 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2175 g_param_spec_uint64("decoded-size", "decoded-size",
2176 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2177 0, G_MAXUINT64,
2178 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2179
2180 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2181 g_param_spec_uint64("undecoded-size", "undecoded-size",
2182 "video decoder element's total undecoded data update unit is byte.",
2183 0, G_MAXUINT64,
2184 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2185
2186 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2187 g_param_spec_string("app-type", "app-type",
2188 "set application type.",
2189 "default_app",
2190 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2191
2192 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2193 g_param_spec_boolean("clip-mode", "clip-mode",
2194 "When seeking, Content is moving faster for a while to skip frames.",
2195 FALSE,
2196 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2197}
2198#endif