diff --git a/libavformat/x11grab.c b/libavformat/x11grab.c
index e0e40184cbeec43598b7746b4f45d597e0a297b4..4b237f6c263f1e864ebddfa8e616fd887e572164 100644
--- a/libavformat/x11grab.c
+++ b/libavformat/x11grab.c
@@ -101,6 +101,11 @@ x11grab_read_header(AVFormatContext *s1, AVFormatParameters *ap)
         return AVERROR_IO;
     }
 
+    if (!ap->device) {
+        av_log(s1, AV_LOG_ERROR, "AVParameters don't specify any device. Use -vd.\n");
+        return AVERROR_IO;
+    }
+
     sscanf(ap->device, "x11:%d,%d", &x_off, &y_off);
     av_log(s1, AV_LOG_INFO, "device: %s -> x: %d y: %d width: %d height: %d\n", ap->device, x_off, y_off, ap->width, ap->height);