[FFmpeg-devel] [PATCH] Add dvdsub workaround for some players

Oliver Fromme oliver at fromme.com
Wed Jul 9 18:43:58 CEST 2014


I mentioned this issue before, now here is a small patch for it.
The issue affects dvdsub subtitles (a.k.a. VOBSUB).

Some players -- in particular hardware players -- cut off
the lowest row of pixels if the number of rows in the subtitle
is odd.

The patch below implements a work-around for that.  If the
number of rows is odd, it is simply rounded up to an even
number, adding an invisible (i.e. fully transparent) row.
The work-around can be enabled or disabled with a new
option -even_rows_fix.  The default is disabled, so there
is no change of behaviour for users who don't care about it.

The overhead for the fix is low, and in many cases even zero:
For subtitles with an odd number of rows (i.e. in 50% of
cases on average), the size increases by two bytes because
a fully transparent row is encoded as 0x00 0x00.  However,
in the VOBSUB standard, all data packets are padded to 2KB
anyway, so in most cases the additional bytes just use some
part of the padding, so there is no overhead.  Only in the
rare case that the 2KB boundary is hit (0.1% chance), a full
2KB block is added.

Suggested commit message (if you think this is too concise,
you can also use what I've written above):

Implement work-around for some players having problems with
odd-sized dvdsub subtitles.  If enabled with -even_rows_fix 1,
the number of rows is rounded up to an even number.

Best regards
   Oliver

--- a/libavcodec/dvdsubenc.c	2014-05-20 19:20:06.000000000 +0200
+++ b/libavcodec/dvdsubenc.c	2014-07-09 17:30:50.000000000 +0200
@@ -24,9 +24,12 @@
 #include "libavutil/avassert.h"
 #include "libavutil/bprint.h"
 #include "libavutil/imgutils.h"
+#include "libavutil/opt.h"
 
 typedef struct {
+    AVClass *class;
     uint32_t global_palette[16];
+    int even_rows_fix;
 } DVDSubtitleContext;
 
 // ncnt is the nibble counter
@@ -345,6 +348,13 @@
     dvd_encode_rle(&q, vrect.pict.data[0] + vrect.w, vrect.w * 2,
                    vrect.w, vrect.h >> 1, cmap);
 
+    if (dvdc->even_rows_fix && (vrect.h & 1)) {
+        // Work-around for some players that want the height to be even.
+        vrect.h++;
+        *q++ = 0x00; // 0x00 0x00 == empty row, i.e. fully transparent
+        *q++ = 0x00;
+    }
+
     // set data packet size
     qq = outbuf + 2;
     bytestream_put_be16(&qq, q - outbuf);
@@ -439,6 +449,20 @@
     return ret;
 }
 
+#define OFFSET(x) offsetof(DVDSubtitleContext, x)
+#define SE AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+static const AVOption options[] = {
+    {"even_rows_fix", "Make number of rows even (workaround for some players)", OFFSET(even_rows_fix), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, SE},
+    { NULL },
+};
+
+static const AVClass dvdsubenc_class = {
+    .class_name = "VOBSUB subtitle encoder",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
 AVCodec ff_dvdsub_encoder = {
     .name           = "dvdsub",
     .long_name      = NULL_IF_CONFIG_SMALL("DVD subtitles"),
@@ -446,5 +470,6 @@
     .id             = AV_CODEC_ID_DVD_SUBTITLE,
     .init           = dvdsub_init,
     .encode_sub     = dvdsub_encode,
+    .priv_class     = &dvdsubenc_class,
     .priv_data_size = sizeof(DVDSubtitleContext),
 };


More information about the ffmpeg-devel mailing list