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

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

Unified · Split

libavfilter/formats.c+24−61
@@ -33,11 +33,17 @@ · 32 unmodified lines
33 33
34/**34/**
35 * Add all refs from a to ret and destroy a.35 * Add all refs from a to ret and destroy a.
36 * ret->refs must have enough spare room left for this.
37 */36 */
38#define MERGE_REF_NO_ALLOC(ret, a, fmts) \37#define MERGE_REF(ret, a, fmts, type, fail_statement) \
39do { \38do { \
39 type ***tmp; \
40 int i; \40 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 \
41 for (i = 0; i < a->refcount; i ++) { \47 for (i = 0; i < a->refcount; i ++) { \
42 ret->refs[ret->refcount] = a->refs[i]; \48 ret->refs[ret->refcount] = a->refs[i]; \
43 *ret->refs[ret->refcount++] = ret; \49 *ret->refs[ret->refcount++] = ret; \
@@ -48,57 +54,31 @@ do { \ · 4 unmodified lines
48 av_freep(&a); \54 av_freep(&a); \
49} while (0)55} while (0)
50 56
51#define MERGE_REF(ret, a, fmts, type, fail_statement) \
52do { \
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
62/**57/**
63 * Add all formats common for a and b to ret, copy the refs and destroy58 * Add all formats common to a and b to a, add b's refs to a and destroy b.
64 * a and b.
65 */59 */
66#define MERGE_FORMATS(ret, a, b, fmts, nb, type, fail) \60#define MERGE_FORMATS(a, b, fmts, nb, type, fail_statement) \
67do { \61do { \
68 int i, j, k = 0, count = a->nb; \62 int i, j, k = 0; \
69 type ***tmp; \
70 \
71 if (!(ret = av_mallocz(sizeof(*ret)))) \
72 goto fail; \
73 \63 \
74 if (count) { \
75 if (!(ret->fmts = av_malloc_array(count, sizeof(*ret->fmts)))) \
76 goto fail; \
77 for (i = 0; i < a->nb; i++) \64 for (i = 0; i < a->nb; i++) \
78 for (j = 0; j < b->nb; j++) \65 for (j = 0; j < b->nb; j++) \
79 if (a->fmts[i] == b->fmts[j]) { \66 if (a->fmts[i] == b->fmts[j]) { \
80 ret->fmts[k++] = a->fmts[i]; \67 a->fmts[k++] = a->fmts[i]; \
81 break; \68 break; \
82 } \69 } \
83 } \70 /* Check that there was at least one common format. \
84 ret->nb = k; \71 * Notice that both a and b are unchanged if not. */ \
85 /* check that there was at least one common format */ \72 if (!k) \
86 if (!ret->nb) \73 { fail_statement } \
87 goto fail; \74 a->nb = k; \
88 \
89 tmp = av_realloc_array(NULL, a->refcount + b->refcount, sizeof(*tmp)); \
90 if (!tmp) \
91 goto fail; \
92 ret->refs = tmp; \
93 \75 \
94 MERGE_REF_NO_ALLOC(ret, a, fmts); \76 MERGE_REF(a, b, fmts, type, fail_statement); \
95 MERGE_REF_NO_ALLOC(ret, b, fmts); \
96} while (0)77} while (0)
97 78
98AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,79AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b,
99 enum AVMediaType type)80 enum AVMediaType type)
100{81{
101 AVFilterFormats *ret = NULL;
102 int i, j;82 int i, j;
103 int alpha1=0, alpha2=0;83 int alpha1=0, alpha2=0;
104 int chroma1=0, chroma2=0;84 int chroma1=0, chroma2=0;
@@ -130,43 +110,26 @@ AVFilterFormats *ff_merge_formats(AVFilterFormats *a, AVFilterFormats *b, · 25 unmodified lines
130 if (alpha2 > alpha1 || chroma2 > chroma1)110 if (alpha2 > alpha1 || chroma2 > chroma1)
131 return NULL;111 return NULL;
132 112
133 MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail);113 MERGE_FORMATS(a, b, formats, nb_formats, AVFilterFormats, return NULL;);
134 114
135 return ret;115 return a;
136fail:
137 if (ret) {
138 av_assert1(!ret->refs);
139 av_freep(&ret->formats);
140 av_freep(&ret);
141 }
142 return NULL;
143}116}
144 117
145AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,118AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a,
146 AVFilterFormats *b)119 AVFilterFormats *b)
147{120{
148 AVFilterFormats *ret = NULL;
149
150 if (a == b) return a;121 if (a == b) return a;
151 122
152 if (a->nb_formats && b->nb_formats) {123 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;
154 } else if (a->nb_formats) {126 } else if (a->nb_formats) {
155 MERGE_REF(a, b, formats, AVFilterFormats, return NULL;);127 MERGE_REF(a, b, formats, AVFilterFormats, return NULL;);
156 ret = a;128 return a;
157 } else {129 } else {
158 MERGE_REF(b, a, formats, AVFilterFormats, return NULL;);130 MERGE_REF(b, a, formats, AVFilterFormats, return NULL;);
159 ret = b;131 return b;
160 }
161
162 return ret;
163fail:
164 if (ret) {
165 av_assert1(!ret->refs);
166 av_freep(&ret->formats);
167 av_freep(&ret);
168 }132 }
169 return NULL;
170}133}
171 134
172AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a,135AVFilterChannelLayouts *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