FFmpeg
Loading...
Searching...
No Matches
rematrix.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2026 Ayoub Nabil Boubagrat
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <limits.h>
22#include <stdio.h>
23
27
28/* swr_build_matrix2() accesses an internal SWR_CH_MAX by SWR_CH_MAX matrix. */
29#define MATRIX_STRIDE 64
30
35
36static void print_matrix_row(const double *matrix,
37 const AVChannelLayout *in_layout,
38 int out, const char *out_name)
39{
40 char in_name[16];
41
42 printf("[%s] = { ", out_name);
43 for (int i = 0; i < 64; i++) {
44 enum AVChannel in_ch = av_channel_layout_channel_from_index(in_layout, i);
45 if (in_ch == AV_CHAN_NONE)
46 continue;
47 av_channel_name(in_name, sizeof(in_name), in_ch);
48 if (in_ch == AV_CHAN_UNUSED)
49 printf(".UNSD%d = %f, ", i,
51 else
52 printf(".%s = %f, ", in_name, matrix[out * MATRIX_STRIDE + i]);
53 }
54 printf("},\n");
55}
56
57static int print_matrix(const AVChannelLayout *in_layout,
58 const AVChannelLayout *out_layout)
59{
60 double matrix[MATRIX_STRIDE * MATRIX_STRIDE] = { 0 };
61 char out_name[16];
62 int ret;
63
64 /* ensure swr_build_matrix2() overwrites unused columns and rows with zero. */
65 for (int out = 0; out < out_layout->nb_channels; out++)
66 for (int in = 0; in < in_layout->nb_channels; in++)
67 if (channel_is_unused(in_layout, in) ||
68 channel_is_unused(out_layout, out))
69 matrix[out * MATRIX_STRIDE + in] = 1.0;
70
71 /* Disable normalization so the raw downmix gains can be checked. */
72 ret = swr_build_matrix2(in_layout, out_layout, M_SQRT1_2,
74 0.0, INT_MAX, 1.0, matrix, MATRIX_STRIDE,
76 if (ret < 0) {
77 fprintf(stderr, "swr_build_matrix2 failed with error %d\n", ret);
78 return 1;
79 }
80
81 for (int out = 0; out < out_layout->nb_channels; out++) {
82 for (int in = 0; in < in_layout->nb_channels; in++) {
83 if ((channel_is_unused(in_layout, in) ||
84 channel_is_unused(out_layout, out)) &&
85 matrix[out * MATRIX_STRIDE + in] != 0.0) {
86 fprintf(stderr, "unused matrix position %d:%d is non-zero\n",
87 out, in);
88 return 1;
89 }
90 }
91 }
92
93 for (int i = 0; i < out_layout->nb_channels; i++) {
94 enum AVChannel out_ch = av_channel_layout_channel_from_index(out_layout, i);
95 if (out_ch == AV_CHAN_NONE)
96 continue;
97 if (out_ch == AV_CHAN_UNUSED)
98 snprintf(out_name, sizeof(out_name), "UNSD%d", i);
99 else
100 av_channel_name(out_name, sizeof(out_name), out_ch);
101 print_matrix_row(matrix, in_layout, i, out_name);
102 }
103
104 return 0;
105}
106
107int main(int argc, char **argv)
108{
109 AVChannelLayout in_layout = { 0 }, out_layout = { 0 };
110 const char *in, *out;
111 int ret = 0;
112
113 if (argc != 3) {
114 printf("usage: rematrix input_layout output_layout\n");
115 return 0;
116 }
117
118 in = argv[1];
119 out = argv[2];
120
121 ret = av_channel_layout_from_string(&in_layout, in);
122 if (ret < 0) {
123 if (ret == AVERROR(EINVAL))
124 fprintf(stderr, "Invalid input layout %s\n", in);
125 ret = 1;
126 goto end;
127 }
128
129 ret = av_channel_layout_from_string(&out_layout, out);
130 if (ret < 0) {
131 if (ret == AVERROR(EINVAL))
132 fprintf(stderr, "Invalid output layout %s\n", out);
133 ret = 1;
134 goto end;
135 }
136
137 if (in_layout.nb_channels > MATRIX_STRIDE ||
138 out_layout.nb_channels > MATRIX_STRIDE) {
139 fprintf(stderr, "channel layout exceeds matrix capacity\n");
140 ret = 1;
141 goto end;
142 }
143
144 ret = print_matrix(&in_layout, &out_layout);
145
146end:
147 av_channel_layout_uninit(&in_layout);
148 av_channel_layout_uninit(&out_layout);
149
150 return ret;
151}
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
Public libavutil channel layout APIs header.
#define NULL
Definition coverity.c:32
__device__ int printf(const char *,...)
int main
Definition dovi_rpuenc.c:38
@ AV_MATRIX_ENCODING_NONE
int av_channel_layout_from_string(AVChannelLayout *channel_layout, const char *str)
Initialize a channel layout from a given string description.
int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id)
Get a human readable string in an abbreviated form describing a given channel.
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVChannel
@ AV_CHAN_UNUSED
Channel is empty can be safely skipped.
@ AV_CHAN_NONE
Invalid channel index.
#define AVERROR(e)
Definition error.h:45
av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout, double center_mix_level, double surround_mix_level, double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param, ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
Generate a channel mixing matrix.
Definition rematrix.c:572
int index
Definition gxfenc.c:90
#define M_SQRT1_2
uint64_t layout
#define snprintf
Definition snprintf.h:34
An AVChannelLayout holds information about the channel layout of audio data.
int nb_channels
Number of channels in this layout.
libswresample public header
static FILE * out
Definition movenc.c:55
static int channel_is_unused(const AVChannelLayout *layout, int index)
Definition rematrix.c:31
#define MATRIX_STRIDE
Definition rematrix.c:29
static int print_matrix(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout)
Definition rematrix.c:57
static void print_matrix_row(const double *matrix, const AVChannelLayout *in_layout, int out, const char *out_name)
Definition rematrix.c:36