]>
Commit | Line | Data |
---|---|---|
5641dccf KS |
1 | use nihav_core::io::bitreader::*; |
2 | use nihav_core::io::codebook::*; | |
3 | use nihav_core::formats; | |
4 | use nihav_core::codecs::*; | |
77d06de2 KS |
5 | |
6 | static INDEO2_DELTA_TABLE: [[u8; 256]; 4] = [ | |
7 | [ | |
8 | 0x80, 0x80, 0x84, 0x84, 0x7C, 0x7C, 0x7F, 0x85, | |
9 | 0x81, 0x7B, 0x85, 0x7F, 0x7B, 0x81, 0x8C, 0x8C, | |
10 | 0x74, 0x74, 0x83, 0x8D, 0x7D, 0x73, 0x8D, 0x83, | |
11 | 0x73, 0x7D, 0x77, 0x89, 0x89, 0x77, 0x89, 0x77, | |
12 | 0x77, 0x89, 0x8C, 0x95, 0x74, 0x6B, 0x95, 0x8C, | |
13 | 0x6B, 0x74, 0x7C, 0x90, 0x84, 0x70, 0x90, 0x7C, | |
14 | 0x70, 0x84, 0x96, 0x96, 0x6A, 0x6A, 0x82, 0x98, | |
15 | 0x7E, 0x68, 0x98, 0x82, 0x68, 0x7E, 0x97, 0xA2, | |
16 | 0x69, 0x5E, 0xA2, 0x97, 0x5E, 0x69, 0xA2, 0xA2, | |
17 | 0x5E, 0x5E, 0x8B, 0xA3, 0x75, 0x5D, 0xA3, 0x8B, | |
18 | 0x5D, 0x75, 0x71, 0x95, 0x8F, 0x6B, 0x95, 0x71, | |
19 | 0x6B, 0x8F, 0x78, 0x9D, 0x88, 0x63, 0x9D, 0x78, | |
20 | 0x63, 0x88, 0x7F, 0xA7, 0x81, 0x59, 0xA7, 0x7F, | |
21 | 0x59, 0x81, 0xA4, 0xB1, 0x5C, 0x4F, 0xB1, 0xA4, | |
22 | 0x4F, 0x5C, 0x96, 0xB1, 0x6A, 0x4F, 0xB1, 0x96, | |
23 | 0x4F, 0x6A, 0xB2, 0xB2, 0x4E, 0x4E, 0x65, 0x9B, | |
24 | 0x9B, 0x65, 0x9B, 0x65, 0x65, 0x9B, 0x89, 0xB4, | |
25 | 0x77, 0x4C, 0xB4, 0x89, 0x4C, 0x77, 0x6A, 0xA3, | |
26 | 0x96, 0x5D, 0xA3, 0x6A, 0x5D, 0x96, 0x73, 0xAC, | |
27 | 0x8D, 0x54, 0xAC, 0x73, 0x54, 0x8D, 0xB4, 0xC3, | |
28 | 0x4C, 0x3D, 0xC3, 0xB4, 0x3D, 0x4C, 0xA4, 0xC3, | |
29 | 0x5C, 0x3D, 0xC3, 0xA4, 0x3D, 0x5C, 0xC4, 0xC4, | |
30 | 0x3C, 0x3C, 0x96, 0xC6, 0x6A, 0x3A, 0xC6, 0x96, | |
31 | 0x3A, 0x6A, 0x7C, 0xBA, 0x84, 0x46, 0xBA, 0x7C, | |
32 | 0x46, 0x84, 0x5B, 0xAB, 0xA5, 0x55, 0xAB, 0x5B, | |
33 | 0x55, 0xA5, 0x63, 0xB4, 0x9D, 0x4C, 0xB4, 0x63, | |
34 | 0x4C, 0x9D, 0x86, 0xCA, 0x7A, 0x36, 0xCA, 0x86, | |
35 | 0x36, 0x7A, 0xB6, 0xD7, 0x4A, 0x29, 0xD7, 0xB6, | |
36 | 0x29, 0x4A, 0xC8, 0xD7, 0x38, 0x29, 0xD7, 0xC8, | |
37 | 0x29, 0x38, 0xA4, 0xD8, 0x5C, 0x28, 0xD8, 0xA4, | |
38 | 0x28, 0x5C, 0x6C, 0xC1, 0x94, 0x3F, 0xC1, 0x6C, | |
39 | 0x3F, 0x94, 0xD9, 0xD9, 0x27, 0x27, 0x80, 0x80, | |
40 | ], [ | |
41 | 0x80, 0x80, 0x85, 0x85, 0x7B, 0x7B, 0x7E, 0x87, | |
42 | 0x82, 0x79, 0x87, 0x7E, 0x79, 0x82, 0x8F, 0x8F, | |
43 | 0x71, 0x71, 0x84, 0x8F, 0x7C, 0x71, 0x8F, 0x84, | |
44 | 0x71, 0x7C, 0x75, 0x8B, 0x8B, 0x75, 0x8B, 0x75, | |
45 | 0x75, 0x8B, 0x8E, 0x9A, 0x72, 0x66, 0x9A, 0x8E, | |
46 | 0x66, 0x72, 0x7B, 0x93, 0x85, 0x6D, 0x93, 0x7B, | |
47 | 0x6D, 0x85, 0x9B, 0x9B, 0x65, 0x65, 0x82, 0x9D, | |
48 | 0x7E, 0x63, 0x9D, 0x82, 0x63, 0x7E, 0x9B, 0xA8, | |
49 | 0x65, 0x58, 0xA8, 0x9B, 0x58, 0x65, 0xA9, 0xA9, | |
50 | 0x57, 0x57, 0x8D, 0xAA, 0x73, 0x56, 0xAA, 0x8D, | |
51 | 0x56, 0x73, 0x6E, 0x99, 0x92, 0x67, 0x99, 0x6E, | |
52 | 0x67, 0x92, 0x76, 0xA2, 0x8A, 0x5E, 0xA2, 0x76, | |
53 | 0x5E, 0x8A, 0x7F, 0xAF, 0x81, 0x51, 0xAF, 0x7F, | |
54 | 0x51, 0x81, 0xAB, 0xBA, 0x55, 0x46, 0xBA, 0xAB, | |
55 | 0x46, 0x55, 0x9A, 0xBB, 0x66, 0x45, 0xBB, 0x9A, | |
56 | 0x45, 0x66, 0xBB, 0xBB, 0x45, 0x45, 0x60, 0xA0, | |
57 | 0xA0, 0x60, 0xA0, 0x60, 0x60, 0xA0, 0x8B, 0xBE, | |
58 | 0x75, 0x42, 0xBE, 0x8B, 0x42, 0x75, 0x66, 0xAA, | |
59 | 0x9A, 0x56, 0xAA, 0x66, 0x56, 0x9A, 0x70, 0xB5, | |
60 | 0x90, 0x4B, 0xB5, 0x70, 0x4B, 0x90, 0xBE, 0xCF, | |
61 | 0x42, 0x31, 0xCF, 0xBE, 0x31, 0x42, 0xAB, 0xD0, | |
62 | 0x55, 0x30, 0xD0, 0xAB, 0x30, 0x55, 0xD1, 0xD1, | |
63 | 0x2F, 0x2F, 0x9A, 0xD3, 0x66, 0x2D, 0xD3, 0x9A, | |
64 | 0x2D, 0x66, 0x7B, 0xC5, 0x85, 0x3B, 0xC5, 0x7B, | |
65 | 0x3B, 0x85, 0x54, 0xB4, 0xAC, 0x4C, 0xB4, 0x54, | |
66 | 0x4C, 0xAC, 0x5E, 0xBE, 0xA2, 0x42, 0xBE, 0x5E, | |
67 | 0x42, 0xA2, 0x87, 0xD8, 0x79, 0x28, 0xD8, 0x87, | |
68 | 0x28, 0x79, 0xC0, 0xE8, 0x40, 0x18, 0xE8, 0xC0, | |
69 | 0x18, 0x40, 0xD5, 0xE8, 0x2B, 0x18, 0xE8, 0xD5, | |
70 | 0x18, 0x2B, 0xAB, 0xE9, 0x55, 0x17, 0xE9, 0xAB, | |
71 | 0x17, 0x55, 0x68, 0xCD, 0x98, 0x33, 0xCD, 0x68, | |
72 | 0x33, 0x98, 0xEA, 0xEA, 0x16, 0x16, 0x80, 0x80, | |
73 | ], [ | |
74 | 0x80, 0x80, 0x86, 0x86, 0x7A, 0x7A, 0x7E, 0x88, | |
75 | 0x82, 0x78, 0x88, 0x7E, 0x78, 0x82, 0x92, 0x92, | |
76 | 0x6E, 0x6E, 0x85, 0x92, 0x7B, 0x6E, 0x92, 0x85, | |
77 | 0x6E, 0x7B, 0x73, 0x8D, 0x8D, 0x73, 0x8D, 0x73, | |
78 | 0x73, 0x8D, 0x91, 0x9E, 0x6F, 0x62, 0x9E, 0x91, | |
79 | 0x62, 0x6F, 0x79, 0x97, 0x87, 0x69, 0x97, 0x79, | |
80 | 0x69, 0x87, 0xA0, 0xA0, 0x60, 0x60, 0x83, 0xA2, | |
81 | 0x7D, 0x5E, 0xA2, 0x83, 0x5E, 0x7D, 0xA0, 0xB0, | |
82 | 0x60, 0x50, 0xB0, 0xA0, 0x50, 0x60, 0xB1, 0xB1, | |
83 | 0x4F, 0x4F, 0x8F, 0xB2, 0x71, 0x4E, 0xB2, 0x8F, | |
84 | 0x4E, 0x71, 0x6B, 0x9E, 0x95, 0x62, 0x9E, 0x6B, | |
85 | 0x62, 0x95, 0x74, 0xA9, 0x8C, 0x57, 0xA9, 0x74, | |
86 | 0x57, 0x8C, 0x7F, 0xB8, 0x81, 0x48, 0xB8, 0x7F, | |
87 | 0x48, 0x81, 0xB4, 0xC5, 0x4C, 0x3B, 0xC5, 0xB4, | |
88 | 0x3B, 0x4C, 0x9F, 0xC6, 0x61, 0x3A, 0xC6, 0x9F, | |
89 | 0x3A, 0x61, 0xC6, 0xC6, 0x3A, 0x3A, 0x59, 0xA7, | |
90 | 0xA7, 0x59, 0xA7, 0x59, 0x59, 0xA7, 0x8D, 0xCA, | |
91 | 0x73, 0x36, 0xCA, 0x8D, 0x36, 0x73, 0x61, 0xB2, | |
92 | 0x9F, 0x4E, 0xB2, 0x61, 0x4E, 0x9F, 0x6D, 0xBF, | |
93 | 0x93, 0x41, 0xBF, 0x6D, 0x41, 0x93, 0xCA, 0xDF, | |
94 | 0x36, 0x21, 0xDF, 0xCA, 0x21, 0x36, 0xB3, 0xDF, | |
95 | 0x4D, 0x21, 0xDF, 0xB3, 0x21, 0x4D, 0xE1, 0xE1, | |
96 | 0x1F, 0x1F, 0x9F, 0xE3, 0x61, 0x1D, 0xE3, 0x9F, | |
97 | 0x1D, 0x61, 0x7A, 0xD3, 0x86, 0x2D, 0xD3, 0x7A, | |
98 | 0x2D, 0x86, 0x4C, 0xBE, 0xB4, 0x42, 0xBE, 0x4C, | |
99 | 0x42, 0xB4, 0x57, 0xCA, 0xA9, 0x36, 0xCA, 0x57, | |
100 | 0x36, 0xA9, 0x88, 0xE9, 0x78, 0x17, 0xE9, 0x88, | |
101 | 0x17, 0x78, 0xCC, 0xFB, 0x34, 0x05, 0xFB, 0xCC, | |
102 | 0x05, 0x34, 0xE6, 0xFB, 0x1A, 0x05, 0xFB, 0xE6, | |
103 | 0x05, 0x1A, 0xB4, 0xFD, 0x4C, 0x03, 0xFD, 0xB4, | |
104 | 0x03, 0x4C, 0x63, 0xDC, 0x9D, 0x24, 0xDC, 0x63, | |
105 | 0x24, 0x9D, 0xFE, 0xFE, 0x02, 0x02, 0x80, 0x80, | |
106 | ], [ | |
107 | 0x80, 0x80, 0x87, 0x87, 0x79, 0x79, 0x7E, 0x89, | |
108 | 0x82, 0x77, 0x89, 0x7E, 0x77, 0x82, 0x95, 0x95, | |
109 | 0x6B, 0x6B, 0x86, 0x96, 0x7A, 0x6A, 0x96, 0x86, | |
110 | 0x6A, 0x7A, 0x70, 0x90, 0x90, 0x70, 0x90, 0x70, | |
111 | 0x70, 0x90, 0x94, 0xA4, 0x6C, 0x5C, 0xA4, 0x94, | |
112 | 0x5C, 0x6C, 0x78, 0x9B, 0x88, 0x65, 0x9B, 0x78, | |
113 | 0x65, 0x88, 0xA6, 0xA6, 0x5A, 0x5A, 0x83, 0xA9, | |
114 | 0x7D, 0x57, 0xA9, 0x83, 0x57, 0x7D, 0xA6, 0xB9, | |
115 | 0x5A, 0x47, 0xB9, 0xA6, 0x47, 0x5A, 0xBA, 0xBA, | |
116 | 0x46, 0x46, 0x92, 0xBC, 0x6E, 0x44, 0xBC, 0x92, | |
117 | 0x44, 0x6E, 0x67, 0xA3, 0x99, 0x5D, 0xA3, 0x67, | |
118 | 0x5D, 0x99, 0x72, 0xB0, 0x8E, 0x50, 0xB0, 0x72, | |
119 | 0x50, 0x8E, 0x7F, 0xC3, 0x81, 0x3D, 0xC3, 0x7F, | |
120 | 0x3D, 0x81, 0xBE, 0xD2, 0x42, 0x2E, 0xD2, 0xBE, | |
121 | 0x2E, 0x42, 0xA5, 0xD4, 0x5B, 0x2C, 0xD4, 0xA5, | |
122 | 0x2C, 0x5B, 0xD4, 0xD4, 0x2C, 0x2C, 0x52, 0xAE, | |
123 | 0xAE, 0x52, 0xAE, 0x52, 0x52, 0xAE, 0x8F, 0xD8, | |
124 | 0x71, 0x28, 0xD8, 0x8F, 0x28, 0x71, 0x5B, 0xBB, | |
125 | 0xA5, 0x45, 0xBB, 0x5B, 0x45, 0xA5, 0x69, 0xCB, | |
126 | 0x97, 0x35, 0xCB, 0x69, 0x35, 0x97, 0xD8, 0xF0, | |
127 | 0x28, 0x10, 0xF0, 0xD8, 0x10, 0x28, 0xBD, 0xF1, | |
128 | 0x43, 0x0F, 0xF1, 0xBD, 0x0F, 0x43, 0xF3, 0xF3, | |
129 | 0x0D, 0x0D, 0xA5, 0xF6, 0x5B, 0x0A, 0xF6, 0xA5, | |
130 | 0x0A, 0x5B, 0x78, 0xE2, 0x88, 0x1E, 0xE2, 0x78, | |
131 | 0x1E, 0x88, 0x42, 0xC9, 0xBE, 0x37, 0xC9, 0x42, | |
132 | 0x37, 0xBE, 0x4F, 0xD8, 0xB1, 0x28, 0xD8, 0x4F, | |
133 | 0x28, 0xB1, 0x8A, 0xFD, 0x76, 0x03, 0xFD, 0x8A, | |
134 | 0x03, 0x76, 0xDB, 0xFF, 0x25, 0x01, 0xFF, 0xDB, | |
135 | 0x01, 0x25, 0xF9, 0xFF, 0x07, 0x01, 0xFF, 0xF9, | |
136 | 0x01, 0x07, 0xBE, 0xFF, 0x42, 0x01, 0xFF, 0xBE, | |
137 | 0x01, 0x42, 0x5E, 0xED, 0xA2, 0x13, 0xED, 0x5E, | |
138 | 0x13, 0xA2, 0xFF, 0xFF, 0x01, 0x01, 0x80, 0x80, | |
139 | ] | |
140 | ]; | |
141 | ||
142 | static INDEO2_CODE_CODES: &[u16] = &[ | |
143 | 0x0000, 0x0004, 0x0006, 0x0001, 0x0009, 0x0019, 0x000D, 0x001D, | |
144 | 0x0023, 0x0013, 0x0033, 0x000B, 0x002B, 0x001B, 0x0007, 0x0087, | |
145 | 0x0027, 0x00A7, 0x0067, 0x00E7, 0x0097, 0x0057, 0x0037, 0x00B7, | |
146 | 0x00F7, 0x000F, 0x008F, 0x018F, 0x014F, 0x00CF, 0x002F, 0x012F, | |
147 | 0x01AF, 0x006F, 0x00EF, 0x01EF, 0x001F, 0x021F, 0x011F, 0x031F, | |
148 | 0x009F, 0x029F, 0x019F, 0x039F, 0x005F, 0x025F, 0x015F, 0x035F, | |
149 | 0x00DF, 0x02DF, 0x01DF, 0x03DF, 0x003F, 0x103F, 0x083F, 0x183F, | |
150 | 0x043F, 0x143F, 0x0C3F, 0x1C3F, 0x023F, 0x123F, 0x0A3F, 0x1A3F, | |
151 | 0x063F, 0x163F, 0x0E3F, 0x1E3F, 0x013F, 0x113F, 0x093F, 0x193F, | |
152 | 0x053F, 0x153F, 0x0D3F, 0x1D3F, 0x033F, 0x133F, 0x0B3F, 0x1B3F, | |
153 | 0x073F, 0x173F, 0x0F3F, 0x1F3F, 0x00BF, 0x10BF, 0x08BF, 0x18BF, | |
154 | 0x04BF, 0x14BF, 0x0CBF, 0x1CBF, 0x02BF, 0x12BF, 0x0ABF, 0x1ABF, | |
155 | 0x06BF, 0x16BF, 0x0EBF, 0x1EBF, 0x01BF, 0x11BF, 0x09BF, 0x19BF, | |
156 | 0x05BF, 0x15BF, 0x0DBF, 0x1DBF, 0x03BF, 0x13BF, 0x0BBF, 0x1BBF, | |
157 | 0x07BF, 0x17BF, 0x0FBF, 0x1FBF, 0x007F, 0x207F, 0x107F, 0x307F, | |
158 | 0x087F, 0x287F, 0x187F, 0x387F, 0x047F, 0x247F, 0x147F, 0x0002, | |
159 | 0x0011, 0x0005, 0x0015, 0x0003, 0x003B, 0x0047, 0x00C7, 0x0017, | |
160 | 0x00D7, 0x0077, 0x010F, 0x004F, 0x01CF, 0x00AF, 0x016F | |
161 | ]; | |
162 | ||
163 | static INDEO2_CODE_LENGTHS: &[u8] = &[ | |
164 | 3, 3, 3, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 8, 8, | |
165 | 8, 8, 8, 8, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, | |
166 | 9, 9, 9, 9, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, | |
167 | 10, 10, 10, 10, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, | |
168 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, | |
169 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, | |
170 | 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, | |
171 | 13, 13, 13, 13, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 3, | |
172 | 5, 5, 5, 6, 6, 8, 8, 8, 8, 8, 9, 9, 9, 9, 9 | |
173 | ]; | |
174 | ||
175 | struct IR2CodeReader { } | |
176 | ||
177 | impl CodebookDescReader<u8> for IR2CodeReader { | |
178 | fn bits(&mut self, idx: usize) -> u8 { INDEO2_CODE_LENGTHS[idx] } | |
f2af8eca | 179 | fn code(&mut self, idx: usize) -> u32 { u32::from(INDEO2_CODE_CODES[idx]) } |
77d06de2 KS |
180 | fn sym (&mut self, idx: usize) -> u8 { |
181 | if idx < 0x7F { (idx + 1) as u8 } else { (idx + 2) as u8 } | |
182 | } | |
183 | fn len(&mut self) -> usize { INDEO2_CODE_LENGTHS.len() } | |
184 | } | |
185 | ||
70259941 | 186 | struct Indeo2Decoder { |
2422d969 | 187 | info: NACodecInfoRef, |
77d06de2 | 188 | cb: Codebook<u8>, |
88c03b61 | 189 | frmmgr: HAMShuffler, |
77d06de2 KS |
190 | } |
191 | ||
192 | impl Indeo2Decoder { | |
70259941 | 193 | fn new() -> Self { |
2422d969 | 194 | let dummy_info = NACodecInfo::new_dummy(); |
77d06de2 KS |
195 | let mut coderead = IR2CodeReader{}; |
196 | let cb = Codebook::new(&mut coderead, CodebookMode::LSB).unwrap(); | |
f2af8eca | 197 | Indeo2Decoder { info: dummy_info, cb, frmmgr: HAMShuffler::new() } |
77d06de2 KS |
198 | } |
199 | ||
200 | fn decode_plane_intra(&self, br: &mut BitReader, | |
22cb00db | 201 | buf: &mut NAVideoBuffer<u8>, planeno: usize, |
77d06de2 | 202 | tableno: usize) -> DecoderResult<()> { |
22cb00db KS |
203 | let offs = buf.get_offset(planeno); |
204 | let (w, h) = buf.get_dimensions(planeno); | |
205 | let stride = buf.get_stride(planeno); | |
77d06de2 KS |
206 | let cb = &self.cb; |
207 | ||
f2af8eca | 208 | let data = buf.get_data_mut().unwrap(); |
9037cf6b | 209 | let framebuf: &mut [u8] = data.as_mut_slice(); |
77d06de2 KS |
210 | |
211 | let table = &INDEO2_DELTA_TABLE[tableno]; | |
212 | ||
213 | let mut base = offs; | |
214 | let mut x: usize = 0; | |
215 | while x < w { | |
216 | let idx = br.read_cb(cb)? as usize; | |
217 | if idx >= 0x80 { | |
218 | let run = (idx - 0x80) * 2; | |
219 | if x + run > w { return Err(DecoderError::InvalidData); } | |
220 | for i in 0..run { | |
221 | framebuf[base + x + i] = 0x80; | |
222 | } | |
223 | x += run; | |
224 | } else { | |
225 | framebuf[base + x + 0] = table[(idx * 2 + 0) as usize]; | |
226 | framebuf[base + x + 1] = table[(idx * 2 + 1) as usize]; | |
227 | x += 2; | |
228 | } | |
229 | } | |
230 | base += stride; | |
231 | for _ in 1..h { | |
232 | let mut x: usize = 0; | |
233 | while x < w { | |
234 | let idx = br.read_cb(cb)? as usize; | |
235 | if idx >= 0x80 { | |
236 | let run = (idx - 0x80) * 2; | |
237 | if x + run > w { return Err(DecoderError::InvalidData); } | |
238 | for i in 0..run { | |
239 | framebuf[base + x + i] = framebuf[base + x + i - stride]; | |
240 | } | |
241 | x += run; | |
242 | } else { | |
f2af8eca KS |
243 | let delta0 = i16::from(table[idx * 2 + 0]) - 0x80; |
244 | let delta1 = i16::from(table[idx * 2 + 1]) - 0x80; | |
245 | let mut pix0 = i16::from(framebuf[base + x + 0 - stride]); | |
246 | let mut pix1 = i16::from(framebuf[base + x + 1 - stride]); | |
77d06de2 KS |
247 | pix0 += delta0; |
248 | pix1 += delta1; | |
249 | if pix0 < 0 { pix0 = 0; } | |
250 | if pix1 < 0 { pix1 = 0; } | |
251 | if pix0 > 255 { pix0 = 255; } | |
252 | if pix1 > 255 { pix1 = 255; } | |
253 | framebuf[base + x + 0] = pix0 as u8; | |
254 | framebuf[base + x + 1] = pix1 as u8; | |
255 | x += 2; | |
256 | } | |
257 | } | |
258 | base += stride; | |
259 | } | |
260 | Ok(()) | |
261 | } | |
262 | ||
263 | fn decode_plane_inter(&self, br: &mut BitReader, | |
22cb00db | 264 | buf: &mut NAVideoBuffer<u8>, planeno: usize, |
77d06de2 | 265 | tableno: usize) -> DecoderResult<()> { |
22cb00db KS |
266 | let offs = buf.get_offset(planeno); |
267 | let (w, h) = buf.get_dimensions(planeno); | |
268 | let stride = buf.get_stride(planeno); | |
77d06de2 KS |
269 | let cb = &self.cb; |
270 | ||
1a967e6b | 271 | let data = buf.get_data_mut().unwrap(); |
9037cf6b | 272 | let framebuf: &mut [u8] = data.as_mut_slice(); |
77d06de2 KS |
273 | |
274 | let table = &INDEO2_DELTA_TABLE[tableno]; | |
275 | ||
276 | let mut base = offs; | |
277 | for _ in 0..h { | |
278 | let mut x: usize = 0; | |
279 | while x < w { | |
280 | let idx = br.read_cb(cb)? as usize; | |
281 | if idx >= 0x80 { | |
282 | let run = (idx - 0x80) * 2; | |
283 | if x + run > w { return Err(DecoderError::InvalidData); } | |
284 | x += run; | |
285 | } else { | |
f2af8eca KS |
286 | let delta0 = i16::from(table[idx * 2 + 0]) - 0x80; |
287 | let delta1 = i16::from(table[idx * 2 + 1]) - 0x80; | |
288 | let mut pix0 = i16::from(framebuf[base + x + 0]); | |
289 | let mut pix1 = i16::from(framebuf[base + x + 1]); | |
290 | pix0 += (delta0 * 3) >> 2; | |
291 | pix1 += (delta1 * 3) >> 2; | |
77d06de2 KS |
292 | if pix0 < 0 { pix0 = 0; } |
293 | if pix1 < 0 { pix1 = 0; } | |
294 | if pix0 > 255 { pix0 = 255; } | |
295 | if pix1 > 255 { pix1 = 255; } | |
296 | framebuf[base + x + 0] = pix0 as u8; | |
297 | framebuf[base + x + 1] = pix1 as u8; | |
298 | x += 2; | |
299 | } | |
300 | } | |
301 | base += stride; | |
302 | } | |
303 | Ok(()) | |
304 | } | |
305 | } | |
306 | ||
307 | const IR2_START: usize = 48; | |
308 | ||
309 | impl NADecoder for Indeo2Decoder { | |
01613464 | 310 | fn init(&mut self, _supp: &mut NADecoderSupport, info: NACodecInfoRef) -> DecoderResult<()> { |
77d06de2 KS |
311 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { |
312 | let w = vinfo.get_width(); | |
313 | let h = vinfo.get_height(); | |
314 | let f = vinfo.is_flipped(); | |
315 | let fmt = formats::YUV410_FORMAT; | |
316 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(w, h, f, fmt)); | |
2422d969 | 317 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); |
88c03b61 | 318 | self.frmmgr.clear(); |
77d06de2 KS |
319 | Ok(()) |
320 | } else { | |
321 | Err(DecoderError::InvalidData) | |
322 | } | |
323 | } | |
01613464 | 324 | fn decode(&mut self, _supp: &mut NADecoderSupport, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { |
77d06de2 KS |
325 | let src = pkt.get_buffer(); |
326 | if src.len() <= IR2_START { return Err(DecoderError::ShortData); } | |
327 | let interframe = src[18]; | |
328 | let tabs = src[34]; | |
fa90ccfb | 329 | let mut br = BitReader::new(&src[IR2_START..], BitReaderMode::LE); |
77d06de2 KS |
330 | let luma_tab = tabs & 3; |
331 | let chroma_tab = (tabs >> 2) & 3; | |
332 | if interframe != 0 { | |
22cb00db | 333 | let vinfo = self.info.get_properties().get_video_info().unwrap(); |
f2af8eca | 334 | let bufinfo = alloc_video_buffer(vinfo, 2)?; |
22cb00db | 335 | let mut buf = bufinfo.get_vbuf().unwrap(); |
77d06de2 KS |
336 | for plane in 0..3 { |
337 | let tabidx = (if plane == 0 { luma_tab } else { chroma_tab }) as usize; | |
846959ef KS |
338 | let planeno = if plane == 0 { 0 } else { plane ^ 3 }; |
339 | self.decode_plane_intra(&mut br, &mut buf, planeno, tabidx)?; | |
77d06de2 | 340 | } |
22cb00db KS |
341 | self.frmmgr.add_frame(buf); |
342 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
343 | frm.set_keyframe(true); | |
344 | frm.set_frame_type(FrameType::I); | |
171860fc | 345 | Ok(frm.into_ref()) |
77d06de2 | 346 | } else { |
22cb00db | 347 | let bufret = self.frmmgr.clone_ref(); |
f2af8eca | 348 | if bufret.is_none() { return Err(DecoderError::MissingReference); } |
22cb00db KS |
349 | let mut buf = bufret.unwrap(); |
350 | ||
77d06de2 KS |
351 | for plane in 0..3 { |
352 | let tabidx = (if plane == 0 { luma_tab } else { chroma_tab }) as usize; | |
846959ef KS |
353 | let planeno = if plane == 0 { 0 } else { plane ^ 3 }; |
354 | self.decode_plane_inter(&mut br, &mut buf, planeno, tabidx)?; | |
77d06de2 | 355 | } |
22cb00db KS |
356 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), NABufferType::Video(buf)); |
357 | frm.set_keyframe(false); | |
358 | frm.set_frame_type(FrameType::P); | |
171860fc | 359 | Ok(frm.into_ref()) |
77d06de2 KS |
360 | } |
361 | } | |
f9be4e75 KS |
362 | fn flush(&mut self) { |
363 | self.frmmgr.clear(); | |
364 | } | |
77d06de2 KS |
365 | } |
366 | ||
08a1fab7 | 367 | pub fn get_decoder() -> Box<dyn NADecoder + Send> { |
70259941 KS |
368 | Box::new(Indeo2Decoder::new()) |
369 | } | |
370 | ||
77d06de2 KS |
371 | #[cfg(test)] |
372 | mod test { | |
3167c45c KS |
373 | use nihav_core::codecs::RegisteredDecoders; |
374 | use nihav_core::demuxers::RegisteredDemuxers; | |
375 | use nihav_core::test::dec_video::*; | |
376 | use crate::codecs::indeo_register_all_codecs; | |
377 | use nihav_commonfmt::demuxers::generic_register_all_demuxers; | |
77d06de2 KS |
378 | #[test] |
379 | fn test_indeo2() { | |
3167c45c KS |
380 | let mut dmx_reg = RegisteredDemuxers::new(); |
381 | generic_register_all_demuxers(&mut dmx_reg); | |
382 | let mut dec_reg = RegisteredDecoders::new(); | |
383 | indeo_register_all_codecs(&mut dec_reg); | |
384 | ||
23099ca9 KS |
385 | test_decoding("avi", "indeo2", "assets/Indeo/laser05.avi", Some(10), |
386 | &dmx_reg, &dec_reg, ExpectedTestResult::MD5Frames(vec![ | |
387 | [0x55f509ad, 0x62fb52d5, 0x6e9a86b2, 0x3910ce74], | |
388 | [0x76a2b95d, 0x97bd2eca, 0xc9815f99, 0xe196b47a], | |
389 | [0x4ce19793, 0x46ff7429, 0x89d5c3aa, 0x822b8825], | |
390 | [0xb9cd338f, 0x3d4884a7, 0x5a9e978d, 0xc5abcfe8], | |
391 | [0xc4c6997a, 0x7dbb3a97, 0x1e4e65f6, 0xb5b6fba5], | |
392 | [0xe315980e, 0x817f51e5, 0xf9a45363, 0x943c94b9], | |
393 | [0x09b8c723, 0xb39aa17e, 0x6de2a61b, 0xaceca224], | |
394 | [0xdc1b1966, 0xba5a13b3, 0x3a7fbdab, 0xdebb504c], | |
395 | [0xd33eed2a, 0x7b3834a6, 0x2d57cd23, 0x73644cd9], | |
396 | [0xd7bd2ade, 0x114f973e, 0xe9a9cf45, 0x3c04297e], | |
397 | [0x4d851f61, 0x519c41df, 0x325dc9f9, 0xdf88b57a]])); | |
77d06de2 | 398 | } |
77d06de2 | 399 | } |