1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
//! H.264 B-slice decoding: bi-directional motion vectors, prediction modes,
//! and bi-predictive motion compensation.
use crate::h264_motion::{MotionVector, motion_compensate_16x16, parse_mvd, predict_mv};
use crate::{BitstreamReader, VideoError};
// ---------------------------------------------------------------------------
// Bi-directional motion vector types
// ---------------------------------------------------------------------------
/// Bi-directional motion vectors for B-slice macroblocks.
#[derive(Debug, Clone, Copy, Default)]
pub struct BiMotionVector {
/// Motion vector from past (forward) reference.
pub forward: MotionVector,
/// Motion vector from future (backward) reference.
pub backward: MotionVector,
/// Prediction mode for this macroblock.
pub mode: BPredMode,
}
/// B-slice prediction mode.
#[derive(Debug, Clone, Copy, Default, PartialEq)]
pub enum BPredMode {
/// Use only forward (past) reference.
#[default]
Forward,
/// Use only backward (future) reference.
Backward,
/// Average of forward and backward predictions.
BiPred,
/// Derive MVs from co-located MB in future reference.
Direct,
}
// ---------------------------------------------------------------------------
// Bi-directional motion compensation
// ---------------------------------------------------------------------------
/// Bi-directional motion compensation: dispatches to the correct mode and
/// averages forward and backward predictions when needed.
#[allow(clippy::too_many_arguments)]
pub fn motion_compensate_bipred(
ref_fwd: &[u8],
ref_bwd: &[u8],
width: usize,
height: usize,
channels: usize,
bi_mv: &BiMotionVector,
mb_x: usize,
mb_y: usize,
output: &mut [u8],
out_width: usize,
) {
match bi_mv.mode {
BPredMode::Forward => {
motion_compensate_16x16(
ref_fwd,
width,
height,
channels,
bi_mv.forward,
mb_x,
mb_y,
output,
out_width,
);
}
BPredMode::Backward => {
motion_compensate_16x16(
ref_bwd,
width,
height,
channels,
bi_mv.backward,
mb_x,
mb_y,
output,
out_width,
);
}
BPredMode::BiPred | BPredMode::Direct => {
// Allocate temporary buffers for each prediction direction.
let mut fwd_block = vec![0u8; 16 * 16 * channels];
let mut bwd_block = vec![0u8; 16 * 16 * channels];
motion_compensate_16x16(
ref_fwd,
width,
height,
channels,
bi_mv.forward,
mb_x,
mb_y,
&mut fwd_block,
16,
);
motion_compensate_16x16(
ref_bwd,
width,
height,
channels,
bi_mv.backward,
mb_x,
mb_y,
&mut bwd_block,
16,
);
// Average the two predictions with rounding.
for row in 0..16 {
for col in 0..16 {
let dst_y = mb_y * 16 + row;
let dst_x = mb_x * 16 + col;
for c in 0..channels {
let f = fwd_block[(row * 16 + col) * channels + c] as u16;
let b = bwd_block[(row * 16 + col) * channels + c] as u16;
let dst_idx = (dst_y * out_width + dst_x) * channels + c;
if dst_idx < output.len() {
output[dst_idx] = (f + b).div_ceil(2) as u8;
}
}
}
}
}
}
}
// ---------------------------------------------------------------------------
// B-slice macroblock decoder
// ---------------------------------------------------------------------------
/// Decode a B-slice macroblock: parse mb_type and motion vectors, then apply
/// motion compensation.
///
/// Returns the decoded `BiMotionVector` so the caller can store it for
/// neighboring-block prediction of subsequent macroblocks.
#[allow(clippy::too_many_arguments)]
pub fn decode_b_macroblock(
reader: &mut BitstreamReader,
ref_fwd: &[u8],
ref_bwd: &[u8],
width: usize,
height: usize,
mb_x: usize,
mb_y: usize,
neighbor_mvs_fwd: &[MotionVector],
neighbor_mvs_bwd: &[MotionVector],
output: &mut [u8],
out_width: usize,
) -> Result<BiMotionVector, VideoError> {
// 1. Parse mb_type to determine prediction mode.
let mb_type = reader.read_ue()?;
let mode = match mb_type {
0 => BPredMode::Direct,
1 => BPredMode::Forward,
2 => BPredMode::Backward,
_ => BPredMode::BiPred,
};
// 2. Parse motion vector differences and predict final MVs.
let forward = if mode == BPredMode::Forward || mode == BPredMode::BiPred {
let (mvd_x, mvd_y) = parse_mvd(reader)?;
let predicted = predict_mv(
neighbor_mvs_fwd.first().copied().unwrap_or_default(),
neighbor_mvs_fwd.get(1).copied().unwrap_or_default(),
neighbor_mvs_fwd.get(2).copied().unwrap_or_default(),
);
MotionVector {
dx: predicted.dx + mvd_x,
dy: predicted.dy + mvd_y,
ref_idx: 0,
}
} else {
MotionVector::default()
};
let backward = if mode == BPredMode::Backward || mode == BPredMode::BiPred {
let (mvd_x, mvd_y) = parse_mvd(reader)?;
let predicted = predict_mv(
neighbor_mvs_bwd.first().copied().unwrap_or_default(),
neighbor_mvs_bwd.get(1).copied().unwrap_or_default(),
neighbor_mvs_bwd.get(2).copied().unwrap_or_default(),
);
MotionVector {
dx: predicted.dx + mvd_x,
dy: predicted.dy + mvd_y,
ref_idx: 0,
}
} else {
MotionVector::default()
};
let bi_mv = BiMotionVector {
forward,
backward,
mode,
};
// 3. Apply motion compensation.
let channels = 3;
motion_compensate_bipred(
ref_fwd, ref_bwd, width, height, channels, &bi_mv, mb_x, mb_y, output, out_width,
);
Ok(bi_mv)
}
// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bipred_averages_references() {
// Two 32x32 single-channel reference frames: one filled with 100, one with 200.
let width = 32;
let height = 32;
let channels = 1;
let ref_fwd = vec![100u8; width * height * channels];
let ref_bwd = vec![200u8; width * height * channels];
let bi_mv = BiMotionVector {
forward: MotionVector::default(),
backward: MotionVector::default(),
mode: BPredMode::BiPred,
};
let mut output = vec![0u8; width * height * channels];
motion_compensate_bipred(
&ref_fwd,
&ref_bwd,
width,
height,
channels,
&bi_mv,
0,
0,
&mut output,
width,
);
// Average of 100 and 200 with rounding = (100 + 200 + 1) / 2 = 150.
for row in 0..16 {
for col in 0..16 {
assert_eq!(
output[row * width + col],
150,
"bipred average mismatch at ({row}, {col})"
);
}
}
}
#[test]
fn bpred_forward_only() {
let width = 32;
let height = 32;
let channels = 1;
let ref_fwd = vec![42u8; width * height * channels];
let ref_bwd = vec![200u8; width * height * channels];
let bi_mv = BiMotionVector {
forward: MotionVector::default(),
backward: MotionVector::default(),
mode: BPredMode::Forward,
};
let mut output = vec![0u8; width * height * channels];
motion_compensate_bipred(
&ref_fwd,
&ref_bwd,
width,
height,
channels,
&bi_mv,
0,
0,
&mut output,
width,
);
// Forward-only should use ref_fwd exclusively.
for row in 0..16 {
for col in 0..16 {
assert_eq!(
output[row * width + col],
42,
"forward-only mismatch at ({row}, {col})"
);
}
}
}
#[test]
fn bpred_backward_only() {
let width = 32;
let height = 32;
let channels = 1;
let ref_fwd = vec![42u8; width * height * channels];
let ref_bwd = vec![77u8; width * height * channels];
let bi_mv = BiMotionVector {
forward: MotionVector::default(),
backward: MotionVector::default(),
mode: BPredMode::Backward,
};
let mut output = vec![0u8; width * height * channels];
motion_compensate_bipred(
&ref_fwd,
&ref_bwd,
width,
height,
channels,
&bi_mv,
0,
0,
&mut output,
width,
);
// Backward-only should use ref_bwd exclusively.
for row in 0..16 {
for col in 0..16 {
assert_eq!(
output[row * width + col],
77,
"backward-only mismatch at ({row}, {col})"
);
}
}
}
}