diff mbox series

[FFmpeg-devel,6/7] avformat/movenc: don't use mono layout when a front center label is expected

Message ID 20220328232453.54773-6-jamrial@gmail.com
State Accepted
Commit 7ccc9108032aeaa21b8ad91b201ed19c88b8d239
Headers show
Series [FFmpeg-devel,1/7] avformat/mov_chan: rename mov_get_channel_label() to better reflect its purpose | expand

Commit Message

James Almer March 28, 2022, 11:24 p.m. UTC
On output streams where a multichannel stream needs to be stored as one track
per channel, each track will have a channel layout describing the position of
the channel they contain. For the track with front center, the mov muxer was
using the mov layout "mono" instead of the label for the front center position.

Since our channel layout API considers front center == mono, we need to do some
heuristics. To achieve this, we make sure all audio tracks contain streams with
a single channel, and only one of them is front center. In that case, we write
the front center label instead of signaling mono layout.

Fixes the last part of ticket #2865

Signed-off-by: James Almer <jamrial@gmail.com>
---
 libavformat/movenc.c | 25 +++++++++++++++++++++++++
 libavformat/movenc.h |  1 +
 2 files changed, 26 insertions(+)
diff mbox series

Patch

diff --git a/libavformat/movenc.c b/libavformat/movenc.c
index 263649f1da..b9956e699c 100644
--- a/libavformat/movenc.c
+++ b/libavformat/movenc.c
@@ -887,6 +887,17 @@  static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra
         return ret;
     }
 
+    if (layout_tag == MOV_CH_LAYOUT_MONO && track->mono_as_fc > 0) {
+        av_assert0(!channel_desc);
+        channel_desc = av_malloc(sizeof(*channel_desc));
+        if (!channel_desc)
+            return AVERROR(ENOMEM);
+
+        layout_tag = 0;
+        bitmap = 0;
+        *channel_desc = 3; // channel label "Center"
+    }
+
     num_desc = layout_tag ? 0 : track->par->ch_layout.nb_channels;
 
     avio_wb32(pb, 0);           // Size
@@ -6970,6 +6981,20 @@  static int mov_write_header(AVFormatContext *s)
             if (j == i)
                 continue;
 
+            if (stj->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
+                (trackj->par->ch_layout.nb_channels != 1 ||
+                 !av_channel_layout_compare(&trackj->par->ch_layout,
+                                            &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO))
+            )
+                track->mono_as_fc = -1;
+
+            if (stj->codecpar->codec_type == AVMEDIA_TYPE_AUDIO &&
+                av_channel_layout_compare(&trackj->par->ch_layout,
+                                          &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) &&
+                trackj->par->ch_layout.nb_channels == 1 && track->mono_as_fc >= 0
+            )
+                track->mono_as_fc++;
+
             if (stj->codecpar->codec_type != AVMEDIA_TYPE_AUDIO ||
                 av_channel_layout_compare(&trackj->par->ch_layout,
                                           &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) ||
diff --git a/libavformat/movenc.h b/libavformat/movenc.h
index 2ac84ed070..67d6d4fb66 100644
--- a/libavformat/movenc.h
+++ b/libavformat/movenc.h
@@ -107,6 +107,7 @@  typedef struct MOVTrack {
     int         tag; ///< stsd fourcc
     AVStream        *st;
     AVCodecParameters *par;
+    int mono_as_fc;
     int multichannel_as_mono;
 
     int         vos_len;