From patchwork Sun Feb 21 19:51:13 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 25868 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 B8A4D449DB5 for ; Sun, 21 Feb 2021 21:53:44 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 9CFFD68A8F0; Sun, 21 Feb 2021 21:53:44 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f54.google.com (mail-wr1-f54.google.com [209.85.221.54]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 956C6689805 for ; Sun, 21 Feb 2021 21:53:34 +0200 (EET) Received: by mail-wr1-f54.google.com with SMTP id n8so16853823wrm.10 for ; Sun, 21 Feb 2021 11:53:34 -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=FcscOkfabZE42eht3O6YMCf42g2uK2gOUnDoqoWNNuk=; b=YYLdoAf1SHV4jNx0djFGHmCBcffPPbtvT4LESDUOKZCVnwCbrP5J+B1mPsOmfSjlYs 48QLk/trgGNQJLL94hu6piTRiRA5dw5mVdud2Bz3RWNzzXXvDwNqpdFpG4dZPCQt1Ki5 1k8q0P5EQouDZ1sr/rIef3CPCVl240Xv9Mk/iKLpxvcS/pD/BbCmgZEHPVqcyzQao9nQ J6lqZ+lEkCND+5uKV9zOxf99wi/9Fd7tnBUOLsqjlMU2rdsvHOKVkhWkCRAvPL5TbFmH mPurnosTOILdpws3s0x2HMmWcRNJCrkd/L7gg7eCJpBO8ogbxMfIjBSg+jMBkXPPXDoU Xc2w== 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=FcscOkfabZE42eht3O6YMCf42g2uK2gOUnDoqoWNNuk=; b=V8xjUiKjyirnEk864/lSjqNCPoUT5iZ8y7cI1VGk5v3nFkdpErhXyfcLlrCvUNpMQ5 pFzT0qOzrLoTMFQtovdW3dTfrCdUgsusCbs47/xip4rZagKIErIgts4YA3nnOrnsDbrH AdwuoTm2pH9PTeW3RGHw7zT+bibbEvjTBMJnmbC8z0h93B/0cdmw8IYAuziU0qki7pgD d3TNhgmdUEJbUNnCsULsGDtplRv8vlqJO/p+ZoKxNAnbXB3aL7n7VsJTbKgFzyWcTzol zeyIn7MmPqmhAnl88PEnNEAVUxqGvx9f7M6sjHBCsavKu+KG5H+K/yDieqb/LPRFps2S lMrg== X-Gm-Message-State: AOAM5318apbMG9gmpE6TrYDPWYarLqILztcrQTkiJnWVyGej2m/cDhjs E6pWChysHO9HZVXRrOczbo5UsDdejsqPvA== X-Google-Smtp-Source: ABdhPJxCBY6kpIH/ZzCpSaVPtz7zAJo+L02MJbkn8L5DIpTKOdQQ+md4GQc8GBe3IkbHtAlggssMfQ== X-Received: by 2002:a05:6000:1806:: with SMTP id m6mr18355948wrh.320.1613937214167; Sun, 21 Feb 2021 11:53:34 -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 z18sm1780372wrs.19.2021.02.21.11.53.33 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 21 Feb 2021 11:53:33 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Sun, 21 Feb 2021 19:51:13 +0000 Message-Id: <20210221195125.1901683-6-sw@jkqxz.net> X-Mailer: git-send-email 2.30.0 In-Reply-To: <20210221195125.1901683-1-sw@jkqxz.net> References: <20210221195125.1901683-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v2 06/18] cbs_h264: Implement fill and extract for display orientation SEI message 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" --- libavcodec/cbs_h2645.c | 79 ++++++++++++++++++++++++++++++++++++++++++ libavcodec/cbs_sei.c | 2 ++ 2 files changed, 81 insertions(+) diff --git a/libavcodec/cbs_h2645.c b/libavcodec/cbs_h2645.c index ecd2001816..c01d243455 100644 --- a/libavcodec/cbs_h2645.c +++ b/libavcodec/cbs_h2645.c @@ -18,6 +18,7 @@ #include "libavutil/attributes.h" #include "libavutil/avassert.h" +#include "libavutil/display.h" #include "libavutil/mastering_display_metadata.h" #include "bytestream.h" @@ -1683,6 +1684,83 @@ static const SEIMessageTypeDescriptor cbs_sei_common_types[] = { SEI_MESSAGE_TYPE_END, }; +static void cbs_h264_fill_sei_display_orientation + (H264RawSEIDisplayOrientation *disp, const int32_t matrix[9]) +{ + double dmatrix[9]; + int hflip, vflip, i; + double scale_x, scale_y, angle; + uint16_t rotation; + + if (matrix[2] != 0 || matrix[5] != 0 || + matrix[6] != 0 || matrix[7] != 0) { + // Not representable. + goto cancel; + } + + 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; + } + + // Extract rotation. + angle = -atan2(dmatrix[3], dmatrix[0]); + if (!(angle >= -M_PI && angle <= M_PI)) { + // Not representable. + goto cancel; + } + if (angle < 0.0) + angle += 2 * M_PI; + rotation = (uint16_t)lrint(angle * 32768.0 / M_PI); + + if (!hflip && !vflip && rotation == 0) { + // This is the identity transformation, so rather than setting + // values for a new display orientation we should instead cancel + // any previous message. + cancel: + *disp = (H264RawSEIDisplayOrientation) { + .display_orientation_cancel_flag = 1, + }; + } else { + *disp = (H264RawSEIDisplayOrientation) { + .hor_flip = hflip, + .ver_flip = vflip, + .anticlockwise_rotation = rotation, + .display_orientation_repetition_period = 1, + }; + } +} + +static void cbs_h264_extract_sei_display_orientation + (int32_t matrix[9], const H264RawSEIDisplayOrientation *disp) +{ + if (disp->display_orientation_cancel_flag) { + // Set identity matrix. + av_display_rotation_set(matrix, 0.0); + } else { + av_display_rotation_set(matrix, + disp->anticlockwise_rotation * + 360.0 / 65536.0); + av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip); + } +} + static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = { { SEI_TYPE_BUFFERING_PERIOD, @@ -1719,6 +1797,7 @@ static const SEIMessageTypeDescriptor cbs_sei_h264_types[] = { 1, 0, sizeof(H264RawSEIDisplayOrientation), SEI_MESSAGE_RW(h264, sei_display_orientation), + SEI_MESSAGE_FE(h264, sei_display_orientation), }, SEI_MESSAGE_TYPE_END }; diff --git a/libavcodec/cbs_sei.c b/libavcodec/cbs_sei.c index a5990205d0..27b5997e47 100644 --- a/libavcodec/cbs_sei.c +++ b/libavcodec/cbs_sei.c @@ -384,6 +384,8 @@ static const SEIMetadata cbs_sei_metadata[] = { SEI_TYPE_MASTERING_DISPLAY_COLOUR_VOLUME }, { CBS_METADATA_CONTENT_LIGHT_LEVEL, SEI_TYPE_CONTENT_LIGHT_LEVEL_INFO }, + { CBS_METADATA_DISPLAY_MATRIX, + SEI_TYPE_DISPLAY_ORIENTATION }, }; static const SEIMessageTypeDescriptor *cbs_sei_find_type_from_metadata