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