1
0
mirror of https://github.com/FFmpeg/FFmpeg.git synced 2024-11-26 19:01:44 +02:00

avformat/movenc: add support for writing SA3D boxes

Signed-off-by: James Almer <jamrial@gmail.com>
This commit is contained in:
James Almer 2024-05-13 21:10:19 -03:00
parent 8c97449482
commit 62397bcf6a
2 changed files with 64 additions and 1 deletions

View File

@ -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;

View File

@ -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, \