From 6a4c92b64b1dbb1fc218d4ef78bc002584d398cf Mon Sep 17 00:00:00 2001
From: Jernej Fijacko <jernej.fijacko@fora.si>
Date: Thu, 11 Jul 2019 16:39:07 +0200
Subject: [PATCH] Add support for Display Definition Segment to DVB Subtitle
 encoder

Signed-off-by: Jernej Fijacko <jernej.fijacko@fora.si>
---
 libavcodec/dvbsub.c | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/libavcodec/dvbsub.c b/libavcodec/dvbsub.c
index 8cce702a9e..c83856718e 100644
--- a/libavcodec/dvbsub.c
+++ b/libavcodec/dvbsub.c
@@ -247,7 +247,8 @@ static void dvb_encode_rle8(uint8_t **pq,
     *pq = q;
 }
 
-static int encode_dvb_subtitles(DVBSubtitleContext *s,
+static int encode_dvb_subtitles(AVCodecContext *avctx,
+                                DVBSubtitleContext *s,
                                 uint8_t *outbuf, const AVSubtitle *h)
 {
     uint8_t *q, *pseg_len;
@@ -261,6 +262,19 @@ static int encode_dvb_subtitles(DVBSubtitleContext *s,
     if (h->num_rects && !h->rects)
         return -1;
 
+    if (avctx->width > 0 && avctx->height > 0) {
+        /* display definition segment */
+        *q++ = 0x0f; /* sync_byte */
+        *q++ = 0x14; /* segment_type */
+        bytestream_put_be16(&q, page_id);
+        pseg_len = q;
+        q += 2; /* segment length */
+        *q++ = 0x20; /* dds version number & display window flag */
+        bytestream_put_be16(&q, avctx->width - 1); /* display width */
+        bytestream_put_be16(&q, avctx->height - 1); /* display height */
+        bytestream_put_be16(&pseg_len, q - pseg_len - 2);
+    }
+
     /* page composition segment */
 
     *q++ = 0x0f; /* sync_byte */
@@ -449,7 +463,7 @@ static int dvbsub_encode(AVCodecContext *avctx,
     DVBSubtitleContext *s = avctx->priv_data;
     int ret;
 
-    ret = encode_dvb_subtitles(s, buf, sub);
+    ret = encode_dvb_subtitles(avctx, s, buf, sub);
     return ret;
 }
 
-- 
2.21.0

