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
26 #include "libavutil/attributes.h"
27 #include "libavutil/colorspace.h"
28 #include "libavutil/opt.h"
29 #include "libavutil/imgutils.h"
30 #include "libavutil/avstring.h"
31 #include "libavutil/bswap.h"
33 typedef struct DVDSubContext
45 uint8_t used_color[256];
51 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
53 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
56 int r_add, g_add, b_add;
58 for (i = num_values; i > 0; i--) {
62 YUV_TO_RGB1_CCIR(cb, cr);
63 YUV_TO_RGB2_CCIR(r, g, b, y);
64 *rgba++ = ((unsigned)*alpha++ << 24) | (r << 16) | (g << 8) | b;
68 static int decode_run_2bit(GetBitContext *gb, int *color)
73 for (t = 1; v < t && t <= 0x40; t <<= 2)
74 v = (v << 4) | get_bits(gb, 4);
76 if (v < 4) { /* Code for fill rest of line */
82 static int decode_run_8bit(GetBitContext *gb, int *color)
85 int has_run = get_bits1(gb);
86 *color = get_bits(gb, 2 + 6*get_bits1(gb));
89 len = get_bits(gb, 7);
95 len = get_bits(gb, 3) + 2;
101 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256],
102 const uint8_t *buf, int start, int buf_size, int is_8bit)
106 int x, y, len, color;
109 if (start >= buf_size)
112 if (w <= 0 || h <= 0)
115 bit_len = (buf_size - start) * 8;
116 init_get_bits(&gb, buf + start, bit_len);
122 if (get_bits_count(&gb) > bit_len)
125 len = decode_run_8bit(&gb, &color);
127 len = decode_run_2bit(&gb, &color);
128 if (len != INT_MAX && len > w - x)
129 return AVERROR_INVALIDDATA;
130 len = FFMIN(len, w - x);
131 memset(d + x, color, len);
132 used_color[color] = 1;
147 static void guess_palette(DVDSubContext* ctx,
148 uint32_t *rgba_palette,
149 uint32_t subtitle_color)
151 static const uint8_t level_map[4][4] = {
152 // this configuration (full range, lowest to highest) in tests
153 // seemed most common, so assume this
157 {0x00, 0x55, 0xaa, 0xff},
159 uint8_t color_used[16] = { 0 };
160 int nb_opaque_colors, i, level, j, r, g, b;
161 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
163 if(ctx->has_palette) {
164 for(i = 0; i < 4; i++)
165 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
166 | ((alpha[i] * 17U) << 24);
170 for(i = 0; i < 4; i++)
173 nb_opaque_colors = 0;
174 for(i = 0; i < 4; i++) {
175 if (alpha[i] != 0 && !color_used[colormap[i]]) {
176 color_used[colormap[i]] = 1;
181 if (nb_opaque_colors == 0)
185 memset(color_used, 0, 16);
186 for(i = 0; i < 4; i++) {
188 if (!color_used[colormap[i]]) {
189 level = level_map[nb_opaque_colors - 1][j];
190 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
191 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
192 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
193 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17U) << 24);
194 color_used[colormap[i]] = (i + 1);
197 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
198 ((alpha[i] * 17U) << 24);
204 static void reset_rects(AVSubtitle *sub_header)
208 if (sub_header->rects) {
209 for (i = 0; i < sub_header->num_rects; i++) {
210 av_freep(&sub_header->rects[i]->data[0]);
211 av_freep(&sub_header->rects[i]->data[1]);
212 av_freep(&sub_header->rects[i]);
214 av_freep(&sub_header->rects);
215 sub_header->num_rects = 0;
219 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
221 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
222 const uint8_t *buf, int buf_size)
224 int cmd_pos, pos, cmd, x1, y1, x2, y2, next_cmd_pos;
225 int big_offsets, offset_size, is_8bit = 0;
226 const uint8_t *yuv_palette = NULL;
227 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
232 int64_t offset1, offset2;
237 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
247 size = READ_OFFSET(buf + (big_offsets ? 2 : 0));
248 cmd_pos = READ_OFFSET(buf + cmd_pos);
250 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
251 if (cmd_pos > size) {
252 av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
255 return AVERROR(EAGAIN);
258 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
259 date = AV_RB16(buf + cmd_pos);
260 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
261 ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
262 cmd_pos, next_cmd_pos, date);
263 pos = cmd_pos + 2 + offset_size;
266 x1 = y1 = x2 = y2 = 0;
267 while (pos < buf_size) {
269 ff_dlog(NULL, "cmd=%02x\n", cmd);
272 /* menu subpicture */
277 sub_header->start_display_time = (date << 10) / 90;
281 sub_header->end_display_time = (date << 10) / 90;
285 if ((buf_size - pos) < 2)
287 colormap[3] = buf[pos] >> 4;
288 colormap[2] = buf[pos] & 0x0f;
289 colormap[1] = buf[pos + 1] >> 4;
290 colormap[0] = buf[pos + 1] & 0x0f;
295 if ((buf_size - pos) < 2)
297 alpha[3] = buf[pos] >> 4;
298 alpha[2] = buf[pos] & 0x0f;
299 alpha[1] = buf[pos + 1] >> 4;
300 alpha[0] = buf[pos + 1] & 0x0f;
302 ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
306 if ((buf_size - pos) < 6)
308 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
309 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
310 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
311 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
314 ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
318 if ((buf_size - pos) < 4)
320 offset1 = AV_RB16(buf + pos);
321 offset2 = AV_RB16(buf + pos + 2);
322 ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
326 if ((buf_size - pos) < 8)
328 offset1 = AV_RB32(buf + pos);
329 offset2 = AV_RB32(buf + pos + 4);
330 ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
336 if ((buf_size - pos) < 768)
338 yuv_palette = buf + pos;
342 /* HD set contrast (alpha) */
343 if ((buf_size - pos) < 256)
345 for (i = 0; i < 256; i++)
346 alpha[i] = 0xFF - buf[pos+i];
353 ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
358 if (offset1 >= buf_size || offset2 >= buf_size)
361 if (offset1 >= 0 && offset2 >= 0) {
365 /* decode the bitmap */
372 if (w > 0 && h > 1) {
373 reset_rects(sub_header);
374 memset(ctx->used_color, 0, sizeof(ctx->used_color));
375 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
376 if (!sub_header->rects)
378 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
379 if (!sub_header->rects[0])
381 sub_header->num_rects = 1;
382 bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
385 if (decode_rle(bitmap, w * 2, w, (h + 1) / 2, ctx->used_color,
386 buf, offset1, buf_size, is_8bit) < 0)
388 if (decode_rle(bitmap + w, w * 2, w, h / 2, ctx->used_color,
389 buf, offset2, buf_size, is_8bit) < 0)
391 sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
392 if (!sub_header->rects[0]->data[1])
397 sub_header->rects[0]->nb_colors = 256;
398 yuv_a_to_rgba(yuv_palette, alpha,
399 (uint32_t *)sub_header->rects[0]->data[1],
402 sub_header->rects[0]->nb_colors = 4;
403 guess_palette(ctx, (uint32_t*)sub_header->rects[0]->data[1],
406 sub_header->rects[0]->x = x1;
407 sub_header->rects[0]->y = y1;
408 sub_header->rects[0]->w = w;
409 sub_header->rects[0]->h = h;
410 sub_header->rects[0]->type = SUBTITLE_BITMAP;
411 sub_header->rects[0]->linesize[0] = w;
412 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
415 FF_DISABLE_DEPRECATION_WARNINGS
416 for (i = 0; i < 4; i++) {
417 sub_header->rects[0]->pict.data[i] = sub_header->rects[0]->data[i];
418 sub_header->rects[0]->pict.linesize[i] = sub_header->rects[0]->linesize[i];
420 FF_ENABLE_DEPRECATION_WARNINGS
424 if (next_cmd_pos < cmd_pos) {
425 av_log(ctx, AV_LOG_ERROR, "Invalid command offset\n");
428 if (next_cmd_pos == cmd_pos)
430 cmd_pos = next_cmd_pos;
432 if (sub_header->num_rects > 0)
435 reset_rects(sub_header);
439 static int is_transp(const uint8_t *buf, int pitch, int n,
440 const uint8_t *transp_color)
443 for(i = 0; i < n; i++) {
444 if (!transp_color[*buf])
451 /* return 0 if empty rectangle, 1 if non empty */
452 static int find_smallest_bounding_rectangle(DVDSubContext *ctx, AVSubtitle *s)
454 uint8_t transp_color[256] = { 0 };
455 int y1, y2, x1, x2, y, w, h, i;
459 if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
462 for(i = 0; i < s->rects[0]->nb_colors; i++) {
463 if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0) {
465 } else if (ctx->used_color[i])
471 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
472 1, s->rects[0]->w, transp_color))
474 if (y1 == s->rects[0]->h) {
475 av_freep(&s->rects[0]->data[0]);
476 s->rects[0]->w = s->rects[0]->h = 0;
480 y2 = s->rects[0]->h - 1;
481 while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
482 s->rects[0]->w, transp_color))
485 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
486 s->rects[0]->h, transp_color))
488 x2 = s->rects[0]->w - 1;
489 while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
494 bitmap = av_malloc(w * h);
497 for(y = 0; y < h; y++) {
498 memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
500 av_freep(&s->rects[0]->data[0]);
501 s->rects[0]->data[0] = bitmap;
502 s->rects[0]->linesize[0] = w;
505 s->rects[0]->x += x1;
506 s->rects[0]->y += y1;
509 FF_DISABLE_DEPRECATION_WARNINGS
510 for (i = 0; i < 4; i++) {
511 s->rects[0]->pict.data[i] = s->rects[0]->data[i];
512 s->rects[0]->pict.linesize[i] = s->rects[0]->linesize[i];
514 FF_ENABLE_DEPRECATION_WARNINGS
521 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
522 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
523 uint32_t *rgba_palette)
527 int back[3] = {0, 255, 0}; /* green background */
530 f = fopen(filename, "w");
539 for(y = 0; y < h; y++) {
540 for(x = 0; x < w; x++) {
541 v = rgba_palette[bitmap[y * w + x]];
543 putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
544 putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
545 putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
552 static int append_to_cached_buf(AVCodecContext *avctx,
553 const uint8_t *buf, int buf_size)
555 DVDSubContext *ctx = avctx->priv_data;
557 av_assert0(buf_size >= 0 && ctx->buf_size <= sizeof(ctx->buf));
558 if (buf_size >= sizeof(ctx->buf) - ctx->buf_size) {
559 av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
560 "too large SPU packets aborted.\n");
562 return AVERROR_INVALIDDATA;
564 memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
565 ctx->buf_size += buf_size;
569 static int dvdsub_decode(AVCodecContext *avctx,
570 void *data, int *data_size,
573 DVDSubContext *ctx = avctx->priv_data;
574 const uint8_t *buf = avpkt->data;
575 int buf_size = avpkt->size;
576 AVSubtitle *sub = data;
581 int ret = append_to_cached_buf(avctx, buf, buf_size);
587 buf_size = ctx->buf_size;
591 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
592 if (is_menu == AVERROR(EAGAIN)) {
594 return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
605 if (!is_menu && find_smallest_bounding_rectangle(ctx, sub) == 0)
608 if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
615 snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
616 ff_dlog(NULL, "start=%d ms end =%d ms\n",
617 sub->start_display_time,
618 sub->end_display_time);
619 ppm_save(ppm_name, sub->rects[0]->data[0],
620 sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->data[1]);
629 static void parse_palette(DVDSubContext *ctx, char *p)
633 ctx->has_palette = 1;
635 ctx->palette[i] = strtoul(p, &p, 16);
636 while(*p == ',' || av_isspace(*p))
641 static int parse_ifo_palette(DVDSubContext *ctx, char *p)
645 uint32_t sp_pgci, pgci, off_pgc, pgc;
646 uint8_t r, g, b, yuv[65], *buf;
647 int i, y, cb, cr, r_add, g_add, b_add;
649 const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
651 ctx->has_palette = 0;
652 if ((ifo = fopen(p, "r")) == NULL) {
653 av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
656 if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
657 av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
658 ret = AVERROR_INVALIDDATA;
661 if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
662 ret = AVERROR(errno);
665 if (fread(&sp_pgci, 4, 1, ifo) == 1) {
666 pgci = av_be2ne32(sp_pgci) * 2048;
667 if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
668 ret = AVERROR(errno);
671 if (fread(&off_pgc, 4, 1, ifo) == 1) {
672 pgc = pgci + av_be2ne32(off_pgc);
673 if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
674 ret = AVERROR(errno);
677 if (fread(yuv, 64, 1, ifo) == 1) {
679 for(i=0; i<16; i++) {
683 YUV_TO_RGB1_CCIR(cb, cr);
684 YUV_TO_RGB2_CCIR(r, g, b, y);
685 ctx->palette[i] = (r << 16) + (g << 8) + b;
688 ctx->has_palette = 1;
692 if (ctx->has_palette == 0) {
693 av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
694 ret = AVERROR_INVALIDDATA;
701 static int dvdsub_parse_extradata(AVCodecContext *avctx)
703 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
704 char *dataorig, *data;
707 if (!avctx->extradata || !avctx->extradata_size)
710 dataorig = data = av_malloc(avctx->extradata_size+1);
712 return AVERROR(ENOMEM);
713 memcpy(data, avctx->extradata, avctx->extradata_size);
714 data[avctx->extradata_size] = '\0';
717 int pos = strcspn(data, "\n\r");
718 if (pos==0 && *data==0)
721 if (strncmp("palette:", data, 8) == 0) {
722 parse_palette(ctx, data + 8);
723 } else if (strncmp("size:", data, 5) == 0) {
725 if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
726 ret = ff_set_dimensions(avctx, w, h);
733 data += strspn(data, "\n\r");
741 static av_cold int dvdsub_init(AVCodecContext *avctx)
743 DVDSubContext *ctx = avctx->priv_data;
746 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
750 parse_ifo_palette(ctx, ctx->ifo_str);
751 if (ctx->palette_str)
752 parse_palette(ctx, ctx->palette_str);
753 if (ctx->has_palette) {
755 av_log(avctx, AV_LOG_DEBUG, "palette:");
757 av_log(avctx, AV_LOG_DEBUG, " 0x%06"PRIx32, ctx->palette[i]);
758 av_log(avctx, AV_LOG_DEBUG, "\n");
764 static void dvdsub_flush(AVCodecContext *avctx)
766 DVDSubContext *ctx = avctx->priv_data;
770 static av_cold int dvdsub_close(AVCodecContext *avctx)
776 #define OFFSET(field) offsetof(DVDSubContext, field)
777 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
778 static const AVOption options[] = {
779 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
780 { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
781 { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
784 static const AVClass dvdsub_class = {
785 .class_name = "dvdsubdec",
786 .item_name = av_default_item_name,
788 .version = LIBAVUTIL_VERSION_INT,
791 AVCodec ff_dvdsub_decoder = {
793 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
794 .type = AVMEDIA_TYPE_SUBTITLE,
795 .id = AV_CODEC_ID_DVD_SUBTITLE,
796 .priv_data_size = sizeof(DVDSubContext),
798 .decode = dvdsub_decode,
799 .flush = dvdsub_flush,
800 .close = dvdsub_close,
801 .priv_class = &dvdsub_class,