FFmpeg
Loading...
Searching...
No Matches
dts_gen.c
Go to the documentation of this file.
1/*
2 * This file is part of FFmpeg.
3 *
4 * FFmpeg is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * FFmpeg is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with FFmpeg; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 */
18
19#include <stdio.h>
20
21#include "libavutil/macros.h"
22
23#include "libavcodec/avcodec.h"
25#include "libavcodec/dts_gen.h"
26
27#define NP AV_NOPTS_VALUE
28
29/* Pts of the input frames, in display order. */
30static const int64_t cfr[] = { 0, 10, 20, 30, 40, 50, 60, 70, 80 };
31static const int64_t vfr[] = { 0, 5, 25, 45, 65, 85, 105, 125, 145 };
32
33/* Pts of the output packets, in decode order. */
34static const int64_t cfr_out[] = { 0, 40, 20, 10, 30, 80, 60, 50, 70 };
35static const int64_t vfr_out[] = { 0, 65, 25, 5, 45, 145, 105, 85, 125 };
36
37typedef struct Case {
38 const char *name;
40 int delay;
42 const int64_t *input; /* passed to ff_dts_gen_record_pts() */
44 const int64_t *pts; /* pts of the output packets */
45 int nb;
46 const int64_t *dts; /* expected dts of each output packet */
47 int warned; /* whether a dts that does not increase is reported */
48} Case;
49
50#define ARRAY(a) a, FF_ARRAY_ELEMS(a)
51#define SEQ(...) (const int64_t[]) { __VA_ARGS__ }, \
52 FF_ARRAY_ELEMS(((const int64_t[]) { __VA_ARGS__ }))
53#define DTS(...) (const int64_t[]) { __VA_ARGS__ }
54
55static const Case cases[] = {
56 { "no reordering", AV_CODEC_ID_H264, 0, { 0, 0 },
57 ARRAY(cfr), ARRAY(cfr), DTS(0, 10, 20, 30, 40, 50, 60, 70, 80) },
58 { "reordering", AV_CODEC_ID_H264, 2, { 0, 0 },
59 ARRAY(cfr), ARRAY(cfr_out), DTS(-20, -10, 0, 10, 20, 30, 40, 50, 60) },
60 { "variable frame rate", AV_CODEC_ID_H264, 2, { 0, 0 },
61 ARRAY(vfr), ARRAY(vfr_out), DTS(-10, -5, 0, 5, 25, 45, 65, 85, 105) },
62 { "delay above depth", AV_CODEC_ID_H264, 4, { 0, 0 },
63 ARRAY(cfr), ARRAY(cfr_out), DTS(-40, -30, -20, -10, 0, 10, 20, 30, 40) },
64 { "skip", AV_CODEC_ID_H264, FF_DTS_GEN_DELAY_SKIP, { 0, 0 },
65 ARRAY(cfr), ARRAY(cfr_out), DTS(NP, NP, NP, NP, NP, NP, NP, NP, NP) },
66 /* The window reproduces the repeated pts, so dts stalls. */
67 { "repeated pts", AV_CODEC_ID_H264, 1, { 0, 0 },
68 SEQ(0, 1, 1, 2), SEQ(0, 1, 1, 2), DTS(-1, 0, 1, 1), 1 },
69 /* The first dts values step by the recorded interval, else by the
70 * frame rate, else by one tick.
71 */
72 { "recorded interval", AV_CODEC_ID_H264, 2, { 50, 1 },
73 ARRAY(cfr), ARRAY(cfr_out), DTS(-20, -10, 0, 10, 20, 30, 40, 50, 60) },
74 { "frame rate", AV_CODEC_ID_H264, 2, { 50, 1 },
75 NULL, 0, ARRAY(cfr_out), DTS(-40, -20, 0, 10, 20, 30, 40, 50, 60) },
76 { "one tick", AV_CODEC_ID_H264, 2, { 0, 0 },
77 NULL, 0, ARRAY(cfr_out), DTS(-2, -1, 0, 10, 20, 30, 40, 50, 60) },
78 /* The first packet follows frames that precede it in presentation
79 * order, so the first input pts anchors the first dts values.
80 */
81 { "first packet not earliest", AV_CODEC_ID_H264, 2, { 0, 0 },
82 SEQ(0, 10, 20, 30, 40, 50), SEQ(20, 0, 10, 50, 30, 40),
83 DTS(-20, -10, 0, 10, 20, 30) },
84 /* AV1 does not reorder, so dts equals pts whatever the delay. */
85 { "codec without reordering", AV_CODEC_ID_AV1, FF_DTS_GEN_DELAY_SKIP,
86 { 0, 0 }, ARRAY(cfr), ARRAY(cfr), DTS(0, 10, 20, 30, 40, 50, 60, 70, 80) },
87};
88
90{
92
93 if (avctx) {
94 avctx->codec_id = id;
96 avctx->time_base = (AVRational) { 1, 1000 };
97 avctx->framerate = framerate;
98 }
99 return avctx;
100}
101
102static int run_case(const Case *c)
103{
104 AVCodecContext *avctx = alloc_context(c->id, c->framerate);
105 FFDTSGen g;
106 int err = 0;
107
108 if (!avctx || ff_dts_gen_init(avctx, &g, c->delay) < 0) {
109 avcodec_free_context(&avctx);
110 return 1;
111 }
112
113 for (int i = 0; i < c->nb_input; i++)
114 ff_dts_gen_record_pts(&g, c->input[i]);
115
116 for (int i = 0; i < c->nb; i++) {
117 AVPacket pkt = { .pts = c->pts[i] };
118
119 ff_dts_gen_fill_pkt(avctx, &g, &pkt);
120 if (pkt.dts != c->dts[i]) {
121 fprintf(stderr, "%s: packet %d: dts %"PRId64", expected %"PRId64"\n",
122 c->name, i, pkt.dts, c->dts[i]);
123 err = 1;
124 }
125 }
126 if (g.warned != c->warned) {
127 fprintf(stderr, "%s: warned %d, expected %d\n",
128 c->name, g.warned, c->warned);
129 err = 1;
130 }
131
132 avcodec_free_context(&avctx);
133 return err;
134}
135
136/* ff_dts_gen_reset() must drop the recorded interval and the window. */
137static int run_reset(void)
138{
140 AVPacket pkt = { .pts = 0 };
141 FFDTSGen g;
142 int err = 0;
143
144 if (!avctx || ff_dts_gen_init(avctx, &g, 1) < 0) {
145 avcodec_free_context(&avctx);
146 return 1;
147 }
148
151 ff_dts_gen_fill_pkt(avctx, &g, &pkt);
152
154 pkt.pts = 100;
155 ff_dts_gen_fill_pkt(avctx, &g, &pkt);
156 if (pkt.dts != 80) {
157 fprintf(stderr, "reset: dts %"PRId64", expected 80\n", pkt.dts);
158 err = 1;
159 }
160
161 avcodec_free_context(&avctx);
162 return err;
163}
164
165static int run_invalid_delays(void)
166{
167 static const int delays[] = { FF_DTS_GEN_DELAY_AUTO,
171 FFDTSGen g;
172 int err = 0;
173
174 if (!avctx)
175 return 1;
176
177 for (int i = 0; i < FF_ARRAY_ELEMS(delays); i++) {
178 if (ff_dts_gen_init(avctx, &g, delays[i]) != AVERROR(EINVAL)) {
179 fprintf(stderr, "init accepted delay %d\n", delays[i]);
180 err = 1;
181 }
182 }
183
184 avcodec_free_context(&avctx);
185 return err;
186}
187
188int main(void)
189{
190 int err = 0;
191
192 for (int i = 0; i < FF_ARRAY_ELEMS(cases); i++)
193 err |= run_case(&cases[i]);
194 err |= run_reset();
195 err |= run_invalid_delays();
196
197 return err;
198}
Libavcodec external API header.
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
enum AVCodecID id
Definition dts2pts.c:612
void ff_dts_gen_fill_pkt(AVCodecContext *avctx, FFDTSGen *g, AVPacket *pkt)
Definition dts_gen.c:79
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
const AVCodecDescriptor * avcodec_descriptor_get(enum AVCodecID id)
AVCodecContext * avcodec_alloc_context3(const AVCodec *codec)
Allocate an AVCodecContext and set its fields to default values.
Definition options.c:149
AVCodecID
Identify the syntax and semantics of the bitstream.
Definition codec_id.h:47
void avcodec_free_context(AVCodecContext **avctx)
Free the codec context and everything associated with it and write NULL to the provided pointer.
Definition options.c:168
@ AV_CODEC_ID_H264
Definition codec_id.h:77
@ AV_CODEC_ID_AV1
Definition codec_id.h:275
#define AVERROR(e)
Definition error.h:45
Utility Preprocessor macros.
#define FF_ARRAY_ELEMS(a)
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
enum AVCodecID codec_id
Definition avcodec.h:453
This structure stores compressed data.
Definition packet.h:587
Rational number (pair of numerator and denominator).
Definition rational.h:58
Definition dts_gen.c:37
AVRational framerate
Definition dts_gen.c:41
const int64_t * input
Definition dts_gen.c:42
const int64_t * pts
Definition dts_gen.c:44
const int64_t * dts
Definition dts_gen.c:46
int warned
Definition dts_gen.c:47
int delay
Definition dts_gen.c:40
int nb
Definition dts_gen.c:45
int nb_input
Definition dts_gen.c:43
enum AVCodecID id
Definition dts_gen.c:39
const char * name
Definition dts_gen.c:38
float framerate
Definition av1_levels.c:29
#define ARRAY(a)
Definition dts_gen.c:50
static int run_invalid_delays(void)
Definition dts_gen.c:165
#define SEQ(...)
Definition dts_gen.c:51
static const int64_t cfr[]
Definition dts_gen.c:30
static const int64_t vfr_out[]
Definition dts_gen.c:35
static const int64_t vfr[]
Definition dts_gen.c:31
static const int64_t cfr_out[]
Definition dts_gen.c:34
int main(void)
Definition dts_gen.c:188
#define DTS(...)
Definition dts_gen.c:53
static int run_reset(void)
Definition dts_gen.c:137
static const Case cases[]
Definition dts_gen.c:55
#define NP
Definition dts_gen.c:27
static AVCodecContext * alloc_context(enum AVCodecID id, AVRational framerate)
Definition dts_gen.c:89
static int run_case(const Case *c)
Definition dts_gen.c:102
const char * g
Definition vf_curves.c:128
static double c[64]