]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvbsubdec.c
Merge commit 'ba42c852477e87f6e47a5587e8f7829c46c52032'
[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 #include "avcodec.h"
22 #include "dsputil.h"
23 #include "get_bits.h"
24 #include "bytestream.h"
25 #include "libavutil/colorspace.h"
26
27 #define DVBSUB_PAGE_SEGMENT     0x10
28 #define DVBSUB_REGION_SEGMENT   0x11
29 #define DVBSUB_CLUT_SEGMENT     0x12
30 #define DVBSUB_OBJECT_SEGMENT   0x13
31 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
32 #define DVBSUB_DISPLAY_SEGMENT  0x80
33
34 #define cm (ff_cropTbl + MAX_NEG_CROP)
35
36 #ifdef DEBUG
37 #if 0
38 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
39                      uint32_t *rgba_palette)
40 {
41     int x, y, v;
42     FILE *f;
43     char fname[40], fname2[40];
44     char command[1024];
45
46     snprintf(fname, 40, "%s.ppm", filename);
47
48     f = fopen(fname, "w");
49     if (!f) {
50         perror(fname);
51         return;
52     }
53     fprintf(f, "P6\n"
54             "%d %d\n"
55             "%d\n",
56             w, h, 255);
57     for(y = 0; y < h; y++) {
58         for(x = 0; x < w; x++) {
59             v = rgba_palette[bitmap[y * w + x]];
60             putc((v >> 16) & 0xff, f);
61             putc((v >> 8) & 0xff, f);
62             putc((v >> 0) & 0xff, f);
63         }
64     }
65     fclose(f);
66
67
68     snprintf(fname2, 40, "%s-a.pgm", filename);
69
70     f = fopen(fname2, "w");
71     if (!f) {
72         perror(fname2);
73         return;
74     }
75     fprintf(f, "P5\n"
76             "%d %d\n"
77             "%d\n",
78             w, h, 255);
79     for(y = 0; y < h; y++) {
80         for(x = 0; x < w; x++) {
81             v = rgba_palette[bitmap[y * w + x]];
82             putc((v >> 24) & 0xff, f);
83         }
84     }
85     fclose(f);
86
87     snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
88     system(command);
89
90     snprintf(command, 1024, "rm %s %s", fname, fname2);
91     system(command);
92 }
93 #endif
94
95 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
96 {
97     int x, y, v;
98     FILE *f;
99     char fname[40], fname2[40];
100     char command[1024];
101
102     snprintf(fname, sizeof(fname), "%s.ppm", filename);
103
104     f = fopen(fname, "w");
105     if (!f) {
106         perror(fname);
107         return;
108     }
109     fprintf(f, "P6\n"
110             "%d %d\n"
111             "%d\n",
112             w, h, 255);
113     for(y = 0; y < h; y++) {
114         for(x = 0; x < w; x++) {
115             v = bitmap[y * w + x];
116             putc((v >> 16) & 0xff, f);
117             putc((v >> 8) & 0xff, f);
118             putc((v >> 0) & 0xff, f);
119         }
120     }
121     fclose(f);
122
123
124     snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
125
126     f = fopen(fname2, "w");
127     if (!f) {
128         perror(fname2);
129         return;
130     }
131     fprintf(f, "P5\n"
132             "%d %d\n"
133             "%d\n",
134             w, h, 255);
135     for(y = 0; y < h; y++) {
136         for(x = 0; x < w; x++) {
137             v = bitmap[y * w + x];
138             putc((v >> 24) & 0xff, f);
139         }
140     }
141     fclose(f);
142
143     snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
144     system(command);
145
146     snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
147     system(command);
148 }
149 #endif
150
151 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
152
153 typedef struct DVBSubCLUT {
154     int id;
155     int version;
156
157     uint32_t clut4[4];
158     uint32_t clut16[16];
159     uint32_t clut256[256];
160
161     struct DVBSubCLUT *next;
162 } DVBSubCLUT;
163
164 static DVBSubCLUT default_clut;
165
166 typedef struct DVBSubObjectDisplay {
167     int object_id;
168     int region_id;
169
170     int x_pos;
171     int y_pos;
172
173     int fgcolor;
174     int bgcolor;
175
176     struct DVBSubObjectDisplay *region_list_next;
177     struct DVBSubObjectDisplay *object_list_next;
178 } DVBSubObjectDisplay;
179
180 typedef struct DVBSubObject {
181     int id;
182     int version;
183
184     int type;
185
186     DVBSubObjectDisplay *display_list;
187
188     struct DVBSubObject *next;
189 } DVBSubObject;
190
191 typedef struct DVBSubRegionDisplay {
192     int region_id;
193
194     int x_pos;
195     int y_pos;
196
197     struct DVBSubRegionDisplay *next;
198 } DVBSubRegionDisplay;
199
200 typedef struct DVBSubRegion {
201     int id;
202     int version;
203
204     int width;
205     int height;
206     int depth;
207
208     int clut;
209     int bgcolor;
210
211     uint8_t *pbuf;
212     int buf_size;
213     int dirty;
214
215     DVBSubObjectDisplay *display_list;
216
217     struct DVBSubRegion *next;
218 } DVBSubRegion;
219
220 typedef struct DVBSubDisplayDefinition {
221     int version;
222
223     int x;
224     int y;
225     int width;
226     int height;
227 } DVBSubDisplayDefinition;
228
229 typedef struct DVBSubContext {
230     int composition_id;
231     int ancillary_id;
232
233     int version;
234     int time_out;
235     DVBSubRegion *region_list;
236     DVBSubCLUT   *clut_list;
237     DVBSubObject *object_list;
238
239     DVBSubRegionDisplay *display_list;
240     DVBSubDisplayDefinition *display_definition;
241 } DVBSubContext;
242
243
244 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
245 {
246     DVBSubObject *ptr = ctx->object_list;
247
248     while (ptr && ptr->id != object_id) {
249         ptr = ptr->next;
250     }
251
252     return ptr;
253 }
254
255 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
256 {
257     DVBSubCLUT *ptr = ctx->clut_list;
258
259     while (ptr && ptr->id != clut_id) {
260         ptr = ptr->next;
261     }
262
263     return ptr;
264 }
265
266 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
267 {
268     DVBSubRegion *ptr = ctx->region_list;
269
270     while (ptr && ptr->id != region_id) {
271         ptr = ptr->next;
272     }
273
274     return ptr;
275 }
276
277 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
278 {
279     DVBSubObject *object, *obj2, **obj2_ptr;
280     DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
281
282     while (region->display_list) {
283         display = region->display_list;
284
285         object = get_object(ctx, display->object_id);
286
287         if (object) {
288             obj_disp_ptr = &object->display_list;
289             obj_disp = *obj_disp_ptr;
290
291             while (obj_disp && obj_disp != display) {
292                 obj_disp_ptr = &obj_disp->object_list_next;
293                 obj_disp = *obj_disp_ptr;
294             }
295
296             if (obj_disp) {
297                 *obj_disp_ptr = obj_disp->object_list_next;
298
299                 if (!object->display_list) {
300                     obj2_ptr = &ctx->object_list;
301                     obj2 = *obj2_ptr;
302
303                     while (obj2 != object) {
304                         assert(obj2);
305                         obj2_ptr = &obj2->next;
306                         obj2 = *obj2_ptr;
307                     }
308
309                     *obj2_ptr = obj2->next;
310
311                     av_free(obj2);
312                 }
313             }
314         }
315
316         region->display_list = display->region_list_next;
317
318         av_free(display);
319     }
320
321 }
322
323 static void delete_cluts(DVBSubContext *ctx)
324 {
325     DVBSubCLUT *clut;
326
327     while (ctx->clut_list) {
328         clut = ctx->clut_list;
329
330         ctx->clut_list = clut->next;
331
332         av_free(clut);
333     }
334 }
335
336 static void delete_objects(DVBSubContext *ctx)
337 {
338     DVBSubObject *object;
339
340     while (ctx->object_list) {
341         object = ctx->object_list;
342
343         ctx->object_list = object->next;
344
345         av_free(object);
346     }
347 }
348
349 static void delete_regions(DVBSubContext *ctx)
350 {
351     DVBSubRegion *region;
352
353     while (ctx->region_list) {
354         region = ctx->region_list;
355
356         ctx->region_list = region->next;
357
358         delete_region_display_list(ctx, region);
359
360         av_free(region->pbuf);
361         av_free(region);
362     }
363 }
364
365 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
366 {
367     int i, r, g, b, a = 0;
368     DVBSubContext *ctx = avctx->priv_data;
369
370     if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
371         av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
372         ctx->composition_id = -1;
373         ctx->ancillary_id   = -1;
374     } else {
375         if (avctx->extradata_size > 5) {
376             av_log(avctx, AV_LOG_WARNING, "Decoding first DVB subtitles sub-stream\n");
377         }
378
379         ctx->composition_id = AV_RB16(avctx->extradata);
380         ctx->ancillary_id   = AV_RB16(avctx->extradata + 2);
381     }
382
383     ctx->version = -1;
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_free(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                 if (map_table)
745                     bits = map_table[bits];
746                 else while (run_length-- > 0 && pixels_read < dbuf_len) {
747                     *destbuf++ = bits;
748                     pixels_read++;
749                 }
750             }
751         }
752     }
753
754     if (*(*srcbuf)++)
755         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
756
757     return pixels_read;
758 }
759
760
761
762 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
763                                           const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
764 {
765     DVBSubContext *ctx = avctx->priv_data;
766
767     DVBSubRegion *region = get_region(ctx, display->region_id);
768     const uint8_t *buf_end = buf + buf_size;
769     uint8_t *pbuf;
770     int x_pos, y_pos;
771     int i;
772
773     uint8_t map2to4[] = { 0x0,  0x7,  0x8,  0xf};
774     uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
775     uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
776                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
777     uint8_t *map_table;
778
779 #if 0
780     av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
781             top_bottom ? "bottom" : "top");
782
783     for (i = 0; i < buf_size; i++) {
784         if (i % 16 == 0)
785             av_dlog(avctx, "0x%8p: ", buf+i);
786
787         av_dlog(avctx, "%02x ", buf[i]);
788         if (i % 16 == 15)
789             av_dlog(avctx, "\n");
790     }
791
792     if (i % 16)
793         av_dlog(avctx, "\n");
794 #endif
795
796     if (region == 0)
797         return;
798
799     pbuf = region->pbuf;
800     region->dirty = 1;
801
802     x_pos = display->x_pos;
803     y_pos = display->y_pos;
804
805     y_pos += top_bottom;
806
807     while (buf < buf_end) {
808         if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
809             av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
810             return;
811         }
812
813         switch (*buf++) {
814         case 0x10:
815             if (region->depth == 8)
816                 map_table = map2to8;
817             else if (region->depth == 4)
818                 map_table = map2to4;
819             else
820                 map_table = NULL;
821
822             x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width),
823                                             region->width, &buf, buf_end - buf,
824                                             non_mod, map_table, x_pos);
825             break;
826         case 0x11:
827             if (region->depth < 4) {
828                 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
829                 return;
830             }
831
832             if (region->depth == 8)
833                 map_table = map4to8;
834             else
835                 map_table = NULL;
836
837             x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width),
838                                             region->width, &buf, buf_end - buf,
839                                             non_mod, map_table, x_pos);
840             break;
841         case 0x12:
842             if (region->depth < 8) {
843                 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
844                 return;
845             }
846
847             x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width),
848                                             region->width, &buf, buf_end - buf,
849                                             non_mod, NULL, x_pos);
850             break;
851
852         case 0x20:
853             map2to4[0] = (*buf) >> 4;
854             map2to4[1] = (*buf++) & 0xf;
855             map2to4[2] = (*buf) >> 4;
856             map2to4[3] = (*buf++) & 0xf;
857             break;
858         case 0x21:
859             for (i = 0; i < 4; i++)
860                 map2to8[i] = *buf++;
861             break;
862         case 0x22:
863             for (i = 0; i < 16; i++)
864                 map4to8[i] = *buf++;
865             break;
866
867         case 0xf0:
868             x_pos = display->x_pos;
869             y_pos += 2;
870             break;
871         default:
872             av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
873         }
874     }
875
876 }
877
878 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
879                                         const uint8_t *buf, int buf_size)
880 {
881     DVBSubContext *ctx = avctx->priv_data;
882
883     const uint8_t *buf_end = buf + buf_size;
884     int object_id;
885     DVBSubObject *object;
886     DVBSubObjectDisplay *display;
887     int top_field_len, bottom_field_len;
888
889     int coding_method, non_modifying_color;
890
891     object_id = AV_RB16(buf);
892     buf += 2;
893
894     object = get_object(ctx, object_id);
895
896     if (!object)
897         return;
898
899     coding_method = ((*buf) >> 2) & 3;
900     non_modifying_color = ((*buf++) >> 1) & 1;
901
902     if (coding_method == 0) {
903         top_field_len = AV_RB16(buf);
904         buf += 2;
905         bottom_field_len = AV_RB16(buf);
906         buf += 2;
907
908         if (buf + top_field_len + bottom_field_len > buf_end) {
909             av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
910             return;
911         }
912
913         for (display = object->display_list; display; display = display->object_list_next) {
914             const uint8_t *block = buf;
915             int bfl = bottom_field_len;
916
917             dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
918                                             non_modifying_color);
919
920             if (bottom_field_len > 0)
921                 block = buf + top_field_len;
922             else
923                 bfl = top_field_len;
924
925             dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
926                                             non_modifying_color);
927         }
928
929 /*  } else if (coding_method == 1) {*/
930
931     } else {
932         av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
933     }
934
935 }
936
937 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
938                                         const uint8_t *buf, int buf_size)
939 {
940     DVBSubContext *ctx = avctx->priv_data;
941
942     const uint8_t *buf_end = buf + buf_size;
943     int i, clut_id;
944     int version;
945     DVBSubCLUT *clut;
946     int entry_id, depth , full_range;
947     int y, cr, cb, alpha;
948     int r, g, b, r_add, g_add, b_add;
949
950     av_dlog(avctx, "DVB clut packet:\n");
951
952     for (i=0; i < buf_size; i++) {
953         av_dlog(avctx, "%02x ", buf[i]);
954         if (i % 16 == 15)
955             av_dlog(avctx, "\n");
956     }
957
958     if (i % 16)
959         av_dlog(avctx, "\n");
960
961     clut_id = *buf++;
962     version = ((*buf)>>4)&15;
963     buf += 1;
964
965     clut = get_clut(ctx, clut_id);
966
967     if (!clut) {
968         clut = av_malloc(sizeof(DVBSubCLUT));
969
970         memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
971
972         clut->id = clut_id;
973         clut->version = -1;
974
975         clut->next = ctx->clut_list;
976         ctx->clut_list = clut;
977     }
978
979     if (clut->version != version) {
980
981     clut->version = version;
982
983     while (buf + 4 < buf_end) {
984         entry_id = *buf++;
985
986         depth = (*buf) & 0xe0;
987
988         if (depth == 0) {
989             av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
990             return 0;
991         }
992
993         full_range = (*buf++) & 1;
994
995         if (full_range) {
996             y = *buf++;
997             cr = *buf++;
998             cb = *buf++;
999             alpha = *buf++;
1000         } else {
1001             y = buf[0] & 0xfc;
1002             cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1003             cb = (buf[1] << 2) & 0xf0;
1004             alpha = (buf[1] << 6) & 0xc0;
1005
1006             buf += 2;
1007         }
1008
1009         if (y == 0)
1010             alpha = 0xff;
1011
1012         YUV_TO_RGB1_CCIR(cb, cr);
1013         YUV_TO_RGB2_CCIR(r, g, b, y);
1014
1015         av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1016         if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1017             av_dlog(avctx, "More than one bit level marked: %x\n", depth);
1018             if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
1019                 return AVERROR_INVALIDDATA;
1020         }
1021
1022         if (depth & 0x80)
1023             clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1024         else if (depth & 0x40)
1025             clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1026         else if (depth & 0x20)
1027             clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1028     }
1029     }
1030     return 0;
1031 }
1032
1033
1034 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1035                                         const uint8_t *buf, int buf_size)
1036 {
1037     DVBSubContext *ctx = avctx->priv_data;
1038
1039     const uint8_t *buf_end = buf + buf_size;
1040     int region_id, object_id;
1041     int av_unused version;
1042     DVBSubRegion *region;
1043     DVBSubObject *object;
1044     DVBSubObjectDisplay *display;
1045     int fill;
1046
1047     if (buf_size < 10)
1048         return;
1049
1050     region_id = *buf++;
1051
1052     region = get_region(ctx, region_id);
1053
1054     if (!region) {
1055         region = av_mallocz(sizeof(DVBSubRegion));
1056
1057         region->id = region_id;
1058         region->version = -1;
1059
1060         region->next = ctx->region_list;
1061         ctx->region_list = region;
1062     }
1063
1064     version = ((*buf)>>4) & 15;
1065     fill = ((*buf++) >> 3) & 1;
1066
1067     region->width = AV_RB16(buf);
1068     buf += 2;
1069     region->height = AV_RB16(buf);
1070     buf += 2;
1071
1072     if (region->width * region->height != region->buf_size) {
1073         av_free(region->pbuf);
1074
1075         region->buf_size = region->width * region->height;
1076
1077         region->pbuf = av_malloc(region->buf_size);
1078
1079         fill = 1;
1080         region->dirty = 0;
1081     }
1082
1083     region->depth = 1 << (((*buf++) >> 2) & 7);
1084     if(region->depth<2 || region->depth>8){
1085         av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1086         region->depth= 4;
1087     }
1088     region->clut = *buf++;
1089
1090     if (region->depth == 8) {
1091         region->bgcolor = *buf++;
1092         buf += 1;
1093     } else {
1094         buf += 1;
1095
1096         if (region->depth == 4)
1097             region->bgcolor = (((*buf++) >> 4) & 15);
1098         else
1099             region->bgcolor = (((*buf++) >> 2) & 3);
1100     }
1101
1102     av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1103
1104     if (fill) {
1105         memset(region->pbuf, region->bgcolor, region->buf_size);
1106         av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1107     }
1108
1109     delete_region_display_list(ctx, region);
1110
1111     while (buf + 5 < buf_end) {
1112         object_id = AV_RB16(buf);
1113         buf += 2;
1114
1115         object = get_object(ctx, object_id);
1116
1117         if (!object) {
1118             object = av_mallocz(sizeof(DVBSubObject));
1119
1120             object->id = object_id;
1121             object->next = ctx->object_list;
1122             ctx->object_list = object;
1123         }
1124
1125         object->type = (*buf) >> 6;
1126
1127         display = av_mallocz(sizeof(DVBSubObjectDisplay));
1128
1129         display->object_id = object_id;
1130         display->region_id = region_id;
1131
1132         display->x_pos = AV_RB16(buf) & 0xfff;
1133         buf += 2;
1134         display->y_pos = AV_RB16(buf) & 0xfff;
1135         buf += 2;
1136
1137         if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1138             display->fgcolor = *buf++;
1139             display->bgcolor = *buf++;
1140         }
1141
1142         display->region_list_next = region->display_list;
1143         region->display_list = display;
1144
1145         display->object_list_next = object->display_list;
1146         object->display_list = display;
1147     }
1148 }
1149
1150 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1151                                         const uint8_t *buf, int buf_size)
1152 {
1153     DVBSubContext *ctx = avctx->priv_data;
1154     DVBSubRegionDisplay *display;
1155     DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1156
1157     const uint8_t *buf_end = buf + buf_size;
1158     int region_id;
1159     int page_state;
1160     int timeout;
1161     int version;
1162
1163     if (buf_size < 1)
1164         return;
1165
1166     timeout = *buf++;
1167     version = ((*buf)>>4) & 15;
1168     page_state = ((*buf++) >> 2) & 3;
1169
1170     if (ctx->version != version) {
1171
1172     ctx->time_out = timeout;
1173     ctx->version = version;
1174
1175     av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1176
1177     if (page_state == 1 || page_state == 2) {
1178         delete_regions(ctx);
1179         delete_objects(ctx);
1180         delete_cluts(ctx);
1181     }
1182
1183     tmp_display_list = ctx->display_list;
1184     ctx->display_list = NULL;
1185
1186     while (buf + 5 < buf_end) {
1187         region_id = *buf++;
1188         buf += 1;
1189
1190         display = tmp_display_list;
1191         tmp_ptr = &tmp_display_list;
1192
1193         while (display && display->region_id != region_id) {
1194             tmp_ptr = &display->next;
1195             display = display->next;
1196         }
1197
1198         if (!display)
1199             display = av_mallocz(sizeof(DVBSubRegionDisplay));
1200
1201         display->region_id = region_id;
1202
1203         display->x_pos = AV_RB16(buf);
1204         buf += 2;
1205         display->y_pos = AV_RB16(buf);
1206         buf += 2;
1207
1208         *tmp_ptr = display->next;
1209
1210         display->next = ctx->display_list;
1211         ctx->display_list = display;
1212
1213         av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1214     }
1215
1216     while (tmp_display_list) {
1217         display = tmp_display_list;
1218
1219         tmp_display_list = display->next;
1220
1221         av_free(display);
1222     }
1223     }
1224
1225 }
1226
1227
1228 #ifdef DEBUG
1229 static void save_display_set(DVBSubContext *ctx)
1230 {
1231     DVBSubRegion *region;
1232     DVBSubRegionDisplay *display;
1233     DVBSubCLUT *clut;
1234     uint32_t *clut_table;
1235     int x_pos, y_pos, width, height;
1236     int x, y, y_off, x_off;
1237     uint32_t *pbuf;
1238     char filename[32];
1239     static int fileno_index = 0;
1240
1241     x_pos = -1;
1242     y_pos = -1;
1243     width = 0;
1244     height = 0;
1245
1246     for (display = ctx->display_list; display; display = display->next) {
1247         region = get_region(ctx, display->region_id);
1248
1249         if (x_pos == -1) {
1250             x_pos = display->x_pos;
1251             y_pos = display->y_pos;
1252             width = region->width;
1253             height = region->height;
1254         } else {
1255             if (display->x_pos < x_pos) {
1256                 width += (x_pos - display->x_pos);
1257                 x_pos = display->x_pos;
1258             }
1259
1260             if (display->y_pos < y_pos) {
1261                 height += (y_pos - display->y_pos);
1262                 y_pos = display->y_pos;
1263             }
1264
1265             if (display->x_pos + region->width > x_pos + width) {
1266                 width = display->x_pos + region->width - x_pos;
1267             }
1268
1269             if (display->y_pos + region->height > y_pos + height) {
1270                 height = display->y_pos + region->height - y_pos;
1271             }
1272         }
1273     }
1274
1275     if (x_pos >= 0) {
1276
1277         pbuf = av_malloc(width * height * 4);
1278
1279         for (display = ctx->display_list; display; display = display->next) {
1280             region = get_region(ctx, display->region_id);
1281
1282             x_off = display->x_pos - x_pos;
1283             y_off = display->y_pos - y_pos;
1284
1285             clut = get_clut(ctx, region->clut);
1286
1287             if (clut == 0)
1288                 clut = &default_clut;
1289
1290             switch (region->depth) {
1291             case 2:
1292                 clut_table = clut->clut4;
1293                 break;
1294             case 8:
1295                 clut_table = clut->clut256;
1296                 break;
1297             case 4:
1298             default:
1299                 clut_table = clut->clut16;
1300                 break;
1301             }
1302
1303             for (y = 0; y < region->height; y++) {
1304                 for (x = 0; x < region->width; x++) {
1305                     pbuf[((y + y_off) * width) + x_off + x] =
1306                         clut_table[region->pbuf[y * region->width + x]];
1307                 }
1308             }
1309
1310         }
1311
1312         snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1313
1314         png_save2(filename, pbuf, width, height);
1315
1316         av_free(pbuf);
1317     }
1318
1319     fileno_index++;
1320 }
1321 #endif
1322
1323 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1324                                                     const uint8_t *buf,
1325                                                     int buf_size)
1326 {
1327     DVBSubContext *ctx = avctx->priv_data;
1328     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1329     int dds_version, info_byte;
1330
1331     if (buf_size < 5)
1332         return;
1333
1334     info_byte   = bytestream_get_byte(&buf);
1335     dds_version = info_byte >> 4;
1336     if (display_def && display_def->version == dds_version)
1337         return; // already have this display definition version
1338
1339     if (!display_def) {
1340         display_def             = av_mallocz(sizeof(*display_def));
1341         ctx->display_definition = display_def;
1342     }
1343     if (!display_def)
1344         return;
1345
1346     display_def->version = dds_version;
1347     display_def->x       = 0;
1348     display_def->y       = 0;
1349     display_def->width   = bytestream_get_be16(&buf) + 1;
1350     display_def->height  = bytestream_get_be16(&buf) + 1;
1351     if (!avctx->width || !avctx->height) {
1352         avctx->width  = display_def->width;
1353         avctx->height = display_def->height;
1354     }
1355
1356     if (buf_size < 13)
1357         return;
1358
1359     if (info_byte & 1<<3) { // display_window_flag
1360         display_def->x = bytestream_get_be16(&buf);
1361         display_def->width  = bytestream_get_be16(&buf) - display_def->x + 1;
1362         display_def->y = bytestream_get_be16(&buf);
1363         display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1364     }
1365 }
1366
1367 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1368                                         int buf_size, AVSubtitle *sub)
1369 {
1370     DVBSubContext *ctx = avctx->priv_data;
1371     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1372
1373     DVBSubRegion *region;
1374     DVBSubRegionDisplay *display;
1375     AVSubtitleRect *rect;
1376     DVBSubCLUT *clut;
1377     uint32_t *clut_table;
1378     int i;
1379     int offset_x=0, offset_y=0;
1380
1381     sub->end_display_time = ctx->time_out * 1000;
1382
1383     if (display_def) {
1384         offset_x = display_def->x;
1385         offset_y = display_def->y;
1386     }
1387
1388     sub->num_rects = 0;
1389     for (display = ctx->display_list; display; display = display->next)
1390     {
1391         region = get_region(ctx, display->region_id);
1392         if (region && region->dirty)
1393             sub->num_rects++;
1394     }
1395
1396     if (sub->num_rects > 0){
1397         sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1398         for(i=0; i<sub->num_rects; i++)
1399             sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1400
1401     i = 0;
1402
1403     for (display = ctx->display_list; display; display = display->next) {
1404         region = get_region(ctx, display->region_id);
1405
1406         if (!region)
1407             continue;
1408
1409         if (!region->dirty)
1410             continue;
1411
1412         rect = sub->rects[i];
1413         rect->x = display->x_pos + offset_x;
1414         rect->y = display->y_pos + offset_y;
1415         rect->w = region->width;
1416         rect->h = region->height;
1417         rect->nb_colors = (1 << region->depth);
1418         rect->type      = SUBTITLE_BITMAP;
1419         rect->pict.linesize[0] = region->width;
1420
1421         clut = get_clut(ctx, region->clut);
1422
1423         if (!clut)
1424             clut = &default_clut;
1425
1426         switch (region->depth) {
1427         case 2:
1428             clut_table = clut->clut4;
1429             break;
1430         case 8:
1431             clut_table = clut->clut256;
1432             break;
1433         case 4:
1434         default:
1435             clut_table = clut->clut16;
1436             break;
1437         }
1438
1439         rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1440         memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1441
1442         rect->pict.data[0] = av_malloc(region->buf_size);
1443         memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1444
1445         i++;
1446     }
1447     }
1448 #ifdef DEBUG
1449     save_display_set(ctx);
1450 #endif
1451
1452     return 1;
1453 }
1454
1455 static int dvbsub_decode(AVCodecContext *avctx,
1456                          void *data, int *data_size,
1457                          AVPacket *avpkt)
1458 {
1459     const uint8_t *buf = avpkt->data;
1460     int buf_size = avpkt->size;
1461     DVBSubContext *ctx = avctx->priv_data;
1462     AVSubtitle *sub = data;
1463     const uint8_t *p, *p_end;
1464     int segment_type;
1465     int page_id;
1466     int segment_length;
1467     int i;
1468     int ret;
1469     int got_segment = 0;
1470
1471     av_dlog(avctx, "DVB sub packet:\n");
1472
1473     for (i=0; i < buf_size; i++) {
1474         av_dlog(avctx, "%02x ", buf[i]);
1475         if (i % 16 == 15)
1476             av_dlog(avctx, "\n");
1477     }
1478
1479     if (i % 16)
1480         av_dlog(avctx, "\n");
1481
1482     if (buf_size <= 6 || *buf != 0x0f) {
1483         av_dlog(avctx, "incomplete or broken packet");
1484         return -1;
1485     }
1486
1487     p = buf;
1488     p_end = buf + buf_size;
1489
1490     while (p_end - p >= 6 && *p == 0x0f) {
1491         p += 1;
1492         segment_type = *p++;
1493         page_id = AV_RB16(p);
1494         p += 2;
1495         segment_length = AV_RB16(p);
1496         p += 2;
1497
1498         if (p_end - p < segment_length) {
1499             av_dlog(avctx, "incomplete or broken packet");
1500             return -1;
1501         }
1502
1503         if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1504             ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1505             switch (segment_type) {
1506             case DVBSUB_PAGE_SEGMENT:
1507                 dvbsub_parse_page_segment(avctx, p, segment_length);
1508                 got_segment |= 1;
1509                 break;
1510             case DVBSUB_REGION_SEGMENT:
1511                 dvbsub_parse_region_segment(avctx, p, segment_length);
1512                 got_segment |= 2;
1513                 break;
1514             case DVBSUB_CLUT_SEGMENT:
1515                 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1516                 if (ret < 0) return ret;
1517                 got_segment |= 4;
1518                 break;
1519             case DVBSUB_OBJECT_SEGMENT:
1520                 dvbsub_parse_object_segment(avctx, p, segment_length);
1521                 got_segment |= 8;
1522                 break;
1523             case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1524                 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1525                 break;
1526             case DVBSUB_DISPLAY_SEGMENT:
1527                 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1528                 got_segment |= 16;
1529                 break;
1530             default:
1531                 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1532                         segment_type, page_id, segment_length);
1533                 break;
1534             }
1535         }
1536
1537         p += segment_length;
1538     }
1539     // Some streams do not send a display segment but if we have all the other
1540     // segments then we need no further data.
1541     if (got_segment == 15 && sub)
1542         *data_size = dvbsub_display_end_segment(avctx, p, 0, sub);
1543
1544     return p - buf;
1545 }
1546
1547
1548 AVCodec ff_dvbsub_decoder = {
1549     .name           = "dvbsub",
1550     .long_name      = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1551     .type           = AVMEDIA_TYPE_SUBTITLE,
1552     .id             = AV_CODEC_ID_DVB_SUBTITLE,
1553     .priv_data_size = sizeof(DVBSubContext),
1554     .init           = dvbsub_init_decoder,
1555     .close          = dvbsub_close_decoder,
1556     .decode         = dvbsub_decode,
1557 };