From patchwork Sun Feb 23 23:41:24 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 17880 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 B5912449F1B for ; Mon, 24 Feb 2020 01:41:56 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id A1D3968B4EA; Mon, 24 Feb 2020 01:41:56 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f66.google.com (mail-wr1-f66.google.com [209.85.221.66]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9FCCE687F1F for ; Mon, 24 Feb 2020 01:41:53 +0200 (EET) Received: by mail-wr1-f66.google.com with SMTP id u6so8296615wrt.0 for ; Sun, 23 Feb 2020 15:41:53 -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=BDxOag9W3yj8SDavhDhYMJeGpd38vVFhnceMlGSxJFA=; b=X+EItI0I5F5EvvGrUzB02dVsZvPuHGDz99mPy24e3Z+8KV0ooKHG0gj0+iNKUFxfRR VkZwNgKrreO0kp7aAXGX4o0qe/ag5hblDo2VcQfZ6zQIDl72ti3Y4EJ8A5helIbB2Knh KzLZFHMcGgul/tYoWhSGdOeckp0GMi3OqqagivBZ38/uaHwxNoEj7SebNshkRGc3Krsm 4FTuh/IkbOE1MDcyQN/KP0zW4zIQAX2mfSgSkn3tFrYtfhQFuDdTb88ePQ8ex2ryIQUY +gXpP6P337AOA72Aoq1vBxHlTHA6UZWXiuVSDDpB7TxrpJorPOMq1fyv3BoV9XUcci/Z Vh8w== 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=BDxOag9W3yj8SDavhDhYMJeGpd38vVFhnceMlGSxJFA=; b=WDfUujfjB2ucGHJbpapYZ1cXZ00G5/vMpI2BZS5tqMGIVlO34hYwnXEaA85R94OIZ1 aE/LW1LFnRdXXWGe29rQ0UkAEj+ZN7EG/IpG69QuRgG8X2VM/7kJw0r7QD2SCzhaAmqE QBjm6/V3gxJcsMGTIt7/CoLuSzZFTAriqQApyJGl9LxRfkzjY/G1t+UjOX7Or+d71t9v fTV/mUtgFdSOUUMzb0DmkmVwCPyx51E1clYD5sA1fexldsd3su++VyMPfOT0piDPoICk fegW6AgP0Wkig4FosB+8Kj5HEPnCSFoaDyzTvkQd3aZQ5ouyPCJz9SHpvYPLTElAW49e Ca5g== X-Gm-Message-State: APjAAAWKdaGZ6a0FewO0HxEw8SJ9YrJRe/cM8tgbAVatvwgiMWqaFRQO otwv4LTgpdrrOs+mLMa98ACqAATfVh8= X-Google-Smtp-Source: APXvYqzz/sgx5CjC83I1JX4TRH5BEonF+AurxjTkxiU93JQu9etP2dO3+59YxrfR/gXDFA8ksfo2Jw== X-Received: by 2002:a5d:6445:: with SMTP id d5mr61980158wrw.244.1582501312902; Sun, 23 Feb 2020 15:41:52 -0800 (PST) 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 y131sm16358070wmc.13.2020.02.23.15.41.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 23 Feb 2020 15:41:52 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 23 Feb 2020 23:41:24 +0000 Message-Id: <20200223234124.17689-21-sw@jkqxz.net> X-Mailer: git-send-email 2.25.0 In-Reply-To: <20200223234124.17689-1-sw@jkqxz.net> References: <20200223234124.17689-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4 21/21] 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 b2304373bf..8fc02c5f41 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -397,23 +397,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 { @@ -421,8 +437,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; } }