From 949e55f1407fb37dd8be895f6095c68f60a00e43 Mon Sep 17 00:00:00 2001 From: "yu.dongliang" <18588496441@163.com> Date: Mon, 10 Apr 2023 17:11:34 +0800 Subject: [PATCH] output --- simp_ffmpeg_input.c | 6 +++--- simp_ffmpeg_output.c | 48 ++++++++++++++++++++++++++++++++++++++------ simp_filter.c | 16 +++++++++++---- 3 files changed, 57 insertions(+), 13 deletions(-) diff --git a/simp_ffmpeg_input.c b/simp_ffmpeg_input.c index 553b780..28b3bea 100644 --- a/simp_ffmpeg_input.c +++ b/simp_ffmpeg_input.c @@ -251,16 +251,16 @@ static int __ffmpeg_decode(simp_avio_t* io, AVCodecContext* dec_ctx, AVPacket* p io->frame_rate.num = s->r_frame_rate.den; io->frame_rate.den = s->r_frame_rate.num; - frame->pts = frame->pts * av_q2d(s->time_base) / av_q2d(io->frame_rate); + f->frame->pts = f->frame->pts * av_q2d(s->time_base) / av_q2d(io->frame_rate); - scf_logi("frame->pts: %ld, stream_index: %d, s->r_frame_rate: %d:%d, s->time_base: %d:%d\n", + scf_logd("frame->pts: %ld, stream_index: %d, s->r_frame_rate: %d:%d, s->time_base: %d:%d\n", frame->pts, pkt->stream_index, s->r_frame_rate.num, s->r_frame_rate.den, s->time_base.num, s->time_base.den); } else { io->sample_rate = s->time_base; - scf_logi("frame->pts: %ld, stream_index: %d, s->time_base: %d:%d\n", + scf_logd("frame->pts: %ld, stream_index: %d, s->time_base: %d:%d\n", frame->pts, pkt->stream_index, s->time_base.num, s->time_base.den); } diff --git a/simp_ffmpeg_output.c b/simp_ffmpeg_output.c index ff6c607..c94cc90 100644 --- a/simp_ffmpeg_output.c +++ b/simp_ffmpeg_output.c @@ -6,7 +6,7 @@ static int _video_init(simp_ffmpeg_t* priv) AVStream* s; AVCodec* c; - c = avcodec_find_encoder(priv->fmt_ctx->oformat->video_codec); + c = avcodec_find_encoder(AV_CODEC_ID_H264); if (!c) return -EINVAL; @@ -29,6 +29,7 @@ static int _video_init(simp_ffmpeg_t* priv) priv->vcodec_ctx->gop_size = 30; priv->vcodec_ctx->max_b_frames = 2; priv->vcodec_ctx->pix_fmt = AV_PIX_FMT_YUV420P; + priv->vcodec_ctx->sample_aspect_ratio = (AVRational){16, 9}; s->time_base = priv->vcodec_ctx->time_base; if (priv->fmt_ctx->oformat->flags & AVFMT_GLOBALHEADER) @@ -36,7 +37,13 @@ static int _video_init(simp_ffmpeg_t* priv) int ret = avcodec_open2(priv->vcodec_ctx, c, NULL); if (ret < 0) { - printf("avcodec_parameters_to_context error, ret: %d, %s\n", ret, av_err2str(ret)); + scf_loge("avcodec_open2 error, ret: %d, %s\n", ret, av_err2str(ret)); + return ret; + } + + ret = avcodec_parameters_from_context(s->codecpar, priv->vcodec_ctx); + if (ret < 0) { + scf_loge("avcodec_parameters_from_context error, ret: %d, %s\n", ret, av_err2str(ret)); return ret; } @@ -52,7 +59,7 @@ static int _audio_init(simp_ffmpeg_t* priv) AVStream* s; AVCodec* c; - c = avcodec_find_encoder(priv->fmt_ctx->oformat->audio_codec); + c = avcodec_find_encoder(AV_CODEC_ID_AAC); if (!c) return -EINVAL; @@ -81,7 +88,13 @@ static int _audio_init(simp_ffmpeg_t* priv) int ret = avcodec_open2(priv->acodec_ctx, c, NULL); if (ret < 0) { - printf("avcodec_parameters_to_context error, ret: %d, %s\n", ret, av_err2str(ret)); + scf_loge("avcodec_parameters_to_context error, ret: %d, %s\n", ret, av_err2str(ret)); + return ret; + } + + ret = avcodec_parameters_from_context(s->codecpar, priv->acodec_ctx); + if (ret < 0) { + scf_loge("avcodec_parameters_from_context error, ret: %d, %s\n", ret, av_err2str(ret)); return ret; } @@ -247,6 +260,25 @@ static void* __ffmpeg_output_run(void* arg) simp_frame_t* f; scf_list_t* l; + int ret = 0; + + av_dump_format(priv->fmt_ctx, 0, priv->fmt_ctx->url, 1); + + if (!(priv->fmt_ctx->oformat->flags & AVFMT_NOFILE)) { + + ret = avio_open(&priv->fmt_ctx->pb, priv->fmt_ctx->url, AVIO_FLAG_WRITE); + if (ret < 0) { + scf_loge("Could not open output file '%s'", priv->fmt_ctx->url); + goto end; + } + } + + ret = avformat_write_header(priv->fmt_ctx, NULL); + if (ret < 0) { + scf_loge("avformat_write_header error, ret: %s\n", av_err2str(ret)); + goto end; + } + while (!io->exit) { pthread_mutex_lock(&io->mutex); @@ -257,7 +289,9 @@ static void* __ffmpeg_output_run(void* arg) scf_list_del(&f->list); pthread_mutex_unlock(&io->mutex); - int ret = __ffmpeg_encode(io, priv->vcodec_ctx, priv->vpkt, f->frame); + scf_logw("frame->pts: %ld\n", f->frame->pts); + + ret = __ffmpeg_encode(io, priv->vcodec_ctx, priv->vpkt, f->frame); simp_frame_free(f); f = NULL; @@ -274,7 +308,9 @@ static void* __ffmpeg_output_run(void* arg) scf_list_del(&f->list); pthread_mutex_unlock(&io->mutex); - int ret = av_audio_fifo_write(priv->afifo, (void**)f->frame->data, f->frame->nb_samples); + scf_logw("frame->pts: %ld\n", f->frame->pts); + + ret = av_audio_fifo_write(priv->afifo, (void**)f->frame->data, f->frame->nb_samples); simp_frame_free(f); f = NULL; diff --git a/simp_filter.c b/simp_filter.c index ad792a9..74a949e 100644 --- a/simp_filter.c +++ b/simp_filter.c @@ -96,7 +96,7 @@ static int _init_filters(simp_filter_t* f) } const enum AVPixelFormat pix_fmts[] = {AV_PIX_FMT_YUV420P, AV_PIX_FMT_NONE}; - const enum AVSampleFormat sample_fmts[] = {AV_SAMPLE_FMT_S16, -1}; + const enum AVSampleFormat sample_fmts[] = {AV_SAMPLE_FMT_FLTP, -1}; const int sample_rates[] = {44100, -1 }; const int64_t layouts[] = {AV_CH_LAYOUT_STEREO, -1}; @@ -230,7 +230,7 @@ static int _filter_add_video(simp_avio_t* io) io->nb_vframes--; pthread_mutex_unlock(&io->mutex); - scf_logi("vf->pts: %ld, vtime: %ld, time: %ld, nb_vframes: %d\n", vf->frame->pts, vtime, time, io->nb_vframes); + scf_logw("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_logi("af->pts: %ld, atime: %ld, time: %ld\n", af->frame->pts, atime, time); + scf_logw("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); @@ -309,7 +309,9 @@ static void* __filter_run(void* arg) int vn = 0; int an = 0; - for (l = scf_list_head(&f->inputs); l != scf_list_sentinel(&f->inputs); l != scf_list_next(l)) { + sleep(2); + + 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); if (io->start_time == 0) @@ -332,6 +334,7 @@ static void* __filter_run(void* arg) int ret = av_buffersink_get_frame(f->vbuffersink_ctx, f->vframe); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + scf_loge("\n"); break; } else if (ret < 0) { scf_loge("av_buffersink_get_frame error, ret: %s\n", av_err2str(ret)); @@ -340,6 +343,8 @@ static void* __filter_run(void* arg) goto error; } + scf_logw("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)) { io = scf_list_data(l, simp_avio_t, list); @@ -372,6 +377,7 @@ static void* __filter_run(void* arg) int ret = av_buffersink_get_frame(f->abuffersink_ctx, f->aframe); if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) { + scf_loge("\n"); break; } else if (ret < 0) { scf_loge("av_buffersink_get_frame error, ret: %s\n", av_err2str(ret)); @@ -398,6 +404,8 @@ static void* __filter_run(void* arg) goto error; } + scf_logw("af->frame->pts: %ld\n", af->frame->pts); + pthread_mutex_lock(&io->mutex); scf_list_add_tail(&io->aout, &af->list); pthread_cond_signal(&io->cond); -- 2.25.1