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)
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)
129 len =
FFMIN(len, w - x);
130 memset(d + x, color, len);
146 uint32_t *rgba_palette,
147 uint32_t subtitle_color)
149 static const uint8_t level_map[4][4] = {
155 {0x00, 0x55, 0xaa, 0xff},
157 uint8_t color_used[16] = { 0 };
158 int nb_opaque_colors, i,
level, j,
r,
g,
b;
162 for(i = 0; i < 4; i++)
163 rgba_palette[i] = (ctx->
palette[colormap[i]] & 0x00ffffff)
164 | ((
alpha[i] * 17
U) << 24);
168 for(i = 0; i < 4; i++)
171 nb_opaque_colors = 0;
172 for(i = 0; i < 4; i++) {
173 if (
alpha[i] != 0 && !color_used[colormap[i]]) {
174 color_used[colormap[i]] = 1;
179 if (nb_opaque_colors == 0)
183 memset(color_used, 0, 16);
184 for(i = 0; i < 4; i++) {
186 if (!color_used[colormap[i]]) {
187 level = level_map[nb_opaque_colors - 1][j];
188 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
189 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
190 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
191 rgba_palette[i] = b | (g << 8) | (r << 16) | ((
alpha[i] * 17
U) << 24);
192 color_used[colormap[i]] = (i + 1);
195 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
206 if (sub_header->
rects) {
207 for (i = 0; i < sub_header->
num_rects; i++) {
217 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a)) 222 int cmd_pos, pos, cmd, x1, y1, x2, y2, next_cmd_pos;
223 int big_offsets, offset_size, is_8bit = 0;
230 int64_t offset1, offset2;
248 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
249 if (cmd_pos > size) {
256 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
259 ff_dlog(
NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
260 cmd_pos, next_cmd_pos, date);
261 pos = cmd_pos + 2 + offset_size;
264 x1 = y1 = x2 = y2 = 0;
265 while (pos < buf_size) {
283 if ((buf_size - pos) < 2)
285 colormap[3] = buf[pos] >> 4;
286 colormap[2] = buf[pos] & 0x0f;
287 colormap[1] = buf[pos + 1] >> 4;
288 colormap[0] = buf[pos + 1] & 0x0f;
293 if ((buf_size - pos) < 2)
295 alpha[3] = buf[pos] >> 4;
296 alpha[2] = buf[pos] & 0x0f;
297 alpha[1] = buf[pos + 1] >> 4;
298 alpha[0] = buf[pos + 1] & 0x0f;
304 if ((buf_size - pos) < 6)
306 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
307 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
308 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
309 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
312 ff_dlog(
NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
316 if ((buf_size - pos) < 4)
319 offset2 =
AV_RB16(buf + pos + 2);
320 ff_dlog(
NULL,
"offset1=0x%04"PRIx64
" offset2=0x%04"PRIx64
"\n", offset1, offset2);
324 if ((buf_size - pos) < 8)
327 offset2 =
AV_RB32(buf + pos + 4);
328 ff_dlog(
NULL,
"offset1=0x%04"PRIx64
" offset2=0x%04"PRIx64
"\n", offset1, offset2);
334 if ((buf_size - pos) < 768)
336 yuv_palette = buf + pos;
341 if ((buf_size - pos) < 256)
343 for (i = 0; i < 256; i++)
344 alpha[i] = 0xFF - buf[pos+i];
351 ff_dlog(
NULL,
"unrecognised subpicture command 0x%x\n", cmd);
356 if (offset1 >= buf_size || offset2 >= buf_size)
359 if (offset1 >= 0 && offset2 >= 0) {
370 if (w > 0 && h > 1) {
374 if (!sub_header->
rects)
377 if (!sub_header->
rects[0])
384 buf, offset1, buf_size, is_8bit) < 0)
387 buf, offset2, buf_size, is_8bit) < 0)
397 (uint32_t *)sub_header->
rects[0]->
data[1],
404 sub_header->
rects[0]->
x = x1;
405 sub_header->
rects[0]->
y = y1;
406 sub_header->
rects[0]->
w = w;
414 for (i = 0; i < 4; i++) {
422 if (next_cmd_pos < cmd_pos) {
426 if (next_cmd_pos == cmd_pos)
428 cmd_pos = next_cmd_pos;
441 for(i = 0; i <
n; i++) {
442 if (!transp_color[*buf])
452 uint8_t transp_color[256] = { 0 };
453 int y1, y2, x1, x2, y, w,
h, i;
460 if ((((uint32_t *)s->
rects[0]->
data[1])[i] >> 24) == 0)
465 1, s->
rects[0]->
w, transp_color))
467 if (y1 == s->
rects[0]->
h) {
475 s->
rects[0]->
w, transp_color))
479 s->
rects[0]->
h, transp_color))
490 for(y = 0; y <
h; y++) {
503 for (i = 0; i < 4; i++) {
514 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255) 515 static void ppm_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
516 uint32_t *rgba_palette)
520 int back[3] = {0, 255, 0};
523 f = fopen(filename,
"w");
532 for(y = 0; y <
h; y++) {
533 for(x = 0; x < w; x++) {
534 v = rgba_palette[bitmap[y * w + x]];
536 putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
537 putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
538 putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
551 if (buf_size >=
sizeof(ctx->
buf) - ctx->
buf_size) {
553 "too large SPU packets aborted.\n");
563 void *
data,
int *data_size,
585 if (is_menu ==
AVERROR(EAGAIN)) {
607 snprintf(ppm_name,
sizeof(ppm_name),
"/tmp/%05d.ppm", ctx->sub_id++);
611 ppm_save(ppm_name, sub->
rects[0]->
data[0],
627 ctx->
palette[i] = strtoul(p, &p, 16);
637 uint32_t sp_pgci, pgci, off_pgc, pgc;
639 int i, y,
cb,
cr, r_add, g_add, b_add;
644 if ((ifo = fopen(p,
"r")) ==
NULL) {
648 if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr,
"DVDVIDEO-VTS", 12)) {
653 if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
657 if (fread(&sp_pgci, 4, 1, ifo) == 1) {
659 if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
663 if (fread(&off_pgc, 4, 1, ifo) == 1) {
665 if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
669 if (fread(yuv, 64, 1, ifo) == 1) {
671 for(i=0; i<16; i++) {
677 ctx->
palette[i] = (r << 16) + (g << 8) +
b;
696 char *dataorig, *
data;
709 int pos = strcspn(
data,
"\n\r");
710 if (pos==0 && *
data==0)
713 if (strncmp(
"palette:",
data, 8) == 0) {
715 }
else if (strncmp(
"size:",
data, 5) == 0) {
717 if (sscanf(
data + 5,
"%dx%d", &w, &h) == 2) {
768 #define OFFSET(field) offsetof(DVDSubContext, field) 769 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM 793 .priv_class = &dvdsub_class,
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static void dvdsub_flush(AVCodecContext *avctx)
int x
top left corner of pict, undefined when pict is not set
static void flush(AVCodecContext *avctx)
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define YUV_TO_RGB1_CCIR(cb1, cr1)
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.
static av_cold int init(AVCodecContext *avctx)
int nb_colors
number of colors in pict, undefined when pict is not set
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Various defines for YUV<->RGB conversion.
static av_cold int dvdsub_close(AVCodecContext *avctx)
attribute_deprecated AVPicture pict
Macro definitions for various function/variable attributes.
int w
width of pict, undefined when pict is not set
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
#define av_assert0(cond)
assert() equivalent, that is always enabled.
static double cb(void *priv, double x, double y)
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
static av_cold int end(AVCodecContext *avctx)
attribute_deprecated int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
static void reset_rects(AVSubtitle *sub_header)
static int get_bits_count(const GetBitContext *s)
#define AVERROR_EOF
End of file.
bitstream reader API header.
int h
height of pict, undefined when pict is not set
static av_cold int dvdsub_init(AVCodecContext *avctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int y
top left corner of pict, undefined when pict is not set
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
const char * name
Name of the codec implementation.
static int append_to_cached_buf(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
uint32_t end_display_time
static int decode_run_8bit(GetBitContext *gb, int *color)
A bitmap, pict will be set.
#define AV_SUBTITLE_FLAG_FORCED
static const AVClass dvdsub_class
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
static int parse_ifo_palette(DVDSubContext *ctx, char *p)
static int is_transp(const uint8_t *buf, int pitch, int n, const uint8_t *transp_color)
attribute_deprecated uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
AVCodec ff_dvdsub_decoder
static void parse_palette(DVDSubContext *ctx, char *p)
Libavcodec external API header.
main external API structure.
static const AVOption options[]
static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, const uint8_t *buf, int buf_size)
static unsigned int get_bits1(GetBitContext *s)
Describe the class of an AVClass context structure.
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
static int decode_run_2bit(GetBitContext *gb, int *color)
static int find_smallest_bounding_rectangle(AVSubtitle *s)
#define FF_DISABLE_DEPRECATION_WARNINGS
common internal api header.
static int dvdsub_parse_extradata(AVCodecContext *avctx)
uint32_t start_display_time
static void guess_palette(DVDSubContext *ctx, uint32_t *rgba_palette, uint32_t subtitle_color)
#define FF_ENABLE_DEPRECATION_WARNINGS
static int dvdsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
static const uint8_t * align_get_bits(GetBitContext *s)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
static double cr(void *priv, double x, double y)
This structure stores compressed data.
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, const uint8_t *buf, int start, int buf_size, int is_8bit)