History

ffmpeg: add option readrate

Allows to read inputs at arbitrary rates. -re is equivalent to -readrate 1 Tested with -copyts {+ start_at_zero}, -ss, streamcopied & decoded streams.

Gyan Doshi committed 5 years ago

Commit c320b781 parent 075157e4 files · +41 −11Browse files

Unified · Split

4 files
  1. doc/ffmpeg.texi +14 −7
  2. fftools/ffmpeg.c +10 −3
  3. fftools/ffmpeg.h +2 −0
  4. fftools/ffmpeg_opt.c +15 −1
doc/ffmpeg.texi+14−7
@@ -1575,14 +1575,21 @@ Exit after ffmpeg has been running for @var{duration} seconds in CPU user time. · 1574 unmodified lines
15751575 Dump each input packet to stderr.
15761576 @item -hex (@emph{global})
15771577 When dumping packets, also dump the payload.
1578+@item -readrate @var{speed} (@emph{input})
1579+Limit input read speed.
1580+
1581+Its value is a floating-point positive number which represents the maximum duration of
1582+media, in seconds, that should be ingested in one second of wallclock time.
1583+Default value is zero and represents no imposed limitation on speed of ingestion.
1584+Value @code{1} represents real-time speed and is equivalent to @code{-re}.
1585+
1586+Mainly used to simulate a capture device or live input stream (e.g. when reading from a file).
1587+Should not be used with a low value when input is an actual capture device or live stream as
1588+it may cause packet loss.
1589+
1590+It is useful for when flow speed of output packets is important, such as live streaming.
15781591 @item -re (@emph{input})
1579−Read input at native frame rate. Mainly used to simulate a grab device,
1580−or live input stream (e.g. when reading from a file). Should not be used
1581−with actual grab devices or live input streams (where it can cause packet
1582−loss).
1583−By default @command{ffmpeg} attempts to read the input(s) as fast as possible.
1584−This option will slow down the reading of the input(s) to the native frame rate
1585−of the input(s). It is useful for real-time output (e.g. live streaming).
1592+Read input at native frame rate. This is equivalent to setting @code{-readrate 1}.
15861593 @item -vsync @var{parameter}
15871594 Video sync method.
15881595 For compatibility reasons old values can be specified as numbers.
fftools/ffmpeg.c+10−3
@@ -3765,7 +3765,7 @@ static int transcode_init(void) · 3764 unmodified lines
37653765 /* init framerate emulation */
37663766 for (i = 0; i < nb_input_files; i++) {
37673767 InputFile *ifile = input_files[i];
3768− if (ifile->rate_emu)
3768+ if (ifile->readrate || ifile->rate_emu)
37693769 for (j = 0; j < ifile->nb_streams; j++)
37703770 input_streams[j + ifile->ist_index]->start = av_gettime_relative();
37713771 }
@@ -4225,12 +4225,19 @@ static int get_input_packet_mt(InputFile *f, AVPacket **pkt) · 453 unmodified lines
42254225
42264226 static int get_input_packet(InputFile *f, AVPacket **pkt)
42274227 {
4228− if (f->rate_emu) {
4228+ if (f->readrate || f->rate_emu) {
42294229 int i;
4230+ int64_t file_start = copy_ts * (
4231+ (f->ctx->start_time != AV_NOPTS_VALUE ? f->ctx->start_time * !start_at_zero : 0) +
4232+ (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0)
4233+ );
4234+ float scale = f->rate_emu ? 1.0 : f->readrate;
42304235 for (i = 0; i < f->nb_streams; i++) {
42314236 InputStream *ist = input_streams[f->ist_index + i];
4237+ if (!ist->nb_packets) continue;
4238+ int64_t stream_ts_offset = FFMAX(ist->first_dts != AV_NOPTS_VALUE ? ist->first_dts : 0, file_start);
42324239 int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE);
4233− int64_t now = av_gettime_relative() - ist->start;
4240+ int64_t now = (av_gettime_relative() - ist->start)*scale + stream_ts_offset;
42344241 if (pts > now)
42354242 return AVERROR(EAGAIN);
42364243 }
fftools/ffmpeg.h+2−0
@@ -119,6 +119,7 @@ typedef struct OptionsContext { · 118 unmodified lines
119119 int64_t input_ts_offset;
120120 int loop;
121121 int rate_emu;
122+ float readrate;
122123 int accurate_seek;
123124 int thread_queue_size;
124125
@@ -418,6 +419,7 @@ typedef struct InputFile { · 293 unmodified lines
418419 from ctx.nb_streams if new streams appear during av_read_frame() */
419420 int nb_streams_warn; /* number of streams that the user was warned of */
420421 int rate_emu;
422+ float readrate;
421423 int accurate_seek;
422424
423425 AVPacket *pkt;
fftools/ffmpeg_opt.c+15−1
@@ -1286,6 +1286,17 @@ static int open_input_file(OptionsContext *o, const char *filename) · 1285 unmodified lines
12861286 f->loop = o->loop;
12871287 f->duration = 0;
12881288 f->time_base = (AVRational){ 1, 1 };
1289+
1290+ f->readrate = o->readrate ? o->readrate : 0.0;
1291+ if (f->readrate < 0.0f) {
1292+ av_log(NULL, AV_LOG_ERROR, "Option -readrate for Input #%d is %0.3f; it must be non-negative.\n", nb_input_files, f->readrate);
1293+ exit_program(1);
1294+ }
1295+ if (f->readrate && f->rate_emu) {
1296+ av_log(NULL, AV_LOG_WARNING, "Both -readrate and -re set for Input #%d. Using -readrate %0.3f.\n", nb_input_files, f->readrate);
1297+ f->rate_emu = 0;
1298+ }
1299+
12891300 f->pkt = av_packet_alloc();
12901301 if (!f->pkt)
12911302 exit_program(1);
@@ -3507,7 +3518,10 @@ const OptionDef options[] = { · 2215 unmodified lines
35073518 "when dumping packets, also dump the payload" },
35083519 { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET |
35093520 OPT_INPUT, { .off = OFFSET(rate_emu) },
3510− "read input at native frame rate", "" },
3521+ "read input at native frame rate; equivalent to -readrate 1", "" },
3522+ { "readrate", HAS_ARG | OPT_FLOAT | OPT_OFFSET |
3523+ OPT_EXPERT | OPT_INPUT, { .off = OFFSET(readrate) },
3524+ "read input at specified rate", "speed" },
35113525 { "target", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_target },
35123526 "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" "
35133527 "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")", "type" },

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t