From: green-hand-shandong 
<[email protected]>

hi_mpi_venc echoes the frame index we submitted as the stream pts, in
whatever value was passed to send_frame. Track the submitted AVFrame
pts in a small FIFO and re-attach the original (CLI timebase) pts to
the output packet, so downstream muxers see correct timestamps.

Document the faster-than-realtime caveat: CLI fps-mode drops frames to
synchronize unless -fps_mode passthrough is set for offline file
transcodes.

End-to-end validation (Atlas 300I-Duo, CANN 9.1.0, dev7):
- 1080p->720p decode(hw) -> scale_ascend -> encode: 748/750 frames
  @ 48 fps, 4Mbps CBR output intact
- 2 concurrent transcode processes on one chip: both complete
- 3x loop long-run (2243 frames): stable, no DVPP memory growth
- full-feature smoke: 8/8 paths rc=0

Signed-off-by: green-hand-shandong 
<[email protected]>
---
 libavcodec/ascendenc.c | 30 ++++++++++++++++++++++++------
 1 file changed, 24 insertions(+), 6 deletions(-)

diff --git a/libavcodec/ascendenc.c b/libavcodec/ascendenc.c
index 430bf81eb3..1fc3e780f2 100644
--- a/libavcodec/ascendenc.c
+++ b/libavcodec/ascendenc.c
@@ -2,6 +2,11 @@
  * Ascend NPU (Atlas 300I-Duo / Ascend 310P) hardware video encoder
  * via the CANN DVPP media APIs (hi_mpi_venc).
  *
+ * NOTE for CLI users: the hardware encoder is much faster than real-time;
+ * when transcoding offline files the CLI's default fps-mode may drop
+ * frames to keep sync. Pass -fps_mode passthrough for file transcodes
+ * (the standard practice for faster-than-realtime encoders).
+ *
  * Copyright (c) 2026 green-hand-shandong
  *
  * This file is part of FFmpeg.
@@ -60,6 +65,8 @@ typedef struct AscendEncContext {
 
     uint64_t frame_count;
     uint64_t frame_count_out;
+    int64_t  pts_lut[64];      /* submitted frame->pts ring, consumed FIFO */
+    int      pts_lut_count;
 
     /* staging DVPP input buffers; VENC references them asynchronously,
      * so frames are staged in rotation. */
@@ -351,10 +358,19 @@ static int ascend_collect_stream(AVCodecContext *avctx, 
AVPacket *pkt,
             pkt->size += stream.pack[i].len;
         }
     }
-    /* VENC echoes back the pts we submitted (in the input's container
-     * timebase). Downstream muxers assume a monotonically increasing
-     * sequence in the packet timebase, so renumber by frame index. */
-    pkt->pts = pkt->dts = (int64_t)ctx->frame_count_out++;
+    /* VENC echoes the frame index we submitted; map it back to the
+     * original frame pts (in the CLI-provided encoder timebase). */
+    {
+        int64_t orig = ctx->pts_lut_count > 0 ? ctx->pts_lut[0] :
+                       (int64_t)ctx->frame_count_out;
+        if (ctx->pts_lut_count > 0) {
+            memmove(ctx->pts_lut, ctx->pts_lut + 1,
+                    sizeof(ctx->pts_lut[0]) * (ctx->pts_lut_count - 1));
+            ctx->pts_lut_count--;
+        }
+        pkt->pts = pkt->dts = orig;
+    }
+    ctx->frame_count_out++;
     {
         const hi_venc_h264_nalu_type h264t = 
stream.pack[0].data_type.h264_type;
         const hi_venc_h265_nalu_type h265t = 
stream.pack[0].data_type.h265_type;
@@ -446,8 +462,10 @@ static int ascend_encode_cb(AVCodecContext *avctx, 
AVPacket *pkt,
         /* time_ref must be monotonically even; VDEC/VENC pairing uses it
          * for reference marking. */
         fi.v_frame.time_ref         = (uint32_t)(ctx->frame_count * 2);
-        fi.v_frame.pts              = frame->pts != AV_NOPTS_VALUE ?
-                                      (uint64_t)frame->pts : ctx->frame_count;
+        fi.v_frame.pts              = ctx->frame_count;  /* VENC echoes this 
back */
+        if (ctx->pts_lut_count < 64)
+            ctx->pts_lut[ctx->pts_lut_count++] =
+                frame->pts != AV_NOPTS_VALUE ? frame->pts : 
(int64_t)ctx->frame_count;
 
         ret = hi_mpi_venc_send_frame(ctx->chn, &fi, ASCEND_ENC_TIMEOUT_MS);
         if (ret != 0) {
-- 
2.25.1


_______________________________________________
ffmpeg-devel mailing list -- [email protected]
To unsubscribe send an email to [email protected]

Reply via email to