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