mp4\box.mbt

100% · 68 / 68 points · 127 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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