/
load_text.c
3679 lines (3251 loc) · 113 KB
/
load_text.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2000-2021
* All rights reserved
*
* This file is part of GPAC / text import filter
*
* GPAC 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, or (at your option)
* any later version.
*
* GPAC 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 this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <gpac/filters.h>
#include <gpac/constants.h>
#include <gpac/utf.h>
#include <gpac/xml.h>
#include <gpac/token.h>
#include <gpac/color.h>
#include <gpac/internal/media_dev.h>
#include <gpac/internal/isomedia_dev.h>
#ifndef GPAC_DISABLE_SWF_IMPORT
/* SWF Importer */
#include <gpac/internal/swf_dev.h>
#endif
#if !defined(GPAC_DISABLE_ISOM_WRITE) && !defined(GPAC_DISABLE_CORE_TOOLS)
#define TTML_NAMESPACE "http://www.w3.org/ns/ttml"
#define CHECK_STR(__str) \
if (!__str) { \
e = gf_import_message(import, GF_BAD_PARAM, "Invalid XML formatting (line %d)", parser.line); \
goto exit; \
}
typedef struct __txtin_ctx GF_TXTIn;
struct __txtin_ctx
{
//opts
u32 width, height, txtx, txty, fontsize;
s32 zorder;
const char *fontname, *lang, *ttml_zero;
Bool nodefbox, noflush, webvtt, ttml_embed;
u32 timescale;
GF_Fraction fps;
s32 ttml_dur;
GF_FilterPid *ipid, *opid;
char *file_name;
u32 fmt;
u32 playstate;
//0: not seeking, 1: seek request pending, 2: seek configured, discarding packets up until start_range
u32 seek_state;
Double start_range;
Bool is_setup;
GF_Err (*text_process)(GF_Filter *filter, GF_TXTIn *ctx);
s32 unicode_type;
FILE *src;
GF_BitStream *bs_w;
Bool first_samp;
Bool hdr_parsed;
//state vars for srt
u32 state, default_color;
GF_TextSample *samp;
u64 start, end, prev_end;
u32 curLine;
GF_StyleRecord style;
//WebVTT state
GF_WebVTTParser *vttparser;
//TTXT state
GF_DOMParser *parser;
u32 cur_child_idx, nb_children, last_desc_idx;
GF_List *text_descs;
Bool last_sample_empty;
u64 last_sample_duration;
//TTML state is the same as ttxt plus the timescale and start (webvtt) for cts compute
u32 txml_timescale;
u32 current_tt_interval;
//TTML state
GF_XMLNode *root_working_copy, *body_node;
GF_DOMParser *parser_working_copy;
Bool non_compliant_ttml;
u32 tick_rate, ttml_fps_num, ttml_fps_den, ttml_sfps;
GF_List *ttml_resources;
GF_List *div_nodes_list;
Bool has_images;
#ifndef GPAC_DISABLE_SWF_IMPORT
//SWF text
SWFReader *swf_parse;
Bool do_suspend;
#endif
GF_List *intervals;
};
typedef struct
{
u32 size;
u8 *data;
Bool global;
} TTMLRes;
typedef struct
{
s64 begin, end;
GF_List *resources;
} TTMLInterval;
enum
{
GF_TXTIN_MODE_NONE = 0,
GF_TXTIN_MODE_SRT,
GF_TXTIN_MODE_SUB,
GF_TXTIN_MODE_TTXT,
GF_TXTIN_MODE_TEXML,
GF_TXTIN_MODE_WEBVTT,
GF_TXTIN_MODE_TTML,
GF_TXTIN_MODE_SWF_SVG,
};
#define REM_TRAIL_MARKS(__str, __sep) while (1) { \
u32 _len = (u32) strlen(__str); \
if (!_len) break; \
_len--; \
if (strchr(__sep, __str[_len])) __str[_len] = 0; \
else break; \
} \
s32 gf_text_get_utf_type(FILE *in_src)
{
u32 read;
unsigned char BOM[5];
read = (u32) gf_fread(BOM, 5, in_src);
if ((s32) read < 1)
return -1;
if ((BOM[0]==0xFF) && (BOM[1]==0xFE)) {
/*UTF32 not supported*/
if (!BOM[2] && !BOM[3]) return -1;
gf_fseek(in_src, 2, SEEK_SET);
return 3;
}
if ((BOM[0]==0xFE) && (BOM[1]==0xFF)) {
/*UTF32 not supported*/
if (!BOM[2] && !BOM[3]) return -1;
gf_fseek(in_src, 2, SEEK_SET);
return 2;
} else if ((BOM[0]==0xEF) && (BOM[1]==0xBB) && (BOM[2]==0xBF)) {
gf_fseek(in_src, 3, SEEK_SET);
return 1;
}
if (BOM[0]<0x80) {
gf_fseek(in_src, 0, SEEK_SET);
return 0;
}
return -1;
}
static void ttxt_dom_progress(void *cbk, u64 cur_samp, u64 count)
{
GF_TXTIn *ctx = (GF_TXTIn *)cbk;
ctx->end = count;
}
static GF_Err gf_text_guess_format(const char *filename, u32 *fmt)
{
char szLine[2048];
u32 val;
s32 uni_type;
FILE *test = gf_fopen(filename, "rb");
if (!test) return GF_URL_ERROR;
uni_type = gf_text_get_utf_type(test);
if (uni_type>1) {
const u16 *sptr;
char szUTF[1024];
u32 read = (u32) gf_fread(szUTF, 1023, test);
if ((s32) read < 0) {
gf_fclose(test);
return GF_IO_ERR;
}
szUTF[read]=0;
sptr = (u16*)szUTF;
/*read = (u32) */gf_utf8_wcstombs(szLine, read, &sptr);
} else {
val = (u32) gf_fread(szLine, 1024, test);
if ((s32) val<0) return GF_IO_ERR;
szLine[val]=0;
}
REM_TRAIL_MARKS(szLine, "\r\n\t ")
*fmt = GF_TXTIN_MODE_NONE;
if ((szLine[0]=='{') && strstr(szLine, "}{")) *fmt = GF_TXTIN_MODE_SUB;
else if (szLine[0] == '<') {
char *ext = gf_file_ext_start(filename);
if (!strnicmp(ext, ".ttxt", 5)) *fmt = GF_TXTIN_MODE_TTXT;
else if (!strnicmp(ext, ".ttml", 5)) *fmt = GF_TXTIN_MODE_TTML;
ext = strstr(szLine, "?>");
if (ext) ext += 2;
if (ext && !ext[0]) {
if (!gf_fgets(szLine, 2048, test))
szLine[0] = '\0';
}
if (strstr(szLine, "x-quicktime-tx3g") || strstr(szLine, "text3GTrack")) *fmt = GF_TXTIN_MODE_TEXML;
else if (strstr(szLine, "TextStream")) *fmt = GF_TXTIN_MODE_TTXT;
else if (strstr(szLine, "tt")) *fmt = GF_TXTIN_MODE_TTML;
}
else if (strstr(szLine, "WEBVTT") )
*fmt = GF_TXTIN_MODE_WEBVTT;
else if (strstr(szLine, " --> ") )
*fmt = GF_TXTIN_MODE_SRT; /* might want to change the default to WebVTT */
else if (!strncmp(szLine, "FWS", 3) || !strncmp(szLine, "CWS", 3))
*fmt = GF_TXTIN_MODE_SWF_SVG;
gf_fclose(test);
return GF_OK;
}
char *gf_text_get_utf8_line(char *szLine, u32 lineSize, FILE *txt_in, s32 unicode_type)
{
u32 i, j, len;
char *sOK;
char szLineConv[2048];
unsigned short *sptr;
memset(szLine, 0, sizeof(char)*lineSize);
sOK = gf_fgets(szLine, lineSize, txt_in);
if (!sOK) return NULL;
if (unicode_type<=1) {
j=0;
len = (u32) strlen(szLine);
for (i=0; i<len; i++) {
if (!unicode_type && (szLine[i] & 0x80)) {
/*non UTF8 (likely some win-CP)*/
if ((szLine[i+1] & 0xc0) != 0x80) {
szLineConv[j] = 0xc0 | ( (szLine[i] >> 6) & 0x3 );
j++;
szLine[i] &= 0xbf;
}
/*UTF8 2 bytes char*/
else if ( (szLine[i] & 0xe0) == 0xc0) {
szLineConv[j] = szLine[i];
i++;
j++;
}
/*UTF8 3 bytes char*/
else if ( (szLine[i] & 0xf0) == 0xe0) {
szLineConv[j] = szLine[i];
i++;
j++;
szLineConv[j] = szLine[i];
i++;
j++;
}
/*UTF8 4 bytes char*/
else if ( (szLine[i] & 0xf8) == 0xf0) {
szLineConv[j] = szLine[i];
i++;
j++;
szLineConv[j] = szLine[i];
i++;
j++;
szLineConv[j] = szLine[i];
i++;
j++;
} else {
i+=1;
continue;
}
}
szLineConv[j] = szLine[i];
j++;
}
szLineConv[j] = 0;
strcpy(szLine, szLineConv);
return sOK;
}
#ifdef GPAC_BIG_ENDIAN
if (unicode_type==3)
#else
if (unicode_type==2)
#endif
{
i=0;
while (1) {
char c;
if (!szLine[i] && !szLine[i+1]) break;
c = szLine[i+1];
szLine[i+1] = szLine[i];
szLine[i] = c;
i+=2;
}
}
sptr = (u16 *)szLine;
i = gf_utf8_wcstombs(szLineConv, 2048, (const unsigned short **) &sptr);
if (i == GF_UTF8_FAIL) i = 0;
szLineConv[i] = 0;
strcpy(szLine, szLineConv);
/*this is ugly indeed: since input is UTF16-LE, there are many chances the gf_fgets never reads the \0 after a \n*/
if (unicode_type==3) gf_fgetc(txt_in);
return sOK;
}
static void txtin_probe_duration(GF_TXTIn *ctx)
{
GF_Fraction64 dur;
dur.num = 0;
if (ctx->fmt == GF_TXTIN_MODE_SWF_SVG) {
#ifndef GPAC_DISABLE_SWF_IMPORT
u32 frame_count, frame_rate;
gf_swf_get_duration(ctx->swf_parse, &frame_rate, &frame_count);
if (frame_count) {
GF_Fraction64 tdur;
tdur.num = frame_count;
tdur.den = frame_rate;
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, &PROP_FRAC64(tdur));
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_PLAYBACK_MODE, &PROP_UINT(GF_PLAYBACK_MODE_FASTFORWARD ) );
}
#endif
return;
}
if ((ctx->fmt == GF_TXTIN_MODE_SRT) || (ctx->fmt == GF_TXTIN_MODE_WEBVTT) || (ctx->fmt == GF_TXTIN_MODE_SUB)) {
u64 pos = gf_ftell(ctx->src);
gf_fseek(ctx->src, 0, SEEK_SET);
while (!gf_feof(ctx->src)) {
u64 end;
char szLine[2048];
char *sOK = gf_text_get_utf8_line(szLine, 2048, ctx->src, ctx->unicode_type);
if (!sOK) break;
REM_TRAIL_MARKS(szLine, "\r\n\t ")
if (ctx->fmt == GF_TXTIN_MODE_SUB) {
char szText[2048];
u32 sframe, eframe;
if (sscanf(szLine, "{%d}{%d}%2047s", &sframe, &eframe, szText) == 3) {
if (ctx->fps.den)
end = 1000 * eframe * ctx->fps.num / ctx->fps.den;
else
end = 1000 * eframe / 25;
if (end > (u64) dur.num) dur.num = (s64) end;
}
} else {
u32 eh, em, es, ems;
char *start = strstr(szLine, "-->");
if (!start) continue;
while (start[0] && ((start[0] == ' ') || (start[0] == '\t'))) start++;
if (sscanf(start, "%u:%u:%u,%u", &eh, &em, &es, &ems) != 4) {
eh = 0;
if (sscanf(szLine, "%u:%u,%u", &em, &es, &ems) != 3) {
continue;
}
}
end = (3600*eh + 60*em + es)*1000 + ems;
if (end > (u64) dur.num) dur.num = (s64) end;
}
}
gf_fseek(ctx->src, pos, SEEK_SET);
if (dur.num) {
dur.den = 1000;
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, &PROP_FRAC64(dur));
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_PLAYBACK_MODE, &PROP_UINT(GF_PLAYBACK_MODE_FASTFORWARD ) );
}
return;
}
if ((ctx->fmt == GF_TXTIN_MODE_TTXT) || (ctx->fmt == GF_TXTIN_MODE_TEXML)) {
u32 i=0;
GF_XMLNode *node, *root = gf_xml_dom_get_root(ctx->parser);
while ((node = gf_list_enum(root->content, &i))) {
u32 j;
u64 duration;
GF_XMLAttribute *att;
if (node->type) {
continue;
}
/*sample text*/
if ((ctx->fmt == GF_TXTIN_MODE_TTXT) && strcmp(node->name, "TextSample")) continue;
else if ((ctx->fmt == GF_TXTIN_MODE_TEXML) && strcmp(node->name, "sample")) continue;
j=0;
while ( (att=(GF_XMLAttribute*)gf_list_enum(node->attributes, &j))) {
u32 h, m, s, ms;
u64 ts=0;
if (ctx->fmt == GF_TXTIN_MODE_TTXT) {
if (strcmp(att->name, "sampleTime")) continue;
if (sscanf(att->value, "%u:%u:%u.%u", &h, &m, &s, &ms) == 4) {
ts = (h*3600 + m*60 + s)*1000 + ms;
} else {
ts = (u32) (atof(att->value) * 1000);
}
if (ts > (u64) dur.num) dur.num = (s64) ts;
} else {
if (strcmp(att->name, "duration")) continue;
duration = atoi(att->value);
dur.num += (s32) ( (1000 * duration) / ctx->txml_timescale);
}
}
}
if (dur.num) {
dur.den = 1000;
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, &PROP_FRAC64(dur));
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_PLAYBACK_MODE, &PROP_UINT(GF_PLAYBACK_MODE_FASTFORWARD ) );
}
return;
}
if (ctx->fmt == GF_TXTIN_MODE_TTML) {
u32 i=0, k=0;
GF_XMLNode *div_node;
while ((div_node = gf_list_enum(ctx->div_nodes_list, &k))) {
GF_XMLNode *node;
while ((node = gf_list_enum(div_node->content, &i))) {
GF_XMLNode *p_node;
GF_XMLAttribute *att;
u32 h, m, s, ms, p_idx=0;
u64 ts_end=0;
h = m = s = ms = 0;
while ( (att = (GF_XMLAttribute*)gf_list_enum(node->attributes, &p_idx))) {
if (strcmp(att->name, "end")) continue;
if (sscanf(att->value, "%u:%u:%u.%u", &h, &m, &s, &ms) == 4) {
ts_end = (h*3600 + m*60+s)*1000+ms;
} else if (sscanf(att->value, "%u:%u:%u", &h, &m, &s) == 3) {
ts_end = (h*3600 + m*60+s)*1000;
}
}
//or under a <span>
p_idx = 0;
while ( (p_node = (GF_XMLNode*)gf_list_enum(node->content, &p_idx))) {
u32 span_idx = 0;
while ( (att = (GF_XMLAttribute*)gf_list_enum(p_node->attributes, &span_idx))) {
if (strcmp(att->name, "end")) continue;
if (sscanf(att->value, "%u:%u:%u.%u", &h, &m, &s, &ms) == 4) {
ts_end = (h*3600 + m*60+s)*1000+ms;
} else if (sscanf(att->value, "%u:%u:%u", &h, &m, &s) == 3) {
ts_end = (h*3600 + m*60+s)*1000;
}
}
}
if (ts_end > (u64) dur.num) dur.num = (s64) ts_end;
}
}
if (dur.num) {
dur.den = 1000;
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, &PROP_FRAC64(dur));
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_PLAYBACK_MODE, &PROP_UINT(GF_PLAYBACK_MODE_FASTFORWARD ) );
}
return;
}
GF_LOG(GF_LOG_WARNING, GF_LOG_PARSER, ("[TXTIn] Duration probing not supported for format %d\n", ctx->fmt));
}
static GF_Err txtin_setup_srt(GF_Filter *filter, GF_TXTIn *ctx)
{
u32 ID, OCR_ES_ID, dsi_len, file_size;
u8 *dsi;
GF_TextSampleDescriptor *sd;
ctx->src = gf_fopen(ctx->file_name, "rb");
if (!ctx->src) return GF_URL_ERROR;
file_size = (u32) gf_fsize(ctx->src);
ctx->unicode_type = gf_text_get_utf_type(ctx->src);
if (ctx->unicode_type<0) {
gf_fclose(ctx->src);
ctx->src = NULL;
GF_LOG(GF_LOG_ERROR, GF_LOG_PARSER, ("[TXTIn] Unsupported SRT UTF encoding\n"));
return GF_NOT_SUPPORTED;
}
if (!ctx->timescale) ctx->timescale = 1000;
OCR_ES_ID = ID = 0;
if (!ctx->opid) ctx->opid = gf_filter_pid_new(filter);
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_STREAM_TYPE, &PROP_UINT(GF_STREAM_TEXT) );
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_CODECID, &PROP_UINT(GF_CODECID_TX3G) );
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_TIMESCALE, &PROP_UINT(ctx->timescale) );
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DOWN_SIZE, &PROP_LONGUINT(file_size) );
if (!ID) ID = 1;
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_ID, &PROP_UINT(ID) );
if (OCR_ES_ID) gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_CLOCK_ID, &PROP_UINT(OCR_ES_ID) );
if (ctx->width) gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_WIDTH, &PROP_UINT(ctx->width) );
if (ctx->height) gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_HEIGHT, &PROP_UINT(ctx->height) );
if (ctx->zorder) gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_ZORDER, &PROP_SINT(ctx->zorder) );
if (ctx->lang) gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_LANGUAGE, &PROP_STRING( ctx->lang) );
sd = (GF_TextSampleDescriptor*)gf_odf_desc_new(GF_ODF_TX3G_TAG);
sd->fonts = (GF_FontRecord*)gf_malloc(sizeof(GF_FontRecord));
sd->font_count = 1;
sd->fonts[0].fontID = 1;
sd->fonts[0].fontName = gf_strdup(ctx->fontname ? ctx->fontname : "Serif");
sd->back_color = 0x00000000; /*transparent*/
sd->default_style.fontID = 1;
sd->default_style.font_size = ctx->fontsize;
sd->default_style.text_color = 0xFFFFFFFF; /*white*/
sd->default_style.style_flags = 0;
sd->horiz_justif = 1; /*center of scene*/
sd->vert_justif = (s8) -1; /*bottom of scene*/
if (ctx->nodefbox) {
sd->default_pos.top = sd->default_pos.left = sd->default_pos.right = sd->default_pos.bottom = 0;
} else if ((sd->default_pos.bottom==sd->default_pos.top) || (sd->default_pos.right==sd->default_pos.left)) {
sd->default_pos.left = ctx->txtx;
sd->default_pos.top = ctx->txty;
sd->default_pos.right = ctx->width + sd->default_pos.left;
sd->default_pos.bottom = ctx->height + sd->default_pos.top;
}
/*store attribs*/
ctx->style = sd->default_style;
gf_odf_tx3g_write(sd, &dsi, &dsi_len);
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DECODER_CONFIG, &PROP_DATA_NO_COPY(dsi, dsi_len) );
gf_odf_desc_del((GF_Descriptor *)sd);
ctx->default_color = ctx->style.text_color;
ctx->samp = gf_isom_new_text_sample();
ctx->state = 0;
ctx->end = ctx->prev_end = ctx->start = 0;
ctx->first_samp = GF_TRUE;
ctx->curLine = 0;
txtin_probe_duration(ctx);
return GF_OK;
}
static void txtin_process_send_text_sample(GF_TXTIn *ctx, GF_TextSample *txt_samp, u64 ts, u32 duration, Bool is_rap)
{
GF_FilterPacket *dst_pck;
u8 *pck_data;
u32 size;
if (ctx->seek_state==2) {
Double end = (Double) (ts+duration);
end /= 1000;
if (end < ctx->start_range) return;
ctx->seek_state = 0;
}
size = gf_isom_text_sample_size(txt_samp);
dst_pck = gf_filter_pck_new_alloc(ctx->opid, size, &pck_data);
if (!dst_pck) return;
gf_bs_reassign_buffer(ctx->bs_w, pck_data, size);
gf_isom_text_sample_write_bs(txt_samp, ctx->bs_w);
ts = gf_timestamp_rescale(ts, 1000, ctx->timescale);
duration = (u32) gf_timestamp_rescale(duration, 1000, ctx->timescale);
gf_filter_pck_set_sap(dst_pck, is_rap ? GF_FILTER_SAP_1 : GF_FILTER_SAP_NONE);
gf_filter_pck_set_cts(dst_pck, ts);
gf_filter_pck_set_duration(dst_pck, duration);
gf_filter_pck_send(dst_pck);
}
static GF_Err txtin_process_srt(GF_Filter *filter, GF_TXTIn *ctx)
{
u32 i;
u32 sh, sm, ss, sms, eh, em, es, ems, txt_line, char_len, char_line, j, rem_styles;
Bool set_start_char, set_end_char, rem_color;
u32 line, len;
char szLine[2048], szText[2048], *ptr;
unsigned short uniLine[5000], uniText[5000], *sptr;
if (!ctx->is_setup) {
ctx->is_setup = GF_TRUE;
return txtin_setup_srt(filter, ctx);
}
if (!ctx->opid) return GF_NOT_SUPPORTED;
if (!ctx->playstate) return GF_OK;
else if (ctx->playstate==2) return GF_EOS;
txt_line = 0;
set_start_char = set_end_char = GF_FALSE;
char_len = 0;
if (ctx->seek_state == 1) {
ctx->seek_state = 2;
gf_fseek(ctx->src, 0, SEEK_SET);
}
while (1) {
Bool is_empty = GF_FALSE;
char *sOK = gf_text_get_utf8_line(szLine, 2048, ctx->src, ctx->unicode_type);
if (sOK) {
REM_TRAIL_MARKS(szLine, "\r\n\t ")
if (ctx->unicode_type<=1) is_empty = strlen(szLine) ? GF_FALSE : GF_TRUE;
else is_empty = (!szLine[0] && !szLine[1]) ? GF_TRUE : GF_FALSE;
}
if (!sOK || is_empty) {
u32 utf_inc = (ctx->unicode_type<=1) ? 1 : 2;
u32 nb_empty = utf_inc;
u32 pos = (u32) gf_ftell(ctx->src);
if (ctx->state) {
while (!gf_feof(ctx->src)) {
sOK = gf_text_get_utf8_line(szLine+nb_empty, 2048-nb_empty, ctx->src, ctx->unicode_type);
if (sOK) REM_TRAIL_MARKS((szLine+nb_empty), "\r\n\t ")
if (!sOK) {
gf_fseek(ctx->src, pos, SEEK_SET);
break;
} else if (!strlen(szLine+nb_empty)) {
nb_empty+=utf_inc;
continue;
} else if ( sscanf(szLine+nb_empty, "%u", &line) == 1) {
gf_fseek(ctx->src, pos, SEEK_SET);
break;
} else {
u32 k;
for (k=0; k<nb_empty; k++) szLine[k] = '\n';
goto force_line;
}
}
}
ctx->style.style_flags = 0;
ctx->style.text_color = 0xFFFFFFFF;
ctx->style.startCharOffset = ctx->style.endCharOffset = 0;
if (txt_line) {
if (ctx->prev_end && (ctx->start != ctx->prev_end) && (ctx->state<=2)) {
GF_TextSample * empty_samp = gf_isom_new_text_sample();
txtin_process_send_text_sample(ctx, empty_samp, ctx->prev_end, (u32) (ctx->start - ctx->prev_end), GF_TRUE );
gf_isom_delete_text_sample(empty_samp);
}
if (ctx->state<=2) {
txtin_process_send_text_sample(ctx, ctx->samp, ctx->start, (u32) (ctx->end - ctx->start), GF_TRUE);
ctx->prev_end = ctx->end;
}
txt_line = 0;
char_len = 0;
set_start_char = set_end_char = GF_FALSE;
ctx->style.startCharOffset = ctx->style.endCharOffset = 0;
gf_isom_text_reset(ctx->samp);
gf_filter_pid_set_info(ctx->opid, GF_PROP_PID_DOWN_BYTES, &PROP_LONGUINT( gf_ftell(ctx->src )) );
}
ctx->state = 0;
if (!sOK) break;
continue;
}
force_line:
switch (ctx->state) {
case 0:
if (sscanf(szLine, "%u", &line) != 1) {
GF_LOG(GF_LOG_ERROR, GF_LOG_PARSER, ("[TXTIn] Bad SRT formatting - expecting number got \"%s\"\n", szLine));
break;
}
if (line != ctx->curLine + 1) {
GF_LOG(GF_LOG_WARNING, GF_LOG_PARSER, ("[TXTIn] Corrupted SRT frame %d after frame %d\n", line, ctx->curLine));
}
ctx->curLine = line;
ctx->state = 1;
break;
case 1:
if (sscanf(szLine, "%u:%u:%u,%u --> %u:%u:%u,%u", &sh, &sm, &ss, &sms, &eh, &em, &es, &ems) != 8) {
if (sscanf(szLine, "%u:%u:%u.%u --> %u:%u:%u.%u", &sh, &sm, &ss, &sms, &eh, &em, &es, &ems) != 8) {
sh = eh = 0;
if (sscanf(szLine, "%u:%u,%u --> %u:%u,%u", &sm, &ss, &sms, &em, &es, &ems) != 6) {
if (sscanf(szLine, "%u:%u.%u --> %u:%u.%u", &sm, &ss, &sms, &em, &es, &ems) != 6) {
GF_LOG(GF_LOG_WARNING, GF_LOG_PARSER, ("[TXTIn] Error scanning SRT frame %d timing\n", ctx->curLine));
ctx->state = 0;
break;
}
}
}
}
ctx->start = (3600*sh + 60*sm + ss)*1000 + sms;
if (ctx->start < ctx->end) {
GF_LOG(GF_LOG_WARNING, GF_LOG_PARSER, ("[TXTIn] Overlapping SRT frame %d - starts "LLD" ms is before end of previous one "LLD" ms - adjusting time stamps\n", ctx->curLine, ctx->start, ctx->end));
ctx->start = ctx->end;
}
ctx->end = (3600*eh + 60*em + es)*1000 + ems;
/*make stream start at 0 by inserting a fake AU*/
if (ctx->first_samp && (ctx->start > 0)) {
txtin_process_send_text_sample(ctx, ctx->samp, 0, (u32) ctx->start, GF_TRUE);
}
ctx->style.style_flags = 0;
ctx->state = 2;
if (ctx->end <= ctx->prev_end) {
GF_LOG(GF_LOG_WARNING, GF_LOG_PARSER, ("[TXTIn] Overlapping SRT frame %d end "LLD" is at or before previous end "LLD" - removing\n", ctx->curLine, ctx->end, ctx->prev_end));
ctx->start = ctx->end;
ctx->state = 3;
}
break;
default:
/*reset only when text is present*/
ctx->first_samp = GF_FALSE;
/*go to line*/
if (txt_line) {
gf_isom_text_add_text(ctx->samp, "\n", 1);
char_len += 1;
}
ptr = (char *) szLine;
{
len = gf_utf8_mbstowcs(uniLine, 5000, (const char **) &ptr);
if (len == GF_UTF8_FAIL) {
GF_LOG(GF_LOG_WARNING, GF_LOG_PARSER, ("[TXTIn] Invalid UTF data (line %d)\n", ctx->curLine));
ctx->state = 0;
len = 0;
}
}
i=j=0;
rem_styles = 0;
rem_color = 0;
while (i<len) {
u32 font_style = 0;
u32 style_nb_chars = 0;
u32 style_def_type = 0;
if ( (uniLine[i]=='<') && (uniLine[i+2]=='>')) {
style_nb_chars = 3;
style_def_type = 1;
}
else if ( (uniLine[i]=='<') && (uniLine[i+1]=='/') && (uniLine[i+3]=='>')) {
style_def_type = 2;
style_nb_chars = 4;
}
else if (uniLine[i]=='<') {
const unsigned short* src = uniLine + i;
u32 alen = gf_utf8_wcstombs(szLine, 2048, (const unsigned short**) & src);
if (alen == GF_UTF8_FAIL) alen = 0;
szLine[alen] = 0;
strlwr(szLine);
if (!strncmp(szLine, "<font ", 6) ) {
char *a_sep = strstr(szLine, "color");
if (a_sep) a_sep = strchr(a_sep, '"');
if (a_sep) {
char *e_sep = strchr(a_sep+1, '"');
if (e_sep) {
e_sep[0] = 0;
font_style = gf_color_parse(a_sep+1);
e_sep[0] = '"';
e_sep = strchr(e_sep+1, '>');
if (e_sep) {
style_nb_chars = (u32) (1 + e_sep - szLine);
style_def_type = 1;
}
}
}
}
else if (!strncmp(szLine, "</font>", 7) ) {
style_nb_chars = 7;
style_def_type = 2;
font_style = 0xFFFFFFFF;
}
else if (!strncmp(szLine, "<strike>", 8) ) {
style_nb_chars = 8;
style_def_type = 1;
}
else if (!strncmp(szLine, "</strike>", 9) ) {
style_nb_chars = 9;
style_def_type = 2;
font_style = 0xFFFFFFFF;
}
//skip unknown
else {
char *a_sep = strstr(szLine, ">");
if (a_sep) {
style_nb_chars = (u32) (1 + a_sep - szLine);
i += style_nb_chars;
continue;
}
}
}
/*start of new style*/
if (style_def_type==1) {
/*store prev style*/
if (set_end_char) {
assert(set_start_char);
gf_isom_text_add_style(ctx->samp, &ctx->style);
set_end_char = set_start_char = GF_FALSE;
ctx->style.style_flags &= ~rem_styles;
rem_styles = 0;
if (rem_color) {
ctx->style.text_color = ctx->default_color;
rem_color = 0;
}
}
if (set_start_char && (ctx->style.startCharOffset != j)) {
ctx->style.endCharOffset = char_len + j;
if (ctx->style.style_flags) gf_isom_text_add_style(ctx->samp, &ctx->style);
}
switch (uniLine[i+1]) {
case 'b':
case 'B':
ctx->style.style_flags |= GF_TXT_STYLE_BOLD;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
break;
case 'i':
case 'I':
ctx->style.style_flags |= GF_TXT_STYLE_ITALIC;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
break;
case 'u':
case 'U':
ctx->style.style_flags |= GF_TXT_STYLE_UNDERLINED;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
break;
case 's':
case 'S':
ctx->style.style_flags |= GF_TXT_STYLE_STRIKETHROUGH;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
break;
case 'f':
case 'F':
if (font_style) {
ctx->style.text_color = font_style;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
}
break;
}
i += style_nb_chars;
continue;
}
/*end of prev style*/
if (style_def_type==2) {
switch (uniLine[i+2]) {
case 'b':
case 'B':
rem_styles |= GF_TXT_STYLE_BOLD;
set_end_char = GF_TRUE;
ctx->style.endCharOffset = char_len + j;
break;
case 'i':
case 'I':
rem_styles |= GF_TXT_STYLE_ITALIC;
set_end_char = GF_TRUE;
ctx->style.endCharOffset = char_len + j;
break;
case 'u':
case 'U':
rem_styles |= GF_TXT_STYLE_UNDERLINED;
set_end_char = GF_TRUE;
ctx->style.endCharOffset = char_len + j;
break;
case 's':
case 'S':
rem_styles |= GF_TXT_STYLE_STRIKETHROUGH;
set_end_char = GF_TRUE;
ctx->style.endCharOffset = char_len + j;
break;
case 'f':
case 'F':
if (font_style) {
rem_color = 1;
set_end_char = GF_TRUE;
ctx->style.endCharOffset = char_len + j;
}
}
i+=style_nb_chars;
continue;
}
/*store style*/
if (set_end_char) {
gf_isom_text_add_style(ctx->samp, &ctx->style);
set_end_char = GF_FALSE;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
ctx->style.style_flags &= ~rem_styles;
rem_styles = 0;
ctx->style.text_color = ctx->default_color;
rem_color = 0;
}
uniText[j] = uniLine[i];
j++;
i++;
}
/*store last style*/
if (set_end_char) {
gf_isom_text_add_style(ctx->samp, &ctx->style);
set_end_char = GF_FALSE;
set_start_char = GF_TRUE;
ctx->style.startCharOffset = char_len + j;
ctx->style.style_flags &= ~rem_styles;
}
char_line = j;
uniText[j] = 0;
sptr = (u16 *) uniText;
len = gf_utf8_wcstombs(szText, 5000, (const u16 **) &sptr);
if (len == GF_UTF8_FAIL) len = 0;
gf_isom_text_add_text(ctx->samp, szText, len);
char_len += char_line;
txt_line ++;
break;
}
if (gf_filter_pid_would_block(ctx->opid))
return GF_OK;
}
/*final flush*/
if (ctx->end && ! ctx->noflush) {
gf_isom_text_reset(ctx->samp);
txtin_process_send_text_sample(ctx, ctx->samp, ctx->end, 0, GF_TRUE);
ctx->end = 0;
}
gf_isom_text_reset(ctx->samp);
return GF_EOS;
}
/* Structure used to pass importer and track data to the parsers without exposing the GF_MediaImporter structure
used by WebVTT and Flash->SVG */
typedef struct {
GF_TXTIn *ctx;
u32 timescale;
u32 track;
u32 descriptionIndex;
} GF_ISOFlusher;
#ifndef GPAC_DISABLE_VTT
static GF_Err gf_webvtt_import_report(void *user, GF_Err e, char *message, const char *line)
{
GF_LOG(e ? GF_LOG_WARNING : GF_LOG_INFO, GF_LOG_AUTHOR, ("[TXTIn] WebVTT line %s: %s\n", line, message) );
return e;
}
static void gf_webvtt_import_header(void *user, const char *config)
{
GF_TXTIn *ctx = (GF_TXTIn *)user;
if (!ctx->hdr_parsed) {
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DECODER_CONFIG, &PROP_DATA((char *) config, (u32) (1+strlen(config)) ) );
ctx->hdr_parsed = GF_TRUE;
gf_webvtt_parser_suspend(ctx->vttparser);
}
}
static void gf_webvtt_flush_sample(void *user, GF_WebVTTSample *samp)
{
u64 start, end;
GF_TXTIn *ctx = (GF_TXTIn *)user;