avutil/channel_layout: print known layout names in custom layout

If a custom layout is equivalent to a native one, check if it matches one of the
known layout names and print that instead.

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2024-02-13 14:30:15 -03:00
parent e06ce6d2b4
commit b5911654c4
2 changed files with 44 additions and 28 deletions

View File

@ -679,6 +679,29 @@ int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
return 0; return 0;
} }
static int64_t masked_description(const AVChannelLayout *channel_layout, int start_channel)
{
uint64_t mask = 0;
for (int i = start_channel; i < channel_layout->nb_channels; i++) {
enum AVChannel ch = channel_layout->u.map[i].id;
if (ch >= 0 && ch < 63 && mask < (1ULL << ch))
mask |= (1ULL << ch);
else
return AVERROR(EINVAL);
}
return mask;
}
static int has_channel_names(const AVChannelLayout *channel_layout)
{
if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
return 0;
for (int i = 0; i < channel_layout->nb_channels; i++)
if (channel_layout->u.map[i].name[0])
return 1;
return 0;
}
/** /**
* If the layout is n-th order standard-order ambisonic, with optional * If the layout is n-th order standard-order ambisonic, with optional
* extra non-diegetic channels at the end, return the order. * extra non-diegetic channels at the end, return the order.
@ -745,11 +768,19 @@ static int try_describe_ambisonic(AVBPrint *bp, const AVChannelLayout *channel_l
extra.order = AV_CHANNEL_ORDER_NATIVE; extra.order = AV_CHANNEL_ORDER_NATIVE;
extra.nb_channels = av_popcount64(channel_layout->u.mask); extra.nb_channels = av_popcount64(channel_layout->u.mask);
extra.u.mask = channel_layout->u.mask; extra.u.mask = channel_layout->u.mask;
} else {
int64_t mask;
if (!has_channel_names(channel_layout) &&
(mask = masked_description(channel_layout, nb_ambi_channels)) > 0) {
extra.order = AV_CHANNEL_ORDER_NATIVE;
extra.nb_channels = av_popcount64(mask);
extra.u.mask = mask;
} else { } else {
extra.order = AV_CHANNEL_ORDER_CUSTOM; extra.order = AV_CHANNEL_ORDER_CUSTOM;
extra.nb_channels = channel_layout->nb_channels - nb_ambi_channels; extra.nb_channels = channel_layout->nb_channels - nb_ambi_channels;
extra.u.map = channel_layout->u.map + nb_ambi_channels; extra.u.map = channel_layout->u.map + nb_ambi_channels;
} }
}
av_bprint_chars(bp, '+', 1); av_bprint_chars(bp, '+', 1);
av_channel_layout_describe_bprint(&extra, bp); av_channel_layout_describe_bprint(&extra, bp);
@ -774,9 +805,17 @@ int av_channel_layout_describe_bprint(const AVChannelLayout *channel_layout,
// fall-through // fall-through
case AV_CHANNEL_ORDER_CUSTOM: case AV_CHANNEL_ORDER_CUSTOM:
if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) { if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
int64_t mask;
int res = try_describe_ambisonic(bp, channel_layout); int res = try_describe_ambisonic(bp, channel_layout);
if (res >= 0) if (res >= 0)
return 0; return 0;
if (!has_channel_names(channel_layout) &&
(mask = masked_description(channel_layout, 0)) > 0) {
AVChannelLayout native = { .order = AV_CHANNEL_ORDER_NATIVE,
.nb_channels = av_popcount64(mask),
.u.mask = mask };
return av_channel_layout_describe_bprint(&native, bp);
}
} }
if (channel_layout->nb_channels) if (channel_layout->nb_channels)
av_bprintf(bp, "%d channels (", channel_layout->nb_channels); av_bprintf(bp, "%d channels (", channel_layout->nb_channels);
@ -1037,29 +1076,6 @@ uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout,
return ret; return ret;
} }
static int64_t masked_description(AVChannelLayout *channel_layout, int start_channel)
{
uint64_t mask = 0;
for (int i = start_channel; i < channel_layout->nb_channels; i++) {
enum AVChannel ch = channel_layout->u.map[i].id;
if (ch >= 0 && ch < 63 && mask < (1ULL << ch))
mask |= (1ULL << ch);
else
return AVERROR(EINVAL);
}
return mask;
}
static int has_channel_names(AVChannelLayout *channel_layout)
{
if (channel_layout->order != AV_CHANNEL_ORDER_CUSTOM)
return 0;
for (int i = 0; i < channel_layout->nb_channels; i++)
if (channel_layout->u.map[i].name[0])
return 1;
return 0;
}
int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags) int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrder order, int flags)
{ {
int allow_lossy = !(flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS); int allow_lossy = !(flags & AV_CHANNEL_LAYOUT_RETYPE_FLAG_LOSSLESS);

View File

@ -195,7 +195,7 @@ With "FL@Boo": CUSTOM (1 channels (FL@Boo))
With "stereo": NATIVE (stereo) With "stereo": NATIVE (stereo)
~~ UNSPEC (2 channels) ~~ UNSPEC (2 channels)
== NATIVE (stereo) == NATIVE (stereo)
== CUSTOM (2 channels (FL+FR)) == CUSTOM (stereo)
!= AMBI != AMBI
With "FR+FL": CUSTOM (2 channels (FR+FL)) With "FR+FL": CUSTOM (2 channels (FR+FL))
~~ UNSPEC (2 channels) ~~ UNSPEC (2 channels)
@ -205,7 +205,7 @@ With "FR+FL": CUSTOM (2 channels (FR+FL))
With "ambisonic 2+stereo": AMBI (ambisonic 2+stereo) With "ambisonic 2+stereo": AMBI (ambisonic 2+stereo)
~~ UNSPEC (11 channels) ~~ UNSPEC (11 channels)
!= NATIVE != NATIVE
== CUSTOM (ambisonic 2+2 channels (FL+FR)) == CUSTOM (ambisonic 2+stereo)
== AMBI (ambisonic 2+stereo) == AMBI (ambisonic 2+stereo)
With "2C": UNSPEC (2 channels) With "2C": UNSPEC (2 channels)
== UNSPEC (2 channels) == UNSPEC (2 channels)