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
//! H.263 video codec implementation.
//!
//! This module provides a complete H.263 encoder and decoder based on
//! ITU-T Recommendation H.263 (1998). H.263 patents expired in 2019,
//! making it safe for educational and compatibility purposes.
//!
//! # Features
//!
//! ## Decoder
//! - Picture header parsing (I and P frames)
//! - GOB (Group of Blocks) layer parsing
//! - Macroblock layer decoding
//! - Motion compensation with half-pixel precision
//! - INTER and INTRA modes
//! - Optional advanced features (UMV, AP)
//! - DCT/IDCT transform
//! - Loop filter (deblocking)
//!
//! ## Encoder
//! - Picture encoding with I and P frames
//! - Rate control (CBR, VBR, CRF)
//! - Motion estimation (diamond, hexagon, three-step)
//! - Mode decision (INTER vs INTRA)
//! - Quantization with perceptual weighting
//! - VLC encoding
//!
//! # Supported Formats
//!
//! - Sub-QCIF (128x96)
//! - QCIF (176x144)
//! - CIF (352x288)
//! - 4CIF (704x576)
//! - 16CIF (1408x1152)
//! - Extended (custom dimensions)
//!
//! # Example
//!
//! ```ignore
//! use oximedia_codec::h263::{H263Decoder, PictureFormat};
//! use oximedia_codec::traits::{VideoDecoder, DecoderConfig};
//! use oximedia_core::CodecId;
//!
//! let config = DecoderConfig {
//!     codec: CodecId::H263,
//!     extradata: None,
//!     threads: 1,
//!     low_latency: false,
//! };
//!
//! let mut decoder = H263Decoder::new(config)?;
//! decoder.send_packet(&packet_data, 0)?;
//!
//! if let Some(frame) = decoder.receive_frame()? {
//!     // Process decoded frame
//! }
//! ```
//!
//! # References
//!
//! - ITU-T Recommendation H.263 (1998)
//! - ITU-T Recommendation H.263 (2005) - Advanced features

mod bitstream;
mod header;
mod motion;
mod quantization;
mod vlc;

pub use header::{AdvancedModes, PictureFormat};

use crate::{
    traits::{DecoderConfig, EncodedPacket, EncoderConfig, VideoDecoder, VideoEncoder},
    CodecError, CodecResult, FrameType, Plane, VideoFrame,
};
use bitstream::{BitReader, BitWriter};
use header::{GobHeader, MacroblockHeader, MacroblockType, PictureCodingType, PictureHeader};
use motion::{LoopFilter, MotionEstimator, MotionVector, MotionVectorPredictor, SearchAlgorithm};
use oximedia_core::{CodecId, PixelFormat};
use quantization::{dequantize_block, quantize_block};
use vlc::{
    decode_cbpy, decode_mcbpc_i, decode_mcbpc_p, decode_mvd, encode_cbpy, encode_mcbpc_i,
    encode_mcbpc_p, encode_mvd,
};

/// H.263 video decoder.
pub struct H263Decoder {
    /// Decoder configuration.
    config: DecoderConfig,
    /// Current frame width.
    width: Option<u32>,
    /// Current frame height.
    height: Option<u32>,
    /// Current picture format.
    format: Option<PictureFormat>,
    /// Reference frame (previous decoded frame).
    reference_frame: Option<VideoFrame>,
    /// Pending decoded frames.
    pending_frames: Vec<VideoFrame>,
    /// Current quantizer.
    quantizer: u8,
    /// Loop filter enabled.
    loop_filter_enabled: bool,
    /// Advanced modes.
    advanced_modes: AdvancedModes,
}

impl H263Decoder {
    /// Create a new H.263 decoder.
    ///
    /// # Arguments
    ///
    /// * `config` - Decoder configuration
    ///
    /// # Errors
    ///
    /// Returns error if configuration is invalid.
    pub fn new(config: DecoderConfig) -> CodecResult<Self> {
        Ok(Self {
            config,
            width: None,
            height: None,
            format: None,
            reference_frame: None,
            pending_frames: Vec::new(),
            quantizer: quantization::DEFAULT_QP,
            loop_filter_enabled: false,
            advanced_modes: AdvancedModes::default(),
        })
    }

    /// Decode a picture.
    fn decode_picture(&mut self, data: &[u8], pts: i64) -> CodecResult<VideoFrame> {
        let mut reader = BitReader::new(data);

        // Parse picture header
        let header = PictureHeader::parse(&mut reader)?;

        // Update decoder state
        let (width, height) = header.dimensions()?;
        self.width = Some(width);
        self.height = Some(height);
        self.format = Some(header.source_format);
        self.quantizer = header.quantizer;

        // Update advanced modes
        self.advanced_modes.umv = header.umv_mode;
        self.advanced_modes.sac = header.sac_mode;
        self.advanced_modes.ap = header.ap_mode;
        self.advanced_modes.pb_frames = header.pb_frames_mode;

        // Create mutable plane buffers
        let y_stride = ((width + 15) / 16) * 16; // Align to 16
        let c_stride = ((width / 2 + 15) / 16) * 16;
        let y_size = (y_stride * height) as usize;
        let c_size = (c_stride * (height / 2)) as usize;

        let mut y_plane = vec![0u8; y_size];
        let mut cb_plane = vec![0u8; c_size];
        let mut cr_plane = vec![0u8; c_size];

        // Decode picture data
        let mb_width = ((width + 15) / 16) as usize;
        let mb_height = ((height + 15) / 16) as usize;

        let mut mv_predictor = MotionVectorPredictor::new(mb_width);

        for mb_y in 0..mb_height {
            for mb_x in 0..mb_width {
                self.decode_macroblock(
                    &mut reader,
                    &header,
                    &mut y_plane,
                    &mut cb_plane,
                    &mut cr_plane,
                    y_stride as usize,
                    c_stride as usize,
                    &mut mv_predictor,
                    mb_x,
                    mb_y,
                )?;
            }
            mv_predictor.next_row();
        }

        // Apply loop filter if enabled
        if self.loop_filter_enabled {
            let filter = LoopFilter::new(self.quantizer / 2);
            for mb_y in 0..mb_height {
                for mb_x in 0..mb_width {
                    filter.filter_mb_edge(&mut y_plane, y_stride as usize, mb_x, mb_y, true);
                    filter.filter_mb_edge(&mut y_plane, y_stride as usize, mb_x, mb_y, false);
                }
            }
        }

        // Create output frame
        let mut frame = VideoFrame::new(PixelFormat::Yuv420p, width, height);
        frame.timestamp = oximedia_core::Timestamp::new(pts, oximedia_core::Rational::new(1, 1000));
        frame.frame_type = if header.is_intra() {
            FrameType::Key
        } else {
            FrameType::Inter
        };

        // Convert buffers to planes
        frame.planes.push(Plane::new(y_plane, y_stride as usize));
        frame.planes.push(Plane::new(cb_plane, c_stride as usize));
        frame.planes.push(Plane::new(cr_plane, c_stride as usize));

        // Store as reference frame for inter prediction
        if !header.is_intra() {
            self.reference_frame = Some(frame.clone());
        } else {
            self.reference_frame = Some(frame.clone());
        }

        Ok(frame)
    }

    /// Decode a macroblock.
    #[allow(clippy::too_many_arguments)]
    fn decode_macroblock(
        &mut self,
        reader: &mut BitReader<'_>,
        header: &PictureHeader,
        y_plane: &mut [u8],
        cb_plane: &mut [u8],
        cr_plane: &mut [u8],
        y_stride: usize,
        c_stride: usize,
        mv_predictor: &mut MotionVectorPredictor,
        mb_x: usize,
        mb_y: usize,
    ) -> CodecResult<()> {
        // Check for COD (Coded macroblock) bit in P-frames
        if header.is_inter() {
            let cod = reader.read_bit()?;
            if !cod {
                // Macroblock is not coded, copy from reference
                // Simplified: just skip for now
                return Ok(());
            }
        }

        // Decode MCBPC (MB type and CBPC)
        let (mb_type_code, cbpc) = if header.is_intra() {
            self.decode_mcbpc_i(reader)?
        } else {
            self.decode_mcbpc_p(reader)?
        };

        let mb_type = match mb_type_code {
            0 => MacroblockType::Inter,
            1 => MacroblockType::InterQ,
            2 => MacroblockType::Inter4V,
            3 => MacroblockType::Intra,
            4 => MacroblockType::IntraQ,
            _ => return Err(CodecError::InvalidData("Invalid MB type".into())),
        };

        // Decode CBPY (Coded Block Pattern for Y)
        let cbpy = self.decode_cbpy(reader, mb_type.is_intra())?;

        // Decode DQUANT if present
        let mut current_quant = self.quantizer;
        if mb_type.has_quant() {
            let dquant = reader.read_signed_bits(2)?;
            current_quant = ((current_quant as i32) + dquant).clamp(1, 31) as u8;
            self.quantizer = current_quant;
        }

        // Decode motion vectors for inter macroblocks
        if mb_type.is_inter() {
            let mv = self.decode_motion_vector(reader, mv_predictor, mb_x, mb_y)?;
            mv_predictor.update(mb_x, mv);
        }

        // Decode blocks
        let mb_header = MacroblockHeader::new(mb_type);

        // Decode and reconstruct blocks (simplified implementation)
        for block_idx in 0..6 {
            if mb_header.is_block_coded(block_idx) {
                let block =
                    self.decode_block(reader, current_quant, block_idx == 0 && mb_type.is_intra())?;

                // Apply IDCT
                let idct_block = idct_8x8(&block);

                // Write block to output planes
                self.write_block_to_planes(
                    &idct_block,
                    y_plane,
                    cb_plane,
                    cr_plane,
                    y_stride,
                    c_stride,
                    mb_x,
                    mb_y,
                    block_idx,
                );
            }
        }

        Ok(())
    }

    /// Write a decoded block to the appropriate plane.
    #[allow(clippy::too_many_arguments)]
    fn write_block_to_planes(
        &self,
        block: &[[i16; 8]; 8],
        y_plane: &mut [u8],
        cb_plane: &mut [u8],
        cr_plane: &mut [u8],
        y_stride: usize,
        c_stride: usize,
        mb_x: usize,
        mb_y: usize,
        block_idx: usize,
    ) {
        let (plane, stride, bx, by) = if block_idx < 4 {
            // Y blocks
            let bx = (block_idx % 2) * 8;
            let by = (block_idx / 2) * 8;
            (y_plane, y_stride, mb_x * 16 + bx, mb_y * 16 + by)
        } else if block_idx == 4 {
            // Cb block
            (cb_plane, c_stride, mb_x * 8, mb_y * 8)
        } else {
            // Cr block
            (cr_plane, c_stride, mb_x * 8, mb_y * 8)
        };

        for y in 0..8 {
            for x in 0..8 {
                let offset = (by + y) * stride + (bx + x);
                if offset < plane.len() {
                    plane[offset] = block[y][x].clamp(0, 255) as u8;
                }
            }
        }
    }

    /// Decode MCBPC for I-frames.
    fn decode_mcbpc_i(&self, reader: &mut BitReader<'_>) -> CodecResult<(u8, u8)> {
        // Try reading VLC codes of increasing length
        for bits in 1..=8 {
            if let Ok(code) = reader.peek_bits(bits) {
                if let Some((mb_type, cbpc)) = decode_mcbpc_i(code, bits) {
                    reader.skip_bits(bits as usize)?;
                    return Ok((mb_type, cbpc));
                }
            }
        }

        Err(CodecError::InvalidData("Invalid MCBPC code".into()))
    }

    /// Decode MCBPC for P-frames.
    fn decode_mcbpc_p(&self, reader: &mut BitReader<'_>) -> CodecResult<(u8, u8)> {
        for bits in 1..=11 {
            if let Ok(code) = reader.peek_bits(bits) {
                if let Some((mb_type, cbpc)) = decode_mcbpc_p(code, bits) {
                    reader.skip_bits(bits as usize)?;
                    return Ok((mb_type, cbpc));
                }
            }
        }

        Err(CodecError::InvalidData("Invalid MCBPC code".into()))
    }

    /// Decode CBPY.
    fn decode_cbpy(&self, reader: &mut BitReader<'_>, intra: bool) -> CodecResult<u8> {
        for bits in 4..=5 {
            if let Ok(code) = reader.peek_bits(bits) {
                if let Some(cbpy) = decode_cbpy(code, bits, intra) {
                    reader.skip_bits(bits as usize)?;
                    return Ok(cbpy);
                }
            }
        }

        Err(CodecError::InvalidData("Invalid CBPY code".into()))
    }

    /// Decode motion vector.
    fn decode_motion_vector(
        &self,
        reader: &mut BitReader<'_>,
        mv_predictor: &MotionVectorPredictor,
        mb_x: usize,
        mb_y: usize,
    ) -> CodecResult<MotionVector> {
        let pred_mv = mv_predictor.predict(mb_x, mb_y);

        // Decode MVD_X
        let mvd_x = self.decode_mvd(reader)?;
        // Decode MVD_Y
        let mvd_y = self.decode_mvd(reader)?;

        let mv = MotionVector::new(pred_mv.x + mvd_x, pred_mv.y + mvd_y);

        Ok(mv)
    }

    /// Decode motion vector difference.
    fn decode_mvd(&self, reader: &mut BitReader<'_>) -> CodecResult<i16> {
        for bits in 1..=13 {
            if let Ok(code) = reader.peek_bits(bits) {
                if let Some(mvd) = decode_mvd(code, bits) {
                    reader.skip_bits(bits as usize)?;
                    return Ok(mvd as i16);
                }
            }
        }

        Err(CodecError::InvalidData("Invalid MVD code".into()))
    }

    /// Decode a transform block.
    fn decode_block(
        &self,
        reader: &mut BitReader<'_>,
        qp: u8,
        is_dc_intra: bool,
    ) -> CodecResult<[[i16; 8]; 8]> {
        let mut block = [[0i16; 8]; 8];

        // Decode DC coefficient for intra blocks
        if is_dc_intra {
            let dc_size = self.decode_intra_dc_size(reader)?;
            if dc_size > 0 {
                let dc_diff = reader.read_signed_bits(dc_size)?;
                block[0][0] = dc_diff as i16;
            }
        }

        // Decode AC coefficients
        let mut coeff_idx = if is_dc_intra { 1 } else { 0 };

        loop {
            // Try to decode TCOEF VLC
            let (run, level, last) = self.decode_tcoef(reader)?;

            coeff_idx += run as usize;
            if coeff_idx >= 64 {
                break;
            }

            let row = coeff_idx / 8;
            let col = coeff_idx % 8;
            block[row][col] = level;

            coeff_idx += 1;

            if last || coeff_idx >= 64 {
                break;
            }
        }

        // Dequantize
        let dequantized = dequantize_block(&block, qp);

        Ok(dequantized)
    }

    /// Decode intra DC coefficient size.
    fn decode_intra_dc_size(&self, reader: &mut BitReader<'_>) -> CodecResult<u8> {
        // Simple implementation - read 8-bit size
        let size = reader.read_bits(3)?;
        Ok(size as u8)
    }

    /// Decode TCOEF (transform coefficient).
    fn decode_tcoef(&self, reader: &mut BitReader<'_>) -> CodecResult<(u8, i16, bool)> {
        // Check for escape code
        let escape = reader.peek_bits(7)?;
        if escape == 0b0000011 {
            // ESCAPE code
            reader.skip_bits(7)?;
            let last = reader.read_bit()?;
            let run = reader.read_bits(6)? as u8;
            let level = reader.read_signed_bits(8)? as i16;
            return Ok((run, level, last));
        }

        // Try VLC decoding
        for bits in 2..=13 {
            if let Ok(code) = reader.peek_bits(bits) {
                if let Some(entry) = vlc::find_tcoef_entry(code, bits) {
                    reader.skip_bits(bits as usize)?;

                    // Read sign bit
                    let sign = reader.read_bit()?;
                    let level = if sign { -entry.level } else { entry.level };

                    return Ok((entry.run, level, entry.last));
                }
            }
        }

        Err(CodecError::InvalidData("Invalid TCOEF code".into()))
    }
}

impl VideoDecoder for H263Decoder {
    fn codec(&self) -> CodecId {
        CodecId::H263
    }

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

    fn receive_frame(&mut self) -> CodecResult<Option<VideoFrame>> {
        if self.pending_frames.is_empty() {
            return Ok(None);
        }

        Ok(Some(self.pending_frames.remove(0)))
    }

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

    fn reset(&mut self) {
        self.pending_frames.clear();
        self.reference_frame = None;
        self.width = None;
        self.height = None;
        self.format = None;
    }

    fn output_format(&self) -> Option<PixelFormat> {
        Some(PixelFormat::Yuv420p)
    }

    fn dimensions(&self) -> Option<(u32, u32)> {
        self.width.and_then(|w| self.height.map(|h| (w, h)))
    }
}

/// H.263 video encoder.
pub struct H263Encoder {
    /// Encoder configuration.
    config: EncoderConfig,
    /// Current frame number.
    frame_count: u64,
    /// Reference frame.
    reference_frame: Option<VideoFrame>,
    /// Motion estimator.
    motion_estimator: MotionEstimator,
    /// Current quantizer.
    quantizer: u8,
    /// Frames since last keyframe.
    frames_since_keyframe: u32,
}

impl H263Encoder {
    /// Create a new H.263 encoder.
    ///
    /// # Arguments
    ///
    /// * `config` - Encoder configuration
    ///
    /// # Errors
    ///
    /// Returns error if configuration is invalid.
    pub fn new(config: EncoderConfig) -> CodecResult<Self> {
        // Determine initial quantizer based on bitrate mode
        let quantizer = match &config.bitrate {
            crate::traits::BitrateMode::Crf(crf) => (*crf as u8).clamp(1, 31),
            crate::traits::BitrateMode::Cbr(_) => 10,
            crate::traits::BitrateMode::Vbr { .. } => 10,
            crate::traits::BitrateMode::Lossless => 1,
        };

        Ok(Self {
            config,
            frame_count: 0,
            reference_frame: None,
            motion_estimator: MotionEstimator::new(SearchAlgorithm::Diamond, 16),
            quantizer,
            frames_since_keyframe: 0,
        })
    }

    /// Encode a frame.
    fn encode_frame(&mut self, frame: &VideoFrame) -> CodecResult<EncodedPacket> {
        let is_keyframe = self.frame_count % u64::from(self.config.keyint) == 0;
        self.frames_since_keyframe = if is_keyframe {
            0
        } else {
            self.frames_since_keyframe + 1
        };

        let mut writer = BitWriter::new();

        // Write picture header
        self.write_picture_header(&mut writer, frame, is_keyframe)?;

        // Encode macroblocks
        let mb_width = ((frame.width + 15) / 16) as usize;
        let mb_height = ((frame.height + 15) / 16) as usize;

        let mut mv_predictor = MotionVectorPredictor::new(mb_width);

        for mb_y in 0..mb_height {
            for mb_x in 0..mb_width {
                self.encode_macroblock(
                    &mut writer,
                    frame,
                    &mut mv_predictor,
                    mb_x,
                    mb_y,
                    is_keyframe,
                )?;
            }
            mv_predictor.next_row();
        }

        // Store reference frame
        self.reference_frame = Some(frame.clone());
        self.frame_count += 1;

        Ok(EncodedPacket {
            data: writer.into_vec(),
            pts: frame.timestamp.pts,
            dts: frame.timestamp.pts,
            keyframe: is_keyframe,
            duration: None,
        })
    }

    /// Write picture header.
    fn write_picture_header(
        &mut self,
        writer: &mut BitWriter,
        frame: &VideoFrame,
        is_keyframe: bool,
    ) -> CodecResult<()> {
        // Picture Start Code (PSC): 22 bits (0x20)
        writer.write_bits(0x20, 22);

        // Temporal Reference (TR): 8 bits
        let tr = (self.frame_count % 256) as u8;
        writer.write_bits(u32::from(tr), 8);

        // PTYPE fields
        writer.write_bit(true); // Marker bit
        writer.write_bit(false); // Split screen off
        writer.write_bit(false); // Document camera off
        writer.write_bit(false); // Freeze picture release off

        // Source format
        let format = self.determine_format(frame.width, frame.height);
        writer.write_bits(u32::from(format.to_code()), 3);

        // Picture type (0=I, 1=P)
        writer.write_bit(!is_keyframe);

        // Optional modes (all off for baseline)
        writer.write_bit(false); // UMV off
        writer.write_bit(false); // SAC off
        writer.write_bit(false); // AP off
        writer.write_bit(false); // PB-frames off
        writer.write_bit(false); // Reserved bit

        // CPM off
        writer.write_bit(false);

        // PQUANT: 5 bits
        writer.write_bits(u32::from(self.quantizer), 5);

        // PEI: 0 (no extra information)
        writer.write_bit(false);

        Ok(())
    }

    /// Determine picture format from dimensions.
    fn determine_format(&self, width: u32, height: u32) -> PictureFormat {
        match (width, height) {
            (128, 96) => PictureFormat::SubQcif,
            (176, 144) => PictureFormat::Qcif,
            (352, 288) => PictureFormat::Cif,
            (704, 576) => PictureFormat::FourCif,
            (1408, 1152) => PictureFormat::SixteenCif,
            _ => PictureFormat::Extended,
        }
    }

    /// Encode a macroblock.
    #[allow(clippy::too_many_arguments)]
    fn encode_macroblock(
        &mut self,
        writer: &mut BitWriter,
        frame: &VideoFrame,
        mv_predictor: &mut MotionVectorPredictor,
        mb_x: usize,
        mb_y: usize,
        is_keyframe: bool,
    ) -> CodecResult<()> {
        if is_keyframe {
            // Intra macroblock
            self.encode_intra_macroblock(writer, frame, mb_x, mb_y)?;
        } else {
            // Inter macroblock
            self.encode_inter_macroblock(writer, frame, mv_predictor, mb_x, mb_y)?;
        }

        Ok(())
    }

    /// Encode intra macroblock.
    fn encode_intra_macroblock(
        &mut self,
        writer: &mut BitWriter,
        frame: &VideoFrame,
        mb_x: usize,
        mb_y: usize,
    ) -> CodecResult<()> {
        // Encode MCBPC (Intra, CBPC=0)
        if let Some(vlc) = encode_mcbpc_i(3, 0) {
            writer.write_vlc(vlc.code, vlc.bits);
        }

        // Encode CBPY (assume all blocks coded)
        if let Some(vlc) = encode_cbpy(15, true) {
            writer.write_vlc(vlc.code, vlc.bits);
        }

        // Encode blocks
        for block_idx in 0..6 {
            let block = self.extract_block(frame, mb_x, mb_y, block_idx);
            self.encode_block(writer, &block, true)?;
        }

        Ok(())
    }

    /// Encode inter macroblock.
    fn encode_inter_macroblock(
        &mut self,
        writer: &mut BitWriter,
        frame: &VideoFrame,
        mv_predictor: &mut MotionVectorPredictor,
        mb_x: usize,
        mb_y: usize,
    ) -> CodecResult<()> {
        // COD bit (1 = coded)
        writer.write_bit(true);

        // Encode MCBPC (Inter, CBPC=0)
        if let Some(vlc) = encode_mcbpc_p(0, 0) {
            writer.write_vlc(vlc.code, vlc.bits);
        }

        // Encode CBPY
        if let Some(vlc) = encode_cbpy(15, false) {
            writer.write_vlc(vlc.code, vlc.bits);
        }

        // Encode motion vector
        let mv = self.estimate_motion(frame, mb_x, mb_y);
        self.encode_motion_vector(writer, mv_predictor, mb_x, mb_y, mv)?;
        mv_predictor.update(mb_x, mv);

        // Encode residual blocks
        for block_idx in 0..6 {
            let block = self.extract_block(frame, mb_x, mb_y, block_idx);
            self.encode_block(writer, &block, false)?;
        }

        Ok(())
    }

    /// Estimate motion for macroblock.
    fn estimate_motion(&self, frame: &VideoFrame, mb_x: usize, mb_y: usize) -> MotionVector {
        if let Some(ref_frame) = &self.reference_frame {
            if !frame.planes.is_empty() && !ref_frame.planes.is_empty() {
                let cur_plane = &frame.planes[0];
                let ref_plane = &ref_frame.planes[0];

                return self.motion_estimator.estimate(
                    &cur_plane.data,
                    cur_plane.stride,
                    &ref_plane.data,
                    ref_plane.stride,
                    mb_x,
                    mb_y,
                    frame.width as usize,
                    frame.height as usize,
                );
            }
        }

        MotionVector::zero()
    }

    /// Encode motion vector.
    fn encode_motion_vector(
        &self,
        writer: &mut BitWriter,
        mv_predictor: &MotionVectorPredictor,
        mb_x: usize,
        mb_y: usize,
        mv: MotionVector,
    ) -> CodecResult<()> {
        let pred_mv = mv_predictor.predict(mb_x, mb_y);
        let mvd = mv.sub(&pred_mv);

        // Encode MVD_X
        if let Some(vlc) = encode_mvd(mvd.x as i32) {
            writer.write_vlc(vlc.code, vlc.bits);
        }

        // Encode MVD_Y
        if let Some(vlc) = encode_mvd(mvd.y as i32) {
            writer.write_vlc(vlc.code, vlc.bits);
        }

        Ok(())
    }

    /// Extract 8x8 block from frame.
    fn extract_block(
        &self,
        frame: &VideoFrame,
        mb_x: usize,
        mb_y: usize,
        block_idx: usize,
    ) -> [[i16; 8]; 8] {
        let mut block = [[0i16; 8]; 8];

        let (plane_idx, bx, by) = if block_idx < 4 {
            let bx = (block_idx % 2) * 8;
            let by = (block_idx / 2) * 8;
            (0, mb_x * 16 + bx, mb_y * 16 + by)
        } else {
            let plane_idx = block_idx - 3;
            (plane_idx, mb_x * 8, mb_y * 8)
        };

        if plane_idx < frame.planes.len() {
            let plane = &frame.planes[plane_idx];

            for y in 0..8 {
                for x in 0..8 {
                    let offset = (by + y) * plane.stride + (bx + x);
                    if offset < plane.data.len() {
                        block[y][x] = plane.data[offset] as i16;
                    }
                }
            }
        }

        block
    }

    /// Encode a block.
    fn encode_block(
        &self,
        writer: &mut BitWriter,
        block: &[[i16; 8]; 8],
        intra: bool,
    ) -> CodecResult<()> {
        // Apply DCT
        let dct_block = dct_8x8(block);

        // Quantize
        let quant_block = quantize_block(&dct_block, self.quantizer);

        // Encode DC coefficient for intra
        if intra {
            let dc = quant_block[0][0];
            // Write DC coefficient (simplified)
            writer.write_bits(3, 3); // size
            writer.write_signed_bits(dc as i32, 8);
        }

        // Encode AC coefficients using TCOEF VLC
        let mut last_idx = 63;
        for i in (0..64).rev() {
            let row = i / 8;
            let col = i % 8;
            if quant_block[row][col] != 0 {
                last_idx = i;
                break;
            }
        }

        let mut idx = if intra { 1 } else { 0 };
        let mut run = 0u8;

        while idx <= last_idx {
            let row = idx / 8;
            let col = idx % 8;
            let level = quant_block[row][col];

            if level == 0 {
                run += 1;
            } else {
                let last = idx == last_idx;
                let entry = vlc::TcoefEntry::new(run, level, last);

                if let Some(vlc) = vlc::find_tcoef_vlc(&entry) {
                    writer.write_vlc(vlc.code, vlc.bits);
                    // Write sign bit
                    writer.write_bit(level < 0);
                } else {
                    // Use ESCAPE code
                    writer.write_bits(0b0000011, 7);
                    writer.write_bit(last);
                    writer.write_bits(u32::from(run), 6);
                    writer.write_signed_bits(level as i32, 8);
                }

                if last {
                    break;
                }

                run = 0;
            }

            idx += 1;
        }

        Ok(())
    }
}

impl VideoEncoder for H263Encoder {
    fn codec(&self) -> CodecId {
        CodecId::H263
    }

    fn send_frame(&mut self, frame: &VideoFrame) -> CodecResult<()> {
        // Frames are encoded immediately in H.263
        Ok(())
    }

    fn receive_packet(&mut self) -> CodecResult<Option<EncodedPacket>> {
        // H.263 doesn't buffer frames
        Ok(None)
    }

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

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

/// 8x8 Forward DCT (Discrete Cosine Transform).
#[must_use]
fn dct_8x8(input: &[[i16; 8]; 8]) -> [[i16; 8]; 8] {
    let mut output = [[0i16; 8]; 8];
    let mut temp = [[0f32; 8]; 8];

    // 1D DCT on rows
    for i in 0..8 {
        for j in 0..8 {
            let mut sum = 0.0f32;
            for k in 0..8 {
                sum += (input[i][k] as f32)
                    * ((2 * k + 1) as f32 * j as f32 * std::f32::consts::PI / 16.0).cos();
            }
            let c = if j == 0 { 1.0 / (2.0_f32).sqrt() } else { 1.0 };
            temp[i][j] = c * sum / 2.0;
        }
    }

    // 1D DCT on columns
    for j in 0..8 {
        for i in 0..8 {
            let mut sum = 0.0f32;
            for k in 0..8 {
                sum += temp[k][j]
                    * ((2 * k + 1) as f32 * i as f32 * std::f32::consts::PI / 16.0).cos();
            }
            let c = if i == 0 { 1.0 / (2.0_f32).sqrt() } else { 1.0 };
            output[i][j] = ((c * sum / 2.0).round()) as i16;
        }
    }

    output
}

/// 8x8 Inverse DCT (IDCT).
#[must_use]
fn idct_8x8(input: &[[i16; 8]; 8]) -> [[i16; 8]; 8] {
    let mut output = [[0i16; 8]; 8];
    let mut temp = [[0f32; 8]; 8];

    // 1D IDCT on rows
    for i in 0..8 {
        for j in 0..8 {
            let mut sum = 0.0f32;
            for k in 0..8 {
                let c = if k == 0 { 1.0 / (2.0_f32).sqrt() } else { 1.0 };
                sum += c
                    * (input[i][k] as f32)
                    * ((2 * j + 1) as f32 * k as f32 * std::f32::consts::PI / 16.0).cos();
            }
            temp[i][j] = sum / 2.0;
        }
    }

    // 1D IDCT on columns
    for j in 0..8 {
        for i in 0..8 {
            let mut sum = 0.0f32;
            for k in 0..8 {
                let c = if k == 0 { 1.0 / (2.0_f32).sqrt() } else { 1.0 };
                sum += c
                    * temp[k][j]
                    * ((2 * i + 1) as f32 * k as f32 * std::f32::consts::PI / 16.0).cos();
            }
            output[i][j] = ((sum / 2.0).round().clamp(-256.0, 255.0)) as i16;
        }
    }

    output
}