diff --git a/configure b/configure
index ae0eddac6c..27bed3a20d 100755
--- a/configure
+++ b/configure
@@ -1934,6 +1934,7 @@ HEADERS_LIST="
     openjpeg_2_1_openjpeg_h
     openjpeg_2_0_openjpeg_h
     openjpeg_1_5_openjpeg_h
+    opj_config_h
     OpenGL_gl3_h
     poll_h
     soundcard_h
@@ -5972,6 +5973,7 @@ enabled libopenjpeg       && { { check_lib libopenjpeg openjpeg-2.2/openjpeg.h o
                                { check_lib libopenjpeg openjpeg-2.0/openjpeg.h opj_version -lopenjp2 -DOPJ_STATIC && add_cppflags -DOPJ_STATIC; } ||
                                { check_lib libopenjpeg openjpeg-1.5/openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC && add_cppflags -DOPJ_STATIC; } ||
                                { check_lib libopenjpeg openjpeg.h opj_version -lopenjpeg -DOPJ_STATIC && add_cppflags -DOPJ_STATIC; } ||
+                               { use_pkg_config libopenjpeg libopenjp2 openjpeg.h opj_version && check_header opj_config.h; } ||
                                die "ERROR: libopenjpeg not found"; }
 enabled libopenmpt        && require_pkg_config libopenmpt "libopenmpt >= 0.2.6557" libopenmpt/libopenmpt.h openmpt_module_create
 enabled libopus           && {
diff --git a/libavcodec/libopenjpegdec.c b/libavcodec/libopenjpegdec.c
index 1210123265..822140c181 100644
--- a/libavcodec/libopenjpegdec.c
+++ b/libavcodec/libopenjpegdec.c
@@ -36,18 +36,37 @@
 
 #if HAVE_OPENJPEG_2_2_OPENJPEG_H
 #  include <openjpeg-2.2/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(2, 2, 0)
 #elif HAVE_OPENJPEG_2_1_OPENJPEG_H
 #  include <openjpeg-2.1/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(2, 1, 0)
 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
 #  include <openjpeg-2.0/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(2, 0, 0)
 #elif HAVE_OPENJPEG_1_5_OPENJPEG_H
 #  include <openjpeg-1.5/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(1, 5, 0)
 #else
 #  include <openjpeg.h>
 #endif
 
-#if HAVE_OPENJPEG_2_2_OPENJPEG_H || HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H
-#  define OPENJPEG_MAJOR_VERSION 2
+#if HAVE_OPJ_CONFIG_H
+#  include <opj_config.h>
+#  ifndef OPJ_VERSION_INT
+#    define OPJ_VERSION_INT AV_VERSION_INT(OPJ_VERSION_MAJOR, OPJ_VERSION_MINOR, OPJ_VERSION_BUILD)
+#  endif
+#endif
+
+#ifndef OPJ_VERSION_INT
+#  define OPJ_VERSION_INT AV_VERSION_INT(1, 0, 0)
+#endif
+
+#ifndef OPJ_VERSION_MAJOR
+#  define AV_VERSION_MAJOR AV_VERSION_MAJOR(OPJ_VERSION_INT)
+#endif
+
+#if OPJ_VERSION_MAJOR >= 2
+#  define OPENJPEG_MAJOR_VERSION OPJ_VERSION_MAJOR
 #  define OPJ(x) OPJ_##x
 #else
 #  define OPENJPEG_MAJOR_VERSION 1
@@ -431,12 +450,10 @@ static int libopenjpeg_decode_frame(AVCodecContext *avctx,
     opj_stream_set_read_function(stream, stream_read);
     opj_stream_set_skip_function(stream, stream_skip);
     opj_stream_set_seek_function(stream, stream_seek);
-#if HAVE_OPENJPEG_2_2_OPENJPEG_H || HAVE_OPENJPEG_2_1_OPENJPEG_H
+#if OPJ_VERSION_INT >= AV_VERSION_INT(2, 1, 0)
     opj_stream_set_user_data(stream, &reader, NULL);
-#elif HAVE_OPENJPEG_2_0_OPENJPEG_H
-    opj_stream_set_user_data(stream, &reader);
 #else
-#error Missing call to opj_stream_set_user_data
+    opj_stream_set_user_data(stream, &reader);
 #endif
     opj_stream_set_user_data_length(stream, avpkt->size);
     // Decode the header only.
diff --git a/libavcodec/libopenjpegenc.c b/libavcodec/libopenjpegenc.c
index b67e533d1d..6c2e4c5ea6 100644
--- a/libavcodec/libopenjpegenc.c
+++ b/libavcodec/libopenjpegenc.c
@@ -34,18 +34,37 @@
 
 #if HAVE_OPENJPEG_2_2_OPENJPEG_H
 #  include <openjpeg-2.2/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(2, 2, 0)
 #elif HAVE_OPENJPEG_2_1_OPENJPEG_H
 #  include <openjpeg-2.1/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(2, 1, 0)
 #elif HAVE_OPENJPEG_2_0_OPENJPEG_H
 #  include <openjpeg-2.0/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(2, 0, 0)
 #elif HAVE_OPENJPEG_1_5_OPENJPEG_H
 #  include <openjpeg-1.5/openjpeg.h>
+#  define OPJ_VERSION_INT AV_VERSION_INT(1, 5, 0)
 #else
 #  include <openjpeg.h>
 #endif
 
-#if HAVE_OPENJPEG_2_2_OPENJPEG_H || HAVE_OPENJPEG_2_1_OPENJPEG_H || HAVE_OPENJPEG_2_0_OPENJPEG_H
-#  define OPENJPEG_MAJOR_VERSION 2
+#if HAVE_OPJ_CONFIG_H
+#  include <opj_config.h>
+#  ifndef OPJ_VERSION_INT
+#    define OPJ_VERSION_INT AV_VERSION_INT(OPJ_VERSION_MAJOR, OPJ_VERSION_MINOR, OPJ_VERSION_BUILD)
+#  endif
+#endif
+
+#ifndef OPJ_VERSION_INT
+#  define OPJ_VERSION_INT AV_VERSION_INT(1, 0, 0)
+#endif
+
+#ifndef OPJ_VERSION_MAJOR
+#  define AV_VERSION_MAJOR AV_VERSION_MAJOR(OPJ_VERSION_INT)
+#endif
+
+#if OPJ_VERSION_MAJOR >= 2
+#  define OPENJPEG_MAJOR_VERSION OPJ_VERSION_MAJOR
 #  define OPJ(x) OPJ_##x
 #else
 #  define OPENJPEG_MAJOR_VERSION 1
@@ -771,12 +790,10 @@ static int libopenjpeg_encode_frame(AVCodecContext *avctx, AVPacket *pkt,
     opj_stream_set_write_function(stream, stream_write);
     opj_stream_set_skip_function(stream, stream_skip);
     opj_stream_set_seek_function(stream, stream_seek);
-#if HAVE_OPENJPEG_2_2_OPENJPEG_H || HAVE_OPENJPEG_2_1_OPENJPEG_H
+#if OPJ_VERSION_INT >= AV_VERSION_INT(2, 1, 0)
     opj_stream_set_user_data(stream, &writer, NULL);
-#elif HAVE_OPENJPEG_2_0_OPENJPEG_H
-    opj_stream_set_user_data(stream, &writer);
 #else
-#error Missing call to opj_stream_set_user_data
+    opj_stream_set_user_data(stream, &writer);
 #endif
 
     if (!opj_start_compress(compress, image, stream) ||
