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"
32 typedef struct DVDSubContext
47 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
49 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
52 int r_add, g_add, b_add;
54 for (i = num_values; i > 0; i--) {
58 YUV_TO_RGB1_CCIR(cb, cr);
59 YUV_TO_RGB2_CCIR(r, g, b, y);
60 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
64 static int decode_run_2bit(GetBitContext *gb, int *color)
69 for (t = 1; v < t && t <= 0x40; t <<= 2)
70 v = (v << 4) | get_bits(gb, 4);
72 if (v < 4) { /* Code for fill rest of line */
78 static int decode_run_8bit(GetBitContext *gb, int *color)
81 int has_run = get_bits1(gb);
83 *color = get_bits(gb, 8);
85 *color = get_bits(gb, 2);
88 len = get_bits(gb, 7);
94 len = get_bits(gb, 3) + 2;
100 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
101 const uint8_t *buf, int start, int buf_size, int is_8bit)
105 int x, y, len, color;
108 bit_len = (buf_size - start) * 8;
109 init_get_bits(&gb, buf + start, bit_len);
115 if (get_bits_count(&gb) > bit_len)
118 len = decode_run_8bit(&gb, &color);
120 len = decode_run_2bit(&gb, &color);
121 len = FFMIN(len, w - x);
122 memset(d + x, color, len);
137 static void guess_palette(DVDSubContext* ctx,
138 uint32_t *rgba_palette,
139 uint32_t subtitle_color)
141 static const uint8_t level_map[4][4] = {
142 // this configuration (full range, lowest to highest) in tests
143 // seemed most common, so assume this
147 {0x00, 0x55, 0xaa, 0xff},
149 uint8_t color_used[16] = { 0 };
150 int nb_opaque_colors, i, level, j, r, g, b;
151 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
153 if(ctx->has_palette) {
154 for(i = 0; i < 4; i++)
155 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
156 | ((alpha[i] * 17U) << 24);
160 for(i = 0; i < 4; i++)
163 nb_opaque_colors = 0;
164 for(i = 0; i < 4; i++) {
165 if (alpha[i] != 0 && !color_used[colormap[i]]) {
166 color_used[colormap[i]] = 1;
171 if (nb_opaque_colors == 0)
175 memset(color_used, 0, 16);
176 for(i = 0; i < 4; i++) {
178 if (!color_used[colormap[i]]) {
179 level = level_map[nb_opaque_colors][j];
180 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
181 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
182 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
183 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
184 color_used[colormap[i]] = (i + 1);
187 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
188 ((alpha[i] * 17) << 24);
194 static void reset_rects(AVSubtitle *sub_header)
198 if (sub_header->rects != NULL) {
199 for (i = 0; i < sub_header->num_rects; i++) {
200 av_freep(&sub_header->rects[i]->pict.data[0]);
201 av_freep(&sub_header->rects[i]->pict.data[1]);
202 av_freep(&sub_header->rects[i]);
204 av_freep(&sub_header->rects);
205 sub_header->num_rects = 0;
209 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
211 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
212 const uint8_t *buf, int buf_size)
214 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
215 int big_offsets, offset_size, is_8bit = 0;
216 const uint8_t *yuv_palette = 0;
217 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
225 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
235 cmd_pos = READ_OFFSET(buf + cmd_pos);
237 if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
238 return AVERROR(EAGAIN);
240 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
241 date = AV_RB16(buf + cmd_pos);
242 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
243 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
244 cmd_pos, next_cmd_pos, date);
245 pos = cmd_pos + 2 + offset_size;
248 x1 = y1 = x2 = y2 = 0;
249 while (pos < buf_size) {
251 av_dlog(NULL, "cmd=%02x\n", cmd);
254 /* menu subpicture */
259 sub_header->start_display_time = (date << 10) / 90;
263 sub_header->end_display_time = (date << 10) / 90;
267 if ((buf_size - pos) < 2)
269 colormap[3] = buf[pos] >> 4;
270 colormap[2] = buf[pos] & 0x0f;
271 colormap[1] = buf[pos + 1] >> 4;
272 colormap[0] = buf[pos + 1] & 0x0f;
277 if ((buf_size - pos) < 2)
279 alpha[3] = buf[pos] >> 4;
280 alpha[2] = buf[pos] & 0x0f;
281 alpha[1] = buf[pos + 1] >> 4;
282 alpha[0] = buf[pos + 1] & 0x0f;
284 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
288 if ((buf_size - pos) < 6)
290 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
291 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
292 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
293 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
296 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
300 if ((buf_size - pos) < 4)
302 offset1 = AV_RB16(buf + pos);
303 offset2 = AV_RB16(buf + pos + 2);
304 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
308 if ((buf_size - pos) < 8)
310 offset1 = AV_RB32(buf + pos);
311 offset2 = AV_RB32(buf + pos + 4);
312 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
318 if ((buf_size - pos) < 768)
320 yuv_palette = buf + pos;
324 /* HD set contrast (alpha) */
325 if ((buf_size - pos) < 256)
327 for (i = 0; i < 256; i++)
328 alpha[i] = 0xFF - buf[pos+i];
335 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
344 /* decode the bitmap */
351 if (w > 0 && h > 0) {
352 reset_rects(sub_header);
354 bitmap = av_malloc(w * h);
355 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
356 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
357 sub_header->num_rects = 1;
358 sub_header->rects[0]->pict.data[0] = bitmap;
359 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
360 buf, offset1, buf_size, is_8bit);
361 decode_rle(bitmap + w, w * 2, w, h / 2,
362 buf, offset2, buf_size, is_8bit);
363 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
365 if (yuv_palette == 0)
367 sub_header->rects[0]->nb_colors = 256;
368 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
370 sub_header->rects[0]->nb_colors = 4;
371 guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
374 sub_header->rects[0]->x = x1;
375 sub_header->rects[0]->y = y1;
376 sub_header->rects[0]->w = w;
377 sub_header->rects[0]->h = h;
378 sub_header->rects[0]->type = SUBTITLE_BITMAP;
379 sub_header->rects[0]->pict.linesize[0] = w;
380 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
383 if (next_cmd_pos < cmd_pos) {
384 av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
387 if (next_cmd_pos == cmd_pos)
389 cmd_pos = next_cmd_pos;
391 if (sub_header->num_rects > 0)
394 reset_rects(sub_header);
398 static int is_transp(const uint8_t *buf, int pitch, int n,
399 const uint8_t *transp_color)
402 for(i = 0; i < n; i++) {
403 if (!transp_color[*buf])
410 /* return 0 if empty rectangle, 1 if non empty */
411 static int find_smallest_bounding_rectangle(AVSubtitle *s)
413 uint8_t transp_color[256] = { 0 };
414 int y1, y2, x1, x2, y, w, h, i;
417 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
420 for(i = 0; i < s->rects[0]->nb_colors; i++) {
421 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
425 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
426 1, s->rects[0]->w, transp_color))
428 if (y1 == s->rects[0]->h) {
429 av_freep(&s->rects[0]->pict.data[0]);
430 s->rects[0]->w = s->rects[0]->h = 0;
434 y2 = s->rects[0]->h - 1;
435 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
436 s->rects[0]->w, transp_color))
439 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
440 s->rects[0]->h, transp_color))
442 x2 = s->rects[0]->w - 1;
443 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
448 bitmap = av_malloc(w * h);
451 for(y = 0; y < h; y++) {
452 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
454 av_freep(&s->rects[0]->pict.data[0]);
455 s->rects[0]->pict.data[0] = bitmap;
456 s->rects[0]->pict.linesize[0] = w;
459 s->rects[0]->x += x1;
460 s->rects[0]->y += y1;
465 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
466 uint32_t *rgba_palette)
471 f = fopen(filename, "w");
480 for(y = 0; y < h; y++) {
481 for(x = 0; x < w; x++) {
482 v = rgba_palette[bitmap[y * w + x]];
483 putc((v >> 16) & 0xff, f);
484 putc((v >> 8) & 0xff, f);
485 putc((v >> 0) & 0xff, f);
492 static int append_to_cached_buf(AVCodecContext *avctx,
493 const uint8_t *buf, int buf_size)
495 DVDSubContext *ctx = avctx->priv_data;
497 if (ctx->buf_size > 0xffff - buf_size) {
498 av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
499 "too large SPU packets aborted.\n");
501 return AVERROR_INVALIDDATA;
503 ctx->buf = av_realloc(ctx->buf, ctx->buf_size + buf_size);
505 return AVERROR(ENOMEM);
506 memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
507 ctx->buf_size += buf_size;
511 static int dvdsub_decode(AVCodecContext *avctx,
512 void *data, int *data_size,
515 DVDSubContext *ctx = avctx->priv_data;
516 const uint8_t *buf = avpkt->data;
517 int buf_size = avpkt->size;
518 AVSubtitle *sub = data;
522 int ret = append_to_cached_buf(avctx, buf, buf_size);
528 buf_size = ctx->buf_size;
531 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
532 if (is_menu == AVERROR(EAGAIN)) {
534 return append_to_cached_buf(avctx, buf, buf_size);
543 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
550 snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
551 av_dlog(NULL, "start=%d ms end =%d ms\n",
552 sub->start_display_time,
553 sub->end_display_time);
554 ppm_save(ppm_name, sub->rects[0]->pict.data[0],
555 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
565 static void parse_palette(DVDSubContext *ctx, char *p)
569 ctx->has_palette = 1;
571 ctx->palette[i] = strtoul(p, &p, 16);
572 while(*p == ',' || av_isspace(*p))
577 static int dvdsub_parse_extradata(AVCodecContext *avctx)
579 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
580 char *dataorig, *data;
582 if (!avctx->extradata || !avctx->extradata_size)
585 dataorig = data = av_malloc(avctx->extradata_size+1);
587 return AVERROR(ENOMEM);
588 memcpy(data, avctx->extradata, avctx->extradata_size);
589 data[avctx->extradata_size] = '\0';
592 int pos = strcspn(data, "\n\r");
593 if (pos==0 && *data==0)
596 if (strncmp("palette:", data, 8) == 0) {
597 parse_palette(ctx, data + 8);
598 } else if (strncmp("size:", data, 5) == 0) {
600 if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
601 int ret = ff_set_dimensions(avctx, w, h);
610 data += strspn(data, "\n\r");
617 static av_cold int dvdsub_init(AVCodecContext *avctx)
619 DVDSubContext *ctx = avctx->priv_data;
622 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
625 if (ctx->palette_str)
626 parse_palette(ctx, ctx->palette_str);
627 if (ctx->has_palette) {
629 av_log(avctx, AV_LOG_DEBUG, "palette:");
631 av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
632 av_log(avctx, AV_LOG_DEBUG, "\n");
638 static av_cold int dvdsub_close(AVCodecContext *avctx)
640 DVDSubContext *ctx = avctx->priv_data;
646 #define OFFSET(field) offsetof(DVDSubContext, field)
647 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
648 static const AVOption options[] = {
649 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
652 static const AVClass dvdsub_class = {
653 .class_name = "dvdsubdec",
654 .item_name = av_default_item_name,
656 .version = LIBAVUTIL_VERSION_INT,
659 AVCodec ff_dvdsub_decoder = {
661 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
662 .type = AVMEDIA_TYPE_SUBTITLE,
663 .id = AV_CODEC_ID_DVD_SUBTITLE,
664 .priv_data_size = sizeof(DVDSubContext),
666 .decode = dvdsub_decode,
667 .close = dvdsub_close,
668 .priv_class = &dvdsub_class,