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