diff mbox series

[FFmpeg-devel,v3,1/2] ffmpeg: refactor post-decoding steps for subtitles into a function

Message ID 20220729133537.53269-1-jeebjp@gmail.com
State New
Headers show
Series [FFmpeg-devel,v3,1/2] ffmpeg: refactor post-decoding steps for subtitles into a function | expand

Checks

Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Jan Ekström July 29, 2022, 1:35 p.m. UTC
From: Jan Ekström <jan.ekstrom@24i.com>

This enables us to later call this when generating additional
subtitles for splitting purposes.

Co-authored-by: Andrzej Nadachowski <andrzej.nadachowski@24i.com>

Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
---
 fftools/ffmpeg.c | 50 ++++++++++++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 21 deletions(-)

Comments

Anton Khirnov Aug. 2, 2022, 11:03 a.m. UTC | #1
Quoting Jan Ekström (2022-07-29 15:35:36)
> From: Jan Ekström <jan.ekstrom@24i.com>
> 
> This enables us to later call this when generating additional
> subtitles for splitting purposes.
> 
> Co-authored-by: Andrzej Nadachowski <andrzej.nadachowski@24i.com>
> 
> Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
> ---
>  fftools/ffmpeg.c | 50 ++++++++++++++++++++++++++++--------------------
>  1 file changed, 29 insertions(+), 21 deletions(-)
> 
> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
> index 4746742c02..adcab43a30 100644
> --- a/fftools/ffmpeg.c
> +++ b/fftools/ffmpeg.c
> @@ -2301,27 +2301,16 @@ fail:
>      return err < 0 ? err : ret;
>  }
>  
> -static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
> -                               int *decode_failed)
> +static int encode_mux_subtitles(InputStream *ist, AVSubtitle *subtitle, int *got_output)
>  {
> -    AVSubtitle subtitle;
> +    int ret = 0;
>      int free_sub = 1;
> -    int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
> -                                          &subtitle, got_output, pkt);
>  
> -    check_decode_result(NULL, got_output, ret);
> -
> -    if (ret < 0 || !*got_output) {
> -        *decode_failed = 1;
> -        if (!pkt->size)
> -            sub2video_flush(ist);
> -        return ret;
> -    }
>  
>      if (ist->fix_sub_duration) {
>          int end = 1;
>          if (ist->prev_sub.got_output) {
> -            end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
> +            end = av_rescale(subtitle->pts - ist->prev_sub.subtitle.pts,
>                               1000, AV_TIME_BASE);
>              if (end < ist->prev_sub.subtitle.end_display_time) {
>                  av_log(NULL, AV_LOG_DEBUG,
> @@ -2333,7 +2322,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
>          }
>          FFSWAP(int,        *got_output, ist->prev_sub.got_output);
>          FFSWAP(int,        ret,         ist->prev_sub.ret);
> -        FFSWAP(AVSubtitle, subtitle,    ist->prev_sub.subtitle);
> +        FFSWAP(AVSubtitle, *subtitle,   ist->prev_sub.subtitle);
>          if (end <= 0)
>              goto out;
>      }
> @@ -2342,40 +2331,59 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
>          return ret;
>  
>      if (ist->sub2video.frame) {
> -        sub2video_update(ist, INT64_MIN, &subtitle);
> +        sub2video_update(ist, INT64_MIN, subtitle);
>      } else if (ist->nb_filters) {
>          if (!ist->sub2video.sub_queue)
>              ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
>          if (!ist->sub2video.sub_queue)
>              exit_program(1);
>  
> -        ret = av_fifo_write(ist->sub2video.sub_queue, &subtitle, 1);
> +        ret = av_fifo_write(ist->sub2video.sub_queue, subtitle, 1);
>          if (ret < 0)
>              exit_program(1);
>          free_sub = 0;
>      }
>  
> -    if (!subtitle.num_rects)
> +    if (!subtitle->num_rects)
>          goto out;
>  
>      ist->frames_decoded++;
>  
> -    for (i = 0; i < nb_output_streams; i++) {
> +    for (int i = 0; i < nb_output_streams; i++) {
>          OutputStream *ost = output_streams[i];
>  
>          if (!check_output_constraints(ist, ost) || !ost->encoding_needed
>              || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
>              continue;
>  
> -        do_subtitle_out(output_files[ost->file_index], ost, &subtitle);
> +        do_subtitle_out(output_files[ost->file_index], ost, subtitle);
>      }
>  
>  out:
>      if (free_sub)
> -        avsubtitle_free(&subtitle);
> +        avsubtitle_free(subtitle);
>      return ret;
>  }
>  
> +static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
> +                               int *decode_failed)
> +{
> +    AVSubtitle subtitle;
> +    int ret = avcodec_decode_subtitle2(ist->dec_ctx,
> +                                       &subtitle, got_output, pkt);
> +
> +    check_decode_result(NULL, got_output, ret);
> +
> +    if (ret < 0 || !*got_output) {
> +        *decode_failed = 1;
> +        if (!pkt->size)
> +            sub2video_flush(ist);
> +        return ret;
> +    }
> +
> +    return encode_mux_subtitles(ist, &subtitle, got_output);
> +}
> +
>  static int send_filter_eof(InputStream *ist)
>  {
>      int i, ret;
> -- 
> 2.37.1
> 
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
> 
> To unsubscribe, visit link above, or email
> ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
Anton Khirnov Aug. 2, 2022, 1:15 p.m. UTC | #2
Quoting Jan Ekström (2022-07-29 15:35:36)
> From: Jan Ekström <jan.ekstrom@24i.com>
> 
> This enables us to later call this when generating additional
> subtitles for splitting purposes.
> 
> Co-authored-by: Andrzej Nadachowski <andrzej.nadachowski@24i.com>
> 
> Signed-off-by: Jan Ekström <jan.ekstrom@24i.com>
> ---
>  fftools/ffmpeg.c | 50 ++++++++++++++++++++++++++++--------------------
>  1 file changed, 29 insertions(+), 21 deletions(-)
> 
> diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
> index 4746742c02..adcab43a30 100644
> --- a/fftools/ffmpeg.c
> +++ b/fftools/ffmpeg.c
> @@ -2301,27 +2301,16 @@ fail:
>      return err < 0 ? err : ret;
>  }
>  
> -static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
> -                               int *decode_failed)
> +static int encode_mux_subtitles(InputStream *ist, AVSubtitle *subtitle, int *got_output)

A function called 'encode_mux' should not be doing anything with an
input stream.

I also think this will spurisouly increment ist->frames_decoded after
the second patch.
diff mbox series

Patch

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 4746742c02..adcab43a30 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2301,27 +2301,16 @@  fail:
     return err < 0 ? err : ret;
 }
 
-static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
-                               int *decode_failed)
+static int encode_mux_subtitles(InputStream *ist, AVSubtitle *subtitle, int *got_output)
 {
-    AVSubtitle subtitle;
+    int ret = 0;
     int free_sub = 1;
-    int i, ret = avcodec_decode_subtitle2(ist->dec_ctx,
-                                          &subtitle, got_output, pkt);
 
-    check_decode_result(NULL, got_output, ret);
-
-    if (ret < 0 || !*got_output) {
-        *decode_failed = 1;
-        if (!pkt->size)
-            sub2video_flush(ist);
-        return ret;
-    }
 
     if (ist->fix_sub_duration) {
         int end = 1;
         if (ist->prev_sub.got_output) {
-            end = av_rescale(subtitle.pts - ist->prev_sub.subtitle.pts,
+            end = av_rescale(subtitle->pts - ist->prev_sub.subtitle.pts,
                              1000, AV_TIME_BASE);
             if (end < ist->prev_sub.subtitle.end_display_time) {
                 av_log(NULL, AV_LOG_DEBUG,
@@ -2333,7 +2322,7 @@  static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
         }
         FFSWAP(int,        *got_output, ist->prev_sub.got_output);
         FFSWAP(int,        ret,         ist->prev_sub.ret);
-        FFSWAP(AVSubtitle, subtitle,    ist->prev_sub.subtitle);
+        FFSWAP(AVSubtitle, *subtitle,   ist->prev_sub.subtitle);
         if (end <= 0)
             goto out;
     }
@@ -2342,40 +2331,59 @@  static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
         return ret;
 
     if (ist->sub2video.frame) {
-        sub2video_update(ist, INT64_MIN, &subtitle);
+        sub2video_update(ist, INT64_MIN, subtitle);
     } else if (ist->nb_filters) {
         if (!ist->sub2video.sub_queue)
             ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
         if (!ist->sub2video.sub_queue)
             exit_program(1);
 
-        ret = av_fifo_write(ist->sub2video.sub_queue, &subtitle, 1);
+        ret = av_fifo_write(ist->sub2video.sub_queue, subtitle, 1);
         if (ret < 0)
             exit_program(1);
         free_sub = 0;
     }
 
-    if (!subtitle.num_rects)
+    if (!subtitle->num_rects)
         goto out;
 
     ist->frames_decoded++;
 
-    for (i = 0; i < nb_output_streams; i++) {
+    for (int i = 0; i < nb_output_streams; i++) {
         OutputStream *ost = output_streams[i];
 
         if (!check_output_constraints(ist, ost) || !ost->encoding_needed
             || ost->enc->type != AVMEDIA_TYPE_SUBTITLE)
             continue;
 
-        do_subtitle_out(output_files[ost->file_index], ost, &subtitle);
+        do_subtitle_out(output_files[ost->file_index], ost, subtitle);
     }
 
 out:
     if (free_sub)
-        avsubtitle_free(&subtitle);
+        avsubtitle_free(subtitle);
     return ret;
 }
 
+static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
+                               int *decode_failed)
+{
+    AVSubtitle subtitle;
+    int ret = avcodec_decode_subtitle2(ist->dec_ctx,
+                                       &subtitle, got_output, pkt);
+
+    check_decode_result(NULL, got_output, ret);
+
+    if (ret < 0 || !*got_output) {
+        *decode_failed = 1;
+        if (!pkt->size)
+            sub2video_flush(ist);
+        return ret;
+    }
+
+    return encode_mux_subtitles(ist, &subtitle, got_output);
+}
+
 static int send_filter_eof(InputStream *ist)
 {
     int i, ret;