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 all 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)
|Hardware Decoding|[see](examples/hardware_decoding/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/hw_decode.c)
|Remuxing|[see](examples/remuxing/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/remuxing.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
|Transcoding|[see](examples/transcoding/main.go)|[see](https://github.com/FFmpeg/FFmpeg/blob/n5.1.2/doc/examples/transcoding.c)

*Tip: you can use the video sample located in the `testdata` directory for your tests*
Expand Down
98 changes: 98 additions & 0 deletions examples/scaling/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package main

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

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

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

Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
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()
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved

// Usage
if *output == "" || *dstWidth <= 0 || *dstHeight <= 0 {
log.Println("Usage: <binary path> -o <output path> -w <output width> -h <output height>")
return
}

// Create destination file
dstFile, err := os.Create(*output)
if err != nil {
log.Fatal(fmt.Errorf("main: creating %s failed: %w", *output, err))
}
defer dstFile.Close()

// Create source frame
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
srcFrame.SetWidth(320)
srcFrame.SetHeight(240)
srcFrame.SetPixelFormat(astiav.PixelFormatYuv420P)
if err = srcFrame.AllocBuffer(1); err != nil {
log.Fatal(fmt.Errorf("main: allocating source frame buffer failed: %w", err))
}
if err = srcFrame.ImageFillBlack(); err != nil {
log.Fatal(fmt.Errorf("main: filling source frame with black image failed: %w", err))
}

// Create destination frame
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

// Create software scale context
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))
}
defer swsCtx.Free()

// Scale frame
if err := swsCtx.ScaleFrame(srcFrame, dstFrame); err != nil {
log.Fatal(fmt.Errorf("main: scaling frame failed: %w", err))
}

// Guess destination image format
img, err := dstFrame.Data().GuessImageFormat()
if err != nil {
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))
}

// Success
log.Println("success")
}
52 changes: 36 additions & 16 deletions flags.go

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

27 changes: 18 additions & 9 deletions flags_test.go

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

3 changes: 2 additions & 1 deletion internal/cmd/flags/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ 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: "SoftwareScaleContext"},
Cacsjep marked this conversation as resolved.
Show resolved Hide resolved
{Name: "StreamEvent"},
}

Expand Down
Loading
Loading