-
Notifications
You must be signed in to change notification settings - Fork 170
/
sws_go112.go
108 lines (88 loc) · 2.07 KB
/
sws_go112.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
// +build go1.12
package gmf
/*
#cgo pkg-config: libswscale
#include "libswscale/swscale.h"
*/
import "C"
import (
"fmt"
"unsafe"
)
var (
SWS_FAST_BILINEAR int = C.SWS_FAST_BILINEAR
SWS_BILINEAR int = C.SWS_BILINEAR
SWS_BICUBIC int = C.SWS_BICUBIC
SWS_X int = C.SWS_X
SWS_POINT int = C.SWS_POINT
SWS_AREA int = C.SWS_AREA
SWS_BICUBLIN int = C.SWS_BICUBLIN
SWS_GAUSS int = C.SWS_GAUSS
SWS_SINC int = C.SWS_SINC
SWS_LANCZOS int = C.SWS_LANCZOS
SWS_SPLINE int = C.SWS_SPLINE
)
type SwsCtx struct {
swsCtx *C.struct_SwsContext
width int
height int
pixfmt int32
}
func NewSwsCtx(srcW, srcH int, srcPixFmt int32, dstW, dstH int, dstPixFmt int32, method int) (*SwsCtx, error) {
ctx := C.sws_getContext(
C.int(srcW),
C.int(srcH),
srcPixFmt,
C.int(dstW),
C.int(dstH),
dstPixFmt,
C.int(method), nil, nil, nil,
)
if ctx == nil {
return nil, fmt.Errorf("error creating sws context\n")
}
return &SwsCtx{
swsCtx: ctx,
width: dstW,
height: dstH,
pixfmt: dstPixFmt,
}, nil
}
func (ctx *SwsCtx) Scale(src *Frame, dst *Frame) {
C.sws_scale(
ctx.swsCtx,
(**C.uint8_t)(unsafe.Pointer(&src.avFrame.data)),
(*C.int)(unsafe.Pointer(&src.avFrame.linesize)),
0,
C.int(src.Height()),
(**C.uint8_t)(unsafe.Pointer(&dst.avFrame.data)),
(*C.int)(unsafe.Pointer(&dst.avFrame.linesize)))
}
func (ctx *SwsCtx) Free() {
if ctx.swsCtx != nil {
C.sws_freeContext(ctx.swsCtx)
}
}
func DefaultRescaler(ctx *SwsCtx, frames []*Frame) ([]*Frame, error) {
var (
result []*Frame = make([]*Frame, 0)
tmp *Frame
err error
)
for i, _ := range frames {
tmp = NewFrame().SetWidth(ctx.width).SetHeight(ctx.height).SetFormat(ctx.pixfmt)
if err = tmp.ImgAlloc(); err != nil {
return nil, fmt.Errorf("error allocation tmp frame - %s", err)
}
ctx.Scale(frames[i], tmp)
tmp.SetPts(frames[i].Pts())
tmp.SetPktDts(frames[i].PktDts())
result = append(result, tmp)
}
for i := 0; i < len(frames); i++ {
if frames[i] != nil {
frames[i].Free()
}
}
return result, nil
}