From patchwork Wed May 15 01:08:28 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 48890 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:3a48:b0:1af:fc2d:ff5a with SMTP id zu8csp1302314pzb; Tue, 14 May 2024 18:08:53 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCUNdCXkN8Bul4KwourJpfHlTlnxEHY5eGdEWltfdNsxikk9QRDkWTW3yoF5ogj/T1GIpIAy3fk8aL7vJiYAXQzEqkoHBnShU8T15Q== X-Google-Smtp-Source: AGHT+IFS//NagaSKc9K49xXnvZ99vYvBjqPM0njtFn2XfkyWM8syn+gH0y1zOZDL8AnFYIq1PLqE X-Received: by 2002:a17:906:fe0e:b0:a5a:5c8f:120d with SMTP id a640c23a62f3a-a5a5c8f12a7mr844296966b.36.1715735333178; Tue, 14 May 2024 18:08:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715735333; cv=none; d=google.com; s=arc-20160816; b=Qxln8vYV8SBN/EmjwPRwAU10mXCnpmfQKVS9c0uuzajYBJdFfOi+4H/ef+IddyAl3w di5ms3DlqabitYFkMlX3ItqFy5noNbfW5NFPdwfLP1eTSnxHcd6IABdL1Xc2UM8HJe9E 5kYmqDTF+6f3GWC+bpj5IiyZKOoDN/gUWPoLaM+WP+uWtn5go3ASo908N+X5GN6Meq9P bMPdu5898kYkB6VYCaxB8D0FeO87k6GRHCBwtSQOASuhz3eOTFINTMXGb3sjhwEU5vQY CDeJS7JQkjCoaoEzgTGmFHkuXQjbHU2OFThGa+N9mIPdLaK60zTUuurPOq8Y5Q10ELBK 4+Ng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:message-id:date:to:from :dkim-signature:delivered-to; bh=t9IKp4pKIsdhuCoF6qV+h0oh/HihJHK6FuIwNkj3h6k=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=pmKYKndP5TV7glBapTdFalHj0KPKwBUIa2Iw24WZCLRKPR+aS2dsy1NsXZJilVFcRA RBy7wgf66jt5x31XVcobXLVJWUGrGyurLHZ0hAkjcE6zgWXjTlN43QTp4Fp33mUiYP5X xpdsaznRUvb3jnLYLKQEeRX8nTryVwN6zEqoOX+YiiQyV1MznRJ04QHn0s86aLmyZdac hxrPpgFj4vIfLK+HYL9LtubvFrXAPqJex5toZpFEGqD8nrveVorFnL+zmVD2rrpncSkG JYSHVgyDeyF9QR1KphAPwlbLPgHQHV4yYbmqcfIt57bsYWxNuG6vjFt27BiSm+BonJq1 moGw==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=b93Jfu4n; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id a640c23a62f3a-a5a1b7907f7si713502666b.772.2024.05.14.18.08.52; Tue, 14 May 2024 18:08:53 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=b93Jfu4n; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 8D5AD68D65B; Wed, 15 May 2024 04:08:48 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id BB29A68D4E8 for ; Wed, 15 May 2024 04:08:42 +0300 (EEST) Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-6f44390e328so5760244b3a.2 for ; Tue, 14 May 2024 18:08:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715735319; x=1716340119; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=UvbMLB54jW4Sq4H8Bv+SrlU+z7aA1NA/RAjYcLiJ/iA=; b=b93Jfu4nJHZ/al4zhzQqEsL2+TiDfEWPW3pqnrIsz9IdfJoDnGPSIk7quacvHj+FYq iA9d7K3tRQugqtwFI7W4EKyFHM2772eF4UgGVJh7hlDmqnO/EDQcgeqMF8JMX5+M/TxM e7gqBiEPTApxG3lF5yv2wa0kmMTBTZ9tmw5sxUIZZSe9ayAb899Fz6c9e1jxxQzACs6r OOj1Nu8wJ2oATftFJcX6X9xdaIj9bNGoZI7abZDPY/f1f10UI0FPfOAw7x5x5jr12ag6 /KNAeP8dW/nCGE3dQO/gC4li8Q1TEBYi8e598DKQcNpaqugDcDKfUQ3GOyCdUqGjVLS+ GMVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715735319; x=1716340119; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=UvbMLB54jW4Sq4H8Bv+SrlU+z7aA1NA/RAjYcLiJ/iA=; b=g8Qjey3/71BP6bweztdyhAMD3viZydQjfvzWZ26yEwLXPKdKN5/zf+HLxxFf8RRtq6 2dVfz3TK5alH6zyodW+P0kVZcShT9ijriOZbqx6IkWROlIy2mE/d2x/C0Nwhp0AGfWNJ md4tpzP+8v/Ub1g9kwIC8+aSc4xmgjWzNna5W3IvpLV6PyzNz0rsVvs9UQ9RJy/6kHIJ 9Dz8th4QkzPOdf3n2ALZVje8htgObrYup1HJFDvYPPM+S3q9IshpcOCa/eWd6yIj+UfB 9D5SNB7l/MliBa2KLGlXnm013gNWmx7VHlLkRV1wkCAnp8xpmEP1M/26aITVECWrr3B+ qZig== X-Gm-Message-State: AOJu0YzL58NRqzyqOHa5Si599m0MX2bePL+ZZ/p82jYoyc3qjsSz3fZA 2zzoGdK1omtM3/diuXaF6691BhG8tA5YNDtC9Qx1T+/w5Sm5xRdFuygWLw== X-Received: by 2002:a05:6a00:99f:b0:6ed:21c0:986c with SMTP id d2e1a72fcca58-6f4e03484a5mr23903350b3a.24.1715735319203; Tue, 14 May 2024 18:08:39 -0700 (PDT) Received: from localhost.localdomain ([190.194.167.233]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66691sm10135237b3a.16.2024.05.14.18.08.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 18:08:38 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 May 2024 22:08:28 -0300 Message-ID: <20240515010829.2667-1-jamrial@gmail.com> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avutil/channel_layout: add a helper function to get the ambisonic order of a layout X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: jF/HwTTuJKNn Signed-off-by: James Almer --- libavutil/channel_layout.c | 17 ++++++++--------- libavutil/channel_layout.h | 10 ++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/libavutil/channel_layout.c b/libavutil/channel_layout.c index fd6718e0e7..e213f68666 100644 --- a/libavutil/channel_layout.c +++ b/libavutil/channel_layout.c @@ -473,15 +473,14 @@ static int has_channel_names(const AVChannelLayout *channel_layout) return 0; } -/** - * If the layout is n-th order standard-order ambisonic, with optional - * extra non-diegetic channels at the end, return the order. - * Return a negative error code otherwise. - */ -static int ambisonic_order(const AVChannelLayout *channel_layout) +int av_channel_layout_get_ambisonic_order(const AVChannelLayout *channel_layout) { int i, highest_ambi, order; + if (channel_layout->order != AV_CHANNEL_ORDER_AMBISONIC && + channel_layout->order != AV_CHANNEL_ORDER_CUSTOM) + return AVERROR(EINVAL); + highest_ambi = -1; if (channel_layout->order == AV_CHANNEL_ORDER_AMBISONIC) highest_ambi = channel_layout->nb_channels - av_popcount64(channel_layout->u.mask) - 1; @@ -536,7 +535,7 @@ static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout) if (masked_description(channel_layout, 0) > 0) return AV_CHANNEL_ORDER_NATIVE; - order = ambisonic_order(channel_layout); + order = av_channel_layout_get_ambisonic_order(channel_layout); if (order >= 0 && masked_description(channel_layout, (order + 1) * (order + 1)) >= 0) return AV_CHANNEL_ORDER_AMBISONIC; @@ -551,7 +550,7 @@ static enum AVChannelOrder canonical_order(AVChannelLayout *channel_layout) static int try_describe_ambisonic(AVBPrint *bp, const AVChannelLayout *channel_layout) { int nb_ambi_channels; - int order = ambisonic_order(channel_layout); + int order = av_channel_layout_get_ambisonic_order(channel_layout); if (order < 0) return order; @@ -945,7 +944,7 @@ int av_channel_layout_retype(AVChannelLayout *channel_layout, enum AVChannelOrde if (channel_layout->order == AV_CHANNEL_ORDER_CUSTOM) { int64_t mask; int nb_channels = channel_layout->nb_channels; - int order = ambisonic_order(channel_layout); + int order = av_channel_layout_get_ambisonic_order(channel_layout); if (order < 0) return AVERROR(ENOSYS); mask = masked_description(channel_layout, (order + 1) * (order + 1)); diff --git a/libavutil/channel_layout.h b/libavutil/channel_layout.h index 8a078d1601..c2ab236488 100644 --- a/libavutil/channel_layout.h +++ b/libavutil/channel_layout.h @@ -679,6 +679,16 @@ int av_channel_layout_check(const AVChannelLayout *channel_layout); */ int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1); +/** + * Return the order if the layout is n-th order standard-order ambisonic. + * The presence of optional extra non-diegetic channels at the end is not taken + * into account. + * + * @param channel_layout input channel layout + * @return the order of the layout, a negative error code otherwise. + */ +int av_channel_layout_get_ambisonic_order(const AVChannelLayout *channel_layout); + /** * The conversion must be lossless. */ From patchwork Wed May 15 01:08:29 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: James Almer X-Patchwork-Id: 48891 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:3a48:b0:1af:fc2d:ff5a with SMTP id zu8csp1302371pzb; Tue, 14 May 2024 18:09:03 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCW1qHa2nJ0kfwIFVQDQn7aP6A2ibr8RayxzOUQPSw0/JJlXuXDGoGb9knkrMLiktrZJ+eR+NHSRRDZWrSRAhI+Wc/ZcjuRT3dIJ5A== X-Google-Smtp-Source: AGHT+IE37xDhNeQYIIAOl9swOQ1gFkRDrbxT4s5lOpA2uGvJVHUEIh5W79eL85GUb0TURax0j8vg X-Received: by 2002:a50:9553:0:b0:574:ea65:f9ab with SMTP id 4fb4d7f45d1cf-574ea65fa49mr1816862a12.20.1715735343545; Tue, 14 May 2024 18:09:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1715735343; cv=none; d=google.com; s=arc-20160816; b=oCijo4J9Nb29fqJudFlJW9mWim0xkXy1TdflZr5KF2tEQZntwf7B16DvnrSnMIt7OU nLqwM3OTCkUxlImYhqAUxLRW5chTQPWlUyNepa8juXcasyZHrtTUQlLIz8tJJspy4o2i m5GYI33bPmsRqA6IyMT3vP01fwsOuixw5Jk6mqMx+fHK8yqzKhFKtk6QzUgcZJR84ZWi /SnAMYd6rKPjme3lY0UJh7bwO+szGmSsqcZzaPfX6ZXp8+ZuprDJT9f/9puvR/eqrAAd drDNKOvxkHqKhe6aFB7J0DCQIo8/+8uccrb3yQSQY8a/lUnr8vdcM8ICIt+L3G18+nc/ dJ4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:reply-to:list-subscribe :list-help:list-post:list-archive:list-unsubscribe:list-id :precedence:subject:mime-version:references:in-reply-to:message-id :date:to:from:dkim-signature:delivered-to; bh=barpbTtZVLXiH6SFFq55d3eTHYfxI9LQXDnkq36hYHI=; fh=YOA8vD9MJZuwZ71F/05pj6KdCjf6jQRmzLS+CATXUQk=; b=N+0FABGQpZwlo9EunWMt5cFLKi4VXblq1AJny7qLEoxZV9PVtyBFv25jnKNZWt7mV6 n0OUNDjQGsZTVwkxc1Ldb7hv0ONXpKfAFDr8grKPUXe6O3xcPE3utMEvAK/BPCwE8MxJ Xvj0onH9uwdXuNCpT+biKYyZ15HXSO7JldtB+8kAHRRzpRN/WETntGEzGf2iqbS03Ton x4tNt1avZrz2AOGUdDOVNEJ7p0aTs8XkwNoUoRCX1T55Dlkj97bHZ12CbIhy1inHrfJN MkyX9N91XjKapYtp8jndQDRyeWP+xP8rEtcyEojtFxQASVF4qSZf4+quamkOeIog/Kr8 u43w==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=k0PlgeZH; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id 4fb4d7f45d1cf-5733c3777e0si6757609a12.605.2024.05.14.18.09.03; Tue, 14 May 2024 18:09:03 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@gmail.com header.s=20230601 header.b=k0PlgeZH; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org; dmarc=fail (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id C5CA168D4D0; Wed, 15 May 2024 04:08:51 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f178.google.com (mail-pf1-f178.google.com [209.85.210.178]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 9CB0A68D670 for ; Wed, 15 May 2024 04:08:43 +0300 (EEST) Received: by mail-pf1-f178.google.com with SMTP id d2e1a72fcca58-6f44bcbaae7so5594096b3a.2 for ; Tue, 14 May 2024 18:08:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1715735321; x=1716340121; 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=rmIhzIE95fWoUSftNH5w96W/OYKp/ezAOoEvyXF7Z8M=; b=k0PlgeZHIYZej2dd0U0gxQvvuUglgT9IJcAD9wnGH3Yd4Skmcx8gHacBmbaR2TBzaH VVmv023WmgIvxxpHse5HhhbG4KitzHBHo6EyHWT2uDq3qpSM4ZdBYmFP1MbdmQyv8L4c 0X7iTm24OZfDUu/70logkvHzhMzv5nP4xIzGiqbWQYRt7g+4mCM4aw1TtRaWzx28FsmH mi/mzN5tvCyLyJZ8WDqXOfKCqrjBKMkAh0po+6IPkXvA7pZEGsxeX9VNAn8dl13+cI9J 3N6F9sjx79hlmkM71WZnbHwlqIoUQecD5xlTebO9EP4upHfETnMJlEE6OigXQV70gTjP 06Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715735321; x=1716340121; 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=rmIhzIE95fWoUSftNH5w96W/OYKp/ezAOoEvyXF7Z8M=; b=oVT9Auy5+niszFIZ9YiWhK0p5Yh5p+DX/13Lvvi9E2XoDewjDsamQv5b6WuRVrihct XF/lUio1aL4iYcTxbfIwgNyU1LF0HSZ07dVIGkjyMY0WsUelWE+sK8MV/6P1MN17T5zK uC0icQQ05LEzoO405AAEe7jroSjWDIdLZ0YcAidSFqyEaeM/yc5eIc63oSi+OUiOal0E jyZZAE86J9vLuTUfFGd3BquQWGlD4MoLK0vmG9v+AmcqIxtO8YYCVcvIEY/o3olSHnbD kQb/vC8GhcGow6EwpVb++pva9q1GxfIT5SnhivhcF3zYWq1I0p205p6neiv65dCkyx87 e4Ag== X-Gm-Message-State: AOJu0YyY6GMqd5Pe6GwUjyluQ6sTMJqBYetk5nErorcbCx0qvOrmFmcU DBInY7MQKqNmea1/177oJs6B+DTuB+o3cp6b6tIJZwawcYY4nTgqqCX/eg== X-Received: by 2002:a05:6a00:2410:b0:6f3:ea67:48c3 with SMTP id d2e1a72fcca58-6f4e01b9368mr16099577b3a.0.1715735320882; Tue, 14 May 2024 18:08:40 -0700 (PDT) Received: from localhost.localdomain ([190.194.167.233]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-6f4d2a66691sm10135237b3a.16.2024.05.14.18.08.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 May 2024 18:08:40 -0700 (PDT) From: James Almer To: ffmpeg-devel@ffmpeg.org Date: Tue, 14 May 2024 22:08:29 -0300 Message-ID: <20240515010829.2667-2-jamrial@gmail.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240515010829.2667-1-jamrial@gmail.com> References: <20240515010829.2667-1-jamrial@gmail.com> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 2/2] avformat/movenc: add support for writing SA3D boxes X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 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" X-TUID: UwIdh7rZX1jO Signed-off-by: James Almer --- libavformat/movenc.c | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) diff --git a/libavformat/movenc.c b/libavformat/movenc.c index f907f67752..2aec9a8d17 100644 --- a/libavformat/movenc.c +++ b/libavformat/movenc.c @@ -916,6 +916,62 @@ static int mov_write_dmlp_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *tra return update_size(pb, pos); } +static int mov_write_SA3D_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track) +{ + AVChannelLayout ch_layout = { 0 }; + const AVDictionaryEntry *str = av_dict_get(track->st->metadata, "SA3D", NULL, 0); + int64_t pos; + int ambisonic_order, ambi_channels, non_diegetic_channels; + int i, ret; + + if (!str) + return 0; + + ret = av_channel_layout_from_string(&ch_layout, str->value); + if (ret < 0) { + if (ret == AVERROR(EINVAL)) { +invalid: + av_log(s, AV_LOG_ERROR, "Invalid SA3D layout: \"%s\"\n", str->value); + ret = 0; + } + av_channel_layout_uninit(&ch_layout); + return ret; + } + + if (track->st->codecpar->ch_layout.nb_channels != ch_layout.nb_channels) + goto invalid; + + ambisonic_order = av_channel_layout_get_ambisonic_order(&ch_layout); + if (ambisonic_order < 0) + goto invalid; + + ambi_channels = (ambisonic_order + 1LL) * (ambisonic_order + 1LL); + non_diegetic_channels = ch_layout.nb_channels - ambi_channels; + if (non_diegetic_channels && non_diegetic_channels != 2) + goto invalid; + + av_log(s, AV_LOG_VERBOSE, "Inserting SA3D box with layout: \"%s\"\n", str->value); + + pos = avio_tell(pb); + + avio_wb32(pb, 0); // Size + ffio_wfourcc(pb, "SA3D"); + avio_w8(pb, 0); // version + avio_w8(pb, (!!non_diegetic_channels) << 7); // head_locked_stereo and ambisonic_type + avio_wb32(pb, ambisonic_order); // ambisonic_order + avio_w8(pb, 0); // ambisonic_channel_ordering + avio_w8(pb, 0); // ambisonic_normalization + avio_wb32(pb, ch_layout.nb_channels); // num_channels + for (i = 0; i < ambi_channels; i++) + avio_wb32(pb, av_channel_layout_channel_from_index(&ch_layout, i) - AV_CHAN_AMBISONIC_BASE); + for (; i < ch_layout.nb_channels; i++) + avio_wb32(pb, i); + + av_channel_layout_uninit(&ch_layout); + + return update_size(pb, pos); +} + static int mov_write_chan_tag(AVFormatContext *s, AVIOContext *pb, MOVTrack *track) { uint32_t layout_tag, bitmap, *channel_desc; @@ -1419,6 +1475,11 @@ static int mov_write_audio_tag(AVFormatContext *s, AVIOContext *pb, MOVMuxContex if (ret < 0) return ret; + if (track->mode == MODE_MP4 && track->par->codec_type == AVMEDIA_TYPE_AUDIO + && ((ret = mov_write_SA3D_tag(s, pb, track)) < 0)) { + return ret; + } + if (track->mode == MODE_MOV && track->par->codec_type == AVMEDIA_TYPE_AUDIO && ((ret = mov_write_chan_tag(s, pb, track)) < 0)) { return ret;