From patchwork Mon Apr 10 11:56:48 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eran Kornblau X-Patchwork-Id: 3355 Delivered-To: ffmpegpatchwork@gmail.com Received: by 10.103.44.195 with SMTP id s186csp1327743vss; Mon, 10 Apr 2017 04:57:00 -0700 (PDT) X-Received: by 10.223.135.196 with SMTP id c4mr26407310wrc.109.1491825420620; Mon, 10 Apr 2017 04:57:00 -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 r10si10403080wrc.146.2017.04.10.04.56.59; Mon, 10 Apr 2017 04:57:00 -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 EAE4168972D; Mon, 10 Apr 2017 14:56:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from EUR03-VE1-obe.outbound.protection.outlook.com (mail-eopbgr50095.outbound.protection.outlook.com [40.107.5.95]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id EA2D8688284 for ; Mon, 10 Apr 2017 14:56:45 +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=kFEc2rWR/JHDxeziKK63LFIuDO4MeSypeGjMyWENzYo=; b=BlIYcTV/xt5zH8XL2hWGe0+VZ35/pieaf8jKGuZBGhMiPVUs9jvmFZAQ0IA2ln9ZJEKM3QxTBca9bGtNp+TpUNLBy2kTlCiK2usJpw/rUXVhf1JLRtWd5YwUnoz8SFIKY7/2r54fbWRSLAF37yM+NZM0+gme7YDdItw0Mihygsw= 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.1019.17; Mon, 10 Apr 2017 11:56:49 +0000 Received: from AM4PR0401MB1731.eurprd04.prod.outlook.com ([fe80::2c0c:cc1f:5a63:3f1c]) by AM4PR0401MB1731.eurprd04.prod.outlook.com ([fe80::2c0c:cc1f:5a63:3f1c%14]) with mapi id 15.01.1019.025; Mon, 10 Apr 2017 11:56:49 +0000 From: Eran Kornblau To: FFmpeg development discussions and patches Thread-Topic: Match source video timestamp Thread-Index: AdKnuWHaR54rLj3xQOml42gAgJXgzQEoyacwAWU8kHA= Date: Mon, 10 Apr 2017 11:56:48 +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.142.103] x-microsoft-exchange-diagnostics: 1; AM4PR0401MB1731; 7:BhYdvoVVeboxH+XUsPhRH2rkFXOq0naP6h4nhheKco3rRnEDE14FlSTfojdDzZ9RAxyfW+qe34O5AdbAYaX/X14r7mhXEkwsxFBwAXuBtrvOFGULgbN2hs8aVL6fW3poC2gXqqbbZarhXCCpPjUcL6oqIcQ80qcmaknWd4eXegaACoKBUoHeFhQ6WjeFZUREVdS67DKNiqYAZjV8e2EMMhD431Iu+5G+b/iZmytW9EgS/RMzxyiTXYlQtzSyZYiLOMWFYQlTW8YudojuVyc5/y5yeY2dUlfiF1qP4oi08d2bmItVbNk/McrTACp58wTcU0P5Fo2PUjfa0pD0M42S7g== x-ms-office365-filtering-correlation-id: f9844138-b448-47a6-4122-08d48008ab86 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)(8121501046)(5005006)(3002001)(10201501046)(93006095)(93001095)(6055026)(6041248)(201703131423075)(201702281528075)(201703061421075)(20161123564025)(20161123560025)(20161123555025)(20161123562025)(6072148); SRVR:AM4PR0401MB1731; BCL:0; PCL:0; RULEID:; SRVR:AM4PR0401MB1731; x-forefront-prvs: 027367F73D x-forefront-antispam-report: SFV:NSPM; SFS:(10019020)(39840400002)(39450400003)(39410400002)(39850400002)(39400400002)(377454003)(53754006)(229853002)(6436002)(6246003)(790700001)(3846002)(6116002)(102836003)(3280700002)(6506006)(110136004)(54356999)(2900100001)(53936002)(99286003)(53546009)(6306002)(55016002)(25786009)(236005)(50986999)(189998001)(9686003)(54896002)(38730400002)(3660700001)(8936002)(7736002)(33656002)(74316002)(81166006)(8676002)(66066001)(7696004)(2906002)(575784001)(86362001)(3480700004)(6916009)(5250100002); 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: 10 Apr 2017 11:56:49.0159 (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" Pinging again... Thanks Eran From: Eran Kornblau Sent: Monday, April 3, 2017 12:29 PM To: 'FFmpeg development discussions and patches' Subject: RE: Match source video timestamp 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