vp9_decodeframe.c 112 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055
  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. #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  24. #include "vpx_util/vpx_debug_util.h"
  25. #endif // CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  26. #include "vp9/common/vp9_alloccommon.h"
  27. #include "vp9/common/vp9_common.h"
  28. #include "vp9/common/vp9_entropy.h"
  29. #include "vp9/common/vp9_entropymode.h"
  30. #include "vp9/common/vp9_idct.h"
  31. #include "vp9/common/vp9_thread_common.h"
  32. #include "vp9/common/vp9_pred_common.h"
  33. #include "vp9/common/vp9_quant_common.h"
  34. #include "vp9/common/vp9_reconintra.h"
  35. #include "vp9/common/vp9_reconinter.h"
  36. #include "vp9/common/vp9_seg_common.h"
  37. #include "vp9/common/vp9_tile_common.h"
  38. #include "vp9/decoder/vp9_decodeframe.h"
  39. #include "vp9/decoder/vp9_detokenize.h"
  40. #include "vp9/decoder/vp9_decodemv.h"
  41. #include "vp9/decoder/vp9_decoder.h"
  42. #include "vp9/decoder/vp9_dsubexp.h"
  43. #include "vp9/decoder/vp9_job_queue.h"
  44. #define MAX_VP9_HEADER_SIZE 80
  45. typedef int (*predict_recon_func)(TileWorkerData *twd, MODE_INFO *const mi,
  46. int plane, int row, int col, TX_SIZE tx_size);
  47. typedef void (*intra_recon_func)(TileWorkerData *twd, MODE_INFO *const mi,
  48. int plane, int row, int col, TX_SIZE tx_size);
  49. static int read_is_valid(const uint8_t *start, size_t len, const uint8_t *end) {
  50. return len != 0 && len <= (size_t)(end - start);
  51. }
  52. static int decode_unsigned_max(struct vpx_read_bit_buffer *rb, int max) {
  53. const int data = vpx_rb_read_literal(rb, get_unsigned_bits(max));
  54. return data > max ? max : data;
  55. }
  56. static TX_MODE read_tx_mode(vpx_reader *r) {
  57. TX_MODE tx_mode = vpx_read_literal(r, 2);
  58. if (tx_mode == ALLOW_32X32) tx_mode += vpx_read_bit(r);
  59. return tx_mode;
  60. }
  61. static void read_tx_mode_probs(struct tx_probs *tx_probs, vpx_reader *r) {
  62. int i, j;
  63. for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
  64. for (j = 0; j < TX_SIZES - 3; ++j)
  65. vp9_diff_update_prob(r, &tx_probs->p8x8[i][j]);
  66. for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
  67. for (j = 0; j < TX_SIZES - 2; ++j)
  68. vp9_diff_update_prob(r, &tx_probs->p16x16[i][j]);
  69. for (i = 0; i < TX_SIZE_CONTEXTS; ++i)
  70. for (j = 0; j < TX_SIZES - 1; ++j)
  71. vp9_diff_update_prob(r, &tx_probs->p32x32[i][j]);
  72. }
  73. static void read_switchable_interp_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
  74. int i, j;
  75. for (j = 0; j < SWITCHABLE_FILTER_CONTEXTS; ++j)
  76. for (i = 0; i < SWITCHABLE_FILTERS - 1; ++i)
  77. vp9_diff_update_prob(r, &fc->switchable_interp_prob[j][i]);
  78. }
  79. static void read_inter_mode_probs(FRAME_CONTEXT *fc, vpx_reader *r) {
  80. int i, j;
  81. for (i = 0; i < INTER_MODE_CONTEXTS; ++i)
  82. for (j = 0; j < INTER_MODES - 1; ++j)
  83. vp9_diff_update_prob(r, &fc->inter_mode_probs[i][j]);
  84. }
  85. static REFERENCE_MODE read_frame_reference_mode(const VP9_COMMON *cm,
  86. vpx_reader *r) {
  87. if (vp9_compound_reference_allowed(cm)) {
  88. return vpx_read_bit(r)
  89. ? (vpx_read_bit(r) ? REFERENCE_MODE_SELECT : COMPOUND_REFERENCE)
  90. : SINGLE_REFERENCE;
  91. } else {
  92. return SINGLE_REFERENCE;
  93. }
  94. }
  95. static void read_frame_reference_mode_probs(VP9_COMMON *cm, vpx_reader *r) {
  96. FRAME_CONTEXT *const fc = cm->fc;
  97. int i;
  98. if (cm->reference_mode == REFERENCE_MODE_SELECT)
  99. for (i = 0; i < COMP_INTER_CONTEXTS; ++i)
  100. vp9_diff_update_prob(r, &fc->comp_inter_prob[i]);
  101. if (cm->reference_mode != COMPOUND_REFERENCE)
  102. for (i = 0; i < REF_CONTEXTS; ++i) {
  103. vp9_diff_update_prob(r, &fc->single_ref_prob[i][0]);
  104. vp9_diff_update_prob(r, &fc->single_ref_prob[i][1]);
  105. }
  106. if (cm->reference_mode != SINGLE_REFERENCE)
  107. for (i = 0; i < REF_CONTEXTS; ++i)
  108. vp9_diff_update_prob(r, &fc->comp_ref_prob[i]);
  109. }
  110. static void update_mv_probs(vpx_prob *p, int n, vpx_reader *r) {
  111. int i;
  112. for (i = 0; i < n; ++i)
  113. if (vpx_read(r, MV_UPDATE_PROB)) p[i] = (vpx_read_literal(r, 7) << 1) | 1;
  114. }
  115. static void read_mv_probs(nmv_context *ctx, int allow_hp, vpx_reader *r) {
  116. int i, j;
  117. update_mv_probs(ctx->joints, MV_JOINTS - 1, r);
  118. for (i = 0; i < 2; ++i) {
  119. nmv_component *const comp_ctx = &ctx->comps[i];
  120. update_mv_probs(&comp_ctx->sign, 1, r);
  121. update_mv_probs(comp_ctx->classes, MV_CLASSES - 1, r);
  122. update_mv_probs(comp_ctx->class0, CLASS0_SIZE - 1, r);
  123. update_mv_probs(comp_ctx->bits, MV_OFFSET_BITS, r);
  124. }
  125. for (i = 0; i < 2; ++i) {
  126. nmv_component *const comp_ctx = &ctx->comps[i];
  127. for (j = 0; j < CLASS0_SIZE; ++j)
  128. update_mv_probs(comp_ctx->class0_fp[j], MV_FP_SIZE - 1, r);
  129. update_mv_probs(comp_ctx->fp, 3, r);
  130. }
  131. if (allow_hp) {
  132. for (i = 0; i < 2; ++i) {
  133. nmv_component *const comp_ctx = &ctx->comps[i];
  134. update_mv_probs(&comp_ctx->class0_hp, 1, r);
  135. update_mv_probs(&comp_ctx->hp, 1, r);
  136. }
  137. }
  138. }
  139. static void inverse_transform_block_inter(MACROBLOCKD *xd, int plane,
  140. const TX_SIZE tx_size, uint8_t *dst,
  141. int stride, int eob) {
  142. struct macroblockd_plane *const pd = &xd->plane[plane];
  143. tran_low_t *const dqcoeff = pd->dqcoeff;
  144. assert(eob > 0);
  145. #if CONFIG_VP9_HIGHBITDEPTH
  146. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  147. uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
  148. if (xd->lossless) {
  149. vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
  150. } else {
  151. switch (tx_size) {
  152. case TX_4X4:
  153. vp9_highbd_idct4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
  154. break;
  155. case TX_8X8:
  156. vp9_highbd_idct8x8_add(dqcoeff, dst16, stride, eob, xd->bd);
  157. break;
  158. case TX_16X16:
  159. vp9_highbd_idct16x16_add(dqcoeff, dst16, stride, eob, xd->bd);
  160. break;
  161. case TX_32X32:
  162. vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
  163. break;
  164. default: assert(0 && "Invalid transform size");
  165. }
  166. }
  167. } else {
  168. if (xd->lossless) {
  169. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  170. } else {
  171. switch (tx_size) {
  172. case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
  173. case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
  174. case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
  175. case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
  176. default: assert(0 && "Invalid transform size"); return;
  177. }
  178. }
  179. }
  180. #else
  181. if (xd->lossless) {
  182. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  183. } else {
  184. switch (tx_size) {
  185. case TX_4X4: vp9_idct4x4_add(dqcoeff, dst, stride, eob); break;
  186. case TX_8X8: vp9_idct8x8_add(dqcoeff, dst, stride, eob); break;
  187. case TX_16X16: vp9_idct16x16_add(dqcoeff, dst, stride, eob); break;
  188. case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
  189. default: assert(0 && "Invalid transform size"); return;
  190. }
  191. }
  192. #endif // CONFIG_VP9_HIGHBITDEPTH
  193. if (eob == 1) {
  194. dqcoeff[0] = 0;
  195. } else {
  196. if (tx_size <= TX_16X16 && eob <= 10)
  197. memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
  198. else if (tx_size == TX_32X32 && eob <= 34)
  199. memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
  200. else
  201. memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
  202. }
  203. }
  204. static void inverse_transform_block_intra(MACROBLOCKD *xd, int plane,
  205. const TX_TYPE tx_type,
  206. const TX_SIZE tx_size, uint8_t *dst,
  207. int stride, int eob) {
  208. struct macroblockd_plane *const pd = &xd->plane[plane];
  209. tran_low_t *const dqcoeff = pd->dqcoeff;
  210. assert(eob > 0);
  211. #if CONFIG_VP9_HIGHBITDEPTH
  212. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  213. uint16_t *const dst16 = CONVERT_TO_SHORTPTR(dst);
  214. if (xd->lossless) {
  215. vp9_highbd_iwht4x4_add(dqcoeff, dst16, stride, eob, xd->bd);
  216. } else {
  217. switch (tx_size) {
  218. case TX_4X4:
  219. vp9_highbd_iht4x4_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
  220. break;
  221. case TX_8X8:
  222. vp9_highbd_iht8x8_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
  223. break;
  224. case TX_16X16:
  225. vp9_highbd_iht16x16_add(tx_type, dqcoeff, dst16, stride, eob, xd->bd);
  226. break;
  227. case TX_32X32:
  228. vp9_highbd_idct32x32_add(dqcoeff, dst16, stride, eob, xd->bd);
  229. break;
  230. default: assert(0 && "Invalid transform size");
  231. }
  232. }
  233. } else {
  234. if (xd->lossless) {
  235. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  236. } else {
  237. switch (tx_size) {
  238. case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
  239. case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
  240. case TX_16X16:
  241. vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
  242. break;
  243. case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
  244. default: assert(0 && "Invalid transform size"); return;
  245. }
  246. }
  247. }
  248. #else
  249. if (xd->lossless) {
  250. vp9_iwht4x4_add(dqcoeff, dst, stride, eob);
  251. } else {
  252. switch (tx_size) {
  253. case TX_4X4: vp9_iht4x4_add(tx_type, dqcoeff, dst, stride, eob); break;
  254. case TX_8X8: vp9_iht8x8_add(tx_type, dqcoeff, dst, stride, eob); break;
  255. case TX_16X16:
  256. vp9_iht16x16_add(tx_type, dqcoeff, dst, stride, eob);
  257. break;
  258. case TX_32X32: vp9_idct32x32_add(dqcoeff, dst, stride, eob); break;
  259. default: assert(0 && "Invalid transform size"); return;
  260. }
  261. }
  262. #endif // CONFIG_VP9_HIGHBITDEPTH
  263. if (eob == 1) {
  264. dqcoeff[0] = 0;
  265. } else {
  266. if (tx_type == DCT_DCT && tx_size <= TX_16X16 && eob <= 10)
  267. memset(dqcoeff, 0, 4 * (4 << tx_size) * sizeof(dqcoeff[0]));
  268. else if (tx_size == TX_32X32 && eob <= 34)
  269. memset(dqcoeff, 0, 256 * sizeof(dqcoeff[0]));
  270. else
  271. memset(dqcoeff, 0, (16 << (tx_size << 1)) * sizeof(dqcoeff[0]));
  272. }
  273. }
  274. static void predict_and_reconstruct_intra_block(TileWorkerData *twd,
  275. MODE_INFO *const mi, int plane,
  276. int row, int col,
  277. TX_SIZE tx_size) {
  278. MACROBLOCKD *const xd = &twd->xd;
  279. struct macroblockd_plane *const pd = &xd->plane[plane];
  280. PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
  281. uint8_t *dst;
  282. dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
  283. if (mi->sb_type < BLOCK_8X8)
  284. if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
  285. vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
  286. dst, pd->dst.stride, col, row, plane);
  287. if (!mi->skip) {
  288. const TX_TYPE tx_type =
  289. (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
  290. const scan_order *sc = (plane || xd->lossless)
  291. ? &vp9_default_scan_orders[tx_size]
  292. : &vp9_scan_orders[tx_size][tx_type];
  293. const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
  294. mi->segment_id);
  295. if (eob > 0) {
  296. inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
  297. pd->dst.stride, eob);
  298. }
  299. }
  300. }
  301. static void parse_intra_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi,
  302. int plane, int row, int col,
  303. TX_SIZE tx_size) {
  304. MACROBLOCKD *const xd = &twd->xd;
  305. PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
  306. if (mi->sb_type < BLOCK_8X8)
  307. if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
  308. if (!mi->skip) {
  309. struct macroblockd_plane *const pd = &xd->plane[plane];
  310. const TX_TYPE tx_type =
  311. (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
  312. const scan_order *sc = (plane || xd->lossless)
  313. ? &vp9_default_scan_orders[tx_size]
  314. : &vp9_scan_orders[tx_size][tx_type];
  315. *pd->eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
  316. mi->segment_id);
  317. /* Keep the alignment to 16 */
  318. pd->dqcoeff += (16 << (tx_size << 1));
  319. pd->eob++;
  320. }
  321. }
  322. static void predict_and_reconstruct_intra_block_row_mt(TileWorkerData *twd,
  323. MODE_INFO *const mi,
  324. int plane, int row,
  325. int col,
  326. TX_SIZE tx_size) {
  327. MACROBLOCKD *const xd = &twd->xd;
  328. struct macroblockd_plane *const pd = &xd->plane[plane];
  329. PREDICTION_MODE mode = (plane == 0) ? mi->mode : mi->uv_mode;
  330. uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
  331. if (mi->sb_type < BLOCK_8X8)
  332. if (plane == 0) mode = xd->mi[0]->bmi[(row << 1) + col].as_mode;
  333. vp9_predict_intra_block(xd, pd->n4_wl, tx_size, mode, dst, pd->dst.stride,
  334. dst, pd->dst.stride, col, row, plane);
  335. if (!mi->skip) {
  336. const TX_TYPE tx_type =
  337. (plane || xd->lossless) ? DCT_DCT : intra_mode_to_tx_type_lookup[mode];
  338. if (*pd->eob > 0) {
  339. inverse_transform_block_intra(xd, plane, tx_type, tx_size, dst,
  340. pd->dst.stride, *pd->eob);
  341. }
  342. /* Keep the alignment to 16 */
  343. pd->dqcoeff += (16 << (tx_size << 1));
  344. pd->eob++;
  345. }
  346. }
  347. static int reconstruct_inter_block(TileWorkerData *twd, MODE_INFO *const mi,
  348. int plane, int row, int col, TX_SIZE tx_size,
  349. int mi_row, int mi_col) {
  350. MACROBLOCKD *const xd = &twd->xd;
  351. struct macroblockd_plane *const pd = &xd->plane[plane];
  352. const scan_order *sc = &vp9_default_scan_orders[tx_size];
  353. const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
  354. mi->segment_id);
  355. uint8_t *dst = &pd->dst.buf[4 * row * pd->dst.stride + 4 * col];
  356. if (eob > 0) {
  357. inverse_transform_block_inter(xd, plane, tx_size, dst, pd->dst.stride, eob);
  358. }
  359. #if CONFIG_MISMATCH_DEBUG
  360. {
  361. int pixel_c, pixel_r;
  362. int blk_w = 1 << (tx_size + TX_UNIT_SIZE_LOG2);
  363. int blk_h = 1 << (tx_size + TX_UNIT_SIZE_LOG2);
  364. mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, col, row,
  365. pd->subsampling_x, pd->subsampling_y);
  366. mismatch_check_block_tx(dst, pd->dst.stride, plane, pixel_c, pixel_r, blk_w,
  367. blk_h, xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
  368. }
  369. #else
  370. (void)mi_row;
  371. (void)mi_col;
  372. #endif
  373. return eob;
  374. }
  375. static int parse_inter_block_row_mt(TileWorkerData *twd, MODE_INFO *const mi,
  376. int plane, int row, int col,
  377. TX_SIZE tx_size) {
  378. MACROBLOCKD *const xd = &twd->xd;
  379. struct macroblockd_plane *const pd = &xd->plane[plane];
  380. const scan_order *sc = &vp9_default_scan_orders[tx_size];
  381. const int eob = vp9_decode_block_tokens(twd, plane, sc, col, row, tx_size,
  382. mi->segment_id);
  383. *pd->eob = eob;
  384. pd->dqcoeff += (16 << (tx_size << 1));
  385. pd->eob++;
  386. return eob;
  387. }
  388. static int reconstruct_inter_block_row_mt(TileWorkerData *twd,
  389. MODE_INFO *const mi, int plane,
  390. int row, int col, TX_SIZE tx_size) {
  391. MACROBLOCKD *const xd = &twd->xd;
  392. struct macroblockd_plane *const pd = &xd->plane[plane];
  393. const int eob = *pd->eob;
  394. (void)mi;
  395. if (eob > 0) {
  396. inverse_transform_block_inter(
  397. xd, plane, tx_size, &pd->dst.buf[4 * row * pd->dst.stride + 4 * col],
  398. pd->dst.stride, eob);
  399. }
  400. pd->dqcoeff += (16 << (tx_size << 1));
  401. pd->eob++;
  402. return eob;
  403. }
  404. static void build_mc_border(const uint8_t *src, int src_stride, uint8_t *dst,
  405. int dst_stride, int x, int y, int b_w, int b_h,
  406. int w, int h) {
  407. // Get a pointer to the start of the real data for this row.
  408. const uint8_t *ref_row = src - x - y * src_stride;
  409. if (y >= h)
  410. ref_row += (h - 1) * src_stride;
  411. else if (y > 0)
  412. ref_row += y * src_stride;
  413. do {
  414. int right = 0, copy;
  415. int left = x < 0 ? -x : 0;
  416. if (left > b_w) left = b_w;
  417. if (x + b_w > w) right = x + b_w - w;
  418. if (right > b_w) right = b_w;
  419. copy = b_w - left - right;
  420. if (left) memset(dst, ref_row[0], left);
  421. if (copy) memcpy(dst + left, ref_row + x + left, copy);
  422. if (right) memset(dst + left + copy, ref_row[w - 1], right);
  423. dst += dst_stride;
  424. ++y;
  425. if (y > 0 && y < h) ref_row += src_stride;
  426. } while (--b_h);
  427. }
  428. #if CONFIG_VP9_HIGHBITDEPTH
  429. static void high_build_mc_border(const uint8_t *src8, int src_stride,
  430. uint16_t *dst, int dst_stride, int x, int y,
  431. int b_w, int b_h, int w, int h) {
  432. // Get a pointer to the start of the real data for this row.
  433. const uint16_t *src = CONVERT_TO_SHORTPTR(src8);
  434. const uint16_t *ref_row = src - x - y * src_stride;
  435. if (y >= h)
  436. ref_row += (h - 1) * src_stride;
  437. else if (y > 0)
  438. ref_row += y * src_stride;
  439. do {
  440. int right = 0, copy;
  441. int left = x < 0 ? -x : 0;
  442. if (left > b_w) left = b_w;
  443. if (x + b_w > w) right = x + b_w - w;
  444. if (right > b_w) right = b_w;
  445. copy = b_w - left - right;
  446. if (left) vpx_memset16(dst, ref_row[0], left);
  447. if (copy) memcpy(dst + left, ref_row + x + left, copy * sizeof(uint16_t));
  448. if (right) vpx_memset16(dst + left + copy, ref_row[w - 1], right);
  449. dst += dst_stride;
  450. ++y;
  451. if (y > 0 && y < h) ref_row += src_stride;
  452. } while (--b_h);
  453. }
  454. #endif // CONFIG_VP9_HIGHBITDEPTH
  455. #if CONFIG_VP9_HIGHBITDEPTH
  456. static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
  457. int x0, int y0, int b_w, int b_h,
  458. int frame_width, int frame_height,
  459. int border_offset, uint8_t *const dst,
  460. int dst_buf_stride, int subpel_x, int subpel_y,
  461. const InterpKernel *kernel,
  462. const struct scale_factors *sf, MACROBLOCKD *xd,
  463. int w, int h, int ref, int xs, int ys) {
  464. DECLARE_ALIGNED(16, uint16_t, mc_buf_high[80 * 2 * 80 * 2]);
  465. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  466. high_build_mc_border(buf_ptr1, pre_buf_stride, mc_buf_high, b_w, x0, y0,
  467. b_w, b_h, frame_width, frame_height);
  468. highbd_inter_predictor(mc_buf_high + border_offset, b_w,
  469. CONVERT_TO_SHORTPTR(dst), dst_buf_stride, subpel_x,
  470. subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
  471. } else {
  472. build_mc_border(buf_ptr1, pre_buf_stride, (uint8_t *)mc_buf_high, b_w, x0,
  473. y0, b_w, b_h, frame_width, frame_height);
  474. inter_predictor(((uint8_t *)mc_buf_high) + border_offset, b_w, dst,
  475. dst_buf_stride, subpel_x, subpel_y, sf, w, h, ref, kernel,
  476. xs, ys);
  477. }
  478. }
  479. #else
  480. static void extend_and_predict(const uint8_t *buf_ptr1, int pre_buf_stride,
  481. int x0, int y0, int b_w, int b_h,
  482. int frame_width, int frame_height,
  483. int border_offset, uint8_t *const dst,
  484. int dst_buf_stride, int subpel_x, int subpel_y,
  485. const InterpKernel *kernel,
  486. const struct scale_factors *sf, int w, int h,
  487. int ref, int xs, int ys) {
  488. DECLARE_ALIGNED(16, uint8_t, mc_buf[80 * 2 * 80 * 2]);
  489. const uint8_t *buf_ptr;
  490. build_mc_border(buf_ptr1, pre_buf_stride, mc_buf, b_w, x0, y0, b_w, b_h,
  491. frame_width, frame_height);
  492. buf_ptr = mc_buf + border_offset;
  493. inter_predictor(buf_ptr, b_w, dst, dst_buf_stride, subpel_x, subpel_y, sf, w,
  494. h, ref, kernel, xs, ys);
  495. }
  496. #endif // CONFIG_VP9_HIGHBITDEPTH
  497. static void dec_build_inter_predictors(
  498. MACROBLOCKD *xd, int plane, int bw, int bh, int x, int y, int w, int h,
  499. int mi_x, int mi_y, const InterpKernel *kernel,
  500. const struct scale_factors *sf, struct buf_2d *pre_buf,
  501. struct buf_2d *dst_buf, const MV *mv, RefCntBuffer *ref_frame_buf,
  502. int is_scaled, int ref) {
  503. struct macroblockd_plane *const pd = &xd->plane[plane];
  504. uint8_t *const dst = dst_buf->buf + dst_buf->stride * y + x;
  505. MV32 scaled_mv;
  506. int xs, ys, x0, y0, x0_16, y0_16, frame_width, frame_height, buf_stride,
  507. subpel_x, subpel_y;
  508. uint8_t *ref_frame, *buf_ptr;
  509. // Get reference frame pointer, width and height.
  510. if (plane == 0) {
  511. frame_width = ref_frame_buf->buf.y_crop_width;
  512. frame_height = ref_frame_buf->buf.y_crop_height;
  513. ref_frame = ref_frame_buf->buf.y_buffer;
  514. } else {
  515. frame_width = ref_frame_buf->buf.uv_crop_width;
  516. frame_height = ref_frame_buf->buf.uv_crop_height;
  517. ref_frame =
  518. plane == 1 ? ref_frame_buf->buf.u_buffer : ref_frame_buf->buf.v_buffer;
  519. }
  520. if (is_scaled) {
  521. const MV mv_q4 = clamp_mv_to_umv_border_sb(
  522. xd, mv, bw, bh, pd->subsampling_x, pd->subsampling_y);
  523. // Co-ordinate of containing block to pixel precision.
  524. int x_start = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x));
  525. int y_start = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y));
  526. #if 0 // CONFIG_BETTER_HW_COMPATIBILITY
  527. assert(xd->mi[0]->sb_type != BLOCK_4X8 &&
  528. xd->mi[0]->sb_type != BLOCK_8X4);
  529. assert(mv_q4.row == mv->row * (1 << (1 - pd->subsampling_y)) &&
  530. mv_q4.col == mv->col * (1 << (1 - pd->subsampling_x)));
  531. #endif
  532. // Co-ordinate of the block to 1/16th pixel precision.
  533. x0_16 = (x_start + x) << SUBPEL_BITS;
  534. y0_16 = (y_start + y) << SUBPEL_BITS;
  535. // Co-ordinate of current block in reference frame
  536. // to 1/16th pixel precision.
  537. x0_16 = sf->scale_value_x(x0_16, sf);
  538. y0_16 = sf->scale_value_y(y0_16, sf);
  539. // Map the top left corner of the block into the reference frame.
  540. x0 = sf->scale_value_x(x_start + x, sf);
  541. y0 = sf->scale_value_y(y_start + y, sf);
  542. // Scale the MV and incorporate the sub-pixel offset of the block
  543. // in the reference frame.
  544. scaled_mv = vp9_scale_mv(&mv_q4, mi_x + x, mi_y + y, sf);
  545. xs = sf->x_step_q4;
  546. ys = sf->y_step_q4;
  547. } else {
  548. // Co-ordinate of containing block to pixel precision.
  549. x0 = (-xd->mb_to_left_edge >> (3 + pd->subsampling_x)) + x;
  550. y0 = (-xd->mb_to_top_edge >> (3 + pd->subsampling_y)) + y;
  551. // Co-ordinate of the block to 1/16th pixel precision.
  552. x0_16 = x0 << SUBPEL_BITS;
  553. y0_16 = y0 << SUBPEL_BITS;
  554. scaled_mv.row = mv->row * (1 << (1 - pd->subsampling_y));
  555. scaled_mv.col = mv->col * (1 << (1 - pd->subsampling_x));
  556. xs = ys = 16;
  557. }
  558. subpel_x = scaled_mv.col & SUBPEL_MASK;
  559. subpel_y = scaled_mv.row & SUBPEL_MASK;
  560. // Calculate the top left corner of the best matching block in the
  561. // reference frame.
  562. x0 += scaled_mv.col >> SUBPEL_BITS;
  563. y0 += scaled_mv.row >> SUBPEL_BITS;
  564. x0_16 += scaled_mv.col;
  565. y0_16 += scaled_mv.row;
  566. // Get reference block pointer.
  567. buf_ptr = ref_frame + y0 * pre_buf->stride + x0;
  568. buf_stride = pre_buf->stride;
  569. // Do border extension if there is motion or the
  570. // width/height is not a multiple of 8 pixels.
  571. if (is_scaled || scaled_mv.col || scaled_mv.row || (frame_width & 0x7) ||
  572. (frame_height & 0x7)) {
  573. int y1 = ((y0_16 + (h - 1) * ys) >> SUBPEL_BITS) + 1;
  574. // Get reference block bottom right horizontal coordinate.
  575. int x1 = ((x0_16 + (w - 1) * xs) >> SUBPEL_BITS) + 1;
  576. int x_pad = 0, y_pad = 0;
  577. if (subpel_x || (sf->x_step_q4 != SUBPEL_SHIFTS)) {
  578. x0 -= VP9_INTERP_EXTEND - 1;
  579. x1 += VP9_INTERP_EXTEND;
  580. x_pad = 1;
  581. }
  582. if (subpel_y || (sf->y_step_q4 != SUBPEL_SHIFTS)) {
  583. y0 -= VP9_INTERP_EXTEND - 1;
  584. y1 += VP9_INTERP_EXTEND;
  585. y_pad = 1;
  586. }
  587. // Skip border extension if block is inside the frame.
  588. if (x0 < 0 || x0 > frame_width - 1 || x1 < 0 || x1 > frame_width - 1 ||
  589. y0 < 0 || y0 > frame_height - 1 || y1 < 0 || y1 > frame_height - 1) {
  590. // Extend the border.
  591. const uint8_t *const buf_ptr1 = ref_frame + y0 * buf_stride + x0;
  592. const int b_w = x1 - x0 + 1;
  593. const int b_h = y1 - y0 + 1;
  594. const int border_offset = y_pad * 3 * b_w + x_pad * 3;
  595. extend_and_predict(buf_ptr1, buf_stride, x0, y0, b_w, b_h, frame_width,
  596. frame_height, border_offset, dst, dst_buf->stride,
  597. subpel_x, subpel_y, kernel, sf,
  598. #if CONFIG_VP9_HIGHBITDEPTH
  599. xd,
  600. #endif
  601. w, h, ref, xs, ys);
  602. return;
  603. }
  604. }
  605. #if CONFIG_VP9_HIGHBITDEPTH
  606. if (xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH) {
  607. highbd_inter_predictor(CONVERT_TO_SHORTPTR(buf_ptr), buf_stride,
  608. CONVERT_TO_SHORTPTR(dst), dst_buf->stride, subpel_x,
  609. subpel_y, sf, w, h, ref, kernel, xs, ys, xd->bd);
  610. } else {
  611. inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x,
  612. subpel_y, sf, w, h, ref, kernel, xs, ys);
  613. }
  614. #else
  615. inter_predictor(buf_ptr, buf_stride, dst, dst_buf->stride, subpel_x, subpel_y,
  616. sf, w, h, ref, kernel, xs, ys);
  617. #endif // CONFIG_VP9_HIGHBITDEPTH
  618. }
  619. static void dec_build_inter_predictors_sb(VP9Decoder *const pbi,
  620. MACROBLOCKD *xd, int mi_row,
  621. int mi_col) {
  622. int plane;
  623. const int mi_x = mi_col * MI_SIZE;
  624. const int mi_y = mi_row * MI_SIZE;
  625. const MODE_INFO *mi = xd->mi[0];
  626. const InterpKernel *kernel = vp9_filter_kernels[mi->interp_filter];
  627. const BLOCK_SIZE sb_type = mi->sb_type;
  628. const int is_compound = has_second_ref(mi);
  629. int ref;
  630. int is_scaled;
  631. for (ref = 0; ref < 1 + is_compound; ++ref) {
  632. const MV_REFERENCE_FRAME frame = mi->ref_frame[ref];
  633. RefBuffer *ref_buf = &pbi->common.frame_refs[frame - LAST_FRAME];
  634. const struct scale_factors *const sf = &ref_buf->sf;
  635. const int idx = ref_buf->idx;
  636. BufferPool *const pool = pbi->common.buffer_pool;
  637. RefCntBuffer *const ref_frame_buf = &pool->frame_bufs[idx];
  638. if (!vp9_is_valid_scale(sf))
  639. vpx_internal_error(xd->error_info, VPX_CODEC_UNSUP_BITSTREAM,
  640. "Reference frame has invalid dimensions");
  641. is_scaled = vp9_is_scaled(sf);
  642. vp9_setup_pre_planes(xd, ref, ref_buf->buf, mi_row, mi_col,
  643. is_scaled ? sf : NULL);
  644. xd->block_refs[ref] = ref_buf;
  645. if (sb_type < BLOCK_8X8) {
  646. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  647. struct macroblockd_plane *const pd = &xd->plane[plane];
  648. struct buf_2d *const dst_buf = &pd->dst;
  649. const int num_4x4_w = pd->n4_w;
  650. const int num_4x4_h = pd->n4_h;
  651. const int n4w_x4 = 4 * num_4x4_w;
  652. const int n4h_x4 = 4 * num_4x4_h;
  653. struct buf_2d *const pre_buf = &pd->pre[ref];
  654. int i = 0, x, y;
  655. for (y = 0; y < num_4x4_h; ++y) {
  656. for (x = 0; x < num_4x4_w; ++x) {
  657. const MV mv = average_split_mvs(pd, mi, ref, i++);
  658. dec_build_inter_predictors(xd, plane, n4w_x4, n4h_x4, 4 * x, 4 * y,
  659. 4, 4, mi_x, mi_y, kernel, sf, pre_buf,
  660. dst_buf, &mv, ref_frame_buf, is_scaled,
  661. ref);
  662. }
  663. }
  664. }
  665. } else {
  666. const MV mv = mi->mv[ref].as_mv;
  667. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  668. struct macroblockd_plane *const pd = &xd->plane[plane];
  669. struct buf_2d *const dst_buf = &pd->dst;
  670. const int num_4x4_w = pd->n4_w;
  671. const int num_4x4_h = pd->n4_h;
  672. const int n4w_x4 = 4 * num_4x4_w;
  673. const int n4h_x4 = 4 * num_4x4_h;
  674. struct buf_2d *const pre_buf = &pd->pre[ref];
  675. dec_build_inter_predictors(xd, plane, n4w_x4, n4h_x4, 0, 0, n4w_x4,
  676. n4h_x4, mi_x, mi_y, kernel, sf, pre_buf,
  677. dst_buf, &mv, ref_frame_buf, is_scaled, ref);
  678. }
  679. }
  680. }
  681. }
  682. static INLINE void dec_reset_skip_context(MACROBLOCKD *xd) {
  683. int i;
  684. for (i = 0; i < MAX_MB_PLANE; i++) {
  685. struct macroblockd_plane *const pd = &xd->plane[i];
  686. memset(pd->above_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_w);
  687. memset(pd->left_context, 0, sizeof(ENTROPY_CONTEXT) * pd->n4_h);
  688. }
  689. }
  690. static void set_plane_n4(MACROBLOCKD *const xd, int bw, int bh, int bwl,
  691. int bhl) {
  692. int i;
  693. for (i = 0; i < MAX_MB_PLANE; i++) {
  694. xd->plane[i].n4_w = (bw << 1) >> xd->plane[i].subsampling_x;
  695. xd->plane[i].n4_h = (bh << 1) >> xd->plane[i].subsampling_y;
  696. xd->plane[i].n4_wl = bwl - xd->plane[i].subsampling_x;
  697. xd->plane[i].n4_hl = bhl - xd->plane[i].subsampling_y;
  698. }
  699. }
  700. static MODE_INFO *set_offsets_recon(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  701. int mi_row, int mi_col, int bw, int bh,
  702. int bwl, int bhl) {
  703. const int offset = mi_row * cm->mi_stride + mi_col;
  704. const TileInfo *const tile = &xd->tile;
  705. xd->mi = cm->mi_grid_visible + offset;
  706. set_plane_n4(xd, bw, bh, bwl, bhl);
  707. set_skip_context(xd, mi_row, mi_col);
  708. // Distance of Mb to the various image edges. These are specified to 8th pel
  709. // as they are always compared to values that are in 1/8th pel units
  710. set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
  711. vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
  712. return xd->mi[0];
  713. }
  714. static MODE_INFO *set_offsets(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  715. BLOCK_SIZE bsize, int mi_row, int mi_col, int bw,
  716. int bh, int x_mis, int y_mis, int bwl, int bhl) {
  717. const int offset = mi_row * cm->mi_stride + mi_col;
  718. int x, y;
  719. const TileInfo *const tile = &xd->tile;
  720. xd->mi = cm->mi_grid_visible + offset;
  721. xd->mi[0] = &cm->mi[offset];
  722. // TODO(slavarnway): Generate sb_type based on bwl and bhl, instead of
  723. // passing bsize from decode_partition().
  724. xd->mi[0]->sb_type = bsize;
  725. for (y = 0; y < y_mis; ++y)
  726. for (x = !y; x < x_mis; ++x) {
  727. xd->mi[y * cm->mi_stride + x] = xd->mi[0];
  728. }
  729. set_plane_n4(xd, bw, bh, bwl, bhl);
  730. set_skip_context(xd, mi_row, mi_col);
  731. // Distance of Mb to the various image edges. These are specified to 8th pel
  732. // as they are always compared to values that are in 1/8th pel units
  733. set_mi_row_col(xd, tile, mi_row, bh, mi_col, bw, cm->mi_rows, cm->mi_cols);
  734. vp9_setup_dst_planes(xd->plane, get_frame_new_buffer(cm), mi_row, mi_col);
  735. return xd->mi[0];
  736. }
  737. static INLINE int predict_recon_inter(MACROBLOCKD *xd, MODE_INFO *mi,
  738. TileWorkerData *twd,
  739. predict_recon_func func) {
  740. int eobtotal = 0;
  741. int plane;
  742. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  743. const struct macroblockd_plane *const pd = &xd->plane[plane];
  744. const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
  745. const int num_4x4_w = pd->n4_w;
  746. const int num_4x4_h = pd->n4_h;
  747. const int step = (1 << tx_size);
  748. int row, col;
  749. const int max_blocks_wide =
  750. num_4x4_w + (xd->mb_to_right_edge >= 0
  751. ? 0
  752. : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  753. const int max_blocks_high =
  754. num_4x4_h + (xd->mb_to_bottom_edge >= 0
  755. ? 0
  756. : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  757. xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
  758. xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
  759. for (row = 0; row < max_blocks_high; row += step)
  760. for (col = 0; col < max_blocks_wide; col += step)
  761. eobtotal += func(twd, mi, plane, row, col, tx_size);
  762. }
  763. return eobtotal;
  764. }
  765. static INLINE void predict_recon_intra(MACROBLOCKD *xd, MODE_INFO *mi,
  766. TileWorkerData *twd,
  767. intra_recon_func func) {
  768. int plane;
  769. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  770. const struct macroblockd_plane *const pd = &xd->plane[plane];
  771. const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
  772. const int num_4x4_w = pd->n4_w;
  773. const int num_4x4_h = pd->n4_h;
  774. const int step = (1 << tx_size);
  775. int row, col;
  776. const int max_blocks_wide =
  777. num_4x4_w + (xd->mb_to_right_edge >= 0
  778. ? 0
  779. : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  780. const int max_blocks_high =
  781. num_4x4_h + (xd->mb_to_bottom_edge >= 0
  782. ? 0
  783. : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  784. xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
  785. xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
  786. for (row = 0; row < max_blocks_high; row += step)
  787. for (col = 0; col < max_blocks_wide; col += step)
  788. func(twd, mi, plane, row, col, tx_size);
  789. }
  790. }
  791. static void decode_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
  792. int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
  793. VP9_COMMON *const cm = &pbi->common;
  794. const int less8x8 = bsize < BLOCK_8X8;
  795. const int bw = 1 << (bwl - 1);
  796. const int bh = 1 << (bhl - 1);
  797. const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
  798. const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
  799. vpx_reader *r = &twd->bit_reader;
  800. MACROBLOCKD *const xd = &twd->xd;
  801. MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
  802. y_mis, bwl, bhl);
  803. if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
  804. const BLOCK_SIZE uv_subsize =
  805. ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
  806. if (uv_subsize == BLOCK_INVALID)
  807. vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
  808. "Invalid block size.");
  809. }
  810. vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
  811. if (mi->skip) {
  812. dec_reset_skip_context(xd);
  813. }
  814. if (!is_inter_block(mi)) {
  815. int plane;
  816. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  817. const struct macroblockd_plane *const pd = &xd->plane[plane];
  818. const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
  819. const int num_4x4_w = pd->n4_w;
  820. const int num_4x4_h = pd->n4_h;
  821. const int step = (1 << tx_size);
  822. int row, col;
  823. const int max_blocks_wide =
  824. num_4x4_w + (xd->mb_to_right_edge >= 0
  825. ? 0
  826. : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  827. const int max_blocks_high =
  828. num_4x4_h + (xd->mb_to_bottom_edge >= 0
  829. ? 0
  830. : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  831. xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
  832. xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
  833. for (row = 0; row < max_blocks_high; row += step)
  834. for (col = 0; col < max_blocks_wide; col += step)
  835. predict_and_reconstruct_intra_block(twd, mi, plane, row, col,
  836. tx_size);
  837. }
  838. } else {
  839. // Prediction
  840. dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
  841. #if CONFIG_MISMATCH_DEBUG
  842. {
  843. int plane;
  844. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  845. const struct macroblockd_plane *pd = &xd->plane[plane];
  846. int pixel_c, pixel_r;
  847. const BLOCK_SIZE plane_bsize =
  848. get_plane_block_size(VPXMAX(bsize, BLOCK_8X8), &xd->plane[plane]);
  849. const int bw = get_block_width(plane_bsize);
  850. const int bh = get_block_height(plane_bsize);
  851. mi_to_pixel_loc(&pixel_c, &pixel_r, mi_col, mi_row, 0, 0,
  852. pd->subsampling_x, pd->subsampling_y);
  853. mismatch_check_block_pre(pd->dst.buf, pd->dst.stride, plane, pixel_c,
  854. pixel_r, bw, bh,
  855. xd->cur_buf->flags & YV12_FLAG_HIGHBITDEPTH);
  856. }
  857. }
  858. #endif
  859. // Reconstruction
  860. if (!mi->skip) {
  861. int eobtotal = 0;
  862. int plane;
  863. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  864. const struct macroblockd_plane *const pd = &xd->plane[plane];
  865. const TX_SIZE tx_size = plane ? get_uv_tx_size(mi, pd) : mi->tx_size;
  866. const int num_4x4_w = pd->n4_w;
  867. const int num_4x4_h = pd->n4_h;
  868. const int step = (1 << tx_size);
  869. int row, col;
  870. const int max_blocks_wide =
  871. num_4x4_w + (xd->mb_to_right_edge >= 0
  872. ? 0
  873. : xd->mb_to_right_edge >> (5 + pd->subsampling_x));
  874. const int max_blocks_high =
  875. num_4x4_h +
  876. (xd->mb_to_bottom_edge >= 0
  877. ? 0
  878. : xd->mb_to_bottom_edge >> (5 + pd->subsampling_y));
  879. xd->max_blocks_wide = xd->mb_to_right_edge >= 0 ? 0 : max_blocks_wide;
  880. xd->max_blocks_high = xd->mb_to_bottom_edge >= 0 ? 0 : max_blocks_high;
  881. for (row = 0; row < max_blocks_high; row += step)
  882. for (col = 0; col < max_blocks_wide; col += step)
  883. eobtotal += reconstruct_inter_block(twd, mi, plane, row, col,
  884. tx_size, mi_row, mi_col);
  885. }
  886. if (!less8x8 && eobtotal == 0) mi->skip = 1; // skip loopfilter
  887. }
  888. }
  889. xd->corrupted |= vpx_reader_has_error(r);
  890. if (cm->lf.filter_level) {
  891. vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
  892. }
  893. }
  894. static void recon_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
  895. int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
  896. VP9_COMMON *const cm = &pbi->common;
  897. const int bw = 1 << (bwl - 1);
  898. const int bh = 1 << (bhl - 1);
  899. MACROBLOCKD *const xd = &twd->xd;
  900. MODE_INFO *mi = set_offsets_recon(cm, xd, mi_row, mi_col, bw, bh, bwl, bhl);
  901. if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
  902. const BLOCK_SIZE uv_subsize =
  903. ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
  904. if (uv_subsize == BLOCK_INVALID)
  905. vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
  906. "Invalid block size.");
  907. }
  908. if (!is_inter_block(mi)) {
  909. predict_recon_intra(xd, mi, twd,
  910. predict_and_reconstruct_intra_block_row_mt);
  911. } else {
  912. // Prediction
  913. dec_build_inter_predictors_sb(pbi, xd, mi_row, mi_col);
  914. // Reconstruction
  915. if (!mi->skip) {
  916. predict_recon_inter(xd, mi, twd, reconstruct_inter_block_row_mt);
  917. }
  918. }
  919. vp9_build_mask(cm, mi, mi_row, mi_col, bw, bh);
  920. }
  921. static void parse_block(TileWorkerData *twd, VP9Decoder *const pbi, int mi_row,
  922. int mi_col, BLOCK_SIZE bsize, int bwl, int bhl) {
  923. VP9_COMMON *const cm = &pbi->common;
  924. const int bw = 1 << (bwl - 1);
  925. const int bh = 1 << (bhl - 1);
  926. const int x_mis = VPXMIN(bw, cm->mi_cols - mi_col);
  927. const int y_mis = VPXMIN(bh, cm->mi_rows - mi_row);
  928. vpx_reader *r = &twd->bit_reader;
  929. MACROBLOCKD *const xd = &twd->xd;
  930. MODE_INFO *mi = set_offsets(cm, xd, bsize, mi_row, mi_col, bw, bh, x_mis,
  931. y_mis, bwl, bhl);
  932. if (bsize >= BLOCK_8X8 && (cm->subsampling_x || cm->subsampling_y)) {
  933. const BLOCK_SIZE uv_subsize =
  934. ss_size_lookup[bsize][cm->subsampling_x][cm->subsampling_y];
  935. if (uv_subsize == BLOCK_INVALID)
  936. vpx_internal_error(xd->error_info, VPX_CODEC_CORRUPT_FRAME,
  937. "Invalid block size.");
  938. }
  939. vp9_read_mode_info(twd, pbi, mi_row, mi_col, x_mis, y_mis);
  940. if (mi->skip) {
  941. dec_reset_skip_context(xd);
  942. }
  943. if (!is_inter_block(mi)) {
  944. predict_recon_intra(xd, mi, twd, parse_intra_block_row_mt);
  945. } else {
  946. if (!mi->skip) {
  947. tran_low_t *dqcoeff[MAX_MB_PLANE];
  948. int *eob[MAX_MB_PLANE];
  949. int plane;
  950. int eobtotal;
  951. // Based on eobtotal and bsize, this may be mi->skip may be set to true
  952. // In that case dqcoeff and eob need to be backed up and restored as
  953. // recon_block will not increment these pointers for skip cases
  954. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  955. const struct macroblockd_plane *const pd = &xd->plane[plane];
  956. dqcoeff[plane] = pd->dqcoeff;
  957. eob[plane] = pd->eob;
  958. }
  959. eobtotal = predict_recon_inter(xd, mi, twd, parse_inter_block_row_mt);
  960. if (bsize >= BLOCK_8X8 && eobtotal == 0) {
  961. mi->skip = 1; // skip loopfilter
  962. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  963. struct macroblockd_plane *pd = &xd->plane[plane];
  964. pd->dqcoeff = dqcoeff[plane];
  965. pd->eob = eob[plane];
  966. }
  967. }
  968. }
  969. }
  970. xd->corrupted |= vpx_reader_has_error(r);
  971. }
  972. static INLINE int dec_partition_plane_context(TileWorkerData *twd, int mi_row,
  973. int mi_col, int bsl) {
  974. const PARTITION_CONTEXT *above_ctx = twd->xd.above_seg_context + mi_col;
  975. const PARTITION_CONTEXT *left_ctx =
  976. twd->xd.left_seg_context + (mi_row & MI_MASK);
  977. int above = (*above_ctx >> bsl) & 1, left = (*left_ctx >> bsl) & 1;
  978. // assert(bsl >= 0);
  979. return (left * 2 + above) + bsl * PARTITION_PLOFFSET;
  980. }
  981. static INLINE void dec_update_partition_context(TileWorkerData *twd, int mi_row,
  982. int mi_col, BLOCK_SIZE subsize,
  983. int bw) {
  984. PARTITION_CONTEXT *const above_ctx = twd->xd.above_seg_context + mi_col;
  985. PARTITION_CONTEXT *const left_ctx =
  986. twd->xd.left_seg_context + (mi_row & MI_MASK);
  987. // update the partition context at the end notes. set partition bits
  988. // of block sizes larger than the current one to be one, and partition
  989. // bits of smaller block sizes to be zero.
  990. memset(above_ctx, partition_context_lookup[subsize].above, bw);
  991. memset(left_ctx, partition_context_lookup[subsize].left, bw);
  992. }
  993. static PARTITION_TYPE read_partition(TileWorkerData *twd, int mi_row,
  994. int mi_col, int has_rows, int has_cols,
  995. int bsl) {
  996. const int ctx = dec_partition_plane_context(twd, mi_row, mi_col, bsl);
  997. const vpx_prob *const probs = twd->xd.partition_probs[ctx];
  998. FRAME_COUNTS *counts = twd->xd.counts;
  999. PARTITION_TYPE p;
  1000. vpx_reader *r = &twd->bit_reader;
  1001. if (has_rows && has_cols)
  1002. p = (PARTITION_TYPE)vpx_read_tree(r, vp9_partition_tree, probs);
  1003. else if (!has_rows && has_cols)
  1004. p = vpx_read(r, probs[1]) ? PARTITION_SPLIT : PARTITION_HORZ;
  1005. else if (has_rows && !has_cols)
  1006. p = vpx_read(r, probs[2]) ? PARTITION_SPLIT : PARTITION_VERT;
  1007. else
  1008. p = PARTITION_SPLIT;
  1009. if (counts) ++counts->partition[ctx][p];
  1010. return p;
  1011. }
  1012. // TODO(slavarnway): eliminate bsize and subsize in future commits
  1013. static void decode_partition(TileWorkerData *twd, VP9Decoder *const pbi,
  1014. int mi_row, int mi_col, BLOCK_SIZE bsize,
  1015. int n4x4_l2) {
  1016. VP9_COMMON *const cm = &pbi->common;
  1017. const int n8x8_l2 = n4x4_l2 - 1;
  1018. const int num_8x8_wh = 1 << n8x8_l2;
  1019. const int hbs = num_8x8_wh >> 1;
  1020. PARTITION_TYPE partition;
  1021. BLOCK_SIZE subsize;
  1022. const int has_rows = (mi_row + hbs) < cm->mi_rows;
  1023. const int has_cols = (mi_col + hbs) < cm->mi_cols;
  1024. MACROBLOCKD *const xd = &twd->xd;
  1025. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  1026. partition = read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
  1027. subsize = subsize_lookup[partition][bsize]; // get_subsize(bsize, partition);
  1028. if (!hbs) {
  1029. // calculate bmode block dimensions (log 2)
  1030. xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
  1031. xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
  1032. decode_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
  1033. } else {
  1034. switch (partition) {
  1035. case PARTITION_NONE:
  1036. decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
  1037. break;
  1038. case PARTITION_HORZ:
  1039. decode_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
  1040. if (has_rows)
  1041. decode_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
  1042. n8x8_l2);
  1043. break;
  1044. case PARTITION_VERT:
  1045. decode_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
  1046. if (has_cols)
  1047. decode_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
  1048. n4x4_l2);
  1049. break;
  1050. case PARTITION_SPLIT:
  1051. decode_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2);
  1052. decode_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2);
  1053. decode_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2);
  1054. decode_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
  1055. n8x8_l2);
  1056. break;
  1057. default: assert(0 && "Invalid partition type");
  1058. }
  1059. }
  1060. // update partition context
  1061. if (bsize >= BLOCK_8X8 &&
  1062. (bsize == BLOCK_8X8 || partition != PARTITION_SPLIT))
  1063. dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
  1064. }
  1065. static void process_partition(TileWorkerData *twd, VP9Decoder *const pbi,
  1066. int mi_row, int mi_col, BLOCK_SIZE bsize,
  1067. int n4x4_l2, int parse_recon_flag,
  1068. process_block_fn_t process_block) {
  1069. VP9_COMMON *const cm = &pbi->common;
  1070. const int n8x8_l2 = n4x4_l2 - 1;
  1071. const int num_8x8_wh = 1 << n8x8_l2;
  1072. const int hbs = num_8x8_wh >> 1;
  1073. PARTITION_TYPE partition;
  1074. BLOCK_SIZE subsize;
  1075. const int has_rows = (mi_row + hbs) < cm->mi_rows;
  1076. const int has_cols = (mi_col + hbs) < cm->mi_cols;
  1077. MACROBLOCKD *const xd = &twd->xd;
  1078. if (mi_row >= cm->mi_rows || mi_col >= cm->mi_cols) return;
  1079. if (parse_recon_flag & PARSE) {
  1080. *xd->partition =
  1081. read_partition(twd, mi_row, mi_col, has_rows, has_cols, n8x8_l2);
  1082. }
  1083. partition = *xd->partition;
  1084. xd->partition++;
  1085. subsize = get_subsize(bsize, partition);
  1086. if (!hbs) {
  1087. // calculate bmode block dimensions (log 2)
  1088. xd->bmode_blocks_wl = 1 >> !!(partition & PARTITION_VERT);
  1089. xd->bmode_blocks_hl = 1 >> !!(partition & PARTITION_HORZ);
  1090. process_block(twd, pbi, mi_row, mi_col, subsize, 1, 1);
  1091. } else {
  1092. switch (partition) {
  1093. case PARTITION_NONE:
  1094. process_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n4x4_l2);
  1095. break;
  1096. case PARTITION_HORZ:
  1097. process_block(twd, pbi, mi_row, mi_col, subsize, n4x4_l2, n8x8_l2);
  1098. if (has_rows)
  1099. process_block(twd, pbi, mi_row + hbs, mi_col, subsize, n4x4_l2,
  1100. n8x8_l2);
  1101. break;
  1102. case PARTITION_VERT:
  1103. process_block(twd, pbi, mi_row, mi_col, subsize, n8x8_l2, n4x4_l2);
  1104. if (has_cols)
  1105. process_block(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
  1106. n4x4_l2);
  1107. break;
  1108. case PARTITION_SPLIT:
  1109. process_partition(twd, pbi, mi_row, mi_col, subsize, n8x8_l2,
  1110. parse_recon_flag, process_block);
  1111. process_partition(twd, pbi, mi_row, mi_col + hbs, subsize, n8x8_l2,
  1112. parse_recon_flag, process_block);
  1113. process_partition(twd, pbi, mi_row + hbs, mi_col, subsize, n8x8_l2,
  1114. parse_recon_flag, process_block);
  1115. process_partition(twd, pbi, mi_row + hbs, mi_col + hbs, subsize,
  1116. n8x8_l2, parse_recon_flag, process_block);
  1117. break;
  1118. default: assert(0 && "Invalid partition type");
  1119. }
  1120. }
  1121. if (parse_recon_flag & PARSE) {
  1122. // update partition context
  1123. if ((bsize == BLOCK_8X8 || partition != PARTITION_SPLIT) &&
  1124. bsize >= BLOCK_8X8)
  1125. dec_update_partition_context(twd, mi_row, mi_col, subsize, num_8x8_wh);
  1126. }
  1127. }
  1128. static void setup_token_decoder(const uint8_t *data, const uint8_t *data_end,
  1129. size_t read_size,
  1130. struct vpx_internal_error_info *error_info,
  1131. vpx_reader *r, vpx_decrypt_cb decrypt_cb,
  1132. void *decrypt_state) {
  1133. // Validate the calculated partition length. If the buffer
  1134. // described by the partition can't be fully read, then restrict
  1135. // it to the portion that can be (for EC mode) or throw an error.
  1136. if (!read_is_valid(data, read_size, data_end))
  1137. vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
  1138. "Truncated packet or corrupt tile length");
  1139. if (vpx_reader_init(r, data, read_size, decrypt_cb, decrypt_state))
  1140. vpx_internal_error(error_info, VPX_CODEC_MEM_ERROR,
  1141. "Failed to allocate bool decoder %d", 1);
  1142. }
  1143. static void read_coef_probs_common(vp9_coeff_probs_model *coef_probs,
  1144. vpx_reader *r) {
  1145. int i, j, k, l, m;
  1146. if (vpx_read_bit(r))
  1147. for (i = 0; i < PLANE_TYPES; ++i)
  1148. for (j = 0; j < REF_TYPES; ++j)
  1149. for (k = 0; k < COEF_BANDS; ++k)
  1150. for (l = 0; l < BAND_COEFF_CONTEXTS(k); ++l)
  1151. for (m = 0; m < UNCONSTRAINED_NODES; ++m)
  1152. vp9_diff_update_prob(r, &coef_probs[i][j][k][l][m]);
  1153. }
  1154. static void read_coef_probs(FRAME_CONTEXT *fc, TX_MODE tx_mode, vpx_reader *r) {
  1155. const TX_SIZE max_tx_size = tx_mode_to_biggest_tx_size[tx_mode];
  1156. TX_SIZE tx_size;
  1157. for (tx_size = TX_4X4; tx_size <= max_tx_size; ++tx_size)
  1158. read_coef_probs_common(fc->coef_probs[tx_size], r);
  1159. }
  1160. static void setup_segmentation(struct segmentation *seg,
  1161. struct vpx_read_bit_buffer *rb) {
  1162. int i, j;
  1163. seg->update_map = 0;
  1164. seg->update_data = 0;
  1165. seg->enabled = vpx_rb_read_bit(rb);
  1166. if (!seg->enabled) return;
  1167. // Segmentation map update
  1168. seg->update_map = vpx_rb_read_bit(rb);
  1169. if (seg->update_map) {
  1170. for (i = 0; i < SEG_TREE_PROBS; i++)
  1171. seg->tree_probs[i] =
  1172. vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
  1173. seg->temporal_update = vpx_rb_read_bit(rb);
  1174. if (seg->temporal_update) {
  1175. for (i = 0; i < PREDICTION_PROBS; i++)
  1176. seg->pred_probs[i] =
  1177. vpx_rb_read_bit(rb) ? vpx_rb_read_literal(rb, 8) : MAX_PROB;
  1178. } else {
  1179. for (i = 0; i < PREDICTION_PROBS; i++) seg->pred_probs[i] = MAX_PROB;
  1180. }
  1181. }
  1182. // Segmentation data update
  1183. seg->update_data = vpx_rb_read_bit(rb);
  1184. if (seg->update_data) {
  1185. seg->abs_delta = vpx_rb_read_bit(rb);
  1186. vp9_clearall_segfeatures(seg);
  1187. for (i = 0; i < MAX_SEGMENTS; i++) {
  1188. for (j = 0; j < SEG_LVL_MAX; j++) {
  1189. int data = 0;
  1190. const int feature_enabled = vpx_rb_read_bit(rb);
  1191. if (feature_enabled) {
  1192. vp9_enable_segfeature(seg, i, j);
  1193. data = decode_unsigned_max(rb, vp9_seg_feature_data_max(j));
  1194. if (vp9_is_segfeature_signed(j))
  1195. data = vpx_rb_read_bit(rb) ? -data : data;
  1196. }
  1197. vp9_set_segdata(seg, i, j, data);
  1198. }
  1199. }
  1200. }
  1201. }
  1202. static void setup_loopfilter(struct loopfilter *lf,
  1203. struct vpx_read_bit_buffer *rb) {
  1204. lf->filter_level = vpx_rb_read_literal(rb, 6);
  1205. lf->sharpness_level = vpx_rb_read_literal(rb, 3);
  1206. // Read in loop filter deltas applied at the MB level based on mode or ref
  1207. // frame.
  1208. lf->mode_ref_delta_update = 0;
  1209. lf->mode_ref_delta_enabled = vpx_rb_read_bit(rb);
  1210. if (lf->mode_ref_delta_enabled) {
  1211. lf->mode_ref_delta_update = vpx_rb_read_bit(rb);
  1212. if (lf->mode_ref_delta_update) {
  1213. int i;
  1214. for (i = 0; i < MAX_REF_LF_DELTAS; i++)
  1215. if (vpx_rb_read_bit(rb))
  1216. lf->ref_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
  1217. for (i = 0; i < MAX_MODE_LF_DELTAS; i++)
  1218. if (vpx_rb_read_bit(rb))
  1219. lf->mode_deltas[i] = vpx_rb_read_signed_literal(rb, 6);
  1220. }
  1221. }
  1222. }
  1223. static INLINE int read_delta_q(struct vpx_read_bit_buffer *rb) {
  1224. return vpx_rb_read_bit(rb) ? vpx_rb_read_signed_literal(rb, 4) : 0;
  1225. }
  1226. static void setup_quantization(VP9_COMMON *const cm, MACROBLOCKD *const xd,
  1227. struct vpx_read_bit_buffer *rb) {
  1228. cm->base_qindex = vpx_rb_read_literal(rb, QINDEX_BITS);
  1229. cm->y_dc_delta_q = read_delta_q(rb);
  1230. cm->uv_dc_delta_q = read_delta_q(rb);
  1231. cm->uv_ac_delta_q = read_delta_q(rb);
  1232. cm->dequant_bit_depth = cm->bit_depth;
  1233. xd->lossless = cm->base_qindex == 0 && cm->y_dc_delta_q == 0 &&
  1234. cm->uv_dc_delta_q == 0 && cm->uv_ac_delta_q == 0;
  1235. #if CONFIG_VP9_HIGHBITDEPTH
  1236. xd->bd = (int)cm->bit_depth;
  1237. #endif
  1238. }
  1239. static void setup_segmentation_dequant(VP9_COMMON *const cm) {
  1240. // Build y/uv dequant values based on segmentation.
  1241. if (cm->seg.enabled) {
  1242. int i;
  1243. for (i = 0; i < MAX_SEGMENTS; ++i) {
  1244. const int qindex = vp9_get_qindex(&cm->seg, i, cm->base_qindex);
  1245. cm->y_dequant[i][0] =
  1246. vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
  1247. cm->y_dequant[i][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
  1248. cm->uv_dequant[i][0] =
  1249. vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
  1250. cm->uv_dequant[i][1] =
  1251. vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
  1252. }
  1253. } else {
  1254. const int qindex = cm->base_qindex;
  1255. // When segmentation is disabled, only the first value is used. The
  1256. // remaining are don't cares.
  1257. cm->y_dequant[0][0] = vp9_dc_quant(qindex, cm->y_dc_delta_q, cm->bit_depth);
  1258. cm->y_dequant[0][1] = vp9_ac_quant(qindex, 0, cm->bit_depth);
  1259. cm->uv_dequant[0][0] =
  1260. vp9_dc_quant(qindex, cm->uv_dc_delta_q, cm->bit_depth);
  1261. cm->uv_dequant[0][1] =
  1262. vp9_ac_quant(qindex, cm->uv_ac_delta_q, cm->bit_depth);
  1263. }
  1264. }
  1265. static INTERP_FILTER read_interp_filter(struct vpx_read_bit_buffer *rb) {
  1266. const INTERP_FILTER literal_to_filter[] = { EIGHTTAP_SMOOTH, EIGHTTAP,
  1267. EIGHTTAP_SHARP, BILINEAR };
  1268. return vpx_rb_read_bit(rb) ? SWITCHABLE
  1269. : literal_to_filter[vpx_rb_read_literal(rb, 2)];
  1270. }
  1271. static void setup_render_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1272. cm->render_width = cm->width;
  1273. cm->render_height = cm->height;
  1274. if (vpx_rb_read_bit(rb))
  1275. vp9_read_frame_size(rb, &cm->render_width, &cm->render_height);
  1276. }
  1277. static void resize_mv_buffer(VP9_COMMON *cm) {
  1278. vpx_free(cm->cur_frame->mvs);
  1279. cm->cur_frame->mi_rows = cm->mi_rows;
  1280. cm->cur_frame->mi_cols = cm->mi_cols;
  1281. CHECK_MEM_ERROR(cm, cm->cur_frame->mvs,
  1282. (MV_REF *)vpx_calloc(cm->mi_rows * cm->mi_cols,
  1283. sizeof(*cm->cur_frame->mvs)));
  1284. }
  1285. static void resize_context_buffers(VP9_COMMON *cm, int width, int height) {
  1286. #if CONFIG_SIZE_LIMIT
  1287. if (width > DECODE_WIDTH_LIMIT || height > DECODE_HEIGHT_LIMIT)
  1288. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1289. "Dimensions of %dx%d beyond allowed size of %dx%d.",
  1290. width, height, DECODE_WIDTH_LIMIT, DECODE_HEIGHT_LIMIT);
  1291. #endif
  1292. if (cm->width != width || cm->height != height) {
  1293. const int new_mi_rows =
  1294. ALIGN_POWER_OF_TWO(height, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
  1295. const int new_mi_cols =
  1296. ALIGN_POWER_OF_TWO(width, MI_SIZE_LOG2) >> MI_SIZE_LOG2;
  1297. // Allocations in vp9_alloc_context_buffers() depend on individual
  1298. // dimensions as well as the overall size.
  1299. if (new_mi_cols > cm->mi_cols || new_mi_rows > cm->mi_rows) {
  1300. if (vp9_alloc_context_buffers(cm, width, height)) {
  1301. // The cm->mi_* values have been cleared and any existing context
  1302. // buffers have been freed. Clear cm->width and cm->height to be
  1303. // consistent and to force a realloc next time.
  1304. cm->width = 0;
  1305. cm->height = 0;
  1306. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1307. "Failed to allocate context buffers");
  1308. }
  1309. } else {
  1310. vp9_set_mb_mi(cm, width, height);
  1311. }
  1312. vp9_init_context_buffers(cm);
  1313. cm->width = width;
  1314. cm->height = height;
  1315. }
  1316. if (cm->cur_frame->mvs == NULL || cm->mi_rows > cm->cur_frame->mi_rows ||
  1317. cm->mi_cols > cm->cur_frame->mi_cols) {
  1318. resize_mv_buffer(cm);
  1319. }
  1320. }
  1321. static void setup_frame_size(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1322. int width, height;
  1323. BufferPool *const pool = cm->buffer_pool;
  1324. vp9_read_frame_size(rb, &width, &height);
  1325. resize_context_buffers(cm, width, height);
  1326. setup_render_size(cm, rb);
  1327. if (vpx_realloc_frame_buffer(
  1328. get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
  1329. cm->subsampling_y,
  1330. #if CONFIG_VP9_HIGHBITDEPTH
  1331. cm->use_highbitdepth,
  1332. #endif
  1333. VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
  1334. &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
  1335. pool->cb_priv)) {
  1336. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1337. "Failed to allocate frame buffer");
  1338. }
  1339. pool->frame_bufs[cm->new_fb_idx].released = 0;
  1340. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
  1341. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
  1342. pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
  1343. pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
  1344. pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
  1345. pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
  1346. pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
  1347. }
  1348. static INLINE int valid_ref_frame_img_fmt(vpx_bit_depth_t ref_bit_depth,
  1349. int ref_xss, int ref_yss,
  1350. vpx_bit_depth_t this_bit_depth,
  1351. int this_xss, int this_yss) {
  1352. return ref_bit_depth == this_bit_depth && ref_xss == this_xss &&
  1353. ref_yss == this_yss;
  1354. }
  1355. static void setup_frame_size_with_refs(VP9_COMMON *cm,
  1356. struct vpx_read_bit_buffer *rb) {
  1357. int width, height;
  1358. int found = 0, i;
  1359. int has_valid_ref_frame = 0;
  1360. BufferPool *const pool = cm->buffer_pool;
  1361. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1362. if (vpx_rb_read_bit(rb)) {
  1363. if (cm->frame_refs[i].idx != INVALID_IDX) {
  1364. YV12_BUFFER_CONFIG *const buf = cm->frame_refs[i].buf;
  1365. width = buf->y_crop_width;
  1366. height = buf->y_crop_height;
  1367. found = 1;
  1368. break;
  1369. } else {
  1370. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1371. "Failed to decode frame size");
  1372. }
  1373. }
  1374. }
  1375. if (!found) vp9_read_frame_size(rb, &width, &height);
  1376. if (width <= 0 || height <= 0)
  1377. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1378. "Invalid frame size");
  1379. // Check to make sure at least one of frames that this frame references
  1380. // has valid dimensions.
  1381. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1382. RefBuffer *const ref_frame = &cm->frame_refs[i];
  1383. has_valid_ref_frame |=
  1384. (ref_frame->idx != INVALID_IDX &&
  1385. valid_ref_frame_size(ref_frame->buf->y_crop_width,
  1386. ref_frame->buf->y_crop_height, width, height));
  1387. }
  1388. if (!has_valid_ref_frame)
  1389. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1390. "Referenced frame has invalid size");
  1391. for (i = 0; i < REFS_PER_FRAME; ++i) {
  1392. RefBuffer *const ref_frame = &cm->frame_refs[i];
  1393. if (ref_frame->idx == INVALID_IDX ||
  1394. !valid_ref_frame_img_fmt(ref_frame->buf->bit_depth,
  1395. ref_frame->buf->subsampling_x,
  1396. ref_frame->buf->subsampling_y, cm->bit_depth,
  1397. cm->subsampling_x, cm->subsampling_y))
  1398. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1399. "Referenced frame has incompatible color format");
  1400. }
  1401. resize_context_buffers(cm, width, height);
  1402. setup_render_size(cm, rb);
  1403. if (vpx_realloc_frame_buffer(
  1404. get_frame_new_buffer(cm), cm->width, cm->height, cm->subsampling_x,
  1405. cm->subsampling_y,
  1406. #if CONFIG_VP9_HIGHBITDEPTH
  1407. cm->use_highbitdepth,
  1408. #endif
  1409. VP9_DEC_BORDER_IN_PIXELS, cm->byte_alignment,
  1410. &pool->frame_bufs[cm->new_fb_idx].raw_frame_buffer, pool->get_fb_cb,
  1411. pool->cb_priv)) {
  1412. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  1413. "Failed to allocate frame buffer");
  1414. }
  1415. pool->frame_bufs[cm->new_fb_idx].released = 0;
  1416. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_x = cm->subsampling_x;
  1417. pool->frame_bufs[cm->new_fb_idx].buf.subsampling_y = cm->subsampling_y;
  1418. pool->frame_bufs[cm->new_fb_idx].buf.bit_depth = (unsigned int)cm->bit_depth;
  1419. pool->frame_bufs[cm->new_fb_idx].buf.color_space = cm->color_space;
  1420. pool->frame_bufs[cm->new_fb_idx].buf.color_range = cm->color_range;
  1421. pool->frame_bufs[cm->new_fb_idx].buf.render_width = cm->render_width;
  1422. pool->frame_bufs[cm->new_fb_idx].buf.render_height = cm->render_height;
  1423. }
  1424. static void setup_tile_info(VP9_COMMON *cm, struct vpx_read_bit_buffer *rb) {
  1425. int min_log2_tile_cols, max_log2_tile_cols, max_ones;
  1426. vp9_get_tile_n_bits(cm->mi_cols, &min_log2_tile_cols, &max_log2_tile_cols);
  1427. // columns
  1428. max_ones = max_log2_tile_cols - min_log2_tile_cols;
  1429. cm->log2_tile_cols = min_log2_tile_cols;
  1430. while (max_ones-- && vpx_rb_read_bit(rb)) cm->log2_tile_cols++;
  1431. if (cm->log2_tile_cols > 6)
  1432. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1433. "Invalid number of tile columns");
  1434. // rows
  1435. cm->log2_tile_rows = vpx_rb_read_bit(rb);
  1436. if (cm->log2_tile_rows) cm->log2_tile_rows += vpx_rb_read_bit(rb);
  1437. }
  1438. // Reads the next tile returning its size and adjusting '*data' accordingly
  1439. // based on 'is_last'.
  1440. static void get_tile_buffer(const uint8_t *const data_end, int is_last,
  1441. struct vpx_internal_error_info *error_info,
  1442. const uint8_t **data, vpx_decrypt_cb decrypt_cb,
  1443. void *decrypt_state, TileBuffer *buf) {
  1444. size_t size;
  1445. if (!is_last) {
  1446. if (!read_is_valid(*data, 4, data_end))
  1447. vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
  1448. "Truncated packet or corrupt tile length");
  1449. if (decrypt_cb) {
  1450. uint8_t be_data[4];
  1451. decrypt_cb(decrypt_state, *data, be_data, 4);
  1452. size = mem_get_be32(be_data);
  1453. } else {
  1454. size = mem_get_be32(*data);
  1455. }
  1456. *data += 4;
  1457. if (size > (size_t)(data_end - *data))
  1458. vpx_internal_error(error_info, VPX_CODEC_CORRUPT_FRAME,
  1459. "Truncated packet or corrupt tile size");
  1460. } else {
  1461. size = data_end - *data;
  1462. }
  1463. buf->data = *data;
  1464. buf->size = size;
  1465. *data += size;
  1466. }
  1467. static void get_tile_buffers(VP9Decoder *pbi, const uint8_t *data,
  1468. const uint8_t *data_end, int tile_cols,
  1469. int tile_rows,
  1470. TileBuffer (*tile_buffers)[1 << 6]) {
  1471. int r, c;
  1472. for (r = 0; r < tile_rows; ++r) {
  1473. for (c = 0; c < tile_cols; ++c) {
  1474. const int is_last = (r == tile_rows - 1) && (c == tile_cols - 1);
  1475. TileBuffer *const buf = &tile_buffers[r][c];
  1476. buf->col = c;
  1477. get_tile_buffer(data_end, is_last, &pbi->common.error, &data,
  1478. pbi->decrypt_cb, pbi->decrypt_state, buf);
  1479. }
  1480. }
  1481. }
  1482. static void map_write(RowMTWorkerData *const row_mt_worker_data, int map_idx,
  1483. int sync_idx) {
  1484. #if CONFIG_MULTITHREAD
  1485. pthread_mutex_lock(&row_mt_worker_data->recon_sync_mutex[sync_idx]);
  1486. row_mt_worker_data->recon_map[map_idx] = 1;
  1487. pthread_cond_signal(&row_mt_worker_data->recon_sync_cond[sync_idx]);
  1488. pthread_mutex_unlock(&row_mt_worker_data->recon_sync_mutex[sync_idx]);
  1489. #else
  1490. (void)row_mt_worker_data;
  1491. (void)map_idx;
  1492. (void)sync_idx;
  1493. #endif // CONFIG_MULTITHREAD
  1494. }
  1495. static void map_read(RowMTWorkerData *const row_mt_worker_data, int map_idx,
  1496. int sync_idx) {
  1497. #if CONFIG_MULTITHREAD
  1498. volatile int8_t *map = row_mt_worker_data->recon_map + map_idx;
  1499. pthread_mutex_t *const mutex =
  1500. &row_mt_worker_data->recon_sync_mutex[sync_idx];
  1501. pthread_mutex_lock(mutex);
  1502. while (!(*map)) {
  1503. pthread_cond_wait(&row_mt_worker_data->recon_sync_cond[sync_idx], mutex);
  1504. }
  1505. pthread_mutex_unlock(mutex);
  1506. #else
  1507. (void)row_mt_worker_data;
  1508. (void)map_idx;
  1509. (void)sync_idx;
  1510. #endif // CONFIG_MULTITHREAD
  1511. }
  1512. static int lpf_map_write_check(VP9LfSync *lf_sync, int row, int num_tile_cols) {
  1513. int return_val = 0;
  1514. #if CONFIG_MULTITHREAD
  1515. int corrupted;
  1516. pthread_mutex_lock(&lf_sync->lf_mutex);
  1517. corrupted = lf_sync->corrupted;
  1518. pthread_mutex_unlock(&lf_sync->lf_mutex);
  1519. if (!corrupted) {
  1520. pthread_mutex_lock(&lf_sync->recon_done_mutex[row]);
  1521. lf_sync->num_tiles_done[row] += 1;
  1522. if (num_tile_cols == lf_sync->num_tiles_done[row]) return_val = 1;
  1523. pthread_mutex_unlock(&lf_sync->recon_done_mutex[row]);
  1524. }
  1525. #else
  1526. (void)lf_sync;
  1527. (void)row;
  1528. (void)num_tile_cols;
  1529. #endif
  1530. return return_val;
  1531. }
  1532. static void vp9_tile_done(VP9Decoder *pbi) {
  1533. #if CONFIG_MULTITHREAD
  1534. int terminate;
  1535. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  1536. const int all_parse_done = 1 << pbi->common.log2_tile_cols;
  1537. pthread_mutex_lock(&row_mt_worker_data->recon_done_mutex);
  1538. row_mt_worker_data->num_tiles_done++;
  1539. terminate = all_parse_done == row_mt_worker_data->num_tiles_done;
  1540. pthread_mutex_unlock(&row_mt_worker_data->recon_done_mutex);
  1541. if (terminate) {
  1542. vp9_jobq_terminate(&row_mt_worker_data->jobq);
  1543. }
  1544. #else
  1545. (void)pbi;
  1546. #endif
  1547. }
  1548. static void vp9_jobq_alloc(VP9Decoder *pbi) {
  1549. VP9_COMMON *const cm = &pbi->common;
  1550. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  1551. const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
  1552. const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
  1553. const int tile_cols = 1 << cm->log2_tile_cols;
  1554. const size_t jobq_size = (tile_cols * sb_rows * 2 + sb_rows) * sizeof(Job);
  1555. if (jobq_size > row_mt_worker_data->jobq_size) {
  1556. vpx_free(row_mt_worker_data->jobq_buf);
  1557. CHECK_MEM_ERROR(cm, row_mt_worker_data->jobq_buf, vpx_calloc(1, jobq_size));
  1558. vp9_jobq_init(&row_mt_worker_data->jobq, row_mt_worker_data->jobq_buf,
  1559. jobq_size);
  1560. row_mt_worker_data->jobq_size = jobq_size;
  1561. }
  1562. }
  1563. static void recon_tile_row(TileWorkerData *tile_data, VP9Decoder *pbi,
  1564. int mi_row, int is_last_row, VP9LfSync *lf_sync,
  1565. int cur_tile_col) {
  1566. VP9_COMMON *const cm = &pbi->common;
  1567. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  1568. const int tile_cols = 1 << cm->log2_tile_cols;
  1569. const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1570. const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
  1571. const int cur_sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
  1572. int mi_col_start = tile_data->xd.tile.mi_col_start;
  1573. int mi_col_end = tile_data->xd.tile.mi_col_end;
  1574. int mi_col;
  1575. vp9_zero(tile_data->xd.left_context);
  1576. vp9_zero(tile_data->xd.left_seg_context);
  1577. for (mi_col = mi_col_start; mi_col < mi_col_end; mi_col += MI_BLOCK_SIZE) {
  1578. const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
  1579. int plane;
  1580. const int sb_num = (cur_sb_row * (aligned_cols >> MI_BLOCK_SIZE_LOG2) + c);
  1581. // Top Dependency
  1582. if (cur_sb_row) {
  1583. map_read(row_mt_worker_data, ((cur_sb_row - 1) * sb_cols) + c,
  1584. ((cur_sb_row - 1) * tile_cols) + cur_tile_col);
  1585. }
  1586. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  1587. tile_data->xd.plane[plane].eob =
  1588. row_mt_worker_data->eob[plane] + (sb_num << EOBS_PER_SB_LOG2);
  1589. tile_data->xd.plane[plane].dqcoeff =
  1590. row_mt_worker_data->dqcoeff[plane] + (sb_num << DQCOEFFS_PER_SB_LOG2);
  1591. }
  1592. tile_data->xd.partition =
  1593. row_mt_worker_data->partition + (sb_num * PARTITIONS_PER_SB);
  1594. process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, RECON,
  1595. recon_block);
  1596. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1597. // Queue LPF_JOB
  1598. int is_lpf_job_ready = 0;
  1599. if (mi_col + MI_BLOCK_SIZE >= mi_col_end) {
  1600. // Checks if this row has been decoded in all tiles
  1601. is_lpf_job_ready = lpf_map_write_check(lf_sync, cur_sb_row, tile_cols);
  1602. if (is_lpf_job_ready) {
  1603. Job lpf_job;
  1604. lpf_job.job_type = LPF_JOB;
  1605. if (cur_sb_row > 0) {
  1606. lpf_job.row_num = mi_row - MI_BLOCK_SIZE;
  1607. vp9_jobq_queue(&row_mt_worker_data->jobq, &lpf_job,
  1608. sizeof(lpf_job));
  1609. }
  1610. if (is_last_row) {
  1611. lpf_job.row_num = mi_row;
  1612. vp9_jobq_queue(&row_mt_worker_data->jobq, &lpf_job,
  1613. sizeof(lpf_job));
  1614. }
  1615. }
  1616. }
  1617. }
  1618. map_write(row_mt_worker_data, (cur_sb_row * sb_cols) + c,
  1619. (cur_sb_row * tile_cols) + cur_tile_col);
  1620. }
  1621. }
  1622. static void parse_tile_row(TileWorkerData *tile_data, VP9Decoder *pbi,
  1623. int mi_row, int cur_tile_col, uint8_t **data_end) {
  1624. int mi_col;
  1625. VP9_COMMON *const cm = &pbi->common;
  1626. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  1627. TileInfo *tile = &tile_data->xd.tile;
  1628. TileBuffer *const buf = &pbi->tile_buffers[cur_tile_col];
  1629. const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1630. vp9_zero(tile_data->dqcoeff);
  1631. vp9_tile_init(tile, cm, 0, cur_tile_col);
  1632. /* Update reader only at the beginning of each row in a tile */
  1633. if (mi_row == 0) {
  1634. setup_token_decoder(buf->data, *data_end, buf->size, &tile_data->error_info,
  1635. &tile_data->bit_reader, pbi->decrypt_cb,
  1636. pbi->decrypt_state);
  1637. }
  1638. vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
  1639. tile_data->xd.error_info = &tile_data->error_info;
  1640. vp9_zero(tile_data->xd.left_context);
  1641. vp9_zero(tile_data->xd.left_seg_context);
  1642. for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
  1643. mi_col += MI_BLOCK_SIZE) {
  1644. const int r = mi_row >> MI_BLOCK_SIZE_LOG2;
  1645. const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
  1646. int plane;
  1647. const int sb_num = (r * (aligned_cols >> MI_BLOCK_SIZE_LOG2) + c);
  1648. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  1649. tile_data->xd.plane[plane].eob =
  1650. row_mt_worker_data->eob[plane] + (sb_num << EOBS_PER_SB_LOG2);
  1651. tile_data->xd.plane[plane].dqcoeff =
  1652. row_mt_worker_data->dqcoeff[plane] + (sb_num << DQCOEFFS_PER_SB_LOG2);
  1653. }
  1654. tile_data->xd.partition =
  1655. row_mt_worker_data->partition + sb_num * PARTITIONS_PER_SB;
  1656. process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4, PARSE,
  1657. parse_block);
  1658. }
  1659. }
  1660. static int row_decode_worker_hook(void *arg1, void *arg2) {
  1661. ThreadData *const thread_data = (ThreadData *)arg1;
  1662. uint8_t **data_end = (uint8_t **)arg2;
  1663. VP9Decoder *const pbi = thread_data->pbi;
  1664. VP9_COMMON *const cm = &pbi->common;
  1665. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  1666. const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1667. const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
  1668. const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
  1669. const int tile_cols = 1 << cm->log2_tile_cols;
  1670. Job job;
  1671. LFWorkerData *lf_data = thread_data->lf_data;
  1672. VP9LfSync *lf_sync = thread_data->lf_sync;
  1673. volatile int corrupted = 0;
  1674. while (!vp9_jobq_dequeue(&row_mt_worker_data->jobq, &job, sizeof(job), 1)) {
  1675. int mi_col;
  1676. const int mi_row = job.row_num;
  1677. if (job.job_type == LPF_JOB) {
  1678. lf_data->start = mi_row;
  1679. lf_data->stop = lf_data->start + MI_BLOCK_SIZE;
  1680. if (cm->lf.filter_level && !cm->skip_loop_filter &&
  1681. mi_row < cm->mi_rows) {
  1682. vp9_loopfilter_job(lf_data, lf_sync);
  1683. }
  1684. } else if (job.job_type == RECON_JOB) {
  1685. const int cur_sb_row = mi_row >> MI_BLOCK_SIZE_LOG2;
  1686. const int is_last_row = sb_rows - 1 == cur_sb_row;
  1687. TileWorkerData twd_recon;
  1688. TileWorkerData *const tile_data_recon = &twd_recon;
  1689. int mi_col_start, mi_col_end;
  1690. tile_data_recon->xd = pbi->mb;
  1691. vp9_tile_init(&tile_data_recon->xd.tile, cm, 0, job.tile_col);
  1692. vp9_init_macroblockd(cm, &tile_data_recon->xd, tile_data_recon->dqcoeff);
  1693. mi_col_start = tile_data_recon->xd.tile.mi_col_start;
  1694. mi_col_end = tile_data_recon->xd.tile.mi_col_end;
  1695. if (setjmp(tile_data_recon->error_info.jmp)) {
  1696. const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
  1697. tile_data_recon->error_info.setjmp = 0;
  1698. corrupted = 1;
  1699. for (mi_col = mi_col_start; mi_col < mi_col_end;
  1700. mi_col += MI_BLOCK_SIZE) {
  1701. const int c = mi_col >> MI_BLOCK_SIZE_LOG2;
  1702. map_write(row_mt_worker_data, (cur_sb_row * sb_cols) + c,
  1703. (cur_sb_row * tile_cols) + job.tile_col);
  1704. }
  1705. if (is_last_row) {
  1706. vp9_tile_done(pbi);
  1707. }
  1708. continue;
  1709. }
  1710. tile_data_recon->error_info.setjmp = 1;
  1711. tile_data_recon->xd.error_info = &tile_data_recon->error_info;
  1712. recon_tile_row(tile_data_recon, pbi, mi_row, is_last_row, lf_sync,
  1713. job.tile_col);
  1714. if (corrupted)
  1715. vpx_internal_error(&tile_data_recon->error_info,
  1716. VPX_CODEC_CORRUPT_FRAME,
  1717. "Failed to decode tile data");
  1718. if (is_last_row) {
  1719. vp9_tile_done(pbi);
  1720. }
  1721. } else if (job.job_type == PARSE_JOB) {
  1722. TileWorkerData *const tile_data = &pbi->tile_worker_data[job.tile_col];
  1723. if (setjmp(tile_data->error_info.jmp)) {
  1724. tile_data->error_info.setjmp = 0;
  1725. corrupted = 1;
  1726. vp9_tile_done(pbi);
  1727. continue;
  1728. }
  1729. tile_data->xd = pbi->mb;
  1730. tile_data->xd.counts =
  1731. cm->frame_parallel_decoding_mode ? 0 : &tile_data->counts;
  1732. tile_data->error_info.setjmp = 1;
  1733. parse_tile_row(tile_data, pbi, mi_row, job.tile_col, data_end);
  1734. corrupted |= tile_data->xd.corrupted;
  1735. if (corrupted)
  1736. vpx_internal_error(&tile_data->error_info, VPX_CODEC_CORRUPT_FRAME,
  1737. "Failed to decode tile data");
  1738. /* Queue in the recon_job for this row */
  1739. {
  1740. Job recon_job;
  1741. recon_job.row_num = mi_row;
  1742. recon_job.tile_col = job.tile_col;
  1743. recon_job.job_type = RECON_JOB;
  1744. vp9_jobq_queue(&row_mt_worker_data->jobq, &recon_job,
  1745. sizeof(recon_job));
  1746. }
  1747. /* Queue next parse job */
  1748. if (mi_row + MI_BLOCK_SIZE < cm->mi_rows) {
  1749. Job parse_job;
  1750. parse_job.row_num = mi_row + MI_BLOCK_SIZE;
  1751. parse_job.tile_col = job.tile_col;
  1752. parse_job.job_type = PARSE_JOB;
  1753. vp9_jobq_queue(&row_mt_worker_data->jobq, &parse_job,
  1754. sizeof(parse_job));
  1755. }
  1756. }
  1757. }
  1758. return !corrupted;
  1759. }
  1760. static const uint8_t *decode_tiles(VP9Decoder *pbi, const uint8_t *data,
  1761. const uint8_t *data_end) {
  1762. VP9_COMMON *const cm = &pbi->common;
  1763. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  1764. const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1765. const int tile_cols = 1 << cm->log2_tile_cols;
  1766. const int tile_rows = 1 << cm->log2_tile_rows;
  1767. TileBuffer tile_buffers[4][1 << 6];
  1768. int tile_row, tile_col;
  1769. int mi_row, mi_col;
  1770. TileWorkerData *tile_data = NULL;
  1771. if (cm->lf.filter_level && !cm->skip_loop_filter &&
  1772. pbi->lf_worker.data1 == NULL) {
  1773. CHECK_MEM_ERROR(cm, pbi->lf_worker.data1,
  1774. vpx_memalign(32, sizeof(LFWorkerData)));
  1775. pbi->lf_worker.hook = vp9_loop_filter_worker;
  1776. if (pbi->max_threads > 1 && !winterface->reset(&pbi->lf_worker)) {
  1777. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  1778. "Loop filter thread creation failed");
  1779. }
  1780. }
  1781. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1782. LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
  1783. // Be sure to sync as we might be resuming after a failed frame decode.
  1784. winterface->sync(&pbi->lf_worker);
  1785. vp9_loop_filter_data_reset(lf_data, get_frame_new_buffer(cm), cm,
  1786. pbi->mb.plane);
  1787. }
  1788. assert(tile_rows <= 4);
  1789. assert(tile_cols <= (1 << 6));
  1790. // Note: this memset assumes above_context[0], [1] and [2]
  1791. // are allocated as part of the same buffer.
  1792. memset(cm->above_context, 0,
  1793. sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_cols);
  1794. memset(cm->above_seg_context, 0,
  1795. sizeof(*cm->above_seg_context) * aligned_cols);
  1796. vp9_reset_lfm(cm);
  1797. get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows, tile_buffers);
  1798. // Load all tile information into tile_data.
  1799. for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
  1800. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  1801. const TileBuffer *const buf = &tile_buffers[tile_row][tile_col];
  1802. tile_data = pbi->tile_worker_data + tile_cols * tile_row + tile_col;
  1803. tile_data->xd = pbi->mb;
  1804. tile_data->xd.corrupted = 0;
  1805. tile_data->xd.counts =
  1806. cm->frame_parallel_decoding_mode ? NULL : &cm->counts;
  1807. vp9_zero(tile_data->dqcoeff);
  1808. vp9_tile_init(&tile_data->xd.tile, cm, tile_row, tile_col);
  1809. setup_token_decoder(buf->data, data_end, buf->size, &cm->error,
  1810. &tile_data->bit_reader, pbi->decrypt_cb,
  1811. pbi->decrypt_state);
  1812. vp9_init_macroblockd(cm, &tile_data->xd, tile_data->dqcoeff);
  1813. }
  1814. }
  1815. for (tile_row = 0; tile_row < tile_rows; ++tile_row) {
  1816. TileInfo tile;
  1817. vp9_tile_set_row(&tile, cm, tile_row);
  1818. for (mi_row = tile.mi_row_start; mi_row < tile.mi_row_end;
  1819. mi_row += MI_BLOCK_SIZE) {
  1820. for (tile_col = 0; tile_col < tile_cols; ++tile_col) {
  1821. const int col =
  1822. pbi->inv_tile_order ? tile_cols - tile_col - 1 : tile_col;
  1823. tile_data = pbi->tile_worker_data + tile_cols * tile_row + col;
  1824. vp9_tile_set_col(&tile, cm, col);
  1825. vp9_zero(tile_data->xd.left_context);
  1826. vp9_zero(tile_data->xd.left_seg_context);
  1827. for (mi_col = tile.mi_col_start; mi_col < tile.mi_col_end;
  1828. mi_col += MI_BLOCK_SIZE) {
  1829. if (pbi->row_mt == 1) {
  1830. int plane;
  1831. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  1832. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  1833. tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane];
  1834. tile_data->xd.plane[plane].dqcoeff =
  1835. row_mt_worker_data->dqcoeff[plane];
  1836. }
  1837. tile_data->xd.partition = row_mt_worker_data->partition;
  1838. process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4,
  1839. PARSE, parse_block);
  1840. for (plane = 0; plane < MAX_MB_PLANE; ++plane) {
  1841. tile_data->xd.plane[plane].eob = row_mt_worker_data->eob[plane];
  1842. tile_data->xd.plane[plane].dqcoeff =
  1843. row_mt_worker_data->dqcoeff[plane];
  1844. }
  1845. tile_data->xd.partition = row_mt_worker_data->partition;
  1846. process_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4,
  1847. RECON, recon_block);
  1848. } else {
  1849. decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
  1850. }
  1851. }
  1852. pbi->mb.corrupted |= tile_data->xd.corrupted;
  1853. if (pbi->mb.corrupted)
  1854. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  1855. "Failed to decode tile data");
  1856. }
  1857. // Loopfilter one row.
  1858. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1859. const int lf_start = mi_row - MI_BLOCK_SIZE;
  1860. LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
  1861. // delay the loopfilter by 1 macroblock row.
  1862. if (lf_start < 0) continue;
  1863. // decoding has completed: finish up the loop filter in this thread.
  1864. if (mi_row + MI_BLOCK_SIZE >= cm->mi_rows) continue;
  1865. winterface->sync(&pbi->lf_worker);
  1866. lf_data->start = lf_start;
  1867. lf_data->stop = mi_row;
  1868. if (pbi->max_threads > 1) {
  1869. winterface->launch(&pbi->lf_worker);
  1870. } else {
  1871. winterface->execute(&pbi->lf_worker);
  1872. }
  1873. }
  1874. }
  1875. }
  1876. // Loopfilter remaining rows in the frame.
  1877. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  1878. LFWorkerData *const lf_data = (LFWorkerData *)pbi->lf_worker.data1;
  1879. winterface->sync(&pbi->lf_worker);
  1880. lf_data->start = lf_data->stop;
  1881. lf_data->stop = cm->mi_rows;
  1882. winterface->execute(&pbi->lf_worker);
  1883. }
  1884. // Get last tile data.
  1885. tile_data = pbi->tile_worker_data + tile_cols * tile_rows - 1;
  1886. return vpx_reader_find_end(&tile_data->bit_reader);
  1887. }
  1888. static void set_rows_after_error(VP9LfSync *lf_sync, int start_row, int mi_rows,
  1889. int num_tiles_left, int total_num_tiles) {
  1890. do {
  1891. int mi_row;
  1892. const int aligned_rows = mi_cols_aligned_to_sb(mi_rows);
  1893. const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
  1894. const int corrupted = 1;
  1895. for (mi_row = start_row; mi_row < mi_rows; mi_row += MI_BLOCK_SIZE) {
  1896. const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
  1897. vp9_set_row(lf_sync, total_num_tiles, mi_row >> MI_BLOCK_SIZE_LOG2,
  1898. is_last_row, corrupted);
  1899. }
  1900. /* If there are multiple tiles, the second tile should start marking row
  1901. * progress from row 0.
  1902. */
  1903. start_row = 0;
  1904. } while (num_tiles_left--);
  1905. }
  1906. // On entry 'tile_data->data_end' points to the end of the input frame, on exit
  1907. // it is updated to reflect the bitreader position of the final tile column if
  1908. // present in the tile buffer group or NULL otherwise.
  1909. static int tile_worker_hook(void *arg1, void *arg2) {
  1910. TileWorkerData *const tile_data = (TileWorkerData *)arg1;
  1911. VP9Decoder *const pbi = (VP9Decoder *)arg2;
  1912. TileInfo *volatile tile = &tile_data->xd.tile;
  1913. const int final_col = (1 << pbi->common.log2_tile_cols) - 1;
  1914. const uint8_t *volatile bit_reader_end = NULL;
  1915. VP9_COMMON *cm = &pbi->common;
  1916. LFWorkerData *lf_data = tile_data->lf_data;
  1917. VP9LfSync *lf_sync = tile_data->lf_sync;
  1918. volatile int mi_row = 0;
  1919. volatile int n = tile_data->buf_start;
  1920. tile_data->error_info.setjmp = 1;
  1921. if (setjmp(tile_data->error_info.jmp)) {
  1922. tile_data->error_info.setjmp = 0;
  1923. tile_data->xd.corrupted = 1;
  1924. tile_data->data_end = NULL;
  1925. if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
  1926. const int num_tiles_left = tile_data->buf_end - n;
  1927. const int mi_row_start = mi_row;
  1928. set_rows_after_error(lf_sync, mi_row_start, cm->mi_rows, num_tiles_left,
  1929. 1 << cm->log2_tile_cols);
  1930. }
  1931. return 0;
  1932. }
  1933. tile_data->xd.corrupted = 0;
  1934. do {
  1935. int mi_col;
  1936. const TileBuffer *const buf = pbi->tile_buffers + n;
  1937. /* Initialize to 0 is safe since we do not deal with streams that have
  1938. * more than one row of tiles. (So tile->mi_row_start will be 0)
  1939. */
  1940. assert(cm->log2_tile_rows == 0);
  1941. mi_row = 0;
  1942. vp9_zero(tile_data->dqcoeff);
  1943. vp9_tile_init(tile, &pbi->common, 0, buf->col);
  1944. setup_token_decoder(buf->data, tile_data->data_end, buf->size,
  1945. &tile_data->error_info, &tile_data->bit_reader,
  1946. pbi->decrypt_cb, pbi->decrypt_state);
  1947. vp9_init_macroblockd(&pbi->common, &tile_data->xd, tile_data->dqcoeff);
  1948. // init resets xd.error_info
  1949. tile_data->xd.error_info = &tile_data->error_info;
  1950. for (mi_row = tile->mi_row_start; mi_row < tile->mi_row_end;
  1951. mi_row += MI_BLOCK_SIZE) {
  1952. vp9_zero(tile_data->xd.left_context);
  1953. vp9_zero(tile_data->xd.left_seg_context);
  1954. for (mi_col = tile->mi_col_start; mi_col < tile->mi_col_end;
  1955. mi_col += MI_BLOCK_SIZE) {
  1956. decode_partition(tile_data, pbi, mi_row, mi_col, BLOCK_64X64, 4);
  1957. }
  1958. if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
  1959. const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
  1960. const int sb_rows = (aligned_rows >> MI_BLOCK_SIZE_LOG2);
  1961. const int is_last_row = (sb_rows - 1 == mi_row >> MI_BLOCK_SIZE_LOG2);
  1962. vp9_set_row(lf_sync, 1 << cm->log2_tile_cols,
  1963. mi_row >> MI_BLOCK_SIZE_LOG2, is_last_row,
  1964. tile_data->xd.corrupted);
  1965. }
  1966. }
  1967. if (buf->col == final_col) {
  1968. bit_reader_end = vpx_reader_find_end(&tile_data->bit_reader);
  1969. }
  1970. } while (!tile_data->xd.corrupted && ++n <= tile_data->buf_end);
  1971. if (pbi->lpf_mt_opt && n < tile_data->buf_end && cm->lf.filter_level &&
  1972. !cm->skip_loop_filter) {
  1973. /* This was not incremented in the tile loop, so increment before tiles left
  1974. * calculation
  1975. */
  1976. ++n;
  1977. set_rows_after_error(lf_sync, 0, cm->mi_rows, tile_data->buf_end - n,
  1978. 1 << cm->log2_tile_cols);
  1979. }
  1980. if (pbi->lpf_mt_opt && !tile_data->xd.corrupted && cm->lf.filter_level &&
  1981. !cm->skip_loop_filter) {
  1982. vp9_loopfilter_rows(lf_data, lf_sync);
  1983. }
  1984. tile_data->data_end = bit_reader_end;
  1985. return !tile_data->xd.corrupted;
  1986. }
  1987. // sorts in descending order
  1988. static int compare_tile_buffers(const void *a, const void *b) {
  1989. const TileBuffer *const buf_a = (const TileBuffer *)a;
  1990. const TileBuffer *const buf_b = (const TileBuffer *)b;
  1991. return (buf_a->size < buf_b->size) - (buf_a->size > buf_b->size);
  1992. }
  1993. static INLINE void init_mt(VP9Decoder *pbi) {
  1994. int n;
  1995. VP9_COMMON *const cm = &pbi->common;
  1996. VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
  1997. const int aligned_mi_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  1998. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  1999. if (pbi->num_tile_workers == 0) {
  2000. const int num_threads = pbi->max_threads;
  2001. CHECK_MEM_ERROR(cm, pbi->tile_workers,
  2002. vpx_malloc(num_threads * sizeof(*pbi->tile_workers)));
  2003. for (n = 0; n < num_threads; ++n) {
  2004. VPxWorker *const worker = &pbi->tile_workers[n];
  2005. ++pbi->num_tile_workers;
  2006. winterface->init(worker);
  2007. if (n < num_threads - 1 && !winterface->reset(worker)) {
  2008. vpx_internal_error(&cm->error, VPX_CODEC_ERROR,
  2009. "Tile decoder thread creation failed");
  2010. }
  2011. }
  2012. }
  2013. // Initialize LPF
  2014. if ((pbi->lpf_mt_opt || pbi->row_mt) && cm->lf.filter_level &&
  2015. !cm->skip_loop_filter) {
  2016. vp9_lpf_mt_init(lf_row_sync, cm, cm->lf.filter_level,
  2017. pbi->num_tile_workers);
  2018. }
  2019. // Note: this memset assumes above_context[0], [1] and [2]
  2020. // are allocated as part of the same buffer.
  2021. memset(cm->above_context, 0,
  2022. sizeof(*cm->above_context) * MAX_MB_PLANE * 2 * aligned_mi_cols);
  2023. memset(cm->above_seg_context, 0,
  2024. sizeof(*cm->above_seg_context) * aligned_mi_cols);
  2025. vp9_reset_lfm(cm);
  2026. }
  2027. static const uint8_t *decode_tiles_row_wise_mt(VP9Decoder *pbi,
  2028. const uint8_t *data,
  2029. const uint8_t *data_end) {
  2030. VP9_COMMON *const cm = &pbi->common;
  2031. RowMTWorkerData *const row_mt_worker_data = pbi->row_mt_worker_data;
  2032. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  2033. const int tile_cols = 1 << cm->log2_tile_cols;
  2034. const int tile_rows = 1 << cm->log2_tile_rows;
  2035. const int num_workers = pbi->max_threads;
  2036. int i, n;
  2037. int col;
  2038. int corrupted = 0;
  2039. const int sb_rows = mi_cols_aligned_to_sb(cm->mi_rows) >> MI_BLOCK_SIZE_LOG2;
  2040. const int sb_cols = mi_cols_aligned_to_sb(cm->mi_cols) >> MI_BLOCK_SIZE_LOG2;
  2041. VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
  2042. YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
  2043. assert(tile_cols <= (1 << 6));
  2044. assert(tile_rows == 1);
  2045. (void)tile_rows;
  2046. memset(row_mt_worker_data->recon_map, 0,
  2047. sb_rows * sb_cols * sizeof(*row_mt_worker_data->recon_map));
  2048. init_mt(pbi);
  2049. // Reset tile decoding hook
  2050. for (n = 0; n < num_workers; ++n) {
  2051. VPxWorker *const worker = &pbi->tile_workers[n];
  2052. ThreadData *const thread_data = &pbi->row_mt_worker_data->thread_data[n];
  2053. winterface->sync(worker);
  2054. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  2055. thread_data->lf_sync = lf_row_sync;
  2056. thread_data->lf_data = &thread_data->lf_sync->lfdata[n];
  2057. vp9_loop_filter_data_reset(thread_data->lf_data, new_fb, cm,
  2058. pbi->mb.plane);
  2059. }
  2060. thread_data->pbi = pbi;
  2061. worker->hook = row_decode_worker_hook;
  2062. worker->data1 = thread_data;
  2063. worker->data2 = (void *)&row_mt_worker_data->data_end;
  2064. }
  2065. for (col = 0; col < tile_cols; ++col) {
  2066. TileWorkerData *const tile_data = &pbi->tile_worker_data[col];
  2067. tile_data->xd = pbi->mb;
  2068. tile_data->xd.counts =
  2069. cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
  2070. }
  2071. /* Reset the jobq to start of the jobq buffer */
  2072. vp9_jobq_reset(&row_mt_worker_data->jobq);
  2073. row_mt_worker_data->num_tiles_done = 0;
  2074. row_mt_worker_data->data_end = NULL;
  2075. // Load tile data into tile_buffers
  2076. get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
  2077. &pbi->tile_buffers);
  2078. // Initialize thread frame counts.
  2079. if (!cm->frame_parallel_decoding_mode) {
  2080. for (col = 0; col < tile_cols; ++col) {
  2081. TileWorkerData *const tile_data = &pbi->tile_worker_data[col];
  2082. vp9_zero(tile_data->counts);
  2083. }
  2084. }
  2085. // queue parse jobs for 0th row of every tile
  2086. for (col = 0; col < tile_cols; ++col) {
  2087. Job parse_job;
  2088. parse_job.row_num = 0;
  2089. parse_job.tile_col = col;
  2090. parse_job.job_type = PARSE_JOB;
  2091. vp9_jobq_queue(&row_mt_worker_data->jobq, &parse_job, sizeof(parse_job));
  2092. }
  2093. for (i = 0; i < num_workers; ++i) {
  2094. VPxWorker *const worker = &pbi->tile_workers[i];
  2095. worker->had_error = 0;
  2096. if (i == num_workers - 1) {
  2097. winterface->execute(worker);
  2098. } else {
  2099. winterface->launch(worker);
  2100. }
  2101. }
  2102. for (; n > 0; --n) {
  2103. VPxWorker *const worker = &pbi->tile_workers[n - 1];
  2104. // TODO(jzern): The tile may have specific error data associated with
  2105. // its vpx_internal_error_info which could be propagated to the main info
  2106. // in cm. Additionally once the threads have been synced and an error is
  2107. // detected, there's no point in continuing to decode tiles.
  2108. corrupted |= !winterface->sync(worker);
  2109. }
  2110. pbi->mb.corrupted = corrupted;
  2111. {
  2112. /* Set data end */
  2113. TileWorkerData *const tile_data = &pbi->tile_worker_data[tile_cols - 1];
  2114. row_mt_worker_data->data_end = vpx_reader_find_end(&tile_data->bit_reader);
  2115. }
  2116. // Accumulate thread frame counts.
  2117. if (!cm->frame_parallel_decoding_mode) {
  2118. for (i = 0; i < tile_cols; ++i) {
  2119. TileWorkerData *const tile_data = &pbi->tile_worker_data[i];
  2120. vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
  2121. }
  2122. }
  2123. return row_mt_worker_data->data_end;
  2124. }
  2125. static const uint8_t *decode_tiles_mt(VP9Decoder *pbi, const uint8_t *data,
  2126. const uint8_t *data_end) {
  2127. VP9_COMMON *const cm = &pbi->common;
  2128. const VPxWorkerInterface *const winterface = vpx_get_worker_interface();
  2129. const uint8_t *bit_reader_end = NULL;
  2130. VP9LfSync *lf_row_sync = &pbi->lf_row_sync;
  2131. YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
  2132. const int tile_cols = 1 << cm->log2_tile_cols;
  2133. const int tile_rows = 1 << cm->log2_tile_rows;
  2134. const int num_workers = VPXMIN(pbi->max_threads, tile_cols);
  2135. int n;
  2136. assert(tile_cols <= (1 << 6));
  2137. assert(tile_rows == 1);
  2138. (void)tile_rows;
  2139. init_mt(pbi);
  2140. // Reset tile decoding hook
  2141. for (n = 0; n < num_workers; ++n) {
  2142. VPxWorker *const worker = &pbi->tile_workers[n];
  2143. TileWorkerData *const tile_data =
  2144. &pbi->tile_worker_data[n + pbi->total_tiles];
  2145. winterface->sync(worker);
  2146. if (pbi->lpf_mt_opt && cm->lf.filter_level && !cm->skip_loop_filter) {
  2147. tile_data->lf_sync = lf_row_sync;
  2148. tile_data->lf_data = &tile_data->lf_sync->lfdata[n];
  2149. vp9_loop_filter_data_reset(tile_data->lf_data, new_fb, cm, pbi->mb.plane);
  2150. tile_data->lf_data->y_only = 0;
  2151. }
  2152. tile_data->xd = pbi->mb;
  2153. tile_data->xd.counts =
  2154. cm->frame_parallel_decoding_mode ? NULL : &tile_data->counts;
  2155. worker->hook = tile_worker_hook;
  2156. worker->data1 = tile_data;
  2157. worker->data2 = pbi;
  2158. }
  2159. // Load tile data into tile_buffers
  2160. get_tile_buffers(pbi, data, data_end, tile_cols, tile_rows,
  2161. &pbi->tile_buffers);
  2162. // Sort the buffers based on size in descending order.
  2163. qsort(pbi->tile_buffers, tile_cols, sizeof(pbi->tile_buffers[0]),
  2164. compare_tile_buffers);
  2165. if (num_workers == tile_cols) {
  2166. // Rearrange the tile buffers such that the largest, and
  2167. // presumably the most difficult, tile will be decoded in the main thread.
  2168. // This should help minimize the number of instances where the main thread
  2169. // is waiting for a worker to complete.
  2170. const TileBuffer largest = pbi->tile_buffers[0];
  2171. memmove(pbi->tile_buffers, pbi->tile_buffers + 1,
  2172. (tile_cols - 1) * sizeof(pbi->tile_buffers[0]));
  2173. pbi->tile_buffers[tile_cols - 1] = largest;
  2174. } else {
  2175. int start = 0, end = tile_cols - 2;
  2176. TileBuffer tmp;
  2177. // Interleave the tiles to distribute the load between threads, assuming a
  2178. // larger tile implies it is more difficult to decode.
  2179. while (start < end) {
  2180. tmp = pbi->tile_buffers[start];
  2181. pbi->tile_buffers[start] = pbi->tile_buffers[end];
  2182. pbi->tile_buffers[end] = tmp;
  2183. start += 2;
  2184. end -= 2;
  2185. }
  2186. }
  2187. // Initialize thread frame counts.
  2188. if (!cm->frame_parallel_decoding_mode) {
  2189. for (n = 0; n < num_workers; ++n) {
  2190. TileWorkerData *const tile_data =
  2191. (TileWorkerData *)pbi->tile_workers[n].data1;
  2192. vp9_zero(tile_data->counts);
  2193. }
  2194. }
  2195. {
  2196. const int base = tile_cols / num_workers;
  2197. const int remain = tile_cols % num_workers;
  2198. int buf_start = 0;
  2199. for (n = 0; n < num_workers; ++n) {
  2200. const int count = base + (remain + n) / num_workers;
  2201. VPxWorker *const worker = &pbi->tile_workers[n];
  2202. TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
  2203. tile_data->buf_start = buf_start;
  2204. tile_data->buf_end = buf_start + count - 1;
  2205. tile_data->data_end = data_end;
  2206. buf_start += count;
  2207. worker->had_error = 0;
  2208. if (n == num_workers - 1) {
  2209. assert(tile_data->buf_end == tile_cols - 1);
  2210. winterface->execute(worker);
  2211. } else {
  2212. winterface->launch(worker);
  2213. }
  2214. }
  2215. for (; n > 0; --n) {
  2216. VPxWorker *const worker = &pbi->tile_workers[n - 1];
  2217. TileWorkerData *const tile_data = (TileWorkerData *)worker->data1;
  2218. // TODO(jzern): The tile may have specific error data associated with
  2219. // its vpx_internal_error_info which could be propagated to the main info
  2220. // in cm. Additionally once the threads have been synced and an error is
  2221. // detected, there's no point in continuing to decode tiles.
  2222. pbi->mb.corrupted |= !winterface->sync(worker);
  2223. if (!bit_reader_end) bit_reader_end = tile_data->data_end;
  2224. }
  2225. }
  2226. // Accumulate thread frame counts.
  2227. if (!cm->frame_parallel_decoding_mode) {
  2228. for (n = 0; n < num_workers; ++n) {
  2229. TileWorkerData *const tile_data =
  2230. (TileWorkerData *)pbi->tile_workers[n].data1;
  2231. vp9_accumulate_frame_counts(&cm->counts, &tile_data->counts, 1);
  2232. }
  2233. }
  2234. assert(bit_reader_end || pbi->mb.corrupted);
  2235. return bit_reader_end;
  2236. }
  2237. static void error_handler(void *data) {
  2238. VP9_COMMON *const cm = (VP9_COMMON *)data;
  2239. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME, "Truncated packet");
  2240. }
  2241. static void read_bitdepth_colorspace_sampling(VP9_COMMON *cm,
  2242. struct vpx_read_bit_buffer *rb) {
  2243. if (cm->profile >= PROFILE_2) {
  2244. cm->bit_depth = vpx_rb_read_bit(rb) ? VPX_BITS_12 : VPX_BITS_10;
  2245. #if CONFIG_VP9_HIGHBITDEPTH
  2246. cm->use_highbitdepth = 1;
  2247. #endif
  2248. } else {
  2249. cm->bit_depth = VPX_BITS_8;
  2250. #if CONFIG_VP9_HIGHBITDEPTH
  2251. cm->use_highbitdepth = 0;
  2252. #endif
  2253. }
  2254. cm->color_space = vpx_rb_read_literal(rb, 3);
  2255. if (cm->color_space != VPX_CS_SRGB) {
  2256. cm->color_range = (vpx_color_range_t)vpx_rb_read_bit(rb);
  2257. if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
  2258. cm->subsampling_x = vpx_rb_read_bit(rb);
  2259. cm->subsampling_y = vpx_rb_read_bit(rb);
  2260. if (cm->subsampling_x == 1 && cm->subsampling_y == 1)
  2261. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2262. "4:2:0 color not supported in profile 1 or 3");
  2263. if (vpx_rb_read_bit(rb))
  2264. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2265. "Reserved bit set");
  2266. } else {
  2267. cm->subsampling_y = cm->subsampling_x = 1;
  2268. }
  2269. } else {
  2270. cm->color_range = VPX_CR_FULL_RANGE;
  2271. if (cm->profile == PROFILE_1 || cm->profile == PROFILE_3) {
  2272. // Note if colorspace is SRGB then 4:4:4 chroma sampling is assumed.
  2273. // 4:2:2 or 4:4:0 chroma sampling is not allowed.
  2274. cm->subsampling_y = cm->subsampling_x = 0;
  2275. if (vpx_rb_read_bit(rb))
  2276. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2277. "Reserved bit set");
  2278. } else {
  2279. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2280. "4:4:4 color not supported in profile 0 or 2");
  2281. }
  2282. }
  2283. }
  2284. static INLINE void flush_all_fb_on_key(VP9_COMMON *cm) {
  2285. if (cm->frame_type == KEY_FRAME && cm->current_video_frame > 0) {
  2286. RefCntBuffer *const frame_bufs = cm->buffer_pool->frame_bufs;
  2287. BufferPool *const pool = cm->buffer_pool;
  2288. int i;
  2289. for (i = 0; i < FRAME_BUFFERS; ++i) {
  2290. if (i == cm->new_fb_idx) continue;
  2291. frame_bufs[i].ref_count = 0;
  2292. if (!frame_bufs[i].released) {
  2293. pool->release_fb_cb(pool->cb_priv, &frame_bufs[i].raw_frame_buffer);
  2294. frame_bufs[i].released = 1;
  2295. }
  2296. }
  2297. }
  2298. }
  2299. static size_t read_uncompressed_header(VP9Decoder *pbi,
  2300. struct vpx_read_bit_buffer *rb) {
  2301. VP9_COMMON *const cm = &pbi->common;
  2302. BufferPool *const pool = cm->buffer_pool;
  2303. RefCntBuffer *const frame_bufs = pool->frame_bufs;
  2304. int i, mask, ref_index = 0;
  2305. size_t sz;
  2306. cm->last_frame_type = cm->frame_type;
  2307. cm->last_intra_only = cm->intra_only;
  2308. if (vpx_rb_read_literal(rb, 2) != VP9_FRAME_MARKER)
  2309. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2310. "Invalid frame marker");
  2311. cm->profile = vp9_read_profile(rb);
  2312. #if CONFIG_VP9_HIGHBITDEPTH
  2313. if (cm->profile >= MAX_PROFILES)
  2314. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2315. "Unsupported bitstream profile");
  2316. #else
  2317. if (cm->profile >= PROFILE_2)
  2318. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2319. "Unsupported bitstream profile");
  2320. #endif
  2321. cm->show_existing_frame = vpx_rb_read_bit(rb);
  2322. if (cm->show_existing_frame) {
  2323. // Show an existing frame directly.
  2324. const int frame_to_show = cm->ref_frame_map[vpx_rb_read_literal(rb, 3)];
  2325. if (frame_to_show < 0 || frame_bufs[frame_to_show].ref_count < 1) {
  2326. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2327. "Buffer %d does not contain a decoded frame",
  2328. frame_to_show);
  2329. }
  2330. ref_cnt_fb(frame_bufs, &cm->new_fb_idx, frame_to_show);
  2331. pbi->refresh_frame_flags = 0;
  2332. cm->lf.filter_level = 0;
  2333. cm->show_frame = 1;
  2334. return 0;
  2335. }
  2336. cm->frame_type = (FRAME_TYPE)vpx_rb_read_bit(rb);
  2337. cm->show_frame = vpx_rb_read_bit(rb);
  2338. cm->error_resilient_mode = vpx_rb_read_bit(rb);
  2339. if (cm->frame_type == KEY_FRAME) {
  2340. if (!vp9_read_sync_code(rb))
  2341. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2342. "Invalid frame sync code");
  2343. read_bitdepth_colorspace_sampling(cm, rb);
  2344. pbi->refresh_frame_flags = (1 << REF_FRAMES) - 1;
  2345. for (i = 0; i < REFS_PER_FRAME; ++i) {
  2346. cm->frame_refs[i].idx = INVALID_IDX;
  2347. cm->frame_refs[i].buf = NULL;
  2348. }
  2349. setup_frame_size(cm, rb);
  2350. if (pbi->need_resync) {
  2351. memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
  2352. flush_all_fb_on_key(cm);
  2353. pbi->need_resync = 0;
  2354. }
  2355. } else {
  2356. cm->intra_only = cm->show_frame ? 0 : vpx_rb_read_bit(rb);
  2357. cm->reset_frame_context =
  2358. cm->error_resilient_mode ? 0 : vpx_rb_read_literal(rb, 2);
  2359. if (cm->intra_only) {
  2360. if (!vp9_read_sync_code(rb))
  2361. vpx_internal_error(&cm->error, VPX_CODEC_UNSUP_BITSTREAM,
  2362. "Invalid frame sync code");
  2363. if (cm->profile > PROFILE_0) {
  2364. read_bitdepth_colorspace_sampling(cm, rb);
  2365. } else {
  2366. // NOTE: The intra-only frame header does not include the specification
  2367. // of either the color format or color sub-sampling in profile 0. VP9
  2368. // specifies that the default color format should be YUV 4:2:0 in this
  2369. // case (normative).
  2370. cm->color_space = VPX_CS_BT_601;
  2371. cm->color_range = VPX_CR_STUDIO_RANGE;
  2372. cm->subsampling_y = cm->subsampling_x = 1;
  2373. cm->bit_depth = VPX_BITS_8;
  2374. #if CONFIG_VP9_HIGHBITDEPTH
  2375. cm->use_highbitdepth = 0;
  2376. #endif
  2377. }
  2378. pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
  2379. setup_frame_size(cm, rb);
  2380. if (pbi->need_resync) {
  2381. memset(&cm->ref_frame_map, -1, sizeof(cm->ref_frame_map));
  2382. pbi->need_resync = 0;
  2383. }
  2384. } else if (pbi->need_resync != 1) { /* Skip if need resync */
  2385. pbi->refresh_frame_flags = vpx_rb_read_literal(rb, REF_FRAMES);
  2386. for (i = 0; i < REFS_PER_FRAME; ++i) {
  2387. const int ref = vpx_rb_read_literal(rb, REF_FRAMES_LOG2);
  2388. const int idx = cm->ref_frame_map[ref];
  2389. RefBuffer *const ref_frame = &cm->frame_refs[i];
  2390. ref_frame->idx = idx;
  2391. ref_frame->buf = &frame_bufs[idx].buf;
  2392. cm->ref_frame_sign_bias[LAST_FRAME + i] = vpx_rb_read_bit(rb);
  2393. }
  2394. setup_frame_size_with_refs(cm, rb);
  2395. cm->allow_high_precision_mv = vpx_rb_read_bit(rb);
  2396. cm->interp_filter = read_interp_filter(rb);
  2397. for (i = 0; i < REFS_PER_FRAME; ++i) {
  2398. RefBuffer *const ref_buf = &cm->frame_refs[i];
  2399. #if CONFIG_VP9_HIGHBITDEPTH
  2400. vp9_setup_scale_factors_for_frame(
  2401. &ref_buf->sf, ref_buf->buf->y_crop_width,
  2402. ref_buf->buf->y_crop_height, cm->width, cm->height,
  2403. cm->use_highbitdepth);
  2404. #else
  2405. vp9_setup_scale_factors_for_frame(
  2406. &ref_buf->sf, ref_buf->buf->y_crop_width,
  2407. ref_buf->buf->y_crop_height, cm->width, cm->height);
  2408. #endif
  2409. }
  2410. }
  2411. }
  2412. #if CONFIG_VP9_HIGHBITDEPTH
  2413. get_frame_new_buffer(cm)->bit_depth = cm->bit_depth;
  2414. #endif
  2415. get_frame_new_buffer(cm)->color_space = cm->color_space;
  2416. get_frame_new_buffer(cm)->color_range = cm->color_range;
  2417. get_frame_new_buffer(cm)->render_width = cm->render_width;
  2418. get_frame_new_buffer(cm)->render_height = cm->render_height;
  2419. if (pbi->need_resync) {
  2420. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2421. "Keyframe / intra-only frame required to reset decoder"
  2422. " state");
  2423. }
  2424. if (!cm->error_resilient_mode) {
  2425. cm->refresh_frame_context = vpx_rb_read_bit(rb);
  2426. cm->frame_parallel_decoding_mode = vpx_rb_read_bit(rb);
  2427. if (!cm->frame_parallel_decoding_mode) vp9_zero(cm->counts);
  2428. } else {
  2429. cm->refresh_frame_context = 0;
  2430. cm->frame_parallel_decoding_mode = 1;
  2431. }
  2432. // This flag will be overridden by the call to vp9_setup_past_independence
  2433. // below, forcing the use of context 0 for those frame types.
  2434. cm->frame_context_idx = vpx_rb_read_literal(rb, FRAME_CONTEXTS_LOG2);
  2435. // Generate next_ref_frame_map.
  2436. for (mask = pbi->refresh_frame_flags; mask; mask >>= 1) {
  2437. if (mask & 1) {
  2438. cm->next_ref_frame_map[ref_index] = cm->new_fb_idx;
  2439. ++frame_bufs[cm->new_fb_idx].ref_count;
  2440. } else {
  2441. cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
  2442. }
  2443. // Current thread holds the reference frame.
  2444. if (cm->ref_frame_map[ref_index] >= 0)
  2445. ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
  2446. ++ref_index;
  2447. }
  2448. for (; ref_index < REF_FRAMES; ++ref_index) {
  2449. cm->next_ref_frame_map[ref_index] = cm->ref_frame_map[ref_index];
  2450. // Current thread holds the reference frame.
  2451. if (cm->ref_frame_map[ref_index] >= 0)
  2452. ++frame_bufs[cm->ref_frame_map[ref_index]].ref_count;
  2453. }
  2454. pbi->hold_ref_buf = 1;
  2455. if (frame_is_intra_only(cm) || cm->error_resilient_mode)
  2456. vp9_setup_past_independence(cm);
  2457. setup_loopfilter(&cm->lf, rb);
  2458. setup_quantization(cm, &pbi->mb, rb);
  2459. setup_segmentation(&cm->seg, rb);
  2460. setup_segmentation_dequant(cm);
  2461. setup_tile_info(cm, rb);
  2462. if (pbi->row_mt == 1) {
  2463. int num_sbs = 1;
  2464. const int aligned_rows = mi_cols_aligned_to_sb(cm->mi_rows);
  2465. const int sb_rows = aligned_rows >> MI_BLOCK_SIZE_LOG2;
  2466. const int num_jobs = sb_rows << cm->log2_tile_cols;
  2467. if (pbi->row_mt_worker_data == NULL) {
  2468. CHECK_MEM_ERROR(cm, pbi->row_mt_worker_data,
  2469. vpx_calloc(1, sizeof(*pbi->row_mt_worker_data)));
  2470. #if CONFIG_MULTITHREAD
  2471. pthread_mutex_init(&pbi->row_mt_worker_data->recon_done_mutex, NULL);
  2472. #endif
  2473. }
  2474. if (pbi->max_threads > 1) {
  2475. const int aligned_cols = mi_cols_aligned_to_sb(cm->mi_cols);
  2476. const int sb_cols = aligned_cols >> MI_BLOCK_SIZE_LOG2;
  2477. num_sbs = sb_cols * sb_rows;
  2478. }
  2479. if (num_sbs > pbi->row_mt_worker_data->num_sbs ||
  2480. num_jobs > pbi->row_mt_worker_data->num_jobs) {
  2481. vp9_dec_free_row_mt_mem(pbi->row_mt_worker_data);
  2482. vp9_dec_alloc_row_mt_mem(pbi->row_mt_worker_data, cm, num_sbs,
  2483. pbi->max_threads, num_jobs);
  2484. }
  2485. vp9_jobq_alloc(pbi);
  2486. }
  2487. sz = vpx_rb_read_literal(rb, 16);
  2488. if (sz == 0)
  2489. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2490. "Invalid header size");
  2491. return sz;
  2492. }
  2493. static int read_compressed_header(VP9Decoder *pbi, const uint8_t *data,
  2494. size_t partition_size) {
  2495. VP9_COMMON *const cm = &pbi->common;
  2496. MACROBLOCKD *const xd = &pbi->mb;
  2497. FRAME_CONTEXT *const fc = cm->fc;
  2498. vpx_reader r;
  2499. int k;
  2500. if (vpx_reader_init(&r, data, partition_size, pbi->decrypt_cb,
  2501. pbi->decrypt_state))
  2502. vpx_internal_error(&cm->error, VPX_CODEC_MEM_ERROR,
  2503. "Failed to allocate bool decoder 0");
  2504. cm->tx_mode = xd->lossless ? ONLY_4X4 : read_tx_mode(&r);
  2505. if (cm->tx_mode == TX_MODE_SELECT) read_tx_mode_probs(&fc->tx_probs, &r);
  2506. read_coef_probs(fc, cm->tx_mode, &r);
  2507. for (k = 0; k < SKIP_CONTEXTS; ++k)
  2508. vp9_diff_update_prob(&r, &fc->skip_probs[k]);
  2509. if (!frame_is_intra_only(cm)) {
  2510. nmv_context *const nmvc = &fc->nmvc;
  2511. int i, j;
  2512. read_inter_mode_probs(fc, &r);
  2513. if (cm->interp_filter == SWITCHABLE) read_switchable_interp_probs(fc, &r);
  2514. for (i = 0; i < INTRA_INTER_CONTEXTS; i++)
  2515. vp9_diff_update_prob(&r, &fc->intra_inter_prob[i]);
  2516. cm->reference_mode = read_frame_reference_mode(cm, &r);
  2517. if (cm->reference_mode != SINGLE_REFERENCE)
  2518. vp9_setup_compound_reference_mode(cm);
  2519. read_frame_reference_mode_probs(cm, &r);
  2520. for (j = 0; j < BLOCK_SIZE_GROUPS; j++)
  2521. for (i = 0; i < INTRA_MODES - 1; ++i)
  2522. vp9_diff_update_prob(&r, &fc->y_mode_prob[j][i]);
  2523. for (j = 0; j < PARTITION_CONTEXTS; ++j)
  2524. for (i = 0; i < PARTITION_TYPES - 1; ++i)
  2525. vp9_diff_update_prob(&r, &fc->partition_prob[j][i]);
  2526. read_mv_probs(nmvc, cm->allow_high_precision_mv, &r);
  2527. }
  2528. return vpx_reader_has_error(&r);
  2529. }
  2530. static struct vpx_read_bit_buffer *init_read_bit_buffer(
  2531. VP9Decoder *pbi, struct vpx_read_bit_buffer *rb, const uint8_t *data,
  2532. const uint8_t *data_end, uint8_t clear_data[MAX_VP9_HEADER_SIZE]) {
  2533. rb->bit_offset = 0;
  2534. rb->error_handler = error_handler;
  2535. rb->error_handler_data = &pbi->common;
  2536. if (pbi->decrypt_cb) {
  2537. const int n = (int)VPXMIN(MAX_VP9_HEADER_SIZE, data_end - data);
  2538. pbi->decrypt_cb(pbi->decrypt_state, data, clear_data, n);
  2539. rb->bit_buffer = clear_data;
  2540. rb->bit_buffer_end = clear_data + n;
  2541. } else {
  2542. rb->bit_buffer = data;
  2543. rb->bit_buffer_end = data_end;
  2544. }
  2545. return rb;
  2546. }
  2547. //------------------------------------------------------------------------------
  2548. int vp9_read_sync_code(struct vpx_read_bit_buffer *const rb) {
  2549. return vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_0 &&
  2550. vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_1 &&
  2551. vpx_rb_read_literal(rb, 8) == VP9_SYNC_CODE_2;
  2552. }
  2553. void vp9_read_frame_size(struct vpx_read_bit_buffer *rb, int *width,
  2554. int *height) {
  2555. *width = vpx_rb_read_literal(rb, 16) + 1;
  2556. *height = vpx_rb_read_literal(rb, 16) + 1;
  2557. }
  2558. BITSTREAM_PROFILE vp9_read_profile(struct vpx_read_bit_buffer *rb) {
  2559. int profile = vpx_rb_read_bit(rb);
  2560. profile |= vpx_rb_read_bit(rb) << 1;
  2561. if (profile > 2) profile += vpx_rb_read_bit(rb);
  2562. return (BITSTREAM_PROFILE)profile;
  2563. }
  2564. void vp9_decode_frame(VP9Decoder *pbi, const uint8_t *data,
  2565. const uint8_t *data_end, const uint8_t **p_data_end) {
  2566. VP9_COMMON *const cm = &pbi->common;
  2567. MACROBLOCKD *const xd = &pbi->mb;
  2568. struct vpx_read_bit_buffer rb;
  2569. int context_updated = 0;
  2570. uint8_t clear_data[MAX_VP9_HEADER_SIZE];
  2571. const size_t first_partition_size = read_uncompressed_header(
  2572. pbi, init_read_bit_buffer(pbi, &rb, data, data_end, clear_data));
  2573. const int tile_rows = 1 << cm->log2_tile_rows;
  2574. const int tile_cols = 1 << cm->log2_tile_cols;
  2575. YV12_BUFFER_CONFIG *const new_fb = get_frame_new_buffer(cm);
  2576. #if CONFIG_BITSTREAM_DEBUG || CONFIG_MISMATCH_DEBUG
  2577. bitstream_queue_set_frame_read(cm->current_video_frame * 2 + cm->show_frame);
  2578. #endif
  2579. #if CONFIG_MISMATCH_DEBUG
  2580. mismatch_move_frame_idx_r();
  2581. #endif
  2582. xd->cur_buf = new_fb;
  2583. if (!first_partition_size) {
  2584. // showing a frame directly
  2585. *p_data_end = data + (cm->profile <= PROFILE_2 ? 1 : 2);
  2586. return;
  2587. }
  2588. data += vpx_rb_bytes_read(&rb);
  2589. if (!read_is_valid(data, first_partition_size, data_end))
  2590. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2591. "Truncated packet or corrupt header length");
  2592. cm->use_prev_frame_mvs =
  2593. !cm->error_resilient_mode && cm->width == cm->last_width &&
  2594. cm->height == cm->last_height && !cm->last_intra_only &&
  2595. cm->last_show_frame && (cm->last_frame_type != KEY_FRAME);
  2596. vp9_setup_block_planes(xd, cm->subsampling_x, cm->subsampling_y);
  2597. *cm->fc = cm->frame_contexts[cm->frame_context_idx];
  2598. if (!cm->fc->initialized)
  2599. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2600. "Uninitialized entropy context.");
  2601. xd->corrupted = 0;
  2602. new_fb->corrupted = read_compressed_header(pbi, data, first_partition_size);
  2603. if (new_fb->corrupted)
  2604. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2605. "Decode failed. Frame data header is corrupted.");
  2606. if (cm->lf.filter_level && !cm->skip_loop_filter) {
  2607. vp9_loop_filter_frame_init(cm, cm->lf.filter_level);
  2608. }
  2609. if (pbi->tile_worker_data == NULL ||
  2610. (tile_cols * tile_rows) != pbi->total_tiles) {
  2611. const int num_tile_workers =
  2612. tile_cols * tile_rows + ((pbi->max_threads > 1) ? pbi->max_threads : 0);
  2613. const size_t twd_size = num_tile_workers * sizeof(*pbi->tile_worker_data);
  2614. // Ensure tile data offsets will be properly aligned. This may fail on
  2615. // platforms without DECLARE_ALIGNED().
  2616. assert((sizeof(*pbi->tile_worker_data) % 16) == 0);
  2617. vpx_free(pbi->tile_worker_data);
  2618. CHECK_MEM_ERROR(cm, pbi->tile_worker_data, vpx_memalign(32, twd_size));
  2619. pbi->total_tiles = tile_rows * tile_cols;
  2620. }
  2621. if (pbi->max_threads > 1 && tile_rows == 1 &&
  2622. (tile_cols > 1 || pbi->row_mt == 1)) {
  2623. if (pbi->row_mt == 1) {
  2624. *p_data_end =
  2625. decode_tiles_row_wise_mt(pbi, data + first_partition_size, data_end);
  2626. } else {
  2627. // Multi-threaded tile decoder
  2628. *p_data_end = decode_tiles_mt(pbi, data + first_partition_size, data_end);
  2629. if (!pbi->lpf_mt_opt) {
  2630. if (!xd->corrupted) {
  2631. if (!cm->skip_loop_filter) {
  2632. // If multiple threads are used to decode tiles, then we use those
  2633. // threads to do parallel loopfiltering.
  2634. vp9_loop_filter_frame_mt(
  2635. new_fb, cm, pbi->mb.plane, cm->lf.filter_level, 0, 0,
  2636. pbi->tile_workers, pbi->num_tile_workers, &pbi->lf_row_sync);
  2637. }
  2638. } else {
  2639. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2640. "Decode failed. Frame data is corrupted.");
  2641. }
  2642. }
  2643. }
  2644. } else {
  2645. *p_data_end = decode_tiles(pbi, data + first_partition_size, data_end);
  2646. }
  2647. if (!xd->corrupted) {
  2648. if (!cm->error_resilient_mode && !cm->frame_parallel_decoding_mode) {
  2649. vp9_adapt_coef_probs(cm);
  2650. if (!frame_is_intra_only(cm)) {
  2651. vp9_adapt_mode_probs(cm);
  2652. vp9_adapt_mv_probs(cm, cm->allow_high_precision_mv);
  2653. }
  2654. }
  2655. } else {
  2656. vpx_internal_error(&cm->error, VPX_CODEC_CORRUPT_FRAME,
  2657. "Decode failed. Frame data is corrupted.");
  2658. }
  2659. // Non frame parallel update frame context here.
  2660. if (cm->refresh_frame_context && !context_updated)
  2661. cm->frame_contexts[cm->frame_context_idx] = *cm->fc;
  2662. }