avformat/mov: support SpatialAudioBox ambisonic layouts with arbitrary channel mapping

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2024-04-26 11:37:10 -03:00
parent 08781ebe1a
commit 37c8d93e56
1 changed files with 22 additions and 7 deletions

View File

@ -7925,7 +7925,8 @@ cleanup:
static int mov_read_SA3D(MOVContext *c, AVIOContext *pb, MOVAtom atom)
{
AVStream *st;
int i, version, type;
AVChannelLayout ch_layout = { 0 };
int ret, i, version, type;
int ambisonic_order, channel_order, normalization, channel_count;
if (c->fc->nb_streams < 1)
@ -7968,24 +7969,38 @@ static int mov_read_SA3D(MOVContext *c, AVIOContext *pb, MOVAtom atom)
}
channel_count = avio_rb32(pb);
if (ambisonic_order < 0 || channel_count != (ambisonic_order + 1LL) * (ambisonic_order + 1LL)) {
if (ambisonic_order < 0 || ambisonic_order > 31 ||
channel_count != (ambisonic_order + 1LL) * (ambisonic_order + 1LL)) {
av_log(c->fc, AV_LOG_ERROR,
"Invalid number of channels (%d / %d)\n",
channel_count, ambisonic_order);
return 0;
}
ret = av_channel_layout_custom_init(&ch_layout, channel_count);
if (ret < 0)
return 0;
for (i = 0; i < channel_count; i++) {
if (i != avio_rb32(pb)) {
av_log(c->fc, AV_LOG_WARNING,
"Ambisonic channel reordering is not supported\n");
unsigned channel = avio_rb32(pb);
if (channel >= channel_count) {
av_log(c->fc, AV_LOG_ERROR, "Invalid channel index (%d / %d)\n",
channel, ambisonic_order);
av_channel_layout_uninit(&ch_layout);
return 0;
}
ch_layout.u.map[i].id = AV_CHAN_AMBISONIC_BASE + channel;
}
ret = av_channel_layout_retype(&ch_layout, 0, AV_CHANNEL_LAYOUT_RETYPE_FLAG_CANONICAL);
if (ret < 0) {
av_channel_layout_uninit(&ch_layout);
return 0;
}
av_channel_layout_uninit(&st->codecpar->ch_layout);
st->codecpar->ch_layout.order = AV_CHANNEL_ORDER_AMBISONIC;
st->codecpar->ch_layout.nb_channels = channel_count;
st->codecpar->ch_layout = ch_layout;
return 0;
}