From 5a52b9265c71296f676be899f7f8ef893e12f7bb Mon Sep 17 00:00:00 2001
From: Sven Gothel <sgothel@jausoft.com>
Date: Fri, 12 Jan 2018 01:53:01 +0100
Subject: [PATCH] FFMPEG: Use avcodec_free_context(..) instead of deprecated
 leaking avcodec_close(..)

Hence rename hb_avcodec_close -> hb_avcodec_free_context and pass the required ptr-ptr.
avcodec_free_context(..) ensures releasing of all resources attached to the context.
---
 libhb/decavcodec.c      | 13 ++++---------
 libhb/decpgssub.c       |  2 +-
 libhb/encavcodec.c      |  9 +++++----
 libhb/encavcodecaudio.c |  6 +++---
 libhb/hb.c              |  6 ++----
 libhb/hbffmpeg.h        |  2 +-
 6 files changed, 16 insertions(+), 22 deletions(-)

diff --git a/libhb/decavcodec.c b/libhb/decavcodec.c
index f12e20df5..26430dce4 100644
--- a/libhb/decavcodec.c
+++ b/libhb/decavcodec.c
@@ -390,13 +390,12 @@ static void closePrivData( hb_work_private_t ** ppv )
             //if (!(pv->qsv.decode && pv->job != NULL && (pv->job->vcodec & HB_VCODEC_QSV_MASK)))
 #endif
             {
-                hb_avcodec_close(pv->context);
+                hb_avcodec_free_context(&pv->context);
             }
         }
         if ( pv->context )
         {
-            av_freep( &pv->context->extradata );
-            av_freep( &pv->context );
+            hb_avcodec_free_context(&pv->context);
         }
         hb_audio_resample_free(pv->resample);
 
@@ -821,9 +820,7 @@ static int decavcodecaBSInfo( hb_work_object_t *w, const hb_buffer_t *buf,
 
     if ( parser != NULL )
         av_parser_close( parser );
-    hb_avcodec_close( context );
-    av_freep( &context->extradata );
-    av_freep( &context );
+    hb_avcodec_free_context(&context);
     return result;
 }
 
@@ -2105,9 +2102,7 @@ static void decavcodecvFlush( hb_work_object_t *w )
         if ( pv->title->opaque_priv == NULL )
         {
             pv->video_codec_opened = 0;
-            hb_avcodec_close( pv->context );
-            av_freep( &pv->context->extradata );
-            av_freep( &pv->context );
+            hb_avcodec_free_context(&pv->context);
             if ( pv->parser )
             {
                 av_parser_close(pv->parser);
diff --git a/libhb/decpgssub.c b/libhb/decpgssub.c
index e4c22d206..acb0e1dde 100644
--- a/libhb/decpgssub.c
+++ b/libhb/decpgssub.c
@@ -502,7 +502,7 @@ static void decsubClose( hb_work_object_t * w )
 {
     hb_work_private_t * pv = w->private_data;
     avcodec_flush_buffers( pv->context );
-    avcodec_close( pv->context );
+    avcodec_free_context( &pv->context );
 }
 
 hb_work_object_t hb_decpgssub =
diff --git a/libhb/encavcodec.c b/libhb/encavcodec.c
index 01a1fc389..8484bba6a 100644
--- a/libhb/encavcodec.c
+++ b/libhb/encavcodec.c
@@ -399,12 +399,13 @@ void encavcodecClose( hb_work_object_t * w )
         return;
     }
     hb_chapter_queue_close(&pv->chapter_queue);
-    if( pv->context && pv->context->codec )
+    if( pv->context )
     {
         hb_deep_log( 2, "encavcodec: closing libavcodec" );
-        avcodec_flush_buffers( pv->context );
-        hb_avcodec_close( pv->context );
-        av_free( pv->context );
+        if( pv->context->codec ) {
+            avcodec_flush_buffers( pv->context );
+        }
+        hb_avcodec_free_context(&pv->context);
     }
     if( pv->file )
     {
diff --git a/libhb/encavcodecaudio.c b/libhb/encavcodecaudio.c
index 79f775a9d..d3c1fe5d1 100644
--- a/libhb/encavcodecaudio.c
+++ b/libhb/encavcodecaudio.c
@@ -308,10 +308,10 @@ static void encavcodecaClose(hb_work_object_t * w)
         {
             Finalize(w);
             hb_deep_log(2, "encavcodecaudio: closing libavcodec");
-            if (pv->context->codec != NULL)
+            if (pv->context->codec != NULL) {
                 avcodec_flush_buffers(pv->context);
-            hb_avcodec_close(pv->context);
-            av_free( pv->context );
+            }
+            hb_avcodec_free_context(&pv->context);
         }
 
         if (pv->output_buf != NULL)
diff --git a/libhb/hb.c b/libhb/hb.c
index b973645b0..a0d1b7291 100644
--- a/libhb/hb.c
+++ b/libhb/hb.c
@@ -140,11 +140,9 @@ int hb_avcodec_open(AVCodecContext *avctx, AVCodec *codec,
     return ret;
 }
 
-int hb_avcodec_close(AVCodecContext *avctx)
+void hb_avcodec_free_context(AVCodecContext **avctx)
 {
-    int ret;
-    ret = avcodec_close(avctx);
-    return ret;
+    avcodec_free_context(avctx);
 }
 
 
diff --git a/libhb/hbffmpeg.h b/libhb/hbffmpeg.h
index c27f1f3d0..890b62ffc 100644
--- a/libhb/hbffmpeg.h
+++ b/libhb/hbffmpeg.h
@@ -24,7 +24,7 @@
 
 void hb_avcodec_init(void);
 int  hb_avcodec_open(AVCodecContext *, AVCodec *, AVDictionary **, int);
-int  hb_avcodec_close(AVCodecContext *);
+void hb_avcodec_free_context(AVCodecContext **avctx);
 const char* const* hb_av_preset_get_names(int encoder);
 
 uint64_t hb_ff_mixdown_xlat(int hb_mixdown, int *downmix_mode);
-- 
2.40.0