Coverage Report

Created: 2026-12-01 06:48

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