Skip to content

Commit b4ccbed

Browse files
committed
Optimize AssetLocator allocation
Rev Patchset to 7
1 parent 1155157 commit b4ccbed

File tree

3 files changed

+32
-48
lines changed

3 files changed

+32
-48
lines changed

imf-prepare-patches.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
set -e
44

5-
PATCH_VERSION="6"
5+
PATCH_VERSION="7"
66

77
PATCH_NAME="avformat/imf"
88

@@ -82,7 +82,8 @@ CHANGE NOTES:
8282
- fixed rational initialization
8383
- removed extraneous call to xmlCleanupParser()
8484
- fix if/for single line braces
85-
- replace av_realloc_f with av_fast_realloc when allocating CPL Resources
85+
- replace av_realloc_f with av_fast_realloc when allocating CPL Resources and
86+
ASSETMAP assets
8687
"
8788

8889
# add tests back to the Makefile

libavformat/imfdec.c

Lines changed: 20 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ typedef struct IMFAssetLocator {
7474
*/
7575
typedef struct IMFAssetLocatorMap {
7676
uint8_t asset_count;
77-
IMFAssetLocator **assets;
77+
IMFAssetLocator *assets;
7878
} IMFAssetLocatorMap;
7979

8080
typedef struct IMFVirtualTrackResourcePlaybackCtx {
@@ -184,34 +184,36 @@ static int parse_imf_asset_map_from_xml_dom(AVFormatContext *s,
184184
av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing AssetList node\n");
185185
return AVERROR_INVALIDDATA;
186186
}
187-
187+
asset_map->assets = av_realloc_f(asset_map->assets,
188+
xmlChildElementCount(node),
189+
sizeof(IMFAssetLocator));
190+
if (!asset_map->assets) {
191+
av_log(NULL, AV_LOG_PANIC, "Cannot allocate IMF asset locators\n");
192+
return AVERROR(ENOMEM);
193+
}
194+
asset_map->asset_count = 0;
188195
node = xmlFirstElementChild(node);
189196
while (node) {
190197
if (av_strcasecmp(node->name, "Asset") != 0)
191198
continue;
192199

193-
asset = av_malloc(sizeof(IMFAssetLocator));
194-
if (!asset)
195-
return AVERROR(ENOMEM);
200+
asset = &(asset_map->assets[asset_map->asset_count]);
196201

197202
if (ff_xml_read_UUID(ff_xml_get_child_element_by_name(node, "Id"), asset->uuid)) {
198203
av_log(s, AV_LOG_ERROR, "Could not parse UUID from asset in asset map.\n");
199-
ret = AVERROR_INVALIDDATA;
200-
goto clean_up_asset;
204+
return AVERROR_INVALIDDATA;
201205
}
202206

203207
av_log(s, AV_LOG_DEBUG, "Found asset id: " FF_UUID_FORMAT "\n", UID_ARG(asset->uuid));
204208

205209
if (!(node = ff_xml_get_child_element_by_name(node, "ChunkList"))) {
206210
av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing ChunkList node\n");
207-
ret = AVERROR_INVALIDDATA;
208-
goto clean_up_asset;
211+
return AVERROR_INVALIDDATA;
209212
}
210213

211214
if (!(node = ff_xml_get_child_element_by_name(node, "Chunk"))) {
212215
av_log(s, AV_LOG_ERROR, "Unable to parse asset map XML - missing Chunk node\n");
213-
ret = AVERROR_INVALIDDATA;
214-
goto clean_up_asset;
216+
return AVERROR_INVALIDDATA;
215217
}
216218

217219
uri = xmlNodeGetContent(ff_xml_get_child_element_by_name(node, "Path"));
@@ -222,28 +224,13 @@ static int parse_imf_asset_map_from_xml_dom(AVFormatContext *s,
222224
xmlFree(uri);
223225
if (!asset->absolute_uri) {
224226
av_log(NULL, AV_LOG_PANIC, "Cannot allocate asset locator absolute URI\n");
225-
ret = AVERROR(ENOMEM);
226-
goto clean_up_asset;
227+
return AVERROR(ENOMEM);
227228
}
228229

229230
av_log(s, AV_LOG_DEBUG, "Found asset absolute URI: %s\n", asset->absolute_uri);
230231

231-
node = xmlNextElementSibling(node->parent->parent);
232-
233-
asset_map->assets = av_realloc_f(asset_map->assets,
234-
asset_map->asset_count + 1,
235-
sizeof(IMFAssetLocator));
236-
if (!asset_map->assets) {
237-
av_log(NULL, AV_LOG_PANIC, "Cannot allocate IMF asset locators\n");
238-
ret = AVERROR(ENOMEM);
239-
goto clean_up_asset;
240-
}
241-
asset_map->assets[asset_map->asset_count++] = asset;
242-
continue;
243-
244-
clean_up_asset:
245-
av_freep(&asset);
246-
return ret;
232+
asset_map->asset_count++;
233+
node = xmlNextElementSibling(node);
247234
}
248235

249236
return ret;
@@ -275,8 +262,7 @@ static void imf_asset_locator_map_free(IMFAssetLocatorMap *asset_map)
275262
return;
276263

277264
for (int i = 0; i < asset_map->asset_count; ++i) {
278-
av_free(asset_map->assets[i]->absolute_uri);
279-
av_free(asset_map->assets[i]);
265+
av_free(asset_map->assets[i].absolute_uri);
280266
}
281267

282268
av_freep(&asset_map->assets);
@@ -360,12 +346,9 @@ static int parse_assetmap(AVFormatContext *s, const char *url, AVIOContext *in)
360346

361347
static IMFAssetLocator *find_asset_map_locator(IMFAssetLocatorMap *asset_map, FFUUID uuid)
362348
{
363-
IMFAssetLocator *asset_locator;
364-
for (int i = 0; i < asset_map->asset_count; ++i) {
365-
asset_locator = asset_map->assets[i];
366-
if (memcmp(asset_map->assets[i]->uuid, uuid, 16) == 0)
367-
return asset_locator;
368-
}
349+
for (int i = 0; i < asset_map->asset_count; ++i)
350+
if (memcmp(asset_map->assets[i].uuid, uuid, 16) == 0)
351+
return &(asset_map->assets[i]);
369352
return NULL;
370353
}
371354

libavformat/tests/imf.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -343,26 +343,26 @@ static int test_bad_cpl_parsing(void)
343343
return 0;
344344
}
345345

346-
static int check_asset_locator_attributes(IMFAssetLocator *asset, IMFAssetLocator expected_asset)
346+
static int check_asset_locator_attributes(IMFAssetLocator *asset, IMFAssetLocator *expected_asset)
347347
{
348348

349349
printf("\tCompare " FF_UUID_FORMAT " to " FF_UUID_FORMAT ".\n",
350350
UID_ARG(asset->uuid),
351-
UID_ARG(expected_asset.uuid));
351+
UID_ARG(expected_asset->uuid));
352352
for (int i = 0; i < 16; ++i) {
353-
if (asset->uuid[i] != expected_asset.uuid[i]) {
353+
if (asset->uuid[i] != expected_asset->uuid[i]) {
354354
printf("Invalid asset locator UUID: found " FF_UUID_FORMAT " instead of " FF_UUID_FORMAT " expected.\n",
355355
UID_ARG(asset->uuid),
356-
UID_ARG(expected_asset.uuid));
356+
UID_ARG(expected_asset->uuid));
357357
return 1;
358358
}
359359
}
360360

361-
printf("\tCompare %s to %s.\n", asset->absolute_uri, expected_asset.absolute_uri);
362-
if (strcmp(asset->absolute_uri, expected_asset.absolute_uri) != 0) {
361+
printf("\tCompare %s to %s.\n", asset->absolute_uri, expected_asset->absolute_uri);
362+
if (strcmp(asset->absolute_uri, expected_asset->absolute_uri) != 0) {
363363
printf("Invalid asset locator URI: found %s instead of %s expected.\n",
364364
asset->absolute_uri,
365-
expected_asset.absolute_uri);
365+
expected_asset->absolute_uri);
366366
return 1;
367367
}
368368

@@ -414,8 +414,8 @@ static int test_asset_map_parsing(void)
414414

415415
for (int i = 0; i < asset_locator_map->asset_count; ++i) {
416416
printf("For asset: %d:\n", i);
417-
ret = check_asset_locator_attributes(asset_locator_map->assets[i],
418-
ASSET_MAP_EXPECTED_LOCATORS[i]);
417+
ret = check_asset_locator_attributes(&(asset_locator_map->assets[i]),
418+
&(ASSET_MAP_EXPECTED_LOCATORS[i]));
419419
if (ret > 0)
420420
goto cleanup;
421421
}

0 commit comments

Comments
 (0)