lavfi: switch to AVFrame.
[FFMpeg-mirror/mplayer-patches.git] / libavcodec / dvdsubdec.c
blobae16f05e4c5dbaa1aaa3a200ae345b1406d694d7
1 /*
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
21 #include "avcodec.h"
22 #include "get_bits.h"
23 #include "dsputil.h"
24 #include "libavutil/colorspace.h"
25 #include "libavutil/imgutils.h"
26 #include "libavutil/avstring.h"
28 //#define DEBUG
30 typedef struct DVDSubContext {
31 uint32_t palette[16];
32 int has_palette;
33 } 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;
38 uint8_t r, g, b;
39 int i, y, cb, cr;
40 int r_add, g_add, b_add;
42 for (i = num_values; i > 0; i--) {
43 y = *ycbcr++;
44 cr = *ycbcr++;
45 cb = *ycbcr++;
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)
54 unsigned int v, t;
56 v = 0;
57 for (t = 1; v < t && t <= 0x40; t <<= 2)
58 v = (v << 4) | get_bits(gb, 4);
59 *color = v & 3;
60 if (v < 4) { /* Code for fill rest of line */
61 return INT_MAX;
63 return v >> 2;
66 static int decode_run_8bit(GetBitContext *gb, int *color)
68 int len;
69 int has_run = get_bits1(gb);
70 if (get_bits1(gb))
71 *color = get_bits(gb, 8);
72 else
73 *color = get_bits(gb, 2);
74 if (has_run) {
75 if (get_bits1(gb)) {
76 len = get_bits(gb, 7);
77 if (len == 0)
78 len = INT_MAX;
79 else
80 len += 9;
81 } else
82 len = get_bits(gb, 3) + 2;
83 } else
84 len = 1;
85 return len;
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)
91 GetBitContext gb;
92 int bit_len;
93 int x, y, len, color;
94 uint8_t *d;
96 bit_len = (buf_size - start) * 8;
97 init_get_bits(&gb, buf + start, bit_len);
99 x = 0;
100 y = 0;
101 d = bitmap;
102 for(;;) {
103 if (get_bits_count(&gb) > bit_len)
104 return -1;
105 if (is_8bit)
106 len = decode_run_8bit(&gb, &color);
107 else
108 len = decode_run_2bit(&gb, &color);
109 len = FFMIN(len, w - x);
110 memset(d + x, color, len);
111 x += len;
112 if (x >= w) {
113 y++;
114 if (y >= h)
115 break;
116 d += linesize;
117 x = 0;
118 /* byte align */
119 align_get_bits(&gb);
122 return 0;
125 static void guess_palette(DVDSubContext* ctx,
126 uint32_t *rgba_palette,
127 uint8_t *colormap,
128 uint8_t *alpha,
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);
138 return;
141 for(i = 0; i < 4; i++)
142 rgba_palette[i] = 0;
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;
148 nb_opaque_colors++;
152 if (nb_opaque_colors == 0)
153 return;
155 j = nb_opaque_colors;
156 memset(color_used, 0, 16);
157 for(i = 0; i < 4; i++) {
158 if (alpha[i] != 0) {
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);
166 j--;
167 } else {
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 };
184 int date;
185 int i;
186 int is_menu = 0;
188 if (buf_size < 10)
189 return -1;
190 memset(sub_header, 0, sizeof(*sub_header));
192 if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
193 big_offsets = 1;
194 offset_size = 4;
195 cmd_pos = 6;
196 } else {
197 big_offsets = 0;
198 offset_size = 2;
199 cmd_pos = 2;
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;
210 offset1 = -1;
211 offset2 = -1;
212 x1 = y1 = x2 = y2 = 0;
213 while (pos < buf_size) {
214 cmd = buf[pos++];
215 av_dlog(NULL, "cmd=%02x\n", cmd);
216 switch(cmd) {
217 case 0x00:
218 /* menu subpicture */
219 is_menu = 1;
220 break;
221 case 0x01:
222 /* set start date */
223 sub_header->start_display_time = (date << 10) / 90;
224 break;
225 case 0x02:
226 /* set end date */
227 sub_header->end_display_time = (date << 10) / 90;
228 break;
229 case 0x03:
230 /* set colormap */
231 if ((buf_size - pos) < 2)
232 goto fail;
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;
237 pos += 2;
238 break;
239 case 0x04:
240 /* set alpha */
241 if ((buf_size - pos) < 2)
242 goto fail;
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;
247 pos += 2;
248 av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
249 break;
250 case 0x05:
251 case 0x85:
252 if ((buf_size - pos) < 6)
253 goto fail;
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];
258 if (cmd & 0x80)
259 is_8bit = 1;
260 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
261 pos += 6;
262 break;
263 case 0x06:
264 if ((buf_size - pos) < 4)
265 goto fail;
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);
269 pos += 4;
270 break;
271 case 0x86:
272 if ((buf_size - pos) < 8)
273 goto fail;
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);
277 pos += 8;
278 break;
280 case 0x83:
281 /* HD set palette */
282 if ((buf_size - pos) < 768)
283 goto fail;
284 yuv_palette = buf + pos;
285 pos += 768;
286 break;
287 case 0x84:
288 /* HD set contrast (alpha) */
289 if ((buf_size - pos) < 256)
290 goto fail;
291 for (i = 0; i < 256; i++)
292 alpha[i] = 0xFF - buf[pos+i];
293 pos += 256;
294 break;
296 case 0xff:
297 goto the_end;
298 default:
299 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
300 goto the_end;
303 the_end:
304 if (offset1 >= 0) {
305 int w, h;
306 uint8_t *bitmap;
308 /* decode the bitmap */
309 w = x2 - x1 + 1;
310 if (w < 0)
311 w = 0;
312 h = y2 - y1;
313 if (h < 0)
314 h = 0;
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);
336 if (is_8bit) {
337 if (yuv_palette == 0)
338 goto fail;
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);
341 } else {
342 sub_header->rects[0]->nb_colors = 4;
343 guess_palette(ctx,
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)
356 break;
357 cmd_pos = next_cmd_pos;
359 if (sub_header->num_rects > 0)
360 return is_menu;
361 fail:
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;
371 return -1;
374 static int is_transp(const uint8_t *buf, int pitch, int n,
375 const uint8_t *transp_color)
377 int i;
378 for(i = 0; i < n; i++) {
379 if (!transp_color[*buf])
380 return 0;
381 buf += pitch;
383 return 1;
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;
391 uint8_t *bitmap;
393 if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
394 return 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)
398 transp_color[i] = 1;
400 y1 = 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))
403 y1++;
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;
407 return 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))
413 y2--;
414 x1 = 0;
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))
417 x1++;
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,
420 transp_color))
421 x2--;
422 w = x2 - x1 + 1;
423 h = y2 - y1 + 1;
424 bitmap = av_malloc(w * h);
425 if (!bitmap)
426 return 1;
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;
433 s->rects[0]->w = w;
434 s->rects[0]->h = h;
435 s->rects[0]->x += x1;
436 s->rects[0]->y += y1;
437 return 1;
440 #ifdef DEBUG
441 #undef fprintf
442 #undef perror
443 #undef exit
444 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
445 uint32_t *rgba_palette)
447 int x, y, v;
448 FILE *f;
450 f = fopen(filename, "w");
451 if (!f) {
452 perror(filename);
453 exit(1);
455 fprintf(f, "P6\n"
456 "%d %d\n"
457 "%d\n",
458 w, h, 255);
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);
467 fclose(f);
469 #endif
471 static int dvdsub_decode(AVCodecContext *avctx,
472 void *data, int *data_size,
473 AVPacket *avpkt)
475 DVDSubContext *ctx = avctx->priv_data;
476 const uint8_t *buf = avpkt->data;
477 int buf_size = avpkt->size;
478 AVSubtitle *sub = data;
479 int is_menu;
481 is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
483 if (is_menu < 0) {
484 no_subtitle:
485 *data_size = 0;
487 return buf_size;
489 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
490 goto no_subtitle;
492 #if defined(DEBUG)
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]);
498 #endif
500 *data_size = 1;
501 return buf_size;
504 static int dvdsub_init(AVCodecContext *avctx)
506 DVDSubContext *ctx = avctx->priv_data;
507 char *data, *cur;
509 if (!avctx->extradata || !avctx->extradata_size)
510 return 0;
512 data = av_malloc(avctx->extradata_size + 1);
513 if (!data)
514 return AVERROR(ENOMEM);
515 memcpy(data, avctx->extradata, avctx->extradata_size);
516 data[avctx->extradata_size] = '\0';
517 cur = data;
519 while (*cur) {
520 if (strncmp("palette:", cur, 8) == 0) {
521 int i;
522 char *p = cur + 8;
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))
527 p++;
529 } else if (!strncmp("size:", cur, 5)) {
530 int w, h;
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");
538 av_free(data);
539 return 0;
542 AVCodec ff_dvdsub_decoder = {
543 .name = "dvdsub",
544 .type = AVMEDIA_TYPE_SUBTITLE,
545 .id = AV_CODEC_ID_DVD_SUBTITLE,
546 .priv_data_size = sizeof(DVDSubContext),
547 .init = dvdsub_init,
548 .decode = dvdsub_decode,
549 .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),