aboutsummaryrefslogtreecommitdiffstats
path: root/libavformat/movenc.c
diff options
context:
space:
mode:
authorJames Almer <jamrial@gmail.com>2024-05-13 21:10:19 -0300
committerJames Almer <jamrial@gmail.com>2024-05-23 19:06:46 -0300
commit62397bcf6a5b4052388ab4898a747cda56abc082 (patch)
tree1f6fbb7246f3ebc4c146ebd5700bb558e910166b /libavformat/movenc.c
parent8c974494822cdb59cbc271c5dfec4771a579d668 (diff)
downloadffmpeg-62397bcf6a5b4052388ab4898a747cda56abc082.tar.gz
avformat/movenc: add support for writing SA3D boxes
Signed-off-by: James Almer <jamrial@gmail.com>
Diffstat (limited to 'libavformat/movenc.c')
-rw-r--r--libavformat/movenc.c63
1 files changed, 63 insertions, 0 deletions
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;