Skip to content

Commit

Permalink
Correctly handle error for buffer alloc in PrepareDestinationFrameFor…
Browse files Browse the repository at this point in the history
…Scaling
  • Loading branch information
Cacsjep committed Jan 25, 2024
1 parent 124b0fa commit 2dcc530
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
4 changes: 3 additions & 1 deletion examples/scaling/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ func main() {
defer swsCtx.Free()

// Prepare destination frame (Width, Height and Buffer for correct scaling would be set)
swsCtx.PrepareDestinationFrameForScaling(dstFrame)
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 {
Expand Down
4 changes: 2 additions & 2 deletions software_scale_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,11 @@ func (ssc *SoftwareScaleContext) updateContext() error {
return nil
}

func (ssc *SoftwareScaleContext) PrepareDestinationFrameForScaling(dstFrame *Frame) {
func (ssc *SoftwareScaleContext) PrepareDestinationFrameForScaling(dstFrame *Frame) error {
dstFrame.SetPixelFormat(PixelFormat(ssc.dstFormat))
dstFrame.SetWidth(int(ssc.dstW))
dstFrame.SetHeight(int(ssc.dstH))
dstFrame.AllocBuffer(1)
return dstFrame.AllocBuffer(1)
}

func (ssc *SoftwareScaleContext) SetDestinationHeight(i int) error {
Expand Down
4 changes: 2 additions & 2 deletions software_scale_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func TestSoftwareScaleContext(t *testing.T) {
swsc := astiav.NewSoftwareScaleContext(srcW, srcH, srcPixelFormat, dstW, dstH, dstPixelFormat, swscf)
require.NotNil(t, swsc)

swsc.PrepareDestinationFrameForScaling(f2)
require.NoError(t, swsc.PrepareDestinationFrameForScaling(f2))
require.Equal(t, dstH, swsc.ScaleFrame(f1, f2))

require.Equal(t, dstW, f2.Height())
Expand All @@ -63,7 +63,7 @@ func TestSoftwareScaleContext(t *testing.T) {
require.NoError(t, swsc.SetDestinationHeight(dstH))
require.NoError(t, swsc.SetDestinationPixelFormat(dstPixelFormat))

swsc.PrepareDestinationFrameForScaling(f3)
require.NoError(t, swsc.PrepareDestinationFrameForScaling(f3))
require.Equal(t, f3.Height(), dstH)
require.Equal(t, f3.Width(), dstW)
require.Equal(t, f3.PixelFormat(), dstPixelFormat)
Expand Down

0 comments on commit 2dcc530

Please sign in to comment.