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 */
26
27 #define DVBSUB_PAGE_SEGMENT 0x10
28 #define DVBSUB_REGION_SEGMENT 0x11
29 #define DVBSUB_CLUT_SEGMENT 0x12
30 #define DVBSUB_OBJECT_SEGMENT 0x13
31 #define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
32 #define DVBSUB_DISPLAY_SEGMENT 0x80
33
34 #define cm (ff_cropTbl + MAX_NEG_CROP)
35
36 #ifdef DEBUG
37 #undef fprintf
38 #undef perror
39 #if 0
40 static void png_save(
const char *filename,
uint8_t *bitmap,
int w,
int h,
41 uint32_t *rgba_palette)
42 {
44 FILE *f;
45 char fname[40], fname2[40];
47
48 snprintf(fname, 40,
"%s.ppm", filename);
49
50 f = fopen(fname, "w");
51 if (!f) {
52 perror(fname);
53 return;
54 }
55 fprintf(f, "P6\n"
56 "%d %d\n"
57 "%d\n",
58 w, h, 255);
59 for(y = 0; y < h; y++) {
60 for(x = 0; x < w; x++) {
61 v = rgba_palette[bitmap[y * w + x]];
62 putc((v >> 16) & 0xff, f);
63 putc((v >> 8) & 0xff, f);
64 putc((v >> 0) & 0xff, f);
65 }
66 }
67 fclose(f);
68
69
70 snprintf(fname2, 40,
"%s-a.pgm", filename);
71
72 f = fopen(fname2, "w");
73 if (!f) {
74 perror(fname2);
75 return;
76 }
77 fprintf(f, "P5\n"
78 "%d %d\n"
79 "%d\n",
80 w, h, 255);
81 for(y = 0; y < h; y++) {
82 for(x = 0; x < w; x++) {
83 v = rgba_palette[bitmap[y * w + x]];
84 putc((v >> 24) & 0xff, f);
85 }
86 }
87 fclose(f);
88
89 snprintf(command, 1024,
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
90 system(command);
91
92 snprintf(command, 1024,
"rm %s %s", fname, fname2);
93 system(command);
94 }
95 #endif
96
97 static void png_save2(const char *filename, uint32_t *bitmap, int w, int h)
98 {
100 FILE *f;
101 char fname[40], fname2[40];
103
104 snprintf(fname,
sizeof(fname),
"%s.ppm", filename);
105
106 f = fopen(fname, "w");
107 if (!f) {
108 perror(fname);
109 return;
110 }
111 fprintf(f, "P6\n"
112 "%d %d\n"
113 "%d\n",
114 w, h, 255);
115 for(y = 0; y < h; y++) {
116 for(x = 0; x < w; x++) {
117 v = bitmap[y * w + x];
118 putc((v >> 16) & 0xff, f);
119 putc((v >> 8) & 0xff, f);
120 putc((v >> 0) & 0xff, f);
121 }
122 }
123 fclose(f);
124
125
126 snprintf(fname2,
sizeof(fname2),
"%s-a.pgm", filename);
127
128 f = fopen(fname2, "w");
129 if (!f) {
130 perror(fname2);
131 return;
132 }
133 fprintf(f, "P5\n"
134 "%d %d\n"
135 "%d\n",
136 w, h, 255);
137 for(y = 0; y < h; y++) {
138 for(x = 0; x < w; x++) {
139 v = bitmap[y * w + x];
140 putc((v >> 24) & 0xff, f);
141 }
142 }
143 fclose(f);
144
145 snprintf(command,
sizeof(command),
"pnmtopng -alpha %s %s > %s.png 2> /dev/null", fname2, fname, filename);
146 system(command);
147
148 snprintf(command,
sizeof(command),
"rm %s %s", fname, fname2);
149 system(command);
150 }
151 #endif
152
153 #define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
154
158
162
165
167
171
174
177
181
185
187
189
192
195
198
201
205
209
212
216
218
221
224
230
234
240
245
246
248 {
250
251 while (ptr && ptr->
id != object_id) {
253 }
254
255 return ptr;
256 }
257
259 {
261
262 while (ptr && ptr->
id != clut_id) {
264 }
265
266 return ptr;
267 }
268
270 {
272
273 while (ptr && ptr->
id != region_id) {
275 }
276
277 return ptr;
278 }
279
281 {
284
287
289
290 if (object) {
291 obj_disp_ptr = &object->display_list;
292 obj_disp = *obj_disp_ptr;
293
294 while (obj_disp && obj_disp != display) {
296 obj_disp = *obj_disp_ptr;
297 }
298
299 if (obj_disp) {
301
304 obj2 = *obj2_ptr;
305
306 while (obj2 != object) {
307 assert(obj2);
308 obj2_ptr = &obj2->
next;
309 obj2 = *obj2_ptr;
310 }
311
312 *obj2_ptr = obj2->
next;
313
315 }
316 }
317 }
318
320
322 }
323
324 }
325
327 {
329
332
334
336 }
337 }
338
340 {
342
345
347
349 }
350 }
351
353 {
355
358
360
362
365 }
366 }
367
369 {
370 int i,
r,
g,
b,
a = 0;
372
377 } else {
380 }
381
383
384 default_clut.
id = -1;
385 default_clut.
next = NULL;
386
387 default_clut.
clut4[0] =
RGBA( 0, 0, 0, 0);
388 default_clut.
clut4[1] =
RGBA(255, 255, 255, 255);
389 default_clut.
clut4[2] =
RGBA( 0, 0, 0, 255);
390 default_clut.
clut4[3] =
RGBA(127, 127, 127, 255);
391
393 for (i = 1; i < 16; i++) {
394 if (i < 8) {
395 r = (i & 1) ? 255 : 0;
396 g = (i & 2) ? 255 : 0;
397 b = (i & 4) ? 255 : 0;
398 } else {
399 r = (i & 1) ? 127 : 0;
400 g = (i & 2) ? 127 : 0;
401 b = (i & 4) ? 127 : 0;
402 }
404 }
405
407 for (i = 1; i < 256; i++) {
408 if (i < 8) {
409 r = (i & 1) ? 255 : 0;
410 g = (i & 2) ? 255 : 0;
411 b = (i & 4) ? 255 : 0;
412 a = 63;
413 } else {
414 switch (i & 0x88) {
415 case 0x00:
416 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
417 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
418 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
419 a = 255;
420 break;
421 case 0x08:
422 r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
423 g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
424 b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
425 a = 127;
426 break;
427 case 0x80:
428 r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
429 g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
430 b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
431 a = 255;
432 break;
433 case 0x88:
434 r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
435 g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
436 b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
437 a = 255;
438 break;
439 }
440 }
442 }
443
444 return 0;
445 }
446
448 {
451
453
455
457
459
463
465 }
466
467 return 0;
468 }
469
471 const uint8_t **srcbuf,
int buf_size,
472 int non_mod,
uint8_t *map_table,
int x_pos)
473 {
475
477 int run_length;
478 int pixels_read = x_pos;
479
481
482 destbuf += x_pos;
483
484 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
486
487 if (bits) {
488 if (non_mod != 1 || bits != 1) {
489 if (map_table)
490 *destbuf++ = map_table[
bits];
491 else
493 }
494 pixels_read++;
495 } else {
497 if (bits == 1) {
500
501 if (non_mod == 1 && bits == 1)
502 pixels_read += run_length;
503 else {
504 if (map_table)
505 bits = map_table[
bits];
506 while (run_length-- > 0 && pixels_read < dbuf_len) {
508 pixels_read++;
509 }
510 }
511 } else {
513 if (bits == 0) {
515 if (bits == 2) {
518
519 if (non_mod == 1 && bits == 1)
520 pixels_read += run_length;
521 else {
522 if (map_table)
523 bits = map_table[
bits];
524 while (run_length-- > 0 && pixels_read < dbuf_len) {
526 pixels_read++;
527 }
528 }
529 } else if (bits == 3) {
532
533 if (non_mod == 1 && bits == 1)
534 pixels_read += run_length;
535 else {
536 if (map_table)
537 bits = map_table[
bits];
538 while (run_length-- > 0 && pixels_read < dbuf_len) {
540 pixels_read++;
541 }
542 }
543 } else if (bits == 1) {
544 if (map_table)
545 bits = map_table[0];
546 else
547 bits = 0;
548 run_length = 2;
549 while (run_length-- > 0 && pixels_read < dbuf_len) {
551 pixels_read++;
552 }
553 } else {
555 return pixels_read;
556 }
557 } else {
558 if (map_table)
559 bits = map_table[0];
560 else
561 bits = 0;
563 pixels_read++;
564 }
565 }
566 }
567 }
568
571
573
574 return pixels_read;
575 }
576
578 const uint8_t **srcbuf,
int buf_size,
579 int non_mod,
uint8_t *map_table,
int x_pos)
580 {
582
584 int run_length;
585 int pixels_read = x_pos;
586
588
589 destbuf += x_pos;
590
591 while (
get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
593
594 if (bits) {
595 if (non_mod != 1 || bits != 1) {
596 if (map_table)
597 *destbuf++ = map_table[
bits];
598 else
600 }
601 pixels_read++;
602 } else {
604 if (bits == 0) {
606
607 if (run_length == 0) {
609 return pixels_read;
610 }
611
612 run_length += 2;
613
614 if (map_table)
615 bits = map_table[0];
616 else
617 bits = 0;
618
619 while (run_length-- > 0 && pixels_read < dbuf_len) {
621 pixels_read++;
622 }
623 } else {
625 if (bits == 0) {
628
629 if (non_mod == 1 && bits == 1)
630 pixels_read += run_length;
631 else {
632 if (map_table)
633 bits = map_table[
bits];
634 while (run_length-- > 0 && pixels_read < dbuf_len) {
636 pixels_read++;
637 }
638 }
639 } else {
641 if (bits == 2) {
644
645 if (non_mod == 1 && bits == 1)
646 pixels_read += run_length;
647 else {
648 if (map_table)
649 bits = map_table[
bits];
650 while (run_length-- > 0 && pixels_read < dbuf_len) {
652 pixels_read++;
653 }
654 }
655 } else if (bits == 3) {
658
659 if (non_mod == 1 && bits == 1)
660 pixels_read += run_length;
661 else {
662 if (map_table)
663 bits = map_table[
bits];
664 while (run_length-- > 0 && pixels_read < dbuf_len) {
666 pixels_read++;
667 }
668 }
669 } else if (bits == 1) {
670 if (map_table)
671 bits = map_table[0];
672 else
673 bits = 0;
674 run_length = 2;
675 while (run_length-- > 0 && pixels_read < dbuf_len) {
677 pixels_read++;
678 }
679 } else {
680 if (map_table)
681 bits = map_table[0];
682 else
683 bits = 0;
685 pixels_read ++;
686 }
687 }
688 }
689 }
690 }
691
694
696
697 return pixels_read;
698 }
699
701 const uint8_t **srcbuf,
int buf_size,
702 int non_mod,
uint8_t *map_table,
int x_pos)
703 {
704 const uint8_t *sbuf_end = (*srcbuf) + buf_size;
706 int run_length;
707 int pixels_read = x_pos;
708
709 destbuf += x_pos;
710
711 while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
712 bits = *(*srcbuf)++;
713
714 if (bits) {
715 if (non_mod != 1 || bits != 1) {
716 if (map_table)
717 *destbuf++ = map_table[
bits];
718 else
720 }
721 pixels_read++;
722 } else {
723 bits = *(*srcbuf)++;
724 run_length = bits & 0x7f;
725 if ((bits & 0x80) == 0) {
726 if (run_length == 0) {
727 return pixels_read;
728 }
729
730 if (map_table)
731 bits = map_table[0];
732 else
733 bits = 0;
734 while (run_length-- > 0 && pixels_read < dbuf_len) {
736 pixels_read++;
737 }
738 } else {
739 bits = *(*srcbuf)++;
740
741 if (non_mod == 1 && bits == 1)
742 pixels_read += run_length;
743 if (map_table)
744 bits = map_table[
bits];
745 else while (run_length-- > 0 && pixels_read < dbuf_len) {
747 pixels_read++;
748 }
749 }
750 }
751 }
752
753 if (*(*srcbuf)++)
755
756 return pixels_read;
757 }
758
759
760
762 const uint8_t *
buf,
int buf_size,
int top_bottom,
int non_mod)
763 {
765
767 const uint8_t *buf_end = buf + buf_size;
769 int x_pos, y_pos;
770 int i;
771
772 uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
773 uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
774 uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
775 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
777
778 #if 0
779 av_dlog(avctx,
"DVB pixel block size %d, %s field:\n", buf_size,
780 top_bottom ? "bottom" : "top");
781
782 for (i = 0; i < buf_size; i++) {
783 if (i % 16 == 0)
784 av_dlog(avctx,
"0x%8p: ", buf+i);
785
786 av_dlog(avctx,
"%02x ", buf[i]);
787 if (i % 16 == 15)
789 }
790
791 if (i % 16)
793 #endif
794
795 if (region == 0)
796 return;
797
800
801 x_pos = display->
x_pos;
802 y_pos = display->
y_pos;
803
804 y_pos += top_bottom;
805
806 while (buf < buf_end) {
807 if ((*buf!=0xf0 && x_pos >= region->
width) || y_pos >= region->
height) {
809 return;
810 }
811
812 switch (*buf++) {
813 case 0x10:
814 if (region->
depth == 8)
815 map_table = map2to8;
816 else if (region->
depth == 4)
817 map_table = map2to4;
818 else
819 map_table = NULL;
820
822 region->
width, &buf, buf_end - buf,
823 non_mod, map_table, x_pos);
824 break;
825 case 0x11:
826 if (region->
depth < 4) {
828 return;
829 }
830
831 if (region->
depth == 8)
832 map_table = map4to8;
833 else
834 map_table = NULL;
835
837 region->
width, &buf, buf_end - buf,
838 non_mod, map_table, x_pos);
839 break;
840 case 0x12:
841 if (region->
depth < 8) {
843 return;
844 }
845
847 region->
width, &buf, buf_end - buf,
848 non_mod, NULL, x_pos);
849 break;
850
851 case 0x20:
852 map2to4[0] = (*buf) >> 4;
853 map2to4[1] = (*buf++) & 0xf;
854 map2to4[2] = (*buf) >> 4;
855 map2to4[3] = (*buf++) & 0xf;
856 break;
857 case 0x21:
858 for (i = 0; i < 4; i++)
859 map2to8[i] = *buf++;
860 break;
861 case 0x22:
862 for (i = 0; i < 16; i++)
863 map4to8[i] = *buf++;
864 break;
865
866 case 0xf0:
867 x_pos = display->
x_pos;
868 y_pos += 2;
869 break;
870 default:
872 }
873 }
874
875 }
876
879 {
881
882 const uint8_t *buf_end = buf + buf_size;
883 int object_id;
886 int top_field_len, bottom_field_len;
887
888 int coding_method, non_modifying_color;
889
891 buf += 2;
892
894
895 if (!object)
896 return;
897
898 coding_method = ((*buf) >> 2) & 3;
899 non_modifying_color = ((*buf++) >> 1) & 1;
900
901 if (coding_method == 0) {
903 buf += 2;
904 bottom_field_len =
AV_RB16(buf);
905 buf += 2;
906
907 if (buf + top_field_len + bottom_field_len > buf_end) {
909 return;
910 }
911
914 int bfl = bottom_field_len;
915
917 non_modifying_color);
918
919 if (bottom_field_len > 0)
920 block = buf + top_field_len;
921 else
922 bfl = top_field_len;
923
925 non_modifying_color);
926 }
927
928 /* } else if (coding_method == 1) {*/
929
930 } else {
932 }
933
934 }
935
938 {
940
941 const uint8_t *buf_end = buf + buf_size;
942 int i, clut_id;
945 int entry_id,
depth , full_range;
947 int r,
g,
b, r_add, g_add, b_add;
948
949 av_dlog(avctx,
"DVB clut packet:\n");
950
951 for (i=0; i < buf_size; i++) {
952 av_dlog(avctx,
"%02x ", buf[i]);
953 if (i % 16 == 15)
955 }
956
957 if (i % 16)
959
960 clut_id = *buf++;
961 version = ((*buf)>>4)&15;
962 buf += 1;
963
965
966 if (!clut) {
968
969 memcpy(clut, &default_clut,
sizeof(
DVBSubCLUT));
970
973
976 }
977
978 if (clut->
version != version) {
979
981
982 while (buf + 4 < buf_end) {
983 entry_id = *buf++;
984
985 depth = (*buf) & 0xe0;
986
987 if (depth == 0) {
989 return;
990 }
991
992 full_range = (*buf++) & 1;
993
994 if (full_range) {
995 y = *buf++;
996 cr = *buf++;
997 cb = *buf++;
998 alpha = *buf++;
999 } else {
1000 y = buf[0] & 0xfc;
1001 cr = (((buf[0] & 3) << 2) | ((buf[1] >> 6) & 3)) << 4;
1002 cb = (buf[1] << 2) & 0xf0;
1003 alpha = (buf[1] << 6) & 0xc0;
1004
1005 buf += 2;
1006 }
1007
1008 if (y == 0)
1009 alpha = 0xff;
1010
1013
1014 av_dlog(avctx,
"clut %d := (%d,%d,%d,%d)\n", entry_id, r, g, b, alpha);
1015
1016 if (depth & 0x80)
1017 clut->
clut4[entry_id] =
RGBA(r,g,b,255 - alpha);
1018 if (depth & 0x40)
1019 clut->
clut16[entry_id] =
RGBA(r,g,b,255 - alpha);
1020 if (depth & 0x20)
1021 clut->
clut256[entry_id] =
RGBA(r,g,b,255 - alpha);
1022 }
1023 }
1024 }
1025
1026
1029 {
1031
1032 const uint8_t *buf_end = buf + buf_size;
1033 int region_id, object_id;
1038 int fill;
1039
1040 if (buf_size < 10)
1041 return;
1042
1043 region_id = *buf++;
1044
1046
1047 if (!region) {
1049
1050 region->
id = region_id;
1052
1055 }
1056
1057 version = ((*buf)>>4) & 15;
1058 fill = ((*buf++) >> 3) & 1;
1059
1061 buf += 2;
1063 buf += 2;
1064
1067
1069
1071
1072 fill = 1;
1074 }
1075
1076 region->
depth = 1 << (((*buf++) >> 2) & 7);
1080 }
1081 region->
clut = *buf++;
1082
1083 if (region->
depth == 8) {
1085 buf += 1;
1086 } else {
1087 buf += 1;
1088
1089 if (region->
depth == 4)
1090 region->
bgcolor = (((*buf++) >> 4) & 15);
1091 else
1092 region->
bgcolor = (((*buf++) >> 2) & 3);
1093 }
1094
1096
1097 if (fill) {
1100 }
1101
1103
1104 while (buf + 5 < buf_end) {
1106 buf += 2;
1107
1109
1110 if (!object) {
1112
1113 object->id = object_id;
1116 }
1117
1118 object->
type = (*buf) >> 6;
1119
1121
1124
1126 buf += 2;
1128 buf += 2;
1129
1130 if ((object->
type == 1 || object->
type == 2) && buf+1 < buf_end) {
1133 }
1134
1137
1139 object->display_list = display;
1140 }
1141 }
1142
1145 {
1149
1150 const uint8_t *buf_end = buf + buf_size;
1151 int region_id;
1152 int page_state;
1153 int timeout;
1155
1156 if (buf_size < 1)
1157 return;
1158
1159 timeout = *buf++;
1160 version = ((*buf)>>4) & 15;
1161 page_state = ((*buf++) >> 2) & 3;
1162
1163 if (ctx->
version != version) {
1164
1167
1168 av_dlog(avctx,
"Page time out %ds, state %d\n", ctx->
time_out, page_state);
1169
1170 if (page_state == 1 || page_state == 2) {
1174 }
1175
1179
1180 while (buf + 5 < buf_end) {
1181 region_id = *buf++;
1182 buf += 1;
1183
1184 display = tmp_display_list;
1185 tmp_ptr = &tmp_display_list;
1186
1187 while (display && display->
region_id != region_id) {
1188 tmp_ptr = &display->
next;
1189 display = display->
next;
1190 }
1191
1192 if (!display)
1194
1196
1198 buf += 2;
1200 buf += 2;
1201
1202 *tmp_ptr = display->
next;
1203
1207
1208 av_dlog(avctx,
"Region %d, (%d,%d)\n", region_id, display->
x_pos, display->
y_pos);
1209 }
1210
1211 while (tmp_display_list) {
1212 display = tmp_display_list;
1213
1214 tmp_display_list = display->
next;
1215
1217 }
1218 }
1219
1220 }
1221
1222
1223 #ifdef DEBUG
1225 {
1229 uint32_t *clut_table;
1231 int x,
y, y_off, x_off;
1232 uint32_t *pbuf;
1233 char filename[32];
1234 static int fileno_index = 0;
1235
1236 x_pos = -1;
1237 y_pos = -1;
1238 width = 0;
1239 height = 0;
1240
1243
1244 if (x_pos == -1) {
1245 x_pos = display->
x_pos;
1246 y_pos = display->
y_pos;
1247 width = region->
width;
1249 } else {
1250 if (display->
x_pos < x_pos) {
1251 width += (x_pos - display->
x_pos);
1252 x_pos = display->
x_pos;
1253 }
1254
1255 if (display->
y_pos < y_pos) {
1256 height += (y_pos - display->
y_pos);
1257 y_pos = display->
y_pos;
1258 }
1259
1260 if (display->
x_pos + region->
width > x_pos + width) {
1261 width = display->
x_pos + region->
width - x_pos;
1262 }
1263
1264 if (display->
y_pos + region->
height > y_pos + height) {
1266 }
1267 }
1268 }
1269
1270 if (x_pos >= 0) {
1271
1273
1276
1277 x_off = display->
x_pos - x_pos;
1278 y_off = display->
y_pos - y_pos;
1279
1281
1282 if (clut == 0)
1284
1285 switch (region->
depth) {
1286 case 2:
1287 clut_table = clut->
clut4;
1288 break;
1289 case 8:
1291 break;
1292 case 4:
1293 default:
1294 clut_table = clut->
clut16;
1295 break;
1296 }
1297
1298 for (y = 0; y < region->
height; y++) {
1299 for (x = 0; x < region->
width; x++) {
1300 pbuf[((y + y_off) * width) + x_off + x] =
1301 clut_table[region->
pbuf[y * region->
width + x]];
1302 }
1303 }
1304
1305 }
1306
1307 snprintf(filename,
sizeof(filename),
"dvbs.%d", fileno_index);
1308
1309 png_save2(filename, pbuf, width, height);
1310
1312 }
1313
1314 fileno_index++;
1315 }
1316 #endif
1317
1320 int buf_size)
1321 {
1324 int dds_version, info_byte;
1325
1326 if (buf_size < 5)
1327 return;
1328
1329 info_byte = bytestream_get_byte(&buf);
1330 dds_version = info_byte >> 4;
1331 if (display_def && display_def->
version == dds_version)
1332 return; // already have this display definition version
1333
1334 if (!display_def) {
1335 display_def =
av_mallocz(
sizeof(*display_def));
1337 }
1338 if (!display_def)
1339 return;
1340
1341 display_def->
version = dds_version;
1344 display_def->
width = bytestream_get_be16(&buf) + 1;
1345 display_def->
height = bytestream_get_be16(&buf) + 1;
1349 }
1350
1351 if (buf_size < 13)
1352 return;
1353
1354 if (info_byte & 1<<3) { // display_window_flag
1355 display_def->
x = bytestream_get_be16(&buf);
1356 display_def->
y = bytestream_get_be16(&buf);
1357 display_def->
width = bytestream_get_be16(&buf) - display_def->
x + 1;
1358 display_def->
height = bytestream_get_be16(&buf) - display_def->
y + 1;
1359 }
1360 }
1361
1364 {
1367
1372 uint32_t *clut_table;
1373 int i;
1374 int offset_x=0, offset_y=0;
1375
1377
1378 if (display_def) {
1379 offset_x = display_def->
x;
1380 offset_y = display_def->
y;
1381 }
1382
1384
1389
1390 i = 0;
1391
1394
1395 if (!region)
1396 continue;
1397
1399 continue;
1400
1401 rect = sub->
rects[i];
1402 rect->
x = display->
x_pos + offset_x;
1403 rect->
y = display->
y_pos + offset_y;
1409
1411
1412 if (!clut)
1414
1415 switch (region->
depth) {
1416 case 2:
1417 clut_table = clut->
clut4;
1418 break;
1419 case 8:
1421 break;
1422 case 4:
1423 default:
1424 clut_table = clut->
clut16;
1425 break;
1426 }
1427
1429 memcpy(rect->
pict.
data[1], clut_table, (1 << region->
depth) *
sizeof(uint32_t));
1430
1433
1434 i++;
1435 }
1436
1438 }
1439 #ifdef DEBUG
1440 save_display_set(ctx);
1441 #endif
1442
1443 return 1;
1444 }
1445
1447 void *
data,
int *data_size,
1449 {
1451 int buf_size = avpkt->
size;
1455 int segment_type;
1456 int page_id;
1457 int segment_length;
1458 int i;
1459 int got_segment = 0;
1460
1461 av_dlog(avctx,
"DVB sub packet:\n");
1462
1463 for (i=0; i < buf_size; i++) {
1464 av_dlog(avctx,
"%02x ", buf[i]);
1465 if (i % 16 == 15)
1467 }
1468
1469 if (i % 16)
1471
1472 if (buf_size <= 6 || *buf != 0x0f) {
1473 av_dlog(avctx,
"incomplete or broken packet");
1474 return -1;
1475 }
1476
1478 p_end = buf + buf_size;
1479
1480 while (p_end - p >= 6 && *p == 0x0f) {
1481 p += 1;
1482 segment_type = *p++;
1484 p += 2;
1486 p += 2;
1487
1488 if (p_end - p < segment_length) {
1489 av_dlog(avctx,
"incomplete or broken packet");
1490 return -1;
1491 }
1492
1495 switch (segment_type) {
1498 got_segment |= 1;
1499 break;
1502 got_segment |= 2;
1503 break;
1506 got_segment |= 4;
1507 break;
1510 got_segment |= 8;
1511 break;
1514 break;
1517 got_segment |= 16;
1518 break;
1519 default:
1520 av_dlog(avctx,
"Subtitling segment type 0x%x, page id %d, length %d\n",
1521 segment_type, page_id, segment_length);
1522 break;
1523 }
1524 }
1525
1526 p += segment_length;
1527 }
1528 // Some streams do not send a display segment but if we have all the other
1529 // segments then we need no further data.
1530 if (got_segment == 15 && sub)
1532
1534 }
1535
1536
1546 };