vp9_decodeframe.c 82 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271
  1. /*
  2. * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <assert.h>
  11. #include <stdlib.h> // qsort()
  12. #include "./vp9_rtcd.h"
  13. #include "./vpx_dsp_rtcd.h"
  14. #include "./vpx_scale_rtcd.h"
  15. #include "vpx_dsp/bitreader_buffer.h"
  16. #include "vpx_dsp/bitreader.h"
  17. #include "vpx_dsp/vpx_dsp_common.h"
  18. #include "vpx_mem/vpx_mem.h"
  19. #include "vpx_ports/mem.h"
  20. #include "vpx_ports/mem_ops.h"
  21. #include "vpx_scale/vpx_scale.h"
  22. #include "vpx_util/vpx_thread.h"
  23. #include "vp9/common/vp9_alloccommon.h"
  24. #include "vp9/common/vp9_common.h"
  25. #include "vp9/common/vp9_entropy.h"
  26. #include "vp9/common/vp9_entropymode.h"
  27. #include "vp9/common/vp9_idct.h"
  28. #include "vp9/common/vp9_thread_common.h"
  29. #include "vp9/common/vp9_pred_common.h"
  30. #include "vp9/common/vp9_quant_common.h"
  31. #include "vp9/common/vp9_reconintra.h"
  32. #include "vp9/common/vp9_reconinter.h"
  33. #include "vp9/common/vp9_seg_common.h"
  34. #include "vp9/common/vp9_tile_common.h"
  35. #include "vp9/decoder/vp9_decodeframe.h"
  36. #include "vp9/decoder/vp9_detokenize.h"
  37. #include "vp9/decoder/vp9_decodemv.h"
  38. #include "vp9/decoder/vp9_decoder.h"
  39. #include "vp9/decoder/vp9_dsubexp.h"
  40. #define MAX_VP9_HEADER_SIZE 80
  41. static int is_compound_reference_allowed(const VP9_COMMON *cm) {
  42. int i;
  43. for (i = 1; i < REFS_PER_FRAME; ++i)
  44. if (cm->ref_frame_sign_bias[i + 1] != cm->ref_frame_sign_bias[1])
  45. return 1;
  46. return 0;
  47. }
  48. static void setup_compound_reference_mode(VP9_COMMON *cm) {
  49. if (cm->ref_frame_sign_bias[LAST_FRAME] ==
  50. cm->ref_frame_sign_bias[GOLDEN_FRAME]) {
  51. cm->comp_fixed_ref = ALTREF_FRAME;
  52. cm->comp_var_ref[0] = LAST_FRAME;
  53. cm->comp_var_ref[1] = GOLDEN_FRAME;
  54. } else if (cm->ref_frame_sign_bias[LAST_FRAME] ==
  55. cm->ref_frame_sign_bias[ALTREF_FRAME]) {
  56. cm->comp_fixed_ref = GOLDEN_FRAME;
  57. cm->comp_var_ref[0] = LAST_FRAME;
  58. cm->comp_var_ref[1] = ALTREF_FRAME;
  59. } else {
  60. cm->comp_fixed_ref = LAST_FRAME;
  61. cm->comp_var_ref[0] = GOLDEN_FRAME;
  62. cm->comp_var_ref[1] = ALTREF_FRAME;
  63. }
  64. }
  65. static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
  66. return len != 0 && len <= (size_t)(end - start);
  67. }
  68. static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
  69. const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
  70. return data > max ? max : data;
  71. }
  72. static TX_MODE read_tx_mode(vpx_reader *r) {
  73. TX_MODE tx_mode = vpx_read_literal(r, 2);
  74. if (tx_mode == ALLOW_32X32)
  75. tx_mode += vpx_read_bit(r);
  76. return tx_mode;
  77. }
  78. static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
  79. int i, j;
  80. for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
  81. for (j = 0; j < TX_SIZES - 3; ++j)
  82. vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
  83. for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
  84. for (j = 0; j < TX_SIZES - 2; ++j)
  85. vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
  86. for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
  87. for (j = 0; j < TX_SIZES - 1; ++j)
  88. vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
  89. }
  90. static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
  91. int i, j;
  92. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
  93. for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
  94. vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
  95. }
  96. static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
  97. int i, j;
  98. for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
  99. for (j = 0; j < INTER_MODES - 1; ++j)
  100. vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
  101. }
  102. static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
  103. vpx_reader *r) {
  104. if (is_compound_reference_allowed(cm)) {
  105. return vpx_read_bit(r) ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT
  106. : COMPOUND_REFERENCE)
  107. : SINGLE_REFERENCE;
  108. } else {
  109. return SINGLE_REFERENCE;
  110. }
  111. }
  112. static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
  113. FRAME_CONTEXT *const fc = cm->fc;
  114. int i;
  115. if (cm->reference_mode == REFERENCE_MODE_SELECT)
  116. for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
  117. vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
  118. if (cm->reference_mode != COMPOUND_REFERENCE)
  119. for (i = 0; i < REF_CONTEXTS; ++i) {
  120. vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
  121. vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
  122. }
  123. if (cm->reference_mode != SINGLE_REFERENCE)
  124. for (i = 0; i < REF_CONTEXTS; ++i)
  125. vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
  126. }
  127. static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
  128. int i;
  129. for (i = 0; i < n; ++i)
  130. if (vpx_read(r, MV_UPDATE_PROB))
  131. p[i] = (vpx_read_literal(r, 7) << 1) | 1;
  132. }
  133. static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
  134. int i, j;
  135. update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
  136. for (i = 0; i < 2; ++i) {
  137. nmv_component *const comp_ctx = &ctx->comps[i];
  138. update_mv_probs(&comp_ctx->sign, 1, r);
  139. update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
  140. update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
  141. update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
  142. }
  143. for (i = 0; i < 2; ++i) {
  144. nmv_component *const comp_ctx = &ctx->comps[i];
  145. for (j = 0; j < CLASS0_SIZE; ++j)
  146. update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
  147. update_mv_probs(comp_ctx->fp, 3, r);
  148. }
  149. if (allow_hp) {
  150. for (i = 0; i < 2; ++i) {
  151. nmv_component *const comp_ctx = &ctx->comps[i];
  152. update_mv_probs(&comp_ctx->class0_hp, 1, r);
  153. update_mv_probs(&comp_ctx->hp, 1, r);
  154. }
  155. }
  156. }
  157. static void inverse_transform_block_inter(MACROBLOCKD* xd, int plane,
  158. const TX_SIZE tx_size,
  159. uint8_t *dst, int stride,
  160. int eob) {
  161. struct macroblockd_plane *const pd = &xd->plane[plane];
  162. tran_low_t *const dqcoeff = pd->dqcoeff;
  163. assert(eob > 0);
  164. #if CONFIG_VP9_HIGHBITDEPTH
  165. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  166. if (xd->lossless) {
  167. vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
  168. } else {
  169. switch (tx_size) {
  170. case TX_4X4:
  171. vp9_highbd_idct4x4_add(dqcoeff, dst, stride, eob, xd->bd);
  172. break;
  173. case TX_8X8:
  174. vp9_highbd_idct8x8_add(dqcoeff, dst, stride, eob, xd->bd);
  175. break;
  176. case TX_16X16:
  177. vp9_highbd_idct16x16_add(dqcoeff, dst, stride, eob, xd->bd);
  178. break;
  179. case TX_32X32:
  180. vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
  181. break;
  182. default:
  183. assert(0 && "Invalid transform size");
  184. }
  185. }
  186. } else {
  187. if (xd->lossless) {
  188. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  189. } else {
  190. switch (tx_size) {
  191. case TX_4X4:
  192. vp9_idct4x4_add(dqcoeff, dst, stride, eob);
  193. break;
  194. case TX_8X8:
  195. vp9_idct8x8_add(dqcoeff, dst, stride, eob);
  196. break;
  197. case TX_16X16:
  198. vp9_idct16x16_add(dqcoeff, dst, stride, eob);
  199. break;
  200. case TX_32X32:
  201. vp9_idct32x32_add(dqcoeff, dst, stride, eob);
  202. break;
  203. default:
  204. assert(0 && "Invalid transform size");
  205. return;
  206. }
  207. }
  208. }
  209. #else
  210. if (xd->lossless) {
  211. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  212. } else {
  213. switch (tx_size) {
  214. case TX_4X4:
  215. vp9_idct4x4_add(dqcoeff, dst, stride, eob);
  216. break;
  217. case TX_8X8:
  218. vp9_idct8x8_add(dqcoeff, dst, stride, eob);
  219. break;
  220. case TX_16X16:
  221. vp9_idct16x16_add(dqcoeff, dst, stride, eob);
  222. break;
  223. case TX_32X32:
  224. vp9_idct32x32_add(dqcoeff, dst, stride, eob);
  225. break;
  226. default:
  227. assert(0 && "Invalid transform size");
  228. return;
  229. }
  230. }
  231. #endif // CONFIG_VP9_HIGHBITDEPTH
  232. if (eob == 1) {
  233. dqcoeff[0] = 0;
  234. } else {
  235. if (tx_size <= TX_16X16 && eob <= 10)
  236. memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
  237. else if (tx_size == TX_32X32 && eob <= 34)
  238. memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
  239. else
  240. memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
  241. }
  242. }
  243. static void inverse_transform_block_intra(MACROBLOCKD* xd, int plane,
  244. const TX_TYPE tx_type,
  245. const TX_SIZE tx_size,
  246. uint8_t *dst, int stride,
  247. int eob) {
  248. struct macroblockd_plane *const pd = &xd->plane[plane];
  249. tran_low_t *const dqcoeff = pd->dqcoeff;
  250. assert(eob > 0);
  251. #if CONFIG_VP9_HIGHBITDEPTH
  252. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  253. if (xd->lossless) {
  254. vp9_highbd_iwht4x4_add(dqcoeff, dst, stride, eob, xd->bd);
  255. } else {
  256. switch (tx_size) {
  257. case TX_4X4:
  258. vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
  259. break;
  260. case TX_8X8:
  261. vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
  262. break;
  263. case TX_16X16:
  264. vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst, stride, eob, xd->bd);
  265. break;
  266. case TX_32X32:
  267. vp9_highbd_idct32x32_add(dqcoeff, dst, stride, eob, xd->bd);
  268. break;
  269. default:
  270. assert(0 && "Invalid transform size");
  271. }
  272. }
  273. } else {
  274. if (xd->lossless) {
  275. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  276. } else {
  277. switch (tx_size) {
  278. case TX_4X4:
  279. vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob);
  280. break;
  281. case TX_8X8:
  282. vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
  283. break;
  284. case TX_16X16:
  285. vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
  286. break;
  287. case TX_32X32:
  288. vp9_idct32x32_add(dqcoeff, dst, stride, eob);
  289. break;
  290. default:
  291. assert(0 && "Invalid transform size");
  292. return;
  293. }
  294. }
  295. }
  296. #else
  297. if (xd->lossless) {
  298. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  299. } else {
  300. switch (tx_size) {
  301. case TX_4X4:
  302. vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob);
  303. break;
  304. case TX_8X8:
  305. vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob);
  306. break;
  307. case TX_16X16:
  308. vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
  309. break;
  310. case TX_32X32:
  311. vp9_idct32x32_add(dqcoeff, dst, stride, eob);
  312. break;
  313. default:
  314. assert(0 && "Invalid transform size");
  315. return;
  316. }
  317. }
  318. #endif // CONFIG_VP9_HIGHBITDEPTH
  319. if (eob == 1) {
  320. dqcoeff[0] = 0;
  321. } else {
  322. if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
  323. memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
  324. else if (tx_size == TX_32X32 && eob <= 34)
  325. memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
  326. else
  327. memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
  328. }
  329. }
  330. static void predict_and_reconstruct_intra_block(MACROBLOCKD *const xd,
  331. vpx_reader *r,
  332. MODE_INFO *const mi,
  333. int plane,
  334. int row, int col,
  335. TX_SIZE tx_size) {
  336. struct macroblockd_plane *const pd = &xd->plane[plane];
  337. PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
  338. uint8_t *dst;
  339. dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
  340. if (mi->sb_type < BLOCK_8X8)
  341. if (plane == 0)
  342. mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
  343. vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode,
  344. dst, pd->dst.stride, dst, pd->dst.stride,
  345. col, row, plane);
  346. if (!mi->skip) {
  347. const TX_TYPE tx_type = (plane || xd->lossless) ?
  348. DCT_DCT : intra_mode_to_tx_type_lookup[mode];
  349. const scan_order *sc = (plane || xd->lossless) ?
  350. &vp9_default_scan_orders[tx_size] : &vp9_scan_orders[tx_size][tx_type];
  351. const int eob = vp9_decode_block_tokens(xd, plane, sc, col, row, tx_size,
  352. r, mi->segment_id);
  353. if (eob > 0) {
  354. inverse_transform_block_intra(xd, plane, tx_type, tx_size,
  355. dst, pd->dst.stride, eob);
  356. }
  357. }
  358. }
  359. static int reconstruct_inter_block(MACROBLOCKD *const xd, vpx_reader *r,
  360. MODE_INFO *const mi, int plane,
  361. int row, int col, TX_SIZE tx_size) {
  362. struct macroblockd_plane *const pd = &xd->plane[plane];
  363. const scan_order *sc = &vp9_default_scan_orders[tx_size];
  364. const int eob = vp9_decode_block_tokens(xd, plane, sc, col, row, tx_size, r,
  365. mi->segment_id);
  366. if (eob > 0) {
  367. inverse_transform_block_inter(
  368. xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
  369. pd->dst.stride, eob);
  370. }
  371. return eob;
  372. }
  373. static void build_mc_border(const uint8_t *src, int src_stride,
  374. uint8_t *dst, int dst_stride,
  375. int x, int y, int b_w, int b_h, int w, int h) {
  376. // Get a pointer to the start of the real data for this row.
  377. const uint8_t *ref_row = src - x - y * src_stride;
  378. if (y >= h)
  379. ref_row += (h - 1) * src_stride;
  380. else if (y > 0)
  381. ref_row += y * src_stride;
  382. do {
  383. int right = 0, copy;
  384. int left = x < 0 ? -x : 0;
  385. if (left > b_w)
  386. left = b_w;
  387. if (x + b_w > w)
  388. right = x + b_w - w;
  389. if (right > b_w)
  390. right = b_w;
  391. copy = b_w - left - right;
  392. if (left)
  393. memset(dst, ref_row[0], left);
  394. if (copy)
  395. memcpy(dst + left, ref_row + x + left, copy);
  396. if (right)
  397. memset(dst + left + copy, ref_row[w - 1], right);
  398. dst += dst_stride;
  399. ++y;
  400. if (y > 0 && y < h)
  401. ref_row += src_stride;
  402. } while (--b_h);
  403. }
  404. #if CONFIG_VP9_HIGHBITDEPTH
  405. static void high_build_mc_border(const uint8_t *src8, int src_stride,
  406. uint16_t *dst, int dst_stride,
  407. int x, int y, int b_w, int b_h,
  408. int w, int h) {
  409. // Get a pointer to the start of the real data for this row.
  410. const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  411. const uint16_t *ref_row = src - x - y * src_stride;
  412. if (y >= h)
  413. ref_row += (h - 1) * src_stride;
  414. else if (y > 0)
  415. ref_row += y * src_stride;
  416. do {
  417. int right = 0, copy;
  418. int left = x < 0 ? -x : 0;
  419. if (left > b_w)
  420. left = b_w;
  421. if (x + b_w > w)
  422. right = x + b_w - w;
  423. if (right > b_w)
  424. right = b_w;
  425. copy = b_w - left - right;
  426. if (left)
  427. vpx_memset16(dst, ref_row[0], left);
  428. if (copy)
  429. memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
  430. if (right)
  431. vpx_memset16(dst + left + copy, ref_row[w - 1], right);
  432. dst += dst_stride;
  433. ++y;
  434. if (y > 0 && y < h)
  435. ref_row += src_stride;
  436. } while (--b_h);
  437. }
  438. #endif // CONFIG_VP9_HIGHBITDEPTH
  439. #if CONFIG_VP9_HIGHBITDEPTH
  440. static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
  441. int x0, int y0, int b_w, int b_h,
  442. int frame_width, int frame_height,
  443. int border_offset,
  444. uint8_t *const dst, int dst_buf_stride,
  445. int subpel_x, int subpel_y,
  446. const InterpKernel *kernel,
  447. const struct scale_factors *sf,
  448. MACROBLOCKD *xd,
  449. int w, int h, int ref, int xs, int ys) {
  450. DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
  451. const uint8_t *buf_ptr;
  452. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  453. high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w,
  454. x0, y0, b_w, b_h, frame_width, frame_height);
  455. buf_ptr = CONVERT_TO_BYTEPTR(mc_buf_high) + border_offset;
  456. } else {
  457. build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w,
  458. x0, y0, b_w, b_h, frame_width, frame_height);
  459. buf_ptr = ((uint8_t *)mc_buf_high) + border_offset;
  460. }
  461. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  462. highbd_inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
  463. subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
  464. } else {
  465. inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
  466. subpel_y, sf, w, h, ref, kernel, xs, ys);
  467. }
  468. }
  469. #else
  470. static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
  471. int x0, int y0, int b_w, int b_h,
  472. int frame_width, int frame_height,
  473. int border_offset,
  474. uint8_t *const dst, int dst_buf_stride,
  475. int subpel_x, int subpel_y,
  476. const InterpKernel *kernel,
  477. const struct scale_factors *sf,
  478. int w, int h, int ref, int xs, int ys) {
  479. DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
  480. const uint8_t *buf_ptr;
  481. build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w,
  482. x0, y0, b_w, b_h, frame_width, frame_height);
  483. buf_ptr = mc_buf + border_offset;
  484. inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x,
  485. subpel_y, sf, w, h, ref, kernel, xs, ys);
  486. }
  487. #endif // CONFIG_VP9_HIGHBITDEPTH
  488. static void dec_build_inter_predictors(VPxWorker *const worker, MACROBLOCKD *xd,
  489. int plane, int bw, int bh, int x,
  490. int y, int w, int h, int mi_x, int mi_y,
  491. const InterpKernel *kernel,
  492. const struct scale_factors *sf,
  493. struct buf_2d *pre_buf,
  494. struct buf_2d *dst_buf, const MV* mv,
  495. RefCntBuffer *ref_frame_buf,
  496. int is_scaled, int ref) {
  497. struct macroblockd_plane *const pd = &xd->plane[plane];
  498. uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
  499. MV32 scaled_mv;
  500. int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height,
  501. buf_stride, subpel_x, subpel_y;
  502. uint8_t *ref_frame, *buf_ptr;
  503. // Get reference frame pointer, width and height.
  504. if (plane == 0) {
  505. frame_width = ref_frame_buf->buf.y_crop_width;
  506. frame_height = ref_frame_buf->buf.y_crop_height;
  507. ref_frame = ref_frame_buf->buf.y_buffer;
  508. } else {
  509. frame_width = ref_frame_buf->buf.uv_crop_width;
  510. frame_height = ref_frame_buf->buf.uv_crop_height;
  511. ref_frame = plane == 1 ? ref_frame_buf->buf.u_buffer
  512. : ref_frame_buf->buf.v_buffer;
  513. }
  514. if (is_scaled) {
  515. const MV mv_q4 = clamp_mv_to_umv_border_sb(xd, mv, bw, bh,
  516. pd->subsampling_x,
  517. pd->subsampling_y);
  518. // Co-ordinate of containing block to pixel precision.
  519. int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
  520. int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
  521. #if CONFIG_BETTER_HW_COMPATIBILITY
  522. assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
  523. xd->mi[0]->sb_type != BLOCK_8X4);
  524. assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) &&
  525. mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x)));
  526. #endif
  527. // Co-ordinate of the block to 1/16th pixel precision.
  528. x0_16 = (x_start + x) << SUBPEL_BITS;
  529. y0_16 = (y_start + y) << SUBPEL_BITS;
  530. // Co-ordinate of current block in reference frame
  531. // to 1/16th pixel precision.
  532. x0_16 = sf->scale_value_x(x0_16, sf);
  533. y0_16 = sf->scale_value_y(y0_16, sf);
  534. // Map the top left corner of the block into the reference frame.
  535. x0 = sf->scale_value_x(x_start + x, sf);
  536. y0 = sf->scale_value_y(y_start + y, sf);
  537. // Scale the MV and incorporate the sub-pixel offset of the block
  538. // in the reference frame.
  539. scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
  540. xs = sf->x_step_q4;
  541. ys = sf->y_step_q4;
  542. } else {
  543. // Co-ordinate of containing block to pixel precision.
  544. x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
  545. y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
  546. // Co-ordinate of the block to 1/16th pixel precision.
  547. x0_16 = x0 << SUBPEL_BITS;
  548. y0_16 = y0 << SUBPEL_BITS;
  549. scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
  550. scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
  551. xs = ys = 16;
  552. }
  553. subpel_x = scaled_mv.col & SUBPEL_MASK;
  554. subpel_y = scaled_mv.row & SUBPEL_MASK;
  555. // Calculate the top left corner of the best matching block in the
  556. // reference frame.
  557. x0 += scaled_mv.col >> SUBPEL_BITS;
  558. y0 += scaled_mv.row >> SUBPEL_BITS;
  559. x0_16 += scaled_mv.col;
  560. y0_16 += scaled_mv.row;
  561. // Get reference block pointer.
  562. buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
  563. buf_stride = pre_buf->stride;
  564. // Do border extension if there is motion or the
  565. // width/height is not a multiple of 8 pixels.
  566. if (is_scaled || scaled_mv.col || scaled_mv.row ||
  567. (frame_width & 0x7) || (frame_height & 0x7)) {
  568. int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
  569. // Get reference block bottom right horizontal coordinate.
  570. int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
  571. int x_pad = 0, y_pad = 0;
  572. if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
  573. x0 -= VP9_INTERP_EXTEND - 1;
  574. x1 += VP9_INTERP_EXTEND;
  575. x_pad = 1;
  576. }
  577. if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
  578. y0 -= VP9_INTERP_EXTEND - 1;
  579. y1 += VP9_INTERP_EXTEND;
  580. y_pad = 1;
  581. }
  582. // Wait until reference block is ready. Pad 7 more pixels as last 7
  583. // pixels of each superblock row can be changed by next superblock row.
  584. if (worker != NULL)
  585. vp9_frameworker_wait(worker, ref_frame_buf,
  586. VPXMAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
  587. // Skip border extension if block is inside the frame.
  588. if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
  589. y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
  590. // Extend the border.
  591. const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
  592. const int b_w = x1 - x0 + 1;
  593. const int b_h = y1 - y0 + 1;
  594. const int border_offset = y_pad * 3 * b_w + x_pad * 3;
  595. extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h,
  596. frame_width, frame_height, border_offset,
  597. dst, dst_buf->stride,
  598. subpel_x, subpel_y,
  599. kernel, sf,
  600. #if CONFIG_VP9_HIGHBITDEPTH
  601. xd,
  602. #endif
  603. w, h, ref, xs, ys);
  604. return;
  605. }
  606. } else {
  607. // Wait until reference block is ready. Pad 7 more pixels as last 7
  608. // pixels of each superblock row can be changed by next superblock row.
  609. if (worker != NULL) {
  610. const int y1 = (y0_16 + (h - 1) * ys) >> SUBPEL_BITS;
  611. vp9_frameworker_wait(worker, ref_frame_buf,
  612. VPXMAX(0, (y1 + 7)) << (plane == 0 ? 0 : 1));
  613. }
  614. }
  615. #if CONFIG_VP9_HIGHBITDEPTH
  616. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  617. highbd_inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
  618. subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
  619. } else {
  620. inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
  621. subpel_y, sf, w, h, ref, kernel, xs, ys);
  622. }
  623. #else
  624. inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
  625. subpel_y, sf, w, h, ref, kernel, xs, ys);
  626. #endif // CONFIG_VP9_HIGHBITDEPTH
  627. }
  628. static void dec_build_inter_predictors_sb(VP9Decoder *const pbi,
  629. MACROBLOCKD *xd,
  630. int mi_row, int mi_col) {
  631. int plane;
  632. const int mi_x = mi_col * MI_SIZE;
  633. const int mi_y = mi_row * MI_SIZE;
  634. const MODE_INFO *mi = xd->mi[0];
  635. const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
  636. const BLOCK_SIZE sb_type = mi->sb_type;
  637. const int is_compound = has_second_ref(mi);
  638. int ref;
  639. int is_scaled;
  640. VPxWorker *const fwo = pbi->frame_parallel_decode ?
  641. pbi->frame_worker_owner : NULL;
  642. for (ref = 0; ref < 1 + is_compound; ++ref) {
  643. const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
  644. RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
  645. const struct scale_factors *const sf = &ref_buf->sf;
  646. const int idx = ref_buf->idx;
  647. BufferPool *const pool = pbi->common.buffer_pool;
  648. RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
  649. if (!vp9_is_valid_scale(sf))
  650. vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
  651. "Reference frame has invalid dimensions");
  652. is_scaled = vp9_is_scaled(sf);
  653. vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
  654. is_scaled ? sf : NULL);
  655. xd->block_refs[ref] = ref_buf;
  656. if (sb_type < BLOCK_8X8) {
  657. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  658. struct macroblockd_plane *const pd = &xd->plane[plane];
  659. struct buf_2d *const dst_buf = &pd->dst;
  660. const int num_4x4_w = pd->n4_w;
  661. const int num_4x4_h = pd->n4_h;
  662. const int n4w_x4 = 4 * num_4x4_w;
  663. const int n4h_x4 = 4 * num_4x4_h;
  664. struct buf_2d *const pre_buf = &pd->pre[ref];
  665. int i = 0, x, y;
  666. for (y = 0; y < num_4x4_h; ++y) {
  667. for (x = 0; x < num_4x4_w; ++x) {
  668. const MV mv = average_split_mvs(pd, mi, ref, i++);
  669. dec_build_inter_predictors(fwo, xd, plane, n4w_x4, n4h_x4,
  670. 4 * x, 4 * y, 4, 4, mi_x, mi_y, kernel,
  671. sf, pre_buf, dst_buf, &mv,
  672. ref_frame_buf, is_scaled, ref);
  673. }
  674. }
  675. }
  676. } else {
  677. const MV mv = mi->mv[ref].as_mv;
  678. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  679. struct macroblockd_plane *const pd = &xd->plane[plane];
  680. struct buf_2d *const dst_buf = &pd->dst;
  681. const int num_4x4_w = pd->n4_w;
  682. const int num_4x4_h = pd->n4_h;
  683. const int n4w_x4 = 4 * num_4x4_w;
  684. const int n4h_x4 = 4 * num_4x4_h;
  685. struct buf_2d *const pre_buf = &pd->pre[ref];
  686. dec_build_inter_predictors(fwo, xd, plane, n4w_x4, n4h_x4,
  687. 0, 0, n4w_x4, n4h_x4, mi_x, mi_y, kernel,
  688. sf, pre_buf, dst_buf, &mv,
  689. ref_frame_buf, is_scaled, ref);
  690. }
  691. }
  692. }
  693. }
  694. static INLINE TX_SIZE dec_get_uv_tx_size(const MODE_INFO *mi,
  695. int n4_wl, int n4_hl) {
  696. // get minimum log2 num4x4s dimension
  697. const int x = VPXMIN(n4_wl, n4_hl);
  698. return VPXMIN(mi->tx_size, x);
  699. }
  700. static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
  701. int i;
  702. for (i = 0; i < MAX_MB_PLANE; i++) {
  703. struct macroblockd_plane *const pd = &xd->plane[i];
  704. memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
  705. memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
  706. }
  707. }
  708. static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
  709. int bhl) {
  710. int i;
  711. for (i = 0; i < MAX_MB_PLANE; i++) {
  712. xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
  713. xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
  714. xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
  715. xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
  716. }
  717. }
  718. static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  719. BLOCK_SIZE bsize, int mi_row, int mi_col,
  720. int bw, int bh, int x_mis, int y_mis,
  721. int bwl, int bhl) {
  722. const int offset = mi_row * cm->mi_stride + mi_col;
  723. int x, y;
  724. const TileInfo *const tile = &xd->tile;
  725. xd->mi = cm->mi_grid_visible + offset;
  726. xd->mi[0] = &cm->mi[offset];
  727. // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
  728. // passing bsize from decode_partition().
  729. xd->mi[0]->sb_type = bsize;
  730. for (y = 0; y < y_mis; ++y)
  731. for (x = !y; x < x_mis; ++x) {
  732. xd->mi[y * cm->mi_stride + x] = xd->mi[0];
  733. }
  734. set_plane_n4(xd, bw, bh, bwl, bhl);
  735. set_skip_context(xd, mi_row, mi_col);
  736. // Distance of Mb to the various image edges. These are specified to 8th pel
  737. // as they are always compared to values that are in 1/8th pel units
  738. set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
  739. vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
  740. return xd->mi[0];
  741. }
  742. static void decode_block(VP9Decoder *const pbi, MACROBLOCKD *const xd,
  743. int mi_row, int mi_col,
  744. vpx_reader *r, BLOCK_SIZE bsize,
  745. int bwl, int bhl) {
  746. VP9_COMMON *const cm = &pbi->common;
  747. const int less8x8 = bsize < BLOCK_8X8;
  748. const int bw = 1 << (bwl - 1);
  749. const int bh = 1 << (bhl - 1);
  750. const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
  751. const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
  752. MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col,
  753. bw, bh, x_mis, y_mis, bwl, bhl);
  754. if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
  755. const BLOCK_SIZE uv_subsize =
  756. ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
  757. if (uv_subsize == BLOCK_INVALID)
  758. vpx_internal_error(xd->error_info,
  759. VPX_CODEC_CORRUPT_FRAME, "Invalid block size.");
  760. }
  761. vp9_read_mode_info(pbi, xd, mi_row, mi_col, r, x_mis, y_mis);
  762. if (mi->skip) {
  763. dec_reset_skip_context(xd);
  764. }
  765. if (!is_inter_block(mi)) {
  766. int plane;
  767. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  768. const struct macroblockd_plane *const pd = &xd->plane[plane];
  769. const TX_SIZE tx_size =
  770. plane ? dec_get_uv_tx_size(mi, pd->n4_wl, pd->n4_hl)
  771. : mi->tx_size;
  772. const int num_4x4_w = pd->n4_w;
  773. const int num_4x4_h = pd->n4_h;
  774. const int step = (1 << tx_size);
  775. int row, col;
  776. const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ?
  777. 0 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  778. const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ?
  779. 0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  780. xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
  781. xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
  782. for (row = 0; row < max_blocks_high; row += step)
  783. for (col = 0; col < max_blocks_wide; col += step)
  784. predict_and_reconstruct_intra_block(xd, r, mi, plane,
  785. row, col, tx_size);
  786. }
  787. } else {
  788. // Prediction
  789. dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
  790. // Reconstruction
  791. if (!mi->skip) {
  792. int eobtotal = 0;
  793. int plane;
  794. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  795. const struct macroblockd_plane *const pd = &xd->plane[plane];
  796. const TX_SIZE tx_size =
  797. plane ? dec_get_uv_tx_size(mi, pd->n4_wl, pd->n4_hl)
  798. : mi->tx_size;
  799. const int num_4x4_w = pd->n4_w;
  800. const int num_4x4_h = pd->n4_h;
  801. const int step = (1 << tx_size);
  802. int row, col;
  803. const int max_blocks_wide = num_4x4_w + (xd->mb_to_right_edge >= 0 ?
  804. 0 : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  805. const int max_blocks_high = num_4x4_h + (xd->mb_to_bottom_edge >= 0 ?
  806. 0 : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  807. xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
  808. xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
  809. for (row = 0; row < max_blocks_high; row += step)
  810. for (col = 0; col < max_blocks_wide; col += step)
  811. eobtotal += reconstruct_inter_block(xd, r, mi, plane, row, col,
  812. tx_size);
  813. }
  814. if (!less8x8 && eobtotal == 0)
  815. mi->skip = 1; // skip loopfilter
  816. }
  817. }
  818. xd->corrupted |= vpx_reader_has_error(r);
  819. if (cm->lf.filter_level) {
  820. vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
  821. }
  822. }
  823. static INLINE int dec_partition_plane_context(const MACROBLOCKD *xd,
  824. int mi_row, int mi_col,
  825. int bsl) {
  826. const PARTITION_CONTEXT *above_ctx = xd->above_seg_context + mi_col;
  827. const PARTITION_CONTEXT *left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
  828. int above = (*above_ctx >> bsl) & 1 , left = (*left_ctx >> bsl) & 1;
  829. // assert(bsl >= 0);
  830. return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
  831. }
  832. static INLINE void dec_update_partition_context(MACROBLOCKD *xd,
  833. int mi_row, int mi_col,
  834. BLOCK_SIZE subsize,
  835. int bw) {
  836. PARTITION_CONTEXT *const above_ctx = xd->above_seg_context + mi_col;
  837. PARTITION_CONTEXT *const left_ctx = xd->left_seg_context + (mi_row & MI_MASK);
  838. // update the partition context at the end notes. set partition bits
  839. // of block sizes larger than the current one to be one, and partition
  840. // bits of smaller block sizes to be zero.
  841. memset(above_ctx, partition_context_lookup[subsize].above, bw);
  842. memset(left_ctx, partition_context_lookup[subsize].left, bw);
  843. }
  844. static PARTITION_TYPE read_partition(MACROBLOCKD *xd, int mi_row, int mi_col,
  845. vpx_reader *r,
  846. int has_rows, int has_cols, int bsl) {
  847. const int ctx = dec_partition_plane_context(xd, mi_row, mi_col, bsl);
  848. const vpx_prob *const probs = get_partition_probs(xd, ctx);
  849. FRAME_COUNTS *counts = xd->counts;
  850. PARTITION_TYPE p;
  851. if (has_rows && has_cols)
  852. p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
  853. else if (!has_rows && has_cols)
  854. p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
  855. else if (has_rows && !has_cols)
  856. p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
  857. else
  858. p = PARTITION_SPLIT;
  859. if (counts)
  860. ++counts->partition[ctx][p];
  861. return p;
  862. }
  863. // TODO(slavarnway): eliminate bsize and subsize in future commits
  864. static void decode_partition(VP9Decoder *const pbi, MACROBLOCKD *const xd,
  865. int mi_row, int mi_col,
  866. vpx_reader* r, BLOCK_SIZE bsize, int n4x4_l2) {
  867. VP9_COMMON *const cm = &pbi->common;
  868. const int n8x8_l2 = n4x4_l2 - 1;
  869. const int num_8x8_wh = 1 << n8x8_l2;
  870. const int hbs = num_8x8_wh >> 1;
  871. PARTITION_TYPE partition;
  872. BLOCK_SIZE subsize;
  873. const int has_rows = (mi_row + hbs) < cm->mi_rows;
  874. const int has_cols = (mi_col + hbs) < cm->mi_cols;
  875. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols)
  876. return;
  877. partition = read_partition(xd, mi_row, mi_col, r, has_rows, has_cols,
  878. n8x8_l2);
  879. subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
  880. if (!hbs) {
  881. // calculate bmode block dimensions (log 2)
  882. xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
  883. xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
  884. decode_block(pbi, xd, mi_row, mi_col, r, subsize, 1, 1);
  885. } else {
  886. switch (partition) {
  887. case PARTITION_NONE:
  888. decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n4x4_l2);
  889. break;
  890. case PARTITION_HORZ:
  891. decode_block(pbi, xd, mi_row, mi_col, r, subsize, n4x4_l2, n8x8_l2);
  892. if (has_rows)
  893. decode_block(pbi, xd, mi_row + hbs, mi_col, r, subsize, n4x4_l2,
  894. n8x8_l2);
  895. break;
  896. case PARTITION_VERT:
  897. decode_block(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2, n4x4_l2);
  898. if (has_cols)
  899. decode_block(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2,
  900. n4x4_l2);
  901. break;
  902. case PARTITION_SPLIT:
  903. decode_partition(pbi, xd, mi_row, mi_col, r, subsize, n8x8_l2);
  904. decode_partition(pbi, xd, mi_row, mi_col + hbs, r, subsize, n8x8_l2);
  905. decode_partition(pbi, xd, mi_row + hbs, mi_col, r, subsize, n8x8_l2);
  906. decode_partition(pbi, xd, mi_row + hbs, mi_col + hbs, r, subsize,
  907. n8x8_l2);
  908. break;
  909. default:
  910. assert(0 && "Invalid partition type");
  911. }
  912. }
  913. // update partition context
  914. if (bsize >= BLOCK_8X8 &&
  915. (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
  916. dec_update_partition_context(xd, mi_row, mi_col, subsize, num_8x8_wh);
  917. }
  918. static void setup_token_decoder(const uint8_t *data,
  919. const uint8_t *data_end,
  920. size_t read_size,
  921. struct vpx_internal_error_info *error_info,
  922. vpx_reader *r,
  923. vpx_decrypt_cb decrypt_cb,
  924. void *decrypt_state) {
  925. // Validate the calculated partition length. If the buffer
  926. // described by the partition can't be fully read, then restrict
  927. // it to the portion that can be (for EC mode) or throw an error.
  928. if (!read_is_valid(data, read_size, data_end))
  929. vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
  930. "Truncated packet or corrupt tile length");
  931. if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
  932. vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
  933. "Failed to allocate bool decoder %d", 1);
  934. }
  935. static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
  936. vpx_reader *r) {
  937. int i, j, k, l, m;
  938. if (vpx_read_bit(r))
  939. for (i = 0; i < PLANE_TYPES; ++i)
  940. for (j = 0; j < REF_TYPES; ++j)
  941. for (k = 0; k < COEF_BANDS; ++k)
  942. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
  943. for (m = 0; m < UNCONSTRAINED_NODES; ++m)
  944. vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
  945. }
  946. static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode,
  947. vpx_reader *r) {
  948. const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  949. TX_SIZE tx_size;
  950. for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
  951. read_coef_probs_common(fc->coef_probs[tx_size], r);
  952. }
  953. static void setup_segmentation(struct segmentation *seg,
  954. struct vpx_read_bit_buffer *rb) {
  955. int i, j;
  956. seg->update_map = 0;
  957. seg->update_data = 0;
  958. seg->enabled = vpx_rb_read_bit(rb);
  959. if (!seg->enabled)
  960. return;
  961. // Segmentation map update
  962. seg->update_map = vpx_rb_read_bit(rb);
  963. if (seg->update_map) {
  964. for (i = 0; i < SEG_TREE_PROBS; i++)
  965. seg->tree_probs[i] = vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8)
  966. : MAX_PROB;
  967. seg->temporal_update = vpx_rb_read_bit(rb);
  968. if (seg->temporal_update) {
  969. for (i = 0; i < PREDICTION_PROBS; i++)
  970. seg->pred_probs[i] = vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8)
  971. : MAX_PROB;
  972. } else {
  973. for (i = 0; i < PREDICTION_PROBS; i++)
  974. seg->pred_probs[i] = MAX_PROB;
  975. }
  976. }
  977. // Segmentation data update
  978. seg->update_data = vpx_rb_read_bit(rb);
  979. if (seg->update_data) {
  980. seg->abs_delta = vpx_rb_read_bit(rb);
  981. vp9_clearall_segfeatures(seg);
  982. for (i = 0; i < MAX_SEGMENTS; i++) {
  983. for (j = 0; j < SEG_LVL_MAX; j++) {
  984. int data = 0;
  985. const int feature_enabled = vpx_rb_read_bit(rb);
  986. if (feature_enabled) {
  987. vp9_enable_segfeature(seg, i, j);
  988. data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
  989. if (vp9_is_segfeature_signed(j))
  990. data = vpx_rb_read_bit(rb) ? -data : data;
  991. }
  992. vp9_set_segdata(seg, i, j, data);
  993. }
  994. }
  995. }
  996. }
  997. static void setup_loopfilter(struct loopfilter *lf,
  998. struct vpx_read_bit_buffer *rb) {
  999. lf->filter_level = vpx_rb_read_literal(rb, 6);
  1000. lf->sharpness_level = vpx_rb_read_literal(rb, 3);
  1001. // Read in loop filter deltas applied at the MB level based on mode or ref
  1002. // frame.
  1003. lf->mode_ref_delta_update = 0;
  1004. lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
  1005. if (lf->mode_ref_delta_enabled) {
  1006. lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
  1007. if (lf->mode_ref_delta_update) {
  1008. int i;
  1009. for (i = 0; i < MAX_REF_LF_DELTAS; i++)
  1010. if (vpx_rb_read_bit(rb))
  1011. lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
  1012. for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
  1013. if (vpx_rb_read_bit(rb))
  1014. lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
  1015. }
  1016. }
  1017. }
  1018. static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
  1019. return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
  1020. }
  1021. static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  1022. struct vpx_read_bit_buffer *rb) {
  1023. cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
  1024. cm->y_dc_delta_q = read_delta_q(rb);
  1025. cm->uv_dc_delta_q = read_delta_q(rb);
  1026. cm->uv_ac_delta_q = read_delta_q(rb);
  1027. cm->dequant_bit_depth = cm->bit_depth;
  1028. xd->lossless = cm->base_qindex == 0 &&
  1029. cm->y_dc_delta_q == 0 &&
  1030. cm->uv_dc_delta_q == 0 &&
  1031. cm->uv_ac_delta_q == 0;
  1032. #if CONFIG_VP9_HIGHBITDEPTH
  1033. xd->bd = (int)cm->bit_depth;
  1034. #endif
  1035. }
  1036. static void setup_segmentation_dequant(VP9_COMMON *const cm) {
  1037. // Build y/uv dequant values based on segmentation.
  1038. if (cm->seg.enabled) {
  1039. int i;
  1040. for (i = 0; i < MAX_SEGMENTS; ++i) {
  1041. const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
  1042. cm->y_dequant[i][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q,
  1043. cm->bit_depth);
  1044. cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
  1045. cm->uv_dequant[i][0] = vp9_dc_quant(qindex, cm->uv_dc_delta_q,
  1046. cm->bit_depth);
  1047. cm->uv_dequant[i][1] = vp9_ac_quant(qindex, cm->uv_ac_delta_q,
  1048. cm->bit_depth);
  1049. }
  1050. } else {
  1051. const int qindex = cm->base_qindex;
  1052. // When segmentation is disabled, only the first value is used. The
  1053. // remaining are don't cares.
  1054. cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
  1055. cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
  1056. cm->uv_dequant[0][0] = vp9_dc_quant(qindex, cm->uv_dc_delta_q,
  1057. cm->bit_depth);
  1058. cm->uv_dequant[0][1] = vp9_ac_quant(qindex, cm->uv_ac_delta_q,
  1059. cm->bit_depth);
  1060. }
  1061. }
  1062. static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
  1063. const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH,
  1064. EIGHTTAP,
  1065. EIGHTTAP_SHARP,
  1066. BILINEAR };
  1067. return vpx_rb_read_bit(rb) ? SWITCHABLE
  1068. : literal_to_filter[vpx_rb_read_literal(rb, 2)];
  1069. }
  1070. static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1071. cm->render_width = cm->width;
  1072. cm->render_height = cm->height;
  1073. if (vpx_rb_read_bit(rb))
  1074. vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
  1075. }
  1076. static void resize_mv_buffer(VP9_COMMON *cm) {
  1077. vpx_free(cm->cur_frame->mvs);
  1078. cm->cur_frame->mi_rows = cm->mi_rows;
  1079. cm->cur_frame->mi_cols = cm->mi_cols;
  1080. CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
  1081. (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
  1082. sizeof(*cm->cur_frame->mvs)));
  1083. }
  1084. static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
  1085. #if CONFIG_SIZE_LIMIT
  1086. if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
  1087. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1088. "Dimensions of %dx%d beyond allowed size of %dx%d.",
  1089. width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
  1090. #endif
  1091. if (cm->width != width || cm->height != height) {
  1092. const int new_mi_rows =
  1093. ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
  1094. const int new_mi_cols =
  1095. ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
  1096. // Allocations in vp9_alloc_context_buffers() depend on individual
  1097. // dimensions as well as the overall size.
  1098. if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
  1099. if (vp9_alloc_context_buffers(cm, width, height))
  1100. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1101. "Failed to allocate context buffers");
  1102. } else {
  1103. vp9_set_mb_mi(cm, width, height);
  1104. }
  1105. vp9_init_context_buffers(cm);
  1106. cm->width = width;
  1107. cm->height = height;
  1108. }
  1109. if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
  1110. cm->mi_cols > cm->cur_frame->mi_cols) {
  1111. resize_mv_buffer(cm);
  1112. }
  1113. }
  1114. static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1115. int width, height;
  1116. BufferPool *const pool = cm->buffer_pool;
  1117. vp9_read_frame_size(rb, &width, &height);
  1118. resize_context_buffers(cm, width, height);
  1119. setup_render_size(cm, rb);
  1120. lock_buffer_pool(pool);
  1121. if (vpx_realloc_frame_buffer(
  1122. get_frame_new_buffer(cm), cm->width, cm->height,
  1123. cm->subsampling_x, cm->subsampling_y,
  1124. #if CONFIG_VP9_HIGHBITDEPTH
  1125. cm->use_highbitdepth,
  1126. #endif
  1127. VP9_DEC_BORDER_IN_PIXELS,
  1128. cm->byte_alignment,
  1129. &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
  1130. pool->cb_priv)) {
  1131. unlock_buffer_pool(pool);
  1132. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1133. "Failed to allocate frame buffer");
  1134. }
  1135. unlock_buffer_pool(pool);
  1136. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
  1137. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
  1138. pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
  1139. pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
  1140. pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
  1141. pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
  1142. pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
  1143. }
  1144. static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
  1145. int ref_xss, int ref_yss,
  1146. vpx_bit_depth_t this_bit_depth,
  1147. int this_xss, int this_yss) {
  1148. return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
  1149. ref_yss == this_yss;
  1150. }
  1151. static void setup_frame_size_with_refs(VP9_COMMON *cm,
  1152. struct vpx_read_bit_buffer *rb) {
  1153. int width, height;
  1154. int found = 0, i;
  1155. int has_valid_ref_frame = 0;
  1156. BufferPool *const pool = cm->buffer_pool;
  1157. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1158. if (vpx_rb_read_bit(rb)) {
  1159. if (cm->frame_refs[i].idx != INVALID_IDX) {
  1160. YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
  1161. width = buf->y_crop_width;
  1162. height = buf->y_crop_height;
  1163. found = 1;
  1164. break;
  1165. } else {
  1166. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1167. "Failed to decode frame size");
  1168. }
  1169. }
  1170. }
  1171. if (!found)
  1172. vp9_read_frame_size(rb, &width, &height);
  1173. if (width <= 0 || height <= 0)
  1174. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1175. "Invalid frame size");
  1176. // Check to make sure at least one of frames that this frame references
  1177. // has valid dimensions.
  1178. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1179. RefBuffer *const ref_frame = &cm->frame_refs[i];
  1180. has_valid_ref_frame |= (ref_frame->idx != INVALID_IDX &&
  1181. valid_ref_frame_size(ref_frame->buf->y_crop_width,
  1182. ref_frame->buf->y_crop_height,
  1183. width, height));
  1184. }
  1185. if (!has_valid_ref_frame)
  1186. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1187. "Referenced frame has invalid size");
  1188. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1189. RefBuffer *const ref_frame = &cm->frame_refs[i];
  1190. if (ref_frame->idx == INVALID_IDX ||
  1191. !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
  1192. ref_frame->buf->subsampling_x,
  1193. ref_frame->buf->subsampling_y,
  1194. cm->bit_depth,
  1195. cm->subsampling_x,
  1196. cm->subsampling_y))
  1197. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1198. "Referenced frame has incompatible color format");
  1199. }
  1200. resize_context_buffers(cm, width, height);
  1201. setup_render_size(cm, rb);
  1202. lock_buffer_pool(pool);
  1203. if (vpx_realloc_frame_buffer(
  1204. get_frame_new_buffer(cm), cm->width, cm->height,
  1205. cm->subsampling_x, cm->subsampling_y,
  1206. #if CONFIG_VP9_HIGHBITDEPTH
  1207. cm->use_highbitdepth,
  1208. #endif
  1209. VP9_DEC_BORDER_IN_PIXELS,
  1210. cm->byte_alignment,
  1211. &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
  1212. pool->cb_priv)) {
  1213. unlock_buffer_pool(pool);
  1214. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1215. "Failed to allocate frame buffer");
  1216. }
  1217. unlock_buffer_pool(pool);
  1218. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
  1219. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
  1220. pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
  1221. pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
  1222. pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
  1223. pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
  1224. pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
  1225. }
  1226. static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1227. int min_log2_tile_cols, max_log2_tile_cols, max_ones;
  1228. vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
  1229. // columns
  1230. max_ones = max_log2_tile_cols - min_log2_tile_cols;
  1231. cm->log2_tile_cols = min_log2_tile_cols;
  1232. while (max_ones-- && vpx_rb_read_bit(rb))
  1233. cm->log2_tile_cols++;
  1234. if (cm->log2_tile_cols > 6)
  1235. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1236. "Invalid number of tile columns");
  1237. // rows
  1238. cm->log2_tile_rows = vpx_rb_read_bit(rb);
  1239. if (cm->log2_tile_rows)
  1240. cm->log2_tile_rows += vpx_rb_read_bit(rb);
  1241. }
  1242. // Reads the next tile returning its size and adjusting '*data' accordingly
  1243. // based on 'is_last'.
  1244. static void get_tile_buffer(const uint8_t *const data_end,
  1245. int is_last,
  1246. struct vpx_internal_error_info *error_info,
  1247. const uint8_t **data,
  1248. vpx_decrypt_cb decrypt_cb, void *decrypt_state,
  1249. TileBuffer *buf) {
  1250. size_t size;
  1251. if (!is_last) {
  1252. if (!read_is_valid(*data, 4, data_end))
  1253. vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
  1254. "Truncated packet or corrupt tile length");
  1255. if (decrypt_cb) {
  1256. uint8_t be_data[4];
  1257. decrypt_cb(decrypt_state, *data, be_data, 4);
  1258. size = mem_get_be32(be_data);
  1259. } else {
  1260. size = mem_get_be32(*data);
  1261. }
  1262. *data += 4;
  1263. if (size > (size_t)(data_end - *data))
  1264. vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
  1265. "Truncated packet or corrupt tile size");
  1266. } else {
  1267. size = data_end - *data;
  1268. }
  1269. buf->data = *data;
  1270. buf->size = size;
  1271. *data += size;
  1272. }
  1273. static void get_tile_buffers(VP9Decoder *pbi,
  1274. const uint8_t *data, const uint8_t *data_end,
  1275. int tile_cols, int tile_rows,
  1276. TileBuffer (*tile_buffers)[1 << 6]) {
  1277. int r, c;
  1278. for (r = 0; r < tile_rows; ++r) {
  1279. for (c = 0; c < tile_cols; ++c) {
  1280. const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
  1281. TileBuffer *const buf = &tile_buffers[r][c];
  1282. buf->col = c;
  1283. get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
  1284. pbi->decrypt_cb, pbi->decrypt_state, buf);
  1285. }
  1286. }
  1287. }
  1288. static const uint8_t *decode_tiles(VP9Decoder *pbi,
  1289. const uint8_t *data,
  1290. const uint8_t *data_end) {
  1291. VP9_COMMON *const cm = &pbi->common;
  1292. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  1293. const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1294. const int tile_cols = 1 << cm->log2_tile_cols;
  1295. const int tile_rows = 1 << cm->log2_tile_rows;
  1296. TileBuffer tile_buffers[4][1 << 6];
  1297. int tile_row, tile_col;
  1298. int mi_row, mi_col;
  1299. TileWorkerData *tile_data = NULL;
  1300. if (cm->lf.filter_level && !cm->skip_loop_filter &&
  1301. pbi->lf_worker.data1 == NULL) {
  1302. CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
  1303. vpx_memalign(32, sizeof(LFWorkerData)));
  1304. pbi->lf_worker.hook = (VPxWorkerHook)vp9_loop_filter_worker;
  1305. if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
  1306. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  1307. "Loop filter thread creation failed");
  1308. }
  1309. }
  1310. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1311. LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
  1312. // Be sure to sync as we might be resuming after a failed frame decode.
  1313. winterface->sync(&pbi->lf_worker);
  1314. vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
  1315. pbi->mb.plane);
  1316. }
  1317. assert(tile_rows <= 4);
  1318. assert(tile_cols <= (1 << 6));
  1319. // Note: this memset assumes above_context[0], [1] and [2]
  1320. // are allocated as part of the same buffer.
  1321. memset(cm->above_context, 0,
  1322. sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
  1323. memset(cm->above_seg_context, 0,
  1324. sizeof(*cm->above_seg_context) * aligned_cols);
  1325. vp9_reset_lfm(cm);
  1326. get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
  1327. // Load all tile information into tile_data.
  1328. for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
  1329. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  1330. const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
  1331. tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
  1332. tile_data->xd = pbi->mb;
  1333. tile_data->xd.corrupted = 0;
  1334. tile_data->xd.counts =
  1335. cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
  1336. vp9_zero(tile_data->dqcoeff);
  1337. vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
  1338. setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
  1339. &tile_data->bit_reader, pbi->decrypt_cb,
  1340. pbi->decrypt_state);
  1341. vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
  1342. }
  1343. }
  1344. for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
  1345. TileInfo tile;
  1346. vp9_tile_set_row(&tile, cm, tile_row);
  1347. for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
  1348. mi_row += MI_BLOCK_SIZE) {
  1349. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  1350. const int col = pbi->inv_tile_order ?
  1351. tile_cols - tile_col - 1 : tile_col;
  1352. tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
  1353. vp9_tile_set_col(&tile, cm, col);
  1354. vp9_zero(tile_data->xd.left_context);
  1355. vp9_zero(tile_data->xd.left_seg_context);
  1356. for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
  1357. mi_col += MI_BLOCK_SIZE) {
  1358. decode_partition(pbi, &tile_data->xd, mi_row,
  1359. mi_col, &tile_data->bit_reader, BLOCK_64X64, 4);
  1360. }
  1361. pbi->mb.corrupted |= tile_data->xd.corrupted;
  1362. if (pbi->mb.corrupted)
  1363. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1364. "Failed to decode tile data");
  1365. }
  1366. // Loopfilter one row.
  1367. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1368. const int lf_start = mi_row - MI_BLOCK_SIZE;
  1369. LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
  1370. // delay the loopfilter by 1 macroblock row.
  1371. if (lf_start < 0) continue;
  1372. // decoding has completed: finish up the loop filter in this thread.
  1373. if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
  1374. winterface->sync(&pbi->lf_worker);
  1375. lf_data->start = lf_start;
  1376. lf_data->stop = mi_row;
  1377. if (pbi->max_threads > 1) {
  1378. winterface->launch(&pbi->lf_worker);
  1379. } else {
  1380. winterface->execute(&pbi->lf_worker);
  1381. }
  1382. }
  1383. // After loopfiltering, the last 7 row pixels in each superblock row may
  1384. // still be changed by the longest loopfilter of the next superblock
  1385. // row.
  1386. if (pbi->frame_parallel_decode)
  1387. vp9_frameworker_broadcast(pbi->cur_buf,
  1388. mi_row << MI_BLOCK_SIZE_LOG2);
  1389. }
  1390. }
  1391. // Loopfilter remaining rows in the frame.
  1392. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1393. LFWorkerData *const lf_data = (LFWorkerData*)pbi->lf_worker.data1;
  1394. winterface->sync(&pbi->lf_worker);
  1395. lf_data->start = lf_data->stop;
  1396. lf_data->stop = cm->mi_rows;
  1397. winterface->execute(&pbi->lf_worker);
  1398. }
  1399. // Get last tile data.
  1400. tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
  1401. if (pbi->frame_parallel_decode)
  1402. vp9_frameworker_broadcast(pbi->cur_buf, INT_MAX);
  1403. return vpx_reader_find_end(&tile_data->bit_reader);
  1404. }
  1405. // On entry 'tile_data->data_end' points to the end of the input frame, on exit
  1406. // it is updated to reflect the bitreader position of the final tile column if
  1407. // present in the tile buffer group or NULL otherwise.
  1408. static int tile_worker_hook(TileWorkerData *const tile_data,
  1409. VP9Decoder *const pbi) {
  1410. TileInfo *volatile tile = &tile_data->xd.tile;
  1411. const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
  1412. const uint8_t *volatile bit_reader_end = NULL;
  1413. volatile int n = tile_data->buf_start;
  1414. tile_data->error_info.setjmp = 1;
  1415. if (setjmp(tile_data->error_info.jmp)) {
  1416. tile_data->error_info.setjmp = 0;
  1417. tile_data->xd.corrupted = 1;
  1418. tile_data->data_end = NULL;
  1419. return 0;
  1420. }
  1421. tile_data->xd.error_info = &tile_data->error_info;
  1422. tile_data->xd.corrupted = 0;
  1423. do {
  1424. int mi_row, mi_col;
  1425. const TileBuffer *const buf = pbi->tile_buffers + n;
  1426. vp9_zero(tile_data->dqcoeff);
  1427. vp9_tile_init(tile, &pbi->common, 0, buf->col);
  1428. setup_token_decoder(buf->data, tile_data->data_end, buf->size,
  1429. &tile_data->error_info, &tile_data->bit_reader,
  1430. pbi->decrypt_cb, pbi->decrypt_state);
  1431. vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
  1432. for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
  1433. mi_row += MI_BLOCK_SIZE) {
  1434. vp9_zero(tile_data->xd.left_context);
  1435. vp9_zero(tile_data->xd.left_seg_context);
  1436. for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
  1437. mi_col += MI_BLOCK_SIZE) {
  1438. decode_partition(pbi, &tile_data->xd, mi_row, mi_col,
  1439. &tile_data->bit_reader, BLOCK_64X64, 4);
  1440. }
  1441. }
  1442. if (buf->col == final_col) {
  1443. bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
  1444. }
  1445. } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
  1446. tile_data->data_end = bit_reader_end;
  1447. return !tile_data->xd.corrupted;
  1448. }
  1449. // sorts in descending order
  1450. static int compare_tile_buffers(const void *a, const void *b) {
  1451. const TileBuffer *const buf1 = (const TileBuffer*)a;
  1452. const TileBuffer *const buf2 = (const TileBuffer*)b;
  1453. return (int)(buf2->size - buf1->size);
  1454. }
  1455. static const uint8_t *decode_tiles_mt(VP9Decoder *pbi,
  1456. const uint8_t *data,
  1457. const uint8_t *data_end) {
  1458. VP9_COMMON *const cm = &pbi->common;
  1459. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  1460. const uint8_t *bit_reader_end = NULL;
  1461. const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1462. const int tile_cols = 1 << cm->log2_tile_cols;
  1463. const int tile_rows = 1 << cm->log2_tile_rows;
  1464. const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
  1465. int n;
  1466. assert(tile_cols <= (1 << 6));
  1467. assert(tile_rows == 1);
  1468. (void)tile_rows;
  1469. if (pbi->num_tile_workers == 0) {
  1470. const int num_threads = pbi->max_threads;
  1471. CHECK_MEM_ERROR(cm, pbi->tile_workers,
  1472. vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
  1473. for (n = 0; n < num_threads; ++n) {
  1474. VPxWorker *const worker = &pbi->tile_workers[n];
  1475. ++pbi->num_tile_workers;
  1476. winterface->init(worker);
  1477. if (n < num_threads - 1 && !winterface->reset(worker)) {
  1478. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  1479. "Tile decoder thread creation failed");
  1480. }
  1481. }
  1482. }
  1483. // Reset tile decoding hook
  1484. for (n = 0; n < num_workers; ++n) {
  1485. VPxWorker *const worker = &pbi->tile_workers[n];
  1486. TileWorkerData *const tile_data =
  1487. &pbi->tile_worker_data[n + pbi->total_tiles];
  1488. winterface->sync(worker);
  1489. tile_data->xd = pbi->mb;
  1490. tile_data->xd.counts =
  1491. cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
  1492. worker->hook = (VPxWorkerHook)tile_worker_hook;
  1493. worker->data1 = tile_data;
  1494. worker->data2 = pbi;
  1495. }
  1496. // Note: this memset assumes above_context[0], [1] and [2]
  1497. // are allocated as part of the same buffer.
  1498. memset(cm->above_context, 0,
  1499. sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
  1500. memset(cm->above_seg_context, 0,
  1501. sizeof(*cm->above_seg_context) * aligned_mi_cols);
  1502. vp9_reset_lfm(cm);
  1503. // Load tile data into tile_buffers
  1504. get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
  1505. &pbi->tile_buffers);
  1506. // Sort the buffers based on size in descending order.
  1507. qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
  1508. compare_tile_buffers);
  1509. if (num_workers == tile_cols) {
  1510. // Rearrange the tile buffers such that the largest, and
  1511. // presumably the most difficult, tile will be decoded in the main thread.
  1512. // This should help minimize the number of instances where the main thread
  1513. // is waiting for a worker to complete.
  1514. const TileBuffer largest = pbi->tile_buffers[0];
  1515. memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
  1516. (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
  1517. pbi->tile_buffers[tile_cols - 1] = largest;
  1518. } else {
  1519. int start = 0, end = tile_cols - 2;
  1520. TileBuffer tmp;
  1521. // Interleave the tiles to distribute the load between threads, assuming a
  1522. // larger tile implies it is more difficult to decode.
  1523. while (start < end) {
  1524. tmp = pbi->tile_buffers[start];
  1525. pbi->tile_buffers[start] = pbi->tile_buffers[end];
  1526. pbi->tile_buffers[end] = tmp;
  1527. start += 2;
  1528. end -= 2;
  1529. }
  1530. }
  1531. // Initialize thread frame counts.
  1532. if (!cm->frame_parallel_decoding_mode) {
  1533. for (n = 0; n < num_workers; ++n) {
  1534. TileWorkerData *const tile_data =
  1535. (TileWorkerData*)pbi->tile_workers[n].data1;
  1536. vp9_zero(tile_data->counts);
  1537. }
  1538. }
  1539. {
  1540. const int base = tile_cols / num_workers;
  1541. const int remain = tile_cols % num_workers;
  1542. int buf_start = 0;
  1543. for (n = 0; n < num_workers; ++n) {
  1544. const int count = base + (remain + n) / num_workers;
  1545. VPxWorker *const worker = &pbi->tile_workers[n];
  1546. TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
  1547. tile_data->buf_start = buf_start;
  1548. tile_data->buf_end = buf_start + count - 1;
  1549. tile_data->data_end = data_end;
  1550. buf_start += count;
  1551. worker->had_error = 0;
  1552. if (n == num_workers - 1) {
  1553. assert(tile_data->buf_end == tile_cols - 1);
  1554. winterface->execute(worker);
  1555. } else {
  1556. winterface->launch(worker);
  1557. }
  1558. }
  1559. for (; n > 0; --n) {
  1560. VPxWorker *const worker = &pbi->tile_workers[n - 1];
  1561. TileWorkerData *const tile_data = (TileWorkerData*)worker->data1;
  1562. // TODO(jzern): The tile may have specific error data associated with
  1563. // its vpx_internal_error_info which could be propagated to the main info
  1564. // in cm. Additionally once the threads have been synced and an error is
  1565. // detected, there's no point in continuing to decode tiles.
  1566. pbi->mb.corrupted |= !winterface->sync(worker);
  1567. if (!bit_reader_end) bit_reader_end = tile_data->data_end;
  1568. }
  1569. }
  1570. // Accumulate thread frame counts.
  1571. if (!cm->frame_parallel_decoding_mode) {
  1572. for (n = 0; n < num_workers; ++n) {
  1573. TileWorkerData *const tile_data =
  1574. (TileWorkerData*)pbi->tile_workers[n].data1;
  1575. vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
  1576. }
  1577. }
  1578. assert(bit_reader_end || pbi->mb.corrupted);
  1579. return bit_reader_end;
  1580. }
  1581. static void error_handler(void *data) {
  1582. VP9_COMMON *const cm = (VP9_COMMON *)data;
  1583. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
  1584. }
  1585. static void read_bitdepth_colorspace_sampling(
  1586. VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1587. if (cm->profile >= PROFILE_2) {
  1588. cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
  1589. #if CONFIG_VP9_HIGHBITDEPTH
  1590. cm->use_highbitdepth = 1;
  1591. #endif
  1592. } else {
  1593. cm->bit_depth = VPX_BITS_8;
  1594. #if CONFIG_VP9_HIGHBITDEPTH
  1595. cm->use_highbitdepth = 0;
  1596. #endif
  1597. }
  1598. cm->color_space = vpx_rb_read_literal(rb, 3);
  1599. if (cm->color_space != VPX_CS_SRGB) {
  1600. cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
  1601. if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
  1602. cm->subsampling_x = vpx_rb_read_bit(rb);
  1603. cm->subsampling_y = vpx_rb_read_bit(rb);
  1604. if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
  1605. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1606. "4:2:0 color not supported in profile 1 or 3");
  1607. if (vpx_rb_read_bit(rb))
  1608. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1609. "Reserved bit set");
  1610. } else {
  1611. cm->subsampling_y = cm->subsampling_x = 1;
  1612. }
  1613. } else {
  1614. cm->color_range = VPX_CR_FULL_RANGE;
  1615. if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
  1616. // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
  1617. // 4:2:2 or 4:4:0 chroma sampling is not allowed.
  1618. cm->subsampling_y = cm->subsampling_x = 0;
  1619. if (vpx_rb_read_bit(rb))
  1620. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1621. "Reserved bit set");
  1622. } else {
  1623. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1624. "4:4:4 color not supported in profile 0 or 2");
  1625. }
  1626. }
  1627. }
  1628. static size_t read_uncompressed_header(VP9Decoder *pbi,
  1629. struct vpx_read_bit_buffer *rb) {
  1630. VP9_COMMON *const cm = &pbi->common;
  1631. BufferPool *const pool = cm->buffer_pool;
  1632. RefCntBuffer *const frame_bufs = pool->frame_bufs;
  1633. int i, mask, ref_index = 0;
  1634. size_t sz;
  1635. cm->last_frame_type = cm->frame_type;
  1636. cm->last_intra_only = cm->intra_only;
  1637. if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
  1638. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1639. "Invalid frame marker");
  1640. cm->profile = vp9_read_profile(rb);
  1641. #if CONFIG_VP9_HIGHBITDEPTH
  1642. if (cm->profile >= MAX_PROFILES)
  1643. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1644. "Unsupported bitstream profile");
  1645. #else
  1646. if (cm->profile >= PROFILE_2)
  1647. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1648. "Unsupported bitstream profile");
  1649. #endif
  1650. cm->show_existing_frame = vpx_rb_read_bit(rb);
  1651. if (cm->show_existing_frame) {
  1652. // Show an existing frame directly.
  1653. const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
  1654. lock_buffer_pool(pool);
  1655. if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
  1656. unlock_buffer_pool(pool);
  1657. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1658. "Buffer %d does not contain a decoded frame",
  1659. frame_to_show);
  1660. }
  1661. ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
  1662. unlock_buffer_pool(pool);
  1663. pbi->refresh_frame_flags = 0;
  1664. cm->lf.filter_level = 0;
  1665. cm->show_frame = 1;
  1666. if (pbi->frame_parallel_decode) {
  1667. for (i = 0; i < REF_FRAMES; ++i)
  1668. cm->next_ref_frame_map[i] = cm->ref_frame_map[i];
  1669. }
  1670. return 0;
  1671. }
  1672. cm->frame_type = (FRAME_TYPE) vpx_rb_read_bit(rb);
  1673. cm->show_frame = vpx_rb_read_bit(rb);
  1674. cm->error_resilient_mode = vpx_rb_read_bit(rb);
  1675. if (cm->frame_type == KEY_FRAME) {
  1676. if (!vp9_read_sync_code(rb))
  1677. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1678. "Invalid frame sync code");
  1679. read_bitdepth_colorspace_sampling(cm, rb);
  1680. pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
  1681. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1682. cm->frame_refs[i].idx = INVALID_IDX;
  1683. cm->frame_refs[i].buf = NULL;
  1684. }
  1685. setup_frame_size(cm, rb);
  1686. if (pbi->need_resync) {
  1687. memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
  1688. pbi->need_resync = 0;
  1689. }
  1690. } else {
  1691. cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
  1692. cm->reset_frame_context = cm->error_resilient_mode ?
  1693. 0 : vpx_rb_read_literal(rb, 2);
  1694. if (cm->intra_only) {
  1695. if (!vp9_read_sync_code(rb))
  1696. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  1697. "Invalid frame sync code");
  1698. if (cm->profile > PROFILE_0) {
  1699. read_bitdepth_colorspace_sampling(cm, rb);
  1700. } else {
  1701. // NOTE: The intra-only frame header does not include the specification
  1702. // of either the color format or color sub-sampling in profile 0. VP9
  1703. // specifies that the default color format should be YUV 4:2:0 in this
  1704. // case (normative).
  1705. cm->color_space = VPX_CS_BT_601;
  1706. cm->color_range = VPX_CR_STUDIO_RANGE;
  1707. cm->subsampling_y = cm->subsampling_x = 1;
  1708. cm->bit_depth = VPX_BITS_8;
  1709. #if CONFIG_VP9_HIGHBITDEPTH
  1710. cm->use_highbitdepth = 0;
  1711. #endif
  1712. }
  1713. pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
  1714. setup_frame_size(cm, rb);
  1715. if (pbi->need_resync) {
  1716. memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
  1717. pbi->need_resync = 0;
  1718. }
  1719. } else if (pbi->need_resync != 1) { /* Skip if need resync */
  1720. pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
  1721. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1722. const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
  1723. const int idx = cm->ref_frame_map[ref];
  1724. RefBuffer *const ref_frame = &cm->frame_refs[i];
  1725. ref_frame->idx = idx;
  1726. ref_frame->buf = &frame_bufs[idx].buf;
  1727. cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
  1728. }
  1729. setup_frame_size_with_refs(cm, rb);
  1730. cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
  1731. cm->interp_filter = read_interp_filter(rb);
  1732. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1733. RefBuffer *const ref_buf = &cm->frame_refs[i];
  1734. #if CONFIG_VP9_HIGHBITDEPTH
  1735. vp9_setup_scale_factors_for_frame(&ref_buf->sf,
  1736. ref_buf->buf->y_crop_width,
  1737. ref_buf->buf->y_crop_height,
  1738. cm->width, cm->height,
  1739. cm->use_highbitdepth);
  1740. #else
  1741. vp9_setup_scale_factors_for_frame(&ref_buf->sf,
  1742. ref_buf->buf->y_crop_width,
  1743. ref_buf->buf->y_crop_height,
  1744. cm->width, cm->height);
  1745. #endif
  1746. }
  1747. }
  1748. }
  1749. #if CONFIG_VP9_HIGHBITDEPTH
  1750. get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
  1751. #endif
  1752. get_frame_new_buffer(cm)->color_space = cm->color_space;
  1753. get_frame_new_buffer(cm)->color_range = cm->color_range;
  1754. get_frame_new_buffer(cm)->render_width = cm->render_width;
  1755. get_frame_new_buffer(cm)->render_height = cm->render_height;
  1756. if (pbi->need_resync) {
  1757. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1758. "Keyframe / intra-only frame required to reset decoder"
  1759. " state");
  1760. }
  1761. if (!cm->error_resilient_mode) {
  1762. cm->refresh_frame_context = vpx_rb_read_bit(rb);
  1763. cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
  1764. if (!cm->frame_parallel_decoding_mode)
  1765. vp9_zero(cm->counts);
  1766. } else {
  1767. cm->refresh_frame_context = 0;
  1768. cm->frame_parallel_decoding_mode = 1;
  1769. }
  1770. // This flag will be overridden by the call to vp9_setup_past_independence
  1771. // below, forcing the use of context 0 for those frame types.
  1772. cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
  1773. // Generate next_ref_frame_map.
  1774. lock_buffer_pool(pool);
  1775. for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
  1776. if (mask & 1) {
  1777. cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
  1778. ++frame_bufs[cm->new_fb_idx].ref_count;
  1779. } else {
  1780. cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
  1781. }
  1782. // Current thread holds the reference frame.
  1783. if (cm->ref_frame_map[ref_index] >= 0)
  1784. ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
  1785. ++ref_index;
  1786. }
  1787. for (; ref_index < REF_FRAMES; ++ref_index) {
  1788. cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
  1789. // Current thread holds the reference frame.
  1790. if (cm->ref_frame_map[ref_index] >= 0)
  1791. ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
  1792. }
  1793. unlock_buffer_pool(pool);
  1794. pbi->hold_ref_buf = 1;
  1795. if (frame_is_intra_only(cm) || cm->error_resilient_mode)
  1796. vp9_setup_past_independence(cm);
  1797. setup_loopfilter(&cm->lf, rb);
  1798. setup_quantization(cm, &pbi->mb, rb);
  1799. setup_segmentation(&cm->seg, rb);
  1800. setup_segmentation_dequant(cm);
  1801. setup_tile_info(cm, rb);
  1802. sz = vpx_rb_read_literal(rb, 16);
  1803. if (sz == 0)
  1804. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1805. "Invalid header size");
  1806. return sz;
  1807. }
  1808. static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
  1809. size_t partition_size) {
  1810. VP9_COMMON *const cm = &pbi->common;
  1811. MACROBLOCKD *const xd = &pbi->mb;
  1812. FRAME_CONTEXT *const fc = cm->fc;
  1813. vpx_reader r;
  1814. int k;
  1815. if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
  1816. pbi->decrypt_state))
  1817. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1818. "Failed to allocate bool decoder 0");
  1819. cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
  1820. if (cm->tx_mode == TX_MODE_SELECT)
  1821. read_tx_mode_probs(&fc->tx_probs, &r);
  1822. read_coef_probs(fc, cm->tx_mode, &r);
  1823. for (k = 0; k < SKIP_CONTEXTS; ++k)
  1824. vp9_diff_update_prob(&r, &fc->skip_probs[k]);
  1825. if (!frame_is_intra_only(cm)) {
  1826. nmv_context *const nmvc = &fc->nmvc;
  1827. int i, j;
  1828. read_inter_mode_probs(fc, &r);
  1829. if (cm->interp_filter == SWITCHABLE)
  1830. read_switchable_interp_probs(fc, &r);
  1831. for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
  1832. vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
  1833. cm->reference_mode = read_frame_reference_mode(cm, &r);
  1834. if (cm->reference_mode != SINGLE_REFERENCE)
  1835. setup_compound_reference_mode(cm);
  1836. read_frame_reference_mode_probs(cm, &r);
  1837. for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
  1838. for (i = 0; i < INTRA_MODES - 1; ++i)
  1839. vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
  1840. for (j = 0; j < PARTITION_CONTEXTS; ++j)
  1841. for (i = 0; i < PARTITION_TYPES - 1; ++i)
  1842. vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
  1843. read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
  1844. }
  1845. return vpx_reader_has_error(&r);
  1846. }
  1847. static struct vpx_read_bit_buffer *init_read_bit_buffer(
  1848. VP9Decoder *pbi,
  1849. struct vpx_read_bit_buffer *rb,
  1850. const uint8_t *data,
  1851. const uint8_t *data_end,
  1852. uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
  1853. rb->bit_offset = 0;
  1854. rb->error_handler = error_handler;
  1855. rb->error_handler_data = &pbi->common;
  1856. if (pbi->decrypt_cb) {
  1857. const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
  1858. pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
  1859. rb->bit_buffer = clear_data;
  1860. rb->bit_buffer_end = clear_data + n;
  1861. } else {
  1862. rb->bit_buffer = data;
  1863. rb->bit_buffer_end = data_end;
  1864. }
  1865. return rb;
  1866. }
  1867. //------------------------------------------------------------------------------
  1868. int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
  1869. return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
  1870. vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
  1871. vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
  1872. }
  1873. void vp9_read_frame_size(struct vpx_read_bit_buffer *rb,
  1874. int *width, int *height) {
  1875. *width = vpx_rb_read_literal(rb, 16) + 1;
  1876. *height = vpx_rb_read_literal(rb, 16) + 1;
  1877. }
  1878. BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
  1879. int profile = vpx_rb_read_bit(rb);
  1880. profile |= vpx_rb_read_bit(rb) << 1;
  1881. if (profile > 2)
  1882. profile += vpx_rb_read_bit(rb);
  1883. return (BITSTREAM_PROFILE) profile;
  1884. }
  1885. void vp9_decode_frame(VP9Decoder *pbi,
  1886. const uint8_t *data, const uint8_t *data_end,
  1887. const uint8_t **p_data_end) {
  1888. VP9_COMMON *const cm = &pbi->common;
  1889. MACROBLOCKD *const xd = &pbi->mb;
  1890. struct vpx_read_bit_buffer rb;
  1891. int context_updated = 0;
  1892. uint8_t clear_data[MAX_VP9_HEADER_SIZE];
  1893. const size_t first_partition_size = read_uncompressed_header(pbi,
  1894. init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
  1895. const int tile_rows = 1 << cm->log2_tile_rows;
  1896. const int tile_cols = 1 << cm->log2_tile_cols;
  1897. YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
  1898. xd->cur_buf = new_fb;
  1899. if (!first_partition_size) {
  1900. // showing a frame directly
  1901. *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
  1902. return;
  1903. }
  1904. data += vpx_rb_bytes_read(&rb);
  1905. if (!read_is_valid(data, first_partition_size, data_end))
  1906. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1907. "Truncated packet or corrupt header length");
  1908. cm->use_prev_frame_mvs = !cm->error_resilient_mode &&
  1909. cm->width == cm->last_width &&
  1910. cm->height == cm->last_height &&
  1911. !cm->last_intra_only &&
  1912. cm->last_show_frame &&
  1913. (cm->last_frame_type != KEY_FRAME);
  1914. vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
  1915. *cm->fc = cm->frame_contexts[cm->frame_context_idx];
  1916. if (!cm->fc->initialized)
  1917. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1918. "Uninitialized entropy context.");
  1919. xd->corrupted = 0;
  1920. new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
  1921. if (new_fb->corrupted)
  1922. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1923. "Decode failed. Frame data header is corrupted.");
  1924. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1925. vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
  1926. }
  1927. // If encoded in frame parallel mode, frame context is ready after decoding
  1928. // the frame header.
  1929. if (pbi->frame_parallel_decode && cm->frame_parallel_decoding_mode) {
  1930. VPxWorker *const worker = pbi->frame_worker_owner;
  1931. FrameWorkerData *const frame_worker_data = worker->data1;
  1932. if (cm->refresh_frame_context) {
  1933. context_updated = 1;
  1934. cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
  1935. }
  1936. vp9_frameworker_lock_stats(worker);
  1937. pbi->cur_buf->row = -1;
  1938. pbi->cur_buf->col = -1;
  1939. frame_worker_data->frame_context_ready = 1;
  1940. // Signal the main thread that context is ready.
  1941. vp9_frameworker_signal_stats(worker);
  1942. vp9_frameworker_unlock_stats(worker);
  1943. }
  1944. if (pbi->tile_worker_data == NULL ||
  1945. (tile_cols * tile_rows) != pbi->total_tiles) {
  1946. const int num_tile_workers = tile_cols * tile_rows +
  1947. ((pbi->max_threads > 1) ? pbi->max_threads : 0);
  1948. const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data);
  1949. // Ensure tile data offsets will be properly aligned. This may fail on
  1950. // platforms without DECLARE_ALIGNED().
  1951. assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
  1952. vpx_free(pbi->tile_worker_data);
  1953. CHECK_MEM_ERROR(cm, pbi->tile_worker_data, vpx_memalign(32, twd_size));
  1954. pbi->total_tiles = tile_rows * tile_cols;
  1955. }
  1956. if (pbi->max_threads > 1 && tile_rows == 1 && tile_cols > 1) {
  1957. // Multi-threaded tile decoder
  1958. *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
  1959. if (!xd->corrupted) {
  1960. if (!cm->skip_loop_filter) {
  1961. // If multiple threads are used to decode tiles, then we use those
  1962. // threads to do parallel loopfiltering.
  1963. vp9_loop_filter_frame_mt(new_fb, cm, pbi->mb.plane,
  1964. cm->lf.filter_level, 0, 0, pbi->tile_workers,
  1965. pbi->num_tile_workers, &pbi->lf_row_sync);
  1966. }
  1967. } else {
  1968. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1969. "Decode failed. Frame data is corrupted.");
  1970. }
  1971. } else {
  1972. *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
  1973. }
  1974. if (!xd->corrupted) {
  1975. if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
  1976. vp9_adapt_coef_probs(cm);
  1977. if (!frame_is_intra_only(cm)) {
  1978. vp9_adapt_mode_probs(cm);
  1979. vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
  1980. }
  1981. }
  1982. } else {
  1983. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1984. "Decode failed. Frame data is corrupted.");
  1985. }
  1986. // Non frame parallel update frame context here.
  1987. if (cm->refresh_frame_context && !context_updated)
  1988. cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
  1989. }