vp8.c 102 KB

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