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