Skip to content

Commit

Permalink
Allocate the threadData array directly
Browse files Browse the repository at this point in the history
The threadData array (formerly named tdArray) has a fixed size (i.e.,
the array does not grow or shrink while in use), so we can allocate the
array directly. It is not necessary to define the array as avifArray.
  • Loading branch information
wantehchang committed Nov 27, 2023
1 parent 959109d commit c74189f
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/reformat.c
Original file line number Diff line number Diff line change
Expand Up @@ -1588,11 +1588,12 @@ avifResult avifImageYUVToRGB(const avifImage * image, avifRGBImage * rgb)
return avifImageYUVToRGBImpl(image, rgb, &state, alphaMultiplyMode);
}

AVIF_ARRAY_DECLARE(YUVToRGBThreadDataArray, YUVToRGBThreadData, threadData);
YUVToRGBThreadDataArray tdArray;
if (!avifArrayCreate(&tdArray, sizeof(YUVToRGBThreadData), jobs)) {
const size_t byteCount = sizeof(YUVToRGBThreadData) * jobs;
YUVToRGBThreadData * threadData = avifAlloc(byteCount);
if (!threadData) {
return AVIF_RESULT_OUT_OF_MEMORY;
}
memset(threadData, 0, byteCount);
int rowsPerJob = image->height / jobs;
if (rowsPerJob % 2) {
++rowsPerJob;
Expand All @@ -1601,7 +1602,7 @@ avifResult avifImageYUVToRGB(const avifImage * image, avifRGBImage * rgb)
int startRow = 0;
uint32_t i;
for (i = 0; i < jobs; ++i, startRow += rowsPerJob) {
YUVToRGBThreadData * tdata = &tdArray.threadData[i];
YUVToRGBThreadData * tdata = &threadData[i];
const avifCropRect rect = { .x = 0, .y = startRow, .width = image->width, .height = (i == jobs - 1) ? rowsForLastJob : rowsPerJob };
if (avifImageSetViewRect(&tdata->image, image, &rect) != AVIF_RESULT_OK) {
tdata->result = AVIF_RESULT_REFORMAT_FAILED;
Expand All @@ -1625,19 +1626,19 @@ avifResult avifImageYUVToRGB(const avifImage * image, avifRGBImage * rgb)
}
// If above loop ran successfully, Run the first job in the current thread.
if (i == jobs) {
avifImageYUVToRGBThreadWorker(&tdArray.threadData[0]);
avifImageYUVToRGBThreadWorker(&threadData[0]);
}
avifResult result = AVIF_RESULT_OK;
for (i = 0; i < jobs; ++i) {
YUVToRGBThreadData * tdata = &tdArray.threadData[i];
YUVToRGBThreadData * tdata = &threadData[i];
if (tdata->threadCreated && !avifJoinYUVToRGBThread(tdata)) {
result = AVIF_RESULT_REFORMAT_FAILED;
}
if (tdata->result != AVIF_RESULT_OK) {
result = tdata->result;
}
}
avifArrayDestroy(&tdArray);
avifFree(threadData);
return result;
}

Expand Down

0 comments on commit c74189f

Please sign in to comment.