From 6b8d86347107a634e4a6b615e1fc93ff9c142987 Mon Sep 17 00:00:00 2001 From: Toby Date: Tue, 15 Mar 2022 16:44:52 -0700 Subject: [PATCH 1/5] fix incorrect ImageFrame memory ownership handling --- mediapipe_api/framework/formats/image_frame.cc | 7 +++++-- mediapipe_api/framework/formats/image_frame.h | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/mediapipe_api/framework/formats/image_frame.cc b/mediapipe_api/framework/formats/image_frame.cc index cbd42f7..b7634a3 100644 --- a/mediapipe_api/framework/formats/image_frame.cc +++ b/mediapipe_api/framework/formats/image_frame.cc @@ -21,10 +21,13 @@ MpReturnCode mp_ImageFrame__ui_i_i_ui(mediapipe::ImageFormat::Format format, int CATCH_ALL } -MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, Deleter* deleter, +MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, mediapipe::ImageFrame** image_frame_out) { TRY_ALL - *image_frame_out = new mediapipe::ImageFrame{format, width, height, width_step, pixel_data, deleter}; + // Copy pixel data + uint8* pixel_data_copy = new uint8[width_step * height]; + memcpy(pixel_data_copy, pixel_data, width_step * height); + *image_frame_out = new mediapipe::ImageFrame{format, width, height, width_step, pixel_data_copy}; RETURN_CODE(MpReturnCode::Success); CATCH_ALL } diff --git a/mediapipe_api/framework/formats/image_frame.h b/mediapipe_api/framework/formats/image_frame.h index 4d0b1ed..d4c585a 100644 --- a/mediapipe_api/framework/formats/image_frame.h +++ b/mediapipe_api/framework/formats/image_frame.h @@ -25,7 +25,7 @@ MP_CAPI(MpReturnCode) mp_ImageFrame__(mediapipe::ImageFrame** image_frame_out); MP_CAPI(MpReturnCode) mp_ImageFrame__ui_i_i_ui(mediapipe::ImageFormat::Format format, int width, int height, uint32 alignment_boundary, mediapipe::ImageFrame** image_frame_out); MP_CAPI(MpReturnCode) mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, - Deleter* deleter, mediapipe::ImageFrame** image_frame_out); + mediapipe::ImageFrame** image_frame_out); MP_CAPI(void) mp_ImageFrame__delete(mediapipe::ImageFrame* image_frame); MP_CAPI(bool) mp_ImageFrame__IsEmpty(mediapipe::ImageFrame* image_frame); MP_CAPI(MpReturnCode) mp_ImageFrame__SetToZero(mediapipe::ImageFrame* image_frame); From 49a8cda5c23d59c2f55c23dba4a5da9f55ad9582 Mon Sep 17 00:00:00 2001 From: Toby Date: Wed, 16 Mar 2022 18:50:13 -0700 Subject: [PATCH 2/5] Update mediapipe_api/framework/formats/image_frame.cc Co-authored-by: Speykious --- mediapipe_api/framework/formats/image_frame.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/mediapipe_api/framework/formats/image_frame.cc b/mediapipe_api/framework/formats/image_frame.cc index b7634a3..ce628ab 100644 --- a/mediapipe_api/framework/formats/image_frame.cc +++ b/mediapipe_api/framework/formats/image_frame.cc @@ -25,9 +25,10 @@ MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format form mediapipe::ImageFrame** image_frame_out) { TRY_ALL // Copy pixel data - uint8* pixel_data_copy = new uint8[width_step * height]; - memcpy(pixel_data_copy, pixel_data, width_step * height); - *image_frame_out = new mediapipe::ImageFrame{format, width, height, width_step, pixel_data_copy}; + mediapipe::ImageFrame* output_frame = absl::make_unique(); + output_frame->CopyPixelData(format, width, + height, width_step, pixel_data, mediapipe::ImageFrame::kDefaultAlignmentBoundary); + *image_frame_out = output_frame; RETURN_CODE(MpReturnCode::Success); CATCH_ALL } From 7e9e067d3c5d03bb6acaabb68a5d3f3772019360 Mon Sep 17 00:00:00 2001 From: Toby Date: Wed, 16 Mar 2022 19:29:01 -0700 Subject: [PATCH 3/5] code fix --- mediapipe_api/framework/formats/image_frame.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe_api/framework/formats/image_frame.cc b/mediapipe_api/framework/formats/image_frame.cc index ce628ab..4298700 100644 --- a/mediapipe_api/framework/formats/image_frame.cc +++ b/mediapipe_api/framework/formats/image_frame.cc @@ -25,7 +25,7 @@ MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format form mediapipe::ImageFrame** image_frame_out) { TRY_ALL // Copy pixel data - mediapipe::ImageFrame* output_frame = absl::make_unique(); + mediapipe::ImageFrame* output_frame = new mediapipe::ImageFrame{}; output_frame->CopyPixelData(format, width, height, width_step, pixel_data, mediapipe::ImageFrame::kDefaultAlignmentBoundary); *image_frame_out = output_frame; From e1ee6c66e2ef6be752ba9fea8a49e800be7d7c82 Mon Sep 17 00:00:00 2001 From: Ayane Date: Sun, 20 Mar 2022 22:59:49 +0800 Subject: [PATCH 4/5] Change function declaration to represent new type change Co-authored-by: Speykious --- mediapipe_api/framework/formats/image_frame.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe_api/framework/formats/image_frame.cc b/mediapipe_api/framework/formats/image_frame.cc index 4298700..4cef65d 100644 --- a/mediapipe_api/framework/formats/image_frame.cc +++ b/mediapipe_api/framework/formats/image_frame.cc @@ -21,7 +21,7 @@ MpReturnCode mp_ImageFrame__ui_i_i_ui(mediapipe::ImageFormat::Format format, int CATCH_ALL } -MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, +MpReturnCode mp_ImageFrame__ui_i_i_i_Pui8(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, mediapipe::ImageFrame** image_frame_out) { TRY_ALL // Copy pixel data From ec9bc8c0886e4018e985c3b098be74f4e44e51a4 Mon Sep 17 00:00:00 2001 From: Speykious Date: Sun, 20 Mar 2022 16:05:05 +0100 Subject: [PATCH 5/5] Update header file as well --- mediapipe_api/framework/formats/image_frame.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mediapipe_api/framework/formats/image_frame.h b/mediapipe_api/framework/formats/image_frame.h index d4c585a..6016b61 100644 --- a/mediapipe_api/framework/formats/image_frame.h +++ b/mediapipe_api/framework/formats/image_frame.h @@ -24,7 +24,7 @@ typedef void(Deleter)(uint8*); MP_CAPI(MpReturnCode) mp_ImageFrame__(mediapipe::ImageFrame** image_frame_out); MP_CAPI(MpReturnCode) mp_ImageFrame__ui_i_i_ui(mediapipe::ImageFormat::Format format, int width, int height, uint32 alignment_boundary, mediapipe::ImageFrame** image_frame_out); -MP_CAPI(MpReturnCode) mp_ImageFrame__ui_i_i_i_Pui8_PF(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, +MP_CAPI(MpReturnCode) mp_ImageFrame__ui_i_i_i_Pui8(mediapipe::ImageFormat::Format format, int width, int height, int width_step, uint8* pixel_data, mediapipe::ImageFrame** image_frame_out); MP_CAPI(void) mp_ImageFrame__delete(mediapipe::ImageFrame* image_frame); MP_CAPI(bool) mp_ImageFrame__IsEmpty(mediapipe::ImageFrame* image_frame);