mirror of
https://github.com/asticode/go-astiav.git
synced 2025-10-05 08:06:59 +08:00
Implementation for SWS scale (#33)
* Draft implementation for SWS scale
* Update go.mod
* Revert "Update go.mod"
This reverts commit 760fb8c427
.
* Renaming to AllocSwsContext and remove ChangeResolution this should handled by users
* update example to use new name and remove ChangeResolution
* Follow scaling example from libav, update readme, improve sws
Change scaling example to an similar libav example
Update readme
Add func to UpdateScalingParameters
Rename AllocSwsContext to SwsGetContext
Using a type for scaling algos/flags
* Fix Errors in scale example
* Review Changes
Now using sws context flags
Restructer sws context and adding ned simpler methods to update the ctx
Update the example
Update the test
* Correctly handle error for buffer alloc in PrepareDestinationFrameForScaling
* Add more getter and setter for sws
Get/Set source w,h,pixfmt
Get/Set dst w,h,pixfmt
Get/Set sws flags
* Adding resolution get/set
* Use CachedContext when updating sws ctx
* Review changes
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
* fix fmt error args bug
* Review Changes
Simpfy sws update
Use c.int for sws flags
update test
This commit is contained in:
98
examples/scaling/main.go
Normal file
98
examples/scaling/main.go
Normal file
@@ -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")
|
||||
)
|
||||
|
||||
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()
|
||||
|
||||
// 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()
|
||||
defer srcFrame.Free()
|
||||
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()
|
||||
defer dstFrame.Free()
|
||||
|
||||
// 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")
|
||||
}
|
Reference in New Issue
Block a user