]>
Commit | Line | Data |
---|---|---|
2994c841 KS |
1 | use nihav_core::codecs::*; |
2 | use nihav_core::io::byteio::*; | |
3 | ||
4 | #[derive(Default)] | |
5 | struct Decryptor { | |
6 | key: [u8; 4], | |
7 | } | |
8 | ||
9 | impl Decryptor { | |
10 | fn decrypt(&mut self, buf: &mut [u8]) { | |
11 | let mut pos: u8 = 0; | |
12 | for el in buf.iter_mut() { | |
13 | *el ^= self.key[(pos & 3) as usize]; | |
14 | pos = pos.wrapping_add(1); | |
15 | } | |
16 | } | |
17 | fn set_state(&mut self, mut key: u32) { | |
18 | for _ in 0..3 { | |
19 | let bit31 = (key >> 31) & 1; | |
20 | let bit21 = (key >> 21) & 1; | |
21 | let bit01 = (key >> 1) & 1; | |
22 | let nbit0 = !key & 1; | |
23 | key = (key << 1) | (bit31 ^ bit21 ^ bit01 ^ nbit0); | |
24 | } | |
25 | for i in 0..4 { | |
26 | self.key[i] = (key >> (8 * (i ^ 3))) as u8; | |
27 | } | |
28 | } | |
29 | } | |
30 | ||
31 | struct Deltas { | |
32 | tabs: [[i16; 256]; 2], | |
33 | codebook: [[u8; 8]; 256], | |
34 | num_elems: [usize; 256], | |
35 | num_vq: usize, | |
36 | vq_idx: usize, | |
37 | vq_pos: usize, | |
0cc2044a | 38 | vq_esc: u8, |
2994c841 KS |
39 | } |
40 | ||
41 | impl Deltas { | |
42 | fn reset(&mut self, br: &mut ByteReader) -> DecoderResult<()> { | |
43 | let b = br.read_byte()? as usize; | |
44 | self.vq_idx = b; | |
45 | self.vq_pos = 0; | |
46 | Ok(()) | |
47 | } | |
48 | fn get_val(&mut self, br: &mut ByteReader) -> DecoderResult<u8> { | |
49 | if self.vq_idx > self.codebook.len() { return Err(DecoderError::ShortData); } | |
50 | let ret = self.codebook[self.vq_idx][self.vq_pos]; | |
51 | self.vq_pos += 1; | |
52 | if self.vq_pos == self.num_elems[self.vq_idx] { | |
53 | if br.left() > 0 { | |
54 | self.reset(br)?; | |
55 | } else { | |
56 | self.vq_idx = self.codebook.len() + 1; | |
57 | } | |
58 | } | |
59 | Ok(ret) | |
60 | } | |
0cc2044a KS |
61 | fn remap(val: u16) -> i16 { |
62 | let hval = (val >> 1) as i16; | |
63 | if (val & 1) == 0 { | |
64 | hval | |
65 | } else { | |
66 | -1 - hval | |
67 | } | |
68 | } | |
2994c841 KS |
69 | fn get_int(&mut self, br: &mut ByteReader) -> DecoderResult<i16> { |
70 | let b = self.get_val(br)?; | |
0cc2044a KS |
71 | if b != self.vq_esc - 1 { |
72 | return Ok(Self::remap(b as u16)); | |
73 | } | |
74 | let mut run = 0; | |
75 | let mut val; | |
76 | let mut pow = self.vq_esc as u16; | |
77 | loop { | |
78 | let b = self.get_val(br)?; | |
79 | run += 1; | |
80 | if b != self.vq_esc - 1 { | |
81 | val = (b as u16) * pow; | |
82 | break; | |
83 | } | |
84 | pow *= self.vq_esc as u16; | |
85 | } | |
86 | ||
87 | for _ in 0..run { | |
88 | pow /= self.vq_esc as u16; | |
89 | let b = self.get_val(br)? as u16; | |
90 | val += pow * (b as u16); | |
91 | } | |
92 | Ok(Self::remap(val)) | |
2994c841 KS |
93 | } |
94 | fn get_dy(&mut self, br: &mut ByteReader) -> DecoderResult<i16> { | |
95 | let b = self.get_val(br)?; | |
96 | Ok(self.tabs[1][b as usize]) | |
97 | } | |
98 | fn get_dc(&mut self, br: &mut ByteReader) -> DecoderResult<i16> { | |
99 | let b = self.get_val(br)?; | |
100 | Ok(self.tabs[0][b as usize]) | |
101 | } | |
102 | } | |
103 | ||
104 | impl Default for Deltas { | |
105 | fn default() -> Self { | |
106 | Self { | |
107 | tabs: [[0; 256]; 2], | |
108 | codebook: [[0; 8]; 256], | |
109 | num_elems: [0; 256], | |
110 | num_vq: 0, | |
111 | vq_idx: 0, | |
112 | vq_pos: 0, | |
0cc2044a | 113 | vq_esc: 0, |
2994c841 KS |
114 | } |
115 | } | |
116 | } | |
117 | ||
118 | #[derive(Clone,Copy, Default)] | |
119 | struct BlkInfo { | |
120 | btype: u8, | |
121 | mode: u8, | |
122 | mv_x: i16, | |
123 | mv_y: i16, | |
124 | } | |
125 | ||
126 | const NUM_CPARAMS: usize = 25; | |
127 | const CPARAM_NONE: u8 = 42; | |
128 | const CPARAM_MISSING: u8 = 42 * 2; | |
0cc2044a | 129 | const CPARAM_MV: u8 = 42 * 3; |
2994c841 KS |
130 | |
131 | macro_rules! apply_delta { | |
132 | ($buf:expr, $off:expr, $stride:expr, $hpred: expr, $delta:expr) => { | |
133 | $hpred = $hpred.wrapping_add($delta); | |
134 | $buf[$off] = $buf[$off - $stride].wrapping_add($hpred); | |
135 | }; | |
136 | } | |
137 | macro_rules! copy_line { | |
138 | ($buf:expr, $off:expr, $stride:expr) => { | |
139 | for i in 0..8 { | |
140 | $buf[$off + i] = $buf[$off + i - $stride]; | |
141 | } | |
142 | }; | |
143 | } | |
144 | ||
0cc2044a KS |
145 | #[derive(Default)] |
146 | struct Frame { | |
147 | ydata: Vec<i16>, | |
148 | udata: Vec<i16>, | |
149 | vdata: Vec<i16>, | |
150 | stride: usize, | |
151 | } | |
152 | ||
153 | impl Frame { | |
154 | fn resize(&mut self, w: usize, h: usize) { | |
155 | self.stride = w; | |
156 | self.ydata.resize(self.stride * (h + 1), 0x80); | |
157 | self.udata.resize(self.stride * (h + 1), 0x80); | |
158 | self.vdata.resize(self.stride * (h + 1), 0x80); | |
159 | } | |
160 | } | |
161 | ||
2994c841 KS |
162 | #[derive(Default)] |
163 | struct TM2XDecoder { | |
2422d969 | 164 | info: NACodecInfoRef, |
2994c841 KS |
165 | width: usize, |
166 | height: usize, | |
167 | dec_buf: Vec<u8>, | |
168 | version: u8, | |
0cc2044a | 169 | is_intra: bool, |
2994c841 KS |
170 | deltas: Deltas, |
171 | blk_info: Vec<BlkInfo>, | |
172 | tile_size: usize, | |
173 | cparams: [[u8; 8]; NUM_CPARAMS], | |
0cc2044a KS |
174 | cur_frame: Frame, |
175 | ref_frame: Frame, | |
2994c841 KS |
176 | } |
177 | ||
178 | impl TM2XDecoder { | |
179 | fn new() -> Self { Self::default() } | |
180 | fn output_frame(&mut self, buf: &mut NAVideoBuffer<u8>) { | |
0cc2044a KS |
181 | let mut offs = [ buf.get_offset(0), buf.get_offset(1), buf.get_offset(2) ]; |
182 | let strides = [ buf.get_stride(0), buf.get_stride(1), buf.get_stride(2) ]; | |
1a967e6b | 183 | let data = buf.get_data_mut().unwrap(); |
2994c841 KS |
184 | let dst = data.as_mut_slice(); |
185 | ||
0cc2044a | 186 | let mut pos = self.cur_frame.stride; |
2994c841 | 187 | for _y in 0..self.height { |
0cc2044a KS |
188 | for x in 0..self.width { |
189 | let y = self.cur_frame.ydata[pos + x]; | |
190 | let u = self.cur_frame.udata[pos + x]; | |
191 | let v = self.cur_frame.vdata[pos + x]; | |
192 | dst[offs[0] + x] = y.max(0).min(255) as u8; | |
193 | dst[offs[1] + x] = u.max(0).min(255) as u8; | |
194 | dst[offs[2] + x] = v.max(0).min(255) as u8; | |
195 | } | |
196 | for c in 0..3 { | |
197 | offs[c] += strides[c]; | |
2994c841 | 198 | } |
0cc2044a | 199 | pos += self.cur_frame.stride; |
2994c841 KS |
200 | } |
201 | } | |
202 | fn parse_init(&mut self, version: u8) -> DecoderResult<()> { | |
203 | self.version = version; | |
204 | ||
205 | let mut mr = MemoryReader::new_read(&self.dec_buf); | |
206 | let mut br = ByteReader::new(&mut mr); | |
207 | if version > 4 { | |
208 | let _smth = br.read_u32be()?; | |
209 | } | |
210 | let height = br.read_u16be()? as usize; | |
211 | let width = br.read_u16be()? as usize; | |
212 | validate!(width == self.width && height == self.height); | |
213 | if version > 4 { | |
214 | let _smth = br.read_u32be()?; | |
215 | } | |
216 | let _smth = br.read_byte()?; | |
217 | let _nfuncs = br.read_byte()? as usize; | |
218 | let _smth = br.read_u16be()? as usize; | |
219 | let has_mv = br.read_byte()?; | |
0cc2044a | 220 | self.is_intra = has_mv == 0; |
2994c841 KS |
221 | if version >= 4 { |
222 | let _flags = br.read_u16be()?; | |
223 | let id_len = br.read_byte()? as usize; | |
224 | br.read_skip(id_len)?; | |
225 | let _smth1 = br.read_byte()?; | |
226 | let len = br.read_byte()? as usize; | |
227 | br.read_skip(len)?; | |
228 | let _smth = br.read_u32be()?; | |
229 | } | |
230 | ||
231 | Ok(()) | |
232 | } | |
233 | fn parse_tabs(&mut self) -> DecoderResult<()> { | |
234 | let mut mr = MemoryReader::new_read(&self.dec_buf); | |
235 | let mut br = ByteReader::new(&mut mr); | |
236 | ||
237 | let idx = br.read_byte()? as usize; | |
238 | validate!(idx < self.deltas.tabs.len()); | |
239 | let len = br.read_byte()? as usize; | |
240 | validate!(((len * 2) as i64) == br.left()); | |
241 | for i in 0..len { | |
242 | self.deltas.tabs[idx][i] = br.read_u16be()? as i16; | |
243 | } | |
244 | ||
245 | Ok(()) | |
246 | } | |
247 | fn parse_cb_desc(&mut self, version: u8) -> DecoderResult<()> { | |
248 | let mut mr = MemoryReader::new_read(&self.dec_buf); | |
249 | let mut br = ByteReader::new(&mut mr); | |
250 | ||
251 | if version == 0x0A { | |
0cc2044a KS |
252 | self.deltas.vq_esc = br.read_byte()?; |
253 | validate!(self.deltas.vq_esc > 1); | |
2994c841 KS |
254 | let _tag = br.read_u16be()?; |
255 | } | |
256 | let len = br.read_u16be()? as usize; | |
257 | validate!(len + 3 == (br.left() as usize)); | |
258 | let num_entries = br.read_u16be()?; | |
259 | validate!(num_entries == 256); | |
260 | let max_elems = br.read_byte()?; | |
261 | validate!(max_elems > 0 && max_elems <= 8); | |
262 | let mut idx = 0; | |
263 | while br.left() > 0 { | |
264 | validate!(idx < self.deltas.codebook.len()); | |
265 | let num_elems = br.read_byte()? as usize; | |
266 | validate!(num_elems <= 8); | |
267 | self.deltas.num_elems[idx] = num_elems; | |
268 | for i in 0..num_elems { | |
269 | self.deltas.codebook[idx][i] = br.read_byte()?; | |
270 | } | |
271 | idx += 1; | |
272 | } | |
273 | validate!(idx == 256); | |
274 | self.deltas.num_vq = idx; | |
275 | Ok(()) | |
276 | } | |
277 | ||
278 | fn decode_frame(&mut self, src: &[u8]) -> DecoderResult<()> { | |
279 | let mut mr = MemoryReader::new_read(src); | |
280 | let mut br = ByteReader::new(&mut mr); | |
281 | ||
282 | self.deltas.reset(&mut br)?; | |
283 | let bw = self.width / 8; | |
284 | let bh = self.height / 8; | |
285 | let ntiles = (bw + self.tile_size - 1) / self.tile_size; | |
0cc2044a KS |
286 | let mut pos = self.cur_frame.stride; |
287 | let ydata = &mut self.cur_frame.ydata; | |
288 | let udata = &mut self.cur_frame.udata; | |
289 | let vdata = &mut self.cur_frame.vdata; | |
290 | let stride = self.cur_frame.stride; | |
291 | for by in 0..bh { | |
2994c841 KS |
292 | for tile in 0..ntiles { |
293 | let xpos = tile * self.tile_size; | |
294 | let len = self.tile_size.min(bw - xpos); | |
0cc2044a KS |
295 | let mut last_x = 0; |
296 | let mut last_y = 0; | |
2994c841 KS |
297 | for el in self.blk_info.iter_mut().take(len) { |
298 | let t1 = self.deltas.get_val(&mut br)?; | |
299 | let t2 = self.deltas.get_val(&mut br)?; | |
2994c841 KS |
300 | validate!((t1 as usize) < NUM_CPARAMS); |
301 | validate!(self.cparams[t1 as usize][0] != CPARAM_MISSING); | |
302 | el.btype = t1; | |
303 | el.mode = t2; | |
304 | if t2 > 0 { | |
305 | el.mv_x = self.deltas.get_int(&mut br)?; | |
306 | el.mv_y = self.deltas.get_int(&mut br)?; | |
0cc2044a KS |
307 | last_x = el.mv_x; |
308 | last_y = el.mv_y; | |
2994c841 | 309 | } else { |
0cc2044a KS |
310 | el.mv_x = last_x; |
311 | el.mv_y = last_y; | |
2994c841 KS |
312 | } |
313 | } | |
314 | for line in 0..8 { | |
315 | let mut ypred = 0i16; | |
316 | let mut upred = 0i16; | |
317 | let mut vpred = 0i16; | |
318 | for x in 0..len { | |
319 | let bx = xpos + x; | |
320 | let op = self.cparams[self.blk_info[x].btype as usize][line]; | |
0cc2044a | 321 | let cur_off = pos + bx * 8 + line * stride; |
2994c841 KS |
322 | match op { |
323 | 0 => { // y4|y4 | |
324 | for i in 0..8 { | |
325 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 326 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 327 | } |
0cc2044a KS |
328 | copy_line!(udata, cur_off, stride); |
329 | copy_line!(vdata, cur_off, stride); | |
2994c841 KS |
330 | upred = 0; |
331 | vpred = 0; | |
332 | }, | |
333 | 1 => { // y2|y2 | |
334 | for i in 0..8 { | |
335 | if (i & 1) == 0 { | |
336 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 337 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 338 | } else { |
0cc2044a | 339 | ydata[cur_off + i] = ydata[cur_off + i - 1]; |
2994c841 KS |
340 | } |
341 | } | |
0cc2044a KS |
342 | copy_line!(udata, cur_off, stride); |
343 | copy_line!(vdata, cur_off, stride); | |
2994c841 KS |
344 | upred = 0; |
345 | vpred = 0; | |
346 | }, | |
347 | 2 => { // y1|y1 | |
348 | for i in 0..8 { | |
349 | if (i & 3) == 0 { | |
350 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 351 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 352 | } else { |
0cc2044a | 353 | ydata[cur_off + i] = ydata[cur_off + i - 1]; |
2994c841 KS |
354 | } |
355 | } | |
0cc2044a KS |
356 | copy_line!(udata, cur_off, stride); |
357 | copy_line!(vdata, cur_off, stride); | |
2994c841 KS |
358 | upred = 0; |
359 | vpred = 0; | |
360 | }, | |
361 | 3 => { // y1|0 | |
362 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 363 | apply_delta!(ydata, cur_off, stride, ypred, delta); |
2994c841 | 364 | for i in 1..8 { |
0cc2044a | 365 | ydata[cur_off + i] = ydata[cur_off]; |
2994c841 | 366 | } |
0cc2044a KS |
367 | copy_line!(udata, cur_off, stride); |
368 | copy_line!(vdata, cur_off, stride); | |
2994c841 KS |
369 | upred = 0; |
370 | vpred = 0; | |
371 | }, | |
372 | 4 => { // c2y2c2y2|c2y2c2y2 | |
373 | for i in (0..8).step_by(2) { | |
374 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a KS |
375 | apply_delta!(udata, cur_off + i + 0, stride, upred, delta); |
376 | udata[cur_off + i + 1] = udata[cur_off + i]; | |
2994c841 | 377 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a KS |
378 | apply_delta!(vdata, cur_off + i + 0, stride, vpred, delta); |
379 | vdata[cur_off + i + 1] = vdata[cur_off + i]; | |
2994c841 | 380 | let delta = self.deltas.get_dy(&mut br)?; |
0cc2044a | 381 | apply_delta!(ydata, cur_off + i + 0, stride, ypred, delta); |
2994c841 | 382 | let delta = self.deltas.get_dy(&mut br)?; |
0cc2044a | 383 | apply_delta!(ydata, cur_off + i + 1, stride, ypred, delta); |
2994c841 KS |
384 | } |
385 | }, | |
386 | 5 => { // c2y1|c2y1 | |
387 | for i in 0..8 { | |
388 | if (i & 3) == 0 { | |
389 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 390 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 391 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 392 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 393 | let delta = self.deltas.get_dy(&mut br)?; |
0cc2044a | 394 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 395 | } else { |
0cc2044a KS |
396 | udata[cur_off + i] = udata[cur_off + i - 1]; |
397 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
398 | ydata[cur_off + i] = ydata[cur_off + i - 1]; | |
2994c841 KS |
399 | } |
400 | } | |
401 | }, | |
402 | 6 | 7 => unreachable!(), | |
403 | 8 => { // c2y4|c2y4 | |
404 | for i in 0..8 { | |
405 | if (i & 3) == 0 { | |
406 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 407 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 408 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 409 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 410 | } else { |
0cc2044a KS |
411 | udata[cur_off + i] = udata[cur_off + i - 1]; |
412 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
2994c841 KS |
413 | } |
414 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 415 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 KS |
416 | } |
417 | }, | |
418 | 9 => { // c2y2|c2y2 | |
419 | for i in 0..8 { | |
420 | if (i & 3) == 0 { | |
421 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 422 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 423 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 424 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 425 | } else { |
0cc2044a KS |
426 | udata[cur_off + i] = udata[cur_off + i - 1]; |
427 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
2994c841 KS |
428 | } |
429 | if (i & 1) == 0 { | |
430 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 431 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 432 | } else { |
0cc2044a | 433 | ydata[cur_off + i] = ydata[cur_off + i - 1]; |
2994c841 KS |
434 | } |
435 | } | |
436 | }, | |
437 | 10 => { // c2y1|c2y1 | |
438 | for i in 0..8 { | |
439 | if (i & 3) == 0 { | |
440 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 441 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 442 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 443 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 444 | let delta = self.deltas.get_dy(&mut br)?; |
0cc2044a | 445 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 446 | } else { |
0cc2044a KS |
447 | udata[cur_off + i] = udata[cur_off + i - 1]; |
448 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
449 | ydata[cur_off + i] = ydata[cur_off + i - 1]; | |
2994c841 KS |
450 | } |
451 | } | |
452 | }, | |
453 | 11 => unreachable!(), | |
454 | 12 => { // c2y8 | |
455 | for i in 0..8 { | |
456 | if i == 0 { | |
457 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 458 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 459 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 460 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 461 | } else { |
0cc2044a KS |
462 | udata[cur_off + i] = udata[cur_off + i - 1]; |
463 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
2994c841 KS |
464 | } |
465 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 466 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 KS |
467 | } |
468 | }, | |
469 | 13 => { // c2y4 | |
470 | for i in 0..8 { | |
471 | if i == 0 { | |
472 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 473 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 474 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 475 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 476 | } else { |
0cc2044a KS |
477 | udata[cur_off + i] = udata[cur_off + i - 1]; |
478 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
2994c841 KS |
479 | } |
480 | if (i & 1) == 0 { | |
481 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 482 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 483 | } else { |
0cc2044a | 484 | ydata[cur_off + i] = ydata[cur_off + i - 1]; |
2994c841 KS |
485 | } |
486 | } | |
487 | }, | |
488 | 14 => { // c2y2 | |
489 | for i in 0..8 { | |
490 | if i == 0 { | |
491 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 492 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 493 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 494 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 495 | } else { |
0cc2044a KS |
496 | udata[cur_off + i] = udata[cur_off + i - 1]; |
497 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
2994c841 KS |
498 | } |
499 | if (i & 3) == 0 { | |
500 | let delta = self.deltas.get_dy(&mut br)?; | |
0cc2044a | 501 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 502 | } else { |
0cc2044a | 503 | ydata[cur_off + i] = ydata[cur_off + i - 1]; |
2994c841 KS |
504 | } |
505 | } | |
506 | }, | |
507 | 15 => { // c2y1 | |
508 | for i in 0..8 { | |
509 | if i == 0 { | |
510 | let delta = self.deltas.get_dc(&mut br)?; | |
0cc2044a | 511 | apply_delta!(udata, cur_off + i, stride, upred, delta); |
2994c841 | 512 | let delta = self.deltas.get_dc(&mut br)?; |
0cc2044a | 513 | apply_delta!(vdata, cur_off + i, stride, vpred, delta); |
2994c841 | 514 | let delta = self.deltas.get_dy(&mut br)?; |
0cc2044a | 515 | apply_delta!(ydata, cur_off + i, stride, ypred, delta); |
2994c841 | 516 | } else { |
0cc2044a KS |
517 | udata[cur_off + i] = udata[cur_off + i - 1]; |
518 | vdata[cur_off + i] = vdata[cur_off + i - 1]; | |
519 | ydata[cur_off + i] = ydata[cur_off + i - 1]; | |
2994c841 KS |
520 | } |
521 | } | |
522 | }, | |
523 | CPARAM_NONE => { | |
0cc2044a KS |
524 | copy_line!(ydata, cur_off, stride); |
525 | copy_line!(udata, cur_off, stride); | |
526 | copy_line!(vdata, cur_off, stride); | |
2994c841 KS |
527 | ypred = 0; |
528 | upred = 0; | |
529 | vpred = 0; | |
530 | }, | |
0cc2044a KS |
531 | CPARAM_MV => { |
532 | let src_x = (bx as i16) * 8 + self.blk_info[x].mv_x; | |
533 | let src_y = ((by * 8 + line) as i16) + self.blk_info[x].mv_y; | |
534 | validate!(src_x >= 0 && (src_x as usize) + 8 <= self.width); | |
535 | validate!(src_y >= 0 && (src_y as usize) + 1 <= self.height); | |
536 | let ref_off = (src_x as usize) + ((src_y + 1) as usize) * stride; | |
537 | for i in 0..8 { | |
538 | ydata[cur_off + i] = self.ref_frame.ydata[ref_off + i]; | |
539 | udata[cur_off + i] = self.ref_frame.udata[ref_off + i]; | |
540 | vdata[cur_off + i] = self.ref_frame.vdata[ref_off + i]; | |
541 | } | |
542 | ypred = ydata[cur_off + 7] - ydata[cur_off + 7 - stride]; | |
543 | upred = udata[cur_off + 7] - udata[cur_off + 7 - stride]; | |
544 | vpred = vdata[cur_off + 7] - vdata[cur_off + 7 - stride]; | |
545 | }, | |
2994c841 KS |
546 | _ => unreachable!(), |
547 | } | |
548 | } | |
549 | } | |
550 | } | |
0cc2044a | 551 | pos += 8 * stride; |
2994c841 KS |
552 | } |
553 | ||
554 | Ok(()) | |
555 | } | |
556 | } | |
557 | ||
558 | impl NADecoder for TM2XDecoder { | |
2422d969 | 559 | fn init(&mut self, info: NACodecInfoRef) -> DecoderResult<()> { |
2994c841 | 560 | if let NACodecTypeInfo::Video(vinfo) = info.get_properties() { |
0cc2044a KS |
561 | let fmt = NAPixelFormaton::new(ColorModel::YUV(YUVSubmodel::YUVJ), |
562 | Some(NAPixelChromaton::new(0, 0, false, 8, 0, 0, 1)), | |
563 | Some(NAPixelChromaton::new(0, 0, false, 8, 0, 1, 1)), | |
564 | Some(NAPixelChromaton::new(0, 0, false, 8, 0, 2, 1)), | |
565 | None, None, 0, 3); | |
566 | let myinfo = NACodecTypeInfo::Video(NAVideoInfo::new(vinfo.get_width(), vinfo.get_height(), false, fmt)); | |
2994c841 KS |
567 | self.width = vinfo.get_width(); |
568 | self.height = vinfo.get_height(); | |
0cc2044a KS |
569 | self.cur_frame.resize(self.width, self.height); |
570 | self.ref_frame.resize(self.width, self.height); | |
2422d969 | 571 | self.info = NACodecInfo::new_ref(info.get_name(), myinfo, info.get_extradata()).into_ref(); |
2994c841 KS |
572 | Ok(()) |
573 | } else { | |
574 | Err(DecoderError::InvalidData) | |
575 | } | |
576 | } | |
577 | fn decode(&mut self, pkt: &NAPacket) -> DecoderResult<NAFrameRef> { | |
578 | let src = pkt.get_buffer(); | |
579 | validate!(src.len() > 8); | |
580 | let mut mr = MemoryReader::new_read(&src); | |
581 | let mut br = ByteReader::new(&mut mr); | |
582 | let mut dec = Decryptor::default(); | |
583 | ||
584 | let mut initialised = false; | |
585 | let mut got_key = false; | |
586 | let mut data_size = 0; | |
587 | self.cparams = [[CPARAM_MISSING; 8]; NUM_CPARAMS]; | |
0cc2044a | 588 | self.is_intra = false; |
2994c841 KS |
589 | while br.left() >= 8 { |
590 | let magic = br.read_u24be()?; | |
591 | let ctype = br.read_byte()?; | |
592 | let size = br.read_u32be()? as usize; | |
593 | validate!(magic == 0xA00001 && br.left() >= (size as i64)); | |
594 | if ctype == 0x06 { | |
595 | validate!(size >= 4); | |
596 | let key = br.read_u32be()?; | |
597 | validate!((key as usize) == size - 4); | |
598 | dec.set_state(key); | |
599 | data_size = size - 4; | |
600 | br.read_skip(size - 4)?; | |
601 | got_key = true; | |
602 | continue; | |
603 | } | |
604 | self.dec_buf.resize(size, 0); | |
605 | br.read_buf(&mut self.dec_buf)?; | |
606 | if ctype != 0x0A { | |
607 | dec.decrypt(&mut self.dec_buf); | |
608 | } | |
609 | match ctype { | |
610 | 0x08 | 0x0C | 0x10 | 0x11 | 0x15 | 0x16 => { | |
611 | if ctype < 0x15 { // old versions are not encrypted, roll back and zero key | |
612 | dec.decrypt(&mut self.dec_buf); | |
613 | dec.set_state(0); | |
614 | got_key = true; | |
615 | } | |
616 | validate!(got_key && !initialised); | |
617 | match ctype { | |
618 | 0x08 => self.parse_init(0)?, | |
619 | 0x0C => self.parse_init(1)?, | |
620 | 0x10 => self.parse_init(2)?, | |
621 | 0x11 => self.parse_init(3)?, | |
622 | 0x15 => self.parse_init(4)?, | |
623 | 0x16 => self.parse_init(5)?, | |
624 | _ => unreachable!(), | |
625 | }; | |
626 | initialised = true; | |
627 | }, | |
628 | 0x09 => { | |
629 | validate!(initialised); | |
630 | validate!(self.dec_buf.len() == 3); | |
631 | validate!(self.dec_buf[0] == 8); | |
632 | validate!(self.dec_buf[1] > 0); | |
633 | validate!(self.dec_buf[2] == 1); | |
634 | self.tile_size = self.dec_buf[1] as usize; | |
635 | self.blk_info.resize(self.tile_size, BlkInfo::default()); | |
636 | }, | |
637 | 0x0B => { | |
638 | validate!(initialised); | |
639 | validate!(self.dec_buf.len() == 4); | |
640 | let idx = self.dec_buf[3] as usize; | |
641 | validate!(idx < NUM_CPARAMS); | |
2994c841 | 642 | validate!((self.dec_buf[0] as usize) < TM2X_CODING_PARAMS.len()); |
0cc2044a KS |
643 | if self.dec_buf[0] != 0 { |
644 | let tab = &TM2X_CODING_PARAMS[self.dec_buf[0] as usize]; | |
645 | let m0 = tab[0] as usize; | |
646 | let m1 = tab[1] as usize; | |
647 | let m2 = tab[2] as usize; | |
648 | let m3 = tab[3] as usize; | |
649 | let full_mode = (m2 * 4 + m0) as u8; | |
650 | let lores_mode = m0 as u8; | |
651 | for i in 0..8 { | |
652 | if (i % m1) == 0 && (i % m3) == 0 { | |
653 | self.cparams[idx][i] = full_mode; | |
654 | } else if (i % m1) == 0 { | |
655 | self.cparams[idx][i] = lores_mode; | |
656 | } else { | |
657 | self.cparams[idx][i] = CPARAM_NONE; | |
658 | } | |
2994c841 | 659 | } |
0cc2044a KS |
660 | } else { |
661 | for i in 0..8 { self.cparams[idx][i] = CPARAM_MV; } | |
2994c841 KS |
662 | } |
663 | }, | |
664 | 0x02 => { | |
665 | validate!(initialised); | |
666 | self.parse_tabs()?; | |
667 | }, | |
668 | 0x0A => { | |
669 | validate!(initialised); | |
670 | self.parse_cb_desc(0xA)?; | |
671 | }, | |
672 | _ => { unimplemented!(); }, | |
673 | }; | |
674 | } | |
0cc2044a KS |
675 | if !self.is_intra { |
676 | std::mem::swap(&mut self.cur_frame, &mut self.ref_frame); | |
677 | } | |
2994c841 KS |
678 | self.decode_frame(&src[12..][..data_size])?; |
679 | ||
0cc2044a | 680 | let myinfo = self.info.get_properties().get_video_info().unwrap(); |
2994c841 KS |
681 | let bufret = alloc_video_buffer(myinfo, 2); |
682 | if let Err(_) = bufret { return Err(DecoderError::InvalidData); } | |
683 | let bufinfo = bufret.unwrap(); | |
684 | let mut buf = bufinfo.get_vbuf().unwrap(); | |
685 | ||
2994c841 KS |
686 | self.output_frame(&mut buf); |
687 | ||
688 | let mut frm = NAFrame::new_from_pkt(pkt, self.info.clone(), bufinfo); | |
0cc2044a KS |
689 | frm.set_keyframe(self.is_intra); |
690 | frm.set_frame_type(if self.is_intra { FrameType::I } else { FrameType::P }); | |
171860fc | 691 | Ok(frm.into_ref()) |
2994c841 KS |
692 | } |
693 | } | |
694 | ||
695 | pub fn get_decoder() -> Box<NADecoder> { | |
696 | Box::new(TM2XDecoder::new()) | |
697 | } | |
698 | ||
699 | #[cfg(test)] | |
700 | mod test { | |
701 | use nihav_core::codecs::RegisteredDecoders; | |
702 | use nihav_core::demuxers::RegisteredDemuxers; | |
703 | use nihav_core::test::dec_video::*; | |
704 | use crate::codecs::duck_register_all_codecs; | |
705 | use nihav_commonfmt::demuxers::generic_register_all_demuxers; | |
706 | #[test] | |
707 | fn test_tm2x() { | |
708 | let mut dmx_reg = RegisteredDemuxers::new(); | |
709 | generic_register_all_demuxers(&mut dmx_reg); | |
710 | let mut dec_reg = RegisteredDecoders::new(); | |
711 | duck_register_all_codecs(&mut dec_reg); | |
712 | ||
713 | test_file_decoding("avi", "assets/Duck/TM2x.avi", Some(16), true, false, None/*Some("tm2x")*/, &dmx_reg, &dec_reg); | |
714 | } | |
715 | } | |
716 | ||
717 | const TM2X_CODING_PARAMS: [[u8; 4]; 25] = [ | |
718 | [ 0, 0, 0, 0 ], [ 0, 1, 1, 1 ], [ 0, 1, 1, 2 ], [ 0, 1, 2, 4 ], [ 1, 1, 2, 4 ], | |
719 | [ 0, 2, 2, 4 ], [ 1, 2, 2, 4 ], [ 2, 2, 2, 4 ], [ 1, 4, 2, 4 ], [ 2, 4, 2, 4 ], | |
720 | [ 2, 8, 3, 8 ], [ 3, 4, 3, 8 ], [ 3, 8, 3, 8 ], [ 0, 1, 1, 4 ], [ 0, 1, 2, 2 ], | |
721 | [ 0, 2, 1, 4 ], [ 1, 1, 2, 2 ], [ 1, 4, 2, 8 ], [ 2, 2, 3, 4 ], [ 2, 4, 3, 8 ], | |
722 | [ 0, 1, 3, 8 ], [ 1, 2, 3, 8 ], [ 2, 4, 2, 4 ], [ 2, 4, 3, 8 ], [ 3, 8, 3, 8 ] | |
723 | ]; |