digiKam
decctx.h
Go to the documentation of this file.
1 /*
2  * H.265 video codec.
3  * Copyright (c) 2013-2014 struktur AG, Dirk Farin <farin@struktur.de>
4  *
5  * This file is part of libde265.
6  *
7  * libde265 is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of
10  * the License, or (at your option) any later version.
11  *
12  * libde265 is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with libde265. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef DE265_DECCTX_H
22 #define DE265_DECCTX_H
23 
24 #include "libde265/vps.h"
25 #include "libde265/sps.h"
26 #include "libde265/pps.h"
27 #include "libde265/nal.h"
28 #include "libde265/slice.h"
29 #include "libde265/image.h"
30 #include "libde265/motion.h"
31 #include "libde265/de265.h"
32 #include "libde265/dpb.h"
33 #include "libde265/sei.h"
34 #include "libde265/threads.h"
35 #include "libde265/acceleration.h"
36 #include "libde265/nal-parser.h"
37 
38 #include <memory>
39 
40 #define DE265_MAX_VPS_SETS 16 // this is the maximum as defined in the standard
41 #define DE265_MAX_SPS_SETS 16 // this is the maximum as defined in the standard
42 #define DE265_MAX_PPS_SETS 64 // this is the maximum as defined in the standard
43 
44 #define MAX_WARNINGS 20
45 
46 
48 class image_unit;
49 class slice_unit;
50 class decoder_context;
51 
52 
54 {
55 public:
57 
60 
61  int CtbX, CtbY;
62 
63 
64  // motion vectors
65 
67 
68 
69  // prediction
70 
71  // enum IntraPredMode IntraPredModeC[4]; // chroma intra-prediction mode for current CB
73 
74 
75  // residual data
76 
78  uint8_t transform_skip_flag[3];
81 
82  // we need 16 bytes of extra memory (8*int16) to shift the base for the
83  // alignment required for SSE code !
84  int16_t _coeffBuf[(32*32)+8];
85  int16_t *coeffBuf; // the base pointer for into _coeffBuf, aligned to 16 bytes
86 
87  int16_t coeffList[3][32*32];
88  int16_t coeffPos[3][32*32];
89  int16_t nCoeff[3];
90 
91  int32_t residual_luma[32*32]; // only used when cross-comp-prediction is enabled
92 
93 
94  // quantization
95 
97  int CuQpDelta;
100 
104 
106 
108 
110  uint8_t StatCoeff[4];
111 
113  struct de265_image *img;
115 
118  thread_task* task; // executing thread_task or NULL if not multi-threaded
119 
120 private:
121  thread_context(const thread_context&); // not allowed
122  const thread_context& operator=(const thread_context&); // not allowed
123 };
124 
125 
126 
128 {
129  public:
131 
132  void add_warning(de265_error warning, bool once);
134 
135  private:
136  de265_error warnings[MAX_WARNINGS];
137  int nWarnings;
138  de265_error warnings_shown[MAX_WARNINGS]; // warnings that have already occurred
139  int nWarningsShown;
140 };
141 
142 
143 
145 {
146 public:
149 
150  NAL_unit* nal; // we are the owner
151  slice_segment_header* shdr; // not the owner (de265_image is owner)
153 
155 
157 
158 
159  // decoding status
160 
163  Decoded
164  } state;
165 
167  int nThreads;
168 
169  int first_decoded_CTB_RS; // TODO
170  int last_decoded_CTB_RS; // TODO
171 
174  assert(n < nThreadContexts);
175  return &thread_contexts[n];
176  }
177  int num_thread_contexts() const { return nThreadContexts; }
178 
179 private:
180  thread_context* thread_contexts; /* NOTE: cannot use std::vector, because thread_context has
181  no copy constructor. */
182  int nThreadContexts;
183 
184 public:
186 
187 private:
188  slice_unit(const slice_unit&); // not allowed
189  const slice_unit& operator=(const slice_unit&); // not allowed
190 };
191 
192 
194 {
195 public:
198 
200  de265_image sao_output; // if SAO is used, this is allocated and used as SAO output buffer
201 
202  std::vector<slice_unit*> slice_units;
203  std::vector<sei_message> suffix_SEIs;
204 
206  for (int i=0;i<slice_units.size();i++) {
208  return slice_units[i];
209  }
210  }
211 
212  return NULL;
213  }
214 
216  for (int i=1; i<slice_units.size(); i++) {
217  if (slice_units[i]==s) {
218  return slice_units[i-1];
219  }
220  }
221 
222  return NULL;
223  }
224 
226  for (int i=0; i<slice_units.size()-1; i++) {
227  if (slice_units[i]==s) {
228  return slice_units[i+1];
229  }
230  }
231 
232  return NULL;
233  }
234 
235  void dump_slices() const {
236  for (int i=0; i<slice_units.size(); i++) {
237  printf("[%d] = %p\n",i,slice_units[i]);
238  }
239  }
240 
242  if (slice_units.size()==0) return true;
243  if (slice_units.back()->state != slice_unit::Unprocessed) return true;
244  return false;
245  }
246 
247  bool is_first_slice_segment(const slice_unit* s) const {
248  if (slice_units.size()==0) return false;
249  return (slice_units[0] == s);
250  }
251 
252  enum { Invalid, // headers not read yet
253  Unknown, // SPS/PPS available
254  Reference, // will be used as reference
255  Leaf // not a reference picture
256  } role;
257 
258  enum { Unprocessed,
261  Dropped // will not be decoded
262  } state;
263 
264  std::vector<thread_task*> tasks; // we are the owner
265 
266  /* Saved context models for WPP.
267  There is one saved model for the initialization of each CTB row.
268  The array is unused for non-WPP streams. */
269  std::vector<context_model_table> ctx_models; // TODO: move this into image ?
270 };
271 
272 
273 class base_context : public error_queue
274 {
275  public:
277  virtual ~base_context() { }
278 
279  // --- accelerated DSP functions ---
280 
282 
283  struct acceleration_functions acceleration; // CPU optimized functions
284 
285  //virtual /* */ de265_image* get_image(int dpb_index) { return dpb.get_image(dpb_index); }
286  virtual const de265_image* get_image(int frame_id) const = 0;
287  virtual bool has_image(int frame_id) const = 0;
288 };
289 
290 
292  public:
295 
298 
299  void reset();
300 
301  bool has_sps(int id) const { return (bool)sps[id]; }
302  bool has_pps(int id) const { return (bool)pps[id]; }
303 
304  std::shared_ptr<const seq_parameter_set> get_shared_sps(int id) { return sps[id]; }
305  std::shared_ptr<const pic_parameter_set> get_shared_pps(int id) { return pps[id]; }
306 
307  /* */ seq_parameter_set* get_sps(int id) { return sps[id].get(); }
308  const seq_parameter_set* get_sps(int id) const { return sps[id].get(); }
309  /* */ pic_parameter_set* get_pps(int id) { return pps[id].get(); }
310  const pic_parameter_set* get_pps(int id) const { return pps[id].get(); }
311 
312  /*
313  const slice_segment_header* get_SliceHeader_atCtb(int ctb) {
314  return img->slices[img->get_SliceHeaderIndex_atIndex(ctb)];
315  }
316  */
317 
318  uint8_t get_nal_unit_type() const { return nal_unit_type; }
319  bool get_RapPicFlag() const { return RapPicFlag; }
320 
322 
323  de265_error decode(int* more);
324  de265_error decode_some(bool* did_work);
325 
330 
331 
333 
335  de265_error*, de265_PTS pts,
336  nal_header* nal_hdr, void* user_data);
337 
338  //void push_current_picture_to_output_queue();
340 
341 
342  // --- parameters ---
343 
347 
352 
355  //bool param_disable_mc_residual_idct; // not implemented yet
356  //bool param_disable_intra_residual_idct; // not implemented yet
357 
358  void set_image_allocation_functions(de265_image_allocation* allocfunc, void* userdata);
359 
362 
363 
364  // --- input stream data ---
365 
367 
368 
369  int get_num_worker_threads() const { return num_worker_threads; }
370 
371  /* */ de265_image* get_image(int dpb_index) { return dpb.get_image(dpb_index); }
372  const de265_image* get_image(int dpb_index) const { return dpb.get_image(dpb_index); }
373 
374  bool has_image(int dpb_index) const { return dpb_index>=0 && dpb_index<dpb.size(); }
375 
379 
380  private:
381  de265_error read_vps_NAL(bitreader&);
382  de265_error read_sps_NAL(bitreader&);
383  de265_error read_pps_NAL(bitreader&);
384  de265_error read_sei_NAL(bitreader& reader, bool suffix);
385  de265_error read_eos_NAL(bitreader& reader);
386  de265_error read_slice_NAL(bitreader&, NAL_unit* nal, nal_header& nal_hdr);
387 
388  private:
389  // --- internal data ---
390 
391  std::shared_ptr<video_parameter_set> vps[ DE265_MAX_VPS_SETS ];
392  std::shared_ptr<seq_parameter_set> sps[ DE265_MAX_SPS_SETS ];
393  std::shared_ptr<pic_parameter_set> pps[ DE265_MAX_PPS_SETS ];
394 
395  std::shared_ptr<video_parameter_set> current_vps;
396  std::shared_ptr<seq_parameter_set> current_sps;
397  std::shared_ptr<pic_parameter_set> current_pps;
398 
399  public:
401 
402  private:
403  int num_worker_threads;
404 
405 
406  public:
407  // --- frame dropping ---
408 
409  void set_limit_TID(int tid);
410  int get_highest_TID() const;
411  int get_current_TID() const { return current_HighestTid; }
412  int change_framerate(int more_vs_less); // 1: more, -1: less
413  void set_framerate_ratio(int percent);
414 
415  private:
416  // input parameters
417  int limit_HighestTid; // never switch to a layer above this one
418  int framerate_ratio;
419 
420  // current control parameters
421  int goal_HighestTid; // this is the layer we want to decode at
422  int layer_framerate_ratio; // ratio of frames to keep in the current layer
423 
424  int current_HighestTid; // the layer which we are currently decoding
425 
426  struct {
427  int8_t tid;
428  int8_t ratio;
429  } framedrop_tab[100+1];
430  int framedrop_tid_index[6+1];
431 
432  void compute_framedrop_table();
433  void calc_tid_and_framerate_ratio();
434 
435  private:
436  // --- decoded picture buffer ---
437 
439 
440  int current_image_poc_lsb;
441  bool first_decoded_picture;
442  bool NoRaslOutputFlag;
443  bool HandleCraAsBlaFlag;
444  bool FirstAfterEndOfSequenceNAL;
445 
446  int PicOrderCntMsb;
447  int prevPicOrderCntLsb; // at precTid0Pic
448  int prevPicOrderCntMsb; // at precTid0Pic
449 
450  de265_image* img;
451 
452  public:
453  const slice_segment_header* previous_slice_header; /* Remember the last slice for a successive
454  dependent slice. */
455 
456 
457  // --- motion compensation ---
458 
459  public:
463  private:
464  int CurrDeltaPocMsbPresentFlag[MAX_NUM_REF_PICS];
465  int FollDeltaPocMsbPresentFlag[MAX_NUM_REF_PICS];
466 
467  // The number of entries in the lists below.
468  int NumPocStCurrBefore;
469  int NumPocStCurrAfter;
470  int NumPocStFoll;
471  int NumPocLtCurr;
472  int NumPocLtFoll;
473 
474  // These lists contain absolute POC values.
475  int PocStCurrBefore[MAX_NUM_REF_PICS]; // used for reference in current picture, smaller POC
476  int PocStCurrAfter[MAX_NUM_REF_PICS]; // used for reference in current picture, larger POC
477  int PocStFoll[MAX_NUM_REF_PICS]; // not used for reference in current picture, but in future picture
478  int PocLtCurr[MAX_NUM_REF_PICS]; // used in current picture
479  int PocLtFoll[MAX_NUM_REF_PICS]; // used in some future picture
480 
481  // These lists contain indices into the DPB.
482  int RefPicSetStCurrBefore[MAX_NUM_REF_PICS];
483  int RefPicSetStCurrAfter[MAX_NUM_REF_PICS];
484  int RefPicSetStFoll[MAX_NUM_REF_PICS];
485  int RefPicSetLtCurr[MAX_NUM_REF_PICS];
486  int RefPicSetLtFoll[MAX_NUM_REF_PICS];
487 
488 
489  // --- parameters derived from parameter sets ---
490 
491  // NAL
492 
493  uint8_t nal_unit_type;
494 
495  char IdrPicFlag;
496  char RapPicFlag;
497 
498 
499  // --- image unit queue ---
500 
501  std::vector<image_unit*> image_units;
502 
503  bool flush_reorder_buffer_at_this_frame;
504 
505  private:
506  void init_thread_context(thread_context* tctx);
507  void add_task_decode_CTB_row(thread_context* tctx, bool firstSliceSubstream, int ctbRow);
508  void add_task_decode_slice_segment(thread_context* tctx, bool firstSliceSubstream,
509  int ctbX,int ctbY);
510 
511  void mark_whole_slice_as_processed(image_unit* imgunit,
512  slice_unit* sliceunit,
513  int progress);
514 
515  void process_picture_order_count(slice_segment_header* hdr);
516  int generate_unavailable_reference_picture(const seq_parameter_set* sps,
517  int POC, bool longTerm);
518  void process_reference_picture_set(slice_segment_header* hdr);
519  bool construct_reference_picture_lists(slice_segment_header* hdr);
520 
521 
522  void remove_images_from_dpb(const std::vector<int>& removeImageList);
523  void run_postprocessing_filters_sequential(struct de265_image* img);
524  void run_postprocessing_filters_parallel(image_unit* img);
525 };
526 
527 
528 #endif
Definition: nal-parser.h:88
Definition: nal-parser.h:36
Definition: motion.h:49
Definition: decctx.h:274
virtual ~base_context()
Definition: decctx.h:277
virtual bool has_image(int frame_id) const =0
virtual const de265_image * get_image(int frame_id) const =0
void set_acceleration_functions(enum de265_acceleration)
struct acceleration_functions acceleration
Definition: decctx.h:283
Definition: contextmodel.h:100
Definition: threads.h:79
Definition: dpb.h:32
struct de265_image * get_next_picture_in_output_queue() const
Definition: dpb.h:93
int num_pictures_in_output_queue() const
Definition: dpb.h:90
void pop_next_picture_in_output_queue()
int size() const
Definition: dpb.h:53
de265_image * get_image(int index)
Definition: dpb.h:57
Definition: decctx.h:291
bool param_disable_deblocking
Definition: decctx.h:353
uint8_t get_nal_unit_type() const
Definition: decctx.h:318
bool has_pps(int id) const
Definition: decctx.h:302
de265_error decode_slice_unit_tiles(image_unit *imgunit, slice_unit *sliceunit)
bool get_RapPicFlag() const
Definition: decctx.h:319
void * param_image_allocation_userdata
Definition: decctx.h:361
int param_sps_headers_fd
Definition: decctx.h:348
int get_current_TID() const
Definition: decctx.h:411
const seq_parameter_set * get_sps(int id) const
Definition: decctx.h:308
int UsedByCurrPicLt[MAX_NUM_REF_PICS]
Definition: decctx.h:461
pic_parameter_set * get_pps(int id)
Definition: decctx.h:309
int param_slice_headers_fd
Definition: decctx.h:351
de265_error decode_NAL(NAL_unit *nal)
int num_pictures_in_output_queue() const
Definition: decctx.h:377
bool process_slice_segment_header(slice_segment_header *, de265_error *, de265_PTS pts, nal_header *nal_hdr, void *user_data)
de265_error decode_slice_unit_sequential(image_unit *imgunit, slice_unit *sliceunit)
de265_image * get_next_picture_in_output_queue()
Definition: decctx.h:376
de265_error decode(int *more)
de265_image * get_image(int dpb_index)
Definition: decctx.h:371
bool param_sei_check_hash
Definition: decctx.h:344
int get_highest_TID() const
int8_t tid
Definition: decctx.h:427
seq_parameter_set * get_sps(int id)
Definition: decctx.h:307
std::shared_ptr< const pic_parameter_set > get_shared_pps(int id)
Definition: decctx.h:305
int param_vps_headers_fd
Definition: decctx.h:349
de265_error push_picture_to_output_queue(image_unit *)
int param_pps_headers_fd
Definition: decctx.h:350
void set_image_allocation_functions(de265_image_allocation *allocfunc, void *userdata)
const pic_parameter_set * get_pps(int id) const
Definition: decctx.h:310
int DeltaPocMsbCycleLt[MAX_NUM_REF_PICS]
Definition: decctx.h:462
de265_error decode_slice_unit_parallel(image_unit *imgunit, slice_unit *sliceunit)
void stop_thread_pool()
thread_pool thread_pool_
Definition: decctx.h:400
void set_framerate_ratio(int percent)
int PocLsbLt[MAX_NUM_REF_PICS]
Definition: decctx.h:460
bool has_image(int dpb_index) const
Definition: decctx.h:374
void process_nal_hdr(nal_header *)
std::shared_ptr< const seq_parameter_set > get_shared_sps(int id)
Definition: decctx.h:304
const de265_image * get_image(int dpb_index) const
Definition: decctx.h:372
void set_limit_TID(int tid)
int get_num_worker_threads() const
Definition: decctx.h:369
bool param_conceal_stream_errors
Definition: decctx.h:345
NAL_Parser nal_parser
Definition: decctx.h:366
int change_framerate(int more_vs_less)
bool param_disable_sao
Definition: decctx.h:354
void pop_next_picture_in_output_queue()
Definition: decctx.h:378
bool has_sps(int id) const
Definition: decctx.h:301
de265_error decode_slice_unit_WPP(image_unit *imgunit, slice_unit *sliceunit)
int8_t ratio
Definition: decctx.h:428
de265_image_allocation param_image_allocation_functions
Definition: decctx.h:360
bool param_suppress_faulty_pictures
Definition: decctx.h:346
de265_error decode_some(bool *did_work)
de265_error start_thread_pool(int nThreads)
const slice_segment_header * previous_slice_header
Definition: decctx.h:453
Definition: decctx.h:128
void add_warning(de265_error warning, bool once)
de265_error get_warning()
Definition: decctx.h:194
std::vector< thread_task * > tasks
Definition: decctx.h:264
slice_unit * get_prev_slice_segment(slice_unit *s) const
Definition: decctx.h:215
slice_unit * get_next_slice_segment(slice_unit *s) const
Definition: decctx.h:225
enum image_unit::@26 state
std::vector< slice_unit * > slice_units
Definition: decctx.h:202
de265_image * img
Definition: decctx.h:199
de265_image sao_output
Definition: decctx.h:200
std::vector< context_model_table > ctx_models
Definition: decctx.h:269
enum image_unit::@25 role
@ Reference
Definition: decctx.h:254
@ Unknown
Definition: decctx.h:253
@ Leaf
Definition: decctx.h:255
@ Invalid
Definition: decctx.h:252
std::vector< sei_message > suffix_SEIs
Definition: decctx.h:203
@ Unprocessed
Definition: decctx.h:258
@ Decoded
Definition: decctx.h:260
@ InProgress
Definition: decctx.h:259
@ Dropped
Definition: decctx.h:261
bool is_first_slice_segment(const slice_unit *s) const
Definition: decctx.h:247
bool all_slice_segments_processed() const
Definition: decctx.h:241
void dump_slices() const
Definition: decctx.h:235
slice_unit * get_next_unprocessed_slice_segment() const
Definition: decctx.h:205
Definition: pps.h:59
Definition: sps.h:86
Definition: slice.h:130
Definition: decctx.h:145
int first_decoded_CTB_RS
Definition: decctx.h:169
bitreader reader
Definition: decctx.h:152
bool flush_reorder_buffer
Definition: decctx.h:156
int num_thread_contexts() const
Definition: decctx.h:177
slice_segment_header * shdr
Definition: decctx.h:151
int last_decoded_CTB_RS
Definition: decctx.h:170
NAL_unit * nal
Definition: decctx.h:150
de265_progress_lock finished_threads
Definition: decctx.h:166
slice_unit(decoder_context *decctx)
image_unit * imgunit
Definition: decctx.h:154
decoder_context * ctx
Definition: decctx.h:185
SliceDecodingProgress
Definition: decctx.h:161
@ Decoded
Definition: decctx.h:163
@ Unprocessed
Definition: decctx.h:161
@ InProgress
Definition: decctx.h:162
enum slice_unit::SliceDecodingProgress state
void allocate_thread_contexts(int n)
int nThreads
Definition: decctx.h:167
thread_context * get_thread_context(int n)
Definition: decctx.h:173
Definition: decctx.h:54
slice_segment_header * shdr
Definition: decctx.h:114
image_unit * imgunit
Definition: decctx.h:116
uint8_t transform_skip_flag[3]
Definition: decctx.h:78
int32_t residual_luma[32 *32]
Definition: decctx.h:91
int IsCuChromaQpOffsetCoded
Definition: decctx.h:98
int IsCuQpDeltaCoded
Definition: decctx.h:96
int ResScaleVal
Definition: decctx.h:72
struct de265_image * img
Definition: decctx.h:113
int16_t _coeffBuf[(32 *32)+8]
Definition: decctx.h:84
int CtbX
Definition: decctx.h:61
context_model_table ctx_model
Definition: decctx.h:109
int qPYPrime
Definition: decctx.h:105
PBMotionCoding motion
Definition: decctx.h:66
int qPCbPrime
Definition: decctx.h:105
int CuQpOffsetCr
Definition: decctx.h:99
uint8_t StatCoeff[4]
Definition: decctx.h:110
slice_unit * sliceunit
Definition: decctx.h:117
int lastQPYinPreviousQG
Definition: decctx.h:103
int currentQG_y
Definition: decctx.h:102
int CtbY
Definition: decctx.h:61
uint8_t cu_transquant_bypass_flag
Definition: decctx.h:77
int currentQPY
Definition: decctx.h:101
int16_t * coeffBuf
Definition: decctx.h:85
int CuQpDelta
Definition: decctx.h:97
CABAC_decoder cabac_decoder
Definition: decctx.h:107
uint8_t explicit_rdpcm_dir
Definition: decctx.h:80
int16_t coeffList[3][32 *32]
Definition: decctx.h:87
decoder_context * decctx
Definition: decctx.h:112
int16_t nCoeff[3]
Definition: decctx.h:89
int currentQG_x
Definition: decctx.h:102
int qPCrPrime
Definition: decctx.h:105
int16_t coeffPos[3][32 *32]
Definition: decctx.h:88
int CtbAddrInRS
Definition: decctx.h:58
int CuQpOffsetCb
Definition: decctx.h:99
thread_task * task
Definition: decctx.h:118
int CtbAddrInTS
Definition: decctx.h:59
uint8_t explicit_rdpcm_flag
Definition: decctx.h:79
Definition: threads.h:124
Definition: threads.h:102
de265_acceleration
Definition: de265.h:391
int64_t de265_PTS
Definition: de265.h:166
de265_error
Definition: de265.h:82
#define MAX_WARNINGS
Definition: decctx.h:44
#define DE265_MAX_SPS_SETS
Definition: decctx.h:41
#define DE265_MAX_VPS_SETS
Definition: decctx.h:40
#define DE265_MAX_PPS_SETS
Definition: decctx.h:42
QStringView suffix
Definition: itemviewutilities.cpp:594
#define MAX_NUM_REF_PICS
Definition: refpic.h:26
Definition: cabac.h:28
Definition: acceleration.h:30
Definition: bitstream.h:39
Definition: de265.h:326
Definition: image.h:222
Definition: nal.h:36