+ +
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_avcodec.c - FFmpeg libavcodec Runtime-Loaded Video Decoder & Encoder Backend |
| 7 | */ |
| 8 | |
| 9 | #include "avx_video_codec.h" |
| 10 | #include "avx_internal.h" |
| 11 | #include "avx_color.h" |
| 12 | |
| 13 | #if defined(AVX_OS_LINUX) || defined(AVX_OS_WINDOWS) |
| 14 | |
| 15 | #include <errno.h> |
| 16 | #include <limits.h> |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | |
| 21 | #define AVX_AV_CODEC_ID_H264 27 |
| 22 | #define AVX_AV_CODEC_ID_HEVC 173 |
| 23 | #define AVX_AV_CODEC_ID_AV1 226 |
| 24 | #define AVX_AV_CODEC_ID_MJPEG 7 |
| 25 | #define AVX_AV_NUM_DATA_POINTERS 8 |
| 26 | #define AVX_AV_LOG_QUIET (-8) |
| 27 | #define AVX_AV_LOG_FATAL 8 |
| 28 | #define AVX_AV_LOG_ERROR 16 |
| 29 | |
| 30 | typedef struct avx_av_packet_stub_t { |
| 31 | void *buf; |
| 32 | int64_t pts; |
| 33 | int64_t dts; |
| 34 | uint8_t *data; |
| 35 | int size; |
| 36 | int stream_index; |
| 37 | int flags; |
| 38 | } avx_av_packet_stub_t; |
| 39 | |
| 40 | typedef struct avx_av_frame_stub_t { |
| 41 | uint8_t *data[AVX_AV_NUM_DATA_POINTERS]; |
| 42 | int linesize[AVX_AV_NUM_DATA_POINTERS]; |
| 43 | uint8_t **extended_data; |
| 44 | int width; |
| 45 | int height; |
| 46 | int nb_samples; |
| 47 | int format; |
| 48 | int key_frame; |
| 49 | int pict_type; |
| 50 | int sar_num; |
| 51 | int sar_den; |
| 52 | int64_t pts; |
| 53 | } avx_av_frame_stub_t; |
| 54 | |
| 55 | typedef unsigned (*fn_avcodec_version)(void); |
| 56 | typedef unsigned (*fn_avutil_version)(void); |
| 57 | typedef void *(*fn_avcodec_find_decoder)(int id); |
| 58 | typedef void *(*fn_avcodec_find_decoder_by_name)(const char *name); |
| 59 | typedef void *(*fn_avcodec_find_encoder)(int id); |
| 60 | typedef void *(*fn_avcodec_find_encoder_by_name)(const char *name); |
| 61 | typedef void *(*fn_avcodec_alloc_context3)(const void *codec); |
| 62 | typedef void (*fn_avcodec_free_context)(void **avctx); |
| 63 | typedef int (*fn_avcodec_open2)(void *avctx, const void *codec, void **options); |
| 64 | typedef int (*fn_avcodec_send_packet)(void *avctx, const void *pkt); |
| 65 | typedef int (*fn_avcodec_receive_frame)(void *avctx, void *frame); |
| 66 | typedef int (*fn_avcodec_send_frame)(void *avctx, const void *frame); |
| 67 | typedef int (*fn_avcodec_receive_packet)(void *avctx, void *pkt); |
| 68 | typedef void (*fn_avcodec_flush_buffers)(void *avctx); |
| 69 | |
| 70 | typedef void *(*fn_av_packet_alloc)(void); |
| 71 | typedef void (*fn_av_packet_free)(void **pkt); |
| 72 | typedef int (*fn_av_new_packet)(void *pkt, int size); |
| 73 | typedef void (*fn_av_packet_unref)(void *pkt); |
| 74 | |
| 75 | typedef void *(*fn_av_frame_alloc)(void); |
| 76 | typedef void (*fn_av_frame_free)(void **frame); |
| 77 | typedef void (*fn_av_frame_unref)(void *frame); |
| 78 | typedef int (*fn_av_frame_get_buffer)(void *frame, int align); |
| 79 | typedef int (*fn_av_get_pix_fmt)(const char *name); |
| 80 | typedef int (*fn_av_opt_set)(void *obj, const char *name, const char *val, int search_flags); |
| 81 | typedef int (*fn_av_opt_set_int)(void *obj, const char *name, int64_t val, int search_flags); |
| 82 | typedef void (*fn_av_log_set_level)(int level); |
| 83 | |
| 84 | typedef struct { |
| 85 | const char *codec_soname; |
| 86 | const char *util_soname; |
| 87 | unsigned codec_major; |
| 88 | unsigned util_major; |
| 89 | } avx_ffmpeg_soname_pair_t; |
| 90 | |
| 91 | #if defined(AVX_OS_WINDOWS) |
| 92 | static const avx_ffmpeg_soname_pair_t k_ffmpeg_pairs[] = { |
| 93 | {"avcodec-62.dll", "avutil-60.dll", 62U, 60U}, /* FFmpeg 8.x */ |
| 94 | {"avcodec-61.dll", "avutil-59.dll", 61U, 59U}, /* FFmpeg 7.x */ |
| 95 | {"avcodec-60.dll", "avutil-58.dll", 60U, 58U} /* FFmpeg 6.x */ |
| 96 | }; |
| 97 | #else |
| 98 | static const avx_ffmpeg_soname_pair_t k_ffmpeg_pairs[] = { |
| 99 | {"libavcodec.so.62", "libavutil.so.60", 62U, 60U}, /* FFmpeg 8.x */ |
| 100 | {"libavcodec.so.61", "libavutil.so.59", 61U, 59U}, /* FFmpeg 7.x */ |
| 101 | {"libavcodec.so.60", "libavutil.so.58", 60U, 58U} /* FFmpeg 6.x */ |
| 102 | }; |
| 103 | #endif |
| 104 | |
| 105 | static struct { |
| 106 | avx_mutex_t lock; |
| 107 | bool probed; |
| 108 | bool available; |
| 109 | void *codec_handle; |
| 110 | void *util_handle; |
| 111 | |
| 112 | fn_avcodec_version avcodec_version; |
| 113 | fn_avcodec_find_decoder avcodec_find_decoder; |
| 114 | fn_avcodec_find_decoder_by_name avcodec_find_decoder_by_name; |
| 115 | fn_avcodec_find_encoder avcodec_find_encoder; |
| 116 | fn_avcodec_find_encoder_by_name avcodec_find_encoder_by_name; |
| 117 | fn_avcodec_alloc_context3 avcodec_alloc_context3; |
| 118 | fn_avcodec_free_context avcodec_free_context; |
| 119 | fn_avcodec_open2 avcodec_open2; |
| 120 | fn_avcodec_send_packet avcodec_send_packet; |
| 121 | fn_avcodec_receive_frame avcodec_receive_frame; |
| 122 | fn_avcodec_send_frame avcodec_send_frame; |
| 123 | fn_avcodec_receive_packet avcodec_receive_packet; |
| 124 | fn_avcodec_flush_buffers avcodec_flush_buffers; |
| 125 | fn_av_packet_alloc av_packet_alloc; |
| 126 | fn_av_packet_free av_packet_free; |
| 127 | fn_av_new_packet av_new_packet; |
| 128 | fn_av_packet_unref av_packet_unref; |
| 129 | |
| 130 | fn_avutil_version avutil_version; |
| 131 | fn_av_frame_alloc av_frame_alloc; |
| 132 | fn_av_frame_free av_frame_free; |
| 133 | fn_av_frame_unref av_frame_unref; |
| 134 | fn_av_frame_get_buffer av_frame_get_buffer; |
| 135 | fn_av_get_pix_fmt av_get_pix_fmt; |
| 136 | fn_av_opt_set av_opt_set; |
| 137 | fn_av_opt_set_int av_opt_set_int; |
| 138 | fn_av_log_set_level av_log_set_level; |
| 139 | |
| 140 | int fmt_yuv420p; |
| 141 | int fmt_yuvj420p; |
| 142 | int fmt_nv12; |
| 143 | int fmt_yuv422p; |
| 144 | int fmt_yuvj422p; |
| 145 | int fmt_yuv420p10le; |
| 146 | int fmt_yuv422p10le; |
| 147 | } g_avcodec = {.lock = AVX_MUTEX_INITIALIZER, |
| 148 | .probed = false, |
| 149 | .available = false, |
| 150 | .fmt_yuv420p = -1, |
| 151 | .fmt_yuvj420p = -1, |
| 152 | .fmt_nv12 = -1, |
| 153 | .fmt_yuv422p = -1, |
| 154 | .fmt_yuvj422p = -1, |
| 155 | .fmt_yuv420p10le = -1, |
| 156 | .fmt_yuv422p10le = -1}; |
| 157 | |
| 158 | /* Dynamically probes and binds libavcodec and libavutil across versions 6.x, 7.x, 8.x via dlopen. */ |
| 159 | static bool probe_avcodec_library(void) { |
| 160 | avx_mutex_lock(&g_avcodec.lock); |
| 161 | if (g_avcodec.probed) { |
| 162 | bool ok = g_avcodec.available; |
| 163 | avx_mutex_unlock(&g_avcodec.lock); |
| 164 | return ok; |
| 165 | } |
| 166 | g_avcodec.probed = true; |
| 167 | |
| 168 | for (size_t i = 0; i < sizeof(k_ffmpeg_pairs) / sizeof(k_ffmpeg_pairs[0]); ++i) { |
| 169 | const avx_ffmpeg_soname_pair_t *pair = &k_ffmpeg_pairs[i]; |
| 170 | void *h_util = avx_dlopen(pair->util_soname); |
| 171 | if (!h_util) { |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | void *h_codec = avx_dlopen(pair->codec_soname); |
| 176 | if (!h_codec) { |
| 177 | avx_dlclose(h_util); |
| 178 | continue; |
| 179 | } |
| 180 | |
| 181 | fn_avutil_version p_util_ver = (fn_avutil_version)avx_dlsym(h_util, "avutil_version"); |
| 182 | fn_avcodec_version p_codec_ver = (fn_avcodec_version)avx_dlsym(h_codec, "avcodec_version"); |
| 183 | if (!p_util_ver || !p_codec_ver) { |
| 184 | avx_dlclose(h_codec); |
| 185 | avx_dlclose(h_util); |
| 186 | continue; |
| 187 | } |
| 188 | |
| 189 | unsigned util_major = p_util_ver() >> 16; |
| 190 | unsigned codec_major = p_codec_ver() >> 16; |
| 191 | if (util_major != pair->util_major || codec_major != pair->codec_major) { |
| 192 | avx_dlclose(h_codec); |
| 193 | avx_dlclose(h_util); |
| 194 | continue; |
| 195 | } |
| 196 | |
| 197 | g_avcodec.avcodec_version = p_codec_ver; |
| 198 | g_avcodec.avcodec_find_decoder = (fn_avcodec_find_decoder)avx_dlsym(h_codec, "avcodec_find_decoder"); |
| 199 | g_avcodec.avcodec_find_decoder_by_name = |
| 200 | (fn_avcodec_find_decoder_by_name)avx_dlsym(h_codec, "avcodec_find_decoder_by_name"); |
| 201 | g_avcodec.avcodec_find_encoder = (fn_avcodec_find_encoder)avx_dlsym(h_codec, "avcodec_find_encoder"); |
| 202 | g_avcodec.avcodec_find_encoder_by_name = |
| 203 | (fn_avcodec_find_encoder_by_name)avx_dlsym(h_codec, "avcodec_find_encoder_by_name"); |
| 204 | g_avcodec.avcodec_alloc_context3 = (fn_avcodec_alloc_context3)avx_dlsym(h_codec, "avcodec_alloc_context3"); |
| 205 | g_avcodec.avcodec_free_context = (fn_avcodec_free_context)avx_dlsym(h_codec, "avcodec_free_context"); |
| 206 | g_avcodec.avcodec_open2 = (fn_avcodec_open2)avx_dlsym(h_codec, "avcodec_open2"); |
| 207 | g_avcodec.avcodec_send_packet = (fn_avcodec_send_packet)avx_dlsym(h_codec, "avcodec_send_packet"); |
| 208 | g_avcodec.avcodec_receive_frame = (fn_avcodec_receive_frame)avx_dlsym(h_codec, "avcodec_receive_frame"); |
| 209 | g_avcodec.avcodec_send_frame = (fn_avcodec_send_frame)avx_dlsym(h_codec, "avcodec_send_frame"); |
| 210 | g_avcodec.avcodec_receive_packet = (fn_avcodec_receive_packet)avx_dlsym(h_codec, "avcodec_receive_packet"); |
| 211 | g_avcodec.avcodec_flush_buffers = (fn_avcodec_flush_buffers)avx_dlsym(h_codec, "avcodec_flush_buffers"); |
| 212 | g_avcodec.av_packet_alloc = (fn_av_packet_alloc)avx_dlsym(h_codec, "av_packet_alloc"); |
| 213 | g_avcodec.av_packet_free = (fn_av_packet_free)avx_dlsym(h_codec, "av_packet_free"); |
| 214 | g_avcodec.av_new_packet = (fn_av_new_packet)avx_dlsym(h_codec, "av_new_packet"); |
| 215 | g_avcodec.av_packet_unref = (fn_av_packet_unref)avx_dlsym(h_codec, "av_packet_unref"); |
| 216 | |
| 217 | g_avcodec.avutil_version = p_util_ver; |
| 218 | g_avcodec.av_frame_alloc = (fn_av_frame_alloc)avx_dlsym(h_util, "av_frame_alloc"); |
| 219 | g_avcodec.av_frame_free = (fn_av_frame_free)avx_dlsym(h_util, "av_frame_free"); |
| 220 | g_avcodec.av_frame_unref = (fn_av_frame_unref)avx_dlsym(h_util, "av_frame_unref"); |
| 221 | g_avcodec.av_frame_get_buffer = (fn_av_frame_get_buffer)avx_dlsym(h_util, "av_frame_get_buffer"); |
| 222 | g_avcodec.av_get_pix_fmt = (fn_av_get_pix_fmt)avx_dlsym(h_util, "av_get_pix_fmt"); |
| 223 | g_avcodec.av_opt_set = (fn_av_opt_set)avx_dlsym(h_util, "av_opt_set"); |
| 224 | g_avcodec.av_opt_set_int = (fn_av_opt_set_int)avx_dlsym(h_util, "av_opt_set_int"); |
| 225 | g_avcodec.av_log_set_level = (fn_av_log_set_level)avx_dlsym(h_util, "av_log_set_level"); |
| 226 | |
| 227 | if (!g_avcodec.avcodec_find_decoder || !g_avcodec.avcodec_alloc_context3 || !g_avcodec.avcodec_free_context || |
| 228 | !g_avcodec.avcodec_open2 || !g_avcodec.avcodec_send_packet || !g_avcodec.avcodec_receive_frame || |
| 229 | !g_avcodec.av_packet_alloc || !g_avcodec.av_packet_free || !g_avcodec.av_new_packet || |
| 230 | !g_avcodec.av_packet_unref || !g_avcodec.av_frame_alloc || !g_avcodec.av_frame_free || |
| 231 | !g_avcodec.av_frame_unref || !g_avcodec.av_get_pix_fmt) { |
| 232 | avx_dlclose(h_codec); |
| 233 | avx_dlclose(h_util); |
| 234 | continue; |
| 235 | } |
| 236 | |
| 237 | if (g_avcodec.av_log_set_level) { |
| 238 | g_avcodec.av_log_set_level(AVX_AV_LOG_FATAL); |
| 239 | } |
| 240 | |
| 241 | g_avcodec.fmt_yuv420p = g_avcodec.av_get_pix_fmt("yuv420p"); |
| 242 | g_avcodec.fmt_yuvj420p = g_avcodec.av_get_pix_fmt("yuvj420p"); |
| 243 | g_avcodec.fmt_nv12 = g_avcodec.av_get_pix_fmt("nv12"); |
| 244 | g_avcodec.fmt_yuv422p = g_avcodec.av_get_pix_fmt("yuv422p"); |
| 245 | g_avcodec.fmt_yuvj422p = g_avcodec.av_get_pix_fmt("yuvj422p"); |
| 246 | g_avcodec.fmt_yuv420p10le = g_avcodec.av_get_pix_fmt("yuv420p10le"); |
| 247 | g_avcodec.fmt_yuv422p10le = g_avcodec.av_get_pix_fmt("yuv422p10le"); |
| 248 | |
| 249 | if (g_avcodec.fmt_yuv420p < 0 && g_avcodec.fmt_nv12 < 0) { |
| 250 | avx_dlclose(h_codec); |
| 251 | avx_dlclose(h_util); |
| 252 | continue; |
| 253 | } |
| 254 | |
| 255 | g_avcodec.codec_handle = h_codec; |
| 256 | g_avcodec.util_handle = h_util; |
| 257 | g_avcodec.available = true; |
| 258 | break; |
| 259 | } |
| 260 | |
| 261 | bool ok = g_avcodec.available; |
| 262 | avx_mutex_unlock(&g_avcodec.lock); |
| 263 | return ok; |
| 264 | } |
| 265 | |
| 266 | typedef struct avx_avcodec_dec_ctx_t { |
| 267 | void *avctx; |
| 268 | void *pkt; |
| 269 | void *frame; |
| 270 | uint32_t src_fourcc; |
| 271 | uint8_t *frame_buf; |
| 272 | size_t frame_buf_size; |
| 273 | } avx_avcodec_dec_ctx_t; |
| 274 | |
| 275 | static void *find_decoder_for_fourcc(uint32_t fourcc) { |
| 276 | if (!probe_avcodec_library()) { |
| 277 | return NULL; |
| 278 | } |
| 279 | |
| 280 | static const char *k_h264_dec_cands[] = {"h264", NULL}; |
| 281 | static const char *k_hevc_dec_cands[] = {"hevc", NULL}; |
| 282 | static const char *k_av1_dec_cands[] = {"libdav1d", "av1", "libaom-av1", NULL}; |
| 283 | static const char *k_mjpeg_dec_cands[] = {"mjpeg", NULL}; |
| 284 | |
| 285 | const char **cands = NULL; |
| 286 | int fallback_codec_id = 0; |
| 287 | |
| 288 | if (fourcc == OAVX_FOURCC_VIDEO_TYPE_H264) { |
| 289 | cands = k_h264_dec_cands; |
| 290 | fallback_codec_id = AVX_AV_CODEC_ID_H264; |
| 291 | } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC || fourcc == 0x43564568U) { |
| 292 | cands = k_hevc_dec_cands; |
| 293 | fallback_codec_id = AVX_AV_CODEC_ID_HEVC; |
| 294 | } else if (fourcc == OAVX_FOURCC_VIDEO_TYPE_AV01) { |
| 295 | cands = k_av1_dec_cands; |
| 296 | fallback_codec_id = AVX_AV_CODEC_ID_AV1; |
| 297 | } else if (fourcc == 0x47504A4D || fourcc == 0x4A50474D) { |
| 298 | cands = k_mjpeg_dec_cands; |
| 299 | fallback_codec_id = AVX_AV_CODEC_ID_MJPEG; |
| 300 | } else { |
| 301 | return NULL; |
| 302 | } |
| 303 | |
| 304 | if (cands && g_avcodec.avcodec_find_decoder_by_name) { |
| 305 | for (int i = 0; cands[i]; ++i) { |
| 306 | void *dec = g_avcodec.avcodec_find_decoder_by_name(cands[i]); |
| 307 | if (dec) return dec; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | if (fallback_codec_id != 0 && g_avcodec.avcodec_find_decoder) { |
| 312 | return g_avcodec.avcodec_find_decoder(fallback_codec_id); |
| 313 | } |
| 314 | return NULL; |
| 315 | } |
| 316 | |
| 317 | static bool avcodec_dec_supports_fourcc(uint32_t fourcc) { |
| 318 | return find_decoder_for_fourcc(fourcc) != NULL; |
| 319 | } |
| 320 | |
| 321 | static void avcodec_dec_destroy(void *backend_ctx) { |
| 322 | avx_avcodec_dec_ctx_t *ctx = (avx_avcodec_dec_ctx_t *)backend_ctx; |
| 323 | if (!ctx) return; |
| 324 | |
| 325 | if (ctx->avctx && g_avcodec.avcodec_free_context) { |
| 326 | g_avcodec.avcodec_free_context(&ctx->avctx); |
| 327 | } |
| 328 | if (ctx->pkt && g_avcodec.av_packet_free) { |
| 329 | g_avcodec.av_packet_free(&ctx->pkt); |
| 330 | } |
| 331 | if (ctx->frame && g_avcodec.av_frame_free) { |
| 332 | g_avcodec.av_frame_free(&ctx->frame); |
| 333 | } |
| 334 | free(ctx->frame_buf); |
| 335 | free(ctx); |
| 336 | } |
| 337 | |
| 338 | /* Allocates FFmpeg AVCodecContext configured for ultra-low latency slice-threaded decoding. */ |
| 339 | static int avcodec_dec_create(const avx_video_dec_cfg_t *cfg, void **backend_ctx) { |
| 340 | if (!cfg || !backend_ctx) return -1; |
| 341 | if (!probe_avcodec_library()) return -1; |
| 342 | |
| 343 | void *codec = find_decoder_for_fourcc(cfg->fourcc); |
| 344 | if (!codec) return -1; |
| 345 | |
| 346 | void *avctx = g_avcodec.avcodec_alloc_context3(codec); |
| 347 | if (!avctx) return -1; |
| 348 | |
| 349 | /* Low-delay slice threading prevents multi-frame buffering latency */ |
| 350 | if (g_avcodec.av_opt_set) { |
| 351 | g_avcodec.av_opt_set(avctx, "flags", "+low_delay", 0); |
| 352 | if (cfg->fourcc == OAVX_FOURCC_VIDEO_TYPE_H264 || cfg->fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC) { |
| 353 | g_avcodec.av_opt_set(avctx, "thread_type", "slice", 0); |
| 354 | } |
| 355 | } |
| 356 | if (g_avcodec.av_opt_set_int) { |
| 357 | g_avcodec.av_opt_set_int(avctx, "threads", 2, 0); |
| 358 | } |
| 359 | |
| 360 | if (g_avcodec.avcodec_open2(avctx, codec, NULL) < 0) { |
| 361 | g_avcodec.avcodec_free_context(&avctx); |
| 362 | return -1; |
| 363 | } |
| 364 | |
| 365 | avx_avcodec_dec_ctx_t *ctx = (avx_avcodec_dec_ctx_t *)calloc(1, sizeof(*ctx)); |
| 366 | if (!ctx) { |
| 367 | g_avcodec.avcodec_free_context(&avctx); |
| 368 | return -1; |
| 369 | } |
| 370 | |
| 371 | ctx->avctx = avctx; |
| 372 | ctx->src_fourcc = cfg->fourcc; |
| 373 | ctx->pkt = g_avcodec.av_packet_alloc(); |
| 374 | ctx->frame = g_avcodec.av_frame_alloc(); |
| 375 | if (!ctx->pkt || !ctx->frame) { |
| 376 | avcodec_dec_destroy(ctx); |
| 377 | return -1; |
| 378 | } |
| 379 | |
| 380 | *backend_ctx = ctx; |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | static int avcodec_ensure_frame_buf(avx_avcodec_dec_ctx_t *ctx, uint64_t needed_u64) { |
| 385 | if (needed_u64 == 0 || needed_u64 > AVX_MAX_DECOMPRESSED_FRAME_SIZE) { |
| 386 | return -1; |
| 387 | } |
| 388 | size_t needed = (size_t)needed_u64; |
| 389 | if (ctx->frame_buf_size < needed) { |
| 390 | uint8_t *nb = (uint8_t *)realloc(ctx->frame_buf, needed); |
| 391 | if (!nb) return -1; |
| 392 | ctx->frame_buf = nb; |
| 393 | ctx->frame_buf_size = needed; |
| 394 | } |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* Converts decoded AVFrame pixel formats (NV12, I420, P010) into OpenAVX destination layout. */ |
| 399 | static int avcodec_copy_out_frame(avx_avcodec_dec_ctx_t *ctx, oavx_video_frame_t *out_frame) { |
| 400 | const avx_av_frame_stub_t *f = (const avx_av_frame_stub_t *)ctx->frame; |
| 401 | if (f->width <= 0 || f->height <= 0 || f->width > AVX_MAX_VIDEO_WIDTH || f->height > AVX_MAX_VIDEO_HEIGHT) { |
| 402 | return AVX_VIDEO_CODEC_ERROR; |
| 403 | } |
| 404 | |
| 405 | uint32_t w = (uint32_t)f->width; |
| 406 | uint32_t h = (uint32_t)f->height; |
| 407 | int fmt = f->format; |
| 408 | |
| 409 | if (fmt >= 0 && (fmt == g_avcodec.fmt_yuv420p || fmt == g_avcodec.fmt_yuvj420p)) { |
| 410 | uint32_t cw = (w + 1U) / 2U; |
| 411 | uint32_t ch = (h + 1U) / 2U; |
| 412 | if (!f->data[0] || !f->data[1] || !f->data[2] || f->linesize[0] < (int)w || f->linesize[1] < (int)cw || |
| 413 | f->linesize[2] < (int)cw) { |
| 414 | return AVX_VIDEO_CODEC_ERROR; |
| 415 | } |
| 416 | uint64_t needed = (uint64_t)w * h + 2ULL * (uint64_t)cw * ch; |
| 417 | if (avcodec_ensure_frame_buf(ctx, needed) != 0) { |
| 418 | return AVX_VIDEO_CODEC_ERROR; |
| 419 | } |
| 420 | |
| 421 | for (uint32_t y = 0; y < h; ++y) { |
| 422 | memcpy(ctx->frame_buf + (size_t)y * w, f->data[0] + (size_t)y * (size_t)f->linesize[0], w); |
| 423 | } |
| 424 | uint8_t *dst_u = ctx->frame_buf + (size_t)w * h; |
| 425 | for (uint32_t y = 0; y < ch; ++y) { |
| 426 | memcpy(dst_u + (size_t)y * cw, f->data[1] + (size_t)y * (size_t)f->linesize[1], cw); |
| 427 | } |
| 428 | uint8_t *dst_v = dst_u + (size_t)cw * ch; |
| 429 | for (uint32_t y = 0; y < ch; ++y) { |
| 430 | memcpy(dst_v + (size_t)y * cw, f->data[2] + (size_t)y * (size_t)f->linesize[2], cw); |
| 431 | } |
| 432 | |
| 433 | out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_I420; |
| 434 | out_frame->width = w; |
| 435 | out_frame->height = h; |
| 436 | out_frame->planes[0] = ctx->frame_buf; |
| 437 | out_frame->strides[0] = w; |
| 438 | out_frame->planes[1] = dst_u; |
| 439 | out_frame->strides[1] = cw; |
| 440 | out_frame->planes[2] = dst_v; |
| 441 | out_frame->strides[2] = cw; |
| 442 | out_frame->planes[3] = NULL; |
| 443 | out_frame->strides[3] = 0; |
| 444 | return AVX_VIDEO_CODEC_OK; |
| 445 | } |
| 446 | |
| 447 | if (fmt >= 0 && fmt == g_avcodec.fmt_nv12) { |
| 448 | uint32_t ch = (h + 1U) / 2U; |
| 449 | if (!f->data[0] || !f->data[1] || f->linesize[0] < (int)w || f->linesize[1] < (int)w) { |
| 450 | return AVX_VIDEO_CODEC_ERROR; |
| 451 | } |
| 452 | uint64_t needed = (uint64_t)w * h + (uint64_t)w * ch; |
| 453 | if (avcodec_ensure_frame_buf(ctx, needed) != 0) { |
| 454 | return AVX_VIDEO_CODEC_ERROR; |
| 455 | } |
| 456 | |
| 457 | for (uint32_t y = 0; y < h; ++y) { |
| 458 | memcpy(ctx->frame_buf + (size_t)y * w, f->data[0] + (size_t)y * (size_t)f->linesize[0], w); |
| 459 | } |
| 460 | uint8_t *dst_uv = ctx->frame_buf + (size_t)w * h; |
| 461 | for (uint32_t y = 0; y < ch; ++y) { |
| 462 | memcpy(dst_uv + (size_t)y * w, f->data[1] + (size_t)y * (size_t)f->linesize[1], w); |
| 463 | } |
| 464 | |
| 465 | out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_NV12; |
| 466 | out_frame->width = w; |
| 467 | out_frame->height = h; |
| 468 | out_frame->planes[0] = ctx->frame_buf; |
| 469 | out_frame->strides[0] = w; |
| 470 | out_frame->planes[1] = dst_uv; |
| 471 | out_frame->strides[1] = w; |
| 472 | out_frame->planes[2] = NULL; |
| 473 | out_frame->strides[2] = 0; |
| 474 | out_frame->planes[3] = NULL; |
| 475 | out_frame->strides[3] = 0; |
| 476 | return AVX_VIDEO_CODEC_OK; |
| 477 | } |
| 478 | |
| 479 | if (fmt >= 0 && (fmt == g_avcodec.fmt_yuv422p || fmt == g_avcodec.fmt_yuvj422p)) { |
| 480 | uint32_t cw = (w + 1U) / 2U; |
| 481 | if (!f->data[0] || !f->data[1] || !f->data[2] || f->linesize[0] < (int)w || f->linesize[1] < (int)cw || |
| 482 | f->linesize[2] < (int)cw) { |
| 483 | return AVX_VIDEO_CODEC_ERROR; |
| 484 | } |
| 485 | uint64_t needed = (uint64_t)w * h * 2ULL; |
| 486 | if (avcodec_ensure_frame_buf(ctx, needed) != 0) { |
| 487 | return AVX_VIDEO_CODEC_ERROR; |
| 488 | } |
| 489 | |
| 490 | for (uint32_t y = 0; y < h; ++y) { |
| 491 | const uint8_t *sy = f->data[0] + (size_t)y * (size_t)f->linesize[0]; |
| 492 | const uint8_t *su = f->data[1] + (size_t)y * (size_t)f->linesize[1]; |
| 493 | const uint8_t *sv = f->data[2] + (size_t)y * (size_t)f->linesize[2]; |
| 494 | uint8_t *dst = ctx->frame_buf + (size_t)y * (size_t)w * 2ULL; |
| 495 | uint32_t x = 0; |
| 496 | for (; x + 1U < w; x += 2U) { |
| 497 | uint32_t cx = x >> 1; |
| 498 | dst[x * 2U + 0] = su[cx]; |
| 499 | dst[x * 2U + 1] = sy[x]; |
| 500 | dst[x * 2U + 2] = sv[cx]; |
| 501 | dst[x * 2U + 3] = sy[x + 1U]; |
| 502 | } |
| 503 | if (x < w) { |
| 504 | uint32_t cx = x >> 1; |
| 505 | dst[x * 2U + 0] = su[cx]; |
| 506 | dst[x * 2U + 1] = sy[x]; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_UYVY; |
| 511 | out_frame->width = w; |
| 512 | out_frame->height = h; |
| 513 | out_frame->planes[0] = ctx->frame_buf; |
| 514 | out_frame->strides[0] = (size_t)w * 2ULL; |
| 515 | out_frame->planes[1] = NULL; |
| 516 | out_frame->strides[1] = 0; |
| 517 | out_frame->planes[2] = NULL; |
| 518 | out_frame->strides[2] = 0; |
| 519 | out_frame->planes[3] = NULL; |
| 520 | out_frame->strides[3] = 0; |
| 521 | return AVX_VIDEO_CODEC_OK; |
| 522 | } |
| 523 | |
| 524 | if (fmt >= 0 && (fmt == g_avcodec.fmt_yuv420p10le || fmt == g_avcodec.fmt_yuv422p10le)) { |
| 525 | bool is_420 = (fmt == g_avcodec.fmt_yuv420p10le); |
| 526 | uint32_t cw = (w + 1U) / 2U; |
| 527 | if (!f->data[0] || !f->data[1] || !f->data[2] || f->linesize[0] < (int)(w * 2U) || |
| 528 | f->linesize[1] < (int)(cw * 2U) || f->linesize[2] < (int)(cw * 2U)) { |
| 529 | return AVX_VIDEO_CODEC_ERROR; |
| 530 | } |
| 531 | uint64_t needed = (uint64_t)w * (uint64_t)h * 4ULL; |
| 532 | if (avcodec_ensure_frame_buf(ctx, needed) != 0) { |
| 533 | return AVX_VIDEO_CODEC_ERROR; |
| 534 | } |
| 535 | |
| 536 | uint32_t half_w = w / 2U; |
| 537 | for (uint32_t y = 0; y < h; ++y) { |
| 538 | const uint16_t *sy = (const uint16_t *)(const void *)(f->data[0] + (size_t)y * (size_t)f->linesize[0]); |
| 539 | uint16_t *dy = (uint16_t *)(void *)(ctx->frame_buf + (size_t)y * (size_t)w * 2ULL); |
| 540 | for (uint32_t x = 0; x < w; ++x) { |
| 541 | dy[x] = (uint16_t)(sy[x] << 6); |
| 542 | } |
| 543 | |
| 544 | uint32_t cy = is_420 ? (y >> 1) : y; |
| 545 | const uint16_t *su = (const uint16_t *)(const void *)(f->data[1] + (size_t)cy * (size_t)f->linesize[1]); |
| 546 | const uint16_t *sv = (const uint16_t *)(const void *)(f->data[2] + (size_t)cy * (size_t)f->linesize[2]); |
| 547 | uint16_t *duv = (uint16_t *)(void *)(ctx->frame_buf + (size_t)w * h * 2ULL + (size_t)y * (size_t)w * 2ULL); |
| 548 | for (uint32_t cx = 0; cx < half_w; ++cx) { |
| 549 | duv[2U * cx + 0U] = (uint16_t)(su[cx] << 6); |
| 550 | duv[2U * cx + 1U] = (uint16_t)(sv[cx] << 6); |
| 551 | } |
| 552 | if ((w & 1U) != 0U && half_w < cw) { |
| 553 | duv[2U * half_w] = (uint16_t)(su[half_w] << 6); |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | out_frame->fourcc = OAVX_FOURCC_VIDEO_TYPE_P216; |
| 558 | out_frame->width = w; |
| 559 | out_frame->height = h; |
| 560 | out_frame->planes[0] = ctx->frame_buf; |
| 561 | out_frame->strides[0] = (size_t)w * 2ULL; |
| 562 | out_frame->planes[1] = ctx->frame_buf + (size_t)w * h * 2ULL; |
| 563 | out_frame->strides[1] = (size_t)w * 2ULL; |
| 564 | out_frame->planes[2] = NULL; |
| 565 | out_frame->strides[2] = 0; |
| 566 | out_frame->planes[3] = NULL; |
| 567 | out_frame->strides[3] = 0; |
| 568 | return AVX_VIDEO_CODEC_OK; |
| 569 | } |
| 570 | |
| 571 | return AVX_VIDEO_CODEC_ERROR; |
| 572 | } |
| 573 | |
| 574 | static bool avx_pkt_has_slice_nal(uint32_t fourcc, const uint8_t *data, size_t size) { |
| 575 | if (!data || size < 4) return false; |
| 576 | |
| 577 | if (fourcc == OAVX_FOURCC_VIDEO_TYPE_AV01) { |
| 578 | size_t pos = 0; |
| 579 | if (size >= 4 && data[0] == 0 && data[1] == 0) { |
| 580 | if (data[2] == 1) |
| 581 | pos = 3; |
| 582 | else if (data[2] == 0 && data[3] == 1) |
| 583 | pos = 4; |
| 584 | } |
| 585 | while (pos < size) { |
| 586 | uint8_t hdr = data[pos++]; |
| 587 | int obu_type = (hdr >> 3) & 0x0F; |
| 588 | if (obu_type == 3 || obu_type == 4 || obu_type == 6) { |
| 589 | return true; |
| 590 | } |
| 591 | if (hdr & 0x04) { /* extension flag */ |
| 592 | if (pos >= size) break; |
| 593 | pos++; |
| 594 | } |
| 595 | if (hdr & 0x02) { /* has size field (LEB128) */ |
| 596 | size_t val = 0, shift = 0; |
| 597 | while (pos < size) { |
| 598 | uint8_t b = data[pos++]; |
| 599 | val |= (size_t)(b & 0x7F) << shift; |
| 600 | if (!(b & 0x80)) break; |
| 601 | shift += 7; |
| 602 | } |
| 603 | pos += val; |
| 604 | } else { |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | return false; |
| 609 | } |
| 610 | |
| 611 | bool is_hevc = (fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC || fourcc == 0x43564568U); |
| 612 | size_t pos = 0; |
| 613 | while (pos + 3 <= size) { |
| 614 | size_t sc_len = 0; |
| 615 | if (data[pos] == 0 && data[pos + 1] == 0) { |
| 616 | if (data[pos + 2] == 1) |
| 617 | sc_len = 3; |
| 618 | else if (pos + 4 <= size && data[pos + 2] == 0 && data[pos + 3] == 1) |
| 619 | sc_len = 4; |
| 620 | } |
| 621 | if (sc_len > 0) { |
| 622 | pos += sc_len; |
| 623 | if (pos >= size) break; |
| 624 | if (is_hevc) { |
| 625 | uint8_t nal_type = (data[pos] >> 1) & 0x3F; |
| 626 | if (nal_type <= 31) return true; |
| 627 | } else { |
| 628 | uint8_t nal_type = data[pos] & 0x1F; |
| 629 | if (nal_type >= 1 && nal_type <= 5) return true; |
| 630 | } |
| 631 | } else { |
| 632 | pos++; |
| 633 | } |
| 634 | } |
| 635 | return false; |
| 636 | } |
| 637 | |
| 638 | /* Feeds Annex-B NAL packets into libavcodec and drains completed picture frames. */ |
| 639 | static int avcodec_dec_decode(void *backend_ctx, const uint8_t *pkt_data, size_t pkt_size, |
| 640 | oavx_video_frame_t *out_frame) { |
| 641 | avx_avcodec_dec_ctx_t *ctx = (avx_avcodec_dec_ctx_t *)backend_ctx; |
| 642 | if (!ctx || !pkt_data || pkt_size == 0 || pkt_size > (size_t)INT_MAX || !out_frame) { |
| 643 | return AVX_VIDEO_CODEC_ERROR; |
| 644 | } |
| 645 | if (!ctx->avctx || !ctx->pkt || !ctx->frame) { |
| 646 | return AVX_VIDEO_CODEC_ERROR; |
| 647 | } |
| 648 | |
| 649 | g_avcodec.av_packet_unref(ctx->pkt); |
| 650 | if (g_avcodec.av_new_packet(ctx->pkt, (int)pkt_size) < 0) { |
| 651 | return AVX_VIDEO_CODEC_ERROR; |
| 652 | } |
| 653 | |
| 654 | avx_av_packet_stub_t *pkt_stub = (avx_av_packet_stub_t *)ctx->pkt; |
| 655 | if (!pkt_stub->data || pkt_stub->size < (int)pkt_size) { |
| 656 | g_avcodec.av_packet_unref(ctx->pkt); |
| 657 | return AVX_VIDEO_CODEC_ERROR; |
| 658 | } |
| 659 | memcpy(pkt_stub->data, pkt_data, pkt_size); |
| 660 | |
| 661 | int send_rc = g_avcodec.avcodec_send_packet(ctx->avctx, ctx->pkt); |
| 662 | if (send_rc == -EAGAIN) { |
| 663 | /* Input queue full: drain one decoded frame first, then retry send */ |
| 664 | int drain_rc = g_avcodec.avcodec_receive_frame(ctx->avctx, ctx->frame); |
| 665 | if (drain_rc == 0) { |
| 666 | int copy_rc = avcodec_copy_out_frame(ctx, out_frame); |
| 667 | g_avcodec.av_frame_unref(ctx->frame); |
| 668 | if (copy_rc != AVX_VIDEO_CODEC_OK) { |
| 669 | g_avcodec.av_packet_unref(ctx->pkt); |
| 670 | return copy_rc; |
| 671 | } |
| 672 | (void)g_avcodec.avcodec_send_packet(ctx->avctx, ctx->pkt); |
| 673 | g_avcodec.av_packet_unref(ctx->pkt); |
| 674 | return AVX_VIDEO_CODEC_OK; |
| 675 | } |
| 676 | } |
| 677 | |
| 678 | g_avcodec.av_packet_unref(ctx->pkt); |
| 679 | bool has_slice = avx_pkt_has_slice_nal(ctx->src_fourcc, pkt_data, pkt_size); |
| 680 | if (send_rc < 0 && send_rc != -EAGAIN) { |
| 681 | if (!has_slice) { |
| 682 | /* Parameter/priming packet consumed by decoder without slice/frame */ |
| 683 | return AVX_VIDEO_CODEC_NEED_MORE_DATA; |
| 684 | } |
| 685 | return AVX_VIDEO_CODEC_ERROR; |
| 686 | } |
| 687 | |
| 688 | int recv_rc = g_avcodec.avcodec_receive_frame(ctx->avctx, ctx->frame); |
| 689 | if (recv_rc == -EAGAIN) { |
| 690 | return AVX_VIDEO_CODEC_NEED_MORE_DATA; |
| 691 | } |
| 692 | if (recv_rc < 0) { |
| 693 | return AVX_VIDEO_CODEC_ERROR; |
| 694 | } |
| 695 | |
| 696 | int copy_rc = avcodec_copy_out_frame(ctx, out_frame); |
| 697 | g_avcodec.av_frame_unref(ctx->frame); |
| 698 | return copy_rc; |
| 699 | } |
| 700 | |
| 701 | static void avcodec_dec_reset(void *backend_ctx) { |
| 702 | avx_avcodec_dec_ctx_t *ctx = (avx_avcodec_dec_ctx_t *)backend_ctx; |
| 703 | if (!ctx) return; |
| 704 | |
| 705 | if (ctx->avctx && g_avcodec.avcodec_flush_buffers) { |
| 706 | g_avcodec.avcodec_flush_buffers(ctx->avctx); |
| 707 | } |
| 708 | if (ctx->pkt && g_avcodec.av_packet_unref) { |
| 709 | g_avcodec.av_packet_unref(ctx->pkt); |
| 710 | } |
| 711 | if (ctx->frame && g_avcodec.av_frame_unref) { |
| 712 | g_avcodec.av_frame_unref(ctx->frame); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | static const avx_video_decoder_ops_t s_avcodec_decoder_ops = {.name = "libavcodec-dlopen", |
| 717 | .priority = 50, |
| 718 | .is_hardware = false, |
| 719 | .supports_fourcc = avcodec_dec_supports_fourcc, |
| 720 | .create = avcodec_dec_create, |
| 721 | .decode = avcodec_dec_decode, |
| 722 | .reset = avcodec_dec_reset, |
| 723 | .destroy = avcodec_dec_destroy}; |
| 724 | |
| 725 | const avx_video_decoder_ops_t *avx_video_avcodec_get_ops(void) { |
| 726 | return &s_avcodec_decoder_ops; |
| 727 | } |
| 728 | |
| 729 | /* ========================================================================= */ |
| 730 | /* Video Encoder Backend Implementation */ |
| 731 | /* ========================================================================= */ |
| 732 | |
| 733 | typedef struct avx_avcodec_enc_ctx_t { |
| 734 | void *avctx; |
| 735 | void *pkt; |
| 736 | void *frame; |
| 737 | uint32_t target_fourcc; |
| 738 | uint32_t width; |
| 739 | uint32_t height; |
| 740 | int32_t fps_num; |
| 741 | int32_t fps_den; |
| 742 | uint32_t bitrate_kbps; |
| 743 | uint8_t quality; |
| 744 | int64_t pts_counter; |
| 745 | bool force_idr_next; |
| 746 | char encoder_name[64]; |
| 747 | bool is_hardware; |
| 748 | uint8_t *tmp_buf; |
| 749 | size_t tmp_cap; |
| 750 | } avx_avcodec_enc_ctx_t; |
| 751 | |
| 752 | static const char *k_h264_enc_candidates[] = {"h264_nvenc", "h264_qsv", "h264_amf", "libx264", NULL}; |
| 753 | |
| 754 | static const char *k_hevc_enc_candidates[] = {"hevc_nvenc", "hevc_qsv", "hevc_amf", "libx265", NULL}; |
| 755 | |
| 756 | static const char *k_av1_enc_candidates[] = {"av1_nvenc", "av1_qsv", "av1_amf", "libsvtav1", "libaom-av1", NULL}; |
| 757 | |
| 758 | static bool avcodec_enc_has_support_locked(void) { |
| 759 | return (g_avcodec.available && g_avcodec.avcodec_find_encoder && g_avcodec.avcodec_send_frame && |
| 760 | g_avcodec.avcodec_receive_packet && g_avcodec.av_frame_get_buffer && g_avcodec.fmt_yuv420p >= 0); |
| 761 | } |
| 762 | |
| 763 | static bool avcodec_enc_supports_fourcc(uint32_t target_fourcc) { |
| 764 | if (target_fourcc != OAVX_FOURCC_VIDEO_TYPE_H264 && target_fourcc != OAVX_FOURCC_VIDEO_TYPE_HEVC && |
| 765 | target_fourcc != OAVX_FOURCC_VIDEO_TYPE_AV01) { |
| 766 | return false; |
| 767 | } |
| 768 | if (!probe_avcodec_library()) { |
| 769 | return false; |
| 770 | } |
| 771 | if (!avcodec_enc_has_support_locked()) { |
| 772 | return false; |
| 773 | } |
| 774 | const char **cands = NULL; |
| 775 | int fallback_id = 0; |
| 776 | if (target_fourcc == OAVX_FOURCC_VIDEO_TYPE_H264) { |
| 777 | cands = k_h264_enc_candidates; |
| 778 | fallback_id = AVX_AV_CODEC_ID_H264; |
| 779 | } else if (target_fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC) { |
| 780 | cands = k_hevc_enc_candidates; |
| 781 | fallback_id = AVX_AV_CODEC_ID_HEVC; |
| 782 | } else if (target_fourcc == OAVX_FOURCC_VIDEO_TYPE_AV01) { |
| 783 | cands = k_av1_enc_candidates; |
| 784 | fallback_id = AVX_AV_CODEC_ID_AV1; |
| 785 | } |
| 786 | if (cands && g_avcodec.avcodec_find_encoder_by_name) { |
| 787 | for (int i = 0; cands[i]; ++i) { |
| 788 | if (g_avcodec.avcodec_find_encoder_by_name(cands[i])) { |
| 789 | return true; |
| 790 | } |
| 791 | } |
| 792 | } |
| 793 | return (fallback_id != 0 && g_avcodec.avcodec_find_encoder && g_avcodec.avcodec_find_encoder(fallback_id) != NULL); |
| 794 | } |
| 795 | |
| 796 | static void avcodec_enc_destroy(void *backend_ctx) { |
| 797 | avx_avcodec_enc_ctx_t *ctx = (avx_avcodec_enc_ctx_t *)backend_ctx; |
| 798 | if (!ctx) return; |
| 799 | |
| 800 | if (ctx->avctx && g_avcodec.avcodec_send_frame) { |
| 801 | g_avcodec.avcodec_send_frame(ctx->avctx, NULL); |
| 802 | } |
| 803 | if (ctx->pkt && g_avcodec.av_packet_free) { |
| 804 | g_avcodec.av_packet_free(&ctx->pkt); |
| 805 | } |
| 806 | if (ctx->frame && g_avcodec.av_frame_free) { |
| 807 | g_avcodec.av_frame_free(&ctx->frame); |
| 808 | } |
| 809 | if (ctx->avctx && g_avcodec.avcodec_free_context) { |
| 810 | g_avcodec.avcodec_free_context(&ctx->avctx); |
| 811 | } |
| 812 | if (ctx->tmp_buf) { |
| 813 | free(ctx->tmp_buf); |
| 814 | } |
| 815 | free(ctx); |
| 816 | } |
| 817 | |
| 818 | /* Allocates and tunes FFmpeg hardware/software encoder (NVENC, QSV, x264) for zero-latency streaming. */ |
| 819 | static int avcodec_enc_create(const avx_video_enc_cfg_t *cfg, void **backend_ctx) { |
| 820 | if (!cfg || !backend_ctx || cfg->width == 0 || cfg->height == 0) { |
| 821 | return AVX_VIDEO_CODEC_ERROR; |
| 822 | } |
| 823 | *backend_ctx = NULL; |
| 824 | |
| 825 | if (!probe_avcodec_library() || !avcodec_enc_has_support_locked()) { |
| 826 | return AVX_VIDEO_CODEC_ERROR; |
| 827 | } |
| 828 | |
| 829 | const char **cands = NULL; |
| 830 | int fallback_codec_id = 0; |
| 831 | if (cfg->fourcc == OAVX_FOURCC_VIDEO_TYPE_H264) { |
| 832 | cands = k_h264_enc_candidates; |
| 833 | fallback_codec_id = AVX_AV_CODEC_ID_H264; |
| 834 | } else if (cfg->fourcc == OAVX_FOURCC_VIDEO_TYPE_HEVC) { |
| 835 | cands = k_hevc_enc_candidates; |
| 836 | fallback_codec_id = AVX_AV_CODEC_ID_HEVC; |
| 837 | } else if (cfg->fourcc == OAVX_FOURCC_VIDEO_TYPE_AV01) { |
| 838 | cands = k_av1_enc_candidates; |
| 839 | fallback_codec_id = AVX_AV_CODEC_ID_AV1; |
| 840 | } else { |
| 841 | return AVX_VIDEO_CODEC_ERROR; |
| 842 | } |
| 843 | |
| 844 | void *enc = NULL; |
| 845 | void *avctx = NULL; |
| 846 | char chosen_name[64] = "unknown"; |
| 847 | bool is_hw = false; |
| 848 | |
| 849 | int fps_num = cfg->fps_num > 0 ? cfg->fps_num : 30; |
| 850 | int fps_den = cfg->fps_den > 0 ? cfg->fps_den : 1; |
| 851 | char tb_str[64], fr_str[64], size_str[64]; |
| 852 | snprintf(tb_str, sizeof(tb_str), "%d/%d", fps_den, fps_num); |
| 853 | snprintf(fr_str, sizeof(fr_str), "%d/%d", fps_num, fps_den); |
| 854 | snprintf(size_str, sizeof(size_str), "%ux%u", (unsigned)cfg->width, (unsigned)cfg->height); |
| 855 | |
| 856 | uint32_t br_kbps = cfg->bitrate_kbps; |
| 857 | if (br_kbps == 0) { |
| 858 | uint64_t pixels = (uint64_t)cfg->width * cfg->height; |
| 859 | if (pixels <= 1280 * 720) { |
| 860 | br_kbps = 4000; |
| 861 | } else if (pixels <= 1920 * 1080) { |
| 862 | br_kbps = 8000; |
| 863 | } else { |
| 864 | br_kbps = 20000; |
| 865 | } |
| 866 | if (cfg->quality > 0) { |
| 867 | br_kbps = (uint32_t)((uint64_t)br_kbps * cfg->quality / 50ULL); |
| 868 | if (br_kbps < 500) br_kbps = 500; |
| 869 | } |
| 870 | } |
| 871 | uint32_t gop = cfg->keyframe_interval > 0 ? cfg->keyframe_interval : 60; |
| 872 | |
| 873 | int cand_idx = 0; |
| 874 | while (1) { |
| 875 | const char *cand_name = (cands && cands[cand_idx]) ? cands[cand_idx] : NULL; |
| 876 | if (cand_name) { |
| 877 | cand_idx++; |
| 878 | if (!g_avcodec.avcodec_find_encoder_by_name) continue; |
| 879 | enc = g_avcodec.avcodec_find_encoder_by_name(cand_name); |
| 880 | if (!enc) continue; |
| 881 | snprintf(chosen_name, sizeof(chosen_name), "%s", cand_name); |
| 882 | } else if (fallback_codec_id != 0 && g_avcodec.avcodec_find_encoder) { |
| 883 | enc = g_avcodec.avcodec_find_encoder(fallback_codec_id); |
| 884 | fallback_codec_id = 0; |
| 885 | if (!enc) break; |
| 886 | snprintf(chosen_name, sizeof(chosen_name), "default"); |
| 887 | } else { |
| 888 | break; |
| 889 | } |
| 890 | |
| 891 | avctx = g_avcodec.avcodec_alloc_context3(enc); |
| 892 | if (!avctx) continue; |
| 893 | |
| 894 | g_avcodec.av_opt_set(avctx, "video_size", size_str, 0); |
| 895 | g_avcodec.av_opt_set_int(avctx, "pixel_format", g_avcodec.fmt_yuv420p, 0); |
| 896 | g_avcodec.av_opt_set(avctx, "time_base", tb_str, 0); |
| 897 | g_avcodec.av_opt_set(avctx, "framerate", fr_str, 0); |
| 898 | g_avcodec.av_opt_set_int(avctx, "b", (int64_t)br_kbps * 1000LL, 0); |
| 899 | g_avcodec.av_opt_set_int(avctx, "g", gop, 0); |
| 900 | g_avcodec.av_opt_set_int(avctx, "bf", 0, 0); |
| 901 | |
| 902 | if (strstr(chosen_name, "nvenc")) { |
| 903 | is_hw = true; |
| 904 | g_avcodec.av_opt_set(avctx, "preset", "p1", 1); |
| 905 | g_avcodec.av_opt_set(avctx, "tune", "ull", 1); |
| 906 | g_avcodec.av_opt_set_int(avctx, "zerolatency", 1, 1); |
| 907 | g_avcodec.av_opt_set_int(avctx, "delay", 0, 1); |
| 908 | } else if (strstr(chosen_name, "qsv")) { |
| 909 | is_hw = true; |
| 910 | g_avcodec.av_opt_set(avctx, "preset", "veryfast", 1); |
| 911 | g_avcodec.av_opt_set_int(avctx, "async_depth", 1, 1); |
| 912 | } else if (strstr(chosen_name, "amf")) { |
| 913 | is_hw = true; |
| 914 | g_avcodec.av_opt_set(avctx, "usage", "lowlatency", 1); |
| 915 | g_avcodec.av_opt_set(avctx, "quality", "speed", 1); |
| 916 | } else { |
| 917 | is_hw = false; |
| 918 | g_avcodec.av_opt_set(avctx, "preset", "ultrafast", 1); |
| 919 | g_avcodec.av_opt_set(avctx, "tune", "zerolatency", 1); |
| 920 | } |
| 921 | g_avcodec.av_opt_set(avctx, "forced-idr", "1", 1); |
| 922 | |
| 923 | int open_rc = g_avcodec.avcodec_open2(avctx, enc, NULL); |
| 924 | if (open_rc == 0) { |
| 925 | oavx_log_write(OAVX_LOG_INFO, "avcodec", "Selected %s %s encoder for %ux%u @ %u kbps", |
| 926 | is_hw ? "Hardware" : "Software", chosen_name, (unsigned)cfg->width, (unsigned)cfg->height, |
| 927 | (unsigned)br_kbps); |
| 928 | break; |
| 929 | } |
| 930 | |
| 931 | /* If opening this candidate failed (e.g. resolution not supported by ASIC or GPU busy), try next */ |
| 932 | g_avcodec.avcodec_free_context(&avctx); |
| 933 | avctx = NULL; |
| 934 | enc = NULL; |
| 935 | } |
| 936 | |
| 937 | if (!avctx || !enc) { |
| 938 | oavx_log_write(OAVX_LOG_ERROR, "avcodec", "Failed to initialize any encoder candidate for FourCC %08X", |
| 939 | cfg->fourcc); |
| 940 | return AVX_VIDEO_CODEC_ERROR; |
| 941 | } |
| 942 | |
| 943 | avx_avcodec_enc_ctx_t *ctx = (avx_avcodec_enc_ctx_t *)calloc(1, sizeof(*ctx)); |
| 944 | if (!ctx) { |
| 945 | g_avcodec.avcodec_free_context(&avctx); |
| 946 | return AVX_VIDEO_CODEC_ERROR; |
| 947 | } |
| 948 | |
| 949 | ctx->avctx = avctx; |
| 950 | ctx->target_fourcc = cfg->fourcc; |
| 951 | ctx->width = cfg->width; |
| 952 | ctx->height = cfg->height; |
| 953 | ctx->fps_num = fps_num; |
| 954 | ctx->fps_den = fps_den; |
| 955 | ctx->bitrate_kbps = cfg->bitrate_kbps; |
| 956 | ctx->quality = cfg->quality; |
| 957 | ctx->pts_counter = 0; |
| 958 | ctx->force_idr_next = true; |
| 959 | snprintf(ctx->encoder_name, sizeof(ctx->encoder_name), "%s (%s)", chosen_name, is_hw ? "Hardware" : "Software"); |
| 960 | ctx->is_hardware = is_hw; |
| 961 | |
| 962 | ctx->frame = g_avcodec.av_frame_alloc(); |
| 963 | if (!ctx->frame) { |
| 964 | avcodec_enc_destroy(ctx); |
| 965 | return AVX_VIDEO_CODEC_ERROR; |
| 966 | } |
| 967 | |
| 968 | avx_av_frame_stub_t *f = (avx_av_frame_stub_t *)ctx->frame; |
| 969 | f->width = (int)cfg->width; |
| 970 | f->height = (int)cfg->height; |
| 971 | f->format = g_avcodec.fmt_yuv420p; |
| 972 | |
| 973 | int buf_rc = g_avcodec.av_frame_get_buffer(ctx->frame, 32); |
| 974 | if (buf_rc < 0) { |
| 975 | avcodec_enc_destroy(ctx); |
| 976 | return AVX_VIDEO_CODEC_ERROR; |
| 977 | } |
| 978 | |
| 979 | ctx->pkt = g_avcodec.av_packet_alloc(); |
| 980 | if (!ctx->pkt) { |
| 981 | avcodec_enc_destroy(ctx); |
| 982 | return AVX_VIDEO_CODEC_ERROR; |
| 983 | } |
| 984 | |
| 985 | *backend_ctx = ctx; |
| 986 | return AVX_VIDEO_CODEC_OK; |
| 987 | } |
| 988 | |
| 989 | static int avcodec_enc_prepare_input(avx_avcodec_enc_ctx_t *ctx, const oavx_video_frame_t *in_frame) { |
| 990 | if (!ctx || !in_frame || !ctx->frame) return -1; |
| 991 | uint32_t w = ctx->width; |
| 992 | uint32_t h = ctx->height; |
| 993 | avx_av_frame_stub_t *f = (avx_av_frame_stub_t *)ctx->frame; |
| 994 | |
| 995 | uint32_t src_fourcc = (uint32_t)in_frame->fourcc; |
| 996 | if (src_fourcc == 0 || src_fourcc == OAVX_FOURCC_VIDEO_TYPE_UYVY) { |
| 997 | size_t s0 = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w * 2; |
| 998 | avx_color_uyvy_to_i420(in_frame->planes[0], s0, f->data[0], (size_t)f->linesize[0], f->data[1], |
| 999 | (size_t)f->linesize[1], f->data[2], (size_t)f->linesize[2], (int)w, (int)h); |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_RGBA) { |
| 1004 | size_t s0 = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w * 4; |
| 1005 | avx_color_rgba_to_i420(in_frame->planes[0], s0, f->data[0], (size_t)f->linesize[0], f->data[1], |
| 1006 | (size_t)f->linesize[1], f->data[2], (size_t)f->linesize[2], (int)w, (int)h); |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
| 1010 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_BGRA) { |
| 1011 | size_t needed = (size_t)w * (size_t)h * 2; |
| 1012 | if (ctx->tmp_cap < needed) { |
| 1013 | uint8_t *nb = (uint8_t *)realloc(ctx->tmp_buf, needed); |
| 1014 | if (!nb) return -1; |
| 1015 | ctx->tmp_buf = nb; |
| 1016 | ctx->tmp_cap = needed; |
| 1017 | } |
| 1018 | avx_color_bgra_to_uyvy(in_frame->planes[0], ctx->tmp_buf, (int)w, (int)h); |
| 1019 | avx_color_uyvy_to_i420(ctx->tmp_buf, (size_t)w * 2, f->data[0], (size_t)f->linesize[0], f->data[1], |
| 1020 | (size_t)f->linesize[1], f->data[2], (size_t)f->linesize[2], (int)w, (int)h); |
| 1021 | return 0; |
| 1022 | } |
| 1023 | |
| 1024 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_P216) { |
| 1025 | size_t needed = (size_t)w * (size_t)h * 2; |
| 1026 | if (ctx->tmp_cap < needed) { |
| 1027 | uint8_t *nb = (uint8_t *)realloc(ctx->tmp_buf, needed); |
| 1028 | if (!nb) return -1; |
| 1029 | ctx->tmp_buf = nb; |
| 1030 | ctx->tmp_cap = needed; |
| 1031 | } |
| 1032 | avx_color_p216_to_uyvy(in_frame->planes[0], ctx->tmp_buf, (int)w, (int)h); |
| 1033 | avx_color_uyvy_to_i420(ctx->tmp_buf, (size_t)w * 2, f->data[0], (size_t)f->linesize[0], f->data[1], |
| 1034 | (size_t)f->linesize[1], f->data[2], (size_t)f->linesize[2], (int)w, (int)h); |
| 1035 | return 0; |
| 1036 | } |
| 1037 | |
| 1038 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_NV12) { |
| 1039 | size_t ys = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w; |
| 1040 | size_t uvs = in_frame->strides[1] ? in_frame->strides[1] : (size_t)w; |
| 1041 | const uint8_t *src_y = in_frame->planes[0]; |
| 1042 | const uint8_t *src_uv = in_frame->planes[1]; |
| 1043 | if (!src_uv) src_uv = src_y + ys * (size_t)h; |
| 1044 | |
| 1045 | for (uint32_t r = 0; r < h; ++r) { |
| 1046 | memcpy(f->data[0] + r * (size_t)f->linesize[0], src_y + r * ys, w); |
| 1047 | } |
| 1048 | for (uint32_t r = 0; r < h / 2; ++r) { |
| 1049 | const uint8_t *row_uv = src_uv + r * uvs; |
| 1050 | uint8_t *dst_u = f->data[1] + r * (size_t)f->linesize[1]; |
| 1051 | uint8_t *dst_v = f->data[2] + r * (size_t)f->linesize[2]; |
| 1052 | for (uint32_t c = 0; c < w / 2; ++c) { |
| 1053 | dst_u[c] = row_uv[c * 2 + 0]; |
| 1054 | dst_v[c] = row_uv[c * 2 + 1]; |
| 1055 | } |
| 1056 | } |
| 1057 | return 0; |
| 1058 | } |
| 1059 | |
| 1060 | if (src_fourcc == OAVX_FOURCC_VIDEO_TYPE_I420) { |
| 1061 | size_t ys = in_frame->strides[0] ? in_frame->strides[0] : (size_t)w; |
| 1062 | size_t us = in_frame->strides[1] ? in_frame->strides[1] : ((size_t)w / 2); |
| 1063 | size_t vs = in_frame->strides[2] ? in_frame->strides[2] : ((size_t)w / 2); |
| 1064 | const uint8_t *src_y = in_frame->planes[0]; |
| 1065 | const uint8_t *src_u = in_frame->planes[1]; |
| 1066 | const uint8_t *src_v = in_frame->planes[2]; |
| 1067 | if (!src_u) src_u = src_y + ys * (size_t)h; |
| 1068 | if (!src_v) src_v = src_u + us * ((size_t)h / 2); |
| 1069 | |
| 1070 | for (uint32_t r = 0; r < h; ++r) { |
| 1071 | memcpy(f->data[0] + r * (size_t)f->linesize[0], src_y + r * ys, w); |
| 1072 | } |
| 1073 | for (uint32_t r = 0; r < h / 2; ++r) { |
| 1074 | memcpy(f->data[1] + r * (size_t)f->linesize[1], src_u + r * us, w / 2); |
| 1075 | memcpy(f->data[2] + r * (size_t)f->linesize[2], src_v + r * vs, w / 2); |
| 1076 | } |
| 1077 | return 0; |
| 1078 | } |
| 1079 | |
| 1080 | return -1; |
| 1081 | } |
| 1082 | |
| 1083 | /* Submits raw frame to libavcodec encoder and drains Annex-B NAL stream packet. */ |
| 1084 | static int avcodec_enc_encode(void *backend_ctx, const oavx_video_frame_t *in_frame, uint8_t *out_buf, size_t out_cap, |
| 1085 | size_t *out_size, bool *out_is_keyframe) { |
| 1086 | avx_avcodec_enc_ctx_t *ctx = (avx_avcodec_enc_ctx_t *)backend_ctx; |
| 1087 | if (!ctx || !in_frame || !out_buf || !out_size || !out_is_keyframe) { |
| 1088 | return AVX_VIDEO_CODEC_ERROR; |
| 1089 | } |
| 1090 | *out_size = 0; |
| 1091 | *out_is_keyframe = false; |
| 1092 | |
| 1093 | if (in_frame->width != ctx->width || in_frame->height != ctx->height) { |
| 1094 | return AVX_VIDEO_CODEC_ERROR; |
| 1095 | } |
| 1096 | |
| 1097 | if (avcodec_enc_prepare_input(ctx, in_frame) != 0) { |
| 1098 | return AVX_VIDEO_CODEC_ERROR; |
| 1099 | } |
| 1100 | |
| 1101 | avx_av_frame_stub_t *f = (avx_av_frame_stub_t *)ctx->frame; |
| 1102 | f->pts = ctx->pts_counter++; |
| 1103 | if (ctx->force_idr_next) { |
| 1104 | f->key_frame = 1; |
| 1105 | f->pict_type = 1; /* AV_PICTURE_TYPE_I */ |
| 1106 | ctx->force_idr_next = false; |
| 1107 | } else { |
| 1108 | f->key_frame = 0; |
| 1109 | f->pict_type = 0; /* AV_PICTURE_TYPE_NONE */ |
| 1110 | } |
| 1111 | |
| 1112 | int send_rc = g_avcodec.avcodec_send_frame(ctx->avctx, ctx->frame); |
| 1113 | if (send_rc < 0) { |
| 1114 | return AVX_VIDEO_CODEC_ERROR; |
| 1115 | } |
| 1116 | |
| 1117 | g_avcodec.av_packet_unref(ctx->pkt); |
| 1118 | int recv_rc = g_avcodec.avcodec_receive_packet(ctx->avctx, ctx->pkt); |
| 1119 | if (recv_rc == 0) { |
| 1120 | avx_av_packet_stub_t *pkt = (avx_av_packet_stub_t *)ctx->pkt; |
| 1121 | if (pkt->size < 0 || (size_t)pkt->size > out_cap) { |
| 1122 | g_avcodec.av_packet_unref(ctx->pkt); |
| 1123 | return AVX_VIDEO_CODEC_ERROR; |
| 1124 | } |
| 1125 | memcpy(out_buf, pkt->data, (size_t)pkt->size); |
| 1126 | *out_size = (size_t)pkt->size; |
| 1127 | *out_is_keyframe = ((pkt->flags & 1) != 0); |
| 1128 | g_avcodec.av_packet_unref(ctx->pkt); |
| 1129 | return AVX_VIDEO_CODEC_OK; |
| 1130 | } else if (recv_rc == -11 || recv_rc == -EAGAIN) { |
| 1131 | *out_size = 0; |
| 1132 | *out_is_keyframe = false; |
| 1133 | return AVX_VIDEO_CODEC_OK; |
| 1134 | } |
| 1135 | |
| 1136 | return AVX_VIDEO_CODEC_ERROR; |
| 1137 | } |
| 1138 | |
| 1139 | static int avcodec_enc_set_quality(void *backend_ctx, uint8_t quality) { |
| 1140 | avx_avcodec_enc_ctx_t *ctx = (avx_avcodec_enc_ctx_t *)backend_ctx; |
| 1141 | if (!ctx) return AVX_VIDEO_CODEC_ERROR; |
| 1142 | ctx->quality = quality; |
| 1143 | if (ctx->bitrate_kbps == 0 && quality > 0) { |
| 1144 | uint64_t pixels = (uint64_t)ctx->width * ctx->height; |
| 1145 | uint32_t base_kbps = (pixels <= 1280 * 720) ? 4000 : (pixels <= 1920 * 1080 ? 8000 : 20000); |
| 1146 | uint32_t br_kbps = (uint32_t)((uint64_t)base_kbps * quality / 50ULL); |
| 1147 | if (br_kbps < 500) br_kbps = 500; |
| 1148 | if (g_avcodec.av_opt_set_int) { |
| 1149 | g_avcodec.av_opt_set_int(ctx->avctx, "b", (int64_t)br_kbps * 1000LL, 0); |
| 1150 | } |
| 1151 | } |
| 1152 | return AVX_VIDEO_CODEC_OK; |
| 1153 | } |
| 1154 | |
| 1155 | static void avcodec_enc_request_keyframe(void *backend_ctx) { |
| 1156 | avx_avcodec_enc_ctx_t *ctx = (avx_avcodec_enc_ctx_t *)backend_ctx; |
| 1157 | if (ctx) { |
| 1158 | ctx->force_idr_next = true; |
| 1159 | } |
| 1160 | } |
| 1161 | |
| 1162 | static const char *avcodec_enc_get_name(void *backend_ctx) { |
| 1163 | avx_avcodec_enc_ctx_t *ctx = (avx_avcodec_enc_ctx_t *)backend_ctx; |
| 1164 | return (ctx && ctx->encoder_name[0]) ? ctx->encoder_name : "libavcodec-dlopen"; |
| 1165 | } |
| 1166 | |
| 1167 | static const avx_video_encoder_ops_t s_avcodec_encoder_ops = {.name = "libavcodec-dlopen", |
| 1168 | .priority = 85, |
| 1169 | .is_hardware = true, |
| 1170 | .supports_fourcc = avcodec_enc_supports_fourcc, |
| 1171 | .create = avcodec_enc_create, |
| 1172 | .encode = avcodec_enc_encode, |
| 1173 | .set_quality = avcodec_enc_set_quality, |
| 1174 | .request_keyframe = avcodec_enc_request_keyframe, |
| 1175 | .get_name = avcodec_enc_get_name, |
| 1176 | .destroy = avcodec_enc_destroy}; |
| 1177 | |
| 1178 | const avx_video_encoder_ops_t *avx_video_avcodec_get_encoder_ops(void) { |
| 1179 | return &s_avcodec_encoder_ops; |
| 1180 | } |
| 1181 | |
| 1182 | #else |
| 1183 | |
| 1184 | const avx_video_decoder_ops_t *avx_video_avcodec_get_ops(void) { |
| 1185 | return NULL; |
| 1186 | } |
| 1187 | |
| 1188 | const avx_video_encoder_ops_t *avx_video_avcodec_get_encoder_ops(void) { |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | |
| 1192 | #endif /* AVX_OS_LINUX || AVX_OS_WINDOWS */ |