2 * DVD subtitle decoding for ffmpeg
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
25 static int dvdsub_init_decoder(AVCodecContext *avctx)
30 static int get_nibble(const uint8_t *buf, int nibble_offset)
32 return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
35 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
36 const uint8_t *buf, int nibble_offset, int buf_size)
39 int x, y, len, color, nibble_end;
42 nibble_end = buf_size * 2;
47 if (nibble_offset >= nibble_end)
49 v = get_nibble(buf, nibble_offset++);
51 v = (v << 4) | get_nibble(buf, nibble_offset++);
53 v = (v << 4) | get_nibble(buf, nibble_offset++);
55 v = (v << 4) | get_nibble(buf, nibble_offset++);
66 memset(d + x, color, len);
75 nibble_offset += (nibble_offset & 1);
81 static void guess_palette(uint32_t *rgba_palette,
84 uint32_t subtitle_color)
86 uint8_t color_used[16];
87 int nb_opaque_colors, i, level, j, r, g, b;
89 for(i = 0; i < 4; i++)
92 memset(color_used, 0, 16);
94 for(i = 0; i < 4; i++) {
95 if (alpha[i] != 0 && !color_used[palette[i]]) {
96 color_used[palette[i]] = 1;
101 if (nb_opaque_colors == 0)
104 j = nb_opaque_colors;
105 memset(color_used, 0, 16);
106 for(i = 0; i < 4; i++) {
108 if (!color_used[palette[i]]) {
109 level = (0xff * j) / nb_opaque_colors;
110 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
111 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
112 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
113 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
114 color_used[palette[i]] = (i + 1);
117 rgba_palette[i] = (rgba_palette[color_used[palette[i]] - 1] & 0x00ffffff) |
118 ((alpha[i] * 17) << 24);
124 static int decode_dvd_subtitles(AVSubtitle *sub_header,
125 const uint8_t *buf, int buf_size)
127 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
128 uint8_t palette[4], alpha[4];
135 sub_header->rects = NULL;
136 sub_header->num_rects = 0;
137 sub_header->start_display_time = 0;
138 sub_header->end_display_time = 0;
140 cmd_pos = AV_RB16(buf + 2);
141 while ((cmd_pos + 4) < buf_size) {
142 date = AV_RB16(buf + cmd_pos);
143 next_cmd_pos = AV_RB16(buf + cmd_pos + 2);
145 av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
146 cmd_pos, next_cmd_pos, date);
151 x1 = y1 = x2 = y2 = 0;
152 while (pos < buf_size) {
155 av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
159 /* menu subpicture */
164 sub_header->start_display_time = (date << 10) / 90;
168 sub_header->end_display_time = (date << 10) / 90;
172 if ((buf_size - pos) < 2)
174 palette[3] = buf[pos] >> 4;
175 palette[2] = buf[pos] & 0x0f;
176 palette[1] = buf[pos + 1] >> 4;
177 palette[0] = buf[pos + 1] & 0x0f;
182 if ((buf_size - pos) < 2)
184 alpha[3] = buf[pos] >> 4;
185 alpha[2] = buf[pos] & 0x0f;
186 alpha[1] = buf[pos + 1] >> 4;
187 alpha[0] = buf[pos + 1] & 0x0f;
190 av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
194 if ((buf_size - pos) < 6)
196 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
197 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
198 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
199 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
201 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
207 if ((buf_size - pos) < 4)
209 offset1 = AV_RB16(buf + pos);
210 offset2 = AV_RB16(buf + pos + 2);
212 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
226 /* decode the bitmap */
233 if (w > 0 && h > 0) {
234 if (sub_header->rects != NULL) {
235 for (i = 0; i < sub_header->num_rects; i++) {
236 av_free(sub_header->rects[i].bitmap);
237 av_free(sub_header->rects[i].rgba_palette);
239 av_freep(&sub_header->rects);
240 sub_header->num_rects = 0;
243 bitmap = av_malloc(w * h);
244 sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
245 sub_header->num_rects = 1;
246 sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
247 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
248 buf, offset1 * 2, buf_size);
249 decode_rle(bitmap + w, w * 2, w, h / 2,
250 buf, offset2 * 2, buf_size);
251 guess_palette(sub_header->rects[0].rgba_palette,
252 palette, alpha, 0xffff00);
253 sub_header->rects[0].x = x1;
254 sub_header->rects[0].y = y1;
255 sub_header->rects[0].w = w;
256 sub_header->rects[0].h = h;
257 sub_header->rects[0].nb_colors = 4;
258 sub_header->rects[0].linesize = w;
259 sub_header->rects[0].bitmap = bitmap;
262 if (next_cmd_pos == cmd_pos)
264 cmd_pos = next_cmd_pos;
266 if (sub_header->num_rects > 0)
272 static int is_transp(const uint8_t *buf, int pitch, int n,
273 const uint8_t *transp_color)
276 for(i = 0; i < n; i++) {
277 if (!transp_color[*buf])
284 /* return 0 if empty rectangle, 1 if non empty */
285 static int find_smallest_bounding_rectangle(AVSubtitle *s)
287 uint8_t transp_color[256];
288 int y1, y2, x1, x2, y, w, h, i;
291 if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
294 memset(transp_color, 0, 256);
295 for(i = 0; i < s->rects[0].nb_colors; i++) {
296 if ((s->rects[0].rgba_palette[i] >> 24) == 0)
300 while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
301 1, s->rects[0].w, transp_color))
303 if (y1 == s->rects[0].h) {
304 av_freep(&s->rects[0].bitmap);
305 s->rects[0].w = s->rects[0].h = 0;
309 y2 = s->rects[0].h - 1;
310 while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
311 s->rects[0].w, transp_color))
314 while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
315 s->rects[0].h, transp_color))
317 x2 = s->rects[0].w - 1;
318 while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
323 bitmap = av_malloc(w * h);
326 for(y = 0; y < h; y++) {
327 memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
329 av_freep(&s->rects[0].bitmap);
330 s->rects[0].bitmap = bitmap;
331 s->rects[0].linesize = w;
339 static int dvdsub_close_decoder(AVCodecContext *avctx)
346 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
347 uint32_t *rgba_palette)
352 f = fopen(filename, "w");
361 for(y = 0; y < h; y++) {
362 for(x = 0; x < w; x++) {
363 v = rgba_palette[bitmap[y * w + x]];
364 putc((v >> 16) & 0xff, f);
365 putc((v >> 8) & 0xff, f);
366 putc((v >> 0) & 0xff, f);
373 static int dvdsub_decode(AVCodecContext *avctx,
374 void *data, int *data_size,
375 uint8_t *buf, int buf_size)
377 AVSubtitle *sub = (void *)data;
380 is_menu = decode_dvd_subtitles(sub, buf, buf_size);
388 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
392 av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
393 sub->start_display_time,
394 sub->end_display_time);
395 ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
396 sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
403 AVCodec dvdsub_decoder = {
406 CODEC_ID_DVD_SUBTITLE,
410 dvdsub_close_decoder,
414 /* parser definition */
415 typedef struct DVDSubParseContext {
419 } DVDSubParseContext;
421 static int dvdsub_parse_init(AVCodecParserContext *s)
426 static int dvdsub_parse(AVCodecParserContext *s,
427 AVCodecContext *avctx,
428 uint8_t **poutbuf, int *poutbuf_size,
429 const uint8_t *buf, int buf_size)
431 DVDSubParseContext *pc = s->priv_data;
433 if (pc->packet_index == 0) {
436 pc->packet_len = AV_RB16(buf);
437 av_freep(&pc->packet);
438 pc->packet = av_malloc(pc->packet_len);
441 if (pc->packet_index + buf_size <= pc->packet_len) {
442 memcpy(pc->packet + pc->packet_index, buf, buf_size);
443 pc->packet_index += buf_size;
444 if (pc->packet_index >= pc->packet_len) {
445 *poutbuf = pc->packet;
446 *poutbuf_size = pc->packet_len;
447 pc->packet_index = 0;
452 pc->packet_index = 0;
460 static void dvdsub_parse_close(AVCodecParserContext *s)
462 DVDSubParseContext *pc = s->priv_data;
463 av_freep(&pc->packet);
466 AVCodecParser dvdsub_parser = {
467 { CODEC_ID_DVD_SUBTITLE },
468 sizeof(DVDSubParseContext),