blob: 58147463365bcd3f96bbfc954d8d6cdfcc29dead [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{
bo.xiao857b8682024-09-12 16:40:32 +080068 gchar *device;
69 GstCaps *sink_caps;
70 GstCaps *src_caps;
71 const gchar *longname;
72 const gchar *description;
xuesong.jiangae1548e2022-05-06 16:38:46 +080073} GstAmlV4l2VideoDecCData;
74
75enum
76{
bo.xiao857b8682024-09-12 16:40:32 +080077 PROP_0,
78 V4L2_STD_OBJECT_PROPS,
xuesong.jiang61ea8012022-05-12 15:38:17 +080079#if GST_IMPORT_LGE_PROP
bo.xiao857b8682024-09-12 16:40:32 +080080 LGE_RESOURCE_INFO,
81 LGE_DECODE_SIZE,
82 LGE_UNDECODE_SIZE,
83 LGE_APP_TYPE,
84 LGE_CLIP_MODE
xuesong.jiang61ea8012022-05-12 15:38:17 +080085#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,
bo.xiao857b8682024-09-12 16:40:32 +080099 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.jiang0223de52024-09-13 15:33:10 +0800107static GstFlowReturn gst_aml_v4l2_video_dec_clip_and_push_frame (GstAmlVideoDecoder * decoder, GstAmlVideoCodecFrame * frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800108
109static void
110gst_aml_v4l2_video_dec_set_property(GObject *object,
bo.xiao857b8682024-09-12 16:40:32 +0800111 guint prop_id, const GValue * value, GParamSpec * pspec)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800112{
bo.xiao857b8682024-09-12 16:40:32 +0800113 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800114
bo.xiao857b8682024-09-12 16:40:32 +0800115 switch (prop_id)
116 {
xuesong.jiangae1548e2022-05-06 16:38:46 +0800117 case PROP_CAPTURE_IO_MODE:
xuesong.jiangae1548e2022-05-06 16:38:46 +0800118 case PROP_DUMP_FRAME_LOCATION:
zengliang.lidcd41462024-06-19 16:05:12 +0800119 case PROP_CC_DATA:
bo.xiao857b8682024-09-12 16:40:32 +0800120 if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture,
121 prop_id, value, pspec))
122 {
123 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
124 }
125 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800126#if GST_IMPORT_LGE_PROP
bo.xiao857b8682024-09-12 16:40:32 +0800127 case LGE_RESOURCE_INFO:
128 {
129 GST_DEBUG_OBJECT(self, "LGE up layer set res info");
130 GstStructure *r_info = g_value_get_object(value);
131 if (r_info)
132 {
133 if (gst_structure_has_field(r_info, "coretype"))
134 {
135 if (self->lge_ctxt->res_info.coretype)
136 g_free(self->lge_ctxt->res_info.coretype);
137 self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype"));
138 }
139 if (gst_structure_has_field(r_info, "videoport"))
140 gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport));
141 if (gst_structure_has_field(r_info, "audioport"))
142 gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport));
143 if (gst_structure_has_field(r_info, "maxwidth"))
144 gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth));
145 if (gst_structure_has_field(r_info, "maxheight"))
146 gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight));
147 if (gst_structure_has_field(r_info, "mixerport"))
148 gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport));
149 }
150 break;
151 }
152 case LGE_APP_TYPE:
153 {
154 GST_DEBUG_OBJECT(self, "LGE up layer set app type");
155 if (self->lge_ctxt->app_type)
156 g_free(self->lge_ctxt->app_type);
157 self->lge_ctxt->app_type = g_strdup(g_value_get_string(value));
158 break;
159 }
160 case LGE_CLIP_MODE:
161 {
162 GST_DEBUG_OBJECT(self, "LGE up layer set clip mode");
163 self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value));
164 break;
165 }
xuesong.jiang61ea8012022-05-12 15:38:17 +0800166#endif
bo.xiao857b8682024-09-12 16:40:32 +0800167 /* By default, only set on output */
xuesong.jiangae1548e2022-05-06 16:38:46 +0800168 default:
bo.xiao857b8682024-09-12 16:40:32 +0800169 if (!gst_aml_v4l2_object_set_property_helper (self->v4l2output,
170 prop_id, value, pspec))
171 {
172 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
173 }
174 break;
175 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800176}
177
178static void
179gst_aml_v4l2_video_dec_get_property(GObject *object,
180 guint prop_id, GValue *value, GParamSpec *pspec)
181{
bo.xiao857b8682024-09-12 16:40:32 +0800182 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800183
bo.xiao857b8682024-09-12 16:40:32 +0800184 switch (prop_id)
185 {
xuesong.jiangae1548e2022-05-06 16:38:46 +0800186 case PROP_CAPTURE_IO_MODE:
zengliang.lidcd41462024-06-19 16:05:12 +0800187 case PROP_CC_DATA:
fei.dengaf682762024-06-24 19:06:03 +0800188 case PROP_DECODING_ERROR_FRAMES:
bo.xiao857b8682024-09-12 16:40:32 +0800189 if (!gst_aml_v4l2_object_get_property_helper (self->v4l2capture,
190 prop_id, value, pspec))
191 {
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
193 }
194 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800195#if GST_IMPORT_LGE_PROP
196 case LGE_DECODE_SIZE:
197 {
bo.xiao857b8682024-09-12 16:40:32 +0800198 GST_DEBUG_OBJECT(self, "LGE up layer get dec size");
199 self->lge_ctxt->dec_size = -1;
200 g_value_set_int(value, self->lge_ctxt->dec_size);
201 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800202 }
203 case LGE_UNDECODE_SIZE:
204 {
bo.xiao857b8682024-09-12 16:40:32 +0800205 GST_DEBUG_OBJECT(self, "LGE up layer get undec size");
206 self->lge_ctxt->undec_size = -1;
207 g_value_set_int(value, self->lge_ctxt->undec_size);
208 break;
xuesong.jiang61ea8012022-05-12 15:38:17 +0800209 }
210#endif
211
bo.xiao857b8682024-09-12 16:40:32 +0800212 /* By default read from output */
213 default:
214 if (!gst_aml_v4l2_object_get_property_helper (self->v4l2output,
215 prop_id, value, pspec))
216 {
217 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800218 }
bo.xiao857b8682024-09-12 16:40:32 +0800219 break;
220 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800221}
222
223static gboolean
le.han02c38f02024-08-16 02:35:36 +0000224gst_aml_v4l2_video_dec_open(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800225{
bo.xiao857b8682024-09-12 16:40:32 +0800226 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
227 GstCaps *codec_caps;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800228
bo.xiao857b8682024-09-12 16:40:32 +0800229 GST_DEBUG_OBJECT (self, "Opening");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800230
bo.xiao857b8682024-09-12 16:40:32 +0800231 if (!gst_aml_v4l2_object_open (self->v4l2output))
xuesong.jiangae1548e2022-05-06 16:38:46 +0800232 goto failure;
233
bo.xiao857b8682024-09-12 16:40:32 +0800234 if (!gst_aml_v4l2_object_open_shared (self->v4l2capture, self->v4l2output))
235 goto failure;
236
237 codec_caps = gst_pad_get_pad_template_caps (decoder->sinkpad);
238 self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps (self->v4l2output,
239 codec_caps);
240 gst_caps_unref (codec_caps);
241
242 if (gst_caps_is_empty (self->probed_sinkcaps))
243 goto no_encoded_format;
244
245 return TRUE;
246
247no_encoded_format:
248 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
249 (_("Decoder on device %s has no supported input format"),
250 self->v4l2output->videodev), (NULL));
251 goto failure;
252
xuesong.jiangae1548e2022-05-06 16:38:46 +0800253failure:
bo.xiao857b8682024-09-12 16:40:32 +0800254 if (GST_AML_V4L2_IS_OPEN (self->v4l2output))
255 gst_aml_v4l2_object_close (self->v4l2output);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800256
bo.xiao857b8682024-09-12 16:40:32 +0800257 if (GST_AML_V4L2_IS_OPEN (self->v4l2capture))
258 gst_aml_v4l2_object_close (self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800259
bo.xiao857b8682024-09-12 16:40:32 +0800260 gst_caps_replace (&self->probed_srccaps, NULL);
261 gst_caps_replace (&self->probed_sinkcaps, NULL);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800262
bo.xiao857b8682024-09-12 16:40:32 +0800263 return FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800264}
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{
bo.xiao857b8682024-09-12 16:40:32 +0800269 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800270
bo.xiao857b8682024-09-12 16:40:32 +0800271 GST_DEBUG_OBJECT (self, "Closing");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800272
bo.xiao857b8682024-09-12 16:40:32 +0800273 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);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800277
bo.xiao857b8682024-09-12 16:40:32 +0800278 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800279}
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{
bo.xiao857b8682024-09-12 16:40:32 +0800284 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800285
bo.xiao857b8682024-09-12 16:40:32 +0800286 GST_DEBUG_OBJECT (self, "Starting");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800287
bo.xiao857b8682024-09-12 16:40:32 +0800288 gst_aml_v4l2_object_flush_start (self->v4l2output);
289 g_atomic_int_set (&self->active, TRUE);
290 self->output_flow = GST_FLOW_OK;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800291
bo.xiao857b8682024-09-12 16:40:32 +0800292 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800293}
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{
bo.xiao857b8682024-09-12 16:40:32 +0800298 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800299
bo.xiao857b8682024-09-12 16:40:32 +0800300 GST_DEBUG_OBJECT (self, "Stopping");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800301
bo.xiao857b8682024-09-12 16:40:32 +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
bo.xiao857b8682024-09-12 16:40:32 +0800305 /* Wait for capture thread to stop */
306 gst_pad_stop_task (decoder->srcpad);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800307
bo.xiao857b8682024-09-12 16:40:32 +0800308 GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder);
309 self->output_flow = GST_FLOW_OK;
310 GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800311
bo.xiao857b8682024-09-12 16:40:32 +0800312 /* Should have been flushed already */
313 g_assert (g_atomic_int_get (&self->active) == FALSE);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800314
bo.xiao857b8682024-09-12 16:40:32 +0800315 gst_aml_v4l2_object_stop(self->v4l2output);
316 gst_aml_v4l2_object_stop(self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800317
bo.xiao857b8682024-09-12 16:40:32 +0800318 if (self->input_state)
319 {
320 gst_aml_video_codec_state_unref(self->input_state);
321 self->input_state = NULL;
322 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800323
bo.xiao857b8682024-09-12 16:40:32 +0800324 GST_DEBUG_OBJECT (self, "Stopped");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800325
bo.xiao857b8682024-09-12 16:40:32 +0800326 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800327}
328
329static gboolean
hanghang.luo68527bd2024-11-04 13:41:42 +0800330gst_aml_v4l2_video_dec_update_probe_src_caps(GstAmlVideoDecoder *decoder)
331{
332 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
333 GstCaps *caps;
334
335 GST_DEBUG_OBJECT (self, "update probe src caps");
336 gst_caps_replace (&self->probed_srccaps, NULL);
337 self->probed_srccaps = gst_aml_v4l2_object_probe_caps (self->v4l2capture,
338 gst_aml_v4l2_object_get_raw_caps ());
339 if (gst_caps_is_empty (self->probed_srccaps))
340 {
341 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
342 (_("Decoder on device %s has no supported output format"),
343 self->v4l2output->videodev), (NULL));
344 return FALSE;
345 }
346
347 caps = gst_caps_copy(self->probed_srccaps);
348 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
349 gst_caps_append(self->probed_srccaps, caps);
350 GST_DEBUG_OBJECT (self, "update probe src caps: %" GST_PTR_FORMAT,self->probed_srccaps);
351 return TRUE;
352}
353
354static gboolean
le.han02c38f02024-08-16 02:35:36 +0000355gst_aml_v4l2_video_dec_codec_chg(GstAmlVideoDecoder *decoder,
356 GstAmlVideoCodecState *state)
hanghang.luo8e1225b2023-10-10 08:54:28 +0000357{
bo.xiao857b8682024-09-12 16:40:32 +0800358 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
359 GstStructure *s_old = NULL;
360 GstStructure *s_new = NULL;
hanghang.luo8e1225b2023-10-10 08:54:28 +0000361
bo.xiao857b8682024-09-12 16:40:32 +0800362 // first play, must set foramt;
363 if (!self->input_state)
364 return TRUE;
hanghang.luo8e1225b2023-10-10 08:54:28 +0000365
bo.xiao857b8682024-09-12 16:40:32 +0800366 if (self->input_state->caps)
367 s_old = gst_caps_get_structure(self->input_state->caps,0);
368 if (state->caps)
369 s_new = gst_caps_get_structure(state->caps,0);
hanghang.luo8e1225b2023-10-10 08:54:28 +0000370
bo.xiao857b8682024-09-12 16:40:32 +0800371 if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old)))
372 return TRUE;
373 return FALSE;
hanghang.luo8e1225b2023-10-10 08:54:28 +0000374}
375
hanghang.luo8e1225b2023-10-10 08:54:28 +0000376
377static gboolean
le.han02c38f02024-08-16 02:35:36 +0000378gst_aml_v4l2_video_dec_set_format(GstAmlVideoDecoder *decoder,
379 GstAmlVideoCodecState *state)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800380{
hanghang.luo68527bd2024-11-04 13:41:42 +0800381
bo.xiao857b8682024-09-12 16:40:32 +0800382 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
383 gboolean ret = TRUE;
384 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
bo.xiao857b8682024-09-12 16:40:32 +0800385 GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0);
386 GstStructure *s = gst_caps_get_structure(state->caps,0);
hanghang.luo68527bd2024-11-04 13:41:42 +0800387
388 GST_DEBUG_OBJECT (self, "Setting format: %" GST_PTR_FORMAT, state->caps);
389
bo.xiao857b8682024-09-12 16:40:32 +0800390 if (s && gst_structure_has_field(s,"format"))
391 {
392 if (!strcmp("XVID",gst_structure_get_string(s,"format")))
393 {
394 GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support");
395 ret = FALSE;
396 goto done;
397 }
398 }
399
400 if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
401 self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT;
402
403 if (self->input_state)
404 {
hanghang.luo68527bd2024-11-04 13:41:42 +0800405 // 1、Only resolution change caused by codec changes are triggered by the gst,
406 // 2、the rest are triggered by drive.
407 if (!gst_aml_v4l2_video_dec_codec_chg(decoder,state))
408 {
409 if ((ret = gst_aml_v4l2_video_dec_update_probe_src_caps(decoder)) == FALSE)
410 GST_ERROR_OBJECT(self, "update probe caps error");
411 goto done;
412 }
413 }
bo.xiao857b8682024-09-12 16:40:32 +0800414
hanghang.luo68527bd2024-11-04 13:41:42 +0800415 if (self->input_state)
416 {
bo.xiao857b8682024-09-12 16:40:32 +0800417 gst_aml_v4l2_video_dec_finish (decoder);
418 gst_aml_v4l2_object_stop (self->v4l2output);
419
420 gst_aml_video_codec_state_unref(self->input_state);
421 self->input_state = NULL;
422
423 /* The renegotiation flow don't blend with the base class flow. To properly
424 * stop the capture pool, if the buffers can't be orphaned, we need to
425 * reclaim our buffers, which will happend through the allocation query.
426 * The allocation query is triggered by gst_aml_video_decoder_negotiate() which
427 * requires the output caps to be set, but we can't know this information
428 * as we rely on the decoder, which requires the capture queue to be
429 * stopped.
430 *
431 * To workaround this issue, we simply run an allocation query with the
432 * old negotiated caps in order to drain/reclaim our buffers. That breaks
433 * the complexity and should not have much impact in performance since the
434 * following allocation query will happen on a drained pipeline and won't
435 * block. */
436 if (self->v4l2capture->pool &&
437 !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool))
438 {
439 GstCaps *caps = gst_pad_get_current_caps (decoder->srcpad);
440 if (caps)
441 {
442 GstQuery *query = gst_query_new_allocation (caps, FALSE);
443 gst_pad_peer_query (decoder->srcpad, query);
444 gst_query_unref (query);
445 gst_caps_unref (caps);
446 }
447 }
448
449 gst_aml_v4l2_object_stop (self->v4l2capture);
450 self->output_flow = GST_FLOW_OK;
451 }
452 if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE)
453 {
454 GST_ERROR_OBJECT(self, "config output drm mode error");
455 goto done;
456 }
457
458 if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE)
459 {
460 GST_ERROR_OBJECT(self, "config output stream mode error");
461 goto done;
462 }
463
hanghang.luo68527bd2024-11-04 13:41:42 +0800464 if ((ret = gst_aml_v4l2_object_set_format (self->v4l2output, state->caps, &error)) == FALSE)
bo.xiao857b8682024-09-12 16:40:32 +0800465 {
466 GST_ERROR_OBJECT(self, "set format error");
467 goto done;
468 }
469
470 // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT.
471 if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE)
472 {
473 GST_ERROR_OBJECT(self, "config I frame mode error");
474 goto done;
475 }
476
hanghang.luo68527bd2024-11-04 13:41:42 +0800477 if ((ret = gst_aml_v4l2_video_dec_update_probe_src_caps(decoder)) == FALSE)
478 {
479 GST_ERROR_OBJECT(self, "update probe caps error");
480 goto done;
481 }
bo.xiao857b8682024-09-12 16:40:32 +0800482
bo.xiao857b8682024-09-12 16:40:32 +0800483 if (ret)
484 self->input_state = gst_aml_video_codec_state_ref (state);
485 else
486 gst_aml_v4l2_error (self, &error);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800487
488done:
bo.xiao857b8682024-09-12 16:40:32 +0800489 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800490}
491
492static gboolean
le.han02c38f02024-08-16 02:35:36 +0000493gst_aml_v4l2_video_dec_flush(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800494{
bo.xiao857b8682024-09-12 16:40:32 +0800495 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800496
bo.xiao857b8682024-09-12 16:40:32 +0800497 GST_DEBUG_OBJECT(self, "Flushed");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800498
bo.xiao857b8682024-09-12 16:40:32 +0800499 /* Ensure the processing thread has stopped for the reverse playback
500 * discount case */
501 if (gst_pad_get_task_state (decoder->srcpad) == GST_TASK_STARTED)
502 {
503 GST_AML_VIDEO_DECODER_STREAM_UNLOCK (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800504
bo.xiao857b8682024-09-12 16:40:32 +0800505 gst_aml_v4l2_object_flush_start (self->v4l2output);
506 gst_aml_v4l2_object_flush_start (self->v4l2capture);
507 gst_pad_stop_task (decoder->srcpad);
508 GST_AML_VIDEO_DECODER_STREAM_LOCK (decoder);
509 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800510
bo.xiao857b8682024-09-12 16:40:32 +0800511 self->output_flow = GST_FLOW_OK;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800512
bo.xiao857b8682024-09-12 16:40:32 +0800513 gst_aml_v4l2_object_flush_stop (self->v4l2output);
514 gst_aml_v4l2_object_flush_stop (self->v4l2capture);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800515
bo.xiao857b8682024-09-12 16:40:32 +0800516 if (self->v4l2output->pool)
517 gst_aml_v4l2_buffer_pool_flush (self->v4l2output->pool);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800518
bo.xiao857b8682024-09-12 16:40:32 +0800519 /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be
520 * called after gst_aml_v4l2_object_flush_stop() stopped flushing the buffer
521 * pool. */
522 if (self->v4l2capture->pool)
523 gst_aml_v4l2_buffer_pool_flush (self->v4l2capture->pool);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800524
bo.xiao857b8682024-09-12 16:40:32 +0800525 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800526}
527
528static gboolean
le.han02c38f02024-08-16 02:35:36 +0000529gst_aml_v4l2_video_dec_negotiate(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800530{
bo.xiao857b8682024-09-12 16:40:32 +0800531 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800532
xuesong.jiang0223de52024-09-13 15:33:10 +0800533 if (TRUE == decoder->svp)
bo.xiao857b8682024-09-12 16:40:32 +0800534 {
535 GstStructure *s;
536 GstEvent *event;
xuesong.jiang681d3602022-06-24 21:23:35 +0800537
bo.xiao857b8682024-09-12 16:40:32 +0800538 s = gst_structure_new_empty ("IS_SVP");
539 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s);
540 GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event);
541 gst_pad_push_event (decoder->srcpad, event);
542 GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event);
543 }
xuesong.jiang681d3602022-06-24 21:23:35 +0800544
bo.xiao857b8682024-09-12 16:40:32 +0800545 /* We don't allow renegotiation without carefull disabling the pool */
546 if (self->v4l2capture->pool &&
547 gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool)))
548 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800549
bo.xiao857b8682024-09-12 16:40:32 +0800550 return GST_AML_VIDEO_DECODER_CLASS (parent_class)->negotiate (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800551}
552
553static gboolean
554gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags)
555{
bo.xiao857b8682024-09-12 16:40:32 +0800556 struct v4l2_decoder_cmd dcmd = { 0, };
xuesong.jiangae1548e2022-05-06 16:38:46 +0800557
bo.xiao857b8682024-09-12 16:40:32 +0800558 GST_DEBUG_OBJECT (v4l2object->element,
559 "sending v4l2 decoder command %u with flags %u", cmd, flags);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800560
bo.xiao857b8682024-09-12 16:40:32 +0800561 if (!GST_AML_V4L2_IS_OPEN (v4l2object))
562 return FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800563
bo.xiao857b8682024-09-12 16:40:32 +0800564 dcmd.cmd = cmd;
565 dcmd.flags = flags;
566 if (v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0)
567 goto dcmd_failed;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800568
bo.xiao857b8682024-09-12 16:40:32 +0800569 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800570
571dcmd_failed:
bo.xiao857b8682024-09-12 16:40:32 +0800572 if (errno == ENOTTY)
573 {
574 GST_INFO_OBJECT (v4l2object->element,
575 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
576 cmd, flags, v4l2object->videodev, g_strerror (errno));
577 }
578 else
579 {
580 GST_ERROR_OBJECT (v4l2object->element,
581 "Failed to send decoder command %u with flags %u for '%s'. (%s)",
582 cmd, flags, v4l2object->videodev, g_strerror (errno));
583 }
584 return FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800585}
586
587static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +0000588gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800589{
bo.xiao857b8682024-09-12 16:40:32 +0800590 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (decoder);
591 GstFlowReturn ret = GST_FLOW_OK;
592 GstBuffer *buffer;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800593
bo.xiao857b8682024-09-12 16:40:32 +0800594 if (gst_pad_get_task_state (decoder->srcpad) != GST_TASK_STARTED)
595 goto done;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800596
bo.xiao857b8682024-09-12 16:40:32 +0800597 GST_DEBUG_OBJECT (self, "Finishing decoding");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800598
bo.xiao857b8682024-09-12 16:40:32 +0800599 GST_AML_VIDEO_DECODER_STREAM_UNLOCK (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800600
bo.xiao857b8682024-09-12 16:40:32 +0800601 if (gst_aml_v4l2_decoder_cmd (self->v4l2output, V4L2_DEC_CMD_STOP, 0))
602 {
603 GstTask *task = decoder->srcpad->task;
604
605 /* If the decoder stop command succeeded, just wait until processing is
606 * finished */
607 GST_DEBUG_OBJECT (self, "Waiting for decoder stop");
608 GST_OBJECT_LOCK (task);
609 while (GST_TASK_STATE (task) == GST_TASK_STARTED)
610 GST_TASK_WAIT (task);
611 GST_OBJECT_UNLOCK (task);
612
613 ret = GST_FLOW_FLUSHING;
614 }
615 else
616 {
617 /* otherwise keep queuing empty buffers until the processing thread has
618 * stopped, _pool_process() will return FLUSHING when that happened */
619 while (ret == GST_FLOW_OK)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800620 {
bo.xiao857b8682024-09-12 16:40:32 +0800621 GST_DEBUG_OBJECT(self, "queue empty output buf");
622 buffer = gst_buffer_new ();
623 ret =
624 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer);
625 gst_buffer_unref (buffer);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800626 }
bo.xiao857b8682024-09-12 16:40:32 +0800627 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800628
bo.xiao857b8682024-09-12 16:40:32 +0800629 /* and ensure the processing thread has stopped in case another error
630 * occurred. */
631 gst_aml_v4l2_object_flush_start (self->v4l2capture);
632 gst_pad_stop_task (decoder->srcpad);
633 GST_AML_VIDEO_DECODER_STREAM_LOCK (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800634
bo.xiao857b8682024-09-12 16:40:32 +0800635 if (ret == GST_FLOW_FLUSHING)
636 ret = self->output_flow;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800637
bo.xiao857b8682024-09-12 16:40:32 +0800638 /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop.
639 should reset need_wait_event=true to wait source change event*/
640 self->v4l2capture->need_wait_event = TRUE;
641 GST_DEBUG_OBJECT (decoder, "Done draining buffers");
xuesong.jiangae1548e2022-05-06 16:38:46 +0800642
bo.xiao857b8682024-09-12 16:40:32 +0800643 /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */
xuesong.jiangae1548e2022-05-06 16:38:46 +0800644
645done:
bo.xiao857b8682024-09-12 16:40:32 +0800646 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800647}
648
649static GstFlowReturn
le.han02c38f02024-08-16 02:35:36 +0000650gst_aml_v4l2_video_dec_drain(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +0800651{
652 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
653
bo.xiao857b8682024-09-12 16:40:32 +0800654 GST_DEBUG_OBJECT (self, "Draining...");
655 gst_aml_v4l2_video_dec_finish (decoder);
656 gst_aml_v4l2_video_dec_flush (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +0800657
bo.xiao857b8682024-09-12 16:40:32 +0800658 return GST_FLOW_OK;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800659}
660
le.han02c38f02024-08-16 02:35:36 +0000661static GstAmlVideoCodecFrame *
662gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstAmlVideoDecoder *decoder, GstClockTime pts)
fei.dengbee20862022-06-14 14:59:48 +0800663{
bo.xiao857b8682024-09-12 16:40:32 +0800664 GstAmlVideoCodecFrame *frame = NULL;
665 GList *frames, *l;
666 gint count = 0;
fei.dengbee20862022-06-14 14:59:48 +0800667
bo.xiao857b8682024-09-12 16:40:32 +0800668 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000669
bo.xiao857b8682024-09-12 16:40:32 +0800670 frames = gst_aml_video_decoder_get_frames(decoder);
fei.dengbee20862022-06-14 14:59:48 +0800671
bo.xiao857b8682024-09-12 16:40:32 +0800672 for (l = frames; l != NULL; l = l->next)
673 {
674 GstAmlVideoCodecFrame *f = l->data;
fei.denge9458472023-04-18 02:05:48 +0000675
bo.xiao857b8682024-09-12 16:40:32 +0800676 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) {
677 frame = f;
678 }
679 count++;
680 }
fei.dengbee20862022-06-14 14:59:48 +0800681
bo.xiao857b8682024-09-12 16:40:32 +0800682 if (!frame)
683 {
684 for (l = frames; l != NULL; l = l->next)
685 {
686 GstAmlVideoCodecFrame *f = l->data;
687 if (!GST_CLOCK_TIME_IS_VALID(f->pts))
688 {
689 frame = f;
690 GST_DEBUG("The pts of the expected output frame is invalid");
691 break;
692 }
693 }
694 }
zengliang.liddee2da2023-07-14 07:27:05 +0000695
bo.xiao857b8682024-09-12 16:40:32 +0800696 if (frame)
697 {
698 GST_LOG_OBJECT(decoder,
699 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
700 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1);
701 gst_aml_video_codec_frame_ref(frame);
702 }
703 else
704 {
705 GST_LOG_OBJECT(decoder,
706 "buffer %" GST_TIME_FORMAT " unmatch, create new frame", GST_TIME_ARGS(pts));
707 frame = gst_aml_video_decoder_v4l2_new_frame (decoder);
708 }
fei.dengbee20862022-06-14 14:59:48 +0800709
bo.xiao857b8682024-09-12 16:40:32 +0800710 g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref);
fei.dengbee20862022-06-14 14:59:48 +0800711
bo.xiao857b8682024-09-12 16:40:32 +0800712 return frame;
fei.dengbee20862022-06-14 14:59:48 +0800713}
714
le.han02c38f02024-08-16 02:35:36 +0000715static GstAmlVideoCodecFrame *
716gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstAmlVideoDecoder *decoder, GstClockTime pts)
xuesong.jiange24aef92023-06-16 06:39:10 +0000717{
bo.xiao857b8682024-09-12 16:40:32 +0800718 GstAmlVideoCodecFrame *frame = NULL;
719 GList *frames, *l;
720 guint frames_len = 0;
721 GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000722
bo.xiao857b8682024-09-12 16:40:32 +0800723 if (!(frames = gst_aml_video_decoder_get_frames(decoder)))
724 goto done;
hanghang.luo4486d7d2024-07-24 10:06:35 +0800725
bo.xiao857b8682024-09-12 16:40:32 +0800726 frames_len = g_list_length(frames);
727 GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len);
xuesong.jiange24aef92023-06-16 06:39:10 +0000728
bo.xiao857b8682024-09-12 16:40:32 +0800729 for (l = frames; l != NULL; l = l->next)
730 {
731 GstAmlVideoCodecFrame *f = l->data;
xuesong.jiange24aef92023-06-16 06:39:10 +0000732
bo.xiao857b8682024-09-12 16:40:32 +0800733 if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000)
734 {
735 /* found the right frame */
736 GST_LOG_OBJECT(decoder,
737 "found frame %" GST_TIME_FORMAT "with pts %" GST_TIME_FORMAT,
738 GST_TIME_ARGS(f->pts), GST_TIME_ARGS(pts));
739 frame = f;
740 break;
741 }
742 else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts))
743 {
744 GST_LOG_OBJECT(decoder,
745 "stream mode drop frame %d %" GST_TIME_FORMAT,
746 f->system_frame_number, GST_TIME_ARGS(f->pts));
xuesong.jiange24aef92023-06-16 06:39:10 +0000747
bo.xiao857b8682024-09-12 16:40:32 +0800748 gst_aml_video_codec_frame_ref(f);
749 // gst_aml_video_decoder_drop_frame(decoder, f);
750 gst_aml_video_decoder_release_frame(decoder, f);
751 }
752 else
753 {
754 GST_LOG_OBJECT (decoder, "dbg");
755 }
756 }
xuesong.jiange24aef92023-06-16 06:39:10 +0000757
bo.xiao857b8682024-09-12 16:40:32 +0800758 if (frame)
759 {
760 guint l_len = 0;
761 l = gst_aml_video_decoder_get_frames(decoder);
762 l_len = g_list_length(l);
763 g_list_free_full(l, (GDestroyNotify)gst_aml_video_codec_frame_unref);
xuesong.jiange24aef92023-06-16 06:39:10 +0000764
bo.xiao857b8682024-09-12 16:40:32 +0800765 GST_LOG_OBJECT(decoder,
766 "frame %p is %d %" GST_TIME_FORMAT " and %d frames left",
767 frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len);
768 gst_aml_video_codec_frame_ref(frame);
769 }
xuesong.jiange24aef92023-06-16 06:39:10 +0000770
bo.xiao857b8682024-09-12 16:40:32 +0800771 g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref);
xuesong.jiange24aef92023-06-16 06:39:10 +0000772
hanghang.luo4486d7d2024-07-24 10:06:35 +0800773done:
bo.xiao857b8682024-09-12 16:40:32 +0800774 return frame;
xuesong.jiange24aef92023-06-16 06:39:10 +0000775}
776
le.han02c38f02024-08-16 02:35:36 +0000777static GstAmlVideoCodecFrame *
778gst_aml_v4l2_video_dec_get_right_frame(GstAmlVideoDecoder *decoder, GstClockTime pts)
xuesong.jiange24aef92023-06-16 06:39:10 +0000779{
bo.xiao857b8682024-09-12 16:40:32 +0800780 GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder;
781 if (self->v4l2output->stream_mode)
782 return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts);
783 else
784 return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts);
xuesong.jiange24aef92023-06-16 06:39:10 +0000785}
786
xuesong.jiangae1548e2022-05-06 16:38:46 +0800787static gboolean
788gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features,
789 GstStructure *structure, gpointer user_data)
790{
bo.xiao857b8682024-09-12 16:40:32 +0800791 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data);
792 GstVideoAlignment *align = &self->v4l2capture->align;
793 GstVideoInfo *info = &self->v4l2capture->info;
794 int width, height;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800795
bo.xiao857b8682024-09-12 16:40:32 +0800796 if (!gst_structure_get_int(structure, "width", &width))
797 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800798
bo.xiao857b8682024-09-12 16:40:32 +0800799 if (!gst_structure_get_int(structure, "height", &height))
800 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800801
bo.xiao857b8682024-09-12 16:40:32 +0800802 if (align->padding_left != 0 || align->padding_top != 0 ||
803 height != info->height + align->padding_bottom)
804 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800805
bo.xiao857b8682024-09-12 16:40:32 +0800806 if (height == info->height + align->padding_bottom)
807 {
808 /* Some drivers may round up width to the padded with */
809 if (width == info->width + align->padding_right)
810 gst_structure_set(structure,
811 "width", G_TYPE_INT, width - align->padding_right,
812 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
813 /* Some drivers may keep visible width and only round up bytesperline */
814 else if (width == info->width)
815 gst_structure_set(structure,
816 "height", G_TYPE_INT, height - align->padding_bottom, NULL);
817 }
xuesong.jiangae1548e2022-05-06 16:38:46 +0800818
bo.xiao857b8682024-09-12 16:40:32 +0800819 return TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +0800820}
821
822static void
sheng.liubcf036c2022-06-21 15:55:42 +0800823gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object)
sheng.liub56bbc52022-06-21 11:02:33 +0800824{
825 struct v4l2_event evt;
826 gint ret;
827
828 memset (&evt, 0x00, sizeof (struct v4l2_event));
829 ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt);
830 if (ret < 0)
831 {
832 GST_DEBUG_OBJECT (v4l2object, "dqevent failed");
833 return;
834 }
835
836 switch (evt.type)
837 {
838 case V4L2_EVENT_SOURCE_CHANGE:
839 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE");
840 break;
841 case V4L2_EVENT_EOS:
842 GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER");
843 break;
844 default:
845 break;
846 }
847
848 return;
849}
850
851static void
le.han02c38f02024-08-16 02:35:36 +0000852gst_aml_v4l2_video_dec_set_output_status(GstAmlVideoDecoder *decoder,GstVideoInfo info)
hanghang.luo2eec4892023-07-18 06:44:42 +0000853{
bo.xiao857b8682024-09-12 16:40:32 +0800854 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
855 GstAmlVideoCodecState *output_state;
856 struct v4l2_selection sel;
857 struct v4l2_rect *r = NULL;
858 GstStructure *s;
859 gint width = 0;
860 gint height = 0;
861 GST_DEBUG("%d %d",info.width, info.height);
862 output_state = gst_aml_video_decoder_set_output_state(decoder,
863 info.finfo->format, info.width, info.height, self->input_state);
864 memset(&sel, 0, sizeof(struct v4l2_selection));
865 sel.type = self->v4l2capture->type;
le.han3b118242024-09-29 06:32:02 +0000866 sel.target = V4L2_SEL_TGT_CROP_DEFAULT;
bo.xiao857b8682024-09-12 16:40:32 +0800867 if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0)
868 {
869 r = &sel.r;
870 width = (r->width/2)*2;
871 height = (r->height/2)*2;
872 GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height);
873 }
874 else
875 GST_DEBUG_OBJECT(self, "iotcl error");
876 if (output_state)
877 {
878 output_state->info.interlace_mode = info.interlace_mode;
879 output_state->allocation_caps =gst_video_info_to_caps(&info);
880 output_state->caps =gst_video_info_to_caps(&info);
881 s = gst_caps_get_structure(output_state->caps, 0);
882 if (s)
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000883 {
bo.xiao857b8682024-09-12 16:40:32 +0800884 gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL);
885 gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL);
886 gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL);
887 gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL);
le.han3b118242024-09-29 06:32:02 +0000888 gst_structure_set(s,"dw_mode",G_TYPE_INT,self->v4l2output->dw_mode,NULL);
889 gst_structure_set(s,"stride",G_TYPE_INT,self->v4l2output->stride,NULL);
bo.xiao857b8682024-09-12 16:40:32 +0800890 GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps);
891 gst_aml_video_codec_state_unref(output_state);
hanghang.luo9b60a3c2023-08-01 16:01:47 +0000892 }
bo.xiao857b8682024-09-12 16:40:32 +0800893 }
hanghang.luo2eec4892023-07-18 06:44:42 +0000894}
895
zengliang.lidcd41462024-06-19 16:05:12 +0800896static GQuark
897gst_aml_v4l2_buffer_pool_cc_import_quark (void)
898{
899 static GQuark quark = 0;
900
901 if (quark == 0)
902 quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData");
903
904 return quark;
905}
906
907static gboolean
le.han02c38f02024-08-16 02:35:36 +0000908foreach_cc_buffer_list_match_pts_func(GList *list , GstAmlVideoCodecFrame *frame)
zengliang.lidcd41462024-06-19 16:05:12 +0800909{
bo.xiao857b8682024-09-12 16:40:32 +0800910 GList *l;
911 if (g_list_length (list) > 0)
912 {
913 for (l = list; l != NULL; l = l->next)
zengliang.lidcd41462024-06-19 16:05:12 +0800914 {
bo.xiao857b8682024-09-12 16:40:32 +0800915 GstBuffer *cc_buffer = l->data;
916 if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer))
917 {
918 gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK,
919 gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref);
920 #if 0
921 //Debug code:dump cc data
922 GstMapInfo gst_map;
923 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
924 int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777);
925 if (gst_map.size>0)
926 write(fd,gst_map.data,gst_map.size);
927 close(fd);
928 gst_buffer_unmap(cc_buffer,&gst_map);
929 #endif
930 GST_DEBUG("match success");
931 return TRUE;
932 }
933 else
934 {
935 GST_DEBUG("match fail");
936 }
zengliang.lidcd41462024-06-19 16:05:12 +0800937 }
bo.xiao857b8682024-09-12 16:40:32 +0800938 GST_WARNING("no match frame in the bufferlist");
939 }
940 else
941 {
942 GST_WARNING("list is null,can not foreach");
943 }
944 return FALSE;
zengliang.lidcd41462024-06-19 16:05:12 +0800945}
946
bo.xiao8e3054d2024-07-31 17:13:57 +0800947static GstClockTime
le.han44125c32024-09-03 07:55:57 +0000948gst_aml_v4l2_video_dec_calc_output_buffer_pts(GstAmlVideoDecoder *decoder)
bo.xiao8e3054d2024-07-31 17:13:57 +0800949{
bo.xiao857b8682024-09-12 16:40:32 +0800950 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
951 GstClockTime pts = GST_CLOCK_TIME_NONE;
bo.xiao8e3054d2024-07-31 17:13:57 +0800952
bo.xiao857b8682024-09-12 16:40:32 +0800953 if (GST_CLOCK_TIME_IS_VALID (self->last_out_pts) && GST_CLOCK_TIME_IS_VALID(self->frame_duration)) {
954 pts = self->last_out_pts + self->frame_duration;
955 GST_LOG_OBJECT (decoder,
956 "calculate PTS %" GST_TIME_FORMAT " by duration: %" GST_TIME_FORMAT,
957 GST_TIME_ARGS (pts), GST_TIME_ARGS (self->frame_duration));
958 }
959 else
960 {
961 pts = 0;
962 GST_INFO_OBJECT (decoder,"Set PTS=0");
963 }
964 return pts;
bo.xiao8e3054d2024-07-31 17:13:57 +0800965}
966
967static GstClockTime
le.han44125c32024-09-03 07:55:57 +0000968gst_aml_v4l2_video_dec_calc_duration(GstAmlVideoDecoder *decoder)
bo.xiao8e3054d2024-07-31 17:13:57 +0800969{
970 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
971 GstClockTime duration = GST_CLOCK_TIME_NONE;
972 gint fps_n, fps_d;
973 fps_n = gst_value_get_fraction_numerator(self->v4l2capture->fps);
974 fps_d = gst_value_get_fraction_denominator(self->v4l2capture->fps);
975
976 if (fps_n != 0 && fps_d != 0)
977 {
978 duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n);
979
980 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
981 {
982 duration = duration / 2;
983 }
984 }
985
986 GST_INFO_OBJECT (decoder,"framerate(fps_d: %d, fps_n: %d) -> frame_duration = %" GST_TIME_FORMAT, fps_d, fps_n, GST_TIME_ARGS(duration));
987
988 return duration;
989}
990
xuesong.jiang0223de52024-09-13 15:33:10 +0800991static GstFlowReturn
992gst_aml_v4l2_video_dec_clip_and_push_frame (GstAmlVideoDecoder * decoder, GstAmlVideoCodecFrame * frame)
993{
994 GstFlowReturn ret = GST_FLOW_OK;
995 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
996 gboolean drop = FALSE;
997
998 if ((decoder->input_segment.rate > 0 && GST_CLOCK_TIME_NONE == decoder->input_segment.start) ||
999 (decoder->input_segment.rate < 0 && GST_CLOCK_TIME_NONE == decoder->input_segment.stop))
1000 {
1001 GST_DEBUG_OBJECT(self, "don't do clip when we have no input segment");
1002 goto done;
1003 }
1004
1005 if (decoder->input_segment.rate > 0)
1006 {
1007 GstClockTime duration = 0;
1008
1009 if (GST_CLOCK_TIME_NONE != frame->duration)
1010 duration = frame->duration;
1011
1012 if ((frame->pts + duration) < decoder->input_segment.start)
1013 {
1014 GST_DEBUG_OBJECT(self, "drop frame with pts(dur %" GST_TIME_FORMAT ") %" GST_TIME_FORMAT " < start %" GST_TIME_FORMAT,
1015 GST_TIME_ARGS(frame->pts), GST_TIME_ARGS(frame->duration), GST_TIME_ARGS(decoder->input_segment.start));
1016 drop = TRUE;
1017 goto done;
1018 }
1019 }
1020 else if (decoder->input_segment.rate < 0)
1021 {
1022 if (frame->pts > decoder->input_segment.stop)
1023 {
1024 GST_DEBUG_OBJECT(self, "drop frame with pts(dur %" GST_TIME_FORMAT ") %" GST_TIME_FORMAT " > stop %" GST_TIME_FORMAT,
1025 GST_TIME_ARGS(frame->pts), GST_TIME_ARGS(frame->duration), GST_TIME_ARGS(decoder->input_segment.stop));
1026 drop = TRUE;
1027 goto done;
1028 }
1029 }
1030
1031done:
1032 if (G_LIKELY (!drop))
1033 ret = gst_aml_video_decoder_finish_frame(decoder, frame);
1034 else
1035 ret = gst_aml_video_decoder_drop_frame(decoder, frame);
1036 return ret;
1037}
1038
hanghang.luo2eec4892023-07-18 06:44:42 +00001039static void
le.han02c38f02024-08-16 02:35:36 +00001040gst_aml_v4l2_video_dec_loop(GstAmlVideoDecoder *decoder)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001041{
bo.xiao857b8682024-09-12 16:40:32 +08001042 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1043 GstAmlV4l2BufferPool *v4l2_pool;
1044 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1045 GstBufferPool *pool;
1046 GstAmlVideoCodecFrame *frame;
1047 GstBuffer *buffer = NULL;
1048 GstFlowReturn ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001049
bo.xiao857b8682024-09-12 16:40:32 +08001050 if (G_UNLIKELY (!GST_AML_V4L2_IS_ACTIVE (self->v4l2capture)))
1051 {
1052 GstVideoInfo info;
1053 GstCaps *acquired_caps, *available_caps, *caps, *filter;
1054 GstStructure *st;
1055 GST_DEBUG_OBJECT(self, "waitting source change event");
1056 /* Wait until received SOURCE_CHANGE event to get right video format */
1057 while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001058 {
bo.xiao857b8682024-09-12 16:40:32 +08001059 ret = gst_aml_v4l2_object_dqevent (self->v4l2capture);
1060 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1061 {
1062 //let flush start event blocked until capture buffer pool actived
1063 self->is_res_chg = TRUE;
1064 GST_DEBUG_OBJECT (self, "Received source change event");
1065 break;
1066 }
1067 else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER)
1068 {
1069 GST_DEBUG_OBJECT (self, "Received eos event");
1070 goto beach;
1071 }
1072 else if (ret != GST_FLOW_OK)
1073 {
1074 GST_ERROR_OBJECT (self, "dqevent error");
1075 goto beach;
1076 }
1077 }
1078 self->v4l2capture->need_wait_event = FALSE;
1079
xuesong.jiang0223de52024-09-13 15:33:10 +08001080 if (TRUE == decoder->svp)
bo.xiao857b8682024-09-12 16:40:32 +08001081 {
1082 GstPad *peer;
1083 GstStructure *s;
1084 GstEvent *event;
1085
1086 peer = gst_pad_get_peer (decoder->srcpad);
1087 if (peer)
1088 {
1089 s = gst_structure_new_empty ("IS_SVP");
1090 if (s)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001091 {
bo.xiao857b8682024-09-12 16:40:32 +08001092 event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s);
1093 gst_pad_send_event (peer, event);
1094 GST_DEBUG_OBJECT(self, "Send SVP Event");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001095 }
bo.xiao857b8682024-09-12 16:40:32 +08001096 gst_object_unref (peer);
1097 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001098 }
1099
bo.xiao857b8682024-09-12 16:40:32 +08001100 if (self->v4l2capture->need_drop_event)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001101 {
bo.xiao857b8682024-09-12 16:40:32 +08001102 // drop V4L2_EVENT_SOURCE_CHANGE
1103 gst_v4l2_drop_event(self->v4l2capture);
1104 self->v4l2capture->need_drop_event = FALSE;
bo.xiao8e3054d2024-07-31 17:13:57 +08001105 }
1106
bo.xiao857b8682024-09-12 16:40:32 +08001107 if (!gst_aml_v4l2_object_acquire_format (self->v4l2capture, &info))
1108 goto not_negotiated;
1109
1110 /* Create caps from the acquired format, remove the format field */
1111 acquired_caps = gst_video_info_to_caps (&info);
1112 GST_DEBUG_OBJECT (self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps);
1113 st = gst_caps_get_structure (acquired_caps, 0);
1114 gst_structure_remove_fields (st, "format", "colorimetry", "chroma-site", NULL);
1115
1116 /* Probe currently available pixel formats */
1117 available_caps = gst_caps_copy (self->probed_srccaps);
1118 GST_DEBUG_OBJECT (self, "Available caps: %" GST_PTR_FORMAT, available_caps);
1119
1120 /* Replace coded size with visible size, we want to negotiate visible size
1121 * with downstream, not coded size. */
1122 gst_caps_map_in_place (available_caps, gst_aml_v4l2_video_remove_padding, self);
1123
1124 filter = gst_caps_intersect_full (available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST);
1125 caps = gst_caps_copy(filter);
1126 gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
1127 gst_caps_append(filter, caps);
1128
1129 GST_DEBUG_OBJECT (self, "Filtered caps: %" GST_PTR_FORMAT, filter);
1130 gst_caps_unref (acquired_caps);
1131 gst_caps_unref (available_caps);
1132 caps = gst_pad_peer_query_caps (decoder->srcpad, filter);
1133 gst_caps_unref (filter);
1134
1135 GST_DEBUG_OBJECT (self, "Possible decoded caps: %" GST_PTR_FORMAT, caps);
1136 if (gst_caps_is_empty (caps))
bo.xiao8e3054d2024-07-31 17:13:57 +08001137 {
bo.xiao857b8682024-09-12 16:40:32 +08001138 gst_caps_unref (caps);
1139 goto not_negotiated;
bo.xiao8e3054d2024-07-31 17:13:57 +08001140 }
1141
bo.xiao857b8682024-09-12 16:40:32 +08001142 /* Fixate pixel format */
1143 caps = gst_caps_fixate (caps);
bo.xiao8e3054d2024-07-31 17:13:57 +08001144
bo.xiao857b8682024-09-12 16:40:32 +08001145 GST_DEBUG_OBJECT (self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps);
zengliang.lidcd41462024-06-19 16:05:12 +08001146
bo.xiao857b8682024-09-12 16:40:32 +08001147 /* Try to set negotiated format, on success replace acquired format */
1148 if (gst_aml_v4l2_object_set_format (self->v4l2capture, caps, &error))
1149 gst_video_info_from_caps (&info, caps);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001150 else
bo.xiao857b8682024-09-12 16:40:32 +08001151 gst_aml_v4l2_clear_error (&error);
1152 gst_caps_unref (caps);
hanghang.luo68527bd2024-11-04 13:41:42 +08001153
bo.xiao857b8682024-09-12 16:40:32 +08001154 gst_aml_v4l2_video_dec_set_output_status(decoder,info);
1155 if (!gst_aml_video_decoder_negotiate (decoder))
xuesong.jiangae1548e2022-05-06 16:38:46 +08001156 {
bo.xiao857b8682024-09-12 16:40:32 +08001157 if (GST_PAD_IS_FLUSHING (decoder->srcpad))
1158 goto flushing;
1159 else
1160 goto not_negotiated;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001161 }
1162
bo.xiao857b8682024-09-12 16:40:32 +08001163 /* Ensure our internal pool is activated */
1164 if (!gst_buffer_pool_set_active (GST_BUFFER_POOL (self->v4l2capture->pool),
1165 TRUE))
1166 goto activate_failed;
1167
1168 //cal duration when got resolution event
1169 self->frame_duration = gst_aml_v4l2_video_dec_calc_duration(decoder);
1170
1171 g_mutex_lock(&self->res_chg_lock);
1172 GST_LOG_OBJECT(decoder, "signal resolution changed");
1173 self->is_res_chg = FALSE;
1174 g_cond_signal(&self->res_chg_cond);
1175 g_mutex_unlock(&self->res_chg_lock);
1176 }
1177
1178 GST_LOG_OBJECT (decoder, "Allocate output buffer");
1179 v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool);
1180
1181 self->output_flow = GST_FLOW_OK;
1182 do
1183 {
1184 /* We cannot use the base class allotate helper since it taking the internal
1185 * stream lock. we know that the acquire may need to poll until more frames
1186 * comes in and holding this lock would prevent that.
1187 */
1188 pool = gst_aml_video_decoder_get_buffer_pool (decoder);
1189
1190 /* Pool may be NULL if we started going to READY state */
1191 if (pool == NULL)
1192 {
1193 GST_WARNING_OBJECT(decoder, "gst_aml_video_decoder_get_buffer_pool goto beach");
1194 ret = GST_FLOW_FLUSHING;
1195 goto beach;
1196 }
1197
1198 ret = gst_buffer_pool_acquire_buffer (pool, &buffer, NULL);
1199
1200 g_object_unref (pool);
1201
1202 if (ret == GST_FLOW_OK && GST_BUFFER_FLAG_IS_SET(buffer,GST_AML_V4L2_BUFFER_FLAG_LAST_EMPTY)) {
1203 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER");
1204 self->v4l2capture->need_drop_event = TRUE;
1205 gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1206 if (self->is_res_chg) {
1207 //we must release last buffer
1208 gst_buffer_unref(buffer);
1209 //if resolution changed event received,we should set need_drop_event to false
1210 self->v4l2capture->need_drop_event = FALSE;
1211 gst_aml_v4l2_object_stop(self->v4l2capture);
fei.deng2a06e042023-10-10 03:09:45 +00001212 //unblock flush start event
1213 g_mutex_lock(&self->res_chg_lock);
1214 self->is_res_chg = FALSE;
1215 g_cond_signal(&self->res_chg_cond);
1216 g_mutex_unlock(&self->res_chg_lock);
bo.xiao857b8682024-09-12 16:40:32 +08001217 return;
1218 } else {
1219 goto beach;
1220 }
fei.deng2a06e042023-10-10 03:09:45 +00001221 }
bo.xiao857b8682024-09-12 16:40:32 +08001222
1223 if (ret == GST_AML_V4L2_FLOW_CC_DATA)
1224 {
1225 GST_DEBUG_OBJECT(decoder, "already got cc data, just continue.");
1226 continue;
1227 }
1228
1229 if (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT)
1230 {
1231 GST_DEBUG_OBJECT(decoder, "unknow event, just continue.");
1232 continue;
1233 }
1234
1235 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1236 {
1237 GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE");
1238
1239 g_mutex_lock (&self->res_chg_lock);
1240 self->is_res_chg = TRUE;
1241 g_mutex_unlock (&self->res_chg_lock);
1242 return;
1243 }
1244
1245 //decoding error happened
1246 if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)
1247 {
1248 GST_DEBUG("send error pts:%llu - %" GST_TIME_FORMAT, v4l2_pool->obj->error_frame_pts, GST_TIME_ARGS(v4l2_pool->obj->error_frame_pts));
1249 g_signal_emit (self, g_signals[SIGNAL_DECODED_ERROR_PTS], 0, v4l2_pool->obj->error_frame_pts, NULL);
1250 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, v4l2_pool->obj->error_frame_pts);
1251 continue;
1252 }
1253
1254 if (ret != GST_FLOW_OK) {
1255 GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret);
1256 goto beach;
1257 }
1258
1259 GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num);
1260 ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer);
1261
1262 GST_DEBUG_OBJECT(decoder, "decoded pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer)));
1263 g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer));
1264
1265 if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE)
1266 {
1267 gst_aml_v4l2_object_stop (self->v4l2capture);
1268 return;
1269 }
1270
1271 } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) ||
1272 (ret == GST_AML_V4L2_FLOW_CC_DATA) ||
1273 (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT) ||
1274 (ret == GST_AML_V4L2_FLOW_DECODING_ERROR));
1275
1276 if (ret != GST_FLOW_OK)
1277 goto beach;
1278
1279 if (!GST_BUFFER_PTS_IS_VALID (buffer)
1280 || (GST_BUFFER_TIMESTAMP(buffer) == 0 && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED))
1281 {
1282 GST_BUFFER_TIMESTAMP(buffer) = gst_aml_v4l2_video_dec_calc_output_buffer_pts(decoder);
1283 }
1284
1285 if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)
1286 {
1287 GST_BUFFER_DURATION(buffer) = self->frame_duration; // got at resolution event.
1288 GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED);
1289 }
1290
1291 frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer));
1292 if (frame)
1293 {
1294 self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer);
1295 frame->output_buffer = buffer;
1296 frame->pts = GST_BUFFER_TIMESTAMP(buffer);
1297 frame->duration = GST_BUFFER_DURATION(buffer);
1298
1299 buffer = NULL;
1300
1301 if (self->v4l2capture->enable_cc_data)
1302 {
1303 if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame))
1304 {
1305 GST_DEBUG("cc buffer and frame bind success");
1306 GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer),
1307 GST_AML_V4L2_CC_IMPORT_QUARK);
1308 #if 0
1309 //Debug code:dump cc data
1310 GstMapInfo gst_map;
1311 gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ);
1312 int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777);
1313 if (gst_map.size>0)
1314 write(fd,gst_map.data,gst_map.size);
1315 close(fd);
1316 gst_buffer_unmap(cc_buffer,&gst_map);
1317 #endif
1318 v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer);
1319 gst_buffer_unref(cc_buffer);
1320 }
1321 else
1322 {
1323 GST_WARNING("bufferlist is empty or no match frame in the bufferlist");
1324 }
1325 }
xuesong.jiang0223de52024-09-13 15:33:10 +08001326
1327 ret = gst_aml_v4l2_video_dec_clip_and_push_frame(decoder, frame);
bo.xiao857b8682024-09-12 16:40:32 +08001328
1329 if (ret != GST_FLOW_OK)
1330 goto beach;
1331 }
1332 else
1333 {
1334 GST_WARNING_OBJECT(decoder, "Unmatch buffer, should be push, need refine");
1335 //gst_pad_push (decoder->srcpad, buffer);
1336 gst_buffer_unref (buffer);
1337 }
1338
1339 return;
1340 /* ERRORS */
1341not_negotiated:
1342 {
1343 GST_ERROR_OBJECT (self, "not negotiated");
1344 ret = GST_FLOW_NOT_NEGOTIATED;
1345 goto beach;
1346 }
1347activate_failed:
1348 {
1349 GST_ERROR_OBJECT (self, "Buffer pool activation failed");
1350 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
1351 (_("Failed to allocate required memory.")),
1352 ("Buffer pool activation failed"));
1353 ret = GST_FLOW_ERROR;
1354 goto beach;
1355 }
1356flushing:
1357 {
1358 ret = GST_FLOW_FLUSHING;
1359 goto beach;
1360 }
1361beach:
1362 GST_DEBUG_OBJECT (decoder, "Leaving output thread: %s",
1363 gst_flow_get_name (ret));
1364 if (self->is_res_chg) {
1365 //unblock flush start event
1366 g_mutex_lock(&self->res_chg_lock);
1367 self->is_res_chg = FALSE;
1368 g_cond_signal(&self->res_chg_cond);
1369 g_mutex_unlock(&self->res_chg_lock);
1370 }
1371 gst_buffer_replace (&buffer, NULL);
1372 self->output_flow = ret;
1373 gst_aml_v4l2_object_flush_start (self->v4l2output);
1374 gst_pad_pause_task (decoder->srcpad);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001375}
1376
1377static GstFlowReturn
bo.xiao857b8682024-09-12 16:40:32 +08001378gst_aml_v4l2_video_dec_handle_frame (GstAmlVideoDecoder * decoder,
1379 GstAmlVideoCodecFrame * frame)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001380{
bo.xiao857b8682024-09-12 16:40:32 +08001381 GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT;
1382 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1383 GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool);
1384 GstFlowReturn ret = GST_FLOW_OK;
1385 gboolean processed = FALSE;
1386 GstBuffer *tmp;
1387 GstTaskState task_state;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001388
bo.xiao857b8682024-09-12 16:40:32 +08001389 GST_DEBUG_OBJECT (self, "Handling frame %d", frame->system_frame_number);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001390
bo.xiao857b8682024-09-12 16:40:32 +08001391 if (G_UNLIKELY (!g_atomic_int_get (&self->active)))
1392 goto flushing;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001393
bo.xiao857b8682024-09-12 16:40:32 +08001394 if (G_UNLIKELY(!GST_CLOCK_TIME_IS_VALID(self->frame_duration)))
1395 self->frame_duration = frame->duration;
le.han44125c32024-09-03 07:55:57 +00001396
bo.xiao857b8682024-09-12 16:40:32 +08001397 if (G_UNLIKELY (!GST_AML_V4L2_IS_ACTIVE (self->v4l2output)))
1398 {
1399 if (!self->input_state)
1400 goto not_negotiated;
1401 if (!gst_aml_v4l2_object_set_format (self->v4l2output, self->input_state->caps,
1402 &error))
1403 goto not_negotiated;
1404 }
1405
1406 if (G_UNLIKELY (!GST_AML_V4L2_IS_ACTIVE (self->v4l2capture)))
1407 {
1408 GstBuffer *codec_data;
1409 GstCapsFeatures *features = NULL;
1410
1411 features = gst_caps_get_features(self->input_state->caps, 0);
1412 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001413 {
bo.xiao857b8682024-09-12 16:40:32 +08001414 GST_DEBUG_OBJECT(self, "Is SVP");
xuesong.jiang0223de52024-09-13 15:33:10 +08001415 decoder->svp = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001416 }
1417
bo.xiao857b8682024-09-12 16:40:32 +08001418 GST_DEBUG_OBJECT (self, "Sending header");
1419
1420 codec_data = self->input_state->codec_data;
1421
1422 /* We are running in byte-stream mode, so we don't know the headers, but
1423 * we need to send something, otherwise the decoder will refuse to
1424 * initialize.
1425 */
1426 if (codec_data)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001427 {
bo.xiao857b8682024-09-12 16:40:32 +08001428 gst_buffer_ref (codec_data);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001429 }
1430 else
1431 {
bo.xiao857b8682024-09-12 16:40:32 +08001432 codec_data = gst_buffer_ref (frame->input_buffer);
1433 processed = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001434 }
1435
bo.xiao857b8682024-09-12 16:40:32 +08001436 /* Ensure input internal pool is active */
1437 if (!gst_buffer_pool_is_active (pool))
1438 {
1439 GstStructure *config = gst_buffer_pool_get_config (pool);
1440 // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS);
1441 // guint max = VIDEO_MAX_FRAME;
1442 // gst_buffer_pool_config_set_params (config, self->input_state->caps,
1443 // self->v4l2output->info.size, min, max);
1444 gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers);
1445
1446 /* There is no reason to refuse this config */
1447 if (!gst_buffer_pool_set_config (pool, config))
1448 goto activate_failed;
1449 GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config);
1450
1451 if (!gst_buffer_pool_set_active (pool, TRUE))
1452 goto activate_failed;
1453 }
1454
1455 GST_AML_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1456 ret =
1457 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data);
1458 self->codec_data_inject = TRUE;
1459 GST_AML_VIDEO_DECODER_STREAM_LOCK (decoder);
1460
1461 gst_buffer_unref (codec_data);
1462
1463 /* For decoders G_FMT returns coded size, G_SELECTION returns visible size
1464 * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both
1465 * and returns the visible size as with/height and the coded size as
1466 * padding. */
1467 }
1468
1469 task_state = gst_pad_get_task_state (GST_AML_VIDEO_DECODER_SRC_PAD (self));
1470 if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED)
1471 {
1472 /* It's possible that the processing thread stopped due to an error */
1473 if (self->output_flow != GST_FLOW_OK &&
1474 self->output_flow != GST_FLOW_FLUSHING)
1475 {
1476 GST_DEBUG_OBJECT (self, "Processing loop stopped with error, leaving");
1477 ret = self->output_flow;
1478 goto drop;
1479 }
1480
1481 GST_DEBUG_OBJECT (self, "Starting decoding thread");
1482
1483 /* Start the processing task, when it quits, the task will disable input
1484 * processing to unlock input if draining, or prevent potential block */
1485 self->output_flow = GST_FLOW_FLUSHING;
1486 /*reset poll and need_drop_event before start decoding loop thread*/
1487 self->v4l2capture->need_drop_event = FALSE;
1488 gst_poll_set_flushing(self->v4l2capture->poll, FALSE);
1489 if (!gst_pad_start_task(decoder->srcpad,
1490 (GstTaskFunction) gst_aml_v4l2_video_dec_loop, self, NULL))
1491 goto start_task_failed;
1492 }
1493
1494 if (!processed)
1495 {
1496 GST_AML_VIDEO_DECODER_STREAM_UNLOCK (decoder);
1497 if (!self->codec_data_inject && self->input_state->codec_data)
1498 {
le.han935e7aa2024-09-13 01:50:36 +00001499 if (!self->v4l2output->secure_es)
1500 {
1501 GstBuffer *buffer_input = gst_buffer_new();
1502 gst_buffer_copy_into (buffer_input, self->input_state->codec_data,
1503 GST_BUFFER_COPY_MEMORY, 0, -1);
1504 gst_buffer_copy_into (buffer_input, frame->input_buffer,
1505 GST_BUFFER_COPY_MEMORY, 0, -1);
1506 gst_buffer_unref(frame->input_buffer);
1507 frame->input_buffer = buffer_input;
1508 GST_BUFFER_PTS(frame->input_buffer) = frame->pts;
1509 self->codec_data_inject = TRUE;
1510 }
1511 else
1512 {
1513 ret = gst_aml_v4l2_buffer_pool_process
1514 (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data);
1515 self->codec_data_inject = TRUE;
1516 if (ret != GST_FLOW_OK)
1517 goto send_codec_failed;
1518 }
bo.xiao857b8682024-09-12 16:40:32 +08001519 }
1520 ret =
1521 gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer);
1522 GST_AML_VIDEO_DECODER_STREAM_LOCK (decoder);
1523
1524 if (ret == GST_FLOW_FLUSHING)
1525 {
1526 if (gst_pad_get_task_state (GST_AML_VIDEO_DECODER_SRC_PAD (self)) !=
1527 GST_TASK_STARTED)
1528 ret = self->output_flow;
1529 goto drop;
1530 }
1531 else if (ret != GST_FLOW_OK)
1532 {
1533 goto process_failed;
1534 }
1535 }
1536
1537 /* No need to keep input around */
1538 tmp = frame->input_buffer;
1539 frame->input_buffer = gst_buffer_new ();
1540 gst_buffer_copy_into (frame->input_buffer, tmp,
1541 GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS |
1542 GST_BUFFER_COPY_META, 0, 0);
1543 gst_buffer_unref (tmp);
1544
1545 gst_aml_video_codec_frame_unref (frame);
1546 return ret;
1547
1548 /* ERRORS */
1549send_codec_failed:
1550 GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret);
1551 goto drop;
1552not_negotiated:
1553 {
1554 GST_ERROR_OBJECT (self, "not negotiated");
1555 ret = GST_FLOW_NOT_NEGOTIATED;
1556 gst_aml_v4l2_error (self, &error);
1557 goto drop;
1558 }
1559activate_failed:
1560 {
1561 GST_ELEMENT_ERROR (self, RESOURCE, SETTINGS,
1562 (_("Failed to allocate required memory.")),
1563 ("Buffer pool activation failed"));
1564 ret = GST_FLOW_ERROR;
1565 goto drop;
1566 }
1567flushing:
1568 {
1569 ret = GST_FLOW_FLUSHING;
1570 goto drop;
1571 }
1572
1573start_task_failed:
1574 {
1575 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1576 (_("Failed to start decoding thread.")), (NULL));
1577 ret = GST_FLOW_ERROR;
1578 goto drop;
1579 }
1580process_failed:
1581 {
1582 GST_ELEMENT_ERROR (self, RESOURCE, FAILED,
1583 (_("Failed to process frame.")),
1584 ("Maybe be due to not enough memory or failing driver"));
1585 ret = GST_FLOW_ERROR;
1586 goto drop;
1587 }
1588drop:
1589 {
1590 gst_aml_video_decoder_drop_frame (decoder, frame);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001591 return ret;
bo.xiao857b8682024-09-12 16:40:32 +08001592 }
1593}
1594
1595static gboolean
1596gst_aml_v4l2_video_dec_decide_allocation (GstAmlVideoDecoder * decoder,
1597 GstQuery * query)
1598{
1599 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (decoder);
1600 GstClockTime latency;
1601 gboolean ret = FALSE;
1602
1603 if (gst_aml_v4l2_object_decide_allocation (self->v4l2capture, query))
1604 ret = GST_AML_VIDEO_DECODER_CLASS (parent_class)->decide_allocation (decoder, query);
1605
1606 if (GST_CLOCK_TIME_IS_VALID (self->v4l2capture->duration))
1607 {
1608 latency = self->v4l2capture->min_buffers * self->v4l2capture->duration;
1609 GST_DEBUG_OBJECT (self, "Setting latency: %" GST_TIME_FORMAT " (%"
1610 G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS (latency),
1611 self->v4l2capture->min_buffers, self->v4l2capture->duration);
1612 gst_aml_video_decoder_set_latency (decoder, latency, latency);
1613 }
1614 else
1615 {
1616 GST_WARNING_OBJECT (self, "Duration invalid, not setting latency");
1617 }
1618
1619 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001620}
1621
1622static gboolean
le.han02c38f02024-08-16 02:35:36 +00001623gst_aml_v4l2_video_dec_src_query(GstAmlVideoDecoder *decoder, GstQuery *query)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001624{
bo.xiao857b8682024-09-12 16:40:32 +08001625 gboolean ret = TRUE;
1626 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001627
bo.xiao857b8682024-09-12 16:40:32 +08001628 switch (GST_QUERY_TYPE (query))
1629 {
xuesong.jiangae1548e2022-05-06 16:38:46 +08001630 case GST_QUERY_CAPS:
1631 {
bo.xiao857b8682024-09-12 16:40:32 +08001632 GstCaps *filter, *result = NULL;
1633 GstPad *pad = GST_AML_VIDEO_DECODER_SRC_PAD (decoder);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001634
bo.xiao857b8682024-09-12 16:40:32 +08001635 gst_query_parse_caps (query, &filter);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001636
bo.xiao857b8682024-09-12 16:40:32 +08001637 if (self->probed_srccaps)
1638 result = gst_caps_ref (self->probed_srccaps);
1639 else
1640 result = gst_pad_get_pad_template_caps (pad);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001641
bo.xiao857b8682024-09-12 16:40:32 +08001642 if (filter)
1643 {
1644 GstCaps *tmp = result;
1645 result =
1646 gst_caps_intersect_full (filter, tmp, GST_CAPS_INTERSECT_FIRST);
1647 gst_caps_unref (tmp);
1648 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001649
bo.xiao857b8682024-09-12 16:40:32 +08001650 GST_DEBUG_OBJECT (self, "Returning src caps %" GST_PTR_FORMAT, result);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001651
bo.xiao857b8682024-09-12 16:40:32 +08001652 gst_query_set_caps_result (query, result);
1653 gst_caps_unref (result);
1654 break;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001655 }
1656
1657 default:
bo.xiao857b8682024-09-12 16:40:32 +08001658 ret = GST_AML_VIDEO_DECODER_CLASS (parent_class)->src_query (decoder, query);
1659 break;
1660 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001661
bo.xiao857b8682024-09-12 16:40:32 +08001662 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001663}
1664
1665static GstCaps *
le.han02c38f02024-08-16 02:35:36 +00001666gst_aml_v4l2_video_dec_sink_getcaps(GstAmlVideoDecoder *decoder, GstCaps *filter)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001667{
bo.xiao857b8682024-09-12 16:40:32 +08001668 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder);
1669 GstCaps *result;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001670
bo.xiao857b8682024-09-12 16:40:32 +08001671 result = gst_aml_video_decoder_proxy_getcaps (decoder, self->probed_sinkcaps,
1672 filter);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001673
bo.xiao857b8682024-09-12 16:40:32 +08001674 GST_DEBUG_OBJECT (self, "Returning sink caps %" GST_PTR_FORMAT, result);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001675
bo.xiao857b8682024-09-12 16:40:32 +08001676 return result;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001677}
1678
1679static gboolean
bo.xiao857b8682024-09-12 16:40:32 +08001680gst_aml_v4l2_video_dec_sink_event (GstAmlVideoDecoder *decoder, GstEvent *event)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001681{
bo.xiao857b8682024-09-12 16:40:32 +08001682 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (decoder);
1683 gboolean ret;
1684 GstEventType type = GST_EVENT_TYPE (event);
1685 GST_DEBUG_OBJECT (self, "received event %p %" GST_PTR_FORMAT, event, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001686
bo.xiao857b8682024-09-12 16:40:32 +08001687 switch (type)
1688 {
xuesong.jiang406ee302023-06-28 03:45:22 +00001689 case GST_EVENT_STREAM_START:
1690 {
bo.xiao857b8682024-09-12 16:40:32 +08001691 GstStructure *s;
1692 GstEvent *event;
1693 s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL);
1694 event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s);
1695 GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event);
1696 gst_pad_push_event (decoder->sinkpad, event);
1697 GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event);
1698 break;
xuesong.jiang406ee302023-06-28 03:45:22 +00001699 }
zengliang.li51f54b62023-10-10 12:14:49 +00001700 case GST_EVENT_CAPS:
1701 {
bo.xiao857b8682024-09-12 16:40:32 +08001702 GstCaps *caps;
1703 GstStructure *structure;
1704 gint num, denom;
zengliang.li51f54b62023-10-10 12:14:49 +00001705
bo.xiao857b8682024-09-12 16:40:32 +08001706 gst_event_parse_caps (event, &caps);
bo.xiao34e36202024-07-17 16:04:01 +08001707
bo.xiao857b8682024-09-12 16:40:32 +08001708 structure= gst_caps_get_structure(caps, 0);
1709 if ( gst_structure_has_field(structure, "parsed") )
1710 {
1711 gboolean parsed = TRUE;
1712 if ( gst_structure_get_boolean( structure, "parsed", &parsed ) )
zengliang.li51f54b62023-10-10 12:14:49 +00001713 {
bo.xiao857b8682024-09-12 16:40:32 +08001714 self->v4l2output->stream_mode = !parsed;
1715 GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode);
1716 }
1717 }
1718
1719 if ( gst_structure_has_field(structure, "secure") )
1720 {
1721 gboolean is_secure = FALSE;
1722 if ( gst_structure_get_boolean( structure, "secure", &is_secure ) )
1723 {
1724 self->v4l2output->secure_es = is_secure;
1725 GST_DEBUG("is secure es:%d", self->v4l2output->secure_es);
1726 }
1727 }
1728 else
1729 {
1730 GstCapsFeatures *const features = gst_caps_get_features(caps, 0);
1731 if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF))
1732 {
1733 self->v4l2output->secure_es = TRUE;
1734 GST_DEBUG("If there is no secure field in caps, consider dma es is secure");
1735 }
1736 }
1737
1738 if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) )
1739 {
1740 if ( denom == 0 ) denom= 1;
1741
1742 if (self->v4l2capture->fps)
1743 {
1744 g_value_unset(self->v4l2capture->fps);
1745 g_free(self->v4l2capture->fps);
zengliang.li51f54b62023-10-10 12:14:49 +00001746 }
sheng.liu641aa422023-12-26 07:05:59 +00001747
bo.xiao857b8682024-09-12 16:40:32 +08001748 self->v4l2capture->fps = g_new0(GValue, 1);
1749 g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION);
1750 gst_value_set_fraction(self->v4l2capture->fps, num, denom);
1751
1752 GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom);
1753 }
1754
1755 if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) &&
1756 ( !self->v4l2capture->have_set_par ) )
1757 {
1758 if ( (num <= 0) || (denom <= 0) )
xuesong.jiang26749f32024-07-24 09:40:47 +08001759 {
bo.xiao857b8682024-09-12 16:40:32 +08001760 num= denom= 1;
xuesong.jiang578add22024-07-25 15:11:30 +08001761 }
xuesong.jiang26749f32024-07-24 09:40:47 +08001762
bo.xiao857b8682024-09-12 16:40:32 +08001763 if ( self->v4l2capture->par )
sheng.liudb26f7d2024-01-22 11:24:23 +00001764 {
bo.xiao857b8682024-09-12 16:40:32 +08001765 g_value_unset(self->v4l2capture->par);
1766 g_free(self->v4l2capture->par);
sheng.liudb26f7d2024-01-22 11:24:23 +00001767 }
1768
bo.xiao857b8682024-09-12 16:40:32 +08001769 self->v4l2capture->par = g_new0(GValue, 1);
1770 g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION);
1771 gst_value_set_fraction(self->v4l2capture->par, num, denom);
1772 GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom);
1773 }
1774 break;
zengliang.li51f54b62023-10-10 12:14:49 +00001775 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001776 case GST_EVENT_FLUSH_START:
bo.xiao857b8682024-09-12 16:40:32 +08001777 GST_DEBUG_OBJECT (self, "flush start");
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001778
bo.xiao857b8682024-09-12 16:40:32 +08001779 g_mutex_lock (&self->res_chg_lock);
1780 while (self->is_res_chg)
1781 {
1782 GST_LOG_OBJECT(decoder, "wait resolution change finish");
1783 g_cond_wait(&self->res_chg_cond, &self->res_chg_lock);
1784 }
1785 g_mutex_unlock (&self->res_chg_lock);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001786
bo.xiao857b8682024-09-12 16:40:32 +08001787 self->last_out_pts = GST_CLOCK_TIME_NONE;
1788 gst_aml_v4l2_object_flush_start (self->v4l2output);
1789 gst_aml_v4l2_object_flush_start (self->v4l2capture);
1790 break;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001791 default:
bo.xiao857b8682024-09-12 16:40:32 +08001792 break;
1793 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001794
bo.xiao857b8682024-09-12 16:40:32 +08001795 ret = GST_AML_VIDEO_DECODER_CLASS (parent_class)->sink_event (decoder, event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001796
bo.xiao857b8682024-09-12 16:40:32 +08001797 switch (type)
1798 {
xuesong.jiangae1548e2022-05-06 16:38:46 +08001799 case GST_EVENT_FLUSH_START:
bo.xiao857b8682024-09-12 16:40:32 +08001800 /* The processing thread should stop now, wait for it */
1801 gst_pad_stop_task (decoder->srcpad);
1802 self->codec_data_inject = FALSE;
1803 GST_DEBUG_OBJECT (self, "flush start done");
1804 break;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001805 default:
bo.xiao857b8682024-09-12 16:40:32 +08001806 break;
1807 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001808
bo.xiao857b8682024-09-12 16:40:32 +08001809 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001810}
1811
1812static GstStateChangeReturn
bo.xiao857b8682024-09-12 16:40:32 +08001813gst_aml_v4l2_video_dec_change_state (GstElement * element,
1814 GstStateChange transition)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001815{
bo.xiao857b8682024-09-12 16:40:32 +08001816 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element);
1817 GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(element);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001818
bo.xiao857b8682024-09-12 16:40:32 +08001819 GST_DEBUG_OBJECT(element, "change state from %s to %s",
1820 gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)),
1821 gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition)));
bo.xiao8e3054d2024-07-31 17:13:57 +08001822
bo.xiao857b8682024-09-12 16:40:32 +08001823 if (transition == GST_STATE_CHANGE_PAUSED_TO_READY)
1824 {
1825 g_atomic_int_set (&self->active, FALSE);
1826 gst_aml_v4l2_object_flush_start (self->v4l2output);
1827 gst_aml_v4l2_object_flush_start (self->v4l2capture);
1828 gst_pad_stop_task (decoder->srcpad);
1829 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08001830
bo.xiao857b8682024-09-12 16:40:32 +08001831 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001832}
1833
1834static void
bo.xiao857b8682024-09-12 16:40:32 +08001835gst_aml_v4l2_video_dec_dispose (GObject * object)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001836{
bo.xiao857b8682024-09-12 16:40:32 +08001837 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (object);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001838
bo.xiao857b8682024-09-12 16:40:32 +08001839 gst_caps_replace (&self->probed_sinkcaps, NULL);
1840 gst_caps_replace (&self->probed_srccaps, NULL);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001841
bo.xiao857b8682024-09-12 16:40:32 +08001842 G_OBJECT_CLASS (parent_class)->dispose (object);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001843}
1844
1845static void
bo.xiao857b8682024-09-12 16:40:32 +08001846gst_aml_v4l2_video_dec_finalize (GObject * object)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001847{
bo.xiao857b8682024-09-12 16:40:32 +08001848 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (object);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001849
bo.xiao857b8682024-09-12 16:40:32 +08001850 gst_aml_v4l2_object_destroy (self->v4l2capture);
1851 gst_aml_v4l2_object_destroy (self->v4l2output);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001852
bo.xiao857b8682024-09-12 16:40:32 +08001853 g_mutex_clear(&self->res_chg_lock);
1854 g_cond_clear(&self->res_chg_cond);
xuesong.jiangc5dac0f2023-02-01 14:42:24 +08001855
xuesong.jiang61ea8012022-05-12 15:38:17 +08001856#if GST_IMPORT_LGE_PROP
bo.xiao857b8682024-09-12 16:40:32 +08001857 if (self->lge_ctxt)
1858 {
1859 if (self->lge_ctxt->app_type)
1860 g_free(self->lge_ctxt->app_type);
1861 if (self->lge_ctxt->res_info.coretype)
1862 g_free(self->lge_ctxt->res_info.coretype);
1863 free(self->lge_ctxt);
1864 }
xuesong.jiang61ea8012022-05-12 15:38:17 +08001865
1866#endif
1867
bo.xiao857b8682024-09-12 16:40:32 +08001868 G_OBJECT_CLASS (parent_class)->finalize (object);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001869}
1870
1871static void
bo.xiao857b8682024-09-12 16:40:32 +08001872gst_aml_v4l2_video_dec_init (GstAmlV4l2VideoDec * self)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001873{
bo.xiao857b8682024-09-12 16:40:32 +08001874 /* V4L2 object are created in subinstance_init */
1875 self->last_out_pts = GST_CLOCK_TIME_NONE;
1876 self->frame_duration = GST_CLOCK_TIME_NONE;
1877 self->is_secure_path = FALSE;
1878 self->is_res_chg = FALSE;
1879 self->codec_data_inject = FALSE;
1880 g_mutex_init(&self->res_chg_lock);
1881 g_cond_init(&self->res_chg_cond);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001882#if GST_IMPORT_LGE_PROP
bo.xiao857b8682024-09-12 16:40:32 +08001883 self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt));
1884 memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt));
xuesong.jiang61ea8012022-05-12 15:38:17 +08001885#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001886}
1887
1888static void
bo.xiao857b8682024-09-12 16:40:32 +08001889gst_aml_v4l2_video_dec_subinstance_init (GTypeInstance * instance, gpointer g_class)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001890{
bo.xiao857b8682024-09-12 16:40:32 +08001891 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS (g_class);
1892 GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC (instance);
1893 GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER (instance);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001894
bo.xiao857b8682024-09-12 16:40:32 +08001895 gst_aml_video_decoder_set_packetized (decoder, TRUE);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001896
bo.xiao857b8682024-09-12 16:40:32 +08001897 self->v4l2output = gst_aml_v4l2_object_new (GST_ELEMENT (self),
1898 GST_OBJECT (GST_AML_VIDEO_DECODER_SINK_PAD (self)),
1899 V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device,
1900 gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL);
1901 self->v4l2output->no_initial_format = TRUE;
1902 self->v4l2output->keep_aspect = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001903
bo.xiao857b8682024-09-12 16:40:32 +08001904 self->v4l2capture = gst_aml_v4l2_object_new (GST_ELEMENT (self),
1905 GST_OBJECT (GST_AML_VIDEO_DECODER_SRC_PAD (self)),
1906 V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device,
1907 gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL);
1908 self->v4l2capture->need_wait_event = TRUE;
1909 self->v4l2capture->need_drop_event = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001910}
1911
1912static void
bo.xiao857b8682024-09-12 16:40:32 +08001913gst_aml_v4l2_video_dec_class_init (GstAmlV4l2VideoDecClass * klass)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001914{
bo.xiao857b8682024-09-12 16:40:32 +08001915 GstElementClass *element_class;
1916 GObjectClass *gobject_class;
1917 GstAmlVideoDecoderClass *video_decoder_class;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001918
bo.xiao857b8682024-09-12 16:40:32 +08001919 parent_class = g_type_class_peek_parent (klass);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001920
bo.xiao857b8682024-09-12 16:40:32 +08001921 element_class = (GstElementClass *) klass;
1922 gobject_class = (GObjectClass *) klass;
1923 video_decoder_class = (GstAmlVideoDecoderClass *) klass;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001924
bo.xiao857b8682024-09-12 16:40:32 +08001925 GST_DEBUG_CATEGORY_INIT (gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0,
1926 "AML V4L2 Video Decoder");
xuesong.jiangae1548e2022-05-06 16:38:46 +08001927
bo.xiao857b8682024-09-12 16:40:32 +08001928 gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose);
1929 gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize);
1930 gobject_class->set_property =
1931 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property);
1932 gobject_class->get_property =
1933 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001934
bo.xiao857b8682024-09-12 16:40:32 +08001935 video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open);
1936 video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close);
1937 video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start);
1938 video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop);
1939 video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish);
1940 video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush);
1941 video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain);
1942 video_decoder_class->set_format =
1943 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format);
1944 video_decoder_class->negotiate =
1945 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate);
1946 video_decoder_class->decide_allocation =
1947 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation);
1948 /* FIXME propose_allocation or not ? */
1949 video_decoder_class->handle_frame =
1950 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame);
1951 video_decoder_class->getcaps =
1952 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps);
1953 video_decoder_class->src_query =
1954 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query);
1955 video_decoder_class->sink_event =
1956 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001957
bo.xiao857b8682024-09-12 16:40:32 +08001958 element_class->change_state =
1959 GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state);
xuesong.jiangae1548e2022-05-06 16:38:46 +08001960
xuesong.jiang406ee302023-06-28 03:45:22 +00001961 g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts",
bo.xiao857b8682024-09-12 16:40:32 +08001962 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1963 G_SIGNAL_RUN_LAST,
1964 0, /* class offset */
1965 NULL, /* accumulator */
1966 NULL, /* accu data */
1967 g_cclosure_marshal_generic,
1968 G_TYPE_NONE,
1969 1,
1970 G_TYPE_UINT64);
1971 g_signals[SIGNAL_DECODED_ERROR_PTS] = g_signal_new ("decoded-error-pts",
1972 G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)),
1973 G_SIGNAL_RUN_LAST,
1974 0, /* class offset */
1975 NULL, /* accumulator */
1976 NULL, /* accu data */
1977 g_cclosure_marshal_generic,
1978 G_TYPE_NONE,
1979 1,
1980 G_TYPE_UINT64);
xuesong.jiang406ee302023-06-28 03:45:22 +00001981
bo.xiao857b8682024-09-12 16:40:32 +08001982 gst_aml_v4l2_object_install_m2m_properties_helper (gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001983#if GST_IMPORT_LGE_PROP
bo.xiao857b8682024-09-12 16:40:32 +08001984 gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class);
xuesong.jiang61ea8012022-05-12 15:38:17 +08001985#endif
xuesong.jiangae1548e2022-05-06 16:38:46 +08001986}
1987
1988static void
bo.xiao857b8682024-09-12 16:40:32 +08001989gst_aml_v4l2_video_dec_subclass_init (gpointer g_class, gpointer data)
xuesong.jiangae1548e2022-05-06 16:38:46 +08001990{
bo.xiao857b8682024-09-12 16:40:32 +08001991 GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS (g_class);
1992 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
1993 GstAmlV4l2VideoDecCData *cdata = data;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001994
bo.xiao857b8682024-09-12 16:40:32 +08001995 klass->default_device = cdata->device;
xuesong.jiangae1548e2022-05-06 16:38:46 +08001996
bo.xiao857b8682024-09-12 16:40:32 +08001997 /* Note: gst_pad_template_new() take the floating ref from the caps */
1998 gst_element_class_add_pad_template (element_class,
1999 gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS,
2000 cdata->sink_caps));
2001 gst_element_class_add_pad_template (element_class,
2002 gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS,
2003 cdata->src_caps));
xuesong.jiangae1548e2022-05-06 16:38:46 +08002004
bo.xiao857b8682024-09-12 16:40:32 +08002005 gst_element_class_set_metadata (element_class, cdata->longname,
2006 "Codec/Decoder/Video/Hardware", cdata->description,
2007 "Xuesong Jiang <Xuesong.Jiang@amlogic.com>");
xuesong.jiangae1548e2022-05-06 16:38:46 +08002008
bo.xiao857b8682024-09-12 16:40:32 +08002009 gst_caps_unref (cdata->sink_caps);
2010 gst_caps_unref (cdata->src_caps);
2011 g_free (cdata);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002012}
2013
2014/* Probing functions */
2015gboolean
bo.xiao857b8682024-09-12 16:40:32 +08002016gst_aml_v4l2_is_video_dec (GstCaps * sink_caps, GstCaps * src_caps)
xuesong.jiangae1548e2022-05-06 16:38:46 +08002017{
bo.xiao857b8682024-09-12 16:40:32 +08002018 gboolean ret = FALSE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002019
bo.xiao857b8682024-09-12 16:40:32 +08002020 if (gst_caps_is_subset (sink_caps, gst_aml_v4l2_object_get_codec_caps ())
2021 && gst_caps_is_subset (src_caps, gst_aml_v4l2_object_get_raw_caps ()))
2022 ret = TRUE;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002023
bo.xiao857b8682024-09-12 16:40:32 +08002024 return ret;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002025}
2026
2027static gchar *
bo.xiao857b8682024-09-12 16:40:32 +08002028gst_aml_v4l2_video_dec_set_metadata (GstStructure * s, GstAmlV4l2VideoDecCData * cdata,
2029 const gchar * basename)
xuesong.jiangae1548e2022-05-06 16:38:46 +08002030{
bo.xiao857b8682024-09-12 16:40:32 +08002031 gchar *codec_name = NULL;
2032 gchar *type_name = NULL;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002033
bo.xiao857b8682024-09-12 16:40:32 +08002034#define SET_META(codec) \
2035G_STMT_START { \
2036 cdata->longname = "AML V4L2 " codec " Decoder"; \
2037 cdata->description = "Decodes " codec " streams via V4L2 API"; \
2038 codec_name = g_ascii_strdown (codec, -1); \
2039} G_STMT_END
xuesong.jiangae1548e2022-05-06 16:38:46 +08002040
bo.xiao857b8682024-09-12 16:40:32 +08002041 if (gst_structure_has_name(s, "video/mjpeg"))
2042 {
2043 SET_META("MJPEG");
2044 }
2045 else if (gst_structure_has_name (s, "video/mpeg"))
2046 {
2047 //include mpeg1, mpeg2, mpeg4
2048 SET_META("MPEG4");
2049 }
2050 else if (gst_structure_has_name (s, "video/x-h263"))
2051 {
2052 SET_META ("H263");
2053 }
2054 else if (gst_structure_has_name (s, "video/x-fwht"))
2055 {
2056 SET_META ("FWHT");
2057 }
2058 else if (gst_structure_has_name (s, "video/x-h264"))
2059 {
2060 SET_META ("H264");
2061 }
2062 else if (gst_structure_has_name (s, "video/x-h265"))
2063 {
2064 SET_META ("H265");
2065 }
2066 else if (gst_structure_has_name (s, "video/x-wmv"))
2067 {
2068 SET_META ("VC1");
2069 }
2070 else if (gst_structure_has_name (s, "video/x-vp8"))
2071 {
2072 SET_META ("VP8");
2073 }
2074 else if (gst_structure_has_name (s, "video/x-vp9"))
2075 {
2076 SET_META ("VP9");
2077 }
2078 else if (gst_structure_has_name(s, "video/x-av1"))
2079 {
2080 SET_META("AV1");
2081 }
2082 else if (gst_structure_has_name(s, "video/x-avs"))
2083 {
2084 SET_META("AVS");
2085 }
2086 else if (gst_structure_has_name(s, "video/x-avs2"))
2087 {
2088 SET_META("AVS2");
2089 }
2090 else if (gst_structure_has_name(s, "video/x-avs3"))
2091 {
2092 SET_META("AVS3");
2093 }
2094 else if (gst_structure_has_name (s, "video/x-bayer"))
2095 {
2096 SET_META ("BAYER");
2097 }
2098 else if (gst_structure_has_name (s, "video/x-sonix"))
2099 {
2100 SET_META ("SONIX");
2101 }
2102 else if (gst_structure_has_name (s, "video/x-pwc1"))
2103 {
2104 SET_META ("PWC1");
2105 }
2106 else if (gst_structure_has_name (s, "video/x-pwc2"))
2107 {
2108 SET_META ("PWC2");
2109 }
2110 else
2111 {
2112 /* This code should be kept on sync with the exposed CODEC type of format
2113 * from gstamlv4l2object.c. This warning will only occur in case we forget
2114 * to also add a format here. */
2115 gchar *s_str = gst_structure_to_string (s);
2116 g_warning ("Missing fixed name mapping for caps '%s', this is a GStreamer "
2117 "bug, please report at https://bugs.gnome.org", s_str);
2118 g_free (s_str);
2119 }
2120
2121 if (codec_name)
2122 {
2123 type_name = g_strdup_printf ("amlv4l2%sdec", codec_name);
2124 if (g_type_from_name (type_name) != 0)
xuesong.jiangae1548e2022-05-06 16:38:46 +08002125 {
bo.xiao857b8682024-09-12 16:40:32 +08002126 g_free (type_name);
2127 type_name = g_strdup_printf ("amlv4l2%s%sdec", basename, codec_name);
xuesong.jiangae1548e2022-05-06 16:38:46 +08002128 }
2129
bo.xiao857b8682024-09-12 16:40:32 +08002130 g_free (codec_name);
2131 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002132
bo.xiao857b8682024-09-12 16:40:32 +08002133 return type_name;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002134#undef SET_META
2135}
2136
bo.xiao857b8682024-09-12 16:40:32 +08002137void
2138gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename,
xuesong.jiangae1548e2022-05-06 16:38:46 +08002139 const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps)
2140{
bo.xiao857b8682024-09-12 16:40:32 +08002141 gint i;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002142
bo.xiao857b8682024-09-12 16:40:32 +08002143 for (i = 0; i < gst_caps_get_size (sink_caps); i++)
2144 {
2145 GstAmlV4l2VideoDecCData *cdata;
2146 GstStructure *s;
2147 GTypeQuery type_query;
2148 GTypeInfo type_info = { 0, };
2149 GType type, subtype;
2150 gchar *type_name;
2151
2152 s = gst_caps_get_structure (sink_caps, i);
2153
2154 cdata = g_new0 (GstAmlV4l2VideoDecCData, 1);
2155 cdata->device = g_strdup (device_path);
2156 cdata->sink_caps = gst_caps_new_empty ();
2157 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
2158 gst_caps_append_structure (cdata->sink_caps, gst_structure_copy (s));
2159 gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2160 cdata->src_caps = gst_caps_copy(src_caps);
2161 gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF));
2162 gst_caps_append(cdata->src_caps, gst_caps_copy (src_caps));
2163 type_name = gst_aml_v4l2_video_dec_set_metadata (s, cdata, basename);
2164
2165 /* Skip over if we hit an unmapped type */
2166 if (!type_name)
xuesong.jiangae1548e2022-05-06 16:38:46 +08002167 {
bo.xiao857b8682024-09-12 16:40:32 +08002168 g_free (cdata);
2169 continue;
xuesong.jiangae1548e2022-05-06 16:38:46 +08002170 }
bo.xiao857b8682024-09-12 16:40:32 +08002171
2172 type = gst_aml_v4l2_video_dec_get_type ();
2173 g_type_query (type, &type_query);
2174 memset (&type_info, 0, sizeof (type_info));
2175 type_info.class_size = type_query.class_size;
2176 type_info.instance_size = type_query.instance_size;
2177 type_info.class_init = gst_aml_v4l2_video_dec_subclass_init;
2178 type_info.class_data = cdata;
2179 type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init;
2180
2181 subtype = g_type_register_static (type, type_name, &type_info, 0);
2182 if (!gst_element_register (plugin, type_name, GST_RANK_PRIMARY + 1,
2183 subtype))
2184 GST_WARNING ("Failed to register plugin '%s'", type_name);
2185
2186 g_free (type_name);
2187 }
xuesong.jiangae1548e2022-05-06 16:38:46 +08002188}
xuesong.jiang61ea8012022-05-12 15:38:17 +08002189
2190#if GST_IMPORT_LGE_PROP
2191static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class)
2192{
2193 g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO,
2194 g_param_spec_object("resource-info", "resource-info",
2195 "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink",
2196 GST_TYPE_STRUCTURE,
2197 G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS));
2198
2199 g_object_class_install_property(gobject_class, LGE_DECODE_SIZE,
2200 g_param_spec_uint64("decoded-size", "decoded-size",
2201 "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.",
2202 0, G_MAXUINT64,
2203 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2204
2205 g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE,
2206 g_param_spec_uint64("undecoded-size", "undecoded-size",
2207 "video decoder element's total undecoded data update unit is byte.",
2208 0, G_MAXUINT64,
2209 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2210
2211 g_object_class_install_property(gobject_class, LGE_APP_TYPE,
2212 g_param_spec_string("app-type", "app-type",
2213 "set application type.",
2214 "default_app",
2215 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2216
2217 g_object_class_install_property(gobject_class, LGE_CLIP_MODE,
2218 g_param_spec_boolean("clip-mode", "clip-mode",
2219 "When seeking, Content is moving faster for a while to skip frames.",
2220 FALSE,
2221 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
2222}
2223#endif