mp4\box.mbt
100% · 68 / 68 points · 127 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
/// ============================================================================ - 2
/// ISO BMFF (MP4) box 结构解析 — 纯 MoonBit 零依赖实现。 - 3
/// ============================================================================ - 4
- 5
///| - 6
/// 4 字节 ASCII fourcc → String(非 Unicode scalar 字节以 U+FFFD 占位,保证不匹配任何已知 box 类型)。 - 7
fn fourcc(data : Bytes, offset : Int32) -> String { - 8●
String::from_array([ - 9●
data[offset].to_int().to_char().unwrap_or('\u{FFFD}'), - 10●
data[offset + 1].to_int().to_char().unwrap_or('\u{FFFD}'), - 11●
data[offset + 2].to_int().to_char().unwrap_or('\u{FFFD}'), - 12●
data[offset + 3].to_int().to_char().unwrap_or('\u{FFFD}'), - 13
]) - 14
} - 15
- 16
///| - 17
/// u32 大端读取(ISO BMFF 长度/计数字段)。 - 18
fn read_u32(data : Bytes, offset : Int32) -> Int32 { - 19●
(data[offset].to_int() << 24) | - 20●
(data[offset + 1].to_int() << 16) | - 21●
(data[offset + 2].to_int() << 8) | - 22●
data[offset + 3].to_int() - 23
} - 24
- 25
///| - 26
/// u64 大端读取(largesize / co64 偏移)。 - 27
fn read_u64(data : Bytes, offset : Int32) -> Int64 { - 28●
((data[offset].to_int() & 0xFF).to_int64() << 56) | - 29●
((data[offset + 1].to_int() & 0xFF).to_int64() << 48) | - 30●
((data[offset + 2].to_int() & 0xFF).to_int64() << 40) | - 31●
((data[offset + 3].to_int() & 0xFF).to_int64() << 32) | - 32●
((data[offset + 4].to_int() & 0xFF).to_int64() << 24) | - 33●
((data[offset + 5].to_int() & 0xFF).to_int64() << 16) | - 34●
((data[offset + 6].to_int() & 0xFF).to_int64() << 8) | - 35●
(data[offset + 7].to_int() & 0xFF).to_int64() - 36
} - 37
- 38
///| - 39
/// 解析出的 MP4 box。 - 40
pub(all) struct Mp4Box { - 41
box_type : String - 42
start : Int32 - 43
payload_start : Int32 - 44
size : Int32 - 45
} - 46
- 47
///| - 48
/// 在 [start, end) 范围内**单层遍历** box 序列,对每个 box 调用 f。 - 49
fn walk_boxes_one_level( - 50
data : Bytes, - 51
start : Int32, - 52
end : Int32, - 53
f : (Mp4Box) -> Unit, - 54
) -> Unit { - 55●
let mut off = start - 56
while off + 8 <= end { - 57●
let size = read_u32(data, off) - 58●
let ty = fourcc(data, off + 4) - 59●
let header = if size == 1 { 16 } else { 8 } - 60
if size != 0 && size != 1 && size < header { - 61●
return // 非法 size(1..7),停止遍历 - 62
} - 63
let total = if size == 1 { - 64●
let large = read_u64(data, off + 8) - 65●
if large.to_int() < header || off + large.to_int() > end { - 66●
return // 截断 - 67
} - 68●
large.to_int() - 69●
} else if size == 0 { - 70●
end - off // size==0:box 延伸到容器末尾 - 71
} else { - 72●
size - 73
} - 74
if off + total > end { - 75●
return // 截断 - 76
} - 77●
f({ box_type: ty, start: off, payload_start: off + header, size: total, }) - 78
if size == 0 { - 79●
return - 80
} - 81
off = off + total - 82
} - 83
} - 84
- 85
///| - 86
/// 在 [start, end) 单层查找指定类型的第一个 box。 - 87
fn find_box(data : Bytes, start : Int32, end : Int32, ty : String) -> Mp4Box? { - 88●
let mut result : Mp4Box? = None - 89●
walk_boxes_one_level(data, start, end, fn(b) { - 90●
if b.box_type == ty { - 91●
match result { - 92●
None => result = Some(b) - 93●
Some(_) => () - 94
} - 95
} - 96
}) - 97
result - 98
} - 99
- 100
///| - 101
/// 在 [start, end) 单层收集指定类型的全部 box(如多 trak)。 - 102
fn collect_boxes( - 103
data : Bytes, - 104
start : Int32, - 105
end : Int32, - 106
ty : String, - 107
) -> Array[Mp4Box] { - 108●
let arr : Array[Mp4Box] = [] - 109●
walk_boxes_one_level(data, start, end, fn(b) { - 110●
if b.box_type == ty { - 111●
arr.push(b) - 112
} - 113
}) - 114
arr - 115
} - 116
- 117
///| - 118
/// 绝对偏移 u32 读取。 - 119
fn payload_u32_at(data : Bytes, base : Int32, rel : Int32) -> Int32 { - 120●
read_u32(data, base + rel) - 121
} - 122
- 123
///| - 124
/// 绝对偏移 u64 读取。 - 125
fn payload_u64_at(data : Bytes, base : Int32, rel : Int32) -> Int64 { - 126●
read_u64(data, base + rel) - 127
}