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
//! Support for codec parameter values
//!
//! See also,
//!  - [MDN: The "codecs" parameter in common media types](https://developer.mozilla.org/en-US/docs/Web/Media/Formats/codecs_parameter)
//!
//! ## Basic usage
//!
//! Parse a codec string,
//! ```rust
//! # use rfc6381_codec::Codec;
//! # use std::str::FromStr;
//! let codec = Codec::from_str("avc1.4D401E");
//! if let Ok(Codec::Avc1(avc1)) = codec {
//!     assert_eq!(avc1.profile(), 0x4d);
//! } else {
//!     panic!("unexpected codec type");
//! }
//! ```
//!
//! Generate a codec string,
//!
//! ```rust
//! # use rfc6381_codec::Codec;
//! let codec = Codec::avc1(0x4d, 0x40, 0x1e);
//! assert_eq!(codec.to_string(), "avc1.4D401E")
//! ```
//!
//! ## No support for 'fancy' syntax
//!
//! RFC 6381 specifies the following BNF grammar for general syntax, which this crate does not
//! yet fully support:
//!
//! ```text
//!   codecs      := cod-simple / cod-fancy
//!   cod-simple  := "codecs" "=" unencodedv
//!   unencodedv  := id-simple / simp-list
//!   simp-list   := DQUOTE id-simple *( "," id-simple ) DQUOTE
//!   id-simple   := element
//!               ; "." reserved as hierarchy delimiter
//!   element     := 1*octet-sim
//!   octet-sim   := <any TOKEN character>
//!
//!               ; Within a 'codecs' parameter value, "." is reserved
//!               ; as a hierarchy delimiter
//!   cod-fancy   := "codecs*" "=" encodedv
//!   encodedv    := fancy-sing / fancy-list
//!   fancy-sing  := [charset] "'" [language] "'" id-encoded
//!               ; Parsers MAY ignore <language>
//!               ; Parsers MAY support only US-ASCII and UTF-8
//!   fancy-list  := DQUOTE [charset] "'" [language] "'" id-list DQUOTE
//!               ; Parsers MAY ignore <language>
//!               ; Parsers MAY support only US-ASCII and UTF-8
//!   id-list     := id-encoded *( "," id-encoded )
//!   id-encoded  := encoded-elm *( "." encoded-elm )
//!               ; "." reserved as hierarchy delimiter
//!   encoded-elm := 1*octet-fancy
//!   octet-fancy := ext-octet / attribute-char
//!
//!   DQUOTE      := %x22 ; " (double quote)
//! ```
//!
//! In particular note the following productions:
//!
//!  - `cod-simple` - specifies the attribute name+value structure `codec=".."` — this crate only
//!    supports dealing with the value of this attribute (the bit inside quotes).
//!  - `cod-fancy` (and related productions `fancy-sing` / `fancy-list` etc.) — show extended
//!    structures that can optionally specify a charset for the data like `en-gb'UTF-8'%25%20xz` or `''%25%20xz` — this crate does not support values
//!    using these structures.

use four_cc::FourCC;
use mp4ra_rust::{ObjectTypeIdentifier, SampleEntryCode};
use mpeg4_audio_const::AudioObjectType;
use std::convert::TryFrom;
use std::fmt;
use std::str::FromStr;

#[derive(Debug)]
#[non_exhaustive]
pub enum Codec {
    Avc1(Avc1),
    Mp4a(Mp4a),
    Unknown(String),
}
impl Codec {
    pub fn parse_codecs(codecs: &str) -> impl Iterator<Item = Result<Codec, CodecError>> + '_ {
        codecs.split(',').map(|s| s.trim().parse())
    }

    pub fn avc1(profile: u8, constraints: u8, level: u8) -> Self {
        Codec::Avc1(Avc1 {
            profile,
            constraints,
            level,
        })
    }
}
impl FromStr for Codec {
    type Err = CodecError;

    fn from_str(codec: &str) -> Result<Codec, Self::Err> {
        if let Some(pos) = codec.find('.') {
            let (fourcc, rest) = codec.split_at(pos);
            if fourcc.len() != 4 {
                return Ok(Codec::Unknown(codec.to_string()));
            }
            let fourcc = FourCC::from(fourcc.as_bytes());
            let sample_entry = SampleEntryCode::from(fourcc);
            match sample_entry {
                SampleEntryCode::MP4A => Ok(Codec::Mp4a(get_rest(rest)?.parse()?)),
                SampleEntryCode::AVC1 => Ok(Codec::Avc1(get_rest(rest)?.parse()?)),
                _ => Ok(Codec::Unknown(codec.to_owned())),
            }
        } else {
            Err(CodecError::ExpectedHierarchySeparator(codec.to_string()))
        }
    }
}
impl fmt::Display for Codec {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), std::fmt::Error> {
        match self {
            Codec::Avc1(Avc1 {
                profile,
                constraints,
                level,
            }) => write!(f, "avc1.{:02X}{:02X}{:02X}", profile, constraints, level),
            Codec::Mp4a(mp4a) => write!(f, "mp4a.{}", mp4a),
            Codec::Unknown(val) => f.write_str(val),
        }
    }
}

fn get_rest(text: &str) -> Result<&str, CodecError> {
    if text.is_empty() {
        Ok(text)
    } else if let Some(rest) = text.strip_prefix('.') {
        Ok(rest)
    } else {
        Err(CodecError::ExpectedHierarchySeparator(text.to_string()))
    }
}

#[derive(Debug)]
pub enum CodecError {
    /// The given codec-string-component was not valid
    InvalidComponent(String),
    /// expected the '.', but instead found the text included in the variant
    ExpectedHierarchySeparator(String),
    /// The length of the given string did not match the expected length
    UnexpectedLength { expected: usize, got: String },
}

#[derive(Debug)]
pub struct Avc1 {
    profile: u8,
    constraints: u8,
    level: u8,
}
impl Avc1 {
    pub fn profile(&self) -> u8 {
        self.profile
    }
    pub fn constraints(&self) -> u8 {
        self.constraints
    }
    pub fn level(&self) -> u8 {
        self.level
    }
}
impl FromStr for Avc1 {
    type Err = CodecError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        if value.len() != 6 {
            return Err(CodecError::UnexpectedLength {
                expected: 6,
                got: value.to_string(),
            });
        }

        let profile = u8::from_str_radix(&value[0..2], 16)
            .map_err(|_| CodecError::InvalidComponent(value.to_string()))?;

        let constraints = u8::from_str_radix(&value[2..4], 16)
            .map_err(|_| CodecError::InvalidComponent(value.to_string()))?;

        let level = u8::from_str_radix(&value[4..6], 16)
            .map_err(|_| CodecError::InvalidComponent(value.to_string()))?;

        Ok(Avc1 {
            profile,
            constraints,
            level,
        })
    }
}

#[derive(Debug)]
#[non_exhaustive]
pub enum Mp4a {
    Mpeg4Audio {
        audio_object_type: Option<AudioObjectType>,
    },
    Unknown {
        object_type_indication: ObjectTypeIdentifier,
        audio_object_type_indication: Option<u8>,
    },
}
impl fmt::Display for Mp4a {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Mp4a::Mpeg4Audio { audio_object_type } => {
                write!(
                    f,
                    "{:02x}",
                    u8::from(ObjectTypeIdentifier::AUDIO_ISO_IEC_14496_3)
                )?;
                if let Some(aoti) = audio_object_type {
                    write!(f, ".{}", u8::from(*aoti))?;
                }
                Ok(())
            }
            Mp4a::Unknown {
                object_type_indication,
                audio_object_type_indication,
            } => {
                write!(f, "{:02x}", u8::from(*object_type_indication))?;
                if let Some(aoti) = audio_object_type_indication {
                    write!(f, ".{}", aoti)?;
                }
                Ok(())
            }
        }
    }
}

impl FromStr for Mp4a {
    type Err = CodecError;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        let mut i = value.splitn(2, '.');
        let s = i.next().unwrap();
        let oti =
            u8::from_str_radix(s, 16).map_err(|_| CodecError::InvalidComponent(s.to_string()))?;
        let oti = ObjectTypeIdentifier::from(oti);
        let aoti = i
            .next()
            .map(|v| u8::from_str(v))
            .transpose()
            .map_err(|e| CodecError::InvalidComponent(e.to_string()))?;
        match oti {
            ObjectTypeIdentifier::AUDIO_ISO_IEC_14496_3 => {
                let aoti = aoti
                    .map(AudioObjectType::try_from)
                    .transpose()
                    .map_err(|_e| CodecError::InvalidComponent(aoti.unwrap().to_string()))?;
                Ok(Mp4a::Mpeg4Audio {
                    audio_object_type: aoti,
                })
            }
            _ => Ok(Mp4a::Unknown {
                object_type_indication: oti,
                audio_object_type_indication: aoti,
            }),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::*;

    fn roundtrip(codec: &str) {
        assert_eq!(codec, Codec::from_str(codec).unwrap().to_string())
    }

    #[test]
    fn mp4a() {
        assert_matches!(
            Codec::from_str("mp4a.40.3"),
            Ok(Codec::Mp4a(Mp4a::Mpeg4Audio {
                audio_object_type: Some(AudioObjectType::AAC_SSR)
            }))
        );
        roundtrip("mp4a.40.3");
    }

    #[test]
    fn unknown_oti() {
        const RESERVED_X41: ObjectTypeIdentifier = ObjectTypeIdentifier(0x41);
        assert_matches!(
            Codec::from_str("mp4a.41"),
            Ok(Codec::Mp4a(Mp4a::Unknown {
                object_type_indication: RESERVED_X41,
                audio_object_type_indication: None
            }))
        );
        roundtrip("mp4a.41");
    }

    #[test]
    fn bad_oti_digit() {
        assert_matches!(Codec::from_str("mp4a.4g"), Err(_));
    }

    #[test]
    fn list() {
        let mut i = Codec::parse_codecs("mp4a.40.2,avc1.4d401e");
        assert_matches!(
            i.next().unwrap(),
            Ok(Codec::Mp4a(Mp4a::Mpeg4Audio {
                audio_object_type: Some(AudioObjectType::AAC_LC)
            }))
        );
        assert_matches!(
            i.next().unwrap(),
            Ok(Codec::Avc1(Avc1 {
                profile: 0x4d,
                constraints: 0x40,
                level: 0x1e
            }))
        );
    }

    #[test]
    fn avc1() {
        assert_matches!(
            Codec::from_str("avc1.4d401e"),
            Ok(Codec::Avc1(Avc1 {
                profile: 0x4d,
                constraints: 0x40,
                level: 0x1e
            }))
        );
        roundtrip("avc1.4D401E");
    }

    #[test]
    fn bad_avc1_lengths() {
        assert_matches!(Codec::from_str("avc1.41141"), Err(CodecError::UnexpectedLength { expected: 6, got: text }) if text == "41141");
        assert_matches!(Codec::from_str("avc1.4114134"), Err(CodecError::UnexpectedLength { expected: 6, got: text }) if text == "4114134");
    }

    #[test]
    fn unknown_fourcc() {
        assert_matches!(Codec::from_str("badd.41"), Ok(Codec::Unknown(v)) if v == "badd.41");
        roundtrip("badd.41");
    }

    #[test]
    fn invalid_unicode_boundary() {
        // byte position 4 is in the middle of a unicode codepoint - if we naively split off the
        // first 4 bytes this would panic.  We shouldn't panic, we should instead produce an Err.
        assert!(Codec::from_str("cod👍ec").is_err())
    }
}