55 int r_add, g_add, b_add;
57 for (i = num_values; i > 0; i--) {
63 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
72 for (t = 1; v < t && t <= 0x40; t <<= 2)
111 bit_len = (buf_size -
start) * 8;
124 len =
FFMIN(len, w - x);
125 memset(d + x, color, len);
141 uint32_t *rgba_palette,
142 uint32_t subtitle_color)
144 static const uint8_t level_map[4][4] = {
150 {0x00, 0x55, 0xaa, 0xff},
152 uint8_t color_used[16] = { 0 };
153 int nb_opaque_colors, i,
level, j,
r,
g,
b;
157 for(i = 0; i < 4; i++)
158 rgba_palette[i] = (ctx->
palette[colormap[i]] & 0x00ffffff)
159 | ((
alpha[i] * 17
U) << 24);
163 for(i = 0; i < 4; i++)
166 nb_opaque_colors = 0;
167 for(i = 0; i < 4; i++) {
168 if (
alpha[i] != 0 && !color_used[colormap[i]]) {
169 color_used[colormap[i]] = 1;
174 if (nb_opaque_colors == 0)
178 memset(color_used, 0, 16);
179 for(i = 0; i < 4; i++) {
181 if (!color_used[colormap[i]]) {
182 level = level_map[nb_opaque_colors][j];
183 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
184 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
185 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
186 rgba_palette[i] = b | (g << 8) | (r << 16) | ((
alpha[i] * 17) << 24);
187 color_used[colormap[i]] = (i + 1);
190 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
191 ((
alpha[i] * 17) << 24);
201 if (sub_header->
rects) {
202 for (i = 0; i < sub_header->
num_rects; i++) {
212 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
217 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
218 int big_offsets, offset_size, is_8bit = 0;
219 const uint8_t *yuv_palette = NULL;
240 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
243 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
246 av_dlog(NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
247 cmd_pos, next_cmd_pos, date);
248 pos = cmd_pos + 2 + offset_size;
251 x1 = y1 = x2 = y2 = 0;
252 while (pos < buf_size) {
254 av_dlog(NULL,
"cmd=%02x\n", cmd);
270 if ((buf_size - pos) < 2)
272 colormap[3] = buf[pos] >> 4;
273 colormap[2] = buf[pos] & 0x0f;
274 colormap[1] = buf[pos + 1] >> 4;
275 colormap[0] = buf[pos + 1] & 0x0f;
280 if ((buf_size - pos) < 2)
282 alpha[3] = buf[pos] >> 4;
283 alpha[2] = buf[pos] & 0x0f;
284 alpha[1] = buf[pos + 1] >> 4;
285 alpha[0] = buf[pos + 1] & 0x0f;
291 if ((buf_size - pos) < 6)
293 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
294 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
295 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
296 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
299 av_dlog(NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
303 if ((buf_size - pos) < 4)
306 offset2 =
AV_RB16(buf + pos + 2);
307 av_dlog(NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
311 if ((buf_size - pos) < 8)
314 offset2 =
AV_RB32(buf + pos + 4);
315 av_dlog(NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
321 if ((buf_size - pos) < 768)
323 yuv_palette = buf + pos;
328 if ((buf_size - pos) < 256)
330 for (i = 0; i < 256; i++)
331 alpha[i] = 0xFF - buf[pos+i];
338 av_dlog(NULL,
"unrecognised subpicture command 0x%x\n", cmd);
354 if (w > 0 && h > 0) {
363 buf, offset1, buf_size, is_8bit);
365 buf, offset2, buf_size, is_8bit);
377 sub_header->
rects[0]->
x = x1;
378 sub_header->
rects[0]->
y = y1;
379 sub_header->
rects[0]->
w = w;
380 sub_header->
rects[0]->
h = h;
386 if (next_cmd_pos < cmd_pos) {
390 if (next_cmd_pos == cmd_pos)
392 cmd_pos = next_cmd_pos;
405 for(i = 0; i <
n; i++) {
406 if (!transp_color[*buf])
416 uint8_t transp_color[256] = { 0 };
417 int y1, y2, x1, x2,
y, w, h, i;
429 1, s->
rects[0]->
w, transp_color))
431 if (y1 == s->
rects[0]->
h) {
439 s->
rects[0]->
w, transp_color))
443 s->
rects[0]->
h, transp_color))
454 for(y = 0; y < h; y++) {
468 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
469 static void ppm_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
470 uint32_t *rgba_palette)
474 int back[3] = {0, 255, 0};
477 f = fopen(filename,
"w");
486 for(y = 0; y < h; y++) {
487 for(x = 0; x < w; x++) {
488 v = rgba_palette[bitmap[y * w + x]];
490 putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
491 putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
492 putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
504 if (ctx->
buf_size > 0xffff - buf_size) {
506 "too large SPU packets aborted.\n");
519 void *
data,
int *data_size,
524 int buf_size = avpkt->
size;
539 if (is_menu ==
AVERROR(EAGAIN)) {
561 snprintf(ppm_name,
sizeof(ppm_name),
"/tmp/%05d.ppm", ctx->sub_id++);
562 av_dlog(NULL,
"start=%d ms end =%d ms\n",
582 ctx->
palette[i] = strtoul(p, &p, 16);
592 uint32_t sp_pgci, pgci, off_pgc, pgc;
594 int i,
y,
cb,
cr, r_add, g_add, b_add;
599 if ((ifo = fopen(p,
"r")) == NULL) {
603 if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr,
"DVDVIDEO-VTS", 12)) {
608 if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
612 if (fread(&sp_pgci, 4, 1, ifo) == 1) {
614 if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
618 if (fread(&off_pgc, 4, 1, ifo) == 1) {
620 if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
624 if (fread(yuv, 64, 1, ifo) == 1) {
626 for(i=0; i<16; i++) {
632 ctx->
palette[i] = (r << 16) + (g << 8) +
b;
651 char *dataorig, *
data;
664 int pos = strcspn(
data,
"\n\r");
665 if (pos==0 && *
data==0)
668 if (strncmp(
"palette:",
data, 8) == 0) {
670 }
else if (strncmp(
"size:",
data, 5) == 0) {
672 if (sscanf(
data + 5,
"%dx%d", &w, &h) == 2) {
719 #define OFFSET(field) offsetof(DVDSubContext, field)
720 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
723 {
"ifo_palette",
"obtain the global palette from .IFO file",
OFFSET(ifo_str),
AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0,
SD },
724 {
"forced_subs_only",
"Only show forced subtitles",
OFFSET(forced_subs_only),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,
SD},
743 .priv_class = &dvdsub_class,