FFmpeg  1.2.12
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/colorspace.h"
25 #include "libavutil/opt.h"
26 #include "libavutil/imgutils.h"
27 #include "libavutil/avstring.h"
28 
29 //#define DEBUG
30 
31 typedef struct DVDSubContext
32 {
33  AVClass *class;
34  uint32_t palette[16];
35  char *palette_str;
38  uint8_t alpha[256];
40 
41 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
42 {
44  uint8_t r, g, b;
45  int i, y, cb, cr;
46  int r_add, g_add, b_add;
47 
48  for (i = num_values; i > 0; i--) {
49  y = *ycbcr++;
50  cr = *ycbcr++;
51  cb = *ycbcr++;
52  YUV_TO_RGB1_CCIR(cb, cr);
53  YUV_TO_RGB2_CCIR(r, g, b, y);
54  *rgba++ = (*alpha++ << 24) | (r << 16) | (g << 8) | b;
55  }
56 }
57 
58 static int decode_run_2bit(GetBitContext *gb, int *color)
59 {
60  unsigned int v, t;
61 
62  v = 0;
63  for (t = 1; v < t && t <= 0x40; t <<= 2)
64  v = (v << 4) | get_bits(gb, 4);
65  *color = v & 3;
66  if (v < 4) { /* Code for fill rest of line */
67  return INT_MAX;
68  }
69  return v >> 2;
70 }
71 
72 static int decode_run_8bit(GetBitContext *gb, int *color)
73 {
74  int len;
75  int has_run = get_bits1(gb);
76  if (get_bits1(gb))
77  *color = get_bits(gb, 8);
78  else
79  *color = get_bits(gb, 2);
80  if (has_run) {
81  if (get_bits1(gb)) {
82  len = get_bits(gb, 7);
83  if (len == 0)
84  len = INT_MAX;
85  else
86  len += 9;
87  } else
88  len = get_bits(gb, 3) + 2;
89  } else
90  len = 1;
91  return len;
92 }
93 
94 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h,
95  const uint8_t *buf, int start, int buf_size, int is_8bit)
96 {
97  GetBitContext gb;
98  int bit_len;
99  int x, y, len, color;
100  uint8_t *d;
101 
102  if (start >= buf_size)
103  return -1;
104 
105  if (w <= 0 || h <= 0)
106  return -1;
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 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
195 
196 static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header,
197  const uint8_t *buf, int buf_size)
198 {
199  int cmd_pos, pos, cmd, x1, y1, x2, y2, offset1, offset2, next_cmd_pos;
200  int big_offsets, offset_size, is_8bit = 0;
201  const uint8_t *yuv_palette = 0;
202  uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
203  int date;
204  int i;
205  int is_menu = 0;
206 
207  if (buf_size < 10)
208  return -1;
209 
210  if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
211  big_offsets = 1;
212  offset_size = 4;
213  cmd_pos = 6;
214  } else {
215  big_offsets = 0;
216  offset_size = 2;
217  cmd_pos = 2;
218  }
219 
220  cmd_pos = READ_OFFSET(buf + cmd_pos);
221 
222  while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
223  date = AV_RB16(buf + cmd_pos);
224  next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
225  av_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
226  cmd_pos, next_cmd_pos, date);
227  pos = cmd_pos + 2 + offset_size;
228  offset1 = -1;
229  offset2 = -1;
230  x1 = y1 = x2 = y2 = 0;
231  while (pos < buf_size) {
232  cmd = buf[pos++];
233  av_dlog(NULL, "cmd=%02x\n", cmd);
234  switch(cmd) {
235  case 0x00:
236  /* menu subpicture */
237  is_menu = 1;
238  break;
239  case 0x01:
240  /* set start date */
241  sub_header->start_display_time = (date << 10) / 90;
242  break;
243  case 0x02:
244  /* set end date */
245  sub_header->end_display_time = (date << 10) / 90;
246  break;
247  case 0x03:
248  /* set colormap */
249  if ((buf_size - pos) < 2)
250  goto fail;
251  colormap[3] = buf[pos] >> 4;
252  colormap[2] = buf[pos] & 0x0f;
253  colormap[1] = buf[pos + 1] >> 4;
254  colormap[0] = buf[pos + 1] & 0x0f;
255  pos += 2;
256  break;
257  case 0x04:
258  /* set alpha */
259  if ((buf_size - pos) < 2)
260  goto fail;
261  alpha[3] = buf[pos] >> 4;
262  alpha[2] = buf[pos] & 0x0f;
263  alpha[1] = buf[pos + 1] >> 4;
264  alpha[0] = buf[pos + 1] & 0x0f;
265  pos += 2;
266  av_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
267  break;
268  case 0x05:
269  case 0x85:
270  if ((buf_size - pos) < 6)
271  goto fail;
272  x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
273  x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
274  y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
275  y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
276  if (cmd & 0x80)
277  is_8bit = 1;
278  av_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
279  pos += 6;
280  break;
281  case 0x06:
282  if ((buf_size - pos) < 4)
283  goto fail;
284  offset1 = AV_RB16(buf + pos);
285  offset2 = AV_RB16(buf + pos + 2);
286  av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
287  pos += 4;
288  break;
289  case 0x86:
290  if ((buf_size - pos) < 8)
291  goto fail;
292  offset1 = AV_RB32(buf + pos);
293  offset2 = AV_RB32(buf + pos + 4);
294  av_dlog(NULL, "offset1=0x%04x offset2=0x%04x\n", offset1, offset2);
295  pos += 8;
296  break;
297 
298  case 0x83:
299  /* HD set palette */
300  if ((buf_size - pos) < 768)
301  goto fail;
302  yuv_palette = buf + pos;
303  pos += 768;
304  break;
305  case 0x84:
306  /* HD set contrast (alpha) */
307  if ((buf_size - pos) < 256)
308  goto fail;
309  for (i = 0; i < 256; i++)
310  alpha[i] = 0xFF - buf[pos+i];
311  pos += 256;
312  break;
313 
314  case 0xff:
315  goto the_end;
316  default:
317  av_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
318  goto the_end;
319  }
320  }
321  the_end:
322  if (offset1 >= 0) {
323  int w, h;
324  uint8_t *bitmap;
325 
326  /* decode the bitmap */
327  w = x2 - x1 + 1;
328  if (w < 0)
329  w = 0;
330  h = y2 - y1;
331  if (h < 0)
332  h = 0;
333  if (w > 0 && h > 0) {
334  if (sub_header->rects != NULL) {
335  for (i = 0; i < sub_header->num_rects; i++) {
336  av_freep(&sub_header->rects[i]->pict.data[0]);
337  av_freep(&sub_header->rects[i]->pict.data[1]);
338  av_freep(&sub_header->rects[i]);
339  }
340  av_freep(&sub_header->rects);
341  sub_header->num_rects = 0;
342  }
343 
344  bitmap = av_malloc(w * h);
345  sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
346  sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
347  sub_header->num_rects = 1;
348  sub_header->rects[0]->pict.data[0] = bitmap;
349  if (decode_rle(bitmap, w * 2, w, (h + 1) / 2,
350  buf, offset1, buf_size, is_8bit) < 0)
351  goto fail;
352  if (decode_rle(bitmap + w, w * 2, w, h / 2,
353  buf, offset2, buf_size, is_8bit) < 0)
354  goto fail;
355  sub_header->rects[0]->pict.data[1] = av_mallocz(AVPALETTE_SIZE);
356  if (is_8bit) {
357  if (yuv_palette == 0)
358  goto fail;
359  sub_header->rects[0]->nb_colors = 256;
360  yuv_a_to_rgba(yuv_palette, alpha, (uint32_t*)sub_header->rects[0]->pict.data[1], 256);
361  } else {
362  sub_header->rects[0]->nb_colors = 4;
363  guess_palette(ctx, (uint32_t*)sub_header->rects[0]->pict.data[1],
364  0xffff00);
365  }
366  sub_header->rects[0]->x = x1;
367  sub_header->rects[0]->y = y1;
368  sub_header->rects[0]->w = w;
369  sub_header->rects[0]->h = h;
370  sub_header->rects[0]->type = SUBTITLE_BITMAP;
371  sub_header->rects[0]->pict.linesize[0] = w;
372  sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
373  }
374  }
375  if (next_cmd_pos < cmd_pos) {
376  av_log(NULL, AV_LOG_ERROR, "Invalid command offset\n");
377  break;
378  }
379  if (next_cmd_pos == cmd_pos)
380  break;
381  cmd_pos = next_cmd_pos;
382  }
383  if (sub_header->num_rects > 0)
384  return is_menu;
385  fail:
386  if (sub_header->rects != NULL) {
387  for (i = 0; i < sub_header->num_rects; i++) {
388  av_freep(&sub_header->rects[i]->pict.data[0]);
389  av_freep(&sub_header->rects[i]->pict.data[1]);
390  av_freep(&sub_header->rects[i]);
391  }
392  av_freep(&sub_header->rects);
393  sub_header->num_rects = 0;
394  }
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 == NULL || 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 #undef fprintf
466 #undef perror
467 #undef exit
468 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
469  uint32_t *rgba_palette)
470 {
471  int x, y, v;
472  FILE *f;
473 
474  f = fopen(filename, "w");
475  if (!f) {
476  perror(filename);
477  exit(1);
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  putc((v >> 16) & 0xff, f);
487  putc((v >> 8) & 0xff, f);
488  putc((v >> 0) & 0xff, f);
489  }
490  }
491  fclose(f);
492 }
493 #endif
494 
495 static int dvdsub_decode(AVCodecContext *avctx,
496  void *data, int *data_size,
497  AVPacket *avpkt)
498 {
499  DVDSubContext *ctx = avctx->priv_data;
500  const uint8_t *buf = avpkt->data;
501  int buf_size = avpkt->size;
502  AVSubtitle *sub = data;
503  int is_menu;
504 
505  is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
506 
507  if (is_menu < 0) {
508  no_subtitle:
509  *data_size = 0;
510 
511  return buf_size;
512  }
513  if (!is_menu && find_smallest_bounding_rectangle(sub) == 0)
514  goto no_subtitle;
515 
516 #if defined(DEBUG)
517  av_dlog(NULL, "start=%d ms end =%d ms\n",
518  sub->start_display_time,
519  sub->end_display_time);
520  ppm_save("/tmp/a.ppm", sub->rects[0]->pict.data[0],
521  sub->rects[0]->w, sub->rects[0]->h, sub->rects[0]->pict.data[1]);
522 #endif
523 
524  *data_size = 1;
525  return buf_size;
526 }
527 
528 static void parse_palette(DVDSubContext *ctx, char *p)
529 {
530  int i;
531 
532  ctx->has_palette = 1;
533  for(i=0;i<16;i++) {
534  ctx->palette[i] = strtoul(p, &p, 16);
535  while(*p == ',' || av_isspace(*p))
536  p++;
537  }
538 }
539 
541 {
542  DVDSubContext *ctx = (DVDSubContext*) avctx->priv_data;
543  char *dataorig, *data;
544 
545  if (!avctx->extradata || !avctx->extradata_size)
546  return 1;
547 
548  dataorig = data = av_malloc(avctx->extradata_size+1);
549  if (!data)
550  return AVERROR(ENOMEM);
551  memcpy(data, avctx->extradata, avctx->extradata_size);
552  data[avctx->extradata_size] = '\0';
553 
554  for(;;) {
555  int pos = strcspn(data, "\n\r");
556  if (pos==0 && *data==0)
557  break;
558 
559  if (strncmp("palette:", data, 8) == 0) {
560  parse_palette(ctx, data + 8);
561  } else if (strncmp("size:", data, 5) == 0) {
562  int w, h;
563  if (sscanf(data + 5, "%dx%d", &w, &h) == 2 &&
564  av_image_check_size(w, h, 0, avctx) >= 0)
565  avcodec_set_dimensions(avctx, w, h);
566  }
567 
568  data += pos;
569  data += strspn(data, "\n\r");
570  }
571 
572  av_free(dataorig);
573  return 1;
574 }
575 
576 static int dvdsub_init(AVCodecContext *avctx)
577 {
578  DVDSubContext *ctx = avctx->priv_data;
579  int ret;
580 
581  if ((ret = dvdsub_parse_extradata(avctx)) < 0)
582  return ret;
583 
584  if (ctx->palette_str)
585  parse_palette(ctx, ctx->palette_str);
586  if (ctx->has_palette) {
587  int i;
588  av_log(avctx, AV_LOG_DEBUG, "palette:");
589  for(i=0;i<16;i++)
590  av_log(avctx, AV_LOG_DEBUG, " 0x%06x", ctx->palette[i]);
591  av_log(avctx, AV_LOG_DEBUG, "\n");
592  }
593 
594  return 1;
595 }
596 
597 #define OFFSET(field) offsetof(DVDSubContext, field)
598 #define VD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
599 static const AVOption options[] = {
600  { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, VD },
601  { NULL }
602 };
603 static const AVClass class = {
604  .class_name = "dvdsubdec",
605  .item_name = av_default_item_name,
606  .option = options,
608 };
609 
611  .name = "dvdsub",
612  .type = AVMEDIA_TYPE_SUBTITLE,
614  .priv_data_size = sizeof(DVDSubContext),
615  .init = dvdsub_init,
617  .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
618  .priv_class = &class,
619 };