vp9_uncompressed_header_parser.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122
  1. // Copyright 2016 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "media/filters/vp9_uncompressed_header_parser.h"
  5. #include <type_traits>
  6. #include "base/logging.h"
  7. namespace media {
  8. namespace {
  9. // 10.5 Default probability tables
  10. Vp9FrameContext kVp9DefaultFrameContext = {
  11. // tx_probs_8x8
  12. {{100}, {66}},
  13. // tx_probs_16x16
  14. {{20, 152}, {15, 101}},
  15. // tx_probs_32x32
  16. {{3, 136, 37}, {5, 52, 13}},
  17. // coef_probs
  18. {// 4x4
  19. {{{{{195, 29, 183}, {84, 49, 136}, {8, 42, 71}},
  20. {{31, 107, 169},
  21. {35, 99, 159},
  22. {17, 82, 140},
  23. {8, 66, 114},
  24. {2, 44, 76},
  25. {1, 19, 32}},
  26. {{40, 132, 201},
  27. {29, 114, 187},
  28. {13, 91, 157},
  29. {7, 75, 127},
  30. {3, 58, 95},
  31. {1, 28, 47}},
  32. {{69, 142, 221},
  33. {42, 122, 201},
  34. {15, 91, 159},
  35. {6, 67, 121},
  36. {1, 42, 77},
  37. {1, 17, 31}},
  38. {{102, 148, 228},
  39. {67, 117, 204},
  40. {17, 82, 154},
  41. {6, 59, 114},
  42. {2, 39, 75},
  43. {1, 15, 29}},
  44. {{156, 57, 233},
  45. {119, 57, 212},
  46. {58, 48, 163},
  47. {29, 40, 124},
  48. {12, 30, 81},
  49. {3, 12, 31}}},
  50. {{{191, 107, 226}, {124, 117, 204}, {25, 99, 155}},
  51. {{29, 148, 210},
  52. {37, 126, 194},
  53. {8, 93, 157},
  54. {2, 68, 118},
  55. {1, 39, 69},
  56. {1, 17, 33}},
  57. {{41, 151, 213},
  58. {27, 123, 193},
  59. {3, 82, 144},
  60. {1, 58, 105},
  61. {1, 32, 60},
  62. {1, 13, 26}},
  63. {{59, 159, 220},
  64. {23, 126, 198},
  65. {4, 88, 151},
  66. {1, 66, 114},
  67. {1, 38, 71},
  68. {1, 18, 34}},
  69. {{114, 136, 232},
  70. {51, 114, 207},
  71. {11, 83, 155},
  72. {3, 56, 105},
  73. {1, 33, 65},
  74. {1, 17, 34}},
  75. {{149, 65, 234},
  76. {121, 57, 215},
  77. {61, 49, 166},
  78. {28, 36, 114},
  79. {12, 25, 76},
  80. {3, 16, 42}}}},
  81. {{{{214, 49, 220}, {132, 63, 188}, {42, 65, 137}},
  82. {{85, 137, 221},
  83. {104, 131, 216},
  84. {49, 111, 192},
  85. {21, 87, 155},
  86. {2, 49, 87},
  87. {1, 16, 28}},
  88. {{89, 163, 230},
  89. {90, 137, 220},
  90. {29, 100, 183},
  91. {10, 70, 135},
  92. {2, 42, 81},
  93. {1, 17, 33}},
  94. {{108, 167, 237},
  95. {55, 133, 222},
  96. {15, 97, 179},
  97. {4, 72, 135},
  98. {1, 45, 85},
  99. {1, 19, 38}},
  100. {{124, 146, 240},
  101. {66, 124, 224},
  102. {17, 88, 175},
  103. {4, 58, 122},
  104. {1, 36, 75},
  105. {1, 18, 37}},
  106. {{141, 79, 241},
  107. {126, 70, 227},
  108. {66, 58, 182},
  109. {30, 44, 136},
  110. {12, 34, 96},
  111. {2, 20, 47}}},
  112. {{{229, 99, 249}, {143, 111, 235}, {46, 109, 192}},
  113. {{82, 158, 236},
  114. {94, 146, 224},
  115. {25, 117, 191},
  116. {9, 87, 149},
  117. {3, 56, 99},
  118. {1, 33, 57}},
  119. {{83, 167, 237},
  120. {68, 145, 222},
  121. {10, 103, 177},
  122. {2, 72, 131},
  123. {1, 41, 79},
  124. {1, 20, 39}},
  125. {{99, 167, 239},
  126. {47, 141, 224},
  127. {10, 104, 178},
  128. {2, 73, 133},
  129. {1, 44, 85},
  130. {1, 22, 47}},
  131. {{127, 145, 243},
  132. {71, 129, 228},
  133. {17, 93, 177},
  134. {3, 61, 124},
  135. {1, 41, 84},
  136. {1, 21, 52}},
  137. {{157, 78, 244},
  138. {140, 72, 231},
  139. {69, 58, 184},
  140. {31, 44, 137},
  141. {14, 38, 105},
  142. {8, 23, 61}}}}},
  143. // 8x8
  144. {{{{{125, 34, 187}, {52, 41, 133}, {6, 31, 56}},
  145. {{37, 109, 153},
  146. {51, 102, 147},
  147. {23, 87, 128},
  148. {8, 67, 101},
  149. {1, 41, 63},
  150. {1, 19, 29}},
  151. {{31, 154, 185},
  152. {17, 127, 175},
  153. {6, 96, 145},
  154. {2, 73, 114},
  155. {1, 51, 82},
  156. {1, 28, 45}},
  157. {{23, 163, 200},
  158. {10, 131, 185},
  159. {2, 93, 148},
  160. {1, 67, 111},
  161. {1, 41, 69},
  162. {1, 14, 24}},
  163. {{29, 176, 217},
  164. {12, 145, 201},
  165. {3, 101, 156},
  166. {1, 69, 111},
  167. {1, 39, 63},
  168. {1, 14, 23}},
  169. {{57, 192, 233},
  170. {25, 154, 215},
  171. {6, 109, 167},
  172. {3, 78, 118},
  173. {1, 48, 69},
  174. {1, 21, 29}}},
  175. {{{202, 105, 245}, {108, 106, 216}, {18, 90, 144}},
  176. {{33, 172, 219},
  177. {64, 149, 206},
  178. {14, 117, 177},
  179. {5, 90, 141},
  180. {2, 61, 95},
  181. {1, 37, 57}},
  182. {{33, 179, 220},
  183. {11, 140, 198},
  184. {1, 89, 148},
  185. {1, 60, 104},
  186. {1, 33, 57},
  187. {1, 12, 21}},
  188. {{30, 181, 221},
  189. {8, 141, 198},
  190. {1, 87, 145},
  191. {1, 58, 100},
  192. {1, 31, 55},
  193. {1, 12, 20}},
  194. {{32, 186, 224},
  195. {7, 142, 198},
  196. {1, 86, 143},
  197. {1, 58, 100},
  198. {1, 31, 55},
  199. {1, 12, 22}},
  200. {{57, 192, 227},
  201. {20, 143, 204},
  202. {3, 96, 154},
  203. {1, 68, 112},
  204. {1, 42, 69},
  205. {1, 19, 32}}}},
  206. {{{{212, 35, 215}, {113, 47, 169}, {29, 48, 105}},
  207. {{74, 129, 203},
  208. {106, 120, 203},
  209. {49, 107, 178},
  210. {19, 84, 144},
  211. {4, 50, 84},
  212. {1, 15, 25}},
  213. {{71, 172, 217},
  214. {44, 141, 209},
  215. {15, 102, 173},
  216. {6, 76, 133},
  217. {2, 51, 89},
  218. {1, 24, 42}},
  219. {{64, 185, 231},
  220. {31, 148, 216},
  221. {8, 103, 175},
  222. {3, 74, 131},
  223. {1, 46, 81},
  224. {1, 18, 30}},
  225. {{65, 196, 235},
  226. {25, 157, 221},
  227. {5, 105, 174},
  228. {1, 67, 120},
  229. {1, 38, 69},
  230. {1, 15, 30}},
  231. {{65, 204, 238},
  232. {30, 156, 224},
  233. {7, 107, 177},
  234. {2, 70, 124},
  235. {1, 42, 73},
  236. {1, 18, 34}}},
  237. {{{225, 86, 251}, {144, 104, 235}, {42, 99, 181}},
  238. {{85, 175, 239},
  239. {112, 165, 229},
  240. {29, 136, 200},
  241. {12, 103, 162},
  242. {6, 77, 123},
  243. {2, 53, 84}},
  244. {{75, 183, 239},
  245. {30, 155, 221},
  246. {3, 106, 171},
  247. {1, 74, 128},
  248. {1, 44, 76},
  249. {1, 17, 28}},
  250. {{73, 185, 240},
  251. {27, 159, 222},
  252. {2, 107, 172},
  253. {1, 75, 127},
  254. {1, 42, 73},
  255. {1, 17, 29}},
  256. {{62, 190, 238},
  257. {21, 159, 222},
  258. {2, 107, 172},
  259. {1, 72, 122},
  260. {1, 40, 71},
  261. {1, 18, 32}},
  262. {{61, 199, 240},
  263. {27, 161, 226},
  264. {4, 113, 180},
  265. {1, 76, 129},
  266. {1, 46, 80},
  267. {1, 23, 41}}}}},
  268. // 16x16
  269. {{{{{7, 27, 153}, {5, 30, 95}, {1, 16, 30}},
  270. {{50, 75, 127},
  271. {57, 75, 124},
  272. {27, 67, 108},
  273. {10, 54, 86},
  274. {1, 33, 52},
  275. {1, 12, 18}},
  276. {{43, 125, 151},
  277. {26, 108, 148},
  278. {7, 83, 122},
  279. {2, 59, 89},
  280. {1, 38, 60},
  281. {1, 17, 27}},
  282. {{23, 144, 163},
  283. {13, 112, 154},
  284. {2, 75, 117},
  285. {1, 50, 81},
  286. {1, 31, 51},
  287. {1, 14, 23}},
  288. {{18, 162, 185},
  289. {6, 123, 171},
  290. {1, 78, 125},
  291. {1, 51, 86},
  292. {1, 31, 54},
  293. {1, 14, 23}},
  294. {{15, 199, 227},
  295. {3, 150, 204},
  296. {1, 91, 146},
  297. {1, 55, 95},
  298. {1, 30, 53},
  299. {1, 11, 20}}},
  300. {{{19, 55, 240}, {19, 59, 196}, {3, 52, 105}},
  301. {{41, 166, 207},
  302. {104, 153, 199},
  303. {31, 123, 181},
  304. {14, 101, 152},
  305. {5, 72, 106},
  306. {1, 36, 52}},
  307. {{35, 176, 211},
  308. {12, 131, 190},
  309. {2, 88, 144},
  310. {1, 60, 101},
  311. {1, 36, 60},
  312. {1, 16, 28}},
  313. {{28, 183, 213},
  314. {8, 134, 191},
  315. {1, 86, 142},
  316. {1, 56, 96},
  317. {1, 30, 53},
  318. {1, 12, 20}},
  319. {{20, 190, 215},
  320. {4, 135, 192},
  321. {1, 84, 139},
  322. {1, 53, 91},
  323. {1, 28, 49},
  324. {1, 11, 20}},
  325. {{13, 196, 216},
  326. {2, 137, 192},
  327. {1, 86, 143},
  328. {1, 57, 99},
  329. {1, 32, 56},
  330. {1, 13, 24}}}},
  331. {{{{211, 29, 217}, {96, 47, 156}, {22, 43, 87}},
  332. {{78, 120, 193},
  333. {111, 116, 186},
  334. {46, 102, 164},
  335. {15, 80, 128},
  336. {2, 49, 76},
  337. {1, 18, 28}},
  338. {{71, 161, 203},
  339. {42, 132, 192},
  340. {10, 98, 150},
  341. {3, 69, 109},
  342. {1, 44, 70},
  343. {1, 18, 29}},
  344. {{57, 186, 211},
  345. {30, 140, 196},
  346. {4, 93, 146},
  347. {1, 62, 102},
  348. {1, 38, 65},
  349. {1, 16, 27}},
  350. {{47, 199, 217},
  351. {14, 145, 196},
  352. {1, 88, 142},
  353. {1, 57, 98},
  354. {1, 36, 62},
  355. {1, 15, 26}},
  356. {{26, 219, 229},
  357. {5, 155, 207},
  358. {1, 94, 151},
  359. {1, 60, 104},
  360. {1, 36, 62},
  361. {1, 16, 28}}},
  362. {{{233, 29, 248}, {146, 47, 220}, {43, 52, 140}},
  363. {{100, 163, 232},
  364. {179, 161, 222},
  365. {63, 142, 204},
  366. {37, 113, 174},
  367. {26, 89, 137},
  368. {18, 68, 97}},
  369. {{85, 181, 230},
  370. {32, 146, 209},
  371. {7, 100, 164},
  372. {3, 71, 121},
  373. {1, 45, 77},
  374. {1, 18, 30}},
  375. {{65, 187, 230},
  376. {20, 148, 207},
  377. {2, 97, 159},
  378. {1, 68, 116},
  379. {1, 40, 70},
  380. {1, 14, 29}},
  381. {{40, 194, 227},
  382. {8, 147, 204},
  383. {1, 94, 155},
  384. {1, 65, 112},
  385. {1, 39, 66},
  386. {1, 14, 26}},
  387. {{16, 208, 228},
  388. {3, 151, 207},
  389. {1, 98, 160},
  390. {1, 67, 117},
  391. {1, 41, 74},
  392. {1, 17, 31}}}}},
  393. // 32x32
  394. {{{{{17, 38, 140}, {7, 34, 80}, {1, 17, 29}},
  395. {{37, 75, 128},
  396. {41, 76, 128},
  397. {26, 66, 116},
  398. {12, 52, 94},
  399. {2, 32, 55},
  400. {1, 10, 16}},
  401. {{50, 127, 154},
  402. {37, 109, 152},
  403. {16, 82, 121},
  404. {5, 59, 85},
  405. {1, 35, 54},
  406. {1, 13, 20}},
  407. {{40, 142, 167},
  408. {17, 110, 157},
  409. {2, 71, 112},
  410. {1, 44, 72},
  411. {1, 27, 45},
  412. {1, 11, 17}},
  413. {{30, 175, 188},
  414. {9, 124, 169},
  415. {1, 74, 116},
  416. {1, 48, 78},
  417. {1, 30, 49},
  418. {1, 11, 18}},
  419. {{10, 222, 223},
  420. {2, 150, 194},
  421. {1, 83, 128},
  422. {1, 48, 79},
  423. {1, 27, 45},
  424. {1, 11, 17}}},
  425. {{{36, 41, 235}, {29, 36, 193}, {10, 27, 111}},
  426. {{85, 165, 222},
  427. {177, 162, 215},
  428. {110, 135, 195},
  429. {57, 113, 168},
  430. {23, 83, 120},
  431. {10, 49, 61}},
  432. {{85, 190, 223},
  433. {36, 139, 200},
  434. {5, 90, 146},
  435. {1, 60, 103},
  436. {1, 38, 65},
  437. {1, 18, 30}},
  438. {{72, 202, 223},
  439. {23, 141, 199},
  440. {2, 86, 140},
  441. {1, 56, 97},
  442. {1, 36, 61},
  443. {1, 16, 27}},
  444. {{55, 218, 225},
  445. {13, 145, 200},
  446. {1, 86, 141},
  447. {1, 57, 99},
  448. {1, 35, 61},
  449. {1, 13, 22}},
  450. {{15, 235, 212},
  451. {1, 132, 184},
  452. {1, 84, 139},
  453. {1, 57, 97},
  454. {1, 34, 56},
  455. {1, 14, 23}}}},
  456. {{{{181, 21, 201}, {61, 37, 123}, {10, 38, 71}},
  457. {{47, 106, 172},
  458. {95, 104, 173},
  459. {42, 93, 159},
  460. {18, 77, 131},
  461. {4, 50, 81},
  462. {1, 17, 23}},
  463. {{62, 147, 199},
  464. {44, 130, 189},
  465. {28, 102, 154},
  466. {18, 75, 115},
  467. {2, 44, 65},
  468. {1, 12, 19}},
  469. {{55, 153, 210},
  470. {24, 130, 194},
  471. {3, 93, 146},
  472. {1, 61, 97},
  473. {1, 31, 50},
  474. {1, 10, 16}},
  475. {{49, 186, 223},
  476. {17, 148, 204},
  477. {1, 96, 142},
  478. {1, 53, 83},
  479. {1, 26, 44},
  480. {1, 11, 17}},
  481. {{13, 217, 212},
  482. {2, 136, 180},
  483. {1, 78, 124},
  484. {1, 50, 83},
  485. {1, 29, 49},
  486. {1, 14, 23}}},
  487. {{{197, 13, 247}, {82, 17, 222}, {25, 17, 162}},
  488. {{126, 186, 247},
  489. {234, 191, 243},
  490. {176, 177, 234},
  491. {104, 158, 220},
  492. {66, 128, 186},
  493. {55, 90, 137}},
  494. {{111, 197, 242},
  495. {46, 158, 219},
  496. {9, 104, 171},
  497. {2, 65, 125},
  498. {1, 44, 80},
  499. {1, 17, 91}},
  500. {{104, 208, 245},
  501. {39, 168, 224},
  502. {3, 109, 162},
  503. {1, 79, 124},
  504. {1, 50, 102},
  505. {1, 43, 102}},
  506. {{84, 220, 246},
  507. {31, 177, 231},
  508. {2, 115, 180},
  509. {1, 79, 134},
  510. {1, 55, 77},
  511. {1, 60, 79}},
  512. {{43, 243, 240},
  513. {8, 180, 217},
  514. {1, 115, 166},
  515. {1, 84, 121},
  516. {1, 51, 67},
  517. {1, 16, 6}}}}}},
  518. // skip_prob
  519. {192, 128, 64},
  520. // inter_mode_probs
  521. {{2, 173, 34},
  522. {7, 145, 85},
  523. {7, 166, 63},
  524. {7, 94, 66},
  525. {8, 64, 46},
  526. {17, 81, 31},
  527. {25, 29, 30}},
  528. // interp_filter_probs
  529. {{235, 162}, {36, 255}, {34, 3}, {149, 144}},
  530. // is_inter_prob
  531. {9, 102, 187, 225},
  532. // comp_mode_prob
  533. {239, 183, 119, 96, 41},
  534. // single_ref_prob
  535. {{33, 16}, {77, 74}, {142, 142}, {172, 170}, {238, 247}},
  536. // comp_ref_prob
  537. {50, 126, 123, 221, 226},
  538. // y_mode_probs
  539. {{65, 32, 18, 144, 162, 194, 41, 51, 98},
  540. {132, 68, 18, 165, 217, 196, 45, 40, 78},
  541. {173, 80, 19, 176, 240, 193, 64, 35, 46},
  542. {221, 135, 38, 194, 248, 121, 96, 85, 29}},
  543. // uv_mode_probs
  544. {{120, 7, 76, 176, 208, 126, 28, 54, 103},
  545. {48, 12, 154, 155, 139, 90, 34, 117, 119},
  546. {67, 6, 25, 204, 243, 158, 13, 21, 96},
  547. {97, 5, 44, 131, 176, 139, 48, 68, 97},
  548. {83, 5, 42, 156, 111, 152, 26, 49, 152},
  549. {80, 5, 58, 178, 74, 83, 33, 62, 145},
  550. {86, 5, 32, 154, 192, 168, 14, 22, 163},
  551. {85, 5, 32, 156, 216, 148, 19, 29, 73},
  552. {77, 7, 64, 116, 132, 122, 37, 126, 120},
  553. {101, 21, 107, 181, 192, 103, 19, 67, 125}},
  554. // partition_probs
  555. {{199, 122, 141},
  556. {147, 63, 159},
  557. {148, 133, 118},
  558. {121, 104, 114},
  559. {174, 73, 87},
  560. {92, 41, 83},
  561. {82, 99, 50},
  562. {53, 39, 39},
  563. {177, 58, 59},
  564. {68, 26, 63},
  565. {52, 79, 25},
  566. {17, 14, 12},
  567. {222, 34, 30},
  568. {72, 16, 44},
  569. {58, 32, 12},
  570. {10, 7, 6}},
  571. // mv_joint_probs
  572. {32, 64, 96},
  573. // mv_sign_prob
  574. {128, 128},
  575. // mv_class_probs
  576. {{224, 144, 192, 168, 192, 176, 192, 198, 198, 245},
  577. {216, 128, 176, 160, 176, 176, 192, 198, 198, 208}},
  578. // mv_class0_bit_prob
  579. {216, 208},
  580. // mv_bits_prob
  581. {{136, 140, 148, 160, 176, 192, 224, 234, 234, 240},
  582. {136, 140, 148, 160, 176, 192, 224, 234, 234, 240}},
  583. // mv_class0_fr_probs
  584. {{{128, 128, 64}, {96, 112, 64}}, {{128, 128, 64}, {96, 112, 64}}},
  585. // mv_fr_probs
  586. {{64, 96, 64}, {64, 96, 64}},
  587. // mv_class0_hp_prob
  588. {160, 160},
  589. // mv_hp_prob
  590. {128, 128},
  591. };
  592. // Helper function for Vp9Parser::ReadTileInfo. Defined as
  593. // calc_min_log2_tile_cols in spec 6.2.14 Tile size calculation.
  594. int GetMinLog2TileCols(int sb64_cols) {
  595. const int kMaxTileWidthB64 = 64;
  596. int min_log2 = 0;
  597. while ((kMaxTileWidthB64 << min_log2) < sb64_cols)
  598. min_log2++;
  599. return min_log2;
  600. }
  601. // Helper function for Vp9Parser::ReadTileInfo. Defined as
  602. // calc_max_log2_tile_cols in spec 6.2.14 Tile size calculation.
  603. int GetMaxLog2TileCols(int sb64_cols) {
  604. const int kMinTileWidthB64 = 4;
  605. int max_log2 = 1;
  606. while ((sb64_cols >> max_log2) >= kMinTileWidthB64)
  607. max_log2++;
  608. return max_log2 - 1;
  609. }
  610. } // namespace
  611. Vp9UncompressedHeaderParser::Vp9UncompressedHeaderParser(
  612. Vp9Parser::Context* context)
  613. : context_(context) {}
  614. const Vp9FrameContext&
  615. Vp9UncompressedHeaderParser::GetVp9DefaultFrameContextForTesting() const {
  616. return kVp9DefaultFrameContext;
  617. }
  618. uint8_t Vp9UncompressedHeaderParser::ReadProfile() {
  619. uint8_t profile = 0;
  620. // LSB first.
  621. if (reader_.ReadBool())
  622. profile |= 1;
  623. if (reader_.ReadBool())
  624. profile |= 2;
  625. if (profile > 2 && reader_.ReadBool())
  626. profile += 1;
  627. return profile;
  628. }
  629. // 6.2.1 Frame sync syntax
  630. bool Vp9UncompressedHeaderParser::VerifySyncCode() {
  631. const int kSyncCode = 0x498342;
  632. if (reader_.ReadLiteral(8 * 3) != kSyncCode) {
  633. DVLOG(1) << "Invalid frame sync code";
  634. return false;
  635. }
  636. return true;
  637. }
  638. // 6.2.2 Color config syntax
  639. bool Vp9UncompressedHeaderParser::ReadColorConfig(Vp9FrameHeader* fhdr) {
  640. if (fhdr->profile == 2 || fhdr->profile == 3) {
  641. fhdr->bit_depth = reader_.ReadBool() ? 12 : 10;
  642. } else {
  643. fhdr->bit_depth = 8;
  644. }
  645. fhdr->color_space = static_cast<Vp9ColorSpace>(reader_.ReadLiteral(3));
  646. if (fhdr->color_space != Vp9ColorSpace::SRGB) {
  647. fhdr->color_range = reader_.ReadBool();
  648. if (fhdr->profile == 1 || fhdr->profile == 3) {
  649. fhdr->subsampling_x = reader_.ReadBool() ? 1 : 0;
  650. fhdr->subsampling_y = reader_.ReadBool() ? 1 : 0;
  651. if (fhdr->subsampling_x == 1 && fhdr->subsampling_y == 1) {
  652. DVLOG(1) << "4:2:0 color not supported in profile 1 or 3";
  653. return false;
  654. }
  655. bool reserved = reader_.ReadBool();
  656. if (reserved) {
  657. DVLOG(1) << "reserved bit set";
  658. return false;
  659. }
  660. } else {
  661. fhdr->subsampling_x = fhdr->subsampling_y = 1;
  662. }
  663. } else {
  664. fhdr->color_range = true;
  665. if (fhdr->profile == 1 || fhdr->profile == 3) {
  666. fhdr->subsampling_x = fhdr->subsampling_y = 0;
  667. bool reserved = reader_.ReadBool();
  668. if (reserved) {
  669. DVLOG(1) << "reserved bit set";
  670. return false;
  671. }
  672. } else {
  673. DVLOG(1) << "4:4:4 color not supported in profile 0 or 2";
  674. return false;
  675. }
  676. }
  677. return true;
  678. }
  679. // 6.2.3 Frame size syntax
  680. void Vp9UncompressedHeaderParser::ReadFrameSize(Vp9FrameHeader* fhdr) {
  681. fhdr->frame_width = reader_.ReadLiteral(16) + 1;
  682. fhdr->frame_height = reader_.ReadLiteral(16) + 1;
  683. }
  684. // 6.2.4 Render size syntax
  685. void Vp9UncompressedHeaderParser::ReadRenderSize(Vp9FrameHeader* fhdr) {
  686. if (reader_.ReadBool()) {
  687. fhdr->render_width = reader_.ReadLiteral(16) + 1;
  688. fhdr->render_height = reader_.ReadLiteral(16) + 1;
  689. } else {
  690. fhdr->render_width = fhdr->frame_width;
  691. fhdr->render_height = fhdr->frame_height;
  692. }
  693. }
  694. // 6.2.5 Frame size with refs syntax
  695. bool Vp9UncompressedHeaderParser::ReadFrameSizeFromRefs(Vp9FrameHeader* fhdr) {
  696. bool found_ref = false;
  697. for (const auto& idx : fhdr->ref_frame_idx) {
  698. found_ref = reader_.ReadBool();
  699. if (found_ref) {
  700. const Vp9Parser::ReferenceSlot& ref = context_->GetRefSlot(idx);
  701. DCHECK(ref.initialized);
  702. fhdr->frame_width = ref.frame_width;
  703. fhdr->frame_height = ref.frame_height;
  704. const unsigned kMaxDimension = 1u << 16;
  705. DCHECK_LE(fhdr->frame_width, kMaxDimension);
  706. DCHECK_LE(fhdr->frame_height, kMaxDimension);
  707. break;
  708. }
  709. }
  710. if (!found_ref)
  711. ReadFrameSize(fhdr);
  712. // 7.2.5 Frame size with refs semantics
  713. bool has_valid_ref_frame = false;
  714. for (const auto& idx : fhdr->ref_frame_idx) {
  715. const Vp9Parser::ReferenceSlot& ref = context_->GetRefSlot(idx);
  716. if (2 * fhdr->frame_width >= ref.frame_width &&
  717. 2 * fhdr->frame_height >= ref.frame_height &&
  718. fhdr->frame_width <= 16 * ref.frame_width &&
  719. fhdr->frame_height <= 16 * ref.frame_height) {
  720. has_valid_ref_frame = true;
  721. break;
  722. }
  723. }
  724. if (!has_valid_ref_frame) {
  725. DVLOG(1) << "There should be at least one reference frame meeting "
  726. << "size conditions.";
  727. return false;
  728. }
  729. ReadRenderSize(fhdr);
  730. return true;
  731. }
  732. // 6.2.7 Interpolation filter syntax
  733. Vp9InterpolationFilter Vp9UncompressedHeaderParser::ReadInterpolationFilter() {
  734. if (reader_.ReadBool())
  735. return Vp9InterpolationFilter::SWITCHABLE;
  736. // The mapping table for next two bits.
  737. const Vp9InterpolationFilter table[] = {
  738. Vp9InterpolationFilter::EIGHTTAP_SMOOTH, Vp9InterpolationFilter::EIGHTTAP,
  739. Vp9InterpolationFilter::EIGHTTAP_SHARP, Vp9InterpolationFilter::BILINEAR,
  740. };
  741. return table[reader_.ReadLiteral(2)];
  742. }
  743. void Vp9UncompressedHeaderParser::SetupPastIndependence(Vp9FrameHeader* fhdr) {
  744. memset(&context_->segmentation_, 0, sizeof(context_->segmentation_));
  745. memset(fhdr->ref_frame_sign_bias, 0, sizeof(fhdr->ref_frame_sign_bias));
  746. ResetLoopfilter();
  747. fhdr->frame_context = kVp9DefaultFrameContext;
  748. DCHECK(fhdr->frame_context.IsValid());
  749. }
  750. // 6.2.8 Loop filter params syntax
  751. void Vp9UncompressedHeaderParser::ReadLoopFilterParams() {
  752. Vp9LoopFilterParams& loop_filter = context_->loop_filter_;
  753. loop_filter.level = reader_.ReadLiteral(6);
  754. loop_filter.sharpness = reader_.ReadLiteral(3);
  755. loop_filter.delta_update = false;
  756. loop_filter.delta_enabled = reader_.ReadBool();
  757. if (loop_filter.delta_enabled) {
  758. loop_filter.delta_update = reader_.ReadBool();
  759. if (loop_filter.delta_update) {
  760. for (size_t i = 0; i < Vp9RefType::VP9_FRAME_MAX; i++) {
  761. loop_filter.update_ref_deltas[i] = reader_.ReadBool();
  762. if (loop_filter.update_ref_deltas[i])
  763. loop_filter.ref_deltas[i] = reader_.ReadSignedLiteral(6);
  764. }
  765. for (size_t i = 0; i < Vp9LoopFilterParams::kNumModeDeltas; i++) {
  766. loop_filter.update_mode_deltas[i] = reader_.ReadBool();
  767. if (loop_filter.update_mode_deltas[i])
  768. loop_filter.mode_deltas[i] = reader_.ReadSignedLiteral(6);
  769. }
  770. }
  771. }
  772. }
  773. // 6.2.9 Quantization params syntax
  774. void Vp9UncompressedHeaderParser::ReadQuantizationParams(
  775. Vp9QuantizationParams* quants) {
  776. quants->base_q_idx = reader_.ReadLiteral(8);
  777. quants->delta_q_y_dc = ReadDeltaQ();
  778. quants->delta_q_uv_dc = ReadDeltaQ();
  779. quants->delta_q_uv_ac = ReadDeltaQ();
  780. }
  781. // 6.2.10 Delta quantizer syntax
  782. int8_t Vp9UncompressedHeaderParser::ReadDeltaQ() {
  783. if (reader_.ReadBool())
  784. return reader_.ReadSignedLiteral(4);
  785. return 0;
  786. }
  787. // 6.2.11 Segmentation params syntax
  788. bool Vp9UncompressedHeaderParser::ReadSegmentationParams() {
  789. Vp9SegmentationParams& segmentation = context_->segmentation_;
  790. segmentation.update_map = false;
  791. segmentation.update_data = false;
  792. segmentation.enabled = reader_.ReadBool();
  793. if (!segmentation.enabled)
  794. return true;
  795. segmentation.update_map = reader_.ReadBool();
  796. if (segmentation.update_map) {
  797. for (auto& tree_prob : segmentation.tree_probs) {
  798. tree_prob = ReadProb();
  799. }
  800. segmentation.temporal_update = reader_.ReadBool();
  801. for (auto& pred_prob : segmentation.pred_probs) {
  802. pred_prob = segmentation.temporal_update ? ReadProb() : kVp9MaxProb;
  803. }
  804. }
  805. segmentation.update_data = reader_.ReadBool();
  806. if (segmentation.update_data) {
  807. segmentation.abs_or_delta_update = reader_.ReadBool();
  808. const int kFeatureDataBits[] = {8, 6, 2, 0};
  809. const bool kFeatureDataSigned[] = {true, true, false, false};
  810. for (size_t i = 0; i < Vp9SegmentationParams::kNumSegments; i++) {
  811. for (size_t j = 0; j < Vp9SegmentationParams::SEG_LVL_MAX; j++) {
  812. int16_t data = 0;
  813. segmentation.feature_enabled[i][j] = reader_.ReadBool();
  814. if (segmentation.feature_enabled[i][j]) {
  815. data = reader_.ReadLiteral(kFeatureDataBits[j]);
  816. if (kFeatureDataSigned[j])
  817. if (reader_.ReadBool()) {
  818. // 7.2.9
  819. if (segmentation.abs_or_delta_update) {
  820. DVLOG(1) << "feature_sign should be 0"
  821. << " if abs_or_delta_update is 1";
  822. return false;
  823. }
  824. data = -data;
  825. }
  826. }
  827. segmentation.feature_data[i][j] = data;
  828. }
  829. }
  830. }
  831. return true;
  832. }
  833. // 6.2.12 Probability syntax
  834. uint8_t Vp9UncompressedHeaderParser::ReadProb() {
  835. return reader_.ReadBool() ? reader_.ReadLiteral(8) : kVp9MaxProb;
  836. }
  837. // 6.2.13 Tile info syntax
  838. bool Vp9UncompressedHeaderParser::ReadTileInfo(Vp9FrameHeader* fhdr) {
  839. int sb64_cols = (fhdr->frame_width + 63) / 64;
  840. int min_log2_tile_cols = GetMinLog2TileCols(sb64_cols);
  841. int max_log2_tile_cols = GetMaxLog2TileCols(sb64_cols);
  842. int max_ones = max_log2_tile_cols - min_log2_tile_cols;
  843. fhdr->tile_cols_log2 = min_log2_tile_cols;
  844. while (max_ones-- && reader_.ReadBool())
  845. fhdr->tile_cols_log2++;
  846. fhdr->tile_rows_log2 = reader_.ReadBool() ? 1 : 0;
  847. if (fhdr->tile_rows_log2 > 0 && reader_.ReadBool())
  848. fhdr->tile_rows_log2++;
  849. // 7.2.11 Tile info semantics
  850. if (fhdr->tile_cols_log2 > 6) {
  851. DVLOG(1) << "tile_cols_log2 should be <= 6";
  852. return false;
  853. }
  854. return true;
  855. }
  856. void Vp9UncompressedHeaderParser::ResetLoopfilter() {
  857. Vp9LoopFilterParams& loop_filter = context_->loop_filter_;
  858. loop_filter.delta_enabled = true;
  859. loop_filter.delta_update = true;
  860. loop_filter.ref_deltas[VP9_FRAME_INTRA] = 1;
  861. loop_filter.ref_deltas[VP9_FRAME_LAST] = 0;
  862. loop_filter.ref_deltas[VP9_FRAME_GOLDEN] = -1;
  863. loop_filter.ref_deltas[VP9_FRAME_ALTREF] = -1;
  864. memset(loop_filter.mode_deltas, 0, sizeof(loop_filter.mode_deltas));
  865. }
  866. // 6.2 Uncompressed header syntax
  867. bool Vp9UncompressedHeaderParser::Parse(const uint8_t* stream,
  868. off_t frame_size,
  869. Vp9FrameHeader* fhdr) {
  870. DVLOG(2) << "Vp9UncompressedHeaderParser::Parse";
  871. reader_.Initialize(stream, frame_size);
  872. fhdr->data = stream;
  873. fhdr->frame_size = frame_size;
  874. // frame marker
  875. if (reader_.ReadLiteral(2) != 0x2) {
  876. DVLOG(1) << "frame marker shall be equal to 2";
  877. return false;
  878. }
  879. fhdr->profile = ReadProfile();
  880. if (fhdr->profile >= kVp9MaxProfile) {
  881. DVLOG(1) << "Unsupported bitstream profile";
  882. return false;
  883. }
  884. fhdr->show_existing_frame = reader_.ReadBool();
  885. if (fhdr->show_existing_frame) {
  886. fhdr->frame_to_show_map_idx = reader_.ReadLiteral(3);
  887. fhdr->show_frame = true;
  888. if (!reader_.ConsumeTrailingBits()) {
  889. DVLOG(1) << "trailing bits are not zero";
  890. return false;
  891. }
  892. if (!reader_.IsValid()) {
  893. DVLOG(1) << "parser reads beyond the end of buffer";
  894. return false;
  895. }
  896. fhdr->uncompressed_header_size = reader_.GetBytesRead();
  897. fhdr->header_size_in_bytes = 0;
  898. return true;
  899. }
  900. fhdr->frame_type = static_cast<Vp9FrameHeader::FrameType>(reader_.ReadBool());
  901. fhdr->show_frame = reader_.ReadBool();
  902. fhdr->error_resilient_mode = reader_.ReadBool();
  903. if (fhdr->IsKeyframe()) {
  904. if (!VerifySyncCode())
  905. return false;
  906. if (!ReadColorConfig(fhdr))
  907. return false;
  908. ReadFrameSize(fhdr);
  909. ReadRenderSize(fhdr);
  910. fhdr->refresh_frame_flags = 0xff;
  911. } else {
  912. if (!fhdr->show_frame)
  913. fhdr->intra_only = reader_.ReadBool();
  914. if (!fhdr->error_resilient_mode)
  915. fhdr->reset_frame_context = reader_.ReadLiteral(2);
  916. if (fhdr->intra_only) {
  917. if (!VerifySyncCode())
  918. return false;
  919. if (fhdr->profile > 0) {
  920. if (!ReadColorConfig(fhdr))
  921. return false;
  922. } else {
  923. fhdr->bit_depth = 8;
  924. fhdr->color_space = Vp9ColorSpace::BT_601;
  925. fhdr->subsampling_x = fhdr->subsampling_y = 1;
  926. }
  927. fhdr->refresh_frame_flags = reader_.ReadLiteral(8);
  928. ReadFrameSize(fhdr);
  929. ReadRenderSize(fhdr);
  930. } else {
  931. fhdr->refresh_frame_flags = reader_.ReadLiteral(8);
  932. static_assert(std::extent<decltype(fhdr->ref_frame_sign_bias)>() >=
  933. Vp9RefType::VP9_FRAME_LAST + kVp9NumRefsPerFrame,
  934. "ref_frame_sign_bias is not big enough");
  935. for (size_t i = 0; i < kVp9NumRefsPerFrame; i++) {
  936. fhdr->ref_frame_idx[i] = reader_.ReadLiteral(kVp9NumRefFramesLog2);
  937. fhdr->ref_frame_sign_bias[Vp9RefType::VP9_FRAME_LAST + i] =
  938. reader_.ReadBool();
  939. // 8.2 Frame order constraints
  940. // ref_frame_idx[i] refers to an earlier decoded frame.
  941. const Vp9Parser::ReferenceSlot& ref =
  942. context_->GetRefSlot(fhdr->ref_frame_idx[i]);
  943. if (!ref.initialized) {
  944. DVLOG(1) << "ref_frame_idx[" << i
  945. << "]=" << static_cast<int>(fhdr->ref_frame_idx[i])
  946. << " refers to unused frame";
  947. return false;
  948. }
  949. // 7.2 Uncompressed header semantics
  950. // the selected reference frames match the current frame in bit depth,
  951. // profile, chroma subsampling, and color space.
  952. if (ref.profile != fhdr->profile) {
  953. DVLOG(1) << "profile of referenced frame mismatch";
  954. return false;
  955. }
  956. if (i == 0) {
  957. // Below fields are not specified for inter-frame in header, so copy
  958. // them from referenced frame.
  959. fhdr->bit_depth = ref.bit_depth;
  960. fhdr->color_space = ref.color_space;
  961. fhdr->subsampling_x = ref.subsampling_x;
  962. fhdr->subsampling_y = ref.subsampling_y;
  963. } else {
  964. if (fhdr->bit_depth != ref.bit_depth) {
  965. DVLOG(1) << "bit_depth of referenced frame mismatch";
  966. return false;
  967. }
  968. // There are encoded streams with no color_space information
  969. // with in the frame header of an Intra only frame, so we assigned the
  970. // default color_space :BT_601 as per the spec. But reference list
  971. // might have frames with UNKNOWN color space information too. So we
  972. // relax the requirement a bit to cover more video samples and added
  973. // an exception for UNKNOWN colorspace
  974. if (fhdr->color_space != ref.color_space &&
  975. fhdr->color_space != Vp9ColorSpace::UNKNOWN &&
  976. ref.color_space != Vp9ColorSpace::UNKNOWN) {
  977. DVLOG(1) << "color_space of referenced frame mismatch";
  978. return false;
  979. }
  980. if (fhdr->subsampling_x != ref.subsampling_x ||
  981. fhdr->subsampling_y != ref.subsampling_y) {
  982. DVLOG(1) << "chroma subsampling of referenced frame mismatch";
  983. return false;
  984. }
  985. }
  986. }
  987. if (!ReadFrameSizeFromRefs(fhdr))
  988. return false;
  989. fhdr->allow_high_precision_mv = reader_.ReadBool();
  990. fhdr->interpolation_filter = ReadInterpolationFilter();
  991. }
  992. }
  993. if (fhdr->error_resilient_mode) {
  994. fhdr->refresh_frame_context = false;
  995. fhdr->frame_parallel_decoding_mode = true;
  996. } else {
  997. fhdr->refresh_frame_context = reader_.ReadBool();
  998. fhdr->frame_parallel_decoding_mode = reader_.ReadBool();
  999. }
  1000. fhdr->frame_context_idx_to_save_probs = fhdr->frame_context_idx =
  1001. reader_.ReadLiteral(kVp9NumFrameContextsLog2);
  1002. if (fhdr->IsIntra() || fhdr->error_resilient_mode) {
  1003. SetupPastIndependence(fhdr);
  1004. if (fhdr->IsKeyframe() || fhdr->error_resilient_mode ||
  1005. fhdr->reset_frame_context == 3) {
  1006. for (size_t i = 0; i < kVp9NumFrameContexts; ++i)
  1007. context_->UpdateFrameContext(i, fhdr->frame_context);
  1008. } else if (fhdr->reset_frame_context == 2) {
  1009. context_->UpdateFrameContext(fhdr->frame_context_idx,
  1010. fhdr->frame_context);
  1011. }
  1012. fhdr->frame_context_idx = 0;
  1013. }
  1014. ReadLoopFilterParams();
  1015. // Update loop_filter in current_frame_hdr
  1016. fhdr->loop_filter = context_->loop_filter_;
  1017. ReadQuantizationParams(&fhdr->quant_params);
  1018. if (!ReadSegmentationParams())
  1019. return false;
  1020. // Update segmentation in current_frame_hdr
  1021. fhdr->segmentation = context_->segmentation_;
  1022. if (!ReadTileInfo(fhdr))
  1023. return false;
  1024. fhdr->header_size_in_bytes = reader_.ReadLiteral(16);
  1025. if (fhdr->header_size_in_bytes == 0) {
  1026. DVLOG(1) << "invalid header size";
  1027. return false;
  1028. }
  1029. if (!reader_.ConsumeTrailingBits()) {
  1030. DVLOG(1) << "trailing bits are not zero";
  1031. return false;
  1032. }
  1033. if (!reader_.IsValid()) {
  1034. DVLOG(1) << "parser reads beyond the end of buffer";
  1035. return false;
  1036. }
  1037. fhdr->uncompressed_header_size = reader_.GetBytesRead();
  1038. return true;
  1039. }
  1040. } // namespace media