2013-04-05 13:25:09 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* mpv is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2015-05-21 18:37:17 +00:00
|
|
|
#include <libavutil/common.h>
|
|
|
|
|
2014-11-24 18:56:01 +00:00
|
|
|
#include "common/common.h"
|
2013-12-17 01:39:45 +00:00
|
|
|
#include "common/msg.h"
|
2013-04-05 13:25:09 +00:00
|
|
|
#include "chmap.h"
|
|
|
|
|
|
|
|
// Names taken from libavutil/channel_layout.c (Not accessible by API.)
|
|
|
|
// Use of these names is hard-coded in some places (e.g. ao_alsa.c)
|
2014-06-10 21:56:05 +00:00
|
|
|
static const char *const speaker_names[MP_SPEAKER_ID_COUNT][2] = {
|
2013-04-05 13:25:09 +00:00
|
|
|
[MP_SPEAKER_ID_FL] = {"fl", "front left"},
|
|
|
|
[MP_SPEAKER_ID_FR] = {"fr", "front right"},
|
|
|
|
[MP_SPEAKER_ID_FC] = {"fc", "front center"},
|
|
|
|
[MP_SPEAKER_ID_LFE] = {"lfe", "low frequency"},
|
|
|
|
[MP_SPEAKER_ID_BL] = {"bl", "back left"},
|
|
|
|
[MP_SPEAKER_ID_BR] = {"br", "back right"},
|
|
|
|
[MP_SPEAKER_ID_FLC] = {"flc", "front left-of-center"},
|
|
|
|
[MP_SPEAKER_ID_FRC] = {"frc", "front right-of-center"},
|
|
|
|
[MP_SPEAKER_ID_BC] = {"bc", "back center"},
|
|
|
|
[MP_SPEAKER_ID_SL] = {"sl", "side left"},
|
|
|
|
[MP_SPEAKER_ID_SR] = {"sr", "side right"},
|
|
|
|
[MP_SPEAKER_ID_TC] = {"tc", "top center"},
|
|
|
|
[MP_SPEAKER_ID_TFL] = {"tfl", "top front left"},
|
|
|
|
[MP_SPEAKER_ID_TFC] = {"tfc", "top front center"},
|
|
|
|
[MP_SPEAKER_ID_TFR] = {"tfr", "top front right"},
|
|
|
|
[MP_SPEAKER_ID_TBL] = {"tbl", "top back left"},
|
|
|
|
[MP_SPEAKER_ID_TBC] = {"tbc", "top back center"},
|
|
|
|
[MP_SPEAKER_ID_TBR] = {"tbr", "top back right"},
|
|
|
|
[MP_SPEAKER_ID_DL] = {"dl", "downmix left"},
|
|
|
|
[MP_SPEAKER_ID_DR] = {"dr", "downmix right"},
|
|
|
|
[MP_SPEAKER_ID_WL] = {"wl", "wide left"},
|
|
|
|
[MP_SPEAKER_ID_WR] = {"wr", "wide right"},
|
|
|
|
[MP_SPEAKER_ID_SDL] = {"sdl", "surround direct left"},
|
|
|
|
[MP_SPEAKER_ID_SDR] = {"sdr", "surround direct right"},
|
|
|
|
[MP_SPEAKER_ID_LFE2] = {"lfe2", "low frequency 2"},
|
2015-05-07 21:07:14 +00:00
|
|
|
[MP_SPEAKER_ID_NA] = {"na", "not available"},
|
2013-04-05 13:25:09 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Names taken from libavutil/channel_layout.c (Not accessible by API.)
|
|
|
|
// Channel order corresponds to lavc/waveex, except for the alsa entries.
|
2014-06-10 21:56:05 +00:00
|
|
|
static const char *const std_layout_names[][2] = {
|
2013-04-05 13:25:09 +00:00
|
|
|
{"empty", ""}, // not in lavc
|
|
|
|
{"mono", "fc"},
|
2015-02-26 14:41:45 +00:00
|
|
|
{"1.0", "fc"}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{"stereo", "fl-fr"},
|
2015-02-26 14:41:45 +00:00
|
|
|
{"2.0", "fl-fr"}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{"2.1", "fl-fr-lfe"},
|
|
|
|
{"3.0", "fl-fr-fc"},
|
|
|
|
{"3.0(back)", "fl-fr-bc"},
|
|
|
|
{"4.0", "fl-fr-fc-bc"},
|
|
|
|
{"quad", "fl-fr-bl-br"},
|
|
|
|
{"quad(side)", "fl-fr-sl-sr"},
|
|
|
|
{"3.1", "fl-fr-fc-lfe"},
|
2015-06-12 21:35:17 +00:00
|
|
|
{"3.1(back)", "fl-fr-lfe-bc"}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{"5.0", "fl-fr-fc-bl-br"},
|
|
|
|
{"5.0(alsa)", "fl-fr-bl-br-fc"}, // not in lavc
|
|
|
|
{"5.0(side)", "fl-fr-fc-sl-sr"},
|
|
|
|
{"4.1", "fl-fr-fc-lfe-bc"},
|
|
|
|
{"4.1(alsa)", "fl-fr-bl-br-lfe"}, // not in lavc
|
|
|
|
{"5.1", "fl-fr-fc-lfe-bl-br"},
|
|
|
|
{"5.1(alsa)", "fl-fr-bl-br-fc-lfe"}, // not in lavc
|
|
|
|
{"5.1(side)", "fl-fr-fc-lfe-sl-sr"},
|
|
|
|
{"6.0", "fl-fr-fc-bc-sl-sr"},
|
|
|
|
{"6.0(front)", "fl-fr-flc-frc-sl-sr"},
|
|
|
|
{"hexagonal", "fl-fr-fc-bl-br-bc"},
|
2013-05-13 21:54:27 +00:00
|
|
|
{"6.1", "fl-fr-fc-lfe-bc-sl-sr"},
|
|
|
|
{"6.1(back)", "fl-fr-fc-lfe-bl-br-bc"}, // lavc calls this "6.1" too
|
2015-06-12 21:35:17 +00:00
|
|
|
{"6.1(top)", "fl-fr-fc-lfe-bl-br-tc"}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{"6.1(front)", "fl-fr-lfe-flc-frc-sl-sr"},
|
|
|
|
{"7.0", "fl-fr-fc-bl-br-sl-sr"},
|
|
|
|
{"7.0(front)", "fl-fr-fc-flc-frc-sl-sr"},
|
2015-06-12 21:35:17 +00:00
|
|
|
{"7.0(rear)", "fl-fr-fc-bl-br-sdl-sdr"}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{"7.1", "fl-fr-fc-lfe-bl-br-sl-sr"},
|
|
|
|
{"7.1(alsa)", "fl-fr-bl-br-fc-lfe-sl-sr"}, // not in lavc
|
|
|
|
{"7.1(wide)", "fl-fr-fc-lfe-bl-br-flc-frc"},
|
|
|
|
{"7.1(wide-side)", "fl-fr-fc-lfe-flc-frc-sl-sr"},
|
af_lavrresample: don't drop sl/sr channels for 7.1 on ALSA
ao_alsa: attempt to fix 7.1 over HDMI
The last 2 channels of 7.1 (RLC/RRC in ALSA) were exported as sdl/sdr
instead of sl/sr (I don't even know why I chose sdl/sdr, but SL/SR
and RLC/RRC are different in the ALSA API). libsw/avresample do not
move the sl/sr channels to sdl/sdr when rematrixing, so silence was
sent for 2 channels. If my selection of sdl/sdr is essentially API
abuse, there's no reason why they should do this differently.
The mess here is really that ALSa doesn't map the HDMI layouts cleanly.
Most ALSA drivers export 7.1 in a way compatible to our expectations,
but Intel HDA/HDMI does not:
mpv/ffmpeg: fl-fr-fc-lfe-bl-br-sl-sr
ALSA/generic: FL FR FC LFE RL RR SL SR [1]
ALSA/HDMI: FL FR LFE FC RL RR RLC RRC [2]
The HDMI layout is layout 0x13 (going by CEA-861-B). The comment in
the kernel code has to be correct too. The early standard defines only
1 other layout, which replaces RLC/RRC with FRC/FLC - this probably
corresponds to what we call "7.1(wide)".
So it appears when ALSA requests RLC/RRC, we should feed it sl/sr.
To make it more complicated, Kodi/xbmc apparently also have to deal with
ALSA being special, but instead of sending sl/sr to RLC/RRC, they swap
the last two pairs of the layout, and send sl/sr to RL/RR and bl/br to
RLC/RRC. Or I might have misunderstood their code. I don't have a
7.1-capable A/V receiver, so I can't test this.
For now, go with the simpler solution, and wait until someone tests it.
If the speakers end up swapped, a completely different solution will be
needed.
[1] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/sound/core/pcm_lib.c?id=refs/tags/v4.3#n2434
[2] https://git.kernel.org/cgit/linux/kernel/git/torvalds/linux.git/tree/sound/pci/hda/patch_hdmi.c?id=refs/tags/v4.3#n307
2015-11-02 23:25:20 +00:00
|
|
|
{"7.1(rear)", "fl-fr-fc-lfe-bl-br-sdl-sdr"}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{"octagonal", "fl-fr-fc-bl-br-bc-sl-sr"},
|
|
|
|
{"downmix", "dl-dr"},
|
2014-10-30 21:58:17 +00:00
|
|
|
{"auto", ""}, // not in lavc
|
2013-04-05 13:25:09 +00:00
|
|
|
{0}
|
|
|
|
};
|
|
|
|
|
2015-06-12 15:45:56 +00:00
|
|
|
static const struct mp_chmap default_layouts[] = {
|
2013-04-05 13:25:09 +00:00
|
|
|
{0}, // empty
|
|
|
|
MP_CHMAP_INIT_MONO, // mono
|
|
|
|
MP_CHMAP2(FL, FR), // stereo
|
|
|
|
MP_CHMAP3(FL, FR, LFE), // 2.1
|
|
|
|
MP_CHMAP4(FL, FR, FC, BC), // 4.0
|
|
|
|
MP_CHMAP5(FL, FR, FC, BL, BR), // 5.0
|
|
|
|
MP_CHMAP6(FL, FR, FC, LFE, BL, BR), // 5.1
|
2013-05-13 21:54:27 +00:00
|
|
|
MP_CHMAP7(FL, FR, FC, LFE, BC, SL, SR), // 6.1
|
2013-04-05 13:25:09 +00:00
|
|
|
MP_CHMAP8(FL, FR, FC, LFE, BL, BR, SL, SR), // 7.1
|
|
|
|
};
|
|
|
|
|
|
|
|
// Returns true if speakers are mapped uniquely, and there's at least 1 channel.
|
|
|
|
bool mp_chmap_is_valid(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
bool mapped[MP_SPEAKER_ID_COUNT] = {0};
|
|
|
|
for (int n = 0; n < src->num; n++) {
|
|
|
|
int sp = src->speaker[n];
|
|
|
|
if (sp >= MP_SPEAKER_ID_COUNT || mapped[sp])
|
|
|
|
return false;
|
2015-05-07 21:07:14 +00:00
|
|
|
if (sp != MP_SPEAKER_ID_NA)
|
|
|
|
mapped[sp] = true;
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
return src->num > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_chmap_is_empty(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
return src->num == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if the channel map defines the number of the channels only, and
|
|
|
|
// the channels have to meaning associated with them.
|
|
|
|
bool mp_chmap_is_unknown(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < src->num; n++) {
|
2015-05-07 21:15:52 +00:00
|
|
|
if (src->speaker[n] != MP_SPEAKER_ID_NA)
|
|
|
|
return false;
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
2015-05-07 21:15:52 +00:00
|
|
|
return mp_chmap_is_valid(src);
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Note: empty channel maps compare as equal. Invalid ones can equal too.
|
|
|
|
bool mp_chmap_equals(const struct mp_chmap *a, const struct mp_chmap *b)
|
|
|
|
{
|
|
|
|
if (a->num != b->num)
|
|
|
|
return false;
|
|
|
|
for (int n = 0; n < a->num; n++) {
|
|
|
|
if (a->speaker[n] != b->speaker[n])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Whether they use the same speakers (even if in different order).
|
|
|
|
bool mp_chmap_equals_reordered(const struct mp_chmap *a, const struct mp_chmap *b)
|
|
|
|
{
|
|
|
|
struct mp_chmap t1 = *a, t2 = *b;
|
|
|
|
mp_chmap_reorder_norm(&t1);
|
|
|
|
mp_chmap_reorder_norm(&t2);
|
|
|
|
return mp_chmap_equals(&t1, &t2);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_chmap_is_stereo(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
static const struct mp_chmap stereo = MP_CHMAP_INIT_STEREO;
|
|
|
|
return mp_chmap_equals(src, &stereo);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int comp_uint8(const void *a, const void *b)
|
|
|
|
{
|
|
|
|
return *(const uint8_t *)a - *(const uint8_t *)b;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reorder channels to normal order, with monotonically increasing speaker IDs.
|
|
|
|
// We define this order as the same order used with waveex.
|
|
|
|
void mp_chmap_reorder_norm(struct mp_chmap *map)
|
|
|
|
{
|
|
|
|
uint8_t *arr = &map->speaker[0];
|
|
|
|
qsort(arr, map->num, 1, comp_uint8);
|
|
|
|
}
|
|
|
|
|
2015-05-04 21:59:20 +00:00
|
|
|
// Remove silent (NA) channels, if any.
|
|
|
|
void mp_chmap_remove_na(struct mp_chmap *map)
|
|
|
|
{
|
|
|
|
struct mp_chmap new = {0};
|
|
|
|
for (int n = 0; n < map->num; n++) {
|
|
|
|
int sp = map->speaker[n];
|
2015-05-07 21:07:14 +00:00
|
|
|
if (sp != MP_SPEAKER_ID_NA)
|
2015-05-04 21:59:20 +00:00
|
|
|
new.speaker[new.num++] = map->speaker[n];
|
|
|
|
}
|
|
|
|
*map = new;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add silent (NA) channels to map until map->num >= num.
|
|
|
|
void mp_chmap_fill_na(struct mp_chmap *map, int num)
|
|
|
|
{
|
|
|
|
assert(num <= MP_NUM_CHANNELS);
|
2015-05-07 21:07:14 +00:00
|
|
|
while (map->num < num)
|
|
|
|
map->speaker[map->num++] = MP_SPEAKER_ID_NA;
|
2015-05-04 21:59:20 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 13:25:09 +00:00
|
|
|
// Set *dst to a standard layout with the given number of channels.
|
|
|
|
// If the number of channels is invalid, an invalid map is set, and
|
|
|
|
// mp_chmap_is_valid(dst) will return false.
|
|
|
|
void mp_chmap_from_channels(struct mp_chmap *dst, int num_channels)
|
|
|
|
{
|
2015-06-12 15:45:56 +00:00
|
|
|
*dst = (struct mp_chmap) {0};
|
|
|
|
if (num_channels >= 0 && num_channels < MP_ARRAY_SIZE(default_layouts))
|
2013-04-05 13:25:09 +00:00
|
|
|
*dst = default_layouts[num_channels];
|
2015-06-12 15:45:56 +00:00
|
|
|
if (!dst->num)
|
|
|
|
mp_chmap_set_unknown(dst, num_channels);
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set *dst to an unknown layout for the given numbers of channels.
|
|
|
|
// If the number of channels is invalid, an invalid map is set, and
|
|
|
|
// mp_chmap_is_valid(dst) will return false.
|
2015-05-07 21:15:52 +00:00
|
|
|
// A mp_chmap with all entries set to NA is treated specially in some
|
|
|
|
// contexts (watch out for mp_chmap_is_unknown()).
|
2013-04-05 13:25:09 +00:00
|
|
|
void mp_chmap_set_unknown(struct mp_chmap *dst, int num_channels)
|
|
|
|
{
|
|
|
|
if (num_channels < 0 || num_channels > MP_NUM_CHANNELS) {
|
|
|
|
*dst = (struct mp_chmap) {0};
|
|
|
|
} else {
|
|
|
|
dst->num = num_channels;
|
|
|
|
for (int n = 0; n < dst->num; n++)
|
2015-05-07 21:15:52 +00:00
|
|
|
dst->speaker[n] = MP_SPEAKER_ID_NA;
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return channel index of the given speaker, or -1.
|
|
|
|
static int mp_chmap_find_speaker(const struct mp_chmap *map, int speaker)
|
|
|
|
{
|
|
|
|
for (int n = 0; n < map->num; n++) {
|
|
|
|
if (map->speaker[n] == speaker)
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void mp_chmap_remove_speaker(struct mp_chmap *map, int speaker)
|
|
|
|
{
|
|
|
|
int index = mp_chmap_find_speaker(map, speaker);
|
|
|
|
if (index >= 0) {
|
|
|
|
for (int n = index; n < map->num - 1; n++)
|
|
|
|
map->speaker[n] = map->speaker[n + 1];
|
|
|
|
map->num--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Some decoders output additional, redundant channels, which are usually
|
|
|
|
// useless and will mess up proper audio output channel handling.
|
|
|
|
// map: channel map from which the channels should be removed
|
|
|
|
// requested: if not NULL, and if it contains any of the "useless" channels,
|
|
|
|
// don't remove them (this is for convenience)
|
|
|
|
void mp_chmap_remove_useless_channels(struct mp_chmap *map,
|
|
|
|
const struct mp_chmap *requested)
|
|
|
|
{
|
|
|
|
if (requested &&
|
|
|
|
mp_chmap_find_speaker(requested, MP_SPEAKER_ID_DL) >= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (map->num > 2) {
|
|
|
|
mp_chmap_remove_speaker(map, MP_SPEAKER_ID_DL);
|
|
|
|
mp_chmap_remove_speaker(map, MP_SPEAKER_ID_DR);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the ffmpeg/libav channel layout as in <libavutil/channel_layout.h>.
|
2015-05-04 21:56:27 +00:00
|
|
|
// Speakers not representable by ffmpeg/libav are dropped.
|
2013-04-05 13:25:09 +00:00
|
|
|
// Warning: this ignores the order of the channels, and will return a channel
|
|
|
|
// mask even if the order is different from libavcodec's.
|
2015-10-26 14:52:21 +00:00
|
|
|
// Also, "unknown" channel maps are translated to non-sense channel
|
|
|
|
// maps with the same number of channels.
|
2013-04-05 13:25:09 +00:00
|
|
|
uint64_t mp_chmap_to_lavc_unchecked(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
struct mp_chmap t = *src;
|
2015-10-26 14:52:21 +00:00
|
|
|
if (t.num > 64)
|
|
|
|
return 0;
|
|
|
|
// lavc has no concept for unknown layouts yet, so pick something that does
|
|
|
|
// the job of signaling the number of channels, even if it makes no sense
|
|
|
|
// as a proper layout.
|
2013-04-05 13:25:09 +00:00
|
|
|
if (mp_chmap_is_unknown(&t))
|
2015-10-26 14:52:21 +00:00
|
|
|
return t.num == 64 ? (uint64_t)-1 : (1ULL << t.num) - 1;
|
2013-04-05 13:25:09 +00:00
|
|
|
uint64_t mask = 0;
|
2015-05-04 21:56:27 +00:00
|
|
|
for (int n = 0; n < t.num; n++) {
|
2015-05-07 21:07:14 +00:00
|
|
|
if (t.speaker[n] < 64) // ignore MP_SPEAKER_ID_NA etc.
|
2015-05-04 21:56:27 +00:00
|
|
|
mask |= 1ULL << t.speaker[n];
|
|
|
|
}
|
2013-04-05 13:25:09 +00:00
|
|
|
return mask;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the ffmpeg/libav channel layout as in <libavutil/channel_layout.h>.
|
|
|
|
// Returns 0 if the channel order doesn't match lavc's or if it's invalid.
|
|
|
|
uint64_t mp_chmap_to_lavc(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
if (!mp_chmap_is_lavc(src))
|
|
|
|
return 0;
|
|
|
|
return mp_chmap_to_lavc_unchecked(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set channel map from the ffmpeg/libav channel layout as in
|
|
|
|
// <libavutil/channel_layout.h>.
|
|
|
|
// If the number of channels exceed MP_NUM_CHANNELS, set dst to empty.
|
|
|
|
void mp_chmap_from_lavc(struct mp_chmap *dst, uint64_t src)
|
|
|
|
{
|
|
|
|
dst->num = 0;
|
|
|
|
for (int n = 0; n < 64; n++) {
|
|
|
|
if (src & (1ULL << n)) {
|
|
|
|
if (dst->num >= MP_NUM_CHANNELS) {
|
|
|
|
dst->num = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dst->speaker[dst->num] = n;
|
|
|
|
dst->num++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool mp_chmap_is_lavc(const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
if (!mp_chmap_is_valid(src))
|
|
|
|
return false;
|
|
|
|
if (mp_chmap_is_unknown(src))
|
|
|
|
return true;
|
|
|
|
// lavc's channel layout is a bit mask, and channels are always ordered
|
|
|
|
// from LSB to MSB speaker bits, so speaker IDs have to increase.
|
|
|
|
assert(src->num > 0);
|
|
|
|
for (int n = 1; n < src->num; n++) {
|
|
|
|
if (src->speaker[n - 1] >= src->speaker[n])
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (int n = 0; n < src->num; n++) {
|
|
|
|
if (src->speaker[n] >= 64)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-10-26 14:52:21 +00:00
|
|
|
// Warning: for "unknown" channel maps, this returns something that may not
|
|
|
|
// make sense. Invalid channel maps are not changed.
|
2013-04-05 13:25:09 +00:00
|
|
|
void mp_chmap_reorder_to_lavc(struct mp_chmap *map)
|
|
|
|
{
|
|
|
|
if (!mp_chmap_is_valid(map))
|
|
|
|
return;
|
|
|
|
uint64_t mask = mp_chmap_to_lavc_unchecked(map);
|
|
|
|
mp_chmap_from_lavc(map, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get reordering array for from->to reordering. from->to must have the same set
|
|
|
|
// of speakers (i.e. same number and speaker IDs, just different order). Then,
|
2015-05-04 21:59:20 +00:00
|
|
|
// for each speaker n, src[n] will be set such that:
|
|
|
|
// to->speaker[n] = from->speaker[src[n]]
|
|
|
|
// (src[n] gives the source channel for destination channel n)
|
|
|
|
// If *from and *to don't contain the same set of speakers, then the above
|
|
|
|
// invariant is not guaranteed. Instead, src[n] can be set to -1 if the channel
|
|
|
|
// at to->speaker[n] is unmapped.
|
|
|
|
void mp_chmap_get_reorder(int src[MP_NUM_CHANNELS], const struct mp_chmap *from,
|
2013-04-05 13:25:09 +00:00
|
|
|
const struct mp_chmap *to)
|
|
|
|
{
|
2015-05-04 21:59:20 +00:00
|
|
|
for (int n = 0; n < MP_NUM_CHANNELS; n++)
|
|
|
|
src[n] = -1;
|
|
|
|
|
2013-04-05 13:25:09 +00:00
|
|
|
if (mp_chmap_is_unknown(from) || mp_chmap_is_unknown(to)) {
|
2015-05-04 21:59:20 +00:00
|
|
|
for (int n = 0; n < to->num; n++)
|
|
|
|
src[n] = n < from->num ? n : -1;
|
2013-04-05 13:25:09 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-05-04 21:59:20 +00:00
|
|
|
|
2015-06-12 15:45:47 +00:00
|
|
|
for (int n = 0; n < to->num; n++) {
|
|
|
|
for (int i = 0; i < from->num; i++) {
|
2015-05-04 21:59:20 +00:00
|
|
|
if (to->speaker[n] == from->speaker[i]) {
|
|
|
|
src[n] = i;
|
2013-04-05 13:25:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-04 21:59:20 +00:00
|
|
|
|
|
|
|
for (int n = 0; n < to->num; n++)
|
2015-05-07 21:26:33 +00:00
|
|
|
assert(src[n] < 0 || (to->speaker[n] == from->speaker[src[n]]));
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
2015-05-08 17:31:54 +00:00
|
|
|
// Return the number of channels only in a.
|
|
|
|
int mp_chmap_diffn(const struct mp_chmap *a, const struct mp_chmap *b)
|
|
|
|
{
|
2015-05-08 19:22:39 +00:00
|
|
|
uint64_t a_mask = mp_chmap_to_lavc_unchecked(a);
|
|
|
|
uint64_t b_mask = mp_chmap_to_lavc_unchecked(b);
|
2015-05-21 18:37:17 +00:00
|
|
|
return av_popcount64((a_mask ^ b_mask) & a_mask);
|
2015-05-08 17:31:54 +00:00
|
|
|
}
|
|
|
|
|
2013-04-05 13:25:09 +00:00
|
|
|
// Returns something like "fl-fr-fc". If there's a standard layout in lavc
|
|
|
|
// order, return that, e.g. "3.0" instead of "fl-fr-fc".
|
|
|
|
// Unassigned but valid speakers get names like "sp28".
|
2014-11-24 18:56:01 +00:00
|
|
|
char *mp_chmap_to_str_buf(char *buf, size_t buf_size, const struct mp_chmap *src)
|
2013-04-05 13:25:09 +00:00
|
|
|
{
|
2014-11-24 18:56:01 +00:00
|
|
|
buf[0] = '\0';
|
2013-04-05 13:25:09 +00:00
|
|
|
|
2014-11-24 18:56:01 +00:00
|
|
|
if (mp_chmap_is_unknown(src)) {
|
|
|
|
snprintf(buf, buf_size, "unknown%d", src->num);
|
|
|
|
return buf;
|
|
|
|
}
|
2013-04-05 13:25:09 +00:00
|
|
|
|
|
|
|
for (int n = 0; n < src->num; n++) {
|
|
|
|
int sp = src->speaker[n];
|
|
|
|
const char *s = sp < MP_SPEAKER_ID_COUNT ? speaker_names[sp][0] : NULL;
|
2014-11-24 18:56:01 +00:00
|
|
|
char sp_buf[10];
|
2013-04-05 13:25:09 +00:00
|
|
|
if (!s) {
|
2015-05-07 21:07:14 +00:00
|
|
|
snprintf(sp_buf, sizeof(sp_buf), "sp%d", sp);
|
2014-11-24 18:56:01 +00:00
|
|
|
s = sp_buf;
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
2014-11-24 18:56:01 +00:00
|
|
|
mp_snprintf_cat(buf, buf_size, "%s%s", n > 0 ? "-" : "", s);
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// To standard layout name
|
|
|
|
for (int n = 0; std_layout_names[n][0]; n++) {
|
2014-11-24 18:56:01 +00:00
|
|
|
if (strcmp(buf, std_layout_names[n][1]) == 0) {
|
|
|
|
snprintf(buf, buf_size, "%s", std_layout_names[n][0]);
|
2013-04-05 13:25:09 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-24 18:56:01 +00:00
|
|
|
return buf;
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If src can be parsed as channel map (as produced by mp_chmap_to_str()),
|
|
|
|
// return true and set *dst. Otherwise, return false and don't change *dst.
|
|
|
|
// Note: call mp_chmap_is_valid() to test whether the returned map is valid
|
|
|
|
// the map could be empty, or contain multiply mapped channels
|
|
|
|
bool mp_chmap_from_str(struct mp_chmap *dst, bstr src)
|
|
|
|
{
|
|
|
|
// Single number corresponds to mp_chmap_from_channels()
|
|
|
|
if (src.len > 0) {
|
|
|
|
bstr t = src;
|
|
|
|
bool unknown = bstr_eatstart0(&t, "unknown");
|
|
|
|
bstr rest;
|
|
|
|
long long count = bstrtoll(t, &rest, 10);
|
|
|
|
if (rest.len == 0) {
|
|
|
|
struct mp_chmap res;
|
|
|
|
if (unknown) {
|
|
|
|
mp_chmap_set_unknown(&res, count);
|
|
|
|
} else {
|
|
|
|
mp_chmap_from_channels(&res, count);
|
|
|
|
}
|
|
|
|
if (mp_chmap_is_valid(&res)) {
|
|
|
|
*dst = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// From standard layout name
|
|
|
|
for (int n = 0; std_layout_names[n][0]; n++) {
|
|
|
|
if (bstr_equals0(src, std_layout_names[n][0])) {
|
|
|
|
src = bstr0(std_layout_names[n][1]);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Explicit speaker list (separated by "-")
|
|
|
|
struct mp_chmap res = {0};
|
|
|
|
while (src.len) {
|
|
|
|
bstr s;
|
|
|
|
bstr_split_tok(src, "-", &s, &src);
|
|
|
|
int speaker = -1;
|
|
|
|
for (int n = 0; n < MP_SPEAKER_ID_COUNT; n++) {
|
|
|
|
const char *name = speaker_names[n][0];
|
|
|
|
if (name && bstr_equals0(s, name)) {
|
|
|
|
speaker = n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (speaker < 0) {
|
|
|
|
if (bstr_eatstart0(&s, "sp")) {
|
|
|
|
long long sp = bstrtoll(s, &s, 0);
|
|
|
|
if (s.len == 0 && sp >= 0 && sp < MP_SPEAKER_ID_COUNT)
|
|
|
|
speaker = sp;
|
|
|
|
}
|
|
|
|
if (speaker < 0)
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (res.num >= MP_NUM_CHANNELS)
|
|
|
|
return false;
|
|
|
|
res.speaker[res.num] = speaker;
|
|
|
|
res.num++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dst = res;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-06-25 17:10:24 +00:00
|
|
|
// Output a human readable "canonical" channel map string. Converting this from
|
|
|
|
// a string back to a channel map can yield a different map, but the string
|
|
|
|
// looks nicer. E.g. "fc-fl-fr-na" becomes "3.0".
|
|
|
|
char *mp_chmap_to_str_hr_buf(char *buf, size_t buf_size, const struct mp_chmap *src)
|
|
|
|
{
|
|
|
|
struct mp_chmap map = *src;
|
|
|
|
mp_chmap_remove_na(&map);
|
|
|
|
for (int n = 0; std_layout_names[n][0]; n++) {
|
|
|
|
struct mp_chmap s;
|
|
|
|
if (mp_chmap_from_str(&s, bstr0(std_layout_names[n][0])) &&
|
|
|
|
mp_chmap_equals_reordered(&s, &map))
|
|
|
|
{
|
|
|
|
map = s;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mp_chmap_to_str_buf(buf, buf_size, &map);
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:45:42 +00:00
|
|
|
void mp_chmap_print_help(struct mp_log *log)
|
2013-04-05 13:25:09 +00:00
|
|
|
{
|
2013-12-21 18:45:42 +00:00
|
|
|
mp_info(log, "Speakers:\n");
|
2013-04-05 13:25:09 +00:00
|
|
|
for (int n = 0; n < MP_SPEAKER_ID_COUNT; n++) {
|
|
|
|
if (speaker_names[n][0])
|
2013-12-21 18:45:42 +00:00
|
|
|
mp_info(log, " %-16s (%s)\n",
|
|
|
|
speaker_names[n][0], speaker_names[n][1]);
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
2013-12-21 18:45:42 +00:00
|
|
|
mp_info(log, "Standard layouts:\n");
|
2013-04-05 13:25:09 +00:00
|
|
|
for (int n = 0; std_layout_names[n][0]; n++) {
|
2013-12-21 18:45:42 +00:00
|
|
|
mp_info(log, " %-16s (%s)\n",
|
|
|
|
std_layout_names[n][0], std_layout_names[n][1]);
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|
|
|
|
for (int n = 0; n < MP_NUM_CHANNELS; n++)
|
2014-03-15 17:41:52 +00:00
|
|
|
mp_info(log, " unknown%d\n", n + 1);
|
2013-04-05 13:25:09 +00:00
|
|
|
}
|