From 6261cbcd71e5cd192623434fa698e9f1e701f365 Mon Sep 17 00:00:00 2001
From: Paul Kendall <paul@kcbbs.gen.nz>
Date: Sun, 29 Jan 2012 14:50:19 +0100
Subject: [PATCH 1/1] Fix dvb subtitle decoding when display segment is
 missing.

closes ticket #8504
---
 libavcodec/dvbsubdec.c |   11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/libavcodec/dvbsubdec.c b/libavcodec/dvbsubdec.c
index e2a804a..503223f 100644
--- a/libavcodec/dvbsubdec.c
+++ b/libavcodec/dvbsubdec.c
@@ -1451,6 +1451,7 @@ static int dvbsub_decode(AVCodecContext *avctx,
     int segment_type;
     int page_id;
     int segment_length;
+    int got_segment = 0;
     int i;
 
     av_dlog(avctx, "DVB sub packet:\n");
@@ -1490,21 +1491,26 @@ static int dvbsub_decode(AVCodecContext *avctx,
             switch (segment_type) {
             case DVBSUB_PAGE_SEGMENT:
                 dvbsub_parse_page_segment(avctx, p, segment_length);
+                got_segment |= 1;
                 break;
             case DVBSUB_REGION_SEGMENT:
                 dvbsub_parse_region_segment(avctx, p, segment_length);
+                got_segment |= 2;
                 break;
             case DVBSUB_CLUT_SEGMENT:
                 dvbsub_parse_clut_segment(avctx, p, segment_length);
+                got_segment |= 4;
                 break;
             case DVBSUB_OBJECT_SEGMENT:
                 dvbsub_parse_object_segment(avctx, p, segment_length);
+                got_segment |= 8;
                 break;
             case DVBSUB_DISPLAYDEFINITION_SEGMENT:
                 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
                 break;
             case DVBSUB_DISPLAY_SEGMENT:
                 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
+                got_segment |= 16;
                 break;
             default:
                 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
@@ -1516,6 +1522,11 @@ static int dvbsub_decode(AVCodecContext *avctx,
         p += segment_length;
     }
 
+    // Some streams do not send a display segment but if we have all the other
+    // segments then we need no further data.
+    if (got_segment == 15 && sub)
+        *data_size = dvbsub_display_end_segment(avctx, p, 0, sub);
+
     return p - buf;
 }
 
-- 
1.7.9.4

