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