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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
// Copyright (C) 2017 Sebastian Dröge <sebastian@centricular.com>
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use gst_audio_sys;
use AudioChannelPosition;

use std::mem;

use glib;
use glib::translate::{from_glib, ToGlib};
use gst;

use array_init;

impl AudioChannelPosition {
    pub fn to_mask(self) -> u64 {
        let pos = self.to_glib();
        if pos < 0 {
            return 0;
        }

        1 << (pos as u32)
    }

    pub fn positions_to_mask(
        positions: &[AudioChannelPosition],
        force_order: bool,
    ) -> Result<u64, glib::error::BoolError> {
        assert_initialized_main_thread!();

        let len = positions.len();
        if len > 64 {
            return Err(glib_bool_error!("Invalid number of channels"));
        }

        let positions_raw: [gst_audio_sys::GstAudioChannelPosition; 64] =
            array_init::array_init(|i| {
                if i >= len as usize {
                    gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
                } else {
                    positions[i].to_glib()
                }
            });

        unsafe {
            let mut mask = mem::MaybeUninit::uninit();
            let valid: bool = from_glib(gst_audio_sys::gst_audio_channel_positions_to_mask(
                positions_raw.as_ptr() as *mut _,
                len as i32,
                force_order.to_glib(),
                mask.as_mut_ptr(),
            ));
            if valid {
                Ok(mask.assume_init())
            } else {
                Err(glib_bool_error!(
                    "Couldn't convert channel positions to mask"
                ))
            }
        }
    }

    pub fn positions_from_mask(
        mask: u64,
        positions: &mut [AudioChannelPosition],
    ) -> Result<(), glib::BoolError> {
        assert_initialized_main_thread!();

        if positions.len() > 64 {
            return Err(glib_bool_error!("Invalid number of channels"));
        }

        let len = positions.len();
        let mut positions_raw: [gst_audio_sys::GstAudioChannelPosition; 64] =
            [gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID; 64];
        let valid: bool = unsafe {
            from_glib(gst_audio_sys::gst_audio_channel_positions_from_mask(
                len as i32,
                mask,
                positions_raw.as_mut_ptr(),
            ))
        };

        if valid {
            for (d, s) in positions.iter_mut().zip(positions_raw.iter()) {
                *d = from_glib(*s);
            }
            Ok(())
        } else {
            Err(glib_bool_error!(
                "Couldn't convert channel positions to mask",
            ))
        }
    }

    pub fn positions_to_valid_order(
        positions: &mut [AudioChannelPosition],
    ) -> Result<(), glib::BoolError> {
        assert_initialized_main_thread!();

        if positions.len() > 64 {
            return Err(glib_bool_error!("Invalid number of channels"));
        }

        let len = positions.len();
        let mut positions_raw: [gst_audio_sys::GstAudioChannelPosition; 64] =
            array_init::array_init(|i| {
                if i >= len as usize {
                    gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
                } else {
                    positions[i].to_glib()
                }
            });

        let valid: bool = unsafe {
            from_glib(gst_audio_sys::gst_audio_channel_positions_to_valid_order(
                positions_raw.as_mut_ptr(),
                len as i32,
            ))
        };

        if valid {
            for (d, s) in positions.iter_mut().zip(positions_raw.iter()) {
                *d = from_glib(*s);
            }
            Ok(())
        } else {
            Err(glib_bool_error!(
                "Couldn't convert channel positions to mask",
            ))
        }
    }

    pub fn get_fallback_mask(channels: u32) -> u64 {
        assert_initialized_main_thread!();

        unsafe { gst_audio_sys::gst_audio_channel_get_fallback_mask(channels as i32) }
    }

    pub fn check_valid_channel_positions(
        positions: &[::AudioChannelPosition],
        force_order: bool,
    ) -> bool {
        assert_initialized_main_thread!();

        if positions.len() > 64 {
            return false;
        }

        let len = positions.len();
        let positions_raw: [gst_audio_sys::GstAudioChannelPosition; 64] =
            array_init::array_init(|i| {
                if i >= len as usize {
                    gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
                } else {
                    positions[i].to_glib()
                }
            });

        unsafe {
            from_glib(gst_audio_sys::gst_audio_check_valid_channel_positions(
                positions_raw.as_ptr() as *mut _,
                len as i32,
                force_order.to_glib(),
            ))
        }
    }
}

pub fn buffer_reorder_channels(
    buffer: &mut gst::BufferRef,
    format: ::AudioFormat,
    channels: u32,
    from: &[AudioChannelPosition],
    to: &[AudioChannelPosition],
) -> Result<(), glib::BoolError> {
    assert_initialized_main_thread!();

    if from.len() != to.len() || from.len() > 64 {
        return Err(glib_bool_error!("Invalid number of channels"));
    }

    let from_len = from.len();
    let to_len = to.len();

    let from_raw: [gst_audio_sys::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
        if i >= from_len as usize {
            gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
        } else {
            from[i].to_glib()
        }
    });

    let to_raw: [gst_audio_sys::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
        if i >= to_len as usize {
            gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
        } else {
            to[i].to_glib()
        }
    });

    let valid: bool = unsafe {
        from_glib(gst_audio_sys::gst_audio_buffer_reorder_channels(
            buffer.as_mut_ptr(),
            format.to_glib(),
            channels as i32,
            from_raw.as_ptr() as *mut _,
            to_raw.as_ptr() as *mut _,
        ))
    };

    if valid {
        Ok(())
    } else {
        Err(glib_bool_error!("Failed to reorder channels"))
    }
}

pub fn reorder_channels(
    data: &mut [u8],
    format: ::AudioFormat,
    channels: u32,
    from: &[AudioChannelPosition],
    to: &[AudioChannelPosition],
) -> Result<(), glib::BoolError> {
    assert_initialized_main_thread!();

    if from.len() != to.len() || from.len() > 64 {
        return Err(glib_bool_error!("Invalid number of channels"));
    }

    let from_len = from.len();
    let to_len = to.len();

    let from_raw: [gst_audio_sys::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
        if i >= from_len as usize {
            gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
        } else {
            from[i].to_glib()
        }
    });

    let to_raw: [gst_audio_sys::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
        if i >= to_len as usize {
            gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
        } else {
            to[i].to_glib()
        }
    });

    let valid: bool = unsafe {
        from_glib(gst_audio_sys::gst_audio_reorder_channels(
            data.as_mut_ptr() as *mut _,
            data.len(),
            format.to_glib(),
            channels as i32,
            from_raw.as_ptr() as *mut _,
            to_raw.as_ptr() as *mut _,
        ))
    };

    if valid {
        Ok(())
    } else {
        Err(glib_bool_error!("Failed to reorder channels"))
    }
}

pub fn get_channel_reorder_map(
    from: &[AudioChannelPosition],
    to: &[AudioChannelPosition],
    reorder_map: &mut [usize],
) -> Result<(), glib::BoolError> {
    assert_initialized_main_thread!();

    if from.len() != to.len() || from.len() != reorder_map.len() || from.len() > 64 {
        return Err(glib_bool_error!("Invalid number of channels"));
    }

    let from_len = from.len();
    let to_len = to.len();

    let from_raw: [gst_audio_sys::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
        if i >= from_len as usize {
            gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
        } else {
            from[i].to_glib()
        }
    });

    let to_raw: [gst_audio_sys::GstAudioChannelPosition; 64] = array_init::array_init(|i| {
        if i >= to_len as usize {
            gst_audio_sys::GST_AUDIO_CHANNEL_POSITION_INVALID
        } else {
            to[i].to_glib()
        }
    });

    let mut reorder_map_raw = [0i32, 64];
    let valid: bool = unsafe {
        from_glib(gst_audio_sys::gst_audio_get_channel_reorder_map(
            from_len as i32,
            from_raw.as_ptr() as *mut _,
            to_raw.as_ptr() as *mut _,
            reorder_map_raw.as_mut_ptr(),
        ))
    };

    if valid {
        for (d, s) in reorder_map.iter_mut().zip(reorder_map_raw.iter()) {
            *d = *s as usize;
        }
        Ok(())
    } else {
        Err(glib_bool_error!("Failed to reorder channels"))
    }
}