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