tmp
authoryu.dongliang <18588496441@163.com>
Sat, 8 Apr 2023 15:28:34 +0000 (23:28 +0800)
committeryu.dongliang <18588496441@163.com>
Sat, 8 Apr 2023 15:28:34 +0000 (23:28 +0800)
simp.c
simp.h
simp_ffmpeg_input.c
simp_filter.c

diff --git a/simp.c b/simp.c
index 4491bfc99c1ff529b5a3e0858c5bbc9356641302..fa77b468716af30e222c1f094ec2a82a0e77c7da 100644 (file)
--- 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 f209d878f8ff4cdafdd3813119746e2ba2a2c076..8e2dba9f99790ddd7444c7edf56c90c8f25affeb 100644 (file)
--- 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;
index ba0099591af5032d68620822756f1c87c941c78d..670f8cb976fd187be383c509d65ddb2a48d5a867 100644 (file)
@@ -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;
index 209b1bed8b280a5b01f14a0fd8997678e71e150c..760263f9c8e84dfb1e3e930e07df8b4d46a4c494 100644 (file)
@@ -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);