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