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/attributes.h"
25 #include "libavutil/colorspace.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avstring.h"
29 typedef struct DVDSubContext {
34 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
36 const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
39 int r_add, g_add, b_add;
41 for (i = num_values; i > 0; i--) {
45 YUV_TO_RGB1_CCIR(cb, cr);
46 YUV_TO_RGB2_CCIR(r, g, b, y);
47 *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
51 static int decode_run_2bit(GetBitContext *gb, int *color)
56 for (t = 1; v < t && t <= 0x40; t <<= 2)
57 v = (v << 4) | get_bits(gb, 4);
59 if (v < 4) { /* Code for fill rest of line */
65 static int decode_run_8bit(GetBitContext *gb, int *color)
68 int has_run = get_bits1(gb);
70 *color = get_bits(gb, 8);
72 *color = get_bits(gb, 2);
75 len = get_bits(gb, 7);
81 len = get_bits(gb, 3) + 2;
87 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
88 const uint8_t *buf, int start, int buf_size, int is_8bit)
95 bit_len = (buf_size - start) * 8;
96 init_get_bits(&gb, buf + start, bit_len);
102 if (get_bits_count(&gb) > bit_len)
105 len = decode_run_8bit(&gb, &color);
107 len = decode_run_2bit(&gb, &color);
108 len = FFMIN(len, w - x);
109 memset(d + x, color, len);
124 static void guess_palette(DVDSubContext* ctx,
125 uint32_t *rgba_palette,
128 uint32_t subtitle_color)
130 uint8_t color_used[16] = { 0 };
131 int nb_opaque_colors, i, level, j, r, g, b;
133 if (ctx->has_palette) {
134 for (i = 0; i < 4; i++)
135 rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
136 | ((alpha[i] * 17) << 24);
140 for(i = 0; i < 4; i++)
143 nb_opaque_colors = 0;
144 for(i = 0; i < 4; i++) {
145 if (alpha[i] != 0 && !color_used[colormap[i]]) {
146 color_used[colormap[i]] = 1;
151 if (nb_opaque_colors == 0)
154 j = nb_opaque_colors;
155 memset(color_used, 0, 16);
156 for(i = 0; i < 4; i++) {
158 if (!color_used[colormap[i]]) {
159 level = (0xff * j) / nb_opaque_colors;
160 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
161 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
162 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
163 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
164 color_used[colormap[i]] = (i + 1);
167 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
168 ((alpha[i] * 17) << 24);
174 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
176 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
177 const uint8_t *buf, int buf_size)
179 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
180 int big_offsets, offset_size, is_8bit = 0;
181 const uint8_t *yuv_palette = 0;
182 uint8_t colormap[4] = { 0 }, alpha[256] = { 0 };
189 memset(sub_header, 0, sizeof(*sub_header));
191 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
201 cmd_pos = READ_OFFSET(buf + cmd_pos);
203 while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
204 date = AV_RB16(buf + cmd_pos);
205 next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
206 av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
207 cmd_pos, next_cmd_pos, date);
208 pos = cmd_pos + 2 + offset_size;
211 x1 = y1 = x2 = y2 = 0;
212 while (pos < buf_size) {
214 av_dlog(NULL, "cmd=%02x\n", cmd);
217 /* menu subpicture */
222 sub_header->start_display_time = (date << 10) / 90;
226 sub_header->end_display_time = (date << 10) / 90;
230 if ((buf_size - pos) < 2)
232 colormap[3] = buf[pos] >> 4;
233 colormap[2] = buf[pos] & 0x0f;
234 colormap[1] = buf[pos + 1] >> 4;
235 colormap[0] = buf[pos + 1] & 0x0f;
240 if ((buf_size - pos) < 2)
242 alpha[3] = buf[pos] >> 4;
243 alpha[2] = buf[pos] & 0x0f;
244 alpha[1] = buf[pos + 1] >> 4;
245 alpha[0] = buf[pos + 1] & 0x0f;
247 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
251 if ((buf_size - pos) < 6)
253 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
254 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
255 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
256 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
259 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
263 if ((buf_size - pos) < 4)
265 offset1 = AV_RB16(buf + pos);
266 offset2 = AV_RB16(buf + pos + 2);
267 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
271 if ((buf_size - pos) < 8)
273 offset1 = AV_RB32(buf + pos);
274 offset2 = AV_RB32(buf + pos + 4);
275 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
281 if ((buf_size - pos) < 768)
283 yuv_palette = buf + pos;
287 /* HD set contrast (alpha) */
288 if ((buf_size - pos) < 256)
290 for (i = 0; i < 256; i++)
291 alpha[i] = 0xFF - buf[pos+i];
298 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
307 /* decode the bitmap */
314 if (w > 0 && h > 0) {
315 if (sub_header->rects != NULL) {
316 for (i = 0; i < sub_header->num_rects; i++) {
317 av_freep(&sub_header->rects[i]->pict.data[0]);
318 av_freep(&sub_header->rects[i]->pict.data[1]);
319 av_freep(&sub_header->rects[i]);
321 av_freep(&sub_header->rects);
322 sub_header->num_rects = 0;
325 bitmap = av_malloc(w * h);
326 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
327 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
328 sub_header->num_rects = 1;
329 sub_header->rects[0]->pict.data[0] = bitmap;
330 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
331 buf, offset1, buf_size, is_8bit);
332 decode_rle(bitmap + w, w * 2, w, h / 2,
333 buf, offset2, buf_size, is_8bit);
334 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
336 if (yuv_palette == 0)
338 sub_header->rects[0]->nb_colors = 256;
339 yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
341 sub_header->rects[0]->nb_colors = 4;
343 (uint32_t*)sub_header->rects[0]->pict.data[1],
344 colormap, alpha, 0xffff00);
346 sub_header->rects[0]->x = x1;
347 sub_header->rects[0]->y = y1;
348 sub_header->rects[0]->w = w;
349 sub_header->rects[0]->h = h;
350 sub_header->rects[0]->type = SUBTITLE_BITMAP;
351 sub_header->rects[0]->pict.linesize[0] = w;
354 if (next_cmd_pos == cmd_pos)
356 cmd_pos = next_cmd_pos;
358 if (sub_header->num_rects > 0)
361 if (sub_header->rects != NULL) {
362 for (i = 0; i < sub_header->num_rects; i++) {
363 av_freep(&sub_header->rects[i]->pict.data[0]);
364 av_freep(&sub_header->rects[i]->pict.data[1]);
365 av_freep(&sub_header->rects[i]);
367 av_freep(&sub_header->rects);
368 sub_header->num_rects = 0;
373 static int is_transp(const uint8_t *buf, int pitch, int n,
374 const uint8_t *transp_color)
377 for(i = 0; i < n; i++) {
378 if (!transp_color[*buf])
385 /* return 0 if empty rectangle, 1 if non empty */
386 static int find_smallest_bounding_rectangle(AVSubtitle *s)
388 uint8_t transp_color[256] = { 0 };
389 int y1, y2, x1, x2, y, w, h, i;
392 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
395 for(i = 0; i < s->rects[0]->nb_colors; i++) {
396 if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
400 while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
401 1, s->rects[0]->w, transp_color))
403 if (y1 == s->rects[0]->h) {
404 av_freep(&s->rects[0]->pict.data[0]);
405 s->rects[0]->w = s->rects[0]->h = 0;
409 y2 = s->rects[0]->h - 1;
410 while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
411 s->rects[0]->w, transp_color))
414 while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
415 s->rects[0]->h, transp_color))
417 x2 = s->rects[0]->w - 1;
418 while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
423 bitmap = av_malloc(w * h);
426 for(y = 0; y < h; y++) {
427 memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
429 av_freep(&s->rects[0]->pict.data[0]);
430 s->rects[0]->pict.data[0] = bitmap;
431 s->rects[0]->pict.linesize[0] = w;
434 s->rects[0]->x += x1;
435 s->rects[0]->y += y1;
443 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
444 uint32_t *rgba_palette)
449 f = fopen(filename, "w");
458 for(y = 0; y < h; y++) {
459 for(x = 0; x < w; x++) {
460 v = rgba_palette[bitmap[y * w + x]];
461 putc((v >> 16) & 0xff, f);
462 putc((v >> 8) & 0xff, f);
463 putc((v >> 0) & 0xff, f);
470 static int dvdsub_decode(AVCodecContext *avctx,
471 void *data, int *data_size,
474 DVDSubContext *ctx = avctx->priv_data;
475 const uint8_t *buf = avpkt->data;
476 int buf_size = avpkt->size;
477 AVSubtitle *sub = data;
480 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
488 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
492 av_dlog(NULL, "start=%d ms end =%d ms\n",
493 sub->start_display_time,
494 sub->end_display_time);
495 ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
496 sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
503 static av_cold int dvdsub_init(AVCodecContext *avctx)
505 DVDSubContext *ctx = avctx->priv_data;
508 if (!avctx->extradata || !avctx->extradata_size)
511 data = av_malloc(avctx->extradata_size + 1);
513 return AVERROR(ENOMEM);
514 memcpy(data, avctx->extradata, avctx->extradata_size);
515 data[avctx->extradata_size] = '\0';
519 if (strncmp("palette:", cur, 8) == 0) {
522 ctx->has_palette = 1;
523 for (i = 0; i < 16; i++) {
524 ctx->palette[i] = strtoul(p, &p, 16);
525 while (*p == ',' || av_isspace(*p))
528 } else if (!strncmp("size:", cur, 5)) {
530 if (sscanf(cur + 5, "%dx%d", &w, &h) == 2 &&
531 av_image_check_size(w, h, 0, avctx) >= 0)
532 avcodec_set_dimensions(avctx, w, h);
534 cur += strcspn(cur, "\n\r");
535 cur += strspn(cur, "\n\r");
541 AVCodec ff_dvdsub_decoder = {
543 .type = AVMEDIA_TYPE_SUBTITLE,
544 .id = AV_CODEC_ID_DVD_SUBTITLE,
545 .priv_data_size = sizeof(DVDSubContext),
547 .decode = dvdsub_decode,
548 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),