output
authoryu.dongliang <18588496441@163.com>
Mon, 10 Apr 2023 09:11:34 +0000 (17:11 +0800)
committeryu.dongliang <18588496441@163.com>
Mon, 10 Apr 2023 09:11:34 +0000 (17:11 +0800)
simp_ffmpeg_input.c
simp_ffmpeg_output.c
simp_filter.c

index 553b780851f24765230b4b8226ed147534236fd4..28b3bea548d9b5c8331c75f426a5ac8efe0047ab 100644 (file)
@@ -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);
                }
index ff6c607cad8df9f23a02d599f6ea9a0a827054f2..c94cc90597eae3f706e4a2a31addaf9d5b5a432b 100644 (file)
@@ -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;
index ad792a9cb30ae924fa0bd67c809a774391ef9e81..74a949e27d7fc5e5a559f792bf5baf25a3f4c3b4 100644 (file)
@@ -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);