forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
148 lines (117 loc) · 3.25 KB
/
utils.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
package gmf
/*
#cgo pkg-config: libavcodec libavutil
#include "libavutil/avutil.h"
#include "libavutil/error.h"
#include "libavutil/mathematics.h"
#include "libavutil/rational.h"
#include "libavutil/samplefmt.h"
*/
import "C"
import (
"bytes"
"errors"
"syscall"
"unsafe"
)
type AVRational C.struct_AVRational
type AVR struct {
Num int
Den int
}
const (
AVERROR_EOF = -541478725
)
var (
AV_TIME_BASE int = C.AV_TIME_BASE
AV_TIME_BASE_Q AVRational = AVRational{1, C.int(AV_TIME_BASE)}
)
func (this AVR) AVRational() AVRational {
return AVRational{C.int(this.Num), C.int(this.Den)}
}
func (this AVRational) AVR() AVR {
return AVR{Num: int(this.num), Den: int(this.den)}
}
func AvError(averr int) error {
errlen := 1024
b := make([]byte, errlen)
C.av_strerror(C.int(averr), (*C.char)(unsafe.Pointer(&b[0])), C.size_t(errlen))
return errors.New(string(b[:bytes.Index(b, []byte{0})]))
}
func AvErrno(ret int) syscall.Errno {
if ret < 0 {
ret = -ret
}
return syscall.Errno(ret)
}
func RescaleQ(a int64, encBase AVRational, stBase AVRational) int64 {
return int64(C.av_rescale_q(C.int64_t(a), C.struct_AVRational(encBase), C.struct_AVRational(stBase)))
}
func CompareTimeStamp(aTimestamp int, aTimebase AVRational, bTimestamp int, bTimebase AVRational) int {
return int(C.av_compare_ts(C.int64_t(aTimestamp), C.struct_AVRational(aTimebase),
C.int64_t(bTimestamp), C.struct_AVRational(bTimebase)))
}
func RescaleDelta(inTb AVRational, inTs int64, fsTb AVRational, duration int, last *int64, outTb AVRational) int64 {
return int64(C.av_rescale_delta(C.struct_AVRational(inTb), C.int64_t(inTs), C.struct_AVRational(fsTb), C.int(duration), (*C.int64_t)(unsafe.Pointer(&last)), C.struct_AVRational(outTb)))
}
func Rescale(a, b, c int64) int64 {
return int64(C.av_rescale(C.int64_t(a), C.int64_t(b), C.int64_t(c)))
}
func GetSampleFmtName(fmt int32) string {
return C.GoString(C.av_get_sample_fmt_name(fmt))
}
// Synthetic video generator. It produces 25 iteratable frames.
// Used for tests.
func GenSyntVideoNewFrame(w, h int, fmt int32) chan *Frame {
yield := make(chan *Frame)
go func() {
defer close(yield)
for i := 0; i < 25; i++ {
frame := NewFrame().SetWidth(w).SetHeight(h).SetFormat(fmt)
if err := frame.ImgAlloc(); err != nil {
return
}
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
frame.SetData(0, y*frame.LineSize(0)+x, x+y+i*3)
}
}
// Cb and Cr
for y := 0; y < h/2; y++ {
for x := 0; x < w/2; x++ {
frame.SetData(1, y*frame.LineSize(1)+x, 128+y+i*2)
frame.SetData(2, y*frame.LineSize(2)+x, 64+x+i*5)
}
}
yield <- frame
}
}()
return yield
}
// tmp
func GenSyntVideoN(N, w, h int, fmt int32) chan *Frame {
yield := make(chan *Frame)
go func() {
defer close(yield)
for i := 0; i < N; i++ {
frame := NewFrame().SetWidth(w).SetHeight(h).SetFormat(fmt)
if err := frame.ImgAlloc(); err != nil {
return
}
for y := 0; y < h; y++ {
for x := 0; x < w; x++ {
frame.SetData(0, y*frame.LineSize(0)+x, x+y+i*3)
}
}
// Cb and Cr
for y := 0; y < h/2; y++ {
for x := 0; x < w/2; x++ {
frame.SetData(1, y*frame.LineSize(1)+x, 128+y+i*2)
frame.SetData(2, y*frame.LineSize(2)+x, 64+x+i*5)
}
}
yield <- frame
}
}()
return yield
}