55 int r_add, g_add, b_add;
57 for (
i = num_values;
i > 0;
i--) {
63 *rgba++ = ((unsigned)*
alpha++ << 24) | (
r << 16) | (
g << 8) |
b;
72 for (t = 1; v < t && t <= 0x40; t <<= 2)
101 const uint8_t *buf,
int start,
int buf_size,
int is_8bit)
108 if (start >= buf_size)
111 if (
w <= 0 ||
h <= 0)
114 bit_len = (buf_size - start) * 8;
127 if (
len != INT_MAX &&
len >
w - x)
131 used_color[
color] = 1;
147 uint32_t *rgba_palette,
148 uint32_t subtitle_color)
150 static const uint8_t level_map[4][4] = {
156 {0x00, 0x55, 0xaa, 0xff},
158 uint8_t color_used[16] = { 0 };
159 int nb_opaque_colors,
i,
level, j,
r,
g,
b;
162 if(
ctx->has_palette) {
163 for(
i = 0;
i < 4;
i++)
164 rgba_palette[
i] = (
ctx->palette[colormap[
i]] & 0x00ffffff)
165 | ((
alpha[
i] * 17U) << 24);
169 for(
i = 0;
i < 4;
i++)
172 nb_opaque_colors = 0;
173 for(
i = 0;
i < 4;
i++) {
174 if (
alpha[
i] != 0 && !color_used[colormap[
i]]) {
175 color_used[colormap[
i]] = 1;
180 if (nb_opaque_colors == 0)
184 memset(color_used, 0, 16);
185 for(
i = 0;
i < 4;
i++) {
187 if (!color_used[colormap[
i]]) {
188 level = level_map[nb_opaque_colors - 1][j];
189 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
190 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
191 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
192 rgba_palette[
i] =
b | (
g << 8) | (
r << 16) | ((
alpha[
i] * 17U) << 24);
193 color_used[colormap[
i]] = (
i + 1);
196 rgba_palette[
i] = (rgba_palette[color_used[colormap[
i]] - 1] & 0x00ffffff) |
207 if (sub_header->
rects) {
218 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
221 const uint8_t *buf,
int buf_size)
223 int cmd_pos,
pos, cmd, x1, y1, x2, y2, next_cmd_pos;
224 int big_offsets, offset_size, is_8bit = 0;
231 int64_t offset1, offset2;
249 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
250 if (cmd_pos >
size) {
257 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
260 ff_dlog(
NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
261 cmd_pos, next_cmd_pos, date);
262 pos = cmd_pos + 2 + offset_size;
265 x1 = y1 = x2 = y2 = 0;
266 while (
pos < buf_size) {
284 if ((buf_size -
pos) < 2)
286 colormap[3] = buf[
pos] >> 4;
287 colormap[2] = buf[
pos] & 0x0f;
288 colormap[1] = buf[
pos + 1] >> 4;
289 colormap[0] = buf[
pos + 1] & 0x0f;
294 if ((buf_size -
pos) < 2)
305 if ((buf_size -
pos) < 6)
307 x1 = (buf[
pos] << 4) | (buf[
pos + 1] >> 4);
308 x2 = ((buf[
pos + 1] & 0x0f) << 8) | buf[
pos + 2];
309 y1 = (buf[
pos + 3] << 4) | (buf[
pos + 4] >> 4);
310 y2 = ((buf[
pos + 4] & 0x0f) << 8) | buf[
pos + 5];
313 ff_dlog(
NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
317 if ((buf_size -
pos) < 4)
321 ff_dlog(
NULL,
"offset1=0x%04"PRIx64
" offset2=0x%04"PRIx64
"\n", offset1, offset2);
325 if ((buf_size -
pos) < 8)
329 ff_dlog(
NULL,
"offset1=0x%04"PRIx64
" offset2=0x%04"PRIx64
"\n", offset1, offset2);
335 if ((buf_size -
pos) < 768)
337 yuv_palette = buf +
pos;
342 if ((buf_size -
pos) < 256)
344 for (
i = 0;
i < 256;
i++)
352 ff_dlog(
NULL,
"unrecognised subpicture command 0x%x\n", cmd);
357 if (offset1 >= buf_size || offset2 >= buf_size)
360 if (offset1 >= 0 && offset2 >= 0) {
371 if (
w > 0 &&
h > 1) {
373 memset(
ctx->used_color, 0,
sizeof(
ctx->used_color));
375 if (!sub_header->
rects)
378 if (!sub_header->
rects[0])
385 buf, offset1, buf_size, is_8bit) < 0)
388 buf, offset2, buf_size, is_8bit) < 0)
398 (uint32_t *)sub_header->
rects[0]->
data[1],
405 sub_header->
rects[0]->
x = x1;
406 sub_header->
rects[0]->
y = y1;
415 for (
i = 0;
i < 4;
i++) {
423 if (next_cmd_pos < cmd_pos) {
427 if (next_cmd_pos == cmd_pos)
429 cmd_pos = next_cmd_pos;
442 for(
i = 0;
i < n;
i++) {
443 if (!transp_color[*buf])
453 uint8_t transp_color[256] = { 0 };
454 int y1, y2, x1, x2, y,
w,
h,
i;
458 if (
s->num_rects == 0 || !
s->rects ||
s->rects[0]->w <= 0 ||
s->rects[0]->h <= 0)
461 for(
i = 0;
i <
s->rects[0]->nb_colors;
i++) {
462 if ((((uint32_t *)
s->rects[0]->data[1])[
i] >> 24) == 0) {
464 }
else if (
ctx->used_color[
i])
470 while (y1 < s->rects[0]->
h &&
is_transp(
s->rects[0]->data[0] + y1 *
s->rects[0]->linesize[0],
471 1,
s->rects[0]->w, transp_color))
473 if (y1 ==
s->rects[0]->h) {
475 s->rects[0]->w =
s->rects[0]->h = 0;
479 y2 =
s->rects[0]->h - 1;
480 while (y2 > 0 &&
is_transp(
s->rects[0]->data[0] + y2 *
s->rects[0]->linesize[0], 1,
481 s->rects[0]->w, transp_color))
484 while (x1 < (
s->rects[0]->w - 1) &&
is_transp(
s->rects[0]->data[0] + x1,
s->rects[0]->linesize[0],
485 s->rects[0]->h, transp_color))
487 x2 =
s->rects[0]->w - 1;
488 while (x2 > 0 &&
is_transp(
s->rects[0]->data[0] + x2,
s->rects[0]->linesize[0],
s->rects[0]->h,
496 for(y = 0; y <
h; y++) {
497 memcpy(bitmap +
w * y,
s->rects[0]->data[0] + x1 + (y1 + y) *
s->rects[0]->linesize[0],
w);
500 s->rects[0]->data[0] = bitmap;
501 s->rects[0]->linesize[0] =
w;
504 s->rects[0]->x += x1;
505 s->rects[0]->y += y1;
509 for (
i = 0;
i < 4;
i++) {
510 s->rects[0]->pict.data[
i] =
s->rects[0]->data[
i];
511 s->rects[0]->pict.linesize[
i] =
s->rects[0]->linesize[
i];
520 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
521 static void ppm_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
522 uint32_t *rgba_palette)
526 int back[3] = {0, 255, 0};
529 f = fopen(filename,
"w");
538 for(y = 0; y <
h; y++) {
539 for(x = 0; x <
w; x++) {
540 v = rgba_palette[bitmap[y *
w + x]];
542 putc(ALPHA_MIX(
alpha, back[0], (v >> 16) & 0xff),
f);
543 putc(ALPHA_MIX(
alpha, back[1], (v >> 8) & 0xff),
f);
544 putc(ALPHA_MIX(
alpha, back[2], (v >> 0) & 0xff),
f);
552 const uint8_t *buf,
int buf_size)
557 if (buf_size >=
sizeof(
ctx->buf) -
ctx->buf_size) {
559 "too large SPU packets aborted.\n");
563 memcpy(
ctx->buf +
ctx->buf_size, buf, buf_size);
564 ctx->buf_size += buf_size;
569 void *
data,
int *data_size,
574 int buf_size = avpkt->
size;
586 buf_size =
ctx->buf_size;
591 if (is_menu ==
AVERROR(EAGAIN)) {
614 snprintf(ppm_name,
sizeof(ppm_name),
"/tmp/%05d.ppm",
ctx->sub_id++);
616 sub->start_display_time,
617 sub->end_display_time);
618 ppm_save(ppm_name,
sub->rects[0]->data[0],
619 sub->rects[0]->w,
sub->rects[0]->h, (uint32_t*)
sub->rects[0]->data[1]);
632 uint32_t sp_pgci, pgci, off_pgc, pgc;
634 int i, y,
cb,
cr, r_add, g_add, b_add;
638 ctx->has_palette = 0;
639 if ((ifo = fopen(p,
"r")) ==
NULL) {
643 if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr,
"DVDVIDEO-VTS", 12)) {
648 if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
652 if (fread(&sp_pgci, 4, 1, ifo) == 1) {
654 if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
658 if (fread(&off_pgc, 4, 1, ifo) == 1) {
660 if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
664 if (fread(yuv, 64, 1, ifo) == 1) {
666 for(
i=0;
i<16;
i++) {
672 ctx->palette[
i] = (
r << 16) + (
g << 8) +
b;
675 ctx->has_palette = 1;
679 if (
ctx->has_palette == 0) {
691 char *dataorig, *
data;
704 int pos = strcspn(
data,
"\n\r");
708 if (strncmp(
"palette:",
data, 8) == 0) {
709 ctx->has_palette = 1;
711 }
else if (strncmp(
"size:",
data, 5) == 0) {
713 if (sscanf(
data + 5,
"%dx%d", &
w, &
h) == 2) {
739 if (
ctx->palette_str) {
740 ctx->has_palette = 1;
743 if (
ctx->has_palette) {
766 #define OFFSET(field) offsetof(DVDSubContext, field)
767 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
771 {
"forced_subs_only",
"Only show forced subtitles",
OFFSET(forced_subs_only),
AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,
SD},
static void flush(AVCodecContext *avctx)
Macro definitions for various function/variable attributes.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Libavcodec external API header.
static av_cold int init(AVCodecContext *avctx)
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
static float sub(float src0, float src1)
void ff_dvdsub_parse_palette(uint32_t *palette, const char *p)
static void dvdsub_flush(AVCodecContext *avctx)
AVCodec ff_dvdsub_decoder
static av_cold int dvdsub_close(AVCodecContext *avctx)
static int dvdsub_parse_extradata(AVCodecContext *avctx)
static av_cold int dvdsub_init(AVCodecContext *avctx)
static void reset_rects(AVSubtitle *sub_header)
static const AVOption options[]
static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, const uint8_t *buf, int buf_size)
static void guess_palette(DVDSubContext *ctx, uint32_t *rgba_palette, uint32_t subtitle_color)
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256], const uint8_t *buf, int start, int buf_size, int is_8bit)
static const AVClass dvdsub_class
static int decode_run_8bit(GetBitContext *gb, int *color)
static int parse_ifo_palette(DVDSubContext *ctx, char *p)
static int append_to_cached_buf(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
static int dvdsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
static int find_smallest_bounding_rectangle(DVDSubContext *ctx, AVSubtitle *s)
static int is_transp(const uint8_t *buf, int pitch, int n, const uint8_t *transp_color)
static int decode_run_2bit(GetBitContext *gb, int *color)
bitstream reader API header.
static unsigned int get_bits1(GetBitContext *s)
static int get_bits_count(const GetBitContext *s)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
static const uint8_t * align_get_bits(GetBitContext *s)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
#define AV_SUBTITLE_FLAG_FORCED
@ SUBTITLE_BITMAP
A bitmap, pict will be set.
@ AV_CODEC_ID_DVD_SUBTITLE
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
#define AVERROR_EOF
End of file.
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
const char * av_default_item_name(void *ptr)
Return the context name.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
#define LIBAVUTIL_VERSION_INT
static const int16_t alpha[]
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Various defines for YUV<->RGB conversion.
#define YUV_TO_RGB1_CCIR(cb1, cr1)
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
common internal API header
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
#define FF_DISABLE_DEPRECATION_WARNINGS
#define FF_ENABLE_DEPRECATION_WARNINGS
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Describe the class of an AVClass context structure.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
main external API structure.
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
const char * name
Name of the codec implementation.
This structure stores compressed data.
attribute_deprecated uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
attribute_deprecated int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
int x
top left corner of pict, undefined when pict is not set
int w
width of pict, undefined when pict is not set
attribute_deprecated AVPicture pict
int nb_colors
number of colors in pict, undefined when pict is not set
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
int y
top left corner of pict, undefined when pict is not set
int h
height of pict, undefined when pict is not set
uint32_t start_display_time
uint32_t end_display_time
static double cr(void *priv, double x, double y)
static double cb(void *priv, double x, double y)