Newer
Older

Karsten Suehring
committed
/* The copyright in this software is being made available under the BSD
* License, included below. This software may be subject to other third party
* and contributor rights, including patent rights, and no such rights are
* granted under this license.
*
* Copyright (c) 2010-2020, ITU/ISO/IEC

Karsten Suehring
committed
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
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of the ITU/ISO/IEC nor the names of its contributors may
* be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
/** \file DecApp.cpp
\brief Decoder application class
*/
#include <list>
#include <vector>
#include <stdio.h>
#include <fcntl.h>
#include "DecApp.h"
#include "DecoderLib/AnnexBread.h"
#include "DecoderLib/NALread.h"
#if RExt__DECODER_DEBUG_STATISTICS
#include "CommonLib/CodingStatistics.h"
#endif
#include "CommonLib/dtrace_codingstruct.h"
//! \ingroup DecoderApp
//! \{
// ====================================================================================================================
// Constructor / destructor / initialization / destroy
// ====================================================================================================================
DecApp::DecApp()
: m_iPOCLastDisplay(-MAX_INT)
{
}
// ====================================================================================================================
// Public member functions
// ====================================================================================================================
/**
- create internal class
- initialize internal class
- until the end of the bitstream, call decoding function in DecApp class
- delete allocated buffers
- destroy internal class
- returns the number of mismatching pictures
*/
uint32_t DecApp::decode()
{
int poc;
PicList* pcListPic = NULL;
ifstream bitstreamFile(m_bitstreamFileName.c_str(), ifstream::in | ifstream::binary);
if (!bitstreamFile)
{
EXIT( "Failed to open bitstream file " << m_bitstreamFileName.c_str() << " for reading" ) ;
}
InputByteStream bytestream(bitstreamFile);
if (!m_outputDecodedSEIMessagesFilename.empty() && m_outputDecodedSEIMessagesFilename!="-")
{
m_seiMessageFileStream.open(m_outputDecodedSEIMessagesFilename.c_str(), std::ios::out);
if (!m_seiMessageFileStream.is_open() || !m_seiMessageFileStream.good())
{
EXIT( "Unable to open file "<< m_outputDecodedSEIMessagesFilename.c_str() << " for writing decoded SEI messages");
}
}
// create & initialize internal classes
xCreateDecLib();
m_iPOCLastDisplay += m_iSkipFrame; // set the last displayed POC correctly for skip forward.
// clear contents of colour-remap-information-SEI output file
if (!m_colourRemapSEIFileName.empty())
{
std::ofstream ofile(m_colourRemapSEIFileName.c_str());
if (!ofile.good() || !ofile.is_open())
{
EXIT( "Unable to open file " << m_colourRemapSEIFileName.c_str() << " for writing colour-remap-information-SEI video");
}
}
// main decoder loop
bool loopFiltered = false;
bool bPicSkipped = false;

Karsten Suehring
committed
while (!!bitstreamFile)
{
InputNALUnit nalu;
nalu.m_nalUnitType = NAL_UNIT_INVALID;
// determine if next NAL unit will be the first one from a new picture
bool bNewPicture = isNewPicture(&bitstreamFile, &bytestream);
bool bNewAccessUnit = bNewPicture && isNewAccessUnit( bNewPicture, &bitstreamFile, &bytestream );
if(!bNewPicture)
{
AnnexBStats stats = AnnexBStats();
// find next NAL unit in stream
byteStreamNALUnit(bytestream, nalu.getBitstream().getFifo(), stats);
if (nalu.getBitstream().getFifo().empty())
{
/* this can happen if the following occur:
* - empty input file
* - two back-to-back start_code_prefixes
* - start_code_prefix immediately followed by EOF
*/
msg( ERROR, "Warning: Attempt to decode an empty NAL unit\n");
}
else
{
// read NAL unit header
read(nalu);
// flush output for first slice of an IDR picture
if(m_cDecLib.getFirstSliceInPicture() &&
(nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL ||
nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP))
{
xFlushOutput(pcListPic, nalu.m_nuhLayerId);
}
// parse NAL unit syntax if within target decoding layer
if ((m_iMaxTemporalLayer < 0 || nalu.m_temporalId <= m_iMaxTemporalLayer) && isNaluWithinTargetDecLayerIdSet(&nalu))
if (bPicSkipped)
{
if ((nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_TRAIL) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_STSA) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RASL) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_RADL) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_W_RADL) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_IDR_N_LP) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_CRA) || (nalu.m_nalUnitType == NAL_UNIT_CODED_SLICE_GDR))
{
if (m_cDecLib.isSliceNaluFirstInAU(true, nalu))
{
m_cDecLib.resetAccessUnitNals();
m_cDecLib.resetAccessUnitApsNals();
}
bPicSkipped = false;
}
}
m_cDecLib.decode(nalu, m_iSkipFrame, m_iPOCLastDisplay);
if (nalu.m_nalUnitType == NAL_UNIT_VPS)
{
deriveOutputLayerSet();
}
else
{
bPicSkipped = true;
}

Karsten Suehring
committed
if ((bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS) && !m_cDecLib.getFirstSliceInSequence() && !bPicSkipped)

Karsten Suehring
committed
{
if (!loopFiltered || bitstreamFile)
{
m_cDecLib.executeLoopFilters();
m_cDecLib.finishPicture( poc, pcListPic );
}
loopFiltered = (nalu.m_nalUnitType == NAL_UNIT_EOS);
if (nalu.m_nalUnitType == NAL_UNIT_EOS)
{
m_cDecLib.setFirstSliceInSequence(true);
}
}
else if ( (bNewPicture || !bitstreamFile || nalu.m_nalUnitType == NAL_UNIT_EOS ) &&
m_cDecLib.getFirstSliceInSequence () )
{
m_cDecLib.setFirstSliceInPicture (true);

Karsten Suehring
committed
}
if( pcListPic )
{
if( !m_reconFileName.empty() && !m_cVideoIOYuvReconFile[nalu.m_nuhLayerId].isOpen() )

Karsten Suehring
committed
{
const BitDepths &bitDepths=pcListPic->front()->cs->sps->getBitDepths(); // use bit depths of first reconstructed picture.
for( uint32_t channelType = 0; channelType < MAX_NUM_CHANNEL_TYPE; channelType++ )
{
if( m_outputBitDepth[channelType] == 0 )
{
m_outputBitDepth[channelType] = bitDepths.recon[channelType];
}
}
if (m_packedYUVMode && (m_outputBitDepth[CH_L] != 10 && m_outputBitDepth[CH_L] != 12))
{
EXIT ("Invalid output bit-depth for packed YUV output, aborting\n");
}
std::string reconFileName = m_reconFileName;
if (m_reconFileName.compare("/dev/null") && (m_cDecLib.getVPS() != nullptr) && (m_cDecLib.getVPS()->getMaxLayers() > 1) && (isNaluWithinTargetOutputLayerIdSet(&nalu)))
size_t pos = reconFileName.find_last_of('.');
if (pos != string::npos)
reconFileName.insert( pos, std::to_string( nalu.m_nuhLayerId ) );
}
else
{
reconFileName.append( std::to_string( nalu.m_nuhLayerId ) );
Remy Foray
committed
if(((m_cDecLib.getVPS() != nullptr) &&
((m_cDecLib.getVPS()->getMaxLayers() == 1) || (isNaluWithinTargetOutputLayerIdSet(&nalu)))) ||
(m_cDecLib.getVPS() == nullptr))
m_cVideoIOYuvReconFile[nalu.m_nuhLayerId].open(reconFileName, true, m_outputBitDepth, m_outputBitDepth, bitDepths.recon); // write mode

Karsten Suehring
committed
}
// write reconstruction to file
if( bNewPicture )
{
xWriteOutput( pcListPic, nalu.m_temporalId );
}
if (nalu.m_nalUnitType == NAL_UNIT_EOS)
{
xWriteOutput( pcListPic, nalu.m_temporalId );
m_cDecLib.setFirstSliceInPicture (false);
}
// write reconstruction to file -- for additional bumping as defined in C.5.2.3
if (!bNewPicture && ((nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_TRAIL && nalu.m_nalUnitType <= NAL_UNIT_RESERVED_IRAP_VCL_12)
|| (nalu.m_nalUnitType >= NAL_UNIT_CODED_SLICE_IDR_W_RADL && nalu.m_nalUnitType <= NAL_UNIT_CODED_SLICE_GDR)))

Karsten Suehring
committed
{
xWriteOutput( pcListPic, nalu.m_temporalId );
}
}
{
m_cDecLib.resetAccessUnitNals();
m_cDecLib.resetAccessUnitApsNals();
}

Karsten Suehring
committed
}
xFlushOutput( pcListPic );
// get the number of checksum errors
uint32_t nRet = m_cDecLib.getNumberOfChecksumErrorsDetected();
// delete buffers
m_cDecLib.deletePicBuffer();
// destroy internal classes
xDestroyDecLib();
#if RExt__DECODER_DEBUG_STATISTICS
CodingStatistics::DestroyInstance();
#endif
destroyROM();
return nRet;
}
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
bool DecApp::deriveOutputLayerSet()
{
int vps_max_layers_minus1 = m_cDecLib.getVPS()->getMaxLayers() - 1;
int TotalNumOlss = 0;
int each_layer_is_an_ols_flag = m_cDecLib.getVPS()->getEachLayerIsAnOlsFlag();
int ols_mode_idc = m_cDecLib.getVPS()->getOlsModeIdc();
int num_output_layer_sets_minus1 = m_cDecLib.getVPS()->getNumOutputLayerSets() - 1;
int i = 0, j = 0, k = 0, r = 0;
int* NumOutputLayersInOls;
int* NumLayersInOls;
int** OutputLayerIdInOls;
int** OutputLayerIdx;
int** layerIncludedInOlsFlag;
int** LayerIdInOls;
int** dependencyFlag;
int** RefLayerIdx;
int* NumRefLayers;
if (vps_max_layers_minus1 == 0)
TotalNumOlss = 1;
else if (each_layer_is_an_ols_flag || ols_mode_idc == 0 || ols_mode_idc == 1)
TotalNumOlss = vps_max_layers_minus1 + 1;
else if (ols_mode_idc == 2)
TotalNumOlss = num_output_layer_sets_minus1 + 1;
NumOutputLayersInOls = new int[m_cDecLib.getVPS()->getNumOutputLayerSets()];
NumLayersInOls = new int[m_cDecLib.getVPS()->getNumOutputLayerSets()];
OutputLayerIdInOls = new int*[TotalNumOlss];
OutputLayerIdx = new int*[TotalNumOlss];
layerIncludedInOlsFlag = new int*[TotalNumOlss];
LayerIdInOls = new int*[TotalNumOlss];
for (i = 0; i < TotalNumOlss; i++)
{
OutputLayerIdInOls[i] = new int[vps_max_layers_minus1 + 1];
OutputLayerIdx[i] = new int[vps_max_layers_minus1 + 1];
layerIncludedInOlsFlag[i] = new int[vps_max_layers_minus1 + 1];
LayerIdInOls[i] = new int[vps_max_layers_minus1 + 1];
}
dependencyFlag = new int*[vps_max_layers_minus1 + 1];
RefLayerIdx = new int*[vps_max_layers_minus1 + 1];
NumRefLayers = new int[vps_max_layers_minus1 + 1];
for (i = 0; i <= vps_max_layers_minus1; i++)
{
dependencyFlag[i] = new int[vps_max_layers_minus1 + 1];
RefLayerIdx[i] = new int[vps_max_layers_minus1 + 1];
}
for (i = 0; i <= vps_max_layers_minus1; i++) {
for (j = 0; j <= vps_max_layers_minus1; j++) {
dependencyFlag[i][j] = m_cDecLib.getVPS()->getDirectRefLayerFlag(i, j);
for (k = 0; k < i; k++)
if (m_cDecLib.getVPS()->getDirectRefLayerFlag(i, k) && dependencyFlag[k][j])
dependencyFlag[i][j] = 1;
}
}
for (i = 0; i <= vps_max_layers_minus1; i++)
{
for (j = 0, r = 0; j <= vps_max_layers_minus1; j++)
{
if (dependencyFlag[i][j])
RefLayerIdx[i][r++] = j;
}
NumRefLayers[i] = r;
}
NumOutputLayersInOls[0] = 1;
OutputLayerIdInOls[0][0] = m_cDecLib.getVPS()->getLayerId(0);
for (i = 1; i < TotalNumOlss; i++)
{
if (each_layer_is_an_ols_flag || ols_mode_idc == 0)
{
NumOutputLayersInOls[i] = 1;
OutputLayerIdInOls[i][0] = m_cDecLib.getVPS()->getLayerId(i);
}
else if (ols_mode_idc == 1) {
NumOutputLayersInOls[i] = i + 1;
for (j = 0; j < NumOutputLayersInOls[i]; j++)
OutputLayerIdInOls[i][j] = m_cDecLib.getVPS()->getLayerId(j);
}
else if (ols_mode_idc == 2) {
for (j = 0; j <= vps_max_layers_minus1; j++)
{
layerIncludedInOlsFlag[i][j] = 0;
}
for (k = 0, j = 0; k <= vps_max_layers_minus1; k++)
{
if (m_cDecLib.getVPS()->getOlsOutputLayerFlag(i, k))
{
layerIncludedInOlsFlag[i][k] = 1;
OutputLayerIdx[i][j] = k;
OutputLayerIdInOls[i][j++] = m_cDecLib.getVPS()->getLayerId(k);
}
}
NumOutputLayersInOls[i] = j;
for (j = 0; j < NumOutputLayersInOls[i]; j++)
{
int idx = OutputLayerIdx[i][j];
for (k = 0; k < NumRefLayers[idx]; k++)
layerIncludedInOlsFlag[i][RefLayerIdx[idx][k]] = 1;
}
}
}
m_targetOutputLayerIdSet.clear();
for (i = 0; i < NumOutputLayersInOls[m_iTargetOLS]; i++)
m_targetOutputLayerIdSet.push_back(OutputLayerIdInOls[m_iTargetOLS][i]);
NumLayersInOls[0] = 1;
LayerIdInOls[0][0] = m_cDecLib.getVPS()->getLayerId(0);
for (i = 1; i < TotalNumOlss; i++)
{
if (each_layer_is_an_ols_flag)
{
NumLayersInOls[i] = 1;
LayerIdInOls[i][0] = m_cDecLib.getVPS()->getLayerId(i);
}
else if (ols_mode_idc == 0 || ols_mode_idc == 1)
{
NumLayersInOls[i] = i + 1;
for (j = 0; j < NumLayersInOls[i]; j++)
LayerIdInOls[i][j] = m_cDecLib.getVPS()->getLayerId(j);
}
else if (ols_mode_idc == 2)
{
for (k = 0, j = 0; k <= vps_max_layers_minus1; k++)
if (layerIncludedInOlsFlag[i][k])
LayerIdInOls[i][j++] = m_cDecLib.getVPS()->getLayerId(k);
NumLayersInOls[i] = j;
}
}
m_targetDecLayerIdSet.clear();
for (i = 0; i < NumLayersInOls[m_iTargetOLS]; i++)
m_targetDecLayerIdSet.push_back(LayerIdInOls[m_iTargetOLS][i]);
delete[] NumOutputLayersInOls;
delete[] NumLayersInOls;
delete[] NumRefLayers;
for (i = 0; i < TotalNumOlss; i++)
{
delete[] OutputLayerIdInOls[i];
delete[] OutputLayerIdx[i];
delete[] layerIncludedInOlsFlag[i];
delete[] LayerIdInOls[i];
}
delete[] OutputLayerIdInOls;
delete[] OutputLayerIdx;
delete[] layerIncludedInOlsFlag;
delete[] LayerIdInOls;
for (i = 0; i <= vps_max_layers_minus1; i++)
{
delete[] dependencyFlag[i];
delete[] RefLayerIdx[i];
}
delete[] dependencyFlag;
delete[] RefLayerIdx;
return true;
}
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
/**
- lookahead through next NAL units to determine if current NAL unit is the first NAL unit in a new picture
*/
bool DecApp::isNewPicture(ifstream *bitstreamFile, class InputByteStream *bytestream)
{
bool ret = false;
bool finished = false;
// cannot be a new picture if there haven't been any slices yet
if(m_cDecLib.getFirstSliceInPicture())
{
return false;
}
// save stream position for backup
#if RExt__DECODER_DEBUG_STATISTICS
CodingStatistics::CodingStatisticsData* backupStats = new CodingStatistics::CodingStatisticsData(CodingStatistics::GetStatistics());
streampos location = bitstreamFile->tellg() - streampos(bytestream->GetNumBufferedBytes());
#else
streampos location = bitstreamFile->tellg();
#endif
// look ahead until picture start location is determined
while (!finished && !!(*bitstreamFile))
{
AnnexBStats stats = AnnexBStats();
InputNALUnit nalu;
byteStreamNALUnit(*bytestream, nalu.getBitstream().getFifo(), stats);
if (nalu.getBitstream().getFifo().empty())
{
msg( ERROR, "Warning: Attempt to decode an empty NAL unit\n");
}
else
{
// get next NAL unit type
read(nalu);
switch( nalu.m_nalUnitType
Loading
Loading full blame...