78
78
#define CTB_PROGRESS_DEBLK_H 3
79
79
#define CTB_PROGRESS_SAO 4
81
class decoder_context;
81
83
template <class DataUnit> class MetaDataArray
84
86
MetaDataArray() { data=NULL; data_size=0; log2unitSize=0; width_in_units=0; height_in_units=0; }
85
87
~MetaDataArray() { free(data); }
87
bool alloc(int w,int h, int _log2unitSize) {
89
LIBDE265_CHECK_RESULT bool alloc(int w,int h, int _log2unitSize) {
90
92
if (size != data_size) {
92
94
data = (DataUnit*)malloc(size * sizeof(DataUnit));
98
105
log2unitSize = _log2unitSize;
100
107
return data != NULL;
122
135
int unitX = x>>log2unitSize;
123
136
int unitY = y>>log2unitSize;
138
assert(unitX >= 0 && unitX < width_in_units);
139
assert(unitY >= 0 && unitY < height_in_units);
125
141
data[ unitX + unitY*width_in_units ] = d;
128
144
DataUnit& operator[](int idx) { return data[idx]; }
129
145
const DataUnit& operator[](int idx) const { return data[idx]; }
147
int size() const { return data_size; }
146
164
cb_info[ cbx + cby*cb_info.width_in_units ].Field = value; \
167
#define CLEAR_TB_BLK(x,y,log2BlkWidth) \
168
int tuX = x >> tu_info.log2unitSize; \
169
int tuY = y >> tu_info.log2unitSize; \
170
int width = 1 << (log2BlkWidth - tu_info.log2unitSize); \
171
for (int tuy=tuY;tuy<tuY+width;tuy++) \
172
for (int tux=tuX;tux<tuX+width;tux++) \
174
tu_info[ tux + tuy*tu_info.width_in_units ] = 0; \
151
179
uint16_t SliceAddrRS;
154
182
sao_info saoInfo;
155
183
bool deblock; // this CTB has to be deblocked
156
bool has_pcm; // pcm is used in this CTB
157
bool has_cu_transquant_bypass; // transquant_bypass is used in this CTB
185
// The following flag helps to quickly check whether we have to
186
// check all conditions in the SAO filter or whether we can skip them.
187
bool has_pcm_or_cu_transquant_bypass; // pcm or transquant_bypass is used in this CTB
162
uint8_t log2CbSize : 3; // [0;6] (1<<log2CbSize) = 64
192
uint8_t log2CbSize : 3; /* [0;6] (1<<log2CbSize) = 64
193
This is only set in the top-left corner of the CB.
194
The other values should be zero.
195
TODO: in the encoder, we have to clear to zero.
196
Used in deblocking and QP-scale decoding */
163
197
uint8_t PartMode : 3; // (enum PartMode) [0;7] set only in top-left of CB
164
// TODO: could be removed if prediction-block-boundaries would be
165
// set during decoding
166
uint8_t ctDepth : 2; // [0:3]? (0:64, 1:32, 2:16, 3:8)
198
// Used for spatial merging candidates in current frame
199
// and for deriving interSplitFlag in decoding.
201
uint8_t ctDepth : 2; // [0:3]? (for CTB size 64: 0:64, 1:32, 2:16, 3:8)
202
// Used for decoding/encoding split_cu flag.
204
// --- byte boundary ---
167
205
uint8_t PredMode : 2; // (enum PredMode) [0;2] must be saved for past images
168
uint8_t pcm_flag : 1; //
169
uint8_t cu_transquant_bypass : 1;
173
// uint8_t pcm_flag; // TODO
206
// Used in motion decoding.
207
uint8_t pcm_flag : 1; // Stored for intra-prediction / SAO
208
uint8_t cu_transquant_bypass : 1; // Stored for SAO
211
// --- byte boundary ---
212
int8_t QP_Y; // Stored for QP prediction
178
PredVectorInfo mvi; // TODO: this can be done in 16x16 grid
218
MotionVectorSpec mv; // TODO: this can be done in 16x16 grid
221
// intraPredMode: Used for determining scanIdx when decoding/encoding coefficients.
183
225
struct de265_image {
188
de265_error alloc_image(int w,int h, enum de265_chroma c, const seq_parameter_set* sps,
189
bool allocMetadata, decoder_context* ctx, de265_PTS pts, void* user_data,
230
de265_error alloc_image(int w,int h, enum de265_chroma c,
231
const seq_parameter_set* sps,
233
decoder_context* dctx,
234
class encoder_context* ectx,
235
de265_PTS pts, void* user_data,
236
bool useCustomAllocFunctions);
238
//de265_error alloc_encoder_data(const seq_parameter_set* sps);
192
240
bool is_allocated() const { return pixels[0] != NULL; }
212
260
return pixels[cIdx] + xpos + ypos*stride;
264
/// xpos;ypos in actual plane resolution
265
template <class pixel_t>
266
pixel_t* get_image_plane_at_pos_NEW(int cIdx, int xpos,int ypos)
268
int stride = get_image_stride(cIdx);
269
return (pixel_t*)(pixels[cIdx] + (xpos + ypos*stride)*sizeof(pixel_t));
215
272
const uint8_t* get_image_plane_at_pos(int cIdx, int xpos,int ypos) const
217
274
int stride = get_image_stride(cIdx);
218
275
return pixels[cIdx] + xpos + ypos*stride;
278
void* get_image_plane_at_pos_any_depth(int cIdx, int xpos,int ypos)
280
int stride = get_image_stride(cIdx);
281
return pixels[cIdx] + ((xpos + ypos*stride) << bpp_shift[cIdx]);
284
const void* get_image_plane_at_pos_any_depth(int cIdx, int xpos,int ypos) const
286
int stride = get_image_stride(cIdx);
287
return pixels[cIdx] + ((xpos + ypos*stride) << bpp_shift[cIdx]);
290
/* Number of pixels in one row (not number of bytes).
221
292
int get_image_stride(int cIdx) const
223
294
if (cIdx==0) return stride;
233
304
enum de265_chroma get_chroma_format() const { return chroma_format; }
306
int get_bit_depth(int cIdx) const {
307
if (cIdx==0) return sps.BitDepth_Y;
308
else return sps.BitDepth_C;
311
int get_bytes_per_pixel(int cIdx) const {
312
return (get_bit_depth(cIdx)+7)/8;
315
bool high_bit_depth(int cIdx) const {
316
return get_bit_depth(cIdx)>8;
236
319
bool can_be_released() const { return PicOutputFlag==false && PicState==UnusedForReference; }
252
335
static de265_image_allocation default_image_allocation;
337
void printBlk(const char* title, int x0,int y0,int blkSize,int cIdx) const {
338
::printBlk(title, get_image_plane_at_pos(cIdx,x0,y0),
339
blkSize, get_image_stride(cIdx));
256
344
static uint32_t s_next_image_ID;
258
346
uint8_t* pixels[3];
347
uint8_t bpp_shift[3]; // 0 for 8 bit, 1 for 16 bit
260
349
enum de265_chroma chroma_format;
291
380
seq_parameter_set sps; // the SPS used for decoding this image
292
381
pic_parameter_set pps; // the PPS used for decoding this image
293
382
decoder_context* decctx;
383
class encoder_context* encctx;
385
int number_of_ctbs() const { return ctb_info.size(); }
296
388
MetaDataArray<CTB_info> ctb_info;
297
389
MetaDataArray<CB_ref_info> cb_info;
298
390
MetaDataArray<PB_ref_info> pb_info;
299
391
MetaDataArray<uint8_t> intraPredMode;
392
MetaDataArray<uint8_t> intraPredModeC;
300
393
MetaDataArray<uint8_t> tu_info;
301
394
MetaDataArray<uint8_t> deblk_info;
308
401
void* plane_user_data[3]; // this is logically attached to the pixel data pointers
309
402
de265_image_allocation image_allocation_functions; // the functions used for memory allocation
403
void (*encoder_image_release_func)(en265_encoder_context*,
311
407
uint8_t integrity; /* Whether an error occured while the image was decoded.
312
408
When generated, this is initialized to INTEGRITY_CORRECT,
330
426
void thread_start(int nThreads);
427
void thread_run(const thread_task*);
332
428
void thread_blocks();
333
429
void thread_unblocks();
334
void thread_finishes(); /* NOTE: you should not access any data in the thread_task after
335
calling this, as this function may unlock other threads that
336
will push this image to the output queue and free all decoder data. */
430
/* NOTE: you should not access any data in the thread_task after
431
calling this, as this function may unlock other threads that
432
will push this image to the output queue and free all decoder data. */
433
void thread_finishes(const thread_task*);
338
435
void wait_for_progress(thread_task* task, int ctbx,int ctby, int progress);
339
436
void wait_for_progress(thread_task* task, int ctbAddrRS, int progress);
384
481
return get_pred_mode(x,y)==MODE_SKIP;
387
void set_pcm_flag(int x,int y, int log2BlkWidth)
484
void set_pcm_flag(int x,int y, int log2BlkWidth, uint8_t value=1)
389
SET_CB_BLK(x,y,log2BlkWidth, pcm_flag, 1);
390
ctb_info.get(x,y).has_pcm = true;
486
SET_CB_BLK(x,y,log2BlkWidth, pcm_flag, value);
488
// TODO: in the encoder, we somewhere have to clear this
489
ctb_info.get(x,y).has_pcm_or_cu_transquant_bypass = true;
393
492
int get_pcm_flag(int x,int y) const
395
494
return cb_info.get(x,y).pcm_flag;
398
void set_cu_transquant_bypass(int x,int y, int log2BlkWidth)
497
void set_cu_transquant_bypass(int x,int y, int log2BlkWidth, uint8_t value=1)
400
SET_CB_BLK(x,y,log2BlkWidth, cu_transquant_bypass, 1);
401
ctb_info.get(x,y).has_cu_transquant_bypass = true;
499
SET_CB_BLK(x,y,log2BlkWidth, cu_transquant_bypass, value);
501
// TODO: in the encoder, we somewhere have to clear this
502
ctb_info.get(x,y).has_pcm_or_cu_transquant_bypass = true;
404
505
int get_cu_transquant_bypass(int x,int y) const
406
507
return cb_info.get(x,y).cu_transquant_bypass;
409
void set_log2CbSize(int x0, int y0, int log2CbSize)
510
void set_log2CbSize(int x0, int y0, int log2CbSize, bool fill)
512
// In theory, we could assume that remaining cb_info blocks are initialized to zero.
513
// But in corrupted streams, slices may overlap and set contradicting log2CbSizes.
514
// We also need this for encoding.
516
SET_CB_BLK(x0,y0,log2CbSize, log2CbSize, 0);
411
519
cb_info.get(x0,y0).log2CbSize = log2CbSize;
413
// assume that remaining cb_info blocks are initialized to zero
416
522
int get_log2CbSize(int x0, int y0) const
461
567
tu_info.get(x0,y0) |= (1<<trafoDepth);
570
void clear_split_transform_flags(int x0,int y0,int log2CbSize)
572
CLEAR_TB_BLK (x0,y0, log2CbSize);
464
575
int get_split_transform_flag(int x0,int y0,int trafoDepth) const
466
577
return (tu_info.get(x0,y0) & (1<<trafoDepth));
500
611
void set_IntraPredMode(int PUidx,int log2blkSize, enum IntraPredMode mode)
502
613
int pbSize = 1<<(log2blkSize - intraPredMode.log2unitSize);
504
615
for (int y=0;y<pbSize;y++)
505
616
for (int x=0;x<pbSize;x++)
506
617
intraPredMode[PUidx + x + y*intraPredMode.width_in_units] = mode;
620
void set_IntraPredMode(int x0,int y0,int log2blkSize,
621
enum IntraPredMode mode)
623
int pbSize = 1<<(log2blkSize - intraPredMode.log2unitSize);
624
int PUidx = (x0>>sps.Log2MinPUSize) + (y0>>sps.Log2MinPUSize)*sps.PicWidthInMinPUs;
626
for (int y=0;y<pbSize;y++)
627
for (int x=0;x<pbSize;x++) {
628
assert(x<sps.PicWidthInMinPUs);
629
assert(y<sps.PicHeightInMinPUs);
631
int idx = PUidx + x + y*intraPredMode.width_in_units;
632
assert(idx<intraPredMode.data_size);
633
intraPredMode[idx] = mode;
638
enum IntraPredMode get_IntraPredModeC(int x,int y) const
640
return (enum IntraPredMode)(intraPredModeC.get(x,y) & 0x3f);
643
bool is_IntraPredModeC_Mode4(int x,int y) const
645
return intraPredModeC.get(x,y) & 0x80;
648
void set_IntraPredModeC(int x0,int y0,int log2blkSize, enum IntraPredMode mode,
651
uint8_t combinedValue = mode;
652
if (is_mode4) combinedValue |= 0x80;
654
int pbSize = 1<<(log2blkSize - intraPredMode.log2unitSize);
655
int PUidx = (x0>>sps.Log2MinPUSize) + (y0>>sps.Log2MinPUSize)*sps.PicWidthInMinPUs;
657
for (int y=0;y<pbSize;y++)
658
for (int x=0;x<pbSize;x++) {
659
assert(x<sps.PicWidthInMinPUs);
660
assert(y<sps.PicHeightInMinPUs);
662
int idx = PUidx + x + y*intraPredModeC.width_in_units;
663
assert(idx<intraPredModeC.data_size);
664
intraPredModeC[idx] = combinedValue;
670
// NOTE: encoder only
671
void set_ChromaIntraPredMode(int x,int y,int log2BlkWidth, enum IntraChromaPredMode mode)
673
SET_CB_BLK (x, y, log2BlkWidth, intra_chroma_pred_mode, mode);
676
// NOTE: encoder only
677
enum IntraChromaPredMode get_ChromaIntraPredMode(int x,int y) const
679
return (enum IntraChromaPredMode)(cb_info.get(x,y).intra_chroma_pred_mode);
510
683
// --- CTB metadata access ---
547
720
return ctb_info[ctb].SliceHeaderIndex;
723
bool is_SliceHeader_available(int x,int y) const
725
int idx = ctb_info.get(x,y).SliceHeaderIndex;
726
return idx >= 0 && idx < slices.size();
550
729
slice_segment_header* get_SliceHeader(int x, int y)
552
return slices[ get_SliceHeaderIndex(x,y) ];
731
int idx = get_SliceHeaderIndex(x,y);
732
if (idx >= slices.size()) { return NULL; }
555
736
slice_segment_header* get_SliceHeaderCtb(int ctbX, int ctbY)
557
return slices[ get_SliceHeaderIndexCtb(ctbX,ctbY) ];
738
int idx = get_SliceHeaderIndexCtb(ctbX,ctbY);
739
if (idx >= slices.size()) { return NULL; }
560
743
const slice_segment_header* get_SliceHeaderCtb(int ctbX, int ctbY) const
562
return slices[ get_SliceHeaderIndexCtb(ctbX,ctbY) ];
745
int idx = get_SliceHeaderIndexCtb(ctbX,ctbY);
746
if (idx >= slices.size()) { return NULL; }
565
750
void set_sao_info(int ctbX,int ctbY,const sao_info* saoinfo)
567
752
sao_info* sao = &ctb_info[ctbX + ctbY*ctb_info.width_in_units].saoInfo;
571
756
sizeof(sao_info));
574
759
const sao_info* get_sao_info(int ctbX,int ctbY) const
576
761
return &ctb_info[ctbX + ctbY*ctb_info.width_in_units].saoInfo;
592
bool get_CTB_has_pcm(int ctbX,int ctbY) const
594
int idx = ctbX + ctbY*ctb_info.width_in_units;
595
return ctb_info[idx].has_pcm;
598
bool get_CTB_has_cu_transquant_bypass(int ctbX,int ctbY) const
600
int idx = ctbX + ctbY*ctb_info.width_in_units;
601
return ctb_info[idx].has_cu_transquant_bypass;
777
bool get_CTB_has_pcm_or_cu_transquant_bypass(int ctbX,int ctbY) const
779
int idx = ctbX + ctbY*ctb_info.width_in_units;
780
return ctb_info[idx].has_pcm_or_cu_transquant_bypass;
643
822
// --- PB metadata access ---
645
const PredVectorInfo* get_mv_info(int x,int y) const
824
const MotionVectorSpec* get_mv_info(int x,int y) const
647
return &pb_info.get(x,y).mvi;
826
return &pb_info.get(x,y).mv;
650
void set_mv_info(int x,int y, int nPbW,int nPbH, const PredVectorInfo* mv);
652
// --- value logging ---
829
void set_mv_info(int x,int y, int nPbW,int nPbH, const MotionVectorSpec& mv);
831
// --- value logging ---
833
void printBlk(int x0,int y0, int cIdx, int log2BlkSize);