Skip to content

Commit

Permalink
Review changes
Browse files Browse the repository at this point in the history
Reorder flags, and update them
# Update Example
Use renaming function to create sws context
Clean up
Use new framdata image funcs
# Sws scale context
New way to update the context
Use sws_scale_frame instead of sws_scale
Reordering funcs for get and set
# Sws sclate context flag
Add "Flag" for algo name
# Update sws test
  • Loading branch information
Cacsjep committed Jan 27, 2024
1 parent 101fbf8 commit 4724734
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 212 deletions.
58 changes: 35 additions & 23 deletions examples/scaling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,23 @@ import (
"image/png"
"log"
"os"
"strings"

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

func main() {
var (
output = flag.String("o", "", "the png output path")
dstWidth = flag.Int("w", 50, "destination width")
dstHeight = flag.Int("h", 50, "destination height")
)

var (
output = flag.String("o", "", "the png output path")
dstWidth = flag.Int("w", 50, "destination width")
dstHeight = flag.Int("h", 50, "destination height")
)
func main() {
// Handle ffmpeg logs
astiav.SetLogLevel(astiav.LogLevelDebug)
astiav.SetLogCallback(func(l astiav.LogLevel, fmt, msg, parent string) {
log.Printf("ffmpeg log: %s (level: %d)\n", strings.TrimSpace(msg), l)
})

// Parse flags
flag.Parse()
Expand Down Expand Up @@ -51,36 +57,42 @@ func main() {
dstFrame := astiav.AllocFrame()
defer dstFrame.Free()

// Create software scale context flags
swscf := astiav.NewSoftwareScaleContextFlags(astiav.SoftwareScaleContextBilinear)

// Create software scale context
swsCtx := astiav.NewSoftwareScaleContext(srcFrame.Width(), srcFrame.Height(), srcFrame.PixelFormat(), *dstWidth, *dstHeight, astiav.PixelFormatRgba, swscf)
if swsCtx == nil {
log.Fatal("main: creating software scale context failed")
swsCtx, err := astiav.CreateSoftwareScaleContext(
srcFrame.Width(),
srcFrame.Height(),
srcFrame.PixelFormat(),
*dstWidth,
*dstHeight,
astiav.PixelFormatRgba,
astiav.NewSoftwareScaleContextFlags(astiav.SoftwareScaleContextFlagBilinear),
)
if err != nil {
log.Fatal(fmt.Errorf("main: creating software scale context failed: %w"), err)

Check failure on line 71 in examples/scaling/main.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 71 in examples/scaling/main.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 71 in examples/scaling/main.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

fmt.Errorf format %w reads arg #1, but call has 0 args
}
defer swsCtx.Free()

// Prepare destination frame (Width, Height and Buffer for correct scaling would be set)
if err = swsCtx.PrepareDestinationFrameForScaling(dstFrame); err != nil {
log.Fatal(fmt.Errorf("main: prepare destination image failed: %w", err))
}

// Scale frame
if output_slice_height := swsCtx.ScaleFrame(srcFrame, dstFrame); output_slice_height != *dstHeight {
log.Fatal(fmt.Errorf("main: scale error, expected output slice height %d, but got %d", *dstHeight, output_slice_height))
if err := swsCtx.ScaleFrame(srcFrame, dstFrame); err != nil {
log.Fatal(fmt.Errorf("main: scale frame failed: %w"), err)

Check failure on line 77 in examples/scaling/main.go

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 77 in examples/scaling/main.go

View workflow job for this annotation

GitHub Actions / test (macos-latest)

fmt.Errorf format %w reads arg #1, but call has 0 args

Check failure on line 77 in examples/scaling/main.go

View workflow job for this annotation

GitHub Actions / test (windows-latest)

fmt.Errorf format %w reads arg #1, but call has 0 args
}

// Get image
img, err := dstFrame.Data().Image()
// Guess destination image format
img, err := dstFrame.Data().GuessImageFormat()
if err != nil {
log.Fatal(fmt.Errorf("main: getting destination image failed: %w", err))
log.Fatal(fmt.Errorf("main: guessing destination image format failed: %w", err))
}

// Copy frame data to destination image
if err = dstFrame.Data().ToImage(img); err != nil {
log.Fatal(fmt.Errorf("main: copying frame data to destination image failed: %w", err))
}

// Encode to png
if err = png.Encode(dstFile, img); err != nil {
log.Fatal(fmt.Errorf("main: encoding to png failed: %w", err))
}

log.Println("done")
// Success
log.Println("success")
}
64 changes: 32 additions & 32 deletions flags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 18 additions & 18 deletions flags_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions internal/cmd/flags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ var list = []listItem{
{Name: "CodecHardwareConfigMethod"},
{Name: "Dictionary"},
{Name: "FilterCommand"},
{Name: "FormatContextCtx"},
{Name: "FormatContext"},
{Name: "FormatContextCtx"},
{Name: "FormatEvent"},
{Name: "IOContext"},
{Name: "IOFormat"},
{Name: "Packet"},
{Name: "Seek"},
{Name: "StreamEvent"},
{Name: "SoftwareScaleContext"},
{Name: "StreamEvent"},
}

var tmpl = `// Code generated by astiav. DO NOT EDIT.
Expand Down
Loading

0 comments on commit 4724734

Please sign in to comment.