From 7692b3f7d2208092b0c69fb5e880304f5c6ff9c6 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Mon, 10 Apr 2023 18:09:24 +0800 Subject: [PATCH] ok --- simp_ffmpeg.h | 2 ++ simp_ffmpeg_output.c | 26 +++++++++++++++++++++++--- simp_filter.c | 14 +++++++------- 3 files changed, 32 insertions(+), 10 deletions(-) diff --git a/simp_ffmpeg.h b/simp_ffmpeg.h index aabb7a5..945b05d 100644 --- a/simp_ffmpeg.h +++ b/simp_ffmpeg.h @@ -13,6 +13,8 @@ typedef struct { int vidx; int aidx; + int nb_samples; + AVFrame* vframe; AVFrame* aframe; AVPacket* vpkt; diff --git a/simp_ffmpeg_output.c b/simp_ffmpeg_output.c index 323b64a..b5e4cc1 100644 --- a/simp_ffmpeg_output.c +++ b/simp_ffmpeg_output.c @@ -220,6 +220,7 @@ static int _ffmpeg_output_close(simp_avio_t* io) static int __ffmpeg_encode(simp_avio_t* io, AVCodecContext* c, AVPacket* pkt, AVFrame* frame, int idx) { simp_ffmpeg_t* priv = io->priv; + AVStream* s = NULL; int ret = avcodec_send_frame(c, frame); if (ret < 0) { @@ -230,14 +231,23 @@ static int __ffmpeg_encode(simp_avio_t* io, AVCodecContext* c, AVPacket* pkt, AV while (ret >= 0) { ret = avcodec_receive_packet(c, pkt); - if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) + if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { break; - else if (ret < 0) { + } else if (ret < 0) { scf_loge("avcodec_receive_packet error, ret: %s\n", av_err2str(ret)); return ret; } - scf_logw("idx: %d, frame->pts: %ld, pkt->stream_index: %d, pkt->pts: %ld\n", idx, frame->pts, pkt->stream_index, pkt->pts); + int64_t pts = pkt->pts; + + s = priv->fmt_ctx->streams[idx]; + + pkt->stream_index = idx; + +// pkt->pts = pkt->pts * av_q2d(c->time_base) / av_q2d(s->time_base); + + scf_logw("idx: %d, frame->pts: %ld, pkt->stream_index: %d, pkt->pts: %ld, pts: %ld, c->time_base: %d:%d, s->time_base: %d:%d\n", + idx, frame->pts, pkt->stream_index, pkt->pts, pts, c->time_base.num, c->time_base.den, s->time_base.num, s->time_base.den); ret = av_write_frame(priv->fmt_ctx, pkt); av_packet_unref(pkt); @@ -289,6 +299,8 @@ static void* __ffmpeg_output_run(void* arg) scf_list_del(&f->list); pthread_mutex_unlock(&io->mutex); + scf_logd("priv->vidx: %d, frame->pts: %ld\n", priv->vidx, f->frame->pts); + ret = __ffmpeg_encode(io, priv->vcodec_ctx, priv->vpkt, f->frame, priv->vidx); simp_frame_free(f); @@ -308,6 +320,9 @@ static void* __ffmpeg_output_run(void* arg) ret = av_audio_fifo_write(priv->afifo, (void**)f->frame->data, f->frame->nb_samples); + scf_logd("priv->aidx: %d, frame->pts: %ld, frame->nb_samples: %d, afifo->size: %d\n", + priv->aidx, f->frame->pts, f->frame->nb_samples, av_audio_fifo_size(priv->afifo)); + simp_frame_free(f); f = NULL; @@ -328,6 +343,11 @@ static void* __ffmpeg_output_run(void* arg) goto end; } + priv->nb_samples += priv->aframe->nb_samples; + priv->aframe->pts = priv->nb_samples; + + scf_logd("aframe->pts: %ld\n", priv->aframe->pts); + ret = __ffmpeg_encode(io, priv->acodec_ctx, priv->apkt, priv->aframe, priv->aidx); if (ret < 0) { io->error = ret; diff --git a/simp_filter.c b/simp_filter.c index dc5b312..978da9a 100644 --- a/simp_filter.c +++ b/simp_filter.c @@ -230,7 +230,7 @@ static int _filter_add_video(simp_avio_t* io) io->nb_vframes--; pthread_mutex_unlock(&io->mutex); - scf_logw("vf->pts: %ld, vtime: %ld, time: %ld, nb_vframes: %d\n", vf->frame->pts, vtime, time, io->nb_vframes); + scf_logd("vf->pts: %ld, vtime: %ld, time: %ld, nb_vframes: %d\n", vf->frame->pts, vtime, time, io->nb_vframes); int ret = av_buffersrc_add_frame_flags(io->vbuffersrc_ctx, vf->frame, AV_BUFFERSRC_FLAG_KEEP_REF); @@ -270,7 +270,7 @@ static int _filter_add_audio(simp_avio_t* io) scf_list_del(&af->list); pthread_mutex_unlock(&io->mutex); - scf_logw("af->pts: %ld, atime: %ld, time: %ld\n", af->frame->pts, atime, time); + scf_logd("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); @@ -342,9 +342,7 @@ static void* __filter_run(void* arg) goto error; } - scf_loge("f->frame->pts: %ld\n", f->vframe->pts); - - for (l = scf_list_head(&f->outputs); l != scf_list_sentinel(&f->outputs); l != scf_list_next(l)) { + 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); vf = calloc(1, sizeof(simp_frame_t)); @@ -362,6 +360,8 @@ static void* __filter_run(void* arg) goto error; } + scf_logd("vf->frame->pts: %ld\n", vf->frame->pts); + pthread_mutex_lock(&io->mutex); scf_list_add_tail(&io->vout, &vf->list); pthread_cond_signal(&io->cond); @@ -384,7 +384,7 @@ 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)) { + 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); af = calloc(1, sizeof(simp_frame_t)); @@ -402,7 +402,7 @@ static void* __filter_run(void* arg) goto error; } - scf_logw("af->frame->pts: %ld\n", af->frame->pts); + scf_logd("af->frame->pts: %ld\n", af->frame->pts); pthread_mutex_lock(&io->mutex); scf_list_add_tail(&io->aout, &af->list); -- 2.25.1