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