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