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.
Unified · Split
| @@ -48,13 +48,13 @@ do { \ · 47 unmodified lines | ||
| 48 | 48 | av_freep(&a); \ |
| 49 | 49 | } while (0) |
| 50 | 50 | |
| 51 | −#define MERGE_REF(ret, a, fmts, type, fail) \ | |
| 51 | +#define MERGE_REF(ret, a, fmts, type, fail_statement) \ | |
| 52 | 52 | do { \ |
| 53 | 53 | type ***tmp; \ |
| 54 | 54 | \ |
| 55 | 55 | if (!(tmp = av_realloc_array(ret->refs, ret->refcount + a->refcount, \ |
| 56 | 56 | sizeof(*tmp)))) \ |
| 57 | − goto fail; \ | |
| 57 | + { fail_statement } \ | |
| 58 | 58 | ret->refs = tmp; \ |
| 59 | 59 | MERGE_REF_NO_ALLOC(ret, a, fmts); \ |
| 60 | 60 | } while (0) |
| @@ -157,10 +157,10 @@ AVFilterFormats *ff_merge_samplerates(AVFilterFormats *a, · 96 unmodified lines | ||
| 157 | 157 | if (a->nb_formats && b->nb_formats) { |
| 158 | 158 | MERGE_FORMATS(ret, a, b, formats, nb_formats, AVFilterFormats, fail); |
| 159 | 159 | } else if (a->nb_formats) { |
| 160 | − MERGE_REF(a, b, formats, AVFilterFormats, fail); | |
| 160 | + MERGE_REF(a, b, formats, AVFilterFormats, return NULL;); | |
| 161 | 161 | ret = a; |
| 162 | 162 | } else { |
| 163 | − MERGE_REF(b, a, formats, AVFilterFormats, fail); | |
| 163 | + MERGE_REF(b, a, formats, AVFilterFormats, return NULL;); | |
| 164 | 164 | ret = b; |
| 165 | 165 | } |
| 166 | 166 | |
| @@ -177,7 +177,7 @@ fail: · 10 unmodified lines | ||
| 177 | 177 | AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, |
| 178 | 178 | AVFilterChannelLayouts *b) |
| 179 | 179 | { |
| 180 | − AVFilterChannelLayouts *ret = NULL; | |
| 180 | + uint64_t *channel_layouts; | |
| 181 | 181 | unsigned a_all = a->all_layouts + a->all_counts; |
| 182 | 182 | unsigned b_all = b->all_layouts + b->all_counts; |
| 183 | 183 | int ret_max, ret_nb = 0, i, j, round; |
| @@ -201,15 +201,13 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 17 unmodified lines | ||
| 201 | 201 | return NULL; |
| 202 | 202 | b->nb_channel_layouts = j; |
| 203 | 203 | } |
| 204 | − MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, fail); | |
| 204 | + MERGE_REF(b, a, channel_layouts, AVFilterChannelLayouts, return NULL;); | |
| 205 | 205 | return b; |
| 206 | 206 | } |
| 207 | 207 | |
| 208 | 208 | 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; | |
| 213 | 211 | |
| 214 | 212 | /* a[known] intersect b[known] */ |
| 215 | 213 | for (i = 0; i < a->nb_channel_layouts; i++) { |
| @@ -217,7 +215,7 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 1 unmodified line | ||
| 217 | 215 | continue; |
| 218 | 216 | for (j = 0; j < b->nb_channel_layouts; j++) { |
| 219 | 217 | 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]; | |
| 221 | 219 | a->channel_layouts[i] = b->channel_layouts[j] = 0; |
| 222 | 220 | break; |
| 223 | 221 | } |
| @@ -233,7 +231,7 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 9 unmodified lines | ||
| 233 | 231 | bfmt = FF_COUNT2LAYOUT(av_get_channel_layout_nb_channels(fmt)); |
| 234 | 232 | for (j = 0; j < b->nb_channel_layouts; j++) |
| 235 | 233 | 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]; | |
| 237 | 235 | } |
| 238 | 236 | /* 1st round: swap to prepare 2nd round; 2nd round: put it back */ |
| 239 | 237 | FFSWAP(AVFilterChannelLayouts *, a, b); |
| @@ -244,27 +242,23 @@ AVFilterChannelLayouts *ff_merge_channel_layouts(AVFilterChannelLayouts *a, · 4 unmodified lines | ||
| 244 | 242 | continue; |
| 245 | 243 | for (j = 0; j < b->nb_channel_layouts; j++) |
| 246 | 244 | 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]; | |
| 248 | 246 | } |
| 249 | 247 | |
| 250 | − ret->nb_channel_layouts = ret_nb; | |
| 251 | − if (!ret->nb_channel_layouts) | |
| 248 | + if (!ret_nb) | |
| 252 | 249 | goto fail; |
| 253 | 250 | |
| 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; | |
| 261 | 259 | |
| 262 | 260 | 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); | |
| 268 | 262 | return NULL; |
| 269 | 263 | } |
| 270 | 264 | |