]> 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 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];
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     memset(color_used, 0, 16);
153     nb_opaque_colors = 0;
154     for(i = 0; i < 4; i++) {
155         if (alpha[i] != 0 && !color_used[colormap[i]]) {
156             color_used[colormap[i]] = 1;
157             nb_opaque_colors++;
158         }
159     }
160
161     if (nb_opaque_colors == 0)
162         return;
163
164     j = 0;
165     memset(color_used, 0, 16);
166     for(i = 0; i < 4; i++) {
167         if (alpha[i] != 0) {
168             if (!color_used[colormap[i]])  {
169                 level = level_map[nb_opaque_colors][j];
170                 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
171                 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
172                 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
173                 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
174                 color_used[colormap[i]] = (i + 1);
175                 j++;
176             } else {
177                 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
178                                     ((alpha[i] * 17) << 24);
179             }
180         }
181     }
182 }
183
184 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
185
186 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
187                                 const uint8_t *buf, int buf_size)
188 {
189     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
190     int big_offsets, offset_size, is_8bit = 0;
191     const uint8_t *yuv_palette = 0;
192     uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
193     int date;
194     int i;
195     int is_menu = 0;
196
197     if (buf_size < 10)
198         return -1;
199
200     if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
201         big_offsets = 1;
202         offset_size = 4;
203         cmd_pos = 6;
204     } else {
205         big_offsets = 0;
206         offset_size = 2;
207         cmd_pos = 2;
208     }
209
210     cmd_pos = READ_OFFSET(buf + cmd_pos);
211
212     while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
213         date = AV_RB16(buf + cmd_pos);
214         next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
215         av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
216                 cmd_pos, next_cmd_pos, date);
217         pos = cmd_pos + 2 + offset_size;
218         offset1 = -1;
219         offset2 = -1;
220         x1 = y1 = x2 = y2 = 0;
221         while (pos < buf_size) {
222             cmd = buf[pos++];
223             av_dlog(NULL, "cmd=%02x\n", cmd);
224             switch(cmd) {
225             case 0x00:
226                 /* menu subpicture */
227                 is_menu = 1;
228                 break;
229             case 0x01:
230                 /* set start date */
231                 sub_header->start_display_time = (date << 10) / 90;
232                 break;
233             case 0x02:
234                 /* set end date */
235                 sub_header->end_display_time = (date << 10) / 90;
236                 break;
237             case 0x03:
238                 /* set colormap */
239                 if ((buf_size - pos) < 2)
240                     goto fail;
241                 colormap[3] = buf[pos] >> 4;
242                 colormap[2] = buf[pos] & 0x0f;
243                 colormap[1] = buf[pos + 1] >> 4;
244                 colormap[0] = buf[pos + 1] & 0x0f;
245                 pos += 2;
246                 break;
247             case 0x04:
248                 /* set alpha */
249                 if ((buf_size - pos) < 2)
250                     goto fail;
251                 alpha[3] = buf[pos] >> 4;
252                 alpha[2] = buf[pos] & 0x0f;
253                 alpha[1] = buf[pos + 1] >> 4;
254                 alpha[0] = buf[pos + 1] & 0x0f;
255                 pos += 2;
256             av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
257                 break;
258             case 0x05:
259             case 0x85:
260                 if ((buf_size - pos) < 6)
261                     goto fail;
262                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
263                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
264                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
265                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
266                 if (cmd & 0x80)
267                     is_8bit = 1;
268                 av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
269                 pos += 6;
270                 break;
271             case 0x06:
272                 if ((buf_size - pos) < 4)
273                     goto fail;
274                 offset1 = AV_RB16(buf + pos);
275                 offset2 = AV_RB16(buf + pos + 2);
276                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
277                 pos += 4;
278                 break;
279             case 0x86:
280                 if ((buf_size - pos) < 8)
281                     goto fail;
282                 offset1 = AV_RB32(buf + pos);
283                 offset2 = AV_RB32(buf + pos + 4);
284                 av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
285                 pos += 8;
286                 break;
287
288             case 0x83:
289                 /* HD set palette */
290                 if ((buf_size - pos) < 768)
291                     goto fail;
292                 yuv_palette = buf + pos;
293                 pos += 768;
294                 break;
295             case 0x84:
296                 /* HD set contrast (alpha) */
297                 if ((buf_size - pos) < 256)
298                     goto fail;
299                 for (i = 0; i < 256; i++)
300                     alpha[i] = 0xFF - buf[pos+i];
301                 pos += 256;
302                 break;
303
304             case 0xff:
305                 goto the_end;
306             default:
307                 av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
308                 goto the_end;
309             }
310         }
311     the_end:
312         if (offset1 >= 0) {
313             int w, h;
314             uint8_t *bitmap;
315
316             /* decode the bitmap */
317             w = x2 - x1 + 1;
318             if (w < 0)
319                 w = 0;
320             h = y2 - y1;
321             if (h < 0)
322                 h = 0;
323             if (w > 0 && h > 0) {
324                 if (sub_header->rects != NULL) {
325                     for (i = 0; i < sub_header->num_rects; i++) {
326                         av_freep(&sub_header->rects[i]->pict.data[0]);
327                         av_freep(&sub_header->rects[i]->pict.data[1]);
328                         av_freep(&sub_header->rects[i]);
329                     }
330                     av_freep(&sub_header->rects);
331                     sub_header->num_rects = 0;
332                 }
333
334                 bitmap = av_malloc(w * h);
335                 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
336                 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
337                 sub_header->num_rects = 1;
338                 sub_header->rects[0]->pict.data[0] = bitmap;
339                 decode_rle(bitmap, w * 2, w, (h + 1) / 2,
340                            buf, offset1, buf_size, is_8bit);
341                 decode_rle(bitmap + w, w * 2, w, h / 2,
342                            buf, offset2, buf_size, is_8bit);
343                 sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
344                 if (is_8bit) {
345                     if (yuv_palette == 0)
346                         goto fail;
347                     sub_header->rects[0]->nb_colors = 256;
348                     yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
349                 } else {
350                     sub_header->rects[0]->nb_colors = 4;
351                     guess_palette((uint32_t*)sub_header->rects[0]->pict.data[1], ctx,
352                                   0xffff00);
353                 }
354                 sub_header->rects[0]->x = x1;
355                 sub_header->rects[0]->y = y1;
356                 sub_header->rects[0]->w = w;
357                 sub_header->rects[0]->h = h;
358                 sub_header->rects[0]->type = SUBTITLE_BITMAP;
359                 sub_header->rects[0]->pict.linesize[0] = w;
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];
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     memset(transp_color, 0, 256);
408     for(i = 0; i < s->rects[0]->nb_colors; i++) {
409         if ((((uint32_t*)s->rects[0]->pict.data[1])[i] >> 24) == 0)
410             transp_color[i] = 1;
411     }
412     y1 = 0;
413     while (y1 < s->rects[0]->h && is_transp(s->rects[0]->pict.data[0] + y1 * s->rects[0]->pict.linesize[0],
414                                   1, s->rects[0]->w, transp_color))
415         y1++;
416     if (y1 == s->rects[0]->h) {
417         av_freep(&s->rects[0]->pict.data[0]);
418         s->rects[0]->w = s->rects[0]->h = 0;
419         return 0;
420     }
421
422     y2 = s->rects[0]->h - 1;
423     while (y2 > 0 && is_transp(s->rects[0]->pict.data[0] + y2 * s->rects[0]->pict.linesize[0], 1,
424                                s->rects[0]->w, transp_color))
425         y2--;
426     x1 = 0;
427     while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->pict.data[0] + x1, s->rects[0]->pict.linesize[0],
428                                         s->rects[0]->h, transp_color))
429         x1++;
430     x2 = s->rects[0]->w - 1;
431     while (x2 > 0 && is_transp(s->rects[0]->pict.data[0] + x2, s->rects[0]->pict.linesize[0], s->rects[0]->h,
432                                   transp_color))
433         x2--;
434     w = x2 - x1 + 1;
435     h = y2 - y1 + 1;
436     bitmap = av_malloc(w * h);
437     if (!bitmap)
438         return 1;
439     for(y = 0; y < h; y++) {
440         memcpy(bitmap + w * y, s->rects[0]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
441     }
442     av_freep(&s->rects[0]->pict.data[0]);
443     s->rects[0]->pict.data[0] = bitmap;
444     s->rects[0]->pict.linesize[0] = w;
445     s->rects[0]->w = w;
446     s->rects[0]->h = h;
447     s->rects[0]->x += x1;
448     s->rects[0]->y += y1;
449     return 1;
450 }
451
452 #ifdef DEBUG
453 #undef fprintf
454 #undef perror
455 #undef exit
456 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
457                      uint32_t *rgba_palette)
458 {
459     int x, y, v;
460     FILE *f;
461
462     f = fopen(filename, "w");
463     if (!f) {
464         perror(filename);
465         exit(1);
466     }
467     fprintf(f, "P6\n"
468             "%d %d\n"
469             "%d\n",
470             w, h, 255);
471     for(y = 0; y < h; y++) {
472         for(x = 0; x < w; x++) {
473             v = rgba_palette[bitmap[y * w + x]];
474             putc((v >> 16) & 0xff, f);
475             putc((v >> 8) & 0xff, f);
476             putc((v >> 0) & 0xff, f);
477         }
478     }
479     fclose(f);
480 }
481 #endif
482
483 static int dvdsub_decode(AVCodecContext *avctx,
484                          void *data, int *data_size,
485                          AVPacket *avpkt)
486 {
487     DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
488     const uint8_t *buf = avpkt->data;
489     int buf_size = avpkt->size;
490     AVSubtitle *sub = data;
491     int is_menu;
492
493     is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
494
495     if (is_menu < 0) {
496     no_subtitle:
497         *data_size = 0;
498
499         return buf_size;
500     }
501     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
502         goto no_subtitle;
503
504 #if defined(DEBUG)
505     av_dlog(NULL, "start=%d ms end =%d ms\n",
506             sub->start_display_time,
507             sub->end_display_time);
508     ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
509              sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
510 #endif
511
512     *data_size = 1;
513     return buf_size;
514 }
515
516 static int dvdsub_init(AVCodecContext *avctx)
517 {
518     DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
519     char *dataorig, *data;
520
521     if (!avctx->extradata || !avctx->extradata_size)
522         return 1;
523
524     dataorig = data = av_malloc(avctx->extradata_size+1);
525     if (!data)
526         return AVERROR(ENOMEM);
527     memcpy(data, avctx->extradata, avctx->extradata_size);
528     data[avctx->extradata_size] = '\0';
529
530     for(;;) {
531         int pos = strcspn(data, "\n\r");
532         if (pos==0 && *data==0)
533             break;
534
535         if (strncmp("palette:", data, 8) == 0) {
536             int i;
537             char *p = data+8;
538             ctx->has_palette = 1;
539             for(i=0;i<16;i++) {
540                 ctx->palette[i] = strtoul(p, &p, 16);
541                 while(*p == ',' || isspace(*p))
542                     p++;
543             }
544         }
545
546         data += pos;
547         data += strspn(data, "\n\r");
548     }
549
550     if (ctx->has_palette) {
551         int i;
552         av_log(avctx, AV_LOG_DEBUG, "palette:");
553         for(i=0;i<16;i++)
554             av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
555         av_log(avctx, AV_LOG_DEBUG, "\n");
556     }
557
558     av_free(dataorig);
559     return 1;
560 }
561
562 AVCodec ff_dvdsub_decoder = {
563     .name           = "dvdsub",
564     .type           = AVMEDIA_TYPE_SUBTITLE,
565     .id             = CODEC_ID_DVD_SUBTITLE,
566     .priv_data_size = sizeof(DVDSubContext),
567     .init           = dvdsub_init,
568     .decode         = dvdsub_decode,
569     .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
570 };