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