FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
dvbsubdec.c
Go to the documentation of this file.
1 /*
2  * DVB subtitle decoding
3  * Copyright (c) 2005 Ian Caulfield
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 "bytestream.h"
25 #include "internal.h"
26 #include "libavutil/colorspace.h"
27 #include "libavutil/imgutils.h"
28 #include "libavutil/opt.h"
29 
30 #define DVBSUB_PAGE_SEGMENT 0x10
31 #define DVBSUB_REGION_SEGMENT 0x11
32 #define DVBSUB_CLUT_SEGMENT 0x12
33 #define DVBSUB_OBJECT_SEGMENT 0x13
34 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
35 #define DVBSUB_DISPLAY_SEGMENT 0x80
36 
37 #define cm (ff_crop_tab + MAX_NEG_CROP)
38 
39 #ifdef DEBUG
40 static void png_save(const char *filename, uint32_t *bitmap, int w, int h)
41 {
42  int x, y, v;
43  FILE *f;
44  char fname[40], fname2[40];
45  char command[1024];
46 
47  snprintf(fname, sizeof(fname), "%s.ppm", filename);
48 
49  f = fopen(fname, "w");
50  if (!f) {
51  perror(fname);
52  return;
53  }
54  fprintf(f, "P6\n"
55  "%d %d\n"
56  "%d\n",
57  w, h, 255);
58  for(y = 0; y < h; y++) {
59  for(x = 0; x < w; x++) {
60  v = bitmap[y * w + x];
61  putc((v >> 16) & 0xff, f);
62  putc((v >> 8) & 0xff, f);
63  putc((v >> 0) & 0xff, f);
64  }
65  }
66  fclose(f);
67 
68 
69  snprintf(fname2, sizeof(fname2), "%s-a.pgm", filename);
70 
71  f = fopen(fname2, "w");
72  if (!f) {
73  perror(fname2);
74  return;
75  }
76  fprintf(f, "P5\n"
77  "%d %d\n"
78  "%d\n",
79  w, h, 255);
80  for(y = 0; y < h; y++) {
81  for(x = 0; x < w; x++) {
82  v = bitmap[y * w + x];
83  putc((v >> 24) & 0xff, f);
84  }
85  }
86  fclose(f);
87 
88  snprintf(command, sizeof(command), "pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
89  system(command);
90 
91  snprintf(command, sizeof(command), "rm %s %s", fname, fname2);
92  system(command);
93 }
94 #endif
95 
96 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
97 
98 typedef struct DVBSubCLUT {
99  int id;
100  int version;
101 
102  uint32_t clut4[4];
103  uint32_t clut16[16];
104  uint32_t clut256[256];
105 
106  struct DVBSubCLUT *next;
107 } DVBSubCLUT;
108 
110 
111 typedef struct DVBSubObjectDisplay {
114 
115  int x_pos;
116  int y_pos;
117 
118  int fgcolor;
119  int bgcolor;
120 
124 
125 typedef struct DVBSubObject {
126  int id;
127  int version;
128 
129  int type;
130 
132 
134 } DVBSubObject;
135 
136 typedef struct DVBSubRegionDisplay {
138 
139  int x_pos;
140  int y_pos;
141 
144 
145 typedef struct DVBSubRegion {
146  int id;
147  int version;
148 
149  int width;
150  int height;
151  int depth;
152 
153  int clut;
154  int bgcolor;
155 
157  int buf_size;
158  int dirty;
159 
161 
163 } DVBSubRegion;
164 
165 typedef struct DVBSubDisplayDefinition {
166  int version;
167 
168  int x;
169  int y;
170  int width;
171  int height;
173 
174 typedef struct DVBSubContext {
175  AVClass *class;
178 
179  int version;
180  int time_out;
181  int compute_edt; /**< if 1 end display time calculated using pts
182  if 0 (Default) calculated using time out */
185  int64_t prev_start;
189 
192 } DVBSubContext;
193 
194 
195 static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
196 {
197  DVBSubObject *ptr = ctx->object_list;
198 
199  while (ptr && ptr->id != object_id) {
200  ptr = ptr->next;
201  }
202 
203  return ptr;
204 }
205 
206 static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
207 {
208  DVBSubCLUT *ptr = ctx->clut_list;
209 
210  while (ptr && ptr->id != clut_id) {
211  ptr = ptr->next;
212  }
213 
214  return ptr;
215 }
216 
217 static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
218 {
219  DVBSubRegion *ptr = ctx->region_list;
220 
221  while (ptr && ptr->id != region_id) {
222  ptr = ptr->next;
223  }
224 
225  return ptr;
226 }
227 
229 {
230  DVBSubObject *object, *obj2, **obj2_ptr;
231  DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
232 
233  while (region->display_list) {
234  display = region->display_list;
235 
236  object = get_object(ctx, display->object_id);
237 
238  if (object) {
239  obj_disp_ptr = &object->display_list;
240  obj_disp = *obj_disp_ptr;
241 
242  while (obj_disp && obj_disp != display) {
243  obj_disp_ptr = &obj_disp->object_list_next;
244  obj_disp = *obj_disp_ptr;
245  }
246 
247  if (obj_disp) {
248  *obj_disp_ptr = obj_disp->object_list_next;
249 
250  if (!object->display_list) {
251  obj2_ptr = &ctx->object_list;
252  obj2 = *obj2_ptr;
253 
254  while (obj2 != object) {
255  av_assert0(obj2);
256  obj2_ptr = &obj2->next;
257  obj2 = *obj2_ptr;
258  }
259 
260  *obj2_ptr = obj2->next;
261 
262  av_freep(&obj2);
263  }
264  }
265  }
266 
267  region->display_list = display->region_list_next;
268 
269  av_freep(&display);
270  }
271 
272 }
273 
275 {
276  while (ctx->clut_list) {
277  DVBSubCLUT *clut = ctx->clut_list;
278 
279  ctx->clut_list = clut->next;
280 
281  av_freep(&clut);
282  }
283 }
284 
286 {
287  while (ctx->object_list) {
288  DVBSubObject *object = ctx->object_list;
289 
290  ctx->object_list = object->next;
291 
292  av_freep(&object);
293  }
294 }
295 
297 {
298  while (ctx->region_list) {
299  DVBSubRegion *region = ctx->region_list;
300 
301  ctx->region_list = region->next;
302 
303  delete_region_display_list(ctx, region);
304 
305  av_freep(&region->pbuf);
306  av_freep(&region);
307  }
308 }
309 
311 {
312  int i, r, g, b, a = 0;
313  DVBSubContext *ctx = avctx->priv_data;
314 
315  if (ctx->substream < 0) {
316  ctx->composition_id = -1;
317  ctx->ancillary_id = -1;
318  } else if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
319  av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
320  ctx->composition_id = -1;
321  ctx->ancillary_id = -1;
322  } else {
323  if (avctx->extradata_size > 5*ctx->substream + 2) {
324  ctx->composition_id = AV_RB16(avctx->extradata + 5*ctx->substream);
325  ctx->ancillary_id = AV_RB16(avctx->extradata + 5*ctx->substream + 2);
326  } else {
327  av_log(avctx, AV_LOG_WARNING, "Selected DVB subtitles sub-stream %d is not available\n", ctx->substream);
328  ctx->composition_id = AV_RB16(avctx->extradata);
329  ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
330  }
331  }
332 
333  ctx->version = -1;
334  ctx->prev_start = AV_NOPTS_VALUE;
335 
336  default_clut.id = -1;
337  default_clut.next = NULL;
338 
339  default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
340  default_clut.clut4[1] = RGBA(255, 255, 255, 255);
341  default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
342  default_clut.clut4[3] = RGBA(127, 127, 127, 255);
343 
344  default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
345  for (i = 1; i < 16; i++) {
346  if (i < 8) {
347  r = (i & 1) ? 255 : 0;
348  g = (i & 2) ? 255 : 0;
349  b = (i & 4) ? 255 : 0;
350  } else {
351  r = (i & 1) ? 127 : 0;
352  g = (i & 2) ? 127 : 0;
353  b = (i & 4) ? 127 : 0;
354  }
355  default_clut.clut16[i] = RGBA(r, g, b, 255);
356  }
357 
358  default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
359  for (i = 1; i < 256; i++) {
360  if (i < 8) {
361  r = (i & 1) ? 255 : 0;
362  g = (i & 2) ? 255 : 0;
363  b = (i & 4) ? 255 : 0;
364  a = 63;
365  } else {
366  switch (i & 0x88) {
367  case 0x00:
368  r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
369  g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
370  b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
371  a = 255;
372  break;
373  case 0x08:
374  r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
375  g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
376  b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
377  a = 127;
378  break;
379  case 0x80:
380  r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
381  g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
382  b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
383  a = 255;
384  break;
385  case 0x88:
386  r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
387  g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
388  b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
389  a = 255;
390  break;
391  }
392  }
393  default_clut.clut256[i] = RGBA(r, g, b, a);
394  }
395 
396  return 0;
397 }
398 
400 {
401  DVBSubContext *ctx = avctx->priv_data;
402  DVBSubRegionDisplay *display;
403 
404  delete_regions(ctx);
405 
406  delete_objects(ctx);
407 
408  delete_cluts(ctx);
409 
411 
412  while (ctx->display_list) {
413  display = ctx->display_list;
414  ctx->display_list = display->next;
415 
416  av_freep(&display);
417  }
418 
419  return 0;
420 }
421 
423  uint8_t *destbuf, int dbuf_len,
424  const uint8_t **srcbuf, int buf_size,
425  int non_mod, uint8_t *map_table, int x_pos)
426 {
427  GetBitContext gb;
428 
429  int bits;
430  int run_length;
431  int pixels_read = x_pos;
432 
433  init_get_bits(&gb, *srcbuf, buf_size << 3);
434 
435  destbuf += x_pos;
436 
437  while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
438  bits = get_bits(&gb, 2);
439 
440  if (bits) {
441  if (non_mod != 1 || bits != 1) {
442  if (map_table)
443  *destbuf++ = map_table[bits];
444  else
445  *destbuf++ = bits;
446  }
447  pixels_read++;
448  } else {
449  bits = get_bits1(&gb);
450  if (bits == 1) {
451  run_length = get_bits(&gb, 3) + 3;
452  bits = get_bits(&gb, 2);
453 
454  if (non_mod == 1 && bits == 1)
455  pixels_read += run_length;
456  else {
457  if (map_table)
458  bits = map_table[bits];
459  while (run_length-- > 0 && pixels_read < dbuf_len) {
460  *destbuf++ = bits;
461  pixels_read++;
462  }
463  }
464  } else {
465  bits = get_bits1(&gb);
466  if (bits == 0) {
467  bits = get_bits(&gb, 2);
468  if (bits == 2) {
469  run_length = get_bits(&gb, 4) + 12;
470  bits = get_bits(&gb, 2);
471 
472  if (non_mod == 1 && bits == 1)
473  pixels_read += run_length;
474  else {
475  if (map_table)
476  bits = map_table[bits];
477  while (run_length-- > 0 && pixels_read < dbuf_len) {
478  *destbuf++ = bits;
479  pixels_read++;
480  }
481  }
482  } else if (bits == 3) {
483  run_length = get_bits(&gb, 8) + 29;
484  bits = get_bits(&gb, 2);
485 
486  if (non_mod == 1 && bits == 1)
487  pixels_read += run_length;
488  else {
489  if (map_table)
490  bits = map_table[bits];
491  while (run_length-- > 0 && pixels_read < dbuf_len) {
492  *destbuf++ = bits;
493  pixels_read++;
494  }
495  }
496  } else if (bits == 1) {
497  if (map_table)
498  bits = map_table[0];
499  else
500  bits = 0;
501  run_length = 2;
502  while (run_length-- > 0 && pixels_read < dbuf_len) {
503  *destbuf++ = bits;
504  pixels_read++;
505  }
506  } else {
507  (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
508  return pixels_read;
509  }
510  } else {
511  if (map_table)
512  bits = map_table[0];
513  else
514  bits = 0;
515  *destbuf++ = bits;
516  pixels_read++;
517  }
518  }
519  }
520  }
521 
522  if (get_bits(&gb, 6))
523  av_log(avctx, AV_LOG_ERROR, "line overflow\n");
524 
525  (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
526 
527  return pixels_read;
528 }
529 
530 static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len,
531  const uint8_t **srcbuf, int buf_size,
532  int non_mod, uint8_t *map_table, int x_pos)
533 {
534  GetBitContext gb;
535 
536  int bits;
537  int run_length;
538  int pixels_read = x_pos;
539 
540  init_get_bits(&gb, *srcbuf, buf_size << 3);
541 
542  destbuf += x_pos;
543 
544  while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
545  bits = get_bits(&gb, 4);
546 
547  if (bits) {
548  if (non_mod != 1 || bits != 1) {
549  if (map_table)
550  *destbuf++ = map_table[bits];
551  else
552  *destbuf++ = bits;
553  }
554  pixels_read++;
555  } else {
556  bits = get_bits1(&gb);
557  if (bits == 0) {
558  run_length = get_bits(&gb, 3);
559 
560  if (run_length == 0) {
561  (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
562  return pixels_read;
563  }
564 
565  run_length += 2;
566 
567  if (map_table)
568  bits = map_table[0];
569  else
570  bits = 0;
571 
572  while (run_length-- > 0 && pixels_read < dbuf_len) {
573  *destbuf++ = bits;
574  pixels_read++;
575  }
576  } else {
577  bits = get_bits1(&gb);
578  if (bits == 0) {
579  run_length = get_bits(&gb, 2) + 4;
580  bits = get_bits(&gb, 4);
581 
582  if (non_mod == 1 && bits == 1)
583  pixels_read += run_length;
584  else {
585  if (map_table)
586  bits = map_table[bits];
587  while (run_length-- > 0 && pixels_read < dbuf_len) {
588  *destbuf++ = bits;
589  pixels_read++;
590  }
591  }
592  } else {
593  bits = get_bits(&gb, 2);
594  if (bits == 2) {
595  run_length = get_bits(&gb, 4) + 9;
596  bits = get_bits(&gb, 4);
597 
598  if (non_mod == 1 && bits == 1)
599  pixels_read += run_length;
600  else {
601  if (map_table)
602  bits = map_table[bits];
603  while (run_length-- > 0 && pixels_read < dbuf_len) {
604  *destbuf++ = bits;
605  pixels_read++;
606  }
607  }
608  } else if (bits == 3) {
609  run_length = get_bits(&gb, 8) + 25;
610  bits = get_bits(&gb, 4);
611 
612  if (non_mod == 1 && bits == 1)
613  pixels_read += run_length;
614  else {
615  if (map_table)
616  bits = map_table[bits];
617  while (run_length-- > 0 && pixels_read < dbuf_len) {
618  *destbuf++ = bits;
619  pixels_read++;
620  }
621  }
622  } else if (bits == 1) {
623  if (map_table)
624  bits = map_table[0];
625  else
626  bits = 0;
627  run_length = 2;
628  while (run_length-- > 0 && pixels_read < dbuf_len) {
629  *destbuf++ = bits;
630  pixels_read++;
631  }
632  } else {
633  if (map_table)
634  bits = map_table[0];
635  else
636  bits = 0;
637  *destbuf++ = bits;
638  pixels_read ++;
639  }
640  }
641  }
642  }
643  }
644 
645  if (get_bits(&gb, 8))
646  av_log(avctx, AV_LOG_ERROR, "line overflow\n");
647 
648  (*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
649 
650  return pixels_read;
651 }
652 
654  uint8_t *destbuf, int dbuf_len,
655  const uint8_t **srcbuf, int buf_size,
656  int non_mod, uint8_t *map_table, int x_pos)
657 {
658  const uint8_t *sbuf_end = (*srcbuf) + buf_size;
659  int bits;
660  int run_length;
661  int pixels_read = x_pos;
662 
663  destbuf += x_pos;
664 
665  while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
666  bits = *(*srcbuf)++;
667 
668  if (bits) {
669  if (non_mod != 1 || bits != 1) {
670  if (map_table)
671  *destbuf++ = map_table[bits];
672  else
673  *destbuf++ = bits;
674  }
675  pixels_read++;
676  } else {
677  bits = *(*srcbuf)++;
678  run_length = bits & 0x7f;
679  if ((bits & 0x80) == 0) {
680  if (run_length == 0) {
681  return pixels_read;
682  }
683 
684  bits = 0;
685  } else {
686  bits = *(*srcbuf)++;
687  }
688  if (non_mod == 1 && bits == 1)
689  pixels_read += run_length;
690  else {
691  if (map_table)
692  bits = map_table[bits];
693  while (run_length-- > 0 && pixels_read < dbuf_len) {
694  *destbuf++ = bits;
695  pixels_read++;
696  }
697  }
698  }
699  }
700 
701  if (*(*srcbuf)++)
702  av_log(avctx, AV_LOG_ERROR, "line overflow\n");
703 
704  return pixels_read;
705 }
706 
707 static void compute_default_clut(AVSubtitleRect *rect, int w, int h)
708 {
709  uint8_t list[256] = {0};
710  uint8_t list_inv[256];
711  int counttab[256] = {0};
712  int count, i, x, y;
713 
714 #define V(x,y) rect->data[0][(x) + (y)*rect->linesize[0]]
715  for (y = 0; y<h; y++) {
716  for (x = 0; x<w; x++) {
717  int v = V(x,y) + 1;
718  int vl = x ? V(x-1,y) + 1 : 0;
719  int vr = x+1<w ? V(x+1,y) + 1 : 0;
720  int vt = y ? V(x,y-1) + 1 : 0;
721  int vb = y+1<h ? V(x,y+1) + 1 : 0;
722  counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
723  }
724  }
725 #define L(x,y) list[ rect->data[0][(x) + (y)*rect->linesize[0]] ]
726 
727  for (i = 0; i<256; i++) {
728  int scoretab[256] = {0};
729  int bestscore = 0;
730  int bestv = 0;
731  for (y = 0; y<h; y++) {
732  for (x = 0; x<w; x++) {
733  int v = rect->data[0][x + y*rect->linesize[0]];
734  int l_m = list[v];
735  int l_l = x ? L(x-1, y) : 1;
736  int l_r = x+1<w ? L(x+1, y) : 1;
737  int l_t = y ? L(x, y-1) : 1;
738  int l_b = y+1<h ? L(x, y+1) : 1;
739  int score;
740  if (l_m)
741  continue;
742  scoretab[v] += l_l + l_r + l_t + l_b;
743  score = 1024LL*scoretab[v] / counttab[v];
744  if (score > bestscore) {
745  bestscore = score;
746  bestv = v;
747  }
748  }
749  }
750  if (!bestscore)
751  break;
752  list [ bestv ] = 1;
753  list_inv[ i ] = bestv;
754  }
755 
756  count = FFMAX(i - 1, 1);
757  for (i--; i>=0; i--) {
758  int v = i*255/count;
759  AV_WN32(rect->data[1] + 4*list_inv[i], RGBA(v/2,v,v/2,v));
760  }
761 }
762 
763 
764 static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
765 {
766  DVBSubContext *ctx = avctx->priv_data;
767  DVBSubRegionDisplay *display;
768  DVBSubDisplayDefinition *display_def = ctx->display_definition;
769  DVBSubRegion *region;
771  DVBSubCLUT *clut;
772  uint32_t *clut_table;
773  int i;
774  int offset_x=0, offset_y=0;
775  int ret = 0;
776 
777 
778  if (display_def) {
779  offset_x = display_def->x;
780  offset_y = display_def->y;
781  }
782 
783  /* Not touching AVSubtitles again*/
784  if(sub->num_rects) {
785  avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
786  return AVERROR_PATCHWELCOME;
787  }
788  for (display = ctx->display_list; display; display = display->next) {
789  region = get_region(ctx, display->region_id);
790  if (region && region->dirty)
791  sub->num_rects++;
792  }
793 
794  if(ctx->compute_edt == 0) {
795  sub->end_display_time = ctx->time_out * 1000;
796  *got_output = 1;
797  } else if (ctx->prev_start != AV_NOPTS_VALUE) {
798  sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
799  *got_output = 1;
800  }
801  if (sub->num_rects > 0) {
802 
803  sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
804  if (!sub->rects) {
805  ret = AVERROR(ENOMEM);
806  goto fail;
807  }
808 
809  for(i=0; i<sub->num_rects; i++)
810  sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
811 
812  i = 0;
813 
814  for (display = ctx->display_list; display; display = display->next) {
815  region = get_region(ctx, display->region_id);
816 
817  if (!region)
818  continue;
819 
820  if (!region->dirty)
821  continue;
822 
823  rect = sub->rects[i];
824  rect->x = display->x_pos + offset_x;
825  rect->y = display->y_pos + offset_y;
826  rect->w = region->width;
827  rect->h = region->height;
828  rect->nb_colors = (1 << region->depth);
829  rect->type = SUBTITLE_BITMAP;
830  rect->linesize[0] = region->width;
831 
832  clut = get_clut(ctx, region->clut);
833 
834  if (!clut)
835  clut = &default_clut;
836 
837  switch (region->depth) {
838  case 2:
839  clut_table = clut->clut4;
840  break;
841  case 8:
842  clut_table = clut->clut256;
843  break;
844  case 4:
845  default:
846  clut_table = clut->clut16;
847  break;
848  }
849 
850  rect->data[1] = av_mallocz(AVPALETTE_SIZE);
851  if (!rect->data[1]) {
852  ret = AVERROR(ENOMEM);
853  goto fail;
854  }
855  memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
856 
857  rect->data[0] = av_malloc(region->buf_size);
858  if (!rect->data[0]) {
859  ret = AVERROR(ENOMEM);
860  goto fail;
861  }
862 
863  memcpy(rect->data[0], region->pbuf, region->buf_size);
864 
865  if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1)
867 
868 #if FF_API_AVPICTURE
870 {
871  int j;
872  for (j = 0; j < 4; j++) {
873  rect->pict.data[j] = rect->data[j];
874  rect->pict.linesize[j] = rect->linesize[j];
875  }
876 }
878 #endif
879 
880  i++;
881  }
882  }
883 
884  return 0;
885 fail:
886  if (sub->rects) {
887  for(i=0; i<sub->num_rects; i++) {
888  rect = sub->rects[i];
889  if (rect) {
890  av_freep(&rect->data[0]);
891  av_freep(&rect->data[1]);
892  }
893  av_freep(&sub->rects[i]);
894  }
895  av_freep(&sub->rects);
896  }
897  sub->num_rects = 0;
898  return ret;
899 }
900 
902  const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
903 {
904  DVBSubContext *ctx = avctx->priv_data;
905 
906  DVBSubRegion *region = get_region(ctx, display->region_id);
907  const uint8_t *buf_end = buf + buf_size;
908  uint8_t *pbuf;
909  int x_pos, y_pos;
910  int i;
911 
912  uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
913  uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
914  uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
915  0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
916  uint8_t *map_table;
917 
918 #if 0
919  ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
920  top_bottom ? "bottom" : "top");
921 
922  for (i = 0; i < buf_size; i++) {
923  if (i % 16 == 0)
924  ff_dlog(avctx, "0x%8p: ", buf+i);
925 
926  ff_dlog(avctx, "%02x ", buf[i]);
927  if (i % 16 == 15)
928  ff_dlog(avctx, "\n");
929  }
930 
931  if (i % 16)
932  ff_dlog(avctx, "\n");
933 #endif
934 
935  if (!region)
936  return;
937 
938  pbuf = region->pbuf;
939  region->dirty = 1;
940 
941  x_pos = display->x_pos;
942  y_pos = display->y_pos;
943 
944  y_pos += top_bottom;
945 
946  while (buf < buf_end) {
947  if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
948  av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
949  return;
950  }
951 
952  switch (*buf++) {
953  case 0x10:
954  if (region->depth == 8)
955  map_table = map2to8;
956  else if (region->depth == 4)
957  map_table = map2to4;
958  else
959  map_table = NULL;
960 
961  x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
962  region->width, &buf, buf_end - buf,
963  non_mod, map_table, x_pos);
964  break;
965  case 0x11:
966  if (region->depth < 4) {
967  av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
968  return;
969  }
970 
971  if (region->depth == 8)
972  map_table = map4to8;
973  else
974  map_table = NULL;
975 
976  x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
977  region->width, &buf, buf_end - buf,
978  non_mod, map_table, x_pos);
979  break;
980  case 0x12:
981  if (region->depth < 8) {
982  av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
983  return;
984  }
985 
986  x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
987  region->width, &buf, buf_end - buf,
988  non_mod, NULL, x_pos);
989  break;
990 
991  case 0x20:
992  map2to4[0] = (*buf) >> 4;
993  map2to4[1] = (*buf++) & 0xf;
994  map2to4[2] = (*buf) >> 4;
995  map2to4[3] = (*buf++) & 0xf;
996  break;
997  case 0x21:
998  for (i = 0; i < 4; i++)
999  map2to8[i] = *buf++;
1000  break;
1001  case 0x22:
1002  for (i = 0; i < 16; i++)
1003  map4to8[i] = *buf++;
1004  break;
1005 
1006  case 0xf0:
1007  x_pos = display->x_pos;
1008  y_pos += 2;
1009  break;
1010  default:
1011  av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
1012  }
1013  }
1014 
1015 }
1016 
1018  const uint8_t *buf, int buf_size)
1019 {
1020  DVBSubContext *ctx = avctx->priv_data;
1021 
1022  const uint8_t *buf_end = buf + buf_size;
1023  int object_id;
1024  DVBSubObject *object;
1025  DVBSubObjectDisplay *display;
1026  int top_field_len, bottom_field_len;
1027 
1028  int coding_method, non_modifying_color;
1029 
1030  object_id = AV_RB16(buf);
1031  buf += 2;
1032 
1033  object = get_object(ctx, object_id);
1034 
1035  if (!object)
1036  return AVERROR_INVALIDDATA;
1037 
1038  coding_method = ((*buf) >> 2) & 3;
1039  non_modifying_color = ((*buf++) >> 1) & 1;
1040 
1041  if (coding_method == 0) {
1042  top_field_len = AV_RB16(buf);
1043  buf += 2;
1044  bottom_field_len = AV_RB16(buf);
1045  buf += 2;
1046 
1047  if (buf + top_field_len + bottom_field_len > buf_end) {
1048  av_log(avctx, AV_LOG_ERROR, "Field data size %d+%d too large\n", top_field_len, bottom_field_len);
1049  return AVERROR_INVALIDDATA;
1050  }
1051 
1052  for (display = object->display_list; display; display = display->object_list_next) {
1053  const uint8_t *block = buf;
1054  int bfl = bottom_field_len;
1055 
1056  dvbsub_parse_pixel_data_block(avctx, display, block, top_field_len, 0,
1057  non_modifying_color);
1058 
1059  if (bottom_field_len > 0)
1060  block = buf + top_field_len;
1061  else
1062  bfl = top_field_len;
1063 
1064  dvbsub_parse_pixel_data_block(avctx, display, block, bfl, 1,
1065  non_modifying_color);
1066  }
1067 
1068 /* } else if (coding_method == 1) {*/
1069 
1070  } else {
1071  av_log(avctx, AV_LOG_ERROR, "Unknown object coding %d\n", coding_method);
1072  }
1073 
1074  return 0;
1075 }
1076 
1078  const uint8_t *buf, int buf_size)
1079 {
1080  DVBSubContext *ctx = avctx->priv_data;
1081 
1082  const uint8_t *buf_end = buf + buf_size;
1083  int i, clut_id;
1084  int version;
1085  DVBSubCLUT *clut;
1086  int entry_id, depth , full_range;
1087  int y, cr, cb, alpha;
1088  int r, g, b, r_add, g_add, b_add;
1089 
1090  ff_dlog(avctx, "DVB clut packet:\n");
1091 
1092  for (i=0; i < buf_size; i++) {
1093  ff_dlog(avctx, "%02x ", buf[i]);
1094  if (i % 16 == 15)
1095  ff_dlog(avctx, "\n");
1096  }
1097 
1098  if (i % 16)
1099  ff_dlog(avctx, "\n");
1100 
1101  clut_id = *buf++;
1102  version = ((*buf)>>4)&15;
1103  buf += 1;
1104 
1105  clut = get_clut(ctx, clut_id);
1106 
1107  if (!clut) {
1108  clut = av_malloc(sizeof(DVBSubCLUT));
1109  if (!clut)
1110  return AVERROR(ENOMEM);
1111 
1112  memcpy(clut, &default_clut, sizeof(DVBSubCLUT));
1113 
1114  clut->id = clut_id;
1115  clut->version = -1;
1116 
1117  clut->next = ctx->clut_list;
1118  ctx->clut_list = clut;
1119  }
1120 
1121  if (clut->version != version) {
1122 
1123  clut->version = version;
1124 
1125  while (buf + 4 < buf_end) {
1126  entry_id = *buf++;
1127 
1128  depth = (*buf) & 0xe0;
1129 
1130  if (depth == 0) {
1131  av_log(avctx, AV_LOG_ERROR, "Invalid clut depth 0x%x!\n", *buf);
1132  }
1133 
1134  full_range = (*buf++) & 1;
1135 
1136  if (full_range) {
1137  y = *buf++;
1138  cr = *buf++;
1139  cb = *buf++;
1140  alpha = *buf++;
1141  } else {
1142  y = buf[0] & 0xfc;
1143  cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1144  cb = (buf[1] << 2) & 0xf0;
1145  alpha = (buf[1] << 6) & 0xc0;
1146 
1147  buf += 2;
1148  }
1149 
1150  if (y == 0)
1151  alpha = 0xff;
1152 
1153  YUV_TO_RGB1_CCIR(cb, cr);
1154  YUV_TO_RGB2_CCIR(r, g, b, y);
1155 
1156  ff_dlog(avctx, "clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1157  if (!!(depth & 0x80) + !!(depth & 0x40) + !!(depth & 0x20) > 1) {
1158  ff_dlog(avctx, "More than one bit level marked: %x\n", depth);
1160  return AVERROR_INVALIDDATA;
1161  }
1162 
1163  if (depth & 0x80 && entry_id < 4)
1164  clut->clut4[entry_id] = RGBA(r,g,b,255 - alpha);
1165  else if (depth & 0x40 && entry_id < 16)
1166  clut->clut16[entry_id] = RGBA(r,g,b,255 - alpha);
1167  else if (depth & 0x20)
1168  clut->clut256[entry_id] = RGBA(r,g,b,255 - alpha);
1169  }
1170  }
1171 
1172  return 0;
1173 }
1174 
1175 
1177  const uint8_t *buf, int buf_size)
1178 {
1179  DVBSubContext *ctx = avctx->priv_data;
1180 
1181  const uint8_t *buf_end = buf + buf_size;
1182  int region_id, object_id;
1183  int av_unused version;
1184  DVBSubRegion *region;
1185  DVBSubObject *object;
1186  DVBSubObjectDisplay *display;
1187  int fill;
1188  int ret;
1189 
1190  if (buf_size < 10)
1191  return AVERROR_INVALIDDATA;
1192 
1193  region_id = *buf++;
1194 
1195  region = get_region(ctx, region_id);
1196 
1197  if (!region) {
1198  region = av_mallocz(sizeof(DVBSubRegion));
1199  if (!region)
1200  return AVERROR(ENOMEM);
1201 
1202  region->id = region_id;
1203  region->version = -1;
1204 
1205  region->next = ctx->region_list;
1206  ctx->region_list = region;
1207  }
1208 
1209  version = ((*buf)>>4) & 15;
1210  fill = ((*buf++) >> 3) & 1;
1211 
1212  region->width = AV_RB16(buf);
1213  buf += 2;
1214  region->height = AV_RB16(buf);
1215  buf += 2;
1216 
1217  ret = av_image_check_size(region->width, region->height, 0, avctx);
1218  if (ret < 0) {
1219  region->width= region->height= 0;
1220  return ret;
1221  }
1222 
1223  if (region->width * region->height != region->buf_size) {
1224  av_free(region->pbuf);
1225 
1226  region->buf_size = region->width * region->height;
1227 
1228  region->pbuf = av_malloc(region->buf_size);
1229  if (!region->pbuf) {
1230  region->buf_size =
1231  region->width =
1232  region->height = 0;
1233  return AVERROR(ENOMEM);
1234  }
1235 
1236  fill = 1;
1237  region->dirty = 0;
1238  }
1239 
1240  region->depth = 1 << (((*buf++) >> 2) & 7);
1241  if(region->depth<2 || region->depth>8){
1242  av_log(avctx, AV_LOG_ERROR, "region depth %d is invalid\n", region->depth);
1243  region->depth= 4;
1244  }
1245  region->clut = *buf++;
1246 
1247  if (region->depth == 8) {
1248  region->bgcolor = *buf++;
1249  buf += 1;
1250  } else {
1251  buf += 1;
1252 
1253  if (region->depth == 4)
1254  region->bgcolor = (((*buf++) >> 4) & 15);
1255  else
1256  region->bgcolor = (((*buf++) >> 2) & 3);
1257  }
1258 
1259  ff_dlog(avctx, "Region %d, (%dx%d)\n", region_id, region->width, region->height);
1260 
1261  if (fill) {
1262  memset(region->pbuf, region->bgcolor, region->buf_size);
1263  ff_dlog(avctx, "Fill region (%d)\n", region->bgcolor);
1264  }
1265 
1266  delete_region_display_list(ctx, region);
1267 
1268  while (buf + 5 < buf_end) {
1269  object_id = AV_RB16(buf);
1270  buf += 2;
1271 
1272  object = get_object(ctx, object_id);
1273 
1274  if (!object) {
1275  object = av_mallocz(sizeof(DVBSubObject));
1276  if (!object)
1277  return AVERROR(ENOMEM);
1278 
1279  object->id = object_id;
1280  object->next = ctx->object_list;
1281  ctx->object_list = object;
1282  }
1283 
1284  object->type = (*buf) >> 6;
1285 
1286  display = av_mallocz(sizeof(DVBSubObjectDisplay));
1287  if (!display)
1288  return AVERROR(ENOMEM);
1289 
1290  display->object_id = object_id;
1291  display->region_id = region_id;
1292 
1293  display->x_pos = AV_RB16(buf) & 0xfff;
1294  buf += 2;
1295  display->y_pos = AV_RB16(buf) & 0xfff;
1296  buf += 2;
1297 
1298  if ((object->type == 1 || object->type == 2) && buf+1 < buf_end) {
1299  display->fgcolor = *buf++;
1300  display->bgcolor = *buf++;
1301  }
1302 
1303  display->region_list_next = region->display_list;
1304  region->display_list = display;
1305 
1306  display->object_list_next = object->display_list;
1307  object->display_list = display;
1308  }
1309 
1310  return 0;
1311 }
1312 
1314  const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
1315 {
1316  DVBSubContext *ctx = avctx->priv_data;
1317  DVBSubRegionDisplay *display;
1318  DVBSubRegionDisplay *tmp_display_list, **tmp_ptr;
1319 
1320  const uint8_t *buf_end = buf + buf_size;
1321  int region_id;
1322  int page_state;
1323  int timeout;
1324  int version;
1325 
1326  if (buf_size < 1)
1327  return AVERROR_INVALIDDATA;
1328 
1329  timeout = *buf++;
1330  version = ((*buf)>>4) & 15;
1331  page_state = ((*buf++) >> 2) & 3;
1332 
1333  if (ctx->version == version) {
1334  return 0;
1335  }
1336 
1337  ctx->time_out = timeout;
1338  ctx->version = version;
1339 
1340  ff_dlog(avctx, "Page time out %ds, state %d\n", ctx->time_out, page_state);
1341 
1342  if(ctx->compute_edt == 1)
1343  save_subtitle_set(avctx, sub, got_output);
1344 
1345  if (page_state == 1 || page_state == 2) {
1346  delete_regions(ctx);
1347  delete_objects(ctx);
1348  delete_cluts(ctx);
1349  }
1350 
1351  tmp_display_list = ctx->display_list;
1352  ctx->display_list = NULL;
1353 
1354  while (buf + 5 < buf_end) {
1355  region_id = *buf++;
1356  buf += 1;
1357 
1358  display = tmp_display_list;
1359  tmp_ptr = &tmp_display_list;
1360 
1361  while (display && display->region_id != region_id) {
1362  tmp_ptr = &display->next;
1363  display = display->next;
1364  }
1365 
1366  if (!display) {
1367  display = av_mallocz(sizeof(DVBSubRegionDisplay));
1368  if (!display)
1369  return AVERROR(ENOMEM);
1370  }
1371 
1372  display->region_id = region_id;
1373 
1374  display->x_pos = AV_RB16(buf);
1375  buf += 2;
1376  display->y_pos = AV_RB16(buf);
1377  buf += 2;
1378 
1379  *tmp_ptr = display->next;
1380 
1381  display->next = ctx->display_list;
1382  ctx->display_list = display;
1383 
1384  ff_dlog(avctx, "Region %d, (%d,%d)\n", region_id, display->x_pos, display->y_pos);
1385  }
1386 
1387  while (tmp_display_list) {
1388  display = tmp_display_list;
1389 
1390  tmp_display_list = display->next;
1391 
1392  av_freep(&display);
1393  }
1394 
1395  return 0;
1396 }
1397 
1398 
1399 #ifdef DEBUG
1400 static int save_display_set(DVBSubContext *ctx)
1401 {
1402  DVBSubRegion *region;
1403  DVBSubRegionDisplay *display;
1404  DVBSubCLUT *clut;
1405  uint32_t *clut_table;
1406  int x_pos, y_pos, width, height;
1407  int x, y, y_off, x_off;
1408  uint32_t *pbuf;
1409  char filename[32];
1410  static int fileno_index = 0;
1411 
1412  x_pos = -1;
1413  y_pos = -1;
1414  width = 0;
1415  height = 0;
1416 
1417  for (display = ctx->display_list; display; display = display->next) {
1418  region = get_region(ctx, display->region_id);
1419 
1420  if (!region)
1421  return -1;
1422 
1423  if (x_pos == -1) {
1424  x_pos = display->x_pos;
1425  y_pos = display->y_pos;
1426  width = region->width;
1427  height = region->height;
1428  } else {
1429  if (display->x_pos < x_pos) {
1430  width += (x_pos - display->x_pos);
1431  x_pos = display->x_pos;
1432  }
1433 
1434  if (display->y_pos < y_pos) {
1435  height += (y_pos - display->y_pos);
1436  y_pos = display->y_pos;
1437  }
1438 
1439  if (display->x_pos + region->width > x_pos + width) {
1440  width = display->x_pos + region->width - x_pos;
1441  }
1442 
1443  if (display->y_pos + region->height > y_pos + height) {
1444  height = display->y_pos + region->height - y_pos;
1445  }
1446  }
1447  }
1448 
1449  if (x_pos >= 0) {
1450 
1451  pbuf = av_malloc(width * height * 4);
1452  if (!pbuf)
1453  return -1;
1454 
1455  for (display = ctx->display_list; display; display = display->next) {
1456  region = get_region(ctx, display->region_id);
1457 
1458  if (!region)
1459  return -1;
1460 
1461  x_off = display->x_pos - x_pos;
1462  y_off = display->y_pos - y_pos;
1463 
1464  clut = get_clut(ctx, region->clut);
1465 
1466  if (!clut)
1467  clut = &default_clut;
1468 
1469  switch (region->depth) {
1470  case 2:
1471  clut_table = clut->clut4;
1472  break;
1473  case 8:
1474  clut_table = clut->clut256;
1475  break;
1476  case 4:
1477  default:
1478  clut_table = clut->clut16;
1479  break;
1480  }
1481 
1482  for (y = 0; y < region->height; y++) {
1483  for (x = 0; x < region->width; x++) {
1484  pbuf[((y + y_off) * width) + x_off + x] =
1485  clut_table[region->pbuf[y * region->width + x]];
1486  }
1487  }
1488 
1489  }
1490 
1491  snprintf(filename, sizeof(filename), "dvbs.%d", fileno_index);
1492 
1493  png_save(filename, pbuf, width, height);
1494 
1495  av_freep(&pbuf);
1496  }
1497 
1498  fileno_index++;
1499  return 0;
1500 }
1501 #endif
1502 
1504  const uint8_t *buf,
1505  int buf_size)
1506 {
1507  DVBSubContext *ctx = avctx->priv_data;
1508  DVBSubDisplayDefinition *display_def = ctx->display_definition;
1509  int dds_version, info_byte;
1510 
1511  if (buf_size < 5)
1512  return AVERROR_INVALIDDATA;
1513 
1514  info_byte = bytestream_get_byte(&buf);
1515  dds_version = info_byte >> 4;
1516  if (display_def && display_def->version == dds_version)
1517  return 0; // already have this display definition version
1518 
1519  if (!display_def) {
1520  display_def = av_mallocz(sizeof(*display_def));
1521  if (!display_def)
1522  return AVERROR(ENOMEM);
1523  ctx->display_definition = display_def;
1524  }
1525 
1526  display_def->version = dds_version;
1527  display_def->x = 0;
1528  display_def->y = 0;
1529  display_def->width = bytestream_get_be16(&buf) + 1;
1530  display_def->height = bytestream_get_be16(&buf) + 1;
1531  if (!avctx->width || !avctx->height) {
1532  avctx->width = display_def->width;
1533  avctx->height = display_def->height;
1534  }
1535 
1536  if (info_byte & 1<<3) { // display_window_flag
1537  if (buf_size < 13)
1538  return AVERROR_INVALIDDATA;
1539 
1540  display_def->x = bytestream_get_be16(&buf);
1541  display_def->width = bytestream_get_be16(&buf) - display_def->x + 1;
1542  display_def->y = bytestream_get_be16(&buf);
1543  display_def->height = bytestream_get_be16(&buf) - display_def->y + 1;
1544  }
1545 
1546  return 0;
1547 }
1548 
1550  int buf_size, AVSubtitle *sub,int *got_output)
1551 {
1552  DVBSubContext *ctx = avctx->priv_data;
1553 
1554  if(ctx->compute_edt == 0)
1555  save_subtitle_set(avctx, sub, got_output);
1556 #ifdef DEBUG
1557  save_display_set(ctx);
1558 #endif
1559  return 0;
1560 }
1561 
1562 static int dvbsub_decode(AVCodecContext *avctx,
1563  void *data, int *data_size,
1564  AVPacket *avpkt)
1565 {
1566  const uint8_t *buf = avpkt->data;
1567  int buf_size = avpkt->size;
1568  DVBSubContext *ctx = avctx->priv_data;
1569  AVSubtitle *sub = data;
1570  const uint8_t *p, *p_end;
1571  int segment_type;
1572  int page_id;
1573  int segment_length;
1574  int i;
1575  int ret = 0;
1576  int got_segment = 0;
1577  int got_dds = 0;
1578 
1579  ff_dlog(avctx, "DVB sub packet:\n");
1580 
1581  for (i=0; i < buf_size; i++) {
1582  ff_dlog(avctx, "%02x ", buf[i]);
1583  if (i % 16 == 15)
1584  ff_dlog(avctx, "\n");
1585  }
1586 
1587  if (i % 16)
1588  ff_dlog(avctx, "\n");
1589 
1590  if (buf_size <= 6 || *buf != 0x0f) {
1591  ff_dlog(avctx, "incomplete or broken packet");
1592  return AVERROR_INVALIDDATA;
1593  }
1594 
1595  p = buf;
1596  p_end = buf + buf_size;
1597 
1598  while (p_end - p >= 6 && *p == 0x0f) {
1599  p += 1;
1600  segment_type = *p++;
1601  page_id = AV_RB16(p);
1602  p += 2;
1603  segment_length = AV_RB16(p);
1604  p += 2;
1605 
1606  if (avctx->debug & FF_DEBUG_STARTCODE) {
1607  av_log(avctx, AV_LOG_DEBUG, "segment_type:%d page_id:%d segment_length:%d\n", segment_type, page_id, segment_length);
1608  }
1609 
1610  if (p_end - p < segment_length) {
1611  ff_dlog(avctx, "incomplete or broken packet");
1612  ret = -1;
1613  goto end;
1614  }
1615 
1616  if (page_id == ctx->composition_id || page_id == ctx->ancillary_id ||
1617  ctx->composition_id == -1 || ctx->ancillary_id == -1) {
1618  int ret = 0;
1619  switch (segment_type) {
1620  case DVBSUB_PAGE_SEGMENT:
1621  ret = dvbsub_parse_page_segment(avctx, p, segment_length, sub, data_size);
1622  got_segment |= 1;
1623  break;
1624  case DVBSUB_REGION_SEGMENT:
1625  ret = dvbsub_parse_region_segment(avctx, p, segment_length);
1626  got_segment |= 2;
1627  break;
1628  case DVBSUB_CLUT_SEGMENT:
1629  ret = dvbsub_parse_clut_segment(avctx, p, segment_length);
1630  if (ret < 0) goto end;
1631  got_segment |= 4;
1632  break;
1633  case DVBSUB_OBJECT_SEGMENT:
1634  ret = dvbsub_parse_object_segment(avctx, p, segment_length);
1635  got_segment |= 8;
1636  break;
1639  segment_length);
1640  got_dds = 1;
1641  break;
1643  ret = dvbsub_display_end_segment(avctx, p, segment_length, sub, data_size);
1644  if (got_segment == 15 && !got_dds && !avctx->width && !avctx->height) {
1645  // Default from ETSI EN 300 743 V1.3.1 (7.2.1)
1646  avctx->width = 720;
1647  avctx->height = 576;
1648  }
1649  got_segment |= 16;
1650  break;
1651  default:
1652  ff_dlog(avctx, "Subtitling segment type 0x%x, page id %d, length %d\n",
1653  segment_type, page_id, segment_length);
1654  break;
1655  }
1656  if (ret < 0)
1657  goto end;
1658  }
1659 
1660  p += segment_length;
1661  }
1662  // Some streams do not send a display segment but if we have all the other
1663  // segments then we need no further data.
1664  if (got_segment == 15) {
1665  av_log(avctx, AV_LOG_DEBUG, "Missing display_end_segment, emulating\n");
1666  dvbsub_display_end_segment(avctx, p, 0, sub, data_size);
1667  }
1668 
1669 end:
1670  if(ret < 0) {
1671  *data_size = 0;
1672  avsubtitle_free(sub);
1673  return ret;
1674  } else {
1675  if(ctx->compute_edt == 1 )
1676  FFSWAP(int64_t, ctx->prev_start, sub->pts);
1677  }
1678 
1679  return p - buf;
1680 }
1681 
1682 #define DS AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_SUBTITLE_PARAM
1683 static const AVOption options[] = {
1684  {"compute_edt", "compute end of time using pts or timeout", offsetof(DVBSubContext, compute_edt), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1, DS},
1685  {"compute_clut", "compute clut when not available(-1) or always(1) or never(0)", offsetof(DVBSubContext, compute_clut), AV_OPT_TYPE_BOOL, {.i64 = -1}, -1, 1, DS},
1686  {"dvb_substream", "", offsetof(DVBSubContext, substream), AV_OPT_TYPE_INT, {.i64 = -1}, -1, 63, DS},
1687  {NULL}
1688 };
1689 static const AVClass dvbsubdec_class = {
1690  .class_name = "DVB Sub Decoder",
1691  .item_name = av_default_item_name,
1692  .option = options,
1693  .version = LIBAVUTIL_VERSION_INT,
1694 };
1695 
1697  .name = "dvbsub",
1698  .long_name = NULL_IF_CONFIG_SMALL("DVB subtitles"),
1699  .type = AVMEDIA_TYPE_SUBTITLE,
1701  .priv_data_size = sizeof(DVBSubContext),
1703  .close = dvbsub_close_decoder,
1704  .decode = dvbsub_decode,
1705  .priv_class = &dvbsubdec_class,
1706 };
int version
Definition: dvbsubdec.c:100
#define NULL
Definition: coverity.c:32
static DVBSubCLUT * get_clut(DVBSubContext *ctx, int clut_id)
Definition: dvbsubdec.c:206
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int dvbsub_read_8bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len, const uint8_t **srcbuf, int buf_size, int non_mod, uint8_t *map_table, int x_pos)
Definition: dvbsubdec.c:653
static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display, const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
Definition: dvbsubdec.c:901
static DVBSubRegion * get_region(DVBSubContext *ctx, int region_id)
Definition: dvbsubdec.c:217
AVOption.
Definition: opt.h:246
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:101
DVBSubRegionDisplay * display_list
Definition: dvbsubdec.c:190
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:85
#define YUV_TO_RGB1_CCIR(cb1, cr1)
Definition: colorspace.h:34
const char * g
Definition: vf_curves.c:112
int64_t prev_start
Definition: dvbsubdec.c:185
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
int size
Definition: avcodec.h:1658
const char * b
Definition: vf_curves.c:113
#define V(x, y)
Various defines for YUV<->RGB conversion.
unsigned num_rects
Definition: avcodec.h:4046
#define RGBA(r, g, b, a)
Definition: dvbsubdec.c:96
int version
Definition: avisynth_c.h:766
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:222
static int dvbsub_parse_page_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
Definition: dvbsubdec.c:1313
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
AVCodec.
Definition: avcodec.h:3681
static int16_t block[64]
Definition: dct.c:115
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
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
uint8_t bits
Definition: crc.c:296
uint8_t
#define av_cold
Definition: attributes.h:82
#define av_malloc(s)
static int dvbsub_read_4bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len, const uint8_t **srcbuf, int buf_size, int non_mod, uint8_t *map_table, int x_pos)
Definition: dvbsubdec.c:530
static void delete_cluts(DVBSubContext *ctx)
Definition: dvbsubdec.c:274
AVOptions.
#define AVPALETTE_SIZE
Definition: pixfmt.h:32
static av_cold int end(AVCodecContext *avctx)
Definition: avrndec.c:90
int y
Definition: f_ebur128.c:91
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:1847
static const AVClass dvbsubdec_class
Definition: dvbsubdec.c:1689
#define height
uint8_t * data
Definition: avcodec.h:1657
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:199
#define ff_dlog(a,...)
bitstream reader API header.
DVBSubRegion * region_list
Definition: dvbsubdec.c:186
#define av_log(a,...)
static void delete_regions(DVBSubContext *ctx)
Definition: dvbsubdec.c:296
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:142
#define DVBSUB_DISPLAYDEFINITION_SEGMENT
Definition: dvbsubdec.c:34
#define L(x, y)
struct DVBSubRegion * next
Definition: dvbsubdec.c:162
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
av_default_item_name
#define AVERROR(e)
Definition: error.h:43
int compute_clut
Definition: dvbsubdec.c:183
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:179
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 DVBSubCLUT default_clut
Definition: dvbsubdec.c:109
static int dvbsub_parse_display_definition_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: dvbsubdec.c:1503
const char * name
Name of the codec implementation.
Definition: avcodec.h:3688
GLsizei count
Definition: opengl_enc.c:109
#define FFMAX(a, b)
Definition: common.h:94
static void * av_mallocz_array(size_t nmemb, size_t size)
Definition: mem.h:229
#define fail()
Definition: checkasm.h:89
int depth
Definition: v4l.c:62
uint32_t end_display_time
Definition: avcodec.h:4045
int64_t pts
Same as packet pts, in AV_TIME_BASE.
Definition: avcodec.h:4048
struct DVBSubObject * next
Definition: dvbsubdec.c:133
AVCodec ff_dvbsub_decoder
Definition: dvbsubdec.c:1696
int av_image_check_size(unsigned int w, unsigned int h, int log_offset, void *log_ctx)
Check if the given dimension of an image is valid, meaning that all bytes of the image can be address...
Definition: imgutils.c:281
A bitmap, pict will be set.
Definition: avcodec.h:3990
int linesize[4]
Definition: avcodec.h:4026
int composition_id
Definition: dvbsubdec.c:176
DVBSubObjectDisplay * display_list
Definition: dvbsubdec.c:160
struct DVBSubObjectDisplay * region_list_next
Definition: dvbsubdec.c:121
#define width
int width
picture width / height.
Definition: avcodec.h:1919
static int dvbsub_parse_clut_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: dvbsubdec.c:1077
AVFormatContext * ctx
Definition: movenc.c:48
struct DVBSubCLUT * next
Definition: dvbsubdec.c:106
static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
Definition: dvbsubdec.c:764
uint32_t clut16[16]
Definition: dvbsubdec.c:103
DVBSubObjectDisplay * display_list
Definition: dvbsubdec.c:131
static void delete_objects(DVBSubContext *ctx)
Definition: dvbsubdec.c:285
DVBSubDisplayDefinition * display_definition
Definition: dvbsubdec.c:191
uint8_t * data[4]
data+linesize for the bitmap of this subtitle.
Definition: avcodec.h:4025
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
Definition: error.h:62
#define DS
Definition: dvbsubdec.c:1682
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
#define DVBSUB_CLUT_SEGMENT
Definition: dvbsubdec.c:32
Libavcodec external API header.
uint8_t * pbuf
Definition: dvbsubdec.c:156
#define AV_TIME_BASE_Q
Internal time base represented as fractional value.
Definition: avutil.h:260
uint32_t clut256[256]
Definition: dvbsubdec.c:104
int debug
debug
Definition: avcodec.h:2973
main external API structure.
Definition: avcodec.h:1732
static int dvbsub_display_end_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size, AVSubtitle *sub, int *got_output)
Definition: dvbsubdec.c:1549
static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
Definition: vf_drawtext.c:770
void avsubtitle_free(AVSubtitle *sub)
Free all allocated data in the given subtitle struct.
Definition: utils.c:2782
int x
Definition: f_ebur128.c:91
DVBSubCLUT * clut_list
Definition: dvbsubdec.c:187
static DVBSubObject * get_object(DVBSubContext *ctx, int object_id)
Definition: dvbsubdec.c:195
void * buf
Definition: avisynth_c.h:690
int extradata_size
Definition: avcodec.h:1848
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:313
static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
Definition: dvbsubdec.c:228
Describe the class of an AVClass context structure.
Definition: log.h:67
Definition: f_ebur128.c:91
#define DVBSUB_DISPLAY_SEGMENT
Definition: dvbsubdec.c:35
struct DVBSubRegionDisplay * next
Definition: dvbsubdec.c:142
#define FF_COMPLIANCE_NORMAL
Definition: avcodec.h:2954
Rational number (pair of numerator and denominator).
Definition: rational.h:58
#define YUV_TO_RGB2_CCIR(r, g, b, y1)
Definition: colorspace.h:55
struct DVBSubObjectDisplay * object_list_next
Definition: dvbsubdec.c:122
static int init_get_bits(GetBitContext *s, const uint8_t *buffer, int bit_size)
Initialize GetBitContext.
Definition: get_bits.h:425
int h
Definition: f_ebur128.c:91
#define snprintf
Definition: snprintf.h:34
#define DVBSUB_PAGE_SEGMENT
Definition: dvbsubdec.c:30
uint32_t clut4[4]
Definition: dvbsubdec.c:102
static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
Definition: dvbsubdec.c:310
static const AVOption options[]
Definition: dvbsubdec.c:1683
static void compute_default_clut(AVSubtitleRect *rect, int w, int h)
Definition: dvbsubdec.c:707
#define DVBSUB_REGION_SEGMENT
Definition: dvbsubdec.c:31
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:83
common internal api header.
int ancillary_id
Definition: dvbsubdec.c:177
#define AV_WN32(p, v)
Definition: intreadwrite.h:381
int w
Definition: f_ebur128.c:91
static int dvbsub_decode(AVCodecContext *avctx, void *data, int *data_size, AVPacket *avpkt)
Definition: dvbsubdec.c:1562
void * priv_data
Definition: avcodec.h:1774
#define av_free(p)
#define FF_DEBUG_STARTCODE
Definition: avcodec.h:2987
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:84
static int dvbsub_parse_object_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: dvbsubdec.c:1017
DVBSubObject * object_list
Definition: dvbsubdec.c:188
static int dvbsub_read_2bit_string(AVCodecContext *avctx, uint8_t *destbuf, int dbuf_len, const uint8_t **srcbuf, int buf_size, int non_mod, uint8_t *map_table, int x_pos)
Definition: dvbsubdec.c:422
#define av_freep(p)
static int decode(AVCodecContext *avctx, AVFrame *frame, int *got_frame, AVPacket *pkt)
Definition: ffmpeg.c:2257
static int dvbsub_parse_region_segment(AVCodecContext *avctx, const uint8_t *buf, int buf_size)
Definition: dvbsubdec.c:1176
#define FFSWAP(type, a, b)
Definition: common.h:99
int compute_edt
if 1 end display time calculated using pts if 0 (Default) calculated using time out ...
Definition: dvbsubdec.c:181
#define DVBSUB_OBJECT_SEGMENT
Definition: dvbsubdec.c:33
static double cr(void *priv, double x, double y)
Definition: vf_geq.c:98
static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
Definition: dvbsubdec.c:399
This structure stores compressed data.
Definition: avcodec.h:1634
int strict_std_compliance
strictly follow the standard (MPEG-4, ...).
Definition: avcodec.h:2951
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
#define av_unused
Definition: attributes.h:125