]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdsub.c
subtitle codec type support
[ffmpeg] / libavcodec / dvdsub.c
1 /*
2  * DVD subtitle decoding for ffmpeg
3  * Copyright (c) 2005 Fabrice Bellard.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19 #include "avcodec.h"
20
21 //#define DEBUG
22
23 typedef struct DVDSubContext {
24 } DVDSubContext;
25
26 static int dvdsub_init_decoder(AVCodecContext *avctx)
27 {
28     return 0;
29 }
30
31 uint16_t getbe16(const uint8_t *p)
32 {
33     return (p[0] << 8) | p[1];
34 }
35
36 int get_nibble(const uint8_t *buf, int nibble_offset)
37 {
38     return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
39 }
40
41 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, 
42                       const uint8_t *buf, int nibble_offset, int buf_size)
43 {
44     unsigned int v;
45     int x, y, len, color, nibble_end;
46     uint8_t *d;
47
48     nibble_end = buf_size * 2;
49     x = 0;
50     y = 0;
51     d = bitmap;
52     for(;;) {
53         if (nibble_offset >= nibble_end)
54             return -1;
55         v = get_nibble(buf, nibble_offset++);
56         if (v < 0x4) {
57             v = (v << 4) | get_nibble(buf, nibble_offset++);
58             if (v < 0x10) {
59                 v = (v << 4) | get_nibble(buf, nibble_offset++);
60                 if (v < 0x040) {
61                     v = (v << 4) | get_nibble(buf, nibble_offset++);
62                     if (v < 4) {
63                         v |= (w - x) << 2;
64                     }
65                 }
66             }
67         }
68         len = v >> 2;
69         if (len > (w - x))
70             len = (w - x);
71         color = v & 0x03;
72         memset(d + x, color, len);
73         x += len;
74         if (x >= w) {
75             y++;
76             if (y >= h)
77                 break;
78             d += linesize;
79             x = 0;
80             /* byte align */
81             nibble_offset += (nibble_offset & 1);
82         }
83     }
84     return 0;
85 }
86
87 static void guess_palette(uint32_t *rgba_palette,
88                           uint8_t *palette,
89                           uint8_t *alpha,
90                           uint32_t subtitle_color)
91 {
92     uint8_t color_used[16];
93     int nb_opaque_colors, i, level, j, r, g, b;
94     
95     for(i = 0; i < 4; i++)
96         rgba_palette[i] = 0;
97
98     memset(color_used, 0, 16);
99     nb_opaque_colors = 0;
100     for(i = 0; i < 4; i++) {
101         if (alpha[i] != 0 && !color_used[palette[i]]) {
102             color_used[palette[i]] = 1;
103             nb_opaque_colors++;
104         }
105     }
106     
107     if (nb_opaque_colors == 0)
108         return;
109     
110     j = 0;
111     memset(color_used, 0, 16);
112     for(i = 0; i < 4; i++) {
113         if (alpha[i] != 0) {
114             if (!color_used[palette[i]])  {
115                 level = (0xff * (j + 1)) / nb_opaque_colors;
116                 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
117                 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
118                 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
119                 rgba_palette[i] = b | (g << 8) | (r << 16) | (0xff << 24);
120                 color_used[palette[i]] = (i + 1);
121                 j++;
122             } else {
123                 rgba_palette[i] = rgba_palette[color_used[palette[i]] - 1];
124             }
125         }
126     }
127 }
128
129 static int decode_dvd_subtitles(AVSubtitle *sub_header, 
130                                 const uint8_t *buf, int buf_size)
131 {
132     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
133     uint8_t palette[4], alpha[4];
134     int date;
135     
136     if (buf_size < 4)
137         return -1;
138     sub_header->bitmap = NULL;
139     sub_header->rgba_palette = NULL;
140     sub_header->start_display_time = 0;
141     sub_header->end_display_time = 0;
142
143     cmd_pos = getbe16(buf + 2);
144     while ((cmd_pos + 4) < buf_size) {
145         date = getbe16(buf + cmd_pos);
146         next_cmd_pos = getbe16(buf + cmd_pos + 2);
147 #ifdef DEBUG
148         av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n", 
149                cmd_pos, next_cmd_pos, date);
150 #endif
151         pos = cmd_pos + 4;
152         offset1 = -1;
153         offset2 = -1;
154         x1 = y1 = x2 = y2 = 0;
155         while (pos < buf_size) {
156             cmd = buf[pos++];
157 #ifdef DEBUG
158             av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
159 #endif
160             switch(cmd) {
161             case 0x00:
162                 /* force display */
163                 break;
164             case 0x01:
165                 /* set start date */
166                 sub_header->start_display_time = date * 10;
167                 break;
168             case 0x02:
169                 /* set end date */
170                 sub_header->end_display_time = date * 10;
171                 break;
172             case 0x03:
173                 /* set palette */
174                 if ((buf_size - pos) < 2)
175                     goto fail;
176                 palette[0] = buf[pos] >> 4;
177                 palette[1] = buf[pos] & 0x0f;
178                 palette[2] = buf[pos + 1] >> 4;
179                 palette[3] = buf[pos + 1] & 0x0f;
180                 pos += 2;
181                 break;
182             case 0x04:
183                 /* set alpha */
184                 if ((buf_size - pos) < 2)
185                     goto fail;
186                 alpha[0] = buf[pos] >> 4;
187                 alpha[1] = buf[pos] & 0x0f;
188                 alpha[2] = buf[pos + 1] >> 4;
189                 alpha[3] = buf[pos + 1] & 0x0f;
190                 pos += 2;
191                 break;
192             case 0x05:
193                 if ((buf_size - pos) < 6)
194                     goto fail;
195                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
196                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
197                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
198                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
199 #ifdef DEBUG
200                 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
201                        x1, x2, y1, y2);
202 #endif
203                 pos += 6;
204                 break;
205             case 0x06:
206                 if ((buf_size - pos) < 4)
207                     goto fail;
208                 offset1 = getbe16(buf + pos);
209                 offset2 = getbe16(buf + pos + 2);
210 #ifdef DEBUG
211                 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
212 #endif
213                 pos += 4;
214                 break;
215             case 0xff:
216             default:
217                 goto the_end;
218             }
219         }
220     the_end:
221         if (offset1 >= 0) {
222             int w, h;
223             uint8_t *bitmap;
224             
225             /* decode the bitmap */
226             w = x2 - x1 + 1;
227             if (w < 0)
228                 w = 0;
229             h = y2 - y1;
230             if (h < 0)
231                 h = 0;
232             if (w > 0 && h > 0) {
233                 av_freep(&sub_header->bitmap);
234                 av_freep(&sub_header->rgba_palette);
235                 bitmap = av_malloc(w * h);
236                 sub_header->rgba_palette = av_malloc(4 * 4);
237                 decode_rle(bitmap, w * 2, w, h / 2,
238                            buf, offset1 * 2, buf_size);
239                 decode_rle(bitmap + w, w * 2, w, h / 2,
240                            buf, offset2 * 2, buf_size);
241                 guess_palette(sub_header->rgba_palette, 
242                               palette, alpha, 0xffff00);
243                 sub_header->x = x1;
244                 sub_header->y = y1;
245                 sub_header->w = w;
246                 sub_header->h = h;
247                 sub_header->nb_colors = 4;
248                 sub_header->linesize = w;
249                 sub_header->bitmap = bitmap;
250             }
251         }
252         if (next_cmd_pos == cmd_pos)
253             break;
254         cmd_pos = next_cmd_pos;
255     }
256     if (sub_header->bitmap)
257         return 0;
258  fail:
259     return -1;
260 }
261
262 static int is_transp(const uint8_t *buf, int pitch, int n, 
263                      const uint8_t *transp_color)
264 {
265     int i;
266     for(i = 0; i < n; i++) {
267         if (!transp_color[*buf])
268             return 0;
269         buf += pitch;
270     }
271     return 1;
272 }
273
274 /* return 0 if empty rectangle, 1 if non empty */
275 static int find_smallest_bouding_rectangle(AVSubtitle *s)
276 {
277     uint8_t transp_color[256];
278     int y1, y2, x1, x2, y, w, h, i;
279     uint8_t *bitmap;
280
281     if (s->w <= 0 || s->h <= 0)
282         return 0;
283
284     memset(transp_color, 0, 256);
285     for(i = 0; i < s->nb_colors; i++) {
286         if ((s->rgba_palette[i] >> 24) == 0)
287             transp_color[i] = 1;
288     }
289     y1 = 0;
290     while (y1 < s->h && is_transp(s->bitmap + y1 * s->linesize, 1, s->w, 
291                                   transp_color))
292         y1++;
293     if (y1 == s->h) {
294         av_freep(&s->bitmap);
295         s->w = s->h = 0;
296         return 0;
297     }
298
299     y2 = s->h - 1;
300     while (y2 > 0 && is_transp(s->bitmap + y2 * s->linesize, 1, s->w, 
301                                transp_color))
302         y2--;
303     x1 = 0;
304     while (x1 < (s->w - 1) && is_transp(s->bitmap + x1, s->linesize, s->h, 
305                                         transp_color))
306         x1++;
307     x2 = s->w - 1;
308     while (x2 > 0 && is_transp(s->bitmap + x2, s->linesize, s->h, 
309                                   transp_color))
310         x2--;
311     w = x2 - x1 + 1;
312     h = y2 - y1 + 1;
313     bitmap = av_malloc(w * h);
314     if (!bitmap)
315         return 1;
316     for(y = 0; y < h; y++) {
317         memcpy(bitmap + w * y, s->bitmap + x1 + (y1 + y) * s->linesize, w);
318     }
319     av_freep(&s->bitmap);
320     s->bitmap = bitmap;
321     s->linesize = w;
322     s->w = w;
323     s->h = h;
324     s->x += x1;
325     s->y += y1;
326     return 1;
327 }
328
329 static int dvdsub_close_decoder(AVCodecContext *avctx)
330 {
331     return 0;
332 }
333
334 #ifdef DEBUG
335 #undef fprintf
336 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
337                      uint32_t *rgba_palette)
338 {
339     int x, y, v;
340     FILE *f;
341
342     f = fopen(filename, "w");
343     if (!f) {
344         perror(filename);
345         exit(1);
346     }
347     fprintf(f, "P6\n"
348             "%d %d\n"
349             "%d\n",
350             w, h, 255);
351     for(y = 0; y < h; y++) {
352         for(x = 0; x < w; x++) {
353             v = rgba_palette[bitmap[y * w + x]];
354             putc((v >> 16) & 0xff, f);
355             putc((v >> 8) & 0xff, f);
356             putc((v >> 0) & 0xff, f);
357         }
358     }
359     fclose(f);
360 }
361 #endif
362
363 static int dvdsub_decode(AVCodecContext *avctx,
364                          void *data, int *data_size,
365                          uint8_t *buf, int buf_size)
366 {
367     AVSubtitle *sub = (void *)data;
368
369     if (decode_dvd_subtitles(sub, buf, buf_size) < 0) {
370     no_subtitle:
371         *data_size = 0;
372
373         return buf_size;
374     }
375     if (find_smallest_bouding_rectangle(sub) == 0)
376         goto no_subtitle;
377
378 #if defined(DEBUG)
379     av_log(NULL, AV_LOG_INFO, "start=%d ms end =%d ms\n", 
380            sub->start_display_time,
381            sub->end_display_time);
382     ppm_save("/tmp/a.ppm", sub->bitmap, 
383              sub->w, sub->h, sub->rgba_palette);
384 #endif
385
386     *data_size = 1;
387     return buf_size;
388 }
389
390 AVCodec dvdsub_decoder = {
391     "dvdsub",
392     CODEC_TYPE_SUBTITLE,
393     CODEC_ID_DVD_SUBTITLE,
394     sizeof(DVDSubContext),
395     dvdsub_init_decoder,
396     NULL,
397     dvdsub_close_decoder,
398     dvdsub_decode,
399 };
400
401 /* parser definition */
402 typedef struct DVDSubParseContext {
403     uint8_t *packet;
404     int packet_len;
405     int packet_index;
406 } DVDSubParseContext;
407
408 static int dvdsub_parse_init(AVCodecParserContext *s)
409 {
410     return 0;
411 }
412
413 static int dvdsub_parse(AVCodecParserContext *s,
414                         AVCodecContext *avctx,
415                         uint8_t **poutbuf, int *poutbuf_size, 
416                         const uint8_t *buf, int buf_size)
417 {
418     DVDSubParseContext *pc = s->priv_data;
419     
420     if (pc->packet_index == 0) {
421         if (buf_size < 2)
422             return 0;
423         pc->packet_len = (buf[0] << 8) | buf[1];
424         av_freep(&pc->packet);
425         pc->packet = av_malloc(pc->packet_len);
426     }
427     if (pc->packet) {
428         if (pc->packet_index + buf_size <= pc->packet_len) {
429             memcpy(pc->packet + pc->packet_index, buf, buf_size);
430             pc->packet_index += buf_size;
431             if (pc->packet_index >= pc->packet_len) {
432                 *poutbuf = pc->packet;
433                 *poutbuf_size = pc->packet_len;
434                 pc->packet_index = 0;
435                 return buf_size;
436             }
437         } else {
438             /* erroneous size */
439             pc->packet_index = 0;
440         }
441     }
442     *poutbuf = NULL;
443     *poutbuf_size = 0;
444     return buf_size;
445 }
446
447 static void dvdsub_parse_close(AVCodecParserContext *s)
448 {
449     DVDSubParseContext *pc = s->priv_data;
450     av_freep(&pc->packet);
451 }
452
453 AVCodecParser dvdsub_parser = {
454     { CODEC_ID_DVD_SUBTITLE },
455     sizeof(DVDSubParseContext),
456     dvdsub_parse_init,
457     dvdsub_parse,
458     dvdsub_parse_close,
459 };