vp9_decodeframe.c 74 KB

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