mpls 0.2.0

A movie playlist file (MPLS) parser.
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
use crate::parser::parse_mpls;
use crate::MplsError;
use std::{
    fmt::{Debug, Display},
    io::Read,
};

/// The movie playlist.
///
/// See the [crate-level docs] for high-level documentation about how to use this type.
///
/// [crate-level docs]: ../index.html
#[derive(Debug, Clone)]
pub struct Mpls {
    pub app_info_play_list: AppInfoPlayList,
    pub play_list: PlayList,
    pub marks: Vec<PlayListMark>,
    pub ext: Vec<ExtensionDataEntry>,
}

/// Represents a playlist's angle.
///
/// "Angles", as they are called, are just a variation of a playlist where one
/// or more segments are swapped out for different ones. The overall number of
/// segments, however, is always the same for all angles.
///
/// You can use the [`segments`] method to retrieve the playlist segments
/// associated with this angle.
///
/// [`segments`]: #method.segments
#[derive(Copy, Clone, Debug)]
pub struct Angle<'mpls> {
    /// The angle index in this playlist.
    pub index: u8,
    mpls: &'mpls Mpls,
}

impl<'a> Display for Angle<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.index)
    }
}

impl Mpls {
    /// Attempts to parse a movie playlist from the given reader.
    ///
    /// # Examples
    /// ```no_run
    /// # fn main() -> std::io::Result<()> {
    /// use std::fs::File;
    /// use std::io::Read;
    /// use mpls::Mpls;
    ///
    /// let mut file = File::open("00800.mpls")?;
    /// let mpls = Mpls::from(&file).expect("failed to parse MPLS file.");
    /// # Ok(())
    /// # }
    /// ```
    pub fn from<R: Read>(mut reader: R) -> Result<Mpls, MplsError> {
        let bytes = {
            let mut buffer = Vec::new();
            reader.read_to_end(&mut buffer)?;
            buffer
        };

        parse_mpls(&bytes)
            .map_err(|_| MplsError::ParseError)
            .map(|(_, m)| m)
    }

    /// Gets all of the movie's angles.
    ///
    /// This method will always return at least one element, since it counts the
    /// main feature as an angle regardless of whether the movie contains any
    /// additional angles.
    ///
    /// # Examples
    /// A playlist with four angles in total (one of them being the main feature):
    /// ```
    /// use mpls::Mpls;
    ///
    /// // let mpls = Mpls::from(...)?;
    /// # let mpls = {
    /// #     let bytes = include_bytes!("../assets/multi-angle.mpls");
    /// #     let mpls = Mpls::from(&bytes[..]).expect("failed to parse MPLS file.");
    /// #     mpls
    /// # };
    /// let angles = mpls.angles();
    /// assert_eq!(angles.len(), 4);
    /// ```
    ///
    /// A playlist that has no additional angles, only the main feature:
    /// ```
    /// use mpls::Mpls;
    ///
    /// // let mpls = Mpls::from(...)?;
    /// # let mpls = {
    /// #     let bytes = include_bytes!("../assets/simple.mpls");
    /// #     let mpls = Mpls::from(&bytes[..]).expect("failed to parse MPLS file.");
    /// #     mpls
    /// # };
    /// # let angles = mpls.angles();
    /// assert_eq!(angles.len(), 1);
    /// ```
    pub fn angles(&self) -> Vec<Angle> {
        self.play_list
            .play_items
            .iter()
            .map(|p| p.angles.len() + 1)
            .max()
            .map(|n| {
                (0..n)
                    .map(|i| Angle {
                        index: i as u8,
                        mpls: &self,
                    })
                    .collect()
            })
            .unwrap_or(Vec::new())
    }
}

impl Angle<'_> {
    /// Gets all segments for this angle.
    ///
    /// # Examples
    /// Get all clip file names (without their extension) for an angle:
    /// ```
    /// use mpls::Mpls;
    ///
    /// # let mpls = {
    /// #     let bytes = include_bytes!("../assets/simple.mpls");
    /// #     let mpls = Mpls::from(&bytes[..]).unwrap();
    /// #     mpls
    /// # };
    /// # let angle = mpls.angles()[0];
    /// let segments: Vec<&str> = angle
    ///     .segments()
    ///     .iter()
    ///     .map(|s| s.file_name.as_ref())
    ///     .collect();
    ///
    /// assert_eq!(segments, &["00055", "00059", "00061"])
    /// ```
    ///
    /// Multi-angle:
    /// ```
    /// use mpls::Mpls;
    ///
    /// // let mpls = Mpls::from(...)?;
    /// # let mpls = {
    /// #     let bytes = include_bytes!("../assets/multi-angle.mpls");
    /// #     let mpls = Mpls::from(&bytes[..]).unwrap();
    /// #     mpls
    /// # };
    /// let angles = mpls.angles();
    /// let segments: (Vec<&str>, Vec<&str>) = (
    ///     angles[0].segments().iter().map(|s| s.file_name.as_ref()).collect(),
    ///     angles[1].segments().iter().map(|s| s.file_name.as_ref()).collect());
    ///
    /// assert_eq!(&segments.0[..5], &["00081", "00082", "00086", "00087", "00091"]);
    /// assert_eq!(&segments.1[..5], &["00081", "00083", "00086", "00088", "00091"]);
    /// ```
    pub fn segments(&self) -> Vec<&Clip> {
        let play_items = &self.mpls.play_list.play_items;
        let mut clips: Vec<&Clip> = Vec::with_capacity(play_items.len());
        for play_item in play_items.iter() {
            let clip = play_item.clip_for_angle(self);
            clips.push(clip);
        }
        clips
    }
}

#[derive(Debug, Clone)]
pub struct PlayItem {
    pub clip: Clip,
    pub in_time: TimeStamp,
    pub out_time: TimeStamp,
    pub user_opt_mask: u64,
    pub angles: Vec<Clip>,
    pub angle_info: Option<AngleInfo>,
    pub stream_number_table: StreamNumberTable,
}

impl PlayItem {
    pub fn clip_for_angle(&self, angle: &Angle) -> &Clip {
        match angle.index {
            0 => &self.clip,
            i => {
                let idx = i.saturating_sub(1) as usize;
                match (&self.angles).get(idx) {
                    Some(c) => c,
                    None => &self.clip,
                }
            }
        }
    }
}

#[derive(Debug, Clone)]
pub struct SubPlayItem {
    pub clip: Clip,
    pub in_time: TimeStamp,
    pub out_time: TimeStamp,
    pub sync_play_item_id: u16,
    pub sync_start_pts: u32,
    pub multi_clip_entries: Vec<Clip>,
}

#[derive(Debug, Clone)]
pub struct ExtensionDataEntry {
    pub data_type: u16,
    pub data_version: u16,
    pub data: Vec<u8>,
}

#[derive(Debug, Clone)]
pub struct SubPath {
    pub sub_path_type: u8,
    pub is_repeat: bool,
    pub play_items: Vec<SubPlayItem>,
}

#[derive(Debug, Copy, Clone)]
pub struct AngleInfo {
    pub is_different_audios: bool,
    pub is_seamless_angle_change: bool,
}

/// A clip file, also known as a segment.
///
/// This identifies the playable stream file. `file_name` consists of 5 numbers
/// (e.g. "00055"), and `codec_id` of 4 letters which will usually be "M2TS" on
/// blu-rays.
#[derive(Debug, Clone)]
pub struct Clip {
    pub file_name: String,
    pub codec_id: String,
}

#[derive(Debug, Clone)]
pub struct PlayList {
    pub play_items: Vec<PlayItem>,
    pub sub_paths: Vec<SubPath>,
}

#[derive(Debug, Copy, Clone)]
pub struct PlayListMark {
    pub mark_type: MarkType,
    pub play_item: PlayItemRef,
    pub time_stamp: TimeStamp,
    pub duration: Option<TimeStamp>,
}

#[derive(Debug, Copy, Clone)]
pub enum MarkType {
    EntryPoint,
    LinkPoint,
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub enum PlaybackType {
    Standard,
    Random,
    Shuffle,
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub struct AppInfoPlayList {
    pub playback_type: PlaybackType,
    pub playback_count: Option<u16>,
    pub user_opt_mask: u64,
    pub flags: u16,
}

#[derive(Debug, Clone)]
pub struct StreamNumberTable {
    pub primary_video_streams: Vec<Stream>,
    pub primary_audio_streams: Vec<Stream>,
    pub primary_pgs_streams: Vec<Stream>,
    pub primary_igs_streams: Vec<Stream>,
    pub secondary_audio_streams: Vec<Stream>,
    pub secondary_video_streams: Vec<Stream>,
    pub secondary_pgs_streams: Vec<Stream>,
    pub dolby_vision_streams: Vec<Stream>,
}

/// A media stream within a [`Clip`].
///
/// [`Clip`]: struct.Clip.html
#[derive(Debug, Clone)]
pub struct Stream {
    pub entry: StreamEntry,
    pub attrs: StreamAttributes,
}

#[derive(Debug, Copy, Clone)]
pub struct StreamEntry {
    pub stream_type: u8,
    pub refs: StreamEntryRef,
}

#[derive(Debug, Copy, Clone)]
pub enum StreamEntryRef {
    PlayItem(Ref),
    SubPathKind1(Ref, Ref, Ref),
    SubPathKind2(Ref, Ref),
}

#[derive(Debug, Copy, Clone)]
pub struct SubPathRef(pub u8);

#[derive(Debug, Copy, Clone)]
pub struct SubClipRef(pub u8);

#[derive(Debug, Copy, Clone)]
pub struct PlayItemRef(pub u16);

#[derive(Debug, Copy, Clone)]
pub struct StreamRef(pub u16);

#[derive(Debug, Copy, Clone)]
pub enum Ref {
    SubPath(SubPathRef),
    SubClip(SubClipRef),
    PlayItem(PlayItemRef),
    Stream(StreamRef),
}

#[derive(Debug, Copy, Clone)]
pub enum SubPathStream {
    Type2(u8, u8, u16),
    Type3(),
}

#[derive(Debug, Clone)]
pub struct StreamAttributes {
    pub coding_type: u8,
    pub stream_type: StreamType,
}

#[derive(Debug, Clone)]
pub enum StreamType {
    SdrVideo(VideoFormat, FrameRate),
    HdrVideo(VideoFormat, FrameRate, DynamicRange, ColorSpace),
    Audio(AudioFormat, SampleRate, LanguageCode),
    Graphics(LanguageCode),
    Text(LanguageCode, CharacterCode),
    Unknown,
}

pub type LanguageCode = String;

#[derive(Debug, Copy, Clone)]
pub enum CharacterCode {
    Utf8,
    Utf16BE,
    ShiftJIS,
    EucKr,
    Gb18030,
    EucCn,
    Big5,
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub enum AudioFormat {
    Mono,
    Stereo,
    Multichannel,
    StereoAndMultichannel,
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub enum SampleRate {
    One(u32),
    Two(u32, u32),
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub enum VideoFormat {
    Interlaced480,
    Interlaced576,
    Interlaced1080,
    Progressive480,
    Progressive576,
    Progressive720,
    Progressive1080,
    Progressive2160,
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub enum DynamicRange {
    Sdr,
    Hdr10,
    DolbyVision,
    Unknown,
}

#[derive(Debug, Copy, Clone)]
pub enum ColorSpace {
    BT709,
    BT2020,
    Unknown,
}

pub type FrameRate = Option<FrameRateFraction>;

/// A video frame rate, represented as a fraction.
#[derive(Debug, Copy, Clone)]
pub struct FrameRateFraction {
    pub numerator: i32,
    pub denominator: i32,
}

impl FrameRateFraction {
    /// Returns the fraction's value as an `f64`.
    pub fn fps(&self) -> f64 {
        (self.numerator as f64) / (self.denominator as f64)
    }

    /// Returns the fraction's value as an `f32`.
    pub fn fps_single(&self) -> f32 {
        (self.numerator as f32) / (self.denominator as f32)
    }
}

/// A time stamp, relative to some System Time Clock sequence, expressed in 45 KHz.
///
/// To get a floating-point value in seconds, you can use the [`seconds`] method.
///
/// [`seconds`]: #method.seconds
#[derive(Copy, Clone)]
pub struct TimeStamp(pub u32);

impl TimeStamp {
    /// Returns this time stamp in units of seconds.
    pub fn seconds(&self) -> f64 {
        (self.0 as f64) / 45_000f64
    }
}

impl Debug for TimeStamp {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TimeStamp")
            .field("raw", &self.0)
            .field("secs", &self.seconds())
            .finish()
    }
}