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 uint16_t getbe16(const uint8_t *p)
32 return (p[0] << 8) | p[1];
35 static int get_nibble(const uint8_t *buf, int nibble_offset)
37 return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
40 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
41 const uint8_t *buf, int nibble_offset, int buf_size)
44 int x, y, len, color, nibble_end;
47 nibble_end = buf_size * 2;
52 if (nibble_offset >= nibble_end)
54 v = get_nibble(buf, nibble_offset++);
56 v = (v << 4) | get_nibble(buf, nibble_offset++);
58 v = (v << 4) | get_nibble(buf, nibble_offset++);
60 v = (v << 4) | get_nibble(buf, nibble_offset++);
71 memset(d + x, color, len);
80 nibble_offset += (nibble_offset & 1);
86 static void guess_palette(uint32_t *rgba_palette,
89 uint32_t subtitle_color)
91 uint8_t color_used[16];
92 int nb_opaque_colors, i, level, j, r, g, b;
94 for(i = 0; i < 4; i++)
97 memset(color_used, 0, 16);
99 for(i = 0; i < 4; i++) {
100 if (alpha[i] != 0 && !color_used[palette[i]]) {
101 color_used[palette[i]] = 1;
106 if (nb_opaque_colors == 0)
109 j = nb_opaque_colors;
110 memset(color_used, 0, 16);
111 for(i = 0; i < 4; i++) {
113 if (!color_used[palette[i]]) {
114 level = (0xff * j) / nb_opaque_colors;
115 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
116 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
117 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
118 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
119 color_used[palette[i]] = (i + 1);
122 rgba_palette[i] = (rgba_palette[color_used[palette[i]] - 1] & 0x00ffffff) |
123 ((alpha[i] * 17) << 24);
129 static int decode_dvd_subtitles(AVSubtitle *sub_header,
130 const uint8_t *buf, int buf_size)
132 int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
133 uint8_t palette[4], alpha[4];
140 sub_header->rects = NULL;
141 sub_header->num_rects = 0;
142 sub_header->start_display_time = 0;
143 sub_header->end_display_time = 0;
145 cmd_pos = getbe16(buf + 2);
146 while ((cmd_pos + 4) < buf_size) {
147 date = getbe16(buf + cmd_pos);
148 next_cmd_pos = getbe16(buf + cmd_pos + 2);
150 av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
151 cmd_pos, next_cmd_pos, date);
156 x1 = y1 = x2 = y2 = 0;
157 while (pos < buf_size) {
160 av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
164 /* menu subpicture */
169 sub_header->start_display_time = (date << 10) / 90;
173 sub_header->end_display_time = (date << 10) / 90;
177 if ((buf_size - pos) < 2)
179 palette[3] = buf[pos] >> 4;
180 palette[2] = buf[pos] & 0x0f;
181 palette[1] = buf[pos + 1] >> 4;
182 palette[0] = buf[pos + 1] & 0x0f;
187 if ((buf_size - pos) < 2)
189 alpha[3] = buf[pos] >> 4;
190 alpha[2] = buf[pos] & 0x0f;
191 alpha[1] = buf[pos + 1] >> 4;
192 alpha[0] = buf[pos + 1] & 0x0f;
195 av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
199 if ((buf_size - pos) < 6)
201 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
202 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
203 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
204 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
206 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
212 if ((buf_size - pos) < 4)
214 offset1 = getbe16(buf + pos);
215 offset2 = getbe16(buf + pos + 2);
217 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
231 /* decode the bitmap */
238 if (w > 0 && h > 0) {
239 if (sub_header->rects != NULL) {
240 for (i = 0; i < sub_header->num_rects; i++) {
241 av_free(sub_header->rects[i].bitmap);
242 av_free(sub_header->rects[i].rgba_palette);
244 av_freep(&sub_header->rects);
245 sub_header->num_rects = 0;
248 bitmap = av_malloc(w * h);
249 sub_header->rects = av_mallocz(sizeof(AVSubtitleRect));
250 sub_header->num_rects = 1;
251 sub_header->rects[0].rgba_palette = av_malloc(4 * 4);
252 decode_rle(bitmap, w * 2, w, h / 2,
253 buf, offset1 * 2, buf_size);
254 decode_rle(bitmap + w, w * 2, w, h / 2,
255 buf, offset2 * 2, buf_size);
256 guess_palette(sub_header->rects[0].rgba_palette,
257 palette, alpha, 0xffff00);
258 sub_header->rects[0].x = x1;
259 sub_header->rects[0].y = y1;
260 sub_header->rects[0].w = w;
261 sub_header->rects[0].h = h;
262 sub_header->rects[0].nb_colors = 4;
263 sub_header->rects[0].linesize = w;
264 sub_header->rects[0].bitmap = bitmap;
267 if (next_cmd_pos == cmd_pos)
269 cmd_pos = next_cmd_pos;
271 if (sub_header->num_rects > 0)
277 static int is_transp(const uint8_t *buf, int pitch, int n,
278 const uint8_t *transp_color)
281 for(i = 0; i < n; i++) {
282 if (!transp_color[*buf])
289 /* return 0 if empty rectangle, 1 if non empty */
290 static int find_smallest_bounding_rectangle(AVSubtitle *s)
292 uint8_t transp_color[256];
293 int y1, y2, x1, x2, y, w, h, i;
296 if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
299 memset(transp_color, 0, 256);
300 for(i = 0; i < s->rects[0].nb_colors; i++) {
301 if ((s->rects[0].rgba_palette[i] >> 24) == 0)
305 while (y1 < s->rects[0].h && is_transp(s->rects[0].bitmap + y1 * s->rects[0].linesize,
306 1, s->rects[0].w, transp_color))
308 if (y1 == s->rects[0].h) {
309 av_freep(&s->rects[0].bitmap);
310 s->rects[0].w = s->rects[0].h = 0;
314 y2 = s->rects[0].h - 1;
315 while (y2 > 0 && is_transp(s->rects[0].bitmap + y2 * s->rects[0].linesize, 1,
316 s->rects[0].w, transp_color))
319 while (x1 < (s->rects[0].w - 1) && is_transp(s->rects[0].bitmap + x1, s->rects[0].linesize,
320 s->rects[0].h, transp_color))
322 x2 = s->rects[0].w - 1;
323 while (x2 > 0 && is_transp(s->rects[0].bitmap + x2, s->rects[0].linesize, s->rects[0].h,
328 bitmap = av_malloc(w * h);
331 for(y = 0; y < h; y++) {
332 memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
334 av_freep(&s->rects[0].bitmap);
335 s->rects[0].bitmap = bitmap;
336 s->rects[0].linesize = w;
344 static int dvdsub_close_decoder(AVCodecContext *avctx)
351 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
352 uint32_t *rgba_palette)
357 f = fopen(filename, "w");
366 for(y = 0; y < h; y++) {
367 for(x = 0; x < w; x++) {
368 v = rgba_palette[bitmap[y * w + x]];
369 putc((v >> 16) & 0xff, f);
370 putc((v >> 8) & 0xff, f);
371 putc((v >> 0) & 0xff, f);
378 static int dvdsub_decode(AVCodecContext *avctx,
379 void *data, int *data_size,
380 uint8_t *buf, int buf_size)
382 AVSubtitle *sub = (void *)data;
385 is_menu = decode_dvd_subtitles(sub, buf, buf_size);
393 if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
397 av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n",
398 sub->start_display_time,
399 sub->end_display_time);
400 ppm_save("/tmp/a.ppm", sub->rects[0].bitmap,
401 sub->rects[0].w, sub->rects[0].h, sub->rects[0].rgba_palette);
408 AVCodec dvdsub_decoder = {
411 CODEC_ID_DVD_SUBTITLE,
415 dvdsub_close_decoder,
419 /* parser definition */
420 typedef struct DVDSubParseContext {
424 } DVDSubParseContext;
426 static int dvdsub_parse_init(AVCodecParserContext *s)
431 static int dvdsub_parse(AVCodecParserContext *s,
432 AVCodecContext *avctx,
433 uint8_t **poutbuf, int *poutbuf_size,
434 const uint8_t *buf, int buf_size)
436 DVDSubParseContext *pc = s->priv_data;
438 if (pc->packet_index == 0) {
441 pc->packet_len = (buf[0] << 8) | buf[1];
442 av_freep(&pc->packet);
443 pc->packet = av_malloc(pc->packet_len);
446 if (pc->packet_index + buf_size <= pc->packet_len) {
447 memcpy(pc->packet + pc->packet_index, buf, buf_size);
448 pc->packet_index += buf_size;
449 if (pc->packet_index >= pc->packet_len) {
450 *poutbuf = pc->packet;
451 *poutbuf_size = pc->packet_len;
452 pc->packet_index = 0;
457 pc->packet_index = 0;
465 static void dvdsub_parse_close(AVCodecParserContext *s)
467 DVDSubParseContext *pc = s->priv_data;
468 av_freep(&pc->packet);
471 AVCodecParser dvdsub_parser = {
472 { CODEC_ID_DVD_SUBTITLE },
473 sizeof(DVDSubParseContext),