image.codec\codec\avif_extended.mbt

85% · 51 / 60 points · 177 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/// # AVIF 扩展 AVIF Extended
  3. 3///
  4. 4/// AVIF 扩展功能 AVIF extended features
  5. 5
  6. 6///|
  7. 7/// AVIF 扩展信息(包含色彩空间、旋转、编码配置等)
  8. 8pub struct AvifInfoExtended {
  9. 9 width : Int32
  10. 10 height : Int32
  11. 11 bit_depth : Int32
  12. 12 num_channels : Int32
  13. 13 has_alpha : Bool
  14. 14 rotation : Int32
  15. 15 mirror : Int32
  16. 16 color_type : String
  17. 17 color_primaries : Int32
  18. 18 transfer_characteristics : Int32
  19. 19 matrix_coefficients : Int32
  20. 20 full_range : Bool
  21. 21 av1_profile : Int32
  22. 22 av1_level : Int32
  23. 23 av1_tier : Int32
  24. 24 av1_monochrome : Bool
  25. 25}
  26. 26
  27. 27///|
  28. 28fn read_4cc(data : Bytes, offset : Int32) -> String {
  29. 29● let b0 = data[offset].to_int()
  30. 30● let b1 = data[offset + 1].to_int()
  31. 31● let b2 = data[offset + 2].to_int()
  32. 32● let b3 = data[offset + 3].to_int()
  33. 33 let s : Array[Int32] = [b0, b1, b2, b3]
  34. 34 let buf : Array[String] = []
  35. 35 for c in s {
  36. 36● match c.to_char() {
  37. 37● Some(ch) => buf.push(ch.to_string())
  38. 38○ None => buf.push("?")
  39. 39 }
  40. 40 }
  41. 41 buf[0] + buf[1] + buf[2] + buf[3]
  42. 42}
  43. 43
  44. 44///|
  45. 45fn box_type_str(data : Bytes, offset : Int32) -> String {
  46. 46● read_4cc(data, offset + 4)
  47. 47}
  48. 48
  49. 49///|
  50. 50/// 解析 AVIF 扩展头部信息
  51. 51///
  52. 52/// 递归遍历 ISOBMFF box 结构,提取色彩空间、旋转、镜像、AV1 编码配置等
  53. 53pub fn avif_info_extended(
  54. 54 data : Bytes,
  55. 55) -> AvifInfoExtended raise @types.LoadError {
  56. 56● if !is_avif_format(data) {
  57. 57● raise @types.LoadError::UnsupportedFormat("Not an AVIF file")
  58. 58 }
  59. 59● let len = data.length()
  60. 60 let mut width = 0
  61. 61 let mut height = 0
  62. 62 let mut bit_depth = 8
  63. 63 let mut has_alpha = false
  64. 64 let mut rotation = 0
  65. 65 let mut mirror = 0
  66. 66 let mut color_type = "unknown"
  67. 67 let mut color_primaries = 0
  68. 68 let mut transfer_characteristics = 0
  69. 69 let mut matrix_coefficients = 0
  70. 70 let mut full_range = false
  71. 71 let mut av1_profile = 0
  72. 72 let mut av1_level = 0
  73. 73 let mut av1_tier = 0
  74. 74 let mut av1_monochrome = false
  75. 75 // 递归遍历所有 box
  76. 76● scan_boxes(data, 0, len, fn(box_off, _box_size, box_type) {
  77. 77● if box_type == "ispe" && box_off + 20 <= len {
  78. 78● width = read_u32_be(data, box_off + 12)
  79. 79● height = read_u32_be(data, box_off + 16)
  80. 80 }
  81. 81 if box_type == "auxC" {
  82. 82● has_alpha = true
  83. 83 }
  84. 84 if box_type == "pixi" && box_off + 13 <= len {
  85. 85● let num_ch = data[box_off + 12].to_int()
  86. 86 if num_ch > 0 && box_off + 13 + num_ch <= len {
  87. 87● bit_depth = data[box_off + 13].to_int()
  88. 88 }
  89. 89 }
  90. 90 if box_type == "irot" && box_off + 9 <= len {
  91. 91● let angle_bits = data[box_off + 8].to_int() & 0x03
  92. 92 rotation = angle_bits * 90
  93. 93 }
  94. 94 if box_type == "imir" && box_off + 9 <= len {
  95. 95● mirror = data[box_off + 8].to_int() & 0x01
  96. 96 }
  97. 97 if box_type == "colr" && box_off + 16 <= len {
  98. 98● let ctype = read_4cc(data, box_off + 8)
  99. 99 if ctype == "nclx" && box_off + 19 <= len {
  100. 100● color_type = "nclx"
  101. 101● color_primaries = read_u16_be(data, box_off + 12)
  102. 102● transfer_characteristics = read_u16_be(data, box_off + 14)
  103. 103● matrix_coefficients = read_u16_be(data, box_off + 16)
  104. 104● full_range = (data[box_off + 18].to_int() & 0x80) != 0
  105. 105○ } else if ctype == "rICC" {
  106. 106○ color_type = "rICC"
  107. 107○ } else if ctype == "prof" {
  108. 108○ color_type = "prof"
  109. 109 }
  110. 110 }
  111. 111 if box_type == "av1C" && box_off + 12 <= len {
  112. 112● let b1 = data[box_off + 9].to_int()
  113. 113 av1_profile = (b1 >> 5) & 0x07
  114. 114 av1_level = b1 & 0x1F
  115. 115 if box_off + 13 <= len {
  116. 116○ let b2 = data[box_off + 10].to_int()
  117. 117 av1_tier = (b2 >> 7) & 0x01
  118. 118 av1_monochrome = ((b2 >> 4) & 0x01) == 1
  119. 119 }
  120. 120 }
  121. 121 })
  122. 122 if width == 0 || height == 0 {
  123. 123● raise @types.LoadError::DecodeFailed(
  124. 124 "AVIF: could not determine image dimensions",
  125. 125 )
  126. 126 }
  127. 127● let num_channels = if has_alpha { 4 } else { 3 }
  128. 128 AvifInfoExtended::{
  129. 129 width,
  130. 130 height,
  131. 131 bit_depth,
  132. 132 num_channels,
  133. 133 has_alpha,
  134. 134 rotation,
  135. 135 mirror,
  136. 136 color_type,
  137. 137 color_primaries,
  138. 138 transfer_characteristics,
  139. 139 matrix_coefficients,
  140. 140 full_range,
  141. 141 av1_profile,
  142. 142 av1_level,
  143. 143 av1_tier,
  144. 144 av1_monochrome,
  145. 145 }
  146. 146}
  147. 147
  148. 148///|
  149. 149/// 递归扫描 ISOBMFF box 结构
  150. 150fn scan_boxes(
  151. 151 data : Bytes,
  152. 152 start : Int32,
  153. 153 end : Int32,
  154. 154 callback : (Int32, Int32, String) -> Unit,
  155. 155) -> Unit {
  156. 156● let mut offset = start
  157. 157 while offset + 8 <= end {
  158. 158● let box_size = read_u32_be(data, offset)
  159. 159 if box_size < 8 || box_size > end - offset {
  160. 160○ break
  161. 161 }
  162. 162● let btype = box_type_str(data, offset)
  163. 163● callback(offset, box_size, btype)
  164. 164 // 递归进入容器 box: meta, iprp, ipco, moov, trak, mdia
  165. 165 if btype == "meta" ||
  166. 166 btype == "iprp" ||
  167. 167 btype == "ipco" ||
  168. 168 btype == "moov" ||
  169. 169 btype == "trak" ||
  170. 170 btype == "mdia" {
  171. 171 // full box: skip version(4) + flags for meta
  172. 172● let child_start = if btype == "meta" { offset + 12 } else { offset + 8 }
  173. 173● scan_boxes(data, child_start, offset + box_size, callback)
  174. 174 }
  175. 175 offset += box_size
  176. 176 }
  177. 177}