From patchwork Tue Mar 28 12:12:56 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eran Kornblau X-Patchwork-Id: 3145 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp598114vss; Tue, 28 Mar 2017 05:13:07 -0700 (PDT) X-Received: by 10.223.139.87 with SMTP id v23mr503162wra.143.1490703187598; Tue, 28 Mar 2017 05:13:07 -0700 (PDT) Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 200si3237296wmg.4.2017.03.28.05.13.07; Tue, 28 Mar 2017 05:13:07 -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; dkim=neutral (body hash did not verify) header.i=@kaltura.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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=kaltura.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8145768991E; Tue, 28 Mar 2017 15:12:42 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-VE1-obe.outbound.protection.outlook.com (mail-ve1eur01on0138.outbound.protection.outlook.com [104.47.1.138]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 992DA6898D1 for ; Tue, 28 Mar 2017 15:12:35 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kaltura.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version; bh=usAZXFZZo5ufd54/5Ku7AzTeshzQToXjp2pgPnrxYAM=; b=nJHlHvrV7SDHKY9C+MqZdTBylZPDFAqFAMDBaugNAxpNf9X+D3JMG2pGA0kFrKCi10brxXxEviHbAyq09m7W6EkmlRY8OxjXqvXpCWL2UZO/g3RLoBT5GwOydRz1kp97EvxUoIQSEDCWJB2qa1XqXpzXPigKrOkUhIe0dE4avQI= Received: from AM4PR0401MB1731.eurprd04.prod.outlook.com (10.165.244.137) by AM4PR0401MB1731.eurprd04.prod.outlook.com (10.165.244.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256_P256) id 15.1.991.14; Tue, 28 Mar 2017 12:12:56 +0000 Received: from AM4PR0401MB1731.eurprd04.prod.outlook.com ([fe80::909c:b105:c087:c8f8]) by AM4PR0401MB1731.eurprd04.prod.outlook.com ([fe80::909c:b105:c087:c8f8%14]) with mapi id 15.01.0991.020; Tue, 28 Mar 2017 12:12:56 +0000 From: Eran Kornblau To: FFmpeg development discussions and patches Thread-Topic: Match source video timestamp Thread-Index: AdKnuWHaR54rLj3xQOml42gAgJXgzQ== Date: Tue, 28 Mar 2017 12:12:56 +0000 Message-ID: Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: authentication-results: ffmpeg.org; dkim=none (message not signed) header.d=none; ffmpeg.org; dmarc=none action=none header.from=kaltura.com; x-originating-ip: [62.0.105.133] x-microsoft-exchange-diagnostics: 1; AM4PR0401MB1731; 7:c0/J/e8Pv6PsKKa8OXTmXPuc7RKM/1M0CTBMhkp79xFRu/vWIHgWdLjDNbY9qXGRVndqBakLSCIo14XL/IbYWp8YZzyG9ppd0hpECwSV8cBK8j3hakn3sRBwOYaNqc5Gd6WKpGpAPtoucyaapMzv2FoCFpogh5kZAGNir0ewhI2O0scHtpwe9XRQBjTQpsHZWIltU5LtncDKI6RUW96bleq0vFA5qEnJ39ugZR++uiyG2nEsNiohuSbXRIjNGuiF/yy1V4w3K8bgvC23BiD5c7EaZEeFHPTFqNIFNkAPOwNSTWR4iwMTo+4SAECGb5OHFwrxyjbK6KG7j4bXi2+EdA== x-ms-office365-filtering-correlation-id: d15484ed-7371-434f-43eb-08d475d3c4ba x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254075)(48565401081); SRVR:AM4PR0401MB1731; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(21748063052155); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040375)(601004)(2401047)(5005006)(8121501046)(10201501046)(3002001)(6055026)(6041248)(20161123562025)(20161123564025)(20161123558025)(20161123555025)(20161123560025)(6072148); SRVR:AM4PR0401MB1731; BCL:0; PCL:0; RULEID:; SRVR:AM4PR0401MB1731; x-forefront-prvs: 0260457E99 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(6009001)(39840400002)(39450400003)(39410400002)(39850400002)(53754006)(25786009)(575784001)(3846002)(38730400002)(86362001)(74316002)(790700001)(6116002)(2906002)(55016002)(7736002)(50986999)(102836003)(33656002)(8936002)(8676002)(110136004)(5250100002)(81166006)(3480700004)(54356999)(189998001)(66066001)(6506006)(6916009)(5660300001)(3280700002)(7696004)(3660700001)(6436002)(99286003)(53936002)(9686003)(6306002)(54896002)(2900100001); DIR:OUT; SFP:1102; SCL:1; SRVR:AM4PR0401MB1731; H:AM4PR0401MB1731.eurprd04.prod.outlook.com; FPR:; SPF:None; MLV:sfv; LANG:en; spamdiagnosticoutput: 1:99 spamdiagnosticmetadata: NSPM MIME-Version: 1.0 X-OriginatorOrg: kaltura.com X-MS-Exchange-CrossTenant-originalarrivaltime: 28 Mar 2017 12:12:56.2691 (UTC) X-MS-Exchange-CrossTenant-fromentityheader: Hosted X-MS-Exchange-CrossTenant-id: 0c503748-de3f-4e25-97e2-6819d53a42b6 X-MS-Exchange-Transport-CrossTenantHeadersStamped: AM4PR0401MB1731 X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] Match source video timestamp X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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" Hi all, I'm trying to transcode some video file (MP4/h264 baseline vfr/AAC) with ffmpeg, and I would like to have the frame timestamps in the transcoded file perfectly match the source video. This is in order to have them both play together adaptively without issues (from my experience, DASH is very sensitive to timestamp continuity, a difference as small as a couple of frames between renditions causes playback to fail) After trying all sorts of parameters (-vsync 0/2, -copytb, with/out -r, -video_track_timescale) without success, I found that the reason for the mismatched timestamps is that ffmpeg uses 1/frame_rate as the time base of the encoder. With cfr this is probably ok, but with vfr this quantizes the timestamps and they come out different than the source. Just for testing, I made this change: root@pa-front-vod-stg2 FFmpeg# git diff And I am able to get a perfect match (=identical stts atom) between the source & transcoded videos. This is the command line I'm using (the -r is probably meaningless...): ffmpeg -threads 1 -i input.mp4 -c:v libx264 -subq 2 -qcomp 0.6 -qmin 10 -qmax 50 -qdiff 4 -coder 0 -x264opts stitchable -vprofile baseline -force_key_frames source -pix_fmt yuv420p -b:v 400k -s 640x480 -r 25.174 -g 86400 -aspect 640:480 -c:a copy -map_chapters -1 -map_metadata -1 -f mp4 -flags +loop+mv4 -cmp 256 -partitions +parti4x4+partp8x8+partb8x8 -trellis 1 -refs 1 -me_range 16 -keyint_min 20 -sc_threshold 0 -i_qfactor 0.71 -bt 100k -maxrate 400k -bufsize 1200k -rc_eq 'blurCplx^(1-qComp)' -level 30 -vsync 2 -threads 4 -y output.mp4 I'm thinking about adding variables for this -video_timescale / -audio_timescale (following the convention of movenc's video_track_timescale) that will get these values: 1. 0 (default) - the existing behavior - 1/frame rate for video, 1/sampling rate for audio 2. -1 - match the input stream (as in the patch above) 3. >0 - fixed value (e.g. when passing 1000 it will use 1/1000) Does this make sense? Any other ideas for solving this? Thank you! Eran diff --git a/ffmpeg.c b/ffmpeg.c index 3b91710..9cba0d5 100644 --- a/ffmpeg.c +++ b/ffmpeg.c @@ -3351,7 +3351,7 @@ static int transcode_init(void) enc_ctx->time_base = (AVRational){ 1, enc_ctx->sample_rate }; break; case AVMEDIA_TYPE_VIDEO: - enc_ctx->time_base = av_inv_q(ost->frame_rate); + enc_ctx->time_base = ist->st->time_base; //av_inv_q(ost->frame_rate); if (!(enc_ctx->time_base.num && enc_ctx->time_base.den)) enc_ctx->time_base = ost->filter->filter->inputs[0]->time_base; if ( av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != VSYNC_PASSTHROUGH