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