summarylogtreecommitdiffstats
path: root/0002-rkmpp-4.patch
blob: 1074b9834663c42b71d0e0fccf3ba538b960206a (plain)
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
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
From d9e3c95ad3ea5efeff5245b77e6ddad6175bc61d Mon Sep 17 00:00:00 2001
From: rigaya <rigaya34589@live.jp>
Date: Fri, 7 Apr 2023 22:45:51 +0900
Subject: [PATCH 01/13] avdevice/v4l2: add v4l2 multi-planar API support

---
 libavdevice/v4l2.c | 173 ++++++++++++++++++++++++++++++++-------------
 1 file changed, 124 insertions(+), 49 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index 365bacd771..e194c1df24 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -75,14 +75,16 @@ struct video_data {
     int frame_size;
     int interlaced;
     int top_field_first;
+    int multi_planer;
     int ts_mode;
     TimeFilter *timefilter;
     int64_t last_time_m;
 
     int buffers;
     atomic_int buffers_queued;
-    void **buf_start;
-    unsigned int *buf_len;
+    int plane_count;
+    void ***buf_start;
+    unsigned int **buf_len;
     char *standard;
     v4l2_std_id std_id;
     int channel;
@@ -169,11 +171,12 @@ static int device_open(AVFormatContext *ctx, const char* device_path)
     av_log(ctx, AV_LOG_VERBOSE, "fd:%d capabilities:%x\n",
            fd, cap.capabilities);
 
-    if (!(cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)) {
+    if (!(cap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE))) {
         av_log(ctx, AV_LOG_ERROR, "Not a video capture device.\n");
         err = AVERROR(ENODEV);
         goto fail;
     }
+    s->multi_planer = ((cap.capabilities & (V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_CAPTURE_MPLANE)) == V4L2_CAP_VIDEO_CAPTURE_MPLANE) ? 1 : 0;
 
     if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
         av_log(ctx, AV_LOG_ERROR,
@@ -193,7 +196,7 @@ static int device_init(AVFormatContext *ctx, int *width, int *height,
                        uint32_t pixelformat)
 {
     struct video_data *s = ctx->priv_data;
-    struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
+    struct v4l2_format fmt = { .type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE };
     int res = 0;
 
     fmt.fmt.pix.width = *width;
@@ -275,7 +278,7 @@ static void list_framesizes(AVFormatContext *ctx, uint32_t pixelformat)
 static void list_formats(AVFormatContext *ctx, int type)
 {
     const struct video_data *s = ctx->priv_data;
-    struct v4l2_fmtdesc vfd = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
+    struct v4l2_fmtdesc vfd = { .type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE };
 
     while(!v4l2_ioctl(s->fd, VIDIOC_ENUM_FMT, &vfd)) {
         enum AVCodecID codec_id = ff_fmt_v4l2codec(vfd.pixelformat);
@@ -339,7 +342,7 @@ static int mmap_init(AVFormatContext *ctx)
     int i, res;
     struct video_data *s = ctx->priv_data;
     struct v4l2_requestbuffers req = {
-        .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
+        .type   = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE,
         .count  = desired_video_buffers,
         .memory = V4L2_MEMORY_MMAP
     };
@@ -355,45 +358,71 @@ static int mmap_init(AVFormatContext *ctx)
         return AVERROR(ENOMEM);
     }
     s->buffers = req.count;
-    s->buf_start = av_malloc_array(s->buffers, sizeof(void *));
+    s->buf_start = av_malloc_array(s->buffers, sizeof(void **));
     if (!s->buf_start) {
         av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
         return AVERROR(ENOMEM);
     }
-    s->buf_len = av_malloc_array(s->buffers, sizeof(unsigned int));
+    s->buf_len = av_malloc_array(s->buffers, sizeof(unsigned int*));
     if (!s->buf_len) {
         av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
         av_freep(&s->buf_start);
         return AVERROR(ENOMEM);
     }
 
+    s->plane_count = 0;
     for (i = 0; i < req.count; i++) {
+        int total_frame_size = 0;
+        int plane_count = 0;
+        struct v4l2_plane planes[VIDEO_MAX_PLANES];
         struct v4l2_buffer buf = {
-            .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
-            .index  = i,
-            .memory = V4L2_MEMORY_MMAP
+            .type     = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE,
+            .index    = i,
+            .memory   = V4L2_MEMORY_MMAP,
+            .m.planes = (s->multi_planer) ? planes : 0,
+            .length   = (s->multi_planer) ? VIDEO_MAX_PLANES : 0
         };
         if (v4l2_ioctl(s->fd, VIDIOC_QUERYBUF, &buf) < 0) {
             res = AVERROR(errno);
             av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_QUERYBUF): %s\n", av_err2str(res));
             return res;
         }
-
-        s->buf_len[i] = buf.length;
-        if (s->frame_size > 0 && s->buf_len[i] < s->frame_size) {
-            av_log(ctx, AV_LOG_ERROR,
-                   "buf_len[%d] = %d < expected frame size %d\n",
-                   i, s->buf_len[i], s->frame_size);
+        plane_count = (s->multi_planer) ? buf.length : 1;
+        if (s->plane_count > 0 && s->plane_count != plane_count) {
+            av_log(ctx, AV_LOG_ERROR, "Plane count differed between buffers\n");
+            return AVERROR(EINVAL);
+        }
+        s->plane_count = plane_count;
+        s->buf_start[i] = av_malloc_array(s->plane_count, sizeof(void *));
+        if (!s->buf_start[i]) {
+            av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer pointers\n");
             return AVERROR(ENOMEM);
         }
-        s->buf_start[i] = v4l2_mmap(NULL, buf.length,
-                               PROT_READ | PROT_WRITE, MAP_SHARED,
-                               s->fd, buf.m.offset);
+        s->buf_len[i] = av_malloc_array(s->plane_count, sizeof(unsigned int*));
+        if (!s->buf_len[i]) {
+            av_log(ctx, AV_LOG_ERROR, "Cannot allocate buffer sizes\n");
+            av_freep(&s->buf_start);
+            return AVERROR(ENOMEM);
+        }
+        for (int iplane = 0; iplane < s->plane_count; iplane++) {
+            s->buf_len[i][iplane] = (s->multi_planer) ? buf.m.planes[iplane].length : buf.length;
+            total_frame_size += s->buf_len[i][iplane];
+            s->buf_start[i][iplane] = v4l2_mmap(NULL, s->buf_len[i][iplane],
+                                PROT_READ | PROT_WRITE, MAP_SHARED,
+                                s->fd, (s->multi_planer) ? buf.m.planes[iplane].m.mem_offset : buf.m.offset);
+
+            if (s->buf_start[i] == MAP_FAILED) {
+                res = AVERROR(errno);
+                av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", av_err2str(res));
+                return res;
+            }
+        }
 
-        if (s->buf_start[i] == MAP_FAILED) {
-            res = AVERROR(errno);
-            av_log(ctx, AV_LOG_ERROR, "mmap: %s\n", av_err2str(res));
-            return res;
+        if (s->frame_size > 0 && total_frame_size < s->frame_size) {
+            av_log(ctx, AV_LOG_ERROR,
+                "buf_len[%d] = %d < expected frame size %d\n",
+                i, total_frame_size, s->frame_size);
+            return AVERROR(ENOMEM);
         }
     }
 
@@ -420,9 +449,9 @@ static void mmap_release_buffer(void *opaque, uint8_t *data)
     struct buff_data *buf_descriptor = opaque;
     struct video_data *s = buf_descriptor->s;
 
-    buf.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    buf.type   = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
     buf.memory = V4L2_MEMORY_MMAP;
-    buf.index = buf_descriptor->index;
+    buf.index  = buf_descriptor->index;
     av_free(buf_descriptor);
 
     enqueue_buffer(s, &buf);
@@ -492,9 +521,12 @@ static int convert_timestamp(AVFormatContext *ctx, int64_t *ts)
 static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
 {
     struct video_data *s = ctx->priv_data;
+    struct v4l2_plane planes[VIDEO_MAX_PLANES];
     struct v4l2_buffer buf = {
-        .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
-        .memory = V4L2_MEMORY_MMAP
+        .type     = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE,
+        .memory   = V4L2_MEMORY_MMAP,
+        .m.planes = (s->multi_planer) ? planes : 0,
+        .length   = (s->multi_planer) ? VIDEO_MAX_PLANES : 0
     };
     struct timeval buf_ts;
     int res;
@@ -532,29 +564,63 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
     } else
 #endif
     {
+        int total_frame_size = 0;
+
         /* CPIA is a compressed format and we don't know the exact number of bytes
          * used by a frame, so set it here as the driver announces it. */
         if (ctx->video_codec_id == AV_CODEC_ID_CPIA)
             s->frame_size = buf.bytesused;
 
-        if (s->frame_size > 0 && buf.bytesused != s->frame_size) {
+        if (V4L2_TYPE_IS_MULTIPLANAR(buf.type)) {
+            for (int iplane = 0; iplane < buf.length; iplane++) {
+                total_frame_size += buf.m.planes[iplane].bytesused;
+            }
+        } else {
+            total_frame_size = buf.bytesused;
+        }
+        if (s->frame_size > 0 && total_frame_size != s->frame_size) {
             av_log(ctx, AV_LOG_WARNING,
                    "Dequeued v4l2 buffer contains %d bytes, but %d were expected. Flags: 0x%08X.\n",
-                   buf.bytesused, s->frame_size, buf.flags);
-            buf.bytesused = 0;
+                   total_frame_size, s->frame_size, buf.flags);
+            if (V4L2_TYPE_IS_MULTIPLANAR(buf.type)) {
+                for (int iplane = 0; iplane < buf.length; iplane++) {
+                    buf.m.planes[iplane].bytesused = 0;
+                }
+            } else {
+                buf.bytesused = 0;
+            }
         }
     }
 
-    /* Image is at s->buff_start[buf.index] */
-    if (atomic_load(&s->buffers_queued) == FFMAX(s->buffers / 8, 1)) {
-        /* when we start getting low on queued buffers, fall back on copying data */
-        res = av_new_packet(pkt, buf.bytesused);
-        if (res < 0) {
-            av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
-            enqueue_buffer(s, &buf);
-            return res;
+    if (atomic_load(&s->buffers_queued) == FFMAX(s->buffers / 8, 1) || V4L2_TYPE_IS_MULTIPLANAR(buf.type)) {
+        if (V4L2_TYPE_IS_MULTIPLANAR(buf.type)) {
+            int totalbytes = 0;
+            for (int iplane = 0; iplane < buf.length; iplane++) {
+                totalbytes += buf.m.planes[iplane].bytesused;
+            }
+            res = av_new_packet(pkt, totalbytes);
+            if (res < 0) {
+                av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
+                enqueue_buffer(s, &buf);
+                return res;
+            }
+            totalbytes = 0;
+            for (int iplane = 0; iplane < buf.length; iplane++) {
+                struct v4l2_plane *plane = &buf.m.planes[iplane];
+                memcpy(pkt->data + totalbytes, s->buf_start[buf.index][plane->data_offset], plane->bytesused);
+                totalbytes += plane->bytesused;
+            }
+        } else {
+            /* Image is at s->buff_start[buf.index] */
+            /* when we start getting low on queued buffers, fall back on copying data */
+            res = av_new_packet(pkt, buf.bytesused);
+            if (res < 0) {
+                av_log(ctx, AV_LOG_ERROR, "Error allocating a packet.\n");
+                enqueue_buffer(s, &buf);
+                return res;
+            }
+            memcpy(pkt->data, s->buf_start[buf.index][0], buf.bytesused);
         }
-        memcpy(pkt->data, s->buf_start[buf.index], buf.bytesused);
 
         res = enqueue_buffer(s, &buf);
         if (res) {
@@ -564,7 +630,7 @@ static int mmap_read_frame(AVFormatContext *ctx, AVPacket *pkt)
     } else {
         struct buff_data *buf_descriptor;
 
-        pkt->data     = s->buf_start[buf.index];
+        pkt->data     = s->buf_start[buf.index][0];
         pkt->size     = buf.bytesused;
 
         buf_descriptor = av_malloc(sizeof(struct buff_data));
@@ -602,10 +668,13 @@ static int mmap_start(AVFormatContext *ctx)
     int i, res;
 
     for (i = 0; i < s->buffers; i++) {
+        struct v4l2_plane planes[VIDEO_MAX_PLANES];
         struct v4l2_buffer buf = {
-            .type   = V4L2_BUF_TYPE_VIDEO_CAPTURE,
-            .index  = i,
-            .memory = V4L2_MEMORY_MMAP
+            .type     = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE,
+            .index    = i,
+            .memory   = V4L2_MEMORY_MMAP,
+            .m.planes = (s->multi_planer) ? planes : 0,
+            .length   = (s->multi_planer) ? VIDEO_MAX_PLANES : 0
         };
 
         if (v4l2_ioctl(s->fd, VIDIOC_QBUF, &buf) < 0) {
@@ -617,7 +686,7 @@ static int mmap_start(AVFormatContext *ctx)
     }
     atomic_store(&s->buffers_queued, s->buffers);
 
-    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
     if (v4l2_ioctl(s->fd, VIDIOC_STREAMON, &type) < 0) {
         res = AVERROR(errno);
         av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_STREAMON): %s\n",
@@ -633,13 +702,19 @@ static void mmap_close(struct video_data *s)
     enum v4l2_buf_type type;
     int i;
 
-    type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
     /* We do not check for the result, because we could
      * not do anything about it anyway...
      */
     v4l2_ioctl(s->fd, VIDIOC_STREAMOFF, &type);
     for (i = 0; i < s->buffers; i++) {
-        v4l2_munmap(s->buf_start[i], s->buf_len[i]);
+        for (int iplane = 0; iplane < s->plane_count; iplane++) {
+            v4l2_munmap(s->buf_start[i][iplane], s->buf_len[i][iplane]);
+        }
+    }
+    for (int iplane = 0; iplane < s->plane_count; iplane++) {
+        av_freep(&s->buf_start[iplane]);
+        av_freep(&s->buf_len[iplane]);
     }
     av_freep(&s->buf_start);
     av_freep(&s->buf_len);
@@ -720,7 +795,7 @@ static int v4l2_set_parameters(AVFormatContext *ctx)
         tpf = &streamparm.parm.capture.timeperframe;
     }
 
-    streamparm.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
+    streamparm.type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
     if (v4l2_ioctl(s->fd, VIDIOC_G_PARM, &streamparm) < 0) {
         ret = AVERROR(errno);
         av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
@@ -908,7 +983,7 @@ static int v4l2_read_header(AVFormatContext *ctx)
     }
 
     if (!s->width && !s->height) {
-        struct v4l2_format fmt = { .type = V4L2_BUF_TYPE_VIDEO_CAPTURE };
+        struct v4l2_format fmt = { .type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE };
 
         av_log(ctx, AV_LOG_VERBOSE,
                "Querying the device for the current frame size\n");
-- 
2.41.0


From 25de40876e8e9d3868f22dc37ac492b64be186a0 Mon Sep 17 00:00:00 2001
From: rigaya <rigaya34589@live.jp>
Date: Fri, 7 Apr 2023 22:47:10 +0900
Subject: [PATCH 02/13] avdevice/v4l2: add nv16, nv24 support

---
 libavdevice/v4l2-common.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libavdevice/v4l2-common.c b/libavdevice/v4l2-common.c
index b5b4448a31..1926179fdc 100644
--- a/libavdevice/v4l2-common.c
+++ b/libavdevice/v4l2-common.c
@@ -49,6 +49,8 @@ const struct fmt_map ff_fmt_conversion_table[] = {
 #ifdef V4L2_PIX_FMT_Z16
     { AV_PIX_FMT_GRAY16LE,AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_Z16     },
 #endif
+    { AV_PIX_FMT_NV24,    AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV24    },
+    { AV_PIX_FMT_NV16,    AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV16    },
     { AV_PIX_FMT_NV12,    AV_CODEC_ID_RAWVIDEO, V4L2_PIX_FMT_NV12    },
     { AV_PIX_FMT_NONE,    AV_CODEC_ID_MJPEG,    V4L2_PIX_FMT_MJPEG   },
     { AV_PIX_FMT_NONE,    AV_CODEC_ID_MJPEG,    V4L2_PIX_FMT_JPEG    },
-- 
2.41.0


From ebfac2ef875422a57955bd7c1f09c1b9d48d958a Mon Sep 17 00:00:00 2001
From: rigaya <rigaya34589@live.jp>
Date: Sat, 8 Apr 2023 09:48:45 +0900
Subject: [PATCH 03/13] avdevice/v4l2: add option to ignore input error
 (-ignore_input_error).

Some device returns error with VIDIOC_S_INPUT, VIDIOC_G_INPUT.
---
 libavdevice/v4l2.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index e194c1df24..fc20da7ef7 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -77,6 +77,7 @@ struct video_data {
     int top_field_first;
     int multi_planer;
     int ts_mode;
+    int ignore_input_error;
     TimeFilter *timefilter;
     int64_t last_time_m;
 
@@ -798,7 +799,7 @@ static int v4l2_set_parameters(AVFormatContext *ctx)
     streamparm.type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
     if (v4l2_ioctl(s->fd, VIDIOC_G_PARM, &streamparm) < 0) {
         ret = AVERROR(errno);
-        av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
+            av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
     } else if (framerate_q.num && framerate_q.den) {
         if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
             tpf = &streamparm.parm.capture.timeperframe;
@@ -928,8 +929,10 @@ static int v4l2_read_header(AVFormatContext *ctx)
         av_log(ctx, AV_LOG_DEBUG, "Selecting input_channel: %d\n", s->channel);
         if (v4l2_ioctl(s->fd, VIDIOC_S_INPUT, &s->channel) < 0) {
             res = AVERROR(errno);
-            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
-            goto fail;
+            av_log(ctx, (s->ignore_input_error) ? AV_LOG_WARNING : AV_LOG_ERROR, "ioctl(VIDIOC_S_INPUT): %s\n", av_err2str(res));
+            if (!s->ignore_input_error) {
+                goto fail;
+            }
         }
     } else {
         /* get current video input */
@@ -1181,6 +1184,7 @@ static const AVOption options[] = {
     { "pixel_format", "set preferred pixel format",                               OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
     { "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
     { "framerate",    "set frame rate",                                           OFFSET(framerate),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
+    { "ignore_input_error", "ignore input error",                                 OFFSET(ignore_input_error), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1,      DEC },
 
     { "list_formats", "list available formats and exit",                          OFFSET(list_format),  AV_OPT_TYPE_INT,    {.i64 = 0 },  0, INT_MAX, DEC, "list_formats" },
     { "all",          "show all available formats",                               OFFSET(list_format),  AV_OPT_TYPE_CONST,  {.i64 = V4L_ALLFORMATS  },    0, INT_MAX, DEC, "list_formats" },
-- 
2.41.0


From 287b4e0959df84cc293a03300e98a4fdb0193935 Mon Sep 17 00:00:00 2001
From: rigaya <rigaya34589@live.jp>
Date: Sat, 8 Apr 2023 09:49:05 +0900
Subject: [PATCH 04/13] avdevice/v4l2: estimate framerate from dv-timings when
 VIDIOC_G_PARM returns error.

---
 libavdevice/v4l2.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index fc20da7ef7..e5aa33e453 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -798,8 +798,38 @@ static int v4l2_set_parameters(AVFormatContext *ctx)
 
     streamparm.type = (s->multi_planer) ? V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE : V4L2_BUF_TYPE_VIDEO_CAPTURE;
     if (v4l2_ioctl(s->fd, VIDIOC_G_PARM, &streamparm) < 0) {
+        // for error cases, try to get frame rate from VIDIOC_G_DV_TIMINGS
+        struct v4l2_dv_timings timings;
         ret = AVERROR(errno);
+        if (v4l2_ioctl(s->fd, VIDIOC_G_DV_TIMINGS, &timings) == 0) {
+            const int total_width  = timings.bt.width  + timings.bt.hfrontporch + timings.bt.hsync + timings.bt.hbackporch;
+            const int total_height = timings.bt.height + timings.bt.vfrontporch + timings.bt.vsync + timings.bt.vbackporch;
+            int64_t framerate_den = 1001;
+            int64_t framerate_num = av_rescale(timings.bt.pixelclock, framerate_den, (int64_t)total_width * total_height);
+            framerate_num = ((framerate_num + 5) / 10) * 10; // round by 10
+            if (framerate_num % 1000 == 0) {
+                tpf->numerator   = framerate_den;
+                tpf->denominator = framerate_num;
+            } else {
+                int framerate_num_dst = 0, framerate_den_dst = 0;
+                framerate_den = 1000;
+                framerate_num = av_rescale(timings.bt.pixelclock, framerate_den, (int64_t)total_width * total_height);
+                framerate_num = ((framerate_num + 5) / 10) * 10; // round by 10
+                av_reduce(&framerate_num_dst, &framerate_den_dst, framerate_num, framerate_den, INT_MAX);
+                tpf->numerator   = framerate_den_dst;
+                tpf->denominator = framerate_num_dst;
+            }
+            av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s, estimated framerate %d/%d from dv timings.\n",
+                av_err2str(ret), tpf->denominator, tpf->numerator);
+        } else if (framerate_q.num && framerate_q.den) {
+            // use user defined framerate for further error cases.
+            tpf->numerator   = framerate_q.num;
+            tpf->denominator = framerate_q.den;
+            av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s, using framerate %d/%d\n",
+                av_err2str(ret), framerate_q.num, framerate_q.den);
+        } else {
             av_log(ctx, AV_LOG_WARNING, "ioctl(VIDIOC_G_PARM): %s\n", av_err2str(ret));
+        }
     } else if (framerate_q.num && framerate_q.den) {
         if (streamparm.parm.capture.capability & V4L2_CAP_TIMEPERFRAME) {
             tpf = &streamparm.parm.capture.timeperframe;
-- 
2.41.0


From 305e4e3c84e42368c41d3b9e9b814f0027c8f7f2 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Sat, 17 Jun 2023 18:56:02 +0200
Subject: [PATCH 05/13] v4l2: use always channel 0 if driver does not return
 it. Always ignore device select / set errors

---
 libavdevice/v4l2.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/libavdevice/v4l2.c b/libavdevice/v4l2.c
index e5aa33e453..9b64caecaa 100644
--- a/libavdevice/v4l2.c
+++ b/libavdevice/v4l2.c
@@ -968,8 +968,11 @@ static int v4l2_read_header(AVFormatContext *ctx)
         /* get current video input */
         if (v4l2_ioctl(s->fd, VIDIOC_G_INPUT, &s->channel) < 0) {
             res = AVERROR(errno);
-            av_log(ctx, AV_LOG_ERROR, "ioctl(VIDIOC_G_INPUT): %s\n", av_err2str(res));
-            goto fail;
+            av_log(ctx, (s->ignore_input_error) ? AV_LOG_WARNING : AV_LOG_ERROR, "ioctl(VIDIOC_G_INPUT): %s\n", av_err2str(res));
+            if (!s->ignore_input_error)
+                goto fail;
+            else
+                s->channel = 0;
         }
     }
 
@@ -1214,7 +1217,7 @@ static const AVOption options[] = {
     { "pixel_format", "set preferred pixel format",                               OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
     { "input_format", "set preferred pixel format (for raw video) or codec name", OFFSET(pixel_format), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
     { "framerate",    "set frame rate",                                           OFFSET(framerate),    AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,       DEC },
-    { "ignore_input_error", "ignore input error",                                 OFFSET(ignore_input_error), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1,      DEC },
+    { "ignore_input_error", "ignore input error",                                 OFFSET(ignore_input_error), AV_OPT_TYPE_BOOL, {.i64 = 1 }, 0, 1,      DEC },
 
     { "list_formats", "list available formats and exit",                          OFFSET(list_format),  AV_OPT_TYPE_INT,    {.i64 = 0 },  0, INT_MAX, DEC, "list_formats" },
     { "all",          "show all available formats",                               OFFSET(list_format),  AV_OPT_TYPE_CONST,  {.i64 = V4L_ALLFORMATS  },    0, INT_MAX, DEC, "list_formats" },
-- 
2.41.0


From 204a6da5244c5905836c6542e4a3f4f78128ff18 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 24 Jul 2023 16:05:01 +0200
Subject: [PATCH 06/13] remove old rkmpp

---
 libavcodec/rkmppdec.c | 586 ------------------------------------------
 1 file changed, 586 deletions(-)
 delete mode 100644 libavcodec/rkmppdec.c

diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c
deleted file mode 100644
index a60962dc86..0000000000
--- a/libavcodec/rkmppdec.c
+++ /dev/null
@@ -1,586 +0,0 @@
-/*
- * RockChip MPP Video Decoder
- * Copyright (c) 2017 Lionel CHAZALLON
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg 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.
- *
- * FFmpeg 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 FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include <drm_fourcc.h>
-#include <pthread.h>
-#include <rockchip/mpp_buffer.h>
-#include <rockchip/rk_mpi.h>
-#include <time.h>
-#include <unistd.h>
-
-#include "avcodec.h"
-#include "decode.h"
-#include "hwconfig.h"
-#include "internal.h"
-#include "libavutil/buffer.h"
-#include "libavutil/common.h"
-#include "libavutil/frame.h"
-#include "libavutil/hwcontext.h"
-#include "libavutil/hwcontext_drm.h"
-#include "libavutil/imgutils.h"
-#include "libavutil/log.h"
-
-#define RECEIVE_FRAME_TIMEOUT   100
-#define FRAMEGROUP_MAX_FRAMES   16
-#define INPUT_MAX_PACKETS       4
-
-typedef struct {
-    MppCtx ctx;
-    MppApi *mpi;
-    MppBufferGroup frame_group;
-
-    char first_packet;
-    char eos_reached;
-
-    AVBufferRef *frames_ref;
-    AVBufferRef *device_ref;
-} RKMPPDecoder;
-
-typedef struct {
-    AVClass *av_class;
-    AVBufferRef *decoder_ref;
-} RKMPPDecodeContext;
-
-typedef struct {
-    MppFrame frame;
-    AVBufferRef *decoder_ref;
-} RKMPPFrameContext;
-
-static MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx)
-{
-    switch (avctx->codec_id) {
-    case AV_CODEC_ID_H264:          return MPP_VIDEO_CodingAVC;
-    case AV_CODEC_ID_HEVC:          return MPP_VIDEO_CodingHEVC;
-    case AV_CODEC_ID_VP8:           return MPP_VIDEO_CodingVP8;
-    case AV_CODEC_ID_VP9:           return MPP_VIDEO_CodingVP9;
-    default:                        return MPP_VIDEO_CodingUnused;
-    }
-}
-
-static uint32_t rkmpp_get_frameformat(MppFrameFormat mppformat)
-{
-    switch (mppformat) {
-    case MPP_FMT_YUV420SP:          return DRM_FORMAT_NV12;
-#ifdef DRM_FORMAT_NV12_10
-    case MPP_FMT_YUV420SP_10BIT:    return DRM_FORMAT_NV12_10;
-#endif
-    default:                        return 0;
-    }
-}
-
-static int rkmpp_write_data(AVCodecContext *avctx, uint8_t *buffer, int size, int64_t pts)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
-    int ret;
-    MppPacket packet;
-
-    // create the MPP packet
-    ret = mpp_packet_init(&packet, buffer, size);
-    if (ret != MPP_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to init MPP packet (code = %d)\n", ret);
-        return AVERROR_UNKNOWN;
-    }
-
-    mpp_packet_set_pts(packet, pts);
-
-    if (!buffer)
-        mpp_packet_set_eos(packet);
-
-    ret = decoder->mpi->decode_put_packet(decoder->ctx, packet);
-    if (ret != MPP_OK) {
-        if (ret == MPP_ERR_BUFFER_FULL) {
-            av_log(avctx, AV_LOG_DEBUG, "Buffer full writing %d bytes to decoder\n", size);
-            ret = AVERROR(EAGAIN);
-        } else
-            ret = AVERROR_UNKNOWN;
-    }
-    else
-        av_log(avctx, AV_LOG_DEBUG, "Wrote %d bytes to decoder\n", size);
-
-    mpp_packet_deinit(&packet);
-
-    return ret;
-}
-
-static int rkmpp_close_decoder(AVCodecContext *avctx)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    av_buffer_unref(&rk_context->decoder_ref);
-    return 0;
-}
-
-static void rkmpp_release_decoder(void *opaque, uint8_t *data)
-{
-    RKMPPDecoder *decoder = (RKMPPDecoder *)data;
-
-    if (decoder->mpi) {
-        decoder->mpi->reset(decoder->ctx);
-        mpp_destroy(decoder->ctx);
-        decoder->ctx = NULL;
-    }
-
-    if (decoder->frame_group) {
-        mpp_buffer_group_put(decoder->frame_group);
-        decoder->frame_group = NULL;
-    }
-
-    av_buffer_unref(&decoder->frames_ref);
-    av_buffer_unref(&decoder->device_ref);
-
-    av_free(decoder);
-}
-
-static int rkmpp_init_decoder(AVCodecContext *avctx)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    RKMPPDecoder *decoder = NULL;
-    MppCodingType codectype = MPP_VIDEO_CodingUnused;
-    int ret;
-    RK_S64 paramS64;
-    RK_S32 paramS32;
-
-    avctx->pix_fmt = AV_PIX_FMT_DRM_PRIME;
-
-    // create a decoder and a ref to it
-    decoder = av_mallocz(sizeof(RKMPPDecoder));
-    if (!decoder) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    rk_context->decoder_ref = av_buffer_create((uint8_t *)decoder, sizeof(*decoder), rkmpp_release_decoder,
-                                               NULL, AV_BUFFER_FLAG_READONLY);
-    if (!rk_context->decoder_ref) {
-        av_free(decoder);
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-
-    av_log(avctx, AV_LOG_DEBUG, "Initializing RKMPP decoder.\n");
-
-    codectype = rkmpp_get_codingtype(avctx);
-    if (codectype == MPP_VIDEO_CodingUnused) {
-        av_log(avctx, AV_LOG_ERROR, "Unknown codec type (%d).\n", avctx->codec_id);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    ret = mpp_check_support_format(MPP_CTX_DEC, codectype);
-    if (ret != MPP_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Codec type (%d) unsupported by MPP\n", avctx->codec_id);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    // Create the MPP context
-    ret = mpp_create(&decoder->ctx, &decoder->mpi);
-    if (ret != MPP_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to create MPP context (code = %d).\n", ret);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    // initialize mpp
-    ret = mpp_init(decoder->ctx, MPP_CTX_DEC, codectype);
-    if (ret != MPP_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to initialize MPP context (code = %d).\n", ret);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    // make decode calls blocking with a timeout
-    paramS32 = MPP_POLL_BLOCK;
-    ret = decoder->mpi->control(decoder->ctx, MPP_SET_OUTPUT_BLOCK, &paramS32);
-    if (ret != MPP_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to set blocking mode on MPI (code = %d).\n", ret);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    paramS64 = RECEIVE_FRAME_TIMEOUT;
-    ret = decoder->mpi->control(decoder->ctx, MPP_SET_OUTPUT_BLOCK_TIMEOUT, &paramS64);
-    if (ret != MPP_OK) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to set block timeout on MPI (code = %d).\n", ret);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    ret = mpp_buffer_group_get_internal(&decoder->frame_group, MPP_BUFFER_TYPE_ION);
-    if (ret) {
-       av_log(avctx, AV_LOG_ERROR, "Failed to retrieve buffer group (code = %d)\n", ret);
-       ret = AVERROR_UNKNOWN;
-       goto fail;
-    }
-
-    ret = decoder->mpi->control(decoder->ctx, MPP_DEC_SET_EXT_BUF_GROUP, decoder->frame_group);
-    if (ret) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to assign buffer group (code = %d)\n", ret);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    ret = mpp_buffer_group_limit_config(decoder->frame_group, 0, FRAMEGROUP_MAX_FRAMES);
-    if (ret) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to set buffer group limit (code = %d)\n", ret);
-        ret = AVERROR_UNKNOWN;
-        goto fail;
-    }
-
-    decoder->first_packet = 1;
-
-    av_log(avctx, AV_LOG_DEBUG, "RKMPP decoder initialized successfully.\n");
-
-    decoder->device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM);
-    if (!decoder->device_ref) {
-        ret = AVERROR(ENOMEM);
-        goto fail;
-    }
-    ret = av_hwdevice_ctx_init(decoder->device_ref);
-    if (ret < 0)
-        goto fail;
-
-    return 0;
-
-fail:
-    av_log(avctx, AV_LOG_ERROR, "Failed to initialize RKMPP decoder.\n");
-    rkmpp_close_decoder(avctx);
-    return ret;
-}
-
-static int rkmpp_send_packet(AVCodecContext *avctx, const AVPacket *avpkt)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
-    int ret;
-
-    // handle EOF
-    if (!avpkt->size) {
-        av_log(avctx, AV_LOG_DEBUG, "End of stream.\n");
-        decoder->eos_reached = 1;
-        ret = rkmpp_write_data(avctx, NULL, 0, 0);
-        if (ret)
-            av_log(avctx, AV_LOG_ERROR, "Failed to send EOS to decoder (code = %d)\n", ret);
-        return ret;
-    }
-
-    // on first packet, send extradata
-    if (decoder->first_packet) {
-        if (avctx->extradata_size) {
-            ret = rkmpp_write_data(avctx, avctx->extradata,
-                                            avctx->extradata_size,
-                                            avpkt->pts);
-            if (ret) {
-                av_log(avctx, AV_LOG_ERROR, "Failed to write extradata to decoder (code = %d)\n", ret);
-                return ret;
-            }
-        }
-        decoder->first_packet = 0;
-    }
-
-    // now send packet
-    ret = rkmpp_write_data(avctx, avpkt->data, avpkt->size, avpkt->pts);
-    if (ret && ret!=AVERROR(EAGAIN))
-        av_log(avctx, AV_LOG_ERROR, "Failed to write data to decoder (code = %d)\n", ret);
-
-    return ret;
-}
-
-static void rkmpp_release_frame(void *opaque, uint8_t *data)
-{
-    AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data;
-    AVBufferRef *framecontextref = (AVBufferRef *)opaque;
-    RKMPPFrameContext *framecontext = (RKMPPFrameContext *)framecontextref->data;
-
-    mpp_frame_deinit(&framecontext->frame);
-    av_buffer_unref(&framecontext->decoder_ref);
-    av_buffer_unref(&framecontextref);
-
-    av_free(desc);
-}
-
-static int rkmpp_retrieve_frame(AVCodecContext *avctx, AVFrame *frame)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
-    RKMPPFrameContext *framecontext = NULL;
-    AVBufferRef *framecontextref = NULL;
-    int ret;
-    MppFrame mppframe = NULL;
-    MppBuffer buffer = NULL;
-    AVDRMFrameDescriptor *desc = NULL;
-    AVDRMLayerDescriptor *layer = NULL;
-    int mode;
-    MppFrameFormat mppformat;
-    uint32_t drmformat;
-
-    ret = decoder->mpi->decode_get_frame(decoder->ctx, &mppframe);
-    if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT) {
-        av_log(avctx, AV_LOG_ERROR, "Failed to get a frame from MPP (code = %d)\n", ret);
-        goto fail;
-    }
-
-    if (mppframe) {
-        // Check whether we have a special frame or not
-        if (mpp_frame_get_info_change(mppframe)) {
-            AVHWFramesContext *hwframes;
-
-            av_log(avctx, AV_LOG_INFO, "Decoder noticed an info change (%dx%d), format=%d\n",
-                                        (int)mpp_frame_get_width(mppframe), (int)mpp_frame_get_height(mppframe),
-                                        (int)mpp_frame_get_fmt(mppframe));
-
-            avctx->width = mpp_frame_get_width(mppframe);
-            avctx->height = mpp_frame_get_height(mppframe);
-
-            decoder->mpi->control(decoder->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
-
-            av_buffer_unref(&decoder->frames_ref);
-
-            decoder->frames_ref = av_hwframe_ctx_alloc(decoder->device_ref);
-            if (!decoder->frames_ref) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
-
-            mppformat = mpp_frame_get_fmt(mppframe);
-            drmformat = rkmpp_get_frameformat(mppformat);
-
-            hwframes = (AVHWFramesContext*)decoder->frames_ref->data;
-            hwframes->format    = AV_PIX_FMT_DRM_PRIME;
-            hwframes->sw_format = drmformat == DRM_FORMAT_NV12 ? AV_PIX_FMT_NV12 : AV_PIX_FMT_NONE;
-            hwframes->width     = avctx->width;
-            hwframes->height    = avctx->height;
-            ret = av_hwframe_ctx_init(decoder->frames_ref);
-            if (ret < 0)
-                goto fail;
-
-            // here decoder is fully initialized, we need to feed it again with data
-            ret = AVERROR(EAGAIN);
-            goto fail;
-        } else if (mpp_frame_get_eos(mppframe)) {
-            av_log(avctx, AV_LOG_DEBUG, "Received a EOS frame.\n");
-            decoder->eos_reached = 1;
-            ret = AVERROR_EOF;
-            goto fail;
-        } else if (mpp_frame_get_discard(mppframe)) {
-            av_log(avctx, AV_LOG_DEBUG, "Received a discard frame.\n");
-            ret = AVERROR(EAGAIN);
-            goto fail;
-        } else if (mpp_frame_get_errinfo(mppframe)) {
-            av_log(avctx, AV_LOG_ERROR, "Received a errinfo frame.\n");
-            ret = AVERROR_UNKNOWN;
-            goto fail;
-        }
-
-        // here we should have a valid frame
-        av_log(avctx, AV_LOG_DEBUG, "Received a frame.\n");
-
-        // setup general frame fields
-        frame->format           = AV_PIX_FMT_DRM_PRIME;
-        frame->width            = mpp_frame_get_width(mppframe);
-        frame->height           = mpp_frame_get_height(mppframe);
-        frame->pts              = mpp_frame_get_pts(mppframe);
-        frame->color_range      = mpp_frame_get_color_range(mppframe);
-        frame->color_primaries  = mpp_frame_get_color_primaries(mppframe);
-        frame->color_trc        = mpp_frame_get_color_trc(mppframe);
-        frame->colorspace       = mpp_frame_get_colorspace(mppframe);
-
-        mode = mpp_frame_get_mode(mppframe);
-        frame->interlaced_frame = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED);
-        frame->top_field_first  = ((mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST);
-
-        mppformat = mpp_frame_get_fmt(mppframe);
-        drmformat = rkmpp_get_frameformat(mppformat);
-
-        // now setup the frame buffer info
-        buffer = mpp_frame_get_buffer(mppframe);
-        if (buffer) {
-            desc = av_mallocz(sizeof(AVDRMFrameDescriptor));
-            if (!desc) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
-
-            desc->nb_objects = 1;
-            desc->objects[0].fd = mpp_buffer_get_fd(buffer);
-            desc->objects[0].size = mpp_buffer_get_size(buffer);
-
-            desc->nb_layers = 1;
-            layer = &desc->layers[0];
-            layer->format = drmformat;
-            layer->nb_planes = 2;
-
-            layer->planes[0].object_index = 0;
-            layer->planes[0].offset = 0;
-            layer->planes[0].pitch = mpp_frame_get_hor_stride(mppframe);
-
-            layer->planes[1].object_index = 0;
-            layer->planes[1].offset = layer->planes[0].pitch * mpp_frame_get_ver_stride(mppframe);
-            layer->planes[1].pitch = layer->planes[0].pitch;
-
-            // we also allocate a struct in buf[0] that will allow to hold additionnal information
-            // for releasing properly MPP frames and decoder
-            framecontextref = av_buffer_allocz(sizeof(*framecontext));
-            if (!framecontextref) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
-
-            // MPP decoder needs to be closed only when all frames have been released.
-            framecontext = (RKMPPFrameContext *)framecontextref->data;
-            framecontext->decoder_ref = av_buffer_ref(rk_context->decoder_ref);
-            framecontext->frame = mppframe;
-
-            frame->data[0]  = (uint8_t *)desc;
-            frame->buf[0]   = av_buffer_create((uint8_t *)desc, sizeof(*desc), rkmpp_release_frame,
-                                               framecontextref, AV_BUFFER_FLAG_READONLY);
-
-            if (!frame->buf[0]) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
-
-            frame->hw_frames_ctx = av_buffer_ref(decoder->frames_ref);
-            if (!frame->hw_frames_ctx) {
-                ret = AVERROR(ENOMEM);
-                goto fail;
-            }
-
-            return 0;
-        } else {
-            av_log(avctx, AV_LOG_ERROR, "Failed to retrieve the frame buffer, frame is dropped (code = %d)\n", ret);
-            mpp_frame_deinit(&mppframe);
-        }
-    } else if (decoder->eos_reached) {
-        return AVERROR_EOF;
-    } else if (ret == MPP_ERR_TIMEOUT) {
-        av_log(avctx, AV_LOG_DEBUG, "Timeout when trying to get a frame from MPP\n");
-    }
-
-    return AVERROR(EAGAIN);
-
-fail:
-    if (mppframe)
-        mpp_frame_deinit(&mppframe);
-
-    if (framecontext)
-        av_buffer_unref(&framecontext->decoder_ref);
-
-    if (framecontextref)
-        av_buffer_unref(&framecontextref);
-
-    if (desc)
-        av_free(desc);
-
-    return ret;
-}
-
-static int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
-    int ret = MPP_NOK;
-    AVPacket pkt = {0};
-    RK_S32 usedslots, freeslots;
-
-    if (!decoder->eos_reached) {
-        // we get the available slots in decoder
-        ret = decoder->mpi->control(decoder->ctx, MPP_DEC_GET_STREAM_COUNT, &usedslots);
-        if (ret != MPP_OK) {
-            av_log(avctx, AV_LOG_ERROR, "Failed to get decoder used slots (code = %d).\n", ret);
-            return ret;
-        }
-
-        freeslots = INPUT_MAX_PACKETS - usedslots;
-        if (freeslots > 0) {
-            ret = ff_decode_get_packet(avctx, &pkt);
-            if (ret < 0 && ret != AVERROR_EOF) {
-                return ret;
-            }
-
-            ret = rkmpp_send_packet(avctx, &pkt);
-            av_packet_unref(&pkt);
-
-            if (ret < 0) {
-                av_log(avctx, AV_LOG_ERROR, "Failed to send packet to decoder (code = %d)\n", ret);
-                return ret;
-            }
-        }
-
-        // make sure we keep decoder full
-        if (freeslots > 1)
-            return AVERROR(EAGAIN);
-    }
-
-    return rkmpp_retrieve_frame(avctx, frame);
-}
-
-static void rkmpp_flush(AVCodecContext *avctx)
-{
-    RKMPPDecodeContext *rk_context = avctx->priv_data;
-    RKMPPDecoder *decoder = (RKMPPDecoder *)rk_context->decoder_ref->data;
-    int ret = MPP_NOK;
-
-    av_log(avctx, AV_LOG_DEBUG, "Flush.\n");
-
-    ret = decoder->mpi->reset(decoder->ctx);
-    if (ret == MPP_OK) {
-        decoder->first_packet = 1;
-    } else
-        av_log(avctx, AV_LOG_ERROR, "Failed to reset MPI (code = %d)\n", ret);
-}
-
-static const AVCodecHWConfigInternal *const rkmpp_hw_configs[] = {
-    HW_CONFIG_INTERNAL(DRM_PRIME),
-    NULL
-};
-
-#define RKMPP_DEC_CLASS(NAME) \
-    static const AVClass rkmpp_##NAME##_dec_class = { \
-        .class_name = "rkmpp_" #NAME "_dec", \
-        .version    = LIBAVUTIL_VERSION_INT, \
-    };
-
-#define RKMPP_DEC(NAME, ID, BSFS) \
-    RKMPP_DEC_CLASS(NAME) \
-    AVCodec ff_##NAME##_rkmpp_decoder = { \
-        .name           = #NAME "_rkmpp", \
-        .long_name      = NULL_IF_CONFIG_SMALL(#NAME " (rkmpp)"), \
-        .type           = AVMEDIA_TYPE_VIDEO, \
-        .id             = ID, \
-        .priv_data_size = sizeof(RKMPPDecodeContext), \
-        .init           = rkmpp_init_decoder, \
-        .close          = rkmpp_close_decoder, \
-        .receive_frame  = rkmpp_receive_frame, \
-        .flush          = rkmpp_flush, \
-        .priv_class     = &rkmpp_##NAME##_dec_class, \
-        .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
-        .pix_fmts       = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \
-                                                         AV_PIX_FMT_NONE}, \
-        .hw_configs     = rkmpp_hw_configs, \
-        .bsfs           = BSFS, \
-        .wrapper_name   = "rkmpp", \
-    };
-
-RKMPP_DEC(h264,  AV_CODEC_ID_H264,          "h264_mp4toannexb")
-RKMPP_DEC(hevc,  AV_CODEC_ID_HEVC,          "hevc_mp4toannexb")
-RKMPP_DEC(vp8,   AV_CODEC_ID_VP8,           NULL)
-RKMPP_DEC(vp9,   AV_CODEC_ID_VP9,           NULL)
-- 
2.41.0


From e10c2ce641ed9e9018ee257679eaa3841d3c0f9f Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 24 Jul 2023 16:01:21 +0200
Subject: [PATCH 07/13] prepare buildsystem for rkmpp

---
 configure           | 18 +++++++++++++++++-
 libavcodec/Makefile | 16 ++++++++++++----
 2 files changed, 29 insertions(+), 5 deletions(-)

diff --git a/configure b/configure
index fb55e04ee7..041039c9c4 100755
--- a/configure
+++ b/configure
@@ -3070,8 +3070,10 @@ nvenc_encoder_deps="nvenc"
 aac_mf_encoder_deps="mediafoundation"
 ac3_mf_encoder_deps="mediafoundation"
 av1_cuvid_decoder_deps="cuvid CUVIDAV1PICPARAMS"
+av1_rkmpp_decoder_deps="rkmpp"
 h263_v4l2m2m_decoder_deps="v4l2_m2m h263_v4l2_m2m"
 h263_v4l2m2m_encoder_deps="v4l2_m2m h263_v4l2_m2m"
+h263_rkmpp_decoder_deps="rkmpp"
 h264_amf_encoder_deps="amf"
 h264_crystalhd_decoder_select="crystalhd h264_mp4toannexb_bsf h264_parser"
 h264_cuvid_decoder_deps="cuvid"
@@ -3087,6 +3089,8 @@ h264_qsv_decoder_select="h264_mp4toannexb_bsf qsvdec"
 h264_qsv_encoder_select="atsc_a53 qsvenc"
 h264_rkmpp_decoder_deps="rkmpp"
 h264_rkmpp_decoder_select="h264_mp4toannexb_bsf"
+h264_rkmpp_encoder_deps="rkmpp"
+h264_rkmpp_encoder_select="h264_mp4toannexb_bsf"
 h264_vaapi_encoder_select="cbs_h264 vaapi_encode"
 h264_v4l2m2m_decoder_deps="v4l2_m2m h264_v4l2_m2m"
 h264_v4l2m2m_decoder_select="h264_mp4toannexb_bsf"
@@ -3103,6 +3107,8 @@ hevc_qsv_decoder_select="hevc_mp4toannexb_bsf qsvdec"
 hevc_qsv_encoder_select="hevcparse qsvenc"
 hevc_rkmpp_decoder_deps="rkmpp"
 hevc_rkmpp_decoder_select="hevc_mp4toannexb_bsf"
+hevc_rkmpp_encoder_deps="rkmpp"
+hevc_rkmpp_encoder_select="hevc_mp4toannexb_bsf"
 hevc_vaapi_encoder_deps="VAEncPictureParameterBufferHEVC"
 hevc_vaapi_encoder_select="cbs_h265 vaapi_encode"
 hevc_v4l2m2m_decoder_deps="v4l2_m2m hevc_v4l2_m2m"
@@ -3117,6 +3123,7 @@ mjpeg_vaapi_encoder_select="cbs_jpeg jpegtables vaapi_encode"
 mp3_mf_encoder_deps="mediafoundation"
 mpeg1_cuvid_decoder_deps="cuvid"
 mpeg1_v4l2m2m_decoder_deps="v4l2_m2m mpeg1_v4l2_m2m"
+mpeg1_rkmpp_decoder_deps="rkmpp"
 mpeg2_crystalhd_decoder_select="crystalhd"
 mpeg2_cuvid_decoder_deps="cuvid"
 mpeg2_mmal_decoder_deps="mmal"
@@ -3125,6 +3132,7 @@ mpeg2_qsv_decoder_select="qsvdec"
 mpeg2_qsv_encoder_select="qsvenc"
 mpeg2_vaapi_encoder_select="cbs_mpeg2 vaapi_encode"
 mpeg2_v4l2m2m_decoder_deps="v4l2_m2m mpeg2_v4l2_m2m"
+mpeg2_rkmpp_decoder_deps="rkmpp"
 mpeg4_crystalhd_decoder_select="crystalhd"
 mpeg4_cuvid_decoder_deps="cuvid"
 mpeg4_mediacodec_decoder_deps="mediacodec"
@@ -3132,6 +3140,7 @@ mpeg4_mmal_decoder_deps="mmal"
 mpeg4_omx_encoder_deps="omx"
 mpeg4_v4l2m2m_decoder_deps="v4l2_m2m mpeg4_v4l2_m2m"
 mpeg4_v4l2m2m_encoder_deps="v4l2_m2m mpeg4_v4l2_m2m"
+mpeg4_rkmpp_decoder_deps="rkmpp"
 msmpeg4_crystalhd_decoder_select="crystalhd"
 nvenc_h264_encoder_select="h264_nvenc_encoder"
 nvenc_hevc_encoder_select="hevc_nvenc_encoder"
@@ -3144,6 +3153,7 @@ vp8_cuvid_decoder_deps="cuvid"
 vp8_mediacodec_decoder_deps="mediacodec"
 vp8_qsv_decoder_select="qsvdec"
 vp8_rkmpp_decoder_deps="rkmpp"
+vp8_rkmpp_encoder_deps="rkmpp"
 vp8_vaapi_encoder_deps="VAEncPictureParameterBufferVP8"
 vp8_vaapi_encoder_select="vaapi_encode"
 vp8_v4l2m2m_decoder_deps="v4l2_m2m vp8_v4l2_m2m"
@@ -6541,8 +6551,14 @@ enabled openssl           && { check_pkg_config openssl openssl openssl/ssl.h OP
 enabled pocketsphinx      && require_pkg_config pocketsphinx pocketsphinx pocketsphinx/pocketsphinx.h ps_init
 enabled rkmpp             && { require_pkg_config rkmpp rockchip_mpp  rockchip/rk_mpi.h mpp_create &&
                                require_pkg_config rockchip_mpp "rockchip_mpp >= 1.3.7" rockchip/rk_mpi.h mpp_create &&
+                               { check_lib librga rga/RgaApi.h c_RkRgaInit -lrga || 
+                                 die "ERROR: librga is necessary for rkmpp"; } &&
+                               prepend rkmpp_deps "librga" &&
+                               { check_lib libyuv libyuv/planar_functions.h SplitUVPlane -lyuv ||
+                                 die "ERROR: libyuv is necessary for rkmpp"; } &&
+                               prepend rkmpp_deps "libyuv" &&
                                { enabled libdrm ||
-                                 die "ERROR: rkmpp requires --enable-libdrm"; }
+                                 die "ERROR: rkmpp requires --enable-libdrm"; } 
                              }
 enabled vapoursynth       && require_pkg_config vapoursynth "vapoursynth-script >= 42" VSScript.h vsscript_init
 
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index b3d284d7d0..b9b821458a 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -231,6 +231,7 @@ OBJS-$(CONFIG_AURA_DECODER)            += cyuv.o
 OBJS-$(CONFIG_AURA2_DECODER)           += aura.o
 OBJS-$(CONFIG_AV1_DECODER)             += av1dec.o
 OBJS-$(CONFIG_AV1_CUVID_DECODER)       += cuviddec.o
+OBJS-$(CONFIG_AV1_RKMPP_DECODER)       += rkmpp.o rkplane.o rkmppdec.o
 OBJS-$(CONFIG_AVRN_DECODER)            += avrndec.o
 OBJS-$(CONFIG_AVRP_DECODER)            += r210dec.o
 OBJS-$(CONFIG_AVRP_ENCODER)            += r210enc.o
@@ -364,6 +365,7 @@ OBJS-$(CONFIG_H263_ENCODER)            += mpeg4video.o  \
                                           h263.o ituh263enc.o h263data.o
 OBJS-$(CONFIG_H263_V4L2M2M_DECODER)    += v4l2_m2m_dec.o
 OBJS-$(CONFIG_H263_V4L2M2M_ENCODER)    += v4l2_m2m_enc.o
+OBJS-$(CONFIG_H263_RKMPP_DECODER)      += rkmpp.o rkplane.o rkmppdec.o
 OBJS-$(CONFIG_H264_DECODER)            += h264dec.o h264_cabac.o h264_cavlc.o \
                                           h264_direct.o h264_loopfilter.o  \
                                           h264_mb.o h264_picture.o \
@@ -380,7 +382,8 @@ OBJS-$(CONFIG_NVENC_H264_ENCODER)      += nvenc.o nvenc_h264.o
 OBJS-$(CONFIG_H264_OMX_ENCODER)        += omx.o
 OBJS-$(CONFIG_H264_QSV_DECODER)        += qsvdec.o
 OBJS-$(CONFIG_H264_QSV_ENCODER)        += qsvenc_h264.o
-OBJS-$(CONFIG_H264_RKMPP_DECODER)      += rkmppdec.o
+OBJS-$(CONFIG_H264_RKMPP_DECODER)      += rkmpp.o rkplane.o rkmppdec.o
+OBJS-$(CONFIG_H264_RKMPP_ENCODER)      += rkmpp.o rkplane.o rkmppenc.o
 OBJS-$(CONFIG_H264_VAAPI_ENCODER)      += vaapi_encode_h264.o h264_levels.o
 OBJS-$(CONFIG_H264_VIDEOTOOLBOX_ENCODER) += videotoolboxenc.o
 OBJS-$(CONFIG_H264_V4L2M2M_DECODER)    += v4l2_m2m_dec.o
@@ -401,7 +404,8 @@ OBJS-$(CONFIG_NVENC_HEVC_ENCODER)      += nvenc.o nvenc_hevc.o
 OBJS-$(CONFIG_HEVC_QSV_DECODER)        += qsvdec.o
 OBJS-$(CONFIG_HEVC_QSV_ENCODER)        += qsvenc_hevc.o hevc_ps_enc.o       \
                                           hevc_data.o
-OBJS-$(CONFIG_HEVC_RKMPP_DECODER)      += rkmppdec.o
+OBJS-$(CONFIG_HEVC_RKMPP_DECODER)      += rkmpp.o rkplane.o rkmppdec.o
+OBJS-$(CONFIG_HEVC_RKMPP_ENCODER)      += rkmpp.o rkplane.o rkmppenc.o
 OBJS-$(CONFIG_HEVC_VAAPI_ENCODER)      += vaapi_encode_h265.o h265_profile_level.o
 OBJS-$(CONFIG_HEVC_V4L2M2M_DECODER)    += v4l2_m2m_dec.o
 OBJS-$(CONFIG_HEVC_V4L2M2M_ENCODER)    += v4l2_m2m_enc.o
@@ -488,6 +492,7 @@ OBJS-$(CONFIG_MPEG1VIDEO_DECODER)      += mpeg12dec.o mpeg12.o mpeg12data.o
 OBJS-$(CONFIG_MPEG1VIDEO_ENCODER)      += mpeg12enc.o mpeg12.o
 OBJS-$(CONFIG_MPEG1_CUVID_DECODER)     += cuviddec.o
 OBJS-$(CONFIG_MPEG1_V4L2M2M_DECODER)   += v4l2_m2m_dec.o
+OBJS-$(CONFIG_MPEG1_RKMPP_DECODER)     += rkmpp.o rkplane.o rkmppdec.o
 OBJS-$(CONFIG_MPEG2_MMAL_DECODER)      += mmaldec.o
 OBJS-$(CONFIG_MPEG2_QSV_DECODER)       += qsvdec.o
 OBJS-$(CONFIG_MPEG2_QSV_ENCODER)       += qsvenc_mpeg2.o
@@ -498,12 +503,14 @@ OBJS-$(CONFIG_MPEG2_MEDIACODEC_DECODER) += mediacodecdec.o
 OBJS-$(CONFIG_MPEG2_VAAPI_ENCODER)     += vaapi_encode_mpeg2.o
 OBJS-$(CONFIG_MPEG2_V4L2M2M_DECODER)   += v4l2_m2m_dec.o
 OBJS-$(CONFIG_MPEG4_DECODER)           += xvididct.o
+OBJS-$(CONFIG_MPEG2_RKMPP_DECODER)     += rkmpp.o rkplane.o rkmppdec.o
 OBJS-$(CONFIG_MPEG4_ENCODER)           += mpeg4videoenc.o
 OBJS-$(CONFIG_MPEG4_CUVID_DECODER)     += cuviddec.o
 OBJS-$(CONFIG_MPEG4_MEDIACODEC_DECODER) += mediacodecdec.o
 OBJS-$(CONFIG_MPEG4_OMX_ENCODER)       += omx.o
 OBJS-$(CONFIG_MPEG4_V4L2M2M_DECODER)   += v4l2_m2m_dec.o
 OBJS-$(CONFIG_MPEG4_V4L2M2M_ENCODER)   += v4l2_m2m_enc.o
+OBJS-$(CONFIG_MPEG4_RKMPP_DECODER)     += rkmpp.o rkplane.o rkmppdec.o
 OBJS-$(CONFIG_MPL2_DECODER)            += mpl2dec.o ass.o
 OBJS-$(CONFIG_MSA1_DECODER)            += mss3.o
 OBJS-$(CONFIG_MSCC_DECODER)            += mscc.o
@@ -716,7 +723,8 @@ OBJS-$(CONFIG_VP8_DECODER)             += vp8.o vp56rac.o
 OBJS-$(CONFIG_VP8_CUVID_DECODER)       += cuviddec.o
 OBJS-$(CONFIG_VP8_MEDIACODEC_DECODER)  += mediacodecdec.o
 OBJS-$(CONFIG_VP8_QSV_DECODER)         += qsvdec.o
-OBJS-$(CONFIG_VP8_RKMPP_DECODER)       += rkmppdec.o
+OBJS-$(CONFIG_VP8_RKMPP_DECODER)       += rkmpp.o rkplane.o rkmppdec.o
+OBJS-$(CONFIG_VP8_RKMPP_ENCODER)       += rkmpp.o rkplane.o rkmppenc.o
 OBJS-$(CONFIG_VP8_VAAPI_ENCODER)       += vaapi_encode_vp8.o
 OBJS-$(CONFIG_VP8_V4L2M2M_DECODER)     += v4l2_m2m_dec.o
 OBJS-$(CONFIG_VP8_V4L2M2M_ENCODER)     += v4l2_m2m_enc.o
@@ -725,7 +733,7 @@ OBJS-$(CONFIG_VP9_DECODER)             += vp9.o vp9data.o vp9dsp.o vp9lpf.o vp9r
                                           vp9dsp_8bpp.o vp9dsp_10bpp.o vp9dsp_12bpp.o
 OBJS-$(CONFIG_VP9_CUVID_DECODER)       += cuviddec.o
 OBJS-$(CONFIG_VP9_MEDIACODEC_DECODER)  += mediacodecdec.o
-OBJS-$(CONFIG_VP9_RKMPP_DECODER)       += rkmppdec.o
+OBJS-$(CONFIG_VP9_RKMPP_DECODER)       += rkmpp.o rkplane.o rkmppdec.o
 OBJS-$(CONFIG_VP9_VAAPI_ENCODER)       += vaapi_encode_vp9.o
 OBJS-$(CONFIG_VP9_QSV_ENCODER)         += qsvenc_vp9.o
 OBJS-$(CONFIG_VPLAYER_DECODER)         += textdec.o ass.o
-- 
2.41.0


From f19e87b3dc632156cc0fc9eec9949a6e1e8cddf7 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 24 Jul 2023 16:14:02 +0200
Subject: [PATCH 08/13] register all codec as highest prio

---
 libavcodec/allcodecs.c | 17 +++++++++++++----
 1 file changed, 13 insertions(+), 4 deletions(-)

diff --git a/libavcodec/allcodecs.c b/libavcodec/allcodecs.c
index 2e9a3581de..03a07aaa41 100644
--- a/libavcodec/allcodecs.c
+++ b/libavcodec/allcodecs.c
@@ -28,6 +28,19 @@
 #include "libavutil/thread.h"
 #include "avcodec.h"
 #include "version.h"
+extern AVCodec ff_av1_rkmpp_decoder;
+extern AVCodec ff_h263_rkmpp_decoder;
+extern AVCodec ff_h264_rkmpp_decoder;
+extern AVCodec ff_hevc_rkmpp_decoder;
+extern AVCodec ff_mpeg1_rkmpp_decoder;
+extern AVCodec ff_mpeg2_rkmpp_decoder;
+extern AVCodec ff_mpeg4_rkmpp_decoder;
+extern AVCodec ff_vp8_rkmpp_decoder;
+extern AVCodec ff_vp9_rkmpp_decoder;
+
+extern AVCodec ff_vp8_rkmpp_encoder;
+extern AVCodec ff_h264_rkmpp_encoder;
+extern AVCodec ff_hevc_rkmpp_encoder;
 
 extern AVCodec ff_a64multi_encoder;
 extern AVCodec ff_a64multi5_encoder;
@@ -147,12 +160,10 @@ extern AVCodec ff_h264_v4l2m2m_decoder;
 extern AVCodec ff_h264_mediacodec_decoder;
 extern AVCodec ff_h264_mmal_decoder;
 extern AVCodec ff_h264_qsv_decoder;
-extern AVCodec ff_h264_rkmpp_decoder;
 extern AVCodec ff_hap_encoder;
 extern AVCodec ff_hap_decoder;
 extern AVCodec ff_hevc_decoder;
 extern AVCodec ff_hevc_qsv_decoder;
-extern AVCodec ff_hevc_rkmpp_decoder;
 extern AVCodec ff_hevc_v4l2m2m_decoder;
 extern AVCodec ff_hnm4_video_decoder;
 extern AVCodec ff_hq_hqa_decoder;
@@ -357,10 +368,8 @@ extern AVCodec ff_vp6a_decoder;
 extern AVCodec ff_vp6f_decoder;
 extern AVCodec ff_vp7_decoder;
 extern AVCodec ff_vp8_decoder;
-extern AVCodec ff_vp8_rkmpp_decoder;
 extern AVCodec ff_vp8_v4l2m2m_decoder;
 extern AVCodec ff_vp9_decoder;
-extern AVCodec ff_vp9_rkmpp_decoder;
 extern AVCodec ff_vp9_v4l2m2m_decoder;
 extern AVCodec ff_vqa_decoder;
 extern AVCodec ff_webp_decoder;
-- 
2.41.0


From 6852941a67515c3cb337804679706797575e1fb2 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 24 Jul 2023 16:10:39 +0200
Subject: [PATCH 09/13] initial

---
 libavcodec/rkmpp.c    | 272 +++++++++++++++++
 libavcodec/rkmpp.h    | 279 ++++++++++++++++++
 libavcodec/rkmppdec.c | 335 +++++++++++++++++++++
 libavcodec/rkmppenc.c | 620 ++++++++++++++++++++++++++++++++++++++
 libavcodec/rkplane.c  | 669 ++++++++++++++++++++++++++++++++++++++++++
 libavcodec/rkplane.h  |  17 ++
 6 files changed, 2192 insertions(+)
 create mode 100644 libavcodec/rkmpp.c
 create mode 100644 libavcodec/rkmpp.h
 create mode 100644 libavcodec/rkmppdec.c
 create mode 100644 libavcodec/rkmppenc.c
 create mode 100644 libavcodec/rkplane.c
 create mode 100644 libavcodec/rkplane.h

diff --git a/libavcodec/rkmpp.c b/libavcodec/rkmpp.c
new file mode 100644
index 0000000000..a33a139569
--- /dev/null
+++ b/libavcodec/rkmpp.c
@@ -0,0 +1,272 @@
+/*
+ * RockChip MPP Video Codec
+ * Copyright (c) 2023 Huseyin BIYIK
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include <fcntl.h>
+#include <time.h>
+#include "rkmpp.h"
+
+static rkformat rkformats[13] = {
+        { .av = AV_PIX_FMT_YUV420P, .mpp = MPP_FMT_YUV420P,        .drm = DRM_FORMAT_YUV420,   .rga = RK_FORMAT_YCbCr_420_P},
+        { .av = AV_PIX_FMT_YUV422P, .mpp = MPP_FMT_YUV422P,        .drm = DRM_FORMAT_YUV422,   .rga = RK_FORMAT_YCbCr_422_P},
+        { .av = AV_PIX_FMT_NV12,    .mpp = MPP_FMT_YUV420SP,       .drm = DRM_FORMAT_NV12,     .rga = RK_FORMAT_YCbCr_420_SP},
+        { .av = AV_PIX_FMT_NV16,    .mpp = MPP_FMT_YUV422SP,       .drm = DRM_FORMAT_NV16,     .rga = RK_FORMAT_YCbCr_422_SP},
+        { .av = AV_PIX_FMT_NONE,    .mpp = MPP_FMT_YUV420SP_10BIT, .drm = DRM_FORMAT_NV15,     .rga = RK_FORMAT_YCbCr_420_SP_10B},
+        { .av = AV_PIX_FMT_BGR24,   .mpp = MPP_FMT_BGR888,         .drm = DRM_FORMAT_BGR888,   .rga = RK_FORMAT_BGR_888},
+        { .av = AV_PIX_FMT_BGR0,    .mpp = MPP_FMT_BGRA8888,       .drm = DRM_FORMAT_XRGB8888, .rga = RK_FORMAT_BGRX_8888},
+        { .av = AV_PIX_FMT_BGRA,    .mpp = MPP_FMT_BGRA8888,       .drm = DRM_FORMAT_ARGB8888, .rga = RK_FORMAT_BGRA_8888},
+        { .av = AV_PIX_FMT_BGR565,  .mpp = MPP_FMT_BGR565,         .drm = DRM_FORMAT_BGR565,   .rga = RK_FORMAT_BGR_565},
+        { .av = AV_PIX_FMT_YUYV422, .mpp = MPP_FMT_YUV422_YUYV,    .drm = DRM_FORMAT_YUYV,     .rga = RK_FORMAT_YUYV_422},
+        { .av = AV_PIX_FMT_UYVY422, .mpp = MPP_FMT_YUV422_UYVY,    .drm = DRM_FORMAT_UYVY,     .rga = RK_FORMAT_UYVY_422},
+        { .av = AV_PIX_FMT_NV24,    .mpp = MPP_FMT_YUV444SP,       .drm = DRM_FORMAT_NV24,     .rga = RK_FORMAT_UNKNOWN},
+        { .av = AV_PIX_FMT_YUV444P, .mpp = MPP_FMT_YUV444P,        .drm = DRM_FORMAT_YUV444,   .rga = RK_FORMAT_UNKNOWN},
+};
+
+#define GETFORMAT(NAME, TYPE)\
+int rkmpp_get_##NAME##_format(rkformat *format, TYPE informat){ \
+    for(int i=0; i < 13; i++){ \
+        if(rkformats[i].NAME == informat){ \
+            format->av = rkformats[i].av;\
+            format->mpp = rkformats[i].mpp;\
+            format->drm = rkformats[i].drm;\
+            format->rga = rkformats[i].rga;\
+            return 0;\
+        }\
+    }\
+    return -1;\
+}
+
+GETFORMAT(drm, uint32_t)
+GETFORMAT(mpp, MppFrameFormat)
+GETFORMAT(rga, enum _Rga_SURF_FORMAT)
+GETFORMAT(av, enum AVPixelFormat)
+
+MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx)
+{
+    switch (avctx->codec_id) {
+    case AV_CODEC_ID_H263:          return MPP_VIDEO_CodingH263;
+    case AV_CODEC_ID_H264:          return MPP_VIDEO_CodingAVC;
+    case AV_CODEC_ID_HEVC:          return MPP_VIDEO_CodingHEVC;
+    case AV_CODEC_ID_AV1:           return MPP_VIDEO_CodingAV1;
+    case AV_CODEC_ID_VP8:           return MPP_VIDEO_CodingVP8;
+    case AV_CODEC_ID_VP9:           return MPP_VIDEO_CodingVP9;
+    case AV_CODEC_ID_MPEG1VIDEO:    /* fallthrough */
+    case AV_CODEC_ID_MPEG2VIDEO:    return MPP_VIDEO_CodingMPEG2;
+    case AV_CODEC_ID_MPEG4:         return MPP_VIDEO_CodingMPEG4;
+    default:                        return MPP_VIDEO_CodingUnused;
+    }
+}
+
+int rkmpp_close_codec(AVCodecContext *avctx)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+
+    av_packet_unref(&codec->lastpacket);
+    av_frame_unref(&codec->lastframe);
+
+    av_buffer_unref(&rk_context->codec_ref);
+    return 0;
+}
+
+void rkmpp_release_codec(void *opaque, uint8_t *data)
+{
+    RKMPPCodec *codec = (RKMPPCodec *)data;
+
+    if (codec->mpi) {
+        codec->mpi->reset(codec->ctx);
+        mpp_destroy(codec->ctx);
+        codec->ctx = NULL;
+    }
+
+    if (codec->buffer_group) {
+        mpp_buffer_group_put(codec->buffer_group);
+        codec->buffer_group = NULL;
+    }
+
+    if(codec->hwframes_ref)
+        av_buffer_unref(&codec->hwframes_ref);
+    if(codec->hwdevice_ref)
+        av_buffer_unref(&codec->hwdevice_ref);
+
+    av_free(codec);
+}
+
+int rkmpp_init_codec(AVCodecContext *avctx)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = NULL;
+    MppCodingType codectype = MPP_VIDEO_CodingUnused;
+    char *env;
+    int ret;
+
+    // create a codec and a ref to it
+    codec = av_mallocz(sizeof(RKMPPCodec));
+    if (!codec) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    rk_context->codec_ref = av_buffer_create((uint8_t *)codec, sizeof(*codec), rkmpp_release_codec,
+                                               NULL, AV_BUFFER_FLAG_READONLY);
+    if (!rk_context->codec_ref) {
+        av_free(codec);
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    env = getenv("FFMPEG_RKMPP_LOG_FPS");
+    if (env != NULL)
+        codec->print_fps = !!atoi(env);
+
+    av_log(avctx, AV_LOG_DEBUG, "Initializing RKMPP Codec.\n");
+
+    codectype = rkmpp_get_codingtype(avctx);
+    if (codectype == MPP_VIDEO_CodingUnused) {
+        av_log(avctx, AV_LOG_ERROR, "Unknown codec type (%d).\n", avctx->codec_id);
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    ret = mpp_check_support_format(codec->mppctxtype, codectype);
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Codec type (%d) unsupported by MPP\n", avctx->codec_id);
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    // Create the MPP context
+    ret = mpp_create(&codec->ctx, &codec->mpi);
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to create MPP context (code = %d).\n", ret);
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    if(avctx->codec->receive_frame){
+        codec->init_callback = rkmpp_init_decoder;
+        codec->mppctxtype = MPP_CTX_DEC;
+
+        ret = 1;
+        codec->mpi->control(codec->ctx, MPP_DEC_SET_PARSER_FAST_MODE, &ret);
+
+        avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
+
+        // override the the pixfmt according env variable
+        env = getenv("FFMPEG_RKMPP_PIXFMT");
+        if(env != NULL){
+            if(!strcmp(env, "YUV420P"))
+                avctx->pix_fmt = AV_PIX_FMT_YUV420P;
+            else if (!strcmp(env, "NV12"))
+                avctx->pix_fmt = AV_PIX_FMT_NV12;
+            else if(!strcmp(env, "DRMPRIME"))
+                avctx->pix_fmt = AV_PIX_FMT_DRM_PRIME;
+        }
+    } else if (avctx->codec->encode2){
+        codec->mppctxtype = MPP_CTX_ENC;
+        codec->init_callback = rkmpp_init_encoder;
+    } else {
+        ret = AVERROR(ENOMEM);
+        av_log(avctx, AV_LOG_DEBUG, "RKMPP Codec can not determine if the mode is decoder or encoder\n");
+        goto fail;
+    }
+
+    av_log(avctx, AV_LOG_INFO, "Picture format is %s.\n", av_get_pix_fmt_name(avctx->pix_fmt));
+
+    // initialize mpp
+    ret = mpp_init(codec->ctx, codec->mppctxtype, codectype);
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to initialize MPP context (code = %d).\n", ret);
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    env = getenv("FFMPEG_RKMPP_NORGA");
+    if(env != NULL){
+        codec->norga = 1;
+        av_log(avctx, AV_LOG_INFO, "Bypassing RGA and using libyuv soft conversion\n");
+    }
+
+    ret = mpp_buffer_group_get_internal(&codec->buffer_group, MPP_BUFFER_TYPE_ION | MPP_BUFFER_FLAGS_DMA32);
+    if (ret) {
+       av_log(avctx, AV_LOG_ERROR, "Failed to get buffer group (code = %d)\n", ret);
+       ret = AVERROR_UNKNOWN;
+       goto fail;
+    }
+
+    ret = codec->init_callback(avctx);
+    if(ret){
+        av_log(avctx, AV_LOG_ERROR, "Failed to init Codec (code = %d).\n", ret);
+        goto fail;
+    }
+
+    return 0;
+
+fail:
+    av_log(avctx, AV_LOG_ERROR, "Failed to initialize RKMPP Codec.\n");
+    rkmpp_close_codec(avctx);
+    return ret;
+}
+
+void rkmpp_flush(AVCodecContext *avctx)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+
+    av_log(avctx, AV_LOG_DEBUG, "Flush.\n");
+
+    codec->mpi->reset(codec->ctx);
+    codec->last_frame_time = codec->frames = codec->hascfg = 0;
+
+    av_packet_unref(&codec->lastpacket);
+    av_frame_unref(&codec->lastframe);
+}
+
+uint64_t rkmpp_update_latency(AVCodecContext *avctx, int latency)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    struct timespec tv;
+    uint64_t curr_time;
+    float fps = 0.0f;
+
+    if (!codec->print_fps)
+        return 0;
+
+    clock_gettime(CLOCK_MONOTONIC, &tv);
+    curr_time = tv.tv_sec * 10e5 + tv.tv_nsec / 10e2;
+    if (latency == -1){
+        latency = codec->last_frame_time ? curr_time - codec->last_frame_time : 0;
+        codec->last_frame_time = curr_time;
+        codec->latencies[codec->frames % RKMPP_FPS_FRAME_MACD] = latency;
+        return latency;
+    } else if (latency == 0 || codec->frames < RKMPP_FPS_FRAME_MACD) {
+        fps = -1.0f;
+    } else {
+       for(int i = 0; i < RKMPP_FPS_FRAME_MACD; i++) {
+          fps += codec->latencies[i];
+       }
+        fps = RKMPP_FPS_FRAME_MACD * 1000000.0f / fps;
+    }
+    av_log(avctx, AV_LOG_INFO,
+           "[FFMPEG RKMPP] FPS(MACD%d): %6.1f || Frames: %" PRIu64 " || Latency: %d us || Buffer Delay %" PRIu64 "us\n",
+           RKMPP_FPS_FRAME_MACD, fps, codec->frames, latency, (uint64_t)(curr_time - codec->last_frame_time));
+
+    return 0;
+}
diff --git a/libavcodec/rkmpp.h b/libavcodec/rkmpp.h
new file mode 100644
index 0000000000..1e4280dc3b
--- /dev/null
+++ b/libavcodec/rkmpp.h
@@ -0,0 +1,279 @@
+#include <drm_fourcc.h>
+#include <rockchip/rk_mpi.h>
+#include <unistd.h>
+#include <stdint.h>
+
+#include "internal.h"
+#include "avcodec.h"
+#include "hwconfig.h"
+#include "decode.h"
+#include "encode.h"
+#include "rga/RgaApi.h"
+#include "libavutil/macros.h"
+#include "libavutil/log.h"
+#include "libavutil/opt.h"
+#include "libavutil/buffer.h"
+#include "libavutil/pixfmt.h"
+#include "libavutil/pixdesc.h"
+#include "libavutil/hwcontext_drm.h"
+
+// HACK: Older BSP kernel use NA12 for NV15.
+#ifndef DRM_FORMAT_NV15 // fourcc_code('N', 'V', '1', '5')
+#define DRM_FORMAT_NV15 fourcc_code('N', 'A', '1', '2')
+#endif
+
+#define RKMPP_FPS_FRAME_MACD 30
+#define RKMPP_STRIDE_ALIGN 16
+#define RKMPP_RGA_MIN_SIZE 128
+#define RKMPP_RGA_MAX_SIZE 4096
+#define RKMPP_MPPFRAME_BUFINDEX 7
+#define HDR_SIZE 1024
+#define QMAX_H26x 51
+#define QMIN_H26x 10
+#define QMAX_VPx 127
+#define QMIN_VPx 40
+#define QMAX_JPEG 99
+#define QMIN_JPEG 1
+
+
+#define DRMFORMATNAME(buf, format) \
+    buf[0] = format & 0xff; \
+    buf[1] = (format >> 8) & 0xff; \
+    buf[2] = (format >> 16) & 0xff; \
+    buf[3] = (format >> 24) & 0x7f; \
+
+typedef struct {
+    AVClass *av_class;
+    AVBufferRef *codec_ref;
+    int rc_mode;
+    int profile;
+    int qmin;
+    int qmax;
+    int level;
+    int coder;
+    int dct8x8;
+    enum AVPixelFormat postrga_format;
+    int postrga_width;
+    int postrga_height;
+} RKMPPCodecContext;
+
+typedef struct {
+    MppCtx ctx;
+    MppApi *mpi;
+    MppBufferGroup buffer_group;
+    MppCtxType mppctxtype;
+    MppEncCfg enccfg;
+    int hascfg;
+    int64_t ptsstep;
+    int64_t pts;
+
+    AVPacket lastpacket;
+    AVFrame lastframe;
+    AVBufferRef *hwframes_ref;
+    AVBufferRef *hwdevice_ref;
+
+    char print_fps;
+    uint64_t last_frame_time;
+    uint64_t frames;
+    uint64_t latencies[RKMPP_FPS_FRAME_MACD];
+
+    int8_t norga;
+    int (*init_callback)(struct AVCodecContext *avctx);
+} RKMPPCodec;
+
+typedef struct {
+    enum AVPixelFormat av;
+    MppFrameFormat mpp;
+    uint32_t drm;
+    enum _Rga_SURF_FORMAT rga;
+} rkformat;
+
+MppCodingType rkmpp_get_codingtype(AVCodecContext *avctx);
+int rkmpp_get_drm_format(rkformat *format, uint32_t informat);
+int rkmpp_get_mpp_format(rkformat *format, MppFrameFormat informat);
+int rkmpp_get_rga_format(rkformat *format, enum _Rga_SURF_FORMAT informat);
+int rkmpp_get_av_format(rkformat *format, enum AVPixelFormat informat);
+int rkmpp_init_encoder(AVCodecContext *avctx);
+int rkmpp_encode(AVCodecContext *avctx, AVPacket *packet, const AVFrame *frame, int *got_packet);
+int rkmpp_init_decoder(AVCodecContext *avctx);
+int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame);
+int rkmpp_init_codec(AVCodecContext *avctx);
+int rkmpp_close_codec(AVCodecContext *avctx);
+void rkmpp_release_codec(void *opaque, uint8_t *data);
+void rkmpp_flush(AVCodecContext *avctx);
+uint64_t rkmpp_update_latency(AVCodecContext *avctx, int latency);
+
+#define OFFSET(x) offsetof(RKMPPCodecContext, x)
+#define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
+
+#define ENCODEROPTS() \
+    { "rc_mode", "Set rate control mode", OFFSET(rc_mode), AV_OPT_TYPE_INT, \
+            { .i64 = MPP_ENC_RC_MODE_CBR }, MPP_ENC_RC_MODE_VBR, MPP_ENC_RC_MODE_BUTT, VE, "rc_mode"}, \
+        {"VBR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MPP_ENC_RC_MODE_VBR }, 0, 0, VE, "rc_mode" }, \
+        {"CBR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MPP_ENC_RC_MODE_CBR }, 0, 0, VE, "rc_mode" }, \
+        {"CQP", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MPP_ENC_RC_MODE_FIXQP }, 0, 0, VE, "rc_mode" }, \
+        {"AVBR", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = MPP_ENC_RC_MODE_AVBR }, 0, 0, VE, "rc_mode" }, \
+    { "quality_min", "Minimum Quality", OFFSET(qmin), AV_OPT_TYPE_INT, \
+        { .i64=50 }, 0, 100, VE, "qmin"}, \
+    { "quality_max", "Maximum Quality", OFFSET(qmax), AV_OPT_TYPE_INT, \
+            { .i64=100 }, 0, 100, VE, "qmax"}, \
+    { "width", "scale to Width", OFFSET(postrga_width), AV_OPT_TYPE_INT, \
+             { .i64=0 }, 0, RKMPP_RGA_MAX_SIZE, VE, "width"}, \
+    { "height", "scale to Height", OFFSET(postrga_height), AV_OPT_TYPE_INT, \
+             { .i64=0 }, 0, RKMPP_RGA_MAX_SIZE, VE, "height"},
+
+static const AVOption options_h264_encoder[] = {
+    ENCODEROPTS()
+    { "profile", "Set profile restrictions", OFFSET(profile), AV_OPT_TYPE_INT,
+            { .i64=FF_PROFILE_H264_HIGH }, -1, FF_PROFILE_H264_HIGH, VE, "profile"},
+        { "baseline",   NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_PROFILE_H264_BASELINE},  INT_MIN, INT_MAX, VE, "profile" },
+        { "main",       NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_PROFILE_H264_MAIN},      INT_MIN, INT_MAX, VE, "profile" },
+        { "high",       NULL, 0, AV_OPT_TYPE_CONST, {.i64 = FF_PROFILE_H264_HIGH},      INT_MIN, INT_MAX, VE, "profile" },
+    { "level", "Compression Level", OFFSET(level), AV_OPT_TYPE_INT,
+            { .i64 = 0 }, FF_LEVEL_UNKNOWN, 0xff, VE, "level"},
+        { "1",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 10 }, 0, 0, VE, "level"},
+        { "1.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 11 }, 0, 0, VE, "level"},
+        { "1.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 12 }, 0, 0, VE, "level"},
+        { "1.3",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 13 }, 0, 0, VE, "level"},
+        { "2",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 20 }, 0, 0, VE, "level"},
+        { "2.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 21 }, 0, 0, VE, "level"},
+        { "2.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 22 }, 0, 0, VE, "level"},
+        { "3",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, 0, 0, VE, "level"},
+        { "3.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 31 }, 0, 0, VE, "level"},
+        { "3.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 32 }, 0, 0, VE, "level"},
+        { "4",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 40 }, 0, 0, VE, "level"},
+        { "4.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 41 }, 0, 0, VE, "level"},
+        { "4.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 42 }, 0, 0, VE, "level"},
+        { "5",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 50 }, 0, 0, VE, "level"},
+        { "5.1",           NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 51 }, 0, 0, VE, "level"},
+        { "5.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 52 }, 0, 0, VE, "level"},
+        { "6",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 60 }, 0, 0, VE, "level"},
+        { "6.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 61 }, 0, 0, VE, "level"},
+        { "6.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 62 }, 0, 0, VE, "level"},
+    { "coder", "Entropy coder type (from 0 to 1) (default cabac)", OFFSET(coder), AV_OPT_TYPE_INT,
+            { .i64 = 1 }, 0, 1, VE, "coder"},
+        { "cavlc", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 0 }, INT_MIN, INT_MAX, VE, "coder" },
+        { "cabac", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 1 }, INT_MIN, INT_MAX, VE, "coder" },
+    { "8x8dct", "High profile 8x8 transform.",  OFFSET(dct8x8), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE},
+    { NULL }
+};
+
+static const AVOption options_hevc_encoder[] = {
+    ENCODEROPTS()
+    { "level", "Compression Level", OFFSET(level), AV_OPT_TYPE_INT,
+            { .i64 = 0 }, FF_LEVEL_UNKNOWN, 0xff, VE, "level"},
+        { "1",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 30 }, 0, 0, VE, "level"},
+        { "2",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 60 }, 0, 0, VE, "level"},
+        { "2.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 63 }, 0, 0, VE, "level"},
+        { "3",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 90 }, 0, 0, VE, "level"},
+        { "3.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 93 }, 0, 0, VE, "level"},
+        { "4",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 120 }, 0, 0, VE, "level"},
+        { "4.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 123 }, 0, 0, VE, "level"},
+        { "5",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 150 }, 0, 0, VE, "level"},
+        { "5.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 153 }, 0, 0, VE, "level"},
+        { "5.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 156 }, 0, 0, VE, "level"},
+        { "6",          NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 180 }, 0, 0, VE, "level"},
+        { "6.1",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 183 }, 0, 0, VE, "level"},
+        { "6.2",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 186 }, 0, 0, VE, "level"},
+        { "8.5",        NULL, 0, AV_OPT_TYPE_CONST, { .i64 = 255 }, 0, 0, VE, "level"},
+    { NULL }
+};
+
+static const AVOption options_vp8_encoder[] = {
+    ENCODEROPTS()
+    { NULL }
+};
+
+#define DECODEROPTIONS(NAME, TYPE) \
+static const AVOption options_##NAME##_##TYPE[] = { \
+            { NULL } \
+        };
+
+DECODEROPTIONS(h263, decoder);
+DECODEROPTIONS(h264, decoder);
+DECODEROPTIONS(hevc, decoder);
+DECODEROPTIONS(av1, decoder);
+DECODEROPTIONS(vp8, decoder);
+DECODEROPTIONS(vp9, decoder);
+DECODEROPTIONS(mpeg1, decoder);
+DECODEROPTIONS(mpeg2, decoder);
+DECODEROPTIONS(mpeg4, decoder);
+
+static const AVCodecDefault rkmpp_enc_defaults[] = {
+    { "b",  "6M" },
+    { "g",  "60" },
+    { NULL }
+};
+
+static const enum AVPixelFormat rkmppvepu1formats[] = {
+        AV_PIX_FMT_NV16,
+        AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_YUYV422,
+        AV_PIX_FMT_UYVY422,
+        AV_PIX_FMT_BGRA,
+        AV_PIX_FMT_BGR0,
+        AV_PIX_FMT_NV12,
+        AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_DRM_PRIME,
+        AV_PIX_FMT_NONE,
+};
+
+static const enum AVPixelFormat rkmppvepu5formats[] = {
+        AV_PIX_FMT_NV24,
+        AV_PIX_FMT_YUV444P,
+        AV_PIX_FMT_NV16,
+        AV_PIX_FMT_YUV422P,
+        AV_PIX_FMT_BGR24,
+        AV_PIX_FMT_YUYV422,
+        AV_PIX_FMT_UYVY422,
+        AV_PIX_FMT_BGRA,
+        AV_PIX_FMT_BGR0,
+        AV_PIX_FMT_NV12,
+        AV_PIX_FMT_YUV420P,
+        AV_PIX_FMT_DRM_PRIME,
+        AV_PIX_FMT_NONE,
+};
+
+#define RKMPP_CODEC(NAME, ID, BSFS, TYPE) \
+        static const AVClass rkmpp_##NAME##_##TYPE##_class = { \
+            .class_name = "rkmpp_" #NAME "_" #TYPE, \
+            .item_name  = av_default_item_name,\
+            .option     = options_##NAME##_##TYPE, \
+            .version    = LIBAVUTIL_VERSION_INT, \
+        }; \
+        const AVCodec ff_##NAME##_rkmpp_##TYPE = { \
+            .name           = #NAME "_rkmpp_" #TYPE, \
+            .long_name      = #NAME " (rkmpp " #TYPE " )", \
+            .type           = AVMEDIA_TYPE_VIDEO, \
+            .id             = ID, \
+            .priv_data_size = sizeof(RKMPPCodecContext), \
+            .init           = rkmpp_init_codec, \
+            .close          = rkmpp_close_codec, \
+            .flush          = rkmpp_flush, \
+            .priv_class     = &rkmpp_##NAME##_##TYPE##_class, \
+            .bsfs           = BSFS, \
+            .wrapper_name   = "rkmpp",
+
+
+#define RKMPP_DEC(NAME, ID, BSFS) \
+        RKMPP_CODEC(NAME, ID, BSFS, decoder) \
+        .receive_frame  = rkmpp_receive_frame, \
+        .capabilities   = AV_CODEC_CAP_DELAY | AV_CODEC_CAP_AVOID_PROBING | AV_CODEC_CAP_HARDWARE, \
+        .pix_fmts        = (const enum AVPixelFormat[]) { AV_PIX_FMT_DRM_PRIME, \
+                                                         AV_PIX_FMT_NV12, \
+                                                         AV_PIX_FMT_YUV420P, \
+                                                         AV_PIX_FMT_NONE}, \
+        .hw_configs     = (const AVCodecHWConfigInternal *const []) { HW_CONFIG_INTERNAL(DRM_PRIME), \
+                                                                      HW_CONFIG_INTERNAL(NV12), \
+                                                                      NULL}, \
+    };
+
+#define RKMPP_ENC(NAME, ID, VEPU) \
+        RKMPP_CODEC(NAME, ID, NULL, encoder) \
+        .encode2        = rkmpp_encode, \
+        .capabilities = AV_CODEC_CAP_HARDWARE, \
+        .defaults       = rkmpp_enc_defaults, \
+        .pix_fmts     = rkmpp##VEPU##formats, \
+        .hw_configs     = (const AVCodecHWConfigInternal *const []) { HW_CONFIG_INTERNAL(NV12), \
+                                                                      NULL}, \
+    };
diff --git a/libavcodec/rkmppdec.c b/libavcodec/rkmppdec.c
new file mode 100644
index 0000000000..acec713140
--- /dev/null
+++ b/libavcodec/rkmppdec.c
@@ -0,0 +1,335 @@
+/*
+ * RockChip MPP Video Decoder
+ * Copyright (c) 2017 Lionel CHAZALLON
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include "rkmpp.h"
+#include "rkplane.h"
+
+
+int rkmpp_init_decoder(AVCodecContext *avctx){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+
+    int ret;
+
+    ret = codec->mpi->control(codec->ctx, MPP_DEC_SET_EXT_BUF_GROUP, codec->buffer_group);
+    if (ret) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to assign buffer group (code = %d)\n", ret);
+        return AVERROR_UNKNOWN;
+    }
+
+    codec->mpi->control(codec->ctx, MPP_DEC_SET_DISABLE_ERROR, NULL);
+    if (ret < 0) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to prepare Codec (code = %d)\n", ret);
+        return AVERROR_UNKNOWN;
+    }
+
+    avctx->coded_width = FFALIGN(avctx->width, 64);
+    avctx->coded_height = FFALIGN(avctx->height, 64);
+
+    codec->hwdevice_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_DRM);
+    if (!codec->hwdevice_ref) {
+        return AVERROR(ENOMEM);
+    }
+
+    ret = av_hwdevice_ctx_init(codec->hwdevice_ref);
+    if (ret < 0)
+        return ret;
+
+    av_buffer_unref(&codec->hwframes_ref);
+    codec->hwframes_ref = av_hwframe_ctx_alloc(codec->hwdevice_ref);
+    if (!codec->hwframes_ref) {
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+static int rkmpp_get_frame(AVCodecContext *avctx, AVFrame *frame, int timeout)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppFrameFormat mpp_format;
+    MppFrame mppframe = NULL;
+    MppBuffer buffer = NULL;
+    rkformat format;
+    int ret, latency;
+    int mpp_width, mpp_height, mpp_mode;
+    enum AVColorRange mpp_color_range;
+    enum AVColorPrimaries mpp_color_primaries;
+    enum AVColorTransferCharacteristic mpp_color_trc;
+    enum AVColorSpace mpp_color_space;
+    int64_t mpp_pts;
+
+    codec->mpi->control(codec->ctx, MPP_SET_OUTPUT_TIMEOUT, (MppParam)&timeout);
+
+    ret = codec->mpi->decode_get_frame(codec->ctx, &mppframe);
+    if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to get frame (code = %d)\n", ret);
+        return AVERROR_UNKNOWN;
+    }
+
+    if (!mppframe) {
+        av_log(avctx, AV_LOG_DEBUG, "Timeout getting decoded frame.\n");
+        return AVERROR(EAGAIN);
+    }
+
+    if (mpp_frame_get_eos(mppframe)) {
+        av_log(avctx, AV_LOG_DEBUG, "Received a EOS frame.\n");
+        ret = AVERROR_EOF;
+        goto clean;
+    }
+
+    if (mpp_frame_get_discard(mppframe)) {
+        av_log(avctx, AV_LOG_DEBUG, "Received a discard frame.\n");
+        ret = AVERROR(EAGAIN);
+        goto clean;
+    }
+
+    if (mpp_frame_get_errinfo(mppframe)) {
+        av_log(avctx, AV_LOG_ERROR, "Received a errinfo frame.\n");
+        ret = AVERROR_UNKNOWN;
+        goto clean;
+    }
+
+     mpp_format = mpp_frame_get_fmt(mppframe) & MPP_FRAME_FMT_MASK;
+
+    if (mpp_frame_get_info_change(mppframe)) {
+        if(codec->hascfg)
+            ret = AVERROR(EAGAIN);
+        else{
+            if (avctx->pix_fmt == AV_PIX_FMT_DRM_PRIME){
+                char drmname[4];
+                AVHWFramesContext *hwframes;
+                rkmpp_get_mpp_format(&format, mpp_format);
+                DRMFORMATNAME(drmname, format.drm)
+
+                hwframes = (AVHWFramesContext*)codec->hwframes_ref->data;
+                hwframes->format    = AV_PIX_FMT_DRM_PRIME;
+                hwframes->sw_format = format.av;
+                hwframes->width     = avctx->width;
+                hwframes->height    = avctx->height;
+                ret = av_hwframe_ctx_init(codec->hwframes_ref);
+                // FIXME: handle error
+                av_log(avctx, AV_LOG_INFO, "Decoder is set to DRM Prime with format %s.\n", drmname);
+            } else if (mpp_format == MPP_FMT_YUV420SP_10BIT)
+                av_log(avctx, AV_LOG_WARNING, "10bit NV15 plane will be downgraded to 8bit %s.\n", av_get_pix_fmt_name(avctx->pix_fmt));
+            codec->hascfg = 1;
+        }
+
+        av_log(avctx, AV_LOG_INFO, "Decoder noticed an info change\n");
+        codec->mpi->control(codec->ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
+        goto clean;
+    }
+
+    // here we should have a valid frame
+    av_log(avctx, AV_LOG_DEBUG, "Received a frame.\n");
+
+    // now setup the frame buffer info
+    buffer = mpp_frame_get_buffer(mppframe);
+    if (!buffer) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to get the frame buffer, frame is dropped (code = %d)\n", ret);
+        ret = AVERROR(EAGAIN);
+        goto clean;
+    }
+
+    latency = rkmpp_update_latency(avctx, -1);
+    mpp_pts = mpp_frame_get_pts(mppframe);
+    mpp_width = mpp_frame_get_width(mppframe);
+    mpp_height = mpp_frame_get_height(mppframe);
+    mpp_color_range = mpp_frame_get_color_range(mppframe);
+    mpp_color_primaries = mpp_frame_get_color_primaries(mppframe);
+    mpp_color_trc = mpp_frame_get_color_trc(mppframe);
+    mpp_color_space = mpp_frame_get_colorspace(mppframe);
+    mpp_mode = mpp_frame_get_mode(mppframe);
+
+    if (avctx->pix_fmt == AV_PIX_FMT_DRM_PRIME){
+        ret = import_mpp_to_drm(avctx, mppframe, frame);
+    } else if (mpp_format == MPP_FMT_YUV420SP_10BIT && avctx->pix_fmt == AV_PIX_FMT_NV12){
+        ret = mpp_nv15_av_nv12(avctx, mppframe, frame);
+    } else if (mpp_format == MPP_FMT_YUV420SP_10BIT && avctx->pix_fmt == AV_PIX_FMT_YUV420P){
+        ret = mpp_nv15_av_yuv420p(avctx, mppframe, frame);
+    } else if (mpp_format == MPP_FMT_YUV420SP && avctx->pix_fmt == AV_PIX_FMT_NV12){
+        ret = mpp_nv12_av_nv12(avctx, mppframe, frame);
+    } else {
+        rkmpp_get_mpp_format(&format, mpp_format);
+        ret = convert_mpp_to_av(avctx, mppframe, frame, format.av, avctx->pix_fmt);
+    }
+
+    if(ret < 0){
+        av_log(avctx, AV_LOG_ERROR, "Failed set frame buffer (code = %d)\n", ret);
+        return ret;
+    }
+
+    // setup general frame fields
+    frame->format = avctx->pix_fmt;
+    frame->width = mpp_width;
+    frame->height = mpp_height;
+    frame->color_range = mpp_color_range;
+    frame->color_primaries = mpp_color_primaries;
+    frame->color_trc = mpp_color_trc;
+    frame->colorspace = mpp_color_space;
+    frame->pts = mpp_pts;
+
+    // when mpp can not determine the color space, it returns reserved (0) value
+    // firefox does not understand this and instead expect unspecified (2) values
+    frame->color_primaries = frame->color_primaries == AVCOL_PRI_RESERVED0 ? AVCOL_PRI_UNSPECIFIED : frame->color_primaries;
+    frame->color_trc = frame->color_trc == AVCOL_TRC_RESERVED0 ? AVCOL_TRC_UNSPECIFIED : frame->color_trc;
+    frame->colorspace = frame->colorspace == AVCOL_SPC_RGB ? AVCOL_SPC_UNSPECIFIED: frame->color_trc;
+
+    frame->interlaced_frame = ((mpp_mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_DEINTERLACED);
+    frame->top_field_first  = ((mpp_mode & MPP_FRAME_FLAG_FIELD_ORDER_MASK) == MPP_FRAME_FLAG_TOP_FIRST);
+
+    codec->frames++;
+    rkmpp_update_latency(avctx, latency);
+    return 0;
+
+clean:
+    if (mppframe)
+        mpp_frame_deinit(&mppframe);
+    return ret;
+}
+
+static int rkmpp_send_packet(AVCodecContext *avctx, AVPacket *packet)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppPacket mpkt;
+    int64_t pts = packet->pts;
+    int ret;
+
+    if(pts == AV_NOPTS_VALUE || pts < 0){
+        if(!codec->ptsstep && avctx->framerate.den && avctx->framerate.num){
+            int64_t x = avctx->pkt_timebase.den * (int64_t)avctx->framerate.den;
+            int64_t y = avctx->pkt_timebase.num * (int64_t)avctx->framerate.num;
+            codec->ptsstep = x / y;
+        }
+        if(codec->ptsstep && (packet->dts == AV_NOPTS_VALUE || packet->dts < 0)){
+            pts = codec->pts;
+            codec->pts += codec->ptsstep;
+        } else {
+            codec->pts = packet->dts;
+            pts = packet->dts;
+        }
+    }
+
+    ret = mpp_packet_init(&mpkt, packet->data, packet->size);
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to init MPP packet (code = %d)\n", ret);
+        return AVERROR_UNKNOWN;
+    }
+
+    mpp_packet_set_pts(mpkt, pts);
+
+    ret = codec->mpi->decode_put_packet(codec->ctx, mpkt);
+    mpp_packet_deinit(&mpkt);
+
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_TRACE, "Decoder buffer full\n");
+        return AVERROR(EAGAIN);
+    }
+
+    av_log(avctx, AV_LOG_DEBUG, "Wrote %d bytes to decoder\n", packet->size);
+    return 0;
+}
+
+static int rkmpp_send_eos(AVCodecContext *avctx)
+{
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppPacket mpkt;
+    int ret;
+
+    ret = mpp_packet_init(&mpkt, NULL, 0);
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to init EOS packet (code = %d)\n", ret);
+        return AVERROR_UNKNOWN;
+    }
+
+    mpp_packet_set_eos(mpkt);
+
+    do {
+        ret = codec->mpi->decode_put_packet(codec->ctx, mpkt);
+    } while (ret != MPP_OK);
+    mpp_packet_deinit(&mpkt);
+
+    return 0;
+}
+
+int rkmpp_receive_frame(AVCodecContext *avctx, AVFrame *frame)
+{
+    AVCodecInternal *avci = avctx->internal;
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    AVPacket *packet = &codec->lastpacket;
+    int ret_send, ret_get;
+
+    // get packet if not already available from previous iteration
+    if (!avci->draining){
+        if(!packet->size){
+            switch(ff_decode_get_packet(avctx, packet)){
+                case AVERROR_EOF:
+                    av_log(avctx, AV_LOG_DEBUG, "Decoder Draining.\n");
+                    return rkmpp_send_eos(avctx);
+                case AVERROR(EAGAIN):
+                    av_log(avctx, AV_LOG_TRACE, "Decoder Can't get packet retrying.\n");
+                    return AVERROR(EAGAIN);
+                }
+        }
+
+sendpacket:
+        // there is definitely a packet to send to decoder here
+        ret_send = rkmpp_send_packet(avctx, packet);
+        if (ret_send == 0){
+            // send successful, continue until decoder input buffer is full
+            av_packet_unref(packet);
+            return AVERROR(EAGAIN);
+        } else if (ret_send < 0 && ret_send != AVERROR(EAGAIN)) {
+            // something went wrong, raise error
+            av_log(avctx, AV_LOG_ERROR, "Decoder Failed to send data (code = %d)\n", ret_send);
+            return ret_send;
+        }
+    }
+
+    // were here only when draining and buffer is full
+    ret_get = rkmpp_get_frame(avctx, frame, MPP_TIMEOUT_BLOCK);
+
+    if (ret_get == AVERROR_EOF){
+        av_log(avctx, AV_LOG_DEBUG, "Decoder is at EOS.\n");
+    // this is not likely but lets handle it in case synchronization issues of mpp
+    } else if (ret_get == AVERROR(EAGAIN) && ret_send == AVERROR(EAGAIN))
+        goto sendpacket;
+    // only for logging
+    else if (ret_get < 0 && ret_get != AVERROR(EAGAIN)) // FIXME
+        av_log(avctx, AV_LOG_ERROR, "Decoder Failed to get frame (code = %d)\n", ret_get);
+
+   return ret_get;
+}
+
+
+RKMPP_DEC(h263, AV_CODEC_ID_H263, NULL)
+RKMPP_DEC(h264, AV_CODEC_ID_H264, "h264_mp4toannexb")
+RKMPP_DEC(hevc, AV_CODEC_ID_HEVC, "hevc_mp4toannexb")
+RKMPP_DEC(av1, AV_CODEC_ID_AV1, NULL)
+RKMPP_DEC(vp8,AV_CODEC_ID_VP8, NULL)
+RKMPP_DEC(vp9, AV_CODEC_ID_VP9, NULL)
+RKMPP_DEC(mpeg1, AV_CODEC_ID_MPEG1VIDEO, NULL)
+RKMPP_DEC(mpeg2, AV_CODEC_ID_MPEG2VIDEO, NULL)
+RKMPP_DEC(mpeg4,AV_CODEC_ID_MPEG4, "mpeg4_unpack_bframes")
+
diff --git a/libavcodec/rkmppenc.c b/libavcodec/rkmppenc.c
new file mode 100644
index 0000000000..f4da62cca7
--- /dev/null
+++ b/libavcodec/rkmppenc.c
@@ -0,0 +1,620 @@
+/*
+ * RockChip MPP Video Decoder
+ * Copyright (c) 2023 Huseyin BIYIK
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+#include "rkmpp.h"
+#include "rkplane.h"
+
+static int rkmpp_config_withframe(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppEncCfg cfg = codec->enccfg;
+
+    if(codec->hascfg == 0){
+        rkformat format;
+
+        int ret;
+        avctx->time_base.num = avctx->framerate.den;
+        avctx->time_base.den = avctx->framerate.num;
+
+        mpp_enc_cfg_set_s32(cfg, "prep:width", mpp_frame_get_width(mppframe));
+        mpp_enc_cfg_set_s32(cfg, "prep:height", mpp_frame_get_height(mppframe));
+        mpp_enc_cfg_set_s32(cfg, "prep:hor_stride", mpp_frame_get_hor_stride(mppframe));
+        mpp_enc_cfg_set_s32(cfg, "prep:ver_stride", mpp_frame_get_ver_stride(mppframe));
+        mpp_enc_cfg_set_s32(cfg, "prep:format", mpp_frame_get_fmt(mppframe) & MPP_FRAME_FMT_MASK);
+        ret = codec->mpi->control(codec->ctx, MPP_ENC_SET_CFG, cfg);
+        if (ret != MPP_OK) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to config with frame (code = %d).\n", ret);
+            return AVERROR_UNKNOWN;
+        }
+        codec->hascfg = 1;
+        rkmpp_get_mpp_format(&format, mpp_frame_get_fmt(mppframe));
+        av_log(avctx, AV_LOG_INFO, "Reconfigured with w=%d, h=%d, format=%s.\n", mpp_frame_get_width(mppframe),
+                mpp_frame_get_height(mppframe), av_get_pix_fmt_name(format.av));
+        return 0;
+    }
+    return 0;
+}
+
+static int rkmpp_config(AVCodecContext *avctx){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppEncCfg cfg = codec->enccfg;
+    RK_U32 rc_mode, split_mode, split_arg, split_out, fps_num, fps_den;
+    MppCodingType coding_type = rkmpp_get_codingtype(avctx);
+    MppEncHeaderMode header_mode;
+    MppEncSeiMode sei_mode;
+    int ret, max_bps, min_bps, qmin, qmax;
+
+    //prep config
+    mpp_enc_cfg_set_s32(cfg, "prep:width", avctx->width);
+    mpp_enc_cfg_set_s32(cfg, "prep:height", avctx->height);
+    mpp_enc_cfg_set_s32(cfg, "prep:hor_stride", FFALIGN(avctx->width, RKMPP_STRIDE_ALIGN));
+    mpp_enc_cfg_set_s32(cfg, "prep:ver_stride", FFALIGN(avctx->height, RKMPP_STRIDE_ALIGN));
+    // later to be reconfigured with the first frame received
+    mpp_enc_cfg_set_s32(cfg, "prep:format", MPP_FMT_YUV420SP);
+    mpp_enc_cfg_set_s32(cfg, "prep:mirroring", 0);
+    mpp_enc_cfg_set_s32(cfg, "prep:rotation", 0);
+    mpp_enc_cfg_set_s32(cfg, "prep:flip", 0);
+
+    //rc config
+    // make sure time base of avctx is synced to input frames
+    av_reduce(&fps_num, &fps_den, avctx->time_base.den, avctx->time_base.num, 65535);
+
+    /* fix input / output frame rate */
+    mpp_enc_cfg_set_s32(cfg, "rc:fps_in_flex", 0);
+    mpp_enc_cfg_set_s32(cfg, "rc:fps_in_num", fps_num);
+    mpp_enc_cfg_set_s32(cfg, "rc:fps_in_denorm", fps_den);
+    mpp_enc_cfg_set_s32(cfg, "rc:fps_out_flex", 0);
+    mpp_enc_cfg_set_s32(cfg, "rc:fps_out_num",fps_num);
+    mpp_enc_cfg_set_s32(cfg, "rc:fps_out_denorm", fps_den);
+
+    mpp_enc_cfg_set_s32(cfg, "rc:gop", FFMAX(avctx->gop_size, 1));
+
+    // config rc: mode
+    rc_mode = rk_context->rc_mode;
+    if(rc_mode == MPP_ENC_RC_MODE_BUTT)
+        rc_mode = MPP_ENC_RC_MODE_CBR;
+
+    switch(rc_mode){
+        case MPP_ENC_RC_MODE_VBR:
+            av_log(avctx, AV_LOG_INFO, "Rate Control mode is set to VBR\n"); break;
+        case MPP_ENC_RC_MODE_CBR:
+            av_log(avctx, AV_LOG_INFO, "Rate Control mode is set to CBR\n"); break;
+        case MPP_ENC_RC_MODE_FIXQP:
+            av_log(avctx, AV_LOG_INFO, "Rate Control mode is set to CQP\n"); break;
+        case MPP_ENC_RC_MODE_AVBR:
+            av_log(avctx, AV_LOG_INFO, "Rate Control mode is set to AVBR\n"); break;
+    }
+
+    mpp_enc_cfg_set_u32(cfg, "rc:mode", rc_mode);
+
+    // config rc: bps
+    mpp_enc_cfg_set_u32(cfg, "rc:bps_target", avctx->bit_rate);
+
+    switch (rc_mode) {
+        case MPP_ENC_RC_MODE_FIXQP : {
+            /* do not setup bitrate on FIXQP mode */
+            min_bps =  max_bps = avctx->bit_rate;
+            break;
+        }
+        case MPP_ENC_RC_MODE_CBR : {
+            /* CBR mode has narrow bound */
+            max_bps =  avctx->bit_rate * 17 / 16;
+            min_bps =  avctx->bit_rate * 15 / 16;
+            break;
+        }
+        case MPP_ENC_RC_MODE_VBR :
+        case MPP_ENC_RC_MODE_AVBR : {
+            /* VBR mode has wide bound */
+            max_bps =  avctx->bit_rate * 17 / 16;
+            min_bps =  avctx->bit_rate * 1 / 16;
+            break;
+        }
+        default : {
+            /* default use CBR mode */
+            max_bps =  avctx->bit_rate * 17 / 16;
+            min_bps =  avctx->bit_rate * 15 / 16;
+            break;
+        }
+    }
+
+    mpp_enc_cfg_set_s32(cfg, "rc:bps_max", max_bps);
+    mpp_enc_cfg_set_s32(cfg, "rc:bps_min", min_bps);
+
+    av_log(avctx, AV_LOG_INFO, "Bitrate Target/Min/Max is set to %ld/%d/%d\n", avctx->bit_rate, min_bps, max_bps);
+
+    // config rc: drop behaviour
+    mpp_enc_cfg_set_u32(cfg, "rc:drop_mode", MPP_ENC_RC_DROP_FRM_DISABLED);
+    mpp_enc_cfg_set_u32(cfg, "rc:drop_thd", 20); // 20% of max bps
+    mpp_enc_cfg_set_u32(cfg, "rc:drop_gap", 1); // Do not continuous drop frame
+
+    // config rc: qp
+    switch (coding_type) {
+        case MPP_VIDEO_CodingAVC :
+        case MPP_VIDEO_CodingHEVC : {
+            qmax = QMIN_H26x + (100 - rk_context->qmin) * (QMAX_H26x - QMIN_H26x) / 100;
+            qmin = QMIN_H26x + (100 - rk_context->qmax) * (QMAX_H26x - QMIN_H26x) / 100;
+            switch (rc_mode) {
+                case MPP_ENC_RC_MODE_FIXQP : {
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_init", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_max", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_min", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_max_i", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_min_i", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_ip", 0);
+                    break;
+                }
+                case MPP_ENC_RC_MODE_CBR :
+                case MPP_ENC_RC_MODE_VBR :
+                case MPP_ENC_RC_MODE_AVBR : {
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_init", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_max", qmax);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_min", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_max_i",qmax);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_min_i", qmin);
+                    mpp_enc_cfg_set_s32(cfg, "rc:qp_ip", 2);
+                    break;
+                }
+                default : {
+                    av_log(avctx, AV_LOG_ERROR, "Unsupported Encoder Mode %d.\n", rc_mode);
+                    break;
+                }
+            }
+            break;
+        }
+        case MPP_VIDEO_CodingVP8 : {
+            // vp8 only setup base qp range
+            qmax = QMIN_VPx + (100 - rk_context->qmin) * (QMAX_VPx - QMIN_VPx) / 100;
+            qmin = QMIN_VPx + (100 - rk_context->qmax) * (QMAX_VPx - QMIN_VPx) / 100;
+            mpp_enc_cfg_set_s32(cfg, "rc:qp_init", qmin);
+            mpp_enc_cfg_set_s32(cfg, "rc:qp_max", qmax);
+            mpp_enc_cfg_set_s32(cfg, "rc:qp_min", qmin);
+            mpp_enc_cfg_set_s32(cfg, "rc:qp_max_i", qmax);
+            mpp_enc_cfg_set_s32(cfg, "rc:qp_min_i", qmin);
+            mpp_enc_cfg_set_s32(cfg, "rc:qp_ip", 6);
+            break;
+        }
+        case MPP_VIDEO_CodingMJPEG : {
+            qmax = QMIN_JPEG + (100 - rk_context->qmin) * (QMAX_JPEG - QMIN_JPEG) / 100;
+            qmin = QMIN_JPEG + (100 - rk_context->qmax) * (QMAX_JPEG- QMIN_JPEG) / 100;
+            // jpeg use special codec config to control qtable
+            mpp_enc_cfg_set_s32(cfg, "jpeg:q_factor", 80);
+            mpp_enc_cfg_set_s32(cfg, "jpeg:qf_max", qmax);
+            mpp_enc_cfg_set_s32(cfg, "jpeg:qf_min", qmin);
+            break;
+        }
+        default : {
+            break;
+        }
+    }
+
+    // setup codec
+     mpp_enc_cfg_set_s32(cfg, "codec:type", coding_type);
+     switch (coding_type) {
+         case MPP_VIDEO_CodingAVC : {
+             avctx->profile = rk_context->profile;
+             avctx->level = rk_context->level;
+             mpp_enc_cfg_set_s32(cfg, "h264:profile", avctx->profile);
+             mpp_enc_cfg_set_s32(cfg, "h264:level", avctx->level);
+             mpp_enc_cfg_set_s32(cfg, "h264:cabac_en", rk_context->coder);
+             mpp_enc_cfg_set_s32(cfg, "h264:cabac_idc", 0);
+             mpp_enc_cfg_set_s32(cfg, "h264:trans8x8", rk_context->dct8x8 && avctx->profile == FF_PROFILE_H264_HIGH ? 1 : 0);
+             switch(avctx->profile){
+                 case FF_PROFILE_H264_BASELINE: av_log(avctx, AV_LOG_INFO, "Profile is set to BASELINE\n"); break;
+                 case FF_PROFILE_H264_MAIN: av_log(avctx, AV_LOG_INFO, "Profile is set to MAIN\n"); break;
+                 case FF_PROFILE_H264_HIGH:
+                     av_log(avctx, AV_LOG_INFO, "Profile is set to HIGH\n");
+                     if(rk_context->dct8x8)
+                         av_log(avctx, AV_LOG_INFO, "8x8 Transform is enabled\n");
+                     break;
+             }
+             av_log(avctx, AV_LOG_INFO, "Level is set to %d\n", avctx->level);
+             if(rk_context->coder)
+                 av_log(avctx, AV_LOG_INFO, "Coder is set to CABAC\n");
+             else
+                 av_log(avctx, AV_LOG_INFO, "Coder is set to CAVLC\n");
+             break;
+         }
+         case MPP_VIDEO_CodingHEVC : {
+             avctx->profile = FF_PROFILE_HEVC_MAIN;
+             avctx->level = rk_context->level;
+             mpp_enc_cfg_set_s32(cfg, "h265:profile", avctx->profile);
+             mpp_enc_cfg_set_s32(cfg, "h265:level", avctx->level);
+             switch(avctx->profile){
+                 case FF_PROFILE_HEVC_MAIN: av_log(avctx, AV_LOG_INFO, "Profile is set to MAIN\n"); break;
+                 case FF_PROFILE_HEVC_MAIN_10: av_log(avctx, AV_LOG_INFO, "Profile is set to MAIN 10\n"); break;
+             }
+             av_log(avctx, AV_LOG_INFO, "Level is set to %d\n", avctx->level == 255 ? 85 : avctx->level / 3);
+             break;
+         }
+         case MPP_VIDEO_CodingMJPEG :
+         case MPP_VIDEO_CodingVP8 :
+             mpp_enc_cfg_set_s32(cfg, "vp8:disable_ivf", 1);
+             break;
+         default : {
+             av_log(avctx, AV_LOG_ERROR, "Unsupported coding type for config (code = %d).\n", coding_type);
+             break;
+         }
+     }
+
+     av_log(avctx, AV_LOG_INFO, "Quality Min/Max is set to %d%%(Quant=%d) / %d%%(Quant=%d)\n",
+             rk_context->qmin, qmax, rk_context->qmax, qmin);
+
+     split_mode = 0;
+     split_arg = 0;
+     split_out = 0;
+
+     if (split_mode) {
+         mpp_enc_cfg_set_s32(cfg, "split:mode", split_mode);
+         mpp_enc_cfg_set_s32(cfg, "split:arg", split_arg);
+         mpp_enc_cfg_set_s32(cfg, "split:out", split_out);
+     }
+
+     ret = codec->mpi->control(codec->ctx, MPP_ENC_SET_CFG, cfg);
+     if (ret != MPP_OK) {
+         av_log(avctx, AV_LOG_ERROR, "Failed to set cfg on MPI (code = %d).\n", ret);
+         return AVERROR_UNKNOWN;
+     }
+
+     sei_mode = MPP_ENC_SEI_MODE_DISABLE;
+     ret = codec->mpi->control(codec->ctx, MPP_ENC_SET_SEI_CFG, &sei_mode);
+     if (ret != MPP_OK) {
+         av_log(avctx, AV_LOG_ERROR, "Failed to set sei cfg on MPI (code = %d).\n", ret);
+         return AVERROR_UNKNOWN;
+     }
+
+     header_mode = MPP_ENC_HEADER_MODE_EACH_IDR;
+     if (coding_type == MPP_VIDEO_CodingAVC || coding_type == MPP_VIDEO_CodingHEVC) {
+         ret = codec->mpi->control(codec->ctx, MPP_ENC_SET_HEADER_MODE, &header_mode);
+         if (ret) {
+             av_log(avctx, AV_LOG_ERROR, "Failed header mode on MPI (code = %d).\n", ret);
+             return ret;
+         }
+     }
+
+    return 0;
+}
+
+//https://github.com/rockchip-linux/mpp/issues/417
+//Encoder does not support 422 planes, but we can do this with rga
+//FIX-ME: NV12/YUV422P do not have libyuv fallbacks when encoding vp8
+static int check_vp8_planes(AVCodecContext *avctx, enum AVPixelFormat pix_fmt){
+    MppCodingType coding_type = rkmpp_get_codingtype(avctx);
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+
+    if(coding_type == MPP_VIDEO_CodingVP8 &&
+            (pix_fmt == AV_PIX_FMT_NV16 ||
+             pix_fmt == AV_PIX_FMT_YUV422P)){
+        rk_context->postrga_format = AV_PIX_FMT_NV12;
+
+        if (avctx->width < RKMPP_RGA_MIN_SIZE || avctx->width > RKMPP_RGA_MAX_SIZE){
+            av_log(avctx, AV_LOG_ERROR, "Frame width (%d) not in rga scalable range (%d - %d)\n",
+                    avctx->width, RKMPP_RGA_MIN_SIZE, RKMPP_RGA_MAX_SIZE);
+            return -1;
+        } else
+            rk_context->postrga_width = avctx->width;
+
+        if (avctx->height < RKMPP_RGA_MIN_SIZE || avctx->height > RKMPP_RGA_MAX_SIZE){
+            av_log(avctx, AV_LOG_ERROR, "Frame height (%d) not in rga scalable range (%d - %d)\n",
+                    avctx->height, RKMPP_RGA_MIN_SIZE, RKMPP_RGA_MAX_SIZE);
+            return -1;
+        } else
+            rk_context->postrga_height = avctx->height;
+    } else
+        rk_context->postrga_format = AV_PIX_FMT_NONE;
+    return 0;
+}
+
+static int check_scaling(AVCodecContext *avctx, enum AVPixelFormat pix_fmt){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+
+    if(rk_context->postrga_width || rk_context->postrga_height){
+        if(pix_fmt != AV_PIX_FMT_NV16 && pix_fmt != AV_PIX_FMT_NV12 &&
+                pix_fmt != AV_PIX_FMT_YUV422P && pix_fmt != AV_PIX_FMT_YUV420P){
+            av_log(avctx, AV_LOG_ERROR, "Scaling is only supported for NV12,NV16,YUV420P,YUV422P. %s requested\n",
+                    av_get_pix_fmt_name(pix_fmt));
+            return -1;
+        }
+        // align it to accepted RGA range
+        rk_context->postrga_width = FFMAX(rk_context->postrga_width, RKMPP_RGA_MIN_SIZE);
+        rk_context->postrga_height = FFMAX(rk_context->postrga_height, RKMPP_RGA_MIN_SIZE);
+        rk_context->postrga_width = FFMIN(rk_context->postrga_width, RKMPP_RGA_MAX_SIZE);
+        rk_context->postrga_height = FFMIN(rk_context->postrga_height, RKMPP_RGA_MAX_SIZE);
+        avctx->width = rk_context->postrga_width;
+        avctx->height = rk_context->postrga_height;
+        if(rk_context->postrga_format == AV_PIX_FMT_NONE)
+            rk_context->postrga_format = pix_fmt;
+    }
+    return 0;
+}
+
+int rkmpp_init_encoder(AVCodecContext *avctx){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppCodingType coding_type = rkmpp_get_codingtype(avctx);
+    RK_U8 enc_hdr_buf[HDR_SIZE];
+    MppPacket packet = NULL;
+    size_t packetlen;
+    void *packetpos;
+    int ret;
+    int input_timeout = 500;
+
+    // ENCODER SETUP
+    ret = mpp_enc_cfg_init(&codec->enccfg);
+    if (ret) {
+        av_log(avctx, AV_LOG_ERROR, "Codec failed to initialize encoder config (code = %d)\n", ret);
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    ret = codec->mpi->control(codec->ctx, MPP_ENC_GET_CFG, codec->enccfg);
+    if (ret) {
+        av_log(avctx, AV_LOG_ERROR, "Codec failed to get encoder config (code = %d)\n", ret);
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    if(avctx->pix_fmt != AV_PIX_FMT_DRM_PRIME){
+        if(check_vp8_planes(avctx, avctx->pix_fmt)){
+            ret = AVERROR_UNKNOWN;
+            goto fail;
+        }
+        if(check_scaling(avctx, avctx->pix_fmt)){
+            ret = AVERROR_UNKNOWN;
+            goto fail;
+        }
+    }
+
+    if(rkmpp_config(avctx)){
+        ret = AVERROR_UNKNOWN;
+        goto fail;
+    }
+
+    // copy sps/pps/vps to extradata for h26x
+    if(coding_type == MPP_VIDEO_CodingAVC || coding_type == MPP_VIDEO_CodingHEVC){
+        memset(enc_hdr_buf, 0 , HDR_SIZE);
+
+        ret = mpp_packet_init(&packet, (void *)enc_hdr_buf, HDR_SIZE);
+        if (!packet) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to init extra info packet (code = %d).\n", ret);
+            ret = AVERROR_UNKNOWN;
+            goto fail;
+        }
+
+        mpp_packet_set_length(packet, 0);
+        ret = codec->mpi->control(codec->ctx, MPP_ENC_GET_HDR_SYNC, packet);
+        if (ret != MPP_OK) {
+            av_log(avctx, AV_LOG_ERROR, "Failed to get extra info on MPI (code = %d).\n", ret);
+            ret = AVERROR_UNKNOWN;
+            goto fail;
+        }
+
+        /* get and write sps/pps for H.264/H.265 */
+        packetpos = mpp_packet_get_pos(packet);
+        packetlen  = mpp_packet_get_length(packet);
+
+        if (avctx->extradata != NULL) {
+            av_free(avctx->extradata);
+            avctx->extradata = NULL;
+        }
+        avctx->extradata = av_malloc(packetlen + AV_INPUT_BUFFER_PADDING_SIZE);
+        if (avctx->extradata == NULL) {
+            ret = AVERROR(ENOMEM);
+            goto fail;
+        }
+        avctx->extradata_size = packetlen + AV_INPUT_BUFFER_PADDING_SIZE;
+        memcpy(avctx->extradata, packetpos, packetlen);
+        memset(avctx->extradata + packetlen, 0, AV_INPUT_BUFFER_PADDING_SIZE);
+        mpp_packet_deinit(&packet);
+    }
+
+    codec->mpi->control(codec->ctx, MPP_SET_INPUT_TIMEOUT, &input_timeout);
+    return 0;
+
+fail:
+    av_log(avctx, AV_LOG_ERROR, "Failed to initialize RKMPP Codec.\n");
+    if(packet)
+        mpp_packet_deinit(&packet);
+    return ret;
+}
+
+static void rkmpp_release_packet_buf(void *opaque, uint8_t *data){
+    MppPacket mpppacket = opaque;
+    mpp_packet_deinit(&mpppacket);
+}
+
+static int rkmpp_send_frame(AVCodecContext *avctx, AVFrame *frame){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppFrame mppframe = NULL;
+    rkformat format;
+    int ret=0, keepframe=0;
+
+    // EOS frame, avframe=NULL
+    if (!frame) {
+        av_log(avctx, AV_LOG_DEBUG, "End of stream.\n");
+        mpp_frame_init(&mppframe);
+        mpp_frame_set_eos(mppframe, 1);
+    } else {
+        if (avctx->pix_fmt == AV_PIX_FMT_DRM_PRIME){
+            // the frame is coming from a DRMPRIME enabled decoder, no copy necessary
+            // just import existing fd and buffer to mmpp
+            mppframe = import_drm_to_mpp(avctx, frame);
+        } else {
+            // the frame is coming from a RKMPP decoder, no copy necessary
+            // use existing mppframe which is atatched to
+            // RKMPP_MPPFRAME_BUFINDEX of the frame buffers
+            // those frames need to be cleaned by the decoder itself therefore dont clean them
+            mppframe = get_mppframe_from_av(frame);
+            if(mppframe)
+                keepframe = 1;
+            else
+                // soft frames needs to be copied to a buffer region where mpp supports.
+                // a copy is necessary here
+                mppframe = create_mpp_frame(frame->width, frame->height, avctx->pix_fmt, codec->buffer_group, NULL, frame);
+        }
+
+        if(!mppframe){
+            ret = AVERROR_UNKNOWN;
+            goto clean;
+        }
+
+        rkmpp_get_mpp_format(&format, mpp_frame_get_fmt(mppframe));
+
+        if(check_vp8_planes(avctx, format.av)){
+            ret = AVERROR_UNKNOWN;
+            goto clean;
+        }
+        if(check_scaling(avctx, format.av)){
+            ret = AVERROR_UNKNOWN;
+            goto clean;
+        }
+
+        if(rk_context->postrga_format != AV_PIX_FMT_NONE || rk_context->postrga_width || rk_context->postrga_height){
+            MppFrame postmppframe = NULL;
+
+            postmppframe = create_mpp_frame(rk_context->postrga_width , rk_context->postrga_height, rk_context->postrga_format,
+                    codec->buffer_group, NULL, NULL);
+
+            if(!postmppframe){
+                ret = AVERROR_UNKNOWN;
+                av_log(avctx, AV_LOG_ERROR, "Error creating post mpp frame\n");
+                goto clean;
+            }
+
+            ret = rga_convert_mpp_mpp(avctx, mppframe, postmppframe);
+            if(ret){
+                mpp_frame_deinit(&postmppframe);
+                av_log(avctx, AV_LOG_ERROR, "Error applying Post RGA\n");
+                goto clean;
+            }
+
+            if(!keepframe)
+                mpp_frame_deinit(&mppframe);
+            else
+                keepframe = 0;
+
+            mppframe = postmppframe;
+        }
+
+        mpp_frame_set_pts(mppframe, frame->pts);
+    }
+
+    ret = rkmpp_config_withframe(avctx, mppframe, frame);
+    if(ret){
+        ret = AVERROR_UNKNOWN;
+        goto clean;
+    }
+
+    // put the frame in encoder
+    ret = codec->mpi->encode_put_frame(codec->ctx, mppframe);
+
+    if (ret != MPP_OK) {
+        av_log(avctx, AV_LOG_DEBUG, "Encoder buffer full\n");
+        ret = AVERROR(EAGAIN);
+    } else
+        av_log(avctx, AV_LOG_DEBUG, "Wrote %ld bytes to encoder\n", mpp_frame_get_buf_size(mppframe));
+
+clean:
+    if(!keepframe)
+        mpp_frame_deinit(&mppframe);
+    return ret;
+}
+
+
+static int rkmpp_get_packet(AVCodecContext *avctx, AVPacket *packet, int timeout){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppPacket mpppacket = NULL;
+    MppMeta meta = NULL;
+    int ret, keyframe=0;
+
+    codec->mpi->control(codec->ctx, MPP_SET_OUTPUT_TIMEOUT, (MppParam)&timeout);
+
+    ret = codec->mpi->encode_get_packet(codec->ctx, &mpppacket);
+
+    // rest of above code is never tested most likely broken
+    if (ret != MPP_OK && ret != MPP_ERR_TIMEOUT) {
+        av_log(avctx, AV_LOG_ERROR, "Failed to get packet (code = %d)\n", ret);
+        return AVERROR(EAGAIN);
+    }
+
+    if (!mpppacket) {
+        av_log(avctx, AV_LOG_DEBUG, "Timeout getting encoded packet.\n");
+        return AVERROR(EAGAIN);
+    }
+
+    // TO-DO: Handle EOS
+    if (mpp_packet_get_eos(mpppacket)) {
+        av_log(avctx, AV_LOG_DEBUG, "Received an EOS packet.\n");
+        ret = AVERROR_EOF;
+        goto fail;
+    }
+
+    av_log(avctx, AV_LOG_DEBUG, "Received a packet.\n");
+
+    packet->data = mpp_packet_get_data(mpppacket);
+    packet->size = mpp_packet_get_length(mpppacket);
+    packet->buf = av_buffer_create(packet->data, packet->size, rkmpp_release_packet_buf,
+            mpppacket, AV_BUFFER_FLAG_READONLY);
+    if (!packet->buf) {
+        ret = AVERROR(ENOMEM);
+        goto fail;
+    }
+
+    packet->pts = mpp_packet_get_pts(mpppacket);
+    packet->dts = mpp_packet_get_pts(mpppacket);
+    codec->frames++;
+
+    meta = mpp_packet_get_meta(mpppacket);
+    if (meta)
+        mpp_meta_get_s32(meta, KEY_OUTPUT_INTRA, &keyframe);
+    if (keyframe)
+        packet->flags |= AV_PKT_FLAG_KEY;
+
+    return 0;
+fail:
+    if (mpppacket)
+        mpp_packet_deinit(&mpppacket);
+    return ret;
+}
+
+
+int rkmpp_encode(AVCodecContext *avctx, AVPacket *packet, const AVFrame *frame, int *got_packet){
+    int ret;
+
+    ret = rkmpp_send_frame(avctx, (AVFrame *)frame);
+    if (ret)
+        return ret;
+
+    ret = rkmpp_get_packet(avctx, packet, MPP_TIMEOUT_BLOCK);
+    if (ret == AVERROR(EAGAIN) || ret == AVERROR_EOF) {
+        *got_packet = 0;
+    } else if (ret) {
+        return ret;
+    } else {
+        *got_packet = 1;
+    }
+    return 0;
+}
+
+RKMPP_ENC(h264, AV_CODEC_ID_H264, vepu5)
+RKMPP_ENC(hevc, AV_CODEC_ID_HEVC, vepu5)
+RKMPP_ENC(vp8, AV_CODEC_ID_VP8, vepu1)
diff --git a/libavcodec/rkplane.c b/libavcodec/rkplane.c
new file mode 100644
index 0000000000..a138a1883e
--- /dev/null
+++ b/libavcodec/rkplane.c
@@ -0,0 +1,669 @@
+/*
+ * RockChip MPP Plane Conversions
+ * Copyright (c) 2023 Huseyin BYIIK
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg 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.
+ *
+ * FFmpeg 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 FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+
+/*
+ * Mpp decoder/encoder outputs & inputs generally semi-planar pictures
+ * FFmpeg on the other hand uses planar pictures. Normally libavfilter has
+ * several filters to handle this conversion however it is not cpu affective
+ * This file handles several plane conversion with hardware accelerated RGA chip in
+ * rockchip socs, whenever there is a failure it falls back to libyuv which is SIMD
+ * optimized plane conversion library. Failure is expected time to time, because
+ * RGA is not always consistent in between kernel versions of rockchips downstream
+ *
+ * Normally both RGA and enhancements in libyuv should be a part or libavfilter, but
+ * currently this is easier. May be in future someone take this up and move to avfilter.
+ */
+
+#include <fcntl.h>
+#include "rkmpp.h"
+#include "rkplane.h"
+#include "libyuv/planar_functions.h"
+#include "libyuv/scale_uv.h"
+#include "libyuv/scale.h"
+
+
+#define AV_VSTRIDE(AVFRAME) (FFALIGN(AVFRAME->buf[0] && AVFRAME->buf[1] ? AVFRAME->buf[0]->size / AVFRAME->linesize[0] : (AVFRAME->data[1] - AVFRAME->data[0]) / AVFRAME->linesize[0], 16))
+
+static void rkmpp_release_mppframe(void *opaque, uint8_t *data)
+{
+    MppFrame mppframe = opaque;
+    mpp_frame_deinit(&mppframe);
+}
+
+static void rkmpp_release_drm_desc(void *opaque, uint8_t *data)
+{
+    AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)opaque;
+    av_free(desc);
+}
+
+
+static int set_mppframe_to_avbuff(MppFrame mppframe, AVFrame * frame, int index){
+    int i;
+
+    // find the first available buffer in  [buf[0], buf[4]]
+    for(i=0; i<5; i++){
+        if(i > 3)
+            return -1;
+        else if(!frame->buf[i])
+            break;
+    }
+
+    frame->buf[i] = av_buffer_create(mppframe, mpp_frame_get_buf_size(mppframe),
+            rkmpp_release_mppframe, mppframe, AV_BUFFER_FLAG_READONLY);
+
+    if(i >= 0){
+        if(index >= 0)
+            frame->data[index] = frame->buf[i]->data;
+        return 0;
+    }
+
+    return -1;
+}
+
+static int set_drmdesc_to_avbuff(AVDRMFrameDescriptor *desc, AVFrame *frame){
+    int i;
+
+    for(i=0; i<5; i++){
+        if(i > 3)
+            return -1;
+        else if(!frame->buf[i])
+            break;
+    }
+
+    frame->buf[i] = av_buffer_create((unsigned char *) desc, sizeof(AVDRMFrameDescriptor),
+            rkmpp_release_drm_desc, desc, AV_BUFFER_FLAG_READONLY);
+
+    return i;
+}
+
+static int rga_scale(uint64_t src_fd, uint64_t src_y, uint16_t src_width, uint16_t src_height, uint16_t src_hstride, uint16_t src_vstride,
+        uint64_t dst_fd, uint64_t dst_y, uint16_t dst_width, uint16_t dst_height, uint16_t dst_hstride, uint16_t dst_vstride,
+        enum _Rga_SURF_FORMAT informat, enum _Rga_SURF_FORMAT outformat){
+    rga_info_t src = {0};
+    rga_info_t dst = {0};
+
+    if(dst_hstride < dst_width)
+        dst_width = FFALIGN(dst_width, RKMPP_STRIDE_ALIGN);
+    if(dst_vstride < dst_height)
+        dst_height = FFALIGN(dst_height, RKMPP_STRIDE_ALIGN);
+    if(src_hstride < src_width)
+        src_width = FFALIGN(src_width, RKMPP_STRIDE_ALIGN);
+    if(src_vstride < src_height)
+        src_height = FFALIGN(src_height, RKMPP_STRIDE_ALIGN);
+
+    src.fd = src_fd;
+    src.virAddr = (void *)src_y;
+    src.mmuFlag = 1;
+    src.format = informat;
+    rga_set_rect(&src.rect, 0, 0,
+            src_width, src_height, src_hstride, src_vstride, informat);
+
+    dst.fd = dst_fd;
+    dst.virAddr = (void *)dst_y;
+    dst.mmuFlag = 1;
+    dst.format = outformat;
+    rga_set_rect(&dst.rect, 0, 0,
+            dst_width, dst_height, dst_hstride, dst_vstride, outformat);
+
+    return c_RkRgaBlit(&src, &dst, NULL);
+}
+
+int rga_convert_mpp_mpp(AVCodecContext *avctx, MppFrame in_mppframe, MppFrame out_mppframe){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    rkformat informat, outformat;
+
+    if (!codec->norga){
+        if(!out_mppframe)
+            return -1;
+        rkmpp_get_mpp_format(&informat, mpp_frame_get_fmt(in_mppframe) & MPP_FRAME_FMT_MASK);
+        rkmpp_get_mpp_format(&outformat, mpp_frame_get_fmt(out_mppframe) & MPP_FRAME_FMT_MASK);
+        if(rga_scale(mpp_buffer_get_fd(mpp_frame_get_buffer(in_mppframe)), 0,
+            mpp_frame_get_width(in_mppframe), mpp_frame_get_height(in_mppframe),
+            mpp_frame_get_hor_stride(in_mppframe),  mpp_frame_get_ver_stride(in_mppframe),
+            mpp_buffer_get_fd(mpp_frame_get_buffer(out_mppframe)), 0,
+            mpp_frame_get_width(out_mppframe), mpp_frame_get_height(out_mppframe),
+            mpp_frame_get_hor_stride(out_mppframe),  mpp_frame_get_ver_stride(out_mppframe),
+            informat.rga,
+            outformat.rga)){
+                av_log(avctx, AV_LOG_WARNING, "RGA failed falling back to soft conversion\n");
+                codec->norga = 1; // fallback to soft conversion
+                return -1;
+        } else
+            return 0;
+    }
+
+    return -1;
+}
+
+static void mpp_nv12_av_yuv420p_soft(MppFrame mppframe, AVFrame *frame){
+    // warning: mpp frame must not be released until displayed
+    MppBuffer buffer = mpp_frame_get_buffer(mppframe);
+    int hstride = mpp_frame_get_hor_stride(mppframe);
+    int vstride = mpp_frame_get_ver_stride(mppframe);
+
+    frame->data[0] = mpp_buffer_get_ptr(buffer); // use existing y plane
+    frame->linesize[0] = hstride;
+
+    // convert only uv plane from semi-planar to planar
+    SplitUVPlane(frame->data[0] + hstride * vstride, hstride,
+            frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2],
+            (frame->width + 1) >> 1, (frame->height + 1) >> 1);
+}
+
+static void mpp_nv16_av_yuv420p_soft(MppFrame mppframe, AVFrame *frame){
+    // warning: mpp frame must not be released until displayed
+    MppBuffer buffer = mpp_frame_get_buffer(mppframe);
+    int hstride = mpp_frame_get_hor_stride(mppframe);
+    int vstride = mpp_frame_get_ver_stride(mppframe);
+    char *src = (char *)mpp_buffer_get_ptr(buffer) + hstride * vstride;
+
+    // scale down uv plane by 2 and write it to y plane of avbuffer temporarily
+    UVScale(src, hstride, frame->width, frame->height,
+            frame->data[0], frame->linesize[0],
+            (frame->width + 1) >> 1, (frame->height + 1) >> 1, kFilterNone);
+
+    // convert uv plane from semi-planar to planar
+    SplitUVPlane(frame->data[0], frame->linesize[0],
+            frame->data[1], frame->linesize[1], frame->data[2], frame->linesize[2],
+            (frame->width + 1) >> 1, (frame->height + 1) >> 1);
+
+    // use existing y plane from mppbuffer
+    frame->data[0] = mpp_buffer_get_ptr(buffer);
+    frame->linesize[0] = hstride;
+}
+
+static void mpp_nv16_av_nv12_soft(MppFrame mppframe, AVFrame *frame){
+    // warning: mpp frame must not be released until displayed
+    MppBuffer buffer = mpp_frame_get_buffer(mppframe);
+    int hstride = mpp_frame_get_hor_stride(mppframe);
+    int vstride = mpp_frame_get_ver_stride(mppframe);
+    char *src = (char *)mpp_buffer_get_ptr(buffer) + hstride * vstride;
+
+    // scale down uv plane by 2 and write it to uv plane of avbuffer
+    UVScale(src, hstride, frame->width, frame->height,
+            frame->data[1], frame->linesize[0],
+            (frame->width + 1) >> 1, (frame->height + 1) >> 1, kFilterNone);
+
+    // use existing y plane from mppbuffer
+    frame->data[0] = mpp_buffer_get_ptr(buffer);
+    frame->linesize[0] = hstride;
+}
+
+static MppFrame wrap_mpp_to_avframe(AVCodecContext *avctx, AVFrame *frame, MppFrame targetframe){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppBuffer targetbuffer = NULL;
+    int planesize;
+
+    if(!targetframe)
+        targetframe = create_mpp_frame(avctx->width, avctx->height, avctx->pix_fmt, codec->buffer_group, NULL, NULL);
+
+    if(!targetframe)
+        return NULL;
+
+    targetbuffer = mpp_frame_get_buffer(targetframe);
+    planesize = mpp_frame_get_hor_stride(targetframe) * mpp_frame_get_ver_stride(targetframe);
+
+    frame->data[0] = mpp_buffer_get_ptr(targetbuffer);
+    frame->linesize[0] = mpp_frame_get_hor_stride(targetframe);
+    frame->width = avctx->width;
+    frame->height = avctx->height;
+    frame->extended_data = frame->data;
+
+    switch(avctx->pix_fmt){
+    case AV_PIX_FMT_YUV420P:
+        frame->data[1] = frame->data[0] + planesize;
+        frame->linesize[1] = (frame->linesize[0] + 1) >> 1;
+        frame->data[2] = frame->data[1] + ((planesize + 1) >> 2);
+        frame->linesize[2] = frame->linesize[1];
+        return targetframe;
+    case AV_PIX_FMT_NV12:
+        frame->data[1] = frame->data[0] + planesize;
+        frame->linesize[1] = frame->linesize[0];
+        return targetframe;
+    }
+
+    rkmpp_release_mppframe(targetframe, NULL);
+    return NULL;
+}
+
+MppFrame create_mpp_frame(int width, int height, enum AVPixelFormat avformat, MppBufferGroup buffer_group, AVDRMFrameDescriptor *desc, AVFrame *frame){
+    MppFrame mppframe = NULL;
+    MppBuffer mppbuffer = NULL;
+    rkformat format;
+    int avmap[3][4]; //offset, dststride, width, height of max 3 planes
+    int size, ret, hstride, vstride;
+    int hstride_mult = 1;
+    int planes = 2;
+    int haspitch = 0;
+    int overshoot = 1024;
+
+    ret = mpp_frame_init(&mppframe);
+
+    if (ret) {
+        goto clean;
+     }
+
+    vstride = FFALIGN(height, RKMPP_STRIDE_ALIGN);
+
+    switch(avformat){
+    case AV_PIX_FMT_NV12:
+        planes = 2;
+        hstride = FFALIGN(width, RKMPP_STRIDE_ALIGN);
+        // y plane
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width,
+        avmap[0][3] = height;
+        // uv plane
+        avmap[1][0] = hstride * vstride; // uv offset = y plane size
+        avmap[1][1] = hstride; // uv stride = hstride
+        avmap[1][2] = width; // uv width = width
+        avmap[1][3] = (height + 1)>> 1; // uv height = height / 2
+        size = avmap[1][0] + ((avmap[1][0] + 1) >> 1) + overshoot; // total size = y+uv planesize
+        break;
+    case AV_PIX_FMT_YUV420P:
+        planes = 3;
+        hstride = FFALIGN(width, RKMPP_STRIDE_ALIGN);
+        // y plane
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width,
+        avmap[0][3] = height;
+        // u plane
+        avmap[1][0] = hstride * vstride; // u offset = y plane size
+        avmap[1][1] = (hstride + 1)>> 1; // u stride = hstride / 2
+        avmap[1][2] = (width + 1)>> 1; // u width = width / 2
+        avmap[1][3] = (height + 1)>> 1; // u height = height / 2
+        // v plane
+        avmap[2][0] = avmap[1][0] + ((avmap[1][0] + 1) >> 2); // v offset = y+u plane size
+        avmap[2][1] = avmap[1][1]; // v stride = hstride / 2
+        avmap[2][2] = avmap[1][2]; // v width = width / 2
+        avmap[2][3] = avmap[1][3]; // v height = height / 2
+        size = avmap[2][0] + ((avmap[1][0] + 1) >> 2) + overshoot; // total size = y+u+v planesize
+        break;
+    case AV_PIX_FMT_NV16:
+        planes = 2;
+        hstride = FFALIGN(width, RKMPP_STRIDE_ALIGN);
+        // y plane
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width,
+        avmap[0][3] = height;
+        // uv plane
+        avmap[1][0] = hstride * vstride; // uv offset = y plane size
+        avmap[1][1] = hstride; // uv stride = hstride
+        avmap[1][2] = width; // uv width = width
+        avmap[1][3] = height; // uv height = height
+        size = avmap[1][0] * 2 + overshoot; // total size = y+uv planesize
+        break;
+    case AV_PIX_FMT_YUV422P:
+        planes = 3;
+        hstride = FFALIGN(width, RKMPP_STRIDE_ALIGN);
+        //y plane
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width,
+        avmap[0][3] = height;
+        //u plane
+        avmap[1][0] = hstride * vstride; // u offset = y plane size
+        avmap[1][1] = (hstride + 1)>> 1; // u stride = hstride / 2
+        avmap[1][2] = width; // u width = width
+        avmap[1][3] = height; // u height = height
+        //v plane
+        avmap[2][0] = avmap[1][0] + ((avmap[1][0] + 1) >> 1); // v offset = y+u plane size
+        avmap[2][1] = avmap[1][1]; // v stride = hstride
+        avmap[2][2] = avmap[1][2]; // v width = width
+        avmap[2][3] = avmap[1][3]; // v height = height / 2
+        size = avmap[1][0] * 2 + overshoot; // total size = y+u+v planesize
+        break;
+    case AV_PIX_FMT_NV24:
+        planes = 2;
+        hstride = FFALIGN(width, RKMPP_STRIDE_ALIGN);
+        // y plane
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width,
+        avmap[0][3] = height;
+        // uv plane
+        avmap[1][0] = hstride * vstride; // uv offset = y plane size
+        avmap[1][1] = hstride << 1; // uv stride = hstride * 2
+        avmap[1][2] = width << 1; // uv width = width * 2
+        avmap[1][3] = height; // uv height = height
+        size = avmap[1][0] * 3 + overshoot; // total size = y+u+v planesize
+        break;
+    case AV_PIX_FMT_YUV444P:
+        planes = 3;
+        hstride = FFALIGN(width, RKMPP_STRIDE_ALIGN);
+        //y plane
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width,
+        avmap[0][3] = height;
+        //u plane
+        avmap[1][0] = hstride * vstride; // u offset = y plane size
+        avmap[1][1] = hstride; // u stride = hstride
+        avmap[1][2] = width; // u width = width
+        avmap[1][3] = height; // u height = height
+        //v plane
+        avmap[2][0] = avmap[1][0] * 2; // v offset = y+u plane size
+        avmap[2][1] = avmap[1][1]; // v stride = hstride
+        avmap[2][2] = avmap[1][2]; // v width = width
+        avmap[2][3] = avmap[1][3]; // v height = height
+        size = avmap[1][0] * 3 + overshoot; // total size = y+u+v planesize
+        break;
+    case AV_PIX_FMT_YUYV422:
+    case AV_PIX_FMT_UYVY422:
+        planes = 1;
+        haspitch = 1;
+        hstride_mult = 2;
+        hstride = FFALIGN(width * hstride_mult, RKMPP_STRIDE_ALIGN);
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width << 1,
+        avmap[0][3] = height;
+        size = hstride * vstride;
+        break;
+    case AV_PIX_FMT_RGB24:
+    case AV_PIX_FMT_BGR24:
+        haspitch = 1;
+        hstride_mult = 3;
+        hstride = FFALIGN(width * hstride_mult, RKMPP_STRIDE_ALIGN);
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width * 3,
+        avmap[0][3] = height;
+        size = hstride * vstride;
+        planes = 1;
+        break;
+    case AV_PIX_FMT_0RGB:
+    case AV_PIX_FMT_0BGR:
+    case AV_PIX_FMT_BGR0:
+    case AV_PIX_FMT_RGB0:
+    case AV_PIX_FMT_ARGB:
+    case AV_PIX_FMT_ABGR:
+    case AV_PIX_FMT_BGRA:
+    case AV_PIX_FMT_RGBA:
+        haspitch = 1;
+        hstride_mult = 4;
+        hstride = FFALIGN(width * hstride_mult, RKMPP_STRIDE_ALIGN);
+        avmap[0][0] = 0;
+        avmap[0][1] = hstride;
+        avmap[0][2] = width << 2,
+        avmap[0][3] = height;
+        size = hstride * vstride;
+        planes = 1;
+        break;
+    }
+
+    if(desc){
+        MppBufferInfo info;
+        AVDRMLayerDescriptor *layer = &desc->layers[0];
+        rkmpp_get_drm_format(&format, layer->format);
+
+        size = desc->objects[0].size;
+        if(haspitch)
+            hstride = layer->planes[0].pitch;
+        else
+            hstride = layer->planes[0].pitch * hstride_mult;
+
+        if(planes == 1)
+            vstride = size / hstride;
+        else
+            vstride = layer->planes[1].offset / hstride;
+
+        memset(&info, 0, sizeof(info));
+        info.type   = MPP_BUFFER_TYPE_DRM;
+        info.size   = size;
+        info.fd     = desc->objects[0].fd;
+
+        ret = mpp_buffer_import(&mppbuffer, &info);
+        rkmpp_get_drm_format(&format, layer->format);
+    } else {
+        ret = mpp_buffer_get(buffer_group, &mppbuffer, size);
+        rkmpp_get_av_format(&format, avformat);
+    }
+
+     if (ret)
+         goto clean;
+
+     mpp_frame_set_width(mppframe, width);
+     mpp_frame_set_height(mppframe, height);
+     mpp_frame_set_fmt(mppframe, format.mpp);
+     mpp_frame_set_hor_stride(mppframe, hstride);
+     mpp_frame_set_ver_stride(mppframe, vstride);
+     mpp_frame_set_buffer(mppframe, mppbuffer);
+     mpp_frame_set_buf_size(mppframe, size);
+     mpp_buffer_put(mppbuffer);
+
+     if(frame){
+         for(int i = 0; i < planes; i++){
+             CopyPlane(frame->data[i], frame->linesize[i],
+                     (char *)mpp_buffer_get_ptr(mppbuffer) + avmap[i][0], avmap[i][1], avmap[i][2], avmap[i][3]);
+         }
+     }
+
+     return mppframe;
+
+clean:
+     if(mppbuffer)
+         mpp_buffer_put(mppbuffer);
+     if(mppframe)
+         mpp_frame_deinit(&mppframe);
+     return mppframe;
+}
+//for decoder
+int mpp_nv15_av_yuv420p(AVCodecContext *avctx, MppFrame nv15frame, AVFrame *frame){
+    // rga1 which supports yuv420P output does not support nv15 input
+    // therefore this first converts NV15->NV12 with rga2 than NV12 -> yuv420P with libyuv
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppFrame nv12frame = create_mpp_frame(mpp_frame_get_width(nv15frame), mpp_frame_get_height(nv15frame),
+            AV_PIX_FMT_NV12, codec->buffer_group, NULL, NULL);
+    MppFrame yuv420pframe = NULL;
+    int ret = rga_convert_mpp_mpp(avctx, nv15frame, nv12frame);
+
+    rkmpp_release_mppframe(nv15frame, NULL);
+
+    if(!ret){
+        MppFrame yuv420pframe = wrap_mpp_to_avframe(avctx, frame, NULL);
+        if(yuv420pframe &&
+                !set_mppframe_to_avbuff(nv12frame, frame, RKMPP_MPPFRAME_BUFINDEX) &&
+                !set_mppframe_to_avbuff(yuv420pframe, frame, RKMPP_MPPFRAME_BUFINDEX - 1)){
+                    mpp_nv12_av_yuv420p_soft(nv12frame, frame);
+                    return 0;
+        }
+    }
+
+    if(nv12frame)
+        rkmpp_release_mppframe(nv12frame, NULL);
+    if(yuv420pframe)
+        rkmpp_release_mppframe(yuv420pframe, NULL);
+    return -1;
+}
+
+//for decoder
+int mpp_nv12_av_nv12(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame){
+    if(wrap_mpp_to_avframe(avctx, frame, mppframe)){
+        return set_mppframe_to_avbuff(mppframe, frame, RKMPP_MPPFRAME_BUFINDEX);
+    }
+
+    rkmpp_release_mppframe(mppframe, NULL);
+    return -1;
+}
+//for decoder
+int mpp_nv15_av_nv12(AVCodecContext *avctx, MppFrame nv15frame, AVFrame *frame){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppFrame nv12frame = create_mpp_frame(mpp_frame_get_width(nv15frame), mpp_frame_get_height(nv15frame),
+            AV_PIX_FMT_NV12, codec->buffer_group, NULL, NULL);
+    int ret = rga_convert_mpp_mpp(avctx, nv15frame, nv12frame);
+
+    rkmpp_release_mppframe(nv15frame, NULL);
+
+    if(!ret){
+        ret = mpp_nv12_av_nv12(avctx, nv12frame, frame);
+    } else {
+        if(nv12frame)
+            rkmpp_release_mppframe(nv12frame, NULL);
+        av_log(avctx, AV_LOG_ERROR, "RGA failed to convert NV15 -> NV12. No Soft Conversion Possible\n");
+    }
+
+    return ret;
+}
+
+int convert_mpp_to_av(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame,
+        enum AVPixelFormat informat, enum AVPixelFormat outformat){
+    MppFrame targetframe = wrap_mpp_to_avframe(avctx, frame, NULL);
+    int ret=0;
+
+    if(!targetframe){
+        rkmpp_release_mppframe(mppframe, NULL);
+        return -1;
+    }
+
+    if(set_mppframe_to_avbuff(targetframe, frame, RKMPP_MPPFRAME_BUFINDEX - 1))
+        return -1;
+    if(set_mppframe_to_avbuff(mppframe, frame, RKMPP_MPPFRAME_BUFINDEX))
+        return -1;
+
+    if(rga_convert_mpp_mpp(avctx, mppframe, targetframe)){
+        if (informat == AV_PIX_FMT_NV16 && outformat == AV_PIX_FMT_NV12)
+            mpp_nv16_av_nv12_soft(mppframe, frame);
+        else if (informat == AV_PIX_FMT_NV16 && outformat == AV_PIX_FMT_YUV420P)
+            mpp_nv16_av_yuv420p_soft(mppframe, frame);
+        else if (informat == AV_PIX_FMT_NV12 && outformat == AV_PIX_FMT_YUV420P)
+            mpp_nv12_av_yuv420p_soft(mppframe, frame);
+        else {
+            ret = -1;
+            av_log(avctx, AV_LOG_ERROR, "No software conversion for %s -> %s available\n",
+                    av_get_pix_fmt_name(informat), av_get_pix_fmt_name(outformat));
+        }
+    }
+
+    return ret;
+}
+
+MppFrame import_drm_to_mpp(AVCodecContext *avctx, AVFrame *frame){
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    MppFrame mppframe = NULL;
+    AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor*) frame->data[0];
+    AVDRMLayerDescriptor *layer = &desc->layers[0];
+    rkformat format;
+    char drmname[4];
+    DRMFORMATNAME(drmname, layer->format)
+
+    if(rkmpp_get_drm_format(&format, layer->format)){
+        av_log(avctx, AV_LOG_ERROR, "Unsupported DRM Format %s\n", drmname);
+        return NULL;
+    }
+
+    if(format.drm == DRM_FORMAT_NV15){
+        // encoder does not support 10bit frames, we down scale them to 8bit
+        MppFrame nv15frame = create_mpp_frame(frame->width, frame->height, AV_PIX_FMT_NONE, NULL, desc, NULL);
+        if(nv15frame){
+            mppframe = create_mpp_frame(frame->width, frame->height, AV_PIX_FMT_NV12, codec->buffer_group, NULL, NULL);
+            if(mppframe && rga_convert_mpp_mpp(avctx, nv15frame, mppframe))
+                rkmpp_release_mppframe(mppframe, NULL);
+            rkmpp_release_mppframe(nv15frame, NULL);
+        }
+    } else {
+        mppframe = create_mpp_frame(frame->width, frame->height, format.av, NULL, desc, NULL);
+    }
+
+    return mppframe;
+}
+
+int import_mpp_to_drm(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame)
+{
+    // mppframe & desc is cleared when AVFrame is released
+    RKMPPCodecContext *rk_context = avctx->priv_data;
+    RKMPPCodec *codec = (RKMPPCodec *)rk_context->codec_ref->data;
+    AVDRMFrameDescriptor *desc = NULL;
+    AVDRMLayerDescriptor *layer = NULL;
+    rkformat format;
+    MppBuffer buffer = mpp_frame_get_buffer(mppframe);
+    int hstride = mpp_frame_get_hor_stride(mppframe);
+    int vstride = mpp_frame_get_ver_stride(mppframe);
+    int ret;
+
+    rkmpp_get_mpp_format(&format, mpp_frame_get_fmt(mppframe) & MPP_FRAME_FMT_MASK);
+
+    if(set_mppframe_to_avbuff(mppframe, frame, -1)){
+        ret = AVERROR(ENOMEM);
+        goto error;
+    }
+
+    desc = av_mallocz(sizeof(AVDRMFrameDescriptor));
+    if (!desc || set_drmdesc_to_avbuff(desc, frame) < 0) {
+        ret = AVERROR(ENOMEM);
+        goto error;
+    }
+
+    desc->nb_objects = 1;
+    desc->objects[0].fd = mpp_buffer_get_fd(buffer);
+    desc->objects[0].size = mpp_buffer_get_size(buffer);
+
+    desc->nb_layers = 1;
+    layer = &desc->layers[0];
+    layer->format = format.drm;
+    layer->nb_planes = 2;
+
+    layer->planes[0].object_index = 0;
+    layer->planes[0].offset = 0;
+    layer->planes[0].pitch = hstride;
+
+    layer->planes[1].object_index = 0;
+    layer->planes[1].offset = hstride * vstride;
+    layer->planes[1].pitch = hstride;
+
+    frame->data[0]  = (uint8_t *)desc;
+
+    frame->hw_frames_ctx = av_buffer_ref(codec->hwframes_ref);
+    if (!frame->hw_frames_ctx) {
+        ret = AVERROR(ENOMEM);
+        goto error;
+    }
+
+    return 0;
+
+error:
+    av_log(avctx, AV_LOG_ERROR, "Memory Error during importing mpp frame to drmprime\n");
+    if (mppframe)
+        rkmpp_release_mppframe(mppframe, NULL);
+    if (desc)
+        rkmpp_release_drm_desc(desc, NULL);
+
+    return ret;
+}
+
+MppFrame get_mppframe_from_av(AVFrame *frame){
+    if(frame->data[RKMPP_MPPFRAME_BUFINDEX]){
+        rkmpp_frame_type * mppframe = (rkmpp_frame_type *) frame->data[RKMPP_MPPFRAME_BUFINDEX];
+        if(mppframe->name && !strcmp(mppframe->name, "mpp_frame") &&
+                mpp_frame_get_fmt(frame->data[RKMPP_MPPFRAME_BUFINDEX]) != MPP_FMT_YUV420SP_10BIT)
+            return frame->data[RKMPP_MPPFRAME_BUFINDEX];
+    }
+    return NULL;
+}
diff --git a/libavcodec/rkplane.h b/libavcodec/rkplane.h
new file mode 100644
index 0000000000..f52d29228b
--- /dev/null
+++ b/libavcodec/rkplane.h
@@ -0,0 +1,17 @@
+#include <rockchip/rk_mpi.h>
+#include "avcodec.h"
+
+typedef struct {
+    char  *name;
+} rkmpp_frame_type;
+
+int rga_convert_mpp_mpp(AVCodecContext *avctx, MppFrame in_mppframe, MppFrame out_mppframe);
+int mpp_nv15_av_yuv420p(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame);
+int mpp_nv15_av_nv12(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame);
+int mpp_nv12_av_nv12(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame);
+int convert_mpp_to_av(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame,
+        enum AVPixelFormat informat, enum AVPixelFormat outformat);
+MppFrame create_mpp_frame(int width, int height, enum AVPixelFormat avformat, MppBufferGroup buffer_group, AVDRMFrameDescriptor *desc, AVFrame *frame);
+MppFrame import_drm_to_mpp(AVCodecContext *avctx, AVFrame *frame);
+int import_mpp_to_drm(AVCodecContext *avctx, MppFrame mppframe, AVFrame *frame);
+MppFrame get_mppframe_from_av(AVFrame *frame);
-- 
2.41.0


From f3fcb573fb6cf83dd5c9f144592fb047a575d833 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 24 Jul 2023 19:27:37 +0200
Subject: [PATCH 10/13] lavu/hwcontext_drm: Add internal frame allocation

---
 libavutil/hwcontext_drm.c | 322 +++++++++++++++++++++++++++++++++++++-
 libavutil/hwcontext_drm.h |  14 +-
 2 files changed, 328 insertions(+), 8 deletions(-)

diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
index 7a9fdbd263..2d1962c9c0 100644
--- a/libavutil/hwcontext_drm.c
+++ b/libavutil/hwcontext_drm.c
@@ -18,6 +18,7 @@
 
 #include "config.h"
 
+#define _GNU_SOURCE
 #include <fcntl.h>
 #include <sys/mman.h>
 #include <unistd.h>
@@ -30,7 +31,7 @@
 #include <sys/ioctl.h>
 #endif
 
-#include <drm.h>
+#include <drm_fourcc.h>
 #include <xf86drm.h>
 
 #include "avassert.h"
@@ -39,6 +40,103 @@
 #include "hwcontext_internal.h"
 #include "imgutils.h"
 
+/**
+ * Copy from libdrm_macros.h while is not exposed by libdrm,
+ * be replaced by #include "libdrm_macros.h" someday.
+ */
+
+/**
+ * Static (compile-time) assertion.
+ * Basically, use COND to dimension an array.  If COND is false/zero the
+ * array size will be -1 and we'll get a compilation error.
+ */
+#define STATIC_ASSERT(COND) \
+   do { \
+      (void) sizeof(char [1 - 2*!(COND)]); \
+   } while (0)
+
+#if defined(ANDROID) && !defined(__LP64__)
+#include <errno.h> /* for EINVAL */
+
+extern void *__mmap2(void *, size_t, int, int, int, size_t);
+
+static inline void *drm_mmap(void *addr, size_t length, int prot, int flags,
+                             int fd, loff_t offset)
+{
+   /* offset must be aligned to 4096 (not necessarily the page size) */
+   if (offset & 4095) {
+      errno = EINVAL;
+      return MAP_FAILED;
+   }
+
+   return __mmap2(addr, length, prot, flags, fd, (size_t) (offset >> 12));
+}
+
+#  define drm_munmap(addr, length) \
+              munmap(addr, length)
+
+#else
+
+/* assume large file support exists */
+#  define drm_mmap(addr, length, prot, flags, fd, offset) \
+              mmap(addr, length, prot, flags, fd, offset)
+
+static inline int drm_munmap(void *addr, size_t length)
+{
+   /* Copied from configure code generated by AC_SYS_LARGEFILE */
+#define LARGE_OFF_T ((((off_t) 1 << 31) << 31) - 1 + \
+                     (((off_t) 1 << 31) << 31))
+   STATIC_ASSERT(LARGE_OFF_T % 2147483629 == 721 &&
+                 LARGE_OFF_T % 2147483647 == 1);
+#undef LARGE_OFF_T
+
+   return munmap(addr, length);
+}
+#endif
+
+// default
+static int card_index = 0;
+
+static const struct {
+    enum AVPixelFormat pixfmt;
+    uint32_t drm_format;
+} supported_formats[] = {
+    { AV_PIX_FMT_NV12,      DRM_FORMAT_NV12,        },
+//#ifdef DRM_FORMAT_NV12_10
+//    { AV_PIX_FMT_P010LE,    DRM_FORMAT_NV12_10,     },
+//    { AV_PIX_FMT_P010BE,    DRM_FORMAT_NV12_10  | DRM_FORMAT_BIG_ENDIAN },
+//#endif
+    { AV_PIX_FMT_NV21,      DRM_FORMAT_NV21,        },
+    { AV_PIX_FMT_YUV420P,   DRM_FORMAT_YUV420,      },
+    { AV_PIX_FMT_YUYV422,   DRM_FORMAT_YUYV,        },
+    { AV_PIX_FMT_YVYU422,   DRM_FORMAT_YVYU,        },
+    { AV_PIX_FMT_UYVY422,   DRM_FORMAT_UYVY,        },
+    { AV_PIX_FMT_NV16,      DRM_FORMAT_NV16,        },
+    { AV_PIX_FMT_YUV422P,   DRM_FORMAT_YUV422,      },
+#ifdef DRM_FORMAT_R16
+    { AV_PIX_FMT_GRAY16LE,  DRM_FORMAT_R16,         },
+    { AV_PIX_FMT_GRAY16BE,  DRM_FORMAT_R16      | DRM_FORMAT_BIG_ENDIAN },
+#endif
+    { AV_PIX_FMT_BGR8,      DRM_FORMAT_BGR233,      },
+    { AV_PIX_FMT_RGB555LE,  DRM_FORMAT_XRGB1555,    },
+    { AV_PIX_FMT_RGB555BE,  DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
+    { AV_PIX_FMT_BGR555LE,  DRM_FORMAT_XBGR1555,    },
+    { AV_PIX_FMT_BGR555BE,  DRM_FORMAT_XBGR1555 | DRM_FORMAT_BIG_ENDIAN },
+    { AV_PIX_FMT_RGB565LE,  DRM_FORMAT_RGB565,      },
+    { AV_PIX_FMT_RGB565BE,  DRM_FORMAT_RGB565   | DRM_FORMAT_BIG_ENDIAN },
+    { AV_PIX_FMT_BGR565LE,  DRM_FORMAT_BGR565,      },
+    { AV_PIX_FMT_BGR565BE,  DRM_FORMAT_BGR565   | DRM_FORMAT_BIG_ENDIAN },
+    { AV_PIX_FMT_RGB24,     DRM_FORMAT_RGB888,      },
+    { AV_PIX_FMT_BGR24,     DRM_FORMAT_BGR888,      },
+    { AV_PIX_FMT_0RGB,      DRM_FORMAT_BGRX8888,    },
+    { AV_PIX_FMT_0BGR,      DRM_FORMAT_RGBX8888,    },
+    { AV_PIX_FMT_RGB0,      DRM_FORMAT_XBGR8888,    },
+    { AV_PIX_FMT_BGR0,      DRM_FORMAT_XRGB8888,    },
+    { AV_PIX_FMT_ARGB,      DRM_FORMAT_BGRA8888,    },
+    { AV_PIX_FMT_ABGR,      DRM_FORMAT_RGBA8888,    },
+    { AV_PIX_FMT_RGBA,      DRM_FORMAT_ABGR8888,    },
+    { AV_PIX_FMT_BGRA,      DRM_FORMAT_ARGB8888,    },
+};
 
 static void drm_device_free(AVHWDeviceContext *hwdev)
 {
@@ -52,7 +150,14 @@ static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
 {
     AVDRMDeviceContext *hwctx = hwdev->hwctx;
     drmVersionPtr version;
-
+    char drm_dev[] = "/dev/dri/card0000";
+    uint64_t has_dumb;
+ 
+    if (!device) {
+        snprintf(drm_dev, sizeof(drm_dev), DRM_DEV_NAME, DRM_DIR_NAME,
+                 card_index);
+        device = drm_dev;
+    }
     hwctx->fd = open(device, O_RDWR);
     if (hwctx->fd < 0)
         return AVERROR(errno);
@@ -72,11 +177,219 @@ static int drm_device_create(AVHWDeviceContext *hwdev, const char *device,
 
     drmFreeVersion(version);
 
+    if (drmGetCap(hwctx->fd, DRM_CAP_DUMB_BUFFER, &has_dumb) < 0 ||
+        !has_dumb) {
+        av_log(hwdev, AV_LOG_ERROR, "drm device '%s' "
+               "does not support dumb buffers\n", device);
+        close(hwctx->fd);
+        return AVERROR(EINVAL);
+    }
+
     hwdev->free = &drm_device_free;
 
     return 0;
 }
 
+static int drm_frames_get_constraints(AVHWDeviceContext *hwdev,
+                                      const void *hwconfig,
+                                      AVHWFramesConstraints *constraints)
+{
+    int i;
+
+    constraints->min_width = 16;
+    constraints->min_height = 16;
+
+    constraints->valid_hw_formats =
+        av_malloc_array(2, sizeof(enum AVPixelFormat));
+    if (!constraints->valid_hw_formats)
+        return AVERROR(ENOMEM);
+    constraints->valid_hw_formats[0] = AV_PIX_FMT_DRM_PRIME;
+    constraints->valid_hw_formats[1] = AV_PIX_FMT_NONE;
+
+    constraints->valid_sw_formats =
+        av_malloc_array(FF_ARRAY_ELEMS(supported_formats) + 1,
+                        sizeof(enum AVPixelFormat));
+    if (!constraints->valid_sw_formats)
+        return AVERROR(ENOMEM);
+    for(i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
+        constraints->valid_sw_formats[i] = supported_formats[i].pixfmt;
+    constraints->valid_sw_formats[i] = AV_PIX_FMT_NONE;
+
+    return 0;
+}
+
+static void free_drm_frame_descriptor(AVDRMDeviceContext *hwctx,
+                                      AVDRMFrameDescriptor *desc)
+{
+    int i;
+    if (!desc)
+        return;
+
+    for (i = 0; i < desc->nb_objects; i++) {
+        AVDRMObjectDescriptor *object = &desc->objects[i];
+        if (object->ptr)
+            drm_munmap(object->ptr, object->size);
+        if (object->fd > 0) {
+            int ret;
+            uint32_t handle = 0;
+
+            ret = drmPrimeFDToHandle(hwctx->fd, object->fd, &handle);
+            if (ret)
+                av_log(NULL, AV_LOG_WARNING,
+                       "Failed to convert drm fd to handle: %m\n");
+            if (handle > 0) {
+                struct drm_mode_destroy_dumb data = {
+                    .handle = handle,
+                };
+                ret = drmIoctl(hwctx->fd, DRM_IOCTL_MODE_DESTROY_DUMB, &data);
+                if (ret)
+                    av_log(NULL, AV_LOG_WARNING,
+                           "Failed to free drm handle: %m\n");
+            }
+
+            ret = close(object->fd);
+            if (ret)
+                av_log(NULL, AV_LOG_WARNING,
+                       "Failed to close drm buffer fd = %d: %m\n", object->fd);
+        }
+    }
+
+    memset(desc, 0, sizeof(*desc));
+    av_free(desc);
+}
+
+static void drm_buffer_free(void *opaque, uint8_t *data)
+{
+    AVHWFramesContext *hwfc = opaque;
+    AVDRMDeviceContext *hwctx = hwfc->device_ctx->hwctx;
+    AVDRMFrameDescriptor *desc = (AVDRMFrameDescriptor *)data;
+
+    free_drm_frame_descriptor(hwctx, desc);
+}
+
+static AVBufferRef *drm_pool_alloc(void *opaque, size_t size)
+{
+    int ret;
+    AVHWFramesContext *hwfc = opaque;
+    AVDRMDeviceContext *hwctx = hwfc->device_ctx->hwctx;
+    AVDRMFrameDescriptor *desc;
+    AVDRMLayerDescriptor *layer;
+    AVBufferRef *ref;
+
+    int i;
+    const AVPixFmtDescriptor *pixdesc = av_pix_fmt_desc_get(hwfc->sw_format);
+    struct drm_mode_create_dumb dmcb;
+    struct drm_mode_map_dumb dmmd;
+
+    desc = av_mallocz(sizeof(*desc));
+    if (!desc)
+        return NULL;
+
+    memset(&dmcb, 0, sizeof(struct drm_mode_create_dumb));
+    dmcb.bpp = av_get_bits_per_pixel(pixdesc);
+    dmcb.width  = FFALIGN(hwfc->width, 16);
+    dmcb.height = FFALIGN(hwfc->height, 16);
+    ret = drmIoctl(hwctx->fd, DRM_IOCTL_MODE_CREATE_DUMB, &dmcb);
+    if (ret < 0) {
+        av_log(hwfc, AV_LOG_ERROR,
+               "Failed to create dumb<w,h,bpp: %d,%d,%d>: %m.\n",
+               dmcb.width, dmcb.height, dmcb.bpp);
+        goto fail;
+    }
+    av_assert0(dmcb.size >= dmcb.width * dmcb.height * dmcb.bpp / 8);
+
+    desc->nb_objects = 1;
+    desc->nb_layers = 1;
+    ret = drmPrimeHandleToFD(hwctx->fd, dmcb.handle, DRM_CLOEXEC | DRM_RDWR,
+                             &desc->objects[0].fd);
+    if (ret) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to convert handle to fd: %m\n");
+        goto fail;
+    }
+    memset(&dmmd, 0, sizeof(dmmd));
+    dmmd.handle = dmcb.handle;
+
+    ret = drmIoctl(hwctx->fd, DRM_IOCTL_MODE_MAP_DUMB, &dmmd);
+    if (ret) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to map dumb: %m\n");
+        goto fail;
+    }
+
+    // default read and write
+    desc->objects[0].ptr = drm_mmap(NULL, dmcb.size, PROT_READ | PROT_WRITE,
+                                    MAP_SHARED, hwctx->fd, dmmd.offset);
+    if (desc->objects[0].ptr == MAP_FAILED) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to drm_mmap: %m\n");
+        goto fail;
+    }
+
+    desc->objects[0].size = dmcb.size;
+
+    layer = &desc->layers[0];
+    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++) {
+        if (supported_formats[i].pixfmt == hwfc->sw_format) {
+            layer->format = supported_formats[i].drm_format;
+            break;
+        }
+    }
+    layer->nb_planes = av_pix_fmt_count_planes(hwfc->sw_format);
+    layer->planes[0].object_index = 0;
+    layer->planes[0].offset = 0;
+    layer->planes[0].pitch =
+        av_image_get_linesize(hwfc->sw_format, hwfc->width, 0);
+
+    for (i = 1; i < layer->nb_planes; i++) {
+        layer->planes[i].object_index = 0;
+        layer->planes[i].offset = layer->planes[i-1].pitch * hwfc->height;
+        layer->planes[i].pitch =
+            av_image_get_linesize(hwfc->sw_format, hwfc->width, i);
+    }
+
+    ref = av_buffer_create((uint8_t*)desc, sizeof(*desc), drm_buffer_free,
+                           opaque, 0);
+    if (!ref) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to create drm buffer.\n");
+        goto fail;
+    }
+
+    return ref;
+
+fail:
+    free_drm_frame_descriptor(hwctx, desc);
+    return NULL;
+}
+
+static int drm_frames_init(AVHWFramesContext *hwfc)
+{
+    int i;
+    if (hwfc->pool) {
+        // has been set outside?
+        return 0;
+    }
+
+    for (i = 0; i < FF_ARRAY_ELEMS(supported_formats); i++)
+        if (supported_formats[i].pixfmt == hwfc->sw_format)
+            break;
+    if (i >= FF_ARRAY_ELEMS(supported_formats)) {
+        av_log(hwfc, AV_LOG_ERROR, "Unsupported format: %s.\n",
+               av_get_pix_fmt_name(hwfc->sw_format));
+        return AVERROR(EINVAL);
+    }
+
+    hwfc->internal->pool_internal =
+        av_buffer_pool_init2(sizeof(AVDRMFrameDescriptor), hwfc, drm_pool_alloc,
+                             NULL);
+    if (!hwfc->internal->pool_internal) {
+        av_log(hwfc, AV_LOG_ERROR, "Failed to create drm buffer pool.\n");
+        return AVERROR(ENOMEM);
+    }
+
+    return 0;
+}
+
+static void drm_frames_uninit(AVHWFramesContext *hwfc av_unused)
+{}
+
 static int drm_get_buffer(AVHWFramesContext *hwfc, AVFrame *frame)
 {
     frame->buf[0] = av_buffer_pool_get(hwfc->pool);
@@ -309,8 +622,11 @@ const HWContextType ff_hwcontext_type_drm = {
 
     .device_create          = &drm_device_create,
 
-    .frames_get_buffer      = &drm_get_buffer,
+    .frames_get_constraints = &drm_frames_get_constraints,
 
+    .frames_get_buffer      = &drm_get_buffer,
+    .frames_init            = &drm_frames_init,
+    .frames_uninit          = &drm_frames_uninit,
     .transfer_get_formats   = &drm_transfer_get_formats,
     .transfer_data_to       = &drm_transfer_data_to,
     .transfer_data_from     = &drm_transfer_data_from,
diff --git a/libavutil/hwcontext_drm.h b/libavutil/hwcontext_drm.h
index 42709f215e..80f2e8597e 100644
--- a/libavutil/hwcontext_drm.h
+++ b/libavutil/hwcontext_drm.h
@@ -25,11 +25,6 @@
 /**
  * @file
  * API-specific header for AV_HWDEVICE_TYPE_DRM.
- *
- * Internal frame allocation is not currently supported - all frames
- * must be allocated by the user.  Thus AVHWFramesContext is always
- * NULL, though this may change if support for frame allocation is
- * added in future.
  */
 
 enum {
@@ -50,6 +45,12 @@ typedef struct AVDRMObjectDescriptor {
      * DRM PRIME fd for the object.
      */
     int fd;
+    /**
+     * DRM PRIME mapped virtual ptr for above fd.
+     *
+     * The content of this buffer must be readonly when acting decoder's out buffer.
+     */
+    void *ptr;
     /**
      * Total size of the object.
      *
@@ -141,10 +142,13 @@ typedef struct AVDRMFrameDescriptor {
     AVDRMObjectDescriptor objects[AV_DRM_MAX_PLANES];
     /**
      * Number of layers in the frame.
+     *
+     * Set by users if need more than 1.
      */
     int nb_layers;
     /**
      * Array of layers in the frame.
+     * NOTE: total planes of layers must not be more than AV_NUM_DATA_POINTERS.
      */
     AVDRMLayerDescriptor layers[AV_DRM_MAX_PLANES];
 } AVDRMFrameDescriptor;
-- 
2.41.0


From 51962528d54301898213fb92bb593a707f2bf87b Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 24 Jul 2023 22:19:14 +0200
Subject: [PATCH 11/13] add yuv444 & nv24 support for drm hwcontext

---
 libavutil/hwcontext_drm.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
index 2d1962c9c0..9210b6c9f5 100644
--- a/libavutil/hwcontext_drm.c
+++ b/libavutil/hwcontext_drm.c
@@ -117,6 +117,8 @@ static const struct {
     { AV_PIX_FMT_GRAY16LE,  DRM_FORMAT_R16,         },
     { AV_PIX_FMT_GRAY16BE,  DRM_FORMAT_R16      | DRM_FORMAT_BIG_ENDIAN },
 #endif
+    { AV_PIX_FMT_NV24,      DRM_FORMAT_NV24,        },
+    { AV_PIX_FMT_YUV444P,   DRM_FORMAT_YUV444,        },
     { AV_PIX_FMT_BGR8,      DRM_FORMAT_BGR233,      },
     { AV_PIX_FMT_RGB555LE,  DRM_FORMAT_XRGB1555,    },
     { AV_PIX_FMT_RGB555BE,  DRM_FORMAT_XRGB1555 | DRM_FORMAT_BIG_ENDIAN },
-- 
2.41.0


From de5398097e8c3aa9f2ef69b50d8be63c87932a19 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Fri, 28 Jul 2023 22:08:47 +0200
Subject: [PATCH 12/13] add AV_PIX_FMT_NV15 definition as a placeholder to
 enable supported DRM frames

---
 libavcodec/rkmpp.c        | 2 +-
 libavutil/hwcontext_drm.c | 1 +
 libavutil/pixfmt.h        | 2 ++
 3 files changed, 4 insertions(+), 1 deletion(-)

diff --git a/libavcodec/rkmpp.c b/libavcodec/rkmpp.c
index a33a139569..22bd4c2171 100644
--- a/libavcodec/rkmpp.c
+++ b/libavcodec/rkmpp.c
@@ -27,7 +27,7 @@ static rkformat rkformats[13] = {
         { .av = AV_PIX_FMT_YUV422P, .mpp = MPP_FMT_YUV422P,        .drm = DRM_FORMAT_YUV422,   .rga = RK_FORMAT_YCbCr_422_P},
         { .av = AV_PIX_FMT_NV12,    .mpp = MPP_FMT_YUV420SP,       .drm = DRM_FORMAT_NV12,     .rga = RK_FORMAT_YCbCr_420_SP},
         { .av = AV_PIX_FMT_NV16,    .mpp = MPP_FMT_YUV422SP,       .drm = DRM_FORMAT_NV16,     .rga = RK_FORMAT_YCbCr_422_SP},
-        { .av = AV_PIX_FMT_NONE,    .mpp = MPP_FMT_YUV420SP_10BIT, .drm = DRM_FORMAT_NV15,     .rga = RK_FORMAT_YCbCr_420_SP_10B},
+        { .av = AV_PIX_FMT_NV15,    .mpp = MPP_FMT_YUV420SP_10BIT, .drm = DRM_FORMAT_NV15,     .rga = RK_FORMAT_YCbCr_420_SP_10B},
         { .av = AV_PIX_FMT_BGR24,   .mpp = MPP_FMT_BGR888,         .drm = DRM_FORMAT_BGR888,   .rga = RK_FORMAT_BGR_888},
         { .av = AV_PIX_FMT_BGR0,    .mpp = MPP_FMT_BGRA8888,       .drm = DRM_FORMAT_XRGB8888, .rga = RK_FORMAT_BGRX_8888},
         { .av = AV_PIX_FMT_BGRA,    .mpp = MPP_FMT_BGRA8888,       .drm = DRM_FORMAT_ARGB8888, .rga = RK_FORMAT_BGRA_8888},
diff --git a/libavutil/hwcontext_drm.c b/libavutil/hwcontext_drm.c
index 9210b6c9f5..32bf958c5d 100644
--- a/libavutil/hwcontext_drm.c
+++ b/libavutil/hwcontext_drm.c
@@ -138,6 +138,7 @@ static const struct {
     { AV_PIX_FMT_ABGR,      DRM_FORMAT_RGBA8888,    },
     { AV_PIX_FMT_RGBA,      DRM_FORMAT_ABGR8888,    },
     { AV_PIX_FMT_BGRA,      DRM_FORMAT_ARGB8888,    },
+    { AV_PIX_FMT_NV15,      DRM_FORMAT_NV15,        },
 };
 
 static void drm_device_free(AVHWDeviceContext *hwdev)
diff --git a/libavutil/pixfmt.h b/libavutil/pixfmt.h
index 46ef211add..b04975fba0 100644
--- a/libavutil/pixfmt.h
+++ b/libavutil/pixfmt.h
@@ -360,6 +360,8 @@ enum AVPixelFormat {
 
     AV_PIX_FMT_X2RGB10LE, ///< packed RGB 10:10:10, 30bpp, (msb)2X 10R 10G 10B(lsb), little-endian, X=unused/undefined
     AV_PIX_FMT_X2RGB10BE, ///< packed RGB 10:10:10, 30bpp, (msb)2X 10R 10G 10B(lsb), big-endian, X=unused/undefined
+    AV_PIX_FMT_NV15,        ///< Rockchip Specific 10bit NV12
+
     AV_PIX_FMT_NB         ///< number of pixel formats, DO NOT USE THIS if you want to link with shared libav* because the number of formats might differ between versions
 };
 
-- 
2.41.0


From 0e4b85d085cb8ce785185c92c91ba90411a20ed4 Mon Sep 17 00:00:00 2001
From: boogie <boogiepop@gmx.com>
Date: Mon, 31 Jul 2023 01:32:54 +0200
Subject: [PATCH 13/13] give pixel descriptor to prevent crashes. descriptor
 might not be %100 correct

---
 libavutil/pixdesc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/libavutil/pixdesc.c b/libavutil/pixdesc.c
index 18c7a0efc8..3216dc19e3 100644
--- a/libavutil/pixdesc.c
+++ b/libavutil/pixdesc.c
@@ -2395,6 +2395,18 @@ static const AVPixFmtDescriptor av_pix_fmt_descriptors[AV_PIX_FMT_NB] = {
         .name = "vulkan",
         .flags = AV_PIX_FMT_FLAG_HWACCEL,
     },
+    [AV_PIX_FMT_NV15] = {
+        .name = "nv15",
+        .nb_components = 3,
+        .log2_chroma_w = 1,
+        .log2_chroma_h = 1,
+        .comp = {
+            { 0, 1, 0, 0, 10 },        /* Y */
+            { 1, 2, 0, 0, 10 },        /* U */
+            { 1, 2, 1, 0, 10 },        /* V */
+        },
+        .flags = AV_PIX_FMT_FLAG_PLANAR,
+    },
 };
 #if FF_API_PLUS1_MINUS1
 FF_ENABLE_DEPRECATION_WARNINGS
-- 
2.41.0