forked from 3d0c/gmf
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.go
103 lines (80 loc) · 2.47 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
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"
"unsafe"
)
type AVRational C.struct_AVRational
type AVR struct {
Num int
Den int
}
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)}
}
var (
AV_TIME_BASE int = C.AV_TIME_BASE
AV_TIME_BASE_Q AVRational = AVRational{1, C.int(AV_TIME_BASE)}
)
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 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
}