]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdsubdec.c
faster and simpler vp6 bilinear mc
[ffmpeg] / libavcodec / dvdsubdec.c
1 /*
2  * DVD subtitle decoding for ffmpeg
3  * Copyright (c) 2005 Fabrice Bellard.
4  *
5  * This file is part of FFmpeg.
6  *
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.
11  *
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.
16  *
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
20  */
21 #include "avcodec.h"
22
23 //#define DEBUG
24
25 static int dvdsub_init_decoder(AVCodecContext *avctx)
26 {
27     return 0;
28 }
29
30 static int get_nibble(const uint8_t *buf, int nibble_offset)
31 {
32     return (buf[nibble_offset >> 1] >> ((1 - (nibble_offset & 1)) << 2)) & 0xf;
33 }
34
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)
37 {
38     unsigned int v;
39     int x, y, len, color, nibble_end;
40     uint8_t *d;
41
42     nibble_end = buf_size * 2;
43     x = 0;
44     y = 0;
45     d = bitmap;
46     for(;;) {
47         if (nibble_offset >= nibble_end)
48             return -1;
49         v = get_nibble(buf, nibble_offset++);
50         if (v < 0x4) {
51             v = (v << 4) | get_nibble(buf, nibble_offset++);
52             if (v < 0x10) {
53                 v = (v << 4) | get_nibble(buf, nibble_offset++);
54                 if (v < 0x040) {
55                     v = (v << 4) | get_nibble(buf, nibble_offset++);
56                     if (v < 4) {
57                         v |= (w - x) << 2;
58                     }
59                 }
60             }
61         }
62         len = v >> 2;
63         if (len > (w - x))
64             len = (w - x);
65         color = v & 0x03;
66         memset(d + x, color, len);
67         x += len;
68         if (x >= w) {
69             y++;
70             if (y >= h)
71                 break;
72             d += linesize;
73             x = 0;
74             /* byte align */
75             nibble_offset += (nibble_offset & 1);
76         }
77     }
78     return 0;
79 }
80
81 static void guess_palette(uint32_t *rgba_palette,
82                           uint8_t *palette,
83                           uint8_t *alpha,
84                           uint32_t subtitle_color)
85 {
86     uint8_t color_used[16];
87     int nb_opaque_colors, i, level, j, r, g, b;
88
89     for(i = 0; i < 4; i++)
90         rgba_palette[i] = 0;
91
92     memset(color_used, 0, 16);
93     nb_opaque_colors = 0;
94     for(i = 0; i < 4; i++) {
95         if (alpha[i] != 0 && !color_used[palette[i]]) {
96             color_used[palette[i]] = 1;
97             nb_opaque_colors++;
98         }
99     }
100
101     if (nb_opaque_colors == 0)
102         return;
103
104     j = nb_opaque_colors;
105     memset(color_used, 0, 16);
106     for(i = 0; i < 4; i++) {
107         if (alpha[i] != 0) {
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);
115                 j--;
116             } else {
117                 rgba_palette[i] = (rgba_palette[color_used[palette[i]] - 1] & 0x00ffffff) |
118                                     ((alpha[i] * 17) << 24);
119             }
120         }
121     }
122 }
123
124 static int decode_dvd_subtitles(AVSubtitle *sub_header,
125                                 const uint8_t *buf, int buf_size)
126 {
127     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
128     uint8_t palette[4], alpha[4];
129     int date;
130     int i;
131     int is_menu = 0;
132
133     if (buf_size < 4)
134         return -1;
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;
139
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);
144 #ifdef DEBUG
145         av_log(NULL, AV_LOG_INFO, "cmd_pos=0x%04x next=0x%04x date=%d\n",
146                cmd_pos, next_cmd_pos, date);
147 #endif
148         pos = cmd_pos + 4;
149         offset1 = -1;
150         offset2 = -1;
151         x1 = y1 = x2 = y2 = 0;
152         while (pos < buf_size) {
153             cmd = buf[pos++];
154 #ifdef DEBUG
155             av_log(NULL, AV_LOG_INFO, "cmd=%02x\n", cmd);
156 #endif
157             switch(cmd) {
158             case 0x00:
159                 /* menu subpicture */
160                 is_menu = 1;
161                 break;
162             case 0x01:
163                 /* set start date */
164                 sub_header->start_display_time = (date << 10) / 90;
165                 break;
166             case 0x02:
167                 /* set end date */
168                 sub_header->end_display_time = (date << 10) / 90;
169                 break;
170             case 0x03:
171                 /* set palette */
172                 if ((buf_size - pos) < 2)
173                     goto fail;
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;
178                 pos += 2;
179                 break;
180             case 0x04:
181                 /* set alpha */
182                 if ((buf_size - pos) < 2)
183                     goto fail;
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;
188                 pos += 2;
189 #ifdef DEBUG
190             av_log(NULL, AV_LOG_INFO, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
191 #endif
192                 break;
193             case 0x05:
194                 if ((buf_size - pos) < 6)
195                     goto fail;
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];
200 #ifdef DEBUG
201                 av_log(NULL, AV_LOG_INFO, "x1=%d x2=%d y1=%d y2=%d\n",
202                        x1, x2, y1, y2);
203 #endif
204                 pos += 6;
205                 break;
206             case 0x06:
207                 if ((buf_size - pos) < 4)
208                     goto fail;
209                 offset1 = AV_RB16(buf + pos);
210                 offset2 = AV_RB16(buf + pos + 2);
211 #ifdef DEBUG
212                 av_log(NULL, AV_LOG_INFO, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
213 #endif
214                 pos += 4;
215                 break;
216             case 0xff:
217             default:
218                 goto the_end;
219             }
220         }
221     the_end:
222         if (offset1 >= 0) {
223             int w, h;
224             uint8_t *bitmap;
225
226             /* decode the bitmap */
227             w = x2 - x1 + 1;
228             if (w < 0)
229                 w = 0;
230             h = y2 - y1;
231             if (h < 0)
232                 h = 0;
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);
238                     }
239                     av_freep(&sub_header->rects);
240                     sub_header->num_rects = 0;
241                 }
242
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;
260             }
261         }
262         if (next_cmd_pos == cmd_pos)
263             break;
264         cmd_pos = next_cmd_pos;
265     }
266     if (sub_header->num_rects > 0)
267         return is_menu;
268  fail:
269     return -1;
270 }
271
272 static int is_transp(const uint8_t *buf, int pitch, int n,
273                      const uint8_t *transp_color)
274 {
275     int i;
276     for(i = 0; i < n; i++) {
277         if (!transp_color[*buf])
278             return 0;
279         buf += pitch;
280     }
281     return 1;
282 }
283
284 /* return 0 if empty rectangle, 1 if non empty */
285 static int find_smallest_bounding_rectangle(AVSubtitle *s)
286 {
287     uint8_t transp_color[256];
288     int y1, y2, x1, x2, y, w, h, i;
289     uint8_t *bitmap;
290
291     if (s->num_rects == 0 || s->rects == NULL || s->rects[0].w <= 0 || s->rects[0].h <= 0)
292         return 0;
293
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)
297             transp_color[i] = 1;
298     }
299     y1 = 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))
302         y1++;
303     if (y1 == s->rects[0].h) {
304         av_freep(&s->rects[0].bitmap);
305         s->rects[0].w = s->rects[0].h = 0;
306         return 0;
307     }
308
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))
312         y2--;
313     x1 = 0;
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))
316         x1++;
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,
319                                   transp_color))
320         x2--;
321     w = x2 - x1 + 1;
322     h = y2 - y1 + 1;
323     bitmap = av_malloc(w * h);
324     if (!bitmap)
325         return 1;
326     for(y = 0; y < h; y++) {
327         memcpy(bitmap + w * y, s->rects[0].bitmap + x1 + (y1 + y) * s->rects[0].linesize, w);
328     }
329     av_freep(&s->rects[0].bitmap);
330     s->rects[0].bitmap = bitmap;
331     s->rects[0].linesize = w;
332     s->rects[0].w = w;
333     s->rects[0].h = h;
334     s->rects[0].x += x1;
335     s->rects[0].y += y1;
336     return 1;
337 }
338
339 static int dvdsub_close_decoder(AVCodecContext *avctx)
340 {
341     return 0;
342 }
343
344 #ifdef DEBUG
345 #undef fprintf
346 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
347                      uint32_t *rgba_palette)
348 {
349     int x, y, v;
350     FILE *f;
351
352     f = fopen(filename, "w");
353     if (!f) {
354         perror(filename);
355         exit(1);
356     }
357     fprintf(f, "P6\n"
358             "%d %d\n"
359             "%d\n",
360             w, h, 255);
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);
367         }
368     }
369     fclose(f);
370 }
371 #endif
372
373 static int dvdsub_decode(AVCodecContext *avctx,
374                          void *data, int *data_size,
375                          uint8_t *buf, int buf_size)
376 {
377     AVSubtitle *sub = (void *)data;
378     int is_menu;
379
380     is_menu = decode_dvd_subtitles(sub, buf, buf_size);
381
382     if (is_menu < 0) {
383     no_subtitle:
384         *data_size = 0;
385
386         return buf_size;
387     }
388     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
389         goto no_subtitle;
390
391 #if defined(DEBUG)
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);
397 #endif
398
399     *data_size = 1;
400     return buf_size;
401 }
402
403 AVCodec dvdsub_decoder = {
404     "dvdsub",
405     CODEC_TYPE_SUBTITLE,
406     CODEC_ID_DVD_SUBTITLE,
407     0,
408     dvdsub_init_decoder,
409     NULL,
410     dvdsub_close_decoder,
411     dvdsub_decode,
412 };
413
414 /* parser definition */
415 typedef struct DVDSubParseContext {
416     uint8_t *packet;
417     int packet_len;
418     int packet_index;
419 } DVDSubParseContext;
420
421 static int dvdsub_parse_init(AVCodecParserContext *s)
422 {
423     return 0;
424 }
425
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)
430 {
431     DVDSubParseContext *pc = s->priv_data;
432
433     if (pc->packet_index == 0) {
434         if (buf_size < 2)
435             return 0;
436         pc->packet_len = AV_RB16(buf);
437         av_freep(&pc->packet);
438         pc->packet = av_malloc(pc->packet_len);
439     }
440     if (pc->packet) {
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;
448                 return buf_size;
449             }
450         } else {
451             /* erroneous size */
452             pc->packet_index = 0;
453         }
454     }
455     *poutbuf = NULL;
456     *poutbuf_size = 0;
457     return buf_size;
458 }
459
460 static void dvdsub_parse_close(AVCodecParserContext *s)
461 {
462     DVDSubParseContext *pc = s->priv_data;
463     av_freep(&pc->packet);
464 }
465
466 AVCodecParser dvdsub_parser = {
467     { CODEC_ID_DVD_SUBTITLE },
468     sizeof(DVDSubParseContext),
469     dvdsub_parse_init,
470     dvdsub_parse,
471     dvdsub_parse_close,
472 };