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
use std::convert::{TryFrom, TryInto};

use ebml_iterable::tools::{self as ebml_tools, Vint};

use super::super::errors::WebmCoercionError;
use super::MatroskaSpec;

///
/// An enum describing different block lacing options.
///
/// This enum is based on the definition for [Lacing](https://www.matroska.org/technical/basics.html#lacing) as defined by the [Matroska Spec](http://www.matroska.org/technical/specs/index.html).
///
#[derive(PartialEq, Copy, Clone, Debug)]
pub enum BlockLacing {
    Xiph,
    Ebml,
    FixedSize,
}

///
/// A single frame of data within a block.
/// 
/// There may be a single frame or multiple frames within a "Block" or "SimpleBlock".  If only one frame is present, "BlockLacing" must be None.  If more than one frame is present, "BlockLacing" must be one of: Xiph, Ebml, FixedSize.
/// 
#[derive(Clone, Debug)]
pub struct Frame {
    pub data: Vec<u8>
}

///
/// A typed interpretation of the Matroska "Block" element.
///
/// This struct has fields specific to the [Block](https://www.matroska.org/technical/basics.html#block-structure) element as defined by the [Matroska Spec](http://www.matroska.org/technical/specs/index.html).  This struct implements `TryFrom<MatroskaSpec>` and `Into<MatroskaSpec>` to simplify coercion to and from regular variants.
///
/// ## Example
///
/// ```
/// # use std::convert::TryInto;
/// use webm_iterable::matroska_spec::{MatroskaSpec, Block};
///
/// let variant = MatroskaSpec::Block(vec![0x83,0x00,0x01,0x9d,0x00,0x00,0x00]);
/// let mut block: Block = variant.try_into().unwrap();
/// assert_eq!(3, block.track);
/// ```
///
#[derive(Clone, Debug)]
pub struct Block {
    pub frames: Vec<Frame>,
    pub track: u64,
    pub timestamp: i16,

    pub invisible: bool,
    pub lacing: Option<BlockLacing>,
}

impl TryFrom<&[u8]> for Block {
    type Error = WebmCoercionError;

    fn try_from(data: &[u8]) -> Result<Self, Self::Error> {
        let mut position: usize = 0;
        let (track, track_size) = ebml_tools::read_vint(data)
            .map_err(|_| WebmCoercionError::BlockCoercionError(String::from("Unable to read track data in Block.")))?
            .ok_or_else(|| WebmCoercionError::BlockCoercionError(String::from("Unable to read track data in Block.")))?;

        position += track_size;

        let value: [u8; 2] = data[position..position + 2].try_into()
            .map_err(|_| WebmCoercionError::BlockCoercionError(String::from("Attempting to create Block tag, but binary data length was not 2")))?;
        let timestamp = i16::from_be_bytes(value);
        position += 2;

        let flags: u8 = data[position];
        position += 1;
        let invisible = (flags & 0x08) == 0x08;

        let lacing: Option<BlockLacing>;
        if flags & 0x06 == 0x06 {
            lacing = Some(BlockLacing::Ebml);
        } else if flags & 0x06 == 0x04 {
            lacing = Some(BlockLacing::FixedSize);
        } else if flags & 0x06 == 0x02 {
            lacing = Some(BlockLacing::Xiph);
        } else {
            lacing = None;
        }

        let payload = &data[position..];

        Ok(Block {
            frames: get_block_frames(payload, lacing)?,
            track,
            timestamp,
            invisible,
            lacing,
        })
    }
}

fn get_block_frames(payload: &[u8], lacing: Option<BlockLacing>) -> Result<Vec<Frame>, WebmCoercionError> {
    if let Some(lacing) = lacing {
        let frame_count = payload[0] as usize + 1;
        let (mut frame_start, sizes) = match lacing {
            BlockLacing::Xiph => {
                let mut sizes: Vec<usize> = Vec::with_capacity(frame_count-1);
                let mut position: usize = 1;
                let mut size = 0;
                while sizes.len() < frame_count - 1 {
                    size += payload[position] as usize;
                    if payload[position] != 0xFF {
                        sizes.push(size);
                        size = 0;
                    }
                    position += 1;
                }
                Ok((position, sizes))
            },
            BlockLacing::Ebml => {
                let mut sizes: Vec<usize> = Vec::with_capacity(frame_count-1);
                let mut position: usize = 1;
                while sizes.len() < frame_count - 1 {
                    if let Some((val, val_len)) = ebml_tools::read_vint(&payload[position..]).ok().flatten() {
                        if let Some(last) = sizes.last() {
                            // This reads the value in two's complement notation like the spec describes
                            // let difference = if val > ((1 << ((7 * val_len) - 1)) - 1) {
                            //     val | !((1 << (7 * val_len)) - 1)
                            // } else {
                            //     val
                            // } as i64;

                            // But the spec example just subtracts half the range like this
                            let difference = (val as i64) - ((1 << ((7 * val_len) - 1)) - 1);

                            // I've opened up an issue with the specification: https://github.com/ietf-wg-cellar/matroska-specification/issues/726
                            // In the mean time, example files with EBML Lacing seem to use the "subtract half range" approach, so we'll make
                            // the assumption to go with that until there's an update otherwise.

                            sizes.push((difference + (*last as i64)) as usize);
                        } else {
                            sizes.push(val as usize);
                        }
                        position += val_len;
                    } else {
                        return Err(WebmCoercionError::BlockCoercionError(String::from("Unable to read ebml lacing frame sizes in block")));
                    }
                }

                Ok((position, sizes))
            },
            BlockLacing::FixedSize => {
                let total_size = payload.len() - 1;
                if total_size % frame_count == 0 {
                    let frame_size = total_size / frame_count;
                    Ok((1usize, vec![frame_size; frame_count - 1]))
                } else {
                    Err(WebmCoercionError::BlockCoercionError(String::from("Block frame count with fixed lacing size did not match payload length")))
                }
            }
        }?;

        let mut frames: Vec<Frame> = Vec::with_capacity(frame_count);
        for size in sizes {
            frames.push(Frame { data: payload[frame_start..(frame_start+size)].to_vec() });
            frame_start += size;
        }
        frames.push(Frame { data: payload[frame_start..].to_vec() });

        Ok(frames)
    } else {
        Ok(vec![Frame { data: payload.to_vec() }])
    }
}

impl TryFrom<MatroskaSpec> for Block {
    type Error = WebmCoercionError;

    fn try_from(value: MatroskaSpec) -> Result<Self, Self::Error> {
        match value {
            MatroskaSpec::Block(data) => {
                let data: &[u8] = &data;
                Block::try_from(data)
            }
            _ => Err(WebmCoercionError::BlockCoercionError(String::from("Expected binary tag type for Block tag, but received a different type!"))),
        }
    }
}

impl From<Block> for MatroskaSpec {
    fn from(mut block: Block) -> Self {
        if block.frames.len() == 1 {
            // If there is only 1 frame, lacing doesn't apply
            block.lacing = None;
        } else if block.lacing.is_none() {
            // If there is more than 1 frame and lacing is not set, default to Ebml lacing
            block.lacing = Some(BlockLacing::Ebml);
        }

        let mut flags: u8 = 0x00;
        if block.invisible {
            flags |= 0x08;
        }

        if block.lacing.is_some() {
            match block.lacing.unwrap() {
                BlockLacing::Xiph => {
                    flags |= 0x02;
                }
                BlockLacing::Ebml => {
                    flags |= 0x06;
                }
                BlockLacing::FixedSize => {
                    flags |= 0x04;
                }
            }
        }

        let payload = build_frame_payload(block.frames, block.lacing);

        let mut result = Vec::with_capacity(payload.len() + 11);
        result.extend_from_slice(&block.track.as_vint().expect("Unable to convert track value to vint"));
        result.extend_from_slice(&block.timestamp.to_be_bytes());
        result.extend_from_slice(&flags.to_be_bytes());
        result.extend_from_slice(&payload);

        MatroskaSpec::Block(result)
    }
}

pub fn build_frame_payload(frames: Vec<Frame>, lacing: Option<BlockLacing>) -> Vec<u8> {
    if let Some(lacing) = lacing {
        let sizes = match lacing {
            BlockLacing::Xiph => {
                let mut sizes: Vec<u8> = Vec::new();
                for frame in &frames[..frames.len()-1] {
                    sizes.resize(sizes.len() + frame.data.len()/255, 0xFF);
                    sizes.push((frame.data.len()%255) as u8);
                }
                sizes
            },
            BlockLacing::Ebml => {
                let mut last_size: Option<usize> = None;
                let mut sizes: Vec<u8> = Vec::new();
                for frame in &frames[..frames.len()-1] {
                    let size = frame.data.len();
                    let written_size = if let Some(last_size) = last_size {
                        // Just like the issue in parsing EBML lacing, this writes the value in two's complement notation like the spec describes
                        // let mut diff = (size as i64) - (last_size as i64);
                        // if diff < 0 {
                        //     let mut length: usize = 1;
                        //     while length <= 8 {
                        //         if diff > -(1 << ((7 * length) - 1)) {
                        //             break;
                        //         }
                        //         length += 1;
                        //     }
                        //     diff &= (1 << (7 * length)) - 1;

                        // }
                        // diff as u64

                        // But the spec example would be to just add half the range like this
                        let diff = (size as i64) - (last_size as i64);
                        let mut length: usize = 1;
                        while length <= 8 {
                            if diff > -(1 << ((7 * length) - 1)) && diff < (1 << ((7 * length) - 1)) {
                                break;
                            }
                            length += 1;
                        }
                        (diff + (1 << ((7 * length) - 1)) - 1) as u64
                    } else {
                        size as u64
                    };
                    sizes.append(&mut written_size.as_vint().unwrap());
                    last_size = Some(size);
                }
                sizes
            },
            BlockLacing::FixedSize => {
                vec![]
            }
        };

        let mut payload: Vec<u8> = Vec::with_capacity(1 + sizes.len() + frames.iter().fold(0, |a, c| a + c.data.len()));

        payload.push((frames.len()-1) as u8);
        payload.extend_from_slice(sizes.as_slice());
        for frame in frames {
            payload.extend_from_slice(frame.data.as_slice());
        }

        payload
    } else {
        frames[0].data.clone()
    }
}