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
|
/*
* Copyright (c) 2019, Alliance for Open Media. All rights reserved.
*
* This source code is subject to the terms of the BSD 2 Clause License and
* the Alliance for Open Media Patent License 1.0. If the BSD 2 Clause License
* was not distributed with this source code in the LICENSE file, you can
* obtain it at www.aomedia.org/license/software. If the Alliance for Open
* Media Patent License 1.0 was not distributed with this source code in the
* PATENTS file, you can obtain it at www.aomedia.org/license/patent.
*/
// This is an example demonstrating how to implement a multi-layer AOM
// encoding scheme for RTC video applications.
#include <assert.h>
#include <inttypes.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <memory>
#include "config/aom_config.h"
#if CONFIG_AV1_DECODER
#include "aom/aom_decoder.h"
#endif
#include "aom/aom_encoder.h"
#include "aom/aom_image.h"
#include "aom/aom_integer.h"
#include "aom/aomcx.h"
#include "aom_dsp/bitwriter_buffer.h"
#include "aom_ports/aom_timer.h"
#include "av1/ratectrl_rtc.h"
#include "common/args.h"
#include "common/tools_common.h"
#include "common/video_writer.h"
#include "examples/encoder_util.h"
#include "examples/multilayer_metadata.h"
#define OPTION_BUFFER_SIZE 1024
#define MAX_NUM_SPATIAL_LAYERS 4
#define GOOD_QUALITY 0
typedef struct {
const char *output_filename;
char options[OPTION_BUFFER_SIZE];
struct AvxInputContext input_ctx[MAX_NUM_SPATIAL_LAYERS];
int speed;
int aq_mode;
int layering_mode;
int output_obu;
int decode;
int tune_content;
int show_psnr;
bool use_external_rc;
bool scale_factors_explicitly_set;
const char *multilayer_metadata_file;
} AppInput;
typedef enum {
QUANTIZER = 0,
BITRATE,
SCALE_FACTOR,
AUTO_ALT_REF,
ALL_OPTION_TYPES
} LAYER_OPTION_TYPE;
enum { kSkip = 0, kDeltaQ = 1, kDeltaLF = 2, kReference = 3 };
static const arg_def_t outputfile =
ARG_DEF("o", "output", 1, "Output filename");
static const arg_def_t frames_arg =
ARG_DEF("f", "frames", 1, "Number of frames to encode");
static const arg_def_t threads_arg =
ARG_DEF("th", "threads", 1, "Number of threads to use");
static const arg_def_t width_arg = ARG_DEF("w", "width", 1, "Source width");
static const arg_def_t height_arg = ARG_DEF("h", "height", 1, "Source height");
static const arg_def_t timebase_arg =
ARG_DEF("t", "timebase", 1, "Timebase (num/den)");
static const arg_def_t bitrate_arg = ARG_DEF(
"b", "target-bitrate", 1, "Encoding bitrate, in kilobits per second");
static const arg_def_t spatial_layers_arg =
ARG_DEF("sl", "spatial-layers", 1, "Number of spatial SVC layers");
static const arg_def_t temporal_layers_arg =
ARG_DEF("tl", "temporal-layers", 1, "Number of temporal SVC layers");
static const arg_def_t layering_mode_arg =
ARG_DEF("lm", "layering-mode", 1, "Temporal layering scheme.");
static const arg_def_t kf_dist_arg =
ARG_DEF("k", "kf-dist", 1, "Number of frames between keyframes");
static const arg_def_t scale_factors_arg =
ARG_DEF("r", "scale-factors", 1, "Scale factors (lowest to highest layer)");
static const arg_def_t min_q_arg =
ARG_DEF(NULL, "min-q", 1, "Minimum quantizer");
static const arg_def_t max_q_arg =
ARG_DEF(NULL, "max-q", 1, "Maximum quantizer");
static const arg_def_t speed_arg =
ARG_DEF("sp", "speed", 1, "Speed configuration");
static const arg_def_t aqmode_arg =
ARG_DEF("aq", "aqmode", 1, "AQ mode off/on");
static const arg_def_t bitrates_arg =
ARG_DEF("bl", "bitrates", 1,
"Bitrates[spatial_layer * num_temporal_layer + temporal_layer]");
static const arg_def_t dropframe_thresh_arg =
ARG_DEF(NULL, "drop-frame", 1, "Temporal resampling threshold (buf %)");
static const arg_def_t error_resilient_arg =
ARG_DEF(NULL, "error-resilient", 1, "Error resilient flag");
static const arg_def_t output_obu_arg =
ARG_DEF(NULL, "output-obu", 1,
"Write OBUs when set to 1. Otherwise write IVF files.");
static const arg_def_t test_decode_arg =
ARG_DEF(NULL, "test-decode", 1,
"Attempt to test decoding the output when set to 1. Default is 1.");
static const arg_def_t psnr_arg =
ARG_DEF(NULL, "psnr", -1, "Show PSNR in status line.");
static const arg_def_t ext_rc_arg =
ARG_DEF(NULL, "use-ext-rc", 0, "Use external rate control.");
static const struct arg_enum_list tune_content_enum[] = {
{ "default", AOM_CONTENT_DEFAULT },
{ "screen", AOM_CONTENT_SCREEN },
{ "film", AOM_CONTENT_FILM },
{ NULL, 0 }
};
static const arg_def_t tune_content_arg = ARG_DEF_ENUM(
NULL, "tune-content", 1, "Tune content type", tune_content_enum);
#if CONFIG_CWG_E050
static const arg_def_t multilayer_metadata_file_arg =
ARG_DEF("ml", "multilayer_metadata_file", 1,
"Experimental: path to multilayer metadata file");
#endif
#if CONFIG_AV1_HIGHBITDEPTH
static const struct arg_enum_list bitdepth_enum[] = { { "8", AOM_BITS_8 },
{ "10", AOM_BITS_10 },
{ NULL, 0 } };
static const arg_def_t bitdepth_arg = ARG_DEF_ENUM(
"d", "bit-depth", 1, "Bit depth for codec 8 or 10. ", bitdepth_enum);
#endif // CONFIG_AV1_HIGHBITDEPTH
static const arg_def_t *svc_args[] = {
&frames_arg,
&outputfile,
&width_arg,
&height_arg,
&timebase_arg,
&bitrate_arg,
&spatial_layers_arg,
&kf_dist_arg,
&scale_factors_arg,
&min_q_arg,
&max_q_arg,
&temporal_layers_arg,
&layering_mode_arg,
&threads_arg,
&aqmode_arg,
#if CONFIG_AV1_HIGHBITDEPTH
&bitdepth_arg,
#endif
&speed_arg,
&bitrates_arg,
&dropframe_thresh_arg,
&error_resilient_arg,
&output_obu_arg,
&test_decode_arg,
&tune_content_arg,
&psnr_arg,
#if CONFIG_CWG_E050
&multilayer_metadata_file_arg,
#endif
NULL,
};
#define zero(Dest) memset(&(Dest), 0, sizeof(Dest))
static const char *exec_name;
void usage_exit(void) {
fprintf(stderr,
"Usage: %s <options> input_filename [input_filename ...] -o "
"output_filename\n",
exec_name);
fprintf(stderr, "Options:\n");
arg_show_usage(stderr, svc_args);
fprintf(
stderr,
"Input files must be y4m or yuv.\n"
"If multiple input files are specified, they correspond to spatial "
"layers, and there should be as many as there are spatial layers.\n"
"All input files must have the same width, height, frame rate and number "
"of frames.\n"
"If only one file is specified, it is used for all spatial layers.\n");
exit(EXIT_FAILURE);
}
static int file_is_y4m(const char detect[4]) {
return memcmp(detect, "YUV4", 4) == 0;
}
static int fourcc_is_ivf(const char detect[4]) {
if (memcmp(detect, "DKIF", 4) == 0) {
return 1;
}
return 0;
}
static const int option_max_values[ALL_OPTION_TYPES] = { 63, INT_MAX, INT_MAX,
1 };
static const int option_min_values[ALL_OPTION_TYPES] = { 0, 0, 1, 0 };
static void open_input_file(struct AvxInputContext *input,
aom_chroma_sample_position_t csp) {
/* Parse certain options from the input file, if possible */
input->file = strcmp(input->filename, "-") ? fopen(input->filename, "rb")
: set_binary_mode(stdin);
if (!input->file) fatal("Failed to open input file");
if (!fseeko(input->file, 0, SEEK_END)) {
/* Input file is seekable. Figure out how long it is, so we can get
* progress info.
*/
input->length = ftello(input->file);
rewind(input->file);
}
/* Default to 1:1 pixel aspect ratio. */
input->pixel_aspect_ratio.numerator = 1;
input->pixel_aspect_ratio.denominator = 1;
/* For RAW input sources, these bytes will applied on the first frame
* in read_frame().
*/
input->detect.buf_read = fread(input->detect.buf, 1, 4, input->file);
input->detect.position = 0;
if (input->detect.buf_read == 4 && file_is_y4m(input->detect.buf)) {
if (y4m_input_open(&input->y4m, input->file, input->detect.buf, 4, csp,
input->only_i420) >= 0) {
input->file_type = FILE_TYPE_Y4M;
input->width = input->y4m.pic_w;
input->height = input->y4m.pic_h;
input->pixel_aspect_ratio.numerator = input->y4m.par_n;
input->pixel_aspect_ratio.denominator = input->y4m.par_d;
input->framerate.numerator = input->y4m.fps_n;
input->framerate.denominator = input->y4m.fps_d;
input->fmt = input->y4m.aom_fmt;
input->bit_depth = static_cast<aom_bit_depth_t>(input->y4m.bit_depth);
} else {
fatal("Unsupported Y4M stream.");
}
} else if (input->detect.buf_read == 4 && fourcc_is_ivf(input->detect.buf)) {
fatal("IVF is not supported as input.");
} else {
input->file_type = FILE_TYPE_RAW;
}
}
static aom_codec_err_t extract_option(LAYER_OPTION_TYPE type, char *input,
int *value0, int *value1) {
if (type == SCALE_FACTOR) {
*value0 = (int)strtol(input, &input, 10);
if (*input++ != '/') return AOM_CODEC_INVALID_PARAM;
*value1 = (int)strtol(input, &input, 10);
if (*value0 < option_min_values[SCALE_FACTOR] ||
*value1 < option_min_values[SCALE_FACTOR] ||
*value0 > option_max_values[SCALE_FACTOR] ||
*value1 > option_max_values[SCALE_FACTOR] ||
*value0 > *value1) // num shouldn't be greater than den
return AOM_CODEC_INVALID_PARAM;
} else {
*value0 = atoi(input);
if (*value0 < option_min_values[type] || *value0 > option_max_values[type])
return AOM_CODEC_INVALID_PARAM;
}
return AOM_CODEC_OK;
}
static aom_codec_err_t parse_layer_options_from_string(
aom_svc_params_t *svc_params, LAYER_OPTION_TYPE type, const char *input,
int *option0, int *option1) {
aom_codec_err_t res = AOM_CODEC_OK;
char *input_string;
char *token;
const char *delim = ",";
int num_layers = svc_params->number_spatial_layers;
int i = 0;
if (type == BITRATE)
num_layers =
svc_params->number_spatial_layers * svc_params->number_temporal_layers;
if (input == NULL || option0 == NULL ||
(option1 == NULL && type == SCALE_FACTOR))
return AOM_CODEC_INVALID_PARAM;
const size_t input_length = strlen(input);
input_string = reinterpret_cast<char *>(malloc(input_length + 1));
if (input_string == NULL) return AOM_CODEC_MEM_ERROR;
memcpy(input_string, input, input_length + 1);
token = strtok(input_string, delim); // NOLINT
for (i = 0; i < num_layers; ++i) {
if (token != NULL) {
res = extract_option(type, token, option0 + i, option1 + i);
if (res != AOM_CODEC_OK) break;
token = strtok(NULL, delim); // NOLINT
} else {
res = AOM_CODEC_INVALID_PARAM;
break;
}
}
free(input_string);
return res;
}
static void parse_command_line(int argc, const char **argv_,
AppInput *app_input,
aom_svc_params_t *svc_params,
aom_codec_enc_cfg_t *enc_cfg) {
struct arg arg;
char **argv = NULL;
char **argi = NULL;
char **argj = NULL;
char string_options[1024] = { 0 };
// Default settings
svc_params->number_spatial_layers = 1;
svc_params->number_temporal_layers = 1;
app_input->layering_mode = 0;
app_input->output_obu = 0;
app_input->decode = 1;
enc_cfg->g_threads = 1;
enc_cfg->rc_end_usage = AOM_CBR;
// process command line options
argv = argv_dup(argc - 1, argv_ + 1);
if (!argv) {
fprintf(stderr, "Error allocating argument list\n");
exit(EXIT_FAILURE);
}
for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
arg.argv_step = 1;
if (arg_match(&arg, &outputfile, argi)) {
app_input->output_filename = arg.val;
} else if (arg_match(&arg, &width_arg, argi)) {
enc_cfg->g_w = arg_parse_uint(&arg);
} else if (arg_match(&arg, &height_arg, argi)) {
enc_cfg->g_h = arg_parse_uint(&arg);
} else if (arg_match(&arg, &timebase_arg, argi)) {
enc_cfg->g_timebase = arg_parse_rational(&arg);
} else if (arg_match(&arg, &bitrate_arg, argi)) {
enc_cfg->rc_target_bitrate = arg_parse_uint(&arg);
} else if (arg_match(&arg, &spatial_layers_arg, argi)) {
svc_params->number_spatial_layers = arg_parse_uint(&arg);
} else if (arg_match(&arg, &temporal_layers_arg, argi)) {
svc_params->number_temporal_layers = arg_parse_uint(&arg);
} else if (arg_match(&arg, &speed_arg, argi)) {
app_input->speed = arg_parse_uint(&arg);
if (app_input->speed > 11) {
aom_tools_warn("Mapping speed %d to speed 11.\n", app_input->speed);
}
} else if (arg_match(&arg, &aqmode_arg, argi)) {
app_input->aq_mode = arg_parse_uint(&arg);
} else if (arg_match(&arg, &threads_arg, argi)) {
enc_cfg->g_threads = arg_parse_uint(&arg);
} else if (arg_match(&arg, &layering_mode_arg, argi)) {
app_input->layering_mode = arg_parse_int(&arg);
} else if (arg_match(&arg, &kf_dist_arg, argi)) {
enc_cfg->kf_min_dist = arg_parse_uint(&arg);
enc_cfg->kf_max_dist = enc_cfg->kf_min_dist;
} else if (arg_match(&arg, &scale_factors_arg, argi)) {
aom_codec_err_t res = parse_layer_options_from_string(
svc_params, SCALE_FACTOR, arg.val, svc_params->scaling_factor_num,
svc_params->scaling_factor_den);
app_input->scale_factors_explicitly_set = true;
if (res != AOM_CODEC_OK) {
die("Failed to parse scale factors: %s\n",
aom_codec_err_to_string(res));
}
} else if (arg_match(&arg, &min_q_arg, argi)) {
enc_cfg->rc_min_quantizer = arg_parse_uint(&arg);
} else if (arg_match(&arg, &max_q_arg, argi)) {
enc_cfg->rc_max_quantizer = arg_parse_uint(&arg);
#if CONFIG_AV1_HIGHBITDEPTH
} else if (arg_match(&arg, &bitdepth_arg, argi)) {
enc_cfg->g_bit_depth =
static_cast<aom_bit_depth_t>(arg_parse_enum_or_int(&arg));
switch (enc_cfg->g_bit_depth) {
case AOM_BITS_8:
enc_cfg->g_input_bit_depth = 8;
enc_cfg->g_profile = 0;
break;
case AOM_BITS_10:
enc_cfg->g_input_bit_depth = 10;
enc_cfg->g_profile = 0;
break;
default:
die("Error: Invalid bit depth selected (%d)\n", enc_cfg->g_bit_depth);
}
#endif // CONFIG_VP9_HIGHBITDEPTH
} else if (arg_match(&arg, &dropframe_thresh_arg, argi)) {
enc_cfg->rc_dropframe_thresh = arg_parse_uint(&arg);
} else if (arg_match(&arg, &error_resilient_arg, argi)) {
enc_cfg->g_error_resilient = arg_parse_uint(&arg);
if (enc_cfg->g_error_resilient != 0 && enc_cfg->g_error_resilient != 1)
die("Invalid value for error resilient (0, 1): %d.",
enc_cfg->g_error_resilient);
} else if (arg_match(&arg, &output_obu_arg, argi)) {
app_input->output_obu = arg_parse_uint(&arg);
if (app_input->output_obu != 0 && app_input->output_obu != 1)
die("Invalid value for obu output flag (0, 1): %d.",
app_input->output_obu);
} else if (arg_match(&arg, &test_decode_arg, argi)) {
app_input->decode = arg_parse_uint(&arg);
if (app_input->decode != 0 && app_input->decode != 1)
die("Invalid value for test decode flag (0, 1): %d.",
app_input->decode);
} else if (arg_match(&arg, &tune_content_arg, argi)) {
app_input->tune_content = arg_parse_enum_or_int(&arg);
printf("tune content %d\n", app_input->tune_content);
} else if (arg_match(&arg, &psnr_arg, argi)) {
app_input->show_psnr = 1;
} else if (arg_match(&arg, &ext_rc_arg, argi)) {
app_input->use_external_rc = true;
#if CONFIG_CWG_E050
} else if (arg_match(&arg, &multilayer_metadata_file_arg, argi)) {
app_input->multilayer_metadata_file = arg.val;
#endif
} else {
++argj;
}
}
// Total bitrate needs to be parsed after the number of layers.
for (argi = argj = argv; (*argj = *argi); argi += arg.argv_step) {
arg.argv_step = 1;
if (arg_match(&arg, &bitrates_arg, argi)) {
aom_codec_err_t res = parse_layer_options_from_string(
svc_params, BITRATE, arg.val, svc_params->layer_target_bitrate, NULL);
if (res != AOM_CODEC_OK) {
die("Failed to parse bitrates: %s\n", aom_codec_err_to_string(res));
}
} else {
++argj;
}
}
// There will be a space in front of the string options
if (strlen(string_options) > 0)
strncpy(app_input->options, string_options, OPTION_BUFFER_SIZE);
// Check for unrecognized options
for (argi = argv; *argi; ++argi)
if (argi[0][0] == '-' && strlen(argi[0]) > 1)
die("Error: Unrecognized option %s\n", *argi);
if (argv[0] == NULL) {
usage_exit();
}
int input_count = 0;
while (argv[input_count] != NULL && input_count < MAX_NUM_SPATIAL_LAYERS) {
app_input->input_ctx[input_count].filename = argv[input_count];
++input_count;
}
if (input_count > 1 && input_count != svc_params->number_spatial_layers) {
die("Error: Number of input files does not match number of spatial layers");
}
if (argv[input_count] != NULL) {
die("Error: Too many input files specified, there should be at most %d",
MAX_NUM_SPATIAL_LAYERS);
}
free(argv);
for (int i = 0; i < input_count; ++i) {
open_input_file(&app_input->input_ctx[i], AOM_CSP_UNKNOWN);
if (app_input->input_ctx[i].file_type == FILE_TYPE_Y4M) {
if (enc_cfg->g_w == 0 || enc_cfg->g_h == 0) {
// Override these settings with the info from Y4M file.
enc_cfg->g_w = app_input->input_ctx[i].width;
enc_cfg->g_h = app_input->input_ctx[i].height;
// g_timebase is the reciprocal of frame rate.
enc_cfg->g_timebase.num = app_input->input_ctx[i].framerate.denominator;
enc_cfg->g_timebase.den = app_input->input_ctx[i].framerate.numerator;
} else if (enc_cfg->g_w != app_input->input_ctx[i].width ||
enc_cfg->g_h != app_input->input_ctx[i].height ||
enc_cfg->g_timebase.num !=
app_input->input_ctx[i].framerate.denominator ||
enc_cfg->g_timebase.den !=
app_input->input_ctx[i].framerate.numerator) {
die("Error: Input file dimensions and/or frame rate mismatch");
}
}
}
if (enc_cfg->g_w == 0 || enc_cfg->g_h == 0) {
die("Error: Input file dimensions not set, use -w and -h");
}
if (enc_cfg->g_w < 16 || enc_cfg->g_w % 2 || enc_cfg->g_h < 16 ||
enc_cfg->g_h % 2)
die("Invalid resolution: %d x %d\n", enc_cfg->g_w, enc_cfg->g_h);
printf(
"Codec %s\n"
"layers: %d\n"
"width %u, height: %u\n"
"num: %d, den: %d, bitrate: %u\n"
"gop size: %u\n",
aom_codec_iface_name(aom_codec_av1_cx()),
svc_params->number_spatial_layers, enc_cfg->g_w, enc_cfg->g_h,
enc_cfg->g_timebase.num, enc_cfg->g_timebase.den,
enc_cfg->rc_target_bitrate, enc_cfg->kf_max_dist);
}
static const int mode_to_num_temporal_layers[12] = {
1, 2, 3, 3, 2, 1, 1, 3, 3, 3, 3, 3,
};
static const int mode_to_num_spatial_layers[12] = {
1, 1, 1, 1, 1, 2, 3, 2, 3, 3, 3, 3,
};
// For rate control encoding stats.
struct RateControlMetrics {
// Number of input frames per layer.
int layer_input_frames[AOM_MAX_TS_LAYERS];
// Number of encoded non-key frames per layer.
int layer_enc_frames[AOM_MAX_TS_LAYERS];
// Framerate per layer layer (cumulative).
double layer_framerate[AOM_MAX_TS_LAYERS];
// Target average frame size per layer (per-frame-bandwidth per layer).
double layer_pfb[AOM_MAX_LAYERS];
// Actual average frame size per layer.
double layer_avg_frame_size[AOM_MAX_LAYERS];
// Average rate mismatch per layer (|target - actual| / target).
double layer_avg_rate_mismatch[AOM_MAX_LAYERS];
// Actual encoding bitrate per layer (cumulative across temporal layers).
double layer_encoding_bitrate[AOM_MAX_LAYERS];
// Average of the short-time encoder actual bitrate.
// TODO(marpan): Should we add these short-time stats for each layer?
double avg_st_encoding_bitrate;
// Variance of the short-time encoder actual bitrate.
double variance_st_encoding_bitrate;
// Window (number of frames) for computing short-timee encoding bitrate.
int window_size;
// Number of window measurements.
int window_count;
int layer_target_bitrate[AOM_MAX_LAYERS];
};
static const int REF_FRAMES = 8;
static const int INTER_REFS_PER_FRAME = 7;
// Reference frames used in this example encoder.
enum {
SVC_LAST_FRAME = 0,
SVC_LAST2_FRAME,
SVC_LAST3_FRAME,
SVC_GOLDEN_FRAME,
SVC_BWDREF_FRAME,
SVC_ALTREF2_FRAME,
SVC_ALTREF_FRAME
};
static int read_frame(struct AvxInputContext *input_ctx, aom_image_t *img) {
FILE *f = input_ctx->file;
y4m_input *y4m = &input_ctx->y4m;
int shortread = 0;
if (input_ctx->file_type == FILE_TYPE_Y4M) {
if (y4m_input_fetch_frame(y4m, f, img) < 1) return 0;
} else {
shortread = read_yuv_frame(input_ctx, img);
}
return !shortread;
}
static void close_input_file(struct AvxInputContext *input) {
fclose(input->file);
if (input->file_type == FILE_TYPE_Y4M) y4m_input_close(&input->y4m);
}
// Note: these rate control metrics assume only 1 key frame in the
// sequence (i.e., first frame only). So for temporal pattern# 7
// (which has key frame for every frame on base layer), the metrics
// computation will be off/wrong.
// TODO(marpan): Update these metrics to account for multiple key frames
// in the stream.
static void set_rate_control_metrics(struct RateControlMetrics *rc,
double framerate, int ss_number_layers,
int ts_number_layers) {
int ts_rate_decimator[AOM_MAX_TS_LAYERS] = { 1 };
ts_rate_decimator[0] = 1;
if (ts_number_layers == 2) {
ts_rate_decimator[0] = 2;
ts_rate_decimator[1] = 1;
}
if (ts_number_layers == 3) {
ts_rate_decimator[0] = 4;
ts_rate_decimator[1] = 2;
ts_rate_decimator[2] = 1;
}
// Set the layer (cumulative) framerate and the target layer (non-cumulative)
// per-frame-bandwidth, for the rate control encoding stats below.
for (int sl = 0; sl < ss_number_layers; ++sl) {
int i = sl * ts_number_layers;
rc->layer_framerate[0] = framerate / ts_rate_decimator[0];
rc->layer_pfb[i] =
1000.0 * rc->layer_target_bitrate[i] / rc->layer_framerate[0];
for (int tl = 0; tl < ts_number_layers; ++tl) {
i = sl * ts_number_layers + tl;
if (tl > 0) {
rc->layer_framerate[tl] = framerate / ts_rate_decimator[tl];
rc->layer_pfb[i] =
1000.0 *
(rc->layer_target_bitrate[i] - rc->layer_target_bitrate[i - 1]) /
(rc->layer_framerate[tl] - rc->layer_framerate[tl - 1]);
}
rc->layer_input_frames[tl] = 0;
rc->layer_enc_frames[tl] = 0;
rc->layer_encoding_bitrate[i] = 0.0;
rc->layer_avg_frame_size[i] = 0.0;
rc->layer_avg_rate_mismatch[i] = 0.0;
}
}
rc->window_count = 0;
rc->window_size = 15;
rc->avg_st_encoding_bitrate = 0.0;
rc->variance_st_encoding_bitrate = 0.0;
}
static void printout_rate_control_summary(struct RateControlMetrics *rc,
int frame_cnt, int ss_number_layers,
int ts_number_layers) {
int tot_num_frames = 0;
double perc_fluctuation = 0.0;
printf("Total number of processed frames: %d\n\n", frame_cnt - 1);
printf("Rate control layer stats for %d layer(s):\n\n", ts_number_layers);
for (int sl = 0; sl < ss_number_layers; ++sl) {
tot_num_frames = 0;
for (int tl = 0; tl < ts_number_layers; ++tl) {
int i = sl * ts_number_layers + tl;
const int num_dropped =
tl > 0 ? rc->layer_input_frames[tl] - rc->layer_enc_frames[tl]
: rc->layer_input_frames[tl] - rc->layer_enc_frames[tl] - 1;
tot_num_frames += rc->layer_input_frames[tl];
rc->layer_encoding_bitrate[i] = 0.001 * rc->layer_framerate[tl] *
rc->layer_encoding_bitrate[i] /
tot_num_frames;
rc->layer_avg_frame_size[i] =
rc->layer_avg_frame_size[i] / rc->layer_enc_frames[tl];
rc->layer_avg_rate_mismatch[i] =
100.0 * rc->layer_avg_rate_mismatch[i] / rc->layer_enc_frames[tl];
printf("For layer#: %d %d \n", sl, tl);
printf("Bitrate (target vs actual): %d %f\n", rc->layer_target_bitrate[i],
rc->layer_encoding_bitrate[i]);
printf("Average frame size (target vs actual): %f %f\n", rc->layer_pfb[i],
rc->layer_avg_frame_size[i]);
printf("Average rate_mismatch: %f\n", rc->layer_avg_rate_mismatch[i]);
printf(
"Number of input frames, encoded (non-key) frames, "
"and perc dropped frames: %d %d %f\n",
rc->layer_input_frames[tl], rc->layer_enc_frames[tl],
100.0 * num_dropped / rc->layer_input_frames[tl]);
printf("\n");
}
}
rc->avg_st_encoding_bitrate = rc->avg_st_encoding_bitrate / rc->window_count;
rc->variance_st_encoding_bitrate =
rc->variance_st_encoding_bitrate / rc->window_count -
(rc->avg_st_encoding_bitrate * rc->avg_st_encoding_bitrate);
perc_fluctuation = 100.0 * sqrt(rc->variance_st_encoding_bitrate) /
rc->avg_st_encoding_bitrate;
printf("Short-time stats, for window of %d frames:\n", rc->window_size);
printf("Average, rms-variance, and percent-fluct: %f %f %f\n",
rc->avg_st_encoding_bitrate, sqrt(rc->variance_st_encoding_bitrate),
perc_fluctuation);
if (frame_cnt - 1 != tot_num_frames)
die("Error: Number of input frames not equal to output!\n");
}
// Layer pattern configuration.
static void set_layer_pattern(
int layering_mode, int superframe_cnt, aom_svc_layer_id_t *layer_id,
aom_svc_ref_frame_config_t *ref_frame_config,
aom_svc_ref_frame_comp_pred_t *ref_frame_comp_pred, int *use_svc_control,
int spatial_layer_id, int is_key_frame, int ksvc_mode, int speed,
int *reference_updated, int test_roi_map) {
// Setting this flag to 1 enables simplex example of
// RPS (Reference Picture Selection) for 1 layer.
int use_rps_example = 0;
int i;
int enable_longterm_temporal_ref = 1;
int shift = (layering_mode == 8) ? 2 : 0;
int simulcast_mode = (layering_mode == 11);
*use_svc_control = 1;
layer_id->spatial_layer_id = spatial_layer_id;
int lag_index = 0;
int base_count = superframe_cnt >> 2;
ref_frame_comp_pred->use_comp_pred[0] = 0; // GOLDEN_LAST
ref_frame_comp_pred->use_comp_pred[1] = 0; // LAST2_LAST
ref_frame_comp_pred->use_comp_pred[2] = 0; // ALTREF_LAST
// Set the reference map buffer idx for the 7 references:
// LAST_FRAME (0), LAST2_FRAME(1), LAST3_FRAME(2), GOLDEN_FRAME(3),
// BWDREF_FRAME(4), ALTREF2_FRAME(5), ALTREF_FRAME(6).
for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->ref_idx[i] = i;
for (i = 0; i < INTER_REFS_PER_FRAME; i++) ref_frame_config->reference[i] = 0;
for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
if (ksvc_mode) {
// Same pattern as case 9, but the reference strucutre will be constrained
// below.
layering_mode = 9;
}
switch (layering_mode) {
case 0:
if (use_rps_example == 0) {
// 1-layer: update LAST on every frame, reference LAST.
layer_id->temporal_layer_id = 0;
layer_id->spatial_layer_id = 0;
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
// Add additional reference (GOLDEN) if test_roi_map is set,
// to test reference frame feature on segment.
if (test_roi_map) ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
} else {
// Pattern of 2 references (ALTREF and GOLDEN) trailing
// LAST by 4 and 8 frames, with some switching logic to
// sometimes only predict from the longer-term reference
//(golden here). This is simple example to test RPS
// (reference picture selection).
int last_idx = 0;
int last_idx_refresh = 0;
int gld_idx = 0;
int alt_ref_idx = 0;
int lag_alt = 4;
int lag_gld = 8;
layer_id->temporal_layer_id = 0;
layer_id->spatial_layer_id = 0;
int sh = 8; // slots 0 - 7.
// Moving index slot for last: 0 - (sh - 1)
if (superframe_cnt > 1) last_idx = (superframe_cnt - 1) % sh;
// Moving index for refresh of last: one ahead for next frame.
last_idx_refresh = superframe_cnt % sh;
// Moving index for gld_ref, lag behind current by lag_gld
if (superframe_cnt > lag_gld) gld_idx = (superframe_cnt - lag_gld) % sh;
// Moving index for alt_ref, lag behind LAST by lag_alt frames.
if (superframe_cnt > lag_alt)
alt_ref_idx = (superframe_cnt - lag_alt) % sh;
// Set the ref_idx.
// Default all references to slot for last.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = last_idx;
// Set the ref_idx for the relevant references.
ref_frame_config->ref_idx[SVC_LAST_FRAME] = last_idx;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = last_idx_refresh;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = gld_idx;
ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = alt_ref_idx;
// Refresh this slot, which will become LAST on next frame.
ref_frame_config->refresh[last_idx_refresh] = 1;
// Reference LAST, ALTREF, and GOLDEN
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
// Switch to only GOLDEN every 300 frames.
if (superframe_cnt % 200 == 0 && superframe_cnt > 0) {
ref_frame_config->reference[SVC_LAST_FRAME] = 0;
ref_frame_config->reference[SVC_ALTREF_FRAME] = 0;
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
// Test if the long-term is LAST instead, this is just a renaming
// but its tests if encoder behaves the same, whether its
// LAST or GOLDEN.
if (superframe_cnt % 400 == 0 && superframe_cnt > 0) {
ref_frame_config->ref_idx[SVC_LAST_FRAME] = gld_idx;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
ref_frame_config->reference[SVC_ALTREF_FRAME] = 0;
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 0;
}
}
}
break;
case 1:
// 2-temporal layer.
// 1 3 5
// 0 2 4
// Keep golden fixed at slot 3.
base_count = superframe_cnt >> 1;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
// Cyclically refresh slots 5, 6, 7, for lag alt ref.
lag_index = 5;
if (base_count > 0) {
lag_index = 5 + (base_count % 3);
if (superframe_cnt % 2 != 0) lag_index = 5 + ((base_count + 1) % 3);
}
// Set the altref slot to lag_index.
ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
if (superframe_cnt % 2 == 0) {
layer_id->temporal_layer_id = 0;
// Update LAST on layer 0, reference LAST.
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
// Refresh lag_index slot, needed for lagging golen.
ref_frame_config->refresh[lag_index] = 1;
// Refresh GOLDEN every x base layer frames.
if (base_count % 32 == 0) ref_frame_config->refresh[3] = 1;
} else {
layer_id->temporal_layer_id = 1;
// No updates on layer 1, reference LAST (TL0).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
}
// Always reference golden and altref on TL0.
if (layer_id->temporal_layer_id == 0) {
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
}
break;
case 2:
// 3-temporal layer:
// 1 3 5 7
// 2 6
// 0 4 8
if (superframe_cnt % 4 == 0) {
// Base layer.
layer_id->temporal_layer_id = 0;
// Update LAST on layer 0, reference LAST.
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 1) % 4 == 0) {
layer_id->temporal_layer_id = 2;
// First top layer: no updates, only reference LAST (TL0).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 2) % 4 == 0) {
layer_id->temporal_layer_id = 1;
// Middle layer (TL1): update LAST2, only reference LAST (TL0).
ref_frame_config->refresh[1] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 3) % 4 == 0) {
layer_id->temporal_layer_id = 2;
// Second top layer: no updates, only reference LAST.
// Set buffer idx for LAST to slot 1, since that was the slot
// updated in previous frame. So LAST is TL1 frame.
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
}
break;
case 3:
// 3 TL, same as above, except allow for predicting
// off 2 more references (GOLDEN and ALTREF), with
// GOLDEN updated periodically, and ALTREF lagging from
// LAST from ~4 frames. Both GOLDEN and ALTREF
// can only be updated on base temporal layer.
// Keep golden fixed at slot 3.
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
// Cyclically refresh slots 5, 6, 7, for lag altref.
lag_index = 5;
if (base_count > 0) {
lag_index = 5 + (base_count % 3);
if (superframe_cnt % 4 != 0) lag_index = 5 + ((base_count + 1) % 3);
}
// Set the altref slot to lag_index.
ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = lag_index;
if (superframe_cnt % 4 == 0) {
// Base layer.
layer_id->temporal_layer_id = 0;
// Update LAST on layer 0, reference LAST.
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
// Refresh GOLDEN every x ~10 base layer frames.
if (base_count % 10 == 0) ref_frame_config->refresh[3] = 1;
// Refresh lag_index slot, needed for lagging altref.
ref_frame_config->refresh[lag_index] = 1;
} else if ((superframe_cnt - 1) % 4 == 0) {
layer_id->temporal_layer_id = 2;
// First top layer: no updates, only reference LAST (TL0).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 2) % 4 == 0) {
layer_id->temporal_layer_id = 1;
// Middle layer (TL1): update LAST2, only reference LAST (TL0).
ref_frame_config->refresh[1] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 3) % 4 == 0) {
layer_id->temporal_layer_id = 2;
// Second top layer: no updates, only reference LAST.
// Set buffer idx for LAST to slot 1, since that was the slot
// updated in previous frame. So LAST is TL1 frame.
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 0;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
}
// Every frame can reference GOLDEN AND ALTREF.
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
// Allow for compound prediction for LAST-ALTREF and LAST-GOLDEN.
if (speed >= 7) {
ref_frame_comp_pred->use_comp_pred[2] = 1;
ref_frame_comp_pred->use_comp_pred[0] = 1;
}
break;
case 4:
// 3-temporal layer: but middle layer updates GF, so 2nd TL2 will
// only reference GF (not LAST). Other frames only reference LAST.
// 1 3 5 7
// 2 6
// 0 4 8
if (superframe_cnt % 4 == 0) {
// Base layer.
layer_id->temporal_layer_id = 0;
// Update LAST on layer 0, only reference LAST.
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 1) % 4 == 0) {
layer_id->temporal_layer_id = 2;
// First top layer: no updates, only reference LAST (TL0).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 2) % 4 == 0) {
layer_id->temporal_layer_id = 1;
// Middle layer (TL1): update GF, only reference LAST (TL0).
ref_frame_config->refresh[3] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if ((superframe_cnt - 3) % 4 == 0) {
layer_id->temporal_layer_id = 2;
// Second top layer: no updates, only reference GF.
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
}
break;
case 5:
/*
// 2 spatial layers, 1 temporal, without temporal prediction on SL1.
layer_id->temporal_layer_id = 0;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST, update LAST.
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST, which is SL0, and no refresh.
ref_frame_config->refresh[0] = 0;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
}
break;
*/
// 2 spatial layers, 1 temporal.
layer_id->temporal_layer_id = 0;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST, update LAST.
ref_frame_config->refresh[0] = 1;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 2;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
// and GOLDEN to slot 0. Update slot 1 (LAST).
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 0;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 2;
ref_frame_config->refresh[1] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
}
break;
case 6:
// 3 spatial layers, 1 temporal.
// Note for this case, we set the buffer idx for all references to be
// either LAST or GOLDEN, which are always valid references, since decoder
// will check if any of the 7 references is valid scale in
// valid_ref_frame_size().
layer_id->temporal_layer_id = 0;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST, update LAST. Set all buffer_idx to 0.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->refresh[0] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1
// and GOLDEN (and all other refs) to slot 0.
// Update slot 1 (LAST).
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->refresh[1] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
} else if (layer_id->spatial_layer_id == 2) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2
// and GOLDEN (and all other refs) to slot 1.
// Update slot 2 (LAST).
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 1;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
ref_frame_config->refresh[2] = 1;
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
// For 3 spatial layer case: allow for top spatial layer to use
// additional temporal reference. Update every 10 frames.
if (enable_longterm_temporal_ref) {
ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
if (base_count % 10 == 0)
ref_frame_config->refresh[REF_FRAMES - 1] = 1;
}
}
break;
case 7:
// 2 spatial and 3 temporal layer.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
if (superframe_cnt % 4 == 0) {
// Base temporal layer
layer_id->temporal_layer_id = 0;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST, update LAST
// Set all buffer_idx to 0
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->refresh[0] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->refresh[1] = 1;
}
} else if ((superframe_cnt - 1) % 4 == 0) {
// First top temporal enhancement layer.
layer_id->temporal_layer_id = 2;
if (layer_id->spatial_layer_id == 0) {
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
ref_frame_config->refresh[3] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
// GOLDEN (and all other refs) to slot 3.
// No update.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 3;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
}
} else if ((superframe_cnt - 2) % 4 == 0) {
// Middle temporal enhancement layer.
layer_id->temporal_layer_id = 1;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST.
// Set all buffer_idx to 0.
// Set GOLDEN to slot 5 and update slot 5.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
ref_frame_config->refresh[5 - shift] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
// GOLDEN (and all other refs) to slot 5.
// Set LAST3 to slot 6 and update slot 6.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 5 - shift;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
ref_frame_config->refresh[6 - shift] = 1;
}
} else if ((superframe_cnt - 3) % 4 == 0) {
// Second top temporal enhancement layer.
layer_id->temporal_layer_id = 2;
if (layer_id->spatial_layer_id == 0) {
// Set LAST to slot 5 and reference LAST.
// Set GOLDEN to slot 3 and update slot 3.
// Set all other buffer_idx to 0.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
ref_frame_config->refresh[3] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
// GOLDEN to slot 3. No update.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
}
}
break;
case 8:
// 3 spatial and 3 temporal layer.
// Same as case 9 but overalap in the buffer slot updates.
// (shift = 2). The slots 3 and 4 updated by first TL2 are
// reused for update in TL1 superframe.
// Note for this case, frame order hint must be disabled for
// lower resolutios (operating points > 0) to be decoedable.
case 9:
// 3 spatial and 3 temporal layer.
// No overlap in buffer updates between TL2 and TL1.
// TL2 updates slot 3 and 4, TL1 updates 5, 6, 7.
// Set the references via the svc_ref_frame_config control.
// Always reference LAST.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
if (superframe_cnt % 4 == 0) {
// Base temporal layer.
layer_id->temporal_layer_id = 0;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST, update LAST.
// Set all buffer_idx to 0.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->refresh[0] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
// GOLDEN (and all other refs) to slot 0.
// Update slot 1 (LAST).
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->refresh[1] = 1;
} else if (layer_id->spatial_layer_id == 2) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
// GOLDEN (and all other refs) to slot 1.
// Update slot 2 (LAST).
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 1;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
ref_frame_config->refresh[2] = 1;
}
} else if ((superframe_cnt - 1) % 4 == 0) {
// First top temporal enhancement layer.
layer_id->temporal_layer_id = 2;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST (slot 0).
// Set GOLDEN to slot 3 and update slot 3.
// Set all other buffer_idx to slot 0.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
ref_frame_config->refresh[3] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
// GOLDEN (and all other refs) to slot 3.
// Set LAST2 to slot 4 and Update slot 4.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 3;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
ref_frame_config->refresh[4] = 1;
} else if (layer_id->spatial_layer_id == 2) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
// GOLDEN (and all other refs) to slot 4.
// No update.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 4;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
}
} else if ((superframe_cnt - 2) % 4 == 0) {
// Middle temporal enhancement layer.
layer_id->temporal_layer_id = 1;
if (layer_id->spatial_layer_id == 0) {
// Reference LAST.
// Set all buffer_idx to 0.
// Set GOLDEN to slot 5 and update slot 5.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5 - shift;
ref_frame_config->refresh[5 - shift] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 1,
// GOLDEN (and all other refs) to slot 5.
// Set LAST3 to slot 6 and update slot 6.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 5 - shift;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 6 - shift;
ref_frame_config->refresh[6 - shift] = 1;
} else if (layer_id->spatial_layer_id == 2) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 2,
// GOLDEN (and all other refs) to slot 6.
// Set LAST3 to slot 7 and update slot 7.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 6 - shift;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
ref_frame_config->ref_idx[SVC_LAST3_FRAME] = 7 - shift;
ref_frame_config->refresh[7 - shift] = 1;
}
} else if ((superframe_cnt - 3) % 4 == 0) {
// Second top temporal enhancement layer.
layer_id->temporal_layer_id = 2;
if (layer_id->spatial_layer_id == 0) {
// Set LAST to slot 5 and reference LAST.
// Set GOLDEN to slot 3 and update slot 3.
// Set all other buffer_idx to 0.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5 - shift;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
ref_frame_config->refresh[3] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 6,
// GOLDEN to slot 3. Set LAST2 to slot 4 and update slot 4.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 6 - shift;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
ref_frame_config->ref_idx[SVC_LAST2_FRAME] = 4;
ref_frame_config->refresh[4] = 1;
} else if (layer_id->spatial_layer_id == 2) {
// Reference LAST and GOLDEN. Set buffer_idx for LAST to slot 7,
// GOLDEN to slot 4. No update.
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 7 - shift;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 4;
}
}
break;
case 11:
// Simulcast mode for 3 spatial and 3 temporal layers.
// No inter-layer predicton, only prediction is temporal and single
// reference (LAST).
// No overlap in buffer slots between spatial layers. So for example,
// SL0 only uses slots 0 and 1.
// SL1 only uses slots 2 and 3.
// SL2 only uses slots 4 and 5.
// All 7 references for each inter-frame must only access buffer slots
// for that spatial layer.
// On key (super)frames: SL1 and SL2 must have no references set
// and must refresh all the slots for that layer only (so 2 and 3
// for SL1, 4 and 5 for SL2). The base SL0 will be labelled internally
// as a Key frame (refresh all slots). SL1/SL2 will be labelled
// internally as Intra-only frames that allow that stream to be decoded.
// These conditions will allow for each spatial stream to be
// independently decodeable.
// Initialize all references to 0 (don't use reference).
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->reference[i] = 0;
// Initialize as no refresh/update for all slots.
for (i = 0; i < REF_FRAMES; i++) ref_frame_config->refresh[i] = 0;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
if (is_key_frame) {
if (layer_id->spatial_layer_id == 0) {
// Assign LAST/GOLDEN to slot 0/1.
// Refesh slots 0 and 1 for SL0.
// SL0: this will get set to KEY frame internally.
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 1;
ref_frame_config->refresh[0] = 1;
ref_frame_config->refresh[1] = 1;
} else if (layer_id->spatial_layer_id == 1) {
// Assign LAST/GOLDEN to slot 2/3.
// Refesh slots 2 and 3 for SL1.
// This will get set to Intra-only frame internally.
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 3;
ref_frame_config->refresh[2] = 1;
ref_frame_config->refresh[3] = 1;
} else if (layer_id->spatial_layer_id == 2) {
// Assign LAST/GOLDEN to slot 4/5.
// Refresh slots 4 and 5 for SL2.
// This will get set to Intra-only frame internally.
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
ref_frame_config->ref_idx[SVC_GOLDEN_FRAME] = 5;
ref_frame_config->refresh[4] = 1;
ref_frame_config->refresh[5] = 1;
}
} else if (superframe_cnt % 4 == 0) {
// Base temporal layer: TL0
layer_id->temporal_layer_id = 0;
if (layer_id->spatial_layer_id == 0) { // SL0
// Reference LAST. Assign all references to either slot
// 0 or 1. Here we assign LAST to slot 0, all others to 1.
// Update slot 0 (LAST).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 1;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
ref_frame_config->refresh[0] = 1;
} else if (layer_id->spatial_layer_id == 1) { // SL1
// Reference LAST. Assign all references to either slot
// 2 or 3. Here we assign LAST to slot 2, all others to 3.
// Update slot 2 (LAST).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 3;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
ref_frame_config->refresh[2] = 1;
} else if (layer_id->spatial_layer_id == 2) { // SL2
// Reference LAST. Assign all references to either slot
// 4 or 5. Here we assign LAST to slot 4, all others to 5.
// Update slot 4 (LAST).
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 5;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
ref_frame_config->refresh[4] = 1;
}
} else if ((superframe_cnt - 1) % 4 == 0) {
// First top temporal enhancement layer: TL2
layer_id->temporal_layer_id = 2;
if (layer_id->spatial_layer_id == 0) { // SL0
// Reference LAST (slot 0). Assign other references to slot 1.
// No update/refresh on any slots.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 1;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
} else if (layer_id->spatial_layer_id == 1) { // SL1
// Reference LAST (slot 2). Assign other references to slot 3.
// No update/refresh on any slots.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 3;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
} else if (layer_id->spatial_layer_id == 2) { // SL2
// Reference LAST (slot 4). Assign other references to slot 4.
// No update/refresh on any slots.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 5;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
}
} else if ((superframe_cnt - 2) % 4 == 0) {
// Middle temporal enhancement layer: TL1
layer_id->temporal_layer_id = 1;
if (layer_id->spatial_layer_id == 0) { // SL0
// Reference LAST (slot 0).
// Set GOLDEN to slot 1 and update slot 1.
// This will be used as reference for next TL2.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 1;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 0;
ref_frame_config->refresh[1] = 1;
} else if (layer_id->spatial_layer_id == 1) { // SL1
// Reference LAST (slot 2).
// Set GOLDEN to slot 3 and update slot 3.
// This will be used as reference for next TL2.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 3;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 2;
ref_frame_config->refresh[3] = 1;
} else if (layer_id->spatial_layer_id == 2) { // SL2
// Reference LAST (slot 4).
// Set GOLDEN to slot 5 and update slot 5.
// This will be used as reference for next TL2.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 5;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 4;
ref_frame_config->refresh[5] = 1;
}
} else if ((superframe_cnt - 3) % 4 == 0) {
// Second top temporal enhancement layer: TL2
layer_id->temporal_layer_id = 2;
if (layer_id->spatial_layer_id == 0) { // SL0
// Reference LAST (slot 1). Assign other references to slot 0.
// No update/refresh on any slots.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 0;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 1;
} else if (layer_id->spatial_layer_id == 1) { // SL1
// Reference LAST (slot 3). Assign other references to slot 2.
// No update/refresh on any slots.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 2;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 3;
} else if (layer_id->spatial_layer_id == 2) { // SL2
// Reference LAST (slot 5). Assign other references to slot 4.
// No update/refresh on any slots.
ref_frame_config->reference[SVC_LAST_FRAME] = 1;
for (i = 0; i < INTER_REFS_PER_FRAME; i++)
ref_frame_config->ref_idx[i] = 4;
ref_frame_config->ref_idx[SVC_LAST_FRAME] = 5;
}
}
if (!simulcast_mode && layer_id->spatial_layer_id > 0) {
// Always reference GOLDEN (inter-layer prediction).
ref_frame_config->reference[SVC_GOLDEN_FRAME] = 1;
if (ksvc_mode) {
// KSVC: only keep the inter-layer reference (GOLDEN) for
// superframes whose base is key.
if (!is_key_frame) ref_frame_config->reference[SVC_GOLDEN_FRAME] = 0;
}
if (is_key_frame && layer_id->spatial_layer_id > 1) {
// On superframes whose base is key: remove LAST to avoid prediction
// off layer two levels below.
ref_frame_config->reference[SVC_LAST_FRAME] = 0;
}
}
// For 3 spatial layer case 8 (where there is free buffer slot):
// allow for top spatial layer to use additional temporal reference.
// Additional reference is only updated on base temporal layer, every
// 10 TL0 frames here.
if (!simulcast_mode && enable_longterm_temporal_ref &&
layer_id->spatial_layer_id == 2 && layering_mode == 8) {
ref_frame_config->ref_idx[SVC_ALTREF_FRAME] = REF_FRAMES - 1;
if (!is_key_frame) ref_frame_config->reference[SVC_ALTREF_FRAME] = 1;
if (base_count % 10 == 0 && layer_id->temporal_layer_id == 0)
ref_frame_config->refresh[REF_FRAMES - 1] = 1;
}
break;
default: assert(0); die("Error: Unsupported temporal layering mode!\n");
}
for (i = 0; i < REF_FRAMES; i++) {
if (ref_frame_config->refresh[i] == 1) {
*reference_updated = 1;
break;
}
}
}
static void write_literal(struct aom_write_bit_buffer *wb, uint32_t data,
uint8_t bits, uint32_t offset = 0) {
if (bits > 32) {
die("Invalid bits value %d > 32\n", bits);
}
const uint32_t max = static_cast<uint32_t>(((uint64_t)1 << bits) - 1);
if (data < offset || (data - offset) > max) {
die("Invalid data, value %u out of range [%u, %" PRIu64 "]\n", data, offset,
(uint64_t)max + offset);
}
aom_wb_write_unsigned_literal(wb, data - offset, bits);
}
static void write_depth_representation_element(
struct aom_write_bit_buffer *buffer,
const std::pair<libaom_examples::DepthRepresentationElement, bool>
&element) {
if (!element.second) {
return;
}
write_literal(buffer, element.first.sign_flag, 1);
write_literal(buffer, element.first.exponent, 7);
if (element.first.mantissa_len == 0 || element.first.mantissa_len > 32) {
die("Invalid mantissan_len %d\n", element.first.mantissa_len);
}
write_literal(buffer, element.first.mantissa_len - 1, 5);
write_literal(buffer, element.first.mantissa, element.first.mantissa_len);
}
static void write_color_properties(
struct aom_write_bit_buffer *buffer,
const std::pair<libaom_examples::ColorProperties, bool> &color_properties) {
write_literal(buffer, color_properties.second, 1);
if (color_properties.second) {
write_literal(buffer, color_properties.first.color_range, 1);
write_literal(buffer, color_properties.first.color_primaries, 8);
write_literal(buffer, color_properties.first.transfer_characteristics, 8);
write_literal(buffer, color_properties.first.matrix_coefficients, 8);
} else {
write_literal(buffer, 0, 1); // reserved_1bit
}
}
static void add_multilayer_metadata(
aom_image_t *frame, const libaom_examples::MultilayerMetadata &multilayer) {
// Large enough buffer for the multilayer metadata.
// Each layer's metadata is less than 100 bytes and there are at most 4
// layers.
std::vector<uint8_t> data(1024);
struct aom_write_bit_buffer buffer = { data.data(), 0 };
write_literal(&buffer, multilayer.use_case, 6);
if (multilayer.layers.empty()) {
die("Invalid multilayer metadata, no layers found\n");
} else if (multilayer.layers.size() > MAX_NUM_SPATIAL_LAYERS) {
die("Invalid multilayer metadata, too many layers (max is %d)\n",
MAX_NUM_SPATIAL_LAYERS);
}
write_literal(&buffer, (int)multilayer.layers.size() - 1, 2);
assert(buffer.bit_offset % 8 == 0);
for (size_t i = 0; i < multilayer.layers.size(); ++i) {
const libaom_examples::LayerMetadata &layer = multilayer.layers[i];
// Alpha info with segmentation with labels can be up to about 66k bytes,
// which requires 3 bytes to encode in leb128.
const int bytes_reserved_for_size = 3;
// Placeholder for layer_metadata_size which will be written later.
write_literal(&buffer, 0, bytes_reserved_for_size * 8);
const uint32_t metadata_start = buffer.bit_offset;
write_literal(&buffer, (int)i, 2); // ml_spatial_id
write_literal(&buffer, layer.layer_type, 5);
write_literal(&buffer, layer.luma_plane_only_flag, 1);
write_literal(&buffer, layer.layer_view_type, 3);
write_literal(&buffer, layer.group_id, 2);
write_literal(&buffer, layer.layer_dependency_idc, 3);
write_literal(&buffer, layer.layer_metadata_scope, 2);
write_literal(&buffer, 0, 4); // ml_reserved_4bits
if (i > 0) {
write_color_properties(&buffer, layer.layer_color_description);
} else {
write_literal(&buffer, 0, 2); // ml_reserved_2bits
}
assert(buffer.bit_offset % 8 == 0);
if (layer.layer_type == libaom_examples::MULTILAYER_LAYER_TYPE_ALPHA &&
layer.layer_metadata_scope >= libaom_examples::SCOPE_GLOBAL) {
const libaom_examples::AlphaInformation &alpha_info =
layer.global_alpha_info;
write_literal(&buffer, alpha_info.alpha_use_idc, 2);
write_literal(&buffer, alpha_info.alpha_simple_flag, 1);
if (!alpha_info.alpha_simple_flag) {
write_literal(&buffer, alpha_info.alpha_bit_depth, 3, /*offset=*/8);
write_literal(&buffer, alpha_info.alpha_clip_idc, 2);
write_literal(&buffer, alpha_info.alpha_incr_flag, 1);
write_literal(&buffer, alpha_info.alpha_transparent_value,
alpha_info.alpha_bit_depth + 1);
write_literal(&buffer, alpha_info.alpha_opaque_value,
alpha_info.alpha_bit_depth + 1);
if (buffer.bit_offset % 8 != 0) {
// ai_byte_alignment_bits
write_literal(&buffer, 0, 8 - (buffer.bit_offset % 8));
}
assert(buffer.bit_offset % 8 == 0);
write_literal(&buffer, 0, 6); // ai_reserved_6bits
write_color_properties(&buffer, alpha_info.alpha_color_description);
} else {
write_literal(&buffer, 0, 5); // ai_reserved_5bits
}
assert(buffer.bit_offset % 8 == 0);
} else if (layer.layer_type ==
libaom_examples::MULTILAYER_LAYER_TYPE_DEPTH &&
layer.layer_metadata_scope >= libaom_examples::SCOPE_GLOBAL) {
const libaom_examples::DepthInformation &depth_info =
layer.global_depth_info;
write_literal(&buffer, depth_info.z_near.second, 1);
write_literal(&buffer, depth_info.z_far.second, 1);
write_literal(&buffer, depth_info.d_min.second, 1);
write_literal(&buffer, depth_info.d_max.second, 1);
write_literal(&buffer, depth_info.depth_representation_type, 4);
if (depth_info.d_min.second || depth_info.d_max.second) {
write_literal(&buffer, depth_info.disparity_ref_view_id, 2);
}
write_depth_representation_element(&buffer, depth_info.z_near);
write_depth_representation_element(&buffer, depth_info.z_far);
write_depth_representation_element(&buffer, depth_info.d_min);
write_depth_representation_element(&buffer, depth_info.d_max);
if (buffer.bit_offset % 8 != 0) {
write_literal(&buffer, 0, 8 - (buffer.bit_offset % 8));
}
assert(buffer.bit_offset % 8 == 0);
}
assert(buffer.bit_offset % 8 == 0);
const int metadata_size_bytes = (buffer.bit_offset - metadata_start) / 8;
const uint8_t size_pos = metadata_start / 8 - bytes_reserved_for_size;
size_t coded_size;
if (aom_uleb_encode_fixed_size(metadata_size_bytes, bytes_reserved_for_size,
bytes_reserved_for_size,
&buffer.bit_buffer[size_pos], &coded_size)) {
// Need to increase bytes_reserved_for_size in the code above.
die("Error: Failed to write metadata size\n");
}
}
assert(buffer.bit_offset % 8 == 0);
if (aom_img_add_metadata(frame, 33 /*METADATA_TYPE_MULTILAYER*/,
buffer.bit_buffer, buffer.bit_offset / 8,
AOM_MIF_KEY_FRAME)) {
die("Error: Failed to add metadata\n");
}
}
#if CONFIG_AV1_DECODER
// Returns whether there is a mismatch between the encoder's new frame and the
// decoder's new frame.
static int test_decode(aom_codec_ctx_t *encoder, aom_codec_ctx_t *decoder,
const int frames_out) {
aom_image_t enc_img, dec_img;
int mismatch = 0;
/* Get the internal new frame */
AOM_CODEC_CONTROL_TYPECHECKED(encoder, AV1_GET_NEW_FRAME_IMAGE, &enc_img);
AOM_CODEC_CONTROL_TYPECHECKED(decoder, AV1_GET_NEW_FRAME_IMAGE, &dec_img);
#if CONFIG_AV1_HIGHBITDEPTH
if ((enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) !=
(dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH)) {
if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
aom_image_t enc_hbd_img;
aom_img_alloc(
&enc_hbd_img,
static_cast<aom_img_fmt_t>(enc_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH),
enc_img.d_w, enc_img.d_h, 16);
aom_img_truncate_16_to_8(&enc_hbd_img, &enc_img);
enc_img = enc_hbd_img;
}
if (dec_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
aom_image_t dec_hbd_img;
aom_img_alloc(
&dec_hbd_img,
static_cast<aom_img_fmt_t>(dec_img.fmt - AOM_IMG_FMT_HIGHBITDEPTH),
dec_img.d_w, dec_img.d_h, 16);
aom_img_truncate_16_to_8(&dec_hbd_img, &dec_img);
dec_img = dec_hbd_img;
}
}
#endif
if (!aom_compare_img(&enc_img, &dec_img)) {
int y[4], u[4], v[4];
#if CONFIG_AV1_HIGHBITDEPTH
if (enc_img.fmt & AOM_IMG_FMT_HIGHBITDEPTH) {
aom_find_mismatch_high(&enc_img, &dec_img, y, u, v);
} else {
aom_find_mismatch(&enc_img, &dec_img, y, u, v);
}
#else
aom_find_mismatch(&enc_img, &dec_img, y, u, v);
#endif
fprintf(stderr,
"Encode/decode mismatch on frame %d at"
" Y[%d, %d] {%d/%d},"
" U[%d, %d] {%d/%d},"
" V[%d, %d] {%d/%d}\n",
frames_out, y[0], y[1], y[2], y[3], u[0], u[1], u[2], u[3], v[0],
v[1], v[2], v[3]);
mismatch = 1;
}
aom_img_free(&enc_img);
aom_img_free(&dec_img);
return mismatch;
}
#endif // CONFIG_AV1_DECODER
struct psnr_stats {
// The second element of these arrays is reserved for high bitdepth.
uint64_t psnr_sse_total[2];
uint64_t psnr_samples_total[2];
double psnr_totals[2][4];
int psnr_count[2];
};
static void show_psnr(struct psnr_stats *psnr_stream, double peak) {
double ovpsnr;
if (!psnr_stream->psnr_count[0]) return;
fprintf(stderr, "\nPSNR (Overall/Avg/Y/U/V)");
ovpsnr = sse_to_psnr((double)psnr_stream->psnr_samples_total[0], peak,
(double)psnr_stream->psnr_sse_total[0]);
fprintf(stderr, " %.3f", ovpsnr);
for (int i = 0; i < 4; i++) {
fprintf(stderr, " %.3f",
psnr_stream->psnr_totals[0][i] / psnr_stream->psnr_count[0]);
}
fprintf(stderr, "\n");
}
static aom::AV1RateControlRtcConfig create_rtc_rc_config(
const aom_codec_enc_cfg_t &cfg, const AppInput &app_input) {
aom::AV1RateControlRtcConfig rc_cfg;
rc_cfg.width = cfg.g_w;
rc_cfg.height = cfg.g_h;
rc_cfg.max_quantizer = cfg.rc_max_quantizer;
rc_cfg.min_quantizer = cfg.rc_min_quantizer;
rc_cfg.target_bandwidth = cfg.rc_target_bitrate;
rc_cfg.buf_initial_sz = cfg.rc_buf_initial_sz;
rc_cfg.buf_optimal_sz = cfg.rc_buf_optimal_sz;
rc_cfg.buf_sz = cfg.rc_buf_sz;
rc_cfg.overshoot_pct = cfg.rc_overshoot_pct;
rc_cfg.undershoot_pct = cfg.rc_undershoot_pct;
// This is hardcoded as AOME_SET_MAX_INTRA_BITRATE_PCT
rc_cfg.max_intra_bitrate_pct = 300;
rc_cfg.framerate = cfg.g_timebase.den;
// TODO(jianj): Add suppor for SVC.
rc_cfg.ss_number_layers = 1;
rc_cfg.ts_number_layers = 1;
rc_cfg.scaling_factor_num[0] = 1;
rc_cfg.scaling_factor_den[0] = 1;
rc_cfg.layer_target_bitrate[0] = static_cast<int>(rc_cfg.target_bandwidth);
rc_cfg.max_quantizers[0] = rc_cfg.max_quantizer;
rc_cfg.min_quantizers[0] = rc_cfg.min_quantizer;
rc_cfg.aq_mode = app_input.aq_mode;
return rc_cfg;
}
static int qindex_to_quantizer(int qindex) {
// Table that converts 0-63 range Q values passed in outside to the 0-255
// range Qindex used internally.
static const int quantizer_to_qindex[] = {
0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48,
52, 56, 60, 64, 68, 72, 76, 80, 84, 88, 92, 96, 100,
104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 144, 148, 152,
156, 160, 164, 168, 172, 176, 180, 184, 188, 192, 196, 200, 204,
208, 212, 216, 220, 224, 228, 232, 236, 240, 244, 249, 255,
};
for (int quantizer = 0; quantizer < 64; ++quantizer)
if (quantizer_to_qindex[quantizer] >= qindex) return quantizer;
return 63;
}
static void set_active_map(const aom_codec_enc_cfg_t *cfg,
aom_codec_ctx_t *codec, int frame_cnt) {
aom_active_map_t map = { 0, 0, 0 };
map.rows = (cfg->g_h + 15) / 16;
map.cols = (cfg->g_w + 15) / 16;
map.active_map = (uint8_t *)malloc(map.rows * map.cols);
if (!map.active_map) die("Failed to allocate active map");
// Example map for testing.
for (unsigned int i = 0; i < map.rows; ++i) {
for (unsigned int j = 0; j < map.cols; ++j) {
int index = map.cols * i + j;
map.active_map[index] = 1;
if (frame_cnt < 300) {
if (i < map.rows / 2 && j < map.cols / 2) map.active_map[index] = 0;
} else if (frame_cnt >= 300) {
if (i < map.rows / 2 && j >= map.cols / 2) map.active_map[index] = 0;
}
}
}
if (aom_codec_control(codec, AOME_SET_ACTIVEMAP, &map))
die_codec(codec, "Failed to set active map");
free(map.active_map);
}
static void set_roi_map(const aom_codec_enc_cfg_t *cfg, aom_codec_ctx_t *codec,
int roi_feature) {
aom_roi_map_t roi = aom_roi_map_t();
const int block_size = 4;
roi.rows = (cfg->g_h + block_size - 1) / block_size;
roi.cols = (cfg->g_w + block_size - 1) / block_size;
memset(&roi.skip, 0, sizeof(roi.skip));
memset(&roi.delta_q, 0, sizeof(roi.delta_q));
memset(&roi.delta_lf, 0, sizeof(roi.delta_lf));
memset(roi.ref_frame, -1, sizeof(roi.ref_frame));
// Set ROI map to be 1 (segment #1) in middle square of image,
// 0 elsewhere.
roi.enabled = 1;
roi.roi_map = (uint8_t *)calloc(roi.rows * roi.cols, sizeof(*roi.roi_map));
for (unsigned int i = 0; i < roi.rows; ++i) {
for (unsigned int j = 0; j < roi.cols; ++j) {
const int idx = i * roi.cols + j;
if (i > roi.rows / 4 && i < (3 * roi.rows) / 4 && j > roi.cols / 4 &&
j < (3 * roi.cols) / 4)
roi.roi_map[idx] = 1;
else
roi.roi_map[idx] = 0;
}
}
// Set the ROI feature, on segment #1.
if (roi_feature == kSkip)
roi.skip[1] = 1;
else if (roi_feature == kDeltaQ)
roi.delta_q[1] = -40;
else if (roi_feature == kDeltaLF)
roi.delta_lf[1] = 40;
else if (roi_feature == kReference)
roi.ref_frame[1] = 4; // GOLDEN_FRAME
if (aom_codec_control(codec, AOME_SET_ROI_MAP, &roi))
die_codec(codec, "Failed to set roi map");
free(roi.roi_map);
}
int main(int argc, const char **argv) {
AppInput app_input;
AvxVideoWriter *outfile[AOM_MAX_LAYERS] = { NULL };
FILE *obu_files[AOM_MAX_LAYERS] = { NULL };
AvxVideoWriter *total_layer_file = NULL;
FILE *total_layer_obu_file = NULL;
aom_codec_enc_cfg_t cfg;
int frame_cnt = 0;
aom_image_t raw;
int frame_avail;
int got_data = 0;
int flags = 0;
int i;
int pts = 0; // PTS starts at 0.
int frame_duration = 1; // 1 timebase tick per frame.
aom_svc_layer_id_t layer_id;
aom_svc_params_t svc_params;
aom_svc_ref_frame_config_t ref_frame_config;
aom_svc_ref_frame_comp_pred_t ref_frame_comp_pred;
#if CONFIG_INTERNAL_STATS
FILE *stats_file = fopen("opsnr.stt", "a");
if (stats_file == NULL) {
die("Cannot open opsnr.stt\n");
}
#endif
#if CONFIG_AV1_DECODER
aom_codec_ctx_t decoder;
#endif
struct RateControlMetrics rc;
int64_t cx_time = 0;
int64_t cx_time_layer[AOM_MAX_LAYERS]; // max number of layers.
int frame_cnt_layer[AOM_MAX_LAYERS];
double sum_bitrate = 0.0;
double sum_bitrate2 = 0.0;
double framerate = 30.0;
int use_svc_control = 1;
int set_err_resil_frame = 0;
int test_changing_bitrate = 0;
zero(rc.layer_target_bitrate);
memset(&layer_id, 0, sizeof(aom_svc_layer_id_t));
memset(&app_input, 0, sizeof(AppInput));
memset(&svc_params, 0, sizeof(svc_params));
// Flag to test dynamic scaling of source frames for single
// spatial stream, using the scaling_mode control.
const int test_dynamic_scaling_single_layer = 0;
// Flag to test setting speed per layer.
const int test_speed_per_layer = 0;
// Flag for testing active maps.
const int test_active_maps = 0;
// Flag for testing roi map.
const int test_roi_map = 0;
/* Setup default input stream settings */
for (i = 0; i < MAX_NUM_SPATIAL_LAYERS; ++i) {
app_input.input_ctx[i].framerate.numerator = 30;
app_input.input_ctx[i].framerate.denominator = 1;
app_input.input_ctx[i].only_i420 = 0;
app_input.input_ctx[i].bit_depth = AOM_BITS_8;
}
app_input.speed = 7;
exec_name = argv[0];
// start with default encoder configuration
#if GOOD_QUALITY
aom_codec_err_t res = aom_codec_enc_config_default(aom_codec_av1_cx(), &cfg,
AOM_USAGE_GOOD_QUALITY);
#else
aom_codec_err_t res = aom_codec_enc_config_default(aom_codec_av1_cx(), &cfg,
AOM_USAGE_REALTIME);
#endif
if (res != AOM_CODEC_OK) {
die("Failed to get config: %s\n", aom_codec_err_to_string(res));
}
#if GOOD_QUALITY
cfg.g_usage = AOM_USAGE_GOOD_QUALITY;
#else
// Real time parameters.
cfg.g_usage = AOM_USAGE_REALTIME;
#endif
cfg.rc_end_usage = AOM_CBR;
cfg.rc_min_quantizer = 2;
cfg.rc_max_quantizer = 52;
cfg.rc_undershoot_pct = 50;
cfg.rc_overshoot_pct = 50;
cfg.rc_buf_initial_sz = 600;
cfg.rc_buf_optimal_sz = 600;
cfg.rc_buf_sz = 1000;
cfg.rc_resize_mode = 0; // Set to RESIZE_DYNAMIC for dynamic resize.
cfg.g_lag_in_frames = 0;
cfg.kf_mode = AOM_KF_AUTO;
cfg.g_w = 0; // Force user to specify width and height for raw input.
cfg.g_h = 0;
parse_command_line(argc, argv, &app_input, &svc_params, &cfg);
int ts_number_layers = svc_params.number_temporal_layers;
int ss_number_layers = svc_params.number_spatial_layers;
unsigned int width = cfg.g_w;
unsigned int height = cfg.g_h;
if (app_input.layering_mode >= 0) {
if (ts_number_layers !=
mode_to_num_temporal_layers[app_input.layering_mode] ||
ss_number_layers !=
mode_to_num_spatial_layers[app_input.layering_mode]) {
die("Number of layers doesn't match layering mode.");
}
}
bool has_non_y4m_input = false;
for (i = 0; i < AOM_MAX_LAYERS; ++i) {
if (app_input.input_ctx[i].file_type != FILE_TYPE_Y4M) {
has_non_y4m_input = true;
break;
}
}
// Y4M reader has its own allocation.
if (has_non_y4m_input) {
if (!aom_img_alloc(&raw, AOM_IMG_FMT_I420, width, height, 32)) {
die("Failed to allocate image (%dx%d)", width, height);
}
}
aom_codec_iface_t *encoder = aom_codec_av1_cx();
memcpy(&rc.layer_target_bitrate[0], &svc_params.layer_target_bitrate[0],
sizeof(svc_params.layer_target_bitrate));
unsigned int total_rate = 0;
for (i = 0; i < ss_number_layers; i++) {
total_rate +=
svc_params
.layer_target_bitrate[i * ts_number_layers + ts_number_layers - 1];
}
if (total_rate != cfg.rc_target_bitrate) {
die("Incorrect total target bitrate, expected: %d", total_rate);
}
svc_params.framerate_factor[0] = 1;
if (ts_number_layers == 2) {
svc_params.framerate_factor[0] = 2;
svc_params.framerate_factor[1] = 1;
} else if (ts_number_layers == 3) {
svc_params.framerate_factor[0] = 4;
svc_params.framerate_factor[1] = 2;
svc_params.framerate_factor[2] = 1;
}
libaom_examples::MultilayerMetadata multilayer_metadata;
if (app_input.multilayer_metadata_file != NULL) {
if (!libaom_examples::parse_multilayer_file(
app_input.multilayer_metadata_file, &multilayer_metadata)) {
die("Failed to parse multilayer metadata");
}
libaom_examples::print_multilayer_metadata(multilayer_metadata);
}
framerate = cfg.g_timebase.den / cfg.g_timebase.num;
set_rate_control_metrics(&rc, framerate, ss_number_layers, ts_number_layers);
AvxVideoInfo info;
info.codec_fourcc = get_fourcc_by_aom_encoder(encoder);
info.frame_width = cfg.g_w;
info.frame_height = cfg.g_h;
info.time_base.numerator = cfg.g_timebase.num;
info.time_base.denominator = cfg.g_timebase.den;
// Open an output file for each stream.
for (int sl = 0; sl < ss_number_layers; ++sl) {
for (int tl = 0; tl < ts_number_layers; ++tl) {
i = sl * ts_number_layers + tl;
char file_name[PATH_MAX];
snprintf(file_name, sizeof(file_name), "%s_%d.av1",
app_input.output_filename, i);
if (app_input.output_obu) {
obu_files[i] = fopen(file_name, "wb");
if (!obu_files[i]) die("Failed to open %s for writing", file_name);
} else {
outfile[i] = aom_video_writer_open(file_name, kContainerIVF, &info);
if (!outfile[i]) die("Failed to open %s for writing", file_name);
}
}
}
if (app_input.output_obu) {
total_layer_obu_file = fopen(app_input.output_filename, "wb");
if (!total_layer_obu_file)
die("Failed to open %s for writing", app_input.output_filename);
} else {
total_layer_file =
aom_video_writer_open(app_input.output_filename, kContainerIVF, &info);
if (!total_layer_file)
die("Failed to open %s for writing", app_input.output_filename);
}
// Initialize codec.
aom_codec_ctx_t codec;
aom_codec_flags_t flag = 0;
flag |= cfg.g_input_bit_depth == AOM_BITS_8 ? 0 : AOM_CODEC_USE_HIGHBITDEPTH;
flag |= app_input.show_psnr ? AOM_CODEC_USE_PSNR : 0;
if (aom_codec_enc_init(&codec, encoder, &cfg, flag))
die_codec(&codec, "Failed to initialize encoder");
#if CONFIG_AV1_DECODER
if (app_input.decode) {
if (aom_codec_dec_init(&decoder, get_aom_decoder_by_index(0), NULL, 0))
die_codec(&decoder, "Failed to initialize decoder");
}
#endif
aom_codec_control(&codec, AOME_SET_CPUUSED, app_input.speed);
aom_codec_control(&codec, AV1E_SET_AQ_MODE, app_input.aq_mode ? 3 : 0);
aom_codec_control(&codec, AV1E_SET_GF_CBR_BOOST_PCT, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_CDEF, 1);
aom_codec_control(&codec, AV1E_SET_LOOPFILTER_CONTROL, 1);
aom_codec_control(&codec, AV1E_SET_ENABLE_WARPED_MOTION, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_OBMC, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_GLOBAL_MOTION, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_ORDER_HINT, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_TPL_MODEL, 0);
aom_codec_control(&codec, AV1E_SET_DELTAQ_MODE, 0);
#if GOOD_QUALITY
aom_codec_control(&codec, AV1E_SET_COEFF_COST_UPD_FREQ, 0);
aom_codec_control(&codec, AV1E_SET_MODE_COST_UPD_FREQ, 0);
aom_codec_control(&codec, AV1E_SET_MV_COST_UPD_FREQ, 0);
aom_codec_control(&codec, AV1E_SET_DV_COST_UPD_FREQ, 0);
#else
aom_codec_control(&codec, AV1E_SET_COEFF_COST_UPD_FREQ, 3);
aom_codec_control(&codec, AV1E_SET_MODE_COST_UPD_FREQ, 3);
aom_codec_control(&codec, AV1E_SET_MV_COST_UPD_FREQ, 3);
aom_codec_control(&codec, AV1E_SET_DV_COST_UPD_FREQ, 3);
#endif
aom_codec_control(&codec, AV1E_SET_CDF_UPDATE_MODE, 1);
// Settings to reduce key frame encoding time.
aom_codec_control(&codec, AV1E_SET_ENABLE_CFL_INTRA, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_SMOOTH_INTRA, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_ANGLE_DELTA, 0);
aom_codec_control(&codec, AV1E_SET_ENABLE_FILTER_INTRA, 0);
aom_codec_control(&codec, AV1E_SET_INTRA_DEFAULT_TX_ONLY, 1);
aom_codec_control(&codec, AV1E_SET_AUTO_TILES, 1);
aom_codec_control(&codec, AV1E_SET_TUNE_CONTENT, app_input.tune_content);
if (app_input.tune_content == AOM_CONTENT_SCREEN) {
aom_codec_control(&codec, AV1E_SET_ENABLE_PALETTE, 1);
// INTRABC is currently disabled for rt mode, as it's too slow.
aom_codec_control(&codec, AV1E_SET_ENABLE_INTRABC, 0);
}
if (app_input.use_external_rc) {
aom_codec_control(&codec, AV1E_SET_RTC_EXTERNAL_RC, 1);
}
aom_codec_control(&codec, AV1E_SET_MAX_CONSEC_FRAME_DROP_MS_CBR, INT_MAX);
aom_codec_control(&codec, AV1E_SET_SVC_FRAME_DROP_MODE,
AOM_FULL_SUPERFRAME_DROP);
aom_codec_control(&codec, AV1E_SET_POSTENCODE_DROP_RTC, 1);
svc_params.number_spatial_layers = ss_number_layers;
svc_params.number_temporal_layers = ts_number_layers;
for (i = 0; i < ss_number_layers * ts_number_layers; ++i) {
svc_params.max_quantizers[i] = cfg.rc_max_quantizer;
svc_params.min_quantizers[i] = cfg.rc_min_quantizer;
}
// SET QUANTIZER PER LAYER, E.G FOR 2 SPATIAL LAYERS:
// svc_params.max_quantizers[0] = 40;
// svc_params.min_quantizers[0] = 40;
// svc_params.max_quantizers[1] = 50;
// svc_params.min_quantizers[1] = 50;
if (!app_input.scale_factors_explicitly_set) {
for (i = 0; i < ss_number_layers; ++i) {
svc_params.scaling_factor_num[i] = 1;
svc_params.scaling_factor_den[i] = 1;
}
if (ss_number_layers == 2) {
svc_params.scaling_factor_num[0] = 1;
svc_params.scaling_factor_den[0] = 2;
} else if (ss_number_layers == 3) {
svc_params.scaling_factor_num[0] = 1;
svc_params.scaling_factor_den[0] = 4;
svc_params.scaling_factor_num[1] = 1;
svc_params.scaling_factor_den[1] = 2;
}
}
aom_codec_control(&codec, AV1E_SET_SVC_PARAMS, &svc_params);
// TODO(aomedia:3032): Configure KSVC in fixed mode.
// This controls the maximum target size of the key frame.
// For generating smaller key frames, use a smaller max_intra_size_pct
// value, like 100 or 200.
{
const int max_intra_size_pct = 300;
aom_codec_control(&codec, AOME_SET_MAX_INTRA_BITRATE_PCT,
max_intra_size_pct);
}
for (int lx = 0; lx < ts_number_layers * ss_number_layers; lx++) {
cx_time_layer[lx] = 0;
frame_cnt_layer[lx] = 0;
}
std::unique_ptr<aom::AV1RateControlRTC> rc_api;
if (app_input.use_external_rc) {
const aom::AV1RateControlRtcConfig rc_cfg =
create_rtc_rc_config(cfg, app_input);
rc_api = aom::AV1RateControlRTC::Create(rc_cfg);
}
frame_avail = 1;
struct psnr_stats psnr_stream;
memset(&psnr_stream, 0, sizeof(psnr_stream));
while (frame_avail || got_data) {
struct aom_usec_timer timer;
frame_avail = read_frame(&(app_input.input_ctx[0]), &raw);
// Loop over spatial layers.
for (int slx = 0; slx < ss_number_layers; slx++) {
if (slx > 0 && app_input.input_ctx[slx].filename != NULL) {
const int previous_layer_frame_avail = frame_avail;
frame_avail = read_frame(&(app_input.input_ctx[slx]), &raw);
if (previous_layer_frame_avail != frame_avail) {
die("Mismatch in number of frames between spatial layer input files");
}
}
aom_codec_iter_t iter = NULL;
const aom_codec_cx_pkt_t *pkt;
int reference_updated = 0;
int layer = 0;
// Flag for superframe whose base is key.
int is_key_frame = (frame_cnt % cfg.kf_max_dist) == 0;
// For flexible mode:
if (app_input.layering_mode >= 0) {
// Set the reference/update flags, layer_id, and reference_map
// buffer index.
set_layer_pattern(app_input.layering_mode, frame_cnt, &layer_id,
&ref_frame_config, &ref_frame_comp_pred,
&use_svc_control, slx, is_key_frame,
(app_input.layering_mode == 10), app_input.speed,
&reference_updated, test_roi_map);
aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
if (use_svc_control) {
aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_CONFIG,
&ref_frame_config);
aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_COMP_PRED,
&ref_frame_comp_pred);
}
if (app_input.multilayer_metadata_file != NULL) {
add_multilayer_metadata(&raw, multilayer_metadata);
}
// Set the speed per layer.
if (test_speed_per_layer) {
int speed_per_layer = 10;
if (layer_id.spatial_layer_id == 0) {
if (layer_id.temporal_layer_id == 0) speed_per_layer = 6;
if (layer_id.temporal_layer_id == 1) speed_per_layer = 7;
if (layer_id.temporal_layer_id == 2) speed_per_layer = 8;
} else if (layer_id.spatial_layer_id == 1) {
if (layer_id.temporal_layer_id == 0) speed_per_layer = 7;
if (layer_id.temporal_layer_id == 1) speed_per_layer = 8;
if (layer_id.temporal_layer_id == 2) speed_per_layer = 9;
} else if (layer_id.spatial_layer_id == 2) {
if (layer_id.temporal_layer_id == 0) speed_per_layer = 8;
if (layer_id.temporal_layer_id == 1) speed_per_layer = 9;
if (layer_id.temporal_layer_id == 2) speed_per_layer = 10;
}
aom_codec_control(&codec, AOME_SET_CPUUSED, speed_per_layer);
}
} else {
// Only up to 3 temporal layers supported in fixed mode.
// Only need to set spatial and temporal layer_id: reference
// prediction, refresh, and buffer_idx are set internally.
layer_id.spatial_layer_id = slx;
layer_id.temporal_layer_id = 0;
if (ts_number_layers == 2) {
layer_id.temporal_layer_id = (frame_cnt % 2) != 0;
} else if (ts_number_layers == 3) {
if (frame_cnt % 2 != 0)
layer_id.temporal_layer_id = 2;
else if ((frame_cnt > 1) && ((frame_cnt - 2) % 4 == 0))
layer_id.temporal_layer_id = 1;
}
aom_codec_control(&codec, AV1E_SET_SVC_LAYER_ID, &layer_id);
}
if (set_err_resil_frame && cfg.g_error_resilient == 0) {
// Set error_resilient per frame: off/0 for base layer and
// on/1 for enhancement layer frames.
// Note that this is can only be done on the fly/per-frame/layer
// if the config error_resilience is off/0. See the logic for updating
// in set_encoder_config():
// tool_cfg->error_resilient_mode =
// cfg->g_error_resilient | extra_cfg->error_resilient_mode;
const int err_resil_mode =
layer_id.spatial_layer_id > 0 || layer_id.temporal_layer_id > 0;
aom_codec_control(&codec, AV1E_SET_ERROR_RESILIENT_MODE,
err_resil_mode);
}
layer = slx * ts_number_layers + layer_id.temporal_layer_id;
if (frame_avail && slx == 0) ++rc.layer_input_frames[layer];
if (test_dynamic_scaling_single_layer) {
// Example to scale source down by 2x2, then 4x4, and then back up to
// 2x2, and then back to original.
int frame_2x2 = 200;
int frame_4x4 = 400;
int frame_2x2up = 600;
int frame_orig = 800;
if (frame_cnt >= frame_2x2 && frame_cnt < frame_4x4) {
// Scale source down by 2x2.
struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
} else if (frame_cnt >= frame_4x4 && frame_cnt < frame_2x2up) {
// Scale source down by 4x4.
struct aom_scaling_mode mode = { AOME_ONEFOUR, AOME_ONEFOUR };
aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
} else if (frame_cnt >= frame_2x2up && frame_cnt < frame_orig) {
// Source back up to 2x2.
struct aom_scaling_mode mode = { AOME_ONETWO, AOME_ONETWO };
aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
} else if (frame_cnt >= frame_orig) {
// Source back up to original resolution (no scaling).
struct aom_scaling_mode mode = { AOME_NORMAL, AOME_NORMAL };
aom_codec_control(&codec, AOME_SET_SCALEMODE, &mode);
}
if (frame_cnt == frame_2x2 || frame_cnt == frame_4x4 ||
frame_cnt == frame_2x2up || frame_cnt == frame_orig) {
// For dynamic resize testing on single layer: refresh all references
// on the resized frame: this is to avoid decode error:
// if resize goes down by >= 4x4 then libaom decoder will throw an
// error that some reference (even though not used) is beyond the
// limit size (must be smaller than 4x4).
for (i = 0; i < REF_FRAMES; i++) ref_frame_config.refresh[i] = 1;
if (use_svc_control) {
aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_CONFIG,
&ref_frame_config);
aom_codec_control(&codec, AV1E_SET_SVC_REF_FRAME_COMP_PRED,
&ref_frame_comp_pred);
}
}
}
// Change target_bitrate every other frame.
if (test_changing_bitrate && frame_cnt % 2 == 0) {
if (frame_cnt < 500)
cfg.rc_target_bitrate += 10;
else
cfg.rc_target_bitrate -= 10;
// Do big increase and decrease.
if (frame_cnt == 100) cfg.rc_target_bitrate <<= 1;
if (frame_cnt == 600) cfg.rc_target_bitrate >>= 1;
if (cfg.rc_target_bitrate < 100) cfg.rc_target_bitrate = 100;
// Call change_config, or bypass with new control.
// res = aom_codec_enc_config_set(&codec, &cfg);
if (aom_codec_control(&codec, AV1E_SET_BITRATE_ONE_PASS_CBR,
cfg.rc_target_bitrate))
die_codec(&codec, "Failed to SET_BITRATE_ONE_PASS_CBR");
}
if (rc_api) {
aom::AV1FrameParamsRTC frame_params;
// TODO(jianj): Add support for SVC.
frame_params.spatial_layer_id = 0;
frame_params.temporal_layer_id = 0;
frame_params.frame_type =
is_key_frame ? aom::kKeyFrame : aom::kInterFrame;
rc_api->ComputeQP(frame_params);
const int current_qp = rc_api->GetQP();
if (aom_codec_control(&codec, AV1E_SET_QUANTIZER_ONE_PASS,
qindex_to_quantizer(current_qp))) {
die_codec(&codec, "Failed to SET_QUANTIZER_ONE_PASS");
}
}
if (test_active_maps) set_active_map(&cfg, &codec, frame_cnt);
if (test_roi_map) set_roi_map(&cfg, &codec, kDeltaQ);
// Do the layer encode.
aom_usec_timer_start(&timer);
if (aom_codec_encode(&codec, frame_avail ? &raw : NULL, pts, 1, flags))
die_codec(&codec, "Failed to encode frame");
aom_usec_timer_mark(&timer);
cx_time += aom_usec_timer_elapsed(&timer);
cx_time_layer[layer] += aom_usec_timer_elapsed(&timer);
frame_cnt_layer[layer] += 1;
// Get the high motion content flag.
int content_flag = 0;
if (aom_codec_control(&codec, AV1E_GET_HIGH_MOTION_CONTENT_SCREEN_RTC,
&content_flag)) {
die_codec(&codec, "Failed to GET_HIGH_MOTION_CONTENT_SCREEN_RTC");
}
got_data = 0;
// For simulcast (mode 11): write out each spatial layer to the file.
int ss_layers_write = (app_input.layering_mode == 11)
? layer_id.spatial_layer_id + 1
: ss_number_layers;
while ((pkt = aom_codec_get_cx_data(&codec, &iter))) {
switch (pkt->kind) {
case AOM_CODEC_CX_FRAME_PKT:
for (int sl = layer_id.spatial_layer_id; sl < ss_layers_write;
++sl) {
for (int tl = layer_id.temporal_layer_id; tl < ts_number_layers;
++tl) {
int j = sl * ts_number_layers + tl;
if (app_input.output_obu) {
fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
obu_files[j]);
} else {
aom_video_writer_write_frame(
outfile[j],
reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
pkt->data.frame.sz, pts);
}
if (sl == layer_id.spatial_layer_id)
rc.layer_encoding_bitrate[j] += 8.0 * pkt->data.frame.sz;
}
}
got_data = 1;
// Write everything into the top layer.
if (app_input.output_obu) {
fwrite(pkt->data.frame.buf, 1, pkt->data.frame.sz,
total_layer_obu_file);
} else {
aom_video_writer_write_frame(
total_layer_file,
reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
pkt->data.frame.sz, pts);
}
// Keep count of rate control stats per layer (for non-key).
if (!(pkt->data.frame.flags & AOM_FRAME_IS_KEY)) {
int j = layer_id.spatial_layer_id * ts_number_layers +
layer_id.temporal_layer_id;
assert(j >= 0);
rc.layer_avg_frame_size[j] += 8.0 * pkt->data.frame.sz;
rc.layer_avg_rate_mismatch[j] +=
fabs(8.0 * pkt->data.frame.sz - rc.layer_pfb[j]) /
rc.layer_pfb[j];
if (slx == 0) ++rc.layer_enc_frames[layer_id.temporal_layer_id];
}
if (rc_api) {
rc_api->PostEncodeUpdate(pkt->data.frame.sz);
}
// Update for short-time encoding bitrate states, for moving window
// of size rc->window, shifted by rc->window / 2.
// Ignore first window segment, due to key frame.
// For spatial layers: only do this for top/highest SL.
if (frame_cnt > rc.window_size && slx == ss_number_layers - 1) {
sum_bitrate += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
rc.window_size = (rc.window_size <= 0) ? 1 : rc.window_size;
if (frame_cnt % rc.window_size == 0) {
rc.window_count += 1;
rc.avg_st_encoding_bitrate += sum_bitrate / rc.window_size;
rc.variance_st_encoding_bitrate +=
(sum_bitrate / rc.window_size) *
(sum_bitrate / rc.window_size);
sum_bitrate = 0.0;
}
}
// Second shifted window.
if (frame_cnt > rc.window_size + rc.window_size / 2 &&
slx == ss_number_layers - 1) {
sum_bitrate2 += 0.001 * 8.0 * pkt->data.frame.sz * framerate;
if (frame_cnt > 2 * rc.window_size &&
frame_cnt % rc.window_size == 0) {
rc.window_count += 1;
rc.avg_st_encoding_bitrate += sum_bitrate2 / rc.window_size;
rc.variance_st_encoding_bitrate +=
(sum_bitrate2 / rc.window_size) *
(sum_bitrate2 / rc.window_size);
sum_bitrate2 = 0.0;
}
}
#if CONFIG_AV1_DECODER
if (app_input.decode) {
if (aom_codec_decode(
&decoder,
reinterpret_cast<const uint8_t *>(pkt->data.frame.buf),
pkt->data.frame.sz, NULL))
die_codec(&decoder, "Failed to decode frame");
}
#endif
break;
case AOM_CODEC_PSNR_PKT:
if (app_input.show_psnr) {
psnr_stream.psnr_sse_total[0] += pkt->data.psnr.sse[0];
psnr_stream.psnr_samples_total[0] += pkt->data.psnr.samples[0];
for (int plane = 0; plane < 4; plane++) {
psnr_stream.psnr_totals[0][plane] += pkt->data.psnr.psnr[plane];
}
psnr_stream.psnr_count[0]++;
}
break;
default: break;
}
}
#if CONFIG_AV1_DECODER
if (got_data && app_input.decode) {
// Don't look for mismatch on non reference frames.
if (reference_updated) {
if (test_decode(&codec, &decoder, frame_cnt)) {
#if CONFIG_INTERNAL_STATS
fprintf(stats_file, "First mismatch occurred in frame %d\n",
frame_cnt);
fclose(stats_file);
#endif
fatal("Mismatch seen");
}
}
}
#endif
} // loop over spatial layers
++frame_cnt;
pts += frame_duration;
}
for (i = 0; i < MAX_NUM_SPATIAL_LAYERS; ++i) {
if (app_input.input_ctx[i].filename == NULL) {
break;
}
close_input_file(&(app_input.input_ctx[i]));
}
printout_rate_control_summary(&rc, frame_cnt, ss_number_layers,
ts_number_layers);
printf("\n");
for (int slx = 0; slx < ss_number_layers; slx++)
for (int tlx = 0; tlx < ts_number_layers; tlx++) {
int lx = slx * ts_number_layers + tlx;
printf("Per layer encoding time/FPS stats for encoder: %d %d %d %f %f \n",
slx, tlx, frame_cnt_layer[lx],
(float)cx_time_layer[lx] / (double)(frame_cnt_layer[lx] * 1000),
1000000 * (double)frame_cnt_layer[lx] / (double)cx_time_layer[lx]);
}
printf("\n");
printf("Frame cnt and encoding time/FPS stats for encoding: %d %f %f\n",
frame_cnt, 1000 * (float)cx_time / (double)(frame_cnt * 1000000),
1000000 * (double)frame_cnt / (double)cx_time);
if (app_input.show_psnr) {
show_psnr(&psnr_stream, 255.0);
}
if (aom_codec_destroy(&codec)) die_codec(&codec, "Failed to destroy encoder");
#if CONFIG_AV1_DECODER
if (app_input.decode) {
if (aom_codec_destroy(&decoder))
die_codec(&decoder, "Failed to destroy decoder");
}
#endif
#if CONFIG_INTERNAL_STATS
fprintf(stats_file, "No mismatch detected in recon buffers\n");
fclose(stats_file);
#endif
// Try to rewrite the output file headers with the actual frame count.
for (i = 0; i < ss_number_layers * ts_number_layers; ++i)
aom_video_writer_close(outfile[i]);
aom_video_writer_close(total_layer_file);
if (has_non_y4m_input) {
aom_img_free(&raw);
}
return EXIT_SUCCESS;
}
|