diff mbox series

[FFmpeg-devel,07/12] Close libass after using

Message ID 20220503161328.842587-8-traian.coza@gmail.com
State New
Headers show
Series I added text to bitmap subtitle conversion functionality! | expand

Checks

Context Check Description
yinshiyou/commit_msg_loongarch64 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 fail Make fate failed
andriy/commit_msg_x86 warning The first line of the commit message must start with a context terminated by a colon and a space, for example "lavu/opt: " or "doc: ".
andriy/make_x86 success Make finished
andriy/make_fate_x86 fail Make fate failed

Commit Message

Traian Coza May 3, 2022, 4:13 p.m. UTC
---
 fftools/ffmpeg.c            |  2 +-
 libavcodec/avcodec.c        |  9 +++++++++
 libavcodec/text_to_bitmap.c | 13 +++++++++++--
 libavcodec/text_to_bitmap.h |  8 +++++++-
 4 files changed, 28 insertions(+), 4 deletions(-)
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0ff9da7bf4..ae622492ee 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3239,7 +3239,7 @@  static int init_output_stream(OutputStream *ost, AVFrame *frame,
                 return AVERROR_INVALIDDATA;
             }
             if (input_props == AV_CODEC_PROP_TEXT_SUB && output_props == AV_CODEC_PROP_BITMAP_SUB)
-                init_ass_context(ist, ost);
+                init_ass_context(input_streams, output_streams, nb_input_streams, nb_output_streams, ost->source_index, ost->index);
 #else
             if (input_props && output_props && input_props != output_props) {
                 snprintf(error, error_len,
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index d11f035481..1e308f39c2 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -42,6 +42,10 @@ 
 #include "thread.h"
 #include "version.h"
 
+#if CONFIG_LIBASS
+#include "libavcodec/text_to_bitmap.h"
+#endif
+
 #include "libavutil/ffversion.h"
 const char av_codec_ffversion[] = "FFmpeg version " FFMPEG_VERSION;
 
@@ -534,6 +538,11 @@  av_cold int avcodec_close(AVCodecContext *avctx)
     } else if (av_codec_is_decoder(avctx->codec))
         av_freep(&avctx->subtitle_header);
 
+#if CONFIG_LIBASS
+    if (av_codec_is_decoder(avctx->codec) && avctx->ass_context != NULL)
+        free_ass_context(avctx->ass_context);
+#endif
+
     avctx->codec = NULL;
     avctx->active_thread_type = 0;
 
diff --git a/libavcodec/text_to_bitmap.c b/libavcodec/text_to_bitmap.c
index 273564cb67..c419e89c20 100644
--- a/libavcodec/text_to_bitmap.c
+++ b/libavcodec/text_to_bitmap.c
@@ -18,13 +18,22 @@  struct ASS_Context {
     ASSSplitContext *ass_split_context;
 };
 
-void init_ass_context(InputStream *ist, OutputStream *ost)
+void init_ass_context(
+        InputStream **input_streams,
+        OutputStream **output_streams,
+        int nb_input_streams,
+        int nb_output_streams,
+        int ist_i,
+        int ost_i)
 {
+    InputStream *ist = input_streams[ist_i];
+    OutputStream *ost = output_streams[ost_i];
     if (ist->dec_ctx->ass_context) return;
+
     ASS_Context *context = (ASS_Context *)malloc(sizeof(ASS_Context));
     context->library = ass_library_init();
     ass_set_extract_fonts(context->library, 1);
-    // TODO: ass_add_font(context->library, );
+    // TODO: ass_add_font(context->library, ...);
 
     // Try to get a height and width from somewhere
     int width = 0, height = 0;
diff --git a/libavcodec/text_to_bitmap.h b/libavcodec/text_to_bitmap.h
index 37d346ae0c..0c4689b039 100644
--- a/libavcodec/text_to_bitmap.h
+++ b/libavcodec/text_to_bitmap.h
@@ -11,7 +11,13 @@ 
 struct ASS_Context;
 typedef struct ASS_Context ASS_Context;
 
-void init_ass_context(InputStream *ist, OutputStream *ost);
+void init_ass_context(
+        InputStream **input_streams,
+        OutputStream **output_streams,
+        int nb_input_streams,
+        int nb_output_streams,
+        int ist_i,
+        int ost_i);
 void render_avsub_ass(ASS_Context *, AVSubtitle *);
 void free_ass_context(ASS_Context *context);