Coverage Report

Created: 2026-05-16 06:41

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