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)
38 static void png_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
39 uint32_t *rgba_palette)
43 char fname[40], fname2[40];
46 snprintf(fname, 40,
"%s.ppm", filename);
48 f = fopen(fname,
"w");
57 for(y = 0; y < h; y++) {
58 for(x = 0; x < w; x++) {
59 v = rgba_palette[bitmap[y * w + x]];
60 putc((v >> 16) & 0xff, f);
61 putc((v >> 8) & 0xff, f);
62 putc((v >> 0) & 0xff, f);
68 snprintf(fname2, 40,
"%s-a.pgm", filename);
70 f = fopen(fname2,
"w");
79 for(y = 0; y < h; y++) {
80 for(x = 0; x < w; x++) {
81 v = rgba_palette[bitmap[y * w + x]];
82 putc((v >> 24) & 0xff, f);
87 snprintf(command, 1024,
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
90 snprintf(command, 1024,
"rm %s %s", fname, fname2);
95 static void png_save2(
const char *filename, uint32_t *bitmap,
int w,
int h)
99 char fname[40], fname2[40];
102 snprintf(fname,
sizeof(fname),
"%s.ppm", filename);
104 f = fopen(fname,
"w");
113 for(y = 0; y < h; y++) {
114 for(x = 0; x < w; x++) {
115 v = bitmap[y * w + x];
116 putc((v >> 16) & 0xff, f);
117 putc((v >> 8) & 0xff, f);
118 putc((v >> 0) & 0xff, f);
124 snprintf(fname2,
sizeof(fname2),
"%s-a.pgm", filename);
126 f = fopen(fname2,
"w");
135 for(y = 0; y < h; y++) {
136 for(x = 0; x < w; x++) {
137 v = bitmap[y * w + x];
138 putc((v >> 24) & 0xff, f);
143 snprintf(command,
sizeof(command),
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
146 snprintf(command,
sizeof(command),
"rm %s %s", fname, fname2);
151 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
248 while (ptr && ptr->
id != object_id) {
259 while (ptr && ptr->
id != clut_id) {
270 while (ptr && ptr->
id != region_id) {
288 obj_disp_ptr = &
object->display_list;
289 obj_disp = *obj_disp_ptr;
291 while (obj_disp && obj_disp != display) {
293 obj_disp = *obj_disp_ptr;
303 while (obj2 !=
object) {
305 obj2_ptr = &obj2->
next;
309 *obj2_ptr = obj2->
next;
367 int i,
r,
g,
b,
a = 0;
385 default_clut.
id = -1;
386 default_clut.
next = NULL;
388 default_clut.
clut4[0] =
RGBA( 0, 0, 0, 0);
389 default_clut.
clut4[1] =
RGBA(255, 255, 255, 255);
390 default_clut.
clut4[2] =
RGBA( 0, 0, 0, 255);
391 default_clut.
clut4[3] =
RGBA(127, 127, 127, 255);
394 for (i = 1; i < 16; i++) {
396 r = (i & 1) ? 255 : 0;
397 g = (i & 2) ? 255 : 0;
398 b = (i & 4) ? 255 : 0;
400 r = (i & 1) ? 127 : 0;
401 g = (i & 2) ? 127 : 0;
402 b = (i & 4) ? 127 : 0;
408 for (i = 1; i < 256; i++) {
410 r = (i & 1) ? 255 : 0;
411 g = (i & 2) ? 255 : 0;
412 b = (i & 4) ? 255 : 0;
417 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
418 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
419 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
423 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
424 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
425 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
429 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
430 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
431 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
435 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
436 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
437 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
472 const uint8_t **srcbuf,
int buf_size,
473 int non_mod,
uint8_t *map_table,
int x_pos)
479 int pixels_read = x_pos;
485 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
489 if (non_mod != 1 || bits != 1) {
491 *destbuf++ = map_table[
bits];
502 if (non_mod == 1 && bits == 1)
503 pixels_read += run_length;
506 bits = map_table[
bits];
507 while (run_length-- > 0 && pixels_read < dbuf_len) {
520 if (non_mod == 1 && bits == 1)
521 pixels_read += run_length;
524 bits = map_table[
bits];
525 while (run_length-- > 0 && pixels_read < dbuf_len) {
530 }
else if (bits == 3) {
534 if (non_mod == 1 && bits == 1)
535 pixels_read += run_length;
538 bits = map_table[
bits];
539 while (run_length-- > 0 && pixels_read < dbuf_len) {
544 }
else if (bits == 1) {
550 while (run_length-- > 0 && pixels_read < dbuf_len) {
579 const uint8_t **srcbuf,
int buf_size,
580 int non_mod,
uint8_t *map_table,
int x_pos)
586 int pixels_read = x_pos;
592 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
596 if (non_mod != 1 || bits != 1) {
598 *destbuf++ = map_table[
bits];
608 if (run_length == 0) {
620 while (run_length-- > 0 && pixels_read < dbuf_len) {
630 if (non_mod == 1 && bits == 1)
631 pixels_read += run_length;
634 bits = map_table[
bits];
635 while (run_length-- > 0 && pixels_read < dbuf_len) {
646 if (non_mod == 1 && bits == 1)
647 pixels_read += run_length;
650 bits = map_table[
bits];
651 while (run_length-- > 0 && pixels_read < dbuf_len) {
656 }
else if (bits == 3) {
660 if (non_mod == 1 && bits == 1)
661 pixels_read += run_length;
664 bits = map_table[
bits];
665 while (run_length-- > 0 && pixels_read < dbuf_len) {
670 }
else if (bits == 1) {
676 while (run_length-- > 0 && pixels_read < dbuf_len) {
702 const uint8_t **srcbuf,
int buf_size,
703 int non_mod,
uint8_t *map_table,
int x_pos)
705 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
708 int pixels_read = x_pos;
712 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
716 if (non_mod != 1 || bits != 1) {
718 *destbuf++ = map_table[
bits];
725 run_length = bits & 0x7f;
726 if ((bits & 0x80) == 0) {
727 if (run_length == 0) {
735 while (run_length-- > 0 && pixels_read < dbuf_len) {
742 if (non_mod == 1 && bits == 1)
743 pixels_read += run_length;
745 bits = map_table[
bits];
746 else while (run_length-- > 0 && pixels_read < dbuf_len) {
763 const uint8_t *
buf,
int buf_size,
int top_bottom,
int non_mod)
768 const uint8_t *buf_end = buf + buf_size;
773 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
774 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
775 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
776 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
780 av_dlog(avctx,
"DVB pixel block size %d, %s field:\n", buf_size,
781 top_bottom ?
"bottom" :
"top");
783 for (i = 0; i < buf_size; i++) {
785 av_dlog(avctx,
"0x%8p: ", buf+i);
787 av_dlog(avctx,
"%02x ", buf[i]);
802 x_pos = display->
x_pos;
803 y_pos = display->
y_pos;
807 while (buf < buf_end) {
808 if ((*buf!=0xf0 && x_pos >= region->
width) || y_pos >= region->
height) {
815 if (region->
depth == 8)
817 else if (region->
depth == 4)
823 region->
width, &buf, buf_end - buf,
824 non_mod, map_table, x_pos);
827 if (region->
depth < 4) {
832 if (region->
depth == 8)
838 region->
width, &buf, buf_end - buf,
839 non_mod, map_table, x_pos);
842 if (region->
depth < 8) {
848 region->
width, &buf, buf_end - buf,
849 non_mod, NULL, x_pos);
853 map2to4[0] = (*buf) >> 4;
854 map2to4[1] = (*buf++) & 0xf;
855 map2to4[2] = (*buf) >> 4;
856 map2to4[3] = (*buf++) & 0xf;
859 for (i = 0; i < 4; i++)
863 for (i = 0; i < 16; i++)
868 x_pos = display->
x_pos;
883 const uint8_t *buf_end = buf + buf_size;
887 int top_field_len, bottom_field_len;
889 int coding_method, non_modifying_color;
899 coding_method = ((*buf) >> 2) & 3;
900 non_modifying_color = ((*buf++) >> 1) & 1;
902 if (coding_method == 0) {
905 bottom_field_len =
AV_RB16(buf);
908 if (buf + top_field_len + bottom_field_len > buf_end) {
915 int bfl = bottom_field_len;
918 non_modifying_color);
920 if (bottom_field_len > 0)
921 block = buf + top_field_len;
926 non_modifying_color);
942 const uint8_t *buf_end = buf + buf_size;
946 int entry_id,
depth , full_range;
948 int r,
g,
b, r_add, g_add, b_add;
950 av_dlog(avctx,
"DVB clut packet:\n");
952 for (i=0; i < buf_size; i++) {
953 av_dlog(avctx,
"%02x ", buf[i]);
962 version = ((*buf)>>4)&15;
970 memcpy(clut, &default_clut,
sizeof(
DVBSubCLUT));
979 if (clut->
version != version) {
983 while (buf + 4 < buf_end) {
986 depth = (*buf) & 0xe0;
993 full_range = (*buf++) & 1;
1002 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1003 cb = (buf[1] << 2) & 0xf0;
1004 alpha = (buf[1] << 6) & 0xc0;
1015 av_dlog(avctx,
"clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1016 if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1017 av_dlog(avctx,
"More than one bit level marked: %x\n", depth);
1023 clut->
clut4[entry_id] =
RGBA(r,g,b,255 - alpha);
1024 else if (depth & 0x40)
1025 clut->
clut16[entry_id] =
RGBA(r,g,b,255 - alpha);
1026 else if (depth & 0x20)
1027 clut->
clut256[entry_id] =
RGBA(r,g,b,255 - alpha);
1039 const uint8_t *buf_end = buf + buf_size;
1040 int region_id, object_id;
1057 region->
id = region_id;
1064 version = ((*buf)>>4) & 15;
1065 fill = ((*buf++) >> 3) & 1;
1083 region->
depth = 1 << (((*buf++) >> 2) & 7);
1088 region->
clut = *buf++;
1090 if (region->
depth == 8) {
1096 if (region->
depth == 4)
1097 region->
bgcolor = (((*buf++) >> 4) & 15);
1099 region->
bgcolor = (((*buf++) >> 2) & 3);
1111 while (buf + 5 < buf_end) {
1120 object->id = object_id;
1125 object->
type = (*buf) >> 6;
1137 if ((object->
type == 1 || object->
type == 2) && buf+1 < buf_end) {
1146 object->display_list = display;
1157 const uint8_t *buf_end = buf + buf_size;
1167 version = ((*buf)>>4) & 15;
1168 page_state = ((*buf++) >> 2) & 3;
1170 if (ctx->
version != version) {
1175 av_dlog(avctx,
"Page time out %ds, state %d\n", ctx->
time_out, page_state);
1177 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;
1213 av_dlog(avctx,
"Region %d, (%d,%d)\n", region_id, display->
x_pos, display->
y_pos);
1216 while (tmp_display_list) {
1217 display = tmp_display_list;
1219 tmp_display_list = display->
next;
1234 uint32_t *clut_table;
1236 int x,
y, y_off, x_off;
1239 static int fileno_index = 0;
1250 x_pos = display->
x_pos;
1251 y_pos = display->
y_pos;
1252 width = region->
width;
1255 if (display->
x_pos < x_pos) {
1256 width += (x_pos - display->
x_pos);
1257 x_pos = display->
x_pos;
1260 if (display->
y_pos < y_pos) {
1261 height += (y_pos - display->
y_pos);
1262 y_pos = display->
y_pos;
1265 if (display->
x_pos + region->
width > x_pos + width) {
1266 width = display->
x_pos + region->
width - x_pos;
1269 if (display->
y_pos + region->
height > y_pos + height) {
1282 x_off = display->
x_pos - x_pos;
1283 y_off = display->
y_pos - y_pos;
1290 switch (region->
depth) {
1292 clut_table = clut->
clut4;
1299 clut_table = clut->
clut16;
1303 for (y = 0; y < region->
height; y++) {
1304 for (x = 0; x < region->
width; x++) {
1305 pbuf[((y + y_off) * width) + x_off + x] =
1306 clut_table[region->
pbuf[y * region->
width + x]];
1312 snprintf(filename,
sizeof(filename),
"dvbs.%d", fileno_index);
1314 png_save2(filename, pbuf, width, height);
1329 int dds_version, info_byte;
1334 info_byte = bytestream_get_byte(&buf);
1335 dds_version = info_byte >> 4;
1336 if (display_def && display_def->
version == dds_version)
1340 display_def =
av_mallocz(
sizeof(*display_def));
1346 display_def->
version = dds_version;
1349 display_def->
width = bytestream_get_be16(&buf) + 1;
1350 display_def->
height = bytestream_get_be16(&buf) + 1;
1359 if (info_byte & 1<<3) {
1360 display_def->
x = bytestream_get_be16(&buf);
1361 display_def->
width = bytestream_get_be16(&buf) - display_def->
x + 1;
1362 display_def->
y = bytestream_get_be16(&buf);
1363 display_def->
height = bytestream_get_be16(&buf) - display_def->
y + 1;
1377 uint32_t *clut_table;
1379 int offset_x=0, offset_y=0;
1384 offset_x = display_def->
x;
1385 offset_y = display_def->
y;
1392 if (region && region->
dirty)
1412 rect = sub->
rects[i];
1413 rect->
x = display->
x_pos + offset_x;
1414 rect->
y = display->
y_pos + offset_y;
1426 switch (region->
depth) {
1428 clut_table = clut->
clut4;
1435 clut_table = clut->
clut16;
1440 memcpy(rect->
pict.
data[1], clut_table, (1 << region->
depth) *
sizeof(uint32_t));
1449 save_display_set(ctx);
1460 int buf_size = avpkt->
size;
1469 int got_segment = 0;
1471 av_dlog(avctx,
"DVB sub packet:\n");
1473 for (i=0; i < buf_size; i++) {
1474 av_dlog(avctx,
"%02x ", buf[i]);
1482 if (buf_size <= 6 || *buf != 0x0f) {
1483 av_dlog(avctx,
"incomplete or broken packet");
1488 p_end = buf + buf_size;
1490 while (p_end - p >= 6 && *p == 0x0f) {
1492 segment_type = *p++;
1498 if (p_end - p < segment_length) {
1499 av_dlog(avctx,
"incomplete or broken packet");
1505 switch (segment_type) {
1516 if (ret < 0)
return ret;
1531 av_dlog(avctx,
"Subtitling segment type 0x%x, page id %d, length %d\n",
1532 segment_type, page_id, segment_length);
1537 p += segment_length;
1541 if (got_segment == 15 && sub)