diff mbox series

[FFmpeg-devel,18/30] lavc/encode: pass through frame durations to encoded packets

Message ID 20221127170351.11477-18-anton@khirnov.net
State New
Headers show
Series [FFmpeg-devel,01/30] lavc/libx264: factor out setting up the input frame | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Anton Khirnov Nov. 27, 2022, 5:03 p.m. UTC
The generic code can only handle the no-delay case. Encoders with delay
need to be handled individually, which will be done in the following
commits.
---
 doc/APIchanges             |  2 +-
 libavcodec/avcodec.h       |  7 +++++++
 libavcodec/encode.c        | 14 ++++++++++++--
 libavcodec/options_table.h |  1 +
 4 files changed, 21 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/doc/APIchanges b/doc/APIchanges
index a3c9819e32..4ae5571f71 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,7 +15,7 @@  libavutil:     2021-04-27
 API changes, most recent first:
 
 2022-12-xx - xxxxxxxxxx - lavc 59.55.100 - avcodec.h
-  Add AV_CODEC_FLAG_COPY_OPAQUE.
+  Add AV_CODEC_FLAG_COPY_OPAQUE and AV_CODEC_FLAG_FRAME_DURATION.
 
 2022-11-xx - xxxxxxxxxx - lavu 57.43.100 - tx.h
   Add AV_TX_FLOAT_DCT, AV_TX_DOUBLE_DCT and AV_TX_INT32_DCT.
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 79558786ee..a31d413179 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -249,6 +249,13 @@  typedef struct RcOverride{
  * @ref AV_CODEC_CAP_ENCODER_REORDERED_OPAQUE capability flag.
  */
 #define AV_CODEC_FLAG_COPY_OPAQUE     (1 <<  7)
+/**
+ * Signal to the encoder that the values of AVFrame.duration are valid and
+ * should be used (typically for transferring them to output packets).
+ *
+ * If this flag is not set, frame durations are ignored.
+ */
+#define AV_CODEC_FLAG_FRAME_DURATION  (1 <<  8)
 /**
  * Use internal 2pass ratecontrol in first pass mode.
  */
diff --git a/libavcodec/encode.c b/libavcodec/encode.c
index e4270b6c34..00b7a7dde8 100644
--- a/libavcodec/encode.c
+++ b/libavcodec/encode.c
@@ -215,10 +215,13 @@  int ff_encode_encode_cb(AVCodecContext *avctx, AVPacket *avpkt,
             if (avpkt->pts == AV_NOPTS_VALUE)
                 avpkt->pts = frame->pts;
 
-            if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
-                if (!avpkt->duration)
+            if (!avpkt->duration) {
+                if (frame->duration)
+                    avpkt->duration = frame->duration;
+                else if (avctx->codec->type == AVMEDIA_TYPE_AUDIO) {
                     avpkt->duration = ff_samples_to_time_base(avctx,
                                                               frame->nb_samples);
+                }
             }
 
             avctx->reordered_opaque = frame->reordered_opaque;
@@ -454,6 +457,13 @@  FF_ENABLE_DEPRECATION_WARNINGS
             return ret;
     }
 
+    // unset frame duration unless AV_CODEC_FLAG_FRAME_DURATION is set,
+    // since otherwise we cannot be sure that whatever value it has is in the
+    // right timebase, so we would produce an incorrect value, which is worse
+    // than none at all
+    if (!(avctx->flags & AV_CODEC_FLAG_FRAME_DURATION))
+        dst->duration = 0;
+
     return 0;
 }
 
diff --git a/libavcodec/options_table.h b/libavcodec/options_table.h
index ad41dc7bed..011314db96 100644
--- a/libavcodec/options_table.h
+++ b/libavcodec/options_table.h
@@ -59,6 +59,7 @@  static const AVOption avcodec_options[] = {
 {"qscale", "use fixed qscale", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_QSCALE }, INT_MIN, INT_MAX, 0, "flags"},
 {"recon_frame", "export reconstructed frames", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_RECON_FRAME}, .unit = "flags"},
 {"copy_opaque", "propagate opaque values", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_COPY_OPAQUE}, .unit = "flags"},
+{"frame_duration", "use frame durations", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_FRAME_DURATION}, .unit = "flags"},
 {"pass1", "use internal 2-pass ratecontrol in first  pass mode", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_PASS1 }, INT_MIN, INT_MAX, 0, "flags"},
 {"pass2", "use internal 2-pass ratecontrol in second pass mode", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_PASS2 }, INT_MIN, INT_MAX, 0, "flags"},
 {"gray", "only decode/encode grayscale", 0, AV_OPT_TYPE_CONST, {.i64 = AV_CODEC_FLAG_GRAY }, INT_MIN, INT_MAX, V|E|D, "flags"},