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"
32 typedef struct DVDSubContext
42 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
44 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
47 int r_add, g_add, b_add;
49 for (i = num_values; i > 0; i--) {
53 YUV_TO_RGB1_CCIR(cb, cr);
54 YUV_TO_RGB2_CCIR(r, g, b, y);
55 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
59 static int decode_run_2bit(GetBitContext *gb, int *color)
64 for (t = 1; v < t && t <= 0x40; t <<= 2)
65 v = (v << 4) | get_bits(gb, 4);
67 if (v < 4) { /* Code for fill rest of line */
73 static int decode_run_8bit(GetBitContext *gb, int *color)
76 int has_run = get_bits1(gb);
78 *color = get_bits(gb, 8);
80 *color = get_bits(gb, 2);
83 len = get_bits(gb, 7);
89 len = get_bits(gb, 3) + 2;
95 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
96 const uint8_t *buf, int start, int buf_size, int is_8bit)
100 int x, y, len, color;
103 bit_len = (buf_size - start) * 8;
104 init_get_bits(&gb, buf + start, bit_len);
110 if (get_bits_count(&gb) > bit_len)
113 len = decode_run_8bit(&gb, &color);
115 len = decode_run_2bit(&gb, &color);
116 len = FFMIN(len, w - x);
117 memset(d + x, color, len);
132 static void guess_palette(DVDSubContext* ctx,
133 uint32_t *rgba_palette,
134 uint32_t subtitle_color)
136 static const uint8_t level_map[4][4] = {
137 // this configuration (full range, lowest to highest) in tests
138 // seemed most common, so assume this
142 {0x00, 0x55, 0xaa, 0xff},
144 uint8_t color_used[16] = { 0 };
145 int nb_opaque_colors, i, level, j, r, g, b;
146 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
148 if(ctx->has_palette) {
149 for(i = 0; i < 4; i++)
150 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
151 | ((alpha[i] * 17U) << 24);
155 for(i = 0; i < 4; i++)
158 nb_opaque_colors = 0;
159 for(i = 0; i < 4; i++) {
160 if (alpha[i] != 0 && !color_used[colormap[i]]) {
161 color_used[colormap[i]] = 1;
166 if (nb_opaque_colors == 0)
170 memset(color_used, 0, 16);
171 for(i = 0; i < 4; i++) {
173 if (!color_used[colormap[i]]) {
174 level = level_map[nb_opaque_colors][j];
175 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
176 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
177 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
178 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
179 color_used[colormap[i]] = (i + 1);
182 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
183 ((alpha[i] * 17) << 24);
189 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
191 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
192 const uint8_t *buf, int buf_size)
194 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
195 int big_offsets, offset_size, is_8bit = 0;
196 const uint8_t *yuv_palette = 0;
197 uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
205 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
215 cmd_pos = READ_OFFSET(buf + cmd_pos);
217 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
218 date = AV_RB16(buf + cmd_pos);
219 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
220 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
221 cmd_pos, next_cmd_pos, date);
222 pos = cmd_pos + 2 + offset_size;
225 x1 = y1 = x2 = y2 = 0;
226 while (pos < buf_size) {
228 av_dlog(NULL, "cmd=%02x\n", cmd);
231 /* menu subpicture */
236 sub_header->start_display_time = (date << 10) / 90;
240 sub_header->end_display_time = (date << 10) / 90;
244 if ((buf_size - pos) < 2)
246 colormap[3] = buf[pos] >> 4;
247 colormap[2] = buf[pos] & 0x0f;
248 colormap[1] = buf[pos + 1] >> 4;
249 colormap[0] = buf[pos + 1] & 0x0f;
254 if ((buf_size - pos) < 2)
256 alpha[3] = buf[pos] >> 4;
257 alpha[2] = buf[pos] & 0x0f;
258 alpha[1] = buf[pos + 1] >> 4;
259 alpha[0] = buf[pos + 1] & 0x0f;
261 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
265 if ((buf_size - pos) < 6)
267 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
268 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
269 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
270 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
273 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
277 if ((buf_size - pos) < 4)
279 offset1 = AV_RB16(buf + pos);
280 offset2 = AV_RB16(buf + pos + 2);
281 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
285 if ((buf_size - pos) < 8)
287 offset1 = AV_RB32(buf + pos);
288 offset2 = AV_RB32(buf + pos + 4);
289 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
295 if ((buf_size - pos) < 768)
297 yuv_palette = buf + pos;
301 /* HD set contrast (alpha) */
302 if ((buf_size - pos) < 256)
304 for (i = 0; i < 256; i++)
305 alpha[i] = 0xFF - buf[pos+i];
312 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
321 /* decode the bitmap */
328 if (w > 0 && h > 0) {
329 if (sub_header->rects != NULL) {
330 for (i = 0; i < sub_header->num_rects; i++) {
331 av_freep(&sub_header->rects[i]->pict.data[0]);
332 av_freep(&sub_header->rects[i]->pict.data[1]);
333 av_freep(&sub_header->rects[i]);
335 av_freep(&sub_header->rects);
336 sub_header->num_rects = 0;
339 bitmap = av_malloc(w * h);
340 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
341 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
342 sub_header->num_rects = 1;
343 sub_header->rects[0]->pict.data[0] = bitmap;
344 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
345 buf, offset1, buf_size, is_8bit);
346 decode_rle(bitmap + w, w * 2, w, h / 2,
347 buf, offset2, buf_size, is_8bit);
348 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
350 if (yuv_palette == 0)
352 sub_header->rects[0]->nb_colors = 256;
353 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
355 sub_header->rects[0]->nb_colors = 4;
356 guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
359 sub_header->rects[0]->x = x1;
360 sub_header->rects[0]->y = y1;
361 sub_header->rects[0]->w = w;
362 sub_header->rects[0]->h = h;
363 sub_header->rects[0]->type = SUBTITLE_BITMAP;
364 sub_header->rects[0]->pict.linesize[0] = w;
365 sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
368 if (next_cmd_pos < cmd_pos) {
369 av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
372 if (next_cmd_pos == cmd_pos)
374 cmd_pos = next_cmd_pos;
376 if (sub_header->num_rects > 0)
379 if (sub_header->rects != NULL) {
380 for (i = 0; i < sub_header->num_rects; i++) {
381 av_freep(&sub_header->rects[i]->pict.data[0]);
382 av_freep(&sub_header->rects[i]->pict.data[1]);
383 av_freep(&sub_header->rects[i]);
385 av_freep(&sub_header->rects);
386 sub_header->num_rects = 0;
391 static int is_transp(const uint8_t *buf, int pitch, int n,
392 const uint8_t *transp_color)
395 for(i = 0; i < n; i++) {
396 if (!transp_color[*buf])
403 /* return 0 if empty rectangle, 1 if non empty */
404 static int find_smallest_bounding_rectangle(AVSubtitle *s)
406 uint8_t transp_color[256] = { 0 };
407 int y1, y2, x1, x2, y, w, h, i;
410 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
413 for(i = 0; i < s->rects[0]->nb_colors; i++) {
414 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
418 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
419 1, s->rects[0]->w, transp_color))
421 if (y1 == s->rects[0]->h) {
422 av_freep(&s->rects[0]->pict.data[0]);
423 s->rects[0]->w = s->rects[0]->h = 0;
427 y2 = s->rects[0]->h - 1;
428 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
429 s->rects[0]->w, transp_color))
432 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
433 s->rects[0]->h, transp_color))
435 x2 = s->rects[0]->w - 1;
436 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
441 bitmap = av_malloc(w * h);
444 for(y = 0; y < h; y++) {
445 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
447 av_freep(&s->rects[0]->pict.data[0]);
448 s->rects[0]->pict.data[0] = bitmap;
449 s->rects[0]->pict.linesize[0] = w;
452 s->rects[0]->x += x1;
453 s->rects[0]->y += y1;
461 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
462 uint32_t *rgba_palette)
467 f = fopen(filename, "w");
476 for(y = 0; y < h; y++) {
477 for(x = 0; x < w; x++) {
478 v = rgba_palette[bitmap[y * w + x]];
479 putc((v >> 16) & 0xff, f);
480 putc((v >> 8) & 0xff, f);
481 putc((v >> 0) & 0xff, f);
488 static int dvdsub_decode(AVCodecContext *avctx,
489 void *data, int *data_size,
492 DVDSubContext *ctx = avctx->priv_data;
493 const uint8_t *buf = avpkt->data;
494 int buf_size = avpkt->size;
495 AVSubtitle *sub = data;
498 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
506 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
510 av_dlog(NULL, "start=%d ms end =%d ms\n",
511 sub->start_display_time,
512 sub->end_display_time);
513 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
514 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
521 static void parse_palette(DVDSubContext *ctx, char *p)
525 ctx->has_palette = 1;
527 ctx->palette[i] = strtoul(p, &p, 16);
528 while(*p == ',' || av_isspace(*p))
533 static int dvdsub_parse_extradata(AVCodecContext *avctx)
535 DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
536 char *dataorig, *data;
538 if (!avctx->extradata || !avctx->extradata_size)
541 dataorig = data = av_malloc(avctx->extradata_size+1);
543 return AVERROR(ENOMEM);
544 memcpy(data, avctx->extradata, avctx->extradata_size);
545 data[avctx->extradata_size] = '\0';
548 int pos = strcspn(data, "\n\r");
549 if (pos==0 && *data==0)
552 if (strncmp("palette:", data, 8) == 0) {
553 parse_palette(ctx, data + 8);
554 } else if (strncmp("size:", data, 5) == 0) {
556 if (sscanf(data + 5, "%dx%d", &w, &h) == 2 &&
557 av_image_check_size(w, h, 0, avctx) >= 0)
558 avcodec_set_dimensions(avctx, w, h);
562 data += strspn(data, "\n\r");
569 static av_cold int dvdsub_init(AVCodecContext *avctx)
571 DVDSubContext *ctx = avctx->priv_data;
574 if ((ret = dvdsub_parse_extradata(avctx)) < 0)
577 if (ctx->palette_str)
578 parse_palette(ctx, ctx->palette_str);
579 if (ctx->has_palette) {
581 av_log(avctx, AV_LOG_DEBUG, "palette:");
583 av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
584 av_log(avctx, AV_LOG_DEBUG, "\n");
590 #define OFFSET(field) offsetof(DVDSubContext, field)
591 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
592 static const AVOption options[] = {
593 { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
596 static const AVClass class = {
597 .class_name = "dvdsubdec",
598 .item_name = av_default_item_name,
600 .version = LIBAVUTIL_VERSION_INT,
603 AVCodec ff_dvdsub_decoder = {
605 .type = AVMEDIA_TYPE_SUBTITLE,
606 .id = AV_CODEC_ID_DVD_SUBTITLE,
607 .priv_data_size = sizeof(DVDSubContext),
609 .decode = dvdsub_decode,
610 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
611 .priv_class = &class,