2 * DVD subtitle decoding
3 * Copyright (c) 2005 Fabrice Bellard
5 * This file is part of FFmpeg.
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 #include "libavutil/colorspace.h"
28 typedef struct DVDSubContext
36 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
38 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
41 int r_add, g_add, b_add;
43 for (i = num_values; i > 0; i--) {
47 YUV_TO_RGB1_CCIR(cb, cr);
48 YUV_TO_RGB2_CCIR(r, g, b, y);
49 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
53 static int decode_run_2bit(GetBitContext *gb, int *color)
58 for (t = 1; v < t && t <= 0x40; t <<= 2)
59 v = (v << 4) | get_bits(gb, 4);
61 if (v < 4) { /* Code for fill rest of line */
67 static int decode_run_8bit(GetBitContext *gb, int *color)
70 int has_run = get_bits1(gb);
72 *color = get_bits(gb, 8);
74 *color = get_bits(gb, 2);
77 len = get_bits(gb, 7);
83 len = get_bits(gb, 3) + 2;
89 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
90 const uint8_t *buf, int start, int buf_size, int is_8bit)
97 bit_len = (buf_size - start) * 8;
98 init_get_bits(&gb, buf + start, bit_len);
104 if (get_bits_count(&gb) > bit_len)
107 len = decode_run_8bit(&gb, &color);
109 len = decode_run_2bit(&gb, &color);
110 len = FFMIN(len, w - x);
111 memset(d + x, color, len);
126 static void guess_palette(uint32_t *rgba_palette,
128 uint32_t subtitle_color)
130 static const uint8_t level_map[4][4] = {
131 // this configuration (full range, lowest to highest) in tests
132 // seemed most common, so assume this
136 {0x00, 0x55, 0xaa, 0xff},
138 uint8_t color_used[16] = { 0 };
139 int nb_opaque_colors, i, level, j, r, g, b;
140 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
142 if(ctx->has_palette) {
143 for(i = 0; i < 4; i++)
144 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
145 | ((alpha[i] * 17) << 24);
149 for(i = 0; i < 4; i++)
152 nb_opaque_colors = 0;
153 for(i = 0; i < 4; i++) {
154 if (alpha[i] != 0 && !color_used[colormap[i]]) {
155 color_used[colormap[i]] = 1;
160 if (nb_opaque_colors == 0)
164 memset(color_used, 0, 16);
165 for(i = 0; i < 4; i++) {
167 if (!color_used[colormap[i]]) {
168 level = level_map[nb_opaque_colors][j];
169 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
170 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
171 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
172 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
173 color_used[colormap[i]] = (i + 1);
176 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
177 ((alpha[i] * 17) << 24);
183 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
185 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
186 const uint8_t *buf, int buf_size)
188 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
189 int big_offsets, offset_size, is_8bit = 0;
190 const uint8_t *yuv_palette = 0;
191 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
199 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
209 cmd_pos = READ_OFFSET(buf + cmd_pos);
211 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
212 date = AV_RB16(buf + cmd_pos);
213 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
214 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
215 cmd_pos, next_cmd_pos, date);
216 pos = cmd_pos + 2 + offset_size;
219 x1 = y1 = x2 = y2 = 0;
220 while (pos < buf_size) {
222 av_dlog(NULL, "cmd=%02x\n", cmd);
225 /* menu subpicture */
230 sub_header->start_display_time = (date << 10) / 90;
234 sub_header->end_display_time = (date << 10) / 90;
238 if ((buf_size - pos) < 2)
240 colormap[3] = buf[pos] >> 4;
241 colormap[2] = buf[pos] & 0x0f;
242 colormap[1] = buf[pos + 1] >> 4;
243 colormap[0] = buf[pos + 1] & 0x0f;
248 if ((buf_size - pos) < 2)
250 alpha[3] = buf[pos] >> 4;
251 alpha[2] = buf[pos] & 0x0f;
252 alpha[1] = buf[pos + 1] >> 4;
253 alpha[0] = buf[pos + 1] & 0x0f;
255 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
259 if ((buf_size - pos) < 6)
261 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
262 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
263 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
264 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
267 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
271 if ((buf_size - pos) < 4)
273 offset1 = AV_RB16(buf + pos);
274 offset2 = AV_RB16(buf + pos + 2);
275 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
279 if ((buf_size - pos) < 8)
281 offset1 = AV_RB32(buf + pos);
282 offset2 = AV_RB32(buf + pos + 4);
283 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
289 if ((buf_size - pos) < 768)
291 yuv_palette = buf + pos;
295 /* HD set contrast (alpha) */
296 if ((buf_size - pos) < 256)
298 for (i = 0; i < 256; i++)
299 alpha[i] = 0xFF - buf[pos+i];
306 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
315 /* decode the bitmap */
322 if (w > 0 && h > 0) {
323 if (sub_header->rects != NULL) {
324 for (i = 0; i < sub_header->num_rects; i++) {
325 av_freep(&sub_header->rects[i]->pict.data[0]);
326 av_freep(&sub_header->rects[i]->pict.data[1]);
327 av_freep(&sub_header->rects[i]);
329 av_freep(&sub_header->rects);
330 sub_header->num_rects = 0;
333 bitmap = av_malloc(w * h);
334 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
335 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
336 sub_header->num_rects = 1;
337 sub_header->rects[0]->pict.data[0] = bitmap;
338 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
339 buf, offset1, buf_size, is_8bit);
340 decode_rle(bitmap + w, w * 2, w, h / 2,
341 buf, offset2, buf_size, is_8bit);
342 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
344 if (yuv_palette == 0)
346 sub_header->rects[0]->nb_colors = 256;
347 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
349 sub_header->rects[0]->nb_colors = 4;
350 guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1], ctx,
353 sub_header->rects[0]->x = x1;
354 sub_header->rects[0]->y = y1;
355 sub_header->rects[0]->w = w;
356 sub_header->rects[0]->h = h;
357 sub_header->rects[0]->type = SUBTITLE_BITMAP;
358 sub_header->rects[0]->pict.linesize[0] = w;
359 sub_header->rects[0]->forced = is_menu;
362 if (next_cmd_pos < cmd_pos) {
363 av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
366 if (next_cmd_pos == cmd_pos)
368 cmd_pos = next_cmd_pos;
370 if (sub_header->num_rects > 0)
373 if (sub_header->rects != NULL) {
374 for (i = 0; i < sub_header->num_rects; i++) {
375 av_freep(&sub_header->rects[i]->pict.data[0]);
376 av_freep(&sub_header->rects[i]->pict.data[1]);
377 av_freep(&sub_header->rects[i]);
379 av_freep(&sub_header->rects);
380 sub_header->num_rects = 0;
385 static int is_transp(const uint8_t *buf, int pitch, int n,
386 const uint8_t *transp_color)
389 for(i = 0; i < n; i++) {
390 if (!transp_color[*buf])
397 /* return 0 if empty rectangle, 1 if non empty */
398 static int find_smallest_bounding_rectangle(AVSubtitle *s)
400 uint8_t transp_color[256] = { 0 };
401 int y1, y2, x1, x2, y, w, h, i;
404 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
407 for(i = 0; i < s->rects[0]->nb_colors; i++) {
408 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
412 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
413 1, s->rects[0]->w, transp_color))
415 if (y1 == s->rects[0]->h) {
416 av_freep(&s->rects[0]->pict.data[0]);
417 s->rects[0]->w = s->rects[0]->h = 0;
421 y2 = s->rects[0]->h - 1;
422 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
423 s->rects[0]->w, transp_color))
426 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
427 s->rects[0]->h, transp_color))
429 x2 = s->rects[0]->w - 1;
430 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
435 bitmap = av_malloc(w * h);
438 for(y = 0; y < h; y++) {
439 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
441 av_freep(&s->rects[0]->pict.data[0]);
442 s->rects[0]->pict.data[0] = bitmap;
443 s->rects[0]->pict.linesize[0] = w;
446 s->rects[0]->x += x1;
447 s->rects[0]->y += y1;
455 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
456 uint32_t *rgba_palette)
461 f = fopen(filename, "w");
470 for(y = 0; y < h; y++) {
471 for(x = 0; x < w; x++) {
472 v = rgba_palette[bitmap[y * w + x]];
473 putc((v >> 16) & 0xff, f);
474 putc((v >> 8) & 0xff, f);
475 putc((v >> 0) & 0xff, f);
482 static int dvdsub_decode(AVCodecContext *avctx,
483 void *data, int *data_size,
486 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
487 const uint8_t *buf = avpkt->data;
488 int buf_size = avpkt->size;
489 AVSubtitle *sub = data;
492 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
500 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
504 av_dlog(NULL, "start=%d ms end =%d ms\n",
505 sub->start_display_time,
506 sub->end_display_time);
507 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
508 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
515 static int dvdsub_init(AVCodecContext *avctx)
517 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
518 char *dataorig, *data;
520 if (!avctx->extradata || !avctx->extradata_size)
523 dataorig = data = av_malloc(avctx->extradata_size+1);
525 return AVERROR(ENOMEM);
526 memcpy(data, avctx->extradata, avctx->extradata_size);
527 data[avctx->extradata_size] = '\0';
530 int pos = strcspn(data, "\n\r");
531 if (pos==0 && *data==0)
534 if (strncmp("palette:", data, 8) == 0) {
537 ctx->has_palette = 1;
539 ctx->palette[i] = strtoul(p, &p, 16);
540 while(*p == ',' || isspace(*p))
546 data += strspn(data, "\n\r");
549 if (ctx->has_palette) {
551 av_log(avctx, AV_LOG_DEBUG, "palette:");
553 av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
554 av_log(avctx, AV_LOG_DEBUG, "\n");
561 AVCodec ff_dvdsub_decoder = {
563 .type = AVMEDIA_TYPE_SUBTITLE,
564 .id = CODEC_ID_DVD_SUBTITLE,
565 .priv_data_size = sizeof(DVDSubContext),
567 .decode = dvdsub_decode,
568 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),