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 #include "libavutil/bswap.h"
32 
33 typedef struct DVDSubContext
34 {
35  AVClass *class;
36  uint32_t palette[16];
37  char *palette_str;
38  char *ifo_str;
41  uint8_t alpha[256];
42  uint8_t buf[0x10000];
43  int buf_size;
46 #ifdef DEBUG
47  int sub_id;
48 #endif
50 
51 static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
52 {
54  uint8_t r, g, b;
55  int i, y, cb, cr;
56  int r_add, g_add, b_add;
57 
58  for (i = num_values; i > 0; i--) {
59  y = *ycbcr++;
60  cr = *ycbcr++;
61  cb = *ycbcr++;
62  YUV_TO_RGB1_CCIR(cb, cr);
63  YUV_TO_RGB2_CCIR(r, g, b, y);
64  *rgba++ = ((unsigned)*alpha++ << 24) | (r << 16) | (g << 8) | b;
65  }
66 }
67 
68 static int decode_run_2bit(GetBitContext *gb, int *color)
69 {
70  unsigned int v, t;
71 
72  v = 0;
73  for (t = 1; v < t && t <= 0x40; t <<= 2)
74  v = (v << 4) | get_bits(gb, 4);
75  *color = v & 3;
76  if (v < 4) { /* Code for fill rest of line */
77  return INT_MAX;
78  }
79  return v >> 2;
80 }
81 
82 static int decode_run_8bit(GetBitContext *gb, int *color)
83 {
84  int len;
85  int has_run = get_bits1(gb);
86  if (get_bits1(gb))
87  *color = get_bits(gb, 8);
88  else
89  *color = get_bits(gb, 2);
90  if (has_run) {
91  if (get_bits1(gb)) {
92  len = get_bits(gb, 7);
93  if (len == 0)
94  len = INT_MAX;
95  else
96  len += 9;
97  } else
98  len = get_bits(gb, 3) + 2;
99  } else
100  len = 1;
101  return len;
102 }
103 
104 static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256],
105  const uint8_t *buf, int start, int buf_size, int is_8bit)
106 {
107  GetBitContext gb;
108  int bit_len;
109  int x, y, len, color;
110  uint8_t *d;
111 
112  if (start >= buf_size)
113  return -1;
114 
115  if (w <= 0 || h <= 0)
116  return -1;
117 
118  bit_len = (buf_size - start) * 8;
119  init_get_bits(&gb, buf + start, bit_len);
120 
121  x = 0;
122  y = 0;
123  d = bitmap;
124  for(;;) {
125  if (get_bits_count(&gb) > bit_len)
126  return -1;
127  if (is_8bit)
128  len = decode_run_8bit(&gb, &color);
129  else
130  len = decode_run_2bit(&gb, &color);
131  len = FFMIN(len, w - x);
132  memset(d + x, color, len);
133  used_color[color] = 1;
134  x += len;
135  if (x >= w) {
136  y++;
137  if (y >= h)
138  break;
139  d += linesize;
140  x = 0;
141  /* byte align */
142  align_get_bits(&gb);
143  }
144  }
145  return 0;
146 }
147 
149  uint32_t *rgba_palette,
150  uint32_t subtitle_color)
151 {
152  static const uint8_t level_map[4][4] = {
153  // this configuration (full range, lowest to highest) in tests
154  // seemed most common, so assume this
155  {0xff},
156  {0x00, 0xff},
157  {0x00, 0x80, 0xff},
158  {0x00, 0x55, 0xaa, 0xff},
159  };
160  uint8_t color_used[16] = { 0 };
161  int nb_opaque_colors, i, level, j, r, g, b;
162  uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
163 
164  if(ctx->has_palette) {
165  for(i = 0; i < 4; i++)
166  rgba_palette[i] = (ctx->palette[colormap[i]] & 0x00ffffff)
167  | ((alpha[i] * 17U) << 24);
168  return;
169  }
170 
171  for(i = 0; i < 4; i++)
172  rgba_palette[i] = 0;
173 
174  nb_opaque_colors = 0;
175  for(i = 0; i < 4; i++) {
176  if (alpha[i] != 0 && !color_used[colormap[i]]) {
177  color_used[colormap[i]] = 1;
178  nb_opaque_colors++;
179  }
180  }
181 
182  if (nb_opaque_colors == 0)
183  return;
184 
185  j = 0;
186  memset(color_used, 0, 16);
187  for(i = 0; i < 4; i++) {
188  if (alpha[i] != 0) {
189  if (!color_used[colormap[i]]) {
190  level = level_map[nb_opaque_colors - 1][j];
191  r = (((subtitle_color >> 16) & 0xff) * level) >> 8;
192  g = (((subtitle_color >> 8) & 0xff) * level) >> 8;
193  b = (((subtitle_color >> 0) & 0xff) * level) >> 8;
194  rgba_palette[i] = b | (g << 8) | (r << 16) | ((alpha[i] * 17U) << 24);
195  color_used[colormap[i]] = (i + 1);
196  j++;
197  } else {
198  rgba_palette[i] = (rgba_palette[color_used[colormap[i]] - 1] & 0x00ffffff) |
199  ((alpha[i] * 17U) << 24);
200  }
201  }
202  }
203 }
204 
205 static void reset_rects(AVSubtitle *sub_header)
206 {
207  int i;
208 
209  if (sub_header->rects) {
210  for (i = 0; i < sub_header->num_rects; i++) {
211  av_freep(&sub_header->rects[i]->data[0]);
212  av_freep(&sub_header->rects[i]->data[1]);
213  av_freep(&sub_header->rects[i]);
214  }
215  av_freep(&sub_header->rects);
216  sub_header->num_rects = 0;
217  }
218 }
219 
220 #define READ_OFFSET(a) (big_offsets ? AV_RB32(a) : AV_RB16(a))
221 
223  const uint8_t *buf, int buf_size)
224 {
225  int cmd_pos, pos, cmd, x1, y1, x2, y2, next_cmd_pos;
226  int big_offsets, offset_size, is_8bit = 0;
227  const uint8_t *yuv_palette = NULL;
228  uint8_t *colormap = ctx->colormap, *alpha = ctx->alpha;
229  int date;
230  int i;
231  int is_menu = 0;
232  uint32_t size;
233  int64_t offset1, offset2;
234 
235  if (buf_size < 10)
236  return -1;
237 
238  if (AV_RB16(buf) == 0) { /* HD subpicture with 4-byte offsets */
239  big_offsets = 1;
240  offset_size = 4;
241  cmd_pos = 6;
242  } else {
243  big_offsets = 0;
244  offset_size = 2;
245  cmd_pos = 2;
246  }
247 
248  size = READ_OFFSET(buf + (big_offsets ? 2 : 0));
249  cmd_pos = READ_OFFSET(buf + cmd_pos);
250 
251  if (cmd_pos < 0 || cmd_pos > buf_size - 2 - offset_size) {
252  if (cmd_pos > size) {
253  av_log(ctx, AV_LOG_ERROR, "Discarding invalid packet\n");
254  return 0;
255  }
256  return AVERROR(EAGAIN);
257  }
258 
259  while (cmd_pos > 0 && cmd_pos < buf_size - 2 - offset_size) {
260  date = AV_RB16(buf + cmd_pos);
261  next_cmd_pos = READ_OFFSET(buf + cmd_pos + 2);
262  ff_dlog(NULL, "cmd_pos=0x%04x next=0x%04x date=%d\n",
263  cmd_pos, next_cmd_pos, date);
264  pos = cmd_pos + 2 + offset_size;
265  offset1 = -1;
266  offset2 = -1;
267  x1 = y1 = x2 = y2 = 0;
268  while (pos < buf_size) {
269  cmd = buf[pos++];
270  ff_dlog(NULL, "cmd=%02x\n", cmd);
271  switch(cmd) {
272  case 0x00:
273  /* menu subpicture */
274  is_menu = 1;
275  break;
276  case 0x01:
277  /* set start date */
278  sub_header->start_display_time = (date << 10) / 90;
279  break;
280  case 0x02:
281  /* set end date */
282  sub_header->end_display_time = (date << 10) / 90;
283  break;
284  case 0x03:
285  /* set colormap */
286  if ((buf_size - pos) < 2)
287  goto fail;
288  colormap[3] = buf[pos] >> 4;
289  colormap[2] = buf[pos] & 0x0f;
290  colormap[1] = buf[pos + 1] >> 4;
291  colormap[0] = buf[pos + 1] & 0x0f;
292  pos += 2;
293  break;
294  case 0x04:
295  /* set alpha */
296  if ((buf_size - pos) < 2)
297  goto fail;
298  alpha[3] = buf[pos] >> 4;
299  alpha[2] = buf[pos] & 0x0f;
300  alpha[1] = buf[pos + 1] >> 4;
301  alpha[0] = buf[pos + 1] & 0x0f;
302  pos += 2;
303  ff_dlog(NULL, "alpha=%x%x%x%x\n", alpha[0],alpha[1],alpha[2],alpha[3]);
304  break;
305  case 0x05:
306  case 0x85:
307  if ((buf_size - pos) < 6)
308  goto fail;
309  x1 = (buf[pos] << 4) | (buf[pos + 1] >> 4);
310  x2 = ((buf[pos + 1] & 0x0f) << 8) | buf[pos + 2];
311  y1 = (buf[pos + 3] << 4) | (buf[pos + 4] >> 4);
312  y2 = ((buf[pos + 4] & 0x0f) << 8) | buf[pos + 5];
313  if (cmd & 0x80)
314  is_8bit = 1;
315  ff_dlog(NULL, "x1=%d x2=%d y1=%d y2=%d\n", x1, x2, y1, y2);
316  pos += 6;
317  break;
318  case 0x06:
319  if ((buf_size - pos) < 4)
320  goto fail;
321  offset1 = AV_RB16(buf + pos);
322  offset2 = AV_RB16(buf + pos + 2);
323  ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
324  pos += 4;
325  break;
326  case 0x86:
327  if ((buf_size - pos) < 8)
328  goto fail;
329  offset1 = AV_RB32(buf + pos);
330  offset2 = AV_RB32(buf + pos + 4);
331  ff_dlog(NULL, "offset1=0x%04"PRIx64" offset2=0x%04"PRIx64"\n", offset1, offset2);
332  pos += 8;
333  break;
334 
335  case 0x83:
336  /* HD set palette */
337  if ((buf_size - pos) < 768)
338  goto fail;
339  yuv_palette = buf + pos;
340  pos += 768;
341  break;
342  case 0x84:
343  /* HD set contrast (alpha) */
344  if ((buf_size - pos) < 256)
345  goto fail;
346  for (i = 0; i < 256; i++)
347  alpha[i] = 0xFF - buf[pos+i];
348  pos += 256;
349  break;
350 
351  case 0xff:
352  goto the_end;
353  default:
354  ff_dlog(NULL, "unrecognised subpicture command 0x%x\n", cmd);
355  goto the_end;
356  }
357  }
358  the_end:
359  if (offset1 >= buf_size || offset2 >= buf_size)
360  goto fail;
361 
362  if (offset1 >= 0 && offset2 >= 0) {
363  int w, h;
364  uint8_t *bitmap;
365 
366  /* decode the bitmap */
367  w = x2 - x1 + 1;
368  if (w < 0)
369  w = 0;
370  h = y2 - y1 + 1;
371  if (h < 0)
372  h = 0;
373  if (w > 0 && h > 1) {
374  reset_rects(sub_header);
375  memset(ctx->used_color, 0, sizeof(ctx->used_color));
376  sub_header->rects = av_mallocz(sizeof(*sub_header->rects));
377  if (!sub_header->rects)
378  goto fail;
379  sub_header->rects[0] = av_mallocz(sizeof(AVSubtitleRect));
380  if (!sub_header->rects[0])
381  goto fail;
382  sub_header->num_rects = 1;
383  bitmap = sub_header->rects[0]->data[0] = av_malloc(w * h);
384  if (!bitmap)
385  goto fail;
386  if (decode_rle(bitmap, w * 2, w, (h + 1) / 2, ctx->used_color,
387  buf, offset1, buf_size, is_8bit) < 0)
388  goto fail;
389  if (decode_rle(bitmap + w, w * 2, w, h / 2, ctx->used_color,
390  buf, offset2, buf_size, is_8bit) < 0)
391  goto fail;
392  sub_header->rects[0]->data[1] = av_mallocz(AVPALETTE_SIZE);
393  if (!sub_header->rects[0]->data[1])
394  goto fail;
395  if (is_8bit) {
396  if (!yuv_palette)
397  goto fail;
398  sub_header->rects[0]->nb_colors = 256;
399  yuv_a_to_rgba(yuv_palette, alpha,
400  (uint32_t *)sub_header->rects[0]->data[1],
401  256);
402  } else {
403  sub_header->rects[0]->nb_colors = 4;
404  guess_palette(ctx, (uint32_t*)sub_header->rects[0]->data[1],
405  0xffff00);
406  }
407  sub_header->rects[0]->x = x1;
408  sub_header->rects[0]->y = y1;
409  sub_header->rects[0]->w = w;
410  sub_header->rects[0]->h = h;
411  sub_header->rects[0]->type = SUBTITLE_BITMAP;
412  sub_header->rects[0]->linesize[0] = w;
413  sub_header->rects[0]->flags = is_menu ? AV_SUBTITLE_FLAG_FORCED : 0;
414 
415 #if FF_API_AVPICTURE
417  for (i = 0; i < 4; i++) {
418  sub_header->rects[0]->pict.data[i] = sub_header->rects[0]->data[i];
419  sub_header->rects[0]->pict.linesize[i] = sub_header->rects[0]->linesize[i];
420  }
422 #endif
423  }
424  }
425  if (next_cmd_pos < cmd_pos) {
426  av_log(ctx, AV_LOG_ERROR, "Invalid command offset\n");
427  break;
428  }
429  if (next_cmd_pos == cmd_pos)
430  break;
431  cmd_pos = next_cmd_pos;
432  }
433  if (sub_header->num_rects > 0)
434  return is_menu;
435  fail:
436  reset_rects(sub_header);
437  return -1;
438 }
439 
440 static int is_transp(const uint8_t *buf, int pitch, int n,
441  const uint8_t *transp_color)
442 {
443  int i;
444  for(i = 0; i < n; i++) {
445  if (!transp_color[*buf])
446  return 0;
447  buf += pitch;
448  }
449  return 1;
450 }
451 
452 /* return 0 if empty rectangle, 1 if non empty */
454 {
455  uint8_t transp_color[256] = { 0 };
456  int y1, y2, x1, x2, y, w, h, i;
457  uint8_t *bitmap;
458  int transparent = 1;
459 
460  if (s->num_rects == 0 || !s->rects || s->rects[0]->w <= 0 || s->rects[0]->h <= 0)
461  return 0;
462 
463  for(i = 0; i < s->rects[0]->nb_colors; i++) {
464  if ((((uint32_t *)s->rects[0]->data[1])[i] >> 24) == 0) {
465  transp_color[i] = 1;
466  } else if (ctx->used_color[i])
467  transparent = 0;
468  }
469  if (transparent)
470  return 0;
471  y1 = 0;
472  while (y1 < s->rects[0]->h && is_transp(s->rects[0]->data[0] + y1 * s->rects[0]->linesize[0],
473  1, s->rects[0]->w, transp_color))
474  y1++;
475  if (y1 == s->rects[0]->h) {
476  av_freep(&s->rects[0]->data[0]);
477  s->rects[0]->w = s->rects[0]->h = 0;
478  return 0;
479  }
480 
481  y2 = s->rects[0]->h - 1;
482  while (y2 > 0 && is_transp(s->rects[0]->data[0] + y2 * s->rects[0]->linesize[0], 1,
483  s->rects[0]->w, transp_color))
484  y2--;
485  x1 = 0;
486  while (x1 < (s->rects[0]->w - 1) && is_transp(s->rects[0]->data[0] + x1, s->rects[0]->linesize[0],
487  s->rects[0]->h, transp_color))
488  x1++;
489  x2 = s->rects[0]->w - 1;
490  while (x2 > 0 && is_transp(s->rects[0]->data[0] + x2, s->rects[0]->linesize[0], s->rects[0]->h,
491  transp_color))
492  x2--;
493  w = x2 - x1 + 1;
494  h = y2 - y1 + 1;
495  bitmap = av_malloc(w * h);
496  if (!bitmap)
497  return 1;
498  for(y = 0; y < h; y++) {
499  memcpy(bitmap + w * y, s->rects[0]->data[0] + x1 + (y1 + y) * s->rects[0]->linesize[0], w);
500  }
501  av_freep(&s->rects[0]->data[0]);
502  s->rects[0]->data[0] = bitmap;
503  s->rects[0]->linesize[0] = w;
504  s->rects[0]->w = w;
505  s->rects[0]->h = h;
506  s->rects[0]->x += x1;
507  s->rects[0]->y += y1;
508 
509 #if FF_API_AVPICTURE
511  for (i = 0; i < 4; i++) {
512  s->rects[0]->pict.data[i] = s->rects[0]->data[i];
513  s->rects[0]->pict.linesize[i] = s->rects[0]->linesize[i];
514  }
516 #endif
517 
518  return 1;
519 }
520 
521 #ifdef DEBUG
522 #define ALPHA_MIX(A,BACK,FORE) (((255-(A)) * (BACK) + (A) * (FORE)) / 255)
523 static void ppm_save(const char *filename, uint8_t *bitmap, int w, int h,
524  uint32_t *rgba_palette)
525 {
526  int x, y, alpha;
527  uint32_t v;
528  int back[3] = {0, 255, 0}; /* green background */
529  FILE *f;
530 
531  f = fopen(filename, "w");
532  if (!f) {
533  perror(filename);
534  return;
535  }
536  fprintf(f, "P6\n"
537  "%d %d\n"
538  "%d\n",
539  w, h, 255);
540  for(y = 0; y < h; y++) {
541  for(x = 0; x < w; x++) {
542  v = rgba_palette[bitmap[y * w + x]];
543  alpha = v >> 24;
544  putc(ALPHA_MIX(alpha, back[0], (v >> 16) & 0xff), f);
545  putc(ALPHA_MIX(alpha, back[1], (v >> 8) & 0xff), f);
546  putc(ALPHA_MIX(alpha, back[2], (v >> 0) & 0xff), f);
547  }
548  }
549  fclose(f);
550 }
551 #endif
552 
554  const uint8_t *buf, int buf_size)
555 {
556  DVDSubContext *ctx = avctx->priv_data;
557 
558  av_assert0(buf_size >= 0 && ctx->buf_size <= sizeof(ctx->buf));
559  if (buf_size >= sizeof(ctx->buf) - ctx->buf_size) {
560  av_log(avctx, AV_LOG_WARNING, "Attempt to reconstruct "
561  "too large SPU packets aborted.\n");
562  ctx->buf_size = 0;
563  return AVERROR_INVALIDDATA;
564  }
565  memcpy(ctx->buf + ctx->buf_size, buf, buf_size);
566  ctx->buf_size += buf_size;
567  return 0;
568 }
569 
570 static int dvdsub_decode(AVCodecContext *avctx,
571  void *data, int *data_size,
572  AVPacket *avpkt)
573 {
574  DVDSubContext *ctx = avctx->priv_data;
575  const uint8_t *buf = avpkt->data;
576  int buf_size = avpkt->size;
577  AVSubtitle *sub = data;
578  int appended = 0;
579  int is_menu;
580 
581  if (ctx->buf_size) {
582  int ret = append_to_cached_buf(avctx, buf, buf_size);
583  if (ret < 0) {
584  *data_size = 0;
585  return ret;
586  }
587  buf = ctx->buf;
588  buf_size = ctx->buf_size;
589  appended = 1;
590  }
591 
592  is_menu = decode_dvd_subtitles(ctx, sub, buf, buf_size);
593  if (is_menu == AVERROR(EAGAIN)) {
594  *data_size = 0;
595  return appended ? 0 : append_to_cached_buf(avctx, buf, buf_size);
596  }
597 
598  if (is_menu < 0) {
599  no_subtitle:
600  reset_rects(sub);
601  *data_size = 0;
602 
603  return buf_size;
604  }
605  if (!is_menu && find_smallest_bounding_rectangle(ctx, sub) == 0)
606  goto no_subtitle;
607 
608  if (ctx->forced_subs_only && !(sub->rects[0]->flags & AV_SUBTITLE_FLAG_FORCED))
609  goto no_subtitle;
610 
611 #if defined(DEBUG)
612  {
613  char ppm_name[32];
614 
615  snprintf(ppm_name, sizeof(ppm_name), "/tmp/%05d.ppm", ctx->sub_id++);
616  ff_dlog(NULL, "start=%d ms end =%d ms\n",
617  sub->start_display_time,
618  sub->end_display_time);
619  ppm_save(ppm_name, sub->rects[0]->data[0],
620  sub->rects[0]->w, sub->rects[0]->h, (uint32_t*) sub->rects[0]->data[1]);
621  }
622 #endif
623 
624  ctx->buf_size = 0;
625  *data_size = 1;
626  return buf_size;
627 }
628 
629 static void parse_palette(DVDSubContext *ctx, char *p)
630 {
631  int i;
632 
633  ctx->has_palette = 1;
634  for(i=0;i<16;i++) {
635  ctx->palette[i] = strtoul(p, &p, 16);
636  while(*p == ',' || av_isspace(*p))
637  p++;
638  }
639 }
640 
641 static int parse_ifo_palette(DVDSubContext *ctx, char *p)
642 {
643  FILE *ifo;
644  char ifostr[12];
645  uint32_t sp_pgci, pgci, off_pgc, pgc;
646  uint8_t r, g, b, yuv[65], *buf;
647  int i, y, cb, cr, r_add, g_add, b_add;
648  int ret = 0;
649  const uint8_t *cm = ff_crop_tab + MAX_NEG_CROP;
650 
651  ctx->has_palette = 0;
652  if ((ifo = fopen(p, "r")) == NULL) {
653  av_log(ctx, AV_LOG_WARNING, "Unable to open IFO file \"%s\": %s\n", p, av_err2str(AVERROR(errno)));
654  return AVERROR_EOF;
655  }
656  if (fread(ifostr, 12, 1, ifo) != 1 || memcmp(ifostr, "DVDVIDEO-VTS", 12)) {
657  av_log(ctx, AV_LOG_WARNING, "\"%s\" is not a proper IFO file\n", p);
658  ret = AVERROR_INVALIDDATA;
659  goto end;
660  }
661  if (fseek(ifo, 0xCC, SEEK_SET) == -1) {
662  ret = AVERROR(errno);
663  goto end;
664  }
665  if (fread(&sp_pgci, 4, 1, ifo) == 1) {
666  pgci = av_be2ne32(sp_pgci) * 2048;
667  if (fseek(ifo, pgci + 0x0C, SEEK_SET) == -1) {
668  ret = AVERROR(errno);
669  goto end;
670  }
671  if (fread(&off_pgc, 4, 1, ifo) == 1) {
672  pgc = pgci + av_be2ne32(off_pgc);
673  if (fseek(ifo, pgc + 0xA4, SEEK_SET) == -1) {
674  ret = AVERROR(errno);
675  goto end;
676  }
677  if (fread(yuv, 64, 1, ifo) == 1) {
678  buf = yuv;
679  for(i=0; i<16; i++) {
680  y = *++buf;
681  cr = *++buf;
682  cb = *++buf;
683  YUV_TO_RGB1_CCIR(cb, cr);
684  YUV_TO_RGB2_CCIR(r, g, b, y);
685  ctx->palette[i] = (r << 16) + (g << 8) + b;
686  buf++;
687  }
688  ctx->has_palette = 1;
689  }
690  }
691  }
692  if (ctx->has_palette == 0) {
693  av_log(ctx, AV_LOG_WARNING, "Failed to read palette from IFO file \"%s\"\n", p);
694  ret = AVERROR_INVALIDDATA;
695  }
696 end:
697  fclose(ifo);
698  return ret;
699 }
700 
702 {
704  char *dataorig, *data;
705  int ret = 1;
706 
707  if (!avctx->extradata || !avctx->extradata_size)
708  return 1;
709 
710  dataorig = data = av_malloc(avctx->extradata_size+1);
711  if (!data)
712  return AVERROR(ENOMEM);
713  memcpy(data, avctx->extradata, avctx->extradata_size);
714  data[avctx->extradata_size] = '\0';
715 
716  for(;;) {
717  int pos = strcspn(data, "\n\r");
718  if (pos==0 && *data==0)
719  break;
720 
721  if (strncmp("palette:", data, 8) == 0) {
722  parse_palette(ctx, data + 8);
723  } else if (strncmp("size:", data, 5) == 0) {
724  int w, h;
725  if (sscanf(data + 5, "%dx%d", &w, &h) == 2) {
726  ret = ff_set_dimensions(avctx, w, h);
727  if (ret < 0)
728  goto fail;
729  }
730  }
731 
732  data += pos;
733  data += strspn(data, "\n\r");
734  }
735 
736 fail:
737  av_free(dataorig);
738  return ret;
739 }
740 
742 {
743  DVDSubContext *ctx = avctx->priv_data;
744  int ret;
745 
746  if ((ret = dvdsub_parse_extradata(avctx)) < 0)
747  return ret;
748 
749  if (ctx->ifo_str)
750  parse_ifo_palette(ctx, ctx->ifo_str);
751  if (ctx->palette_str)
752  parse_palette(ctx, ctx->palette_str);
753  if (ctx->has_palette) {
754  int i;
755  av_log(avctx, AV_LOG_DEBUG, "palette:");
756  for(i=0;i<16;i++)
757  av_log(avctx, AV_LOG_DEBUG, " 0x%06"PRIx32, ctx->palette[i]);
758  av_log(avctx, AV_LOG_DEBUG, "\n");
759  }
760 
761  return 1;
762 }
763 
764 static void dvdsub_flush(AVCodecContext *avctx)
765 {
766  DVDSubContext *ctx = avctx->priv_data;
767  ctx->buf_size = 0;
768 }
769 
771 {
772  dvdsub_flush(avctx);
773  return 0;
774 }
775 
776 #define OFFSET(field) offsetof(DVDSubContext, field)
777 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
778 static const AVOption options[] = {
779  { "palette", "set the global palette", OFFSET(palette_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
780  { "ifo_palette", "obtain the global palette from .IFO file", OFFSET(ifo_str), AV_OPT_TYPE_STRING, { .str = NULL }, 0, 0, SD },
781  { "forced_subs_only", "Only show forced subtitles", OFFSET(forced_subs_only), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, SD},
782  { NULL }
783 };
784 static const AVClass dvdsub_class = {
785  .class_name = "dvdsubdec",
786  .item_name = av_default_item_name,
787  .option = options,
788  .version = LIBAVUTIL_VERSION_INT,
789 };
790 
792  .name = "dvdsub",
793  .long_name = NULL_IF_CONFIG_SMALL("DVD subtitles"),
794  .type = AVMEDIA_TYPE_SUBTITLE,
796  .priv_data_size = sizeof(DVDSubContext),
797  .init = dvdsub_init,
799  .flush = dvdsub_flush,
800  .close = dvdsub_close,
801  .priv_class = &dvdsub_class,
802 };
uint8_t colormap[4]
Definition: dvdsubdec.c:40
#define NULL
Definition: coverity.c:32
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static void dvdsub_flush(AVCodecContext *avctx)
Definition: dvdsubdec.c:764
static float alpha(float a)
int x
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:3830
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
static void flush(AVCodecContext *avctx)
misc image utilities
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:269
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:182
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
uint8_t alpha[256]
Definition: dvdsubdec.c:41
int ff_set_dimensions(AVCodecContext *s, int width, int height)
Check that the provided frame dimensions are valid and set them on the codec context.
Definition: utils.c:104
const char * g
Definition: vf_curves.c:112
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int nb_colors
number of colors in pict, undefined when pict is not set
Definition: avcodec.h:3834
int size
Definition: avcodec.h:1435
const char * b
Definition: vf_curves.c:113
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:191
static av_const int av_isspace(int c)
Locale-independent conversion of ASCII isspace.
Definition: avstring.h:222
#define MAX_NEG_CROP
Definition: mathops.h:31
unsigned num_rects
Definition: avcodec.h:3868
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:236
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:87
#define av_be2ne32(x)
Definition: bswap.h:93
uint32_t palette[16]
Definition: dvdsubdec.c:36
AVCodec.
Definition: avcodec.h:3412
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:42
static av_cold int dvdsub_close(AVCodecContext *avctx)
Definition: dvdsubdec.c:770
attribute_deprecated AVPicture pict
Definition: avcodec.h:3841
Macro definitions for various function/variable attributes.
AVSubtitleRect ** rects
Definition: avcodec.h:3869
int w
width of pict, undefined when pict is not set
Definition: avcodec.h:3832
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:72
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
static double cb(void *priv, double x, double y)
Definition: vf_geq.c:112
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
AVOptions.
static const uint32_t color[16+AV_CLASS_CATEGORY_NB]
Definition: log.c:92
#define f(width, name)
Definition: cbs_vp9.c:255
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
attribute_deprecated int linesize[AV_NUM_DATA_POINTERS]
number of bytes per line
Definition: avcodec.h:3801
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1622
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:87
char * ifo_str
Definition: dvdsubdec.c:38
static void reset_rects(AVSubtitle *sub_header)
Definition: dvdsubdec.c:205
uint8_t * data
Definition: avcodec.h:1434
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:200
#define ff_dlog(a,...)
#define READ_OFFSET(a)
Definition: dvdsubdec.c:220
#define AVERROR_EOF
End of file.
Definition: error.h:55
bitstream reader API header.
ptrdiff_t size
Definition: opengl_enc.c:101
int h
height of pict, undefined when pict is not set
Definition: avcodec.h:3833
static av_cold int dvdsub_init(AVCodecContext *avctx)
Definition: dvdsubdec.c:741
#define av_log(a,...)
#define cm
Definition: dvbsubdec.c:37
#define SD
Definition: dvdsubdec.c:777
#define U(x)
Definition: vp56_arith.h:37
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
Various defines for YUV<->RGB conversion.
int y
top left corner of pict, undefined when pict is not set
Definition: avcodec.h:3831
#define AVERROR(e)
Definition: error.h:43
uint8_t used_color[256]
Definition: dvdsubdec.c:45
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:186
const char * r
Definition: vf_curves.c:111
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
static int find_smallest_bounding_rectangle(DVDSubContext *ctx, AVSubtitle *s)
Definition: dvdsubdec.c:453
const char * name
Name of the codec implementation.
Definition: avcodec.h:3419
static int append_to_cached_buf(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: dvdsubdec.c:553
#define fail()
Definition: checkasm.h:117
#define OFFSET(field)
Definition: dvdsubdec.c:776
uint32_t end_display_time
Definition: avcodec.h:3867
uint8_t buf[0x10000]
Definition: dvdsubdec.c:42
static int decode_run_8bit(GetBitContext *gb, int *color)
Definition: dvdsubdec.c:82
A bitmap, pict will be set.
Definition: avcodec.h:3812
#define AV_SUBTITLE_FLAG_FORCED
Definition: avcodec.h:3827
int linesize[4]
Definition: avcodec.h:3848
static const AVClass dvdsub_class
Definition: dvdsubdec.c:784
#define FFMIN(a, b)
Definition: common.h:96
static int decode_rle(uint8_t *bitmap, int linesize, int w, int h, uint8_t used_color[256], const uint8_t *buf, int start, int buf_size, int is_8bit)
Definition: dvdsubdec.c:104
uint8_t w
Definition: llviddspenc.c:38
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
Definition: colorspace.h:55
static void yuv_a_to_rgba(const uint8_t *ycbcr, const uint8_t *alpha, uint32_t *rgba, int num_values)
Definition: dvdsubdec.c:51
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
static int parse_ifo_palette(DVDSubContext *ctx, char *p)
Definition: dvdsubdec.c:641
static int is_transp(const uint8_t *buf, int pitch, int n, const uint8_t *transp_color)
Definition: dvdsubdec.c:440
attribute_deprecated uint8_t * data[AV_NUM_DATA_POINTERS]
pointers to the image data planes
Definition: avcodec.h:3799
#define s(width, name)
Definition: cbs_vp9.c:257
int n
Definition: avisynth_c.h:684
int has_palette
Definition: dvdsubdec.c:39
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
Definition: avcodec.h:3847
AVCodec ff_dvdsub_decoder
Definition: dvdsubdec.c:791
#define YUV_TO_RGB1_CCIR(cb1, cr1)
Definition: colorspace.h:34
static void parse_palette(DVDSubContext *ctx, char *p)
Definition: dvdsubdec.c:629
Libavcodec external API header.
main external API structure.
Definition: avcodec.h:1522
static const AVOption options[]
Definition: dvdsubdec.c:778
static int decode_dvd_subtitles(DVDSubContext *ctx, AVSubtitle *sub_header, const uint8_t *buf, int buf_size)
Definition: dvdsubdec.c:222
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1623
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:321
Describe the class of an AVClass context structure.
Definition: log.h:67
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:433
byte swapping routines
static int decode_run_2bit(GetBitContext *gb, int *color)
Definition: dvdsubdec.c:68
#define snprintf
Definition: snprintf.h:34
uint8_t level
Definition: svq3.c:207
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
common internal api header.
if(ret< 0)
Definition: vf_mcdeint.c:279
static int dvdsub_parse_extradata(AVCodecContext *avctx)
Definition: dvdsubdec.c:701
uint32_t start_display_time
Definition: avcodec.h:3866
static void guess_palette(DVDSubContext *ctx, uint32_t *rgba_palette, uint32_t subtitle_color)
Definition: dvdsubdec.c:148
#define ff_crop_tab
void * priv_data
Definition: avcodec.h:1549
#define av_free(p)
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:85
int len
static int dvdsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: dvdsubdec.c:570
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:472
#define av_freep(p)
void INT64 start
Definition: avisynth_c.h:690
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:113
enum AVSubtitleType type
Definition: avcodec.h:3850
This structure stores compressed data.
Definition: avcodec.h:1411
char * palette_str
Definition: dvdsubdec.c:37
int forced_subs_only
Definition: dvdsubdec.c:44