Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for SWS scale #33

Merged
merged 18 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ Examples are located in the [examples](examples) directory and mirror as much as
|Filtering|[see](examples/filtering/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/filtering_video.c)
|Remuxing|[see](examples/remuxing/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/remuxing.c)
|Transcoding|[see](examples/transcoding/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/transcoding.c)
|Scaling|[see](examples/scaling/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/scaling_video.c)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

*Tip: you can use the video sample located in the `testdata` directory for your tests*

Expand Down
74 changes: 74 additions & 0 deletions examples/scaling/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package main

import (
"flag"
"fmt"
"image/png"
"log"
"os"

"github.com/asticode/go-astiav"
)

func main() {
var (
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
dstFilename string
dstWidth int
dstHeight int
)

flag.StringVar(&dstFilename, "output", "", "Output file name")
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
flag.IntVar(&dstWidth, "w", 0, "Destination width")
flag.IntVar(&dstHeight, "h", 0, "Destination height")
flag.Parse()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

if dstFilename == "" || dstWidth <= 0 || dstHeight <= 0 {
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
fmt.Fprintf(os.Stderr, "Usage: %s -output output_file -w W -h H\n", os.Args[0])
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
flag.PrintDefaults()
os.Exit(1)
}

dstFile, err := os.Create(dstFilename)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
fmt.Fprintf(os.Stderr, "Could not open destination file %s\n", dstFilename)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}
defer dstFile.Close()

srcW, srcH := 320, 240
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcPixFmt, dstPixFmt := astiav.PixelFormatYuv420P, astiav.PixelFormatRgba
srcFrame := astiav.AllocFrame()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcFrame.SetHeight(srcH)
srcFrame.SetWidth(srcW)
srcFrame.SetPixelFormat(srcPixFmt)
srcFrame.AllocBuffer(1)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcFrame.ImageFillBlack()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
defer srcFrame.Free()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

dstFrame := astiav.AllocFrame()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
defer dstFrame.Free()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

swsCtx := astiav.SwsGetContext(srcW, srcH, srcPixFmt, dstWidth, dstHeight, dstPixFmt, astiav.SWS_POINT, dstFrame)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if swsCtx == nil {
fmt.Fprintln(os.Stderr, "Unable to create scale context")
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
os.Exit(1)
}
defer swsCtx.Free()

err = swsCtx.Scale(srcFrame, dstFrame)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("Unable to scale: %s", err.Error())
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}

img, err := dstFrame.Data().Image()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("Unable to get image: %s", err.Error())
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}

err = png.Encode(dstFile, img)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("Unable to encode image to png: %s", err.Error())
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}

log.Printf("Successfully scale to %dx%d and write image to: %s", dstWidth, dstHeight, dstFilename)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}
111 changes: 111 additions & 0 deletions sws_context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
package astiav
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

//#cgo pkg-config: libswscale
//#include <libswscale/swscale.h>
import "C"
import (
"fmt"
)

// https://github.com/FFmpeg/FFmpeg/blob/n4.2.7/libswscale/swscale_internal.h#L280
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
type SWSContext struct {
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
c *C.struct_SwsContext
dstFormat PixelFormat
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcFormat PixelFormat
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcW int
srcH int
dstW int
dstH int
flags ScalingAlgorithm
dstFrame *Frame
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}

// https://github.com/FFmpeg/FFmpeg/blob/n5.0/libswscale/swscale.h#L59
type ScalingAlgorithm int
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

const (
SWS_FAST_BILINEAR ScalingAlgorithm = ScalingAlgorithm(C.SWS_FAST_BILINEAR)
SWS_BILINEAR ScalingAlgorithm = ScalingAlgorithm(C.SWS_BILINEAR)
SWS_BICUBIC ScalingAlgorithm = ScalingAlgorithm(C.SWS_BICUBIC)
SWS_X ScalingAlgorithm = ScalingAlgorithm(C.SWS_X)
SWS_POINT ScalingAlgorithm = ScalingAlgorithm(C.SWS_POINT)
SWS_AREA ScalingAlgorithm = ScalingAlgorithm(C.SWS_AREA)
SWS_BICUBLIN ScalingAlgorithm = ScalingAlgorithm(C.SWS_BICUBLIN)
SWS_GAUSS ScalingAlgorithm = ScalingAlgorithm(C.SWS_GAUSS)
SWS_SINC ScalingAlgorithm = ScalingAlgorithm(C.SWS_SINC)
SWS_LANCZOS ScalingAlgorithm = ScalingAlgorithm(C.SWS_LANCZOS)
SWS_SPLINE ScalingAlgorithm = ScalingAlgorithm(C.SWS_SPLINE)
)

func SwsGetContext(srcW, srcH int, srcFormat PixelFormat, dstW, dstH int, dstFormat PixelFormat, flags ScalingAlgorithm, dstFrame *Frame) *SWSContext {
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
dstFrame.SetPixelFormat(dstFormat)
dstFrame.SetWidth(dstW)
dstFrame.SetHeight(dstH)
dstFrame.AllocBuffer(1)

swsCtx := C.sws_getContext(
C.int(srcW),
C.int(srcH),
C.enum_AVPixelFormat(srcFormat),
C.int(dstW),
C.int(dstH),
C.enum_AVPixelFormat(dstFormat),
C.int(flags),
nil, nil, nil,
)
if swsCtx == nil {
return nil
}
return &SWSContext{c: swsCtx, dstFormat: dstFormat, srcFormat: srcFormat, srcW: srcW, srcH: srcH, dstW: dstW, dstH: dstH, flags: flags, dstFrame: dstFrame}
}

func (sc *SWSContext) Scale(srcFrame, dstFrame *Frame) error {
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
height := int(
C.sws_scale(
sc.c,
&srcFrame.c.data[0],
&srcFrame.c.linesize[0],
0,
C.int(srcFrame.Height()),
&dstFrame.c.data[0], &dstFrame.c.linesize[0]))

if height != dstFrame.Height() {
return fmt.Errorf("sws_scale did not process all lines, expected: %d, got: %d", dstFrame.Height(), height)
}
return nil
}

func (sc *SWSContext) UpdateScalingParameters(dstW, dstH int, dstFormat PixelFormat) error {
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
if sc.dstW != dstW || sc.dstH != dstH || sc.dstFormat != dstFormat {
sc.dstW = dstW
sc.dstH = dstH
sc.dstFormat = dstFormat

// Reallocate the destination frame buffer
sc.dstFrame.SetPixelFormat(dstFormat)
sc.dstFrame.SetWidth(dstW)
sc.dstFrame.SetHeight(dstH)
sc.dstFrame.AllocBuffer(1)

// Update the sws context
sc.c = C.sws_getCachedContext(
sc.c,
C.int(sc.srcW),
C.int(sc.srcH),
C.enum_AVPixelFormat(sc.srcFormat),
C.int(dstW),
C.int(dstH),
C.enum_AVPixelFormat(dstFormat),
C.int(sc.flags),
nil, nil, nil,
)
if sc.c == nil {
return fmt.Errorf("failed to update sws context")
}
}
return nil
}

func (sc *SWSContext) Free() {
C.sws_freeContext(sc.c)
}
66 changes: 66 additions & 0 deletions sws_context_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package astiav_test

import (
"image"
"reflect"
"testing"

"github.com/asticode/go-astiav"
"github.com/stretchr/testify/require"
)

// Test constants for source and destination dimensions and formats
const (
srcW = 100
srcH = 100
dstW = 200
dstH = 200
)

// assertImageType is a helper function to check the type of an image.
func assertImageType(t *testing.T, img image.Image, expectedType reflect.Type) {
actualType := reflect.TypeOf(img)
require.Equal(t, expectedType, actualType, "Image type does not match")
}

// TestSWS tests the scaling functionality provided by the SWSContext.
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
func TestSWS(t *testing.T) {
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
// Allocate and initialize source and destination frames
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcFrame := astiav.AllocFrame()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
defer srcFrame.Free()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
dstFrame := astiav.AllocFrame()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
defer dstFrame.Free()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
srcFrame.SetHeight(srcH)
srcFrame.SetWidth(srcW)
srcFrame.SetPixelFormat(astiav.PixelFormatYuv420P)
srcFrame.AllocBuffer(1)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
swsc := astiav.SwsGetContext(srcW, srcH, astiav.PixelFormatYuv420P, dstW, dstH, astiav.PixelFormatRgba, astiav.SWS_BILINEAR, dstFrame)
require.NotNil(t, swsc)

Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
err := swsc.Scale(srcFrame, dstFrame)
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
require.NoError(t, err)

require.Equal(t, dstW, dstFrame.Height())
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
require.Equal(t, dstH, dstFrame.Width())
require.Equal(t, astiav.PixelFormatRgba, dstFrame.PixelFormat())

// Convert frame data to image and perform additional verifications
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
i1, err := dstFrame.Data().Image()
require.NoError(t, err)
require.Equal(t, dstW, i1.Bounds().Dx())
require.Equal(t, dstH, i1.Bounds().Dy())
assertImageType(t, i1, reflect.TypeOf((*image.NRGBA)(nil)))
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

// Update sws ctx tests
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
err = swsc.UpdateScalingParameters(50, 50, astiav.PixelFormatRgb24)
require.NoError(t, err)
require.Equal(t, astiav.PixelFormatRgb24, dstFrame.PixelFormat())
err = swsc.Scale(srcFrame, dstFrame)
require.NoError(t, err)
require.Equal(t, dstFrame.Width(), 50)
require.Equal(t, dstFrame.Height(), 50)

swsc.Free()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
}