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