]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvbsubdec.c
avcodec/dvbsubdec: Fix 8bit non_mod case
[ffmpeg] / libavcodec / dvbsubdec.c
1 /*
2  * DVB subtitle decoding
3  * Copyright (c) 2005 Ian Caulfield
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "bytestream.h"
25 #include "libavutil/colorspace.h"
26 #include "libavutil/opt.h"
27
28 #define DVBSUB_PAGE_SEGMENT     0x10
29 #define DVBSUB_REGION_SEGMENT   0x11
30 #define DVBSUB_CLUT_SEGMENT     0x12
31 #define DVBSUB_OBJECT_SEGMENT   0x13
32 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
33 #define DVBSUB_DISPLAY_SEGMENT  0x80
34
35 #define cm (ff_crop_tab + MAX_NEG_CROP)
36
37 #ifdef DEBUG
38 #if 0
39 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
40                      uint32_t *rgba_palette)
41 {
42     int x, y, v;
43     FILE *f;
44     char fname[40], fname2[40];
45     char command[1024];
46
47     snprintf(fname, 40, "%s.ppm", filename);
48
49     f = fopen(fname, "w");
50     if (!f) {
51         perror(fname);
52         return;
53     }
54     fprintf(f, "P6\n"
55             "%d %d\n"
56             "%d\n",
57             w, h, 255);
58     for(y = 0; y < h; y++) {
59         for(x = 0; x < w; x++) {
60             v = rgba_palette[bitmap[y * w + x]];
61             putc((v >> 16) & 0xff, f);
62             putc((v >> 8) & 0xff, f);
63             putc((v >> 0) & 0xff, f);
64         }
65     }
66     fclose(f);
67
68
69     snprintf(fname2, 40, "%s-a.pgm", filename);
70
71     f = fopen(fname2, "w");
72     if (!f) {
73         perror(fname2);
74         return;
75     }
76     fprintf(f, "P5\n"
77             "%d %d\n"
78             "%d\n",
79             w, h, 255);
80     for(y = 0; y < h; y++) {
81         for(x = 0; x < w; x++) {
82             v = rgba_palette[bitmap[y * w + x]];
83             putc((v >> 24) & 0xff, f);
84         }
85     }
86     fclose(f);
87
88     snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
89     system(command);
90
91     snprintf(command, 1024, "rm %s %s", fname, fname2);
92     system(command);
93 }
94 #endif
95
96 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
97 {
98     int x, y, v;
99     FILE *f;
100     char fname[40], fname2[40];
101     char command[1024];
102
103     snprintf(fname, sizeof(fname), "%s.ppm", filename);
104
105     f = fopen(fname, "w");
106     if (!f) {
107         perror(fname);
108         return;
109     }
110     fprintf(f, "P6\n"
111             "%d %d\n"
112             "%d\n",
113             w, h, 255);
114     for(y = 0; y < h; y++) {
115         for(x = 0; x < w; x++) {
116             v = bitmap[y * w + x];
117             putc((v >> 16) & 0xff, f);
118             putc((v >> 8) & 0xff, f);
119             putc((v >> 0) & 0xff, f);
120         }
121     }
122     fclose(f);
123
124
125     snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
126
127     f = fopen(fname2, "w");
128     if (!f) {
129         perror(fname2);
130         return;
131     }
132     fprintf(f, "P5\n"
133             "%d %d\n"
134             "%d\n",
135             w, h, 255);
136     for(y = 0; y < h; y++) {
137         for(x = 0; x < w; x++) {
138             v = bitmap[y * w + x];
139             putc((v >> 24) & 0xff, f);
140         }
141     }
142     fclose(f);
143
144     snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
145     system(command);
146
147     snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
148     system(command);
149 }
150 #endif
151
152 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
153
154 typedef struct DVBSubCLUT {
155     int id;
156     int version;
157
158     uint32_t clut4[4];
159     uint32_t clut16[16];
160     uint32_t clut256[256];
161
162     struct DVBSubCLUT *next;
163 } DVBSubCLUT;
164
165 static DVBSubCLUT default_clut;
166
167 typedef struct DVBSubObjectDisplay {
168     int object_id;
169     int region_id;
170
171     int x_pos;
172     int y_pos;
173
174     int fgcolor;
175     int bgcolor;
176
177     struct DVBSubObjectDisplay *region_list_next;
178     struct DVBSubObjectDisplay *object_list_next;
179 } DVBSubObjectDisplay;
180
181 typedef struct DVBSubObject {
182     int id;
183     int version;
184
185     int type;
186
187     DVBSubObjectDisplay *display_list;
188
189     struct DVBSubObject *next;
190 } DVBSubObject;
191
192 typedef struct DVBSubRegionDisplay {
193     int region_id;
194
195     int x_pos;
196     int y_pos;
197
198     struct DVBSubRegionDisplay *next;
199 } DVBSubRegionDisplay;
200
201 typedef struct DVBSubRegion {
202     int id;
203     int version;
204
205     int width;
206     int height;
207     int depth;
208
209     int clut;
210     int bgcolor;
211
212     uint8_t *pbuf;
213     int buf_size;
214     int dirty;
215
216     DVBSubObjectDisplay *display_list;
217
218     struct DVBSubRegion *next;
219 } DVBSubRegion;
220
221 typedef struct DVBSubDisplayDefinition {
222     int version;
223
224     int x;
225     int y;
226     int width;
227     int height;
228 } DVBSubDisplayDefinition;
229
230 typedef struct DVBSubContext {
231     AVClass *class;
232     int composition_id;
233     int ancillary_id;
234
235     int version;
236     int time_out;
237     int compute_edt; /**< if 1 end display time calculated using pts
238                           if 0 (Default) calculated using time out */
239     int64_t prev_start;
240     DVBSubRegion *region_list;
241     DVBSubCLUT   *clut_list;
242     DVBSubObject *object_list;
243
244     DVBSubRegionDisplay *display_list;
245     DVBSubDisplayDefinition *display_definition;
246 } DVBSubContext;
247
248
249 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
250 {
251     DVBSubObject *ptr = ctx->object_list;
252
253     while (ptr && ptr->id != object_id) {
254         ptr = ptr->next;
255     }
256
257     return ptr;
258 }
259
260 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
261 {
262     DVBSubCLUT *ptr = ctx->clut_list;
263
264     while (ptr && ptr->id != clut_id) {
265         ptr = ptr->next;
266     }
267
268     return ptr;
269 }
270
271 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
272 {
273     DVBSubRegion *ptr = ctx->region_list;
274
275     while (ptr && ptr->id != region_id) {
276         ptr = ptr->next;
277     }
278
279     return ptr;
280 }
281
282 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
283 {
284     DVBSubObject *object, *obj2, **obj2_ptr;
285     DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
286
287     while (region->display_list) {
288         display = region->display_list;
289
290         object = get_object(ctx, display->object_id);
291
292         if (object) {
293             obj_disp_ptr = &object->display_list;
294             obj_disp = *obj_disp_ptr;
295
296             while (obj_disp && obj_disp != display) {
297                 obj_disp_ptr = &obj_disp->object_list_next;
298                 obj_disp = *obj_disp_ptr;
299             }
300
301             if (obj_disp) {
302                 *obj_disp_ptr = obj_disp->object_list_next;
303
304                 if (!object->display_list) {
305                     obj2_ptr = &ctx->object_list;
306                     obj2 = *obj2_ptr;
307
308                     while (obj2 != object) {
309                         assert(obj2);
310                         obj2_ptr = &obj2->next;
311                         obj2 = *obj2_ptr;
312                     }
313
314                     *obj2_ptr = obj2->next;
315
316                     av_freep(&obj2);
317                 }
318             }
319         }
320
321         region->display_list = display->region_list_next;
322
323         av_freep(&display);
324     }
325
326 }
327
328 static void delete_cluts(DVBSubContext *ctx)
329 {
330     while (ctx->clut_list) {
331         DVBSubCLUT *clut = ctx->clut_list;
332
333         ctx->clut_list = clut->next;
334
335         av_freep(&clut);
336     }
337 }
338
339 static void delete_objects(DVBSubContext *ctx)
340 {
341     while (ctx->object_list) {
342         DVBSubObject *object = ctx->object_list;
343
344         ctx->object_list = object->next;
345
346         av_freep(&object);
347     }
348 }
349
350 static void delete_regions(DVBSubContext *ctx)
351 {
352     while (ctx->region_list) {
353         DVBSubRegion *region = ctx->region_list;
354
355         ctx->region_list = region->next;
356
357         delete_region_display_list(ctx, region);
358
359         av_freep(&region->pbuf);
360         av_freep(&region);
361     }
362 }
363
364 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
365 {
366     int i, r, g, b, a = 0;
367     DVBSubContext *ctx = avctx->priv_data;
368
369     if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
370         av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
371         ctx->composition_id = -1;
372         ctx->ancillary_id   = -1;
373     } else {
374         if (avctx->extradata_size > 5) {
375             av_log(avctx, AV_LOG_WARNING, "Decoding first DVB subtitles sub-stream\n");
376         }
377
378         ctx->composition_id = AV_RB16(avctx->extradata);
379         ctx->ancillary_id   = AV_RB16(avctx->extradata + 2);
380     }
381
382     ctx->version = -1;
383     ctx->prev_start = AV_NOPTS_VALUE;
384
385     default_clut.id = -1;
386     default_clut.next = NULL;
387
388     default_clut.clut4[0] = RGBA(  0,   0,   0,   0);
389     default_clut.clut4[1] = RGBA(255, 255, 255, 255);
390     default_clut.clut4[2] = RGBA(  0,   0,   0, 255);
391     default_clut.clut4[3] = RGBA(127, 127, 127, 255);
392
393     default_clut.clut16[0] = RGBA(  0,   0,   0,   0);
394     for (i = 1; i < 16; i++) {
395         if (i < 8) {
396             r = (i & 1) ? 255 : 0;
397             g = (i & 2) ? 255 : 0;
398             b = (i & 4) ? 255 : 0;
399         } else {
400             r = (i & 1) ? 127 : 0;
401             g = (i & 2) ? 127 : 0;
402             b = (i & 4) ? 127 : 0;
403         }
404         default_clut.clut16[i] = RGBA(r, g, b, 255);
405     }
406
407     default_clut.clut256[0] = RGBA(  0,   0,   0,   0);
408     for (i = 1; i < 256; i++) {
409         if (i < 8) {
410             r = (i & 1) ? 255 : 0;
411             g = (i & 2) ? 255 : 0;
412             b = (i & 4) ? 255 : 0;
413             a = 63;
414         } else {
415             switch (i & 0x88) {
416             case 0x00:
417                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
418                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
419                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
420                 a = 255;
421                 break;
422             case 0x08:
423                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
424                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
425                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
426                 a = 127;
427                 break;
428             case 0x80:
429                 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
430                 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
431                 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
432                 a = 255;
433                 break;
434             case 0x88:
435                 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
436                 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
437                 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
438                 a = 255;
439                 break;
440             }
441         }
442         default_clut.clut256[i] = RGBA(r, g, b, a);
443     }
444
445     return 0;
446 }
447
448 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
449 {
450     DVBSubContext *ctx = avctx->priv_data;
451     DVBSubRegionDisplay *display;
452
453     delete_regions(ctx);
454
455     delete_objects(ctx);
456
457     delete_cluts(ctx);
458
459     av_freep(&ctx->display_definition);
460
461     while (ctx->display_list) {
462         display = ctx->display_list;
463         ctx->display_list = display->next;
464
465         av_freep(&display);
466     }
467
468     return 0;
469 }
470
471 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
472                                    const uint8_t **srcbuf, int buf_size,
473                                    int non_mod, uint8_t *map_table, int x_pos)
474 {
475     GetBitContext gb;
476
477     int bits;
478     int run_length;
479     int pixels_read = x_pos;
480
481     init_get_bits(&gb, *srcbuf, buf_size << 3);
482
483     destbuf += x_pos;
484
485     while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
486         bits = get_bits(&gb, 2);
487
488         if (bits) {
489             if (non_mod != 1 || bits != 1) {
490                 if (map_table)
491                     *destbuf++ = map_table[bits];
492                 else
493                     *destbuf++ = bits;
494             }
495             pixels_read++;
496         } else {
497             bits = get_bits1(&gb);
498             if (bits == 1) {
499                 run_length = get_bits(&gb, 3) + 3;
500                 bits = get_bits(&gb, 2);
501
502                 if (non_mod == 1 && bits == 1)
503                     pixels_read += run_length;
504                 else {
505                     if (map_table)
506                         bits = map_table[bits];
507                     while (run_length-- > 0 && pixels_read < dbuf_len) {
508                         *destbuf++ = bits;
509                         pixels_read++;
510                     }
511                 }
512             } else {
513                 bits = get_bits1(&gb);
514                 if (bits == 0) {
515                     bits = get_bits(&gb, 2);
516                     if (bits == 2) {
517                         run_length = get_bits(&gb, 4) + 12;
518                         bits = get_bits(&gb, 2);
519
520                         if (non_mod == 1 && bits == 1)
521                             pixels_read += run_length;
522                         else {
523                             if (map_table)
524                                 bits = map_table[bits];
525                             while (run_length-- > 0 && pixels_read < dbuf_len) {
526                                 *destbuf++ = bits;
527                                 pixels_read++;
528                             }
529                         }
530                     } else if (bits == 3) {
531                         run_length = get_bits(&gb, 8) + 29;
532                         bits = get_bits(&gb, 2);
533
534                         if (non_mod == 1 && bits == 1)
535                             pixels_read += run_length;
536                         else {
537                             if (map_table)
538                                 bits = map_table[bits];
539                             while (run_length-- > 0 && pixels_read < dbuf_len) {
540                                 *destbuf++ = bits;
541                                 pixels_read++;
542                             }
543                         }
544                     } else if (bits == 1) {
545                         if (map_table)
546                             bits = map_table[0];
547                         else
548                             bits = 0;
549                         run_length = 2;
550                         while (run_length-- > 0 && pixels_read < dbuf_len) {
551                             *destbuf++ = bits;
552                             pixels_read++;
553                         }
554                     } else {
555                         (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
556                         return pixels_read;
557                     }
558                 } else {
559                     if (map_table)
560                         bits = map_table[0];
561                     else
562                         bits = 0;
563                     *destbuf++ = bits;
564                     pixels_read++;
565                 }
566             }
567         }
568     }
569
570     if (get_bits(&gb, 6))
571         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
572
573     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
574
575     return pixels_read;
576 }
577
578 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
579                                    const uint8_t **srcbuf, int buf_size,
580                                    int non_mod, uint8_t *map_table, int x_pos)
581 {
582     GetBitContext gb;
583
584     int bits;
585     int run_length;
586     int pixels_read = x_pos;
587
588     init_get_bits(&gb, *srcbuf, buf_size << 3);
589
590     destbuf += x_pos;
591
592     while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
593         bits = get_bits(&gb, 4);
594
595         if (bits) {
596             if (non_mod != 1 || bits != 1) {
597                 if (map_table)
598                     *destbuf++ = map_table[bits];
599                 else
600                     *destbuf++ = bits;
601             }
602             pixels_read++;
603         } else {
604             bits = get_bits1(&gb);
605             if (bits == 0) {
606                 run_length = get_bits(&gb, 3);
607
608                 if (run_length == 0) {
609                     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
610                     return pixels_read;
611                 }
612
613                 run_length += 2;
614
615                 if (map_table)
616                     bits = map_table[0];
617                 else
618                     bits = 0;
619
620                 while (run_length-- > 0 && pixels_read < dbuf_len) {
621                     *destbuf++ = bits;
622                     pixels_read++;
623                 }
624             } else {
625                 bits = get_bits1(&gb);
626                 if (bits == 0) {
627                     run_length = get_bits(&gb, 2) + 4;
628                     bits = get_bits(&gb, 4);
629
630                     if (non_mod == 1 && bits == 1)
631                         pixels_read += run_length;
632                     else {
633                         if (map_table)
634                             bits = map_table[bits];
635                         while (run_length-- > 0 && pixels_read < dbuf_len) {
636                             *destbuf++ = bits;
637                             pixels_read++;
638                         }
639                     }
640                 } else {
641                     bits = get_bits(&gb, 2);
642                     if (bits == 2) {
643                         run_length = get_bits(&gb, 4) + 9;
644                         bits = get_bits(&gb, 4);
645
646                         if (non_mod == 1 && bits == 1)
647                             pixels_read += run_length;
648                         else {
649                             if (map_table)
650                                 bits = map_table[bits];
651                             while (run_length-- > 0 && pixels_read < dbuf_len) {
652                                 *destbuf++ = bits;
653                                 pixels_read++;
654                             }
655                         }
656                     } else if (bits == 3) {
657                         run_length = get_bits(&gb, 8) + 25;
658                         bits = get_bits(&gb, 4);
659
660                         if (non_mod == 1 && bits == 1)
661                             pixels_read += run_length;
662                         else {
663                             if (map_table)
664                                 bits = map_table[bits];
665                             while (run_length-- > 0 && pixels_read < dbuf_len) {
666                                 *destbuf++ = bits;
667                                 pixels_read++;
668                             }
669                         }
670                     } else if (bits == 1) {
671                         if (map_table)
672                             bits = map_table[0];
673                         else
674                             bits = 0;
675                         run_length = 2;
676                         while (run_length-- > 0 && pixels_read < dbuf_len) {
677                             *destbuf++ = bits;
678                             pixels_read++;
679                         }
680                     } else {
681                         if (map_table)
682                             bits = map_table[0];
683                         else
684                             bits = 0;
685                         *destbuf++ = bits;
686                         pixels_read ++;
687                     }
688                 }
689             }
690         }
691     }
692
693     if (get_bits(&gb, 8))
694         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
695
696     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
697
698     return pixels_read;
699 }
700
701 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
702                                     const uint8_t **srcbuf, int buf_size,
703                                     int non_mod, uint8_t *map_table, int x_pos)
704 {
705     const uint8_t *sbuf_end = (*srcbuf) + buf_size;
706     int bits;
707     int run_length;
708     int pixels_read = x_pos;
709
710     destbuf += x_pos;
711
712     while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
713         bits = *(*srcbuf)++;
714
715         if (bits) {
716             if (non_mod != 1 || bits != 1) {
717                 if (map_table)
718                     *destbuf++ = map_table[bits];
719                 else
720                     *destbuf++ = bits;
721             }
722             pixels_read++;
723         } else {
724             bits = *(*srcbuf)++;
725             run_length = bits & 0x7f;
726             if ((bits & 0x80) == 0) {
727                 if (run_length == 0) {
728                     return pixels_read;
729                 }
730
731                 if (map_table)
732                     bits = map_table[0];
733                 else
734                     bits = 0;
735                 while (run_length-- > 0 && pixels_read < dbuf_len) {
736                     *destbuf++ = bits;
737                     pixels_read++;
738                 }
739             } else {
740                 bits = *(*srcbuf)++;
741
742                 if (non_mod == 1 && bits == 1)
743                     pixels_read += run_length;
744                 else {
745                     if (map_table)
746                         bits = map_table[bits];
747                     while (run_length-- > 0 && pixels_read < dbuf_len) {
748                         *destbuf++ = bits;
749                         pixels_read++;
750                     }
751                 }
752             }
753         }
754     }
755
756     if (*(*srcbuf)++)
757         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
758
759     return pixels_read;
760 }
761
762 static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
763 {
764     DVBSubContext *ctx = avctx->priv_data;
765     DVBSubRegionDisplay *display;
766     DVBSubDisplayDefinition *display_def = ctx->display_definition;
767     DVBSubRegion *region;
768     AVSubtitleRect *rect;
769     DVBSubCLUT *clut;
770     uint32_t *clut_table;
771     int i;
772     int offset_x=0, offset_y=0;
773
774
775     if (display_def) {
776         offset_x = display_def->x;
777         offset_y = display_def->y;
778     }
779
780     /* Not touching AVSubtitles again*/
781     if(sub->num_rects) {
782         avpriv_request_sample(ctx, "Different Version of Segment asked Twice\n");
783         return AVERROR_PATCHWELCOME;
784     }
785     for (display = ctx->display_list; display; display = display->next) {
786         region = get_region(ctx, display->region_id);
787         if (region && region->dirty)
788             sub->num_rects++;
789     }
790
791     if(ctx->compute_edt == 0) {
792         sub->end_display_time = ctx->time_out * 1000;
793         *got_output = 1;
794     } else if (ctx->prev_start != AV_NOPTS_VALUE) {
795         sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
796         *got_output = 1;
797     }
798     if (sub->num_rects > 0) {
799
800         sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
801         if (!sub->rects)
802             return AVERROR(ENOMEM);
803
804         for(i=0; i<sub->num_rects; i++)
805             sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
806
807         i = 0;
808
809         for (display = ctx->display_list; display; display = display->next) {
810             region = get_region(ctx, display->region_id);
811
812             if (!region)
813                 continue;
814
815             if (!region->dirty)
816                 continue;
817
818             rect = sub->rects[i];
819             rect->x = display->x_pos + offset_x;
820             rect->y = display->y_pos + offset_y;
821             rect->w = region->width;
822             rect->h = region->height;
823             rect->nb_colors = (1 << region->depth);
824             rect->type      = SUBTITLE_BITMAP;
825             rect->pict.linesize[0] = region->width;
826
827             clut = get_clut(ctx, region->clut);
828
829             if (!clut)
830                 clut = &default_clut;
831
832             switch (region->depth) {
833             case 2:
834                 clut_table = clut->clut4;
835                 break;
836             case 8:
837                 clut_table = clut->clut256;
838                 break;
839             case 4:
840             default:
841                 clut_table = clut->clut16;
842                 break;
843             }
844
845             rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
846             if (!rect->pict.data[1]) {
847                 av_freep(&sub->rects);
848                 return AVERROR(ENOMEM);
849             }
850             memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
851
852             rect->pict.data[0] = av_malloc(region->buf_size);
853             if (!rect->pict.data[0]) {
854                 av_freep(&rect->pict.data[1]);
855                 av_freep(&sub->rects);
856                 return AVERROR(ENOMEM);
857             }
858
859             memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
860
861             i++;
862         }
863     }
864
865     return 0;
866 }
867
868 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
869                                           const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
870 {
871     DVBSubContext *ctx = avctx->priv_data;
872
873     DVBSubRegion *region = get_region(ctx, display->region_id);
874     const uint8_t *buf_end = buf + buf_size;
875     uint8_t *pbuf;
876     int x_pos, y_pos;
877     int i;
878
879     uint8_t map2to4[] = { 0x0,  0x7,  0x8,  0xf};
880     uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
881     uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
882                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
883     uint8_t *map_table;
884
885 #if 0
886     av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
887             top_bottom ? "bottom" : "top");
888
889     for (i = 0; i < buf_size; i++) {
890         if (i % 16 == 0)
891             av_dlog(avctx, "0x%8p: ", buf+i);
892
893         av_dlog(avctx, "%02x ", buf[i]);
894         if (i % 16 == 15)
895             av_dlog(avctx, "\n");
896     }
897
898     if (i % 16)
899         av_dlog(avctx, "\n");
900 #endif
901
902     if (!region)
903         return;
904
905     pbuf = region->pbuf;
906     region->dirty = 1;
907
908     x_pos = display->x_pos;
909     y_pos = display->y_pos;
910
911     y_pos += top_bottom;
912
913     while (buf < buf_end) {
914         if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
915             av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
916             return;
917         }
918
919         switch (*buf++) {
920         case 0x10:
921             if (region->depth == 8)
922                 map_table = map2to8;
923             else if (region->depth == 4)
924                 map_table = map2to4;
925             else
926                 map_table = NULL;
927
928             x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width),
929                                             region->width, &buf, buf_end - buf,
930                                             non_mod, map_table, x_pos);
931             break;
932         case 0x11:
933             if (region->depth < 4) {
934                 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
935                 return;
936             }
937
938             if (region->depth == 8)
939                 map_table = map4to8;
940             else
941                 map_table = NULL;
942
943             x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width),
944                                             region->width, &buf, buf_end - buf,
945                                             non_mod, map_table, x_pos);
946             break;
947         case 0x12:
948             if (region->depth < 8) {
949                 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
950                 return;
951             }
952
953             x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width),
954                                             region->width, &buf, buf_end - buf,
955                                             non_mod, NULL, x_pos);
956             break;
957
958         case 0x20:
959             map2to4[0] = (*buf) >> 4;
960             map2to4[1] = (*buf++) & 0xf;
961             map2to4[2] = (*buf) >> 4;
962             map2to4[3] = (*buf++) & 0xf;
963             break;
964         case 0x21:
965             for (i = 0; i < 4; i++)
966                 map2to8[i] = *buf++;
967             break;
968         case 0x22:
969             for (i = 0; i < 16; i++)
970                 map4to8[i] = *buf++;
971             break;
972
973         case 0xf0:
974             x_pos = display->x_pos;
975             y_pos += 2;
976             break;
977         default:
978             av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
979         }
980     }
981
982 }
983
984 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
985                                         const uint8_t *buf, int buf_size)
986 {
987     DVBSubContext *ctx = avctx->priv_data;
988
989     const uint8_t *buf_end = buf + buf_size;
990     int object_id;
991     DVBSubObject *object;
992     DVBSubObjectDisplay *display;
993     int top_field_len, bottom_field_len;
994
995     int coding_method, non_modifying_color;
996
997     object_id = AV_RB16(buf);
998     buf += 2;
999
1000     object = get_object(ctx, object_id);
1001
1002     if (!object)
1003         return;
1004
1005     coding_method = ((*buf) >> 2) & 3;
1006     non_modifying_color = ((*buf++) >> 1) & 1;
1007
1008     if (coding_method == 0) {
1009         top_field_len = AV_RB16(buf);
1010         buf += 2;
1011         bottom_field_len = AV_RB16(buf);
1012         buf += 2;
1013
1014         if (buf + top_field_len + bottom_field_len > buf_end) {
1015             av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
1016             return;
1017         }
1018
1019         for (display = object->display_list; display; display = display->object_list_next) {
1020             const uint8_t *block = buf;
1021             int bfl = bottom_field_len;
1022
1023             dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
1024                                             non_modifying_color);
1025
1026             if (bottom_field_len > 0)
1027                 block = buf + top_field_len;
1028             else
1029                 bfl = top_field_len;
1030
1031             dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
1032                                             non_modifying_color);
1033         }
1034
1035 /*  } else if (coding_method == 1) {*/
1036
1037     } else {
1038         av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
1039     }
1040
1041 }
1042
1043 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
1044                                         const uint8_t *buf, int buf_size)
1045 {
1046     DVBSubContext *ctx = avctx->priv_data;
1047
1048     const uint8_t *buf_end = buf + buf_size;
1049     int i, clut_id;
1050     int version;
1051     DVBSubCLUT *clut;
1052     int entry_id, depth , full_range;
1053     int y, cr, cb, alpha;
1054     int r, g, b, r_add, g_add, b_add;
1055
1056     av_dlog(avctx, "DVB clut packet:\n");
1057
1058     for (i=0; i < buf_size; i++) {
1059         av_dlog(avctx, "%02x ", buf[i]);
1060         if (i % 16 == 15)
1061             av_dlog(avctx, "\n");
1062     }
1063
1064     if (i % 16)
1065         av_dlog(avctx, "\n");
1066
1067     clut_id = *buf++;
1068     version = ((*buf)>>4)&15;
1069     buf += 1;
1070
1071     clut = get_clut(ctx, clut_id);
1072
1073     if (!clut) {
1074         clut = av_malloc(sizeof(DVBSubCLUT));
1075
1076         memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
1077
1078         clut->id = clut_id;
1079         clut->version = -1;
1080
1081         clut->next = ctx->clut_list;
1082         ctx->clut_list = clut;
1083     }
1084
1085     if (clut->version != version) {
1086
1087     clut->version = version;
1088
1089     while (buf + 4 < buf_end) {
1090         entry_id = *buf++;
1091
1092         depth = (*buf) & 0xe0;
1093
1094         if (depth == 0) {
1095             av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
1096             return 0;
1097         }
1098
1099         full_range = (*buf++) & 1;
1100
1101         if (full_range) {
1102             y = *buf++;
1103             cr = *buf++;
1104             cb = *buf++;
1105             alpha = *buf++;
1106         } else {
1107             y = buf[0] & 0xfc;
1108             cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1109             cb = (buf[1] << 2) & 0xf0;
1110             alpha = (buf[1] << 6) & 0xc0;
1111
1112             buf += 2;
1113         }
1114
1115         if (y == 0)
1116             alpha = 0xff;
1117
1118         YUV_TO_RGB1_CCIR(cb, cr);
1119         YUV_TO_RGB2_CCIR(r, g, b, y);
1120
1121         av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1122         if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1123             av_dlog(avctx, "More than one bit level marked: %x\n", depth);
1124             if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
1125                 return AVERROR_INVALIDDATA;
1126         }
1127
1128         if (depth & 0x80)
1129             clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1130         else if (depth & 0x40)
1131             clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1132         else if (depth & 0x20)
1133             clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1134     }
1135     }
1136     return 0;
1137 }
1138
1139
1140 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1141                                         const uint8_t *buf, int buf_size)
1142 {
1143     DVBSubContext *ctx = avctx->priv_data;
1144
1145     const uint8_t *buf_end = buf + buf_size;
1146     int region_id, object_id;
1147     int av_unused version;
1148     DVBSubRegion *region;
1149     DVBSubObject *object;
1150     DVBSubObjectDisplay *display;
1151     int fill;
1152
1153     if (buf_size < 10)
1154         return;
1155
1156     region_id = *buf++;
1157
1158     region = get_region(ctx, region_id);
1159
1160     if (!region) {
1161         region = av_mallocz(sizeof(DVBSubRegion));
1162
1163         region->id = region_id;
1164         region->version = -1;
1165
1166         region->next = ctx->region_list;
1167         ctx->region_list = region;
1168     }
1169
1170     version = ((*buf)>>4) & 15;
1171     fill = ((*buf++) >> 3) & 1;
1172
1173     region->width = AV_RB16(buf);
1174     buf += 2;
1175     region->height = AV_RB16(buf);
1176     buf += 2;
1177
1178     if (region->width * region->height != region->buf_size) {
1179         av_free(region->pbuf);
1180
1181         region->buf_size = region->width * region->height;
1182
1183         region->pbuf = av_malloc(region->buf_size);
1184
1185         fill = 1;
1186         region->dirty = 0;
1187     }
1188
1189     region->depth = 1 << (((*buf++) >> 2) & 7);
1190     if(region->depth<2 || region->depth>8){
1191         av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1192         region->depth= 4;
1193     }
1194     region->clut = *buf++;
1195
1196     if (region->depth == 8) {
1197         region->bgcolor = *buf++;
1198         buf += 1;
1199     } else {
1200         buf += 1;
1201
1202         if (region->depth == 4)
1203             region->bgcolor = (((*buf++) >> 4) & 15);
1204         else
1205             region->bgcolor = (((*buf++) >> 2) & 3);
1206     }
1207
1208     av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1209
1210     if (fill) {
1211         memset(region->pbuf, region->bgcolor, region->buf_size);
1212         av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1213     }
1214
1215     delete_region_display_list(ctx, region);
1216
1217     while (buf + 5 < buf_end) {
1218         object_id = AV_RB16(buf);
1219         buf += 2;
1220
1221         object = get_object(ctx, object_id);
1222
1223         if (!object) {
1224             object = av_mallocz(sizeof(DVBSubObject));
1225
1226             object->id = object_id;
1227             object->next = ctx->object_list;
1228             ctx->object_list = object;
1229         }
1230
1231         object->type = (*buf) >> 6;
1232
1233         display = av_mallocz(sizeof(DVBSubObjectDisplay));
1234
1235         display->object_id = object_id;
1236         display->region_id = region_id;
1237
1238         display->x_pos = AV_RB16(buf) & 0xfff;
1239         buf += 2;
1240         display->y_pos = AV_RB16(buf) & 0xfff;
1241         buf += 2;
1242
1243         if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1244             display->fgcolor = *buf++;
1245             display->bgcolor = *buf++;
1246         }
1247
1248         display->region_list_next = region->display_list;
1249         region->display_list = display;
1250
1251         display->object_list_next = object->display_list;
1252         object->display_list = display;
1253     }
1254 }
1255
1256 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1257                                         const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
1258 {
1259     DVBSubContext *ctx = avctx->priv_data;
1260     DVBSubRegionDisplay *display;
1261     DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1262
1263     const uint8_t *buf_end = buf + buf_size;
1264     int region_id;
1265     int page_state;
1266     int timeout;
1267     int version;
1268
1269     if (buf_size < 1)
1270         return;
1271
1272     timeout = *buf++;
1273     version = ((*buf)>>4) & 15;
1274     page_state = ((*buf++) >> 2) & 3;
1275
1276     if (ctx->version == version) {
1277         return;
1278     }
1279
1280     ctx->time_out = timeout;
1281     ctx->version = version;
1282
1283     av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1284
1285     if(ctx->compute_edt == 1)
1286         save_subtitle_set(avctx, sub, got_output);
1287
1288     if (page_state == 1 || page_state == 2) {
1289         delete_regions(ctx);
1290         delete_objects(ctx);
1291         delete_cluts(ctx);
1292     }
1293
1294     tmp_display_list = ctx->display_list;
1295     ctx->display_list = NULL;
1296
1297     while (buf + 5 < buf_end) {
1298         region_id = *buf++;
1299         buf += 1;
1300
1301         display = tmp_display_list;
1302         tmp_ptr = &tmp_display_list;
1303
1304         while (display && display->region_id != region_id) {
1305             tmp_ptr = &display->next;
1306             display = display->next;
1307         }
1308
1309         if (!display)
1310             display = av_mallocz(sizeof(DVBSubRegionDisplay));
1311
1312         display->region_id = region_id;
1313
1314         display->x_pos = AV_RB16(buf);
1315         buf += 2;
1316         display->y_pos = AV_RB16(buf);
1317         buf += 2;
1318
1319         *tmp_ptr = display->next;
1320
1321         display->next = ctx->display_list;
1322         ctx->display_list = display;
1323
1324         av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1325     }
1326
1327     while (tmp_display_list) {
1328         display = tmp_display_list;
1329
1330         tmp_display_list = display->next;
1331
1332         av_freep(&display);
1333     }
1334
1335 }
1336
1337
1338 #ifdef DEBUG
1339 static void save_display_set(DVBSubContext *ctx)
1340 {
1341     DVBSubRegion *region;
1342     DVBSubRegionDisplay *display;
1343     DVBSubCLUT *clut;
1344     uint32_t *clut_table;
1345     int x_pos, y_pos, width, height;
1346     int x, y, y_off, x_off;
1347     uint32_t *pbuf;
1348     char filename[32];
1349     static int fileno_index = 0;
1350
1351     x_pos = -1;
1352     y_pos = -1;
1353     width = 0;
1354     height = 0;
1355
1356     for (display = ctx->display_list; display; display = display->next) {
1357         region = get_region(ctx, display->region_id);
1358
1359         if (x_pos == -1) {
1360             x_pos = display->x_pos;
1361             y_pos = display->y_pos;
1362             width = region->width;
1363             height = region->height;
1364         } else {
1365             if (display->x_pos < x_pos) {
1366                 width += (x_pos - display->x_pos);
1367                 x_pos = display->x_pos;
1368             }
1369
1370             if (display->y_pos < y_pos) {
1371                 height += (y_pos - display->y_pos);
1372                 y_pos = display->y_pos;
1373             }
1374
1375             if (display->x_pos + region->width > x_pos + width) {
1376                 width = display->x_pos + region->width - x_pos;
1377             }
1378
1379             if (display->y_pos + region->height > y_pos + height) {
1380                 height = display->y_pos + region->height - y_pos;
1381             }
1382         }
1383     }
1384
1385     if (x_pos >= 0) {
1386
1387         pbuf = av_malloc(width * height * 4);
1388
1389         for (display = ctx->display_list; display; display = display->next) {
1390             region = get_region(ctx, display->region_id);
1391
1392             x_off = display->x_pos - x_pos;
1393             y_off = display->y_pos - y_pos;
1394
1395             clut = get_clut(ctx, region->clut);
1396
1397             if (!clut)
1398                 clut = &default_clut;
1399
1400             switch (region->depth) {
1401             case 2:
1402                 clut_table = clut->clut4;
1403                 break;
1404             case 8:
1405                 clut_table = clut->clut256;
1406                 break;
1407             case 4:
1408             default:
1409                 clut_table = clut->clut16;
1410                 break;
1411             }
1412
1413             for (y = 0; y < region->height; y++) {
1414                 for (x = 0; x < region->width; x++) {
1415                     pbuf[((y + y_off) * width) + x_off + x] =
1416                         clut_table[region->pbuf[y * region->width + x]];
1417                 }
1418             }
1419
1420         }
1421
1422         snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1423
1424         png_save2(filename, pbuf, width, height);
1425
1426         av_freep(&pbuf);
1427     }
1428
1429     fileno_index++;
1430 }
1431 #endif
1432
1433 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1434                                                     const uint8_t *buf,
1435                                                     int buf_size)
1436 {
1437     DVBSubContext *ctx = avctx->priv_data;
1438     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1439     int dds_version, info_byte;
1440
1441     if (buf_size < 5)
1442         return;
1443
1444     info_byte   = bytestream_get_byte(&buf);
1445     dds_version = info_byte >> 4;
1446     if (display_def && display_def->version == dds_version)
1447         return; // already have this display definition version
1448
1449     if (!display_def) {
1450         display_def             = av_mallocz(sizeof(*display_def));
1451         ctx->display_definition = display_def;
1452     }
1453     if (!display_def)
1454         return;
1455
1456     display_def->version = dds_version;
1457     display_def->x       = 0;
1458     display_def->y       = 0;
1459     display_def->width   = bytestream_get_be16(&buf) + 1;
1460     display_def->height  = bytestream_get_be16(&buf) + 1;
1461     if (!avctx->width || !avctx->height) {
1462         avctx->width  = display_def->width;
1463         avctx->height = display_def->height;
1464     }
1465
1466     if (buf_size < 13)
1467         return;
1468
1469     if (info_byte & 1<<3) { // display_window_flag
1470         display_def->x = bytestream_get_be16(&buf);
1471         display_def->width  = bytestream_get_be16(&buf) - display_def->x + 1;
1472         display_def->y = bytestream_get_be16(&buf);
1473         display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1474     }
1475 }
1476
1477 static void dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1478                                         int buf_size, AVSubtitle *sub,int *got_output)
1479 {
1480     DVBSubContext *ctx = avctx->priv_data;
1481
1482     if(ctx->compute_edt == 0)
1483         save_subtitle_set(avctx, sub, got_output);
1484 #ifdef DEBUG
1485     save_display_set(ctx);
1486 #endif
1487
1488 }
1489
1490 static int dvbsub_decode(AVCodecContext *avctx,
1491                          void *data, int *data_size,
1492                          AVPacket *avpkt)
1493 {
1494     const uint8_t *buf = avpkt->data;
1495     int buf_size = avpkt->size;
1496     DVBSubContext *ctx = avctx->priv_data;
1497     AVSubtitle *sub = data;
1498     const uint8_t *p, *p_end;
1499     int segment_type;
1500     int page_id;
1501     int segment_length;
1502     int i;
1503     int ret = 0;
1504     int got_segment = 0;
1505
1506     av_dlog(avctx, "DVB sub packet:\n");
1507
1508     for (i=0; i < buf_size; i++) {
1509         av_dlog(avctx, "%02x ", buf[i]);
1510         if (i % 16 == 15)
1511             av_dlog(avctx, "\n");
1512     }
1513
1514     if (i % 16)
1515         av_dlog(avctx, "\n");
1516
1517     if (buf_size <= 6 || *buf != 0x0f) {
1518         av_dlog(avctx, "incomplete or broken packet");
1519         return -1;
1520     }
1521
1522     p = buf;
1523     p_end = buf + buf_size;
1524
1525     while (p_end - p >= 6 && *p == 0x0f) {
1526         p += 1;
1527         segment_type = *p++;
1528         page_id = AV_RB16(p);
1529         p += 2;
1530         segment_length = AV_RB16(p);
1531         p += 2;
1532
1533         if (avctx->debug & FF_DEBUG_STARTCODE) {
1534             av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
1535         }
1536
1537         if (p_end - p < segment_length) {
1538             av_dlog(avctx, "incomplete or broken packet");
1539             ret = -1;
1540             goto end;
1541         }
1542
1543         if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1544             ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1545             switch (segment_type) {
1546             case DVBSUB_PAGE_SEGMENT:
1547                 dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
1548                 got_segment |= 1;
1549                 break;
1550             case DVBSUB_REGION_SEGMENT:
1551                 dvbsub_parse_region_segment(avctx, p, segment_length);
1552                 got_segment |= 2;
1553                 break;
1554             case DVBSUB_CLUT_SEGMENT:
1555                 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1556                 if (ret < 0) goto end;
1557                 got_segment |= 4;
1558                 break;
1559             case DVBSUB_OBJECT_SEGMENT:
1560                 dvbsub_parse_object_segment(avctx, p, segment_length);
1561                 got_segment |= 8;
1562                 break;
1563             case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1564                 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1565                 break;
1566             case DVBSUB_DISPLAY_SEGMENT:
1567                 dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
1568                 got_segment |= 16;
1569                 break;
1570             default:
1571                 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1572                         segment_type, page_id, segment_length);
1573                 break;
1574             }
1575         }
1576
1577         p += segment_length;
1578     }
1579     // Some streams do not send a display segment but if we have all the other
1580     // segments then we need no further data.
1581     if (got_segment == 15) {
1582         av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
1583         dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
1584     }
1585
1586 end:
1587     if(ret < 0) {
1588         *data_size = 0;
1589         avsubtitle_free(sub);
1590         return ret;
1591     } else {
1592         if(ctx->compute_edt == 1 )
1593             FFSWAP(int64_t, ctx->prev_start, sub->pts);
1594     }
1595
1596     return p - buf;
1597 }
1598
1599 #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
1600 static const AVOption options[] = {
1601     {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), FF_OPT_TYPE_INT, {.i64 = 0}, 0, 1, DS},
1602     {NULL}
1603 };
1604 static const AVClass dvbsubdec_class = {
1605     .class_name = "DVB Sub Decoder",
1606     .item_name  = av_default_item_name,
1607     .option     = options,
1608     .version    = LIBAVUTIL_VERSION_INT,
1609 };
1610
1611 AVCodec ff_dvbsub_decoder = {
1612     .name           = "dvbsub",
1613     .long_name      = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1614     .type           = AVMEDIA_TYPE_SUBTITLE,
1615     .id             = AV_CODEC_ID_DVB_SUBTITLE,
1616     .priv_data_size = sizeof(DVBSubContext),
1617     .init           = dvbsub_init_decoder,
1618     .close          = dvbsub_close_decoder,
1619     .decode         = dvbsub_decode,
1620     .priv_class     = &dvbsubdec_class,
1621 };