From patchwork Mon Apr 3 09:29:10 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eran Kornblau X-Patchwork-Id: 3261 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp3093760vss; Mon, 3 Apr 2017 02:29:23 -0700 (PDT) X-Received: by 10.223.171.179 with SMTP id s48mr12828433wrc.167.1491211763469; Mon, 03 Apr 2017 02:29:23 -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 e63si9900389wmi.114.2017.04.03.02.29.21; Mon, 03 Apr 2017 02:29:23 -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 7351E688337; Mon, 3 Apr 2017 12:29:17 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR01-DB5-obe.outbound.protection.outlook.com (mail-db5eur01on0127.outbound.protection.outlook.com [104.47.2.127]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9B99468828B for ; Mon, 3 Apr 2017 12:29:10 +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=a4P31M+8EXpaSzTSVhq+PsQmEFTzU+JtnyJsSQHrg7U=; b=DW/RDuAOvMZ2WPMkMShWLojJT9OmIzF1lCSac1vCb8fz199UqzhdCCntWmiI4GgB1oxw+Bu5cKTtiw+7hZA1MzTCdMvSMX4ebvHdjucpOEQIA5tqo/FxWR03Zyd8Mt0FCeymHkViPgLDZBwK5tpMdXDHbVSep9MftX1bFBXuZSE= 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.1005.10; Mon, 3 Apr 2017 09:29:10 +0000 Received: from AM4PR0401MB1731.eurprd04.prod.outlook.com ([fe80::61bd:76c3:658e:841d]) by AM4PR0401MB1731.eurprd04.prod.outlook.com ([fe80::61bd:76c3:658e:841d%15]) with mapi id 15.01.1005.017; Mon, 3 Apr 2017 09:29:10 +0000 From: Eran Kornblau To: FFmpeg development discussions and patches Thread-Topic: Match source video timestamp Thread-Index: AdKnuWHaR54rLj3xQOml42gAgJXgzQEoyacw Date: Mon, 3 Apr 2017 09:29:10 +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: [79.176.202.124] x-microsoft-exchange-diagnostics: 1; AM4PR0401MB1731; 7:YjvJqllToep9u6V8jnMnOjzVeUCcZqUQ5g0R8n0t49URIyHR+bxqizXwmGmdviabCBjJhjZd6xwMSZ+Cfu3+WGDCPBA1/g3SSpVSSULEZK1ByBqn/pIYM6GZlHslei9F5rm0ZGn3xHGA+256yRy1R5CKgW3j1lka1w/JardRe7W3fTDClhf+aGgfkxUzuGTH3Ok1X+l7GA5TqAYPX0gPBHJj0qExbAXHiKQ6iuwc1WwTKTQTGYGE0cQgZ8ii6wYWr8BN97li5a/1dZrhfm8gmuRfZ5lWGr5grPWfBwCXTZtneXA5IUyYcGZJKjBDXeS4Av3vnBjSpfh0b3GHNEV0/Q== x-ms-office365-filtering-correlation-id: 643e3d78-1b03-49bd-5005-08d47a73e298 x-ms-office365-filtering-ht: Tenant x-microsoft-antispam: UriScan:; BCL:0; PCL:0; RULEID:(22001)(2017030254075)(48565401081)(201703131423075)(201703031133081); SRVR:AM4PR0401MB1731; x-microsoft-antispam-prvs: x-exchange-antispam-report-test: UriScan:(21748063052155); x-exchange-antispam-report-cfa-test: BCL:0; PCL:0; RULEID:(6040450)(601004)(2401047)(5005006)(8121501046)(93006095)(93001095)(10201501046)(3002001)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(20161123555025)(20161123564025)(20161123562025)(20161123560025)(6072148); SRVR:AM4PR0401MB1731; BCL:0; PCL:0; RULEID:; SRVR:AM4PR0401MB1731; x-forefront-prvs: 0266491E90 x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(6009001)(39450400003)(39400400002)(39410400002)(39840400002)(39850400002)(377454003)(53754006)(790700001)(7736002)(6116002)(102836003)(74316002)(3846002)(110136004)(33656002)(6916009)(38730400002)(5660300001)(189998001)(81166006)(8936002)(8676002)(2900100001)(66066001)(575784001)(86362001)(55016002)(3480700004)(3660700001)(53936002)(229853002)(3280700002)(53546009)(54356999)(7696004)(25786009)(50986999)(6436002)(5250100002)(2906002)(99286003)(6306002)(9686003)(54896002); 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: 03 Apr 2017 09:29:10.6293 (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: Re: [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" Ping From: Eran Kornblau Sent: Tuesday, March 28, 2017 3:13 PM To: FFmpeg development discussions and patches Subject: Match source video timestamp 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