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
//! GIF (Graphics Interchange Format) codec implementation.
//!
//! Supports GIF89a specification:
//! - Decoding and encoding
//! - Animation with multiple frames
//! - Transparency support
//! - Color quantization (median cut, octree)
//! - Dithering (Floyd-Steinberg, ordered)
//! - Loop count control
//!
//! # Examples
//!
//! ## Decoding
//!
//! ```ignore
//! use oximedia_codec::gif::GifDecoder;
//!
//! let data = std::fs::read("animation.gif")?;
//! let decoder = GifDecoder::new(&data)?;
//!
//! println!("Frames: {}", decoder.frame_count());
//! println!("Size: {}x{}", decoder.width(), decoder.height());
//!
//! for i in 0..decoder.frame_count() {
//!     let frame = decoder.decode_frame(i)?;
//!     // Process frame...
//! }
//! ```
//!
//! ## Encoding
//!
//! ```ignore
//! use oximedia_codec::gif::{GifEncoder, GifEncoderConfig, GifFrameConfig};
//!
//! let config = GifEncoderConfig {
//!     colors: 256,
//!     loop_count: 0, // Infinite loop
//!     ..Default::default()
//! };
//!
//! let mut encoder = GifEncoder::new(640, 480, config)?;
//!
//! let frame_config = GifFrameConfig {
//!     delay_time: 10, // 100ms
//!     ..Default::default()
//! };
//!
//! let data = encoder.encode(&frames, &[frame_config; frames.len()])?;
//! std::fs::write("output.gif", &data)?;
//! ```

mod decoder;
mod encoder;
mod lzw;
pub mod quality;

use crate::error::{CodecError, CodecResult};
use crate::frame::VideoFrame;

pub use decoder::{GifFrame, GraphicsControlExtension, ImageDescriptor, LogicalScreenDescriptor};
pub use encoder::{DitheringMethod, GifEncoderConfig, GifFrameConfig, QuantizationMethod};

use decoder::GifDecoderState;
use encoder::GifEncoderState;

/// GIF decoder for reading GIF89a files.
///
/// Supports:
/// - Single images and animations
/// - Transparency
/// - Interlaced images
/// - Global and local color tables
pub struct GifDecoder {
    state: GifDecoderState,
}

impl GifDecoder {
    /// Create a new GIF decoder from data.
    ///
    /// # Arguments
    ///
    /// * `data` - GIF file data
    ///
    /// # Errors
    ///
    /// Returns error if data is not a valid GIF file.
    pub fn new(data: &[u8]) -> CodecResult<Self> {
        let state = GifDecoderState::decode(data)?;
        Ok(Self { state })
    }

    /// Get canvas width.
    #[must_use]
    pub fn width(&self) -> u32 {
        u32::from(self.state.screen_descriptor.width)
    }

    /// Get canvas height.
    #[must_use]
    pub fn height(&self) -> u32 {
        u32::from(self.state.screen_descriptor.height)
    }

    /// Get number of frames.
    #[must_use]
    pub fn frame_count(&self) -> usize {
        self.state.frames.len()
    }

    /// Get loop count (0 = infinite).
    #[must_use]
    pub fn loop_count(&self) -> u16 {
        self.state.loop_count
    }

    /// Get the logical screen descriptor.
    #[must_use]
    pub fn screen_descriptor(&self) -> &LogicalScreenDescriptor {
        &self.state.screen_descriptor
    }

    /// Get the global color table.
    #[must_use]
    pub fn global_color_table(&self) -> &[u8] {
        &self.state.global_color_table
    }

    /// Get frame information.
    ///
    /// # Arguments
    ///
    /// * `index` - Frame index
    ///
    /// # Errors
    ///
    /// Returns error if index is out of range.
    pub fn frame_info(&self, index: usize) -> CodecResult<&GifFrame> {
        self.state.frames.get(index).ok_or_else(|| {
            CodecError::InvalidParameter(format!("Frame index {} out of range", index))
        })
    }

    /// Decode a specific frame.
    ///
    /// # Arguments
    ///
    /// * `index` - Frame index to decode
    ///
    /// # Errors
    ///
    /// Returns error if decoding fails or index is out of range.
    pub fn decode_frame(&self, index: usize) -> CodecResult<VideoFrame> {
        self.state.frame_to_video_frame(index)
    }

    /// Decode all frames.
    ///
    /// # Errors
    ///
    /// Returns error if decoding fails.
    pub fn decode_all_frames(&self) -> CodecResult<Vec<VideoFrame>> {
        let mut frames = Vec::with_capacity(self.frame_count());
        for i in 0..self.frame_count() {
            frames.push(self.decode_frame(i)?);
        }
        Ok(frames)
    }

    /// Get frame delay time in milliseconds.
    ///
    /// # Arguments
    ///
    /// * `index` - Frame index
    ///
    /// # Errors
    ///
    /// Returns error if index is out of range.
    pub fn frame_delay_ms(&self, index: usize) -> CodecResult<u32> {
        let frame = self.frame_info(index)?;
        if let Some(ref control) = frame.control {
            // Delay time is in hundredths of a second
            Ok(u32::from(control.delay_time) * 10)
        } else {
            Ok(0)
        }
    }

    /// Check if frame has transparency.
    ///
    /// # Arguments
    ///
    /// * `index` - Frame index
    ///
    /// # Errors
    ///
    /// Returns error if index is out of range.
    pub fn frame_has_transparency(&self, index: usize) -> CodecResult<bool> {
        let frame = self.frame_info(index)?;
        Ok(frame.control.as_ref().map_or(false, |c| c.has_transparency))
    }

    /// Get frame disposal method.
    ///
    /// # Arguments
    ///
    /// * `index` - Frame index
    ///
    /// # Errors
    ///
    /// Returns error if index is out of range.
    pub fn frame_disposal_method(&self, index: usize) -> CodecResult<DisposalMethod> {
        let frame = self.frame_info(index)?;
        if let Some(ref control) = frame.control {
            Ok(DisposalMethod::from_value(control.disposal_method))
        } else {
            Ok(DisposalMethod::None)
        }
    }

    /// Check if this is an animated GIF.
    #[must_use]
    pub fn is_animated(&self) -> bool {
        self.frame_count() > 1
    }

    /// Get total animation duration in milliseconds.
    #[must_use]
    pub fn total_duration_ms(&self) -> u32 {
        let mut total = 0;
        for i in 0..self.frame_count() {
            if let Ok(delay) = self.frame_delay_ms(i) {
                total += delay;
            }
        }
        total
    }
}

/// GIF encoder for creating GIF89a files.
///
/// Supports:
/// - Single images and animations
/// - Color quantization with configurable palette size
/// - Dithering options
/// - Transparency
/// - Loop count control
pub struct GifEncoder {
    state: GifEncoderState,
}

impl GifEncoder {
    /// Create a new GIF encoder.
    ///
    /// # Arguments
    ///
    /// * `width` - Canvas width
    /// * `height` - Canvas height
    /// * `config` - Encoder configuration
    ///
    /// # Errors
    ///
    /// Returns error if parameters are invalid.
    pub fn new(width: u32, height: u32, config: GifEncoderConfig) -> CodecResult<Self> {
        let state = GifEncoderState::new(width, height, config)?;
        Ok(Self { state })
    }

    /// Encode frames to GIF data.
    ///
    /// # Arguments
    ///
    /// * `frames` - Frames to encode
    /// * `frame_configs` - Configuration for each frame
    ///
    /// # Errors
    ///
    /// Returns error if encoding fails.
    pub fn encode(
        &mut self,
        frames: &[VideoFrame],
        frame_configs: &[GifFrameConfig],
    ) -> CodecResult<Vec<u8>> {
        self.state.encode(frames, frame_configs)
    }

    /// Encode a single frame (non-animated GIF).
    ///
    /// # Arguments
    ///
    /// * `frame` - Frame to encode
    ///
    /// # Errors
    ///
    /// Returns error if encoding fails.
    pub fn encode_single(&mut self, frame: &VideoFrame) -> CodecResult<Vec<u8>> {
        let config = GifFrameConfig::default();
        self.encode(&[frame.clone()], &[config])
    }

    /// Encode multiple frames with the same delay time.
    ///
    /// # Arguments
    ///
    /// * `frames` - Frames to encode
    /// * `delay_ms` - Delay between frames in milliseconds
    ///
    /// # Errors
    ///
    /// Returns error if encoding fails.
    pub fn encode_animation(
        &mut self,
        frames: &[VideoFrame],
        delay_ms: u32,
    ) -> CodecResult<Vec<u8>> {
        let delay_time = (delay_ms / 10) as u16; // Convert to hundredths of a second
        let config = GifFrameConfig {
            delay_time,
            ..Default::default()
        };
        let configs = vec![config; frames.len()];
        self.encode(frames, &configs)
    }
}

/// Frame disposal method.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DisposalMethod {
    /// No disposal specified.
    None,
    /// Do not dispose (keep frame).
    Keep,
    /// Restore to background color.
    Background,
    /// Restore to previous frame.
    Previous,
}

impl DisposalMethod {
    /// Convert from GIF disposal method value.
    #[must_use]
    pub fn from_value(value: u8) -> Self {
        match value {
            0 => Self::None,
            1 => Self::Keep,
            2 => Self::Background,
            3 => Self::Previous,
            _ => Self::None,
        }
    }

    /// Convert to GIF disposal method value.
    #[must_use]
    pub fn to_value(self) -> u8 {
        match self {
            Self::None => 0,
            Self::Keep => 1,
            Self::Background => 2,
            Self::Previous => 3,
        }
    }
}

/// Detect if data is a GIF file.
///
/// # Arguments
///
/// * `data` - Data to check
///
/// # Examples
///
/// ```ignore
/// let data = std::fs::read("image.gif")?;
/// if is_gif(&data) {
///     let decoder = GifDecoder::new(&data)?;
///     // ...
/// }
/// ```
#[must_use]
pub fn is_gif(data: &[u8]) -> bool {
    data.len() >= 6 && data.starts_with(b"GIF") && (&data[3..6] == b"89a" || &data[3..6] == b"87a")
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::frame::{Plane, VideoFrame};
    use bytes::Bytes;
    use oximedia_core::PixelFormat;

    fn create_test_frame(width: u32, height: u32, color: [u8; 3]) -> VideoFrame {
        let size = (width * height * 4) as usize;
        let mut data = Vec::with_capacity(size);

        for _ in 0..(width * height) {
            data.extend_from_slice(&[color[0], color[1], color[2], 255]);
        }

        let plane = Plane {
            data,
            stride: (width * 4) as usize,
            width,
            height,
        };

        let mut frame = VideoFrame::new(PixelFormat::Rgba32, width, height);
        frame.planes = vec![plane];
        frame
    }

    #[test]
    fn test_is_gif() {
        assert!(is_gif(b"GIF89a\x00\x00"));
        assert!(is_gif(b"GIF87a\x00\x00"));
        assert!(!is_gif(b"PNG\x00\x00\x00"));
        assert!(!is_gif(b"GIF"));
    }

    #[test]
    fn test_disposal_method() {
        assert_eq!(DisposalMethod::from_value(0), DisposalMethod::None);
        assert_eq!(DisposalMethod::from_value(1), DisposalMethod::Keep);
        assert_eq!(DisposalMethod::from_value(2), DisposalMethod::Background);
        assert_eq!(DisposalMethod::from_value(3), DisposalMethod::Previous);

        assert_eq!(DisposalMethod::None.to_value(), 0);
        assert_eq!(DisposalMethod::Keep.to_value(), 1);
        assert_eq!(DisposalMethod::Background.to_value(), 2);
        assert_eq!(DisposalMethod::Previous.to_value(), 3);
    }

    #[test]
    fn test_encoder_single_frame() {
        let config = GifEncoderConfig::default();
        let mut encoder = GifEncoder::new(16, 16, config).expect("should succeed");

        let frame = create_test_frame(16, 16, [255, 0, 0]);
        let data = encoder.encode_single(&frame).expect("should succeed");

        assert!(is_gif(&data));
        assert!(data.len() > 100); // Should have header + data
    }

    #[test]
    fn test_encoder_animation() {
        let config = GifEncoderConfig {
            colors: 16,
            loop_count: 0,
            ..Default::default()
        };
        let mut encoder = GifEncoder::new(16, 16, config).expect("should succeed");

        let frames = vec![
            create_test_frame(16, 16, [255, 0, 0]),
            create_test_frame(16, 16, [0, 255, 0]),
            create_test_frame(16, 16, [0, 0, 255]),
        ];

        let data = encoder
            .encode_animation(&frames, 100)
            .expect("should succeed");

        assert!(is_gif(&data));

        // Decode and verify
        let decoder = GifDecoder::new(&data).expect("should succeed");
        assert_eq!(decoder.frame_count(), 3);
        assert_eq!(decoder.width(), 16);
        assert_eq!(decoder.height(), 16);
        assert!(decoder.is_animated());
    }

    #[test]
    fn test_roundtrip() {
        let config = GifEncoderConfig {
            colors: 256,
            ..Default::default()
        };
        let mut encoder = GifEncoder::new(8, 8, config).expect("should succeed");

        let original_frame = create_test_frame(8, 8, [128, 64, 192]);
        let data = encoder
            .encode_single(&original_frame)
            .expect("should succeed");

        let decoder = GifDecoder::new(&data).expect("should succeed");
        assert_eq!(decoder.frame_count(), 1);
        assert_eq!(decoder.width(), 8);
        assert_eq!(decoder.height(), 8);

        let decoded_frame = decoder.decode_frame(0).expect("should succeed");
        assert_eq!(decoded_frame.format, PixelFormat::Rgba32);
        assert_eq!(decoded_frame.width, 8);
        assert_eq!(decoded_frame.height, 8);
    }
}