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