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