From 166d53b7721d9d73ff428364995d2d7d493cdcc6 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Fri, 14 Apr 2023 20:26:41 +0800 Subject: [PATCH] use vector instead of list in filter --- simp.c | 1 - simp.h | 7 ++-- simp_filter.c | 95 ++++++++++++++++++++++++++++++++------------------- 3 files changed, 63 insertions(+), 40 deletions(-) diff --git a/simp.c b/simp.c index c3cf266..2b8acda 100644 --- a/simp.c +++ b/simp.c @@ -59,7 +59,6 @@ int simp_avio_open(simp_avio_t** pio, const char* type, const char* in, const ch if (!io) return -ENOMEM; - scf_list_init(&io->list); scf_list_init(&io->vin); scf_list_init(&io->ain); scf_list_init(&io->vout); diff --git a/simp.h b/simp.h index a2fad70..3cab8e7 100644 --- a/simp.h +++ b/simp.h @@ -2,6 +2,7 @@ #define SIMP_H #include"scf_list.h" +#include"scf_vector.h" #include "libavformat/avformat.h" #include "libavcodec/avcodec.h" @@ -68,7 +69,7 @@ typedef struct { struct simp_avio_s { - scf_list_t list; + int refs; scf_list_t vin; scf_list_t ain; @@ -111,8 +112,8 @@ struct simp_avio_s struct simp_filter_s { - scf_list_t inputs; - scf_list_t outputs; + scf_vector_t* inputs; + scf_vector_t* outputs; AVFilterGraph* vgraph; AVFilterGraph* agraph; diff --git a/simp_filter.c b/simp_filter.c index 490f30d..4ff8054 100644 --- a/simp_filter.c +++ b/simp_filter.c @@ -34,10 +34,10 @@ static int _init_filters(simp_filter_t* f) simp_avio_t* io; scf_list_t* l; + int i; int n = 0; - for (l = scf_list_head(&f->inputs); l !=scf_list_sentinel(&f->inputs); l = scf_list_next(l)) - { - io = scf_list_data(l, simp_avio_t, list); + for (i = 0; i < f->inputs->size; i++) { + io = f->inputs->data[i]; if (io->width > 0 && io->height > 0 && io->vopen) { @@ -183,20 +183,20 @@ int simp_filter_open(simp_filter_t** pf) return -ENOMEM; f->vframe = av_frame_alloc(); - if (!f->vframe) { - free(f); - return -ENOMEM; - } + if (!f->vframe) + goto vframe_error; f->aframe = av_frame_alloc(); - if (!f->aframe) { - av_frame_free(&f->vframe); - free(f); - return -ENOMEM; - } + if (!f->aframe) + goto aframe_error; - scf_list_init(&f->inputs); - scf_list_init(&f->outputs); + f->inputs = scf_vector_alloc(); + if (!f->inputs) + goto inputs_error; + + f->outputs = scf_vector_alloc(); + if (!f->outputs) + goto outputs_error; pthread_mutex_init(&f->mutex, NULL); pthread_cond_init (&f->cond, NULL); @@ -205,6 +205,16 @@ int simp_filter_open(simp_filter_t** pf) *pf = f; return 0; + +outputs_error: + scf_vector_free(f->inputs); +inputs_error: + av_frame_free(&f->aframe); +aframe_error: + av_frame_free(&f->vframe); +vframe_error: + free(f); + return -ENOMEM; } int simp_filter_close(simp_filter_t* f, int flush) @@ -236,11 +246,9 @@ int simp_filter_close(simp_filter_t* f, int flush) av_frame_free(&f->vframe); av_frame_free(&f->aframe); - for (l = scf_list_head(&f->inputs); l != scf_list_sentinel(&f->inputs); ) { - io = scf_list_data(l, simp_avio_t, list); - l = scf_list_next(l); - - scf_list_del(&io->list); + int i; + for (i = 0; i < f->inputs->size; i++) { + io = f->inputs->data[i]; io->abuffersrc_ctx = NULL; io->vbuffersrc_ctx = NULL; @@ -248,22 +256,26 @@ int simp_filter_close(simp_filter_t* f, int flush) if (flush) io->flush = 1; - simp_avio_close(io); - io = NULL; + if (0 == --io->refs) + simp_avio_close(io); + + f->inputs->data[i] = NULL; } - for (l = scf_list_head(&f->outputs); l != scf_list_sentinel(&f->outputs); ) { - io = scf_list_data(l, simp_avio_t, list); - l = scf_list_next(l); + for (i = 0; i < f->outputs->size; i++) { + io = f->outputs->data[i]; if (flush) io->flush = 1; - scf_list_del(&io->list); - simp_avio_close(io); - io = NULL; + if (0 == --io->refs) + simp_avio_close(io); + + f->outputs->data[i] = NULL; } + scf_vector_free(f->inputs); + scf_vector_free(f->outputs); return 0; } @@ -382,13 +394,14 @@ static void* __filter_run(void* arg) } int flushed = 0; + int i; while (!f->exit) { usleep(100); - for (l = scf_list_head(&f->inputs); l != scf_list_sentinel(&f->inputs); l = scf_list_next(l)) { - io = scf_list_data(l, simp_avio_t, list); + for (i = 0; i < f->inputs->size; i++) { + io = f->inputs->data[i]; if (io->start_time == 0) io->start_time = gettime(); @@ -441,7 +454,7 @@ static void* __filter_run(void* arg) f->error = ret; goto error; } -#if 1 +#if 0 static int fd = -1; if (-1 == fd) fd = open("1.yuv", O_RDWR | O_CREAT | O_TRUNC, 0666); @@ -458,8 +471,8 @@ static void* __filter_run(void* arg) write(fd, f->vframe->data[2] + i * f->vframe->linesize[2], f->vframe->width / 2); } #endif - for (l = scf_list_head(&f->outputs); l != scf_list_sentinel(&f->outputs); l = scf_list_next(l)) { - io = scf_list_data(l, simp_avio_t, list); + for (i = 0; i < f->outputs->size; i++) { + io = f->outputs->data[i]; if (io->vopen) { vf = calloc(1, sizeof(simp_frame_t)); @@ -505,8 +518,8 @@ static void* __filter_run(void* arg) goto error; } - for (l = scf_list_head(&f->outputs); l != scf_list_sentinel(&f->outputs); l = scf_list_next(l)) { - io = scf_list_data(l, simp_avio_t, list); + for (i = 0; i < f->outputs->size; i++) { + io = f->outputs->data[i]; if (io->aopen) { af = calloc(1, sizeof(simp_frame_t)); @@ -563,7 +576,12 @@ int simp_filter_run(simp_filter_t* f) int simp_filter_add_input(simp_filter_t* f, simp_avio_t* input) { if (f && input) { - scf_list_add_tail(&f->inputs, &input->list); + + int ret = scf_vector_add(f->inputs, input); + if (ret < 0) + return ret; + + input->refs++; return 0; } @@ -574,7 +592,12 @@ int simp_filter_add_input(simp_filter_t* f, simp_avio_t* input) int simp_filter_add_output(simp_filter_t* f, simp_avio_t* output) { if (f && output) { - scf_list_add_tail(&f->outputs, &output->list); + + int ret = scf_vector_add(f->outputs, output); + if (ret < 0) + return ret; + + output->refs++; return 0; } -- 2.25.1