From 62397bcf6a5b4052388ab4898a747cda56abc082 Mon Sep 17 00:00:00 2001 From: James Almer Date: Mon, 13 May 2024 21:10:19 -0300 Subject: [PATCH] avformat/movenc: add support for writing SA3D boxes Signed-off-by: James Almer --- libavformat/movenc.c | 63 +++++++++++++++++++++++++++++++++++++++++++ libavformat/version.h | 2 +- 2 files changed, 64 insertions(+), 1 deletion(-) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index f907f67752..86c1b1da2c 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -916,6 +916,64 @@ static int mov_write_dmlp_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra return update_size(pb, pos); } +static int mov_write_SA3D_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track) +{ + const AVDictionaryEntry *str = av_dict_get(track->st->metadata, "SA3D", NULL, 0); + AVChannelLayout ch_layout = { 0 }; + int64_t pos; + int ambisonic_order, ambi_channels, non_diegetic_channels; + int i, ret; + + if (!str) + return 0; + + ret = av_channel_layout_from_string(&ch_layout, str->value); + if (ret < 0) { + if (ret == AVERROR(EINVAL)) { +invalid: + av_log(s, AV_LOG_ERROR, "Invalid SA3D layout: \"%s\"\n", str->value); + ret = 0; + } + av_channel_layout_uninit(&ch_layout); + return ret; + } + + if (track->st->codecpar->ch_layout.nb_channels != ch_layout.nb_channels) + goto invalid; + + ambisonic_order = av_channel_layout_ambisonic_order(&ch_layout); + if (ambisonic_order < 0) + goto invalid; + + ambi_channels = (ambisonic_order + 1LL) * (ambisonic_order + 1LL); + non_diegetic_channels = ch_layout.nb_channels - ambi_channels; + if (non_diegetic_channels && + (non_diegetic_channels != 2 || + av_channel_layout_subset(&ch_layout, AV_CH_LAYOUT_STEREO) != AV_CH_LAYOUT_STEREO)) + goto invalid; + + av_log(s, AV_LOG_VERBOSE, "Inserting SA3D box with layout: \"%s\"\n", str->value); + + pos = avio_tell(pb); + + avio_wb32(pb, 0); // Size + ffio_wfourcc(pb, "SA3D"); + avio_w8(pb, 0); // version + avio_w8(pb, (!!non_diegetic_channels) << 7); // head_locked_stereo and ambisonic_type + avio_wb32(pb, ambisonic_order); // ambisonic_order + avio_w8(pb, 0); // ambisonic_channel_ordering + avio_w8(pb, 0); // ambisonic_normalization + avio_wb32(pb, ch_layout.nb_channels); // num_channels + for (i = 0; i < ambi_channels; i++) + avio_wb32(pb, av_channel_layout_channel_from_index(&ch_layout, i) - AV_CHAN_AMBISONIC_BASE); + for (; i < ch_layout.nb_channels; i++) + avio_wb32(pb, av_channel_layout_channel_from_index(&ch_layout, i) + ambi_channels); + + av_channel_layout_uninit(&ch_layout); + + return update_size(pb, pos); +} + static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track) { uint32_t layout_tag, bitmap, *channel_desc; @@ -1419,6 +1477,11 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex if (ret < 0) return ret; + if (track->mode == MODE_MP4 && track->par->codec_type == AVMEDIA_TYPE_AUDIO + && ((ret = mov_write_SA3D_tag(s, pb, track)) < 0)) { + return ret; + } + if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO && ((ret = mov_write_chan_tag(s, pb, track)) < 0)) { return ret; diff --git a/libavformat/version.h b/libavformat/version.h index 5310326bda..4687cd857c 100644 --- a/libavformat/version.h +++ b/libavformat/version.h @@ -32,7 +32,7 @@ #include "version_major.h" #define LIBAVFORMAT_VERSION_MINOR 3 -#define LIBAVFORMAT_VERSION_MICRO 103 +#define LIBAVFORMAT_VERSION_MICRO 104 #define LIBAVFORMAT_VERSION_INT AV_VERSION_INT(LIBAVFORMAT_VERSION_MAJOR, \ LIBAVFORMAT_VERSION_MINOR, \