+ +
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_rate_control.c - Adaptive Bitrate & Dynamic FEC Controller (GCC / BBR Hybrid) |
| 7 | */ |
| 8 | |
| 9 | #include "avx_rate_control.h" |
| 10 | #include <stdlib.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | struct avx_rate_controller_t { |
| 14 | avx_rate_controller_config_t cfg; |
| 15 | avx_mutex_t lock; |
| 16 | |
| 17 | /* Live link telemetry tracking */ |
| 18 | uint32_t srtt_ms; |
| 19 | uint32_t rttvar_ms; |
| 20 | uint32_t min_rtt_ms; |
| 21 | int64_t min_rtt_ts_ms; |
| 22 | |
| 23 | /* Previous telemetry snapshot for differential calculation */ |
| 24 | uint64_t prev_packets_sent; |
| 25 | uint64_t prev_packets_lost; |
| 26 | uint64_t prev_packets_retrans; |
| 27 | int64_t last_update_ms; |
| 28 | |
| 29 | /* Filtered metrics */ |
| 30 | float loss_ratio; /* Smoothed packet loss ratio (0.0 .. 1.0) */ |
| 31 | float queuing_delay_ms; /* srtt - min_rtt */ |
| 32 | |
| 33 | /* Output recommendations */ |
| 34 | uint32_t current_bitrate_bps; |
| 35 | uint8_t current_quality; |
| 36 | uint32_t cadence; |
| 37 | avx_wan_congestion_state_t congestion_state; |
| 38 | avx_fec_config_t current_fec; |
| 39 | }; |
| 40 | |
| 41 | /* |
| 42 | * Initializes hybrid GCC/BBR rate controller tracking minimum RTT, queuing delay |
| 43 | * gradients, and packet loss ratio to compute optimal bitrate, quality, and FEC. |
| 44 | */ |
| 45 | avx_rate_controller_t* avx_rate_controller_create(const avx_rate_controller_config_t* cfg) { |
| 46 | avx_rate_controller_t* c = (avx_rate_controller_t*)calloc(1, sizeof(avx_rate_controller_t)); |
| 47 | if (!c) return NULL; |
| 48 | |
| 49 | if (cfg) { |
| 50 | c->cfg = *cfg; |
| 51 | } else { |
| 52 | c->cfg.min_bitrate_bps = 2000000; /* 2 Mbps */ |
| 53 | c->cfg.max_bitrate_bps = 150000000; /* 150 Mbps */ |
| 54 | c->cfg.start_bitrate_bps = 50000000; /* 50 Mbps */ |
| 55 | c->cfg.target_fps = 60.0f; |
| 56 | c->cfg.min_quality = 30; |
| 57 | c->cfg.max_quality = 96; |
| 58 | c->cfg.fec_auto_tune = true; |
| 59 | } |
| 60 | |
| 61 | if (c->cfg.min_bitrate_bps == 0) c->cfg.min_bitrate_bps = 1000000; |
| 62 | if (c->cfg.max_bitrate_bps < c->cfg.min_bitrate_bps) c->cfg.max_bitrate_bps = c->cfg.min_bitrate_bps * 2; |
| 63 | if (c->cfg.start_bitrate_bps < c->cfg.min_bitrate_bps) c->cfg.start_bitrate_bps = c->cfg.min_bitrate_bps; |
| 64 | if (c->cfg.start_bitrate_bps > c->cfg.max_bitrate_bps) c->cfg.start_bitrate_bps = c->cfg.max_bitrate_bps; |
| 65 | if (c->cfg.target_fps <= 0.0f) c->cfg.target_fps = 60.0f; |
| 66 | if (c->cfg.min_quality == 0) c->cfg.min_quality = 20; |
| 67 | if (c->cfg.max_quality < c->cfg.min_quality) c->cfg.max_quality = 96; |
| 68 | |
| 69 | avx_mutex_init(&c->lock); |
| 70 | avx_rate_controller_reset(c); |
| 71 | return c; |
| 72 | } |
| 73 | |
| 74 | void avx_rate_controller_destroy(avx_rate_controller_t* c) { |
| 75 | if (!c) return; |
| 76 | avx_mutex_destroy(&c->lock); |
| 77 | free(c); |
| 78 | } |
| 79 | |
| 80 | void avx_rate_controller_reset(avx_rate_controller_t* c) { |
| 81 | if (!c) return; |
| 82 | avx_mutex_lock(&c->lock); |
| 83 | c->srtt_ms = 20; |
| 84 | c->rttvar_ms = 10; |
| 85 | c->min_rtt_ms = 0; |
| 86 | c->min_rtt_ts_ms = 0; |
| 87 | c->prev_packets_sent = 0; |
| 88 | c->prev_packets_lost = 0; |
| 89 | c->prev_packets_retrans = 0; |
| 90 | c->last_update_ms = 0; |
| 91 | c->loss_ratio = 0.0f; |
| 92 | c->queuing_delay_ms = 0.0f; |
| 93 | |
| 94 | c->current_bitrate_bps = c->cfg.start_bitrate_bps; |
| 95 | float rho = (float)(c->current_bitrate_bps - c->cfg.min_bitrate_bps) / |
| 96 | (float)(c->cfg.max_bitrate_bps - c->cfg.min_bitrate_bps); |
| 97 | if (rho < 0.0f) rho = 0.0f; |
| 98 | if (rho > 1.0f) rho = 1.0f; |
| 99 | c->current_quality = (uint8_t)(c->cfg.min_quality + (uint8_t)(rho * (float)(c->cfg.max_quality - c->cfg.min_quality))); |
| 100 | c->cadence = 1; |
| 101 | c->congestion_state = AVX_WAN_CONG_NORMAL; |
| 102 | |
| 103 | c->current_fec.enabled = true; |
| 104 | c->current_fec.matrix_d = 10; |
| 105 | c->current_fec.matrix_l = 5; |
| 106 | c->current_fec.enable_row_fec = false; |
| 107 | avx_mutex_unlock(&c->lock); |
| 108 | } |
| 109 | |
| 110 | /* Analyzes round-trip link feedback to adapt video bitrate, SpeedHQ quality, and FEC matrix dimensions. */ |
| 111 | void avx_rate_controller_update_telemetry( |
| 112 | avx_rate_controller_t* c, |
| 113 | const avx_rudp_telemetry_t* telem, |
| 114 | int64_t now_ms |
| 115 | ) { |
| 116 | if (!c || !telem) return; |
| 117 | |
| 118 | avx_mutex_lock(&c->lock); |
| 119 | |
| 120 | c->srtt_ms = telem->srtt_ms; |
| 121 | c->rttvar_ms = telem->rttvar_ms; |
| 122 | |
| 123 | /* 1. Track minimum baseline RTT (physical propagation delay without queue bufferbloat) */ |
| 124 | if (telem->srtt_ms > 0) { |
| 125 | if (c->min_rtt_ms == 0 || telem->srtt_ms < c->min_rtt_ms || |
| 126 | (c->min_rtt_ts_ms > 0 && (now_ms - c->min_rtt_ts_ms) > 10000)) { |
| 127 | c->min_rtt_ms = telem->srtt_ms; |
| 128 | c->min_rtt_ts_ms = now_ms; |
| 129 | } |
| 130 | } else if (c->min_rtt_ms == 0) { |
| 131 | c->min_rtt_ms = 20; |
| 132 | } |
| 133 | |
| 134 | /* 2. Compute queuing delay (gradient of RTT relative to minimum baseline) */ |
| 135 | float q_delay = (float)telem->srtt_ms - (float)c->min_rtt_ms; |
| 136 | if (q_delay < 0.0f) q_delay = 0.0f; |
| 137 | c->queuing_delay_ms = 0.7f * c->queuing_delay_ms + 0.3f * q_delay; |
| 138 | |
| 139 | /* 3. Compute differential packet loss ratio over elapsed window */ |
| 140 | uint64_t delta_sent = (telem->packets_sent > c->prev_packets_sent) |
| 141 | ? (telem->packets_sent - c->prev_packets_sent) : 0; |
| 142 | uint64_t delta_lost = ((telem->packets_lost > c->prev_packets_lost) |
| 143 | ? (telem->packets_lost - c->prev_packets_lost) : 0) + |
| 144 | ((telem->packets_retransmitted > c->prev_packets_retrans) |
| 145 | ? (telem->packets_retransmitted - c->prev_packets_retrans) : 0); |
| 146 | |
| 147 | c->prev_packets_sent = telem->packets_sent; |
| 148 | c->prev_packets_lost = telem->packets_lost; |
| 149 | c->prev_packets_retrans = telem->packets_retransmitted; |
| 150 | |
| 151 | float sample_loss = 0.0f; |
| 152 | if (delta_sent + delta_lost > 0) { |
| 153 | sample_loss = (float)delta_lost / (float)(delta_sent + delta_lost); |
| 154 | } |
| 155 | c->loss_ratio = 0.6f * c->loss_ratio + 0.4f * sample_loss; |
| 156 | |
| 157 | /* 4. GCC Congestion Assessment: delay threshold gamma adaptive to RTT */ |
| 158 | float gamma = 0.20f * (float)c->min_rtt_ms; |
| 159 | if (gamma < 15.0f) gamma = 15.0f; |
| 160 | if (gamma > 50.0f) gamma = 50.0f; |
| 161 | |
| 162 | if (c->loss_ratio >= 0.05f || c->queuing_delay_ms >= gamma) { |
| 163 | c->congestion_state = AVX_WAN_CONG_OVERUSE; |
| 164 | } else if (c->loss_ratio >= 0.02f || c->queuing_delay_ms >= (0.6f * gamma)) { |
| 165 | c->congestion_state = AVX_WAN_CONG_PRESSURE; |
| 166 | } else { |
| 167 | c->congestion_state = AVX_WAN_CONG_NORMAL; |
| 168 | } |
| 169 | |
| 170 | /* 5. Target Bitrate Adaptation */ |
| 171 | int64_t elapsed_ms = (c->last_update_ms > 0 && now_ms > c->last_update_ms) |
| 172 | ? (now_ms - c->last_update_ms) : 100; |
| 173 | if (elapsed_ms > 1000) elapsed_ms = 1000; |
| 174 | if (elapsed_ms < 20) elapsed_ms = 20; |
| 175 | |
| 176 | if (c->congestion_state == AVX_WAN_CONG_OVERUSE) { |
| 177 | /* Multiplicative decrease */ |
| 178 | float backoff = (c->loss_ratio > 0.10f) ? 0.65f : 0.80f; |
| 179 | c->current_bitrate_bps = (uint32_t)((float)c->current_bitrate_bps * backoff); |
| 180 | } else if (c->congestion_state == AVX_WAN_CONG_PRESSURE) { |
| 181 | /* Mild throttle */ |
| 182 | c->current_bitrate_bps = (uint32_t)((float)c->current_bitrate_bps * 0.95f); |
| 183 | } else { |
| 184 | /* Additive increase: probe upward when link is clean */ |
| 185 | uint32_t add_step = (uint32_t)((float)c->current_bitrate_bps * 0.05f * ((float)elapsed_ms / 1000.0f)); |
| 186 | if (add_step < 500000) add_step = 500000; /* Minimum 500 kbps probe */ |
| 187 | c->current_bitrate_bps += add_step; |
| 188 | } |
| 189 | |
| 190 | if (c->current_bitrate_bps < c->cfg.min_bitrate_bps) c->current_bitrate_bps = c->cfg.min_bitrate_bps; |
| 191 | if (c->current_bitrate_bps > c->cfg.max_bitrate_bps) c->current_bitrate_bps = c->cfg.max_bitrate_bps; |
| 192 | c->last_update_ms = now_ms; |
| 193 | |
| 194 | /* 6. SpeedHQ Quality & Cadence Mapping */ |
| 195 | float rho = (float)(c->current_bitrate_bps - c->cfg.min_bitrate_bps) / |
| 196 | (float)(c->cfg.max_bitrate_bps - c->cfg.min_bitrate_bps); |
| 197 | if (rho < 0.0f) rho = 0.0f; |
| 198 | if (rho > 1.0f) rho = 1.0f; |
| 199 | c->current_quality = (uint8_t)(c->cfg.min_quality + (uint8_t)(rho * (float)(c->cfg.max_quality - c->cfg.min_quality))); |
| 200 | |
| 201 | if (c->current_bitrate_bps < 8000000) { |
| 202 | c->cadence = 2; /* Half-fps under extreme bottleneck */ |
| 203 | } else { |
| 204 | c->cadence = 1; /* Full fps */ |
| 205 | } |
| 206 | |
| 207 | /* 7. Dynamic FEC Density Auto-Tuning */ |
| 208 | if (c->cfg.fec_auto_tune) { |
| 209 | c->current_fec.enabled = true; |
| 210 | if (c->loss_ratio < 0.005f) { |
| 211 | /* Tier 0: Clean Link (Sparse 1D Column Parity: ~6.25% overhead) */ |
| 212 | c->current_fec.matrix_d = 16; |
| 213 | c->current_fec.matrix_l = 5; |
| 214 | c->current_fec.enable_row_fec = false; |
| 215 | } else if (c->loss_ratio < 0.03f) { |
| 216 | /* Tier 1: Mild Loss (Standard 1D Column Parity: ~10% overhead) */ |
| 217 | c->current_fec.matrix_d = 10; |
| 218 | c->current_fec.matrix_l = 5; |
| 219 | c->current_fec.enable_row_fec = false; |
| 220 | } else if (c->loss_ratio < 0.08f) { |
| 221 | /* Tier 2: Moderate / Burst Loss (2D Column + Row Parity: ~25% overhead) */ |
| 222 | c->current_fec.matrix_d = 8; |
| 223 | c->current_fec.matrix_l = 4; |
| 224 | c->current_fec.enable_row_fec = true; |
| 225 | } else { |
| 226 | /* Tier 3: Severe Loss (Dense 2D Matrix Parity: ~50% overhead) */ |
| 227 | c->current_fec.matrix_d = 6; |
| 228 | c->current_fec.matrix_l = 3; |
| 229 | c->current_fec.enable_row_fec = true; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | avx_mutex_unlock(&c->lock); |
| 234 | } |
| 235 | |
| 236 | uint32_t avx_rate_controller_get_target_bitrate(const avx_rate_controller_t* c) { |
| 237 | if (!c) return 0; |
| 238 | avx_rate_controller_t* mut_c = (avx_rate_controller_t*)c; |
| 239 | avx_mutex_lock(&mut_c->lock); |
| 240 | uint32_t br = c->current_bitrate_bps; |
| 241 | avx_mutex_unlock(&mut_c->lock); |
| 242 | return br; |
| 243 | } |
| 244 | |
| 245 | uint8_t avx_rate_controller_get_quality(const avx_rate_controller_t* c) { |
| 246 | if (!c) return 96; |
| 247 | avx_rate_controller_t* mut_c = (avx_rate_controller_t*)c; |
| 248 | avx_mutex_lock(&mut_c->lock); |
| 249 | uint8_t q = c->current_quality; |
| 250 | avx_mutex_unlock(&mut_c->lock); |
| 251 | return q; |
| 252 | } |
| 253 | |
| 254 | void avx_rate_controller_get_fec_config(const avx_rate_controller_t* c, avx_fec_config_t* out_fec) { |
| 255 | if (!c || !out_fec) return; |
| 256 | avx_rate_controller_t* mut_c = (avx_rate_controller_t*)c; |
| 257 | avx_mutex_lock(&mut_c->lock); |
| 258 | *out_fec = c->current_fec; |
| 259 | avx_mutex_unlock(&mut_c->lock); |
| 260 | } |
| 261 | |
| 262 | uint32_t avx_rate_controller_get_cadence(const avx_rate_controller_t* c) { |
| 263 | if (!c) return 1; |
| 264 | avx_rate_controller_t* mut_c = (avx_rate_controller_t*)c; |
| 265 | avx_mutex_lock(&mut_c->lock); |
| 266 | uint32_t cad = c->cadence; |
| 267 | avx_mutex_unlock(&mut_c->lock); |
| 268 | return cad; |
| 269 | } |
| 270 | |
| 271 | avx_wan_congestion_state_t avx_rate_controller_get_congestion_state(const avx_rate_controller_t* c) { |
| 272 | if (!c) return AVX_WAN_CONG_NORMAL; |
| 273 | avx_rate_controller_t* mut_c = (avx_rate_controller_t*)c; |
| 274 | avx_mutex_lock(&mut_c->lock); |
| 275 | avx_wan_congestion_state_t st = c->congestion_state; |
| 276 | avx_mutex_unlock(&mut_c->lock); |
| 277 | return st; |
| 278 | } |
| 279 | |
| 280 | /* Pushes updated pacing rate, congestion window, and FEC matrices to active RUDP sender. */ |
| 281 | void avx_rate_controller_apply_to_sender(const avx_rate_controller_t* c, avx_udp_rudp_sender_t* s) { |
| 282 | if (!c || !s) return; |
| 283 | avx_fec_config_t fec; |
| 284 | avx_rate_controller_get_fec_config(c, &fec); |
| 285 | avx_udp_rudp_sender_set_fec(s, &fec); |
| 286 | } |