28 static void yuv_a_to_rgba(
const uint8_t *ycbcr,
const uint8_t *alpha, uint32_t *rgba,
int num_values)
33 int r_add, g_add, b_add;
35 for (i = num_values; i > 0; i--) {
41 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
50 for (t = 1; v < t && t <= 0x40; t <<= 2)
81 static int decode_rle(uint8_t *bitmap,
int linesize,
int w,
int h,
82 const uint8_t *buf,
int start,
int buf_size,
int is_8bit)
89 bit_len = (buf_size - start) * 8;
102 len =
FFMIN(len, w - x);
103 memset(d + x, color, len);
121 uint32_t subtitle_color)
123 uint8_t color_used[16];
124 int nb_opaque_colors, i,
level, j,
r,
g,
b;
126 for(i = 0; i < 4; i++)
129 memset(color_used, 0, 16);
130 nb_opaque_colors = 0;
131 for(i = 0; i < 4; i++) {
132 if (alpha[i] != 0 && !color_used[colormap[i]]) {
133 color_used[colormap[i]] = 1;
138 if (nb_opaque_colors == 0)
141 j = nb_opaque_colors;
142 memset(color_used, 0, 16);
143 for(i = 0; i < 4; i++) {
145 if (!color_used[colormap[i]]) {
146 level = (0xff * j) / nb_opaque_colors;
147 r = (((subtitle_color >> 16) & 0xff) *
level) >> 8;
148 g = (((subtitle_color >> 8) & 0xff) *
level) >> 8;
149 b = (((subtitle_color >> 0) & 0xff) *
level) >> 8;
150 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
151 color_used[colormap[i]] = (i + 1);
154 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
155 ((alpha[i] * 17) << 24);
161 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
164 const uint8_t *buf,
int buf_size)
166 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
167 int big_offsets, offset_size, is_8bit = 0;
168 const uint8_t *yuv_palette = 0;
169 uint8_t colormap[4], alpha[256];
176 memset(sub_header, 0,
sizeof(*sub_header));
190 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
193 av_dlog(
NULL,
"cmd_pos=0x%04x next=0x%04x date=%d\n",
194 cmd_pos, next_cmd_pos, date);
195 pos = cmd_pos + 2 + offset_size;
198 x1 = y1 = x2 = y2 = 0;
199 while (pos < buf_size) {
217 if ((buf_size - pos) < 2)
219 colormap[3] = buf[pos] >> 4;
220 colormap[2] = buf[pos] & 0x0f;
221 colormap[1] = buf[pos + 1] >> 4;
222 colormap[0] = buf[pos + 1] & 0x0f;
227 if ((buf_size - pos) < 2)
229 alpha[3] = buf[pos] >> 4;
230 alpha[2] = buf[pos] & 0x0f;
231 alpha[1] = buf[pos + 1] >> 4;
232 alpha[0] = buf[pos + 1] & 0x0f;
234 av_dlog(
NULL,
"alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
238 if ((buf_size - pos) < 6)
240 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
241 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
242 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
243 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
246 av_dlog(
NULL,
"x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
250 if ((buf_size - pos) < 4)
253 offset2 =
AV_RB16(buf + pos + 2);
254 av_dlog(
NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
258 if ((buf_size - pos) < 8)
261 offset2 =
AV_RB32(buf + pos + 4);
262 av_dlog(
NULL,
"offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
268 if ((buf_size - pos) < 768)
270 yuv_palette = buf + pos;
275 if ((buf_size - pos) < 256)
277 for (i = 0; i < 256; i++)
278 alpha[i] = 0xFF - buf[pos+i];
285 av_dlog(
NULL,
"unrecognised subpicture command 0x%x\n", cmd);
301 if (w > 0 && h > 0) {
303 for (i = 0; i < sub_header->
num_rects; i++) {
318 buf, offset1, buf_size, is_8bit);
320 buf, offset2, buf_size, is_8bit);
323 if (yuv_palette == 0)
330 colormap, alpha, 0xffff00);
332 sub_header->
rects[0]->
x = x1;
333 sub_header->
rects[0]->
y = y1;
334 sub_header->
rects[0]->
w = w;
335 sub_header->
rects[0]->
h = h;
340 if (next_cmd_pos == cmd_pos)
342 cmd_pos = next_cmd_pos;
348 for (i = 0; i < sub_header->
num_rects; i++) {
359 static int is_transp(
const uint8_t *buf,
int pitch,
int n,
360 const uint8_t *transp_color)
363 for(i = 0; i < n; i++) {
364 if (!transp_color[*buf])
374 uint8_t transp_color[256];
375 int y1, y2, x1, x2, y, w, h, i;
381 memset(transp_color, 0, 256);
388 1, s->
rects[0]->
w, transp_color))
390 if (y1 == s->
rects[0]->
h) {
398 s->
rects[0]->
w, transp_color))
402 s->
rects[0]->
h, transp_color))
413 for(y = 0; y < h; y++) {
430 static void ppm_save(
const char *filename, uint8_t *bitmap,
int w,
int h,
431 uint32_t *rgba_palette)
436 f = fopen(filename,
"w");
445 for(y = 0; y < h; y++) {
446 for(x = 0; x < w; x++) {
447 v = rgba_palette[bitmap[y * w + x]];
448 putc((v >> 16) & 0xff, f);
449 putc((v >> 8) & 0xff, f);
450 putc((v >> 0) & 0xff, f);
458 void *
data,
int *data_size,
461 const uint8_t *buf = avpkt->
data;
462 int buf_size = avpkt->
size;
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
AV_WL32 AV_WL24 AV_WL16 AV_WB32 AV_WB24 AV_RB16
int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
int x
top left corner of pict, undefined when pict is not set
AV_WL32 AV_WL24 AV_WL16 AV_RB32
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
#define YUV_TO_RGB1_CCIR(cb1, cr1)
static void align_get_bits(GetBitContext *s)
int nb_colors
number of colors in pict, undefined when pict is not set
Various defines for YUV<->RGB conversion.
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
int w
width of pict, undefined when pict is not set
uint8_t * data[AV_NUM_DATA_POINTERS]
static int get_bits_count(const GetBitContext *s)
bitstream reader API header.
int h
height of pict, undefined when pict is not set
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. ...
const char * name
Name of the codec implementation.
static int decode_dvd_subtitles(AVSubtitle *sub_header, const uint8_t *buf, int buf_size)
uint32_t end_display_time
static int decode_run_8bit(GetBitContext *gb, int *color)
AVPicture pict
data+linesize for the bitmap of this subtitle.
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)
static void guess_palette(uint32_t *rgba_palette, uint8_t *colormap, uint8_t *alpha, uint32_t subtitle_color)
#define av_dlog(pctx,...)
av_dlog macros Useful to print debug messages that shouldn't get compiled in normally.
AVCodec ff_dvdsub_decoder
main external API structure.
A bitmap, pict will be set.
static unsigned int get_bits1(GetBitContext *s)
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#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)
uint32_t start_display_time
static const uint8_t color[]
static int dvdsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
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)