image.vision\video\video_transcode.mbt

91.3% · 84 / 92 points · 390 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_transcode
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 视频转码 — 格式/分辨率/码率
  7. 7// Video transcoding: format / resolution / bitrate
  8. 8//
  9. 9// Phase 17 v394: 视频处理大扩展 — 视频转码。
  10. 10
  11. 11///|
  12. 12/// 视频格式。
  13. 13/// 视频格式 Video format.
  14. 14/// `#non_exhaustive` 表示未来可能添加新构造器
  15. 15#derive(Eq, @debug.Debug)
  16. 16#non_exhaustive
  17. 17pub(all) enum VideoFormat {
  18. 18 Raw // 原始格式
  19. 19 MPEG // MPEG
  20. 20 H264 // H.264
  21. 21 VP9 // VP9
  22. 22 AV1 // AV1
  23. 23}
  24. 24
  25. 25///|
  26. 26pub extend VideoFormat with @debug.Debug::{to_repr}
  27. 27
  28. 28///|
  29. 29pub extend VideoFormat with Eq::{not_equal, equal}
  30. 30
  31. 31///|
  32. 32/// 分辨率调整模式。
  33. 33/// 分辨率调整 Resolution adjustment
  34. 34/// `#non_exhaustive` 表示未来可能添加新构造器
  35. 35#derive(Eq, @debug.Debug)
  36. 36#non_exhaustive
  37. 37pub(all) enum ResolutionMode {
  38. 38 Scale // 缩放
  39. 39 Crop // 裁剪
  40. 40 Pad // 填充
  41. 41}
  42. 42
  43. 43///|
  44. 44pub extend ResolutionMode with @debug.Debug::{to_repr}
  45. 45
  46. 46///|
  47. 47pub extend ResolutionMode with Eq::{not_equal, equal}
  48. 48
  49. 49///|
  50. 50/// 码率控制模式。
  51. 51/// 码率控制模式 Bitrate control mode.
  52. 52/// `#non_exhaustive` 表示未来可能添加新构造器
  53. 53#derive(Eq, @debug.Debug)
  54. 54#non_exhaustive
  55. 55pub(all) enum BitrateMode {
  56. 56 CBR // 恒定码率
  57. 57 VBR // 可变码率
  58. 58 CRF // 恒定质量
  59. 59}
  60. 60
  61. 61///|
  62. 62pub extend BitrateMode with @debug.Debug::{to_repr}
  63. 63
  64. 64///|
  65. 65pub extend BitrateMode with Eq::{not_equal, equal}
  66. 66
  67. 67///|
  68. 68/// 转码参数。
  69. 69/// 转码参数 Transcode parameters.
  70. 70#derive(Eq, @debug.Debug)
  71. 71pub(all) struct TranscodeParams {
  72. 72 target_format : VideoFormat
  73. 73 target_width : Int32
  74. 74 target_height : Int32
  75. 75 resolution_mode : ResolutionMode
  76. 76 bitrate_mode : BitrateMode
  77. 77 target_bitrate : Int32 // kbps
  78. 78 crf : Int32 // 1-51 (仅 CRF 模式)
  79. 79 fps : Int32 // 帧率
  80. 80}
  81. 81
  82. 82///|
  83. 83pub extend TranscodeParams with @debug.Debug::{to_repr}
  84. 84
  85. 85///|
  86. 86pub extend TranscodeParams with Eq::{not_equal, equal}
  87. 87
  88. 88///|
  89. 89/// 转码结果。
  90. 90/// 转码结果 Transcode result.
  91. 91#derive(Eq, @debug.Debug)
  92. 92pub(all) struct TranscodeResult {
  93. 93 frames : Array[@types.Image]
  94. 94 format : VideoFormat
  95. 95 width : Int32
  96. 96 height : Int32
  97. 97 bitrate : Int32
  98. 98 fps : Int32
  99. 99 size_bytes : Int32
  100. 100}
  101. 101
  102. 102///|
  103. 103pub extend TranscodeResult with @debug.Debug::{to_repr}
  104. 104
  105. 105///|
  106. 106pub extend TranscodeResult with Eq::{not_equal, equal}
  107. 107
  108. 108///|
  109. 109/// 默认转码参数。
  110. 110/// 默认转码参数 Default transcode parameters.
  111. 111pub fn default_transcode_params() -> TranscodeParams {
  112. 112● {
  113. 113 target_format: H264,
  114. 114 target_width: 1280,
  115. 115 target_height: 720,
  116. 116 resolution_mode: Scale,
  117. 117 bitrate_mode: CRF,
  118. 118 target_bitrate: 2000,
  119. 119 crf: 23,
  120. 120 fps: 30,
  121. 121 }
  122. 122}
  123. 123
  124. 124///|
  125. 125/// 缩放图像。
  126. 126/// 缩放图像到目标尺寸 Scale image to target size.
  127. 127pub fn scale_image(
  128. 128 img : @types.Image,
  129. 129 target_width : Int32,
  130. 130 target_height : Int32,
  131. 131) -> @types.Image {
  132. 132● if target_width <= 0 || target_height <= 0 {
  133. 133○ return img
  134. 134 }
  135. 135 let ch = img.channels
  136. 136 let data : Array[Byte] = []
  137. 137● let sx = img.width.to_double() / target_width.to_double()
  138. 138● let sy = img.height.to_double() / target_height.to_double()
  139. 139 for oy in 0..<target_height {
  140. 140● for ox in 0..<target_width {
  141. 141● let ix = (ox.to_double() * sx).to_int().clamp(min=0, max=img.width - 1)
  142. 142● let iy = (oy.to_double() * sy).to_int().clamp(min=0, max=img.height - 1)
  143. 143 for c in 0..<ch {
  144. 144● data.push(img.data[(iy * img.width + ix) * ch + c])
  145. 145 }
  146. 146 }
  147. 147 }
  148. 148 {
  149. 149 width: target_width,
  150. 150 height: target_height,
  151. 151 channels: ch,
  152. 152● data: Bytes::from_array(data),
  153. 153 }
  154. 154}
  155. 155
  156. 156///|
  157. 157/// 裁剪图像。
  158. 158/// 裁剪图像到目标尺寸 Crop image to target size.
  159. 159pub fn crop_image(
  160. 160 img : @types.Image,
  161. 161 target_width : Int32,
  162. 162 target_height : Int32,
  163. 163) -> @types.Image {
  164. 164● if target_width <= 0 || target_height <= 0 {
  165. 165○ return img
  166. 166 }
  167. 167 let ch = img.channels
  168. 168 let x0 = (img.width - target_width) / 2
  169. 169 let y0 = (img.height - target_height) / 2
  170. 170 let data : Array[Byte] = []
  171. 171 for y in 0..<target_height {
  172. 172● for x in 0..<target_width {
  173. 173● let sx = (x0 + x).clamp(min=0, max=img.width - 1)
  174. 174● let sy = (y0 + y).clamp(min=0, max=img.height - 1)
  175. 175 for c in 0..<ch {
  176. 176● data.push(img.data[(sy * img.width + sx) * ch + c])
  177. 177 }
  178. 178 }
  179. 179 }
  180. 180 {
  181. 181 width: target_width,
  182. 182 height: target_height,
  183. 183 channels: ch,
  184. 184● data: Bytes::from_array(data),
  185. 185 }
  186. 186}
  187. 187
  188. 188///|
  189. 189/// 填充图像到目标尺寸。
  190. 190/// 填充图像到目标尺寸 Pad image to target size.
  191. 191pub fn pad_image(
  192. 192 img : @types.Image,
  193. 193 target_width : Int32,
  194. 194 target_height : Int32,
  195. 195 fill : Byte,
  196. 196) -> @types.Image {
  197. 197● if target_width <= 0 || target_height <= 0 {
  198. 198○ return img
  199. 199 }
  200. 200 let ch = img.channels
  201. 201 let data : Array[Byte] = []
  202. 202 let x0 = (target_width - img.width) / 2
  203. 203 let y0 = (target_height - img.height) / 2
  204. 204 for y in 0..<target_height {
  205. 205● for x in 0..<target_width {
  206. 206● let sx = x - x0
  207. 207 let sy = y - y0
  208. 208 for c in 0..<ch {
  209. 209● if sx >= 0 && sx < img.width && sy >= 0 && sy < img.height {
  210. 210● data.push(img.data[(sy * img.width + sx) * ch + c])
  211. 211 } else {
  212. 212● data.push(fill)
  213. 213 }
  214. 214 }
  215. 215 }
  216. 216 }
  217. 217 {
  218. 218 width: target_width,
  219. 219 height: target_height,
  220. 220 channels: ch,
  221. 221● data: Bytes::from_array(data),
  222. 222 }
  223. 223}
  224. 224
  225. 225///|
  226. 226/// 调整分辨率。
  227. 227/// 调整分辨率 Adjust resolution.
  228. 228pub fn resize_image(
  229. 229 img : @types.Image,
  230. 230 target_width : Int32,
  231. 231 target_height : Int32,
  232. 232 mode : ResolutionMode,
  233. 233) -> @types.Image {
  234. 234● match mode {
  235. 235● Scale => scale_image(img, target_width, target_height)
  236. 236● Crop => crop_image(img, target_width, target_height)
  237. 237● Pad => pad_image(img, target_width, target_height, (0 : Byte))
  238. 238 }
  239. 239}
  240. 240
  241. 241///|
  242. 242/// 计算估计码率。
  243. 243/// 计算估计码率 Compute estimated bitrate.
  244. 244pub fn compute_bitrate(
  245. 245 width : Int32,
  246. 246 height : Int32,
  247. 247 fps : Int32,
  248. 248 crf : Int32,
  249. 249) -> Int32 {
  250. 250 // 简化估算:码率 ∝ 宽×高×帧率 / (CRF因子)
  251. 251● let pixels = width.to_double() * height.to_double()
  252. 252● let crf_factor = crf.to_double() / 51.0 * 10.0 + 1.0
  253. 253● let bitrate = pixels * fps.to_double() * 0.01 / crf_factor
  254. 254● (bitrate / 1000.0).to_int()
  255. 255}
  256. 256
  257. 257///|
  258. 258/// 估计质量分数。
  259. 259/// 估计质量分(0-100) Estimate quality score (0-100).
  260. 260pub fn estimate_quality(crf : Int32, bitrate : Int32) -> Double {
  261. 261● let crf_score = (51 - crf).to_double() / 51.0 * 50.0
  262. 262● let bitrate_score = (bitrate.to_double() / 5000.0).clamp(min=0.0, max=1.0) *
  263. 263 50.0
  264. 264● (crf_score + bitrate_score).clamp(min=0.0, max=100.0)
  265. 265}
  266. 266
  267. 267///|
  268. 268/// 转码视频。
  269. 269/// 转码视频 Transcode video.
  270. 270pub fn transcode_video(
  271. 271 frames : Array[@types.Image],
  272. 272 params : TranscodeParams,
  273. 273) -> TranscodeResult {
  274. 274● if frames.length() == 0 {
  275. 275● return {
  276. 276 frames: [],
  277. 277 format: params.target_format,
  278. 278 width: params.target_width,
  279. 279 height: params.target_height,
  280. 280 bitrate: 0,
  281. 281 fps: params.fps,
  282. 282 size_bytes: 0,
  283. 283 }
  284. 284 }
  285. 285 let transcoded : Array[@types.Image] = []
  286. 286 for frame in frames {
  287. 287● transcoded.push(
  288. 288● resize_image(
  289. 289 frame,
  290. 290 params.target_width,
  291. 291 params.target_height,
  292. 292 params.resolution_mode,
  293. 293 ),
  294. 294 )
  295. 295 }
  296. 296● let bitrate = match params.bitrate_mode {
  297. 297● CBR => params.target_bitrate
  298. 298○ VBR => params.target_bitrate * 9 / 10
  299. 299 CRF =>
  300. 300● compute_bitrate(
  301. 301 params.target_width,
  302. 302 params.target_height,
  303. 303 params.fps,
  304. 304 params.crf,
  305. 305 )
  306. 306 }
  307. 307 let mut size = 0
  308. 308 for frame in transcoded {
  309. 309● size = size + frame.data.length()
  310. 310 }
  311. 311 // 格式压缩比估算
  312. 312● let compression = match params.target_format {
  313. 313○ Raw => 1.0
  314. 314● MPEG => 0.3
  315. 315● H264 => 0.2
  316. 316○ VP9 => 0.15
  317. 317○ AV1 => 0.1
  318. 318 }
  319. 319● let compressed_size = (size.to_double() * compression).to_int()
  320. 320 {
  321. 321 frames: transcoded,
  322. 322 format: params.target_format,
  323. 323 width: params.target_width,
  324. 324 height: params.target_height,
  325. 325 bitrate,
  326. 326 fps: params.fps,
  327. 327 size_bytes: compressed_size,
  328. 328 }
  329. 329}
  330. 330
  331. 331///|
  332. 332/// 调整视频帧率。
  333. 333/// 调整视频帧率 Adjust video framerate.
  334. 334pub fn adjust_framerate(
  335. 335 frames : Array[@types.Image],
  336. 336 source_fps : Int32,
  337. 337 target_fps : Int32,
  338. 338) -> Array[@types.Image] {
  339. 339● if source_fps <= 0 || target_fps <= 0 {
  340. 340○ return frames
  341. 341 }
  342. 342 if target_fps == source_fps {
  343. 343● return frames
  344. 344 }
  345. 345 if target_fps > source_fps {
  346. 346 // 帧率提升:重复帧
  347. 347● let ratio = target_fps / source_fps
  348. 348 let result : Array[@types.Image] = []
  349. 349 for frame in frames {
  350. 350● for _ in 0..<ratio {
  351. 351● result.push(frame)
  352. 352 }
  353. 353 }
  354. 354 result
  355. 355 } else {
  356. 356 // 帧率降低:跳帧
  357. 357● let ratio = source_fps / target_fps
  358. 358 let result : Array[@types.Image] = []
  359. 359● let n = frames.length()
  360. 360 let mut i = 0
  361. 361 while i < n {
  362. 362● result.push(frames[i])
  363. 363 i = i + ratio
  364. 364 }
  365. 365 result
  366. 366 }
  367. 367}
  368. 368
  369. 369///|
  370. 370/// 计算视频时长(秒)。
  371. 371/// 计算视频时长(秒) Compute video duration in seconds.
  372. 372pub fn compute_duration(frame_count : Int32, fps : Int32) -> Double {
  373. 373● if fps <= 0 {
  374. 374● return 0.0
  375. 375 }
  376. 376● frame_count.to_double() / fps.to_double()
  377. 377}
  378. 378
  379. 379///|
  380. 380/// 计算压缩比。
  381. 381/// 计算压缩比 Compute compression ratio.
  382. 382pub fn transcode_compression_ratio(
  383. 383 original_size : Int32,
  384. 384 transcoded_size : Int32,
  385. 385) -> Double {
  386. 386● if transcoded_size <= 0 {
  387. 387● return 0.0
  388. 388 }
  389. 389● original_size.to_double() / transcoded_size.to_double()
  390. 390}