-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
140 lines (114 loc) · 3.31 KB
/
parser_test.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
package main
import (
"errors"
"fmt"
"git.gammaspectra.live/WeebDataHoarder/swf-go"
swftag "git.gammaspectra.live/WeebDataHoarder/swf-go/tag"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/ass"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/settings"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/math"
"git.gammaspectra.live/WeebDataHoarder/swf2ass-go/types/shapes"
"io"
"os"
"testing"
)
func TestParser(t *testing.T) {
file, err := os.Open("im_alive.swf")
if err != nil {
t.Fatal(err)
}
defer file.Close()
swfReader, err := swf.NewReader(file)
if err != nil {
t.Fatal(err)
}
var tags []swftag.Tag
for {
readTag, err := swfReader.Tag()
if err != nil {
if errors.Is(err, swftag.ErrUnknownTag) {
continue
}
if errors.Is(err, io.EOF) {
break
}
panic(err)
}
if readTag == nil {
//not decoded
continue
}
tags = append(tags, readTag)
if readTag.Code() == swftag.RecordEnd {
break
}
switch t := readTag.(type) {
default:
_ = t
}
}
var frameOffset int64
processor := types.NewSWFProcessor(tags, shapes.RectangleFromSWF(swfReader.Header().FrameSize), swfReader.Header().FrameRate.Float64(), int64(swfReader.Header().FrameCount), swfReader.Header().Version)
assRenderer := ass.NewRenderer("sample.swf", processor.FrameRate, processor.ViewPort)
var lastFrame *types.FrameInformation
for {
frame := processor.NextFrameOutput()
if frame == nil {
break
}
lastFrame = frame
if !processor.Playing || processor.Loops > 0 {
break
}
//TODO: handle multiple sounds
if processor.Audio != nil && frameOffset == 0 {
if processor.Audio.Start == nil {
fmt.Printf("Skipped frame %d: audio not started\n", frame.FrameNumber)
continue
}
frameOffset = *processor.Audio.Start
} else if processor.Audio == nil {
//TODO: make this an option
fmt.Printf("Skipped frame %d: no audio\n", frame.FrameNumber)
continue
}
frame.FrameOffset = frameOffset
rendered := frame.Frame.Render(0, nil, types.None[math.ColorTransform](), types.None[math.MatrixTransform]())
if frame.GetFrameNumber() == 0 {
for _, object := range rendered {
t.Logf("frame 0: object %d depth: %s\n", object.ObjectId, object.Depth.String())
}
}
filteredRendered := make(types.RenderedFrame, 0, len(rendered))
var drawCalls, drawItems, filteredObjects, clipCalls, clipItems int
for _, object := range rendered {
if object.Clip != nil {
clipCalls++
clipItems += len(object.Clip.GetShape())
}
for _, p := range object.DrawPathList {
drawCalls++
drawItems += len(p.Shape)
}
filteredRendered = append(filteredRendered, object)
}
t.Logf("=== frame %d/%d ~ %d : Depth count: %d :: Object count: %d :: Paths: %d draw calls, %d items :: Filtered: %d :: Clips %d draw calls, %d items\n",
frame.GetFrameNumber(),
processor.ExpectedFrameCount,
frameOffset,
len(frame.Frame.DepthMap),
len(filteredRendered),
drawCalls,
drawItems,
filteredObjects,
clipCalls,
clipItems,
)
assRenderer.RenderFrame(*frame, filteredRendered, settings.GlobalSettings.KeyFrameInterval, settings.GlobalSettings.FlushInterval, settings.GlobalSettings.FlushCountLimit)
}
if lastFrame == nil {
t.Fatal("no frames generated")
}
assRenderer.Flush(*lastFrame)
}