diff --git a/libavcodec/wma.c b/libavcodec/wma.c
index d9fda2dbfa..4cd60cc35e 100644
--- a/libavcodec/wma.c
+++ b/libavcodec/wma.c
@@ -79,6 +79,7 @@ static av_cold int init_coef_vlc(VLC *vlc, uint16_t **prun_table,
 av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
 {
     WMACodecContext *s = avctx->priv_data;
+    int channels = avctx->ch_layout.nb_channels;
     int i, ret;
     float bps1, high_freq;
     float bps;
@@ -86,7 +87,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
     int coef_vlc_table;
 
     if (avctx->sample_rate <= 0 || avctx->sample_rate > 50000 ||
-        avctx->channels    <= 0 || avctx->channels    > 2     ||
+        channels           <= 0 || channels    > 2            ||
         avctx->bit_rate    <= 0)
         return -1;
 
@@ -107,7 +108,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
     if (s->use_variable_block_len) {
         int nb_max, nb;
         nb = ((flags2 >> 3) & 3) + 1;
-        if ((avctx->bit_rate / avctx->channels) >= 32000)
+        if ((avctx->bit_rate / channels) >= 32000)
             nb += 2;
         nb_max = s->frame_len_bits - BLOCK_MIN_BITS;
         if (nb > nb_max)
@@ -136,7 +137,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
     }
 
     bps                 = (float) avctx->bit_rate /
-                          (float) (avctx->channels * avctx->sample_rate);
+                          (float) (channels * avctx->sample_rate);
     s->byte_offset_bits = av_log2((int) (bps * s->frame_len / 8.0 + 0.5)) + 2;
     if (s->byte_offset_bits + 3 > MIN_CACHE_BITS) {
         av_log(avctx, AV_LOG_ERROR, "byte_offset_bits %d is too large\n", s->byte_offset_bits);
@@ -146,7 +147,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
     /* compute high frequency value and choose if noise coding should
      * be activated */
     bps1 = bps;
-    if (avctx->channels == 2)
+    if (channels == 2)
         bps1 = bps * 1.6;
     if (sample_rate1 == 44100) {
         if (bps1 >= 0.61)
@@ -184,7 +185,7 @@ av_cold int ff_wma_init(AVCodecContext *avctx, int flags2)
     }
     ff_dlog(s->avctx, "flags2=0x%x\n", flags2);
     ff_dlog(s->avctx, "version=%d channels=%d sample_rate=%d bitrate=%"PRId64" block_align=%d\n",
-            s->version, avctx->channels, avctx->sample_rate, avctx->bit_rate,
+            s->version, channels, avctx->sample_rate, avctx->bit_rate,
             avctx->block_align);
     ff_dlog(s->avctx, "bps=%f bps1=%f high_freq=%f bitoffset=%d\n",
             bps, bps1, high_freq, s->byte_offset_bits);
diff --git a/libavcodec/wmadec.c b/libavcodec/wmadec.c
index 9955aaa7d6..8b0893c74e 100644
--- a/libavcodec/wmadec.c
+++ b/libavcodec/wmadec.c
@@ -439,6 +439,7 @@ static void wma_window(WMACodecContext *s, float *out)
  */
 static int wma_decode_block(WMACodecContext *s)
 {
+    int channels = s->avctx->ch_layout.nb_channels;
     int n, v, a, ch, bsize;
     int coef_nb_bits, total_gain;
     int nb_coefs[MAX_CHANNELS];
@@ -504,10 +505,10 @@ static int wma_decode_block(WMACodecContext *s)
         return -1;
     }
 
-    if (s->avctx->channels == 2)
+    if (channels == 2)
         s->ms_stereo = get_bits1(&s->gb);
     v = 0;
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         a                    = get_bits1(&s->gb);
         s->channel_coded[ch] = a;
         v                   |= a;
@@ -538,12 +539,12 @@ static int wma_decode_block(WMACodecContext *s)
 
     /* compute number of coefficients */
     n = s->coefs_end[bsize] - s->coefs_start;
-    for (ch = 0; ch < s->avctx->channels; ch++)
+    for (ch = 0; ch < channels; ch++)
         nb_coefs[ch] = n;
 
     /* complex coding */
     if (s->use_noise_coding) {
-        for (ch = 0; ch < s->avctx->channels; ch++) {
+        for (ch = 0; ch < channels; ch++) {
             if (s->channel_coded[ch]) {
                 int i, n, a;
                 n = s->exponent_high_sizes[bsize];
@@ -556,7 +557,7 @@ static int wma_decode_block(WMACodecContext *s)
                 }
             }
         }
-        for (ch = 0; ch < s->avctx->channels; ch++) {
+        for (ch = 0; ch < channels; ch++) {
             if (s->channel_coded[ch]) {
                 int i, n, val;
 
@@ -579,7 +580,7 @@ static int wma_decode_block(WMACodecContext *s)
 
     /* exponents can be reused in short blocks. */
     if ((s->block_len_bits == s->frame_len_bits) || get_bits1(&s->gb)) {
-        for (ch = 0; ch < s->avctx->channels; ch++) {
+        for (ch = 0; ch < channels; ch++) {
             if (s->channel_coded[ch]) {
                 if (s->use_exp_vlc) {
                     if (decode_exp_vlc(s, ch) < 0)
@@ -593,13 +594,13 @@ static int wma_decode_block(WMACodecContext *s)
         }
     }
 
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         if (s->channel_coded[ch] && !s->exponents_initialized[ch])
             return AVERROR_INVALIDDATA;
     }
 
     /* parse spectral coefficients : just RLE encoding */
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         if (s->channel_coded[ch]) {
             int tindex;
             WMACoef *ptr = &s->coefs1[ch][0];
@@ -616,7 +617,7 @@ static int wma_decode_block(WMACodecContext *s)
             if (ret < 0)
                 return ret;
         }
-        if (s->version == 1 && s->avctx->channels >= 2)
+        if (s->version == 1 && channels >= 2)
             align_get_bits(&s->gb);
     }
 
@@ -629,7 +630,7 @@ static int wma_decode_block(WMACodecContext *s)
     }
 
     /* finally compute the MDCT coefficients */
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         if (s->channel_coded[ch]) {
             WMACoef *coefs1;
             float *coefs, *exponents, mult, mult1, noise;
@@ -730,7 +731,7 @@ static int wma_decode_block(WMACodecContext *s)
     }
 
 #ifdef TRACE
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         if (s->channel_coded[ch]) {
             dump_floats(s, "exponents", 3, s->exponents[ch], s->block_len);
             dump_floats(s, "coefs", 1, s->coefs[ch], s->block_len);
@@ -754,7 +755,7 @@ static int wma_decode_block(WMACodecContext *s)
 next:
     mdct = &s->mdct_ctx[bsize];
 
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         int n4, index;
 
         n4 = s->block_len / 2;
@@ -799,7 +800,7 @@ static int wma_decode_frame(WMACodecContext *s, float **samples,
             break;
     }
 
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < s->avctx->ch_layout.nb_channels; ch++) {
         /* copy current block to output */
         memcpy(samples[ch] + samples_offset, s->frame_out[ch],
                s->frame_len * sizeof(*s->frame_out[ch]));
@@ -838,7 +839,7 @@ static int wma_decode_superframe(AVCodecContext *avctx, void *data,
         if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
             return ret;
 
-        for (i = 0; i < s->avctx->channels; i++)
+        for (i = 0; i < s->avctx->ch_layout.nb_channels; i++)
             memcpy(frame->extended_data[i], &s->frame_out[i][0],
                    frame->nb_samples * sizeof(s->frame_out[i][0]));
 
diff --git a/libavcodec/wmaenc.c b/libavcodec/wmaenc.c
index 66e3537cb0..4eeaefffba 100644
--- a/libavcodec/wmaenc.c
+++ b/libavcodec/wmaenc.c
@@ -38,10 +38,10 @@ static av_cold int encode_init(AVCodecContext *avctx)
 
     s->avctx = avctx;
 
-    if (avctx->channels > MAX_CHANNELS) {
+    if (avctx->ch_layout.nb_channels > MAX_CHANNELS) {
         av_log(avctx, AV_LOG_ERROR,
                "too many channels: got %i, need %i or fewer\n",
-               avctx->channels, MAX_CHANNELS);
+               avctx->ch_layout.nb_channels, MAX_CHANNELS);
         return AVERROR(EINVAL);
     }
 
@@ -82,7 +82,7 @@ static av_cold int encode_init(AVCodecContext *avctx)
     s->use_exp_vlc            = flags2 & 0x0001;
     s->use_bit_reservoir      = flags2 & 0x0002;
     s->use_variable_block_len = flags2 & 0x0004;
-    if (avctx->channels == 2)
+    if (avctx->ch_layout.nb_channels == 2)
         s->ms_stereo = 1;
 
     if ((ret = ff_wma_init(avctx, flags2)) < 0)
@@ -116,7 +116,7 @@ static int apply_window_and_mdct(AVCodecContext *avctx, const AVFrame *frame)
     int window_len     = 1 << s->block_len_bits;
     float n            = 2.0 * 32768.0 / window_len;
 
-    for (ch = 0; ch < avctx->channels; ch++) {
+    for (ch = 0; ch < avctx->ch_layout.nb_channels; ch++) {
         memcpy(s->output, s->frame_out[ch], window_len * sizeof(*s->output));
         s->fdsp->vector_fmul_scalar(s->frame_out[ch], audio[ch], n, len);
         s->fdsp->vector_fmul_reverse(&s->output[window_len], s->frame_out[ch],
@@ -186,6 +186,7 @@ static void encode_exp_vlc(WMACodecContext *s, int ch, const int *exp_param)
 static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                         int total_gain)
 {
+    int channels = s->avctx->ch_layout.nb_channels;
     int v, bsize, ch, coef_nb_bits, parse_exponents;
     float mdct_norm;
     int nb_coefs[MAX_CHANNELS];
@@ -213,7 +214,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
 
     // FIXME factor
     v = s->coefs_end[bsize] - s->coefs_start;
-    for (ch = 0; ch < s->avctx->channels; ch++)
+    for (ch = 0; ch < channels; ch++)
         nb_coefs[ch] = v;
     {
         int n4 = s->block_len / 2;
@@ -222,17 +223,17 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
             mdct_norm *= sqrt(n4);
     }
 
-    if (s->avctx->channels == 2)
+    if (channels == 2)
         put_bits(&s->pb, 1, !!s->ms_stereo);
 
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         // FIXME only set channel_coded when needed, instead of always
         s->channel_coded[ch] = 1;
         if (s->channel_coded[ch])
             init_exp(s, ch, fixed_exp);
     }
 
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         if (s->channel_coded[ch]) {
             WMACoef *coefs1;
             float *coefs, *exponents, mult;
@@ -260,7 +261,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     }
 
     v = 0;
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         int a = s->channel_coded[ch];
         put_bits(&s->pb, 1, a);
         v |= a;
@@ -276,7 +277,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     coef_nb_bits = ff_wma_total_gain_to_bits(total_gain);
 
     if (s->use_noise_coding) {
-        for (ch = 0; ch < s->avctx->channels; ch++) {
+        for (ch = 0; ch < channels; ch++) {
             if (s->channel_coded[ch]) {
                 int i, n;
                 n = s->exponent_high_sizes[bsize];
@@ -294,7 +295,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
         put_bits(&s->pb, 1, parse_exponents);
 
     if (parse_exponents) {
-        for (ch = 0; ch < s->avctx->channels; ch++) {
+        for (ch = 0; ch < channels; ch++) {
             if (s->channel_coded[ch]) {
                 if (s->use_exp_vlc) {
                     encode_exp_vlc(s, ch, fixed_exp);
@@ -307,7 +308,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
     } else
         av_assert0(0); // FIXME not implemented
 
-    for (ch = 0; ch < s->avctx->channels; ch++) {
+    for (ch = 0; ch < channels; ch++) {
         if (s->channel_coded[ch]) {
             int run, tindex;
             WMACoef *ptr, *eptr;
@@ -346,7 +347,7 @@ static int encode_block(WMACodecContext *s, float (*src_coefs)[BLOCK_MAX_SIZE],
                 put_bits(&s->pb, s->coef_vlcs[tindex]->huffbits[1],
                          s->coef_vlcs[tindex]->huffcodes[1]);
         }
-        if (s->version == 1 && s->avctx->channels >= 2)
+        if (s->version == 1 && channels >= 2)
             align_put_bits(&s->pb);
     }
     return 0;
diff --git a/libavcodec/wmalosslessdec.c b/libavcodec/wmalosslessdec.c
index 74c91f4f7e..cd05b22689 100644
--- a/libavcodec/wmalosslessdec.c
+++ b/libavcodec/wmalosslessdec.c
@@ -190,14 +190,14 @@ static av_cold int decode_init(AVCodecContext *avctx)
         return AVERROR(EINVAL);
     }
 
-    av_assert0(avctx->channels >= 0);
-    if (avctx->channels > WMALL_MAX_CHANNELS) {
+    av_assert0(avctx->ch_layout.nb_channels >= 0);
+    if (avctx->ch_layout.nb_channels > WMALL_MAX_CHANNELS) {
         avpriv_request_sample(avctx,
                               "More than " AV_STRINGIFY(WMALL_MAX_CHANNELS) " channels");
         return AVERROR_PATCHWELCOME;
     }
 
-    s->max_frame_size = MAX_FRAMESIZE * avctx->channels;
+    s->max_frame_size = MAX_FRAMESIZE * avctx->ch_layout.nb_channels;
     s->frame_data = av_mallocz(s->max_frame_size + AV_INPUT_BUFFER_PADDING_SIZE);
     if (!s->frame_data)
         return AVERROR(ENOMEM);
@@ -244,7 +244,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
     av_assert0(s->samples_per_frame <= WMALL_BLOCK_MAX_SIZE);
 
     /* init previous block len */
-    for (i = 0; i < avctx->channels; i++)
+    for (i = 0; i < avctx->ch_layout.nb_channels; i++)
         s->channel[i].prev_block_len = s->samples_per_frame;
 
     /* subframe info */
@@ -263,7 +263,7 @@ static av_cold int decode_init(AVCodecContext *avctx)
         return AVERROR_INVALIDDATA;
     }
 
-    s->num_channels = avctx->channels;
+    s->num_channels = avctx->ch_layout.nb_channels;
 
     /* extract lfe channel position */
     s->lfe_channel = -1;
@@ -279,7 +279,8 @@ static av_cold int decode_init(AVCodecContext *avctx)
     if (!s->frame)
         return AVERROR(ENOMEM);
 
-    avctx->channel_layout = channel_mask;
+    av_channel_layout_uninit(&avctx->ch_layout);
+    av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
     return 0;
 }
 
diff --git a/libavcodec/wmaprodec.c b/libavcodec/wmaprodec.c
index ba7bddc51c..5c1d38eca5 100644
--- a/libavcodec/wmaprodec.c
+++ b/libavcodec/wmaprodec.c
@@ -384,7 +384,7 @@ static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int nu
         s->decode_flags    = 0x10d6;
         s->bits_per_sample = 16;
         channel_mask       = 0; //AV_RL32(edata_ptr+2); /* not always in expected order */
-        if ((num_stream+1) * XMA_MAX_CHANNELS_STREAM > avctx->channels) /* stream config is 2ch + 2ch + ... + 1/2ch */
+        if ((num_stream+1) * XMA_MAX_CHANNELS_STREAM > avctx->ch_layout.nb_channels) /* stream config is 2ch + 2ch + ... + 1/2ch */
             s->nb_channels = 1;
         else
             s->nb_channels = 2;
@@ -402,7 +402,7 @@ static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int nu
         s->decode_flags    = AV_RL16(edata_ptr+14);
         channel_mask       = AV_RL32(edata_ptr+2);
         s->bits_per_sample = AV_RL16(edata_ptr);
-        s->nb_channels     = avctx->channels;
+        s->nb_channels     = avctx->ch_layout.nb_channels;
 
         if (s->bits_per_sample > 32 || s->bits_per_sample < 1) {
             avpriv_request_sample(avctx, "bits per sample is %d", s->bits_per_sample);
@@ -474,7 +474,7 @@ static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int nu
         av_log(avctx, AV_LOG_ERROR, "invalid number of channels per XMA stream %d\n",
                s->nb_channels);
         return AVERROR_INVALIDDATA;
-    } else if (s->nb_channels > WMAPRO_MAX_CHANNELS || s->nb_channels > avctx->channels) {
+    } else if (s->nb_channels > WMAPRO_MAX_CHANNELS || s->nb_channels > avctx->ch_layout.nb_channels) {
         avpriv_request_sample(avctx,
                               "More than %d channels", WMAPRO_MAX_CHANNELS);
         return AVERROR_PATCHWELCOME;
@@ -575,8 +575,13 @@ static av_cold int decode_init(WMAProDecodeCtx *s, AVCodecContext *avctx, int nu
     if (avctx->debug & FF_DEBUG_BITSTREAM)
         dump_context(s);
 
-    if (avctx->codec_id == AV_CODEC_ID_WMAPRO)
-        avctx->channel_layout = channel_mask;
+    if (avctx->codec_id == AV_CODEC_ID_WMAPRO) {
+        if (channel_mask) {
+            av_channel_layout_uninit(&avctx->ch_layout);
+            av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
+        } else
+            avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
+    }
 
     ff_thread_once(&init_static_once, decode_init_static);
 
@@ -1775,7 +1780,7 @@ static int decode_packet(AVCodecContext *avctx, WMAProDecodeCtx *s,
         AVFrame *frame = data;
 
         if (s->trim_start < frame->nb_samples) {
-            for (int ch = 0; ch < frame->channels; ch++)
+            for (int ch = 0; ch < frame->ch_layout.nb_channels; ch++)
                 frame->extended_data[ch] += s->trim_start * 4;
 
             frame->nb_samples -= s->trim_start;
@@ -1952,13 +1957,18 @@ static av_cold int xma_decode_init(AVCodecContext *avctx)
     XMADecodeCtx *s = avctx->priv_data;
     int i, ret, start_channels = 0;
 
-    if (avctx->channels <= 0 || avctx->extradata_size == 0)
+    if (avctx->ch_layout.nb_channels <= 0 || avctx->extradata_size == 0)
         return AVERROR_INVALIDDATA;
 
     /* get stream config */
     if (avctx->codec_id == AV_CODEC_ID_XMA2 && avctx->extradata_size == 34) { /* XMA2WAVEFORMATEX */
+        unsigned int channel_mask = AV_RL32(avctx->extradata + 2);
+        if (channel_mask) {
+            av_channel_layout_uninit(&avctx->ch_layout);
+            av_channel_layout_from_mask(&avctx->ch_layout, channel_mask);
+        } else
+            avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC;
         s->num_streams = AV_RL16(avctx->extradata);
-        avctx->channel_layout = AV_RL32(avctx->extradata + 2);
     } else if (avctx->codec_id == AV_CODEC_ID_XMA2 && avctx->extradata_size >= 2) { /* XMA2WAVEFORMAT */
         s->num_streams = avctx->extradata[1];
         if (avctx->extradata_size != (32 + ((avctx->extradata[0]==3)?0:8) + 4*s->num_streams)) {
@@ -1979,7 +1989,7 @@ static av_cold int xma_decode_init(AVCodecContext *avctx)
     }
 
     /* encoder supports up to 64 streams / 64*2 channels (would have to alloc arrays) */
-    if (avctx->channels > XMA_MAX_CHANNELS || s->num_streams > XMA_MAX_STREAMS ||
+    if (avctx->ch_layout.nb_channels > XMA_MAX_CHANNELS || s->num_streams > XMA_MAX_STREAMS ||
         s->num_streams <= 0
     ) {
         avpriv_request_sample(avctx, "More than %d channels in %d streams", XMA_MAX_CHANNELS, s->num_streams);
@@ -1999,7 +2009,7 @@ static av_cold int xma_decode_init(AVCodecContext *avctx)
         s->start_channel[i] = start_channels;
         start_channels += s->xma[i].nb_channels;
     }
-    if (start_channels != avctx->channels)
+    if (start_channels != avctx->ch_layout.nb_channels)
         return AVERROR_INVALIDDATA;
 
     for (int i = 0; i < XMA_MAX_STREAMS; i++) {
diff --git a/libavcodec/wmavoice.c b/libavcodec/wmavoice.c
index e096b8029d..47fc0947c5 100644
--- a/libavcodec/wmavoice.c
+++ b/libavcodec/wmavoice.c
@@ -475,8 +475,8 @@ static av_cold int wmavoice_decode_init(AVCodecContext *ctx)
                                   2 * (s->block_conv_table[1] - 2 * s->min_pitch_val);
     s->block_pitch_nbits        = av_ceil_log2(s->block_pitch_range);
 
-    ctx->channels               = 1;
-    ctx->channel_layout         = AV_CH_LAYOUT_MONO;
+    av_channel_layout_uninit(&ctx->ch_layout);
+    ctx->ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
     ctx->sample_fmt             = AV_SAMPLE_FMT_FLT;
 
     return 0;
