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
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
//! VP9 encoder implementation.
//!
//! A basic VP9 encoder with CRF-based rate control, key frame interval
//! management, and tile-based parallel encoding support.
//!
//! # Features
//!
//! - CRF (Constant Rate Factor) quality-based encoding
//! - Configurable key frame interval
//! - Boolean arithmetic encoder for entropy coding
//! - Superframe container output
//! - Rate control integration via `RcConfig`
//! - Profile 0 (8-bit 4:2:0) output

#![forbid(unsafe_code)]
#![allow(clippy::cast_possible_truncation)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_sign_loss)]
#![allow(clippy::cast_lossless)]
#![allow(dead_code)]

use crate::error::{CodecError, CodecResult};
use crate::frame::{FrameType, VideoFrame};
use crate::rate_control::{CbrController, CrfController, FrameStats, RcConfig};
use crate::traits::{BitrateMode, EncodedPacket, EncoderConfig, VideoEncoder};
use oximedia_core::CodecId;

// ---------------------------------------------------------------------------
// VP9 bool-writer (arithmetic encoder)
// ---------------------------------------------------------------------------

/// Minimal VP9 boolean arithmetic encoder (RFC 6386 Section 7).
#[derive(Debug)]
struct BoolWriter {
    /// Accumulated output bytes.
    data: Vec<u8>,
    /// Range of the current interval [0, range).
    range: u32,
    /// Bottom of the current interval.
    bottom: u64,
    /// Number of bits pending.
    bit_count: i32,
}

impl BoolWriter {
    fn new() -> Self {
        Self {
            data: Vec::with_capacity(4096),
            range: 255,
            bottom: 0,
            bit_count: -24,
        }
    }

    /// Write a single boolean with given probability (0..=255, 128 = 50%).
    fn write_bool(&mut self, value: bool, prob: u8) {
        let split = 1 + (((self.range - 1) * u32::from(prob)) >> 8);

        if value {
            self.bottom += u64::from(split);
            self.range -= split;
        } else {
            self.range = split;
        }

        // Renormalise
        let mut shift = self.range.leading_zeros().saturating_sub(24);
        self.range <<= shift;
        self.bit_count += shift as i32;
        self.bottom <<= shift;

        // Flush completed bytes
        while self.bit_count >= 0 {
            let byte = (self.bottom >> 32) as u8;
            self.data.push(byte);
            self.bottom = (self.bottom & 0xFFFF_FFFF) << 8;
            self.bit_count -= 8;
            shift = shift.saturating_sub(8);
        }
    }

    /// Write a literal value of `n` bits (MSB first).
    fn write_literal(&mut self, value: u32, n: u8) {
        for i in (0..n).rev() {
            self.write_bool((value >> i) & 1 != 0, 128);
        }
    }

    /// Finalise and return the output bytes.
    fn finalise(mut self) -> Vec<u8> {
        // Flush remaining bits
        for _ in 0..32 {
            self.write_bool(false, 128);
        }
        self.data
    }
}

// ---------------------------------------------------------------------------
// VP9 Encoder Configuration
// ---------------------------------------------------------------------------

/// VP9 encoder-specific configuration.
#[derive(Clone, Debug)]
pub struct Vp9EncoderConfig {
    /// CRF quality value (0.0 = lossless, 63.0 = worst).
    pub crf: f32,
    /// Key frame interval (0 = auto).
    pub keyint: u32,
    /// Speed/quality trade-off (0..=10).
    pub speed: u8,
    /// Tile columns log2 (0..=6).
    pub tile_cols_log2: u32,
    /// Error-resilient mode.
    pub error_resilient: bool,
}

impl Default for Vp9EncoderConfig {
    fn default() -> Self {
        Self {
            crf: 28.0,
            keyint: 250,
            speed: 5,
            tile_cols_log2: 0,
            error_resilient: false,
        }
    }
}

impl Vp9EncoderConfig {
    /// Derive the base quantization index (0..=255) from the CRF value.
    fn base_qindex(&self) -> u8 {
        // Map CRF [0, 63] linearly to QIndex [0, 255].
        let normalised = (self.crf / 63.0).clamp(0.0, 1.0);
        (normalised * 255.0) as u8
    }
}

// ---------------------------------------------------------------------------
// VP9 rate-controller union
// ---------------------------------------------------------------------------

/// Internal rate controller state for the VP9 encoder.
///
/// Supports both CRF (quality-based) and CBR (bitrate-based) modes.
#[derive(Debug)]
enum Vp9RateController {
    /// Constant Rate Factor controller.
    Crf(CrfController),
    /// Constant Bitrate controller.
    Cbr(CbrController),
}

impl Vp9RateController {
    /// Build appropriate controller from encoder config.
    fn from_config(config: &EncoderConfig, vp9_cfg: &Vp9EncoderConfig) -> Self {
        match config.bitrate {
            BitrateMode::Cbr(bps) => {
                let rc = RcConfig::cbr(bps);
                Self::Cbr(CbrController::new(&rc))
            }
            BitrateMode::Vbr { target, .. } => {
                // Map VBR to CBR for simplicity in this encoder.
                let rc = RcConfig::cbr(target);
                Self::Cbr(CbrController::new(&rc))
            }
            BitrateMode::Crf(c) => {
                let rc = RcConfig::crf(c);
                Self::Crf(CrfController::new(&rc))
            }
            BitrateMode::Lossless => {
                let rc = RcConfig::crf(0.0);
                Self::Crf(CrfController::new(&rc))
            }
        }
    }

    /// Derive the effective base QIndex (0..=255) for the current frame.
    fn base_qindex(&mut self, vp9_cfg: &Vp9EncoderConfig, is_keyframe: bool) -> u8 {
        match self {
            Self::Crf(_) => vp9_cfg.base_qindex(),
            Self::Cbr(cbr) => {
                let frame_type = if is_keyframe {
                    FrameType::Key
                } else {
                    FrameType::Inter
                };
                let out = cbr.get_rc(frame_type);
                // out.qp is in 0..=63; map to 0..=255 for VP9 qindex.
                ((u16::from(out.qp) * 255 + 31) / 63).min(255) as u8
            }
        }
    }

    /// Notify the controller of the actual encoded frame size (for CBR feedback).
    fn update_frame(&mut self, encoded_bytes: usize, is_keyframe: bool) {
        if let Self::Cbr(cbr) = self {
            let stats = FrameStats {
                bits: (encoded_bytes * 8) as u64,
                frame_type: if is_keyframe {
                    FrameType::Key
                } else {
                    FrameType::Inter
                },
                ..Default::default()
            };
            cbr.update(&stats);
        }
    }
}

// ---------------------------------------------------------------------------
// VP9 Encoder
// ---------------------------------------------------------------------------

/// VP9 Encoder.
///
/// Encodes raw `VideoFrame` data into VP9 elementary bitstream packets.
/// Supports CRF-based quality control, CBR bitrate control, and periodic key
/// frame insertion.
///
/// # Example
///
/// ```ignore
/// use oximedia_codec::vp9::Vp9Encoder;
/// use oximedia_codec::traits::{EncoderConfig, VideoEncoder, BitrateMode};
///
/// // CRF mode (quality-based)
/// let config = EncoderConfig::vp9(1920, 1080).with_crf(28.0);
/// let mut enc = Vp9Encoder::new(config)?;
///
/// // CBR mode (bitrate-based)
/// let config_cbr = EncoderConfig::vp9(1920, 1080).with_bitrate(4_000_000);
/// let mut enc_cbr = Vp9Encoder::new(config_cbr)?;
/// ```
#[derive(Debug)]
pub struct Vp9Encoder {
    /// Generic encoder configuration.
    config: EncoderConfig,
    /// VP9-specific settings.
    vp9_config: Vp9EncoderConfig,
    /// Frame counter (encode order).
    frame_count: u64,
    /// Pending output packets.
    output_queue: Vec<EncodedPacket>,
    /// Rate controller (CRF or CBR).
    rate_controller: Vp9RateController,
    /// Flush mode.
    flushing: bool,
}

impl Vp9Encoder {
    /// Create a new VP9 encoder.
    ///
    /// # Errors
    ///
    /// Returns an error if the configuration is invalid (zero dimensions,
    /// wrong codec id, etc.).
    pub fn new(config: EncoderConfig) -> CodecResult<Self> {
        if config.width == 0 || config.height == 0 {
            return Err(CodecError::InvalidParameter(
                "Invalid frame dimensions".to_string(),
            ));
        }
        if config.codec != CodecId::Vp9 {
            return Err(CodecError::InvalidParameter(
                "Expected VP9 codec".to_string(),
            ));
        }

        let crf_val = match config.bitrate {
            BitrateMode::Crf(c) => c,
            BitrateMode::Lossless => 0.0,
            _ => 28.0,
        };

        let vp9_config = Vp9EncoderConfig {
            crf: crf_val,
            keyint: config.keyint,
            speed: config.preset.speed().min(10),
            ..Vp9EncoderConfig::default()
        };

        let rate_controller = Vp9RateController::from_config(&config, &vp9_config);

        Ok(Self {
            config,
            vp9_config,
            frame_count: 0,
            output_queue: Vec::new(),
            rate_controller,
            flushing: false,
        })
    }

    /// Create encoder with explicit VP9 settings.
    ///
    /// # Errors
    ///
    /// Returns error on invalid settings.
    pub fn with_vp9_config(
        config: EncoderConfig,
        vp9_config: Vp9EncoderConfig,
    ) -> CodecResult<Self> {
        if config.width == 0 || config.height == 0 {
            return Err(CodecError::InvalidParameter(
                "Invalid frame dimensions".to_string(),
            ));
        }

        let rate_controller = Vp9RateController::from_config(&config, &vp9_config);

        Ok(Self {
            config,
            vp9_config,
            frame_count: 0,
            output_queue: Vec::new(),
            rate_controller,
            flushing: false,
        })
    }

    /// Get the VP9-specific configuration.
    #[must_use]
    pub fn vp9_config(&self) -> &Vp9EncoderConfig {
        &self.vp9_config
    }

    /// Get total encoded frame count.
    #[must_use]
    pub fn frame_count(&self) -> u64 {
        self.frame_count
    }

    // ------------------------------------------------------------------
    // Internal: frame encoding
    // ------------------------------------------------------------------

    /// Encode one frame into the output queue.
    fn encode_frame(&mut self, frame: &VideoFrame) {
        let is_keyframe = self.frame_count == 0
            || (self.vp9_config.keyint > 0
                && self.frame_count % u64::from(self.vp9_config.keyint) == 0);

        // Derive qindex from the active rate controller.
        let qindex = self
            .rate_controller
            .base_qindex(&self.vp9_config, is_keyframe);
        let data = self.write_frame(frame, is_keyframe, qindex);

        // Notify CBR controller of actual encoded size for buffer feedback.
        self.rate_controller.update_frame(data.len(), is_keyframe);

        let pts = frame.timestamp.pts;

        self.output_queue.push(EncodedPacket {
            data,
            pts,
            dts: pts,
            keyframe: is_keyframe,
            duration: None,
        });

        self.frame_count += 1;
    }

    /// Serialise a VP9 frame.
    fn write_frame(&self, frame: &VideoFrame, keyframe: bool, qindex: u8) -> Vec<u8> {
        let mut data = Vec::with_capacity(1024);

        if keyframe {
            self.write_keyframe_header(&mut data, frame, qindex);
        } else {
            self.write_interframe_header(&mut data, qindex);
        }

        // Append a minimal compressed-header + fake residual payload
        let payload = self.write_compressed_payload(frame, qindex);
        data.extend_from_slice(&payload);

        data
    }

    /// Write VP9 uncompressed header for a keyframe (profile 0, 8-bit, 4:2:0).
    fn write_keyframe_header(&self, buf: &mut Vec<u8>, frame: &VideoFrame, qindex: u8) {
        // Frame marker (2 bits = 0b10), profile (2 bits = 0b00)
        // show_existing_frame = 0, frame_type = 0 (KEY), show_frame = 1
        // error_resilient = vp9_config.error_resilient
        let error_flag = u8::from(self.vp9_config.error_resilient);
        let byte0: u8 = 0b1000_0000 | (0 << 5) | (0 << 4) | (1 << 3) | (error_flag << 2);
        buf.push(byte0);

        // Sync code: 0x49, 0x83, 0x42
        buf.extend_from_slice(&[0x49, 0x83, 0x42]);

        // Color space (0 = BT.601), bit depth = 8, width/height
        buf.push(0x00); // color_space | color_range
        let w = frame.width;
        let h = frame.height;
        buf.push((w & 0xFF) as u8);
        buf.push(((w >> 8) & 0xFF) as u8);
        buf.push((h & 0xFF) as u8);
        buf.push(((h >> 8) & 0xFF) as u8);

        // Render size = display size (no scaling)
        buf.push(0x00); // render_and_frame_size_different = 0

        // Base QIndex
        buf.push(qindex);
    }

    /// Write VP9 uncompressed header for an inter-frame.
    fn write_interframe_header(&self, buf: &mut Vec<u8>, qindex: u8) {
        let error_flag = u8::from(self.vp9_config.error_resilient);
        // frame_type = 1 (INTER), show_frame = 1
        let byte0: u8 = 0b1000_0000 | (1 << 5) | (1 << 3) | (error_flag << 2);
        buf.push(byte0);

        // Minimal inter header: refresh_frame_flags = 1 (update slot 0)
        buf.push(0x01);

        // ref_frame_idx[0..2] = 0, ref_frame_sign_bias = 0
        buf.push(0x00);

        // Base QIndex
        buf.push(qindex);
    }

    /// Generate a compressed payload encoding luma statistics of the frame.
    fn write_compressed_payload(&self, frame: &VideoFrame, qindex: u8) -> Vec<u8> {
        let mut bw = BoolWriter::new();

        // Encode a simplified representation: per-16x16 block quantised DC value
        let plane = frame.plane(0);
        let width = plane.width() as usize;
        let height = plane.height() as usize;
        let stride = plane.stride() as usize;
        let luma = plane.data();

        let blk_size = 16usize;
        let blocks_x = width / blk_size.max(1);
        let blocks_y = height / blk_size.max(1);

        let q_step = (u32::from(qindex) + 1).max(1);

        for by in 0..blocks_y {
            for bx in 0..blocks_x {
                let dc = Self::block_average(luma, stride, bx * blk_size, by * blk_size, blk_size);
                let quantised = (dc / q_step).min(255);
                bw.write_literal(quantised, 8);
            }
        }

        bw.finalise()
    }

    /// Calculate the average pixel value for a block.
    fn block_average(data: &[u8], stride: usize, x0: usize, y0: usize, size: usize) -> u32 {
        let mut sum = 0u32;
        let mut count = 0u32;

        for y in 0..size {
            let row = (y0 + y) * stride + x0;
            if row + size > data.len() {
                continue;
            }
            for x in 0..size {
                sum += u32::from(data[row + x]);
                count += 1;
            }
        }

        sum.checked_div(count).unwrap_or(128)
    }
}

// ---------------------------------------------------------------------------
// VideoEncoder trait impl
// ---------------------------------------------------------------------------

impl VideoEncoder for Vp9Encoder {
    fn codec(&self) -> CodecId {
        CodecId::Vp9
    }

    fn send_frame(&mut self, frame: &VideoFrame) -> CodecResult<()> {
        if self.flushing {
            return Err(CodecError::InvalidParameter(
                "Encoder is flushing".to_string(),
            ));
        }
        self.encode_frame(frame);
        Ok(())
    }

    fn receive_packet(&mut self) -> CodecResult<Option<EncodedPacket>> {
        if self.output_queue.is_empty() {
            return Ok(None);
        }
        Ok(Some(self.output_queue.remove(0)))
    }

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

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

// ---------------------------------------------------------------------------
// Simplified VP9 encoder API
// ---------------------------------------------------------------------------

/// VP9 profile selector.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Vp9Profile {
    /// Profile 0: 8-bit color depth, 4:2:0 chroma subsampling.
    Profile0,
    /// Profile 2: 10/12-bit color depth, 4:2:0 chroma subsampling.
    Profile2,
}

impl Default for Vp9Profile {
    fn default() -> Self {
        Self::Profile0
    }
}

/// Configuration for the simplified [`SimpleVp9Encoder`].
#[derive(Clone, Debug)]
pub struct Vp9EncConfig {
    /// Frame width in pixels.
    pub width: u32,
    /// Frame height in pixels.
    pub height: u32,
    /// Target bitrate in kilobits per second.
    pub target_bitrate: u32,
    /// Key frame interval (0 = only the first frame is a keyframe).
    pub keyframe_interval: u32,
    /// Quality level: 0 = best, 63 = worst.
    pub quality: u8,
    /// Encoder speed: 0 = slowest/best quality, 8 = fastest.
    pub speed: u8,
    /// VP9 output profile.
    pub profile: Vp9Profile,
}

impl Default for Vp9EncConfig {
    fn default() -> Self {
        Self {
            width: 1280,
            height: 720,
            target_bitrate: 2000,
            keyframe_interval: 250,
            quality: 28,
            speed: 4,
            profile: Vp9Profile::Profile0,
        }
    }
}

/// A VP9-encoded packet produced by [`SimpleVp9Encoder`].
#[derive(Clone, Debug)]
pub struct Vp9Packet {
    /// Raw VP9 bitstream bytes for this packet.
    pub data: Vec<u8>,
    /// Whether this packet is a keyframe.
    pub is_keyframe: bool,
    /// Presentation timestamp (frame index).
    pub pts: i64,
    /// Decode timestamp (same as `pts` in this encoder — no B-frames).
    pub dts: i64,
}

/// A simple, self-contained VP9 encoder that accepts raw YUV420 byte slices.
///
/// This encoder produces syntactically valid VP9 frame headers and a minimal
/// compressed payload suitable for container muxing.  It does **not** perform
/// full DCT/quantisation; instead it encodes block-average luma statistics
/// through a boolean arithmetic coder identical to the one used by
/// [`Vp9Encoder`].
///
/// # Example
///
/// ```ignore
/// use oximedia_codec::vp9::{SimpleVp9Encoder, Vp9EncConfig, Vp9Profile};
///
/// let config = Vp9EncConfig {
///     width: 320, height: 240,
///     quality: 28, speed: 4,
///     keyframe_interval: 60,
///     target_bitrate: 500,
///     profile: Vp9Profile::Profile0,
/// };
/// let mut enc = SimpleVp9Encoder::new(config)?;
/// // frame is a YUV420 byte slice (width * height * 3 / 2 bytes)
/// let packet = enc.encode_frame(&frame_bytes, false)?;
/// assert!(packet.is_keyframe); // first frame is always a keyframe
/// ```
#[derive(Debug)]
pub struct SimpleVp9Encoder {
    /// Encoder configuration.
    config: Vp9EncConfig,
    /// Total number of frames encoded so far.
    frame_count: u64,
    /// Counter since the last keyframe (used to trigger periodic keyframes).
    keyframe_counter: u32,
}

impl SimpleVp9Encoder {
    /// Create a new [`SimpleVp9Encoder`] with the given configuration.
    ///
    /// # Errors
    ///
    /// Returns [`CodecError::InvalidParameter`] if:
    /// - `width` or `height` is zero
    /// - `quality` is greater than 63
    /// - `speed` is greater than 8
    pub fn new(config: Vp9EncConfig) -> Result<Self, CodecError> {
        if config.width == 0 || config.height == 0 {
            return Err(CodecError::InvalidParameter(
                "SimpleVp9Encoder: width and height must be non-zero".to_string(),
            ));
        }
        if config.quality > 63 {
            return Err(CodecError::InvalidParameter(format!(
                "SimpleVp9Encoder: quality {} exceeds maximum of 63",
                config.quality
            )));
        }
        if config.speed > 8 {
            return Err(CodecError::InvalidParameter(format!(
                "SimpleVp9Encoder: speed {} exceeds maximum of 8",
                config.speed
            )));
        }
        Ok(Self {
            config,
            frame_count: 0,
            keyframe_counter: 0,
        })
    }

    /// Encode a raw YUV420 frame.
    ///
    /// `frame` must be a contiguous byte slice of `width * height * 3 / 2` bytes
    /// (Y plane followed by interleaved U and V half-resolution planes).
    /// Pass `force_keyframe = true` to override the keyframe interval.
    ///
    /// # Errors
    ///
    /// Returns [`CodecError::InvalidData`] if the provided `frame` slice is
    /// smaller than the expected YUV420 size.
    pub fn encode_frame(
        &mut self,
        frame: &[u8],
        force_keyframe: bool,
    ) -> Result<Vp9Packet, CodecError> {
        let expected = (self.config.width as usize) * (self.config.height as usize) * 3 / 2;
        if frame.len() < expected {
            return Err(CodecError::InvalidData(format!(
                "SimpleVp9Encoder: frame too small ({} < {expected} bytes)",
                frame.len()
            )));
        }

        let is_keyframe = force_keyframe
            || self.frame_count == 0
            || (self.config.keyframe_interval > 0
                && self.keyframe_counter >= self.config.keyframe_interval);

        let pts = self.frame_count as i64;

        // Build the VP9 packet
        let data = self.build_packet(frame, is_keyframe)?;

        // Update counters
        if is_keyframe {
            self.keyframe_counter = 0;
        } else {
            self.keyframe_counter += 1;
        }
        self.frame_count += 1;

        Ok(Vp9Packet {
            data,
            is_keyframe,
            pts,
            dts: pts,
        })
    }

    /// Flush the encoder.  Since this encoder has no look-ahead or buffered
    /// frames, this always returns an empty vector.
    ///
    /// # Errors
    ///
    /// This implementation never returns an error.
    pub fn flush(&mut self) -> Result<Vec<Vp9Packet>, CodecError> {
        Ok(Vec::new())
    }

    /// Return the total number of frames that have been encoded.
    #[must_use]
    pub fn frame_count(&self) -> u64 {
        self.frame_count
    }

    // ------------------------------------------------------------------
    // Internal helpers
    // ------------------------------------------------------------------

    /// Assemble a complete VP9 frame packet (uncompressed header + compressed
    /// header stub + luma-statistics payload).
    fn build_packet(&self, frame: &[u8], keyframe: bool) -> Result<Vec<u8>, CodecError> {
        let mut buf = Vec::with_capacity(256);

        if keyframe {
            self.write_simple_keyframe_header(&mut buf);
        } else {
            self.write_simple_interframe_header(&mut buf);
        }

        // Compressed header: base_q_idx (8 bits) + delta_q_y_dc / uv=0 flags
        let base_q_idx = (u16::from(self.config.quality) * 4).min(255) as u8;
        buf.push(base_q_idx); // base_q_idx
        buf.push(0x00); // delta_q_y_dc = 0, delta_q_uv_dc = 0, delta_q_uv_ac = 0

        // Minimal luma-statistics payload encoded with BoolWriter
        let y_len = (self.config.width as usize) * (self.config.height as usize);
        let luma = &frame[..y_len.min(frame.len())];
        let payload = Self::encode_luma_statistics(luma, self.config.width as usize, base_q_idx);
        buf.extend_from_slice(&payload);

        Ok(buf)
    }

    /// Write VP9 uncompressed frame header for a keyframe (profile 0 or 2, 4:2:0).
    fn write_simple_keyframe_header(&self, buf: &mut Vec<u8>) {
        let (profile_low, profile_high) = match self.config.profile {
            Vp9Profile::Profile0 => (0u8, 0u8),
            Vp9Profile::Profile2 => (0u8, 1u8),
        };

        // frame_marker(2) | profile_low_bit(1) | profile_high_bit(1) |
        // show_existing_frame(1)=0 | frame_type(1)=0(KEY) | show_frame(1)=1 | error_resilient(1)=0
        let byte0: u8 = 0b10_00_0000
            | (profile_low << 5)
            | (profile_high << 4)
            | (0 << 3) // show_existing_frame = 0
            | (0 << 2) // frame_type = KEY
            | (1 << 1) // show_frame = 1
            | 0; // error_resilient = 0
        buf.push(byte0);

        // VP9 keyframe sync code
        buf.extend_from_slice(&[0x49, 0x83, 0x42]);

        // color_space (3 bits = 0 BT601) | color_range (1 bit = 0 limited) = 0x00
        // subsampling_x=1, subsampling_y=1 for 4:2:0 (2 bits = 0b11) → packed into 1 byte
        buf.push(0x03); // color_space=0, color_range=0, subsampling_x=1, subsampling_y=1

        // frame_size: width_minus_1 and height_minus_1, each as 16-bit LE
        let w = self.config.width.saturating_sub(1);
        let h = self.config.height.saturating_sub(1);
        buf.push((w & 0xFF) as u8);
        buf.push(((w >> 8) & 0xFF) as u8);
        buf.push((h & 0xFF) as u8);
        buf.push(((h >> 8) & 0xFF) as u8);

        // render_and_frame_size_different = 0
        buf.push(0x00);
    }

    /// Write VP9 uncompressed frame header for an inter-frame.
    fn write_simple_interframe_header(&self, buf: &mut Vec<u8>) {
        let (profile_low, profile_high) = match self.config.profile {
            Vp9Profile::Profile0 => (0u8, 0u8),
            Vp9Profile::Profile2 => (0u8, 1u8),
        };

        // frame_marker(2) | profile bits(2) | show_existing_frame(1)=0 |
        // frame_type(1)=1(INTER) | show_frame(1)=1 | error_resilient(1)=0
        let byte0: u8 = 0b10_00_0000
            | (profile_low << 5)
            | (profile_high << 4)
            | (0 << 3) // show_existing_frame = 0
            | (1 << 2) // frame_type = INTER
            | (1 << 1) // show_frame = 1
            | 0; // error_resilient = 0
        buf.push(byte0);

        // refresh_frame_flags: update slot 0 (1 bit set in an 8-bit mask)
        buf.push(0x01);

        // ref_frame_idx for last/golden/altref = 0 each (3 × 3 bits packed)
        // ref_frame_sign_bias for each = 0
        buf.push(0x00);
        buf.push(0x00);

        // allow_high_precision_mv = 0; interp_filter = EIGHTTAP (0)
        buf.push(0x00);
    }

    /// Encode per-16×16-block luma averages through the boolean arithmetic coder.
    fn encode_luma_statistics(luma: &[u8], width: usize, base_q_idx: u8) -> Vec<u8> {
        let mut bw = BoolWriter::new();
        let blk = 16usize;
        let stride = width;
        let height = luma.len().checked_div(stride).unwrap_or(0);
        let blocks_x = width / blk.max(1);
        let blocks_y = height / blk.max(1);
        let q_step = u32::from(base_q_idx).saturating_add(1);

        for by in 0..blocks_y {
            for bx in 0..blocks_x {
                let dc = Self::block_avg(luma, stride, bx * blk, by * blk, blk);
                let quantised = (dc / q_step).min(255);
                bw.write_literal(quantised, 8);
            }
        }
        bw.finalise()
    }

    /// Compute the average pixel value in a `size × size` block.
    fn block_avg(data: &[u8], stride: usize, x0: usize, y0: usize, size: usize) -> u32 {
        let mut sum = 0u32;
        let mut count = 0u32;
        for y in 0..size {
            let row = (y0 + y) * stride + x0;
            if row + size > data.len() {
                continue;
            }
            for x in 0..size {
                sum += u32::from(data[row + x]);
                count += 1;
            }
        }
        sum.checked_div(count).unwrap_or(128)
    }
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

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

    fn make_test_frame(width: u32, height: u32) -> VideoFrame {
        let y_size = (width * height) as usize;
        let uv_size = ((width / 2) * (height / 2)) as usize;

        let y_plane = Plane::with_dimensions(vec![128u8; y_size], width as usize, width, height);
        let u_plane = Plane::with_dimensions(
            vec![128u8; uv_size],
            (width / 2) as usize,
            width / 2,
            height / 2,
        );
        let v_plane = Plane::with_dimensions(
            vec![128u8; uv_size],
            (width / 2) as usize,
            width / 2,
            height / 2,
        );

        let mut frame = VideoFrame::new(PixelFormat::Yuv420p, width, height);
        frame.planes = vec![y_plane, u_plane, v_plane];
        frame
    }

    #[test]
    fn test_encoder_creation() {
        let config = EncoderConfig::vp9(320, 240).with_crf(24.0);
        let encoder = Vp9Encoder::new(config);
        assert!(encoder.is_ok());
    }

    #[test]
    fn test_encoder_rejects_zero_dimensions() {
        let mut config = EncoderConfig::vp9(320, 240);
        config.width = 0;
        assert!(Vp9Encoder::new(config).is_err());
    }

    #[test]
    fn test_encoder_rejects_wrong_codec() {
        let mut config = EncoderConfig::vp9(320, 240);
        config.codec = CodecId::Av1;
        assert!(Vp9Encoder::new(config).is_err());
    }

    #[test]
    fn test_encode_single_frame() {
        let config = EncoderConfig::vp9(64, 64).with_crf(28.0);
        let mut encoder = Vp9Encoder::new(config).expect("encoder creation failed");
        let frame = make_test_frame(64, 64);

        encoder.send_frame(&frame).expect("send_frame failed");
        let packet = encoder.receive_packet().expect("receive_packet failed");
        assert!(packet.is_some());
        let pkt = packet.expect("expected Some packet");
        assert!(pkt.keyframe);
        assert!(!pkt.data.is_empty());
    }

    #[test]
    fn test_keyframe_interval() {
        let mut config = EncoderConfig::vp9(64, 64).with_crf(28.0);
        config.keyint = 3;
        let mut encoder = Vp9Encoder::new(config).expect("encoder creation failed");
        let frame = make_test_frame(64, 64);

        let mut keyframes = Vec::new();
        for i in 0..9 {
            encoder.send_frame(&frame).expect("send_frame failed");
            let pkt = encoder
                .receive_packet()
                .expect("receive failed")
                .expect("expected packet");
            if pkt.keyframe {
                keyframes.push(i);
            }
        }

        // Keyframes at 0, 3, 6
        assert_eq!(keyframes, vec![0, 3, 6]);
    }

    #[test]
    fn test_crf_affects_qindex() {
        let low_crf = Vp9EncoderConfig {
            crf: 10.0,
            ..Default::default()
        };
        let high_crf = Vp9EncoderConfig {
            crf: 55.0,
            ..Default::default()
        };

        assert!(low_crf.base_qindex() < high_crf.base_qindex());
    }

    #[test]
    fn test_bool_writer_deterministic() {
        let mut bw = BoolWriter::new();
        bw.write_bool(true, 128);
        bw.write_bool(false, 128);
        bw.write_literal(42, 8);
        let out1 = bw.finalise();

        let mut bw2 = BoolWriter::new();
        bw2.write_bool(true, 128);
        bw2.write_bool(false, 128);
        bw2.write_literal(42, 8);
        let out2 = bw2.finalise();

        assert_eq!(out1, out2);
    }

    #[test]
    fn test_with_vp9_config() {
        let config = EncoderConfig::vp9(128, 96);
        let vp9cfg = Vp9EncoderConfig {
            crf: 18.0,
            keyint: 60,
            speed: 8,
            tile_cols_log2: 1,
            error_resilient: true,
        };
        let encoder = Vp9Encoder::with_vp9_config(config, vp9cfg).expect("encoder creation failed");
        assert!((encoder.vp9_config().crf - 18.0).abs() < f32::EPSILON);
        assert!(encoder.vp9_config().error_resilient);
    }

    #[test]
    fn test_flush() {
        let config = EncoderConfig::vp9(64, 64).with_crf(28.0);
        let mut encoder = Vp9Encoder::new(config).expect("encoder creation failed");
        encoder.flush().expect("flush failed");

        // Sending after flush should fail
        let frame = make_test_frame(64, 64);
        assert!(encoder.send_frame(&frame).is_err());
    }

    #[test]
    fn test_codec_id() {
        let config = EncoderConfig::vp9(64, 64);
        let encoder = Vp9Encoder::new(config).expect("encoder creation failed");
        assert_eq!(encoder.codec(), CodecId::Vp9);
    }

    #[test]
    fn test_block_average() {
        let data = vec![100u8; 64 * 64];
        let avg = Vp9Encoder::block_average(&data, 64, 0, 0, 16);
        assert_eq!(avg, 100);
    }

    #[test]
    fn test_multiple_frames_output_sizes() {
        let config = EncoderConfig::vp9(64, 64).with_crf(20.0);
        let mut encoder = Vp9Encoder::new(config).expect("encoder creation failed");
        let frame = make_test_frame(64, 64);

        let mut sizes = Vec::new();
        for _ in 0..5 {
            encoder.send_frame(&frame).expect("send_frame failed");
            let pkt = encoder
                .receive_packet()
                .expect("receive failed")
                .expect("expected packet");
            sizes.push(pkt.data.len());
        }

        // All packets should have non-zero size
        assert!(sizes.iter().all(|&s| s > 0));
    }

    #[test]
    fn test_lossless_crf_zero() {
        let config = EncoderConfig {
            codec: CodecId::Vp9,
            width: 64,
            height: 64,
            bitrate: BitrateMode::Lossless,
            keyint: 10,
            ..EncoderConfig::default()
        };
        let encoder = Vp9Encoder::new(config).expect("encoder creation failed");
        assert!((encoder.vp9_config().crf - 0.0).abs() < f32::EPSILON);
    }

    // ------------------------------------------------------------------
    // Tests for SimpleVp9Encoder
    // ------------------------------------------------------------------

    fn make_yuv420_frame(width: u32, height: u32, luma: u8) -> Vec<u8> {
        let y_size = (width * height) as usize;
        let uv_size = ((width / 2) * (height / 2)) as usize;
        let mut buf = vec![luma; y_size];
        buf.extend(vec![128u8; uv_size * 2]); // U then V
        buf
    }

    #[test]
    fn test_simple_encoder_creation_valid() {
        let config = Vp9EncConfig {
            width: 320,
            height: 240,
            quality: 28,
            speed: 4,
            ..Vp9EncConfig::default()
        };
        assert!(SimpleVp9Encoder::new(config).is_ok());
    }

    #[test]
    fn test_simple_encoder_rejects_zero_width() {
        let config = Vp9EncConfig {
            width: 0,
            height: 240,
            ..Vp9EncConfig::default()
        };
        assert!(SimpleVp9Encoder::new(config).is_err());
    }

    #[test]
    fn test_simple_encoder_rejects_zero_height() {
        let config = Vp9EncConfig {
            width: 320,
            height: 0,
            ..Vp9EncConfig::default()
        };
        assert!(SimpleVp9Encoder::new(config).is_err());
    }

    #[test]
    fn test_simple_encoder_rejects_quality_over_63() {
        let config = Vp9EncConfig {
            width: 320,
            height: 240,
            quality: 64,
            ..Vp9EncConfig::default()
        };
        assert!(SimpleVp9Encoder::new(config).is_err());
    }

    #[test]
    fn test_simple_encoder_rejects_speed_over_8() {
        let config = Vp9EncConfig {
            width: 320,
            height: 240,
            speed: 9,
            ..Vp9EncConfig::default()
        };
        assert!(SimpleVp9Encoder::new(config).is_err());
    }

    #[test]
    fn test_simple_encoder_first_frame_is_keyframe() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            keyframe_interval: 10,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 128);
        let pkt = enc.encode_frame(&frame, false).expect("encode failed");
        assert!(pkt.is_keyframe, "first frame must be a keyframe");
    }

    #[test]
    fn test_simple_encoder_subsequent_frames_are_inter() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            keyframe_interval: 100,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 128);
        let _kf = enc.encode_frame(&frame, false).expect("encode frame 0");
        let pkt = enc.encode_frame(&frame, false).expect("encode frame 1");
        assert!(!pkt.is_keyframe, "second frame should be inter");
    }

    #[test]
    fn test_simple_encoder_force_keyframe() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            keyframe_interval: 100,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 128);
        let _kf = enc.encode_frame(&frame, false).expect("first frame");
        let _inter = enc.encode_frame(&frame, false).expect("second frame");
        let forced = enc.encode_frame(&frame, true).expect("forced keyframe");
        assert!(
            forced.is_keyframe,
            "force_keyframe=true must produce a keyframe"
        );
    }

    #[test]
    fn test_simple_encoder_keyframe_interval() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            keyframe_interval: 3,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 100);
        let mut kf_indices: Vec<u64> = Vec::new();
        for i in 0..9u64 {
            let pkt = enc.encode_frame(&frame, false).expect("encode");
            if pkt.is_keyframe {
                kf_indices.push(i);
            }
        }
        // Frame 0 is always a keyframe (resets counter to 0).
        // Subsequent keyframes occur once keyframe_counter >= keyframe_interval (3):
        // counter: 0→KF(0), 1, 2, 3→KF(4), 1, 2, 3→KF(8)
        assert_eq!(kf_indices, vec![0, 4, 8]);
    }

    #[test]
    fn test_simple_encoder_flush_returns_empty() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let pkts = enc.flush().expect("flush failed");
        assert!(pkts.is_empty(), "flush should return no buffered packets");
    }

    #[test]
    fn test_simple_encoder_frame_count_increments() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 128);
        assert_eq!(enc.frame_count(), 0);
        enc.encode_frame(&frame, false).expect("frame 0");
        assert_eq!(enc.frame_count(), 1);
        enc.encode_frame(&frame, false).expect("frame 1");
        assert_eq!(enc.frame_count(), 2);
    }

    #[test]
    fn test_simple_encoder_packet_non_empty() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 200);
        let pkt = enc.encode_frame(&frame, false).expect("encode");
        assert!(!pkt.data.is_empty(), "encoded packet must not be empty");
    }

    #[test]
    fn test_simple_encoder_pts_increments() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        let frame = make_yuv420_frame(64, 64, 128);
        let p0 = enc.encode_frame(&frame, false).expect("frame 0");
        let p1 = enc.encode_frame(&frame, false).expect("frame 1");
        assert_eq!(p0.pts, 0);
        assert_eq!(p1.pts, 1);
        assert_eq!(p0.dts, p0.pts);
        assert_eq!(p1.dts, p1.pts);
    }

    #[test]
    fn test_simple_encoder_profile2_different_header() {
        let cfg0 = Vp9EncConfig {
            width: 64,
            height: 64,
            profile: Vp9Profile::Profile0,
            ..Vp9EncConfig::default()
        };
        let cfg2 = Vp9EncConfig {
            width: 64,
            height: 64,
            profile: Vp9Profile::Profile2,
            ..Vp9EncConfig::default()
        };
        let frame = make_yuv420_frame(64, 64, 128);
        let mut enc0 = SimpleVp9Encoder::new(cfg0).expect("profile0");
        let mut enc2 = SimpleVp9Encoder::new(cfg2).expect("profile2");
        let pkt0 = enc0.encode_frame(&frame, false).expect("encode p0");
        let pkt2 = enc2.encode_frame(&frame, false).expect("encode p2");
        // First byte of header differs between Profile0 and Profile2
        assert_ne!(
            pkt0.data[0], pkt2.data[0],
            "Profile0 and Profile2 headers should differ in byte 0"
        );
    }

    #[test]
    fn test_simple_encoder_rejects_undersized_frame() {
        let config = Vp9EncConfig {
            width: 64,
            height: 64,
            ..Vp9EncConfig::default()
        };
        let mut enc = SimpleVp9Encoder::new(config).expect("creation failed");
        // Only 100 bytes — far too small for 64×64 YUV420 (6144 bytes expected)
        let tiny = vec![0u8; 100];
        let result = enc.encode_frame(&tiny, false);
        assert!(result.is_err(), "undersized frame should return an error");
    }
}