Allows to read inputs at arbitrary rates. -re is equivalent to -readrate 1 Tested with -copyts {+ start_at_zero}, -ss, streamcopied & decoded streams.
Unified · Split
| @@ -1575,14 +1575,21 @@ Exit after ffmpeg has been running for @var{duration} seconds in CPU user time. · 1574 unmodified lines | |||
| 1575 | Dump each input packet to stderr. | 1575 | Dump each input packet to stderr. |
| 1576 | @item -hex (@emph{global}) | 1576 | @item -hex (@emph{global}) |
| 1577 | When dumping packets, also dump the payload. | 1577 | 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. | ||
| 1578 | @item -re (@emph{input}) | 1591 | @item -re (@emph{input}) |
| 1579 | Read input at native frame rate. Mainly used to simulate a grab device, | 1592 | Read input at native frame rate. This is equivalent to setting @code{-readrate 1}. |
| 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). | ||
| 1586 | @item -vsync @var{parameter} | 1593 | @item -vsync @var{parameter} |
| 1587 | Video sync method. | 1594 | Video sync method. |
| 1588 | For compatibility reasons old values can be specified as numbers. | 1595 | For compatibility reasons old values can be specified as numbers. |
| @@ -3765,7 +3765,7 @@ static int transcode_init(void) · 3764 unmodified lines | |||
| 3765 | /* init framerate emulation */ | 3765 | /* init framerate emulation */ |
| 3766 | for (i = 0; i < nb_input_files; i++) { | 3766 | for (i = 0; i < nb_input_files; i++) { |
| 3767 | InputFile *ifile = input_files[i]; | 3767 | InputFile *ifile = input_files[i]; |
| 3768 | if (ifile->rate_emu) | 3768 | if (ifile->readrate || ifile->rate_emu) |
| 3769 | for (j = 0; j < ifile->nb_streams; j++) | 3769 | for (j = 0; j < ifile->nb_streams; j++) |
| 3770 | input_streams[j + ifile->ist_index]->start = av_gettime_relative(); | 3770 | input_streams[j + ifile->ist_index]->start = av_gettime_relative(); |
| 3771 | } | 3771 | } |
| @@ -4225,12 +4225,19 @@ static int get_input_packet_mt(InputFile *f, AVPacket **pkt) · 453 unmodified lines | |||
| 4225 | 4225 | ||
| 4226 | static int get_input_packet(InputFile *f, AVPacket **pkt) | 4226 | static int get_input_packet(InputFile *f, AVPacket **pkt) |
| 4227 | { | 4227 | { |
| 4228 | if (f->rate_emu) { | 4228 | if (f->readrate || f->rate_emu) { |
| 4229 | int i; | 4229 | 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; | ||
| 4230 | for (i = 0; i < f->nb_streams; i++) { | 4235 | for (i = 0; i < f->nb_streams; i++) { |
| 4231 | InputStream *ist = input_streams[f->ist_index + i]; | 4236 | 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); | ||
| 4232 | int64_t pts = av_rescale(ist->dts, 1000000, AV_TIME_BASE); | 4239 | 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; |
| 4234 | if (pts > now) | 4241 | if (pts > now) |
| 4235 | return AVERROR(EAGAIN); | 4242 | return AVERROR(EAGAIN); |
| 4236 | } | 4243 | } |
| @@ -119,6 +119,7 @@ typedef struct OptionsContext { · 118 unmodified lines | |||
| 119 | int64_t input_ts_offset; | 119 | int64_t input_ts_offset; |
| 120 | int loop; | 120 | int loop; |
| 121 | int rate_emu; | 121 | int rate_emu; |
| 122 | float readrate; | ||
| 122 | int accurate_seek; | 123 | int accurate_seek; |
| 123 | int thread_queue_size; | 124 | int thread_queue_size; |
| 124 | 125 | ||
| @@ -418,6 +419,7 @@ typedef struct InputFile { · 293 unmodified lines | |||
| 418 | from ctx.nb_streams if new streams appear during av_read_frame() */ | 419 | from ctx.nb_streams if new streams appear during av_read_frame() */ |
| 419 | int nb_streams_warn; /* number of streams that the user was warned of */ | 420 | int nb_streams_warn; /* number of streams that the user was warned of */ |
| 420 | int rate_emu; | 421 | int rate_emu; |
| 422 | float readrate; | ||
| 421 | int accurate_seek; | 423 | int accurate_seek; |
| 422 | 424 | ||
| 423 | AVPacket *pkt; | 425 | AVPacket *pkt; |
| @@ -1286,6 +1286,17 @@ static int open_input_file(OptionsContext *o, const char *filename) · 1285 unmodified lines | |||
| 1286 | f->loop = o->loop; | 1286 | f->loop = o->loop; |
| 1287 | f->duration = 0; | 1287 | f->duration = 0; |
| 1288 | f->time_base = (AVRational){ 1, 1 }; | 1288 | 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 | |||
| 1289 | f->pkt = av_packet_alloc(); | 1300 | f->pkt = av_packet_alloc(); |
| 1290 | if (!f->pkt) | 1301 | if (!f->pkt) |
| 1291 | exit_program(1); | 1302 | exit_program(1); |
| @@ -3507,7 +3518,10 @@ const OptionDef options[] = { · 2215 unmodified lines | |||
| 3507 | "when dumping packets, also dump the payload" }, | 3518 | "when dumping packets, also dump the payload" }, |
| 3508 | { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET | | 3519 | { "re", OPT_BOOL | OPT_EXPERT | OPT_OFFSET | |
| 3509 | OPT_INPUT, { .off = OFFSET(rate_emu) }, | 3520 | 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" }, | ||
| 3511 | { "target", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_target }, | 3525 | { "target", HAS_ARG | OPT_PERFILE | OPT_OUTPUT, { .func_arg = opt_target }, |
| 3512 | "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" " | 3526 | "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\" or \"dv50\" " |
| 3513 | "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")", "type" }, | 3527 | "with optional prefixes \"pal-\", \"ntsc-\" or \"film-\")", "type" }, |