[FFmpeg-devel] [PATCH 109/281] mxf: convert to new channel layout API

James Almer jamrial at gmail.com
Thu Jan 13 03:56:02 EET 2022


From: Vittorio Giovara <vittorio.giovara at gmail.com>

Signed-off-by: Vittorio Giovara <vittorio.giovara at gmail.com>
Signed-off-by: Anton Khirnov <anton at khirnov.net>
Signed-off-by: James Almer <jamrial at gmail.com>
---
 libavformat/mxfdec.c | 34 +++++++++++++++++++++-------------
 libavformat/mxfenc.c | 20 +++++++++++---------
 2 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/libavformat/mxfdec.c b/libavformat/mxfdec.c
index b85c10bf19..2db32255ab 100644
--- a/libavformat/mxfdec.c
+++ b/libavformat/mxfdec.c
@@ -591,18 +591,19 @@ static int mxf_get_d10_aes3_packet(AVIOContext *pb, AVStream *st, AVPacket *pkt,
     end_ptr = pkt->data + length;
     buf_ptr = pkt->data + 4; /* skip SMPTE 331M header */
 
-    if (st->codecpar->channels > 8)
+    if (st->codecpar->ch_layout.nb_channels > 8)
         return AVERROR_INVALIDDATA;
 
-    for (; end_ptr - buf_ptr >= st->codecpar->channels * 4; ) {
-        for (i = 0; i < st->codecpar->channels; i++) {
+    for (; end_ptr - buf_ptr >= st->codecpar->ch_layout.nb_channels * 4; ) {
+        for (i = 0; i < st->codecpar->ch_layout.nb_channels; i++) {
             uint32_t sample = bytestream_get_le32(&buf_ptr);
             if (st->codecpar->bits_per_coded_sample == 24)
                 bytestream_put_le24(&data_ptr, (sample >> 4) & 0xffffff);
             else
                 bytestream_put_le16(&data_ptr, (sample >> 12) & 0xffff);
         }
-        buf_ptr += 32 - st->codecpar->channels*4; // always 8 channels stored SMPTE 331M
+        // always 8 channels stored SMPTE 331M
+        buf_ptr += 32 - st->codecpar->ch_layout.nb_channels * 4;
     }
     av_shrink_packet(pkt, data_ptr - pkt->data);
     return 0;
@@ -2536,6 +2537,7 @@ static int parse_mca_labels(MXFContext *mxf, MXFTrack *source_track, MXFDescript
 
     if (has_channel_label) {
         uint64_t channel_layout = 0;
+        int ret;
 
         for (int i = 0; i < descriptor->channels; i++) {
             if (!routing[i]) {
@@ -2544,9 +2546,11 @@ static int parse_mca_labels(MXFContext *mxf, MXFTrack *source_track, MXFDescript
                 return 0;
             }
             if (channel_layout & routing[i]) {
+                char buf[32];
+                av_channel_name(buf, sizeof(buf), routing[i]);
                 av_log(mxf->fc, AV_LOG_WARNING, "%s audio channel is used multiple times in stream #%d, "
                                                 "falling back to unknown channel layout\n",
-                                                av_get_channel_name(routing[i]), st->index);
+                                                buf, st->index);
                 return 0;
             }
             if (routing[i] < channel_layout) {
@@ -2557,9 +2561,11 @@ static int parse_mca_labels(MXFContext *mxf, MXFTrack *source_track, MXFDescript
             channel_layout |= routing[i];
         }
 
-        av_assert0(descriptor->channels == av_get_channel_layout_nb_channels(channel_layout));
+        av_assert0(descriptor->channels == av_popcount64(channel_layout));
 
-        st->codecpar->channel_layout = channel_layout;
+        ret = av_channel_layout_from_mask(&st->codecpar->ch_layout, channel_layout);
+        if (ret < 0)
+            return ret;
     }
 
     return 0;
@@ -2927,7 +2933,8 @@ static int mxf_parse_structural_metadata(MXFContext *mxf)
             /* Only overwrite existing codec ID if it is unset or A-law, which is the default according to SMPTE RP 224. */
             if (st->codecpar->codec_id == AV_CODEC_ID_NONE || (st->codecpar->codec_id == AV_CODEC_ID_PCM_ALAW && (enum AVCodecID)container_ul->id != AV_CODEC_ID_NONE))
                 st->codecpar->codec_id = (enum AVCodecID)container_ul->id;
-            st->codecpar->channels = descriptor->channels;
+            st->codecpar->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+            st->codecpar->ch_layout.nb_channels = descriptor->channels;
 
             if (descriptor->sample_rate.den > 0) {
                 st->codecpar->sample_rate = descriptor->sample_rate.num / descriptor->sample_rate.den;
@@ -3553,7 +3560,8 @@ static int mxf_handle_missing_index_segment(MXFContext *mxf, AVStream *st)
         return 0;
 
     if (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && is_pcm(st->codecpar->codec_id)) {
-        edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->channels) >> 3;
+        edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) *
+                                st->codecpar->ch_layout.nb_channels) >> 3;
     } else if (st->duration > 0 && p->first_essence_klv.length > 0 && p->first_essence_klv.length % st->duration == 0) {
         edit_unit_byte_count = p->first_essence_klv.length / st->duration;
     }
@@ -3858,12 +3866,12 @@ static int mxf_set_audio_pts(MXFContext *mxf, AVCodecParameters *par,
 
     pkt->pts = track->sample_count;
 
-    if (   par->channels <= 0
-        || bits_per_sample <= 0
-        || par->channels * (int64_t)bits_per_sample < 8)
+    if (par->ch_layout.nb_channels <= 0 ||
+        bits_per_sample <= 0            ||
+        par->ch_layout.nb_channels * (int64_t)bits_per_sample < 8)
         track->sample_count = mxf_compute_sample_count(mxf, st, av_rescale_q(track->sample_count, st->time_base, av_inv_q(track->edit_rate)) + 1);
     else
-        track->sample_count += pkt->size / (par->channels * (int64_t)bits_per_sample / 8);
+        track->sample_count += pkt->size / (par->ch_layout.nb_channels * (int64_t)bits_per_sample / 8);
 
     return 0;
 }
diff --git a/libavformat/mxfenc.c b/libavformat/mxfenc.c
index 5e068c8220..5fa234c5c0 100644
--- a/libavformat/mxfenc.c
+++ b/libavformat/mxfenc.c
@@ -1455,17 +1455,19 @@ static int64_t mxf_write_generic_sound_common(AVFormatContext *s, AVStream *st,
 
     mxf_write_local_tag(s, 4, 0x3D07);
     if (mxf->channel_count == -1) {
-        if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) && (st->codecpar->channels != 4) && (st->codecpar->channels != 8))
+        if (show_warnings && (s->oformat == &ff_mxf_d10_muxer) &&
+            (st->codecpar->ch_layout.nb_channels != 4) &&
+            (st->codecpar->ch_layout.nb_channels != 8))
             av_log(s, AV_LOG_WARNING, "the number of audio channels shall be 4 or 8 : the output will not comply to MXF D-10 specs, use -d10_channelcount to fix this\n");
-        avio_wb32(pb, st->codecpar->channels);
+        avio_wb32(pb, st->codecpar->ch_layout.nb_channels);
     } else if (s->oformat == &ff_mxf_d10_muxer) {
-        if (show_warnings && (mxf->channel_count < st->codecpar->channels))
+        if (show_warnings && (mxf->channel_count < st->codecpar->ch_layout.nb_channels))
             av_log(s, AV_LOG_WARNING, "d10_channelcount < actual number of audio channels : some channels will be discarded\n");
         if (show_warnings && (mxf->channel_count != 4) && (mxf->channel_count != 8))
             av_log(s, AV_LOG_WARNING, "d10_channelcount shall be set to 4 or 8 : the output will not comply to MXF D-10 specs\n");
         avio_wb32(pb, mxf->channel_count);
     } else {
-        avio_wb32(pb, st->codecpar->channels);
+        avio_wb32(pb, st->codecpar->ch_layout.nb_channels);
     }
 
     mxf_write_local_tag(s, 4, 0x3D01);
@@ -2624,7 +2626,7 @@ static int mxf_init(AVFormatContext *s)
                     av_log(s, AV_LOG_ERROR, "Only pcm_s16le and pcm_s24le audio codecs are implemented\n");
                     return AVERROR_PATCHWELCOME;
                 }
-                if (st->codecpar->channels != 1) {
+                if (st->codecpar->ch_layout.nb_channels != 1) {
                     av_log(s, AV_LOG_ERROR, "MXF OPAtom only supports single channel audio\n");
                     return AVERROR(EINVAL);
                 }
@@ -2633,11 +2635,11 @@ static int mxf_init(AVFormatContext *s)
                 if((ret = mxf_init_timecode(s, st, tbc)) < 0)
                     return ret;
 
-                mxf->edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->channels) >> 3;
+                mxf->edit_unit_byte_count = (av_get_bits_per_sample(st->codecpar->codec_id) * st->codecpar->ch_layout.nb_channels) >> 3;
                 sc->index = INDEX_WAV;
             } else {
                 mxf->slice_count = 1;
-                sc->frame_size = st->codecpar->channels *
+                sc->frame_size = st->codecpar->ch_layout.nb_channels *
                                  av_rescale_rnd(st->codecpar->sample_rate, mxf->time_base.num, mxf->time_base.den, AV_ROUND_UP) *
                                  av_get_bits_per_sample(st->codecpar->codec_id) / 8;
             }
@@ -2783,10 +2785,10 @@ static void mxf_write_d10_audio_packet(AVFormatContext *s, AVStream *st, AVPacke
 
     avio_w8(pb, (frame_size == 1920 ? 0 : (mxf->edit_units_count-1) % 5 + 1));
     avio_wl16(pb, frame_size);
-    avio_w8(pb, (1<<st->codecpar->channels)-1);
+    avio_w8(pb, (1 << st->codecpar->ch_layout.nb_channels)-1);
 
     while (samples < end) {
-        for (i = 0; i < st->codecpar->channels; i++) {
+        for (i = 0; i < st->codecpar->ch_layout.nb_channels; i++) {
             uint32_t sample;
             if (st->codecpar->codec_id == AV_CODEC_ID_PCM_S24LE) {
                 sample = AV_RL24(samples)<< 4;
-- 
2.34.1



More information about the ffmpeg-devel mailing list