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