Raw ↗
+ +
2da5fe7 OpenAVX v0.2.0 - High-performance open-standard IP audio and video transport suite
saifmukhtar · 2026-11-24
c · 1654 lines · 59.0 KB · SPDX: LGPL-2.1-or-later
main
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-or-later
3 *
4 * Copyright (c) 2017 Steinar H. Gunderson
5 * Copyright (c) 2017-2026 FFmpeg contributors
6 * Copyright (c) 2026 Saif Mukhtar (integration and optimizations for OpenAVX)
7 *
8 * OpenAVX - Open Audio-Video eXchange
9 * src/avx_speedhq.c - Standalone SpeedHQ intra-frame DCT encoder and decoder
10 *
11 * Derived from FFmpeg libavcodec (speedhq.c / speedhqenc.c).
12 * Licensed under the GNU Lesser General Public License version 2.1 or later.
13 */
14  
15 #define _GNU_SOURCE
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19 #include <stdbool.h>
20 #include <stdint.h>
21 #include "avx_platform.h"
22 #include "avx_internal.h"
23 #include "avx_speedhq.h"
24 #include "avx_simd.h"
25  
26 /* SpeedHQ unscaled base quantization matrix (MPEG-2 intra baseline) */
27 static const uint8_t s_unscaled_quant_matrix[64] = {
28 16, 16, 19, 22, 26, 27, 29, 34,
29 16, 16, 22, 24, 27, 29, 34, 37,
30 19, 22, 26, 27, 29, 34, 34, 38,
31 22, 22, 26, 27, 29, 34, 37, 40,
32 22, 26, 27, 29, 32, 35, 40, 48,
33 26, 27, 29, 32, 35, 40, 48, 58,
34 26, 27, 29, 34, 38, 46, 56, 69,
35 27, 29, 35, 38, 46, 56, 69, 83
36 };
37  
38 /* Standard 8x8 Zig-Zag scan order */
39 static const uint8_t s_zigzag[64] = {
40 0, 1, 8, 16, 9, 2, 3, 10,
41 17, 24, 32, 25, 18, 11, 4, 5,
42 12, 19, 26, 33, 40, 48, 41, 34,
43 27, 20, 13, 6, 7, 14, 21, 28,
44 35, 42, 49, 56, 57, 50, 43, 36,
45 29, 22, 15, 23, 30, 37, 44, 51,
46 58, 59, 52, 45, 38, 31, 39, 46,
47 53, 60, 61, 54, 47, 55, 62, 63
48 };
49  
50 /* SpeedHQ AC Variable Length Coding Entry */
51 typedef struct {
52 uint16_t code; /* VLC bit pattern */
53 uint8_t len; /* Code length in bits (2..16) */
54 uint8_t run; /* Zero run count (0..31) */
55 uint8_t level; /* Absolute coefficient magnitude (1..40) */
56 } avx_shq_vlc_entry_t;
57  
58 #define SPEEDHQ_AC_VLC_COUNT 121
59 #define SPEEDHQ_AC_SHORT_COUNT 41
60  
61 #define SPEEDHQ_ESCAPE_CODE 0x0020
62 #define SPEEDHQ_ESCAPE_BITS 6
63 #define SPEEDHQ_EOB_CODE 0x0006
64 #define SPEEDHQ_EOB_BITS 4
65  
66 #define SHQ_DEC_EOB 254
67 #define SHQ_DEC_ESCAPE 253
68 #define SHQ_DEC_LONG 0
69  
70 /* Re-packed AC VLC Table sorted strictly by (len, code) */
71 static const avx_shq_vlc_entry_t s_speedhq_ac_table[121] = {
72 { 0x0001, 2, 0, 1 }, { 0x0002, 3, 1, 1 }, { 0x0003, 3, 0, 2 }, { 0x000E, 4, 0, 3 },
73 { 0x0007, 5, 0, 4 }, { 0x000C, 5, 1, 2 }, { 0x0014, 5, 2, 1 }, { 0x0017, 5, 0, 5 },
74 { 0x001C, 5, 3, 1 }, { 0x0008, 6, 0, 7 }, { 0x0018, 6, 4, 1 }, { 0x0028, 6, 0, 6 },
75 { 0x0038, 6, 5, 1 }, { 0x000F, 7, 9, 1 }, { 0x0010, 7, 7, 1 }, { 0x001F, 7, 0, 9 },
76 { 0x002F, 7, 10, 1 }, { 0x0030, 7, 6, 1 }, { 0x004F, 7, 1, 3 }, { 0x0050, 7, 8, 1 },
77 { 0x006F, 7, 0, 8 }, { 0x0070, 7, 2, 2 }, { 0x0004, 8, 1, 5 }, { 0x0024, 8, 13, 1 },
78 { 0x003F, 8, 2, 3 }, { 0x0044, 8, 0, 11 }, { 0x005F, 8, 0, 12 }, { 0x0064, 8, 3, 2 },
79 { 0x007F, 8, 0, 14 }, { 0x0084, 8, 11, 1 }, { 0x00A4, 8, 12, 1 }, { 0x00BF, 8, 4, 2 },
80 { 0x00C4, 8, 0, 10 }, { 0x00DF, 8, 0, 13 }, { 0x00E4, 8, 1, 4 }, { 0x00FF, 8, 0, 15 },
81 { 0x0040, 9, 5, 2 }, { 0x0140, 9, 14, 1 }, { 0x01C0, 9, 15, 1 }, { 0x00C0, 10, 2, 4 },
82 { 0x02C0, 10, 16, 1 }, { 0x0080, 12, 2, 9 }, { 0x0180, 12, 2, 6 }, { 0x0280, 12, 2, 7 },
83 { 0x0380, 12, 3, 3 }, { 0x0480, 12, 4, 3 }, { 0x0580, 12, 18, 1 }, { 0x0680, 12, 21, 1 },
84 { 0x0780, 12, 6, 2 }, { 0x0880, 12, 8, 2 }, { 0x0980, 12, 19, 1 }, { 0x0A80, 12, 7, 2 },
85 { 0x0B80, 12, 4, 4 }, { 0x0C80, 12, 2, 8 }, { 0x0D80, 12, 3, 5 }, { 0x0E80, 12, 20, 1 },
86 { 0x0F80, 12, 17, 1 }, { 0x0100, 13, 10, 2 }, { 0x0300, 13, 1, 19 }, { 0x0500, 13, 2, 5 },
87 { 0x0700, 13, 25, 1 }, { 0x0900, 13, 5, 3 }, { 0x0B00, 13, 2, 10 }, { 0x0D00, 13, 1, 6 },
88 { 0x0F00, 13, 23, 1 }, { 0x1100, 13, 9, 2 }, { 0x1300, 13, 2, 11 }, { 0x1500, 13, 1, 7 },
89 { 0x1700, 13, 24, 1 }, { 0x1900, 13, 3, 4 }, { 0x1B00, 13, 26, 1 }, { 0x1D00, 13, 1, 20 },
90 { 0x1F00, 13, 22, 1 }, { 0x0200, 14, 0, 31 }, { 0x0600, 14, 0, 23 }, { 0x0A00, 14, 0, 27 },
91 { 0x0E00, 14, 0, 19 }, { 0x1200, 14, 0, 29 }, { 0x1600, 14, 0, 21 }, { 0x1A00, 14, 0, 25 },
92 { 0x1E00, 14, 0, 17 }, { 0x2200, 14, 0, 30 }, { 0x2600, 14, 0, 22 }, { 0x2A00, 14, 0, 26 },
93 { 0x2E00, 14, 0, 18 }, { 0x3200, 14, 0, 28 }, { 0x3600, 14, 0, 20 }, { 0x3A00, 14, 0, 24 },
94 { 0x3E00, 14, 0, 16 }, { 0x0400, 15, 0, 40 }, { 0x0C00, 15, 0, 32 }, { 0x1400, 15, 0, 36 },
95 { 0x1C00, 15, 1, 11 }, { 0x2400, 15, 0, 38 }, { 0x2C00, 15, 1, 13 }, { 0x3400, 15, 0, 34 },
96 { 0x3C00, 15, 1, 9 }, { 0x4400, 15, 0, 39 }, { 0x4C00, 15, 1, 14 }, { 0x5400, 15, 0, 35 },
97 { 0x5C00, 15, 1, 10 }, { 0x6400, 15, 0, 37 }, { 0x6C00, 15, 1, 12 }, { 0x7400, 15, 0, 33 },
98 { 0x7C00, 15, 1, 8 }, { 0x0800, 16, 1, 18 }, { 0x1800, 16, 13, 2 }, { 0x2800, 16, 6, 3 },
99 { 0x3800, 16, 30, 1 }, { 0x4800, 16, 1, 16 }, { 0x5800, 16, 11, 2 }, { 0x6800, 16, 15, 2 },
100 { 0x7800, 16, 28, 1 }, { 0x8800, 16, 1, 17 }, { 0x9800, 16, 12, 2 }, { 0xA800, 16, 16, 2 },
101 { 0xB800, 16, 29, 1 }, { 0xC800, 16, 1, 15 }, { 0xD800, 16, 31, 1 }, { 0xE800, 16, 14, 2 },
102 { 0xF800, 16, 27, 1 },
103 };
104  
105 /* Fast O(1) Prefix Lookups */
106 static uint8_t s_shq_dec_lut[1024];
107 static int16_t s_shq_enc_lut[32][41];
108 static bool s_shq_tables_ready = false;
109  
110 /*
111 * Populates 10-bit direct lookup table (s_shq_dec_lut[1024]) for O(1) AC VLC decoding
112 * of the 41 most common codes, plus EOB and Escape tokens.
113 */
114 static void speedhq_init_vlc_tables(void)
115 {
116 if (s_shq_tables_ready) return;
117 memset(s_shq_dec_lut, 0, sizeof(s_shq_dec_lut));
118 memset(s_shq_enc_lut, 0xFF, sizeof(s_shq_enc_lut));
119  
120 /* Mark EOB (0x0006, 4 bits) in 10-bit LUT */
121 for (int v = 0; v < 1024; ++v) {
122 if ((v & 0x0F) == SPEEDHQ_EOB_CODE) s_shq_dec_lut[v] = SHQ_DEC_EOB;
123 }
124 /* Mark Escape (0x0020, 6 bits) in 10-bit LUT */
125 for (int v = 0; v < 1024; ++v) {
126 if ((v & 0x3F) == SPEEDHQ_ESCAPE_CODE) s_shq_dec_lut[v] = SHQ_DEC_ESCAPE;
127 }
128  
129 /* Populate 41 short codes (len <= 10) in 10-bit LUT */
130 for (int i = 0; i < SPEEDHQ_AC_SHORT_COUNT; ++i) {
131 const avx_shq_vlc_entry_t *e = &s_speedhq_ac_table[i];
132 uint32_t mask = (1U << e->len) - 1;
133 for (int v = 0; v < 1024; ++v) {
134 if ((v & mask) == e->code) {
135 s_shq_dec_lut[v] = (uint8_t)(i + 1);
136 }
137 }
138 }
139  
140 /* Populate 2D encoder LUT */
141 for (int i = 0; i < SPEEDHQ_AC_VLC_COUNT; ++i) {
142 const avx_shq_vlc_entry_t *e = &s_speedhq_ac_table[i];
143 if (e->run < 32 && e->level <= 40) {
144 s_shq_enc_lut[e->run][e->level] = (int16_t)i;
145 }
146 }
147  
148 s_shq_tables_ready = true;
149 }
150  
151  
152  
153 /* Fast reverse DC codes matching little-endian MPEG-2 / SpeedHQ */
154 static const uint16_t s_dc_lum_bits[12] = { 3, 2, 2, 3, 3, 4, 5, 6, 7, 8, 9, 9 };
155 static const uint16_t s_dc_lum_code[12] = {
156 0x1, 0x0, 0x2, 0x5, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF
157 };
158 static const uint16_t s_dc_chr_bits[12] = { 2, 2, 2, 3, 4, 5, 6, 7, 8, 9, 10, 10 };
159 static const uint16_t s_dc_chr_code[12] = {
160 0x0, 0x2, 0x1, 0x3, 0x7, 0xF, 0x1F, 0x3F, 0x7F, 0xFF, 0x1FF, 0x3FF
161 };
162  
163 /* Little-endian Bitwriter (64-bit accumulator with optional growable slice buffer) */
164 typedef struct {
165 uint8_t *buf;
166 size_t size;
167 size_t capacity;
168 size_t max_capacity;
169 bool can_grow;
170 bool overflow;
171 uint64_t bit_buf;
172 int bit_count;
173 } avx_bitwriter_t;
174  
175 static inline void bitwriter_init_ex(avx_bitwriter_t *bw, uint8_t *buf, size_t capacity,
176 bool can_grow, size_t max_capacity)
177 {
178 bw->buf = buf;
179 bw->size = 0;
180 bw->capacity = capacity;
181 bw->max_capacity = max_capacity;
182 bw->can_grow = can_grow;
183 bw->overflow = false;
184 bw->bit_buf = 0;
185 bw->bit_count = 0;
186 }
187  
188 static inline void bitwriter_init(avx_bitwriter_t *bw, uint8_t *buf, size_t capacity)
189 {
190 bitwriter_init_ex(bw, buf, capacity, false, capacity);
191 }
192  
193 static inline bool bitwriter_ensure_room(avx_bitwriter_t *bw, size_t need_bytes)
194 {
195 if (bw->size + need_bytes <= bw->capacity) return true;
196 if (!bw->can_grow || bw->capacity >= bw->max_capacity) {
197 bw->overflow = true;
198 return false;
199 }
200 size_t new_cap = bw->capacity ? bw->capacity * 2 : 65536;
201 while (new_cap < bw->size + need_bytes && new_cap < bw->max_capacity) {
202 if (new_cap > bw->max_capacity / 2) {
203 new_cap = bw->max_capacity;
204 break;
205 }
206 new_cap *= 2;
207 }
208 if (new_cap > bw->max_capacity) new_cap = bw->max_capacity;
209 if (new_cap < bw->size + need_bytes) {
210 bw->overflow = true;
211 return false;
212 }
213 uint8_t *nb = (uint8_t *)realloc(bw->buf, new_cap);
214 if (!nb) {
215 bw->overflow = true;
216 return false;
217 }
218 bw->buf = nb;
219 bw->capacity = new_cap;
220 return true;
221 }
222  
223 /*
224 * Appends bits in little-endian order (LSB first into accumulator), flushing
225 * 32-bit words in little-endian byte order as required by SpeedHQ bitstreams.
226 */
227 static inline void bitwriter_put_le(avx_bitwriter_t *bw, int n, uint32_t val)
228 {
229 uint64_t mask = (n >= 32) ? 0xFFFFFFFFULL : ((1ULL << n) - 1ULL);
230 bw->bit_buf |= ((uint64_t)val & mask) << bw->bit_count;
231 bw->bit_count += n;
232 if (bw->bit_count >= 32) {
233 if (bw->size + 4 <= bw->capacity || bitwriter_ensure_room(bw, 4)) {
234 uint32_t lo = (uint32_t)(bw->bit_buf & 0xFFFFFFFFULL);
235 bw->buf[bw->size + 0] = (uint8_t)(lo);
236 bw->buf[bw->size + 1] = (uint8_t)(lo >> 8);
237 bw->buf[bw->size + 2] = (uint8_t)(lo >> 16);
238 bw->buf[bw->size + 3] = (uint8_t)(lo >> 24);
239 bw->size += 4;
240 bw->bit_buf >>= 32;
241 bw->bit_count -= 32;
242 } else {
243 while (bw->bit_count >= 8) {
244 if (bw->size < bw->capacity) {
245 bw->buf[bw->size++] = (uint8_t)(bw->bit_buf & 0xFFU);
246 } else {
247 bw->overflow = true;
248 }
249 bw->bit_buf >>= 8;
250 bw->bit_count -= 8;
251 }
252 }
253 }
254 }
255  
256 static inline void bitwriter_reserve_3(avx_bitwriter_t *bw)
257 {
258 /* Called at byte-aligned slice boundary (bit_count == 0) to hold 24-bit slice length */
259 if (bitwriter_ensure_room(bw, 3)) {
260 bw->buf[bw->size + 0] = 0;
261 bw->buf[bw->size + 1] = 0;
262 bw->buf[bw->size + 2] = 0;
263 bw->size += 3;
264 }
265 }
266  
267 static inline void bitwriter_flush(avx_bitwriter_t *bw)
268 {
269 while (bw->bit_count >= 8) {
270 if (bitwriter_ensure_room(bw, 1)) {
271 bw->buf[bw->size++] = (uint8_t)(bw->bit_buf & 0xFFU);
272 }
273 bw->bit_buf >>= 8;
274 bw->bit_count -= 8;
275 }
276 if (bw->bit_count > 0) {
277 if (bitwriter_ensure_room(bw, 1)) {
278 bw->buf[bw->size++] = (uint8_t)(bw->bit_buf & 0xFFU);
279 }
280 bw->bit_buf = 0;
281 bw->bit_count = 0;
282 }
283 }
284  
285 /* Precomputed per-frame quantization thresholds & fixed-point division reciprocals */
286 typedef struct {
287 uint32_t quant_zz[64];
288 uint32_t mul_zz[64];
289 int32_t dec_quant_zz[64];
290 } avx_shq_quant_ctx_t;
291  
292 static inline void speedhq_prepare_quant_ctx(avx_shq_quant_ctx_t *qctx, int qscale)
293 {
294 qctx->quant_zz[0] = 1;
295 qctx->mul_zz[0] = 0x80000000U;
296 qctx->dec_quant_zz[0] = 1;
297 for (int i = 1; i < 64; ++i) {
298 int mat = s_unscaled_quant_matrix[s_zigzag[i]];
299 int dq = (mat * qscale) / 16;
300 int eq = (dq < 1) ? 1 : dq;
301 qctx->dec_quant_zz[i] = (int32_t)dq;
302 qctx->quant_zz[i] = (uint32_t)eq;
303 qctx->mul_zz[i] = (uint32_t)(((1ULL << 31) + (uint32_t)eq - 1ULL) / (uint32_t)eq);
304 }
305 }
306  
307 /* Little-endian Bitreader */
308 typedef struct {
309 const uint8_t *buf;
310 size_t size;
311 size_t pos;
312 uint32_t bit_buf;
313 int bit_count;
314 } avx_bitreader_t;
315  
316 static inline void bitreader_init(avx_bitreader_t *br, const uint8_t *buf, size_t size)
317 {
318 br->buf = buf;
319 br->size = size;
320 br->pos = 0;
321 br->bit_buf = 0;
322 br->bit_count = 0;
323 }
324  
325 static inline void bitreader_refill(avx_bitreader_t *br)
326 {
327 if (br->bit_count < 0) br->bit_count = 0;
328 while (br->bit_count <= 24 && br->pos < br->size) {
329 br->bit_buf |= ((uint32_t)br->buf[br->pos++]) << br->bit_count;
330 br->bit_count += 8;
331 }
332 }
333  
334 static inline uint32_t bitreader_get_le(avx_bitreader_t *br, int n)
335 {
336 bitreader_refill(br);
337 uint32_t val = (n >= 32) ? br->bit_buf : (br->bit_buf & ((1U << n) - 1));
338 br->bit_buf = (n >= 32) ? 0 : (br->bit_buf >> n);
339 br->bit_count -= n;
340 if (br->bit_count < 0) br->bit_count = 0;
341 return val;
342 }
343  
344 static inline uint32_t bitreader_peek_le(avx_bitreader_t *br, int n)
345 {
346 bitreader_refill(br);
347 return (n >= 32) ? br->bit_buf : (br->bit_buf & ((1U << n) - 1));
348 }
349  
350 static inline void bitreader_skip(avx_bitreader_t *br, int n)
351 {
352 br->bit_buf = (n >= 32) ? 0 : (br->bit_buf >> n);
353 br->bit_count -= n;
354 if (br->bit_count < 0) br->bit_count = 0;
355 }
356  
357 /* Fast 2D Integer Forward DCT (AAN integer approximation - SIMD Accelerated) */
358 static void fdct_8x8(const int16_t in[64], int16_t out[64])
359 {
360 avx_simd_fdct_8x8(in, out);
361 }
362  
363 /* Fast 2D Integer Inverse DCT (IDCT) with saturation to [0, 255] - SIMD Accelerated */
364 static void idct_8x8_put(const int16_t in[64], uint8_t *dest, int stride)
365 {
366 avx_simd_idct_8x8_put(in, dest, stride);
367 }
368  
369 /* Encode a DC difference */
370 static inline void encode_dc_diff(avx_bitwriter_t *bw, int diff, int component)
371 {
372 if (diff == 0) {
373 if (component == 0) bitwriter_put_le(bw, s_dc_lum_bits[0], s_dc_lum_code[0]);
374 else bitwriter_put_le(bw, s_dc_chr_bits[0], s_dc_chr_code[0]);
375 return;
376 }
377  
378 int abs_d = diff < 0 ? -diff : diff;
379 #if defined(__GNUC__) || defined(__clang__)
380 int index = (abs_d >= 2048) ? 11 : (32 - __builtin_clz((unsigned)abs_d));
381 #else
382 int index = 0;
383 while ((1 << index) <= abs_d && index < 11) index++;
384 #endif
385  
386 int extra = diff > 0 ? diff : (diff - 1) & ((1 << index) - 1);
387  
388 if (component == 0) {
389 bitwriter_put_le(bw, s_dc_lum_bits[index], s_dc_lum_code[index]);
390 } else {
391 bitwriter_put_le(bw, s_dc_chr_bits[index], s_dc_chr_code[index]);
392 }
393 bitwriter_put_le(bw, index, (uint32_t)extra);
394 }
395  
396 /* Encode 8x8 block into bitwriter using precomputed division-free quantization */
397 static void encode_block_c99(avx_bitwriter_t *bw, const int16_t block[64],
398 int *last_dc, int component, const avx_shq_quant_ctx_t *qctx)
399 {
400 /* 1. DC Coefficient: diff is last_dc - block[0] (opposite in SpeedHQ) */
401 int dc = (block[0] + 1) >> 1;
402 int diff = *last_dc - dc;
403 encode_dc_diff(bw, diff, component);
404 *last_dc = dc;
405  
406 /* 2. AC Coefficients (division-free via exact 31-bit fixed-point reciprocals) */
407 int run = 0;
408 for (int i = 1; i < 64; ++i) {
409 int32_t val = (int32_t)block[s_zigzag[i]];
410 int32_t mask = val >> 31;
411 uint32_t aval = (uint32_t)((val ^ mask) - mask);
412 if (aval < qctx->quant_zz[i]) {
413 run++;
414 continue;
415 }
416  
417 uint32_t alevel = (uint32_t)(((uint64_t)aval * (uint64_t)qctx->mul_zz[i]) >> 31);
418 int sign = mask & 1;
419  
420 /* Check if (run, alevel) matches precomputed VLC table via O(1) LUT */
421 int code_idx = -1;
422 if (run < 32 && alevel <= 40U) {
423 code_idx = s_shq_enc_lut[run][alevel];
424 }
425  
426 if (code_idx >= 0) {
427 /* Emit VLC code + sign bit */
428 const avx_shq_vlc_entry_t *entry = &s_speedhq_ac_table[code_idx];
429 uint32_t vlc = (uint32_t)entry->code | ((uint32_t)sign << entry->len);
430 bitwriter_put_le(bw, entry->len + 1, vlc);
431 } else {
432 /* Emit Escape (0x20, 6 bits) + run (6 bits) + (level + 2048) (12 bits) */
433 int level = sign ? -(int)alevel : (int)alevel;
434 uint32_t esc = SPEEDHQ_ESCAPE_CODE | ((uint32_t)run << 6) | ((uint32_t)(level + 2048) << 12);
435 bitwriter_put_le(bw, 24, esc);
436 }
437 run = 0;
438 }
439  
440 /* 3. End-Of-Block (EOB): 4 bits (0x0006) */
441 bitwriter_put_le(bw, SPEEDHQ_EOB_BITS, SPEEDHQ_EOB_CODE);
442 }
443  
444 /* Helper to decode a single 8x8 DCT block */
445 static void decode_single_block_c99(avx_bitreader_t *br, int *last_dc, int comp,
446 const avx_shq_quant_ctx_t *qctx, uint8_t *dest)
447 {
448 int16_t block[64] = {0};
449  
450 /* Decode DC */
451 int index = 0;
452 if (comp == 0) {
453 while (index < 12) {
454 if (bitreader_peek_le(br, s_dc_lum_bits[index]) == s_dc_lum_code[index]) {
455 bitreader_skip(br, s_dc_lum_bits[index]);
456 break;
457 }
458 index++;
459 }
460 } else {
461 while (index < 12) {
462 if (bitreader_peek_le(br, s_dc_chr_bits[index]) == s_dc_chr_code[index]) {
463 bitreader_skip(br, s_dc_chr_bits[index]);
464 break;
465 }
466 index++;
467 }
468 }
469  
470 int diff = 0;
471 if (index > 0 && index < 12) {
472 uint32_t extra = bitreader_get_le(br, index);
473 if (extra & (1 << (index - 1))) {
474 diff = (int)extra;
475 } else {
476 diff = (int)extra - ((1 << index) - 1);
477 }
478 }
479  
480 *last_dc -= diff;
481 block[s_zigzag[0]] = (int16_t)(*last_dc << 1);
482  
483 /* Decode AC coefficients */
484 int ac_idx = 1;
485 while (ac_idx < 64) {
486 if (br->pos >= br->size && br->bit_count <= 0) break;
487  
488 uint32_t peek10 = bitreader_peek_le(br, 10);
489 uint8_t sym = s_shq_dec_lut[peek10];
490  
491 if (sym == SHQ_DEC_EOB) {
492 bitreader_skip(br, SPEEDHQ_EOB_BITS);
493 break;
494 }
495  
496 if (sym == SHQ_DEC_ESCAPE) {
497 bitreader_skip(br, SPEEDHQ_ESCAPE_BITS);
498 int run = (int)bitreader_get_le(br, 6);
499 int level = (int)bitreader_get_le(br, 12) - 2048;
500 ac_idx += run;
501 if (ac_idx < 64) {
502 block[s_zigzag[ac_idx]] = (int16_t)(level * qctx->dec_quant_zz[ac_idx]);
503 }
504 ac_idx++;
505 continue;
506 }
507  
508 if (sym != SHQ_DEC_LONG) {
509 const avx_shq_vlc_entry_t *entry = &s_speedhq_ac_table[sym - 1];
510 int sign;
511 if (entry->len <= 9) {
512 sign = (int)((peek10 >> entry->len) & 1U);
513 bitreader_skip(br, entry->len + 1);
514 } else {
515 bitreader_skip(br, entry->len);
516 sign = (int)bitreader_get_le(br, 1);
517 }
518 int level = sign ? -(int)entry->level : (int)entry->level;
519  
520 ac_idx += entry->run;
521 if (ac_idx < 64) {
522 block[s_zigzag[ac_idx]] = (int16_t)(level * qctx->dec_quant_zz[ac_idx]);
523 }
524 ac_idx++;
525 continue;
526 }
527  
528 /* Fallback for long codes (length >= 12, entries 41..120) */
529 bool matched = false;
530 for (int c = SPEEDHQ_AC_SHORT_COUNT; c < SPEEDHQ_AC_VLC_COUNT; ++c) {
531 const avx_shq_vlc_entry_t *entry = &s_speedhq_ac_table[c];
532 if (bitreader_peek_le(br, entry->len) == entry->code) {
533 bitreader_skip(br, entry->len);
534 int sign = (int)bitreader_get_le(br, 1);
535 int level = (int)entry->level;
536 if (sign) level = -level;
537  
538 ac_idx += entry->run;
539 if (ac_idx < 64) {
540 block[s_zigzag[ac_idx]] = (int16_t)(level * qctx->dec_quant_zz[ac_idx]);
541 }
542 ac_idx++;
543 matched = true;
544 break;
545 }
546 }
547 if (!matched) {
548 if (br->pos >= br->size && br->bit_count <= 0) break;
549 bitreader_skip(br, 1);
550 }
551 }
552  
553 idct_8x8_put(block, dest, 8);
554 }
555  
556 /* Encode a 16x8 block of Alpha values using SpeedHQ RLE */
557 static void encode_alpha_block(avx_bitwriter_t *bw, const uint8_t *src, int stride, int max_rows, uint8_t last_alpha[16])
558 {
559 uint8_t diff_block[128];
560 if (src && max_rows > 0) {
561 for (int y = 0; y < 8; ++y) {
562 int y_clamped = (y < max_rows) ? y : (max_rows - 1);
563 for (int x = 0; x < 16; ++x) {
564 uint8_t cur = src[(size_t)y_clamped * stride + x];
565 diff_block[y * 16 + x] = (uint8_t)(last_alpha[x] - cur);
566 last_alpha[x] = cur;
567 }
568 }
569 } else {
570 for (int y = 0; y < 8; ++y) {
571 for (int x = 0; x < 16; ++x) {
572 diff_block[y * 16 + x] = (uint8_t)(last_alpha[x] - 255);
573 last_alpha[x] = 255;
574 }
575 }
576 }
577  
578 int i = 0;
579 while (i < 128) {
580 int run = 0;
581 while (i + run < 128 && diff_block[i + run] == 0) {
582 run++;
583 }
584 if (i + run == 128) {
585 bitwriter_put_le(bw, 3, 3); /* EOB */
586 break;
587 }
588  
589 if (run == 0) {
590 bitwriter_put_le(bw, 1, 0);
591 } else if (run >= 1 && run <= 4) {
592 bitwriter_put_le(bw, 4, ((uint32_t)(run - 1) << 2) | 1);
593 } else {
594 bitwriter_put_le(bw, 10, ((uint32_t)run << 3) | 7);
595 }
596 i += run;
597  
598 int8_t sdiff = (int8_t)diff_block[i];
599 if (sdiff == 1) {
600 bitwriter_put_le(bw, 2, 1);
601 } else if (sdiff == -1) {
602 bitwriter_put_le(bw, 2, 3);
603 } else if (sdiff >= 2 && sdiff <= 5) {
604 bitwriter_put_le(bw, 5, ((uint32_t)(sdiff - 2) << 3) | 2);
605 } else if (sdiff >= -5 && sdiff <= -2) {
606 bitwriter_put_le(bw, 5, (((uint32_t)(-sdiff - 2)) << 3) | (1 << 2) | 2);
607 } else {
608 bitwriter_put_le(bw, 10, ((uint32_t)(uint8_t)diff_block[i]) << 2);
609 }
610 i++;
611 }
612 }
613  
614 /* Decode a 16x8 block of Alpha values using SpeedHQ RLE */
615 static void decode_alpha_block(avx_bitreader_t *br, uint8_t *dest, int stride, int max_rows, uint8_t last_alpha[16])
616 {
617 uint8_t block[128];
618 memset(block, 0, sizeof(block));
619 int i = 0;
620  
621 while (i < 128) {
622 if (br->pos >= br->size && br->bit_count <= 0) break;
623  
624 /* EOB: 110 (3 bits, code 3) */
625 if (bitreader_peek_le(br, 3) == 3) {
626 bitreader_skip(br, 3);
627 break;
628 }
629  
630 /* Decode run */
631 int run = 0;
632 if (bitreader_peek_le(br, 1) == 0) {
633 bitreader_skip(br, 1);
634 run = 0;
635 } else if ((bitreader_peek_le(br, 2) & 3) == 1) {
636 uint32_t code = bitreader_get_le(br, 4);
637 run = (int)(code >> 2) + 1;
638 } else if ((bitreader_peek_le(br, 3) & 7) == 7) {
639 uint32_t code = bitreader_get_le(br, 10);
640 run = (int)(code >> 3);
641 } else {
642 bitreader_skip(br, 1);
643 continue;
644 }
645  
646 i += run;
647 if (i >= 128) break;
648  
649 /* Decode level */
650 int level = 0;
651 if ((bitreader_peek_le(br, 1) & 1) == 1) {
652 uint32_t code = bitreader_get_le(br, 2);
653 level = (code >> 1) ? -1 : 1;
654 } else if ((bitreader_peek_le(br, 2) & 3) == 2) {
655 uint32_t code = bitreader_get_le(br, 5);
656 int sign = (int)((code >> 2) & 1);
657 int val = (int)(code >> 3) + 2;
658 level = sign ? -val : val;
659 } else if ((bitreader_peek_le(br, 2) & 3) == 0) {
660 uint32_t code = bitreader_get_le(br, 10);
661 level = (int)(code >> 2);
662 } else {
663 bitreader_skip(br, 1);
664 continue;
665 }
666  
667 block[i++] = (uint8_t)level;
668 }
669  
670 if (dest && max_rows > 0) {
671 for (int y = 0; y < 8; ++y) {
672 for (int x = 0; x < 16; ++x) {
673 last_alpha[x] -= block[y * 16 + x];
674 }
675 if (y < max_rows) {
676 memcpy(dest + (size_t)y * stride, last_alpha, 16);
677 }
678 }
679 } else {
680 for (int y = 0; y < 8; ++y) {
681 for (int x = 0; x < 16; ++x) {
682 last_alpha[x] -= block[y * 16 + x];
683 }
684 }
685 }
686 }
687  
688 /*
689 * C99 Standalone Encoder: 1080p UYVY + Alpha -> SpeedHQ
690 */
691 static volatile uint8_t s_speedhq_quality = 96;
692  
693 void avx_speedhq_set_quality(uint8_t quality) {
694 if (quality < 20) quality = 20;
695 if (quality > 98) quality = 98;
696 s_speedhq_quality = quality;
697 }
698  
699 uint8_t avx_speedhq_get_quality(void) {
700 return s_speedhq_quality;
701 }
702  
703 #define SHQ_POOL_MAX_WORKERS 3
704 #define SHQ_TASK_QUEUE_CAP 32
705 #define SHQ_SCRATCH_SLOTS 4
706  
707 typedef void (*shq_slice_fn_t)(void *ctx, int slice_idx);
708  
709 typedef struct {
710 avx_mutex_t lock;
711 avx_cond_t cond;
712 int remaining;
713 } avx_shq_job_t;
714  
715 typedef struct {
716 shq_slice_fn_t fn;
717 void *ctx;
718 int slice_idx;
719 avx_shq_job_t *job;
720 } avx_shq_task_t;
721  
722 typedef struct {
723 bool in_use;
724 uint8_t *buf[3];
725 size_t cap[3];
726 } avx_shq_scratch_slot_t;
727  
728 typedef struct {
729 avx_mutex_t lock;
730 avx_cond_t work_cond;
731 bool started;
732 bool shutdown;
733 #if defined(AVX_OS_POSIX)
734 pid_t owner_pid;
735 #endif
736 int num_workers;
737 avx_thread_t workers[SHQ_POOL_MAX_WORKERS];
738 avx_shq_task_t queue[SHQ_TASK_QUEUE_CAP];
739 int q_head;
740 int q_count;
741 avx_shq_scratch_slot_t scratch[SHQ_SCRATCH_SLOTS];
742 } avx_shq_pool_t;
743  
744 static avx_shq_pool_t s_shq_pool = {
745 .lock = AVX_MUTEX_INITIALIZER,
746 .work_cond = AVX_COND_INITIALIZER,
747 .started = false,
748 .shutdown = false,
749 .num_workers = 0,
750 .q_head = 0,
751 .q_count = 0
752 };
753  
754 static int speedhq_cpu_count(void)
755 {
756 const char *env = getenv("OPENAVX_SPEEDHQ_THREADS");
757 if (env && env[0] != '\0') {
758 int forced = atoi(env);
759 if (forced >= 1) return (forced > 4) ? 4 : forced;
760 }
761 #if defined(AVX_OS_WINDOWS)
762 SYSTEM_INFO si;
763 GetSystemInfo(&si);
764 return (si.dwNumberOfProcessors > 0) ? (int)si.dwNumberOfProcessors : 1;
765 #else
766 long n = sysconf(_SC_NPROCESSORS_ONLN);
767 return (n > 0) ? (int)n : 1;
768 #endif
769 }
770  
771 static void *shq_worker_main(void *arg)
772 {
773 (void)arg;
774 while (1) {
775 avx_mutex_lock(&s_shq_pool.lock);
776 while (!s_shq_pool.shutdown && s_shq_pool.q_count == 0) {
777 avx_cond_wait(&s_shq_pool.work_cond, &s_shq_pool.lock);
778 }
779 if (s_shq_pool.shutdown && s_shq_pool.q_count == 0) {
780 avx_mutex_unlock(&s_shq_pool.lock);
781 break;
782 }
783 avx_shq_task_t task = s_shq_pool.queue[s_shq_pool.q_head];
784 s_shq_pool.q_head = (s_shq_pool.q_head + 1) % SHQ_TASK_QUEUE_CAP;
785 s_shq_pool.q_count--;
786 avx_mutex_unlock(&s_shq_pool.lock);
787  
788 if (task.fn) {
789 task.fn(task.ctx, task.slice_idx);
790 }
791  
792 avx_mutex_lock(&task.job->lock);
793 task.job->remaining--;
794 if (task.job->remaining == 0) {
795 avx_cond_signal(&task.job->cond);
796 }
797 avx_mutex_unlock(&task.job->lock);
798 }
799 return NULL;
800 }
801  
802 static void shq_pool_reset_if_forked_locked(void)
803 {
804 #if defined(AVX_OS_POSIX)
805 if (s_shq_pool.started && s_shq_pool.owner_pid != getpid()) {
806 s_shq_pool.started = false;
807 s_shq_pool.shutdown = false;
808 s_shq_pool.num_workers = 0;
809 s_shq_pool.q_head = 0;
810 s_shq_pool.q_count = 0;
811 memset(s_shq_pool.scratch, 0, sizeof(s_shq_pool.scratch));
812 avx_cond_init(&s_shq_pool.work_cond);
813 }
814 #endif
815 }
816  
817 static bool shq_pool_ensure_started_locked(void)
818 {
819 shq_pool_reset_if_forked_locked();
820 if (s_shq_pool.started) return (s_shq_pool.num_workers > 0);
821 if (s_shq_pool.shutdown) return false;
822  
823 int want = speedhq_cpu_count() - 1;
824 if (want > SHQ_POOL_MAX_WORKERS) want = SHQ_POOL_MAX_WORKERS;
825 if (want < 0) want = 0;
826  
827 s_shq_pool.started = true;
828 #if defined(AVX_OS_POSIX)
829 s_shq_pool.owner_pid = getpid();
830 #endif
831 s_shq_pool.num_workers = 0;
832  
833 for (int i = 0; i < want; ++i) {
834 if (avx_thread_create(&s_shq_pool.workers[s_shq_pool.num_workers], shq_worker_main, NULL) == 0) {
835 s_shq_pool.num_workers++;
836 } else {
837 break;
838 }
839 }
840 return (s_shq_pool.num_workers > 0);
841 }
842  
843 static void shq_dispatch_slices(shq_slice_fn_t fn, void *ctx, int num_slices)
844 {
845 if (num_slices <= 1) {
846 if (num_slices == 1) fn(ctx, 0);
847 return;
848 }
849  
850 avx_mutex_lock(&s_shq_pool.lock);
851 bool pool_ok = shq_pool_ensure_started_locked();
852 if (!pool_ok || s_shq_pool.shutdown) {
853 avx_mutex_unlock(&s_shq_pool.lock);
854 for (int s = 0; s < num_slices; ++s) {
855 fn(ctx, s);
856 }
857 return;
858 }
859  
860 avx_shq_job_t job;
861 avx_mutex_init(&job.lock);
862 avx_cond_init(&job.cond);
863 job.remaining = 0;
864  
865 bool local_run[4] = { true, false, false, false };
866 for (int s = 1; s < num_slices && s < 4; ++s) {
867 if (s_shq_pool.q_count < SHQ_TASK_QUEUE_CAP) {
868 int tail = (s_shq_pool.q_head + s_shq_pool.q_count) % SHQ_TASK_QUEUE_CAP;
869 s_shq_pool.queue[tail].fn = fn;
870 s_shq_pool.queue[tail].ctx = ctx;
871 s_shq_pool.queue[tail].slice_idx = s;
872 s_shq_pool.queue[tail].job = &job;
873 s_shq_pool.q_count++;
874 job.remaining++;
875 } else {
876 local_run[s] = true;
877 }
878 }
879  
880 if (job.remaining > 0) {
881 avx_cond_broadcast(&s_shq_pool.work_cond);
882 }
883 avx_mutex_unlock(&s_shq_pool.lock);
884  
885 /* Execute slice 0 (and any overflow slices) directly on caller thread */
886 for (int s = 0; s < num_slices && s < 4; ++s) {
887 if (local_run[s]) {
888 fn(ctx, s);
889 }
890 }
891  
892 /* Help complete any still-queued tasks belonging to this job to guarantee zero starvation */
893 while (1) {
894 avx_shq_task_t stolen;
895 bool found_own = false;
896  
897 avx_mutex_lock(&s_shq_pool.lock);
898 for (int k = 0; k < s_shq_pool.q_count; ++k) {
899 int idx = (s_shq_pool.q_head + k) % SHQ_TASK_QUEUE_CAP;
900 if (s_shq_pool.queue[idx].job == &job) {
901 stolen = s_shq_pool.queue[idx];
902 for (int m = k; m + 1 < s_shq_pool.q_count; ++m) {
903 int dst = (s_shq_pool.q_head + m) % SHQ_TASK_QUEUE_CAP;
904 int src = (s_shq_pool.q_head + m + 1) % SHQ_TASK_QUEUE_CAP;
905 s_shq_pool.queue[dst] = s_shq_pool.queue[src];
906 }
907 s_shq_pool.q_count--;
908 found_own = true;
909 break;
910 }
911 }
912 avx_mutex_unlock(&s_shq_pool.lock);
913  
914 if (found_own) {
915 if (stolen.fn) stolen.fn(stolen.ctx, stolen.slice_idx);
916 avx_mutex_lock(&job.lock);
917 job.remaining--;
918 avx_mutex_unlock(&job.lock);
919 continue;
920 }
921  
922 avx_mutex_lock(&job.lock);
923 while (job.remaining > 0) {
924 avx_cond_wait(&job.cond, &job.lock);
925 }
926 avx_mutex_unlock(&job.lock);
927 break;
928 }
929  
930 avx_cond_destroy(&job.cond);
931 avx_mutex_destroy(&job.lock);
932 }
933  
934 static int shq_scratch_checkout(uint8_t *out_bufs[3], size_t out_caps[3], size_t min_cap, size_t max_cap)
935 {
936 int slot_idx = -1;
937 avx_mutex_lock(&s_shq_pool.lock);
938 shq_pool_reset_if_forked_locked();
939 for (int i = 0; i < SHQ_SCRATCH_SLOTS; ++i) {
940 if (!s_shq_pool.scratch[i].in_use) {
941 s_shq_pool.scratch[i].in_use = true;
942 slot_idx = i;
943 for (int s = 0; s < 3; ++s) {
944 out_bufs[s] = s_shq_pool.scratch[i].buf[s];
945 out_caps[s] = s_shq_pool.scratch[i].cap[s];
946 }
947 break;
948 }
949 }
950 avx_mutex_unlock(&s_shq_pool.lock);
951  
952 for (int s = 0; s < 3; ++s) {
953 if (out_caps[s] < min_cap) {
954 size_t req = (min_cap <= max_cap) ? min_cap : max_cap;
955 uint8_t *nb = (uint8_t *)realloc(out_bufs[s], req);
956 if (!nb) {
957 for (int k = 0; k < 3; ++k) {
958 if (slot_idx >= 0) {
959 /* Persist already-grown buffers even if a later allocation fails */
960 if (out_bufs[k]) {
961 avx_mutex_lock(&s_shq_pool.lock);
962 s_shq_pool.scratch[slot_idx].buf[k] = out_bufs[k];
963 s_shq_pool.scratch[slot_idx].cap[k] = out_caps[k];
964 avx_mutex_unlock(&s_shq_pool.lock);
965 }
966 } else {
967 free(out_bufs[k]);
968 out_bufs[k] = NULL;
969 }
970 }
971 if (slot_idx >= 0) {
972 avx_mutex_lock(&s_shq_pool.lock);
973 s_shq_pool.scratch[slot_idx].in_use = false;
974 avx_mutex_unlock(&s_shq_pool.lock);
975 }
976 return -2;
977 }
978 out_bufs[s] = nb;
979 out_caps[s] = req;
980 }
981 }
982 return slot_idx; /* >= 0 if pooled slot checked out, -1 if transient malloc buffers */
983 }
984  
985 static void shq_scratch_checkin(int slot_idx, uint8_t *bufs[3], const size_t caps[3])
986 {
987 if (slot_idx >= 0) {
988 avx_mutex_lock(&s_shq_pool.lock);
989 for (int s = 0; s < 3; ++s) {
990 s_shq_pool.scratch[slot_idx].buf[s] = bufs[s];
991 s_shq_pool.scratch[slot_idx].cap[s] = caps[s];
992 }
993 s_shq_pool.scratch[slot_idx].in_use = false;
994 avx_mutex_unlock(&s_shq_pool.lock);
995 } else {
996 for (int s = 0; s < 3; ++s) {
997 free(bufs[s]);
998 }
999 }
1000 }
1001  
1002 static void shq_pool_shutdown_internal(void)
1003 {
1004 avx_thread_t join_threads[SHQ_POOL_MAX_WORKERS];
1005 int join_count = 0;
1006  
1007 avx_mutex_lock(&s_shq_pool.lock);
1008 shq_pool_reset_if_forked_locked();
1009 if (s_shq_pool.started) {
1010 s_shq_pool.shutdown = true;
1011 avx_cond_broadcast(&s_shq_pool.work_cond);
1012 join_count = s_shq_pool.num_workers;
1013 for (int i = 0; i < join_count; ++i) {
1014 join_threads[i] = s_shq_pool.workers[i];
1015 }
1016 s_shq_pool.num_workers = 0;
1017 s_shq_pool.started = false;
1018 }
1019 avx_mutex_unlock(&s_shq_pool.lock);
1020  
1021 for (int i = 0; i < join_count; ++i) {
1022 avx_thread_join(join_threads[i]);
1023 }
1024  
1025 avx_mutex_lock(&s_shq_pool.lock);
1026 for (int i = 0; i < SHQ_SCRATCH_SLOTS; ++i) {
1027 if (!s_shq_pool.scratch[i].in_use) {
1028 for (int s = 0; s < 3; ++s) {
1029 free(s_shq_pool.scratch[i].buf[s]);
1030 s_shq_pool.scratch[i].buf[s] = NULL;
1031 s_shq_pool.scratch[i].cap[s] = 0;
1032 }
1033 }
1034 }
1035 s_shq_pool.shutdown = false;
1036 avx_mutex_unlock(&s_shq_pool.lock);
1037 }
1038  
1039 #if defined(__GNUC__) || defined(__clang__)
1040 __attribute__((destructor))
1041 static void speedhq_module_destructor(void)
1042 {
1043 shq_pool_shutdown_internal();
1044 }
1045 #endif
1046  
1047 typedef struct {
1048 int width;
1049 int height;
1050 int mb_w;
1051 int mb_h;
1052 int num_slices;
1053 bool has_alpha;
1054 bool is_dct_alpha;
1055 const uint8_t *uyvy_data;
1056 size_t uyvy_stride;
1057 const uint8_t *alpha_data;
1058 size_t alpha_stride;
1059 const avx_shq_quant_ctx_t *qctx;
1060 avx_bitwriter_t bws[4];
1061 } shq_enc_frame_ctx_t;
1062  
1063 static inline void shq_extract_uyvy_row_pair(const uint8_t *row_top, const uint8_t *row_bot, int r,
1064 int16_t blk_y0[64], int16_t blk_y1[64],
1065 int16_t blk_y2[64], int16_t blk_y3[64],
1066 int16_t blk_cb0[64], int16_t blk_cb1[64],
1067 int16_t blk_cr0[64], int16_t blk_cr1[64])
1068 {
1069 int base = r * 8;
1070 for (int c = 0; c < 8; ++c) {
1071 blk_y0[base + c] = row_top[c * 2 + 1];
1072 blk_y1[base + c] = row_top[(c + 8) * 2 + 1];
1073 blk_y2[base + c] = row_bot[c * 2 + 1];
1074 blk_y3[base + c] = row_bot[(c + 8) * 2 + 1];
1075  
1076 blk_cb0[base + c] = row_top[c * 4 + 0];
1077 blk_cr0[base + c] = row_top[c * 4 + 2];
1078 blk_cb1[base + c] = row_bot[c * 4 + 0];
1079 blk_cr1[base + c] = row_bot[c * 4 + 2];
1080 }
1081 }
1082  
1083 static void shq_encode_slice_fn(void *vctx, int slice)
1084 {
1085 shq_enc_frame_ctx_t *fctx = (shq_enc_frame_ctx_t *)vctx;
1086 avx_bitwriter_t *bw = &fctx->bws[slice];
1087 int mb_w = fctx->mb_w;
1088 int mb_h = fctx->mb_h;
1089 int num_slices = fctx->num_slices;
1090 int width = fctx->width;
1091 int height = fctx->height;
1092 size_t uyvy_stride = fctx->uyvy_stride ? fctx->uyvy_stride : (size_t)width * 2;
1093 const uint8_t *uyvy_data = fctx->uyvy_data;
1094 const uint8_t *alpha_data = fctx->alpha_data;
1095 size_t alpha_stride = fctx->alpha_stride ? fctx->alpha_stride : (size_t)width;
1096 const avx_shq_quant_ctx_t *qctx = fctx->qctx;
1097  
1098 size_t slice_start_pos = bw->size;
1099 bitwriter_reserve_3(bw);
1100  
1101 for (int mb_y = slice; mb_y < mb_h; mb_y += num_slices) {
1102 int last_dc[4] = { 1024, 1024, 1024, 1024 };
1103 uint8_t last_alpha[16];
1104 if (fctx->has_alpha && !fctx->is_dct_alpha) {
1105 memset(last_alpha, 255, sizeof(last_alpha));
1106 }
1107 int y_base = mb_y * 16;
1108 bool interior_y = (y_base + 15 < height);
1109  
1110 for (int mb_x = 0; mb_x < mb_w; ++mb_x) {
1111 int x_base = mb_x * 16;
1112  
1113 int16_t blk_y0[64], blk_y1[64], blk_y2[64], blk_y3[64];
1114 int16_t blk_cb0[64], blk_cb1[64], blk_cr0[64], blk_cr1[64];
1115  
1116 if (interior_y) {
1117 const uint8_t *row_top = uyvy_data + (size_t)y_base * uyvy_stride + (size_t)x_base * 2;
1118 const uint8_t *row_bot = row_top + 8 * uyvy_stride;
1119 for (int r = 0; r < 8; ++r) {
1120 shq_extract_uyvy_row_pair(row_top, row_bot, r,
1121 blk_y0, blk_y1, blk_y2, blk_y3,
1122 blk_cb0, blk_cb1, blk_cr0, blk_cr1);
1123 row_top += uyvy_stride;
1124 row_bot += uyvy_stride;
1125 }
1126 } else {
1127 for (int r = 0; r < 8; ++r) {
1128 int y_top = y_base + r;
1129 if (y_top >= height) y_top = height - 1;
1130 int y_bot = y_base + r + 8;
1131 if (y_bot >= height) y_bot = height - 1;
1132 const uint8_t *row_top = uyvy_data + (size_t)y_top * uyvy_stride + (size_t)x_base * 2;
1133 const uint8_t *row_bot = uyvy_data + (size_t)y_bot * uyvy_stride + (size_t)x_base * 2;
1134 shq_extract_uyvy_row_pair(row_top, row_bot, r,
1135 blk_y0, blk_y1, blk_y2, blk_y3,
1136 blk_cb0, blk_cb1, blk_cr0, blk_cr1);
1137 }
1138 }
1139  
1140 /* FDCT & Encode */
1141 int16_t dct[64];
1142 fdct_8x8(blk_y0, dct); encode_block_c99(bw, dct, &last_dc[0], 0, qctx);
1143 fdct_8x8(blk_y1, dct); encode_block_c99(bw, dct, &last_dc[0], 0, qctx);
1144 fdct_8x8(blk_y2, dct); encode_block_c99(bw, dct, &last_dc[0], 0, qctx);
1145 fdct_8x8(blk_y3, dct); encode_block_c99(bw, dct, &last_dc[0], 0, qctx);
1146  
1147 fdct_8x8(blk_cb0, dct); encode_block_c99(bw, dct, &last_dc[1], 1, qctx);
1148 fdct_8x8(blk_cr0, dct); encode_block_c99(bw, dct, &last_dc[2], 2, qctx);
1149 fdct_8x8(blk_cb1, dct); encode_block_c99(bw, dct, &last_dc[1], 1, qctx);
1150 fdct_8x8(blk_cr1, dct); encode_block_c99(bw, dct, &last_dc[2], 2, qctx);
1151  
1152 /* Alpha encoding */
1153 if (fctx->has_alpha) {
1154 if (fctx->is_dct_alpha) {
1155 int16_t blk_a0[64], blk_a1[64], blk_a2[64], blk_a3[64];
1156 for (int r = 0; r < 8; ++r) {
1157 int y_top = y_base + r;
1158 if (y_top >= height) y_top = height - 1;
1159 int y_bot = y_base + r + 8;
1160 if (y_bot >= height) y_bot = height - 1;
1161 const uint8_t *a_top = alpha_data ? (alpha_data + (size_t)y_top * alpha_stride + x_base) : NULL;
1162 const uint8_t *a_bot = alpha_data ? (alpha_data + (size_t)y_bot * alpha_stride + x_base) : NULL;
1163 for (int c = 0; c < 8; ++c) {
1164 blk_a0[r * 8 + c] = a_top ? a_top[c] : 255;
1165 blk_a1[r * 8 + c] = a_top ? a_top[c + 8] : 255;
1166 blk_a2[r * 8 + c] = a_bot ? a_bot[c] : 255;
1167 blk_a3[r * 8 + c] = a_bot ? a_bot[c + 8] : 255;
1168 }
1169 }
1170 fdct_8x8(blk_a0, dct); encode_block_c99(bw, dct, &last_dc[3], 0, qctx);
1171 fdct_8x8(blk_a1, dct); encode_block_c99(bw, dct, &last_dc[3], 0, qctx);
1172 fdct_8x8(blk_a2, dct); encode_block_c99(bw, dct, &last_dc[3], 0, qctx);
1173 fdct_8x8(blk_a3, dct); encode_block_c99(bw, dct, &last_dc[3], 0, qctx);
1174 } else {
1175 int rows_top = (height > y_base) ? (height - y_base) : 0;
1176 if (rows_top > 8) rows_top = 8;
1177 const uint8_t *a_top = (alpha_data && rows_top > 0) ?
1178 (alpha_data + (size_t)y_base * alpha_stride + x_base) : NULL;
1179 encode_alpha_block(bw, a_top, (int)alpha_stride, rows_top, last_alpha);
1180  
1181 int rows_bot = (height > y_base + 8) ? (height - (y_base + 8)) : 0;
1182 if (rows_bot > 8) rows_bot = 8;
1183 const uint8_t *a_bot = (alpha_data && rows_bot > 0) ?
1184 (alpha_data + (size_t)(y_base + 8) * alpha_stride + x_base) : NULL;
1185 encode_alpha_block(bw, a_bot, (int)alpha_stride, rows_bot, last_alpha);
1186 }
1187 }
1188 }
1189 }
1190  
1191 bitwriter_flush(bw);
1192  
1193 if (!bw->overflow && slice_start_pos + 3 <= bw->size) {
1194 uint32_t slice_len = (uint32_t)(bw->size - slice_start_pos);
1195 bw->buf[slice_start_pos + 0] = (uint8_t)(slice_len & 0xFF);
1196 bw->buf[slice_start_pos + 1] = (uint8_t)((slice_len >> 8) & 0xFF);
1197 bw->buf[slice_start_pos + 2] = (uint8_t)((slice_len >> 16) & 0xFF);
1198 }
1199 }
1200  
1201 /*
1202 * Core SpeedHQ encoder with support for custom strides and alpha channels.
1203 * Uses 4-slice vertical multithreading for frames >= 128k pixels; writes
1204 * slice 0 in-place and slices 1..3 via thread-pooled scratch buffers.
1205 */
1206 int avx_speedhq_encode_strided_ex(uint32_t fourcc, int width, int height,
1207 const uint8_t *uyvy_data, size_t uyvy_stride,
1208 const uint8_t *alpha_data, size_t alpha_stride,
1209 uint8_t quality,
1210 uint8_t *out_buf, size_t out_max,
1211 size_t *out_size)
1212 {
1213 avx_speedhq_init();
1214  
1215 if (!uyvy_data || !out_buf || !out_size || width <= 0 || height <= 0 ||
1216 width > AVX_MAX_VIDEO_WIDTH || height > AVX_MAX_VIDEO_HEIGHT || width % 16 != 0) {
1217 return -1;
1218 }
1219  
1220 size_t min_uyvy_stride = (size_t)width * 2;
1221 size_t min_alpha_stride = (size_t)width;
1222 if (uyvy_stride == 0) uyvy_stride = min_uyvy_stride;
1223 if (alpha_stride == 0) alpha_stride = min_alpha_stride;
1224 if (uyvy_stride < min_uyvy_stride) return -1;
1225 if (alpha_data && alpha_stride < min_alpha_stride) return -1;
1226  
1227 uint32_t tag = fourcc ? fourcc : (alpha_data ? 0x33514853 : 0x32514853);
1228 if (!avx_is_speedhq_fourcc(tag)) return -1;
1229 bool has_alpha = (tag == 0x31514853 || tag == 0x33514853 || tag == 0x35514853 ||
1230 tag == 0x37514853 || tag == 0x39514853 || alpha_data != NULL);
1231  
1232 /* SpeedHQ Header: Quality byte + 24-bit little endian second_field_offset */
1233 uint8_t effective_q = quality ? quality : s_speedhq_quality;
1234 if (effective_q < 20) effective_q = 20;
1235 if (effective_q > 98) effective_q = 98;
1236 int qscale = 100 - effective_q;
1237 if (qscale < 1) qscale = 1;
1238  
1239 if (out_max < 64) return -2;
1240  
1241 out_buf[0] = effective_q;
1242 out_buf[1] = 4; /* second_field_offset = 4 (progressive / single-field) */
1243 out_buf[2] = 0;
1244 out_buf[3] = 0;
1245  
1246 avx_shq_quant_ctx_t qctx;
1247 speedhq_prepare_quant_ctx(&qctx, qscale);
1248  
1249 int mb_w = width / 16;
1250 int mb_h = (height + 15) / 16;
1251 int num_slices = (mb_h >= 4) ? 4 : 1;
1252  
1253 shq_enc_frame_ctx_t fctx;
1254 fctx.width = width;
1255 fctx.height = height;
1256 fctx.mb_w = mb_w;
1257 fctx.mb_h = mb_h;
1258 fctx.num_slices = num_slices;
1259 fctx.has_alpha = has_alpha;
1260 fctx.is_dct_alpha = (tag == 0x37514853 || tag == 0x39514853);
1261 fctx.uyvy_data = uyvy_data;
1262 fctx.uyvy_stride = uyvy_stride;
1263 fctx.alpha_data = alpha_data;
1264 fctx.alpha_stride = alpha_stride;
1265 fctx.qctx = &qctx;
1266  
1267 bool use_mt = (num_slices == 4) && ((size_t)width * (size_t)height >= 131072U);
1268 if (!use_mt) {
1269 avx_bitwriter_t bw;
1270 bitwriter_init(&bw, out_buf + 4, out_max - 4);
1271 fctx.bws[0] = bw;
1272 for (int slice = 0; slice < num_slices; ++slice) {
1273 shq_encode_slice_fn(&fctx, slice);
1274 if (slice + 1 < num_slices) {
1275 fctx.bws[slice + 1] = fctx.bws[slice];
1276 }
1277 }
1278 if (fctx.bws[num_slices - 1].overflow) return -2;
1279 *out_size = 4 + fctx.bws[num_slices - 1].size;
1280 return 0;
1281 }
1282  
1283 size_t max_slice_cap = out_max - 4;
1284 size_t slice_mb_rows = (size_t)((mb_h + 3) / 4);
1285 size_t init_slice_cap = (size_t)mb_w * slice_mb_rows * (has_alpha ? 1024U : 600U) + 8192U;
1286 if (init_slice_cap > max_slice_cap) init_slice_cap = max_slice_cap;
1287 if (init_slice_cap < 4096U && max_slice_cap >= 4096U) init_slice_cap = 4096U;
1288  
1289 uint8_t *scratch_bufs[3] = { NULL, NULL, NULL };
1290 size_t scratch_caps[3] = { 0, 0, 0 };
1291 int slot_idx = shq_scratch_checkout(scratch_bufs, scratch_caps, init_slice_cap, max_slice_cap);
1292 if (slot_idx == -2) {
1293 return -2;
1294 }
1295  
1296 /* Slice 0 writes directly into out_buf + 4; slices 1..3 use pooled scratch buffers */
1297 bitwriter_init(&fctx.bws[0], out_buf + 4, max_slice_cap);
1298 for (int s = 1; s < 4; ++s) {
1299 bitwriter_init_ex(&fctx.bws[s], scratch_bufs[s - 1], scratch_caps[s - 1], true, max_slice_cap);
1300 }
1301  
1302 shq_dispatch_slices(shq_encode_slice_fn, &fctx, 4);
1303  
1304 for (int s = 1; s < 4; ++s) {
1305 scratch_bufs[s - 1] = fctx.bws[s].buf;
1306 scratch_caps[s - 1] = fctx.bws[s].capacity;
1307 }
1308  
1309 bool overflowed = fctx.bws[0].overflow;
1310 size_t write_pos = 4 + fctx.bws[0].size;
1311 for (int s = 1; s < 4 && !overflowed; ++s) {
1312 if (fctx.bws[s].overflow || write_pos + fctx.bws[s].size > out_max) {
1313 overflowed = true;
1314 break;
1315 }
1316 memcpy(out_buf + write_pos, fctx.bws[s].buf, fctx.bws[s].size);
1317 write_pos += fctx.bws[s].size;
1318 }
1319  
1320 shq_scratch_checkin(slot_idx, scratch_bufs, scratch_caps);
1321  
1322 if (overflowed) return -2;
1323 *out_size = write_pos;
1324 return 0;
1325 }
1326  
1327 int avx_speedhq_encode_c99_ex(uint32_t fourcc, int width, int height,
1328 const uint8_t *uyvy_data, const uint8_t *alpha_data,
1329 uint8_t *out_buf, size_t out_max,
1330 size_t *out_size)
1331 {
1332 return avx_speedhq_encode_strided_ex(fourcc, width, height,
1333 uyvy_data, 0,
1334 alpha_data, 0,
1335 0,
1336 out_buf, out_max, out_size);
1337 }
1338  
1339 int avx_speedhq_encode_c99(int width, int height,
1340 const uint8_t *uyvy_data,
1341 uint8_t *out_buf, size_t out_max,
1342 size_t *out_size)
1343 {
1344 return avx_speedhq_encode_c99_ex(0x32514853, width, height, uyvy_data, NULL, out_buf, out_max, out_size);
1345 }
1346  
1347 /*
1348 * C99 Standalone Decoder: SpeedHQ -> UYVY + Alpha
1349 */
1350 typedef struct {
1351 int width;
1352 int height;
1353 int mb_w;
1354 int mb_h;
1355 int num_slices;
1356 bool has_alpha;
1357 bool is_dct_alpha;
1358 uint8_t *uyvy_out;
1359 uint8_t *alpha_out;
1360 const avx_shq_quant_ctx_t *qctx;
1361 const uint8_t *slice_ptrs[4];
1362 size_t slice_bytes[4];
1363 } shq_dec_frame_ctx_t;
1364  
1365 static inline void shq_store_uyvy_row(uint8_t *row, const uint8_t *cb, const uint8_t *cr,
1366 const uint8_t *y_left, const uint8_t *y_right, int r)
1367 {
1368 int base = r * 8;
1369 for (int c = 0; c < 4; ++c) {
1370 row[c * 4 + 0] = cb[base + c];
1371 row[c * 4 + 1] = y_left[base + c * 2 + 0];
1372 row[c * 4 + 2] = cr[base + c];
1373 row[c * 4 + 3] = y_left[base + c * 2 + 1];
1374 }
1375 for (int c = 0; c < 4; ++c) {
1376 row[(c + 4) * 4 + 0] = cb[base + c + 4];
1377 row[(c + 4) * 4 + 1] = y_right[base + c * 2 + 0];
1378 row[(c + 4) * 4 + 2] = cr[base + c + 4];
1379 row[(c + 4) * 4 + 3] = y_right[base + c * 2 + 1];
1380 }
1381 }
1382  
1383 static void shq_decode_slice_fn(void *vctx, int slice)
1384 {
1385 shq_dec_frame_ctx_t *fctx = (shq_dec_frame_ctx_t *)vctx;
1386 if (!fctx->slice_ptrs[slice]) return;
1387  
1388 avx_bitreader_t br;
1389 bitreader_init(&br, fctx->slice_ptrs[slice], fctx->slice_bytes[slice]);
1390  
1391 int mb_w = fctx->mb_w;
1392 int mb_h = fctx->mb_h;
1393 int num_slices = fctx->num_slices;
1394 int width = fctx->width;
1395 int height = fctx->height;
1396 size_t uyvy_stride = (size_t)width * 2;
1397 uint8_t *uyvy_out = fctx->uyvy_out;
1398 uint8_t *alpha_out = fctx->alpha_out;
1399 const avx_shq_quant_ctx_t *qctx = fctx->qctx;
1400  
1401 for (int mb_y = slice; mb_y < mb_h; mb_y += num_slices) {
1402 int last_dc[4] = { 1024, 1024, 1024, 1024 };
1403 uint8_t last_alpha[16];
1404 if (fctx->has_alpha && !fctx->is_dct_alpha) {
1405 memset(last_alpha, 255, sizeof(last_alpha));
1406 }
1407 int y_base = mb_y * 16;
1408 bool interior_y = (y_base + 15 < height);
1409  
1410 for (int mb_x = 0; mb_x < mb_w; ++mb_x) {
1411 int x_base = mb_x * 16;
1412  
1413 /* Decode 8 blocks: 4 Y, 2 Cb, 2 Cr */
1414 uint8_t dec_y0[64], dec_y1[64], dec_y2[64], dec_y3[64];
1415 uint8_t dec_cb0[64], dec_cb1[64], dec_cr0[64], dec_cr1[64];
1416  
1417 decode_single_block_c99(&br, &last_dc[0], 0, qctx, dec_y0);
1418 decode_single_block_c99(&br, &last_dc[0], 0, qctx, dec_y1);
1419 decode_single_block_c99(&br, &last_dc[0], 0, qctx, dec_y2);
1420 decode_single_block_c99(&br, &last_dc[0], 0, qctx, dec_y3);
1421  
1422 decode_single_block_c99(&br, &last_dc[1], 1, qctx, dec_cb0);
1423 decode_single_block_c99(&br, &last_dc[2], 2, qctx, dec_cr0);
1424 decode_single_block_c99(&br, &last_dc[1], 1, qctx, dec_cb1);
1425 decode_single_block_c99(&br, &last_dc[2], 2, qctx, dec_cr1);
1426  
1427 /* Reconstruct into packed UYVY */
1428 if (interior_y) {
1429 uint8_t *row_top = uyvy_out + (size_t)y_base * uyvy_stride + (size_t)x_base * 2;
1430 uint8_t *row_bot = row_top + 8 * uyvy_stride;
1431 for (int r = 0; r < 8; ++r) {
1432 shq_store_uyvy_row(row_top, dec_cb0, dec_cr0, dec_y0, dec_y1, r);
1433 shq_store_uyvy_row(row_bot, dec_cb1, dec_cr1, dec_y2, dec_y3, r);
1434 row_top += uyvy_stride;
1435 row_bot += uyvy_stride;
1436 }
1437 } else {
1438 for (int r = 0; r < 8; ++r) {
1439 int y_top = y_base + r;
1440 int y_bot = y_base + r + 8;
1441 if (y_top < height) {
1442 uint8_t *row_top = uyvy_out + (size_t)y_top * uyvy_stride + (size_t)x_base * 2;
1443 shq_store_uyvy_row(row_top, dec_cb0, dec_cr0, dec_y0, dec_y1, r);
1444 }
1445 if (y_bot < height) {
1446 uint8_t *row_bot = uyvy_out + (size_t)y_bot * uyvy_stride + (size_t)x_base * 2;
1447 shq_store_uyvy_row(row_bot, dec_cb1, dec_cr1, dec_y2, dec_y3, r);
1448 }
1449 }
1450 }
1451  
1452 /* Decode Alpha if present */
1453 if (fctx->has_alpha) {
1454 if (fctx->is_dct_alpha) {
1455 uint8_t dec_a0[64], dec_a1[64], dec_a2[64], dec_a3[64];
1456 decode_single_block_c99(&br, &last_dc[3], 0, qctx, dec_a0);
1457 decode_single_block_c99(&br, &last_dc[3], 0, qctx, dec_a1);
1458 decode_single_block_c99(&br, &last_dc[3], 0, qctx, dec_a2);
1459 decode_single_block_c99(&br, &last_dc[3], 0, qctx, dec_a3);
1460  
1461 if (alpha_out) {
1462 for (int r = 0; r < 8; ++r) {
1463 int y_top = y_base + r;
1464 int y_bot = y_base + r + 8;
1465 if (y_top < height) {
1466 uint8_t *row_top = alpha_out + (size_t)y_top * width + x_base;
1467 memcpy(row_top, dec_a0 + r * 8, 8);
1468 memcpy(row_top + 8, dec_a1 + r * 8, 8);
1469 }
1470 if (y_bot < height) {
1471 uint8_t *row_bot = alpha_out + (size_t)y_bot * width + x_base;
1472 memcpy(row_bot, dec_a2 + r * 8, 8);
1473 memcpy(row_bot + 8, dec_a3 + r * 8, 8);
1474 }
1475 }
1476 }
1477 } else {
1478 int rows_top = (height > y_base) ? (height - y_base) : 0;
1479 if (rows_top > 8) rows_top = 8;
1480 uint8_t *a_top = (alpha_out && rows_top > 0) ?
1481 (alpha_out + (size_t)y_base * width + x_base) : NULL;
1482 decode_alpha_block(&br, a_top, width, rows_top, last_alpha);
1483  
1484 int rows_bot = (height > y_base + 8) ? (height - (y_base + 8)) : 0;
1485 if (rows_bot > 8) rows_bot = 8;
1486 uint8_t *a_bot = (alpha_out && rows_bot > 0) ?
1487 (alpha_out + (size_t)(y_base + 8) * width + x_base) : NULL;
1488 decode_alpha_block(&br, a_bot, width, rows_bot, last_alpha);
1489 }
1490 }
1491 }
1492 }
1493 }
1494  
1495 /*
1496 * Decodes SpeedHQ intra-frame bitstream into packed UYVY and optional alpha plane.
1497 * Reconstructs DC predictors per slice and uses O(1) LUT for high-frequency AC runs.
1498 */
1499 int avx_speedhq_decode_c99_ex(uint32_t fourcc, int width, int height,
1500 const uint8_t *shq_data, size_t shq_size,
1501 uint8_t *uyvy_out, uint8_t *alpha_out,
1502 bool *p_has_alpha)
1503 {
1504 avx_speedhq_init();
1505  
1506 if (!shq_data || !uyvy_out || shq_size < 16 || width <= 0 || height <= 0 ||
1507 width > AVX_MAX_VIDEO_WIDTH || height > AVX_MAX_VIDEO_HEIGHT || width % 16 != 0) {
1508 return -1;
1509 }
1510  
1511 uint32_t tag = fourcc ? fourcc : 0x32514853;
1512 if (!avx_is_speedhq_fourcc(tag)) return -1;
1513 bool has_alpha = (tag == 0x31514853 || tag == 0x33514853 || tag == 0x35514853 ||
1514 tag == 0x37514853 || tag == 0x39514853);
1515 if (p_has_alpha) *p_has_alpha = has_alpha;
1516  
1517 uint8_t quality = shq_data[0];
1518 int qscale = 100 - quality;
1519 if (qscale < 1) qscale = 1;
1520  
1521 uint32_t second_field_offset = (uint32_t)shq_data[1] |
1522 ((uint32_t)shq_data[2] << 8) |
1523 ((uint32_t)shq_data[3] << 16);
1524 if (second_field_offset >= shq_size && second_field_offset != 4) return -1;
1525  
1526 avx_shq_quant_ctx_t qctx;
1527 speedhq_prepare_quant_ctx(&qctx, qscale);
1528  
1529 size_t pos = 4;
1530 int mb_w = width / 16;
1531 int mb_h = (height + 15) / 16;
1532 int num_slices = (mb_h >= 4) ? 4 : 1;
1533  
1534 shq_dec_frame_ctx_t fctx;
1535 memset(&fctx, 0, sizeof(fctx));
1536 fctx.width = width;
1537 fctx.height = height;
1538 fctx.mb_w = mb_w;
1539 fctx.mb_h = mb_h;
1540 fctx.num_slices = num_slices;
1541 fctx.has_alpha = has_alpha;
1542 fctx.is_dct_alpha = (tag == 0x37514853 || tag == 0x39514853);
1543 fctx.uyvy_out = uyvy_out;
1544 fctx.alpha_out = alpha_out;
1545 fctx.qctx = &qctx;
1546  
1547 int valid_slices = 0;
1548 size_t total_slice_payload = 0;
1549 for (int slice = 0; slice < num_slices && pos < shq_size; ++slice) {
1550 if (pos + 3 > shq_size) break;
1551 uint32_t slice_len = (uint32_t)shq_data[pos] |
1552 ((uint32_t)shq_data[pos + 1] << 8) |
1553 ((uint32_t)shq_data[pos + 2] << 16);
1554 if (slice_len < 3 || pos + slice_len > shq_size) break;
1555 fctx.slice_ptrs[slice] = shq_data + pos + 3;
1556 fctx.slice_bytes[slice] = slice_len - 3;
1557 total_slice_payload += fctx.slice_bytes[slice];
1558 valid_slices++;
1559 pos += slice_len;
1560 }
1561  
1562 if (num_slices == 4 && valid_slices == 4 &&
1563 (size_t)width * (size_t)height >= 131072U && total_slice_payload >= 1024U) {
1564 shq_dispatch_slices(shq_decode_slice_fn, &fctx, 4);
1565 } else {
1566 for (int slice = 0; slice < valid_slices; ++slice) {
1567 shq_decode_slice_fn(&fctx, slice);
1568 }
1569 }
1570  
1571 if (!has_alpha && alpha_out) {
1572 memset(alpha_out, 255, (size_t)width * height);
1573 }
1574  
1575 return 0;
1576 }
1577  
1578 int avx_speedhq_decode_c99(int width, int height,
1579 const uint8_t *shq_data, size_t shq_size,
1580 uint8_t *uyvy_out)
1581 {
1582 return avx_speedhq_decode_c99_ex(0x32514853, width, height, shq_data, shq_size, uyvy_out, NULL, NULL);
1583 }
1584  
1585 /*
1586 * Global Initialization and Dispatchers
1587 */
1588 static bool s_initialized = false;
1589 static avx_mutex_t s_init_lock = AVX_MUTEX_INITIALIZER;
1590  
1591 OPENAVX_API bool avx_speedhq_init(void)
1592 {
1593 avx_mutex_lock(&s_init_lock);
1594 if (!s_initialized) {
1595 speedhq_init_vlc_tables();
1596 s_initialized = true;
1597 }
1598 avx_mutex_unlock(&s_init_lock);
1599 return true;
1600 }
1601  
1602 bool avx_speedhq_has_acceleration(void)
1603 {
1604 return avx_simd_get_caps() != AVX_SIMD_NONE;
1605 }
1606  
1607 int avx_speedhq_encode_ex(uint32_t fourcc, int width, int height,
1608 const uint8_t *uyvy_data, const uint8_t *alpha_data,
1609 uint8_t *out_buf, size_t out_max,
1610 size_t *out_size)
1611 {
1612 avx_speedhq_init();
1613  
1614 uint32_t tag = fourcc ? fourcc : (alpha_data ? 0x33514853 : 0x32514853);
1615 if (!avx_is_speedhq_fourcc(tag)) return -1;
1616  
1617 return avx_speedhq_encode_c99_ex(tag, width, height, uyvy_data, alpha_data, out_buf, out_max, out_size);
1618 }
1619  
1620 int avx_speedhq_decode_ex(uint32_t fourcc, int width, int height,
1621 const uint8_t *shq_data, size_t shq_size,
1622 uint8_t *uyvy_out, uint8_t *alpha_out,
1623 bool *p_has_alpha)
1624 {
1625 avx_speedhq_init();
1626  
1627 uint32_t tag = fourcc ? fourcc : 0x32514853;
1628 if (!avx_is_speedhq_fourcc(tag)) return -1;
1629  
1630 return avx_speedhq_decode_c99_ex(tag, width, height, shq_data, shq_size, uyvy_out, alpha_out, p_has_alpha);
1631 }
1632  
1633 int avx_speedhq_encode(int width, int height,
1634 const uint8_t *uyvy_data,
1635 uint8_t *out_buf, size_t out_max,
1636 size_t *out_size)
1637 {
1638 return avx_speedhq_encode_ex(0x32514853, width, height, uyvy_data, NULL, out_buf, out_max, out_size);
1639 }
1640  
1641 int avx_speedhq_decode(int width, int height,
1642 const uint8_t *shq_data, size_t shq_size,
1643 uint8_t *uyvy_out)
1644 {
1645 return avx_speedhq_decode_ex(0x32514853, width, height, shq_data, shq_size, uyvy_out, NULL, NULL);
1646 }
1647  
1648 void avx_speedhq_destroy(void)
1649 {
1650 shq_pool_shutdown_internal();
1651 avx_mutex_lock(&s_init_lock);
1652 s_initialized = false;
1653 avx_mutex_unlock(&s_init_lock);
1654 }