History

avfilter/formats: Avoid allocations when merging channel layouts

When one merges two AVFilterChannelLayouts structs, there is no need to allocate a new one. Instead one can reuse one of the two given ones. If one does this, one also doesn't need to update the references of the AVFilterChannelLayouts that is reused. Therefore this commit reuses the structure with the higher refcount.

Andreas Rheinhardt committed 6 years ago

Commit cfc65521 parent 4147f631 file · +21 −27Browse files

Unified · Split

libavfilter/formats.c+21−27
@@ -48,13 +48,13 @@ do { \ · 47 unmodified lines
4848 av_freep(&a); \
4949 } while (0)
5050
51−#define MERGE_REF(ret, a, fmts, type, fail) \
51+#define MERGE_REF(ret, a, fmts, type, fail_statement) \
5252 do { \
5353 type ***tmp; \
5454 \
5555 if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
5656 sizeof(*tmp)))) \
57− goto fail; \
57+ { fail_statement } \
5858 ret->refs = tmp; \
5959 MERGE_REF_NO_ALLOC(ret, a, fmts); \
6060 } while (0)
@@ -157,10 +157,10 @@ AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a, · 96 unmodified lines
157157 if (a->nb_formats && b->nb_formats) {
158158 MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
159159 } else if (a->nb_formats) {
160− MERGE_REF(a, b, formats, AVFilterFormats, fail);
160+ MERGE_REF(a, b, formats, AVFilterFormats, return NULL;);
161161 ret = a;
162162 } else {
163− MERGE_REF(b, a, formats, AVFilterFormats, fail);
163+ MERGE_REF(b, a, formats, AVFilterFormats, return NULL;);
164164 ret = b;
165165 }
166166
@@ -177,7 +177,7 @@ fail: · 10 unmodified lines
177177 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,
178178 AVFilterChannelLayouts *b)
179179 {
180− AVFilterChannelLayouts *ret = NULL;
180+ uint64_t *channel_layouts;
181181 unsigned a_all = a->all_layouts + a->all_counts;
182182 unsigned b_all = b->all_layouts + b->all_counts;
183183 int ret_max, ret_nb = 0, i, j, round;
@@ -201,15 +201,13 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 17 unmodified lines
201201 return NULL;
202202 b->nb_channel_layouts = j;
203203 }
204− MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail);
204+ MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, return NULL;);
205205 return b;
206206 }
207207
208208 ret_max = a->nb_channel_layouts + b->nb_channel_layouts;
209− if (!(ret = av_mallocz(sizeof(*ret))) ||
210− !(ret->channel_layouts = av_malloc_array(ret_max,
211− sizeof(*ret->channel_layouts))))
212− goto fail;
209+ if (!(channel_layouts = av_malloc_array(ret_max, sizeof(*channel_layouts))))
210+ return NULL;
213211
214212 /* a[known] intersect b[known] */
215213 for (i = 0; i < a->nb_channel_layouts; i++) {
@@ -217,7 +215,7 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 1 unmodified line
217215 continue;
218216 for (j = 0; j < b->nb_channel_layouts; j++) {
219217 if (a->channel_layouts[i] == b->channel_layouts[j]) {
220− ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
218+ channel_layouts[ret_nb++] = a->channel_layouts[i];
221219 a->channel_layouts[i] = b->channel_layouts[j] = 0;
222220 break;
223221 }
@@ -233,7 +231,7 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 9 unmodified lines
233231 bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt));
234232 for (j = 0; j < b->nb_channel_layouts; j++)
235233 if (b->channel_layouts[j] == bfmt)
236− ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
234+ channel_layouts[ret_nb++] = a->channel_layouts[i];
237235 }
238236 /* 1st round: swap to prepare 2nd round; 2nd round: put it back */
239237 FFSWAP(AVFilterChannelLayouts *, a, b);
@@ -244,27 +242,23 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 4 unmodified lines
244242 continue;
245243 for (j = 0; j < b->nb_channel_layouts; j++)
246244 if (a->channel_layouts[i] == b->channel_layouts[j])
247− ret->channel_layouts[ret_nb++] = a->channel_layouts[i];
245+ channel_layouts[ret_nb++] = a->channel_layouts[i];
248246 }
249247
250− ret->nb_channel_layouts = ret_nb;
251− if (!ret->nb_channel_layouts)
248+ if (!ret_nb)
252249 goto fail;
253250
254− ret->refs = av_realloc_array(NULL, a->refcount + b->refcount,
255− sizeof(*ret->refs));
256− if (!ret->refs)
257− goto fail;
258− MERGE_REF_NO_ALLOC(ret, a, channel_layouts);
259− MERGE_REF_NO_ALLOC(ret, b, channel_layouts);
260− return ret;
251+ if (a->refcount > b->refcount)
252+ FFSWAP(AVFilterChannelLayouts *, a, b);
253+
254+ MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, goto fail;);
255+ av_freep(&b->channel_layouts);
256+ b->channel_layouts = channel_layouts;
257+ b->nb_channel_layouts = ret_nb;
258+ return b;
261259
262260 fail:
263− if (ret) {
264− av_assert1(!ret->refs);
265− av_freep(&ret->channel_layouts);
266− av_freep(&ret);
267− }
261+ av_free(channel_layouts);
268262 return NULL;
269263 }
270264

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