1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
|
/*
* gstvaapidecoder_h265.c - H.265 decoder
*
* Copyright (C) 2015 Intel Corporation
* Author: Sreerenj Balachandran <sreerenj.balachandran@intel.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2.1
* of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free
* Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/**
* SECTION:gstvaapidecoder_h265
* @short_description: H.265 decoder
*/
#include "sysdeps.h"
#include <string.h>
#include <math.h>
#include <gst/base/gstadapter.h>
#include <gst/codecparsers/gsth265parser.h>
#include "gstvaapicompat.h"
#include "gstvaapidecoder_h265.h"
#include "gstvaapidecoder_objects.h"
#include "gstvaapidecoder_priv.h"
#include "gstvaapidisplay_priv.h"
#include "gstvaapiobject_priv.h"
#include "gstvaapiutils_h265_priv.h"
#define DEBUG 1
#include "gstvaapidebug.h"
/* Defined to 1 if strict ordering of DPB is needed. Only useful for debug */
#define USE_STRICT_DPB_ORDERING 0
typedef struct _GstVaapiDecoderH265Private GstVaapiDecoderH265Private;
typedef struct _GstVaapiDecoderH265Class GstVaapiDecoderH265Class;
typedef struct _GstVaapiFrameStore GstVaapiFrameStore;
typedef struct _GstVaapiFrameStoreClass GstVaapiFrameStoreClass;
typedef struct _GstVaapiParserInfoH265 GstVaapiParserInfoH265;
typedef struct _GstVaapiPictureH265 GstVaapiPictureH265;
static gboolean nal_is_slice (guint8 nal_type);
/* ------------------------------------------------------------------------- */
/* --- H.265 Parser Info --- */
/* ------------------------------------------------------------------------- */
/*
* Extended decoder unit flags:
*
* @GST_VAAPI_DECODER_UNIT_AU_START: marks the start of an access unit.
* @GST_VAAPI_DECODER_UNIT_AU_END: marks the end of an access unit.
*/
enum
{
GST_VAAPI_DECODER_UNIT_FLAG_AU_START =
(GST_VAAPI_DECODER_UNIT_FLAG_LAST << 0),
GST_VAAPI_DECODER_UNIT_FLAG_AU_END = (GST_VAAPI_DECODER_UNIT_FLAG_LAST << 1),
GST_VAAPI_DECODER_UNIT_FLAGS_AU = (GST_VAAPI_DECODER_UNIT_FLAG_AU_START |
GST_VAAPI_DECODER_UNIT_FLAG_AU_END),
};
#define GST_VAAPI_PARSER_INFO_H265(obj) \
((GstVaapiParserInfoH265 *)(obj))
struct _GstVaapiParserInfoH265
{
GstVaapiMiniObject parent_instance;
GstH265NalUnit nalu;
union
{
GstH265VPS vps;
GstH265SPS sps;
GstH265PPS pps;
GArray *sei;
GstH265SliceHdr slice_hdr;
} data;
guint state;
guint flags; // Same as decoder unit flags (persistent)
};
static void
gst_vaapi_parser_info_h265_finalize (GstVaapiParserInfoH265 * pi)
{
if (nal_is_slice (pi->nalu.type))
gst_h265_slice_hdr_free (&pi->data.slice_hdr);
else {
switch (pi->nalu.type) {
case GST_H265_NAL_VPS:
case GST_H265_NAL_SPS:
case GST_H265_NAL_PPS:
break;
case GST_H265_NAL_PREFIX_SEI:
case GST_H265_NAL_SUFFIX_SEI:
if (pi->data.sei) {
g_array_unref (pi->data.sei);
pi->data.sei = NULL;
}
break;
}
}
}
static inline const GstVaapiMiniObjectClass *
gst_vaapi_parser_info_h265_class (void)
{
static const GstVaapiMiniObjectClass GstVaapiParserInfoH265Class = {
.size = sizeof (GstVaapiParserInfoH265),
.finalize = (GDestroyNotify) gst_vaapi_parser_info_h265_finalize
};
return &GstVaapiParserInfoH265Class;
}
static inline GstVaapiParserInfoH265 *
gst_vaapi_parser_info_h265_new (void)
{
return (GstVaapiParserInfoH265 *)
gst_vaapi_mini_object_new (gst_vaapi_parser_info_h265_class ());
}
#define gst_vaapi_parser_info_h265_ref(pi) \
gst_vaapi_mini_object_ref(GST_VAAPI_MINI_OBJECT(pi))
#define gst_vaapi_parser_info_h265_unref(pi) \
gst_vaapi_mini_object_unref(GST_VAAPI_MINI_OBJECT(pi))
#define gst_vaapi_parser_info_h265_replace(old_pi_ptr, new_pi) \
gst_vaapi_mini_object_replace((GstVaapiMiniObject **)(old_pi_ptr), \
(GstVaapiMiniObject *)(new_pi))
/* ------------------------------------------------------------------------- */
/* --- H.265 Pictures --- */
/* ------------------------------------------------------------------------- */
/*
* Extended picture flags:
*
* @GST_VAAPI_PICTURE_FLAG_IDR: flag that specifies an IDR picture
* @GST_VAAPI_PICTURE_FLAG_AU_START: flag that marks the start of an
* access unit (AU)
* @GST_VAAPI_PICTURE_FLAG_AU_END: flag that marks the end of an
* access unit (AU)
* @GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_BEFORE: flag indicate the inclusion
* of picture in RefPicSetStCurrBefore reference list
* @GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_AFTER: flag indicate the inclusion
* of picture in RefPictSetStCurrAfter reference list
* @GST_VAAPI_PICTURE_FLAG_RPS_ST_FOLL: flag indicate the inclusion
* of picture in RefPicSetStFoll reference list
* @GST_VAAPI_PICTURE_FLAG_RPS_LT_CURR: flag indicate the inclusion
* of picture in RefPicSetLtCurr reference list
* @GST_VAAPI_PICTURE_FLAG_RPS_LT_FOLL: flag indicate the inclusion
* of picture in RefPicSetLtFoll reference list
* @GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE: flag that specifies
* "used for short-term reference"
* @GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE: flag that specifies
* "used for long-term reference"
* @GST_VAAPI_PICTURE_FLAGS_REFERENCE: mask covering any kind of
* reference picture (short-term reference or long-term reference)
*/
enum
{
GST_VAAPI_PICTURE_FLAG_IDR = (GST_VAAPI_PICTURE_FLAG_LAST << 0),
GST_VAAPI_PICTURE_FLAG_REFERENCE2 = (GST_VAAPI_PICTURE_FLAG_LAST << 1),
GST_VAAPI_PICTURE_FLAG_AU_START = (GST_VAAPI_PICTURE_FLAG_LAST << 4),
GST_VAAPI_PICTURE_FLAG_AU_END = (GST_VAAPI_PICTURE_FLAG_LAST << 5),
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_BEFORE =
(GST_VAAPI_PICTURE_FLAG_LAST << 6),
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_AFTER = (GST_VAAPI_PICTURE_FLAG_LAST << 7),
GST_VAAPI_PICTURE_FLAG_RPS_ST_FOLL = (GST_VAAPI_PICTURE_FLAG_LAST << 8),
GST_VAAPI_PICTURE_FLAG_RPS_LT_CURR = (GST_VAAPI_PICTURE_FLAG_LAST << 9),
GST_VAAPI_PICTURE_FLAG_RPS_LT_FOLL = (GST_VAAPI_PICTURE_FLAG_LAST << 10),
GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE =
(GST_VAAPI_PICTURE_FLAG_REFERENCE),
GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE =
(GST_VAAPI_PICTURE_FLAG_REFERENCE | GST_VAAPI_PICTURE_FLAG_REFERENCE2),
GST_VAAPI_PICTURE_FLAGS_REFERENCE =
(GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE |
GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE),
GST_VAAPI_PICTURE_FLAGS_RPS_ST =
(GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_BEFORE |
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_AFTER |
GST_VAAPI_PICTURE_FLAG_RPS_ST_FOLL),
GST_VAAPI_PICTURE_FLAGS_RPS_LT =
(GST_VAAPI_PICTURE_FLAG_RPS_LT_CURR | GST_VAAPI_PICTURE_FLAG_RPS_LT_FOLL),
};
#define GST_VAAPI_PICTURE_IS_IDR(picture) \
(GST_VAAPI_PICTURE_FLAG_IS_SET(picture, GST_VAAPI_PICTURE_FLAG_IDR))
#define GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE(picture) \
((GST_VAAPI_PICTURE_FLAGS(picture) & \
GST_VAAPI_PICTURE_FLAGS_REFERENCE) == \
GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE)
#define GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE(picture) \
((GST_VAAPI_PICTURE_FLAGS(picture) & \
GST_VAAPI_PICTURE_FLAGS_REFERENCE) == \
GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE)
#define GST_VAAPI_PICTURE_H265(picture) \
((GstVaapiPictureH265 *)(picture))
struct _GstVaapiPictureH265
{
GstVaapiPicture base;
GstH265SliceHdr *last_slice_hdr;
guint structure;
gint32 poc; // PicOrderCntVal (8.3.1)
gint32 poc_lsb; // slice_pic_order_cnt_lsb
guint32 pic_latency_cnt; // PicLatencyCount
guint output_flag:1;
guint output_needed:1;
guint NoRaslOutputFlag:1;
guint NoOutputOfPriorPicsFlag:1;
guint RapPicFlag:1; // nalu type between 16 and 21
guint IntraPicFlag:1; // Intra pic (only Intra slices)
};
GST_VAAPI_CODEC_DEFINE_TYPE (GstVaapiPictureH265, gst_vaapi_picture_h265);
void
gst_vaapi_picture_h265_destroy (GstVaapiPictureH265 * picture)
{
gst_vaapi_picture_destroy (GST_VAAPI_PICTURE (picture));
}
gboolean
gst_vaapi_picture_h265_create (GstVaapiPictureH265 * picture,
const GstVaapiCodecObjectConstructorArgs * args)
{
if (!gst_vaapi_picture_create (GST_VAAPI_PICTURE (picture), args))
return FALSE;
picture->structure = picture->base.structure;
picture->poc = G_MAXINT32;
picture->output_needed = FALSE;
return TRUE;
}
static inline GstVaapiPictureH265 *
gst_vaapi_picture_h265_new (GstVaapiDecoderH265 * decoder)
{
return (GstVaapiPictureH265 *)
gst_vaapi_codec_object_new (&GstVaapiPictureH265Class,
GST_VAAPI_CODEC_BASE (decoder), NULL,
sizeof (VAPictureParameterBufferHEVC), NULL, 0, 0);
}
static inline void
gst_vaapi_picture_h265_set_reference (GstVaapiPictureH265 * picture,
guint reference_flags)
{
if (!picture)
return;
GST_VAAPI_PICTURE_FLAG_UNSET (picture,
GST_VAAPI_PICTURE_FLAGS_RPS_ST | GST_VAAPI_PICTURE_FLAGS_RPS_LT);
GST_VAAPI_PICTURE_FLAG_UNSET (picture, GST_VAAPI_PICTURE_FLAGS_REFERENCE);
GST_VAAPI_PICTURE_FLAG_SET (picture, reference_flags);
}
/* ------------------------------------------------------------------------- */
/* --- Frame Buffers (DPB) --- */
/* ------------------------------------------------------------------------- */
struct _GstVaapiFrameStore
{
/*< private > */
GstVaapiMiniObject parent_instance;
GstVaapiPictureH265 *buffer;
};
static void
gst_vaapi_frame_store_finalize (gpointer object)
{
GstVaapiFrameStore *const fs = object;
gst_vaapi_picture_replace (&fs->buffer, NULL);
}
static GstVaapiFrameStore *
gst_vaapi_frame_store_new (GstVaapiPictureH265 * picture)
{
GstVaapiFrameStore *fs;
static const GstVaapiMiniObjectClass GstVaapiFrameStoreClass = {
sizeof (GstVaapiFrameStore),
gst_vaapi_frame_store_finalize
};
fs = (GstVaapiFrameStore *)
gst_vaapi_mini_object_new (&GstVaapiFrameStoreClass);
if (!fs)
return NULL;
fs->buffer = gst_vaapi_picture_ref (picture);
return fs;
}
static inline gboolean
gst_vaapi_frame_store_has_reference (GstVaapiFrameStore * fs)
{
if (GST_VAAPI_PICTURE_IS_REFERENCE (fs->buffer))
return TRUE;
return FALSE;
}
#define gst_vaapi_frame_store_ref(fs) \
gst_vaapi_mini_object_ref(GST_VAAPI_MINI_OBJECT(fs))
#define gst_vaapi_frame_store_unref(fs) \
gst_vaapi_mini_object_unref(GST_VAAPI_MINI_OBJECT(fs))
#define gst_vaapi_frame_store_replace(old_fs_p, new_fs) \
gst_vaapi_mini_object_replace((GstVaapiMiniObject **)(old_fs_p), \
(GstVaapiMiniObject *)(new_fs))
/* ------------------------------------------------------------------------- */
/* --- H.265 Decoder --- */
/* ------------------------------------------------------------------------- */
#define GST_VAAPI_DECODER_H265_CAST(decoder) \
((GstVaapiDecoderH265 *)(decoder))
typedef enum
{
GST_H265_VIDEO_STATE_GOT_VPS = 1 << 0,
GST_H265_VIDEO_STATE_GOT_SPS = 1 << 1,
GST_H265_VIDEO_STATE_GOT_PPS = 1 << 2,
GST_H265_VIDEO_STATE_GOT_SLICE = 1 << 3,
GST_H265_VIDEO_STATE_VALID_PICTURE_HEADERS =
(GST_H265_VIDEO_STATE_GOT_SPS | GST_H265_VIDEO_STATE_GOT_PPS),
GST_H265_VIDEO_STATE_VALID_PICTURE =
(GST_H265_VIDEO_STATE_VALID_PICTURE_HEADERS |
GST_H265_VIDEO_STATE_GOT_SLICE)
} GstH265VideoState;
struct _GstVaapiDecoderH265Private
{
GstH265Parser *parser;
guint parser_state;
guint decoder_state;
GstVaapiStreamAlignH265 stream_alignment;
GstVaapiPictureH265 *current_picture;
GstVaapiParserInfoH265 *vps[GST_H265_MAX_VPS_COUNT];
GstVaapiParserInfoH265 *active_vps;
GstVaapiParserInfoH265 *sps[GST_H265_MAX_SPS_COUNT];
GstVaapiParserInfoH265 *active_sps;
GstVaapiParserInfoH265 *pps[GST_H265_MAX_PPS_COUNT];
GstVaapiParserInfoH265 *active_pps;
GstVaapiParserInfoH265 *prev_pi;
GstVaapiParserInfoH265 *prev_slice_pi;
GstVaapiParserInfoH265 *prev_independent_slice_pi;
GstVaapiFrameStore **dpb;
guint dpb_count;
guint dpb_size;
guint dpb_size_max;
GstVaapiProfile profile;
GstVaapiEntrypoint entrypoint;
GstVaapiChromaType chroma_type;
GstVaapiPictureH265 *RefPicSetStCurrBefore[16];
GstVaapiPictureH265 *RefPicSetStCurrAfter[16];
GstVaapiPictureH265 *RefPicSetStFoll[16];
GstVaapiPictureH265 *RefPicSetLtCurr[16];
GstVaapiPictureH265 *RefPicSetLtFoll[16];
GstVaapiPictureH265 *RefPicList0[16];
guint RefPicList0_count;
GstVaapiPictureH265 *RefPicList1[16];
guint RefPicList1_count;
guint32 SpsMaxLatencyPictures;
gint32 WpOffsetHalfRangeC;
guint nal_length_size;
guint pic_width_in_luma_samples; //sps->pic_width_in_luma_samples
guint pic_height_in_luma_samples; //sps->pic_height_in_luma_samples
guint pic_structure; // pic_struct (from SEI pic_timing() or inferred)
gint32 poc; // PicOrderCntVal
gint32 poc_msb; // PicOrderCntMsb
gint32 poc_lsb; // pic_order_cnt_lsb (from slice_header())
gint32 prev_poc_msb; // prevPicOrderCntMsb
gint32 prev_poc_lsb; // prevPicOrderCntLsb
gint32 prev_tid0pic_poc_lsb;
gint32 prev_tid0pic_poc_msb;
gint32 PocStCurrBefore[16];
gint32 PocStCurrAfter[16];
gint32 PocStFoll[16];
gint32 PocLtCurr[16];
gint32 PocLtFoll[16];
guint NumPocStCurrBefore;
guint NumPocStCurrAfter;
guint NumPocStFoll;
guint NumPocLtCurr;
guint NumPocLtFoll;
guint NumPocTotalCurr;
guint is_opened:1;
guint is_hvcC:1;
guint has_context:1;
guint progressive_sequence:1;
guint new_bitstream:1;
guint prev_nal_is_eos:1; /*previous nal type is EOS */
guint associated_irap_NoRaslOutputFlag:1;
};
/**
* GstVaapiDecoderH265:
*
* A decoder based on H265.
*/
struct _GstVaapiDecoderH265
{
/*< private > */
GstVaapiDecoder parent_instance;
GstVaapiDecoderH265Private priv;
};
/**
* GstVaapiDecoderH265Class:
*
* A decoder class based on H265.
*/
struct _GstVaapiDecoderH265Class
{
/*< private > */
GstVaapiDecoderClass parent_class;
};
#define RSV_VCL_N10 10
#define RSV_VCL_N12 12
#define RSV_VCL_N14 14
static gboolean
nal_is_idr (guint8 nal_type)
{
if ((nal_type == GST_H265_NAL_SLICE_IDR_W_RADL) ||
(nal_type == GST_H265_NAL_SLICE_IDR_N_LP))
return TRUE;
return FALSE;
}
static gboolean
nal_is_irap (guint8 nal_type)
{
if ((nal_type >= GST_H265_NAL_SLICE_BLA_W_LP) &&
(nal_type <= RESERVED_IRAP_NAL_TYPE_MAX))
return TRUE;
return FALSE;
}
static gboolean
nal_is_bla (guint8 nal_type)
{
if ((nal_type >= GST_H265_NAL_SLICE_BLA_W_LP) &&
(nal_type <= GST_H265_NAL_SLICE_BLA_N_LP))
return TRUE;
return FALSE;
}
static gboolean
nal_is_cra (guint8 nal_type)
{
if (nal_type == GST_H265_NAL_SLICE_CRA_NUT)
return TRUE;
return FALSE;
}
static gboolean
nal_is_radl (guint8 nal_type)
{
if ((nal_type >= GST_H265_NAL_SLICE_RADL_N) &&
(nal_type <= GST_H265_NAL_SLICE_RADL_R))
return TRUE;
return FALSE;
}
static gboolean
nal_is_rasl (guint8 nal_type)
{
if ((nal_type >= GST_H265_NAL_SLICE_RASL_N) &&
(nal_type <= GST_H265_NAL_SLICE_RASL_R))
return TRUE;
return FALSE;
}
static gboolean
nal_is_slice (guint8 nal_type)
{
if ((nal_type >= GST_H265_NAL_SLICE_TRAIL_N) &&
(nal_type <= GST_H265_NAL_SLICE_CRA_NUT))
return TRUE;
return FALSE;
}
static gboolean
nal_is_ref (guint8 nal_type)
{
gboolean ret = FALSE;
switch (nal_type) {
case GST_H265_NAL_SLICE_TRAIL_N:
case GST_H265_NAL_SLICE_TSA_N:
case GST_H265_NAL_SLICE_STSA_N:
case GST_H265_NAL_SLICE_RADL_N:
case GST_H265_NAL_SLICE_RASL_N:
case RSV_VCL_N10:
case RSV_VCL_N12:
case RSV_VCL_N14:
ret = FALSE;
break;
default:
ret = TRUE;
break;
}
return ret;
}
/* Activates the supplied PPS */
static GstH265PPS *
ensure_pps (GstVaapiDecoderH265 * decoder, GstH265PPS * pps)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = priv->pps[pps->id];
gst_vaapi_parser_info_h265_replace (&priv->active_pps, pi);
return pi ? &pi->data.pps : NULL;
}
/* Returns the active PPS */
static inline GstH265PPS *
get_pps (GstVaapiDecoderH265 * decoder)
{
GstVaapiParserInfoH265 *const pi = decoder->priv.active_pps;
return pi ? &pi->data.pps : NULL;
}
/* Activate the supplied SPS */
static GstH265SPS *
ensure_sps (GstVaapiDecoderH265 * decoder, GstH265SPS * sps)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = priv->sps[sps->id];
gst_vaapi_parser_info_h265_replace (&priv->active_sps, pi);
return pi ? &pi->data.sps : NULL;
}
/* Returns the active SPS */
static inline GstH265SPS *
get_sps (GstVaapiDecoderH265 * decoder)
{
GstVaapiParserInfoH265 *const pi = decoder->priv.active_sps;
return pi ? &pi->data.sps : NULL;
}
/* VPS nal is not necessary to decode the base layers, so this is not
* needed at the moment. But in future we need this, especially when
* dealing with MVC and scalable layer decoding.
* See https://bugzilla.gnome.org/show_bug.cgi?id=754250
*/
#if 0
/* Activate the supplied VPS */
static GstH265VPS *
ensure_vps (GstVaapiDecoderH265 * decoder, GstH265VPS * vps)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = priv->vps[vps->id];
gst_vaapi_parser_info_h265_replace (&priv->active_vps, pi);
return pi ? &pi->data.vps : NULL;
}
/* Returns the active VPS */
static inline GstH265VPS *
get_vps (GstVaapiDecoderH265 * decoder)
{
GstVaapiParserInfoH265 *const pi = decoder->priv.active_vps;
return pi ? &pi->data.vps : NULL;
}
#endif
/* Get number of reference frames to use */
static guint
get_max_dec_frame_buffering (GstH265SPS * sps)
{
G_GNUC_UNUSED guint max_dec_frame_buffering; /* FIXME */
GstVaapiLevelH265 level;
const GstVaapiH265LevelLimits *level_limits;
level = gst_vaapi_utils_h265_get_level (sps->profile_tier_level.level_idc);
level_limits = gst_vaapi_utils_h265_get_level_limits (level);
if (G_UNLIKELY (!level_limits)) {
GST_FIXME ("unsupported level_idc value (%d)",
sps->profile_tier_level.level_idc);
max_dec_frame_buffering = 16;
}
/* Fixme: Add limit check based on Annex A */
/* Assuming HighestTid as sps_max_sub_layers_minus1 */
return MAX (1,
(sps->max_dec_pic_buffering_minus1[sps->max_sub_layers_minus1] + 1));
}
static void
dpb_remove_index (GstVaapiDecoderH265 * decoder, gint index)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
guint i, num_frames = --priv->dpb_count;
if (USE_STRICT_DPB_ORDERING) {
for (i = index; i < num_frames; i++)
gst_vaapi_frame_store_replace (&priv->dpb[i], priv->dpb[i + 1]);
} else if (index != num_frames)
gst_vaapi_frame_store_replace (&priv->dpb[index], priv->dpb[num_frames]);
gst_vaapi_frame_store_replace (&priv->dpb[num_frames], NULL);
}
static gboolean
dpb_output (GstVaapiDecoderH265 * decoder, GstVaapiFrameStore * fs)
{
GstVaapiPictureH265 *picture;
g_return_val_if_fail (fs != NULL, FALSE);
picture = fs->buffer;
g_return_val_if_fail (picture != NULL, FALSE);
picture->output_needed = FALSE;
return gst_vaapi_picture_output (GST_VAAPI_PICTURE_CAST (picture));
}
/* Get the dpb picture having the specifed poc or poc_lsb */
static GstVaapiPictureH265 *
dpb_get_picture (GstVaapiDecoderH265 * decoder, gint poc, gboolean match_lsb)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
guint i;
for (i = 0; i < priv->dpb_count; i++) {
GstVaapiPictureH265 *const picture = priv->dpb[i]->buffer;
if (picture && GST_VAAPI_PICTURE_FLAG_IS_SET (picture,
GST_VAAPI_PICTURE_FLAGS_REFERENCE)) {
if (match_lsb) {
if (picture->poc_lsb == poc)
return picture;
} else {
if (picture->poc == poc)
return picture;
}
}
}
return NULL;
}
/* Get the dpb picture having the specifed poc and shor/long ref flags */
static GstVaapiPictureH265 *
dpb_get_ref_picture (GstVaapiDecoderH265 * decoder, gint poc, gboolean is_short)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
guint i;
for (i = 0; i < priv->dpb_count; i++) {
GstVaapiPictureH265 *const picture = priv->dpb[i]->buffer;
if (picture && picture->poc == poc) {
if (is_short && GST_VAAPI_PICTURE_IS_SHORT_TERM_REFERENCE (picture))
return picture;
else if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE (picture))
return picture;
}
}
return NULL;
}
/* Finds the picture with the lowest POC that needs to be output */
static gint
dpb_find_lowest_poc (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 ** found_picture_ptr)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPictureH265 *found_picture = NULL;
guint i, found_index;
for (i = 0; i < priv->dpb_count; i++) {
GstVaapiPictureH265 *const picture = priv->dpb[i]->buffer;
if (picture && !picture->output_needed)
continue;
if (!found_picture || found_picture->poc > picture->poc)
found_picture = picture, found_index = i;
}
if (found_picture_ptr)
*found_picture_ptr = found_picture;
return found_picture ? found_index : -1;
}
static gboolean
dpb_bump (GstVaapiDecoderH265 * decoder, GstVaapiPictureH265 * picture)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPictureH265 *found_picture;
gint found_index;
gboolean success;
found_index = dpb_find_lowest_poc (decoder, &found_picture);
if (found_index < 0)
return FALSE;
success = dpb_output (decoder, priv->dpb[found_index]);
if (!gst_vaapi_frame_store_has_reference (priv->dpb[found_index]))
dpb_remove_index (decoder, found_index);
return success;
}
static void
dpb_clear (GstVaapiDecoderH265 * decoder, gboolean hard_flush)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPictureH265 *pic;
guint i;
if (hard_flush) {
for (i = 0; i < priv->dpb_count; i++)
dpb_remove_index (decoder, i);
priv->dpb_count = 0;
} else {
/* Remove unused pictures from DPB */
i = 0;
while (i < priv->dpb_count) {
GstVaapiFrameStore *const fs = priv->dpb[i];
pic = fs->buffer;
if (!pic->output_needed && !gst_vaapi_frame_store_has_reference (fs))
dpb_remove_index (decoder, i);
else
i++;
}
}
}
static void
dpb_flush (GstVaapiDecoderH265 * decoder)
{
/* Output any frame remaining in DPB */
while (dpb_bump (decoder, NULL));
dpb_clear (decoder, TRUE);
}
static gint
dpb_get_num_need_output (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
guint i = 0, n_output_needed = 0;
while (i < priv->dpb_count) {
GstVaapiFrameStore *const fs = priv->dpb[i];
if (fs->buffer->output_needed)
n_output_needed++;
i++;
}
return n_output_needed;
}
static gboolean
check_latency_cnt (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPictureH265 *tmp_pic;
guint i = 0;
while (i < priv->dpb_count) {
GstVaapiFrameStore *const fs = priv->dpb[i];
tmp_pic = fs->buffer;
if (tmp_pic->output_needed) {
if (tmp_pic->pic_latency_cnt >= priv->SpsMaxLatencyPictures)
return TRUE;
}
i++;
}
return FALSE;
}
static gboolean
dpb_add (GstVaapiDecoderH265 * decoder, GstVaapiPictureH265 * picture)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstH265SPS *const sps = get_sps (decoder);
GstVaapiFrameStore *fs;
GstVaapiPictureH265 *tmp_pic;
guint i = 0;
/* C.5.2.3 */
if (picture->output_flag) {
while (i < priv->dpb_count) {
GstVaapiFrameStore *const fs = priv->dpb[i];
tmp_pic = fs->buffer;
if (tmp_pic->output_needed)
tmp_pic->pic_latency_cnt += 1;
i++;
}
}
/* Create new frame store */
fs = gst_vaapi_frame_store_new (picture);
if (!fs)
return FALSE;
gst_vaapi_frame_store_replace (&priv->dpb[priv->dpb_count++], fs);
gst_vaapi_frame_store_unref (fs);
if (picture->output_flag) {
picture->output_needed = 1;
picture->pic_latency_cnt = 0;
} else
picture->output_needed = 0;
/* set pic as short_term_ref */
gst_vaapi_picture_h265_set_reference (picture,
GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE);
/* C.5.2.4 "Bumping" process */
while ((dpb_get_num_need_output (decoder) >
sps->max_num_reorder_pics[sps->max_sub_layers_minus1])
|| (sps->max_latency_increase_plus1[sps->max_sub_layers_minus1]
&& check_latency_cnt (decoder)))
dpb_bump (decoder, picture);
return TRUE;
}
/* C.5.2.2 */
static gboolean
dpb_init (GstVaapiDecoderH265 * decoder, GstVaapiPictureH265 * picture,
GstVaapiParserInfoH265 * pi)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
GstH265SPS *const sps = get_sps (decoder);
if (nal_is_irap (pi->nalu.type)
&& picture->NoRaslOutputFlag && !priv->new_bitstream) {
if (pi->nalu.type == GST_H265_NAL_SLICE_CRA_NUT)
picture->NoOutputOfPriorPicsFlag = 1;
else
picture->NoOutputOfPriorPicsFlag =
slice_hdr->no_output_of_prior_pics_flag;
if (picture->NoOutputOfPriorPicsFlag)
dpb_clear (decoder, TRUE);
else {
dpb_clear (decoder, FALSE);
while (dpb_bump (decoder, NULL));
}
} else {
dpb_clear (decoder, FALSE);
while ((dpb_get_num_need_output (decoder) >
sps->max_num_reorder_pics[sps->max_sub_layers_minus1])
|| (sps->max_latency_increase_plus1[sps->max_sub_layers_minus1]
&& check_latency_cnt (decoder))
|| (priv->dpb_count >=
(sps->max_dec_pic_buffering_minus1[sps->max_sub_layers_minus1] +
1))) {
dpb_bump (decoder, picture);
}
}
return TRUE;
}
static gboolean
dpb_reset (GstVaapiDecoderH265 * decoder, guint dpb_size)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
if (dpb_size > priv->dpb_size_max) {
priv->dpb = g_try_realloc_n (priv->dpb, dpb_size, sizeof (*priv->dpb));
if (!priv->dpb)
return FALSE;
memset (&priv->dpb[priv->dpb_size_max], 0,
(dpb_size - priv->dpb_size_max) * sizeof (*priv->dpb));
priv->dpb_size_max = dpb_size;
}
priv->dpb_size = dpb_size;
GST_DEBUG ("DPB size %u", priv->dpb_size);
return TRUE;
}
static GstVaapiDecoderStatus
get_status (GstH265ParserResult result)
{
GstVaapiDecoderStatus status;
switch (result) {
case GST_H265_PARSER_OK:
status = GST_VAAPI_DECODER_STATUS_SUCCESS;
break;
case GST_H265_PARSER_NO_NAL_END:
status = GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
break;
case GST_H265_PARSER_ERROR:
status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
break;
default:
status = GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
break;
}
return status;
}
static void
gst_vaapi_decoder_h265_close (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
gst_vaapi_picture_replace (&priv->current_picture, NULL);
gst_vaapi_parser_info_h265_replace (&priv->prev_slice_pi, NULL);
gst_vaapi_parser_info_h265_replace (&priv->prev_independent_slice_pi, NULL);
gst_vaapi_parser_info_h265_replace (&priv->prev_pi, NULL);
dpb_clear (decoder, TRUE);
if (priv->parser) {
gst_h265_parser_free (priv->parser);
priv->parser = NULL;
}
}
static gboolean
gst_vaapi_decoder_h265_open (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
gst_vaapi_decoder_h265_close (decoder);
priv->parser = gst_h265_parser_new ();
if (!priv->parser)
return FALSE;
return TRUE;
}
static void
gst_vaapi_decoder_h265_destroy (GstVaapiDecoder * base_decoder)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
GstVaapiDecoderH265Private *const priv = &decoder->priv;
guint i;
gst_vaapi_decoder_h265_close (decoder);
g_free (priv->dpb);
priv->dpb = NULL;
priv->dpb_size = 0;
for (i = 0; i < G_N_ELEMENTS (priv->pps); i++)
gst_vaapi_parser_info_h265_replace (&priv->pps[i], NULL);
gst_vaapi_parser_info_h265_replace (&priv->active_pps, NULL);
for (i = 0; i < G_N_ELEMENTS (priv->sps); i++)
gst_vaapi_parser_info_h265_replace (&priv->sps[i], NULL);
gst_vaapi_parser_info_h265_replace (&priv->active_sps, NULL);
for (i = 0; i < G_N_ELEMENTS (priv->vps); i++)
gst_vaapi_parser_info_h265_replace (&priv->vps[i], NULL);
gst_vaapi_parser_info_h265_replace (&priv->active_vps, NULL);
}
static gboolean
gst_vaapi_decoder_h265_create (GstVaapiDecoder * base_decoder)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
GstVaapiDecoderH265Private *const priv = &decoder->priv;
priv->profile = GST_VAAPI_PROFILE_UNKNOWN;
priv->entrypoint = GST_VAAPI_ENTRYPOINT_VLD;
priv->chroma_type = GST_VAAPI_CHROMA_TYPE_YUV420;
priv->progressive_sequence = TRUE;
priv->new_bitstream = TRUE;
priv->prev_nal_is_eos = FALSE;
return TRUE;
}
static void
fill_profiles (GstVaapiProfile profiles[16], guint * n_profiles_ptr,
GstVaapiProfile profile)
{
guint n_profiles = *n_profiles_ptr;
profiles[n_profiles++] = profile;
switch (profile) {
case GST_VAAPI_PROFILE_H265_MAIN:
profiles[n_profiles++] = GST_VAAPI_PROFILE_H265_MAIN10;
break;
case GST_VAAPI_PROFILE_H265_MAIN_STILL_PICTURE:
profiles[n_profiles++] = GST_VAAPI_PROFILE_H265_MAIN;
profiles[n_profiles++] = GST_VAAPI_PROFILE_H265_MAIN10;
break;
default:
break;
}
*n_profiles_ptr = n_profiles;
}
static GstVaapiProfile
get_profile (GstVaapiDecoderH265 * decoder, GstH265SPS * sps, guint dpb_size)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiDisplay *const display = GST_VAAPI_DECODER_DISPLAY (decoder);
GstVaapiProfile profile, profiles[3];
guint i, n_profiles = 0;
profile =
gst_vaapi_utils_h265_get_profile (sps->profile_tier_level.profile_idc);
if (!profile) {
/* HACK: This is a work-around to identify some main profile streams having wrong profile_idc.
* There are some wrongly encoded main profile streams(eg: ENTP_C_LG_3.bin) which doesn't
* have any of the profile_idc values mentioned in Annex-A, instead general_profile_idc
* has been set as zero and having general_profile_compatibility_flag[general_profile_idc]
* is TRUE. Assuming them as MAIN profile for now */
if (sps->profile_tier_level.profile_space == 0 &&
sps->profile_tier_level.profile_idc == 0 &&
sps->profile_tier_level.profile_compatibility_flag[0] == 1) {
GST_WARNING ("Wrong profile_idc, blindly setting it as main profile !!");
profile = GST_VAAPI_PROFILE_H265_MAIN;
} else
return GST_VAAPI_PROFILE_UNKNOWN;
}
fill_profiles (profiles, &n_profiles, profile);
switch (profile) {
case GST_VAAPI_PROFILE_H265_MAIN10:
if (sps->profile_tier_level.profile_compatibility_flag[1]) { // A.2.3.2 (main profile)
fill_profiles (profiles, &n_profiles, GST_VAAPI_PROFILE_H265_MAIN);
}
break;
default:
break;
}
/* If the preferred profile (profiles[0]) matches one that we already
found, then just return it now instead of searching for it again */
if (profiles[0] == priv->profile)
return priv->profile;
for (i = 0; i < n_profiles; i++) {
if (gst_vaapi_display_has_decoder (display, profiles[i], priv->entrypoint))
return profiles[i];
}
return GST_VAAPI_PROFILE_UNKNOWN;
}
static GstVaapiDecoderStatus
ensure_context (GstVaapiDecoderH265 * decoder, GstH265SPS * sps)
{
GstVaapiDecoder *const base_decoder = GST_VAAPI_DECODER_CAST (decoder);
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiContextInfo info;
GstVaapiProfile profile;
GstVaapiChromaType chroma_type;
gboolean reset_context = FALSE;
guint dpb_size;
dpb_size = get_max_dec_frame_buffering (sps);
if (priv->dpb_size < dpb_size) {
GST_DEBUG ("DPB size increased");
reset_context = TRUE;
}
profile = get_profile (decoder, sps, dpb_size);
if (!profile) {
GST_ERROR ("unsupported profile_idc %u",
sps->profile_tier_level.profile_idc);
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_PROFILE;
}
if (!priv->profile || (priv->profile != profile)) {
GST_DEBUG ("profile changed");
reset_context = TRUE;
priv->profile = profile;
}
chroma_type =
gst_vaapi_utils_h265_get_chroma_type (sps->chroma_format_idc,
sps->bit_depth_luma_minus8 + 8);
if (!chroma_type) {
GST_ERROR ("unsupported chroma_format_idc %u", sps->chroma_format_idc);
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT;
}
if (priv->chroma_type != chroma_type) {
GST_DEBUG ("chroma format changed");
reset_context = TRUE;
priv->chroma_type = chroma_type;
}
if (priv->pic_width_in_luma_samples != sps->pic_width_in_luma_samples ||
priv->pic_height_in_luma_samples != sps->pic_height_in_luma_samples) {
GST_DEBUG ("size changed");
reset_context = TRUE;
priv->pic_width_in_luma_samples = sps->pic_width_in_luma_samples;
priv->pic_height_in_luma_samples = sps->pic_height_in_luma_samples;
}
priv->progressive_sequence = 1; /*Fixme */
gst_vaapi_decoder_set_interlaced (base_decoder, !priv->progressive_sequence);
gst_vaapi_decoder_set_pixel_aspect_ratio (base_decoder,
sps->vui_params.par_n, sps->vui_params.par_d);
if (!reset_context && priv->has_context)
return GST_VAAPI_DECODER_STATUS_SUCCESS;
/* XXX: fix surface size when cropping is implemented */
info.profile = priv->profile;
info.entrypoint = priv->entrypoint;
info.chroma_type = priv->chroma_type;
info.width = sps->width;
info.height = sps->height;
info.ref_frames = dpb_size;
if (!gst_vaapi_decoder_ensure_context (GST_VAAPI_DECODER (decoder), &info))
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
priv->has_context = TRUE;
/* Reset DPB */
if (!dpb_reset (decoder, dpb_size))
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static void
fill_iq_matrix_4x4 (VAIQMatrixBufferHEVC * iq_matrix,
GstH265ScalingList * scaling_list)
{
guint i;
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList4x4) == 6);
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList4x4[0]) == 16);
for (i = 0; i < G_N_ELEMENTS (iq_matrix->ScalingList4x4); i++) {
gst_h265_quant_matrix_4x4_get_raster_from_uprightdiagonal
(iq_matrix->ScalingList4x4[i], scaling_list->scaling_lists_4x4[i]);
}
}
static void
fill_iq_matrix_8x8 (VAIQMatrixBufferHEVC * iq_matrix,
GstH265ScalingList * scaling_list)
{
guint i;
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList8x8) == 6);
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList8x8[0]) == 64);
for (i = 0; i < G_N_ELEMENTS (iq_matrix->ScalingList8x8); i++) {
gst_h265_quant_matrix_8x8_get_raster_from_uprightdiagonal
(iq_matrix->ScalingList8x8[i], scaling_list->scaling_lists_8x8[i]);
}
}
static void
fill_iq_matrix_16x16 (VAIQMatrixBufferHEVC * iq_matrix,
GstH265ScalingList * scaling_list)
{
guint i;
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList16x16) == 6);
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList16x16[0]) == 64);
for (i = 0; i < G_N_ELEMENTS (iq_matrix->ScalingList16x16); i++) {
gst_h265_quant_matrix_16x16_get_raster_from_uprightdiagonal
(iq_matrix->ScalingList16x16[i], scaling_list->scaling_lists_16x16[i]);
}
}
static void
fill_iq_matrix_32x32 (VAIQMatrixBufferHEVC * iq_matrix,
GstH265ScalingList * scaling_list)
{
guint i;
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList32x32) == 2);
g_assert (G_N_ELEMENTS (iq_matrix->ScalingList32x32[0]) == 64);
for (i = 0; i < G_N_ELEMENTS (iq_matrix->ScalingList32x32); i++) {
gst_h265_quant_matrix_32x32_get_raster_from_uprightdiagonal
(iq_matrix->ScalingList32x32[i], scaling_list->scaling_lists_32x32[i]);
}
}
static void
fill_iq_matrix_dc_16x16 (VAIQMatrixBufferHEVC * iq_matrix,
GstH265ScalingList * scaling_list)
{
guint i;
for (i = 0; i < 6; i++)
iq_matrix->ScalingListDC16x16[i] =
scaling_list->scaling_list_dc_coef_minus8_16x16[i] + 8;
}
static void
fill_iq_matrix_dc_32x32 (VAIQMatrixBufferHEVC * iq_matrix,
GstH265ScalingList * scaling_list)
{
guint i;
for (i = 0; i < 2; i++)
iq_matrix->ScalingListDC32x32[i] =
scaling_list->scaling_list_dc_coef_minus8_32x32[i] + 8;
}
static GstVaapiDecoderStatus
ensure_quant_matrix (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture)
{
GstVaapiPicture *const base_picture = &picture->base;
GstH265PPS *const pps = get_pps (decoder);
GstH265SPS *const sps = get_sps (decoder);
GstH265ScalingList *scaling_list = NULL;
VAIQMatrixBufferHEVC *iq_matrix;
if (pps &&
(pps->scaling_list_data_present_flag ||
(sps->scaling_list_enabled_flag
&& !sps->scaling_list_data_present_flag)))
scaling_list = &pps->scaling_list;
else if (sps && sps->scaling_list_enabled_flag
&& sps->scaling_list_data_present_flag)
scaling_list = &sps->scaling_list;
else
return GST_VAAPI_DECODER_STATUS_SUCCESS;
base_picture->iq_matrix = GST_VAAPI_IQ_MATRIX_NEW (HEVC, decoder);
if (!base_picture->iq_matrix) {
GST_ERROR ("failed to allocate IQ matrix");
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
}
iq_matrix = base_picture->iq_matrix->param;
/* Only supporting 4:2:0 */
if (sps->chroma_format_idc != 1)
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CHROMA_FORMAT;
fill_iq_matrix_4x4 (iq_matrix, scaling_list);
fill_iq_matrix_8x8 (iq_matrix, scaling_list);
fill_iq_matrix_16x16 (iq_matrix, scaling_list);
fill_iq_matrix_32x32 (iq_matrix, scaling_list);
fill_iq_matrix_dc_16x16 (iq_matrix, scaling_list);
fill_iq_matrix_dc_32x32 (iq_matrix, scaling_list);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static inline gboolean
is_valid_state (guint state, guint ref_state)
{
return (state & ref_state) == ref_state;
}
static GstVaapiDecoderStatus
decode_current_picture (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPictureH265 *const picture = priv->current_picture;
if (!is_valid_state (priv->decoder_state, GST_H265_VIDEO_STATE_VALID_PICTURE)) {
goto drop_frame;
}
priv->decoder_state = 0;
/*Fixme: Use SEI header values */
priv->pic_structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
if (!picture)
return GST_VAAPI_DECODER_STATUS_SUCCESS;
if (!gst_vaapi_picture_decode (GST_VAAPI_PICTURE_CAST (picture)))
goto error;
if (!dpb_add (decoder, picture))
goto error;
gst_vaapi_picture_replace (&priv->current_picture, NULL);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
error:
gst_vaapi_picture_replace (&priv->current_picture, NULL);
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
drop_frame:
priv->decoder_state = 0;
priv->pic_structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
return (GstVaapiDecoderStatus) GST_VAAPI_DECODER_STATUS_DROP_FRAME;
}
static GstVaapiDecoderStatus
parse_vps (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265VPS *const vps = &pi->data.vps;
GstH265ParserResult result;
GST_DEBUG ("parse VPS");
priv->parser_state = 0;
memset (vps, 0, sizeof (GstH265VPS));
result = gst_h265_parser_parse_vps (priv->parser, &pi->nalu, vps);
if (result != GST_H265_PARSER_OK)
return get_status (result);
priv->parser_state |= GST_H265_VIDEO_STATE_GOT_VPS;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
parse_sps (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265SPS *const sps = &pi->data.sps;
GstH265ParserResult result;
GST_DEBUG ("parse SPS");
priv->parser_state = 0;
memset (sps, 0, sizeof (GstH265SPS));
result = gst_h265_parser_parse_sps (priv->parser, &pi->nalu, sps, TRUE);
if (result != GST_H265_PARSER_OK)
return get_status (result);
priv->parser_state |= GST_H265_VIDEO_STATE_GOT_SPS;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
parse_pps (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265PPS *const pps = &pi->data.pps;
GstH265ParserResult result;
guint col_width[19], row_height[21];
GST_DEBUG ("parse PPS");
priv->parser_state &= GST_H265_VIDEO_STATE_GOT_SPS;
memset (col_width, 0, sizeof (col_width));
memset (row_height, 0, sizeof (row_height));
memset (pps, 0, sizeof (GstH265PPS));
result = gst_h265_parser_parse_pps (priv->parser, &pi->nalu, pps);
if (result != GST_H265_PARSER_OK)
return get_status (result);
priv->parser_state |= GST_H265_VIDEO_STATE_GOT_PPS;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
parse_sei (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GArray **const sei_ptr = &pi->data.sei;
GstH265ParserResult result;
GST_DEBUG ("parse SEI");
result = gst_h265_parser_parse_sei (priv->parser, &pi->nalu, sei_ptr);
if (result != GST_H265_PARSER_OK) {
GST_WARNING ("failed to parse SEI messages");
return get_status (result);
}
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
parse_slice (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
GstH265ParserResult result;
GST_DEBUG ("parse slice");
priv->parser_state &= (GST_H265_VIDEO_STATE_GOT_SPS |
GST_H265_VIDEO_STATE_GOT_PPS);
slice_hdr->short_term_ref_pic_set_idx = 0;
memset (slice_hdr, 0, sizeof (GstH265SliceHdr));
result = gst_h265_parser_parse_slice_hdr (priv->parser, &pi->nalu, slice_hdr);
if (result != GST_H265_PARSER_OK)
return get_status (result);
priv->parser_state |= GST_H265_VIDEO_STATE_GOT_SLICE;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
decode_vps (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265VPS *const vps = &pi->data.vps;
GST_DEBUG ("decode VPS");
gst_vaapi_parser_info_h265_replace (&priv->vps[vps->id], pi);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
decode_sps (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265SPS *const sps = &pi->data.sps;
guint high_precision_offsets_enabled_flag = 0, bitdepthC = 0;
GST_DEBUG ("decode SPS");
if (sps->max_latency_increase_plus1[sps->max_sub_layers_minus1])
priv->SpsMaxLatencyPictures =
sps->max_num_reorder_pics[sps->max_sub_layers_minus1] +
sps->max_latency_increase_plus1[sps->max_sub_layers_minus1] - 1;
/* Calculate WpOffsetHalfRangeC: (7-34)
* Fixme: We don't have parser API for sps_range_extension, so assuming
* high_precision_offsets_enabled_flag as zero */
bitdepthC = sps->bit_depth_chroma_minus8 + 8;
priv->WpOffsetHalfRangeC =
1 << (high_precision_offsets_enabled_flag ? (bitdepthC - 1) : 7);
gst_vaapi_parser_info_h265_replace (&priv->sps[sps->id], pi);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
decode_pps (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstH265PPS *const pps = &pi->data.pps;
GST_DEBUG ("decode PPS");
gst_vaapi_parser_info_h265_replace (&priv->pps[pps->id], pi);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
decode_sei (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
guint i;
GST_DEBUG ("decode SEI messages");
for (i = 0; i < pi->data.sei->len; i++) {
const GstH265SEIMessage *const sei =
&g_array_index (pi->data.sei, GstH265SEIMessage, i);
switch (sei->payloadType) {
case GST_H265_SEI_PIC_TIMING:{
const GstH265PicTiming *const pic_timing = &sei->payload.pic_timing;
priv->pic_structure = pic_timing->pic_struct;
break;
}
default:
break;
}
}
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static GstVaapiDecoderStatus
decode_sequence_end (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderStatus status;
GST_DEBUG ("decode sequence-end");
status = decode_current_picture (decoder);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
return status;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
/* 8.3.1 - Decoding process for picture order count */
static void
init_picture_poc (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstVaapiParserInfoH265 * pi)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
GstH265SPS *const sps = get_sps (decoder);
const gint32 MaxPicOrderCntLsb =
1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
guint8 nal_type = pi->nalu.type;
guint8 temporal_id = pi->nalu.temporal_id_plus1 - 1;
GST_DEBUG ("decode PicOrderCntVal");
priv->prev_poc_lsb = priv->poc_lsb;
priv->prev_poc_msb = priv->poc_msb;
if (!(nal_is_irap (nal_type) && picture->NoRaslOutputFlag)) {
priv->prev_poc_lsb = priv->prev_tid0pic_poc_lsb;
priv->prev_poc_msb = priv->prev_tid0pic_poc_msb;
}
/* Finding PicOrderCntMsb */
if (nal_is_irap (nal_type) && picture->NoRaslOutputFlag)
priv->poc_msb = 0;
else {
/* (8-1) */
if ((slice_hdr->pic_order_cnt_lsb < priv->prev_poc_lsb) &&
((priv->prev_poc_lsb - slice_hdr->pic_order_cnt_lsb) >=
(MaxPicOrderCntLsb / 2)))
priv->poc_msb = priv->prev_poc_msb + MaxPicOrderCntLsb;
else if ((slice_hdr->pic_order_cnt_lsb > priv->prev_poc_lsb) &&
((slice_hdr->pic_order_cnt_lsb - priv->prev_poc_lsb) >
(MaxPicOrderCntLsb / 2)))
priv->poc_msb = priv->prev_poc_msb - MaxPicOrderCntLsb;
else
priv->poc_msb = priv->prev_poc_msb;
}
/* (8-2) */
priv->poc = picture->poc = priv->poc_msb + slice_hdr->pic_order_cnt_lsb;
priv->poc_lsb = picture->poc_lsb = slice_hdr->pic_order_cnt_lsb;
if (nal_is_idr (nal_type)) {
picture->poc = 0;
picture->poc_lsb = 0;
priv->poc_lsb = 0;
priv->poc_msb = 0;
priv->prev_poc_lsb = 0;
priv->prev_poc_msb = 0;
priv->prev_tid0pic_poc_lsb = 0;
priv->prev_tid0pic_poc_msb = 0;
}
picture->base.poc = picture->poc;
GST_DEBUG ("PicOrderCntVal %d", picture->base.poc);
if (!temporal_id && !nal_is_rasl (nal_type) &&
!nal_is_radl (nal_type) && nal_is_ref (nal_type)) {
priv->prev_tid0pic_poc_lsb = slice_hdr->pic_order_cnt_lsb;
priv->prev_tid0pic_poc_msb = priv->poc_msb;
}
}
static void
init_picture_refs (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstH265SliceHdr * slice_hdr)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
guint32 NumRpsCurrTempList0 = 0, NumRpsCurrTempList1 = 0;
GstVaapiPictureH265 *RefPicListTemp0[16] = { NULL, }, *RefPicListTemp1[16] = {
NULL,};
guint i, rIdx = 0;
guint num_ref_idx_l0_active_minus1 = 0;
guint num_ref_idx_l1_active_minus1 = 0;
GstH265RefPicListModification *ref_pic_list_modification;
guint type;
memset (priv->RefPicList0, 0, sizeof (GstVaapiPictureH265 *) * 16);
memset (priv->RefPicList1, 0, sizeof (GstVaapiPictureH265 *) * 16);
priv->RefPicList0_count = priv->RefPicList1_count = 0;
num_ref_idx_l0_active_minus1 = slice_hdr->num_ref_idx_l0_active_minus1;
num_ref_idx_l1_active_minus1 = slice_hdr->num_ref_idx_l1_active_minus1;
ref_pic_list_modification = &slice_hdr->ref_pic_list_modification;
type = slice_hdr->type;
/* decoding process for reference picture list construction needs to be
* invoked only for P and B slice */
if (type == GST_H265_I_SLICE)
return;
NumRpsCurrTempList0 =
MAX ((num_ref_idx_l0_active_minus1 + 1), priv->NumPocTotalCurr);
NumRpsCurrTempList1 =
MAX ((num_ref_idx_l1_active_minus1 + 1), priv->NumPocTotalCurr);
/* (8-8) */
while (rIdx < NumRpsCurrTempList0) {
for (i = 0; i < priv->NumPocStCurrBefore && rIdx < NumRpsCurrTempList0;
rIdx++, i++)
RefPicListTemp0[rIdx] = priv->RefPicSetStCurrBefore[i];
for (i = 0; i < priv->NumPocStCurrAfter && rIdx < NumRpsCurrTempList0;
rIdx++, i++)
RefPicListTemp0[rIdx] = priv->RefPicSetStCurrAfter[i];
for (i = 0; i < priv->NumPocLtCurr && rIdx < NumRpsCurrTempList0;
rIdx++, i++)
RefPicListTemp0[rIdx] = priv->RefPicSetLtCurr[i];
}
/* construct RefPicList0 (8-9) */
for (rIdx = 0; rIdx <= num_ref_idx_l0_active_minus1; rIdx++)
priv->RefPicList0[rIdx] =
ref_pic_list_modification->ref_pic_list_modification_flag_l0 ?
RefPicListTemp0[ref_pic_list_modification->list_entry_l0[rIdx]] :
RefPicListTemp0[rIdx];
priv->RefPicList0_count = rIdx;
if (type == GST_H265_B_SLICE) {
rIdx = 0;
/* (8-10) */
while (rIdx < NumRpsCurrTempList1) {
for (i = 0; i < priv->NumPocStCurrAfter && rIdx < NumRpsCurrTempList1;
rIdx++, i++)
RefPicListTemp1[rIdx] = priv->RefPicSetStCurrAfter[i];
for (i = 0; i < priv->NumPocStCurrBefore && rIdx < NumRpsCurrTempList1;
rIdx++, i++)
RefPicListTemp1[rIdx] = priv->RefPicSetStCurrBefore[i];
for (i = 0; i < priv->NumPocLtCurr && rIdx < NumRpsCurrTempList1;
rIdx++, i++)
RefPicListTemp1[rIdx] = priv->RefPicSetLtCurr[i];
}
/* construct RefPicList1 (8-10) */
for (rIdx = 0; rIdx <= num_ref_idx_l1_active_minus1; rIdx++)
priv->RefPicList1[rIdx] =
ref_pic_list_modification->ref_pic_list_modification_flag_l1 ?
RefPicListTemp1[ref_pic_list_modification->list_entry_l1
[rIdx]] : RefPicListTemp1[rIdx];
priv->RefPicList1_count = rIdx;
}
}
static gboolean
init_picture (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstVaapiParserInfoH265 * pi)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPicture *const base_picture = &picture->base;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
base_picture->pts = GST_VAAPI_DECODER_CODEC_FRAME (decoder)->pts;
base_picture->type = GST_VAAPI_PICTURE_TYPE_NONE;
if (nal_is_idr (pi->nalu.type)) {
GST_DEBUG ("<IDR>");
GST_VAAPI_PICTURE_FLAG_SET (picture, GST_VAAPI_PICTURE_FLAG_IDR);
}
if (pi->nalu.type >= GST_H265_NAL_SLICE_BLA_W_LP &&
pi->nalu.type <= GST_H265_NAL_SLICE_CRA_NUT)
picture->RapPicFlag = TRUE;
/*Fixme: Use SEI header values */
base_picture->structure = GST_VAAPI_PICTURE_STRUCTURE_FRAME;
picture->structure = base_picture->structure;
/*NoRaslOutputFlag ==1 if the current picture is
1) an IDR picture
2) a BLA picture
3) a CRA picture that is the first access unit in the bitstream
4) first picture that follows an end of sequence NAL unit in decoding order
5) has HandleCraAsBlaFlag == 1 (set by external means, so not considering )
*/
if (nal_is_idr (pi->nalu.type) || nal_is_bla (pi->nalu.type) ||
(nal_is_cra (pi->nalu.type) && priv->new_bitstream)
|| priv->prev_nal_is_eos) {
picture->NoRaslOutputFlag = 1;
}
if (nal_is_irap (pi->nalu.type)) {
picture->IntraPicFlag = TRUE;
priv->associated_irap_NoRaslOutputFlag = picture->NoRaslOutputFlag;
}
if (nal_is_rasl (pi->nalu.type) && priv->associated_irap_NoRaslOutputFlag)
picture->output_flag = FALSE;
else
picture->output_flag = slice_hdr->pic_output_flag;
init_picture_poc (decoder, picture, pi);
return TRUE;
}
static void
vaapi_init_picture (VAPictureHEVC * pic)
{
pic->picture_id = VA_INVALID_SURFACE;
pic->pic_order_cnt = 0;
pic->flags = VA_PICTURE_HEVC_INVALID;
}
static void
vaapi_fill_picture (VAPictureHEVC * pic, GstVaapiPictureH265 * picture,
guint picture_structure)
{
if (!picture_structure)
picture_structure = picture->structure;
pic->picture_id = picture->base.surface_id;
pic->pic_order_cnt = picture->poc;
pic->flags = 0;
/* Set the VAPictureHEVC flags */
if (GST_VAAPI_PICTURE_IS_LONG_TERM_REFERENCE (picture))
pic->flags |= VA_PICTURE_HEVC_LONG_TERM_REFERENCE;
if (GST_VAAPI_PICTURE_FLAG_IS_SET (picture,
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_BEFORE))
pic->flags |= VA_PICTURE_HEVC_RPS_ST_CURR_BEFORE;
else if (GST_VAAPI_PICTURE_FLAG_IS_SET (picture,
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_AFTER))
pic->flags |= VA_PICTURE_HEVC_RPS_ST_CURR_AFTER;
else if (GST_VAAPI_PICTURE_FLAG_IS_SET (picture,
GST_VAAPI_PICTURE_FLAG_RPS_LT_CURR))
pic->flags |= VA_PICTURE_HEVC_RPS_LT_CURR;
switch (picture_structure) {
case GST_VAAPI_PICTURE_STRUCTURE_FRAME:
break;
case GST_VAAPI_PICTURE_STRUCTURE_TOP_FIELD:
pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
break;
case GST_VAAPI_PICTURE_STRUCTURE_BOTTOM_FIELD:
pic->flags |= VA_PICTURE_HEVC_FIELD_PIC;
pic->flags |= VA_PICTURE_HEVC_BOTTOM_FIELD;
break;
default:
break;
}
}
static guint
get_index_for_RefPicListX (VAPictureHEVC * ReferenceFrames,
GstVaapiPictureH265 * pic)
{
gint i;
for (i = 0; i < 15; i++) {
if ((ReferenceFrames[i].picture_id != VA_INVALID_ID) && pic) {
if ((ReferenceFrames[i].pic_order_cnt == pic->poc) &&
(ReferenceFrames[i].picture_id == pic->base.surface_id)) {
return i;
}
}
}
return 0xff;
}
static gboolean
fill_picture (GstVaapiDecoderH265 * decoder, GstVaapiPictureH265 * picture)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPicture *const base_picture = &picture->base;
GstH265PPS *const pps = get_pps (decoder);
GstH265SPS *const sps = get_sps (decoder);
VAPictureParameterBufferHEVC *const pic_param = base_picture->param;
guint i, n;
pic_param->pic_fields.value = 0;
pic_param->slice_parsing_fields.value = 0;
/* Fill in VAPictureHEVC */
vaapi_fill_picture (&pic_param->CurrPic, picture, 0);
/* Fill in ReferenceFrames */
for (i = 0, n = 0; i < priv->dpb_count; i++) {
GstVaapiFrameStore *const fs = priv->dpb[i];
if ((gst_vaapi_frame_store_has_reference (fs)))
vaapi_fill_picture (&pic_param->ReferenceFrames[n++], fs->buffer,
fs->buffer->structure);
if (n >= G_N_ELEMENTS (pic_param->ReferenceFrames))
break;
}
for (; n < G_N_ELEMENTS (pic_param->ReferenceFrames); n++)
vaapi_init_picture (&pic_param->ReferenceFrames[n]);
#define COPY_FIELD(s, f) \
pic_param->f = (s)->f
#define COPY_BFM(a, s, f) \
pic_param->a.bits.f = (s)->f
COPY_FIELD (sps, pic_width_in_luma_samples);
COPY_FIELD (sps, pic_height_in_luma_samples);
COPY_BFM (pic_fields, sps, chroma_format_idc);
COPY_BFM (pic_fields, sps, separate_colour_plane_flag);
COPY_BFM (pic_fields, sps, pcm_enabled_flag);
COPY_BFM (pic_fields, sps, scaling_list_enabled_flag);
COPY_BFM (pic_fields, pps, transform_skip_enabled_flag);
COPY_BFM (pic_fields, sps, amp_enabled_flag);
COPY_BFM (pic_fields, sps, strong_intra_smoothing_enabled_flag);
COPY_BFM (pic_fields, pps, sign_data_hiding_enabled_flag);
COPY_BFM (pic_fields, pps, constrained_intra_pred_flag);
COPY_BFM (pic_fields, pps, cu_qp_delta_enabled_flag);
COPY_BFM (pic_fields, pps, weighted_pred_flag);
COPY_BFM (pic_fields, pps, weighted_bipred_flag);
COPY_BFM (pic_fields, pps, transquant_bypass_enabled_flag);
COPY_BFM (pic_fields, pps, tiles_enabled_flag);
COPY_BFM (pic_fields, pps, entropy_coding_sync_enabled_flag);
pic_param->pic_fields.bits.pps_loop_filter_across_slices_enabled_flag =
pps->loop_filter_across_slices_enabled_flag;
COPY_BFM (pic_fields, pps, loop_filter_across_tiles_enabled_flag);
COPY_BFM (pic_fields, sps, pcm_loop_filter_disabled_flag);
/* Fix: Assign value based on sps_max_num_reorder_pics */
pic_param->pic_fields.bits.NoPicReorderingFlag = 0;
/* Fix: Enable if picture has no B slices */
pic_param->pic_fields.bits.NoBiPredFlag = 0;
pic_param->sps_max_dec_pic_buffering_minus1 =
sps->max_dec_pic_buffering_minus1[0];
COPY_FIELD (sps, bit_depth_luma_minus8);
COPY_FIELD (sps, bit_depth_chroma_minus8);
COPY_FIELD (sps, pcm_sample_bit_depth_luma_minus1);
COPY_FIELD (sps, pcm_sample_bit_depth_chroma_minus1);
COPY_FIELD (sps, log2_min_luma_coding_block_size_minus3);
COPY_FIELD (sps, log2_diff_max_min_luma_coding_block_size);
COPY_FIELD (sps, log2_min_transform_block_size_minus2);
COPY_FIELD (sps, log2_diff_max_min_transform_block_size);
COPY_FIELD (sps, log2_min_pcm_luma_coding_block_size_minus3);
COPY_FIELD (sps, log2_diff_max_min_pcm_luma_coding_block_size);
COPY_FIELD (sps, max_transform_hierarchy_depth_intra);
COPY_FIELD (sps, max_transform_hierarchy_depth_inter);
COPY_FIELD (pps, init_qp_minus26);
COPY_FIELD (pps, diff_cu_qp_delta_depth);
pic_param->pps_cb_qp_offset = pps->cb_qp_offset;
pic_param->pps_cr_qp_offset = pps->cr_qp_offset;
COPY_FIELD (pps, log2_parallel_merge_level_minus2);
COPY_FIELD (pps, num_tile_columns_minus1);
COPY_FIELD (pps, num_tile_rows_minus1);
for (i = 0; i <= pps->num_tile_columns_minus1; i++)
pic_param->column_width_minus1[i] = pps->column_width_minus1[i];
for (; i < 19; i++)
pic_param->column_width_minus1[i] = 0;
for (i = 0; i <= pps->num_tile_rows_minus1; i++)
pic_param->row_height_minus1[i] = pps->row_height_minus1[i];
for (; i < 21; i++)
pic_param->row_height_minus1[i] = 0;
COPY_BFM (slice_parsing_fields, pps, lists_modification_present_flag);
COPY_BFM (slice_parsing_fields, sps, long_term_ref_pics_present_flag);
pic_param->slice_parsing_fields.bits.sps_temporal_mvp_enabled_flag =
sps->temporal_mvp_enabled_flag;
COPY_BFM (slice_parsing_fields, pps, cabac_init_present_flag);
COPY_BFM (slice_parsing_fields, pps, output_flag_present_flag);
COPY_BFM (slice_parsing_fields, pps, dependent_slice_segments_enabled_flag);
pic_param->slice_parsing_fields.bits.
pps_slice_chroma_qp_offsets_present_flag =
pps->slice_chroma_qp_offsets_present_flag;
COPY_BFM (slice_parsing_fields, sps, sample_adaptive_offset_enabled_flag);
COPY_BFM (slice_parsing_fields, pps, deblocking_filter_override_enabled_flag);
pic_param->slice_parsing_fields.bits.pps_disable_deblocking_filter_flag =
pps->deblocking_filter_disabled_flag;
COPY_BFM (slice_parsing_fields, pps,
slice_segment_header_extension_present_flag);
pic_param->slice_parsing_fields.bits.RapPicFlag = picture->RapPicFlag;
pic_param->slice_parsing_fields.bits.IdrPicFlag =
GST_VAAPI_PICTURE_FLAG_IS_SET (picture, GST_VAAPI_PICTURE_FLAG_IDR);
pic_param->slice_parsing_fields.bits.IntraPicFlag = picture->IntraPicFlag;
COPY_FIELD (sps, log2_max_pic_order_cnt_lsb_minus4);
COPY_FIELD (sps, num_short_term_ref_pic_sets);
pic_param->num_long_term_ref_pic_sps = sps->num_long_term_ref_pics_sps;
COPY_FIELD (pps, num_ref_idx_l0_default_active_minus1);
COPY_FIELD (pps, num_ref_idx_l1_default_active_minus1);
pic_param->pps_beta_offset_div2 = pps->beta_offset_div2;
pic_param->pps_tc_offset_div2 = pps->tc_offset_div2;
COPY_FIELD (pps, num_extra_slice_header_bits);
/*Fixme: Set correct value as mentioned in va_dec_hevc.h */
pic_param->st_rps_bits = 0;
return TRUE;
}
/* Detection of the first VCL NAL unit of a coded picture (7.4.2.4.5 ) */
static gboolean
is_new_picture (GstVaapiParserInfoH265 * pi, GstVaapiParserInfoH265 * prev_pi)
{
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
if (!prev_pi)
return TRUE;
if (slice_hdr->first_slice_segment_in_pic_flag)
return TRUE;
return FALSE;
}
/* Detection of a new access unit, assuming we are already in presence
of a new picture */
static inline gboolean
is_new_access_unit (GstVaapiParserInfoH265 * pi,
GstVaapiParserInfoH265 * prev_pi)
{
if (!prev_pi)
return TRUE;
return FALSE;
}
static gboolean
has_entry_in_rps (GstVaapiPictureH265 * dpb_pic,
GstVaapiPictureH265 ** rps_list, guint rps_list_length)
{
guint i;
if (!dpb_pic || !rps_list || !rps_list_length)
return FALSE;
for (i = 0; i < rps_list_length; i++) {
if (rps_list[i] && rps_list[i]->poc == dpb_pic->poc)
return TRUE;
}
return FALSE;
}
/* the derivation process for the RPS and the picture marking */
static void
derive_and_mark_rps (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstVaapiParserInfoH265 * pi,
gint32 * CurrDeltaPocMsbPresentFlag, gint32 * FollDeltaPocMsbPresentFlag)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiPictureH265 *dpb_pic = NULL;
guint i;
memset (priv->RefPicSetLtCurr, 0, sizeof (GstVaapiPictureH265 *) * 16);
memset (priv->RefPicSetLtFoll, 0, sizeof (GstVaapiPictureH265 *) * 16);
memset (priv->RefPicSetStCurrBefore, 0, sizeof (GstVaapiPictureH265 *) * 16);
memset (priv->RefPicSetStCurrAfter, 0, sizeof (GstVaapiPictureH265 *) * 16);
memset (priv->RefPicSetStFoll, 0, sizeof (GstVaapiPictureH265 *) * 16);
/* (8-6) */
for (i = 0; i < priv->NumPocLtCurr; i++) {
if (!CurrDeltaPocMsbPresentFlag[i]) {
dpb_pic = dpb_get_picture (decoder, priv->PocLtCurr[i], TRUE);
if (dpb_pic)
priv->RefPicSetLtCurr[i] = dpb_pic;
else
priv->RefPicSetLtCurr[i] = NULL;
} else {
dpb_pic = dpb_get_picture (decoder, priv->PocLtCurr[i], FALSE);
if (dpb_pic)
priv->RefPicSetLtCurr[i] = dpb_pic;
else
priv->RefPicSetLtCurr[i] = NULL;
}
}
for (; i < 16; i++)
priv->RefPicSetLtCurr[i] = NULL;
for (i = 0; i < priv->NumPocLtFoll; i++) {
if (!FollDeltaPocMsbPresentFlag[i]) {
dpb_pic = dpb_get_picture (decoder, priv->PocLtFoll[i], TRUE);
if (dpb_pic)
priv->RefPicSetLtFoll[i] = dpb_pic;
else
priv->RefPicSetLtFoll[i] = NULL;
} else {
dpb_pic = dpb_get_picture (decoder, priv->PocLtFoll[i], FALSE);
if (dpb_pic)
priv->RefPicSetLtFoll[i] = dpb_pic;
else
priv->RefPicSetLtFoll[i] = NULL;
}
}
for (; i < 16; i++)
priv->RefPicSetLtFoll[i] = NULL;
/* Mark all ref pics in RefPicSetLtCurr and RefPicSetLtFol as long_term_refs */
for (i = 0; i < priv->NumPocLtCurr; i++) {
if (priv->RefPicSetLtCurr[i])
gst_vaapi_picture_h265_set_reference (priv->RefPicSetLtCurr[i],
GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE |
GST_VAAPI_PICTURE_FLAG_RPS_LT_CURR);
}
for (i = 0; i < priv->NumPocLtFoll; i++) {
if (priv->RefPicSetLtFoll[i])
gst_vaapi_picture_h265_set_reference (priv->RefPicSetLtFoll[i],
GST_VAAPI_PICTURE_FLAG_LONG_TERM_REFERENCE |
GST_VAAPI_PICTURE_FLAG_RPS_LT_FOLL);
}
/* (8-7) */
for (i = 0; i < priv->NumPocStCurrBefore; i++) {
dpb_pic = dpb_get_ref_picture (decoder, priv->PocStCurrBefore[i], TRUE);
if (dpb_pic) {
gst_vaapi_picture_h265_set_reference (dpb_pic,
GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE |
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_BEFORE);
priv->RefPicSetStCurrBefore[i] = dpb_pic;
} else
priv->RefPicSetStCurrBefore[i] = NULL;
}
for (; i < 16; i++)
priv->RefPicSetStCurrBefore[i] = NULL;
for (i = 0; i < priv->NumPocStCurrAfter; i++) {
dpb_pic = dpb_get_ref_picture (decoder, priv->PocStCurrAfter[i], TRUE);
if (dpb_pic) {
gst_vaapi_picture_h265_set_reference (dpb_pic,
GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE |
GST_VAAPI_PICTURE_FLAG_RPS_ST_CURR_AFTER);
priv->RefPicSetStCurrAfter[i] = dpb_pic;
} else
priv->RefPicSetStCurrAfter[i] = NULL;
}
for (; i < 16; i++)
priv->RefPicSetStCurrAfter[i] = NULL;
for (i = 0; i < priv->NumPocStFoll; i++) {
dpb_pic = dpb_get_ref_picture (decoder, priv->PocStFoll[i], TRUE);
if (dpb_pic) {
gst_vaapi_picture_h265_set_reference (dpb_pic,
GST_VAAPI_PICTURE_FLAG_SHORT_TERM_REFERENCE |
GST_VAAPI_PICTURE_FLAG_RPS_ST_FOLL);
priv->RefPicSetStFoll[i] = dpb_pic;
} else
priv->RefPicSetStFoll[i] = NULL;
}
for (; i < 16; i++)
priv->RefPicSetStFoll[i] = NULL;
/* Mark all dpb pics not beloging to RefPicSet*[] as unused for ref */
for (i = 0; i < priv->dpb_count; i++) {
dpb_pic = priv->dpb[i]->buffer;
if (dpb_pic &&
!has_entry_in_rps (dpb_pic, priv->RefPicSetLtCurr, priv->NumPocLtCurr)
&& !has_entry_in_rps (dpb_pic, priv->RefPicSetLtFoll,
priv->NumPocLtFoll)
&& !has_entry_in_rps (dpb_pic, priv->RefPicSetStCurrAfter,
priv->NumPocStCurrAfter)
&& !has_entry_in_rps (dpb_pic, priv->RefPicSetStCurrBefore,
priv->NumPocStCurrBefore)
&& !has_entry_in_rps (dpb_pic, priv->RefPicSetStFoll,
priv->NumPocStFoll))
gst_vaapi_picture_h265_set_reference (dpb_pic, 0);
}
}
/* Decoding process for reference picture set (8.3.2) */
static gboolean
decode_ref_pic_set (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstVaapiParserInfoH265 * pi)
{
guint i, j, k;
gint32 CurrDeltaPocMsbPresentFlag[16] = { 0, };
gint32 FollDeltaPocMsbPresentFlag[16] = { 0, };
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
GstH265SPS *const sps = get_sps (decoder);
const gint32 MaxPicOrderCntLsb =
1 << (sps->log2_max_pic_order_cnt_lsb_minus4 + 4);
/* if it is an irap pic, set all ref pics in dpb as unused for ref */
if (nal_is_irap (pi->nalu.type) && picture->NoRaslOutputFlag) {
for (i = 0; i < priv->dpb_count; i++) {
GstVaapiFrameStore *const fs = priv->dpb[i];
gst_vaapi_picture_h265_set_reference (fs->buffer, 0);
}
}
/* Reset everything for IDR */
if (nal_is_idr (pi->nalu.type)) {
memset (priv->PocStCurrBefore, 0, sizeof (guint) * 16);
memset (priv->PocStCurrAfter, 0, sizeof (guint) * 16);
memset (priv->PocStFoll, 0, sizeof (guint) * 16);
memset (priv->PocLtCurr, 0, sizeof (guint) * 16);
memset (priv->PocLtFoll, 0, sizeof (guint) * 16);
priv->NumPocStCurrBefore = priv->NumPocStCurrAfter = priv->NumPocStFoll = 0;
priv->NumPocLtCurr = priv->NumPocLtFoll = 0;
} else {
GstH265ShortTermRefPicSet *stRefPic = NULL;
gint32 num_lt_pics, pocLt, PocLsbLt[16] = { 0, }
, UsedByCurrPicLt[16] = {
0,};
gint32 DeltaPocMsbCycleLt[16] = { 0, };
gint numtotalcurr = 0;
/* this is based on CurrRpsIdx described in spec */
if (!slice_hdr->short_term_ref_pic_set_sps_flag)
stRefPic = &slice_hdr->short_term_ref_pic_sets;
else if (sps->num_short_term_ref_pic_sets)
stRefPic =
&sps->short_term_ref_pic_set[slice_hdr->short_term_ref_pic_set_idx];
g_assert (stRefPic != NULL);
for (i = 0, j = 0, k = 0; i < stRefPic->NumNegativePics; i++) {
if (stRefPic->UsedByCurrPicS0[i]) {
priv->PocStCurrBefore[j++] = picture->poc + stRefPic->DeltaPocS0[i];
numtotalcurr++;
} else
priv->PocStFoll[k++] = picture->poc + stRefPic->DeltaPocS0[i];
}
priv->NumPocStCurrBefore = j;
for (i = 0, j = 0; i < stRefPic->NumPositivePics; i++) {
if (stRefPic->UsedByCurrPicS1[i]) {
priv->PocStCurrAfter[j++] = picture->poc + stRefPic->DeltaPocS1[i];
numtotalcurr++;
} else
priv->PocStFoll[k++] = picture->poc + stRefPic->DeltaPocS1[i];
}
priv->NumPocStCurrAfter = j;
priv->NumPocStFoll = k;
num_lt_pics = slice_hdr->num_long_term_sps + slice_hdr->num_long_term_pics;
/* The variables PocLsbLt[i] and UsedByCurrPicLt[i] are derived as follows: */
for (i = 0; i < num_lt_pics; i++) {
if (i < slice_hdr->num_long_term_sps) {
PocLsbLt[i] = sps->lt_ref_pic_poc_lsb_sps[slice_hdr->lt_idx_sps[i]];
UsedByCurrPicLt[i] =
sps->used_by_curr_pic_lt_sps_flag[slice_hdr->lt_idx_sps[i]];
} else {
PocLsbLt[i] = slice_hdr->poc_lsb_lt[i];
UsedByCurrPicLt[i] = slice_hdr->used_by_curr_pic_lt_flag[i];
}
if (UsedByCurrPicLt[i])
numtotalcurr++;
}
priv->NumPocTotalCurr = numtotalcurr;
/* The variable DeltaPocMsbCycleLt[i] is derived as follows: (7-38) */
for (i = 0; i < num_lt_pics; i++) {
if (i == 0 || i == slice_hdr->num_long_term_sps)
DeltaPocMsbCycleLt[i] = slice_hdr->delta_poc_msb_cycle_lt[i];
else
DeltaPocMsbCycleLt[i] =
slice_hdr->delta_poc_msb_cycle_lt[i] + DeltaPocMsbCycleLt[i - 1];
}
/* (8-5) */
for (i = 0, j = 0, k = 0; i < num_lt_pics; i++) {
pocLt = PocLsbLt[i];
if (slice_hdr->delta_poc_msb_present_flag[i])
pocLt +=
picture->poc - DeltaPocMsbCycleLt[i] * MaxPicOrderCntLsb -
slice_hdr->pic_order_cnt_lsb;
if (UsedByCurrPicLt[i]) {
priv->PocLtCurr[j] = pocLt;
CurrDeltaPocMsbPresentFlag[j++] =
slice_hdr->delta_poc_msb_present_flag[i];
} else {
priv->PocLtFoll[k] = pocLt;
FollDeltaPocMsbPresentFlag[k++] =
slice_hdr->delta_poc_msb_present_flag[i];
}
}
priv->NumPocLtCurr = j;
priv->NumPocLtFoll = k;
}
/* the derivation process for the RPS and the picture marking */
derive_and_mark_rps (decoder, picture, pi, CurrDeltaPocMsbPresentFlag,
FollDeltaPocMsbPresentFlag);
return TRUE;
}
static GstVaapiDecoderStatus
decode_picture (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *pi = unit->parsed_info;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
GstH265PPS *const pps = ensure_pps (decoder, slice_hdr->pps);
GstH265SPS *const sps = ensure_sps (decoder, slice_hdr->pps->sps);
GstVaapiPictureH265 *picture;
GstVaapiDecoderStatus status;
g_return_val_if_fail (pps != NULL, GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN);
g_return_val_if_fail (sps != NULL, GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN);
status = ensure_context (decoder, sps);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
return status;
priv->decoder_state = 0;
/* Create new picture */
picture = gst_vaapi_picture_h265_new (decoder);
if (!picture) {
GST_ERROR ("failed to allocate picture");
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
}
gst_vaapi_picture_replace (&priv->current_picture, picture);
gst_vaapi_picture_unref (picture);
/* Update cropping rectangle */
if (sps->conformance_window_flag) {
GstVaapiRectangle crop_rect;
crop_rect.x = sps->crop_rect_x;
crop_rect.y = sps->crop_rect_y;
crop_rect.width = sps->crop_rect_width;
crop_rect.height = sps->crop_rect_height;
gst_vaapi_picture_set_crop_rect (&picture->base, &crop_rect);
}
status = ensure_quant_matrix (decoder, picture);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS) {
GST_ERROR ("failed to reset quantizer matrix");
return status;
}
if (!init_picture (decoder, picture, pi))
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
/* Drop all RASL pictures having NoRaslOutputFlag is TRUE for the
* associated IRAP picture */
if (nal_is_rasl (pi->nalu.type) && priv->associated_irap_NoRaslOutputFlag) {
gst_vaapi_picture_replace (&priv->current_picture, NULL);
return (GstVaapiDecoderStatus) GST_VAAPI_DECODER_STATUS_DROP_FRAME;
}
if (!decode_ref_pic_set (decoder, picture, pi))
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
if (!dpb_init (decoder, picture, pi))
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
if (!fill_picture (decoder, picture))
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
priv->decoder_state = pi->state;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static inline guint
get_slice_data_byte_offset (GstH265SliceHdr * slice_hdr, guint nal_header_bytes)
{
guint epb_count;
epb_count = slice_hdr->n_emulation_prevention_bytes;
return nal_header_bytes + (slice_hdr->header_size + 7) / 8 - epb_count;
}
static gboolean
fill_pred_weight_table (GstVaapiDecoderH265 * decoder,
GstVaapiSlice * slice, GstH265SliceHdr * slice_hdr)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
VASliceParameterBufferHEVC *const slice_param = slice->param;
GstH265PPS *const pps = get_pps (decoder);
GstH265SPS *const sps = get_sps (decoder);
GstH265PredWeightTable *const w = &slice_hdr->pred_weight_table;
gint chroma_weight, chroma_log2_weight_denom;
gint i, j;
slice_param->luma_log2_weight_denom = 0;
slice_param->delta_chroma_log2_weight_denom = 0;
if ((pps->weighted_pred_flag && GST_H265_IS_P_SLICE (slice_hdr)) ||
(pps->weighted_bipred_flag && GST_H265_IS_B_SLICE (slice_hdr))) {
/* Fixme: This should be done in parser apis */
memset (slice_param->delta_luma_weight_l0, 0,
sizeof (slice_param->delta_luma_weight_l0));
memset (slice_param->luma_offset_l0, 0,
sizeof (slice_param->luma_offset_l0));
memset (slice_param->delta_luma_weight_l1, 0,
sizeof (slice_param->delta_luma_weight_l1));
memset (slice_param->luma_offset_l1, 0,
sizeof (slice_param->luma_offset_l1));
memset (slice_param->delta_chroma_weight_l0, 0,
sizeof (slice_param->delta_chroma_weight_l0));
memset (slice_param->ChromaOffsetL0, 0,
sizeof (slice_param->ChromaOffsetL0));
memset (slice_param->delta_chroma_weight_l1, 0,
sizeof (slice_param->delta_chroma_weight_l1));
memset (slice_param->ChromaOffsetL1, 0,
sizeof (slice_param->ChromaOffsetL1));
slice_param->luma_log2_weight_denom = w->luma_log2_weight_denom;
if (sps->chroma_array_type != 0)
slice_param->delta_chroma_log2_weight_denom =
w->delta_chroma_log2_weight_denom;
chroma_log2_weight_denom =
slice_param->luma_log2_weight_denom +
slice_param->delta_chroma_log2_weight_denom;
for (i = 0; i <= slice_param->num_ref_idx_l0_active_minus1; i++) {
if (slice_hdr->pred_weight_table.luma_weight_l0_flag[i]) {
slice_param->delta_luma_weight_l0[i] = w->delta_luma_weight_l0[i];
slice_param->luma_offset_l0[i] = w->luma_offset_l0[i];
}
if (slice_hdr->pred_weight_table.chroma_weight_l0_flag[i]) {
for (j = 0; j < 2; j++) {
slice_param->delta_chroma_weight_l0[i][j] =
w->delta_chroma_weight_l0[i][j];
/* Find ChromaWeightL0 */
chroma_weight =
(1 << chroma_log2_weight_denom) + w->delta_chroma_weight_l0[i][j];
/* 7-56 */
slice_param->ChromaOffsetL0[i][j] = CLAMP (
(priv->WpOffsetHalfRangeC + w->delta_chroma_offset_l0[i][j] -
((priv->WpOffsetHalfRangeC *
chroma_weight) >> chroma_log2_weight_denom)),
-priv->WpOffsetHalfRangeC, priv->WpOffsetHalfRangeC - 1);
}
}
}
if (GST_H265_IS_B_SLICE (slice_hdr)) {
for (i = 0; i <= slice_param->num_ref_idx_l1_active_minus1; i++) {
if (slice_hdr->pred_weight_table.luma_weight_l1_flag[i]) {
slice_param->delta_luma_weight_l1[i] = w->delta_luma_weight_l1[i];
slice_param->luma_offset_l1[i] = w->luma_offset_l1[i];
}
if (slice_hdr->pred_weight_table.chroma_weight_l1_flag[i]) {
for (j = 0; j < 2; j++) {
slice_param->delta_chroma_weight_l1[i][j] =
w->delta_chroma_weight_l1[i][j];
/* Find ChromaWeightL1 */
chroma_weight =
(1 << chroma_log2_weight_denom) +
w->delta_chroma_weight_l1[i][j];
/* 7-56 */
slice_param->ChromaOffsetL1[i][j] =
CLAMP ((priv->WpOffsetHalfRangeC +
w->delta_chroma_offset_l1[i][j] -
((priv->WpOffsetHalfRangeC *
chroma_weight) >> chroma_log2_weight_denom)),
-priv->WpOffsetHalfRangeC, priv->WpOffsetHalfRangeC - 1);
}
}
}
}
}
return TRUE;
}
static gboolean
fill_RefPicList (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstVaapiSlice * slice,
GstH265SliceHdr * slice_hdr)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
VASliceParameterBufferHEVC *const slice_param = slice->param;
GstVaapiPicture *const base_picture = &picture->base;
VAPictureParameterBufferHEVC *const pic_param = base_picture->param;
guint i, num_ref_lists = 0, j;
slice_param->num_ref_idx_l0_active_minus1 = 0;
slice_param->num_ref_idx_l1_active_minus1 = 0;
for (j = 0; j < 2; j++)
for (i = 0; i < 15; i++)
slice_param->RefPicList[j][i] = 0xFF;
if (GST_H265_IS_B_SLICE (slice_hdr))
num_ref_lists = 2;
else if (GST_H265_IS_I_SLICE (slice_hdr))
num_ref_lists = 0;
else
num_ref_lists = 1;
if (num_ref_lists < 1)
return TRUE;
slice_param->num_ref_idx_l0_active_minus1 =
slice_hdr->num_ref_idx_l0_active_minus1;
slice_param->num_ref_idx_l1_active_minus1 =
slice_hdr->num_ref_idx_l1_active_minus1;
for (i = 0; i < priv->RefPicList0_count; i++)
slice_param->RefPicList[0][i] =
get_index_for_RefPicListX (pic_param->ReferenceFrames,
priv->RefPicList0[i]);
for (; i < 15; i++)
slice_param->RefPicList[0][i] = 0xFF;
if (num_ref_lists < 2)
return TRUE;
for (i = 0; i < priv->RefPicList1_count; i++)
slice_param->RefPicList[1][i] =
get_index_for_RefPicListX (pic_param->ReferenceFrames,
priv->RefPicList1[i]);
for (; i < 15; i++)
slice_param->RefPicList[1][i] = 0xFF;
return TRUE;
}
static gboolean
fill_slice (GstVaapiDecoderH265 * decoder,
GstVaapiPictureH265 * picture, GstVaapiSlice * slice,
GstVaapiParserInfoH265 * pi, GstVaapiDecoderUnit * unit)
{
VASliceParameterBufferHEVC *const slice_param = slice->param;
GstH265SliceHdr *slice_hdr = &pi->data.slice_hdr;
/* Fill in VASliceParameterBufferH265 */
slice_param->LongSliceFlags.value = 0;
slice_param->slice_data_byte_offset =
get_slice_data_byte_offset (slice_hdr, pi->nalu.header_bytes);
slice_param->slice_segment_address = slice_hdr->segment_address;
#define COPY_LFF(f) \
slice_param->LongSliceFlags.fields.f = (slice_hdr)->f
if (GST_VAAPI_PICTURE_FLAG_IS_SET (picture, GST_VAAPI_PICTURE_FLAG_AU_END))
slice_param->LongSliceFlags.fields.LastSliceOfPic = 1;
else
slice_param->LongSliceFlags.fields.LastSliceOfPic = 0;
COPY_LFF (dependent_slice_segment_flag);
COPY_LFF (mvd_l1_zero_flag);
COPY_LFF (cabac_init_flag);
COPY_LFF (collocated_from_l0_flag);
slice_param->LongSliceFlags.fields.color_plane_id =
slice_hdr->colour_plane_id;
slice_param->LongSliceFlags.fields.slice_type = slice_hdr->type;
slice_param->LongSliceFlags.fields.slice_sao_luma_flag =
slice_hdr->sao_luma_flag;
slice_param->LongSliceFlags.fields.slice_sao_chroma_flag =
slice_hdr->sao_chroma_flag;
slice_param->LongSliceFlags.fields.slice_temporal_mvp_enabled_flag =
slice_hdr->temporal_mvp_enabled_flag;
slice_param->LongSliceFlags.fields.slice_deblocking_filter_disabled_flag =
slice_hdr->deblocking_filter_disabled_flag;
slice_param->LongSliceFlags.fields.
slice_loop_filter_across_slices_enabled_flag =
slice_hdr->loop_filter_across_slices_enabled_flag;
if (!slice_hdr->temporal_mvp_enabled_flag)
slice_param->collocated_ref_idx = 0xFF;
else
slice_param->collocated_ref_idx = slice_hdr->collocated_ref_idx;
slice_param->num_ref_idx_l0_active_minus1 =
slice_hdr->num_ref_idx_l0_active_minus1;
slice_param->num_ref_idx_l1_active_minus1 =
slice_hdr->num_ref_idx_l1_active_minus1;
slice_param->slice_qp_delta = slice_hdr->qp_delta;
slice_param->slice_cb_qp_offset = slice_hdr->cb_qp_offset;
slice_param->slice_cr_qp_offset = slice_hdr->cr_qp_offset;
slice_param->slice_beta_offset_div2 = slice_hdr->beta_offset_div2;
slice_param->slice_tc_offset_div2 = slice_hdr->tc_offset_div2;
slice_param->five_minus_max_num_merge_cand =
slice_hdr->five_minus_max_num_merge_cand;
if (!fill_RefPicList (decoder, picture, slice, slice_hdr))
return FALSE;
if (!fill_pred_weight_table (decoder, slice, slice_hdr))
return FALSE;
return TRUE;
}
static GstVaapiDecoderStatus
decode_slice (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstVaapiPictureH265 *const picture = priv->current_picture;
GstH265SliceHdr *const slice_hdr = &pi->data.slice_hdr;
GstVaapiSlice *slice;
GstBuffer *const buffer =
GST_VAAPI_DECODER_CODEC_FRAME (decoder)->input_buffer;
GstMapInfo map_info;
GST_DEBUG ("slice (%u bytes)", pi->nalu.size);
if (!is_valid_state (pi->state, GST_H265_VIDEO_STATE_VALID_PICTURE_HEADERS)) {
GST_WARNING ("failed to receive enough headers to decode slice");
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
if (!ensure_pps (decoder, slice_hdr->pps)) {
GST_ERROR ("failed to activate PPS");
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
}
if (!ensure_sps (decoder, slice_hdr->pps->sps)) {
GST_ERROR ("failed to activate SPS");
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
}
if (!gst_buffer_map (buffer, &map_info, GST_MAP_READ)) {
GST_ERROR ("failed to map buffer");
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
}
/* Check wether this is the first/last slice in the current access unit */
if (pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_START)
GST_VAAPI_PICTURE_FLAG_SET (picture, GST_VAAPI_PICTURE_FLAG_AU_START);
if (pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_END)
GST_VAAPI_PICTURE_FLAG_SET (picture, GST_VAAPI_PICTURE_FLAG_AU_END);
slice = GST_VAAPI_SLICE_NEW (HEVC, decoder,
(map_info.data + unit->offset + pi->nalu.offset), pi->nalu.size);
gst_buffer_unmap (buffer, &map_info);
if (!slice) {
GST_ERROR ("failed to allocate slice");
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
}
init_picture_refs (decoder, picture, slice_hdr);
if (!fill_slice (decoder, picture, slice, pi, unit)) {
gst_vaapi_mini_object_unref (GST_VAAPI_MINI_OBJECT (slice));
return GST_VAAPI_DECODER_STATUS_ERROR_UNKNOWN;
}
gst_vaapi_picture_add_slice (GST_VAAPI_PICTURE_CAST (picture), slice);
picture->last_slice_hdr = slice_hdr;
priv->decoder_state |= GST_H265_VIDEO_STATE_GOT_SLICE;
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static inline gint
scan_for_start_code (GstAdapter * adapter, guint ofs, guint size, guint32 * scp)
{
return (gint) gst_adapter_masked_scan_uint32_peek (adapter,
0xffffff00, 0x00000100, ofs, size, scp);
}
static GstVaapiDecoderStatus
decode_unit (GstVaapiDecoderH265 * decoder, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserInfoH265 *const pi = unit->parsed_info;
GstVaapiDecoderStatus status;
priv->decoder_state |= pi->state;
switch (pi->nalu.type) {
case GST_H265_NAL_VPS:
status = decode_vps (decoder, unit);
break;
case GST_H265_NAL_SPS:
status = decode_sps (decoder, unit);
break;
case GST_H265_NAL_PPS:
status = decode_pps (decoder, unit);
break;
case GST_H265_NAL_SLICE_TRAIL_N:
case GST_H265_NAL_SLICE_TRAIL_R:
case GST_H265_NAL_SLICE_TSA_N:
case GST_H265_NAL_SLICE_TSA_R:
case GST_H265_NAL_SLICE_STSA_N:
case GST_H265_NAL_SLICE_STSA_R:
case GST_H265_NAL_SLICE_RADL_N:
case GST_H265_NAL_SLICE_RADL_R:
case GST_H265_NAL_SLICE_RASL_N:
case GST_H265_NAL_SLICE_RASL_R:
case GST_H265_NAL_SLICE_BLA_W_LP:
case GST_H265_NAL_SLICE_BLA_W_RADL:
case GST_H265_NAL_SLICE_BLA_N_LP:
case GST_H265_NAL_SLICE_IDR_W_RADL:
case GST_H265_NAL_SLICE_IDR_N_LP:
case GST_H265_NAL_SLICE_CRA_NUT:
/* slice decoding will get started only after completing all the
initialization routines for each picture which is hanlding in
start_frame() call back, so the new_bitstream and prev_nal_is_eos
flags will have effects starting from the next frame onwards */
priv->new_bitstream = FALSE;
priv->prev_nal_is_eos = FALSE;
status = decode_slice (decoder, unit);
break;
case GST_H265_NAL_EOB:
priv->new_bitstream = TRUE;
GST_DEBUG
("Next AU(if there is any) will be the begining of new bitstream");
status = decode_sequence_end (decoder);
break;
case GST_H265_NAL_EOS:
priv->prev_nal_is_eos = TRUE;
status = decode_sequence_end (decoder);
break;
case GST_H265_NAL_SUFFIX_SEI:
case GST_H265_NAL_PREFIX_SEI:
status = decode_sei (decoder, unit);
break;
default:
GST_WARNING ("unsupported NAL unit type %d", pi->nalu.type);
status = GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
break;
}
return status;
}
static GstVaapiDecoderStatus
gst_vaapi_decoder_h265_decode_codec_data (GstVaapiDecoder *
base_decoder, const guchar * buf, guint buf_size)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiDecoderStatus status;
GstVaapiDecoderUnit unit;
GstVaapiParserInfoH265 *pi = NULL;
GstH265ParserResult result;
guint num_nal_arrays, num_nals;
guint i, j, ofs;
unit.parsed_info = NULL;
if (buf_size < 23)
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
if (buf[0] != 1) {
GST_ERROR ("failed to decode codec-data, not in hvcC format");
return GST_VAAPI_DECODER_STATUS_ERROR_BITSTREAM_PARSER;
}
priv->nal_length_size = (buf[21] & 0x03) + 1;
GST_DEBUG ("nal length size %u", priv->nal_length_size);
num_nal_arrays = buf[22];
ofs = 23;
for (i = 0; i < num_nal_arrays; i++) {
num_nals = GST_READ_UINT16_BE (buf + ofs + 1);
for (j = 0; j < num_nals; j++) {
pi = gst_vaapi_parser_info_h265_new ();
if (!pi)
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
unit.parsed_info = pi;
result = gst_h265_parser_identify_nalu_hevc (priv->parser,
buf, ofs + 3, buf_size, 2, &pi->nalu);
if (result != GST_H265_PARSER_OK) {
status = get_status (result);
goto cleanup;
}
switch (pi->nalu.type) {
case GST_H265_NAL_VPS:
status = parse_vps (decoder, &unit);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto cleanup;
status = decode_vps (decoder, &unit);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto cleanup;
break;
case GST_H265_NAL_SPS:
status = parse_sps (decoder, &unit);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto cleanup;
status = decode_sps (decoder, &unit);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto cleanup;
break;
case GST_H265_NAL_PPS:
status = parse_pps (decoder, &unit);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto cleanup;
status = decode_pps (decoder, &unit);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto cleanup;
break;
}
gst_vaapi_parser_info_h265_replace (&pi, NULL);
}
}
priv->is_hvcC = TRUE;
status = GST_VAAPI_DECODER_STATUS_SUCCESS;
cleanup:
gst_vaapi_parser_info_h265_replace (&pi, NULL);
return status;
}
static GstVaapiDecoderStatus
ensure_decoder (GstVaapiDecoderH265 * decoder)
{
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiDecoderStatus status;
if (!priv->is_opened) {
priv->is_opened = gst_vaapi_decoder_h265_open (decoder);
if (!priv->is_opened)
return GST_VAAPI_DECODER_STATUS_ERROR_UNSUPPORTED_CODEC;
status =
gst_vaapi_decoder_decode_codec_data (GST_VAAPI_DECODER_CAST (decoder));
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
return status;
}
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static void
populate_dependent_slice_hdr (GstVaapiParserInfoH265 * pi,
GstVaapiParserInfoH265 * indep_pi)
{
GstH265SliceHdr *slice_hdr = &pi->data.slice_hdr;
GstH265SliceHdr *indep_slice_hdr = &indep_pi->data.slice_hdr;
memcpy (&slice_hdr->type, &indep_slice_hdr->type,
offsetof (GstH265SliceHdr, num_entry_point_offsets) -
offsetof (GstH265SliceHdr, type));
}
static GstVaapiDecoderStatus
gst_vaapi_decoder_h265_parse (GstVaapiDecoder * base_decoder,
GstAdapter * adapter, gboolean at_eos, GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
GstVaapiDecoderH265Private *const priv = &decoder->priv;
GstVaapiParserState *const ps = GST_VAAPI_PARSER_STATE (base_decoder);
GstVaapiParserInfoH265 *pi;
GstVaapiDecoderStatus status;
GstH265ParserResult result;
guchar *buf;
guint i, size, buf_size, nalu_size, flags;
guint32 start_code;
gint ofs, ofs2;
gboolean at_au_end = FALSE;
status = ensure_decoder (decoder);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
return status;
switch (priv->stream_alignment) {
case GST_VAAPI_STREAM_ALIGN_H265_NALU:
case GST_VAAPI_STREAM_ALIGN_H265_AU:
size = gst_adapter_available_fast (adapter);
break;
default:
size = gst_adapter_available (adapter);
break;
}
if (priv->is_hvcC) {
if (size < priv->nal_length_size)
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
buf = (guchar *) & start_code;
g_assert (priv->nal_length_size <= sizeof (start_code));
gst_adapter_copy (adapter, buf, 0, priv->nal_length_size);
nalu_size = 0;
for (i = 0; i < priv->nal_length_size; i++)
nalu_size = (nalu_size << 8) | buf[i];
buf_size = priv->nal_length_size + nalu_size;
if (size < buf_size)
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
else if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H265_AU)
at_au_end = (buf_size == size);
} else {
if (size < 4)
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H265_NALU) {
buf_size = size;
ofs = scan_for_start_code (adapter, 4, size - 4, NULL);
if (ofs > 0)
buf_size = ofs;
} else {
ofs = scan_for_start_code (adapter, 0, size, NULL);
if (ofs < 0)
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
if (ofs > 0) {
gst_adapter_flush (adapter, ofs);
size -= ofs;
}
ofs2 = ps->input_offset2 - ofs - 4;
if (ofs2 < 4)
ofs2 = 4;
ofs = G_UNLIKELY (size < ofs2 + 4) ? -1 :
scan_for_start_code (adapter, ofs2, size - ofs2, NULL);
if (ofs < 0) {
// Assume the whole NAL unit is present if end-of-stream
// or stream buffers aligned on access unit boundaries
if (priv->stream_alignment == GST_VAAPI_STREAM_ALIGN_H265_AU)
at_au_end = TRUE;
else if (!at_eos) {
ps->input_offset2 = size;
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
}
ofs = size;
}
buf_size = ofs;
}
}
ps->input_offset2 = 0;
buf = (guchar *) gst_adapter_map (adapter, buf_size);
if (!buf)
return GST_VAAPI_DECODER_STATUS_ERROR_NO_DATA;
unit->size = buf_size;
pi = gst_vaapi_parser_info_h265_new ();
if (!pi)
return GST_VAAPI_DECODER_STATUS_ERROR_ALLOCATION_FAILED;
gst_vaapi_decoder_unit_set_parsed_info (unit,
pi, (GDestroyNotify) gst_vaapi_mini_object_unref);
if (priv->is_hvcC)
result = gst_h265_parser_identify_nalu_hevc (priv->parser,
buf, 0, buf_size, priv->nal_length_size, &pi->nalu);
else
result = gst_h265_parser_identify_nalu_unchecked (priv->parser,
buf, 0, buf_size, &pi->nalu);
status = get_status (result);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto exit;
switch (pi->nalu.type) {
case GST_H265_NAL_VPS:
status = parse_vps (decoder, unit);
break;
case GST_H265_NAL_SPS:
status = parse_sps (decoder, unit);
break;
case GST_H265_NAL_PPS:
status = parse_pps (decoder, unit);
break;
case GST_H265_NAL_PREFIX_SEI:
case GST_H265_NAL_SUFFIX_SEI:
status = parse_sei (decoder, unit);
break;
case GST_H265_NAL_SLICE_TRAIL_N:
case GST_H265_NAL_SLICE_TRAIL_R:
case GST_H265_NAL_SLICE_TSA_N:
case GST_H265_NAL_SLICE_TSA_R:
case GST_H265_NAL_SLICE_STSA_N:
case GST_H265_NAL_SLICE_STSA_R:
case GST_H265_NAL_SLICE_RADL_N:
case GST_H265_NAL_SLICE_RADL_R:
case GST_H265_NAL_SLICE_RASL_N:
case GST_H265_NAL_SLICE_RASL_R:
case GST_H265_NAL_SLICE_BLA_W_LP:
case GST_H265_NAL_SLICE_BLA_W_RADL:
case GST_H265_NAL_SLICE_BLA_N_LP:
case GST_H265_NAL_SLICE_IDR_W_RADL:
case GST_H265_NAL_SLICE_IDR_N_LP:
case GST_H265_NAL_SLICE_CRA_NUT:
status = parse_slice (decoder, unit);
break;
default:
status = GST_VAAPI_DECODER_STATUS_SUCCESS;
break;
}
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
goto exit;
flags = 0;
if (at_au_end) {
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_END |
GST_VAAPI_DECODER_UNIT_FLAG_AU_END;
}
switch (pi->nalu.type) {
case GST_H265_NAL_AUD:
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
/* fall-through */
case GST_H265_NAL_FD:
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SKIP;
break;
case GST_H265_NAL_EOB:
flags |= GST_VAAPI_DECODER_UNIT_FLAG_STREAM_END;
/* fall-through */
case GST_H265_NAL_SUFFIX_SEI:
case GST_H265_NAL_EOS:
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_END;
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_END;
break;
case GST_H265_NAL_VPS:
case GST_H265_NAL_SPS:
case GST_H265_NAL_PPS:
case GST_H265_NAL_PREFIX_SEI:
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
break;
case GST_H265_NAL_SLICE_TRAIL_N:
case GST_H265_NAL_SLICE_TRAIL_R:
case GST_H265_NAL_SLICE_TSA_N:
case GST_H265_NAL_SLICE_TSA_R:
case GST_H265_NAL_SLICE_STSA_N:
case GST_H265_NAL_SLICE_STSA_R:
case GST_H265_NAL_SLICE_RADL_N:
case GST_H265_NAL_SLICE_RADL_R:
case GST_H265_NAL_SLICE_RASL_N:
case GST_H265_NAL_SLICE_RASL_R:
case GST_H265_NAL_SLICE_BLA_W_LP:
case GST_H265_NAL_SLICE_BLA_W_RADL:
case GST_H265_NAL_SLICE_BLA_N_LP:
case GST_H265_NAL_SLICE_IDR_W_RADL:
case GST_H265_NAL_SLICE_IDR_N_LP:
case GST_H265_NAL_SLICE_CRA_NUT:
flags |= GST_VAAPI_DECODER_UNIT_FLAG_SLICE;
if (priv->prev_pi &&
(priv->prev_pi->flags & GST_VAAPI_DECODER_UNIT_FLAG_AU_END)) {
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START |
GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
} else if (is_new_picture (pi, priv->prev_slice_pi)) {
flags |= GST_VAAPI_DECODER_UNIT_FLAG_FRAME_START;
if (is_new_access_unit (pi, priv->prev_slice_pi))
flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_START;
}
gst_vaapi_parser_info_h265_replace (&priv->prev_slice_pi, pi);
if (!pi->data.slice_hdr.dependent_slice_segment_flag)
gst_vaapi_parser_info_h265_replace (&priv->prev_independent_slice_pi,
pi);
else
populate_dependent_slice_hdr (pi, priv->prev_independent_slice_pi);
break;
default:
/* Fix */
break;
}
if ((flags & GST_VAAPI_DECODER_UNIT_FLAGS_AU) && priv->prev_slice_pi)
priv->prev_slice_pi->flags |= GST_VAAPI_DECODER_UNIT_FLAG_AU_END;
GST_VAAPI_DECODER_UNIT_FLAG_SET (unit, flags);
pi->nalu.data = NULL;
pi->state = priv->parser_state;
pi->flags = flags;
gst_vaapi_parser_info_h265_replace (&priv->prev_pi, pi);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
exit:
gst_vaapi_parser_info_h265_unref (pi);
return status;
}
static GstVaapiDecoderStatus
gst_vaapi_decoder_h265_decode (GstVaapiDecoder * base_decoder,
GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
GstVaapiDecoderStatus status;
status = ensure_decoder (decoder);
if (status != GST_VAAPI_DECODER_STATUS_SUCCESS)
return status;
return decode_unit (decoder, unit);
}
static GstVaapiDecoderStatus
gst_vaapi_decoder_h265_start_frame (GstVaapiDecoder * base_decoder,
GstVaapiDecoderUnit * unit)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
return decode_picture (decoder, unit);
}
static GstVaapiDecoderStatus
gst_vaapi_decoder_h265_end_frame (GstVaapiDecoder * base_decoder)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
return decode_current_picture (decoder);
}
static GstVaapiDecoderStatus
gst_vaapi_decoder_h265_flush (GstVaapiDecoder * base_decoder)
{
GstVaapiDecoderH265 *const decoder =
GST_VAAPI_DECODER_H265_CAST (base_decoder);
dpb_flush (decoder);
return GST_VAAPI_DECODER_STATUS_SUCCESS;
}
static void
gst_vaapi_decoder_h265_class_init (GstVaapiDecoderH265Class * klass)
{
GstVaapiMiniObjectClass *const object_class =
GST_VAAPI_MINI_OBJECT_CLASS (klass);
GstVaapiDecoderClass *const decoder_class = GST_VAAPI_DECODER_CLASS (klass);
object_class->size = sizeof (GstVaapiDecoderH265);
object_class->finalize = (GDestroyNotify) gst_vaapi_decoder_finalize;
decoder_class->create = gst_vaapi_decoder_h265_create;
decoder_class->destroy = gst_vaapi_decoder_h265_destroy;
decoder_class->parse = gst_vaapi_decoder_h265_parse;
decoder_class->decode = gst_vaapi_decoder_h265_decode;
decoder_class->start_frame = gst_vaapi_decoder_h265_start_frame;
decoder_class->end_frame = gst_vaapi_decoder_h265_end_frame;
decoder_class->flush = gst_vaapi_decoder_h265_flush;
decoder_class->decode_codec_data = gst_vaapi_decoder_h265_decode_codec_data;
}
static inline const GstVaapiDecoderClass *
gst_vaapi_decoder_h265_class (void)
{
static GstVaapiDecoderH265Class g_class;
static gsize g_class_init = FALSE;
if (g_once_init_enter (&g_class_init)) {
gst_vaapi_decoder_h265_class_init (&g_class);
g_once_init_leave (&g_class_init, TRUE);
}
return GST_VAAPI_DECODER_CLASS (&g_class);
}
/**
* gst_vaapi_decoder_h265_set_alignment:
* @decoder: a #GstVaapiDecoderH265
* @alignment: the #GstVaapiStreamAlignH265
*
* Specifies how stream buffers are aligned / fed, i.e. the boundaries
* of each buffer that is supplied to the decoder. This could be no
* specific alignment, NAL unit boundaries, or access unit boundaries.
*/
void
gst_vaapi_decoder_h265_set_alignment (GstVaapiDecoderH265 * decoder,
GstVaapiStreamAlignH265 alignment)
{
g_return_if_fail (decoder != NULL);
decoder->priv.stream_alignment = alignment;
}
/**
* gst_vaapi_decoder_h265_new:
* @display: a #GstVaapiDisplay
* @caps: a #GstCaps holding codec information
*
* Creates a new #GstVaapiDecoder for MPEG-2 decoding. The @caps can
* hold extra information like codec-data and pictured coded size.
*
* Return value: the newly allocated #GstVaapiDecoder object
*/
GstVaapiDecoder *
gst_vaapi_decoder_h265_new (GstVaapiDisplay * display, GstCaps * caps)
{
return gst_vaapi_decoder_new (gst_vaapi_decoder_h265_class (), display, caps);
}
|