LCOV - code coverage report
Current view: top level - Codec - EbPredictionStructure.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 359 368 97.6 %
Date: 2019-11-25 17:38:06 Functions: 5 5 100.0 %

          Line data    Source code
       1             : /*
       2             : * Copyright(c) 2019 Intel Corporation
       3             : * SPDX - License - Identifier: BSD - 2 - Clause - Patent
       4             : */
       5             : 
       6             : #include <stdlib.h>
       7             : #include <string.h>
       8             : 
       9             : #include "EbDefinitions.h"
      10             : #include "EbPredictionStructure.h"
      11             : #include "EbUtility.h"
      12             : 
      13             : /**********************************************************
      14             :  * Macros
      15             :  **********************************************************/
      16             : #define PRED_STRUCT_INDEX(hierarchicalLevelCount, predType, refCount) (((hierarchicalLevelCount) * EB_PRED_TOTAL_COUNT + (predType)) * REF_LIST_MAX_DEPTH + (refCount))
      17             : 
      18             :  //#define DEP_INDEX(predIdx, entryIndex, entryTotalCount) ((((predIdx) - ((int32_t) entryIndex)) % (entryTotalCount)))
      19             : #define DEP_INDEX(predIdx, entryIndex, entryTotalCount) (((((int32_t) entryIndex) - (predIdx)) % (entryTotalCount)))
      20             : 
      21             : /**********************************************************
      22             :  * Instructions for how to create a Predicion Structure
      23             :  *
      24             :  * Overview:
      25             :  *   The prediction structure consists of a collection
      26             :  *   of Prediction Structure Entires, which themselves
      27             :  *   consist of reference and dependent lists.  The
      28             :  *   reference lists are exactly like those found in
      29             :  *   the standard and can be clipped in order to reduce
      30             :  *   the number of references.
      31             :  *
      32             :  *   Dependent lists are the corollary to reference lists,
      33             :  *   the describe how a particular picture is referenced.
      34             :  *   Dependent lists can also be clipped at predefined
      35             :  *   junctions (i.e. the list_count array) in order
      36             :  *   to reduce the number of references.  Note that the
      37             :  *   dependent deltaPOCs must be grouped together in order
      38             :  *   of ascending referencePicture in order for the Dependent
      39             :  *   List clip to work properly.
      40             :  *
      41             :  *   All control and RPS information is derived from
      42             :  *   these lists.  The lists for a structure are defined
      43             :  *   for both P & B-picture variants.  In the case of
      44             :  *   P-pictures, only Lists 0 are used.
      45             :  *
      46             :  *   Negative deltaPOCs are for backward-referencing pictures
      47             :  *   in display order and positive deltaPOCs are for
      48             :  *   forward-referencing pictures.
      49             :  *
      50             :  *   Please note that there is no assigned coding order,
      51             :  *   the PictureManager will start pictures as soon as
      52             :  *   their references become available.
      53             :  *
      54             :  *   Any prediction structure is possible; however, we are
      55             :  *     restricting usage to the following controls:
      56             :  *     # Hierarchical Levels
      57             :  *     # Number of References
      58             :  *     # B-pictures enabled
      59             :  *     # Intra Refresh Period
      60             :  *
      61             :  *  To Get Low Delay P, only use List 0
      62             :  *  To Get Low Delay B, replace List 1 with List 0
      63             :  *  To Get Random Access, use the preduction structure as is
      64             :  **********************************************************/
      65             : 
      66             :  /************************************************
      67             :   * Flat
      68             :   *
      69             :   *  I-B-B-B-B-B-B-B-B
      70             :   *
      71             :   * Display & Coding Order:
      72             :   *  0 1 2 3 4 5 6 7 8
      73             :   *
      74             :   ************************************************/
      75             : static PredictionStructureConfigEntry flat_pred_struct[] = {
      76             :     {
      77             :         0,               // GOP Index 0 - Temporal Layer
      78             :         0,               // GOP Index 0 - Decode Order
      79             :         {1, 2, 3, 4},    // GOP Index 0 - Ref List 0
      80             :         {1, 2, 3, 4}     // GOP Index 0 - Ref List 1
      81             :     }
      82             : };
      83             : 
      84             : /************************************************
      85             :  * Random Access - Two-Level Hierarchical
      86             :  *
      87             :  *    b   b   b   b      Temporal Layer 1
      88             :  *   / \ / \ / \ / \
      89             :  *  I---B---B---B---B    Temporal Layer 0
      90             :  *
      91             :  * Display Order:
      92             :  *  0 1 2 3 4 5 6 7 8
      93             :  *
      94             :  * Coding Order:
      95             :  *  0 2 1 4 3 6 5 8 7
      96             :  ************************************************/
      97             : static PredictionStructureConfigEntry two_level_hierarchical_pred_struct[] = {
      98             :     {
      99             :         0,                // GOP Index 0 - Temporal Layer
     100             :         0,                // GOP Index 0 - Decode Order
     101             :         {2, 4, 6, 8},     // GOP Index 0 - Ref List 0
     102             :         {2, 4, 6, 8}      // GOP Index 0 - Ref List 1
     103             :     },
     104             :     {
     105             :         1,                // GOP Index 1 - Temporal Layer
     106             :         1,                // GOP Index 1 - Decode Order
     107             :         { 1, 3, 5, 7},    // GOP Index 1 - Ref List 0
     108             :         {-1, 1, 3, 5}     // GOP Index 1 - Ref List 1
     109             :     }
     110             : };
     111             : 
     112             : /************************************************
     113             :  * Three-Level Hierarchical
     114             :  *
     115             :  *      b   b       b   b       b   b        Temporal Layer 2
     116             :  *     / \ / \     / \ / \     / \ / \
     117             :  *    /   B   \   /   B   \   /   B   \      Temporal Layer 1
     118             :  *   /   / \   \ /   / \   \ /   / \   \
     119             :  *  I-----------B-----------B-----------B    Temporal Layer 0
     120             :  *
     121             :  * Display Order:
     122             :  *  0   1 2 3   4   5 6 7   8   9 1 1   1
     123             :  *                                0 1   2
     124             :  *
     125             :  * Coding Order:
     126             :  *  0   3 2 4   1   7 6 8   5   1 1 1   9
     127             :  *                              1 0 2
     128             :  ************************************************/
     129             : static PredictionStructureConfigEntry three_level_hierarchical_pred_struct[] = {
     130             :     {
     131             :         0,                  // GOP Index 0 - Temporal Layer
     132             :         0,                  // GOP Index 0 - Decode Order
     133             :         {4, 8, 12, 16},     // GOP Index 0 - Ref List 0
     134             :         {4, 8, 12, 16}      // GOP Index 0 - Ref List 1
     135             :     },
     136             :     {
     137             :         2,                  // GOP Index 1 - Temporal Layer
     138             :         2,                  // GOP Index 1 - Decode Order
     139             :         { 1,  3, 5, 0},     // GOP Index 1 - Ref List 0
     140             :         {-1, -3, 1, 3}      // GOP Index 1 - Ref List 1
     141             :     },
     142             :     {
     143             :         1,                  // GOP Index 2 - Temporal Layer
     144             :         1,                  // GOP Index 2 - Decode Order
     145             :         { 2, 6, 10, 14},    // GOP Index 2 - Ref List 0
     146             :         {-2, 2,  6, 10}     // GOP Index 2 - Ref List 1
     147             :     },
     148             :     {
     149             :         2,                  // GOP Index 3 - Temporal Layer
     150             :         3,                  // GOP Index 3 - Decode Order
     151             :         { 1, 3, 5, 7},      // GOP Index 3 - Ref List 0
     152             :         {-1, 1, 3, 5}       // GOP Index 3 - Ref List 1
     153             :     }
     154             : };
     155             : 
     156             : /************************************************************************************************************
     157             :  * Four-Level Hierarchical
     158             :  *
     159             :  *
     160             :  *          b     b           b     b               b     b           b     b           Temporal Layer 3
     161             :  *         / \   / \         / \   / \             / \   / \         / \   / \
     162             :  *        /   \ /   \       /   \ /   \           /   \ /   \       /   \ /   \
     163             :  *       /     B     \     /     B     \         /     B     \     /     B     \        Temporal Layer 2
     164             :  *      /     / \     \   /     / \     \       /     / \     \   /     / \     \
     165             :  *     /     /   \     \ /     /   \     \     /     /   \     \ /     /   \     \
     166             :  *    /     /     ------B------     \     \   /     /     ------B------     \     \     Temporal Layer 1
     167             :  *   /     /           / \           \     \ /     /           / \           \     \
     168             :  *  I---------------------------------------B---------------------------------------B   Temporal Layer 0
     169             :  *
     170             :  * Display Order:
     171             :  *  0       1  2  3     4     5  6  7       8       9  1  1     1     1  1  1       1
     172             :  *                                                     0  1     2     3  4  5       6
     173             :  *
     174             :  * Coding Order:
     175             :  *  0       4  3  5     2     7  6  8       1       1  1  1     1     1  1  1       9
     176             :  *                                                  2  1  3     0     5  4  6
     177             :  *
     178             :  ***********************************************************************************************************/
     179             : PredictionStructureConfigEntry four_level_hierarchical_pred_struct[] = {
     180             :     {
     181             :         0,                      // GOP Index 0 - Temporal Layer
     182             :         0,                      // GOP Index 0 - Decode Order
     183             :         { 8, 0, 0, 0},          // GOP Index 0 - Ref List 0
     184             :         { 8, 0, 0, 0}           // GOP Index 0 - Ref List 1
     185             :     },
     186             :     {
     187             :         3,                      // GOP Index 1 - Temporal Layer
     188             :         3,                      // GOP Index 1 - Decode Order
     189             : #if PRED_CHANGE
     190             :         { 1, 3, 5, 8},          // GOP Index 1 - Ref List 0
     191             : #else
     192             :         { 1, 3, 5, 9},          // GOP Index 1 - Ref List 0
     193             : #endif
     194             :         {-1,-3,-7, 0}           // GOP Index 1 - Ref List 1
     195             :     },
     196             :     {
     197             :         2,                      // GOP Index 2 - Temporal Layer
     198             :         2,                      // GOP Index 2 - Decode Order
     199             :         { 2, 4, 6, 10},         // GOP Index 2 - Ref List 0
     200             :         {-2,-6, 0,  0}          // GOP Index 2 - Ref List 1
     201             :     },
     202             :     {
     203             :         3,                      // GOP Index 3 - Temporal Layer
     204             :         4,                      // GOP Index 3 - Decode Order
     205             : #if PRED_CHANGE_MOD
     206             :         { 1, 3, 2, 5},          // GOP Index 3 - Ref List 0
     207             : #elif PRED_CHANGE
     208             :         { 1, 2, 3, 5},          // GOP Index 3 - Ref List 0
     209             : #else
     210             :         { 1, 3, 5, 7},          // GOP Index 3 - Ref List 0
     211             : #endif
     212             :         {-1, -5, 0, 0}          // GOP Index 3 - Ref List 1
     213             :     },
     214             :     {
     215             :         1,                      // GOP Index 4 - Temporal Layer
     216             :         1,                      // GOP Index 4 - Decode Order
     217             :         { 4, 8, 12, 0},         // GOP Index 4 - Ref List 0
     218             :         {-4, 0,  0, 0}          // GOP Index 4 - Ref List 1
     219             :     },
     220             :     {
     221             :         3,                      // GOP Index 5 - Temporal Layer
     222             :         6,                      // GOP Index 5 - Decode Order
     223             : #if PRED_CHANGE
     224             :         { 1, 3, 5, 4},          // GOP Index 5 - Ref List 0
     225             : #else
     226             :         { 1, 3, 5, 9},          // GOP Index 5 - Ref List 0
     227             : #endif
     228             :         {-1, -3, 0, 0}          // GOP Index 5 - Ref List 1
     229             :     },
     230             :     {
     231             :         2,                      // GOP Index 6 - Temporal Layer
     232             :         5,                      // GOP Index 6 - Decode Order
     233             :         { 2, 4, 6, 10},         // GOP Index 6 - Ref List 0
     234             :         {-2, 0, 0,  0}          // GOP Index 6 - Ref List 1
     235             :     },
     236             :     {
     237             :         3,                      // GOP Index 7 - Temporal Layer
     238             :         7,                      // GOP Index 7 - Decode Order
     239             : #if PRED_CHANGE
     240             :         { 1, 3, 5, 6},          // GOP Index 7 - Ref List 0
     241             : #else
     242             :         { 1, 3, 5, 7},          // GOP Index 7 - Ref List 0
     243             : #endif
     244             :         {-1, 0, 0, 0}           // GOP Index 7 - Ref List 1
     245             :     }
     246             : };
     247             : 
     248             : /***********************************************************************************************************
     249             :  * Five-Level Level Hierarchical
     250             :  *
     251             :  *           b     b           b     b               b     b           b     b              Temporal Layer 4
     252             :  *          / \   / \         / \   / \             / \   / \         / \   / \
     253             :  *         /   \ /   \       /   \ /   \           /   \ /   \       /   \ /   \
     254             :  *        /     B     \     /     B     \         /     B     \     /     B     \           Temporal Layer 3
     255             :  *       /     / \     \   /     / \     \       /     / \     \   /     / \     \
     256             :  *      /     /   \     \ /     /   \     \     /     /   \     \ /     /   \     \
     257             :  *     /     /     ------B------     \     \   /     /     ------B------     \     \        Temporal Layer 2
     258             :  *    /     /           / \           \     \ /     /           / \           \     \
     259             :  *   /     /           /   \-----------------B------------------   \           \     \      Temporal Layer 1
     260             :  *  /     /           /                     / \                     \           \     \
     261             :  * I-----------------------------------------------------------------------------------B    Temporal Layer 0
     262             :  *
     263             :  * Display Order:
     264             :  *  0        1  2  3     4     5  6  7       8       9  1  1     1     1  1  1         1
     265             :  *                                                      0  1     2     3  4  5         6
     266             :  *
     267             :  * Coding Order:
     268             :  *  0        5  4  6     3     8  7  9       2       1  1  1     1     1  1  1         1
     269             :  *                                                   2  1  3     0     5  4  6
     270             :  *
     271             :  ***********************************************************************************************************/
     272             : PredictionStructureConfigEntry five_level_hierarchical_pred_struct[] = {
     273             : 
     274             :     {
     275             :         0,                      // GOP Index 0 - Temporal Layer
     276             :         0,                      // GOP Index 0 - Decode Order
     277             :         {16, 48, 0, 0},         // GOP Index 0 - Ref List 0
     278             :         {16, 32, 0, 0}          // GOP Index 0 - Ref List 1     //we need keep 16 as first entry in List1, this will ensure that for poc=16 there is a valid ref frame in List1.
     279             :     },
     280             :     {
     281             :         4,                      // GOP Index 1 - Temporal Layer
     282             :         4,                      // GOP Index 1 - Decode Order
     283             : #if PRED_CHANGE_MOD
     284             :         { 1, 9, 8, 17},          // GOP Index 1 - Ref List 0
     285             : #elif PRED_CHANGE_5L
     286             :         { 1, 8, 9, 17},          // GOP Index 1 - Ref List 0
     287             : #else
     288             :         { 1, 5, 9, 17},          // GOP Index 1 - Ref List 0
     289             : #endif
     290             :         { -1, -3, -7, 0}         // GOP Index 1 - Ref List 1
     291             :     },
     292             :     {
     293             :         3,                      // GOP Index 2 - Temporal Layer
     294             :         3,                      // GOP Index 2 - Decode Order
     295             : #if PRED_CHANGE_5L
     296             :         { 2, 4, 10, 18},        // GOP Index 2 - Ref List 0
     297             : #else
     298             :         { 2, 4, 6, 10},         // GOP Index 2 - Ref List 0
     299             : #endif
     300             :         { -2, -6, -14, 0}       // GOP Index 2 - Ref List 1
     301             :     },
     302             :     {
     303             :         4,                      // GOP Index 3 - Temporal Layer
     304             :         5,                      // GOP Index 3 - Decode Order
     305             : #if PRED_CHANGE_MOD
     306             :         { 1, 3, 2, 11},         // GOP Index 3 - Ref List 0
     307             : #elif PRED_CHANGE_5L
     308             :         { 1, 2, 3, 11},         // GOP Index 3 - Ref List 0
     309             : #else
     310             :         { 1, 3, 7, 11},         // GOP Index 3 - Ref List 0
     311             : #endif
     312             :         { -1, -5, -13, 0}       // GOP Index 3 - Ref List 1
     313             :     },
     314             :     {
     315             :         2,                      // GOP Index 4 - Temporal Layer
     316             :         2,                      // GOP Index 4 - Decode Order
     317             :         {  4, 8, 12, 20},       // GOP Index 4 - Ref List 0
     318             :         { -4, -12, 0, 0}        // GOP Index 4 - Ref List 1
     319             :     },
     320             :     {
     321             :         4,                      // GOP Index 5 - Temporal Layer
     322             :         7,                      // GOP Index 5 - Decode Order
     323             : #if PRED_CHANGE_MOD
     324             :         { 1, 5, 4, 13},         // GOP Index 5 - Ref List 0
     325             : #elif PRED_CHANGE_5L
     326             :         { 1, 4, 5, 13},         // GOP Index 5 - Ref List 0
     327             : #else
     328             :         { 1, 5, 9, 13},         // GOP Index 5 - Ref List 0
     329             : #endif
     330             :         { -1, -3, -11, 0}       // GOP Index 5 - Ref List 1
     331             :     },
     332             :     {
     333             :         3,                      // GOP Index 6 - Temporal Layer
     334             :         6,                      // GOP Index 6 - Decode Order
     335             : #if PRED_CHANGE_5L
     336             :         { 2, 4, 6, 14},         // GOP Index 6 - Ref List 0
     337             : #else
     338             :         { 2, 4, 6, 10},         // GOP Index 6 - Ref List 0
     339             : #endif
     340             :         { -2, -10, 0, 0}        // GOP Index 6 - Ref List 1
     341             :     },
     342             :     {
     343             :         4,                      // GOP Index 7 - Temporal Layer
     344             :         8,                      // GOP Index 7 - Decode Order
     345             : #if PRED_CHANGE_MOD
     346             :         { 1, 3, 6, 7},          // GOP Index 7 - Ref List 0
     347             : #elif PRED_CHANGE_5L
     348             :         { 1, 3, 6, 7},          // GOP Index 7 - Ref List 0
     349             : #else
     350             :         { 1, 3, 7, 11},         // GOP Index 7 - Ref List 0
     351             : #endif
     352             :         { -1, -9, 0, 0}         // GOP Index 7 - Ref List 1
     353             :     },
     354             :     {
     355             :         1,                      // GOP Index 8 - Temporal Layer
     356             :         1,                      // GOP Index 8 - Decode Order
     357             :         {  8, 16, 24, 0},       // GOP Index 8 - Ref List 0
     358             :         { -8, 0, 0, 0}          // GOP Index 8 - Ref List 1
     359             :     },
     360             :     {
     361             :         4,                      // GOP Index 9 - Temporal Layer
     362             :         11,                     // GOP Index 9 - Decode Order
     363             : #if PRED_CHANGE_MOD
     364             :         { 1, 9, 8, 17},         // GOP Index 9 - Ref List 0
     365             : #elif PRED_CHANGE_5L
     366             :         { 1, 8, 9, 17},         // GOP Index 9 - Ref List 0
     367             : #else
     368             :         { 1, 5, 9, 17},         // GOP Index 9 - Ref List 0
     369             : #endif
     370             :         { -1, -3, -7, 0}        // GOP Index 9 - Ref List 1
     371             :     },
     372             :     {
     373             :         3,                      // GOP Index 10 - Temporal Layer
     374             :         10,                     // GOP Index 10 - Decode Order
     375             : #if PRED_CHANGE_5L
     376             :         { 2, 4, 10, 18},        // GOP Index 10 - Ref List 0
     377             : #else
     378             :         { 2, 4, 6, 10},         // GOP Index 10 - Ref List 0
     379             : #endif
     380             :         { -2, -6, 0, 0}         // GOP Index 10 - Ref List 1
     381             :     },
     382             :     {
     383             :         4,                      // GOP Index 11 - Temporal Layer
     384             :         12,                     // GOP Index 11 - Decode Order
     385             : #if PRED_CHANGE_MOD
     386             :         { 1, 3, 2, 11},         // GOP Index 11 - Ref List 0
     387             : #elif PRED_CHANGE_5L
     388             :         { 1, 2, 3, 11},         // GOP Index 11 - Ref List 0
     389             : #else
     390             :         { 1, 3, 7, 11},         // GOP Index 11 - Ref List 0
     391             : #endif
     392             :         { -1, -5, 0, 0}         // GOP Index 11 - Ref List 1
     393             :     },
     394             :     {
     395             :         2,                      // GOP Index 12 - Temporal Layer
     396             :         9,                      // GOP Index 12 - Decode Order
     397             :         { 4, 8, 12, 0},         // GOP Index 12 - Ref List 0
     398             :         { -4, 0, 0, 0}          // GOP Index 12 - Ref List 1
     399             :     },
     400             :     {
     401             :         4,                      // GOP Index 13 - Temporal Layer
     402             :         14,                     // GOP Index 13 - Decode Order
     403             : #if PRED_CHANGE_MOD
     404             :         { 1, 5, 4, 13},         // GOP Index 13 - Ref List 0
     405             : #elif PRED_CHANGE_5L
     406             :         { 1, 4, 5, 13},         // GOP Index 13 - Ref List 0
     407             : #else
     408             :         { 1, 5, 9, 13},         // GOP Index 13 - Ref List 0
     409             : #endif
     410             :         { -1, -3, 0, 0}         // GOP Index 13 - Ref List 1
     411             :     },
     412             :     {
     413             :         3,                      // GOP Index 14 - Temporal Layer
     414             :         13,                     // GOP Index 14 - Decode Order
     415             :         { 2, 4, 6, 14},         // GOP Index 14 - Ref List 0
     416             :         { -2, 0, 0, 0}          // GOP Index 14 - Ref List 1
     417             : 
     418             :     },
     419             :     {
     420             :         4,                      // GOP Index 15 - Temporal Layer
     421             :         15,                     // GOP Index 15 - Decode Order
     422             : #if PRED_CHANGE_MOD
     423             :         { 1, 3, 6, 7},          // GOP Index 15 - Ref List 0
     424             : #elif PRED_CHANGE_5L
     425             :         { 1, 3, 6, 7},          // GOP Index 15 - Ref List 0
     426             : #else
     427             :         { 1, 3, 7, 11},         // GOP Index 15 - Ref List 0
     428             : #endif
     429             :         { -1, 0, 0, 0}          // GOP Index 15 - Ref List 1
     430             :     }
     431             : };
     432             : 
     433             : /**********************************************************************************************************************************************************************************************************************
     434             :  * Six-Level Level Hierarchical
     435             :  *
     436             :  *
     437             :  *              b     b           b     b               b     b           b     b                   b     b           b     b               b     b           b     b               Temporal Layer 5
     438             :  *             / \   / \         / \   / \             / \   / \         / \   / \                 / \   / \         / \   / \             / \   / \         / \   / \
     439             :  *            /   \ /   \       /   \ /   \           /   \ /   \       /   \ /   \               /   \ /   \       /   \ /   \           /   \ /   \       /   \ /   \
     440             :  *           /     B     \     /     B     \         /     B     \     /     B     \             /     B     \     /     B     \         /     B     \     /     B     \            Temporal Layer 4
     441             :  *          /     / \     \   /     / \     \       /     / \     \   /     / \     \           /     / \     \   /     / \     \       /     / \     \   /     / \     \
     442             :  *         /     /   \     \ /     /   \     \     /     /   \     \ /     /   \     \         /     /   \     \ /     /   \     \     /     /   \     \ /     /   \     \
     443             :  *        /     /     ------B------     \     \   /     /     ------B------     \     \       /     /     ------B------     \     \   /     /     ------B------     \     \         Temporal Layer 3
     444             :  *       /     /           / \           \     \ /     /           / \           \     \     /     /           / \           \     \ /     /           / \           \     \
     445             :  *      /     /           /   \-----------------B------------------   \           \     \   /     /           /   \-----------------B------------------   \           \     \       Temporal Layer 2
     446             :  *     /     /           /                     / \                     \           \     \ /     /           /                     / \                     \           \     \
     447             :  *    /     /           /                     /   \---------------------------------------B---------------------------------------/   \                     \           \     \     Temporal Layer 1
     448             :  *   /     /           /                     /                                           / \                                           \                     \           \     \
     449             :  *  I---------------------------------------------------------------------------------------------------------------------------------------------------------------------------B   Temporal Layer 0
     450             :  *
     451             :  * Display Order:
     452             :  *  0           1  2  3     4     5  6  7       8       9  1  1     1     1  1  1         1         1  1  1     2     2  2  2       2       2  2  2     2     2  3  3           3
     453             :  *                                                         0  1     2     3  4  5         6         7  8  9     0     1  2  3       4       5  6  7     8     9  0  1           2
     454             :  *
     455             :  * Coding Order:
     456             :  *  0           6  5  7     4     9  8  1       3       1  1  1     1     1  1  1         2         2  2  2     1     2  2  2       1       2  2  2     2     3  3  3           1
     457             :  *                                      0               3  2  4     1     6  5  7                   1  0  2     9     4  3  5       8       8  7  9     6     1  0  2
     458             :  *
     459             :  **********************************************************************************************************************************************************************************************************************/
     460             : static PredictionStructureConfigEntry six_level_hierarchical_pred_struct[] = {
     461             :     {
     462             :         0,                  // GOP Index 0 - Temporal Layer
     463             :         0,                  // GOP Index 0 - Decode Order
     464             :         {32,  0, 0, 0},     // GOP Index 0 - Ref List 0
     465             :         {32,  0, 0, 0}      // GOP Index 0 - Ref List 1
     466             :     },
     467             :     {
     468             :         5,                  // GOP Index 1 - Temporal Layer
     469             :         5,                  // GOP Index 1 - Decode Order
     470             :         { 1,  0,  0,   0},  // GOP Index 1 - Ref List 0
     471             :         {-1, -3, -7, -15}   // GOP Index 1 - Ref List 1
     472             :     },
     473             :     {
     474             :         4,                  // GOP Index 2 - Temporal Layer
     475             :         4,                  // GOP Index 2 - Decode Order
     476             :         {2,   0,   0,   0}, // GOP Index 2 - Ref List 0
     477             :         {-2, -6, -14, -30}  // GOP Index 2 - Ref List 1
     478             :     },
     479             :     {
     480             :         5,                  // GOP Index 3 - Temporal Layer
     481             :         6,                  // GOP Index 3 - Decode Order
     482             :         { 1,  3,   0,   0}, // GOP Index 3 - Ref List 0
     483             :         {-1, -5, -13,   0}  // GOP Index 3 - Ref List 1
     484             :     },
     485             :     {
     486             :         3,                  // GOP Index 4 - Temporal Layer
     487             :         3,                  // GOP Index 4 - Decode Order
     488             :         { 4,   0,   0,  0}, // GOP Index 4 - Ref List 0
     489             :         {-4, -12, -28,  4}  // GOP Index 4 - Ref List 1
     490             :     },
     491             :     {
     492             :         5,                  // GOP Index 5 - Temporal Layer
     493             :         8,                  // GOP Index 5 - Decode Order
     494             :         { 1,  5,   0, 0},   // GOP Index 5 - Ref List 0
     495             :         {-1, -3, -11, 0}    // GOP Index 5 - Ref List 1
     496             :     },
     497             :     {
     498             :         4,                  // GOP Index 6 - Temporal Layer
     499             :         7,                  // GOP Index 6 - Decode Order
     500             :         { 2,   6,   0, 0},  // GOP Index 6 - Ref List 0
     501             :         {-2, -10, -26, 2}   // GOP Index 6 - Ref List 1
     502             :     },
     503             :     {
     504             :         5,                  // GOP Index 7 - Temporal Layer
     505             :         9,                  // GOP Index 7 - Decode Order
     506             :         { 1,  3,   0, 0},   // GOP Index 7 - Ref List 0
     507             :         {-1, -9, -25, 1}    // GOP Index 7 - Ref List 1
     508             :     },
     509             :     {
     510             :         2,                  // GOP Index 8 - Temporal Layer
     511             :         2,                  // GOP Index 8 - Decode Order
     512             :         { 8,   0, 0, 0},    // GOP Index 8 - Ref List 0
     513             :         {-8, -24, 8, 0}     // GOP Index 8 - Ref List 1
     514             :     },
     515             :     {
     516             :         5,                  // GOP Index 9 - Temporal Layer
     517             :         12,                 // GOP Index 9 - Decode Order
     518             :         { 1,  9,  0, 0},    // GOP Index 9 - Ref List 0
     519             :         {-1, -3, -7, 0}     // GOP Index 9 - Ref List 1
     520             :     },
     521             :     {
     522             :         4,                  // GOP Index 10 - Temporal Layer
     523             :         11,                 // GOP Index 10 - Decode Order
     524             :         {2,  10,   0,  0},  // GOP Index 10 - Ref List 0
     525             :         {-2, -6, -22,  2}   // GOP Index 10 - Ref List 1
     526             :     },
     527             :     {
     528             :         5,                  // GOP Index 11 - Temporal Layer
     529             :         13,                 // GOP Index 11 - Decode Order
     530             :         { 1,  3,   0, 0},   // GOP Index 11 - Ref List 0
     531             :         {-1, -5, -21, 1}    // GOP Index 11 - Ref List 1
     532             :     },
     533             :     {
     534             :         3,                  // GOP Index 12 - Temporal Layer
     535             :         10,                 // GOP Index 12 - Decode Order
     536             :         { 4,  12,  0,  0},  // GOP Index 12 - Ref List 0
     537             :         {-4, -20,  4, 12}   // GOP Index 12 - Ref List 1
     538             :     },
     539             :     {
     540             :         5,                  // GOP Index 13 - Temporal Layer
     541             :         15,                 // GOP Index 13 - Decode Order
     542             :         {  1,  5,   0, 0},  // GOP Index 13 - Ref List 0
     543             :         { -1, -3, -19, 1}   // GOP Index 13 - Ref List 1
     544             :     },
     545             :     {
     546             :         4,                  // GOP Index 14 - Temporal Layer
     547             :         14,                 // GOP Index 14 - Decode Order
     548             :         { 2,   6, 14,  0},  // GOP Index 14 - Ref List 0
     549             :         {-2, -18,  2,  6}   // GOP Index 14 - Ref List 1
     550             :     },
     551             :     {
     552             :         5,                  // GOP Index 15 - Temporal Layer
     553             :         16,                 // GOP Index 15 - Decode Order
     554             :         { 1,   3, 7,  0},   // GOP Index 15 - Ref List 0
     555             :         {-1, -17, 1,  3}    // GOP Index 15 - Ref List 1
     556             :     },
     557             :     {
     558             :         1,                  // GOP Index 16 - Temporal Layer
     559             :         1,                  // GOP Index 16 - Decode Order
     560             :         { 16,  0, 0, 0},    // GOP Index 16 - Ref List 0
     561             :         {-16, 16, 0, 0}     // GOP Index 16 - Ref List 1
     562             :     },
     563             :     {
     564             :         5,                  // GOP Index 17 - Temporal Layer
     565             :         20,                 // GOP Index 17 - Decode Order
     566             :         { 1, 17,  0,  0},   // GOP Index 17 - Ref List 0
     567             :         {-1, -3, -7,  0}    // GOP Index 17 - Ref List 1
     568             :     },
     569             :     {
     570             :         4,                  // GOP Index 18 - Temporal Layer
     571             :         19,                 // GOP Index 18 - Decode Order
     572             :         { 2, 18,   0,  0},  // GOP Index 18 - Ref List 0
     573             :         {-2, -6, -14,  2}   // GOP Index 18 - Ref List 1
     574             :     },
     575             :     {
     576             :         5,                  // GOP Index 19 - Temporal Layer
     577             :         21,                 // GOP Index 19 - Decode Order
     578             :         { 1,  3,   0, 0},   // GOP Index 19 - Ref List 0
     579             :         {-1, -5, -13, 1}    // GOP Index 19 - Ref List 1
     580             :     },
     581             :     {
     582             :         3,                  // GOP Index 20 - Temporal Layer
     583             :         18,                 // GOP Index 20 - Decode Order
     584             :         { 4,  20, 0,  0},   // GOP Index 20 - Ref List 0
     585             :         {-4, -12, 4, 20}    // GOP Index 20 - Ref List 1
     586             :     },
     587             :     {
     588             :         5,                  // GOP Index 21 - Temporal Layer
     589             :         23,                 // GOP Index 21 - Decode Order
     590             :         { 1,  5,   0, 0},   // GOP Index 21 - Ref List 0
     591             :         {-1, -3, -11, 1}    // GOP Index 21 - Ref List 1
     592             :     },
     593             :     {
     594             :         4,                  // GOP Index 22 - Temporal Layer
     595             :         22,                 // GOP Index 22 - Decode Order
     596             :         { 2,   6, 22, 0},   // GOP Index 22 - Ref List 0
     597             :         {-2, -10,  2, 6}    // GOP Index 22 - Ref List 1
     598             :     },
     599             :     {
     600             :         5,                  // GOP Index 23 - Temporal Layer
     601             :         24,                 // GOP Index 23 - Decode Order
     602             :         { 1,  3, 7,  0},    // GOP Index 23 - Ref List 0
     603             :         {-1, -9, 1,  3}     // GOP Index 23 - Ref List 1
     604             :     },
     605             :     {
     606             :         2,                  // GOP Index 24 - Temporal Layer
     607             :         17,                 // GOP Index 24 - Decode Order
     608             :         { 8, 24,  0, 0},    // GOP Index 24 - Ref List 0
     609             :         {-8,  8, 24, 0}     // GOP Index 24 - Ref List 1
     610             :     },
     611             :     {
     612             :         5,                  // GOP Index 25 - Temporal Layer
     613             :         27,                 // GOP Index 25 - Decode Order
     614             :         { 1,  9,  0, 0},    // GOP Index 25 - Ref List 0
     615             :         {-1, -3, -7, 1}     // GOP Index 25 - Ref List 1
     616             :     },
     617             :     {
     618             :         4,                  // GOP Index 26 - Temporal Layer
     619             :         26,                 // GOP Index 26 - Decode Order
     620             :         { 2, 10, 26,  0},   // GOP Index 26 - Ref List 0
     621             :         {-2, -6,  2, 10}    // GOP Index 26 - Ref List 1
     622             :     },
     623             :     {
     624             :         5,                  // GOP Index 27 - Temporal Layer
     625             :         28,                 // GOP Index 27 - Decode Order
     626             :         { 1,  3, 11,  0},   // GOP Index 27 - Ref List 0
     627             :         {-1, -5,  1,  3}    // GOP Index 27 - Ref List 1
     628             :     },
     629             :     {
     630             :         3,                  // GOP Index 28 - Temporal Layer
     631             :         25,                 // GOP Index 28 - Decode Order
     632             :         { 4, 12, 28,  0},   // GOP Index 28 - Ref List 0
     633             :         {-4,  4, 12, 28}    // GOP Index 28 - Ref List 1
     634             :     },
     635             :     {
     636             :         5,                  // GOP Index 29 - Temporal Layer
     637             :         30,                 // GOP Index 29 - Decode Order
     638             :         { 1,  5, 13,  0},   // GOP Index 29 - Ref List 0
     639             :         {-1, -3,  1,  5}    // GOP Index 29 - Ref List 1
     640             :     },
     641             :     {
     642             :         4,                  // GOP Index 30 - Temporal Layer
     643             :         29,                 // GOP Index 30 - Decode Order
     644             :         { 2, 6, 14,  0},    // GOP Index 30 - Ref List 0
     645             :         {-2, 2,  6, 14}     // GOP Index 30 - Ref List 1
     646             :     },
     647             :     {
     648             :         5,                  // GOP Index 31 - Temporal Layer
     649             :         31,                 // GOP Index 31 - Decode Order
     650             :         { 1, 3, 5, 15},     // GOP Index 31 - Ref List 0
     651             :         {-1, 1, 3,  5}      // GOP Index 31 - Ref List 1
     652             :     }
     653             : };
     654             : 
     655             : /************************************************
     656             :  * Prediction Structure Config Array
     657             :  ************************************************/
     658             : static const PredictionStructureConfig PredictionStructureConfigArray[] = {
     659             :     {1,     flat_pred_struct},
     660             :     {2,     two_level_hierarchical_pred_struct},
     661             :     {4,     three_level_hierarchical_pred_struct},
     662             :     {8,     four_level_hierarchical_pred_struct},
     663             :     {16,    five_level_hierarchical_pred_struct},
     664             :     {32,    six_level_hierarchical_pred_struct},
     665             :     {0,     (PredictionStructureConfigEntry*)EB_NULL} // Terminating Code, must always come last!
     666             : };
     667             : 
     668             : /************************************************
     669             :  * Get Prediction Structure
     670             :  ************************************************/
     671         132 : PredictionStructure* get_prediction_structure(
     672             :     PredictionStructureGroup     *predictionStructureGroupPtr,
     673             :     EbPred                         predStructure,
     674             :     uint32_t                          numberOfReferences,
     675             :     uint32_t                          levelsOfHierarchy)
     676             : {
     677             :     PredictionStructure *predStructPtr;
     678             :     uint32_t predStructIndex;
     679             : 
     680             :     // Convert numberOfReferences to an index
     681         132 :     --numberOfReferences;
     682             : 
     683             :     // Determine the Index value
     684         132 :     predStructIndex = PRED_STRUCT_INDEX(levelsOfHierarchy, (uint32_t)predStructure, numberOfReferences);
     685             : 
     686         132 :     predStructPtr = predictionStructureGroupPtr->prediction_structure_ptr_array[predStructIndex];
     687             : 
     688         132 :     return predStructPtr;
     689             : }
     690             : 
     691         144 : static void PredictionStructureDctor(EbPtr p)
     692             : {
     693         144 :     PredictionStructure *obj = (PredictionStructure*)p;
     694         144 :     PredictionStructureEntry** pe = obj->pred_struct_entry_ptr_array;
     695         144 :     uint32_t count = obj->pred_struct_entry_count;
     696         144 :     if (pe) {
     697        3156 :         for (uint32_t i = 0; i < count; i++) {
     698        3012 :             EB_FREE_ARRAY(pe[i]->ref_list0.reference_list);
     699        3012 :             EB_FREE_ARRAY(pe[i]->ref_list1.reference_list);
     700        3012 :             EB_FREE_ARRAY(pe[i]->dep_list0.list);
     701        3012 :             EB_FREE_ARRAY(pe[i]->dep_list1.list);
     702             :         }
     703         144 :         EB_FREE_2D(obj->pred_struct_entry_ptr_array);
     704             :     }
     705         144 :     EB_FREE_ARRAY(obj->decodeOrderTable);
     706         144 :     EB_FREE_ARRAY(obj->displayOrderTable);
     707         144 :     EB_FREE_ARRAY(obj->timelineMap);
     708         144 : }
     709             : 
     710             : 
     711             : /********************************************************************************************
     712             :  * Prediction Structure Ctor
     713             :  *
     714             :  * GOP Type:
     715             :  *   For Low Delay P, eliminate Ref List 0
     716             :  *   Fow Low Delay B, copy Ref List 0 into Ref List 1
     717             :  *   For Random Access, leave config as is
     718             :  *
     719             :  * numberOfReferences:
     720             :  *   Clip the Ref Lists
     721             :  *
     722             :  *  Summary:
     723             :  *
     724             :  *  The Pred Struct Ctor constructs the Reference Lists, Dependent Lists, and RPS for each
     725             :  *    valid prediction structure position. The full prediction structure is composed of four
     726             :  *    sections:
     727             :  *    a. Leading Pictures
     728             :  *    b. Initialization Pictures
     729             :  *    c. Steady-state Pictures
     730             :  *    d. Trailing Pictures
     731             :  *
     732             :  *  By definition, the Prediction Structure Config describes the Steady-state Picture
     733             :  *    Set. From the PS Config, the other sections are determined by following a simple
     734             :  *    set of construction rules. These rules are:
     735             :  *    -Leading Pictures use only List 1 of the Steady-state for forward-prediction
     736             :  *    -Init Pictures don't violate CRA mechanics
     737             :  *    -Steady-state Pictures come directly from the PS Config
     738             :  *    -Following pictures use only List 0 of the Steady-state for rear-prediction
     739             :  *
     740             :  *  In general terms, Leading and Trailing pictures are useful when trying to reduce
     741             :  *    the number of base-layer pictures in the presense of scene changes.  Trailing
     742             :  *    pictures are also useful for terminating sequences.  Init pictures are needed
     743             :  *    when using multiple references that expand outside of a Prediction Structure.
     744             :  *    Steady-state pictures are the normal use cases.
     745             :  *
     746             :  *  Leading and Trailing Pictures are not applicable to Low Delay prediction structures.
     747             :  *
     748             :  *  Below are a set of example PS diagrams
     749             :  *
     750             :  *  Low-delay P, Flat, 2 reference:
     751             :  *
     752             :  *                    I---P---P
     753             :  *
     754             :  *  Display Order     0   1   2
     755             :  *
     756             :  *  Sections:
     757             :  *    Let PredStructSize = N
     758             :  *    Leading Pictures:     [null]  Size: 0
     759             :  *    Init Pictures:        [0-1]   Size: Ceil(MaxReference, N) - N + 1
     760             :  *    Stead-state Pictures: [2]     Size: N
     761             :  *    Trailing Pictures:    [null]  Size: 0
     762             :  *    ------------------------------------------
     763             :  *      Total Size: Ceil(MaxReference, N) + 1
     764             :  *
     765             :  *  Low-delay B, 3-level, 2 references:
     766             :  *
     767             :  *                         b   b     b   b
     768             :  *                        /   /     /   /
     769             :  *                       /   B     /   B
     770             :  *                      /   /     /   /
     771             :  *                     I---------B-----------B
     772             :  *
     773             :  *  Display Order      0   1 2 3 4   5 6 7   8
     774             :  *
     775             :  *  Sections:
     776             :  *    Let PredStructSize = N
     777             :  *    Leading Pictures:     [null]  Size: 0
     778             :  *    Init Pictures:        [1-4]   Size: Ceil(MaxReference, N) - N + 1
     779             :  *    Stead-state Pictures: [5-8]   Size: N
     780             :  *    Trailing Pictures:    [null]  Size: 0
     781             :  *    ------------------------------------------
     782             :  *      Total Size: Ceil(MaxReference, N) + 1
     783             :  *
     784             :  *  Random Access, 3-level structure with 3 references:
     785             :  *
     786             :  *                   p   p       b   b       b   b       b   b       p   p
     787             :  *                    \   \     / \ / \     / \ / \     / \ / \     /   /
     788             :  *                     P   \   /   B   \   /   B   \   /   B   \   /   P
     789             :  *                      \   \ /   / \   \ /   / \   \ /   / \   \ /   /
     790             :  *                       ----I-----------B-----------B-----------B----
     791             :  *  Display Order:   0 1 2   3   4 5 6   7   8 9 1   1   1 1 1   1   1 1 1
     792             :  *                                               0   1   2 3 4   5   6 7 8
     793             :  *
     794             :  *  Decode Order:    2 1 3   0   6 5 7   4   1 9 1   8   1 1 1   1   1 1 1
     795             :  *                                           0   1       4 3 5   2   6 7 8
     796             :  *
     797             :  *  Sections:
     798             :  *    Let PredStructSize = N
     799             :  *    Leading Pictures:      [0-2]   Size: N - 1
     800             :  *    Init Pictures:         [3-11]  Size: Ceil(MaxReference, N) - N + 1
     801             :  *    Steady-state Pictures: [12-15] Size: N
     802             :  *    Trailing Pictures:     [16-18] Size: N - 1
     803             :  *    ------------------------------------------
     804             :  *      Total Size: 2*N + Ceil(MaxReference, N) - 1
     805             :  *
     806             :  *  Encoding Order:
     807             :  *                   -------->----------->----------->-----------|------->
     808             :  *                                                   |           |
     809             :  *                                                   |----<------|
     810             :  *
     811             :  *
     812             :  *  Timeline:
     813             :  *
     814             :  *  The timeline is a tool that is used to determine for how long a
     815             :  *    picture should be preserved for future reference. The concept of
     816             :  *    future reference is equivalently defined as dependence.  The RPS
     817             :  *    mechanism works by signaling for each picture in the DPB whether
     818             :  *    it is used for direct reference, kept for future reference, or
     819             :  *    discarded.  The timeline merely provides a means of determing
     820             :  *    the reference picture states at each prediction structure position.
     821             :  *    Its also important to note that all signaling should be done relative
     822             :  *    to decode order, not display order. Display order is irrelevant except
     823             :  *    for signaling the POC.
     824             :  *
     825             :  *  Timeline Example: 3-Level Hierarchical with Leading and Trailing Pictures, 3 References
     826             :  *
     827             :  *                   p   p       b   b       b   b       b   b       p   p   Temporal Layer 2
     828             :  *                    \   \     / \ / \     / \ / \     / \ / \     /   /
     829             :  *                     P   \   /   B   \   /   B   \   /   B   \   /   P     Temporal Layer 1
     830             :  *                      \   \ /   / \   \ /   / \   \ /   / \   \ /   /
     831             :  *                       ----I-----------B-----------B-----------B----       Temporal Layer 0
     832             :  *
     833             :  *  Decode Order:    2 1 3   0   6 5 7   4   1 9 1   8   1 1 1   1   1 1 1
     834             :  *                                           0   1       4 3 5   2   6 7 8
     835             :  *
     836             :  *  Display Order:   0 1 2   3   4 5 6   7   8 9 1   1   1 1 1   1   1 1 1
     837             :  *                                               0   1   2 3 4   5   6 7 8
     838             :  *            X --->
     839             :  *
     840             :  *                                1 1 1 1 1 1 1 1 1   DECODE ORDER
     841             :  *            0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8
     842             :  *
     843             :  *         |----------------------------------------------------------
     844             :  *         |
     845             :  *  Y D  0 |  \ x---x-x-x-x---x-x-x---x-x-x
     846             :  *  | E  1 |    \ x
     847             :  *  | C  2 |      \
     848             :  *  | O  3 |        \
     849             :  *  v D  4 |          \ x---x-x-x-x---x-x-x---x-x-x
     850             :  *    E  5 |            \ x-x
     851             :  *       6 |              \
     852             :  *    O  7 |                \
     853             :  *    R  8 |                  \ x---x-x-x-x---x-x-x
     854             :  *    D  9 |                    \ x-x
     855             :  *    E 10 |                      \
     856             :  *    R 11 |                        \
     857             :  *      12 |                          \ x---x-x-x-x
     858             :  *      13 |                            \ x-x
     859             :  *      14 |                              \
     860             :  *      15 |                                \
     861             :  *      16 |                                  \
     862             :  *      17 |                                    \ x
     863             :  *      18 |                                      \
     864             :  *
     865             :  *  Interpreting the timeline:
     866             :  *
     867             :  *  The most important detail to keep in mind is that all signaling
     868             :  *    is done in Decode Order space. The symbols mean the following:
     869             :  *    'x' directly referenced picture
     870             :  *    '-' picture kept for future reference
     871             :  *    ' ' not referenced, inferred discard
     872             :  *    '\' eqivalent to ' ', deliminiter that nothing can be to the left of
     873             :  *
     874             :  *  The basic steps for constructing the timeline are to increment through
     875             :  *    each position in the prediction structure (Y-direction on the timeline)
     876             :  *    and mark the appropriate state: directly referenced, kept for future reference,
     877             :  *    or discarded.  As shown, base-layer pictures are referenced much more
     878             :  *    frequently than by the other layers.
     879             :  *
     880             :  *  The RPS is constructed by looking at each 'x' position in the timeline and
     881             :  *    signaling each 'y' reference as depicted in the timeline. DPB analysis is
     882             :  *    fairly straigtforward - the total number of directly-referenced and
     883             :  *    kept-for-future-reference pictures should not exceed the DPB size.
     884             :  *
     885             :  *  The RPS Ctor code follows these construction steps.
     886             :  ******************************************************************************************/
     887         144 : static EbErrorType PredictionStructureCtor(
     888             :     PredictionStructure              *predictionStructurePtr,
     889             :     const PredictionStructureConfig  *predictionStructureConfigPtr,
     890             :     EbPred                       predType,
     891             :     uint32_t                        numberOfReferences)
     892             : {
     893             :     uint32_t                  entryIndex;
     894             :     uint32_t                  configEntryIndex;
     895             :     uint32_t                  refIndex;
     896             : 
     897             :     // Section Variables
     898             :     uint32_t                  leadingPicCount;
     899             :     uint32_t                  initPicCount;
     900             :     uint32_t                  steadyStatePicCount;
     901             : 
     902         144 :     predictionStructurePtr->dctor = PredictionStructureDctor;
     903             : 
     904         144 :     predictionStructurePtr->pred_type = predType;
     905             : 
     906             :     // Set the Pred Struct Period
     907         144 :     predictionStructurePtr->pred_struct_period = predictionStructureConfigPtr->entry_count;
     908             : 
     909             :     //----------------------------------------
     910             :     // Find the Pred Struct Entry Count
     911             :     //   There are four sections of the pred struct:
     912             :     //     -Leading Pictures        Size: N-1
     913             :     //     -Init Pictures           Size: Ceil(MaxReference, N) - N + 1
     914             :     //     -Steady-state Pictures   Size: N
     915             :     //     -Trailing Pictures       Size: N-1
     916             :     //----------------------------------------
     917             : 
     918             :     //----------------------------------------
     919             :     // Determine the Prediction Structure Size
     920             :     //   First, start by determining
     921             :     //   Ceil(MaxReference, N)
     922             :     //----------------------------------------
     923             :     {
     924         144 :         int32_t maxRef = MIN_SIGNED_VALUE;
     925        1656 :         for (configEntryIndex = 0, entryIndex = predictionStructureConfigPtr->entry_count - 1; configEntryIndex < predictionStructureConfigPtr->entry_count; ++configEntryIndex) {
     926             :             // Increment through Reference List 0
     927        1512 :             refIndex = 0;
     928        4269 :             while (refIndex < numberOfReferences && predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex] != 0) {
     929             :                 //maxRef = MAX(predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex], maxRef);
     930        2757 :                 maxRef = MAX((int32_t)(predictionStructureConfigPtr->entry_count - entryIndex - 1) + predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex], maxRef);
     931        2757 :                 ++refIndex;
     932             :             }
     933             : 
     934             :             // Increment through Reference List 1 (Random Access only)
     935        1512 :             if (predType == EB_PRED_RANDOM_ACCESS) {
     936         504 :                 refIndex = 0;
     937        1509 :                 while (refIndex < numberOfReferences && predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex] != 0) {
     938             :                     //maxRef = MAX(predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex], maxRef);
     939        1005 :                     maxRef = MAX((int32_t)(predictionStructureConfigPtr->entry_count - entryIndex - 1) + predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex], maxRef);
     940        1005 :                     ++refIndex;
     941             :                 }
     942             :             }
     943             : 
     944             :             // Increment entryIndex
     945        1512 :             entryIndex = (entryIndex == predictionStructureConfigPtr->entry_count - 1) ? 0 : entryIndex + 1;
     946             :         }
     947             : 
     948             :         // Perform the Ceil(MaxReference, N) operation
     949         144 :         predictionStructurePtr->maximum_extent = CEILING(maxRef, predictionStructurePtr->pred_struct_period);
     950             : 
     951             :         // Set the Section Sizes
     952         144 :         leadingPicCount = (predType == EB_PRED_RANDOM_ACCESS) ?     // No leading pictures in low-delay configurations
     953         144 :             predictionStructurePtr->pred_struct_period - 1 :
     954             :             0;
     955         144 :         initPicCount = predictionStructurePtr->maximum_extent - predictionStructurePtr->pred_struct_period + 1;
     956         144 :         steadyStatePicCount = predictionStructurePtr->pred_struct_period;
     957             :         //trailingPicCount        = (predType == EB_PRED_RANDOM_ACCESS) ?     // No trailing pictures in low-delay configurations
     958             :         //    predictionStructurePtr->pred_struct_period - 1:
     959             :         //    0;
     960             : 
     961             :         // Set the total Entry Count
     962         144 :         predictionStructurePtr->pred_struct_entry_count =
     963         144 :             leadingPicCount +
     964         144 :             initPicCount +
     965             :             steadyStatePicCount;
     966             : 
     967             :         // Set the Section Indices
     968         144 :         predictionStructurePtr->leading_pic_index = 0;
     969         144 :         predictionStructurePtr->init_pic_index = predictionStructurePtr->leading_pic_index + leadingPicCount;
     970         144 :         predictionStructurePtr->steady_state_index = predictionStructurePtr->init_pic_index + initPicCount;
     971             :     }
     972             : 
     973             :     // Allocate the entry array
     974        3012 :     EB_CALLOC_2D(predictionStructurePtr->pred_struct_entry_ptr_array, predictionStructurePtr->pred_struct_entry_count, 1);
     975             : 
     976             :     // Find the Max Temporal Layer Index
     977         144 :     predictionStructurePtr->temporal_layer_count = 0;
     978        1656 :     for (configEntryIndex = 0; configEntryIndex < predictionStructureConfigPtr->entry_count; ++configEntryIndex)
     979        1512 :         predictionStructurePtr->temporal_layer_count = MAX(predictionStructureConfigPtr->entry_array[configEntryIndex].temporal_layer_index, predictionStructurePtr->temporal_layer_count);
     980             :     // Increment the Zero-indexed temporal layer index to get the total count
     981         144 :     ++predictionStructurePtr->temporal_layer_count;
     982             : 
     983             :     //----------------------------------------
     984             :     // Construct Leading Pictures
     985             :     //   -Use only Ref List1 from the Config
     986             :     //   -Note the Config starts from the 2nd position to construct the leading pictures
     987             :     //----------------------------------------
     988             :     {
     989         600 :         for (entryIndex = 0, configEntryIndex = 1; entryIndex < leadingPicCount; ++entryIndex, ++configEntryIndex) {
     990             :             // Find the Size of the Config's Reference List 1
     991         456 :             refIndex = 0;
     992        1371 :             while (refIndex < numberOfReferences && predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex] != 0)
     993         915 :                 ++refIndex;
     994             :             // Set Leading Picture's Reference List 0 Count {Config List1 => LeadingPic List 0}
     995         456 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count = refIndex;
     996             : 
     997             :             // Allocate the Leading Picture Reference List 0
     998         456 :             if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count) {
     999         456 :                 EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count);
    1000             :             }
    1001             :             else
    1002           0 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list = (int32_t*)EB_NULL;
    1003             :             // Copy Config List1 => LeadingPic Reference List 0
    1004        1371 :             for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex)
    1005         915 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] = predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex];
    1006             :             // Null out List 1
    1007         456 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = 0;
    1008         456 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1009             : 
    1010             :             // Set the Temporal Layer Index
    1011         456 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->temporal_layer_index = predictionStructureConfigPtr->entry_array[configEntryIndex].temporal_layer_index;
    1012             : 
    1013             :             // Set the Decode Order
    1014         456 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->decode_order = (predType == EB_PRED_RANDOM_ACCESS) ?
    1015         456 :                 predictionStructureConfigPtr->entry_array[configEntryIndex].decode_order :
    1016             :                 entryIndex;
    1017             :         }
    1018             :     }
    1019             : 
    1020             :     //----------------------------------------
    1021             :     // Construct Init Pictures
    1022             :     //   -Use only references from Ref List0 & Ref List1 from the Config that don't violate CRA mechanics
    1023             :     //   -The Config Index cycles through continuously
    1024             :     //----------------------------------------
    1025             :     {
    1026         144 :         uint32_t terminatingEntryIndex = entryIndex + initPicCount;
    1027             :         int32_t pocValue;
    1028             : 
    1029        1188 :         for (configEntryIndex = 0, pocValue = 0; entryIndex < terminatingEntryIndex; ++entryIndex, ++pocValue) {
    1030             :             // REFERENCE LIST 0
    1031             : 
    1032             :             // Find the Size of the Config's Reference List 0
    1033        1044 :             refIndex = 0;
    1034        2676 :             while (
    1035        2406 :                 refIndex < numberOfReferences &&
    1036        2406 :                 predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex] != 0 &&
    1037        2106 :                 pocValue - predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex] >= 0)  // Stop when we violate the CRA (i.e. reference past it)
    1038             :             {
    1039        1632 :                 ++refIndex;
    1040             :             }
    1041             : 
    1042             :             // Set Leading Picture's Reference List 0 Count
    1043        1044 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count = refIndex;
    1044             : 
    1045             :             // Allocate the Leading Picture Reference List 0
    1046        1044 :             if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count) {
    1047         900 :                 EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count);
    1048             :             }
    1049             :             else
    1050         144 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list = (int32_t*)EB_NULL;
    1051             :             // Copy Reference List 0
    1052        2676 :             for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex)
    1053        1632 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] = predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex];
    1054             :             // REFERENCE LIST 1
    1055        1044 :             switch (predType) {
    1056         348 :             case EB_PRED_LOW_DELAY_P:
    1057             : 
    1058             :                 // Null out List 1
    1059         348 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = 0;
    1060         348 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1061             : 
    1062         348 :                 break;
    1063             : 
    1064         348 :             case EB_PRED_LOW_DELAY_B:
    1065             : 
    1066             :                 // Copy List 0 => List 1
    1067             : 
    1068             :                 // Set Leading Picture's Reference List 1 Count
    1069         348 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count;
    1070             : 
    1071             :                 // Allocate the Leading Picture Reference List 1
    1072         348 :                 if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count) {
    1073         300 :                     EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count);
    1074             :                 }
    1075             :                 else
    1076          48 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1077             :                 // Copy Reference List 1
    1078         892 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex)
    1079         544 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] = predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1080         348 :                 break;
    1081             : 
    1082         348 :             case EB_PRED_RANDOM_ACCESS:
    1083             : 
    1084             :                 // Find the Size of the Config's Reference List 1
    1085         348 :                 refIndex = 0;
    1086         870 :                 while (
    1087         799 :                     refIndex < numberOfReferences &&
    1088         799 :                     predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex] != 0 &&
    1089         628 :                     pocValue - predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex] >= 0) // Stop when we violate the CRA (i.e. reference past it)
    1090             :                 {
    1091         522 :                     ++refIndex;
    1092             :                 }
    1093             : 
    1094             :                 // Set Leading Picture's Reference List 1 Count
    1095         348 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = refIndex;
    1096             : 
    1097             :                 // Allocate the Leading Picture Reference List 1
    1098         348 :                 if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count) {
    1099         300 :                     EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count);
    1100             :                 }
    1101             :                 else
    1102          48 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1103             :                 // Copy Reference List 1
    1104         870 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex)
    1105         522 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] = predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex];
    1106         348 :                 break;
    1107             : 
    1108           0 :             default:
    1109           0 :                 break;
    1110             :             }
    1111             : 
    1112             :             // Set the Temporal Layer Index
    1113        1044 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->temporal_layer_index = predictionStructureConfigPtr->entry_array[configEntryIndex].temporal_layer_index;
    1114             : 
    1115             :             // Set the Decode Order
    1116        1044 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->decode_order = (predType == EB_PRED_RANDOM_ACCESS) ?
    1117        1044 :                 predictionStructureConfigPtr->entry_array[configEntryIndex].decode_order :
    1118             :                 entryIndex;
    1119             : 
    1120             :             // Rollover the Config Index
    1121        1044 :             configEntryIndex = (configEntryIndex == predictionStructureConfigPtr->entry_count - 1) ?
    1122        1044 :                 0 :
    1123             :                 configEntryIndex + 1;
    1124             :         }
    1125             :     }
    1126             : 
    1127             :     //----------------------------------------
    1128             :     // Construct Steady-state Pictures
    1129             :     //   -Copy directly from the Config
    1130             :     //----------------------------------------
    1131             :     {
    1132         144 :         uint32_t terminatingEntryIndex = entryIndex + steadyStatePicCount;
    1133             : 
    1134        1656 :         for (/*configEntryIndex = 0*/; entryIndex < terminatingEntryIndex; ++entryIndex/*, ++configEntryIndex*/) {
    1135             :             // Find the Size of Reference List 0
    1136        1512 :             refIndex = 0;
    1137        4269 :             while (refIndex < numberOfReferences && predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex] != 0)
    1138        2757 :                 ++refIndex;
    1139             :             // Set Reference List 0 Count
    1140        1512 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count = refIndex;
    1141             : 
    1142             :             // Allocate Reference List 0
    1143        1512 :             EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count);
    1144             :             // Copy Reference List 0
    1145        4269 :             for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex)
    1146        2757 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] = predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex];
    1147             :             // REFERENCE LIST 1
    1148        1512 :             switch (predType) {
    1149         504 :             case EB_PRED_LOW_DELAY_P:
    1150             : 
    1151             :                 // Null out List 1
    1152         504 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = 0;
    1153         504 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1154             : 
    1155         504 :                 break;
    1156             : 
    1157         504 :             case EB_PRED_LOW_DELAY_B:
    1158             : 
    1159             :                 // Copy List 0 => List 1
    1160             : 
    1161             :                 // Set Leading Picture's Reference List 1 Count
    1162         504 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count;
    1163             : 
    1164             :                 // Allocate the Leading Picture Reference List 1
    1165         504 :                 if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count) {
    1166         504 :                     EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list,  predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count);
    1167             :                 }
    1168             :                 else
    1169           0 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1170             :                 // Copy Reference List 1
    1171        1423 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex)
    1172         919 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] = predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1173         504 :                 break;
    1174             : 
    1175         504 :             case EB_PRED_RANDOM_ACCESS:
    1176             : 
    1177             :                 // Find the Size of the Config's Reference List 1
    1178         504 :                 refIndex = 0;
    1179        1509 :                 while (refIndex < numberOfReferences && predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex] != 0)
    1180        1005 :                     ++refIndex;
    1181             :                 // Set Leading Picture's Reference List 1 Count
    1182         504 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = refIndex;
    1183             : 
    1184             :                 // Allocate the Leading Picture Reference List 1
    1185         504 :                 if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count) {
    1186         504 :                     EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count);
    1187             :                 }
    1188             :                 else
    1189           0 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*)EB_NULL;
    1190             :                 // Copy Reference List 1
    1191        1509 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex)
    1192        1005 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] = predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list1[refIndex];
    1193         504 :                 break;
    1194             : 
    1195           0 :             default:
    1196           0 :                 break;
    1197             :             }
    1198             : 
    1199             :             // Set the Temporal Layer Index
    1200        1512 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->temporal_layer_index = predictionStructureConfigPtr->entry_array[configEntryIndex].temporal_layer_index;
    1201             : 
    1202             :             // Set the Decode Order
    1203        1512 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->decode_order = (predType == EB_PRED_RANDOM_ACCESS) ?
    1204        1512 :                 predictionStructureConfigPtr->entry_array[configEntryIndex].decode_order :
    1205             :                 entryIndex;
    1206             : 
    1207             :             // Rollover the Config Index
    1208        1512 :             configEntryIndex = (configEntryIndex == predictionStructureConfigPtr->entry_count - 1) ?
    1209        1512 :                 0 :
    1210             :                 configEntryIndex + 1;
    1211             :         }
    1212             :     }
    1213             : 
    1214             :     //----------------------------------------
    1215             :     // Construct Trailing Pictures
    1216             :     //   -Use only Ref List0 from the Config
    1217             :     //----------------------------------------
    1218             :     //{
    1219             :     //    uint32_t terminatingEntryIndex = entryIndex + trailingPicCount;
    1220             :     //
    1221             :     //    for(configEntryIndex = 0; entryIndex < terminatingEntryIndex; ++entryIndex, ++configEntryIndex) {
    1222             :     //
    1223             :     //        // Set Reference List 0 Count
    1224             :     //        // *Note - only 1 reference is used for trailing pictures.  If you have frequent CRAs and the Pred Struct
    1225             :     //        //   has many references, you can run into edge conditions.
    1226             :     //        predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count = 1;
    1227             :     //
    1228             :     //        // Allocate Reference List 0
    1229             :     //        predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list = (int32_t*) malloc(sizeof(int32_t) * predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count);
    1230             :     //
    1231             :     //        // Copy Reference List 0
    1232             :     //        for(refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex) {
    1233             :     //            predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] = predictionStructureConfigPtr->entry_array[configEntryIndex].ref_list0[refIndex];
    1234             :     //        }
    1235             :     //
    1236             :     //        // Null out List 1
    1237             :     //        predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count = 0;
    1238             :     //        predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list = (int32_t*) EB_NULL;
    1239             :     //
    1240             :     //        // Set the Temporal Layer Index
    1241             :     //        predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->temporal_layer_index = predictionStructureConfigPtr->entry_array[configEntryIndex].temporal_layer_index;
    1242             :     //
    1243             :     //        // Set the Decode Order
    1244             :     //        predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->decode_order = (predType == EB_PRED_RANDOM_ACCESS) ?
    1245             :     //            predictionStructureConfigPtr->entry_array[configEntryIndex].decode_order :
    1246             :     //            entryIndex;
    1247             :     //    }
    1248             :     //}
    1249             : 
    1250             :     //----------------------------------------
    1251             :     // CONSTRUCT DEPENDENT LIST 0
    1252             :     //----------------------------------------
    1253             : 
    1254             :     {
    1255             :         int64_t  depIndex;
    1256             :         uint64_t  pictureNumber;
    1257             : 
    1258             :         // First, determine the Dependent List Size for each Entry by incrementing the dependent list length
    1259             :         {
    1260             :             // Go through a single pass of the Leading Pictures and Init pictures
    1261        1644 :             for (pictureNumber = 0, entryIndex = 0; pictureNumber < predictionStructurePtr->steady_state_index; ++pictureNumber) {
    1262             :                 // Go through each Reference picture and accumulate counts
    1263        4047 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex) {
    1264        2547 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1265             : 
    1266        2547 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period))
    1267        2489 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list0.list_count;
    1268             :                 }
    1269             : 
    1270             :                 // Increment the entryIndex
    1271        1500 :                 ++entryIndex;
    1272             :             }
    1273             : 
    1274             :             // Go through an entire maximum extent pass for the Steady-state pictures
    1275        5112 :             for (entryIndex = predictionStructurePtr->steady_state_index; pictureNumber <= predictionStructurePtr->steady_state_index + 2 * predictionStructurePtr->maximum_extent; ++pictureNumber) {
    1276             :                 // Go through each Reference picture and accumulate counts
    1277       15078 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex) {
    1278       10110 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1279             : 
    1280       10110 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period))
    1281        4350 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list0.list_count;
    1282             :                 }
    1283             : 
    1284             :                 // Rollover the entryIndex each time it reaches the end of the steady state index
    1285        4968 :                 entryIndex = (entryIndex == predictionStructurePtr->pred_struct_entry_count - 1) ?
    1286        4968 :                     predictionStructurePtr->steady_state_index :
    1287             :                     entryIndex + 1;
    1288             :             }
    1289             :         }
    1290             : 
    1291             :         // Second, allocate memory for each dependent list of each Entry
    1292        3156 :         for (entryIndex = 0; entryIndex < predictionStructurePtr->pred_struct_entry_count; ++entryIndex) {
    1293             :             // If the dependent list count is non-zero, allocate the list, else the list is NULL.
    1294        3012 :             if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list_count > 0) {
    1295        1636 :                 EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list_count);
    1296             :             }
    1297             :             else
    1298        1376 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list = (int32_t*)EB_NULL;
    1299             :         }
    1300             : 
    1301             :         // Third, reset the Dependent List Length (they are re-derived)
    1302        3156 :         for (entryIndex = 0; entryIndex < predictionStructurePtr->pred_struct_entry_count; ++entryIndex)
    1303        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list_count = 0;
    1304             :         // Fourth, run through each Reference List entry again and populate the Dependent Lists and Dep List Counts
    1305             :         {
    1306             :             // Go through a single pass of the Leading Pictures and Init pictures
    1307        1644 :             for (pictureNumber = 0, entryIndex = 0; pictureNumber < predictionStructurePtr->steady_state_index; ++pictureNumber) {
    1308             :                 // Go through each Reference picture and accumulate counts
    1309        4047 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex) {
    1310        2547 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1311             : 
    1312        2547 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period)) {
    1313        2489 :                         predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list0.list[predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list0.list_count++] =
    1314        2489 :                             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1315             :                     }
    1316             :                 }
    1317             : 
    1318             :                 // Increment the entryIndex
    1319        1500 :                 ++entryIndex;
    1320             :             }
    1321             : 
    1322             :             // Go through an entire maximum extent pass for the Steady-state pictures
    1323        5112 :             for (entryIndex = predictionStructurePtr->steady_state_index; pictureNumber <= predictionStructurePtr->steady_state_index + 2 * predictionStructurePtr->maximum_extent; ++pictureNumber) {
    1324             :                 // Go through each Reference picture and accumulate counts
    1325       15078 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count; ++refIndex) {
    1326       10110 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1327             : 
    1328             :                     // Assign the Reference to the Dep List and Increment the Dep List Count
    1329       10110 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period)) {
    1330        4350 :                         predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list0.list[predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list0.list_count++] =
    1331        4350 :                             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex];
    1332             :                     }
    1333             :                 }
    1334             : 
    1335             :                 // Rollover the entryIndex each time it reaches the end of the steady state index
    1336        4968 :                 entryIndex = (entryIndex == predictionStructurePtr->pred_struct_entry_count - 1) ?
    1337        4968 :                     predictionStructurePtr->steady_state_index :
    1338             :                     entryIndex + 1;
    1339             :             }
    1340             :         }
    1341             :     }
    1342             : 
    1343             :     //----------------------------------------
    1344             :     // CONSTRUCT DEPENDENT LIST 1
    1345             :     //----------------------------------------
    1346             : 
    1347             :     {
    1348             :         int32_t  depIndex;
    1349             :         uint32_t  pictureNumber;
    1350             : 
    1351             :         // First, determine the Dependent List Size for each Entry by incrementing the dependent list length
    1352             :         {
    1353             :             // Go through a single pass of the Leading Pictures and Init pictures
    1354        1644 :             for (pictureNumber = 0, entryIndex = 0; pictureNumber < predictionStructurePtr->steady_state_index; ++pictureNumber) {
    1355             :                 // Go through each Reference picture and accumulate counts
    1356        2566 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex) {
    1357        1066 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex];
    1358             : 
    1359        1066 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period))
    1360        1066 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list1.list_count;
    1361             :                 }
    1362             : 
    1363             :                 // Increment the entryIndex
    1364        1500 :                 ++entryIndex;
    1365             :             }
    1366             : 
    1367             :             // Go through an entire maximum extent pass for the Steady-state pictures
    1368        5112 :             for (entryIndex = predictionStructurePtr->steady_state_index; pictureNumber <= predictionStructurePtr->steady_state_index + 2 * predictionStructurePtr->maximum_extent; ++pictureNumber) {
    1369             :                 // Go through each Reference picture and accumulate counts
    1370       11670 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex) {
    1371        6702 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex];
    1372             : 
    1373        6702 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period))
    1374        2685 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list1.list_count;
    1375             :                 }
    1376             : 
    1377             :                 // Rollover the entryIndex each time it reaches the end of the steady state index
    1378        4968 :                 entryIndex = (entryIndex == predictionStructurePtr->pred_struct_entry_count - 1) ?
    1379        4968 :                     predictionStructurePtr->steady_state_index :
    1380             :                     entryIndex + 1;
    1381             :             }
    1382             :         }
    1383             : 
    1384             :         // Second, allocate memory for each dependent list of each Entry
    1385        3156 :         for (entryIndex = 0; entryIndex < predictionStructurePtr->pred_struct_entry_count; ++entryIndex) {
    1386             :             // If the dependent list count is non-zero, allocate the list, else the list is NULL.
    1387        3012 :             if (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list_count > 0) {
    1388         936 :                 EB_MALLOC_ARRAY(predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list, predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list_count);
    1389             :             }
    1390             :             else
    1391        2076 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list = (int32_t*)EB_NULL;
    1392             :         }
    1393             : 
    1394             :         // Third, reset the Dependent List Length (they are re-derived)
    1395        3156 :         for (entryIndex = 0; entryIndex < predictionStructurePtr->pred_struct_entry_count; ++entryIndex)
    1396        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list_count = 0;
    1397             :         // Fourth, run through each Reference List entry again and populate the Dependent Lists and Dep List Counts
    1398             :         {
    1399             :             // Go through a single pass of the Leading Pictures and Init pictures
    1400        1644 :             for (pictureNumber = 0, entryIndex = 0; pictureNumber < predictionStructurePtr->steady_state_index; ++pictureNumber) {
    1401             :                 // Go through each Reference picture and accumulate counts
    1402        2566 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex) {
    1403        1066 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex];
    1404             : 
    1405        1066 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period)) {
    1406        1066 :                         predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list1.list[predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list1.list_count++] =
    1407        1066 :                             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex];
    1408             :                     }
    1409             :                 }
    1410             : 
    1411             :                 // Increment the entryIndex
    1412        1500 :                 ++entryIndex;
    1413             :             }
    1414             : 
    1415             :             // Go through an entire maximum extent pass for the Steady-state pictures
    1416        5112 :             for (entryIndex = predictionStructurePtr->steady_state_index; pictureNumber <= predictionStructurePtr->steady_state_index + 2 * predictionStructurePtr->maximum_extent; ++pictureNumber) {
    1417             :                 // Go through each Reference picture and accumulate counts
    1418       11670 :                 for (refIndex = 0; refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count; ++refIndex) {
    1419        6702 :                     depIndex = pictureNumber - predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex];
    1420             : 
    1421             :                     // Assign the Reference to the Dep List and Increment the Dep List Count
    1422        6702 :                     if (depIndex >= 0 && depIndex < (int32_t)(predictionStructurePtr->steady_state_index + predictionStructurePtr->pred_struct_period)) {
    1423        2685 :                         predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list1.list[predictionStructurePtr->pred_struct_entry_ptr_array[depIndex]->dep_list1.list_count++] =
    1424        2685 :                             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex];
    1425             :                     }
    1426             :                 }
    1427             : 
    1428             :                 // Rollover the entryIndex each time it reaches the end of the steady state index
    1429        4968 :                 entryIndex = (entryIndex == predictionStructurePtr->pred_struct_entry_count - 1) ?
    1430        4968 :                     predictionStructurePtr->steady_state_index :
    1431             :                     entryIndex + 1;
    1432             :             }
    1433             :         }
    1434             :     }
    1435             : 
    1436             :     // Set is_referenced for each entry
    1437        3156 :     for (entryIndex = 0; entryIndex < predictionStructurePtr->pred_struct_entry_count; ++entryIndex) {
    1438        3012 :         predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->is_referenced =
    1439        3012 :             ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list_count > 0) ||
    1440        1376 :             (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list_count > 0)) ?
    1441        3012 :             EB_TRUE :
    1442             :             EB_FALSE;
    1443             :     }
    1444             : 
    1445             :     //----------------------------------------
    1446             :     // CONSTRUCT THE RPSes
    1447             :     //----------------------------------------
    1448             :     {
    1449             :         // Counts & Indicies
    1450             :         uint32_t      refIndex;
    1451             :         uint32_t      depIndex;
    1452             :         uint32_t      entryIndex;
    1453             :         uint32_t      currentPocIndex;
    1454             :         uint32_t      refPocIndex;
    1455             : 
    1456             :         uint32_t      decodeOrderTableSize;
    1457             :         int32_t     *decodeOrderTable;
    1458             :         uint32_t     *displayOrderTable;
    1459             :         uint32_t      gopNumber;
    1460             :         uint32_t      baseNumber;
    1461             : 
    1462             :         // Timeline Map Variables
    1463             :         EbBool    *timelineMap;
    1464             :         uint32_t      timelineSize;
    1465             : 
    1466             :         int32_t      depListMax;
    1467             :         int32_t      depListMin;
    1468             : 
    1469             :         int32_t      lifetimeStart;
    1470             :         int32_t      lifetimeSpan;
    1471             : 
    1472             :         int32_t      deltaPoc;
    1473             :         int32_t      prevDeltaPoc;
    1474             :         EbBool     pocInreference_list0;
    1475             :         EbBool     pocInreference_list1;
    1476             :         EbBool     pocInTimeline;
    1477             : 
    1478             :         int32_t      adjustedDepIndex;
    1479             : 
    1480             :         // Allocate & Initialize the Timeline map
    1481         144 :         timelineSize = predictionStructurePtr->pred_struct_entry_count;
    1482         144 :         decodeOrderTableSize = CEILING(predictionStructurePtr->pred_struct_entry_count + predictionStructurePtr->maximum_extent, predictionStructurePtr->pred_struct_entry_count);
    1483         144 :         EB_CALLOC_ARRAY(predictionStructurePtr->timelineMap, SQR(timelineSize));
    1484         144 :         timelineMap = predictionStructurePtr->timelineMap;
    1485             : 
    1486             :         // Construct the Decode & Display Order
    1487         144 :         EB_MALLOC_ARRAY(predictionStructurePtr->decodeOrderTable, decodeOrderTableSize);
    1488         144 :         decodeOrderTable = predictionStructurePtr->decodeOrderTable;
    1489             : 
    1490         144 :         EB_MALLOC_ARRAY(predictionStructurePtr->displayOrderTable, decodeOrderTableSize);
    1491         144 :         displayOrderTable = predictionStructurePtr->displayOrderTable;
    1492             : 
    1493        6168 :         for (currentPocIndex = 0, entryIndex = 0; currentPocIndex < decodeOrderTableSize; ++currentPocIndex) {
    1494             :             // Set the Decode Order
    1495        6024 :             gopNumber = (currentPocIndex / predictionStructurePtr->pred_struct_period);
    1496        6024 :             baseNumber = gopNumber * predictionStructurePtr->pred_struct_period;
    1497             : 
    1498        6024 :             if (predType == EB_PRED_RANDOM_ACCESS)
    1499        2616 :                 decodeOrderTable[currentPocIndex] = baseNumber + predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->decode_order;
    1500             :             else
    1501        3408 :                 decodeOrderTable[currentPocIndex] = currentPocIndex;
    1502        6024 :             displayOrderTable[decodeOrderTable[currentPocIndex]] = currentPocIndex;
    1503             : 
    1504             :             // Increment the entryIndex
    1505        6024 :             entryIndex = (entryIndex == predictionStructurePtr->pred_struct_entry_count - 1) ?
    1506        6024 :                 predictionStructurePtr->pred_struct_entry_count - predictionStructurePtr->pred_struct_period :
    1507             :                 entryIndex + 1;
    1508             :         }
    1509             : 
    1510             :         // Construct the timeline map from the dependency lists
    1511        3156 :         for (refPocIndex = 0, entryIndex = 0; refPocIndex < timelineSize; ++refPocIndex) {
    1512             :             // Initialize Max to most negative signed value and Min to most positive signed value
    1513        3012 :             depListMax = MIN_SIGNED_VALUE;
    1514        3012 :             depListMin = MAX_SIGNED_VALUE;
    1515             : 
    1516             :             // Find depListMax and depListMin for the entryIndex in the prediction structure for dep_list0
    1517        9851 :             for (depIndex = 0; depIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list_count; ++depIndex) {
    1518        6839 :                 adjustedDepIndex = predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list[depIndex] + (int32_t)refPocIndex;
    1519             : 
    1520             :                 //if(adjustedDepIndex >= 0 && adjustedDepIndex < (int32_t) timelineSize) {
    1521        6839 :                 if (adjustedDepIndex >= 0) {
    1522             :                     // Update Max
    1523        6839 :                     depListMax = MAX(decodeOrderTable[adjustedDepIndex], depListMax);
    1524             : 
    1525             :                     // Update Min
    1526        6839 :                     depListMin = MIN(decodeOrderTable[adjustedDepIndex], depListMin);
    1527             :                 }
    1528             :             }
    1529             : 
    1530             :             // Continue search for depListMax and depListMin for the entryIndex in the prediction structure for dep_list1,
    1531             :             //   the lists are combined in the RPS logic
    1532        6763 :             for (depIndex = 0; depIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list_count; ++depIndex) {
    1533        3751 :                 adjustedDepIndex = predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list[depIndex] + (int32_t)refPocIndex;
    1534             : 
    1535             :                 //if(adjustedDepIndex >= 0 && adjustedDepIndex < (int32_t) timelineSize)  {
    1536        3751 :                 if (adjustedDepIndex >= 0) {
    1537             :                     // Update Max
    1538        3751 :                     depListMax = MAX(decodeOrderTable[adjustedDepIndex], depListMax);
    1539             : 
    1540             :                     // Update Min
    1541        3751 :                     depListMin = MIN(decodeOrderTable[adjustedDepIndex], depListMin);
    1542             :                 }
    1543             :             }
    1544             : 
    1545             :             // If the Dependent Lists are empty, ensure that no RPS signaling is set
    1546        3012 :             if ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list0.list_count > 0) ||
    1547        1376 :                 (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->dep_list1.list_count > 0)) {
    1548             :                 // Determine lifetimeStart and lifetimeSpan - its important to note that out-of-range references are
    1549             :                 //   clipped/eliminated to not violate IDR/CRA referencing rules
    1550        1636 :                 lifetimeStart = depListMin;
    1551             : 
    1552        1636 :                 if (lifetimeStart < (int32_t)timelineSize) {
    1553        1532 :                     lifetimeStart = CLIP3(0, (int32_t)(timelineSize - 1), lifetimeStart);
    1554             : 
    1555        1532 :                     lifetimeSpan = depListMax - depListMin + 1;
    1556        1532 :                     lifetimeSpan = CLIP3(0, (int32_t)timelineSize - lifetimeStart, lifetimeSpan);
    1557             : 
    1558             :                     // Set the timelineMap
    1559       11011 :                     for (currentPocIndex = (uint32_t)lifetimeStart; currentPocIndex < (uint32_t)(lifetimeStart + lifetimeSpan); ++currentPocIndex)
    1560        9479 :                         timelineMap[refPocIndex*timelineSize + displayOrderTable[currentPocIndex]] = EB_TRUE;
    1561             :                 }
    1562             :             }
    1563             : 
    1564             :             // Increment the entryIndex
    1565        3012 :             entryIndex = (entryIndex == predictionStructurePtr->pred_struct_entry_count - 1) ?
    1566        3012 :                 predictionStructurePtr->pred_struct_entry_count - predictionStructurePtr->pred_struct_period :
    1567             :                 entryIndex + 1;
    1568             :         }
    1569             : 
    1570             :         //--------------------------------------------------------
    1571             :         // Create the RPS for Prediction Structure Entry
    1572             :         //--------------------------------------------------------
    1573             : 
    1574             :         // *Note- many of the below Syntax Elements are signaled
    1575             :         //    in the Slice Header and not in the RPS.  These syntax
    1576             :         //    elements can be configured during runtime to manipulate
    1577             :         //    existing RPS structures.  E.g. a reference list could
    1578             :         //    be shortened...
    1579             : 
    1580             :         // Initialize the RPS Group
    1581         144 :         predictionStructurePtr->restricted_ref_pic_lists_enable_flag = EB_TRUE;
    1582         144 :         predictionStructurePtr->lists_modification_enable_flag = EB_FALSE;
    1583         144 :         predictionStructurePtr->long_term_enable_flag = EB_FALSE;
    1584         144 :         predictionStructurePtr->default_ref_pics_list0_total_count_minus1 = 0;
    1585         144 :         predictionStructurePtr->default_ref_pics_list1_total_count_minus1 = 0;
    1586             : 
    1587             :         // For each RPS Index
    1588        3156 :         for (entryIndex = 0; entryIndex < predictionStructurePtr->pred_struct_entry_count; ++entryIndex) {
    1589             :             // Determine the Current POC Index
    1590        3012 :             currentPocIndex = entryIndex;
    1591             : 
    1592             :             // Initialize the RPS
    1593        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->short_term_rps_in_sps_flag = EB_TRUE;
    1594        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->short_term_rps_in_sps_index = entryIndex;
    1595        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->inter_rps_prediction_flag = EB_FALSE;
    1596        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->long_term_rps_present_flag = EB_FALSE;
    1597        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->list0_modification_flag = EB_FALSE;
    1598        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->negative_ref_pics_total_count = 0;
    1599        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->positive_ref_pics_total_count = 0;
    1600        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1 = ~0;
    1601        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1 = ~0;
    1602             : 
    1603             :             // Create the Negative List
    1604        3012 :             prevDeltaPoc = 0;
    1605       60372 :             for (refPocIndex = currentPocIndex - 1; (int32_t)refPocIndex >= 0; --refPocIndex) {
    1606             :                 // Find the deltaPoc value
    1607       57360 :                 deltaPoc = (int32_t)currentPocIndex - (int32_t)refPocIndex;
    1608             : 
    1609             :                 // Check to see if the deltaPoc is in Reference List 0
    1610       57360 :                 pocInreference_list0 = EB_FALSE;
    1611      163887 :                 for (refIndex = 0; (refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count) && (pocInreference_list0 == EB_FALSE); ++refIndex) {
    1612             :                     // Reference List 0
    1613      106527 :                     if ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] != 0) &&
    1614      106527 :                         (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] == deltaPoc))
    1615             :                     {
    1616        4503 :                         pocInreference_list0 = EB_TRUE;
    1617        4503 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1;
    1618             :                     }
    1619             :                 }
    1620             : 
    1621             :                 // Check to see if the deltaPoc is in Reference List 1
    1622       57360 :                 pocInreference_list1 = EB_FALSE;
    1623      131909 :                 for (refIndex = 0; (refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count) && (pocInreference_list1 == EB_FALSE); ++refIndex) {
    1624             :                     // Reference List 1
    1625       74549 :                     if ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] != 0) &&
    1626       74549 :                         (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] == deltaPoc))
    1627             :                     {
    1628        1886 :                         pocInreference_list1 = EB_TRUE;
    1629        1886 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1;
    1630             :                     }
    1631             :                 }
    1632             : 
    1633             :                 // Check to see if the refPocIndex is in the timeline
    1634       57360 :                 pocInTimeline = timelineMap[refPocIndex*timelineSize + currentPocIndex];
    1635             : 
    1636             :                 // If the deltaPoc is in the timeline
    1637       57360 :                 if (pocInTimeline == EB_TRUE) {
    1638        6935 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->used_by_negative_curr_pic_flag[predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->negative_ref_pics_total_count] = (pocInreference_list0 == EB_TRUE || pocInreference_list1 == EB_TRUE) ? EB_TRUE : EB_FALSE;
    1639        6935 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->delta_negative_gop_pos_minus1[predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->negative_ref_pics_total_count++] = deltaPoc - 1 - prevDeltaPoc;
    1640        6935 :                     prevDeltaPoc = deltaPoc;
    1641             :                 }
    1642             :             }
    1643             : 
    1644             :             // Create the Positive List
    1645        3012 :             prevDeltaPoc = 0;
    1646       60372 :             for (refPocIndex = currentPocIndex + 1; refPocIndex < timelineSize; ++refPocIndex) {
    1647             :                 // Find the deltaPoc value
    1648       57360 :                 deltaPoc = (int32_t)currentPocIndex - (int32_t)refPocIndex;
    1649             : 
    1650             :                 // Check to see if the deltaPoc is in Ref List 0
    1651       57360 :                 pocInreference_list0 = EB_FALSE;
    1652      157573 :                 for (refIndex = 0; (refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list_count) && (pocInreference_list0 == EB_FALSE); ++refIndex) {
    1653             :                     // Reference List 0
    1654      100213 :                     if ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] != 0) &&
    1655      100213 :                         (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list0.reference_list[refIndex] == deltaPoc))
    1656             :                     {
    1657         743 :                         pocInreference_list0 = EB_TRUE;
    1658         743 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1;
    1659             :                     }
    1660             :                 }
    1661             : 
    1662             :                 // Check to see if the deltaPoc is in Ref List 1
    1663       57360 :                 pocInreference_list1 = EB_FALSE;
    1664       99254 :                 for (refIndex = 0; (refIndex < predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list_count) && (pocInreference_list1 == EB_FALSE); ++refIndex) {
    1665       41894 :                     if ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] != 0) &&
    1666       41894 :                         (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_list1.reference_list[refIndex] == deltaPoc))
    1667             :                     {
    1668        1104 :                         pocInreference_list1 = EB_TRUE;
    1669        1104 :                         ++predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1;
    1670             :                     }
    1671             :                 }
    1672             : 
    1673             :                 // Check to see if the Y-position is in the timeline
    1674       57360 :                 pocInTimeline = timelineMap[refPocIndex*timelineSize + currentPocIndex];
    1675             : 
    1676             :                 // If the Y-position is in the time lime
    1677       57360 :                 if (pocInTimeline == EB_TRUE) {
    1678        2544 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->used_by_positive_curr_pic_flag[predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->positive_ref_pics_total_count] = (pocInreference_list0 == EB_TRUE || pocInreference_list1 == EB_TRUE) ? EB_TRUE : EB_FALSE;
    1679        2544 :                     predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->delta_positive_gop_pos_minus1[predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->positive_ref_pics_total_count++] = -deltaPoc - 1 - prevDeltaPoc;
    1680        2544 :                     prevDeltaPoc = -deltaPoc;
    1681             :                 }
    1682             :             }
    1683             : 
    1684             :             // Adjust Reference Counts if list is empty
    1685        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1 =
    1686        3012 :                 (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1 == ~0) ?
    1687        3012 :                 0 :
    1688        2868 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1;
    1689             : 
    1690        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1 =
    1691        3012 :                 (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1 == ~0) ?
    1692        3012 :                 0 :
    1693        1608 :                 predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1;
    1694             : 
    1695             :             // Set ref_pics_override_total_count_flag to TRUE if Reflist_count is different than the default
    1696        3012 :             predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_override_total_count_flag =
    1697        3012 :                 ((predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list0_total_count_minus1 != (int32_t)predictionStructurePtr->default_ref_pics_list0_total_count_minus1) ||
    1698        1551 :                 (predictionStructurePtr->pred_struct_entry_ptr_array[entryIndex]->ref_pics_list1_total_count_minus1 != (int32_t)predictionStructurePtr->default_ref_pics_list1_total_count_minus1)) ?
    1699        3012 :                 EB_TRUE :
    1700             :                 EB_FALSE;
    1701             :         }
    1702             :     }
    1703             : 
    1704         144 :     return EB_ErrorNone;
    1705             : }
    1706             : 
    1707           2 : static void prediction_structure_group_dctor(EbPtr p)
    1708             : {
    1709           2 :     PredictionStructureGroup *obj = (PredictionStructureGroup*)p;
    1710         146 :     EB_DELETE_PTR_ARRAY(obj->prediction_structure_ptr_array, obj->prediction_structure_count);
    1711           2 : }
    1712             : /*************************************************
    1713             :  * Prediction Structure Group Ctor
    1714             :  *
    1715             :  * Summary: Converts the Prediction Structure Config
    1716             :  *   into the usable Prediction Structure with RPS and
    1717             :  *   Dependent List control.
    1718             :  *
    1719             :  * From each config, several prediction structures
    1720             :  *   are created. These include:
    1721             :  *   -Variable Number of References
    1722             :  *      # [1 - 4]
    1723             :  *   -Temporal Layers
    1724             :  *      # [1 - 6]
    1725             :  *   -GOP Type
    1726             :  *      # Low Delay P
    1727             :  *      # Low Delay B
    1728             :  *      # Random Access
    1729             :  *
    1730             :  *************************************************/
    1731             : 
    1732           2 : EbErrorType prediction_structure_group_ctor(
    1733             :     PredictionStructureGroup   *predictionStructureGroupPtr,
    1734             :     uint8_t          enc_mode,
    1735             :     uint32_t                         baseLayerSwitchMode)
    1736             : {
    1737           2 :     uint32_t          predStructIndex = 0;
    1738             :     uint32_t          refIdx;
    1739             :     uint32_t          hierarchicalLevelIdx;
    1740             :     uint32_t          predTypeIdx;
    1741             :     uint32_t          numberOfReferences;
    1742             : 
    1743           2 :     predictionStructureGroupPtr->dctor = prediction_structure_group_dctor;
    1744             : 
    1745           2 :     if (enc_mode > ENC_M0) {
    1746           8 :         for (int gop_i = 1; gop_i < 8; ++gop_i) {
    1747          28 :             for (int i = 1; i < 4; ++i) {
    1748          21 :                 four_level_hierarchical_pred_struct[gop_i].ref_list0[i] = 0;
    1749          21 :                 four_level_hierarchical_pred_struct[gop_i].ref_list1[i] = 0;
    1750             :             }
    1751             :         }
    1752             : 
    1753          16 :         for (int gop_i = 1; gop_i < 16; ++gop_i) {
    1754          60 :             for (int i = 1; i < 4; ++i) {
    1755          45 :                 five_level_hierarchical_pred_struct[gop_i].ref_list0[i] = 0;
    1756          45 :                 five_level_hierarchical_pred_struct[gop_i].ref_list1[i] = 0;
    1757             :             }
    1758             :         }
    1759             :     }
    1760             : 
    1761             :     // Count the number of Prediction Structures
    1762          14 :     while ((PredictionStructureConfigArray[predStructIndex].entry_array != 0) && (PredictionStructureConfigArray[predStructIndex].entry_count != 0)) {
    1763             :         // Get Random Access + P for temporal ID 0
    1764          12 :         if (PredictionStructureConfigArray[predStructIndex].entry_array->temporal_layer_index == 0 && baseLayerSwitchMode) {
    1765           0 :             for (refIdx = 0; refIdx < REF_LIST_MAX_DEPTH; ++refIdx)
    1766           0 :                 PredictionStructureConfigArray[predStructIndex].entry_array->ref_list1[refIdx] = 0;
    1767             :         }
    1768          12 :         ++predStructIndex;
    1769             :     }
    1770             : 
    1771           2 :     predictionStructureGroupPtr->prediction_structure_count = MAX_TEMPORAL_LAYERS * EB_PRED_TOTAL_COUNT * REF_LIST_MAX_DEPTH;
    1772           2 :     EB_ALLOC_PTR_ARRAY(predictionStructureGroupPtr->prediction_structure_ptr_array, predictionStructureGroupPtr->prediction_structure_count);
    1773          14 :     for (hierarchicalLevelIdx = 0; hierarchicalLevelIdx < MAX_TEMPORAL_LAYERS; ++hierarchicalLevelIdx) {
    1774          48 :         for (predTypeIdx = 0; predTypeIdx < EB_PRED_TOTAL_COUNT; ++predTypeIdx) {
    1775         180 :             for (refIdx = 0; refIdx < REF_LIST_MAX_DEPTH; ++refIdx) {
    1776         144 :                 predStructIndex = PRED_STRUCT_INDEX(hierarchicalLevelIdx, predTypeIdx, refIdx);
    1777         144 :                 numberOfReferences = refIdx + 1;
    1778             : 
    1779         144 :                 EB_NEW(
    1780             :                     predictionStructureGroupPtr->prediction_structure_ptr_array[predStructIndex],
    1781             :                     PredictionStructureCtor,
    1782             :                     &(PredictionStructureConfigArray[hierarchicalLevelIdx]),
    1783             :                     (EbPred)predTypeIdx,
    1784             :                     numberOfReferences);
    1785             :             }
    1786             :         }
    1787             :     }
    1788             : 
    1789           2 :     return EB_ErrorNone;
    1790             : }

Generated by: LCOV version 1.14