From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <ffmpeg-devel-bounces@ffmpeg.org>
Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100])
	by master.gitmailbox.com (Postfix) with ESMTPS id 2110D4BCDA
	for <ffmpegdev@gitmailbox.com>; Sun,  2 Feb 2025 21:47:19 +0000 (UTC)
Received: from [127.0.1.1] (localhost [127.0.0.1])
	by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id CE0D268C20D;
	Sun,  2 Feb 2025 23:47:06 +0200 (EET)
Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com
 [209.85.214.182])
 by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 6D78768C097
 for <ffmpeg-devel@ffmpeg.org>; Sun,  2 Feb 2025 23:46:59 +0200 (EET)
Received: by mail-pl1-f182.google.com with SMTP id
 d9443c01a7336-215770613dbso47887795ad.2
 for <ffmpeg-devel@ffmpeg.org>; Sun, 02 Feb 2025 13:46:59 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=gmail.com; s=20230601; t=1738532817; x=1739137617; darn=ffmpeg.org;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:to:from:from:to:cc:subject:date:message-id
 :reply-to; bh=1hsyKbhMQRqlZd+rrPAUmxnNbHchFW0ZBFl8aitK9WM=;
 b=P/k/+nGiW5m5fQuguKIi9yR7CVlpMr6M5FXFH2JfFSuI4lRmm1Ox1ECAgKqqVPVKGv
 qKwNBWdgLBD9Sv69KD5DIbCUOBgz1sd9lQ77b6SuQlfbYuQgj3l3U+8CtHdhHRYfzt+f
 /qO6weXvPqqIBF4TXnstF7oKUS/Xy+iDrO1QX6hnG0TKEUU2rJwbnRp76o/qDaWrzqvh
 WYxqd500zuCrhIev8NQ5z8TL8S9rh1iDG3EW6TM8txYvNTVMfaXVm4cIAxkpauYNcn3I
 l5LmnlLZwjvaBBnSGVVcwa7GP+FZV8ndMmLFUZ5Ukrchm6agICO4AmMlnBKVf60f0wZN
 4cWA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
 d=1e100.net; s=20230601; t=1738532817; x=1739137617;
 h=content-transfer-encoding:mime-version:references:in-reply-to
 :message-id:date:subject:to:from:x-gm-message-state:from:to:cc
 :subject:date:message-id:reply-to;
 bh=1hsyKbhMQRqlZd+rrPAUmxnNbHchFW0ZBFl8aitK9WM=;
 b=FXEEsXC0CkGLdI6qvbMVNTd1U5pLNu9CFYmRPaq+10r1hhHNTwPgNS5wyJ2KkKjj4l
 0ldUKPMg8BoVVau2AMgLCh3TX8vvMP/KU4jtHBexoSDkXYiSA1qXyAgxFF3Jy08g+Nom
 GNi0hwLqA95Ytti9xxk7JPt5LFp+x4xIhWhjxaU2pEEzz87BE1StKZGtdB3kcg48hlwj
 2qBB+jQlQCWK9DuUBdsLYBSYEd2meSHYM85HLQFDwTWzY5bJ/5QvJJqLoHPpiMAIbw/h
 ZkWZPJvGePuAc7IAawVDjBZDbLjDLMtzn8OlpmEoWJzYQy//hk1sAMAWaCqXbG2zAF52
 ER4w==
X-Gm-Message-State: AOJu0Yx9fNvSlSPSKFvr6dzACOGM/oILSZLoLMVmS3Q6jAK2fB8g9TbY
 YNmBwXQ0mRqkUMsjCxTwwT9DCtWAUv3Nz8tOSdKOAv3PhmSZF1pWpQZiGEtm
X-Gm-Gg: ASbGnctaDtGnDK8W+y2rphkRmZbjTSHBcTa4jXcjib+GK1m47nMzQ6cQchiWRJFbOR1
 z8plM2t2jSgbuYuHEYdgjVuHQAcCr+yCWOqFN6AO8nEAZztWI3XUiZl4g/SG+kXdl7TPfzt86bP
 jB72Fauxwhe8eKy1vpWru0uJP1rPhdnZhI507LBhA5qPaqPTShgNlCgEdMJ6quPT/Rh46z0b+sB
 7U65U/w7JKlwqV+sEqUY+S8TOASFidlfM6SLD5DrdUsmhRLO2Smm54fUQMjL5n3NCFOMTXwpP8K
 raUIA+qb6CaWhFR2Q+ramtSgrtz8DBvw
X-Google-Smtp-Source: AGHT+IEa96IVv8onWtiGpIUeOW4A0PIi0Ux8Onu4VRfjqJW8Flcg1a4ujMUSYxk7sX0eCypS2Y8xmA==
X-Received: by 2002:a05:6a00:2405:b0:728:f337:a721 with SMTP id
 d2e1a72fcca58-72fd0be5c27mr29935007b3a.7.1738532817232; 
 Sun, 02 Feb 2025 13:46:57 -0800 (PST)
Received: from localhost.localdomain ([2800:2121:b040:c:511c:7b61:5388:930d])
 by smtp.gmail.com with ESMTPSA id
 d2e1a72fcca58-72fe653ab3asm7000029b3a.82.2025.02.02.13.46.55
 for <ffmpeg-devel@ffmpeg.org>
 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);
 Sun, 02 Feb 2025 13:46:56 -0800 (PST)
From: James Almer <jamrial@gmail.com>
To: ffmpeg-devel@ffmpeg.org
Date: Sun,  2 Feb 2025 18:46:42 -0300
Message-ID: <20250202214642.5316-2-jamrial@gmail.com>
X-Mailer: git-send-email 2.48.1
In-Reply-To: <20250202214642.5316-1-jamrial@gmail.com>
References: <20250202214642.5316-1-jamrial@gmail.com>
MIME-Version: 1.0
Subject: [FFmpeg-devel] [PATCH 2/2] swresample/rematrix: add support for
 custom order channel layouts
X-BeenThere: ffmpeg-devel@ffmpeg.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: FFmpeg development discussions and patches <ffmpeg-devel.ffmpeg.org>
List-Unsubscribe: <https://ffmpeg.org/mailman/options/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=unsubscribe>
List-Archive: <https://ffmpeg.org/pipermail/ffmpeg-devel>
List-Post: <mailto:ffmpeg-devel@ffmpeg.org>
List-Help: <mailto:ffmpeg-devel-request@ffmpeg.org?subject=help>
List-Subscribe: <https://ffmpeg.org/mailman/listinfo/ffmpeg-devel>,
 <mailto:ffmpeg-devel-request@ffmpeg.org?subject=subscribe>
Reply-To: FFmpeg development discussions and patches <ffmpeg-devel@ffmpeg.org>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: ffmpeg-devel-bounces@ffmpeg.org
Sender: "ffmpeg-devel" <ffmpeg-devel-bounces@ffmpeg.org>
Archived-At: <https://master.gitmailbox.com/ffmpegdev/20250202214642.5316-2-jamrial@gmail.com/>
List-Archive: <https://master.gitmailbox.com/ffmpegdev/>
List-Post: <mailto:ffmpegdev@gitmailbox.com>

Limited to the same channels as a native layout, but not constrained by channel ordering.

Signed-off-by: James Almer <jamrial@gmail.com>
---
Untested, as i don't have a sample from which we export a custom layout (Like PCM in mp4).

 libswresample/rematrix.c | 28 +++++++++++++++++-----------
 1 file changed, 17 insertions(+), 11 deletions(-)

diff --git a/libswresample/rematrix.c b/libswresample/rematrix.c
index 8b6e8ae1c5..e3c2cc1211 100644
--- a/libswresample/rematrix.c
+++ b/libswresample/rematrix.c
@@ -104,6 +104,14 @@ static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s
 }
 
 static int sane_layout(AVChannelLayout *ch_layout) {
+    if(ch_layout->nb_channels >= SWR_CH_MAX)
+        return 0;
+    if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
+        for (int i = 0; i < ch_layout->nb_channels; i++) {
+            if (ch_layout->u.map[i].id >= 64)
+                return 0;
+        }
+    else
     if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
         return 0;
     if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
@@ -118,8 +126,6 @@ static int sane_layout(AVChannelLayout *ch_layout) {
         return 0;
     if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
         return 0;
-    if(ch_layout->nb_channels >= SWR_CH_MAX)
-        return 0;
 
     return 1;
 }
@@ -130,9 +136,10 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
                          ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
 {
     double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
-    uint64_t unaccounted = in_ch_layout->u.mask & ~out_ch_layout->u.mask;
+    uint64_t unaccounted =  av_channel_layout_subset(in_ch_layout,  UINT64_MAX) &
+                           ~av_channel_layout_subset(out_ch_layout, UINT64_MAX);
     double maxcoef=0;
-    int i, j, out_i;
+    int i, j;
 
     for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
         if(   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
@@ -305,14 +312,15 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
     }
 
 
-    for(out_i=i=0; i<64; i++){
+    for(i=0; i<64; i++){
         double sum=0;
-        int in_i=0;
-        if (av_channel_layout_index_from_channel(out_ch_layout, i) < 0)
+        int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
+        if (out_i < 0)
             continue;
         for(j=0; j<64; j++){
-            if (av_channel_layout_index_from_channel(in_ch_layout, j) < 0)
-               continue;
+            int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
+            if (in_i < 0)
+                continue;
             if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
                 matrix_param[stride*out_i + in_i] = matrix[i][j];
             else
@@ -320,10 +328,8 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
                 (   av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
                  && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
             sum += fabs(matrix_param[stride*out_i + in_i]);
-            in_i++;
         }
         maxcoef= FFMAX(maxcoef, sum);
-        out_i++;
     }
     if(rematrix_volume  < 0)
         maxcoef = -rematrix_volume;
-- 
2.48.1

_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".