From patchwork Fri Jan 1 21:35:31 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Thompson X-Patchwork-Id: 24740 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 88FE644BCD1 for ; Fri, 1 Jan 2021 23:38:14 +0200 (EET) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 703D868A9CE; Fri, 1 Jan 2021 23:38:14 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-wr1-f43.google.com (mail-wr1-f43.google.com [209.85.221.43]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BE47668A8A6 for ; Fri, 1 Jan 2021 23:38:10 +0200 (EET) Received: by mail-wr1-f43.google.com with SMTP id w5so22894602wrm.11 for ; Fri, 01 Jan 2021 13:38:10 -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=na4uHB3dcRvELcgEPmQdCUb5FXZJkuITuP8jUbhq4vQ=; b=vom+SWRhO5sfcIjX83lKqr3O8Lg/LKq/0iB3EBLUmGDnNERT6SOaJ4+0ICWL5i1rcV iGETOHwj31j2fQmOlg2veVWc+cOlb+DG7Wm2b2CAxgH4Qb9Y1U1NBk/XWGHPw5VBvLiK DhdeRVpe2wEPJurmogag33Qmv/uxNIC/LpJbM/CIFZUZqoKNddRCwjF342Sdj/u+eiLQ RIKDmrVUsMJIxR7C/zzkZqQsx4UTInJ1mZPB80cMyq9ZIAxtio+xXobO8vSzSvhESYPP q/O6mMLLiJvCe/+ydWCLl7HLuWS4NOL2lr/2xQFe9Ci9M31G4y2mVE670axIib8jBils QtqQ== 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=na4uHB3dcRvELcgEPmQdCUb5FXZJkuITuP8jUbhq4vQ=; b=mdsk5885l4iENyXKaKE6Rw1D9uhp6kWjAtURlhORTLV/uHnuuCA59LxPPihW4Mivnk GG2fVw4Tnf5eJLvU0D35VtaDJRpKnI6G2gf7r3gs5mKc5ZhGDkQPaDoG93noZ3q37zQB 44PW4o14XrGWBLKnAwZA7/rexaQqLuh9Ftm7RgWPHlqnC70raprjJTjD66eYp7SNZO2u b1L8+JXI0g9dta/mhmGarsiK/h+vRdclfKC76l3bY45Tow/hoj5NAeDtO1IZ6WW7xw3d ZhBw28yNPQgm3p7mAVKnszfCx94PohlrUQHzTtZTJpEYKc4RJNIhoEu2HY5TUCPa5aSG m2+A== X-Gm-Message-State: AOAM5315iBbtA367ULMNqy9JsUrB61CU1QKhCHOyeOLPyO6xYTz+elSj M8XRxe9fv39QYnE4belRSsbq+tIXgPItYA== X-Google-Smtp-Source: ABdhPJzPJl8gx8A4yvWQEHppysP2jOCeFpaR4jrB/dMbVfd+QkpRH7pd11udyQ7mEMBt125Dx+4sMA== X-Received: by 2002:a5d:604a:: with SMTP id j10mr70779241wrt.290.1609537089991; Fri, 01 Jan 2021 13:38:09 -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 v20sm18784595wml.34.2021.01.01.13.38.09 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Jan 2021 13:38:09 -0800 (PST) From: Mark Thompson To: ffmpeg-devel@ffmpeg.org Date: Fri, 1 Jan 2021 21:35:31 +0000 Message-Id: <20210101213537.169546-22-sw@jkqxz.net> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20210101213537.169546-1-sw@jkqxz.net> References: <20210101213537.169546-1-sw@jkqxz.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 21/27] h264_metadata_bsf: Simplify display orientation handling 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 case where the user supplies new value directly as options still requires a bit of special handling. --- libavcodec/h264_metadata_bsf.c | 118 ++++++--------------------------- 1 file changed, 21 insertions(+), 97 deletions(-) diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c index f5f439018d..aae91a6e23 100644 --- a/libavcodec/h264_metadata_bsf.c +++ b/libavcodec/h264_metadata_bsf.c @@ -25,6 +25,7 @@ #include "cbs.h" #include "cbs_bsf.h" #include "cbs_h264.h" +#include "cbs_metadata.h" #include "h264.h" #include "h264_levels.h" #include "h264_sei.h" @@ -75,7 +76,6 @@ typedef struct H264MetadataContext { int display_orientation; double rotate; int flip; - H264RawSEIDisplayOrientation display_orientation_payload; int level; } H264MetadataContext; @@ -328,122 +328,46 @@ static int h264_metadata_handle_display_orientation(AVBSFContext *bsf, int seek_point) { H264MetadataContext *ctx = bsf->priv_data; - SEIRawMessage *message; int err; - message = NULL; - while (ff_cbs_sei_find_message(ctx->common.output, au, - H264_SEI_TYPE_DISPLAY_ORIENTATION, - &message) == 0) { - H264RawSEIDisplayOrientation *disp = message->payload; - int32_t *matrix; - - matrix = av_malloc(9 * sizeof(int32_t)); - if (!matrix) - return AVERROR(ENOMEM); - - av_display_rotation_set(matrix, - disp->anticlockwise_rotation * - 180.0 / 65536.0); - av_display_matrix_flip(matrix, disp->hor_flip, disp->ver_flip); - - // If there are multiple display orientation messages in an - // access unit, then the last one added to the packet (i.e. - // the first one in the access unit) will prevail. - err = av_packet_add_side_data(pkt, AV_PKT_DATA_DISPLAYMATRIX, - (uint8_t*)matrix, - 9 * sizeof(int32_t)); - if (err < 0) { - av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted " - "displaymatrix side data to packet.\n"); - av_free(matrix); - return AVERROR(ENOMEM); - } - } - - if (ctx->display_orientation == BSF_ELEMENT_REMOVE || - ctx->display_orientation == BSF_ELEMENT_INSERT) { - ff_cbs_sei_delete_message_type(ctx->common.output, au, - H264_SEI_TYPE_DISPLAY_ORIENTATION); - } - - if (ctx->display_orientation == BSF_ELEMENT_INSERT) { - H264RawSEIDisplayOrientation *disp = - &ctx->display_orientation_payload; + if (ctx->display_orientation != BSF_ELEMENT_INSERT) { + err = ff_cbs_bsf_apply_metadata(bsf, pkt, au, + CBS_METADATA_DISPLAY_MATRIX, + ctx->display_orientation); + if (err < 0) + return err; + } else { + int32_t matrix[9]; uint8_t *data; int size; int write = 0; + ff_cbs_sei_delete_message_type(ctx->common.output, au, + H264_SEI_TYPE_DISPLAY_ORIENTATION); + 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, i; - double scale_x, scale_y, angle; - - memcpy(matrix, data, sizeof(matrix)); - - 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) || - 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 { - disp->hor_flip = hflip; - disp->ver_flip = vflip; - disp->anticlockwise_rotation = - (uint16_t)rint((angle >= 0.0 ? angle - : angle + 2 * M_PI) * - 32768.0 / M_PI); - write = 1; - } + memcpy(matrix, data, 9 * sizeof(int32_t)); + write = 1; } if (seek_point) { if (!isnan(ctx->rotate)) { - disp->anticlockwise_rotation = - (uint16_t)rint((ctx->rotate >= 0.0 ? ctx->rotate - : ctx->rotate + 360.0) * - 65536.0 / 360.0); + av_display_rotation_set(matrix, ctx->rotate); write = 1; } if (ctx->flip) { - disp->hor_flip = !!(ctx->flip & FLIP_HORIZONTAL); - disp->ver_flip = !!(ctx->flip & FLIP_VERTICAL); + av_display_matrix_flip(matrix, + !!(ctx->flip & FLIP_HORIZONTAL), + !!(ctx->flip & FLIP_VERTICAL)); write = 1; } } if (write) { - disp->display_orientation_repetition_period = 1; - - err = ff_cbs_sei_add_message(ctx->common.output, au, 1, - H264_SEI_TYPE_DISPLAY_ORIENTATION, - disp, NULL); + err = ff_cbs_sei_insert_metadata(ctx->common.output, au, + CBS_METADATA_DISPLAY_MATRIX, + matrix); if (err < 0) { av_log(bsf, AV_LOG_ERROR, "Failed to add display orientation " "SEI message to access unit.\n");