From patchwork Mon Jan 18 22:44:48 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 25024 Return-Path: X-Original-To: patchwork@ffaux-bg.ffmpeg.org Delivered-To: patchwork@ffaux-bg.ffmpeg.org Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by ffaux.localdomain (Postfix) with ESMTP id 9622144B378 for ; Tue, 19 Jan 2021 00:54:56 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 7C0806882F0; Tue, 19 Jan 2021 00:54:56 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-lf1-f48.google.com (mail-lf1-f48.google.com [209.85.167.48]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id C76326805A5 for ; Tue, 19 Jan 2021 00:54:49 +0200 (EET) Received: by mail-lf1-f48.google.com with SMTP id o17so26384748lfg.4 for ; Mon, 18 Jan 2021 14:54:49 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=jkqxz-net.20150623.gappssmtp.com; s=20150623; h=from:to:subject:date:message-id:in-reply-to:references:mime-version :content-transfer-encoding; bh=aR9uOOoh3oK/aQLNaF4a7C2DYm1gIiIvrFWx3+McuuQ=; b=xHQCd8wyZAJbXP4i2ii6LEl5auTti8Ip6uLhOGw4OmBwhsUSxZEHJoNcgyyPk9qraB v1gRdn3YqO6sXCYFEjDnKsyIVCZ5tbrXol9auklyMY7yVIyWm0svknrofOcaEYGairxh uXfViXMZCi5vf+2dBywLHR5hq7tpfaE696vQAYP1VM3iFRdNtXhmjkIuN1CY53fLyqMt bWsy9/HyQsCrnZaU61mKDK9tTyWtHrYHufqGW5gqgLk9J0kTVlz+YGSeZpmqh/53wP5M PaXAoUNnPDtqB9kagMVoQtA6vvdhLhe4QVaze1/syzir+wH0k/AAhxXSMmQeZFBFEcId 7/yw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=aR9uOOoh3oK/aQLNaF4a7C2DYm1gIiIvrFWx3+McuuQ=; b=nfq70o1A1bgPlDEX8FCT1skWein6O/6p+5ucVAb7/rhJU57vh4Wsr8+Ty+DqGnoRHZ I2sH0qLMvyWanAQN7dARieXY8NjQsdPI2RDVeK0EyMuSQ8rNM9wH6brD1+mi0c7WIS3e n/Vwmfgt5j8A4B2n9Kbf4L0ilGNOfP3XoluWrW0ql6v+Sme4UCNMYdaTZqqm015U3Hqv ZCdLThle+kp0PeOg/EPhxAy6h41Qs7+tS8zyDGF/ixkKWGEcUpwTrnDCGQtv2YDS/T6C JybeU/BCRvLzL03o8wYA7sUG3rRH8/3nIuzB/JTgP4X/LsMU4TK18ExBR5I0uN24wEIs CBIw== X-Gm-Message-State: AOAM533xybJMvjs3Bzv4fV4gvMQHLB3B7UsYreiLL6sWbQZrI7vJsVcP 1QkXHXb8ZNcQtSPEBbcX+PMVHwXDE5DCdA== X-Google-Smtp-Source: ABdhPJyfmz5MScqrtv5uHbXD4HHqWfYvoWvveY2Ea8JHyNFOa6syxJBxD3+Ru254vMRAKcKjHngHKQ== X-Received: by 2002:adf:f6cc:: with SMTP id y12mr1459889wrp.35.1611010028314; Mon, 18 Jan 2021 14:47:08 -0800 (PST) Received: from localhost.localdomain (cpc91226-cmbg18-2-0-cust7.5-4.cable.virginm.net. [82.0.29.8]) by smtp.gmail.com with ESMTPSA id b3sm1525898wme.32.2021.01.18.14.47.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 18 Jan 2021 14:47:07 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Mon, 18 Jan 2021 22:44:48 +0000 Message-Id: <20210118224455.750030-7-sw@jkqxz.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210118224455.750030-1-sw@jkqxz.net> References: <20210118224455.750030-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 06/13] h264_metadata_bsf: Improve interpretation of input display matrices 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" The previous code here only worked in more limited cases. --- libavcodec/h264_metadata_bsf.c | 44 +++++++++++++++++++++++----------- 1 file changed, 30 insertions(+), 14 deletions(-) diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index 36f8aab245..71b49696cd 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -431,23 +431,39 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, data = av_packet_get_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, &size); if (data && size >= 9 * sizeof(int32_t)) { int32_t matrix[9]; - int hflip, vflip; - double angle; + double dmatrix[9]; + int hflip, vflip, i; + double scale_x, scale_y, angle; memcpy(matrix, data, sizeof(matrix)); - hflip = vflip = 0; - if (matrix[0] < 0 && matrix[4] > 0) - hflip = 1; - else if (matrix[0] > 0 && matrix[4] < 0) - vflip = 1; - av_display_matrix_flip(matrix, hflip, vflip); + for (i = 0; i < 9; i++) + dmatrix[i] = matrix[i] / 65536.0; + + // Extract scale factors. + scale_x = hypot(dmatrix[0], dmatrix[3]); + scale_y = hypot(dmatrix[1], dmatrix[4]); + + // Select flips to make the main diagonal positive. + hflip = dmatrix[0] < 0.0; + vflip = dmatrix[4] < 0.0; + if (hflip) + scale_x = -scale_x; + if (vflip) + scale_y = -scale_y; + + // Rescale. + for (i = 0; i < 9; i += 3) { + dmatrix[i] /= scale_x; + dmatrix[i + 1] /= scale_y; + } - angle = av_display_rotation_get(matrix); + // Extract rotation. + angle = atan2(dmatrix[3], dmatrix[0]); - if (!(angle >= -180.0 && angle <= 180.0 /* also excludes NaN */) || - matrix[2] != 0 || matrix[5] != 0 || - matrix[6] != 0 || matrix[7] != 0) { + if (!(angle >= -M_PI && angle <= M_PI) || + matrix[2] != 0.0 || matrix[5] != 0.0 || + matrix[6] != 0.0 || matrix[7] != 0.0) { av_log(bsf, AV_LOG_WARNING, "Input display matrix is not " "representable in H.264 parameters.\n"); } else { @@ -455,8 +471,8 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, disp->ver_flip = vflip; disp->anticlockwise_rotation = (uint16_t)rint((angle >= 0.0 ? angle - : angle + 360.0) * - 65536.0 / 360.0); + : angle + 2 * M_PI) * + 32768.0 / M_PI); write = 1; } }