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 <fcntl.h> |
| 25 | |
| 26 | #include <sys/mman.h> |
| 27 | #include <string.h> |
| 28 | #include <unistd.h> |
| 29 | #include <stdio.h> |
| 30 | |
| 31 | #include "gst/video/video.h" |
| 32 | #include "gst/video/gstvideometa.h" |
| 33 | #include "gst/video/gstvideopool.h" |
| 34 | #include "gst/allocators/gstdmabuf.h" |
| 35 | |
| 36 | #include <gstamlv4l2bufferpool.h> |
| 37 | |
| 38 | #include "gstamlv4l2object.h" |
| 39 | #include "gst/gst-i18n-plugin.h" |
| 40 | #include <gst/glib-compat-private.h> |
| 41 | |
| 42 | #define GST_DUMP_CAPTURE_BP_STAT_FILENAME "amlv4l2dec_capture_bp_buf_stat" |
| 43 | #define GST_DUMP_OUTPUT_BP_STAT_FILENAME "amlv4l2dec_output_bp_buf_stat" |
| 44 | |
| 45 | GST_DEBUG_CATEGORY_STATIC(amlv4l2bufferpool_debug); |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 46 | GST_DEBUG_CATEGORY_STATIC (CAT_PERFORMANCE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 47 | #define GST_CAT_DEFAULT amlv4l2bufferpool_debug |
| 48 | |
| 49 | #define GST_AML_V4L2_IMPORT_QUARK gst_aml_v4l2_buffer_pool_import_quark() |
| 50 | |
| 51 | /* |
| 52 | * GstAmlV4l2BufferPool: |
| 53 | */ |
| 54 | #define gst_aml_v4l2_buffer_pool_parent_class parent_class |
| 55 | G_DEFINE_TYPE(GstAmlV4l2BufferPool, gst_aml_v4l2_buffer_pool, GST_TYPE_BUFFER_POOL); |
| 56 | |
| 57 | enum _GstAmlV4l2BufferPoolAcquireFlags |
| 58 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 59 | GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT = |
| 60 | GST_BUFFER_POOL_ACQUIRE_FLAG_LAST, |
| 61 | GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_LAST |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 62 | }; |
| 63 | |
| 64 | static void gst_aml_v4l2_buffer_pool_release_buffer(GstBufferPool *bpool, |
| 65 | GstBuffer *buffer); |
| 66 | |
| 67 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 68 | static gboolean gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(GstBufferPool *bpool); |
| 69 | #endif |
| 70 | |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 71 | #define MAX_CC_LEN 72 |
| 72 | #ifndef V4L2_CID_USER_AMLOGIC_BASE |
| 73 | #define V4L2_CID_USER_AMLOGIC_BASE (V4L2_CID_USER_BASE + 0x1100) |
| 74 | #define AML_V4L2_GET_DECINFO_SET (V4L2_CID_USER_AMLOGIC_BASE + 13) |
| 75 | #endif |
| 76 | |
| 77 | enum E_DECINFO_CMD_GET { |
| 78 | AML_DECINFO_GET_STREAM_TYPE = 0, |
| 79 | AML_DECINFO_GET_STATISTIC_TYPE, |
| 80 | AML_DECINFO_GET_AFD_TYPE, |
| 81 | AML_DECINFO_GET_CC_TYPE, |
| 82 | AML_DECINFO_GET_HDR10_TYPE, |
| 83 | AML_DECINFO_GET_HDR10P_TYPE, |
| 84 | AML_DECINFO_GET_CUVA_TYPE, |
| 85 | AML_DECINFO_GET_DV_TYPE, |
| 86 | AML_DECINFO_GET_FRAME_TYPE, |
| 87 | AML_DECINFO_GET_COMPOSITE_TYPE = 30, |
| 88 | AML_DECINFO_GET_CMD_BOTTOM = 31, |
| 89 | }; |
| 90 | |
| 91 | struct v4l_userdata_meta_data_t { |
| 92 | __u32 poc_number; |
| 93 | __u16 flags; |
| 94 | __u16 video_format; |
| 95 | __u16 extension_data; |
| 96 | __u16 frame_type; |
| 97 | __u32 vpts; |
| 98 | __u32 vpts_valid; |
| 99 | __u64 timestamp; |
| 100 | __u32 records_in_que; |
| 101 | unsigned long long priv_data; |
| 102 | __u32 padding_data[64]; |
| 103 | }; |
| 104 | |
| 105 | struct sei_usd_param_s { |
| 106 | __u32 info_type; /* CC or AFD or aux data*/ |
| 107 | __u32 data_size; /* size of the data domain */ |
| 108 | __u64 data; /* pointer to data domain */ |
| 109 | __u64 v_addr; /* used for kernelspace data */ |
| 110 | struct v4l_userdata_meta_data_t meta_data; /* meta_data */ |
| 111 | }; |
| 112 | |
| 113 | enum E_ASPECT_RATIO { |
| 114 | ASPECT_RATIO_4_3, |
| 115 | ASPECT_RATIO_16_9, |
| 116 | ASPECT_UNDEFINED = 255 |
| 117 | }; |
| 118 | |
| 119 | struct aspect_ratio_size { |
| 120 | __s32 sar_width; |
| 121 | __s32 sar_height; |
| 122 | __s32 dar_width; |
| 123 | __s32 dar_height; |
| 124 | }; |
| 125 | |
| 126 | struct dec_stream_info_s { |
| 127 | __u32 info_type; |
| 128 | char vdec_name[32]; |
| 129 | __u32 vdec_type; |
| 130 | __u32 dual_core_flag; |
| 131 | __u32 is_secure; |
| 132 | __u32 profile_idc; |
| 133 | __u32 level_idc; |
| 134 | __u32 filed_flag; |
| 135 | __u32 frame_width; |
| 136 | __u32 frame_height; |
| 137 | __u32 crop_top; |
| 138 | __u32 crop_bottom; |
| 139 | __u32 crop_left; |
| 140 | __u32 crop_right; |
| 141 | __u32 frame_rate; |
| 142 | __u32 fence_enable; |
| 143 | __u32 fast_output_enable; |
| 144 | __u32 trick_mode; |
| 145 | __u32 bit_depth; |
| 146 | __u32 double_write_mode; |
| 147 | __u32 error_handle_policy; |
| 148 | enum E_ASPECT_RATIO eu_aspect_ratio; |
| 149 | struct aspect_ratio_size ratio_size; |
| 150 | char reserved[64]; |
| 151 | }; |
| 152 | |
| 153 | struct dec_statistics_info_s { |
| 154 | __u32 info_ype; |
| 155 | __u32 total_decoded_frames; |
| 156 | __u32 error_frames; |
| 157 | __u32 drop_frames; |
| 158 | __u32 i_decoded_frames; |
| 159 | __u32 i_drop_frames; |
| 160 | __u32 i_error_frames; |
| 161 | __u32 p_decoded_frames; |
| 162 | __u32 p_drop_frames; |
| 163 | __u32 p_error_frames; |
| 164 | __u32 b_decoded_frames; |
| 165 | __u32 b_drop_frames; |
| 166 | __u32 b_error_frames; |
| 167 | __u32 av_resynch_counter; |
| 168 | __u64 total_decoded_datasize; |
| 169 | char reserved[64]; |
| 170 | }; |
| 171 | |
| 172 | struct vframe_qos_s { |
| 173 | __u32 num; |
| 174 | __u32 type; |
| 175 | __u32 size; |
| 176 | __u32 pts; |
| 177 | int max_qp; |
| 178 | int avg_qp; |
| 179 | int min_qp; |
| 180 | int max_skip; |
| 181 | int avg_skip; |
| 182 | int min_skip; |
| 183 | int max_mv; |
| 184 | int min_mv; |
| 185 | int avg_mv; |
| 186 | int decode_buffer; |
| 187 | } /*vframe_qos */; |
| 188 | |
| 189 | struct dec_frame_info_s { |
| 190 | __u32 info_type; |
| 191 | struct vframe_qos_s qos; /* qos */ |
| 192 | __u32 num; |
| 193 | __u32 type; |
| 194 | __s32 frame_poc; |
| 195 | __u32 decode_time_cost; |
| 196 | __u32 pic_width; |
| 197 | __u32 pic_height; |
| 198 | __u32 error_flag; |
| 199 | __u32 status; |
| 200 | __u32 bitrate; |
| 201 | __u32 field_output_order; |
| 202 | __u32 offset; |
| 203 | __u32 ratio_control; |
| 204 | __u32 vf_type; |
| 205 | __u32 signal_type; |
| 206 | __u32 ext_signal_type; |
| 207 | __u32 pts; |
| 208 | __u64 pts_us64; |
| 209 | __u64 timestamp; |
| 210 | __u32 frame_size; |
| 211 | char reserved[64]; |
| 212 | }; |
| 213 | |
| 214 | struct aux_data_static_t { |
| 215 | __u32 info_type; /* HDR10 HLG FMM or IMAX */ |
| 216 | struct aml_vdec_hdr_infos hdr_info; |
| 217 | }; |
| 218 | |
| 219 | struct v4l_dec_data_extension { |
| 220 | ulong ptr; /* for future extension */ |
| 221 | __u32 data_size; |
| 222 | }; |
| 223 | |
| 224 | /** |
| 225 | * struct vdec_common_s - Structure used to receive infos from decoder |
| 226 | */ |
| 227 | struct vdec_common_s { |
| 228 | __u32 version_magic; |
| 229 | __u32 vdec_id; |
| 230 | __u32 type; |
| 231 | union { |
| 232 | struct dec_stream_info_s stream_info; |
| 233 | struct dec_frame_info_s frame_info; |
| 234 | struct dec_statistics_info_s decoder_statistics; |
| 235 | struct sei_usd_param_s usd_param; |
| 236 | struct aux_data_static_t aux_data; |
| 237 | struct v4l_dec_data_extension data_ext; |
| 238 | char raw_data[512]; |
| 239 | } u; |
| 240 | __u32 size; |
| 241 | }; |
| 242 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 243 | static gboolean |
| 244 | gst_aml_v4l2_is_buffer_valid(GstBuffer *buffer, GstAmlV4l2MemoryGroup **out_group) |
| 245 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 246 | GstMemory *mem = gst_buffer_peek_memory (buffer, 0); |
| 247 | gboolean valid = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 248 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 249 | if (GST_BUFFER_FLAG_IS_SET (buffer, GST_BUFFER_FLAG_TAG_MEMORY)) |
| 250 | goto done; |
| 251 | |
| 252 | if (gst_is_dmabuf_memory (mem)) |
| 253 | mem = gst_mini_object_get_qdata (GST_MINI_OBJECT (mem), |
| 254 | GST_AML_V4L2_MEMORY_QUARK); |
| 255 | |
| 256 | if (mem && gst_aml_is_v4l2_memory(mem)) |
| 257 | { |
| 258 | GstAmlV4l2Memory *vmem = (GstAmlV4l2Memory *)mem; |
| 259 | GstAmlV4l2MemoryGroup *group = vmem->group; |
| 260 | gint i; |
| 261 | |
| 262 | if (group->n_mem != gst_buffer_n_memory (buffer)) |
| 263 | goto done; |
| 264 | |
| 265 | for (i = 0; i < group->n_mem; i++) |
| 266 | { |
| 267 | if (group->mem[i] != gst_buffer_peek_memory (buffer, i)) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 268 | goto done; |
| 269 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 270 | if (!gst_memory_is_writable (group->mem[i])) |
| 271 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 272 | } |
| 273 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 274 | valid = TRUE; |
| 275 | if (out_group) |
| 276 | *out_group = group; |
| 277 | } |
| 278 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 279 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 280 | return valid; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static GstFlowReturn |
| 284 | gst_aml_v4l2_buffer_pool_copy_buffer(GstAmlV4l2BufferPool *pool, GstBuffer *dest, |
| 285 | GstBuffer *src) |
| 286 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 287 | const GstVideoFormatInfo *finfo = pool->caps_info.finfo; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 288 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 289 | GST_LOG_OBJECT(pool, "copying buffer"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 290 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 291 | if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN && |
| 292 | finfo->format != GST_VIDEO_FORMAT_ENCODED)) |
| 293 | { |
| 294 | GstVideoFrame src_frame, dest_frame; |
| 295 | |
| 296 | GST_DEBUG_OBJECT (pool, "copy video frame"); |
| 297 | |
| 298 | /* we have raw video, use videoframe copy to get strides right */ |
| 299 | if (!gst_video_frame_map (&src_frame, &pool->caps_info, src, GST_MAP_READ)) |
| 300 | goto invalid_buffer; |
| 301 | |
| 302 | if (!gst_video_frame_map (&dest_frame, &pool->caps_info, dest, |
| 303 | GST_MAP_WRITE)) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 304 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 305 | gst_video_frame_unmap (&src_frame); |
| 306 | goto invalid_buffer; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 307 | } |
| 308 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 309 | gst_video_frame_copy (&dest_frame, &src_frame); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 310 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 311 | gst_video_frame_unmap (&src_frame); |
| 312 | gst_video_frame_unmap (&dest_frame); |
| 313 | } |
| 314 | else |
| 315 | { |
| 316 | GstMapInfo map; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 317 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 318 | GST_DEBUG_OBJECT(pool, "copy raw bytes size:%d", gst_buffer_get_size(src)); |
| 319 | |
| 320 | if (!gst_buffer_map (src, &map, GST_MAP_READ)) |
| 321 | goto invalid_buffer; |
| 322 | |
| 323 | gst_buffer_fill (dest, 0, map.data, gst_buffer_get_size (src)); |
| 324 | |
| 325 | gst_buffer_unmap (src, &map); |
| 326 | gst_buffer_resize (dest, 0, gst_buffer_get_size (src)); |
| 327 | } |
| 328 | |
| 329 | gst_buffer_copy_into (dest, src, |
| 330 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 331 | |
| 332 | GST_CAT_LOG_OBJECT (CAT_PERFORMANCE, pool, "slow copy into buffer %p", dest); |
| 333 | |
| 334 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 335 | |
| 336 | invalid_buffer: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 337 | { |
| 338 | GST_ERROR_OBJECT (pool, "could not map buffer"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 339 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 340 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | struct UserPtrData |
| 344 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 345 | GstBuffer *buffer; |
| 346 | gboolean is_frame; |
| 347 | GstVideoFrame frame; |
| 348 | GstMapInfo map; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 349 | }; |
| 350 | |
| 351 | static GQuark |
| 352 | gst_aml_v4l2_buffer_pool_import_quark(void) |
| 353 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 354 | static GQuark quark = 0; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 355 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 356 | if (quark == 0) |
| 357 | quark = g_quark_from_string("GstAmlV4l2BufferPoolUsePtrData"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 358 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 359 | return quark; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 360 | } |
| 361 | |
| 362 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 363 | _unmap_userptr_frame (struct UserPtrData *data) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 364 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 365 | if (data->is_frame) |
| 366 | gst_video_frame_unmap (&data->frame); |
| 367 | else |
| 368 | gst_buffer_unmap (data->buffer, &data->map); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 369 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 370 | if (data->buffer) |
| 371 | gst_buffer_unref (data->buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 372 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 373 | g_slice_free (struct UserPtrData, data); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static GstFlowReturn |
| 377 | gst_aml_v4l2_buffer_pool_import_userptr(GstAmlV4l2BufferPool *pool, |
| 378 | GstBuffer *dest, GstBuffer *src) |
| 379 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 380 | GstFlowReturn ret = GST_FLOW_OK; |
| 381 | GstAmlV4l2MemoryGroup *group = NULL; |
| 382 | GstMapFlags flags; |
| 383 | const GstVideoFormatInfo *finfo = pool->caps_info.finfo; |
| 384 | struct UserPtrData *data = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 385 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 386 | GST_LOG_OBJECT (pool, "importing userptr"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 387 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 388 | /* get the group */ |
| 389 | if (!gst_aml_v4l2_is_buffer_valid(dest, &group)) |
| 390 | goto not_our_buffer; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 391 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 392 | if (V4L2_TYPE_IS_OUTPUT (pool->obj->type)) |
| 393 | flags = GST_MAP_READ; |
| 394 | else |
| 395 | flags = GST_MAP_WRITE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 396 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 397 | data = g_slice_new0 (struct UserPtrData); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 398 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 399 | if (finfo && (finfo->format != GST_VIDEO_FORMAT_UNKNOWN && |
| 400 | finfo->format != GST_VIDEO_FORMAT_ENCODED)) |
| 401 | { |
| 402 | gsize size[GST_VIDEO_MAX_PLANES] = { 0, }; |
| 403 | gint i; |
| 404 | |
| 405 | data->is_frame = TRUE; |
| 406 | |
| 407 | if (!gst_video_frame_map (&data->frame, &pool->caps_info, src, flags)) |
| 408 | goto invalid_buffer; |
| 409 | |
| 410 | for (i = 0; i < GST_VIDEO_FORMAT_INFO_N_PLANES(finfo); i++) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 411 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 412 | if (GST_VIDEO_FORMAT_INFO_IS_TILED(finfo)) |
| 413 | { |
| 414 | gint tinfo = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i); |
| 415 | gint pstride; |
| 416 | guint pheight; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 417 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 418 | pstride = GST_VIDEO_TILE_X_TILES (tinfo) << |
| 419 | GST_VIDEO_FORMAT_INFO_TILE_WS (finfo); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 420 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 421 | pheight = GST_VIDEO_TILE_Y_TILES (tinfo) << |
| 422 | GST_VIDEO_FORMAT_INFO_TILE_HS (finfo); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 423 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 424 | size[i] = pstride * pheight; |
| 425 | } |
| 426 | else |
| 427 | { |
| 428 | size[i] = GST_VIDEO_FRAME_PLANE_STRIDE (&data->frame, i) * |
| 429 | GST_VIDEO_FRAME_COMP_HEIGHT (&data->frame, i); |
| 430 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 431 | } |
| 432 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 433 | /* In the single planar API, planes must be contiguous in memory and |
| 434 | * therefore they must have expected size. ie: no padding. |
| 435 | * To check these conditions, we check that plane 'i' start address |
| 436 | * + plane 'i' size equals to plane 'i+1' start address */ |
| 437 | if (!V4L2_TYPE_IS_MULTIPLANAR(pool->obj->type)) |
| 438 | { |
| 439 | for (i = 0; i < (GST_VIDEO_FORMAT_INFO_N_PLANES(finfo) - 1); i++) |
| 440 | { |
| 441 | const struct v4l2_pix_format *pix_fmt = &pool->obj->format.fmt.pix; |
| 442 | gpointer tmp; |
| 443 | gint estride = gst_aml_v4l2_object_extrapolate_stride(finfo, i, |
| 444 | pix_fmt->bytesperline); |
| 445 | guint eheight = GST_VIDEO_FORMAT_INFO_SCALE_HEIGHT (finfo, i, |
| 446 | pix_fmt->height); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 447 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 448 | tmp = ((guint8 *) data->frame.data[i]) + estride * eheight; |
| 449 | if (tmp != data->frame.data[i + 1]) |
| 450 | goto non_contiguous_mem; |
| 451 | } |
| 452 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 453 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 454 | if (!gst_aml_v4l2_allocator_import_userptr(pool->vallocator, group, |
| 455 | data->frame.info.size, finfo->n_planes, data->frame.data, size)) |
| 456 | goto import_failed; |
| 457 | } |
| 458 | else |
| 459 | { |
| 460 | gpointer ptr[1]; |
| 461 | gsize size[1]; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 462 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 463 | data->is_frame = FALSE; |
| 464 | |
| 465 | if (!gst_buffer_map (src, &data->map, flags)) |
| 466 | goto invalid_buffer; |
| 467 | |
| 468 | ptr[0] = data->map.data; |
| 469 | size[0] = data->map.size; |
| 470 | |
| 471 | if (!gst_aml_v4l2_allocator_import_userptr(pool->vallocator, group, |
| 472 | data->map.size, 1, ptr, size)) |
| 473 | goto import_failed; |
| 474 | } |
| 475 | |
| 476 | data->buffer = gst_buffer_ref (src); |
| 477 | |
| 478 | gst_mini_object_set_qdata(GST_MINI_OBJECT(dest), GST_AML_V4L2_IMPORT_QUARK, |
| 479 | data, (GDestroyNotify) _unmap_userptr_frame); |
| 480 | |
| 481 | gst_buffer_copy_into (dest, src, |
| 482 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
| 483 | |
| 484 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 485 | |
| 486 | not_our_buffer: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 487 | { |
| 488 | GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 489 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 490 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 491 | invalid_buffer: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 492 | { |
| 493 | GST_ERROR_OBJECT (pool, "could not map buffer"); |
| 494 | g_slice_free (struct UserPtrData, data); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 495 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 496 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 497 | non_contiguous_mem: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 498 | { |
| 499 | GST_ERROR_OBJECT (pool, "memory is not contiguous or plane size mismatch"); |
| 500 | _unmap_userptr_frame (data); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 501 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 502 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 503 | import_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 504 | { |
| 505 | GST_ERROR_OBJECT (pool, "failed to import data"); |
| 506 | _unmap_userptr_frame (data); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 507 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 508 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 509 | } |
| 510 | |
| 511 | static GstFlowReturn |
| 512 | gst_aml_v4l2_buffer_pool_import_dmabuf(GstAmlV4l2BufferPool *pool, |
| 513 | GstBuffer *dest, GstBuffer *src) |
| 514 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 515 | GstAmlV4l2MemoryGroup *group = NULL; |
| 516 | GstMemory *dma_mem[GST_VIDEO_MAX_PLANES] = { 0 }; |
| 517 | guint n_mem = gst_buffer_n_memory (src); |
| 518 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 519 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 520 | GST_LOG_OBJECT (pool, "importing dmabuf"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 521 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 522 | if (!gst_aml_v4l2_is_buffer_valid(dest, &group)) |
| 523 | goto not_our_buffer; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 524 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 525 | if (n_mem > GST_VIDEO_MAX_PLANES) |
| 526 | goto too_many_mems; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 527 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 528 | for (i = 0; i < n_mem; i++) |
| 529 | dma_mem[i] = gst_buffer_peek_memory (src, i); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 530 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 531 | if (!gst_aml_v4l2_allocator_import_dmabuf(pool->vallocator, group, n_mem, |
| 532 | dma_mem)) |
| 533 | goto import_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 534 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 535 | // Output buf is secure memory, Need to unref by itselt |
| 536 | // Capture buf is secure memory, Need to unref by downstreaming element gstvideosink |
| 537 | if (V4L2_TYPE_IS_OUTPUT (pool->obj->type)) |
| 538 | gst_mini_object_set_qdata(GST_MINI_OBJECT(dest), GST_AML_V4L2_IMPORT_QUARK, |
| 539 | gst_buffer_ref(src), (GDestroyNotify) gst_buffer_unref); |
| 540 | else |
| 541 | gst_mini_object_set_qdata(GST_MINI_OBJECT(dest), GST_AML_V4L2_IMPORT_QUARK, gst_buffer_ref(src), NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 542 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 543 | gst_buffer_copy_into (dest, src, |
| 544 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 545 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 546 | GST_DEBUG_OBJECT(pool, "v4l2 buf:%p, import buf:%p as qdata", dest, src); |
| 547 | |
| 548 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 549 | |
| 550 | not_our_buffer: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 551 | { |
| 552 | GST_ERROR_OBJECT (pool, "destination buffer invalid or not from our pool"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 553 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 554 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 555 | too_many_mems: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 556 | { |
| 557 | GST_ERROR_OBJECT (pool, "could not map buffer"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 558 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 559 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 560 | import_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 561 | { |
| 562 | GST_ERROR_OBJECT (pool, "failed to import dmabuf"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 563 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 564 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static GstFlowReturn |
| 568 | gst_aml_v4l2_buffer_pool_prepare_buffer(GstAmlV4l2BufferPool *pool, |
| 569 | GstBuffer *dest, GstBuffer *src) |
| 570 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 571 | GstFlowReturn ret = GST_FLOW_OK; |
| 572 | gboolean own_src = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 573 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 574 | if (src == NULL) |
| 575 | { |
| 576 | if (pool->other_pool == NULL) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 577 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 578 | GST_ERROR_OBJECT (pool, "can't prepare buffer, source buffer missing"); |
| 579 | return GST_FLOW_ERROR; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 580 | } |
| 581 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 582 | ret = gst_buffer_pool_acquire_buffer(pool->other_pool, &src, NULL); |
| 583 | if (ret != GST_FLOW_OK) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 584 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 585 | GST_ERROR_OBJECT (pool, "failed to acquire buffer from downstream pool"); |
| 586 | goto done; |
| 587 | } |
| 588 | |
| 589 | own_src = TRUE; |
| 590 | } |
| 591 | |
| 592 | switch (pool->obj->mode) |
| 593 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 594 | case GST_V4L2_IO_MMAP: |
| 595 | case GST_V4L2_IO_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 596 | ret = gst_aml_v4l2_buffer_pool_copy_buffer(pool, dest, src); |
| 597 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 598 | case GST_V4L2_IO_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 599 | ret = gst_aml_v4l2_buffer_pool_import_userptr(pool, dest, src); |
| 600 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 601 | case GST_V4L2_IO_DMABUF_IMPORT: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 602 | ret = gst_aml_v4l2_buffer_pool_import_dmabuf(pool, dest, src); |
| 603 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 604 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 605 | break; |
| 606 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 607 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 608 | if (own_src) |
| 609 | gst_buffer_unref (src); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 610 | |
| 611 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 612 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static GstFlowReturn |
| 616 | gst_aml_v4l2_buffer_pool_alloc_buffer(GstBufferPool *bpool, GstBuffer **buffer, |
| 617 | GstBufferPoolAcquireParams *params) |
| 618 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 619 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 620 | GstAmlV4l2MemoryGroup *group = NULL; |
| 621 | GstBuffer *newbuf = NULL; |
| 622 | GstAmlV4l2Object *obj; |
| 623 | GstVideoInfo *info; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 624 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 625 | obj = pool->obj; |
| 626 | info = &obj->info; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 627 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 628 | switch (obj->mode) |
| 629 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 630 | case GST_V4L2_IO_RW: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 631 | newbuf = |
| 632 | gst_buffer_new_allocate (pool->allocator, pool->size, &pool->params); |
| 633 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 634 | case GST_V4L2_IO_MMAP: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 635 | group = gst_aml_v4l2_allocator_alloc_mmap(pool->vallocator); |
| 636 | break; |
| 637 | case GST_V4L2_IO_DMABUF: |
| 638 | group = gst_aml_v4l2_allocator_alloc_dmabuf(pool->vallocator, |
| 639 | pool->allocator); |
| 640 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 641 | case GST_V4L2_IO_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 642 | group = gst_aml_v4l2_allocator_alloc_userptr(pool->vallocator); |
| 643 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 644 | case GST_V4L2_IO_DMABUF_IMPORT: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 645 | group = gst_aml_v4l2_allocator_alloc_dmabufin(pool->vallocator); |
| 646 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 647 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 648 | newbuf = NULL; |
| 649 | g_assert_not_reached (); |
| 650 | break; |
| 651 | } |
| 652 | |
| 653 | if (group != NULL) |
| 654 | { |
| 655 | gint i; |
| 656 | newbuf = gst_buffer_new (); |
| 657 | |
| 658 | for (i = 0; i < group->n_mem; i++) |
| 659 | gst_buffer_append_memory (newbuf, group->mem[i]); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 660 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 661 | else if (newbuf == NULL) |
| 662 | { |
| 663 | goto allocation_failed; |
| 664 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 665 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 666 | /* add metadata to raw video buffers */ |
| 667 | if (pool->add_videometa) |
| 668 | gst_buffer_add_video_meta_full(newbuf, GST_VIDEO_FRAME_FLAG_NONE, |
| 669 | GST_VIDEO_INFO_FORMAT(info), GST_VIDEO_INFO_WIDTH(info), |
| 670 | GST_VIDEO_INFO_HEIGHT(info), GST_VIDEO_INFO_N_PLANES(info), |
| 671 | info->offset, info->stride); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 672 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 673 | *buffer = newbuf; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 674 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 675 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 676 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 677 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 678 | allocation_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 679 | { |
| 680 | GST_ERROR_OBJECT (pool, "failed to allocate buffer"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 681 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 682 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 683 | } |
| 684 | |
| 685 | static gboolean |
| 686 | gst_aml_v4l2_buffer_pool_set_config(GstBufferPool *bpool, GstStructure *config) |
| 687 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 688 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 689 | GstAmlV4l2Object *obj = pool->obj; |
| 690 | GstCaps *caps; |
| 691 | guint size, min_buffers, max_buffers; |
| 692 | GstAllocator *allocator; |
| 693 | GstAllocationParams params; |
| 694 | gboolean can_allocate = FALSE; |
| 695 | gboolean updated = FALSE; |
| 696 | gboolean ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 697 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 698 | pool->add_videometa = |
| 699 | gst_buffer_pool_config_has_option (config, |
| 700 | GST_BUFFER_POOL_OPTION_VIDEO_META); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 701 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 702 | /* parse the config and keep around */ |
| 703 | if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers, |
| 704 | &max_buffers)) |
| 705 | goto wrong_config; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 706 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 707 | if (!gst_buffer_pool_config_get_allocator (config, &allocator, ¶ms)) |
| 708 | goto wrong_config; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 709 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 710 | GST_DEBUG_OBJECT (pool, "config %" GST_PTR_FORMAT, config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 711 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 712 | if (pool->allocator) |
| 713 | gst_object_unref (pool->allocator); |
| 714 | pool->allocator = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 715 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 716 | switch (obj->mode) |
| 717 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 718 | case GST_V4L2_IO_DMABUF: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 719 | pool->allocator = gst_dmabuf_allocator_new (); |
| 720 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP); |
| 721 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 722 | case GST_V4L2_IO_MMAP: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 723 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP); |
| 724 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 725 | case GST_V4L2_IO_USERPTR: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 726 | can_allocate = |
| 727 | GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, USERPTR); |
| 728 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 729 | case GST_V4L2_IO_DMABUF_IMPORT: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 730 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, DMABUF); |
| 731 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 732 | case GST_V4L2_IO_RW: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 733 | if (allocator) |
| 734 | pool->allocator = g_object_ref (allocator); |
| 735 | pool->params = params; |
| 736 | /* No need to change the configuration */ |
| 737 | goto done; |
| 738 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 739 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 740 | g_assert_not_reached (); |
| 741 | break; |
| 742 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 743 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 744 | /* libv4l2 conversion code does not handle CREATE_BUFS, and may lead to |
| 745 | * instability and crash, disable it for now */ |
| 746 | if (can_allocate && obj->fmtdesc->flags & V4L2_FMT_FLAG_EMULATED) |
| 747 | { |
| 748 | GST_WARNING_OBJECT (pool, |
| 749 | "libv4l2 converter detected, disabling CREATE_BUFS"); |
| 750 | can_allocate = FALSE; |
| 751 | GST_OBJECT_FLAG_UNSET (pool->vallocator, |
| 752 | GST_V4L2_ALLOCATOR_FLAG_MMAP_CREATE_BUFS |
| 753 | | GST_V4L2_ALLOCATOR_FLAG_USERPTR_CREATE_BUFS |
| 754 | | GST_V4L2_ALLOCATOR_FLAG_DMABUF_CREATE_BUFS); |
| 755 | } |
| 756 | |
| 757 | if (min_buffers < GST_AML_V4L2_MIN_BUFFERS) |
| 758 | { |
| 759 | updated = TRUE; |
| 760 | min_buffers = GST_AML_V4L2_MIN_BUFFERS; |
| 761 | GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers); |
| 762 | } |
| 763 | |
| 764 | /* respect driver requirements */ |
| 765 | if (min_buffers < obj->min_buffers) |
| 766 | { |
| 767 | updated = TRUE; |
| 768 | min_buffers = obj->min_buffers; |
| 769 | GST_INFO_OBJECT (pool, "increasing minimum buffers to %u", min_buffers); |
| 770 | } |
| 771 | |
| 772 | if (max_buffers > VIDEO_MAX_FRAME || max_buffers == 0) |
| 773 | { |
| 774 | updated = TRUE; |
| 775 | max_buffers = VIDEO_MAX_FRAME; |
| 776 | GST_INFO_OBJECT (pool, "reducing maximum buffers to %u", max_buffers); |
| 777 | } |
| 778 | |
| 779 | if (min_buffers > max_buffers) |
| 780 | { |
| 781 | updated = TRUE; |
| 782 | min_buffers = max_buffers; |
| 783 | GST_INFO_OBJECT (pool, "reducing minimum buffers to %u", min_buffers); |
| 784 | } |
| 785 | else if (min_buffers != max_buffers) |
| 786 | { |
| 787 | if (!can_allocate) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 788 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 789 | updated = TRUE; |
| 790 | max_buffers = min_buffers; |
| 791 | GST_INFO_OBJECT (pool, "can't allocate, setting maximum to minimum"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 792 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 793 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 794 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 795 | if (!pool->add_videometa && obj->need_video_meta) |
| 796 | { |
| 797 | GST_INFO_OBJECT (pool, "adding needed video meta"); |
| 798 | updated = TRUE; |
| 799 | gst_buffer_pool_config_add_option (config, |
| 800 | GST_BUFFER_POOL_OPTION_VIDEO_META); |
| 801 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 802 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 803 | /* Always update the config to ensure the configured size matches */ |
| 804 | gst_buffer_pool_config_set_params (config, caps, obj->info.size, min_buffers, |
| 805 | max_buffers); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 806 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 807 | /* keep a GstVideoInfo with defaults for the when we need to copy */ |
| 808 | gst_video_info_from_caps (&pool->caps_info, caps); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 809 | |
| 810 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 811 | ret = GST_BUFFER_POOL_CLASS (parent_class)->set_config (bpool, config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 812 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 813 | /* If anything was changed documentation recommend to return FALSE */ |
| 814 | return !updated && ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 815 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 816 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 817 | wrong_config: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 818 | { |
| 819 | GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 820 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 821 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | static GstFlowReturn |
| 825 | gst_aml_v4l2_buffer_pool_resurrect_buffer(GstAmlV4l2BufferPool *pool) |
| 826 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 827 | GstBufferPoolAcquireParams params = { 0 }; |
| 828 | GstBuffer *buffer = NULL; |
| 829 | GstFlowReturn ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 830 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 831 | GST_DEBUG_OBJECT (pool, "A buffer was lost, reallocating it"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 832 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 833 | /* block recursive calls to this function */ |
| 834 | g_signal_handler_block (pool->vallocator, pool->group_released_handler); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 835 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 836 | params.flags = |
| 837 | (GstBufferPoolAcquireFlags) GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT | |
| 838 | GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT; |
| 839 | ret = |
| 840 | gst_buffer_pool_acquire_buffer (GST_BUFFER_POOL (pool), &buffer, ¶ms); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 841 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 842 | if (ret == GST_FLOW_OK) |
| 843 | gst_buffer_unref (buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 844 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 845 | g_signal_handler_unblock (pool->vallocator, pool->group_released_handler); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 846 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 847 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 848 | } |
| 849 | |
| 850 | static gboolean |
| 851 | gst_aml_v4l2_buffer_pool_streamon(GstAmlV4l2BufferPool *pool) |
| 852 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 853 | GstAmlV4l2Object *obj = pool->obj; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 854 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 855 | if (pool->streaming) |
| 856 | return TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 857 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 858 | switch (obj->mode) |
| 859 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 860 | case GST_V4L2_IO_MMAP: |
| 861 | case GST_V4L2_IO_USERPTR: |
| 862 | case GST_V4L2_IO_DMABUF: |
| 863 | case GST_V4L2_IO_DMABUF_IMPORT: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 864 | if (!V4L2_TYPE_IS_OUTPUT(pool->obj->type)) |
| 865 | { |
| 866 | guint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 867 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 868 | /* For captures, we need to enqueue buffers before we start streaming, |
| 869 | * so the driver don't underflow immediately. As we have put then back |
| 870 | * into the base class queue, resurrect them, then releasing will queue |
| 871 | * them back. */ |
| 872 | for (i = 0; i < pool->num_allocated; i++) |
| 873 | gst_aml_v4l2_buffer_pool_resurrect_buffer(pool); |
| 874 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 875 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 876 | if (obj->ioctl (pool->video_fd, VIDIOC_STREAMON, &obj->type) < 0) |
| 877 | goto streamon_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 878 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 879 | pool->streaming = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 880 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 881 | GST_DEBUG_OBJECT (pool, "Started streaming"); |
| 882 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 883 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 884 | break; |
| 885 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 886 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 887 | return TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 888 | |
| 889 | streamon_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 890 | { |
| 891 | GST_ERROR_OBJECT (pool, "error with STREAMON %d (%s)", errno, |
| 892 | g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 893 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 894 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 895 | } |
| 896 | |
| 897 | /* Call with streamlock held, or when streaming threads are down */ |
| 898 | static void |
| 899 | gst_aml_v4l2_buffer_pool_streamoff(GstAmlV4l2BufferPool *pool) |
| 900 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 901 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class); |
| 902 | GstAmlV4l2Object *obj = pool->obj; |
| 903 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 904 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 905 | if (!pool->streaming) |
| 906 | return; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 907 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 908 | switch (obj->mode) |
| 909 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 910 | case GST_V4L2_IO_MMAP: |
| 911 | case GST_V4L2_IO_USERPTR: |
| 912 | case GST_V4L2_IO_DMABUF: |
| 913 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 914 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 915 | if (obj->ioctl (pool->video_fd, VIDIOC_STREAMOFF, &obj->type) < 0) |
| 916 | GST_WARNING_OBJECT (pool, "STREAMOFF failed with errno %d (%s)", |
| 917 | errno, g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 918 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 919 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 920 | GST_DEBUG_OBJECT (pool, "Stopped streaming"); |
| 921 | |
| 922 | if (pool->vallocator) |
| 923 | gst_aml_v4l2_allocator_flush(pool->vallocator); |
| 924 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 925 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 926 | break; |
| 927 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 928 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 929 | GstBufferPool *bpool = GST_BUFFER_POOL(pool); |
| 930 | if (V4L2_TYPE_IS_OUTPUT(pool->obj->type)) |
| 931 | { |
| 932 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 933 | { |
| 934 | GST_INFO_OBJECT(pool, "deal with output buf index:%d, buf:%p", i, pool->buffers[i]); |
| 935 | if (pool->buffers[i]) |
| 936 | { |
| 937 | GstBuffer *buffer = pool->buffers[i]; |
| 938 | pool->buffers[i] = NULL; |
| 939 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, buffer); |
| 940 | g_atomic_int_add(&pool->num_queued, -1); |
| 941 | } |
| 942 | } |
| 943 | } |
| 944 | else |
| 945 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 946 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 947 | if (GST_V4L2_IO_DMABUF_IMPORT == obj->mode) |
| 948 | { |
| 949 | GST_DEBUG_OBJECT(pool, "have %d ready to free capture buffer", pool->ready_to_free_buf_num); |
| 950 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 951 | { |
| 952 | GST_DEBUG_OBJECT(pool, "buffers[%d]:%p, read_to_free_bufs[%d]:%p", i, pool->buffers[i], i, pool->read_to_free_bufs[i]); |
| 953 | if (pool->buffers[i]) |
| 954 | { |
| 955 | if (pool->other_pool) |
| 956 | { |
| 957 | GstBuffer *other_pool_buf = gst_mini_object_get_qdata(GST_MINI_OBJECT(pool->buffers[i]), GST_AML_V4L2_IMPORT_QUARK); |
| 958 | GST_DEBUG_OBJECT(pool, "release v4l2 capture buf[%d]:%p other pool buf:%p", i, pool->buffers[i], other_pool_buf); |
| 959 | gst_buffer_unref(other_pool_buf); |
| 960 | } |
| 961 | } |
| 962 | else if (pool->read_to_free_bufs[i]) |
| 963 | { |
| 964 | pool->buffers[i] = pool->read_to_free_bufs[i]; |
| 965 | pool->read_to_free_bufs[i] = NULL; |
| 966 | pool->ready_to_free_buf_num--; |
| 967 | } |
| 968 | } |
| 969 | GST_DEBUG_OBJECT(pool, "%d ready to free capture buffer left", pool->ready_to_free_buf_num); |
| 970 | pool->num_queued = 0; |
| 971 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 972 | #endif |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 973 | for (i = 0; i < VIDEO_MAX_FRAME; i++) |
| 974 | { |
| 975 | GST_INFO_OBJECT(pool, "deal with caputre buf index:%d, buf:%p", i, pool->buffers[i]); |
| 976 | if (pool->buffers[i]) |
| 977 | { |
| 978 | GstBuffer *buffer = pool->buffers[i]; |
| 979 | pool->buffers[i] = NULL; |
| 980 | pclass->release_buffer(bpool, buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 981 | #ifndef GST_AML_SPEC_FLOW_FOR_VBP |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 982 | g_atomic_int_add(&pool->num_queued, -1); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 983 | #endif |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | } |
| 987 | pool->streaming = FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static gboolean |
| 991 | gst_aml_v4l2_buffer_pool_start(GstBufferPool *bpool) |
| 992 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 993 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 994 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class); |
| 995 | GstAmlV4l2Object *obj = pool->obj; |
| 996 | GstStructure *config; |
| 997 | GstCaps *caps; |
| 998 | guint size, min_buffers, max_buffers; |
| 999 | guint max_latency, min_latency, copy_threshold = 0; |
| 1000 | gboolean can_allocate = FALSE, ret = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1001 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1002 | GST_DEBUG_OBJECT (pool, "activating pool"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1003 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1004 | if (pool->other_pool) |
| 1005 | { |
| 1006 | GstBuffer *buffer; |
| 1007 | |
| 1008 | if (!gst_buffer_pool_set_active (pool->other_pool, TRUE)) |
| 1009 | goto other_pool_failed; |
| 1010 | |
| 1011 | if (gst_buffer_pool_acquire_buffer (pool->other_pool, &buffer, NULL) != |
| 1012 | GST_FLOW_OK) |
| 1013 | goto other_pool_failed; |
| 1014 | |
| 1015 | if (!gst_aml_v4l2_object_try_import(obj, buffer)) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1016 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1017 | gst_buffer_unref (buffer); |
| 1018 | goto cannot_import; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1019 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1020 | gst_buffer_unref (buffer); |
| 1021 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1022 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1023 | config = gst_buffer_pool_get_config (bpool); |
| 1024 | if (!gst_buffer_pool_config_get_params (config, &caps, &size, &min_buffers, |
| 1025 | &max_buffers)) |
| 1026 | goto wrong_config; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1027 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1028 | min_latency = MAX(GST_AML_V4L2_MIN_BUFFERS, obj->min_buffers); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1029 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1030 | switch (obj->mode) |
| 1031 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1032 | case GST_V4L2_IO_RW: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1033 | can_allocate = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1034 | #ifdef HAVE_LIBV4L2 |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1035 | /* This workaround a unfixable bug in libv4l2 when RW is emulated on top |
| 1036 | * of MMAP. In this case, the first read initialize the queues, but the |
| 1037 | * poll before that will always fail. Doing an empty read, forces the |
| 1038 | * queue to be initialized now. We only do this if we have a streaming |
| 1039 | * driver. */ |
| 1040 | if (obj->device_caps & V4L2_CAP_STREAMING) |
| 1041 | obj->read (obj->video_fd, NULL, 0); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1042 | #endif |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1043 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1044 | case GST_V4L2_IO_DMABUF: |
| 1045 | case GST_V4L2_IO_MMAP: |
| 1046 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1047 | guint count; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1048 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1049 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, MMAP); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1050 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1051 | /* first, lets request buffers, and see how many we can get: */ |
| 1052 | GST_DEBUG_OBJECT (pool, "requesting %d MMAP buffers", min_buffers); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1053 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1054 | count = gst_aml_v4l2_allocator_start(pool->vallocator, min_buffers, |
| 1055 | V4L2_MEMORY_MMAP); |
| 1056 | pool->num_allocated = count; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1057 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1058 | if (count < GST_AML_V4L2_MIN_BUFFERS) |
| 1059 | { |
| 1060 | min_buffers = count; |
| 1061 | goto no_buffers; |
| 1062 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1063 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1064 | /* V4L2 buffer pool are often very limited in the amount of buffers it |
| 1065 | * can offer. The copy_threshold will workaround this limitation by |
| 1066 | * falling back to copy if the pipeline needed more buffers. This also |
| 1067 | * prevent having to do REQBUFS(N)/REQBUFS(0) every time configure is |
| 1068 | * called. */ |
| 1069 | if (count != min_buffers || pool->enable_copy_threshold) |
| 1070 | { |
| 1071 | GST_WARNING_OBJECT (pool, |
| 1072 | "Uncertain or not enough buffers, enabling copy threshold"); |
| 1073 | min_buffers = count; |
| 1074 | copy_threshold = min_latency; |
| 1075 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1076 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1077 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1078 | } |
| 1079 | case GST_V4L2_IO_USERPTR: |
| 1080 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1081 | guint count; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1082 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1083 | can_allocate = |
| 1084 | GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, USERPTR); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1085 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1086 | GST_DEBUG_OBJECT (pool, "requesting %d USERPTR buffers", min_buffers); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1087 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1088 | count = gst_aml_v4l2_allocator_start(pool->vallocator, min_buffers, |
| 1089 | V4L2_MEMORY_USERPTR); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1090 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1091 | /* There is no rational to not get what we asked */ |
| 1092 | if (count < min_buffers) |
| 1093 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1094 | min_buffers = count; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1095 | goto no_buffers; |
| 1096 | } |
| 1097 | |
| 1098 | min_buffers = count; |
| 1099 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1100 | } |
| 1101 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 1102 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1103 | guint count; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1104 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1105 | can_allocate = GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE(pool->vallocator, DMABUF); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1106 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1107 | GST_DEBUG_OBJECT (pool, "requesting %d DMABUF buffers", min_buffers); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1108 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1109 | count = gst_aml_v4l2_allocator_start(pool->vallocator, min_buffers, |
| 1110 | V4L2_MEMORY_DMABUF); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1111 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1112 | /* There is no rational to not get what we asked */ |
| 1113 | if (count < min_buffers) |
| 1114 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1115 | min_buffers = count; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1116 | goto no_buffers; |
| 1117 | } |
| 1118 | |
| 1119 | min_buffers = count; |
| 1120 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1121 | } |
| 1122 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1123 | min_buffers = 0; |
| 1124 | copy_threshold = 0; |
| 1125 | g_assert_not_reached (); |
| 1126 | break; |
| 1127 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1128 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1129 | if (can_allocate) |
| 1130 | max_latency = max_buffers; |
| 1131 | else |
| 1132 | max_latency = min_buffers; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1133 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1134 | pool->size = size; |
| 1135 | pool->copy_threshold = copy_threshold; |
| 1136 | pool->max_latency = max_latency; |
| 1137 | pool->min_latency = min_latency; |
| 1138 | pool->num_queued = 0; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1139 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1140 | if (max_buffers != 0 && max_buffers < min_buffers) |
| 1141 | max_buffers = min_buffers; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1142 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1143 | gst_buffer_pool_config_set_params (config, caps, size, min_buffers, |
| 1144 | max_buffers); |
| 1145 | pclass->set_config (bpool, config); |
| 1146 | gst_structure_free (config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1147 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1148 | /* now, allocate the buffers: */ |
| 1149 | if (!pclass->start (bpool)) |
| 1150 | goto start_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1151 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1152 | if (!V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1153 | { |
| 1154 | if (g_atomic_int_get(&pool->num_queued) < min_buffers) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1155 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1156 | if (obj->old_other_pool || obj->old_old_other_pool) |
| 1157 | GST_DEBUG_OBJECT(pool, "resolution switching flow, need to wait other pool recycle"); |
| 1158 | else |
| 1159 | goto queue_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1160 | } |
| 1161 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1162 | pool->group_released_handler = |
| 1163 | g_signal_connect_swapped (pool->vallocator, "group-released", |
| 1164 | G_CALLBACK(gst_aml_v4l2_buffer_pool_resurrect_buffer), pool); |
| 1165 | ret = gst_aml_v4l2_buffer_pool_streamon(pool); |
| 1166 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1167 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1168 | return ret; |
| 1169 | |
| 1170 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1171 | wrong_config: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1172 | { |
| 1173 | GST_ERROR_OBJECT (pool, "invalid config %" GST_PTR_FORMAT, config); |
| 1174 | gst_structure_free (config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1175 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1176 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1177 | no_buffers: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1178 | { |
| 1179 | GST_ERROR_OBJECT (pool, |
| 1180 | "we received %d buffer from device '%s', we want at least %d", |
| 1181 | min_buffers, obj->videodev, GST_AML_V4L2_MIN_BUFFERS); |
| 1182 | gst_structure_free (config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1183 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1184 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1185 | start_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1186 | { |
| 1187 | GST_ERROR_OBJECT (pool, "allocate failed"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1188 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1189 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1190 | other_pool_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1191 | { |
| 1192 | GST_ERROR_OBJECT (pool, "failed to activate the other pool %" |
| 1193 | GST_PTR_FORMAT, pool->other_pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1194 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1195 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1196 | queue_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1197 | { |
| 1198 | GST_ERROR_OBJECT (pool, "failed to queue buffers into the capture queue"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1199 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1200 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1201 | cannot_import: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1202 | { |
| 1203 | GST_ERROR_OBJECT (pool, "cannot import buffers from downstream pool"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1204 | return FALSE; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1205 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1206 | } |
| 1207 | |
| 1208 | static gboolean |
| 1209 | gst_aml_v4l2_buffer_pool_vallocator_stop(GstAmlV4l2BufferPool *pool) |
| 1210 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1211 | GstAmlV4l2Return vret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1212 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1213 | if (!pool->vallocator) |
| 1214 | return TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1215 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1216 | vret = gst_aml_v4l2_allocator_stop(pool->vallocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1217 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1218 | if (vret == GST_AML_V4L2_BUSY) |
| 1219 | GST_WARNING_OBJECT (pool, "some buffers are still outstanding"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1220 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1221 | return (vret == GST_AML_V4L2_OK); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static gboolean |
| 1225 | gst_aml_v4l2_buffer_pool_stop(GstBufferPool *bpool) |
| 1226 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1227 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 1228 | gboolean ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1229 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1230 | if (pool->orphaned) |
| 1231 | return gst_aml_v4l2_buffer_pool_vallocator_stop(pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1232 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1233 | GST_DEBUG_OBJECT (pool, "stopping pool"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1234 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1235 | if (pool->group_released_handler > 0) |
| 1236 | { |
| 1237 | g_signal_handler_disconnect (pool->vallocator, |
| 1238 | pool->group_released_handler); |
| 1239 | pool->group_released_handler = 0; |
| 1240 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1241 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1242 | gst_aml_v4l2_buffer_pool_streamoff(pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1243 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1244 | ret = GST_BUFFER_POOL_CLASS (parent_class)->stop (bpool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1245 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1246 | if (ret) |
| 1247 | ret = gst_aml_v4l2_buffer_pool_vallocator_stop(pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1248 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1249 | GST_DEBUG_OBJECT (pool, "stopping other_pool"); |
| 1250 | if (pool->other_pool) |
| 1251 | { |
| 1252 | gst_buffer_pool_set_active (pool->other_pool, FALSE); |
| 1253 | gst_object_unref (pool->other_pool); |
| 1254 | pool->other_pool = NULL; |
| 1255 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1256 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1257 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | gboolean |
| 1261 | gst_aml_v4l2_buffer_pool_orphan(GstBufferPool **bpool) |
| 1262 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1263 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(*bpool); |
| 1264 | gboolean ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1265 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1266 | if (!GST_AML_V4L2_ALLOCATOR_CAN_ORPHAN_BUFS(pool->vallocator)) |
| 1267 | return FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1268 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1269 | if (g_getenv("GST_V4L2_FORCE_DRAIN")) |
| 1270 | return FALSE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1271 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1272 | GST_DEBUG_OBJECT (pool, "orphaning pool"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1273 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1274 | gst_buffer_pool_set_active (*bpool, FALSE); |
| 1275 | /* |
| 1276 | * If the buffer pool has outstanding buffers, it will not be stopped |
| 1277 | * by the base class when set inactive. Stop it manually and mark it |
| 1278 | * as orphaned |
| 1279 | */ |
| 1280 | ret = gst_aml_v4l2_buffer_pool_stop(*bpool); |
| 1281 | if (!ret) |
| 1282 | { |
| 1283 | GST_DEBUG_OBJECT(pool, "stop poll fail, try to orphaning allocator"); |
| 1284 | ret = gst_aml_v4l2_allocator_orphan (pool->vallocator); |
| 1285 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1286 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1287 | if (!ret) |
| 1288 | goto orphan_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1289 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1290 | pool->orphaned = TRUE; |
| 1291 | gst_object_unref(*bpool); |
| 1292 | *bpool = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1293 | |
| 1294 | orphan_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1295 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1299 | gst_aml_v4l2_buffer_pool_flush_start (GstBufferPool * bpool) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1300 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1301 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL (bpool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1302 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1303 | GST_DEBUG_OBJECT (pool, "start flushing"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1304 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1305 | gst_poll_set_flushing (pool->poll, TRUE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1306 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1307 | GST_OBJECT_LOCK (pool); |
| 1308 | pool->empty = FALSE; |
| 1309 | g_cond_broadcast (&pool->empty_cond); |
| 1310 | GST_OBJECT_UNLOCK (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1311 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1312 | if (pool->other_pool) |
| 1313 | gst_buffer_pool_set_flushing(pool->other_pool, TRUE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1314 | } |
| 1315 | |
| 1316 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1317 | gst_aml_v4l2_buffer_pool_flush_stop (GstBufferPool * bpool) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1318 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1319 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL (bpool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1320 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1321 | GST_DEBUG_OBJECT (pool, "stop flushing"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1322 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1323 | if (pool->other_pool) |
| 1324 | gst_buffer_pool_set_flushing (pool->other_pool, FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1325 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1326 | gst_poll_set_flushing (pool->poll, FALSE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1327 | } |
| 1328 | |
| 1329 | static GstFlowReturn |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1330 | gst_aml_v4l2_buffer_pool_poll (GstAmlV4l2BufferPool * pool, gboolean wait) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1331 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1332 | gint ret; |
| 1333 | GstClockTime timeout; |
| 1334 | gint try_num = 0; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1335 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1336 | if (wait) |
| 1337 | timeout = GST_CLOCK_TIME_NONE; |
| 1338 | else |
| 1339 | timeout = 0; |
| 1340 | |
| 1341 | /* In RW mode there is no queue, hence no need to wait while the queue is |
| 1342 | * empty */ |
| 1343 | |
| 1344 | if ((pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && |
| 1345 | pool->obj->mode == GST_V4L2_IO_DMABUF_IMPORT) |
| 1346 | { |
| 1347 | GST_TRACE_OBJECT(pool, "CAPTURE DMA don't quit when empty buf"); |
| 1348 | timeout = 5*1000*1000; //5ms |
| 1349 | } |
| 1350 | else |
| 1351 | { |
| 1352 | if (pool->obj->mode != GST_V4L2_IO_RW) |
| 1353 | { |
| 1354 | GST_OBJECT_LOCK(pool); |
| 1355 | |
| 1356 | if (!wait && pool->empty) |
| 1357 | { |
| 1358 | GST_OBJECT_UNLOCK(pool); |
| 1359 | goto no_buffers; |
| 1360 | } |
| 1361 | |
| 1362 | while (pool->empty) |
| 1363 | g_cond_wait(&pool->empty_cond, GST_OBJECT_GET_LOCK(pool)); |
| 1364 | |
| 1365 | GST_OBJECT_UNLOCK(pool); |
| 1366 | } |
| 1367 | } |
| 1368 | |
| 1369 | if (!pool->can_poll_device) |
| 1370 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1371 | if (wait) |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1372 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1373 | else |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1374 | goto no_buffers; |
| 1375 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1376 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1377 | GST_TRACE_OBJECT(pool, "polling device"); |
xuesong.jiang | 93f2dcb | 2022-07-15 14:24:55 +0800 | [diff] [blame] | 1378 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1379 | again: |
| 1380 | ret = gst_poll_wait (pool->poll, timeout); |
| 1381 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 1382 | GST_TRACE_OBJECT(pool, "amlmodbuf poll timeout:%lld, ret:%d, errno:%d", timeout, ret, errno); |
| 1383 | #endif |
| 1384 | if (G_UNLIKELY(ret < 0)) |
| 1385 | { |
| 1386 | switch (errno) |
| 1387 | { |
| 1388 | case EBUSY: |
| 1389 | goto stopped; |
| 1390 | case EAGAIN: |
| 1391 | case EINTR: |
| 1392 | goto again; |
| 1393 | case ENXIO: |
| 1394 | GST_WARNING_OBJECT (pool, |
| 1395 | "v4l2 device doesn't support polling. Disabling" |
| 1396 | " using libv4l2 in this case may cause deadlocks"); |
| 1397 | pool->can_poll_device = FALSE; |
| 1398 | goto done; |
| 1399 | default: |
| 1400 | goto select_error; |
| 1401 | } |
| 1402 | } |
| 1403 | |
| 1404 | if (gst_poll_fd_has_error (pool->poll, &pool->pollfd)) |
| 1405 | { |
| 1406 | //if v4l2 don't have capture buffer, we will poll a error,it cause v4l2dec loop thread exit |
| 1407 | //so we should wait capture buffer release and queue it to v4l2,after this,we try poll again |
xuesong.jiang | 93f2dcb | 2022-07-15 14:24:55 +0800 | [diff] [blame] | 1408 | if ((pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && |
| 1409 | pool->obj->mode == GST_V4L2_IO_DMABUF_IMPORT) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1410 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1411 | if (pool->num_queued == 0) |
| 1412 | { |
| 1413 | ret = 0; |
| 1414 | GST_TRACE_OBJECT(pool,"ignore error when no capture buffer on v4l2"); |
| 1415 | g_usleep(4000); |
| 1416 | goto wait_buffer_queue; |
| 1417 | } |
xuesong.jiang | 93f2dcb | 2022-07-15 14:24:55 +0800 | [diff] [blame] | 1418 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1419 | goto select_error; |
| 1420 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1421 | |
fei.deng | 08cb2dc | 2023-11-08 04:21:41 +0000 | [diff] [blame] | 1422 | wait_buffer_queue: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1423 | if (ret == 0) |
| 1424 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1425 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1426 | if ((pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE || pool->obj->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) && |
| 1427 | pool->obj->mode == GST_V4L2_IO_DMABUF_IMPORT) |
| 1428 | { |
| 1429 | GST_TRACE_OBJECT(pool, "amlmodbuf can't get buffer in capture obj dmaimport mode, try release buf from other pool"); |
| 1430 | gst_aml_v4l2_buffer_pool_dump_stat(pool, GST_DUMP_CAPTURE_BP_STAT_FILENAME, try_num++); |
| 1431 | gst_aml_v4l2_buffer_pool_release_buffer_aml_patch((GstBufferPool *)pool); |
| 1432 | goto again; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1433 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1434 | else |
| 1435 | #endif |
| 1436 | goto no_buffers; |
| 1437 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1438 | |
| 1439 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1440 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1441 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1442 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1443 | stopped: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1444 | { |
| 1445 | GST_DEBUG_OBJECT (pool, "stop called"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1446 | return GST_FLOW_FLUSHING; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1447 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1448 | select_error: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1449 | { |
| 1450 | GST_ELEMENT_ERROR (pool->obj->element, RESOURCE, READ, (NULL), |
| 1451 | ("poll error %d: %s (%d)", ret, g_strerror (errno), errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1452 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1453 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1454 | no_buffers: |
| 1455 | return GST_FLOW_CUSTOM_SUCCESS; |
| 1456 | } |
| 1457 | |
| 1458 | static GstFlowReturn |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1459 | gst_aml_v4l2_buffer_pool_qbuf (GstAmlV4l2BufferPool * pool, GstBuffer * buf, |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1460 | GstAmlV4l2MemoryGroup *group) |
| 1461 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1462 | const GstAmlV4l2Object *obj = pool->obj; |
| 1463 | GstClockTime timestamp; |
| 1464 | gint index; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1465 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1466 | index = group->buffer.index; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1467 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1468 | if (pool->buffers[index] != NULL) |
| 1469 | goto already_queued; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1470 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1471 | if (V4L2_TYPE_IS_OUTPUT (obj->type)) |
| 1472 | { |
| 1473 | enum v4l2_field field; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1474 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1475 | /* Except when field is set to alternate, buffer field is the same as |
| 1476 | * the one defined in format */ |
| 1477 | if (V4L2_TYPE_IS_MULTIPLANAR (obj->type)) |
| 1478 | field = obj->format.fmt.pix_mp.field; |
zengliang.li | 32cb11e | 2022-11-24 12:10:26 +0800 | [diff] [blame] | 1479 | else |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1480 | field = obj->format.fmt.pix.field; |
| 1481 | |
| 1482 | |
| 1483 | /* NB: At this moment, we can't have alternate mode because it not handled |
| 1484 | * yet */ |
| 1485 | if (field == V4L2_FIELD_ALTERNATE) |
zengliang.li | 32cb11e | 2022-11-24 12:10:26 +0800 | [diff] [blame] | 1486 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1487 | if (GST_BUFFER_FLAG_IS_SET(buf, GST_VIDEO_FRAME_FLAG_TFF)) |
| 1488 | field = V4L2_FIELD_TOP; |
| 1489 | else |
| 1490 | field = V4L2_FIELD_BOTTOM; |
zengliang.li | 32cb11e | 2022-11-24 12:10:26 +0800 | [diff] [blame] | 1491 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1492 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1493 | group->buffer.field = field; |
| 1494 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1495 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1496 | if (GST_BUFFER_TIMESTAMP_IS_VALID (buf)) |
| 1497 | { |
| 1498 | timestamp = GST_BUFFER_TIMESTAMP(buf); |
| 1499 | GST_TIME_TO_TIMEVAL (timestamp, group->buffer.timestamp); |
| 1500 | } |
| 1501 | else |
| 1502 | { |
| 1503 | group->buffer.timestamp.tv_sec = -1; |
| 1504 | group->buffer.timestamp.tv_usec = 0; |
| 1505 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1506 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1507 | GST_OBJECT_LOCK (pool); |
| 1508 | g_atomic_int_inc (&pool->num_queued); |
| 1509 | pool->buffers[index] = buf; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1510 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1511 | if (!gst_aml_v4l2_allocator_qbuf (pool->vallocator, group)) |
| 1512 | goto queue_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1513 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1514 | if (!V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1515 | { |
| 1516 | gst_aml_v4l2_buffer_pool_dump_stat(pool, GST_DUMP_CAPTURE_BP_STAT_FILENAME, 0); |
| 1517 | } |
| 1518 | |
fei.deng | e8c6604 | 2024-09-26 14:12:39 +0800 | [diff] [blame] | 1519 | GST_DEBUG_OBJECT (pool, "queued idx:%d num:%d",index, pool->num_queued); |
| 1520 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1521 | pool->empty = FALSE; |
| 1522 | g_cond_signal (&pool->empty_cond); |
| 1523 | GST_OBJECT_UNLOCK (pool); |
| 1524 | |
| 1525 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1526 | |
| 1527 | already_queued: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1528 | { |
| 1529 | GST_ERROR_OBJECT (pool, "the buffer %i was already queued", index); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1530 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1531 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1532 | queue_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1533 | { |
| 1534 | GST_ERROR_OBJECT (pool, "could not queue a buffer %i", index); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1535 | /* Mark broken buffer to the allocator */ |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1536 | GST_BUFFER_FLAG_SET (buf, GST_BUFFER_FLAG_TAG_MEMORY); |
| 1537 | g_atomic_int_add (&pool->num_queued, -1); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1538 | pool->buffers[index] = NULL; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1539 | GST_OBJECT_UNLOCK (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1540 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1541 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static GstFlowReturn |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1545 | gst_aml_v4l2_buffer_pool_dqevent (GstAmlV4l2BufferPool * pool) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1546 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1547 | GstAmlV4l2Object *v4l2object = pool->obj; |
| 1548 | struct v4l2_event evt; |
| 1549 | GstFlowReturn ret = GST_AML_V4L2_FLOW_UNKNOWN_EVENT; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1550 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1551 | memset (&evt, 0x00, sizeof (struct v4l2_event)); |
| 1552 | if (v4l2object->ioctl (pool->video_fd, VIDIOC_DQEVENT, &evt) < 0) |
| 1553 | goto dqevent_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1554 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1555 | switch (evt.type) |
| 1556 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1557 | case V4L2_EVENT_SOURCE_CHANGE: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1558 | if (evt.u.src_change.changes & V4L2_EVENT_SRC_CH_RESOLUTION) |
| 1559 | ret = GST_AML_V4L2_FLOW_SOURCE_CHANGE; |
| 1560 | else |
| 1561 | { |
| 1562 | GST_WARNING_OBJECT (pool, "Unknown source change 0x%x - skipped", evt.u.src_change.changes); |
bo.xiao | b6afda5 | 2024-08-02 16:08:30 +0800 | [diff] [blame] | 1563 | ret = GST_AML_V4L2_FLOW_UNKNOWN_EVENT; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1564 | } |
| 1565 | break; |
| 1566 | case V4L2_EVENT_EOS: |
| 1567 | ret = GST_AML_V4L2_FLOW_LAST_BUFFER; |
| 1568 | break; |
| 1569 | case V4L2_EVENT_PRIVATE_EXT_REPORT_DECINFO: |
| 1570 | if (evt.id == AML_DECINFO_EVENT_CC) |
| 1571 | { |
| 1572 | struct v4l2_ext_control control; |
| 1573 | struct v4l2_ext_controls ctrls; |
| 1574 | struct vdec_common_s vdec_comm; |
| 1575 | char cc_data[MAX_CC_LEN] = {0}; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1576 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1577 | memset(&ctrls, 0, sizeof(ctrls)); |
| 1578 | memset(&control, 0, sizeof(control)); |
| 1579 | memset(&vdec_comm, 0, sizeof(vdec_comm)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1580 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1581 | vdec_comm.type = AML_DECINFO_GET_CC_TYPE; |
| 1582 | vdec_comm.u.usd_param.data = (void *)cc_data; |
| 1583 | vdec_comm.u.usd_param.data_size = MAX_CC_LEN; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1584 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1585 | control.ptr = &vdec_comm; |
| 1586 | control.id = AML_V4L2_GET_DECINFO_SET; |
| 1587 | control.size = sizeof(struct vdec_common_s); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1588 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1589 | ctrls.count = 1; |
| 1590 | ctrls.controls = &control; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1591 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1592 | if (v4l2object->ioctl (pool->video_fd, VIDIOC_S_EXT_CTRLS, &ctrls) < 0) |
| 1593 | { |
| 1594 | GST_DEBUG("VIDIOC_S_EXT_CTRLS fail"); |
| 1595 | } |
| 1596 | else |
| 1597 | { |
| 1598 | if (v4l2object->ioctl (pool->video_fd, VIDIOC_G_EXT_CTRLS, &ctrls) == 0) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1599 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1600 | GstBuffer *cc_buffer = gst_buffer_new (); |
| 1601 | GstMemory *mem = gst_allocator_alloc (NULL, MAX_CC_LEN, NULL); |
| 1602 | gst_buffer_insert_memory (cc_buffer, -1, mem); |
| 1603 | gsize cc_size = gst_buffer_fill (cc_buffer, 0, vdec_comm.u.usd_param.data, vdec_comm.u.usd_param.data_size); |
| 1604 | GST_DEBUG("copy cc data size:%d",cc_size); |
| 1605 | cc_buffer->pts = vdec_comm.u.usd_param.meta_data.timestamp; |
| 1606 | pool->cc_buffer_list = g_list_append (pool->cc_buffer_list,cc_buffer); |
| 1607 | GST_DEBUG("cc_buffer_list size:%d",g_list_length(pool->cc_buffer_list)); |
| 1608 | #if 0 |
| 1609 | //dump decoder metadata |
| 1610 | GST_DEBUG("cc pack pts:%lld",vdec_comm.u.usd_param.meta_data.timestamp); |
| 1611 | int fd=open("/data/test/cc0.data",O_RDWR |O_CREAT|O_APPEND,0777); |
| 1612 | write(fd,vdec_comm.u.usd_param.data,vdec_comm.u.usd_param.data_size); |
| 1613 | close(fd); |
| 1614 | #endif |
hanghang.luo | b216ac4 | 2023-05-12 02:56:54 +0000 | [diff] [blame] | 1615 | } |
| 1616 | else |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1617 | { |
| 1618 | GST_DEBUG("VIDIOC_G_EXT_CTRLS fail"); |
| 1619 | } |
| 1620 | } |
| 1621 | ret = GST_AML_V4L2_FLOW_CC_DATA; |
| 1622 | } |
| 1623 | else |
| 1624 | { |
| 1625 | GST_WARNING_OBJECT (pool, "Unknown DECINFO 0x%x - skipped", evt.id); |
| 1626 | ret = GST_AML_V4L2_FLOW_UNKNOWN_EVENT; |
| 1627 | } |
| 1628 | break; |
| 1629 | case V4L2_EVENT_PRIVATE_EXT_REPORT_ERROR_FRAME: |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1630 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1631 | guint64 pts = 0; |
| 1632 | memcpy(&pts, &(evt.u.data[0]), sizeof(guint64)); |
| 1633 | v4l2object->num_error_frames += 1; |
| 1634 | v4l2object->error_frame_pts = pts; |
| 1635 | GST_WARNING_OBJECT (pool, "decoding video frame error,pts:%" GST_TIME_FORMAT ",total %d", |
| 1636 | GST_TIME_ARGS(pts),v4l2object->num_error_frames); |
| 1637 | ret = GST_AML_V4L2_FLOW_DECODING_ERROR; |
| 1638 | } break; |
| 1639 | default: |
| 1640 | GST_WARNING_OBJECT (pool, "Unknown evt.type 0x%x - skipped", evt.type); |
| 1641 | ret = GST_AML_V4L2_FLOW_UNKNOWN_EVENT; |
| 1642 | break; |
| 1643 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1644 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1645 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1646 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1647 | /* ERRORS */ |
| 1648 | dqevent_failed: |
| 1649 | { |
| 1650 | return GST_FLOW_ERROR; |
| 1651 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1652 | } |
| 1653 | |
| 1654 | static GstFlowReturn |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1655 | gst_aml_v4l2_buffer_pool_dqbuf (GstAmlV4l2BufferPool * pool, GstBuffer ** buffer, |
| 1656 | gboolean wait) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1657 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1658 | GstFlowReturn res; |
| 1659 | GstBuffer *outbuf = NULL; |
| 1660 | GstAmlV4l2Object *obj = pool->obj; |
| 1661 | GstClockTime timestamp; |
| 1662 | GstAmlV4l2MemoryGroup *group; |
| 1663 | GstVideoMeta *vmeta; |
| 1664 | gsize size; |
| 1665 | gint i; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1666 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1667 | res = gst_aml_v4l2_allocator_dqbuf (pool->vallocator, &group); |
| 1668 | if (res == GST_FLOW_EOS) |
| 1669 | goto eos; |
| 1670 | if (res != GST_FLOW_OK) |
| 1671 | goto dqbuf_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1672 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1673 | /* get our GstBuffer with that index from the pool, if the buffer was |
| 1674 | * outstanding we have a serious problem. |
| 1675 | */ |
| 1676 | outbuf = pool->buffers[group->buffer.index]; |
| 1677 | if (outbuf == NULL) |
| 1678 | goto no_buffer; |
| 1679 | |
| 1680 | /* mark the buffer outstanding */ |
| 1681 | pool->buffers[group->buffer.index] = NULL; |
| 1682 | if (g_atomic_int_dec_and_test (&pool->num_queued)) |
| 1683 | { |
| 1684 | GST_OBJECT_LOCK (pool); |
| 1685 | pool->empty = TRUE; |
| 1686 | GST_OBJECT_UNLOCK (pool); |
| 1687 | } |
| 1688 | |
| 1689 | if (-1 == group->buffer.timestamp.tv_sec) |
| 1690 | timestamp = GST_CLOCK_TIME_NONE; |
| 1691 | else |
| 1692 | timestamp = GST_TIMEVAL_TO_TIME (group->buffer.timestamp); |
| 1693 | |
| 1694 | size = 0; |
| 1695 | vmeta = gst_buffer_get_video_meta (outbuf); |
| 1696 | for (i = 0; i < group->n_mem; i++) |
| 1697 | { |
| 1698 | GST_LOG_OBJECT(pool, |
| 1699 | "dequeued buffer %p seq:%d (ix=%d), mem %p used %d, plane=%d, flags %08x, ts %" GST_TIME_FORMAT ", pool-queued=%d, buffer=%p", outbuf, |
| 1700 | group->buffer.sequence, group->buffer.index, group->mem[i], |
| 1701 | group->planes[i].bytesused, i, group->buffer.flags, |
| 1702 | GST_TIME_ARGS(timestamp), pool->num_queued, outbuf); |
| 1703 | |
| 1704 | if (vmeta) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1705 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1706 | vmeta->offset[i] = size; |
| 1707 | size += gst_memory_get_sizes (group->mem[i], NULL, NULL); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1708 | } |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1709 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1710 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1711 | /* Ignore timestamp and field for OUTPUT device */ |
| 1712 | if (V4L2_TYPE_IS_OUTPUT (obj->type)) |
| 1713 | goto done; |
| 1714 | |
| 1715 | /* Check for driver bug in reporting feild */ |
| 1716 | if (group->buffer.field == V4L2_FIELD_ANY) |
| 1717 | { |
| 1718 | /* Only warn once to avoid the spamming */ |
| 1719 | #ifndef GST_DISABLE_GST_DEBUG |
| 1720 | if (!pool->has_warned_on_buggy_field) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1721 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1722 | pool->has_warned_on_buggy_field = TRUE; |
| 1723 | GST_WARNING_OBJECT (pool, |
| 1724 | "Driver should never set v4l2_buffer.field to ANY"); |
| 1725 | } |
| 1726 | #endif |
| 1727 | |
| 1728 | /* Use the value from the format (works for UVC bug) */ |
| 1729 | group->buffer.field = obj->format.fmt.pix.field; |
| 1730 | |
| 1731 | /* If driver also has buggy S_FMT, assume progressive */ |
| 1732 | if (group->buffer.field == V4L2_FIELD_ANY) |
| 1733 | { |
| 1734 | #ifndef GST_DISABLE_GST_DEBUG |
| 1735 | if (!pool->has_warned_on_buggy_field) |
| 1736 | { |
| 1737 | pool->has_warned_on_buggy_field = TRUE; |
| 1738 | GST_WARNING_OBJECT (pool, |
| 1739 | "Driver should never set v4l2_format.pix.field to ANY"); |
| 1740 | } |
| 1741 | #endif |
| 1742 | |
| 1743 | group->buffer.field = V4L2_FIELD_NONE; |
| 1744 | } |
| 1745 | } |
| 1746 | |
| 1747 | /* set top/bottom field first if v4l2_buffer has the information */ |
| 1748 | switch (group->buffer.field) |
| 1749 | { |
| 1750 | case V4L2_FIELD_NONE: |
| 1751 | GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1752 | GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1753 | break; |
| 1754 | case V4L2_FIELD_INTERLACED_TB: |
| 1755 | GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1756 | GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1757 | break; |
| 1758 | case V4L2_FIELD_INTERLACED_BT: |
| 1759 | GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1760 | GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1761 | break; |
| 1762 | case V4L2_FIELD_INTERLACED: |
| 1763 | GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1764 | if (obj->tv_norm == V4L2_STD_NTSC_M || |
| 1765 | obj->tv_norm == V4L2_STD_NTSC_M_JP || |
| 1766 | obj->tv_norm == V4L2_STD_NTSC_M_KR) |
| 1767 | { |
| 1768 | GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1769 | } |
| 1770 | else |
| 1771 | { |
| 1772 | GST_BUFFER_FLAG_SET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1773 | } |
| 1774 | break; |
| 1775 | default: |
| 1776 | GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_INTERLACED); |
| 1777 | GST_BUFFER_FLAG_UNSET (outbuf, GST_VIDEO_BUFFER_FLAG_TFF); |
| 1778 | GST_FIXME_OBJECT (pool, |
| 1779 | "Unhandled enum v4l2_field %d - treating as progressive", |
| 1780 | group->buffer.field); |
| 1781 | break; |
| 1782 | } |
| 1783 | |
| 1784 | if (GST_VIDEO_INFO_FORMAT (&obj->info) == GST_VIDEO_FORMAT_ENCODED) |
| 1785 | { |
| 1786 | if ((group->buffer.flags & V4L2_BUF_FLAG_KEYFRAME) || |
| 1787 | GST_AML_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_MJPEG || |
| 1788 | GST_AML_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_JPEG || |
| 1789 | GST_AML_V4L2_PIXELFORMAT (obj) == V4L2_PIX_FMT_PJPG) |
| 1790 | GST_BUFFER_FLAG_UNSET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT); |
| 1791 | else |
| 1792 | GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_DELTA_UNIT); |
| 1793 | } |
| 1794 | |
| 1795 | if (group->buffer.flags & V4L2_BUF_FLAG_ERROR) |
| 1796 | GST_BUFFER_FLAG_SET (outbuf, GST_BUFFER_FLAG_CORRUPTED); |
| 1797 | |
| 1798 | GST_BUFFER_TIMESTAMP (outbuf) = timestamp; |
| 1799 | GST_BUFFER_OFFSET (outbuf) = group->buffer.sequence; |
| 1800 | GST_BUFFER_OFFSET_END (outbuf) = group->buffer.sequence + 1; |
| 1801 | |
| 1802 | done: |
| 1803 | *buffer = outbuf; |
| 1804 | if ((group->buffer.flags & V4L2_BUF_FLAG_LAST) &&(group->buffer.bytesused == 0)) |
| 1805 | { |
| 1806 | GST_DEBUG_OBJECT (pool,"dequeued empty buffer"); |
| 1807 | GST_BUFFER_FLAG_SET(*buffer, GST_AML_V4L2_BUFFER_FLAG_LAST_EMPTY); |
| 1808 | } |
| 1809 | |
| 1810 | if (!V4L2_TYPE_IS_OUTPUT(obj->type)) |
| 1811 | { |
| 1812 | gst_aml_v4l2_buffer_pool_dump_stat(pool, GST_DUMP_CAPTURE_BP_STAT_FILENAME, 0); |
| 1813 | } |
| 1814 | |
| 1815 | return res; |
| 1816 | |
| 1817 | /* ERRORS */ |
| 1818 | eos: |
| 1819 | { |
| 1820 | return GST_FLOW_EOS; |
| 1821 | } |
| 1822 | dqbuf_failed: |
| 1823 | { |
| 1824 | return GST_FLOW_ERROR; |
| 1825 | } |
| 1826 | no_buffer: |
| 1827 | { |
| 1828 | GST_ERROR_OBJECT (pool, "No free buffer found in the pool at index %d.", |
| 1829 | group->buffer.index); |
| 1830 | return GST_FLOW_ERROR; |
| 1831 | } |
| 1832 | } |
| 1833 | |
| 1834 | static GstFlowReturn |
| 1835 | gst_aml_v4l2_buffer_pool_dequeue (GstAmlV4l2BufferPool * pool, GstBuffer ** buffer, |
| 1836 | gboolean wait) |
| 1837 | { |
| 1838 | GstFlowReturn res; |
| 1839 | GstAmlV4l2Object *obj = pool->obj; |
| 1840 | |
| 1841 | if ((res = gst_aml_v4l2_buffer_pool_poll (pool, wait)) != GST_FLOW_OK) |
| 1842 | goto poll_failed; |
| 1843 | |
| 1844 | if (obj->can_wait_event && gst_poll_fd_can_read_pri (pool->poll, &pool->pollfd)) |
| 1845 | { |
| 1846 | GstFlowReturn res_event = gst_aml_v4l2_buffer_pool_dqevent(pool); |
| 1847 | if (res_event != GST_FLOW_OK) |
| 1848 | { |
| 1849 | /* when drive V4l2 receive cmd_stop, it will finish current decoding frame, then creat |
| 1850 | * a EOS event and a empty buff. if gstreamer dq EOS event first ,the last frame will be drop, |
| 1851 | * this a question |
| 1852 | */ |
| 1853 | if (res_event == GST_AML_V4L2_FLOW_LAST_BUFFER) |
| 1854 | { |
| 1855 | GST_DEBUG_OBJECT(pool," reiceive EOS event, drop it"); |
| 1856 | } |
| 1857 | else |
| 1858 | return res_event; |
| 1859 | } |
| 1860 | } |
| 1861 | if (res == GST_FLOW_CUSTOM_SUCCESS) |
| 1862 | { |
| 1863 | GST_LOG_OBJECT (pool, "nothing to dequeue"); |
| 1864 | *buffer = NULL; |
| 1865 | return res; |
| 1866 | } |
| 1867 | |
| 1868 | GST_LOG_OBJECT (pool, "dequeueing a buffer"); |
| 1869 | return gst_aml_v4l2_buffer_pool_dqbuf(pool, buffer, wait); |
| 1870 | |
| 1871 | /* ERRORS */ |
| 1872 | poll_failed: |
| 1873 | { |
| 1874 | GST_DEBUG_OBJECT (pool, "poll error %s", gst_flow_get_name (res)); |
| 1875 | return res; |
| 1876 | } |
| 1877 | } |
| 1878 | |
| 1879 | static GstFlowReturn |
| 1880 | gst_aml_v4l2_buffer_pool_acquire_buffer (GstBufferPool * bpool, GstBuffer ** buffer, |
| 1881 | GstBufferPoolAcquireParams * params) |
| 1882 | { |
| 1883 | GstFlowReturn ret; |
| 1884 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL (bpool); |
| 1885 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS (parent_class); |
| 1886 | GstAmlV4l2Object *obj = pool->obj; |
| 1887 | |
| 1888 | GST_DEBUG_OBJECT (pool, "acquire"); |
| 1889 | |
| 1890 | /* If this is being called to resurrect a lost buffer */ |
| 1891 | if (params && params->flags & GST_V4L2_BUFFER_POOL_ACQUIRE_FLAG_RESURRECT) |
| 1892 | { |
| 1893 | ret = pclass->acquire_buffer (bpool, buffer, params); |
| 1894 | goto done; |
| 1895 | } |
| 1896 | |
| 1897 | switch (obj->type) |
| 1898 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1899 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 1900 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1901 | /* capture, This function should return a buffer with new captured data */ |
| 1902 | switch (obj->mode) |
| 1903 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1904 | case GST_V4L2_IO_RW: |
| 1905 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1906 | /* take empty buffer from the pool */ |
| 1907 | ret = pclass->acquire_buffer (bpool, buffer, params); |
| 1908 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1909 | } |
| 1910 | case GST_V4L2_IO_DMABUF: |
| 1911 | case GST_V4L2_IO_MMAP: |
| 1912 | case GST_V4L2_IO_USERPTR: |
xuesong.jiang | 3df0f7b | 2022-11-29 14:52:45 +0800 | [diff] [blame] | 1913 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1914 | /* just dequeue a buffer, we basically use the queue of v4l2 as the |
| 1915 | * storage for our buffers. This function does poll first so we can |
| 1916 | * interrupt it fine. */ |
| 1917 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, buffer, TRUE); |
xuesong.jiang | 3df0f7b | 2022-11-29 14:52:45 +0800 | [diff] [blame] | 1918 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1919 | break; |
xuesong.jiang | 3df0f7b | 2022-11-29 14:52:45 +0800 | [diff] [blame] | 1920 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1921 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 1922 | { |
| 1923 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1924 | GST_DEBUG_OBJECT(pool, "amlmodbuf return free buf before acquire buf"); |
| 1925 | gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(bpool); |
| 1926 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, buffer, FALSE); |
| 1927 | GST_DEBUG_OBJECT(pool, "amlmodbuf dequeue return ret:%d", ret); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1928 | #else |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1929 | /* just dequeue a buffer, we basically use the queue of v4l2 as the |
| 1930 | * storage for our buffers. This function does poll first so we can |
| 1931 | * interrupt it fine. */ |
| 1932 | ret = gst_aml_v4l2_buffer_pool_dequeue(pool, buffer, TRUE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1933 | #endif |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1934 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1935 | } |
| 1936 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1937 | ret = GST_FLOW_ERROR; |
| 1938 | g_assert_not_reached (); |
| 1939 | break; |
| 1940 | } |
| 1941 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1942 | |
| 1943 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 1944 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1945 | /* playback, This function should return an empty buffer */ |
| 1946 | switch (obj->mode) |
| 1947 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1948 | case GST_V4L2_IO_RW: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1949 | /* get an empty buffer */ |
| 1950 | ret = pclass->acquire_buffer (bpool, buffer, params); |
| 1951 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1952 | |
| 1953 | case GST_V4L2_IO_MMAP: |
| 1954 | case GST_V4L2_IO_DMABUF: |
| 1955 | case GST_V4L2_IO_USERPTR: |
| 1956 | case GST_V4L2_IO_DMABUF_IMPORT: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1957 | /* get a free unqueued buffer */ |
| 1958 | ret = pclass->acquire_buffer (bpool, buffer, params); |
| 1959 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1960 | |
| 1961 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1962 | ret = GST_FLOW_ERROR; |
| 1963 | g_assert_not_reached (); |
| 1964 | break; |
| 1965 | } |
| 1966 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1967 | |
| 1968 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1969 | ret = GST_FLOW_ERROR; |
| 1970 | g_assert_not_reached (); |
| 1971 | break; |
| 1972 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1973 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1974 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1978 | gst_aml_v4l2_buffer_pool_release_buffer (GstBufferPool * bpool, GstBuffer * buffer) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1979 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1980 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL (bpool); |
| 1981 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 1982 | GstAmlV4l2Object *obj = pool->obj; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1983 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1984 | GST_DEBUG_OBJECT(pool, "release buffer %p", buffer); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1985 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1986 | /* If the buffer's pool has been orphaned, dispose of it so that |
| 1987 | * the pool resources can be freed */ |
| 1988 | if (pool->orphaned) |
| 1989 | { |
| 1990 | GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY); |
| 1991 | pclass->release_buffer(bpool, buffer); |
| 1992 | return; |
| 1993 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 1994 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 1995 | switch (obj->type) |
| 1996 | { |
| 1997 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 1998 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: |
| 1999 | /* capture, put the buffer back in the queue so that we can refill it |
| 2000 | * later. */ |
| 2001 | switch (obj->mode) |
| 2002 | { |
| 2003 | case GST_V4L2_IO_RW: |
| 2004 | /* release back in the pool */ |
| 2005 | pclass->release_buffer(bpool, buffer); |
| 2006 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2007 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2008 | case GST_V4L2_IO_DMABUF: |
| 2009 | case GST_V4L2_IO_MMAP: |
| 2010 | case GST_V4L2_IO_USERPTR: |
| 2011 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 2012 | { |
| 2013 | GstAmlV4l2MemoryGroup *group; |
| 2014 | if (gst_aml_v4l2_is_buffer_valid(buffer, &group)) |
| 2015 | { |
| 2016 | GstFlowReturn ret = GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2017 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2018 | gst_aml_v4l2_allocator_reset_group(pool->vallocator, group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2019 | |
| 2020 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2021 | GST_DEBUG_OBJECT(pool, "amlmodbuf trace in add flow with buf:%p index:%d", buffer, group->buffer.index); |
| 2022 | pool->read_to_free_bufs[group->buffer.index] = buffer; |
| 2023 | pool->ready_to_free_buf_num++; |
| 2024 | if (gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(bpool)) |
| 2025 | { |
| 2026 | GST_DEBUG_OBJECT(pool, "amlmodbuf execute aml code logic, skip the following flow"); |
| 2027 | return; |
| 2028 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2029 | #endif |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2030 | /* queue back in the device */ |
| 2031 | if (pool->other_pool) |
| 2032 | ret = gst_aml_v4l2_buffer_pool_prepare_buffer(pool, buffer, NULL); |
| 2033 | if (ret != GST_FLOW_OK || |
| 2034 | gst_aml_v4l2_buffer_pool_qbuf(pool, buffer, group) != GST_FLOW_OK) |
| 2035 | pclass->release_buffer(bpool, buffer); |
| 2036 | } |
| 2037 | else |
| 2038 | { |
| 2039 | /* Simply release invalide/modified buffer, the allocator will |
| 2040 | * give it back later */ |
| 2041 | GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY); |
| 2042 | pclass->release_buffer(bpool, buffer); |
| 2043 | } |
| 2044 | break; |
| 2045 | } |
| 2046 | default: |
| 2047 | g_assert_not_reached(); |
| 2048 | break; |
| 2049 | } |
| 2050 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2051 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2052 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 2053 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: |
| 2054 | switch (obj->mode) |
| 2055 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2056 | case GST_V4L2_IO_RW: |
| 2057 | /* release back in the pool */ |
| 2058 | pclass->release_buffer(bpool, buffer); |
| 2059 | break; |
| 2060 | |
| 2061 | case GST_V4L2_IO_MMAP: |
| 2062 | case GST_V4L2_IO_DMABUF: |
| 2063 | case GST_V4L2_IO_USERPTR: |
| 2064 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 2065 | { |
| 2066 | GstAmlV4l2MemoryGroup *group; |
| 2067 | guint index; |
| 2068 | |
| 2069 | if (!gst_aml_v4l2_is_buffer_valid(buffer, &group)) |
| 2070 | { |
| 2071 | /* Simply release invalide/modified buffer, the allocator will |
| 2072 | * give it back later */ |
| 2073 | GST_BUFFER_FLAG_SET(buffer, GST_BUFFER_FLAG_TAG_MEMORY); |
| 2074 | pclass->release_buffer(bpool, buffer); |
| 2075 | break; |
| 2076 | } |
| 2077 | |
| 2078 | index = group->buffer.index; |
| 2079 | |
| 2080 | if (pool->buffers[index] == NULL) |
| 2081 | { |
| 2082 | GST_LOG_OBJECT(pool, "buffer %u not queued, putting on free list", |
| 2083 | index); |
| 2084 | |
| 2085 | /* Remove qdata, this will unmap any map data in userptr */ |
| 2086 | gst_mini_object_set_qdata(GST_MINI_OBJECT(buffer), |
| 2087 | GST_AML_V4L2_IMPORT_QUARK, NULL, NULL); |
| 2088 | |
| 2089 | /* reset to default size */ |
| 2090 | gst_aml_v4l2_allocator_reset_group(pool->vallocator, group); |
| 2091 | |
| 2092 | /* playback, put the buffer back in the queue to refill later. */ |
| 2093 | pclass->release_buffer(bpool, buffer); |
| 2094 | } |
| 2095 | else |
| 2096 | { |
| 2097 | /* the buffer is queued in the device but maybe not played yet. We just |
| 2098 | * leave it there and not make it available for future calls to acquire |
| 2099 | * for now. The buffer will be dequeued and reused later. */ |
| 2100 | GST_LOG_OBJECT(pool, "buffer %u is queued", index); |
| 2101 | } |
| 2102 | break; |
| 2103 | } |
| 2104 | |
| 2105 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2106 | g_assert_not_reached (); |
| 2107 | break; |
| 2108 | } |
| 2109 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2110 | |
| 2111 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2112 | g_assert_not_reached (); |
| 2113 | break; |
| 2114 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2115 | } |
| 2116 | |
| 2117 | #ifdef GST_AML_SPEC_FLOW_FOR_VBP |
| 2118 | static gboolean |
| 2119 | gst_aml_v4l2_buffer_pool_release_buffer_aml_patch(GstBufferPool *bpool) |
| 2120 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2121 | GstFlowReturn ret = GST_FLOW_OK; |
| 2122 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 2123 | GstBufferPoolClass *pclass = GST_BUFFER_POOL_CLASS(parent_class); |
| 2124 | GstAmlV4l2Object *obj = pool->obj; |
| 2125 | GstBuffer *src = NULL; |
| 2126 | GstBufferPoolAcquireParams params; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2127 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2128 | if (!(obj->mode == GST_V4L2_IO_DMABUF_IMPORT && pool->other_pool)) |
| 2129 | { |
| 2130 | GST_WARNING_OBJECT(pool,"please check GstV4l2IOMode!"); |
| 2131 | return FALSE; |
| 2132 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2133 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2134 | /*resolution change flow start*/ |
| 2135 | if (obj->old_other_pool || obj->old_old_other_pool) |
| 2136 | { |
| 2137 | gint outstanding_buf_num = 0; |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2138 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2139 | outstanding_buf_num = gst_aml_v4l2_object_get_outstanding_capture_buf_num(obj); |
| 2140 | GST_DEBUG_OBJECT(pool, "amlmodbuf oop outstanding buf num %d", outstanding_buf_num); |
| 2141 | if (outstanding_buf_num != obj->outstanding_buf_num) |
| 2142 | { |
| 2143 | guint update = 0, need_buf_num = 0; |
| 2144 | GstStructure *config = NULL; |
| 2145 | guint size, min_buffers = 0, old_max_buffers = 0; |
xuesong.jiang | c5dac0f | 2023-02-01 14:42:24 +0800 | [diff] [blame] | 2146 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2147 | if (outstanding_buf_num > obj->outstanding_buf_num) |
| 2148 | { |
| 2149 | GST_ERROR_OBJECT(pool, "amlmodbuf old other pool recycle buffer error, outstanding from %d to %d", obj->outstanding_buf_num, outstanding_buf_num); |
| 2150 | return FALSE; |
| 2151 | } |
| 2152 | GST_DEBUG_OBJECT(pool, "amlmodbuf oop outstanding buf num from %d reduce to %d", obj->outstanding_buf_num, outstanding_buf_num); |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2153 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2154 | /*get buffer pool config to calculate max buffer count*/ |
| 2155 | config = gst_buffer_pool_get_config(pool->other_pool); |
| 2156 | if (config) |
| 2157 | { |
| 2158 | if (gst_buffer_pool_config_get_params(config, NULL, &size, &min_buffers, &old_max_buffers) != FALSE) |
| 2159 | { |
| 2160 | if (G_UNLIKELY(obj->min_buffers < old_max_buffers)) |
| 2161 | { |
| 2162 | GST_ERROR("Too many drm buffers are applied"); |
| 2163 | } |
| 2164 | else if (obj->min_buffers == old_max_buffers) |
| 2165 | { |
| 2166 | update = 0; |
| 2167 | } |
| 2168 | else if (obj->min_buffers > old_max_buffers) |
| 2169 | { |
| 2170 | GST_INFO("display occupy: %d,total num of buffer rotations: %u op have set max buf: %u", |
| 2171 | outstanding_buf_num, |
| 2172 | obj->min_buffers, |
| 2173 | old_max_buffers); |
| 2174 | update = obj->outstanding_buf_num - outstanding_buf_num; // available buf num |
| 2175 | need_buf_num = obj->min_buffers - old_max_buffers; //need buf num |
| 2176 | update = need_buf_num >= update ? update : need_buf_num; |
| 2177 | } |
| 2178 | } |
| 2179 | } |
| 2180 | if (update > 0 && !gst_buffer_pool_increase_max_num(pool->other_pool, update)) |
| 2181 | { |
| 2182 | GST_ERROR_OBJECT(pool, "amlmodbuf update other pool max buffer num error"); |
| 2183 | return FALSE; |
| 2184 | } |
| 2185 | obj->outstanding_buf_num = outstanding_buf_num; |
| 2186 | } |
| 2187 | } |
| 2188 | /*resolution change flow stop*/ |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2189 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2190 | /*buffer match flow*/ |
| 2191 | memset(¶ms, 0, sizeof(GstBufferPoolAcquireParams)); |
| 2192 | params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT; |
| 2193 | GST_TRACE_OBJECT(pool, "before release ready_to_free_buf_num:%d", pool->ready_to_free_buf_num); |
| 2194 | while (pool->ready_to_free_buf_num && gst_buffer_pool_acquire_buffer(pool->other_pool, &src, ¶ms) != GST_FLOW_ERROR && src != NULL) |
| 2195 | { |
| 2196 | gint i = 0; |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2197 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2198 | for (; i < VIDEO_MAX_FRAME; i++) |
| 2199 | { |
| 2200 | GST_TRACE_OBJECT(pool, "amlmodbuf check index:%d", i); |
| 2201 | if (pool->read_to_free_bufs[i]) |
| 2202 | { |
| 2203 | GstBuffer *bind_drm_buf = gst_mini_object_get_qdata(GST_MINI_OBJECT(pool->read_to_free_bufs[i]), GST_AML_V4L2_IMPORT_QUARK); |
| 2204 | if (bind_drm_buf == NULL) |
| 2205 | { |
| 2206 | GST_DEBUG_OBJECT(pool, "init flow, bind v4l2 capture buf[%d]:%p with drm buf:%p", i, pool->read_to_free_bufs[i], src); |
| 2207 | } |
| 2208 | else if (src != bind_drm_buf) |
| 2209 | { |
| 2210 | continue; |
| 2211 | } |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2212 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2213 | GST_TRACE_OBJECT(pool, "v4l2 capture buf[%d]:%p found bind drm buf:%p", i, pool->read_to_free_bufs[i], src); |
| 2214 | GstFlowReturn isvalid = GST_FLOW_OK; |
| 2215 | GstAmlV4l2MemoryGroup *tmp_group = NULL; |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2216 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2217 | // bind_drm_buf= gst_mini_object_steal_qdata(GST_MINI_OBJECT(pool->read_to_free_bufs[i]), GST_AML_V4L2_IMPORT_QUARK); |
| 2218 | ret = gst_aml_v4l2_buffer_pool_import_dmabuf(pool, pool->read_to_free_bufs[i], src); |
| 2219 | gst_buffer_unref(src); |
| 2220 | src = NULL; |
| 2221 | isvalid = gst_aml_v4l2_is_buffer_valid(pool->read_to_free_bufs[i], &tmp_group); |
| 2222 | if ((ret != GST_FLOW_OK && isvalid) || gst_aml_v4l2_buffer_pool_qbuf(pool, pool->read_to_free_bufs[i], tmp_group) != GST_FLOW_OK) |
| 2223 | { |
| 2224 | GST_ERROR_OBJECT(pool, "amlmodbuf go into error flow"); |
| 2225 | pclass->release_buffer(bpool, pool->read_to_free_bufs[i]); |
| 2226 | } |
| 2227 | pool->read_to_free_bufs[i] = NULL; |
| 2228 | pool->ready_to_free_buf_num--; |
| 2229 | break; |
| 2230 | } |
| 2231 | } |
| 2232 | if (i == VIDEO_MAX_FRAME) |
| 2233 | { |
| 2234 | GST_ERROR_OBJECT(pool, "drm buf:%p can't match any v4l2 capture buf, error", src); |
| 2235 | gst_buffer_unref(src); |
| 2236 | src = NULL; |
| 2237 | return FALSE; |
| 2238 | } |
| 2239 | } |
| 2240 | GST_TRACE_OBJECT(pool, "after release ready_to_free_buf_num:%d", pool->ready_to_free_buf_num); |
| 2241 | return TRUE; |
hanghang.luo | dc62690 | 2024-08-23 14:59:27 +0800 | [diff] [blame] | 2242 | |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2243 | } |
| 2244 | #endif |
| 2245 | |
| 2246 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2247 | gst_aml_v4l2_buffer_pool_dispose (GObject * object) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2248 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2249 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL (object); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2250 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2251 | if (pool->vallocator) |
| 2252 | gst_object_unref (pool->vallocator); |
| 2253 | pool->vallocator = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2254 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2255 | if (pool->allocator) |
| 2256 | gst_object_unref (pool->allocator); |
| 2257 | pool->allocator = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2258 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2259 | if (pool->other_pool) |
| 2260 | gst_object_unref (pool->other_pool); |
| 2261 | pool->other_pool = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2262 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2263 | G_OBJECT_CLASS (parent_class)->dispose (object); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2267 | gst_aml_v4l2_buffer_pool_finalize (GObject * object) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2268 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2269 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL (object); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2270 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2271 | if (g_list_length (pool->cc_buffer_list) > 0) |
| 2272 | { |
| 2273 | g_list_free_full (pool->cc_buffer_list, (GDestroyNotify) gst_buffer_unref); |
zengliang.li | dcd4146 | 2024-06-19 16:05:12 +0800 | [diff] [blame] | 2274 | pool->cc_buffer_list = NULL; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2275 | } |
| 2276 | |
| 2277 | if (pool->video_fd >= 0) |
| 2278 | pool->obj->close (pool->video_fd); |
| 2279 | |
| 2280 | gst_poll_free (pool->poll); |
| 2281 | |
| 2282 | /* This can't be done in dispose method because we must not set pointer |
| 2283 | * to NULL as it is part of the v4l2object and dispose could be called |
| 2284 | * multiple times */ |
| 2285 | gst_object_unref (pool->obj->element); |
| 2286 | |
| 2287 | g_cond_clear (&pool->empty_cond); |
| 2288 | |
| 2289 | /* FIXME have we done enough here ? */ |
| 2290 | |
| 2291 | G_OBJECT_CLASS (parent_class)->finalize (object); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2292 | } |
| 2293 | |
| 2294 | static void |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2295 | gst_aml_v4l2_buffer_pool_init (GstAmlV4l2BufferPool * pool) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2296 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2297 | pool->poll = gst_poll_new (TRUE); |
| 2298 | pool->can_poll_device = TRUE; |
| 2299 | g_cond_init (&pool->empty_cond); |
| 2300 | GST_OBJECT_LOCK(pool); |
| 2301 | pool->empty = TRUE; |
| 2302 | GST_OBJECT_UNLOCK(pool); |
| 2303 | pool->orphaned = FALSE; |
| 2304 | pool->cc_buffer_list = NULL; |
| 2305 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2306 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2307 | static void |
| 2308 | gst_aml_v4l2_buffer_pool_class_init (GstAmlV4l2BufferPoolClass * klass) |
| 2309 | { |
| 2310 | GObjectClass *object_class = G_OBJECT_CLASS (klass); |
| 2311 | GstBufferPoolClass *bufferpool_class = GST_BUFFER_POOL_CLASS (klass); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2312 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2313 | object_class->dispose = gst_aml_v4l2_buffer_pool_dispose; |
| 2314 | object_class->finalize = gst_aml_v4l2_buffer_pool_finalize; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2315 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2316 | bufferpool_class->start = gst_aml_v4l2_buffer_pool_start; |
| 2317 | bufferpool_class->stop = gst_aml_v4l2_buffer_pool_stop; |
| 2318 | bufferpool_class->set_config = gst_aml_v4l2_buffer_pool_set_config; |
| 2319 | bufferpool_class->alloc_buffer = gst_aml_v4l2_buffer_pool_alloc_buffer; |
| 2320 | bufferpool_class->acquire_buffer = gst_aml_v4l2_buffer_pool_acquire_buffer; |
| 2321 | bufferpool_class->release_buffer = gst_aml_v4l2_buffer_pool_release_buffer; |
| 2322 | bufferpool_class->flush_start = gst_aml_v4l2_buffer_pool_flush_start; |
| 2323 | bufferpool_class->flush_stop = gst_aml_v4l2_buffer_pool_flush_stop; |
| 2324 | |
| 2325 | GST_DEBUG_CATEGORY_INIT (amlv4l2bufferpool_debug, "amlv4l2bufferpool", 0, |
| 2326 | "V4L2 Buffer Pool"); |
| 2327 | GST_DEBUG_CATEGORY_GET (CAT_PERFORMANCE, "GST_PERFORMANCE"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2328 | } |
| 2329 | |
| 2330 | /** |
| 2331 | * gst_aml_v4l2_buffer_pool_new: |
| 2332 | * @obj: the v4l2 object owning the pool |
| 2333 | * |
| 2334 | * Construct a new buffer pool. |
| 2335 | * |
| 2336 | * Returns: the new pool, use gst_object_unref() to free resources |
| 2337 | */ |
| 2338 | GstBufferPool * |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2339 | gst_aml_v4l2_buffer_pool_new (GstAmlV4l2Object * obj, GstCaps * caps) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2340 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2341 | GstAmlV4l2BufferPool *pool; |
| 2342 | GstStructure *config; |
| 2343 | gchar *name, *parent_name; |
| 2344 | gint fd; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2345 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2346 | fd = obj->dup (obj->video_fd); |
| 2347 | if (fd < 0) |
| 2348 | goto dup_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2349 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2350 | /* setting a significant unique name */ |
| 2351 | parent_name = gst_object_get_name (GST_OBJECT (obj->element)); |
| 2352 | name = g_strconcat(parent_name, ":", "pool:", |
| 2353 | V4L2_TYPE_IS_OUTPUT(obj->type) ? "sink" : "src", NULL); |
| 2354 | g_free (parent_name); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2355 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2356 | pool = (GstAmlV4l2BufferPool *) g_object_new (GST_TYPE_AML_V4L2_BUFFER_POOL, |
| 2357 | "name", name, NULL); |
| 2358 | g_object_ref_sink (pool); |
| 2359 | g_free (name); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2360 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2361 | gst_poll_fd_init (&pool->pollfd); |
| 2362 | pool->pollfd.fd = fd; |
| 2363 | gst_poll_add_fd (pool->poll, &pool->pollfd); |
| 2364 | if (V4L2_TYPE_IS_OUTPUT (obj->type)) |
| 2365 | { |
| 2366 | gst_poll_fd_ctl_write (pool->poll, &pool->pollfd, TRUE); |
| 2367 | } |
| 2368 | else |
| 2369 | { |
| 2370 | gst_poll_fd_ctl_read (pool->poll, &pool->pollfd, TRUE); |
| 2371 | gst_poll_fd_ctl_pri (pool->poll, &pool->pollfd, TRUE); |
| 2372 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2373 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2374 | pool->video_fd = fd; |
| 2375 | pool->obj = obj; |
| 2376 | pool->can_poll_device = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2377 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2378 | pool->vallocator = gst_aml_v4l2_allocator_new (GST_OBJECT (pool), obj); |
| 2379 | if (pool->vallocator == NULL) |
| 2380 | goto allocator_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2381 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2382 | gst_object_ref (obj->element); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2383 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2384 | config = gst_buffer_pool_get_config (GST_BUFFER_POOL_CAST (pool)); |
| 2385 | gst_buffer_pool_config_set_params (config, caps, obj->info.size, 0, 0); |
| 2386 | /* This will simply set a default config, but will not configure the pool |
| 2387 | * because min and max are not valid */ |
| 2388 | (void)gst_buffer_pool_set_config (GST_BUFFER_POOL_CAST(pool), config); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2389 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2390 | return GST_BUFFER_POOL (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2391 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2392 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2393 | dup_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2394 | { |
| 2395 | GST_ERROR ("failed to dup fd %d (%s)", errno, g_strerror (errno)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2396 | return NULL; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2397 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2398 | allocator_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2399 | { |
| 2400 | GST_ERROR_OBJECT (pool, "Failed to create V4L2 allocator"); |
| 2401 | gst_object_unref (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2402 | return NULL; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2403 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2404 | } |
| 2405 | |
| 2406 | static GstFlowReturn |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2407 | gst_aml_v4l2_do_read (GstAmlV4l2BufferPool * pool, GstBuffer * buf) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2408 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2409 | GstFlowReturn res; |
| 2410 | GstAmlV4l2Object *obj = pool->obj; |
| 2411 | gint amount; |
| 2412 | GstMapInfo map; |
| 2413 | gint toread; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2414 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2415 | toread = obj->info.size; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2416 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2417 | GST_LOG_OBJECT (pool, "reading %d bytes into buffer %p", toread, buf); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2418 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2419 | gst_buffer_map (buf, &map, GST_MAP_WRITE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2420 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2421 | do |
| 2422 | { |
| 2423 | if ((res = gst_aml_v4l2_buffer_pool_poll (pool, TRUE)) != GST_FLOW_OK) |
| 2424 | goto poll_error; |
| 2425 | |
| 2426 | amount = obj->read (obj->video_fd, map.data, toread); |
| 2427 | |
| 2428 | if (amount == toread) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2429 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2430 | break; |
| 2431 | } |
| 2432 | else if (amount == -1) |
| 2433 | { |
| 2434 | if (errno == EAGAIN || errno == EINTR) |
| 2435 | { |
| 2436 | continue; |
| 2437 | } |
| 2438 | else |
| 2439 | goto read_error; |
| 2440 | } |
| 2441 | else |
| 2442 | { |
| 2443 | /* short reads can happen if a signal interrupts the read */ |
| 2444 | continue; |
| 2445 | } |
| 2446 | } while (TRUE); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2447 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2448 | GST_LOG_OBJECT (pool, "read %d bytes", amount); |
| 2449 | gst_buffer_unmap (buf, &map); |
| 2450 | gst_buffer_resize (buf, 0, amount); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2451 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2452 | return GST_FLOW_OK; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2453 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2454 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2455 | poll_error: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2456 | { |
| 2457 | GST_DEBUG ("poll error %s", gst_flow_get_name (res)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2458 | goto cleanup; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2459 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2460 | read_error: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2461 | { |
| 2462 | GST_ELEMENT_ERROR (obj->element, RESOURCE, READ, |
| 2463 | (_("Error reading %d bytes from device '%s'."), |
| 2464 | toread, obj->videodev), GST_ERROR_SYSTEM); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2465 | res = GST_FLOW_ERROR; |
| 2466 | goto cleanup; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2467 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2468 | cleanup: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2469 | { |
| 2470 | gst_buffer_unmap (buf, &map); |
| 2471 | gst_buffer_resize (buf, 0, 0); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2472 | return res; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2473 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2474 | } |
| 2475 | |
| 2476 | /** |
| 2477 | * gst_aml_v4l2_buffer_pool_process: |
| 2478 | * @bpool: a #GstBufferPool |
| 2479 | * @buf: a #GstBuffer, maybe be replaced |
| 2480 | * |
| 2481 | * Process @buf in @bpool. For capture devices, this functions fills @buf with |
| 2482 | * data from the device. For output devices, this functions send the contents of |
| 2483 | * @buf to the device for playback. |
| 2484 | * |
| 2485 | * Returns: %GST_FLOW_OK on success. |
| 2486 | */ |
| 2487 | GstFlowReturn |
| 2488 | gst_aml_v4l2_buffer_pool_process(GstAmlV4l2BufferPool *pool, GstBuffer **buf) |
| 2489 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2490 | GstFlowReturn ret = GST_FLOW_OK; |
| 2491 | GstBufferPool *bpool = GST_BUFFER_POOL_CAST (pool); |
| 2492 | GstAmlV4l2Object *obj = pool->obj; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2493 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2494 | GST_DEBUG_OBJECT(pool, "process buffer %p, buf_pool:%p, v4l2 output pool:%p", *buf, (*buf)->pool, bpool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2495 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2496 | if (GST_BUFFER_POOL_IS_FLUSHING (pool)) |
| 2497 | return GST_FLOW_FLUSHING; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2498 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2499 | switch (obj->type) |
| 2500 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2501 | case V4L2_BUF_TYPE_VIDEO_CAPTURE: |
| 2502 | case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2503 | /* capture */ |
| 2504 | switch (obj->mode) |
| 2505 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2506 | case GST_V4L2_IO_RW: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2507 | /* capture into the buffer */ |
| 2508 | ret = gst_aml_v4l2_do_read (pool, *buf); |
| 2509 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2510 | |
| 2511 | case GST_V4L2_IO_MMAP: |
| 2512 | case GST_V4L2_IO_DMABUF: |
| 2513 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2514 | GstBuffer *tmp; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2515 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2516 | if ((*buf)->pool == bpool) |
| 2517 | { |
| 2518 | guint num_queued; |
| 2519 | gsize size = gst_buffer_get_size (*buf); |
| 2520 | |
| 2521 | /* Legacy M2M devices return empty buffer when drained */ |
| 2522 | if (size == 0 && GST_AML_V4L2_IS_M2M(obj->device_caps)) |
| 2523 | goto eos; |
| 2524 | |
| 2525 | if (GST_VIDEO_INFO_FORMAT (&pool->caps_info) != |
| 2526 | GST_VIDEO_FORMAT_ENCODED && size < pool->size) |
| 2527 | goto buffer_truncated; |
| 2528 | |
| 2529 | num_queued = g_atomic_int_get (&pool->num_queued); |
| 2530 | GST_TRACE_OBJECT (pool, "Only %i buffer left in the capture queue.", |
| 2531 | num_queued); |
| 2532 | |
| 2533 | /* If we have no more buffer, and can allocate it time to do so */ |
| 2534 | if (num_queued == 0) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2535 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2536 | if (GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) |
| 2537 | { |
| 2538 | ret = gst_aml_v4l2_buffer_pool_resurrect_buffer (pool); |
| 2539 | if (ret == GST_FLOW_OK) |
| 2540 | goto done; |
| 2541 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2542 | } |
| 2543 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2544 | /* start copying buffers when we are running low on buffers */ |
| 2545 | if (num_queued < pool->copy_threshold) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2546 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2547 | GstBuffer *copy; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2548 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2549 | if (GST_AML_V4L2_ALLOCATOR_CAN_ALLOCATE (pool->vallocator, MMAP)) |
| 2550 | { |
| 2551 | ret = gst_aml_v4l2_buffer_pool_resurrect_buffer (pool); |
| 2552 | if (ret == GST_FLOW_OK) |
| 2553 | goto done; |
| 2554 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2555 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2556 | /* copy the buffer */ |
| 2557 | copy = gst_buffer_copy_region (*buf, |
| 2558 | GST_BUFFER_COPY_ALL | GST_BUFFER_COPY_DEEP, 0, -1); |
| 2559 | GST_LOG_OBJECT (pool, "copy buffer %p->%p", *buf, copy); |
| 2560 | |
| 2561 | /* and requeue so that we can continue capturing */ |
| 2562 | gst_buffer_unref (*buf); |
| 2563 | *buf = copy; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2564 | } |
| 2565 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2566 | ret = GST_FLOW_OK; |
| 2567 | /* nothing, data was inside the buffer when we did _acquire() */ |
| 2568 | goto done; |
| 2569 | } |
hanghang.luo | 185a337 | 2023-06-06 02:35:33 +0000 | [diff] [blame] | 2570 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2571 | /* buffer not from our pool, grab a frame and copy it into the target */ |
| 2572 | if ((ret = gst_aml_v4l2_buffer_pool_dequeue (pool, &tmp, TRUE)) |
| 2573 | != GST_FLOW_OK) |
| 2574 | goto done; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2575 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2576 | if (obj->dumpframefile) |
| 2577 | { |
| 2578 | FILE *pFile = fopen(obj->dumpframefile, "ab"); |
| 2579 | if (pFile) |
| 2580 | { |
| 2581 | int n = gst_buffer_n_memory(tmp); |
| 2582 | int i; |
| 2583 | GstMapInfo map_info; |
| 2584 | GstMemory *mem; |
| 2585 | |
| 2586 | for (i = 0; i < n; ++i) |
| 2587 | { |
| 2588 | mem = gst_buffer_peek_memory(tmp, i); |
| 2589 | if (gst_memory_map(mem, &map_info, GST_MAP_READ)) |
| 2590 | { |
| 2591 | fwrite(map_info.data, map_info.size, 1, pFile); |
| 2592 | gst_memory_unmap(mem, &map_info); |
| 2593 | } |
| 2594 | } |
| 2595 | fclose(pFile); |
| 2596 | } |
| 2597 | } |
| 2598 | /* An empty buffer on capture indicates the end of stream */ |
| 2599 | if (gst_buffer_get_size(tmp) == 0) |
| 2600 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2601 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, tmp); |
| 2602 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2603 | /* Legacy M2M devices return empty buffer when drained */ |
| 2604 | if (GST_AML_V4L2_IS_M2M(obj->device_caps)) |
| 2605 | goto eos; |
| 2606 | } |
| 2607 | |
| 2608 | ret = gst_aml_v4l2_buffer_pool_copy_buffer(pool, *buf, tmp); |
| 2609 | if (obj->mode == GST_V4L2_IO_DMABUF && (GST_VIDEO_FORMAT_NV12 == pool->caps_info.finfo->format || GST_VIDEO_FORMAT_NV21 == pool->caps_info.finfo->format) && gst_buffer_get_size (*buf) > (pool->caps_info.width * pool->caps_info.height * 3 / 2)) |
| 2610 | { |
| 2611 | GST_DEBUG_OBJECT (pool, "resizebuf. format:%d [%d, %d] W:%d, H:%d", pool->caps_info.finfo->format, GST_VIDEO_FORMAT_NV12, GST_VIDEO_FORMAT_NV21, pool->caps_info.width, pool->caps_info.height); |
| 2612 | gst_buffer_resize (*buf, 0, pool->caps_info.width * pool->caps_info.height * 3 / 2); |
| 2613 | } |
| 2614 | |
| 2615 | |
| 2616 | /* an queue the buffer again after the copy */ |
| 2617 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, tmp); |
| 2618 | |
| 2619 | if (ret != GST_FLOW_OK) |
| 2620 | goto copy_failed; |
| 2621 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2622 | } |
| 2623 | |
| 2624 | case GST_V4L2_IO_USERPTR: |
| 2625 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2626 | struct UserPtrData *data; |
| 2627 | GstBuffer *tmp; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2628 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2629 | /* Replace our buffer with downstream allocated buffer */ |
| 2630 | data = gst_mini_object_steal_qdata (GST_MINI_OBJECT (*buf), |
| 2631 | GST_AML_V4L2_IMPORT_QUARK); |
| 2632 | tmp = gst_buffer_ref (data->buffer); |
| 2633 | _unmap_userptr_frame (data); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2634 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2635 | /* Now tmp is writable, copy the flags and timestamp */ |
| 2636 | gst_buffer_copy_into (tmp, *buf, |
| 2637 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2638 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2639 | gst_buffer_replace (buf, tmp); |
| 2640 | gst_buffer_unref (tmp); |
| 2641 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2642 | } |
| 2643 | |
| 2644 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 2645 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2646 | GstBuffer *tmp; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2647 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2648 | /* Replace our buffer with downstream allocated buffer */ |
| 2649 | // tmp = gst_mini_object_steal_qdata(GST_MINI_OBJECT(*buf), |
| 2650 | // GST_AML_V4L2_IMPORT_QUARK); |
| 2651 | tmp = gst_mini_object_get_qdata(GST_MINI_OBJECT(*buf), GST_AML_V4L2_IMPORT_QUARK); |
| 2652 | GST_DEBUG("got v4l2 capture buf:%p, with qdata drm buf:%p", *buf, tmp); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2653 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2654 | gst_buffer_copy_into (tmp, *buf, |
| 2655 | GST_BUFFER_COPY_FLAGS | GST_BUFFER_COPY_TIMESTAMPS, 0, -1); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2656 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2657 | gst_buffer_replace (buf, tmp); |
| 2658 | gst_buffer_unref (tmp); |
| 2659 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2663 | g_assert_not_reached (); |
| 2664 | break; |
| 2665 | } |
| 2666 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2667 | |
| 2668 | case V4L2_BUF_TYPE_VIDEO_OUTPUT: |
| 2669 | case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2670 | /* playback */ |
| 2671 | switch (obj->mode) |
| 2672 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2673 | case GST_V4L2_IO_RW: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2674 | /* FIXME, do write() */ |
| 2675 | GST_WARNING_OBJECT (pool, "implement write()"); |
| 2676 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2677 | |
| 2678 | case GST_V4L2_IO_USERPTR: |
| 2679 | case GST_V4L2_IO_DMABUF_IMPORT: |
| 2680 | case GST_V4L2_IO_DMABUF: |
| 2681 | case GST_V4L2_IO_MMAP: |
| 2682 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2683 | GstBuffer *to_queue = NULL; |
| 2684 | GstBuffer *buffer; |
| 2685 | GstAmlV4l2MemoryGroup *group; |
| 2686 | gint index; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2687 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2688 | if ((*buf)->pool != bpool) |
| 2689 | goto copying; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2690 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2691 | if (!gst_aml_v4l2_is_buffer_valid (*buf, &group)) |
| 2692 | goto copying; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2693 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2694 | index = group->buffer.index; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2695 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2696 | GST_LOG_OBJECT (pool, "processing buffer %i from our pool", index); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2697 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2698 | if (pool->buffers[index] != NULL) |
| 2699 | { |
| 2700 | GST_LOG_OBJECT (pool, "buffer %i already queued, copying", index); |
| 2701 | goto copying; |
| 2702 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2703 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2704 | /* we can queue directly */ |
| 2705 | to_queue = gst_buffer_ref (*buf); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2706 | |
| 2707 | copying: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2708 | if (to_queue == NULL) |
| 2709 | { |
| 2710 | GstBufferPoolAcquireParams params = { 0 }; |
| 2711 | |
| 2712 | GST_LOG_OBJECT (pool, "alloc buffer from our pool"); |
| 2713 | |
| 2714 | /* this can return EOS if all buffers are outstanding which would |
| 2715 | * be strange because we would expect the upstream element to have |
| 2716 | * allocated them and returned to us.. */ |
| 2717 | params.flags = GST_BUFFER_POOL_ACQUIRE_FLAG_DONTWAIT; |
| 2718 | ret = gst_buffer_pool_acquire_buffer (bpool, &to_queue, ¶ms); |
| 2719 | if (ret != GST_FLOW_OK) |
| 2720 | goto acquire_failed; |
| 2721 | |
| 2722 | ret = gst_aml_v4l2_buffer_pool_prepare_buffer (pool, to_queue, *buf); |
| 2723 | if (ret != GST_FLOW_OK) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2724 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2725 | gst_buffer_unref (to_queue); |
| 2726 | goto prepare_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2727 | } |
| 2728 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2729 | /* retrieve the group */ |
| 2730 | gst_aml_v4l2_is_buffer_valid (to_queue, &group); |
| 2731 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2732 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2733 | if ((ret = gst_aml_v4l2_buffer_pool_qbuf(pool, to_queue, group)) != GST_FLOW_OK) |
| 2734 | goto queue_failed; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2735 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2736 | /* if we are not streaming yet (this is the first buffer, start |
| 2737 | * streaming now */ |
| 2738 | if (!gst_aml_v4l2_buffer_pool_streamon (pool)) |
| 2739 | { |
| 2740 | /* don't check return value because qbuf would have failed */ |
| 2741 | gst_aml_v4l2_is_buffer_valid (to_queue, &group); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2742 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2743 | /* qbuf has stored to_queue buffer but we are not in |
| 2744 | * streaming state, so the flush logic won't be performed. |
| 2745 | * To avoid leaks, flush the allocator and restore the queued |
| 2746 | * buffer as non-queued */ |
| 2747 | gst_aml_v4l2_allocator_flush (pool->vallocator); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2748 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2749 | pool->buffers[group->buffer.index] = NULL; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2750 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2751 | gst_mini_object_set_qdata (GST_MINI_OBJECT (to_queue), |
| 2752 | GST_AML_V4L2_IMPORT_QUARK, NULL, NULL); |
| 2753 | gst_buffer_unref (to_queue); |
| 2754 | g_atomic_int_add (&pool->num_queued, -1); |
| 2755 | goto start_failed; |
| 2756 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2757 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2758 | /* Remove our ref, we will still hold this buffer in acquire as needed, |
| 2759 | * otherwise the pool will think it is outstanding and will refuse to stop. */ |
| 2760 | gst_buffer_unref (to_queue); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2761 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2762 | /* release as many buffer as possible */ |
| 2763 | while (gst_aml_v4l2_buffer_pool_dequeue (pool, &buffer, FALSE) == |
| 2764 | GST_FLOW_OK) |
| 2765 | { |
| 2766 | if (buffer->pool == NULL) |
| 2767 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, buffer); |
| 2768 | } |
| 2769 | |
| 2770 | if (g_atomic_int_get (&pool->num_queued) >= pool->min_latency) |
| 2771 | { |
| 2772 | /* all buffers are queued, try to dequeue one and release it back |
| 2773 | * into the pool so that _acquire can get to it again. */ |
| 2774 | ret = gst_aml_v4l2_buffer_pool_dequeue (pool, &buffer, TRUE); |
| 2775 | if (ret == GST_FLOW_OK && buffer->pool == NULL) |
| 2776 | /* release the rendered buffer back into the pool. This wakes up any |
| 2777 | * thread waiting for a buffer in _acquire(). */ |
| 2778 | gst_aml_v4l2_buffer_pool_release_buffer(bpool, buffer); |
| 2779 | } |
| 2780 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2781 | } |
| 2782 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2783 | g_assert_not_reached (); |
| 2784 | break; |
| 2785 | } |
| 2786 | break; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2787 | default: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2788 | g_assert_not_reached (); |
| 2789 | break; |
| 2790 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2791 | done: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2792 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2793 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2794 | /* ERRORS */ |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2795 | copy_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2796 | { |
| 2797 | GST_ERROR_OBJECT (pool, "failed to copy buffer"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2798 | return ret; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2799 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2800 | buffer_truncated: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2801 | { |
| 2802 | GST_WARNING_OBJECT (pool, |
| 2803 | "Dropping truncated buffer, this is likely a driver bug."); |
| 2804 | gst_buffer_unref (*buf); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2805 | *buf = NULL; |
| 2806 | return GST_AML_V4L2_FLOW_CORRUPTED_BUFFER; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2807 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2808 | eos: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2809 | { |
| 2810 | GST_DEBUG_OBJECT (pool, "end of stream reached"); |
| 2811 | gst_buffer_unref (*buf); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2812 | *buf = NULL; |
| 2813 | return GST_AML_V4L2_FLOW_LAST_BUFFER; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2814 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2815 | acquire_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2816 | { |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2817 | if (ret == GST_FLOW_FLUSHING) |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2818 | GST_DEBUG_OBJECT (pool, "flushing"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2819 | else |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2820 | GST_WARNING_OBJECT (pool, "failed to acquire a buffer: %s", |
| 2821 | gst_flow_get_name (ret)); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2822 | return ret; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2823 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2824 | prepare_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2825 | { |
| 2826 | GST_ERROR_OBJECT (pool, "failed to prepare data"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2827 | return ret; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2828 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2829 | queue_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2830 | { |
| 2831 | GST_ERROR_OBJECT (pool, "failed to queue buffer"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2832 | return ret; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2833 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2834 | start_failed: |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2835 | { |
| 2836 | GST_ERROR_OBJECT (pool, "failed to start streaming"); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2837 | return GST_FLOW_ERROR; |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2838 | } |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2839 | } |
| 2840 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2841 | void |
| 2842 | gst_aml_v4l2_buffer_pool_set_other_pool (GstAmlV4l2BufferPool * pool, |
| 2843 | GstBufferPool * other_pool) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2844 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2845 | g_return_if_fail (!gst_buffer_pool_is_active (GST_BUFFER_POOL (pool))); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2846 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2847 | if (pool->other_pool) |
| 2848 | gst_object_unref (pool->other_pool); |
| 2849 | pool->other_pool = gst_object_ref (other_pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2850 | } |
| 2851 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2852 | void |
| 2853 | gst_aml_v4l2_buffer_pool_copy_at_threshold (GstAmlV4l2BufferPool * pool, gboolean copy) |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2854 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2855 | GST_OBJECT_LOCK (pool); |
| 2856 | pool->enable_copy_threshold = copy; |
| 2857 | GST_OBJECT_UNLOCK (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2858 | } |
| 2859 | |
| 2860 | gboolean |
| 2861 | gst_aml_v4l2_buffer_pool_flush(GstBufferPool *bpool) |
| 2862 | { |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2863 | GstAmlV4l2BufferPool *pool = GST_AML_V4L2_BUFFER_POOL(bpool); |
| 2864 | gboolean ret = TRUE; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2865 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2866 | gst_aml_v4l2_buffer_pool_streamoff (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2867 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2868 | if (!V4L2_TYPE_IS_OUTPUT (pool->obj->type)) |
| 2869 | ret = gst_aml_v4l2_buffer_pool_streamon (pool); |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2870 | |
bo.xiao | 857b868 | 2024-09-12 16:40:32 +0800 | [diff] [blame] | 2871 | return ret; |
xuesong.jiang | ae1548e | 2022-05-06 16:38:46 +0800 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | void gst_aml_v4l2_buffer_pool_dump_stat(GstAmlV4l2BufferPool *pool, const gchar *file_name, gint try_num) |
| 2875 | { |
| 2876 | const gchar *dump_dir = NULL; |
| 2877 | gchar *full_file_name = NULL; |
| 2878 | FILE *out = NULL; |
| 2879 | |
| 2880 | dump_dir = g_getenv("GST_DEBUG_DUMP_AMLV4L2DEC_STAT_DIR"); |
| 2881 | if (G_LIKELY(dump_dir == NULL)) |
| 2882 | return; |
| 2883 | |
| 2884 | if (!file_name) |
| 2885 | { |
| 2886 | file_name = "unnamed"; |
| 2887 | } |
| 2888 | |
| 2889 | full_file_name = g_strdup_printf("%s" G_DIR_SEPARATOR_S "%s.stat", dump_dir, file_name); |
| 2890 | |
| 2891 | if ((out = fopen(full_file_name, "w"))) |
| 2892 | { |
| 2893 | GstStructure *config = NULL; |
| 2894 | config = gst_buffer_pool_get_config((GstBufferPool *)pool); |
| 2895 | if (config) |
| 2896 | { |
| 2897 | GstCaps *caps; |
| 2898 | guint size, min_buffers, max_buffers; |
| 2899 | if (gst_buffer_pool_config_get_params(config, &caps, &size, &min_buffers, &max_buffers)) |
| 2900 | { |
| 2901 | gchar *stat_info; |
| 2902 | |
| 2903 | /* set local pool info*/ |
| 2904 | gint already_queued = 0; |
| 2905 | gint ready_to_queue_num = 0; |
| 2906 | for (gint i = 0; i < VIDEO_MAX_FRAME; i++) |
| 2907 | { |
| 2908 | if (pool->buffers[i]) |
| 2909 | { |
| 2910 | already_queued++; |
| 2911 | } |
| 2912 | if (pool->read_to_free_bufs[i]) |
| 2913 | { |
| 2914 | ready_to_queue_num++; |
| 2915 | } |
| 2916 | } |
| 2917 | |
| 2918 | stat_info = g_strdup_printf("local pool | size:%d, min_bufs:%d, max_bufs:%d | queued:%d, allocated:%d | already_queued:%d, ready_to_queue:%d | try_num:%d\n", |
| 2919 | size, min_buffers, max_buffers, |
| 2920 | pool->num_queued, pool->num_allocated, |
| 2921 | already_queued, ready_to_queue_num, try_num); |
| 2922 | fputs(stat_info, out); |
| 2923 | g_free(stat_info); |
| 2924 | |
| 2925 | /* set other pool info*/ |
| 2926 | if (pool->other_pool) |
| 2927 | { |
| 2928 | GstStructure *other_config = NULL; |
| 2929 | other_config = gst_buffer_pool_get_config((GstBufferPool *)pool); |
| 2930 | if (other_config) |
| 2931 | { |
| 2932 | GstCaps *other_caps; |
| 2933 | guint other_size, other_min_buffers, other_max_buffers; |
| 2934 | if (gst_buffer_pool_config_get_params(config, &other_caps, &other_size, &other_min_buffers, &other_max_buffers)) |
| 2935 | { |
| 2936 | stat_info = g_strdup_printf("other pool | size:%d, min_bufs:%d, max_bufs:%d\n", |
| 2937 | other_size, other_min_buffers, other_max_buffers); |
| 2938 | |
| 2939 | // TODO:GstBufferPool中没有获取outstanding的接口,所以这里没有统计otherpool中在用buffer的状态,后续需要修改gstreamer-1.0中code新增接口 |
| 2940 | fputs(stat_info, out); |
| 2941 | g_free(stat_info); |
| 2942 | } |
| 2943 | } |
| 2944 | } |
| 2945 | GST_INFO("wrote amlv4l2 bufferpool stat to : '%s' succ", full_file_name); |
| 2946 | } |
| 2947 | } |
| 2948 | else |
| 2949 | { |
| 2950 | GST_WARNING("Failed to get config for pool:%p", pool); |
| 2951 | } |
| 2952 | fclose(out); |
| 2953 | } |
| 2954 | else |
| 2955 | { |
| 2956 | GST_WARNING("Failed to open file '%s' for writing: %s", full_file_name, g_strerror(errno)); |
| 2957 | } |
| 2958 | g_free(full_file_name); |
| 2959 | } |