ffmpeg-sys-the-third 5.0.0+ffmpeg-8.1

FFI bindings to FFmpeg
Documentation
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use libc::c_int;

use crate::*;
use crate::{AVChannel as AVC, AVChannelLayout, AVChannelOrder};

use std::fmt;
use std::ptr::null_mut;

impl AVChannelLayout {
    #[inline]
    pub const fn empty() -> Self {
        Self {
            order: AVChannelOrder::UNSPEC,
            nb_channels: 0,
            u: AVChannelLayout__bindgen_ty_1 { mask: 0 },
            opaque: null_mut(),
        }
    }
}

impl Clone for AVChannelLayout {
    fn clone(&self) -> Self {
        let mut cloned = Self::empty();
        cloned.clone_from(self);

        cloned
    }

    fn clone_from(&mut self, source: &Self) {
        #[cold]
        fn clone_failed(channels: c_int) -> ! {
            use std::alloc::{handle_alloc_error, Layout};

            let alloc_size = channels as usize * size_of::<AVChannelCustom>();
            let layout =
                Layout::from_size_align(alloc_size, align_of::<AVChannelCustom>()).unwrap();
            handle_alloc_error(layout)
        }

        let ret = unsafe { av_channel_layout_copy(self as _, source as _) };

        if ret < 0 {
            clone_failed(self.nb_channels);
        }
    }
}

impl Drop for AVChannelLayout {
    fn drop(&mut self) {
        unsafe { av_channel_layout_uninit(self as _) }
    }
}

impl PartialEq for AVChannelLayout {
    fn eq(&self, other: &Self) -> bool {
        unsafe { av_channel_layout_compare(self as _, other as _) == 0 }
    }
}

impl fmt::Debug for AVChannelLayout {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut dbg = f.debug_struct("AVChannelLayout");
        dbg.field("order", &self.order)
            .field("nb_channels", &self.nb_channels);

        unsafe {
            match self.order {
                AVChannelOrder::UNSPEC => {} // no other valid fields
                AVChannelOrder::NATIVE | AVChannelOrder::AMBISONIC => {
                    dbg.field("mask", &format_args!("0x{:X}", self.u.mask));
                }
                AVChannelOrder::CUSTOM => {
                    dbg.field(
                        "map",
                        &std::slice::from_raw_parts(self.u.map, self.nb_channels as usize),
                    );
                }
                #[cfg(feature = "ffmpeg_7_0")]
                AVChannelOrder::NB => {} // marker value, just here for exhaustive matching
                _ => unimplemented!(),
            }
        }

        dbg.field("opaque", &self.opaque).finish()
    }
}

impl fmt::Debug for AVChannelCustom {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        unsafe {
            f.debug_struct("AVChannelCustom")
                .field("id", &self.id)
                .field("name", &std::ffi::CStr::from_ptr(self.name.as_ptr()))
                .field("opaque", &self.opaque)
                .finish()
        }
    }
}

// Here until https://github.com/rust-lang/rust-bindgen/issues/258 is fixed.
// The constants here should be kept up to date with libavutil/channel_layout.h.

// Audio channel masks
pub const AV_CH_FRONT_LEFT: u64 = 1 << AVC::FRONT_LEFT.0;
pub const AV_CH_FRONT_RIGHT: u64 = 1 << AVC::FRONT_RIGHT.0;
pub const AV_CH_FRONT_CENTER: u64 = 1 << AVC::FRONT_CENTER.0;
pub const AV_CH_LOW_FREQUENCY: u64 = 1 << AVC::LOW_FREQUENCY.0;
pub const AV_CH_BACK_LEFT: u64 = 1 << AVC::BACK_LEFT.0;
pub const AV_CH_BACK_RIGHT: u64 = 1 << AVC::BACK_RIGHT.0;
pub const AV_CH_FRONT_LEFT_OF_CENTER: u64 = 1 << AVC::FRONT_LEFT_OF_CENTER.0;
pub const AV_CH_FRONT_RIGHT_OF_CENTER: u64 = 1 << AVC::FRONT_RIGHT_OF_CENTER.0;
pub const AV_CH_BACK_CENTER: u64 = 1 << AVC::BACK_CENTER.0;
pub const AV_CH_SIDE_LEFT: u64 = 1 << AVC::SIDE_LEFT.0;
pub const AV_CH_SIDE_RIGHT: u64 = 1 << AVC::SIDE_RIGHT.0;
pub const AV_CH_TOP_CENTER: u64 = 1 << AVC::TOP_CENTER.0;
pub const AV_CH_TOP_FRONT_LEFT: u64 = 1 << AVC::TOP_FRONT_LEFT.0;
pub const AV_CH_TOP_FRONT_CENTER: u64 = 1 << AVC::TOP_FRONT_CENTER.0;
pub const AV_CH_TOP_FRONT_RIGHT: u64 = 1 << AVC::TOP_FRONT_RIGHT.0;
pub const AV_CH_TOP_BACK_LEFT: u64 = 1 << AVC::TOP_BACK_LEFT.0;
pub const AV_CH_TOP_BACK_CENTER: u64 = 1 << AVC::TOP_BACK_CENTER.0;
pub const AV_CH_TOP_BACK_RIGHT: u64 = 1 << AVC::TOP_BACK_RIGHT.0;
pub const AV_CH_STEREO_LEFT: u64 = 1 << AVC::STEREO_LEFT.0;
pub const AV_CH_STEREO_RIGHT: u64 = 1 << AVC::STEREO_RIGHT.0;
pub const AV_CH_WIDE_LEFT: u64 = 1 << AVC::WIDE_LEFT.0;
pub const AV_CH_WIDE_RIGHT: u64 = 1 << AVC::WIDE_RIGHT.0;
pub const AV_CH_SURROUND_DIRECT_LEFT: u64 = 1 << AVC::SURROUND_DIRECT_LEFT.0;
pub const AV_CH_SURROUND_DIRECT_RIGHT: u64 = 1 << AVC::SURROUND_DIRECT_RIGHT.0;
pub const AV_CH_LOW_FREQUENCY_2: u64 = 1 << AVC::LOW_FREQUENCY_2.0;
pub const AV_CH_TOP_SIDE_LEFT: u64 = 1 << AVC::TOP_SIDE_LEFT.0;
pub const AV_CH_TOP_SIDE_RIGHT: u64 = 1 << AVC::TOP_SIDE_RIGHT.0;
pub const AV_CH_BOTTOM_FRONT_CENTER: u64 = 1 << AVC::BOTTOM_FRONT_CENTER.0;
pub const AV_CH_BOTTOM_FRONT_LEFT: u64 = 1 << AVC::BOTTOM_FRONT_LEFT.0;
pub const AV_CH_BOTTOM_FRONT_RIGHT: u64 = 1 << AVC::BOTTOM_FRONT_RIGHT.0;

#[cfg(feature = "ffmpeg_7_1")]
pub const AV_CH_SIDE_SURROUND_LEFT: u64 = 1 << AVC::SIDE_SURROUND_LEFT.0;
#[cfg(feature = "ffmpeg_7_1")]
pub const AV_CH_SIDE_SURROUND_RIGHT: u64 = 1 << AVC::SIDE_SURROUND_RIGHT.0;
#[cfg(feature = "ffmpeg_7_1")]
pub const AV_CH_TOP_SURROUND_LEFT: u64 = 1 << AVC::TOP_SURROUND_LEFT.0;
#[cfg(feature = "ffmpeg_7_1")]
pub const AV_CH_TOP_SURROUND_RIGHT: u64 = 1 << AVC::TOP_SURROUND_RIGHT.0;

#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CH_BINAURAL_LEFT: u64 = 1 << AVC::BINAURAL_LEFT.0;
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CH_BINAURAL_RIGHT: u64 = 1 << AVC::BINAURAL_RIGHT.0;

// Audio channel layouts
pub const AV_CH_LAYOUT_MONO: u64 = AV_CH_FRONT_CENTER;
pub const AV_CH_LAYOUT_STEREO: u64 = AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT;
pub const AV_CH_LAYOUT_2POINT1: u64 = AV_CH_LAYOUT_STEREO | AV_CH_LOW_FREQUENCY;
pub const AV_CH_LAYOUT_2_1: u64 = AV_CH_LAYOUT_STEREO | AV_CH_BACK_CENTER;
pub const AV_CH_LAYOUT_SURROUND: u64 = AV_CH_LAYOUT_STEREO | AV_CH_FRONT_CENTER;
pub const AV_CH_LAYOUT_3POINT1: u64 = AV_CH_LAYOUT_SURROUND | AV_CH_LOW_FREQUENCY;
pub const AV_CH_LAYOUT_4POINT0: u64 = AV_CH_LAYOUT_SURROUND | AV_CH_BACK_CENTER;
pub const AV_CH_LAYOUT_4POINT1: u64 = AV_CH_LAYOUT_4POINT0 | AV_CH_LOW_FREQUENCY;
pub const AV_CH_LAYOUT_2_2: u64 = AV_CH_LAYOUT_STEREO | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
pub const AV_CH_LAYOUT_QUAD: u64 = AV_CH_LAYOUT_STEREO | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
pub const AV_CH_LAYOUT_5POINT0: u64 = AV_CH_LAYOUT_SURROUND | AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT;
pub const AV_CH_LAYOUT_5POINT1: u64 = AV_CH_LAYOUT_5POINT0 | AV_CH_LOW_FREQUENCY;
pub const AV_CH_LAYOUT_5POINT0_BACK: u64 =
    AV_CH_LAYOUT_SURROUND | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
pub const AV_CH_LAYOUT_5POINT1_BACK: u64 = AV_CH_LAYOUT_5POINT0_BACK | AV_CH_LOW_FREQUENCY;
pub const AV_CH_LAYOUT_6POINT0: u64 = AV_CH_LAYOUT_5POINT0 | AV_CH_BACK_CENTER;
pub const AV_CH_LAYOUT_6POINT0_FRONT: u64 =
    AV_CH_LAYOUT_2_2 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER;
pub const AV_CH_LAYOUT_HEXAGONAL: u64 = AV_CH_LAYOUT_5POINT0_BACK | AV_CH_BACK_CENTER;
pub const AV_CH_LAYOUT_3POINT1POINT2: u64 =
    AV_CH_LAYOUT_3POINT1 | AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT;
pub const AV_CH_LAYOUT_6POINT1: u64 = AV_CH_LAYOUT_5POINT1 | AV_CH_BACK_CENTER;
pub const AV_CH_LAYOUT_6POINT1_BACK: u64 = AV_CH_LAYOUT_5POINT1_BACK | AV_CH_BACK_CENTER;
pub const AV_CH_LAYOUT_6POINT1_FRONT: u64 = AV_CH_LAYOUT_6POINT0_FRONT | AV_CH_LOW_FREQUENCY;
pub const AV_CH_LAYOUT_7POINT0: u64 = AV_CH_LAYOUT_5POINT0 | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
pub const AV_CH_LAYOUT_7POINT0_FRONT: u64 =
    AV_CH_LAYOUT_5POINT0 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER;
pub const AV_CH_LAYOUT_7POINT1: u64 = AV_CH_LAYOUT_5POINT1 | AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT;
pub const AV_CH_LAYOUT_7POINT1_WIDE: u64 =
    AV_CH_LAYOUT_5POINT1 | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER;
pub const AV_CH_LAYOUT_7POINT1_WIDE_BACK: u64 =
    AV_CH_LAYOUT_5POINT1_BACK | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER;
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CH_LAYOUT_5POINT1POINT2: u64 =
    AV_CH_LAYOUT_5POINT1 | AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT;
pub const AV_CH_LAYOUT_5POINT1POINT2_BACK: u64 =
    AV_CH_LAYOUT_5POINT1_BACK | AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT;
pub const AV_CH_LAYOUT_OCTAGONAL: u64 =
    AV_CH_LAYOUT_5POINT0 | AV_CH_BACK_LEFT | AV_CH_BACK_CENTER | AV_CH_BACK_RIGHT;
pub const AV_CH_LAYOUT_CUBE: u64 = AV_CH_LAYOUT_QUAD
    | AV_CH_TOP_FRONT_LEFT
    | AV_CH_TOP_FRONT_RIGHT
    | AV_CH_TOP_BACK_LEFT
    | AV_CH_TOP_BACK_RIGHT;
pub const AV_CH_LAYOUT_5POINT1POINT4_BACK: u64 =
    AV_CH_LAYOUT_5POINT1POINT2_BACK | AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT;
pub const AV_CH_LAYOUT_7POINT1POINT2: u64 =
    AV_CH_LAYOUT_7POINT1 | AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT;
pub const AV_CH_LAYOUT_7POINT1POINT4_BACK: u64 =
    AV_CH_LAYOUT_7POINT1POINT2 | AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT;
#[cfg(feature = "ffmpeg_7_0")]
pub const AV_CH_LAYOUT_7POINT2POINT3: u64 =
    AV_CH_LAYOUT_7POINT1POINT2 | AV_CH_TOP_BACK_CENTER | AV_CH_LOW_FREQUENCY_2;
#[cfg(feature = "ffmpeg_7_0")]
pub const AV_CH_LAYOUT_9POINT1POINT4_BACK: u64 =
    AV_CH_LAYOUT_7POINT1POINT4_BACK | AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER;
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CH_LAYOUT_9POINT1POINT6: u64 =
    AV_CH_LAYOUT_9POINT1POINT4_BACK | AV_CH_TOP_SIDE_LEFT | AV_CH_TOP_SIDE_RIGHT;
pub const AV_CH_LAYOUT_HEXADECAGONAL: u64 = AV_CH_LAYOUT_OCTAGONAL
    | AV_CH_WIDE_LEFT
    | AV_CH_WIDE_RIGHT
    | AV_CH_TOP_BACK_LEFT
    | AV_CH_TOP_BACK_RIGHT
    | AV_CH_TOP_BACK_CENTER
    | AV_CH_TOP_FRONT_CENTER
    | AV_CH_TOP_FRONT_LEFT
    | AV_CH_TOP_FRONT_RIGHT;
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CH_LAYOUT_BINAURAL: u64 = AV_CH_BINAURAL_LEFT | AV_CH_BINAURAL_RIGHT;
pub const AV_CH_LAYOUT_STEREO_DOWNMIX: u64 = AV_CH_STEREO_LEFT | AV_CH_STEREO_RIGHT;
pub const AV_CH_LAYOUT_22POINT2: u64 = AV_CH_LAYOUT_5POINT1_BACK
    | AV_CH_FRONT_LEFT_OF_CENTER
    | AV_CH_FRONT_RIGHT_OF_CENTER
    | AV_CH_BACK_CENTER
    | AV_CH_LOW_FREQUENCY_2
    | AV_CH_SIDE_LEFT
    | AV_CH_SIDE_RIGHT
    | AV_CH_TOP_FRONT_LEFT
    | AV_CH_TOP_FRONT_RIGHT
    | AV_CH_TOP_FRONT_CENTER
    | AV_CH_TOP_CENTER
    | AV_CH_TOP_BACK_LEFT
    | AV_CH_TOP_BACK_RIGHT
    | AV_CH_TOP_SIDE_LEFT
    | AV_CH_TOP_SIDE_RIGHT
    | AV_CH_TOP_BACK_CENTER
    | AV_CH_BOTTOM_FRONT_CENTER
    | AV_CH_BOTTOM_FRONT_LEFT
    | AV_CH_BOTTOM_FRONT_RIGHT;

pub const AV_CH_LAYOUT_7POINT1_TOP_BACK: u64 = AV_CH_LAYOUT_5POINT1POINT2_BACK;

// Audio channel layouts as AVChannelLayout
pub const fn AV_CHANNEL_LAYOUT_MASK(nb_channels: c_int, channel_mask: u64) -> AVChannelLayout {
    AVChannelLayout {
        order: AVChannelOrder::NATIVE,
        nb_channels,
        u: crate::AVChannelLayout__bindgen_ty_1 { mask: channel_mask },
        opaque: std::ptr::null_mut(),
    }
}

pub const AV_CHANNEL_LAYOUT_MONO: AVChannelLayout = AV_CHANNEL_LAYOUT_MASK(1, AV_CH_LAYOUT_MONO);
pub const AV_CHANNEL_LAYOUT_STEREO: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(2, AV_CH_LAYOUT_STEREO);
pub const AV_CHANNEL_LAYOUT_2POINT1: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(3, AV_CH_LAYOUT_2POINT1);
pub const AV_CHANNEL_LAYOUT_2_1: AVChannelLayout = AV_CHANNEL_LAYOUT_MASK(3, AV_CH_LAYOUT_2_1);
pub const AV_CHANNEL_LAYOUT_SURROUND: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(3, AV_CH_LAYOUT_SURROUND);
pub const AV_CHANNEL_LAYOUT_3POINT1: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_3POINT1);
pub const AV_CHANNEL_LAYOUT_4POINT0: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_4POINT0);
pub const AV_CHANNEL_LAYOUT_4POINT1: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(5, AV_CH_LAYOUT_4POINT1);
pub const AV_CHANNEL_LAYOUT_2_2: AVChannelLayout = AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_2_2);
pub const AV_CHANNEL_LAYOUT_QUAD: AVChannelLayout = AV_CHANNEL_LAYOUT_MASK(4, AV_CH_LAYOUT_QUAD);
pub const AV_CHANNEL_LAYOUT_5POINT0: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(5, AV_CH_LAYOUT_5POINT0);
pub const AV_CHANNEL_LAYOUT_5POINT1: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_5POINT1);
pub const AV_CHANNEL_LAYOUT_5POINT0_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(5, AV_CH_LAYOUT_5POINT0_BACK);
pub const AV_CHANNEL_LAYOUT_5POINT1_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_5POINT1_BACK);
pub const AV_CHANNEL_LAYOUT_6POINT0: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_6POINT0);
pub const AV_CHANNEL_LAYOUT_6POINT0_FRONT: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_6POINT0_FRONT);
pub const AV_CHANNEL_LAYOUT_3POINT1POINT2: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_3POINT1POINT2);
pub const AV_CHANNEL_LAYOUT_HEXAGONAL: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(6, AV_CH_LAYOUT_HEXAGONAL);
pub const AV_CHANNEL_LAYOUT_6POINT1: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_6POINT1);
pub const AV_CHANNEL_LAYOUT_6POINT1_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_6POINT1_BACK);
pub const AV_CHANNEL_LAYOUT_6POINT1_FRONT: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_6POINT1_FRONT);
pub const AV_CHANNEL_LAYOUT_7POINT0: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_7POINT0);
pub const AV_CHANNEL_LAYOUT_7POINT0_FRONT: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(7, AV_CH_LAYOUT_7POINT0_FRONT);
pub const AV_CHANNEL_LAYOUT_7POINT1: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_7POINT1);
pub const AV_CHANNEL_LAYOUT_7POINT1_WIDE: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_7POINT1_WIDE);
pub const AV_CHANNEL_LAYOUT_7POINT1_WIDE_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_7POINT1_WIDE_BACK);
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CHANNEL_LAYOUT_5POINT1POINT2: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_5POINT1POINT2);
pub const AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_5POINT1POINT2_BACK);
pub const AV_CHANNEL_LAYOUT_OCTAGONAL: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_OCTAGONAL);
pub const AV_CHANNEL_LAYOUT_CUBE: AVChannelLayout = AV_CHANNEL_LAYOUT_MASK(8, AV_CH_LAYOUT_CUBE);
pub const AV_CHANNEL_LAYOUT_5POINT1POINT4_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(10, AV_CH_LAYOUT_5POINT1POINT4_BACK);
pub const AV_CHANNEL_LAYOUT_7POINT1POINT2: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(10, AV_CH_LAYOUT_7POINT1POINT2);
pub const AV_CHANNEL_LAYOUT_7POINT1POINT4_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(12, AV_CH_LAYOUT_7POINT1POINT4_BACK);
#[cfg(feature = "ffmpeg_7_0")]
pub const AV_CHANNEL_LAYOUT_7POINT2POINT3: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(12, AV_CH_LAYOUT_7POINT2POINT3);
#[cfg(feature = "ffmpeg_7_0")]
pub const AV_CHANNEL_LAYOUT_9POINT1POINT4_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(14, AV_CH_LAYOUT_9POINT1POINT4_BACK);
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CHANNEL_LAYOUT_9POINT1POINT6: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(16, AV_CH_LAYOUT_9POINT1POINT6);
pub const AV_CHANNEL_LAYOUT_HEXADECAGONAL: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(16, AV_CH_LAYOUT_HEXADECAGONAL);
#[cfg(feature = "ffmpeg_8_0")]
pub const AV_CHANNEL_LAYOUT_BINAURAL: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(2, AV_CH_LAYOUT_BINAURAL);
pub const AV_CHANNEL_LAYOUT_STEREO_DOWNMIX: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(2, AV_CH_LAYOUT_STEREO_DOWNMIX);
pub const AV_CHANNEL_LAYOUT_22POINT2: AVChannelLayout =
    AV_CHANNEL_LAYOUT_MASK(24, AV_CH_LAYOUT_22POINT2);

pub const AV_CHANNEL_LAYOUT_7POINT1_TOP_BACK: AVChannelLayout =
    AV_CHANNEL_LAYOUT_5POINT1POINT2_BACK;

#[cfg(test)]
mod test {
    use super::*;
    use libc::c_char;

    // TODO: Missing: Ambisonic layout

    const EMPTY: AVChannelLayout = AVChannelLayout::empty();
    const UNSPEC: AVChannelLayout = {
        let mut layout = AVChannelLayout::empty();
        layout.nb_channels = 5;
        layout
    };

    const NATIVE: AVChannelLayout = {
        let mut layout = AVChannelLayout::empty();
        layout.order = AVChannelOrder::NATIVE;
        layout.nb_channels = 6;
        layout.u.mask = AV_CH_LAYOUT_5POINT1;
        layout
    };

    const fn c_string<const N: usize>(byte_str: &[u8; N]) -> [c_char; 16] {
        // Need at least one NUL byte at the end
        assert!(N < 16, "input string is too long (max 15 char)");

        let mut result = [0; 16];
        let mut i = 0;

        while i < N {
            result[i] = byte_str[i] as c_char;
            i += 1;
        }

        result
    }

    fn custom_ch<const N: usize>(id: AVChannel, name: &[u8; N]) -> AVChannelCustom {
        AVChannelCustom {
            id,
            name: c_string(name),
            opaque: null_mut(),
        }
    }

    fn custom() -> AVChannelLayout {
        let mut my_data = vec![0u8; 200];

        let channels = [
            custom_ch(AVChannel::FRONT_LEFT, b"front left"),
            custom_ch(AVChannel::TOP_FRONT_RIGHT, b"top front right"),
            custom_ch(AVChannel::FRONT_RIGHT, b"front right"),
            custom_ch(AVChannel::BOTTOM_FRONT_RIGHT, b"btm frt right"),
            custom_ch(AVChannel::TOP_SIDE_LEFT, b"top side left"),
            AVChannelCustom {
                id: AVChannel::LOW_FREQUENCY,
                name: c_string(b"subwoofer"),
                opaque: my_data.as_mut_ptr() as _,
            },
        ];

        let mut layout = AVChannelLayout::empty();
        layout.order = AVChannelOrder::CUSTOM;
        layout.nb_channels = channels.len() as c_int;
        unsafe {
            layout.u.map = av_calloc(channels.len(), size_of::<AVChannelCustom>()) as _;
            assert!(!layout.u.map.is_null());
        }

        for (i, ch) in channels.iter().enumerate() {
            unsafe {
                std::ptr::write(layout.u.map.add(i), *ch);
            }
        }

        layout
    }

    #[test]
    fn check() {
        let tests = [
            (EMPTY, false),
            (UNSPEC, true),
            (NATIVE, true),
            (custom(), true),
        ];

        for (i, (layout, valid)) in tests.iter().enumerate() {
            unsafe {
                println!("{i}");
                assert!((av_channel_layout_check(layout as _) != 0) == *valid);
            }
        }
    }

    #[test]
    fn debug() {
        for layout in [EMPTY, UNSPEC, NATIVE, custom()] {
            println!("{layout:?}");
        }
    }

    #[test]
    fn clone_eq() {
        for layout in [EMPTY, UNSPEC, NATIVE, custom()] {
            let cloned = layout.clone();
            assert_eq!(layout, cloned);
        }
    }
}