1 /*
2  * This file is part of mpv.
3  *
4  * mpv is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * mpv is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with mpv.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef MP_CHMAP_H
19 #define MP_CHMAP_H
20 
21 #include <inttypes.h>
22 #include <stdbool.h>
23 #include "misc/bstr.h"
24 
25 #define MP_NUM_CHANNELS 16
26 
27 // Speaker a channel can be assigned to.
28 // This corresponds to WAVEFORMATEXTENSIBLE channel mask bit indexes.
29 // E.g. channel_mask = (1 << MP_SPEAKER_ID_FL) | ...
30 enum mp_speaker_id {
31     // Official WAVEFORMATEXTENSIBLE (shortened names)
32     MP_SPEAKER_ID_FL = 0,       // FRONT_LEFT
33     MP_SPEAKER_ID_FR,           // FRONT_RIGHT
34     MP_SPEAKER_ID_FC,           // FRONT_CENTER
35     MP_SPEAKER_ID_LFE,          // LOW_FREQUENCY
36     MP_SPEAKER_ID_BL,           // BACK_LEFT
37     MP_SPEAKER_ID_BR,           // BACK_RIGHT
38     MP_SPEAKER_ID_FLC,          // FRONT_LEFT_OF_CENTER
39     MP_SPEAKER_ID_FRC,          // FRONT_RIGHT_OF_CENTER
40     MP_SPEAKER_ID_BC,           // BACK_CENTER
41     MP_SPEAKER_ID_SL,           // SIDE_LEFT
42     MP_SPEAKER_ID_SR,           // SIDE_RIGHT
43     MP_SPEAKER_ID_TC,           // TOP_CENTER
44     MP_SPEAKER_ID_TFL,          // TOP_FRONT_LEFT
45     MP_SPEAKER_ID_TFC,          // TOP_FRONT_CENTER
46     MP_SPEAKER_ID_TFR,          // TOP_FRONT_RIGHT
47     MP_SPEAKER_ID_TBL,          // TOP_BACK_LEFT
48     MP_SPEAKER_ID_TBC,          // TOP_BACK_CENTER
49     MP_SPEAKER_ID_TBR,          // TOP_BACK_RIGHT
50      // Unofficial/libav* extensions
51     MP_SPEAKER_ID_DL = 29,      // STEREO_LEFT (stereo downmix special speakers)
52     MP_SPEAKER_ID_DR,           // STEREO_RIGHT
53     MP_SPEAKER_ID_WL,           // WIDE_LEFT
54     MP_SPEAKER_ID_WR,           // WIDE_RIGHT
55     MP_SPEAKER_ID_SDL,          // SURROUND_DIRECT_LEFT
56     MP_SPEAKER_ID_SDR,          // SURROUND_DIRECT_RIGHT
57     MP_SPEAKER_ID_LFE2,         // LOW_FREQUENCY_2
58 
59     // Speaker IDs >= 64 are not representable in WAVEFORMATEXTENSIBLE or libav*.
60 
61     // "Silent" channels. These are sometimes used to insert padding for
62     // unused channels. Unlike other speaker types, multiple of these can
63     // occur in a single mp_chmap.
64     MP_SPEAKER_ID_NA = 64,
65 
66     // Including the unassigned IDs in between. This is not a valid ID anymore,
67     // but is still within uint8_t.
68     MP_SPEAKER_ID_COUNT,
69 };
70 
71 struct mp_chmap {
72     uint8_t num; // number of channels
73     // Given a channel n, speaker[n] is the speaker ID driven by that channel.
74     // Entries after speaker[num - 1] are undefined.
75     uint8_t speaker[MP_NUM_CHANNELS];
76 };
77 
78 #define MP_SP(speaker) MP_SPEAKER_ID_ ## speaker
79 
80 #define MP_CHMAP2(a, b) \
81     {2, {MP_SP(a), MP_SP(b)}}
82 #define MP_CHMAP3(a, b, c) \
83     {3, {MP_SP(a), MP_SP(b), MP_SP(c)}}
84 #define MP_CHMAP4(a, b, c, d) \
85     {4, {MP_SP(a), MP_SP(b), MP_SP(c), MP_SP(d)}}
86 #define MP_CHMAP5(a, b, c, d, e) \
87     {5, {MP_SP(a), MP_SP(b), MP_SP(c), MP_SP(d), MP_SP(e)}}
88 #define MP_CHMAP6(a, b, c, d, e, f) \
89     {6, {MP_SP(a), MP_SP(b), MP_SP(c), MP_SP(d), MP_SP(e), MP_SP(f)}}
90 #define MP_CHMAP7(a, b, c, d, e, f, g) \
91     {7, {MP_SP(a), MP_SP(b), MP_SP(c), MP_SP(d), MP_SP(e), MP_SP(f), MP_SP(g)}}
92 #define MP_CHMAP8(a, b, c, d, e, f, g, h) \
93     {8, {MP_SP(a), MP_SP(b), MP_SP(c), MP_SP(d), MP_SP(e), MP_SP(f), MP_SP(g), MP_SP(h)}}
94 
95 #define MP_CHMAP_INIT_MONO {1, {MP_SPEAKER_ID_FC}}
96 #define MP_CHMAP_INIT_STEREO MP_CHMAP2(FL, FR)
97 
98 bool mp_chmap_is_valid(const struct mp_chmap *src);
99 bool mp_chmap_is_empty(const struct mp_chmap *src);
100 bool mp_chmap_is_unknown(const struct mp_chmap *src);
101 bool mp_chmap_equals(const struct mp_chmap *a, const struct mp_chmap *b);
102 bool mp_chmap_equals_reordered(const struct mp_chmap *a, const struct mp_chmap *b);
103 bool mp_chmap_is_stereo(const struct mp_chmap *src);
104 
105 void mp_chmap_reorder_norm(struct mp_chmap *map);
106 void mp_chmap_remove_na(struct mp_chmap *map);
107 void mp_chmap_fill_na(struct mp_chmap *map, int num);
108 
109 void mp_chmap_from_channels(struct mp_chmap *dst, int num_channels);
110 void mp_chmap_set_unknown(struct mp_chmap *dst, int num_channels);
111 
112 uint64_t mp_chmap_to_lavc(const struct mp_chmap *src);
113 uint64_t mp_chmap_to_lavc_unchecked(const struct mp_chmap *src);
114 void mp_chmap_from_lavc(struct mp_chmap *dst, uint64_t src);
115 
116 bool mp_chmap_is_lavc(const struct mp_chmap *src);
117 void mp_chmap_reorder_to_lavc(struct mp_chmap *map);
118 
119 void mp_chmap_get_reorder(int src[MP_NUM_CHANNELS], const struct mp_chmap *from,
120                           const struct mp_chmap *to);
121 
122 int mp_chmap_diffn(const struct mp_chmap *a, const struct mp_chmap *b);
123 
124 char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src);
125 #define mp_chmap_to_str(m) mp_chmap_to_str_buf((char[64]){0}, 64, (m))
126 
127 char *mp_chmap_to_str_hr_buf(char *buf, size_t buf_size, const struct mp_chmap *src);
128 #define mp_chmap_to_str_hr(m) mp_chmap_to_str_hr_buf((char[128]){0}, 128, (m))
129 
130 bool mp_chmap_from_str(struct mp_chmap *dst, bstr src);
131 
132 struct mp_log;
133 void mp_chmap_print_help(struct mp_log *log);
134 
135 // Use these to avoid chaos in case lavc's definition should diverge from MS.
136 #define mp_chmap_to_waveext mp_chmap_to_lavc
137 #define mp_chmap_from_waveext mp_chmap_from_lavc
138 #define mp_chmap_is_waveext mp_chmap_is_lavc
139 #define mp_chmap_reorder_to_waveext mp_chmap_reorder_to_lavc
140 
141 #endif
142