vp8.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826
  1. /*
  2. * VP7/VP8 compatible video decoder
  3. *
  4. * Copyright (C) 2010 David Conrad
  5. * Copyright (C) 2010 Ronald S. Bultje
  6. * Copyright (C) 2010 Fiona Glaser
  7. * Copyright (C) 2012 Daniel Kang
  8. * Copyright (C) 2014 Peter Ross
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include "libavutil/imgutils.h"
  27. #include "avcodec.h"
  28. #include "internal.h"
  29. #include "rectangle.h"
  30. #include "thread.h"
  31. #include "vp8.h"
  32. #include "vp8data.h"
  33. #if ARCH_ARM
  34. # include "arm/vp8.h"
  35. #endif
  36. #if CONFIG_VP7_DECODER && CONFIG_VP8_DECODER
  37. #define VPX(vp7, f) (vp7 ? vp7_ ## f : vp8_ ## f)
  38. #elif CONFIG_VP7_DECODER
  39. #define VPX(vp7, f) vp7_ ## f
  40. #else // CONFIG_VP8_DECODER
  41. #define VPX(vp7, f) vp8_ ## f
  42. #endif
  43. static void free_buffers(VP8Context *s)
  44. {
  45. int i;
  46. if (s->thread_data)
  47. for (i = 0; i < MAX_THREADS; i++) {
  48. #if HAVE_THREADS
  49. pthread_cond_destroy(&s->thread_data[i].cond);
  50. pthread_mutex_destroy(&s->thread_data[i].lock);
  51. #endif
  52. av_freep(&s->thread_data[i].filter_strength);
  53. }
  54. av_freep(&s->thread_data);
  55. av_freep(&s->macroblocks_base);
  56. av_freep(&s->intra4x4_pred_mode_top);
  57. av_freep(&s->top_nnz);
  58. av_freep(&s->top_border);
  59. s->macroblocks = NULL;
  60. }
  61. static int vp8_alloc_frame(VP8Context *s, VP8Frame *f, int ref)
  62. {
  63. int ret;
  64. if ((ret = ff_thread_get_buffer(s->avctx, &f->tf,
  65. ref ? AV_GET_BUFFER_FLAG_REF : 0)) < 0)
  66. return ret;
  67. if (!(f->seg_map = av_buffer_allocz(s->mb_width * s->mb_height))) {
  68. ff_thread_release_buffer(s->avctx, &f->tf);
  69. return AVERROR(ENOMEM);
  70. }
  71. return 0;
  72. }
  73. static void vp8_release_frame(VP8Context *s, VP8Frame *f)
  74. {
  75. av_buffer_unref(&f->seg_map);
  76. ff_thread_release_buffer(s->avctx, &f->tf);
  77. }
  78. #if CONFIG_VP8_DECODER
  79. static int vp8_ref_frame(VP8Context *s, VP8Frame *dst, VP8Frame *src)
  80. {
  81. int ret;
  82. vp8_release_frame(s, dst);
  83. if ((ret = ff_thread_ref_frame(&dst->tf, &src->tf)) < 0)
  84. return ret;
  85. if (src->seg_map &&
  86. !(dst->seg_map = av_buffer_ref(src->seg_map))) {
  87. vp8_release_frame(s, dst);
  88. return AVERROR(ENOMEM);
  89. }
  90. return 0;
  91. }
  92. #endif /* CONFIG_VP8_DECODER */
  93. static void vp8_decode_flush_impl(AVCodecContext *avctx, int free_mem)
  94. {
  95. VP8Context *s = avctx->priv_data;
  96. int i;
  97. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
  98. vp8_release_frame(s, &s->frames[i]);
  99. memset(s->framep, 0, sizeof(s->framep));
  100. if (free_mem)
  101. free_buffers(s);
  102. }
  103. static void vp8_decode_flush(AVCodecContext *avctx)
  104. {
  105. vp8_decode_flush_impl(avctx, 0);
  106. }
  107. static VP8Frame *vp8_find_free_buffer(VP8Context *s)
  108. {
  109. VP8Frame *frame = NULL;
  110. int i;
  111. // find a free buffer
  112. for (i = 0; i < 5; i++)
  113. if (&s->frames[i] != s->framep[VP56_FRAME_CURRENT] &&
  114. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  115. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  116. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2]) {
  117. frame = &s->frames[i];
  118. break;
  119. }
  120. if (i == 5) {
  121. av_log(s->avctx, AV_LOG_FATAL, "Ran out of free frames!\n");
  122. abort();
  123. }
  124. if (frame->tf.f->data[0])
  125. vp8_release_frame(s, frame);
  126. return frame;
  127. }
  128. static av_always_inline
  129. int update_dimensions(VP8Context *s, int width, int height, int is_vp7)
  130. {
  131. AVCodecContext *avctx = s->avctx;
  132. int i, ret;
  133. if (width != s->avctx->width || ((width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height) && s->macroblocks_base ||
  134. height != s->avctx->height) {
  135. vp8_decode_flush_impl(s->avctx, 1);
  136. ret = ff_set_dimensions(s->avctx, width, height);
  137. if (ret < 0)
  138. return ret;
  139. }
  140. s->mb_width = (s->avctx->coded_width + 15) / 16;
  141. s->mb_height = (s->avctx->coded_height + 15) / 16;
  142. s->mb_layout = is_vp7 || avctx->active_thread_type == FF_THREAD_SLICE &&
  143. FFMIN(s->num_coeff_partitions, avctx->thread_count) > 1;
  144. if (!s->mb_layout) { // Frame threading and one thread
  145. s->macroblocks_base = av_mallocz((s->mb_width + s->mb_height * 2 + 1) *
  146. sizeof(*s->macroblocks));
  147. s->intra4x4_pred_mode_top = av_mallocz(s->mb_width * 4);
  148. } else // Sliced threading
  149. s->macroblocks_base = av_mallocz((s->mb_width + 2) * (s->mb_height + 2) *
  150. sizeof(*s->macroblocks));
  151. s->top_nnz = av_mallocz(s->mb_width * sizeof(*s->top_nnz));
  152. s->top_border = av_mallocz((s->mb_width + 1) * sizeof(*s->top_border));
  153. s->thread_data = av_mallocz(MAX_THREADS * sizeof(VP8ThreadData));
  154. for (i = 0; i < MAX_THREADS; i++) {
  155. s->thread_data[i].filter_strength =
  156. av_mallocz(s->mb_width * sizeof(*s->thread_data[0].filter_strength));
  157. #if HAVE_THREADS
  158. pthread_mutex_init(&s->thread_data[i].lock, NULL);
  159. pthread_cond_init(&s->thread_data[i].cond, NULL);
  160. #endif
  161. }
  162. if (!s->macroblocks_base || !s->top_nnz || !s->top_border ||
  163. (!s->intra4x4_pred_mode_top && !s->mb_layout))
  164. return AVERROR(ENOMEM);
  165. s->macroblocks = s->macroblocks_base + 1;
  166. return 0;
  167. }
  168. static int vp7_update_dimensions(VP8Context *s, int width, int height)
  169. {
  170. return update_dimensions(s, width, height, IS_VP7);
  171. }
  172. static int vp8_update_dimensions(VP8Context *s, int width, int height)
  173. {
  174. return update_dimensions(s, width, height, IS_VP8);
  175. }
  176. static void parse_segment_info(VP8Context *s)
  177. {
  178. VP56RangeCoder *c = &s->c;
  179. int i;
  180. s->segmentation.update_map = vp8_rac_get(c);
  181. if (vp8_rac_get(c)) { // update segment feature data
  182. s->segmentation.absolute_vals = vp8_rac_get(c);
  183. for (i = 0; i < 4; i++)
  184. s->segmentation.base_quant[i] = vp8_rac_get_sint(c, 7);
  185. for (i = 0; i < 4; i++)
  186. s->segmentation.filter_level[i] = vp8_rac_get_sint(c, 6);
  187. }
  188. if (s->segmentation.update_map)
  189. for (i = 0; i < 3; i++)
  190. s->prob->segmentid[i] = vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  191. }
  192. static void update_lf_deltas(VP8Context *s)
  193. {
  194. VP56RangeCoder *c = &s->c;
  195. int i;
  196. for (i = 0; i < 4; i++) {
  197. if (vp8_rac_get(c)) {
  198. s->lf_delta.ref[i] = vp8_rac_get_uint(c, 6);
  199. if (vp8_rac_get(c))
  200. s->lf_delta.ref[i] = -s->lf_delta.ref[i];
  201. }
  202. }
  203. for (i = MODE_I4x4; i <= VP8_MVMODE_SPLIT; i++) {
  204. if (vp8_rac_get(c)) {
  205. s->lf_delta.mode[i] = vp8_rac_get_uint(c, 6);
  206. if (vp8_rac_get(c))
  207. s->lf_delta.mode[i] = -s->lf_delta.mode[i];
  208. }
  209. }
  210. }
  211. static int setup_partitions(VP8Context *s, const uint8_t *buf, int buf_size)
  212. {
  213. const uint8_t *sizes = buf;
  214. int i;
  215. s->num_coeff_partitions = 1 << vp8_rac_get_uint(&s->c, 2);
  216. buf += 3 * (s->num_coeff_partitions - 1);
  217. buf_size -= 3 * (s->num_coeff_partitions - 1);
  218. if (buf_size < 0)
  219. return -1;
  220. for (i = 0; i < s->num_coeff_partitions - 1; i++) {
  221. int size = AV_RL24(sizes + 3 * i);
  222. if (buf_size - size < 0)
  223. return -1;
  224. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, size);
  225. buf += size;
  226. buf_size -= size;
  227. }
  228. ff_vp56_init_range_decoder(&s->coeff_partition[i], buf, buf_size);
  229. return 0;
  230. }
  231. static void vp7_get_quants(VP8Context *s)
  232. {
  233. VP56RangeCoder *c = &s->c;
  234. int yac_qi = vp8_rac_get_uint(c, 7);
  235. int ydc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  236. int y2dc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  237. int y2ac_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  238. int uvdc_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  239. int uvac_qi = vp8_rac_get(c) ? vp8_rac_get_uint(c, 7) : yac_qi;
  240. s->qmat[0].luma_qmul[0] = vp7_ydc_qlookup[ydc_qi];
  241. s->qmat[0].luma_qmul[1] = vp7_yac_qlookup[yac_qi];
  242. s->qmat[0].luma_dc_qmul[0] = vp7_y2dc_qlookup[y2dc_qi];
  243. s->qmat[0].luma_dc_qmul[1] = vp7_y2ac_qlookup[y2ac_qi];
  244. s->qmat[0].chroma_qmul[0] = FFMIN(vp7_ydc_qlookup[uvdc_qi], 132);
  245. s->qmat[0].chroma_qmul[1] = vp7_yac_qlookup[uvac_qi];
  246. }
  247. static void vp8_get_quants(VP8Context *s)
  248. {
  249. VP56RangeCoder *c = &s->c;
  250. int i, base_qi;
  251. int yac_qi = vp8_rac_get_uint(c, 7);
  252. int ydc_delta = vp8_rac_get_sint(c, 4);
  253. int y2dc_delta = vp8_rac_get_sint(c, 4);
  254. int y2ac_delta = vp8_rac_get_sint(c, 4);
  255. int uvdc_delta = vp8_rac_get_sint(c, 4);
  256. int uvac_delta = vp8_rac_get_sint(c, 4);
  257. for (i = 0; i < 4; i++) {
  258. if (s->segmentation.enabled) {
  259. base_qi = s->segmentation.base_quant[i];
  260. if (!s->segmentation.absolute_vals)
  261. base_qi += yac_qi;
  262. } else
  263. base_qi = yac_qi;
  264. s->qmat[i].luma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + ydc_delta, 7)];
  265. s->qmat[i].luma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi, 7)];
  266. s->qmat[i].luma_dc_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + y2dc_delta, 7)] * 2;
  267. /* 101581>>16 is equivalent to 155/100 */
  268. s->qmat[i].luma_dc_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + y2ac_delta, 7)] * 101581 >> 16;
  269. s->qmat[i].chroma_qmul[0] = vp8_dc_qlookup[av_clip_uintp2(base_qi + uvdc_delta, 7)];
  270. s->qmat[i].chroma_qmul[1] = vp8_ac_qlookup[av_clip_uintp2(base_qi + uvac_delta, 7)];
  271. s->qmat[i].luma_dc_qmul[1] = FFMAX(s->qmat[i].luma_dc_qmul[1], 8);
  272. s->qmat[i].chroma_qmul[0] = FFMIN(s->qmat[i].chroma_qmul[0], 132);
  273. }
  274. }
  275. /**
  276. * Determine which buffers golden and altref should be updated with after this frame.
  277. * The spec isn't clear here, so I'm going by my understanding of what libvpx does
  278. *
  279. * Intra frames update all 3 references
  280. * Inter frames update VP56_FRAME_PREVIOUS if the update_last flag is set
  281. * If the update (golden|altref) flag is set, it's updated with the current frame
  282. * if update_last is set, and VP56_FRAME_PREVIOUS otherwise.
  283. * If the flag is not set, the number read means:
  284. * 0: no update
  285. * 1: VP56_FRAME_PREVIOUS
  286. * 2: update golden with altref, or update altref with golden
  287. */
  288. static VP56Frame ref_to_update(VP8Context *s, int update, VP56Frame ref)
  289. {
  290. VP56RangeCoder *c = &s->c;
  291. if (update)
  292. return VP56_FRAME_CURRENT;
  293. switch (vp8_rac_get_uint(c, 2)) {
  294. case 1:
  295. return VP56_FRAME_PREVIOUS;
  296. case 2:
  297. return (ref == VP56_FRAME_GOLDEN) ? VP56_FRAME_GOLDEN2 : VP56_FRAME_GOLDEN;
  298. }
  299. return VP56_FRAME_NONE;
  300. }
  301. static void vp78_reset_probability_tables(VP8Context *s)
  302. {
  303. int i, j;
  304. for (i = 0; i < 4; i++)
  305. for (j = 0; j < 16; j++)
  306. memcpy(s->prob->token[i][j], vp8_token_default_probs[i][vp8_coeff_band[j]],
  307. sizeof(s->prob->token[i][j]));
  308. }
  309. static void vp78_update_probability_tables(VP8Context *s)
  310. {
  311. VP56RangeCoder *c = &s->c;
  312. int i, j, k, l, m;
  313. for (i = 0; i < 4; i++)
  314. for (j = 0; j < 8; j++)
  315. for (k = 0; k < 3; k++)
  316. for (l = 0; l < NUM_DCT_TOKENS-1; l++)
  317. if (vp56_rac_get_prob_branchy(c, vp8_token_update_probs[i][j][k][l])) {
  318. int prob = vp8_rac_get_uint(c, 8);
  319. for (m = 0; vp8_coeff_band_indexes[j][m] >= 0; m++)
  320. s->prob->token[i][vp8_coeff_band_indexes[j][m]][k][l] = prob;
  321. }
  322. }
  323. #define VP7_MVC_SIZE 17
  324. #define VP8_MVC_SIZE 19
  325. static void vp78_update_pred16x16_pred8x8_mvc_probabilities(VP8Context *s,
  326. int mvc_size)
  327. {
  328. VP56RangeCoder *c = &s->c;
  329. int i, j;
  330. if (vp8_rac_get(c))
  331. for (i = 0; i < 4; i++)
  332. s->prob->pred16x16[i] = vp8_rac_get_uint(c, 8);
  333. if (vp8_rac_get(c))
  334. for (i = 0; i < 3; i++)
  335. s->prob->pred8x8c[i] = vp8_rac_get_uint(c, 8);
  336. // 17.2 MV probability update
  337. for (i = 0; i < 2; i++)
  338. for (j = 0; j < mvc_size; j++)
  339. if (vp56_rac_get_prob_branchy(c, vp8_mv_update_prob[i][j]))
  340. s->prob->mvc[i][j] = vp8_rac_get_nn(c);
  341. }
  342. static void update_refs(VP8Context *s)
  343. {
  344. VP56RangeCoder *c = &s->c;
  345. int update_golden = vp8_rac_get(c);
  346. int update_altref = vp8_rac_get(c);
  347. s->update_golden = ref_to_update(s, update_golden, VP56_FRAME_GOLDEN);
  348. s->update_altref = ref_to_update(s, update_altref, VP56_FRAME_GOLDEN2);
  349. }
  350. static void copy_chroma(AVFrame *dst, AVFrame *src, int width, int height)
  351. {
  352. int i, j;
  353. for (j = 1; j < 3; j++) {
  354. for (i = 0; i < height / 2; i++)
  355. memcpy(dst->data[j] + i * dst->linesize[j],
  356. src->data[j] + i * src->linesize[j], width / 2);
  357. }
  358. }
  359. static void fade(uint8_t *dst, int dst_linesize,
  360. const uint8_t *src, int src_linesize,
  361. int width, int height,
  362. int alpha, int beta)
  363. {
  364. int i, j;
  365. for (j = 0; j < height; j++) {
  366. for (i = 0; i < width; i++) {
  367. uint8_t y = src[j * src_linesize + i];
  368. dst[j * dst_linesize + i] = av_clip_uint8(y + ((y * beta) >> 8) + alpha);
  369. }
  370. }
  371. }
  372. static int vp7_fade_frame(VP8Context *s, VP56RangeCoder *c)
  373. {
  374. int alpha = (int8_t) vp8_rac_get_uint(c, 8);
  375. int beta = (int8_t) vp8_rac_get_uint(c, 8);
  376. int ret;
  377. if (!s->keyframe && (alpha || beta)) {
  378. int width = s->mb_width * 16;
  379. int height = s->mb_height * 16;
  380. AVFrame *src, *dst;
  381. if (!s->framep[VP56_FRAME_PREVIOUS] ||
  382. !s->framep[VP56_FRAME_GOLDEN]) {
  383. av_log(s->avctx, AV_LOG_WARNING, "Discarding interframe without a prior keyframe!\n");
  384. return AVERROR_INVALIDDATA;
  385. }
  386. dst =
  387. src = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
  388. /* preserve the golden frame, write a new previous frame */
  389. if (s->framep[VP56_FRAME_GOLDEN] == s->framep[VP56_FRAME_PREVIOUS]) {
  390. s->framep[VP56_FRAME_PREVIOUS] = vp8_find_free_buffer(s);
  391. if ((ret = vp8_alloc_frame(s, s->framep[VP56_FRAME_PREVIOUS], 1)) < 0)
  392. return ret;
  393. dst = s->framep[VP56_FRAME_PREVIOUS]->tf.f;
  394. copy_chroma(dst, src, width, height);
  395. }
  396. fade(dst->data[0], dst->linesize[0],
  397. src->data[0], src->linesize[0],
  398. width, height, alpha, beta);
  399. }
  400. return 0;
  401. }
  402. static int vp7_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  403. {
  404. VP56RangeCoder *c = &s->c;
  405. int part1_size, hscale, vscale, i, j, ret;
  406. int width = s->avctx->width;
  407. int height = s->avctx->height;
  408. s->profile = (buf[0] >> 1) & 7;
  409. if (s->profile > 1) {
  410. avpriv_request_sample(s->avctx, "Unknown profile %d", s->profile);
  411. return AVERROR_INVALIDDATA;
  412. }
  413. s->keyframe = !(buf[0] & 1);
  414. s->invisible = 0;
  415. part1_size = AV_RL24(buf) >> 4;
  416. if (buf_size < 4 - s->profile + part1_size) {
  417. av_log(s->avctx, AV_LOG_ERROR, "Buffer size %d is too small, needed : %d\n", buf_size, 4 - s->profile + part1_size);
  418. return AVERROR_INVALIDDATA;
  419. }
  420. buf += 4 - s->profile;
  421. buf_size -= 4 - s->profile;
  422. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab, sizeof(s->put_pixels_tab));
  423. ff_vp56_init_range_decoder(c, buf, part1_size);
  424. buf += part1_size;
  425. buf_size -= part1_size;
  426. /* A. Dimension information (keyframes only) */
  427. if (s->keyframe) {
  428. width = vp8_rac_get_uint(c, 12);
  429. height = vp8_rac_get_uint(c, 12);
  430. hscale = vp8_rac_get_uint(c, 2);
  431. vscale = vp8_rac_get_uint(c, 2);
  432. if (hscale || vscale)
  433. avpriv_request_sample(s->avctx, "Upscaling");
  434. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  435. vp78_reset_probability_tables(s);
  436. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
  437. sizeof(s->prob->pred16x16));
  438. memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
  439. sizeof(s->prob->pred8x8c));
  440. for (i = 0; i < 2; i++)
  441. memcpy(s->prob->mvc[i], vp7_mv_default_prob[i],
  442. sizeof(vp7_mv_default_prob[i]));
  443. memset(&s->segmentation, 0, sizeof(s->segmentation));
  444. memset(&s->lf_delta, 0, sizeof(s->lf_delta));
  445. memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
  446. }
  447. if (s->keyframe || s->profile > 0)
  448. memset(s->inter_dc_pred, 0 , sizeof(s->inter_dc_pred));
  449. /* B. Decoding information for all four macroblock-level features */
  450. for (i = 0; i < 4; i++) {
  451. s->feature_enabled[i] = vp8_rac_get(c);
  452. if (s->feature_enabled[i]) {
  453. s->feature_present_prob[i] = vp8_rac_get_uint(c, 8);
  454. for (j = 0; j < 3; j++)
  455. s->feature_index_prob[i][j] =
  456. vp8_rac_get(c) ? vp8_rac_get_uint(c, 8) : 255;
  457. if (vp7_feature_value_size[s->profile][i])
  458. for (j = 0; j < 4; j++)
  459. s->feature_value[i][j] =
  460. vp8_rac_get(c) ? vp8_rac_get_uint(c, vp7_feature_value_size[s->profile][i]) : 0;
  461. }
  462. }
  463. s->segmentation.enabled = 0;
  464. s->segmentation.update_map = 0;
  465. s->lf_delta.enabled = 0;
  466. s->num_coeff_partitions = 1;
  467. ff_vp56_init_range_decoder(&s->coeff_partition[0], buf, buf_size);
  468. if (!s->macroblocks_base || /* first frame */
  469. width != s->avctx->width || height != s->avctx->height ||
  470. (width + 15) / 16 != s->mb_width || (height + 15) / 16 != s->mb_height) {
  471. if ((ret = vp7_update_dimensions(s, width, height)) < 0)
  472. return ret;
  473. }
  474. /* C. Dequantization indices */
  475. vp7_get_quants(s);
  476. /* D. Golden frame update flag (a Flag) for interframes only */
  477. if (!s->keyframe) {
  478. s->update_golden = vp8_rac_get(c) ? VP56_FRAME_CURRENT : VP56_FRAME_NONE;
  479. s->sign_bias[VP56_FRAME_GOLDEN] = 0;
  480. }
  481. s->update_last = 1;
  482. s->update_probabilities = 1;
  483. s->fade_present = 1;
  484. if (s->profile > 0) {
  485. s->update_probabilities = vp8_rac_get(c);
  486. if (!s->update_probabilities)
  487. s->prob[1] = s->prob[0];
  488. if (!s->keyframe)
  489. s->fade_present = vp8_rac_get(c);
  490. }
  491. /* E. Fading information for previous frame */
  492. if (s->fade_present && vp8_rac_get(c)) {
  493. if ((ret = vp7_fade_frame(s ,c)) < 0)
  494. return ret;
  495. }
  496. /* F. Loop filter type */
  497. if (!s->profile)
  498. s->filter.simple = vp8_rac_get(c);
  499. /* G. DCT coefficient ordering specification */
  500. if (vp8_rac_get(c))
  501. for (i = 1; i < 16; i++)
  502. s->prob[0].scan[i] = zigzag_scan[vp8_rac_get_uint(c, 4)];
  503. /* H. Loop filter levels */
  504. if (s->profile > 0)
  505. s->filter.simple = vp8_rac_get(c);
  506. s->filter.level = vp8_rac_get_uint(c, 6);
  507. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  508. /* I. DCT coefficient probability update; 13.3 Token Probability Updates */
  509. vp78_update_probability_tables(s);
  510. s->mbskip_enabled = 0;
  511. /* J. The remaining frame header data occurs ONLY FOR INTERFRAMES */
  512. if (!s->keyframe) {
  513. s->prob->intra = vp8_rac_get_uint(c, 8);
  514. s->prob->last = vp8_rac_get_uint(c, 8);
  515. vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP7_MVC_SIZE);
  516. }
  517. return 0;
  518. }
  519. static int vp8_decode_frame_header(VP8Context *s, const uint8_t *buf, int buf_size)
  520. {
  521. VP56RangeCoder *c = &s->c;
  522. int header_size, hscale, vscale, ret;
  523. int width = s->avctx->width;
  524. int height = s->avctx->height;
  525. s->keyframe = !(buf[0] & 1);
  526. s->profile = (buf[0]>>1) & 7;
  527. s->invisible = !(buf[0] & 0x10);
  528. header_size = AV_RL24(buf) >> 5;
  529. buf += 3;
  530. buf_size -= 3;
  531. if (s->profile > 3)
  532. av_log(s->avctx, AV_LOG_WARNING, "Unknown profile %d\n", s->profile);
  533. if (!s->profile)
  534. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_epel_pixels_tab,
  535. sizeof(s->put_pixels_tab));
  536. else // profile 1-3 use bilinear, 4+ aren't defined so whatever
  537. memcpy(s->put_pixels_tab, s->vp8dsp.put_vp8_bilinear_pixels_tab,
  538. sizeof(s->put_pixels_tab));
  539. if (header_size > buf_size - 7 * s->keyframe) {
  540. av_log(s->avctx, AV_LOG_ERROR, "Header size larger than data provided\n");
  541. return AVERROR_INVALIDDATA;
  542. }
  543. if (s->keyframe) {
  544. if (AV_RL24(buf) != 0x2a019d) {
  545. av_log(s->avctx, AV_LOG_ERROR,
  546. "Invalid start code 0x%x\n", AV_RL24(buf));
  547. return AVERROR_INVALIDDATA;
  548. }
  549. width = AV_RL16(buf + 3) & 0x3fff;
  550. height = AV_RL16(buf + 5) & 0x3fff;
  551. hscale = buf[4] >> 6;
  552. vscale = buf[6] >> 6;
  553. buf += 7;
  554. buf_size -= 7;
  555. if (hscale || vscale)
  556. avpriv_request_sample(s->avctx, "Upscaling");
  557. s->update_golden = s->update_altref = VP56_FRAME_CURRENT;
  558. vp78_reset_probability_tables(s);
  559. memcpy(s->prob->pred16x16, vp8_pred16x16_prob_inter,
  560. sizeof(s->prob->pred16x16));
  561. memcpy(s->prob->pred8x8c, vp8_pred8x8c_prob_inter,
  562. sizeof(s->prob->pred8x8c));
  563. memcpy(s->prob->mvc, vp8_mv_default_prob,
  564. sizeof(s->prob->mvc));
  565. memset(&s->segmentation, 0, sizeof(s->segmentation));
  566. memset(&s->lf_delta, 0, sizeof(s->lf_delta));
  567. }
  568. ff_vp56_init_range_decoder(c, buf, header_size);
  569. buf += header_size;
  570. buf_size -= header_size;
  571. if (s->keyframe) {
  572. if (vp8_rac_get(c))
  573. av_log(s->avctx, AV_LOG_WARNING, "Unspecified colorspace\n");
  574. vp8_rac_get(c); // whether we can skip clamping in dsp functions
  575. }
  576. if ((s->segmentation.enabled = vp8_rac_get(c)))
  577. parse_segment_info(s);
  578. else
  579. s->segmentation.update_map = 0; // FIXME: move this to some init function?
  580. s->filter.simple = vp8_rac_get(c);
  581. s->filter.level = vp8_rac_get_uint(c, 6);
  582. s->filter.sharpness = vp8_rac_get_uint(c, 3);
  583. if ((s->lf_delta.enabled = vp8_rac_get(c)))
  584. if (vp8_rac_get(c))
  585. update_lf_deltas(s);
  586. if (setup_partitions(s, buf, buf_size)) {
  587. av_log(s->avctx, AV_LOG_ERROR, "Invalid partitions\n");
  588. return AVERROR_INVALIDDATA;
  589. }
  590. if (!s->macroblocks_base || /* first frame */
  591. width != s->avctx->width || height != s->avctx->height ||
  592. (width+15)/16 != s->mb_width || (height+15)/16 != s->mb_height)
  593. if ((ret = vp8_update_dimensions(s, width, height)) < 0)
  594. return ret;
  595. vp8_get_quants(s);
  596. if (!s->keyframe) {
  597. update_refs(s);
  598. s->sign_bias[VP56_FRAME_GOLDEN] = vp8_rac_get(c);
  599. s->sign_bias[VP56_FRAME_GOLDEN2 /* altref */] = vp8_rac_get(c);
  600. }
  601. // if we aren't saving this frame's probabilities for future frames,
  602. // make a copy of the current probabilities
  603. if (!(s->update_probabilities = vp8_rac_get(c)))
  604. s->prob[1] = s->prob[0];
  605. s->update_last = s->keyframe || vp8_rac_get(c);
  606. vp78_update_probability_tables(s);
  607. if ((s->mbskip_enabled = vp8_rac_get(c)))
  608. s->prob->mbskip = vp8_rac_get_uint(c, 8);
  609. if (!s->keyframe) {
  610. s->prob->intra = vp8_rac_get_uint(c, 8);
  611. s->prob->last = vp8_rac_get_uint(c, 8);
  612. s->prob->golden = vp8_rac_get_uint(c, 8);
  613. vp78_update_pred16x16_pred8x8_mvc_probabilities(s, VP8_MVC_SIZE);
  614. }
  615. return 0;
  616. }
  617. static av_always_inline
  618. void clamp_mv(VP8Context *s, VP56mv *dst, const VP56mv *src)
  619. {
  620. dst->x = av_clip(src->x, s->mv_min.x, s->mv_max.x);
  621. dst->y = av_clip(src->y, s->mv_min.y, s->mv_max.y);
  622. }
  623. /**
  624. * Motion vector coding, 17.1.
  625. */
  626. static av_always_inline int read_mv_component(VP56RangeCoder *c, const uint8_t *p, int vp7)
  627. {
  628. int bit, x = 0;
  629. if (vp56_rac_get_prob_branchy(c, p[0])) {
  630. int i;
  631. for (i = 0; i < 3; i++)
  632. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  633. for (i = (vp7 ? 7 : 9); i > 3; i--)
  634. x += vp56_rac_get_prob(c, p[9 + i]) << i;
  635. if (!(x & (vp7 ? 0xF0 : 0xFFF0)) || vp56_rac_get_prob(c, p[12]))
  636. x += 8;
  637. } else {
  638. // small_mvtree
  639. const uint8_t *ps = p + 2;
  640. bit = vp56_rac_get_prob(c, *ps);
  641. ps += 1 + 3 * bit;
  642. x += 4 * bit;
  643. bit = vp56_rac_get_prob(c, *ps);
  644. ps += 1 + bit;
  645. x += 2 * bit;
  646. x += vp56_rac_get_prob(c, *ps);
  647. }
  648. return (x && vp56_rac_get_prob(c, p[1])) ? -x : x;
  649. }
  650. static int vp7_read_mv_component(VP56RangeCoder *c, const uint8_t *p)
  651. {
  652. return read_mv_component(c, p, 1);
  653. }
  654. static int vp8_read_mv_component(VP56RangeCoder *c, const uint8_t *p)
  655. {
  656. return read_mv_component(c, p, 0);
  657. }
  658. static av_always_inline
  659. const uint8_t *get_submv_prob(uint32_t left, uint32_t top, int is_vp7)
  660. {
  661. if (is_vp7)
  662. return vp7_submv_prob;
  663. if (left == top)
  664. return vp8_submv_prob[4 - !!left];
  665. if (!top)
  666. return vp8_submv_prob[2];
  667. return vp8_submv_prob[1 - !!left];
  668. }
  669. /**
  670. * Split motion vector prediction, 16.4.
  671. * @returns the number of motion vectors parsed (2, 4 or 16)
  672. */
  673. static av_always_inline
  674. int decode_splitmvs(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  675. int layout, int is_vp7)
  676. {
  677. int part_idx;
  678. int n, num;
  679. VP8Macroblock *top_mb;
  680. VP8Macroblock *left_mb = &mb[-1];
  681. const uint8_t *mbsplits_left = vp8_mbsplits[left_mb->partitioning];
  682. const uint8_t *mbsplits_top, *mbsplits_cur, *firstidx;
  683. VP56mv *top_mv;
  684. VP56mv *left_mv = left_mb->bmv;
  685. VP56mv *cur_mv = mb->bmv;
  686. if (!layout) // layout is inlined, s->mb_layout is not
  687. top_mb = &mb[2];
  688. else
  689. top_mb = &mb[-s->mb_width - 1];
  690. mbsplits_top = vp8_mbsplits[top_mb->partitioning];
  691. top_mv = top_mb->bmv;
  692. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[0])) {
  693. if (vp56_rac_get_prob_branchy(c, vp8_mbsplit_prob[1]))
  694. part_idx = VP8_SPLITMVMODE_16x8 + vp56_rac_get_prob(c, vp8_mbsplit_prob[2]);
  695. else
  696. part_idx = VP8_SPLITMVMODE_8x8;
  697. } else {
  698. part_idx = VP8_SPLITMVMODE_4x4;
  699. }
  700. num = vp8_mbsplit_count[part_idx];
  701. mbsplits_cur = vp8_mbsplits[part_idx],
  702. firstidx = vp8_mbfirstidx[part_idx];
  703. mb->partitioning = part_idx;
  704. for (n = 0; n < num; n++) {
  705. int k = firstidx[n];
  706. uint32_t left, above;
  707. const uint8_t *submv_prob;
  708. if (!(k & 3))
  709. left = AV_RN32A(&left_mv[mbsplits_left[k + 3]]);
  710. else
  711. left = AV_RN32A(&cur_mv[mbsplits_cur[k - 1]]);
  712. if (k <= 3)
  713. above = AV_RN32A(&top_mv[mbsplits_top[k + 12]]);
  714. else
  715. above = AV_RN32A(&cur_mv[mbsplits_cur[k - 4]]);
  716. submv_prob = get_submv_prob(left, above, is_vp7);
  717. if (vp56_rac_get_prob_branchy(c, submv_prob[0])) {
  718. if (vp56_rac_get_prob_branchy(c, submv_prob[1])) {
  719. if (vp56_rac_get_prob_branchy(c, submv_prob[2])) {
  720. mb->bmv[n].y = mb->mv.y +
  721. read_mv_component(c, s->prob->mvc[0], is_vp7);
  722. mb->bmv[n].x = mb->mv.x +
  723. read_mv_component(c, s->prob->mvc[1], is_vp7);
  724. } else {
  725. AV_ZERO32(&mb->bmv[n]);
  726. }
  727. } else {
  728. AV_WN32A(&mb->bmv[n], above);
  729. }
  730. } else {
  731. AV_WN32A(&mb->bmv[n], left);
  732. }
  733. }
  734. return num;
  735. }
  736. /**
  737. * The vp7 reference decoder uses a padding macroblock column (added to right
  738. * edge of the frame) to guard against illegal macroblock offsets. The
  739. * algorithm has bugs that permit offsets to straddle the padding column.
  740. * This function replicates those bugs.
  741. *
  742. * @param[out] edge_x macroblock x address
  743. * @param[out] edge_y macroblock y address
  744. *
  745. * @return macroblock offset legal (boolean)
  746. */
  747. static int vp7_calculate_mb_offset(int mb_x, int mb_y, int mb_width,
  748. int xoffset, int yoffset, int boundary,
  749. int *edge_x, int *edge_y)
  750. {
  751. int vwidth = mb_width + 1;
  752. int new = (mb_y + yoffset) * vwidth + mb_x + xoffset;
  753. if (new < boundary || new % vwidth == vwidth - 1)
  754. return 0;
  755. *edge_y = new / vwidth;
  756. *edge_x = new % vwidth;
  757. return 1;
  758. }
  759. static const VP56mv *get_bmv_ptr(const VP8Macroblock *mb, int subblock)
  760. {
  761. return &mb->bmv[mb->mode == VP8_MVMODE_SPLIT ? vp8_mbsplits[mb->partitioning][subblock] : 0];
  762. }
  763. static av_always_inline
  764. void vp7_decode_mvs(VP8Context *s, VP8Macroblock *mb,
  765. int mb_x, int mb_y, int layout)
  766. {
  767. VP8Macroblock *mb_edge[12];
  768. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR };
  769. enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
  770. int idx = CNT_ZERO;
  771. VP56mv near_mv[3];
  772. uint8_t cnt[3] = { 0 };
  773. VP56RangeCoder *c = &s->c;
  774. int i;
  775. AV_ZERO32(&near_mv[0]);
  776. AV_ZERO32(&near_mv[1]);
  777. AV_ZERO32(&near_mv[2]);
  778. for (i = 0; i < VP7_MV_PRED_COUNT; i++) {
  779. const VP7MVPred * pred = &vp7_mv_pred[i];
  780. int edge_x, edge_y;
  781. if (vp7_calculate_mb_offset(mb_x, mb_y, s->mb_width, pred->xoffset,
  782. pred->yoffset, !s->profile, &edge_x, &edge_y)) {
  783. VP8Macroblock *edge = mb_edge[i] = (s->mb_layout == 1)
  784. ? s->macroblocks_base + 1 + edge_x +
  785. (s->mb_width + 1) * (edge_y + 1)
  786. : s->macroblocks + edge_x +
  787. (s->mb_height - edge_y - 1) * 2;
  788. uint32_t mv = AV_RN32A(get_bmv_ptr(edge, vp7_mv_pred[i].subblock));
  789. if (mv) {
  790. if (AV_RN32A(&near_mv[CNT_NEAREST])) {
  791. if (mv == AV_RN32A(&near_mv[CNT_NEAREST])) {
  792. idx = CNT_NEAREST;
  793. } else if (AV_RN32A(&near_mv[CNT_NEAR])) {
  794. if (mv != AV_RN32A(&near_mv[CNT_NEAR]))
  795. continue;
  796. idx = CNT_NEAR;
  797. } else {
  798. AV_WN32A(&near_mv[CNT_NEAR], mv);
  799. idx = CNT_NEAR;
  800. }
  801. } else {
  802. AV_WN32A(&near_mv[CNT_NEAREST], mv);
  803. idx = CNT_NEAREST;
  804. }
  805. } else {
  806. idx = CNT_ZERO;
  807. }
  808. } else {
  809. idx = CNT_ZERO;
  810. }
  811. cnt[idx] += vp7_mv_pred[i].score;
  812. }
  813. mb->partitioning = VP8_SPLITMVMODE_NONE;
  814. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_ZERO]][0])) {
  815. mb->mode = VP8_MVMODE_MV;
  816. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAREST]][1])) {
  817. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][2])) {
  818. if (cnt[CNT_NEAREST] > cnt[CNT_NEAR])
  819. AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAREST] ? 0 : AV_RN32A(&near_mv[CNT_NEAREST]));
  820. else
  821. AV_WN32A(&mb->mv, cnt[CNT_ZERO] > cnt[CNT_NEAR] ? 0 : AV_RN32A(&near_mv[CNT_NEAR]));
  822. if (vp56_rac_get_prob_branchy(c, vp7_mode_contexts[cnt[CNT_NEAR]][3])) {
  823. mb->mode = VP8_MVMODE_SPLIT;
  824. mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP7) - 1];
  825. } else {
  826. mb->mv.y += vp7_read_mv_component(c, s->prob->mvc[0]);
  827. mb->mv.x += vp7_read_mv_component(c, s->prob->mvc[1]);
  828. mb->bmv[0] = mb->mv;
  829. }
  830. } else {
  831. mb->mv = near_mv[CNT_NEAR];
  832. mb->bmv[0] = mb->mv;
  833. }
  834. } else {
  835. mb->mv = near_mv[CNT_NEAREST];
  836. mb->bmv[0] = mb->mv;
  837. }
  838. } else {
  839. mb->mode = VP8_MVMODE_ZERO;
  840. AV_ZERO32(&mb->mv);
  841. mb->bmv[0] = mb->mv;
  842. }
  843. }
  844. static av_always_inline
  845. void vp8_decode_mvs(VP8Context *s, VP8Macroblock *mb,
  846. int mb_x, int mb_y, int layout)
  847. {
  848. VP8Macroblock *mb_edge[3] = { 0 /* top */,
  849. mb - 1 /* left */,
  850. 0 /* top-left */ };
  851. enum { CNT_ZERO, CNT_NEAREST, CNT_NEAR, CNT_SPLITMV };
  852. enum { VP8_EDGE_TOP, VP8_EDGE_LEFT, VP8_EDGE_TOPLEFT };
  853. int idx = CNT_ZERO;
  854. int cur_sign_bias = s->sign_bias[mb->ref_frame];
  855. int8_t *sign_bias = s->sign_bias;
  856. VP56mv near_mv[4];
  857. uint8_t cnt[4] = { 0 };
  858. VP56RangeCoder *c = &s->c;
  859. if (!layout) { // layout is inlined (s->mb_layout is not)
  860. mb_edge[0] = mb + 2;
  861. mb_edge[2] = mb + 1;
  862. } else {
  863. mb_edge[0] = mb - s->mb_width - 1;
  864. mb_edge[2] = mb - s->mb_width - 2;
  865. }
  866. AV_ZERO32(&near_mv[0]);
  867. AV_ZERO32(&near_mv[1]);
  868. AV_ZERO32(&near_mv[2]);
  869. /* Process MB on top, left and top-left */
  870. #define MV_EDGE_CHECK(n) \
  871. { \
  872. VP8Macroblock *edge = mb_edge[n]; \
  873. int edge_ref = edge->ref_frame; \
  874. if (edge_ref != VP56_FRAME_CURRENT) { \
  875. uint32_t mv = AV_RN32A(&edge->mv); \
  876. if (mv) { \
  877. if (cur_sign_bias != sign_bias[edge_ref]) { \
  878. /* SWAR negate of the values in mv. */ \
  879. mv = ~mv; \
  880. mv = ((mv & 0x7fff7fff) + \
  881. 0x00010001) ^ (mv & 0x80008000); \
  882. } \
  883. if (!n || mv != AV_RN32A(&near_mv[idx])) \
  884. AV_WN32A(&near_mv[++idx], mv); \
  885. cnt[idx] += 1 + (n != 2); \
  886. } else \
  887. cnt[CNT_ZERO] += 1 + (n != 2); \
  888. } \
  889. }
  890. MV_EDGE_CHECK(0)
  891. MV_EDGE_CHECK(1)
  892. MV_EDGE_CHECK(2)
  893. mb->partitioning = VP8_SPLITMVMODE_NONE;
  894. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_ZERO]][0])) {
  895. mb->mode = VP8_MVMODE_MV;
  896. /* If we have three distinct MVs, merge first and last if they're the same */
  897. if (cnt[CNT_SPLITMV] &&
  898. AV_RN32A(&near_mv[1 + VP8_EDGE_TOP]) == AV_RN32A(&near_mv[1 + VP8_EDGE_TOPLEFT]))
  899. cnt[CNT_NEAREST] += 1;
  900. /* Swap near and nearest if necessary */
  901. if (cnt[CNT_NEAR] > cnt[CNT_NEAREST]) {
  902. FFSWAP(uint8_t, cnt[CNT_NEAREST], cnt[CNT_NEAR]);
  903. FFSWAP( VP56mv, near_mv[CNT_NEAREST], near_mv[CNT_NEAR]);
  904. }
  905. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAREST]][1])) {
  906. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_NEAR]][2])) {
  907. /* Choose the best mv out of 0,0 and the nearest mv */
  908. clamp_mv(s, &mb->mv, &near_mv[CNT_ZERO + (cnt[CNT_NEAREST] >= cnt[CNT_ZERO])]);
  909. cnt[CNT_SPLITMV] = ((mb_edge[VP8_EDGE_LEFT]->mode == VP8_MVMODE_SPLIT) +
  910. (mb_edge[VP8_EDGE_TOP]->mode == VP8_MVMODE_SPLIT)) * 2 +
  911. (mb_edge[VP8_EDGE_TOPLEFT]->mode == VP8_MVMODE_SPLIT);
  912. if (vp56_rac_get_prob_branchy(c, vp8_mode_contexts[cnt[CNT_SPLITMV]][3])) {
  913. mb->mode = VP8_MVMODE_SPLIT;
  914. mb->mv = mb->bmv[decode_splitmvs(s, c, mb, layout, IS_VP8) - 1];
  915. } else {
  916. mb->mv.y += vp8_read_mv_component(c, s->prob->mvc[0]);
  917. mb->mv.x += vp8_read_mv_component(c, s->prob->mvc[1]);
  918. mb->bmv[0] = mb->mv;
  919. }
  920. } else {
  921. clamp_mv(s, &mb->mv, &near_mv[CNT_NEAR]);
  922. mb->bmv[0] = mb->mv;
  923. }
  924. } else {
  925. clamp_mv(s, &mb->mv, &near_mv[CNT_NEAREST]);
  926. mb->bmv[0] = mb->mv;
  927. }
  928. } else {
  929. mb->mode = VP8_MVMODE_ZERO;
  930. AV_ZERO32(&mb->mv);
  931. mb->bmv[0] = mb->mv;
  932. }
  933. }
  934. static av_always_inline
  935. void decode_intra4x4_modes(VP8Context *s, VP56RangeCoder *c, VP8Macroblock *mb,
  936. int mb_x, int keyframe, int layout)
  937. {
  938. uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
  939. if (layout) {
  940. VP8Macroblock *mb_top = mb - s->mb_width - 1;
  941. memcpy(mb->intra4x4_pred_mode_top, mb_top->intra4x4_pred_mode_top, 4);
  942. }
  943. if (keyframe) {
  944. int x, y;
  945. uint8_t *top;
  946. uint8_t *const left = s->intra4x4_pred_mode_left;
  947. if (layout)
  948. top = mb->intra4x4_pred_mode_top;
  949. else
  950. top = s->intra4x4_pred_mode_top + 4 * mb_x;
  951. for (y = 0; y < 4; y++) {
  952. for (x = 0; x < 4; x++) {
  953. const uint8_t *ctx;
  954. ctx = vp8_pred4x4_prob_intra[top[x]][left[y]];
  955. *intra4x4 = vp8_rac_get_tree(c, vp8_pred4x4_tree, ctx);
  956. left[y] = top[x] = *intra4x4;
  957. intra4x4++;
  958. }
  959. }
  960. } else {
  961. int i;
  962. for (i = 0; i < 16; i++)
  963. intra4x4[i] = vp8_rac_get_tree(c, vp8_pred4x4_tree,
  964. vp8_pred4x4_prob_inter);
  965. }
  966. }
  967. static av_always_inline
  968. void decode_mb_mode(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
  969. uint8_t *segment, uint8_t *ref, int layout, int is_vp7)
  970. {
  971. VP56RangeCoder *c = &s->c;
  972. const char *vp7_feature_name[] = { "q-index",
  973. "lf-delta",
  974. "partial-golden-update",
  975. "blit-pitch" };
  976. if (is_vp7) {
  977. int i;
  978. *segment = 0;
  979. for (i = 0; i < 4; i++) {
  980. if (s->feature_enabled[i]) {
  981. if (vp56_rac_get_prob_branchy(c, s->feature_present_prob[i])) {
  982. int index = vp8_rac_get_tree(c, vp7_feature_index_tree,
  983. s->feature_index_prob[i]);
  984. av_log(s->avctx, AV_LOG_WARNING,
  985. "Feature %s present in macroblock (value 0x%x)\n",
  986. vp7_feature_name[i], s->feature_value[i][index]);
  987. }
  988. }
  989. }
  990. } else if (s->segmentation.update_map) {
  991. int bit = vp56_rac_get_prob(c, s->prob->segmentid[0]);
  992. *segment = vp56_rac_get_prob(c, s->prob->segmentid[1+bit]) + 2*bit;
  993. } else if (s->segmentation.enabled)
  994. *segment = ref ? *ref : *segment;
  995. mb->segment = *segment;
  996. mb->skip = s->mbskip_enabled ? vp56_rac_get_prob(c, s->prob->mbskip) : 0;
  997. if (s->keyframe) {
  998. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_intra,
  999. vp8_pred16x16_prob_intra);
  1000. if (mb->mode == MODE_I4x4) {
  1001. decode_intra4x4_modes(s, c, mb, mb_x, 1, layout);
  1002. } else {
  1003. const uint32_t modes = (is_vp7 ? vp7_pred4x4_mode
  1004. : vp8_pred4x4_mode)[mb->mode] * 0x01010101u;
  1005. if (s->mb_layout)
  1006. AV_WN32A(mb->intra4x4_pred_mode_top, modes);
  1007. else
  1008. AV_WN32A(s->intra4x4_pred_mode_top + 4 * mb_x, modes);
  1009. AV_WN32A(s->intra4x4_pred_mode_left, modes);
  1010. }
  1011. mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
  1012. vp8_pred8x8c_prob_intra);
  1013. mb->ref_frame = VP56_FRAME_CURRENT;
  1014. } else if (vp56_rac_get_prob_branchy(c, s->prob->intra)) {
  1015. // inter MB, 16.2
  1016. if (vp56_rac_get_prob_branchy(c, s->prob->last))
  1017. mb->ref_frame =
  1018. (!is_vp7 && vp56_rac_get_prob(c, s->prob->golden)) ? VP56_FRAME_GOLDEN2 /* altref */
  1019. : VP56_FRAME_GOLDEN;
  1020. else
  1021. mb->ref_frame = VP56_FRAME_PREVIOUS;
  1022. s->ref_count[mb->ref_frame - 1]++;
  1023. // motion vectors, 16.3
  1024. if (is_vp7)
  1025. vp7_decode_mvs(s, mb, mb_x, mb_y, layout);
  1026. else
  1027. vp8_decode_mvs(s, mb, mb_x, mb_y, layout);
  1028. } else {
  1029. // intra MB, 16.1
  1030. mb->mode = vp8_rac_get_tree(c, vp8_pred16x16_tree_inter, s->prob->pred16x16);
  1031. if (mb->mode == MODE_I4x4)
  1032. decode_intra4x4_modes(s, c, mb, mb_x, 0, layout);
  1033. mb->chroma_pred_mode = vp8_rac_get_tree(c, vp8_pred8x8c_tree,
  1034. s->prob->pred8x8c);
  1035. mb->ref_frame = VP56_FRAME_CURRENT;
  1036. mb->partitioning = VP8_SPLITMVMODE_NONE;
  1037. AV_ZERO32(&mb->bmv[0]);
  1038. }
  1039. }
  1040. /**
  1041. * @param r arithmetic bitstream reader context
  1042. * @param block destination for block coefficients
  1043. * @param probs probabilities to use when reading trees from the bitstream
  1044. * @param i initial coeff index, 0 unless a separate DC block is coded
  1045. * @param qmul array holding the dc/ac dequant factor at position 0/1
  1046. *
  1047. * @return 0 if no coeffs were decoded
  1048. * otherwise, the index of the last coeff decoded plus one
  1049. */
  1050. static av_always_inline
  1051. int decode_block_coeffs_internal(VP56RangeCoder *r, int16_t block[16],
  1052. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1053. int i, uint8_t *token_prob, int16_t qmul[2],
  1054. const uint8_t scan[16], int vp7)
  1055. {
  1056. VP56RangeCoder c = *r;
  1057. goto skip_eob;
  1058. do {
  1059. int coeff;
  1060. restart:
  1061. if (!vp56_rac_get_prob_branchy(&c, token_prob[0])) // DCT_EOB
  1062. break;
  1063. skip_eob:
  1064. if (!vp56_rac_get_prob_branchy(&c, token_prob[1])) { // DCT_0
  1065. if (++i == 16)
  1066. break; // invalid input; blocks should end with EOB
  1067. token_prob = probs[i][0];
  1068. if (vp7)
  1069. goto restart;
  1070. goto skip_eob;
  1071. }
  1072. if (!vp56_rac_get_prob_branchy(&c, token_prob[2])) { // DCT_1
  1073. coeff = 1;
  1074. token_prob = probs[i + 1][1];
  1075. } else {
  1076. if (!vp56_rac_get_prob_branchy(&c, token_prob[3])) { // DCT 2,3,4
  1077. coeff = vp56_rac_get_prob_branchy(&c, token_prob[4]);
  1078. if (coeff)
  1079. coeff += vp56_rac_get_prob(&c, token_prob[5]);
  1080. coeff += 2;
  1081. } else {
  1082. // DCT_CAT*
  1083. if (!vp56_rac_get_prob_branchy(&c, token_prob[6])) {
  1084. if (!vp56_rac_get_prob_branchy(&c, token_prob[7])) { // DCT_CAT1
  1085. coeff = 5 + vp56_rac_get_prob(&c, vp8_dct_cat1_prob[0]);
  1086. } else { // DCT_CAT2
  1087. coeff = 7;
  1088. coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[0]) << 1;
  1089. coeff += vp56_rac_get_prob(&c, vp8_dct_cat2_prob[1]);
  1090. }
  1091. } else { // DCT_CAT3 and up
  1092. int a = vp56_rac_get_prob(&c, token_prob[8]);
  1093. int b = vp56_rac_get_prob(&c, token_prob[9 + a]);
  1094. int cat = (a << 1) + b;
  1095. coeff = 3 + (8 << cat);
  1096. coeff += vp8_rac_get_coeff(&c, ff_vp8_dct_cat_prob[cat]);
  1097. }
  1098. }
  1099. token_prob = probs[i + 1][2];
  1100. }
  1101. block[scan[i]] = (vp8_rac_get(&c) ? -coeff : coeff) * qmul[!!i];
  1102. } while (++i < 16);
  1103. *r = c;
  1104. return i;
  1105. }
  1106. static av_always_inline
  1107. int inter_predict_dc(int16_t block[16], int16_t pred[2])
  1108. {
  1109. int16_t dc = block[0];
  1110. int ret = 0;
  1111. if (pred[1] > 3) {
  1112. dc += pred[0];
  1113. ret = 1;
  1114. }
  1115. if (!pred[0] | !dc | ((int32_t)pred[0] ^ (int32_t)dc) >> 31) {
  1116. block[0] = pred[0] = dc;
  1117. pred[1] = 0;
  1118. } else {
  1119. if (pred[0] == dc)
  1120. pred[1]++;
  1121. block[0] = pred[0] = dc;
  1122. }
  1123. return ret;
  1124. }
  1125. static int vp7_decode_block_coeffs_internal(VP56RangeCoder *r,
  1126. int16_t block[16],
  1127. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1128. int i, uint8_t *token_prob,
  1129. int16_t qmul[2],
  1130. const uint8_t scan[16])
  1131. {
  1132. return decode_block_coeffs_internal(r, block, probs, i,
  1133. token_prob, qmul, scan, IS_VP7);
  1134. }
  1135. #ifndef vp8_decode_block_coeffs_internal
  1136. static int vp8_decode_block_coeffs_internal(VP56RangeCoder *r,
  1137. int16_t block[16],
  1138. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1139. int i, uint8_t *token_prob,
  1140. int16_t qmul[2])
  1141. {
  1142. return decode_block_coeffs_internal(r, block, probs, i,
  1143. token_prob, qmul, zigzag_scan, IS_VP8);
  1144. }
  1145. #endif
  1146. /**
  1147. * @param c arithmetic bitstream reader context
  1148. * @param block destination for block coefficients
  1149. * @param probs probabilities to use when reading trees from the bitstream
  1150. * @param i initial coeff index, 0 unless a separate DC block is coded
  1151. * @param zero_nhood the initial prediction context for number of surrounding
  1152. * all-zero blocks (only left/top, so 0-2)
  1153. * @param qmul array holding the dc/ac dequant factor at position 0/1
  1154. * @param scan scan pattern (VP7 only)
  1155. *
  1156. * @return 0 if no coeffs were decoded
  1157. * otherwise, the index of the last coeff decoded plus one
  1158. */
  1159. static av_always_inline
  1160. int decode_block_coeffs(VP56RangeCoder *c, int16_t block[16],
  1161. uint8_t probs[16][3][NUM_DCT_TOKENS - 1],
  1162. int i, int zero_nhood, int16_t qmul[2],
  1163. const uint8_t scan[16], int vp7)
  1164. {
  1165. uint8_t *token_prob = probs[i][zero_nhood];
  1166. if (!vp56_rac_get_prob_branchy(c, token_prob[0])) // DCT_EOB
  1167. return 0;
  1168. return vp7 ? vp7_decode_block_coeffs_internal(c, block, probs, i,
  1169. token_prob, qmul, scan)
  1170. : vp8_decode_block_coeffs_internal(c, block, probs, i,
  1171. token_prob, qmul);
  1172. }
  1173. static av_always_inline
  1174. void decode_mb_coeffs(VP8Context *s, VP8ThreadData *td, VP56RangeCoder *c,
  1175. VP8Macroblock *mb, uint8_t t_nnz[9], uint8_t l_nnz[9],
  1176. int is_vp7)
  1177. {
  1178. int i, x, y, luma_start = 0, luma_ctx = 3;
  1179. int nnz_pred, nnz, nnz_total = 0;
  1180. int segment = mb->segment;
  1181. int block_dc = 0;
  1182. if (mb->mode != MODE_I4x4 && (is_vp7 || mb->mode != VP8_MVMODE_SPLIT)) {
  1183. nnz_pred = t_nnz[8] + l_nnz[8];
  1184. // decode DC values and do hadamard
  1185. nnz = decode_block_coeffs(c, td->block_dc, s->prob->token[1], 0,
  1186. nnz_pred, s->qmat[segment].luma_dc_qmul,
  1187. zigzag_scan, is_vp7);
  1188. l_nnz[8] = t_nnz[8] = !!nnz;
  1189. if (is_vp7 && mb->mode > MODE_I4x4) {
  1190. nnz |= inter_predict_dc(td->block_dc,
  1191. s->inter_dc_pred[mb->ref_frame - 1]);
  1192. }
  1193. if (nnz) {
  1194. nnz_total += nnz;
  1195. block_dc = 1;
  1196. if (nnz == 1)
  1197. s->vp8dsp.vp8_luma_dc_wht_dc(td->block, td->block_dc);
  1198. else
  1199. s->vp8dsp.vp8_luma_dc_wht(td->block, td->block_dc);
  1200. }
  1201. luma_start = 1;
  1202. luma_ctx = 0;
  1203. }
  1204. // luma blocks
  1205. for (y = 0; y < 4; y++)
  1206. for (x = 0; x < 4; x++) {
  1207. nnz_pred = l_nnz[y] + t_nnz[x];
  1208. nnz = decode_block_coeffs(c, td->block[y][x],
  1209. s->prob->token[luma_ctx],
  1210. luma_start, nnz_pred,
  1211. s->qmat[segment].luma_qmul,
  1212. s->prob[0].scan, is_vp7);
  1213. /* nnz+block_dc may be one more than the actual last index,
  1214. * but we don't care */
  1215. td->non_zero_count_cache[y][x] = nnz + block_dc;
  1216. t_nnz[x] = l_nnz[y] = !!nnz;
  1217. nnz_total += nnz;
  1218. }
  1219. // chroma blocks
  1220. // TODO: what to do about dimensions? 2nd dim for luma is x,
  1221. // but for chroma it's (y<<1)|x
  1222. for (i = 4; i < 6; i++)
  1223. for (y = 0; y < 2; y++)
  1224. for (x = 0; x < 2; x++) {
  1225. nnz_pred = l_nnz[i + 2 * y] + t_nnz[i + 2 * x];
  1226. nnz = decode_block_coeffs(c, td->block[i][(y << 1) + x],
  1227. s->prob->token[2], 0, nnz_pred,
  1228. s->qmat[segment].chroma_qmul,
  1229. s->prob[0].scan, is_vp7);
  1230. td->non_zero_count_cache[i][(y << 1) + x] = nnz;
  1231. t_nnz[i + 2 * x] = l_nnz[i + 2 * y] = !!nnz;
  1232. nnz_total += nnz;
  1233. }
  1234. // if there were no coded coeffs despite the macroblock not being marked skip,
  1235. // we MUST not do the inner loop filter and should not do IDCT
  1236. // Since skip isn't used for bitstream prediction, just manually set it.
  1237. if (!nnz_total)
  1238. mb->skip = 1;
  1239. }
  1240. static av_always_inline
  1241. void backup_mb_border(uint8_t *top_border, uint8_t *src_y,
  1242. uint8_t *src_cb, uint8_t *src_cr,
  1243. int linesize, int uvlinesize, int simple)
  1244. {
  1245. AV_COPY128(top_border, src_y + 15 * linesize);
  1246. if (!simple) {
  1247. AV_COPY64(top_border + 16, src_cb + 7 * uvlinesize);
  1248. AV_COPY64(top_border + 24, src_cr + 7 * uvlinesize);
  1249. }
  1250. }
  1251. static av_always_inline
  1252. void xchg_mb_border(uint8_t *top_border, uint8_t *src_y, uint8_t *src_cb,
  1253. uint8_t *src_cr, int linesize, int uvlinesize, int mb_x,
  1254. int mb_y, int mb_width, int simple, int xchg)
  1255. {
  1256. uint8_t *top_border_m1 = top_border - 32; // for TL prediction
  1257. src_y -= linesize;
  1258. src_cb -= uvlinesize;
  1259. src_cr -= uvlinesize;
  1260. #define XCHG(a, b, xchg) \
  1261. do { \
  1262. if (xchg) \
  1263. AV_SWAP64(b, a); \
  1264. else \
  1265. AV_COPY64(b, a); \
  1266. } while (0)
  1267. XCHG(top_border_m1 + 8, src_y - 8, xchg);
  1268. XCHG(top_border, src_y, xchg);
  1269. XCHG(top_border + 8, src_y + 8, 1);
  1270. if (mb_x < mb_width - 1)
  1271. XCHG(top_border + 32, src_y + 16, 1);
  1272. // only copy chroma for normal loop filter
  1273. // or to initialize the top row to 127
  1274. if (!simple || !mb_y) {
  1275. XCHG(top_border_m1 + 16, src_cb - 8, xchg);
  1276. XCHG(top_border_m1 + 24, src_cr - 8, xchg);
  1277. XCHG(top_border + 16, src_cb, 1);
  1278. XCHG(top_border + 24, src_cr, 1);
  1279. }
  1280. }
  1281. static av_always_inline
  1282. int check_dc_pred8x8_mode(int mode, int mb_x, int mb_y)
  1283. {
  1284. if (!mb_x)
  1285. return mb_y ? TOP_DC_PRED8x8 : DC_128_PRED8x8;
  1286. else
  1287. return mb_y ? mode : LEFT_DC_PRED8x8;
  1288. }
  1289. static av_always_inline
  1290. int check_tm_pred8x8_mode(int mode, int mb_x, int mb_y, int vp7)
  1291. {
  1292. if (!mb_x)
  1293. return mb_y ? VERT_PRED8x8 : (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8);
  1294. else
  1295. return mb_y ? mode : HOR_PRED8x8;
  1296. }
  1297. static av_always_inline
  1298. int check_intra_pred8x8_mode_emuedge(int mode, int mb_x, int mb_y, int vp7)
  1299. {
  1300. switch (mode) {
  1301. case DC_PRED8x8:
  1302. return check_dc_pred8x8_mode(mode, mb_x, mb_y);
  1303. case VERT_PRED8x8:
  1304. return !mb_y ? (vp7 ? DC_128_PRED8x8 : DC_127_PRED8x8) : mode;
  1305. case HOR_PRED8x8:
  1306. return !mb_x ? (vp7 ? DC_128_PRED8x8 : DC_129_PRED8x8) : mode;
  1307. case PLANE_PRED8x8: /* TM */
  1308. return check_tm_pred8x8_mode(mode, mb_x, mb_y, vp7);
  1309. }
  1310. return mode;
  1311. }
  1312. static av_always_inline
  1313. int check_tm_pred4x4_mode(int mode, int mb_x, int mb_y, int vp7)
  1314. {
  1315. if (!mb_x) {
  1316. return mb_y ? VERT_VP8_PRED : (vp7 ? DC_128_PRED : DC_129_PRED);
  1317. } else {
  1318. return mb_y ? mode : HOR_VP8_PRED;
  1319. }
  1320. }
  1321. static av_always_inline
  1322. int check_intra_pred4x4_mode_emuedge(int mode, int mb_x, int mb_y,
  1323. int *copy_buf, int vp7)
  1324. {
  1325. switch (mode) {
  1326. case VERT_PRED:
  1327. if (!mb_x && mb_y) {
  1328. *copy_buf = 1;
  1329. return mode;
  1330. }
  1331. /* fall-through */
  1332. case DIAG_DOWN_LEFT_PRED:
  1333. case VERT_LEFT_PRED:
  1334. return !mb_y ? (vp7 ? DC_128_PRED : DC_127_PRED) : mode;
  1335. case HOR_PRED:
  1336. if (!mb_y) {
  1337. *copy_buf = 1;
  1338. return mode;
  1339. }
  1340. /* fall-through */
  1341. case HOR_UP_PRED:
  1342. return !mb_x ? (vp7 ? DC_128_PRED : DC_129_PRED) : mode;
  1343. case TM_VP8_PRED:
  1344. return check_tm_pred4x4_mode(mode, mb_x, mb_y, vp7);
  1345. case DC_PRED: /* 4x4 DC doesn't use the same "H.264-style" exceptions
  1346. * as 16x16/8x8 DC */
  1347. case DIAG_DOWN_RIGHT_PRED:
  1348. case VERT_RIGHT_PRED:
  1349. case HOR_DOWN_PRED:
  1350. if (!mb_y || !mb_x)
  1351. *copy_buf = 1;
  1352. return mode;
  1353. }
  1354. return mode;
  1355. }
  1356. static av_always_inline
  1357. void intra_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
  1358. VP8Macroblock *mb, int mb_x, int mb_y, int is_vp7)
  1359. {
  1360. int x, y, mode, nnz;
  1361. uint32_t tr;
  1362. /* for the first row, we need to run xchg_mb_border to init the top edge
  1363. * to 127 otherwise, skip it if we aren't going to deblock */
  1364. if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
  1365. xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
  1366. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  1367. s->filter.simple, 1);
  1368. if (mb->mode < MODE_I4x4) {
  1369. mode = check_intra_pred8x8_mode_emuedge(mb->mode, mb_x, mb_y, is_vp7);
  1370. s->hpc.pred16x16[mode](dst[0], s->linesize);
  1371. } else {
  1372. uint8_t *ptr = dst[0];
  1373. uint8_t *intra4x4 = mb->intra4x4_pred_mode_mb;
  1374. const uint8_t lo = is_vp7 ? 128 : 127;
  1375. const uint8_t hi = is_vp7 ? 128 : 129;
  1376. uint8_t tr_top[4] = { lo, lo, lo, lo };
  1377. // all blocks on the right edge of the macroblock use bottom edge
  1378. // the top macroblock for their topright edge
  1379. uint8_t *tr_right = ptr - s->linesize + 16;
  1380. // if we're on the right edge of the frame, said edge is extended
  1381. // from the top macroblock
  1382. if (mb_y && mb_x == s->mb_width - 1) {
  1383. tr = tr_right[-1] * 0x01010101u;
  1384. tr_right = (uint8_t *) &tr;
  1385. }
  1386. if (mb->skip)
  1387. AV_ZERO128(td->non_zero_count_cache);
  1388. for (y = 0; y < 4; y++) {
  1389. uint8_t *topright = ptr + 4 - s->linesize;
  1390. for (x = 0; x < 4; x++) {
  1391. int copy = 0, linesize = s->linesize;
  1392. uint8_t *dst = ptr + 4 * x;
  1393. DECLARE_ALIGNED(4, uint8_t, copy_dst)[5 * 8];
  1394. if ((y == 0 || x == 3) && mb_y == 0) {
  1395. topright = tr_top;
  1396. } else if (x == 3)
  1397. topright = tr_right;
  1398. mode = check_intra_pred4x4_mode_emuedge(intra4x4[x], mb_x + x,
  1399. mb_y + y, &copy, is_vp7);
  1400. if (copy) {
  1401. dst = copy_dst + 12;
  1402. linesize = 8;
  1403. if (!(mb_y + y)) {
  1404. copy_dst[3] = lo;
  1405. AV_WN32A(copy_dst + 4, lo * 0x01010101U);
  1406. } else {
  1407. AV_COPY32(copy_dst + 4, ptr + 4 * x - s->linesize);
  1408. if (!(mb_x + x)) {
  1409. copy_dst[3] = hi;
  1410. } else {
  1411. copy_dst[3] = ptr[4 * x - s->linesize - 1];
  1412. }
  1413. }
  1414. if (!(mb_x + x)) {
  1415. copy_dst[11] =
  1416. copy_dst[19] =
  1417. copy_dst[27] =
  1418. copy_dst[35] = hi;
  1419. } else {
  1420. copy_dst[11] = ptr[4 * x - 1];
  1421. copy_dst[19] = ptr[4 * x + s->linesize - 1];
  1422. copy_dst[27] = ptr[4 * x + s->linesize * 2 - 1];
  1423. copy_dst[35] = ptr[4 * x + s->linesize * 3 - 1];
  1424. }
  1425. }
  1426. s->hpc.pred4x4[mode](dst, topright, linesize);
  1427. if (copy) {
  1428. AV_COPY32(ptr + 4 * x, copy_dst + 12);
  1429. AV_COPY32(ptr + 4 * x + s->linesize, copy_dst + 20);
  1430. AV_COPY32(ptr + 4 * x + s->linesize * 2, copy_dst + 28);
  1431. AV_COPY32(ptr + 4 * x + s->linesize * 3, copy_dst + 36);
  1432. }
  1433. nnz = td->non_zero_count_cache[y][x];
  1434. if (nnz) {
  1435. if (nnz == 1)
  1436. s->vp8dsp.vp8_idct_dc_add(ptr + 4 * x,
  1437. td->block[y][x], s->linesize);
  1438. else
  1439. s->vp8dsp.vp8_idct_add(ptr + 4 * x,
  1440. td->block[y][x], s->linesize);
  1441. }
  1442. topright += 4;
  1443. }
  1444. ptr += 4 * s->linesize;
  1445. intra4x4 += 4;
  1446. }
  1447. }
  1448. mode = check_intra_pred8x8_mode_emuedge(mb->chroma_pred_mode,
  1449. mb_x, mb_y, is_vp7);
  1450. s->hpc.pred8x8[mode](dst[1], s->uvlinesize);
  1451. s->hpc.pred8x8[mode](dst[2], s->uvlinesize);
  1452. if (mb_y && (s->deblock_filter || !mb_y) && td->thread_nr == 0)
  1453. xchg_mb_border(s->top_border[mb_x + 1], dst[0], dst[1], dst[2],
  1454. s->linesize, s->uvlinesize, mb_x, mb_y, s->mb_width,
  1455. s->filter.simple, 0);
  1456. }
  1457. static const uint8_t subpel_idx[3][8] = {
  1458. { 0, 1, 2, 1, 2, 1, 2, 1 }, // nr. of left extra pixels,
  1459. // also function pointer index
  1460. { 0, 3, 5, 3, 5, 3, 5, 3 }, // nr. of extra pixels required
  1461. { 0, 2, 3, 2, 3, 2, 3, 2 }, // nr. of right extra pixels
  1462. };
  1463. /**
  1464. * luma MC function
  1465. *
  1466. * @param s VP8 decoding context
  1467. * @param dst target buffer for block data at block position
  1468. * @param ref reference picture buffer at origin (0, 0)
  1469. * @param mv motion vector (relative to block position) to get pixel data from
  1470. * @param x_off horizontal position of block from origin (0, 0)
  1471. * @param y_off vertical position of block from origin (0, 0)
  1472. * @param block_w width of block (16, 8 or 4)
  1473. * @param block_h height of block (always same as block_w)
  1474. * @param width width of src/dst plane data
  1475. * @param height height of src/dst plane data
  1476. * @param linesize size of a single line of plane data, including padding
  1477. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  1478. */
  1479. static av_always_inline
  1480. void vp8_mc_luma(VP8Context *s, VP8ThreadData *td, uint8_t *dst,
  1481. ThreadFrame *ref, const VP56mv *mv,
  1482. int x_off, int y_off, int block_w, int block_h,
  1483. int width, int height, ptrdiff_t linesize,
  1484. vp8_mc_func mc_func[3][3])
  1485. {
  1486. uint8_t *src = ref->f->data[0];
  1487. if (AV_RN32A(mv)) {
  1488. int src_linesize = linesize;
  1489. int mx = (mv->x << 1) & 7, mx_idx = subpel_idx[0][mx];
  1490. int my = (mv->y << 1) & 7, my_idx = subpel_idx[0][my];
  1491. x_off += mv->x >> 2;
  1492. y_off += mv->y >> 2;
  1493. // edge emulation
  1494. ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 4, 0);
  1495. src += y_off * linesize + x_off;
  1496. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  1497. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  1498. s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
  1499. src - my_idx * linesize - mx_idx,
  1500. EDGE_EMU_LINESIZE, linesize,
  1501. block_w + subpel_idx[1][mx],
  1502. block_h + subpel_idx[1][my],
  1503. x_off - mx_idx, y_off - my_idx,
  1504. width, height);
  1505. src = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
  1506. src_linesize = EDGE_EMU_LINESIZE;
  1507. }
  1508. mc_func[my_idx][mx_idx](dst, linesize, src, src_linesize, block_h, mx, my);
  1509. } else {
  1510. ff_thread_await_progress(ref, (3 + y_off + block_h) >> 4, 0);
  1511. mc_func[0][0](dst, linesize, src + y_off * linesize + x_off,
  1512. linesize, block_h, 0, 0);
  1513. }
  1514. }
  1515. /**
  1516. * chroma MC function
  1517. *
  1518. * @param s VP8 decoding context
  1519. * @param dst1 target buffer for block data at block position (U plane)
  1520. * @param dst2 target buffer for block data at block position (V plane)
  1521. * @param ref reference picture buffer at origin (0, 0)
  1522. * @param mv motion vector (relative to block position) to get pixel data from
  1523. * @param x_off horizontal position of block from origin (0, 0)
  1524. * @param y_off vertical position of block from origin (0, 0)
  1525. * @param block_w width of block (16, 8 or 4)
  1526. * @param block_h height of block (always same as block_w)
  1527. * @param width width of src/dst plane data
  1528. * @param height height of src/dst plane data
  1529. * @param linesize size of a single line of plane data, including padding
  1530. * @param mc_func motion compensation function pointers (bilinear or sixtap MC)
  1531. */
  1532. static av_always_inline
  1533. void vp8_mc_chroma(VP8Context *s, VP8ThreadData *td, uint8_t *dst1,
  1534. uint8_t *dst2, ThreadFrame *ref, const VP56mv *mv,
  1535. int x_off, int y_off, int block_w, int block_h,
  1536. int width, int height, ptrdiff_t linesize,
  1537. vp8_mc_func mc_func[3][3])
  1538. {
  1539. uint8_t *src1 = ref->f->data[1], *src2 = ref->f->data[2];
  1540. if (AV_RN32A(mv)) {
  1541. int mx = mv->x & 7, mx_idx = subpel_idx[0][mx];
  1542. int my = mv->y & 7, my_idx = subpel_idx[0][my];
  1543. x_off += mv->x >> 3;
  1544. y_off += mv->y >> 3;
  1545. // edge emulation
  1546. src1 += y_off * linesize + x_off;
  1547. src2 += y_off * linesize + x_off;
  1548. ff_thread_await_progress(ref, (3 + y_off + block_h + subpel_idx[2][my]) >> 3, 0);
  1549. if (x_off < mx_idx || x_off >= width - block_w - subpel_idx[2][mx] ||
  1550. y_off < my_idx || y_off >= height - block_h - subpel_idx[2][my]) {
  1551. s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
  1552. src1 - my_idx * linesize - mx_idx,
  1553. EDGE_EMU_LINESIZE, linesize,
  1554. block_w + subpel_idx[1][mx],
  1555. block_h + subpel_idx[1][my],
  1556. x_off - mx_idx, y_off - my_idx, width, height);
  1557. src1 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
  1558. mc_func[my_idx][mx_idx](dst1, linesize, src1, EDGE_EMU_LINESIZE, block_h, mx, my);
  1559. s->vdsp.emulated_edge_mc(td->edge_emu_buffer,
  1560. src2 - my_idx * linesize - mx_idx,
  1561. EDGE_EMU_LINESIZE, linesize,
  1562. block_w + subpel_idx[1][mx],
  1563. block_h + subpel_idx[1][my],
  1564. x_off - mx_idx, y_off - my_idx, width, height);
  1565. src2 = td->edge_emu_buffer + mx_idx + EDGE_EMU_LINESIZE * my_idx;
  1566. mc_func[my_idx][mx_idx](dst2, linesize, src2, EDGE_EMU_LINESIZE, block_h, mx, my);
  1567. } else {
  1568. mc_func[my_idx][mx_idx](dst1, linesize, src1, linesize, block_h, mx, my);
  1569. mc_func[my_idx][mx_idx](dst2, linesize, src2, linesize, block_h, mx, my);
  1570. }
  1571. } else {
  1572. ff_thread_await_progress(ref, (3 + y_off + block_h) >> 3, 0);
  1573. mc_func[0][0](dst1, linesize, src1 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1574. mc_func[0][0](dst2, linesize, src2 + y_off * linesize + x_off, linesize, block_h, 0, 0);
  1575. }
  1576. }
  1577. static av_always_inline
  1578. void vp8_mc_part(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
  1579. ThreadFrame *ref_frame, int x_off, int y_off,
  1580. int bx_off, int by_off, int block_w, int block_h,
  1581. int width, int height, VP56mv *mv)
  1582. {
  1583. VP56mv uvmv = *mv;
  1584. /* Y */
  1585. vp8_mc_luma(s, td, dst[0] + by_off * s->linesize + bx_off,
  1586. ref_frame, mv, x_off + bx_off, y_off + by_off,
  1587. block_w, block_h, width, height, s->linesize,
  1588. s->put_pixels_tab[block_w == 8]);
  1589. /* U/V */
  1590. if (s->profile == 3) {
  1591. /* this block only applies VP8; it is safe to check
  1592. * only the profile, as VP7 profile <= 1 */
  1593. uvmv.x &= ~7;
  1594. uvmv.y &= ~7;
  1595. }
  1596. x_off >>= 1;
  1597. y_off >>= 1;
  1598. bx_off >>= 1;
  1599. by_off >>= 1;
  1600. width >>= 1;
  1601. height >>= 1;
  1602. block_w >>= 1;
  1603. block_h >>= 1;
  1604. vp8_mc_chroma(s, td, dst[1] + by_off * s->uvlinesize + bx_off,
  1605. dst[2] + by_off * s->uvlinesize + bx_off, ref_frame,
  1606. &uvmv, x_off + bx_off, y_off + by_off,
  1607. block_w, block_h, width, height, s->uvlinesize,
  1608. s->put_pixels_tab[1 + (block_w == 4)]);
  1609. }
  1610. /* Fetch pixels for estimated mv 4 macroblocks ahead.
  1611. * Optimized for 64-byte cache lines. Inspired by ffh264 prefetch_motion. */
  1612. static av_always_inline
  1613. void prefetch_motion(VP8Context *s, VP8Macroblock *mb, int mb_x, int mb_y,
  1614. int mb_xy, int ref)
  1615. {
  1616. /* Don't prefetch refs that haven't been used very often this frame. */
  1617. if (s->ref_count[ref - 1] > (mb_xy >> 5)) {
  1618. int x_off = mb_x << 4, y_off = mb_y << 4;
  1619. int mx = (mb->mv.x >> 2) + x_off + 8;
  1620. int my = (mb->mv.y >> 2) + y_off;
  1621. uint8_t **src = s->framep[ref]->tf.f->data;
  1622. int off = mx + (my + (mb_x & 3) * 4) * s->linesize + 64;
  1623. /* For threading, a ff_thread_await_progress here might be useful, but
  1624. * it actually slows down the decoder. Since a bad prefetch doesn't
  1625. * generate bad decoder output, we don't run it here. */
  1626. s->vdsp.prefetch(src[0] + off, s->linesize, 4);
  1627. off = (mx >> 1) + ((my >> 1) + (mb_x & 7)) * s->uvlinesize + 64;
  1628. s->vdsp.prefetch(src[1] + off, src[2] - src[1], 2);
  1629. }
  1630. }
  1631. /**
  1632. * Apply motion vectors to prediction buffer, chapter 18.
  1633. */
  1634. static av_always_inline
  1635. void inter_predict(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3],
  1636. VP8Macroblock *mb, int mb_x, int mb_y)
  1637. {
  1638. int x_off = mb_x << 4, y_off = mb_y << 4;
  1639. int width = 16 * s->mb_width, height = 16 * s->mb_height;
  1640. ThreadFrame *ref = &s->framep[mb->ref_frame]->tf;
  1641. VP56mv *bmv = mb->bmv;
  1642. switch (mb->partitioning) {
  1643. case VP8_SPLITMVMODE_NONE:
  1644. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1645. 0, 0, 16, 16, width, height, &mb->mv);
  1646. break;
  1647. case VP8_SPLITMVMODE_4x4: {
  1648. int x, y;
  1649. VP56mv uvmv;
  1650. /* Y */
  1651. for (y = 0; y < 4; y++) {
  1652. for (x = 0; x < 4; x++) {
  1653. vp8_mc_luma(s, td, dst[0] + 4 * y * s->linesize + x * 4,
  1654. ref, &bmv[4 * y + x],
  1655. 4 * x + x_off, 4 * y + y_off, 4, 4,
  1656. width, height, s->linesize,
  1657. s->put_pixels_tab[2]);
  1658. }
  1659. }
  1660. /* U/V */
  1661. x_off >>= 1;
  1662. y_off >>= 1;
  1663. width >>= 1;
  1664. height >>= 1;
  1665. for (y = 0; y < 2; y++) {
  1666. for (x = 0; x < 2; x++) {
  1667. uvmv.x = mb->bmv[2 * y * 4 + 2 * x ].x +
  1668. mb->bmv[2 * y * 4 + 2 * x + 1].x +
  1669. mb->bmv[(2 * y + 1) * 4 + 2 * x ].x +
  1670. mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].x;
  1671. uvmv.y = mb->bmv[2 * y * 4 + 2 * x ].y +
  1672. mb->bmv[2 * y * 4 + 2 * x + 1].y +
  1673. mb->bmv[(2 * y + 1) * 4 + 2 * x ].y +
  1674. mb->bmv[(2 * y + 1) * 4 + 2 * x + 1].y;
  1675. uvmv.x = (uvmv.x + 2 + (uvmv.x >> (INT_BIT - 1))) >> 2;
  1676. uvmv.y = (uvmv.y + 2 + (uvmv.y >> (INT_BIT - 1))) >> 2;
  1677. if (s->profile == 3) {
  1678. uvmv.x &= ~7;
  1679. uvmv.y &= ~7;
  1680. }
  1681. vp8_mc_chroma(s, td, dst[1] + 4 * y * s->uvlinesize + x * 4,
  1682. dst[2] + 4 * y * s->uvlinesize + x * 4, ref,
  1683. &uvmv, 4 * x + x_off, 4 * y + y_off, 4, 4,
  1684. width, height, s->uvlinesize,
  1685. s->put_pixels_tab[2]);
  1686. }
  1687. }
  1688. break;
  1689. }
  1690. case VP8_SPLITMVMODE_16x8:
  1691. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1692. 0, 0, 16, 8, width, height, &bmv[0]);
  1693. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1694. 0, 8, 16, 8, width, height, &bmv[1]);
  1695. break;
  1696. case VP8_SPLITMVMODE_8x16:
  1697. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1698. 0, 0, 8, 16, width, height, &bmv[0]);
  1699. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1700. 8, 0, 8, 16, width, height, &bmv[1]);
  1701. break;
  1702. case VP8_SPLITMVMODE_8x8:
  1703. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1704. 0, 0, 8, 8, width, height, &bmv[0]);
  1705. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1706. 8, 0, 8, 8, width, height, &bmv[1]);
  1707. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1708. 0, 8, 8, 8, width, height, &bmv[2]);
  1709. vp8_mc_part(s, td, dst, ref, x_off, y_off,
  1710. 8, 8, 8, 8, width, height, &bmv[3]);
  1711. break;
  1712. }
  1713. }
  1714. static av_always_inline
  1715. void idct_mb(VP8Context *s, VP8ThreadData *td, uint8_t *dst[3], VP8Macroblock *mb)
  1716. {
  1717. int x, y, ch;
  1718. if (mb->mode != MODE_I4x4) {
  1719. uint8_t *y_dst = dst[0];
  1720. for (y = 0; y < 4; y++) {
  1721. uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[y]);
  1722. if (nnz4) {
  1723. if (nnz4 & ~0x01010101) {
  1724. for (x = 0; x < 4; x++) {
  1725. if ((uint8_t) nnz4 == 1)
  1726. s->vp8dsp.vp8_idct_dc_add(y_dst + 4 * x,
  1727. td->block[y][x],
  1728. s->linesize);
  1729. else if ((uint8_t) nnz4 > 1)
  1730. s->vp8dsp.vp8_idct_add(y_dst + 4 * x,
  1731. td->block[y][x],
  1732. s->linesize);
  1733. nnz4 >>= 8;
  1734. if (!nnz4)
  1735. break;
  1736. }
  1737. } else {
  1738. s->vp8dsp.vp8_idct_dc_add4y(y_dst, td->block[y], s->linesize);
  1739. }
  1740. }
  1741. y_dst += 4 * s->linesize;
  1742. }
  1743. }
  1744. for (ch = 0; ch < 2; ch++) {
  1745. uint32_t nnz4 = AV_RL32(td->non_zero_count_cache[4 + ch]);
  1746. if (nnz4) {
  1747. uint8_t *ch_dst = dst[1 + ch];
  1748. if (nnz4 & ~0x01010101) {
  1749. for (y = 0; y < 2; y++) {
  1750. for (x = 0; x < 2; x++) {
  1751. if ((uint8_t) nnz4 == 1)
  1752. s->vp8dsp.vp8_idct_dc_add(ch_dst + 4 * x,
  1753. td->block[4 + ch][(y << 1) + x],
  1754. s->uvlinesize);
  1755. else if ((uint8_t) nnz4 > 1)
  1756. s->vp8dsp.vp8_idct_add(ch_dst + 4 * x,
  1757. td->block[4 + ch][(y << 1) + x],
  1758. s->uvlinesize);
  1759. nnz4 >>= 8;
  1760. if (!nnz4)
  1761. goto chroma_idct_end;
  1762. }
  1763. ch_dst += 4 * s->uvlinesize;
  1764. }
  1765. } else {
  1766. s->vp8dsp.vp8_idct_dc_add4uv(ch_dst, td->block[4 + ch], s->uvlinesize);
  1767. }
  1768. }
  1769. chroma_idct_end:
  1770. ;
  1771. }
  1772. }
  1773. static av_always_inline
  1774. void filter_level_for_mb(VP8Context *s, VP8Macroblock *mb,
  1775. VP8FilterStrength *f, int is_vp7)
  1776. {
  1777. int interior_limit, filter_level;
  1778. if (s->segmentation.enabled) {
  1779. filter_level = s->segmentation.filter_level[mb->segment];
  1780. if (!s->segmentation.absolute_vals)
  1781. filter_level += s->filter.level;
  1782. } else
  1783. filter_level = s->filter.level;
  1784. if (s->lf_delta.enabled) {
  1785. filter_level += s->lf_delta.ref[mb->ref_frame];
  1786. filter_level += s->lf_delta.mode[mb->mode];
  1787. }
  1788. filter_level = av_clip_uintp2(filter_level, 6);
  1789. interior_limit = filter_level;
  1790. if (s->filter.sharpness) {
  1791. interior_limit >>= (s->filter.sharpness + 3) >> 2;
  1792. interior_limit = FFMIN(interior_limit, 9 - s->filter.sharpness);
  1793. }
  1794. interior_limit = FFMAX(interior_limit, 1);
  1795. f->filter_level = filter_level;
  1796. f->inner_limit = interior_limit;
  1797. f->inner_filter = is_vp7 || !mb->skip || mb->mode == MODE_I4x4 ||
  1798. mb->mode == VP8_MVMODE_SPLIT;
  1799. }
  1800. static av_always_inline
  1801. void filter_mb(VP8Context *s, uint8_t *dst[3], VP8FilterStrength *f,
  1802. int mb_x, int mb_y, int is_vp7)
  1803. {
  1804. int mbedge_lim, bedge_lim_y, bedge_lim_uv, hev_thresh;
  1805. int filter_level = f->filter_level;
  1806. int inner_limit = f->inner_limit;
  1807. int inner_filter = f->inner_filter;
  1808. int linesize = s->linesize;
  1809. int uvlinesize = s->uvlinesize;
  1810. static const uint8_t hev_thresh_lut[2][64] = {
  1811. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1812. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1813. 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3,
  1814. 3, 3, 3, 3 },
  1815. { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
  1816. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  1817. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  1818. 2, 2, 2, 2 }
  1819. };
  1820. if (!filter_level)
  1821. return;
  1822. if (is_vp7) {
  1823. bedge_lim_y = filter_level;
  1824. bedge_lim_uv = filter_level * 2;
  1825. mbedge_lim = filter_level + 2;
  1826. } else {
  1827. bedge_lim_y =
  1828. bedge_lim_uv = filter_level * 2 + inner_limit;
  1829. mbedge_lim = bedge_lim_y + 4;
  1830. }
  1831. hev_thresh = hev_thresh_lut[s->keyframe][filter_level];
  1832. if (mb_x) {
  1833. s->vp8dsp.vp8_h_loop_filter16y(dst[0], linesize,
  1834. mbedge_lim, inner_limit, hev_thresh);
  1835. s->vp8dsp.vp8_h_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1836. mbedge_lim, inner_limit, hev_thresh);
  1837. }
  1838. #define H_LOOP_FILTER_16Y_INNER(cond) \
  1839. if (cond && inner_filter) { \
  1840. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 4, linesize, \
  1841. bedge_lim_y, inner_limit, \
  1842. hev_thresh); \
  1843. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 8, linesize, \
  1844. bedge_lim_y, inner_limit, \
  1845. hev_thresh); \
  1846. s->vp8dsp.vp8_h_loop_filter16y_inner(dst[0] + 12, linesize, \
  1847. bedge_lim_y, inner_limit, \
  1848. hev_thresh); \
  1849. s->vp8dsp.vp8_h_loop_filter8uv_inner(dst[1] + 4, dst[2] + 4, \
  1850. uvlinesize, bedge_lim_uv, \
  1851. inner_limit, hev_thresh); \
  1852. }
  1853. H_LOOP_FILTER_16Y_INNER(!is_vp7)
  1854. if (mb_y) {
  1855. s->vp8dsp.vp8_v_loop_filter16y(dst[0], linesize,
  1856. mbedge_lim, inner_limit, hev_thresh);
  1857. s->vp8dsp.vp8_v_loop_filter8uv(dst[1], dst[2], uvlinesize,
  1858. mbedge_lim, inner_limit, hev_thresh);
  1859. }
  1860. if (inner_filter) {
  1861. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 4 * linesize,
  1862. linesize, bedge_lim_y,
  1863. inner_limit, hev_thresh);
  1864. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 8 * linesize,
  1865. linesize, bedge_lim_y,
  1866. inner_limit, hev_thresh);
  1867. s->vp8dsp.vp8_v_loop_filter16y_inner(dst[0] + 12 * linesize,
  1868. linesize, bedge_lim_y,
  1869. inner_limit, hev_thresh);
  1870. s->vp8dsp.vp8_v_loop_filter8uv_inner(dst[1] + 4 * uvlinesize,
  1871. dst[2] + 4 * uvlinesize,
  1872. uvlinesize, bedge_lim_uv,
  1873. inner_limit, hev_thresh);
  1874. }
  1875. H_LOOP_FILTER_16Y_INNER(is_vp7)
  1876. }
  1877. static av_always_inline
  1878. void filter_mb_simple(VP8Context *s, uint8_t *dst, VP8FilterStrength *f,
  1879. int mb_x, int mb_y)
  1880. {
  1881. int mbedge_lim, bedge_lim;
  1882. int filter_level = f->filter_level;
  1883. int inner_limit = f->inner_limit;
  1884. int inner_filter = f->inner_filter;
  1885. int linesize = s->linesize;
  1886. if (!filter_level)
  1887. return;
  1888. bedge_lim = 2 * filter_level + inner_limit;
  1889. mbedge_lim = bedge_lim + 4;
  1890. if (mb_x)
  1891. s->vp8dsp.vp8_h_loop_filter_simple(dst, linesize, mbedge_lim);
  1892. if (inner_filter) {
  1893. s->vp8dsp.vp8_h_loop_filter_simple(dst + 4, linesize, bedge_lim);
  1894. s->vp8dsp.vp8_h_loop_filter_simple(dst + 8, linesize, bedge_lim);
  1895. s->vp8dsp.vp8_h_loop_filter_simple(dst + 12, linesize, bedge_lim);
  1896. }
  1897. if (mb_y)
  1898. s->vp8dsp.vp8_v_loop_filter_simple(dst, linesize, mbedge_lim);
  1899. if (inner_filter) {
  1900. s->vp8dsp.vp8_v_loop_filter_simple(dst + 4 * linesize, linesize, bedge_lim);
  1901. s->vp8dsp.vp8_v_loop_filter_simple(dst + 8 * linesize, linesize, bedge_lim);
  1902. s->vp8dsp.vp8_v_loop_filter_simple(dst + 12 * linesize, linesize, bedge_lim);
  1903. }
  1904. }
  1905. #define MARGIN (16 << 2)
  1906. static av_always_inline
  1907. void vp78_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *curframe,
  1908. VP8Frame *prev_frame, int is_vp7)
  1909. {
  1910. VP8Context *s = avctx->priv_data;
  1911. int mb_x, mb_y;
  1912. s->mv_min.y = -MARGIN;
  1913. s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
  1914. for (mb_y = 0; mb_y < s->mb_height; mb_y++) {
  1915. VP8Macroblock *mb = s->macroblocks_base +
  1916. ((s->mb_width + 1) * (mb_y + 1) + 1);
  1917. int mb_xy = mb_y * s->mb_width;
  1918. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
  1919. s->mv_min.x = -MARGIN;
  1920. s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
  1921. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  1922. if (mb_y == 0)
  1923. AV_WN32A((mb - s->mb_width - 1)->intra4x4_pred_mode_top,
  1924. DC_PRED * 0x01010101);
  1925. decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
  1926. prev_frame && prev_frame->seg_map ?
  1927. prev_frame->seg_map->data + mb_xy : NULL, 1, is_vp7);
  1928. s->mv_min.x -= 64;
  1929. s->mv_max.x -= 64;
  1930. }
  1931. s->mv_min.y -= 64;
  1932. s->mv_max.y -= 64;
  1933. }
  1934. }
  1935. static void vp7_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
  1936. VP8Frame *prev_frame)
  1937. {
  1938. vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP7);
  1939. }
  1940. static void vp8_decode_mv_mb_modes(AVCodecContext *avctx, VP8Frame *cur_frame,
  1941. VP8Frame *prev_frame)
  1942. {
  1943. vp78_decode_mv_mb_modes(avctx, cur_frame, prev_frame, IS_VP8);
  1944. }
  1945. #if HAVE_THREADS
  1946. #define check_thread_pos(td, otd, mb_x_check, mb_y_check) \
  1947. do { \
  1948. int tmp = (mb_y_check << 16) | (mb_x_check & 0xFFFF); \
  1949. if (otd->thread_mb_pos < tmp) { \
  1950. pthread_mutex_lock(&otd->lock); \
  1951. td->wait_mb_pos = tmp; \
  1952. do { \
  1953. if (otd->thread_mb_pos >= tmp) \
  1954. break; \
  1955. pthread_cond_wait(&otd->cond, &otd->lock); \
  1956. } while (1); \
  1957. td->wait_mb_pos = INT_MAX; \
  1958. pthread_mutex_unlock(&otd->lock); \
  1959. } \
  1960. } while (0);
  1961. #define update_pos(td, mb_y, mb_x) \
  1962. do { \
  1963. int pos = (mb_y << 16) | (mb_x & 0xFFFF); \
  1964. int sliced_threading = (avctx->active_thread_type == FF_THREAD_SLICE) && \
  1965. (num_jobs > 1); \
  1966. int is_null = (next_td == NULL) || (prev_td == NULL); \
  1967. int pos_check = (is_null) ? 1 \
  1968. : (next_td != td && \
  1969. pos >= next_td->wait_mb_pos) || \
  1970. (prev_td != td && \
  1971. pos >= prev_td->wait_mb_pos); \
  1972. td->thread_mb_pos = pos; \
  1973. if (sliced_threading && pos_check) { \
  1974. pthread_mutex_lock(&td->lock); \
  1975. pthread_cond_broadcast(&td->cond); \
  1976. pthread_mutex_unlock(&td->lock); \
  1977. } \
  1978. } while (0);
  1979. #else
  1980. #define check_thread_pos(td, otd, mb_x_check, mb_y_check)
  1981. #define update_pos(td, mb_y, mb_x)
  1982. #endif
  1983. static av_always_inline void decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
  1984. int jobnr, int threadnr, int is_vp7)
  1985. {
  1986. VP8Context *s = avctx->priv_data;
  1987. VP8ThreadData *prev_td, *next_td, *td = &s->thread_data[threadnr];
  1988. int mb_y = td->thread_mb_pos >> 16;
  1989. int mb_x, mb_xy = mb_y * s->mb_width;
  1990. int num_jobs = s->num_jobs;
  1991. VP8Frame *curframe = s->curframe, *prev_frame = s->prev_frame;
  1992. VP56RangeCoder *c = &s->coeff_partition[mb_y & (s->num_coeff_partitions - 1)];
  1993. VP8Macroblock *mb;
  1994. uint8_t *dst[3] = {
  1995. curframe->tf.f->data[0] + 16 * mb_y * s->linesize,
  1996. curframe->tf.f->data[1] + 8 * mb_y * s->uvlinesize,
  1997. curframe->tf.f->data[2] + 8 * mb_y * s->uvlinesize
  1998. };
  1999. if (mb_y == 0)
  2000. prev_td = td;
  2001. else
  2002. prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
  2003. if (mb_y == s->mb_height - 1)
  2004. next_td = td;
  2005. else
  2006. next_td = &s->thread_data[(jobnr + 1) % num_jobs];
  2007. if (s->mb_layout == 1)
  2008. mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
  2009. else {
  2010. // Make sure the previous frame has read its segmentation map,
  2011. // if we re-use the same map.
  2012. if (prev_frame && s->segmentation.enabled &&
  2013. !s->segmentation.update_map)
  2014. ff_thread_await_progress(&prev_frame->tf, mb_y, 0);
  2015. mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
  2016. memset(mb - 1, 0, sizeof(*mb)); // zero left macroblock
  2017. AV_WN32A(s->intra4x4_pred_mode_left, DC_PRED * 0x01010101);
  2018. }
  2019. if (!is_vp7 || mb_y == 0)
  2020. memset(td->left_nnz, 0, sizeof(td->left_nnz));
  2021. s->mv_min.x = -MARGIN;
  2022. s->mv_max.x = ((s->mb_width - 1) << 6) + MARGIN;
  2023. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb_xy++, mb++) {
  2024. // Wait for previous thread to read mb_x+2, and reach mb_y-1.
  2025. if (prev_td != td) {
  2026. if (threadnr != 0) {
  2027. check_thread_pos(td, prev_td,
  2028. mb_x + (is_vp7 ? 2 : 1),
  2029. mb_y - (is_vp7 ? 2 : 1));
  2030. } else {
  2031. check_thread_pos(td, prev_td,
  2032. mb_x + (is_vp7 ? 2 : 1) + s->mb_width + 3,
  2033. mb_y - (is_vp7 ? 2 : 1));
  2034. }
  2035. }
  2036. s->vdsp.prefetch(dst[0] + (mb_x & 3) * 4 * s->linesize + 64,
  2037. s->linesize, 4);
  2038. s->vdsp.prefetch(dst[1] + (mb_x & 7) * s->uvlinesize + 64,
  2039. dst[2] - dst[1], 2);
  2040. if (!s->mb_layout)
  2041. decode_mb_mode(s, mb, mb_x, mb_y, curframe->seg_map->data + mb_xy,
  2042. prev_frame && prev_frame->seg_map ?
  2043. prev_frame->seg_map->data + mb_xy : NULL, 0, is_vp7);
  2044. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_PREVIOUS);
  2045. if (!mb->skip)
  2046. decode_mb_coeffs(s, td, c, mb, s->top_nnz[mb_x], td->left_nnz, is_vp7);
  2047. if (mb->mode <= MODE_I4x4)
  2048. intra_predict(s, td, dst, mb, mb_x, mb_y, is_vp7);
  2049. else
  2050. inter_predict(s, td, dst, mb, mb_x, mb_y);
  2051. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN);
  2052. if (!mb->skip) {
  2053. idct_mb(s, td, dst, mb);
  2054. } else {
  2055. AV_ZERO64(td->left_nnz);
  2056. AV_WN64(s->top_nnz[mb_x], 0); // array of 9, so unaligned
  2057. /* Reset DC block predictors if they would exist
  2058. * if the mb had coefficients */
  2059. if (mb->mode != MODE_I4x4 && mb->mode != VP8_MVMODE_SPLIT) {
  2060. td->left_nnz[8] = 0;
  2061. s->top_nnz[mb_x][8] = 0;
  2062. }
  2063. }
  2064. if (s->deblock_filter)
  2065. filter_level_for_mb(s, mb, &td->filter_strength[mb_x], is_vp7);
  2066. if (s->deblock_filter && num_jobs != 1 && threadnr == num_jobs - 1) {
  2067. if (s->filter.simple)
  2068. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2069. NULL, NULL, s->linesize, 0, 1);
  2070. else
  2071. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2072. dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  2073. }
  2074. prefetch_motion(s, mb, mb_x, mb_y, mb_xy, VP56_FRAME_GOLDEN2);
  2075. dst[0] += 16;
  2076. dst[1] += 8;
  2077. dst[2] += 8;
  2078. s->mv_min.x -= 64;
  2079. s->mv_max.x -= 64;
  2080. if (mb_x == s->mb_width + 1) {
  2081. update_pos(td, mb_y, s->mb_width + 3);
  2082. } else {
  2083. update_pos(td, mb_y, mb_x);
  2084. }
  2085. }
  2086. }
  2087. static void vp7_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
  2088. int jobnr, int threadnr)
  2089. {
  2090. decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, 1);
  2091. }
  2092. static void vp8_decode_mb_row_no_filter(AVCodecContext *avctx, void *tdata,
  2093. int jobnr, int threadnr)
  2094. {
  2095. decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr, 0);
  2096. }
  2097. static av_always_inline void filter_mb_row(AVCodecContext *avctx, void *tdata,
  2098. int jobnr, int threadnr, int is_vp7)
  2099. {
  2100. VP8Context *s = avctx->priv_data;
  2101. VP8ThreadData *td = &s->thread_data[threadnr];
  2102. int mb_x, mb_y = td->thread_mb_pos >> 16, num_jobs = s->num_jobs;
  2103. AVFrame *curframe = s->curframe->tf.f;
  2104. VP8Macroblock *mb;
  2105. VP8ThreadData *prev_td, *next_td;
  2106. uint8_t *dst[3] = {
  2107. curframe->data[0] + 16 * mb_y * s->linesize,
  2108. curframe->data[1] + 8 * mb_y * s->uvlinesize,
  2109. curframe->data[2] + 8 * mb_y * s->uvlinesize
  2110. };
  2111. if (s->mb_layout == 1)
  2112. mb = s->macroblocks_base + ((s->mb_width + 1) * (mb_y + 1) + 1);
  2113. else
  2114. mb = s->macroblocks + (s->mb_height - mb_y - 1) * 2;
  2115. if (mb_y == 0)
  2116. prev_td = td;
  2117. else
  2118. prev_td = &s->thread_data[(jobnr + num_jobs - 1) % num_jobs];
  2119. if (mb_y == s->mb_height - 1)
  2120. next_td = td;
  2121. else
  2122. next_td = &s->thread_data[(jobnr + 1) % num_jobs];
  2123. for (mb_x = 0; mb_x < s->mb_width; mb_x++, mb++) {
  2124. VP8FilterStrength *f = &td->filter_strength[mb_x];
  2125. if (prev_td != td)
  2126. check_thread_pos(td, prev_td,
  2127. (mb_x + 1) + (s->mb_width + 3), mb_y - 1);
  2128. if (next_td != td)
  2129. if (next_td != &s->thread_data[0])
  2130. check_thread_pos(td, next_td, mb_x + 1, mb_y + 1);
  2131. if (num_jobs == 1) {
  2132. if (s->filter.simple)
  2133. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2134. NULL, NULL, s->linesize, 0, 1);
  2135. else
  2136. backup_mb_border(s->top_border[mb_x + 1], dst[0],
  2137. dst[1], dst[2], s->linesize, s->uvlinesize, 0);
  2138. }
  2139. if (s->filter.simple)
  2140. filter_mb_simple(s, dst[0], f, mb_x, mb_y);
  2141. else
  2142. filter_mb(s, dst, f, mb_x, mb_y, is_vp7);
  2143. dst[0] += 16;
  2144. dst[1] += 8;
  2145. dst[2] += 8;
  2146. update_pos(td, mb_y, (s->mb_width + 3) + mb_x);
  2147. }
  2148. }
  2149. static void vp7_filter_mb_row(AVCodecContext *avctx, void *tdata,
  2150. int jobnr, int threadnr)
  2151. {
  2152. filter_mb_row(avctx, tdata, jobnr, threadnr, 1);
  2153. }
  2154. static void vp8_filter_mb_row(AVCodecContext *avctx, void *tdata,
  2155. int jobnr, int threadnr)
  2156. {
  2157. filter_mb_row(avctx, tdata, jobnr, threadnr, 0);
  2158. }
  2159. static av_always_inline
  2160. int vp78_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata, int jobnr,
  2161. int threadnr, int is_vp7)
  2162. {
  2163. VP8Context *s = avctx->priv_data;
  2164. VP8ThreadData *td = &s->thread_data[jobnr];
  2165. VP8ThreadData *next_td = NULL, *prev_td = NULL;
  2166. VP8Frame *curframe = s->curframe;
  2167. int mb_y, num_jobs = s->num_jobs;
  2168. td->thread_nr = threadnr;
  2169. for (mb_y = jobnr; mb_y < s->mb_height; mb_y += num_jobs) {
  2170. if (mb_y >= s->mb_height)
  2171. break;
  2172. td->thread_mb_pos = mb_y << 16;
  2173. s->decode_mb_row_no_filter(avctx, tdata, jobnr, threadnr);
  2174. if (s->deblock_filter)
  2175. s->filter_mb_row(avctx, tdata, jobnr, threadnr);
  2176. update_pos(td, mb_y, INT_MAX & 0xFFFF);
  2177. s->mv_min.y -= 64;
  2178. s->mv_max.y -= 64;
  2179. if (avctx->active_thread_type == FF_THREAD_FRAME)
  2180. ff_thread_report_progress(&curframe->tf, mb_y, 0);
  2181. }
  2182. return 0;
  2183. }
  2184. static int vp7_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
  2185. int jobnr, int threadnr)
  2186. {
  2187. return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP7);
  2188. }
  2189. static int vp8_decode_mb_row_sliced(AVCodecContext *avctx, void *tdata,
  2190. int jobnr, int threadnr)
  2191. {
  2192. return vp78_decode_mb_row_sliced(avctx, tdata, jobnr, threadnr, IS_VP8);
  2193. }
  2194. static av_always_inline
  2195. int vp78_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  2196. AVPacket *avpkt, int is_vp7)
  2197. {
  2198. VP8Context *s = avctx->priv_data;
  2199. int ret, i, referenced, num_jobs;
  2200. enum AVDiscard skip_thresh;
  2201. VP8Frame *av_uninit(curframe), *prev_frame;
  2202. if (is_vp7)
  2203. ret = vp7_decode_frame_header(s, avpkt->data, avpkt->size);
  2204. else
  2205. ret = vp8_decode_frame_header(s, avpkt->data, avpkt->size);
  2206. if (ret < 0)
  2207. goto err;
  2208. prev_frame = s->framep[VP56_FRAME_CURRENT];
  2209. referenced = s->update_last || s->update_golden == VP56_FRAME_CURRENT ||
  2210. s->update_altref == VP56_FRAME_CURRENT;
  2211. skip_thresh = !referenced ? AVDISCARD_NONREF
  2212. : !s->keyframe ? AVDISCARD_NONKEY
  2213. : AVDISCARD_ALL;
  2214. if (avctx->skip_frame >= skip_thresh) {
  2215. s->invisible = 1;
  2216. memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
  2217. goto skip_decode;
  2218. }
  2219. s->deblock_filter = s->filter.level && avctx->skip_loop_filter < skip_thresh;
  2220. // release no longer referenced frames
  2221. for (i = 0; i < 5; i++)
  2222. if (s->frames[i].tf.f->data[0] &&
  2223. &s->frames[i] != prev_frame &&
  2224. &s->frames[i] != s->framep[VP56_FRAME_PREVIOUS] &&
  2225. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN] &&
  2226. &s->frames[i] != s->framep[VP56_FRAME_GOLDEN2])
  2227. vp8_release_frame(s, &s->frames[i]);
  2228. curframe = s->framep[VP56_FRAME_CURRENT] = vp8_find_free_buffer(s);
  2229. /* Given that arithmetic probabilities are updated every frame, it's quite
  2230. * likely that the values we have on a random interframe are complete
  2231. * junk if we didn't start decode on a keyframe. So just don't display
  2232. * anything rather than junk. */
  2233. if (!s->keyframe && (!s->framep[VP56_FRAME_PREVIOUS] ||
  2234. !s->framep[VP56_FRAME_GOLDEN] ||
  2235. !s->framep[VP56_FRAME_GOLDEN2])) {
  2236. av_log(avctx, AV_LOG_WARNING,
  2237. "Discarding interframe without a prior keyframe!\n");
  2238. ret = AVERROR_INVALIDDATA;
  2239. goto err;
  2240. }
  2241. curframe->tf.f->key_frame = s->keyframe;
  2242. curframe->tf.f->pict_type = s->keyframe ? AV_PICTURE_TYPE_I
  2243. : AV_PICTURE_TYPE_P;
  2244. if ((ret = vp8_alloc_frame(s, curframe, referenced)) < 0)
  2245. goto err;
  2246. // check if golden and altref are swapped
  2247. if (s->update_altref != VP56_FRAME_NONE)
  2248. s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[s->update_altref];
  2249. else
  2250. s->next_framep[VP56_FRAME_GOLDEN2] = s->framep[VP56_FRAME_GOLDEN2];
  2251. if (s->update_golden != VP56_FRAME_NONE)
  2252. s->next_framep[VP56_FRAME_GOLDEN] = s->framep[s->update_golden];
  2253. else
  2254. s->next_framep[VP56_FRAME_GOLDEN] = s->framep[VP56_FRAME_GOLDEN];
  2255. if (s->update_last)
  2256. s->next_framep[VP56_FRAME_PREVIOUS] = curframe;
  2257. else
  2258. s->next_framep[VP56_FRAME_PREVIOUS] = s->framep[VP56_FRAME_PREVIOUS];
  2259. s->next_framep[VP56_FRAME_CURRENT] = curframe;
  2260. if (avctx->codec->update_thread_context)
  2261. ff_thread_finish_setup(avctx);
  2262. s->linesize = curframe->tf.f->linesize[0];
  2263. s->uvlinesize = curframe->tf.f->linesize[1];
  2264. memset(s->top_nnz, 0, s->mb_width * sizeof(*s->top_nnz));
  2265. /* Zero macroblock structures for top/top-left prediction
  2266. * from outside the frame. */
  2267. if (!s->mb_layout)
  2268. memset(s->macroblocks + s->mb_height * 2 - 1, 0,
  2269. (s->mb_width + 1) * sizeof(*s->macroblocks));
  2270. if (!s->mb_layout && s->keyframe)
  2271. memset(s->intra4x4_pred_mode_top, DC_PRED, s->mb_width * 4);
  2272. memset(s->ref_count, 0, sizeof(s->ref_count));
  2273. if (s->mb_layout == 1) {
  2274. // Make sure the previous frame has read its segmentation map,
  2275. // if we re-use the same map.
  2276. if (prev_frame && s->segmentation.enabled &&
  2277. !s->segmentation.update_map)
  2278. ff_thread_await_progress(&prev_frame->tf, 1, 0);
  2279. if (is_vp7)
  2280. vp7_decode_mv_mb_modes(avctx, curframe, prev_frame);
  2281. else
  2282. vp8_decode_mv_mb_modes(avctx, curframe, prev_frame);
  2283. }
  2284. if (avctx->active_thread_type == FF_THREAD_FRAME)
  2285. num_jobs = 1;
  2286. else
  2287. num_jobs = FFMIN(s->num_coeff_partitions, avctx->thread_count);
  2288. s->num_jobs = num_jobs;
  2289. s->curframe = curframe;
  2290. s->prev_frame = prev_frame;
  2291. s->mv_min.y = -MARGIN;
  2292. s->mv_max.y = ((s->mb_height - 1) << 6) + MARGIN;
  2293. for (i = 0; i < MAX_THREADS; i++) {
  2294. s->thread_data[i].thread_mb_pos = 0;
  2295. s->thread_data[i].wait_mb_pos = INT_MAX;
  2296. }
  2297. if (is_vp7)
  2298. avctx->execute2(avctx, vp7_decode_mb_row_sliced, s->thread_data, NULL,
  2299. num_jobs);
  2300. else
  2301. avctx->execute2(avctx, vp8_decode_mb_row_sliced, s->thread_data, NULL,
  2302. num_jobs);
  2303. ff_thread_report_progress(&curframe->tf, INT_MAX, 0);
  2304. memcpy(&s->framep[0], &s->next_framep[0], sizeof(s->framep[0]) * 4);
  2305. skip_decode:
  2306. // if future frames don't use the updated probabilities,
  2307. // reset them to the values we saved
  2308. if (!s->update_probabilities)
  2309. s->prob[0] = s->prob[1];
  2310. if (!s->invisible) {
  2311. if ((ret = av_frame_ref(data, curframe->tf.f)) < 0)
  2312. return ret;
  2313. *got_frame = 1;
  2314. }
  2315. return avpkt->size;
  2316. err:
  2317. memcpy(&s->next_framep[0], &s->framep[0], sizeof(s->framep[0]) * 4);
  2318. return ret;
  2319. }
  2320. int ff_vp8_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  2321. AVPacket *avpkt)
  2322. {
  2323. return vp78_decode_frame(avctx, data, got_frame, avpkt, IS_VP8);
  2324. }
  2325. #if CONFIG_VP7_DECODER
  2326. static int vp7_decode_frame(AVCodecContext *avctx, void *data, int *got_frame,
  2327. AVPacket *avpkt)
  2328. {
  2329. return vp78_decode_frame(avctx, data, got_frame, avpkt, IS_VP7);
  2330. }
  2331. #endif /* CONFIG_VP7_DECODER */
  2332. av_cold int ff_vp8_decode_free(AVCodecContext *avctx)
  2333. {
  2334. VP8Context *s = avctx->priv_data;
  2335. int i;
  2336. vp8_decode_flush_impl(avctx, 1);
  2337. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++)
  2338. av_frame_free(&s->frames[i].tf.f);
  2339. return 0;
  2340. }
  2341. static av_cold int vp8_init_frames(VP8Context *s)
  2342. {
  2343. int i;
  2344. for (i = 0; i < FF_ARRAY_ELEMS(s->frames); i++) {
  2345. s->frames[i].tf.f = av_frame_alloc();
  2346. if (!s->frames[i].tf.f)
  2347. return AVERROR(ENOMEM);
  2348. }
  2349. return 0;
  2350. }
  2351. static av_always_inline
  2352. int vp78_decode_init(AVCodecContext *avctx, int is_vp7)
  2353. {
  2354. VP8Context *s = avctx->priv_data;
  2355. int ret;
  2356. s->avctx = avctx;
  2357. s->vp7 = avctx->codec->id == AV_CODEC_ID_VP7;
  2358. avctx->pix_fmt = AV_PIX_FMT_YUV420P;
  2359. avctx->internal->allocate_progress = 1;
  2360. ff_videodsp_init(&s->vdsp, 8);
  2361. ff_vp78dsp_init(&s->vp8dsp);
  2362. if (CONFIG_VP7_DECODER && is_vp7) {
  2363. ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP7, 8, 1);
  2364. ff_vp7dsp_init(&s->vp8dsp);
  2365. s->decode_mb_row_no_filter = vp7_decode_mb_row_no_filter;
  2366. s->filter_mb_row = vp7_filter_mb_row;
  2367. } else if (CONFIG_VP8_DECODER && !is_vp7) {
  2368. ff_h264_pred_init(&s->hpc, AV_CODEC_ID_VP8, 8, 1);
  2369. ff_vp8dsp_init(&s->vp8dsp);
  2370. s->decode_mb_row_no_filter = vp8_decode_mb_row_no_filter;
  2371. s->filter_mb_row = vp8_filter_mb_row;
  2372. }
  2373. /* does not change for VP8 */
  2374. memcpy(s->prob[0].scan, zigzag_scan, sizeof(s->prob[0].scan));
  2375. if ((ret = vp8_init_frames(s)) < 0) {
  2376. ff_vp8_decode_free(avctx);
  2377. return ret;
  2378. }
  2379. return 0;
  2380. }
  2381. #if CONFIG_VP7_DECODER
  2382. static int vp7_decode_init(AVCodecContext *avctx)
  2383. {
  2384. return vp78_decode_init(avctx, IS_VP7);
  2385. }
  2386. #endif /* CONFIG_VP7_DECODER */
  2387. av_cold int ff_vp8_decode_init(AVCodecContext *avctx)
  2388. {
  2389. return vp78_decode_init(avctx, IS_VP8);
  2390. }
  2391. #if CONFIG_VP8_DECODER
  2392. static av_cold int vp8_decode_init_thread_copy(AVCodecContext *avctx)
  2393. {
  2394. VP8Context *s = avctx->priv_data;
  2395. int ret;
  2396. s->avctx = avctx;
  2397. if ((ret = vp8_init_frames(s)) < 0) {
  2398. ff_vp8_decode_free(avctx);
  2399. return ret;
  2400. }
  2401. return 0;
  2402. }
  2403. #define REBASE(pic) ((pic) ? (pic) - &s_src->frames[0] + &s->frames[0] : NULL)
  2404. static int vp8_decode_update_thread_context(AVCodecContext *dst,
  2405. const AVCodecContext *src)
  2406. {
  2407. VP8Context *s = dst->priv_data, *s_src = src->priv_data;
  2408. int i;
  2409. if (s->macroblocks_base &&
  2410. (s_src->mb_width != s->mb_width || s_src->mb_height != s->mb_height)) {
  2411. free_buffers(s);
  2412. s->mb_width = s_src->mb_width;
  2413. s->mb_height = s_src->mb_height;
  2414. }
  2415. s->prob[0] = s_src->prob[!s_src->update_probabilities];
  2416. s->segmentation = s_src->segmentation;
  2417. s->lf_delta = s_src->lf_delta;
  2418. memcpy(s->sign_bias, s_src->sign_bias, sizeof(s->sign_bias));
  2419. for (i = 0; i < FF_ARRAY_ELEMS(s_src->frames); i++) {
  2420. if (s_src->frames[i].tf.f->data[0]) {
  2421. int ret = vp8_ref_frame(s, &s->frames[i], &s_src->frames[i]);
  2422. if (ret < 0)
  2423. return ret;
  2424. }
  2425. }
  2426. s->framep[0] = REBASE(s_src->next_framep[0]);
  2427. s->framep[1] = REBASE(s_src->next_framep[1]);
  2428. s->framep[2] = REBASE(s_src->next_framep[2]);
  2429. s->framep[3] = REBASE(s_src->next_framep[3]);
  2430. return 0;
  2431. }
  2432. #endif /* CONFIG_VP8_DECODER */
  2433. #if CONFIG_VP7_DECODER
  2434. AVCodec ff_vp7_decoder = {
  2435. .name = "vp7",
  2436. .long_name = NULL_IF_CONFIG_SMALL("On2 VP7"),
  2437. .type = AVMEDIA_TYPE_VIDEO,
  2438. .id = AV_CODEC_ID_VP7,
  2439. .priv_data_size = sizeof(VP8Context),
  2440. .init = vp7_decode_init,
  2441. .close = ff_vp8_decode_free,
  2442. .decode = vp7_decode_frame,
  2443. .capabilities = CODEC_CAP_DR1,
  2444. .flush = vp8_decode_flush,
  2445. };
  2446. #endif /* CONFIG_VP7_DECODER */
  2447. #if CONFIG_VP8_DECODER
  2448. AVCodec ff_vp8_decoder = {
  2449. .name = "vp8",
  2450. .long_name = NULL_IF_CONFIG_SMALL("On2 VP8"),
  2451. .type = AVMEDIA_TYPE_VIDEO,
  2452. .id = AV_CODEC_ID_VP8,
  2453. .priv_data_size = sizeof(VP8Context),
  2454. .init = ff_vp8_decode_init,
  2455. .close = ff_vp8_decode_free,
  2456. .decode = ff_vp8_decode_frame,
  2457. .capabilities = CODEC_CAP_DR1 | CODEC_CAP_FRAME_THREADS | CODEC_CAP_SLICE_THREADS,
  2458. .flush = vp8_decode_flush,
  2459. .init_thread_copy = ONLY_IF_THREADS_ENABLED(vp8_decode_init_thread_copy),
  2460. .update_thread_context = ONLY_IF_THREADS_ENABLED(vp8_decode_update_thread_context),
  2461. };
  2462. #endif /* CONFIG_VP7_DECODER */