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