diff --git a/libavformat/http.c b/libavformat/http.c
index 018d25c9d73f4338bf4bc1d99a4de7a6fea29913..1958a1b4d5d8877b99991b85e8f2f727befcc5e9 100644
--- a/libavformat/http.c
+++ b/libavformat/http.c
@@ -272,6 +272,23 @@ int ff_http_do_new_request(URLContext *h, const char *uri)
     return ret;
 }
 
+int ff_http_averror(int status_code, int default_averror)
+{
+    switch (status_code) {
+        case 400: return AVERROR_HTTP_BAD_REQUEST;
+        case 401: return AVERROR_HTTP_UNAUTHORIZED;
+        case 403: return AVERROR_HTTP_FORBIDDEN;
+        case 404: return AVERROR_HTTP_NOT_FOUND;
+        default: break;
+    }
+    if (status_code >= 400 && status_code <= 499)
+        return AVERROR_HTTP_OTHER_4XX;
+    else if (status_code >= 500)
+        return AVERROR_HTTP_SERVER_ERROR;
+    else
+        return default_averror;
+}
+
 static int http_open(URLContext *h, const char *uri, int flags,
                      AVDictionary **options)
 {
diff --git a/libavformat/http.h b/libavformat/http.h
index be8ae7f46015f061fda6281ab0b55e9eecb1115f..7d02713e31096df172c203d28ea95f35f474aefe 100644
--- a/libavformat/http.h
+++ b/libavformat/http.h
@@ -47,4 +47,6 @@ void ff_http_init_auth_state(URLContext *dest, const URLContext *src);
  */
 int ff_http_do_new_request(URLContext *h, const char *uri);
 
+int ff_http_averror(int status_code, int default_averror);
+
 #endif /* AVFORMAT_HTTP_H */