Skip to main content

mp4_atom/moov/trak/mdia/minf/stbl/stsd/
jpeg.rs

1use crate::*;
2
3/// Photo - JPEG ('jpeg'), QuickTime's Motion JPEG visual sample entry.
4///
5/// Unlike most video codecs, JPEG has no separate decoder configuration box:
6/// the quantization/Huffman tables and everything else needed to decode are
7/// self-contained within each JPEG-coded sample (baseline JPEG per
8/// [ITU-T T.81](https://www.itu.int/rec/T-REC-T.81-199209-I/en) | ISO/IEC 10918-1, the
9/// core JPEG bitstream standard), so this is just a [`Visual`] sample entry
10/// plus the usual optional extension boxes.
11///
12/// `'jpeg'` itself is a QuickTime-registered codec, not part of the ISO
13/// base media file format -- see its
14/// [MP4RA registration](https://mp4ra.org/registered-types/codecs) (spec:
15/// "QT") and Apple's QuickTime documentation on the
16/// [Photo Compressor](https://developer.apple.com/Mac/library/documentation/QuickTime/RM/CompressDecompress/ImageComprMgr/E-Chapter/5CompressorsSupplied.html),
17/// which each sample's JPEG bitstream conforms to.
18#[derive(Debug, Clone, PartialEq, Eq, Default)]
19#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
20pub struct Jpeg {
21    pub visual: Visual,
22    pub btrt: Option<Btrt>,
23    pub colr: Option<Colr>,
24    pub pasp: Option<Pasp>,
25    pub fiel: Option<Fiel>,
26}
27
28impl Atom for Jpeg {
29    const KIND: FourCC = FourCC::new(b"jpeg");
30
31    fn decode_body<B: Buf>(buf: &mut B) -> Result<Self> {
32        let visual = Visual::decode(buf)?;
33
34        let mut btrt = None;
35        let mut colr = None;
36        let mut pasp = None;
37        let mut fiel = None;
38        while let Some(atom) = Any::decode_maybe(buf)? {
39            match atom {
40                Any::Btrt(atom) => btrt = atom.into(),
41                Any::Colr(atom) => colr = atom.into(),
42                Any::Pasp(atom) => pasp = atom.into(),
43                Any::Fiel(atom) => fiel = atom.into(),
44                unknown => Self::decode_unknown(&unknown)?,
45            }
46        }
47        skip_trailing_padding(buf);
48
49        Ok(Self {
50            visual,
51            btrt,
52            colr,
53            pasp,
54            fiel,
55        })
56    }
57
58    fn encode_body<B: BufMut>(&self, buf: &mut B) -> Result<()> {
59        self.visual.encode(buf)?;
60        self.btrt.encode(buf)?;
61        self.colr.encode(buf)?;
62        self.pasp.encode(buf)?;
63        self.fiel.encode(buf)?;
64
65        Ok(())
66    }
67}
68
69#[cfg(test)]
70mod tests {
71    use super::*;
72
73    #[test]
74    fn test_jpeg_roundtrip() {
75        let expected = Jpeg {
76            visual: Visual {
77                data_reference_index: 1,
78                width: 1920,
79                height: 1080,
80                compressor: "Photo - JPEG".into(),
81                ..Default::default()
82            },
83            btrt: Some(Btrt {
84                buffer_size_db: 0,
85                max_bitrate: 5_000_000,
86                avg_bitrate: 2_000_000,
87            }),
88            colr: None,
89            pasp: Some(Pasp {
90                h_spacing: 1,
91                v_spacing: 1,
92            }),
93            fiel: None,
94        };
95
96        let mut buf = Vec::new();
97        expected.encode(&mut buf).unwrap();
98
99        let decoded = Jpeg::decode(&mut buf.as_slice()).expect("failed to decode jpeg");
100        assert_eq!(decoded, expected);
101    }
102
103    #[test]
104    fn test_jpeg_minimal() {
105        // No optional boxes at all -- just the bare VisualSampleEntry.
106        let expected = Jpeg {
107            visual: Visual {
108                data_reference_index: 1,
109                width: 640,
110                height: 480,
111                ..Default::default()
112            },
113            ..Default::default()
114        };
115
116        let mut buf = Vec::new();
117        expected.encode(&mut buf).unwrap();
118
119        let decoded = Jpeg::decode(&mut buf.as_slice()).expect("failed to decode jpeg");
120        assert_eq!(decoded, expected);
121    }
122}