28 #define DVBSUB_PAGE_SEGMENT 0x10
29 #define DVBSUB_REGION_SEGMENT 0x11
30 #define DVBSUB_CLUT_SEGMENT 0x12
31 #define DVBSUB_OBJECT_SEGMENT 0x13
32 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
33 #define DVBSUB_DISPLAY_SEGMENT 0x80
35 #define cm (ff_crop_tab + MAX_NEG_CROP)
39 static void png_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
40 uint32_t *rgba_palette)
44 char fname[40], fname2[40];
47 snprintf(fname, 40,
"%s.ppm", filename);
49 f = fopen(fname,
"w");
58 for(y = 0; y < h; y++) {
59 for(x = 0; x < w; x++) {
60 v = rgba_palette[bitmap[y * w + x]];
61 putc((v >> 16) & 0xff, f);
62 putc((v >> 8) & 0xff, f);
63 putc((v >> 0) & 0xff, f);
69 snprintf(fname2, 40,
"%s-a.pgm", filename);
71 f = fopen(fname2,
"w");
80 for(y = 0; y < h; y++) {
81 for(x = 0; x < w; x++) {
82 v = rgba_palette[bitmap[y * w + x]];
83 putc((v >> 24) & 0xff, f);
88 snprintf(command, 1024,
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
91 snprintf(command, 1024,
"rm %s %s", fname, fname2);
96 static void png_save2(
const char *filename, uint32_t *bitmap,
int w,
int h)
100 char fname[40], fname2[40];
103 snprintf(fname,
sizeof(fname),
"%s.ppm", filename);
105 f = fopen(fname,
"w");
114 for(y = 0; y < h; y++) {
115 for(x = 0; x < w; x++) {
116 v = bitmap[y * w + x];
117 putc((v >> 16) & 0xff, f);
118 putc((v >> 8) & 0xff, f);
119 putc((v >> 0) & 0xff, f);
125 snprintf(fname2,
sizeof(fname2),
"%s-a.pgm", filename);
127 f = fopen(fname2,
"w");
136 for(y = 0; y < h; y++) {
137 for(x = 0; x < w; x++) {
138 v = bitmap[y * w + x];
139 putc((v >> 24) & 0xff, f);
144 snprintf(command,
sizeof(command),
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
147 snprintf(command,
sizeof(command),
"rm %s %s", fname, fname2);
152 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
253 while (ptr && ptr->
id != object_id) {
264 while (ptr && ptr->
id != clut_id) {
275 while (ptr && ptr->
id != region_id) {
293 obj_disp_ptr = &
object->display_list;
294 obj_disp = *obj_disp_ptr;
296 while (obj_disp && obj_disp != display) {
298 obj_disp = *obj_disp_ptr;
308 while (obj2 !=
object) {
310 obj2_ptr = &obj2->
next;
314 *obj2_ptr = obj2->
next;
366 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 if (non_mod == 1 && bits == 1)
736 pixels_read += run_length;
739 bits = map_table[
bits];
740 while (run_length-- > 0 && pixels_read < dbuf_len) {
762 uint32_t *clut_table;
764 int offset_x=0, offset_y=0;
769 offset_x = display_def->
x;
770 offset_y = display_def->
y;
780 if (region && region->
dirty)
791 if (sub->num_rects > 0) {
799 for(i=0; i<sub->num_rects; i++)
800 sub->rects[i] =
av_mallocz(
sizeof(*sub->rects[i]));
804 for (display = ctx->display_list; display; display = display->next) {
813 rect = sub->rects[i];
814 rect->
x = display->x_pos + offset_x;
815 rect->
y = display->y_pos + offset_y;
816 rect->
w = region->width;
817 rect->
h = region->height;
818 rect->nb_colors = (1 << region->depth);
820 rect->pict.linesize[0] = region->width;
827 switch (region->depth) {
829 clut_table = clut->
clut4;
832 clut_table = clut->clut256;
836 clut_table = clut->clut16;
841 if (!
rect->pict.data[1]) {
845 memcpy(
rect->pict.data[1], clut_table, (1 << region->depth) *
sizeof(uint32_t));
848 if (!
rect->pict.data[0]) {
853 memcpy(
rect->pict.data[0], region->pbuf, region->buf_size);
862 for(i=0; i<sub->num_rects; i++) {
863 rect = sub->rects[i];
877 const uint8_t *
buf,
int buf_size,
int top_bottom,
int non_mod)
882 const uint8_t *buf_end = buf + buf_size;
887 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
888 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
889 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
890 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
894 av_dlog(avctx,
"DVB pixel block size %d, %s field:\n", buf_size,
895 top_bottom ?
"bottom" :
"top");
897 for (i = 0; i < buf_size; i++) {
899 av_dlog(avctx,
"0x%8p: ", buf+i);
901 av_dlog(avctx,
"%02x ", buf[i]);
916 x_pos = display->
x_pos;
917 y_pos = display->
y_pos;
921 while (buf < buf_end) {
922 if ((*buf!=0xf0 && x_pos >= region->
width) || y_pos >= region->
height) {
929 if (region->
depth == 8)
931 else if (region->
depth == 4)
937 region->
width, &buf, buf_end - buf,
938 non_mod, map_table, x_pos);
941 if (region->
depth < 4) {
946 if (region->
depth == 8)
952 region->
width, &buf, buf_end - buf,
953 non_mod, map_table, x_pos);
956 if (region->
depth < 8) {
962 region->
width, &buf, buf_end - buf,
963 non_mod, NULL, x_pos);
967 map2to4[0] = (*buf) >> 4;
968 map2to4[1] = (*buf++) & 0xf;
969 map2to4[2] = (*buf) >> 4;
970 map2to4[3] = (*buf++) & 0xf;
973 for (i = 0; i < 4; i++)
977 for (i = 0; i < 16; i++)
982 x_pos = display->
x_pos;
997 const uint8_t *buf_end = buf + buf_size;
1001 int top_field_len, bottom_field_len;
1003 int coding_method, non_modifying_color;
1013 coding_method = ((*buf) >> 2) & 3;
1014 non_modifying_color = ((*buf++) >> 1) & 1;
1016 if (coding_method == 0) {
1019 bottom_field_len =
AV_RB16(buf);
1022 if (buf + top_field_len + bottom_field_len > buf_end) {
1029 int bfl = bottom_field_len;
1032 non_modifying_color);
1034 if (bottom_field_len > 0)
1035 block = buf + top_field_len;
1037 bfl = top_field_len;
1040 non_modifying_color);
1056 const uint8_t *buf_end = buf + buf_size;
1060 int entry_id,
depth , full_range;
1062 int r,
g,
b, r_add, g_add, b_add;
1064 av_dlog(avctx,
"DVB clut packet:\n");
1066 for (i=0; i < buf_size; i++) {
1067 av_dlog(avctx,
"%02x ", buf[i]);
1076 version = ((*buf)>>4)&15;
1084 memcpy(clut, &default_clut,
sizeof(
DVBSubCLUT));
1093 if (clut->
version != version) {
1097 while (buf + 4 < buf_end) {
1100 depth = (*buf) & 0xe0;
1107 full_range = (*buf++) & 1;
1116 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1117 cb = (buf[1] << 2) & 0xf0;
1118 alpha = (buf[1] << 6) & 0xc0;
1129 av_dlog(avctx,
"clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1130 if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1131 av_dlog(avctx,
"More than one bit level marked: %x\n", depth);
1137 clut->
clut4[entry_id] =
RGBA(r,g,b,255 - alpha);
1138 else if (depth & 0x40)
1139 clut->
clut16[entry_id] =
RGBA(r,g,b,255 - alpha);
1140 else if (depth & 0x20)
1141 clut->
clut256[entry_id] =
RGBA(r,g,b,255 - alpha);
1153 const uint8_t *buf_end = buf + buf_size;
1154 int region_id, object_id;
1171 region->
id = region_id;
1178 version = ((*buf)>>4) & 15;
1179 fill = ((*buf++) >> 3) & 1;
1197 region->
depth = 1 << (((*buf++) >> 2) & 7);
1202 region->
clut = *buf++;
1204 if (region->
depth == 8) {
1210 if (region->
depth == 4)
1211 region->
bgcolor = (((*buf++) >> 4) & 15);
1213 region->
bgcolor = (((*buf++) >> 2) & 3);
1225 while (buf + 5 < buf_end) {
1234 object->id = object_id;
1239 object->
type = (*buf) >> 6;
1251 if ((object->
type == 1 || object->
type == 2) && buf+1 < buf_end) {
1260 object->display_list = display;
1271 const uint8_t *buf_end = buf + buf_size;
1281 version = ((*buf)>>4) & 15;
1282 page_state = ((*buf++) >> 2) & 3;
1284 if (ctx->
version == version) {
1291 av_dlog(avctx,
"Page time out %ds, state %d\n", ctx->
time_out, page_state);
1296 if (page_state == 1 || page_state == 2) {
1305 while (buf + 5 < buf_end) {
1309 display = tmp_display_list;
1310 tmp_ptr = &tmp_display_list;
1312 while (display && display->
region_id != region_id) {
1313 tmp_ptr = &display->
next;
1314 display = display->
next;
1327 *tmp_ptr = display->
next;
1332 av_dlog(avctx,
"Region %d, (%d,%d)\n", region_id, display->
x_pos, display->
y_pos);
1335 while (tmp_display_list) {
1336 display = tmp_display_list;
1338 tmp_display_list = display->
next;
1352 uint32_t *clut_table;
1354 int x,
y, y_off, x_off;
1357 static int fileno_index = 0;
1368 x_pos = display->
x_pos;
1369 y_pos = display->
y_pos;
1370 width = region->
width;
1373 if (display->
x_pos < x_pos) {
1374 width += (x_pos - display->
x_pos);
1375 x_pos = display->
x_pos;
1378 if (display->
y_pos < y_pos) {
1379 height += (y_pos - display->
y_pos);
1380 y_pos = display->
y_pos;
1383 if (display->
x_pos + region->
width > x_pos + width) {
1384 width = display->
x_pos + region->
width - x_pos;
1387 if (display->
y_pos + region->
height > y_pos + height) {
1400 x_off = display->
x_pos - x_pos;
1401 y_off = display->
y_pos - y_pos;
1408 switch (region->
depth) {
1410 clut_table = clut->
clut4;
1417 clut_table = clut->
clut16;
1421 for (y = 0; y < region->
height; y++) {
1422 for (x = 0; x < region->
width; x++) {
1423 pbuf[((y + y_off) * width) + x_off + x] =
1424 clut_table[region->
pbuf[y * region->
width + x]];
1430 snprintf(filename,
sizeof(filename),
"dvbs.%d", fileno_index);
1432 png_save2(filename, pbuf, width, height);
1447 int dds_version, info_byte;
1452 info_byte = bytestream_get_byte(&buf);
1453 dds_version = info_byte >> 4;
1454 if (display_def && display_def->
version == dds_version)
1458 display_def =
av_mallocz(
sizeof(*display_def));
1464 display_def->
version = dds_version;
1467 display_def->
width = bytestream_get_be16(&buf) + 1;
1468 display_def->
height = bytestream_get_be16(&buf) + 1;
1477 if (info_byte & 1<<3) {
1478 display_def->
x = bytestream_get_be16(&buf);
1479 display_def->
width = bytestream_get_be16(&buf) - display_def->
x + 1;
1480 display_def->
y = bytestream_get_be16(&buf);
1481 display_def->
height = bytestream_get_be16(&buf) - display_def->
y + 1;
1486 int buf_size,
AVSubtitle *sub,
int *got_output)
1493 save_display_set(ctx);
1499 void *
data,
int *data_size,
1503 int buf_size = avpkt->
size;
1512 int got_segment = 0;
1514 av_dlog(avctx,
"DVB sub packet:\n");
1516 for (i=0; i < buf_size; i++) {
1517 av_dlog(avctx,
"%02x ", buf[i]);
1525 if (buf_size <= 6 || *buf != 0x0f) {
1526 av_dlog(avctx,
"incomplete or broken packet");
1531 p_end = buf + buf_size;
1533 while (p_end - p >= 6 && *p == 0x0f) {
1535 segment_type = *p++;
1542 av_log(avctx,
AV_LOG_DEBUG,
"segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
1545 if (p_end - p < segment_length) {
1546 av_dlog(avctx,
"incomplete or broken packet");
1553 switch (segment_type) {
1564 if (ret < 0)
goto end;
1579 av_dlog(avctx,
"Subtitling segment type 0x%x, page id %d, length %d\n",
1580 segment_type, page_id, segment_length);
1585 p += segment_length;
1589 if (got_segment == 15) {
1607 #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
1628 .priv_class = &dvbsubdec_class,