From dea57e26d23a9419e04b26d0485f2ad0404e4d26 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Sat, 8 Apr 2023 23:28:34 +0800 Subject: [PATCH] tmp --- simp.c | 2 +- simp.h | 1 + simp_ffmpeg_input.c | 27 ++++++++++++++++++++------- simp_filter.c | 14 ++++++++++++++ 4 files changed, 36 insertions(+), 8 deletions(-) diff --git a/simp.c b/simp.c index 4491bfc..fa77b46 100644 --- a/simp.c +++ b/simp.c @@ -10,7 +10,7 @@ static simp_avio_ops_t* avio_array[] = simp_frame_t* simp_frame_alloc() { - simp_frame_t* f = malloc(sizeof(simp_frame_t)); + simp_frame_t* f = calloc(1, sizeof(simp_frame_t)); if (!f) return NULL; diff --git a/simp.h b/simp.h index f209d87..8e2dba9 100644 --- a/simp.h +++ b/simp.h @@ -31,6 +31,7 @@ struct simp_avio_s scf_list_t vin; scf_list_t ain; + int nb_vframes; scf_list_t vout; scf_list_t aout; diff --git a/simp_ffmpeg_input.c b/simp_ffmpeg_input.c index ba00995..670f8cb 100644 --- a/simp_ffmpeg_input.c +++ b/simp_ffmpeg_input.c @@ -194,7 +194,7 @@ static int _ffmpeg_close(simp_avio_t* io) return 0; } -static int __ffmpeg_decode(simp_avio_t* io, AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame, scf_list_t* h) +static int __ffmpeg_decode(simp_avio_t* io, AVCodecContext* dec_ctx, AVPacket* pkt, AVFrame* frame, scf_list_t* h, int* nb_vframes) { int ret = avcodec_send_packet(dec_ctx, pkt); if (ret < 0) { @@ -214,16 +214,22 @@ static int __ffmpeg_decode(simp_avio_t* io, AVCodecContext* dec_ctx, AVPacket* p frame->pts = frame->best_effort_timestamp; - simp_frame_t* f = simp_frame_alloc(); + scf_logi("frame->pts: %ld, stream_index: %d\n", frame->pts, pkt->stream_index); + + simp_frame_t* f = calloc(1, sizeof(simp_frame_t)); if (!f) return -ENOMEM; - ret = av_frame_copy(f->frame, frame); - if (ret < 0) - return ret; + f->frame = av_frame_clone(frame); + if (!f->frame) + return -ENOMEM; pthread_mutex_lock(&io->mutex); scf_list_add_tail(h, &f->list); + + if (nb_vframes) + (*nb_vframes)++; + pthread_cond_signal(&io->cond); pthread_mutex_unlock(&io->mutex); } @@ -245,6 +251,13 @@ static void* __ffmpeg_run(void* arg) while (!io->exit) { + if (io->nb_vframes > 10) { + pthread_mutex_lock(&io->mutex); + pthread_cond_wait(&io->cond, &io->mutex); + pthread_mutex_unlock(&io->mutex); + continue; + } + int ret = av_read_frame(priv->fmt_ctx, pkt); if (ret < 0) { scf_loge("av_read_frame error, ret: %s\n", av_err2str(ret)); @@ -260,7 +273,7 @@ static void* __ffmpeg_run(void* arg) continue; } - ret = __ffmpeg_decode(io, priv->adec_ctx, pkt, priv->aframe, &io->ain); + ret = __ffmpeg_decode(io, priv->adec_ctx, pkt, priv->aframe, &io->ain, NULL); if (ret < 0) { io->error = ret; goto end; @@ -276,7 +289,7 @@ static void* __ffmpeg_run(void* arg) continue; } - ret = __ffmpeg_decode(io, priv->vdec_ctx, pkt, priv->vframe, &io->vin); + ret = __ffmpeg_decode(io, priv->vdec_ctx, pkt, priv->vframe, &io->vin, &io->nb_vframes); if (ret < 0) { io->error = ret; goto end; diff --git a/simp_filter.c b/simp_filter.c index 209b1be..760263f 100644 --- a/simp_filter.c +++ b/simp_filter.c @@ -212,6 +212,9 @@ static void* __filter_run(void* arg) scf_list_t* l; while (!f->exit) { + scf_loge("\n"); + + usleep(100); int64_t sys_time = gettime(); int vn = 0; @@ -239,6 +242,8 @@ static void* __filter_run(void* arg) scf_list_del(&vf->list); pthread_mutex_unlock(&io->mutex); + scf_logi("vf->pts: %ld, vtime: %ld, time: %ld\n", vf->frame->pts, vtime, time); + int ret = av_buffersrc_add_frame_flags(io->vbuffersrc_ctx, vf->frame, AV_BUFFERSRC_FLAG_KEEP_REF); simp_frame_free(vf); @@ -256,9 +261,11 @@ static void* __filter_run(void* arg) pthread_cond_signal(&io->cond); pthread_mutex_unlock(&io->mutex); } + scf_loge("\n"); } if (io->aopen) { + scf_loge("\n"); pthread_mutex_lock(&io->mutex); if (!scf_list_empty(&io->ain)) { @@ -272,6 +279,8 @@ static void* __filter_run(void* arg) scf_list_del(&af->list); pthread_mutex_unlock(&io->mutex); + scf_logi("af->pts: %ld, atime: %ld, time: %ld\n", af->frame->pts, atime, time); + int ret = av_buffersrc_add_frame_flags(io->abuffersrc_ctx, af->frame, AV_BUFFERSRC_FLAG_KEEP_REF); simp_frame_free(af); af = NULL; @@ -293,6 +302,7 @@ static void* __filter_run(void* arg) } while (1) { + scf_loge("\n"); int ret = av_buffersink_get_frame(f->vbuffersink_ctx, f->vframe); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { @@ -321,6 +331,7 @@ static void* __filter_run(void* arg) goto error; } + scf_loge("\n"); pthread_mutex_lock(&io->mutex); scf_list_add_tail(&io->vout, &vf->list); pthread_cond_signal(&io->cond); @@ -329,6 +340,7 @@ static void* __filter_run(void* arg) } } + scf_loge("\n"); while (1) { int ret = av_buffersink_get_frame(f->abuffersink_ctx, f->aframe); @@ -341,6 +353,7 @@ static void* __filter_run(void* arg) goto error; } + scf_loge("\n"); 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); @@ -358,6 +371,7 @@ static void* __filter_run(void* arg) goto error; } + scf_loge("\n"); pthread_mutex_lock(&io->mutex); scf_list_add_tail(&io->aout, &af->list); pthread_cond_signal(&io->cond); -- 2.25.1