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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use std::ffi::{CStr, CString};
use std::ops::Index;
use std::ptr;
use std::slice;
use std::str::from_utf8_unchecked;

use ffi::AVSampleFormat::*;
use ffi::*;
use libc::{c_int, c_void};

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Sample {
    None,

    U8(Type),
    I16(Type),
    I32(Type),
    I64(Type),
    F32(Type),
    F64(Type),
}

#[derive(Eq, PartialEq, Copy, Clone, Debug)]
pub enum Type {
    Packed,
    Planar,
}

impl Sample {
    #[inline]
    pub fn name(&self) -> &'static str {
        unsafe {
            from_utf8_unchecked(CStr::from_ptr(av_get_sample_fmt_name((*self).into())).to_bytes())
        }
    }

    #[inline]
    pub fn packed(&self) -> Self {
        unsafe { Sample::from(av_get_packed_sample_fmt((*self).into())) }
    }

    #[inline]
    pub fn planar(&self) -> Self {
        unsafe { Sample::from(av_get_planar_sample_fmt((*self).into())) }
    }

    #[inline]
    pub fn is_planar(&self) -> bool {
        unsafe { av_sample_fmt_is_planar((*self).into()) == 1 }
    }

    #[inline]
    pub fn is_packed(&self) -> bool {
        !self.is_planar()
    }

    #[inline]
    pub fn bytes(&self) -> usize {
        unsafe { av_get_bytes_per_sample((*self).into()) as usize }
    }

    #[inline]
    pub fn buffer(&self, channels: u16, samples: usize, align: bool) -> Buffer {
        Buffer::new(*self, channels, samples, align)
    }
}

impl From<AVSampleFormat> for Sample {
    #[inline]
    fn from(value: AVSampleFormat) -> Self {
        match value {
            AV_SAMPLE_FMT_NONE => Sample::None,

            AV_SAMPLE_FMT_U8 => Sample::U8(Type::Packed),
            AV_SAMPLE_FMT_S16 => Sample::I16(Type::Packed),
            AV_SAMPLE_FMT_S32 => Sample::I32(Type::Packed),
            AV_SAMPLE_FMT_S64 => Sample::I64(Type::Packed),
            AV_SAMPLE_FMT_FLT => Sample::F32(Type::Packed),
            AV_SAMPLE_FMT_DBL => Sample::F64(Type::Packed),

            AV_SAMPLE_FMT_U8P => Sample::U8(Type::Planar),
            AV_SAMPLE_FMT_S16P => Sample::I16(Type::Planar),
            AV_SAMPLE_FMT_S32P => Sample::I32(Type::Planar),
            AV_SAMPLE_FMT_S64P => Sample::I64(Type::Planar),
            AV_SAMPLE_FMT_FLTP => Sample::F32(Type::Planar),
            AV_SAMPLE_FMT_DBLP => Sample::F64(Type::Planar),

            AV_SAMPLE_FMT_NB => Sample::None,
        }
    }
}

impl From<&'static str> for Sample {
    #[inline]
    fn from(value: &'static str) -> Self {
        unsafe {
            let value = CString::new(value).unwrap();

            Sample::from(av_get_sample_fmt(value.as_ptr()))
        }
    }
}

impl From<Sample> for AVSampleFormat {
    #[inline]
    fn from(value: Sample) -> AVSampleFormat {
        match value {
            Sample::None => AV_SAMPLE_FMT_NONE,

            Sample::U8(Type::Packed) => AV_SAMPLE_FMT_U8,
            Sample::I16(Type::Packed) => AV_SAMPLE_FMT_S16,
            Sample::I32(Type::Packed) => AV_SAMPLE_FMT_S32,
            Sample::I64(Type::Packed) => AV_SAMPLE_FMT_S64,
            Sample::F32(Type::Packed) => AV_SAMPLE_FMT_FLT,
            Sample::F64(Type::Packed) => AV_SAMPLE_FMT_DBL,

            Sample::U8(Type::Planar) => AV_SAMPLE_FMT_U8P,
            Sample::I16(Type::Planar) => AV_SAMPLE_FMT_S16P,
            Sample::I32(Type::Planar) => AV_SAMPLE_FMT_S32P,
            Sample::I64(Type::Planar) => AV_SAMPLE_FMT_S64P,
            Sample::F32(Type::Planar) => AV_SAMPLE_FMT_FLTP,
            Sample::F64(Type::Planar) => AV_SAMPLE_FMT_DBLP,
        }
    }
}

pub struct Buffer {
    pub format: Sample,
    pub channels: u16,
    pub samples: usize,
    pub align: bool,

    buffer: *mut *mut u8,
    size: c_int,
}

impl Buffer {
    #[inline]
    pub fn size(format: Sample, channels: u16, samples: usize, align: bool) -> usize {
        unsafe {
            av_samples_get_buffer_size(
                ptr::null_mut(),
                i32::from(channels),
                samples as c_int,
                format.into(),
                !align as c_int,
            ) as usize
        }
    }

    #[inline]
    pub fn new(format: Sample, channels: u16, samples: usize, align: bool) -> Self {
        unsafe {
            let mut buf = Buffer {
                format,
                channels,
                samples,
                align,

                buffer: ptr::null_mut(),
                size: 0,
            };

            av_samples_alloc_array_and_samples(
                &mut buf.buffer,
                &mut buf.size,
                i32::from(channels),
                samples as c_int,
                format.into(),
                !align as c_int,
            );

            buf
        }
    }
}

impl Index<usize> for Buffer {
    type Output = [u8];

    #[inline]
    fn index(&self, index: usize) -> &[u8] {
        if index >= self.samples {
            panic!("out of bounds");
        }

        unsafe { slice::from_raw_parts(*self.buffer.add(index), self.size as usize) }
    }
}

impl Clone for Buffer {
    #[inline]
    fn clone(&self) -> Self {
        let mut buf = Buffer::new(self.format, self.channels, self.samples, self.align);
        buf.clone_from(self);

        buf
    }

    #[inline]
    fn clone_from(&mut self, source: &Self) {
        unsafe {
            av_samples_copy(
                self.buffer,
                source.buffer as *const *mut u8,
                0,
                0,
                source.samples as c_int,
                i32::from(source.channels),
                source.format.into(),
            );
        }
    }
}

impl Drop for Buffer {
    #[inline]
    fn drop(&mut self) {
        unsafe {
            av_freep(self.buffer as *mut c_void);
        }
    }
}