image.vision\video\video_codec.mbt
93.3% · 111 / 119 points · 396 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_codec - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 视频编解码 — 帧内/帧间/熵编码 - 7
// Video codec: intra-frame / inter-frame / entropy coding - 8
// - 9
// Phase 17 v392: 视频处理大扩展 — 视频编解码。 - 10
- 11
///| - 12
/// 帧类型。 - 13
/// 帧类型 Frame type. - 14
/// `#non_exhaustive` 表示未来可能添加新构造器 - 15
#derive(Eq, @debug.Debug) - 16
#non_exhaustive - 17
pub(all) enum FrameType { - 18
IFrame // 帧内编码 - 19
PFrame // 前向预测 - 20
BFrame // 双向预测 - 21
} - 22
- 23
///| - 24
pub extend FrameType with @debug.Debug::{to_repr} - 25
- 26
///| - 27
pub extend FrameType with Eq::{not_equal, equal} - 28
- 29
///| - 30
/// 编码参数。 - 31
/// 编解码参数 Codec parameters. - 32
#derive(Eq, @debug.Debug) - 33
pub(all) struct VideoCodecParams { - 34
qp : Int32 // 量化参数 (1-50) - 35
block_size : Int32 // 块大小 (通常 8 或 16) - 36
search_range : Int32 // 运动搜索范围 - 37
iframe_interval : Int32 // I 帧间隔 - 38
} - 39
- 40
///| - 41
pub extend VideoCodecParams with @debug.Debug::{to_repr} - 42
- 43
///| - 44
pub extend VideoCodecParams with Eq::{not_equal, equal} - 45
- 46
///| - 47
/// 编码帧。 - 48
/// 编码帧 Encoded frame. - 49
#derive(Eq, @debug.Debug) - 50
pub(all) struct EncodedFrame { - 51
frame_type : FrameType - 52
data : Bytes // 压缩数据 - 53
motion_vectors : Array[Int32] // 运动矢量 (dx, dy 交替) - 54
width : Int32 - 55
height : Int32 - 56
channels : Int32 - 57
} - 58
- 59
///| - 60
pub extend EncodedFrame with @debug.Debug::{to_repr} - 61
- 62
///| - 63
pub extend EncodedFrame with Eq::{not_equal, equal} - 64
- 65
///| - 66
/// 编码视频。 - 67
/// 编码视频 Encoded video. - 68
#derive(Eq, @debug.Debug) - 69
pub(all) struct EncodedVideo { - 70
frames : Array[EncodedFrame] - 71
params : VideoCodecParams - 72
width : Int32 - 73
height : Int32 - 74
channels : Int32 - 75
} - 76
- 77
///| - 78
pub extend EncodedVideo with @debug.Debug::{to_repr} - 79
- 80
///| - 81
pub extend EncodedVideo with Eq::{not_equal, equal} - 82
- 83
///| - 84
/// 默认编码参数。 - 85
/// 默认编解码参数 Default codec parameters. - 86
pub fn default_video_codec_params() -> VideoCodecParams { - 87●
{ qp: 10, block_size: 8, search_range: 8, iframe_interval: 10, } - 88
} - 89
- 90
///| - 91
/// 游程编码 (RLE)。 - 92
/// 游程编码 Run-length encoding. - 93
pub fn rle_encode(data : Array[Int32]) -> Bytes { - 94●
let result : Array[Byte] = [] - 95●
let n = data.length() - 96
if n == 0 { - 97●
return Bytes::from_array(result) - 98
} - 99
let mut i = 0 - 100
while i < n { - 101●
let val = data[i] - 102
let mut run = 1 - 103
while i + run < n && data[i + run] == val && run < 255 { - 104●
run = run + 1 - 105
} - 106●
result.push(run.to_byte()) - 107●
result.push(val.clamp(min=0, max=255).to_byte()) - 108
i = i + run - 109
} - 110●
Bytes::from_array(result) - 111
} - 112
- 113
///| - 114
/// 游程解码 (RLE)。 - 115
/// 游程解码 Run-length decoding. - 116
pub fn rle_decode(data : Bytes) -> Array[Int32] { - 117●
let result : Array[Int32] = [] - 118●
let n = data.length() - 119
let mut i = 0 - 120
while i + 1 < n { - 121●
let run = data[i].to_int() - 122●
let val = data[i + 1].to_int() - 123
for _ in 0..<run { - 124●
result.push(val) - 125
} - 126
i = i + 2 - 127
} - 128
result - 129
} - 130
- 131
///| - 132
/// 量化。 - 133
/// 量化 Quantization. - 134
pub fn quantize(values : Array[Int32], qp : Int32) -> Array[Int32] { - 135●
if qp <= 0 { - 136●
return values - 137
} - 138
let q = qp - 139
let result : Array[Int32] = [] - 140
for v in values { - 141●
result.push((v + q / 2) / q) - 142
} - 143
result - 144
} - 145
- 146
///| - 147
/// 反量化。 - 148
/// 反量化 Dequantization. - 149
pub fn dequantize(values : Array[Int32], qp : Int32) -> Array[Int32] { - 150●
if qp <= 0 { - 151○
return values - 152
} - 153
let result : Array[Int32] = [] - 154
for v in values { - 155●
result.push(v * qp) - 156
} - 157
result - 158
} - 159
- 160
///| - 161
/// 帧内编码:将图像转为像素数组 + RLE 压缩。 - 162
/// 帧内编码 Intra-frame encoding - 163
pub fn encode_frame_intra( - 164
img : @types.Image, - 165
params : VideoCodecParams, - 166
) -> EncodedFrame { - 167●
let pixels : Array[Int32] = [] - 168●
for i in 0..<img.data.length() { - 169●
pixels.push(img.data[i].to_int()) - 170
} - 171●
let quantized = quantize(pixels, params.qp) - 172●
let compressed = rle_encode(quantized) - 173
{ - 174
frame_type: IFrame, - 175
data: compressed, - 176
motion_vectors: [], - 177
width: img.width, - 178
height: img.height, - 179
channels: img.channels, - 180
} - 181
} - 182
- 183
///| - 184
/// 帧内解码。 - 185
/// 帧内解码 Intra-frame decoding. - 186
pub fn decode_frame_intra(frame : EncodedFrame) -> @types.Image { - 187●
let quantized = rle_decode(frame.data) - 188●
let pixels = dequantize( - 189
quantized, - 190
frame.width * frame.height * frame.channels, - 191
) - 192
let data : Array[Byte] = [] - 193●
let n = pixels.length() - 194
for i in 0..<(frame.width * frame.height * frame.channels) { - 195●
if i < n { - 196●
data.push(pixels[i].clamp(min=0, max=255).to_byte()) - 197
} else { - 198○
data.push((0 : Byte)) - 199
} - 200
} - 201
{ - 202
width: frame.width, - 203
height: frame.height, - 204
channels: frame.channels, - 205●
data: Bytes::from_array(data), - 206
} - 207
} - 208
- 209
///| - 210
/// 计算运动矢量(简化块匹配)。 - 211
/// 计算运动向量(简化块匹配) Compute motion vectors (simplified block matching). - 212
pub fn compute_motion_vectors( - 213
current : @types.Image, - 214
reference : @types.Image, - 215
search_range : Int32, - 216
) -> Array[Int32] { - 217●
let vectors : Array[Int32] = [] - 218
let w = current.width - 219
let h = current.height - 220
let ch = current.channels - 221
let block = 8 - 222
let mut by = 0 - 223
while by < h { - 224●
let mut bx = 0 - 225
while bx < w { - 226●
let mut best_dx = 0 - 227
let mut best_dy = 0 - 228
let mut best_sad = 999999999.0 - 229
for dy in [0, -search_range, search_range] { - 230●
for dx in [0, -search_range, search_range] { - 231●
let mut sad = 0.0 - 232
for py in 0..<block { - 233●
for px in 0..<block { - 234●
let cx = bx + px - 235
let cy = by + py - 236
if cx >= w || cy >= h { - 237●
continue - 238
} - 239
let rx = cx + dx - 240
let ry = cy + dy - 241
if rx < 0 || rx >= w || ry < 0 || ry >= h { - 242●
continue - 243
} - 244
let ci = (cy * w + cx) * ch - 245
let ri = (ry * w + rx) * ch - 246●
let diff = current.data[ci].to_int() - reference.data[ri].to_int() - 247●
sad = sad + diff.to_double().abs() - 248
} - 249
} - 250
if sad < best_sad { - 251●
best_sad = sad - 252
best_dx = dx - 253
best_dy = dy - 254
} - 255
} - 256
} - 257●
vectors.push(best_dx) - 258●
vectors.push(best_dy) - 259
bx = bx + block - 260
} - 261
by = by + block - 262
} - 263
vectors - 264
} - 265
- 266
///| - 267
/// 帧间编码:运动估计 + 残差编码。 - 268
/// 帧间编码:运动估计 + 残差编码 Inter-frame encoding: motion estimation + residual coding. - 269
pub fn encode_frame_inter( - 270
current : @types.Image, - 271
reference : @types.Image, - 272
params : VideoCodecParams, - 273
) -> EncodedFrame { - 274●
let vectors = compute_motion_vectors(current, reference, params.search_range) - 275
// 残差 = 当前帧 - 运动补偿参考帧 - 276
let residuals : Array[Int32] = [] - 277
let w = current.width - 278
let h = current.height - 279
let ch = current.channels - 280
for y in 0..<h { - 281●
for x in 0..<w { - 282●
for c in 0..<ch { - 283●
let ci = (y * w + x) * ch + c - 284●
let cur = current.data[ci].to_int() - 285●
let ref_val = reference.data[ci].to_int() - 286●
residuals.push(cur - ref_val + 128) - 287
} - 288
} - 289
} - 290●
let quantized = quantize(residuals, params.qp) - 291●
let compressed = rle_encode(quantized) - 292
{ - 293
frame_type: PFrame, - 294
data: compressed, - 295
motion_vectors: vectors, - 296
width: w, - 297
height: h, - 298
channels: ch, - 299
} - 300
} - 301
- 302
///| - 303
/// 帧间解码:运动补偿 + 残差。 - 304
/// 帧间解码:运动补偿 + 残差 Inter-frame decoding: motion compensation + residual. - 305
pub fn decode_frame_inter( - 306
frame : EncodedFrame, - 307
reference : @types.Image, - 308
) -> @types.Image { - 309●
let quantized = rle_decode(frame.data) - 310●
let residuals = dequantize( - 311
quantized, - 312
frame.width * frame.height * frame.channels, - 313
) - 314
let data : Array[Byte] = [] - 315
let w = frame.width - 316
let h = frame.height - 317
let ch = frame.channels - 318
for y in 0..<h { - 319●
for x in 0..<w { - 320●
for c in 0..<ch { - 321●
let i = (y * w + x) * ch + c - 322●
let ref_val = reference.data[i].to_int() - 323◐
let res = if i < residuals.length() { residuals[i] - 128 } else { 0 } - 324
let val = ref_val + res - 325●
data.push(val.clamp(min=0, max=255).to_byte()) - 326
} - 327
} - 328
} - 329●
{ width: w, height: h, channels: ch, data: Bytes::from_array(data), } - 330
} - 331
- 332
///| - 333
/// 视频编码流水线。 - 334
/// 视频编码管线 Video encoding pipeline. - 335
pub fn encode_video( - 336
frames : Array[@types.Image], - 337
params : VideoCodecParams, - 338
) -> EncodedVideo { - 339●
let encoded : Array[EncodedFrame] = [] - 340●
let n = frames.length() - 341
if n == 0 { - 342●
return { frames: encoded, params, width: 0, height: 0, channels: 0, } - 343
} - 344
let w = frames[0].width - 345
let h = frames[0].height - 346
let ch = frames[0].channels - 347
for i in 0..<n { - 348●
if i % params.iframe_interval == 0 || i == 0 { - 349●
encoded.push(encode_frame_intra(frames[i], params)) - 350
} else { - 351●
encoded.push(encode_frame_inter(frames[i], frames[i - 1], params)) - 352
} - 353
} - 354
{ frames: encoded, params, width: w, height: h, channels: ch, } - 355
} - 356
- 357
///| - 358
/// 视频解码流水线。 - 359
/// 视频解码管线 Video decoding pipeline. - 360
pub fn decode_video(encoded : EncodedVideo) -> Array[@types.Image] { - 361●
let frames : Array[@types.Image] = [] - 362●
let n = encoded.frames.length() - 363
for i in 0..<n { - 364●
let frame = encoded.frames[i] - 365
if frame.frame_type is IFrame { - 366●
frames.push(decode_frame_intra(frame)) - 367●
} else if i > 0 { - 368●
let prev = decode_frame_intra(encoded.frames[i - 1]) - 369●
frames.push(decode_frame_inter(frame, prev)) - 370
} else { - 371○
frames.push(decode_frame_intra(frame)) - 372
} - 373
} - 374
frames - 375
} - 376
- 377
///| - 378
/// 计算压缩比。 - 379
/// 计算压缩比 Compute compression ratio. - 380
pub fn compute_compression_ratio( - 381
original : Array[@types.Image], - 382
encoded : EncodedVideo, - 383
) -> Double { - 384●
let mut original_size = 0 - 385
for img in original { - 386●
original_size = original_size + img.data.length() - 387
} - 388
let mut encoded_size = 0 - 389
for frame in encoded.frames { - 390●
encoded_size = encoded_size + frame.data.length() - 391
} - 392
if encoded_size == 0 { - 393○
return 0.0 - 394
} - 395●
original_size.to_double() / encoded_size.to_double() - 396
}