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/attributes.h"
25 #include "libavutil/colorspace.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/avstring.h"
30 typedef struct DVDSubContext
40 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
42 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
45 int r_add, g_add, b_add;
47 for (i = num_values; i > 0; i--) {
51 YUV_TO_RGB1_CCIR(cb, cr);
52 YUV_TO_RGB2_CCIR(r, g, b, y);
53 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
57 static int decode_run_2bit(GetBitContext *gb, int *color)
62 for (t = 1; v < t && t <= 0x40; t <<= 2)
63 v = (v << 4) | get_bits(gb, 4);
65 if (v < 4) { /* Code for fill rest of line */
71 static int decode_run_8bit(GetBitContext *gb, int *color)
74 int has_run = get_bits1(gb);
76 *color = get_bits(gb, 8);
78 *color = get_bits(gb, 2);
81 len = get_bits(gb, 7);
87 len = get_bits(gb, 3) + 2;
93 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
94 const uint8_t *buf, int start, int buf_size, int is_8bit)
101 bit_len = (buf_size - start) * 8;
102 init_get_bits(&gb, buf + start, bit_len);
108 if (get_bits_count(&gb) > bit_len)
111 len = decode_run_8bit(&gb, &color);
113 len = decode_run_2bit(&gb, &color);
114 len = FFMIN(len, w - x);
115 memset(d + x, color, len);
130 static void guess_palette(DVDSubContext* ctx,
131 uint32_t *rgba_palette,
132 uint32_t subtitle_color)
134 static const uint8_t level_map[4][4] = {
135 // this configuration (full range, lowest to highest) in tests
136 // seemed most common, so assume this
140 {0x00, 0x55, 0xaa, 0xff},
142 uint8_t color_used[16] = { 0 };
143 int nb_opaque_colors, i, level, j, r, g, b;
144 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
146 if(ctx->has_palette) {
147 for(i = 0; i < 4; i++)
148 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
149 | ((alpha[i] * 17U) << 24);
153 for(i = 0; i < 4; i++)
156 nb_opaque_colors = 0;
157 for(i = 0; i < 4; i++) {
158 if (alpha[i] != 0 && !color_used[colormap[i]]) {
159 color_used[colormap[i]] = 1;
164 if (nb_opaque_colors == 0)
168 memset(color_used, 0, 16);
169 for(i = 0; i < 4; i++) {
171 if (!color_used[colormap[i]]) {
172 level = level_map[nb_opaque_colors][j];
173 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
174 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
175 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
176 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
177 color_used[colormap[i]] = (i + 1);
180 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
181 ((alpha[i] * 17) << 24);
187 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
189 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
190 const uint8_t *buf, int buf_size)
192 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
193 int big_offsets, offset_size, is_8bit = 0;
194 const uint8_t *yuv_palette = 0;
195 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
203 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
213 cmd_pos = READ_OFFSET(buf + cmd_pos);
215 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
216 date = AV_RB16(buf + cmd_pos);
217 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
218 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
219 cmd_pos, next_cmd_pos, date);
220 pos = cmd_pos + 2 + offset_size;
223 x1 = y1 = x2 = y2 = 0;
224 while (pos < buf_size) {
226 av_dlog(NULL, "cmd=%02x\n", cmd);
229 /* menu subpicture */
234 sub_header->start_display_time = (date << 10) / 90;
238 sub_header->end_display_time = (date << 10) / 90;
242 if ((buf_size - pos) < 2)
244 colormap[3] = buf[pos] >> 4;
245 colormap[2] = buf[pos] & 0x0f;
246 colormap[1] = buf[pos + 1] >> 4;
247 colormap[0] = buf[pos + 1] & 0x0f;
252 if ((buf_size - pos) < 2)
254 alpha[3] = buf[pos] >> 4;
255 alpha[2] = buf[pos] & 0x0f;
256 alpha[1] = buf[pos + 1] >> 4;
257 alpha[0] = buf[pos + 1] & 0x0f;
259 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
263 if ((buf_size - pos) < 6)
265 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
266 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
267 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
268 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
271 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
275 if ((buf_size - pos) < 4)
277 offset1 = AV_RB16(buf + pos);
278 offset2 = AV_RB16(buf + pos + 2);
279 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
283 if ((buf_size - pos) < 8)
285 offset1 = AV_RB32(buf + pos);
286 offset2 = AV_RB32(buf + pos + 4);
287 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
293 if ((buf_size - pos) < 768)
295 yuv_palette = buf + pos;
299 /* HD set contrast (alpha) */
300 if ((buf_size - pos) < 256)
302 for (i = 0; i < 256; i++)
303 alpha[i] = 0xFF - buf[pos+i];
310 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
319 /* decode the bitmap */
326 if (w > 0 && h > 0) {
327 if (sub_header->rects != NULL) {
328 for (i = 0; i < sub_header->num_rects; i++) {
329 av_freep(&sub_header->rects[i]->pict.data[0]);
330 av_freep(&sub_header->rects[i]->pict.data[1]);
331 av_freep(&sub_header->rects[i]);
333 av_freep(&sub_header->rects);
334 sub_header->num_rects = 0;
337 bitmap = av_malloc(w * h);
338 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
339 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
340 sub_header->num_rects = 1;
341 sub_header->rects[0]->pict.data[0] = bitmap;
342 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
343 buf, offset1, buf_size, is_8bit);
344 decode_rle(bitmap + w, w * 2, w, h / 2,
345 buf, offset2, buf_size, is_8bit);
346 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
348 if (yuv_palette == 0)
350 sub_header->rects[0]->nb_colors = 256;
351 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
353 sub_header->rects[0]->nb_colors = 4;
354 guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
357 sub_header->rects[0]->x = x1;
358 sub_header->rects[0]->y = y1;
359 sub_header->rects[0]->w = w;
360 sub_header->rects[0]->h = h;
361 sub_header->rects[0]->type = SUBTITLE_BITMAP;
362 sub_header->rects[0]->pict.linesize[0] = w;
363 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
366 if (next_cmd_pos < cmd_pos) {
367 av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
370 if (next_cmd_pos == cmd_pos)
372 cmd_pos = next_cmd_pos;
374 if (sub_header->num_rects > 0)
377 if (sub_header->rects != NULL) {
378 for (i = 0; i < sub_header->num_rects; i++) {
379 av_freep(&sub_header->rects[i]->pict.data[0]);
380 av_freep(&sub_header->rects[i]->pict.data[1]);
381 av_freep(&sub_header->rects[i]);
383 av_freep(&sub_header->rects);
384 sub_header->num_rects = 0;
389 static int is_transp(const uint8_t *buf, int pitch, int n,
390 const uint8_t *transp_color)
393 for(i = 0; i < n; i++) {
394 if (!transp_color[*buf])
401 /* return 0 if empty rectangle, 1 if non empty */
402 static int find_smallest_bounding_rectangle(AVSubtitle *s)
404 uint8_t transp_color[256] = { 0 };
405 int y1, y2, x1, x2, y, w, h, i;
408 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
411 for(i = 0; i < s->rects[0]->nb_colors; i++) {
412 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
416 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
417 1, s->rects[0]->w, transp_color))
419 if (y1 == s->rects[0]->h) {
420 av_freep(&s->rects[0]->pict.data[0]);
421 s->rects[0]->w = s->rects[0]->h = 0;
425 y2 = s->rects[0]->h - 1;
426 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
427 s->rects[0]->w, transp_color))
430 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
431 s->rects[0]->h, transp_color))
433 x2 = s->rects[0]->w - 1;
434 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
439 bitmap = av_malloc(w * h);
442 for(y = 0; y < h; y++) {
443 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
445 av_freep(&s->rects[0]->pict.data[0]);
446 s->rects[0]->pict.data[0] = bitmap;
447 s->rects[0]->pict.linesize[0] = w;
450 s->rects[0]->x += x1;
451 s->rects[0]->y += y1;
459 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
460 uint32_t *rgba_palette)
465 f = fopen(filename, "w");
474 for(y = 0; y < h; y++) {
475 for(x = 0; x < w; x++) {
476 v = rgba_palette[bitmap[y * w + x]];
477 putc((v >> 16) & 0xff, f);
478 putc((v >> 8) & 0xff, f);
479 putc((v >> 0) & 0xff, f);
486 static int dvdsub_decode(AVCodecContext *avctx,
487 void *data, int *data_size,
490 DVDSubContext *ctx = avctx->priv_data;
491 const uint8_t *buf = avpkt->data;
492 int buf_size = avpkt->size;
493 AVSubtitle *sub = data;
496 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
504 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
508 av_dlog(NULL, "start=%d ms end =%d ms\n",
509 sub->start_display_time,
510 sub->end_display_time);
511 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
512 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
519 static void parse_palette(DVDSubContext *ctx, char *p)
523 ctx->has_palette = 1;
525 ctx->palette[i] = strtoul(p, &p, 16);
526 while(*p == ',' || av_isspace(*p))
531 static int dvdsub_parse_extradata(AVCodecContext *avctx)
533 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
534 char *dataorig, *data;
536 if (!avctx->extradata || !avctx->extradata_size)
539 dataorig = data = av_malloc(avctx->extradata_size+1);
541 return AVERROR(ENOMEM);
542 memcpy(data, avctx->extradata, avctx->extradata_size);
543 data[avctx->extradata_size] = '\0';
546 int pos = strcspn(data, "\n\r");
547 if (pos==0 && *data==0)
550 if (strncmp("palette:", data, 8) == 0) {
551 parse_palette(ctx, data + 8);
552 } else if (strncmp("size:", data, 5) == 0) {
554 if (sscanf(data + 5, "%dx%d", &w, &h) == 2 &&
555 av_image_check_size(w, h, 0, avctx) >= 0)
556 avcodec_set_dimensions(avctx, w, h);
560 data += strspn(data, "\n\r");
567 static av_cold int dvdsub_init(AVCodecContext *avctx)
569 DVDSubContext *ctx = avctx->priv_data;
572 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
575 if (ctx->palette_str)
576 parse_palette(ctx, ctx->palette_str);
577 if (ctx->has_palette) {
579 av_log(avctx, AV_LOG_DEBUG, "palette:");
581 av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
582 av_log(avctx, AV_LOG_DEBUG, "\n");
588 #define OFFSET(field) offsetof(DVDSubContext, field)
589 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
590 static const AVOption options[] = {
591 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
594 static const AVClass class = {
595 .class_name = "dvdsubdec",
596 .item_name = av_default_item_name,
598 .version = LIBAVUTIL_VERSION_INT,
601 AVCodec ff_dvdsub_decoder = {
603 .type = AVMEDIA_TYPE_SUBTITLE,
604 .id = AV_CODEC_ID_DVD_SUBTITLE,
605 .priv_data_size = sizeof(DVDSubContext),
607 .decode = dvdsub_decode,
608 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
609 .priv_class = &class,