vp9_decodeframe.c 79 KB

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