---
 libavfilter/dnn/dnn_backend_torch.cpp | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_torch.cpp 
b/libavfilter/dnn/dnn_backend_torch.cpp
index 4c781cc0b6..fee1cfb3a7 100644
--- a/libavfilter/dnn/dnn_backend_torch.cpp
+++ b/libavfilter/dnn/dnn_backend_torch.cpp
@@ -47,6 +47,8 @@ typedef struct THModel {
 typedef struct THInferRequest {
     torch::Tensor *output;
     torch::Tensor *input_tensor;
+    float *input_data;       ///< persistent buffer for input data
+    size_t input_data_size;  ///< size of the persistent buffer
 } THInferRequest;
 
 typedef struct THRequestItem {
@@ -94,6 +96,7 @@ static void th_free_request(THInferRequest *request)
         delete(request->input_tensor);
         request->input_tensor = NULL;
     }
+    // Add these lines to free the persistent buffer
     if (request->input_data) {
         av_freep(&request->input_data);
         request->input_data_size = 0;
@@ -199,17 +202,12 @@ static int fill_model_input_th(THModel *th_model, 
THRequestItem *request)
     input.dims[height_idx] = task->in_frame->height;
     input.dims[width_idx] = task->in_frame->width;
 
-    // Calculate required size for the current frame
-    cur_size = input.dims[height_idx] * input.dims[width_idx] *
-               input.dims[channel_idx] * sizeof(float);
-
-    /**
-     * Dynamic Resizing Logic:
-     * Only reallocate if the existing buffer is too small or doesn't exist.
-     * Removed the (float *) cast to comply with FFmpeg style guidelines.
-     */
-    if (!infer_request->input_data || infer_request->input_data_size < 
cur_size) {
-        av_freep(&infer_request->input_data);
+    // Calculate required size for current frame
+    size_t cur_size = input.dims[height_idx] * input.dims[width_idx] *
+                      input.dims[channel_idx] * sizeof(float);
+
+    // Initial allocation
+    if (!infer_request->input_data) {
         infer_request->input_data = av_malloc(cur_size);
         if (!infer_request->input_data)
             return AVERROR(ENOMEM);
-- 
2.51.0

_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to