FFmpeg
Loading...
Searching...
No Matches
dts_gen.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Zhao Zhili <quinkblack@foxmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include "libavutil/common.h"
22#include "libavutil/log.h"
24
25#include "codec_desc.h"
26#include "dts_gen.h"
27
29 int delay)
30{
31 if (delay < FF_DTS_GEN_DELAY_SKIP || delay > FF_DTS_GEN_DELAY_MAX ||
32 delay == FF_DTS_GEN_DELAY_AUTO)
33 return AVERROR(EINVAL);
34
35 /* Without reordering dts equals pts, and receive_packet encoders get no
36 * generic fallback that sets it, so FF_DTS_GEN_DELAY_SKIP is not honoured.
37 */
39 delay = 0;
40
41 *g = (FFDTSGen) {
42 .delay = delay,
43 .prev_dts = AV_NOPTS_VALUE,
44 .first_in_pts = AV_NOPTS_VALUE,
45 .prev_in_pts = AV_NOPTS_VALUE,
46 };
47
48 return 0;
49}
50
52{
53 if (pts == AV_NOPTS_VALUE)
54 return;
55
56 if (g->first_in_pts == AV_NOPTS_VALUE)
57 g->first_in_pts = pts;
58
59 if (!g->step && g->prev_in_pts != AV_NOPTS_VALUE && pts > g->prev_in_pts)
60 g->step = av_sat_sub64(pts, g->prev_in_pts);
61
62 g->prev_in_pts = pts;
63}
64
66{
67 if (avctx->framerate.num > 0 && avctx->framerate.den > 0) {
68 int64_t step = av_rescale_q(1,
69 (AVRational) { avctx->framerate.den,
70 avctx->framerate.num },
71 avctx->time_base);
72 if (step > 0)
73 return step;
74 }
75
76 return 1;
77}
78
80{
81 int64_t dts;
82
83 if (g->delay == FF_DTS_GEN_DELAY_SKIP || pkt->pts == AV_NOPTS_VALUE) {
84 pkt->dts = AV_NOPTS_VALUE;
85 return;
86 }
87
88 if (!g->started) {
89 /* A decoder needs delay frames of reordering buffer, so the first
90 * delay packets decode before the first frame is presented and get
91 * timestamps extrapolated from the interval between input frames.
92 * The first packet need not carry the earliest pts, for instance
93 * when an encoder emits a reference picture before the pictures
94 * that precede it in presentation order, so the first input frame
95 * anchors the extrapolation as well.
96 */
97 int64_t step = g->step > 0 ? g->step : dts_gen_step(avctx);
98 int64_t first_pts = g->first_in_pts != AV_NOPTS_VALUE ?
99 FFMIN(g->first_in_pts, pkt->pts) : pkt->pts;
100
101 /* Any positive step keeps these timestamps increasing, so clamping
102 * an absurd interval only keeps the product in range.
103 */
104 step = FFMIN(step, INT64_MAX / (FF_DTS_GEN_DELAY_MAX + 1));
105
106 for (int i = 1; i <= g->delay; i++)
107 g->window[i] = av_sat_sub64(first_pts, (g->delay + 1 - i) * step);
108 g->started = 1;
109 }
110
111 g->window[0] = pkt->pts;
112 for (int i = 0; i < g->delay && g->window[i] > g->window[i + 1]; i++)
113 FFSWAP(int64_t, g->window[i], g->window[i + 1]);
114
115 dts = g->window[0];
116
117 /* Reached when delay is below the real reorder depth, or when two input
118 * frames share a pts, which no delay can fix.
119 */
120 if (g->prev_dts != AV_NOPTS_VALUE && dts <= g->prev_dts)
121 av_log_once(avctx, AV_LOG_ERROR, AV_LOG_VERBOSE, &g->warned,
122 "Generated dts %"PRId64" does not increase over %"PRId64"; "
123 "raise reorder_delay, or check that the input pts values "
124 "are strictly increasing.\n", dts, g->prev_dts);
125
126 g->prev_dts = dts;
127 pkt->dts = dts;
128}
129
131{
132 g->first_in_pts = AV_NOPTS_VALUE;
133 g->prev_in_pts = AV_NOPTS_VALUE;
134 g->step = 0;
135 g->prev_dts = AV_NOPTS_VALUE;
136 g->started = 0;
137 g->warned = 0;
138}
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
common internal and external API header
#define av_sat_sub64
Definition common.h:142
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
void ff_dts_gen_fill_pkt(AVCodecContext *avctx, FFDTSGen *g, AVPacket *pkt)
Definition dts_gen.c:79
static int64_t dts_gen_step(const AVCodecContext *avctx)
Definition dts_gen.c:65
av_cold void ff_dts_gen_reset(FFDTSGen *g)
Definition dts_gen.c:130
void ff_dts_gen_record_pts(FFDTSGen *g, int64_t pts)
Definition dts_gen.c:51
av_cold int ff_dts_gen_init(const AVCodecContext *avctx, FFDTSGen *g, int delay)
Definition dts_gen.c:28
#define FF_DTS_GEN_DELAY_SKIP
Definition dts_gen.h:34
#define FF_DTS_GEN_DELAY_MAX
Definition dts_gen.h:35
#define FF_DTS_GEN_DELAY_AUTO
Definition dts_gen.h:32
#define AV_CODEC_PROP_REORDER
Codec supports frame reordering.
Definition codec_desc.h:92
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define av_cold
Definition attributes.h:117
void av_log_once(void *avcl, int initial_level, int subsequent_level, int *state, const char *fmt,...)
Definition log.c:456
#define FFSWAP(type, a, b)
Definition macros.h:52
#define FFMIN(a, b)
Definition macros.h:49
main external API structure.
Definition avcodec.h:443
const struct AVCodecDescriptor * codec_descriptor
AVCodecDescriptor.
Definition avcodec.h:1719
AVRational framerate
Definition avcodec.h:563
AVRational time_base
This is the fundamental unit of time (in seconds) in terms of which frame timestamps are represented.
Definition avcodec.h:547
int props
Codec properties, a combination of AV_CODEC_PROP_* flags.
Definition codec_desc.h:54
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58
int num
Numerator.
Definition rational.h:59
int den
Denominator.
Definition rational.h:60
static int64_t pts
const char * g
Definition vf_curves.c:128