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