[FFmpeg-devel] [PATCH 09/10] fftools/ffmpeg: move subtitle helpers to ffmpeg_dec, their only user

Anton Khirnov anton at khirnov.net
Wed Feb 14 20:24:34 EET 2024


---
 fftools/ffmpeg.c     | 120 -------------------------------------------
 fftools/ffmpeg.h     |   3 --
 fftools/ffmpeg_dec.c | 120 +++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 120 insertions(+), 123 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 035210a8e9..3ee7b26d26 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -640,126 +640,6 @@ static void print_report(int is_last_report, int64_t timer_start, int64_t cur_ti
     first_report = 0;
 }
 
-int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
-{
-    int ret = AVERROR_BUG;
-    AVSubtitle tmp = {
-        .format = src->format,
-        .start_display_time = src->start_display_time,
-        .end_display_time = src->end_display_time,
-        .num_rects = 0,
-        .rects = NULL,
-        .pts = src->pts
-    };
-
-    if (!src->num_rects)
-        goto success;
-
-    if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
-        return AVERROR(ENOMEM);
-
-    for (int i = 0; i < src->num_rects; i++) {
-        AVSubtitleRect *src_rect = src->rects[i];
-        AVSubtitleRect *dst_rect;
-
-        if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
-            ret = AVERROR(ENOMEM);
-            goto cleanup;
-        }
-
-        tmp.num_rects++;
-
-        dst_rect->type      = src_rect->type;
-        dst_rect->flags     = src_rect->flags;
-
-        dst_rect->x         = src_rect->x;
-        dst_rect->y         = src_rect->y;
-        dst_rect->w         = src_rect->w;
-        dst_rect->h         = src_rect->h;
-        dst_rect->nb_colors = src_rect->nb_colors;
-
-        if (src_rect->text)
-            if (!(dst_rect->text = av_strdup(src_rect->text))) {
-                ret = AVERROR(ENOMEM);
-                goto cleanup;
-            }
-
-        if (src_rect->ass)
-            if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
-                ret = AVERROR(ENOMEM);
-                goto cleanup;
-            }
-
-        for (int j = 0; j < 4; j++) {
-            // SUBTITLE_BITMAP images are special in the sense that they
-            // are like PAL8 images. first pointer to data, second to
-            // palette. This makes the size calculation match this.
-            size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
-                              AVPALETTE_SIZE :
-                              src_rect->h * src_rect->linesize[j];
-
-            if (!src_rect->data[j])
-                continue;
-
-            if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
-                ret = AVERROR(ENOMEM);
-                goto cleanup;
-            }
-            dst_rect->linesize[j] = src_rect->linesize[j];
-        }
-    }
-
-success:
-    *dst = tmp;
-
-    return 0;
-
-cleanup:
-    avsubtitle_free(&tmp);
-
-    return ret;
-}
-
-static void subtitle_free(void *opaque, uint8_t *data)
-{
-    AVSubtitle *sub = (AVSubtitle*)data;
-    avsubtitle_free(sub);
-    av_free(sub);
-}
-
-int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
-{
-    AVBufferRef *buf;
-    AVSubtitle *sub;
-    int ret;
-
-    if (copy) {
-        sub = av_mallocz(sizeof(*sub));
-        ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
-        if (ret < 0) {
-            av_freep(&sub);
-            return ret;
-        }
-    } else {
-        sub = av_memdup(subtitle, sizeof(*subtitle));
-        if (!sub)
-            return AVERROR(ENOMEM);
-        memset(subtitle, 0, sizeof(*subtitle));
-    }
-
-    buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
-                           subtitle_free, NULL, 0);
-    if (!buf) {
-        avsubtitle_free(sub);
-        av_freep(&sub);
-        return AVERROR(ENOMEM);
-    }
-
-    frame->buf[0] = buf;
-
-    return 0;
-}
-
 static void print_stream_maps(void)
 {
     av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 1fdc835723..295f470a62 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -709,9 +709,6 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost,
                             Scheduler *sch, unsigned sch_idx_enc);
 int init_complex_filtergraph(FilterGraph *fg);
 
-int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src);
-int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy);
-
 /**
  * Get our axiliary frame data attached to the frame, allocating it
  * if needed.
diff --git a/fftools/ffmpeg_dec.c b/fftools/ffmpeg_dec.c
index bf41a44f1f..1cfa15b943 100644
--- a/fftools/ffmpeg_dec.c
+++ b/fftools/ffmpeg_dec.c
@@ -371,6 +371,126 @@ static int video_frame_process(DecoderPriv *dp, AVFrame *frame)
     return 0;
 }
 
+static int copy_av_subtitle(AVSubtitle *dst, const AVSubtitle *src)
+{
+    int ret = AVERROR_BUG;
+    AVSubtitle tmp = {
+        .format = src->format,
+        .start_display_time = src->start_display_time,
+        .end_display_time = src->end_display_time,
+        .num_rects = 0,
+        .rects = NULL,
+        .pts = src->pts
+    };
+
+    if (!src->num_rects)
+        goto success;
+
+    if (!(tmp.rects = av_calloc(src->num_rects, sizeof(*tmp.rects))))
+        return AVERROR(ENOMEM);
+
+    for (int i = 0; i < src->num_rects; i++) {
+        AVSubtitleRect *src_rect = src->rects[i];
+        AVSubtitleRect *dst_rect;
+
+        if (!(dst_rect = tmp.rects[i] = av_mallocz(sizeof(*tmp.rects[0])))) {
+            ret = AVERROR(ENOMEM);
+            goto cleanup;
+        }
+
+        tmp.num_rects++;
+
+        dst_rect->type      = src_rect->type;
+        dst_rect->flags     = src_rect->flags;
+
+        dst_rect->x         = src_rect->x;
+        dst_rect->y         = src_rect->y;
+        dst_rect->w         = src_rect->w;
+        dst_rect->h         = src_rect->h;
+        dst_rect->nb_colors = src_rect->nb_colors;
+
+        if (src_rect->text)
+            if (!(dst_rect->text = av_strdup(src_rect->text))) {
+                ret = AVERROR(ENOMEM);
+                goto cleanup;
+            }
+
+        if (src_rect->ass)
+            if (!(dst_rect->ass = av_strdup(src_rect->ass))) {
+                ret = AVERROR(ENOMEM);
+                goto cleanup;
+            }
+
+        for (int j = 0; j < 4; j++) {
+            // SUBTITLE_BITMAP images are special in the sense that they
+            // are like PAL8 images. first pointer to data, second to
+            // palette. This makes the size calculation match this.
+            size_t buf_size = src_rect->type == SUBTITLE_BITMAP && j == 1 ?
+                              AVPALETTE_SIZE :
+                              src_rect->h * src_rect->linesize[j];
+
+            if (!src_rect->data[j])
+                continue;
+
+            if (!(dst_rect->data[j] = av_memdup(src_rect->data[j], buf_size))) {
+                ret = AVERROR(ENOMEM);
+                goto cleanup;
+            }
+            dst_rect->linesize[j] = src_rect->linesize[j];
+        }
+    }
+
+success:
+    *dst = tmp;
+
+    return 0;
+
+cleanup:
+    avsubtitle_free(&tmp);
+
+    return ret;
+}
+
+static void subtitle_free(void *opaque, uint8_t *data)
+{
+    AVSubtitle *sub = (AVSubtitle*)data;
+    avsubtitle_free(sub);
+    av_free(sub);
+}
+
+static int subtitle_wrap_frame(AVFrame *frame, AVSubtitle *subtitle, int copy)
+{
+    AVBufferRef *buf;
+    AVSubtitle *sub;
+    int ret;
+
+    if (copy) {
+        sub = av_mallocz(sizeof(*sub));
+        ret = sub ? copy_av_subtitle(sub, subtitle) : AVERROR(ENOMEM);
+        if (ret < 0) {
+            av_freep(&sub);
+            return ret;
+        }
+    } else {
+        sub = av_memdup(subtitle, sizeof(*subtitle));
+        if (!sub)
+            return AVERROR(ENOMEM);
+        memset(subtitle, 0, sizeof(*subtitle));
+    }
+
+    buf = av_buffer_create((uint8_t*)sub, sizeof(*sub),
+                           subtitle_free, NULL, 0);
+    if (!buf) {
+        avsubtitle_free(sub);
+        av_freep(&sub);
+        return AVERROR(ENOMEM);
+    }
+
+    frame->buf[0] = buf;
+
+    return 0;
+}
+
 static int process_subtitle(DecoderPriv *dp, AVFrame *frame)
 {
     const AVSubtitle *subtitle = (AVSubtitle*)frame->buf[0]->data;
-- 
2.42.0



More information about the ffmpeg-devel mailing list