[FFmpeg-devel] [PATCH 2/2] ffmpeg: add an option to fix subtitles durations.

Nicolas George nicolas.george at normalesup.org
Thu Jul 26 00:30:09 CEST 2012


With this option, transcoding DVB subtitles becomes possible.

Signed-off-by: Nicolas George <nicolas.george at normalesup.org>
---
 doc/ffmpeg.texi |   15 +++++++++++++++
 ffmpeg.c        |   36 ++++++++++++++++++++++++++++++++++--
 2 files changed, 49 insertions(+), 2 deletions(-)


Note: transcoding to DVD subtitles and muxing to VOB or Matroska works,
except that the dvdsub encoder produces wrong colors, but that is another
story.


diff --git a/doc/ffmpeg.texi b/doc/ffmpeg.texi
index 904a505..8a8ebc3 100644
--- a/doc/ffmpeg.texi
+++ b/doc/ffmpeg.texi
@@ -620,6 +620,21 @@ Disable subtitle recording.
 Deprecated, see -bsf
 @end table
 
+ at section Advanced Subtitle options:
+
+ at table @option
+
+ at item -fix_sub_duration
+Fix subtitles durations. For each subtitle, wait for the next packet in the
+same stream and adjust the duration of the first to avoid overlap. This is
+necessary with some subtitles codecs, especially DVB subtitles, because the
+duration in the original packet is only a rough estimate and the end is
+actually marked by an empty subtitle frame. Failing to use this option when
+necessary can result in exaggerated durations or muxing failures due to
+non-monotonic timestamps.
+
+ at end table
+
 @section Audio/Video grab options
 
 @table @option
diff --git a/ffmpeg.c b/ffmpeg.c
index 99a2433..1f7be8a 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -249,6 +249,14 @@ typedef struct InputStream {
     int      resample_channels;
     uint64_t resample_channel_layout;
 
+    int fix_sub_duration;
+    struct { /* previous decoded subtitle and related varialbes */
+        int64_t pts;
+        int got_output;
+        int ret;
+        AVSubtitle subtitle;
+    } prev_sub;
+
     /* a pool of free buffers for decoded data */
     FrameBuffer *buffer_pool;
     int dr1;
@@ -452,6 +460,8 @@ typedef struct OptionsContext {
     int        nb_copy_initial_nonkeyframes;
     SpecifierOpt *filters;
     int        nb_filters;
+    SpecifierOpt *fix_sub_duration;
+    int        nb_fix_sub_duration;
 } OptionsContext;
 
 static void do_video_stats(AVFormatContext *os, OutputStream *ost, int frame_size);
@@ -2625,11 +2635,30 @@ static int decode_video(InputStream *ist, AVPacket *pkt, int *got_output)
 static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
 {
     AVSubtitle subtitle;
+    int64_t pts = pkt->pts;
     int i, ret = avcodec_decode_subtitle2(ist->st->codec,
                                           &subtitle, got_output, pkt);
     if (ret < 0)
         return ret;
-    if (!*got_output)
+
+    if (ist->fix_sub_duration) {
+        if (ist->prev_sub.got_output) {
+            int end = av_rescale_q(pts - ist->prev_sub.pts, ist->st->time_base,
+                                   (AVRational){ 1, 1000 });
+            if (end < ist->prev_sub.subtitle.end_display_time) {
+                av_log(ist->st->codec, AV_LOG_DEBUG,
+                       "Subtitle duration reduced from %d to %d\n",
+                       ist->prev_sub.subtitle.end_display_time, end);
+                ist->prev_sub.subtitle.end_display_time = end;
+            }
+        }
+        FFSWAP(int64_t,    pts,         ist->prev_sub.pts);
+        FFSWAP(int,        *got_output, ist->prev_sub.got_output);
+        FFSWAP(int,        ret,         ist->prev_sub.ret);
+        FFSWAP(AVSubtitle, subtitle,    ist->prev_sub.subtitle);
+    }
+
+    if (!*got_output || !subtitle.num_rects)
         return ret;
 
     rate_emu_sleep(ist);
@@ -2640,7 +2669,7 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output)
         if (!check_output_constraints(ist, ost) || !ost->encoding_needed)
             continue;
 
-        do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pkt->pts);
+        do_subtitle_out(output_files[ost->file_index]->ctx, ost, ist, &subtitle, pts);
     }
 
     avsubtitle_free(&subtitle);
@@ -4336,6 +4365,8 @@ static void add_input_streams(OptionsContext *o, AVFormatContext *ic)
         case AVMEDIA_TYPE_SUBTITLE:
             if(!ist->dec)
                 ist->dec = avcodec_find_decoder(dec->codec_id);
+            MATCH_PER_STREAM_OPT(fix_sub_duration, i, ist->fix_sub_duration, ic, st);
+
             break;
         case AVMEDIA_TYPE_ATTACHMENT:
         case AVMEDIA_TYPE_UNKNOWN:
@@ -6032,6 +6063,7 @@ static const OptionDef options[] = {
     { "sn", OPT_BOOL | OPT_SUBTITLE | OPT_OFFSET, {.off = OFFSET(subtitle_disable)}, "disable subtitle" },
     { "scodec", HAS_ARG | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_subtitle_codec}, "force subtitle codec ('copy' to copy stream)", "codec" },
     { "stag", HAS_ARG | OPT_EXPERT | OPT_SUBTITLE | OPT_FUNC2, {(void*)opt_old2new}, "force subtitle tag/fourcc", "fourcc/tag" },
+    { "fix_sub_duration", OPT_BOOL | OPT_EXPERT | OPT_SUBTITLE | OPT_SPEC, {.off = OFFSET(fix_sub_duration)}, "fix subtitles duration" },
 
     /* grab options */
     { "vc", HAS_ARG | OPT_EXPERT | OPT_VIDEO | OPT_GRAB, {(void*)opt_video_channel}, "deprecated, use -channel", "channel" },
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list