FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dvdsubdec.c
Go to the documentation of this file.
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/attributes.h"
25 #include "libavutil/colorspace.h"
26 #include "libavutil/opt.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/avstring.h"
29 
30 typedef struct DVDSubContext
31 {
32  AVClass *class;
33  uint32_t palette[16];
34  char *palette_str;
37  uint8_t alpha[256];
39  int buf_size;
40 #ifdef DEBUG
41  int sub_id;
42 #endif
44 
45 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
46 {
47  const uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
48  uint8_t r, g, b;
49  int i, y, cb, cr;
50  int r_add, g_add, b_add;
51 
52  for (i = num_values; i > 0; i--) {
53  y = *ycbcr++;
54  cr = *ycbcr++;
55  cb = *ycbcr++;
56  YUV_TO_RGB1_CCIR(cb, cr);
57  YUV_TO_RGB2_CCIR(r, g, b, y);
58  *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
59  }
60 }
61 
62 static int decode_run_2bit(GetBitContext *gb, int *color)
63 {
64  unsigned int v, t;
65 
66  v = 0;
67  for (t = 1; v < t && t <= 0x40; t <<= 2)
68  v = (v << 4) | get_bits(gb, 4);
69  *color = v & 3;
70  if (v < 4) { /* Code for fill rest of line */
71  return INT_MAX;
72  }
73  return v >> 2;
74 }
75 
76 static int decode_run_8bit(GetBitContext *gb, int *color)
77 {
78  int len;
79  int has_run = get_bits1(gb);
80  if (get_bits1(gb))
81  *color = get_bits(gb, 8);
82  else
83  *color = get_bits(gb, 2);
84  if (has_run) {
85  if (get_bits1(gb)) {
86  len = get_bits(gb, 7);
87  if (len == 0)
88  len = INT_MAX;
89  else
90  len += 9;
91  } else
92  len = get_bits(gb, 3) + 2;
93  } else
94  len = 1;
95  return len;
96 }
97 
98 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
99  const uint8_t *buf, int start, int buf_size, int is_8bit)
100 {
101  GetBitContext gb;
102  int bit_len;
103  int x, y, len, color;
104  uint8_t *d;
105 
106  bit_len = (buf_size - start) * 8;
107  init_get_bits(&gb, buf + start, bit_len);
108 
109  x = 0;
110  y = 0;
111  d = bitmap;
112  for(;;) {
113  if (get_bits_count(&gb) > bit_len)
114  return -1;
115  if (is_8bit)
116  len = decode_run_8bit(&gb, &color);
117  else
118  len = decode_run_2bit(&gb, &color);
119  len = FFMIN(len, w - x);
120  memset(d + x, color, len);
121  x += len;
122  if (x >= w) {
123  y++;
124  if (y >= h)
125  break;
126  d += linesize;
127  x = 0;
128  /* byte align */
129  align_get_bits(&gb);
130  }
131  }
132  return 0;
133 }
134 
135 static void guess_palette(DVDSubContext* ctx,
136  uint32_t *rgba_palette,
137  uint32_t subtitle_color)
138 {
139  static const uint8_t level_map[4][4] = {
140  // this configuration (full range, lowest to highest) in tests
141  // seemed most common, so assume this
142  {0xff},
143  {0x00, 0xff},
144  {0x00, 0x80, 0xff},
145  {0x00, 0x55, 0xaa, 0xff},
146  };
147  uint8_t color_used[16] = { 0 };
148  int nb_opaque_colors, i, level, j, r, g, b;
149  uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
150 
151  if(ctx->has_palette) {
152  for(i = 0; i < 4; i++)
153  rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
154  | ((alpha[i] * 17U) << 24);
155  return;
156  }
157 
158  for(i = 0; i < 4; i++)
159  rgba_palette[i] = 0;
160 
161  nb_opaque_colors = 0;
162  for(i = 0; i < 4; i++) {
163  if (alpha[i] != 0 && !color_used[colormap[i]]) {
164  color_used[colormap[i]] = 1;
165  nb_opaque_colors++;
166  }
167  }
168 
169  if (nb_opaque_colors == 0)
170  return;
171 
172  j = 0;
173  memset(color_used, 0, 16);
174  for(i = 0; i < 4; i++) {
175  if (alpha[i] != 0) {
176  if (!color_used[colormap[i]]) {
177  level = level_map[nb_opaque_colors][j];
178  r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
179  g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
180  b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
181  rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17) << 24);
182  color_used[colormap[i]] = (i + 1);
183  j++;
184  } else {
185  rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
186  ((alpha[i] * 17) << 24);
187  }
188  }
189  }
190 }
191 
192 static void reset_rects(AVSubtitle *sub_header)
193 {
194  int i;
195 
196  if (sub_header->rects != NULL) {
197  for (i = 0; i < sub_header->num_rects; i++) {
198  av_freep(&sub_header->rects[i]->pict.data[0]);
199  av_freep(&sub_header->rects[i]->pict.data[1]);
200  av_freep(&sub_header->rects[i]);
201  }
202  av_freep(&sub_header->rects);
203  sub_header->num_rects = 0;
204  }
205 }
206 
207 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
208 
209 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
210  const uint8_t *buf, int buf_size)
211 {
212  int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
213  int big_offsets, offset_size, is_8bit = 0;
214  const uint8_t *yuv_palette = 0;
215  uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
216  int date;
217  int i;
218  int is_menu = 0;
219 
220  if (buf_size < 10)
221  return -1;
222 
223  if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
224  big_offsets = 1;
225  offset_size = 4;
226  cmd_pos = 6;
227  } else {
228  big_offsets = 0;
229  offset_size = 2;
230  cmd_pos = 2;
231  }
232 
233  cmd_pos = READ_OFFSET(buf + cmd_pos);
234 
235  if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size)
236  return AVERROR(EAGAIN);
237 
238  while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
239  date = AV_RB16(buf + cmd_pos);
240  next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
241  av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
242  cmd_pos, next_cmd_pos, date);
243  pos = cmd_pos + 2 + offset_size;
244  offset1 = -1;
245  offset2 = -1;
246  x1 = y1 = x2 = y2 = 0;
247  while (pos < buf_size) {
248  cmd = buf[pos++];
249  av_dlog(NULL, "cmd=%02x\n", cmd);
250  switch(cmd) {
251  case 0x00:
252  /* menu subpicture */
253  is_menu = 1;
254  break;
255  case 0x01:
256  /* set start date */
257  sub_header->start_display_time = (date << 10) / 90;
258  break;
259  case 0x02:
260  /* set end date */
261  sub_header->end_display_time = (date << 10) / 90;
262  break;
263  case 0x03:
264  /* set colormap */
265  if ((buf_size - pos) < 2)
266  goto fail;
267  colormap[3] = buf[pos] >> 4;
268  colormap[2] = buf[pos] & 0x0f;
269  colormap[1] = buf[pos + 1] >> 4;
270  colormap[0] = buf[pos + 1] & 0x0f;
271  pos += 2;
272  break;
273  case 0x04:
274  /* set alpha */
275  if ((buf_size - pos) < 2)
276  goto fail;
277  alpha[3] = buf[pos] >> 4;
278  alpha[2] = buf[pos] & 0x0f;
279  alpha[1] = buf[pos + 1] >> 4;
280  alpha[0] = buf[pos + 1] & 0x0f;
281  pos += 2;
282  av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
283  break;
284  case 0x05:
285  case 0x85:
286  if ((buf_size - pos) < 6)
287  goto fail;
288  x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
289  x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
290  y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
291  y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
292  if (cmd & 0x80)
293  is_8bit = 1;
294  av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
295  pos += 6;
296  break;
297  case 0x06:
298  if ((buf_size - pos) < 4)
299  goto fail;
300  offset1 = AV_RB16(buf + pos);
301  offset2 = AV_RB16(buf + pos + 2);
302  av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
303  pos += 4;
304  break;
305  case 0x86:
306  if ((buf_size - pos) < 8)
307  goto fail;
308  offset1 = AV_RB32(buf + pos);
309  offset2 = AV_RB32(buf + pos + 4);
310  av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
311  pos += 8;
312  break;
313 
314  case 0x83:
315  /* HD set palette */
316  if ((buf_size - pos) < 768)
317  goto fail;
318  yuv_palette = buf + pos;
319  pos += 768;
320  break;
321  case 0x84:
322  /* HD set contrast (alpha) */
323  if ((buf_size - pos) < 256)
324  goto fail;
325  for (i = 0; i < 256; i++)
326  alpha[i] = 0xFF - buf[pos+i];
327  pos += 256;
328  break;
329 
330  case 0xff:
331  goto the_end;
332  default:
333  av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
334  goto the_end;
335  }
336  }
337  the_end:
338  if (offset1 >= 0) {
339  int w, h;
340  uint8_t *bitmap;
341 
342  /* decode the bitmap */
343  w = x2 - x1 + 1;
344  if (w < 0)
345  w = 0;
346  h = y2 - y1;
347  if (h < 0)
348  h = 0;
349  if (w > 0 && h > 0) {
350  reset_rects(sub_header);
351 
352  bitmap = av_malloc(w * h);
353  sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
354  sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
355  sub_header->num_rects = 1;
356  sub_header->rects[0]->pict.data[0] = bitmap;
357  decode_rle(bitmap, w * 2, w, (h + 1) / 2,
358  buf, offset1, buf_size, is_8bit);
359  decode_rle(bitmap + w, w * 2, w, h / 2,
360  buf, offset2, buf_size, is_8bit);
361  sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
362  if (is_8bit) {
363  if (yuv_palette == 0)
364  goto fail;
365  sub_header->rects[0]->nb_colors = 256;
366  yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
367  } else {
368  sub_header->rects[0]->nb_colors = 4;
369  guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
370  0xffff00);
371  }
372  sub_header->rects[0]->x = x1;
373  sub_header->rects[0]->y = y1;
374  sub_header->rects[0]->w = w;
375  sub_header->rects[0]->h = h;
376  sub_header->rects[0]->type = SUBTITLE_BITMAP;
377  sub_header->rects[0]->pict.linesize[0] = w;
378  sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
379  }
380  }
381  if (next_cmd_pos < cmd_pos) {
382  av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
383  break;
384  }
385  if (next_cmd_pos == cmd_pos)
386  break;
387  cmd_pos = next_cmd_pos;
388  }
389  if (sub_header->num_rects > 0)
390  return is_menu;
391  fail:
392  reset_rects(sub_header);
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 */
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 == NULL || 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]->pict.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]->pict.data[0] + y1 * s->rects[0]->pict.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]->pict.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]->pict.data[0] + y2 * s->rects[0]->pict.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]->pict.data[0] + x1, s->rects[0]->pict.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]->pict.data[0] + x2, s->rects[0]->pict.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]->pict.data[0] + x1 + (y1 + y) * s->rects[0]->pict.linesize[0], w);
451  }
452  av_freep(&s->rects[0]->pict.data[0]);
453  s->rects[0]->pict.data[0] = bitmap;
454  s->rects[0]->pict.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 #undef fprintf
464 #undef perror
465 #undef exit
466 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
467  uint32_t *rgba_palette)
468 {
469  int x, y, v;
470  FILE *f;
471 
472  f = fopen(filename, "w");
473  if (!f) {
474  perror(filename);
475  exit(1);
476  }
477  fprintf(f, "P6\n"
478  "%d %d\n"
479  "%d\n",
480  w, h, 255);
481  for(y = 0; y < h; y++) {
482  for(x = 0; x < w; x++) {
483  v = rgba_palette[bitmap[y * w + x]];
484  putc((v >> 16) & 0xff, f);
485  putc((v >> 8) & 0xff, f);
486  putc((v >> 0) & 0xff, f);
487  }
488  }
489  fclose(f);
490 }
491 #endif
492 
494  const uint8_t *buf, int buf_size)
495 {
496  DVDSubContext *ctx = avctx->priv_data;
497 
498  if (ctx->buf_size > 0xffff - buf_size) {
499  av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
500  "too large SPU packets aborted.\n");
501  av_freep(&ctx->buf);
502  return AVERROR_INVALIDDATA;
503  }
504  ctx->buf = av_realloc(ctx->buf, ctx->buf_size + buf_size);
505  if (!ctx->buf)
506  return AVERROR(ENOMEM);
507  memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
508  ctx->buf_size += buf_size;
509  return 0;
510 }
511 
512 static int dvdsub_decode(AVCodecContext *avctx,
513  void *data, int *data_size,
514  AVPacket *avpkt)
515 {
516  DVDSubContext *ctx = avctx->priv_data;
517  const uint8_t *buf = avpkt->data;
518  int buf_size = avpkt->size;
519  AVSubtitle *sub = data;
520  int is_menu;
521 
522  if (ctx->buf) {
523  int ret = append_to_cached_buf(avctx, buf, buf_size);
524  if (ret < 0) {
525  *data_size = 0;
526  return ret;
527  }
528  buf = ctx->buf;
529  buf_size = ctx->buf_size;
530  }
531 
532  is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
533  if (is_menu == AVERROR(EAGAIN)) {
534  *data_size = 0;
535  return append_to_cached_buf(avctx, buf, buf_size);
536  }
537 
538  if (is_menu < 0) {
539  no_subtitle:
540  *data_size = 0;
541 
542  return buf_size;
543  }
544  if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
545  goto no_subtitle;
546 
547 #if defined(DEBUG)
548  {
549  char ppm_name[32];
550 
551  snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
552  av_dlog(NULL, "start=%d ms end =%d ms\n",
553  sub->start_display_time,
554  sub->end_display_time);
555  ppm_save(ppm_name, sub->rects[0]->pict.data[0],
556  sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
557  }
558 #endif
559 
560  av_freep(&ctx->buf);
561  ctx->buf_size = 0;
562  *data_size = 1;
563  return buf_size;
564 }
565 
566 static void parse_palette(DVDSubContext *ctx, char *p)
567 {
568  int i;
569 
570  ctx->has_palette = 1;
571  for(i=0;i<16;i++) {
572  ctx->palette[i] = strtoul(p, &p, 16);
573  while(*p == ',' || av_isspace(*p))
574  p++;
575  }
576 }
577 
579 {
580  DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
581  char *dataorig, *data;
582 
583  if (!avctx->extradata || !avctx->extradata_size)
584  return 1;
585 
586  dataorig = data = av_malloc(avctx->extradata_size+1);
587  if (!data)
588  return AVERROR(ENOMEM);
589  memcpy(data, avctx->extradata, avctx->extradata_size);
590  data[avctx->extradata_size] = '\0';
591 
592  for(;;) {
593  int pos = strcspn(data, "\n\r");
594  if (pos==0 && *data==0)
595  break;
596 
597  if (strncmp("palette:", data, 8) == 0) {
598  parse_palette(ctx, data + 8);
599  } else if (strncmp("size:", data, 5) == 0) {
600  int w, h;
601  if (sscanf(data + 5, "%dx%d", &w, &h) == 2 &&
602  av_image_check_size(w, h, 0, avctx) >= 0)
603  avcodec_set_dimensions(avctx, w, h);
604  }
605 
606  data += pos;
607  data += strspn(data, "\n\r");
608  }
609 
610  av_free(dataorig);
611  return 1;
612 }
613 
615 {
616  DVDSubContext *ctx = avctx->priv_data;
617  int ret;
618 
619  if ((ret = dvdsub_parse_extradata(avctx)) < 0)
620  return ret;
621 
622  if (ctx->palette_str)
623  parse_palette(ctx, ctx->palette_str);
624  if (ctx->has_palette) {
625  int i;
626  av_log(avctx, AV_LOG_DEBUG, "palette:");
627  for(i=0;i<16;i++)
628  av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
629  av_log(avctx, AV_LOG_DEBUG, "\n");
630  }
631 
632  return 1;
633 }
634 
636 {
637  DVDSubContext *ctx = avctx->priv_data;
638  av_freep(&ctx->buf);
639  ctx->buf_size = 0;
640  return 0;
641 }
642 
643 #define OFFSET(field) offsetof(DVDSubContext, field)
644 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
645 static const AVOption options[] = {
646  { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
647  { NULL }
648 };
649 static const AVClass dvdsub_class = {
650  .class_name = "dvdsubdec",
651  .item_name = av_default_item_name,
652  .option = options,
653  .version = LIBAVUTIL_VERSION_INT,
654 };
655 
657  .name = "dvdsub",
658  .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
659  .type = AVMEDIA_TYPE_SUBTITLE,
661  .priv_data_size = sizeof(DVDSubContext),
662  .init = dvdsub_init,
664  .close = dvdsub_close,
665  .priv_class = &dvdsub_class,
666 };