1use alloc::vec;
2use alloc::vec::Vec;
3use core::mem;
4use crate::decoder::MAX_COMPONENTS;
5use crate::error::Result;
6use crate::idct::dequantize_and_idct_block;
7use crate::alloc::sync::Arc;
8use crate::parser::Component;
9use super::{RowData, Worker};
10
11pub struct ImmediateWorker {
12 offsets: [usize; MAX_COMPONENTS],
13 results: Vec<Vec<u8>>,
14 components: Vec<Option<Component>>,
15 quantization_tables: Vec<Option<Arc<[u16; 64]>>>,
16}
17
18impl Default for ImmediateWorker {
19 fn default() -> Self {
20 ImmediateWorker {
21 offsets: [0; MAX_COMPONENTS],
22 results: vec![Vec::new(); MAX_COMPONENTS],
23 components: vec![None; MAX_COMPONENTS],
24 quantization_tables: vec![None; MAX_COMPONENTS],
25 }
26 }
27}
28
29impl ImmediateWorker {
30 pub fn start_immediate(&mut self, data: RowData) {
31 assert!(self.results[data.index].is_empty());
32
33 self.offsets[data.index] = 0;
34 self.results[data.index].resize(data.component.block_size.width as usize * data.component.block_size.height as usize * data.component.dct_scale * data.component.dct_scale, 0u8);
35 self.components[data.index] = Some(data.component);
36 self.quantization_tables[data.index] = Some(data.quantization_table);
37 }
38
39 pub fn append_row_immediate(&mut self, (index, data): (usize, Vec<i16>)) {
40 // Convert coefficients from a MCU row to samples.
41
42 let component = self.components[index].as_ref().unwrap();
43 let quantization_table = self.quantization_tables[index].as_ref().unwrap();
44 let block_count = component.block_size.width as usize * component.vertical_sampling_factor as usize;
45 let line_stride = component.block_size.width as usize * component.dct_scale;
46
47 assert_eq!(data.len(), block_count * 64);
48
49 for i in 0..block_count {
50 let x = (i % component.block_size.width as usize) * component.dct_scale;
51 let y = (i / component.block_size.width as usize) * component.dct_scale;
52
53 let coefficients = data[i * 64..(i + 1) * 64].try_into().unwrap();
54 let output = &mut self.results[index][self.offsets[index] + y * line_stride + x..];
55
56 dequantize_and_idct_block(component.dct_scale, coefficients, quantization_table, line_stride, output);
57 }
58
59 self.offsets[index] += block_count * component.dct_scale * component.dct_scale;
60 }
61
62 pub fn get_result_immediate(&mut self, index: usize) -> Vec<u8> {
63 mem::take(&mut self.results[index])
64 }
65}
66
67impl Worker for ImmediateWorker {
68 fn start(&mut self, data: RowData) -> Result<()> {
69 self.start_immediate(data);
70 Ok(())
71 }
72 fn append_row(&mut self, row: (usize, Vec<i16>)) -> Result<()> {
73 self.append_row_immediate(row);
74 Ok(())
75 }
76 fn get_result(&mut self, index: usize) -> Result<Vec<u8>> {
77 Ok(self.get_result_immediate(index))
78 }
79}
80