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"
25 #include "libavutil/opt.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avstring.h"
31 typedef struct DVDSubContext
41 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
43 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
46 int r_add, g_add, b_add;
48 for (i = num_values; i > 0; i--) {
52 YUV_TO_RGB1_CCIR(cb, cr);
53 YUV_TO_RGB2_CCIR(r, g, b, y);
54 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
58 static int decode_run_2bit(GetBitContext *gb, int *color)
63 for (t = 1; v < t && t <= 0x40; t <<= 2)
64 v = (v << 4) | get_bits(gb, 4);
66 if (v < 4) { /* Code for fill rest of line */
72 static int decode_run_8bit(GetBitContext *gb, int *color)
75 int has_run = get_bits1(gb);
77 *color = get_bits(gb, 8);
79 *color = get_bits(gb, 2);
82 len = get_bits(gb, 7);
88 len = get_bits(gb, 3) + 2;
94 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
95 const uint8_t *buf, int start, int buf_size, int is_8bit)
102 bit_len = (buf_size - start) * 8;
103 init_get_bits(&gb, buf + start, bit_len);
109 if (get_bits_count(&gb) > bit_len)
112 len = decode_run_8bit(&gb, &color);
114 len = decode_run_2bit(&gb, &color);
115 len = FFMIN(len, w - x);
116 memset(d + x, color, len);
131 static void guess_palette(DVDSubContext* ctx,
132 uint32_t *rgba_palette,
133 uint32_t subtitle_color)
135 static const uint8_t level_map[4][4] = {
136 // this configuration (full range, lowest to highest) in tests
137 // seemed most common, so assume this
141 {0x00, 0x55, 0xaa, 0xff},
143 uint8_t color_used[16] = { 0 };
144 int nb_opaque_colors, i, level, j, r, g, b;
145 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
147 if(ctx->has_palette) {
148 for(i = 0; i < 4; i++)
149 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
150 | ((alpha[i] * 17U) << 24);
154 for(i = 0; i < 4; i++)
157 nb_opaque_colors = 0;
158 for(i = 0; i < 4; i++) {
159 if (alpha[i] != 0 && !color_used[colormap[i]]) {
160 color_used[colormap[i]] = 1;
165 if (nb_opaque_colors == 0)
169 memset(color_used, 0, 16);
170 for(i = 0; i < 4; i++) {
172 if (!color_used[colormap[i]]) {
173 level = level_map[nb_opaque_colors][j];
174 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
175 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
176 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
177 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
178 color_used[colormap[i]] = (i + 1);
181 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
182 ((alpha[i] * 17) << 24);
188 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
190 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
191 const uint8_t *buf, int buf_size)
193 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
194 int big_offsets, offset_size, is_8bit = 0;
195 const uint8_t *yuv_palette = 0;
196 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
204 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
214 cmd_pos = READ_OFFSET(buf + cmd_pos);
216 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
217 date = AV_RB16(buf + cmd_pos);
218 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
219 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
220 cmd_pos, next_cmd_pos, date);
221 pos = cmd_pos + 2 + offset_size;
224 x1 = y1 = x2 = y2 = 0;
225 while (pos < buf_size) {
227 av_dlog(NULL, "cmd=%02x\n", cmd);
230 /* menu subpicture */
235 sub_header->start_display_time = (date << 10) / 90;
239 sub_header->end_display_time = (date << 10) / 90;
243 if ((buf_size - pos) < 2)
245 colormap[3] = buf[pos] >> 4;
246 colormap[2] = buf[pos] & 0x0f;
247 colormap[1] = buf[pos + 1] >> 4;
248 colormap[0] = buf[pos + 1] & 0x0f;
253 if ((buf_size - pos) < 2)
255 alpha[3] = buf[pos] >> 4;
256 alpha[2] = buf[pos] & 0x0f;
257 alpha[1] = buf[pos + 1] >> 4;
258 alpha[0] = buf[pos + 1] & 0x0f;
260 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
264 if ((buf_size - pos) < 6)
266 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
267 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
268 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
269 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
272 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
276 if ((buf_size - pos) < 4)
278 offset1 = AV_RB16(buf + pos);
279 offset2 = AV_RB16(buf + pos + 2);
280 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
284 if ((buf_size - pos) < 8)
286 offset1 = AV_RB32(buf + pos);
287 offset2 = AV_RB32(buf + pos + 4);
288 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
294 if ((buf_size - pos) < 768)
296 yuv_palette = buf + pos;
300 /* HD set contrast (alpha) */
301 if ((buf_size - pos) < 256)
303 for (i = 0; i < 256; i++)
304 alpha[i] = 0xFF - buf[pos+i];
311 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
320 /* decode the bitmap */
327 if (w > 0 && h > 0) {
328 if (sub_header->rects != NULL) {
329 for (i = 0; i < sub_header->num_rects; i++) {
330 av_freep(&sub_header->rects[i]->pict.data[0]);
331 av_freep(&sub_header->rects[i]->pict.data[1]);
332 av_freep(&sub_header->rects[i]);
334 av_freep(&sub_header->rects);
335 sub_header->num_rects = 0;
338 bitmap = av_malloc(w * h);
339 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
340 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
341 sub_header->num_rects = 1;
342 sub_header->rects[0]->pict.data[0] = bitmap;
343 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
344 buf, offset1, buf_size, is_8bit);
345 decode_rle(bitmap + w, w * 2, w, h / 2,
346 buf, offset2, buf_size, is_8bit);
347 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
349 if (yuv_palette == 0)
351 sub_header->rects[0]->nb_colors = 256;
352 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
354 sub_header->rects[0]->nb_colors = 4;
355 guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
358 sub_header->rects[0]->x = x1;
359 sub_header->rects[0]->y = y1;
360 sub_header->rects[0]->w = w;
361 sub_header->rects[0]->h = h;
362 sub_header->rects[0]->type = SUBTITLE_BITMAP;
363 sub_header->rects[0]->pict.linesize[0] = w;
364 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
367 if (next_cmd_pos < cmd_pos) {
368 av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
371 if (next_cmd_pos == cmd_pos)
373 cmd_pos = next_cmd_pos;
375 if (sub_header->num_rects > 0)
378 if (sub_header->rects != NULL) {
379 for (i = 0; i < sub_header->num_rects; i++) {
380 av_freep(&sub_header->rects[i]->pict.data[0]);
381 av_freep(&sub_header->rects[i]->pict.data[1]);
382 av_freep(&sub_header->rects[i]);
384 av_freep(&sub_header->rects);
385 sub_header->num_rects = 0;
390 static int is_transp(const uint8_t *buf, int pitch, int n,
391 const uint8_t *transp_color)
394 for(i = 0; i < n; i++) {
395 if (!transp_color[*buf])
402 /* return 0 if empty rectangle, 1 if non empty */
403 static int find_smallest_bounding_rectangle(AVSubtitle *s)
405 uint8_t transp_color[256] = { 0 };
406 int y1, y2, x1, x2, y, w, h, i;
409 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
412 for(i = 0; i < s->rects[0]->nb_colors; i++) {
413 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
417 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
418 1, s->rects[0]->w, transp_color))
420 if (y1 == s->rects[0]->h) {
421 av_freep(&s->rects[0]->pict.data[0]);
422 s->rects[0]->w = s->rects[0]->h = 0;
426 y2 = s->rects[0]->h - 1;
427 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
428 s->rects[0]->w, transp_color))
431 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
432 s->rects[0]->h, transp_color))
434 x2 = s->rects[0]->w - 1;
435 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
440 bitmap = av_malloc(w * h);
443 for(y = 0; y < h; y++) {
444 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
446 av_freep(&s->rects[0]->pict.data[0]);
447 s->rects[0]->pict.data[0] = bitmap;
448 s->rects[0]->pict.linesize[0] = w;
451 s->rects[0]->x += x1;
452 s->rects[0]->y += y1;
460 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
461 uint32_t *rgba_palette)
466 f = fopen(filename, "w");
475 for(y = 0; y < h; y++) {
476 for(x = 0; x < w; x++) {
477 v = rgba_palette[bitmap[y * w + x]];
478 putc((v >> 16) & 0xff, f);
479 putc((v >> 8) & 0xff, f);
480 putc((v >> 0) & 0xff, f);
487 static int dvdsub_decode(AVCodecContext *avctx,
488 void *data, int *data_size,
491 DVDSubContext *ctx = avctx->priv_data;
492 const uint8_t *buf = avpkt->data;
493 int buf_size = avpkt->size;
494 AVSubtitle *sub = data;
497 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
505 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
509 av_dlog(NULL, "start=%d ms end =%d ms\n",
510 sub->start_display_time,
511 sub->end_display_time);
512 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
513 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
520 static void parse_palette(DVDSubContext *ctx, char *p)
524 ctx->has_palette = 1;
526 ctx->palette[i] = strtoul(p, &p, 16);
527 while(*p == ',' || av_isspace(*p))
532 static int dvdsub_parse_extradata(AVCodecContext *avctx)
534 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
535 char *dataorig, *data;
537 if (!avctx->extradata || !avctx->extradata_size)
540 dataorig = data = av_malloc(avctx->extradata_size+1);
542 return AVERROR(ENOMEM);
543 memcpy(data, avctx->extradata, avctx->extradata_size);
544 data[avctx->extradata_size] = '\0';
547 int pos = strcspn(data, "\n\r");
548 if (pos==0 && *data==0)
551 if (strncmp("palette:", data, 8) == 0) {
552 parse_palette(ctx, data + 8);
553 } else if (strncmp("size:", data, 5) == 0) {
555 if (sscanf(data + 5, "%dx%d", &w, &h) == 2 &&
556 av_image_check_size(w, h, 0, avctx) >= 0)
557 avcodec_set_dimensions(avctx, w, h);
561 data += strspn(data, "\n\r");
568 static int dvdsub_init(AVCodecContext *avctx)
570 DVDSubContext *ctx = avctx->priv_data;
573 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
576 if (ctx->palette_str)
577 parse_palette(ctx, ctx->palette_str);
578 if (ctx->has_palette) {
580 av_log(avctx, AV_LOG_DEBUG, "palette:");
582 av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
583 av_log(avctx, AV_LOG_DEBUG, "\n");
589 #define OFFSET(field) offsetof(DVDSubContext, field)
590 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
591 static const AVOption options[] = {
592 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
595 static const AVClass class = {
596 .class_name = "dvdsubdec",
597 .item_name = av_default_item_name,
599 .version = LIBAVUTIL_VERSION_INT,
602 AVCodec ff_dvdsub_decoder = {
604 .type = AVMEDIA_TYPE_SUBTITLE,
605 .id = AV_CODEC_ID_DVD_SUBTITLE,
606 .priv_data_size = sizeof(DVDSubContext),
608 .decode = dvdsub_decode,
609 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
610 .priv_class = &class,