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))
251 while (ptr && ptr->
id != object_id) {
262 while (ptr && ptr->
id != clut_id) {
273 while (ptr && ptr->
id != region_id) {
291 obj_disp_ptr = &
object->display_list;
292 obj_disp = *obj_disp_ptr;
294 while (obj_disp && obj_disp != display) {
296 obj_disp = *obj_disp_ptr;
306 while (obj2 !=
object) {
308 obj2_ptr = &obj2->
next;
312 *obj2_ptr = obj2->
next;
370 int i,
r,
g,
b,
a = 0;
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);
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;
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);
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;
484 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
488 if (non_mod != 1 || bits != 1) {
490 *destbuf++ = map_table[
bits];
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) {
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) {
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) {
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;
591 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
595 if (non_mod != 1 || bits != 1) {
597 *destbuf++ = map_table[
bits];
607 if (run_length == 0) {
619 while (run_length-- > 0 && pixels_read < dbuf_len) {
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) {
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) {
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) {
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) {
762 const uint8_t *
buf,
int buf_size,
int top_bottom,
int non_mod)
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]);
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) {
814 if (region->
depth == 8)
816 else if (region->
depth == 4)
822 region->
width, &buf, buf_end - buf,
823 non_mod, map_table, x_pos);
826 if (region->
depth < 4) {
831 if (region->
depth == 8)
837 region->
width, &buf, buf_end - buf,
838 non_mod, map_table, x_pos);
841 if (region->
depth < 8) {
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;
882 const uint8_t *buf_end = buf + buf_size;
886 int top_field_len, bottom_field_len;
888 int coding_method, non_modifying_color;
898 coding_method = ((*buf) >> 2) & 3;
899 non_modifying_color = ((*buf++) >> 1) & 1;
901 if (coding_method == 0) {
904 bottom_field_len =
AV_RB16(buf);
907 if (buf + top_field_len + bottom_field_len > buf_end) {
914 int bfl = bottom_field_len;
917 non_modifying_color);
919 if (bottom_field_len > 0)
920 block = buf + top_field_len;
925 non_modifying_color);
941 const uint8_t *buf_end = buf + buf_size;
945 int entry_id,
depth , full_range;
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]);
961 version = ((*buf)>>4)&15;
969 memcpy(clut, &default_clut,
sizeof(
DVBSubCLUT));
978 if (clut->
version != version) {
982 while (buf + 4 < buf_end) {
985 depth = (*buf) & 0xe0;
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;
1014 av_dlog(avctx,
"clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1015 if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1016 av_dlog(avctx,
"More than one bit level marked: %x\n", depth);
1022 clut->
clut4[entry_id] =
RGBA(r,g,b,255 - alpha);
1023 else if (depth & 0x40)
1024 clut->
clut16[entry_id] =
RGBA(r,g,b,255 - alpha);
1025 else if (depth & 0x20)
1026 clut->
clut256[entry_id] =
RGBA(r,g,b,255 - alpha);
1038 const uint8_t *buf_end = buf + buf_size;
1039 int region_id, object_id;
1056 region->
id = region_id;
1063 version = ((*buf)>>4) & 15;
1064 fill = ((*buf++) >> 3) & 1;
1082 region->
depth = 1 << (((*buf++) >> 2) & 7);
1087 region->
clut = *buf++;
1089 if (region->
depth == 8) {
1095 if (region->
depth == 4)
1096 region->
bgcolor = (((*buf++) >> 4) & 15);
1098 region->
bgcolor = (((*buf++) >> 2) & 3);
1110 while (buf + 5 < buf_end) {
1119 object->id = object_id;
1124 object->
type = (*buf) >> 6;
1136 if ((object->
type == 1 || object->
type == 2) && buf+1 < buf_end) {
1145 object->display_list = display;
1156 const uint8_t *buf_end = buf + buf_size;
1166 version = ((*buf)>>4) & 15;
1167 page_state = ((*buf++) >> 2) & 3;
1169 if (ctx->
version != version) {
1174 av_dlog(avctx,
"Page time out %ds, state %d\n", ctx->
time_out, page_state);
1176 if (page_state == 1 || page_state == 2) {
1186 while (buf + 5 < buf_end) {
1190 display = tmp_display_list;
1191 tmp_ptr = &tmp_display_list;
1193 while (display && display->
region_id != region_id) {
1194 tmp_ptr = &display->
next;
1195 display = display->
next;
1208 *tmp_ptr = display->
next;
1214 av_dlog(avctx,
"Region %d, (%d,%d)\n", region_id, display->
x_pos, display->
y_pos);
1217 while (tmp_display_list) {
1218 display = tmp_display_list;
1220 tmp_display_list = display->
next;
1235 uint32_t *clut_table;
1237 int x,
y, y_off, x_off;
1240 static int fileno_index = 0;
1251 x_pos = display->
x_pos;
1252 y_pos = display->
y_pos;
1253 width = region->
width;
1256 if (display->
x_pos < x_pos) {
1257 width += (x_pos - display->
x_pos);
1258 x_pos = display->
x_pos;
1261 if (display->
y_pos < y_pos) {
1262 height += (y_pos - display->
y_pos);
1263 y_pos = display->
y_pos;
1266 if (display->
x_pos + region->
width > x_pos + width) {
1267 width = display->
x_pos + region->
width - x_pos;
1270 if (display->
y_pos + region->
height > y_pos + height) {
1283 x_off = display->
x_pos - x_pos;
1284 y_off = display->
y_pos - y_pos;
1291 switch (region->
depth) {
1293 clut_table = clut->
clut4;
1300 clut_table = clut->
clut16;
1304 for (y = 0; y < region->
height; y++) {
1305 for (x = 0; x < region->
width; x++) {
1306 pbuf[((y + y_off) * width) + x_off + x] =
1307 clut_table[region->
pbuf[y * region->
width + x]];
1313 snprintf(filename,
sizeof(filename),
"dvbs.%d", fileno_index);
1315 png_save2(filename, pbuf, width, height);
1330 int dds_version, info_byte;
1335 info_byte = bytestream_get_byte(&buf);
1336 dds_version = info_byte >> 4;
1337 if (display_def && display_def->
version == dds_version)
1341 display_def =
av_mallocz(
sizeof(*display_def));
1347 display_def->
version = dds_version;
1350 display_def->
width = bytestream_get_be16(&buf) + 1;
1351 display_def->
height = bytestream_get_be16(&buf) + 1;
1360 if (info_byte & 1<<3) {
1361 display_def->
x = bytestream_get_be16(&buf);
1362 display_def->
y = bytestream_get_be16(&buf);
1363 display_def->
width = bytestream_get_be16(&buf) - display_def->
x + 1;
1364 display_def->
height = bytestream_get_be16(&buf) - display_def->
y + 1;
1378 uint32_t *clut_table;
1380 int offset_x=0, offset_y=0;
1385 offset_x = display_def->
x;
1386 offset_y = display_def->
y;
1407 rect = sub->
rects[i];
1408 rect->
x = display->
x_pos + offset_x;
1409 rect->
y = display->
y_pos + offset_y;
1421 switch (region->
depth) {
1423 clut_table = clut->
clut4;
1430 clut_table = clut->
clut16;
1435 memcpy(rect->
pict.
data[1], clut_table, (1 << region->
depth) *
sizeof(uint32_t));
1446 save_display_set(ctx);
1453 void *
data,
int *data_size,
1457 int buf_size = avpkt->
size;
1466 int got_segment = 0;
1468 av_dlog(avctx,
"DVB sub packet:\n");
1470 for (i=0; i < buf_size; i++) {
1471 av_dlog(avctx,
"%02x ", buf[i]);
1479 if (buf_size <= 6 || *buf != 0x0f) {
1480 av_dlog(avctx,
"incomplete or broken packet");
1485 p_end = buf + buf_size;
1487 while (p_end - p >= 6 && *p == 0x0f) {
1489 segment_type = *p++;
1495 if (p_end - p < segment_length) {
1496 av_dlog(avctx,
"incomplete or broken packet");
1502 switch (segment_type) {
1513 if (ret < 0)
return ret;
1528 av_dlog(avctx,
"Subtitling segment type 0x%x, page id %d, length %d\n",
1529 segment_type, page_id, segment_length);
1534 p += segment_length;
1538 if (got_segment == 15 && sub)