-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathffmpeg.diff
240 lines (229 loc) · 10.5 KB
/
ffmpeg.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
diff --git a/ffmpeg.c b/ffmpeg.c
index a5ec3c3..49dfdb4 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -130,6 +130,7 @@ static int nb_frames_drop = 0;
static int64_t decode_error_stat[2];
static int current_time;
+static int64_t time_start;
AVIOContext *progress_avio = NULL;
static uint8_t *subtitle_out;
@@ -637,6 +638,7 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
AVBitStreamFilterContext *bsfc = ost->bitstream_filters;
AVCodecContext *avctx = ost->encoding_needed ? ost->enc_ctx : ost->st->codec;
int ret;
+ AVStream *st = s->streams[pkt->stream_index];
if (!ost->st->codec->extradata_size && ost->enc_ctx->extradata_size) {
ost->st->codec->extradata = av_mallocz(ost->enc_ctx->extradata_size + AV_INPUT_BUFFER_PADDING_SIZE);
@@ -711,24 +713,39 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, OutputStream *ost)
(avctx->codec_type == AVMEDIA_TYPE_AUDIO || avctx->codec_type == AVMEDIA_TYPE_VIDEO) &&
pkt->dts != AV_NOPTS_VALUE &&
ost->last_mux_dts != AV_NOPTS_VALUE) {
- int64_t max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
- if (pkt->dts < max) {
- int loglevel = max - pkt->dts > 2 || avctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
- av_log(s, loglevel, "Non-monotonous DTS in output stream "
- "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
- ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
- if (exit_on_error) {
- av_log(NULL, AV_LOG_FATAL, "aborting.\n");
- exit_program(1);
+ int64_t max;
+ if (regen_ts){
+ max = ((av_gettime_relative() - time_start) / 1000000000.0) * AV_TIME_BASE;
+ // av_gettime_relative() doesn't seem to do the job adequately. Sometimes (fairly frequent)
+ // above line results in the same timestamp back to back, possibly multiple frames arriving
+ // at ffmpeg in too short of a time. In any case, we can generally assume that the result of
+ // av_gettime_relative() will never go backwards, and that it won't report on a time until
+ // that time has actually come, which means that if subsequent frames end up with duplicate
+ // timestamps, we can just nudge them forward by close to the expected amount.
+ if (max <= ost->last_mux_dts) max = ost->last_mux_dts + (pkt->duration * 0.9);
+ if (st->cur_dts >= max) max=st->cur_dts+1;
+ pkt->dts = max;
+ pkt->pts = max;
+ } else {
+ max = ost->last_mux_dts + !(s->oformat->flags & AVFMT_TS_NONSTRICT);
+ if (pkt->dts < max) {
+ int loglevel = max - pkt->dts > 2 || avctx->codec_type == AVMEDIA_TYPE_VIDEO ? AV_LOG_WARNING : AV_LOG_DEBUG;
+ av_log(s, loglevel, "Non-monotonous DTS in output stream "
+ "%d:%d; previous: %"PRId64", current: %"PRId64"; ",
+ ost->file_index, ost->st->index, ost->last_mux_dts, pkt->dts);
+ if (exit_on_error) {
+ av_log(NULL, AV_LOG_FATAL, "aborting.\n");
+ exit_program(1);
+ }
+ av_log(s, loglevel, "changing to %"PRId64". This may result "
+ "in incorrect timestamps in the output file.\n",
+ max);
+ if(pkt->pts >= pkt->dts)
+ pkt->pts = FFMAX(pkt->pts, max);
+ pkt->dts = max;
+ }
+ }
}
- av_log(s, loglevel, "changing to %"PRId64". This may result "
- "in incorrect timestamps in the output file.\n",
- max);
- if(pkt->pts >= pkt->dts)
- pkt->pts = FFMAX(pkt->pts, max);
- pkt->dts = max;
- }
- }
}
ost->last_mux_dts = pkt->dts;
@@ -3954,16 +3971,22 @@ static int process_input(int file_index)
} else {
if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
delta > 1LL*dts_error_threshold*AV_TIME_BASE) {
- av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
- pkt.dts = AV_NOPTS_VALUE;
+ if (regen_ts) pkt_dts = dts_error_threshold*AV_TIME_BASE/2;
+ else {
+ av_log(NULL, AV_LOG_WARNING, "DTS %"PRId64", next:%"PRId64" st:%d invalid dropping\n", pkt.dts, ist->next_dts, pkt.stream_index);
+ pkt.dts = AV_NOPTS_VALUE;
+ }
}
if (pkt.pts != AV_NOPTS_VALUE){
int64_t pkt_pts = av_rescale_q(pkt.pts, ist->st->time_base, AV_TIME_BASE_Q);
delta = pkt_pts - ist->next_dts;
if ( delta < -1LL*dts_error_threshold*AV_TIME_BASE ||
delta > 1LL*dts_error_threshold*AV_TIME_BASE) {
- av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
- pkt.pts = AV_NOPTS_VALUE;
+ if (regen_ts) pkt_dts = dts_error_threshold*AV_TIME_BASE/2;
+ else {
+ av_log(NULL, AV_LOG_WARNING, "PTS %"PRId64", next:%"PRId64" invalid dropping st:%d\n", pkt.pts, ist->next_dts, pkt.stream_index);
+ pkt.pts = AV_NOPTS_VALUE;
+ }
}
}
}
@@ -4105,6 +4128,7 @@ static int transcode(void)
}
timer_start = av_gettime_relative();
+ time_start = timer_start;
#if HAVE_PTHREADS
if ((ret = init_input_threads()) < 0)
diff --git a/ffmpeg.h b/ffmpeg.h
index 20322b0..8273815 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -527,6 +527,8 @@ extern int do_deinterlace;
extern int do_hex_dump;
extern int do_pkt_dump;
extern int copy_ts;
+extern int regen_ts;
+extern int drop_n;
extern int start_at_zero;
extern int copy_tb;
extern int debug_ts;
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index bc8355e..91025c7 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -102,6 +102,8 @@ int do_benchmark_all = 0;
int do_hex_dump = 0;
int do_pkt_dump = 0;
int copy_ts = 0;
+int regen_ts = 0;
+int drop_n = 0;
int start_at_zero = 0;
int copy_tb = -1;
int debug_ts = 0;
@@ -3205,6 +3207,10 @@ const OptionDef options[] = {
"audio drift threshold", "threshold" },
{ "copyts", OPT_BOOL | OPT_EXPERT, { ©_ts },
"copy timestamps" },
+ { "regents", OPT_BOOL | OPT_EXPERT, { ®en_ts },
+ "force timestamp regeneration in output stream, suitable for stream capturing only" },
+ { "dropn", HAS_ARG | OPT_INT | OPT_EXPERT, { &drop_n },
+ "drop n out of n+1 frames, only suitable for raw or mjpeg" },
{ "start_at_zero", OPT_BOOL | OPT_EXPERT, { &start_at_zero },
"shift input timestamps to start at 0 when using copyts" },
{ "copytb", HAS_ARG | OPT_INT | OPT_EXPERT, { ©_tb },
diff --git a/libavdevice/v4l2-common.c b/libavdevice/v4l2-common.c
index 196c09b..8198993 100644
--- a/libavdevice/v4l2-common.c
+++ b/libavdevice/v4l2-common.c
@@ -18,6 +18,28 @@
#include "v4l2-common.h"
+#ifndef V4L2_PIX_FMT_Y16
+#define V4L2_PIX_FMT_Y16 v4l2_fourcc('Y', '1', '6', ' ') /* 16 Greyscale */
+#endif
+#ifndef V4L2_PIX_FMT_H264
+#define V4L2_PIX_FMT_H264 v4l2_fourcc('H', '2', '6', '4') /* H264 with start codes */
+#endif
+#ifndef V4L2_PIX_FMT_MPEG4
+#define V4L2_PIX_FMT_MPEG4 v4l2_fourcc('M', 'P', 'G', '4') /* MPEG-4 part 2 ES */
+#endif
+#ifndef V4L2_PIX_FMT_CPIA1
+#define V4L2_PIX_FMT_CPIA1 v4l2_fourcc('C', 'P', 'I', 'A') /* cpia1 YUV */
+#endif
+#ifndef V4L2_PIX_FMT_SGBRG8
+#define V4L2_PIX_FMT_SGBRG8 v4l2_fourcc('G', 'B', 'R', 'G') /* 8 GBGB.. RGRG.. */
+#endif
+#ifndef V4L2_PIX_FMT_SGRBG8
+#define V4L2_PIX_FMT_SGRBG8 v4l2_fourcc('G', 'R', 'B', 'G') /* 8 GRGR.. BGBG.. */
+#endif
+#ifndef V4L2_PIX_FMT_SRGGB8
+#define V4L2_PIX_FMT_SRGGB8 v4l2_fourcc('R', 'G', 'G', 'B') /* 8 RGRG.. GBGB.. */
+#endif
+
const struct fmt_map ff_fmt_conversion_table[] = {
//ff_fmt codec_id v4l2_fmt
{ AV_PIX_FMT_YUV420P, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_YUV420 },
@@ -37,27 +59,17 @@ const struct fmt_map ff_fmt_conversion_table[] = {
{ AV_PIX_FMT_BGR0, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_BGR32 },
{ AV_PIX_FMT_0RGB, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_RGB32 },
{ AV_PIX_FMT_GRAY8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_GREY },
-#ifdef V4L2_PIX_FMT_Y16
{ AV_PIX_FMT_GRAY16LE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_Y16 },
-#endif
{ AV_PIX_FMT_NV12, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12 },
{ AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_MJPEG },
{ AV_PIX_FMT_NONE, AV_CODEC_ID_MJPEG, V4L2_PIX_FMT_JPEG },
-#ifdef V4L2_PIX_FMT_H264
{ AV_PIX_FMT_NONE, AV_CODEC_ID_H264, V4L2_PIX_FMT_H264 },
-#endif
-#ifdef V4L2_PIX_FMT_MPEG4
{ AV_PIX_FMT_NONE, AV_CODEC_ID_MPEG4, V4L2_PIX_FMT_MPEG4 },
-#endif
-#ifdef V4L2_PIX_FMT_CPIA1
{ AV_PIX_FMT_NONE, AV_CODEC_ID_CPIA, V4L2_PIX_FMT_CPIA1 },
-#endif
-#ifdef V4L2_PIX_FMT_SRGGB8
{ AV_PIX_FMT_BAYER_BGGR8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_SBGGR8 },
{ AV_PIX_FMT_BAYER_GBRG8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_SGBRG8 },
{ AV_PIX_FMT_BAYER_GRBG8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_SGRBG8 },
{ AV_PIX_FMT_BAYER_RGGB8, AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_SRGGB8 },
-#endif
{ AV_PIX_FMT_NONE, AV_CODEC_ID_NONE, 0 },
};
diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 383033e..28e753c 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -239,7 +239,6 @@ static int first_field(const struct video_data *s)
return 1;
}
-#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
static void list_framesizes(AVFormatContext *ctx, uint32_t pixelformat)
{
const struct video_data *s = ctx->priv_data;
@@ -264,7 +263,6 @@ static void list_framesizes(AVFormatContext *ctx, uint32_t pixelformat)
vfse.index++;
}
}
-#endif
static void list_formats(AVFormatContext *ctx, int type)
{
@@ -297,9 +295,7 @@ static void list_formats(AVFormatContext *ctx, int type)
if (vfd.flags & V4L2_FMT_FLAG_EMULATED)
av_log(ctx, AV_LOG_INFO, " Emulated :");
#endif
-#if HAVE_STRUCT_V4L2_FRMIVALENUM_DISCRETE
list_framesizes(ctx, vfd.pixelformat);
-#endif
av_log(ctx, AV_LOG_INFO, "\n");
}
}