OSDN Git Service

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