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