From: Debian Multimedia Maintainers <debian-multimedia@lists.debian.org>
Date: Tue, 13 Aug 2024 09:45:27 +0200
Subject: Fix FTBFS with FFmpeg-5.0

Origin: Debian
Forwarded: no
Last-Update: 2022-02-15

Last-Update: 2022-02-15
---
 plugins/ffmpeg/codec.c         | 32 +++++++++++++++-----------------
 plugins/ffmpeg/codecs.c        | 14 +++-----------
 plugins/ffmpeg/ffmpeg_common.c | 23 +++++++++++++++--------
 plugins/ffmpeg/ffmpeg_common.h |  1 +
 4 files changed, 34 insertions(+), 36 deletions(-)

diff --git a/plugins/ffmpeg/codec.c b/plugins/ffmpeg/codec.c
index 9a63bfb..5686154 100644
--- a/plugins/ffmpeg/codec.c
+++ b/plugins/ffmpeg/codec.c
@@ -59,8 +59,15 @@ static int find_encoder(bg_ffmpeg_codec_context_t * ctx)
     return 0;
     }
   
-  /* Set codec deftaults */
-  avcodec_get_context_defaults3(ctx->avctx, ctx->codec);
+  /* Set codec defaults */
+  avcodec_free_context(&ctx->avctx);
+  if(!(ctx->avctx = avcodec_alloc_context3 (ctx->codec)))
+    {
+    gavl_log(GAVL_LOG_ERROR, LOG_DOMAIN,
+           "Context for Codec %s could not be initialized",
+           bg_ffmpeg_get_codec_name(ctx->id));
+    return 0;
+    }
   return 1;
   }
 
@@ -86,12 +93,13 @@ bg_ffmpeg_codec_context_t * bg_ffmpeg_codec_create(int type,
   ret->type = type;
   
   /* Create private codec context */
-  ret->avctx_priv = avcodec_alloc_context3(NULL);
-  ret->avctx = ret->avctx_priv;
+  ret->avctx_priv = NULL;
+  ret->avctx = NULL;
   
   if(!find_encoder(ret))
     {
-    av_free(ret->avctx_priv);
+    if(ret->avctx)
+      avcodec_free_context(&ret->avctx);
     free(ret);
     return NULL;
     }
@@ -826,20 +834,10 @@ void bg_ffmpeg_codec_destroy(bg_ffmpeg_codec_context_t * ctx)
   if(!(ctx->flags & FLAG_FLUSHED))
     bg_ffmpeg_codec_flush(ctx);
   
-  /* Close */
-
-  if(ctx->avctx->stats_in)
-    {
-    free(ctx->avctx->stats_in);
-    ctx->avctx->stats_in = NULL;
-    }
-//  if(ctx->flags & FLAG_INITIALIZED)
-    avcodec_close(ctx->avctx);
-  
   /* Destroy */
 
-  if(ctx->avctx_priv)
-    av_free(ctx->avctx_priv);
+  if(ctx->avctx)
+    avcodec_free_context(&ctx->avctx);
   
   if(ctx->pc)
     bg_encoder_pts_cache_destroy(ctx->pc);
diff --git a/plugins/ffmpeg/codecs.c b/plugins/ffmpeg/codecs.c
index 3f5c0e0..28547a5 100644
--- a/plugins/ffmpeg/codecs.c
+++ b/plugins/ffmpeg/codecs.c
@@ -26,6 +26,8 @@
 #include <gmerlin/utils.h>
 #include <gmerlin/translation.h>
 #include <gmerlin/log.h>
+#include <libavutil/channel_layout.h>
+#include <libavutil/opt.h>
 
 #define LOG_DOMAIN "ffmpeg.codecs"
 
@@ -1110,13 +1112,6 @@ typedef struct
     }                                           \
   }
 
-static const enum_t prediction_method[] =
-  {
-    { "Left",   FF_PRED_LEFT },
-    { "Plane",  FF_PRED_PLANE },
-    { "Median", FF_PRED_MEDIAN }
-  };
-
 static const enum_t compare_func[] =
   {
     { "SAD",  FF_CMP_SAD },
@@ -1283,10 +1278,7 @@ bg_ffmpeg_set_codec_parameter(AVCodecContext * ctx,
   
   if(!strcmp(name, "tga_rle"))
     {
-    if(val->v.i)
-      ctx->coder_type = FF_CODER_TYPE_RLE;
-    else
-      ctx->coder_type = FF_CODER_TYPE_RAW;
+    av_opt_set_int(ctx->priv_data, "rle", !!(val->v.i), 0);
     }
   
   PARAM_DICT_STRING("libvpx_deadline", "deadline");
diff --git a/plugins/ffmpeg/ffmpeg_common.c b/plugins/ffmpeg/ffmpeg_common.c
index 6bb4181..e9365a6 100644
--- a/plugins/ffmpeg/ffmpeg_common.c
+++ b/plugins/ffmpeg/ffmpeg_common.c
@@ -242,6 +242,17 @@ static void set_chapters(AVFormatContext * ctx,
 #endif
   }
 
+static char*ffmpeg_string(char*instr)
+{
+   size_t len = strlen(instr);
+   char*outstr = av_malloc(len+1);
+   if(outstr) {
+      strcpy(outstr, instr);
+      outstr[len] = 0;
+   }
+   return outstr;
+}
+
 static int ffmpeg_open(void * data, const char * filename,
                        gavf_io_t * io,
                        const gavl_dictionary_t * metadata)
@@ -270,8 +281,7 @@ static int ffmpeg_open(void * data, const char * filename,
                priv->format->name);
         return 0;
         }
-      strncpy(priv->ctx->filename,
-              "pipe:", sizeof(priv->ctx->filename));
+      priv->ctx->url = ffmpeg_string("pipe:");
       }
     else
       {
@@ -284,10 +294,7 @@ static int ffmpeg_open(void * data, const char * filename,
         free(tmp_string);
         return 0;
         }
-  
-      strncpy(priv->ctx->filename,
-              tmp_string, sizeof(priv->ctx->filename));
-    
+      priv->ctx->url = ffmpeg_string(tmp_string);
       free(tmp_string);
       }
     }
@@ -820,7 +827,7 @@ int bg_ffmpeg_start(void * data)
                                        io_write,
                                        gavf_io_can_seek(priv->io) ? io_seek : NULL);
     }
-  else if(avio_open(&priv->ctx->pb, priv->ctx->filename, AVIO_FLAG_WRITE) < 0)
+  else if(avio_open(&priv->ctx->pb, priv->ctx->url, AVIO_FLAG_WRITE) < 0)
     return 0;
   
 #if LIBAVFORMAT_VERSION_MAJOR < 54
@@ -956,7 +963,7 @@ int bg_ffmpeg_close(void * data, int do_delete)
     }
   
   if(do_delete && !priv->io)
-    remove(priv->ctx->filename);
+    remove(priv->ctx->url);
 
   if(priv->io_buffer)
     av_free(priv->io_buffer);
diff --git a/plugins/ffmpeg/ffmpeg_common.h b/plugins/ffmpeg/ffmpeg_common.h
index 0fe7bba..718006c 100644
--- a/plugins/ffmpeg/ffmpeg_common.h
+++ b/plugins/ffmpeg/ffmpeg_common.h
@@ -22,6 +22,7 @@
 #include <config.h>
 
 #include <libavformat/avformat.h>
+#include <libavcodec/avcodec.h>
 #include <gmerlin/plugin.h>
 #include <gmerlin/pluginfuncs.h>
 
