image.codec\codec\avif_decode.mbt

100% · 37 / 37 points · 142 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 Decode
  3. 3///
  4. 4/// AVIF 图像格式解码 AVIF image format decoding
  5. 5
  6. 6///|
  7. 7/// AVIF (AV1 Image File Format) 解码器
  8. 8///
  9. 9/// AVIF 基于 AV1 编码,使用 ISOBMFF 容器格式。
  10. 10/// 本模块提供格式检测和基本头部解析,完整解码需 AV1 解码器支持。
  11. 11///
  12. 12/// Magic bytes: 0x00 0x00 0x00 0x?? 0x66 0x74 0x79 0x70 0x61 0x76 0x69 0x66(ftyp box,brand 为 "avif" 或 "avis")
  13. 13
  14. 14///|
  15. 15/// AVIF 图像信息
  16. 16pub struct AvifInfo {
  17. 17 width : Int32
  18. 18 height : Int32
  19. 19 bit_depth : Int32
  20. 20 num_channels : Int32
  21. 21 has_alpha : Bool
  22. 22}
  23. 23
  24. 24///|
  25. 25/// 检测数据是否为 AVIF 格式
  26. 26///
  27. 27/// AVIF 文件以 ftyp box 开头,brand 为 "avif" 或 "avis"
  28. 28pub fn is_avif_format(data : Bytes) -> Bool {
  29. 29● let len = data.length()
  30. 30 if len < 12 {
  31. 31● return false
  32. 32 }
  33. 33 // 检查 ftyp box: bytes 4-7 = "ftyp"
  34. 34 if data[4] != b'f' || data[5] != b't' || data[6] != b'y' || data[7] != b'p' {
  35. 35● return false
  36. 36 }
  37. 37 // 检查 brand: bytes 8-11 = "avif" or "avis"
  38. 38 let b8 = data[8]
  39. 39 let b9 = data[9]
  40. 40 let b10 = data[10]
  41. 41 let b11 = data[11]
  42. 42 // "avif" = 0x61 0x76 0x69 0x66
  43. 43 if b8 == b'a' && b9 == b'v' && b10 == b'i' && b11 == b'f' {
  44. 44● return true
  45. 45 }
  46. 46 // "avis" = 0x61 0x76 0x69 0x73
  47. 47 if b8 == b'a' && b9 == b'v' && b10 == b'i' && b11 == b's' {
  48. 48● return true
  49. 49 }
  50. 50 false
  51. 51}
  52. 52
  53. 53///|
  54. 54/// 解析 AVIF 头部信息
  55. 55///
  56. 56/// 解析 ISOBMFF box 结构,提取图像尺寸和位深信息。
  57. 57/// 注意:完整解析需要遍历所有 box,此处仅解析基本元数据。
  58. 58pub fn avif_info(data : Bytes) -> AvifInfo raise @types.LoadError {
  59. 59● if !is_avif_format(data) {
  60. 60● raise @types.LoadError::UnsupportedFormat("Not an AVIF file")
  61. 61 }
  62. 62● let len = data.length()
  63. 63 let mut offset = 0
  64. 64 let mut width = 0
  65. 65 let mut height = 0
  66. 66 let mut bit_depth = 8
  67. 67 let mut has_alpha = false
  68. 68 // 遍历 ISOBMFF boxes
  69. 69 while offset + 8 <= len {
  70. 70● let box_size = (data[offset].to_int() << 24) |
  71. 71● (data[offset + 1].to_int() << 16) |
  72. 72● (data[offset + 2].to_int() << 8) |
  73. 73● data[offset + 3].to_int()
  74. 74 if box_size < 8 || box_size > len - offset {
  75. 75● break
  76. 76 }
  77. 77 let box_type_0 = data[offset + 4]
  78. 78 let box_type_1 = data[offset + 5]
  79. 79 let box_type_2 = data[offset + 6]
  80. 80 let box_type_3 = data[offset + 7]
  81. 81 // 检查 ispe box (ImageSpatialExtentsProperty)
  82. 82 // box type "ispe" = 0x69 0x73 0x70 0x65
  83. 83 if box_type_0 == b'i' &&
  84. 84 box_type_1 == b's' &&
  85. 85 box_type_2 == b'p' &&
  86. 86 box_type_3 == b'e' {
  87. 87 // ispe box: full_box_header(8) + version(4) + width(4) + height(4)
  88. 88● if offset + 20 <= len {
  89. 89● width = (data[offset + 12].to_int() << 24) |
  90. 90● (data[offset + 13].to_int() << 16) |
  91. 91● (data[offset + 14].to_int() << 8) |
  92. 92● data[offset + 15].to_int()
  93. 93● height = (data[offset + 16].to_int() << 24) |
  94. 94● (data[offset + 17].to_int() << 16) |
  95. 95● (data[offset + 18].to_int() << 8) |
  96. 96● data[offset + 19].to_int()
  97. 97 }
  98. 98 }
  99. 99 // 检查 auxC box (AuxiliaryTypeProperty) for alpha
  100. 100 // box type "auxC" = 0x61 0x75 0x78 0x43
  101. 101 if box_type_0 == b'a' &&
  102. 102 box_type_1 == b'u' &&
  103. 103 box_type_2 == b'x' &&
  104. 104 box_type_3 == b'C' {
  105. 105● has_alpha = true
  106. 106 }
  107. 107 // 检查 pixi box (PixelInformationProperty) for bit depth
  108. 108 // box type "pixi" = 0x70 0x69 0x78 0x69
  109. 109 if box_type_0 == b'p' &&
  110. 110 box_type_1 == b'i' &&
  111. 111 box_type_2 == b'x' &&
  112. 112 box_type_3 == b'i' {
  113. 113 // pixi box: full_box_header(8) + version(4) + num_channels(1) + bit_depths(...)
  114. 114● if offset + 13 <= len {
  115. 115● let num_ch = data[offset + 12].to_int()
  116. 116 if num_ch > 0 && offset + 13 + num_ch <= len {
  117. 117● bit_depth = data[offset + 13].to_int()
  118. 118 }
  119. 119 }
  120. 120 }
  121. 121 offset += box_size
  122. 122 }
  123. 123 if width == 0 || height == 0 {
  124. 124● raise @types.LoadError::DecodeFailed(
  125. 125 "AVIF: could not determine image dimensions",
  126. 126 )
  127. 127 }
  128. 128● let num_channels = if has_alpha { 4 } else { 3 }
  129. 129 AvifInfo::{ width, height, bit_depth, num_channels, has_alpha, }
  130. 130}
  131. 131
  132. 132///|
  133. 133/// 解码 AVIF 图像
  134. 134///
  135. 135/// 注意:完整 AVIF 解码需要 AV1 解码器,当前返回 UnsupportedFormat。
  136. 136/// 使用 avif_info 获取元数据。
  137. 137pub fn avif_decode(data : Bytes) -> @types.Image raise @types.LoadError {
  138. 138● let info = avif_info(data)
  139. 139 raise @types.LoadError::UnsupportedFormat(
  140. 140 "AVIF decode not yet implemented (AV1 decoder required). Image is \{info.width}x\{info.height} \{info.bit_depth}-bit",
  141. 141 )
  142. 142}