]> git.nihav.org Git - nihav.git/blame - nihav-indeo/src/codecs/ivi.rs
avimux: do not record palette change chunks in OpenDML index
[nihav.git] / nihav-indeo / src / codecs / ivi.rs
CommitLineData
01c971c5
KS
1use super::ivibr::{IVICodebook,IVI_CB_ZERO,RVMap,IVI_ZERO_RVMAP,IVI_RVMAPS};
2
3pub fn clip8(a: i16) -> u8 {
4 if a < 0 { 0 }
5 else if a > 255 { 255 }
6 else { a as u8 }
7}
8
9#[derive(Debug,Clone,Copy,PartialEq)]
10pub enum IVIFrameType {
11 Intra,
12 Inter,
13 Bidir,
14 Intra1,
15 InterDroppable,
16 InterScal,
17 NULL,
18 NULL2,
19}
20
21impl IVIFrameType {
22 pub fn is_intra(self) -> bool {
23 (self == IVIFrameType::Intra) || (self == IVIFrameType::Intra1)
24 }
25 pub fn is_null(self) -> bool {
26 (self == IVIFrameType::NULL) || (self == IVIFrameType::NULL2)
27 }
28 pub fn is_bidir(self) -> bool {
29 self == IVIFrameType::Bidir
30 }
31}
32
33#[derive(Clone,Copy)]
34pub struct PictureHeader {
35 pub ftype: IVIFrameType,
36 pub width: usize,
37 pub height: usize,
38 pub slice_w: usize,
39 pub slice_h: usize,
40 pub transparent: bool,
41 pub luma_bands: usize,
42 pub chroma_bands: usize,
43 pub in_q: bool,
44}
45
46impl PictureHeader {
47 pub fn new(ftype: IVIFrameType, width: usize, height: usize, slice_w: usize, slice_h: usize, transparent: bool, luma_bands: usize, chroma_bands: usize, in_q: bool) -> Self {
48 PictureHeader {
f2af8eca
KS
49 ftype,
50 width, height, slice_w, slice_h,
51 transparent,
52 luma_bands, chroma_bands,
53 in_q,
01c971c5
KS
54 }
55 }
56 pub fn new_null(ftype: IVIFrameType) -> Self {
57 PictureHeader {
f2af8eca 58 ftype,
01c971c5
KS
59 width: 0, height: 0, slice_w: 0, slice_h: 0,
60 transparent: false,
61 luma_bands: 0, chroma_bands: 0,
62 in_q: false,
63 }
64 }
65}
66
67#[derive(Debug,Clone,Copy,PartialEq)]
68pub enum TSize {
69 T8x8,
70 T4x4,
71}
72
73#[derive(Debug,Clone,Copy,PartialEq)]
74pub enum TDir {
75 TwoD,
76 Row,
77 Col,
78}
79
80#[derive(Debug,Clone,Copy,PartialEq)]
81pub enum IVITransformType {
82 Haar (TSize, TDir),
83 Slant(TSize, TDir),
84 DCT (TSize, TDir),
85 None (TSize),
86}
87
88pub type TrFunc = fn (&mut [i32; 64]);
89pub type TrFuncDC = fn (&mut [i32; 64], i32);
90
91impl IVITransformType {
f2af8eca
KS
92 pub fn is_8x8(self) -> bool {
93 match self {
01c971c5
KS
94 IVITransformType::Haar (ref sz, _) => { *sz == TSize::T8x8 },
95 IVITransformType::Slant(ref sz, _) => { *sz == TSize::T8x8 },
96 IVITransformType::DCT (ref sz, _) => { *sz == TSize::T8x8 },
97 IVITransformType::None (ref sz) => { *sz == TSize::T8x8 },
98 }
99 }
f2af8eca
KS
100 pub fn is_2d(self) -> bool {
101 match self {
01c971c5
KS
102 IVITransformType::Haar (_, ref dir) => { *dir == TDir::TwoD },
103 IVITransformType::Slant(_, ref dir) => { *dir == TDir::TwoD },
104 IVITransformType::DCT (_, ref dir) => { *dir == TDir::TwoD },
105 _ => { false },
106 }
107 }
108}
109
01c971c5
KS
110#[derive(Clone)]
111pub struct TxParams4x4 {
112 pub quant_intra: &'static [u16; 16],
113 pub quant_inter: &'static [u16; 16],
114 pub scan: &'static [usize; 16],
115}
116
117impl TxParams4x4 {
118 pub fn new(quant_intra: &'static [u16; 16], quant_inter: &'static [u16; 16], scan: &'static [usize; 16]) -> Self {
119 TxParams4x4 {
f2af8eca 120 quant_intra, quant_inter, scan,
01c971c5
KS
121 }
122 }
123}
124
01c971c5
KS
125#[derive(Clone)]
126pub struct TxParams8x8 {
127 pub quant_intra: &'static [u16; 64],
128 pub quant_inter: &'static [u16; 64],
129 pub scan: &'static [usize; 64],
130}
131
132impl TxParams8x8 {
133 pub fn new(quant_intra: &'static [u16; 64], quant_inter: &'static [u16; 64], scan: &'static [usize; 64]) -> Self {
134 TxParams8x8 {
f2af8eca 135 quant_intra, quant_inter, scan,
01c971c5
KS
136 }
137 }
138}
139
140#[derive(Clone)]
141pub enum TxType {
142 Transform4(TxParams4x4),
143 Transform8(TxParams8x8),
144 None,
145}
146
147pub const CORR_MAP_SIZE: usize = 122;
148
149#[derive(Clone)]
802930e6 150#[allow(dead_code)]
01c971c5
KS
151pub struct BandHeader {
152 pub plane_no: usize,
153 pub band_no: usize,
154 pub empty: bool,
155 pub mb_size: usize,
156 pub blk_size: usize,
157 pub halfpel: bool,
158 pub inherit_mv: bool,
159 pub has_qdelta: bool,
160 pub inherit_qd: bool,
161 pub quant: u32,
162 pub blk_cb: IVICodebook,
163 pub rvmap: RVMap,
164 pub tr: IVITransformType,
165 pub ttype: TxType,
166}
167
168impl BandHeader {
39cd2175 169 #[allow(clippy::identity_op)]
01c971c5
KS
170 pub fn new(plane_no: usize, band_no: usize, mb_size: usize, blk_size: usize, halfpel: bool, inherit_mv: bool, has_qdelta: bool, inherit_qd: bool, quant: u32, rvmap_idx: usize, num_corr: usize, corr_map: [u8; CORR_MAP_SIZE], blk_cb: IVICodebook, tr: IVITransformType, ttype: TxType) -> Self {
171 let mut rvmap = IVI_RVMAPS[rvmap_idx].clone();
172 for i in 0..num_corr {
173 let pos1 = corr_map[i * 2 + 0] as usize;
174 let pos2 = corr_map[i * 2 + 1] as usize;
f2af8eca
KS
175 rvmap.runtab.swap(pos1, pos2);
176 rvmap.valtab.swap(pos1, pos2);
01c971c5
KS
177 }
178 BandHeader {
f2af8eca
KS
179 plane_no, band_no,
180 empty: false, halfpel,
181 inherit_mv,
182 has_qdelta, inherit_qd, quant,
183 mb_size, blk_size,
184 rvmap, blk_cb,
185 tr, ttype,
01c971c5
KS
186 }
187 }
188 pub fn new_empty(plane_no: usize, band_no: usize) -> Self {
189 BandHeader {
f2af8eca 190 plane_no, band_no,
01c971c5
KS
191 empty: true, halfpel: true,
192 inherit_mv: false, has_qdelta: false, inherit_qd: false, quant: 0,
193 mb_size: 0, blk_size: 0,
194 rvmap: IVI_ZERO_RVMAP, blk_cb: IVI_CB_ZERO,
195 tr: IVITransformType::None(TSize::T8x8), ttype: TxType::None,
196 }
197 }
198}
199
200#[derive(Debug,Clone,Copy,PartialEq)]
201pub enum MBType {
202 Intra,
203 Inter,
204 Backward,
205 Bidir,
206}
207
208#[derive(Clone,Copy)]
802930e6 209#[allow(dead_code)]
01c971c5
KS
210pub struct MB {
211 pub mtype: MBType,
212 pub pos_x: usize,
213 pub pos_y: usize,
214 pub mv_x: i32,
215 pub mv_y: i32,
216 pub mv2_x: i32,
217 pub mv2_y: i32,
218 pub qd: i16,
219 pub q: u8,
220 pub cbp: u8,
221}
222
223impl MB {
224 pub fn new(x: usize, y: usize) -> Self {
225 MB {
226 mtype: MBType::Intra,
227 pos_x: x, pos_y: y,
228 mv_x: 0, mv_y: 0,
229 mv2_x: 0, mv2_y: 0,
230 cbp: 0, q: 0, qd: 0,
231 }
232 }
233}
234
235pub struct IVITile {
236 pub pos_x: usize,
237 pub pos_y: usize,
238 pub mb_w: usize,
239 pub mb_h: usize,
240 pub w: usize,
241 pub h: usize,
242 pub mb: Vec<MB>,
243}
244
245impl IVITile {
246 pub fn new(pos_x: usize, pos_y: usize, w: usize, h: usize) -> Self {
247 IVITile {
f2af8eca 248 pos_x, pos_y, w, h,
01c971c5
KS
249 mb_w: 0, mb_h: 0, mb: Vec::new(),
250 }
251 }
252}
253
01c971c5
KS
254pub const IVI_SCAN_8X8_VER: [usize; 64] = [
255 0, 8, 16, 24, 32, 40, 48, 56,
256 1, 9, 17, 25, 33, 41, 49, 57,
257 2, 10, 18, 26, 34, 42, 50, 58,
258 3, 11, 19, 27, 35, 43, 51, 59,
259 4, 12, 20, 28, 36, 44, 52, 60,
260 5, 13, 21, 29, 37, 45, 53, 61,
261 6, 14, 22, 30, 38, 46, 54, 62,
262 7, 15, 23, 31, 39, 47, 55, 63
263];
264pub const IVI_SCAN_8X8_HOR: [usize; 64] = [
265 0, 1, 2, 3, 4, 5, 6, 7,
266 8, 9, 10, 11, 12, 13, 14, 15,
267 16, 17, 18, 19, 20, 21, 22, 23,
268 24, 25, 26, 27, 28, 29, 30, 31,
269 32, 33, 34, 35, 36, 37, 38, 39,
270 40, 41, 42, 43, 44, 45, 46, 47,
271 48, 49, 50, 51, 52, 53, 54, 55,
272 56, 57, 58, 59, 60, 61, 62, 63
273];
274pub const IVI_SCAN_4X4: [usize; 16] = [ 0, 1, 4, 8, 5, 2, 3, 6, 9, 12, 13, 10, 7, 11, 14, 15 ];