]> git.sesse.net Git - ffmpeg/blob - libavcodec/dvdsubdec.c
lavc: Add data and linesize to AVSubtitleRect
[ffmpeg] / libavcodec / dvdsubdec.c
1 /*
2  * DVD subtitle decoding
3  * Copyright (c) 2005 Fabrice Bellard
4  *
5  * This file is part of Libav.
6  *
7  * Libav 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  * Libav 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 Libav; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "avcodec.h"
23 #include "get_bits.h"
24 #include "internal.h"
25
26 #include "libavutil/attributes.h"
27 #include "libavutil/colorspace.h"
28 #include "libavutil/imgutils.h"
29 #include "libavutil/avstring.h"
30
31 typedef struct DVDSubContext {
32     uint32_t palette[16];
33     int      has_palette;
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     const uint8_t *cm = ff_crop_tab + 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(DVDSubContext* ctx,
127                           uint32_t *rgba_palette,
128                           uint8_t *colormap,
129                           uint8_t *alpha,
130                           uint32_t subtitle_color)
131 {
132     uint8_t color_used[16] = { 0 };
133     int nb_opaque_colors, i, level, j, r, g, b;
134
135     if (ctx->has_palette) {
136         for (i = 0; i < 4; i++)
137             rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
138                               | ((alpha[i] * 17) << 24);
139         return;
140     }
141
142     for(i = 0; i < 4; i++)
143         rgba_palette[i] = 0;
144
145     nb_opaque_colors = 0;
146     for(i = 0; i < 4; i++) {
147         if (alpha[i] != 0 && !color_used[colormap[i]]) {
148             color_used[colormap[i]] = 1;
149             nb_opaque_colors++;
150         }
151     }
152
153     if (nb_opaque_colors == 0)
154         return;
155
156     j = nb_opaque_colors;
157     memset(color_used, 0, 16);
158     for(i = 0; i < 4; i++) {
159         if (alpha[i] != 0) {
160             if (!color_used[colormap[i]])  {
161                 level = (0xff * j) / nb_opaque_colors;
162                 r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
163                 g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
164                 b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
165                 rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
166                 color_used[colormap[i]] = (i + 1);
167                 j--;
168             } else {
169                 rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
170                                     ((alpha[i] * 17) << 24);
171             }
172         }
173     }
174 }
175
176 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
177
178 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
179                                 const uint8_t *buf, int buf_size)
180 {
181     int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
182     int big_offsets, offset_size, is_8bit = 0;
183     const uint8_t *yuv_palette = 0;
184     uint8_t colormap[4] = { 0 }, alpha[256] = { 0 };
185     int date;
186     int i;
187     int is_menu = 0;
188
189     if (buf_size < 10)
190         return -1;
191     memset(sub_header, 0, sizeof(*sub_header));
192
193     if (AV_RB16(buf) == 0) {   /* HD subpicture with 4-byte offsets */
194         big_offsets = 1;
195         offset_size = 4;
196         cmd_pos = 6;
197     } else {
198         big_offsets = 0;
199         offset_size = 2;
200         cmd_pos = 2;
201     }
202
203     cmd_pos = READ_OFFSET(buf + cmd_pos);
204
205     while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
206         date = AV_RB16(buf + cmd_pos);
207         next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
208         ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
209                 cmd_pos, next_cmd_pos, date);
210         pos = cmd_pos + 2 + offset_size;
211         offset1 = -1;
212         offset2 = -1;
213         x1 = y1 = x2 = y2 = 0;
214         while (pos < buf_size) {
215             cmd = buf[pos++];
216             ff_dlog(NULL, "cmd=%02x\n", cmd);
217             switch(cmd) {
218             case 0x00:
219                 /* menu subpicture */
220                 is_menu = 1;
221                 break;
222             case 0x01:
223                 /* set start date */
224                 sub_header->start_display_time = (date << 10) / 90;
225                 break;
226             case 0x02:
227                 /* set end date */
228                 sub_header->end_display_time = (date << 10) / 90;
229                 break;
230             case 0x03:
231                 /* set colormap */
232                 if ((buf_size - pos) < 2)
233                     goto fail;
234                 colormap[3] = buf[pos] >> 4;
235                 colormap[2] = buf[pos] & 0x0f;
236                 colormap[1] = buf[pos + 1] >> 4;
237                 colormap[0] = buf[pos + 1] & 0x0f;
238                 pos += 2;
239                 break;
240             case 0x04:
241                 /* set alpha */
242                 if ((buf_size - pos) < 2)
243                     goto fail;
244                 alpha[3] = buf[pos] >> 4;
245                 alpha[2] = buf[pos] & 0x0f;
246                 alpha[1] = buf[pos + 1] >> 4;
247                 alpha[0] = buf[pos + 1] & 0x0f;
248                 pos += 2;
249             ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
250                 break;
251             case 0x05:
252             case 0x85:
253                 if ((buf_size - pos) < 6)
254                     goto fail;
255                 x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
256                 x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
257                 y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
258                 y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
259                 if (cmd & 0x80)
260                     is_8bit = 1;
261                 ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
262                 pos += 6;
263                 break;
264             case 0x06:
265                 if ((buf_size - pos) < 4)
266                     goto fail;
267                 offset1 = AV_RB16(buf + pos);
268                 offset2 = AV_RB16(buf + pos + 2);
269                 ff_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
270                 pos += 4;
271                 break;
272             case 0x86:
273                 if ((buf_size - pos) < 8)
274                     goto fail;
275                 offset1 = AV_RB32(buf + pos);
276                 offset2 = AV_RB32(buf + pos + 4);
277                 ff_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
278                 pos += 8;
279                 break;
280
281             case 0x83:
282                 /* HD set palette */
283                 if ((buf_size - pos) < 768)
284                     goto fail;
285                 yuv_palette = buf + pos;
286                 pos += 768;
287                 break;
288             case 0x84:
289                 /* HD set contrast (alpha) */
290                 if ((buf_size - pos) < 256)
291                     goto fail;
292                 for (i = 0; i < 256; i++)
293                     alpha[i] = 0xFF - buf[pos+i];
294                 pos += 256;
295                 break;
296
297             case 0xff:
298                 goto the_end;
299             default:
300                 ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
301                 goto the_end;
302             }
303         }
304     the_end:
305         if (offset1 >= 0) {
306             int w, h;
307             uint8_t *bitmap;
308
309             /* decode the bitmap */
310             w = x2 - x1 + 1;
311             if (w < 0)
312                 w = 0;
313             h = y2 - y1;
314             if (h < 0)
315                 h = 0;
316             if (w > 0 && h > 0) {
317                 int j;
318                 AVSubtitleRect *rect;
319                 if (sub_header->rects) {
320                     for (i = 0; i < sub_header->num_rects; i++) {
321                         av_freep(&sub_header->rects[i]->data[0]);
322                         av_freep(&sub_header->rects[i]->data[1]);
323                         av_freep(&sub_header->rects[i]);
324                     }
325                     av_freep(&sub_header->rects);
326                     sub_header->num_rects = 0;
327                 }
328
329                 sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
330                 if (!sub_header->rects)
331                     goto fail;
332                 sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
333                 if (!sub_header->rects[0])
334                     goto fail;
335                 sub_header->num_rects = 1;
336                 bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
337                 if (!bitmap)
338                     goto fail;
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]->data[1] = av_mallocz(AVPALETTE_SIZE);
344                 if (!sub_header->rects[0]->data[1])
345                     goto fail;
346                 if (is_8bit) {
347                     if (yuv_palette == 0)
348                         goto fail;
349                     sub_header->rects[0]->nb_colors = 256;
350                     yuv_a_to_rgba(yuv_palette, alpha,
351                                   (uint32_t *)sub_header->rects[0]->data[1],
352                                   256);
353                 } else {
354                     sub_header->rects[0]->nb_colors = 4;
355                     guess_palette(ctx,
356                                   (uint32_t *)sub_header->rects[0]->data[1],
357                                   colormap, alpha, 0xffff00);
358                 }
359                 sub_header->rects[0]->x = x1;
360                 sub_header->rects[0]->y = y1;
361                 sub_header->rects[0]->w = w;
362                 sub_header->rects[0]->h = h;
363                 sub_header->rects[0]->type = SUBTITLE_BITMAP;
364                 sub_header->rects[0]->linesize[0] = w;
365
366 #if FF_API_AVPICTURE
367 FF_DISABLE_DEPRECATION_WARNINGS
368                 rect = sub_header->rects[0];
369                 for (j = 0; j < 4; j++) {
370                     rect->pict.data[j] = rect->data[j];
371                     rect->pict.linesize[j] = rect->linesize[j];
372                 }
373 FF_ENABLE_DEPRECATION_WARNINGS
374 #endif
375             }
376         }
377         if (next_cmd_pos == cmd_pos)
378             break;
379         cmd_pos = next_cmd_pos;
380     }
381     if (sub_header->num_rects > 0)
382         return is_menu;
383  fail:
384     if (!sub_header->rects) {
385         for (i = 0; i < sub_header->num_rects; i++) {
386             av_freep(&sub_header->rects[i]->data[0]);
387             av_freep(&sub_header->rects[i]->data[1]);
388             av_freep(&sub_header->rects[i]);
389         }
390         av_freep(&sub_header->rects);
391         sub_header->num_rects = 0;
392     }
393     return -1;
394 }
395
396 static int is_transp(const uint8_t *buf, int pitch, int n,
397                      const uint8_t *transp_color)
398 {
399     int i;
400     for(i = 0; i < n; i++) {
401         if (!transp_color[*buf])
402             return 0;
403         buf += pitch;
404     }
405     return 1;
406 }
407
408 /* return 0 if empty rectangle, 1 if non empty */
409 static int find_smallest_bounding_rectangle(AVSubtitle *s)
410 {
411     uint8_t transp_color[256] = { 0 };
412     int y1, y2, x1, x2, y, w, h, i;
413     uint8_t *bitmap;
414
415     if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
416         return 0;
417
418     for(i = 0; i < s->rects[0]->nb_colors; i++) {
419         if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0)
420             transp_color[i] = 1;
421     }
422     y1 = 0;
423     while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
424                                   1, s->rects[0]->w, transp_color))
425         y1++;
426     if (y1 == s->rects[0]->h) {
427         av_freep(&s->rects[0]->data[0]);
428         s->rects[0]->w = s->rects[0]->h = 0;
429         return 0;
430     }
431
432     y2 = s->rects[0]->h - 1;
433     while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
434                                s->rects[0]->w, transp_color))
435         y2--;
436     x1 = 0;
437     while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
438                                         s->rects[0]->h, transp_color))
439         x1++;
440     x2 = s->rects[0]->w - 1;
441     while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
442                                   transp_color))
443         x2--;
444     w = x2 - x1 + 1;
445     h = y2 - y1 + 1;
446     bitmap = av_malloc(w * h);
447     if (!bitmap)
448         return 1;
449     for(y = 0; y < h; y++) {
450         memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
451     }
452     av_freep(&s->rects[0]->data[0]);
453     s->rects[0]->data[0] = bitmap;
454     s->rects[0]->linesize[0] = w;
455     s->rects[0]->w = w;
456     s->rects[0]->h = h;
457     s->rects[0]->x += x1;
458     s->rects[0]->y += y1;
459     return 1;
460 }
461
462 #ifdef DEBUG
463 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
464                      uint32_t *rgba_palette)
465 {
466     int x, y, v;
467     FILE *f;
468
469     f = fopen(filename, "w");
470     if (!f) {
471         perror(filename);
472         exit(1);
473     }
474     fprintf(f, "P6\n"
475             "%d %d\n"
476             "%d\n",
477             w, h, 255);
478     for(y = 0; y < h; y++) {
479         for(x = 0; x < w; x++) {
480             v = rgba_palette[bitmap[y * w + x]];
481             putc((v >> 16) & 0xff, f);
482             putc((v >> 8) & 0xff, f);
483             putc((v >> 0) & 0xff, f);
484         }
485     }
486     fclose(f);
487 }
488 #endif
489
490 static int dvdsub_decode(AVCodecContext *avctx,
491                          void *data, int *data_size,
492                          AVPacket *avpkt)
493 {
494     DVDSubContext *ctx = avctx->priv_data;
495     const uint8_t *buf = avpkt->data;
496     int buf_size = avpkt->size;
497     AVSubtitle *sub = data;
498     int is_menu;
499
500     is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
501
502     if (is_menu < 0) {
503     no_subtitle:
504         *data_size = 0;
505
506         return buf_size;
507     }
508     if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
509         goto no_subtitle;
510
511 #if defined(DEBUG)
512     ff_dlog(NULL, "start=%d ms end =%d ms\n",
513             sub->start_display_time,
514             sub->end_display_time);
515     ppm_save("/tmp/a.ppm", sub->rects[0]->data[0],
516              sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->data[1]);
517 #endif
518
519     *data_size = 1;
520     return buf_size;
521 }
522
523 static av_cold int dvdsub_init(AVCodecContext *avctx)
524 {
525     DVDSubContext *ctx = avctx->priv_data;
526     char *data, *cur;
527     int ret = 0;
528
529     if (!avctx->extradata || !avctx->extradata_size)
530         return 0;
531
532     data = av_malloc(avctx->extradata_size + 1);
533     if (!data)
534         return AVERROR(ENOMEM);
535     memcpy(data, avctx->extradata, avctx->extradata_size);
536     data[avctx->extradata_size] = '\0';
537     cur = data;
538
539     while (*cur) {
540         if (strncmp("palette:", cur, 8) == 0) {
541             int i;
542             char *p = cur + 8;
543             ctx->has_palette = 1;
544             for (i = 0; i < 16; i++) {
545                 ctx->palette[i] = strtoul(p, &p, 16);
546                 while (*p == ',' || av_isspace(*p))
547                     p++;
548             }
549         } else if (!strncmp("size:", cur, 5)) {
550             int w, h;
551             if (sscanf(cur + 5, "%dx%d", &w, &h) == 2) {
552                ret = ff_set_dimensions(avctx, w, h);
553                if (ret < 0)
554                    goto fail;
555             }
556         }
557         cur += strcspn(cur, "\n\r");
558         cur += strspn(cur, "\n\r");
559     }
560
561 fail:
562     av_free(data);
563     return ret;
564 }
565
566 AVCodec ff_dvdsub_decoder = {
567     .name           = "dvdsub",
568     .long_name      = NULL_IF_CONFIG_SMALL("DVD subtitles"),
569     .type           = AVMEDIA_TYPE_SUBTITLE,
570     .id             = AV_CODEC_ID_DVD_SUBTITLE,
571     .priv_data_size = sizeof(DVDSubContext),
572     .init           = dvdsub_init,
573     .decode         = dvdsub_decode,
574 };