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 typedef struct DVDSubContext {
28 static int dvdsub_init_decoder(AVCodecContext *avctx)
33 static uint16_t getbe16(const uint8_t *p)
35 return (p[0] << 8) | p[1];
38 static int get_nibble(const uint8_t *buf, int nibble_offset)
40 return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
43 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
44 const uint8_t *buf, int nibble_offset, int buf_size)
47 int x, y, len, color, nibble_end;
50 nibble_end = buf_size * 2;
55 if (nibble_offset >= nibble_end)
57 v = get_nibble(buf, nibble_offset++);
59 v = (v << 4) | get_nibble(buf, nibble_offset++);
61 v = (v << 4) | get_nibble(buf, nibble_offset++);
63 v = (v << 4) | get_nibble(buf, nibble_offset++);
74 memset(d + x, color, len);
83 nibble_offset += (nibble_offset & 1);
89 static void guess_palette(uint32_t *rgba_palette,
92 uint32_t subtitle_color)
94 uint8_t color_used[16];
95 int nb_opaque_colors, i, level, j, r, g, b;
97 for(i = 0; i < 4; i++)
100 memset(color_used, 0, 16);
101 nb_opaque_colors = 0;
102 for(i = 0; i < 4; i++) {
103 if (alpha[i] != 0 && !color_used[palette[i]]) {
104 color_used[palette[i]] = 1;
109 if (nb_opaque_colors == 0)
112 j = nb_opaque_colors;
113 memset(color_used, 0, 16);
114 for(i = 0; i < 4; i++) {
116 if (!color_used[palette[i]]) {
117 level = (0xff * j) / nb_opaque_colors;
118 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
119 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
120 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
121 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
122 color_used[palette[i]] = (i + 1);
125 rgba_palette[i] = (rgba_palette[color_used[palette[i]] - 1] & 0x00ffffff) |
126 ((alpha[i] * 17) << 24);
132 static int decode_dvd_subtitles(AVSubtitle *sub_header,
133 const uint8_t *buf, int buf_size)
135 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
136 uint8_t palette[4], alpha[4];
143 sub_header->rects = NULL;
144 sub_header->num_rects = 0;
145 sub_header->start_display_time = 0;
146 sub_header->end_display_time = 0;
148 cmd_pos = getbe16(buf + 2);
149 while ((cmd_pos + 4) < buf_size) {
150 date = getbe16(buf + cmd_pos);
151 next_cmd_pos = getbe16(buf + cmd_pos + 2);
153 av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
154 cmd_pos, next_cmd_pos, date);
159 x1 = y1 = x2 = y2 = 0;
160 while (pos < buf_size) {
163 av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
167 /* menu subpicture */
172 sub_header->start_display_time = (date << 10) / 90;
176 sub_header->end_display_time = (date << 10) / 90;
180 if ((buf_size - pos) < 2)
182 palette[3] = buf[pos] >> 4;
183 palette[2] = buf[pos] & 0x0f;
184 palette[1] = buf[pos + 1] >> 4;
185 palette[0] = buf[pos + 1] & 0x0f;
190 if ((buf_size - pos) < 2)
192 alpha[3] = buf[pos] >> 4;
193 alpha[2] = buf[pos] & 0x0f;
194 alpha[1] = buf[pos + 1] >> 4;
195 alpha[0] = buf[pos + 1] & 0x0f;
198 av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
202 if ((buf_size - pos) < 6)
204 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
205 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
206 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
207 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
209 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
215 if ((buf_size - pos) < 4)
217 offset1 = getbe16(buf + pos);
218 offset2 = getbe16(buf + pos + 2);
220 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
234 /* decode the bitmap */
241 if (w > 0 && h > 0) {
242 if (sub_header->rects != NULL) {
243 for (i = 0; i < sub_header->num_rects; i++) {
244 av_free(sub_header->rects[i].bitmap);
245 av_free(sub_header->rects[i].rgba_palette);
247 av_freep(&sub_header->rects);
248 sub_header->num_rects = 0;
251 bitmap = av_malloc(w * h);
252 sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
253 sub_header->num_rects = 1;
254 sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
255 decode_rle(bitmap, w * 2, w, h / 2,
256 buf, offset1 * 2, buf_size);
257 decode_rle(bitmap + w, w * 2, w, h / 2,
258 buf, offset2 * 2, buf_size);
259 guess_palette(sub_header->rects[0].rgba_palette,
260 palette, alpha, 0xffff00);
261 sub_header->rects[0].x = x1;
262 sub_header->rects[0].y = y1;
263 sub_header->rects[0].w = w;
264 sub_header->rects[0].h = h;
265 sub_header->rects[0].nb_colors = 4;
266 sub_header->rects[0].linesize = w;
267 sub_header->rects[0].bitmap = bitmap;
270 if (next_cmd_pos == cmd_pos)
272 cmd_pos = next_cmd_pos;
274 if (sub_header->num_rects > 0)
280 static int is_transp(const uint8_t *buf, int pitch, int n,
281 const uint8_t *transp_color)
284 for(i = 0; i < n; i++) {
285 if (!transp_color[*buf])
292 /* return 0 if empty rectangle, 1 if non empty */
293 static int find_smallest_bounding_rectangle(AVSubtitle *s)
295 uint8_t transp_color[256];
296 int y1, y2, x1, x2, y, w, h, i;
299 if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
302 memset(transp_color, 0, 256);
303 for(i = 0; i < s->rects[0].nb_colors; i++) {
304 if ((s->rects[0].rgba_palette[i] >> 24) == 0)
308 while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
309 1, s->rects[0].w, transp_color))
311 if (y1 == s->rects[0].h) {
312 av_freep(&s->rects[0].bitmap);
313 s->rects[0].w = s->rects[0].h = 0;
317 y2 = s->rects[0].h - 1;
318 while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
319 s->rects[0].w, transp_color))
322 while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
323 s->rects[0].h, transp_color))
325 x2 = s->rects[0].w - 1;
326 while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
331 bitmap = av_malloc(w * h);
334 for(y = 0; y < h; y++) {
335 memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
337 av_freep(&s->rects[0].bitmap);
338 s->rects[0].bitmap = bitmap;
339 s->rects[0].linesize = w;
347 static int dvdsub_close_decoder(AVCodecContext *avctx)
354 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
355 uint32_t *rgba_palette)
360 f = fopen(filename, "w");
369 for(y = 0; y < h; y++) {
370 for(x = 0; x < w; x++) {
371 v = rgba_palette[bitmap[y * w + x]];
372 putc((v >> 16) & 0xff, f);
373 putc((v >> 8) & 0xff, f);
374 putc((v >> 0) & 0xff, f);
381 static int dvdsub_decode(AVCodecContext *avctx,
382 void *data, int *data_size,
383 uint8_t *buf, int buf_size)
385 AVSubtitle *sub = (void *)data;
388 is_menu = decode_dvd_subtitles(sub, buf, buf_size);
396 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
400 av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
401 sub->start_display_time,
402 sub->end_display_time);
403 ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
404 sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
411 AVCodec dvdsub_decoder = {
414 CODEC_ID_DVD_SUBTITLE,
415 sizeof(DVDSubContext),
418 dvdsub_close_decoder,
422 /* parser definition */
423 typedef struct DVDSubParseContext {
427 } DVDSubParseContext;
429 static int dvdsub_parse_init(AVCodecParserContext *s)
434 static int dvdsub_parse(AVCodecParserContext *s,
435 AVCodecContext *avctx,
436 uint8_t **poutbuf, int *poutbuf_size,
437 const uint8_t *buf, int buf_size)
439 DVDSubParseContext *pc = s->priv_data;
441 if (pc->packet_index == 0) {
444 pc->packet_len = (buf[0] << 8) | buf[1];
445 av_freep(&pc->packet);
446 pc->packet = av_malloc(pc->packet_len);
449 if (pc->packet_index + buf_size <= pc->packet_len) {
450 memcpy(pc->packet + pc->packet_index, buf, buf_size);
451 pc->packet_index += buf_size;
452 if (pc->packet_index >= pc->packet_len) {
453 *poutbuf = pc->packet;
454 *poutbuf_size = pc->packet_len;
455 pc->packet_index = 0;
460 pc->packet_index = 0;
468 static void dvdsub_parse_close(AVCodecParserContext *s)
470 DVDSubParseContext *pc = s->priv_data;
471 av_freep(&pc->packet);
474 AVCodecParser dvdsub_parser = {
475 { CODEC_ID_DVD_SUBTITLE },
476 sizeof(DVDSubParseContext),