oximedia-codec 0.1.7

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
//! Bitstream reading and writing for H.263.
//!
//! This module provides bit-level I/O operations for parsing and
//! generating H.263 bitstreams.

use crate::CodecError;

/// Bitstream reader for H.263 data.
///
/// Reads bits from a byte buffer, MSB first.
pub struct BitReader<'a> {
    data: &'a [u8],
    byte_pos: usize,
    bit_pos: u8,
}

impl<'a> BitReader<'a> {
    /// Create a new bit reader.
    ///
    /// # Arguments
    ///
    /// * `data` - Input byte buffer
    #[must_use]
    pub fn new(data: &'a [u8]) -> Self {
        Self {
            data,
            byte_pos: 0,
            bit_pos: 0,
        }
    }

    /// Read a single bit.
    ///
    /// # Errors
    ///
    /// Returns error if end of buffer reached.
    pub fn read_bit(&mut self) -> Result<bool, CodecError> {
        if self.byte_pos >= self.data.len() {
            return Err(CodecError::InvalidData(
                "Unexpected end of bitstream".into(),
            ));
        }

        let byte = self.data[self.byte_pos];
        let bit = (byte >> (7 - self.bit_pos)) & 1;

        self.bit_pos += 1;
        if self.bit_pos == 8 {
            self.bit_pos = 0;
            self.byte_pos += 1;
        }

        Ok(bit != 0)
    }

    /// Read multiple bits as an unsigned integer.
    ///
    /// # Arguments
    ///
    /// * `n` - Number of bits to read (1-32)
    ///
    /// # Errors
    ///
    /// Returns error if not enough bits available.
    pub fn read_bits(&mut self, n: u8) -> Result<u32, CodecError> {
        if n == 0 || n > 32 {
            return Err(CodecError::InvalidData(format!("Invalid bit count: {n}")));
        }

        let mut value = 0u32;
        for _ in 0..n {
            value = (value << 1) | u32::from(self.read_bit()?);
        }

        Ok(value)
    }

    /// Read a signed integer.
    ///
    /// # Arguments
    ///
    /// * `n` - Number of bits to read (1-32)
    ///
    /// # Errors
    ///
    /// Returns error if not enough bits available.
    pub fn read_signed_bits(&mut self, n: u8) -> Result<i32, CodecError> {
        let value = self.read_bits(n)?;

        // Sign extension
        let sign_bit = 1u32 << (n - 1);
        if (value & sign_bit) != 0 {
            // Negative number
            let mask = (1u32 << n) - 1;
            Ok((value | !mask) as i32)
        } else {
            Ok(value as i32)
        }
    }

    /// Peek at the next n bits without consuming them.
    ///
    /// # Arguments
    ///
    /// * `n` - Number of bits to peek (1-32)
    ///
    /// # Errors
    ///
    /// Returns error if not enough bits available.
    pub fn peek_bits(&self, n: u8) -> Result<u32, CodecError> {
        if n == 0 || n > 32 {
            return Err(CodecError::InvalidData(format!("Invalid bit count: {n}")));
        }

        let mut value = 0u32;
        let mut byte_pos = self.byte_pos;
        let mut bit_pos = self.bit_pos;

        for _ in 0..n {
            if byte_pos >= self.data.len() {
                return Err(CodecError::InvalidData(
                    "Unexpected end of bitstream".into(),
                ));
            }

            let byte = self.data[byte_pos];
            let bit = (byte >> (7 - bit_pos)) & 1;
            value = (value << 1) | u32::from(bit);

            bit_pos += 1;
            if bit_pos == 8 {
                bit_pos = 0;
                byte_pos += 1;
            }
        }

        Ok(value)
    }

    /// Skip n bits.
    ///
    /// # Arguments
    ///
    /// * `n` - Number of bits to skip
    ///
    /// # Errors
    ///
    /// Returns error if not enough bits available.
    pub fn skip_bits(&mut self, n: usize) -> Result<(), CodecError> {
        for _ in 0..n {
            self.read_bit()?;
        }
        Ok(())
    }

    /// Align to next byte boundary.
    pub fn byte_align(&mut self) {
        if self.bit_pos != 0 {
            self.bit_pos = 0;
            self.byte_pos += 1;
        }
    }

    /// Get current bit position.
    #[must_use]
    pub fn bit_position(&self) -> usize {
        self.byte_pos * 8 + self.bit_pos as usize
    }

    /// Get number of bits remaining.
    #[must_use]
    pub fn bits_remaining(&self) -> usize {
        (self.data.len() - self.byte_pos) * 8 - self.bit_pos as usize
    }

    /// Check if at end of buffer.
    #[must_use]
    pub fn is_eof(&self) -> bool {
        self.byte_pos >= self.data.len()
    }

    /// Search for the next start code (0x0000 01xx).
    ///
    /// # Returns
    ///
    /// Position of start code, or None if not found.
    pub fn find_start_code(&mut self) -> Option<usize> {
        self.byte_align();

        while self.byte_pos + 2 < self.data.len() {
            if self.data[self.byte_pos] == 0x00
                && self.data[self.byte_pos + 1] == 0x00
                && (self.data[self.byte_pos + 2] & 0x80) == 0x80
            {
                return Some(self.byte_pos);
            }
            self.byte_pos += 1;
        }

        None
    }

    /// Read VLC code up to max_bits.
    ///
    /// # Arguments
    ///
    /// * `max_bits` - Maximum number of bits to read
    ///
    /// # Errors
    ///
    /// Returns error if not enough bits available.
    pub fn read_vlc(&mut self, max_bits: u8) -> Result<(u32, u8), CodecError> {
        for bits in 1..=max_bits {
            let code = self.peek_bits(bits)?;
            // Caller will validate the code
            self.skip_bits(bits as usize)?;
            return Ok((code, bits));
        }

        Err(CodecError::InvalidData("VLC code too long".into()))
    }
}

/// Bitstream writer for H.263 data.
///
/// Writes bits to a byte buffer, MSB first.
pub struct BitWriter {
    data: Vec<u8>,
    bit_pos: u8,
}

impl BitWriter {
    /// Create a new bit writer.
    #[must_use]
    pub fn new() -> Self {
        Self {
            data: Vec::new(),
            bit_pos: 0,
        }
    }

    /// Create a new bit writer with capacity.
    ///
    /// # Arguments
    ///
    /// * `capacity` - Initial capacity in bytes
    #[must_use]
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            data: Vec::with_capacity(capacity),
            bit_pos: 0,
        }
    }

    /// Write a single bit.
    ///
    /// # Arguments
    ///
    /// * `bit` - Bit value (true = 1, false = 0)
    pub fn write_bit(&mut self, bit: bool) {
        if self.bit_pos == 0 {
            self.data.push(0);
        }

        if bit {
            let last_idx = self.data.len() - 1;
            self.data[last_idx] |= 1 << (7 - self.bit_pos);
        }

        self.bit_pos += 1;
        if self.bit_pos == 8 {
            self.bit_pos = 0;
        }
    }

    /// Write multiple bits from an unsigned integer.
    ///
    /// # Arguments
    ///
    /// * `value` - Value to write
    /// * `n` - Number of bits to write (1-32)
    pub fn write_bits(&mut self, value: u32, n: u8) {
        if n == 0 || n > 32 {
            return;
        }

        for i in (0..n).rev() {
            let bit = (value >> i) & 1;
            self.write_bit(bit != 0);
        }
    }

    /// Write a signed integer.
    ///
    /// # Arguments
    ///
    /// * `value` - Signed value to write
    /// * `n` - Number of bits to write (1-32)
    pub fn write_signed_bits(&mut self, value: i32, n: u8) {
        let mask = (1u32 << n) - 1;
        let unsigned = (value as u32) & mask;
        self.write_bits(unsigned, n);
    }

    /// Align to next byte boundary with zero padding.
    pub fn byte_align(&mut self) {
        while self.bit_pos != 0 {
            self.write_bit(false);
        }
    }

    /// Get the current data.
    #[must_use]
    pub fn data(&self) -> &[u8] {
        &self.data
    }

    /// Consume the writer and return the data.
    #[must_use]
    pub fn into_vec(mut self) -> Vec<u8> {
        if self.bit_pos != 0 {
            self.byte_align();
        }
        self.data
    }

    /// Get current bit position.
    #[must_use]
    pub fn bit_position(&self) -> usize {
        self.data.len() * 8 + self.bit_pos as usize
    }

    /// Write a VLC code.
    ///
    /// # Arguments
    ///
    /// * `code` - VLC code value
    /// * `bits` - Number of bits in the code
    pub fn write_vlc(&mut self, code: u32, bits: u8) {
        self.write_bits(code, bits);
    }

    /// Write stuffing bits (zero padding to byte boundary).
    pub fn write_stuffing(&mut self) {
        self.byte_align();
    }

    /// Write a start code.
    ///
    /// # Arguments
    ///
    /// * `code` - Start code value (0x00-0xFF)
    pub fn write_start_code(&mut self, code: u8) {
        self.byte_align();
        self.data.extend_from_slice(&[0x00, 0x00, code]);
    }
}

impl Default for BitWriter {
    fn default() -> Self {
        Self::new()
    }
}

/// Exponential-Golomb code reader/writer utilities.
pub struct ExpGolomb;

impl ExpGolomb {
    /// Read unsigned Exp-Golomb code.
    ///
    /// # Arguments
    ///
    /// * `reader` - Bit reader
    ///
    /// # Errors
    ///
    /// Returns error if invalid code.
    pub fn read_ue(reader: &mut BitReader<'_>) -> Result<u32, CodecError> {
        let mut leading_zeros = 0;

        while !reader.read_bit()? {
            leading_zeros += 1;
            if leading_zeros > 31 {
                return Err(CodecError::InvalidData("Invalid Exp-Golomb code".into()));
            }
        }

        if leading_zeros == 0 {
            return Ok(0);
        }

        let value = reader.read_bits(leading_zeros)?;
        Ok((1 << leading_zeros) - 1 + value)
    }

    /// Read signed Exp-Golomb code.
    ///
    /// # Arguments
    ///
    /// * `reader` - Bit reader
    ///
    /// # Errors
    ///
    /// Returns error if invalid code.
    pub fn read_se(reader: &mut BitReader<'_>) -> Result<i32, CodecError> {
        let value = Self::read_ue(reader)?;
        if value == 0 {
            return Ok(0);
        }

        let sign = if (value & 1) != 0 { 1 } else { -1 };
        Ok(sign * ((value + 1) / 2) as i32)
    }

    /// Write unsigned Exp-Golomb code.
    ///
    /// # Arguments
    ///
    /// * `writer` - Bit writer
    /// * `value` - Value to encode
    pub fn write_ue(writer: &mut BitWriter, value: u32) {
        if value == 0 {
            writer.write_bit(true);
            return;
        }

        let bits = 32 - (value + 1).leading_zeros();
        let leading_zeros = bits - 1;

        // Write leading zeros
        for _ in 0..leading_zeros {
            writer.write_bit(false);
        }

        // Write 1 bit
        writer.write_bit(true);

        // Write remaining bits
        if leading_zeros > 0 {
            let remainder = value + 1 - (1 << leading_zeros);
            writer.write_bits(remainder, leading_zeros as u8);
        }
    }

    /// Write signed Exp-Golomb code.
    ///
    /// # Arguments
    ///
    /// * `writer` - Bit writer
    /// * `value` - Signed value to encode
    pub fn write_se(writer: &mut BitWriter, value: i32) {
        if value == 0 {
            Self::write_ue(writer, 0);
            return;
        }

        let abs_value = value.unsigned_abs();
        let code = if value > 0 {
            2 * abs_value - 1
        } else {
            2 * abs_value
        };

        Self::write_ue(writer, code);
    }
}

/// Utilities for stuffing and emulation prevention.
pub struct StuffingHelper;

impl StuffingHelper {
    /// Check if byte sequence needs emulation prevention.
    ///
    /// Detects 0x000000-0x000003 patterns that could be confused with start codes.
    #[must_use]
    pub fn needs_emulation_prevention(data: &[u8], pos: usize) -> bool {
        if pos < 2 {
            return false;
        }

        data[pos - 2] == 0x00 && data[pos - 1] == 0x00 && data[pos] <= 0x03
    }

    /// Add emulation prevention bytes.
    ///
    /// Inserts 0x03 byte after 0x0000 sequences to prevent start code emulation.
    #[must_use]
    pub fn add_emulation_prevention(data: &[u8]) -> Vec<u8> {
        let mut result = Vec::with_capacity(data.len() + data.len() / 100);
        let mut zero_count = 0;

        for &byte in data {
            if zero_count == 2 && byte <= 0x03 {
                result.push(0x03); // Emulation prevention byte
                zero_count = 0;
            }

            result.push(byte);

            if byte == 0x00 {
                zero_count += 1;
            } else {
                zero_count = 0;
            }
        }

        result
    }

    /// Remove emulation prevention bytes.
    #[must_use]
    pub fn remove_emulation_prevention(data: &[u8]) -> Vec<u8> {
        let mut result = Vec::with_capacity(data.len());
        let mut i = 0;

        while i < data.len() {
            if i + 2 < data.len() && data[i] == 0x00 && data[i + 1] == 0x00 && data[i + 2] == 0x03 {
                // Found emulation prevention byte
                result.push(0x00);
                result.push(0x00);
                i += 3; // Skip the 0x03 byte
            } else {
                result.push(data[i]);
                i += 1;
            }
        }

        result
    }
}