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
23 #include "bitstream.h"
24 #include "colorspace.h"
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
41 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
42 uint32_t *rgba_palette)
46 char fname[40], fname2[40];
49 snprintf(fname, 40, "%s.ppm", filename);
51 f = fopen(fname, "w");
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);
71 snprintf(fname2, 40, "%s-a.pgm", filename);
73 f = fopen(fname2, "w");
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);
90 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
93 snprintf(command, 1024, "rm %s %s", fname, fname2);
98 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
102 char fname[40], fname2[40];
105 snprintf(fname, 40, "%s.ppm", filename);
107 f = fopen(fname, "w");
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);
127 snprintf(fname2, 40, "%s-a.pgm", filename);
129 f = fopen(fname2, "w");
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);
146 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
149 snprintf(command, 1024, "rm %s %s", fname, fname2);
154 #define RGBA(r,g,b,a) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
156 typedef struct DVBSubCLUT {
161 uint32_t clut256[256];
163 struct DVBSubCLUT *next;
166 static DVBSubCLUT default_clut;
168 typedef struct DVBSubObjectDisplay {
178 struct DVBSubObjectDisplay *region_list_next;
179 struct DVBSubObjectDisplay *object_list_next;
180 } DVBSubObjectDisplay;
182 typedef struct DVBSubObject {
187 DVBSubObjectDisplay *display_list;
189 struct DVBSubObject *next;
192 typedef struct DVBSubRegionDisplay {
198 struct DVBSubRegionDisplay *next;
199 } DVBSubRegionDisplay;
201 typedef struct DVBSubRegion {
214 DVBSubObjectDisplay *display_list;
216 struct DVBSubRegion *next;
219 typedef struct DVBSubContext {
224 DVBSubRegion *region_list;
225 DVBSubCLUT *clut_list;
226 DVBSubObject *object_list;
228 int display_list_size;
229 DVBSubRegionDisplay *display_list;
233 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
235 DVBSubObject *ptr = ctx->object_list;
237 while (ptr != NULL && ptr->id != object_id) {
244 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
246 DVBSubCLUT *ptr = ctx->clut_list;
248 while (ptr != NULL && ptr->id != clut_id) {
255 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
257 DVBSubRegion *ptr = ctx->region_list;
259 while (ptr != NULL && ptr->id != region_id) {
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 != NULL) {
272 display = region->display_list;
274 object = get_object(ctx, display->object_id);
276 if (object != NULL) {
277 obj_disp = object->display_list;
278 obj_disp_ptr = &object->display_list;
280 while (obj_disp != NULL && obj_disp != display) {
281 obj_disp_ptr = &obj_disp->object_list_next;
282 obj_disp = obj_disp->object_list_next;
286 *obj_disp_ptr = obj_disp->object_list_next;
288 if (object->display_list == NULL) {
289 obj2 = ctx->object_list;
290 obj2_ptr = &ctx->object_list;
292 while (obj2 != NULL && obj2 != object) {
293 obj2_ptr = &obj2->next;
297 *obj2_ptr = obj2->next;
304 region->display_list = display->region_list_next;
311 static void delete_state(DVBSubContext *ctx)
313 DVBSubRegion *region;
316 while (ctx->region_list != NULL)
318 region = ctx->region_list;
320 ctx->region_list = region->next;
322 delete_region_display_list(ctx, region);
323 if (region->pbuf != NULL)
324 av_free(region->pbuf);
329 while (ctx->clut_list != NULL)
331 clut = ctx->clut_list;
333 ctx->clut_list = clut->next;
338 /* Should already be null */
339 if (ctx->object_list != NULL)
340 av_log(0, AV_LOG_ERROR, "Memory deallocation error!\n");
343 static int dvbsub_init_decoder(AVCodecContext *avctx)
345 int i, r, g, b, a = 0;
346 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
348 memset(avctx->priv_data, 0, sizeof(DVBSubContext));
350 ctx->composition_id = avctx->sub_id & 0xffff;
351 ctx->ancillary_id = avctx->sub_id >> 16;
353 default_clut.id = -1;
354 default_clut.next = NULL;
356 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
357 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
358 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
359 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
361 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
362 for (i = 1; i < 16; i++) {
364 r = (i & 1) ? 255 : 0;
365 g = (i & 2) ? 255 : 0;
366 b = (i & 4) ? 255 : 0;
368 r = (i & 1) ? 127 : 0;
369 g = (i & 2) ? 127 : 0;
370 b = (i & 4) ? 127 : 0;
372 default_clut.clut16[i] = RGBA(r, g, b, 255);
375 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
376 for (i = 1; i < 256; i++) {
378 r = (i & 1) ? 255 : 0;
379 g = (i & 2) ? 255 : 0;
380 b = (i & 4) ? 255 : 0;
385 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
386 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
387 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
391 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
392 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
393 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
397 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
398 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
399 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
403 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
404 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
405 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
410 default_clut.clut256[i] = RGBA(r, g, b, a);
416 static int dvbsub_close_decoder(AVCodecContext *avctx)
418 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
419 DVBSubRegionDisplay *display;
423 while (ctx->display_list != NULL)
425 display = ctx->display_list;
426 ctx->display_list = display->next;
434 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
435 uint8_t **srcbuf, int buf_size,
436 int non_mod, uint8_t *map_table)
444 init_get_bits(&gb, *srcbuf, buf_size << 8);
446 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
447 bits = get_bits(&gb, 2);
450 if (non_mod != 1 || bits != 1) {
451 if (map_table != NULL)
452 *destbuf++ = map_table[bits];
458 bits = get_bits1(&gb);
460 run_length = get_bits(&gb, 3) + 3;
461 bits = get_bits(&gb, 2);
463 if (non_mod == 1 && bits == 1)
464 pixels_read += run_length;
466 if (map_table != NULL)
467 bits = map_table[bits];
468 while (run_length-- > 0 && pixels_read < dbuf_len) {
474 bits = get_bits1(&gb);
476 bits = get_bits(&gb, 2);
478 run_length = get_bits(&gb, 4) + 12;
479 bits = get_bits(&gb, 2);
481 if (non_mod == 1 && bits == 1)
482 pixels_read += run_length;
484 if (map_table != NULL)
485 bits = map_table[bits];
486 while (run_length-- > 0 && pixels_read < dbuf_len) {
491 } else if (bits == 3) {
492 run_length = get_bits(&gb, 8) + 29;
493 bits = get_bits(&gb, 2);
495 if (non_mod == 1 && bits == 1)
496 pixels_read += run_length;
498 if (map_table != NULL)
499 bits = map_table[bits];
500 while (run_length-- > 0 && pixels_read < dbuf_len) {
505 } else if (bits == 1) {
507 if (map_table != NULL)
511 if (pixels_read <= dbuf_len) {
516 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
520 if (map_table != NULL)
531 if (get_bits(&gb, 6) != 0)
532 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
534 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
539 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
540 uint8_t **srcbuf, int buf_size,
541 int non_mod, uint8_t *map_table)
549 init_get_bits(&gb, *srcbuf, buf_size << 8);
551 while (get_bits_count(&gb) < (buf_size << 8) && pixels_read < dbuf_len) {
552 bits = get_bits(&gb, 4);
555 if (non_mod != 1 || bits != 1) {
556 if (map_table != NULL)
557 *destbuf++ = map_table[bits];
563 bits = get_bits1(&gb);
565 run_length = get_bits(&gb, 3);
567 if (run_length == 0) {
568 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
574 if (map_table != NULL)
579 while (run_length-- > 0 && pixels_read < dbuf_len) {
584 bits = get_bits1(&gb);
586 run_length = get_bits(&gb, 2) + 4;
587 bits = get_bits(&gb, 4);
589 if (non_mod == 1 && bits == 1)
590 pixels_read += run_length;
592 if (map_table != NULL)
593 bits = map_table[bits];
594 while (run_length-- > 0 && pixels_read < dbuf_len) {
600 bits = get_bits(&gb, 2);
602 run_length = get_bits(&gb, 4) + 9;
603 bits = get_bits(&gb, 4);
605 if (non_mod == 1 && bits == 1)
606 pixels_read += run_length;
608 if (map_table != NULL)
609 bits = map_table[bits];
610 while (run_length-- > 0 && pixels_read < dbuf_len) {
615 } else if (bits == 3) {
616 run_length = get_bits(&gb, 8) + 25;
617 bits = get_bits(&gb, 4);
619 if (non_mod == 1 && bits == 1)
620 pixels_read += run_length;
622 if (map_table != NULL)
623 bits = map_table[bits];
624 while (run_length-- > 0 && pixels_read < dbuf_len) {
629 } else if (bits == 1) {
631 if (map_table != NULL)
635 if (pixels_read <= dbuf_len) {
640 if (map_table != NULL)
652 if (get_bits(&gb, 8) != 0)
653 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
655 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
660 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
661 uint8_t **srcbuf, int buf_size,
662 int non_mod, uint8_t *map_table)
664 uint8_t *sbuf_end = (*srcbuf) + buf_size;
669 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
673 if (non_mod != 1 || bits != 1) {
674 if (map_table != NULL)
675 *destbuf++ = map_table[bits];
682 run_length = bits & 0x7f;
683 if ((bits & 0x80) == 0) {
684 if (run_length == 0) {
688 if (map_table != NULL)
692 while (run_length-- > 0 && pixels_read < dbuf_len) {
699 if (non_mod == 1 && bits == 1)
700 pixels_read += run_length;
701 if (map_table != NULL)
702 bits = map_table[bits];
703 else while (run_length-- > 0 && pixels_read < dbuf_len) {
711 if (*(*srcbuf)++ != 0)
712 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
719 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
720 uint8_t *buf, int buf_size, int top_bottom, int non_mod)
722 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
724 DVBSubRegion *region = get_region(ctx, display->region_id);
725 uint8_t *buf_end = buf + buf_size;
730 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
731 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
732 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
733 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
737 av_log(avctx, AV_LOG_INFO, "DVB pixel block size %d, %s field:\n", buf_size,
738 top_bottom ? "bottom" : "top");
741 #ifdef DEBUG_PACKET_CONTENTS
742 for (i = 0; i < buf_size; i++)
745 av_log(avctx, AV_LOG_INFO, "0x%08p: ", buf+i);
747 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
749 av_log(avctx, AV_LOG_INFO, "\n");
753 av_log(avctx, AV_LOG_INFO, "\n");
762 x_pos = display->x_pos;
763 y_pos = display->y_pos;
765 if ((y_pos & 1) != top_bottom)
768 while (buf < buf_end) {
769 if (x_pos > region->width || y_pos > region->height) {
770 av_log(avctx, AV_LOG_ERROR, "Invalid object location!\n");
776 if (region->depth == 8)
778 else if (region->depth == 4)
783 x_pos += dvbsub_read_2bit_string(pbuf + (y_pos * region->width) + x_pos,
784 region->width - x_pos, &buf, buf_size,
788 if (region->depth < 4) {
789 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
793 if (region->depth == 8)
798 x_pos += dvbsub_read_4bit_string(pbuf + (y_pos * region->width) + x_pos,
799 region->width - x_pos, &buf, buf_size,
803 if (region->depth < 8) {
804 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
808 x_pos += dvbsub_read_8bit_string(pbuf + (y_pos * region->width) + x_pos,
809 region->width - x_pos, &buf, buf_size,
814 map2to4[0] = (*buf) >> 4;
815 map2to4[1] = (*buf++) & 0xf;
816 map2to4[2] = (*buf) >> 4;
817 map2to4[3] = (*buf++) & 0xf;
820 for (i = 0; i < 4; i++)
824 for (i = 0; i < 16; i++)
829 x_pos = display->x_pos;
833 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
839 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
840 uint8_t *buf, int buf_size)
842 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
844 uint8_t *buf_end = buf + buf_size;
847 DVBSubObject *object;
848 DVBSubObjectDisplay *display;
849 int top_field_len, bottom_field_len;
851 int coding_method, non_modifying_colour;
853 object_id = AV_RB16(buf);
856 object = get_object(ctx, object_id);
861 coding_method = ((*buf) >> 2) & 3;
862 non_modifying_colour = ((*buf++) >> 1) & 1;
864 if (coding_method == 0) {
865 top_field_len = AV_RB16(buf);
867 bottom_field_len = AV_RB16(buf);
870 if (buf + top_field_len + bottom_field_len > buf_end) {
871 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
875 for (display = object->display_list; display != 0; display = display->object_list_next) {
878 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
879 non_modifying_colour);
881 if (bottom_field_len > 0)
882 block = buf + top_field_len;
884 bottom_field_len = top_field_len;
886 dvbsub_parse_pixel_data_block(avctx, display, block, bottom_field_len, 1,
887 non_modifying_colour);
890 /* } else if (coding_method == 1) {*/
893 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
898 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
899 uint8_t *buf, int buf_size)
901 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
903 uint8_t *buf_end = buf + buf_size;
906 int entry_id, depth , full_range;
907 int y, cr, cb, alpha;
908 int r, g, b, r_add, g_add, b_add;
910 #ifdef DEBUG_PACKET_CONTENTS
913 av_log(avctx, AV_LOG_INFO, "DVB clut packet:\n");
915 for (i=0; i < buf_size; i++)
917 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
919 av_log(avctx, AV_LOG_INFO, "\n");
923 av_log(avctx, AV_LOG_INFO, "\n");
930 clut = get_clut(ctx, clut_id);
933 clut = av_malloc(sizeof(DVBSubCLUT));
935 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
939 clut->next = ctx->clut_list;
940 ctx->clut_list = clut;
943 while (buf + 4 < buf_end)
947 depth = (*buf) & 0xe0;
950 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
954 full_range = (*buf++) & 1;
963 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
964 cb = (buf[1] << 2) & 0xf0;
965 alpha = (buf[1] << 6) & 0xc0;
973 YUV_TO_RGB1_CCIR(cb, cr);
974 YUV_TO_RGB2_CCIR(r, g, b, y);
977 av_log(avctx, AV_LOG_INFO, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
981 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
983 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
985 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
990 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
991 uint8_t *buf, int buf_size)
993 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
995 uint8_t *buf_end = buf + buf_size;
996 int region_id, object_id;
997 DVBSubRegion *region;
998 DVBSubObject *object;
999 DVBSubObjectDisplay *display;
1007 region = get_region(ctx, region_id);
1011 region = av_mallocz(sizeof(DVBSubRegion));
1013 region->id = region_id;
1015 region->next = ctx->region_list;
1016 ctx->region_list = region;
1019 fill = ((*buf++) >> 3) & 1;
1021 region->width = AV_RB16(buf);
1023 region->height = AV_RB16(buf);
1026 if (region->width * region->height != region->buf_size) {
1027 if (region->pbuf != 0)
1028 av_free(region->pbuf);
1030 region->buf_size = region->width * region->height;
1032 region->pbuf = av_malloc(region->buf_size);
1037 region->depth = 1 << (((*buf++) >> 2) & 7);
1038 region->clut = *buf++;
1040 if (region->depth == 8)
1041 region->bgcolour = *buf++;
1045 if (region->depth == 4)
1046 region->bgcolour = (((*buf++) >> 4) & 15);
1048 region->bgcolour = (((*buf++) >> 2) & 3);
1052 av_log(avctx, AV_LOG_INFO, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1056 memset(region->pbuf, region->bgcolour, region->buf_size);
1058 av_log(avctx, AV_LOG_INFO, "Fill region (%d)\n", region->bgcolour);
1062 delete_region_display_list(ctx, region);
1064 while (buf + 5 < buf_end) {
1065 object_id = AV_RB16(buf);
1068 object = get_object(ctx, object_id);
1070 if (object == NULL) {
1071 object = av_mallocz(sizeof(DVBSubObject));
1073 object->id = object_id;
1074 object->next = ctx->object_list;
1075 ctx->object_list = object;
1078 object->type = (*buf) >> 6;
1080 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1082 display->object_id = object_id;
1083 display->region_id = region_id;
1085 display->x_pos = AV_RB16(buf) & 0xfff;
1087 display->y_pos = AV_RB16(buf) & 0xfff;
1090 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1091 display->fgcolour = *buf++;
1092 display->bgcolour = *buf++;
1095 display->region_list_next = region->display_list;
1096 region->display_list = display;
1098 display->object_list_next = object->display_list;
1099 object->display_list = display;
1103 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1104 uint8_t *buf, int buf_size)
1106 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1107 DVBSubRegionDisplay *display;
1108 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1110 uint8_t *buf_end = buf + buf_size;
1117 ctx->time_out = *buf++;
1118 page_state = ((*buf++) >> 2) & 3;
1121 av_log(avctx, AV_LOG_INFO, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1124 if (page_state == 2)
1129 tmp_display_list = ctx->display_list;
1130 ctx->display_list = NULL;
1131 ctx->display_list_size = 0;
1133 while (buf + 5 < buf_end) {
1137 display = tmp_display_list;
1138 tmp_ptr = &tmp_display_list;
1140 while (display != NULL && display->region_id != region_id) {
1141 tmp_ptr = &display->next;
1142 display = display->next;
1145 if (display == NULL)
1146 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1148 display->region_id = region_id;
1150 display->x_pos = AV_RB16(buf);
1152 display->y_pos = AV_RB16(buf);
1155 *tmp_ptr = display->next;
1157 display->next = ctx->display_list;
1158 ctx->display_list = display;
1159 ctx->display_list_size++;
1162 av_log(avctx, AV_LOG_INFO, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1166 while (tmp_display_list != 0) {
1167 display = tmp_display_list;
1169 tmp_display_list = display->next;
1177 #ifdef DEBUG_SAVE_IMAGES
1178 static void save_display_set(DVBSubContext *ctx)
1180 DVBSubRegion *region;
1181 DVBSubRegionDisplay *display;
1183 uint32_t *clut_table;
1184 int x_pos, y_pos, width, height;
1185 int x, y, y_off, x_off;
1188 static int fileno_index = 0;
1195 for (display = ctx->display_list; display != NULL; display = display->next) {
1196 region = get_region(ctx, display->region_id);
1199 x_pos = display->x_pos;
1200 y_pos = display->y_pos;
1201 width = region->width;
1202 height = region->height;
1204 if (display->x_pos < x_pos) {
1205 width += (x_pos - display->x_pos);
1206 x_pos = display->x_pos;
1209 if (display->y_pos < y_pos) {
1210 height += (y_pos - display->y_pos);
1211 y_pos = display->y_pos;
1214 if (display->x_pos + region->width > x_pos + width) {
1215 width = display->x_pos + region->width - x_pos;
1218 if (display->y_pos + region->height > y_pos + height) {
1219 height = display->y_pos + region->height - y_pos;
1226 pbuf = av_malloc(width * height * 4);
1228 for (display = ctx->display_list; display != NULL; display = display->next) {
1229 region = get_region(ctx, display->region_id);
1231 x_off = display->x_pos - x_pos;
1232 y_off = display->y_pos - y_pos;
1234 clut = get_clut(ctx, region->clut);
1237 clut = &default_clut;
1239 switch (region->depth) {
1241 clut_table = clut->clut4;
1244 clut_table = clut->clut256;
1248 clut_table = clut->clut16;
1252 for (y = 0; y < region->height; y++) {
1253 for (x = 0; x < region->width; x++) {
1254 pbuf[((y + y_off) * width) + x_off + x] =
1255 clut_table[region->pbuf[y * region->width + x]];
1261 snprintf(filename, 32, "dvbs.%d", fileno_index);
1263 png_save2(filename, pbuf, width, height);
1272 static int dvbsub_display_end_segment(AVCodecContext *avctx, uint8_t *buf,
1273 int buf_size, AVSubtitle *sub)
1275 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1277 DVBSubRegion *region;
1278 DVBSubRegionDisplay *display;
1279 AVSubtitleRect *rect;
1281 uint32_t *clut_table;
1285 sub->start_display_time = 0;
1286 sub->end_display_time = ctx->time_out * 1000;
1289 sub->num_rects = ctx->display_list_size;
1291 if (sub->num_rects > 0)
1292 sub->rects = av_mallocz(sizeof(AVSubtitleRect) * sub->num_rects);
1296 for (display = ctx->display_list; display != NULL; display = display->next) {
1297 region = get_region(ctx, display->region_id);
1298 rect = &sub->rects[i];
1303 rect->x = display->x_pos;
1304 rect->y = display->y_pos;
1305 rect->w = region->width;
1306 rect->h = region->height;
1307 rect->nb_colors = 16;
1308 rect->linesize = region->width;
1310 clut = get_clut(ctx, region->clut);
1313 clut = &default_clut;
1315 switch (region->depth) {
1317 clut_table = clut->clut4;
1320 clut_table = clut->clut256;
1324 clut_table = clut->clut16;
1328 rect->rgba_palette = av_malloc((1 << region->depth) * sizeof(uint32_t));
1329 memcpy(rect->rgba_palette, clut_table, (1 << region->depth) * sizeof(uint32_t));
1331 rect->bitmap = av_malloc(region->buf_size);
1332 memcpy(rect->bitmap, region->pbuf, region->buf_size);
1339 #ifdef DEBUG_SAVE_IMAGES
1340 save_display_set(ctx);
1346 static int dvbsub_decode(AVCodecContext *avctx,
1347 void *data, int *data_size,
1348 uint8_t *buf, int buf_size)
1350 DVBSubContext *ctx = (DVBSubContext*) avctx->priv_data;
1351 AVSubtitle *sub = (AVSubtitle*) data;
1357 #ifdef DEBUG_PACKET_CONTENTS
1360 av_log(avctx, AV_LOG_INFO, "DVB sub packet:\n");
1362 for (i=0; i < buf_size; i++)
1364 av_log(avctx, AV_LOG_INFO, "%02x ", buf[i]);
1366 av_log(avctx, AV_LOG_INFO, "\n");
1370 av_log(avctx, AV_LOG_INFO, "\n");
1378 p_end = buf + buf_size;
1380 while (p < p_end && *p == 0x0f)
1383 segment_type = *p++;
1384 page_id = AV_RB16(p);
1386 segment_length = AV_RB16(p);
1389 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id) {
1390 switch (segment_type) {
1391 case DVBSUB_PAGE_SEGMENT:
1392 dvbsub_parse_page_segment(avctx, p, segment_length);
1394 case DVBSUB_REGION_SEGMENT:
1395 dvbsub_parse_region_segment(avctx, p, segment_length);
1397 case DVBSUB_CLUT_SEGMENT:
1398 dvbsub_parse_clut_segment(avctx, p, segment_length);
1400 case DVBSUB_OBJECT_SEGMENT:
1401 dvbsub_parse_object_segment(avctx, p, segment_length);
1403 case DVBSUB_DISPLAY_SEGMENT:
1404 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1408 av_log(avctx, AV_LOG_INFO, "Subtitling segment type 0x%x, page id %d, length %d\n",
1409 segment_type, page_id, segment_length);
1415 p += segment_length;
1421 av_log(avctx, AV_LOG_INFO, "Junk at end of packet\n");
1430 AVCodec dvbsub_decoder = {
1432 CODEC_TYPE_SUBTITLE,
1433 CODEC_ID_DVB_SUBTITLE,
1434 sizeof(DVBSubContext),
1435 dvbsub_init_decoder,
1437 dvbsub_close_decoder,