zvidlib-vp9-decoder 0.5.61

Pure-Rust VP9 software decoder for zvidlib
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Probability contexts (section 7.2 of the VP9 specification): the four
//! saved frame contexts, the symbol counts a frame accumulates, and the
//! backward adaptation that merges the two once a frame is decoded
//! (section 8.4). The merge rules and their rounding are libvpx's
//! (`vpx_dsp/prob.h`, `vp9_entropy.c`, `vp9_entropymode.c` and
//! `vp9_entropymv.c`).

use super::tables::*;

/// `vp9_intra_mode_tree`.
pub const INTRA_MODE_TREE: [i8; 18] = [
    -DC_PRED, 2, -TM_PRED, 4, -V_PRED, 6, 8, 12, -H_PRED, 10, -D135_PRED, -D117_PRED, -D45_PRED,
    14, -D63_PRED, 16, -D153_PRED, -D207_PRED,
];
/// `vp9_inter_mode_tree`, with symbols relative to `NEARESTMV`.
pub const INTER_MODE_TREE: [i8; 6] = [-2, 2, 0, 4, -1, -3];
/// `vp9_partition_tree`.
pub const PARTITION_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
/// `vp9_switchable_interp_tree`: regular, smooth, sharp.
pub const SWITCHABLE_INTERP_TREE: [i8; 4] = [0, 2, -1, -2];
/// `vp9_segment_tree`.
pub const SEGMENT_TREE: [i8; 14] = [2, 4, 6, 8, 10, 12, 0, -1, -2, -3, -4, -5, -6, -7];
/// `vp9_mv_joint_tree`.
pub const MV_JOINT_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];
/// `vp9_mv_class_tree`.
pub const MV_CLASS_TREE: [i8; 20] = [
    0, 2, -1, 4, 6, 8, -2, -3, 10, 12, -4, -5, -6, 14, 16, 18, -7, -8, -9, -10,
];
/// `vp9_mv_class0_tree`.
pub const MV_CLASS0_TREE: [i8; 2] = [0, -1];
/// `vp9_mv_fp_tree`.
pub const MV_FP_TREE: [i8; 6] = [0, 2, -1, 4, -2, -3];

pub const DC_PRED: i8 = 0;
pub const V_PRED: i8 = 1;
pub const H_PRED: i8 = 2;
pub const D45_PRED: i8 = 3;
pub const D135_PRED: i8 = 4;
pub const D117_PRED: i8 = 5;
pub const D153_PRED: i8 = 6;
pub const D207_PRED: i8 = 7;
pub const D63_PRED: i8 = 8;
pub const TM_PRED: i8 = 9;

/// The coefficient probabilities of one transform size:
/// `[plane type][ref][band][context][node]` (`vp9_coeff_probs_model`).
pub type CoefProbs = [[[[[u8; 3]; 6]; 6]; 2]; 2];

/// The coefficient counts of one transform size, over the four model tokens
/// (zero, one, more than one, end of block): `[plane type][ref][band]
/// [context][token]` (`vp9_coeff_count_model`).
pub type CoefCounts = [[[[[u32; 4]; 6]; 6]; 2]; 2];

/// The motion vector probabilities of one component (`nmv_component`).
#[derive(Clone, Copy, Debug)]
pub struct MvComponentProbs {
    pub sign: u8,
    pub classes: [u8; 10],
    pub class0: [u8; 1],
    pub bits: [u8; 10],
    pub class0_fp: [[u8; 3]; 2],
    pub fp: [u8; 3],
    pub class0_hp: u8,
    pub hp: u8,
}

/// One frame context (`FRAME_CONTEXT`).
#[derive(Clone, Debug)]
pub struct FrameContext {
    pub y_mode: [[u8; 9]; 4],
    pub uv_mode: [[u8; 9]; 10],
    pub partition: [[u8; 3]; 16],
    /// Indexed by transform size.
    pub coef: [CoefProbs; 4],
    pub switchable_interp: [[u8; 2]; 4],
    pub inter_mode: [[u8; 3]; 7],
    pub intra_inter: [u8; 4],
    pub comp_inter: [u8; 5],
    pub single_ref: [[u8; 2]; 5],
    pub comp_ref: [u8; 5],
    pub tx8: [[u8; 1]; 2],
    pub tx16: [[u8; 2]; 2],
    pub tx32: [[u8; 3]; 2],
    pub skip: [u8; 3],
    pub mv_joints: [u8; 3],
    pub mv: [MvComponentProbs; 2],
}

impl Default for FrameContext {
    /// The defaults `vp9_setup_past_independence` installs.
    fn default() -> Self {
        let component = |classes: [u8; 10], class0: u8| MvComponentProbs {
            sign: 128,
            classes,
            class0: [class0],
            bits: [136, 140, 148, 160, 176, 192, 224, 234, 234, 240],
            class0_fp: [[128, 128, 64], [96, 112, 64]],
            fp: [64, 96, 64],
            class0_hp: 160,
            hp: 128,
        };
        Self {
            y_mode: DEFAULT_IF_Y_PROBS,
            uv_mode: DEFAULT_IF_UV_PROBS,
            partition: DEFAULT_PARTITION_PROBS,
            coef: [
                DEFAULT_COEF_PROBS_4X4,
                DEFAULT_COEF_PROBS_8X8,
                DEFAULT_COEF_PROBS_16X16,
                DEFAULT_COEF_PROBS_32X32,
            ],
            switchable_interp: [[235, 162], [36, 255], [34, 3], [149, 144]],
            inter_mode: DEFAULT_INTER_MODE_PROBS,
            intra_inter: [9, 102, 187, 225],
            comp_inter: [239, 183, 119, 96, 41],
            single_ref: [[33, 16], [77, 74], [142, 142], [172, 170], [238, 247]],
            comp_ref: [50, 126, 123, 221, 226],
            tx8: [[100], [66]],
            tx16: [[20, 152], [15, 101]],
            tx32: [[3, 136, 37], [5, 52, 13]],
            skip: [192, 128, 64],
            mv_joints: [32, 64, 96],
            mv: [
                component([224, 144, 192, 168, 192, 176, 192, 198, 198, 245], 216),
                component([216, 128, 176, 160, 176, 176, 192, 198, 198, 208], 208),
            ],
        }
    }
}

/// The counts of one motion vector component (`nmv_component_counts`).
#[derive(Clone, Copy, Debug, Default)]
pub struct MvComponentCounts {
    pub sign: [u32; 2],
    pub classes: [u32; 11],
    pub class0: [u32; 2],
    pub bits: [[u32; 2]; 10],
    pub class0_fp: [[u32; 4]; 2],
    pub fp: [u32; 4],
    pub class0_hp: [u32; 2],
    pub hp: [u32; 2],
}

/// The symbol counts of one frame (`FRAME_COUNTS`).
#[derive(Clone, Debug, Default)]
pub struct FrameCounts {
    pub y_mode: [[u32; 10]; 4],
    pub uv_mode: [[u32; 10]; 10],
    pub partition: [[u32; 4]; 16],
    /// Indexed by transform size.
    pub coef: [CoefCounts; 4],
    pub eob_branch: [[[[[u32; 6]; 6]; 2]; 2]; 4],
    pub switchable_interp: [[u32; 3]; 4],
    pub inter_mode: [[u32; 4]; 7],
    pub intra_inter: [[u32; 2]; 4],
    pub comp_inter: [[u32; 2]; 5],
    pub single_ref: [[[u32; 2]; 2]; 5],
    pub comp_ref: [[u32; 2]; 5],
    pub tx8: [[u32; 2]; 2],
    pub tx16: [[u32; 3]; 2],
    pub tx32: [[u32; 4]; 2],
    pub skip: [[u32; 2]; 3],
    pub mv_joints: [u32; 4],
    pub mv: [MvComponentCounts; 2],
}

/// `get_prob`.
fn get_prob(numerator: u32, denominator: u32) -> u8 {
    let p = ((u64::from(numerator) * 256 + u64::from(denominator >> 1)) / u64::from(denominator))
        as i64;
    p.clamp(1, 255) as u8
}

/// `get_binary_prob`.
fn get_binary_prob(n0: u32, n1: u32) -> u8 {
    let denominator = n0 + n1;
    if denominator == 0 {
        128
    } else {
        get_prob(n0, denominator)
    }
}

/// `weighted_prob`.
fn weighted_prob(prob1: u8, prob2: u8, factor: u32) -> u8 {
    ((u32::from(prob1) * (256 - factor) + u32::from(prob2) * factor + 128) >> 8) as u8
}

/// `merge_probs`, used for coefficient probabilities.
fn merge_probs(pre: u8, counts: [u32; 2], count_sat: u32, max_update_factor: u32) -> u8 {
    let prob = get_binary_prob(counts[0], counts[1]);
    let count = (counts[0] + counts[1]).min(count_sat);
    let factor = max_update_factor * count / count_sat;
    weighted_prob(pre, prob, factor)
}

const COUNT_TO_UPDATE_FACTOR: [u32; 21] = [
    0, 6, 12, 19, 25, 32, 38, 44, 51, 57, 64, 70, 76, 83, 89, 96, 102, 108, 115, 121, 128,
];

/// `mode_mv_merge_probs`.
fn mode_mv_merge(pre: u8, counts: [u32; 2]) -> u8 {
    let denominator = counts[0] + counts[1];
    if denominator == 0 {
        pre
    } else {
        let factor = COUNT_TO_UPDATE_FACTOR[denominator.min(20) as usize];
        weighted_prob(pre, get_prob(counts[0], denominator), factor)
    }
}

/// `vpx_tree_merge_probs`.
fn tree_merge(tree: &[i8], pre: &[u8], counts: &[u32], probs: &mut [u8]) {
    fn walk(index: usize, tree: &[i8], pre: &[u8], counts: &[u32], probs: &mut [u8]) -> u32 {
        let left = tree[index];
        let left_count = if left <= 0 {
            counts[(-left) as usize]
        } else {
            walk(left as usize, tree, pre, counts, probs)
        };
        let right = tree[index + 1];
        let right_count = if right <= 0 {
            counts[(-right) as usize]
        } else {
            walk(right as usize, tree, pre, counts, probs)
        };
        probs[index >> 1] = mode_mv_merge(pre[index >> 1], [left_count, right_count]);
        left_count + right_count
    }
    walk(0, tree, pre, counts, probs);
}

/// `vp9_adapt_coef_probs`.
pub fn adapt_coef_probs(
    fc: &mut FrameContext,
    pre: &FrameContext,
    counts: &FrameCounts,
    frame_is_intra_only: bool,
    last_frame_was_key: bool,
) {
    let update_factor = if frame_is_intra_only {
        112
    } else if last_frame_was_key {
        128
    } else {
        112
    };
    let count_sat = 24;
    for tx in 0..4 {
        for plane in 0..2 {
            for reference in 0..2 {
                for band in 0..6 {
                    let contexts = if band == 0 { 3 } else { 6 };
                    for context in 0..contexts {
                        let c = counts.coef[tx][plane][reference][band][context];
                        let eob = counts.eob_branch[tx][plane][reference][band][context];
                        let (n0, n1, n2, neob) = (c[0], c[1], c[2], c[3]);
                        let branches = [[neob, eob.wrapping_sub(neob)], [n0, n1 + n2], [n1, n2]];
                        for node in 0..3 {
                            fc.coef[tx][plane][reference][band][context][node] = merge_probs(
                                pre.coef[tx][plane][reference][band][context][node],
                                branches[node],
                                count_sat,
                                update_factor,
                            );
                        }
                    }
                }
            }
        }
    }
}

/// `vp9_adapt_mode_probs`.
pub fn adapt_mode_probs(
    fc: &mut FrameContext,
    pre: &FrameContext,
    counts: &FrameCounts,
    switchable_filter: bool,
    tx_mode_select: bool,
) {
    for i in 0..4 {
        fc.intra_inter[i] = mode_mv_merge(pre.intra_inter[i], counts.intra_inter[i]);
    }
    for i in 0..5 {
        fc.comp_inter[i] = mode_mv_merge(pre.comp_inter[i], counts.comp_inter[i]);
        fc.comp_ref[i] = mode_mv_merge(pre.comp_ref[i], counts.comp_ref[i]);
        for j in 0..2 {
            fc.single_ref[i][j] = mode_mv_merge(pre.single_ref[i][j], counts.single_ref[i][j]);
        }
    }
    for i in 0..7 {
        // The inter mode tree's symbols are offsets from NEARESTMV, but the
        // counts are indexed by the same offsets, so they line up.
        tree_merge(
            &INTER_MODE_TREE,
            &pre.inter_mode[i],
            &counts.inter_mode[i],
            &mut fc.inter_mode[i],
        );
    }
    for i in 0..4 {
        tree_merge(
            &INTRA_MODE_TREE,
            &pre.y_mode[i],
            &counts.y_mode[i],
            &mut fc.y_mode[i],
        );
    }
    for i in 0..10 {
        tree_merge(
            &INTRA_MODE_TREE,
            &pre.uv_mode[i],
            &counts.uv_mode[i],
            &mut fc.uv_mode[i],
        );
    }
    for i in 0..16 {
        tree_merge(
            &PARTITION_TREE,
            &pre.partition[i],
            &counts.partition[i],
            &mut fc.partition[i],
        );
    }
    if switchable_filter {
        for i in 0..4 {
            tree_merge(
                &SWITCHABLE_INTERP_TREE,
                &pre.switchable_interp[i],
                &counts.switchable_interp[i],
                &mut fc.switchable_interp[i],
            );
        }
    }
    if tx_mode_select {
        for i in 0..2 {
            let c8 = counts.tx8[i];
            fc.tx8[i][0] = mode_mv_merge(pre.tx8[i][0], [c8[0], c8[1]]);
            let c16 = counts.tx16[i];
            let branches16 = [[c16[0], c16[1] + c16[2]], [c16[1], c16[2]]];
            for j in 0..2 {
                fc.tx16[i][j] = mode_mv_merge(pre.tx16[i][j], branches16[j]);
            }
            let c32 = counts.tx32[i];
            let branches32 = [
                [c32[0], c32[1] + c32[2] + c32[3]],
                [c32[1], c32[2] + c32[3]],
                [c32[2], c32[3]],
            ];
            for j in 0..3 {
                fc.tx32[i][j] = mode_mv_merge(pre.tx32[i][j], branches32[j]);
            }
        }
    }
    for i in 0..3 {
        fc.skip[i] = mode_mv_merge(pre.skip[i], counts.skip[i]);
    }
}

/// `vp9_adapt_mv_probs`.
pub fn adapt_mv_probs(
    fc: &mut FrameContext,
    pre: &FrameContext,
    counts: &FrameCounts,
    allow_high_precision_mv: bool,
) {
    tree_merge(
        &MV_JOINT_TREE,
        &pre.mv_joints,
        &counts.mv_joints,
        &mut fc.mv_joints,
    );
    for i in 0..2 {
        let component = &mut fc.mv[i];
        let pre_component = &pre.mv[i];
        let c = &counts.mv[i];
        component.sign = mode_mv_merge(pre_component.sign, c.sign);
        tree_merge(
            &MV_CLASS_TREE,
            &pre_component.classes,
            &c.classes,
            &mut component.classes,
        );
        tree_merge(
            &MV_CLASS0_TREE,
            &pre_component.class0,
            &c.class0,
            &mut component.class0,
        );
        for j in 0..10 {
            component.bits[j] = mode_mv_merge(pre_component.bits[j], c.bits[j]);
        }
        for j in 0..2 {
            tree_merge(
                &MV_FP_TREE,
                &pre_component.class0_fp[j],
                &c.class0_fp[j],
                &mut component.class0_fp[j],
            );
        }
        tree_merge(&MV_FP_TREE, &pre_component.fp, &c.fp, &mut component.fp);
        if allow_high_precision_mv {
            component.class0_hp = mode_mv_merge(pre_component.class0_hp, c.class0_hp);
            component.hp = mode_mv_merge(pre_component.hp, c.hp);
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn merge_keeps_the_previous_probability_without_counts() {
        assert_eq!(mode_mv_merge(77, [0, 0]), 77);
        assert_eq!(merge_probs(77, [0, 0], 24, 112), 77);
    }

    #[test]
    fn merge_moves_toward_the_observed_distribution() {
        // 20 or more observations use the full update factor of 128.
        assert_eq!(
            mode_mv_merge(128, [40, 0]),
            ((128 * 128 + 255 * 128 + 128) >> 8) as u8
        );
        assert_eq!(get_prob(0, 10), 1);
        assert_eq!(get_prob(10, 10), 255);
    }

    #[test]
    fn tree_merge_visits_every_node() {
        let mut probs = [0u8; 3];
        tree_merge(
            &PARTITION_TREE,
            &[128, 128, 128],
            &[30, 0, 0, 0],
            &mut probs,
        );
        assert!(probs[0] > 128);
        assert_eq!(probs[1], 128);
        assert_eq!(probs[2], 128);
    }
}