-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Now using sws context flags Restructer sws context and adding ned simpler methods to update the ctx Update the example Update the test
- Loading branch information
Showing
10 changed files
with
300 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
package astiav | ||
|
||
//#cgo pkg-config: libswscale | ||
//#include <libswscale/swscale.h> | ||
import "C" | ||
import ( | ||
"fmt" | ||
) | ||
|
||
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libswscale/swscale_internal.h#L300 | ||
type SoftwareScaleContext struct { | ||
c *C.struct_SwsContext | ||
// We need to store attributes in GO since C attributes are internal and therefore not accessible | ||
dstFormat C.enum_AVPixelFormat | ||
dstH C.int | ||
dstW C.int | ||
flags SoftwareScaleContextFlags | ||
srcFormat C.enum_AVPixelFormat | ||
srcH C.int | ||
srcW C.int | ||
} | ||
|
||
func NewSoftwareScaleContext(srcW, srcH int, srcFormat PixelFormat, dstW, dstH int, dstFormat PixelFormat, flags SoftwareScaleContextFlags) *SoftwareScaleContext { | ||
ssc := SoftwareScaleContext{ | ||
dstFormat: C.enum_AVPixelFormat(dstFormat), | ||
dstH: C.int(dstH), | ||
dstW: C.int(dstW), | ||
flags: flags, | ||
srcFormat: C.enum_AVPixelFormat(srcFormat), | ||
srcH: C.int(srcH), | ||
srcW: C.int(srcW), | ||
} | ||
|
||
ssc.c = C.sws_getContext( | ||
ssc.srcW, | ||
ssc.srcH, | ||
ssc.srcFormat, | ||
ssc.dstW, | ||
ssc.dstH, | ||
ssc.dstFormat, | ||
C.int(ssc.flags), | ||
nil, nil, nil, | ||
) | ||
if ssc.c == nil { | ||
return nil | ||
} | ||
return &ssc | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) ScaleFrame(src, dst *Frame) (height int) { | ||
height = int( | ||
C.sws_scale( | ||
ssc.c, | ||
&src.c.data[0], | ||
&src.c.linesize[0], | ||
0, | ||
C.int(src.Height()), | ||
&dst.c.data[0], &dst.c.linesize[0])) | ||
return | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) updateContext() error { | ||
ssc.c = C.sws_getContext( | ||
ssc.srcW, | ||
ssc.srcH, | ||
ssc.srcFormat, | ||
ssc.dstW, | ||
ssc.dstH, | ||
ssc.dstFormat, | ||
C.int(ssc.flags), | ||
nil, nil, nil, | ||
) | ||
if ssc.c == nil { | ||
return fmt.Errorf("failed to update sws context") | ||
} | ||
return nil | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) PrepareDestinationFrameForScaling(dstFrame *Frame) { | ||
dstFrame.SetPixelFormat(PixelFormat(ssc.dstFormat)) | ||
dstFrame.SetWidth(int(ssc.dstW)) | ||
dstFrame.SetHeight(int(ssc.dstH)) | ||
dstFrame.AllocBuffer(1) | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) SetDestinationHeight(i int) error { | ||
ssc.dstH = C.int(i) | ||
return ssc.updateContext() | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) SetDestinationWidth(i int) error { | ||
ssc.dstW = C.int(i) | ||
return ssc.updateContext() | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) SetDestinationPixelFormat(p PixelFormat) error { | ||
ssc.dstFormat = C.enum_AVPixelFormat(p) | ||
return ssc.updateContext() | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) SetSourceWidth(i int) error { | ||
ssc.srcW = C.int(i) | ||
return ssc.updateContext() | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) SetSourceHeight(i int) error { | ||
ssc.srcH = C.int(i) | ||
return ssc.updateContext() | ||
} | ||
|
||
func (ssc *SoftwareScaleContext) SetSourcePixelFormat(p PixelFormat) error { | ||
ssc.srcFormat = C.enum_AVPixelFormat(p) | ||
return ssc.updateContext() | ||
} | ||
|
||
func (sc *SoftwareScaleContext) Free() { | ||
C.sws_freeContext(sc.c) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package astiav | ||
|
||
//#cgo pkg-config: libswscale | ||
//#include <libswscale/swscale.h> | ||
import "C" | ||
|
||
type SoftwareScaleContextFlag int | ||
|
||
// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libswscale/swscale.h#L59 | ||
const ( | ||
SoftwareScaleContextArea = SoftwareScaleContextFlag(C.SWS_AREA) | ||
SoftwareScaleContextBicubic = SoftwareScaleContextFlag(C.SWS_BICUBIC) | ||
SoftwareScaleContextBicublin = SoftwareScaleContextFlag(C.SWS_BICUBLIN) | ||
SoftwareScaleContextBilinear = SoftwareScaleContextFlag(C.SWS_BILINEAR) | ||
SoftwareScaleContextFastBilinear = SoftwareScaleContextFlag(C.SWS_FAST_BILINEAR) | ||
SoftwareScaleContextGauss = SoftwareScaleContextFlag(C.SWS_GAUSS) | ||
SoftwareScaleContextLanczos = SoftwareScaleContextFlag(C.SWS_LANCZOS) | ||
SoftwareScaleContextPoint = SoftwareScaleContextFlag(C.SWS_POINT) | ||
SoftwareScaleContextSinc = SoftwareScaleContextFlag(C.SWS_SINC) | ||
SoftwareScaleContextSpline = SoftwareScaleContextFlag(C.SWS_SPLINE) | ||
SoftwareScaleContextX = SoftwareScaleContextFlag(C.SWS_X) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
package astiav_test | ||
|
||
import ( | ||
"image" | ||
"testing" | ||
|
||
"github.com/asticode/go-astiav" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSoftwareScaleContext(t *testing.T) { | ||
f1 := astiav.AllocFrame() | ||
require.NotNil(t, f1) | ||
defer f1.Free() | ||
|
||
f2 := astiav.AllocFrame() | ||
require.NotNil(t, f2) | ||
defer f2.Free() | ||
|
||
f3 := astiav.AllocFrame() | ||
require.NotNil(t, f3) | ||
defer f3.Free() | ||
|
||
srcW := 100 | ||
srcH := 100 | ||
srcPixelFormat := astiav.PixelFormatYuv420P | ||
dstW := 200 | ||
dstH := 200 | ||
dstPixelFormat := astiav.PixelFormatRgba | ||
|
||
f1.SetHeight(srcH) | ||
f1.SetWidth(srcW) | ||
f1.SetPixelFormat(srcPixelFormat) | ||
require.NoError(t, f1.AllocBuffer(1)) | ||
|
||
swscf := astiav.NewSoftwareScaleContextFlags(astiav.SoftwareScaleContextBilinear) | ||
swsc := astiav.NewSoftwareScaleContext(srcW, srcH, srcPixelFormat, dstW, dstH, dstPixelFormat, swscf) | ||
require.NotNil(t, swsc) | ||
|
||
swsc.PrepareDestinationFrameForScaling(f2) | ||
require.Equal(t, dstH, swsc.ScaleFrame(f1, f2)) | ||
|
||
require.Equal(t, dstW, f2.Height()) | ||
require.Equal(t, dstH, f2.Width()) | ||
require.Equal(t, dstPixelFormat, f2.PixelFormat()) | ||
|
||
i1, err := f2.Data().Image() | ||
require.NoError(t, err) | ||
require.Equal(t, dstW, i1.Bounds().Dx()) | ||
require.Equal(t, dstH, i1.Bounds().Dy()) | ||
_, nrgbaOk := i1.(*image.NRGBA) | ||
require.True(t, nrgbaOk) | ||
|
||
dstW = 50 | ||
dstH = 50 | ||
dstPixelFormat = astiav.PixelFormatYuv420P | ||
|
||
require.NoError(t, swsc.SetSourceWidth(f2.Width())) | ||
require.NoError(t, swsc.SetSourceHeight(f2.Height())) | ||
require.NoError(t, swsc.SetSourcePixelFormat(f2.PixelFormat())) | ||
|
||
require.NoError(t, swsc.SetDestinationWidth(dstW)) | ||
require.NoError(t, swsc.SetDestinationHeight(dstH)) | ||
require.NoError(t, swsc.SetDestinationPixelFormat(dstPixelFormat)) | ||
|
||
swsc.PrepareDestinationFrameForScaling(f3) | ||
require.Equal(t, f3.Height(), dstH) | ||
require.Equal(t, f3.Width(), dstW) | ||
require.Equal(t, f3.PixelFormat(), dstPixelFormat) | ||
require.Equal(t, dstH, swsc.ScaleFrame(f2, f3)) | ||
require.Equal(t, dstW, f3.Height()) | ||
require.Equal(t, dstH, f3.Width()) | ||
require.Equal(t, dstPixelFormat, f3.PixelFormat()) | ||
|
||
i2, err := f3.Data().Image() | ||
require.NoError(t, err) | ||
require.Equal(t, dstW, i2.Bounds().Dx()) | ||
require.Equal(t, dstH, i2.Bounds().Dy()) | ||
_, ycbcrOk := i2.(*image.YCbCr) | ||
require.True(t, ycbcrOk) | ||
|
||
defer swsc.Free() | ||
} |
Oops, something went wrong.