History

avfilter/af_headphone: Simplify parsing channel mapping string

When parsing the channel mapping string (a string containing '|' delimited tokens each of which is supposed to contain a channel name like "FR"), the old code would at each step read up to seven uppercase characters from the input string and give this to av_get_channel_layout() to parse. The returned layout is then checked for being a layout with a single channel set by computing its logarithm. Besides being overtly complicated this also has the drawback of relying on the assumption that every channel name consists of at most seven uppercase letters only; but said assumption is wrong: The abbreviation of the second low frequency channel is LFE2. Furthermore it treats garbage like "FRfoo" as valid channel. This commit changes this by using av_get_channel_layout() directly; furthermore, av_get_channel_layout_nb_channels() (which uses popcount) is used to find out the number of channels instead of the custom code to calculate the logarithm. (As a consequence, certain other formats to specify the channel layouts are now accepted (like the hex versions of av_get_channel_layout()); but this is actually not bad at all.)

Andreas Rheinhardt committed 6 years ago

Commit 71daaaf1 parent bc533ba1 file · +9 −22Browse files

Unified · Split

libavfilter/af_headphone.c+9−22
@@ -87,26 +87,14 @@ typedef struct HeadphoneContext { · 86 unmodified lines
87 uint64_t mapping[64];87 uint64_t mapping[64];
88} HeadphoneContext;88} HeadphoneContext;
89 89
90static int parse_channel_name(char **arg, uint64_t *rchannel, char *buf)90static int parse_channel_name(const char *arg, uint64_t *rchannel)
91{91{
92 int len, i, channel_id = 0;92 uint64_t layout = av_get_channel_layout(arg);
93 uint64_t layout, layout0;93
94 94 if (av_get_channel_layout_nb_channels(layout) != 1)
95 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {95 return AVERROR(EINVAL);
96 layout0 = layout = av_get_channel_layout(buf);96 *rchannel = layout;
97 for (i = 32; i > 0; i >>= 1) {97 return 0;
98 if (layout >= 1LL << i) {
99 channel_id += i;
100 layout >>= i;
101 }
102 }
103 if (channel_id >= 64 || layout0 != 1ULL << channel_id)
104 return AVERROR(EINVAL);
105 *rchannel = layout0;
106 *arg += len;
107 return 0;
108 }
109 return AVERROR(EINVAL);
110}98}
111 99
112static void parse_map(AVFilterContext *ctx)100static void parse_map(AVFilterContext *ctx)
@@ -124,15 +112,14 @@ static void parse_map(AVFilterContext *ctx) · 11 unmodified lines
124 112
125 while ((arg = av_strtok(p, "|", &tokenizer))) {113 while ((arg = av_strtok(p, "|", &tokenizer))) {
126 uint64_t out_channel;114 uint64_t out_channel;
127 char buf[8];
128 115
129 p = NULL;116 p = NULL;
130 if (parse_channel_name(&arg, &out_channel, buf)) {117 if (parse_channel_name(arg, &out_channel)) {
131 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", arg);118 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", arg);
132 continue;119 continue;
133 }120 }
134 if (used_channels & out_channel) {121 if (used_channels & out_channel) {
135 av_log(ctx, AV_LOG_WARNING, "Ignoring duplicate channel '%s'.\n", buf);122 av_log(ctx, AV_LOG_WARNING, "Ignoring duplicate channel '%s'.\n", arg);
136 continue;123 continue;
137 }124 }
138 used_channels |= out_channel;125 used_channels |= out_channel;

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t