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) (((a) << 24) | ((r) << 16) | ((g) << 8) | (b))
246 while (ptr && ptr->
id != object_id) {
257 while (ptr && ptr->
id != clut_id) {
268 while (ptr && ptr->
id != region_id) {
286 obj_disp_ptr = &
object->display_list;
287 obj_disp = *obj_disp_ptr;
289 while (obj_disp && obj_disp != display) {
291 obj_disp = *obj_disp_ptr;
301 while (obj2 !=
object) {
303 obj2_ptr = &obj2->
next;
307 *obj2_ptr = obj2->
next;
353 int i,
r,
g,
b, a = 0;
365 default_clut.
id = -1;
368 default_clut.
clut4[0] =
RGBA( 0, 0, 0, 0);
369 default_clut.
clut4[1] =
RGBA(255, 255, 255, 255);
370 default_clut.
clut4[2] =
RGBA( 0, 0, 0, 255);
371 default_clut.
clut4[3] =
RGBA(127, 127, 127, 255);
374 for (i = 1; i < 16; i++) {
376 r = (i & 1) ? 255 : 0;
377 g = (i & 2) ? 255 : 0;
378 b = (i & 4) ? 255 : 0;
380 r = (i & 1) ? 127 : 0;
381 g = (i & 2) ? 127 : 0;
382 b = (i & 4) ? 127 : 0;
388 for (i = 1; i < 256; i++) {
390 r = (i & 1) ? 255 : 0;
391 g = (i & 2) ? 255 : 0;
392 b = (i & 4) ? 255 : 0;
397 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
398 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
399 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
403 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
404 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
405 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
409 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
410 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
411 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
415 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
416 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
417 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
446 const uint8_t **srcbuf,
int buf_size,
447 int non_mod, uint8_t *map_table)
457 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
461 if (non_mod != 1 || bits != 1) {
463 *destbuf++ = map_table[
bits];
474 if (non_mod == 1 && bits == 1)
475 pixels_read += run_length;
478 bits = map_table[
bits];
479 while (run_length-- > 0 && pixels_read < dbuf_len) {
492 if (non_mod == 1 && bits == 1)
493 pixels_read += run_length;
496 bits = map_table[
bits];
497 while (run_length-- > 0 && pixels_read < dbuf_len) {
502 }
else if (bits == 3) {
506 if (non_mod == 1 && bits == 1)
507 pixels_read += run_length;
510 bits = map_table[
bits];
511 while (run_length-- > 0 && pixels_read < dbuf_len) {
516 }
else if (bits == 1) {
522 if (pixels_read <= dbuf_len) {
551 const uint8_t **srcbuf,
int buf_size,
552 int non_mod, uint8_t *map_table)
562 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
566 if (non_mod != 1 || bits != 1) {
568 *destbuf++ = map_table[
bits];
578 if (run_length == 0) {
590 while (run_length-- > 0 && pixels_read < dbuf_len) {
600 if (non_mod == 1 && bits == 1)
601 pixels_read += run_length;
604 bits = map_table[
bits];
605 while (run_length-- > 0 && pixels_read < dbuf_len) {
616 if (non_mod == 1 && bits == 1)
617 pixels_read += run_length;
620 bits = map_table[
bits];
621 while (run_length-- > 0 && pixels_read < dbuf_len) {
626 }
else if (bits == 3) {
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) {
640 }
else if (bits == 1) {
646 if (pixels_read <= dbuf_len) {
672 const uint8_t **srcbuf,
int buf_size,
673 int non_mod, uint8_t *map_table)
675 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
680 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
684 if (non_mod != 1 || bits != 1) {
686 *destbuf++ = map_table[
bits];
693 run_length = bits & 0x7f;
694 if ((bits & 0x80) == 0) {
695 if (run_length == 0) {
703 while (run_length-- > 0 && pixels_read < dbuf_len) {
710 if (non_mod == 1 && bits == 1)
711 pixels_read += run_length;
713 bits = map_table[
bits];
714 else while (run_length-- > 0 && pixels_read < dbuf_len) {
731 const uint8_t *buf,
int buf_size,
int top_bottom,
int non_mod)
736 const uint8_t *buf_end = buf + buf_size;
741 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
742 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
743 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
744 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
747 av_dlog(avctx,
"DVB pixel block size %d, %s field:\n", buf_size,
748 top_bottom ?
"bottom" :
"top");
750 for (i = 0; i < buf_size; i++) {
752 av_dlog(avctx,
"0x%8p: ", buf+i);
754 av_dlog(avctx,
"%02x ", buf[i]);
767 x_pos = display->
x_pos;
768 y_pos = display->
y_pos;
770 if ((y_pos & 1) != top_bottom)
773 while (buf < buf_end) {
774 if (x_pos > region->
width || y_pos > region->
height) {
781 if (region->
depth == 8)
783 else if (region->
depth == 4)
789 region->
width - x_pos, &buf, buf_end - buf,
793 if (region->
depth < 4) {
798 if (region->
depth == 8)
804 region->
width - x_pos, &buf, buf_end - buf,
808 if (region->
depth < 8) {
814 region->
width - x_pos, &buf, buf_end - buf,
819 map2to4[0] = (*buf) >> 4;
820 map2to4[1] = (*buf++) & 0xf;
821 map2to4[2] = (*buf) >> 4;
822 map2to4[3] = (*buf++) & 0xf;
825 for (i = 0; i < 4; i++)
829 for (i = 0; i < 16; i++)
834 x_pos = display->
x_pos;
845 const uint8_t *buf,
int buf_size)
849 const uint8_t *buf_end = buf + buf_size;
850 const uint8_t *
block;
854 int top_field_len, bottom_field_len;
856 int coding_method, non_modifying_color;
866 coding_method = ((*buf) >> 2) & 3;
867 non_modifying_color = ((*buf++) >> 1) & 1;
869 if (coding_method == 0) {
872 bottom_field_len =
AV_RB16(buf);
875 if (buf + top_field_len + bottom_field_len > buf_end) {
884 non_modifying_color);
886 if (bottom_field_len > 0)
887 block = buf + top_field_len;
889 bottom_field_len = top_field_len;
892 non_modifying_color);
904 const uint8_t *buf,
int buf_size)
908 const uint8_t *buf_end = buf + buf_size;
911 int entry_id,
depth , full_range;
912 int y, cr, cb, alpha;
913 int r,
g,
b, r_add, g_add, b_add;
915 av_dlog(avctx,
"DVB clut packet:\n");
917 for (i=0; i < buf_size; i++) {
918 av_dlog(avctx,
"%02x ", buf[i]);
934 memcpy(clut, &default_clut,
sizeof(
DVBSubCLUT));
942 while (buf + 4 < buf_end) {
945 depth = (*buf) & 0xe0;
952 full_range = (*buf++) & 1;
961 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
962 cb = (buf[1] << 2) & 0xf0;
963 alpha = (buf[1] << 6) & 0xc0;
974 av_dlog(avctx,
"clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
977 clut->
clut4[entry_id] =
RGBA(r,g,b,255 - alpha);
979 clut->
clut16[entry_id] =
RGBA(r,g,b,255 - alpha);
987 const uint8_t *buf,
int buf_size)
991 const uint8_t *buf_end = buf + buf_size;
992 int region_id, object_id;
1008 region->
id = region_id;
1014 fill = ((*buf++) >> 3) & 1;
1031 region->
depth = 1 << (((*buf++) >> 2) & 7);
1036 region->
clut = *buf++;
1038 if (region->
depth == 8)
1043 if (region->
depth == 4)
1044 region->
bgcolor = (((*buf++) >> 4) & 15);
1046 region->
bgcolor = (((*buf++) >> 2) & 3);
1058 while (buf + 5 < buf_end) {
1067 object->id = object_id;
1072 object->
type = (*buf) >> 6;
1084 if ((object->
type == 1 || object->
type == 2) && buf+1 < buf_end) {
1093 object->display_list = display;
1098 const uint8_t *buf,
int buf_size)
1104 const uint8_t *buf_end = buf + buf_size;
1112 page_state = ((*buf++) >> 2) & 3;
1114 av_dlog(avctx,
"Page time out %ds, state %d\n", ctx->
time_out, page_state);
1116 if (page_state == 2) {
1124 while (buf + 5 < buf_end) {
1128 display = tmp_display_list;
1129 tmp_ptr = &tmp_display_list;
1131 while (display && display->
region_id != region_id) {
1132 tmp_ptr = &display->
next;
1133 display = display->
next;
1146 *tmp_ptr = display->
next;
1152 av_dlog(avctx,
"Region %d, (%d,%d)\n", region_id, display->
x_pos, display->
y_pos);
1155 while (tmp_display_list) {
1156 display = tmp_display_list;
1158 tmp_display_list = display->
next;
1172 uint32_t *clut_table;
1174 int x, y, y_off, x_off;
1177 static int fileno_index = 0;
1188 x_pos = display->
x_pos;
1189 y_pos = display->
y_pos;
1190 width = region->
width;
1193 if (display->
x_pos < x_pos) {
1194 width += (x_pos - display->
x_pos);
1195 x_pos = display->
x_pos;
1198 if (display->
y_pos < y_pos) {
1199 height += (y_pos - display->
y_pos);
1200 y_pos = display->
y_pos;
1203 if (display->
x_pos + region->
width > x_pos + width) {
1204 width = display->
x_pos + region->
width - x_pos;
1207 if (display->
y_pos + region->
height > y_pos + height) {
1220 x_off = display->
x_pos - x_pos;
1221 y_off = display->
y_pos - y_pos;
1228 switch (region->
depth) {
1230 clut_table = clut->
clut4;
1237 clut_table = clut->
clut16;
1241 for (y = 0; y < region->
height; y++) {
1242 for (x = 0; x < region->
width; x++) {
1243 pbuf[((y + y_off) * width) + x_off + x] =
1244 clut_table[region->
pbuf[y * region->
width + x]];
1250 snprintf(filename,
sizeof(filename),
"dvbs.%d", fileno_index);
1252 png_save2(filename, pbuf, width, height);
1267 int dds_version, info_byte;
1272 info_byte = bytestream_get_byte(&buf);
1273 dds_version = info_byte >> 4;
1274 if (display_def && display_def->
version == dds_version)
1278 display_def =
av_mallocz(
sizeof(*display_def));
1284 display_def->
version = dds_version;
1287 display_def->
width = bytestream_get_be16(&buf) + 1;
1288 display_def->
height = bytestream_get_be16(&buf) + 1;
1293 if (info_byte & 1<<3) {
1294 display_def->
x = bytestream_get_be16(&buf);
1295 display_def->
y = bytestream_get_be16(&buf);
1296 display_def->
width = bytestream_get_be16(&buf) - display_def->
x + 1;
1297 display_def->
height = bytestream_get_be16(&buf) - display_def->
y + 1;
1311 uint32_t *clut_table;
1313 int offset_x=0, offset_y=0;
1321 offset_x = display_def->
x;
1322 offset_y = display_def->
y;
1337 rect = sub->
rects[i];
1342 rect->
x = display->
x_pos + offset_x;
1343 rect->
y = display->
y_pos + offset_y;
1355 switch (region->
depth) {
1357 clut_table = clut->
clut4;
1364 clut_table = clut->
clut16;
1369 memcpy(rect->
pict.
data[1], clut_table, (1 << region->
depth) *
sizeof(uint32_t));
1380 save_display_set(ctx);
1387 void *
data,
int *data_size,
1390 const uint8_t *buf = avpkt->
data;
1391 int buf_size = avpkt->
size;
1394 const uint8_t *p, *p_end;
1400 av_dlog(avctx,
"DVB sub packet:\n");
1402 for (i=0; i < buf_size; i++) {
1403 av_dlog(avctx,
"%02x ", buf[i]);
1411 if (buf_size <= 6 || *buf != 0x0f) {
1412 av_dlog(avctx,
"incomplete or broken packet");
1417 p_end = buf + buf_size;
1419 while (p_end - p >= 6 && *p == 0x0f) {
1421 segment_type = *p++;
1427 if (p_end - p < segment_length) {
1428 av_dlog(avctx,
"incomplete or broken packet");
1434 switch (segment_type) {
1453 av_dlog(avctx,
"Subtitling segment type 0x%x, page id %d, length %d\n",
1454 segment_type, page_id, segment_length);
1459 p += segment_length;