52 int r_add, g_add, b_add;
54 for (i = num_values; i > 0; i--) {
60 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
69 for (t = 1; v < t && t <= 0x40; t <<= 2)
108 bit_len = (buf_size -
start) * 8;
121 len =
FFMIN(len, w - x);
122 memset(d + x, color, len);
138 uint32_t *rgba_palette,
139 uint32_t subtitle_color)
141 static const uint8_t level_map[4][4] = {
147 {0x00, 0x55, 0xaa, 0xff},
149 uint8_t color_used[16] = { 0 };
150 int nb_opaque_colors, i,
level, j,
r,
g,
b;
154 for(i = 0; i < 4; i++)
155 rgba_palette[i] = (ctx->
palette[colormap[i]] & 0x00ffffff)
156 | ((
alpha[i] * 17
U) << 24);
160 for(i = 0; i < 4; i++)
163 nb_opaque_colors = 0;
164 for(i = 0; i < 4; i++) {
165 if (
alpha[i] != 0 && !color_used[colormap[i]]) {
166 color_used[colormap[i]] = 1;
171 if (nb_opaque_colors == 0)
175 memset(color_used, 0, 16);
176 for(i = 0; i < 4; i++) {
178 if (!color_used[colormap[i]]) {
179 level = level_map[nb_opaque_colors][j];
180 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
181 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
182 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
183 rgba_palette[i] = b | (g << 8) | (r << 16) | ((
alpha[i] * 17) << 24);
184 color_used[colormap[i]] = (i + 1);
187 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
188 ((
alpha[i] * 17) << 24);
198 if (sub_header->
rects) {
199 for (i = 0; i < sub_header->
num_rects; i++) {
209 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
214 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
215 int big_offsets, offset_size, is_8bit = 0;
216 const uint8_t *yuv_palette = NULL;
237 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
240 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
243 av_dlog(NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
244 cmd_pos, next_cmd_pos, date);
245 pos = cmd_pos + 2 + offset_size;
248 x1 = y1 = x2 = y2 = 0;
249 while (pos < buf_size) {
251 av_dlog(NULL,
"cmd=%02x\n", cmd);
267 if ((buf_size - pos) < 2)
269 colormap[3] = buf[pos] >> 4;
270 colormap[2] = buf[pos] & 0x0f;
271 colormap[1] = buf[pos + 1] >> 4;
272 colormap[0] = buf[pos + 1] & 0x0f;
277 if ((buf_size - pos) < 2)
279 alpha[3] = buf[pos] >> 4;
280 alpha[2] = buf[pos] & 0x0f;
281 alpha[1] = buf[pos + 1] >> 4;
282 alpha[0] = buf[pos + 1] & 0x0f;
288 if ((buf_size - pos) < 6)
290 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
291 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
292 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
293 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
296 av_dlog(NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
300 if ((buf_size - pos) < 4)
303 offset2 =
AV_RB16(buf + pos + 2);
304 av_dlog(NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
308 if ((buf_size - pos) < 8)
311 offset2 =
AV_RB32(buf + pos + 4);
312 av_dlog(NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
318 if ((buf_size - pos) < 768)
320 yuv_palette = buf + pos;
325 if ((buf_size - pos) < 256)
327 for (i = 0; i < 256; i++)
328 alpha[i] = 0xFF - buf[pos+i];
335 av_dlog(NULL,
"unrecognised subpicture command 0x%x\n", cmd);
351 if (w > 0 && h > 0) {
360 buf, offset1, buf_size, is_8bit);
362 buf, offset2, buf_size, is_8bit);
374 sub_header->
rects[0]->
x = x1;
375 sub_header->
rects[0]->
y = y1;
376 sub_header->
rects[0]->
w = w;
377 sub_header->
rects[0]->
h = h;
383 if (next_cmd_pos < cmd_pos) {
387 if (next_cmd_pos == cmd_pos)
389 cmd_pos = next_cmd_pos;
402 for(i = 0; i <
n; i++) {
403 if (!transp_color[*buf])
413 uint8_t transp_color[256] = { 0 };
414 int y1, y2, x1, x2,
y, w, h, i;
426 1, s->
rects[0]->
w, transp_color))
428 if (y1 == s->
rects[0]->
h) {
436 s->
rects[0]->
w, transp_color))
440 s->
rects[0]->
h, transp_color))
451 for(y = 0; y < h; y++) {
465 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
466 static void ppm_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
467 uint32_t *rgba_palette)
471 int back[3] = {0, 255, 0};
474 f = fopen(filename,
"w");
483 for(y = 0; y < h; y++) {
484 for(x = 0; x < w; x++) {
485 v = rgba_palette[bitmap[y * w + x]];
487 putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
488 putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
489 putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
501 if (ctx->
buf_size > 0xffff - buf_size) {
503 "too large SPU packets aborted.\n");
516 void *
data,
int *data_size,
521 int buf_size = avpkt->
size;
536 if (is_menu ==
AVERROR(EAGAIN)) {
555 snprintf(ppm_name,
sizeof(ppm_name),
"/tmp/%05d.ppm", ctx->sub_id++);
556 av_dlog(NULL,
"start=%d ms end =%d ms\n",
576 ctx->
palette[i] = strtoul(p, &p, 16);
585 char *dataorig, *
data;
597 int pos = strcspn(
data,
"\n\r");
598 if (pos==0 && *
data==0)
601 if (strncmp(
"palette:",
data, 8) == 0) {
603 }
else if (strncmp(
"size:",
data, 5) == 0) {
605 if (sscanf(
data + 5,
"%dx%d", &w, &h) == 2) {
651 #define OFFSET(field) offsetof(DVDSubContext, field)
652 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
673 .priv_class = &dvdsub_class,