Coverage Report

Created: 2026-02-26 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/libde265/libde265/decctx.cc
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
#include "decctx.h"
22
#include "util.h"
23
#include "sao.h"
24
#include "sei.h"
25
#include "deblock.h"
26
27
#include <string.h>
28
#include <assert.h>
29
#include <stdlib.h>
30
#include <stdio.h>
31
#include <math.h>
32
33
#include "fallback.h"
34
35
#ifdef HAVE_CONFIG_H
36
#include "config.h"
37
#endif
38
39
#ifdef HAVE_SSE4_1
40
#include "x86/sse.h"
41
#endif
42
43
#ifdef HAVE_ARM
44
#include "arm/arm.h"
45
#endif
46
47
#define SAVE_INTERMEDIATE_IMAGES 0
48
49
#if SAVE_INTERMEDIATE_IMAGES
50
#include "visualize.h"
51
#endif
52
53
extern void thread_decode_CTB_row(void* d);
54
extern void thread_decode_slice_segment(void* d);
55
56
57
thread_context::thread_context()
58
0
{
59
  /*
60
  CtbAddrInRS = 0;
61
  CtbAddrInTS = 0;
62
63
  CtbX = 0;
64
  CtbY = 0;
65
  */
66
67
  /*
68
  refIdx[0] = refIdx[1] = 0;
69
  mvd[0][0] = mvd[0][1] = mvd[1][0] = mvd[1][1] = 0;
70
  merge_flag = 0;
71
  merge_idx = 0;
72
  mvp_lX_flag[0] = mvp_lX_flag[1] = 0;
73
  inter_pred_idc = 0;
74
  */
75
76
  /*
77
  enum IntraPredMode IntraPredModeC; // chroma intra-prediction mode for current CB
78
  */
79
80
  /*
81
  cu_transquant_bypass_flag = false;
82
  memset(transform_skip_flag,0, 3*sizeof(uint8_t));
83
  */
84
85
86
  //memset(coeffList,0,sizeof(int16_t)*3*32*32);
87
  //memset(coeffPos,0,sizeof(int16_t)*3*32*32);
88
  //memset(nCoeff,0,sizeof(int16_t)*3);
89
90
91
92
0
  IsCuQpDeltaCoded = false;
93
0
  CuQpDelta = 0;
94
95
0
  IsCuChromaQpOffsetCoded = false;
96
0
  CuQpOffsetCb = 0;
97
0
  CuQpOffsetCr = 0;
98
99
  /*
100
  currentQPY = 0;
101
  currentQG_x = 0;
102
  currentQG_y = 0;
103
  lastQPYinPreviousQG = 0;
104
  */
105
106
  /*
107
  qPYPrime = 0;
108
  qPCbPrime = 0;
109
  qPCrPrime = 0;
110
  */
111
112
  /*
113
  memset(&cabac_decoder, 0, sizeof(CABAC_decoder));
114
  memset(&ctx_model, 0, sizeof(ctx_model));
115
  */
116
117
0
  decctx = NULL;
118
0
  img = NULL;
119
0
  shdr = NULL;
120
121
0
  imgunit = NULL;
122
0
  sliceunit = NULL;
123
124
125
  //memset(this,0,sizeof(thread_context));
126
127
  // There is a interesting issue here. When aligning _coeffBuf to 16 bytes offset with
128
  // __attribute__((align(16))), the following statement is optimized away since the
129
  // compiler assumes that the pointer would be 16-byte aligned. However, this is not the
130
  // case when the structure has been dynamically allocated. In this case, the base can
131
  // also be at 8 byte offsets (at least with MingW,32 bit).
132
0
  int offset = ((uintptr_t)_coeffBuf) & 0xf;
133
134
0
  if (offset == 0) {
135
0
    coeffBuf = _coeffBuf;  // correctly aligned already
136
0
  }
137
0
  else {
138
0
    coeffBuf = (int16_t *) (((uint8_t *)_coeffBuf) + (16-offset));
139
0
  }
140
141
0
  memset(coeffBuf, 0, 32*32*sizeof(int16_t));
142
0
}
143
144
145
slice_unit::slice_unit(decoder_context* decctx)
146
0
  : nal(NULL),
147
0
    shdr(NULL),
148
0
    imgunit(NULL),
149
0
    flush_reorder_buffer(false),
150
0
    nThreads(0),
151
0
    first_decoded_CTB_RS(-1),
152
0
    last_decoded_CTB_RS(-1),
153
0
    thread_contexts(NULL),
154
0
    ctx(decctx)
155
0
{
156
0
  state = Unprocessed;
157
0
  nThreadContexts = 0;
158
0
}
159
160
slice_unit::~slice_unit()
161
0
{
162
0
  ctx->nal_parser.free_NAL_unit(nal);
163
164
0
  if (thread_contexts) {
165
0
    delete[] thread_contexts;
166
0
  }
167
0
}
168
169
170
void slice_unit::allocate_thread_contexts(int n)
171
0
{
172
0
  assert(thread_contexts==NULL);
173
174
0
  thread_contexts = new thread_context[n];
175
0
  nThreadContexts = n;
176
0
}
177
178
179
image_unit::image_unit()
180
0
{
181
0
  img=NULL;
182
0
  role=Invalid;
183
0
  state=Unprocessed;
184
0
}
185
186
187
image_unit::~image_unit()
188
0
{
189
0
  for (size_t i=0;i<slice_units.size();i++) {
190
0
    delete slice_units[i];
191
0
  }
192
193
0
  for (size_t i=0;i<tasks.size();i++) {
194
0
    delete tasks[i];
195
0
  }
196
0
}
197
198
199
base_context::base_context()
200
0
{
201
0
  set_acceleration_functions(de265_acceleration_AUTO);
202
0
}
203
204
205
decoder_context::decoder_context()
206
0
{
207
  //memset(ctx, 0, sizeof(decoder_context));
208
209
  // --- parameters ---
210
211
0
  param_sei_check_hash = false;
212
0
  param_conceal_stream_errors = true;
213
0
  param_suppress_faulty_pictures = false;
214
215
0
  param_disable_deblocking = false;
216
0
  param_disable_sao = false;
217
  //param_disable_mc_residual_idct = false;
218
  //param_disable_intra_residual_idct = false;
219
220
  // --- processing ---
221
222
0
  param_sps_headers_fd = -1;
223
0
  param_vps_headers_fd = -1;
224
0
  param_pps_headers_fd = -1;
225
0
  param_slice_headers_fd = -1;
226
227
0
  param_image_allocation_functions = de265_image::default_image_allocation;
228
0
  param_image_allocation_userdata  = NULL;
229
230
  /*
231
  memset(&vps, 0, sizeof(video_parameter_set)*DE265_MAX_VPS_SETS);
232
  memset(&sps, 0, sizeof(seq_parameter_set)  *DE265_MAX_SPS_SETS);
233
  memset(&pps, 0, sizeof(pic_parameter_set)  *DE265_MAX_PPS_SETS);
234
  memset(&slice,0,sizeof(slice_segment_header)*DE265_MAX_SLICES);
235
  */
236
237
0
  current_vps = NULL;
238
0
  current_sps = NULL;
239
0
  current_pps = NULL;
240
241
  //memset(&thread_pool,0,sizeof(struct thread_pool));
242
0
  num_worker_threads = 0;
243
244
245
  // frame-rate
246
247
0
  limit_HighestTid = 6;   // decode all temporal layers (up to layer 6)
248
0
  framerate_ratio = 100;  // decode all 100%
249
250
0
  goal_HighestTid = 6;
251
0
  current_HighestTid = 6;
252
0
  layer_framerate_ratio = 100;
253
254
0
  compute_framedrop_table();
255
256
257
  //
258
259
0
  current_image_poc_lsb = 0;
260
0
  first_decoded_picture = 0;
261
0
  NoRaslOutputFlag = 0;
262
0
  HandleCraAsBlaFlag = 0;
263
0
  FirstAfterEndOfSequenceNAL = 0;
264
0
  PicOrderCntMsb = 0;
265
0
  prevPicOrderCntLsb = 0;
266
0
  prevPicOrderCntMsb = 0;
267
0
  img = NULL;
268
0
  previous_slice_header = nullptr;
269
270
  /*
271
  int PocLsbLt[MAX_NUM_REF_PICS];
272
  int UsedByCurrPicLt[MAX_NUM_REF_PICS];
273
  int DeltaPocMsbCycleLt[MAX_NUM_REF_PICS];
274
  int CurrDeltaPocMsbPresentFlag[MAX_NUM_REF_PICS];
275
  int FollDeltaPocMsbPresentFlag[MAX_NUM_REF_PICS];
276
277
  int NumPocStCurrBefore;
278
  int NumPocStCurrAfter;
279
  int NumPocStFoll;
280
  int NumPocLtCurr;
281
  int NumPocLtFoll;
282
283
  // These lists contain absolute POC values.
284
  int PocStCurrBefore[MAX_NUM_REF_PICS]; // used for reference in current picture, smaller POC
285
  int PocStCurrAfter[MAX_NUM_REF_PICS];  // used for reference in current picture, larger POC
286
  int PocStFoll[MAX_NUM_REF_PICS]; // not used for reference in current picture, but in future picture
287
  int PocLtCurr[MAX_NUM_REF_PICS]; // used in current picture
288
  int PocLtFoll[MAX_NUM_REF_PICS]; // used in some future picture
289
290
  // These lists contain indices into the DPB.
291
  int RefPicSetStCurrBefore[DE265_DPB_SIZE];
292
  int RefPicSetStCurrAfter[DE265_DPB_SIZE];
293
  int RefPicSetStFoll[DE265_DPB_SIZE];
294
  int RefPicSetLtCurr[DE265_DPB_SIZE];
295
  int RefPicSetLtFoll[DE265_DPB_SIZE];
296
297
298
  uint8_t nal_unit_type;
299
300
  char IdrPicFlag;
301
  char RapPicFlag;
302
  */
303
304
305
306
  // --- internal data ---
307
308
0
  first_decoded_picture = true;
309
  //ctx->FirstAfterEndOfSequenceNAL = true;
310
  //ctx->last_RAP_picture_NAL_type = NAL_UNIT_UNDEFINED;
311
312
  //de265_init_image(&ctx->coeff);
313
314
  // --- decoded picture buffer ---
315
316
0
  current_image_poc_lsb = -1; // any invalid number
317
0
}
318
319
320
decoder_context::~decoder_context()
321
0
{
322
0
  while (!image_units.empty()) {
323
0
    delete image_units.back();
324
0
    image_units.pop_back();
325
0
  }
326
0
}
327
328
329
void decoder_context::set_image_allocation_functions(de265_image_allocation* allocfunc,
330
                                                     void* userdata)
331
0
{
332
0
  if (allocfunc) {
333
0
    param_image_allocation_functions = *allocfunc;
334
0
    param_image_allocation_userdata  = userdata;
335
0
  }
336
0
  else {
337
0
    assert(false); // actually, it makes no sense to reset the allocation functions
338
339
0
    param_image_allocation_functions = de265_image::default_image_allocation;
340
0
    param_image_allocation_userdata  = NULL;
341
0
  }
342
0
}
343
344
345
de265_error decoder_context::start_thread_pool(int nThreads)
346
0
{
347
0
  ::start_thread_pool(&thread_pool_, nThreads);
348
349
0
  num_worker_threads = nThreads;
350
351
0
  return DE265_OK;
352
0
}
353
354
355
void decoder_context::stop_thread_pool()
356
0
{
357
0
  if (get_num_worker_threads()>0) {
358
    //flush_thread_pool(&ctx->thread_pool);
359
0
    ::stop_thread_pool(&thread_pool_);
360
0
  }
361
0
}
362
363
364
void decoder_context::reset()
365
0
{
366
0
  if (num_worker_threads>0) {
367
    //flush_thread_pool(&ctx->thread_pool);
368
0
    ::stop_thread_pool(&thread_pool_);
369
0
  }
370
371
  // --------------------------------------------------
372
373
#if 0
374
  ctx->end_of_stream = false;
375
  ctx->pending_input_NAL = NULL;
376
  ctx->current_vps = NULL;
377
  ctx->current_sps = NULL;
378
  ctx->current_pps = NULL;
379
  ctx->num_worker_threads = 0;
380
  ctx->current_image_poc_lsb = 0;
381
  ctx->first_decoded_picture = 0;
382
  ctx->NoRaslOutputFlag = 0;
383
  ctx->HandleCraAsBlaFlag = 0;
384
  ctx->FirstAfterEndOfSequenceNAL = 0;
385
  ctx->PicOrderCntMsb = 0;
386
  ctx->prevPicOrderCntLsb = 0;
387
  ctx->prevPicOrderCntMsb = 0;
388
  ctx->NumPocStCurrBefore=0;
389
  ctx->NumPocStCurrAfter=0;
390
  ctx->NumPocStFoll=0;
391
  ctx->NumPocLtCurr=0;
392
  ctx->NumPocLtFoll=0;
393
  ctx->nal_unit_type=0;
394
  ctx->IdrPicFlag=0;
395
  ctx->RapPicFlag=0;
396
#endif
397
398
0
  img = NULL;
399
400
401
  // TODO: remove all pending image_units
402
403
404
  // --- decoded picture buffer ---
405
406
0
  current_image_poc_lsb = -1; // any invalid number
407
0
  first_decoded_picture = true;
408
409
410
  // --- remove all pictures from output queue ---
411
412
  // there was a bug the peek_next_image did not return NULL on empty output queues.
413
  // This was (indirectly) fixed by recreating the DPB buffer, but it should actually
414
  // be sufficient to clear it like this.
415
  // The error showed while scrubbing the ToS video in VLC.
416
0
  dpb.clear();
417
418
0
  nal_parser.remove_pending_input_data();
419
420
421
0
  while (!image_units.empty()) {
422
0
    delete image_units.back();
423
0
    image_units.pop_back();
424
0
  }
425
426
  // --- start threads again ---
427
428
0
  if (num_worker_threads>0) {
429
    // TODO: need error checking
430
0
    start_thread_pool(num_worker_threads);
431
0
  }
432
0
}
433
434
void base_context::set_acceleration_functions(enum de265_acceleration l)
435
0
{
436
  // fill scalar functions first (so that function table is completely filled)
437
438
0
  init_acceleration_functions_fallback(&acceleration);
439
440
441
  // override functions with optimized variants
442
443
0
#ifdef HAVE_SSE4_1
444
0
  if (l>=de265_acceleration_SSE) {
445
0
    init_acceleration_functions_sse(&acceleration);
446
0
  }
447
0
#endif
448
#ifdef HAVE_ARM
449
  if (l>=de265_acceleration_ARM) {
450
    init_acceleration_functions_arm(&acceleration);
451
  }
452
#endif
453
0
}
454
455
456
void decoder_context::init_thread_context(thread_context* tctx)
457
0
{
458
  // zero scrap memory for coefficient blocks
459
0
  memset(tctx->_coeffBuf, 0, sizeof(tctx->_coeffBuf));  // TODO: check if we can safely remove this
460
461
0
  tctx->currentQG_x = -1;
462
0
  tctx->currentQG_y = -1;
463
464
465
466
  // --- find QPY that was active at the end of the previous slice ---
467
468
  // find the previous CTB in TS order
469
470
0
  const pic_parameter_set& pps = tctx->img->get_pps();
471
0
  const seq_parameter_set& sps = tctx->img->get_sps();
472
473
474
0
  if (tctx->shdr->slice_segment_address > 0) {
475
0
    int prevCtb = pps.CtbAddrTStoRS[ pps.CtbAddrRStoTS[tctx->shdr->slice_segment_address] -1 ];
476
477
0
    int ctbX = prevCtb % sps.PicWidthInCtbsY;
478
0
    int ctbY = prevCtb / sps.PicWidthInCtbsY;
479
480
481
    // take the pixel at the bottom right corner (but consider that the image size might be smaller)
482
483
0
    int x = ((ctbX+1) << sps.Log2CtbSizeY)-1;
484
0
    int y = ((ctbY+1) << sps.Log2CtbSizeY)-1;
485
486
0
    x = std::min(x,sps.pic_width_in_luma_samples-1);
487
0
    y = std::min(y,sps.pic_height_in_luma_samples-1);
488
489
    //printf("READ QPY: %d %d -> %d (should %d)\n",x,y,imgunit->img->get_QPY(x,y), tc.currentQPY);
490
491
    //if (tctx->shdr->dependent_slice_segment_flag) {  // TODO: do we need this condition ?
492
0
    tctx->currentQPY = tctx->img->get_QPY(x,y);
493
      //}
494
0
  }
495
0
}
496
497
498
void decoder_context::add_task_decode_CTB_row(thread_context* tctx,
499
                                              bool firstSliceSubstream,
500
                                              int ctbRow)
501
0
{
502
0
  thread_task_ctb_row* task = new thread_task_ctb_row;
503
0
  task->firstSliceSubstream = firstSliceSubstream;
504
0
  task->tctx = tctx;
505
0
  task->debug_startCtbRow = ctbRow;
506
0
  tctx->task = task;
507
508
0
  add_task(&thread_pool_, task);
509
510
0
  tctx->imgunit->tasks.push_back(task);
511
0
}
512
513
514
void decoder_context::add_task_decode_slice_segment(thread_context* tctx, bool firstSliceSubstream,
515
                                                    int ctbx,int ctby)
516
0
{
517
0
  thread_task_slice_segment* task = new thread_task_slice_segment;
518
0
  task->firstSliceSubstream = firstSliceSubstream;
519
0
  task->tctx = tctx;
520
0
  task->debug_startCtbX = ctbx;
521
0
  task->debug_startCtbY = ctby;
522
0
  tctx->task = task;
523
524
0
  add_task(&thread_pool_, task);
525
526
0
  tctx->imgunit->tasks.push_back(task);
527
0
}
528
529
530
de265_error decoder_context::read_vps_NAL(bitreader& reader)
531
0
{
532
0
  logdebug(LogHeaders,"---> read VPS\n");
533
534
0
  std::shared_ptr<video_parameter_set> new_vps = std::make_shared<video_parameter_set>();
535
0
  de265_error err = new_vps->read(this,&reader);
536
0
  if (err != DE265_OK) {
537
0
    return err;
538
0
  }
539
540
0
  if (param_vps_headers_fd>=0) {
541
0
    new_vps->dump(param_vps_headers_fd);
542
0
  }
543
544
0
  vps[ new_vps->video_parameter_set_id ] = new_vps;
545
546
0
  return DE265_OK;
547
0
}
548
549
de265_error decoder_context::read_sps_NAL(bitreader& reader)
550
0
{
551
0
  logdebug(LogHeaders,"----> read SPS\n");
552
553
0
  std::shared_ptr<seq_parameter_set> new_sps = std::make_shared<seq_parameter_set>();
554
0
  de265_error err;
555
556
0
  if ((err=new_sps->read(this, &reader)) != DE265_OK) {
557
0
    return err;
558
0
  }
559
560
0
  if (param_sps_headers_fd>=0) {
561
0
    new_sps->dump(param_sps_headers_fd);
562
0
  }
563
564
0
  sps[ new_sps->seq_parameter_set_id ] = new_sps;
565
566
  // Remove the all PPS that referenced the old SPS because parameters may have changed and we do not want to
567
  // get the SPS and PPS parameters (e.g. image size) out of sync.
568
  
569
0
  for (auto& p : pps) {
570
0
    if (p && p->seq_parameter_set_id == new_sps->seq_parameter_set_id) {
571
0
      p = nullptr;
572
0
    }
573
0
  }
574
575
0
  return DE265_OK;
576
0
}
577
578
de265_error decoder_context::read_pps_NAL(bitreader& reader)
579
0
{
580
0
  logdebug(LogHeaders,"----> read PPS\n");
581
582
0
  std::shared_ptr<pic_parameter_set> new_pps = std::make_shared<pic_parameter_set>();
583
584
0
  bool success = new_pps->read(&reader,this);
585
0
  if (!success) {
586
0
    return DE265_WARNING_PPS_HEADER_INVALID;
587
0
  }
588
589
0
  if (param_pps_headers_fd>=0) {
590
0
    new_pps->dump(param_pps_headers_fd);
591
0
  }
592
593
0
  pps[ (int)new_pps->pic_parameter_set_id ] = new_pps;
594
595
0
  return DE265_OK;
596
0
}
597
598
de265_error decoder_context::read_sei_NAL(bitreader& reader, bool suffix)
599
0
{
600
0
  logdebug(LogHeaders,"----> read SEI\n");
601
602
0
  sei_message sei;
603
604
  //push_current_picture_to_output_queue();
605
606
0
  de265_error err = DE265_OK;
607
608
0
  if ((err=read_sei(&reader,&sei, suffix, current_sps.get())) == DE265_OK) {
609
0
    dump_sei(&sei, current_sps.get());
610
611
0
    if (image_units.empty()==false && suffix) {
612
0
      image_units.back()->suffix_SEIs.push_back(sei);
613
0
    }
614
0
  }
615
0
  else {
616
0
    add_warning(err, false);
617
0
  }
618
619
0
  return err;
620
0
}
621
622
de265_error decoder_context::read_eos_NAL(bitreader& reader)
623
0
{
624
0
  FirstAfterEndOfSequenceNAL = true;
625
0
  return DE265_OK;
626
0
}
627
628
de265_error decoder_context::read_slice_NAL(bitreader& reader, NAL_unit* nal, nal_header& nal_hdr)
629
0
{
630
0
  logdebug(LogHeaders,"---> read slice segment header\n");
631
632
633
  // --- read slice header ---
634
635
0
  slice_segment_header* shdr = new slice_segment_header;
636
0
  bool continueDecoding;
637
0
  de265_error err = shdr->read(&reader,this, &continueDecoding);
638
0
  if (!continueDecoding) {
639
0
    if (img) { img->integrity = INTEGRITY_NOT_DECODED; }
640
0
    nal_parser.free_NAL_unit(nal);
641
0
    delete shdr;
642
0
    return err;
643
0
  }
644
645
0
  if (param_slice_headers_fd>=0) {
646
0
    shdr->dump_slice_segment_header(this, param_slice_headers_fd);
647
0
  }
648
649
650
0
  if (process_slice_segment_header(shdr, &err, nal->pts, &nal_hdr, nal->user_data) == false)
651
0
    {
652
0
      if (img!=NULL) img->integrity = INTEGRITY_NOT_DECODED;
653
0
      nal_parser.free_NAL_unit(nal);
654
0
      delete shdr;
655
0
      return err;
656
0
    }
657
658
0
  this->img->add_slice_segment_header(shdr);
659
660
0
  skip_bits(&reader,1); // TODO: why?
661
0
  prepare_for_CABAC(&reader);
662
663
664
  // modify entry_point_offsets
665
666
0
  int headerLength = reader.data - nal->data();
667
0
  for (int i=0;i<shdr->num_entry_point_offsets;i++) {
668
0
    shdr->entry_point_offset[i] -= nal->num_skipped_bytes_before(shdr->entry_point_offset[i],
669
0
                                                                 headerLength);
670
0
  }
671
672
673
674
  // --- start a new image if this is the first slice ---
675
676
0
  if (shdr->first_slice_segment_in_pic_flag) {
677
0
    image_unit* imgunit = new image_unit;
678
0
    imgunit->img = this->img;
679
0
    image_units.push_back(imgunit);
680
0
  }
681
682
683
  // --- add slice to current picture ---
684
685
0
  if ( ! image_units.empty() ) {
686
687
0
    slice_unit* sliceunit = new slice_unit(this);
688
0
    sliceunit->nal = nal;
689
0
    sliceunit->shdr = shdr;
690
0
    sliceunit->reader = reader;
691
692
0
    sliceunit->flush_reorder_buffer = flush_reorder_buffer_at_this_frame;
693
694
695
0
    image_units.back()->slice_units.push_back(sliceunit);
696
0
  }
697
0
  else {
698
0
    nal_parser.free_NAL_unit(nal);
699
0
  }
700
701
0
  bool did_work;
702
0
  err = decode_some(&did_work);
703
704
0
  return DE265_OK;
705
0
}
706
707
708
template <class T> void pop_front(std::vector<T>& vec)
709
0
{
710
0
  for (size_t i=1;i<vec.size();i++)
711
0
    vec[i-1] = vec[i];
712
713
0
  vec.pop_back();
714
0
}
715
716
717
de265_error decoder_context::decode_some(bool* did_work)
718
0
{
719
0
  de265_error err = DE265_OK;
720
721
0
  *did_work = false;
722
723
0
  if (image_units.empty()) { return DE265_OK; }  // nothing to do
724
725
726
  // decode something if there is work to do
727
728
0
  if ( ! image_units.empty() ) { // && ! image_units[0]->slice_units.empty() ) {
729
730
0
    image_unit* imgunit = image_units[0];
731
0
    slice_unit* sliceunit = imgunit->get_next_unprocessed_slice_segment();
732
733
0
    if (sliceunit != NULL) {
734
735
      //pop_front(imgunit->slice_units);
736
737
0
      if (sliceunit->flush_reorder_buffer) {
738
0
        dpb.flush_reorder_buffer();
739
0
      }
740
741
0
      *did_work = true;
742
743
      //err = decode_slice_unit_sequential(imgunit, sliceunit);
744
0
      err = decode_slice_unit_parallel(imgunit, sliceunit);
745
0
      if (err) {
746
0
        return err;
747
0
      }
748
749
      //delete sliceunit;
750
0
    }
751
0
  }
752
753
754
755
  // if we decoded all slices of the current image and there will not
756
  // be added any more slices to the image, output the image
757
758
0
  if ( ( image_units.size()>=2 && image_units[0]->all_slice_segments_processed()) ||
759
0
       ( image_units.size()>=1 && image_units[0]->all_slice_segments_processed() &&
760
0
         nal_parser.number_of_NAL_units_pending()==0 &&
761
0
         (nal_parser.is_end_of_stream() || nal_parser.is_end_of_frame()) )) {
762
763
0
    image_unit* imgunit = image_units[0];
764
765
0
    *did_work=true;
766
767
768
    // mark all CTBs as decoded even if they are not, because faulty input
769
    // streams could miss part of the picture
770
    // TODO: this will not work when slice decoding is parallel to post-filtering,
771
    // so we will have to replace this with keeping track of which CTB should have
772
    // been decoded (but aren't because of the input stream being faulty)
773
774
0
    imgunit->img->mark_all_CTB_progress(CTB_PROGRESS_PREFILTER);
775
776
777
778
    // run post-processing filters (deblocking & SAO)
779
780
0
    if (img->decctx->num_worker_threads)
781
0
      run_postprocessing_filters_parallel(imgunit);
782
0
    else
783
0
      run_postprocessing_filters_sequential(imgunit->img);
784
785
    // process suffix SEIs
786
787
0
    for (size_t i=0;i<imgunit->suffix_SEIs.size();i++) {
788
0
      const sei_message& sei = imgunit->suffix_SEIs[i];
789
790
0
      err = process_sei(&sei, imgunit->img);
791
0
      if (err != DE265_OK)
792
0
        break;
793
0
    }
794
795
796
0
    push_picture_to_output_queue(imgunit);
797
798
    // remove just decoded image unit from queue
799
800
0
    delete imgunit;
801
802
0
    pop_front(image_units);
803
0
  }
804
805
0
  return err;
806
0
}
807
808
809
de265_error decoder_context::decode_slice_unit_sequential(image_unit* imgunit,
810
                                                          slice_unit* sliceunit)
811
0
{
812
0
  de265_error err = DE265_OK;
813
814
  /*
815
  printf("decode slice POC=%d addr=%d, img=%p\n",
816
         sliceunit->shdr->slice_pic_order_cnt_lsb,
817
         sliceunit->shdr->slice_segment_address,
818
         imgunit->img);
819
  */
820
821
0
  remove_images_from_dpb(sliceunit->shdr->RemoveReferencesList);
822
823
0
  if (sliceunit->shdr->slice_segment_address >= imgunit->img->get_pps().CtbAddrRStoTS.size()) {
824
0
    return DE265_ERROR_CTB_OUTSIDE_IMAGE_AREA;
825
0
  }
826
827
828
0
  struct thread_context tctx;
829
830
0
  tctx.shdr = sliceunit->shdr;
831
0
  tctx.img  = imgunit->img;
832
0
  tctx.decctx = this;
833
0
  tctx.imgunit = imgunit;
834
0
  tctx.sliceunit= sliceunit;
835
0
  tctx.CtbAddrInTS = imgunit->img->get_pps().CtbAddrRStoTS[tctx.shdr->slice_segment_address];
836
0
  tctx.task = NULL;
837
838
0
  init_thread_context(&tctx);
839
840
0
  if (sliceunit->reader.bytes_remaining <= 0) {
841
0
    return DE265_ERROR_PREMATURE_END_OF_SLICE;
842
0
  }
843
844
0
  init_CABAC_decoder(&tctx.cabac_decoder,
845
0
                     sliceunit->reader.data,
846
0
                     sliceunit->reader.bytes_remaining);
847
848
  // alloc CABAC-model array if entropy_coding_sync is enabled
849
850
0
  if (imgunit->img->get_pps().entropy_coding_sync_enabled_flag &&
851
0
      sliceunit->shdr->first_slice_segment_in_pic_flag) {
852
0
    imgunit->ctx_models.resize( (img->get_sps().PicHeightInCtbsY-1) ); //* CONTEXT_MODEL_TABLE_LENGTH );
853
0
  }
854
855
0
  sliceunit->nThreads=1;
856
857
0
  err=read_slice_segment_data(&tctx);
858
859
0
  sliceunit->finished_threads.set_progress(1);
860
861
0
  return err;
862
0
}
863
864
865
void decoder_context::mark_whole_slice_as_processed(image_unit* imgunit,
866
                                                    slice_unit* sliceunit,
867
                                                    int progress)
868
0
{
869
  //printf("mark whole slice\n");
870
871
872
  // mark all CTBs upto the next slice segment as processed
873
874
0
  slice_unit* nextSegment = imgunit->get_next_slice_segment(sliceunit);
875
0
  if (nextSegment) {
876
    /*
877
    printf("mark whole slice between %d and %d\n",
878
           sliceunit->shdr->slice_segment_address,
879
           nextSegment->shdr->slice_segment_address);
880
    */
881
882
0
    for (int ctb=sliceunit->shdr->slice_segment_address;
883
0
         ctb < nextSegment->shdr->slice_segment_address;
884
0
         ctb++)
885
0
      {
886
0
        if (ctb >= imgunit->img->number_of_ctbs())
887
0
          break;
888
889
0
        imgunit->img->ctb_progress[ctb].set_progress(progress);
890
0
      }
891
0
  }
892
0
}
893
894
895
de265_error decoder_context::decode_slice_unit_parallel(image_unit* imgunit,
896
                                                        slice_unit* sliceunit)
897
0
{
898
0
  de265_error err = DE265_OK;
899
900
0
  remove_images_from_dpb(sliceunit->shdr->RemoveReferencesList);
901
902
  /*
903
  printf("-------- decode --------\n");
904
  printf("IMAGE UNIT %p\n",imgunit);
905
  sliceunit->shdr->dump_slice_segment_header(sliceunit->ctx, 1);
906
  imgunit->dump_slices();
907
  */
908
909
0
  de265_image* img = imgunit->img;
910
0
  const pic_parameter_set& pps = img->get_pps();
911
912
0
  sliceunit->state = slice_unit::InProgress;
913
914
0
  bool use_WPP = (img->decctx->num_worker_threads > 0 &&
915
0
                  pps.entropy_coding_sync_enabled_flag);
916
917
0
  bool use_tiles = (img->decctx->num_worker_threads > 0 &&
918
0
                    pps.tiles_enabled_flag);
919
920
921
  // TODO: remove this warning later when we do frame-parallel decoding
922
0
  if (img->decctx->num_worker_threads > 0 &&
923
0
      pps.entropy_coding_sync_enabled_flag == false &&
924
0
      pps.tiles_enabled_flag == false) {
925
926
0
    img->decctx->add_warning(DE265_WARNING_NO_WPP_CANNOT_USE_MULTITHREADING, true);
927
0
  }
928
929
930
  // If this is the first slice segment, mark all CTBs before this as processed
931
  // (the real first slice segment could be missing).
932
933
0
  if (imgunit->is_first_slice_segment(sliceunit)) {
934
0
    slice_segment_header* shdr = sliceunit->shdr;
935
0
    int firstCTB = shdr->slice_segment_address;
936
937
0
    for (int ctb=0;ctb<firstCTB;ctb++) {
938
      //printf("mark pre progress %d\n",ctb);
939
0
      img->ctb_progress[ctb].set_progress(CTB_PROGRESS_PREFILTER);
940
0
    }
941
0
  }
942
943
944
  // if there is a previous slice that has been completely decoded,
945
  // mark all CTBs until the start of this slice as completed
946
947
  //printf("this slice: %p\n",sliceunit);
948
0
  slice_unit* prevSlice = imgunit->get_prev_slice_segment(sliceunit);
949
  //if (prevSlice) printf("prev slice state: %d\n",prevSlice->state);
950
0
  if (prevSlice && prevSlice->state == slice_unit::Decoded) {
951
0
    mark_whole_slice_as_processed(imgunit,prevSlice,CTB_PROGRESS_PREFILTER);
952
0
  }
953
954
955
  // TODO: even though we cannot split this into several tasks, we should run it
956
  // as a background thread
957
0
  if (!use_WPP && !use_tiles) {
958
    //printf("SEQ\n");
959
0
    err = decode_slice_unit_sequential(imgunit, sliceunit);
960
0
    sliceunit->state = slice_unit::Decoded;
961
0
    mark_whole_slice_as_processed(imgunit,sliceunit,CTB_PROGRESS_PREFILTER);
962
0
    return err;
963
0
  }
964
965
966
0
  if (use_WPP && use_tiles) {
967
    // TODO: this is not allowed ... output some warning or error
968
969
0
    return DE265_WARNING_PPS_HEADER_INVALID;
970
0
  }
971
972
973
0
  if (use_WPP) {
974
    //printf("WPP\n");
975
0
    err = decode_slice_unit_WPP(imgunit, sliceunit);
976
0
    sliceunit->state = slice_unit::Decoded;
977
0
    mark_whole_slice_as_processed(imgunit,sliceunit,CTB_PROGRESS_PREFILTER);
978
0
    return err;
979
0
  }
980
0
  else if (use_tiles) {
981
    //printf("TILE\n");
982
0
    err = decode_slice_unit_tiles(imgunit, sliceunit);
983
0
    sliceunit->state = slice_unit::Decoded;
984
0
    mark_whole_slice_as_processed(imgunit,sliceunit,CTB_PROGRESS_PREFILTER);
985
0
    return err;
986
0
  }
987
988
0
  assert(false);
989
0
  return err;
990
0
}
991
992
993
de265_error decoder_context::decode_slice_unit_WPP(image_unit* imgunit,
994
                                                   slice_unit* sliceunit)
995
0
{
996
0
  de265_error err = DE265_OK;
997
998
0
  de265_image* img = imgunit->img;
999
0
  slice_segment_header* shdr = sliceunit->shdr;
1000
0
  const pic_parameter_set& pps = img->get_pps();
1001
1002
0
  int nRows = shdr->num_entry_point_offsets +1;
1003
0
  int ctbsWidth = img->get_sps().PicWidthInCtbsY;
1004
1005
1006
0
  assert(img->num_threads_active() == 0);
1007
1008
1009
  // reserve space to store entropy coding context models for each CTB row
1010
1011
0
  if (shdr->first_slice_segment_in_pic_flag) {
1012
    // reserve space for nRows-1 because we don't need to save the CABAC model in the last CTB row
1013
0
    imgunit->ctx_models.resize( (img->get_sps().PicHeightInCtbsY-1) ); //* CONTEXT_MODEL_TABLE_LENGTH );
1014
0
  }
1015
1016
1017
0
  sliceunit->allocate_thread_contexts(nRows);
1018
1019
1020
  // first CTB in this slice
1021
0
  int ctbAddrRS = shdr->slice_segment_address;
1022
0
  int ctbRow    = ctbAddrRS / ctbsWidth;
1023
1024
0
  for (int entryPt=0;entryPt<nRows;entryPt++) {
1025
    // entry points other than the first start at CTB rows
1026
0
    if (entryPt>0) {
1027
0
      ctbRow++;
1028
0
      ctbAddrRS = ctbRow * ctbsWidth;
1029
0
    }
1030
0
    else if (nRows>1 && (ctbAddrRS % ctbsWidth) != 0) {
1031
      // If slice segment consists of several WPP rows, each of them
1032
      // has to start at a row.
1033
1034
      //printf("does not start at start\n");
1035
1036
0
      err = DE265_WARNING_SLICEHEADER_INVALID;
1037
0
      break;
1038
0
    }
1039
1040
1041
    // prepare thread context
1042
1043
0
    thread_context* tctx = sliceunit->get_thread_context(entryPt);
1044
1045
0
    tctx->shdr    = shdr;
1046
0
    tctx->decctx  = img->decctx;
1047
0
    tctx->img     = img;
1048
0
    tctx->imgunit = imgunit;
1049
0
    tctx->sliceunit= sliceunit;
1050
0
    tctx->CtbAddrInTS = pps.CtbAddrRStoTS[ctbAddrRS];
1051
1052
0
    init_thread_context(tctx);
1053
1054
1055
    // init CABAC
1056
1057
0
    int dataStartIndex;
1058
0
    if (entryPt==0) { dataStartIndex=0; }
1059
0
    else            { dataStartIndex=shdr->entry_point_offset[entryPt-1]; }
1060
1061
0
    int dataEnd;
1062
0
    if (entryPt==nRows-1) dataEnd = sliceunit->reader.bytes_remaining;
1063
0
    else                  dataEnd = shdr->entry_point_offset[entryPt];
1064
1065
0
    if (dataStartIndex<0 || dataEnd>sliceunit->reader.bytes_remaining ||
1066
0
        dataEnd <= dataStartIndex) {
1067
      //printf("WPP premature end\n");
1068
0
      err = DE265_ERROR_PREMATURE_END_OF_SLICE;
1069
0
      break;
1070
0
    }
1071
1072
0
    init_CABAC_decoder(&tctx->cabac_decoder,
1073
0
                       &sliceunit->reader.data[dataStartIndex],
1074
0
                       dataEnd-dataStartIndex);
1075
1076
    // add task
1077
1078
    //printf("start task for ctb-row: %d\n",ctbRow);
1079
0
    img->thread_start(1);
1080
0
    sliceunit->nThreads++;
1081
0
    add_task_decode_CTB_row(tctx, entryPt==0, ctbRow);
1082
0
  }
1083
1084
#if 0
1085
  for (;;) {
1086
    printf("q:%d r:%d b:%d f:%d\n",
1087
           img->nThreadsQueued,
1088
           img->nThreadsRunning,
1089
           img->nThreadsBlocked,
1090
           img->nThreadsFinished);
1091
1092
    if (img->debug_is_completed()) break;
1093
1094
    usleep(1000);
1095
  }
1096
#endif
1097
1098
0
  img->wait_for_completion();
1099
1100
0
  for (size_t i=0;i<imgunit->tasks.size();i++)
1101
0
    delete imgunit->tasks[i];
1102
0
  imgunit->tasks.clear();
1103
1104
0
  return DE265_OK;
1105
0
}
1106
1107
de265_error decoder_context::decode_slice_unit_tiles(image_unit* imgunit,
1108
                                                     slice_unit* sliceunit)
1109
0
{
1110
0
  de265_error err = DE265_OK;
1111
1112
0
  de265_image* img = imgunit->img;
1113
0
  slice_segment_header* shdr = sliceunit->shdr;
1114
0
  const pic_parameter_set& pps = img->get_pps();
1115
1116
0
  int nTiles = shdr->num_entry_point_offsets +1;
1117
0
  int ctbsWidth = img->get_sps().PicWidthInCtbsY;
1118
1119
1120
0
  assert(img->num_threads_active() == 0);
1121
1122
0
  sliceunit->allocate_thread_contexts(nTiles);
1123
1124
1125
  // first CTB in this slice
1126
0
  int ctbAddrRS = shdr->slice_segment_address;
1127
0
  int tileID = pps.TileIdRS[ctbAddrRS];
1128
1129
0
  for (int entryPt=0;entryPt<nTiles;entryPt++) {
1130
    // entry points other than the first start at tile beginnings
1131
0
    if (entryPt>0) {
1132
0
      tileID++;
1133
1134
0
      if (tileID >= pps.num_tile_columns * pps.num_tile_rows) {
1135
0
        err = DE265_WARNING_SLICEHEADER_INVALID;
1136
0
        break;
1137
0
      }
1138
1139
0
      int ctbX = pps.colBd[tileID % pps.num_tile_columns];
1140
0
      int ctbY = pps.rowBd[tileID / pps.num_tile_columns];
1141
0
      ctbAddrRS = ctbY * ctbsWidth + ctbX;
1142
0
    }
1143
1144
    // set thread context
1145
1146
0
    thread_context* tctx = sliceunit->get_thread_context(entryPt);
1147
1148
0
    tctx->shdr   = shdr;
1149
0
    tctx->decctx = img->decctx;
1150
0
    tctx->img    = img;
1151
0
    tctx->imgunit = imgunit;
1152
0
    tctx->sliceunit= sliceunit;
1153
0
    tctx->CtbAddrInTS = pps.CtbAddrRStoTS[ctbAddrRS];
1154
1155
0
    init_thread_context(tctx);
1156
1157
1158
    // init CABAC
1159
1160
0
    int dataStartIndex;
1161
0
    if (entryPt==0) { dataStartIndex=0; }
1162
0
    else            { dataStartIndex=shdr->entry_point_offset[entryPt-1]; }
1163
1164
0
    int dataEnd;
1165
0
    if (entryPt==nTiles-1) dataEnd = sliceunit->reader.bytes_remaining;
1166
0
    else                   dataEnd = shdr->entry_point_offset[entryPt];
1167
1168
0
    if (dataStartIndex<0 || dataEnd>sliceunit->reader.bytes_remaining ||
1169
0
        dataEnd <= dataStartIndex) {
1170
0
      err = DE265_ERROR_PREMATURE_END_OF_SLICE;
1171
0
      break;
1172
0
    }
1173
1174
0
    init_CABAC_decoder(&tctx->cabac_decoder,
1175
0
                       &sliceunit->reader.data[dataStartIndex],
1176
0
                       dataEnd-dataStartIndex);
1177
1178
    // add task
1179
1180
    //printf("add tiles thread\n");
1181
0
    img->thread_start(1);
1182
0
    sliceunit->nThreads++;
1183
0
    add_task_decode_slice_segment(tctx, entryPt==0,
1184
0
                                  ctbAddrRS % ctbsWidth,
1185
0
                                  ctbAddrRS / ctbsWidth);
1186
0
  }
1187
1188
0
  img->wait_for_completion();
1189
1190
0
  for (size_t i=0;i<imgunit->tasks.size();i++)
1191
0
    delete imgunit->tasks[i];
1192
0
  imgunit->tasks.clear();
1193
1194
0
  return err;
1195
0
}
1196
1197
1198
de265_error decoder_context::decode_NAL(NAL_unit* nal)
1199
0
{
1200
  //return decode_NAL_OLD(nal);
1201
1202
0
  decoder_context* ctx = this;
1203
1204
0
  de265_error err = DE265_OK;
1205
1206
0
  bitreader reader;
1207
0
  bitreader_init(&reader, nal->data(), nal->size());
1208
1209
0
  nal_header nal_hdr;
1210
0
  nal_hdr.read(&reader);
1211
0
  ctx->process_nal_hdr(&nal_hdr);
1212
1213
0
  if (nal_hdr.nuh_layer_id > 0) {
1214
    // Discard all NAL units with nuh_layer_id > 0
1215
    // These will have to be handled by an SHVC decoder.
1216
0
    nal_parser.free_NAL_unit(nal);
1217
0
    return DE265_OK;
1218
0
  }
1219
1220
0
  loginfo(LogHighlevel,"NAL: 0x%x 0x%x -  unit type:%s temporal id:%d\n",
1221
0
          nal->data()[0], nal->data()[1],
1222
0
          get_NAL_name(nal_hdr.nal_unit_type),
1223
0
          nal_hdr.nuh_temporal_id);
1224
1225
  /*
1226
    printf("NAL: 0x%x 0x%x -  unit type:%s temporal id:%d\n",
1227
    nal->data()[0], nal->data()[1],
1228
    get_NAL_name(nal_hdr.nal_unit_type),
1229
    nal_hdr.nuh_temporal_id);
1230
  */
1231
1232
  // throw away NALs from higher TIDs than currently selected
1233
  // TODO: better online switching of HighestTID
1234
1235
  //printf("hTid: %d\n", current_HighestTid);
1236
1237
0
  if (nal_hdr.nuh_temporal_id > current_HighestTid) {
1238
0
    nal_parser.free_NAL_unit(nal);
1239
0
    return DE265_OK;
1240
0
  }
1241
1242
1243
0
  if (nal_hdr.nal_unit_type<32) {
1244
0
    err = read_slice_NAL(reader, nal, nal_hdr);
1245
0
  }
1246
0
  else switch (nal_hdr.nal_unit_type) {
1247
0
    case NAL_UNIT_VPS_NUT:
1248
0
      err = read_vps_NAL(reader);
1249
0
      nal_parser.free_NAL_unit(nal);
1250
0
      break;
1251
1252
0
    case NAL_UNIT_SPS_NUT:
1253
0
      err = read_sps_NAL(reader);
1254
0
      nal_parser.free_NAL_unit(nal);
1255
0
      break;
1256
1257
0
    case NAL_UNIT_PPS_NUT:
1258
0
      err = read_pps_NAL(reader);
1259
0
      nal_parser.free_NAL_unit(nal);
1260
0
      break;
1261
1262
0
    case NAL_UNIT_PREFIX_SEI_NUT:
1263
0
    case NAL_UNIT_SUFFIX_SEI_NUT:
1264
0
      err = read_sei_NAL(reader, nal_hdr.nal_unit_type==NAL_UNIT_SUFFIX_SEI_NUT);
1265
0
      nal_parser.free_NAL_unit(nal);
1266
0
      break;
1267
1268
0
    case NAL_UNIT_EOS_NUT:
1269
0
      ctx->FirstAfterEndOfSequenceNAL = true;
1270
0
      nal_parser.free_NAL_unit(nal);
1271
0
      break;
1272
1273
0
    default:
1274
0
      nal_parser.free_NAL_unit(nal);
1275
0
      break;
1276
0
    }
1277
1278
0
  return err;
1279
0
}
1280
1281
1282
de265_error decoder_context::decode(int* more)
1283
0
{
1284
0
  decoder_context* ctx = this;
1285
1286
  // if the stream has ended, and no more NALs are to be decoded, flush all pictures
1287
1288
0
  if (ctx->nal_parser.get_NAL_queue_length() == 0 &&
1289
0
      (ctx->nal_parser.is_end_of_stream() || ctx->nal_parser.is_end_of_frame()) &&
1290
0
      ctx->image_units.empty()) {
1291
1292
    // flush all pending pictures into output queue
1293
1294
    // ctx->push_current_picture_to_output_queue(); // TODO: not with new queue
1295
0
    ctx->dpb.flush_reorder_buffer();
1296
1297
0
    if (more) { *more = ctx->dpb.num_pictures_in_output_queue(); }
1298
1299
0
    return DE265_OK;
1300
0
  }
1301
1302
1303
  // if NAL-queue is empty, we need more data
1304
  // -> input stalled
1305
1306
0
  if (ctx->nal_parser.is_end_of_stream() == false &&
1307
0
      ctx->nal_parser.is_end_of_frame() == false &&
1308
0
      ctx->nal_parser.get_NAL_queue_length() == 0) {
1309
0
    if (more) { *more=1; }
1310
1311
0
    return DE265_ERROR_WAITING_FOR_INPUT_DATA;
1312
0
  }
1313
1314
1315
  // when there are no free image buffers in the DPB, pause decoding
1316
  // -> output stalled
1317
1318
0
  if (!ctx->dpb.has_free_dpb_picture(false)) {
1319
0
    if (more) *more = 1;
1320
0
    return DE265_ERROR_IMAGE_BUFFER_FULL;
1321
0
  }
1322
1323
1324
  // decode one NAL from the queue
1325
1326
0
  de265_error err = DE265_OK;
1327
0
  bool did_work = false;
1328
1329
0
  if (ctx->nal_parser.get_NAL_queue_length()) { // number_of_NAL_units_pending()) {
1330
0
    NAL_unit* nal = ctx->nal_parser.pop_from_NAL_queue();
1331
0
    assert(nal);
1332
0
    err = ctx->decode_NAL(nal);
1333
    // ctx->nal_parser.free_NAL_unit(nal); TODO: do not free NAL with new loop
1334
0
    did_work=true;
1335
0
  }
1336
0
  else if (ctx->nal_parser.is_end_of_frame() == true &&
1337
0
      ctx->image_units.empty()) {
1338
0
    if (more) { *more=1; }
1339
1340
0
    return DE265_ERROR_WAITING_FOR_INPUT_DATA;
1341
0
  }
1342
0
  else {
1343
0
    err = decode_some(&did_work);
1344
0
  }
1345
1346
0
  if (more) {
1347
    // decoding error is assumed to be unrecoverable
1348
0
    *more = (err==DE265_OK && did_work);
1349
0
  }
1350
1351
0
  return err;
1352
0
}
1353
1354
1355
void decoder_context::process_nal_hdr(nal_header* nal)
1356
0
{
1357
0
  nal_unit_type = nal->nal_unit_type;
1358
1359
0
  IdrPicFlag = isIdrPic(nal->nal_unit_type);
1360
0
  RapPicFlag = isRapPic(nal->nal_unit_type);
1361
0
}
1362
1363
1364
1365
/* 8.3.1
1366
 */
1367
void decoder_context::process_picture_order_count(slice_segment_header* hdr)
1368
0
{
1369
0
  loginfo(LogHeaders,"POC computation. lsb:%d prev.pic.lsb:%d msb:%d\n",
1370
0
          hdr->slice_pic_order_cnt_lsb,
1371
0
          prevPicOrderCntLsb,
1372
0
          PicOrderCntMsb);
1373
1374
0
  if (isIRAP(nal_unit_type) &&
1375
0
      NoRaslOutputFlag)
1376
0
    {
1377
0
      PicOrderCntMsb=0;
1378
1379
1380
      // flush all images from reorder buffer
1381
1382
0
      flush_reorder_buffer_at_this_frame = true;
1383
      //ctx->dpb.flush_reorder_buffer();
1384
0
    }
1385
0
  else
1386
0
    {
1387
0
      int MaxPicOrderCntLsb = current_sps->MaxPicOrderCntLsb;
1388
1389
0
      if ((hdr->slice_pic_order_cnt_lsb < prevPicOrderCntLsb) &&
1390
0
          (prevPicOrderCntLsb - hdr->slice_pic_order_cnt_lsb) >= MaxPicOrderCntLsb/2) {
1391
0
        PicOrderCntMsb = prevPicOrderCntMsb + MaxPicOrderCntLsb;
1392
0
      }
1393
0
      else if ((hdr->slice_pic_order_cnt_lsb > prevPicOrderCntLsb) &&
1394
0
               (hdr->slice_pic_order_cnt_lsb - prevPicOrderCntLsb) > MaxPicOrderCntLsb/2) {
1395
0
        PicOrderCntMsb = prevPicOrderCntMsb - MaxPicOrderCntLsb;
1396
0
      }
1397
0
      else {
1398
0
        PicOrderCntMsb = prevPicOrderCntMsb;
1399
0
      }
1400
0
    }
1401
1402
0
  img->PicOrderCntVal = PicOrderCntMsb + hdr->slice_pic_order_cnt_lsb;
1403
0
  img->picture_order_cnt_lsb = hdr->slice_pic_order_cnt_lsb;
1404
1405
0
  loginfo(LogHeaders,"POC computation. new msb:%d POC=%d\n",
1406
0
          PicOrderCntMsb,
1407
0
          img->PicOrderCntVal);
1408
1409
0
  if (img->nal_hdr.nuh_temporal_id==0 &&
1410
0
      !isSublayerNonReference(nal_unit_type) &&
1411
0
      !isRASL(nal_unit_type) &&
1412
0
      !isRADL(nal_unit_type))
1413
0
    {
1414
0
      loginfo(LogHeaders,"set prevPicOrderCntLsb/Msb\n");
1415
1416
0
      prevPicOrderCntLsb = hdr->slice_pic_order_cnt_lsb;
1417
0
      prevPicOrderCntMsb = PicOrderCntMsb;
1418
0
    }
1419
0
}
1420
1421
1422
/* 8.3.3.2
1423
   Returns DPB index of the generated picture.
1424
 */
1425
int decoder_context::generate_unavailable_reference_picture(const seq_parameter_set* sps,
1426
                                                            int POC, bool longTerm)
1427
0
{
1428
0
  assert(dpb.has_free_dpb_picture(true));
1429
1430
0
  std::shared_ptr<const seq_parameter_set> current_sps = this->sps[ (int)current_pps->seq_parameter_set_id ];
1431
1432
0
  int idx = dpb.new_image(current_sps, this, 0,0, false);
1433
0
  if (idx<0) {
1434
0
    return idx;
1435
0
  }
1436
1437
0
  de265_image* img = dpb.get_image(idx);
1438
1439
0
  img->fill_image(1<<(sps->BitDepth_Y-1),
1440
0
                  1<<(sps->BitDepth_C-1),
1441
0
                  1<<(sps->BitDepth_C-1));
1442
1443
0
  img->fill_pred_mode(MODE_INTRA);
1444
1445
0
  img->PicOrderCntVal = POC;
1446
0
  img->picture_order_cnt_lsb = POC & (sps->MaxPicOrderCntLsb-1);
1447
0
  img->PicOutputFlag = false;
1448
0
  img->PicState = (longTerm ? UsedForLongTermReference : UsedForShortTermReference);
1449
0
  img->integrity = INTEGRITY_UNAVAILABLE_REFERENCE;
1450
1451
0
  return idx;
1452
0
}
1453
1454
1455
/* 8.3.2   invoked once per picture
1456
1457
   This function will mark pictures in the DPB as 'unused' or 'used for long-term reference'
1458
 */
1459
de265_error decoder_context::process_reference_picture_set(slice_segment_header* hdr)
1460
0
{
1461
0
  std::vector<int> removeReferencesList;
1462
1463
0
  const int currentID = img->get_ID();
1464
1465
1466
0
  if (isIRAP(nal_unit_type) && NoRaslOutputFlag) {
1467
1468
0
    int currentPOC = img->PicOrderCntVal;
1469
1470
    // reset DPB
1471
1472
    /* The standard says: "When the current picture is an IRAP picture with NoRaslOutputFlag
1473
       equal to 1, all reference pictures currently in the DPB (if any) are marked as
1474
       "unused for reference".
1475
1476
       This seems to be wrong as it also throws out the first CRA picture in a stream like
1477
       RAP_A (decoding order: CRA,POC=64, RASL,POC=60). Removing only the pictures with
1478
       lower POCs seems to be compliant to the reference decoder.
1479
    */
1480
1481
0
    for (size_t i=0;i<dpb.size();i++) {
1482
0
      de265_image* img = dpb.get_image(i);
1483
1484
0
      if (img->PicState != UnusedForReference &&
1485
0
          img->PicOrderCntVal < currentPOC &&
1486
0
          img->removed_at_picture_id > img->get_ID()) {
1487
1488
0
        removeReferencesList.push_back(img->get_ID());
1489
0
        img->removed_at_picture_id = img->get_ID();
1490
1491
        //printf("will remove ID %d (a)\n",img->get_ID());
1492
0
      }
1493
0
    }
1494
0
  }
1495
1496
1497
0
  if (isIDR(nal_unit_type)) {
1498
1499
    // clear all reference pictures
1500
1501
0
    NumPocStCurrBefore = 0;
1502
0
    NumPocStCurrAfter = 0;
1503
0
    NumPocStFoll = 0;
1504
0
    NumPocLtCurr = 0;
1505
0
    NumPocLtFoll = 0;
1506
0
  }
1507
0
  else {
1508
0
    const ref_pic_set* rps = &hdr->CurrRps;
1509
1510
    // (8-98)
1511
1512
0
    int i,j,k;
1513
1514
    // scan ref-pic-set for smaller POCs and fill into PocStCurrBefore / PocStFoll
1515
1516
0
    for (i=0, j=0, k=0;
1517
0
         i<rps->NumNegativePics;
1518
0
         i++)
1519
0
      {
1520
0
        if (rps->UsedByCurrPicS0[i]) {
1521
0
          PocStCurrBefore[j++] = img->PicOrderCntVal + rps->DeltaPocS0[i];
1522
          //printf("PocStCurrBefore = %d\n",PocStCurrBefore[j-1]);
1523
0
        }
1524
0
        else {
1525
0
          PocStFoll[k++] = img->PicOrderCntVal + rps->DeltaPocS0[i];
1526
0
        }
1527
0
      }
1528
1529
0
    NumPocStCurrBefore = j;
1530
1531
1532
    // scan ref-pic-set for larger POCs and fill into PocStCurrAfter / PocStFoll
1533
1534
0
    for (i=0, j=0;
1535
0
         i<rps->NumPositivePics;
1536
0
         i++)
1537
0
      {
1538
0
        if (rps->UsedByCurrPicS1[i]) {
1539
0
          PocStCurrAfter[j++] = img->PicOrderCntVal + rps->DeltaPocS1[i];
1540
          //printf("PocStCurrAfter = %d\n",PocStCurrAfter[j-1]);
1541
0
        }
1542
0
        else {
1543
0
          PocStFoll[k++] = img->PicOrderCntVal + rps->DeltaPocS1[i];
1544
0
        }
1545
0
      }
1546
1547
0
    NumPocStCurrAfter = j;
1548
0
    NumPocStFoll = k;
1549
1550
1551
    // find used / future long-term references
1552
1553
0
    for (i=0, j=0, k=0;
1554
         //i<current_sps->num_long_term_ref_pics_sps + hdr->num_long_term_pics;
1555
0
         i<hdr->num_long_term_sps + hdr->num_long_term_pics;
1556
0
         i++)
1557
0
      {
1558
0
        int pocLt = PocLsbLt[i];
1559
1560
0
        if (hdr->delta_poc_msb_present_flag[i]) {
1561
0
          int currentPictureMSB = img->PicOrderCntVal - hdr->slice_pic_order_cnt_lsb;
1562
0
          pocLt += currentPictureMSB
1563
0
            - DeltaPocMsbCycleLt[i] * current_sps->MaxPicOrderCntLsb;
1564
0
        }
1565
1566
0
        if (UsedByCurrPicLt[i]) {
1567
0
          PocLtCurr[j] = pocLt;
1568
0
          CurrDeltaPocMsbPresentFlag[j] = hdr->delta_poc_msb_present_flag[i];
1569
0
          j++;
1570
0
        }
1571
0
        else {
1572
0
          PocLtFoll[k] = pocLt;
1573
0
          FollDeltaPocMsbPresentFlag[k] = hdr->delta_poc_msb_present_flag[i];
1574
0
          k++;
1575
0
        }
1576
0
      }
1577
1578
0
    NumPocLtCurr = j;
1579
0
    NumPocLtFoll = k;
1580
0
  }
1581
1582
1583
  // (old 8-99) / (new 8-106)
1584
  // 1.
1585
1586
0
  std::vector<char> picInAnyList(dpb.size(), false);
1587
1588
1589
0
  dpb.log_dpb_content();
1590
1591
0
  for (int i=0;i<NumPocLtCurr;i++) {
1592
0
    int k;
1593
0
    if (!CurrDeltaPocMsbPresentFlag[i]) {
1594
0
      k = dpb.DPB_index_of_picture_with_LSB(PocLtCurr[i], currentID, true);
1595
0
    }
1596
0
    else {
1597
0
      k = dpb.DPB_index_of_picture_with_POC(PocLtCurr[i], currentID, true);
1598
0
    }
1599
1600
0
    RefPicSetLtCurr[i] = k; // -1 == "no reference picture"
1601
0
    if (k>=0) picInAnyList[k]=true;
1602
0
    else {
1603
      // TODO, CHECK: is it ok that we generate a picture with POC = LSB (PocLtCurr)
1604
      // We do not know the correct MSB
1605
0
      int concealedPicture = generate_unavailable_reference_picture(current_sps.get(),
1606
0
                                                                    PocLtCurr[i], true);
1607
0
      if (concealedPicture<0) {
1608
0
        return (de265_error)(-concealedPicture);
1609
0
      }
1610
0
      picInAnyList.resize(dpb.size(), false); // adjust size of array to hold new picture
1611
1612
0
      RefPicSetLtCurr[i] = k = concealedPicture;
1613
0
      picInAnyList[concealedPicture]=true;
1614
0
    }
1615
1616
0
    if (dpb.get_image(k)->integrity != INTEGRITY_CORRECT) {
1617
0
      img->integrity = INTEGRITY_DERIVED_FROM_FAULTY_REFERENCE;
1618
0
    }
1619
0
  }
1620
1621
1622
0
  for (int i=0;i<NumPocLtFoll;i++) {
1623
0
    int k;
1624
0
    if (!FollDeltaPocMsbPresentFlag[i]) {
1625
0
      k = dpb.DPB_index_of_picture_with_LSB(PocLtFoll[i], currentID, true);
1626
0
    }
1627
0
    else {
1628
0
      k = dpb.DPB_index_of_picture_with_POC(PocLtFoll[i], currentID, true);
1629
0
    }
1630
1631
0
    RefPicSetLtFoll[i] = k; // -1 == "no reference picture"
1632
0
    if (k>=0) picInAnyList[k]=true;
1633
0
    else {
1634
0
      int concealedPicture = k = generate_unavailable_reference_picture(current_sps.get(),
1635
0
                                                                        PocLtFoll[i], true);
1636
0
      if (concealedPicture<0) {
1637
0
        return (de265_error)(-concealedPicture);
1638
0
      }
1639
0
      picInAnyList.resize(dpb.size(), false); // adjust size of array to hold new picture
1640
1641
0
      RefPicSetLtFoll[i] = concealedPicture;
1642
0
      picInAnyList[concealedPicture]=true;
1643
0
    }
1644
0
  }
1645
1646
1647
  // 2. Mark all pictures in RefPicSetLtCurr / RefPicSetLtFoll as UsedForLongTermReference
1648
1649
0
  for (int i=0;i<NumPocLtCurr;i++) {
1650
0
    dpb.get_image(RefPicSetLtCurr[i])->PicState = UsedForLongTermReference;
1651
0
  }
1652
1653
0
  for (int i=0;i<NumPocLtFoll;i++) {
1654
0
    dpb.get_image(RefPicSetLtFoll[i])->PicState = UsedForLongTermReference;
1655
0
  }
1656
1657
1658
  // 3.
1659
1660
0
  for (int i=0;i<NumPocStCurrBefore;i++) {
1661
0
    int k = dpb.DPB_index_of_picture_with_POC(PocStCurrBefore[i], currentID);
1662
1663
    //printf("st curr before, poc=%d -> idx=%d\n",PocStCurrBefore[i], k);
1664
1665
0
    RefPicSetStCurrBefore[i] = k; // -1 == "no reference picture"
1666
0
    if (k>=0) picInAnyList[k]=true;
1667
0
    else {
1668
0
      int concealedPicture = generate_unavailable_reference_picture(current_sps.get(),
1669
0
                                                                    PocStCurrBefore[i], false);
1670
0
      if (concealedPicture<0) {
1671
0
        return (de265_error)(-concealedPicture);
1672
0
      }
1673
0
      RefPicSetStCurrBefore[i] = k = concealedPicture;
1674
1675
0
      picInAnyList.resize(dpb.size(), false); // adjust size of array to hold new picture
1676
0
      picInAnyList[concealedPicture] = true;
1677
1678
      //printf("  concealed: %d\n", concealedPicture);
1679
0
    }
1680
1681
0
    if (dpb.get_image(k)->integrity != INTEGRITY_CORRECT) {
1682
0
      img->integrity = INTEGRITY_DERIVED_FROM_FAULTY_REFERENCE;
1683
0
    }
1684
0
  }
1685
1686
0
  for (int i=0;i<NumPocStCurrAfter;i++) {
1687
0
    int k = dpb.DPB_index_of_picture_with_POC(PocStCurrAfter[i], currentID);
1688
1689
    //printf("st curr after, poc=%d -> idx=%d\n",PocStCurrAfter[i], k);
1690
1691
0
    RefPicSetStCurrAfter[i] = k; // -1 == "no reference picture"
1692
0
    if (k>=0) picInAnyList[k]=true;
1693
0
    else {
1694
0
      int concealedPicture = generate_unavailable_reference_picture(current_sps.get(),
1695
0
                                                                    PocStCurrAfter[i], false);
1696
0
      if (concealedPicture<0) {
1697
0
        return (de265_error)(-concealedPicture);
1698
0
      }
1699
0
      RefPicSetStCurrAfter[i] = k = concealedPicture;
1700
1701
1702
0
      picInAnyList.resize(dpb.size(), false); // adjust size of array to hold new picture
1703
0
      picInAnyList[concealedPicture]=true;
1704
1705
      //printf("  concealed: %d\n", concealedPicture);
1706
0
    }
1707
1708
0
    if (dpb.get_image(k)->integrity != INTEGRITY_CORRECT) {
1709
0
      img->integrity = INTEGRITY_DERIVED_FROM_FAULTY_REFERENCE;
1710
0
    }
1711
0
  }
1712
1713
0
  for (int i=0;i<NumPocStFoll;i++) {
1714
0
    int k = dpb.DPB_index_of_picture_with_POC(PocStFoll[i], currentID);
1715
    // if (k<0) { assert(false); } // IGNORE
1716
1717
0
    RefPicSetStFoll[i] = k; // -1 == "no reference picture"
1718
0
    if (k>=0) picInAnyList[k]=true;
1719
0
  }
1720
1721
  // 4. any picture that is not marked for reference is put into the "UnusedForReference" state
1722
1723
0
  for (int i=0;i<dpb.size();i++)
1724
0
    if (i>=picInAnyList.size() || !picInAnyList[i])        // no reference
1725
0
      {
1726
0
        de265_image* dpbimg = dpb.get_image(i);
1727
0
        if (dpbimg != img &&  // not the current picture
1728
0
            dpbimg->removed_at_picture_id > img->get_ID()) // has not been removed before
1729
0
          {
1730
0
            if (dpbimg->PicState != UnusedForReference) {
1731
0
              removeReferencesList.push_back(dpbimg->get_ID());
1732
              //printf("will remove ID %d (b)\n",dpbimg->get_ID());
1733
1734
0
              dpbimg->removed_at_picture_id = img->get_ID();
1735
0
            }
1736
0
          }
1737
0
      }
1738
1739
0
  hdr->RemoveReferencesList = removeReferencesList;
1740
1741
  //remove_images_from_dpb(hdr->RemoveReferencesList);
1742
1743
0
  return DE265_OK;
1744
0
}
1745
1746
1747
// 8.3.4
1748
// Returns whether we can continue decoding (or whether there is a severe error).
1749
/* Called at beginning of each slice.
1750
1751
   Constructs
1752
   - the RefPicList[2][], containing indices into the DPB, and
1753
   - the RefPicList_POC[2][], containing POCs.
1754
   - LongTermRefPic[2][] is also set to true if it is a long-term reference
1755
 */
1756
bool decoder_context::construct_reference_picture_lists(slice_segment_header* hdr)
1757
0
{
1758
0
  int NumPocTotalCurr = hdr->NumPocTotalCurr;
1759
0
  int NumRpsCurrTempList0 = libde265_max(hdr->num_ref_idx_l0_active, NumPocTotalCurr);
1760
1761
  // TODO: fold code for both lists together
1762
1763
0
  int RefPicListTemp0[3*MAX_NUM_REF_PICS]; // TODO: what would be the correct maximum ?
1764
0
  int RefPicListTemp1[3*MAX_NUM_REF_PICS]; // TODO: what would be the correct maximum ?
1765
0
  char isLongTerm[2][3*MAX_NUM_REF_PICS];
1766
1767
0
  memset(isLongTerm,0,2*3*MAX_NUM_REF_PICS);
1768
1769
  /* --- Fill RefPicListTmp0 with reference pictures in this order:
1770
     1) short term, past POC
1771
     2) short term, future POC
1772
     3) long term
1773
  */
1774
1775
0
  int rIdx=0;
1776
0
  while (rIdx < NumRpsCurrTempList0) {
1777
0
    for (int i=0;i<NumPocStCurrBefore && rIdx<NumRpsCurrTempList0; rIdx++,i++)
1778
0
      RefPicListTemp0[rIdx] = RefPicSetStCurrBefore[i];
1779
1780
0
    for (int i=0;i<NumPocStCurrAfter && rIdx<NumRpsCurrTempList0; rIdx++,i++)
1781
0
      RefPicListTemp0[rIdx] = RefPicSetStCurrAfter[i];
1782
1783
0
    for (int i=0;i<NumPocLtCurr && rIdx<NumRpsCurrTempList0; rIdx++,i++) {
1784
0
      RefPicListTemp0[rIdx] = RefPicSetLtCurr[i];
1785
0
      isLongTerm[0][rIdx] = true;
1786
0
    }
1787
1788
    // This check is to prevent an endless loop when no images are added above.
1789
0
    if (rIdx==0) {
1790
0
      add_warning(DE265_WARNING_FAULTY_REFERENCE_PICTURE_LIST, false);
1791
0
      return false;
1792
0
    }
1793
0
  }
1794
1795
  /*
1796
  if (hdr->num_ref_idx_l0_active > 16) {
1797
    add_warning(DE265_WARNING_NONEXISTING_REFERENCE_PICTURE_ACCESSED, false);
1798
    return false;
1799
  }
1800
  */
1801
1802
0
  assert(hdr->num_ref_idx_l0_active <= 16);
1803
0
  for (rIdx=0; rIdx<hdr->num_ref_idx_l0_active; rIdx++) {
1804
0
    int idx = hdr->ref_pic_list_modification_flag_l0 ? hdr->list_entry_l0[rIdx] : rIdx;
1805
1806
0
    hdr->RefPicList[0][rIdx] = RefPicListTemp0[idx];
1807
0
    hdr->LongTermRefPic[0][rIdx] = isLongTerm[0][idx];
1808
1809
    // remember POC of referenced image (needed in motion.c, derive_collocated_motion_vector)
1810
0
    de265_image* img_0_rIdx = dpb.get_image(hdr->RefPicList[0][rIdx]);
1811
0
    if (img_0_rIdx==NULL) {
1812
0
      return false;
1813
0
    }
1814
0
    hdr->RefPicList_POC[0][rIdx] = img_0_rIdx->PicOrderCntVal;
1815
0
    hdr->RefPicList_PicState[0][rIdx] = img_0_rIdx->PicState;
1816
0
  }
1817
1818
1819
  /* --- Fill RefPicListTmp1 with reference pictures in this order:
1820
     1) short term, future POC
1821
     2) short term, past POC
1822
     3) long term
1823
  */
1824
1825
0
  if (hdr->slice_type == SLICE_TYPE_B) {
1826
0
    int NumRpsCurrTempList1 = libde265_max(hdr->num_ref_idx_l1_active, NumPocTotalCurr);
1827
1828
0
    int rIdx=0;
1829
0
    while (rIdx < NumRpsCurrTempList1) {
1830
0
      for (int i=0;i<NumPocStCurrAfter && rIdx<NumRpsCurrTempList1; rIdx++,i++) {
1831
0
        RefPicListTemp1[rIdx] = RefPicSetStCurrAfter[i];
1832
0
      }
1833
1834
0
      for (int i=0;i<NumPocStCurrBefore && rIdx<NumRpsCurrTempList1; rIdx++,i++) {
1835
0
        RefPicListTemp1[rIdx] = RefPicSetStCurrBefore[i];
1836
0
      }
1837
1838
0
      for (int i=0;i<NumPocLtCurr && rIdx<NumRpsCurrTempList1; rIdx++,i++) {
1839
0
        RefPicListTemp1[rIdx] = RefPicSetLtCurr[i];
1840
0
        isLongTerm[1][rIdx] = true;
1841
0
      }
1842
1843
      // This check is to prevent an endless loop when no images are added above.
1844
0
      if (rIdx==0) {
1845
0
        add_warning(DE265_WARNING_FAULTY_REFERENCE_PICTURE_LIST, false);
1846
0
        return false;
1847
0
      }
1848
0
    }
1849
1850
0
    if (hdr->num_ref_idx_l0_active > 16) {
1851
0
    add_warning(DE265_WARNING_NONEXISTING_REFERENCE_PICTURE_ACCESSED, false);
1852
0
    return false;
1853
0
  }
1854
1855
0
    assert(hdr->num_ref_idx_l1_active <= 16);
1856
0
    for (rIdx=0; rIdx<hdr->num_ref_idx_l1_active; rIdx++) {
1857
0
      int idx = hdr->ref_pic_list_modification_flag_l1 ? hdr->list_entry_l1[rIdx] : rIdx;
1858
1859
0
      hdr->RefPicList[1][rIdx] = RefPicListTemp1[idx];
1860
0
      hdr->LongTermRefPic[1][rIdx] = isLongTerm[1][idx];
1861
1862
      // remember POC of referenced imaged (needed in motion.c, derive_collocated_motion_vector)
1863
0
      de265_image* img_1_rIdx = dpb.get_image(hdr->RefPicList[1][rIdx]);
1864
0
      if (img_1_rIdx == NULL) { return false; }
1865
0
      hdr->RefPicList_POC[1][rIdx] = img_1_rIdx->PicOrderCntVal;
1866
0
      hdr->RefPicList_PicState[1][rIdx] = img_1_rIdx->PicState;
1867
0
    }
1868
0
  }
1869
1870
1871
  // show reference picture lists
1872
1873
0
  loginfo(LogHeaders,"RefPicList[0] =");
1874
0
  for (rIdx=0; rIdx<hdr->num_ref_idx_l0_active; rIdx++) {
1875
0
    loginfo(LogHeaders,"* [%d]=%d (LT=%d)",
1876
0
            hdr->RefPicList[0][rIdx],
1877
0
            hdr->RefPicList_POC[0][rIdx],
1878
0
            hdr->LongTermRefPic[0][rIdx]
1879
0
            );
1880
0
  }
1881
0
  loginfo(LogHeaders,"*\n");
1882
1883
0
  if (hdr->slice_type == SLICE_TYPE_B) {
1884
0
    loginfo(LogHeaders,"RefPicList[1] =");
1885
0
    for (rIdx=0; rIdx<hdr->num_ref_idx_l1_active; rIdx++) {
1886
0
      loginfo(LogHeaders,"* [%d]=%d (LT=%d)",
1887
0
              hdr->RefPicList[1][rIdx],
1888
0
              hdr->RefPicList_POC[1][rIdx],
1889
0
              hdr->LongTermRefPic[1][rIdx]
1890
0
              );
1891
0
    }
1892
0
    loginfo(LogHeaders,"*\n");
1893
0
  }
1894
1895
0
  return true;
1896
0
}
1897
1898
1899
1900
void decoder_context::run_postprocessing_filters_sequential(de265_image* img)
1901
0
{
1902
#if SAVE_INTERMEDIATE_IMAGES
1903
    char buf[1000];
1904
    sprintf(buf,"pre-lf-%05d.yuv", img->PicOrderCntVal);
1905
    write_picture_to_file(img, buf);
1906
#endif
1907
1908
0
    if (!img->decctx->param_disable_deblocking) {
1909
0
      apply_deblocking_filter(img);
1910
0
    }
1911
1912
#if SAVE_INTERMEDIATE_IMAGES
1913
    sprintf(buf,"pre-sao-%05d.yuv", img->PicOrderCntVal);
1914
    write_picture_to_file(img, buf);
1915
#endif
1916
1917
0
    if (!img->decctx->param_disable_sao) {
1918
0
      apply_sample_adaptive_offset_sequential(img);
1919
0
    }
1920
1921
#if SAVE_INTERMEDIATE_IMAGES
1922
    sprintf(buf,"sao-%05d.yuv", img->PicOrderCntVal);
1923
    write_picture_to_file(img, buf);
1924
#endif
1925
0
}
1926
1927
1928
void decoder_context::run_postprocessing_filters_parallel(image_unit* imgunit)
1929
0
{
1930
0
  de265_image* img = imgunit->img;
1931
1932
0
  int saoWaitsForProgress = CTB_PROGRESS_PREFILTER;
1933
0
  bool waitForCompletion = false;
1934
1935
0
  if (!img->decctx->param_disable_deblocking) {
1936
0
    add_deblocking_tasks(imgunit);
1937
0
    saoWaitsForProgress = CTB_PROGRESS_DEBLK_H;
1938
0
  }
1939
1940
0
  if (!img->decctx->param_disable_sao) {
1941
0
    waitForCompletion |= add_sao_tasks(imgunit, saoWaitsForProgress);
1942
    //apply_sample_adaptive_offset(img);
1943
0
  }
1944
1945
0
  img->wait_for_completion();
1946
0
}
1947
1948
/*
1949
void decoder_context::push_current_picture_to_output_queue()
1950
{
1951
  push_picture_to_output_queue(img);
1952
}
1953
*/
1954
1955
de265_error decoder_context::push_picture_to_output_queue(image_unit* imgunit)
1956
0
{
1957
0
  de265_image* outimg = imgunit->img;
1958
1959
0
  if (outimg==NULL) { return DE265_OK; }
1960
1961
1962
  // push image into output queue
1963
1964
0
  if (outimg->PicOutputFlag) {
1965
0
    loginfo(LogDPB,"new picture has output-flag=true\n");
1966
1967
0
    if (outimg->integrity != INTEGRITY_CORRECT &&
1968
0
        param_suppress_faulty_pictures) {
1969
0
    }
1970
0
    else {
1971
0
      dpb.insert_image_into_reorder_buffer(outimg);
1972
0
    }
1973
1974
0
    loginfo(LogDPB,"push image %d into reordering queue\n", outimg->PicOrderCntVal);
1975
0
  }
1976
1977
  // check for full reorder buffers
1978
1979
0
  int maxNumPicsInReorderBuffer = 0;
1980
1981
  // TODO: I'd like to have the has_vps() check somewhere else (not decode the picture at all)
1982
0
  if (outimg->has_vps()) {
1983
0
    int sublayer = outimg->get_vps().vps_max_sub_layers -1;
1984
0
    maxNumPicsInReorderBuffer = outimg->get_vps().layer[sublayer].vps_max_num_reorder_pics;
1985
0
  }
1986
1987
0
  if (dpb.num_pictures_in_reorder_buffer() > maxNumPicsInReorderBuffer) {
1988
0
    dpb.output_next_picture_in_reorder_buffer();
1989
0
  }
1990
1991
0
  dpb.log_dpb_queues();
1992
1993
0
  return DE265_OK;
1994
0
}
1995
1996
1997
// returns whether we can continue decoding the stream or whether we should give up
1998
bool decoder_context::process_slice_segment_header(slice_segment_header* hdr,
1999
                                                   de265_error* err, de265_PTS pts,
2000
                                                   nal_header* nal_hdr,
2001
                                                   void* user_data)
2002
0
{
2003
0
  *err = DE265_OK;
2004
2005
0
  flush_reorder_buffer_at_this_frame = false;
2006
2007
2008
  // get PPS and SPS for this slice
2009
2010
0
  int pps_id = hdr->slice_pic_parameter_set_id;
2011
0
  if (pps[pps_id]==nullptr || pps[pps_id]->pps_read==false) {
2012
0
    logerror(LogHeaders, "PPS %d has not been read\n", pps_id);
2013
0
    img->decctx->add_warning(DE265_WARNING_NONEXISTING_PPS_REFERENCED, false);
2014
0
    return false;
2015
0
  }
2016
2017
0
  current_pps = pps[pps_id];
2018
0
  current_sps = sps[ (int)current_pps->seq_parameter_set_id ];
2019
0
  current_vps = vps[ (int)current_sps->video_parameter_set_id ];
2020
2021
0
  calc_tid_and_framerate_ratio();
2022
2023
2024
  // --- prepare decoding of new picture ---
2025
2026
0
  if (hdr->first_slice_segment_in_pic_flag) {
2027
2028
    // previous picture has been completely decoded
2029
2030
    //ctx->push_current_picture_to_output_queue();
2031
2032
0
    current_image_poc_lsb = hdr->slice_pic_order_cnt_lsb;
2033
2034
2035
0
    seq_parameter_set* sps = current_sps.get();
2036
2037
2038
    // --- find and allocate image buffer for decoding ---
2039
2040
0
    int image_buffer_idx;
2041
0
    bool isOutputImage = (!sps->sample_adaptive_offset_enabled_flag || param_disable_sao);
2042
0
    image_buffer_idx = dpb.new_image(current_sps, this, pts, user_data, isOutputImage);
2043
0
    if (image_buffer_idx < 0) {
2044
0
      *err = (de265_error)(-image_buffer_idx);
2045
0
      return false;
2046
0
    }
2047
2048
0
    /*de265_image* */ img = dpb.get_image(image_buffer_idx);
2049
0
    img->nal_hdr = *nal_hdr;
2050
2051
    // Note: sps is already set in new_image() -> ??? still the case with shared_ptr ?
2052
2053
0
    img->set_headers(current_vps, current_sps, current_pps);
2054
2055
0
    img->decctx = this;
2056
2057
0
    img->clear_metadata();
2058
2059
2060
0
    if (isIRAP(nal_unit_type)) {
2061
0
      if (isIDR(nal_unit_type) ||
2062
0
          isBLA(nal_unit_type) ||
2063
0
          first_decoded_picture ||
2064
0
          FirstAfterEndOfSequenceNAL)
2065
0
        {
2066
0
          NoRaslOutputFlag = true;
2067
0
          FirstAfterEndOfSequenceNAL = false;
2068
0
        }
2069
0
      else if (0) // TODO: set HandleCraAsBlaFlag by external means
2070
0
        {
2071
0
        }
2072
0
      else
2073
0
        {
2074
0
          NoRaslOutputFlag   = false;
2075
0
          HandleCraAsBlaFlag = false;
2076
0
        }
2077
0
    }
2078
2079
2080
0
    if (isRASL(nal_unit_type) &&
2081
0
        NoRaslOutputFlag)
2082
0
      {
2083
0
        img->PicOutputFlag = false;
2084
0
      }
2085
0
    else
2086
0
      {
2087
0
        img->PicOutputFlag = !!hdr->pic_output_flag;
2088
0
      }
2089
2090
0
    process_picture_order_count(hdr);
2091
2092
0
    if (hdr->first_slice_segment_in_pic_flag) {
2093
      // mark picture so that it is not overwritten by unavailable reference frames
2094
0
      img->PicState = UsedForShortTermReference;
2095
2096
0
      *err = process_reference_picture_set(hdr);
2097
0
      if (*err != DE265_OK) {
2098
0
        return false;
2099
0
      }
2100
0
    }
2101
2102
0
    img->PicState = UsedForShortTermReference;
2103
2104
0
    log_set_current_POC(img->PicOrderCntVal);
2105
2106
2107
    // next image is not the first anymore
2108
2109
0
    first_decoded_picture = false;
2110
0
  }
2111
0
  else {
2112
    // claims to be not the first slice, but there is no active image available
2113
2114
0
    if (img == NULL) {
2115
0
      return false;
2116
0
    }
2117
0
  }
2118
2119
0
  if (hdr->slice_type == SLICE_TYPE_B ||
2120
0
      hdr->slice_type == SLICE_TYPE_P)
2121
0
    {
2122
0
      bool success = construct_reference_picture_lists(hdr);
2123
0
      if (!success) {
2124
0
        return false;
2125
0
      }
2126
0
    }
2127
2128
  //printf("process slice segment header\n");
2129
2130
0
  loginfo(LogHeaders,"end of process-slice-header\n");
2131
0
  dpb.log_dpb_content();
2132
2133
2134
0
  if (hdr->dependent_slice_segment_flag==0) {
2135
0
    hdr->SliceAddrRS = hdr->slice_segment_address;
2136
0
  } else {
2137
0
    hdr->SliceAddrRS = previous_slice_header->SliceAddrRS;
2138
0
  }
2139
2140
0
  previous_slice_header = hdr;
2141
2142
2143
0
  loginfo(LogHeaders,"SliceAddrRS = %d\n",hdr->SliceAddrRS);
2144
2145
0
  return true;
2146
0
}
2147
2148
2149
void decoder_context::remove_images_from_dpb(const std::vector<int>& removeImageList)
2150
0
{
2151
0
  for (size_t i=0;i<removeImageList.size();i++) {
2152
0
    int idx = dpb.DPB_index_of_picture_with_ID( removeImageList[i] );
2153
0
    if (idx>=0) {
2154
      //printf("remove ID %d\n", removeImageList[i]);
2155
0
      de265_image* dpbimg = dpb.get_image( idx );
2156
0
      dpbimg->PicState = UnusedForReference;
2157
0
    }
2158
0
  }
2159
0
}
2160
2161
2162
2163
/*
2164
  .     0     1     2       <- goal_HighestTid
2165
  +-----+-----+-----+
2166
  | -0->| -1->| -2->|
2167
  +-----+-----+-----+
2168
  0     33    66    100     <- framerate_ratio
2169
 */
2170
2171
int  decoder_context::get_highest_TID() const
2172
0
{
2173
0
  if (current_sps) { return current_sps->sps_max_sub_layers-1; }
2174
0
  if (current_vps) { return current_vps->vps_max_sub_layers-1; }
2175
2176
0
  return 6;
2177
0
}
2178
2179
void decoder_context::set_limit_TID(int max_tid)
2180
0
{
2181
0
  limit_HighestTid = max_tid;
2182
0
  calc_tid_and_framerate_ratio();
2183
0
}
2184
2185
int decoder_context::change_framerate(int more)
2186
0
{
2187
0
  if (current_sps == NULL) { return framerate_ratio; }
2188
2189
0
  int highestTid = get_highest_TID();
2190
2191
0
  assert(more>=-1 && more<=1);
2192
2193
0
  goal_HighestTid += more;
2194
0
  goal_HighestTid = std::max(goal_HighestTid, 0);
2195
0
  goal_HighestTid = std::min(goal_HighestTid, highestTid);
2196
2197
0
  framerate_ratio = framedrop_tid_index[goal_HighestTid];
2198
2199
0
  calc_tid_and_framerate_ratio();
2200
2201
0
  return framerate_ratio;
2202
0
}
2203
2204
void decoder_context::set_framerate_ratio(int percent)
2205
0
{
2206
0
  framerate_ratio = percent;
2207
0
  calc_tid_and_framerate_ratio();
2208
0
}
2209
2210
void decoder_context::compute_framedrop_table()
2211
0
{
2212
0
  int highestTID = get_highest_TID();
2213
2214
0
  for (int tid=highestTID ; tid>=0 ; tid--) {
2215
0
    int lower  = 100 *  tid   /(highestTID+1);
2216
0
    int higher = 100 * (tid+1)/(highestTID+1);
2217
2218
0
    for (int l=lower; l<=higher; l++) {
2219
0
      int ratio = 100 * (l-lower) / (higher-lower);
2220
2221
      // if we would exceed our TID limit, decode the highest TID at full frame-rate
2222
0
      if (tid > limit_HighestTid) {
2223
0
        tid   = limit_HighestTid;
2224
0
        ratio = 100;
2225
0
      }
2226
2227
0
      framedrop_tab[l].tid   = tid;
2228
0
      framedrop_tab[l].ratio = ratio;
2229
0
    }
2230
2231
0
    framedrop_tid_index[tid] = higher;
2232
0
  }
2233
2234
#if 0
2235
  for (int i=0;i<=100;i++) {
2236
    printf("%d%%: %d/%d",i, framedrop_tab[i].tid, framedrop_tab[i].ratio);
2237
    for (int k=0;k<=highestTID;k++) {
2238
      if (framedrop_tid_index[k] == i) printf(" ** TID=%d **",k);
2239
    }
2240
    printf("\n");
2241
  }
2242
#endif
2243
0
}
2244
2245
void decoder_context::calc_tid_and_framerate_ratio()
2246
0
{
2247
0
  int highestTID = get_highest_TID();
2248
2249
2250
  // if number of temporal layers changed, we have to recompute the framedrop table
2251
2252
0
  if (framedrop_tab[100].tid != highestTID) {
2253
0
    compute_framedrop_table();
2254
0
  }
2255
2256
0
  goal_HighestTid       = framedrop_tab[framerate_ratio].tid;
2257
0
  layer_framerate_ratio = framedrop_tab[framerate_ratio].ratio;
2258
2259
  // TODO: for now, we switch immediately
2260
0
  current_HighestTid = goal_HighestTid;
2261
0
}
2262
2263
2264
void error_queue::add_warning(de265_error warning, bool once)
2265
0
{
2266
  // check if warning was already shown
2267
0
  bool add=true;
2268
0
  if (once) {
2269
0
    for (int i=0;i<nWarningsShown;i++) {
2270
0
      if (warnings_shown[i] == warning) {
2271
0
        add=false;
2272
0
        break;
2273
0
      }
2274
0
    }
2275
0
  }
2276
2277
0
  if (!add) {
2278
0
    return;
2279
0
  }
2280
2281
2282
  // if this is a one-time warning, remember that it was shown
2283
2284
0
  if (once) {
2285
0
    if (nWarningsShown < MAX_WARNINGS) {
2286
0
      warnings_shown[nWarningsShown++] = warning;
2287
0
    }
2288
0
  }
2289
2290
2291
  // add warning to output queue
2292
2293
0
  if (nWarnings == MAX_WARNINGS) {
2294
0
    warnings[MAX_WARNINGS-1] = DE265_WARNING_WARNING_BUFFER_FULL;
2295
0
    return;
2296
0
  }
2297
2298
0
  warnings[nWarnings++] = warning;
2299
0
}
2300
2301
error_queue::error_queue()
2302
0
{
2303
0
  nWarnings = 0;
2304
0
  nWarningsShown = 0;
2305
0
}
2306
2307
de265_error error_queue::get_warning()
2308
0
{
2309
0
  if (nWarnings==0) {
2310
0
    return DE265_OK;
2311
0
  }
2312
2313
0
  de265_error warn = warnings[0];
2314
0
  nWarnings--;
2315
0
  memmove(warnings, &warnings[1], nWarnings*sizeof(de265_error));
2316
2317
0
  return warn;
2318
0
}