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