1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
use nihav_core::frame::*;
use nihav_core::soundcvt::*;
struct AudioQueue<T> {
start: usize,
end: usize,
stride: usize,
channels: usize,
data: Vec<T>,
ileaved: bool,
}
impl<T:Clone+Copy+From<u8>> AudioQueue<T> {
fn new(channels: usize, rec_size: usize, ileaved: bool) -> Self {
Self {
start: 0,
end: 0,
stride: if ileaved { rec_size * channels } else { rec_size },
channels,
ileaved,
data: vec![0.into(); rec_size * channels],
}
}
fn get_cur_size(&self) -> usize { self.end - self.start }
fn get_cur_avail(&self) -> usize { self.stride - self.end }
fn get_potentially_avail(&self) -> usize { self.stride - self.get_cur_size() }
fn read(&mut self, src: &NAAudioBuffer<T>) {
let mut to_copy = src.get_length();
if self.ileaved {
to_copy *= self.channels;
}
if self.get_cur_avail() < to_copy {
if self.get_potentially_avail() >= to_copy {
self.renorm();
} else {
let new_len = (self.stride * 2).max(self.get_cur_size() + src.get_length());
let mut new_buf = vec![0.into(); new_len * self.channels];
let new_stride = if !self.ileaved { new_len } else { new_len * self.channels };
let old_len = self.get_cur_size();
let new_len = src.get_length();
for (dst, (old, new)) in new_buf.chunks_exact_mut(new_stride).zip(
self.data.chunks_exact(self.stride).zip(
src.get_data().chunks(src.get_stride()))) {
dst[..old_len].copy_from_slice(&old[self.start..self.end]);
dst[old_len..][..new_len].copy_from_slice(&new[..new_len]);
}
self.data = new_buf;
self.stride = new_stride;
self.start = 0;
self.end = old_len + new_len;
return;
}
}
for (dst, src) in self.data.chunks_exact_mut(self.stride).zip(src.get_data().chunks_exact(src.get_stride())) {
dst[self.end..][..to_copy].copy_from_slice(&src[..to_copy]);
}
self.end += to_copy;
}
fn write(&mut self, dbuf: &mut NAAudioBuffer<T>) {
let dst_len = dbuf.get_length();
let dst_stride = dbuf.get_stride();
let dst = dbuf.get_data_mut().unwrap();
for (dst, src) in dst.chunks_mut(dst_stride).zip(self.data.chunks_exact(self.stride)) {
dst[..dst_len].copy_from_slice(&src[self.start..][..dst_len]);
}
self.start += dst_len;
}
fn renorm(&mut self) {
if self.start == 0 {
return;
}
let move_size = self.end - self.start;
if move_size > 0 {
for chan in self.data.chunks_exact_mut(self.stride) {
for i in 0..move_size {
chan[i] = chan[self.start + i];
}
}
}
self.end -= self.start;
self.start = 0;
}
}
enum AudioDataType {
U8(AudioQueue<u8>),
I16(AudioQueue<i16>),
I32(AudioQueue<i32>),
F32(AudioQueue<f32>),
Packed(AudioQueue<u8>),
}
impl AudioDataType {
fn get_length(&self) -> usize {
match self {
AudioDataType::U8(ref queue) => queue.get_cur_size(),
AudioDataType::I16(ref queue) => queue.get_cur_size(),
AudioDataType::I32(ref queue) => queue.get_cur_size(),
AudioDataType::F32(ref queue) => queue.get_cur_size(),
AudioDataType::Packed(ref queue) => queue.get_cur_size(),
}
}
}
pub struct AudioConverter {
queue: AudioDataType,
dst_fmt: NAAudioInfo,
dst_chmap: NAChannelMap,
apts: Option<u64>,
}
impl AudioConverter {
pub fn new(_sinfo: &NAAudioInfo, dinfo: &NAAudioInfo, dst_chmap: NAChannelMap) -> Self {
let ch = usize::from(dinfo.channels);
let size = dinfo.block_len * 2;
let il = !dinfo.format.planar;
let queue = match (dinfo.format.bits, dinfo.format.float, dinfo.format.signed) {
( 8, false, false) => AudioDataType::U8(AudioQueue::new(ch, size, il)),
(16, false, true) => AudioDataType::I16(AudioQueue::new(ch, size, il)),
(32, false, true) => AudioDataType::I32(AudioQueue::new(ch, size, il)),
(32, true, _) => AudioDataType::F32(AudioQueue::new(ch, size, il)),
_ => AudioDataType::Packed(AudioQueue::new(ch, size, il)),
};
Self {
queue,
dst_fmt: *dinfo,
dst_chmap,
apts: None,
}
}
pub fn queue_frame(&mut self, buf: NABufferType, tinfo: NATimeInfo) -> bool {
let ret = convert_audio_frame(&buf, &self.dst_fmt, &self.dst_chmap);
if let Ok(dbuf) = ret {
if self.apts.is_none() && tinfo.get_pts().is_some() {
self.apts = tinfo.get_pts();
}
match (&mut self.queue, dbuf) {
(AudioDataType::U8(ref mut queue), NABufferType::AudioU8(ref buf)) => queue.read(buf),
(AudioDataType::I16(ref mut queue), NABufferType::AudioI16(ref buf)) => queue.read(buf),
(AudioDataType::I32(ref mut queue), NABufferType::AudioI32(ref buf)) => queue.read(buf),
(AudioDataType::F32(ref mut queue), NABufferType::AudioF32(ref buf)) => queue.read(buf),
(AudioDataType::Packed(ref mut queue), NABufferType::AudioPacked(ref buf)) => queue.read(buf),
_ => unimplemented!(),
};
true
} else {
false
}
}
pub fn get_frame(&mut self, info: NACodecInfoRef) -> Option<NAFrame> {
if self.queue.get_length() >= self.dst_fmt.block_len {
if let Ok(mut abuf) = alloc_audio_buffer(self.dst_fmt, self.dst_fmt.block_len, self.dst_chmap.clone()) {
match (&mut self.queue, &mut abuf) {
(AudioDataType::U8(ref mut queue), NABufferType::AudioU8(ref mut buf)) => queue.write(buf),
(AudioDataType::I16(ref mut queue), NABufferType::AudioI16(ref mut buf)) => queue.write(buf),
(AudioDataType::I32(ref mut queue), NABufferType::AudioI32(ref mut buf)) => queue.write(buf),
(AudioDataType::F32(ref mut queue), NABufferType::AudioF32(ref mut buf)) => queue.write(buf),
(AudioDataType::Packed(ref mut queue), NABufferType::AudioPacked(ref mut buf)) => queue.write(buf),
_ => unimplemented!(),
};
let tinfo = NATimeInfo::new(self.apts, None, Some(self.dst_fmt.block_len as u64), 1, self.dst_fmt.sample_rate);
if let Some(ref mut val) = self.apts {
*val += self.dst_fmt.block_len as u64;
}
Some(NAFrame::new(tinfo, FrameType::I, true, info, abuf))
} else {
println!(" failed to allocate audio frame");
None
}
} else {
None
}
}
}
|