ok
authoryu.dongliang <18588496441@163.com>
Mon, 10 Apr 2023 10:09:24 +0000 (18:09 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 10 Apr 2023 10:09:24 +0000 (18:09 +0800)
simp_ffmpeg.h
simp_ffmpeg_output.c
simp_filter.c

index aabb7a5893e9c6fb2b79b4f273142420b463d687..945b05d694f282f84cf7a2a656b7164f038dc0bc 100644 (file)
@@ -13,6 +13,8 @@ typedef struct {
        int               vidx;
        int               aidx;
 
+       int               nb_samples;
+
        AVFrame*          vframe;
        AVFrame*          aframe;
        AVPacket*         vpkt;
index 323b64a478e6bf9a4ddb0684b946791365a77cba..b5e4cc17f8c4b869eb87b362a7747d09e9c853f1 100644 (file)
@@ -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;
index dc5b312d2cab0c16d2678f5a66d354f4e338ad8a..978da9af9eccf8500e47dd8bc6789141022a70c3 100644 (file)
@@ -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);