core/codecs: document blockdsp
[nihav.git] / nihav-core / src / frame.rs
CommitLineData
22cb00db 1use std::cmp::max;
5869fd63 2use std::collections::HashMap;
83e603fa 3use std::fmt;
2422d969 4use std::sync::Arc;
4e8b4f31 5pub use crate::formats::*;
1a967e6b 6pub use crate::refs::*;
94dbb551 7
5869fd63 8#[allow(dead_code)]
66116504 9#[derive(Clone,Copy,PartialEq)]
5869fd63 10pub struct NAAudioInfo {
df159213
KS
11 pub sample_rate: u32,
12 pub channels: u8,
13 pub format: NASoniton,
14 pub block_len: usize,
5869fd63
KS
15}
16
17impl NAAudioInfo {
18 pub fn new(sr: u32, ch: u8, fmt: NASoniton, bl: usize) -> Self {
19 NAAudioInfo { sample_rate: sr, channels: ch, format: fmt, block_len: bl }
20 }
66116504
KS
21 pub fn get_sample_rate(&self) -> u32 { self.sample_rate }
22 pub fn get_channels(&self) -> u8 { self.channels }
23 pub fn get_format(&self) -> NASoniton { self.format }
24 pub fn get_block_len(&self) -> usize { self.block_len }
5869fd63
KS
25}
26
83e603fa
KS
27impl fmt::Display for NAAudioInfo {
28 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
29 write!(f, "{} Hz, {} ch", self.sample_rate, self.channels)
30 }
31}
32
5869fd63 33#[allow(dead_code)]
66116504 34#[derive(Clone,Copy,PartialEq)]
5869fd63 35pub struct NAVideoInfo {
66116504
KS
36 width: usize,
37 height: usize,
5869fd63
KS
38 flipped: bool,
39 format: NAPixelFormaton,
40}
41
42impl NAVideoInfo {
66116504 43 pub fn new(w: usize, h: usize, flip: bool, fmt: NAPixelFormaton) -> Self {
5869fd63
KS
44 NAVideoInfo { width: w, height: h, flipped: flip, format: fmt }
45 }
66116504
KS
46 pub fn get_width(&self) -> usize { self.width as usize }
47 pub fn get_height(&self) -> usize { self.height as usize }
48 pub fn is_flipped(&self) -> bool { self.flipped }
49 pub fn get_format(&self) -> NAPixelFormaton { self.format }
dd1b60e1
KS
50 pub fn set_width(&mut self, w: usize) { self.width = w; }
51 pub fn set_height(&mut self, h: usize) { self.height = h; }
5869fd63
KS
52}
53
83e603fa
KS
54impl fmt::Display for NAVideoInfo {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 write!(f, "{}x{}", self.width, self.height)
57 }
58}
59
66116504 60#[derive(Clone,Copy,PartialEq)]
5869fd63
KS
61pub enum NACodecTypeInfo {
62 None,
63 Audio(NAAudioInfo),
64 Video(NAVideoInfo),
65}
66
22cb00db
KS
67impl NACodecTypeInfo {
68 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
69 match *self {
70 NACodecTypeInfo::Video(vinfo) => Some(vinfo),
71 _ => None,
72 }
73 }
74 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
75 match *self {
76 NACodecTypeInfo::Audio(ainfo) => Some(ainfo),
77 _ => None,
78 }
79 }
5076115b
KS
80 pub fn is_video(&self) -> bool {
81 match *self {
82 NACodecTypeInfo::Video(_) => true,
83 _ => false,
84 }
85 }
86 pub fn is_audio(&self) -> bool {
87 match *self {
88 NACodecTypeInfo::Audio(_) => true,
89 _ => false,
90 }
91 }
22cb00db
KS
92}
93
83e603fa
KS
94impl fmt::Display for NACodecTypeInfo {
95 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
96 let ret = match *self {
e243ceb4 97 NACodecTypeInfo::None => "".to_string(),
83e603fa
KS
98 NACodecTypeInfo::Audio(fmt) => format!("{}", fmt),
99 NACodecTypeInfo::Video(fmt) => format!("{}", fmt),
100 };
101 write!(f, "{}", ret)
102 }
103}
104
22cb00db
KS
105#[derive(Clone)]
106pub struct NAVideoBuffer<T> {
6c8e5c40 107 info: NAVideoInfo,
1a967e6b 108 data: NABufferRef<Vec<T>>,
6c8e5c40
KS
109 offs: Vec<usize>,
110 strides: Vec<usize>,
22cb00db
KS
111}
112
113impl<T: Clone> NAVideoBuffer<T> {
114 pub fn get_offset(&self, idx: usize) -> usize {
115 if idx >= self.offs.len() { 0 }
116 else { self.offs[idx] }
117 }
118 pub fn get_info(&self) -> NAVideoInfo { self.info }
1a967e6b
KS
119 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
120 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
b914ee01 121 pub fn get_num_components(&self) -> usize { self.offs.len() }
22cb00db 122 pub fn copy_buffer(&mut self) -> Self {
1a967e6b
KS
123 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
124 data.clone_from(self.data.as_ref());
22cb00db
KS
125 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
126 offs.clone_from(&self.offs);
6c8e5c40
KS
127 let mut strides: Vec<usize> = Vec::with_capacity(self.strides.len());
128 strides.clone_from(&self.strides);
e243ceb4 129 NAVideoBuffer { info: self.info, data: NABufferRef::new(data), offs, strides }
22cb00db
KS
130 }
131 pub fn get_stride(&self, idx: usize) -> usize {
6c8e5c40
KS
132 if idx >= self.strides.len() { return 0; }
133 self.strides[idx]
22cb00db
KS
134 }
135 pub fn get_dimensions(&self, idx: usize) -> (usize, usize) {
136 get_plane_size(&self.info, idx)
137 }
3fc28ece
KS
138 pub fn into_ref(self) -> NABufferRef<Self> {
139 NABufferRef::new(self)
140 }
22cb00db
KS
141}
142
3fc28ece
KS
143pub type NAVideoBufferRef<T> = NABufferRef<NAVideoBuffer<T>>;
144
22cb00db
KS
145#[derive(Clone)]
146pub struct NAAudioBuffer<T> {
147 info: NAAudioInfo,
1a967e6b 148 data: NABufferRef<Vec<T>>,
22cb00db 149 offs: Vec<usize>,
01e2d496 150 stride: usize,
22cb00db 151 chmap: NAChannelMap,
5076115b 152 len: usize,
22cb00db
KS
153}
154
155impl<T: Clone> NAAudioBuffer<T> {
156 pub fn get_offset(&self, idx: usize) -> usize {
157 if idx >= self.offs.len() { 0 }
158 else { self.offs[idx] }
159 }
01e2d496 160 pub fn get_stride(&self) -> usize { self.stride }
22cb00db 161 pub fn get_info(&self) -> NAAudioInfo { self.info }
8ee4352b 162 pub fn get_chmap(&self) -> &NAChannelMap { &self.chmap }
1a967e6b
KS
163 pub fn get_data(&self) -> &Vec<T> { self.data.as_ref() }
164 pub fn get_data_mut(&mut self) -> Option<&mut Vec<T>> { self.data.as_mut() }
22cb00db 165 pub fn copy_buffer(&mut self) -> Self {
1a967e6b
KS
166 let mut data: Vec<T> = Vec::with_capacity(self.data.len());
167 data.clone_from(self.data.as_ref());
22cb00db
KS
168 let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
169 offs.clone_from(&self.offs);
8ee4352b 170 NAAudioBuffer { info: self.info, data: NABufferRef::new(data), offs, chmap: self.get_chmap().clone(), len: self.len, stride: self.stride }
22cb00db 171 }
5076115b 172 pub fn get_length(&self) -> usize { self.len }
22cb00db
KS
173}
174
87a1ebc3 175impl NAAudioBuffer<u8> {
1a967e6b
KS
176 pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
177 let len = data.len();
01e2d496 178 NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0 }
87a1ebc3
KS
179 }
180}
181
22cb00db
KS
182#[derive(Clone)]
183pub enum NABufferType {
3fc28ece
KS
184 Video (NAVideoBufferRef<u8>),
185 Video16 (NAVideoBufferRef<u16>),
186 Video32 (NAVideoBufferRef<u32>),
187 VideoPacked(NAVideoBufferRef<u8>),
22cb00db
KS
188 AudioU8 (NAAudioBuffer<u8>),
189 AudioI16 (NAAudioBuffer<i16>),
87a1ebc3 190 AudioI32 (NAAudioBuffer<i32>),
22cb00db
KS
191 AudioF32 (NAAudioBuffer<f32>),
192 AudioPacked(NAAudioBuffer<u8>),
1a967e6b 193 Data (NABufferRef<Vec<u8>>),
22cb00db
KS
194 None,
195}
196
197impl NABufferType {
198 pub fn get_offset(&self, idx: usize) -> usize {
199 match *self {
200 NABufferType::Video(ref vb) => vb.get_offset(idx),
201 NABufferType::Video16(ref vb) => vb.get_offset(idx),
3bba1c4a 202 NABufferType::Video32(ref vb) => vb.get_offset(idx),
22cb00db
KS
203 NABufferType::VideoPacked(ref vb) => vb.get_offset(idx),
204 NABufferType::AudioU8(ref ab) => ab.get_offset(idx),
205 NABufferType::AudioI16(ref ab) => ab.get_offset(idx),
206 NABufferType::AudioF32(ref ab) => ab.get_offset(idx),
207 NABufferType::AudioPacked(ref ab) => ab.get_offset(idx),
208 _ => 0,
209 }
210 }
3bba1c4a
KS
211 pub fn get_video_info(&self) -> Option<NAVideoInfo> {
212 match *self {
213 NABufferType::Video(ref vb) => Some(vb.get_info()),
214 NABufferType::Video16(ref vb) => Some(vb.get_info()),
215 NABufferType::Video32(ref vb) => Some(vb.get_info()),
216 NABufferType::VideoPacked(ref vb) => Some(vb.get_info()),
217 _ => None,
218 }
219 }
3fc28ece 220 pub fn get_vbuf(&self) -> Option<NAVideoBufferRef<u8>> {
22cb00db
KS
221 match *self {
222 NABufferType::Video(ref vb) => Some(vb.clone()),
87a1ebc3
KS
223 NABufferType::VideoPacked(ref vb) => Some(vb.clone()),
224 _ => None,
225 }
226 }
3fc28ece 227 pub fn get_vbuf16(&self) -> Option<NAVideoBufferRef<u16>> {
87a1ebc3
KS
228 match *self {
229 NABufferType::Video16(ref vb) => Some(vb.clone()),
230 _ => None,
231 }
232 }
3fc28ece 233 pub fn get_vbuf32(&self) -> Option<NAVideoBufferRef<u32>> {
3bba1c4a
KS
234 match *self {
235 NABufferType::Video32(ref vb) => Some(vb.clone()),
236 _ => None,
237 }
238 }
049474a0
KS
239 pub fn get_audio_info(&self) -> Option<NAAudioInfo> {
240 match *self {
241 NABufferType::AudioU8(ref ab) => Some(ab.get_info()),
242 NABufferType::AudioI16(ref ab) => Some(ab.get_info()),
243 NABufferType::AudioI32(ref ab) => Some(ab.get_info()),
244 NABufferType::AudioF32(ref ab) => Some(ab.get_info()),
245 NABufferType::AudioPacked(ref ab) => Some(ab.get_info()),
246 _ => None,
247 }
248 }
249 pub fn get_chmap(&self) -> Option<&NAChannelMap> {
250 match *self {
251 NABufferType::AudioU8(ref ab) => Some(ab.get_chmap()),
252 NABufferType::AudioI16(ref ab) => Some(ab.get_chmap()),
253 NABufferType::AudioI32(ref ab) => Some(ab.get_chmap()),
254 NABufferType::AudioF32(ref ab) => Some(ab.get_chmap()),
255 NABufferType::AudioPacked(ref ab) => Some(ab.get_chmap()),
256 _ => None,
257 }
258 }
259 pub fn get_audio_length(&self) -> usize {
260 match *self {
261 NABufferType::AudioU8(ref ab) => ab.get_length(),
262 NABufferType::AudioI16(ref ab) => ab.get_length(),
263 NABufferType::AudioI32(ref ab) => ab.get_length(),
264 NABufferType::AudioF32(ref ab) => ab.get_length(),
265 NABufferType::AudioPacked(ref ab) => ab.get_length(),
266 _ => 0,
267 }
268 }
269 pub fn get_audio_stride(&self) -> usize {
270 match *self {
271 NABufferType::AudioU8(ref ab) => ab.get_stride(),
272 NABufferType::AudioI16(ref ab) => ab.get_stride(),
273 NABufferType::AudioI32(ref ab) => ab.get_stride(),
274 NABufferType::AudioF32(ref ab) => ab.get_stride(),
275 NABufferType::AudioPacked(ref ab) => ab.get_stride(),
276 _ => 0,
277 }
278 }
6e09a92e 279 pub fn get_abuf_u8(&self) -> Option<NAAudioBuffer<u8>> {
87a1ebc3
KS
280 match *self {
281 NABufferType::AudioU8(ref ab) => Some(ab.clone()),
282 NABufferType::AudioPacked(ref ab) => Some(ab.clone()),
283 _ => None,
284 }
285 }
6e09a92e 286 pub fn get_abuf_i16(&self) -> Option<NAAudioBuffer<i16>> {
87a1ebc3
KS
287 match *self {
288 NABufferType::AudioI16(ref ab) => Some(ab.clone()),
289 _ => None,
290 }
291 }
6e09a92e 292 pub fn get_abuf_i32(&self) -> Option<NAAudioBuffer<i32>> {
87a1ebc3
KS
293 match *self {
294 NABufferType::AudioI32(ref ab) => Some(ab.clone()),
295 _ => None,
296 }
297 }
6e09a92e 298 pub fn get_abuf_f32(&self) -> Option<NAAudioBuffer<f32>> {
87a1ebc3
KS
299 match *self {
300 NABufferType::AudioF32(ref ab) => Some(ab.clone()),
22cb00db
KS
301 _ => None,
302 }
303 }
304}
305
cd830591
KS
306const NA_SIMPLE_VFRAME_COMPONENTS: usize = 4;
307pub struct NASimpleVideoFrame<'a, T: Copy> {
308 pub width: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
309 pub height: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
310 pub flip: bool,
311 pub stride: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
312 pub offset: [usize; NA_SIMPLE_VFRAME_COMPONENTS],
313 pub components: usize,
dc45d8ce 314 pub data: &'a mut [T],
cd830591
KS
315}
316
317impl<'a, T:Copy> NASimpleVideoFrame<'a, T> {
318 pub fn from_video_buf(vbuf: &'a mut NAVideoBuffer<T>) -> Option<Self> {
319 let vinfo = vbuf.get_info();
320 let components = vinfo.format.components as usize;
321 if components > NA_SIMPLE_VFRAME_COMPONENTS {
322 return None;
323 }
324 let mut w: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
325 let mut h: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
326 let mut s: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
327 let mut o: [usize; NA_SIMPLE_VFRAME_COMPONENTS] = [0; NA_SIMPLE_VFRAME_COMPONENTS];
328 for comp in 0..components {
329 let (width, height) = vbuf.get_dimensions(comp);
330 w[comp] = width;
331 h[comp] = height;
332 s[comp] = vbuf.get_stride(comp);
333 o[comp] = vbuf.get_offset(comp);
334 }
335 let flip = vinfo.flipped;
336 Some(NASimpleVideoFrame {
337 width: w,
338 height: h,
339 flip,
340 stride: s,
341 offset: o,
342 components,
dc45d8ce 343 data: vbuf.data.as_mut_slice(),
cd830591
KS
344 })
345 }
346}
347
22cb00db
KS
348#[derive(Debug,Clone,Copy,PartialEq)]
349pub enum AllocatorError {
350 TooLargeDimensions,
351 FormatError,
352}
353
354pub fn alloc_video_buffer(vinfo: NAVideoInfo, align: u8) -> Result<NABufferType, AllocatorError> {
355 let fmt = &vinfo.format;
356 let mut new_size: usize = 0;
6c8e5c40
KS
357 let mut offs: Vec<usize> = Vec::new();
358 let mut strides: Vec<usize> = Vec::new();
22cb00db
KS
359
360 for i in 0..fmt.get_num_comp() {
361 if fmt.get_chromaton(i) == None { return Err(AllocatorError::FormatError); }
362 }
363
364 let align_mod = ((1 << align) as usize) - 1;
365 let width = ((vinfo.width as usize) + align_mod) & !align_mod;
366 let height = ((vinfo.height as usize) + align_mod) & !align_mod;
367 let mut max_depth = 0;
368 let mut all_packed = true;
3bba1c4a 369 let mut all_bytealigned = true;
22cb00db 370 for i in 0..fmt.get_num_comp() {
6c8e5c40 371 let ochr = fmt.get_chromaton(i);
e243ceb4 372 if ochr.is_none() { continue; }
6c8e5c40 373 let chr = ochr.unwrap();
22cb00db
KS
374 if !chr.is_packed() {
375 all_packed = false;
3bba1c4a
KS
376 } else if ((chr.get_shift() + chr.get_depth()) & 7) != 0 {
377 all_bytealigned = false;
22cb00db
KS
378 }
379 max_depth = max(max_depth, chr.get_depth());
380 }
3bba1c4a
KS
381 let unfit_elem_size = match fmt.get_elem_size() {
382 2 | 4 => false,
383 _ => true,
384 };
22cb00db
KS
385
386//todo semi-packed like NV12
bc6aac3d
KS
387 if fmt.is_paletted() {
388//todo various-sized palettes?
6c8e5c40
KS
389 let stride = vinfo.get_format().get_chromaton(0).unwrap().get_linesize(width);
390 let pic_sz = stride.checked_mul(height);
bc6aac3d
KS
391 if pic_sz == None { return Err(AllocatorError::TooLargeDimensions); }
392 let pal_size = 256 * (fmt.get_elem_size() as usize);
393 let new_size = pic_sz.unwrap().checked_add(pal_size);
394 if new_size == None { return Err(AllocatorError::TooLargeDimensions); }
395 offs.push(0);
6c8e5c40
KS
396 offs.push(stride * height);
397 strides.push(stride);
e243ceb4
KS
398 let data: Vec<u8> = vec![0; new_size.unwrap()];
399 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 400 Ok(NABufferType::Video(buf.into_ref()))
bc6aac3d 401 } else if !all_packed {
22cb00db 402 for i in 0..fmt.get_num_comp() {
6c8e5c40 403 let ochr = fmt.get_chromaton(i);
e243ceb4 404 if ochr.is_none() { continue; }
6c8e5c40 405 let chr = ochr.unwrap();
74afc7de 406 offs.push(new_size as usize);
6c8e5c40 407 let stride = chr.get_linesize(width);
22cb00db 408 let cur_h = chr.get_height(height);
6c8e5c40 409 let cur_sz = stride.checked_mul(cur_h);
22cb00db
KS
410 if cur_sz == None { return Err(AllocatorError::TooLargeDimensions); }
411 let new_sz = new_size.checked_add(cur_sz.unwrap());
412 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
413 new_size = new_sz.unwrap();
6c8e5c40 414 strides.push(stride);
22cb00db
KS
415 }
416 if max_depth <= 8 {
e243ceb4
KS
417 let data: Vec<u8> = vec![0; new_size];
418 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 419 Ok(NABufferType::Video(buf.into_ref()))
3bba1c4a 420 } else if max_depth <= 16 {
e243ceb4
KS
421 let data: Vec<u16> = vec![0; new_size];
422 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 423 Ok(NABufferType::Video16(buf.into_ref()))
3bba1c4a 424 } else {
e243ceb4
KS
425 let data: Vec<u32> = vec![0; new_size];
426 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 427 Ok(NABufferType::Video32(buf.into_ref()))
22cb00db 428 }
3bba1c4a 429 } else if all_bytealigned || unfit_elem_size {
22cb00db
KS
430 let elem_sz = fmt.get_elem_size();
431 let line_sz = width.checked_mul(elem_sz as usize);
432 if line_sz == None { return Err(AllocatorError::TooLargeDimensions); }
433 let new_sz = line_sz.unwrap().checked_mul(height);
434 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
435 new_size = new_sz.unwrap();
e243ceb4 436 let data: Vec<u8> = vec![0; new_size];
6c8e5c40 437 strides.push(line_sz.unwrap());
e243ceb4 438 let buf: NAVideoBuffer<u8> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 439 Ok(NABufferType::VideoPacked(buf.into_ref()))
3bba1c4a
KS
440 } else {
441 let elem_sz = fmt.get_elem_size();
442 let new_sz = width.checked_mul(height);
443 if new_sz == None { return Err(AllocatorError::TooLargeDimensions); }
444 new_size = new_sz.unwrap();
445 match elem_sz {
446 2 => {
e243ceb4 447 let data: Vec<u16> = vec![0; new_size];
3bba1c4a 448 strides.push(width);
e243ceb4 449 let buf: NAVideoBuffer<u16> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 450 Ok(NABufferType::Video16(buf.into_ref()))
3bba1c4a
KS
451 },
452 4 => {
e243ceb4 453 let data: Vec<u32> = vec![0; new_size];
3bba1c4a 454 strides.push(width);
e243ceb4 455 let buf: NAVideoBuffer<u32> = NAVideoBuffer { data: NABufferRef::new(data), info: vinfo, offs, strides };
3fc28ece 456 Ok(NABufferType::Video32(buf.into_ref()))
3bba1c4a
KS
457 },
458 _ => unreachable!(),
459 }
22cb00db
KS
460 }
461}
462
e243ceb4 463#[allow(clippy::collapsible_if)]
22cb00db
KS
464pub fn alloc_audio_buffer(ainfo: NAAudioInfo, nsamples: usize, chmap: NAChannelMap) -> Result<NABufferType, AllocatorError> {
465 let mut offs: Vec<usize> = Vec::new();
4c6c19cb 466 if ainfo.format.is_planar() || (ainfo.channels == 1 && (ainfo.format.get_bits() % 8) == 0) {
22cb00db
KS
467 let len = nsamples.checked_mul(ainfo.channels as usize);
468 if len == None { return Err(AllocatorError::TooLargeDimensions); }
469 let length = len.unwrap();
01e2d496 470 let stride = nsamples;
22cb00db 471 for i in 0..ainfo.channels {
01e2d496 472 offs.push((i as usize) * stride);
22cb00db
KS
473 }
474 if ainfo.format.is_float() {
475 if ainfo.format.get_bits() == 32 {
e243ceb4 476 let data: Vec<f32> = vec![0.0; length];
01e2d496 477 let buf: NAAudioBuffer<f32> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride };
22cb00db
KS
478 Ok(NABufferType::AudioF32(buf))
479 } else {
480 Err(AllocatorError::TooLargeDimensions)
481 }
482 } else {
483 if ainfo.format.get_bits() == 8 && !ainfo.format.is_signed() {
e243ceb4 484 let data: Vec<u8> = vec![0; length];
01e2d496 485 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride };
22cb00db
KS
486 Ok(NABufferType::AudioU8(buf))
487 } else if ainfo.format.get_bits() == 16 && ainfo.format.is_signed() {
e243ceb4 488 let data: Vec<i16> = vec![0; length];
01e2d496 489 let buf: NAAudioBuffer<i16> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride };
22cb00db
KS
490 Ok(NABufferType::AudioI16(buf))
491 } else {
492 Err(AllocatorError::TooLargeDimensions)
493 }
494 }
495 } else {
496 let len = nsamples.checked_mul(ainfo.channels as usize);
497 if len == None { return Err(AllocatorError::TooLargeDimensions); }
498 let length = ainfo.format.get_audio_size(len.unwrap() as u64);
e243ceb4 499 let data: Vec<u8> = vec![0; length];
01e2d496 500 let buf: NAAudioBuffer<u8> = NAAudioBuffer { data: NABufferRef::new(data), info: ainfo, offs, chmap, len: nsamples, stride: 0 };
1a151e53 501 Ok(NABufferType::AudioPacked(buf))
22cb00db
KS
502 }
503}
504
505pub fn alloc_data_buffer(size: usize) -> Result<NABufferType, AllocatorError> {
e243ceb4 506 let data: Vec<u8> = vec![0; size];
1a967e6b 507 let buf: NABufferRef<Vec<u8>> = NABufferRef::new(data);
22cb00db
KS
508 Ok(NABufferType::Data(buf))
509}
510
511pub fn copy_buffer(buf: NABufferType) -> NABufferType {
512 buf.clone()
513}
514
01613464
KS
515pub struct NAVideoBufferPool<T:Copy> {
516 pool: Vec<NAVideoBufferRef<T>>,
1a967e6b 517 max_len: usize,
01613464 518 add_len: usize,
1a967e6b
KS
519}
520
01613464 521impl<T:Copy> NAVideoBufferPool<T> {
1a967e6b
KS
522 pub fn new(max_len: usize) -> Self {
523 Self {
524 pool: Vec::with_capacity(max_len),
525 max_len,
01613464 526 add_len: 0,
1a967e6b
KS
527 }
528 }
01613464
KS
529 pub fn set_dec_bufs(&mut self, add_len: usize) {
530 self.add_len = add_len;
531 }
532 pub fn get_free(&mut self) -> Option<NAVideoBufferRef<T>> {
533 for e in self.pool.iter() {
534 if e.get_num_refs() == 1 {
535 return Some(e.clone());
536 }
537 }
538 None
539 }
540 pub fn get_copy(&mut self, rbuf: &NAVideoBufferRef<T>) -> Option<NAVideoBufferRef<T>> {
e243ceb4 541 let mut dbuf = self.get_free()?;
01613464
KS
542 dbuf.data.copy_from_slice(&rbuf.data);
543 Some(dbuf)
544 }
545 pub fn reset(&mut self) {
546 self.pool.truncate(0);
547 }
548}
549
550impl NAVideoBufferPool<u8> {
1a967e6b 551 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
01613464 552 let nbufs = self.max_len + self.add_len - self.pool.len();
1a967e6b 553 for _ in 0..nbufs {
e243ceb4 554 let vbuf = alloc_video_buffer(vinfo, align)?;
01613464
KS
555 if let NABufferType::Video(buf) = vbuf {
556 self.pool.push(buf);
557 } else if let NABufferType::VideoPacked(buf) = vbuf {
558 self.pool.push(buf);
559 } else {
560 return Err(AllocatorError::FormatError);
561 }
1a967e6b
KS
562 }
563 Ok(())
564 }
01613464
KS
565}
566
567impl NAVideoBufferPool<u16> {
568 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
569 let nbufs = self.max_len + self.add_len - self.pool.len();
1a967e6b 570 for _ in 0..nbufs {
e243ceb4 571 let vbuf = alloc_video_buffer(vinfo, align)?;
01613464
KS
572 if let NABufferType::Video16(buf) = vbuf {
573 self.pool.push(buf);
574 } else {
575 return Err(AllocatorError::FormatError);
576 }
1a967e6b
KS
577 }
578 Ok(())
579 }
01613464
KS
580}
581
582impl NAVideoBufferPool<u32> {
583 pub fn prealloc_video(&mut self, vinfo: NAVideoInfo, align: u8) -> Result<(), AllocatorError> {
584 let nbufs = self.max_len + self.add_len - self.pool.len();
585 for _ in 0..nbufs {
e243ceb4 586 let vbuf = alloc_video_buffer(vinfo, align)?;
01613464
KS
587 if let NABufferType::Video32(buf) = vbuf {
588 self.pool.push(buf);
589 } else {
590 return Err(AllocatorError::FormatError);
1a967e6b
KS
591 }
592 }
01613464 593 Ok(())
1a967e6b
KS
594 }
595}
596
5869fd63 597#[allow(dead_code)]
8869d452
KS
598#[derive(Clone)]
599pub struct NACodecInfo {
ccae5343 600 name: &'static str,
5869fd63 601 properties: NACodecTypeInfo,
2422d969 602 extradata: Option<Arc<Vec<u8>>>,
5869fd63
KS
603}
604
2422d969
KS
605pub type NACodecInfoRef = Arc<NACodecInfo>;
606
8869d452 607impl NACodecInfo {
ccae5343 608 pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
8869d452
KS
609 let extradata = match edata {
610 None => None,
2422d969 611 Some(vec) => Some(Arc::new(vec)),
8869d452 612 };
e243ceb4 613 NACodecInfo { name, properties: p, extradata }
8869d452 614 }
2422d969 615 pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
e243ceb4 616 NACodecInfo { name, properties: p, extradata: edata }
66116504 617 }
2422d969 618 pub fn into_ref(self) -> NACodecInfoRef { Arc::new(self) }
8869d452 619 pub fn get_properties(&self) -> NACodecTypeInfo { self.properties }
2422d969 620 pub fn get_extradata(&self) -> Option<Arc<Vec<u8>>> {
8869d452
KS
621 if let Some(ref vec) = self.extradata { return Some(vec.clone()); }
622 None
5869fd63 623 }
66116504
KS
624 pub fn get_name(&self) -> &'static str { self.name }
625 pub fn is_video(&self) -> bool {
626 if let NACodecTypeInfo::Video(_) = self.properties { return true; }
627 false
628 }
629 pub fn is_audio(&self) -> bool {
630 if let NACodecTypeInfo::Audio(_) = self.properties { return true; }
631 false
632 }
2422d969
KS
633 pub fn new_dummy() -> Arc<Self> {
634 Arc::new(DUMMY_CODEC_INFO)
5076115b 635 }
2422d969
KS
636 pub fn replace_info(&self, p: NACodecTypeInfo) -> Arc<Self> {
637 Arc::new(NACodecInfo { name: self.name, properties: p, extradata: self.extradata.clone() })
5076115b 638 }
66116504
KS
639}
640
241e56f1
KS
641impl Default for NACodecInfo {
642 fn default() -> Self { DUMMY_CODEC_INFO }
643}
644
66116504
KS
645impl fmt::Display for NACodecInfo {
646 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
647 let edata = match self.extradata.clone() {
e243ceb4 648 None => "no extradata".to_string(),
66116504
KS
649 Some(v) => format!("{} byte(s) of extradata", v.len()),
650 };
651 write!(f, "{}: {} {}", self.name, self.properties, edata)
652 }
653}
654
655pub const DUMMY_CODEC_INFO: NACodecInfo = NACodecInfo {
656 name: "none",
657 properties: NACodecTypeInfo::None,
658 extradata: None };
659
66116504
KS
660#[derive(Debug,Clone)]
661pub enum NAValue {
5869fd63
KS
662 None,
663 Int(i32),
664 Long(i64),
665 String(String),
2422d969 666 Data(Arc<Vec<u8>>),
5869fd63
KS
667}
668
88c03b61
KS
669#[derive(Debug,Clone,Copy,PartialEq)]
670#[allow(dead_code)]
671pub enum FrameType {
672 I,
673 P,
674 B,
bc6aac3d 675 Skip,
88c03b61
KS
676 Other,
677}
678
679impl fmt::Display for FrameType {
680 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
681 match *self {
682 FrameType::I => write!(f, "I"),
683 FrameType::P => write!(f, "P"),
684 FrameType::B => write!(f, "B"),
bc6aac3d 685 FrameType::Skip => write!(f, "skip"),
88c03b61
KS
686 FrameType::Other => write!(f, "x"),
687 }
688 }
689}
690
e189501e
KS
691#[derive(Debug,Clone,Copy)]
692pub struct NATimeInfo {
5869fd63
KS
693 pts: Option<u64>,
694 dts: Option<u64>,
695 duration: Option<u64>,
e189501e
KS
696 tb_num: u32,
697 tb_den: u32,
698}
699
700impl NATimeInfo {
701 pub fn new(pts: Option<u64>, dts: Option<u64>, duration: Option<u64>, tb_num: u32, tb_den: u32) -> Self {
e243ceb4 702 NATimeInfo { pts, dts, duration, tb_num, tb_den }
e189501e
KS
703 }
704 pub fn get_pts(&self) -> Option<u64> { self.pts }
705 pub fn get_dts(&self) -> Option<u64> { self.dts }
706 pub fn get_duration(&self) -> Option<u64> { self.duration }
707 pub fn set_pts(&mut self, pts: Option<u64>) { self.pts = pts; }
708 pub fn set_dts(&mut self, dts: Option<u64>) { self.dts = dts; }
709 pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
266da7b9
KS
710
711 pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
712 let tb_num = tb_num as u64;
713 let tb_den = tb_den as u64;
714 let tmp = time.checked_mul(tb_num);
715 if let Some(tmp) = tmp {
716 tmp / base / tb_den
717 } else {
718 let tmp = time.checked_mul(tb_num);
719 if let Some(tmp) = tmp {
720 tmp / base / tb_den
721 } else {
722 let coarse = time / base;
723 let tmp = coarse.checked_mul(tb_num);
724 if let Some(tmp) = tmp {
725 tmp / tb_den
726 } else {
727 (coarse / tb_den) * tb_num
728 }
729 }
730 }
731 }
a65bdeac
KS
732 pub fn ts_to_time(ts: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
733 let tb_num = tb_num as u64;
734 let tb_den = tb_den as u64;
735 let tmp = ts.checked_mul(base);
736 if let Some(tmp) = tmp {
737 let tmp2 = tmp.checked_mul(tb_num);
738 if let Some(tmp2) = tmp2 {
739 tmp2 / tb_den
740 } else {
741 (tmp / tb_den) * tb_num
742 }
743 } else {
744 let tmp = ts.checked_mul(tb_num);
745 if let Some(tmp) = tmp {
746 (tmp / tb_den) * base
747 } else {
748 (ts / tb_den) * base * tb_num
749 }
750 }
751 }
e189501e
KS
752}
753
754#[allow(dead_code)]
755#[derive(Clone)]
756pub struct NAFrame {
757 ts: NATimeInfo,
f18bba90 758 id: i64,
22cb00db 759 buffer: NABufferType,
2422d969 760 info: NACodecInfoRef,
88c03b61
KS
761 ftype: FrameType,
762 key: bool,
66116504
KS
763 options: HashMap<String, NAValue>,
764}
765
171860fc 766pub type NAFrameRef = Arc<NAFrame>;
ebd71c92 767
66116504
KS
768fn get_plane_size(info: &NAVideoInfo, idx: usize) -> (usize, usize) {
769 let chromaton = info.get_format().get_chromaton(idx);
e243ceb4 770 if chromaton.is_none() { return (0, 0); }
66116504
KS
771 let (hs, vs) = chromaton.unwrap().get_subsampling();
772 let w = (info.get_width() + ((1 << hs) - 1)) >> hs;
773 let h = (info.get_height() + ((1 << vs) - 1)) >> vs;
774 (w, h)
775}
776
777impl NAFrame {
e189501e 778 pub fn new(ts: NATimeInfo,
88c03b61
KS
779 ftype: FrameType,
780 keyframe: bool,
2422d969 781 info: NACodecInfoRef,
22cb00db
KS
782 options: HashMap<String, NAValue>,
783 buffer: NABufferType) -> Self {
f18bba90 784 NAFrame { ts, id: 0, buffer, info, ftype, key: keyframe, options }
ebd71c92 785 }
2422d969 786 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
88c03b61
KS
787 pub fn get_frame_type(&self) -> FrameType { self.ftype }
788 pub fn is_keyframe(&self) -> bool { self.key }
88c03b61
KS
789 pub fn set_frame_type(&mut self, ftype: FrameType) { self.ftype = ftype; }
790 pub fn set_keyframe(&mut self, key: bool) { self.key = key; }
e189501e
KS
791 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
792 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
793 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
f18bba90 794 pub fn get_id(&self) -> i64 { self.id }
e189501e
KS
795 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
796 pub fn set_pts(&mut self, pts: Option<u64>) { self.ts.set_pts(pts); }
797 pub fn set_dts(&mut self, dts: Option<u64>) { self.ts.set_dts(dts); }
f18bba90 798 pub fn set_id(&mut self, id: i64) { self.id = id; }
e189501e 799 pub fn set_duration(&mut self, dur: Option<u64>) { self.ts.set_duration(dur); }
66116504 800
22cb00db 801 pub fn get_buffer(&self) -> NABufferType { self.buffer.clone() }
171860fc
KS
802
803 pub fn into_ref(self) -> NAFrameRef { Arc::new(self) }
5869fd63
KS
804}
805
ebd71c92
KS
806impl fmt::Display for NAFrame {
807 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
e243ceb4
KS
808 let mut ostr = format!("frame type {}", self.ftype);
809 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
810 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
811 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
812 if self.key { ostr = format!("{} kf", ostr); }
813 write!(f, "[{}]", ostr)
ebd71c92
KS
814 }
815}
88c03b61 816
48c88fde 817/// Possible stream types.
baf5478c 818#[derive(Debug,Clone,Copy,PartialEq)]
5869fd63 819#[allow(dead_code)]
48c88fde
KS
820pub enum StreamType {
821 /// video stream
822 Video,
823 /// audio stream
824 Audio,
825 /// subtitles
826 Subtitles,
827 /// any data stream (or might be an unrecognized audio/video stream)
828 Data,
829 /// nonexistent stream
830 None,
831}
832
833impl fmt::Display for StreamType {
834 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
835 match *self {
836 StreamType::Video => write!(f, "Video"),
837 StreamType::Audio => write!(f, "Audio"),
838 StreamType::Subtitles => write!(f, "Subtitles"),
839 StreamType::Data => write!(f, "Data"),
840 StreamType::None => write!(f, "-"),
841 }
842 }
843}
844
845#[allow(dead_code)]
846#[derive(Clone)]
847pub struct NAStream {
848 media_type: StreamType,
849 id: u32,
850 num: usize,
2422d969 851 info: NACodecInfoRef,
e189501e
KS
852 tb_num: u32,
853 tb_den: u32,
854}
855
70910ac3
KS
856pub type NAStreamRef = Arc<NAStream>;
857
e189501e
KS
858pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
859 if tb_num == 0 { return (tb_num, tb_den); }
860 if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
861
862 let mut a = tb_num;
863 let mut b = tb_den;
864
865 while a != b {
866 if a > b { a -= b; }
867 else if b > a { b -= a; }
868 }
869
870 (tb_num / a, tb_den / a)
5869fd63 871}
48c88fde
KS
872
873impl NAStream {
e189501e
KS
874 pub fn new(mt: StreamType, id: u32, info: NACodecInfo, tb_num: u32, tb_den: u32) -> Self {
875 let (n, d) = reduce_timebase(tb_num, tb_den);
e243ceb4 876 NAStream { media_type: mt, id, num: 0, info: info.into_ref(), tb_num: n, tb_den: d }
48c88fde
KS
877 }
878 pub fn get_id(&self) -> u32 { self.id }
baf5478c 879 pub fn get_media_type(&self) -> StreamType { self.media_type }
48c88fde
KS
880 pub fn get_num(&self) -> usize { self.num }
881 pub fn set_num(&mut self, num: usize) { self.num = num; }
2422d969 882 pub fn get_info(&self) -> NACodecInfoRef { self.info.clone() }
e189501e
KS
883 pub fn get_timebase(&self) -> (u32, u32) { (self.tb_num, self.tb_den) }
884 pub fn set_timebase(&mut self, tb_num: u32, tb_den: u32) {
885 let (n, d) = reduce_timebase(tb_num, tb_den);
886 self.tb_num = n;
887 self.tb_den = d;
888 }
70910ac3 889 pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
48c88fde
KS
890}
891
892impl fmt::Display for NAStream {
893 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
e189501e 894 write!(f, "({}#{} @ {}/{} - {})", self.media_type, self.id, self.tb_num, self.tb_den, self.info.get_properties())
48c88fde
KS
895 }
896}
897
898#[allow(dead_code)]
899pub struct NAPacket {
70910ac3 900 stream: NAStreamRef,
e189501e 901 ts: NATimeInfo,
1a967e6b 902 buffer: NABufferRef<Vec<u8>>,
48c88fde
KS
903 keyframe: bool,
904// options: HashMap<String, NAValue<'a>>,
905}
906
907impl NAPacket {
70910ac3 908 pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
48c88fde
KS
909// let mut vec: Vec<u8> = Vec::new();
910// vec.resize(size, 0);
e243ceb4 911 NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec) }
48c88fde 912 }
70910ac3 913 pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
e189501e
KS
914 pub fn get_time_information(&self) -> NATimeInfo { self.ts }
915 pub fn get_pts(&self) -> Option<u64> { self.ts.get_pts() }
916 pub fn get_dts(&self) -> Option<u64> { self.ts.get_dts() }
917 pub fn get_duration(&self) -> Option<u64> { self.ts.get_duration() }
48c88fde 918 pub fn is_keyframe(&self) -> bool { self.keyframe }
1a967e6b 919 pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
48c88fde
KS
920}
921
922impl Drop for NAPacket {
923 fn drop(&mut self) {}
924}
925
926impl fmt::Display for NAPacket {
927 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
e243ceb4
KS
928 let mut ostr = format!("[pkt for {} size {}", self.stream, self.buffer.len());
929 if let Some(pts) = self.ts.pts { ostr = format!("{} pts {}", ostr, pts); }
930 if let Some(dts) = self.ts.dts { ostr = format!("{} dts {}", ostr, dts); }
931 if let Some(dur) = self.ts.duration { ostr = format!("{} duration {}", ostr, dur); }
932 if self.keyframe { ostr = format!("{} kf", ostr); }
933 ostr += "]";
934 write!(f, "{}", ostr)
48c88fde
KS
935 }
936}
937
938pub trait FrameFromPacket {
2422d969 939 fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame;
48c88fde
KS
940 fn fill_timestamps(&mut self, pkt: &NAPacket);
941}
942
943impl FrameFromPacket for NAFrame {
2422d969 944 fn new_from_pkt(pkt: &NAPacket, info: NACodecInfoRef, buf: NABufferType) -> NAFrame {
e189501e 945 NAFrame::new(pkt.ts, FrameType::Other, pkt.keyframe, info, HashMap::new(), buf)
48c88fde
KS
946 }
947 fn fill_timestamps(&mut self, pkt: &NAPacket) {
e189501e 948 self.ts = pkt.get_time_information();
48c88fde
KS
949 }
950}
951