diff --git a/libavcodec/vaapi_encode.c b/libavcodec/vaapi_encode.c
index 590f4be4ed..74bb02dc29 100644
--- a/libavcodec/vaapi_encode.c
+++ b/libavcodec/vaapi_encode.c
@@ -108,6 +108,7 @@ static int vaapi_encode_wait(AVCodecContext *avctx,
 {
     VAAPIEncodeContext *ctx = avctx->priv_data;
     VAStatus vas;
+    int i;
 
     av_assert0(pic->encode_issued);
 
@@ -131,6 +132,9 @@ static int vaapi_encode_wait(AVCodecContext *avctx,
     av_frame_free(&pic->input_image);
 
     pic->encode_complete = 1;
+    for (i = 0; i < pic->nb_refs; i++) {
+        pic->refs[i]->ref_count --;
+    }
     return 0;
 }
 
@@ -448,8 +452,8 @@ fail_at_end:
     return err;
 }
 
-static int vaapi_encode_output(AVCodecContext *avctx,
-                               VAAPIEncodePicture *pic, AVPacket *pkt)
+int vaapi_encode_output(AVCodecContext *avctx,
+		        VAAPIEncodePicture *pic, AVPacket *pkt)
 {
     VAAPIEncodeContext *ctx = avctx->priv_data;
     VACodedBufferSegment *buf_list, *buf;
@@ -526,7 +530,7 @@ static int vaapi_encode_discard(AVCodecContext *avctx,
     return 0;
 }
 
-static VAAPIEncodePicture *vaapi_encode_alloc(void)
+VAAPIEncodePicture *vaapi_encode_alloc(void)
 {
     VAAPIEncodePicture *pic;
 
@@ -541,8 +545,8 @@ static VAAPIEncodePicture *vaapi_encode_alloc(void)
     return pic;
 }
 
-static int vaapi_encode_free(AVCodecContext *avctx,
-                             VAAPIEncodePicture *pic)
+int vaapi_encode_free(AVCodecContext *avctx,
+                      VAAPIEncodePicture *pic)
 {
     int i;
 
@@ -573,8 +577,8 @@ static int vaapi_encode_free(AVCodecContext *avctx,
     return 0;
 }
 
-static int vaapi_encode_step(AVCodecContext *avctx,
-                             VAAPIEncodePicture *target)
+int vaapi_encode_step(AVCodecContext *avctx,
+                      VAAPIEncodePicture *target)
 {
     VAAPIEncodeContext *ctx = avctx->priv_data;
     VAAPIEncodePicture *pic;
@@ -1097,6 +1101,8 @@ static av_cold int vaapi_encode_config_attributes(AVCodecContext *avctx)
                 err = AVERROR(EINVAL);
                 goto fail;
             }
+	    ctx->max_ref_l0 = ref_l0;
+	    ctx->max_ref_l1 = ref_l1;
         }
         break;
         case VAConfigAttribEncPackedHeaders:
@@ -1340,6 +1346,7 @@ static av_cold int vaapi_encode_create_recon_frames(AVCodecContext *avctx)
     // At most three IDR/I/P frames and two runs of B frames can be in
     // flight at any one time.
     ctx->recon_frames->initial_pool_size = 3 + 2 * avctx->max_b_frames;
+    ctx->recon_frames->initial_pool_size += ctx->max_ref_l0 + ctx->max_ref_l1;
 
     err = av_hwframe_ctx_init(ctx->recon_frames_ref);
     if (err < 0) {
diff --git a/libavcodec/vaapi_encode.h b/libavcodec/vaapi_encode.h
index bcb9d57371..53950cc0a1 100644
--- a/libavcodec/vaapi_encode.h
+++ b/libavcodec/vaapi_encode.h
@@ -34,7 +34,7 @@ struct VAAPIEncodePicture;
 enum {
     MAX_CONFIG_ATTRIBUTES  = 4,
     MAX_GLOBAL_PARAMS      = 4,
-    MAX_PICTURE_REFERENCES = 2,
+    MAX_PICTURE_REFERENCES = 12,
     MAX_REORDER_DELAY      = 16,
     MAX_PARAM_BUFFER_SIZE  = 1024,
 };
@@ -79,9 +79,12 @@ typedef struct VAAPIEncodePicture {
     void           *priv_data;
     void           *codec_picture_params;
 
+    int          ref_count;
+
     int          nb_refs;
     struct VAAPIEncodePicture *refs[MAX_PICTURE_REFERENCES];
 
+
     int          nb_slices;
     VAAPIEncodeSlice *slices;
 } VAAPIEncodePicture;
@@ -206,6 +209,10 @@ typedef struct VAAPIEncodeContext {
     int p_counter;
     int end_of_stream;
 
+    // max reference frame number
+    int max_ref_l0;
+    int max_ref_l1;
+
     // Codec-local options are allocated to follow this structure in
     // memory (in the AVCodec definition, set priv_data_size to
     // sizeof(VAAPIEncodeContext) + sizeof(VAAPIEncodeFooOptions)).
@@ -279,5 +286,15 @@ int ff_vaapi_encode2(AVCodecContext *avctx, AVPacket *pkt,
 
 int ff_vaapi_encode_init(AVCodecContext *avctx);
 int ff_vaapi_encode_close(AVCodecContext *avctx);
+VAAPIEncodePicture *vaapi_encode_alloc(void);
+int vaapi_encode_free(AVCodecContext *avctx,
+                      VAAPIEncodePicture *pic);
+
+int vaapi_encode_step(AVCodecContext *avctx,
+                      VAAPIEncodePicture *target);
+int vaapi_encode_output(AVCodecContext *avctx,
+                        VAAPIEncodePicture *pic, AVPacket *pkt);
+
+
 
 #endif /* AVCODEC_VAAPI_ENCODE_H */
