Newer
Older

Karsten Suehring
committed
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2019, ITU/ISO/IEC

Karsten Suehring
committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file Picture.cpp
* \brief Description of a coded picture
*/
#include "Picture.h"
#include "SEI.h"
#include "ChromaFormat.h"
#if ENABLE_WPP_PARALLELISM
#if ENABLE_WPP_STATIC_LINK
#include <atomic>
#else
#include <condition_variable>
#endif
#endif
#if ENABLE_WPP_PARALLELISM || ENABLE_SPLIT_PARALLELISM
#if ENABLE_WPP_PARALLELISM
#if ENABLE_WPP_STATIC_LINK
class SyncObj
{
public:
SyncObj() : m_Val(-1) {}
~SyncObj() {}
void reset()
{
m_Val = -1;
}
bool isReady( int64_t val ) const
{
// std::cout << "is ready m_Val " << m_Val << " val " << val << std::endl;
return m_Val >= val;
}
void wait( int64_t idx, int ctuPosY )
{
while( ! isReady( idx ) )
{
}
}
void set( int64_t val, int ctuPosY)
{
m_Val = val;
}
private:
std::atomic<int> m_Val;
};
#else
class SyncObj
{
public:
SyncObj() : m_Val(-1) {}
~SyncObj() {}
void reset()
{
std::unique_lock< std::mutex > lock( m_mutex );
m_Val = -1;
}
bool isReady( int64_t val ) const
{
return m_Val >= val;
}
void wait( int64_t idx, int ctuPosY )
{
std::unique_lock< std::mutex > lock( m_mutex );
while( ! isReady( idx ) )
{
m_cv.wait( lock );
}
}
void set( int64_t val, int ctuPosY)
{
std::unique_lock< std::mutex > lock( m_mutex );
m_Val = val;
m_cv.notify_all();
}
private:
int64_t m_Val;
std::condition_variable m_cv;
std::mutex m_mutex;
};
#endif
#endif
int g_wppThreadId( 0 );
#pragma omp threadprivate(g_wppThreadId)
#if ENABLE_SPLIT_PARALLELISM
int g_splitThreadId( 0 );
#pragma omp threadprivate(g_splitThreadId)
int g_splitJobId( 0 );
#pragma omp threadprivate(g_splitJobId)
#endif
Scheduler::Scheduler() :
#if ENABLE_WPP_PARALLELISM
m_numWppThreads( 1 ),
m_numWppDataInstances( 1 )
#endif
#if ENABLE_SPLIT_PARALLELISM && ENABLE_WPP_PARALLELISM
,
#endif
#if ENABLE_SPLIT_PARALLELISM
m_numSplitThreads( 1 )
#endif
{
}
Scheduler::~Scheduler()
{
#if ENABLE_WPP_PARALLELISM
for( auto & so : m_SyncObjs )
{
delete so;
}
m_SyncObjs.clear();
#endif
}
#if ENABLE_SPLIT_PARALLELISM
unsigned Scheduler::getSplitDataId( int jobId ) const
{
if( m_numSplitThreads > 1 && m_hasParallelBuffer )
{
int splitJobId = jobId == CURR_THREAD_ID ? g_splitJobId : jobId;
return ( g_wppThreadId * NUM_RESERVERD_SPLIT_JOBS ) + splitJobId;
}
else
{
return 0;
}
}
unsigned Scheduler::getSplitPicId( int tId /*= CURR_THREAD_ID */ ) const
{
if( m_numSplitThreads > 1 && m_hasParallelBuffer )
{
int threadId = tId == CURR_THREAD_ID ? g_splitThreadId : tId;
return ( g_wppThreadId * m_numSplitThreads ) + threadId;
}
else
{
return 0;
}
}
unsigned Scheduler::getSplitJobId() const
{
if( m_numSplitThreads > 1 )
{
return g_splitJobId;
}
else
{
return 0;
}
}
void Scheduler::setSplitJobId( const int jobId )
{
CHECK( g_splitJobId != 0 && jobId != 0, "Need to reset the jobId after usage!" );
g_splitJobId = jobId;
}
void Scheduler::startParallel()
{
m_hasParallelBuffer = true;
}
void Scheduler::finishParallel()
{
m_hasParallelBuffer = false;
}
void Scheduler::setSplitThreadId( const int tId )
{
g_splitThreadId = tId == CURR_THREAD_ID ? omp_get_thread_num() : tId;
}
#endif
#if ENABLE_WPP_PARALLELISM
unsigned Scheduler::getWppDataId( int lID ) const
{
const int tId = lID == CURR_THREAD_ID ? g_wppThreadId : lID;
#if ENABLE_SPLIT_PARALLELISM
if( m_numSplitThreads > 1 )
{
return tId * NUM_RESERVERD_SPLIT_JOBS;
}
else
{
return tId;
}
#else
return tId;
#endif
}
unsigned Scheduler::getWppThreadId() const
{
return g_wppThreadId;
}
void Scheduler::setWppThreadId( const int tId )
{
g_wppThreadId = tId == CURR_THREAD_ID ? omp_get_thread_num() : tId;
CHECK( g_wppThreadId >= PARL_WPP_MAX_NUM_THREADS, "The WPP thread ID " << g_wppThreadId << " is invalid!" );
}
#endif
unsigned Scheduler::getDataId() const
{
#if ENABLE_SPLIT_PARALLELISM
if( m_numSplitThreads > 1 )
{
return getSplitDataId();
}
#endif
#if ENABLE_WPP_PARALLELISM
if( m_numWppThreads > 1 )
{
return getWppDataId();
}
#endif
return 0;
}
bool Scheduler::init( const int ctuYsize, const int ctuXsize, const int numWppThreadsRunning, const int numWppExtraLines, const int numSplitThreads )
{
#if ENABLE_SPLIT_PARALLELISM
m_numSplitThreads = numSplitThreads;
#endif
#if ENABLE_WPP_PARALLELISM
m_firstNonFinishedLine = 0;
m_numWppThreadsRunning = 1;
m_numWppDataInstances = numWppThreadsRunning+numWppExtraLines;
m_numWppThreads = numWppThreadsRunning;
m_ctuYsize = ctuYsize;
m_ctuXsize = ctuXsize;
if( m_SyncObjs.size() == 0 )
{
m_SyncObjs.reserve( ctuYsize );
for( int i = (int)m_SyncObjs.size(); i < ctuYsize; i++ )
{
m_SyncObjs.push_back( new SyncObj );
}
}
else
{
CHECK( m_SyncObjs.size() != ctuYsize, "");
}
for( int i = 0; i < ctuYsize; i++ )
{
m_SyncObjs[i]->reset();
}
if( m_numWppThreads != m_numWppDataInstances )
{
m_LineDone.clear();
m_LineDone.resize(ctuYsize, -1);
m_LineProc.clear();
m_LineProc.resize(ctuYsize, false);
m_SyncObjs[0]->set(0,0);
m_LineProc[0]=true;
}
#endif
return true;
}
int Scheduler::getNumPicInstances() const
{
#if !ENABLE_SPLIT_PARALLELISM
return 1;
#elif !ENABLE_WPP_PARALLELISM
return ( m_numSplitThreads > 1 ? m_numSplitThreads : 1 );
#else
return m_numSplitThreads > 1 ? m_numWppDataInstances * m_numSplitThreads : 1;
#endif
}
#if ENABLE_WPP_PARALLELISM
void Scheduler::wait( const int ctuPosX, const int ctuPosY )
{
if( m_numWppThreads == m_numWppDataInstances )
{
if( ctuPosY > 0 && ctuPosX+1 < m_ctuXsize)
{
m_SyncObjs[ctuPosY-1]->wait( ctuPosX+1, ctuPosY-1 );
}
return;
}
m_SyncObjs[ctuPosY]->wait( ctuPosX, ctuPosY );
}
void Scheduler::setReady(const int ctuPosX, const int ctuPosY)
{
if( m_numWppThreads == m_numWppDataInstances )
{
m_SyncObjs[ctuPosY]->set( ctuPosX, ctuPosY);
return;
}
std::unique_lock< std::mutex > lock( m_mutex );
if( ctuPosX+1 == m_ctuXsize )
{
m_LineProc[ctuPosY] = true; //prevent line from be further evaluated
m_LineDone[ctuPosY] = std::numeric_limits<int>::max();
m_firstNonFinishedLine = ctuPosY+1;
}
else
{
m_LineDone[ctuPosY] = ctuPosX;
m_LineProc[ctuPosY] = false; // mark currently not processed
}
int lastLine = m_firstNonFinishedLine + m_numWppDataInstances;
lastLine = std::min( m_ctuYsize, lastLine )-1-m_firstNonFinishedLine;
m_numWppThreadsRunning--;
Position pos;
//if the current encoder is the last
const bool c1 = (ctuPosY == m_firstNonFinishedLine + m_numWppThreads - 1);
const bool c2 = (ctuPosY+1 <= m_firstNonFinishedLine+lastLine);
const bool c3 = (ctuPosX >= m_ctuXsize/4);
if( c1 && c2 && c3 && getNextCtu( pos, ctuPosY+1, 4 ) )
{
// try to continue in the next row
// go on in the current line
m_SyncObjs[pos.y]->set(pos.x, pos.y);
m_numWppThreadsRunning++;
}
else if( getNextCtu( pos, ctuPosY, 1 ) )
{
// try to continue in the same row
// go on in the current line
m_SyncObjs[pos.y]->set(pos.x, pos.y);
m_numWppThreadsRunning++;
}
for( int i = m_numWppThreadsRunning; i < m_numWppThreads; i++ )
{
// just go and get a job
for( int y = 0; y <= lastLine; y++ )
{
if( getNextCtu( pos, m_firstNonFinishedLine+y, 1 ))
{
m_SyncObjs[pos.y]->set(pos.x, pos.y);
m_numWppThreadsRunning++;
break;
}
}
}
}
bool Scheduler::getNextCtu( Position& pos, int ctuLine, int offset)
{
int x = m_LineDone[ctuLine] + 1;
if( ! m_LineProc[ctuLine] )
{
int maxXOffset = x+offset >= m_ctuXsize ? m_ctuXsize-1 : x+offset;
if( (ctuLine == 0 || m_LineDone[ctuLine-1]>=maxXOffset) && (x==0 || m_LineDone[ctuLine]>=+x-1))
{
m_LineProc[ctuLine] = true;
pos.x = x; pos.y = ctuLine;
return true;
}
}
return false;
}
#endif
#endif
// ---------------------------------------------------------------------------
// picture methods
// ---------------------------------------------------------------------------
#if JVET_N0857_TILES_BRICKS
Brick::Brick()
: m_widthInCtus (0)
, m_heightInCtus (0)
, m_colBd (0)
, m_rowBd (0)
, m_firstCtuRsAddr (0)
{
}
Brick::~Brick()
{
}

Karsten Suehring
committed

Karsten Suehring
committed
Tile::Tile()
: m_tileWidthInCtus (0)
, m_tileHeightInCtus (0)
, m_rightEdgePosInCtus (0)
, m_bottomEdgePosInCtus (0)
, m_firstCtuRsAddr (0)
{
}
Tile::~Tile()
{
}

Karsten Suehring
committed

Karsten Suehring
committed
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
TileMap::TileMap()
: pcv(nullptr)
, tiles(0)
, numTiles(0)
, numTileColumns(0)
, numTileRows(0)
, tileIdxMap(nullptr)
, ctuTsToRsAddrMap(nullptr)
, ctuRsToTsAddrMap(nullptr)
{
}
void TileMap::create( const SPS& sps, const PPS& pps )
{
pcv = pps.pcv;
numTileColumns = pps.getNumTileColumnsMinus1() + 1;
numTileRows = pps.getNumTileRowsMinus1() + 1;
numTiles = numTileColumns * numTileRows;
tiles.resize( numTiles );
const uint32_t numCtusInFrame = pcv->sizeInCtus;
tileIdxMap = new uint32_t[numCtusInFrame];
ctuTsToRsAddrMap = new uint32_t[numCtusInFrame+1];
ctuRsToTsAddrMap = new uint32_t[numCtusInFrame+1];
initTileMap( sps, pps );
initCtuTsRsAddrMap();
}
void TileMap::destroy()
{
tiles.clear();
if ( tileIdxMap )
{
delete[] tileIdxMap;
tileIdxMap = nullptr;
}
if ( ctuTsToRsAddrMap )
{
delete[] ctuTsToRsAddrMap;
ctuTsToRsAddrMap = nullptr;
}
if ( ctuRsToTsAddrMap )
{
delete[] ctuRsToTsAddrMap;
ctuRsToTsAddrMap = nullptr;
}
}
void TileMap::initTileMap( const SPS& sps, const PPS& pps )
{
const uint32_t frameWidthInCtus = pcv->widthInCtus;
const uint32_t frameHeightInCtus = pcv->heightInCtus;

Karsten Suehring
committed
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
{
//set width and height for each (uniform) tile
for(int row=0; row < numTileRows; row++)
{
for(int col=0; col < numTileColumns; col++)
{
const int tileIdx = row * numTileColumns + col;
tiles[tileIdx].setTileWidthInCtus( (col+1)*frameWidthInCtus/numTileColumns - (col*frameWidthInCtus)/numTileColumns );
tiles[tileIdx].setTileHeightInCtus( (row+1)*frameHeightInCtus/numTileRows - (row*frameHeightInCtus)/numTileRows );
}
}
}
else
{
//set the width for each tile
for(int row=0; row < numTileRows; row++)
{
int cumulativeTileWidth = 0;
for(int col=0; col < numTileColumns - 1; col++)
{
tiles[row * numTileColumns + col].setTileWidthInCtus( pps.getTileColumnWidth(col) );
cumulativeTileWidth += pps.getTileColumnWidth(col);
}
tiles[row * numTileColumns + numTileColumns - 1].setTileWidthInCtus( frameWidthInCtus-cumulativeTileWidth );
}
//set the height for each tile
for(int col=0; col < numTileColumns; col++)
{
int cumulativeTileHeight = 0;
for(int row=0; row < numTileRows - 1; row++)
{
tiles[row * numTileColumns + col].setTileHeightInCtus( pps.getTileRowHeight(row) );
cumulativeTileHeight += pps.getTileRowHeight(row);
}
tiles[(numTileRows - 1) * numTileColumns + col].setTileHeightInCtus( frameHeightInCtus-cumulativeTileHeight );
}
}
// Tile size check
int minWidth = 1;
int minHeight = 1;

Karsten Suehring
committed
const int profileIdc = sps.getPTL()->getGeneralPTL()->getProfileIdc();
#else
const int profileIdc = sps.getProfileTierLevel()->getProfileIdc();
#endif

Karsten Suehring
committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
if ( profileIdc == Profile::MAIN || profileIdc == Profile::MAIN10)
{
if (pps.getTilesEnabledFlag())
{
minHeight = 64 / sps.getMaxCUHeight();
minWidth = 256 / sps.getMaxCUWidth();
}
}
for(int row=0; row < numTileRows; row++)
{
for(int col=0; col < numTileColumns; col++)
{
const int tileIdx = row * numTileColumns + col;
if(tiles[tileIdx].getTileWidthInCtus() < minWidth) { THROW("Invalid tile size"); }
if(tiles[tileIdx].getTileHeightInCtus() < minHeight) { THROW("Invalid tile size"); }
}
}
//initialize each tile of the current picture
for( int row=0; row < numTileRows; row++ )
{
for( int col=0; col < numTileColumns; col++ )
{
const int tileIdx = row * numTileColumns + col;
//initialize the RightEdgePosInCU for each tile
int rightEdgePosInCTU = 0;
for( int i=0; i <= col; i++ )
{
rightEdgePosInCTU += tiles[row * numTileColumns + i].getTileWidthInCtus();
}
tiles[tileIdx].setRightEdgePosInCtus(rightEdgePosInCTU-1);
//initialize the BottomEdgePosInCU for each tile
int bottomEdgePosInCTU = 0;
for( int i=0; i <= row; i++ )
{
bottomEdgePosInCTU += tiles[i * numTileColumns + col].getTileHeightInCtus();
}
tiles[tileIdx].setBottomEdgePosInCtus(bottomEdgePosInCTU-1);
//initialize the FirstCUAddr for each tile
tiles[tileIdx].setFirstCtuRsAddr( (tiles[tileIdx].getBottomEdgePosInCtus() - tiles[tileIdx].getTileHeightInCtus() + 1) * frameWidthInCtus +
tiles[tileIdx].getRightEdgePosInCtus() - tiles[tileIdx].getTileWidthInCtus() + 1);
}
}
int columnIdx = 0;
int rowIdx = 0;
//initialize the TileIdxMap
const uint32_t numCtusInFrame = pcv->sizeInCtus;
for( int i=0; i<numCtusInFrame; i++)
{
for( int col=0; col < numTileColumns; col++)
{
if(i % frameWidthInCtus <= tiles[col].getRightEdgePosInCtus())
{
columnIdx = col;
break;
}
}
for(int row=0; row < numTileRows; row++)
{
if(i / frameWidthInCtus <= tiles[row*numTileColumns].getBottomEdgePosInCtus())
{
rowIdx = row;
break;
}
}
tileIdxMap[i] = rowIdx * numTileColumns + columnIdx;
}
}
void TileMap::initCtuTsRsAddrMap()
{
//generate the Coding Order Map and Inverse Coding Order Map
const uint32_t numCtusInFrame = pcv->sizeInCtus;
for(int ctuTsAddr=0, ctuRsAddr=0; ctuTsAddr<numCtusInFrame; ctuTsAddr++, ctuRsAddr = calculateNextCtuRSAddr(ctuRsAddr))
{
ctuTsToRsAddrMap[ctuTsAddr] = ctuRsAddr;
ctuRsToTsAddrMap[ctuRsAddr] = ctuTsAddr;
}
ctuTsToRsAddrMap[numCtusInFrame] = numCtusInFrame;
ctuRsToTsAddrMap[numCtusInFrame] = numCtusInFrame;
}
uint32_t TileMap::calculateNextCtuRSAddr( const uint32_t currCtuRsAddr ) const
{
const uint32_t frameWidthInCtus = pcv->widthInCtus;
uint32_t nextCtuRsAddr;
//get the tile index for the current CTU
const uint32_t uiTileIdx = getTileIdxMap(currCtuRsAddr);
//get the raster scan address for the next CTU
if( currCtuRsAddr % frameWidthInCtus == tiles[uiTileIdx].getRightEdgePosInCtus() && currCtuRsAddr / frameWidthInCtus == tiles[uiTileIdx].getBottomEdgePosInCtus() )
//the current CTU is the last CTU of the tile
{
if(uiTileIdx+1 == numTiles)
{
nextCtuRsAddr = pcv->sizeInCtus;
}
else
{
nextCtuRsAddr = tiles[uiTileIdx+1].getFirstCtuRsAddr();
}
}
else //the current CTU is not the last CTU of the tile
{
if( currCtuRsAddr % frameWidthInCtus == tiles[uiTileIdx].getRightEdgePosInCtus() ) //the current CTU is on the rightmost edge of the tile
{
nextCtuRsAddr = currCtuRsAddr + frameWidthInCtus - tiles[uiTileIdx].getTileWidthInCtus() + 1;
}
else
{
nextCtuRsAddr = currCtuRsAddr + 1;
}
}
return nextCtuRsAddr;
}
uint32_t TileMap::getSubstreamForCtuAddr(const uint32_t ctuAddr, const bool bAddressInRaster, Slice *pcSlice) const
{
const bool bWPPEnabled = pcSlice->getPPS()->getEntropyCodingSyncEnabledFlag();
uint32_t subStrm;
if( (bWPPEnabled && pcv->heightInCtus > 1) || (numTiles > 1) ) // wavefronts, and possibly tiles being used.
{
const uint32_t ctuRsAddr = bAddressInRaster ? ctuAddr : getCtuTsToRsAddrMap(ctuAddr);
const uint32_t tileIndex = getTileIdxMap(ctuRsAddr);
if (bWPPEnabled)
{
const uint32_t firstCtuRsAddrOfTile = tiles[tileIndex].getFirstCtuRsAddr();
const uint32_t tileYInCtus = firstCtuRsAddrOfTile / pcv->widthInCtus;
const uint32_t ctuLine = ctuRsAddr / pcv->widthInCtus;
const uint32_t startingSubstreamForTile = (tileYInCtus * numTileColumns) + (tiles[tileIndex].getTileHeightInCtus() * (tileIndex % numTileColumns));
subStrm = startingSubstreamForTile + (ctuLine - tileYInCtus);
}
else
{
subStrm = tileIndex;
}
}
else
{
subStrm = 0;
}
return subStrm;
}
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#else
BrickMap::BrickMap()
: pcv(nullptr)
, numTiles(0)
, numTileColumns(0)
, numTileRows(0)
, brickIdxRsMap(nullptr)
, brickIdxBsMap(nullptr)
, ctuBsToRsAddrMap(nullptr)
, ctuRsToBsAddrMap(nullptr)
{
}
void BrickMap::create( const SPS& sps, const PPS& pps )
{
pcv = pps.pcv;
numTileColumns = pps.getNumTileColumnsMinus1() + 1;
numTileRows = pps.getNumTileRowsMinus1() + 1;
numTiles = numTileColumns * numTileRows;
const uint32_t numCtusInFrame = pcv->sizeInCtus;
brickIdxRsMap = new uint32_t[numCtusInFrame];
brickIdxBsMap = new uint32_t[numCtusInFrame];
ctuBsToRsAddrMap = new uint32_t[numCtusInFrame+1];
ctuRsToBsAddrMap = new uint32_t[numCtusInFrame+1];
initBrickMap( sps, pps );
numTiles = (uint32_t) bricks.size();
}
void BrickMap::destroy()
{
bricks.clear();
if ( brickIdxRsMap )
{
delete[] brickIdxRsMap;
brickIdxRsMap = nullptr;
}
if ( brickIdxBsMap )
{
delete[] brickIdxBsMap;
brickIdxBsMap = nullptr;
}
if ( ctuBsToRsAddrMap )
{
delete[] ctuBsToRsAddrMap;
ctuBsToRsAddrMap = nullptr;
}
if ( ctuRsToBsAddrMap )
{
delete[] ctuRsToBsAddrMap;
ctuRsToBsAddrMap = nullptr;
}
}
void BrickMap::initBrickMap( const SPS& sps, const PPS& pps )
{
const uint32_t frameWidthInCtus = pcv->widthInCtus;
const uint32_t frameHeightInCtus = pcv->heightInCtus;
std::vector<uint32_t> tileRowHeight (numTileRows);
std::vector<uint32_t> tileColWidth (numTileColumns);
if( pps.getUniformTileSpacingFlag() )
{
//set width and height for each (uniform) tile
for(int row=0; row < numTileRows; row++)
{
tileRowHeight[row] = (row+1)*frameHeightInCtus/numTileRows - (row*frameHeightInCtus)/numTileRows;
}
for(int col=0; col < numTileColumns; col++)
{
tileColWidth[col] = (col+1)*frameWidthInCtus/numTileColumns - (col*frameWidthInCtus)/numTileColumns;
}
}
else
{
tileColWidth[ numTileColumns - 1 ] = frameWidthInCtus;
for( int i = 0; i < numTileColumns - 1; i++ )
{
tileColWidth[ i ] = pps.getTileColumnWidth(i);
tileColWidth[ numTileColumns - 1 ] = tileColWidth[ numTileColumns - 1 ] - pps.getTileColumnWidth(i);
}
tileRowHeight[ numTileRows-1 ] = frameHeightInCtus;
for( int j = 0; j < numTileRows-1; j++ )
{
tileRowHeight[ j ] = pps.getTileRowHeight( j );
tileRowHeight[ numTileRows-1 ] = tileRowHeight[ numTileRows-1 ] - pps.getTileRowHeight( j );
}
}
//initialize each tile of the current picture
std::vector<uint32_t> tileRowBd (numTileRows);
std::vector<uint32_t> tileColBd (numTileColumns);
tileColBd[ 0 ] = 0;
for( int i = 0; i < numTileColumns - 1; i++ )
{
tileColBd[ i + 1 ] = tileColBd[ i ] + tileColWidth[ i ];
}
tileRowBd[ 0 ] = 0;
for( int j = 0; j < numTileRows - 1; j++ )
{
tileRowBd[ j + 1 ] = tileRowBd[ j ] + tileRowHeight[ j ];
}
int brickIdx = 0;
for(int tileIdx=0; tileIdx< numTiles; tileIdx++)
{
int tileX = tileIdx % ( pps.getNumTileColumnsMinus1() + 1 );
int tileY = tileIdx / ( pps.getNumTileColumnsMinus1() + 1 );
if ( !pps.getBrickSplittingPresentFlag() || !pps.getBrickSplitFlag(tileIdx))
{
bricks.resize(bricks.size()+1);
bricks[ brickIdx ].setColBd (tileColBd[ tileX ]);
bricks[ brickIdx ].setRowBd (tileRowBd[ tileY ]);
bricks[ brickIdx ].setWidthInCtus (tileColWidth[ tileX ]);
bricks[ brickIdx ].setHeightInCtus(tileRowHeight[ tileY ]);
bricks[ brickIdx ].setFirstCtuRsAddr(bricks[ brickIdx ].getColBd() + bricks[ brickIdx ].getRowBd() * frameWidthInCtus);
brickIdx++;
}
else
{
std::vector<uint32_t> rowHeight2;
std::vector<uint32_t> rowBd2;
int numBrickRowsMinus1 = 0;
if (pps.getUniformBrickSpacingFlag(tileIdx))
{
int brickHeight= pps.getBrickHeightMinus1(tileIdx) + 1;
int remainingHeightInCtbsY = tileRowHeight[ tileY ];
int brickInTile = 0;
while( remainingHeightInCtbsY > brickHeight )
{
rowHeight2.resize(brickInTile+1);
rowHeight2[ brickInTile++ ] = brickHeight;
remainingHeightInCtbsY -= brickHeight;
}
rowHeight2.resize(brickInTile+1);
rowHeight2[ brickInTile ] = remainingHeightInCtbsY;
numBrickRowsMinus1 = brickInTile;
}
else
{
numBrickRowsMinus1 = pps.getNumBrickRowsMinus1(tileIdx);
rowHeight2.resize(numBrickRowsMinus1 + 1);
rowHeight2[ numBrickRowsMinus1 ] = tileRowHeight[ tileY ];
for(int j = 0; j < numBrickRowsMinus1; j++ )
{
rowHeight2[ j ] = pps.getBrickRowHeightMinus1 ( tileIdx, j )+ 1;
rowHeight2[ numBrickRowsMinus1 ] -= rowHeight2[ j ];
}
}
rowBd2.resize(numBrickRowsMinus1 + 1);
rowBd2[ 0 ] = 0;
for( int j = 0; j < numBrickRowsMinus1; j++ )
{
rowBd2[ j + 1 ] = rowBd2[ j ] + rowHeight2[ j ];
}
for( int j = 0; j < numBrickRowsMinus1 + 1; j++ )
{
bricks.resize(bricks.size()+1);
bricks[ brickIdx ].setColBd (tileColBd[ tileX ]);
bricks[ brickIdx ].setRowBd (tileRowBd[ tileY ] + rowBd2[ j ]);
bricks[ brickIdx ].setWidthInCtus (tileColWidth[ tileX ]);
bricks[ brickIdx ].setHeightInCtus(rowHeight2[ j ]);
bricks[ brickIdx ].setFirstCtuRsAddr(bricks[ brickIdx ].getColBd() + bricks[ brickIdx ].getRowBd() * frameWidthInCtus);
brickIdx++;
}
}
}
initCtuBsRsAddrMap();
for( int i = 0; i < (int)bricks.size(); i++ )
{
for( int y = bricks[i].getRowBd(); y < bricks[i].getRowBd() + bricks[i].getHeightInCtus(); y++ )
{
for( int x = bricks[i].getColBd(); x < bricks[i].getColBd() + bricks[i].getWidthInCtus(); x++ )
{
// brickIdxBsMap in BS scan is brickIdxMap as defined in the draft text
brickIdxBsMap[ ctuRsToBsAddrMap[ y * frameWidthInCtus+ x ] ] = i;
// brickIdxRsMap in RS scan is usually required in the software
brickIdxRsMap[ y * frameWidthInCtus+ x ] = i;
}
}
}
}
void BrickMap::initCtuBsRsAddrMap()
{
const uint32_t picWidthInCtbsY = pcv->widthInCtus;
const uint32_t picHeightInCtbsY = pcv->heightInCtus;
const uint32_t picSizeInCtbsY = picWidthInCtbsY * picHeightInCtbsY;
const int numBricksInPic = (int) bricks.size();
for( uint32_t ctbAddrRs = 0; ctbAddrRs < picSizeInCtbsY; ctbAddrRs++ )
{
const uint32_t tbX = ctbAddrRs % picWidthInCtbsY;
const uint32_t tbY = ctbAddrRs / picWidthInCtbsY;
bool brickFound = false;
int bkIdx = (numBricksInPic - 1);
for( int i = 0; i < (numBricksInPic - 1) && !brickFound; i++ )
{
brickFound = tbX < ( bricks[i].getColBd() + bricks[i].getWidthInCtus() ) &&
tbY < ( bricks[i].getRowBd() + bricks[i].getHeightInCtus() );
if( brickFound )
{
bkIdx = i;
}
}
ctuRsToBsAddrMap[ ctbAddrRs ] = 0;
for( uint32_t i = 0; i < bkIdx; i++ )
{
ctuRsToBsAddrMap[ ctbAddrRs ] += bricks[i].getHeightInCtus() * bricks[i].getWidthInCtus();
}
ctuRsToBsAddrMap[ ctbAddrRs ] += ( tbY - bricks[ bkIdx ].getRowBd() ) * bricks[ bkIdx ].getWidthInCtus() + tbX - bricks[ bkIdx ].getColBd();
}
for( uint32_t ctbAddrRs = 0; ctbAddrRs < picSizeInCtbsY; ctbAddrRs++ )
{
ctuBsToRsAddrMap[ ctuRsToBsAddrMap[ ctbAddrRs ] ] = ctbAddrRs;
}
}
uint32_t BrickMap::getSubstreamForCtuAddr(const uint32_t ctuAddr, const bool addressInRaster, Slice *slice) const
{
const bool wppEnabled = slice->getPPS()->getEntropyCodingSyncEnabledFlag();
uint32_t subStrm;
if( (wppEnabled && pcv->heightInCtus > 1) || (numTiles > 1) ) // wavefronts, and possibly tiles being used.
{
// needs to be checked
CHECK (false, "bricks and WPP needs to be checked");
const uint32_t ctuRsAddr = addressInRaster ? ctuAddr : getCtuBsToRsAddrMap(ctuAddr);
const uint32_t brickIndex = getBrickIdxRsMap(ctuRsAddr);
if (wppEnabled)
{
const uint32_t firstCtuRsAddrOfTile = bricks[brickIndex].getFirstCtuRsAddr();
const uint32_t tileYInCtus = firstCtuRsAddrOfTile / pcv->widthInCtus;
const uint32_t ctuLine = ctuRsAddr / pcv->widthInCtus;
const uint32_t startingSubstreamForTile = (tileYInCtus * numTileColumns) + (bricks[brickIndex].getHeightInCtus() * (brickIndex % numTileColumns));
subStrm = startingSubstreamForTile + (ctuLine - tileYInCtus);
}
else
{
subStrm = brickIndex;
}
}
else
{
subStrm = 0;
}
return subStrm;
}