diff --git a/libavformat/options.c b/libavformat/options.c
index 25a506eef86a6fe1223ccf41f204f207e859a47b..9371c72667479066857d4a7d8b24ef207bfb63bc 100644
--- a/libavformat/options.c
+++ b/libavformat/options.c
@@ -102,6 +102,18 @@ static const AVClass av_format_context_class = {
 static int io_open_default(AVFormatContext *s, AVIOContext **pb,
                            const char *url, int flags, AVDictionary **options)
 {
+    int loglevel;
+
+    if (!strcmp(url, s->filename) ||
+        s->iformat && !strcmp(s->iformat->name, "image2") ||
+        s->oformat && !strcmp(s->oformat->name, "image2")
+    ) {
+        loglevel = AV_LOG_DEBUG;
+    } else
+        loglevel = AV_LOG_INFO;
+
+    av_log(s, loglevel, "Opening \'%s\' for %s\n", url, flags & AVIO_FLAG_WRITE ? "writing" : "reading");
+
 #if FF_API_OLD_OPEN_CALLBACKS
 FF_DISABLE_DEPRECATION_WARNINGS
     if (s->open_cb)
diff --git a/libavformat/utils.c b/libavformat/utils.c
index fbd8b58ac21b5e25687c2c04fe14d4929413223c..c5f1eac185daf504ad47bc0b01dbc5a1732c9fa1 100644
--- a/libavformat/utils.c
+++ b/libavformat/utils.c
@@ -533,6 +533,7 @@ int avformat_open_input(AVFormatContext **ps, const char *filename,
     if ((ret = av_opt_set_dict(s, &tmp)) < 0)
         goto fail;
 
+    av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
     if ((ret = init_input(s, filename, &tmp)) < 0)
         goto fail;
     s->probe_score = ret;
@@ -570,7 +571,6 @@ int avformat_open_input(AVFormatContext **ps, const char *filename,
     }
 
     s->duration = s->start_time = AV_NOPTS_VALUE;
-    av_strlcpy(s->filename, filename ? filename : "", sizeof(s->filename));
 
     /* Allocate private data. */
     if (s->iformat->priv_data_size > 0) {