+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
main
| 1 | /* |
| 2 | * SPDX-License-Identifier: AGPL-3.0-or-later WITH Classpath-exception-2.0 |
| 3 | * Copyright (c) 2026 Saif Mukhtar |
| 4 | * |
| 5 | * OpenAVX - Open Audio-Video eXchange |
| 6 | * src/avx_video_codec.c - Video encoder/decoder registry & built-in SpeedHQ backends |
| 7 | */ |
| 8 | |
| 9 | #include "avx_video_codec.h" |
| 10 | #include "avx_internal.h" |
| 11 | #include "avx_speedhq.h" |
| 12 | #include "avx_color.h" |
| 13 | #include <stdlib.h> |
| 14 | #include <string.h> |
| 15 | |
| 16 | #define AVX_MAX_VIDEO_BACKENDS 16 |
| 17 | |
| 18 | /* ========================================================================= */ |
| 19 | /* Internal Instance & Registry State */ |
| 20 | /* ========================================================================= */ |
| 21 | |
| 22 | struct avx_video_encoder_t { |
| 23 | const avx_video_encoder_ops_t *ops; |
| 24 | void *backend_ctx; |
| 25 | avx_video_enc_cfg_t cfg; |
| 26 | }; |
| 27 | |
| 28 | struct avx_video_decoder_t { |
| 29 | const avx_video_decoder_ops_t *ops; |
| 30 | void *backend_ctx; |
| 31 | avx_video_dec_cfg_t cfg; |
| 32 | uint8_t *annexb_buf; |
| 33 | size_t annexb_cap; |
| 34 | }; |
| 35 | |
| 36 | static avx_mutex_t s_vcodec_lock = AVX_MUTEX_INITIALIZER; |
| 37 | static bool s_vcodec_initialized = false; |
| 38 | |
| 39 | static const avx_video_encoder_ops_t *s_enc_backends[AVX_MAX_VIDEO_BACKENDS]; |
| 40 | static int s_enc_count = 0; |
| 41 | |
| 42 | static const avx_video_decoder_ops_t *s_dec_backends[AVX_MAX_VIDEO_BACKENDS]; |
| 43 | static int s_dec_count = 0; |
| 44 | |
| 45 | /* ========================================================================= */ |
| 46 | /* Built-In SpeedHQ Encoder Backend */ |
| 47 | /* ========================================================================= */ |
| 48 | |
| 49 | typedef struct { |
| 50 | uint32_t fourcc; |
| 51 | uint32_t width; |
| 52 | uint32_t height; |
| 53 | uint8_t quality; |
| 54 | uint8_t *uyvy_tmp; |
| 55 | size_t uyvy_tmp_cap; |
| 56 | uint8_t *alpha_tmp; |
| 57 | size_t alpha_tmp_cap; |
| 58 | } avx_shq_enc_ctx_t; |
| 59 | |
| 60 | static bool shq_enc_supports_fourcc(uint32_t target_fourcc) { |
| 61 | return avx_is_speedhq_fourcc(target_fourcc); |
| 62 | } |
| 63 | |
| 64 | static bool shq_fourcc_has_alpha(uint32_t fourcc) { |
| 65 | return (fourcc == OAVX_FOURCC_VIDEO_TYPE_SHQ1 || |
| 66 | fourcc == OAVX_FOURCC_VIDEO_TYPE_SHQ3 || |
| 67 | fourcc == OAVX_FOURCC_VIDEO_TYPE_SHQ5 || |
| 68 | fourcc == OAVX_FOURCC_VIDEO_TYPE_SHQ7 || |
| 69 | fourcc == OAVX_FOURCC_VIDEO_TYPE_SHQ9); |
| 70 | } |
| 71 | |
| 72 | static uint8_t shq_clamp_quality_or_zero(uint8_t q) { |
| 73 | if (q == 0) return 0; |
| 74 | if (q < 20) return 20; |
| 75 | if (q > 98) return 98; |
| 76 | return q; |
| 77 | } |
| 78 | |
| 79 | static int shq_ensure_tmp(uint8_t **buf, size_t *cap, size_t needed) { |
| 80 | if (*cap >= needed && *buf) return 0; |
| 81 | uint8_t *nb = (uint8_t *)realloc(*buf, needed); |
| 82 | if (!nb) return -1; |
| 83 | *buf = nb; |
| 84 | *cap = needed; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | /* |
| 89 | * Adapts source frame format (RGBA/BGRA/planar) to 4:2:2 UYVY and extracts |
| 90 | * an optional alpha plane as required by the destination SpeedHQ subtype. |
| 91 | */ |
| 92 | static int shq_prepare_uyvy_input(avx_shq_enc_ctx_t *ctx, |
| 93 | const oavx_video_frame_t *in_frame, |
| 94 | uint32_t w, uint32_t h, |
| 95 | const uint8_t **out_uyvy, size_t *out_uyvy_stride, |
| 96 | const uint8_t **out_alpha, size_t *out_alpha_stride) |
| 97 | { |
| 98 | uint32_t src_fourcc = (uint32_t)in_frame->fourcc; |
| 99 | bool want_alpha = shq_fourcc_has_alpha(ctx->fourcc); |
| 100 | |
| 101 | if (src_fourcc == 0 || avx_is_speedhq_fourcc(src_fourcc) || |
| 102 | src_fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY || |
| 103 | src_fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVA) { |
| 104 | size_t s0 = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w * 2; |
| 105 | const uint8_t *alpha = in_frame->planes[1]; |
| 106 | if (!alpha && (want_alpha || src_fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVA)) { |
| 107 | alpha = in_frame->planes[0] + s0 * (size_t)h; |
| 108 | } |
| 109 | *out_uyvy = in_frame->planes[0]; |
| 110 | *out_uyvy_stride = s0; |
| 111 | *out_alpha = alpha; |
| 112 | *out_alpha_stride = in_frame->strides[1] ? in_frame->strides[1] : (size_t)w; |
| 113 | return 0; |
| 114 | } |
| 115 | |
| 116 | size_t uyvy_bytes = (size_t)w * (size_t)h * 2; |
| 117 | if (shq_ensure_tmp(&ctx->uyvy_tmp, &ctx->uyvy_tmp_cap, uyvy_bytes) != 0) { |
| 118 | return -1; |
| 119 | } |
| 120 | *out_uyvy = ctx->uyvy_tmp; |
| 121 | *out_uyvy_stride = (size_t)w * 2; |
| 122 | *out_alpha = NULL; |
| 123 | *out_alpha_stride = 0; |
| 124 | |
| 125 | bool is_rgba = (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA || src_fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBX); |
| 126 | bool is_bgra = (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA || src_fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRX); |
| 127 | if (is_rgba || is_bgra) { |
| 128 | size_t s0 = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w * 4; |
| 129 | if (s0 < (size_t)w * 4) return -1; |
| 130 | |
| 131 | if (s0 == (size_t)w * 4) { |
| 132 | if (is_rgba) avx_color_rgba_to_uyvy(in_frame->planes[0], ctx->uyvy_tmp, (int)w, (int)h); |
| 133 | else avx_color_bgra_to_uyvy(in_frame->planes[0], ctx->uyvy_tmp, (int)w, (int)h); |
| 134 | } else { |
| 135 | for (uint32_t y = 0; y < h; ++y) { |
| 136 | const uint8_t *src_row = in_frame->planes[0] + (size_t)y * s0; |
| 137 | uint8_t *dst_row = ctx->uyvy_tmp + (size_t)y * (size_t)w * 2; |
| 138 | if (is_rgba) avx_color_rgba_to_uyvy(src_row, dst_row, (int)w, 1); |
| 139 | else avx_color_bgra_to_uyvy(src_row, dst_row, (int)w, 1); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | if (want_alpha && (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA || |
| 144 | src_fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA)) { |
| 145 | size_t alpha_bytes = (size_t)w * (size_t)h; |
| 146 | if (shq_ensure_tmp(&ctx->alpha_tmp, &ctx->alpha_tmp_cap, alpha_bytes) != 0) { |
| 147 | return -1; |
| 148 | } |
| 149 | for (uint32_t y = 0; y < h; ++y) { |
| 150 | const uint8_t *src_row = in_frame->planes[0] + (size_t)y * s0; |
| 151 | uint8_t *dst_a = ctx->alpha_tmp + (size_t)y * (size_t)w; |
| 152 | for (uint32_t x = 0; x < w; ++x) { |
| 153 | dst_a[x] = src_row[x * 4 + 3]; |
| 154 | } |
| 155 | } |
| 156 | *out_alpha = ctx->alpha_tmp; |
| 157 | *out_alpha_stride = (size_t)w; |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_NV12) { |
| 163 | size_t s0 = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w; |
| 164 | size_t s1 = in_frame->strides[1] ? in_frame->strides[1] : (size_t)w; |
| 165 | const uint8_t *uv = in_frame->planes[1] ? in_frame->planes[1] : (in_frame->planes[0] + s0 * (size_t)h); |
| 166 | avx_color_nv12_to_uyvy(in_frame->planes[0], s0, uv, s1, |
| 167 | ctx->uyvy_tmp, (size_t)w * 2, (int)w, (int)h); |
| 168 | return 0; |
| 169 | } |
| 170 | |
| 171 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_I420 || src_fourcc == OAVX_FOURCC_VIDEO_TYPE_YV12) { |
| 172 | size_t s0 = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w; |
| 173 | size_t cw = (size_t)((w + 1) / 2); |
| 174 | size_t ch = (size_t)((h + 1) / 2); |
| 175 | size_t s1 = in_frame->strides[1] ? in_frame->strides[1] : cw; |
| 176 | size_t s2 = in_frame->strides[2] ? in_frame->strides[2] : cw; |
| 177 | const uint8_t *p1 = in_frame->planes[1] ? in_frame->planes[1] : (in_frame->planes[0] + s0 * (size_t)h); |
| 178 | const uint8_t *p2 = in_frame->planes[2] ? in_frame->planes[2] : (p1 + s1 * ch); |
| 179 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_I420) { |
| 180 | avx_color_i420_to_uyvy(in_frame->planes[0], s0, p1, s1, p2, s2, |
| 181 | ctx->uyvy_tmp, (size_t)w * 2, (int)w, (int)h); |
| 182 | } else { |
| 183 | avx_color_yv12_to_uyvy(in_frame->planes[0], s0, p1, s1, p2, s2, |
| 184 | ctx->uyvy_tmp, (size_t)w * 2, (int)w, (int)h); |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_P216) { |
| 190 | avx_color_p216_to_uyvy(in_frame->planes[0], ctx->uyvy_tmp, (int)w, (int)h); |
| 191 | return 0; |
| 192 | } |
| 193 | |
| 194 | return -1; |
| 195 | } |
| 196 | |
| 197 | static int shq_enc_create(const avx_video_enc_cfg_t *cfg, void **backend_ctx) { |
| 198 | if (!cfg || !backend_ctx || !avx_is_speedhq_fourcc(cfg->fourcc)) return -1; |
| 199 | if (cfg->width == 0 || cfg->height == 0 || |
| 200 | cfg->width > AVX_MAX_VIDEO_WIDTH || cfg->height > AVX_MAX_VIDEO_HEIGHT) { |
| 201 | return -1; |
| 202 | } |
| 203 | |
| 204 | avx_speedhq_init(); |
| 205 | |
| 206 | avx_shq_enc_ctx_t *ctx = (avx_shq_enc_ctx_t *)calloc(1, sizeof(*ctx)); |
| 207 | if (!ctx) return -1; |
| 208 | |
| 209 | ctx->fourcc = cfg->fourcc; |
| 210 | ctx->width = cfg->width; |
| 211 | ctx->height = cfg->height; |
| 212 | ctx->quality = shq_clamp_quality_or_zero(cfg->quality); |
| 213 | *backend_ctx = ctx; |
| 214 | return 0; |
| 215 | } |
| 216 | |
| 217 | /* Compresses raw UYVY/alpha frame into SpeedHQ bitstream slices with quality quantization. */ |
| 218 | static int shq_enc_encode(void *backend_ctx, |
| 219 | const oavx_video_frame_t *in_frame, |
| 220 | uint8_t *out_buf, size_t out_cap, |
| 221 | size_t *out_size, bool *out_is_keyframe) { |
| 222 | avx_shq_enc_ctx_t *ctx = (avx_shq_enc_ctx_t *)backend_ctx; |
| 223 | if (!ctx || !in_frame || !in_frame->planes[0] || !out_buf || !out_size) return -1; |
| 224 | |
| 225 | uint32_t w = in_frame->width ? in_frame->width : ctx->width; |
| 226 | uint32_t h = in_frame->height ? in_frame->height : ctx->height; |
| 227 | if (w == 0 || h == 0 || w > AVX_MAX_VIDEO_WIDTH || h > AVX_MAX_VIDEO_HEIGHT) return -1; |
| 228 | |
| 229 | const uint8_t *uyvy_ptr = NULL; |
| 230 | const uint8_t *alpha_ptr = NULL; |
| 231 | size_t uyvy_stride = 0; |
| 232 | size_t alpha_stride = 0; |
| 233 | if (shq_prepare_uyvy_input(ctx, in_frame, w, h, |
| 234 | &uyvy_ptr, &uyvy_stride, |
| 235 | &alpha_ptr, &alpha_stride) != 0) { |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | int rc = avx_speedhq_encode_strided_ex(ctx->fourcc, (int)w, (int)h, |
| 240 | uyvy_ptr, uyvy_stride, |
| 241 | alpha_ptr, alpha_stride, |
| 242 | ctx->quality, |
| 243 | out_buf, out_cap, out_size); |
| 244 | if (rc == 0 && out_is_keyframe) { |
| 245 | *out_is_keyframe = true; /* SpeedHQ is intra-frame only */ |
| 246 | } |
| 247 | return rc; |
| 248 | } |
| 249 | |
| 250 | static int shq_enc_set_quality(void *backend_ctx, uint8_t quality) { |
| 251 | avx_shq_enc_ctx_t *ctx = (avx_shq_enc_ctx_t *)backend_ctx; |
| 252 | if (!ctx) return -1; |
| 253 | ctx->quality = shq_clamp_quality_or_zero(quality); |
| 254 | return 0; |
| 255 | } |
| 256 | |
| 257 | static void shq_enc_request_keyframe(void *backend_ctx) { |
| 258 | (void)backend_ctx; /* Intra-frame codec: every frame is already a keyframe */ |
| 259 | } |
| 260 | |
| 261 | static void shq_enc_destroy(void *backend_ctx) { |
| 262 | avx_shq_enc_ctx_t *ctx = (avx_shq_enc_ctx_t *)backend_ctx; |
| 263 | if (!ctx) return; |
| 264 | free(ctx->uyvy_tmp); |
| 265 | free(ctx->alpha_tmp); |
| 266 | free(ctx); |
| 267 | } |
| 268 | |
| 269 | static const avx_video_encoder_ops_t s_shq_encoder_ops = { |
| 270 | .name = "speedhq-cpu", |
| 271 | .priority = 50, |
| 272 | .is_hardware = false, |
| 273 | .supports_fourcc = shq_enc_supports_fourcc, |
| 274 | .create = shq_enc_create, |
| 275 | .encode = shq_enc_encode, |
| 276 | .set_quality = shq_enc_set_quality, |
| 277 | .request_keyframe = shq_enc_request_keyframe, |
| 278 | .destroy = shq_enc_destroy |
| 279 | }; |
| 280 | |
| 281 | /* ========================================================================= */ |
| 282 | /* Built-In SpeedHQ Decoder Backend */ |
| 283 | /* ========================================================================= */ |
| 284 | |
| 285 | typedef struct { |
| 286 | uint32_t fourcc; |
| 287 | uint32_t width; |
| 288 | uint32_t height; |
| 289 | uint8_t *buf; |
| 290 | size_t buf_size; |
| 291 | } avx_shq_dec_ctx_t; |
| 292 | |
| 293 | static bool shq_dec_supports_fourcc(uint32_t src_fourcc) { |
| 294 | return avx_is_speedhq_fourcc(src_fourcc); |
| 295 | } |
| 296 | |
| 297 | static int shq_dec_create(const avx_video_dec_cfg_t *cfg, void **backend_ctx) { |
| 298 | if (!cfg || !backend_ctx || !avx_is_speedhq_fourcc(cfg->fourcc)) return -1; |
| 299 | if (cfg->width == 0 || cfg->height == 0 || |
| 300 | cfg->width > AVX_MAX_VIDEO_WIDTH || cfg->height > AVX_MAX_VIDEO_HEIGHT) { |
| 301 | return -1; |
| 302 | } |
| 303 | |
| 304 | avx_speedhq_init(); |
| 305 | |
| 306 | avx_shq_dec_ctx_t *ctx = (avx_shq_dec_ctx_t *)calloc(1, sizeof(*ctx)); |
| 307 | if (!ctx) return -1; |
| 308 | |
| 309 | ctx->fourcc = cfg->fourcc; |
| 310 | ctx->width = cfg->width; |
| 311 | ctx->height = cfg->height; |
| 312 | *backend_ctx = ctx; |
| 313 | return 0; |
| 314 | } |
| 315 | |
| 316 | /* Decompresses SpeedHQ bitstream slices into raw UYVY/alpha output frame. */ |
| 317 | static int shq_dec_decode(void *backend_ctx, |
| 318 | const uint8_t *pkt_data, size_t pkt_size, |
| 319 | oavx_video_frame_t *out_frame) { |
| 320 | avx_shq_dec_ctx_t *ctx = (avx_shq_dec_ctx_t *)backend_ctx; |
| 321 | if (!ctx || !pkt_data || pkt_size == 0 || !out_frame) return -1; |
| 322 | |
| 323 | uint64_t needed_u64 = (uint64_t)ctx->width * (uint64_t)ctx->height * 3ULL; |
| 324 | if (ctx->width == 0 || ctx->height == 0 || |
| 325 | ctx->width > AVX_MAX_VIDEO_WIDTH || ctx->height > AVX_MAX_VIDEO_HEIGHT || |
| 326 | needed_u64 > AVX_MAX_DECOMPRESSED_FRAME_SIZE) { |
| 327 | return -1; |
| 328 | } |
| 329 | |
| 330 | size_t needed = (size_t)needed_u64; |
| 331 | if (ctx->buf_size < needed) { |
| 332 | uint8_t *nb = (uint8_t *)realloc(ctx->buf, needed); |
| 333 | if (!nb) return -1; |
| 334 | ctx->buf = nb; |
| 335 | ctx->buf_size = needed; |
| 336 | } |
| 337 | |
| 338 | bool has_alpha = false; |
| 339 | uint8_t *alpha_plane = ctx->buf + (size_t)ctx->width * (size_t)ctx->height * 2; |
| 340 | if (avx_speedhq_decode_ex(ctx->fourcc, (int)ctx->width, (int)ctx->height, |
| 341 | pkt_data, pkt_size, |
| 342 | ctx->buf, alpha_plane, &has_alpha) != 0) { |
| 343 | return -1; |
| 344 | } |
| 345 | |
| 346 | out_frame->width = ctx->width; |
| 347 | out_frame->height = ctx->height; |
| 348 | out_frame->fourcc = has_alpha ? OAVX_FOURCC_VIDEO_TYPE_UYVA : OAVX_FOURCC_VIDEO_TYPE_UYVY; |
| 349 | out_frame->planes[0] = ctx->buf; |
| 350 | out_frame->strides[0] = (size_t)ctx->width * 2; |
| 351 | if (has_alpha) { |
| 352 | out_frame->planes[1] = alpha_plane; |
| 353 | out_frame->strides[1] = (size_t)ctx->width; |
| 354 | } else { |
| 355 | out_frame->planes[1] = NULL; |
| 356 | out_frame->strides[1] = 0; |
| 357 | } |
| 358 | out_frame->planes[2] = NULL; |
| 359 | out_frame->strides[2] = 0; |
| 360 | out_frame->planes[3] = NULL; |
| 361 | out_frame->strides[3] = 0; |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | static void shq_dec_reset(void *backend_ctx) { |
| 366 | (void)backend_ctx; /* Stateless between intra frames */ |
| 367 | } |
| 368 | |
| 369 | static void shq_dec_destroy(void *backend_ctx) { |
| 370 | avx_shq_dec_ctx_t *ctx = (avx_shq_dec_ctx_t *)backend_ctx; |
| 371 | if (!ctx) return; |
| 372 | free(ctx->buf); |
| 373 | free(ctx); |
| 374 | } |
| 375 | |
| 376 | static const avx_video_decoder_ops_t s_shq_decoder_ops = { |
| 377 | .name = "speedhq-cpu", |
| 378 | .priority = 50, |
| 379 | .is_hardware = false, |
| 380 | .supports_fourcc = shq_dec_supports_fourcc, |
| 381 | .create = shq_dec_create, |
| 382 | .decode = shq_dec_decode, |
| 383 | .reset = shq_dec_reset, |
| 384 | .destroy = shq_dec_destroy |
| 385 | }; |
| 386 | |
| 387 | /* ========================================================================= */ |
| 388 | /* Registry & Selection Helpers */ |
| 389 | /* ========================================================================= */ |
| 390 | |
| 391 | const avx_video_decoder_ops_t *avx_video_v4l2_get_ops(void); |
| 392 | const avx_video_decoder_ops_t *avx_video_avcodec_get_ops(void); |
| 393 | const avx_video_decoder_ops_t *avx_video_mf_get_ops(void); |
| 394 | const avx_video_decoder_ops_t *avx_video_vt_get_ops(void); |
| 395 | const avx_video_encoder_ops_t *avx_video_vt_get_encoder_ops(void); |
| 396 | const avx_video_encoder_ops_t *avx_video_avcodec_get_encoder_ops(void); |
| 397 | |
| 398 | static void register_dec_backend_locked(const avx_video_decoder_ops_t *ops) { |
| 399 | if (!ops || s_dec_count >= AVX_MAX_VIDEO_BACKENDS) return; |
| 400 | for (int i = 0; i < s_dec_count; ++i) { |
| 401 | if (s_dec_backends[i] == ops) return; |
| 402 | } |
| 403 | s_dec_backends[s_dec_count++] = ops; |
| 404 | } |
| 405 | |
| 406 | static void register_enc_backend_locked(const avx_video_encoder_ops_t *ops) { |
| 407 | if (!ops || s_enc_count >= AVX_MAX_VIDEO_BACKENDS) return; |
| 408 | for (int i = 0; i < s_enc_count; ++i) { |
| 409 | if (s_enc_backends[i] == ops) return; |
| 410 | } |
| 411 | s_enc_backends[s_enc_count++] = ops; |
| 412 | } |
| 413 | |
| 414 | static void avx_video_codec_register_builtins_locked(void) { |
| 415 | if (s_vcodec_initialized) return; |
| 416 | |
| 417 | avx_speedhq_init(); |
| 418 | |
| 419 | register_enc_backend_locked(&s_shq_encoder_ops); |
| 420 | register_enc_backend_locked(avx_video_vt_get_encoder_ops()); |
| 421 | register_enc_backend_locked(avx_video_avcodec_get_encoder_ops()); |
| 422 | |
| 423 | register_dec_backend_locked(&s_shq_decoder_ops); |
| 424 | register_dec_backend_locked(avx_video_v4l2_get_ops()); |
| 425 | register_dec_backend_locked(avx_video_avcodec_get_ops()); |
| 426 | register_dec_backend_locked(avx_video_mf_get_ops()); |
| 427 | register_dec_backend_locked(avx_video_vt_get_ops()); |
| 428 | |
| 429 | s_vcodec_initialized = true; |
| 430 | } |
| 431 | |
| 432 | bool avx_video_codec_init(void) { |
| 433 | avx_mutex_lock(&s_vcodec_lock); |
| 434 | avx_video_codec_register_builtins_locked(); |
| 435 | avx_mutex_unlock(&s_vcodec_lock); |
| 436 | return true; |
| 437 | } |
| 438 | |
| 439 | void avx_video_codec_destroy(void) { |
| 440 | avx_mutex_lock(&s_vcodec_lock); |
| 441 | s_enc_count = 0; |
| 442 | s_dec_count = 0; |
| 443 | s_vcodec_initialized = false; |
| 444 | avx_mutex_unlock(&s_vcodec_lock); |
| 445 | |
| 446 | avx_speedhq_destroy(); |
| 447 | } |
| 448 | |
| 449 | bool avx_video_encoder_register(const avx_video_encoder_ops_t *ops) { |
| 450 | if (!ops || !ops->supports_fourcc || !ops->create || !ops->encode || !ops->destroy) { |
| 451 | return false; |
| 452 | } |
| 453 | avx_mutex_lock(&s_vcodec_lock); |
| 454 | avx_video_codec_register_builtins_locked(); |
| 455 | for (int i = 0; i < s_enc_count; ++i) { |
| 456 | if (s_enc_backends[i] == ops) { |
| 457 | avx_mutex_unlock(&s_vcodec_lock); |
| 458 | return true; |
| 459 | } |
| 460 | } |
| 461 | if (s_enc_count >= AVX_MAX_VIDEO_BACKENDS) { |
| 462 | avx_mutex_unlock(&s_vcodec_lock); |
| 463 | return false; |
| 464 | } |
| 465 | s_enc_backends[s_enc_count++] = ops; |
| 466 | avx_mutex_unlock(&s_vcodec_lock); |
| 467 | return true; |
| 468 | } |
| 469 | |
| 470 | bool avx_video_decoder_register(const avx_video_decoder_ops_t *ops) { |
| 471 | if (!ops || !ops->supports_fourcc || !ops->create || !ops->decode || !ops->destroy) { |
| 472 | return false; |
| 473 | } |
| 474 | avx_mutex_lock(&s_vcodec_lock); |
| 475 | avx_video_codec_register_builtins_locked(); |
| 476 | for (int i = 0; i < s_dec_count; ++i) { |
| 477 | if (s_dec_backends[i] == ops) { |
| 478 | avx_mutex_unlock(&s_vcodec_lock); |
| 479 | return true; |
| 480 | } |
| 481 | } |
| 482 | if (s_dec_count >= AVX_MAX_VIDEO_BACKENDS) { |
| 483 | avx_mutex_unlock(&s_vcodec_lock); |
| 484 | return false; |
| 485 | } |
| 486 | s_dec_backends[s_dec_count++] = ops; |
| 487 | avx_mutex_unlock(&s_vcodec_lock); |
| 488 | return true; |
| 489 | } |
| 490 | |
| 491 | static const avx_video_encoder_ops_t *find_encoder_backend_locked(uint32_t target_fourcc) { |
| 492 | const avx_video_encoder_ops_t *best = NULL; |
| 493 | for (int i = 0; i < s_enc_count; ++i) { |
| 494 | const avx_video_encoder_ops_t *ops = s_enc_backends[i]; |
| 495 | if (!ops || !ops->supports_fourcc(target_fourcc)) continue; |
| 496 | if (!best || ops->priority > best->priority) { |
| 497 | best = ops; |
| 498 | } |
| 499 | } |
| 500 | return best; |
| 501 | } |
| 502 | |
| 503 | static const avx_video_decoder_ops_t *find_decoder_backend_locked(uint32_t src_fourcc) { |
| 504 | const avx_video_decoder_ops_t *best = NULL; |
| 505 | for (int i = 0; i < s_dec_count; ++i) { |
| 506 | const avx_video_decoder_ops_t *ops = s_dec_backends[i]; |
| 507 | if (!ops || !ops->supports_fourcc(src_fourcc)) continue; |
| 508 | if (!best || ops->priority > best->priority) { |
| 509 | best = ops; |
| 510 | } |
| 511 | } |
| 512 | return best; |
| 513 | } |
| 514 | |
| 515 | bool avx_video_encoder_has_backend(uint32_t target_fourcc) { |
| 516 | avx_mutex_lock(&s_vcodec_lock); |
| 517 | avx_video_codec_register_builtins_locked(); |
| 518 | bool ok = (find_encoder_backend_locked(target_fourcc) != NULL); |
| 519 | avx_mutex_unlock(&s_vcodec_lock); |
| 520 | return ok; |
| 521 | } |
| 522 | |
| 523 | bool avx_video_decoder_has_backend(uint32_t src_fourcc) { |
| 524 | avx_mutex_lock(&s_vcodec_lock); |
| 525 | avx_video_codec_register_builtins_locked(); |
| 526 | bool ok = (find_decoder_backend_locked(src_fourcc) != NULL); |
| 527 | avx_mutex_unlock(&s_vcodec_lock); |
| 528 | return ok; |
| 529 | } |
| 530 | |
| 531 | /* ========================================================================= */ |
| 532 | /* Per-Stream Video Encoder API */ |
| 533 | /* ========================================================================= */ |
| 534 | |
| 535 | /* Probes and initializes the highest-priority encoder backend supporting the requested FourCC. */ |
| 536 | avx_video_encoder_t *avx_video_encoder_create(const avx_video_enc_cfg_t *cfg) { |
| 537 | if (!cfg || cfg->fourcc == 0) return NULL; |
| 538 | |
| 539 | avx_mutex_lock(&s_vcodec_lock); |
| 540 | avx_video_codec_register_builtins_locked(); |
| 541 | |
| 542 | const avx_video_encoder_ops_t *cands[AVX_MAX_VIDEO_BACKENDS]; |
| 543 | int n_cands = 0; |
| 544 | for (int i = 0; i < s_enc_count; ++i) { |
| 545 | if (s_enc_backends[i] && s_enc_backends[i]->supports_fourcc(cfg->fourcc)) { |
| 546 | cands[n_cands++] = s_enc_backends[i]; |
| 547 | } |
| 548 | } |
| 549 | avx_mutex_unlock(&s_vcodec_lock); |
| 550 | |
| 551 | /* Sort candidate pointers descending by priority without mutating registry */ |
| 552 | for (int i = 0; i < n_cands; ++i) { |
| 553 | for (int j = i + 1; j < n_cands; ++j) { |
| 554 | if (cands[j]->priority > cands[i]->priority) { |
| 555 | const avx_video_encoder_ops_t *tmp = cands[i]; |
| 556 | cands[i] = cands[j]; |
| 557 | cands[j] = tmp; |
| 558 | } |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | for (int i = 0; i < n_cands; ++i) { |
| 563 | void *backend_ctx = NULL; |
| 564 | if (cands[i]->create(cfg, &backend_ctx) == 0 && backend_ctx) { |
| 565 | avx_video_encoder_t *enc = (avx_video_encoder_t *)calloc(1, sizeof(*enc)); |
| 566 | if (!enc) { |
| 567 | cands[i]->destroy(backend_ctx); |
| 568 | return NULL; |
| 569 | } |
| 570 | enc->ops = cands[i]; |
| 571 | enc->backend_ctx = backend_ctx; |
| 572 | enc->cfg = *cfg; |
| 573 | return enc; |
| 574 | } |
| 575 | } |
| 576 | return NULL; |
| 577 | } |
| 578 | |
| 579 | void avx_video_encoder_destroy(avx_video_encoder_t *enc) { |
| 580 | if (!enc) return; |
| 581 | if (enc->ops && enc->ops->destroy && enc->backend_ctx) { |
| 582 | enc->ops->destroy(enc->backend_ctx); |
| 583 | } |
| 584 | free(enc); |
| 585 | } |
| 586 | |
| 587 | int avx_video_encoder_encode(avx_video_encoder_t *enc, |
| 588 | const oavx_video_frame_t *in_frame, |
| 589 | uint8_t *out_buf, size_t out_cap, |
| 590 | size_t *out_size, bool *out_is_keyframe) { |
| 591 | if (!enc || !enc->ops || !enc->ops->encode) return -1; |
| 592 | return enc->ops->encode(enc->backend_ctx, in_frame, out_buf, out_cap, out_size, out_is_keyframe); |
| 593 | } |
| 594 | |
| 595 | int avx_video_encoder_set_quality(avx_video_encoder_t *enc, uint8_t quality) { |
| 596 | if (!enc || !enc->ops) return -1; |
| 597 | enc->cfg.quality = quality; |
| 598 | if (enc->ops->set_quality) { |
| 599 | return enc->ops->set_quality(enc->backend_ctx, quality); |
| 600 | } |
| 601 | return 0; |
| 602 | } |
| 603 | |
| 604 | void avx_video_encoder_request_keyframe(avx_video_encoder_t *enc) { |
| 605 | if (!enc || !enc->ops || !enc->ops->request_keyframe) return; |
| 606 | enc->ops->request_keyframe(enc->backend_ctx); |
| 607 | } |
| 608 | |
| 609 | uint32_t avx_video_encoder_get_fourcc(const avx_video_encoder_t *enc) { |
| 610 | return enc ? enc->cfg.fourcc : 0; |
| 611 | } |
| 612 | |
| 613 | const char *avx_video_encoder_get_backend_name(const avx_video_encoder_t *enc) { |
| 614 | if (!enc || !enc->ops) return "none"; |
| 615 | if (enc->ops->get_name && enc->backend_ctx) { |
| 616 | return enc->ops->get_name(enc->backend_ctx); |
| 617 | } |
| 618 | return enc->ops->name ? enc->ops->name : "none"; |
| 619 | } |
| 620 | |
| 621 | /* ========================================================================= */ |
| 622 | /* Per-Stream Video Decoder API */ |
| 623 | /* ========================================================================= */ |
| 624 | |
| 625 | /* Probes and initializes the highest-priority decoder backend supporting the requested FourCC. */ |
| 626 | avx_video_decoder_t *avx_video_decoder_create(const avx_video_dec_cfg_t *cfg) { |
| 627 | if (!cfg || cfg->fourcc == 0) return NULL; |
| 628 | |
| 629 | avx_mutex_lock(&s_vcodec_lock); |
| 630 | avx_video_codec_register_builtins_locked(); |
| 631 | |
| 632 | const avx_video_decoder_ops_t *cands[AVX_MAX_VIDEO_BACKENDS]; |
| 633 | int n_cands = 0; |
| 634 | for (int i = 0; i < s_dec_count; ++i) { |
| 635 | if (s_dec_backends[i] && s_dec_backends[i]->supports_fourcc(cfg->fourcc)) { |
| 636 | cands[n_cands++] = s_dec_backends[i]; |
| 637 | } |
| 638 | } |
| 639 | avx_mutex_unlock(&s_vcodec_lock); |
| 640 | |
| 641 | for (int i = 0; i < n_cands; ++i) { |
| 642 | for (int j = i + 1; j < n_cands; ++j) { |
| 643 | if (cands[j]->priority > cands[i]->priority) { |
| 644 | const avx_video_decoder_ops_t *tmp = cands[i]; |
| 645 | cands[i] = cands[j]; |
| 646 | cands[j] = tmp; |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | for (int i = 0; i < n_cands; ++i) { |
| 652 | void *backend_ctx = NULL; |
| 653 | if (cands[i]->create(cfg, &backend_ctx) == 0 && backend_ctx) { |
| 654 | avx_video_decoder_t *dec = (avx_video_decoder_t *)calloc(1, sizeof(*dec)); |
| 655 | if (!dec) { |
| 656 | cands[i]->destroy(backend_ctx); |
| 657 | return NULL; |
| 658 | } |
| 659 | dec->ops = cands[i]; |
| 660 | dec->backend_ctx = backend_ctx; |
| 661 | dec->cfg = *cfg; |
| 662 | return dec; |
| 663 | } |
| 664 | } |
| 665 | return NULL; |
| 666 | } |
| 667 | |
| 668 | void avx_video_decoder_destroy(avx_video_decoder_t *dec) { |
| 669 | if (!dec) return; |
| 670 | if (dec->ops && dec->ops->destroy && dec->backend_ctx) { |
| 671 | dec->ops->destroy(dec->backend_ctx); |
| 672 | } |
| 673 | free(dec->annexb_buf); |
| 674 | free(dec); |
| 675 | } |
| 676 | |
| 677 | static bool is_avcc_bitstream(const uint8_t *data, size_t size, bool is_hevc) { |
| 678 | if (!data || size < 5) return false; |
| 679 | /* If already Annex-B with 3-byte or 4-byte start code, not AVCC */ |
| 680 | if (data[0] == 0 && data[1] == 0 && |
| 681 | (data[2] == 1 || (data[2] == 0 && size >= 4 && data[3] == 1))) { |
| 682 | return false; |
| 683 | } |
| 684 | |
| 685 | size_t pos = 0; |
| 686 | int nal_count = 0; |
| 687 | while (pos + 4 <= size) { |
| 688 | uint32_t nlen = ((uint32_t)data[pos] << 24) | |
| 689 | ((uint32_t)data[pos + 1] << 16) | |
| 690 | ((uint32_t)data[pos + 2] << 8) | |
| 691 | (uint32_t)data[pos + 3]; |
| 692 | if (nlen == 0 || pos + 4 + nlen > size) { |
| 693 | return false; |
| 694 | } |
| 695 | uint8_t nh = data[pos + 4]; |
| 696 | if (nh & 0x80) { |
| 697 | return false; /* forbidden_zero_bit must be 0 in H.264 and HEVC */ |
| 698 | } |
| 699 | if (!is_hevc) { |
| 700 | uint8_t ntype = nh & 0x1F; |
| 701 | if (ntype == 0 || ntype > 23) return false; |
| 702 | } else { |
| 703 | uint8_t ntype = (nh >> 1) & 0x3F; |
| 704 | if (ntype > 40) return false; |
| 705 | } |
| 706 | pos += 4 + nlen; |
| 707 | nal_count++; |
| 708 | } |
| 709 | return (pos == size && nal_count > 0); |
| 710 | } |
| 711 | |
| 712 | /* Decodes compressed video packet, converting Annex-B start codes to AVCC/HVCC if required by backend. */ |
| 713 | int avx_video_decoder_decode(avx_video_decoder_t *dec, |
| 714 | const uint8_t *pkt_data, size_t pkt_size, |
| 715 | oavx_video_frame_t *out_frame) { |
| 716 | if (!dec || !dec->ops || !dec->ops->decode) return -1; |
| 717 | if (!pkt_data || pkt_size == 0 || !out_frame) return -1; |
| 718 | |
| 719 | const uint8_t *send_data = pkt_data; |
| 720 | size_t send_size = pkt_size; |
| 721 | |
| 722 | bool is_h264 = (dec->cfg.fourcc == OAVX_FOURCC_VIDEO_TYPE_H264 || dec->cfg.fourcc == 0x34363268U); |
| 723 | bool is_hevc = (dec->cfg.fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC || dec->cfg.fourcc == 0x43564568U); |
| 724 | |
| 725 | if ((is_h264 || is_hevc) && is_avcc_bitstream(pkt_data, pkt_size, is_hevc)) { |
| 726 | if (dec->annexb_cap < pkt_size) { |
| 727 | uint8_t *nb = (uint8_t *)realloc(dec->annexb_buf, pkt_size); |
| 728 | if (nb) { |
| 729 | dec->annexb_buf = nb; |
| 730 | dec->annexb_cap = pkt_size; |
| 731 | } |
| 732 | } |
| 733 | if (dec->annexb_buf && dec->annexb_cap >= pkt_size) { |
| 734 | memcpy(dec->annexb_buf, pkt_data, pkt_size); |
| 735 | size_t pos = 0; |
| 736 | while (pos + 4 <= pkt_size) { |
| 737 | uint32_t nlen = ((uint32_t)dec->annexb_buf[pos] << 24) | |
| 738 | ((uint32_t)dec->annexb_buf[pos + 1] << 16) | |
| 739 | ((uint32_t)dec->annexb_buf[pos + 2] << 8) | |
| 740 | (uint32_t)dec->annexb_buf[pos + 3]; |
| 741 | dec->annexb_buf[pos + 0] = 0x00; |
| 742 | dec->annexb_buf[pos + 1] = 0x00; |
| 743 | dec->annexb_buf[pos + 2] = 0x00; |
| 744 | dec->annexb_buf[pos + 3] = 0x01; |
| 745 | pos += 4 + nlen; |
| 746 | } |
| 747 | send_data = dec->annexb_buf; |
| 748 | send_size = pkt_size; |
| 749 | } |
| 750 | } |
| 751 | |
| 752 | return dec->ops->decode(dec->backend_ctx, send_data, send_size, out_frame); |
| 753 | } |
| 754 | |
| 755 | void avx_video_decoder_reset(avx_video_decoder_t *dec) { |
| 756 | if (!dec || !dec->ops || !dec->ops->reset) return; |
| 757 | dec->ops->reset(dec->backend_ctx); |
| 758 | } |
| 759 | |
| 760 | uint32_t avx_video_decoder_get_fourcc(const avx_video_decoder_t *dec) { |
| 761 | return dec ? dec->cfg.fourcc : 0; |
| 762 | } |
| 763 | |
| 764 | const char *avx_video_decoder_get_backend_name(const avx_video_decoder_t *dec) { |
| 765 | return (dec && dec->ops && dec->ops->name) ? dec->ops->name : "none"; |
| 766 | } |