diff --git a/libavformat/riffdec.c b/libavformat/riffdec.c
index bd32e59837..a62e9c070f 100644
--- a/libavformat/riffdec.c
+++ b/libavformat/riffdec.c
@@ -61,11 +61,14 @@ static void parse_waveformatex(AVFormatContext *s, AVIOContext *pb, AVCodecParam
 {
     ff_asf_guid subformat;
     int bps;
+    uint64_t mask;
 
     bps = avio_rl16(pb);
     if (bps)
         par->bits_per_coded_sample = bps;
-    par->channel_layout        = avio_rl32(pb); /* dwChannelMask */
+
+    mask = avio_rl32(pb); /* dwChannelMask */
+    av_channel_layout_from_mask(&par->ch_layout, mask);
 
     ff_get_guid(pb, &subformat);
     if (!memcmp(subformat + 4,
@@ -90,7 +93,7 @@ static void parse_waveformatex(AVFormatContext *s, AVIOContext *pb, AVCodecParam
 int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
                       AVCodecParameters *par, int size, int big_endian)
 {
-    int id;
+    int id, channels;
     uint64_t bitrate = 0;
 
     if (size < 14) {
@@ -102,14 +105,14 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
     if (!big_endian) {
         id                 = avio_rl16(pb);
         if (id != 0x0165) {
-            par->channels    = avio_rl16(pb);
+            channels         = avio_rl16(pb);
             par->sample_rate = avio_rl32(pb);
             bitrate            = avio_rl32(pb) * 8LL;
             par->block_align = avio_rl16(pb);
         }
     } else {
         id                 = avio_rb16(pb);
-        par->channels    = avio_rb16(pb);
+        channels           = avio_rb16(pb);
         par->sample_rate = avio_rb32(pb);
         bitrate            = avio_rb32(pb) * 8LL;
         par->block_align = avio_rb16(pb);
@@ -160,12 +163,12 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
             return AVERROR(ENOMEM);
         nb_streams         = AV_RL16(par->extradata + 4);
         par->sample_rate   = AV_RL32(par->extradata + 12);
-        par->channels      = 0;
+        channels           = 0;
         bitrate            = 0;
         if (size < 8 + nb_streams * 20)
             return AVERROR_INVALIDDATA;
         for (i = 0; i < nb_streams; i++)
-            par->channels += par->extradata[8 + i * 20 + 17];
+            channels += par->extradata[8 + i * 20 + 17];
     }
 
     par->bit_rate = bitrate;
@@ -178,13 +181,16 @@ int ff_get_wav_header(AVFormatContext *s, AVIOContext *pb,
     if (par->codec_id == AV_CODEC_ID_AAC_LATM) {
         /* Channels and sample_rate values are those prior to applying SBR
          * and/or PS. */
-        par->channels    = 0;
+        channels         = 0;
         par->sample_rate = 0;
     }
     /* override bits_per_coded_sample for G.726 */
     if (par->codec_id == AV_CODEC_ID_ADPCM_G726 && par->sample_rate)
         par->bits_per_coded_sample = par->bit_rate / par->sample_rate;
 
+    par->ch_layout.order       = AV_CHANNEL_ORDER_UNSPEC;
+    par->ch_layout.nb_channels = channels;
+
     return 0;
 }
 
diff --git a/libavformat/riffenc.c b/libavformat/riffenc.c
index ffccfa3d48..04cde7b37f 100644
--- a/libavformat/riffenc.c
+++ b/libavformat/riffenc.c
@@ -77,9 +77,9 @@ int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb,
      * for indicating packet duration. */
     frame_size = av_get_audio_frame_duration2(par, par->block_align);
 
-    waveformatextensible = (par->channels > 2 && par->channel_layout) ||
-                           par->channels == 1 && par->channel_layout && par->channel_layout != AV_CH_LAYOUT_MONO ||
-                           par->channels == 2 && par->channel_layout && par->channel_layout != AV_CH_LAYOUT_STEREO ||
+    waveformatextensible = (par->ch_layout.order == AV_CHANNEL_ORDER_NATIVE &&
+                            av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_MONO) &&
+                            av_channel_layout_compare(&par->ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO)) ||
                            par->sample_rate > 48000 ||
                            par->codec_id == AV_CODEC_ID_EAC3 ||
                            av_get_bits_per_sample(par->codec_id) > 16;
@@ -89,7 +89,7 @@ int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb,
     else
         avio_wl16(pb, par->codec_tag);
 
-    avio_wl16(pb, par->channels);
+    avio_wl16(pb, par->ch_layout.nb_channels);
     avio_wl32(pb, par->sample_rate);
     if (par->codec_id == AV_CODEC_ID_ATRAC3 ||
         par->codec_id == AV_CODEC_ID_G723_1 ||
@@ -119,13 +119,13 @@ int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb,
     } else if (par->codec_id == AV_CODEC_ID_AC3) {
         blkalign = 3840;                /* maximum bytes per frame */
     } else if (par->codec_id == AV_CODEC_ID_AAC) {
-        blkalign = 768 * par->channels; /* maximum bytes per frame */
+        blkalign = 768 * par->ch_layout.nb_channels; /* maximum bytes per frame */
     } else if (par->codec_id == AV_CODEC_ID_G723_1) {
         blkalign = 24;
     } else if (par->block_align != 0) { /* specified by the codec */
         blkalign = par->block_align;
     } else
-        blkalign = bps * par->channels / av_gcd(8, bps);
+        blkalign = bps * par->ch_layout.nb_channels / av_gcd(8, bps);
     if (par->codec_id == AV_CODEC_ID_PCM_U8 ||
         par->codec_id == AV_CODEC_ID_PCM_S24LE ||
         par->codec_id == AV_CODEC_ID_PCM_S32LE ||
@@ -153,7 +153,7 @@ int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb,
         /* dwHeadBitrate */
         bytestream_put_le32(&riff_extradata, par->bit_rate);
         /* fwHeadMode */
-        bytestream_put_le16(&riff_extradata, par->channels == 2 ? 1 : 8);
+        bytestream_put_le16(&riff_extradata, par->ch_layout.nb_channels == 2 ? 1 : 8);
         /* fwHeadModeExt */
         bytestream_put_le16(&riff_extradata, 0);
         /* wHeadEmphasis */
@@ -180,13 +180,13 @@ int ff_put_wav_header(AVFormatContext *s, AVIOContext *pb,
     if (waveformatextensible) {
         int write_channel_mask = !(flags & FF_PUT_WAV_HEADER_SKIP_CHANNELMASK) &&
                                  (s->strict_std_compliance < FF_COMPLIANCE_NORMAL ||
-                                  par->channel_layout < 0x40000);
+                                  par->ch_layout.u.mask < 0x40000);
         /* 22 is WAVEFORMATEXTENSIBLE size */
         avio_wl16(pb, riff_extradata - riff_extradata_start + 22);
         /* ValidBitsPerSample || SamplesPerBlock || Reserved */
         avio_wl16(pb, bps);
         /* dwChannelMask */
-        avio_wl32(pb, write_channel_mask ? par->channel_layout : 0);
+        avio_wl32(pb, write_channel_mask ? par->ch_layout.u.mask : 0);
         /* GUID + next 3 */
         if (par->codec_id == AV_CODEC_ID_EAC3) {
             ff_put_guid(pb, ff_get_codec_guid(par->codec_id, ff_codec_wav_guids));
