From patchwork Fri Oct 21 16:31:49 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 38868 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp860122pzb; Fri, 21 Oct 2022 09:32:37 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5iv49Z1vX0Vv5u/2grXIHHIboAAfkEPKZdydNI6/zvR09g8Mn7KuH30RbBz0MdCY/8WH+p X-Received: by 2002:a05:6402:380a:b0:451:ae08:7a6c with SMTP id es10-20020a056402380a00b00451ae087a6cmr17726972edb.161.1666369957338; Fri, 21 Oct 2022 09:32:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666369957; cv=none; d=google.com; s=arc-20160816; b=r6Uq4LADJOFGqUXYV6uZblvOjr4wPhx9CBUOvdyu3IhbYbGxgswTw/c5IEx42+lRI8 KpVP2xLpw5wauRxpK5Pzfn3g63veOyQOXVh4CDHrBTVBjqF6s0nD+L/y2uO5NxIPMnxV fOkrSsgr4eAnvYuZI24FtGQM8rm5XGOz4AO/uytji24vKiPv4FujOgUPKwq4HLApSayX xjiNi35heSm4nwL75B0zsM2HwqbMOqi3rmV4YIz6ufrIG54zbPZJCYomwU0XDAehH+zz QYAGbSMh5/DQUIlPA4rcq7tNp/R/Z5ebwz66wigysSSXhyrDytigrePyR5ExPoJPqX/s 9IFg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :delivered-to; bh=lKfQk8qi9tthEdtMRcS2utEGu/fTEeLtMNfujTJVSAI=; b=rAptV94VPbInP9x1d2R+dJVt6nMzhG0Jb4YuVsM53G8tRh4+ky2Y+G+wolaJRQ3iqB CqnuwdlJo5RHVVTpzGqbVlEP+03Rr7/IY1FkWy+cILaB4/JYfZRqlsH6GZ2KxI78X8Yo sudzDPV2yvGWcJqu5EBZBy0SXvKFxaWYcbStf20+IGXg/N2cH1BHN5yIZcCDE4S59fKb 5aaoZUB8I5HjuHheJwSCaOkHdMO0IDYKoJpzYOjsTp1i6s4m1FbuZsUdmjDsK1JaK+N3 yB9REcrYvEVif5Zcj5TpauJMihstM5/hSf5+ChC07r+BFBcYqzJNRsbc3pfJK+chM/TE gYYg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id qw22-20020a1709066a1600b0078d3a35e181si19828332ejc.871.2022.10.21.09.32.36; Fri, 21 Oct 2022 09:32:37 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 3883668BEA2; Fri, 21 Oct 2022 19:32:32 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-101.mailbox.org (mout-p-101.mailbox.org [80.241.56.151]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 96A2668BDF8 for ; Fri, 21 Oct 2022 19:32:24 +0300 (EEST) Received: from smtp202.mailbox.org (smtp202.mailbox.org [10.196.197.202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-101.mailbox.org (Postfix) with ESMTPS id 4Mv93p39hrz9sS3 for ; Fri, 21 Oct 2022 18:32:22 +0200 (CEST) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Fri, 21 Oct 2022 22:01:49 +0530 Message-Id: <20221021163150.2196-1-ffmpeg@gyani.pro> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 1/2] ffmpeg: shift start time correction to ffmpeg_opt X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: KJtCkP3GVBaQ In preparation for applying start time correction that accounts for all factors such as copyts, input_ts_offset ..etc --- fftools/ffmpeg.c | 22 ---------------------- fftools/ffmpeg_opt.c | 27 +++++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index e57486fd4a..0fe582be3b 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -3194,28 +3194,6 @@ static int transcode_init(void) input_streams[j + ifile->ist_index]->start = av_gettime_relative(); } - // Correct starttime based on the enabled streams - for (i = 0; i < nb_input_files; i++) { - InputFile *ifile = input_files[i]; - AVFormatContext *is = ifile->ctx; - int64_t new_start_time = INT64_MAX; - - if (is->start_time == AV_NOPTS_VALUE || - !(is->iformat->flags & AVFMT_TS_DISCONT)) - continue; - - for (int j = 0; j < is->nb_streams; j++) { - AVStream *st = is->streams[j]; - if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE) - continue; - new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q)); - } - if (new_start_time > is->start_time) { - av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time); - ifile->ts_offset = -new_start_time; - } - } - /* init input streams */ for (i = 0; i < nb_input_streams; i++) if ((ret = init_input_stream(i, error, sizeof(error))) < 0) diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 9245e02813..28b7d4dc27 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -216,6 +216,31 @@ int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_id return 0; } +static void correct_input_start_times(void) +{ + // Correct starttime based on the enabled streams + for (int i = 0; i < nb_input_files; i++) { + InputFile *ifile = input_files[i]; + AVFormatContext *is = ifile->ctx; + int64_t new_start_time = INT64_MAX; + + if (is->start_time == AV_NOPTS_VALUE || + !(is->iformat->flags & AVFMT_TS_DISCONT)) + continue; + + for (int j = 0; j < is->nb_streams; j++) { + AVStream *st = is->streams[j]; + if(st->discard == AVDISCARD_ALL || st->start_time == AV_NOPTS_VALUE) + continue; + new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q)); + } + if (new_start_time > is->start_time) { + av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time); + ifile->ts_offset = -new_start_time; + } + } +} + static int apply_sync_offsets(void) { for (int i = 0; i < nb_input_files; i++) { @@ -1909,6 +1934,8 @@ int ffmpeg_parse_options(int argc, char **argv) goto fail; } + correct_input_start_times(); + check_filter_outputs(); fail: From patchwork Fri Oct 21 16:31:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Gyan Doshi X-Patchwork-Id: 38869 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:4a86:b0:9d:28a3:170e with SMTP id fn6csp860237pzb; Fri, 21 Oct 2022 09:32:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5iJUAzQ/x5BBhejg62iPqy7tqwTDmaTKAGaM3Nnm9u7z3Tt9peFO8hQFqpVELyjG++n7HU X-Received: by 2002:a17:907:75e5:b0:78d:cb12:6dcc with SMTP id jz5-20020a17090775e500b0078dcb126dccmr15906885ejc.550.1666369967834; Fri, 21 Oct 2022 09:32:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666369967; cv=none; d=google.com; s=arc-20160816; b=Wl8602kLJuC6rLF/9kdhO4AnS2sWZTbVoV0emd74zxOK4UU6+kWRlNjBVDXAexV4ni UbywVipGsmxxixDYDt3fsMRexuVc0y4MZOoic+2pI9M6APefq5sfo8dwU6Cgau5KFWhQ ODzA4TDgrU0foBZkth3uuS+Ca2QSQpELYVjJi0qh2yjYzHjrolAhgsDZ9tzr4qeheN/d U5k3WF51qQTABEbqSyDGLwtHnxuIzryWI3BqtczHvES2CMmefhLL46ipeXfZaaf7/0mC OskKB4SOJ1UrWEmWdh0ZN4PzI6mz/vuU6yaPbfQrrSVqJJ2qw66qzk11GIpQlf9a0wCZ ms0A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:delivered-to; bh=oI75jLweqZuDdwGJ0g09euo6V+xnbLaRMNzO+U5rwF8=; b=I6jY+EsJt72/saYwi65WPutPpllZ6UKtifpcIrSJdKBmoZ4CCTA9o93WNerxLelCbB 4ZLXxtKMw8PYb5M7Ask4GkDVjr80tkQ4Mn7eKAwc5qP0ZV2jImlY4chPA0Yf+1iPTrci /rKYCHCvGbxWYJOwQ9cDc0f9zhPQTlBIKVAddPm/9mi7juLztt/h4H8aal7SU9Xeyq4b aUCOp09OgKXICmKtj8YLe8rY4A6SRVkpqGyzZq0Q7tPwseQprXnJvOaP58MyJihy7ILP ZF26w3wp9yiyDtySblxAImc1pIUzLIHao9bhyy7ICiqHyjnCW2Wz0HxTAKxHwJDSQYAL G50w== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id z88-20020a509e61000000b00459dc4e4b7fsi18426862ede.143.2022.10.21.09.32.46; Fri, 21 Oct 2022 09:32:47 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 4C89A68BEA9; Fri, 21 Oct 2022 19:32:33 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mout-p-102.mailbox.org (mout-p-102.mailbox.org [80.241.56.152]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 5F9D768BDCB for ; Fri, 21 Oct 2022 19:32:26 +0300 (EEST) Received: from smtp202.mailbox.org (smtp202.mailbox.org [IPv6:2001:67c:2050:b231:465::202]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-384) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mout-p-102.mailbox.org (Postfix) with ESMTPS id 4Mv93r1pGzz9sWk for ; Fri, 21 Oct 2022 18:32:24 +0200 (CEST) From: Gyan Doshi To: ffmpeg-devel@ffmpeg.org Date: Fri, 21 Oct 2022 22:01:50 +0530 Message-Id: <20221021163150.2196-2-ffmpeg@gyani.pro> In-Reply-To: <20221021163150.2196-1-ffmpeg@gyani.pro> References: <20221021163150.2196-1-ffmpeg@gyani.pro> MIME-Version: 1.0 X-Rspamd-Queue-Id: 4Mv93r1pGzz9sWk Subject: [FFmpeg-devel] [PATCH v2 2/2] ffmpeg: fix implementation of updated input start time X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: 2nB9ONRK2qRE The current adjustment of input start times just adjusts the tsoffset. And it does so, by resetting the tsoffset to nullify the new start time. This leads to breakage of -copyts, ignoring of input_ts_offset, breaking of -isync as well as breaking wrap correction. Fixed by taking cognizance of these parameters, and by correcting start times just before sync offsets are applied. --- fftools/ffmpeg.c | 2 +- fftools/ffmpeg.h | 5 ++++- fftools/ffmpeg_demux.c | 4 ++-- fftools/ffmpeg_opt.c | 33 +++++++++++++++++++++++---------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c index 0fe582be3b..c564b2649e 100644 --- a/fftools/ffmpeg.c +++ b/fftools/ffmpeg.c @@ -1815,7 +1815,7 @@ static void do_streamcopy(InputStream *ist, OutputStream *ost, const AVPacket *p start_time = 0; if (copy_ts) { start_time += f->start_time != AV_NOPTS_VALUE ? f->start_time : 0; - start_time += start_at_zero ? 0 : f->ctx->start_time; + start_time += start_at_zero ? 0 : f->start_time_effective; } if (ist->pts >= f->recording_time + start_time) { close_output_stream(ost); diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h index 5966cac60e..2d97ccaa15 100644 --- a/fftools/ffmpeg.h +++ b/fftools/ffmpeg.h @@ -447,7 +447,10 @@ typedef struct InputFile { AVRational time_base; /* time base of the duration */ int64_t input_ts_offset; int input_sync_ref; - + /** + * Effective format start time based on enabled streams. + */ + int64_t start_time_effective; int64_t ts_offset; /** * Extra timestamp offset added by discontinuity handling. diff --git a/fftools/ffmpeg_demux.c b/fftools/ffmpeg_demux.c index 6e89f5999a..220fda56da 100644 --- a/fftools/ffmpeg_demux.c +++ b/fftools/ffmpeg_demux.c @@ -120,7 +120,7 @@ static int seek_to_start(InputFile *ifile) static void ts_fixup(InputFile *ifile, AVPacket *pkt, int *repeat_pict) { InputStream *ist = input_streams[ifile->ist_index + pkt->stream_index]; - const int64_t start_time = ifile->ctx->start_time; + const int64_t start_time = ifile->start_time_effective; int64_t duration; if (debug_ts) { @@ -367,7 +367,7 @@ int ifile_get_packet(InputFile *f, AVPacket **pkt) if (f->readrate || f->rate_emu) { int i; int64_t file_start = copy_ts * ( - (f->ctx->start_time != AV_NOPTS_VALUE ? f->ctx->start_time * !start_at_zero : 0) + + (f->start_time_effective != AV_NOPTS_VALUE ? f->start_time_effective * !start_at_zero : 0) + (f->start_time != AV_NOPTS_VALUE ? f->start_time : 0) ); float scale = f->rate_emu ? 1.0 : f->readrate; diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c index 28b7d4dc27..a6fa9b7801 100644 --- a/fftools/ffmpeg_opt.c +++ b/fftools/ffmpeg_opt.c @@ -216,13 +216,15 @@ int parse_and_set_vsync(const char *arg, int *vsync_var, int file_idx, int st_id return 0; } +/* Correct input file start times based on enabled streams */ static void correct_input_start_times(void) { - // Correct starttime based on the enabled streams for (int i = 0; i < nb_input_files; i++) { InputFile *ifile = input_files[i]; AVFormatContext *is = ifile->ctx; - int64_t new_start_time = INT64_MAX; + int64_t new_start_time = INT64_MAX, diff, abs_start_seek; + + ifile->start_time_effective = is->start_time; if (is->start_time == AV_NOPTS_VALUE || !(is->iformat->flags & AVFMT_TS_DISCONT)) @@ -234,9 +236,20 @@ static void correct_input_start_times(void) continue; new_start_time = FFMIN(new_start_time, av_rescale_q(st->start_time, st->time_base, AV_TIME_BASE_Q)); } - if (new_start_time > is->start_time) { - av_log(is, AV_LOG_VERBOSE, "Correcting start time by %"PRId64"\n", new_start_time - is->start_time); - ifile->ts_offset = -new_start_time; + + diff = new_start_time - is->start_time; + if (diff) { + av_log(NULL, AV_LOG_VERBOSE, "Correcting start time of Input #%d by %"PRId64" us.\n", i, diff); + ifile->start_time_effective = new_start_time; + if (copy_ts && start_at_zero) + ifile->ts_offset = -new_start_time; + else if (!copy_ts) { + abs_start_seek = is->start_time + (ifile->start_time != AV_NOPTS_VALUE) ? ifile->start_time : 0; + ifile->ts_offset = abs_start_seek > new_start_time ? -abs_start_seek : -new_start_time; + } else if (copy_ts) + ifile->ts_offset = 0; + + ifile->ts_offset += ifile->input_ts_offset; } } } @@ -269,9 +282,9 @@ static int apply_sync_offsets(void) if (self->ctx->start_time_realtime != AV_NOPTS_VALUE && ref->ctx->start_time_realtime != AV_NOPTS_VALUE) { self_start_time = self->ctx->start_time_realtime; ref_start_time = ref->ctx->start_time_realtime; - } else if (self->ctx->start_time != AV_NOPTS_VALUE && ref->ctx->start_time != AV_NOPTS_VALUE) { - self_start_time = self->ctx->start_time; - ref_start_time = ref->ctx->start_time; + } else if (self->start_time_effective != AV_NOPTS_VALUE && ref->start_time_effective != AV_NOPTS_VALUE) { + self_start_time = self->start_time_effective; + ref_start_time = ref->start_time_effective; } else { start_times_set = 0; } @@ -1918,8 +1931,6 @@ int ffmpeg_parse_options(int argc, char **argv) goto fail; } - apply_sync_offsets(); - /* create the complex filtergraphs */ ret = init_complex_filters(); if (ret < 0) { @@ -1936,6 +1947,8 @@ int ffmpeg_parse_options(int argc, char **argv) correct_input_start_times(); + apply_sync_offsets(); + check_filter_outputs(); fail: