xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1 | /* 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 | |
| 37 | GST_DEBUG_CATEGORY_STATIC(gst_aml_v4l2_video_dec_debug); |
| 38 | #define GST_CAT_DEFAULT gst_aml_v4l2_video_dec_debug |
| 39 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 40 | #ifdef GST_AML_VIDEO_DECODER_STREAM_LOCK |
| 41 | #undef GST_AML_VIDEO_DECODER_STREAM_LOCK |
| 42 | #define GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder) \ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 43 | { \ |
fei.deng | e945847 | 2023-04-18 02:05:48 +0000 | [diff] [blame] | 44 | GST_TRACE("aml v4l2 dec locking"); \ |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 45 | g_rec_mutex_lock(&GST_AML_VIDEO_DECODER(decoder)->stream_lock); \ |
fei.deng | e945847 | 2023-04-18 02:05:48 +0000 | [diff] [blame] | 46 | GST_TRACE("aml v4l2 dec locked"); \ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 47 | } |
| 48 | #endif |
| 49 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 50 | #ifdef GST_AML_VIDEO_DECODER_STREAM_UNLOCK |
| 51 | #undef GST_AML_VIDEO_DECODER_STREAM_UNLOCK |
| 52 | #define GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder) \ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 53 | { \ |
fei.deng | e945847 | 2023-04-18 02:05:48 +0000 | [diff] [blame] | 54 | GST_TRACE("aml v4l2 dec unlocking"); \ |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 55 | g_rec_mutex_unlock(&GST_AML_VIDEO_DECODER(decoder)->stream_lock); \ |
fei.deng | e945847 | 2023-04-18 02:05:48 +0000 | [diff] [blame] | 56 | GST_TRACE("aml v4l2 dec unlocked"); \ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 57 | } |
| 58 | #endif |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 59 | |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 60 | #define GST_AML_V4L2_CC_IMPORT_QUARK gst_aml_v4l2_buffer_pool_cc_import_quark () |
| 61 | |
hanghang.luo | 36df285 | 2022-08-24 15:02:27 +0800 | [diff] [blame] | 62 | #ifndef ABSDIFF |
| 63 | #define ABSDIFF(a,b) (((a) > (b)) ? ((a) - (b)) : ((b) - (a))) |
| 64 | #endif |
| 65 | |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 66 | #if GST_IMPORT_LGE_PROP |
| 67 | typedef struct _GstAmlResourceInfo |
| 68 | { |
| 69 | gchar *coretype; |
| 70 | gint videoport; |
| 71 | gint audioport; |
| 72 | gint maxwidth; |
| 73 | gint maxheight; |
| 74 | gint mixerport; |
| 75 | } GstAmlResourceInfo; |
| 76 | |
| 77 | struct _GstAmlV4l2VideoDecLgeCtxt |
| 78 | { |
| 79 | GstAmlResourceInfo res_info; |
| 80 | guint64 dec_size; |
| 81 | guint64 undec_size; |
| 82 | gchar *app_type; |
| 83 | gboolean clip_mode; |
| 84 | }; |
| 85 | #endif |
| 86 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 87 | typedef struct |
| 88 | { |
| 89 | gchar *device; |
| 90 | GstCaps *sink_caps; |
| 91 | GstCaps *src_caps; |
| 92 | const gchar *longname; |
| 93 | const gchar *description; |
| 94 | } GstAmlV4l2VideoDecCData; |
| 95 | |
| 96 | enum |
| 97 | { |
| 98 | PROP_0, |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 99 | V4L2_STD_OBJECT_PROPS, |
| 100 | #if GST_IMPORT_LGE_PROP |
| 101 | LGE_RESOURCE_INFO, |
| 102 | LGE_DECODE_SIZE, |
| 103 | LGE_UNDECODE_SIZE, |
| 104 | LGE_APP_TYPE, |
| 105 | LGE_CLIP_MODE |
| 106 | #endif |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 107 | }; |
| 108 | |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 109 | enum |
| 110 | { |
| 111 | SIGNAL_DECODED_PTS, |
le.han | 7e9c27d | 2024-08-08 08:10:46 +0000 | [diff] [blame] | 112 | SIGNAL_DECODED_ERROR_PTS, |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 113 | MAX_SIGNAL |
| 114 | }; |
| 115 | |
| 116 | static guint g_signals[MAX_SIGNAL]= {0}; |
| 117 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 118 | #define gst_aml_v4l2_video_dec_parent_class parent_class |
| 119 | G_DEFINE_ABSTRACT_TYPE(GstAmlV4l2VideoDec, gst_aml_v4l2_video_dec, |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 120 | GST_AML_TYPE_VIDEO_DECODER); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 121 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 122 | static GstFlowReturn gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder); |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 123 | #if GST_IMPORT_LGE_PROP |
| 124 | static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class); |
| 125 | #endif |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 126 | static GstClockTime gst_aml_v4l2_video_dec_calc_output_buffer_pts(GstAmlVideoDecoder *decoder); |
| 127 | static GstClockTime gst_aml_v4l2_video_dec_calc_duration(GstAmlVideoDecoder *decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 128 | |
| 129 | static void |
| 130 | gst_aml_v4l2_video_dec_set_property(GObject *object, |
| 131 | guint prop_id, const GValue *value, GParamSpec *pspec) |
| 132 | { |
| 133 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object); |
| 134 | |
| 135 | switch (prop_id) |
| 136 | { |
| 137 | case PROP_CAPTURE_IO_MODE: |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 138 | case PROP_DUMP_FRAME_LOCATION: |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 139 | case PROP_CC_DATA: |
| 140 | if (!gst_aml_v4l2_object_set_property_helper(self->v4l2capture, |
| 141 | prop_id, value, pspec)) |
| 142 | { |
| 143 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
| 144 | } |
| 145 | break; |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 146 | #if GST_IMPORT_LGE_PROP |
| 147 | case LGE_RESOURCE_INFO: |
| 148 | { |
| 149 | GST_DEBUG_OBJECT(self, "LGE up layer set res info"); |
| 150 | GstStructure *r_info = g_value_get_object(value); |
| 151 | if (r_info) |
| 152 | { |
| 153 | if (gst_structure_has_field(r_info, "coretype")) |
| 154 | { |
| 155 | if (self->lge_ctxt->res_info.coretype) |
| 156 | g_free(self->lge_ctxt->res_info.coretype); |
| 157 | self->lge_ctxt->res_info.coretype = g_strdup(gst_structure_get_string(r_info, "coretype")); |
| 158 | } |
| 159 | if (gst_structure_has_field(r_info, "videoport")) |
| 160 | gst_structure_get_int(r_info, "videoport", &(self->lge_ctxt->res_info.videoport)); |
| 161 | if (gst_structure_has_field(r_info, "audioport")) |
| 162 | gst_structure_get_int(r_info, "audioport", &(self->lge_ctxt->res_info.audioport)); |
| 163 | if (gst_structure_has_field(r_info, "maxwidth")) |
| 164 | gst_structure_get_int(r_info, "maxwidth", &(self->lge_ctxt->res_info.maxwidth)); |
| 165 | if (gst_structure_has_field(r_info, "maxheight")) |
| 166 | gst_structure_get_int(r_info, "maxheight", &(self->lge_ctxt->res_info.maxheight)); |
| 167 | if (gst_structure_has_field(r_info, "mixerport")) |
| 168 | gst_structure_get_int(r_info, "mixerport", &(self->lge_ctxt->res_info.mixerport)); |
| 169 | } |
| 170 | break; |
| 171 | } |
| 172 | case LGE_APP_TYPE: |
| 173 | { |
| 174 | GST_DEBUG_OBJECT(self, "LGE up layer set app type"); |
| 175 | if (self->lge_ctxt->app_type) |
| 176 | g_free(self->lge_ctxt->app_type); |
| 177 | self->lge_ctxt->app_type = g_strdup(g_value_get_string(value)); |
| 178 | break; |
| 179 | } |
| 180 | case LGE_CLIP_MODE: |
| 181 | { |
| 182 | GST_DEBUG_OBJECT(self, "LGE up layer set clip mode"); |
| 183 | self->lge_ctxt->clip_mode = g_strdup(g_value_get_boolean(value)); |
| 184 | break; |
| 185 | } |
| 186 | #endif |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 187 | /* By default, only set on output */ |
| 188 | default: |
| 189 | if (!gst_aml_v4l2_object_set_property_helper(self->v4l2output, |
| 190 | prop_id, value, pspec)) |
| 191 | { |
| 192 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
| 193 | } |
| 194 | break; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | static void |
| 199 | gst_aml_v4l2_video_dec_get_property(GObject *object, |
| 200 | guint prop_id, GValue *value, GParamSpec *pspec) |
| 201 | { |
| 202 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object); |
| 203 | |
| 204 | switch (prop_id) |
| 205 | { |
| 206 | case PROP_CAPTURE_IO_MODE: |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 207 | case PROP_CC_DATA: |
fei.deng | af68276 | 2024-06-24 19:06:03 +0800 | [diff] [blame] | 208 | case PROP_DECODING_ERROR_FRAMES: |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 209 | if (!gst_aml_v4l2_object_get_property_helper(self->v4l2capture, |
| 210 | prop_id, value, pspec)) |
| 211 | { |
| 212 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
| 213 | } |
| 214 | break; |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 215 | #if GST_IMPORT_LGE_PROP |
| 216 | case LGE_DECODE_SIZE: |
| 217 | { |
| 218 | GST_DEBUG_OBJECT(self, "LGE up layer get dec size"); |
| 219 | self->lge_ctxt->dec_size = -1; |
| 220 | g_value_set_int(value, self->lge_ctxt->dec_size); |
| 221 | break; |
| 222 | } |
| 223 | case LGE_UNDECODE_SIZE: |
| 224 | { |
| 225 | GST_DEBUG_OBJECT(self, "LGE up layer get undec size"); |
| 226 | self->lge_ctxt->undec_size = -1; |
| 227 | g_value_set_int(value, self->lge_ctxt->undec_size); |
| 228 | break; |
| 229 | } |
| 230 | #endif |
| 231 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 232 | /* By default read from output */ |
| 233 | default: |
| 234 | if (!gst_aml_v4l2_object_get_property_helper(self->v4l2output, |
| 235 | prop_id, value, pspec)) |
| 236 | { |
| 237 | G_OBJECT_WARN_INVALID_PROPERTY_ID(object, prop_id, pspec); |
| 238 | } |
| 239 | break; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 244 | gst_aml_v4l2_video_dec_open(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 245 | { |
| 246 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 247 | GstCaps *codec_caps; |
| 248 | |
| 249 | GST_DEBUG_OBJECT(self, "Opening"); |
| 250 | |
| 251 | if (!gst_aml_v4l2_object_open(self->v4l2output)) |
| 252 | goto failure; |
| 253 | |
| 254 | if (!gst_aml_v4l2_object_open_shared(self->v4l2capture, self->v4l2output)) |
| 255 | goto failure; |
| 256 | |
| 257 | codec_caps = gst_pad_get_pad_template_caps(decoder->sinkpad); |
| 258 | self->probed_sinkcaps = gst_aml_v4l2_object_probe_caps(self->v4l2output, |
| 259 | codec_caps); |
| 260 | gst_caps_unref(codec_caps); |
| 261 | |
| 262 | if (gst_caps_is_empty(self->probed_sinkcaps)) |
| 263 | goto no_encoded_format; |
| 264 | |
| 265 | return TRUE; |
| 266 | |
| 267 | no_encoded_format: |
| 268 | GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS, |
| 269 | (_("Decoder on device %s has no supported input format"), |
| 270 | self->v4l2output->videodev), |
| 271 | (NULL)); |
| 272 | goto failure; |
| 273 | |
| 274 | failure: |
| 275 | if (GST_AML_V4L2_IS_OPEN(self->v4l2output)) |
| 276 | gst_aml_v4l2_object_close(self->v4l2output); |
| 277 | |
| 278 | if (GST_AML_V4L2_IS_OPEN(self->v4l2capture)) |
| 279 | gst_aml_v4l2_object_close(self->v4l2capture); |
| 280 | |
| 281 | gst_caps_replace(&self->probed_srccaps, NULL); |
| 282 | gst_caps_replace(&self->probed_sinkcaps, NULL); |
| 283 | |
| 284 | return FALSE; |
| 285 | } |
| 286 | |
| 287 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 288 | gst_aml_v4l2_video_dec_close(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 289 | { |
| 290 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 291 | |
| 292 | GST_DEBUG_OBJECT(self, "Closing"); |
| 293 | |
| 294 | gst_aml_v4l2_object_close(self->v4l2output); |
| 295 | gst_aml_v4l2_object_close(self->v4l2capture); |
| 296 | gst_caps_replace(&self->probed_srccaps, NULL); |
| 297 | gst_caps_replace(&self->probed_sinkcaps, NULL); |
| 298 | |
| 299 | return TRUE; |
| 300 | } |
| 301 | |
| 302 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 303 | gst_aml_v4l2_video_dec_start(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 304 | { |
| 305 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 306 | |
| 307 | GST_DEBUG_OBJECT(self, "Starting"); |
| 308 | |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 309 | gst_aml_v4l2_object_flush_start(self->v4l2output); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 310 | g_atomic_int_set(&self->active, TRUE); |
| 311 | self->output_flow = GST_FLOW_OK; |
| 312 | |
| 313 | return TRUE; |
| 314 | } |
| 315 | |
| 316 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 317 | gst_aml_v4l2_video_dec_stop(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 318 | { |
| 319 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 320 | |
| 321 | GST_DEBUG_OBJECT(self, "Stopping"); |
| 322 | |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 323 | gst_aml_v4l2_object_flush_start(self->v4l2output); |
| 324 | gst_aml_v4l2_object_flush_start(self->v4l2capture); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 325 | |
| 326 | /* Wait for capture thread to stop */ |
| 327 | gst_pad_stop_task(decoder->srcpad); |
| 328 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 329 | GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 330 | self->output_flow = GST_FLOW_OK; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 331 | GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 332 | |
| 333 | /* Should have been flushed already */ |
| 334 | g_assert(g_atomic_int_get(&self->active) == FALSE); |
| 335 | |
| 336 | gst_aml_v4l2_object_stop(self->v4l2output); |
| 337 | gst_aml_v4l2_object_stop(self->v4l2capture); |
| 338 | |
| 339 | if (self->input_state) |
| 340 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 341 | gst_aml_video_codec_state_unref(self->input_state); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 342 | self->input_state = NULL; |
| 343 | } |
| 344 | |
| 345 | GST_DEBUG_OBJECT(self, "Stopped"); |
| 346 | |
| 347 | return TRUE; |
| 348 | } |
| 349 | |
| 350 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 351 | gst_aml_v4l2_video_dec_codec_chg(GstAmlVideoDecoder *decoder, |
| 352 | GstAmlVideoCodecState *state) |
hanghang.luo | 8e1225b | 2023-10-10 08:54:28 +0000 | [diff] [blame] | 353 | { |
| 354 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 355 | GstStructure *s_old = NULL; |
| 356 | GstStructure *s_new = NULL; |
| 357 | |
| 358 | // first play, must set foramt; |
| 359 | if (!self->input_state) |
| 360 | return TRUE; |
| 361 | |
| 362 | if (self->input_state->caps) |
| 363 | s_old = gst_caps_get_structure(self->input_state->caps,0); |
| 364 | if (state->caps) |
| 365 | s_new = gst_caps_get_structure(state->caps,0); |
| 366 | |
| 367 | if (s_new && s_old && strcmp(gst_structure_get_name(s_new),gst_structure_get_name(s_old))) |
| 368 | return TRUE; |
| 369 | return FALSE; |
| 370 | } |
| 371 | |
| 372 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 373 | gst_aml_v4l2_video_dec_res_chg(GstAmlVideoDecoder *decoder, |
| 374 | GstAmlVideoCodecState *state) |
hanghang.luo | 8e1225b | 2023-10-10 08:54:28 +0000 | [diff] [blame] | 375 | { |
| 376 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 377 | gboolean ret = FALSE; |
| 378 | gint width_new = -1,height_new = -1,width_old = -1,height_old = -1; |
| 379 | GstStructure *s_old = NULL; |
| 380 | GstStructure *s_new = NULL; |
| 381 | |
| 382 | // first play, must set foramt; |
| 383 | if (!self->input_state) |
| 384 | { |
| 385 | ret = TRUE; |
| 386 | goto done; |
| 387 | } |
| 388 | |
| 389 | if (self->input_state->caps) |
| 390 | s_old = gst_caps_get_structure(self->input_state->caps,0); |
| 391 | if (state->caps) |
| 392 | s_new = gst_caps_get_structure(state->caps,0); |
| 393 | |
| 394 | if (s_new && gst_structure_has_field(s_new,"width") && gst_structure_has_field(s_new,"height")) |
| 395 | { |
| 396 | gst_structure_get_int(s_new,"width",&width_new); |
| 397 | gst_structure_get_int(s_new,"height",&height_new); |
| 398 | } |
| 399 | if (s_old && gst_structure_has_field(s_old,"width") && gst_structure_has_field(s_old,"height")) |
| 400 | { |
| 401 | gst_structure_get_int(s_old,"width",&width_old); |
| 402 | gst_structure_get_int(s_old,"height",&height_old); |
| 403 | } |
| 404 | |
| 405 | if (width_new != width_old || height_new != height_old) |
| 406 | ret = TRUE; |
| 407 | |
| 408 | done: |
| 409 | GST_DEBUG_OBJECT(self, "ret is %d",ret); |
| 410 | return ret; |
| 411 | } |
| 412 | |
| 413 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 414 | gst_aml_v4l2_video_dec_set_format(GstAmlVideoDecoder *decoder, |
| 415 | GstAmlVideoCodecState *state) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 416 | { |
| 417 | GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT; |
| 418 | gboolean ret = TRUE; |
| 419 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 420 | GstCaps *caps; |
| 421 | |
| 422 | GST_DEBUG_OBJECT(self, "Setting format: %" GST_PTR_FORMAT, state->caps); |
hanghang.luo | 8e1225b | 2023-10-10 08:54:28 +0000 | [diff] [blame] | 423 | if (self->input_state) |
| 424 | { |
| 425 | if (gst_aml_v4l2_video_dec_res_chg(decoder,state) || gst_aml_v4l2_video_dec_codec_chg(decoder,state)) |
| 426 | GST_DEBUG_OBJECT(self, "resolution or codec changed"); |
| 427 | else |
| 428 | goto done; |
| 429 | } |
| 430 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 431 | GstCapsFeatures *const features = gst_caps_get_features(state->caps, 0); |
hanghang.luo | c54208e | 2023-09-22 02:43:54 +0000 | [diff] [blame] | 432 | GstStructure *s = gst_caps_get_structure(state->caps,0); |
| 433 | if (s && gst_structure_has_field(s,"format")) |
| 434 | { |
| 435 | if (!strcmp("XVID",gst_structure_get_string(s,"format"))) |
| 436 | { |
| 437 | GST_DEBUG_OBJECT(self, "This is a DIVX file, cannot support"); |
| 438 | ret = FALSE; |
| 439 | goto done; |
| 440 | } |
| 441 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 442 | |
| 443 | if (gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF)) |
| 444 | self->v4l2output->req_mode = GST_V4L2_IO_DMABUF_IMPORT; |
| 445 | |
| 446 | if (self->input_state) |
| 447 | { |
| 448 | if (gst_aml_v4l2_object_caps_equal(self->v4l2output, state->caps)) |
| 449 | { |
| 450 | GST_DEBUG_OBJECT(self, "Compatible caps"); |
| 451 | goto done; |
| 452 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 453 | |
| 454 | gst_aml_v4l2_video_dec_finish(decoder); |
| 455 | gst_aml_v4l2_object_stop(self->v4l2output); |
| 456 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 457 | gst_aml_video_codec_state_unref(self->input_state); |
fei.deng | f78f712 | 2024-07-15 14:44:04 +0800 | [diff] [blame] | 458 | self->input_state = NULL; |
| 459 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 460 | /* The renegotiation flow don't blend with the base class flow. To properly |
| 461 | * stop the capture pool, if the buffers can't be orphaned, we need to |
| 462 | * reclaim our buffers, which will happend through the allocation query. |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 463 | * The allocation query is triggered by gst_aml_video_decoder_negotiate() which |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 464 | * requires the output caps to be set, but we can't know this information |
| 465 | * as we rely on the decoder, which requires the capture queue to be |
| 466 | * stopped. |
| 467 | * |
| 468 | * To workaround this issue, we simply run an allocation query with the |
| 469 | * old negotiated caps in order to drain/reclaim our buffers. That breaks |
| 470 | * the complexity and should not have much impact in performance since the |
| 471 | * following allocation query will happen on a drained pipeline and won't |
| 472 | * block. */ |
| 473 | if (self->v4l2capture->pool && |
| 474 | !gst_aml_v4l2_buffer_pool_orphan(&self->v4l2capture->pool)) |
| 475 | { |
| 476 | GstCaps *caps = gst_pad_get_current_caps(decoder->srcpad); |
| 477 | if (caps) |
| 478 | { |
| 479 | GstQuery *query = gst_query_new_allocation(caps, FALSE); |
| 480 | gst_pad_peer_query(decoder->srcpad, query); |
| 481 | gst_query_unref(query); |
| 482 | gst_caps_unref(caps); |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | gst_aml_v4l2_object_stop(self->v4l2capture); |
| 487 | self->output_flow = GST_FLOW_OK; |
| 488 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 489 | if ((ret = gst_aml_v4l2_set_drm_mode(self->v4l2output)) == FALSE) |
| 490 | { |
| 491 | GST_ERROR_OBJECT(self, "config output drm mode error"); |
| 492 | goto done; |
| 493 | } |
| 494 | |
xuesong.jiang | 22a9b11 | 2023-05-24 09:01:59 +0000 | [diff] [blame] | 495 | if ((ret = gst_aml_v4l2_set_stream_mode(self->v4l2output)) == FALSE) |
| 496 | { |
| 497 | GST_ERROR_OBJECT(self, "config output stream mode error"); |
| 498 | goto done; |
| 499 | } |
| 500 | |
hanghang.luo | ac6df00 | 2024-07-11 10:54:17 +0800 | [diff] [blame] | 501 | if (!gst_aml_v4l2_object_set_format(self->v4l2output, state->caps, &error)) |
| 502 | { |
| 503 | GST_ERROR_OBJECT(self, "set format error"); |
| 504 | goto done; |
| 505 | } |
| 506 | |
| 507 | // MUST: aml v4l2 drive request set I frame after VIDIOC_S_FMT. |
| 508 | if ((ret = gst_aml_v4l2_set_I_frame_mode(self->v4l2output)) == FALSE) |
| 509 | { |
| 510 | GST_ERROR_OBJECT(self, "config I frame mode error"); |
| 511 | goto done; |
| 512 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 513 | |
| 514 | gst_caps_replace(&self->probed_srccaps, NULL); |
| 515 | self->probed_srccaps = gst_aml_v4l2_object_probe_caps(self->v4l2capture, |
| 516 | gst_aml_v4l2_object_get_raw_caps()); |
| 517 | |
| 518 | if (gst_caps_is_empty(self->probed_srccaps)) |
| 519 | goto no_raw_format; |
| 520 | |
| 521 | caps = gst_caps_copy(self->probed_srccaps); |
| 522 | gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF)); |
| 523 | gst_caps_append(self->probed_srccaps, caps); |
hanghang.luo | 2dfa0ac | 2024-07-09 11:33:39 +0800 | [diff] [blame] | 524 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 525 | if (ret) |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 526 | self->input_state = gst_aml_video_codec_state_ref(state); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 527 | else |
| 528 | gst_aml_v4l2_error(self, &error); |
| 529 | |
| 530 | done: |
| 531 | return ret; |
| 532 | |
| 533 | no_raw_format: |
| 534 | GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS, |
| 535 | (_("Decoder on device %s has no supported output format"), |
| 536 | self->v4l2output->videodev), |
| 537 | (NULL)); |
| 538 | return GST_FLOW_ERROR; |
| 539 | } |
| 540 | |
| 541 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 542 | gst_aml_v4l2_video_dec_flush(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 543 | { |
| 544 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 545 | |
| 546 | GST_DEBUG_OBJECT(self, "Flushed"); |
| 547 | |
| 548 | /* Ensure the processing thread has stopped for the reverse playback |
| 549 | * discount case */ |
| 550 | if (gst_pad_get_task_state(decoder->srcpad) == GST_TASK_STARTED) |
| 551 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 552 | GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 553 | |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 554 | gst_aml_v4l2_object_flush_start(self->v4l2output); |
| 555 | gst_aml_v4l2_object_flush_start(self->v4l2capture); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 556 | gst_pad_stop_task(decoder->srcpad); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 557 | GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 558 | } |
| 559 | |
| 560 | self->output_flow = GST_FLOW_OK; |
| 561 | |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 562 | gst_aml_v4l2_object_flush_stop(self->v4l2output); |
| 563 | gst_aml_v4l2_object_flush_stop(self->v4l2capture); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 564 | |
| 565 | if (self->v4l2output->pool) |
| 566 | gst_aml_v4l2_buffer_pool_flush(self->v4l2output->pool); |
| 567 | |
| 568 | /* gst_aml_v4l2_buffer_pool_flush() calls streamon the capture pool and must be |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 569 | * called after gst_aml_v4l2_object_flush_stop() stopped flushing the buffer |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 570 | * pool. */ |
| 571 | if (self->v4l2capture->pool) |
| 572 | gst_aml_v4l2_buffer_pool_flush(self->v4l2capture->pool); |
| 573 | |
| 574 | return TRUE; |
| 575 | } |
| 576 | |
| 577 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 578 | gst_aml_v4l2_video_dec_negotiate(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 579 | { |
| 580 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 581 | |
xuesong.jiang | 681d360 | 2022-06-24 21:23:35 +0800 | [diff] [blame] | 582 | if (TRUE == self->v4l2output->is_svp) |
| 583 | { |
| 584 | GstStructure *s; |
| 585 | GstEvent *event; |
| 586 | |
| 587 | s = gst_structure_new_empty ("IS_SVP"); |
| 588 | event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM_STICKY, s); |
| 589 | GST_DEBUG_OBJECT(self, "before Send SVP Event :%p", event); |
| 590 | gst_pad_push_event (decoder->srcpad, event); |
| 591 | GST_DEBUG_OBJECT(self, "after Send SVP Event :%p", event); |
| 592 | } |
| 593 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 594 | /* We don't allow renegotiation without carefull disabling the pool */ |
| 595 | if (self->v4l2capture->pool && |
| 596 | gst_buffer_pool_is_active(GST_BUFFER_POOL(self->v4l2capture->pool))) |
| 597 | return TRUE; |
| 598 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 599 | return GST_AML_VIDEO_DECODER_CLASS(parent_class)->negotiate(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 600 | } |
| 601 | |
| 602 | static gboolean |
| 603 | gst_aml_v4l2_decoder_cmd(GstAmlV4l2Object *v4l2object, guint cmd, guint flags) |
| 604 | { |
| 605 | struct v4l2_decoder_cmd dcmd = { |
| 606 | 0, |
| 607 | }; |
| 608 | |
| 609 | GST_DEBUG_OBJECT(v4l2object->element, |
| 610 | "sending v4l2 decoder command %u with flags %u", cmd, flags); |
| 611 | |
| 612 | if (!GST_AML_V4L2_IS_OPEN(v4l2object)) |
| 613 | return FALSE; |
| 614 | |
| 615 | dcmd.cmd = cmd; |
| 616 | dcmd.flags = flags; |
| 617 | if (v4l2object->ioctl(v4l2object->video_fd, VIDIOC_DECODER_CMD, &dcmd) < 0) |
| 618 | goto dcmd_failed; |
| 619 | |
| 620 | return TRUE; |
| 621 | |
| 622 | dcmd_failed: |
| 623 | if (errno == ENOTTY) |
| 624 | { |
| 625 | GST_INFO_OBJECT(v4l2object->element, |
| 626 | "Failed to send decoder command %u with flags %u for '%s'. (%s)", |
| 627 | cmd, flags, v4l2object->videodev, g_strerror(errno)); |
| 628 | } |
| 629 | else |
| 630 | { |
| 631 | GST_ERROR_OBJECT(v4l2object->element, |
| 632 | "Failed to send decoder command %u with flags %u for '%s'. (%s)", |
| 633 | cmd, flags, v4l2object->videodev, g_strerror(errno)); |
| 634 | } |
| 635 | return FALSE; |
| 636 | } |
| 637 | |
| 638 | static GstFlowReturn |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 639 | gst_aml_v4l2_video_dec_finish(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 640 | { |
| 641 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 642 | GstFlowReturn ret = GST_FLOW_OK; |
| 643 | GstBuffer *buffer; |
| 644 | |
| 645 | if (gst_pad_get_task_state(decoder->srcpad) != GST_TASK_STARTED) |
| 646 | goto done; |
| 647 | |
| 648 | GST_DEBUG_OBJECT(self, "Finishing decoding"); |
| 649 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 650 | GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 651 | |
| 652 | if (gst_aml_v4l2_decoder_cmd(self->v4l2output, V4L2_DEC_CMD_STOP, 0)) |
| 653 | { |
| 654 | GstTask *task = decoder->srcpad->task; |
| 655 | |
| 656 | /* If the decoder stop command succeeded, just wait until processing is |
| 657 | * finished */ |
| 658 | GST_DEBUG_OBJECT(self, "Waiting for decoder stop"); |
| 659 | GST_OBJECT_LOCK(task); |
| 660 | while (GST_TASK_STATE(task) == GST_TASK_STARTED) |
| 661 | GST_TASK_WAIT(task); |
| 662 | GST_OBJECT_UNLOCK(task); |
| 663 | ret = GST_FLOW_FLUSHING; |
| 664 | } |
| 665 | else |
| 666 | { |
| 667 | /* otherwise keep queuing empty buffers until the processing thread has |
| 668 | * stopped, _pool_process() will return FLUSHING when that happened */ |
| 669 | while (ret == GST_FLOW_OK) |
| 670 | { |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 671 | GST_DEBUG_OBJECT(self, "queue empty output buf"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 672 | buffer = gst_buffer_new(); |
| 673 | ret = |
| 674 | gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &buffer); |
| 675 | gst_buffer_unref(buffer); |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | /* and ensure the processing thread has stopped in case another error |
| 680 | * occured. */ |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 681 | gst_aml_v4l2_object_flush_start(self->v4l2capture); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 682 | gst_pad_stop_task(decoder->srcpad); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 683 | GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 684 | |
| 685 | if (ret == GST_FLOW_FLUSHING) |
| 686 | ret = self->output_flow; |
| 687 | |
fei.deng | 4244a26 | 2024-07-31 14:07:14 +0800 | [diff] [blame] | 688 | /*if V4L2_DEC_CMD_STOP called,indicate decoder will stop. |
| 689 | should reset need_wait_event=true to wait source change event*/ |
| 690 | self->v4l2capture->need_wait_event = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 691 | GST_DEBUG_OBJECT(decoder, "Done draining buffers"); |
| 692 | |
| 693 | /* TODO Shall we cleanup any reffed frame to workaround broken decoders ? */ |
| 694 | |
| 695 | done: |
| 696 | return ret; |
| 697 | } |
| 698 | |
| 699 | static GstFlowReturn |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 700 | gst_aml_v4l2_video_dec_drain(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 701 | { |
| 702 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 703 | |
| 704 | GST_DEBUG_OBJECT(self, "Draining..."); |
| 705 | gst_aml_v4l2_video_dec_finish(decoder); |
| 706 | gst_aml_v4l2_video_dec_flush(decoder); |
| 707 | |
| 708 | return GST_FLOW_OK; |
| 709 | } |
| 710 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 711 | static GstAmlVideoCodecFrame * |
| 712 | gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(GstAmlVideoDecoder *decoder, GstClockTime pts) |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 713 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 714 | GstAmlVideoCodecFrame *frame = NULL; |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 715 | GList *frames, *l; |
| 716 | gint count = 0; |
| 717 | |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 718 | GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts)); |
| 719 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 720 | frames = gst_aml_video_decoder_get_frames(decoder); |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 721 | |
| 722 | for (l = frames; l != NULL; l = l->next) |
| 723 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 724 | GstAmlVideoCodecFrame *f = l->data; |
fei.deng | e945847 | 2023-04-18 02:05:48 +0000 | [diff] [blame] | 725 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 726 | if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts,pts)) < 1000) { |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 727 | frame = f; |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 728 | } |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 729 | count++; |
| 730 | } |
| 731 | |
zengliang.li | ddee2da | 2023-07-14 07:27:05 +0000 | [diff] [blame] | 732 | if (!frame) |
| 733 | { |
| 734 | for (l = frames; l != NULL; l = l->next) |
| 735 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 736 | GstAmlVideoCodecFrame *f = l->data; |
zengliang.li | ddee2da | 2023-07-14 07:27:05 +0000 | [diff] [blame] | 737 | if (!GST_CLOCK_TIME_IS_VALID(f->pts)) |
| 738 | { |
| 739 | frame = f; |
fei.deng | f78f712 | 2024-07-15 14:44:04 +0800 | [diff] [blame] | 740 | GST_DEBUG("The pts of the expected output frame is invalid"); |
| 741 | break; |
zengliang.li | ddee2da | 2023-07-14 07:27:05 +0000 | [diff] [blame] | 742 | } |
zengliang.li | ddee2da | 2023-07-14 07:27:05 +0000 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 746 | if (frame) |
| 747 | { |
| 748 | GST_LOG_OBJECT(decoder, |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 749 | "frame %p is %d %" GST_TIME_FORMAT " and %d frames left", |
| 750 | frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), count - 1); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 751 | gst_aml_video_codec_frame_ref(frame); |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 752 | } |
| 753 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 754 | g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref); |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 755 | |
| 756 | return frame; |
| 757 | } |
| 758 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 759 | static GstAmlVideoCodecFrame * |
| 760 | gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(GstAmlVideoDecoder *decoder, GstClockTime pts) |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 761 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 762 | GstAmlVideoCodecFrame *frame = NULL; |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 763 | GList *frames, *l; |
hanghang.luo | 4486d7d | 2024-07-24 10:06:35 +0800 | [diff] [blame] | 764 | guint frames_len = 0; |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 765 | GST_LOG_OBJECT (decoder, "trace in with pts: %" GST_TIME_FORMAT, GST_TIME_ARGS(pts)); |
| 766 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 767 | if (!(frames = gst_aml_video_decoder_get_frames(decoder))) |
hanghang.luo | 4486d7d | 2024-07-24 10:06:35 +0800 | [diff] [blame] | 768 | goto done; |
| 769 | |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 770 | frames_len = g_list_length(frames); |
| 771 | GST_LOG_OBJECT (decoder, "got frames list len:%d", frames_len); |
| 772 | |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 773 | for (l = frames; l != NULL; l = l->next) |
| 774 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 775 | GstAmlVideoCodecFrame *f = l->data; |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 776 | |
| 777 | if (GST_CLOCK_TIME_IS_VALID(pts) && (ABSDIFF(f->pts, pts)) < 1000) |
| 778 | { |
| 779 | /* found the right frame */ |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 780 | GST_LOG_OBJECT(decoder, |
| 781 | "found frame %" GST_TIME_FORMAT "with pts %" GST_TIME_FORMAT, |
| 782 | GST_TIME_ARGS(f->pts), GST_TIME_ARGS(pts)); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 783 | frame = f; |
| 784 | break; |
| 785 | } |
| 786 | else if(GST_CLOCK_TIME_IS_VALID(pts) && (f->pts < pts)) |
| 787 | { |
| 788 | GST_LOG_OBJECT(decoder, |
| 789 | "stream mode drop frame %d %" GST_TIME_FORMAT, |
hanghang.luo | a0626d2 | 2024-07-30 16:10:10 +0800 | [diff] [blame] | 790 | f->system_frame_number, GST_TIME_ARGS(f->pts)); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 791 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 792 | gst_aml_video_codec_frame_ref(f); |
| 793 | // gst_aml_video_decoder_drop_frame(decoder, f); |
| 794 | gst_aml_video_decoder_release_frame(decoder, f); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 795 | } |
| 796 | else |
| 797 | { |
| 798 | GST_LOG_OBJECT (decoder, "dbg"); |
| 799 | } |
| 800 | } |
| 801 | |
| 802 | if (frame) |
| 803 | { |
| 804 | guint l_len = 0; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 805 | l = gst_aml_video_decoder_get_frames(decoder); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 806 | l_len = g_list_length(l); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 807 | g_list_free_full(l, (GDestroyNotify)gst_aml_video_codec_frame_unref); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 808 | |
| 809 | GST_LOG_OBJECT(decoder, |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 810 | "frame %p is %d %" GST_TIME_FORMAT " and %d frames left", |
| 811 | frame, frame->system_frame_number, GST_TIME_ARGS(frame->pts), l_len); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 812 | gst_aml_video_codec_frame_ref(frame); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 813 | } |
| 814 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 815 | g_list_free_full(frames, (GDestroyNotify)gst_aml_video_codec_frame_unref); |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 816 | |
hanghang.luo | 4486d7d | 2024-07-24 10:06:35 +0800 | [diff] [blame] | 817 | done: |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 818 | return frame; |
| 819 | } |
| 820 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 821 | static GstAmlVideoCodecFrame * |
| 822 | gst_aml_v4l2_video_dec_get_right_frame(GstAmlVideoDecoder *decoder, GstClockTime pts) |
xuesong.jiang | e24aef9 | 2023-06-16 06:39:10 +0000 | [diff] [blame] | 823 | { |
| 824 | GstAmlV4l2VideoDec *self = (GstAmlV4l2VideoDec *)decoder; |
| 825 | if (self->v4l2output->stream_mode) |
| 826 | return gst_aml_v4l2_video_dec_get_right_frame_for_stream_mode(decoder, pts); |
| 827 | else |
| 828 | return gst_aml_v4l2_video_dec_get_right_frame_for_frame_mode(decoder, pts); |
| 829 | } |
| 830 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 831 | static gboolean |
| 832 | gst_aml_v4l2_video_remove_padding(GstCapsFeatures *features, |
| 833 | GstStructure *structure, gpointer user_data) |
| 834 | { |
| 835 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(user_data); |
| 836 | GstVideoAlignment *align = &self->v4l2capture->align; |
| 837 | GstVideoInfo *info = &self->v4l2capture->info; |
| 838 | int width, height; |
| 839 | |
| 840 | if (!gst_structure_get_int(structure, "width", &width)) |
| 841 | return TRUE; |
| 842 | |
| 843 | if (!gst_structure_get_int(structure, "height", &height)) |
| 844 | return TRUE; |
| 845 | |
| 846 | if (align->padding_left != 0 || align->padding_top != 0 || |
| 847 | height != info->height + align->padding_bottom) |
| 848 | return TRUE; |
| 849 | |
| 850 | if (height == info->height + align->padding_bottom) |
| 851 | { |
| 852 | /* Some drivers may round up width to the padded with */ |
| 853 | if (width == info->width + align->padding_right) |
| 854 | gst_structure_set(structure, |
| 855 | "width", G_TYPE_INT, width - align->padding_right, |
| 856 | "height", G_TYPE_INT, height - align->padding_bottom, NULL); |
| 857 | /* Some drivers may keep visible width and only round up bytesperline */ |
| 858 | else if (width == info->width) |
| 859 | gst_structure_set(structure, |
| 860 | "height", G_TYPE_INT, height - align->padding_bottom, NULL); |
| 861 | } |
| 862 | |
| 863 | return TRUE; |
| 864 | } |
| 865 | |
| 866 | static void |
sheng.liu | bcf036c | 2022-06-21 15:55:42 +0800 | [diff] [blame] | 867 | gst_v4l2_drop_event (GstAmlV4l2Object * v4l2object) |
sheng.liu | b56bbc5 | 2022-06-21 11:02:33 +0800 | [diff] [blame] | 868 | { |
| 869 | struct v4l2_event evt; |
| 870 | gint ret; |
| 871 | |
| 872 | memset (&evt, 0x00, sizeof (struct v4l2_event)); |
| 873 | ret = v4l2object->ioctl (v4l2object->video_fd, VIDIOC_DQEVENT, &evt); |
| 874 | if (ret < 0) |
| 875 | { |
| 876 | GST_DEBUG_OBJECT (v4l2object, "dqevent failed"); |
| 877 | return; |
| 878 | } |
| 879 | |
| 880 | switch (evt.type) |
| 881 | { |
| 882 | case V4L2_EVENT_SOURCE_CHANGE: |
| 883 | GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_SOURCE_CHANGE"); |
| 884 | break; |
| 885 | case V4L2_EVENT_EOS: |
| 886 | GST_DEBUG_OBJECT (v4l2object, "Drop GST_V4L2_FLOW_LAST_BUFFER"); |
| 887 | break; |
| 888 | default: |
| 889 | break; |
| 890 | } |
| 891 | |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | static void |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 896 | gst_aml_v4l2_video_dec_set_output_status(GstAmlVideoDecoder *decoder,GstVideoInfo info) |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 897 | { |
| 898 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 899 | GstAmlVideoCodecState *output_state; |
hanghang.luo | 9b60a3c | 2023-08-01 16:01:47 +0000 | [diff] [blame] | 900 | struct v4l2_selection sel; |
| 901 | struct v4l2_rect *r = NULL; |
| 902 | GstStructure *s; |
| 903 | gint width = 0; |
| 904 | gint height = 0; |
hanghang.luo | 2dfa0ac | 2024-07-09 11:33:39 +0800 | [diff] [blame] | 905 | GST_DEBUG("%d %d",info.width, info.height); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 906 | output_state = gst_aml_video_decoder_set_output_state(decoder, |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 907 | info.finfo->format, info.width, info.height, self->input_state); |
hanghang.luo | 9b60a3c | 2023-08-01 16:01:47 +0000 | [diff] [blame] | 908 | memset(&sel, 0, sizeof(struct v4l2_selection)); |
| 909 | sel.type = self->v4l2capture->type; |
| 910 | sel.target = V4L2_SEL_TGT_COMPOSE_DEFAULT; |
| 911 | if (self->v4l2capture->ioctl(self->v4l2capture->video_fd, VIDIOC_G_SELECTION, &sel) >= 0) |
| 912 | { |
| 913 | r = &sel.r; |
| 914 | width = (r->width/2)*2; |
| 915 | height = (r->height/2)*2; |
| 916 | GST_DEBUG_OBJECT(self, "w:%d h:%d ",width,height); |
| 917 | } |
| 918 | else |
| 919 | GST_DEBUG_OBJECT(self, "iotcl error"); |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 920 | if (output_state) |
| 921 | { |
| 922 | output_state->info.interlace_mode = info.interlace_mode; |
| 923 | output_state->allocation_caps =gst_video_info_to_caps(&info); |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 924 | output_state->caps =gst_video_info_to_caps(&info); |
hanghang.luo | 9b60a3c | 2023-08-01 16:01:47 +0000 | [diff] [blame] | 925 | s = gst_caps_get_structure(output_state->caps, 0); |
| 926 | if (s) |
| 927 | { |
hanghang.luo | 2dfa0ac | 2024-07-09 11:33:39 +0800 | [diff] [blame] | 928 | gst_structure_set(s,"src_width",G_TYPE_INT,width,NULL); |
| 929 | gst_structure_set(s,"src_height",G_TYPE_INT,height,NULL); |
| 930 | gst_structure_set(s,"width",G_TYPE_INT,info.width,NULL); |
| 931 | gst_structure_set(s,"height",G_TYPE_INT,info.height,NULL); |
hanghang.luo | 9b60a3c | 2023-08-01 16:01:47 +0000 | [diff] [blame] | 932 | GST_DEBUG_OBJECT(self, "output_state->caps: %" GST_PTR_FORMAT, output_state->caps); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 933 | gst_aml_video_codec_state_unref(output_state); |
hanghang.luo | 9b60a3c | 2023-08-01 16:01:47 +0000 | [diff] [blame] | 934 | } |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 935 | } |
| 936 | } |
| 937 | |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 938 | static GQuark |
| 939 | gst_aml_v4l2_buffer_pool_cc_import_quark (void) |
| 940 | { |
| 941 | static GQuark quark = 0; |
| 942 | |
| 943 | if (quark == 0) |
| 944 | quark = g_quark_from_string ("GstAmlV4l2BufferPoolCcUsePtrData"); |
| 945 | |
| 946 | return quark; |
| 947 | } |
| 948 | |
| 949 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 950 | foreach_cc_buffer_list_match_pts_func(GList *list , GstAmlVideoCodecFrame *frame) |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 951 | { |
| 952 | GList *l; |
| 953 | if (g_list_length (list) > 0) |
| 954 | { |
| 955 | for (l = list; l != NULL; l = l->next) |
| 956 | { |
| 957 | GstBuffer *cc_buffer = l->data; |
| 958 | if (GST_BUFFER_TIMESTAMP (frame->output_buffer) == GST_BUFFER_TIMESTAMP (cc_buffer)) |
| 959 | { |
| 960 | gst_mini_object_set_qdata (GST_MINI_OBJECT (frame->output_buffer), GST_AML_V4L2_CC_IMPORT_QUARK, |
| 961 | gst_buffer_ref(cc_buffer), (GDestroyNotify) gst_buffer_unref); |
| 962 | #if 0 |
| 963 | //Debug code:dump cc data |
| 964 | GstMapInfo gst_map; |
| 965 | gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ); |
| 966 | int fd=open("/data/test/cc1.data",O_RDWR |O_CREAT|O_APPEND,0777); |
| 967 | if (gst_map.size>0) |
| 968 | write(fd,gst_map.data,gst_map.size); |
| 969 | close(fd); |
| 970 | gst_buffer_unmap(cc_buffer,&gst_map); |
| 971 | #endif |
| 972 | GST_DEBUG("match success"); |
| 973 | return TRUE; |
| 974 | } |
| 975 | else |
| 976 | { |
| 977 | GST_DEBUG("match fail"); |
| 978 | } |
| 979 | } |
| 980 | GST_WARNING("no match frame in the bufferlist"); |
| 981 | } |
| 982 | else |
| 983 | { |
| 984 | GST_WARNING("list is null,can not foreach"); |
| 985 | } |
| 986 | return FALSE; |
| 987 | } |
| 988 | |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 989 | static GstClockTime |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 990 | gst_aml_v4l2_video_dec_calc_output_buffer_pts(GstAmlVideoDecoder *decoder) |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 991 | { |
| 992 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 993 | GstClockTime pts = GST_CLOCK_TIME_NONE; |
| 994 | |
| 995 | if (GST_CLOCK_TIME_IS_VALID (self->last_out_pts) && GST_CLOCK_TIME_IS_VALID(self->frame_duration)) { |
| 996 | pts = self->last_out_pts + self->frame_duration; |
| 997 | GST_LOG_OBJECT (decoder, |
| 998 | "calculate PTS %" GST_TIME_FORMAT " by duration: %" GST_TIME_FORMAT, |
| 999 | GST_TIME_ARGS (pts), GST_TIME_ARGS (self->frame_duration)); |
| 1000 | } |
| 1001 | else |
| 1002 | { |
| 1003 | pts = 0; |
| 1004 | GST_INFO_OBJECT (decoder,"Set PTS=0"); |
| 1005 | } |
| 1006 | return pts; |
| 1007 | } |
| 1008 | |
| 1009 | static GstClockTime |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 1010 | gst_aml_v4l2_video_dec_calc_duration(GstAmlVideoDecoder *decoder) |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1011 | { |
| 1012 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1013 | GstClockTime duration = GST_CLOCK_TIME_NONE; |
| 1014 | gint fps_n, fps_d; |
| 1015 | fps_n = gst_value_get_fraction_numerator(self->v4l2capture->fps); |
| 1016 | fps_d = gst_value_get_fraction_denominator(self->v4l2capture->fps); |
| 1017 | |
| 1018 | if (fps_n != 0 && fps_d != 0) |
| 1019 | { |
| 1020 | duration = gst_util_uint64_scale (GST_SECOND, fps_d, fps_n); |
| 1021 | |
| 1022 | if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) |
| 1023 | { |
| 1024 | duration = duration / 2; |
| 1025 | } |
| 1026 | } |
| 1027 | |
| 1028 | GST_INFO_OBJECT (decoder,"framerate(fps_d: %d, fps_n: %d) -> frame_duration = %" GST_TIME_FORMAT, fps_d, fps_n, GST_TIME_ARGS(duration)); |
| 1029 | |
| 1030 | return duration; |
| 1031 | } |
| 1032 | |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 1033 | static void |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1034 | gst_aml_v4l2_video_dec_loop(GstAmlVideoDecoder *decoder) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1035 | { |
| 1036 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1037 | GstAmlV4l2BufferPool *v4l2_pool; |
| 1038 | GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT; |
| 1039 | GstBufferPool *pool; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1040 | GstAmlVideoCodecFrame *frame; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1041 | GstBuffer *buffer = NULL; |
| 1042 | GstFlowReturn ret; |
| 1043 | |
| 1044 | if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture))) |
| 1045 | { |
| 1046 | GstVideoInfo info; |
xuesong.jiang | 282ca57 | 2023-05-05 09:03:32 +0000 | [diff] [blame] | 1047 | GstCaps *acquired_caps, *available_caps, *caps, *filter; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1048 | GstStructure *st; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1049 | GST_DEBUG_OBJECT(self, "waitting source change event"); |
| 1050 | /* Wait until received SOURCE_CHANGE event to get right video format */ |
| 1051 | while (self->v4l2capture->can_wait_event && self->v4l2capture->need_wait_event) |
| 1052 | { |
| 1053 | ret = gst_aml_v4l2_object_dqevent(self->v4l2capture); |
| 1054 | if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE) |
| 1055 | { |
fei.deng | 2a06e04 | 2023-10-10 03:09:45 +0000 | [diff] [blame] | 1056 | //let flush start event blocked until capture buffer pool actived |
| 1057 | self->is_res_chg = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1058 | GST_DEBUG_OBJECT(self, "Received source change event"); |
| 1059 | break; |
| 1060 | } |
| 1061 | else if (ret == GST_AML_V4L2_FLOW_LAST_BUFFER) |
| 1062 | { |
| 1063 | GST_DEBUG_OBJECT(self, "Received eos event"); |
| 1064 | goto beach; |
| 1065 | } |
| 1066 | else if (ret != GST_FLOW_OK) |
| 1067 | { |
| 1068 | GST_ERROR_OBJECT(self, "dqevent error"); |
| 1069 | goto beach; |
| 1070 | } |
| 1071 | } |
| 1072 | self->v4l2capture->need_wait_event = FALSE; |
| 1073 | |
sheng.liu | 0c77f6c | 2022-06-17 21:33:20 +0800 | [diff] [blame] | 1074 | if (TRUE == self->v4l2output->is_svp) |
| 1075 | { |
| 1076 | GstPad *peer; |
| 1077 | GstStructure *s; |
| 1078 | GstEvent *event; |
| 1079 | |
| 1080 | peer = gst_pad_get_peer (decoder->srcpad); |
| 1081 | if (peer) |
| 1082 | { |
hanghang.luo | 70f07ef | 2023-07-13 02:23:06 +0000 | [diff] [blame] | 1083 | s = gst_structure_new_empty ("IS_SVP"); |
| 1084 | if (s) |
| 1085 | { |
| 1086 | event = gst_event_new_custom (GST_EVENT_CUSTOM_DOWNSTREAM, s); |
| 1087 | gst_pad_send_event (peer, event); |
| 1088 | GST_DEBUG_OBJECT(self, "Send SVP Event"); |
| 1089 | } |
| 1090 | gst_object_unref (peer); |
sheng.liu | 0c77f6c | 2022-06-17 21:33:20 +0800 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
sheng.liu | b56bbc5 | 2022-06-21 11:02:33 +0800 | [diff] [blame] | 1094 | if (self->v4l2capture->need_drop_event) |
| 1095 | { |
| 1096 | // drop V4L2_EVENT_SOURCE_CHANGE |
| 1097 | gst_v4l2_drop_event(self->v4l2capture); |
| 1098 | self->v4l2capture->need_drop_event = FALSE; |
| 1099 | } |
| 1100 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1101 | if (!gst_aml_v4l2_object_acquire_format(self->v4l2capture, &info)) |
| 1102 | goto not_negotiated; |
hanghang.luo | 8ec04e9 | 2023-10-09 08:14:24 +0000 | [diff] [blame] | 1103 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1104 | /* Create caps from the acquired format, remove the format field */ |
| 1105 | acquired_caps = gst_video_info_to_caps(&info); |
| 1106 | GST_DEBUG_OBJECT(self, "Acquired caps: %" GST_PTR_FORMAT, acquired_caps); |
| 1107 | st = gst_caps_get_structure(acquired_caps, 0); |
xuesong.jiang | 282ca57 | 2023-05-05 09:03:32 +0000 | [diff] [blame] | 1108 | gst_structure_remove_fields(st, "format", "colorimetry", "chroma-site", NULL); |
| 1109 | |
| 1110 | /* Probe currently available pixel formats */ |
| 1111 | available_caps = gst_caps_copy(self->probed_srccaps); |
| 1112 | GST_DEBUG_OBJECT(self, "Available caps: %" GST_PTR_FORMAT, available_caps); |
| 1113 | |
| 1114 | /* Replace coded size with visible size, we want to negotiate visible size |
| 1115 | * with downstream, not coded size. */ |
| 1116 | gst_caps_map_in_place(available_caps, gst_aml_v4l2_video_remove_padding, self); |
| 1117 | |
| 1118 | filter = gst_caps_intersect_full(available_caps, acquired_caps, GST_CAPS_INTERSECT_FIRST); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1119 | caps = gst_caps_copy(filter); |
| 1120 | gst_caps_set_features_simple(caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF)); |
| 1121 | gst_caps_append(filter, caps); |
| 1122 | |
| 1123 | GST_DEBUG_OBJECT(self, "Filtered caps: %" GST_PTR_FORMAT, filter); |
| 1124 | gst_caps_unref(acquired_caps); |
xuesong.jiang | 282ca57 | 2023-05-05 09:03:32 +0000 | [diff] [blame] | 1125 | gst_caps_unref(available_caps); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1126 | caps = gst_pad_peer_query_caps(decoder->srcpad, filter); |
| 1127 | gst_caps_unref(filter); |
| 1128 | |
| 1129 | GST_DEBUG_OBJECT(self, "Possible decoded caps: %" GST_PTR_FORMAT, caps); |
| 1130 | if (gst_caps_is_empty(caps)) |
| 1131 | { |
| 1132 | gst_caps_unref(caps); |
| 1133 | goto not_negotiated; |
| 1134 | } |
| 1135 | |
| 1136 | /* Fixate pixel format */ |
| 1137 | caps = gst_caps_fixate(caps); |
| 1138 | |
| 1139 | GST_DEBUG_OBJECT(self, "Chosen decoded caps: %" GST_PTR_FORMAT, caps); |
| 1140 | |
| 1141 | /* Try to set negotiated format, on success replace acquired format */ |
| 1142 | if (gst_aml_v4l2_object_set_format(self->v4l2capture, caps, &error)) |
| 1143 | gst_video_info_from_caps(&info, caps); |
| 1144 | else |
| 1145 | gst_aml_v4l2_clear_error(&error); |
| 1146 | gst_caps_unref(caps); |
hanghang.luo | 2eec489 | 2023-07-18 06:44:42 +0000 | [diff] [blame] | 1147 | gst_aml_v4l2_video_dec_set_output_status(decoder,info); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1148 | if (!gst_aml_video_decoder_negotiate(decoder)) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1149 | { |
| 1150 | if (GST_PAD_IS_FLUSHING(decoder->srcpad)) |
| 1151 | goto flushing; |
| 1152 | else |
| 1153 | goto not_negotiated; |
| 1154 | } |
| 1155 | |
| 1156 | /* Ensure our internal pool is activated */ |
| 1157 | if (!gst_buffer_pool_set_active(GST_BUFFER_POOL(self->v4l2capture->pool), |
| 1158 | TRUE)) |
| 1159 | goto activate_failed; |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1160 | |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1161 | //cal duration when got resolution event |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 1162 | self->frame_duration = gst_aml_v4l2_video_dec_calc_duration(decoder); |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1163 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1164 | g_mutex_lock(&self->res_chg_lock); |
| 1165 | GST_LOG_OBJECT(decoder, "signal resolution changed"); |
| 1166 | self->is_res_chg = FALSE; |
| 1167 | g_cond_signal(&self->res_chg_cond); |
| 1168 | g_mutex_unlock(&self->res_chg_lock); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1169 | } |
| 1170 | |
| 1171 | GST_LOG_OBJECT(decoder, "Allocate output buffer"); |
| 1172 | |
| 1173 | v4l2_pool = GST_AML_V4L2_BUFFER_POOL(self->v4l2capture->pool); |
| 1174 | |
| 1175 | self->output_flow = GST_FLOW_OK; |
| 1176 | do |
| 1177 | { |
| 1178 | /* We cannot use the base class allotate helper since it taking the internal |
| 1179 | * stream lock. we know that the acquire may need to poll until more frames |
| 1180 | * comes in and holding this lock would prevent that. |
| 1181 | */ |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1182 | pool = gst_aml_video_decoder_get_buffer_pool(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1183 | |
| 1184 | /* Pool may be NULL if we started going to READY state */ |
| 1185 | if (pool == NULL) |
| 1186 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1187 | GST_WARNING_OBJECT(decoder, "gst_aml_video_decoder_get_buffer_pool goto beach"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1188 | ret = GST_FLOW_FLUSHING; |
| 1189 | goto beach; |
| 1190 | } |
| 1191 | |
| 1192 | ret = gst_buffer_pool_acquire_buffer(pool, &buffer, NULL); |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 1193 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1194 | g_object_unref(pool); |
| 1195 | |
fei.deng | 9a5cd6e | 2023-06-30 12:09:18 +0000 | [diff] [blame] | 1196 | if (ret == GST_FLOW_OK && GST_BUFFER_FLAG_IS_SET(buffer,GST_AML_V4L2_BUFFER_FLAG_LAST_EMPTY)) { |
fei.deng | 990965a | 2023-11-15 07:03:15 +0000 | [diff] [blame] | 1197 | GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_LAST_BUFFER"); |
| 1198 | self->v4l2capture->need_drop_event = TRUE; |
| 1199 | gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer); |
| 1200 | if (self->is_res_chg) { |
| 1201 | //we must release last buffer |
| 1202 | gst_buffer_unref(buffer); |
| 1203 | //if resolution changed event received,we should set need_drop_event to false |
| 1204 | self->v4l2capture->need_drop_event = FALSE; |
| 1205 | gst_aml_v4l2_object_stop(self->v4l2capture); |
| 1206 | //unblock flush start event |
| 1207 | g_mutex_lock(&self->res_chg_lock); |
| 1208 | self->is_res_chg = FALSE; |
| 1209 | g_cond_signal(&self->res_chg_cond); |
| 1210 | g_mutex_unlock(&self->res_chg_lock); |
| 1211 | return; |
| 1212 | } else { |
| 1213 | goto beach; |
| 1214 | } |
sheng.liu | b56bbc5 | 2022-06-21 11:02:33 +0800 | [diff] [blame] | 1215 | } |
| 1216 | |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 1217 | if (ret == GST_AML_V4L2_FLOW_CC_DATA) |
| 1218 | { |
bo.xiao | e978c3b | 2024-08-01 20:57:15 +0800 | [diff] [blame] | 1219 | GST_DEBUG_OBJECT(decoder, "already got cc data, just continue."); |
| 1220 | continue; |
| 1221 | } |
| 1222 | |
bo.xiao | b6afda5 | 2024-08-02 16:08:30 +0800 | [diff] [blame] | 1223 | if (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT) |
bo.xiao | e978c3b | 2024-08-01 20:57:15 +0800 | [diff] [blame] | 1224 | { |
| 1225 | GST_DEBUG_OBJECT(decoder, "unknow event, just continue."); |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 1226 | continue; |
| 1227 | } |
| 1228 | |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 1229 | if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE) |
| 1230 | { |
| 1231 | GST_LOG_OBJECT(decoder, "Get GST_AML_V4L2_FLOW_SOURCE_CHANGE"); |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1232 | |
| 1233 | g_mutex_lock (&self->res_chg_lock); |
| 1234 | self->is_res_chg = TRUE; |
| 1235 | g_mutex_unlock (&self->res_chg_lock); |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 1236 | return; |
| 1237 | } |
hanghang.luo | 474440d | 2024-08-16 10:18:43 +0800 | [diff] [blame] | 1238 | |
fei.deng | af68276 | 2024-06-24 19:06:03 +0800 | [diff] [blame] | 1239 | //decoding error happened |
| 1240 | if (ret == GST_AML_V4L2_FLOW_DECODING_ERROR) |
| 1241 | { |
le.han | 5967dcf | 2024-08-21 08:50:56 +0000 | [diff] [blame] | 1242 | GST_DEBUG("send error pts:%llu - %" GST_TIME_FORMAT, v4l2_pool->obj->error_frame_pts, GST_TIME_ARGS(v4l2_pool->obj->error_frame_pts)); |
| 1243 | g_signal_emit (self, g_signals[SIGNAL_DECODED_ERROR_PTS], 0, v4l2_pool->obj->error_frame_pts, NULL); |
hanghang.luo | 474440d | 2024-08-16 10:18:43 +0800 | [diff] [blame] | 1244 | g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, v4l2_pool->obj->error_frame_pts); |
fei.deng | af68276 | 2024-06-24 19:06:03 +0800 | [diff] [blame] | 1245 | continue; |
| 1246 | } |
sheng.liu | 8d18ed2 | 2022-05-26 17:28:15 +0800 | [diff] [blame] | 1247 | |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 1248 | if (ret != GST_FLOW_OK) { |
| 1249 | GST_WARNING_OBJECT(decoder, "gst_buffer_pool_acquire_buffer goto beach ret:%d",ret); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1250 | goto beach; |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 1251 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1252 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1253 | GST_LOG_OBJECT(decoder, "Process output buffer (switching flow outstanding num:%d)", self->v4l2capture->outstanding_buf_num); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1254 | ret = gst_aml_v4l2_buffer_pool_process(v4l2_pool, &buffer); |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 1255 | |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1256 | GST_DEBUG_OBJECT(decoder, "decoded pts:%lld - %" GST_TIME_FORMAT, GST_BUFFER_PTS(buffer), GST_TIME_ARGS(GST_BUFFER_PTS(buffer))); |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 1257 | g_signal_emit (self, g_signals[SIGNAL_DECODED_PTS], 0, GST_BUFFER_PTS(buffer)); |
| 1258 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1259 | if (ret == GST_AML_V4L2_FLOW_SOURCE_CHANGE) |
| 1260 | { |
| 1261 | gst_aml_v4l2_object_stop(self->v4l2capture); |
| 1262 | return; |
| 1263 | } |
| 1264 | |
fei.deng | af68276 | 2024-06-24 19:06:03 +0800 | [diff] [blame] | 1265 | } while ((ret == GST_AML_V4L2_FLOW_CORRUPTED_BUFFER) || |
| 1266 | (ret == GST_AML_V4L2_FLOW_CC_DATA) || |
bo.xiao | b6afda5 | 2024-08-02 16:08:30 +0800 | [diff] [blame] | 1267 | (ret == GST_AML_V4L2_FLOW_UNKNOWN_EVENT) || |
fei.deng | af68276 | 2024-06-24 19:06:03 +0800 | [diff] [blame] | 1268 | (ret == GST_AML_V4L2_FLOW_DECODING_ERROR)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1269 | |
| 1270 | if (ret != GST_FLOW_OK) |
| 1271 | goto beach; |
| 1272 | |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 1273 | if (!GST_BUFFER_PTS_IS_VALID (buffer) |
| 1274 | || (GST_BUFFER_TIMESTAMP(buffer) == 0 && self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED)) |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1275 | { |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 1276 | GST_BUFFER_TIMESTAMP(buffer) = gst_aml_v4l2_video_dec_calc_output_buffer_pts(decoder); |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1277 | } |
| 1278 | |
| 1279 | if (self->v4l2capture->info.interlace_mode == GST_VIDEO_INTERLACE_MODE_INTERLEAVED) |
| 1280 | { |
| 1281 | GST_BUFFER_DURATION(buffer) = self->frame_duration; // got at resolution event. |
| 1282 | GST_BUFFER_FLAG_UNSET(buffer, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1283 | } |
| 1284 | |
fei.deng | bee2086 | 2022-06-14 14:59:48 +0800 | [diff] [blame] | 1285 | frame = gst_aml_v4l2_video_dec_get_right_frame(decoder, GST_BUFFER_TIMESTAMP (buffer)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1286 | if (frame) |
| 1287 | { |
fei.deng | ccc8963 | 2022-07-15 19:10:17 +0800 | [diff] [blame] | 1288 | self->last_out_pts = GST_BUFFER_TIMESTAMP(buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1289 | frame->output_buffer = buffer; |
fei.deng | ccc8963 | 2022-07-15 19:10:17 +0800 | [diff] [blame] | 1290 | frame->pts = GST_BUFFER_TIMESTAMP(buffer); |
| 1291 | frame->duration = GST_BUFFER_DURATION(buffer); |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1292 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1293 | buffer = NULL; |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 1294 | |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 1295 | if (self->v4l2capture->enable_cc_data) |
| 1296 | { |
| 1297 | if (foreach_cc_buffer_list_match_pts_func(v4l2_pool->cc_buffer_list, frame)) |
| 1298 | { |
| 1299 | GST_DEBUG("cc buffer and frame bind success"); |
| 1300 | GstBuffer *cc_buffer = gst_mini_object_get_qdata (GST_MINI_OBJECT (frame->output_buffer), |
| 1301 | GST_AML_V4L2_CC_IMPORT_QUARK); |
| 1302 | #if 0 |
| 1303 | //Debug code:dump cc data |
| 1304 | GstMapInfo gst_map; |
| 1305 | gst_buffer_map(cc_buffer,&gst_map,GST_MAP_READ); |
| 1306 | int fd=open("/data/test/cc2.data",O_RDWR |O_CREAT|O_APPEND,0777); |
| 1307 | if (gst_map.size>0) |
| 1308 | write(fd,gst_map.data,gst_map.size); |
| 1309 | close(fd); |
| 1310 | gst_buffer_unmap(cc_buffer,&gst_map); |
| 1311 | #endif |
| 1312 | v4l2_pool->cc_buffer_list = g_list_remove(v4l2_pool->cc_buffer_list,cc_buffer); |
| 1313 | gst_buffer_unref(cc_buffer); |
| 1314 | } |
| 1315 | else |
| 1316 | { |
| 1317 | GST_WARNING("bufferlist is empty or no match frame in the bufferlist"); |
| 1318 | } |
| 1319 | } |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1320 | ret = gst_aml_video_decoder_finish_frame(decoder, frame); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1321 | |
| 1322 | if (ret != GST_FLOW_OK) |
| 1323 | goto beach; |
| 1324 | } |
| 1325 | else |
| 1326 | { |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1327 | GST_WARNING_OBJECT(decoder, "Unmatch buffer, should be push, need refine"); |
| 1328 | //gst_pad_push (decoder->srcpad, buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1329 | gst_buffer_unref(buffer); |
| 1330 | } |
| 1331 | |
| 1332 | return; |
| 1333 | /* ERRORS */ |
| 1334 | not_negotiated: |
| 1335 | { |
| 1336 | GST_ERROR_OBJECT(self, "not negotiated"); |
| 1337 | ret = GST_FLOW_NOT_NEGOTIATED; |
| 1338 | goto beach; |
| 1339 | } |
| 1340 | activate_failed: |
| 1341 | { |
| 1342 | GST_ERROR_OBJECT(self, "Buffer pool activation failed"); |
| 1343 | GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS, |
| 1344 | (_("Failed to allocate required memory.")), |
| 1345 | ("Buffer pool activation failed")); |
| 1346 | ret = GST_FLOW_ERROR; |
| 1347 | goto beach; |
| 1348 | } |
| 1349 | flushing: |
| 1350 | { |
| 1351 | ret = GST_FLOW_FLUSHING; |
| 1352 | goto beach; |
| 1353 | } |
| 1354 | beach: |
| 1355 | GST_DEBUG_OBJECT(decoder, "Leaving output thread: %s", |
| 1356 | gst_flow_get_name(ret)); |
fei.deng | 2a06e04 | 2023-10-10 03:09:45 +0000 | [diff] [blame] | 1357 | if (self->is_res_chg) { |
| 1358 | //unblock flush start event |
| 1359 | g_mutex_lock(&self->res_chg_lock); |
| 1360 | self->is_res_chg = FALSE; |
| 1361 | g_cond_signal(&self->res_chg_cond); |
| 1362 | g_mutex_unlock(&self->res_chg_lock); |
| 1363 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1364 | gst_buffer_replace(&buffer, NULL); |
| 1365 | self->output_flow = ret; |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 1366 | gst_aml_v4l2_object_flush_start(self->v4l2output); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1367 | gst_pad_pause_task(decoder->srcpad); |
| 1368 | } |
| 1369 | |
| 1370 | static GstFlowReturn |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1371 | gst_aml_v4l2_video_dec_handle_frame(GstAmlVideoDecoder *decoder, |
| 1372 | GstAmlVideoCodecFrame *frame) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1373 | { |
| 1374 | GstAmlV4l2Error error = GST_AML_V4L2_ERROR_INIT; |
| 1375 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1376 | GstBufferPool *pool = GST_BUFFER_POOL(self->v4l2output->pool); |
| 1377 | GstFlowReturn ret = GST_FLOW_OK; |
| 1378 | gboolean processed = FALSE; |
| 1379 | GstBuffer *tmp; |
| 1380 | GstTaskState task_state; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1381 | |
| 1382 | GST_DEBUG_OBJECT(self, "Handling frame %d", frame->system_frame_number); |
| 1383 | |
| 1384 | if (G_UNLIKELY(!g_atomic_int_get(&self->active))) |
| 1385 | goto flushing; |
| 1386 | |
le.han | 44125c3 | 2024-09-03 07:55:57 +0000 | [diff] [blame^] | 1387 | if (G_UNLIKELY(!GST_CLOCK_TIME_IS_VALID(self->frame_duration))) |
| 1388 | self->frame_duration = frame->duration; |
| 1389 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1390 | if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2output))) |
| 1391 | { |
| 1392 | if (!self->input_state) |
| 1393 | goto not_negotiated; |
| 1394 | if (!gst_aml_v4l2_object_set_format(self->v4l2output, self->input_state->caps, |
| 1395 | &error)) |
| 1396 | goto not_negotiated; |
| 1397 | } |
| 1398 | |
| 1399 | if (G_UNLIKELY(!GST_AML_V4L2_IS_ACTIVE(self->v4l2capture))) |
| 1400 | { |
| 1401 | GstBuffer *codec_data; |
sheng.liu | 0c77f6c | 2022-06-17 21:33:20 +0800 | [diff] [blame] | 1402 | GstCapsFeatures *features = NULL; |
| 1403 | |
| 1404 | features = gst_caps_get_features(self->input_state->caps, 0); |
xuesong.jiang | 26749f3 | 2024-07-24 09:40:47 +0800 | [diff] [blame] | 1405 | if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF) && self->v4l2output->secure_es) |
sheng.liu | 0c77f6c | 2022-06-17 21:33:20 +0800 | [diff] [blame] | 1406 | { |
| 1407 | GST_DEBUG_OBJECT(self, "Is SVP"); |
xuesong.jiang | 26749f3 | 2024-07-24 09:40:47 +0800 | [diff] [blame] | 1408 | //TODO:need rm is_svp flag and just using secure_es flag |
sheng.liu | 0c77f6c | 2022-06-17 21:33:20 +0800 | [diff] [blame] | 1409 | self->v4l2output->is_svp = TRUE; |
| 1410 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1411 | |
| 1412 | GST_DEBUG_OBJECT(self, "Sending header"); |
| 1413 | |
| 1414 | codec_data = self->input_state->codec_data; |
| 1415 | |
| 1416 | /* We are running in byte-stream mode, so we don't know the headers, but |
| 1417 | * we need to send something, otherwise the decoder will refuse to |
| 1418 | * intialize. |
| 1419 | */ |
| 1420 | if (codec_data) |
| 1421 | { |
| 1422 | gst_buffer_ref(codec_data); |
| 1423 | } |
| 1424 | else |
| 1425 | { |
| 1426 | codec_data = gst_buffer_ref(frame->input_buffer); |
| 1427 | processed = TRUE; |
| 1428 | } |
| 1429 | |
| 1430 | /* Ensure input internal pool is active */ |
| 1431 | if (!gst_buffer_pool_is_active(pool)) |
| 1432 | { |
| 1433 | GstStructure *config = gst_buffer_pool_get_config(pool); |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1434 | // guint min = MAX(self->v4l2output->min_buffers, GST_AML_V4L2_MIN_BUFFERS); |
| 1435 | // guint max = VIDEO_MAX_FRAME; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1436 | // gst_buffer_pool_config_set_params (config, self->input_state->caps, |
| 1437 | // self->v4l2output->info.size, min, max); |
| 1438 | gst_buffer_pool_config_set_params(config, self->input_state->caps, self->v4l2output->info.size, self->v4l2output->min_buffers, self->v4l2output->min_buffers); |
| 1439 | |
| 1440 | /* There is no reason to refuse this config */ |
| 1441 | if (!gst_buffer_pool_set_config(pool, config)) |
| 1442 | goto activate_failed; |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1443 | GST_DEBUG_OBJECT(self, "setting output pool config to %" GST_PTR_FORMAT, config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1444 | |
| 1445 | if (!gst_buffer_pool_set_active(pool, TRUE)) |
| 1446 | goto activate_failed; |
| 1447 | } |
| 1448 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1449 | GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1450 | ret = |
| 1451 | gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &codec_data); |
hanghang.luo | c54208e | 2023-09-22 02:43:54 +0000 | [diff] [blame] | 1452 | self->codec_data_inject = TRUE; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1453 | GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1454 | |
| 1455 | gst_buffer_unref(codec_data); |
| 1456 | |
| 1457 | /* For decoders G_FMT returns coded size, G_SELECTION returns visible size |
| 1458 | * in the compose rectangle. gst_aml_v4l2_object_acquire_format() checks both |
| 1459 | * and returns the visible size as with/height and the coded size as |
| 1460 | * padding. */ |
| 1461 | } |
| 1462 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1463 | task_state = gst_pad_get_task_state(GST_AML_VIDEO_DECODER_SRC_PAD(self)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1464 | if (task_state == GST_TASK_STOPPED || task_state == GST_TASK_PAUSED) |
| 1465 | { |
| 1466 | /* It's possible that the processing thread stopped due to an error */ |
| 1467 | if (self->output_flow != GST_FLOW_OK && |
| 1468 | self->output_flow != GST_FLOW_FLUSHING) |
| 1469 | { |
| 1470 | GST_DEBUG_OBJECT(self, "Processing loop stopped with error, leaving"); |
| 1471 | ret = self->output_flow; |
| 1472 | goto drop; |
| 1473 | } |
| 1474 | |
| 1475 | GST_DEBUG_OBJECT(self, "Starting decoding thread"); |
| 1476 | |
| 1477 | /* Start the processing task, when it quits, the task will disable input |
| 1478 | * processing to unlock input if draining, or prevent potential block */ |
| 1479 | self->output_flow = GST_FLOW_FLUSHING; |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 1480 | /*reset poll and need_drop_event before start decoding loop thread*/ |
| 1481 | self->v4l2capture->need_drop_event = FALSE; |
| 1482 | gst_poll_set_flushing(self->v4l2capture->poll, FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1483 | if (!gst_pad_start_task(decoder->srcpad, |
| 1484 | (GstTaskFunction)gst_aml_v4l2_video_dec_loop, self, NULL)) |
| 1485 | goto start_task_failed; |
| 1486 | } |
| 1487 | |
| 1488 | if (!processed) |
| 1489 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1490 | GST_AML_VIDEO_DECODER_STREAM_UNLOCK(decoder); |
hanghang.luo | c54208e | 2023-09-22 02:43:54 +0000 | [diff] [blame] | 1491 | if (!self->codec_data_inject && self->input_state->codec_data) |
| 1492 | { |
| 1493 | ret = gst_aml_v4l2_buffer_pool_process |
| 1494 | (GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &self->input_state->codec_data); |
| 1495 | self->codec_data_inject = TRUE; |
| 1496 | if (ret != GST_FLOW_OK) |
| 1497 | goto send_codec_failed; |
| 1498 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1499 | ret = |
| 1500 | gst_aml_v4l2_buffer_pool_process(GST_AML_V4L2_BUFFER_POOL(self->v4l2output->pool), &frame->input_buffer); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1501 | GST_AML_VIDEO_DECODER_STREAM_LOCK(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1502 | |
| 1503 | if (ret == GST_FLOW_FLUSHING) |
| 1504 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1505 | if (gst_pad_get_task_state(GST_AML_VIDEO_DECODER_SRC_PAD(self)) != |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1506 | GST_TASK_STARTED) |
| 1507 | ret = self->output_flow; |
| 1508 | goto drop; |
| 1509 | } |
| 1510 | else if (ret != GST_FLOW_OK) |
| 1511 | { |
| 1512 | goto process_failed; |
| 1513 | } |
| 1514 | } |
| 1515 | |
| 1516 | /* No need to keep input arround */ |
| 1517 | tmp = frame->input_buffer; |
| 1518 | frame->input_buffer = gst_buffer_new(); |
| 1519 | gst_buffer_copy_into(frame->input_buffer, tmp, |
| 1520 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS | |
| 1521 | GST_BUFFER_COPY_META, |
| 1522 | 0, 0); |
| 1523 | gst_buffer_unref(tmp); |
| 1524 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1525 | gst_aml_video_codec_frame_unref(frame); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1526 | return ret; |
| 1527 | |
| 1528 | /* ERRORS */ |
hanghang.luo | c54208e | 2023-09-22 02:43:54 +0000 | [diff] [blame] | 1529 | send_codec_failed: |
| 1530 | GST_ERROR_OBJECT(self, "send codec_date fialed.ret is %d",ret); |
| 1531 | goto drop; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1532 | not_negotiated: |
| 1533 | { |
| 1534 | GST_ERROR_OBJECT(self, "not negotiated"); |
| 1535 | ret = GST_FLOW_NOT_NEGOTIATED; |
| 1536 | gst_aml_v4l2_error(self, &error); |
| 1537 | goto drop; |
| 1538 | } |
| 1539 | activate_failed: |
| 1540 | { |
| 1541 | GST_ELEMENT_ERROR(self, RESOURCE, SETTINGS, |
| 1542 | (_("Failed to allocate required memory.")), |
| 1543 | ("Buffer pool activation failed")); |
| 1544 | ret = GST_FLOW_ERROR; |
| 1545 | goto drop; |
| 1546 | } |
| 1547 | flushing: |
| 1548 | { |
| 1549 | ret = GST_FLOW_FLUSHING; |
| 1550 | goto drop; |
| 1551 | } |
| 1552 | |
| 1553 | start_task_failed: |
| 1554 | { |
| 1555 | GST_ELEMENT_ERROR(self, RESOURCE, FAILED, |
| 1556 | (_("Failed to start decoding thread.")), (NULL)); |
| 1557 | ret = GST_FLOW_ERROR; |
| 1558 | goto drop; |
| 1559 | } |
| 1560 | process_failed: |
| 1561 | { |
| 1562 | GST_ELEMENT_ERROR(self, RESOURCE, FAILED, |
| 1563 | (_("Failed to process frame.")), |
| 1564 | ("Maybe be due to not enough memory or failing driver")); |
| 1565 | ret = GST_FLOW_ERROR; |
| 1566 | goto drop; |
| 1567 | } |
| 1568 | drop: |
| 1569 | { |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1570 | gst_aml_video_decoder_drop_frame(decoder, frame); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1571 | return ret; |
| 1572 | } |
| 1573 | } |
| 1574 | |
| 1575 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1576 | gst_aml_v4l2_video_dec_decide_allocation(GstAmlVideoDecoder *decoder, |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1577 | GstQuery *query) |
| 1578 | { |
| 1579 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1580 | GstClockTime latency; |
| 1581 | gboolean ret = FALSE; |
| 1582 | |
| 1583 | if (gst_aml_v4l2_object_decide_allocation(self->v4l2capture, query)) |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1584 | ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->decide_allocation(decoder, query); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1585 | |
| 1586 | if (GST_CLOCK_TIME_IS_VALID(self->v4l2capture->duration)) |
| 1587 | { |
| 1588 | latency = self->v4l2capture->min_buffers * self->v4l2capture->duration; |
| 1589 | GST_DEBUG_OBJECT(self, "Setting latency: %" GST_TIME_FORMAT " (%" G_GUINT32_FORMAT " * %" G_GUINT64_FORMAT, GST_TIME_ARGS(latency), |
| 1590 | self->v4l2capture->min_buffers, self->v4l2capture->duration); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1591 | gst_aml_video_decoder_set_latency(decoder, latency, latency); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1592 | } |
| 1593 | else |
| 1594 | { |
| 1595 | GST_WARNING_OBJECT(self, "Duration invalid, not setting latency"); |
| 1596 | } |
| 1597 | |
| 1598 | return ret; |
| 1599 | } |
| 1600 | |
| 1601 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1602 | gst_aml_v4l2_video_dec_src_query(GstAmlVideoDecoder *decoder, GstQuery *query) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1603 | { |
| 1604 | gboolean ret = TRUE; |
| 1605 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1606 | |
| 1607 | switch (GST_QUERY_TYPE(query)) |
| 1608 | { |
| 1609 | case GST_QUERY_CAPS: |
| 1610 | { |
| 1611 | GstCaps *filter, *result = NULL; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1612 | GstPad *pad = GST_AML_VIDEO_DECODER_SRC_PAD(decoder); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1613 | |
| 1614 | gst_query_parse_caps(query, &filter); |
| 1615 | |
| 1616 | if (self->probed_srccaps) |
| 1617 | result = gst_caps_ref(self->probed_srccaps); |
| 1618 | else |
| 1619 | result = gst_pad_get_pad_template_caps(pad); |
| 1620 | |
| 1621 | if (filter) |
| 1622 | { |
| 1623 | GstCaps *tmp = result; |
| 1624 | result = |
| 1625 | gst_caps_intersect_full(filter, tmp, GST_CAPS_INTERSECT_FIRST); |
| 1626 | gst_caps_unref(tmp); |
| 1627 | } |
| 1628 | |
| 1629 | GST_DEBUG_OBJECT(self, "Returning src caps %" GST_PTR_FORMAT, result); |
| 1630 | |
| 1631 | gst_query_set_caps_result(query, result); |
| 1632 | gst_caps_unref(result); |
| 1633 | break; |
| 1634 | } |
| 1635 | |
| 1636 | default: |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1637 | ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->src_query(decoder, query); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1638 | break; |
| 1639 | } |
| 1640 | |
| 1641 | return ret; |
| 1642 | } |
| 1643 | |
| 1644 | static GstCaps * |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1645 | gst_aml_v4l2_video_dec_sink_getcaps(GstAmlVideoDecoder *decoder, GstCaps *filter) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1646 | { |
| 1647 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1648 | GstCaps *result; |
| 1649 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1650 | result = gst_aml_video_decoder_proxy_getcaps(decoder, self->probed_sinkcaps, |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1651 | filter); |
| 1652 | |
| 1653 | GST_DEBUG_OBJECT(self, "Returning sink caps %" GST_PTR_FORMAT, result); |
| 1654 | |
| 1655 | return result; |
| 1656 | } |
| 1657 | |
| 1658 | static gboolean |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1659 | gst_aml_v4l2_video_dec_sink_event(GstAmlVideoDecoder *decoder, GstEvent *event) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1660 | { |
| 1661 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(decoder); |
| 1662 | gboolean ret; |
| 1663 | GstEventType type = GST_EVENT_TYPE(event); |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1664 | GST_DEBUG_OBJECT (self, "received event %p %" GST_PTR_FORMAT, event, event); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1665 | |
| 1666 | switch (type) |
| 1667 | { |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 1668 | case GST_EVENT_STREAM_START: |
| 1669 | { |
| 1670 | GstStructure *s; |
| 1671 | GstEvent *event; |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 1672 | s = gst_structure_new("private_signal", "obj_ptr", G_TYPE_POINTER, self, "sig_name", G_TYPE_STRING, "decoded-pts", NULL); |
| 1673 | event = gst_event_new_custom (GST_EVENT_CUSTOM_UPSTREAM, s); |
| 1674 | GST_DEBUG_OBJECT(self, "before Send private_signal Event :%p", event); |
| 1675 | gst_pad_push_event (decoder->sinkpad, event); |
| 1676 | GST_DEBUG_OBJECT(self, "after Send private_signal Event :%p", event); |
| 1677 | break; |
| 1678 | } |
zengliang.li | 51f54b6 | 2023-10-10 12:14:49 +0000 | [diff] [blame] | 1679 | case GST_EVENT_CAPS: |
| 1680 | { |
| 1681 | GstCaps *caps; |
| 1682 | GstStructure *structure; |
sheng.liu | 641aa42 | 2023-12-26 07:05:59 +0000 | [diff] [blame] | 1683 | gint num, denom; |
zengliang.li | 51f54b6 | 2023-10-10 12:14:49 +0000 | [diff] [blame] | 1684 | |
| 1685 | gst_event_parse_caps (event, &caps); |
bo.xiao | 34e3620 | 2024-07-17 16:04:01 +0800 | [diff] [blame] | 1686 | |
zengliang.li | 51f54b6 | 2023-10-10 12:14:49 +0000 | [diff] [blame] | 1687 | structure= gst_caps_get_structure(caps, 0); |
| 1688 | if ( gst_structure_has_field(structure, "parsed") ) |
| 1689 | { |
| 1690 | gboolean parsed = TRUE; |
| 1691 | if ( gst_structure_get_boolean( structure, "parsed", &parsed ) ) |
| 1692 | { |
| 1693 | self->v4l2output->stream_mode = !parsed; |
| 1694 | GST_DEBUG("frame parsed:%d, set stream_mode to %d", parsed, self->v4l2output->stream_mode); |
| 1695 | } |
| 1696 | } |
sheng.liu | 641aa42 | 2023-12-26 07:05:59 +0000 | [diff] [blame] | 1697 | |
xuesong.jiang | 26749f3 | 2024-07-24 09:40:47 +0800 | [diff] [blame] | 1698 | if ( gst_structure_has_field(structure, "secure") ) |
| 1699 | { |
| 1700 | gboolean is_secure = FALSE; |
| 1701 | if ( gst_structure_get_boolean( structure, "secure", &is_secure ) ) |
| 1702 | { |
| 1703 | self->v4l2output->secure_es = is_secure; |
| 1704 | GST_DEBUG("is secure es:%d", self->v4l2output->secure_es); |
| 1705 | } |
| 1706 | } |
xuesong.jiang | 578add2 | 2024-07-25 15:11:30 +0800 | [diff] [blame] | 1707 | else |
| 1708 | { |
| 1709 | GstCapsFeatures *const features = gst_caps_get_features(caps, 0); |
| 1710 | if (features && gst_caps_features_contains(features, GST_CAPS_FEATURE_MEMORY_DMABUF)) |
| 1711 | { |
| 1712 | self->v4l2output->secure_es = TRUE; |
| 1713 | GST_DEBUG("If there is no secure field in caps, consider dma es is secure"); |
| 1714 | } |
| 1715 | } |
xuesong.jiang | 26749f3 | 2024-07-24 09:40:47 +0800 | [diff] [blame] | 1716 | |
sheng.liu | db26f7d | 2024-01-22 11:24:23 +0000 | [diff] [blame] | 1717 | if ( gst_structure_get_fraction( structure, "framerate", &num, &denom ) ) |
| 1718 | { |
| 1719 | if ( denom == 0 ) denom= 1; |
| 1720 | |
| 1721 | if (self->v4l2capture->fps) |
| 1722 | { |
| 1723 | g_value_unset(self->v4l2capture->fps); |
| 1724 | g_free(self->v4l2capture->fps); |
| 1725 | } |
| 1726 | |
| 1727 | self->v4l2capture->fps = g_new0(GValue, 1); |
| 1728 | g_value_init(self->v4l2capture->fps, GST_TYPE_FRACTION); |
| 1729 | gst_value_set_fraction(self->v4l2capture->fps, num, denom); |
| 1730 | |
| 1731 | GST_DEBUG_OBJECT(self, "get framerate ratio %d:%d", num, denom); |
| 1732 | } |
| 1733 | |
sheng.liu | 641aa42 | 2023-12-26 07:05:59 +0000 | [diff] [blame] | 1734 | if (( gst_structure_get_fraction( structure, "pixel-aspect-ratio", &num, &denom ) ) && |
| 1735 | ( !self->v4l2capture->have_set_par ) ) |
| 1736 | { |
| 1737 | if ( (num <= 0) || (denom <= 0) ) |
| 1738 | { |
| 1739 | num= denom= 1; |
| 1740 | } |
| 1741 | |
| 1742 | if ( self->v4l2capture->par ) |
| 1743 | { |
| 1744 | g_value_unset(self->v4l2capture->par); |
| 1745 | g_free(self->v4l2capture->par); |
| 1746 | } |
| 1747 | |
| 1748 | self->v4l2capture->par = g_new0(GValue, 1); |
| 1749 | g_value_init(self->v4l2capture->par, GST_TYPE_FRACTION); |
| 1750 | gst_value_set_fraction(self->v4l2capture->par, num, denom); |
| 1751 | GST_DEBUG_OBJECT(self, "get pixel aspect ratio %d:%d", num, denom); |
| 1752 | } |
zengliang.li | 51f54b6 | 2023-10-10 12:14:49 +0000 | [diff] [blame] | 1753 | break; |
| 1754 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1755 | case GST_EVENT_FLUSH_START: |
| 1756 | GST_DEBUG_OBJECT(self, "flush start"); |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1757 | |
| 1758 | g_mutex_lock (&self->res_chg_lock); |
| 1759 | while (self->is_res_chg) |
| 1760 | { |
| 1761 | GST_LOG_OBJECT(decoder, "wait resolution change finish"); |
| 1762 | g_cond_wait(&self->res_chg_cond, &self->res_chg_lock); |
| 1763 | } |
| 1764 | g_mutex_unlock (&self->res_chg_lock); |
| 1765 | |
zengliang.li | 92ff682 | 2023-06-06 07:12:52 +0000 | [diff] [blame] | 1766 | self->last_out_pts = GST_CLOCK_TIME_NONE; |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 1767 | gst_aml_v4l2_object_flush_start(self->v4l2output); |
| 1768 | gst_aml_v4l2_object_flush_start(self->v4l2capture); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1769 | break; |
| 1770 | default: |
| 1771 | break; |
| 1772 | } |
| 1773 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1774 | ret = GST_AML_VIDEO_DECODER_CLASS(parent_class)->sink_event(decoder, event); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1775 | |
| 1776 | switch (type) |
| 1777 | { |
| 1778 | case GST_EVENT_FLUSH_START: |
| 1779 | /* The processing thread should stop now, wait for it */ |
| 1780 | gst_pad_stop_task(decoder->srcpad); |
hanghang.luo | c54208e | 2023-09-22 02:43:54 +0000 | [diff] [blame] | 1781 | self->codec_data_inject = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1782 | GST_DEBUG_OBJECT(self, "flush start done"); |
| 1783 | break; |
| 1784 | default: |
| 1785 | break; |
| 1786 | } |
| 1787 | |
| 1788 | return ret; |
| 1789 | } |
| 1790 | |
| 1791 | static GstStateChangeReturn |
| 1792 | gst_aml_v4l2_video_dec_change_state(GstElement *element, |
| 1793 | GstStateChange transition) |
| 1794 | { |
| 1795 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(element); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1796 | GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(element); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1797 | |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1798 | GST_DEBUG_OBJECT(element, "change state from %s to %s", |
| 1799 | gst_element_state_get_name (GST_STATE_TRANSITION_CURRENT (transition)), |
| 1800 | gst_element_state_get_name (GST_STATE_TRANSITION_NEXT (transition))); |
| 1801 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1802 | if (transition == GST_STATE_CHANGE_PAUSED_TO_READY) |
| 1803 | { |
| 1804 | g_atomic_int_set(&self->active, FALSE); |
fei.deng | 6bccf82 | 2024-08-22 10:05:44 +0800 | [diff] [blame] | 1805 | gst_aml_v4l2_object_flush_start(self->v4l2output); |
| 1806 | gst_aml_v4l2_object_flush_start(self->v4l2capture); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1807 | gst_pad_stop_task(decoder->srcpad); |
| 1808 | } |
| 1809 | |
| 1810 | return GST_ELEMENT_CLASS(parent_class)->change_state(element, transition); |
| 1811 | } |
| 1812 | |
| 1813 | static void |
| 1814 | gst_aml_v4l2_video_dec_dispose(GObject *object) |
| 1815 | { |
| 1816 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object); |
| 1817 | |
| 1818 | gst_caps_replace(&self->probed_sinkcaps, NULL); |
| 1819 | gst_caps_replace(&self->probed_srccaps, NULL); |
| 1820 | |
| 1821 | G_OBJECT_CLASS(parent_class)->dispose(object); |
| 1822 | } |
| 1823 | |
| 1824 | static void |
| 1825 | gst_aml_v4l2_video_dec_finalize(GObject *object) |
| 1826 | { |
| 1827 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(object); |
| 1828 | |
| 1829 | gst_aml_v4l2_object_destroy(self->v4l2capture); |
| 1830 | gst_aml_v4l2_object_destroy(self->v4l2output); |
| 1831 | |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1832 | g_mutex_clear(&self->res_chg_lock); |
| 1833 | g_cond_clear(&self->res_chg_cond); |
| 1834 | |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 1835 | #if GST_IMPORT_LGE_PROP |
| 1836 | if (self->lge_ctxt) |
| 1837 | { |
| 1838 | if (self->lge_ctxt->app_type) |
| 1839 | g_free(self->lge_ctxt->app_type); |
| 1840 | if (self->lge_ctxt->res_info.coretype) |
| 1841 | g_free(self->lge_ctxt->res_info.coretype); |
| 1842 | free(self->lge_ctxt); |
| 1843 | } |
| 1844 | |
| 1845 | #endif |
| 1846 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1847 | G_OBJECT_CLASS(parent_class)->finalize(object); |
| 1848 | } |
| 1849 | |
| 1850 | static void |
| 1851 | gst_aml_v4l2_video_dec_init(GstAmlV4l2VideoDec *self) |
| 1852 | { |
| 1853 | /* V4L2 object are created in subinstance_init */ |
zengliang.li | 92ff682 | 2023-06-06 07:12:52 +0000 | [diff] [blame] | 1854 | self->last_out_pts = GST_CLOCK_TIME_NONE; |
bo.xiao | 8e3054d | 2024-07-31 17:13:57 +0800 | [diff] [blame] | 1855 | self->frame_duration = GST_CLOCK_TIME_NONE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1856 | self->is_secure_path = FALSE; |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1857 | self->is_res_chg = FALSE; |
hanghang.luo | c54208e | 2023-09-22 02:43:54 +0000 | [diff] [blame] | 1858 | self->codec_data_inject = FALSE; |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 1859 | g_mutex_init(&self->res_chg_lock); |
| 1860 | g_cond_init(&self->res_chg_cond); |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 1861 | #if GST_IMPORT_LGE_PROP |
| 1862 | self->lge_ctxt = malloc(sizeof(GstAmlV4l2VideoDecLgeCtxt)); |
| 1863 | memset(self->lge_ctxt, 0, sizeof(GstAmlV4l2VideoDecLgeCtxt)); |
| 1864 | #endif |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1865 | } |
| 1866 | |
| 1867 | static void |
| 1868 | gst_aml_v4l2_video_dec_subinstance_init(GTypeInstance *instance, gpointer g_class) |
| 1869 | { |
| 1870 | GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class); |
| 1871 | GstAmlV4l2VideoDec *self = GST_AML_V4L2_VIDEO_DEC(instance); |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1872 | GstAmlVideoDecoder *decoder = GST_AML_VIDEO_DECODER(instance); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1873 | |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1874 | gst_aml_video_decoder_set_packetized(decoder, TRUE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1875 | |
| 1876 | self->v4l2output = gst_aml_v4l2_object_new(GST_ELEMENT(self), |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1877 | GST_OBJECT(GST_AML_VIDEO_DECODER_SINK_PAD(self)), |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1878 | V4L2_BUF_TYPE_VIDEO_OUTPUT, klass->default_device, |
| 1879 | gst_aml_v4l2_get_output, gst_aml_v4l2_set_output, NULL); |
| 1880 | self->v4l2output->no_initial_format = TRUE; |
| 1881 | self->v4l2output->keep_aspect = FALSE; |
sheng.liu | 0c77f6c | 2022-06-17 21:33:20 +0800 | [diff] [blame] | 1882 | self->v4l2output->is_svp = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1883 | |
| 1884 | self->v4l2capture = gst_aml_v4l2_object_new(GST_ELEMENT(self), |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1885 | GST_OBJECT(GST_AML_VIDEO_DECODER_SRC_PAD(self)), |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1886 | V4L2_BUF_TYPE_VIDEO_CAPTURE, klass->default_device, |
| 1887 | gst_aml_v4l2_get_input, gst_aml_v4l2_set_input, NULL); |
| 1888 | self->v4l2capture->need_wait_event = TRUE; |
sheng.liu | b56bbc5 | 2022-06-21 11:02:33 +0800 | [diff] [blame] | 1889 | self->v4l2capture->need_drop_event = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1890 | } |
| 1891 | |
| 1892 | static void |
| 1893 | gst_aml_v4l2_video_dec_class_init(GstAmlV4l2VideoDecClass *klass) |
| 1894 | { |
| 1895 | GstElementClass *element_class; |
| 1896 | GObjectClass *gobject_class; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1897 | GstAmlVideoDecoderClass *video_decoder_class; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1898 | |
| 1899 | parent_class = g_type_class_peek_parent(klass); |
| 1900 | |
| 1901 | element_class = (GstElementClass *)klass; |
| 1902 | gobject_class = (GObjectClass *)klass; |
le.han | 02c38f0 | 2024-08-16 02:35:36 +0000 | [diff] [blame] | 1903 | video_decoder_class = (GstAmlVideoDecoderClass *)klass; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1904 | |
| 1905 | GST_DEBUG_CATEGORY_INIT(gst_aml_v4l2_video_dec_debug, "amlv4l2videodec", 0, |
| 1906 | "AML V4L2 Video Decoder"); |
| 1907 | |
| 1908 | gobject_class->dispose = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_dispose); |
| 1909 | gobject_class->finalize = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finalize); |
| 1910 | gobject_class->set_property = |
| 1911 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_property); |
| 1912 | gobject_class->get_property = |
| 1913 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_get_property); |
| 1914 | |
| 1915 | video_decoder_class->open = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_open); |
| 1916 | video_decoder_class->close = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_close); |
| 1917 | video_decoder_class->start = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_start); |
| 1918 | video_decoder_class->stop = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_stop); |
| 1919 | video_decoder_class->finish = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_finish); |
| 1920 | video_decoder_class->flush = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_flush); |
| 1921 | video_decoder_class->drain = GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_drain); |
| 1922 | video_decoder_class->set_format = |
| 1923 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_set_format); |
| 1924 | video_decoder_class->negotiate = |
| 1925 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_negotiate); |
| 1926 | video_decoder_class->decide_allocation = |
| 1927 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_decide_allocation); |
| 1928 | /* FIXME propose_allocation or not ? */ |
| 1929 | video_decoder_class->handle_frame = |
| 1930 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_handle_frame); |
| 1931 | video_decoder_class->getcaps = |
| 1932 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_getcaps); |
| 1933 | video_decoder_class->src_query = |
| 1934 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_src_query); |
| 1935 | video_decoder_class->sink_event = |
| 1936 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_sink_event); |
| 1937 | |
| 1938 | element_class->change_state = |
| 1939 | GST_DEBUG_FUNCPTR(gst_aml_v4l2_video_dec_change_state); |
| 1940 | |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 1941 | g_signals[SIGNAL_DECODED_PTS] = g_signal_new ("decoded-pts", |
| 1942 | G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)), |
| 1943 | G_SIGNAL_RUN_LAST, |
| 1944 | 0, /* class offset */ |
| 1945 | NULL, /* accumulator */ |
| 1946 | NULL, /* accu data */ |
| 1947 | g_cclosure_marshal_generic, |
| 1948 | G_TYPE_NONE, |
| 1949 | 1, |
| 1950 | G_TYPE_UINT64); |
le.han | 7e9c27d | 2024-08-08 08:10:46 +0000 | [diff] [blame] | 1951 | g_signals[SIGNAL_DECODED_ERROR_PTS] = g_signal_new ("decoded-error-pts", |
| 1952 | G_TYPE_FROM_CLASS(GST_ELEMENT_CLASS(klass)), |
| 1953 | G_SIGNAL_RUN_LAST, |
| 1954 | 0, /* class offset */ |
| 1955 | NULL, /* accumulator */ |
| 1956 | NULL, /* accu data */ |
| 1957 | g_cclosure_marshal_generic, |
| 1958 | G_TYPE_NONE, |
| 1959 | 1, |
| 1960 | G_TYPE_UINT64); |
xuesong.jiang | 406ee30 | 2023-06-28 03:45:22 +0000 | [diff] [blame] | 1961 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1962 | gst_aml_v4l2_object_install_m2m_properties_helper(gobject_class); |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 1963 | #if GST_IMPORT_LGE_PROP |
| 1964 | gst_aml_v4l2_video_dec_install_lge_properties_helper(gobject_class); |
| 1965 | #endif |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | static void |
| 1969 | gst_aml_v4l2_video_dec_subclass_init(gpointer g_class, gpointer data) |
| 1970 | { |
| 1971 | GstAmlV4l2VideoDecClass *klass = GST_AML_V4L2_VIDEO_DEC_CLASS(g_class); |
| 1972 | GstElementClass *element_class = GST_ELEMENT_CLASS(g_class); |
| 1973 | GstAmlV4l2VideoDecCData *cdata = data; |
| 1974 | |
| 1975 | klass->default_device = cdata->device; |
| 1976 | |
| 1977 | /* Note: gst_pad_template_new() take the floating ref from the caps */ |
| 1978 | gst_element_class_add_pad_template(element_class, |
| 1979 | gst_pad_template_new("sink", GST_PAD_SINK, GST_PAD_ALWAYS, |
| 1980 | cdata->sink_caps)); |
| 1981 | gst_element_class_add_pad_template(element_class, |
| 1982 | gst_pad_template_new("src", GST_PAD_SRC, GST_PAD_ALWAYS, |
| 1983 | cdata->src_caps)); |
| 1984 | |
| 1985 | gst_element_class_set_metadata(element_class, cdata->longname, |
| 1986 | "Codec/Decoder/Video/Hardware", cdata->description, |
| 1987 | "Xuesong Jiang <Xuesong.Jiang@amlogic.com>"); |
| 1988 | |
| 1989 | gst_caps_unref(cdata->sink_caps); |
| 1990 | gst_caps_unref(cdata->src_caps); |
| 1991 | g_free(cdata); |
| 1992 | } |
| 1993 | |
| 1994 | /* Probing functions */ |
| 1995 | gboolean |
| 1996 | gst_aml_v4l2_is_video_dec(GstCaps *sink_caps, GstCaps *src_caps) |
| 1997 | { |
| 1998 | gboolean ret = FALSE; |
| 1999 | |
| 2000 | if (gst_caps_is_subset(sink_caps, gst_aml_v4l2_object_get_codec_caps()) && gst_caps_is_subset(src_caps, gst_aml_v4l2_object_get_raw_caps())) |
| 2001 | ret = TRUE; |
| 2002 | |
| 2003 | return ret; |
| 2004 | } |
| 2005 | |
| 2006 | static gchar * |
| 2007 | gst_aml_v4l2_video_dec_set_metadata(GstStructure *s, GstAmlV4l2VideoDecCData *cdata, |
| 2008 | const gchar *basename) |
| 2009 | { |
| 2010 | gchar *codec_name = NULL; |
| 2011 | gchar *type_name = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2012 | |
| 2013 | #define SET_META(codec) \ |
| 2014 | G_STMT_START \ |
| 2015 | { \ |
| 2016 | cdata->longname = "AML V4L2 " codec " Decoder"; \ |
| 2017 | cdata->description = "Decodes " codec " streams via V4L2 API"; \ |
| 2018 | codec_name = g_ascii_strdown(codec, -1); \ |
| 2019 | } \ |
| 2020 | G_STMT_END |
| 2021 | |
bo.xiao | ce116a4 | 2024-07-16 16:21:53 +0800 | [diff] [blame] | 2022 | if (gst_structure_has_name(s, "video/mjpeg")) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2023 | { |
bo.xiao | ce116a4 | 2024-07-16 16:21:53 +0800 | [diff] [blame] | 2024 | SET_META("MJPEG"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2025 | } |
| 2026 | else if (gst_structure_has_name(s, "video/mpeg")) |
| 2027 | { |
bo.xiao | b6afda5 | 2024-08-02 16:08:30 +0800 | [diff] [blame] | 2028 | //include mpeg1, mpeg2, mpeg4 |
| 2029 | SET_META("MPEG4"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2030 | } |
| 2031 | else if (gst_structure_has_name(s, "video/x-h263")) |
| 2032 | { |
| 2033 | SET_META("H263"); |
| 2034 | } |
| 2035 | else if (gst_structure_has_name(s, "video/x-fwht")) |
| 2036 | { |
| 2037 | SET_META("FWHT"); |
| 2038 | } |
| 2039 | else if (gst_structure_has_name(s, "video/x-h264")) |
| 2040 | { |
| 2041 | SET_META("H264"); |
| 2042 | } |
| 2043 | else if (gst_structure_has_name(s, "video/x-h265")) |
| 2044 | { |
| 2045 | SET_META("H265"); |
| 2046 | } |
| 2047 | else if (gst_structure_has_name(s, "video/x-wmv")) |
| 2048 | { |
| 2049 | SET_META("VC1"); |
| 2050 | } |
| 2051 | else if (gst_structure_has_name(s, "video/x-vp8")) |
| 2052 | { |
| 2053 | SET_META("VP8"); |
| 2054 | } |
| 2055 | else if (gst_structure_has_name(s, "video/x-vp9")) |
| 2056 | { |
| 2057 | SET_META("VP9"); |
| 2058 | } |
| 2059 | else if (gst_structure_has_name(s, "video/x-av1")) |
| 2060 | { |
| 2061 | SET_META("AV1"); |
| 2062 | } |
zengliang.li | 51f54b6 | 2023-10-10 12:14:49 +0000 | [diff] [blame] | 2063 | else if (gst_structure_has_name(s, "video/x-avs")) |
| 2064 | { |
| 2065 | SET_META("AVS"); |
| 2066 | } |
| 2067 | else if (gst_structure_has_name(s, "video/x-avs2")) |
| 2068 | { |
| 2069 | SET_META("AVS2"); |
| 2070 | } |
| 2071 | else if (gst_structure_has_name(s, "video/x-avs3")) |
| 2072 | { |
| 2073 | SET_META("AVS3"); |
| 2074 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2075 | else if (gst_structure_has_name(s, "video/x-bayer")) |
| 2076 | { |
| 2077 | SET_META("BAYER"); |
| 2078 | } |
| 2079 | else if (gst_structure_has_name(s, "video/x-sonix")) |
| 2080 | { |
| 2081 | SET_META("SONIX"); |
| 2082 | } |
| 2083 | else if (gst_structure_has_name(s, "video/x-pwc1")) |
| 2084 | { |
| 2085 | SET_META("PWC1"); |
| 2086 | } |
| 2087 | else if (gst_structure_has_name(s, "video/x-pwc2")) |
| 2088 | { |
| 2089 | SET_META("PWC2"); |
| 2090 | } |
| 2091 | else |
| 2092 | { |
| 2093 | /* This code should be kept on sync with the exposed CODEC type of format |
| 2094 | * from gstamlv4l2object.c. This warning will only occure in case we forget |
| 2095 | * to also add a format here. */ |
| 2096 | gchar *s_str = gst_structure_to_string(s); |
| 2097 | g_warning("Missing fixed name mapping for caps '%s', this is a GStreamer " |
| 2098 | "bug, please report at https://bugs.gnome.org", |
| 2099 | s_str); |
| 2100 | g_free(s_str); |
| 2101 | } |
| 2102 | |
| 2103 | if (codec_name) |
| 2104 | { |
| 2105 | type_name = g_strdup_printf("amlv4l2%sdec", codec_name); |
| 2106 | if (g_type_from_name(type_name) != 0) |
| 2107 | { |
| 2108 | g_free(type_name); |
| 2109 | type_name = g_strdup_printf("amlv4l2%s%sdec", basename, codec_name); |
| 2110 | } |
| 2111 | |
| 2112 | g_free(codec_name); |
| 2113 | } |
| 2114 | |
| 2115 | return type_name; |
| 2116 | #undef SET_META |
| 2117 | } |
| 2118 | |
| 2119 | void gst_aml_v4l2_video_dec_register(GstPlugin *plugin, const gchar *basename, |
| 2120 | const gchar *device_path, GstCaps *sink_caps, GstCaps *src_caps) |
| 2121 | { |
| 2122 | gint i; |
| 2123 | |
| 2124 | for (i = 0; i < gst_caps_get_size(sink_caps); i++) |
| 2125 | { |
| 2126 | GstAmlV4l2VideoDecCData *cdata; |
| 2127 | GstStructure *s; |
| 2128 | GTypeQuery type_query; |
| 2129 | GTypeInfo type_info = { |
| 2130 | 0, |
| 2131 | }; |
| 2132 | GType type, subtype; |
| 2133 | gchar *type_name; |
| 2134 | |
| 2135 | s = gst_caps_get_structure(sink_caps, i); |
| 2136 | |
| 2137 | cdata = g_new0(GstAmlV4l2VideoDecCData, 1); |
| 2138 | cdata->device = g_strdup(device_path); |
| 2139 | cdata->sink_caps = gst_caps_new_empty(); |
| 2140 | gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s)); |
| 2141 | gst_caps_append_structure(cdata->sink_caps, gst_structure_copy(s)); |
| 2142 | gst_caps_set_features(cdata->sink_caps, 0, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF)); |
| 2143 | cdata->src_caps = gst_caps_copy(src_caps); |
| 2144 | gst_caps_set_features_simple(cdata->src_caps, gst_caps_features_from_string(GST_CAPS_FEATURE_MEMORY_DMABUF)); |
| 2145 | gst_caps_append(cdata->src_caps, gst_caps_copy(src_caps)); |
| 2146 | type_name = gst_aml_v4l2_video_dec_set_metadata(s, cdata, basename); |
| 2147 | |
| 2148 | /* Skip over if we hit an unmapped type */ |
| 2149 | if (!type_name) |
| 2150 | { |
| 2151 | g_free(cdata); |
| 2152 | continue; |
| 2153 | } |
| 2154 | |
| 2155 | type = gst_aml_v4l2_video_dec_get_type(); |
| 2156 | g_type_query(type, &type_query); |
| 2157 | memset(&type_info, 0, sizeof(type_info)); |
| 2158 | type_info.class_size = type_query.class_size; |
| 2159 | type_info.instance_size = type_query.instance_size; |
| 2160 | type_info.class_init = gst_aml_v4l2_video_dec_subclass_init; |
| 2161 | type_info.class_data = cdata; |
| 2162 | type_info.instance_init = gst_aml_v4l2_video_dec_subinstance_init; |
| 2163 | |
| 2164 | subtype = g_type_register_static(type, type_name, &type_info, 0); |
| 2165 | if (!gst_element_register(plugin, type_name, GST_RANK_PRIMARY + 1, |
| 2166 | subtype)) |
| 2167 | GST_WARNING("Failed to register plugin '%s'", type_name); |
| 2168 | |
| 2169 | g_free(type_name); |
| 2170 | } |
| 2171 | } |
xuesong.jiang | 61ea801 | 2022-05-12 15:38:17 +0800 | [diff] [blame] | 2172 | |
| 2173 | #if GST_IMPORT_LGE_PROP |
| 2174 | static void gst_aml_v4l2_video_dec_install_lge_properties_helper(GObjectClass *gobject_class) |
| 2175 | { |
| 2176 | g_object_class_install_property(gobject_class, LGE_RESOURCE_INFO, |
| 2177 | g_param_spec_object("resource-info", "resource-info", |
| 2178 | "After acquisition of H/W resources is completed, allocated resource information must be delivered to the decoder and the sink", |
| 2179 | GST_TYPE_STRUCTURE, |
| 2180 | G_PARAM_READABLE | G_PARAM_CONSTRUCT | G_PARAM_STATIC_STRINGS)); |
| 2181 | |
| 2182 | g_object_class_install_property(gobject_class, LGE_DECODE_SIZE, |
| 2183 | g_param_spec_uint64("decoded-size", "decoded-size", |
| 2184 | "The total amount of decoder element's decoded video es after constructing pipeline or flushing pipeline update unit is byte.", |
| 2185 | 0, G_MAXUINT64, |
| 2186 | 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 2187 | |
| 2188 | g_object_class_install_property(gobject_class, LGE_UNDECODE_SIZE, |
| 2189 | g_param_spec_uint64("undecoded-size", "undecoded-size", |
| 2190 | "video decoder element's total undecoded data update unit is byte.", |
| 2191 | 0, G_MAXUINT64, |
| 2192 | 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 2193 | |
| 2194 | g_object_class_install_property(gobject_class, LGE_APP_TYPE, |
| 2195 | g_param_spec_string("app-type", "app-type", |
| 2196 | "set application type.", |
| 2197 | "default_app", |
| 2198 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 2199 | |
| 2200 | g_object_class_install_property(gobject_class, LGE_CLIP_MODE, |
| 2201 | g_param_spec_boolean("clip-mode", "clip-mode", |
| 2202 | "When seeking, Content is moving faster for a while to skip frames.", |
| 2203 | FALSE, |
| 2204 | G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); |
| 2205 | } |
| 2206 | #endif |