24 #include "libavutil/attributes.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/avstring.h"
50 int r_add, g_add, b_add;
52 for (i = num_values; i > 0; i--) {
58 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
67 for (t = 1; v < t && t <= 0x40; t <<= 2)
106 bit_len = (buf_size -
start) * 8;
119 len =
FFMIN(len, w - x);
120 memset(d + x, color, len);
136 uint32_t *rgba_palette,
137 uint32_t subtitle_color)
139 static const uint8_t level_map[4][4] = {
145 {0x00, 0x55, 0xaa, 0xff},
147 uint8_t color_used[16] = { 0 };
148 int nb_opaque_colors, i,
level, j,
r,
g,
b;
152 for(i = 0; i < 4; i++)
153 rgba_palette[i] = (ctx->
palette[colormap[i]] & 0x00ffffff)
154 | ((
alpha[i] * 17
U) << 24);
158 for(i = 0; i < 4; i++)
161 nb_opaque_colors = 0;
162 for(i = 0; i < 4; i++) {
163 if (
alpha[i] != 0 && !color_used[colormap[i]]) {
164 color_used[colormap[i]] = 1;
169 if (nb_opaque_colors == 0)
173 memset(color_used, 0, 16);
174 for(i = 0; i < 4; i++) {
176 if (!color_used[colormap[i]]) {
177 level = level_map[nb_opaque_colors][j];
178 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
179 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
180 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
181 rgba_palette[i] = b | (g << 8) | (r << 16) | ((
alpha[i] * 17) << 24);
182 color_used[colormap[i]] = (i + 1);
185 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
186 ((
alpha[i] * 17) << 24);
196 if (sub_header->
rects != NULL) {
197 for (i = 0; i < sub_header->
num_rects; i++) {
207 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
212 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
213 int big_offsets, offset_size, is_8bit = 0;
214 const uint8_t *yuv_palette = 0;
235 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
238 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
241 av_dlog(NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
242 cmd_pos, next_cmd_pos, date);
243 pos = cmd_pos + 2 + offset_size;
246 x1 = y1 = x2 = y2 = 0;
247 while (pos < buf_size) {
249 av_dlog(NULL,
"cmd=%02x\n", cmd);
265 if ((buf_size - pos) < 2)
267 colormap[3] = buf[pos] >> 4;
268 colormap[2] = buf[pos] & 0x0f;
269 colormap[1] = buf[pos + 1] >> 4;
270 colormap[0] = buf[pos + 1] & 0x0f;
275 if ((buf_size - pos) < 2)
277 alpha[3] = buf[pos] >> 4;
278 alpha[2] = buf[pos] & 0x0f;
279 alpha[1] = buf[pos + 1] >> 4;
280 alpha[0] = buf[pos + 1] & 0x0f;
286 if ((buf_size - pos) < 6)
288 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
289 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
290 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
291 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
294 av_dlog(NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
298 if ((buf_size - pos) < 4)
301 offset2 =
AV_RB16(buf + pos + 2);
302 av_dlog(NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
306 if ((buf_size - pos) < 8)
309 offset2 =
AV_RB32(buf + pos + 4);
310 av_dlog(NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
316 if ((buf_size - pos) < 768)
318 yuv_palette = buf + pos;
323 if ((buf_size - pos) < 256)
325 for (i = 0; i < 256; i++)
326 alpha[i] = 0xFF - buf[pos+i];
333 av_dlog(NULL,
"unrecognised subpicture command 0x%x\n", cmd);
349 if (w > 0 && h > 0) {
358 buf, offset1, buf_size, is_8bit);
360 buf, offset2, buf_size, is_8bit);
363 if (yuv_palette == 0)
372 sub_header->
rects[0]->
x = x1;
373 sub_header->
rects[0]->
y = y1;
374 sub_header->
rects[0]->
w = w;
375 sub_header->
rects[0]->
h = h;
381 if (next_cmd_pos < cmd_pos) {
385 if (next_cmd_pos == cmd_pos)
387 cmd_pos = next_cmd_pos;
400 for(i = 0; i <
n; i++) {
401 if (!transp_color[*buf])
411 uint8_t transp_color[256] = { 0 };
412 int y1, y2, x1, x2,
y, w, h, i;
424 1, s->
rects[0]->
w, transp_color))
426 if (y1 == s->
rects[0]->
h) {
434 s->
rects[0]->
w, transp_color))
438 s->
rects[0]->
h, transp_color))
449 for(y = 0; y < h; y++) {
466 static void ppm_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
467 uint32_t *rgba_palette)
472 f = fopen(filename,
"w");
481 for(y = 0; y < h; y++) {
482 for(x = 0; x < w; x++) {
483 v = rgba_palette[bitmap[y * w + x]];
484 putc((v >> 16) & 0xff, f);
485 putc((v >> 8) & 0xff, f);
486 putc((v >> 0) & 0xff, f);
498 if (ctx->
buf_size > 0xffff - buf_size) {
500 "too large SPU packets aborted.\n");
513 void *
data,
int *data_size,
518 int buf_size = avpkt->
size;
533 if (is_menu ==
AVERROR(EAGAIN)) {
551 snprintf(ppm_name,
sizeof(ppm_name),
"/tmp/%05d.ppm", ctx->sub_id++);
552 av_dlog(NULL,
"start=%d ms end =%d ms\n",
572 ctx->
palette[i] = strtoul(p, &p, 16);
581 char *dataorig, *
data;
593 int pos = strcspn(
data,
"\n\r");
594 if (pos==0 && *
data==0)
597 if (strncmp(
"palette:",
data, 8) == 0) {
599 }
else if (strncmp(
"size:",
data, 5) == 0) {
601 if (sscanf(
data + 5,
"%dx%d", &w, &h) == 2 &&
643 #define OFFSET(field) offsetof(DVDSubContext, field)
644 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
665 .priv_class = &dvdsub_class,
int x
top left corner of pict, undefined when pict is not set
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
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)
static av_cold int init(AVCodecContext *avctx)
void avcodec_set_dimensions(AVCodecContext *s, int width, int height)
int nb_colors
number of colors in pict, undefined when pict is not set
Various defines for YUV<->RGB conversion.
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level...
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
static av_cold int dvdsub_close(AVCodecContext *avctx)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
void av_freep(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
if((e=av_dict_get(options,"", NULL, AV_DICT_IGNORE_SUFFIX)))
int w
width of pict, undefined when pict is not set
static double cb(void *priv, double x, double y)
const char * av_default_item_name(void *ctx)
Return the context name.
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
const char * name
Name of the codec implementation.
static void reset_rects(AVSubtitle *sub_header)
static int get_bits_count(const GetBitContext *s)
bitstream reader API header.
int h
height of pict, undefined when pict is not set
static av_cold int dvdsub_init(AVCodecContext *avctx)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
const OptionDef options[]
static double alpha(void *priv, double x, double y)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
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.
static int append_to_cached_buf(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Libavcodec external API header.
uint32_t end_display_time
static int decode_run_8bit(GetBitContext *gb, int *color)
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
A bitmap, pict will be set.
#define AV_SUBTITLE_FLAG_FORCED
static const AVClass dvdsub_class
AVPicture pict
data+linesize for the bitmap of this subtitle.
void * av_realloc(void *ptr, size_t size) 1(2)
Allocate or reallocate a block of memory.
void * av_malloc(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
static int is_transp(const uint8_t *buf, int pitch, int n, const uint8_t *transp_color)
AVCodec ff_dvdsub_decoder
static void parse_palette(DVDSubContext *ctx, char *p)
main external API structure.
static void close(AVCodecParserContext *s)
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)
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 AVERROR_INVALIDDATA
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, void *data, int *got_frame, AVPacket *avpkt)
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)
void * av_mallocz(size_t size) av_malloc_attrib 1(1)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...