]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvbsubdec.c
Add a lavc Makefile dependency for the elbg filter.
[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     int display_list_size;
240     DVBSubRegionDisplay *display_list;
241     DVBSubDisplayDefinition *display_definition;
242 } DVBSubContext;
243
244
245 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
246 {
247     DVBSubObject *ptr = ctx->object_list;
248
249     while (ptr && ptr->id != object_id) {
250         ptr = ptr->next;
251     }
252
253     return ptr;
254 }
255
256 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
257 {
258     DVBSubCLUT *ptr = ctx->clut_list;
259
260     while (ptr && ptr->id != clut_id) {
261         ptr = ptr->next;
262     }
263
264     return ptr;
265 }
266
267 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
268 {
269     DVBSubRegion *ptr = ctx->region_list;
270
271     while (ptr && ptr->id != region_id) {
272         ptr = ptr->next;
273     }
274
275     return ptr;
276 }
277
278 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
279 {
280     DVBSubObject *object, *obj2, **obj2_ptr;
281     DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
282
283     while (region->display_list) {
284         display = region->display_list;
285
286         object = get_object(ctx, display->object_id);
287
288         if (object) {
289             obj_disp_ptr = &object->display_list;
290             obj_disp = *obj_disp_ptr;
291
292             while (obj_disp && obj_disp != display) {
293                 obj_disp_ptr = &obj_disp->object_list_next;
294                 obj_disp = *obj_disp_ptr;
295             }
296
297             if (obj_disp) {
298                 *obj_disp_ptr = obj_disp->object_list_next;
299
300                 if (!object->display_list) {
301                     obj2_ptr = &ctx->object_list;
302                     obj2 = *obj2_ptr;
303
304                     while (obj2 != object) {
305                         assert(obj2);
306                         obj2_ptr = &obj2->next;
307                         obj2 = *obj2_ptr;
308                     }
309
310                     *obj2_ptr = obj2->next;
311
312                     av_free(obj2);
313                 }
314             }
315         }
316
317         region->display_list = display->region_list_next;
318
319         av_free(display);
320     }
321
322 }
323
324 static void delete_cluts(DVBSubContext *ctx)
325 {
326     DVBSubCLUT *clut;
327
328     while (ctx->clut_list) {
329         clut = ctx->clut_list;
330
331         ctx->clut_list = clut->next;
332
333         av_free(clut);
334     }
335 }
336
337 static void delete_objects(DVBSubContext *ctx)
338 {
339     DVBSubObject *object;
340
341     while (ctx->object_list) {
342         object = ctx->object_list;
343
344         ctx->object_list = object->next;
345
346         av_free(object);
347     }
348 }
349
350 static void delete_regions(DVBSubContext *ctx)
351 {
352     DVBSubRegion *region;
353
354     while (ctx->region_list) {
355         region = ctx->region_list;
356
357         ctx->region_list = region->next;
358
359         delete_region_display_list(ctx, region);
360
361         av_free(region->pbuf);
362         av_free(region);
363     }
364 }
365
366 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
367 {
368     int i, r, g, b, a = 0;
369     DVBSubContext *ctx = avctx->priv_data;
370
371     if (!avctx->extradata || avctx->extradata_size != 4) {
372         av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
373         ctx->composition_id = -1;
374         ctx->ancillary_id   = -1;
375     } else {
376         ctx->composition_id = AV_RB16(avctx->extradata);
377         ctx->ancillary_id   = AV_RB16(avctx->extradata + 2);
378     }
379
380     ctx->version = -1;
381
382     default_clut.id = -1;
383     default_clut.next = NULL;
384
385     default_clut.clut4[0] = RGBA(  0,   0,   0,   0);
386     default_clut.clut4[1] = RGBA(255, 255, 255, 255);
387     default_clut.clut4[2] = RGBA(  0,   0,   0, 255);
388     default_clut.clut4[3] = RGBA(127, 127, 127, 255);
389
390     default_clut.clut16[0] = RGBA(  0,   0,   0,   0);
391     for (i = 1; i < 16; i++) {
392         if (i < 8) {
393             r = (i & 1) ? 255 : 0;
394             g = (i & 2) ? 255 : 0;
395             b = (i & 4) ? 255 : 0;
396         } else {
397             r = (i & 1) ? 127 : 0;
398             g = (i & 2) ? 127 : 0;
399             b = (i & 4) ? 127 : 0;
400         }
401         default_clut.clut16[i] = RGBA(r, g, b, 255);
402     }
403
404     default_clut.clut256[0] = RGBA(  0,   0,   0,   0);
405     for (i = 1; i < 256; i++) {
406         if (i < 8) {
407             r = (i & 1) ? 255 : 0;
408             g = (i & 2) ? 255 : 0;
409             b = (i & 4) ? 255 : 0;
410             a = 63;
411         } else {
412             switch (i & 0x88) {
413             case 0x00:
414                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
415                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
416                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
417                 a = 255;
418                 break;
419             case 0x08:
420                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
421                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
422                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
423                 a = 127;
424                 break;
425             case 0x80:
426                 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
427                 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
428                 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
429                 a = 255;
430                 break;
431             case 0x88:
432                 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
433                 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
434                 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
435                 a = 255;
436                 break;
437             }
438         }
439         default_clut.clut256[i] = RGBA(r, g, b, a);
440     }
441
442     return 0;
443 }
444
445 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
446 {
447     DVBSubContext *ctx = avctx->priv_data;
448     DVBSubRegionDisplay *display;
449
450     delete_regions(ctx);
451
452     delete_objects(ctx);
453
454     delete_cluts(ctx);
455
456     av_freep(&ctx->display_definition);
457
458     while (ctx->display_list) {
459         display = ctx->display_list;
460         ctx->display_list = display->next;
461
462         av_free(display);
463     }
464
465     return 0;
466 }
467
468 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
469                                    const uint8_t **srcbuf, int buf_size,
470                                    int non_mod, uint8_t *map_table, int x_pos)
471 {
472     GetBitContext gb;
473
474     int bits;
475     int run_length;
476     int pixels_read = x_pos;
477
478     init_get_bits(&gb, *srcbuf, buf_size << 3);
479
480     destbuf += x_pos;
481
482     while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
483         bits = get_bits(&gb, 2);
484
485         if (bits) {
486             if (non_mod != 1 || bits != 1) {
487                 if (map_table)
488                     *destbuf++ = map_table[bits];
489                 else
490                     *destbuf++ = bits;
491             }
492             pixels_read++;
493         } else {
494             bits = get_bits1(&gb);
495             if (bits == 1) {
496                 run_length = get_bits(&gb, 3) + 3;
497                 bits = get_bits(&gb, 2);
498
499                 if (non_mod == 1 && bits == 1)
500                     pixels_read += run_length;
501                 else {
502                     if (map_table)
503                         bits = map_table[bits];
504                     while (run_length-- > 0 && pixels_read < dbuf_len) {
505                         *destbuf++ = bits;
506                         pixels_read++;
507                     }
508                 }
509             } else {
510                 bits = get_bits1(&gb);
511                 if (bits == 0) {
512                     bits = get_bits(&gb, 2);
513                     if (bits == 2) {
514                         run_length = get_bits(&gb, 4) + 12;
515                         bits = get_bits(&gb, 2);
516
517                         if (non_mod == 1 && bits == 1)
518                             pixels_read += run_length;
519                         else {
520                             if (map_table)
521                                 bits = map_table[bits];
522                             while (run_length-- > 0 && pixels_read < dbuf_len) {
523                                 *destbuf++ = bits;
524                                 pixels_read++;
525                             }
526                         }
527                     } else if (bits == 3) {
528                         run_length = get_bits(&gb, 8) + 29;
529                         bits = get_bits(&gb, 2);
530
531                         if (non_mod == 1 && bits == 1)
532                             pixels_read += run_length;
533                         else {
534                             if (map_table)
535                                 bits = map_table[bits];
536                             while (run_length-- > 0 && pixels_read < dbuf_len) {
537                                 *destbuf++ = bits;
538                                 pixels_read++;
539                             }
540                         }
541                     } else if (bits == 1) {
542                         if (map_table)
543                             bits = map_table[0];
544                         else
545                             bits = 0;
546                         run_length = 2;
547                         while (run_length-- > 0 && pixels_read < dbuf_len) {
548                             *destbuf++ = bits;
549                             pixels_read++;
550                         }
551                     } else {
552                         (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
553                         return pixels_read;
554                     }
555                 } else {
556                     if (map_table)
557                         bits = map_table[0];
558                     else
559                         bits = 0;
560                     *destbuf++ = bits;
561                     pixels_read++;
562                 }
563             }
564         }
565     }
566
567     if (get_bits(&gb, 6))
568         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
569
570     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
571
572     return pixels_read;
573 }
574
575 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
576                                    const uint8_t **srcbuf, int buf_size,
577                                    int non_mod, uint8_t *map_table, int x_pos)
578 {
579     GetBitContext gb;
580
581     int bits;
582     int run_length;
583     int pixels_read = x_pos;
584
585     init_get_bits(&gb, *srcbuf, buf_size << 3);
586
587     destbuf += x_pos;
588
589     while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
590         bits = get_bits(&gb, 4);
591
592         if (bits) {
593             if (non_mod != 1 || bits != 1) {
594                 if (map_table)
595                     *destbuf++ = map_table[bits];
596                 else
597                     *destbuf++ = bits;
598             }
599             pixels_read++;
600         } else {
601             bits = get_bits1(&gb);
602             if (bits == 0) {
603                 run_length = get_bits(&gb, 3);
604
605                 if (run_length == 0) {
606                     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
607                     return pixels_read;
608                 }
609
610                 run_length += 2;
611
612                 if (map_table)
613                     bits = map_table[0];
614                 else
615                     bits = 0;
616
617                 while (run_length-- > 0 && pixels_read < dbuf_len) {
618                     *destbuf++ = bits;
619                     pixels_read++;
620                 }
621             } else {
622                 bits = get_bits1(&gb);
623                 if (bits == 0) {
624                     run_length = get_bits(&gb, 2) + 4;
625                     bits = get_bits(&gb, 4);
626
627                     if (non_mod == 1 && bits == 1)
628                         pixels_read += run_length;
629                     else {
630                         if (map_table)
631                             bits = map_table[bits];
632                         while (run_length-- > 0 && pixels_read < dbuf_len) {
633                             *destbuf++ = bits;
634                             pixels_read++;
635                         }
636                     }
637                 } else {
638                     bits = get_bits(&gb, 2);
639                     if (bits == 2) {
640                         run_length = get_bits(&gb, 4) + 9;
641                         bits = get_bits(&gb, 4);
642
643                         if (non_mod == 1 && bits == 1)
644                             pixels_read += run_length;
645                         else {
646                             if (map_table)
647                                 bits = map_table[bits];
648                             while (run_length-- > 0 && pixels_read < dbuf_len) {
649                                 *destbuf++ = bits;
650                                 pixels_read++;
651                             }
652                         }
653                     } else if (bits == 3) {
654                         run_length = get_bits(&gb, 8) + 25;
655                         bits = get_bits(&gb, 4);
656
657                         if (non_mod == 1 && bits == 1)
658                             pixels_read += run_length;
659                         else {
660                             if (map_table)
661                                 bits = map_table[bits];
662                             while (run_length-- > 0 && pixels_read < dbuf_len) {
663                                 *destbuf++ = bits;
664                                 pixels_read++;
665                             }
666                         }
667                     } else if (bits == 1) {
668                         if (map_table)
669                             bits = map_table[0];
670                         else
671                             bits = 0;
672                         run_length = 2;
673                         while (run_length-- > 0 && pixels_read < dbuf_len) {
674                             *destbuf++ = bits;
675                             pixels_read++;
676                         }
677                     } else {
678                         if (map_table)
679                             bits = map_table[0];
680                         else
681                             bits = 0;
682                         *destbuf++ = bits;
683                         pixels_read ++;
684                     }
685                 }
686             }
687         }
688     }
689
690     if (get_bits(&gb, 8))
691         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
692
693     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
694
695     return pixels_read;
696 }
697
698 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
699                                     const uint8_t **srcbuf, int buf_size,
700                                     int non_mod, uint8_t *map_table, int x_pos)
701 {
702     const uint8_t *sbuf_end = (*srcbuf) + buf_size;
703     int bits;
704     int run_length;
705     int pixels_read = x_pos;
706
707     destbuf += x_pos;
708
709     while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
710         bits = *(*srcbuf)++;
711
712         if (bits) {
713             if (non_mod != 1 || bits != 1) {
714                 if (map_table)
715                     *destbuf++ = map_table[bits];
716                 else
717                     *destbuf++ = bits;
718             }
719             pixels_read++;
720         } else {
721             bits = *(*srcbuf)++;
722             run_length = bits & 0x7f;
723             if ((bits & 0x80) == 0) {
724                 if (run_length == 0) {
725                     return pixels_read;
726                 }
727
728                 if (map_table)
729                     bits = map_table[0];
730                 else
731                     bits = 0;
732                 while (run_length-- > 0 && pixels_read < dbuf_len) {
733                     *destbuf++ = bits;
734                     pixels_read++;
735                 }
736             } else {
737                 bits = *(*srcbuf)++;
738
739                 if (non_mod == 1 && bits == 1)
740                     pixels_read += run_length;
741                 if (map_table)
742                     bits = map_table[bits];
743                 else while (run_length-- > 0 && pixels_read < dbuf_len) {
744                     *destbuf++ = bits;
745                     pixels_read++;
746                 }
747             }
748         }
749     }
750
751     if (*(*srcbuf)++)
752         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
753
754     return pixels_read;
755 }
756
757
758
759 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
760                                           const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
761 {
762     DVBSubContext *ctx = avctx->priv_data;
763
764     DVBSubRegion *region = get_region(ctx, display->region_id);
765     const uint8_t *buf_end = buf + buf_size;
766     uint8_t *pbuf;
767     int x_pos, y_pos;
768     int i;
769
770     uint8_t map2to4[] = { 0x0,  0x7,  0x8,  0xf};
771     uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
772     uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
773                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
774     uint8_t *map_table;
775
776 #if 0
777     av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
778             top_bottom ? "bottom" : "top");
779
780     for (i = 0; i < buf_size; i++) {
781         if (i % 16 == 0)
782             av_dlog(avctx, "0x%8p: ", buf+i);
783
784         av_dlog(avctx, "%02x ", buf[i]);
785         if (i % 16 == 15)
786             av_dlog(avctx, "\n");
787     }
788
789     if (i % 16)
790         av_dlog(avctx, "\n");
791 #endif
792
793     if (region == 0)
794         return;
795
796     pbuf = region->pbuf;
797     region->dirty = 1;
798
799     x_pos = display->x_pos;
800     y_pos = display->y_pos;
801
802     y_pos += top_bottom;
803
804     while (buf < buf_end) {
805         if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
806             av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
807             return;
808         }
809
810         switch (*buf++) {
811         case 0x10:
812             if (region->depth == 8)
813                 map_table = map2to8;
814             else if (region->depth == 4)
815                 map_table = map2to4;
816             else
817                 map_table = NULL;
818
819             x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width),
820                                             region->width, &buf, buf_end - buf,
821                                             non_mod, map_table, x_pos);
822             break;
823         case 0x11:
824             if (region->depth < 4) {
825                 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
826                 return;
827             }
828
829             if (region->depth == 8)
830                 map_table = map4to8;
831             else
832                 map_table = NULL;
833
834             x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width),
835                                             region->width, &buf, buf_end - buf,
836                                             non_mod, map_table, x_pos);
837             break;
838         case 0x12:
839             if (region->depth < 8) {
840                 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
841                 return;
842             }
843
844             x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width),
845                                             region->width, &buf, buf_end - buf,
846                                             non_mod, NULL, x_pos);
847             break;
848
849         case 0x20:
850             map2to4[0] = (*buf) >> 4;
851             map2to4[1] = (*buf++) & 0xf;
852             map2to4[2] = (*buf) >> 4;
853             map2to4[3] = (*buf++) & 0xf;
854             break;
855         case 0x21:
856             for (i = 0; i < 4; i++)
857                 map2to8[i] = *buf++;
858             break;
859         case 0x22:
860             for (i = 0; i < 16; i++)
861                 map4to8[i] = *buf++;
862             break;
863
864         case 0xf0:
865             x_pos = display->x_pos;
866             y_pos += 2;
867             break;
868         default:
869             av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
870         }
871     }
872
873 }
874
875 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
876                                         const uint8_t *buf, int buf_size)
877 {
878     DVBSubContext *ctx = avctx->priv_data;
879
880     const uint8_t *buf_end = buf + buf_size;
881     int object_id;
882     DVBSubObject *object;
883     DVBSubObjectDisplay *display;
884     int top_field_len, bottom_field_len;
885
886     int coding_method, non_modifying_color;
887
888     object_id = AV_RB16(buf);
889     buf += 2;
890
891     object = get_object(ctx, object_id);
892
893     if (!object)
894         return;
895
896     coding_method = ((*buf) >> 2) & 3;
897     non_modifying_color = ((*buf++) >> 1) & 1;
898
899     if (coding_method == 0) {
900         top_field_len = AV_RB16(buf);
901         buf += 2;
902         bottom_field_len = AV_RB16(buf);
903         buf += 2;
904
905         if (buf + top_field_len + bottom_field_len > buf_end) {
906             av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
907             return;
908         }
909
910         for (display = object->display_list; display; display = display->object_list_next) {
911             const uint8_t *block = buf;
912             int bfl = bottom_field_len;
913
914             dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
915                                             non_modifying_color);
916
917             if (bottom_field_len > 0)
918                 block = buf + top_field_len;
919             else
920                 bfl = top_field_len;
921
922             dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
923                                             non_modifying_color);
924         }
925
926 /*  } else if (coding_method == 1) {*/
927
928     } else {
929         av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
930     }
931
932 }
933
934 static int dvbsub_parse_clut_segment(AVCodecContext *avctx,
935                                         const uint8_t *buf, int buf_size)
936 {
937     DVBSubContext *ctx = avctx->priv_data;
938
939     const uint8_t *buf_end = buf + buf_size;
940     int i, clut_id;
941     int version;
942     DVBSubCLUT *clut;
943     int entry_id, depth , full_range;
944     int y, cr, cb, alpha;
945     int r, g, b, r_add, g_add, b_add;
946
947     av_dlog(avctx, "DVB clut packet:\n");
948
949     for (i=0; i < buf_size; i++) {
950         av_dlog(avctx, "%02x ", buf[i]);
951         if (i % 16 == 15)
952             av_dlog(avctx, "\n");
953     }
954
955     if (i % 16)
956         av_dlog(avctx, "\n");
957
958     clut_id = *buf++;
959     version = ((*buf)>>4)&15;
960     buf += 1;
961
962     clut = get_clut(ctx, clut_id);
963
964     if (!clut) {
965         clut = av_malloc(sizeof(DVBSubCLUT));
966
967         memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
968
969         clut->id = clut_id;
970         clut->version = -1;
971
972         clut->next = ctx->clut_list;
973         ctx->clut_list = clut;
974     }
975
976     if (clut->version != version) {
977
978     clut->version = version;
979
980     while (buf + 4 < buf_end) {
981         entry_id = *buf++;
982
983         depth = (*buf) & 0xe0;
984
985         if (depth == 0) {
986             av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
987             return 0;
988         }
989
990         full_range = (*buf++) & 1;
991
992         if (full_range) {
993             y = *buf++;
994             cr = *buf++;
995             cb = *buf++;
996             alpha = *buf++;
997         } else {
998             y = buf[0] & 0xfc;
999             cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1000             cb = (buf[1] << 2) & 0xf0;
1001             alpha = (buf[1] << 6) & 0xc0;
1002
1003             buf += 2;
1004         }
1005
1006         if (y == 0)
1007             alpha = 0xff;
1008
1009         YUV_TO_RGB1_CCIR(cb, cr);
1010         YUV_TO_RGB2_CCIR(r, g, b, y);
1011
1012         av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1013         if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1014             av_dlog(avctx, "More than one bit level marked: %x\n", depth);
1015             if (avctx->strict_std_compliance > FF_COMPLIANCE_NORMAL)
1016                 return AVERROR_INVALIDDATA;
1017         }
1018
1019         if (depth & 0x80)
1020             clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1021         else if (depth & 0x40)
1022             clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1023         else if (depth & 0x20)
1024             clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1025     }
1026     }
1027     return 0;
1028 }
1029
1030
1031 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1032                                         const uint8_t *buf, int buf_size)
1033 {
1034     DVBSubContext *ctx = avctx->priv_data;
1035
1036     const uint8_t *buf_end = buf + buf_size;
1037     int region_id, object_id;
1038     int av_unused version;
1039     DVBSubRegion *region;
1040     DVBSubObject *object;
1041     DVBSubObjectDisplay *display;
1042     int fill;
1043
1044     if (buf_size < 10)
1045         return;
1046
1047     region_id = *buf++;
1048
1049     region = get_region(ctx, region_id);
1050
1051     if (!region) {
1052         region = av_mallocz(sizeof(DVBSubRegion));
1053
1054         region->id = region_id;
1055         region->version = -1;
1056
1057         region->next = ctx->region_list;
1058         ctx->region_list = region;
1059     }
1060
1061     version = ((*buf)>>4) & 15;
1062     fill = ((*buf++) >> 3) & 1;
1063
1064     region->width = AV_RB16(buf);
1065     buf += 2;
1066     region->height = AV_RB16(buf);
1067     buf += 2;
1068
1069     if (region->width * region->height != region->buf_size) {
1070         av_free(region->pbuf);
1071
1072         region->buf_size = region->width * region->height;
1073
1074         region->pbuf = av_malloc(region->buf_size);
1075
1076         fill = 1;
1077         region->dirty = 0;
1078     }
1079
1080     region->depth = 1 << (((*buf++) >> 2) & 7);
1081     if(region->depth<2 || region->depth>8){
1082         av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1083         region->depth= 4;
1084     }
1085     region->clut = *buf++;
1086
1087     if (region->depth == 8) {
1088         region->bgcolor = *buf++;
1089         buf += 1;
1090     } else {
1091         buf += 1;
1092
1093         if (region->depth == 4)
1094             region->bgcolor = (((*buf++) >> 4) & 15);
1095         else
1096             region->bgcolor = (((*buf++) >> 2) & 3);
1097     }
1098
1099     av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1100
1101     if (fill) {
1102         memset(region->pbuf, region->bgcolor, region->buf_size);
1103         av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1104     }
1105
1106     delete_region_display_list(ctx, region);
1107
1108     while (buf + 5 < buf_end) {
1109         object_id = AV_RB16(buf);
1110         buf += 2;
1111
1112         object = get_object(ctx, object_id);
1113
1114         if (!object) {
1115             object = av_mallocz(sizeof(DVBSubObject));
1116
1117             object->id = object_id;
1118             object->next = ctx->object_list;
1119             ctx->object_list = object;
1120         }
1121
1122         object->type = (*buf) >> 6;
1123
1124         display = av_mallocz(sizeof(DVBSubObjectDisplay));
1125
1126         display->object_id = object_id;
1127         display->region_id = region_id;
1128
1129         display->x_pos = AV_RB16(buf) & 0xfff;
1130         buf += 2;
1131         display->y_pos = AV_RB16(buf) & 0xfff;
1132         buf += 2;
1133
1134         if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1135             display->fgcolor = *buf++;
1136             display->bgcolor = *buf++;
1137         }
1138
1139         display->region_list_next = region->display_list;
1140         region->display_list = display;
1141
1142         display->object_list_next = object->display_list;
1143         object->display_list = display;
1144     }
1145 }
1146
1147 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1148                                         const uint8_t *buf, int buf_size)
1149 {
1150     DVBSubContext *ctx = avctx->priv_data;
1151     DVBSubRegionDisplay *display;
1152     DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1153
1154     const uint8_t *buf_end = buf + buf_size;
1155     int region_id;
1156     int page_state;
1157     int timeout;
1158     int version;
1159
1160     if (buf_size < 1)
1161         return;
1162
1163     timeout = *buf++;
1164     version = ((*buf)>>4) & 15;
1165     page_state = ((*buf++) >> 2) & 3;
1166
1167     if (ctx->version != version) {
1168
1169     ctx->time_out = timeout;
1170     ctx->version = version;
1171
1172     av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1173
1174     if (page_state == 1 || page_state == 2) {
1175         delete_regions(ctx);
1176         delete_objects(ctx);
1177         delete_cluts(ctx);
1178     }
1179
1180     tmp_display_list = ctx->display_list;
1181     ctx->display_list = NULL;
1182     ctx->display_list_size = 0;
1183
1184     while (buf + 5 < buf_end) {
1185         region_id = *buf++;
1186         buf += 1;
1187
1188         display = tmp_display_list;
1189         tmp_ptr = &tmp_display_list;
1190
1191         while (display && display->region_id != region_id) {
1192             tmp_ptr = &display->next;
1193             display = display->next;
1194         }
1195
1196         if (!display)
1197             display = av_mallocz(sizeof(DVBSubRegionDisplay));
1198
1199         display->region_id = region_id;
1200
1201         display->x_pos = AV_RB16(buf);
1202         buf += 2;
1203         display->y_pos = AV_RB16(buf);
1204         buf += 2;
1205
1206         *tmp_ptr = display->next;
1207
1208         display->next = ctx->display_list;
1209         ctx->display_list = display;
1210         ctx->display_list_size++;
1211
1212         av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1213     }
1214
1215     while (tmp_display_list) {
1216         display = tmp_display_list;
1217
1218         tmp_display_list = display->next;
1219
1220         av_free(display);
1221     }
1222     }
1223
1224 }
1225
1226
1227 #ifdef DEBUG
1228 static void save_display_set(DVBSubContext *ctx)
1229 {
1230     DVBSubRegion *region;
1231     DVBSubRegionDisplay *display;
1232     DVBSubCLUT *clut;
1233     uint32_t *clut_table;
1234     int x_pos, y_pos, width, height;
1235     int x, y, y_off, x_off;
1236     uint32_t *pbuf;
1237     char filename[32];
1238     static int fileno_index = 0;
1239
1240     x_pos = -1;
1241     y_pos = -1;
1242     width = 0;
1243     height = 0;
1244
1245     for (display = ctx->display_list; display; display = display->next) {
1246         region = get_region(ctx, display->region_id);
1247
1248         if (x_pos == -1) {
1249             x_pos = display->x_pos;
1250             y_pos = display->y_pos;
1251             width = region->width;
1252             height = region->height;
1253         } else {
1254             if (display->x_pos < x_pos) {
1255                 width += (x_pos - display->x_pos);
1256                 x_pos = display->x_pos;
1257             }
1258
1259             if (display->y_pos < y_pos) {
1260                 height += (y_pos - display->y_pos);
1261                 y_pos = display->y_pos;
1262             }
1263
1264             if (display->x_pos + region->width > x_pos + width) {
1265                 width = display->x_pos + region->width - x_pos;
1266             }
1267
1268             if (display->y_pos + region->height > y_pos + height) {
1269                 height = display->y_pos + region->height - y_pos;
1270             }
1271         }
1272     }
1273
1274     if (x_pos >= 0) {
1275
1276         pbuf = av_malloc(width * height * 4);
1277
1278         for (display = ctx->display_list; display; display = display->next) {
1279             region = get_region(ctx, display->region_id);
1280
1281             x_off = display->x_pos - x_pos;
1282             y_off = display->y_pos - y_pos;
1283
1284             clut = get_clut(ctx, region->clut);
1285
1286             if (clut == 0)
1287                 clut = &default_clut;
1288
1289             switch (region->depth) {
1290             case 2:
1291                 clut_table = clut->clut4;
1292                 break;
1293             case 8:
1294                 clut_table = clut->clut256;
1295                 break;
1296             case 4:
1297             default:
1298                 clut_table = clut->clut16;
1299                 break;
1300             }
1301
1302             for (y = 0; y < region->height; y++) {
1303                 for (x = 0; x < region->width; x++) {
1304                     pbuf[((y + y_off) * width) + x_off + x] =
1305                         clut_table[region->pbuf[y * region->width + x]];
1306                 }
1307             }
1308
1309         }
1310
1311         snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1312
1313         png_save2(filename, pbuf, width, height);
1314
1315         av_free(pbuf);
1316     }
1317
1318     fileno_index++;
1319 }
1320 #endif
1321
1322 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1323                                                     const uint8_t *buf,
1324                                                     int buf_size)
1325 {
1326     DVBSubContext *ctx = avctx->priv_data;
1327     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1328     int dds_version, info_byte;
1329
1330     if (buf_size < 5)
1331         return;
1332
1333     info_byte   = bytestream_get_byte(&buf);
1334     dds_version = info_byte >> 4;
1335     if (display_def && display_def->version == dds_version)
1336         return; // already have this display definition version
1337
1338     if (!display_def) {
1339         display_def             = av_mallocz(sizeof(*display_def));
1340         ctx->display_definition = display_def;
1341     }
1342     if (!display_def)
1343         return;
1344
1345     display_def->version = dds_version;
1346     display_def->x       = 0;
1347     display_def->y       = 0;
1348     display_def->width   = bytestream_get_be16(&buf) + 1;
1349     display_def->height  = bytestream_get_be16(&buf) + 1;
1350     if (!avctx->width || !avctx->height) {
1351         avctx->width  = display_def->width;
1352         avctx->height = display_def->height;
1353     }
1354
1355     if (buf_size < 13)
1356         return;
1357
1358     if (info_byte & 1<<3) { // display_window_flag
1359         display_def->x = bytestream_get_be16(&buf);
1360         display_def->y = bytestream_get_be16(&buf);
1361         display_def->width  = bytestream_get_be16(&buf) - display_def->x + 1;
1362         display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1363     }
1364 }
1365
1366 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1367                                         int buf_size, AVSubtitle *sub)
1368 {
1369     DVBSubContext *ctx = avctx->priv_data;
1370     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1371
1372     DVBSubRegion *region;
1373     DVBSubRegionDisplay *display;
1374     AVSubtitleRect *rect;
1375     DVBSubCLUT *clut;
1376     uint32_t *clut_table;
1377     int i;
1378     int offset_x=0, offset_y=0;
1379
1380     sub->end_display_time = ctx->time_out * 1000;
1381
1382     if (display_def) {
1383         offset_x = display_def->x;
1384         offset_y = display_def->y;
1385     }
1386
1387     sub->num_rects = ctx->display_list_size;
1388
1389     if (sub->num_rects > 0){
1390         sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1391         for(i=0; i<sub->num_rects; i++)
1392             sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1393
1394     i = 0;
1395
1396     for (display = ctx->display_list; display; display = display->next) {
1397         region = get_region(ctx, display->region_id);
1398
1399         if (!region)
1400             continue;
1401
1402         if (!region->dirty)
1403             continue;
1404
1405         rect = sub->rects[i];
1406         rect->x = display->x_pos + offset_x;
1407         rect->y = display->y_pos + offset_y;
1408         rect->w = region->width;
1409         rect->h = region->height;
1410         rect->nb_colors = (1 << region->depth);
1411         rect->type      = SUBTITLE_BITMAP;
1412         rect->pict.linesize[0] = region->width;
1413
1414         clut = get_clut(ctx, region->clut);
1415
1416         if (!clut)
1417             clut = &default_clut;
1418
1419         switch (region->depth) {
1420         case 2:
1421             clut_table = clut->clut4;
1422             break;
1423         case 8:
1424             clut_table = clut->clut256;
1425             break;
1426         case 4:
1427         default:
1428             clut_table = clut->clut16;
1429             break;
1430         }
1431
1432         rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1433         memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1434
1435         rect->pict.data[0] = av_malloc(region->buf_size);
1436         memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1437
1438         i++;
1439     }
1440
1441     sub->num_rects = i;
1442     }
1443 #ifdef DEBUG
1444     save_display_set(ctx);
1445 #endif
1446
1447     return 1;
1448 }
1449
1450 static int dvbsub_decode(AVCodecContext *avctx,
1451                          void *data, int *data_size,
1452                          AVPacket *avpkt)
1453 {
1454     const uint8_t *buf = avpkt->data;
1455     int buf_size = avpkt->size;
1456     DVBSubContext *ctx = avctx->priv_data;
1457     AVSubtitle *sub = data;
1458     const uint8_t *p, *p_end;
1459     int segment_type;
1460     int page_id;
1461     int segment_length;
1462     int i;
1463     int ret;
1464     int got_segment = 0;
1465
1466     av_dlog(avctx, "DVB sub packet:\n");
1467
1468     for (i=0; i < buf_size; i++) {
1469         av_dlog(avctx, "%02x ", buf[i]);
1470         if (i % 16 == 15)
1471             av_dlog(avctx, "\n");
1472     }
1473
1474     if (i % 16)
1475         av_dlog(avctx, "\n");
1476
1477     if (buf_size <= 6 || *buf != 0x0f) {
1478         av_dlog(avctx, "incomplete or broken packet");
1479         return -1;
1480     }
1481
1482     p = buf;
1483     p_end = buf + buf_size;
1484
1485     while (p_end - p >= 6 && *p == 0x0f) {
1486         p += 1;
1487         segment_type = *p++;
1488         page_id = AV_RB16(p);
1489         p += 2;
1490         segment_length = AV_RB16(p);
1491         p += 2;
1492
1493         if (p_end - p < segment_length) {
1494             av_dlog(avctx, "incomplete or broken packet");
1495             return -1;
1496         }
1497
1498         if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1499             ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1500             switch (segment_type) {
1501             case DVBSUB_PAGE_SEGMENT:
1502                 dvbsub_parse_page_segment(avctx, p, segment_length);
1503                 got_segment |= 1;
1504                 break;
1505             case DVBSUB_REGION_SEGMENT:
1506                 dvbsub_parse_region_segment(avctx, p, segment_length);
1507                 got_segment |= 2;
1508                 break;
1509             case DVBSUB_CLUT_SEGMENT:
1510                 ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1511                 if (ret < 0) return ret;
1512                 got_segment |= 4;
1513                 break;
1514             case DVBSUB_OBJECT_SEGMENT:
1515                 dvbsub_parse_object_segment(avctx, p, segment_length);
1516                 got_segment |= 8;
1517                 break;
1518             case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1519                 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1520                 break;
1521             case DVBSUB_DISPLAY_SEGMENT:
1522                 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1523                 got_segment |= 16;
1524                 break;
1525             default:
1526                 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1527                         segment_type, page_id, segment_length);
1528                 break;
1529             }
1530         }
1531
1532         p += segment_length;
1533     }
1534     // Some streams do not send a display segment but if we have all the other
1535     // segments then we need no further data.
1536     if (got_segment == 15 && sub)
1537         *data_size = dvbsub_display_end_segment(avctx, p, 0, sub);
1538
1539     return p - buf;
1540 }
1541
1542
1543 AVCodec ff_dvbsub_decoder = {
1544     .name           = "dvbsub",
1545     .long_name      = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1546     .type           = AVMEDIA_TYPE_SUBTITLE,
1547     .id             = AV_CODEC_ID_DVB_SUBTITLE,
1548     .priv_data_size = sizeof(DVBSubContext),
1549     .init           = dvbsub_init_decoder,
1550     .close          = dvbsub_close_decoder,
1551     .decode         = dvbsub_decode,
1552 };