Merge avfiltergraphdesc.c in avfiltergraph.c
[ffmpeg-lucabe.git] / libavcodec / dvbsubdec.c
blob52aee28e8817da18c6557e35a08f04b5c08284bb
1 /*
2 * DVB subtitle decoding for ffmpeg
3 * Copyright (c) 2005 Ian Caulfield.
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include "avcodec.h"
22 #include "dsputil.h"
23 #include "bitstream.h"
24 #include "colorspace.h"
26 //#define DEBUG
27 //#define DEBUG_PACKET_CONTENTS
28 //#define DEBUG_SAVE_IMAGES
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
36 #define cm (ff_cropTbl + MAX_NEG_CROP)
38 #ifdef DEBUG_SAVE_IMAGES
39 #undef fprintf
40 #if 0
41 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
42 uint32_t *rgba_palette)
44 int x, y, v;
45 FILE *f;
46 char fname[40], fname2[40];
47 char command[1024];
49 snprintf(fname, 40, "%s.ppm", filename);
51 f = fopen(fname, "w");
52 if (!f) {
53 perror(fname);
54 exit(1);
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);
68 fclose(f);
71 snprintf(fname2, 40, "%s-a.pgm", filename);
73 f = fopen(fname2, "w");
74 if (!f) {
75 perror(fname2);
76 exit(1);
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);
88 fclose(f);
90 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
91 system(command);
93 snprintf(command, 1024, "rm %s %s", fname, fname2);
94 system(command);
96 #endif
98 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
100 int x, y, v;
101 FILE *f;
102 char fname[40], fname2[40];
103 char command[1024];
105 snprintf(fname, sizeof(fname), "%s.ppm", filename);
107 f = fopen(fname, "w");
108 if (!f) {
109 perror(fname);
110 exit(1);
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);
124 fclose(f);
127 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
129 f = fopen(fname2, "w");
130 if (!f) {
131 perror(fname2);
132 exit(1);
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);
144 fclose(f);
146 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
147 system(command);
149 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
150 system(command);
152 #endif
154 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
156 typedef struct DVBSubCLUT {
157 int id;
159 uint32_t clut4[4];
160 uint32_t clut16[16];
161 uint32_t clut256[256];
163 struct DVBSubCLUT *next;
164 } DVBSubCLUT;
166 static DVBSubCLUT default_clut;
168 typedef struct DVBSubObjectDisplay {
169 int object_id;
170 int region_id;
172 int x_pos;
173 int y_pos;
175 int fgcolor;
176 int bgcolor;
178 struct DVBSubObjectDisplay *region_list_next;
179 struct DVBSubObjectDisplay *object_list_next;
180 } DVBSubObjectDisplay;
182 typedef struct DVBSubObject {
183 int id;
185 int type;
187 DVBSubObjectDisplay *display_list;
189 struct DVBSubObject *next;
190 } DVBSubObject;
192 typedef struct DVBSubRegionDisplay {
193 int region_id;
195 int x_pos;
196 int y_pos;
198 struct DVBSubRegionDisplay *next;
199 } DVBSubRegionDisplay;
201 typedef struct DVBSubRegion {
202 int id;
204 int width;
205 int height;
206 int depth;
208 int clut;
209 int bgcolor;
211 uint8_t *pbuf;
212 int buf_size;
214 DVBSubObjectDisplay *display_list;
216 struct DVBSubRegion *next;
217 } DVBSubRegion;
219 typedef struct DVBSubContext {
220 int composition_id;
221 int ancillary_id;
223 int time_out;
224 DVBSubRegion *region_list;
225 DVBSubCLUT *clut_list;
226 DVBSubObject *object_list;
228 int display_list_size;
229 DVBSubRegionDisplay *display_list;
230 } DVBSubContext;
233 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
235 DVBSubObject *ptr = ctx->object_list;
237 while (ptr && ptr->id != object_id) {
238 ptr = ptr->next;
241 return ptr;
244 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
246 DVBSubCLUT *ptr = ctx->clut_list;
248 while (ptr && ptr->id != clut_id) {
249 ptr = ptr->next;
252 return ptr;
255 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
257 DVBSubRegion *ptr = ctx->region_list;
259 while (ptr && ptr->id != region_id) {
260 ptr = ptr->next;
263 return ptr;
266 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
268 DVBSubObject *object, *obj2, **obj2_ptr;
269 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
271 while (region->display_list) {
272 display = region->display_list;
274 object = get_object(ctx, display->object_id);
276 if (object) {
277 obj_disp = object->display_list;
278 obj_disp_ptr = &object->display_list;
280 while (obj_disp && obj_disp != display) {
281 obj_disp_ptr = &obj_disp->object_list_next;
282 obj_disp = obj_disp->object_list_next;
285 if (obj_disp) {
286 *obj_disp_ptr = obj_disp->object_list_next;
288 if (!object->display_list) {
289 obj2 = ctx->object_list;
290 obj2_ptr = &ctx->object_list;
292 while (obj2 && obj2 != object) {
293 obj2_ptr = &obj2->next;
294 obj2 = obj2->next;
297 *obj2_ptr = obj2->next;
299 av_free(obj2);
304 region->display_list = display->region_list_next;
306 av_free(display);
311 static void delete_state(DVBSubContext *ctx)
313 DVBSubRegion *region;
314 DVBSubCLUT *clut;
316 while (ctx->region_list) {
317 region = ctx->region_list;
319 ctx->region_list = region->next;
321 delete_region_display_list(ctx, region);
322 if (region->pbuf)
323 av_free(region->pbuf);
325 av_free(region);
328 while (ctx->clut_list) {
329 clut = ctx->clut_list;
331 ctx->clut_list = clut->next;
333 av_free(clut);
336 /* Should already be null */
337 if (ctx->object_list)
338 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
341 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
343 int i, r, g, b, a = 0;
344 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
346 memset(avctx->priv_data, 0, sizeof(DVBSubContext));
348 ctx->composition_id = avctx->sub_id & 0xffff;
349 ctx->ancillary_id = avctx->sub_id >> 16;
351 default_clut.id = -1;
352 default_clut.next = NULL;
354 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
355 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
356 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
357 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
359 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
360 for (i = 1; i < 16; i++) {
361 if (i < 8) {
362 r = (i & 1) ? 255 : 0;
363 g = (i & 2) ? 255 : 0;
364 b = (i & 4) ? 255 : 0;
365 } else {
366 r = (i & 1) ? 127 : 0;
367 g = (i & 2) ? 127 : 0;
368 b = (i & 4) ? 127 : 0;
370 default_clut.clut16[i] = RGBA(r, g, b, 255);
373 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
374 for (i = 1; i < 256; i++) {
375 if (i < 8) {
376 r = (i & 1) ? 255 : 0;
377 g = (i & 2) ? 255 : 0;
378 b = (i & 4) ? 255 : 0;
379 a = 63;
380 } else {
381 switch (i & 0x88) {
382 case 0x00:
383 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
384 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
385 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
386 a = 255;
387 break;
388 case 0x08:
389 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
390 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
391 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
392 a = 127;
393 break;
394 case 0x80:
395 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
396 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
397 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
398 a = 255;
399 break;
400 case 0x88:
401 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
402 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
403 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
404 a = 255;
405 break;
408 default_clut.clut256[i] = RGBA(r, g, b, a);
411 return 0;
414 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
416 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
417 DVBSubRegionDisplay *display;
419 delete_state(ctx);
421 while (ctx->display_list) {
422 display = ctx->display_list;
423 ctx->display_list = display->next;
425 av_free(display);
428 return 0;
431 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
432 const uint8_t **srcbuf, int buf_size,
433 int non_mod, uint8_t *map_table)
435 GetBitContext gb;
437 int bits;
438 int run_length;
439 int pixels_read = 0;
441 init_get_bits(&gb, *srcbuf, buf_size << 8);
443 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
444 bits = get_bits(&gb, 2);
446 if (bits) {
447 if (non_mod != 1 || bits != 1) {
448 if (map_table)
449 *destbuf++ = map_table[bits];
450 else
451 *destbuf++ = bits;
453 pixels_read++;
454 } else {
455 bits = get_bits1(&gb);
456 if (bits == 1) {
457 run_length = get_bits(&gb, 3) + 3;
458 bits = get_bits(&gb, 2);
460 if (non_mod == 1 && bits == 1)
461 pixels_read += run_length;
462 else {
463 if (map_table)
464 bits = map_table[bits];
465 while (run_length-- > 0 && pixels_read < dbuf_len) {
466 *destbuf++ = bits;
467 pixels_read++;
470 } else {
471 bits = get_bits1(&gb);
472 if (bits == 0) {
473 bits = get_bits(&gb, 2);
474 if (bits == 2) {
475 run_length = get_bits(&gb, 4) + 12;
476 bits = get_bits(&gb, 2);
478 if (non_mod == 1 && bits == 1)
479 pixels_read += run_length;
480 else {
481 if (map_table)
482 bits = map_table[bits];
483 while (run_length-- > 0 && pixels_read < dbuf_len) {
484 *destbuf++ = bits;
485 pixels_read++;
488 } else if (bits == 3) {
489 run_length = get_bits(&gb, 8) + 29;
490 bits = get_bits(&gb, 2);
492 if (non_mod == 1 && bits == 1)
493 pixels_read += run_length;
494 else {
495 if (map_table)
496 bits = map_table[bits];
497 while (run_length-- > 0 && pixels_read < dbuf_len) {
498 *destbuf++ = bits;
499 pixels_read++;
502 } else if (bits == 1) {
503 pixels_read += 2;
504 if (map_table)
505 bits = map_table[0];
506 else
507 bits = 0;
508 if (pixels_read <= dbuf_len) {
509 *destbuf++ = bits;
510 *destbuf++ = bits;
512 } else {
513 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
514 return pixels_read;
516 } else {
517 if (map_table)
518 bits = map_table[0];
519 else
520 bits = 0;
521 *destbuf++ = bits;
522 pixels_read++;
528 if (get_bits(&gb, 6))
529 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
531 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
533 return pixels_read;
536 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
537 const uint8_t **srcbuf, int buf_size,
538 int non_mod, uint8_t *map_table)
540 GetBitContext gb;
542 int bits;
543 int run_length;
544 int pixels_read = 0;
546 init_get_bits(&gb, *srcbuf, buf_size << 8);
548 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
549 bits = get_bits(&gb, 4);
551 if (bits) {
552 if (non_mod != 1 || bits != 1) {
553 if (map_table)
554 *destbuf++ = map_table[bits];
555 else
556 *destbuf++ = bits;
558 pixels_read++;
559 } else {
560 bits = get_bits1(&gb);
561 if (bits == 0) {
562 run_length = get_bits(&gb, 3);
564 if (run_length == 0) {
565 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
566 return pixels_read;
569 run_length += 2;
571 if (map_table)
572 bits = map_table[0];
573 else
574 bits = 0;
576 while (run_length-- > 0 && pixels_read < dbuf_len) {
577 *destbuf++ = bits;
578 pixels_read++;
580 } else {
581 bits = get_bits1(&gb);
582 if (bits == 0) {
583 run_length = get_bits(&gb, 2) + 4;
584 bits = get_bits(&gb, 4);
586 if (non_mod == 1 && bits == 1)
587 pixels_read += run_length;
588 else {
589 if (map_table)
590 bits = map_table[bits];
591 while (run_length-- > 0 && pixels_read < dbuf_len) {
592 *destbuf++ = bits;
593 pixels_read++;
596 } else {
597 bits = get_bits(&gb, 2);
598 if (bits == 2) {
599 run_length = get_bits(&gb, 4) + 9;
600 bits = get_bits(&gb, 4);
602 if (non_mod == 1 && bits == 1)
603 pixels_read += run_length;
604 else {
605 if (map_table)
606 bits = map_table[bits];
607 while (run_length-- > 0 && pixels_read < dbuf_len) {
608 *destbuf++ = bits;
609 pixels_read++;
612 } else if (bits == 3) {
613 run_length = get_bits(&gb, 8) + 25;
614 bits = get_bits(&gb, 4);
616 if (non_mod == 1 && bits == 1)
617 pixels_read += run_length;
618 else {
619 if (map_table)
620 bits = map_table[bits];
621 while (run_length-- > 0 && pixels_read < dbuf_len) {
622 *destbuf++ = bits;
623 pixels_read++;
626 } else if (bits == 1) {
627 pixels_read += 2;
628 if (map_table)
629 bits = map_table[0];
630 else
631 bits = 0;
632 if (pixels_read <= dbuf_len) {
633 *destbuf++ = bits;
634 *destbuf++ = bits;
636 } else {
637 if (map_table)
638 bits = map_table[0];
639 else
640 bits = 0;
641 *destbuf++ = bits;
642 pixels_read ++;
649 if (get_bits(&gb, 8))
650 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
652 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
654 return pixels_read;
657 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
658 const uint8_t **srcbuf, int buf_size,
659 int non_mod, uint8_t *map_table)
661 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
662 int bits;
663 int run_length;
664 int pixels_read = 0;
666 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
667 bits = *(*srcbuf)++;
669 if (bits) {
670 if (non_mod != 1 || bits != 1) {
671 if (map_table)
672 *destbuf++ = map_table[bits];
673 else
674 *destbuf++ = bits;
676 pixels_read++;
677 } else {
678 bits = *(*srcbuf)++;
679 run_length = bits & 0x7f;
680 if ((bits & 0x80) == 0) {
681 if (run_length == 0) {
682 return pixels_read;
685 if (map_table)
686 bits = map_table[0];
687 else
688 bits = 0;
689 while (run_length-- > 0 && pixels_read < dbuf_len) {
690 *destbuf++ = bits;
691 pixels_read++;
693 } else {
694 bits = *(*srcbuf)++;
696 if (non_mod == 1 && bits == 1)
697 pixels_read += run_length;
698 if (map_table)
699 bits = map_table[bits];
700 else while (run_length-- > 0 && pixels_read < dbuf_len) {
701 *destbuf++ = bits;
702 pixels_read++;
708 if (*(*srcbuf)++)
709 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
711 return pixels_read;
716 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
717 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
719 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
721 DVBSubRegion *region = get_region(ctx, display->region_id);
722 const uint8_t *buf_end = buf + buf_size;
723 uint8_t *pbuf;
724 int x_pos, y_pos;
725 int i;
727 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
728 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
729 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
730 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
731 uint8_t *map_table;
733 #ifdef DEBUG
734 av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
735 top_bottom ? "bottom" : "top");
736 #endif
738 #ifdef DEBUG_PACKET_CONTENTS
739 for (i = 0; i < buf_size; i++) {
740 if (i % 16 == 0)
741 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
743 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
744 if (i % 16 == 15)
745 av_log(avctx, AV_LOG_INFO, "\n");
748 if (i % 16)
749 av_log(avctx, AV_LOG_INFO, "\n");
751 #endif
753 if (region == 0)
754 return;
756 pbuf = region->pbuf;
758 x_pos = display->x_pos;
759 y_pos = display->y_pos;
761 if ((y_pos & 1) != top_bottom)
762 y_pos++;
764 while (buf < buf_end) {
765 if (x_pos > region->width || y_pos > region->height) {
766 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
767 return;
770 switch (*buf++) {
771 case 0x10:
772 if (region->depth == 8)
773 map_table = map2to8;
774 else if (region->depth == 4)
775 map_table = map2to4;
776 else
777 map_table = NULL;
779 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
780 region->width - x_pos, &buf, buf_size,
781 non_mod, map_table);
782 break;
783 case 0x11:
784 if (region->depth < 4) {
785 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
786 return;
789 if (region->depth == 8)
790 map_table = map4to8;
791 else
792 map_table = NULL;
794 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
795 region->width - x_pos, &buf, buf_size,
796 non_mod, map_table);
797 break;
798 case 0x12:
799 if (region->depth < 8) {
800 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
801 return;
804 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
805 region->width - x_pos, &buf, buf_size,
806 non_mod, NULL);
807 break;
809 case 0x20:
810 map2to4[0] = (*buf) >> 4;
811 map2to4[1] = (*buf++) & 0xf;
812 map2to4[2] = (*buf) >> 4;
813 map2to4[3] = (*buf++) & 0xf;
814 break;
815 case 0x21:
816 for (i = 0; i < 4; i++)
817 map2to8[i] = *buf++;
818 break;
819 case 0x22:
820 for (i = 0; i < 16; i++)
821 map4to8[i] = *buf++;
822 break;
824 case 0xf0:
825 x_pos = display->x_pos;
826 y_pos += 2;
827 break;
828 default:
829 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
835 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
836 const uint8_t *buf, int buf_size)
838 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
840 const uint8_t *buf_end = buf + buf_size;
841 const uint8_t *block;
842 int object_id;
843 DVBSubObject *object;
844 DVBSubObjectDisplay *display;
845 int top_field_len, bottom_field_len;
847 int coding_method, non_modifying_color;
849 object_id = AV_RB16(buf);
850 buf += 2;
852 object = get_object(ctx, object_id);
854 if (!object)
855 return;
857 coding_method = ((*buf) >> 2) & 3;
858 non_modifying_color = ((*buf++) >> 1) & 1;
860 if (coding_method == 0) {
861 top_field_len = AV_RB16(buf);
862 buf += 2;
863 bottom_field_len = AV_RB16(buf);
864 buf += 2;
866 if (buf + top_field_len + bottom_field_len > buf_end) {
867 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
868 return;
871 for (display = object->display_list; display; display = display->object_list_next) {
872 block = buf;
874 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
875 non_modifying_color);
877 if (bottom_field_len > 0)
878 block = buf + top_field_len;
879 else
880 bottom_field_len = top_field_len;
882 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
883 non_modifying_color);
886 /* } else if (coding_method == 1) {*/
888 } else {
889 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
894 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
895 const uint8_t *buf, int buf_size)
897 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
899 const uint8_t *buf_end = buf + buf_size;
900 int clut_id;
901 DVBSubCLUT *clut;
902 int entry_id, depth , full_range;
903 int y, cr, cb, alpha;
904 int r, g, b, r_add, g_add, b_add;
906 #ifdef DEBUG_PACKET_CONTENTS
907 int i;
909 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
911 for (i=0; i < buf_size; i++) {
912 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
913 if (i % 16 == 15)
914 av_log(avctx, AV_LOG_INFO, "\n");
917 if (i % 16)
918 av_log(avctx, AV_LOG_INFO, "\n");
920 #endif
922 clut_id = *buf++;
923 buf += 1;
925 clut = get_clut(ctx, clut_id);
927 if (!clut) {
928 clut = av_malloc(sizeof(DVBSubCLUT));
930 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
932 clut->id = clut_id;
934 clut->next = ctx->clut_list;
935 ctx->clut_list = clut;
938 while (buf + 4 < buf_end) {
939 entry_id = *buf++;
941 depth = (*buf) & 0xe0;
943 if (depth == 0) {
944 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
945 return;
948 full_range = (*buf++) & 1;
950 if (full_range) {
951 y = *buf++;
952 cr = *buf++;
953 cb = *buf++;
954 alpha = *buf++;
955 } else {
956 y = buf[0] & 0xfc;
957 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
958 cb = (buf[1] << 2) & 0xf0;
959 alpha = (buf[1] << 6) & 0xc0;
961 buf += 2;
964 if (y == 0)
965 alpha = 0xff;
967 YUV_TO_RGB1_CCIR(cb, cr);
968 YUV_TO_RGB2_CCIR(r, g, b, y);
970 #ifdef DEBUG
971 av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
972 #endif
974 if (depth & 0x80)
975 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
976 if (depth & 0x40)
977 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
978 if (depth & 0x20)
979 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
984 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
985 const uint8_t *buf, int buf_size)
987 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
989 const uint8_t *buf_end = buf + buf_size;
990 int region_id, object_id;
991 DVBSubRegion *region;
992 DVBSubObject *object;
993 DVBSubObjectDisplay *display;
994 int fill;
996 if (buf_size < 10)
997 return;
999 region_id = *buf++;
1001 region = get_region(ctx, region_id);
1003 if (!region) {
1004 region = av_mallocz(sizeof(DVBSubRegion));
1006 region->id = region_id;
1008 region->next = ctx->region_list;
1009 ctx->region_list = region;
1012 fill = ((*buf++) >> 3) & 1;
1014 region->width = AV_RB16(buf);
1015 buf += 2;
1016 region->height = AV_RB16(buf);
1017 buf += 2;
1019 if (region->width * region->height != region->buf_size) {
1020 if (region->pbuf)
1021 av_free(region->pbuf);
1023 region->buf_size = region->width * region->height;
1025 region->pbuf = av_malloc(region->buf_size);
1027 fill = 1;
1030 region->depth = 1 << (((*buf++) >> 2) & 7);
1031 if(region->depth<2 || region->depth>8){
1032 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1033 region->depth= 4;
1035 region->clut = *buf++;
1037 if (region->depth == 8)
1038 region->bgcolor = *buf++;
1039 else {
1040 buf += 1;
1042 if (region->depth == 4)
1043 region->bgcolor = (((*buf++) >> 4) & 15);
1044 else
1045 region->bgcolor = (((*buf++) >> 2) & 3);
1048 #ifdef DEBUG
1049 av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1050 #endif
1052 if (fill) {
1053 memset(region->pbuf, region->bgcolor, region->buf_size);
1054 #ifdef DEBUG
1055 av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolor);
1056 #endif
1059 delete_region_display_list(ctx, region);
1061 while (buf + 5 < buf_end) {
1062 object_id = AV_RB16(buf);
1063 buf += 2;
1065 object = get_object(ctx, object_id);
1067 if (!object) {
1068 object = av_mallocz(sizeof(DVBSubObject));
1070 object->id = object_id;
1071 object->next = ctx->object_list;
1072 ctx->object_list = object;
1075 object->type = (*buf) >> 6;
1077 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1079 display->object_id = object_id;
1080 display->region_id = region_id;
1082 display->x_pos = AV_RB16(buf) & 0xfff;
1083 buf += 2;
1084 display->y_pos = AV_RB16(buf) & 0xfff;
1085 buf += 2;
1087 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1088 display->fgcolor = *buf++;
1089 display->bgcolor = *buf++;
1092 display->region_list_next = region->display_list;
1093 region->display_list = display;
1095 display->object_list_next = object->display_list;
1096 object->display_list = display;
1100 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1101 const uint8_t *buf, int buf_size)
1103 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1104 DVBSubRegionDisplay *display;
1105 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1107 const uint8_t *buf_end = buf + buf_size;
1108 int region_id;
1109 int page_state;
1111 if (buf_size < 1)
1112 return;
1114 ctx->time_out = *buf++;
1115 page_state = ((*buf++) >> 2) & 3;
1117 #ifdef DEBUG
1118 av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1119 #endif
1121 if (page_state == 2) {
1122 delete_state(ctx);
1125 tmp_display_list = ctx->display_list;
1126 ctx->display_list = NULL;
1127 ctx->display_list_size = 0;
1129 while (buf + 5 < buf_end) {
1130 region_id = *buf++;
1131 buf += 1;
1133 display = tmp_display_list;
1134 tmp_ptr = &tmp_display_list;
1136 while (display && display->region_id != region_id) {
1137 tmp_ptr = &display->next;
1138 display = display->next;
1141 if (!display)
1142 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1144 display->region_id = region_id;
1146 display->x_pos = AV_RB16(buf);
1147 buf += 2;
1148 display->y_pos = AV_RB16(buf);
1149 buf += 2;
1151 *tmp_ptr = display->next;
1153 display->next = ctx->display_list;
1154 ctx->display_list = display;
1155 ctx->display_list_size++;
1157 #ifdef DEBUG
1158 av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1159 #endif
1162 while (tmp_display_list) {
1163 display = tmp_display_list;
1165 tmp_display_list = display->next;
1167 av_free(display);
1173 #ifdef DEBUG_SAVE_IMAGES
1174 static void save_display_set(DVBSubContext *ctx)
1176 DVBSubRegion *region;
1177 DVBSubRegionDisplay *display;
1178 DVBSubCLUT *clut;
1179 uint32_t *clut_table;
1180 int x_pos, y_pos, width, height;
1181 int x, y, y_off, x_off;
1182 uint32_t *pbuf;
1183 char filename[32];
1184 static int fileno_index = 0;
1186 x_pos = -1;
1187 y_pos = -1;
1188 width = 0;
1189 height = 0;
1191 for (display = ctx->display_list; display; display = display->next) {
1192 region = get_region(ctx, display->region_id);
1194 if (x_pos == -1) {
1195 x_pos = display->x_pos;
1196 y_pos = display->y_pos;
1197 width = region->width;
1198 height = region->height;
1199 } else {
1200 if (display->x_pos < x_pos) {
1201 width += (x_pos - display->x_pos);
1202 x_pos = display->x_pos;
1205 if (display->y_pos < y_pos) {
1206 height += (y_pos - display->y_pos);
1207 y_pos = display->y_pos;
1210 if (display->x_pos + region->width > x_pos + width) {
1211 width = display->x_pos + region->width - x_pos;
1214 if (display->y_pos + region->height > y_pos + height) {
1215 height = display->y_pos + region->height - y_pos;
1220 if (x_pos >= 0) {
1222 pbuf = av_malloc(width * height * 4);
1224 for (display = ctx->display_list; display; display = display->next) {
1225 region = get_region(ctx, display->region_id);
1227 x_off = display->x_pos - x_pos;
1228 y_off = display->y_pos - y_pos;
1230 clut = get_clut(ctx, region->clut);
1232 if (clut == 0)
1233 clut = &default_clut;
1235 switch (region->depth) {
1236 case 2:
1237 clut_table = clut->clut4;
1238 break;
1239 case 8:
1240 clut_table = clut->clut256;
1241 break;
1242 case 4:
1243 default:
1244 clut_table = clut->clut16;
1245 break;
1248 for (y = 0; y < region->height; y++) {
1249 for (x = 0; x < region->width; x++) {
1250 pbuf[((y + y_off) * width) + x_off + x] =
1251 clut_table[region->pbuf[y * region->width + x]];
1257 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1259 png_save2(filename, pbuf, width, height);
1261 av_free(pbuf);
1264 fileno_index++;
1266 #endif
1268 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1269 int buf_size, AVSubtitle *sub)
1271 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1273 DVBSubRegion *region;
1274 DVBSubRegionDisplay *display;
1275 AVSubtitleRect *rect;
1276 DVBSubCLUT *clut;
1277 uint32_t *clut_table;
1278 int i;
1280 sub->rects = NULL;
1281 sub->start_display_time = 0;
1282 sub->end_display_time = ctx->time_out * 1000;
1283 sub->format = 0;
1285 sub->num_rects = ctx->display_list_size;
1287 if (sub->num_rects > 0)
1288 sub->rects = av_mallocz(sizeof(AVSubtitleRect) * sub->num_rects);
1290 i = 0;
1292 for (display = ctx->display_list; display; display = display->next) {
1293 region = get_region(ctx, display->region_id);
1294 rect = &sub->rects[i];
1296 if (!region)
1297 continue;
1299 rect->x = display->x_pos;
1300 rect->y = display->y_pos;
1301 rect->w = region->width;
1302 rect->h = region->height;
1303 rect->nb_colors = 16;
1304 rect->linesize = region->width;
1306 clut = get_clut(ctx, region->clut);
1308 if (!clut)
1309 clut = &default_clut;
1311 switch (region->depth) {
1312 case 2:
1313 clut_table = clut->clut4;
1314 break;
1315 case 8:
1316 clut_table = clut->clut256;
1317 break;
1318 case 4:
1319 default:
1320 clut_table = clut->clut16;
1321 break;
1324 rect->rgba_palette = av_malloc((1 << region->depth) * sizeof(uint32_t));
1325 memcpy(rect->rgba_palette, clut_table, (1 << region->depth) * sizeof(uint32_t));
1327 rect->bitmap = av_malloc(region->buf_size);
1328 memcpy(rect->bitmap, region->pbuf, region->buf_size);
1330 i++;
1333 sub->num_rects = i;
1335 #ifdef DEBUG_SAVE_IMAGES
1336 save_display_set(ctx);
1337 #endif
1339 return 1;
1342 static int dvbsub_decode(AVCodecContext *avctx,
1343 void *data, int *data_size,
1344 const uint8_t *buf, int buf_size)
1346 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1347 AVSubtitle *sub = (AVSubtitle*) data;
1348 const uint8_t *p, *p_end;
1349 int segment_type;
1350 int page_id;
1351 int segment_length;
1353 #ifdef DEBUG_PACKET_CONTENTS
1354 int i;
1356 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1358 for (i=0; i < buf_size; i++) {
1359 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1360 if (i % 16 == 15)
1361 av_log(avctx, AV_LOG_INFO, "\n");
1364 if (i % 16)
1365 av_log(avctx, AV_LOG_INFO, "\n");
1367 #endif
1369 if (buf_size <= 2)
1370 return -1;
1372 p = buf;
1373 p_end = buf + buf_size;
1375 while (p < p_end && *p == 0x0f) {
1376 p += 1;
1377 segment_type = *p++;
1378 page_id = AV_RB16(p);
1379 p += 2;
1380 segment_length = AV_RB16(p);
1381 p += 2;
1383 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1384 switch (segment_type) {
1385 case DVBSUB_PAGE_SEGMENT:
1386 dvbsub_parse_page_segment(avctx, p, segment_length);
1387 break;
1388 case DVBSUB_REGION_SEGMENT:
1389 dvbsub_parse_region_segment(avctx, p, segment_length);
1390 break;
1391 case DVBSUB_CLUT_SEGMENT:
1392 dvbsub_parse_clut_segment(avctx, p, segment_length);
1393 break;
1394 case DVBSUB_OBJECT_SEGMENT:
1395 dvbsub_parse_object_segment(avctx, p, segment_length);
1396 break;
1397 case DVBSUB_DISPLAY_SEGMENT:
1398 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1399 break;
1400 default:
1401 #ifdef DEBUG
1402 av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
1403 segment_type, page_id, segment_length);
1404 #endif
1405 break;
1409 p += segment_length;
1412 if (p != p_end) {
1413 #ifdef DEBUG
1414 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1415 #endif
1416 return -1;
1419 return buf_size;
1423 AVCodec dvbsub_decoder = {
1424 "dvbsub",
1425 CODEC_TYPE_SUBTITLE,
1426 CODEC_ID_DVB_SUBTITLE,
1427 sizeof(DVBSubContext),
1428 dvbsub_init_decoder,
1429 NULL,
1430 dvbsub_close_decoder,
1431 dvbsub_decode,