aboutsummaryrefslogtreecommitdiffstats
path: root/nihav-duck/src/codecs/vp6enc/rdo.rs
blob: d5f252b95e1ad43d2c714c50e0fde7b7bf98a73b (plain) (blame)
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
use nihav_codec_support::codecs::{MV, ZERO_MV};
use super::mb::{Coeffs, InterMB};
//use super::super::vpcommon::VPMBType;

// todo replace with an approximation formula
const AVG_NITS_PER_MB_INTRA: [u32; 64] = [
     143,  146,  148,  150,  154,  163,  169,  177,
     185,  193,  206,  218,  232,  238,  242,  246,
     250,  260,  265,  270,  275,  283,  290,  297,
     304,  319,  327,  343,  358,  368,  381,  393,
     405,  417,  432,  445,  460,  476,  501,  520,
     543,  564,  591,  617,  645,  678,  711,  746,
     786,  833,  892,  966, 1031, 1102, 1187, 1281,
    1395, 1539, 1711, 1938, 2247, 2655, 3314, 4625
];
const AVG_NITS_PER_MB_INTER: [u32; 64] = [
    112,  112,  112,  112,  110,  111,  111,  112,
    112,  113,  112,  118,  121,  117,  118,  118,
    119,  118,  118,  119,  120,  122,  123,  124,
    126,  126,  128,  132,  138,  141,  139,  141,
    148,  151,  152,  155,  158,  162,  172,  177,
    183,  189,  195,  203,  210,  220,  231,  243,
    256,  272,  289,  313,  337,  363,  396,  435,
    481,  539,  613,  710,  864, 1079, 1430, 2225
];
const AVG_NITS_PER_MB_INTRA_HUFF: [u32; 64] = [
    176,  179,  181,  184,  188,  198,  205,  213,
    223,  233,  247,  260,  276,  283,  288,  292,
    297,  307,  313,  318,  324,  334,  341,  349,
    357,  374,  383,  401,  417,  429,  441,  455,
    469,  482,  499,  513,  530,  547,  572,  594,
    621,  644,  673,  703,  734,  771,  807,  847,
    893,  948, 1016, 1097, 1168, 1244, 1335, 1436,
   1555, 1708, 1890, 2130, 2461, 2897, 3604, 5014
];
const AVG_NITS_PER_MB_INTER_HUFF: [u32; 64] = [
    126,  126,  126,  126,  125,  125,  126,  127,
    127,  129,  127,  134,  137,  134,  134,  135,
    136,  135,  136,  137,  138,  141,  142,  143,
    145,  146,  147,  152,  160,  163,  161,  164,
    171,  174,  176,  180,  183,  188,  199,  206,
    212,  219,  227,  236,  244,  255,  268,  281,
    296,  315,  334,  362,  389,  419,  456,  500,
    550,  613,  695,  799,  971, 1205, 1581, 2437
];

// todo replace with an approximation formula
const HDR_SIZES: [(u32, u32); 64] = [
     (5867,  797),  (5971,  802),  (5975,  798),  (5986,  814),
     (6152,  802),  (6269,  821),  (6457,  827),  (6620,  855),
     (6865,  865),  (6852,  878),  (7103,  885),  (7295,  924),
     (7658,  952),  (7710,  950),  (7851,  959),  (8119,  963),
     (8119,  962),  (8292,  914),  (8238,  920),  (8260,  949),
     (8174,  943),  (8515,  952),  (8625,  976),  (8784,  979),
     (8687,  987),  (8853, 1007),  (8934,  998),  (9270, 1013),
     (9524, 1082),  (9595, 1117),  (9889, 1060), (10187, 1070),
    (10241, 1143), (10527, 1164), (10691, 1151), (10694, 1188),
    (10959, 1132), (11092, 1168), (11197, 1237), (11443, 1267),
    (11573, 1295), (11856, 1309), (12165, 1340), (12299, 1364),
    (12767, 1344), (13108, 1373), (13262, 1407), (13497, 1420),
    (14019, 1458), (14442, 1525), (14935, 1574), (15155, 1626),
    (15682, 1691), (15936, 1754), (16394, 1859), (16953, 1944),
    (17407, 2078), (18143, 2195), (18850, 2339), (19677, 2525),
    (20546, 2786), (21228, 3093), (22549, 3469), (24164, 4144)
];

pub fn estimate_frame_size(intra: bool, huffman: bool, q: usize, mb_w: usize, mb_h: usize) -> usize {
    let hdr_nits = if intra { HDR_SIZES[q].0 } else { HDR_SIZES[q].1 } as usize;
    let avg_mb_nits = match (huffman, intra) {
            (false, true)  => AVG_NITS_PER_MB_INTRA[q],
            (false, false) => AVG_NITS_PER_MB_INTER[q],
            (true,  true)  => AVG_NITS_PER_MB_INTRA_HUFF[q],
            (true,  false) => AVG_NITS_PER_MB_INTER_HUFF[q],
        } as usize;
    (hdr_nits + avg_mb_nits * mb_w * mb_h + 7) >> 3
}

pub fn estimate_mv_nits(mv: MV) -> u32 {
    (mv.x.abs() + mv.y.abs()).min(32) as u32
}

// todo make an approximation
const NITS_PER_BLOCK: [[u32; 65]; 16] = [
  [
    0,   42,  103,  150,  197,  245,  290,  332,  362,  399,  438,  485,  516,  555,  568,  615,  645,  704,  752,  775,  829,  807,  850,  847,  847,  424,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938,  938
  ], [
    0,   44,  103,  151,  198,  245,  292,  340,  373,  405,  452,  498,  527,  570,  604,  638,  658,  697,  727,  764,  807,  842,  887,  986,  893,  985, 1048, 1018, 1018, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013, 1013
  ], [
    0,   47,  105,  154,  200,  246,  295,  342,  380,  419,  458,  498,  547,  585,  626,  664,  693,  734,  772,  809,  842,  853,  864,  920,  955,  981, 1031, 1045, 1096, 1152, 1184, 1188, 1206, 1338, 1270, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175, 1175
  ], [
    0,   49,  105,  155,  203,  248,  294,  342,  383,  416,  464,  510,  544,  595,  620,  668,  714,  747,  773,  824,  865,  889,  919,  934,  969, 1029, 1010, 1063, 1088, 1158, 1175, 1204, 1207, 1303, 1283, 1358, 1512, 1405, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337, 1337
  ], [
    0,   50,  106,  156,  203,  252,  297,  345,  387,  423,  463,  509,  552,  594,  627,  668,  722,  758,  788,  824,  870,  898,  936,  963,  983, 1019, 1023, 1100, 1101, 1140, 1199, 1206, 1240, 1278, 1301, 1368, 1405, 1365, 1445, 1436, 1483, 1562, 1562, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620, 1620
  ], [
    1,   51,  107,  156,  204,  252,  298,  347,  391,  424,  465,  509,  553,  591,  634,  685,  709,  747,  805,  826,  881,  914,  937,  997, 1023, 1024, 1056, 1091, 1108, 1145, 1214, 1207, 1246, 1260, 1332, 1400, 1391, 1426, 1486, 1552, 1482, 1573, 1626, 1610, 1610, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692, 1692
  ], [
    1,   53,  109,  158,  205,  253,  301,  352,  397,  425,  470,  505,  557,  595,  642,  682,  720,  755,  805,  849,  878,  930,  964,  995, 1034, 1072, 1093, 1142, 1180, 1194, 1219, 1243, 1286, 1318, 1325, 1379, 1403, 1471, 1477, 1473, 1554, 1585, 1593, 1646, 1677, 1692, 1763, 1825, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817, 1817
  ], [
    1,   54,  111,  159,  206,  255,  305,  355,  406,  428,  472,  516,  555,  593,  636,  688,  730,  765,  807,  842,  892,  933,  987, 1001, 1050, 1097, 1111, 1167, 1175, 1223, 1255, 1294, 1297, 1342, 1361, 1404, 1408, 1471, 1495, 1560, 1553, 1611, 1635, 1692, 1783, 1695, 1765, 1764, 1906, 1906, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018, 2018
  ], [
    1,   55,  113,  160,  209,  255,  306,  357,  408,  435,  477,  521,  557,  598,  641,  685,  730,  773,  814,  844,  889,  940,  982, 1008, 1065, 1102, 1124, 1188, 1228, 1251, 1287, 1297, 1350, 1399, 1380, 1436, 1484, 1514, 1537, 1590, 1599, 1642, 1634, 1726, 1762, 1755, 1799, 1862, 1919, 2086, 1712, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984, 1984
  ], [
    1,   56,  116,  164,  211,  259,  307,  359,  414,  437,  481,  525,  564,  608,  645,  685,  734,  769,  808,  858,  906,  943,  987, 1000, 1058, 1101, 1145, 1183, 1211, 1274, 1274, 1357, 1384, 1412, 1469, 1454, 1505, 1543, 1563, 1617, 1639, 1667, 1681, 1762, 1770, 1797, 1869, 1853, 1909, 1978, 2077, 2034, 1967, 2139, 2139, 2139, 2139, 2139, 2139, 2139, 2139, 2139, 2139, 2139, 2139
  ], [
    1,   59,  118,  166,  213,  261,  309,  360,  416,  437,  483,  524,  570,  611,  651,  686,  732,  768,  806,  844,  895,  938,  983, 1025, 1071, 1103, 1149, 1193, 1226, 1258, 1312, 1340, 1405, 1425, 1479, 1547, 1555, 1588, 1612, 1641, 1662, 1723, 1739, 1797, 1841, 1854, 1888, 1879, 1947, 2025, 2065, 2104, 2158, 2077, 2380, 2177, 2336, 2078, 2078, 2078, 2078, 2078, 2078, 2078, 2078
  ], [
    1,   61,  120,  170,  219,  265,  314,  366,  425,  439,  483,  528,  573,  615,  656,  693,  737,  772,  807,  852,  897,  929,  976, 1008, 1053, 1111, 1140, 1192, 1219, 1279, 1314, 1356, 1387, 1449, 1484, 1513, 1552, 1592, 1673, 1704, 1773, 1784, 1809, 1843, 1903, 1902, 1962, 2002, 2014, 2054, 2109, 2161, 2148, 2198, 2235, 2353, 2360, 2511, 2282, 2282, 2282, 2282, 2282, 2282, 2282
  ], [
    2,   65,  123,  173,  225,  273,  322,  373,  438,  448,  488,  533,  580,  616,  661,  703,  739,  779,  819,  855,  897,  943,  983, 1013, 1040, 1086, 1132, 1167, 1217, 1258, 1299, 1345, 1391, 1452, 1474, 1526, 1574, 1602, 1646, 1721, 1764, 1803, 1825, 1921, 1919, 2030, 2049, 2067, 2134, 2152, 2209, 2225, 2274, 2299, 2375, 2379, 2374, 2462, 2536, 2584, 2822, 2822, 2822, 2822, 2822
  ], [
    2,   74,  126,  177,  230,  284,  332,  386,  454,  462,  507,  542,  592,  630,  673,  717,  753,  799,  841,  874,  913,  955,  996, 1031, 1073, 1103, 1138, 1172, 1219, 1249, 1300, 1325, 1383, 1428, 1446, 1504, 1550, 1585, 1636, 1702, 1728, 1784, 1841, 1905, 1934, 1978, 2055, 2130, 2165, 2231, 2291, 2417, 2405, 2494, 2491, 2525, 2587, 2626, 2658, 2658, 2708, 2713, 2667, 2867, 2867
  ], [
    4,   88,  137,  193,  244,  302,  353,  416,  495,  490,  534,  579,  615,  663,  716,  757,  792,  833,  884,  918,  962, 1006, 1058, 1087, 1125, 1169, 1203, 1258, 1286, 1326, 1365, 1405, 1437, 1477, 1520, 1554, 1597, 1642, 1664, 1709, 1759, 1800, 1842, 1894, 1941, 2002, 2062, 2124, 2138, 2225, 2310, 2377, 2448, 2584, 2628, 2731, 2824, 2846, 2960, 3016, 3147, 3099, 3197, 3107, 3107
  ], [
   18,  145,  182,  239,  297,  360,  415,  485,  584,  579,  638,  684,  737,  782,  834,  878,  924,  967, 1016, 1062, 1111, 1145, 1206, 1258, 1302, 1357, 1388, 1460, 1493, 1538, 1577, 1619, 1684, 1732, 1770, 1823, 1879, 1909, 1959, 2022, 2053, 2113, 2176, 2207, 2272, 2316, 2367, 2428, 2479, 2548, 2622, 2671, 2748, 2813, 2894, 2984, 3064, 3153, 3258, 3392, 3507, 3669, 3870, 4082, 4255
  ]
];

pub fn estimate_intra_mb_nits(mb: &Coeffs, q: usize) -> u32 {
    let mut count = 0;
    for blk in mb.iter() {
        let mut nz = 0;
        for &el in blk.iter() {
            if el != 0 {
                nz += 1;
            }
        }
        count += NITS_PER_BLOCK[q >> 2][nz];
    }
    count
}

pub fn estimate_inter_mb_nits(mb: &InterMB, q: usize, fourmv: bool) -> u32 {
    let mut count = 0;
    for blk in mb.residue.coeffs.iter() {
        let mut nz = 0;
        for &el in blk.iter() {
            if el != 0 {
                nz += 1;
            }
        }
        count += NITS_PER_BLOCK[q >> 2][nz];
    }
    if !fourmv {
        if mb.mv[3] != ZERO_MV {
            count += estimate_mv_nits(mb.mv[3]);
        }
    } else {
        count += 4 * 2 * u32::from(super::models::PROB_BITS[128]);
        for &mv in mb.mv.iter() {
            if mv != ZERO_MV {
                count += estimate_mv_nits(mv);
            }
        }
    }
    count
}

/*pub fn estimate_mb_hdr_size(mb_type: VPMBType) -> u32 {
    match mb_type {
        VPMBType::Intra => 60,
        VPMBType::InterFourMV => 34,
        VPMBType::InterNoMV => 3,
        VPMBType::InterMV | VPMBType::InterNearest | VPMBType::InterNear => 20,
        _ => 50,
    }
}*/