forked from FFmpeg/FFmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvbsubdec.c
1756 lines (1435 loc) · 49.5 KB
/
dvbsubdec.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* DVB subtitle decoding
* Copyright (c) 2005 Ian Caulfield
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "avcodec.h"
#include "get_bits.h"
#include "bytestream.h"
#include "internal.h"
#include "libavutil/colorspace.h"
#include "libavutil/imgutils.h"
#include "libavutil/opt.h"
#define DVBSUB_PAGE_SEGMENT 0x10
#define DVBSUB_REGION_SEGMENT 0x11
#define DVBSUB_CLUT_SEGMENT 0x12
#define DVBSUB_OBJECT_SEGMENT 0x13
#define DVBSUB_DISPLAYDEFINITION_SEGMENT 0x14
#define DVBSUB_DISPLAY_SEGMENT 0x80
#define cm (ff_crop_tab + MAX_NEG_CROP)
#define RGBA(r,g,b,a) (((unsigned)(a) << 24) | ((r) << 16) | ((g) << 8) | (b))
typedef struct DVBSubCLUT {
int id;
int version;
uint32_t clut4[4];
uint32_t clut16[16];
uint32_t clut256[256];
struct DVBSubCLUT *next;
} DVBSubCLUT;
static DVBSubCLUT default_clut;
typedef struct DVBSubObjectDisplay {
int object_id;
int region_id;
int x_pos;
int y_pos;
int fgcolor;
int bgcolor;
struct DVBSubObjectDisplay *region_list_next;
struct DVBSubObjectDisplay *object_list_next;
} DVBSubObjectDisplay;
typedef struct DVBSubObject {
int id;
int version;
int type;
DVBSubObjectDisplay *display_list;
struct DVBSubObject *next;
} DVBSubObject;
typedef struct DVBSubRegionDisplay {
int region_id;
int x_pos;
int y_pos;
struct DVBSubRegionDisplay *next;
} DVBSubRegionDisplay;
typedef struct DVBSubRegion {
int id;
int version;
int width;
int height;
int depth;
int clut;
int bgcolor;
uint8_t computed_clut[4*256];
int has_computed_clut;
uint8_t *pbuf;
int buf_size;
int dirty;
DVBSubObjectDisplay *display_list;
struct DVBSubRegion *next;
} DVBSubRegion;
typedef struct DVBSubDisplayDefinition {
int version;
int x;
int y;
int width;
int height;
} DVBSubDisplayDefinition;
typedef struct DVBSubContext {
AVClass *class;
int composition_id;
int ancillary_id;
int version;
int time_out;
int compute_edt; /**< if 1 end display time calculated using pts
if 0 (Default) calculated using time out */
int compute_clut;
int clut_count2[257][256];
int substream;
int64_t prev_start;
DVBSubRegion *region_list;
DVBSubCLUT *clut_list;
DVBSubObject *object_list;
DVBSubRegionDisplay *display_list;
DVBSubDisplayDefinition *display_definition;
} DVBSubContext;
static DVBSubObject* get_object(DVBSubContext *ctx, int object_id)
{
DVBSubObject *ptr = ctx->object_list;
while (ptr && ptr->id != object_id) {
ptr = ptr->next;
}
return ptr;
}
static DVBSubCLUT* get_clut(DVBSubContext *ctx, int clut_id)
{
DVBSubCLUT *ptr = ctx->clut_list;
while (ptr && ptr->id != clut_id) {
ptr = ptr->next;
}
return ptr;
}
static DVBSubRegion* get_region(DVBSubContext *ctx, int region_id)
{
DVBSubRegion *ptr = ctx->region_list;
while (ptr && ptr->id != region_id) {
ptr = ptr->next;
}
return ptr;
}
static void delete_region_display_list(DVBSubContext *ctx, DVBSubRegion *region)
{
DVBSubObject *object, *obj2, **obj2_ptr;
DVBSubObjectDisplay *display, *obj_disp, **obj_disp_ptr;
while (region->display_list) {
display = region->display_list;
object = get_object(ctx, display->object_id);
if (object) {
obj_disp_ptr = &object->display_list;
obj_disp = *obj_disp_ptr;
while (obj_disp && obj_disp != display) {
obj_disp_ptr = &obj_disp->object_list_next;
obj_disp = *obj_disp_ptr;
}
if (obj_disp) {
*obj_disp_ptr = obj_disp->object_list_next;
if (!object->display_list) {
obj2_ptr = &ctx->object_list;
obj2 = *obj2_ptr;
while (obj2 != object) {
av_assert0(obj2);
obj2_ptr = &obj2->next;
obj2 = *obj2_ptr;
}
*obj2_ptr = obj2->next;
av_freep(&obj2);
}
}
}
region->display_list = display->region_list_next;
av_freep(&display);
}
}
static void delete_cluts(DVBSubContext *ctx)
{
while (ctx->clut_list) {
DVBSubCLUT *clut = ctx->clut_list;
ctx->clut_list = clut->next;
av_freep(&clut);
}
}
static void delete_objects(DVBSubContext *ctx)
{
while (ctx->object_list) {
DVBSubObject *object = ctx->object_list;
ctx->object_list = object->next;
av_freep(&object);
}
}
static void delete_regions(DVBSubContext *ctx)
{
while (ctx->region_list) {
DVBSubRegion *region = ctx->region_list;
ctx->region_list = region->next;
delete_region_display_list(ctx, region);
av_freep(®ion->pbuf);
av_freep(®ion);
}
}
static av_cold int dvbsub_init_decoder(AVCodecContext *avctx)
{
int i, r, g, b, a = 0;
DVBSubContext *ctx = avctx->priv_data;
if (ctx->substream < 0) {
ctx->composition_id = -1;
ctx->ancillary_id = -1;
} else if (!avctx->extradata || (avctx->extradata_size < 4) || ((avctx->extradata_size % 5 != 0) && (avctx->extradata_size != 4))) {
av_log(avctx, AV_LOG_WARNING, "Invalid DVB subtitles stream extradata!\n");
ctx->composition_id = -1;
ctx->ancillary_id = -1;
} else {
if (avctx->extradata_size > 5*ctx->substream + 2) {
ctx->composition_id = AV_RB16(avctx->extradata + 5*ctx->substream);
ctx->ancillary_id = AV_RB16(avctx->extradata + 5*ctx->substream + 2);
} else {
av_log(avctx, AV_LOG_WARNING, "Selected DVB subtitles sub-stream %d is not available\n", ctx->substream);
ctx->composition_id = AV_RB16(avctx->extradata);
ctx->ancillary_id = AV_RB16(avctx->extradata + 2);
}
}
ctx->version = -1;
ctx->prev_start = AV_NOPTS_VALUE;
default_clut.id = -1;
default_clut.next = NULL;
default_clut.clut4[0] = RGBA( 0, 0, 0, 0);
default_clut.clut4[1] = RGBA(255, 255, 255, 255);
default_clut.clut4[2] = RGBA( 0, 0, 0, 255);
default_clut.clut4[3] = RGBA(127, 127, 127, 255);
default_clut.clut16[0] = RGBA( 0, 0, 0, 0);
for (i = 1; i < 16; i++) {
if (i < 8) {
r = (i & 1) ? 255 : 0;
g = (i & 2) ? 255 : 0;
b = (i & 4) ? 255 : 0;
} else {
r = (i & 1) ? 127 : 0;
g = (i & 2) ? 127 : 0;
b = (i & 4) ? 127 : 0;
}
default_clut.clut16[i] = RGBA(r, g, b, 255);
}
default_clut.clut256[0] = RGBA( 0, 0, 0, 0);
for (i = 1; i < 256; i++) {
if (i < 8) {
r = (i & 1) ? 255 : 0;
g = (i & 2) ? 255 : 0;
b = (i & 4) ? 255 : 0;
a = 63;
} else {
switch (i & 0x88) {
case 0x00:
r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
a = 255;
break;
case 0x08:
r = ((i & 1) ? 85 : 0) + ((i & 0x10) ? 170 : 0);
g = ((i & 2) ? 85 : 0) + ((i & 0x20) ? 170 : 0);
b = ((i & 4) ? 85 : 0) + ((i & 0x40) ? 170 : 0);
a = 127;
break;
case 0x80:
r = 127 + ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
g = 127 + ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
b = 127 + ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
a = 255;
break;
case 0x88:
r = ((i & 1) ? 43 : 0) + ((i & 0x10) ? 85 : 0);
g = ((i & 2) ? 43 : 0) + ((i & 0x20) ? 85 : 0);
b = ((i & 4) ? 43 : 0) + ((i & 0x40) ? 85 : 0);
a = 255;
break;
}
}
default_clut.clut256[i] = RGBA(r, g, b, a);
}
return 0;
}
static av_cold int dvbsub_close_decoder(AVCodecContext *avctx)
{
DVBSubContext *ctx = avctx->priv_data;
DVBSubRegionDisplay *display;
delete_regions(ctx);
delete_objects(ctx);
delete_cluts(ctx);
av_freep(&ctx->display_definition);
while (ctx->display_list) {
display = ctx->display_list;
ctx->display_list = display->next;
av_freep(&display);
}
return 0;
}
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)
{
GetBitContext gb;
int bits;
int run_length;
int pixels_read = x_pos;
init_get_bits(&gb, *srcbuf, buf_size << 3);
destbuf += x_pos;
while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
bits = get_bits(&gb, 2);
if (bits) {
if (non_mod != 1 || bits != 1) {
if (map_table)
*destbuf++ = map_table[bits];
else
*destbuf++ = bits;
}
pixels_read++;
} else {
bits = get_bits1(&gb);
if (bits == 1) {
run_length = get_bits(&gb, 3) + 3;
bits = get_bits(&gb, 2);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else {
bits = get_bits1(&gb);
if (bits == 0) {
bits = get_bits(&gb, 2);
if (bits == 2) {
run_length = get_bits(&gb, 4) + 12;
bits = get_bits(&gb, 2);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 3) {
run_length = get_bits(&gb, 8) + 29;
bits = get_bits(&gb, 2);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 1) {
if (map_table)
bits = map_table[0];
else
bits = 0;
run_length = 2;
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
} else {
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
} else {
if (map_table)
bits = map_table[0];
else
bits = 0;
*destbuf++ = bits;
pixels_read++;
}
}
}
}
if (get_bits(&gb, 6))
av_log(avctx, AV_LOG_ERROR, "line overflow\n");
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
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)
{
GetBitContext gb;
int bits;
int run_length;
int pixels_read = x_pos;
init_get_bits(&gb, *srcbuf, buf_size << 3);
destbuf += x_pos;
while (get_bits_count(&gb) < buf_size << 3 && pixels_read < dbuf_len) {
bits = get_bits(&gb, 4);
if (bits) {
if (non_mod != 1 || bits != 1) {
if (map_table)
*destbuf++ = map_table[bits];
else
*destbuf++ = bits;
}
pixels_read++;
} else {
bits = get_bits1(&gb);
if (bits == 0) {
run_length = get_bits(&gb, 3);
if (run_length == 0) {
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
run_length += 2;
if (map_table)
bits = map_table[0];
else
bits = 0;
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
} else {
bits = get_bits1(&gb);
if (bits == 0) {
run_length = get_bits(&gb, 2) + 4;
bits = get_bits(&gb, 4);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else {
bits = get_bits(&gb, 2);
if (bits == 2) {
run_length = get_bits(&gb, 4) + 9;
bits = get_bits(&gb, 4);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 3) {
run_length = get_bits(&gb, 8) + 25;
bits = get_bits(&gb, 4);
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
} else if (bits == 1) {
if (map_table)
bits = map_table[0];
else
bits = 0;
run_length = 2;
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
} else {
if (map_table)
bits = map_table[0];
else
bits = 0;
*destbuf++ = bits;
pixels_read ++;
}
}
}
}
}
if (get_bits(&gb, 8))
av_log(avctx, AV_LOG_ERROR, "line overflow\n");
(*srcbuf) += (get_bits_count(&gb) + 7) >> 3;
return pixels_read;
}
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)
{
const uint8_t *sbuf_end = (*srcbuf) + buf_size;
int bits;
int run_length;
int pixels_read = x_pos;
destbuf += x_pos;
while (*srcbuf < sbuf_end && pixels_read < dbuf_len) {
bits = *(*srcbuf)++;
if (bits) {
if (non_mod != 1 || bits != 1) {
if (map_table)
*destbuf++ = map_table[bits];
else
*destbuf++ = bits;
}
pixels_read++;
} else {
bits = *(*srcbuf)++;
run_length = bits & 0x7f;
if ((bits & 0x80) == 0) {
if (run_length == 0) {
return pixels_read;
}
bits = 0;
} else {
bits = *(*srcbuf)++;
}
if (non_mod == 1 && bits == 1)
pixels_read += run_length;
else {
if (map_table)
bits = map_table[bits];
while (run_length-- > 0 && pixels_read < dbuf_len) {
*destbuf++ = bits;
pixels_read++;
}
}
}
}
if (*(*srcbuf)++)
av_log(avctx, AV_LOG_ERROR, "line overflow\n");
return pixels_read;
}
static void compute_default_clut(DVBSubContext *ctx, uint8_t *clut, AVSubtitleRect *rect, int w, int h)
{
uint8_t list[256] = {0};
uint8_t list_inv[256];
int counttab[256] = {0};
int (*counttab2)[256] = ctx->clut_count2;
int count, i, x, y;
ptrdiff_t stride = rect->linesize[0];
memset(ctx->clut_count2, 0 , sizeof(ctx->clut_count2));
#define V(x,y) rect->data[0][(x) + (y)*stride]
for (y = 0; y<h; y++) {
for (x = 0; x<w; x++) {
int v = V(x,y) + 1;
int vl = x ? V(x-1,y) + 1 : 0;
int vr = x+1<w ? V(x+1,y) + 1 : 0;
int vt = y ? V(x,y-1) + 1 : 0;
int vb = y+1<h ? V(x,y+1) + 1 : 0;
counttab[v-1] += !!((v!=vl) + (v!=vr) + (v!=vt) + (v!=vb));
counttab2[vl][v-1] ++;
counttab2[vr][v-1] ++;
counttab2[vt][v-1] ++;
counttab2[vb][v-1] ++;
}
}
#define L(x,y) list[d[(x) + (y)*stride]]
for (i = 0; i<256; i++) {
counttab2[i+1][i] = 0;
}
for (i = 0; i<256; i++) {
int bestscore = 0;
int bestv = 0;
for (x = 0; x < 256; x++) {
int scorev = 0;
if (list[x])
continue;
scorev += counttab2[0][x];
for (y = 0; y < 256; y++) {
scorev += list[y] * counttab2[y+1][x];
}
if (scorev) {
int score = 1024LL*scorev / counttab[x];
if (score > bestscore) {
bestscore = score;
bestv = x;
}
}
}
if (!bestscore)
break;
list [ bestv ] = 1;
list_inv[ i ] = bestv;
}
count = FFMAX(i - 1, 1);
for (i--; i>=0; i--) {
int v = i*255/count;
AV_WN32(clut + 4*list_inv[i], RGBA(v/2,v,v/2,v));
}
}
static int save_subtitle_set(AVCodecContext *avctx, AVSubtitle *sub, int *got_output)
{
DVBSubContext *ctx = avctx->priv_data;
DVBSubRegionDisplay *display;
DVBSubDisplayDefinition *display_def = ctx->display_definition;
DVBSubRegion *region;
AVSubtitleRect *rect;
DVBSubCLUT *clut;
uint32_t *clut_table;
int i;
int offset_x=0, offset_y=0;
int ret = 0;
if (display_def) {
offset_x = display_def->x;
offset_y = display_def->y;
}
/* Not touching AVSubtitles again*/
if(sub->num_rects) {
avpriv_request_sample(ctx, "Different Version of Segment asked Twice");
return AVERROR_PATCHWELCOME;
}
for (display = ctx->display_list; display; display = display->next) {
region = get_region(ctx, display->region_id);
if (region && region->dirty)
sub->num_rects++;
}
if(ctx->compute_edt == 0) {
sub->end_display_time = ctx->time_out * 1000;
*got_output = 1;
} else if (ctx->prev_start != AV_NOPTS_VALUE) {
sub->end_display_time = av_rescale_q((sub->pts - ctx->prev_start ), AV_TIME_BASE_Q, (AVRational){ 1, 1000 }) - 1;
*got_output = 1;
}
if (sub->num_rects > 0) {
sub->rects = av_mallocz_array(sizeof(*sub->rects), sub->num_rects);
if (!sub->rects) {
ret = AVERROR(ENOMEM);
goto fail;
}
for (i = 0; i < sub->num_rects; i++) {
sub->rects[i] = av_mallocz(sizeof(*sub->rects[i]));
if (!sub->rects[i]) {
ret = AVERROR(ENOMEM);
goto fail;
}
}
i = 0;
for (display = ctx->display_list; display; display = display->next) {
region = get_region(ctx, display->region_id);
if (!region)
continue;
if (!region->dirty)
continue;
rect = sub->rects[i];
rect->x = display->x_pos + offset_x;
rect->y = display->y_pos + offset_y;
rect->w = region->width;
rect->h = region->height;
rect->nb_colors = (1 << region->depth);
rect->type = SUBTITLE_BITMAP;
rect->linesize[0] = region->width;
clut = get_clut(ctx, region->clut);
if (!clut)
clut = &default_clut;
switch (region->depth) {
case 2:
clut_table = clut->clut4;
break;
case 8:
clut_table = clut->clut256;
break;
case 4:
default:
clut_table = clut->clut16;
break;
}
rect->data[1] = av_mallocz(AVPALETTE_SIZE);
if (!rect->data[1]) {
ret = AVERROR(ENOMEM);
goto fail;
}
memcpy(rect->data[1], clut_table, (1 << region->depth) * sizeof(uint32_t));
rect->data[0] = av_malloc(region->buf_size);
if (!rect->data[0]) {
ret = AVERROR(ENOMEM);
goto fail;
}
memcpy(rect->data[0], region->pbuf, region->buf_size);
if ((clut == &default_clut && ctx->compute_clut == -1) || ctx->compute_clut == 1) {
if (!region->has_computed_clut) {
compute_default_clut(ctx, region->computed_clut, rect, rect->w, rect->h);
region->has_computed_clut = 1;
}
memcpy(rect->data[1], region->computed_clut, sizeof(region->computed_clut));
}
#if FF_API_AVPICTURE
FF_DISABLE_DEPRECATION_WARNINGS
{
int j;
for (j = 0; j < 4; j++) {
rect->pict.data[j] = rect->data[j];
rect->pict.linesize[j] = rect->linesize[j];
}
}
FF_ENABLE_DEPRECATION_WARNINGS
#endif
i++;
}
}
return 0;
fail:
if (sub->rects) {
for(i=0; i<sub->num_rects; i++) {
rect = sub->rects[i];
if (rect) {
av_freep(&rect->data[0]);
av_freep(&rect->data[1]);
}
av_freep(&sub->rects[i]);
}
av_freep(&sub->rects);
}
sub->num_rects = 0;
return ret;
}
static void dvbsub_parse_pixel_data_block(AVCodecContext *avctx, DVBSubObjectDisplay *display,
const uint8_t *buf, int buf_size, int top_bottom, int non_mod)
{
DVBSubContext *ctx = avctx->priv_data;
DVBSubRegion *region = get_region(ctx, display->region_id);
const uint8_t *buf_end = buf + buf_size;
uint8_t *pbuf;
int x_pos, y_pos;
int i;
uint8_t map2to4[] = { 0x0, 0x7, 0x8, 0xf};
uint8_t map2to8[] = {0x00, 0x77, 0x88, 0xff};
uint8_t map4to8[] = {0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff};
uint8_t *map_table;
#if 0
ff_dlog(avctx, "DVB pixel block size %d, %s field:\n", buf_size,
top_bottom ? "bottom" : "top");
for (i = 0; i < buf_size; i++) {
if (i % 16 == 0)
ff_dlog(avctx, "0x%8p: ", buf+i);
ff_dlog(avctx, "%02x ", buf[i]);
if (i % 16 == 15)
ff_dlog(avctx, "\n");
}
if (i % 16)
ff_dlog(avctx, "\n");
#endif
if (!region)
return;
pbuf = region->pbuf;
region->dirty = 1;
x_pos = display->x_pos;
y_pos = display->y_pos;
y_pos += top_bottom;
while (buf < buf_end) {
if ((*buf!=0xf0 && x_pos >= region->width) || y_pos >= region->height) {
av_log(avctx, AV_LOG_ERROR, "Invalid object location! %d-%d %d-%d %02x\n", x_pos, region->width, y_pos, region->height, *buf);
return;
}
switch (*buf++) {
case 0x10:
if (region->depth == 8)
map_table = map2to8;
else if (region->depth == 4)
map_table = map2to4;
else
map_table = NULL;
x_pos = dvbsub_read_2bit_string(avctx, pbuf + (y_pos * region->width),
region->width, &buf, buf_end - buf,
non_mod, map_table, x_pos);
break;
case 0x11:
if (region->depth < 4) {
av_log(avctx, AV_LOG_ERROR, "4-bit pixel string in %d-bit region!\n", region->depth);
return;
}
if (region->depth == 8)
map_table = map4to8;
else
map_table = NULL;
x_pos = dvbsub_read_4bit_string(avctx, pbuf + (y_pos * region->width),
region->width, &buf, buf_end - buf,
non_mod, map_table, x_pos);
break;
case 0x12:
if (region->depth < 8) {
av_log(avctx, AV_LOG_ERROR, "8-bit pixel string in %d-bit region!\n", region->depth);
return;
}
x_pos = dvbsub_read_8bit_string(avctx, pbuf + (y_pos * region->width),
region->width, &buf, buf_end - buf,
non_mod, NULL, x_pos);
break;
case 0x20:
map2to4[0] = (*buf) >> 4;
map2to4[1] = (*buf++) & 0xf;
map2to4[2] = (*buf) >> 4;
map2to4[3] = (*buf++) & 0xf;
break;
case 0x21:
for (i = 0; i < 4; i++)
map2to8[i] = *buf++;
break;
case 0x22:
for (i = 0; i < 16; i++)
map4to8[i] = *buf++;
break;
case 0xf0:
x_pos = display->x_pos;
y_pos += 2;
break;
default:
av_log(avctx, AV_LOG_INFO, "Unknown/unsupported pixel block 0x%x\n", *(buf-1));
}
}
region->has_computed_clut = 0;
}
static int dvbsub_parse_object_segment(AVCodecContext *avctx,
const uint8_t *buf, int buf_size)
{
DVBSubContext *ctx = avctx->priv_data;
const uint8_t *buf_end = buf + buf_size;
int object_id;
DVBSubObject *object;
DVBSubObjectDisplay *display;
int top_field_len, bottom_field_len;
int coding_method, non_modifying_color;
object_id = AV_RB16(buf);
buf += 2;