-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdecctx.cc
2294 lines (1670 loc) · 58.9 KB
/
decctx.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* H.265 video codec.
* Copyright (c) 2013-2014 struktur AG, Dirk Farin <[email protected]>
*
* This file is part of libde265.
*
* libde265 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* libde265 is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libde265. If not, see <http://www.gnu.org/licenses/>.
*/
#include "decctx.h"
#include "util.h"
#include "sao.h"
#include "sei.h"
#include "deblock.h"
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "fallback.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_SSE4_1
#include "x86/sse.h"
#endif
#ifdef HAVE_ARM
#include "arm/arm.h"
#endif
#ifdef WASM_SIMD
#include "wasm/simd.h"
#endif
#define SAVE_INTERMEDIATE_IMAGES 0
#if SAVE_INTERMEDIATE_IMAGES
#include "visualize.h"
#endif
extern void thread_decode_CTB_row(void* d);
extern void thread_decode_slice_segment(void* d);
thread_context::thread_context()
{
/*
CtbAddrInRS = 0;
CtbAddrInTS = 0;
CtbX = 0;
CtbY = 0;
*/
/*
refIdx[0] = refIdx[1] = 0;
mvd[0][0] = mvd[0][1] = mvd[1][0] = mvd[1][1] = 0;
merge_flag = 0;
merge_idx = 0;
mvp_lX_flag[0] = mvp_lX_flag[1] = 0;
inter_pred_idc = 0;
*/
/*
enum IntraPredMode IntraPredModeC; // chroma intra-prediction mode for current CB
*/
/*
cu_transquant_bypass_flag = false;
memset(transform_skip_flag,0, 3*sizeof(uint8_t));
*/
//memset(coeffList,0,sizeof(int16_t)*3*32*32);
//memset(coeffPos,0,sizeof(int16_t)*3*32*32);
//memset(nCoeff,0,sizeof(int16_t)*3);
IsCuQpDeltaCoded = false;
CuQpDelta = 0;
IsCuChromaQpOffsetCoded = false;
CuQpOffsetCb = 0;
CuQpOffsetCr = 0;
/*
currentQPY = 0;
currentQG_x = 0;
currentQG_y = 0;
lastQPYinPreviousQG = 0;
*/
/*
qPYPrime = 0;
qPCbPrime = 0;
qPCrPrime = 0;
*/
/*
memset(&cabac_decoder, 0, sizeof(CABAC_decoder));
memset(&ctx_model, 0, sizeof(ctx_model));
*/
decctx = NULL;
img = NULL;
shdr = NULL;
imgunit = NULL;
sliceunit = NULL;
//memset(this,0,sizeof(thread_context));
// There is a interesting issue here. When aligning _coeffBuf to 16 bytes offset with
// __attribute__((align(16))), the following statement is optimized away since the
// compiler assumes that the pointer would be 16-byte aligned. However, this is not the
// case when the structure has been dynamically allocated. In this case, the base can
// also be at 8 byte offsets (at least with MingW,32 bit).
int offset = ((uintptr_t)_coeffBuf) & 0xf;
if (offset == 0) {
coeffBuf = _coeffBuf; // correctly aligned already
}
else {
coeffBuf = (int16_t *) (((uint8_t *)_coeffBuf) + (16-offset));
}
memset(coeffBuf, 0, 32*32*sizeof(int16_t));
}
slice_unit::slice_unit(decoder_context* decctx)
: nal(NULL),
shdr(NULL),
imgunit(NULL),
flush_reorder_buffer(false),
nThreads(0),
first_decoded_CTB_RS(-1),
last_decoded_CTB_RS(-1),
thread_contexts(NULL),
ctx(decctx)
{
state = Unprocessed;
nThreadContexts = 0;
}
slice_unit::~slice_unit()
{
ctx->nal_parser.free_NAL_unit(nal);
if (thread_contexts) {
delete[] thread_contexts;
}
}
void slice_unit::allocate_thread_contexts(int n)
{
assert(thread_contexts==NULL);
thread_contexts = new thread_context[n];
nThreadContexts = n;
}
image_unit::image_unit()
{
img=NULL;
role=Invalid;
state=Unprocessed;
}
image_unit::~image_unit()
{
for (int i=0;i<slice_units.size();i++) {
delete slice_units[i];
}
for (int i=0;i<tasks.size();i++) {
delete tasks[i];
}
}
base_context::base_context()
{
set_acceleration_functions(de265_acceleration_AUTO);
}
decoder_context::decoder_context()
{
//memset(ctx, 0, sizeof(decoder_context));
// --- parameters ---
param_sei_check_hash = false;
param_conceal_stream_errors = true;
param_suppress_faulty_pictures = false;
param_disable_deblocking = false;
param_disable_sao = false;
//param_disable_mc_residual_idct = false;
//param_disable_intra_residual_idct = false;
// --- processing ---
param_sps_headers_fd = -1;
param_vps_headers_fd = -1;
param_pps_headers_fd = -1;
param_slice_headers_fd = -1;
param_image_allocation_functions = de265_image::default_image_allocation;
param_image_allocation_userdata = NULL;
/*
memset(&vps, 0, sizeof(video_parameter_set)*DE265_MAX_VPS_SETS);
memset(&sps, 0, sizeof(seq_parameter_set) *DE265_MAX_SPS_SETS);
memset(&pps, 0, sizeof(pic_parameter_set) *DE265_MAX_PPS_SETS);
memset(&slice,0,sizeof(slice_segment_header)*DE265_MAX_SLICES);
*/
current_vps = NULL;
current_sps = NULL;
current_pps = NULL;
//memset(&thread_pool,0,sizeof(struct thread_pool));
num_worker_threads = 0;
// frame-rate
limit_HighestTid = 6; // decode all temporal layers (up to layer 6)
framerate_ratio = 100; // decode all 100%
goal_HighestTid = 6;
current_HighestTid = 6;
layer_framerate_ratio = 100;
compute_framedrop_table();
//
current_image_poc_lsb = 0;
first_decoded_picture = 0;
NoRaslOutputFlag = 0;
HandleCraAsBlaFlag = 0;
FirstAfterEndOfSequenceNAL = 0;
PicOrderCntMsb = 0;
prevPicOrderCntLsb = 0;
prevPicOrderCntMsb = 0;
img = NULL;
/*
int PocLsbLt[MAX_NUM_REF_PICS];
int UsedByCurrPicLt[MAX_NUM_REF_PICS];
int DeltaPocMsbCycleLt[MAX_NUM_REF_PICS];
int CurrDeltaPocMsbPresentFlag[MAX_NUM_REF_PICS];
int FollDeltaPocMsbPresentFlag[MAX_NUM_REF_PICS];
int NumPocStCurrBefore;
int NumPocStCurrAfter;
int NumPocStFoll;
int NumPocLtCurr;
int NumPocLtFoll;
// These lists contain absolute POC values.
int PocStCurrBefore[MAX_NUM_REF_PICS]; // used for reference in current picture, smaller POC
int PocStCurrAfter[MAX_NUM_REF_PICS]; // used for reference in current picture, larger POC
int PocStFoll[MAX_NUM_REF_PICS]; // not used for reference in current picture, but in future picture
int PocLtCurr[MAX_NUM_REF_PICS]; // used in current picture
int PocLtFoll[MAX_NUM_REF_PICS]; // used in some future picture
// These lists contain indices into the DPB.
int RefPicSetStCurrBefore[DE265_DPB_SIZE];
int RefPicSetStCurrAfter[DE265_DPB_SIZE];
int RefPicSetStFoll[DE265_DPB_SIZE];
int RefPicSetLtCurr[DE265_DPB_SIZE];
int RefPicSetLtFoll[DE265_DPB_SIZE];
uint8_t nal_unit_type;
char IdrPicFlag;
char RapPicFlag;
*/
// --- internal data ---
first_decoded_picture = true;
//ctx->FirstAfterEndOfSequenceNAL = true;
//ctx->last_RAP_picture_NAL_type = NAL_UNIT_UNDEFINED;
//de265_init_image(&ctx->coeff);
// --- decoded picture buffer ---
current_image_poc_lsb = -1; // any invalid number
}
decoder_context::~decoder_context()
{
while (!image_units.empty()) {
delete image_units.back();
image_units.pop_back();
}
}
void decoder_context::set_image_allocation_functions(de265_image_allocation* allocfunc,
void* userdata)
{
if (allocfunc) {
param_image_allocation_functions = *allocfunc;
param_image_allocation_userdata = userdata;
}
else {
assert(false); // actually, it makes no sense to reset the allocation functions
param_image_allocation_functions = de265_image::default_image_allocation;
param_image_allocation_userdata = NULL;
}
}
de265_error decoder_context::start_thread_pool(int nThreads)
{
::start_thread_pool(&thread_pool_, nThreads);
num_worker_threads = nThreads;
return DE265_OK;
}
void decoder_context::stop_thread_pool()
{
if (get_num_worker_threads()>0) {
//flush_thread_pool(&ctx->thread_pool);
::stop_thread_pool(&thread_pool_);
}
}
void decoder_context::reset()
{
if (num_worker_threads>0) {
//flush_thread_pool(&ctx->thread_pool);
::stop_thread_pool(&thread_pool_);
}
// --------------------------------------------------
#if 0
ctx->end_of_stream = false;
ctx->pending_input_NAL = NULL;
ctx->current_vps = NULL;
ctx->current_sps = NULL;
ctx->current_pps = NULL;
ctx->num_worker_threads = 0;
ctx->current_image_poc_lsb = 0;
ctx->first_decoded_picture = 0;
ctx->NoRaslOutputFlag = 0;
ctx->HandleCraAsBlaFlag = 0;
ctx->FirstAfterEndOfSequenceNAL = 0;
ctx->PicOrderCntMsb = 0;
ctx->prevPicOrderCntLsb = 0;
ctx->prevPicOrderCntMsb = 0;
ctx->NumPocStCurrBefore=0;
ctx->NumPocStCurrAfter=0;
ctx->NumPocStFoll=0;
ctx->NumPocLtCurr=0;
ctx->NumPocLtFoll=0;
ctx->nal_unit_type=0;
ctx->IdrPicFlag=0;
ctx->RapPicFlag=0;
#endif
img = NULL;
// TODO: remove all pending image_units
// --- decoded picture buffer ---
current_image_poc_lsb = -1; // any invalid number
first_decoded_picture = true;
// --- remove all pictures from output queue ---
// there was a bug the peek_next_image did not return NULL on empty output queues.
// This was (indirectly) fixed by recreating the DPB buffer, but it should actually
// be sufficient to clear it like this.
// The error showed while scrubbing the ToS video in VLC.
dpb.clear();
nal_parser.remove_pending_input_data();
while (!image_units.empty()) {
delete image_units.back();
image_units.pop_back();
}
// --- start threads again ---
if (num_worker_threads>0) {
// TODO: need error checking
start_thread_pool(num_worker_threads);
}
}
void base_context::set_acceleration_functions(enum de265_acceleration l)
{
// fill scalar functions first (so that function table is completely filled)
init_acceleration_functions_fallback(&acceleration);
// override functions with optimized variants
#ifdef HAVE_SSE4_1
if (l>=de265_acceleration_SSE) {
init_acceleration_functions_sse(&acceleration);
}
#endif
#ifdef HAVE_ARM
if (l>=de265_acceleration_ARM) {
init_acceleration_functions_arm(&acceleration);
}
#endif
#ifdef WASM_SIMD
if (l>=de265_acceleration_WASM) {
init_acceleration_functions_wasm(&acceleration);
}
#endif
}
void decoder_context::init_thread_context(thread_context* tctx)
{
// zero scrap memory for coefficient blocks
memset(tctx->_coeffBuf, 0, sizeof(tctx->_coeffBuf)); // TODO: check if we can safely remove this
tctx->currentQG_x = -1;
tctx->currentQG_y = -1;
// --- find QPY that was active at the end of the previous slice ---
// find the previous CTB in TS order
const pic_parameter_set& pps = tctx->img->get_pps();
const seq_parameter_set& sps = tctx->img->get_sps();
if (tctx->shdr->slice_segment_address > 0) {
int prevCtb = pps.CtbAddrTStoRS[ pps.CtbAddrRStoTS[tctx->shdr->slice_segment_address] -1 ];
int ctbX = prevCtb % sps.PicWidthInCtbsY;
int ctbY = prevCtb / sps.PicWidthInCtbsY;
// take the pixel at the bottom right corner (but consider that the image size might be smaller)
int x = ((ctbX+1) << sps.Log2CtbSizeY)-1;
int y = ((ctbY+1) << sps.Log2CtbSizeY)-1;
x = std::min(x,sps.pic_width_in_luma_samples-1);
y = std::min(y,sps.pic_height_in_luma_samples-1);
//printf("READ QPY: %d %d -> %d (should %d)\n",x,y,imgunit->img->get_QPY(x,y), tc.currentQPY);
//if (tctx->shdr->dependent_slice_segment_flag) { // TODO: do we need this condition ?
tctx->currentQPY = tctx->img->get_QPY(x,y);
//}
}
}
void decoder_context::add_task_decode_CTB_row(thread_context* tctx,
bool firstSliceSubstream,
int ctbRow)
{
thread_task_ctb_row* task = new thread_task_ctb_row;
task->firstSliceSubstream = firstSliceSubstream;
task->tctx = tctx;
task->debug_startCtbRow = ctbRow;
tctx->task = task;
add_task(&thread_pool_, task);
tctx->imgunit->tasks.push_back(task);
}
void decoder_context::add_task_decode_slice_segment(thread_context* tctx, bool firstSliceSubstream,
int ctbx,int ctby)
{
thread_task_slice_segment* task = new thread_task_slice_segment;
task->firstSliceSubstream = firstSliceSubstream;
task->tctx = tctx;
task->debug_startCtbX = ctbx;
task->debug_startCtbY = ctby;
tctx->task = task;
add_task(&thread_pool_, task);
tctx->imgunit->tasks.push_back(task);
}
de265_error decoder_context::read_vps_NAL(bitreader& reader)
{
logdebug(LogHeaders,"---> read VPS\n");
std::shared_ptr<video_parameter_set> new_vps = std::make_shared<video_parameter_set>();
de265_error err = new_vps->read(this,&reader);
if (err != DE265_OK) {
return err;
}
if (param_vps_headers_fd>=0) {
new_vps->dump(param_vps_headers_fd);
}
vps[ new_vps->video_parameter_set_id ] = new_vps;
return DE265_OK;
}
de265_error decoder_context::read_sps_NAL(bitreader& reader)
{
logdebug(LogHeaders,"----> read SPS\n");
std::shared_ptr<seq_parameter_set> new_sps = std::make_shared<seq_parameter_set>();
de265_error err;
if ((err=new_sps->read(this, &reader)) != DE265_OK) {
return err;
}
if (param_sps_headers_fd>=0) {
new_sps->dump(param_sps_headers_fd);
}
sps[ new_sps->seq_parameter_set_id ] = new_sps;
return DE265_OK;
}
de265_error decoder_context::read_pps_NAL(bitreader& reader)
{
logdebug(LogHeaders,"----> read PPS\n");
std::shared_ptr<pic_parameter_set> new_pps = std::make_shared<pic_parameter_set>();
bool success = new_pps->read(&reader,this);
if (param_pps_headers_fd>=0) {
new_pps->dump(param_pps_headers_fd);
}
if (success) {
pps[ (int)new_pps->pic_parameter_set_id ] = new_pps;
}
return success ? DE265_OK : DE265_WARNING_PPS_HEADER_INVALID;
}
de265_error decoder_context::read_sei_NAL(bitreader& reader, bool suffix)
{
logdebug(LogHeaders,"----> read SEI\n");
sei_message sei;
//push_current_picture_to_output_queue();
de265_error err = DE265_OK;
if ((err=read_sei(&reader,&sei, suffix, current_sps.get())) == DE265_OK) {
dump_sei(&sei, current_sps.get());
if (image_units.empty()==false && suffix) {
image_units.back()->suffix_SEIs.push_back(sei);
}
}
else {
add_warning(err, false);
}
return err;
}
de265_error decoder_context::read_eos_NAL(bitreader& reader)
{
FirstAfterEndOfSequenceNAL = true;
return DE265_OK;
}
de265_error decoder_context::read_slice_NAL(bitreader& reader, NAL_unit* nal, nal_header& nal_hdr)
{
logdebug(LogHeaders,"---> read slice segment header\n");
// --- read slice header ---
slice_segment_header* shdr = new slice_segment_header;
bool continueDecoding;
de265_error err = shdr->read(&reader,this, &continueDecoding);
if (!continueDecoding) {
if (img) { img->integrity = INTEGRITY_NOT_DECODED; }
nal_parser.free_NAL_unit(nal);
delete shdr;
return err;
}
if (param_slice_headers_fd>=0) {
shdr->dump_slice_segment_header(this, param_slice_headers_fd);
}
if (process_slice_segment_header(shdr, &err, nal->pts, &nal_hdr, nal->user_data) == false)
{
if (img!=NULL) img->integrity = INTEGRITY_NOT_DECODED;
nal_parser.free_NAL_unit(nal);
delete shdr;
return err;
}
this->img->add_slice_segment_header(shdr);
skip_bits(&reader,1); // TODO: why?
prepare_for_CABAC(&reader);
// modify entry_point_offsets
int headerLength = reader.data - nal->data();
for (int i=0;i<shdr->num_entry_point_offsets;i++) {
shdr->entry_point_offset[i] -= nal->num_skipped_bytes_before(shdr->entry_point_offset[i],
headerLength);
}
// --- start a new image if this is the first slice ---
if (shdr->first_slice_segment_in_pic_flag) {
image_unit* imgunit = new image_unit;
imgunit->img = this->img;
image_units.push_back(imgunit);
}
// --- add slice to current picture ---
if ( ! image_units.empty() ) {
slice_unit* sliceunit = new slice_unit(this);
sliceunit->nal = nal;
sliceunit->shdr = shdr;
sliceunit->reader = reader;
sliceunit->flush_reorder_buffer = flush_reorder_buffer_at_this_frame;
image_units.back()->slice_units.push_back(sliceunit);
}
bool did_work;
err = decode_some(&did_work);
return DE265_OK;
}
template <class T> void pop_front(std::vector<T>& vec)
{
for (int i=1;i<vec.size();i++)
vec[i-1] = vec[i];
vec.pop_back();
}
de265_error decoder_context::decode_some(bool* did_work)
{
de265_error err = DE265_OK;
*did_work = false;
if (image_units.empty()) { return DE265_OK; } // nothing to do
// decode something if there is work to do
if ( ! image_units.empty() ) { // && ! image_units[0]->slice_units.empty() ) {
image_unit* imgunit = image_units[0];
slice_unit* sliceunit = imgunit->get_next_unprocessed_slice_segment();
if (sliceunit != NULL) {
//pop_front(imgunit->slice_units);
if (sliceunit->flush_reorder_buffer) {
dpb.flush_reorder_buffer();
}
*did_work = true;
//err = decode_slice_unit_sequential(imgunit, sliceunit);
err = decode_slice_unit_parallel(imgunit, sliceunit);
if (err) {
return err;
}
//delete sliceunit;
}
}
// if we decoded all slices of the current image and there will not
// be added any more slices to the image, output the image
if ( ( image_units.size()>=2 && image_units[0]->all_slice_segments_processed()) ||
( image_units.size()>=1 && image_units[0]->all_slice_segments_processed() &&
nal_parser.number_of_NAL_units_pending()==0 &&
(nal_parser.is_end_of_stream() || nal_parser.is_end_of_frame()) )) {
image_unit* imgunit = image_units[0];
*did_work=true;
// mark all CTBs as decoded even if they are not, because faulty input
// streams could miss part of the picture
// TODO: this will not work when slice decoding is parallel to post-filtering,
// so we will have to replace this with keeping track of which CTB should have
// been decoded (but aren't because of the input stream being faulty)
imgunit->img->mark_all_CTB_progress(CTB_PROGRESS_PREFILTER);
// run post-processing filters (deblocking & SAO)
if (img->decctx->num_worker_threads)
run_postprocessing_filters_parallel(imgunit);
else
run_postprocessing_filters_sequential(imgunit->img);
// process suffix SEIs
for (int i=0;i<imgunit->suffix_SEIs.size();i++) {
const sei_message& sei = imgunit->suffix_SEIs[i];
err = process_sei(&sei, imgunit->img);
if (err != DE265_OK)
break;
}
push_picture_to_output_queue(imgunit);
// remove just decoded image unit from queue
delete imgunit;
pop_front(image_units);
}
return err;
}
de265_error decoder_context::decode_slice_unit_sequential(image_unit* imgunit,
slice_unit* sliceunit)
{
de265_error err = DE265_OK;
/*
printf("decode slice POC=%d addr=%d, img=%p\n",
sliceunit->shdr->slice_pic_order_cnt_lsb,
sliceunit->shdr->slice_segment_address,
imgunit->img);
*/
remove_images_from_dpb(sliceunit->shdr->RemoveReferencesList);
if (sliceunit->shdr->slice_segment_address >= imgunit->img->get_pps().CtbAddrRStoTS.size()) {
return DE265_ERROR_CTB_OUTSIDE_IMAGE_AREA;
}
struct thread_context tctx;
tctx.shdr = sliceunit->shdr;
tctx.img = imgunit->img;
tctx.decctx = this;
tctx.imgunit = imgunit;
tctx.sliceunit= sliceunit;
tctx.CtbAddrInTS = imgunit->img->get_pps().CtbAddrRStoTS[tctx.shdr->slice_segment_address];
tctx.task = NULL;
init_thread_context(&tctx);
if (sliceunit->reader.bytes_remaining <= 0) {
return DE265_ERROR_PREMATURE_END_OF_SLICE;
}
init_CABAC_decoder(&tctx.cabac_decoder,
sliceunit->reader.data,
sliceunit->reader.bytes_remaining);
// alloc CABAC-model array if entropy_coding_sync is enabled
if (imgunit->img->get_pps().entropy_coding_sync_enabled_flag &&
sliceunit->shdr->first_slice_segment_in_pic_flag) {
imgunit->ctx_models.resize( (img->get_sps().PicHeightInCtbsY-1) ); //* CONTEXT_MODEL_TABLE_LENGTH );
}
sliceunit->nThreads=1;
err=read_slice_segment_data(&tctx);
sliceunit->finished_threads.set_progress(1);
return err;
}
void decoder_context::mark_whole_slice_as_processed(image_unit* imgunit,
slice_unit* sliceunit,
int progress)
{
//printf("mark whole slice\n");
// mark all CTBs upto the next slice segment as processed
slice_unit* nextSegment = imgunit->get_next_slice_segment(sliceunit);
if (nextSegment) {
/*
printf("mark whole slice between %d and %d\n",
sliceunit->shdr->slice_segment_address,
nextSegment->shdr->slice_segment_address);
*/
for (int ctb=sliceunit->shdr->slice_segment_address;
ctb < nextSegment->shdr->slice_segment_address;
ctb++)
{
if (ctb >= imgunit->img->number_of_ctbs())
break;
imgunit->img->ctb_progress[ctb].set_progress(progress);
}
}
}
de265_error decoder_context::decode_slice_unit_parallel(image_unit* imgunit,
slice_unit* sliceunit)
{
de265_error err = DE265_OK;
remove_images_from_dpb(sliceunit->shdr->RemoveReferencesList);
/*
printf("-------- decode --------\n");
printf("IMAGE UNIT %p\n",imgunit);
sliceunit->shdr->dump_slice_segment_header(sliceunit->ctx, 1);
imgunit->dump_slices();
*/
de265_image* img = imgunit->img;
const pic_parameter_set& pps = img->get_pps();
sliceunit->state = slice_unit::InProgress;
bool use_WPP = (img->decctx->num_worker_threads > 0 &&
pps.entropy_coding_sync_enabled_flag);
bool use_tiles = (img->decctx->num_worker_threads > 0 &&
pps.tiles_enabled_flag);
// TODO: remove this warning later when we do frame-parallel decoding
if (img->decctx->num_worker_threads > 0 &&
pps.entropy_coding_sync_enabled_flag == false &&
pps.tiles_enabled_flag == false) {
img->decctx->add_warning(DE265_WARNING_NO_WPP_CANNOT_USE_MULTITHREADING, true);
}
// If this is the first slice segment, mark all CTBs before this as processed
// (the real first slice segment could be missing).
if (imgunit->is_first_slice_segment(sliceunit)) {
slice_segment_header* shdr = sliceunit->shdr;
int firstCTB = shdr->slice_segment_address;
for (int ctb=0;ctb<firstCTB;ctb++) {
//printf("mark pre progress %d\n",ctb);
img->ctb_progress[ctb].set_progress(CTB_PROGRESS_PREFILTER);
}
}
// if there is a previous slice that has been completely decoded,
// mark all CTBs until the start of this slice as completed
//printf("this slice: %p\n",sliceunit);
slice_unit* prevSlice = imgunit->get_prev_slice_segment(sliceunit);
//if (prevSlice) printf("prev slice state: %d\n",prevSlice->state);
if (prevSlice && prevSlice->state == slice_unit::Decoded) {
mark_whole_slice_as_processed(imgunit,prevSlice,CTB_PROGRESS_PREFILTER);
}
// TODO: even though we cannot split this into several tasks, we should run it
// as a background thread
if (!use_WPP && !use_tiles) {
//printf("SEQ\n");
err = decode_slice_unit_sequential(imgunit, sliceunit);
sliceunit->state = slice_unit::Decoded;
mark_whole_slice_as_processed(imgunit,sliceunit,CTB_PROGRESS_PREFILTER);
return err;
}
if (use_WPP && use_tiles) {
// TODO: this is not allowed ... output some warning or error
return DE265_WARNING_PPS_HEADER_INVALID;
}
if (use_WPP) {
//printf("WPP\n");
err = decode_slice_unit_WPP(imgunit, sliceunit);
sliceunit->state = slice_unit::Decoded;
mark_whole_slice_as_processed(imgunit,sliceunit,CTB_PROGRESS_PREFILTER);
return err;
}
else if (use_tiles) {
//printf("TILE\n");
err = decode_slice_unit_tiles(imgunit, sliceunit);
sliceunit->state = slice_unit::Decoded;
mark_whole_slice_as_processed(imgunit,sliceunit,CTB_PROGRESS_PREFILTER);
return err;
}
assert(false);
return err;
}
de265_error decoder_context::decode_slice_unit_WPP(image_unit* imgunit,
slice_unit* sliceunit)
{
de265_error err = DE265_OK;
de265_image* img = imgunit->img;
slice_segment_header* shdr = sliceunit->shdr;
const pic_parameter_set& pps = img->get_pps();
int nRows = shdr->num_entry_point_offsets +1;
int ctbsWidth = img->get_sps().PicWidthInCtbsY;