forge
Explore
Log in

Don't you dare go Hollow

edith_codecs

Clone
★ 0

0 watching0 forksRust370.3 MiB9b67700Tahinli3 days ago4021 commits

ops.rs205 lines, 5.4 KiBHistoryRawDownload
1//! ETSI-style saturating basic operators (port of the 3GPP TS 26.173 basic_op set).
2#![allow(dead_code)]
3
4pub const MAX_16: i16 = 32767;
5pub const MIN_16: i16 = -32768;
6pub const MAX_32: i32 = 0x7fff_ffff;
7pub const MIN_32: i32 = i32::MIN;
8
9#[inline]
10pub fn sat16(x: i32) -> i16 {
11 x.clamp(-32768, 32767) as i16
12}
13
14#[inline]
15pub fn add_int16(a: i16, b: i16) -> i16 {
16 sat16(a as i32 + b as i32)
17}
18#[inline]
19pub fn sub_int16(a: i16, b: i16) -> i16 {
20 sat16(a as i32 - b as i32)
21}
22#[inline]
23pub fn mult_int16(a: i16, b: i16) -> i16 {
24 sat16((a as i32 * b as i32) >> 15)
25}
26#[inline]
27pub fn negate_int16(a: i16) -> i16 {
28 if a == MIN_16 { MAX_16 } else { -a }
29}
30#[inline]
31pub fn add_int32(a: i32, b: i32) -> i32 {
32 a.saturating_add(b)
33}
34#[inline]
35pub fn sub_int32(a: i32, b: i32) -> i32 {
36 a.saturating_sub(b)
37}
38/// L_mult: (a*b)<<1 with saturation of the single overflow case.
39#[inline]
40pub fn mul_16by16_to_int32(a: i16, b: i16) -> i32 {
41 let m = a as i32 * b as i32;
42 if m != 0x4000_0000 { m << 1 } else { MAX_32 }
43}
44/// L_mac
45#[inline]
46pub fn mac_16by16_to_int32(acc: i32, a: i16, b: i16) -> i32 {
47 acc.saturating_add(mul_16by16_to_int32(a, b))
48}
49/// L_msu
50#[inline]
51pub fn msu_16by16_from_int32(acc: i32, a: i16, b: i16) -> i32 {
52 acc.saturating_sub(mul_16by16_to_int32(a, b))
53}
54/// round(): extract_h(L + 0x8000) with the C code's special-case for MAX_32.
55#[inline]
56pub fn amr_wb_round(l: i32) -> i16 {
57 let l = if l != MAX_32 {
58 l.wrapping_add(0x8000)
59 } else {
60 l
61 };
62 (l >> 16) as i16
63}
64#[inline]
65pub fn amr_wb_shl1_round(l: i32) -> i16 {
66 if (l.wrapping_shl(1)) >> 1 == l {
67 ((l.wrapping_add(0x4000)) >> 15) as i16
68 } else {
69 (((l >> 31) ^ MAX_32) >> 16) as i16
70 }
71}
72#[inline]
73pub fn mul_32by16(hi: i16, lo: i16, n: i16) -> i32 {
74 (((hi as i32 * n as i32) + ((lo as i32 * n as i32) >> 15)) as i32).wrapping_shl(1)
75}
76/// plain (non saturating) 16x16 -> 32 multiply-accumulate
77#[inline]
78pub fn fxp_mac_16by16(a: i16, b: i16, acc: i32) -> i32 {
79 acc.wrapping_add(a as i32 * b as i32)
80}
81#[inline]
82pub fn fxp_mul_16by16(a: i16, b: i16) -> i32 {
83 a as i32 * b as i32
84}
85/// (l1 * (l2<<16)) >> 32 ; l2 is a 16-bit value passed as i32 (C passes int32 and shifts it).
86#[inline]
87pub fn fxp_mul32_by_16b(l1: i32, l2: i32) -> i32 {
88 (((l1 as i64) * ((l2.wrapping_shl(16)) as i64)) >> 32) as i32
89}
90/// shl for 16-bit, negative shift = arithmetic right (count masked & 0xf as in the C code).
91#[inline]
92pub fn shl_int16(v: i16, s: i16) -> i16 {
93 if s < 0 {
94 let s = ((-(s as i32)) & 0xf) as u32;
95 v >> s
96 } else {
97 let s = (s & 0xf) as u32;
98 let out = ((v as i32) << s) as i16;
99 if ((out as i32) >> s) != v as i32 {
100 ((v >> 15) ^ MAX_16) as i16
101 } else {
102 out
103 }
104 }
105}
106#[inline]
107pub fn shl_int32(v: i32, s: i16) -> i32 {
108 if s > 0 {
109 let s = (s as u32) & 31;
110 let out = v.wrapping_shl(s);
111 if (out >> s) != v {
112 (v >> 31) ^ MAX_32
113 } else {
114 out
115 }
116 } else {
117 let s = ((-(s as i32)) & 0xf) as u32;
118 v >> s
119 }
120}
121#[inline]
122pub fn shr_int32(v: i32, s: i16) -> i32 {
123 if s >= 0 {
124 v >> ((s as u32) & 0x1f)
125 } else {
126 let s = ((-(s as i32)) as u32) & 0x1f;
127 let out = v.wrapping_shl(s);
128 if (out >> s) != v {
129 (v >> 31) ^ MAX_32
130 } else {
131 out
132 }
133 }
134}
135
136/// normalize_amr_wb (C-equivalent, == norm_l for positive input).
137pub fn normalize_amr_wb(x: i32) -> i16 {
138 let mut i: i32 = if x > 0x0FFF_FFFF {
139 0
140 } else if x > 0x00FF_FFFF {
141 3
142 } else if x > 0x0000_FFFF {
143 if x > 0x000F_FFFF { 7 } else { 11 }
144 } else if x > 0x0000_00FF {
145 if x > 0x0000_0FFF { 15 } else { 19 }
146 } else if x > 0x0000_000F {
147 23
148 } else {
149 27
150 };
151 let xs = x.wrapping_shl(i as u32);
152 match xs & 0x7800_0000 {
153 0x0800_0000 => i += 3,
154 0x1800_0000 | 0x1000_0000 => i += 2,
155 0x2800_0000 | 0x2000_0000 | 0x3800_0000 | 0x3000_0000 => i += 1,
156 _ => {}
157 }
158 i as i16
159}
160#[inline]
161pub fn norm_l(x: i32) -> i16 {
162 normalize_amr_wb(x)
163}
164#[inline]
165pub fn norm_s(x: i16) -> i16 {
166 normalize_amr_wb(x as i32) - 16
167}
168
169// ---- codec constants (pvamrwbdecoder_cnst.h) ----
170pub const L_FRAME: usize = 256;
171pub const L_SUBFR16K: usize = 80;
172pub const L_SUBFR: usize = 64;
173pub const NB_SUBFR: usize = 4;
174pub const M: usize = 16;
175pub const M16K: usize = 20;
176pub const L_FILT16K: usize = 15;
177pub const L_FILT: usize = 12;
178pub const GP_CLIP: i16 = 15565;
179pub const PIT_SHARP: i16 = 27853;
180pub const PIT_MIN: i16 = 34;
181pub const PIT_FR2: i16 = 128;
182pub const PIT_FR1_9B: i16 = 160;
183pub const PIT_FR1_8B: i16 = 92;
184pub const PIT_MAX: i16 = 231;
185pub const L_INTERPOL: usize = 17;
186pub const PREEMPH_FAC: i16 = 22282;
187pub const GAMMA1: i16 = 30147;
188pub const TILT_FAC: i16 = 22282;
189pub const Q_MAX: i16 = 8;
190pub const RANDOM_INITSEED: i16 = 21845;
191pub const L_MEANBUF: usize = 3;
192pub const ONE_PER_MEANBUF: i16 = 10923;
193pub const EHF_MASK: i16 = 0x0008;
194pub const AMR_WB_PCM_FRAME: usize = 320;
195pub const NBBITS_7K: i16 = 132;
196pub const NBBITS_9K: i16 = 177;
197pub const NBBITS_12K: i16 = 253;
198pub const NBBITS_14K: i16 = 285;
199pub const NBBITS_16K: i16 = 317;
200pub const NBBITS_18K: i16 = 365;
201pub const NBBITS_20K: i16 = 397;
202pub const NBBITS_23K: i16 = 461;
203pub const NBBITS_24K: i16 = 477;
204pub const NBBITS_SID: i16 = 35;
205pub const AMR_WB_COMPRESSED: [i16; 10] = [132, 177, 253, 285, 317, 365, 397, 461, 477, 35];