+ +
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 | * include/avx_video_codec.h - Decoupled internal video encoder & decoder contracts |
| 7 | */ |
| 8 | |
| 9 | #ifndef AVX_VIDEO_CODEC_H |
| 10 | #define AVX_VIDEO_CODEC_H |
| 11 | |
| 12 | #include <stddef.h> |
| 13 | #include <stdint.h> |
| 14 | #include <stdbool.h> |
| 15 | #include "openavx.h" |
| 16 | |
| 17 | #ifdef __cplusplus |
| 18 | extern "C" { |
| 19 | #endif |
| 20 | |
| 21 | /* Standard Video Codec Return Codes */ |
| 22 | #define AVX_VIDEO_CODEC_OK 0 |
| 23 | #define AVX_VIDEO_CODEC_NEED_MORE_DATA 1 /* Decoder buffered input (priming/B-frame); no frame yet */ |
| 24 | #define AVX_VIDEO_CODEC_ERROR (-1) |
| 25 | |
| 26 | /* ========================================================================= */ |
| 27 | /* Opaque Per-Stream Video Encoder & Decoder Handles */ |
| 28 | /* ========================================================================= */ |
| 29 | |
| 30 | typedef struct avx_video_encoder_t avx_video_encoder_t; |
| 31 | typedef struct avx_video_decoder_t avx_video_decoder_t; |
| 32 | |
| 33 | /* ========================================================================= */ |
| 34 | /* Video Encoder Configuration & Backend Contract */ |
| 35 | /* ========================================================================= */ |
| 36 | |
| 37 | typedef struct avx_video_enc_cfg_t { |
| 38 | uint32_t fourcc; /* Target compressed FourCC (e.g. SHQ2) */ |
| 39 | uint32_t width; |
| 40 | uint32_t height; |
| 41 | uint8_t quality; /* 0 = backend default, 20..98 for SpeedHQ */ |
| 42 | uint32_t bitrate_kbps; /* Target bitrate (0 = backend/quality default) */ |
| 43 | int32_t fps_num; |
| 44 | int32_t fps_den; |
| 45 | uint32_t keyframe_interval; /* 0 = backend default / intra-frame */ |
| 46 | } avx_video_enc_cfg_t; |
| 47 | |
| 48 | typedef struct avx_video_encoder_ops_t { |
| 49 | const char *name; |
| 50 | int priority; /* Higher priority backend wins when multiple support FourCC */ |
| 51 | bool is_hardware; |
| 52 | |
| 53 | bool (*supports_fourcc)(uint32_t target_fourcc); |
| 54 | int (*create)(const avx_video_enc_cfg_t *cfg, void **backend_ctx); |
| 55 | int (*encode)(void *backend_ctx, |
| 56 | const oavx_video_frame_t *in_frame, |
| 57 | uint8_t *out_buf, size_t out_cap, |
| 58 | size_t *out_size, bool *out_is_keyframe); |
| 59 | int (*set_quality)(void *backend_ctx, uint8_t quality); |
| 60 | void (*request_keyframe)(void *backend_ctx); |
| 61 | const char *(*get_name)(void *backend_ctx); |
| 62 | void (*destroy)(void *backend_ctx); |
| 63 | } avx_video_encoder_ops_t; |
| 64 | |
| 65 | /* ========================================================================= */ |
| 66 | /* Video Decoder Configuration & Backend Contract */ |
| 67 | /* ========================================================================= */ |
| 68 | |
| 69 | typedef struct avx_video_dec_cfg_t { |
| 70 | uint32_t fourcc; /* Compressed input FourCC (SHQ2, H264, HEVC, AV01, ...) */ |
| 71 | uint32_t width; /* Initial frame width (0 if determined from bitstream) */ |
| 72 | uint32_t height; /* Initial frame height (0 if determined from bitstream) */ |
| 73 | uint32_t preferred_fourcc; /* Hint for output pixel FourCC (0 = backend's natural layout) */ |
| 74 | } avx_video_dec_cfg_t; |
| 75 | |
| 76 | typedef struct avx_video_decoder_ops_t { |
| 77 | const char *name; |
| 78 | int priority; /* Higher priority backend wins when multiple support FourCC */ |
| 79 | bool is_hardware; |
| 80 | |
| 81 | bool (*supports_fourcc)(uint32_t src_fourcc); |
| 82 | int (*create)(const avx_video_dec_cfg_t *cfg, void **backend_ctx); |
| 83 | |
| 84 | /** |
| 85 | * Decodes one compressed packet into `out_frame`. |
| 86 | * Backend populates `out_frame->fourcc`, `width`, `height`, `planes[4]`, `strides[4]` |
| 87 | * pointing to backend-owned memory valid until next decode/reset/destroy call. |
| 88 | * Returns: |
| 89 | * AVX_VIDEO_CODEC_OK (0) when `out_frame` contains a decoded frame, |
| 90 | * AVX_VIDEO_CODEC_NEED_MORE_DATA (1) when input was consumed/buffered with no frame yet, |
| 91 | * < 0 on fatal decode error. |
| 92 | */ |
| 93 | int (*decode)(void *backend_ctx, |
| 94 | const uint8_t *pkt_data, size_t pkt_size, |
| 95 | oavx_video_frame_t *out_frame); |
| 96 | void (*reset)(void *backend_ctx); |
| 97 | void (*destroy)(void *backend_ctx); |
| 98 | } avx_video_decoder_ops_t; |
| 99 | |
| 100 | /* ========================================================================= */ |
| 101 | /* Codec Subsystem Lifecycle & Backend Registration */ |
| 102 | /* ========================================================================= */ |
| 103 | |
| 104 | /** |
| 105 | * Initializes the video codec subsystem and registers built-in backends (SpeedHQ). |
| 106 | * Safe to call repeatedly; idempotent under internal locking. |
| 107 | */ |
| 108 | OPENAVX_API bool avx_video_codec_init(void); |
| 109 | |
| 110 | /** |
| 111 | * Releases registered backend table references and shuts down built-in video codecs. |
| 112 | */ |
| 113 | OPENAVX_API void avx_video_codec_destroy(void); |
| 114 | |
| 115 | /** |
| 116 | * Registers an encoder backend. Backends remain independent from decoder backends. |
| 117 | */ |
| 118 | OPENAVX_API bool avx_video_encoder_register(const avx_video_encoder_ops_t *ops); |
| 119 | |
| 120 | /** |
| 121 | * Registers a decoder backend. Backends remain independent from encoder backends. |
| 122 | */ |
| 123 | OPENAVX_API bool avx_video_decoder_register(const avx_video_decoder_ops_t *ops); |
| 124 | |
| 125 | /** |
| 126 | * Returns true if at least one registered encoder backend accepts `target_fourcc`. |
| 127 | */ |
| 128 | OPENAVX_API bool avx_video_encoder_has_backend(uint32_t target_fourcc); |
| 129 | |
| 130 | /** |
| 131 | * Returns true if at least one registered decoder backend accepts `src_fourcc`. |
| 132 | */ |
| 133 | OPENAVX_API bool avx_video_decoder_has_backend(uint32_t src_fourcc); |
| 134 | |
| 135 | /* ========================================================================= */ |
| 136 | /* Per-Stream Video Encoder Operations */ |
| 137 | /* ========================================================================= */ |
| 138 | |
| 139 | OPENAVX_API avx_video_encoder_t *avx_video_encoder_create(const avx_video_enc_cfg_t *cfg); |
| 140 | OPENAVX_API void avx_video_encoder_destroy(avx_video_encoder_t *enc); |
| 141 | OPENAVX_API int avx_video_encoder_encode(avx_video_encoder_t *enc, |
| 142 | const oavx_video_frame_t *in_frame, |
| 143 | uint8_t *out_buf, size_t out_cap, |
| 144 | size_t *out_size, bool *out_is_keyframe); |
| 145 | OPENAVX_API int avx_video_encoder_set_quality(avx_video_encoder_t *enc, uint8_t quality); |
| 146 | OPENAVX_API void avx_video_encoder_request_keyframe(avx_video_encoder_t *enc); |
| 147 | OPENAVX_API uint32_t avx_video_encoder_get_fourcc(const avx_video_encoder_t *enc); |
| 148 | OPENAVX_API const char *avx_video_encoder_get_backend_name(const avx_video_encoder_t *enc); |
| 149 | |
| 150 | /* ========================================================================= */ |
| 151 | /* Per-Stream Video Decoder Operations */ |
| 152 | /* ========================================================================= */ |
| 153 | |
| 154 | OPENAVX_API avx_video_decoder_t *avx_video_decoder_create(const avx_video_dec_cfg_t *cfg); |
| 155 | OPENAVX_API void avx_video_decoder_destroy(avx_video_decoder_t *dec); |
| 156 | OPENAVX_API int avx_video_decoder_decode(avx_video_decoder_t *dec, |
| 157 | const uint8_t *pkt_data, size_t pkt_size, |
| 158 | oavx_video_frame_t *out_frame); |
| 159 | OPENAVX_API void avx_video_decoder_reset(avx_video_decoder_t *dec); |
| 160 | OPENAVX_API uint32_t avx_video_decoder_get_fourcc(const avx_video_decoder_t *dec); |
| 161 | OPENAVX_API const char *avx_video_decoder_get_backend_name(const avx_video_decoder_t *dec); |
| 162 | |
| 163 | #ifdef __cplusplus |
| 164 | } |
| 165 | #endif |
| 166 | |
| 167 | #endif /* AVX_VIDEO_CODEC_H */ |