-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.go
More file actions
275 lines (238 loc) · 8.74 KB
/
process.go
File metadata and controls
275 lines (238 loc) · 8.74 KB
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
// video transformation
package main
import (
"fmt"
"os"
"unsafe"
// "log"
"bytes"
"image"
"image/jpeg"
"regexp"
"reflect"
"errors"
"github.com/giorgisio/goav/swscale"
"github.com/giorgisio/goav/avcodec"
"github.com/giorgisio/goav/avformat"
"github.com/giorgisio/goav/avutil"
)
import "C"
type Game int
// games allowed
const (
G_OTHER Game = 0
G_VALORANT Game = 1
G_CSGO Game = 2
)
type KillLogVid struct {
Vid *VideoInfo // video to be cropped
Game Game // game being cropped
Match *regexp.Regexp // regexp of username
FrameChan chan []byte // channel used to send image to OCR engine
}
// constructor used to make channel allocation easier
func (klv *KillLogVid) Init() {
klv.FrameChan = make(chan []byte, 50) // TODO add knob
}
// methods that crop streams to kill log portion only
func (KillLogVid) CropValorant(klv *KillLogVid) error {
pullText := &PullText{}
pullText.Init(klv)
if err := video2Image(klv, pullText); err != nil {
return err
}
if err := pullText.Finish(klv); err != nil {
return err
}
fmt.Println("finished scanning valorant stream.") // TODO: add title
return nil
}
// returns a Rect() of portion of video we want to scan for text
func gameSubImage(klv *KillLogVid) (image.Rectangle, error) {
nilRect := image.Rect(0,0,0,0) // FIXME: find out why i cant use nil
switch klv.Game {
case G_VALORANT:
if klv.Vid.Res == "1080p" || klv.Vid.Res == "1080p60" { // XXX: add support for other resolutions
return image.Rect(1455, 83, 1899, 339), nil
}
fmt.Printf("%s\n", klv.Vid.Res)
return nilRect, errors.New("unsupported resolution.") // NOTE: part of above XXX
case G_CSGO:
return nilRect, errors.New("BUG: gameSubImage(): CSGO is not currently supported. " +
"This should be checked before this message.")
case G_OTHER:
return nilRect, errors.New("BUG: gameSubImage(): invalid game used. This should be " +
"checked before this message.")
default:
panic("[process] gameSubImage(): invalid game")
}
}
// saveFrame creates an in-memory Image of a single frame
func saveFrame(klv *KillLogVid, frame *avutil.Frame, width int, height int) ([]byte, error) {
img := image.NewRGBA(image.Rect(0, 0, width, height))
// write pixel data
for pixIdx, y := 0, 0; y < height; y++ {
data0 := avutil.Data(frame)[0]
startPos := uintptr(unsafe.Pointer(data0)) + uintptr(y)*uintptr(avutil.Linesize(frame)[0])
for i := 0; i < width*4; i++ { // width*4 == r,g,b,a
element := *(*uint8)(unsafe.Pointer(startPos + uintptr(i)))
img.Pix[pixIdx] = element
pixIdx++
}
}
subImgRect, err := gameSubImage(klv)
if err != nil {
return nil, err
}
croppedFrame := img.SubImage(subImgRect)
var jpgBuf bytes.Buffer
jpeg.Encode(&jpgBuf, croppedFrame, nil)
return jpgBuf.Bytes(), nil
}
// convert video to an image with ffmpeg and send to PullText()
// code modified from goav tutorial
func video2Image(klv *KillLogVid, pt *PullText) (error) {
// open video file
v := klv.Vid
pFormatContext := avformat.AvformatAllocContext()
if avformat.AvformatOpenInput(&pFormatContext, v.File, nil, nil) != 0 {
return fmt.Errorf("unable to open file %s\n", v.File)
}
// retrieve stream information
if pFormatContext.AvformatFindStreamInfo(nil) < 0 {
return errors.New("couldn't find stream information")
}
// dump information about file onto standard error
// XXX: for debug
//pFormatContext.AvDumpFormat(0, v.File, 0)
// find the first video stream
for i := 0; i < int(pFormatContext.NbStreams()); i++ {
switch pFormatContext.Streams()[i].CodecParameters().AvCodecGetType() {
case avformat.AVMEDIA_TYPE_VIDEO:
stream := pFormatContext.Streams()[i]
// get a pointer to the codec context for the video stream
pCodecCtxOrig := stream.Codec()
// find the decoder for the video stream
pCodec := avcodec.AvcodecFindDecoder(avcodec.CodecId(pCodecCtxOrig.GetCodecId()))
if pCodec == nil {
// XXX: add granularity
return errors.New("unsupported codec")
}
// copy context
pCodecCtx := pCodec.AvcodecAllocContext3()
if pCodecCtx.AvcodecCopyContext((*avcodec.Context)(unsafe.Pointer(pCodecCtxOrig))) != 0 {
return errors.New("couldn't copy codec context")
}
// open codec
if pCodecCtx.AvcodecOpen2(pCodec, nil) < 0 {
return errors.New("couldn't open codec")
}
// allocate video frame
pFrame := avutil.AvFrameAlloc()
// allocate an AVFrame structure
pFrameRGBA := avutil.AvFrameAlloc()
if pFrameRGBA == nil {
return errors.New("unable to allocate an RGBA Frame")
}
// determine required buffer size and allocate buffer
numBytes := uintptr(avcodec.AvpictureGetSize(avcodec.AV_PIX_FMT_RGBA, pCodecCtx.Width(),
pCodecCtx.Height()))
buffer := avutil.AvMalloc(numBytes)
// assign appropriate parts of buffer to image planes in pFrameRGBA
// note that pFrameRGBA is an AVFrame, but AVFrame is a superset
// of AVPicture
avp := (*avcodec.Picture)(unsafe.Pointer(pFrameRGBA))
avp.AvpictureFill((*uint8)(buffer), avcodec.AV_PIX_FMT_RGBA, pCodecCtx.Width(), pCodecCtx.Height())
// initialize SWS context for software scaling
swsCtx := swscale.SwsGetcontext(
pCodecCtx.Width(),
pCodecCtx.Height(),
(swscale.PixelFormat)(pCodecCtx.PixFmt()),
pCodecCtx.Width(),
pCodecCtx.Height(),
avcodec.AV_PIX_FMT_RGBA,
avcodec.SWS_BILINEAR,
nil,
nil,
nil,
)
fpsReflect := reflect.ValueOf(stream).Elem().FieldByName("avg_frame_rate")//("time_base")
fpsNum := *(*int32)(unsafe.Pointer(fpsReflect.Field(0).UnsafeAddr()))
fpsDem := *(*int32)(unsafe.Pointer(fpsReflect.Field(1).UnsafeAddr()))
fps := uint64(0)
if fpsNum == 0 || fpsDem == 0 {
fmt.Println("couldn't get video FPS, fallback to reading every frame")
fps, fpsNum, fpsDem = 0, 0, 0
} else {
if fpsNum > fpsDem {
fpsDem, fpsNum = fpsNum, fpsDem // swap variables in case of endianness problems
}
fps = uint64(fpsDem / fpsNum)
fps = fps / 2 // every .5 sec XXX remove me!
fmt.Printf("video fps: %d scanning x2\n", fps)
}
// read frames
frameNum := uint64(0)
packet := avcodec.AvPacketAlloc()
for pFormatContext.AvReadFrame(packet) >= 0 {
// is this a packet from the video stream?
if packet.StreamIndex() == i {
// decode video frame
response := pCodecCtx.AvcodecSendPacket(packet)
if response < 0 {
fmt.Printf("Error while sending a packet to the decoder: %s\n", avutil.ErrorFromCode(response))
}
for response >= 0 {
response = pCodecCtx.AvcodecReceiveFrame((*avcodec.Frame)(unsafe.Pointer(pFrame)))
if response == avutil.AvErrorEAGAIN || response == avutil.AvErrorEOF ||
response == -11 /* FIXME tmp */ {
break
} else if response < 0 {
fmt.Printf("Error while receiving a frame from the decoder: %s\n", avutil.ErrorFromCode(response))
//fmt.Println(response)
return errors.New("video2Image(): decoder err")
}
// get 1 fps
// TODO replace w/ libavfilter
if frameNum % fps != 0 {
packet.AvFreePacket()
frameNum++
continue
}
// convert to RGBA
swscale.SwsScale2(swsCtx, avutil.Data(pFrame),
avutil.Linesize(pFrame), 0, pCodecCtx.Height(),
avutil.Data(pFrameRGBA), avutil.Linesize(pFrameRGBA))
img, err := saveFrame(klv, pFrameRGBA, pCodecCtx.Width(), pCodecCtx.Height())
if err != nil {
return err
}
if pt.err != nil {
return nil // problem with PullText(), don't add a superfluous error
}
klv.FrameChan <-img
frameNum++
}
}
// free the packet that was allocated by av_read_frame
packet.AvFreePacket()
}
// free the RGBA image
avutil.AvFree(buffer)
avutil.AvFrameFree(pFrameRGBA)
// free the YUV frame
avutil.AvFrameFree(pFrame)
// close the codecs
pCodecCtx.AvcodecClose()
(*avcodec.Context)(unsafe.Pointer(pCodecCtxOrig)).AvcodecClose()
// close the video file
pFormatContext.AvformatCloseInput()
// stop after saving frames of first video straem
break
default:
fmt.Println("couldn't find a video stream")
os.Exit(1)
}
}
return nil
}