-
Notifications
You must be signed in to change notification settings - Fork 111
/
stl.go
1119 lines (1012 loc) · 40 KB
/
stl.go
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
package astisub
import (
"bytes"
"encoding/binary"
"errors"
"fmt"
"io"
"math"
"strconv"
"strings"
"time"
"github.com/asticode/go-astikit"
"golang.org/x/text/unicode/norm"
)
// https://tech.ebu.ch/docs/tech/tech3264.pdf
// https://github.com/yanncoupin/stl2srt/blob/master/to_srt.py
// STL block sizes
const (
stlBlockSizeGSI = 1024
stlBlockSizeTTI = 128
)
// STL character code table number
const (
stlCharacterCodeTableNumberLatin uint16 = 12336
stlCharacterCodeTableNumberLatinCyrillic uint16 = 12337
stlCharacterCodeTableNumberLatinArabic uint16 = 12338
stlCharacterCodeTableNumberLatinGreek uint16 = 12339
stlCharacterCodeTableNumberLatinHebrew uint16 = 12340
)
// STL character code tables
// TODO Add missing tables
var (
stlCharacterCodeTables = map[uint16]*astikit.BiMap{
stlCharacterCodeTableNumberLatin: astikit.NewBiMap().
Set(0x20, " ").Set(0x21, "!").Set(0x22, "\"").Set(0x23, "#").
Set(0x24, "¤").Set(0x25, "%").Set(0x26, "&").Set(0x27, "'").
Set(0x28, "(").Set(0x29, ")").Set(0x2a, "*").Set(0x2b, "+").
Set(0x2c, ",").Set(0x2d, "-").Set(0x2e, ".").Set(0x2f, "/").
Set(0x30, "0").Set(0x31, "1").Set(0x32, "2").Set(0x33, "3").
Set(0x34, "4").Set(0x35, "5").Set(0x36, "6").Set(0x37, "7").
Set(0x38, "8").Set(0x39, "9").Set(0x3a, ":").Set(0x3b, ";").
Set(0x3c, "<").Set(0x3d, "=").Set(0x3e, ">").Set(0x3f, "?").
Set(0x40, "@").Set(0x41, "A").Set(0x42, "B").Set(0x43, "C").
Set(0x44, "D").Set(0x45, "E").Set(0x46, "F").Set(0x47, "G").
Set(0x48, "H").Set(0x49, "I").Set(0x4a, "J").Set(0x4b, "K").
Set(0x4c, "L").Set(0x4d, "M").Set(0x4e, "N").Set(0x4f, "O").
Set(0x50, "P").Set(0x51, "Q").Set(0x52, "R").Set(0x53, "S").
Set(0x54, "T").Set(0x55, "U").Set(0x56, "V").Set(0x57, "W").
Set(0x58, "X").Set(0x59, "Y").Set(0x5a, "Z").Set(0x5b, "[").
Set(0x5c, "\\").Set(0x5d, "]").Set(0x5e, "^").Set(0x5f, "_").
Set(0x60, "`").Set(0x61, "a").Set(0x62, "b").Set(0x63, "c").
Set(0x64, "d").Set(0x65, "e").Set(0x66, "f").Set(0x67, "g").
Set(0x68, "h").Set(0x69, "i").Set(0x6a, "j").Set(0x6b, "k").
Set(0x6c, "l").Set(0x6d, "m").Set(0x6e, "n").Set(0x6f, "o").
Set(0x70, "p").Set(0x71, "q").Set(0x72, "r").Set(0x73, "s").
Set(0x74, "t").Set(0x75, "u").Set(0x76, "v").Set(0x77, "w").
Set(0x78, "x").Set(0x79, "y").Set(0x7a, "z").Set(0x7b, "{").
Set(0x7c, "|").Set(0x7d, "}").Set(0x7e, "~").
Set(0xa0, string([]byte{0xC2, 0xA0})).Set(0xa1, "¡").Set(0xa2, "¢").
Set(0xa3, "£").Set(0xa4, "$").Set(0xa5, "¥").Set(0xa7, "§").
Set(0xa9, "‘").Set(0xaa, "“").Set(0xab, "«").Set(0xac, "←").
Set(0xad, "↑").Set(0xae, "→").Set(0xaf, "↓").
Set(0xb0, "°").Set(0xb1, "±").Set(0xb2, "²").Set(0xb3, "³").
Set(0xb4, "×").Set(0xb5, "µ").Set(0xb6, "¶").Set(0xb7, "·").
Set(0xb8, "÷").Set(0xb9, "’").Set(0xba, "”").Set(0xbb, "»").
Set(0xbc, "¼").Set(0xbd, "½").Set(0xbe, "¾").Set(0xbf, "¿").
Set(0xc1, string([]byte{0xCC, 0x80})).Set(0xc2, string([]byte{0xCC, 0x81})).
Set(0xc3, string([]byte{0xCC, 0x82})).Set(0xc4, string([]byte{0xCC, 0x83})).
Set(0xc5, string([]byte{0xCC, 0x84})).Set(0xc6, string([]byte{0xCC, 0x86})).
Set(0xc7, string([]byte{0xCC, 0x87})).Set(0xc8, string([]byte{0xCC, 0x88})).
Set(0xca, string([]byte{0xCC, 0x8A})).Set(0xcb, string([]byte{0xCC, 0xA7})).
Set(0xcd, string([]byte{0xCC, 0x8B})).Set(0xce, string([]byte{0xCC, 0xA8})).
Set(0xcf, string([]byte{0xCC, 0x8C})).
Set(0xd0, "―").Set(0xd1, "¹").Set(0xd2, "®").Set(0xd3, "©").
Set(0xd4, "™").Set(0xd5, "♪").Set(0xd6, "¬").Set(0xd7, "¦").
Set(0xdc, "⅛").Set(0xdd, "⅜").Set(0xde, "⅝").Set(0xdf, "⅞").
Set(0xe0, "Ω").Set(0xe1, "Æ").Set(0xe2, "Đ").Set(0xe3, "ª").
Set(0xe4, "Ħ").Set(0xe6, "IJ").Set(0xe7, "Ŀ").Set(0xe8, "Ł").
Set(0xe9, "Ø").Set(0xea, "Œ").Set(0xeb, "º").Set(0xec, "Þ").
Set(0xed, "Ŧ").Set(0xee, "Ŋ").Set(0xef, "ʼn").
Set(0xf0, "ĸ").Set(0xf1, "æ").Set(0xf2, "đ").Set(0xf3, "ð").
Set(0xf4, "ħ").Set(0xf5, "ı").Set(0xf6, "ij").Set(0xf7, "ŀ").
Set(0xf8, "ł").Set(0xf9, "ø").Set(0xfa, "œ").Set(0xfb, "ß").
Set(0xfc, "þ").Set(0xfd, "ŧ").Set(0xfe, "ŋ").Set(0xff, string([]byte{0xC2, 0xAD})),
}
)
// STL code page numbers
const (
stlCodePageNumberCanadaFrench uint32 = 3683891
stlCodePageNumberMultilingual uint32 = 3683632
stlCodePageNumberNordic uint32 = 3683893
stlCodePageNumberPortugal uint32 = 3683888
stlCodePageNumberUnitedStates uint32 = 3420983
)
// STL comment flag
const (
stlCommentFlagTextContainsSubtitleData = '\x00'
stlCommentFlagTextContainsCommentsNotIntendedForTransmission = '\x01'
)
// STL country codes
const (
stlCountryCodeChinese = "CHN"
stlCountryCodeFrance = "FRA"
stlCountryCodeJapan = "JPN"
stlCountryCodeNorway = "NOR"
)
// STL cumulative status
const (
stlCumulativeStatusFirstSubtitleOfACumulativeSet = '\x01'
stlCumulativeStatusIntermediateSubtitleOfACumulativeSet = '\x02'
stlCumulativeStatusLastSubtitleOfACumulativeSet = '\x03'
stlCumulativeStatusSubtitleNotPartOfACumulativeSet = '\x00'
)
// STL display standard code
const (
stlDisplayStandardCodeOpenSubtitling = "0"
stlDisplayStandardCodeLevel1Teletext = "1"
stlDisplayStandardCodeLevel2Teletext = "2"
)
// STL framerate mapping
var stlFramerateMapping = astikit.NewBiMap().
Set("STL25.01", 25).
Set("STL30.01", 30)
// STL justification code
const (
stlJustificationCodeCentredText = '\x02'
stlJustificationCodeLeftJustifiedText = '\x01'
stlJustificationCodeRightJustifiedText = '\x03'
stlJustificationCodeUnchangedPresentation = '\x00'
)
// STL language codes
const (
stlLanguageCodeChinese = "75"
stlLanguageCodeEnglish = "09"
stlLanguageCodeFrench = "0F"
stllanguageCodeJapanese = "69"
stlLanguageCodeNorwegian = "1E"
)
// STL language mapping
var stlLanguageMapping = astikit.NewBiMap().
Set(stlLanguageCodeChinese, LanguageChinese).
Set(stlLanguageCodeEnglish, LanguageEnglish).
Set(stlLanguageCodeFrench, LanguageFrench).
Set(stllanguageCodeJapanese, LanguageJapanese).
Set(stlLanguageCodeNorwegian, LanguageNorwegian)
// STL timecode status
const (
stlTimecodeStatusNotIntendedForUse = "0"
stlTimecodeStatusIntendedForUse = "1"
)
// TTI Special Extension Block Number
const extensionBlockNumberReservedUserData = 0xfe
const stlLineSeparator = 0x8a
type STLPosition struct {
VerticalPosition int
MaxRows int
Rows int
}
// STLOptions represents STL parsing options
type STLOptions struct {
// IgnoreTimecodeStartOfProgramme - set STLTimecodeStartOfProgramme to zero before parsing
IgnoreTimecodeStartOfProgramme bool
}
// ReadFromSTL parses an .stl content
func ReadFromSTL(i io.Reader, opts STLOptions) (o *Subtitles, err error) {
// Init
o = NewSubtitles()
// Read GSI block
var b []byte
if b, err = readNBytes(i, stlBlockSizeGSI); err != nil {
return
}
// Parse GSI block
var g *gsiBlock
if g, err = parseGSIBlock(b); err != nil {
err = fmt.Errorf("astisub: building gsi block failed: %w", err)
return
}
// Create character handler
var ch *stlCharacterHandler
if ch, err = newSTLCharacterHandler(g.characterCodeTableNumber); err != nil {
err = fmt.Errorf("astisub: creating stl character handler failed: %w", err)
return
}
// Update metadata
// TODO Add more STL fields to metadata
o.Metadata = &Metadata{
Framerate: g.framerate,
STLCountryOfOrigin: g.countryOfOrigin,
STLCreationDate: &g.creationDate,
STLDisplayStandardCode: g.displayStandardCode,
STLEditorContactDetails: g.editorContactDetails,
STLEditorName: g.editorName,
STLMaximumNumberOfDisplayableCharactersInAnyTextRow: astikit.IntPtr(g.maximumNumberOfDisplayableCharactersInAnyTextRow),
STLMaximumNumberOfDisplayableRows: astikit.IntPtr(g.maximumNumberOfDisplayableRows),
STLOriginalEpisodeTitle: g.originalEpisodeTitle,
STLPublisher: g.publisher,
STLRevisionDate: &g.revisionDate,
STLRevisionNumber: g.revisionNumber,
STLSubtitleListReferenceCode: g.subtitleListReferenceCode,
STLTranslatedEpisodeTitle: g.translatedEpisodeTitle,
STLTranslatedProgramTitle: g.translatedProgramTitle,
STLTranslatorContactDetails: g.translatorContactDetails,
STLTranslatorName: g.translatorName,
Title: g.originalProgramTitle,
}
if !opts.IgnoreTimecodeStartOfProgramme {
o.Metadata.STLTimecodeStartOfProgramme = g.timecodeStartOfProgramme
}
if v, ok := stlLanguageMapping.Get(g.languageCode); ok {
o.Metadata.Language = v.(string)
}
// Parse Text and Timing Information (TTI) blocks.
for {
// Read TTI block
if b, err = readNBytes(i, stlBlockSizeTTI); err != nil {
if err == io.EOF {
err = nil
break
}
return
}
// Parse TTI block
var t = parseTTIBlock(b, g.framerate)
// Do not process reserved user data
if t.extensionBlockNumber == extensionBlockNumberReservedUserData {
continue
}
justification := parseSTLJustificationCode(t.justificationCode)
rows := bytes.Split(t.text, []byte{stlLineSeparator})
position := STLPosition{
MaxRows: g.maximumNumberOfDisplayableRows,
Rows: len(rows),
VerticalPosition: t.verticalPosition,
}
styleAttributes := StyleAttributes{
STLJustification: &justification,
STLPosition: &position,
}
styleAttributes.propagateSTLAttributes()
// Create item
var i = &Item{
EndAt: t.timecodeOut - o.Metadata.STLTimecodeStartOfProgramme,
InlineStyle: &styleAttributes,
StartAt: t.timecodeIn - o.Metadata.STLTimecodeStartOfProgramme,
}
// Loop through rows
for _, text := range bytes.Split(t.text, []byte{stlLineSeparator}) {
if g.displayStandardCode == stlDisplayStandardCodeOpenSubtitling {
err = parseOpenSubtitleRow(i, ch, func() styler { return newSTLStyler() }, text)
if err != nil {
return nil, err
}
} else {
parseTeletextRow(i, ch, func() styler { return newSTLStyler() }, text)
}
}
// Append item
o.Items = append(o.Items, i)
}
return
}
// readNBytes reads n bytes
func readNBytes(i io.Reader, c int) (o []byte, err error) {
o = make([]byte, c)
var n int
if n, err = i.Read(o); err != nil || n != len(o) {
if err != nil {
if err == io.EOF {
return
}
err = fmt.Errorf("astisub: reading %d bytes failed: %w", c, err)
return
}
err = fmt.Errorf("astisub: read %d bytes, should have read %d", n, c)
return
}
return
}
// gsiBlock represents a GSI block
type gsiBlock struct {
characterCodeTableNumber uint16
codePageNumber uint32
countryOfOrigin string
creationDate time.Time
diskSequenceNumber int
displayStandardCode string
editorContactDetails string
editorName string
framerate int
languageCode string
maximumNumberOfDisplayableCharactersInAnyTextRow int
maximumNumberOfDisplayableRows int
originalEpisodeTitle string
originalProgramTitle string
publisher string
revisionDate time.Time
revisionNumber int
subtitleListReferenceCode string
timecodeFirstInCue time.Duration
timecodeStartOfProgramme time.Duration
timecodeStatus string
totalNumberOfDisks int
totalNumberOfSubtitleGroups int
totalNumberOfSubtitles int
totalNumberOfTTIBlocks int
translatedEpisodeTitle string
translatedProgramTitle string
translatorContactDetails string
translatorName string
userDefinedArea string
}
// newGSIBlock builds the subtitles GSI block
func newGSIBlock(s Subtitles) (g *gsiBlock) {
// Init
g = &gsiBlock{
characterCodeTableNumber: stlCharacterCodeTableNumberLatin,
codePageNumber: stlCodePageNumberMultilingual,
countryOfOrigin: stlCountryCodeFrance,
creationDate: Now(),
diskSequenceNumber: 1,
displayStandardCode: stlDisplayStandardCodeLevel1Teletext,
framerate: 25,
languageCode: stlLanguageCodeFrench,
maximumNumberOfDisplayableCharactersInAnyTextRow: 40,
maximumNumberOfDisplayableRows: 23,
revisionDate: Now(),
subtitleListReferenceCode: "",
timecodeStatus: stlTimecodeStatusIntendedForUse,
timecodeStartOfProgramme: 0,
totalNumberOfDisks: 1,
totalNumberOfSubtitleGroups: 1,
totalNumberOfSubtitles: len(s.Items),
totalNumberOfTTIBlocks: len(s.Items),
}
// Add metadata
if s.Metadata != nil {
if s.Metadata.STLCreationDate != nil {
g.creationDate = *s.Metadata.STLCreationDate
}
g.countryOfOrigin = s.Metadata.STLCountryOfOrigin
g.displayStandardCode = s.Metadata.STLDisplayStandardCode
g.editorContactDetails = s.Metadata.STLEditorContactDetails
g.editorName = s.Metadata.STLEditorName
g.framerate = s.Metadata.Framerate
if v, ok := stlLanguageMapping.GetInverse(s.Metadata.Language); ok {
g.languageCode = v.(string)
}
g.originalProgramTitle = s.Metadata.Title
if s.Metadata.STLMaximumNumberOfDisplayableCharactersInAnyTextRow != nil {
g.maximumNumberOfDisplayableCharactersInAnyTextRow = *s.Metadata.STLMaximumNumberOfDisplayableCharactersInAnyTextRow
}
if s.Metadata.STLMaximumNumberOfDisplayableRows != nil {
g.maximumNumberOfDisplayableRows = *s.Metadata.STLMaximumNumberOfDisplayableRows
}
g.originalEpisodeTitle = s.Metadata.STLOriginalEpisodeTitle
g.publisher = s.Metadata.STLPublisher
if s.Metadata.STLRevisionDate != nil {
g.revisionDate = *s.Metadata.STLRevisionDate
}
g.revisionNumber = s.Metadata.STLRevisionNumber
g.subtitleListReferenceCode = s.Metadata.STLSubtitleListReferenceCode
g.timecodeStartOfProgramme = s.Metadata.STLTimecodeStartOfProgramme
g.translatedEpisodeTitle = s.Metadata.STLTranslatedEpisodeTitle
g.translatedProgramTitle = s.Metadata.STLTranslatedProgramTitle
g.translatorContactDetails = s.Metadata.STLTranslatorContactDetails
g.translatorName = s.Metadata.STLTranslatorName
}
// Timecode first in cue
if len(s.Items) > 0 {
g.timecodeFirstInCue = s.Items[0].StartAt
}
return
}
// parseGSIBlock parses a GSI block
func parseGSIBlock(b []byte) (g *gsiBlock, err error) {
// Init
g = &gsiBlock{
characterCodeTableNumber: binary.BigEndian.Uint16(b[12:14]),
countryOfOrigin: string(bytes.TrimSpace(b[274:277])),
codePageNumber: binary.BigEndian.Uint32(append([]byte{0x0}, b[0:3]...)),
displayStandardCode: string(bytes.TrimSpace([]byte{b[11]})),
editorName: string(bytes.TrimSpace(b[309:341])),
editorContactDetails: string(bytes.TrimSpace(b[341:373])),
languageCode: string(bytes.TrimSpace(b[14:16])),
originalEpisodeTitle: string(bytes.TrimSpace(b[48:80])),
originalProgramTitle: string(bytes.TrimSpace(b[16:48])),
publisher: string(bytes.TrimSpace(b[277:309])),
subtitleListReferenceCode: string(bytes.TrimSpace(b[208:224])),
timecodeStatus: string(bytes.TrimSpace([]byte{b[255]})),
translatedProgramTitle: string(bytes.TrimSpace(b[80:112])),
translatedEpisodeTitle: string(bytes.TrimSpace(b[112:144])),
translatorContactDetails: string(bytes.TrimSpace(b[176:208])),
translatorName: string(bytes.TrimSpace(b[144:176])),
userDefinedArea: string(bytes.TrimSpace(b[448:])),
}
// Framerate
if v, ok := stlFramerateMapping.Get(string(b[3:11])); ok {
g.framerate = v.(int)
}
// Creation date
if v := strings.TrimSpace(string(b[224:230])); len(v) > 0 {
if g.creationDate, err = time.Parse("060102", v); err != nil {
err = fmt.Errorf("astisub: parsing date %s failed: %w", v, err)
return
}
}
// Revision date
if v := strings.TrimSpace(string(b[230:236])); len(v) > 0 {
if g.revisionDate, err = time.Parse("060102", v); err != nil {
err = fmt.Errorf("astisub: parsing date %s failed: %w", v, err)
return
}
}
// Revision number
if v := strings.TrimSpace(string(b[236:238])); len(v) > 0 {
if g.revisionNumber, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Total number of TTI blocks
if v := strings.TrimSpace(string(b[238:243])); len(v) > 0 {
if g.totalNumberOfTTIBlocks, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Total number of subtitles
if v := strings.TrimSpace(string(b[243:248])); len(v) > 0 {
if g.totalNumberOfSubtitles, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Total number of subtitle groups
if v := strings.TrimSpace(string(b[248:251])); len(v) > 0 {
if g.totalNumberOfSubtitleGroups, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Maximum number of displayable characters in any text row
if v := strings.TrimSpace(string(b[251:253])); len(v) > 0 {
if g.maximumNumberOfDisplayableCharactersInAnyTextRow, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Maximum number of displayable rows
if v := strings.TrimSpace(string(b[253:255])); len(v) > 0 {
if g.maximumNumberOfDisplayableRows, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Timecode start of programme
if v := strings.TrimSpace(string(b[256:264])); len(v) > 0 {
if g.timecodeStartOfProgramme, err = parseDurationSTL(v, g.framerate); err != nil {
err = fmt.Errorf("astisub: parsing of stl duration %s failed: %w", v, err)
return
}
}
// Timecode first in cue
if v := strings.TrimSpace(string(b[264:272])); len(v) > 0 {
if g.timecodeFirstInCue, err = parseDurationSTL(v, g.framerate); err != nil {
err = fmt.Errorf("astisub: parsing of stl duration %s failed: %w", v, err)
return
}
}
// Total number of disks
if v := strings.TrimSpace(string(b[272])); len(v) > 0 {
if g.totalNumberOfDisks, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
// Disk sequence number
if v := strings.TrimSpace(string(b[273])); len(v) > 0 {
if g.diskSequenceNumber, err = strconv.Atoi(v); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", v, err)
return
}
}
return
}
// bytes transforms the GSI block into []byte
func (b gsiBlock) bytes() (o []byte) {
bs := make([]byte, 4)
binary.BigEndian.PutUint32(bs, b.codePageNumber)
o = append(o, astikit.BytesPad(bs[1:], ' ', 3, astikit.PadRight, astikit.PadCut)...) // Code page number
// Disk format code
var f string
if v, ok := stlFramerateMapping.GetInverse(b.framerate); ok {
f = v.(string)
}
o = append(o, astikit.BytesPad([]byte(f), ' ', 8, astikit.PadRight, astikit.PadCut)...)
o = append(o, astikit.BytesPad([]byte(b.displayStandardCode), ' ', 1, astikit.PadRight, astikit.PadCut)...) // Display standard code
binary.BigEndian.PutUint16(bs, b.characterCodeTableNumber)
o = append(o, astikit.BytesPad(bs[:2], ' ', 2, astikit.PadRight, astikit.PadCut)...) // Character code table number
o = append(o, astikit.BytesPad([]byte(b.languageCode), ' ', 2, astikit.PadRight, astikit.PadCut)...) // Language code
o = append(o, astikit.BytesPad([]byte(b.originalProgramTitle), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Original program title
o = append(o, astikit.BytesPad([]byte(b.originalEpisodeTitle), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Original episode title
o = append(o, astikit.BytesPad([]byte(b.translatedProgramTitle), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Translated program title
o = append(o, astikit.BytesPad([]byte(b.translatedEpisodeTitle), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Translated episode title
o = append(o, astikit.BytesPad([]byte(b.translatorName), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Translator's name
o = append(o, astikit.BytesPad([]byte(b.translatorContactDetails), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Translator's contact details
o = append(o, astikit.BytesPad([]byte(b.subtitleListReferenceCode), ' ', 16, astikit.PadRight, astikit.PadCut)...) // Subtitle list reference code
o = append(o, astikit.BytesPad([]byte(b.creationDate.Format("060102")), ' ', 6, astikit.PadRight, astikit.PadCut)...) // Creation date
o = append(o, astikit.BytesPad([]byte(b.revisionDate.Format("060102")), ' ', 6, astikit.PadRight, astikit.PadCut)...) // Revision date
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.revisionNumber)), '0', 2, astikit.PadCut)...) // Revision number
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.totalNumberOfTTIBlocks)), '0', 5, astikit.PadCut)...) // Total number of TTI blocks
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.totalNumberOfSubtitles)), '0', 5, astikit.PadCut)...) // Total number of subtitles
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.totalNumberOfSubtitleGroups)), '0', 3, astikit.PadCut)...) // Total number of subtitle groups
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.maximumNumberOfDisplayableCharactersInAnyTextRow)), '0', 2, astikit.PadCut)...) // Maximum number of displayable characters in any text row
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.maximumNumberOfDisplayableRows)), '0', 2, astikit.PadCut)...) // Maximum number of displayable rows
o = append(o, astikit.BytesPad([]byte(b.timecodeStatus), ' ', 1, astikit.PadRight, astikit.PadCut)...) // Timecode status
o = append(o, astikit.BytesPad([]byte(formatDurationSTL(b.timecodeStartOfProgramme, b.framerate)), ' ', 8, astikit.PadRight, astikit.PadCut)...) // Timecode start of a programme
o = append(o, astikit.BytesPad([]byte(formatDurationSTL(b.timecodeFirstInCue, b.framerate)), ' ', 8, astikit.PadRight, astikit.PadCut)...) // Timecode first in cue
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.totalNumberOfDisks)), ' ', 1, astikit.PadRight, astikit.PadCut)...) // Total number of disks
o = append(o, astikit.BytesPad([]byte(strconv.Itoa(b.diskSequenceNumber)), ' ', 1, astikit.PadRight, astikit.PadCut)...) // Disk sequence number
o = append(o, astikit.BytesPad([]byte(b.countryOfOrigin), ' ', 3, astikit.PadRight, astikit.PadCut)...) // Country of origin
o = append(o, astikit.BytesPad([]byte(b.publisher), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Publisher
o = append(o, astikit.BytesPad([]byte(b.editorName), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Editor's name
o = append(o, astikit.BytesPad([]byte(b.editorContactDetails), ' ', 32, astikit.PadRight, astikit.PadCut)...) // Editor's contact details
o = append(o, astikit.BytesPad([]byte{}, ' ', 75+576, astikit.PadRight, astikit.PadCut)...) // Spare bytes + user defined area // // Editor's contact details
return
}
// parseDurationSTL parses a STL duration
func parseDurationSTL(i string, framerate int) (d time.Duration, err error) {
// Parse hours
var hours, hoursString = 0, i[0:2]
if hours, err = strconv.Atoi(hoursString); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", hoursString, err)
return
}
// Parse minutes
var minutes, minutesString = 0, i[2:4]
if minutes, err = strconv.Atoi(minutesString); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", minutesString, err)
return
}
// Parse seconds
var seconds, secondsString = 0, i[4:6]
if seconds, err = strconv.Atoi(secondsString); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", secondsString, err)
return
}
// Parse frames
var frames, framesString = 0, i[6:8]
if frames, err = strconv.Atoi(framesString); err != nil {
err = fmt.Errorf("astisub: atoi of %s failed: %w", framesString, err)
return
}
// Set duration
d = time.Duration(hours)*time.Hour + time.Duration(minutes)*time.Minute + time.Duration(seconds)*time.Second + time.Duration(1e9*frames/framerate)*time.Nanosecond
return
}
// formatDurationSTL formats a STL duration
func formatDurationSTL(d time.Duration, framerate int) (o string) {
// Add hours
if d.Hours() < 10 {
o += "0"
}
var delta = int(math.Floor(d.Hours()))
o += strconv.Itoa(delta)
d -= time.Duration(delta) * time.Hour
// Add minutes
if d.Minutes() < 10 {
o += "0"
}
delta = int(math.Floor(d.Minutes()))
o += strconv.Itoa(delta)
d -= time.Duration(delta) * time.Minute
// Add seconds
if d.Seconds() < 10 {
o += "0"
}
delta = int(math.Floor(d.Seconds()))
o += strconv.Itoa(delta)
d -= time.Duration(delta) * time.Second
// Add frames
var frames = int(int(d.Nanoseconds()) * framerate / 1e9)
if frames < 10 {
o += "0"
}
o += strconv.Itoa(frames)
return
}
// ttiBlock represents a TTI block
type ttiBlock struct {
commentFlag byte
cumulativeStatus byte
extensionBlockNumber int
justificationCode byte
subtitleGroupNumber int
subtitleNumber int
text []byte
timecodeIn time.Duration
timecodeOut time.Duration
verticalPosition int
}
// newTTIBlock builds an item TTI block
func newTTIBlock(i *Item, idx int) (t *ttiBlock) {
// Init
t = &ttiBlock{
commentFlag: stlCommentFlagTextContainsSubtitleData,
cumulativeStatus: stlCumulativeStatusSubtitleNotPartOfACumulativeSet,
extensionBlockNumber: 255,
justificationCode: stlJustificationCodeFromStyle(i.InlineStyle),
subtitleGroupNumber: 0,
subtitleNumber: idx,
timecodeIn: i.StartAt,
timecodeOut: i.EndAt,
verticalPosition: stlVerticalPositionFromStyle(i.InlineStyle),
}
// Add text
var lines []string
for _, l := range i.Lines {
var lineItems []string
for _, li := range l.Items {
lineItems = append(lineItems, li.STLString())
}
lines = append(lines, strings.Join(lineItems, " "))
}
t.text = []byte(strings.Join(lines, string(rune(stlLineSeparator))))
return
}
func stlJustificationCodeFromStyle(sa *StyleAttributes) byte {
if sa == nil || sa.STLJustification == nil {
return stlJustificationCodeLeftJustifiedText
}
switch *sa.STLJustification {
case JustificationCentered:
return stlJustificationCodeCentredText
case JustificationLeft:
return stlJustificationCodeLeftJustifiedText
case JustificationRight:
return stlJustificationCodeRightJustifiedText
case JustificationUnchanged:
return stlJustificationCodeUnchangedPresentation
default:
return stlJustificationCodeLeftJustifiedText
}
}
func stlVerticalPositionFromStyle(sa *StyleAttributes) int {
if sa != nil && sa.STLPosition != nil {
return sa.STLPosition.VerticalPosition
} else {
return 20
}
}
func (li LineItem) STLString() string {
rs := li.Text
if li.InlineStyle != nil {
if li.InlineStyle.STLItalics != nil && *li.InlineStyle.STLItalics {
rs = string(rune(0x80)) + rs + string(rune(0x81))
}
if li.InlineStyle.STLUnderline != nil && *li.InlineStyle.STLUnderline {
rs = string(rune(0x82)) + rs + string(rune(0x83))
}
if li.InlineStyle.STLBoxing != nil && *li.InlineStyle.STLBoxing {
rs = string(rune(0x84)) + rs + string(rune(0x85))
}
}
return rs
}
// parseTTIBlock parses a TTI block
func parseTTIBlock(p []byte, framerate int) *ttiBlock {
return &ttiBlock{
commentFlag: p[15],
cumulativeStatus: p[4],
extensionBlockNumber: int(uint8(p[3])),
justificationCode: p[14],
subtitleGroupNumber: int(uint8(p[0])),
subtitleNumber: int(binary.LittleEndian.Uint16(p[1:3])),
text: p[16:128],
timecodeIn: parseDurationSTLBytes(p[5:9], framerate),
timecodeOut: parseDurationSTLBytes(p[9:13], framerate),
verticalPosition: int(uint8(p[13])),
}
}
// bytes transforms the TTI block into []byte
func (t *ttiBlock) bytes(g *gsiBlock) (o []byte) {
o = append(o, byte(uint8(t.subtitleGroupNumber))) // Subtitle group number
var b = make([]byte, 2)
binary.LittleEndian.PutUint16(b, uint16(t.subtitleNumber))
o = append(o, b...) // Subtitle number
o = append(o, byte(uint8(t.extensionBlockNumber))) // Extension block number
o = append(o, t.cumulativeStatus) // Cumulative status
o = append(o, formatDurationSTLBytes(t.timecodeIn, g.framerate)...) // Timecode in
o = append(o, formatDurationSTLBytes(t.timecodeOut, g.framerate)...) // Timecode out
o = append(o, validateVerticalPosition(t.verticalPosition, g.displayStandardCode)) // Vertical position
o = append(o, t.justificationCode) // Justification code
o = append(o, t.commentFlag) // Comment flag
o = append(o, astikit.BytesPad(encodeTextSTL(string(t.text)), '\x8f', 112, astikit.PadRight, astikit.PadCut)...) // Text field
return
}
// According to EBU 3264 (https://tech.ebu.ch/docs/tech/tech3264.pdf):
// page 12:
// for teletext subtitles, VP contains a value in the range 1-23 decimal (01h-17h)
// corresponding to theteletext row number of the first subtitle row.
// page 6:
// Teletext ("closed") subtitles are indicated via the Display Standard Code
// in the GSI block.
func validateVerticalPosition(vp int, dsc string) byte {
closed := false
switch dsc {
case stlDisplayStandardCodeLevel1Teletext, stlDisplayStandardCodeLevel2Teletext:
closed = true
}
if vp < 1 && closed {
vp = 1
}
if vp > 23 && closed {
vp = 23
}
return byte(uint8(vp))
}
// formatDurationSTLBytes formats a STL duration in bytes
func formatDurationSTLBytes(d time.Duration, framerate int) (o []byte) {
// Add hours
var hours = int(math.Floor(d.Hours()))
o = append(o, byte(uint8(hours)))
d -= time.Duration(hours) * time.Hour
// Add minutes
var minutes = int(math.Floor(d.Minutes()))
o = append(o, byte(uint8(minutes)))
d -= time.Duration(minutes) * time.Minute
// Add seconds
var seconds = int(math.Floor(d.Seconds()))
o = append(o, byte(uint8(seconds)))
d -= time.Duration(seconds) * time.Second
// Add frames
var frames = int(int(d.Nanoseconds()) * framerate / 1e9)
o = append(o, byte(uint8(frames)))
return
}
// parseDurationSTLBytes parses a STL duration in bytes
func parseDurationSTLBytes(b []byte, framerate int) time.Duration {
return time.Duration(uint8(b[0]))*time.Hour + time.Duration(uint8(b[1]))*time.Minute + time.Duration(uint8(b[2]))*time.Second + time.Duration(1e9*int(uint8(b[3]))/framerate)*time.Nanosecond
}
type stlCharacterHandler struct {
accent string
c uint16
m *astikit.BiMap
}
func newSTLCharacterHandler(characterCodeTable uint16) (*stlCharacterHandler, error) {
if v, ok := stlCharacterCodeTables[characterCodeTable]; ok {
return &stlCharacterHandler{
c: characterCodeTable,
m: v,
}, nil
}
return nil, fmt.Errorf("astisub: table doesn't exist for character code table %d", characterCodeTable)
}
// TODO Use this instead of encodeTextSTL => use in teletext process like for decode
// TODO Test
func (h *stlCharacterHandler) encode(i []byte) byte {
return ' '
}
func (h *stlCharacterHandler) decode(i byte) (o []byte) {
k := int(i)
vi, ok := h.m.Get(k)
if !ok {
return
}
v := vi.(string)
if len(h.accent) > 0 {
o = norm.NFC.Bytes([]byte(v + h.accent))
h.accent = ""
return
} else if h.c == stlCharacterCodeTableNumberLatin && k >= 0xc0 && k <= 0xcf {
h.accent = v
return
}
return []byte(v)
}
type stlStyler struct {
boxing *bool
italics *bool
underline *bool
}
func newSTLStyler() *stlStyler {
return &stlStyler{}
}
func (s *stlStyler) parseSpacingAttribute(i byte) {
switch i {
case 0x80:
s.italics = astikit.BoolPtr(true)
case 0x81:
s.italics = astikit.BoolPtr(false)
case 0x82:
s.underline = astikit.BoolPtr(true)
case 0x83:
s.underline = astikit.BoolPtr(false)
case 0x84:
s.boxing = astikit.BoolPtr(true)
case 0x85:
s.boxing = astikit.BoolPtr(false)
}
}
func (s *stlStyler) hasBeenSet() bool {
return s.italics != nil || s.boxing != nil || s.underline != nil
}
func (s *stlStyler) hasChanged(sa *StyleAttributes) bool {
return s.boxing != sa.STLBoxing || s.italics != sa.STLItalics || s.underline != sa.STLUnderline
}
func (s *stlStyler) propagateStyleAttributes(sa *StyleAttributes) {
sa.propagateSTLAttributes()
}
func (s *stlStyler) update(sa *StyleAttributes) {
if s.boxing != nil && s.boxing != sa.STLBoxing {
sa.STLBoxing = s.boxing
}
if s.italics != nil && s.italics != sa.STLItalics {
sa.STLItalics = s.italics
}
if s.underline != nil && s.underline != sa.STLUnderline {
sa.STLUnderline = s.underline
}
}
// WriteToSTL writes subtitles in .stl format
func (s Subtitles) WriteToSTL(o io.Writer) (err error) {
// Do not write anything if no subtitles
if len(s.Items) == 0 {
err = ErrNoSubtitlesToWrite
return
}
// Write GSI block
var g = newGSIBlock(s)
if _, err = o.Write(g.bytes()); err != nil {
err = fmt.Errorf("astisub: writing gsi block failed: %w", err)
return
}
// Loop through items
for idx, item := range s.Items {
// Write tti block
if _, err = o.Write(newTTIBlock(item, idx+1).bytes(g)); err != nil {
err = fmt.Errorf("astisub: writing tti block #%d failed: %w", idx+1, err)
return
}
}
return
}
// TODO Remove below
// STL unicode diacritic
var stlUnicodeDiacritic = astikit.NewBiMap().
Set(byte('\xc1'), "\u0300"). // Grave accent
Set(byte('\xc2'), "\u0301"). // Acute accent
Set(byte('\xc3'), "\u0302"). // Circumflex
Set(byte('\xc4'), "\u0303"). // Tilde
Set(byte('\xc5'), "\u0304"). // Macron
Set(byte('\xc6'), "\u0306"). // Breve
Set(byte('\xc7'), "\u0307"). // Dot
Set(byte('\xc8'), "\u0308"). // Umlaut
Set(byte('\xca'), "\u030a"). // Ring
Set(byte('\xcb'), "\u0327"). // Cedilla
Set(byte('\xcd'), "\u030B"). // Double acute accent
Set(byte('\xce'), "\u0328"). // Ogonek
Set(byte('\xcf'), "\u030c") // Caron
// STL unicode mapping
var stlUnicodeMapping = astikit.NewBiMap().
Set(byte('\x8a'), "\u000a"). // Line break
Set(byte('\xa8'), "\u00a4"). // ¤
Set(byte('\xa9'), "\u2018"). // ‘
Set(byte('\xaa'), "\u201C"). // “
Set(byte('\xab'), "\u00AB"). // «
Set(byte('\xac'), "\u2190"). // ←
Set(byte('\xad'), "\u2191"). // ↑
Set(byte('\xae'), "\u2192"). // →
Set(byte('\xaf'), "\u2193"). // ↓
Set(byte('\xb4'), "\u00D7"). // ×
Set(byte('\xb8'), "\u00F7"). // ÷
Set(byte('\xb9'), "\u2019"). // ’
Set(byte('\xba'), "\u201D"). // ”
Set(byte('\xbc'), "\u00BC"). // ¼
Set(byte('\xbd'), "\u00BD"). // ½
Set(byte('\xbe'), "\u00BE"). // ¾
Set(byte('\xbf'), "\u00BF"). // ¿
Set(byte('\xd0'), "\u2015"). // ―
Set(byte('\xd1'), "\u00B9"). // ¹
Set(byte('\xd2'), "\u00AE"). // ®
Set(byte('\xd3'), "\u00A9"). // ©
Set(byte('\xd4'), "\u2122"). // ™
Set(byte('\xd5'), "\u266A"). // ♪
Set(byte('\xd6'), "\u00AC"). // ¬
Set(byte('\xd7'), "\u00A6"). // ¦
Set(byte('\xdc'), "\u215B"). // ⅛
Set(byte('\xdd'), "\u215C"). // ⅜
Set(byte('\xde'), "\u215D"). // ⅝
Set(byte('\xdf'), "\u215E"). // ⅞
Set(byte('\xe0'), "\u2126"). // Ohm Ω
Set(byte('\xe1'), "\u00C6"). // Æ
Set(byte('\xe2'), "\u0110"). // Đ
Set(byte('\xe3'), "\u00AA"). // ª
Set(byte('\xe4'), "\u0126"). // Ħ
Set(byte('\xe6'), "\u0132"). // IJ
Set(byte('\xe7'), "\u013F"). // Ŀ
Set(byte('\xe8'), "\u0141"). // Ł
Set(byte('\xe9'), "\u00D8"). // Ø
Set(byte('\xea'), "\u0152"). // Œ
Set(byte('\xeb'), "\u00BA"). // º
Set(byte('\xec'), "\u00DE"). // Þ
Set(byte('\xed'), "\u0166"). // Ŧ