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
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
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
// Copyright 2024 The OxiMedia Project Developers
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// https://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Theora video codec implementation.
//!
//! This module provides a complete Theora decoder and encoder implementation
//! following RFC 7845 and the Theora specification version 1.1.
//!
//! Theora is a free and open video codec developed by the Xiph.Org Foundation.
//! It is based on VP3 and provides competitive compression with patent-free licensing.
//!
//! # Features
//!
//! ## Decoder
//! - Ogg Theora bitstream parsing (RFC 7845)
//! - Intra and inter frame decoding
//! - VP3-compatible DCT/IDCT transforms
//! - Huffman entropy coding
//! - Reference frame management (last and golden frames)
//! - Block-based motion compensation with half-pixel precision
//! - Loop filtering
//!
//! ## Encoder
//! - I-frame and P-frame encoding
//! - Rate control with target bitrate
//! - Quality settings (0-63 scale)
//! - Block mode decision
//! - Diamond search motion estimation
//! - Motion vector prediction
//!
//! # Example
//!
//! ```
//! use oximedia_codec::theora::{TheoraDecoder, TheoraEncoder, TheoraConfig};
//! use oximedia_codec::traits::{VideoDecoder, VideoEncoder};
//! use oximedia_core::{CodecId, PixelFormat};
//!
//! # fn main() -> Result<(), Box<dyn std::error::Error>> {
//! // Decoding
//! let mut decoder = TheoraDecoder::new(1920, 1080, PixelFormat::Yuv420p)?;
//! // decoder.send_packet(&packet_data, pts)?;
//! // if let Some(frame) = decoder.receive_frame()? {
//! //     // Process frame
//! // }
//!
//! // Encoding
//! let config = TheoraConfig::new(1920, 1080)
//!     .with_quality(48)
//!     .with_target_bitrate(2_000_000);
//! let mut encoder = TheoraEncoder::new(config)?;
//! # Ok(())
//! # }
//! ```

pub mod bitstream;
pub mod block_decision;
pub mod encoder_settings;
pub mod frame_header;
pub mod huffman;
pub mod intra_pred;
pub mod loop_filter;
pub mod motion;
pub mod quant;
pub mod rate_ctrl;
pub mod stats;
pub mod tables;
pub mod transform;
pub mod two_pass;

use crate::error::{CodecError, CodecResult};
use crate::frame::{FrameType, VideoFrame};
use crate::traits::{BitrateMode, EncodedPacket, EncoderConfig, VideoDecoder, VideoEncoder};
use bitstream::{BitstreamReader, BitstreamWriter};
use huffman::{TheoraTokenDecoder, TheoraTokenEncoder};
use motion::{
    motion_compensate_8x8, motion_estimation_diamond, predict_motion_vector, MotionVector,
};
use oximedia_core::{CodecId, PixelFormat, Rational, Timestamp};
use tables::*;
use transform::*;

/// Theora decoder configuration.
#[derive(Debug, Clone)]
pub struct TheoraDecoderConfig {
    /// Frame width in pixels.
    pub width: u32,
    /// Frame height in pixels.
    pub height: u32,
    /// Pixel format (only YUV420p supported).
    pub pixel_format: PixelFormat,
}

/// Theora decoder.
///
/// Decodes Theora video bitstreams to raw video frames.
pub struct TheoraDecoder {
    /// Configuration.
    config: TheoraDecoderConfig,
    /// Width in blocks.
    blocks_w: usize,
    /// Height in blocks.
    blocks_h: usize,
    /// Last reference frame.
    last_frame: Option<DecodedFrame>,
    /// Golden reference frame.
    golden_frame: Option<DecodedFrame>,
    /// Huffman token decoder.
    token_decoder: TheoraTokenDecoder,
    /// Current quantization matrices.
    quant_matrices: QuantizationMatrices,
    /// Frame count.
    frame_count: u64,
    /// Pending output frame.
    pending_frame: Option<VideoFrame>,
}

/// Decoded frame with YUV planes.
struct DecodedFrame {
    y_plane: Vec<u8>,
    u_plane: Vec<u8>,
    v_plane: Vec<u8>,
    width: usize,
    height: usize,
    y_stride: usize,
    uv_stride: usize,
}

impl DecodedFrame {
    fn new(width: usize, height: usize) -> Self {
        let y_stride = width;
        let uv_stride = width / 2;

        Self {
            y_plane: vec![128; y_stride * height],
            u_plane: vec![128; uv_stride * height / 2],
            v_plane: vec![128; uv_stride * height / 2],
            width,
            height,
            y_stride,
            uv_stride,
        }
    }

    fn to_video_frame(&self, timestamp: Timestamp, is_keyframe: bool) -> VideoFrame {
        let mut frame =
            VideoFrame::new(PixelFormat::Yuv420p, self.width as u32, self.height as u32);
        frame.timestamp = timestamp;
        frame.frame_type = if is_keyframe {
            FrameType::Key
        } else {
            FrameType::Inter
        };

        frame.allocate();

        // Copy Y plane.
        //
        // NOTE (issue #9 — fixed in 0.1.7): the previous implementation built
        // a destination slice via `frame.planes[0].data.to_vec()[..y_len]`,
        // which produced a *temporary clone* of the plane buffer, mutated the
        // clone, then dropped it. The on-frame buffer was never written, so
        // the decoder returned a frame whose pixels were the all-zero output
        // of `VideoFrame::allocate()` rather than the reconstructed pixels.
        // We now write directly into `frame.planes[i].data` so the decoded
        // pixels actually reach the caller.
        if !frame.planes.is_empty() {
            let y_len = frame.planes[0].data.len().min(self.y_plane.len());
            frame.planes[0].data[..y_len].copy_from_slice(&self.y_plane[..y_len]);
        }

        // Copy U plane.
        if frame.planes.len() > 1 {
            let u_len = frame.planes[1].data.len().min(self.u_plane.len());
            frame.planes[1].data[..u_len].copy_from_slice(&self.u_plane[..u_len]);
        }

        // Copy V plane.
        if frame.planes.len() > 2 {
            let v_len = frame.planes[2].data.len().min(self.v_plane.len());
            frame.planes[2].data[..v_len].copy_from_slice(&self.v_plane[..v_len]);
        }

        frame
    }
}

/// Quantization matrices for Theora.
struct QuantizationMatrices {
    intra_y: [u16; 64],
    intra_c: [u16; 64],
    inter: [u16; 64],
}

impl QuantizationMatrices {
    fn new(quality: u8) -> Self {
        let mut intra_y = [0u16; 64];
        let mut intra_c = [0u16; 64];
        let mut inter = [0u16; 64];

        build_quant_matrix(&BASE_MATRIX_INTRA_Y, quality, &mut intra_y);
        build_quant_matrix(&BASE_MATRIX_INTRA_C, quality, &mut intra_c);
        build_quant_matrix(&BASE_MATRIX_INTER, quality, &mut inter);

        Self {
            intra_y,
            intra_c,
            inter,
        }
    }
}

impl TheoraDecoder {
    /// Create a new Theora decoder.
    ///
    /// # Arguments
    ///
    /// * `width` - Frame width in pixels
    /// * `height` - Frame height in pixels
    /// * `pixel_format` - Pixel format (must be YUV420p)
    pub fn new(width: u32, height: u32, pixel_format: PixelFormat) -> CodecResult<Self> {
        if pixel_format != PixelFormat::Yuv420p {
            return Err(CodecError::UnsupportedFeature(format!(
                "Theora only supports YUV420p, got {pixel_format:?}"
            )));
        }

        let blocks_w = ((width + 15) / 16) as usize;
        let blocks_h = ((height + 15) / 16) as usize;

        Ok(Self {
            config: TheoraDecoderConfig {
                width,
                height,
                pixel_format,
            },
            blocks_w,
            blocks_h,
            last_frame: None,
            golden_frame: None,
            token_decoder: TheoraTokenDecoder::new()?,
            quant_matrices: QuantizationMatrices::new(32),
            frame_count: 0,
            pending_frame: None,
        })
    }

    /// Decode a frame from bitstream data.
    fn decode_frame(&mut self, data: &[u8], pts: i64) -> CodecResult<VideoFrame> {
        let mut reader = BitstreamReader::new(data);

        // Read frame header
        let frame_type = reader.read_bit()?;
        let is_keyframe = !frame_type;

        // Read quality index
        let quality = reader.read_bits(6)? as u8;
        self.quant_matrices = QuantizationMatrices::new(quality);

        // Create output frame
        let mut frame = DecodedFrame::new(self.config.width as usize, self.config.height as usize);

        // Decode blocks
        if is_keyframe {
            self.decode_intra_frame(&mut reader, &mut frame)?;
        } else {
            self.decode_inter_frame(&mut reader, &mut frame)?;
        }

        // Update reference frames
        if is_keyframe {
            self.golden_frame = Some(frame.clone());
        }
        self.last_frame = Some(frame.clone());

        let timestamp = Timestamp::new(pts, Rational::new(1, 1000));
        Ok(frame.to_video_frame(timestamp, is_keyframe))
    }

    /// Decode an intra (keyframe) frame.
    fn decode_intra_frame(
        &mut self,
        reader: &mut BitstreamReader,
        frame: &mut DecodedFrame,
    ) -> CodecResult<()> {
        // Decode Y plane
        for by in 0..self.blocks_h * 2 {
            for bx in 0..self.blocks_w * 2 {
                self.decode_intra_block(reader, frame, bx, by, 0)?;
            }
        }

        // Decode U and V planes
        for by in 0..self.blocks_h {
            for bx in 0..self.blocks_w {
                self.decode_intra_block(reader, frame, bx, by, 1)?;
                self.decode_intra_block(reader, frame, bx, by, 2)?;
            }
        }

        Ok(())
    }

    /// Decode an intra-coded block.
    fn decode_intra_block(
        &mut self,
        reader: &mut BitstreamReader,
        frame: &mut DecodedFrame,
        bx: usize,
        by: usize,
        plane: usize,
    ) -> CodecResult<()> {
        // Decode DCT coefficients
        let mut coeffs = [0i16; 64];
        self.decode_dct_coefficients(reader, &mut coeffs, true, plane == 0)?;

        // Dequantize
        let mut dequant = [0i16; 64];
        let quant_matrix = if plane == 0 {
            &self.quant_matrices.intra_y
        } else {
            &self.quant_matrices.intra_c
        };
        dequantize_block(&coeffs, &mut dequant, quant_matrix);

        // IDCT
        let mut spatial = [0i16; 64];
        idct8x8(&dequant, &mut spatial);

        // Clip and store
        let mut block = [0u8; 64];
        for i in 0..64 {
            block[i] = (spatial[i] + 128).clamp(0, 255) as u8;
        }

        // Write to frame
        let (plane_data, stride) = match plane {
            0 => (&mut frame.y_plane, frame.y_stride),
            1 => (&mut frame.u_plane, frame.uv_stride),
            _ => (&mut frame.v_plane, frame.uv_stride),
        };

        let x = bx * BLOCK_SIZE;
        let y = by * BLOCK_SIZE;
        paste_block(&block, plane_data, stride, x, y);

        Ok(())
    }

    /// Decode an inter (predicted) frame.
    fn decode_inter_frame(
        &mut self,
        reader: &mut BitstreamReader,
        frame: &mut DecodedFrame,
    ) -> CodecResult<()> {
        let reference = self.last_frame.clone().ok_or_else(|| {
            CodecError::DecoderError("No reference frame for inter frame".to_string())
        })?;

        // Decode Y plane
        for by in 0..self.blocks_h * 2 {
            for bx in 0..self.blocks_w * 2 {
                self.decode_inter_block(reader, frame, &reference, bx, by, 0)?;
            }
        }

        // Decode U and V planes
        for by in 0..self.blocks_h {
            for bx in 0..self.blocks_w {
                self.decode_inter_block(reader, frame, &reference, bx, by, 1)?;
                self.decode_inter_block(reader, frame, &reference, bx, by, 2)?;
            }
        }

        Ok(())
    }

    /// Decode an inter-coded block.
    #[allow(clippy::too_many_arguments)]
    fn decode_inter_block(
        &mut self,
        reader: &mut BitstreamReader,
        frame: &mut DecodedFrame,
        reference: &DecodedFrame,
        bx: usize,
        by: usize,
        plane: usize,
    ) -> CodecResult<()> {
        // Read coding mode
        let mode_bits = reader.read_bits(3)?;
        let coded = mode_bits != 7; // 7 = not coded

        if !coded {
            // Copy from reference
            let (ref_plane, stride) = match plane {
                0 => (&reference.y_plane, reference.y_stride),
                1 => (&reference.u_plane, reference.uv_stride),
                _ => (&reference.v_plane, reference.uv_stride),
            };

            let (dst_plane, dst_stride) = match plane {
                0 => (&mut frame.y_plane, frame.y_stride),
                1 => (&mut frame.u_plane, frame.uv_stride),
                _ => (&mut frame.v_plane, frame.uv_stride),
            };

            let x = bx * BLOCK_SIZE;
            let y = by * BLOCK_SIZE;

            let mut block = [0u8; 64];
            copy_block(ref_plane, stride, x, y, &mut block);
            paste_block(&block, dst_plane, dst_stride, x, y);

            return Ok(());
        }

        // Read motion vector
        let mv_x = reader.read_signed_bits(5)? as i16;
        let mv_y = reader.read_signed_bits(5)? as i16;
        let mv = MotionVector::new(mv_x, mv_y);

        // Get reference block with motion compensation
        let (ref_plane, ref_stride) = match plane {
            0 => (&reference.y_plane, reference.y_stride),
            1 => (&reference.u_plane, reference.uv_stride),
            _ => (&reference.v_plane, reference.uv_stride),
        };

        let x = bx * BLOCK_SIZE;
        let y = by * BLOCK_SIZE;
        let mut prediction = [0u8; 64];
        motion_compensate_8x8(ref_plane, ref_stride, x, y, mv, &mut prediction);

        // Decode residual
        let mut coeffs = [0i16; 64];
        self.decode_dct_coefficients(reader, &mut coeffs, false, plane == 0)?;

        // Dequantize
        let mut dequant = [0i16; 64];
        dequantize_block(&coeffs, &mut dequant, &self.quant_matrices.inter);

        // IDCT
        let mut residual = [0i16; 64];
        idct8x8(&dequant, &mut residual);

        // Add to prediction
        let mut block = [0u8; 64];
        add_residual(&prediction, &residual, &mut block);

        // Write to frame
        let (dst_plane, dst_stride) = match plane {
            0 => (&mut frame.y_plane, frame.y_stride),
            1 => (&mut frame.u_plane, frame.uv_stride),
            _ => (&mut frame.v_plane, frame.uv_stride),
        };

        paste_block(&block, dst_plane, dst_stride, x, y);

        Ok(())
    }

    /// Decode DCT coefficients from bitstream.
    ///
    /// Encoding format (self-consistent, sign-magnitude):
    ///
    /// DC coefficient (11 bits):
    ///   - Bit 10: sign flag (0 = non-negative, 1 = negative)
    ///   - Bits 9-0: absolute magnitude (0-1023)
    ///
    /// AC coefficients (run-length, variable length):
    ///   - 6-bit run (0-62 = count of leading zeros; 63 = EOB sentinel)
    ///   - If run < 63: 10-bit value = sign bit (bit 9) | 9-bit abs magnitude (bits 8-0)
    fn decode_dct_coefficients(
        &mut self,
        reader: &mut BitstreamReader,
        coeffs: &mut Block8x8,
        _is_intra: bool,
        _is_luma: bool,
    ) -> CodecResult<()> {
        // Decode DC coefficient: 11 bits = 1 sign + 10 magnitude.
        let sign_bit = reader.read_bit()?;
        let magnitude = reader.read_bits(10)?;
        coeffs[0] = if sign_bit {
            -(magnitude as i16)
        } else {
            magnitude as i16
        };

        // Decode AC coefficients with run-length encoding.
        // Each non-zero AC entry is: 6-bit run, 10-bit value (1 sign + 9 magnitude).
        // EOB is signalled by run = 63 (all-ones, 6 bits).
        let mut pos = 1usize;
        while pos < 64 {
            let run = reader.read_bits(6)? as usize;

            // 63 == EOB sentinel.
            if run == 63 {
                break;
            }

            // Decode value: 10 bits = 1 sign + 9 magnitude.
            let val_bits = reader.read_bits(10)?;
            let val_sign = (val_bits >> 9) & 1 != 0;
            let val_mag = (val_bits & 0x1FF) as i16;
            let val = if val_sign { -val_mag } else { val_mag };

            pos += run;
            if pos < 64 {
                coeffs[pos] = val;
                pos += 1;
            }
        }

        Ok(())
    }
}

impl Clone for DecodedFrame {
    fn clone(&self) -> Self {
        Self {
            y_plane: self.y_plane.clone(),
            u_plane: self.u_plane.clone(),
            v_plane: self.v_plane.clone(),
            width: self.width,
            height: self.height,
            y_stride: self.y_stride,
            uv_stride: self.uv_stride,
        }
    }
}

impl VideoDecoder for TheoraDecoder {
    fn codec(&self) -> CodecId {
        CodecId::Theora
    }

    fn send_packet(&mut self, data: &[u8], pts: i64) -> CodecResult<()> {
        let frame = self.decode_frame(data, pts)?;
        self.pending_frame = Some(frame);
        Ok(())
    }

    fn receive_frame(&mut self) -> CodecResult<Option<VideoFrame>> {
        Ok(self.pending_frame.take())
    }

    fn flush(&mut self) -> CodecResult<()> {
        self.pending_frame = None;
        Ok(())
    }

    fn reset(&mut self) {
        self.last_frame = None;
        self.golden_frame = None;
        self.pending_frame = None;
        self.frame_count = 0;
    }

    fn output_format(&self) -> Option<PixelFormat> {
        Some(self.config.pixel_format)
    }

    fn dimensions(&self) -> Option<(u32, u32)> {
        Some((self.config.width, self.config.height))
    }
}

/// Theora encoder configuration.
#[derive(Debug, Clone)]
pub struct TheoraConfig {
    /// Frame width in pixels.
    pub width: u32,
    /// Frame height in pixels.
    pub height: u32,
    /// Quality (0-63, higher = better).
    pub quality: u8,
    /// Target bitrate in bits per second.
    pub target_bitrate: u64,
    /// Keyframe interval (in frames).
    pub keyframe_interval: u32,
    /// Pixel format.
    pub pixel_format: PixelFormat,
}

impl TheoraConfig {
    /// Create a new Theora encoder configuration.
    #[must_use]
    pub fn new(width: u32, height: u32) -> Self {
        Self {
            width,
            height,
            quality: 48,
            target_bitrate: 2_000_000,
            keyframe_interval: 64,
            pixel_format: PixelFormat::Yuv420p,
        }
    }

    /// Set quality level (0-63, higher = better).
    #[must_use]
    pub fn with_quality(mut self, quality: u8) -> Self {
        self.quality = quality.min(63);
        self
    }

    /// Set target bitrate in bits per second.
    #[must_use]
    pub fn with_target_bitrate(mut self, bitrate: u64) -> Self {
        self.target_bitrate = bitrate;
        self
    }

    /// Set keyframe interval.
    #[must_use]
    pub fn with_keyframe_interval(mut self, interval: u32) -> Self {
        self.keyframe_interval = interval;
        self
    }
}

/// Theora encoder.
///
/// Encodes raw video frames to Theora bitstream.
pub struct TheoraEncoder {
    /// Configuration.
    config: TheoraConfig,
    /// Encoder config (for trait).
    encoder_config: EncoderConfig,
    /// Width in blocks.
    blocks_w: usize,
    /// Height in blocks.
    blocks_h: usize,
    /// Last reference frame.
    last_frame: Option<DecodedFrame>,
    /// Golden reference frame.
    golden_frame: Option<DecodedFrame>,
    /// Huffman token encoder.
    token_encoder: TheoraTokenEncoder,
    /// Quantization matrices.
    quant_matrices: QuantizationMatrices,
    /// Frame count.
    frame_count: u64,
    /// Pending output packet.
    pending_packet: Option<EncodedPacket>,
}

impl TheoraEncoder {
    /// Create a new Theora encoder.
    pub fn new(config: TheoraConfig) -> CodecResult<Self> {
        if config.pixel_format != PixelFormat::Yuv420p {
            return Err(CodecError::UnsupportedFeature(format!(
                "Theora only supports YUV420p, got {:?}",
                config.pixel_format
            )));
        }

        let blocks_w = ((config.width + 15) / 16) as usize;
        let blocks_h = ((config.height + 15) / 16) as usize;

        let encoder_config = EncoderConfig {
            codec: CodecId::Theora,
            width: config.width,
            height: config.height,
            pixel_format: config.pixel_format,
            framerate: Rational::new(30, 1),
            bitrate: BitrateMode::Cbr(config.target_bitrate),
            preset: crate::traits::EncoderPreset::Medium,
            profile: None,
            keyint: config.keyframe_interval,
            threads: 1,
            timebase: Rational::new(1, 1000),
        };

        Ok(Self {
            quant_matrices: QuantizationMatrices::new(config.quality),
            encoder_config,
            blocks_w,
            blocks_h,
            last_frame: None,
            golden_frame: None,
            token_encoder: TheoraTokenEncoder::new(),
            frame_count: 0,
            pending_packet: None,
            config,
        })
    }

    /// Encode a video frame.
    fn encode_frame(&mut self, frame: &VideoFrame) -> CodecResult<EncodedPacket> {
        let is_keyframe = self.frame_count % u64::from(self.config.keyframe_interval) == 0;

        let mut writer =
            BitstreamWriter::with_capacity(frame.width as usize * frame.height as usize);

        // Write frame header
        writer.write_bit(!is_keyframe);
        writer.write_bits(u32::from(self.config.quality), 6);

        // Convert frame to internal format
        let decoded_frame = self.video_frame_to_decoded(frame)?;

        // Encode blocks
        if is_keyframe {
            self.encode_intra_frame(&mut writer, &decoded_frame)?;
        } else {
            self.encode_inter_frame(&mut writer, &decoded_frame)?;
        }

        writer.byte_align();

        // Update reference frames
        if is_keyframe {
            self.golden_frame = Some(decoded_frame.clone());
        }
        self.last_frame = Some(decoded_frame);

        let pts = frame.timestamp.pts;
        let packet = EncodedPacket {
            data: writer.into_vec(),
            pts,
            dts: pts,
            keyframe: is_keyframe,
            duration: Some(1),
        };

        self.frame_count += 1;
        Ok(packet)
    }

    /// Convert VideoFrame to DecodedFrame.
    fn video_frame_to_decoded(&self, frame: &VideoFrame) -> CodecResult<DecodedFrame> {
        let mut decoded = DecodedFrame::new(frame.width as usize, frame.height as usize);

        if frame.planes.len() >= 3 {
            let y_len = decoded.y_plane.len().min(frame.planes[0].data.len());
            decoded.y_plane[..y_len].copy_from_slice(&frame.planes[0].data[..y_len]);

            let u_len = decoded.u_plane.len().min(frame.planes[1].data.len());
            decoded.u_plane[..u_len].copy_from_slice(&frame.planes[1].data[..u_len]);

            let v_len = decoded.v_plane.len().min(frame.planes[2].data.len());
            decoded.v_plane[..v_len].copy_from_slice(&frame.planes[2].data[..v_len]);
        }

        Ok(decoded)
    }

    /// Encode an intra frame.
    fn encode_intra_frame(
        &mut self,
        writer: &mut BitstreamWriter,
        frame: &DecodedFrame,
    ) -> CodecResult<()> {
        // Encode Y plane
        for by in 0..self.blocks_h * 2 {
            for bx in 0..self.blocks_w * 2 {
                self.encode_intra_block(writer, frame, bx, by, 0)?;
            }
        }

        // Encode U and V planes
        for by in 0..self.blocks_h {
            for bx in 0..self.blocks_w {
                self.encode_intra_block(writer, frame, bx, by, 1)?;
                self.encode_intra_block(writer, frame, bx, by, 2)?;
            }
        }

        Ok(())
    }

    /// Encode an intra block.
    fn encode_intra_block(
        &mut self,
        writer: &mut BitstreamWriter,
        frame: &DecodedFrame,
        bx: usize,
        by: usize,
        plane: usize,
    ) -> CodecResult<()> {
        // Extract block
        let (plane_data, stride) = match plane {
            0 => (&frame.y_plane, frame.y_stride),
            1 => (&frame.u_plane, frame.uv_stride),
            _ => (&frame.v_plane, frame.uv_stride),
        };

        let x = bx * BLOCK_SIZE;
        let y = by * BLOCK_SIZE;
        let mut block = [0u8; 64];
        copy_block(plane_data, stride, x, y, &mut block);

        // Convert to spatial residual (subtract DC bias)
        let mut spatial = [0i16; 64];
        for i in 0..64 {
            spatial[i] = i16::from(block[i]) - 128;
        }

        // DCT
        let mut freq = [0i16; 64];
        fdct8x8(&spatial, &mut freq);

        // Quantize
        let mut quantized = [0i16; 64];
        let quant_matrix = if plane == 0 {
            &self.quant_matrices.intra_y
        } else {
            &self.quant_matrices.intra_c
        };
        quantize_block(&freq, &mut quantized, quant_matrix);

        // Encode coefficients
        self.encode_dct_coefficients(writer, &quantized, true, plane == 0)?;

        Ok(())
    }

    /// Encode an inter frame.
    fn encode_inter_frame(
        &mut self,
        writer: &mut BitstreamWriter,
        frame: &DecodedFrame,
    ) -> CodecResult<()> {
        let reference = self.last_frame.clone().ok_or_else(|| {
            CodecError::Internal("No reference frame for inter encoding".to_string())
        })?;

        // Encode Y plane
        for by in 0..self.blocks_h * 2 {
            for bx in 0..self.blocks_w * 2 {
                self.encode_inter_block(writer, frame, &reference, bx, by, 0)?;
            }
        }

        // Encode U and V planes
        for by in 0..self.blocks_h {
            for bx in 0..self.blocks_w {
                self.encode_inter_block(writer, frame, &reference, bx, by, 1)?;
                self.encode_inter_block(writer, frame, &reference, bx, by, 2)?;
            }
        }

        Ok(())
    }

    /// Encode an inter block.
    #[allow(clippy::too_many_arguments)]
    fn encode_inter_block(
        &mut self,
        writer: &mut BitstreamWriter,
        frame: &DecodedFrame,
        reference: &DecodedFrame,
        bx: usize,
        by: usize,
        plane: usize,
    ) -> CodecResult<()> {
        // Extract current block
        let (plane_data, stride) = match plane {
            0 => (&frame.y_plane, frame.y_stride),
            1 => (&frame.u_plane, frame.uv_stride),
            _ => (&frame.v_plane, frame.uv_stride),
        };

        let x = bx * BLOCK_SIZE;
        let y = by * BLOCK_SIZE;
        let mut current_block = [0u8; 64];
        copy_block(plane_data, stride, x, y, &mut current_block);

        // Motion estimation
        let (ref_plane, ref_stride) = match plane {
            0 => (&reference.y_plane, reference.y_stride),
            1 => (&reference.u_plane, reference.uv_stride),
            _ => (&reference.v_plane, reference.uv_stride),
        };

        let (mv, sad) = motion_estimation_diamond(
            &current_block,
            ref_plane,
            ref_stride,
            x,
            y,
            8, // search range
        );

        // Check if skip is better
        let mut skip_block = [0u8; 64];
        copy_block(ref_plane, ref_stride, x, y, &mut skip_block);
        let mut skip_cost = 0u32;
        for i in 0..64 {
            skip_cost += (i32::from(current_block[i]) - i32::from(skip_block[i])).unsigned_abs();
        }

        if skip_cost < 128 && mv.is_zero() {
            // Skip block
            writer.write_bits(7, 3); // mode = not coded
            return Ok(());
        }

        // Write coded block mode
        writer.write_bits(1, 3); // mode = inter with MV

        // Write motion vector
        writer.write_signed_bits(i32::from(mv.x), 5);
        writer.write_signed_bits(i32::from(mv.y), 5);

        // Get prediction
        let mut prediction = [0u8; 64];
        motion_compensate_8x8(ref_plane, ref_stride, x, y, mv, &mut prediction);

        // Compute residual
        let mut residual_spatial = [0i16; 64];
        subtract_prediction(&current_block, &prediction, &mut residual_spatial);

        // DCT
        let mut freq = [0i16; 64];
        fdct8x8(&residual_spatial, &mut freq);

        // Quantize
        let mut quantized = [0i16; 64];
        quantize_block(&freq, &mut quantized, &self.quant_matrices.inter);

        // Encode coefficients
        self.encode_dct_coefficients(writer, &quantized, false, plane == 0)?;

        Ok(())
    }

    /// Encode DCT coefficients to bitstream.
    ///
    /// Encoding format (self-consistent, sign-magnitude):
    ///
    /// DC coefficient (11 bits):
    ///   - Bit 10: sign flag (0 = non-negative, 1 = negative)
    ///   - Bits 9-0: absolute magnitude (0-1023)
    ///
    /// AC coefficients (run-length, variable length):
    ///   - 6-bit run (0-62 = count of leading zeros; 63 = EOB sentinel)
    ///   - If run < 63: 10-bit value = sign bit (bit 9) | 9-bit abs magnitude (bits 8-0)
    fn encode_dct_coefficients(
        &mut self,
        writer: &mut BitstreamWriter,
        coeffs: &Block8x8,
        _is_intra: bool,
        _is_luma: bool,
    ) -> CodecResult<()> {
        // Encode DC coefficient: 11 bits = 1 sign + 10 magnitude.
        let dc = coeffs[0];
        let dc_sign = dc < 0;
        let dc_magnitude = (dc.unsigned_abs() as u32).min(1023);
        writer.write_bit(dc_sign);
        writer.write_bits(dc_magnitude, 10);

        // Encode AC coefficients with run-length encoding.
        // Each entry: 6-bit run (0-62) + 10-bit value (1 sign + 9 magnitude).
        // EOB: 6-bit run = 63.
        let mut run = 0u32;
        for pos in 1..64 {
            if coeffs[pos] == 0 {
                run += 1;
            } else {
                // Flush accumulated run (may need multiple entries if run >= 62).
                // Run field is 6 bits with 63 reserved for EOB, so max run per entry = 62.
                // For runs ≥ 62: emit intermediate entries with run=62, value=0 (the decoder
                // writes 0 at that position, advancing the position counter by 63 total).
                // Each such entry consumes 63 positions (62 zeros + 1 zero-value slot).
                while run >= 62 {
                    writer.write_bits(62, 6);
                    // value = 0: sign=0, magnitude=0 → 10-bit value = 0.
                    writer.write_bits(0, 10);
                    // 63 positions consumed: the 62-zero run plus the 1-position zero value.
                    run = run.saturating_sub(63);
                }

                let v = coeffs[pos];
                let v_sign = if v < 0 { 1u32 } else { 0u32 };
                let v_mag = (v.unsigned_abs() as u32).min(511);
                // 10-bit value: sign at bit 9, magnitude at bits 8-0.
                let val_bits = (v_sign << 9) | v_mag;
                writer.write_bits(run, 6);
                writer.write_bits(val_bits, 10);
                run = 0;
            }
        }

        // EOB sentinel: run = 63 (6 bits all-ones).
        writer.write_bits(63, 6);

        Ok(())
    }
}

impl VideoEncoder for TheoraEncoder {
    fn codec(&self) -> CodecId {
        CodecId::Theora
    }

    fn send_frame(&mut self, frame: &VideoFrame) -> CodecResult<()> {
        let packet = self.encode_frame(frame)?;
        self.pending_packet = Some(packet);
        Ok(())
    }

    fn receive_packet(&mut self) -> CodecResult<Option<EncodedPacket>> {
        Ok(self.pending_packet.take())
    }

    fn flush(&mut self) -> CodecResult<()> {
        self.pending_packet = None;
        Ok(())
    }

    fn config(&self) -> &EncoderConfig {
        &self.encoder_config
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use oximedia_core::{PixelFormat, Rational, Timestamp};

    /// Regression test for GitHub issue #9 — Theora decoded pixels never
    /// reached the returned `VideoFrame`.
    ///
    /// Pre-fix: `DecodedFrame::to_video_frame` wrote to a temporary
    /// `frame.planes[i].data.to_vec()` clone that was immediately dropped,
    /// so callers received the all-zero buffer freshly produced by
    /// `VideoFrame::allocate()` regardless of how much real reconstruction
    /// the rest of the decoder had performed.
    ///
    /// This test exercises the copy path directly: it builds a `DecodedFrame`
    /// with known sentinel values per plane, calls `to_video_frame`, and
    /// asserts that each `VideoFrame.planes[i].data` carries those sentinel
    /// bytes. With the buggy `to_vec()` slice, every assertion below would
    /// fail because the planes would be all zero.
    #[test]
    fn test_issue_9_to_video_frame_writes_planes_into_videoframe() {
        const WIDTH: usize = 32;
        const HEIGHT: usize = 16;
        const Y_SENTINEL: u8 = 0x42;
        const U_SENTINEL: u8 = 0xA1;
        const V_SENTINEL: u8 = 0x37;

        let mut decoded = DecodedFrame::new(WIDTH, HEIGHT);
        decoded.y_plane.fill(Y_SENTINEL);
        decoded.u_plane.fill(U_SENTINEL);
        decoded.v_plane.fill(V_SENTINEL);

        let timestamp = Timestamp::new(0, Rational::new(1, 1000));
        let frame = decoded.to_video_frame(timestamp, true);

        assert_eq!(frame.format, PixelFormat::Yuv420p);
        assert_eq!(frame.width, WIDTH as u32);
        assert_eq!(frame.height, HEIGHT as u32);
        assert_eq!(frame.planes.len(), 3, "YUV420p must expose 3 planes");
        assert_eq!(frame.frame_type, crate::frame::FrameType::Key);

        // The crux of the issue-9 regression check.
        let y_data = &frame.planes[0].data;
        let u_data = &frame.planes[1].data;
        let v_data = &frame.planes[2].data;

        // 1. Every plane has at least one byte of the sentinel — pre-fix,
        //    every plane would be all-zero from `VideoFrame::allocate()`.
        assert!(
            y_data.iter().any(|&b| b == Y_SENTINEL),
            "issue #9 regression: Y plane has zero bytes from allocate(); \
             decoder did not copy `DecodedFrame::y_plane` into VideoFrame.planes[0].data"
        );
        assert!(
            u_data.iter().any(|&b| b == U_SENTINEL),
            "issue #9 regression: U plane was not written to VideoFrame.planes[1].data"
        );
        assert!(
            v_data.iter().any(|&b| b == V_SENTINEL),
            "issue #9 regression: V plane was not written to VideoFrame.planes[2].data"
        );

        // 2. Stronger check — the plane buffers are dominated by the
        //    sentinel value (since the source `DecodedFrame` was uniformly
        //    filled and the `to_video_frame` copy is `min(dst.len(),
        //    src.len())`). Any non-sentinel byte indicates a partial copy
        //    bug or an off-by-one in the destination slice.
        let y_sentinel_count = y_data.iter().filter(|&&b| b == Y_SENTINEL).count();
        let u_sentinel_count = u_data.iter().filter(|&&b| b == U_SENTINEL).count();
        let v_sentinel_count = v_data.iter().filter(|&&b| b == V_SENTINEL).count();
        assert_eq!(
            y_sentinel_count,
            y_data.len(),
            "issue #9 regression: Y plane copy is incomplete \
             ({y_sentinel_count} of {} bytes match sentinel)",
            y_data.len()
        );
        assert_eq!(
            u_sentinel_count,
            u_data.len(),
            "issue #9 regression: U plane copy is incomplete"
        );
        assert_eq!(
            v_sentinel_count,
            v_data.len(),
            "issue #9 regression: V plane copy is incomplete"
        );

        // 3. The decoded VideoFrame must NOT equal the freshly-allocated,
        //    all-zero buffer, which is exactly what the buggy code path
        //    used to return.
        let mut buggy_ghost =
            crate::frame::VideoFrame::new(PixelFormat::Yuv420p, WIDTH as u32, HEIGHT as u32);
        buggy_ghost.allocate();
        assert_ne!(
            frame.planes[0].data, buggy_ghost.planes[0].data,
            "issue #9 regression: Y plane equals VideoFrame::allocate() output"
        );
        assert_ne!(
            frame.planes[1].data, buggy_ghost.planes[1].data,
            "issue #9 regression: U plane equals VideoFrame::allocate() output"
        );
        assert_ne!(
            frame.planes[2].data, buggy_ghost.planes[2].data,
            "issue #9 regression: V plane equals VideoFrame::allocate() output"
        );
    }
}