Skip to main content

mlt_core/frames/v01/stream/
encoder.rs

1use crate::v01::{DataProfile, LogicalEncoder, PhysicalEncoder};
2
3#[derive(Debug, Eq, PartialEq, Clone, Copy)]
4#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
5#[cfg_attr(all(not(test), feature = "arbitrary"), derive(arbitrary::Arbitrary))]
6pub struct IntEncoder {
7    pub(crate) logical: LogicalEncoder,
8    pub(crate) physical: PhysicalEncoder,
9}
10
11impl IntEncoder {
12    #[must_use]
13    pub const fn new(logical: LogicalEncoder, physical: PhysicalEncoder) -> Self {
14        Self { logical, physical }
15    }
16
17    #[must_use]
18    pub fn delta_fastpfor() -> IntEncoder {
19        IntEncoder::new(LogicalEncoder::Delta, PhysicalEncoder::FastPFOR)
20    }
21    #[must_use]
22    pub fn delta_rle_fastpfor() -> IntEncoder {
23        IntEncoder::new(LogicalEncoder::DeltaRle, PhysicalEncoder::FastPFOR)
24    }
25    #[must_use]
26    pub fn delta_rle_varint() -> IntEncoder {
27        IntEncoder::new(LogicalEncoder::DeltaRle, PhysicalEncoder::VarInt)
28    }
29    #[must_use]
30    pub fn delta_varint() -> IntEncoder {
31        IntEncoder::new(LogicalEncoder::Delta, PhysicalEncoder::VarInt)
32    }
33    #[must_use]
34    pub fn fastpfor() -> IntEncoder {
35        IntEncoder::new(LogicalEncoder::None, PhysicalEncoder::FastPFOR)
36    }
37    #[must_use]
38    pub fn plain() -> IntEncoder {
39        IntEncoder::new(LogicalEncoder::None, PhysicalEncoder::None)
40    }
41    #[must_use]
42    pub fn rle_fastpfor() -> IntEncoder {
43        IntEncoder::new(LogicalEncoder::Rle, PhysicalEncoder::FastPFOR)
44    }
45    #[must_use]
46    pub fn rle_varint() -> IntEncoder {
47        IntEncoder::new(LogicalEncoder::Rle, PhysicalEncoder::VarInt)
48    }
49    #[must_use]
50    pub fn varint() -> IntEncoder {
51        IntEncoder::new(LogicalEncoder::None, PhysicalEncoder::VarInt)
52    }
53
54    /// Automatically select the best encoder for a `u32` stream.
55    ///
56    /// Uses the `BTRBlocks` strategy:
57    /// - profile a small sample of the data to prune unsuitable candidates,
58    /// - then encode the same sample with all survivors and
59    /// - return the encoder that produces the smallest output.
60    ///
61    /// `FastPFOR` is always preferred over `VarInt` when sizes are equal.
62    #[must_use]
63    pub fn auto_u32(values: &[u32]) -> IntEncoder {
64        let enc = DataProfile::prune_candidates::<i32>(values);
65        DataProfile::compete_u32(&enc, values)
66    }
67
68    /// Automatically select the best encoder for a `u64` stream.
69    #[must_use]
70    pub fn auto_u64(values: &[u64]) -> IntEncoder {
71        let enc = DataProfile::prune_candidates::<i64>(values);
72        DataProfile::compete_u64(&enc, values)
73    }
74}
75
76#[derive(Debug, Eq, PartialEq, Clone, Copy)]
77#[cfg_attr(test, derive(proptest_derive::Arbitrary))]
78#[cfg_attr(all(not(test), feature = "arbitrary"), derive(arbitrary::Arbitrary))]
79pub struct FsstStrEncoder {
80    pub(crate) symbol_lengths: IntEncoder,
81    pub(crate) dict_lengths: IntEncoder,
82}