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