Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio
@ 2022-05-11  8:16 Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders Anton Khirnov
                   ` (9 more replies)
  0 siblings, 10 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

Call do_video_stats() for every video packets produced by the encoder,
rather than for every frame sent to the encoder.
---
 fftools/ffmpeg.c | 145 ++++++++++++++++++++---------------------------
 1 file changed, 60 insertions(+), 85 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index a85ed18b08..0e62c39522 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -831,63 +831,96 @@ static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
     return ret;
 }
 
-static void do_audio_out(OutputFile *of, OutputStream *ost,
-                         AVFrame *frame)
+static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 {
-    AVCodecContext *enc = ost->enc_ctx;
-    AVPacket *pkt = ost->pkt;
+    AVCodecContext   *enc = ost->enc_ctx;
+    AVPacket         *pkt = ost->pkt;
+    const char *type_desc = av_get_media_type_string(enc->codec_type);
     int ret;
 
-    adjust_frame_pts_to_encoder_tb(of, ost, frame);
-
-    if (!check_recording_time(ost))
-        return;
-
-    if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
-        frame->pts = ost->sync_opts;
-    ost->sync_opts = frame->pts + frame->nb_samples;
-    ost->samples_encoded += frame->nb_samples;
     ost->frames_encoded++;
 
     update_benchmark(NULL);
+
     if (debug_ts) {
-        av_log(NULL, AV_LOG_INFO, "encoder <- type:audio "
+        av_log(NULL, AV_LOG_INFO, "encoder <- type:%s "
                "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
+               type_desc,
                av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
                enc->time_base.num, enc->time_base.den);
     }
 
     ret = avcodec_send_frame(enc, frame);
-    if (ret < 0)
-        goto error;
+    if (ret < 0) {
+        av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
+               type_desc);
+        return ret;
+    }
 
     while (1) {
         ret = avcodec_receive_packet(enc, pkt);
+        update_benchmark("encode_%s %d.%d", type_desc,
+                         ost->file_index, ost->index);
         if (ret == AVERROR(EAGAIN))
-            break;
-        if (ret < 0)
-            goto error;
+            return 0;
+        else if (ret < 0) {
+            av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
+            return ret;
+        }
 
-        update_benchmark("encode_audio %d.%d", ost->file_index, ost->index);
+        if (debug_ts) {
+            av_log(NULL, AV_LOG_INFO, "encoder -> type:%s "
+                   "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
+                   "duration:%s duration_time:%s\n",
+                   type_desc,
+                   av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
+                   av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
+                   av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
+        }
 
         av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
 
         if (debug_ts) {
-            av_log(NULL, AV_LOG_INFO, "encoder -> type:audio "
+            av_log(NULL, AV_LOG_INFO, "encoder -> type:%s "
                    "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
                    "duration:%s duration_time:%s\n",
+                   type_desc,
                    av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
                    av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
                    av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
         }
 
+        if (enc->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename)
+            do_video_stats(ost, pkt->size);
+
         output_packet(of, pkt, ost, 0);
+
+        /* if two pass, output log */
+        if (ost->logfile && enc->stats_out)
+            fprintf(ost->logfile, "%s", enc->stats_out);
     }
 
-    return;
-error:
-    av_log(NULL, AV_LOG_FATAL, "Audio encoding failed\n");
-    exit_program(1);
+    av_assert0(0);
+}
+
+static void do_audio_out(OutputFile *of, OutputStream *ost,
+                         AVFrame *frame)
+{
+    int ret;
+
+    adjust_frame_pts_to_encoder_tb(of, ost, frame);
+
+    if (!check_recording_time(ost))
+        return;
+
+    if (frame->pts == AV_NOPTS_VALUE || audio_sync_method < 0)
+        frame->pts = ost->sync_opts;
+    ost->sync_opts = frame->pts + frame->nb_samples;
+    ost->samples_encoded += frame->nb_samples;
+
+    ret = encode_frame(of, ost, frame);
+    if (ret < 0)
+        exit_program(1);
 }
 
 static void do_subtitle_out(OutputFile *of,
@@ -979,14 +1012,12 @@ static void do_video_out(OutputFile *of,
                          AVFrame *next_picture)
 {
     int ret;
-    AVPacket *pkt = ost->pkt;
     AVCodecContext *enc = ost->enc_ctx;
     AVRational frame_rate;
     int nb_frames, nb0_frames, i;
     double delta, delta0;
     double duration = 0;
     double sync_ipts = AV_NOPTS_VALUE;
-    int frame_size = 0;
     InputStream *ist = NULL;
     AVFilterContext *filter = ost->filter->filter;
 
@@ -1179,73 +1210,17 @@ static void do_video_out(OutputFile *of,
             av_log(NULL, AV_LOG_DEBUG, "Forced keyframe at time %f\n", pts_time);
         }
 
-        update_benchmark(NULL);
-        if (debug_ts) {
-            av_log(NULL, AV_LOG_INFO, "encoder <- type:video "
-                   "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
-                   av_ts2str(in_picture->pts), av_ts2timestr(in_picture->pts, &enc->time_base),
-                   enc->time_base.num, enc->time_base.den);
-        }
-
-        ost->frames_encoded++;
-
-        ret = avcodec_send_frame(enc, in_picture);
+        ret = encode_frame(of, ost, in_picture);
         if (ret < 0)
-            goto error;
-        // Make sure Closed Captions will not be duplicated
-        av_frame_remove_side_data(in_picture, AV_FRAME_DATA_A53_CC);
-
-        while (1) {
-            ret = avcodec_receive_packet(enc, pkt);
-            update_benchmark("encode_video %d.%d", ost->file_index, ost->index);
-            if (ret == AVERROR(EAGAIN))
-                break;
-            if (ret < 0)
-                goto error;
-
-            if (debug_ts) {
-                av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
-                       "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
-                       "duration:%s duration_time:%s\n",
-                       av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &enc->time_base),
-                       av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &enc->time_base),
-                       av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
-            }
-
-            av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
-
-            if (debug_ts) {
-                av_log(NULL, AV_LOG_INFO, "encoder -> type:video "
-                    "pkt_pts:%s pkt_pts_time:%s pkt_dts:%s pkt_dts_time:%s "
-                    "duration:%s duration_time:%s\n",
-                    av_ts2str(pkt->pts), av_ts2timestr(pkt->pts, &ost->mux_timebase),
-                    av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, &ost->mux_timebase),
-                    av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &ost->mux_timebase));
-            }
-
-            frame_size = pkt->size;
-            output_packet(of, pkt, ost, 0);
+            exit_program(1);
 
-            /* if two pass, output log */
-            if (ost->logfile && enc->stats_out) {
-                fprintf(ost->logfile, "%s", enc->stats_out);
-            }
-        }
         ost->sync_opts++;
         ost->frame_number++;
-
-        if (vstats_filename && frame_size)
-            do_video_stats(ost, frame_size);
     }
 
     av_frame_unref(ost->last_frame);
     if (next_picture)
         av_frame_move_ref(ost->last_frame, next_picture);
-
-    return;
-error:
-    av_log(NULL, AV_LOG_FATAL, "Video encoding failed\n");
-    exit_program(1);
 }
 
 static double psnr(double d)
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11 18:53   ` Michael Niedermayer
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg: reindent after previous commit Anton Khirnov
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c | 75 +++++++++++-------------------------------------
 1 file changed, 16 insertions(+), 59 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0e62c39522..9fa0719cf6 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -836,12 +836,12 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
     AVCodecContext   *enc = ost->enc_ctx;
     AVPacket         *pkt = ost->pkt;
     const char *type_desc = av_get_media_type_string(enc->codec_type);
+    const char    *action = frame ? "encode" : "flush";
     int ret;
 
+    if (frame) {
     ost->frames_encoded++;
 
-    update_benchmark(NULL);
-
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "encoder <- type:%s "
                "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
@@ -849,9 +849,12 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
                av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
                enc->time_base.num, enc->time_base.den);
     }
+    }
+
+    update_benchmark(NULL);
 
     ret = avcodec_send_frame(enc, frame);
-    if (ret < 0) {
+    if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
         av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
                type_desc);
         return ret;
@@ -859,11 +862,15 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 
     while (1) {
         ret = avcodec_receive_packet(enc, pkt);
-        update_benchmark("encode_%s %d.%d", type_desc,
+        update_benchmark("%s_%s %d.%d", action, type_desc,
                          ost->file_index, ost->index);
-        if (ret == AVERROR(EAGAIN))
+        if (ret == AVERROR(EAGAIN)) {
+            av_assert0(frame); // should never happen during flushing
             return 0;
-        else if (ret < 0) {
+        } else if (ret == AVERROR_EOF) {
+            output_packet(of, pkt, ost, 1);
+            return ret;
+        } else if (ret < 0) {
             av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
             return ret;
         }
@@ -1762,59 +1769,9 @@ static void flush_encoders(void)
         if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
             continue;
 
-        for (;;) {
-            const char *desc = NULL;
-            AVPacket *pkt = ost->pkt;
-            int pkt_size;
-
-            switch (enc->codec_type) {
-            case AVMEDIA_TYPE_AUDIO:
-                desc   = "audio";
-                break;
-            case AVMEDIA_TYPE_VIDEO:
-                desc   = "video";
-                break;
-            default:
-                av_assert0(0);
-            }
-
-            update_benchmark(NULL);
-
-            while ((ret = avcodec_receive_packet(enc, pkt)) == AVERROR(EAGAIN)) {
-                ret = avcodec_send_frame(enc, NULL);
-                if (ret < 0) {
-                    av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
-                           desc,
-                           av_err2str(ret));
-                    exit_program(1);
-                }
-            }
-
-            update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index);
-            if (ret < 0 && ret != AVERROR_EOF) {
-                av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
-                       desc,
-                       av_err2str(ret));
-                exit_program(1);
-            }
-            if (ost->logfile && enc->stats_out) {
-                fprintf(ost->logfile, "%s", enc->stats_out);
-            }
-            if (ret == AVERROR_EOF) {
-                output_packet(of, pkt, ost, 1);
-                break;
-            }
-            if (ost->finished & MUXER_FINISHED) {
-                av_packet_unref(pkt);
-                continue;
-            }
-            av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
-            pkt_size = pkt->size;
-            output_packet(of, pkt, ost, 0);
-            if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
-                do_video_stats(ost, pkt_size);
-            }
-        }
+        ret = encode_frame(of, ost, NULL);
+        if (ret != AVERROR_EOF)
+            exit_program(1);
     }
 }
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg: reindent after previous commit
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 4/9] fftools/ffmpeg: drop a useless check and reduce indentation Anton Khirnov
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 9fa0719cf6..69b9c42822 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -840,15 +840,15 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
     int ret;
 
     if (frame) {
-    ost->frames_encoded++;
+        ost->frames_encoded++;
 
-    if (debug_ts) {
-        av_log(NULL, AV_LOG_INFO, "encoder <- type:%s "
-               "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
-               type_desc,
-               av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
-               enc->time_base.num, enc->time_base.den);
-    }
+        if (debug_ts) {
+            av_log(NULL, AV_LOG_INFO, "encoder <- type:%s "
+                   "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
+                   type_desc,
+                   av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
+                   enc->time_base.num, enc->time_base.den);
+        }
     }
 
     update_benchmark(NULL);
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 4/9] fftools/ffmpeg: drop a useless check and reduce indentation
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg: reindent after previous commit Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 5/9] fftools/ffmpeg: move do_video_stats() to avoid a forward declaration Anton Khirnov
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

do_video_stats() is only ever called for video.
---
 fftools/ffmpeg.c | 48 +++++++++++++++++++++++-------------------------
 1 file changed, 23 insertions(+), 25 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 69b9c42822..2bc612f448 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1251,31 +1251,29 @@ static void do_video_stats(OutputStream *ost, int frame_size)
     }
 
     enc = ost->enc_ctx;
-    if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-        frame_number = ost->st->nb_frames;
-        if (vstats_version <= 1) {
-            fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-                    ost->quality / (float)FF_QP2LAMBDA);
-        } else  {
-            fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
-                    ost->quality / (float)FF_QP2LAMBDA);
-        }
-
-        if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
-            fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
-
-        fprintf(vstats_file,"f_size= %6d ", frame_size);
-        /* compute pts value */
-        ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
-        if (ti1 < 0.01)
-            ti1 = 0.01;
-
-        bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
-        avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
-        fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
-               (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
-        fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
-    }
+    frame_number = ost->st->nb_frames;
+    if (vstats_version <= 1) {
+        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
+                ost->quality / (float)FF_QP2LAMBDA);
+    } else  {
+        fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
+                ost->quality / (float)FF_QP2LAMBDA);
+    }
+
+    if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
+        fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
+
+    fprintf(vstats_file,"f_size= %6d ", frame_size);
+    /* compute pts value */
+    ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
+    if (ti1 < 0.01)
+        ti1 = 0.01;
+
+    bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
+    avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
+    fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
+           (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
+    fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
 }
 
 static void finish_output_stream(OutputStream *ost)
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 5/9] fftools/ffmpeg: move do_video_stats() to avoid a forward declaration
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (2 preceding siblings ...)
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 4/9] fftools/ffmpeg: drop a useless check and reduce indentation Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 6/9] fftools/ffmpeg: stop using AVStream.nb_frames in do_video_stats() Anton Khirnov
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c | 93 ++++++++++++++++++++++++------------------------
 1 file changed, 46 insertions(+), 47 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 2bc612f448..ae3cc57bef 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -127,7 +127,6 @@ typedef struct BenchmarkTimeStamps {
     int64_t sys_usec;
 } BenchmarkTimeStamps;
 
-static void do_video_stats(OutputStream *ost, int frame_size);
 static BenchmarkTimeStamps get_benchmark_time_stamps(void);
 static int64_t getmaxrss(void);
 static int ifilter_has_all_input_formats(FilterGraph *fg);
@@ -831,6 +830,52 @@ static int init_output_stream_wrapper(OutputStream *ost, AVFrame *frame,
     return ret;
 }
 
+static double psnr(double d)
+{
+    return -10.0 * log10(d);
+}
+
+static void do_video_stats(OutputStream *ost, int frame_size)
+{
+    AVCodecContext *enc;
+    int frame_number;
+    double ti1, bitrate, avg_bitrate;
+
+    /* this is executed just the first time do_video_stats is called */
+    if (!vstats_file) {
+        vstats_file = fopen(vstats_filename, "w");
+        if (!vstats_file) {
+            perror("fopen");
+            exit_program(1);
+        }
+    }
+
+    enc = ost->enc_ctx;
+    frame_number = ost->st->nb_frames;
+    if (vstats_version <= 1) {
+        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
+                ost->quality / (float)FF_QP2LAMBDA);
+    } else  {
+        fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
+                ost->quality / (float)FF_QP2LAMBDA);
+    }
+
+    if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
+        fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
+
+    fprintf(vstats_file,"f_size= %6d ", frame_size);
+    /* compute pts value */
+    ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
+    if (ti1 < 0.01)
+        ti1 = 0.01;
+
+    bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
+    avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
+    fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
+           (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
+    fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
+}
+
 static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 {
     AVCodecContext   *enc = ost->enc_ctx;
@@ -1230,52 +1275,6 @@ static void do_video_out(OutputFile *of,
         av_frame_move_ref(ost->last_frame, next_picture);
 }
 
-static double psnr(double d)
-{
-    return -10.0 * log10(d);
-}
-
-static void do_video_stats(OutputStream *ost, int frame_size)
-{
-    AVCodecContext *enc;
-    int frame_number;
-    double ti1, bitrate, avg_bitrate;
-
-    /* this is executed just the first time do_video_stats is called */
-    if (!vstats_file) {
-        vstats_file = fopen(vstats_filename, "w");
-        if (!vstats_file) {
-            perror("fopen");
-            exit_program(1);
-        }
-    }
-
-    enc = ost->enc_ctx;
-    frame_number = ost->st->nb_frames;
-    if (vstats_version <= 1) {
-        fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-                ost->quality / (float)FF_QP2LAMBDA);
-    } else  {
-        fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", ost->file_index, ost->index, frame_number,
-                ost->quality / (float)FF_QP2LAMBDA);
-    }
-
-    if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
-        fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
-
-    fprintf(vstats_file,"f_size= %6d ", frame_size);
-    /* compute pts value */
-    ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
-    if (ti1 < 0.01)
-        ti1 = 0.01;
-
-    bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
-    avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
-    fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
-           (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
-    fprintf(vstats_file, "type= %c\n", av_get_picture_type_char(ost->pict_type));
-}
-
 static void finish_output_stream(OutputStream *ost)
 {
     OutputFile *of = output_files[ost->file_index];
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 6/9] fftools/ffmpeg: stop using AVStream.nb_frames in do_video_stats()
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (3 preceding siblings ...)
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 5/9] fftools/ffmpeg: move do_video_stats() to avoid a forward declaration Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 7/9] fftools/ffmpeg: stop using av_stream_get_end_pts() " Anton Khirnov
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

Its use for muxing is not documented, in practice it is incremented per
each packet successfully passed to the muxer's write_packet(). Since
there is a lot of indirection between ffmpeg receiving a packet from the
encoder and it actually being written (e.g. bitstream filters, the
interleaving queue), using nb_frames here is incorrect.

Add a new counter for packets received from encoder instead.
---
 fftools/ffmpeg.c | 4 +++-
 fftools/ffmpeg.h | 2 ++
 2 files changed, 5 insertions(+), 1 deletion(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index ae3cc57bef..c15d1486dd 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -851,7 +851,7 @@ static void do_video_stats(OutputStream *ost, int frame_size)
     }
 
     enc = ost->enc_ctx;
-    frame_number = ost->st->nb_frames;
+    frame_number = ost->packets_encoded;
     if (vstats_version <= 1) {
         fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
                 ost->quality / (float)FF_QP2LAMBDA);
@@ -945,6 +945,8 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
         if (enc->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename)
             do_video_stats(ost, pkt->size);
 
+        ost->packets_encoded++;
+
         output_packet(of, pkt, ost, 0);
 
         /* if two pass, output log */
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 9f0c093e34..7326193caf 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -551,6 +551,8 @@ typedef struct OutputStream {
     // number of frames/samples sent to the encoder
     uint64_t frames_encoded;
     uint64_t samples_encoded;
+    // number of packets received from the encoder
+    uint64_t packets_encoded;
 
     /* packet quality factor */
     int quality;
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 7/9] fftools/ffmpeg: stop using av_stream_get_end_pts() in do_video_stats()
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (4 preceding siblings ...)
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 6/9] fftools/ffmpeg: stop using AVStream.nb_frames in do_video_stats() Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 8/9] fftools/ffmpeg: merge variable declaration and initialization Anton Khirnov
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

It retrieves libavformat's internal dts value (contrary to the
function's name), which is not only incorrect in general, but also
unnecessary because we can access the packet directly.
---
 fftools/ffmpeg.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c15d1486dd..5983c57410 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -835,7 +835,7 @@ static double psnr(double d)
     return -10.0 * log10(d);
 }
 
-static void do_video_stats(OutputStream *ost, int frame_size)
+static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
 {
     AVCodecContext *enc;
     int frame_number;
@@ -863,13 +863,13 @@ static void do_video_stats(OutputStream *ost, int frame_size)
     if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
         fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width * enc->height * 255.0 * 255.0)));
 
-    fprintf(vstats_file,"f_size= %6d ", frame_size);
+    fprintf(vstats_file,"f_size= %6d ", pkt->size);
     /* compute pts value */
-    ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
+    ti1 = pkt->dts * av_q2d(ost->mux_timebase);
     if (ti1 < 0.01)
         ti1 = 0.01;
 
-    bitrate     = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
+    bitrate     = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
     avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
     fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= %7.1fkbits/s ",
            (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
@@ -943,7 +943,7 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
         }
 
         if (enc->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename)
-            do_video_stats(ost, pkt->size);
+            do_video_stats(ost, pkt);
 
         ost->packets_encoded++;
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 8/9] fftools/ffmpeg: merge variable declaration and initialization
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (5 preceding siblings ...)
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 7/9] fftools/ffmpeg: stop using av_stream_get_end_pts() " Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: move processing AV_PKT_DATA_QUALITY_STATS to do_video_stats() Anton Khirnov
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

---
 fftools/ffmpeg.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5983c57410..fe7c37db62 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -837,7 +837,7 @@ static double psnr(double d)
 
 static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
 {
-    AVCodecContext *enc;
+    AVCodecContext *enc = ost->enc_ctx;
     int frame_number;
     double ti1, bitrate, avg_bitrate;
 
@@ -850,7 +850,6 @@ static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
         }
     }
 
-    enc = ost->enc_ctx;
     frame_number = ost->packets_encoded;
     if (vstats_version <= 1) {
         fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: move processing AV_PKT_DATA_QUALITY_STATS to do_video_stats()
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (6 preceding siblings ...)
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 8/9] fftools/ffmpeg: merge variable declaration and initialization Anton Khirnov
@ 2022-05-11  8:16 ` Anton Khirnov
  2022-05-11  8:45 ` [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
  2022-05-24 12:03 ` Anton Khirnov
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:16 UTC (permalink / raw)
  To: ffmpeg-devel

This is a more appropriate place for this code, since the values we read
from AV_PKT_DATA_QUALITY_STATS side data are primarily written into
video stats. This ensures that the values written into stats actually
apply to the right packet.

Rename the function to update_video_stats() to better reflect its new
purpose.
---
 fftools/ffmpeg.c     | 23 +++++++++++++++++++----
 fftools/ffmpeg_mux.c | 13 -------------
 2 files changed, 19 insertions(+), 17 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index fe7c37db62..2706719206 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -835,13 +835,28 @@ static double psnr(double d)
     return -10.0 * log10(d);
 }
 
-static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
+static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int write_vstats)
 {
+    const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
+                                                NULL);
     AVCodecContext *enc = ost->enc_ctx;
     int frame_number;
     double ti1, bitrate, avg_bitrate;
 
-    /* this is executed just the first time do_video_stats is called */
+    ost->quality   = sd ? AV_RL32(sd) : -1;
+    ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
+
+    for (int i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
+        if (sd && i < sd[5])
+            ost->error[i] = AV_RL64(sd + 8 + 8*i);
+        else
+            ost->error[i] = -1;
+    }
+
+    if (!write_vstats)
+        return;
+
+    /* this is executed just the first time update_video_stats is called */
     if (!vstats_file) {
         vstats_file = fopen(vstats_filename, "w");
         if (!vstats_file) {
@@ -941,8 +956,8 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
                    av_ts2str(pkt->duration), av_ts2timestr(pkt->duration, &enc->time_base));
         }
 
-        if (enc->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename)
-            do_video_stats(ost, pkt);
+        if (enc->codec_type == AVMEDIA_TYPE_VIDEO)
+            update_video_stats(ost, pkt, !!vstats_filename);
 
         ost->packets_encoded++;
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 3cdaa494d7..794d580635 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -101,19 +101,6 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
         pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
     if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-        int i;
-        uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
-                                              NULL);
-        ost->quality = sd ? AV_RL32(sd) : -1;
-        ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
-
-        for (i = 0; i<FF_ARRAY_ELEMS(ost->error); i++) {
-            if (sd && i < sd[5])
-                ost->error[i] = AV_RL64(sd + 8 + 8*i);
-            else
-                ost->error[i] = -1;
-        }
-
         if (ost->frame_rate.num && ost->is_cfr) {
             if (pkt->duration > 0)
                 av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by frame rate, this should not happen\n");
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (7 preceding siblings ...)
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: move processing AV_PKT_DATA_QUALITY_STATS to do_video_stats() Anton Khirnov
@ 2022-05-11  8:45 ` Anton Khirnov
  2022-05-24 12:03 ` Anton Khirnov
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-11  8:45 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

In case anyone cares about the context - these patches were previously a
part of the muxer-threading set, but I've decided to factor them out and
reshuffle them. do_video_stats() is now kept close to the encoder rather
than being moved to the muxing code.

-- 
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders
  2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders Anton Khirnov
@ 2022-05-11 18:53   ` Michael Niedermayer
  2022-05-20 13:45     ` [FFmpeg-devel] [PATCH] " Anton Khirnov
  0 siblings, 1 reply; 13+ messages in thread
From: Michael Niedermayer @ 2022-05-11 18:53 UTC (permalink / raw)
  To: FFmpeg development discussions and patches


[-- Attachment #1.1: Type: text/plain, Size: 697 bytes --]

On Wed, May 11, 2022 at 10:16:47AM +0200, Anton Khirnov wrote:
> ---
>  fftools/ffmpeg.c | 75 +++++++++++-------------------------------------
>  1 file changed, 16 insertions(+), 59 deletions(-)

breaks 2pass with ffv1

./ffmpeg -y  -i mm-short.mpg -an -vcodec ffv1 -strict -2 -slices 4 -t 1 -coder 1 -level 2 -context 1 -bitexact -pass 1 /tmp/ffv1.2-p1.avi
./ffmpeg -y  -i mm-short.mpg -an -vcodec ffv1 -strict -2 -slices 4 -t 1 -coder 1 -level 2 -context 1 -bitexact -pass 2 /tmp/ffv1.2-p1.avi

[...]
-- 
Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

It is a danger to trust the dream we wish for rather than
the science we have, -- Dr. Kenneth Brown

[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]

[-- Attachment #2: Type: text/plain, Size: 251 bytes --]

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [FFmpeg-devel] [PATCH] fftools/ffmpeg: reuse the encoding code for flushing encoders
  2022-05-11 18:53   ` Michael Niedermayer
@ 2022-05-20 13:45     ` Anton Khirnov
  0 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-20 13:45 UTC (permalink / raw)
  To: ffmpeg-devel

---
Fixed 2pass breakage.

Sent a separate patchset adding tests for 2pass
---
 fftools/ffmpeg.c | 84 ++++++++++++------------------------------------
 1 file changed, 21 insertions(+), 63 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 0e62c39522..9b69a5a2c9 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -836,12 +836,12 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
     AVCodecContext   *enc = ost->enc_ctx;
     AVPacket         *pkt = ost->pkt;
     const char *type_desc = av_get_media_type_string(enc->codec_type);
+    const char    *action = frame ? "encode" : "flush";
     int ret;
 
+    if (frame) {
     ost->frames_encoded++;
 
-    update_benchmark(NULL);
-
     if (debug_ts) {
         av_log(NULL, AV_LOG_INFO, "encoder <- type:%s "
                "frame_pts:%s frame_pts_time:%s time_base:%d/%d\n",
@@ -849,9 +849,12 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
                av_ts2str(frame->pts), av_ts2timestr(frame->pts, &enc->time_base),
                enc->time_base.num, enc->time_base.den);
     }
+    }
+
+    update_benchmark(NULL);
 
     ret = avcodec_send_frame(enc, frame);
-    if (ret < 0) {
+    if (ret < 0 && !(ret == AVERROR_EOF && !frame)) {
         av_log(NULL, AV_LOG_ERROR, "Error submitting %s frame to the encoder\n",
                type_desc);
         return ret;
@@ -859,11 +862,20 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
 
     while (1) {
         ret = avcodec_receive_packet(enc, pkt);
-        update_benchmark("encode_%s %d.%d", type_desc,
+        update_benchmark("%s_%s %d.%d", action, type_desc,
                          ost->file_index, ost->index);
-        if (ret == AVERROR(EAGAIN))
+
+        /* if two pass, output log on success and EOF */
+        if ((ret >= 0 || ret == AVERROR_EOF) && ost->logfile && enc->stats_out)
+            fprintf(ost->logfile, "%s", enc->stats_out);
+
+        if (ret == AVERROR(EAGAIN)) {
+            av_assert0(frame); // should never happen during flushing
             return 0;
-        else if (ret < 0) {
+        } else if (ret == AVERROR_EOF) {
+            output_packet(of, pkt, ost, 1);
+            return ret;
+        } else if (ret < 0) {
             av_log(NULL, AV_LOG_ERROR, "%s encoding failed\n", type_desc);
             return ret;
         }
@@ -894,10 +906,6 @@ static int encode_frame(OutputFile *of, OutputStream *ost, AVFrame *frame)
             do_video_stats(ost, pkt->size);
 
         output_packet(of, pkt, ost, 0);
-
-        /* if two pass, output log */
-        if (ost->logfile && enc->stats_out)
-            fprintf(ost->logfile, "%s", enc->stats_out);
     }
 
     av_assert0(0);
@@ -1762,59 +1770,9 @@ static void flush_encoders(void)
         if (enc->codec_type != AVMEDIA_TYPE_VIDEO && enc->codec_type != AVMEDIA_TYPE_AUDIO)
             continue;
 
-        for (;;) {
-            const char *desc = NULL;
-            AVPacket *pkt = ost->pkt;
-            int pkt_size;
-
-            switch (enc->codec_type) {
-            case AVMEDIA_TYPE_AUDIO:
-                desc   = "audio";
-                break;
-            case AVMEDIA_TYPE_VIDEO:
-                desc   = "video";
-                break;
-            default:
-                av_assert0(0);
-            }
-
-            update_benchmark(NULL);
-
-            while ((ret = avcodec_receive_packet(enc, pkt)) == AVERROR(EAGAIN)) {
-                ret = avcodec_send_frame(enc, NULL);
-                if (ret < 0) {
-                    av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
-                           desc,
-                           av_err2str(ret));
-                    exit_program(1);
-                }
-            }
-
-            update_benchmark("flush_%s %d.%d", desc, ost->file_index, ost->index);
-            if (ret < 0 && ret != AVERROR_EOF) {
-                av_log(NULL, AV_LOG_FATAL, "%s encoding failed: %s\n",
-                       desc,
-                       av_err2str(ret));
-                exit_program(1);
-            }
-            if (ost->logfile && enc->stats_out) {
-                fprintf(ost->logfile, "%s", enc->stats_out);
-            }
-            if (ret == AVERROR_EOF) {
-                output_packet(of, pkt, ost, 1);
-                break;
-            }
-            if (ost->finished & MUXER_FINISHED) {
-                av_packet_unref(pkt);
-                continue;
-            }
-            av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
-            pkt_size = pkt->size;
-            output_packet(of, pkt, ost, 0);
-            if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && vstats_filename) {
-                do_video_stats(ost, pkt_size);
-            }
-        }
+        ret = encode_frame(of, ost, NULL);
+        if (ret != AVERROR_EOF)
+            exit_program(1);
     }
 }
 
-- 
2.34.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

* Re: [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio
  2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
                   ` (8 preceding siblings ...)
  2022-05-11  8:45 ` [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
@ 2022-05-24 12:03 ` Anton Khirnov
  9 siblings, 0 replies; 13+ messages in thread
From: Anton Khirnov @ 2022-05-24 12:03 UTC (permalink / raw)
  To: FFmpeg development discussions and patches

set pushed

-- 
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-05-24 12:03 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-11  8:16 [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 2/9] fftools/ffmpeg: reuse the encoding code for flushing encoders Anton Khirnov
2022-05-11 18:53   ` Michael Niedermayer
2022-05-20 13:45     ` [FFmpeg-devel] [PATCH] " Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 3/9] fftools/ffmpeg: reindent after previous commit Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 4/9] fftools/ffmpeg: drop a useless check and reduce indentation Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 5/9] fftools/ffmpeg: move do_video_stats() to avoid a forward declaration Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 6/9] fftools/ffmpeg: stop using AVStream.nb_frames in do_video_stats() Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 7/9] fftools/ffmpeg: stop using av_stream_get_end_pts() " Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 8/9] fftools/ffmpeg: merge variable declaration and initialization Anton Khirnov
2022-05-11  8:16 ` [FFmpeg-devel] [PATCH 9/9] fftools/ffmpeg: move processing AV_PKT_DATA_QUALITY_STATS to do_video_stats() Anton Khirnov
2022-05-11  8:45 ` [FFmpeg-devel] [PATCH 1/9] fftools/ffmpeg: share the code encoding a single frame between video and audio Anton Khirnov
2022-05-24 12:03 ` Anton Khirnov

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git