From 44cdf07654006dac5c75e53c7e05edbdb84954be Mon Sep 17 00:00:00 2001 From: kurochan Date: Mon, 14 Oct 2024 09:54:52 +0900 Subject: [PATCH 1/3] check if the frame is writable before copyPlanes --- frame_data.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/frame_data.go b/frame_data.go index 30e795e..d994f0c 100644 --- a/frame_data.go +++ b/frame_data.go @@ -284,6 +284,11 @@ func (f *frameDataFrame) copyPlanes(ps []frameDataPlane) (err error) { switch { // Video case f.height() > 0 && f.width() > 0: + // Check writability + if !f.f.IsWritable() { + return errors.New("astiav: frame is not writable") + } + // Loop through planes var cdata [8]*C.uint8_t var clinesizes [8]C.int From 5d13a7bc765be5447da9210f676e54734f905d17 Mon Sep 17 00:00:00 2001 From: kurochan Date: Mon, 14 Oct 2024 22:08:27 +0900 Subject: [PATCH 2/3] apply review --- frame_data.go | 16 ++++++++-------- frame_data_test.go | 9 +++++++++ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/frame_data.go b/frame_data.go index d994f0c..273e714 100644 --- a/frame_data.go +++ b/frame_data.go @@ -280,15 +280,15 @@ func (f *frameDataFrame) bytes(align int) ([]byte, error) { return nil, errors.New("astiav: frame type not implemented") } -func (f *frameDataFrame) copyPlanes(ps []frameDataPlane) (err error) { +func (f *frameDataFrame) copyPlanes(ps []frameDataPlane) error { + // Check writability + if !f.f.IsWritable() { + return errors.New("astiav: frame is not writable") + } + switch { // Video case f.height() > 0 && f.width() > 0: - // Check writability - if !f.f.IsWritable() { - return errors.New("astiav: frame is not writable") - } - // Loop through planes var cdata [8]*C.uint8_t var clinesizes [8]C.int @@ -305,9 +305,9 @@ func (f *frameDataFrame) copyPlanes(ps []frameDataPlane) (err error) { // Copy image C.av_image_copy(&f.f.c.data[0], &f.f.c.linesize[0], &cdata[0], &clinesizes[0], (C.enum_AVPixelFormat)(f.f.c.format), f.f.c.width, f.f.c.height) - return + return nil } - return + return nil } func (f *frameDataFrame) height() int { diff --git a/frame_data_test.go b/frame_data_test.go index db0f30a..25c9483 100644 --- a/frame_data_test.go +++ b/frame_data_test.go @@ -518,5 +518,14 @@ func TestFrameData(t *testing.T) { require.NoError(t, err) b9 := []byte(fmt.Sprintf("%+v", b8)) require.Equal(t, b3, b9) + + f3 := AllocFrame() + defer f3.Free() + require.NoError(t, f3.Ref(f2)) + require.Error(t, fd2.FromImage(i1)) + require.Error(t, fd2.SetBytes(b1, align)) + f3.MakeWritable() + require.NoError(t, fd2.FromImage(i1)) + require.NoError(t, fd2.SetBytes(b1, align)) } } From fd653ce04fc23c25857132b042ecb84e4cb00708 Mon Sep 17 00:00:00 2001 From: kurochan Date: Mon, 14 Oct 2024 23:38:15 +0900 Subject: [PATCH 3/3] make f2 writable --- frame_data_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frame_data_test.go b/frame_data_test.go index 25c9483..968528c 100644 --- a/frame_data_test.go +++ b/frame_data_test.go @@ -524,7 +524,7 @@ func TestFrameData(t *testing.T) { require.NoError(t, f3.Ref(f2)) require.Error(t, fd2.FromImage(i1)) require.Error(t, fd2.SetBytes(b1, align)) - f3.MakeWritable() + f2.MakeWritable() require.NoError(t, fd2.FromImage(i1)) require.NoError(t, fd2.SetBytes(b1, align)) }