-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
188 lines (148 loc) · 4.32 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
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
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"
#include "libavcodec/avcodec.h"
#include "libavutil/imgutils.h"
uint32_t return_int (int num) {
return (uint32_t)(num);
}
uint8_t * gmf_alloc_buffer(int32_t fmt, int width, int height) {
int numBytes = av_image_get_buffer_size(fmt, width, height, 0);
return (uint8_t *) av_malloc(numBytes*sizeof(uint8_t));
}
*/
import "C"
import (
"bytes"
"errors"
"fmt"
"syscall"
"unsafe"
)
type AVRational C.struct_AVRational
type AVR struct {
Num int
Den int
}
const (
AVERROR_EOF = -541478725
// AV_ROUND_PASS_MINMAX = 8192
)
var AVERROR_EAGAIN = -int(syscall.EAGAIN)
var (
AV_TIME_BASE int = C.AV_TIME_BASE
AV_TIME_BASE_Q AVRational = AVRational{1, C.int(AV_TIME_BASE)}
)
func (a AVR) AVRational() AVRational {
return AVRational{C.int(a.Num), C.int(a.Den)}
}
func (a AVR) String() string {
return fmt.Sprintf("%d/%d", a.Num, a.Den)
}
func (a AVR) Av2qd() float64 {
return float64(a.Num) / float64(a.Den)
}
func (a AVR) Invert() AVR {
return AVR{Num: a.Den, Den: a.Num}
}
func (a AVRational) AVR() AVR {
return AVR{Num: int(a.num), Den: int(a.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 RescaleQRnd(a int64, encBase AVRational, stBase AVRational) int64 {
return int64(C.av_rescale_q_rnd(C.int64_t(a), C.struct_AVRational(encBase), C.struct_AVRational(stBase), C.AV_ROUND_NEAR_INF|C.AV_ROUND_PASS_MINMAX))
}
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 RescaleTs(pkt *Packet, encBase AVRational, stBase AVRational) {
C.av_packet_rescale_ts(&pkt.avPacket, C.struct_AVRational(encBase), C.struct_AVRational(stBase))
}
func GetSampleFmtName(fmt int32) string {
return C.GoString(C.av_get_sample_fmt_name(fmt))
}
func AvInvQ(q AVRational) AVRational {
avr := q.AVR()
return AVRational{C.int(avr.Den), C.int(avr.Num)}
}
// 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
}