]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdsubdec.c
Merge remote-tracking branch 'qatar/master'
[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 #include "get_bits.h"
23 #include "dsputil.h"
24 #include "libavutil/colorspace.h"
25
26 //#define DEBUG
27
28 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
29 {
30     uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
31     uint8_t r, g, b;
32     int i, y, cb, cr;
33     int r_add, g_add, b_add;
34
35     for (i = num_values; i > 0; i--) {
36         y = *ycbcr++;
37         cr = *ycbcr++;
38         cb = *ycbcr++;
39         YUV_TO_RGB1_CCIR(cb, cr);
40         YUV_TO_RGB2_CCIR(r, g, b, y);
41         *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
42     }
43 }
44
45 static int decode_run_2bit(GetBitContext *gb, int *color)
46 {
47     unsigned int v, t;
48
49     v = 0;
50     for (t = 1; v < t && t <= 0x40; t <<= 2)
51         v = (v << 4) | get_bits(gb, 4);
52     *color = v & 3;
53     if (v < 4) { /* Code for fill rest of line */
54         return INT_MAX;
55     }
56     return v >> 2;
57 }
58
59 static int decode_run_8bit(GetBitContext *gb, int *color)
60 {
61     int len;
62     int has_run = get_bits1(gb);
63     if (get_bits1(gb))
64         *color = get_bits(gb, 8);
65     else
66         *color = get_bits(gb, 2);
67     if (has_run) {
68         if (get_bits1(gb)) {
69             len = get_bits(gb, 7);
70             if (len == 0)
71                 len = INT_MAX;
72             else
73                 len += 9;
74         } else
75             len = get_bits(gb, 3) + 2;
76     } else
77         len = 1;
78     return len;
79 }
80
81 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
82                       const uint8_t *buf, int start, int buf_size, int is_8bit)
83 {
84     GetBitContext gb;
85     int bit_len;
86     int x, y, len, color;
87     uint8_t *d;
88
89     bit_len = (buf_size - start) * 8;
90     init_get_bits(&gb, buf + start, bit_len);
91
92     x = 0;
93     y = 0;
94     d = bitmap;
95     for(;;) {
96         if (get_bits_count(&gb) > bit_len)
97             return -1;
98         if (is_8bit)
99             len = decode_run_8bit(&gb, &color);
100         else
101             len = decode_run_2bit(&gb, &color);
102         len = FFMIN(len, w - x);
103         memset(d + x, color, len);
104         x += len;
105         if (x >= w) {
106             y++;
107             if (y >= h)
108                 break;
109             d += linesize;
110             x = 0;
111             /* byte align */
112             align_get_bits(&gb);
113         }
114     }
115     return 0;
116 }
117
118 static void guess_palette(uint32_t *rgba_palette,
119                           uint8_t *colormap,
120                           uint8_t *alpha,
121                           uint32_t subtitle_color)
122 {
123     static const uint8_t level_map[4][4] = {
124         // this configuration (full range, lowest to highest) in tests
125         // seemed most common, so assume this
126         {0xff},
127         {0x00, 0xff},
128         {0x00, 0x80, 0xff},
129         {0x00, 0x55, 0xaa, 0xff},
130     };
131     uint8_t color_used[16];
132     int nb_opaque_colors, i, level, j, r, g, b;
133
134     for(i = 0; i < 4; i++)
135         rgba_palette[i] = 0;
136
137     memset(color_used, 0, 16);
138     nb_opaque_colors = 0;
139     for(i = 0; i < 4; i++) {
140         if (alpha[i] != 0 && !color_used[colormap[i]]) {
141             color_used[colormap[i]] = 1;
142             nb_opaque_colors++;
143         }
144     }
145
146     if (nb_opaque_colors == 0)
147         return;
148
149     j = 0;
150     memset(color_used, 0, 16);
151     for(i = 0; i < 4; i++) {
152         if (alpha[i] != 0) {
153             if (!color_used[colormap[i]])  {
154                 level = level_map[nb_opaque_colors][j];
155                 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
156                 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
157                 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
158                 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
159                 color_used[colormap[i]] = (i + 1);
160                 j++;
161             } else {
162                 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
163                                     ((alpha[i] * 17) << 24);
164             }
165         }
166     }
167 }
168
169 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
170
171 static int decode_dvd_subtitles(AVSubtitle *sub_header,
172                                 const uint8_t *buf, int buf_size)
173 {
174     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
175     int big_offsets, offset_size, is_8bit = 0;
176     const uint8_t *yuv_palette = 0;
177     uint8_t colormap[4], alpha[256];
178     int date;
179     int i;
180     int is_menu = 0;
181
182     if (buf_size < 10)
183         return -1;
184
185     if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
186         big_offsets = 1;
187         offset_size = 4;
188         cmd_pos = 6;
189     } else {
190         big_offsets = 0;
191         offset_size = 2;
192         cmd_pos = 2;
193     }
194
195     cmd_pos = READ_OFFSET(buf + cmd_pos);
196
197     while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
198         date = AV_RB16(buf + cmd_pos);
199         next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
200         av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
201                 cmd_pos, next_cmd_pos, date);
202         pos = cmd_pos + 2 + offset_size;
203         offset1 = -1;
204         offset2 = -1;
205         x1 = y1 = x2 = y2 = 0;
206         while (pos < buf_size) {
207             cmd = buf[pos++];
208             av_dlog(NULL, "cmd=%02x\n", cmd);
209             switch(cmd) {
210             case 0x00:
211                 /* menu subpicture */
212                 is_menu = 1;
213                 break;
214             case 0x01:
215                 /* set start date */
216                 sub_header->start_display_time = (date << 10) / 90;
217                 break;
218             case 0x02:
219                 /* set end date */
220                 sub_header->end_display_time = (date << 10) / 90;
221                 break;
222             case 0x03:
223                 /* set colormap */
224                 if ((buf_size - pos) < 2)
225                     goto fail;
226                 colormap[3] = buf[pos] >> 4;
227                 colormap[2] = buf[pos] & 0x0f;
228                 colormap[1] = buf[pos + 1] >> 4;
229                 colormap[0] = buf[pos + 1] & 0x0f;
230                 pos += 2;
231                 break;
232             case 0x04:
233                 /* set alpha */
234                 if ((buf_size - pos) < 2)
235                     goto fail;
236                 alpha[3] = buf[pos] >> 4;
237                 alpha[2] = buf[pos] & 0x0f;
238                 alpha[1] = buf[pos + 1] >> 4;
239                 alpha[0] = buf[pos + 1] & 0x0f;
240                 pos += 2;
241             av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
242                 break;
243             case 0x05:
244             case 0x85:
245                 if ((buf_size - pos) < 6)
246                     goto fail;
247                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
248                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
249                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
250                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
251                 if (cmd & 0x80)
252                     is_8bit = 1;
253                 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
254                 pos += 6;
255                 break;
256             case 0x06:
257                 if ((buf_size - pos) < 4)
258                     goto fail;
259                 offset1 = AV_RB16(buf + pos);
260                 offset2 = AV_RB16(buf + pos + 2);
261                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
262                 pos += 4;
263                 break;
264             case 0x86:
265                 if ((buf_size - pos) < 8)
266                     goto fail;
267                 offset1 = AV_RB32(buf + pos);
268                 offset2 = AV_RB32(buf + pos + 4);
269                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
270                 pos += 8;
271                 break;
272
273             case 0x83:
274                 /* HD set palette */
275                 if ((buf_size - pos) < 768)
276                     goto fail;
277                 yuv_palette = buf + pos;
278                 pos += 768;
279                 break;
280             case 0x84:
281                 /* HD set contrast (alpha) */
282                 if ((buf_size - pos) < 256)
283                     goto fail;
284                 for (i = 0; i < 256; i++)
285                     alpha[i] = 0xFF - buf[pos+i];
286                 pos += 256;
287                 break;
288
289             case 0xff:
290                 goto the_end;
291             default:
292                 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
293                 goto the_end;
294             }
295         }
296     the_end:
297         if (offset1 >= 0) {
298             int w, h;
299             uint8_t *bitmap;
300
301             /* decode the bitmap */
302             w = x2 - x1 + 1;
303             if (w < 0)
304                 w = 0;
305             h = y2 - y1;
306             if (h < 0)
307                 h = 0;
308             if (w > 0 && h > 0) {
309                 if (sub_header->rects != NULL) {
310                     for (i = 0; i < sub_header->num_rects; i++) {
311                         av_freep(&sub_header->rects[i]->pict.data[0]);
312                         av_freep(&sub_header->rects[i]->pict.data[1]);
313                         av_freep(&sub_header->rects[i]);
314                     }
315                     av_freep(&sub_header->rects);
316                     sub_header->num_rects = 0;
317                 }
318
319                 bitmap = av_malloc(w * h);
320                 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
321                 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
322                 sub_header->num_rects = 1;
323                 sub_header->rects[0]->pict.data[0] = bitmap;
324                 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
325                            buf, offset1, buf_size, is_8bit);
326                 decode_rle(bitmap + w, w * 2, w, h / 2,
327                            buf, offset2, buf_size, is_8bit);
328                 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
329                 if (is_8bit) {
330                     if (yuv_palette == 0)
331                         goto fail;
332                     sub_header->rects[0]->nb_colors = 256;
333                     yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
334                 } else {
335                     sub_header->rects[0]->nb_colors = 4;
336                     guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1],
337                                   colormap, alpha, 0xffff00);
338                 }
339                 sub_header->rects[0]->x = x1;
340                 sub_header->rects[0]->y = y1;
341                 sub_header->rects[0]->w = w;
342                 sub_header->rects[0]->h = h;
343                 sub_header->rects[0]->type = SUBTITLE_BITMAP;
344                 sub_header->rects[0]->pict.linesize[0] = w;
345             }
346         }
347         if (next_cmd_pos < cmd_pos) {
348             av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
349             break;
350         }
351         if (next_cmd_pos == cmd_pos)
352             break;
353         cmd_pos = next_cmd_pos;
354     }
355     if (sub_header->num_rects > 0)
356         return is_menu;
357  fail:
358     if (sub_header->rects != NULL) {
359         for (i = 0; i < sub_header->num_rects; i++) {
360             av_freep(&sub_header->rects[i]->pict.data[0]);
361             av_freep(&sub_header->rects[i]->pict.data[1]);
362             av_freep(&sub_header->rects[i]);
363         }
364         av_freep(&sub_header->rects);
365         sub_header->num_rects = 0;
366     }
367     return -1;
368 }
369
370 static int is_transp(const uint8_t *buf, int pitch, int n,
371                      const uint8_t *transp_color)
372 {
373     int i;
374     for(i = 0; i < n; i++) {
375         if (!transp_color[*buf])
376             return 0;
377         buf += pitch;
378     }
379     return 1;
380 }
381
382 /* return 0 if empty rectangle, 1 if non empty */
383 static int find_smallest_bounding_rectangle(AVSubtitle *s)
384 {
385     uint8_t transp_color[256];
386     int y1, y2, x1, x2, y, w, h, i;
387     uint8_t *bitmap;
388
389     if (s->num_rects == 0 || s->rects == NULL || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
390         return 0;
391
392     memset(transp_color, 0, 256);
393     for(i = 0; i < s->rects[0]->nb_colors; i++) {
394         if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
395             transp_color[i] = 1;
396     }
397     y1 = 0;
398     while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
399                                   1, s->rects[0]->w, transp_color))
400         y1++;
401     if (y1 == s->rects[0]->h) {
402         av_freep(&s->rects[0]->pict.data[0]);
403         s->rects[0]->w = s->rects[0]->h = 0;
404         return 0;
405     }
406
407     y2 = s->rects[0]->h - 1;
408     while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
409                                s->rects[0]->w, transp_color))
410         y2--;
411     x1 = 0;
412     while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
413                                         s->rects[0]->h, transp_color))
414         x1++;
415     x2 = s->rects[0]->w - 1;
416     while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
417                                   transp_color))
418         x2--;
419     w = x2 - x1 + 1;
420     h = y2 - y1 + 1;
421     bitmap = av_malloc(w * h);
422     if (!bitmap)
423         return 1;
424     for(y = 0; y < h; y++) {
425         memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
426     }
427     av_freep(&s->rects[0]->pict.data[0]);
428     s->rects[0]->pict.data[0] = bitmap;
429     s->rects[0]->pict.linesize[0] = w;
430     s->rects[0]->w = w;
431     s->rects[0]->h = h;
432     s->rects[0]->x += x1;
433     s->rects[0]->y += y1;
434     return 1;
435 }
436
437 #ifdef DEBUG
438 #undef fprintf
439 #undef perror
440 #undef exit
441 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
442                      uint32_t *rgba_palette)
443 {
444     int x, y, v;
445     FILE *f;
446
447     f = fopen(filename, "w");
448     if (!f) {
449         perror(filename);
450         exit(1);
451     }
452     fprintf(f, "P6\n"
453             "%d %d\n"
454             "%d\n",
455             w, h, 255);
456     for(y = 0; y < h; y++) {
457         for(x = 0; x < w; x++) {
458             v = rgba_palette[bitmap[y * w + x]];
459             putc((v >> 16) & 0xff, f);
460             putc((v >> 8) & 0xff, f);
461             putc((v >> 0) & 0xff, f);
462         }
463     }
464     fclose(f);
465 }
466 #endif
467
468 static int dvdsub_decode(AVCodecContext *avctx,
469                          void *data, int *data_size,
470                          AVPacket *avpkt)
471 {
472     const uint8_t *buf = avpkt->data;
473     int buf_size = avpkt->size;
474     AVSubtitle *sub = data;
475     int is_menu;
476
477     is_menu = decode_dvd_subtitles(sub, buf, buf_size);
478
479     if (is_menu < 0) {
480     no_subtitle:
481         *data_size = 0;
482
483         return buf_size;
484     }
485     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
486         goto no_subtitle;
487
488 #if defined(DEBUG)
489     av_dlog(NULL, "start=%d ms end =%d ms\n",
490             sub->start_display_time,
491             sub->end_display_time);
492     ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
493              sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
494 #endif
495
496     *data_size = 1;
497     return buf_size;
498 }
499
500 AVCodec ff_dvdsub_decoder = {
501     .name           = "dvdsub",
502     .type           = AVMEDIA_TYPE_SUBTITLE,
503     .id             = CODEC_ID_DVD_SUBTITLE,
504     .decode         = dvdsub_decode,
505     .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
506 };