2 * DVB subtitle decoding
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
24 #include "bytestream.h"
25 #include "libavutil/colorspace.h"
27 #define DVBSUB_PAGE_SEGMENT 0x10
28 #define DVBSUB_REGION_SEGMENT 0x11
29 #define DVBSUB_CLUT_SEGMENT 0x12
30 #define DVBSUB_OBJECT_SEGMENT 0x13
31 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
32 #define DVBSUB_DISPLAY_SEGMENT 0x80
34 #define cm (ff_cropTbl + MAX_NEG_CROP)
40 static void png_save(const char *filename, uint8_t *bitmap, int w, int h,
41 uint32_t *rgba_palette)
45 char fname[40], fname2[40];
48 snprintf(fname, 40, "%s.ppm", filename);
50 f = fopen(fname, "w");
59 for(y = 0; y < h; y++) {
60 for(x = 0; x < w; x++) {
61 v = rgba_palette[bitmap[y * w + x]];
62 putc((v >> 16) & 0xff, f);
63 putc((v >> 8) & 0xff, f);
64 putc((v >> 0) & 0xff, f);
70 snprintf(fname2, 40, "%s-a.pgm", filename);
72 f = fopen(fname2, "w");
81 for(y = 0; y < h; y++) {
82 for(x = 0; x < w; x++) {
83 v = rgba_palette[bitmap[y * w + x]];
84 putc((v >> 24) & 0xff, f);
89 snprintf(command, 1024, "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
92 snprintf(command, 1024, "rm %s %s", fname, fname2);
97 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
101 char fname[40], fname2[40];
104 snprintf(fname, sizeof(fname), "%s.ppm", filename);
106 f = fopen(fname, "w");
115 for(y = 0; y < h; y++) {
116 for(x = 0; x < w; x++) {
117 v = bitmap[y * w + x];
118 putc((v >> 16) & 0xff, f);
119 putc((v >> 8) & 0xff, f);
120 putc((v >> 0) & 0xff, f);
126 snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
128 f = fopen(fname2, "w");
137 for(y = 0; y < h; y++) {
138 for(x = 0; x < w; x++) {
139 v = bitmap[y * w + x];
140 putc((v >> 24) & 0xff, f);
145 snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
148 snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
153 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
155 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 {
188 DVBSubObjectDisplay *display_list;
190 struct DVBSubObject *next;
193 typedef struct DVBSubRegionDisplay {
199 struct DVBSubRegionDisplay *next;
200 } DVBSubRegionDisplay;
202 typedef struct DVBSubRegion {
217 DVBSubObjectDisplay *display_list;
219 struct DVBSubRegion *next;
222 typedef struct DVBSubDisplayDefinition {
229 } DVBSubDisplayDefinition;
231 typedef struct DVBSubContext {
237 DVBSubRegion *region_list;
238 DVBSubCLUT *clut_list;
239 DVBSubObject *object_list;
241 int display_list_size;
242 DVBSubRegionDisplay *display_list;
243 DVBSubDisplayDefinition *display_definition;
247 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
249 DVBSubObject *ptr = ctx->object_list;
251 while (ptr && ptr->id != object_id) {
258 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
260 DVBSubCLUT *ptr = ctx->clut_list;
262 while (ptr && ptr->id != clut_id) {
269 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
271 DVBSubRegion *ptr = ctx->region_list;
273 while (ptr && ptr->id != region_id) {
280 static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
282 DVBSubObject *object, *obj2, **obj2_ptr;
283 DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
285 while (region->display_list) {
286 display = region->display_list;
288 object = get_object(ctx, display->object_id);
291 obj_disp_ptr = &object->display_list;
292 obj_disp = *obj_disp_ptr;
294 while (obj_disp && obj_disp != display) {
295 obj_disp_ptr = &obj_disp->object_list_next;
296 obj_disp = *obj_disp_ptr;
300 *obj_disp_ptr = obj_disp->object_list_next;
302 if (!object->display_list) {
303 obj2_ptr = &ctx->object_list;
306 while (obj2 != object) {
308 obj2_ptr = &obj2->next;
312 *obj2_ptr = obj2->next;
319 region->display_list = display->region_list_next;
326 static void delete_cluts(DVBSubContext *ctx)
330 while (ctx->clut_list) {
331 clut = ctx->clut_list;
333 ctx->clut_list = clut->next;
339 static void delete_objects(DVBSubContext *ctx)
341 DVBSubObject *object;
343 while (ctx->object_list) {
344 object = ctx->object_list;
346 ctx->object_list = object->next;
352 static void delete_regions(DVBSubContext *ctx)
354 DVBSubRegion *region;
356 while (ctx->region_list) {
357 region = ctx->region_list;
359 ctx->region_list = region->next;
361 delete_region_display_list(ctx, region);
363 av_free(region->pbuf);
368 static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
370 int i, r, g, b, a = 0;
371 DVBSubContext *ctx = avctx->priv_data;
373 if (!avctx->extradata || avctx->extradata_size != 4) {
374 av_log(avctx, AV_LOG_WARNING, "Invalid extradata, subtitle streams may be combined!\n");
375 ctx->composition_id = -1;
376 ctx->ancillary_id = -1;
378 ctx->composition_id = AV_RB16(avctx->extradata);
379 ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
384 default_clut.id = -1;
385 default_clut.next = NULL;
387 default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
388 default_clut.clut4[1] = RGBA(255, 255, 255, 255);
389 default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
390 default_clut.clut4[3] = RGBA(127, 127, 127, 255);
392 default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
393 for (i = 1; i < 16; i++) {
395 r = (i & 1) ? 255 : 0;
396 g = (i & 2) ? 255 : 0;
397 b = (i & 4) ? 255 : 0;
399 r = (i & 1) ? 127 : 0;
400 g = (i & 2) ? 127 : 0;
401 b = (i & 4) ? 127 : 0;
403 default_clut.clut16[i] = RGBA(r, g, b, 255);
406 default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
407 for (i = 1; i < 256; i++) {
409 r = (i & 1) ? 255 : 0;
410 g = (i & 2) ? 255 : 0;
411 b = (i & 4) ? 255 : 0;
416 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
417 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
418 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
422 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
423 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
424 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
428 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
429 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
430 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
434 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
435 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
436 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
441 default_clut.clut256[i] = RGBA(r, g, b, a);
447 static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
449 DVBSubContext *ctx = avctx->priv_data;
450 DVBSubRegionDisplay *display;
458 av_freep(&ctx->display_definition);
460 while (ctx->display_list) {
461 display = ctx->display_list;
462 ctx->display_list = display->next;
470 static int dvbsub_read_2bit_string(uint8_t *destbuf, int dbuf_len,
471 const uint8_t **srcbuf, int buf_size,
472 int non_mod, uint8_t *map_table, int x_pos)
478 int pixels_read = x_pos;
480 init_get_bits(&gb, *srcbuf, buf_size << 3);
484 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
485 bits = get_bits(&gb, 2);
488 if (non_mod != 1 || bits != 1) {
490 *destbuf++ = map_table[bits];
496 bits = get_bits1(&gb);
498 run_length = get_bits(&gb, 3) + 3;
499 bits = get_bits(&gb, 2);
501 if (non_mod == 1 && bits == 1)
502 pixels_read += run_length;
505 bits = map_table[bits];
506 while (run_length-- > 0 && pixels_read < dbuf_len) {
512 bits = get_bits1(&gb);
514 bits = get_bits(&gb, 2);
516 run_length = get_bits(&gb, 4) + 12;
517 bits = get_bits(&gb, 2);
519 if (non_mod == 1 && bits == 1)
520 pixels_read += run_length;
523 bits = map_table[bits];
524 while (run_length-- > 0 && pixels_read < dbuf_len) {
529 } else if (bits == 3) {
530 run_length = get_bits(&gb, 8) + 29;
531 bits = get_bits(&gb, 2);
533 if (non_mod == 1 && bits == 1)
534 pixels_read += run_length;
537 bits = map_table[bits];
538 while (run_length-- > 0 && pixels_read < dbuf_len) {
543 } else if (bits == 1) {
549 while (run_length-- > 0 && pixels_read < dbuf_len) {
554 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
569 if (get_bits(&gb, 6))
570 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
572 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
577 static int dvbsub_read_4bit_string(uint8_t *destbuf, int dbuf_len,
578 const uint8_t **srcbuf, int buf_size,
579 int non_mod, uint8_t *map_table, int x_pos)
585 int pixels_read = x_pos;
587 init_get_bits(&gb, *srcbuf, buf_size << 3);
591 while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
592 bits = get_bits(&gb, 4);
595 if (non_mod != 1 || bits != 1) {
597 *destbuf++ = map_table[bits];
603 bits = get_bits1(&gb);
605 run_length = get_bits(&gb, 3);
607 if (run_length == 0) {
608 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
619 while (run_length-- > 0 && pixels_read < dbuf_len) {
624 bits = get_bits1(&gb);
626 run_length = get_bits(&gb, 2) + 4;
627 bits = get_bits(&gb, 4);
629 if (non_mod == 1 && bits == 1)
630 pixels_read += run_length;
633 bits = map_table[bits];
634 while (run_length-- > 0 && pixels_read < dbuf_len) {
640 bits = get_bits(&gb, 2);
642 run_length = get_bits(&gb, 4) + 9;
643 bits = get_bits(&gb, 4);
645 if (non_mod == 1 && bits == 1)
646 pixels_read += run_length;
649 bits = map_table[bits];
650 while (run_length-- > 0 && pixels_read < dbuf_len) {
655 } else if (bits == 3) {
656 run_length = get_bits(&gb, 8) + 25;
657 bits = get_bits(&gb, 4);
659 if (non_mod == 1 && bits == 1)
660 pixels_read += run_length;
663 bits = map_table[bits];
664 while (run_length-- > 0 && pixels_read < dbuf_len) {
669 } else if (bits == 1) {
675 while (run_length-- > 0 && pixels_read < dbuf_len) {
692 if (get_bits(&gb, 8))
693 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
695 (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
700 static int dvbsub_read_8bit_string(uint8_t *destbuf, int dbuf_len,
701 const uint8_t **srcbuf, int buf_size,
702 int non_mod, uint8_t *map_table, int x_pos)
704 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
707 int pixels_read = x_pos;
711 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
715 if (non_mod != 1 || bits != 1) {
717 *destbuf++ = map_table[bits];
724 run_length = bits & 0x7f;
725 if ((bits & 0x80) == 0) {
726 if (run_length == 0) {
734 while (run_length-- > 0 && pixels_read < dbuf_len) {
741 if (non_mod == 1 && bits == 1)
742 pixels_read += run_length;
744 bits = map_table[bits];
745 else while (run_length-- > 0 && pixels_read < dbuf_len) {
754 av_log(0, AV_LOG_ERROR, "DVBSub error: line overflow\n");
761 static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
762 const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
764 DVBSubContext *ctx = avctx->priv_data;
766 DVBSubRegion *region = get_region(ctx, display->region_id);
767 const uint8_t *buf_end = buf + buf_size;
772 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
773 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
774 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
775 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
779 av_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
780 top_bottom ? "bottom" : "top");
782 for (i = 0; i < buf_size; i++) {
784 av_dlog(avctx, "0x%8p: ", buf+i);
786 av_dlog(avctx, "%02x ", buf[i]);
788 av_dlog(avctx, "\n");
792 av_dlog(avctx, "\n");
801 x_pos = display->x_pos;
802 y_pos = display->y_pos;
806 while (buf < buf_end) {
807 if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
808 av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
814 if (region->depth == 8)
816 else if (region->depth == 4)
821 x_pos = dvbsub_read_2bit_string(pbuf + (y_pos * region->width),
822 region->width, &buf, buf_end - buf,
823 non_mod, map_table, x_pos);
826 if (region->depth < 4) {
827 av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
831 if (region->depth == 8)
836 x_pos = dvbsub_read_4bit_string(pbuf + (y_pos * region->width),
837 region->width, &buf, buf_end - buf,
838 non_mod, map_table, x_pos);
841 if (region->depth < 8) {
842 av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
846 x_pos = dvbsub_read_8bit_string(pbuf + (y_pos * region->width),
847 region->width, &buf, buf_end - buf,
848 non_mod, NULL, x_pos);
852 map2to4[0] = (*buf) >> 4;
853 map2to4[1] = (*buf++) & 0xf;
854 map2to4[2] = (*buf) >> 4;
855 map2to4[3] = (*buf++) & 0xf;
858 for (i = 0; i < 4; i++)
862 for (i = 0; i < 16; i++)
867 x_pos = display->x_pos;
871 av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
877 static void dvbsub_parse_object_segment(AVCodecContext *avctx,
878 const uint8_t *buf, int buf_size)
880 DVBSubContext *ctx = avctx->priv_data;
882 const uint8_t *buf_end = buf + buf_size;
884 DVBSubObject *object;
885 DVBSubObjectDisplay *display;
886 int top_field_len, bottom_field_len;
888 int coding_method, non_modifying_color;
890 object_id = AV_RB16(buf);
893 object = get_object(ctx, object_id);
898 coding_method = ((*buf) >> 2) & 3;
899 non_modifying_color = ((*buf++) >> 1) & 1;
901 if (coding_method == 0) {
902 top_field_len = AV_RB16(buf);
904 bottom_field_len = AV_RB16(buf);
907 if (buf + top_field_len + bottom_field_len > buf_end) {
908 av_log(avctx, AV_LOG_ERROR, "Field data size too large\n");
912 for (display = object->display_list; display; display = display->object_list_next) {
913 const uint8_t *block = buf;
914 int bfl = bottom_field_len;
916 dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
917 non_modifying_color);
919 if (bottom_field_len > 0)
920 block = buf + top_field_len;
924 dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
925 non_modifying_color);
928 /* } else if (coding_method == 1) {*/
931 av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
936 static void dvbsub_parse_clut_segment(AVCodecContext *avctx,
937 const uint8_t *buf, int buf_size)
939 DVBSubContext *ctx = avctx->priv_data;
941 const uint8_t *buf_end = buf + buf_size;
945 int entry_id, depth , full_range;
946 int y, cr, cb, alpha;
947 int r, g, b, r_add, g_add, b_add;
949 av_dlog(avctx, "DVB clut packet:\n");
951 for (i=0; i < buf_size; i++) {
952 av_dlog(avctx, "%02x ", buf[i]);
954 av_dlog(avctx, "\n");
958 av_dlog(avctx, "\n");
961 version = ((*buf)>>4)&15;
964 clut = get_clut(ctx, clut_id);
967 clut = av_malloc(sizeof(DVBSubCLUT));
969 memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
974 clut->next = ctx->clut_list;
975 ctx->clut_list = clut;
978 if (clut->version != version) {
980 clut->version = version;
982 while (buf + 4 < buf_end) {
985 depth = (*buf) & 0xe0;
988 av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
992 full_range = (*buf++) & 1;
1001 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1002 cb = (buf[1] << 2) & 0xf0;
1003 alpha = (buf[1] << 6) & 0xc0;
1011 YUV_TO_RGB1_CCIR(cb, cr);
1012 YUV_TO_RGB2_CCIR(r, g, b, y);
1014 av_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1017 clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1019 clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1021 clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1027 static void dvbsub_parse_region_segment(AVCodecContext *avctx,
1028 const uint8_t *buf, int buf_size)
1030 DVBSubContext *ctx = avctx->priv_data;
1032 const uint8_t *buf_end = buf + buf_size;
1033 int region_id, object_id;
1034 int av_unused version;
1035 DVBSubRegion *region;
1036 DVBSubObject *object;
1037 DVBSubObjectDisplay *display;
1045 region = get_region(ctx, region_id);
1048 region = av_mallocz(sizeof(DVBSubRegion));
1050 region->id = region_id;
1051 region->version = -1;
1053 region->next = ctx->region_list;
1054 ctx->region_list = region;
1057 version = ((*buf)>>4) & 15;
1058 fill = ((*buf++) >> 3) & 1;
1060 region->width = AV_RB16(buf);
1062 region->height = AV_RB16(buf);
1065 if (region->width * region->height != region->buf_size) {
1066 av_free(region->pbuf);
1068 region->buf_size = region->width * region->height;
1070 region->pbuf = av_malloc(region->buf_size);
1076 region->depth = 1 << (((*buf++) >> 2) & 7);
1077 if(region->depth<2 || region->depth>8){
1078 av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1081 region->clut = *buf++;
1083 if (region->depth == 8) {
1084 region->bgcolor = *buf++;
1089 if (region->depth == 4)
1090 region->bgcolor = (((*buf++) >> 4) & 15);
1092 region->bgcolor = (((*buf++) >> 2) & 3);
1095 av_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1098 memset(region->pbuf, region->bgcolor, region->buf_size);
1099 av_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1102 delete_region_display_list(ctx, region);
1104 while (buf + 5 < buf_end) {
1105 object_id = AV_RB16(buf);
1108 object = get_object(ctx, object_id);
1111 object = av_mallocz(sizeof(DVBSubObject));
1113 object->id = object_id;
1114 object->next = ctx->object_list;
1115 ctx->object_list = object;
1118 object->type = (*buf) >> 6;
1120 display = av_mallocz(sizeof(DVBSubObjectDisplay));
1122 display->object_id = object_id;
1123 display->region_id = region_id;
1125 display->x_pos = AV_RB16(buf) & 0xfff;
1127 display->y_pos = AV_RB16(buf) & 0xfff;
1130 if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1131 display->fgcolor = *buf++;
1132 display->bgcolor = *buf++;
1135 display->region_list_next = region->display_list;
1136 region->display_list = display;
1138 display->object_list_next = object->display_list;
1139 object->display_list = display;
1143 static void dvbsub_parse_page_segment(AVCodecContext *avctx,
1144 const uint8_t *buf, int buf_size)
1146 DVBSubContext *ctx = avctx->priv_data;
1147 DVBSubRegionDisplay *display;
1148 DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1150 const uint8_t *buf_end = buf + buf_size;
1160 version = ((*buf)>>4) & 15;
1161 page_state = ((*buf++) >> 2) & 3;
1163 if (ctx->version != version) {
1165 ctx->time_out = timeout;
1166 ctx->version = version;
1168 av_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1170 if (page_state == 1 || page_state == 2) {
1171 delete_regions(ctx);
1172 delete_objects(ctx);
1176 tmp_display_list = ctx->display_list;
1177 ctx->display_list = NULL;
1178 ctx->display_list_size = 0;
1180 while (buf + 5 < buf_end) {
1184 display = tmp_display_list;
1185 tmp_ptr = &tmp_display_list;
1187 while (display && display->region_id != region_id) {
1188 tmp_ptr = &display->next;
1189 display = display->next;
1193 display = av_mallocz(sizeof(DVBSubRegionDisplay));
1195 display->region_id = region_id;
1197 display->x_pos = AV_RB16(buf);
1199 display->y_pos = AV_RB16(buf);
1202 *tmp_ptr = display->next;
1204 display->next = ctx->display_list;
1205 ctx->display_list = display;
1206 ctx->display_list_size++;
1208 av_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1211 while (tmp_display_list) {
1212 display = tmp_display_list;
1214 tmp_display_list = display->next;
1224 static void save_display_set(DVBSubContext *ctx)
1226 DVBSubRegion *region;
1227 DVBSubRegionDisplay *display;
1229 uint32_t *clut_table;
1230 int x_pos, y_pos, width, height;
1231 int x, y, y_off, x_off;
1234 static int fileno_index = 0;
1241 for (display = ctx->display_list; display; display = display->next) {
1242 region = get_region(ctx, display->region_id);
1245 x_pos = display->x_pos;
1246 y_pos = display->y_pos;
1247 width = region->width;
1248 height = region->height;
1250 if (display->x_pos < x_pos) {
1251 width += (x_pos - display->x_pos);
1252 x_pos = display->x_pos;
1255 if (display->y_pos < y_pos) {
1256 height += (y_pos - display->y_pos);
1257 y_pos = display->y_pos;
1260 if (display->x_pos + region->width > x_pos + width) {
1261 width = display->x_pos + region->width - x_pos;
1264 if (display->y_pos + region->height > y_pos + height) {
1265 height = display->y_pos + region->height - y_pos;
1272 pbuf = av_malloc(width * height * 4);
1274 for (display = ctx->display_list; display; display = display->next) {
1275 region = get_region(ctx, display->region_id);
1277 x_off = display->x_pos - x_pos;
1278 y_off = display->y_pos - y_pos;
1280 clut = get_clut(ctx, region->clut);
1283 clut = &default_clut;
1285 switch (region->depth) {
1287 clut_table = clut->clut4;
1290 clut_table = clut->clut256;
1294 clut_table = clut->clut16;
1298 for (y = 0; y < region->height; y++) {
1299 for (x = 0; x < region->width; x++) {
1300 pbuf[((y + y_off) * width) + x_off + x] =
1301 clut_table[region->pbuf[y * region->width + x]];
1307 snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1309 png_save2(filename, pbuf, width, height);
1318 static void dvbsub_parse_display_definition_segment(AVCodecContext *avctx,
1322 DVBSubContext *ctx = avctx->priv_data;
1323 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1324 int dds_version, info_byte;
1329 info_byte = bytestream_get_byte(&buf);
1330 dds_version = info_byte >> 4;
1331 if (display_def && display_def->version == dds_version)
1332 return; // already have this display definition version
1335 display_def = av_mallocz(sizeof(*display_def));
1336 ctx->display_definition = display_def;
1341 display_def->version = dds_version;
1344 display_def->width = bytestream_get_be16(&buf) + 1;
1345 display_def->height = bytestream_get_be16(&buf) + 1;
1350 if (info_byte & 1<<3) { // display_window_flag
1351 display_def->x = bytestream_get_be16(&buf);
1352 display_def->y = bytestream_get_be16(&buf);
1353 display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1354 display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1358 static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf,
1359 int buf_size, AVSubtitle *sub)
1361 DVBSubContext *ctx = avctx->priv_data;
1362 DVBSubDisplayDefinition *display_def = ctx->display_definition;
1364 DVBSubRegion *region;
1365 DVBSubRegionDisplay *display;
1366 AVSubtitleRect *rect;
1368 uint32_t *clut_table;
1370 int offset_x=0, offset_y=0;
1372 sub->end_display_time = ctx->time_out * 1000;
1375 offset_x = display_def->x;
1376 offset_y = display_def->y;
1379 sub->num_rects = ctx->display_list_size;
1381 if (sub->num_rects > 0){
1382 sub->rects = av_mallocz(sizeof(*sub->rects) * sub->num_rects);
1383 for(i=0; i<sub->num_rects; i++)
1384 sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
1388 for (display = ctx->display_list; display; display = display->next) {
1389 region = get_region(ctx, display->region_id);
1397 rect = sub->rects[i];
1398 rect->x = display->x_pos + offset_x;
1399 rect->y = display->y_pos + offset_y;
1400 rect->w = region->width;
1401 rect->h = region->height;
1402 rect->nb_colors = (1 << region->depth);
1403 rect->type = SUBTITLE_BITMAP;
1404 rect->pict.linesize[0] = region->width;
1406 clut = get_clut(ctx, region->clut);
1409 clut = &default_clut;
1411 switch (region->depth) {
1413 clut_table = clut->clut4;
1416 clut_table = clut->clut256;
1420 clut_table = clut->clut16;
1424 rect->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
1425 memcpy(rect->pict.data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
1427 rect->pict.data[0] = av_malloc(region->buf_size);
1428 memcpy(rect->pict.data[0], region->pbuf, region->buf_size);
1436 save_display_set(ctx);
1442 static int dvbsub_decode(AVCodecContext *avctx,
1443 void *data, int *data_size,
1446 const uint8_t *buf = avpkt->data;
1447 int buf_size = avpkt->size;
1448 DVBSubContext *ctx = avctx->priv_data;
1449 AVSubtitle *sub = data;
1450 const uint8_t *p, *p_end;
1455 int got_segment = 0;
1457 av_dlog(avctx, "DVB sub packet:\n");
1459 for (i=0; i < buf_size; i++) {
1460 av_dlog(avctx, "%02x ", buf[i]);
1462 av_dlog(avctx, "\n");
1466 av_dlog(avctx, "\n");
1468 if (buf_size <= 6 || *buf != 0x0f) {
1469 av_dlog(avctx, "incomplete or broken packet");
1474 p_end = buf + buf_size;
1476 while (p_end - p >= 6 && *p == 0x0f) {
1478 segment_type = *p++;
1479 page_id = AV_RB16(p);
1481 segment_length = AV_RB16(p);
1484 if (p_end - p < segment_length) {
1485 av_dlog(avctx, "incomplete or broken packet");
1489 if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1490 ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1491 switch (segment_type) {
1492 case DVBSUB_PAGE_SEGMENT:
1493 dvbsub_parse_page_segment(avctx, p, segment_length);
1496 case DVBSUB_REGION_SEGMENT:
1497 dvbsub_parse_region_segment(avctx, p, segment_length);
1500 case DVBSUB_CLUT_SEGMENT:
1501 dvbsub_parse_clut_segment(avctx, p, segment_length);
1504 case DVBSUB_OBJECT_SEGMENT:
1505 dvbsub_parse_object_segment(avctx, p, segment_length);
1508 case DVBSUB_DISPLAYDEFINITION_SEGMENT:
1509 dvbsub_parse_display_definition_segment(avctx, p, segment_length);
1511 case DVBSUB_DISPLAY_SEGMENT:
1512 *data_size = dvbsub_display_end_segment(avctx, p, segment_length, sub);
1516 av_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1517 segment_type, page_id, segment_length);
1522 p += segment_length;
1524 // Some streams do not send a display segment but if we have all the other
1525 // segments then we need no further data.
1526 if (got_segment == 15 && sub)
1527 *data_size = dvbsub_display_end_segment(avctx, p, 0, sub);
1533 AVCodec ff_dvbsub_decoder = {
1535 .type = AVMEDIA_TYPE_SUBTITLE,
1536 .id = CODEC_ID_DVB_SUBTITLE,
1537 .priv_data_size = sizeof(DVBSubContext),
1538 .init = dvbsub_init_decoder,
1539 .close = dvbsub_close_decoder,
1540 .decode = dvbsub_decode,
1541 .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),