FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mov_chan.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 103 127 81.1%
Branches: 67 96 69.8%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2011 Justin Ruggles
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 /**
22 * mov 'chan' tag reading/writing.
23 * @author Justin Ruggles
24 */
25
26 #include <stdint.h>
27
28 #include "libavutil/channel_layout.h"
29 #include "libavcodec/codec_id.h"
30 #include "mov_chan.h"
31
32 struct MovChannelLayoutMap {
33 uint32_t tag;
34 uint64_t layout;
35 };
36
37 static const struct MovChannelLayoutMap mov_ch_layout_map_misc[] = {
38 { MOV_CH_LAYOUT_USE_DESCRIPTIONS, 0 },
39 { MOV_CH_LAYOUT_USE_BITMAP, 0 },
40 { MOV_CH_LAYOUT_DISCRETEINORDER, 0 },
41 { MOV_CH_LAYOUT_UNKNOWN, 0 },
42 { MOV_CH_LAYOUT_TMH_10_2_STD, 0 }, // L, R, C, Vhc, Lsd, Rsd,
43 // Ls, Rs, Vhl, Vhr, Lw, Rw,
44 // Csd, Cs, LFE1, LFE2
45 { MOV_CH_LAYOUT_TMH_10_2_FULL, 0 }, // L, R, C, Vhc, Lsd, Rsd,
46 // Ls, Rs, Vhl, Vhr, Lw, Rw,
47 // Csd, Cs, LFE1, LFE2, Lc, Rc,
48 // HI, VI, Haptic
49 { 0, 0 },
50 };
51
52 static const struct MovChannelLayoutMap mov_ch_layout_map_1ch[] = {
53 { MOV_CH_LAYOUT_MONO, AV_CH_LAYOUT_MONO }, // C
54 { 0, 0 },
55 };
56
57 static const struct MovChannelLayoutMap mov_ch_layout_map_2ch[] = {
58 { MOV_CH_LAYOUT_STEREO, AV_CH_LAYOUT_STEREO }, // L, R
59 { MOV_CH_LAYOUT_STEREOHEADPHONES, AV_CH_LAYOUT_STEREO }, // L, R
60 { MOV_CH_LAYOUT_BINAURAL, AV_CH_LAYOUT_STEREO }, // L, R
61 { MOV_CH_LAYOUT_MIDSIDE, AV_CH_LAYOUT_STEREO }, // C, sides
62 { MOV_CH_LAYOUT_XY, AV_CH_LAYOUT_STEREO }, // X (left), Y (right)
63
64 { MOV_CH_LAYOUT_MATRIXSTEREO, AV_CH_LAYOUT_STEREO_DOWNMIX }, // Lt, Rt
65
66 { MOV_CH_LAYOUT_AC3_1_0_1, AV_CH_LAYOUT_MONO | // C, LFE
67 AV_CH_LOW_FREQUENCY },
68 { 0, 0 },
69 };
70
71 static const struct MovChannelLayoutMap mov_ch_layout_map_3ch[] = {
72 { MOV_CH_LAYOUT_MPEG_3_0_A, AV_CH_LAYOUT_SURROUND }, // L, R, C
73 { MOV_CH_LAYOUT_MPEG_3_0_B, AV_CH_LAYOUT_SURROUND }, // C, L, R
74 { MOV_CH_LAYOUT_AC3_3_0, AV_CH_LAYOUT_SURROUND }, // L, C, R
75
76 { MOV_CH_LAYOUT_ITU_2_1, AV_CH_LAYOUT_2_1 }, // L, R, Cs
77
78 { MOV_CH_LAYOUT_DVD_4, AV_CH_LAYOUT_2POINT1 }, // L, R, LFE
79 { 0, 0 },
80 };
81
82 static const struct MovChannelLayoutMap mov_ch_layout_map_4ch[] = {
83 { MOV_CH_LAYOUT_AMBISONIC_B_FORMAT, 0 }, // W, X, Y, Z
84
85 { MOV_CH_LAYOUT_QUADRAPHONIC, AV_CH_LAYOUT_QUAD }, // L, R, Rls, Rrs
86
87 { MOV_CH_LAYOUT_MPEG_4_0_A, AV_CH_LAYOUT_4POINT0 }, // L, R, C, Cs
88 { MOV_CH_LAYOUT_MPEG_4_0_B, AV_CH_LAYOUT_4POINT0 }, // C, L, R, Cs
89 { MOV_CH_LAYOUT_AC3_3_1, AV_CH_LAYOUT_4POINT0 }, // L, C, R, Cs
90
91 { MOV_CH_LAYOUT_ITU_2_2, AV_CH_LAYOUT_2_2 }, // L, R, Ls, Rs
92
93 { MOV_CH_LAYOUT_DVD_5, AV_CH_LAYOUT_2_1 | // L, R, LFE, Cs
94 AV_CH_LOW_FREQUENCY },
95 { MOV_CH_LAYOUT_AC3_2_1_1, AV_CH_LAYOUT_2_1 | // L, R, Cs, LFE
96 AV_CH_LOW_FREQUENCY },
97
98 { MOV_CH_LAYOUT_DVD_10, AV_CH_LAYOUT_3POINT1 }, // L, R, C, LFE
99 { MOV_CH_LAYOUT_AC3_3_0_1, AV_CH_LAYOUT_3POINT1 }, // L, C, R, LFE
100 { MOV_CH_LAYOUT_DTS_3_1, AV_CH_LAYOUT_3POINT1 }, // C, L, R, LFE
101 { 0, 0 },
102 };
103
104 static const struct MovChannelLayoutMap mov_ch_layout_map_5ch[] = {
105 { MOV_CH_LAYOUT_PENTAGONAL, AV_CH_LAYOUT_5POINT0_BACK }, // L, R, Rls, Rrs, C
106
107 { MOV_CH_LAYOUT_MPEG_5_0_A, AV_CH_LAYOUT_5POINT0 }, // L, R, C, Ls, Rs
108 { MOV_CH_LAYOUT_MPEG_5_0_B, AV_CH_LAYOUT_5POINT0 }, // L, R, Ls, Rs, C
109 { MOV_CH_LAYOUT_MPEG_5_0_C, AV_CH_LAYOUT_5POINT0 }, // L, C, R, Ls, Rs
110 { MOV_CH_LAYOUT_MPEG_5_0_D, AV_CH_LAYOUT_5POINT0 }, // C, L, R, Ls, Rs
111
112 { MOV_CH_LAYOUT_DVD_6, AV_CH_LAYOUT_2_2 | // L, R, LFE, Ls, Rs
113 AV_CH_LOW_FREQUENCY },
114 { MOV_CH_LAYOUT_DVD_18, AV_CH_LAYOUT_2_2 | // L, R, Ls, Rs, LFE
115 AV_CH_LOW_FREQUENCY },
116
117 { MOV_CH_LAYOUT_DVD_11, AV_CH_LAYOUT_4POINT1 }, // L, R, C, LFE, Cs
118 { MOV_CH_LAYOUT_AC3_3_1_1, AV_CH_LAYOUT_4POINT1 }, // L, C, R, Cs, LFE
119 { MOV_CH_LAYOUT_DTS_4_1, AV_CH_LAYOUT_4POINT1 }, // C, L, R, Cs, LFE
120 { 0, 0 },
121 };
122
123 static const struct MovChannelLayoutMap mov_ch_layout_map_6ch[] = {
124 { MOV_CH_LAYOUT_HEXAGONAL, AV_CH_LAYOUT_HEXAGONAL }, // L, R, Rls, Rrs, C, Cs
125 { MOV_CH_LAYOUT_DTS_6_0_C, AV_CH_LAYOUT_HEXAGONAL }, // C, Cs, L, R, Rls, Rrs
126
127 { MOV_CH_LAYOUT_MPEG_5_1_A, AV_CH_LAYOUT_5POINT1 }, // L, R, C, LFE, Ls, Rs
128 { MOV_CH_LAYOUT_MPEG_5_1_B, AV_CH_LAYOUT_5POINT1 }, // L, R, Ls, Rs, C, LFE
129 { MOV_CH_LAYOUT_MPEG_5_1_C, AV_CH_LAYOUT_5POINT1 }, // L, C, R, Ls, Rs, LFE
130 { MOV_CH_LAYOUT_MPEG_5_1_D, AV_CH_LAYOUT_5POINT1 }, // C, L, R, Ls, Rs, LFE
131
132 { MOV_CH_LAYOUT_AUDIOUNIT_6_0, AV_CH_LAYOUT_6POINT0 }, // L, R, Ls, Rs, C, Cs
133 { MOV_CH_LAYOUT_AAC_6_0, AV_CH_LAYOUT_6POINT0 }, // C, L, R, Ls, Rs, Cs
134 { MOV_CH_LAYOUT_EAC3_6_0_A, AV_CH_LAYOUT_6POINT0 }, // L, C, R, Ls, Rs, Cs
135
136 { MOV_CH_LAYOUT_DTS_6_0_A, AV_CH_LAYOUT_6POINT0_FRONT }, // Lc, Rc, L, R, Ls, Rs
137
138 { MOV_CH_LAYOUT_DTS_6_0_B, AV_CH_LAYOUT_5POINT0_BACK | // C, L, R, Rls, Rrs, Ts
139 AV_CH_TOP_CENTER },
140 { 0, 0 },
141 };
142
143 static const struct MovChannelLayoutMap mov_ch_layout_map_7ch[] = {
144 { MOV_CH_LAYOUT_MPEG_6_1_A, AV_CH_LAYOUT_6POINT1 }, // L, R, C, LFE, Ls, Rs, Cs
145 { MOV_CH_LAYOUT_AAC_6_1, AV_CH_LAYOUT_6POINT1 }, // C, L, R, Ls, Rs, Cs, LFE
146 { MOV_CH_LAYOUT_EAC3_6_1_A, AV_CH_LAYOUT_6POINT1 }, // L, C, R, Ls, Rs, LFE, Cs
147 { MOV_CH_LAYOUT_DTS_6_1_D, AV_CH_LAYOUT_6POINT1 }, // C, L, R, Ls, Rs, LFE, Cs
148
149 { MOV_CH_LAYOUT_AUDIOUNIT_7_0, AV_CH_LAYOUT_7POINT0 }, // L, R, Ls, Rs, C, Rls, Rrs
150 { MOV_CH_LAYOUT_AAC_7_0, AV_CH_LAYOUT_7POINT0 }, // C, L, R, Ls, Rs, Rls, Rrs
151 { MOV_CH_LAYOUT_EAC3_7_0_A, AV_CH_LAYOUT_7POINT0 }, // L, C, R, Ls, Rs, Rls, Rrs
152
153 { MOV_CH_LAYOUT_AUDIOUNIT_7_0_FRONT, AV_CH_LAYOUT_7POINT0_FRONT }, // L, R, Ls, Rs, C, Lc, Rc
154 { MOV_CH_LAYOUT_DTS_7_0, AV_CH_LAYOUT_7POINT0_FRONT }, // Lc, C, Rc, L, R, Ls, Rs
155
156 { MOV_CH_LAYOUT_EAC3_6_1_B, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Ts
157 AV_CH_TOP_CENTER },
158
159 { MOV_CH_LAYOUT_EAC3_6_1_C, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Vhc
160 AV_CH_TOP_FRONT_CENTER },
161
162 { MOV_CH_LAYOUT_DTS_6_1_A, AV_CH_LAYOUT_6POINT1_FRONT }, // Lc, Rc, L, R, Ls, Rs, LFE
163
164 { MOV_CH_LAYOUT_DTS_6_1_B, AV_CH_LAYOUT_5POINT1_BACK | // C, L, R, Rls, Rrs, Ts, LFE
165 AV_CH_TOP_CENTER },
166
167 { MOV_CH_LAYOUT_DTS_6_1_C, AV_CH_LAYOUT_6POINT1_BACK }, // C, Cs, L, R, Rls, Rrs, LFE
168 { 0, 0 },
169 };
170
171 static const struct MovChannelLayoutMap mov_ch_layout_map_8ch[] = {
172 { MOV_CH_LAYOUT_OCTAGONAL, AV_CH_LAYOUT_OCTAGONAL }, // L, R, Rls, Rrs, C, Cs, Ls, Rs
173 { MOV_CH_LAYOUT_AAC_OCTAGONAL, AV_CH_LAYOUT_OCTAGONAL }, // C, L, R, Ls, Rs, Rls, Rrs, Cs
174
175 { MOV_CH_LAYOUT_CUBE, AV_CH_LAYOUT_CUBE }, // L, R, Rls, Rrs, Vhl, Vhr, Rlt, Rrt
176
177 { MOV_CH_LAYOUT_MPEG_7_1_A, AV_CH_LAYOUT_7POINT1_WIDE }, // L, R, C, LFE, Ls, Rs, Lc, Rc
178 { MOV_CH_LAYOUT_MPEG_7_1_B, AV_CH_LAYOUT_7POINT1_WIDE }, // C, Lc, Rc, L, R, Ls, Rs, LFE
179 { MOV_CH_LAYOUT_EMAGIC_DEFAULT_7_1, AV_CH_LAYOUT_7POINT1_WIDE }, // L, R, Ls, Rs, C, LFE, Lc, Rc
180 { MOV_CH_LAYOUT_EAC3_7_1_B, AV_CH_LAYOUT_7POINT1_WIDE }, // L, C, R, Ls, Rs, LFE, Lc, Rc
181 { MOV_CH_LAYOUT_DTS_7_1, AV_CH_LAYOUT_7POINT1_WIDE }, // Lc, C, Rc, L, R, Ls, Rs, LFE
182
183 { MOV_CH_LAYOUT_MPEG_7_1_C, AV_CH_LAYOUT_7POINT1 }, // L, R, C, LFE, Ls, Rs, Rls, Rrs
184 { MOV_CH_LAYOUT_EAC3_7_1_A, AV_CH_LAYOUT_7POINT1 }, // L, C, R, Ls, Rs, LFE, Rls, Rrs
185
186 { MOV_CH_LAYOUT_SMPTE_DTV, AV_CH_LAYOUT_5POINT1 | // L, R, C, LFE, Ls, Rs, Lt, Rt
187 AV_CH_LAYOUT_STEREO_DOWNMIX },
188
189 { MOV_CH_LAYOUT_EAC3_7_1_C, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Lsd, Rsd
190 AV_CH_SURROUND_DIRECT_LEFT |
191 AV_CH_SURROUND_DIRECT_RIGHT },
192
193 { MOV_CH_LAYOUT_EAC3_7_1_D, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Lw, Rw
194 AV_CH_WIDE_LEFT |
195 AV_CH_WIDE_RIGHT },
196
197 { MOV_CH_LAYOUT_EAC3_7_1_E, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Vhl, Vhr
198 AV_CH_TOP_FRONT_LEFT |
199 AV_CH_TOP_FRONT_RIGHT },
200
201 { MOV_CH_LAYOUT_EAC3_7_1_F, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Cs, Ts
202 AV_CH_BACK_CENTER |
203 AV_CH_TOP_CENTER },
204
205 { MOV_CH_LAYOUT_EAC3_7_1_G, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Cs, Vhc
206 AV_CH_BACK_CENTER |
207 AV_CH_TOP_FRONT_CENTER },
208
209 { MOV_CH_LAYOUT_EAC3_7_1_H, AV_CH_LAYOUT_5POINT1 | // L, C, R, Ls, Rs, LFE, Ts, Vhc
210 AV_CH_TOP_CENTER |
211 AV_CH_TOP_FRONT_CENTER },
212
213 { MOV_CH_LAYOUT_DTS_8_0_A, AV_CH_LAYOUT_2_2 | // Lc, Rc, L, R, Ls, Rs, Rls, Rrs
214 AV_CH_BACK_LEFT |
215 AV_CH_BACK_RIGHT |
216 AV_CH_FRONT_LEFT_OF_CENTER |
217 AV_CH_FRONT_RIGHT_OF_CENTER },
218
219 { MOV_CH_LAYOUT_DTS_8_0_B, AV_CH_LAYOUT_5POINT0 | // Lc, C, Rc, L, R, Ls, Cs, Rs
220 AV_CH_FRONT_LEFT_OF_CENTER |
221 AV_CH_FRONT_RIGHT_OF_CENTER |
222 AV_CH_BACK_CENTER },
223 { 0, 0 },
224 };
225
226 static const struct MovChannelLayoutMap mov_ch_layout_map_9ch[] = {
227 { MOV_CH_LAYOUT_DTS_8_1_A, AV_CH_LAYOUT_2_2 | // Lc, Rc, L, R, Ls, Rs, Rls, Rrs, LFE
228 AV_CH_BACK_LEFT |
229 AV_CH_BACK_RIGHT |
230 AV_CH_FRONT_LEFT_OF_CENTER |
231 AV_CH_FRONT_RIGHT_OF_CENTER |
232 AV_CH_LOW_FREQUENCY },
233
234 { MOV_CH_LAYOUT_DTS_8_1_B, AV_CH_LAYOUT_7POINT1_WIDE | // Lc, C, Rc, L, R, Ls, Cs, Rs, LFE
235 AV_CH_BACK_CENTER },
236 { 0, 0 },
237 };
238
239 static const struct MovChannelLayoutMap * const mov_ch_layout_map[] = {
240 mov_ch_layout_map_misc,
241 mov_ch_layout_map_1ch,
242 mov_ch_layout_map_2ch,
243 mov_ch_layout_map_3ch,
244 mov_ch_layout_map_4ch,
245 mov_ch_layout_map_5ch,
246 mov_ch_layout_map_6ch,
247 mov_ch_layout_map_7ch,
248 mov_ch_layout_map_8ch,
249 mov_ch_layout_map_9ch,
250 };
251
252 static const enum MovChannelLayoutTag mov_ch_layouts_aac[] = {
253 MOV_CH_LAYOUT_MONO,
254 MOV_CH_LAYOUT_STEREO,
255 MOV_CH_LAYOUT_AC3_1_0_1,
256 MOV_CH_LAYOUT_MPEG_3_0_B,
257 MOV_CH_LAYOUT_ITU_2_1,
258 MOV_CH_LAYOUT_DVD_4,
259 MOV_CH_LAYOUT_QUADRAPHONIC,
260 MOV_CH_LAYOUT_MPEG_4_0_B,
261 MOV_CH_LAYOUT_ITU_2_2,
262 MOV_CH_LAYOUT_AC3_2_1_1,
263 MOV_CH_LAYOUT_DTS_3_1,
264 MOV_CH_LAYOUT_MPEG_5_0_D,
265 MOV_CH_LAYOUT_DVD_18,
266 MOV_CH_LAYOUT_DTS_4_1,
267 MOV_CH_LAYOUT_MPEG_5_1_D,
268 MOV_CH_LAYOUT_AAC_6_0,
269 MOV_CH_LAYOUT_DTS_6_0_A,
270 MOV_CH_LAYOUT_AAC_6_1,
271 MOV_CH_LAYOUT_AAC_7_0,
272 MOV_CH_LAYOUT_DTS_6_1_A,
273 MOV_CH_LAYOUT_AAC_OCTAGONAL,
274 MOV_CH_LAYOUT_MPEG_7_1_B,
275 MOV_CH_LAYOUT_DTS_8_0_A,
276 0,
277 };
278
279 static const enum MovChannelLayoutTag mov_ch_layouts_ac3[] = {
280 MOV_CH_LAYOUT_MONO,
281 MOV_CH_LAYOUT_STEREO,
282 MOV_CH_LAYOUT_AC3_1_0_1,
283 MOV_CH_LAYOUT_AC3_3_0,
284 MOV_CH_LAYOUT_ITU_2_1,
285 MOV_CH_LAYOUT_DVD_4,
286 MOV_CH_LAYOUT_AC3_3_1,
287 MOV_CH_LAYOUT_ITU_2_2,
288 MOV_CH_LAYOUT_AC3_2_1_1,
289 MOV_CH_LAYOUT_AC3_3_0_1,
290 MOV_CH_LAYOUT_MPEG_5_0_C,
291 MOV_CH_LAYOUT_DVD_18,
292 MOV_CH_LAYOUT_AC3_3_1_1,
293 MOV_CH_LAYOUT_MPEG_5_1_C,
294 0,
295 };
296
297 static const enum MovChannelLayoutTag mov_ch_layouts_alac[] = {
298 MOV_CH_LAYOUT_MONO,
299 MOV_CH_LAYOUT_STEREO,
300 MOV_CH_LAYOUT_MPEG_3_0_B,
301 MOV_CH_LAYOUT_MPEG_4_0_B,
302 MOV_CH_LAYOUT_MPEG_5_0_D,
303 MOV_CH_LAYOUT_MPEG_5_1_D,
304 MOV_CH_LAYOUT_AAC_6_1,
305 MOV_CH_LAYOUT_MPEG_7_1_B,
306 0,
307 };
308
309 static const enum MovChannelLayoutTag mov_ch_layouts_wav[] = {
310 MOV_CH_LAYOUT_MONO,
311 MOV_CH_LAYOUT_STEREO,
312 MOV_CH_LAYOUT_MATRIXSTEREO,
313 MOV_CH_LAYOUT_MPEG_3_0_A,
314 MOV_CH_LAYOUT_QUADRAPHONIC,
315 MOV_CH_LAYOUT_MPEG_5_0_A,
316 MOV_CH_LAYOUT_MPEG_5_1_A,
317 MOV_CH_LAYOUT_MPEG_6_1_A,
318 MOV_CH_LAYOUT_MPEG_7_1_A,
319 MOV_CH_LAYOUT_MPEG_7_1_C,
320 MOV_CH_LAYOUT_SMPTE_DTV,
321 0,
322 };
323
324 static const struct {
325 enum AVCodecID codec_id;
326 const enum MovChannelLayoutTag *layouts;
327 } mov_codec_ch_layouts[] = {
328 { AV_CODEC_ID_AAC, mov_ch_layouts_aac },
329 { AV_CODEC_ID_AC3, mov_ch_layouts_ac3 },
330 { AV_CODEC_ID_ALAC, mov_ch_layouts_alac },
331 { AV_CODEC_ID_PCM_U8, mov_ch_layouts_wav },
332 { AV_CODEC_ID_PCM_S8, mov_ch_layouts_wav },
333 { AV_CODEC_ID_PCM_S16LE, mov_ch_layouts_wav },
334 { AV_CODEC_ID_PCM_S16BE, mov_ch_layouts_wav },
335 { AV_CODEC_ID_PCM_S24LE, mov_ch_layouts_wav },
336 { AV_CODEC_ID_PCM_S24BE, mov_ch_layouts_wav },
337 { AV_CODEC_ID_PCM_S32LE, mov_ch_layouts_wav },
338 { AV_CODEC_ID_PCM_S32BE, mov_ch_layouts_wav },
339 { AV_CODEC_ID_PCM_F32LE, mov_ch_layouts_wav },
340 { AV_CODEC_ID_PCM_F32BE, mov_ch_layouts_wav },
341 { AV_CODEC_ID_PCM_F64LE, mov_ch_layouts_wav },
342 { AV_CODEC_ID_PCM_F64BE, mov_ch_layouts_wav },
343 { AV_CODEC_ID_NONE, NULL },
344 };
345
346 /**
347 * Get the channel layout for the specified channel layout tag.
348 *
349 * @param[in] tag channel layout tag
350 * @param[out] bitmap channel bitmap (only used if needed)
351 * @return channel layout
352 */
353 57 static uint64_t mov_get_channel_layout(uint32_t tag, uint32_t bitmap)
354 {
355 int i, channels;
356 const struct MovChannelLayoutMap *layout_map;
357
358 /* use ff_mov_get_channel_label() to build a layout instead */
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 57 times.
57 if (tag == MOV_CH_LAYOUT_USE_DESCRIPTIONS)
360 return 0;
361
362 /* handle the use of the channel bitmap */
363
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 43 times.
57 if (tag == MOV_CH_LAYOUT_USE_BITMAP)
364
1/2
✓ Branch 0 taken 14 times.
✗ Branch 1 not taken.
14 return bitmap < 0x40000 ? bitmap : 0;
365
366 /* get the layout map based on the channel count for the specified layout tag */
367 43 channels = tag & 0xFFFF;
368
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 43 times.
43 if (channels > 9)
369 channels = 0;
370 43 layout_map = mov_ch_layout_map[channels];
371
372 /* find the channel layout for the specified layout tag */
373
1/2
✓ Branch 0 taken 48 times.
✗ Branch 1 not taken.
48 for (i = 0; layout_map[i].tag != 0; i++) {
374
2/2
✓ Branch 0 taken 43 times.
✓ Branch 1 taken 5 times.
48 if (layout_map[i].tag == tag)
375 43 break;
376 }
377 43 return layout_map[i].layout;
378 }
379
380 3 static uint64_t mov_get_channel_mask(uint32_t label)
381 {
382
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (label == 0)
383 return 0;
384
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
3 if (label <= 18)
385 1 return 1U << (label - 1);
386
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (label == 35)
387 return AV_CH_WIDE_LEFT;
388
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (label == 36)
389 return AV_CH_WIDE_RIGHT;
390
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (label == 37)
391 return AV_CH_LOW_FREQUENCY_2;
392
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (label == 38)
393 1 return AV_CH_STEREO_LEFT;
394
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (label == 39)
395 1 return AV_CH_STEREO_RIGHT;
396 return 0;
397 }
398
399 2 static uint32_t mov_get_channel_label(enum AVChannel channel)
400 {
401
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel < 0)
402 return 0;
403
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel <= AV_CHAN_TOP_BACK_RIGHT)
404 return channel + 1;
405
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel == AV_CHAN_WIDE_LEFT)
406 return 35;
407
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel == AV_CHAN_WIDE_RIGHT)
408 return 36;
409
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel == AV_CHAN_LOW_FREQUENCY_2)
410 return 37;
411
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (channel == AV_CHAN_STEREO_LEFT)
412 1 return 38;
413
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 if (channel == AV_CHAN_STEREO_RIGHT)
414 1 return 39;
415 return 0;
416 }
417
418 37 int ff_mov_get_channel_layout_tag(const AVCodecParameters *par,
419 uint32_t *layout,
420 uint32_t *bitmap,
421 uint32_t **pchannel_desc)
422 {
423 int i, j;
424 37 uint32_t tag = 0;
425 37 const enum MovChannelLayoutTag *layouts = NULL;
426
427 /* find the layout list for the specified codec */
428
2/2
✓ Branch 0 taken 283 times.
✓ Branch 1 taken 11 times.
294 for (i = 0; mov_codec_ch_layouts[i].codec_id != AV_CODEC_ID_NONE; i++) {
429
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 257 times.
283 if (mov_codec_ch_layouts[i].codec_id == par->codec_id)
430 26 break;
431 }
432
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 11 times.
37 if (mov_codec_ch_layouts[i].codec_id != AV_CODEC_ID_NONE)
433 26 layouts = mov_codec_ch_layouts[i].layouts;
434
435
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 11 times.
37 if (layouts) {
436 int channels;
437 const struct MovChannelLayoutMap *layout_map;
438
439 /* get the layout map based on the channel count */
440 26 channels = par->ch_layout.nb_channels;
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26 times.
26 if (channels > 9)
442 channels = 0;
443 26 layout_map = mov_ch_layout_map[channels];
444
445 /* find the layout tag for the specified channel layout */
446
2/2
✓ Branch 0 taken 111 times.
✓ Branch 1 taken 7 times.
118 for (i = 0; layouts[i] != 0; i++) {
447
2/2
✓ Branch 0 taken 85 times.
✓ Branch 1 taken 26 times.
111 if ((layouts[i] & 0xFFFF) != channels)
448 85 continue;
449
2/2
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 7 times.
33 for (j = 0; layout_map[j].tag != 0; j++) {
450
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 if (layout_map[j].tag == layouts[i] &&
451
1/2
✓ Branch 0 taken 26 times.
✗ Branch 1 not taken.
26 (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
452
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7 times.
26 layout_map[j].layout == par->ch_layout.u.mask))
453 19 break;
454 }
455
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 7 times.
26 if (layout_map[j].tag)
456 19 break;
457 }
458 26 tag = layouts[i];
459 }
460
461 37 *layout = tag;
462 37 *bitmap = 0;
463 37 *pchannel_desc = NULL;
464
465 /* if no tag was found, use channel bitmap or description as a backup if possible */
466
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 19 times.
37 if (tag == 0) {
467 uint32_t *channel_desc;
468
1/2
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
18 if (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
469
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
18 par->ch_layout.u.mask < 0x40000) {
470 16 *layout = MOV_CH_LAYOUT_USE_BITMAP;
471 16 *bitmap = (uint32_t)par->ch_layout.u.mask;
472 16 return 0;
473
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (par->ch_layout.order == AV_CHANNEL_ORDER_UNSPEC)
474 return AVERROR(ENOSYS);
475
476 2 channel_desc = av_malloc_array(par->ch_layout.nb_channels, sizeof(*channel_desc));
477
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (!channel_desc)
478 return AVERROR(ENOMEM);
479
480
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 for (i = 0; i < par->ch_layout.nb_channels; i++) {
481 4 channel_desc[i] =
482 2 mov_get_channel_label(av_channel_layout_channel_from_index(&par->ch_layout, i));
483
484
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (channel_desc[i] == 0) {
485 av_free(channel_desc);
486 return AVERROR(ENOSYS);
487 }
488 }
489
490 2 *pchannel_desc = channel_desc;
491 }
492
493 21 return 0;
494 }
495
496 60 int ff_mov_read_chan(AVFormatContext *s, AVIOContext *pb, AVStream *st,
497 int64_t size)
498 {
499 uint32_t layout_tag, bitmap, num_descr;
500 60 uint64_t label_mask, mask = 0;
501 int i;
502
503
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (size < 12)
504 return AVERROR_INVALIDDATA;
505
506 60 layout_tag = avio_rb32(pb);
507 60 bitmap = avio_rb32(pb);
508 60 num_descr = avio_rb32(pb);
509
510 60 av_log(s, AV_LOG_DEBUG, "chan: layout=%"PRIu32" "
511 "bitmap=%"PRIu32" num_descr=%"PRIu32"\n",
512 layout_tag, bitmap, num_descr);
513
514
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60 times.
60 if (size < 12ULL + num_descr * 20ULL)
515 return 0;
516
517 60 label_mask = 0;
518
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 60 times.
63 for (i = 0; i < num_descr; i++) {
519 uint32_t label;
520
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (pb->eof_reached) {
521 av_log(s, AV_LOG_ERROR,
522 "reached EOF while reading channel layout\n");
523 return AVERROR_INVALIDDATA;
524 }
525 3 label = avio_rb32(pb); // mChannelLabel
526 3 avio_rb32(pb); // mChannelFlags
527 3 avio_rl32(pb); // mCoordinates[0]
528 3 avio_rl32(pb); // mCoordinates[1]
529 3 avio_rl32(pb); // mCoordinates[2]
530 3 size -= 20;
531
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (layout_tag == 0) {
532 3 uint64_t mask_incr = mov_get_channel_mask(label);
533
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 if (mask_incr == 0) {
534 label_mask = 0;
535 break;
536 }
537 3 label_mask |= mask_incr;
538 }
539 }
540
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 57 times.
60 if (layout_tag == 0) {
541
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (label_mask)
542 3 mask = label_mask;
543 } else
544 57 mask = mov_get_channel_layout(layout_tag, bitmap);
545
546
1/2
✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
60 if (mask) {
547 60 av_channel_layout_uninit(&st->codecpar->ch_layout);
548 60 av_channel_layout_from_mask(&st->codecpar->ch_layout, mask);
549 }
550 60 avio_skip(pb, size - 12);
551
552 60 return 0;
553 }
554