1
0
Fork 0
mirror of https://github.com/alfg/mp4-rust.git synced 2024-06-02 13:39:54 +00:00

Add mp4a box

This commit is contained in:
Ian Jun 2020-07-29 22:37:23 +09:00
parent 4cb92fe8a2
commit 7cc433b63a
4 changed files with 79 additions and 24 deletions

49
Cargo.lock generated
View file

@ -1,5 +1,10 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "autocfg"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
name = "byteorder"
version = "1.3.2"
@ -10,9 +15,48 @@ name = "mp4"
version = "0.4.3"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
"num-rational 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"thiserror 1.0.20 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-bigint"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-integer"
version = "0.1.43"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-rational"
version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-bigint 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)",
"num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "num-traits"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "proc-macro2"
version = "1.0.19"
@ -63,7 +107,12 @@ version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[metadata]
"checksum autocfg 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "f8aac770f1885fd7e387acedd76065302551364496e46b3dd00860b2f8359b9d"
"checksum byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)" = "a7c3dd8985a7111efc5c80b44e23ecdd8c007de8ade3b96595387e812b957cf5"
"checksum num-bigint 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b7f3fc75e3697059fb1bc465e3d8cca6cf92f56854f201158b3f9c77d5a3cfa0"
"checksum num-integer 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "8d59457e662d541ba17869cf51cf177c0b5f0cbf476c66bdc90bf1edac4f875b"
"checksum num-rational 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)" = "a5b4d7360f362cfb50dde8143501e6940b22f644be75a4cc90b2d81968908138"
"checksum num-traits 0.2.12 (registry+https://github.com/rust-lang/crates.io-index)" = "ac267bcc07f48ee5f8935ab0d24f316fb722d7a1292e2913f0cc196b29ffd611"
"checksum proc-macro2 1.0.19 (registry+https://github.com/rust-lang/crates.io-index)" = "04f5f085b5d71e2188cb8271e5da0161ad52c3f227a661a3c135fdf28e258b12"
"checksum quote 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)" = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37"
"checksum syn 1.0.36 (registry+https://github.com/rust-lang/crates.io-index)" = "4cdb98bcb1f9d81d07b536179c269ea15999b5d14ea958196413869445bb5250"

View file

@ -70,7 +70,6 @@ impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for Avc1Box {
let header = read_box_header(reader, 0)?;
let BoxHeader{ name, size: s } = header;
println!("{:?}", header);
if name == BoxType::AvcCBox {
let avcc = AvcCBox::read_box(reader, s)?;
@ -123,6 +122,7 @@ impl<W: Write> WriteBox<&mut BufWriter<W>> for Avc1Box {
}
}
#[derive(Debug, Default, PartialEq)]
pub struct AvcCBox {
pub configuration_version: u8,

View file

@ -20,6 +20,7 @@ mod stbl;
mod stts;
mod stsd;
mod avc;
mod mp4a;
pub use ftyp::FtypBox;
pub use moov::MoovBox;
@ -78,7 +79,8 @@ boxtype!{
SmhdBox => 0x736d6864,
Avc1Box => 0x61766331,
AvcCBox => 0x61766343,
Mp4aBox => 0x6d703461
Mp4aBox => 0x6d703461,
EsdsBox => 0x65736473
}
impl fmt::Debug for BoxType {
@ -183,16 +185,13 @@ pub trait WriteBox<T>: Sized {
pub fn read_box_header_ext<R: Read>(reader: &mut BufReader<R>) -> Result<(u8, u32)> {
let version = reader.read_u8()?;
let flags_a = reader.read_u8()?;
let flags_b = reader.read_u8()?;
let flags_c = reader.read_u8()?;
let flags = u32::from(flags_a) << 16 | u32::from(flags_b) << 8 | u32::from(flags_c);
let flags = reader.read_u24::<BigEndian>()?;
Ok((version, flags))
}
pub fn write_box_header_ext<W: Write>(w: &mut BufWriter<W>, v: u8, f: u32) -> Result<u64> {
let d = u32::from(v) << 24 | f;
w.write_u32::<BigEndian>(d)?;
w.write_u8(v)?;
w.write_u24::<BigEndian>(f)?;
Ok(4)
}

View file

@ -2,7 +2,7 @@ use std::io::{BufReader, SeekFrom, Seek, Read, BufWriter, Write};
use byteorder::{BigEndian, ReadBytesExt};
use crate::*;
use crate::atoms::{avc::Avc1Box};
use crate::atoms::{avc::Avc1Box, mp4a::Mp4aBox};
#[derive(Debug)]
@ -10,6 +10,7 @@ pub struct StsdBox {
pub version: u8,
pub flags: u32,
pub avc1: Option<Avc1Box>,
pub mp4a: Option<Mp4aBox>,
}
impl Mp4Box for StsdBox {
@ -18,8 +19,13 @@ impl Mp4Box for StsdBox {
}
fn box_size(&self) -> u64 {
// TODO
0
let mut size = HEADER_SIZE + HEADER_EXT_SIZE;
if let Some(avc1) = &self.avc1 {
size += avc1.box_size();
} else if let Some(mp4a) = &self.mp4a {
size += mp4a.box_size();
}
size
}
}
@ -32,28 +38,29 @@ impl<R: Read + Seek> ReadBox<&mut BufReader<R>> for StsdBox {
let _entry_count = reader.read_u32::<BigEndian>()?;
let mut avc1 = None;
let mut start = 0u64;
while start < size {
// Get box header.
let header = read_box_header(reader, start)?;
let BoxHeader{ name, size: s } = header;
let mut mp4a = None;
match name {
BoxType::Avc1Box => {
avc1 = Some(Avc1Box::read_box(reader, s)?);
}
BoxType::Mp4aBox => {
start += s - HEADER_SIZE;
}
_ => break
// Get box header.
let header = read_box_header(reader, 0)?;
let BoxHeader{ name, size: s } = header;
match name {
BoxType::Avc1Box => {
avc1 = Some(Avc1Box::read_box(reader, s)?);
}
BoxType::Mp4aBox => {
mp4a = Some(Mp4aBox::read_box(reader, s)?);
}
_ => {}
}
skip_read(reader, current, size)?;
Ok(StsdBox {
version,
flags,
avc1,
mp4a,
})
}
}