]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvbsubdec.c
Fix build with hardcoded tables
[ffmpeg] / libavcodec / dvbsubdec.c
1 /*
2  * DVB subtitle decoding for ffmpeg
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 DEBUG
28 //#define DEBUG_PACKET_CONTENTS
29 //#define DEBUG_SAVE_IMAGES
30
31 #define DVBSUB_PAGE_SEGMENT     0x10
32 #define DVBSUB_REGION_SEGMENT   0x11
33 #define DVBSUB_CLUT_SEGMENT     0x12
34 #define DVBSUB_OBJECT_SEGMENT   0x13
35 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
36 #define DVBSUB_DISPLAY_SEGMENT  0x80
37
38 #define cm (ff_cropTbl + MAX_NEG_CROP)
39
40 #ifdef DEBUG_SAVE_IMAGES
41 #undef fprintf
42 #if 0
43 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
44                      uint32_t *rgba_palette)
45 {
46     int x, y, v;
47     FILE *f;
48     char fname[40], fname2[40];
49     char command[1024];
50
51     snprintf(fname, 40, "%s.ppm", filename);
52
53     f = fopen(fname, "w");
54     if (!f) {
55         perror(fname);
56         exit(1);
57     }
58     fprintf(f, "P6\n"
59             "%d %d\n"
60             "%d\n",
61             w, h, 255);
62     for(y = 0; y < h; y++) {
63         for(x = 0; x < w; x++) {
64             v = rgba_palette[bitmap[y * w + x]];
65             putc((v >> 16) & 0xff, f);
66             putc((v >> 8) & 0xff, f);
67             putc((v >> 0) & 0xff, f);
68         }
69     }
70     fclose(f);
71
72
73     snprintf(fname2, 40, "%s-a.pgm", filename);
74
75     f = fopen(fname2, "w");
76     if (!f) {
77         perror(fname2);
78         exit(1);
79     }
80     fprintf(f, "P5\n"
81             "%d %d\n"
82             "%d\n",
83             w, h, 255);
84     for(y = 0; y < h; y++) {
85         for(x = 0; x < w; x++) {
86             v = rgba_palette[bitmap[y * w + x]];
87             putc((v >> 24) & 0xff, f);
88         }
89     }
90     fclose(f);
91
92     snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
93     system(command);
94
95     snprintf(command, 1024, "rm %s %s", fname, fname2);
96     system(command);
97 }
98 #endif
99
100 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
101 {
102     int x, y, v;
103     FILE *f;
104     char fname[40], fname2[40];
105     char command[1024];
106
107     snprintf(fname, sizeof(fname), "%s.ppm", filename);
108
109     f = fopen(fname, "w");
110     if (!f) {
111         perror(fname);
112         exit(1);
113     }
114     fprintf(f, "P6\n"
115             "%d %d\n"
116             "%d\n",
117             w, h, 255);
118     for(y = 0; y < h; y++) {
119         for(x = 0; x < w; x++) {
120             v = bitmap[y * w + x];
121             putc((v >> 16) & 0xff, f);
122             putc((v >> 8) & 0xff, f);
123             putc((v >> 0) & 0xff, f);
124         }
125     }
126     fclose(f);
127
128
129     snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
130
131     f = fopen(fname2, "w");
132     if (!f) {
133         perror(fname2);
134         exit(1);
135     }
136     fprintf(f, "P5\n"
137             "%d %d\n"
138             "%d\n",
139             w, h, 255);
140     for(y = 0; y < h; y++) {
141         for(x = 0; x < w; x++) {
142             v = bitmap[y * w + x];
143             putc((v >> 24) & 0xff, f);
144         }
145     }
146     fclose(f);
147
148     snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
149     system(command);
150
151     snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
152     system(command);
153 }
154 #endif
155
156 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
157
158 typedef struct DVBSubCLUT {
159     int id;
160
161     uint32_t clut4[4];
162     uint32_t clut16[16];
163     uint32_t clut256[256];
164
165     struct DVBSubCLUT *next;
166 } DVBSubCLUT;
167
168 static DVBSubCLUT default_clut;
169
170 typedef struct DVBSubObjectDisplay {
171     int object_id;
172     int region_id;
173
174     int x_pos;
175     int y_pos;
176
177     int fgcolor;
178     int bgcolor;
179
180     struct DVBSubObjectDisplay *region_list_next;
181     struct DVBSubObjectDisplay *object_list_next;
182 } DVBSubObjectDisplay;
183
184 typedef struct DVBSubObject {
185     int id;
186
187     int type;
188
189     DVBSubObjectDisplay *display_list;
190
191     struct DVBSubObject *next;
192 } DVBSubObject;
193
194 typedef struct DVBSubRegionDisplay {
195     int region_id;
196
197     int x_pos;
198     int y_pos;
199
200     struct DVBSubRegionDisplay *next;
201 } DVBSubRegionDisplay;
202
203 typedef struct DVBSubRegion {
204     int id;
205
206     int width;
207     int height;
208     int depth;
209
210     int clut;
211     int bgcolor;
212
213     uint8_t *pbuf;
214     int buf_size;
215
216     DVBSubObjectDisplay *display_list;
217
218     struct DVBSubRegion *next;
219 } DVBSubRegion;
220
221 typedef struct DVBSubDisplayDefinition {
222     int version;
223
224     int x;
225     int y;
226     int width;
227     int height;
228 } DVBSubDisplayDefinition;
229
230 typedef struct DVBSubContext {
231     int composition_id;
232     int ancillary_id;
233
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_state(DVBSubContext *ctx)
325 {
326     DVBSubRegion *region;
327     DVBSubCLUT *clut;
328
329     while (ctx->region_list) {
330         region = ctx->region_list;
331
332         ctx->region_list = region->next;
333
334         delete_region_display_list(ctx, region);
335         if (region->pbuf)
336             av_free(region->pbuf);
337
338         av_free(region);
339     }
340
341     while (ctx->clut_list) {
342         clut = ctx->clut_list;
343
344         ctx->clut_list = clut->next;
345
346         av_free(clut);
347     }
348
349     av_freep(&ctx->display_definition);
350
351     /* Should already be null */
352     if (ctx->object_list)
353         av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
354 }
355
356 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
357 {
358     int i, r, g, b, a = 0;
359     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
360
361     memset(avctx->priv_data, 0, sizeof(DVBSubContext));
362
363     ctx->composition_id = avctx->sub_id & 0xffff;
364     ctx->ancillary_id = avctx->sub_id >> 16;
365
366     default_clut.id = -1;
367     default_clut.next = NULL;
368
369     default_clut.clut4[0] = RGBA(  0,   0,   0,   0);
370     default_clut.clut4[1] = RGBA(255, 255, 255, 255);
371     default_clut.clut4[2] = RGBA(  0,   0,   0, 255);
372     default_clut.clut4[3] = RGBA(127, 127, 127, 255);
373
374     default_clut.clut16[0] = RGBA(  0,   0,   0,   0);
375     for (i = 1; i < 16; i++) {
376         if (i < 8) {
377             r = (i & 1) ? 255 : 0;
378             g = (i & 2) ? 255 : 0;
379             b = (i & 4) ? 255 : 0;
380         } else {
381             r = (i & 1) ? 127 : 0;
382             g = (i & 2) ? 127 : 0;
383             b = (i & 4) ? 127 : 0;
384         }
385         default_clut.clut16[i] = RGBA(r, g, b, 255);
386     }
387
388     default_clut.clut256[0] = RGBA(  0,   0,   0,   0);
389     for (i = 1; i < 256; i++) {
390         if (i < 8) {
391             r = (i & 1) ? 255 : 0;
392             g = (i & 2) ? 255 : 0;
393             b = (i & 4) ? 255 : 0;
394             a = 63;
395         } else {
396             switch (i & 0x88) {
397             case 0x00:
398                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
399                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
400                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
401                 a = 255;
402                 break;
403             case 0x08:
404                 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
405                 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
406                 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
407                 a = 127;
408                 break;
409             case 0x80:
410                 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
411                 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
412                 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
413                 a = 255;
414                 break;
415             case 0x88:
416                 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
417                 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
418                 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
419                 a = 255;
420                 break;
421             }
422         }
423         default_clut.clut256[i] = RGBA(r, g, b, a);
424     }
425
426     return 0;
427 }
428
429 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
430 {
431     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
432     DVBSubRegionDisplay *display;
433
434     delete_state(ctx);
435
436     while (ctx->display_list) {
437         display = ctx->display_list;
438         ctx->display_list = display->next;
439
440         av_free(display);
441     }
442
443     return 0;
444 }
445
446 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
447                                    const uint8_t **srcbuf, int buf_size,
448                                    int non_mod, uint8_t *map_table)
449 {
450     GetBitContext gb;
451
452     int bits;
453     int run_length;
454     int pixels_read = 0;
455
456     init_get_bits(&gb, *srcbuf, buf_size << 3);
457
458     while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
459         bits = get_bits(&gb, 2);
460
461         if (bits) {
462             if (non_mod != 1 || bits != 1) {
463                 if (map_table)
464                     *destbuf++ = map_table[bits];
465                 else
466                     *destbuf++ = bits;
467             }
468             pixels_read++;
469         } else {
470             bits = get_bits1(&gb);
471             if (bits == 1) {
472                 run_length = get_bits(&gb, 3) + 3;
473                 bits = get_bits(&gb, 2);
474
475                 if (non_mod == 1 && bits == 1)
476                     pixels_read += run_length;
477                 else {
478                     if (map_table)
479                         bits = map_table[bits];
480                     while (run_length-- > 0 && pixels_read < dbuf_len) {
481                         *destbuf++ = bits;
482                         pixels_read++;
483                     }
484                 }
485             } else {
486                 bits = get_bits1(&gb);
487                 if (bits == 0) {
488                     bits = get_bits(&gb, 2);
489                     if (bits == 2) {
490                         run_length = get_bits(&gb, 4) + 12;
491                         bits = get_bits(&gb, 2);
492
493                         if (non_mod == 1 && bits == 1)
494                             pixels_read += run_length;
495                         else {
496                             if (map_table)
497                                 bits = map_table[bits];
498                             while (run_length-- > 0 && pixels_read < dbuf_len) {
499                                 *destbuf++ = bits;
500                                 pixels_read++;
501                             }
502                         }
503                     } else if (bits == 3) {
504                         run_length = get_bits(&gb, 8) + 29;
505                         bits = get_bits(&gb, 2);
506
507                         if (non_mod == 1 && bits == 1)
508                             pixels_read += run_length;
509                         else {
510                             if (map_table)
511                                 bits = map_table[bits];
512                             while (run_length-- > 0 && pixels_read < dbuf_len) {
513                                 *destbuf++ = bits;
514                                 pixels_read++;
515                             }
516                         }
517                     } else if (bits == 1) {
518                         pixels_read += 2;
519                         if (map_table)
520                             bits = map_table[0];
521                         else
522                             bits = 0;
523                         if (pixels_read <= dbuf_len) {
524                             *destbuf++ = bits;
525                             *destbuf++ = bits;
526                         }
527                     } else {
528                         (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
529                         return pixels_read;
530                     }
531                 } else {
532                     if (map_table)
533                         bits = map_table[0];
534                     else
535                         bits = 0;
536                     *destbuf++ = bits;
537                     pixels_read++;
538                 }
539             }
540         }
541     }
542
543     if (get_bits(&gb, 6))
544         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
545
546     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
547
548     return pixels_read;
549 }
550
551 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
552                                    const uint8_t **srcbuf, int buf_size,
553                                    int non_mod, uint8_t *map_table)
554 {
555     GetBitContext gb;
556
557     int bits;
558     int run_length;
559     int pixels_read = 0;
560
561     init_get_bits(&gb, *srcbuf, buf_size << 3);
562
563     while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
564         bits = get_bits(&gb, 4);
565
566         if (bits) {
567             if (non_mod != 1 || bits != 1) {
568                 if (map_table)
569                     *destbuf++ = map_table[bits];
570                 else
571                     *destbuf++ = bits;
572             }
573             pixels_read++;
574         } else {
575             bits = get_bits1(&gb);
576             if (bits == 0) {
577                 run_length = get_bits(&gb, 3);
578
579                 if (run_length == 0) {
580                     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
581                     return pixels_read;
582                 }
583
584                 run_length += 2;
585
586                 if (map_table)
587                     bits = map_table[0];
588                 else
589                     bits = 0;
590
591                 while (run_length-- > 0 && pixels_read < dbuf_len) {
592                     *destbuf++ = bits;
593                     pixels_read++;
594                 }
595             } else {
596                 bits = get_bits1(&gb);
597                 if (bits == 0) {
598                     run_length = get_bits(&gb, 2) + 4;
599                     bits = get_bits(&gb, 4);
600
601                     if (non_mod == 1 && bits == 1)
602                         pixels_read += run_length;
603                     else {
604                         if (map_table)
605                             bits = map_table[bits];
606                         while (run_length-- > 0 && pixels_read < dbuf_len) {
607                             *destbuf++ = bits;
608                             pixels_read++;
609                         }
610                     }
611                 } else {
612                     bits = get_bits(&gb, 2);
613                     if (bits == 2) {
614                         run_length = get_bits(&gb, 4) + 9;
615                         bits = get_bits(&gb, 4);
616
617                         if (non_mod == 1 && bits == 1)
618                             pixels_read += run_length;
619                         else {
620                             if (map_table)
621                                 bits = map_table[bits];
622                             while (run_length-- > 0 && pixels_read < dbuf_len) {
623                                 *destbuf++ = bits;
624                                 pixels_read++;
625                             }
626                         }
627                     } else if (bits == 3) {
628                         run_length = get_bits(&gb, 8) + 25;
629                         bits = get_bits(&gb, 4);
630
631                         if (non_mod == 1 && bits == 1)
632                             pixels_read += run_length;
633                         else {
634                             if (map_table)
635                                 bits = map_table[bits];
636                             while (run_length-- > 0 && pixels_read < dbuf_len) {
637                                 *destbuf++ = bits;
638                                 pixels_read++;
639                             }
640                         }
641                     } else if (bits == 1) {
642                         pixels_read += 2;
643                         if (map_table)
644                             bits = map_table[0];
645                         else
646                             bits = 0;
647                         if (pixels_read <= dbuf_len) {
648                             *destbuf++ = bits;
649                             *destbuf++ = bits;
650                         }
651                     } else {
652                         if (map_table)
653                             bits = map_table[0];
654                         else
655                             bits = 0;
656                         *destbuf++ = bits;
657                         pixels_read ++;
658                     }
659                 }
660             }
661         }
662     }
663
664     if (get_bits(&gb, 8))
665         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
666
667     (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
668
669     return pixels_read;
670 }
671
672 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
673                                     const uint8_t **srcbuf, int buf_size,
674                                     int non_mod, uint8_t *map_table)
675 {
676     const uint8_t *sbuf_end = (*srcbuf) + buf_size;
677     int bits;
678     int run_length;
679     int pixels_read = 0;
680
681     while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
682         bits = *(*srcbuf)++;
683
684         if (bits) {
685             if (non_mod != 1 || bits != 1) {
686                 if (map_table)
687                     *destbuf++ = map_table[bits];
688                 else
689                     *destbuf++ = bits;
690             }
691             pixels_read++;
692         } else {
693             bits = *(*srcbuf)++;
694             run_length = bits & 0x7f;
695             if ((bits & 0x80) == 0) {
696                 if (run_length == 0) {
697                     return pixels_read;
698                 }
699
700                 if (map_table)
701                     bits = map_table[0];
702                 else
703                     bits = 0;
704                 while (run_length-- > 0 && pixels_read < dbuf_len) {
705                     *destbuf++ = bits;
706                     pixels_read++;
707                 }
708             } else {
709                 bits = *(*srcbuf)++;
710
711                 if (non_mod == 1 && bits == 1)
712                     pixels_read += run_length;
713                 if (map_table)
714                     bits = map_table[bits];
715                 else while (run_length-- > 0 && pixels_read < dbuf_len) {
716                     *destbuf++ = bits;
717                     pixels_read++;
718                 }
719             }
720         }
721     }
722
723     if (*(*srcbuf)++)
724         av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
725
726     return pixels_read;
727 }
728
729
730
731 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
732                                           const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
733 {
734     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
735
736     DVBSubRegion *region = get_region(ctx, display->region_id);
737     const uint8_t *buf_end = buf + buf_size;
738     uint8_t *pbuf;
739     int x_pos, y_pos;
740     int i;
741
742     uint8_t map2to4[] = { 0x0,  0x7,  0x8,  0xf};
743     uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
744     uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
745                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
746     uint8_t *map_table;
747
748     dprintf(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
749             top_bottom ? "bottom" : "top");
750
751 #ifdef DEBUG_PACKET_CONTENTS
752     for (i = 0; i < buf_size; i++) {
753         if (i % 16 == 0)
754             av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
755
756         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
757         if (i % 16 == 15)
758             av_log(avctx, AV_LOG_INFO, "\n");
759     }
760
761     if (i % 16)
762         av_log(avctx, AV_LOG_INFO, "\n");
763
764 #endif
765
766     if (region == 0)
767         return;
768
769     pbuf = region->pbuf;
770
771     x_pos = display->x_pos;
772     y_pos = display->y_pos;
773
774     if ((y_pos & 1) != top_bottom)
775         y_pos++;
776
777     while (buf < buf_end) {
778         if (x_pos > region->width || y_pos > region->height) {
779             av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
780             return;
781         }
782
783         switch (*buf++) {
784         case 0x10:
785             if (region->depth == 8)
786                 map_table = map2to8;
787             else if (region->depth == 4)
788                 map_table = map2to4;
789             else
790                 map_table = NULL;
791
792             x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
793                                                 region->width - x_pos, &buf, buf_size,
794                                                 non_mod, map_table);
795             break;
796         case 0x11:
797             if (region->depth < 4) {
798                 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
799                 return;
800             }
801
802             if (region->depth == 8)
803                 map_table = map4to8;
804             else
805                 map_table = NULL;
806
807             x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
808                                                 region->width - x_pos, &buf, buf_size,
809                                                 non_mod, map_table);
810             break;
811         case 0x12:
812             if (region->depth < 8) {
813                 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
814                 return;
815             }
816
817             x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
818                                                 region->width - x_pos, &buf, buf_size,
819                                                 non_mod, NULL);
820             break;
821
822         case 0x20:
823             map2to4[0] = (*buf) >> 4;
824             map2to4[1] = (*buf++) & 0xf;
825             map2to4[2] = (*buf) >> 4;
826             map2to4[3] = (*buf++) & 0xf;
827             break;
828         case 0x21:
829             for (i = 0; i < 4; i++)
830                 map2to8[i] = *buf++;
831             break;
832         case 0x22:
833             for (i = 0; i < 16; i++)
834                 map4to8[i] = *buf++;
835             break;
836
837         case 0xf0:
838             x_pos = display->x_pos;
839             y_pos += 2;
840             break;
841         default:
842             av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
843         }
844     }
845
846 }
847
848 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
849                                         const uint8_t *buf, int buf_size)
850 {
851     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
852
853     const uint8_t *buf_end = buf + buf_size;
854     const uint8_t *block;
855     int object_id;
856     DVBSubObject *object;
857     DVBSubObjectDisplay *display;
858     int top_field_len, bottom_field_len;
859
860     int coding_method, non_modifying_color;
861
862     object_id = AV_RB16(buf);
863     buf += 2;
864
865     object = get_object(ctx, object_id);
866
867     if (!object)
868         return;
869
870     coding_method = ((*buf) >> 2) & 3;
871     non_modifying_color = ((*buf++) >> 1) & 1;
872
873     if (coding_method == 0) {
874         top_field_len = AV_RB16(buf);
875         buf += 2;
876         bottom_field_len = AV_RB16(buf);
877         buf += 2;
878
879         if (buf + top_field_len + bottom_field_len > buf_end) {
880             av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
881             return;
882         }
883
884         for (display = object->display_list; display; display = display->object_list_next) {
885             block = buf;
886
887             dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
888                                             non_modifying_color);
889
890             if (bottom_field_len > 0)
891                 block = buf + top_field_len;
892             else
893                 bottom_field_len = top_field_len;
894
895             dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
896                                             non_modifying_color);
897         }
898
899 /*  } else if (coding_method == 1) {*/
900
901     } else {
902         av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
903     }
904
905 }
906
907 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
908                                         const uint8_t *buf, int buf_size)
909 {
910     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
911
912     const uint8_t *buf_end = buf + buf_size;
913     int clut_id;
914     DVBSubCLUT *clut;
915     int entry_id, depth , full_range;
916     int y, cr, cb, alpha;
917     int r, g, b, r_add, g_add, b_add;
918
919 #ifdef DEBUG_PACKET_CONTENTS
920     int i;
921
922     av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
923
924     for (i=0; i < buf_size; i++) {
925         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
926         if (i % 16 == 15)
927             av_log(avctx, AV_LOG_INFO, "\n");
928     }
929
930     if (i % 16)
931         av_log(avctx, AV_LOG_INFO, "\n");
932
933 #endif
934
935     clut_id = *buf++;
936     buf += 1;
937
938     clut = get_clut(ctx, clut_id);
939
940     if (!clut) {
941         clut = av_malloc(sizeof(DVBSubCLUT));
942
943         memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
944
945         clut->id = clut_id;
946
947         clut->next = ctx->clut_list;
948         ctx->clut_list = clut;
949     }
950
951     while (buf + 4 < buf_end) {
952         entry_id = *buf++;
953
954         depth = (*buf) & 0xe0;
955
956         if (depth == 0) {
957             av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
958             return;
959         }
960
961         full_range = (*buf++) & 1;
962
963         if (full_range) {
964             y = *buf++;
965             cr = *buf++;
966             cb = *buf++;
967             alpha = *buf++;
968         } else {
969             y = buf[0] & 0xfc;
970             cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
971             cb = (buf[1] << 2) & 0xf0;
972             alpha = (buf[1] << 6) & 0xc0;
973
974             buf += 2;
975         }
976
977         if (y == 0)
978             alpha = 0xff;
979
980         YUV_TO_RGB1_CCIR(cb, cr);
981         YUV_TO_RGB2_CCIR(r, g, b, y);
982
983         dprintf(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
984
985         if (depth & 0x80)
986             clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
987         if (depth & 0x40)
988             clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
989         if (depth & 0x20)
990             clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
991     }
992 }
993
994
995 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
996                                         const uint8_t *buf, int buf_size)
997 {
998     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
999
1000     const uint8_t *buf_end = buf + buf_size;
1001     int region_id, object_id;
1002     DVBSubRegion *region;
1003     DVBSubObject *object;
1004     DVBSubObjectDisplay *display;
1005     int fill;
1006
1007     if (buf_size < 10)
1008         return;
1009
1010     region_id = *buf++;
1011
1012     region = get_region(ctx, region_id);
1013
1014     if (!region) {
1015         region = av_mallocz(sizeof(DVBSubRegion));
1016
1017         region->id = region_id;
1018
1019         region->next = ctx->region_list;
1020         ctx->region_list = region;
1021     }
1022
1023     fill = ((*buf++) >> 3) & 1;
1024
1025     region->width = AV_RB16(buf);
1026     buf += 2;
1027     region->height = AV_RB16(buf);
1028     buf += 2;
1029
1030     if (region->width * region->height != region->buf_size) {
1031         if (region->pbuf)
1032             av_free(region->pbuf);
1033
1034         region->buf_size = region->width * region->height;
1035
1036         region->pbuf = av_malloc(region->buf_size);
1037
1038         fill = 1;
1039     }
1040
1041     region->depth = 1 << (((*buf++) >> 2) & 7);
1042     if(region->depth<2 || region->depth>8){
1043         av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1044         region->depth= 4;
1045     }
1046     region->clut = *buf++;
1047
1048     if (region->depth == 8)
1049         region->bgcolor = *buf++;
1050     else {
1051         buf += 1;
1052
1053         if (region->depth == 4)
1054             region->bgcolor = (((*buf++) >> 4) & 15);
1055         else
1056             region->bgcolor = (((*buf++) >> 2) & 3);
1057     }
1058
1059     dprintf(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1060
1061     if (fill) {
1062         memset(region->pbuf, region->bgcolor, region->buf_size);
1063         dprintf(avctx, "Fill region (%d)\n", region->bgcolor);
1064     }
1065
1066     delete_region_display_list(ctx, region);
1067
1068     while (buf + 5 < buf_end) {
1069         object_id = AV_RB16(buf);
1070         buf += 2;
1071
1072         object = get_object(ctx, object_id);
1073
1074         if (!object) {
1075             object = av_mallocz(sizeof(DVBSubObject));
1076
1077             object->id = object_id;
1078             object->next = ctx->object_list;
1079             ctx->object_list = object;
1080         }
1081
1082         object->type = (*buf) >> 6;
1083
1084         display = av_mallocz(sizeof(DVBSubObjectDisplay));
1085
1086         display->object_id = object_id;
1087         display->region_id = region_id;
1088
1089         display->x_pos = AV_RB16(buf) & 0xfff;
1090         buf += 2;
1091         display->y_pos = AV_RB16(buf) & 0xfff;
1092         buf += 2;
1093
1094         if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1095             display->fgcolor = *buf++;
1096             display->bgcolor = *buf++;
1097         }
1098
1099         display->region_list_next = region->display_list;
1100         region->display_list = display;
1101
1102         display->object_list_next = object->display_list;
1103         object->display_list = display;
1104     }
1105 }
1106
1107 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1108                                         const uint8_t *buf, int buf_size)
1109 {
1110     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1111     DVBSubRegionDisplay *display;
1112     DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1113
1114     const uint8_t *buf_end = buf + buf_size;
1115     int region_id;
1116     int page_state;
1117
1118     if (buf_size < 1)
1119         return;
1120
1121     ctx->time_out = *buf++;
1122     page_state = ((*buf++) >> 2) & 3;
1123
1124     dprintf(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1125
1126     if (page_state == 2) {
1127         delete_state(ctx);
1128     }
1129
1130     tmp_display_list = ctx->display_list;
1131     ctx->display_list = NULL;
1132     ctx->display_list_size = 0;
1133
1134     while (buf + 5 < buf_end) {
1135         region_id = *buf++;
1136         buf += 1;
1137
1138         display = tmp_display_list;
1139         tmp_ptr = &tmp_display_list;
1140
1141         while (display && display->region_id != region_id) {
1142             tmp_ptr = &display->next;
1143             display = display->next;
1144         }
1145
1146         if (!display)
1147             display = av_mallocz(sizeof(DVBSubRegionDisplay));
1148
1149         display->region_id = region_id;
1150
1151         display->x_pos = AV_RB16(buf);
1152         buf += 2;
1153         display->y_pos = AV_RB16(buf);
1154         buf += 2;
1155
1156         *tmp_ptr = display->next;
1157
1158         display->next = ctx->display_list;
1159         ctx->display_list = display;
1160         ctx->display_list_size++;
1161
1162         dprintf(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1163     }
1164
1165     while (tmp_display_list) {
1166         display = tmp_display_list;
1167
1168         tmp_display_list = display->next;
1169
1170         av_free(display);
1171     }
1172
1173 }
1174
1175
1176 #ifdef DEBUG_SAVE_IMAGES
1177 static void save_display_set(DVBSubContext *ctx)
1178 {
1179     DVBSubRegion *region;
1180     DVBSubRegionDisplay *display;
1181     DVBSubCLUT *clut;
1182     uint32_t *clut_table;
1183     int x_pos, y_pos, width, height;
1184     int x, y, y_off, x_off;
1185     uint32_t *pbuf;
1186     char filename[32];
1187     static int fileno_index = 0;
1188
1189     x_pos = -1;
1190     y_pos = -1;
1191     width = 0;
1192     height = 0;
1193
1194     for (display = ctx->display_list; display; display = display->next) {
1195         region = get_region(ctx, display->region_id);
1196
1197         if (x_pos == -1) {
1198             x_pos = display->x_pos;
1199             y_pos = display->y_pos;
1200             width = region->width;
1201             height = region->height;
1202         } else {
1203             if (display->x_pos < x_pos) {
1204                 width += (x_pos - display->x_pos);
1205                 x_pos = display->x_pos;
1206             }
1207
1208             if (display->y_pos < y_pos) {
1209                 height += (y_pos - display->y_pos);
1210                 y_pos = display->y_pos;
1211             }
1212
1213             if (display->x_pos + region->width > x_pos + width) {
1214                 width = display->x_pos + region->width - x_pos;
1215             }
1216
1217             if (display->y_pos + region->height > y_pos + height) {
1218                 height = display->y_pos + region->height - y_pos;
1219             }
1220         }
1221     }
1222
1223     if (x_pos >= 0) {
1224
1225         pbuf = av_malloc(width * height * 4);
1226
1227         for (display = ctx->display_list; display; display = display->next) {
1228             region = get_region(ctx, display->region_id);
1229
1230             x_off = display->x_pos - x_pos;
1231             y_off = display->y_pos - y_pos;
1232
1233             clut = get_clut(ctx, region->clut);
1234
1235             if (clut == 0)
1236                 clut = &default_clut;
1237
1238             switch (region->depth) {
1239             case 2:
1240                 clut_table = clut->clut4;
1241                 break;
1242             case 8:
1243                 clut_table = clut->clut256;
1244                 break;
1245             case 4:
1246             default:
1247                 clut_table = clut->clut16;
1248                 break;
1249             }
1250
1251             for (y = 0; y < region->height; y++) {
1252                 for (x = 0; x < region->width; x++) {
1253                     pbuf[((y + y_off) * width) + x_off + x] =
1254                         clut_table[region->pbuf[y * region->width + x]];
1255                 }
1256             }
1257
1258         }
1259
1260         snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1261
1262         png_save2(filename, pbuf, width, height);
1263
1264         av_free(pbuf);
1265     }
1266
1267     fileno_index++;
1268 }
1269 #endif
1270
1271 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1272                                                     const uint8_t *buf,
1273                                                     int buf_size)
1274 {
1275     DVBSubContext *ctx = avctx->priv_data;
1276     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1277     int dds_version, info_byte;
1278
1279     if (buf_size < 5)
1280         return;
1281
1282     info_byte   = bytestream_get_byte(&buf);
1283     dds_version = info_byte >> 4;
1284     if (display_def && display_def->version == dds_version)
1285         return; // already have this display definition version
1286
1287     if (!display_def) {
1288         display_def             = av_mallocz(sizeof(*display_def));
1289         ctx->display_definition = display_def;
1290     }
1291     if (!display_def)
1292         return;
1293
1294     display_def->version = dds_version;
1295     display_def->x       = 0;
1296     display_def->y       = 0;
1297     display_def->width   = bytestream_get_be16(&buf) + 1;
1298     display_def->height  = bytestream_get_be16(&buf) + 1;
1299
1300     if (buf_size < 13)
1301         return;
1302
1303     if (info_byte & 1<<3) { // display_window_flag
1304         display_def->x = bytestream_get_be16(&buf);
1305         display_def->y = bytestream_get_be16(&buf);
1306         display_def->width  = bytestream_get_be16(&buf) - display_def->x + 1;
1307         display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1308     }
1309 }
1310
1311 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1312                                         int buf_size, AVSubtitle *sub)
1313 {
1314     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1315     DVBSubDisplayDefinition *display_def = ctx->display_definition;
1316
1317     DVBSubRegion *region;
1318     DVBSubRegionDisplay *display;
1319     AVSubtitleRect *rect;
1320     DVBSubCLUT *clut;
1321     uint32_t *clut_table;
1322     int i;
1323     int offset_x=0, offset_y=0;
1324
1325     sub->rects = NULL;
1326     sub->start_display_time = 0;
1327     sub->end_display_time = ctx->time_out * 1000;
1328     sub->format = 0;
1329
1330     if (display_def) {
1331         offset_x = display_def->x;
1332         offset_y = display_def->y;
1333     }
1334
1335     sub->num_rects = ctx->display_list_size;
1336
1337     if (sub->num_rects > 0){
1338         sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1339         for(i=0; i<sub->num_rects; i++)
1340             sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1341     }
1342
1343     i = 0;
1344
1345     for (display = ctx->display_list; display; display = display->next) {
1346         region = get_region(ctx, display->region_id);
1347         rect = sub->rects[i];
1348
1349         if (!region)
1350             continue;
1351
1352         rect->x = display->x_pos + offset_x;
1353         rect->y = display->y_pos + offset_y;
1354         rect->w = region->width;
1355         rect->h = region->height;
1356         rect->nb_colors = 16;
1357         rect->type      = SUBTITLE_BITMAP;
1358         rect->pict.linesize[0] = region->width;
1359
1360         clut = get_clut(ctx, region->clut);
1361
1362         if (!clut)
1363             clut = &default_clut;
1364
1365         switch (region->depth) {
1366         case 2:
1367             clut_table = clut->clut4;
1368             break;
1369         case 8:
1370             clut_table = clut->clut256;
1371             break;
1372         case 4:
1373         default:
1374             clut_table = clut->clut16;
1375             break;
1376         }
1377
1378         rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1379         memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1380
1381         rect->pict.data[0] = av_malloc(region->buf_size);
1382         memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1383
1384         i++;
1385     }
1386
1387     sub->num_rects = i;
1388
1389 #ifdef DEBUG_SAVE_IMAGES
1390     save_display_set(ctx);
1391 #endif
1392
1393     return 1;
1394 }
1395
1396 static int dvbsub_decode(AVCodecContext *avctx,
1397                          void *data, int *data_size,
1398                          AVPacket *avpkt)
1399 {
1400     const uint8_t *buf = avpkt->data;
1401     int buf_size = avpkt->size;
1402     DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1403     AVSubtitle *sub = (AVSubtitle*) data;
1404     const uint8_t *p, *p_end;
1405     int segment_type;
1406     int page_id;
1407     int segment_length;
1408
1409 #ifdef DEBUG_PACKET_CONTENTS
1410     int i;
1411
1412     av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1413
1414     for (i=0; i < buf_size; i++) {
1415         av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1416         if (i % 16 == 15)
1417             av_log(avctx, AV_LOG_INFO, "\n");
1418     }
1419
1420     if (i % 16)
1421         av_log(avctx, AV_LOG_INFO, "\n");
1422
1423 #endif
1424
1425     if (buf_size <= 2)
1426         return -1;
1427
1428     p = buf;
1429     p_end = buf + buf_size;
1430
1431     while (p < p_end && *p == 0x0f) {
1432         p += 1;
1433         segment_type = *p++;
1434         page_id = AV_RB16(p);
1435         p += 2;
1436         segment_length = AV_RB16(p);
1437         p += 2;
1438
1439         if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1440             switch (segment_type) {
1441             case DVBSUB_PAGE_SEGMENT:
1442                 dvbsub_parse_page_segment(avctx, p, segment_length);
1443                 break;
1444             case DVBSUB_REGION_SEGMENT:
1445                 dvbsub_parse_region_segment(avctx, p, segment_length);
1446                 break;
1447             case DVBSUB_CLUT_SEGMENT:
1448                 dvbsub_parse_clut_segment(avctx, p, segment_length);
1449                 break;
1450             case DVBSUB_OBJECT_SEGMENT:
1451                 dvbsub_parse_object_segment(avctx, p, segment_length);
1452                 break;
1453             case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1454                 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1455             case DVBSUB_DISPLAY_SEGMENT:
1456                 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1457                 break;
1458             default:
1459                 dprintf(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1460                         segment_type, page_id, segment_length);
1461                 break;
1462             }
1463         }
1464
1465         p += segment_length;
1466     }
1467
1468     if (p != p_end) {
1469         dprintf(avctx, "Junk at end of packet\n");
1470         return -1;
1471     }
1472
1473     return buf_size;
1474 }
1475
1476
1477 AVCodec dvbsub_decoder = {
1478     "dvbsub",
1479     AVMEDIA_TYPE_SUBTITLE,
1480     CODEC_ID_DVB_SUBTITLE,
1481     sizeof(DVBSubContext),
1482     dvbsub_init_decoder,
1483     NULL,
1484     dvbsub_close_decoder,
1485     dvbsub_decode,
1486     .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1487 };