edith
Clone
https://git.tahinli.com/Tahinli/edith.git
0 watching·0 forks·Rust·18.3 MiB·f2bee6b·Tahinli·2 months ago·781 commits
| 1 | use std::collections::HashMap; |
| 2 | use std::io::{Read, Seek}; |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use crate::meta::MetaBox; |
| 6 | use crate::*; |
| 7 | |
| 8 | #[derive(Debug)] |
| 9 | pub struct Mp4Reader<R> { |
| 10 | reader: R, |
| 11 | pub ftyp: FtypBox, |
| 12 | pub moov: MoovBox, |
| 13 | pub moofs: Vec<MoofBox>, |
| 14 | pub emsgs: Vec<EmsgBox>, |
| 15 | |
| 16 | tracks: HashMap<u32, Mp4Track>, |
| 17 | size: u64, |
| 18 | } |
| 19 | |
| 20 | impl<R: Read + Seek> Mp4Reader<R> { |
| 21 | pub fn read_header(mut reader: R, size: u64) -> Result<Self> { |
| 22 | let start = reader.stream_position()?; |
| 23 | |
| 24 | let mut ftyp = None; |
| 25 | let mut moov = None; |
| 26 | let mut moofs = Vec::new(); |
| 27 | let mut emsgs = Vec::new(); |
| 28 | |
| 29 | let mut current = start; |
| 30 | while current < size { |
| 31 | // Get box header. |
| 32 | let header = BoxHeader::read(&mut reader)?; |
| 33 | let BoxHeader { name, size: s } = header; |
| 34 | if s > size { |
| 35 | return Err(Error::InvalidData( |
| 36 | "file contains a box with a larger size than it", |
| 37 | )); |
| 38 | } |
| 39 | |
| 40 | // Break if size zero BoxHeader, which can result in dead-loop. |
| 41 | if s == 0 { |
| 42 | break; |
| 43 | } |
| 44 | |
| 45 | // Match and parse the atom boxes. |
| 46 | match name { |
| 47 | BoxType::FtypBox => { |
| 48 | ftyp = Some(FtypBox::read_box(&mut reader, s)?); |
| 49 | } |
| 50 | BoxType::FreeBox => { |
| 51 | skip_box(&mut reader, s)?; |
| 52 | } |
| 53 | BoxType::MdatBox => { |
| 54 | skip_box(&mut reader, s)?; |
| 55 | } |
| 56 | BoxType::MoovBox => { |
| 57 | moov = Some(MoovBox::read_box(&mut reader, s)?); |
| 58 | } |
| 59 | BoxType::MoofBox => { |
| 60 | let moof = MoofBox::read_box(&mut reader, s)?; |
| 61 | moofs.push(moof); |
| 62 | } |
| 63 | BoxType::EmsgBox => { |
| 64 | let emsg = EmsgBox::read_box(&mut reader, s)?; |
| 65 | emsgs.push(emsg); |
| 66 | } |
| 67 | _ => { |
| 68 | // XXX warn!() |
| 69 | skip_box(&mut reader, s)?; |
| 70 | } |
| 71 | } |
| 72 | current = reader.stream_position()?; |
| 73 | } |
| 74 | |
| 75 | if ftyp.is_none() { |
| 76 | return Err(Error::BoxNotFound(BoxType::FtypBox)); |
| 77 | } |
| 78 | if moov.is_none() { |
| 79 | return Err(Error::BoxNotFound(BoxType::MoovBox)); |
| 80 | } |
| 81 | |
| 82 | let size = current - start; |
| 83 | let mut tracks = if let Some(ref moov) = moov { |
| 84 | if moov.traks.iter().any(|trak| trak.tkhd.track_id == 0) { |
| 85 | return Err(Error::InvalidData("illegal track id 0")); |
| 86 | } |
| 87 | moov.traks |
| 88 | .iter() |
| 89 | .map(|trak| (trak.tkhd.track_id, Mp4Track::from(trak))) |
| 90 | .collect() |
| 91 | } else { |
| 92 | HashMap::new() |
| 93 | }; |
| 94 | |
| 95 | // Update tracks if any fragmented (moof) boxes are found. |
| 96 | if !moofs.is_empty() { |
| 97 | let mut default_sample_duration = 0; |
| 98 | if let Some(ref moov) = moov { |
| 99 | if let Some(ref mvex) = &moov.mvex { |
| 100 | default_sample_duration = mvex.trex.default_sample_duration |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | for moof in moofs.iter() { |
| 105 | for traf in moof.trafs.iter() { |
| 106 | let track_id = traf.tfhd.track_id; |
| 107 | if let Some(track) = tracks.get_mut(&track_id) { |
| 108 | track.default_sample_duration = default_sample_duration; |
| 109 | track.trafs.push(traf.clone()) |
| 110 | } else { |
| 111 | return Err(Error::TrakNotFound(track_id)); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | Ok(Mp4Reader { |
| 118 | reader, |
| 119 | ftyp: ftyp.unwrap(), |
| 120 | moov: moov.unwrap(), |
| 121 | moofs, |
| 122 | emsgs, |
| 123 | size, |
| 124 | tracks, |
| 125 | }) |
| 126 | } |
| 127 | |
| 128 | pub fn size(&self) -> u64 { |
| 129 | self.size |
| 130 | } |
| 131 | |
| 132 | pub fn major_brand(&self) -> &FourCC { |
| 133 | &self.ftyp.major_brand |
| 134 | } |
| 135 | |
| 136 | pub fn minor_version(&self) -> u32 { |
| 137 | self.ftyp.minor_version |
| 138 | } |
| 139 | |
| 140 | pub fn compatible_brands(&self) -> &[FourCC] { |
| 141 | &self.ftyp.compatible_brands |
| 142 | } |
| 143 | |
| 144 | pub fn duration(&self) -> Duration { |
| 145 | Duration::from_millis(self.moov.mvhd.duration * 1000 / self.moov.mvhd.timescale as u64) |
| 146 | } |
| 147 | |
| 148 | pub fn timescale(&self) -> u32 { |
| 149 | self.moov.mvhd.timescale |
| 150 | } |
| 151 | |
| 152 | pub fn is_fragmented(&self) -> bool { |
| 153 | !self.moofs.is_empty() |
| 154 | } |
| 155 | |
| 156 | pub fn tracks(&self) -> &HashMap<u32, Mp4Track> { |
| 157 | &self.tracks |
| 158 | } |
| 159 | |
| 160 | pub fn sample_count(&self, track_id: u32) -> Result<u32> { |
| 161 | if let Some(track) = self.tracks.get(&track_id) { |
| 162 | Ok(track.sample_count()) |
| 163 | } else { |
| 164 | Err(Error::TrakNotFound(track_id)) |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | pub fn read_sample(&mut self, track_id: u32, sample_id: u32) -> Result<Option<Mp4Sample>> { |
| 169 | if let Some(track) = self.tracks.get(&track_id) { |
| 170 | track.read_sample(&mut self.reader, sample_id) |
| 171 | } else { |
| 172 | Err(Error::TrakNotFound(track_id)) |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | impl<R> Mp4Reader<R> { |
| 178 | pub fn metadata(&self) -> impl Metadata<'_> { |
| 179 | self.moov.udta.as_ref().and_then(|udta| { |
| 180 | udta.meta.as_ref().and_then(|meta| match meta { |
| 181 | MetaBox::Mdir { ilst } => ilst.as_ref(), |
| 182 | _ => None, |
| 183 | }) |
| 184 | }) |
| 185 | } |
| 186 | } |