2 * DVD subtitle decoding
3 * Copyright (c) 2005 Fabrice Bellard
5 * This file is part of Libav.
7 * Libav 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 * Libav 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 Libav; 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/imgutils.h"
26 #include "libavutil/avstring.h"
30 typedef struct DVDSubContext {
35 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
37 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
40 int r_add, g_add, b_add;
42 for (i = num_values; i > 0; i--) {
46 YUV_TO_RGB1_CCIR(cb, cr);
47 YUV_TO_RGB2_CCIR(r, g, b, y);
48 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
52 static int decode_run_2bit(GetBitContext *gb, int *color)
57 for (t = 1; v < t && t <= 0x40; t <<= 2)
58 v = (v << 4) | get_bits(gb, 4);
60 if (v < 4) { /* Code for fill rest of line */
66 static int decode_run_8bit(GetBitContext *gb, int *color)
69 int has_run = get_bits1(gb);
71 *color = get_bits(gb, 8);
73 *color = get_bits(gb, 2);
76 len = get_bits(gb, 7);
82 len = get_bits(gb, 3) + 2;
88 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
89 const uint8_t *buf, int start, int buf_size, int is_8bit)
96 bit_len = (buf_size - start) * 8;
97 init_get_bits(&gb, buf + start, bit_len);
103 if (get_bits_count(&gb) > bit_len)
106 len = decode_run_8bit(&gb, &color);
108 len = decode_run_2bit(&gb, &color);
109 len = FFMIN(len, w - x);
110 memset(d + x, color, len);
125 static void guess_palette(DVDSubContext* ctx,
126 uint32_t *rgba_palette,
129 uint32_t subtitle_color)
131 uint8_t color_used[16] = { 0 };
132 int nb_opaque_colors, i, level, j, r, g, b;
134 if (ctx->has_palette) {
135 for (i = 0; i < 4; i++)
136 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
137 | ((alpha[i] * 17) << 24);
141 for(i = 0; i < 4; i++)
144 nb_opaque_colors = 0;
145 for(i = 0; i < 4; i++) {
146 if (alpha[i] != 0 && !color_used[colormap[i]]) {
147 color_used[colormap[i]] = 1;
152 if (nb_opaque_colors == 0)
155 j = nb_opaque_colors;
156 memset(color_used, 0, 16);
157 for(i = 0; i < 4; i++) {
159 if (!color_used[colormap[i]]) {
160 level = (0xff * j) / nb_opaque_colors;
161 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
162 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
163 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
164 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
165 color_used[colormap[i]] = (i + 1);
168 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
169 ((alpha[i] * 17) << 24);
175 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
177 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
178 const uint8_t *buf, int buf_size)
180 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
181 int big_offsets, offset_size, is_8bit = 0;
182 const uint8_t *yuv_palette = 0;
183 uint8_t colormap[4] = { 0 }, alpha[256] = { 0 };
190 memset(sub_header, 0, sizeof(*sub_header));
192 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
202 cmd_pos = READ_OFFSET(buf + cmd_pos);
204 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
205 date = AV_RB16(buf + cmd_pos);
206 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
207 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
208 cmd_pos, next_cmd_pos, date);
209 pos = cmd_pos + 2 + offset_size;
212 x1 = y1 = x2 = y2 = 0;
213 while (pos < buf_size) {
215 av_dlog(NULL, "cmd=%02x\n", cmd);
218 /* menu subpicture */
223 sub_header->start_display_time = (date << 10) / 90;
227 sub_header->end_display_time = (date << 10) / 90;
231 if ((buf_size - pos) < 2)
233 colormap[3] = buf[pos] >> 4;
234 colormap[2] = buf[pos] & 0x0f;
235 colormap[1] = buf[pos + 1] >> 4;
236 colormap[0] = buf[pos + 1] & 0x0f;
241 if ((buf_size - pos) < 2)
243 alpha[3] = buf[pos] >> 4;
244 alpha[2] = buf[pos] & 0x0f;
245 alpha[1] = buf[pos + 1] >> 4;
246 alpha[0] = buf[pos + 1] & 0x0f;
248 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
252 if ((buf_size - pos) < 6)
254 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
255 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
256 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
257 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
260 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
264 if ((buf_size - pos) < 4)
266 offset1 = AV_RB16(buf + pos);
267 offset2 = AV_RB16(buf + pos + 2);
268 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
272 if ((buf_size - pos) < 8)
274 offset1 = AV_RB32(buf + pos);
275 offset2 = AV_RB32(buf + pos + 4);
276 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
282 if ((buf_size - pos) < 768)
284 yuv_palette = buf + pos;
288 /* HD set contrast (alpha) */
289 if ((buf_size - pos) < 256)
291 for (i = 0; i < 256; i++)
292 alpha[i] = 0xFF - buf[pos+i];
299 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
308 /* decode the bitmap */
315 if (w > 0 && h > 0) {
316 if (sub_header->rects != NULL) {
317 for (i = 0; i < sub_header->num_rects; i++) {
318 av_freep(&sub_header->rects[i]->pict.data[0]);
319 av_freep(&sub_header->rects[i]->pict.data[1]);
320 av_freep(&sub_header->rects[i]);
322 av_freep(&sub_header->rects);
323 sub_header->num_rects = 0;
326 bitmap = av_malloc(w * h);
327 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
328 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
329 sub_header->num_rects = 1;
330 sub_header->rects[0]->pict.data[0] = bitmap;
331 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
332 buf, offset1, buf_size, is_8bit);
333 decode_rle(bitmap + w, w * 2, w, h / 2,
334 buf, offset2, buf_size, is_8bit);
335 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
337 if (yuv_palette == 0)
339 sub_header->rects[0]->nb_colors = 256;
340 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
342 sub_header->rects[0]->nb_colors = 4;
344 (uint32_t*)sub_header->rects[0]->pict.data[1],
345 colormap, alpha, 0xffff00);
347 sub_header->rects[0]->x = x1;
348 sub_header->rects[0]->y = y1;
349 sub_header->rects[0]->w = w;
350 sub_header->rects[0]->h = h;
351 sub_header->rects[0]->type = SUBTITLE_BITMAP;
352 sub_header->rects[0]->pict.linesize[0] = w;
355 if (next_cmd_pos == cmd_pos)
357 cmd_pos = next_cmd_pos;
359 if (sub_header->num_rects > 0)
362 if (sub_header->rects != NULL) {
363 for (i = 0; i < sub_header->num_rects; i++) {
364 av_freep(&sub_header->rects[i]->pict.data[0]);
365 av_freep(&sub_header->rects[i]->pict.data[1]);
366 av_freep(&sub_header->rects[i]);
368 av_freep(&sub_header->rects);
369 sub_header->num_rects = 0;
374 static int is_transp(const uint8_t *buf, int pitch, int n,
375 const uint8_t *transp_color)
378 for(i = 0; i < n; i++) {
379 if (!transp_color[*buf])
386 /* return 0 if empty rectangle, 1 if non empty */
387 static int find_smallest_bounding_rectangle(AVSubtitle *s)
389 uint8_t transp_color[256] = { 0 };
390 int y1, y2, x1, x2, y, w, h, i;
393 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
396 for(i = 0; i < s->rects[0]->nb_colors; i++) {
397 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
401 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
402 1, s->rects[0]->w, transp_color))
404 if (y1 == s->rects[0]->h) {
405 av_freep(&s->rects[0]->pict.data[0]);
406 s->rects[0]->w = s->rects[0]->h = 0;
410 y2 = s->rects[0]->h - 1;
411 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
412 s->rects[0]->w, transp_color))
415 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
416 s->rects[0]->h, transp_color))
418 x2 = s->rects[0]->w - 1;
419 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
424 bitmap = av_malloc(w * h);
427 for(y = 0; y < h; y++) {
428 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
430 av_freep(&s->rects[0]->pict.data[0]);
431 s->rects[0]->pict.data[0] = bitmap;
432 s->rects[0]->pict.linesize[0] = w;
435 s->rects[0]->x += x1;
436 s->rects[0]->y += y1;
444 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
445 uint32_t *rgba_palette)
450 f = fopen(filename, "w");
459 for(y = 0; y < h; y++) {
460 for(x = 0; x < w; x++) {
461 v = rgba_palette[bitmap[y * w + x]];
462 putc((v >> 16) & 0xff, f);
463 putc((v >> 8) & 0xff, f);
464 putc((v >> 0) & 0xff, f);
471 static int dvdsub_decode(AVCodecContext *avctx,
472 void *data, int *data_size,
475 DVDSubContext *ctx = avctx->priv_data;
476 const uint8_t *buf = avpkt->data;
477 int buf_size = avpkt->size;
478 AVSubtitle *sub = data;
481 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
489 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
493 av_dlog(NULL, "start=%d ms end =%d ms\n",
494 sub->start_display_time,
495 sub->end_display_time);
496 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
497 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
504 static int dvdsub_init(AVCodecContext *avctx)
506 DVDSubContext *ctx = avctx->priv_data;
509 if (!avctx->extradata || !avctx->extradata_size)
512 data = av_malloc(avctx->extradata_size + 1);
514 return AVERROR(ENOMEM);
515 memcpy(data, avctx->extradata, avctx->extradata_size);
516 data[avctx->extradata_size] = '\0';
520 if (strncmp("palette:", cur, 8) == 0) {
523 ctx->has_palette = 1;
524 for (i = 0; i < 16; i++) {
525 ctx->palette[i] = strtoul(p, &p, 16);
526 while (*p == ',' || av_isspace(*p))
529 } else if (!strncmp("size:", cur, 5)) {
531 if (sscanf(cur + 5, "%dx%d", &w, &h) == 2 &&
532 av_image_check_size(w, h, 0, avctx) >= 0)
533 avcodec_set_dimensions(avctx, w, h);
535 cur += strcspn(cur, "\n\r");
536 cur += strspn(cur, "\n\r");
542 AVCodec ff_dvdsub_decoder = {
544 .type = AVMEDIA_TYPE_SUBTITLE,
545 .id = AV_CODEC_ID_DVD_SUBTITLE,
546 .priv_data_size = sizeof(DVDSubContext),
548 .decode = dvdsub_decode,
549 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),