diff --git a/libavcodec/flac.c b/libavcodec/flac.c
index 7b075d4bd3..51014faea1 100644
--- a/libavcodec/flac.c
+++ b/libavcodec/flac.c
@@ -29,15 +29,15 @@
 
 static const int8_t sample_size_table[] = { 0, 8, 12, 0, 16, 20, 24, 0 };
 
-static const uint64_t flac_channel_layouts[8] = {
-    AV_CH_LAYOUT_MONO,
-    AV_CH_LAYOUT_STEREO,
-    AV_CH_LAYOUT_SURROUND,
-    AV_CH_LAYOUT_QUAD,
-    AV_CH_LAYOUT_5POINT0,
-    AV_CH_LAYOUT_5POINT1,
-    AV_CH_LAYOUT_6POINT1,
-    AV_CH_LAYOUT_7POINT1
+static const AVChannelLayout flac_channel_layouts[8] = {
+    AV_CHANNEL_LAYOUT_MONO,
+    AV_CHANNEL_LAYOUT_STEREO,
+    AV_CHANNEL_LAYOUT_SURROUND,
+    AV_CHANNEL_LAYOUT_QUAD,
+    AV_CHANNEL_LAYOUT_5POINT0,
+    AV_CHANNEL_LAYOUT_5POINT1,
+    AV_CHANNEL_LAYOUT_6POINT1,
+    AV_CHANNEL_LAYOUT_7POINT1
 };
 
 static int64_t get_utf8(GetBitContext *gb)
@@ -193,12 +193,19 @@ int ff_flac_is_extradata_valid(AVCodecContext *avctx,
     return 1;
 }
 
-void ff_flac_set_channel_layout(AVCodecContext *avctx)
+void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
 {
-    if (avctx->channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
-        avctx->channel_layout = flac_channel_layouts[avctx->channels - 1];
+    if (channels == avctx->ch_layout.nb_channels &&
+        avctx->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
+        avctx->ch_layout.u.mask)
+        return;
+
+    av_channel_layout_uninit(&avctx->ch_layout);
+    if (channels <= FF_ARRAY_ELEMS(flac_channel_layouts))
+        avctx->ch_layout = flac_channel_layouts[channels - 1];
     else
-        avctx->channel_layout = 0;
+        avctx->ch_layout = (AVChannelLayout){ .order = AV_CHANNEL_ORDER_UNSPEC,
+                                              .nb_channels = channels };
 }
 
 int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
@@ -229,13 +236,9 @@ int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
         return AVERROR_INVALIDDATA;
     }
 
-    avctx->channels = s->channels;
     avctx->sample_rate = s->samplerate;
     avctx->bits_per_raw_sample = s->bps;
-
-    if (!avctx->channel_layout ||
-        av_get_channel_layout_nb_channels(avctx->channel_layout) != avctx->channels)
-        ff_flac_set_channel_layout(avctx);
+    ff_flac_set_channel_layout(avctx, s->channels);
 
     s->samples = get_bits64(&gb, 36);
 
diff --git a/libavcodec/flac.h b/libavcodec/flac.h
index 991ab43f3c..cb220ab4c0 100644
--- a/libavcodec/flac.h
+++ b/libavcodec/flac.h
@@ -131,7 +131,7 @@ int ff_flac_get_max_frame_size(int blocksize, int ch, int bps);
 int ff_flac_decode_frame_header(AVCodecContext *avctx, GetBitContext *gb,
                                 FLACFrameInfo *fi, int log_level_offset);
 
-void ff_flac_set_channel_layout(AVCodecContext *avctx);
+void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels);
 
 /**
  * Parse the metadata block parameters from the header.
diff --git a/libavcodec/flac_parser.c b/libavcodec/flac_parser.c
index cd9a2cb574..81b6f12ab3 100644
--- a/libavcodec/flac_parser.c
+++ b/libavcodec/flac_parser.c
@@ -628,11 +628,8 @@ static int get_best_header(FLACParseContext *fpc, const uint8_t **poutbuf,
         check_header_mismatch(fpc, header, child, 0);
     }
 
-    if (header->fi.channels != fpc->avctx->channels ||
-        !fpc->avctx->channel_layout) {
-        fpc->avctx->channels = header->fi.channels;
-        ff_flac_set_channel_layout(fpc->avctx);
-    }
+    ff_flac_set_channel_layout(fpc->avctx, header->fi.channels);
+
     fpc->avctx->sample_rate = header->fi.samplerate;
     fpc->pc->duration       = header->fi.blocksize;
     *poutbuf = flac_fifo_read_wrap(fpc, header->offset, *poutbuf_size,
diff --git a/libavcodec/flacdec.c b/libavcodec/flacdec.c
index 09051cc663..eaa2dc6cf1 100644
--- a/libavcodec/flacdec.c
+++ b/libavcodec/flacdec.c
@@ -483,15 +483,14 @@ static int decode_frame(FLACContext *s)
     if (   s->flac_stream_info.channels
         && fi.channels != s->flac_stream_info.channels
         && s->got_streaminfo) {
-        s->flac_stream_info.channels = s->avctx->channels = fi.channels;
-        ff_flac_set_channel_layout(s->avctx);
+        s->flac_stream_info.channels = fi.channels;
+        ff_flac_set_channel_layout(s->avctx, fi.channels);
         ret = allocate_buffers(s);
         if (ret < 0)
             return ret;
     }
-    s->flac_stream_info.channels = s->avctx->channels = fi.channels;
-    if (!s->avctx->channel_layout)
-        ff_flac_set_channel_layout(s->avctx);
+    s->flac_stream_info.channels = fi.channels;
+    ff_flac_set_channel_layout(s->avctx, fi.channels);
     s->ch_mode = fi.ch_mode;
 
     if (!s->flac_stream_info.bps && !fi.bps) {
diff --git a/libavcodec/flacenc.c b/libavcodec/flacenc.c
index 9f6f449323..0912ca2f47 100644
--- a/libavcodec/flacenc.c
+++ b/libavcodec/flacenc.c
@@ -241,7 +241,7 @@ static av_cold void dprint_compression_options(FlacEncodeContext *s)
 static av_cold int flac_encode_init(AVCodecContext *avctx)
 {
     int freq = avctx->sample_rate;
-    int channels = avctx->channels;
+    int channels = avctx->ch_layout.nb_channels;
     FlacEncodeContext *s = avctx->priv_data;
     int i, level, ret;
     uint8_t *streaminfo;
@@ -398,18 +398,18 @@ static av_cold int flac_encode_init(AVCodecContext *avctx)
     s->frame_count   = 0;
     s->min_framesize = s->max_framesize;
 
-    if (channels == 3 &&
-            avctx->channel_layout != (AV_CH_LAYOUT_STEREO|AV_CH_FRONT_CENTER) ||
-        channels == 4 &&
-            avctx->channel_layout != AV_CH_LAYOUT_2_2 &&
-            avctx->channel_layout != AV_CH_LAYOUT_QUAD ||
-        channels == 5 &&
-            avctx->channel_layout != AV_CH_LAYOUT_5POINT0 &&
-            avctx->channel_layout != AV_CH_LAYOUT_5POINT0_BACK ||
-        channels == 6 &&
-            avctx->channel_layout != AV_CH_LAYOUT_5POINT1 &&
-            avctx->channel_layout != AV_CH_LAYOUT_5POINT1_BACK) {
-        if (avctx->channel_layout) {
+    if ((channels == 3 &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_SURROUND)) ||
+        (channels == 4 &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_2_2) &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_QUAD)) ||
+        (channels == 5 &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0) &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT0_BACK)) ||
+        (channels == 6 &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1) &&
+         av_channel_layout_compare(&avctx->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_5POINT1_BACK))) {
+        if (avctx->ch_layout.order != AV_CHANNEL_ORDER_UNSPEC) {
             av_log(avctx, AV_LOG_ERROR, "Channel layout not supported by Flac, "
                                              "output stream will have incorrect "
                                              "channel layout.\n");
