History

avfilter/formats: Avoid allocations when merging formats and samplerates

This is the analogue of cfc65520324ae640299bd321ef88ae76dcee6f78 for formats and samplerates; in contrast to said commit, one can avoid allocating a new array for formats as well (the complications of the generic channel layouts made this impossible for channel layouts). This commit also starts to move the line continuation '\' chars to the left to keep them in line with MERGE_REF() as well as with the 80 lines limit.

Andreas Rheinhardt committed 6 years ago

Commit c0aa3df1 parent 55eb24a1 file · +24 −61Browse files

Unified · Split

libavfilter/formats.c+24−61
@@ -33,11 +33,17 @@ · 32 unmodified lines
3333
3434 /**
3535 * Add all refs from a to ret and destroy a.
36− * ret->refs must have enough spare room left for this.
3736 */
38−#define MERGE_REF_NO_ALLOC(ret, a, fmts) \
37+#define MERGE_REF(ret, a, fmts, type, fail_statement) \
3938 do { \
39+ type ***tmp; \
4040 int i; \
41+ \
42+ if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
43+ sizeof(*tmp)))) \
44+ { fail_statement } \
45+ ret->refs = tmp; \
46+ \
4147 for (i = 0; i < a->refcount; i ++) { \
4248 ret->refs[ret->refcount] = a->refs[i]; \
4349 *ret->refs[ret->refcount++] = ret; \
@@ -48,57 +54,31 @@ do { \ · 4 unmodified lines
4854 av_freep(&a); \
4955 } while (0)
5056
51−#define MERGE_REF(ret, a, fmts, type, fail_statement) \
52−do { \
53− type ***tmp; \
54− \
55− if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \
56− sizeof(*tmp)))) \
57− { fail_statement } \
58− ret->refs = tmp; \
59− MERGE_REF_NO_ALLOC(ret, a, fmts); \
60−} while (0)
61−
6257 /**
63− * Add all formats common for a and b to ret, copy the refs and destroy
64− * a and b.
58+ * Add all formats common to a and b to a, add b's refs to a and destroy b.
6559 */
66−#define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \
60+#define MERGE_FORMATS(a, b, fmts, nb, type, fail_statement) \
6761 do { \
68− int i, j, k = 0, count = a->nb; \
69− type ***tmp; \
70− \
71− if (!(ret = av_mallocz(sizeof(*ret)))) \
72− goto fail; \
62+ int i, j, k = 0; \
7363 \
74− if (count) { \
75− if (!(ret->fmts = av_malloc_array(count, sizeof(*ret->fmts)))) \
76− goto fail; \
7764 for (i = 0; i < a->nb; i++) \
7865 for (j = 0; j < b->nb; j++) \
7966 if (a->fmts[i] == b->fmts[j]) { \
80− ret->fmts[k++] = a->fmts[i]; \
67+ a->fmts[k++] = a->fmts[i]; \
8168 break; \
8269 } \
83− } \
84− ret->nb = k; \
85− /* check that there was at least one common format */ \
86− if (!ret->nb) \
87− goto fail; \
88− \
89− tmp = av_realloc_array(NULL, a->refcount + b->refcount, sizeof(*tmp)); \
90− if (!tmp) \
91− goto fail; \
92− ret->refs = tmp; \
70+ /* Check that there was at least one common format. \
71+ * Notice that both a and b are unchanged if not. */ \
72+ if (!k) \
73+ { fail_statement } \
74+ a->nb = k; \
9375 \
94− MERGE_REF_NO_ALLOC(ret, a, fmts); \
95− MERGE_REF_NO_ALLOC(ret, b, fmts); \
76+ MERGE_REF(a, b, fmts, type, fail_statement); \
9677 } while (0)
9778
9879 AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
9980 enum AVMediaType type)
10081 {
101− AVFilterFormats *ret = NULL;
10282 int i, j;
10383 int alpha1=0, alpha2=0;
10484 int chroma1=0, chroma2=0;
@@ -130,43 +110,26 @@ AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b, · 25 unmodified lines
130110 if (alpha2 > alpha1 || chroma2 > chroma1)
131111 return NULL;
132112
133− MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
113+ MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;);
134114
135− return ret;
136−fail:
137− if (ret) {
138− av_assert1(!ret->refs);
139− av_freep(&ret->formats);
140− av_freep(&ret);
141− }
142− return NULL;
115+ return a;
143116 }
144117
145118 AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
146119 AVFilterFormats *b)
147120 {
148− AVFilterFormats *ret = NULL;
149−
150121 if (a == b) return a;
151122
152123 if (a->nb_formats && b->nb_formats) {
153− MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);
124+ MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;);
125+ return a;
154126 } else if (a->nb_formats) {
155127 MERGE_REF(a, b, formats, AVFilterFormats, return NULL;);
156− ret = a;
128+ return a;
157129 } else {
158130 MERGE_REF(b, a, formats, AVFilterFormats, return NULL;);
159− ret = b;
160− }
161−
162− return ret;
163−fail:
164− if (ret) {
165− av_assert1(!ret->refs);
166− av_freep(&ret->formats);
167− av_freep(&ret);
131+ return b;
168132 }
169− return NULL;
170133 }
171134
172135 AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,

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