diff --git a/libavcodec/qtrleenc.c b/libavcodec/qtrleenc.c
index 8750e3097a7d4ef13967c273eeac3d4e06a58d11..d723188413aa16a3d10586dd6e3c6eacb61ee6f9 100644
--- a/libavcodec/qtrleenc.c
+++ b/libavcodec/qtrleenc.c
@@ -86,6 +86,10 @@ static av_cold int qtrle_encode_init(AVCodecContext *avctx)
 
     switch (avctx->pix_fmt) {
     case AV_PIX_FMT_GRAY8:
+        if (avctx->width % 4) {
+            av_log(avctx, AV_LOG_ERROR, "Width not being a multiple of 4 is not supported\n");
+            return AVERROR(EINVAL);
+        }
         s->logical_width = avctx->width / 4;
         s->pixel_size = 4;
         break;