image.vision\video\video_quality.mbt

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