}
/// Reports whether the current stream is video stream.
pub fn is_video(&self) -> bool {
- match *self {
- NACodecTypeInfo::Video(_) => true,
- _ => false,
- }
+ matches!(*self, NACodecTypeInfo::Video(_))
}
/// Reports whether the current stream is audio stream.
pub fn is_audio(&self) -> bool {
- match *self {
- NACodecTypeInfo::Audio(_) => true,
- _ => false,
- }
+ matches!(*self, NACodecTypeInfo::Audio(_))
}
}
}
impl<T: Clone> NAVideoBuffer<T> {
+ /// Constructs video buffer from the provided components.
+ pub fn from_raw_parts(info: NAVideoInfo, data: NABufferRef<Vec<T>>, offs: Vec<usize>, strides: Vec<usize>) -> Self {
+ Self { info, data, offs, strides }
+ }
/// Returns the component offset (0 for all unavailable offsets).
pub fn get_offset(&self, idx: usize) -> usize {
if idx >= self.offs.len() { 0 }
/// Returns the number of components in picture format.
pub fn get_num_components(&self) -> usize { self.offs.len() }
/// Creates a copy of current `NAVideoBuffer`.
- pub fn copy_buffer(&mut self) -> Self {
+ pub fn copy_buffer(&self) -> Self {
let mut data: Vec<T> = Vec::with_capacity(self.data.len());
data.clone_from(self.data.as_ref());
let mut offs: Vec<usize> = Vec::with_capacity(self.offs.len());
impl NAAudioBuffer<u8> {
/// Constructs a new `NAAudioBuffer` instance.
pub fn new_from_buf(info: NAAudioInfo, data: NABufferRef<Vec<u8>>, chmap: NAChannelMap) -> Self {
- let len = data.len();
+ let len = data.len() * 8 / chmap.num_channels() / (info.format.bits as usize);
+
NAAudioBuffer { info, data, chmap, offs: Vec::new(), len, stride: 0, step: 0 }
}
}
_ => 0,
}
}
+ /// Truncates audio frame duration if possible.
+ pub fn truncate_audio(&mut self, len: usize) {
+ match *self {
+ NABufferType::AudioU8(ref mut ab) => ab.truncate(len),
+ NABufferType::AudioI16(ref mut ab) => ab.truncate(len),
+ NABufferType::AudioI32(ref mut ab) => ab.truncate(len),
+ NABufferType::AudioF32(ref mut ab) => ab.truncate(len),
+ NABufferType::AudioPacked(ref mut ab) => ab.truncate(len),
+ _ => {},
+ };
+ }
/// Returns the distance between starts of two channels.
pub fn get_audio_stride(&self) -> usize {
match *self {
}
max_depth = max(max_depth, chr.get_depth());
}
- let unfit_elem_size = match fmt.get_elem_size() {
- 2 | 4 => false,
- _ => true,
- };
+ let unfit_elem_size = !matches!(fmt.get_elem_size(), 2 | 4);
//todo semi-packed like NV12
if fmt.is_paletted() {
}
/// Creates a clone of current buffer.
-pub fn copy_buffer(buf: NABufferType) -> NABufferType {
+pub fn copy_buffer(buf: &NABufferType) -> NABufferType {
buf.clone()
}
}
/// Clears the pool from all frames.
pub fn reset(&mut self) {
- self.pool.truncate(0);
+ self.pool.clear();
}
}
impl NACodecInfo {
/// Constructs a new instance of `NACodecInfo`.
pub fn new(name: &'static str, p: NACodecTypeInfo, edata: Option<Vec<u8>>) -> Self {
- let extradata = match edata {
- None => None,
- Some(vec) => Some(Arc::new(vec)),
- };
- NACodecInfo { name, properties: p, extradata }
+ NACodecInfo { name, properties: p, extradata: edata.map(Arc::new) }
}
/// Constructs a new reference-counted instance of `NACodecInfo`.
pub fn new_ref(name: &'static str, p: NACodecTypeInfo, edata: Option<Arc<Vec<u8>>>) -> Self {
pub fn set_duration(&mut self, dur: Option<u64>) { self.duration = dur; }
/// Converts time in given scale into timestamp in given base.
+ #[allow(clippy::collapsible_if)]
pub fn time_to_ts(time: u64, base: u64, tb_num: u32, tb_den: u32) -> u64 {
let tb_num = u64::from(tb_num);
let tb_den = u64::from(tb_den);
- let tmp = time.checked_mul(tb_num);
+ let tmp = time.checked_mul(tb_den);
if let Some(tmp) = tmp {
- tmp / base / tb_den
+ tmp / base / tb_num
} else {
- let tmp = time.checked_mul(tb_num);
- if let Some(tmp) = tmp {
- tmp / base / tb_den
+ if tb_num < base {
+ let coarse = time / tb_num;
+ if let Some(tmp) = coarse.checked_mul(tb_den) {
+ tmp / base
+ } else {
+ (coarse / base) * tb_den
+ }
} else {
let coarse = time / base;
- let tmp = coarse.checked_mul(tb_num);
- if let Some(tmp) = tmp {
- tmp / tb_den
+ if let Some(tmp) = coarse.checked_mul(tb_den) {
+ tmp / tb_num
} else {
- (coarse / tb_den) * tb_num
+ (coarse / tb_num) * tb_den
}
}
}
let mut mval = 0;
let mut base = 0;
for ch in val.chars() {
- if ch >= '0' && ch <= '9' {
+ if ('0'..='9').contains(&ch) {
mval = mval * 10 + u64::from((ch as u8) - b'0');
base += 1;
if base > 3 { break; }
pub type NAStreamRef = Arc<NAStream>;
/// Downscales the timebase by its greatest common denominator.
+#[allow(clippy::comparison_chain)]
pub fn reduce_timebase(tb_num: u32, tb_den: u32) -> (u32, u32) {
if tb_num == 0 { return (tb_num, tb_den); }
if (tb_den % tb_num) == 0 { return (1, tb_den / tb_num); }
self.tb_den = d;
}
/// Returns stream duration.
- pub fn get_duration(&self) -> usize { self.num }
+ pub fn get_duration(&self) -> u64 { self.duration }
+ /// Constructs a new timestamp.
+ pub fn make_ts(&self, pts: Option<u64>, dts: Option<u64>, duration: Option<u64>) -> NATimeInfo {
+ NATimeInfo::new(pts, dts, duration, self.tb_num, self.tb_den)
+ }
/// Converts current instance into a reference-counted one.
pub fn into_ref(self) -> NAStreamRef { Arc::new(self) }
}
impl NAPacket {
/// Constructs a new `NAPacket` instance.
- pub fn new(str: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
+ pub fn new(stream: NAStreamRef, ts: NATimeInfo, kf: bool, vec: Vec<u8>) -> Self {
// let mut vec: Vec<u8> = Vec::new();
// vec.resize(size, 0);
- NAPacket { stream: str, ts, keyframe: kf, buffer: NABufferRef::new(vec), side_data: Vec::new() }
+ NAPacket { stream, ts, keyframe: kf, buffer: NABufferRef::new(vec), side_data: Vec::new() }
}
/// Constructs a new `NAPacket` instance reusing a buffer reference.
- pub fn new_from_refbuf(str: NAStreamRef, ts: NATimeInfo, kf: bool, buffer: NABufferRef<Vec<u8>>) -> Self {
- NAPacket { stream: str, ts, keyframe: kf, buffer, side_data: Vec::new() }
+ pub fn new_from_refbuf(stream: NAStreamRef, ts: NATimeInfo, kf: bool, buffer: NABufferRef<Vec<u8>>) -> Self {
+ NAPacket { stream, ts, keyframe: kf, buffer, side_data: Vec::new() }
}
/// Returns information about the stream packet belongs to.
pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
/// Adds side data for a packet.
pub fn add_side_data(&mut self, side_data: NASideData) { self.side_data.push(side_data); }
/// Assigns packet to a new stream.
- pub fn reassign(&mut self, str: NAStreamRef, ts: NATimeInfo) {
- self.stream = str;
+ pub fn reassign(&mut self, stream: NAStreamRef, ts: NATimeInfo) {
+ self.stream = stream;
self.ts = ts;
}
}
}
}
+/// Packet with a piece of data for a raw stream.
+pub struct NARawData {
+ stream: NAStreamRef,
+ buffer: NABufferRef<Vec<u8>>,
+}
+
+impl NARawData {
+ /// Constructs a new `NARawData` instance.
+ pub fn new(stream: NAStreamRef, vec: Vec<u8>) -> Self {
+ Self { stream, buffer: NABufferRef::new(vec) }
+ }
+ /// Constructs a new `NARawData` instance reusing a buffer reference.
+ pub fn new_from_refbuf(stream: NAStreamRef, buffer: NABufferRef<Vec<u8>>) -> Self {
+ Self { stream, buffer }
+ }
+ /// Returns information about the stream this data belongs to.
+ pub fn get_stream(&self) -> NAStreamRef { self.stream.clone() }
+ /// Returns a reference to packet data.
+ pub fn get_buffer(&self) -> NABufferRef<Vec<u8>> { self.buffer.clone() }
+ /// Assigns raw data to a new stream.
+ pub fn reassign(&mut self, stream: NAStreamRef) {
+ self.stream = stream;
+ }
+}
+
+impl fmt::Display for NARawData {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ write!(f, "[raw data for {} size {}]", self.stream, self.buffer.len())
+ }
+}
+
#[cfg(test)]
mod test {
use super::*;