image.vision\video\video_quality.mbt
92.8% · 77 / 83 points · 307 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 视频质量 video/video_quality - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 视频质量 — VMAF/PSNR/SSIM - 7
// Video quality: VMAF / PSNR / SSIM - 8
// - 9
// Phase 17 v404: 视频处理大扩展 — 视频质量评估。 - 10
- 11
///| - 12
/// 视频质量度量方法。 - 13
/// 视频质量指标 Video quality metric. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum VideoQualityMetric { - 18
PSNRMetric // PSNR - 19
SSIMMetric // SSIM - 20
VMAFMetric // VMAF (简化) - 21
} - 22
- 23
///| - 24
pub extend VideoQualityMetric with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend VideoQualityMetric with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 视频质量评估结果。 - 31
/// 视频质量评估结果 Video quality assessment result. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct VideoQualityResult { - 34
metric : VideoQualityMetric - 35
score : Double // 质量分数 - 36
per_frame_scores : Array[Double] // 每帧分数 - 37
mean_score : Double // 平均分数 - 38
min_score : Double // 最低分数 - 39
max_score : Double // 最高分数 - 40
} - 41
- 42
///| - 43
pub extend VideoQualityResult with @debug.Debug::{to_repr} - 44
- 45
///| - 46
pub extend VideoQualityResult with Eq::{not_equal, equal} - 47
- 48
///| - 49
/// 计算 PSNR(峰值信噪比)。 - 50
/// 计算 PSNR(峰值信噪比) Compute PSNR (Peak Signal-to-Noise Ratio). - 51
pub fn compute_psnr( - 52
reference : @types.Image, - 53
distorted : @types.Image, - 54
) -> Double { - 55●
let w = reference.width - 56
let h = reference.height - 57
let ch = reference.channels - 58
let mut mse = 0.0 - 59
let n = w * h * ch - 60
if n == 0 { - 61●
return 0.0 - 62
} - 63
for i in 0..<n { - 64●
let diff = reference.data[i].to_double() - distorted.data[i].to_double() - 65
mse = mse + diff * diff - 66
} - 67●
mse = mse / n.to_double() - 68
if mse < 0.0001 { - 69●
return 100.0 - 70
} - 71●
10.0 * @math.log10(255.0 * 255.0 / mse) - 72
} - 73
- 74
///| - 75
/// 计算单窗口 SSIM。 - 76
/// 计算单窗口 SSIM Compute SSIM for a single window. - 77
fn compute_ssim_window( - 78
ref_data : @types.Image, - 79
dis_data : @types.Image, - 80
x0 : Int32, - 81
y0 : Int32, - 82
window_size : Int32, - 83
) -> Double { - 84●
let w = ref_data.width - 85
let ch = ref_data.channels - 86
let c1 = 0.01 * 255.0 * (0.01 * 255.0) - 87
let c2 = 0.03 * 255.0 * (0.03 * 255.0) - 88
let mut sum_ref = 0.0 - 89
let mut sum_dis = 0.0 - 90
let mut sum_ref_sq = 0.0 - 91
let mut sum_dis_sq = 0.0 - 92
let mut sum_ref_dis = 0.0 - 93
let count = window_size * window_size - 94
for dy in 0..<window_size { - 95●
for dx in 0..<window_size { - 96●
let x = x0 + dx - 97
let y = y0 + dy - 98
if x >= w || y >= ref_data.height { - 99○
continue - 100
} - 101
let idx = (y * w + x) * ch - 102●
let r = ref_data.data[idx].to_double() - 103●
let d = dis_data.data[idx].to_double() - 104
sum_ref = sum_ref + r - 105
sum_dis = sum_dis + d - 106
sum_ref_sq = sum_ref_sq + r * r - 107
sum_dis_sq = sum_dis_sq + d * d - 108
sum_ref_dis = sum_ref_dis + r * d - 109
} - 110
} - 111●
let mu_r = sum_ref / count.to_double() - 112●
let mu_d = sum_dis / count.to_double() - 113●
let var_r = sum_ref_sq / count.to_double() - mu_r * mu_r - 114●
let var_d = sum_dis_sq / count.to_double() - mu_d * mu_d - 115●
let cov = sum_ref_dis / count.to_double() - mu_r * mu_d - 116
let numerator = (2.0 * mu_r * mu_d + c1) * (2.0 * cov + c2) - 117
let denominator = (mu_r * mu_r + mu_d * mu_d + c1) * (var_r + var_d + c2) - 118
if denominator > 0.0 { - 119●
numerator / denominator - 120
} else { - 121○
0.0 - 122
} - 123
} - 124
- 125
///| - 126
/// 计算 SSIM(结构相似性指数)。 - 127
/// 计算 SSIM(结构相似度指数) Compute SSIM (Structural Similarity Index). - 128
pub fn compute_ssim( - 129
reference : @types.Image, - 130
distorted : @types.Image, - 131
) -> Double { - 132●
let w = reference.width - 133
let h = reference.height - 134
let window_size = 8 - 135
if w < window_size || h < window_size { - 136●
return compute_ssim_window( - 137
reference, - 138
distorted, - 139
0, - 140
0, - 141
if w < h { - 142○
w - 143
} else { - 144●
h - 145
}, - 146
) - 147
} - 148
let mut total_ssim = 0.0 - 149
let mut count = 0 - 150
let step = window_size - 151
let mut y = 0 - 152
while y + window_size <= h { - 153●
let mut x = 0 - 154
while x + window_size <= w { - 155●
total_ssim = total_ssim + - 156●
compute_ssim_window(reference, distorted, x, y, window_size) - 157
count = count + 1 - 158
x = x + step - 159
} - 160
y = y + step - 161
} - 162
if count > 0 { - 163●
total_ssim / count.to_double() - 164
} else { - 165○
0.0 - 166
} - 167
} - 168
- 169
///| - 170
/// 计算 MS-SSIM(多尺度结构相似性)。 - 171
/// 计算 MS-SSIM(多尺度结构相似度) Compute MS-SSIM (Multi-Scale Structural Similarity). - 172
pub fn compute_msssim( - 173
reference : @types.Image, - 174
distorted : @types.Image, - 175
) -> Double { - 176●
let weights = [0.0448, 0.2856, 0.3001, 0.2363, 0.1333] - 177
let mut result = 1.0 - 178
let mut current_ref = reference - 179
let mut current_dis = distorted - 180
for i in 0..<5 { - 181●
let ssim_val = compute_ssim(current_ref, current_dis) - 182●
result = result * @math.pow(ssim_val, weights[i]) - 183
let new_w = current_ref.width / 2 - 184
let new_h = current_ref.height / 2 - 185
if new_w < 4 || new_h < 4 { - 186●
break - 187
} - 188●
current_ref = downsample(current_ref) - 189●
current_dis = downsample(current_dis) - 190
} - 191●
result.clamp(min=0.0, max=1.0) - 192
} - 193
- 194
///| - 195
/// 下采样图像(2x降采样)。 - 196
/// 下采样图像(2倍) Downsample image (2x downsample). - 197
fn downsample(img : @types.Image) -> @types.Image { - 198●
let w = img.width - 199
let h = img.height - 200
let ch = img.channels - 201
let new_w = w / 2 - 202
let new_h = h / 2 - 203
let data : Array[Byte] = [] - 204
for y in 0..<new_h { - 205●
for x in 0..<new_w { - 206●
for c in 0..<ch { - 207●
let idx = (y * 2 * w + x * 2) * ch + c - 208
let idx_r = (y * 2 * w + x * 2 + 1) * ch + c - 209
let idx_d = ((y * 2 + 1) * w + x * 2) * ch + c - 210
let idx_dr = ((y * 2 + 1) * w + x * 2 + 1) * ch + c - 211
let val = ( - 212●
img.data[idx].to_double() + - 213●
img.data[idx_r].to_double() + - 214●
img.data[idx_d].to_double() + - 215●
img.data[idx_dr].to_double() - 216
) / - 217
4.0 - 218●
data.push(val.clamp(min=0.0, max=255.0).to_int().to_byte()) - 219
} - 220
} - 221
} - 222●
{ width: new_w, height: new_h, channels: ch, data: Bytes::from_array(data), } - 223
} - 224
- 225
///| - 226
/// 计算 VMAF 分数(简化版:基于 PSNR 和 SSIM 的加权组合)。 - 227
/// 计算 VMAF 分数(简化:PSNR 和 SSIM 加权组合) Compute VMAF score (simplified: weighted combination of PSNR and SSIM). - 228
pub fn compute_vmaf_score( - 229
reference : @types.Image, - 230
distorted : @types.Image, - 231
) -> Double { - 232●
let psnr = compute_psnr(reference, distorted) - 233●
let ssim = compute_ssim(reference, distorted) - 234●
let psnr_norm = (psnr / 100.0).clamp(min=0.0, max=1.0) - 235
0.5 * psnr_norm + 0.5 * ssim - 236
} - 237
- 238
///| - 239
/// 计算单帧质量。 - 240
/// 计算单帧质量 Compute single frame quality. - 241
pub fn compute_frame_quality( - 242
reference : @types.Image, - 243
distorted : @types.Image, - 244
metric : VideoQualityMetric, - 245
) -> Double { - 246●
match metric { - 247●
PSNRMetric => { - 248●
let psnr = compute_psnr(reference, distorted) - 249●
(psnr / 100.0).clamp(min=0.0, max=1.0) - 250
} - 251●
SSIMMetric => compute_ssim(reference, distorted) - 252●
VMAFMetric => compute_vmaf_score(reference, distorted) - 253
} - 254
} - 255
- 256
///| - 257
/// 计算视频质量。 - 258
/// 计算视频质量 Compute video quality. - 259
pub fn compute_video_quality( - 260
reference : Array[@types.Image], - 261
distorted : Array[@types.Image], - 262
metric : VideoQualityMetric, - 263
) -> VideoQualityResult { - 264●
let n = reference.length() - 265
let per_frame : Array[Double] = [] - 266
let mut sum_score = 0.0 - 267
let mut min_score = 1.0 - 268
let mut max_score = 0.0 - 269
for i in 0..<n { - 270●
let score = if i < distorted.length() { - 271●
compute_frame_quality(reference[i], distorted[i], metric) - 272
} else { - 273○
0.0 - 274
} - 275●
per_frame.push(score) - 276
sum_score = sum_score + score - 277
if score < min_score { - 278○
min_score = score - 279
} - 280
if score > max_score { - 281●
max_score = score - 282
} - 283
} - 284●
let mean = if n > 0 { sum_score / n.to_double() } else { 0.0 } - 285
{ - 286
metric, - 287
score: mean, - 288
per_frame_scores: per_frame, - 289
mean_score: mean, - 290
min_score, - 291
max_score, - 292
} - 293
} - 294
- 295
///| - 296
/// 质量评估流水线:多度量综合评估。 - 297
/// 质量评估管线:多指标综合评估 Quality assessment pipeline: multi-metric comprehensive assessment. - 298
pub fn quality_pipeline( - 299
reference : Array[@types.Image], - 300
distorted : Array[@types.Image], - 301
) -> Array[VideoQualityResult] { - 302●
let results : Array[VideoQualityResult] = [] - 303●
results.push(compute_video_quality(reference, distorted, PSNRMetric)) - 304●
results.push(compute_video_quality(reference, distorted, SSIMMetric)) - 305●
results.push(compute_video_quality(reference, distorted, VMAFMetric)) - 306
results - 307
}