History

avfilter/dnn_backend_tf: Simplify memory allocation

Zhao Zhili committed 2 years ago

Commit abfefbb1 parent a40df361 file · +13 −20Browse files

Unified · Split

libavfilter/dnn/dnn_backend_tf.c+13−20
@@ -37,8 +37,8 @@ · 36 unmodified lines
3737 #include <tensorflow/c/c_api.h>
3838
3939 typedef struct TFModel {
40+ DNNModel model;
4041 DnnContext *ctx;
41− DNNModel *model;
4242 TF_Graph *graph;
4343 TF_Session *session;
4444 TF_Status *status;
@@ -518,7 +518,7 @@ static void dnn_free_model_tf(DNNModel **model) · 473 unmodified lines
518518 TF_DeleteStatus(tf_model->status);
519519 }
520520 av_freep(&tf_model);
521− av_freep(&model);
521+ *model = NULL;
522522 }
523523
524524 static DNNModel *dnn_load_model_tf(DnnContext *ctx, DNNFunctionType func_type, AVFilterContext *filter_ctx)
@@ -526,18 +526,11 @@ static DNNModel *dnn_load_model_tf(DnnContext *ctx, DNNFunctionType func_type, A · 1 unmodified line
526526 DNNModel *model = NULL;
527527 TFModel *tf_model = NULL;
528528
529− model = av_mallocz(sizeof(DNNModel));
530− if (!model){
531− return NULL;
532− }
533−
534529 tf_model = av_mallocz(sizeof(TFModel));
535− if (!tf_model){
536− av_freep(&model);
530+ if (!tf_model)
537531 return NULL;
538− }
532+ model = &tf_model->model;
539533 model->model = tf_model;
540− tf_model->model = model;
541534 tf_model->ctx = ctx;
542535
543536 if (load_tf_model(tf_model, ctx->model_filename) != 0){
@@ -650,11 +643,11 @@ static int fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) { · 106 unmodified lines
650643 }
651644 input.data = (float *)TF_TensorData(infer_request->input_tensor);
652645
653− switch (tf_model->model->func_type) {
646+ switch (tf_model->model.func_type) {
654647 case DFT_PROCESS_FRAME:
655648 if (task->do_ioproc) {
656− if (tf_model->model->frame_pre_proc != NULL) {
657− tf_model->model->frame_pre_proc(task->in_frame, &input, tf_model->model->filter_ctx);
649+ if (tf_model->model.frame_pre_proc != NULL) {
650+ tf_model->model.frame_pre_proc(task->in_frame, &input, tf_model->model.filter_ctx);
658651 } else {
659652 ff_proc_from_frame_to_dnn(task->in_frame, &input, ctx);
660653 }
@@ -664,7 +657,7 @@ static int fill_model_input_tf(TFModel *tf_model, TFRequestItem *request) { · 3 unmodified lines
664657 ff_frame_to_dnn_detect(task->in_frame, &input, ctx);
665658 break;
666659 default:
667− avpriv_report_missing_feature(ctx, "model function type %d", tf_model->model->func_type);
660+ avpriv_report_missing_feature(ctx, "model function type %d", tf_model->model.func_type);
668661 break;
669662 }
670663
@@ -724,12 +717,12 @@ static void infer_completion_callback(void *args) { · 53 unmodified lines
724717 outputs[i].data = TF_TensorData(infer_request->output_tensors[i]);
725718 outputs[i].dt = (DNNDataType)TF_TensorType(infer_request->output_tensors[i]);
726719 }
727− switch (tf_model->model->func_type) {
720+ switch (tf_model->model.func_type) {
728721 case DFT_PROCESS_FRAME:
729722 //it only support 1 output if it's frame in & frame out
730723 if (task->do_ioproc) {
731− if (tf_model->model->frame_post_proc != NULL) {
732− tf_model->model->frame_post_proc(task->out_frame, outputs, tf_model->model->filter_ctx);
724+ if (tf_model->model.frame_post_proc != NULL) {
725+ tf_model->model.frame_post_proc(task->out_frame, outputs, tf_model->model.filter_ctx);
733726 } else {
734727 ff_proc_from_dnn_to_frame(task->out_frame, outputs, ctx);
735728 }
@@ -741,11 +734,11 @@ static void infer_completion_callback(void *args) { · 5 unmodified lines
741734 }
742735 break;
743736 case DFT_ANALYTICS_DETECT:
744− if (!tf_model->model->detect_post_proc) {
737+ if (!tf_model->model.detect_post_proc) {
745738 av_log(ctx, AV_LOG_ERROR, "Detect filter needs provide post proc\n");
746739 return;
747740 }
748− tf_model->model->detect_post_proc(task->in_frame, outputs, task->nb_output, tf_model->model->filter_ctx);
741+ tf_model->model.detect_post_proc(task->in_frame, outputs, task->nb_output, tf_model->model.filter_ctx);
749742 break;
750743 default:
751744 av_log(ctx, AV_LOG_ERROR, "Tensorflow backend does not support this kind of dnn filter now\n");

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t