19
19
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
21
#include "avcodec.h"
22
#include "bitstream.h"
23
#include "colorspace.h"
25
static int dvdsub_init_decoder(AVCodecContext *avctx)
30
static int get_nibble(const uint8_t *buf, int nibble_offset)
32
return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
28
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
30
uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
33
int r_add, g_add, b_add;
35
for (i = num_values; i > 0; i--) {
39
YUV_TO_RGB1_CCIR(cb, cr);
40
YUV_TO_RGB2_CCIR(r, g, b, y);
41
*rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
45
static int decode_run_2bit(GetBitContext *gb, int *color)
50
for (t = 1; v < t && t <= 0x40; t <<= 2)
51
v = (v << 4) | get_bits(gb, 4);
53
if (v < 4) { /* Code for fill rest of line */
59
static int decode_run_8bit(GetBitContext *gb, int *color)
62
int has_run = get_bits1(gb);
64
*color = get_bits(gb, 8);
66
*color = get_bits(gb, 2);
69
len = get_bits(gb, 7);
75
len = get_bits(gb, 3) + 2;
35
81
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
36
const uint8_t *buf, int nibble_offset, int buf_size)
82
const uint8_t *buf, int start, int buf_size, int is_8bit)
39
int x, y, len, color, nibble_end;
42
nibble_end = buf_size * 2;
89
bit_len = (buf_size - start) * 8;
90
init_get_bits(&gb, buf + start, bit_len);
47
if (nibble_offset >= nibble_end)
96
if (get_bits_count(&gb) > bit_len)
49
v = get_nibble(buf, nibble_offset++);
51
v = (v << 4) | get_nibble(buf, nibble_offset++);
53
v = (v << 4) | get_nibble(buf, nibble_offset++);
55
v = (v << 4) | get_nibble(buf, nibble_offset++);
99
len = decode_run_8bit(&gb, &color);
101
len = decode_run_2bit(&gb, &color);
102
len = FFMIN(len, w - x);
66
103
memset(d + x, color, len);
105
142
memset(color_used, 0, 16);
106
143
for(i = 0; i < 4; i++) {
107
144
if (alpha[i] != 0) {
108
if (!color_used[palette[i]]) {
145
if (!color_used[colormap[i]]) {
109
146
level = (0xff * j) / nb_opaque_colors;
110
147
r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
111
148
g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
112
149
b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
113
150
rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
114
color_used[palette[i]] = (i + 1);
151
color_used[colormap[i]] = (i + 1);
117
rgba_palette[i] = (rgba_palette[color_used[palette[i]] - 1] & 0x00ffffff) |
154
rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
118
155
((alpha[i] * 17) << 24);
161
#define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
124
163
static int decode_dvd_subtitles(AVSubtitle *sub_header,
125
164
const uint8_t *buf, int buf_size)
127
166
int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
128
uint8_t palette[4], alpha[4];
167
int big_offsets, offset_size, is_8bit = 0;
168
const uint8_t *yuv_palette = 0;
169
uint8_t colormap[4], alpha[256];
135
176
sub_header->rects = NULL;
136
177
sub_header->num_rects = 0;
137
178
sub_header->start_display_time = 0;
138
179
sub_header->end_display_time = 0;
140
cmd_pos = AV_RB16(buf + 2);
141
while ((cmd_pos + 4) < buf_size) {
181
if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
191
cmd_pos = READ_OFFSET(buf + cmd_pos);
193
while ((cmd_pos + 2 + offset_size) < buf_size) {
142
194
date = AV_RB16(buf + cmd_pos);
143
next_cmd_pos = AV_RB16(buf + cmd_pos + 2);
195
next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
145
197
av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
146
198
cmd_pos, next_cmd_pos, date);
200
pos = cmd_pos + 2 + offset_size;
151
203
x1 = y1 = x2 = y2 = 0;
168
220
sub_header->end_display_time = (date << 10) / 90;
172
224
if ((buf_size - pos) < 2)
174
palette[3] = buf[pos] >> 4;
175
palette[2] = buf[pos] & 0x0f;
176
palette[1] = buf[pos + 1] >> 4;
177
palette[0] = buf[pos + 1] & 0x0f;
226
colormap[3] = buf[pos] >> 4;
227
colormap[2] = buf[pos] & 0x0f;
228
colormap[1] = buf[pos + 1] >> 4;
229
colormap[0] = buf[pos + 1] & 0x0f;
194
247
if ((buf_size - pos) < 6)
196
249
x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
197
250
x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
198
251
y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
199
252
y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
201
256
av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
272
if ((buf_size - pos) < 8)
274
offset1 = AV_RB32(buf + pos);
275
offset2 = AV_RB32(buf + pos + 4);
277
av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
284
if ((buf_size - pos) < 768)
286
yuv_palette = buf + pos;
290
/* HD set contrast (alpha) */
291
if ((buf_size - pos) < 256)
293
for (i = 0; i < 256; i++)
294
alpha[i] = 0xFF - buf[pos+i];
302
av_log(NULL, AV_LOG_INFO, "unrecognised subpicture command 0x%x\n", cmd);
243
329
bitmap = av_malloc(w * h);
244
330
sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
245
331
sub_header->num_rects = 1;
246
sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
332
sub_header->rects[0].bitmap = bitmap;
247
333
decode_rle(bitmap, w * 2, w, (h + 1) / 2,
248
buf, offset1 * 2, buf_size);
334
buf, offset1, buf_size, is_8bit);
249
335
decode_rle(bitmap + w, w * 2, w, h / 2,
250
buf, offset2 * 2, buf_size);
251
guess_palette(sub_header->rects[0].rgba_palette,
252
palette, alpha, 0xffff00);
336
buf, offset2, buf_size, is_8bit);
338
if (yuv_palette == 0)
340
sub_header->rects[0].rgba_palette = av_malloc(256 * 4);
341
sub_header->rects[0].nb_colors = 256;
342
yuv_a_to_rgba(yuv_palette, alpha, sub_header->rects[0].rgba_palette, 256);
344
sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
345
sub_header->rects[0].nb_colors = 4;
346
guess_palette(sub_header->rects[0].rgba_palette,
347
colormap, alpha, 0xffff00);
253
349
sub_header->rects[0].x = x1;
254
350
sub_header->rects[0].y = y1;
255
351
sub_header->rects[0].w = w;
256
352
sub_header->rects[0].h = h;
257
sub_header->rects[0].nb_colors = 4;
258
353
sub_header->rects[0].linesize = w;
259
sub_header->rects[0].bitmap = bitmap;
262
356
if (next_cmd_pos == cmd_pos)
405
502
CODEC_TYPE_SUBTITLE,
406
503
CODEC_ID_DVD_SUBTITLE,
410
dvdsub_close_decoder,
414
/* parser definition */
415
typedef struct DVDSubParseContext {
419
} DVDSubParseContext;
421
static int dvdsub_parse_init(AVCodecParserContext *s)
426
static int dvdsub_parse(AVCodecParserContext *s,
427
AVCodecContext *avctx,
428
uint8_t **poutbuf, int *poutbuf_size,
429
const uint8_t *buf, int buf_size)
431
DVDSubParseContext *pc = s->priv_data;
433
if (pc->packet_index == 0) {
436
pc->packet_len = AV_RB16(buf);
437
av_freep(&pc->packet);
438
pc->packet = av_malloc(pc->packet_len);
441
if (pc->packet_index + buf_size <= pc->packet_len) {
442
memcpy(pc->packet + pc->packet_index, buf, buf_size);
443
pc->packet_index += buf_size;
444
if (pc->packet_index >= pc->packet_len) {
445
*poutbuf = pc->packet;
446
*poutbuf_size = pc->packet_len;
447
pc->packet_index = 0;
452
pc->packet_index = 0;
460
static void dvdsub_parse_close(AVCodecParserContext *s)
462
DVDSubParseContext *pc = s->priv_data;
463
av_freep(&pc->packet);
466
AVCodecParser dvdsub_parser = {
467
{ CODEC_ID_DVD_SUBTITLE },
468
sizeof(DVDSubParseContext),
509
.long_name = "DVD subtitles",