History

avfilter/af_headphone: Use uint64_t for channel mapping

The headphone filter has an option for the user to specify an assignment of inputs to channels (or from pairs of channels of the second input to channels). Up until now, these channels were stored in an int containing the logarithm of the channel layout. Yet it is not the logarithm that is used lateron and so a retransformation was necessary. Therefore this commit simply stores the uint64_t as is, avoiding the retransformation. This also has the advantage that unset channels (whose corresponding entry is zero) can't be mistaken for valid channels any more; the old code had to initialize the channels to -1 to solve this problem and had to check for whether a channel is set before the retransformation (because 1 << -1 is UB). The only downside of this approach is that the size of the context increases (by 256 bytes); but this is not exceedingly much. Finally, the array has been moved to the end of the context; it is only used a few times during the initialization process and moving it decreased the offsets of lots of other entries, reducing codesize.

Andreas Rheinhardt committed 6 years ago

Commit bc533ba1 parent 5e687271 file · +13 −27Browse files

Showing the changes to libavfilter (1 of 1 file). Show all

Unified · Split

libavfilter/af_headphone.c+13−27
@@ -52,8 +52,6 @@ typedef struct HeadphoneContext { · 51 unmodified lines
52 int ir_len;52 int ir_len;
53 int air_len;53 int air_len;
54 54
55 int mapping[64];
56
57 int nb_inputs;55 int nb_inputs;
58 56
59 int nb_irs;57 int nb_irs;
@@ -86,12 +84,13 @@ typedef struct HeadphoneContext { · 26 unmodified lines
86 int delay_r;84 int delay_r;
87 int eof;85 int eof;
88 } *in;86 } *in;
87 uint64_t mapping[64];
89} HeadphoneContext;88} HeadphoneContext;
90 89
91static int parse_channel_name(char **arg, int *rchannel, char *buf)90static int parse_channel_name(char **arg, uint64_t *rchannel, char *buf)
92{91{
93 int len, i, channel_id = 0;92 int len, i, channel_id = 0;
94 int64_t layout, layout0;93 uint64_t layout, layout0;
95 94
96 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {95 if (sscanf(*arg, "%7[A-Z]%n", buf, &len)) {
97 layout0 = layout = av_get_channel_layout(buf);96 layout0 = layout = av_get_channel_layout(buf);
@@ -101,9 +100,9 @@ static int parse_channel_name(char **arg, int *rchannel, char *buf) · 3 unmodified lines
101 layout >>= i;100 layout >>= i;
102 }101 }
103 }102 }
104 if (channel_id >= 64 || layout0 != 1LL << channel_id)103 if (channel_id >= 64 || layout0 != 1ULL << channel_id)
105 return AVERROR(EINVAL);104 return AVERROR(EINVAL);
106 *rchannel = channel_id;105 *rchannel = layout0;
107 *arg += len;106 *arg += len;
108 return 0;107 return 0;
109 }108 }
@@ -115,7 +114,6 @@ static void parse_map(AVFilterContext *ctx) · 5 unmodified lines
115 HeadphoneContext *s = ctx->priv;114 HeadphoneContext *s = ctx->priv;
116 char *arg, *tokenizer, *p, *args = av_strdup(s->map);115 char *arg, *tokenizer, *p, *args = av_strdup(s->map);
117 uint64_t used_channels = 0;116 uint64_t used_channels = 0;
118 int i;
119 117
120 if (!args)118 if (!args)
121 return;119 return;
@@ -124,27 +122,23 @@ static void parse_map(AVFilterContext *ctx) · 2 unmodified lines
124 s->lfe_channel = -1;122 s->lfe_channel = -1;
125 s->nb_inputs = 1;123 s->nb_inputs = 1;
126 124
127 for (i = 0; i < 64; i++) {
128 s->mapping[i] = -1;
129 }
130
131 while ((arg = av_strtok(p, "|", &tokenizer))) {125 while ((arg = av_strtok(p, "|", &tokenizer))) {
132 int out_ch_id;126 uint64_t out_channel;
133 char buf[8];127 char buf[8];
134 128
135 p = NULL;129 p = NULL;
136 if (parse_channel_name(&arg, &out_ch_id, buf)) {130 if (parse_channel_name(&arg, &out_channel, buf)) {
137 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", arg);131 av_log(ctx, AV_LOG_WARNING, "Failed to parse \'%s\' as channel name.\n", arg);
138 continue;132 continue;
139 }133 }
140 if (used_channels & (1ULL << out_ch_id)) {134 if (used_channels & out_channel) {
141 av_log(ctx, AV_LOG_WARNING, "Ignoring duplicate channel '%s'.\n", buf);135 av_log(ctx, AV_LOG_WARNING, "Ignoring duplicate channel '%s'.\n", buf);
142 continue;136 continue;
143 }137 }
144 used_channels |= 1ULL << out_ch_id;138 used_channels |= out_channel;
145 if (out_ch_id == av_log2(AV_CH_LOW_FREQUENCY))139 if (out_channel == AV_CH_LOW_FREQUENCY)
146 s->lfe_channel = s->nb_irs;140 s->lfe_channel = s->nb_irs;
147 s->mapping[s->nb_irs] = out_ch_id;141 s->mapping[s->nb_irs] = out_channel;
148 s->nb_irs++;142 s->nb_irs++;
149 }143 }
150 144
@@ -499,11 +493,7 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 348 unmodified lines
499 int idx = -1;493 int idx = -1;
500 494
501 for (j = 0; j < inlink->channels; j++) {495 for (j = 0; j < inlink->channels; j++) {
502 if (s->mapping[i] < 0) {496 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == s->mapping[i]) {
503 continue;
504 }
505
506 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == (1LL << s->mapping[i])) {
507 idx = i;497 idx = i;
508 break;498 break;
509 }499 }
@@ -541,11 +531,7 @@ static int convert_coeffs(AVFilterContext *ctx, AVFilterLink *inlink) · 31 unmodified lines
541 int idx = -1;531 int idx = -1;
542 532
543 for (j = 0; j < inlink->channels; j++) {533 for (j = 0; j < inlink->channels; j++) {
544 if (s->mapping[k] < 0) {534 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == s->mapping[k]) {
545 continue;
546 }
547
548 if ((av_channel_layout_extract_channel(inlink->channel_layout, j)) == (1LL << s->mapping[k])) {
549 idx = k;535 idx = k;
550 break;536 break;
551 }537 }

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