diff --git a/libavcodec/utils.c b/libavcodec/utils.c
index e4ea1e5bf7..68441881de 100644
--- a/libavcodec/utils.c
+++ b/libavcodec/utils.c
@@ -584,6 +584,8 @@ int attribute_align_arg avcodec_open(AVCodecContext *avctx, AVCodec *codec)
                 ret = AVERROR(EINVAL);
                 goto free_and_end;
             }
+        } else if (avctx->channel_layout) {
+            avctx->channels = av_get_channel_layout_nb_channels(avctx->channel_layout);
         }
     }