Skip to content

Commit 5b2c512

Browse files
author
Grok Compression
committed
composite: use 16 bit where appropriate
1 parent 04935aa commit 5b2c512

4 files changed

Lines changed: 42 additions & 8 deletions

File tree

src/lib/core/codestream/decompress/CodeStreamDecompress.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1757,6 +1757,41 @@ bool CodeStreamDecompress::activateScratch(bool singleTile, GrkImage* scratch)
17571757
{
17581758
multiTileComposite_->copyHeaderTo(scratch);
17591759

1760+
// Set int16 data_type on scratch components when all components are eligible
1761+
// for 16-bit DWT. This ensures the composite buffer is allocated as int16,
1762+
// avoiding int16→int32 widening during tile compositing.
1763+
// Mirror the eligibility logic from TileProcessor::createDecompressTileComponentWindows.
1764+
// Only do this when there's no region decode, because partial tiles have
1765+
// wholeTileDecompress_=false and fall back to int32 DWT.
1766+
if(scratch->has_multiple_tiles && region_.empty())
1767+
{
1768+
bool allEligible = true;
1769+
bool hasMct =
1770+
defaultTcp_->mct_ == 1 && scratch->numcomps >= 3 && scratch->componentsEqual(3, false);
1771+
for(uint16_t i = 0; i < scratch->numcomps; i++)
1772+
{
1773+
auto tccp = defaultTcp_->tccps_ + i;
1774+
auto comp = scratch->comps + i;
1775+
if(tccp->qmfbid_ != 1)
1776+
{
1777+
allEligible = false;
1778+
break;
1779+
}
1780+
bool isMctComp = hasMct && (i <= 2);
1781+
uint32_t headroom = isMctComp ? 5 : 4;
1782+
if(comp->prec + headroom > 16)
1783+
{
1784+
allEligible = false;
1785+
break;
1786+
}
1787+
}
1788+
if(allEligible)
1789+
{
1790+
for(uint16_t i = 0; i < scratch->numcomps; i++)
1791+
scratch->comps[i].data_type = GRK_INT_16;
1792+
}
1793+
}
1794+
17601795
// no need to allocate composite data if there is only one tile
17611796
// i.e. no compositing — UNLESS band callback is active, which needs
17621797
// a destination buffer for compositing before writing.

src/lib/core/util/GrkImage.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -396,9 +396,10 @@ bool GrkImage::allocData(grk_image_comp* comp, bool clear)
396396
if(!comp || comp->w == 0 || comp->h == 0)
397397
return false;
398398
single_component_data_free(comp);
399-
uint32_t stride = grk_make_aligned_width<int32_t>(comp->w);
399+
uint32_t stride = (comp->data_type == GRK_INT_16) ? grk_make_aligned_width<int16_t>(comp->w)
400+
: grk_make_aligned_width<int32_t>(comp->w);
400401
size_t dataSize = (uint64_t)stride * comp->h * sizeOfDataType(comp->data_type);
401-
auto data = (int32_t*)grk_aligned_malloc(dataSize);
402+
auto data = grk_aligned_malloc(dataSize);
402403
if(!data)
403404
{
404405
grk::grklog.error("Failed to allocate aligned memory buffer of dimensions %u x %u",

src/lib/core/util/GrkImage.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,8 @@ class GrkImage : public grk_image
155155
{
156156
case GRK_INT_32:
157157
return compositeInterleaved_T<int32_t>(src, yBegin, yEnd);
158-
// case GRK_INT_16:
159-
// return compositeInterleaved_T<int16_t>(src,yBegin,yEnd);
158+
case GRK_INT_16:
159+
return compositeInterleaved_T<int16_t>(src, yBegin, yEnd);
160160
// case GRK_INT_8:
161161
// return compositeInterleaved_T<int8_t>(src,yBegin,yEnd);
162162
// case GRK_FLOAT:

src/lib/core/util/GrkImageSIMD.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1127,8 +1127,7 @@ void hwy_copy_tile_to_swath(const grk_image* tile_img, const grk_swath_buffer* b
11271127
}
11281128
else if(buf->prec <= 16 && buf->sgnd)
11291129
{
1130-
int16_t w =
1131-
v <= -32768 ? (int16_t)-32768 : v >= 32767 ? (int16_t)32767 : (int16_t)v;
1130+
int16_t w = v <= -32768 ? (int16_t)-32768 : v >= 32767 ? (int16_t)32767 : (int16_t)v;
11321131
memcpy(dstPx, &w, sizeof(int16_t));
11331132
}
11341133
else if(buf->prec <= 16)
@@ -1186,8 +1185,7 @@ void hwy_copy_tile_to_swath(const grk_image* tile_img, const grk_swath_buffer* b
11861185
}
11871186
else if(buf->prec <= 16 && buf->sgnd)
11881187
{
1189-
int16_t w =
1190-
v <= -32768 ? (int16_t)-32768 : v >= 32767 ? (int16_t)32767 : (int16_t)v;
1188+
int16_t w = v <= -32768 ? (int16_t)-32768 : v >= 32767 ? (int16_t)32767 : (int16_t)v;
11911189
memcpy(dstPx, &w, sizeof(int16_t));
11921190
}
11931191
else if(buf->prec <= 16)

0 commit comments

Comments
 (0)