oximedia-codec 0.1.4

Video codec implementations for OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
//! Palette mode implementation (AV1).
//!
//! Palette mode uses a small set of colors (2-8) to represent the block,
//! with each pixel being an index into the palette. This is particularly
//! effective for blocks with limited color variation, such as graphics
//! or screen content.
//!
//! # Structure
//!
//! - **PaletteInfo**: Contains palette colors and size
//! - **ColorCache**: Previously used colors for prediction
//! - **ColorIndexMap**: Per-pixel palette indices
//!
//! # Encoding
//!
//! 1. Palette colors are signaled in the bitstream
//! 2. Color indices are entropy coded using context
//! 3. The decoder reconstructs pixels by looking up palette colors

#![forbid(unsafe_code)]
#![allow(dead_code)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::doc_markdown)]

use super::{BitDepth, BlockDimensions, IntraPredContext, IntraPredictor};

/// Maximum palette size (AV1 supports 2-8 colors).
pub const MAX_PALETTE_SIZE: usize = 8;

/// Minimum palette size.
pub const MIN_PALETTE_SIZE: usize = 2;

/// Maximum color cache size.
pub const MAX_COLOR_CACHE_SIZE: usize = 64;

/// Palette information for a block.
#[derive(Clone, Debug)]
pub struct PaletteInfo {
    /// Palette colors (Y/U/V or R/G/B values).
    colors: [u16; MAX_PALETTE_SIZE],
    /// Number of colors in the palette (2-8).
    size: usize,
    /// Bit depth of color values.
    bit_depth: BitDepth,
}

impl PaletteInfo {
    /// Create a new empty palette.
    #[must_use]
    pub const fn new(bit_depth: BitDepth) -> Self {
        Self {
            colors: [0; MAX_PALETTE_SIZE],
            size: 0,
            bit_depth,
        }
    }

    /// Create a palette with specified colors.
    #[must_use]
    pub fn with_colors(colors: &[u16], bit_depth: BitDepth) -> Self {
        let size = colors.len().min(MAX_PALETTE_SIZE);
        let mut palette_colors = [0u16; MAX_PALETTE_SIZE];
        palette_colors[..size].copy_from_slice(&colors[..size]);

        Self {
            colors: palette_colors,
            size,
            bit_depth,
        }
    }

    /// Get the palette size.
    #[must_use]
    pub const fn size(&self) -> usize {
        self.size
    }

    /// Set the palette size.
    pub fn set_size(&mut self, size: usize) {
        self.size = size.clamp(MIN_PALETTE_SIZE, MAX_PALETTE_SIZE);
    }

    /// Get a color at the specified index.
    #[must_use]
    pub fn get_color(&self, idx: usize) -> u16 {
        if idx < self.size {
            self.colors[idx]
        } else {
            0
        }
    }

    /// Set a color at the specified index.
    pub fn set_color(&mut self, idx: usize, color: u16) {
        if idx < MAX_PALETTE_SIZE {
            self.colors[idx] = color.min(self.bit_depth.max_value());
            if idx >= self.size {
                self.size = idx + 1;
            }
        }
    }

    /// Get all colors as a slice.
    #[must_use]
    pub fn colors(&self) -> &[u16] {
        &self.colors[..self.size]
    }

    /// Check if the palette is valid.
    #[must_use]
    pub const fn is_valid(&self) -> bool {
        self.size >= MIN_PALETTE_SIZE && self.size <= MAX_PALETTE_SIZE
    }

    /// Sort colors in ascending order.
    pub fn sort_colors(&mut self) {
        self.colors[..self.size].sort_unstable();
    }

    /// Find the nearest color index for a given value.
    #[must_use]
    pub fn find_nearest(&self, value: u16) -> usize {
        let mut best_idx = 0;
        let mut best_diff = u32::MAX;

        for (idx, &color) in self.colors[..self.size].iter().enumerate() {
            let diff = (i32::from(value) - i32::from(color)).unsigned_abs();
            if diff < best_diff {
                best_diff = diff;
                best_idx = idx;
            }
        }

        best_idx
    }
}

impl Default for PaletteInfo {
    fn default() -> Self {
        Self::new(BitDepth::Bits8)
    }
}

/// Color cache for palette mode.
///
/// Stores recently used colors to improve entropy coding efficiency.
#[derive(Clone, Debug)]
pub struct ColorCache {
    /// Cached colors.
    colors: Vec<u16>,
    /// Maximum cache size.
    max_size: usize,
    /// Bit depth.
    bit_depth: BitDepth,
}

impl ColorCache {
    /// Create a new color cache.
    #[must_use]
    pub fn new(max_size: usize, bit_depth: BitDepth) -> Self {
        Self {
            colors: Vec::with_capacity(max_size),
            max_size,
            bit_depth,
        }
    }

    /// Add a color to the cache.
    pub fn add(&mut self, color: u16) {
        // Don't add duplicates
        if self.colors.contains(&color) {
            return;
        }

        if self.colors.len() >= self.max_size {
            // Remove oldest color
            self.colors.remove(0);
        }

        self.colors.push(color);
    }

    /// Check if a color is in the cache.
    #[must_use]
    pub fn contains(&self, color: u16) -> bool {
        self.colors.contains(&color)
    }

    /// Find the index of a color in the cache.
    #[must_use]
    pub fn find(&self, color: u16) -> Option<usize> {
        self.colors.iter().position(|&c| c == color)
    }

    /// Get all cached colors.
    #[must_use]
    pub fn colors(&self) -> &[u16] {
        &self.colors
    }

    /// Get the cache size.
    #[must_use]
    pub fn len(&self) -> usize {
        self.colors.len()
    }

    /// Check if the cache is empty.
    #[must_use]
    pub fn is_empty(&self) -> bool {
        self.colors.is_empty()
    }

    /// Clear the cache.
    pub fn clear(&mut self) {
        self.colors.clear();
    }

    /// Build cache from neighbor samples.
    pub fn build_from_neighbors(&mut self, top: &[u16], left: &[u16]) {
        self.clear();

        // Add unique colors from top
        for &color in top {
            self.add(color);
        }

        // Add unique colors from left
        for &color in left {
            self.add(color);
        }
    }
}

impl Default for ColorCache {
    fn default() -> Self {
        Self::new(MAX_COLOR_CACHE_SIZE, BitDepth::Bits8)
    }
}

/// Color index map for a block.
#[derive(Clone, Debug)]
pub struct ColorIndexMap {
    /// Per-pixel palette indices.
    indices: Vec<u8>,
    /// Block width.
    width: usize,
    /// Block height.
    height: usize,
}

impl ColorIndexMap {
    /// Create a new color index map.
    #[must_use]
    pub fn new(width: usize, height: usize) -> Self {
        Self {
            indices: vec![0; width * height],
            width,
            height,
        }
    }

    /// Get the index at a position.
    #[must_use]
    pub fn get(&self, x: usize, y: usize) -> u8 {
        if x < self.width && y < self.height {
            self.indices[y * self.width + x]
        } else {
            0
        }
    }

    /// Set the index at a position.
    pub fn set(&mut self, x: usize, y: usize, idx: u8) {
        if x < self.width && y < self.height {
            self.indices[y * self.width + x] = idx;
        }
    }

    /// Get all indices as a slice.
    #[must_use]
    pub fn indices(&self) -> &[u8] {
        &self.indices
    }

    /// Get indices as mutable slice.
    pub fn indices_mut(&mut self) -> &mut [u8] {
        &mut self.indices
    }
}

/// Palette predictor.
#[derive(Clone, Debug)]
pub struct PalettePredictor {
    /// Palette information.
    palette: PaletteInfo,
    /// Color index map.
    index_map: ColorIndexMap,
}

impl PalettePredictor {
    /// Create a new palette predictor.
    #[must_use]
    pub fn new(palette: PaletteInfo, width: usize, height: usize) -> Self {
        Self {
            palette,
            index_map: ColorIndexMap::new(width, height),
        }
    }

    /// Get the palette info.
    #[must_use]
    pub const fn palette(&self) -> &PaletteInfo {
        &self.palette
    }

    /// Get mutable palette info.
    pub fn palette_mut(&mut self) -> &mut PaletteInfo {
        &mut self.palette
    }

    /// Get the index map.
    #[must_use]
    pub const fn index_map(&self) -> &ColorIndexMap {
        &self.index_map
    }

    /// Get mutable index map.
    pub fn index_map_mut(&mut self) -> &mut ColorIndexMap {
        &mut self.index_map
    }

    /// Set a color index at a position.
    pub fn set_index(&mut self, x: usize, y: usize, idx: u8) {
        self.index_map.set(x, y, idx);
    }

    /// Reconstruct the block from palette indices.
    pub fn reconstruct(&self, output: &mut [u16], stride: usize, dims: BlockDimensions) {
        for y in 0..dims.height {
            let row_start = y * stride;
            for x in 0..dims.width {
                let idx = self.index_map.get(x, y) as usize;
                output[row_start + x] = self.palette.get_color(idx);
            }
        }
    }
}

impl IntraPredictor for PalettePredictor {
    fn predict(
        &self,
        _ctx: &IntraPredContext,
        output: &mut [u16],
        stride: usize,
        dims: BlockDimensions,
    ) {
        self.reconstruct(output, stride, dims);
    }
}

/// Decode color indices using run-length coding.
pub fn decode_color_indices_rle(
    data: &[u8],
    index_map: &mut ColorIndexMap,
    palette_size: usize,
) -> usize {
    let mut offset = 0;
    let mut x = 0;
    let mut y = 0;
    let width = index_map.width;
    let height = index_map.height;

    while y < height && offset < data.len() {
        let color_idx = data[offset] % (palette_size as u8);
        offset += 1;

        let mut run_length = 1;
        if offset < data.len() {
            run_length = data[offset] as usize + 1;
            offset += 1;
        }

        for _ in 0..run_length {
            if y >= height {
                break;
            }
            index_map.set(x, y, color_idx);
            x += 1;
            if x >= width {
                x = 0;
                y += 1;
            }
        }
    }

    offset
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_palette_info_creation() {
        let palette = PaletteInfo::new(BitDepth::Bits8);
        assert_eq!(palette.size(), 0);
        assert!(!palette.is_valid());

        let palette = PaletteInfo::with_colors(&[100, 150, 200], BitDepth::Bits8);
        assert_eq!(palette.size(), 3);
        assert!(palette.is_valid());
        assert_eq!(palette.get_color(0), 100);
        assert_eq!(palette.get_color(1), 150);
        assert_eq!(palette.get_color(2), 200);
    }

    #[test]
    fn test_palette_set_color() {
        let mut palette = PaletteInfo::new(BitDepth::Bits8);
        palette.set_color(0, 100);
        palette.set_color(1, 200);

        assert_eq!(palette.size(), 2);
        assert!(palette.is_valid());
        assert_eq!(palette.get_color(0), 100);
        assert_eq!(palette.get_color(1), 200);
    }

    #[test]
    fn test_palette_find_nearest() {
        let palette = PaletteInfo::with_colors(&[0, 100, 200, 255], BitDepth::Bits8);

        assert_eq!(palette.find_nearest(0), 0);
        assert_eq!(palette.find_nearest(50), 0); // Closer to 0 than 100 (dist 50 vs 50, first wins)
        assert_eq!(palette.find_nearest(60), 1); // Closer to 100 (dist 40 vs 60)
        assert_eq!(palette.find_nearest(150), 1); // Equal distance to 100 and 200, first wins
        assert_eq!(palette.find_nearest(160), 2); // Closer to 200 (dist 40 vs 60)
        assert_eq!(palette.find_nearest(255), 3);
    }

    #[test]
    fn test_palette_sort() {
        let mut palette = PaletteInfo::with_colors(&[200, 50, 150, 100], BitDepth::Bits8);
        palette.sort_colors();

        assert_eq!(palette.colors(), &[50, 100, 150, 200]);
    }

    #[test]
    fn test_color_cache() {
        let mut cache = ColorCache::new(4, BitDepth::Bits8);

        cache.add(100);
        cache.add(150);
        cache.add(200);

        assert_eq!(cache.len(), 3);
        assert!(cache.contains(100));
        assert!(cache.contains(150));
        assert!(!cache.contains(50));

        assert_eq!(cache.find(150), Some(1));
        assert_eq!(cache.find(50), None);
    }

    #[test]
    fn test_color_cache_overflow() {
        let mut cache = ColorCache::new(3, BitDepth::Bits8);

        cache.add(100);
        cache.add(150);
        cache.add(200);
        cache.add(250); // Should evict 100

        assert_eq!(cache.len(), 3);
        assert!(!cache.contains(100));
        assert!(cache.contains(150));
        assert!(cache.contains(250));
    }

    #[test]
    fn test_color_cache_no_duplicates() {
        let mut cache = ColorCache::new(4, BitDepth::Bits8);

        cache.add(100);
        cache.add(100);
        cache.add(100);

        assert_eq!(cache.len(), 1);
    }

    #[test]
    fn test_color_index_map() {
        let mut map = ColorIndexMap::new(4, 4);

        map.set(0, 0, 1);
        map.set(1, 0, 2);
        map.set(0, 1, 3);

        assert_eq!(map.get(0, 0), 1);
        assert_eq!(map.get(1, 0), 2);
        assert_eq!(map.get(0, 1), 3);
        assert_eq!(map.get(2, 2), 0); // Default
    }

    #[test]
    fn test_palette_predictor() {
        let palette = PaletteInfo::with_colors(&[0, 128, 255], BitDepth::Bits8);
        let mut predictor = PalettePredictor::new(palette, 2, 2);

        predictor.set_index(0, 0, 0);
        predictor.set_index(1, 0, 1);
        predictor.set_index(0, 1, 2);
        predictor.set_index(1, 1, 1);

        let dims = BlockDimensions::new(2, 2);
        let mut output = vec![0u16; 4];

        predictor.reconstruct(&mut output, 2, dims);

        assert_eq!(output[0], 0);
        assert_eq!(output[1], 128);
        assert_eq!(output[2], 255);
        assert_eq!(output[3], 128);
    }

    #[test]
    fn test_decode_color_indices_rle() {
        let mut map = ColorIndexMap::new(4, 2);

        // Color 0 with run of 4, Color 1 with run of 4
        let data = [0, 3, 1, 3];

        let bytes_read = decode_color_indices_rle(&data, &mut map, 3);

        assert_eq!(bytes_read, 4);
        // First row: 0, 0, 0, 0
        assert_eq!(map.get(0, 0), 0);
        assert_eq!(map.get(3, 0), 0);
        // Second row: 1, 1, 1, 1
        assert_eq!(map.get(0, 1), 1);
        assert_eq!(map.get(3, 1), 1);
    }

    #[test]
    fn test_build_cache_from_neighbors() {
        let mut cache = ColorCache::new(16, BitDepth::Bits8);

        let top = [100, 100, 150, 200];
        let left = [100, 125, 175, 200];

        cache.build_from_neighbors(&top, &left);

        // Should have unique colors: 100, 150, 200, 125, 175
        assert!(cache.contains(100));
        assert!(cache.contains(150));
        assert!(cache.contains(200));
        assert!(cache.contains(125));
        assert!(cache.contains(175));
    }
}