From patchwork Mon Jul 27 16:32:37 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 21307 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 7C5F444AF2C for ; Mon, 27 Jul 2020 19:36:40 +0300 (EEST) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C548C68B944; Mon, 27 Jul 2020 19:33:13 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wm1-f65.google.com (mail-wm1-f65.google.com [209.85.128.65]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id E5E0968B94F for ; Mon, 27 Jul 2020 19:33:05 +0300 (EEST) Received: by mail-wm1-f65.google.com with SMTP id 184so15401360wmb.0 for ; Mon, 27 Jul 2020 09:33:05 -0700 (PDT) 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=N/+ZycHUlam0nrlXCmiS0T86tkyZim1/W80+zjnNU8A=; b=qw2rkQyqE1dkriArJah3gR/rdR2DdiLYA2WbpqmxGgeKMw5hjTCgODgdaO4n7MDara 5IQqsPl695LdQ5HBifUaxuohJ8cjmlXY9NlyKzaBynvTCD8f80zWTBmgs1xATR6dnlKW 5H3CU9w9xGw7OhrR3qXq/8PElgTObsSmft1SSrfgyy0UB/q9Zl/GcgMnBIPivqB1tcCH jYGrUsb3holSbzdAkzULoPHOeYt5scyv+Bh3JQ/Ad+78ML7uB4dnGVXPkUrGMHQjh2qj GZGj+kf1yLjBqH7b1VD5bHtOzb+5kg1/3+07cohTFuiK3Nzrqk4FrhWEqquqYGudj96+ yAEA== 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=N/+ZycHUlam0nrlXCmiS0T86tkyZim1/W80+zjnNU8A=; b=cII/DEtCrzoWuKzKyzvvvzvwKD7B0oeAF+AT8NLxCfZuHIO3G5v9De91en2Sde3eO/ w2GgPoPjv974NJbnh8al1qN2LvJczrWHUF0+bsQR16qKWF4Vdovq/3udFsf0I6NngLG7 O5/hAIQtwBKkWBCJTVBzuUHgFmaQ+EwRnhTS8F2Z34F1jFFd/SFDdSyvxaRoTfbeWrM1 BidXx+ARZbKVgg/aZ9TM7FFCPxPSSIbYxma26jyAhkYbVHl2s6MhheOxFuNEH8wb5rxG Y+P+0hxjGRkWOWqpPmSvXxwBhwx4bT9tKEz8sKpjtNNsuWiA4UdNkhgkujLisqqFWaDE /VgA== X-Gm-Message-State: AOAM530P1odIHSu2cT+E9/UzXR316hXbsvvLJknLXw8X62H6CHsGcE+p eZs8+oYP5O4uMEqYunMAs9M5ZsqjnYU= X-Google-Smtp-Source: ABdhPJxMQiSlZt1xhBXlgcnuqwy9aQJEMrgQ4L9Z0vK0PAdOL6T32hQ5d5/BdJKrAk5w6m7dhaHLtA== X-Received: by 2002:a1c:2547:: with SMTP id l68mr86894wml.181.1595867585119; Mon, 27 Jul 2020 09:33:05 -0700 (PDT) Received: from rywe.jkqxz.net (cpc91242-cmbg18-2-0-cust650.5-4.cable.virginm.net. [82.8.130.139]) by smtp.gmail.com with ESMTPSA id w16sm14706791wrg.95.2020.07.27.09.33.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 27 Jul 2020 09:33:04 -0700 (PDT) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Mon, 27 Jul 2020 17:32:37 +0100 Message-Id: <20200727163237.23371-23-sw@jkqxz.net> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200727163237.23371-1-sw@jkqxz.net> References: <20200727163237.23371-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 22/22] 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 | 42 +++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index 1d00ccdfb8..38c9e7f6f5 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -441,23 +441,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]; + double dmatrix[9]; int hflip, vflip; - double angle; + 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 { @@ -465,8 +481,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; } }