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