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