postprocess_template.c 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632
  1. /*
  2. * Copyright (C) 2001-2002 Michael Niedermayer (michaelni@gmx.at)
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * mmx/mmx2/3dnow postprocess code.
  23. */
  24. #include "libavutil/x86_cpu.h"
  25. #define ALIGN_MASK "$-8"
  26. #undef REAL_PAVGB
  27. #undef PAVGB
  28. #undef PMINUB
  29. #undef PMAXUB
  30. #if HAVE_MMX2
  31. #define REAL_PAVGB(a,b) "pavgb " #a ", " #b " \n\t"
  32. #elif HAVE_AMD3DNOW
  33. #define REAL_PAVGB(a,b) "pavgusb " #a ", " #b " \n\t"
  34. #endif
  35. #define PAVGB(a,b) REAL_PAVGB(a,b)
  36. #if HAVE_MMX2
  37. #define PMINUB(a,b,t) "pminub " #a ", " #b " \n\t"
  38. #elif HAVE_MMX
  39. #define PMINUB(b,a,t) \
  40. "movq " #a ", " #t " \n\t"\
  41. "psubusb " #b ", " #t " \n\t"\
  42. "psubb " #t ", " #a " \n\t"
  43. #endif
  44. #if HAVE_MMX2
  45. #define PMAXUB(a,b) "pmaxub " #a ", " #b " \n\t"
  46. #elif HAVE_MMX
  47. #define PMAXUB(a,b) \
  48. "psubusb " #a ", " #b " \n\t"\
  49. "paddb " #a ", " #b " \n\t"
  50. #endif
  51. //FIXME? |255-0| = 1 (should not be a problem ...)
  52. #if HAVE_MMX
  53. /**
  54. * Check if the middle 8x8 Block in the given 8x16 block is flat
  55. */
  56. static inline int RENAME(vertClassify)(uint8_t src[], int stride, PPContext *c){
  57. int numEq= 0, dcOk;
  58. src+= stride*4; // src points to begin of the 8x8 Block
  59. __asm__ volatile(
  60. "movq %0, %%mm7 \n\t"
  61. "movq %1, %%mm6 \n\t"
  62. : : "m" (c->mmxDcOffset[c->nonBQP]), "m" (c->mmxDcThreshold[c->nonBQP])
  63. );
  64. __asm__ volatile(
  65. "lea (%2, %3), %%"REG_a" \n\t"
  66. // 0 1 2 3 4 5 6 7 8 9
  67. // %1 eax eax+%2 eax+2%2 %1+4%2 ecx ecx+%2 ecx+2%2 %1+8%2 ecx+4%2
  68. "movq (%2), %%mm0 \n\t"
  69. "movq (%%"REG_a"), %%mm1 \n\t"
  70. "movq %%mm0, %%mm3 \n\t"
  71. "movq %%mm0, %%mm4 \n\t"
  72. PMAXUB(%%mm1, %%mm4)
  73. PMINUB(%%mm1, %%mm3, %%mm5)
  74. "psubb %%mm1, %%mm0 \n\t" // mm0 = differnece
  75. "paddb %%mm7, %%mm0 \n\t"
  76. "pcmpgtb %%mm6, %%mm0 \n\t"
  77. "movq (%%"REG_a",%3), %%mm2 \n\t"
  78. PMAXUB(%%mm2, %%mm4)
  79. PMINUB(%%mm2, %%mm3, %%mm5)
  80. "psubb %%mm2, %%mm1 \n\t"
  81. "paddb %%mm7, %%mm1 \n\t"
  82. "pcmpgtb %%mm6, %%mm1 \n\t"
  83. "paddb %%mm1, %%mm0 \n\t"
  84. "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
  85. PMAXUB(%%mm1, %%mm4)
  86. PMINUB(%%mm1, %%mm3, %%mm5)
  87. "psubb %%mm1, %%mm2 \n\t"
  88. "paddb %%mm7, %%mm2 \n\t"
  89. "pcmpgtb %%mm6, %%mm2 \n\t"
  90. "paddb %%mm2, %%mm0 \n\t"
  91. "lea (%%"REG_a", %3, 4), %%"REG_a" \n\t"
  92. "movq (%2, %3, 4), %%mm2 \n\t"
  93. PMAXUB(%%mm2, %%mm4)
  94. PMINUB(%%mm2, %%mm3, %%mm5)
  95. "psubb %%mm2, %%mm1 \n\t"
  96. "paddb %%mm7, %%mm1 \n\t"
  97. "pcmpgtb %%mm6, %%mm1 \n\t"
  98. "paddb %%mm1, %%mm0 \n\t"
  99. "movq (%%"REG_a"), %%mm1 \n\t"
  100. PMAXUB(%%mm1, %%mm4)
  101. PMINUB(%%mm1, %%mm3, %%mm5)
  102. "psubb %%mm1, %%mm2 \n\t"
  103. "paddb %%mm7, %%mm2 \n\t"
  104. "pcmpgtb %%mm6, %%mm2 \n\t"
  105. "paddb %%mm2, %%mm0 \n\t"
  106. "movq (%%"REG_a", %3), %%mm2 \n\t"
  107. PMAXUB(%%mm2, %%mm4)
  108. PMINUB(%%mm2, %%mm3, %%mm5)
  109. "psubb %%mm2, %%mm1 \n\t"
  110. "paddb %%mm7, %%mm1 \n\t"
  111. "pcmpgtb %%mm6, %%mm1 \n\t"
  112. "paddb %%mm1, %%mm0 \n\t"
  113. "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
  114. PMAXUB(%%mm1, %%mm4)
  115. PMINUB(%%mm1, %%mm3, %%mm5)
  116. "psubb %%mm1, %%mm2 \n\t"
  117. "paddb %%mm7, %%mm2 \n\t"
  118. "pcmpgtb %%mm6, %%mm2 \n\t"
  119. "paddb %%mm2, %%mm0 \n\t"
  120. "psubusb %%mm3, %%mm4 \n\t"
  121. " \n\t"
  122. #if HAVE_MMX2
  123. "pxor %%mm7, %%mm7 \n\t"
  124. "psadbw %%mm7, %%mm0 \n\t"
  125. #else
  126. "movq %%mm0, %%mm1 \n\t"
  127. "psrlw $8, %%mm0 \n\t"
  128. "paddb %%mm1, %%mm0 \n\t"
  129. "movq %%mm0, %%mm1 \n\t"
  130. "psrlq $16, %%mm0 \n\t"
  131. "paddb %%mm1, %%mm0 \n\t"
  132. "movq %%mm0, %%mm1 \n\t"
  133. "psrlq $32, %%mm0 \n\t"
  134. "paddb %%mm1, %%mm0 \n\t"
  135. #endif
  136. "movq %4, %%mm7 \n\t" // QP,..., QP
  137. "paddusb %%mm7, %%mm7 \n\t" // 2QP ... 2QP
  138. "psubusb %%mm7, %%mm4 \n\t" // Diff <= 2QP -> 0
  139. "packssdw %%mm4, %%mm4 \n\t"
  140. "movd %%mm0, %0 \n\t"
  141. "movd %%mm4, %1 \n\t"
  142. : "=r" (numEq), "=r" (dcOk)
  143. : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
  144. : "%"REG_a
  145. );
  146. numEq= (-numEq) &0xFF;
  147. if(numEq > c->ppMode.flatnessThreshold){
  148. if(dcOk) return 0;
  149. else return 1;
  150. }else{
  151. return 2;
  152. }
  153. }
  154. #endif //HAVE_MMX
  155. /**
  156. * Do a vertical low pass filter on the 8x16 block (only write to the 8x8 block in the middle)
  157. * using the 9-Tap Filter (1,1,2,2,4,2,2,1,1)/16
  158. */
  159. #if !HAVE_ALTIVEC
  160. static inline void RENAME(doVertLowPass)(uint8_t *src, int stride, PPContext *c)
  161. {
  162. #if HAVE_MMX2 || HAVE_AMD3DNOW
  163. src+= stride*3;
  164. __asm__ volatile( //"movv %0 %1 %2\n\t"
  165. "movq %2, %%mm0 \n\t" // QP,..., QP
  166. "pxor %%mm4, %%mm4 \n\t"
  167. "movq (%0), %%mm6 \n\t"
  168. "movq (%0, %1), %%mm5 \n\t"
  169. "movq %%mm5, %%mm1 \n\t"
  170. "movq %%mm6, %%mm2 \n\t"
  171. "psubusb %%mm6, %%mm5 \n\t"
  172. "psubusb %%mm1, %%mm2 \n\t"
  173. "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
  174. "psubusb %%mm0, %%mm2 \n\t" // diff <= QP -> 0
  175. "pcmpeqb %%mm4, %%mm2 \n\t" // diff <= QP -> FF
  176. "pand %%mm2, %%mm6 \n\t"
  177. "pandn %%mm1, %%mm2 \n\t"
  178. "por %%mm2, %%mm6 \n\t"// First Line to Filter
  179. "movq (%0, %1, 8), %%mm5 \n\t"
  180. "lea (%0, %1, 4), %%"REG_a" \n\t"
  181. "lea (%0, %1, 8), %%"REG_c" \n\t"
  182. "sub %1, %%"REG_c" \n\t"
  183. "add %1, %0 \n\t" // %0 points to line 1 not 0
  184. "movq (%0, %1, 8), %%mm7 \n\t"
  185. "movq %%mm5, %%mm1 \n\t"
  186. "movq %%mm7, %%mm2 \n\t"
  187. "psubusb %%mm7, %%mm5 \n\t"
  188. "psubusb %%mm1, %%mm2 \n\t"
  189. "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
  190. "psubusb %%mm0, %%mm2 \n\t" // diff <= QP -> 0
  191. "pcmpeqb %%mm4, %%mm2 \n\t" // diff <= QP -> FF
  192. "pand %%mm2, %%mm7 \n\t"
  193. "pandn %%mm1, %%mm2 \n\t"
  194. "por %%mm2, %%mm7 \n\t" // First Line to Filter
  195. // 1 2 3 4 5 6 7 8
  196. // %0 %0+%1 %0+2%1 eax %0+4%1 eax+2%1 ecx eax+4%1
  197. // 6 4 2 2 1 1
  198. // 6 4 4 2
  199. // 6 8 2
  200. "movq (%0, %1), %%mm0 \n\t" // 1
  201. "movq %%mm0, %%mm1 \n\t" // 1
  202. PAVGB(%%mm6, %%mm0) //1 1 /2
  203. PAVGB(%%mm6, %%mm0) //3 1 /4
  204. "movq (%0, %1, 4), %%mm2 \n\t" // 1
  205. "movq %%mm2, %%mm5 \n\t" // 1
  206. PAVGB((%%REGa), %%mm2) // 11 /2
  207. PAVGB((%0, %1, 2), %%mm2) // 211 /4
  208. "movq %%mm2, %%mm3 \n\t" // 211 /4
  209. "movq (%0), %%mm4 \n\t" // 1
  210. PAVGB(%%mm4, %%mm3) // 4 211 /8
  211. PAVGB(%%mm0, %%mm3) //642211 /16
  212. "movq %%mm3, (%0) \n\t" // X
  213. // mm1=2 mm2=3(211) mm4=1 mm5=5 mm6=0 mm7=9
  214. "movq %%mm1, %%mm0 \n\t" // 1
  215. PAVGB(%%mm6, %%mm0) //1 1 /2
  216. "movq %%mm4, %%mm3 \n\t" // 1
  217. PAVGB((%0,%1,2), %%mm3) // 1 1 /2
  218. PAVGB((%%REGa,%1,2), %%mm5) // 11 /2
  219. PAVGB((%%REGa), %%mm5) // 211 /4
  220. PAVGB(%%mm5, %%mm3) // 2 2211 /8
  221. PAVGB(%%mm0, %%mm3) //4242211 /16
  222. "movq %%mm3, (%0,%1) \n\t" // X
  223. // mm1=2 mm2=3(211) mm4=1 mm5=4(211) mm6=0 mm7=9
  224. PAVGB(%%mm4, %%mm6) //11 /2
  225. "movq (%%"REG_c"), %%mm0 \n\t" // 1
  226. PAVGB((%%REGa, %1, 2), %%mm0) // 11/2
  227. "movq %%mm0, %%mm3 \n\t" // 11/2
  228. PAVGB(%%mm1, %%mm0) // 2 11/4
  229. PAVGB(%%mm6, %%mm0) //222 11/8
  230. PAVGB(%%mm2, %%mm0) //22242211/16
  231. "movq (%0, %1, 2), %%mm2 \n\t" // 1
  232. "movq %%mm0, (%0, %1, 2) \n\t" // X
  233. // mm1=2 mm2=3 mm3=6(11) mm4=1 mm5=4(211) mm6=0(11) mm7=9
  234. "movq (%%"REG_a", %1, 4), %%mm0 \n\t" // 1
  235. PAVGB((%%REGc), %%mm0) // 11 /2
  236. PAVGB(%%mm0, %%mm6) //11 11 /4
  237. PAVGB(%%mm1, %%mm4) // 11 /2
  238. PAVGB(%%mm2, %%mm1) // 11 /2
  239. PAVGB(%%mm1, %%mm6) //1122 11 /8
  240. PAVGB(%%mm5, %%mm6) //112242211 /16
  241. "movq (%%"REG_a"), %%mm5 \n\t" // 1
  242. "movq %%mm6, (%%"REG_a") \n\t" // X
  243. // mm0=7(11) mm1=2(11) mm2=3 mm3=6(11) mm4=1(11) mm5=4 mm7=9
  244. "movq (%%"REG_a", %1, 4), %%mm6 \n\t" // 1
  245. PAVGB(%%mm7, %%mm6) // 11 /2
  246. PAVGB(%%mm4, %%mm6) // 11 11 /4
  247. PAVGB(%%mm3, %%mm6) // 11 2211 /8
  248. PAVGB(%%mm5, %%mm2) // 11 /2
  249. "movq (%0, %1, 4), %%mm4 \n\t" // 1
  250. PAVGB(%%mm4, %%mm2) // 112 /4
  251. PAVGB(%%mm2, %%mm6) // 112242211 /16
  252. "movq %%mm6, (%0, %1, 4) \n\t" // X
  253. // mm0=7(11) mm1=2(11) mm2=3(112) mm3=6(11) mm4=5 mm5=4 mm7=9
  254. PAVGB(%%mm7, %%mm1) // 11 2 /4
  255. PAVGB(%%mm4, %%mm5) // 11 /2
  256. PAVGB(%%mm5, %%mm0) // 11 11 /4
  257. "movq (%%"REG_a", %1, 2), %%mm6 \n\t" // 1
  258. PAVGB(%%mm6, %%mm1) // 11 4 2 /8
  259. PAVGB(%%mm0, %%mm1) // 11224222 /16
  260. "movq %%mm1, (%%"REG_a", %1, 2) \n\t" // X
  261. // mm2=3(112) mm3=6(11) mm4=5 mm5=4(11) mm6=6 mm7=9
  262. PAVGB((%%REGc), %%mm2) // 112 4 /8
  263. "movq (%%"REG_a", %1, 4), %%mm0 \n\t" // 1
  264. PAVGB(%%mm0, %%mm6) // 1 1 /2
  265. PAVGB(%%mm7, %%mm6) // 1 12 /4
  266. PAVGB(%%mm2, %%mm6) // 1122424 /4
  267. "movq %%mm6, (%%"REG_c") \n\t" // X
  268. // mm0=8 mm3=6(11) mm4=5 mm5=4(11) mm7=9
  269. PAVGB(%%mm7, %%mm5) // 11 2 /4
  270. PAVGB(%%mm7, %%mm5) // 11 6 /8
  271. PAVGB(%%mm3, %%mm0) // 112 /4
  272. PAVGB(%%mm0, %%mm5) // 112246 /16
  273. "movq %%mm5, (%%"REG_a", %1, 4) \n\t" // X
  274. "sub %1, %0 \n\t"
  275. :
  276. : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
  277. : "%"REG_a, "%"REG_c
  278. );
  279. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  280. const int l1= stride;
  281. const int l2= stride + l1;
  282. const int l3= stride + l2;
  283. const int l4= stride + l3;
  284. const int l5= stride + l4;
  285. const int l6= stride + l5;
  286. const int l7= stride + l6;
  287. const int l8= stride + l7;
  288. const int l9= stride + l8;
  289. int x;
  290. src+= stride*3;
  291. for(x=0; x<BLOCK_SIZE; x++){
  292. const int first= FFABS(src[0] - src[l1]) < c->QP ? src[0] : src[l1];
  293. const int last= FFABS(src[l8] - src[l9]) < c->QP ? src[l9] : src[l8];
  294. int sums[10];
  295. sums[0] = 4*first + src[l1] + src[l2] + src[l3] + 4;
  296. sums[1] = sums[0] - first + src[l4];
  297. sums[2] = sums[1] - first + src[l5];
  298. sums[3] = sums[2] - first + src[l6];
  299. sums[4] = sums[3] - first + src[l7];
  300. sums[5] = sums[4] - src[l1] + src[l8];
  301. sums[6] = sums[5] - src[l2] + last;
  302. sums[7] = sums[6] - src[l3] + last;
  303. sums[8] = sums[7] - src[l4] + last;
  304. sums[9] = sums[8] - src[l5] + last;
  305. src[l1]= (sums[0] + sums[2] + 2*src[l1])>>4;
  306. src[l2]= (sums[1] + sums[3] + 2*src[l2])>>4;
  307. src[l3]= (sums[2] + sums[4] + 2*src[l3])>>4;
  308. src[l4]= (sums[3] + sums[5] + 2*src[l4])>>4;
  309. src[l5]= (sums[4] + sums[6] + 2*src[l5])>>4;
  310. src[l6]= (sums[5] + sums[7] + 2*src[l6])>>4;
  311. src[l7]= (sums[6] + sums[8] + 2*src[l7])>>4;
  312. src[l8]= (sums[7] + sums[9] + 2*src[l8])>>4;
  313. src++;
  314. }
  315. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  316. }
  317. #endif //HAVE_ALTIVEC
  318. /**
  319. * Experimental Filter 1
  320. * will not damage linear gradients
  321. * Flat blocks should look like they were passed through the (1,1,2,2,4,2,2,1,1) 9-Tap filter
  322. * can only smooth blocks at the expected locations (it cannot smooth them if they did move)
  323. * MMX2 version does correct clipping C version does not
  324. */
  325. static inline void RENAME(vertX1Filter)(uint8_t *src, int stride, PPContext *co)
  326. {
  327. #if HAVE_MMX2 || HAVE_AMD3DNOW
  328. src+= stride*3;
  329. __asm__ volatile(
  330. "pxor %%mm7, %%mm7 \n\t" // 0
  331. "lea (%0, %1), %%"REG_a" \n\t"
  332. "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
  333. // 0 1 2 3 4 5 6 7 8 9
  334. // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
  335. "movq (%%"REG_a", %1, 2), %%mm0 \n\t" // line 3
  336. "movq (%0, %1, 4), %%mm1 \n\t" // line 4
  337. "movq %%mm1, %%mm2 \n\t" // line 4
  338. "psubusb %%mm0, %%mm1 \n\t"
  339. "psubusb %%mm2, %%mm0 \n\t"
  340. "por %%mm1, %%mm0 \n\t" // |l2 - l3|
  341. "movq (%%"REG_c"), %%mm3 \n\t" // line 5
  342. "movq (%%"REG_c", %1), %%mm4 \n\t" // line 6
  343. "movq %%mm3, %%mm5 \n\t" // line 5
  344. "psubusb %%mm4, %%mm3 \n\t"
  345. "psubusb %%mm5, %%mm4 \n\t"
  346. "por %%mm4, %%mm3 \n\t" // |l5 - l6|
  347. PAVGB(%%mm3, %%mm0) // (|l2 - l3| + |l5 - l6|)/2
  348. "movq %%mm2, %%mm1 \n\t" // line 4
  349. "psubusb %%mm5, %%mm2 \n\t"
  350. "movq %%mm2, %%mm4 \n\t"
  351. "pcmpeqb %%mm7, %%mm2 \n\t" // (l4 - l5) <= 0 ? -1 : 0
  352. "psubusb %%mm1, %%mm5 \n\t"
  353. "por %%mm5, %%mm4 \n\t" // |l4 - l5|
  354. "psubusb %%mm0, %%mm4 \n\t" //d = MAX(0, |l4-l5| - (|l2-l3| + |l5-l6|)/2)
  355. "movq %%mm4, %%mm3 \n\t" // d
  356. "movq %2, %%mm0 \n\t"
  357. "paddusb %%mm0, %%mm0 \n\t"
  358. "psubusb %%mm0, %%mm4 \n\t"
  359. "pcmpeqb %%mm7, %%mm4 \n\t" // d <= QP ? -1 : 0
  360. "psubusb "MANGLE(b01)", %%mm3 \n\t"
  361. "pand %%mm4, %%mm3 \n\t" // d <= QP ? d : 0
  362. PAVGB(%%mm7, %%mm3) // d/2
  363. "movq %%mm3, %%mm1 \n\t" // d/2
  364. PAVGB(%%mm7, %%mm3) // d/4
  365. PAVGB(%%mm1, %%mm3) // 3*d/8
  366. "movq (%0, %1, 4), %%mm0 \n\t" // line 4
  367. "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
  368. "psubusb %%mm3, %%mm0 \n\t"
  369. "pxor %%mm2, %%mm0 \n\t"
  370. "movq %%mm0, (%0, %1, 4) \n\t" // line 4
  371. "movq (%%"REG_c"), %%mm0 \n\t" // line 5
  372. "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
  373. "paddusb %%mm3, %%mm0 \n\t"
  374. "pxor %%mm2, %%mm0 \n\t"
  375. "movq %%mm0, (%%"REG_c") \n\t" // line 5
  376. PAVGB(%%mm7, %%mm1) // d/4
  377. "movq (%%"REG_a", %1, 2), %%mm0 \n\t" // line 3
  378. "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l4-1 : l4
  379. "psubusb %%mm1, %%mm0 \n\t"
  380. "pxor %%mm2, %%mm0 \n\t"
  381. "movq %%mm0, (%%"REG_a", %1, 2) \n\t" // line 3
  382. "movq (%%"REG_c", %1), %%mm0 \n\t" // line 6
  383. "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l5-1 : l5
  384. "paddusb %%mm1, %%mm0 \n\t"
  385. "pxor %%mm2, %%mm0 \n\t"
  386. "movq %%mm0, (%%"REG_c", %1) \n\t" // line 6
  387. PAVGB(%%mm7, %%mm1) // d/8
  388. "movq (%%"REG_a", %1), %%mm0 \n\t" // line 2
  389. "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l2-1 : l2
  390. "psubusb %%mm1, %%mm0 \n\t"
  391. "pxor %%mm2, %%mm0 \n\t"
  392. "movq %%mm0, (%%"REG_a", %1) \n\t" // line 2
  393. "movq (%%"REG_c", %1, 2), %%mm0 \n\t" // line 7
  394. "pxor %%mm2, %%mm0 \n\t" //(l4 - l5) <= 0 ? -l7-1 : l7
  395. "paddusb %%mm1, %%mm0 \n\t"
  396. "pxor %%mm2, %%mm0 \n\t"
  397. "movq %%mm0, (%%"REG_c", %1, 2) \n\t" // line 7
  398. :
  399. : "r" (src), "r" ((x86_reg)stride), "m" (co->pQPb)
  400. : "%"REG_a, "%"REG_c
  401. );
  402. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  403. const int l1= stride;
  404. const int l2= stride + l1;
  405. const int l3= stride + l2;
  406. const int l4= stride + l3;
  407. const int l5= stride + l4;
  408. const int l6= stride + l5;
  409. const int l7= stride + l6;
  410. // const int l8= stride + l7;
  411. // const int l9= stride + l8;
  412. int x;
  413. src+= stride*3;
  414. for(x=0; x<BLOCK_SIZE; x++){
  415. int a= src[l3] - src[l4];
  416. int b= src[l4] - src[l5];
  417. int c= src[l5] - src[l6];
  418. int d= FFABS(b) - ((FFABS(a) + FFABS(c))>>1);
  419. d= FFMAX(d, 0);
  420. if(d < co->QP*2){
  421. int v = d * FFSIGN(-b);
  422. src[l2] +=v>>3;
  423. src[l3] +=v>>2;
  424. src[l4] +=(3*v)>>3;
  425. src[l5] -=(3*v)>>3;
  426. src[l6] -=v>>2;
  427. src[l7] -=v>>3;
  428. }
  429. src++;
  430. }
  431. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  432. }
  433. #if !HAVE_ALTIVEC
  434. static inline void RENAME(doVertDefFilter)(uint8_t src[], int stride, PPContext *c)
  435. {
  436. #if HAVE_MMX2 || HAVE_AMD3DNOW
  437. /*
  438. uint8_t tmp[16];
  439. const int l1= stride;
  440. const int l2= stride + l1;
  441. const int l3= stride + l2;
  442. const int l4= (int)tmp - (int)src - stride*3;
  443. const int l5= (int)tmp - (int)src - stride*3 + 8;
  444. const int l6= stride*3 + l3;
  445. const int l7= stride + l6;
  446. const int l8= stride + l7;
  447. memcpy(tmp, src+stride*7, 8);
  448. memcpy(tmp+8, src+stride*8, 8);
  449. */
  450. src+= stride*4;
  451. __asm__ volatile(
  452. #if 0 //slightly more accurate and slightly slower
  453. "pxor %%mm7, %%mm7 \n\t" // 0
  454. "lea (%0, %1), %%"REG_a" \n\t"
  455. "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
  456. // 0 1 2 3 4 5 6 7
  457. // %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 ecx+%1 ecx+2%1
  458. // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1
  459. "movq (%0, %1, 2), %%mm0 \n\t" // l2
  460. "movq (%0), %%mm1 \n\t" // l0
  461. "movq %%mm0, %%mm2 \n\t" // l2
  462. PAVGB(%%mm7, %%mm0) // ~l2/2
  463. PAVGB(%%mm1, %%mm0) // ~(l2 + 2l0)/4
  464. PAVGB(%%mm2, %%mm0) // ~(5l2 + 2l0)/8
  465. "movq (%%"REG_a"), %%mm1 \n\t" // l1
  466. "movq (%%"REG_a", %1, 2), %%mm3 \n\t" // l3
  467. "movq %%mm1, %%mm4 \n\t" // l1
  468. PAVGB(%%mm7, %%mm1) // ~l1/2
  469. PAVGB(%%mm3, %%mm1) // ~(l1 + 2l3)/4
  470. PAVGB(%%mm4, %%mm1) // ~(5l1 + 2l3)/8
  471. "movq %%mm0, %%mm4 \n\t" // ~(5l2 + 2l0)/8
  472. "psubusb %%mm1, %%mm0 \n\t"
  473. "psubusb %%mm4, %%mm1 \n\t"
  474. "por %%mm0, %%mm1 \n\t" // ~|2l0 - 5l1 + 5l2 - 2l3|/8
  475. // mm1= |lenergy|, mm2= l2, mm3= l3, mm7=0
  476. "movq (%0, %1, 4), %%mm0 \n\t" // l4
  477. "movq %%mm0, %%mm4 \n\t" // l4
  478. PAVGB(%%mm7, %%mm0) // ~l4/2
  479. PAVGB(%%mm2, %%mm0) // ~(l4 + 2l2)/4
  480. PAVGB(%%mm4, %%mm0) // ~(5l4 + 2l2)/8
  481. "movq (%%"REG_c"), %%mm2 \n\t" // l5
  482. "movq %%mm3, %%mm5 \n\t" // l3
  483. PAVGB(%%mm7, %%mm3) // ~l3/2
  484. PAVGB(%%mm2, %%mm3) // ~(l3 + 2l5)/4
  485. PAVGB(%%mm5, %%mm3) // ~(5l3 + 2l5)/8
  486. "movq %%mm0, %%mm6 \n\t" // ~(5l4 + 2l2)/8
  487. "psubusb %%mm3, %%mm0 \n\t"
  488. "psubusb %%mm6, %%mm3 \n\t"
  489. "por %%mm0, %%mm3 \n\t" // ~|2l2 - 5l3 + 5l4 - 2l5|/8
  490. "pcmpeqb %%mm7, %%mm0 \n\t" // SIGN(2l2 - 5l3 + 5l4 - 2l5)
  491. // mm0= SIGN(menergy), mm1= |lenergy|, mm2= l5, mm3= |menergy|, mm4=l4, mm5= l3, mm7=0
  492. "movq (%%"REG_c", %1), %%mm6 \n\t" // l6
  493. "movq %%mm6, %%mm5 \n\t" // l6
  494. PAVGB(%%mm7, %%mm6) // ~l6/2
  495. PAVGB(%%mm4, %%mm6) // ~(l6 + 2l4)/4
  496. PAVGB(%%mm5, %%mm6) // ~(5l6 + 2l4)/8
  497. "movq (%%"REG_c", %1, 2), %%mm5 \n\t" // l7
  498. "movq %%mm2, %%mm4 \n\t" // l5
  499. PAVGB(%%mm7, %%mm2) // ~l5/2
  500. PAVGB(%%mm5, %%mm2) // ~(l5 + 2l7)/4
  501. PAVGB(%%mm4, %%mm2) // ~(5l5 + 2l7)/8
  502. "movq %%mm6, %%mm4 \n\t" // ~(5l6 + 2l4)/8
  503. "psubusb %%mm2, %%mm6 \n\t"
  504. "psubusb %%mm4, %%mm2 \n\t"
  505. "por %%mm6, %%mm2 \n\t" // ~|2l4 - 5l5 + 5l6 - 2l7|/8
  506. // mm0= SIGN(menergy), mm1= |lenergy|/8, mm2= |renergy|/8, mm3= |menergy|/8, mm7=0
  507. PMINUB(%%mm2, %%mm1, %%mm4) // MIN(|lenergy|,|renergy|)/8
  508. "movq %2, %%mm4 \n\t" // QP //FIXME QP+1 ?
  509. "paddusb "MANGLE(b01)", %%mm4 \n\t"
  510. "pcmpgtb %%mm3, %%mm4 \n\t" // |menergy|/8 < QP
  511. "psubusb %%mm1, %%mm3 \n\t" // d=|menergy|/8-MIN(|lenergy|,|renergy|)/8
  512. "pand %%mm4, %%mm3 \n\t"
  513. "movq %%mm3, %%mm1 \n\t"
  514. // "psubusb "MANGLE(b01)", %%mm3 \n\t"
  515. PAVGB(%%mm7, %%mm3)
  516. PAVGB(%%mm7, %%mm3)
  517. "paddusb %%mm1, %%mm3 \n\t"
  518. // "paddusb "MANGLE(b01)", %%mm3 \n\t"
  519. "movq (%%"REG_a", %1, 2), %%mm6 \n\t" //l3
  520. "movq (%0, %1, 4), %%mm5 \n\t" //l4
  521. "movq (%0, %1, 4), %%mm4 \n\t" //l4
  522. "psubusb %%mm6, %%mm5 \n\t"
  523. "psubusb %%mm4, %%mm6 \n\t"
  524. "por %%mm6, %%mm5 \n\t" // |l3-l4|
  525. "pcmpeqb %%mm7, %%mm6 \n\t" // SIGN(l3-l4)
  526. "pxor %%mm6, %%mm0 \n\t"
  527. "pand %%mm0, %%mm3 \n\t"
  528. PMINUB(%%mm5, %%mm3, %%mm0)
  529. "psubusb "MANGLE(b01)", %%mm3 \n\t"
  530. PAVGB(%%mm7, %%mm3)
  531. "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
  532. "movq (%0, %1, 4), %%mm2 \n\t"
  533. "pxor %%mm6, %%mm0 \n\t"
  534. "pxor %%mm6, %%mm2 \n\t"
  535. "psubb %%mm3, %%mm0 \n\t"
  536. "paddb %%mm3, %%mm2 \n\t"
  537. "pxor %%mm6, %%mm0 \n\t"
  538. "pxor %%mm6, %%mm2 \n\t"
  539. "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
  540. "movq %%mm2, (%0, %1, 4) \n\t"
  541. #endif //0
  542. "lea (%0, %1), %%"REG_a" \n\t"
  543. "pcmpeqb %%mm6, %%mm6 \n\t" // -1
  544. // 0 1 2 3 4 5 6 7
  545. // %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 ecx+%1 ecx+2%1
  546. // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1
  547. "movq (%%"REG_a", %1, 2), %%mm1 \n\t" // l3
  548. "movq (%0, %1, 4), %%mm0 \n\t" // l4
  549. "pxor %%mm6, %%mm1 \n\t" // -l3-1
  550. PAVGB(%%mm1, %%mm0) // -q+128 = (l4-l3+256)/2
  551. // mm1=-l3-1, mm0=128-q
  552. "movq (%%"REG_a", %1, 4), %%mm2 \n\t" // l5
  553. "movq (%%"REG_a", %1), %%mm3 \n\t" // l2
  554. "pxor %%mm6, %%mm2 \n\t" // -l5-1
  555. "movq %%mm2, %%mm5 \n\t" // -l5-1
  556. "movq "MANGLE(b80)", %%mm4 \n\t" // 128
  557. "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
  558. PAVGB(%%mm3, %%mm2) // (l2-l5+256)/2
  559. PAVGB(%%mm0, %%mm4) // ~(l4-l3)/4 + 128
  560. PAVGB(%%mm2, %%mm4) // ~(l2-l5)/4 +(l4-l3)/8 + 128
  561. PAVGB(%%mm0, %%mm4) // ~(l2-l5)/8 +5(l4-l3)/16 + 128
  562. // mm1=-l3-1, mm0=128-q, mm3=l2, mm4=menergy/16 + 128, mm5= -l5-1
  563. "movq (%%"REG_a"), %%mm2 \n\t" // l1
  564. "pxor %%mm6, %%mm2 \n\t" // -l1-1
  565. PAVGB(%%mm3, %%mm2) // (l2-l1+256)/2
  566. PAVGB((%0), %%mm1) // (l0-l3+256)/2
  567. "movq "MANGLE(b80)", %%mm3 \n\t" // 128
  568. PAVGB(%%mm2, %%mm3) // ~(l2-l1)/4 + 128
  569. PAVGB(%%mm1, %%mm3) // ~(l0-l3)/4 +(l2-l1)/8 + 128
  570. PAVGB(%%mm2, %%mm3) // ~(l0-l3)/8 +5(l2-l1)/16 + 128
  571. // mm0=128-q, mm3=lenergy/16 + 128, mm4= menergy/16 + 128, mm5= -l5-1
  572. PAVGB((%%REGc, %1), %%mm5) // (l6-l5+256)/2
  573. "movq (%%"REG_c", %1, 2), %%mm1 \n\t" // l7
  574. "pxor %%mm6, %%mm1 \n\t" // -l7-1
  575. PAVGB((%0, %1, 4), %%mm1) // (l4-l7+256)/2
  576. "movq "MANGLE(b80)", %%mm2 \n\t" // 128
  577. PAVGB(%%mm5, %%mm2) // ~(l6-l5)/4 + 128
  578. PAVGB(%%mm1, %%mm2) // ~(l4-l7)/4 +(l6-l5)/8 + 128
  579. PAVGB(%%mm5, %%mm2) // ~(l4-l7)/8 +5(l6-l5)/16 + 128
  580. // mm0=128-q, mm2=renergy/16 + 128, mm3=lenergy/16 + 128, mm4= menergy/16 + 128
  581. "movq "MANGLE(b00)", %%mm1 \n\t" // 0
  582. "movq "MANGLE(b00)", %%mm5 \n\t" // 0
  583. "psubb %%mm2, %%mm1 \n\t" // 128 - renergy/16
  584. "psubb %%mm3, %%mm5 \n\t" // 128 - lenergy/16
  585. PMAXUB(%%mm1, %%mm2) // 128 + |renergy/16|
  586. PMAXUB(%%mm5, %%mm3) // 128 + |lenergy/16|
  587. PMINUB(%%mm2, %%mm3, %%mm1) // 128 + MIN(|lenergy|,|renergy|)/16
  588. // mm0=128-q, mm3=128 + MIN(|lenergy|,|renergy|)/16, mm4= menergy/16 + 128
  589. "movq "MANGLE(b00)", %%mm7 \n\t" // 0
  590. "movq %2, %%mm2 \n\t" // QP
  591. PAVGB(%%mm6, %%mm2) // 128 + QP/2
  592. "psubb %%mm6, %%mm2 \n\t"
  593. "movq %%mm4, %%mm1 \n\t"
  594. "pcmpgtb %%mm7, %%mm1 \n\t" // SIGN(menergy)
  595. "pxor %%mm1, %%mm4 \n\t"
  596. "psubb %%mm1, %%mm4 \n\t" // 128 + |menergy|/16
  597. "pcmpgtb %%mm4, %%mm2 \n\t" // |menergy|/16 < QP/2
  598. "psubusb %%mm3, %%mm4 \n\t" //d=|menergy|/16 - MIN(|lenergy|,|renergy|)/16
  599. // mm0=128-q, mm1= SIGN(menergy), mm2= |menergy|/16 < QP/2, mm4= d/16
  600. "movq %%mm4, %%mm3 \n\t" // d
  601. "psubusb "MANGLE(b01)", %%mm4 \n\t"
  602. PAVGB(%%mm7, %%mm4) // d/32
  603. PAVGB(%%mm7, %%mm4) // (d + 32)/64
  604. "paddb %%mm3, %%mm4 \n\t" // 5d/64
  605. "pand %%mm2, %%mm4 \n\t"
  606. "movq "MANGLE(b80)", %%mm5 \n\t" // 128
  607. "psubb %%mm0, %%mm5 \n\t" // q
  608. "paddsb %%mm6, %%mm5 \n\t" // fix bad rounding
  609. "pcmpgtb %%mm5, %%mm7 \n\t" // SIGN(q)
  610. "pxor %%mm7, %%mm5 \n\t"
  611. PMINUB(%%mm5, %%mm4, %%mm3) // MIN(|q|, 5d/64)
  612. "pxor %%mm1, %%mm7 \n\t" // SIGN(d*q)
  613. "pand %%mm7, %%mm4 \n\t"
  614. "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
  615. "movq (%0, %1, 4), %%mm2 \n\t"
  616. "pxor %%mm1, %%mm0 \n\t"
  617. "pxor %%mm1, %%mm2 \n\t"
  618. "paddb %%mm4, %%mm0 \n\t"
  619. "psubb %%mm4, %%mm2 \n\t"
  620. "pxor %%mm1, %%mm0 \n\t"
  621. "pxor %%mm1, %%mm2 \n\t"
  622. "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
  623. "movq %%mm2, (%0, %1, 4) \n\t"
  624. :
  625. : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb)
  626. : "%"REG_a, "%"REG_c
  627. );
  628. /*
  629. {
  630. int x;
  631. src-= stride;
  632. for(x=0; x<BLOCK_SIZE; x++){
  633. const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
  634. if(FFABS(middleEnergy)< 8*QP){
  635. const int q=(src[l4] - src[l5])/2;
  636. const int leftEnergy= 5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
  637. const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
  638. int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
  639. d= FFMAX(d, 0);
  640. d= (5*d + 32) >> 6;
  641. d*= FFSIGN(-middleEnergy);
  642. if(q>0){
  643. d= d<0 ? 0 : d;
  644. d= d>q ? q : d;
  645. }else{
  646. d= d>0 ? 0 : d;
  647. d= d<q ? q : d;
  648. }
  649. src[l4]-= d;
  650. src[l5]+= d;
  651. }
  652. src++;
  653. }
  654. src-=8;
  655. for(x=0; x<8; x++){
  656. int y;
  657. for(y=4; y<6; y++){
  658. int d= src[x+y*stride] - tmp[x+(y-4)*8];
  659. int ad= FFABS(d);
  660. static int max=0;
  661. static int sum=0;
  662. static int num=0;
  663. static int bias=0;
  664. if(max<ad) max=ad;
  665. sum+= ad>3 ? 1 : 0;
  666. if(ad>3){
  667. src[0] = src[7] = src[stride*7] = src[(stride+1)*7]=255;
  668. }
  669. if(y==4) bias+=d;
  670. num++;
  671. if(num%1000000 == 0){
  672. av_log(c, AV_LOG_INFO, " %d %d %d %d\n", num, sum, max, bias);
  673. }
  674. }
  675. }
  676. }
  677. */
  678. #elif HAVE_MMX
  679. src+= stride*4;
  680. __asm__ volatile(
  681. "pxor %%mm7, %%mm7 \n\t"
  682. "lea -40(%%"REG_SP"), %%"REG_c" \n\t" // make space for 4 8-byte vars
  683. "and "ALIGN_MASK", %%"REG_c" \n\t" // align
  684. // 0 1 2 3 4 5 6 7
  685. // %0 %0+%1 %0+2%1 eax+2%1 %0+4%1 eax+4%1 edx+%1 edx+2%1
  686. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1
  687. "movq (%0), %%mm0 \n\t"
  688. "movq %%mm0, %%mm1 \n\t"
  689. "punpcklbw %%mm7, %%mm0 \n\t" // low part of line 0
  690. "punpckhbw %%mm7, %%mm1 \n\t" // high part of line 0
  691. "movq (%0, %1), %%mm2 \n\t"
  692. "lea (%0, %1, 2), %%"REG_a" \n\t"
  693. "movq %%mm2, %%mm3 \n\t"
  694. "punpcklbw %%mm7, %%mm2 \n\t" // low part of line 1
  695. "punpckhbw %%mm7, %%mm3 \n\t" // high part of line 1
  696. "movq (%%"REG_a"), %%mm4 \n\t"
  697. "movq %%mm4, %%mm5 \n\t"
  698. "punpcklbw %%mm7, %%mm4 \n\t" // low part of line 2
  699. "punpckhbw %%mm7, %%mm5 \n\t" // high part of line 2
  700. "paddw %%mm0, %%mm0 \n\t" // 2L0
  701. "paddw %%mm1, %%mm1 \n\t" // 2H0
  702. "psubw %%mm4, %%mm2 \n\t" // L1 - L2
  703. "psubw %%mm5, %%mm3 \n\t" // H1 - H2
  704. "psubw %%mm2, %%mm0 \n\t" // 2L0 - L1 + L2
  705. "psubw %%mm3, %%mm1 \n\t" // 2H0 - H1 + H2
  706. "psllw $2, %%mm2 \n\t" // 4L1 - 4L2
  707. "psllw $2, %%mm3 \n\t" // 4H1 - 4H2
  708. "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2
  709. "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2
  710. "movq (%%"REG_a", %1), %%mm2 \n\t"
  711. "movq %%mm2, %%mm3 \n\t"
  712. "punpcklbw %%mm7, %%mm2 \n\t" // L3
  713. "punpckhbw %%mm7, %%mm3 \n\t" // H3
  714. "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - L3
  715. "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - H3
  716. "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
  717. "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
  718. "movq %%mm0, (%%"REG_c") \n\t" // 2L0 - 5L1 + 5L2 - 2L3
  719. "movq %%mm1, 8(%%"REG_c") \n\t" // 2H0 - 5H1 + 5H2 - 2H3
  720. "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
  721. "movq %%mm0, %%mm1 \n\t"
  722. "punpcklbw %%mm7, %%mm0 \n\t" // L4
  723. "punpckhbw %%mm7, %%mm1 \n\t" // H4
  724. "psubw %%mm0, %%mm2 \n\t" // L3 - L4
  725. "psubw %%mm1, %%mm3 \n\t" // H3 - H4
  726. "movq %%mm2, 16(%%"REG_c") \n\t" // L3 - L4
  727. "movq %%mm3, 24(%%"REG_c") \n\t" // H3 - H4
  728. "paddw %%mm4, %%mm4 \n\t" // 2L2
  729. "paddw %%mm5, %%mm5 \n\t" // 2H2
  730. "psubw %%mm2, %%mm4 \n\t" // 2L2 - L3 + L4
  731. "psubw %%mm3, %%mm5 \n\t" // 2H2 - H3 + H4
  732. "lea (%%"REG_a", %1), %0 \n\t"
  733. "psllw $2, %%mm2 \n\t" // 4L3 - 4L4
  734. "psllw $2, %%mm3 \n\t" // 4H3 - 4H4
  735. "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4
  736. "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4
  737. //50 opcodes so far
  738. "movq (%0, %1, 2), %%mm2 \n\t"
  739. "movq %%mm2, %%mm3 \n\t"
  740. "punpcklbw %%mm7, %%mm2 \n\t" // L5
  741. "punpckhbw %%mm7, %%mm3 \n\t" // H5
  742. "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - L5
  743. "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - H5
  744. "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - 2L5
  745. "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - 2H5
  746. "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
  747. "punpcklbw %%mm7, %%mm6 \n\t" // L6
  748. "psubw %%mm6, %%mm2 \n\t" // L5 - L6
  749. "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
  750. "punpckhbw %%mm7, %%mm6 \n\t" // H6
  751. "psubw %%mm6, %%mm3 \n\t" // H5 - H6
  752. "paddw %%mm0, %%mm0 \n\t" // 2L4
  753. "paddw %%mm1, %%mm1 \n\t" // 2H4
  754. "psubw %%mm2, %%mm0 \n\t" // 2L4 - L5 + L6
  755. "psubw %%mm3, %%mm1 \n\t" // 2H4 - H5 + H6
  756. "psllw $2, %%mm2 \n\t" // 4L5 - 4L6
  757. "psllw $2, %%mm3 \n\t" // 4H5 - 4H6
  758. "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6
  759. "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6
  760. "movq (%0, %1, 4), %%mm2 \n\t"
  761. "movq %%mm2, %%mm3 \n\t"
  762. "punpcklbw %%mm7, %%mm2 \n\t" // L7
  763. "punpckhbw %%mm7, %%mm3 \n\t" // H7
  764. "paddw %%mm2, %%mm2 \n\t" // 2L7
  765. "paddw %%mm3, %%mm3 \n\t" // 2H7
  766. "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6 - 2L7
  767. "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6 - 2H7
  768. "movq (%%"REG_c"), %%mm2 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
  769. "movq 8(%%"REG_c"), %%mm3 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
  770. #if HAVE_MMX2
  771. "movq %%mm7, %%mm6 \n\t" // 0
  772. "psubw %%mm0, %%mm6 \n\t"
  773. "pmaxsw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
  774. "movq %%mm7, %%mm6 \n\t" // 0
  775. "psubw %%mm1, %%mm6 \n\t"
  776. "pmaxsw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
  777. "movq %%mm7, %%mm6 \n\t" // 0
  778. "psubw %%mm2, %%mm6 \n\t"
  779. "pmaxsw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
  780. "movq %%mm7, %%mm6 \n\t" // 0
  781. "psubw %%mm3, %%mm6 \n\t"
  782. "pmaxsw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
  783. #else
  784. "movq %%mm7, %%mm6 \n\t" // 0
  785. "pcmpgtw %%mm0, %%mm6 \n\t"
  786. "pxor %%mm6, %%mm0 \n\t"
  787. "psubw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
  788. "movq %%mm7, %%mm6 \n\t" // 0
  789. "pcmpgtw %%mm1, %%mm6 \n\t"
  790. "pxor %%mm6, %%mm1 \n\t"
  791. "psubw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
  792. "movq %%mm7, %%mm6 \n\t" // 0
  793. "pcmpgtw %%mm2, %%mm6 \n\t"
  794. "pxor %%mm6, %%mm2 \n\t"
  795. "psubw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
  796. "movq %%mm7, %%mm6 \n\t" // 0
  797. "pcmpgtw %%mm3, %%mm6 \n\t"
  798. "pxor %%mm6, %%mm3 \n\t"
  799. "psubw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
  800. #endif
  801. #if HAVE_MMX2
  802. "pminsw %%mm2, %%mm0 \n\t"
  803. "pminsw %%mm3, %%mm1 \n\t"
  804. #else
  805. "movq %%mm0, %%mm6 \n\t"
  806. "psubusw %%mm2, %%mm6 \n\t"
  807. "psubw %%mm6, %%mm0 \n\t"
  808. "movq %%mm1, %%mm6 \n\t"
  809. "psubusw %%mm3, %%mm6 \n\t"
  810. "psubw %%mm6, %%mm1 \n\t"
  811. #endif
  812. "movd %2, %%mm2 \n\t" // QP
  813. "punpcklbw %%mm7, %%mm2 \n\t"
  814. "movq %%mm7, %%mm6 \n\t" // 0
  815. "pcmpgtw %%mm4, %%mm6 \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
  816. "pxor %%mm6, %%mm4 \n\t"
  817. "psubw %%mm6, %%mm4 \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
  818. "pcmpgtw %%mm5, %%mm7 \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
  819. "pxor %%mm7, %%mm5 \n\t"
  820. "psubw %%mm7, %%mm5 \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
  821. // 100 opcodes
  822. "psllw $3, %%mm2 \n\t" // 8QP
  823. "movq %%mm2, %%mm3 \n\t" // 8QP
  824. "pcmpgtw %%mm4, %%mm2 \n\t"
  825. "pcmpgtw %%mm5, %%mm3 \n\t"
  826. "pand %%mm2, %%mm4 \n\t"
  827. "pand %%mm3, %%mm5 \n\t"
  828. "psubusw %%mm0, %%mm4 \n\t" // hd
  829. "psubusw %%mm1, %%mm5 \n\t" // ld
  830. "movq "MANGLE(w05)", %%mm2 \n\t" // 5
  831. "pmullw %%mm2, %%mm4 \n\t"
  832. "pmullw %%mm2, %%mm5 \n\t"
  833. "movq "MANGLE(w20)", %%mm2 \n\t" // 32
  834. "paddw %%mm2, %%mm4 \n\t"
  835. "paddw %%mm2, %%mm5 \n\t"
  836. "psrlw $6, %%mm4 \n\t"
  837. "psrlw $6, %%mm5 \n\t"
  838. "movq 16(%%"REG_c"), %%mm0 \n\t" // L3 - L4
  839. "movq 24(%%"REG_c"), %%mm1 \n\t" // H3 - H4
  840. "pxor %%mm2, %%mm2 \n\t"
  841. "pxor %%mm3, %%mm3 \n\t"
  842. "pcmpgtw %%mm0, %%mm2 \n\t" // sign (L3-L4)
  843. "pcmpgtw %%mm1, %%mm3 \n\t" // sign (H3-H4)
  844. "pxor %%mm2, %%mm0 \n\t"
  845. "pxor %%mm3, %%mm1 \n\t"
  846. "psubw %%mm2, %%mm0 \n\t" // |L3-L4|
  847. "psubw %%mm3, %%mm1 \n\t" // |H3-H4|
  848. "psrlw $1, %%mm0 \n\t" // |L3 - L4|/2
  849. "psrlw $1, %%mm1 \n\t" // |H3 - H4|/2
  850. "pxor %%mm6, %%mm2 \n\t"
  851. "pxor %%mm7, %%mm3 \n\t"
  852. "pand %%mm2, %%mm4 \n\t"
  853. "pand %%mm3, %%mm5 \n\t"
  854. #if HAVE_MMX2
  855. "pminsw %%mm0, %%mm4 \n\t"
  856. "pminsw %%mm1, %%mm5 \n\t"
  857. #else
  858. "movq %%mm4, %%mm2 \n\t"
  859. "psubusw %%mm0, %%mm2 \n\t"
  860. "psubw %%mm2, %%mm4 \n\t"
  861. "movq %%mm5, %%mm2 \n\t"
  862. "psubusw %%mm1, %%mm2 \n\t"
  863. "psubw %%mm2, %%mm5 \n\t"
  864. #endif
  865. "pxor %%mm6, %%mm4 \n\t"
  866. "pxor %%mm7, %%mm5 \n\t"
  867. "psubw %%mm6, %%mm4 \n\t"
  868. "psubw %%mm7, %%mm5 \n\t"
  869. "packsswb %%mm5, %%mm4 \n\t"
  870. "movq (%0), %%mm0 \n\t"
  871. "paddb %%mm4, %%mm0 \n\t"
  872. "movq %%mm0, (%0) \n\t"
  873. "movq (%0, %1), %%mm0 \n\t"
  874. "psubb %%mm4, %%mm0 \n\t"
  875. "movq %%mm0, (%0, %1) \n\t"
  876. : "+r" (src)
  877. : "r" ((x86_reg)stride), "m" (c->pQPb)
  878. : "%"REG_a, "%"REG_c
  879. );
  880. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  881. const int l1= stride;
  882. const int l2= stride + l1;
  883. const int l3= stride + l2;
  884. const int l4= stride + l3;
  885. const int l5= stride + l4;
  886. const int l6= stride + l5;
  887. const int l7= stride + l6;
  888. const int l8= stride + l7;
  889. // const int l9= stride + l8;
  890. int x;
  891. src+= stride*3;
  892. for(x=0; x<BLOCK_SIZE; x++){
  893. const int middleEnergy= 5*(src[l5] - src[l4]) + 2*(src[l3] - src[l6]);
  894. if(FFABS(middleEnergy) < 8*c->QP){
  895. const int q=(src[l4] - src[l5])/2;
  896. const int leftEnergy= 5*(src[l3] - src[l2]) + 2*(src[l1] - src[l4]);
  897. const int rightEnergy= 5*(src[l7] - src[l6]) + 2*(src[l5] - src[l8]);
  898. int d= FFABS(middleEnergy) - FFMIN( FFABS(leftEnergy), FFABS(rightEnergy) );
  899. d= FFMAX(d, 0);
  900. d= (5*d + 32) >> 6;
  901. d*= FFSIGN(-middleEnergy);
  902. if(q>0){
  903. d= d<0 ? 0 : d;
  904. d= d>q ? q : d;
  905. }else{
  906. d= d>0 ? 0 : d;
  907. d= d<q ? q : d;
  908. }
  909. src[l4]-= d;
  910. src[l5]+= d;
  911. }
  912. src++;
  913. }
  914. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  915. }
  916. #endif //HAVE_ALTIVEC
  917. #if !HAVE_ALTIVEC
  918. static inline void RENAME(dering)(uint8_t src[], int stride, PPContext *c)
  919. {
  920. #if HAVE_MMX2 || HAVE_AMD3DNOW
  921. __asm__ volatile(
  922. "pxor %%mm6, %%mm6 \n\t"
  923. "pcmpeqb %%mm7, %%mm7 \n\t"
  924. "movq %2, %%mm0 \n\t"
  925. "punpcklbw %%mm6, %%mm0 \n\t"
  926. "psrlw $1, %%mm0 \n\t"
  927. "psubw %%mm7, %%mm0 \n\t"
  928. "packuswb %%mm0, %%mm0 \n\t"
  929. "movq %%mm0, %3 \n\t"
  930. "lea (%0, %1), %%"REG_a" \n\t"
  931. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  932. // 0 1 2 3 4 5 6 7 8 9
  933. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
  934. #undef REAL_FIND_MIN_MAX
  935. #undef FIND_MIN_MAX
  936. #if HAVE_MMX2
  937. #define REAL_FIND_MIN_MAX(addr)\
  938. "movq " #addr ", %%mm0 \n\t"\
  939. "pminub %%mm0, %%mm7 \n\t"\
  940. "pmaxub %%mm0, %%mm6 \n\t"
  941. #else
  942. #define REAL_FIND_MIN_MAX(addr)\
  943. "movq " #addr ", %%mm0 \n\t"\
  944. "movq %%mm7, %%mm1 \n\t"\
  945. "psubusb %%mm0, %%mm6 \n\t"\
  946. "paddb %%mm0, %%mm6 \n\t"\
  947. "psubusb %%mm0, %%mm1 \n\t"\
  948. "psubb %%mm1, %%mm7 \n\t"
  949. #endif
  950. #define FIND_MIN_MAX(addr) REAL_FIND_MIN_MAX(addr)
  951. FIND_MIN_MAX((%%REGa))
  952. FIND_MIN_MAX((%%REGa, %1))
  953. FIND_MIN_MAX((%%REGa, %1, 2))
  954. FIND_MIN_MAX((%0, %1, 4))
  955. FIND_MIN_MAX((%%REGd))
  956. FIND_MIN_MAX((%%REGd, %1))
  957. FIND_MIN_MAX((%%REGd, %1, 2))
  958. FIND_MIN_MAX((%0, %1, 8))
  959. "movq %%mm7, %%mm4 \n\t"
  960. "psrlq $8, %%mm7 \n\t"
  961. #if HAVE_MMX2
  962. "pminub %%mm4, %%mm7 \n\t" // min of pixels
  963. "pshufw $0xF9, %%mm7, %%mm4 \n\t"
  964. "pminub %%mm4, %%mm7 \n\t" // min of pixels
  965. "pshufw $0xFE, %%mm7, %%mm4 \n\t"
  966. "pminub %%mm4, %%mm7 \n\t"
  967. #else
  968. "movq %%mm7, %%mm1 \n\t"
  969. "psubusb %%mm4, %%mm1 \n\t"
  970. "psubb %%mm1, %%mm7 \n\t"
  971. "movq %%mm7, %%mm4 \n\t"
  972. "psrlq $16, %%mm7 \n\t"
  973. "movq %%mm7, %%mm1 \n\t"
  974. "psubusb %%mm4, %%mm1 \n\t"
  975. "psubb %%mm1, %%mm7 \n\t"
  976. "movq %%mm7, %%mm4 \n\t"
  977. "psrlq $32, %%mm7 \n\t"
  978. "movq %%mm7, %%mm1 \n\t"
  979. "psubusb %%mm4, %%mm1 \n\t"
  980. "psubb %%mm1, %%mm7 \n\t"
  981. #endif
  982. "movq %%mm6, %%mm4 \n\t"
  983. "psrlq $8, %%mm6 \n\t"
  984. #if HAVE_MMX2
  985. "pmaxub %%mm4, %%mm6 \n\t" // max of pixels
  986. "pshufw $0xF9, %%mm6, %%mm4 \n\t"
  987. "pmaxub %%mm4, %%mm6 \n\t"
  988. "pshufw $0xFE, %%mm6, %%mm4 \n\t"
  989. "pmaxub %%mm4, %%mm6 \n\t"
  990. #else
  991. "psubusb %%mm4, %%mm6 \n\t"
  992. "paddb %%mm4, %%mm6 \n\t"
  993. "movq %%mm6, %%mm4 \n\t"
  994. "psrlq $16, %%mm6 \n\t"
  995. "psubusb %%mm4, %%mm6 \n\t"
  996. "paddb %%mm4, %%mm6 \n\t"
  997. "movq %%mm6, %%mm4 \n\t"
  998. "psrlq $32, %%mm6 \n\t"
  999. "psubusb %%mm4, %%mm6 \n\t"
  1000. "paddb %%mm4, %%mm6 \n\t"
  1001. #endif
  1002. "movq %%mm6, %%mm0 \n\t" // max
  1003. "psubb %%mm7, %%mm6 \n\t" // max - min
  1004. "movd %%mm6, %%ecx \n\t"
  1005. "cmpb "MANGLE(deringThreshold)", %%cl \n\t"
  1006. " jb 1f \n\t"
  1007. "lea -24(%%"REG_SP"), %%"REG_c" \n\t"
  1008. "and "ALIGN_MASK", %%"REG_c" \n\t"
  1009. PAVGB(%%mm0, %%mm7) // a=(max + min)/2
  1010. "punpcklbw %%mm7, %%mm7 \n\t"
  1011. "punpcklbw %%mm7, %%mm7 \n\t"
  1012. "punpcklbw %%mm7, %%mm7 \n\t"
  1013. "movq %%mm7, (%%"REG_c") \n\t"
  1014. "movq (%0), %%mm0 \n\t" // L10
  1015. "movq %%mm0, %%mm1 \n\t" // L10
  1016. "movq %%mm0, %%mm2 \n\t" // L10
  1017. "psllq $8, %%mm1 \n\t"
  1018. "psrlq $8, %%mm2 \n\t"
  1019. "movd -4(%0), %%mm3 \n\t"
  1020. "movd 8(%0), %%mm4 \n\t"
  1021. "psrlq $24, %%mm3 \n\t"
  1022. "psllq $56, %%mm4 \n\t"
  1023. "por %%mm3, %%mm1 \n\t" // L00
  1024. "por %%mm4, %%mm2 \n\t" // L20
  1025. "movq %%mm1, %%mm3 \n\t" // L00
  1026. PAVGB(%%mm2, %%mm1) // (L20 + L00)/2
  1027. PAVGB(%%mm0, %%mm1) // (L20 + L00 + 2L10)/4
  1028. "psubusb %%mm7, %%mm0 \n\t"
  1029. "psubusb %%mm7, %%mm2 \n\t"
  1030. "psubusb %%mm7, %%mm3 \n\t"
  1031. "pcmpeqb "MANGLE(b00)", %%mm0 \n\t" // L10 > a ? 0 : -1
  1032. "pcmpeqb "MANGLE(b00)", %%mm2 \n\t" // L20 > a ? 0 : -1
  1033. "pcmpeqb "MANGLE(b00)", %%mm3 \n\t" // L00 > a ? 0 : -1
  1034. "paddb %%mm2, %%mm0 \n\t"
  1035. "paddb %%mm3, %%mm0 \n\t"
  1036. "movq (%%"REG_a"), %%mm2 \n\t" // L11
  1037. "movq %%mm2, %%mm3 \n\t" // L11
  1038. "movq %%mm2, %%mm4 \n\t" // L11
  1039. "psllq $8, %%mm3 \n\t"
  1040. "psrlq $8, %%mm4 \n\t"
  1041. "movd -4(%%"REG_a"), %%mm5 \n\t"
  1042. "movd 8(%%"REG_a"), %%mm6 \n\t"
  1043. "psrlq $24, %%mm5 \n\t"
  1044. "psllq $56, %%mm6 \n\t"
  1045. "por %%mm5, %%mm3 \n\t" // L01
  1046. "por %%mm6, %%mm4 \n\t" // L21
  1047. "movq %%mm3, %%mm5 \n\t" // L01
  1048. PAVGB(%%mm4, %%mm3) // (L21 + L01)/2
  1049. PAVGB(%%mm2, %%mm3) // (L21 + L01 + 2L11)/4
  1050. "psubusb %%mm7, %%mm2 \n\t"
  1051. "psubusb %%mm7, %%mm4 \n\t"
  1052. "psubusb %%mm7, %%mm5 \n\t"
  1053. "pcmpeqb "MANGLE(b00)", %%mm2 \n\t" // L11 > a ? 0 : -1
  1054. "pcmpeqb "MANGLE(b00)", %%mm4 \n\t" // L21 > a ? 0 : -1
  1055. "pcmpeqb "MANGLE(b00)", %%mm5 \n\t" // L01 > a ? 0 : -1
  1056. "paddb %%mm4, %%mm2 \n\t"
  1057. "paddb %%mm5, %%mm2 \n\t"
  1058. // 0, 2, 3, 1
  1059. #define REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
  1060. "movq " #src ", " #sx " \n\t" /* src[0] */\
  1061. "movq " #sx ", " #lx " \n\t" /* src[0] */\
  1062. "movq " #sx ", " #t0 " \n\t" /* src[0] */\
  1063. "psllq $8, " #lx " \n\t"\
  1064. "psrlq $8, " #t0 " \n\t"\
  1065. "movd -4" #src ", " #t1 " \n\t"\
  1066. "psrlq $24, " #t1 " \n\t"\
  1067. "por " #t1 ", " #lx " \n\t" /* src[-1] */\
  1068. "movd 8" #src ", " #t1 " \n\t"\
  1069. "psllq $56, " #t1 " \n\t"\
  1070. "por " #t1 ", " #t0 " \n\t" /* src[+1] */\
  1071. "movq " #lx ", " #t1 " \n\t" /* src[-1] */\
  1072. PAVGB(t0, lx) /* (src[-1] + src[+1])/2 */\
  1073. PAVGB(sx, lx) /* (src[-1] + 2src[0] + src[+1])/4 */\
  1074. PAVGB(lx, pplx) \
  1075. "movq " #lx ", 8(%%"REG_c") \n\t"\
  1076. "movq (%%"REG_c"), " #lx " \n\t"\
  1077. "psubusb " #lx ", " #t1 " \n\t"\
  1078. "psubusb " #lx ", " #t0 " \n\t"\
  1079. "psubusb " #lx ", " #sx " \n\t"\
  1080. "movq "MANGLE(b00)", " #lx " \n\t"\
  1081. "pcmpeqb " #lx ", " #t1 " \n\t" /* src[-1] > a ? 0 : -1*/\
  1082. "pcmpeqb " #lx ", " #t0 " \n\t" /* src[+1] > a ? 0 : -1*/\
  1083. "pcmpeqb " #lx ", " #sx " \n\t" /* src[0] > a ? 0 : -1*/\
  1084. "paddb " #t1 ", " #t0 " \n\t"\
  1085. "paddb " #t0 ", " #sx " \n\t"\
  1086. \
  1087. PAVGB(plx, pplx) /* filtered */\
  1088. "movq " #dst ", " #t0 " \n\t" /* dst */\
  1089. "movq " #t0 ", " #t1 " \n\t" /* dst */\
  1090. "psubusb %3, " #t0 " \n\t"\
  1091. "paddusb %3, " #t1 " \n\t"\
  1092. PMAXUB(t0, pplx)\
  1093. PMINUB(t1, pplx, t0)\
  1094. "paddb " #sx ", " #ppsx " \n\t"\
  1095. "paddb " #psx ", " #ppsx " \n\t"\
  1096. "#paddb "MANGLE(b02)", " #ppsx " \n\t"\
  1097. "pand "MANGLE(b08)", " #ppsx " \n\t"\
  1098. "pcmpeqb " #lx ", " #ppsx " \n\t"\
  1099. "pand " #ppsx ", " #pplx " \n\t"\
  1100. "pandn " #dst ", " #ppsx " \n\t"\
  1101. "por " #pplx ", " #ppsx " \n\t"\
  1102. "movq " #ppsx ", " #dst " \n\t"\
  1103. "movq 8(%%"REG_c"), " #lx " \n\t"
  1104. #define DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1) \
  1105. REAL_DERING_CORE(dst,src,ppsx,psx,sx,pplx,plx,lx,t0,t1)
  1106. /*
  1107. 0000000
  1108. 1111111
  1109. 1111110
  1110. 1111101
  1111. 1111100
  1112. 1111011
  1113. 1111010
  1114. 1111001
  1115. 1111000
  1116. 1110111
  1117. */
  1118. //DERING_CORE(dst ,src ,ppsx ,psx ,sx ,pplx ,plx ,lx ,t0 ,t1)
  1119. DERING_CORE((%%REGa) ,(%%REGa, %1) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
  1120. DERING_CORE((%%REGa, %1) ,(%%REGa, %1, 2),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
  1121. DERING_CORE((%%REGa, %1, 2),(%0, %1, 4) ,%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
  1122. DERING_CORE((%0, %1, 4) ,(%%REGd) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
  1123. DERING_CORE((%%REGd) ,(%%REGd, %1) ,%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
  1124. DERING_CORE((%%REGd, %1) ,(%%REGd, %1, 2),%%mm4,%%mm0,%%mm2,%%mm5,%%mm1,%%mm3,%%mm6,%%mm7)
  1125. DERING_CORE((%%REGd, %1, 2),(%0, %1, 8) ,%%mm0,%%mm2,%%mm4,%%mm1,%%mm3,%%mm5,%%mm6,%%mm7)
  1126. DERING_CORE((%0, %1, 8) ,(%%REGd, %1, 4),%%mm2,%%mm4,%%mm0,%%mm3,%%mm5,%%mm1,%%mm6,%%mm7)
  1127. "1: \n\t"
  1128. : : "r" (src), "r" ((x86_reg)stride), "m" (c->pQPb), "m"(c->pQPb2)
  1129. : "%"REG_a, "%"REG_d, "%"REG_c
  1130. );
  1131. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  1132. int y;
  1133. int min=255;
  1134. int max=0;
  1135. int avg;
  1136. uint8_t *p;
  1137. int s[10];
  1138. const int QP2= c->QP/2 + 1;
  1139. for(y=1; y<9; y++){
  1140. int x;
  1141. p= src + stride*y;
  1142. for(x=1; x<9; x++){
  1143. p++;
  1144. if(*p > max) max= *p;
  1145. if(*p < min) min= *p;
  1146. }
  1147. }
  1148. avg= (min + max + 1)>>1;
  1149. if(max - min <deringThreshold) return;
  1150. for(y=0; y<10; y++){
  1151. int t = 0;
  1152. if(src[stride*y + 0] > avg) t+= 1;
  1153. if(src[stride*y + 1] > avg) t+= 2;
  1154. if(src[stride*y + 2] > avg) t+= 4;
  1155. if(src[stride*y + 3] > avg) t+= 8;
  1156. if(src[stride*y + 4] > avg) t+= 16;
  1157. if(src[stride*y + 5] > avg) t+= 32;
  1158. if(src[stride*y + 6] > avg) t+= 64;
  1159. if(src[stride*y + 7] > avg) t+= 128;
  1160. if(src[stride*y + 8] > avg) t+= 256;
  1161. if(src[stride*y + 9] > avg) t+= 512;
  1162. t |= (~t)<<16;
  1163. t &= (t<<1) & (t>>1);
  1164. s[y] = t;
  1165. }
  1166. for(y=1; y<9; y++){
  1167. int t = s[y-1] & s[y] & s[y+1];
  1168. t|= t>>16;
  1169. s[y-1]= t;
  1170. }
  1171. for(y=1; y<9; y++){
  1172. int x;
  1173. int t = s[y-1];
  1174. p= src + stride*y;
  1175. for(x=1; x<9; x++){
  1176. p++;
  1177. if(t & (1<<x)){
  1178. int f= (*(p-stride-1)) + 2*(*(p-stride)) + (*(p-stride+1))
  1179. +2*(*(p -1)) + 4*(*p ) + 2*(*(p +1))
  1180. +(*(p+stride-1)) + 2*(*(p+stride)) + (*(p+stride+1));
  1181. f= (f + 8)>>4;
  1182. #ifdef DEBUG_DERING_THRESHOLD
  1183. __asm__ volatile("emms\n\t":);
  1184. {
  1185. static long long numPixels=0;
  1186. if(x!=1 && x!=8 && y!=1 && y!=8) numPixels++;
  1187. // if((max-min)<20 || (max-min)*QP<200)
  1188. // if((max-min)*QP < 500)
  1189. // if(max-min<QP/2)
  1190. if(max-min < 20){
  1191. static int numSkipped=0;
  1192. static int errorSum=0;
  1193. static int worstQP=0;
  1194. static int worstRange=0;
  1195. static int worstDiff=0;
  1196. int diff= (f - *p);
  1197. int absDiff= FFABS(diff);
  1198. int error= diff*diff;
  1199. if(x==1 || x==8 || y==1 || y==8) continue;
  1200. numSkipped++;
  1201. if(absDiff > worstDiff){
  1202. worstDiff= absDiff;
  1203. worstQP= QP;
  1204. worstRange= max-min;
  1205. }
  1206. errorSum+= error;
  1207. if(1024LL*1024LL*1024LL % numSkipped == 0){
  1208. av_log(c, AV_LOG_INFO, "sum:%1.3f, skip:%d, wQP:%d, "
  1209. "wRange:%d, wDiff:%d, relSkip:%1.3f\n",
  1210. (float)errorSum/numSkipped, numSkipped, worstQP, worstRange,
  1211. worstDiff, (float)numSkipped/numPixels);
  1212. }
  1213. }
  1214. }
  1215. #endif
  1216. if (*p + QP2 < f) *p= *p + QP2;
  1217. else if(*p - QP2 > f) *p= *p - QP2;
  1218. else *p=f;
  1219. }
  1220. }
  1221. }
  1222. #ifdef DEBUG_DERING_THRESHOLD
  1223. if(max-min < 20){
  1224. for(y=1; y<9; y++){
  1225. int x;
  1226. int t = 0;
  1227. p= src + stride*y;
  1228. for(x=1; x<9; x++){
  1229. p++;
  1230. *p = FFMIN(*p + 20, 255);
  1231. }
  1232. }
  1233. // src[0] = src[7]=src[stride*7]=src[stride*7 + 7]=255;
  1234. }
  1235. #endif
  1236. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  1237. }
  1238. #endif //HAVE_ALTIVEC
  1239. /**
  1240. * Deinterlaces the given block by linearly interpolating every second line.
  1241. * will be called for every 8x8 block and can read & write from line 4-15
  1242. * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
  1243. * lines 4-12 will be read into the deblocking filter and should be deinterlaced
  1244. */
  1245. static inline void RENAME(deInterlaceInterpolateLinear)(uint8_t src[], int stride)
  1246. {
  1247. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1248. src+= 4*stride;
  1249. __asm__ volatile(
  1250. "lea (%0, %1), %%"REG_a" \n\t"
  1251. "lea (%%"REG_a", %1, 4), %%"REG_c" \n\t"
  1252. // 0 1 2 3 4 5 6 7 8 9
  1253. // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %0+8%1 ecx+4%1
  1254. "movq (%0), %%mm0 \n\t"
  1255. "movq (%%"REG_a", %1), %%mm1 \n\t"
  1256. PAVGB(%%mm1, %%mm0)
  1257. "movq %%mm0, (%%"REG_a") \n\t"
  1258. "movq (%0, %1, 4), %%mm0 \n\t"
  1259. PAVGB(%%mm0, %%mm1)
  1260. "movq %%mm1, (%%"REG_a", %1, 2) \n\t"
  1261. "movq (%%"REG_c", %1), %%mm1 \n\t"
  1262. PAVGB(%%mm1, %%mm0)
  1263. "movq %%mm0, (%%"REG_c") \n\t"
  1264. "movq (%0, %1, 8), %%mm0 \n\t"
  1265. PAVGB(%%mm0, %%mm1)
  1266. "movq %%mm1, (%%"REG_c", %1, 2) \n\t"
  1267. : : "r" (src), "r" ((x86_reg)stride)
  1268. : "%"REG_a, "%"REG_c
  1269. );
  1270. #else
  1271. int a, b, x;
  1272. src+= 4*stride;
  1273. for(x=0; x<2; x++){
  1274. a= *(uint32_t*)&src[stride*0];
  1275. b= *(uint32_t*)&src[stride*2];
  1276. *(uint32_t*)&src[stride*1]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1277. a= *(uint32_t*)&src[stride*4];
  1278. *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1279. b= *(uint32_t*)&src[stride*6];
  1280. *(uint32_t*)&src[stride*5]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1281. a= *(uint32_t*)&src[stride*8];
  1282. *(uint32_t*)&src[stride*7]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1283. src += 4;
  1284. }
  1285. #endif
  1286. }
  1287. /**
  1288. * Deinterlaces the given block by cubic interpolating every second line.
  1289. * will be called for every 8x8 block and can read & write from line 4-15
  1290. * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
  1291. * lines 4-12 will be read into the deblocking filter and should be deinterlaced
  1292. * this filter will read lines 3-15 and write 7-13
  1293. */
  1294. static inline void RENAME(deInterlaceInterpolateCubic)(uint8_t src[], int stride)
  1295. {
  1296. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1297. src+= stride*3;
  1298. __asm__ volatile(
  1299. "lea (%0, %1), %%"REG_a" \n\t"
  1300. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  1301. "lea (%%"REG_d", %1, 4), %%"REG_c" \n\t"
  1302. "add %1, %%"REG_c" \n\t"
  1303. "pxor %%mm7, %%mm7 \n\t"
  1304. // 0 1 2 3 4 5 6 7 8 9 10
  1305. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
  1306. #define REAL_DEINT_CUBIC(a,b,c,d,e)\
  1307. "movq " #a ", %%mm0 \n\t"\
  1308. "movq " #b ", %%mm1 \n\t"\
  1309. "movq " #d ", %%mm2 \n\t"\
  1310. "movq " #e ", %%mm3 \n\t"\
  1311. PAVGB(%%mm2, %%mm1) /* (b+d) /2 */\
  1312. PAVGB(%%mm3, %%mm0) /* a(a+e) /2 */\
  1313. "movq %%mm0, %%mm2 \n\t"\
  1314. "punpcklbw %%mm7, %%mm0 \n\t"\
  1315. "punpckhbw %%mm7, %%mm2 \n\t"\
  1316. "movq %%mm1, %%mm3 \n\t"\
  1317. "punpcklbw %%mm7, %%mm1 \n\t"\
  1318. "punpckhbw %%mm7, %%mm3 \n\t"\
  1319. "psubw %%mm1, %%mm0 \n\t" /* L(a+e - (b+d))/2 */\
  1320. "psubw %%mm3, %%mm2 \n\t" /* H(a+e - (b+d))/2 */\
  1321. "psraw $3, %%mm0 \n\t" /* L(a+e - (b+d))/16 */\
  1322. "psraw $3, %%mm2 \n\t" /* H(a+e - (b+d))/16 */\
  1323. "psubw %%mm0, %%mm1 \n\t" /* L(9b + 9d - a - e)/16 */\
  1324. "psubw %%mm2, %%mm3 \n\t" /* H(9b + 9d - a - e)/16 */\
  1325. "packuswb %%mm3, %%mm1 \n\t"\
  1326. "movq %%mm1, " #c " \n\t"
  1327. #define DEINT_CUBIC(a,b,c,d,e) REAL_DEINT_CUBIC(a,b,c,d,e)
  1328. DEINT_CUBIC((%0) , (%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd, %1))
  1329. DEINT_CUBIC((%%REGa, %1), (%0, %1, 4) , (%%REGd) , (%%REGd, %1), (%0, %1, 8))
  1330. DEINT_CUBIC((%0, %1, 4) , (%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGc))
  1331. DEINT_CUBIC((%%REGd, %1), (%0, %1, 8) , (%%REGd, %1, 4), (%%REGc) , (%%REGc, %1, 2))
  1332. : : "r" (src), "r" ((x86_reg)stride)
  1333. : "%"REG_a, "%"REG_d, "%"REG_c
  1334. );
  1335. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  1336. int x;
  1337. src+= stride*3;
  1338. for(x=0; x<8; x++){
  1339. src[stride*3] = CLIP((-src[0] + 9*src[stride*2] + 9*src[stride*4] - src[stride*6])>>4);
  1340. src[stride*5] = CLIP((-src[stride*2] + 9*src[stride*4] + 9*src[stride*6] - src[stride*8])>>4);
  1341. src[stride*7] = CLIP((-src[stride*4] + 9*src[stride*6] + 9*src[stride*8] - src[stride*10])>>4);
  1342. src[stride*9] = CLIP((-src[stride*6] + 9*src[stride*8] + 9*src[stride*10] - src[stride*12])>>4);
  1343. src++;
  1344. }
  1345. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  1346. }
  1347. /**
  1348. * Deinterlaces the given block by filtering every second line with a (-1 4 2 4 -1) filter.
  1349. * will be called for every 8x8 block and can read & write from line 4-15
  1350. * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
  1351. * lines 4-12 will be read into the deblocking filter and should be deinterlaced
  1352. * this filter will read lines 4-13 and write 5-11
  1353. */
  1354. static inline void RENAME(deInterlaceFF)(uint8_t src[], int stride, uint8_t *tmp)
  1355. {
  1356. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1357. src+= stride*4;
  1358. __asm__ volatile(
  1359. "lea (%0, %1), %%"REG_a" \n\t"
  1360. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  1361. "pxor %%mm7, %%mm7 \n\t"
  1362. "movq (%2), %%mm0 \n\t"
  1363. // 0 1 2 3 4 5 6 7 8 9 10
  1364. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
  1365. #define REAL_DEINT_FF(a,b,c,d)\
  1366. "movq " #a ", %%mm1 \n\t"\
  1367. "movq " #b ", %%mm2 \n\t"\
  1368. "movq " #c ", %%mm3 \n\t"\
  1369. "movq " #d ", %%mm4 \n\t"\
  1370. PAVGB(%%mm3, %%mm1) \
  1371. PAVGB(%%mm4, %%mm0) \
  1372. "movq %%mm0, %%mm3 \n\t"\
  1373. "punpcklbw %%mm7, %%mm0 \n\t"\
  1374. "punpckhbw %%mm7, %%mm3 \n\t"\
  1375. "movq %%mm1, %%mm4 \n\t"\
  1376. "punpcklbw %%mm7, %%mm1 \n\t"\
  1377. "punpckhbw %%mm7, %%mm4 \n\t"\
  1378. "psllw $2, %%mm1 \n\t"\
  1379. "psllw $2, %%mm4 \n\t"\
  1380. "psubw %%mm0, %%mm1 \n\t"\
  1381. "psubw %%mm3, %%mm4 \n\t"\
  1382. "movq %%mm2, %%mm5 \n\t"\
  1383. "movq %%mm2, %%mm0 \n\t"\
  1384. "punpcklbw %%mm7, %%mm2 \n\t"\
  1385. "punpckhbw %%mm7, %%mm5 \n\t"\
  1386. "paddw %%mm2, %%mm1 \n\t"\
  1387. "paddw %%mm5, %%mm4 \n\t"\
  1388. "psraw $2, %%mm1 \n\t"\
  1389. "psraw $2, %%mm4 \n\t"\
  1390. "packuswb %%mm4, %%mm1 \n\t"\
  1391. "movq %%mm1, " #b " \n\t"\
  1392. #define DEINT_FF(a,b,c,d) REAL_DEINT_FF(a,b,c,d)
  1393. DEINT_FF((%0) , (%%REGa) , (%%REGa, %1), (%%REGa, %1, 2))
  1394. DEINT_FF((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
  1395. DEINT_FF((%0, %1, 4) , (%%REGd) , (%%REGd, %1), (%%REGd, %1, 2))
  1396. DEINT_FF((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
  1397. "movq %%mm0, (%2) \n\t"
  1398. : : "r" (src), "r" ((x86_reg)stride), "r"(tmp)
  1399. : "%"REG_a, "%"REG_d
  1400. );
  1401. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  1402. int x;
  1403. src+= stride*4;
  1404. for(x=0; x<8; x++){
  1405. int t1= tmp[x];
  1406. int t2= src[stride*1];
  1407. src[stride*1]= CLIP((-t1 + 4*src[stride*0] + 2*t2 + 4*src[stride*2] - src[stride*3] + 4)>>3);
  1408. t1= src[stride*4];
  1409. src[stride*3]= CLIP((-t2 + 4*src[stride*2] + 2*t1 + 4*src[stride*4] - src[stride*5] + 4)>>3);
  1410. t2= src[stride*6];
  1411. src[stride*5]= CLIP((-t1 + 4*src[stride*4] + 2*t2 + 4*src[stride*6] - src[stride*7] + 4)>>3);
  1412. t1= src[stride*8];
  1413. src[stride*7]= CLIP((-t2 + 4*src[stride*6] + 2*t1 + 4*src[stride*8] - src[stride*9] + 4)>>3);
  1414. tmp[x]= t1;
  1415. src++;
  1416. }
  1417. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  1418. }
  1419. /**
  1420. * Deinterlaces the given block by filtering every line with a (-1 2 6 2 -1) filter.
  1421. * will be called for every 8x8 block and can read & write from line 4-15
  1422. * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
  1423. * lines 4-12 will be read into the deblocking filter and should be deinterlaced
  1424. * this filter will read lines 4-13 and write 4-11
  1425. */
  1426. static inline void RENAME(deInterlaceL5)(uint8_t src[], int stride, uint8_t *tmp, uint8_t *tmp2)
  1427. {
  1428. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1429. src+= stride*4;
  1430. __asm__ volatile(
  1431. "lea (%0, %1), %%"REG_a" \n\t"
  1432. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  1433. "pxor %%mm7, %%mm7 \n\t"
  1434. "movq (%2), %%mm0 \n\t"
  1435. "movq (%3), %%mm1 \n\t"
  1436. // 0 1 2 3 4 5 6 7 8 9 10
  1437. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1 ecx
  1438. #define REAL_DEINT_L5(t1,t2,a,b,c)\
  1439. "movq " #a ", %%mm2 \n\t"\
  1440. "movq " #b ", %%mm3 \n\t"\
  1441. "movq " #c ", %%mm4 \n\t"\
  1442. PAVGB(t2, %%mm3) \
  1443. PAVGB(t1, %%mm4) \
  1444. "movq %%mm2, %%mm5 \n\t"\
  1445. "movq %%mm2, " #t1 " \n\t"\
  1446. "punpcklbw %%mm7, %%mm2 \n\t"\
  1447. "punpckhbw %%mm7, %%mm5 \n\t"\
  1448. "movq %%mm2, %%mm6 \n\t"\
  1449. "paddw %%mm2, %%mm2 \n\t"\
  1450. "paddw %%mm6, %%mm2 \n\t"\
  1451. "movq %%mm5, %%mm6 \n\t"\
  1452. "paddw %%mm5, %%mm5 \n\t"\
  1453. "paddw %%mm6, %%mm5 \n\t"\
  1454. "movq %%mm3, %%mm6 \n\t"\
  1455. "punpcklbw %%mm7, %%mm3 \n\t"\
  1456. "punpckhbw %%mm7, %%mm6 \n\t"\
  1457. "paddw %%mm3, %%mm3 \n\t"\
  1458. "paddw %%mm6, %%mm6 \n\t"\
  1459. "paddw %%mm3, %%mm2 \n\t"\
  1460. "paddw %%mm6, %%mm5 \n\t"\
  1461. "movq %%mm4, %%mm6 \n\t"\
  1462. "punpcklbw %%mm7, %%mm4 \n\t"\
  1463. "punpckhbw %%mm7, %%mm6 \n\t"\
  1464. "psubw %%mm4, %%mm2 \n\t"\
  1465. "psubw %%mm6, %%mm5 \n\t"\
  1466. "psraw $2, %%mm2 \n\t"\
  1467. "psraw $2, %%mm5 \n\t"\
  1468. "packuswb %%mm5, %%mm2 \n\t"\
  1469. "movq %%mm2, " #a " \n\t"\
  1470. #define DEINT_L5(t1,t2,a,b,c) REAL_DEINT_L5(t1,t2,a,b,c)
  1471. DEINT_L5(%%mm0, %%mm1, (%0) , (%%REGa) , (%%REGa, %1) )
  1472. DEINT_L5(%%mm1, %%mm0, (%%REGa) , (%%REGa, %1) , (%%REGa, %1, 2))
  1473. DEINT_L5(%%mm0, %%mm1, (%%REGa, %1) , (%%REGa, %1, 2), (%0, %1, 4) )
  1474. DEINT_L5(%%mm1, %%mm0, (%%REGa, %1, 2), (%0, %1, 4) , (%%REGd) )
  1475. DEINT_L5(%%mm0, %%mm1, (%0, %1, 4) , (%%REGd) , (%%REGd, %1) )
  1476. DEINT_L5(%%mm1, %%mm0, (%%REGd) , (%%REGd, %1) , (%%REGd, %1, 2))
  1477. DEINT_L5(%%mm0, %%mm1, (%%REGd, %1) , (%%REGd, %1, 2), (%0, %1, 8) )
  1478. DEINT_L5(%%mm1, %%mm0, (%%REGd, %1, 2), (%0, %1, 8) , (%%REGd, %1, 4))
  1479. "movq %%mm0, (%2) \n\t"
  1480. "movq %%mm1, (%3) \n\t"
  1481. : : "r" (src), "r" ((x86_reg)stride), "r"(tmp), "r"(tmp2)
  1482. : "%"REG_a, "%"REG_d
  1483. );
  1484. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  1485. int x;
  1486. src+= stride*4;
  1487. for(x=0; x<8; x++){
  1488. int t1= tmp[x];
  1489. int t2= tmp2[x];
  1490. int t3= src[0];
  1491. src[stride*0]= CLIP((-(t1 + src[stride*2]) + 2*(t2 + src[stride*1]) + 6*t3 + 4)>>3);
  1492. t1= src[stride*1];
  1493. src[stride*1]= CLIP((-(t2 + src[stride*3]) + 2*(t3 + src[stride*2]) + 6*t1 + 4)>>3);
  1494. t2= src[stride*2];
  1495. src[stride*2]= CLIP((-(t3 + src[stride*4]) + 2*(t1 + src[stride*3]) + 6*t2 + 4)>>3);
  1496. t3= src[stride*3];
  1497. src[stride*3]= CLIP((-(t1 + src[stride*5]) + 2*(t2 + src[stride*4]) + 6*t3 + 4)>>3);
  1498. t1= src[stride*4];
  1499. src[stride*4]= CLIP((-(t2 + src[stride*6]) + 2*(t3 + src[stride*5]) + 6*t1 + 4)>>3);
  1500. t2= src[stride*5];
  1501. src[stride*5]= CLIP((-(t3 + src[stride*7]) + 2*(t1 + src[stride*6]) + 6*t2 + 4)>>3);
  1502. t3= src[stride*6];
  1503. src[stride*6]= CLIP((-(t1 + src[stride*8]) + 2*(t2 + src[stride*7]) + 6*t3 + 4)>>3);
  1504. t1= src[stride*7];
  1505. src[stride*7]= CLIP((-(t2 + src[stride*9]) + 2*(t3 + src[stride*8]) + 6*t1 + 4)>>3);
  1506. tmp[x]= t3;
  1507. tmp2[x]= t1;
  1508. src++;
  1509. }
  1510. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  1511. }
  1512. /**
  1513. * Deinterlaces the given block by filtering all lines with a (1 2 1) filter.
  1514. * will be called for every 8x8 block and can read & write from line 4-15
  1515. * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
  1516. * lines 4-12 will be read into the deblocking filter and should be deinterlaced
  1517. * this filter will read lines 4-13 and write 4-11
  1518. */
  1519. static inline void RENAME(deInterlaceBlendLinear)(uint8_t src[], int stride, uint8_t *tmp)
  1520. {
  1521. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1522. src+= 4*stride;
  1523. __asm__ volatile(
  1524. "lea (%0, %1), %%"REG_a" \n\t"
  1525. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  1526. // 0 1 2 3 4 5 6 7 8 9
  1527. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
  1528. "movq (%2), %%mm0 \n\t" // L0
  1529. "movq (%%"REG_a"), %%mm1 \n\t" // L2
  1530. PAVGB(%%mm1, %%mm0) // L0+L2
  1531. "movq (%0), %%mm2 \n\t" // L1
  1532. PAVGB(%%mm2, %%mm0)
  1533. "movq %%mm0, (%0) \n\t"
  1534. "movq (%%"REG_a", %1), %%mm0 \n\t" // L3
  1535. PAVGB(%%mm0, %%mm2) // L1+L3
  1536. PAVGB(%%mm1, %%mm2) // 2L2 + L1 + L3
  1537. "movq %%mm2, (%%"REG_a") \n\t"
  1538. "movq (%%"REG_a", %1, 2), %%mm2 \n\t" // L4
  1539. PAVGB(%%mm2, %%mm1) // L2+L4
  1540. PAVGB(%%mm0, %%mm1) // 2L3 + L2 + L4
  1541. "movq %%mm1, (%%"REG_a", %1) \n\t"
  1542. "movq (%0, %1, 4), %%mm1 \n\t" // L5
  1543. PAVGB(%%mm1, %%mm0) // L3+L5
  1544. PAVGB(%%mm2, %%mm0) // 2L4 + L3 + L5
  1545. "movq %%mm0, (%%"REG_a", %1, 2) \n\t"
  1546. "movq (%%"REG_d"), %%mm0 \n\t" // L6
  1547. PAVGB(%%mm0, %%mm2) // L4+L6
  1548. PAVGB(%%mm1, %%mm2) // 2L5 + L4 + L6
  1549. "movq %%mm2, (%0, %1, 4) \n\t"
  1550. "movq (%%"REG_d", %1), %%mm2 \n\t" // L7
  1551. PAVGB(%%mm2, %%mm1) // L5+L7
  1552. PAVGB(%%mm0, %%mm1) // 2L6 + L5 + L7
  1553. "movq %%mm1, (%%"REG_d") \n\t"
  1554. "movq (%%"REG_d", %1, 2), %%mm1 \n\t" // L8
  1555. PAVGB(%%mm1, %%mm0) // L6+L8
  1556. PAVGB(%%mm2, %%mm0) // 2L7 + L6 + L8
  1557. "movq %%mm0, (%%"REG_d", %1) \n\t"
  1558. "movq (%0, %1, 8), %%mm0 \n\t" // L9
  1559. PAVGB(%%mm0, %%mm2) // L7+L9
  1560. PAVGB(%%mm1, %%mm2) // 2L8 + L7 + L9
  1561. "movq %%mm2, (%%"REG_d", %1, 2) \n\t"
  1562. "movq %%mm1, (%2) \n\t"
  1563. : : "r" (src), "r" ((x86_reg)stride), "r" (tmp)
  1564. : "%"REG_a, "%"REG_d
  1565. );
  1566. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  1567. int a, b, c, x;
  1568. src+= 4*stride;
  1569. for(x=0; x<2; x++){
  1570. a= *(uint32_t*)&tmp[stride*0];
  1571. b= *(uint32_t*)&src[stride*0];
  1572. c= *(uint32_t*)&src[stride*1];
  1573. a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
  1574. *(uint32_t*)&src[stride*0]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1575. a= *(uint32_t*)&src[stride*2];
  1576. b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
  1577. *(uint32_t*)&src[stride*1]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
  1578. b= *(uint32_t*)&src[stride*3];
  1579. c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
  1580. *(uint32_t*)&src[stride*2]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);
  1581. c= *(uint32_t*)&src[stride*4];
  1582. a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
  1583. *(uint32_t*)&src[stride*3]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1584. a= *(uint32_t*)&src[stride*5];
  1585. b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
  1586. *(uint32_t*)&src[stride*4]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
  1587. b= *(uint32_t*)&src[stride*6];
  1588. c= (b&c) + (((b^c)&0xFEFEFEFEUL)>>1);
  1589. *(uint32_t*)&src[stride*5]= (c|a) - (((c^a)&0xFEFEFEFEUL)>>1);
  1590. c= *(uint32_t*)&src[stride*7];
  1591. a= (a&c) + (((a^c)&0xFEFEFEFEUL)>>1);
  1592. *(uint32_t*)&src[stride*6]= (a|b) - (((a^b)&0xFEFEFEFEUL)>>1);
  1593. a= *(uint32_t*)&src[stride*8];
  1594. b= (a&b) + (((a^b)&0xFEFEFEFEUL)>>1);
  1595. *(uint32_t*)&src[stride*7]= (c|b) - (((c^b)&0xFEFEFEFEUL)>>1);
  1596. *(uint32_t*)&tmp[stride*0]= c;
  1597. src += 4;
  1598. tmp += 4;
  1599. }
  1600. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  1601. }
  1602. /**
  1603. * Deinterlaces the given block by applying a median filter to every second line.
  1604. * will be called for every 8x8 block and can read & write from line 4-15,
  1605. * lines 0-3 have been passed through the deblock / dering filters already, but can be read, too.
  1606. * lines 4-12 will be read into the deblocking filter and should be deinterlaced
  1607. */
  1608. static inline void RENAME(deInterlaceMedian)(uint8_t src[], int stride)
  1609. {
  1610. #if HAVE_MMX
  1611. src+= 4*stride;
  1612. #if HAVE_MMX2
  1613. __asm__ volatile(
  1614. "lea (%0, %1), %%"REG_a" \n\t"
  1615. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  1616. // 0 1 2 3 4 5 6 7 8 9
  1617. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
  1618. "movq (%0), %%mm0 \n\t" //
  1619. "movq (%%"REG_a", %1), %%mm2 \n\t" //
  1620. "movq (%%"REG_a"), %%mm1 \n\t" //
  1621. "movq %%mm0, %%mm3 \n\t"
  1622. "pmaxub %%mm1, %%mm0 \n\t" //
  1623. "pminub %%mm3, %%mm1 \n\t" //
  1624. "pmaxub %%mm2, %%mm1 \n\t" //
  1625. "pminub %%mm1, %%mm0 \n\t"
  1626. "movq %%mm0, (%%"REG_a") \n\t"
  1627. "movq (%0, %1, 4), %%mm0 \n\t" //
  1628. "movq (%%"REG_a", %1, 2), %%mm1 \n\t" //
  1629. "movq %%mm2, %%mm3 \n\t"
  1630. "pmaxub %%mm1, %%mm2 \n\t" //
  1631. "pminub %%mm3, %%mm1 \n\t" //
  1632. "pmaxub %%mm0, %%mm1 \n\t" //
  1633. "pminub %%mm1, %%mm2 \n\t"
  1634. "movq %%mm2, (%%"REG_a", %1, 2) \n\t"
  1635. "movq (%%"REG_d"), %%mm2 \n\t" //
  1636. "movq (%%"REG_d", %1), %%mm1 \n\t" //
  1637. "movq %%mm2, %%mm3 \n\t"
  1638. "pmaxub %%mm0, %%mm2 \n\t" //
  1639. "pminub %%mm3, %%mm0 \n\t" //
  1640. "pmaxub %%mm1, %%mm0 \n\t" //
  1641. "pminub %%mm0, %%mm2 \n\t"
  1642. "movq %%mm2, (%%"REG_d") \n\t"
  1643. "movq (%%"REG_d", %1, 2), %%mm2 \n\t" //
  1644. "movq (%0, %1, 8), %%mm0 \n\t" //
  1645. "movq %%mm2, %%mm3 \n\t"
  1646. "pmaxub %%mm0, %%mm2 \n\t" //
  1647. "pminub %%mm3, %%mm0 \n\t" //
  1648. "pmaxub %%mm1, %%mm0 \n\t" //
  1649. "pminub %%mm0, %%mm2 \n\t"
  1650. "movq %%mm2, (%%"REG_d", %1, 2) \n\t"
  1651. : : "r" (src), "r" ((x86_reg)stride)
  1652. : "%"REG_a, "%"REG_d
  1653. );
  1654. #else // MMX without MMX2
  1655. __asm__ volatile(
  1656. "lea (%0, %1), %%"REG_a" \n\t"
  1657. "lea (%%"REG_a", %1, 4), %%"REG_d" \n\t"
  1658. // 0 1 2 3 4 5 6 7 8 9
  1659. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
  1660. "pxor %%mm7, %%mm7 \n\t"
  1661. #define REAL_MEDIAN(a,b,c)\
  1662. "movq " #a ", %%mm0 \n\t"\
  1663. "movq " #b ", %%mm2 \n\t"\
  1664. "movq " #c ", %%mm1 \n\t"\
  1665. "movq %%mm0, %%mm3 \n\t"\
  1666. "movq %%mm1, %%mm4 \n\t"\
  1667. "movq %%mm2, %%mm5 \n\t"\
  1668. "psubusb %%mm1, %%mm3 \n\t"\
  1669. "psubusb %%mm2, %%mm4 \n\t"\
  1670. "psubusb %%mm0, %%mm5 \n\t"\
  1671. "pcmpeqb %%mm7, %%mm3 \n\t"\
  1672. "pcmpeqb %%mm7, %%mm4 \n\t"\
  1673. "pcmpeqb %%mm7, %%mm5 \n\t"\
  1674. "movq %%mm3, %%mm6 \n\t"\
  1675. "pxor %%mm4, %%mm3 \n\t"\
  1676. "pxor %%mm5, %%mm4 \n\t"\
  1677. "pxor %%mm6, %%mm5 \n\t"\
  1678. "por %%mm3, %%mm1 \n\t"\
  1679. "por %%mm4, %%mm2 \n\t"\
  1680. "por %%mm5, %%mm0 \n\t"\
  1681. "pand %%mm2, %%mm0 \n\t"\
  1682. "pand %%mm1, %%mm0 \n\t"\
  1683. "movq %%mm0, " #b " \n\t"
  1684. #define MEDIAN(a,b,c) REAL_MEDIAN(a,b,c)
  1685. MEDIAN((%0) , (%%REGa) , (%%REGa, %1))
  1686. MEDIAN((%%REGa, %1), (%%REGa, %1, 2), (%0, %1, 4))
  1687. MEDIAN((%0, %1, 4) , (%%REGd) , (%%REGd, %1))
  1688. MEDIAN((%%REGd, %1), (%%REGd, %1, 2), (%0, %1, 8))
  1689. : : "r" (src), "r" ((x86_reg)stride)
  1690. : "%"REG_a, "%"REG_d
  1691. );
  1692. #endif //HAVE_MMX2
  1693. #else //HAVE_MMX
  1694. int x, y;
  1695. src+= 4*stride;
  1696. // FIXME - there should be a way to do a few columns in parallel like w/mmx
  1697. for(x=0; x<8; x++){
  1698. uint8_t *colsrc = src;
  1699. for (y=0; y<4; y++){
  1700. int a, b, c, d, e, f;
  1701. a = colsrc[0 ];
  1702. b = colsrc[stride ];
  1703. c = colsrc[stride*2];
  1704. d = (a-b)>>31;
  1705. e = (b-c)>>31;
  1706. f = (c-a)>>31;
  1707. colsrc[stride ] = (a|(d^f)) & (b|(d^e)) & (c|(e^f));
  1708. colsrc += stride*2;
  1709. }
  1710. src++;
  1711. }
  1712. #endif //HAVE_MMX
  1713. }
  1714. #if HAVE_MMX
  1715. /**
  1716. * transposes and shift the given 8x8 Block into dst1 and dst2
  1717. */
  1718. static inline void RENAME(transpose1)(uint8_t *dst1, uint8_t *dst2, uint8_t *src, int srcStride)
  1719. {
  1720. __asm__(
  1721. "lea (%0, %1), %%"REG_a" \n\t"
  1722. // 0 1 2 3 4 5 6 7 8 9
  1723. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
  1724. "movq (%0), %%mm0 \n\t" // 12345678
  1725. "movq (%%"REG_a"), %%mm1 \n\t" // abcdefgh
  1726. "movq %%mm0, %%mm2 \n\t" // 12345678
  1727. "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
  1728. "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
  1729. "movq (%%"REG_a", %1), %%mm1 \n\t"
  1730. "movq (%%"REG_a", %1, 2), %%mm3 \n\t"
  1731. "movq %%mm1, %%mm4 \n\t"
  1732. "punpcklbw %%mm3, %%mm1 \n\t"
  1733. "punpckhbw %%mm3, %%mm4 \n\t"
  1734. "movq %%mm0, %%mm3 \n\t"
  1735. "punpcklwd %%mm1, %%mm0 \n\t"
  1736. "punpckhwd %%mm1, %%mm3 \n\t"
  1737. "movq %%mm2, %%mm1 \n\t"
  1738. "punpcklwd %%mm4, %%mm2 \n\t"
  1739. "punpckhwd %%mm4, %%mm1 \n\t"
  1740. "movd %%mm0, 128(%2) \n\t"
  1741. "psrlq $32, %%mm0 \n\t"
  1742. "movd %%mm0, 144(%2) \n\t"
  1743. "movd %%mm3, 160(%2) \n\t"
  1744. "psrlq $32, %%mm3 \n\t"
  1745. "movd %%mm3, 176(%2) \n\t"
  1746. "movd %%mm3, 48(%3) \n\t"
  1747. "movd %%mm2, 192(%2) \n\t"
  1748. "movd %%mm2, 64(%3) \n\t"
  1749. "psrlq $32, %%mm2 \n\t"
  1750. "movd %%mm2, 80(%3) \n\t"
  1751. "movd %%mm1, 96(%3) \n\t"
  1752. "psrlq $32, %%mm1 \n\t"
  1753. "movd %%mm1, 112(%3) \n\t"
  1754. "lea (%%"REG_a", %1, 4), %%"REG_a" \n\t"
  1755. "movq (%0, %1, 4), %%mm0 \n\t" // 12345678
  1756. "movq (%%"REG_a"), %%mm1 \n\t" // abcdefgh
  1757. "movq %%mm0, %%mm2 \n\t" // 12345678
  1758. "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
  1759. "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
  1760. "movq (%%"REG_a", %1), %%mm1 \n\t"
  1761. "movq (%%"REG_a", %1, 2), %%mm3 \n\t"
  1762. "movq %%mm1, %%mm4 \n\t"
  1763. "punpcklbw %%mm3, %%mm1 \n\t"
  1764. "punpckhbw %%mm3, %%mm4 \n\t"
  1765. "movq %%mm0, %%mm3 \n\t"
  1766. "punpcklwd %%mm1, %%mm0 \n\t"
  1767. "punpckhwd %%mm1, %%mm3 \n\t"
  1768. "movq %%mm2, %%mm1 \n\t"
  1769. "punpcklwd %%mm4, %%mm2 \n\t"
  1770. "punpckhwd %%mm4, %%mm1 \n\t"
  1771. "movd %%mm0, 132(%2) \n\t"
  1772. "psrlq $32, %%mm0 \n\t"
  1773. "movd %%mm0, 148(%2) \n\t"
  1774. "movd %%mm3, 164(%2) \n\t"
  1775. "psrlq $32, %%mm3 \n\t"
  1776. "movd %%mm3, 180(%2) \n\t"
  1777. "movd %%mm3, 52(%3) \n\t"
  1778. "movd %%mm2, 196(%2) \n\t"
  1779. "movd %%mm2, 68(%3) \n\t"
  1780. "psrlq $32, %%mm2 \n\t"
  1781. "movd %%mm2, 84(%3) \n\t"
  1782. "movd %%mm1, 100(%3) \n\t"
  1783. "psrlq $32, %%mm1 \n\t"
  1784. "movd %%mm1, 116(%3) \n\t"
  1785. :: "r" (src), "r" ((x86_reg)srcStride), "r" (dst1), "r" (dst2)
  1786. : "%"REG_a
  1787. );
  1788. }
  1789. /**
  1790. * transposes the given 8x8 block
  1791. */
  1792. static inline void RENAME(transpose2)(uint8_t *dst, int dstStride, uint8_t *src)
  1793. {
  1794. __asm__(
  1795. "lea (%0, %1), %%"REG_a" \n\t"
  1796. "lea (%%"REG_a",%1,4), %%"REG_d" \n\t"
  1797. // 0 1 2 3 4 5 6 7 8 9
  1798. // %0 eax eax+%1 eax+2%1 %0+4%1 edx edx+%1 edx+2%1 %0+8%1 edx+4%1
  1799. "movq (%2), %%mm0 \n\t" // 12345678
  1800. "movq 16(%2), %%mm1 \n\t" // abcdefgh
  1801. "movq %%mm0, %%mm2 \n\t" // 12345678
  1802. "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
  1803. "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
  1804. "movq 32(%2), %%mm1 \n\t"
  1805. "movq 48(%2), %%mm3 \n\t"
  1806. "movq %%mm1, %%mm4 \n\t"
  1807. "punpcklbw %%mm3, %%mm1 \n\t"
  1808. "punpckhbw %%mm3, %%mm4 \n\t"
  1809. "movq %%mm0, %%mm3 \n\t"
  1810. "punpcklwd %%mm1, %%mm0 \n\t"
  1811. "punpckhwd %%mm1, %%mm3 \n\t"
  1812. "movq %%mm2, %%mm1 \n\t"
  1813. "punpcklwd %%mm4, %%mm2 \n\t"
  1814. "punpckhwd %%mm4, %%mm1 \n\t"
  1815. "movd %%mm0, (%0) \n\t"
  1816. "psrlq $32, %%mm0 \n\t"
  1817. "movd %%mm0, (%%"REG_a") \n\t"
  1818. "movd %%mm3, (%%"REG_a", %1) \n\t"
  1819. "psrlq $32, %%mm3 \n\t"
  1820. "movd %%mm3, (%%"REG_a", %1, 2) \n\t"
  1821. "movd %%mm2, (%0, %1, 4) \n\t"
  1822. "psrlq $32, %%mm2 \n\t"
  1823. "movd %%mm2, (%%"REG_d") \n\t"
  1824. "movd %%mm1, (%%"REG_d", %1) \n\t"
  1825. "psrlq $32, %%mm1 \n\t"
  1826. "movd %%mm1, (%%"REG_d", %1, 2) \n\t"
  1827. "movq 64(%2), %%mm0 \n\t" // 12345678
  1828. "movq 80(%2), %%mm1 \n\t" // abcdefgh
  1829. "movq %%mm0, %%mm2 \n\t" // 12345678
  1830. "punpcklbw %%mm1, %%mm0 \n\t" // 1a2b3c4d
  1831. "punpckhbw %%mm1, %%mm2 \n\t" // 5e6f7g8h
  1832. "movq 96(%2), %%mm1 \n\t"
  1833. "movq 112(%2), %%mm3 \n\t"
  1834. "movq %%mm1, %%mm4 \n\t"
  1835. "punpcklbw %%mm3, %%mm1 \n\t"
  1836. "punpckhbw %%mm3, %%mm4 \n\t"
  1837. "movq %%mm0, %%mm3 \n\t"
  1838. "punpcklwd %%mm1, %%mm0 \n\t"
  1839. "punpckhwd %%mm1, %%mm3 \n\t"
  1840. "movq %%mm2, %%mm1 \n\t"
  1841. "punpcklwd %%mm4, %%mm2 \n\t"
  1842. "punpckhwd %%mm4, %%mm1 \n\t"
  1843. "movd %%mm0, 4(%0) \n\t"
  1844. "psrlq $32, %%mm0 \n\t"
  1845. "movd %%mm0, 4(%%"REG_a") \n\t"
  1846. "movd %%mm3, 4(%%"REG_a", %1) \n\t"
  1847. "psrlq $32, %%mm3 \n\t"
  1848. "movd %%mm3, 4(%%"REG_a", %1, 2) \n\t"
  1849. "movd %%mm2, 4(%0, %1, 4) \n\t"
  1850. "psrlq $32, %%mm2 \n\t"
  1851. "movd %%mm2, 4(%%"REG_d") \n\t"
  1852. "movd %%mm1, 4(%%"REG_d", %1) \n\t"
  1853. "psrlq $32, %%mm1 \n\t"
  1854. "movd %%mm1, 4(%%"REG_d", %1, 2) \n\t"
  1855. :: "r" (dst), "r" ((x86_reg)dstStride), "r" (src)
  1856. : "%"REG_a, "%"REG_d
  1857. );
  1858. }
  1859. #endif //HAVE_MMX
  1860. //static long test=0;
  1861. #if !HAVE_ALTIVEC
  1862. static inline void RENAME(tempNoiseReducer)(uint8_t *src, int stride,
  1863. uint8_t *tempBlurred, uint32_t *tempBlurredPast, int *maxNoise)
  1864. {
  1865. // to save a register (FIXME do this outside of the loops)
  1866. tempBlurredPast[127]= maxNoise[0];
  1867. tempBlurredPast[128]= maxNoise[1];
  1868. tempBlurredPast[129]= maxNoise[2];
  1869. #define FAST_L2_DIFF
  1870. //#define L1_DIFF //u should change the thresholds too if u try that one
  1871. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1872. __asm__ volatile(
  1873. "lea (%2, %2, 2), %%"REG_a" \n\t" // 3*stride
  1874. "lea (%2, %2, 4), %%"REG_d" \n\t" // 5*stride
  1875. "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
  1876. // 0 1 2 3 4 5 6 7 8 9
  1877. // %x %x+%2 %x+2%2 %x+eax %x+4%2 %x+edx %x+2eax %x+ecx %x+8%2
  1878. //FIXME reorder?
  1879. #ifdef L1_DIFF //needs mmx2
  1880. "movq (%0), %%mm0 \n\t" // L0
  1881. "psadbw (%1), %%mm0 \n\t" // |L0-R0|
  1882. "movq (%0, %2), %%mm1 \n\t" // L1
  1883. "psadbw (%1, %2), %%mm1 \n\t" // |L1-R1|
  1884. "movq (%0, %2, 2), %%mm2 \n\t" // L2
  1885. "psadbw (%1, %2, 2), %%mm2 \n\t" // |L2-R2|
  1886. "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
  1887. "psadbw (%1, %%"REG_a"), %%mm3 \n\t" // |L3-R3|
  1888. "movq (%0, %2, 4), %%mm4 \n\t" // L4
  1889. "paddw %%mm1, %%mm0 \n\t"
  1890. "psadbw (%1, %2, 4), %%mm4 \n\t" // |L4-R4|
  1891. "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
  1892. "paddw %%mm2, %%mm0 \n\t"
  1893. "psadbw (%1, %%"REG_d"), %%mm5 \n\t" // |L5-R5|
  1894. "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
  1895. "paddw %%mm3, %%mm0 \n\t"
  1896. "psadbw (%1, %%"REG_a", 2), %%mm6 \n\t" // |L6-R6|
  1897. "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
  1898. "paddw %%mm4, %%mm0 \n\t"
  1899. "psadbw (%1, %%"REG_c"), %%mm7 \n\t" // |L7-R7|
  1900. "paddw %%mm5, %%mm6 \n\t"
  1901. "paddw %%mm7, %%mm6 \n\t"
  1902. "paddw %%mm6, %%mm0 \n\t"
  1903. #else //L1_DIFF
  1904. #if defined (FAST_L2_DIFF)
  1905. "pcmpeqb %%mm7, %%mm7 \n\t"
  1906. "movq "MANGLE(b80)", %%mm6 \n\t"
  1907. "pxor %%mm0, %%mm0 \n\t"
  1908. #define REAL_L2_DIFF_CORE(a, b)\
  1909. "movq " #a ", %%mm5 \n\t"\
  1910. "movq " #b ", %%mm2 \n\t"\
  1911. "pxor %%mm7, %%mm2 \n\t"\
  1912. PAVGB(%%mm2, %%mm5)\
  1913. "paddb %%mm6, %%mm5 \n\t"\
  1914. "movq %%mm5, %%mm2 \n\t"\
  1915. "psllw $8, %%mm5 \n\t"\
  1916. "pmaddwd %%mm5, %%mm5 \n\t"\
  1917. "pmaddwd %%mm2, %%mm2 \n\t"\
  1918. "paddd %%mm2, %%mm5 \n\t"\
  1919. "psrld $14, %%mm5 \n\t"\
  1920. "paddd %%mm5, %%mm0 \n\t"
  1921. #else //defined (FAST_L2_DIFF)
  1922. "pxor %%mm7, %%mm7 \n\t"
  1923. "pxor %%mm0, %%mm0 \n\t"
  1924. #define REAL_L2_DIFF_CORE(a, b)\
  1925. "movq " #a ", %%mm5 \n\t"\
  1926. "movq " #b ", %%mm2 \n\t"\
  1927. "movq %%mm5, %%mm1 \n\t"\
  1928. "movq %%mm2, %%mm3 \n\t"\
  1929. "punpcklbw %%mm7, %%mm5 \n\t"\
  1930. "punpckhbw %%mm7, %%mm1 \n\t"\
  1931. "punpcklbw %%mm7, %%mm2 \n\t"\
  1932. "punpckhbw %%mm7, %%mm3 \n\t"\
  1933. "psubw %%mm2, %%mm5 \n\t"\
  1934. "psubw %%mm3, %%mm1 \n\t"\
  1935. "pmaddwd %%mm5, %%mm5 \n\t"\
  1936. "pmaddwd %%mm1, %%mm1 \n\t"\
  1937. "paddd %%mm1, %%mm5 \n\t"\
  1938. "paddd %%mm5, %%mm0 \n\t"
  1939. #endif //defined (FAST_L2_DIFF)
  1940. #define L2_DIFF_CORE(a, b) REAL_L2_DIFF_CORE(a, b)
  1941. L2_DIFF_CORE((%0) , (%1))
  1942. L2_DIFF_CORE((%0, %2) , (%1, %2))
  1943. L2_DIFF_CORE((%0, %2, 2) , (%1, %2, 2))
  1944. L2_DIFF_CORE((%0, %%REGa) , (%1, %%REGa))
  1945. L2_DIFF_CORE((%0, %2, 4) , (%1, %2, 4))
  1946. L2_DIFF_CORE((%0, %%REGd) , (%1, %%REGd))
  1947. L2_DIFF_CORE((%0, %%REGa,2), (%1, %%REGa,2))
  1948. L2_DIFF_CORE((%0, %%REGc) , (%1, %%REGc))
  1949. #endif //L1_DIFF
  1950. "movq %%mm0, %%mm4 \n\t"
  1951. "psrlq $32, %%mm0 \n\t"
  1952. "paddd %%mm0, %%mm4 \n\t"
  1953. "movd %%mm4, %%ecx \n\t"
  1954. "shll $2, %%ecx \n\t"
  1955. "mov %3, %%"REG_d" \n\t"
  1956. "addl -4(%%"REG_d"), %%ecx \n\t"
  1957. "addl 4(%%"REG_d"), %%ecx \n\t"
  1958. "addl -1024(%%"REG_d"), %%ecx \n\t"
  1959. "addl $4, %%ecx \n\t"
  1960. "addl 1024(%%"REG_d"), %%ecx \n\t"
  1961. "shrl $3, %%ecx \n\t"
  1962. "movl %%ecx, (%%"REG_d") \n\t"
  1963. // "mov %3, %%"REG_c" \n\t"
  1964. // "mov %%"REG_c", test \n\t"
  1965. // "jmp 4f \n\t"
  1966. "cmpl 512(%%"REG_d"), %%ecx \n\t"
  1967. " jb 2f \n\t"
  1968. "cmpl 516(%%"REG_d"), %%ecx \n\t"
  1969. " jb 1f \n\t"
  1970. "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
  1971. "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
  1972. "movq (%0), %%mm0 \n\t" // L0
  1973. "movq (%0, %2), %%mm1 \n\t" // L1
  1974. "movq (%0, %2, 2), %%mm2 \n\t" // L2
  1975. "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
  1976. "movq (%0, %2, 4), %%mm4 \n\t" // L4
  1977. "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
  1978. "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
  1979. "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
  1980. "movq %%mm0, (%1) \n\t" // L0
  1981. "movq %%mm1, (%1, %2) \n\t" // L1
  1982. "movq %%mm2, (%1, %2, 2) \n\t" // L2
  1983. "movq %%mm3, (%1, %%"REG_a") \n\t" // L3
  1984. "movq %%mm4, (%1, %2, 4) \n\t" // L4
  1985. "movq %%mm5, (%1, %%"REG_d") \n\t" // L5
  1986. "movq %%mm6, (%1, %%"REG_a", 2) \n\t" // L6
  1987. "movq %%mm7, (%1, %%"REG_c") \n\t" // L7
  1988. "jmp 4f \n\t"
  1989. "1: \n\t"
  1990. "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
  1991. "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
  1992. "movq (%0), %%mm0 \n\t" // L0
  1993. PAVGB((%1), %%mm0) // L0
  1994. "movq (%0, %2), %%mm1 \n\t" // L1
  1995. PAVGB((%1, %2), %%mm1) // L1
  1996. "movq (%0, %2, 2), %%mm2 \n\t" // L2
  1997. PAVGB((%1, %2, 2), %%mm2) // L2
  1998. "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
  1999. PAVGB((%1, %%REGa), %%mm3) // L3
  2000. "movq (%0, %2, 4), %%mm4 \n\t" // L4
  2001. PAVGB((%1, %2, 4), %%mm4) // L4
  2002. "movq (%0, %%"REG_d"), %%mm5 \n\t" // L5
  2003. PAVGB((%1, %%REGd), %%mm5) // L5
  2004. "movq (%0, %%"REG_a", 2), %%mm6 \n\t" // L6
  2005. PAVGB((%1, %%REGa, 2), %%mm6) // L6
  2006. "movq (%0, %%"REG_c"), %%mm7 \n\t" // L7
  2007. PAVGB((%1, %%REGc), %%mm7) // L7
  2008. "movq %%mm0, (%1) \n\t" // R0
  2009. "movq %%mm1, (%1, %2) \n\t" // R1
  2010. "movq %%mm2, (%1, %2, 2) \n\t" // R2
  2011. "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
  2012. "movq %%mm4, (%1, %2, 4) \n\t" // R4
  2013. "movq %%mm5, (%1, %%"REG_d") \n\t" // R5
  2014. "movq %%mm6, (%1, %%"REG_a", 2) \n\t" // R6
  2015. "movq %%mm7, (%1, %%"REG_c") \n\t" // R7
  2016. "movq %%mm0, (%0) \n\t" // L0
  2017. "movq %%mm1, (%0, %2) \n\t" // L1
  2018. "movq %%mm2, (%0, %2, 2) \n\t" // L2
  2019. "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
  2020. "movq %%mm4, (%0, %2, 4) \n\t" // L4
  2021. "movq %%mm5, (%0, %%"REG_d") \n\t" // L5
  2022. "movq %%mm6, (%0, %%"REG_a", 2) \n\t" // L6
  2023. "movq %%mm7, (%0, %%"REG_c") \n\t" // L7
  2024. "jmp 4f \n\t"
  2025. "2: \n\t"
  2026. "cmpl 508(%%"REG_d"), %%ecx \n\t"
  2027. " jb 3f \n\t"
  2028. "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
  2029. "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
  2030. "movq (%0), %%mm0 \n\t" // L0
  2031. "movq (%0, %2), %%mm1 \n\t" // L1
  2032. "movq (%0, %2, 2), %%mm2 \n\t" // L2
  2033. "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
  2034. "movq (%1), %%mm4 \n\t" // R0
  2035. "movq (%1, %2), %%mm5 \n\t" // R1
  2036. "movq (%1, %2, 2), %%mm6 \n\t" // R2
  2037. "movq (%1, %%"REG_a"), %%mm7 \n\t" // R3
  2038. PAVGB(%%mm4, %%mm0)
  2039. PAVGB(%%mm5, %%mm1)
  2040. PAVGB(%%mm6, %%mm2)
  2041. PAVGB(%%mm7, %%mm3)
  2042. PAVGB(%%mm4, %%mm0)
  2043. PAVGB(%%mm5, %%mm1)
  2044. PAVGB(%%mm6, %%mm2)
  2045. PAVGB(%%mm7, %%mm3)
  2046. "movq %%mm0, (%1) \n\t" // R0
  2047. "movq %%mm1, (%1, %2) \n\t" // R1
  2048. "movq %%mm2, (%1, %2, 2) \n\t" // R2
  2049. "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
  2050. "movq %%mm0, (%0) \n\t" // L0
  2051. "movq %%mm1, (%0, %2) \n\t" // L1
  2052. "movq %%mm2, (%0, %2, 2) \n\t" // L2
  2053. "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
  2054. "movq (%0, %2, 4), %%mm0 \n\t" // L4
  2055. "movq (%0, %%"REG_d"), %%mm1 \n\t" // L5
  2056. "movq (%0, %%"REG_a", 2), %%mm2 \n\t" // L6
  2057. "movq (%0, %%"REG_c"), %%mm3 \n\t" // L7
  2058. "movq (%1, %2, 4), %%mm4 \n\t" // R4
  2059. "movq (%1, %%"REG_d"), %%mm5 \n\t" // R5
  2060. "movq (%1, %%"REG_a", 2), %%mm6 \n\t" // R6
  2061. "movq (%1, %%"REG_c"), %%mm7 \n\t" // R7
  2062. PAVGB(%%mm4, %%mm0)
  2063. PAVGB(%%mm5, %%mm1)
  2064. PAVGB(%%mm6, %%mm2)
  2065. PAVGB(%%mm7, %%mm3)
  2066. PAVGB(%%mm4, %%mm0)
  2067. PAVGB(%%mm5, %%mm1)
  2068. PAVGB(%%mm6, %%mm2)
  2069. PAVGB(%%mm7, %%mm3)
  2070. "movq %%mm0, (%1, %2, 4) \n\t" // R4
  2071. "movq %%mm1, (%1, %%"REG_d") \n\t" // R5
  2072. "movq %%mm2, (%1, %%"REG_a", 2) \n\t" // R6
  2073. "movq %%mm3, (%1, %%"REG_c") \n\t" // R7
  2074. "movq %%mm0, (%0, %2, 4) \n\t" // L4
  2075. "movq %%mm1, (%0, %%"REG_d") \n\t" // L5
  2076. "movq %%mm2, (%0, %%"REG_a", 2) \n\t" // L6
  2077. "movq %%mm3, (%0, %%"REG_c") \n\t" // L7
  2078. "jmp 4f \n\t"
  2079. "3: \n\t"
  2080. "lea (%%"REG_a", %2, 2), %%"REG_d" \n\t" // 5*stride
  2081. "lea (%%"REG_d", %2, 2), %%"REG_c" \n\t" // 7*stride
  2082. "movq (%0), %%mm0 \n\t" // L0
  2083. "movq (%0, %2), %%mm1 \n\t" // L1
  2084. "movq (%0, %2, 2), %%mm2 \n\t" // L2
  2085. "movq (%0, %%"REG_a"), %%mm3 \n\t" // L3
  2086. "movq (%1), %%mm4 \n\t" // R0
  2087. "movq (%1, %2), %%mm5 \n\t" // R1
  2088. "movq (%1, %2, 2), %%mm6 \n\t" // R2
  2089. "movq (%1, %%"REG_a"), %%mm7 \n\t" // R3
  2090. PAVGB(%%mm4, %%mm0)
  2091. PAVGB(%%mm5, %%mm1)
  2092. PAVGB(%%mm6, %%mm2)
  2093. PAVGB(%%mm7, %%mm3)
  2094. PAVGB(%%mm4, %%mm0)
  2095. PAVGB(%%mm5, %%mm1)
  2096. PAVGB(%%mm6, %%mm2)
  2097. PAVGB(%%mm7, %%mm3)
  2098. PAVGB(%%mm4, %%mm0)
  2099. PAVGB(%%mm5, %%mm1)
  2100. PAVGB(%%mm6, %%mm2)
  2101. PAVGB(%%mm7, %%mm3)
  2102. "movq %%mm0, (%1) \n\t" // R0
  2103. "movq %%mm1, (%1, %2) \n\t" // R1
  2104. "movq %%mm2, (%1, %2, 2) \n\t" // R2
  2105. "movq %%mm3, (%1, %%"REG_a") \n\t" // R3
  2106. "movq %%mm0, (%0) \n\t" // L0
  2107. "movq %%mm1, (%0, %2) \n\t" // L1
  2108. "movq %%mm2, (%0, %2, 2) \n\t" // L2
  2109. "movq %%mm3, (%0, %%"REG_a") \n\t" // L3
  2110. "movq (%0, %2, 4), %%mm0 \n\t" // L4
  2111. "movq (%0, %%"REG_d"), %%mm1 \n\t" // L5
  2112. "movq (%0, %%"REG_a", 2), %%mm2 \n\t" // L6
  2113. "movq (%0, %%"REG_c"), %%mm3 \n\t" // L7
  2114. "movq (%1, %2, 4), %%mm4 \n\t" // R4
  2115. "movq (%1, %%"REG_d"), %%mm5 \n\t" // R5
  2116. "movq (%1, %%"REG_a", 2), %%mm6 \n\t" // R6
  2117. "movq (%1, %%"REG_c"), %%mm7 \n\t" // R7
  2118. PAVGB(%%mm4, %%mm0)
  2119. PAVGB(%%mm5, %%mm1)
  2120. PAVGB(%%mm6, %%mm2)
  2121. PAVGB(%%mm7, %%mm3)
  2122. PAVGB(%%mm4, %%mm0)
  2123. PAVGB(%%mm5, %%mm1)
  2124. PAVGB(%%mm6, %%mm2)
  2125. PAVGB(%%mm7, %%mm3)
  2126. PAVGB(%%mm4, %%mm0)
  2127. PAVGB(%%mm5, %%mm1)
  2128. PAVGB(%%mm6, %%mm2)
  2129. PAVGB(%%mm7, %%mm3)
  2130. "movq %%mm0, (%1, %2, 4) \n\t" // R4
  2131. "movq %%mm1, (%1, %%"REG_d") \n\t" // R5
  2132. "movq %%mm2, (%1, %%"REG_a", 2) \n\t" // R6
  2133. "movq %%mm3, (%1, %%"REG_c") \n\t" // R7
  2134. "movq %%mm0, (%0, %2, 4) \n\t" // L4
  2135. "movq %%mm1, (%0, %%"REG_d") \n\t" // L5
  2136. "movq %%mm2, (%0, %%"REG_a", 2) \n\t" // L6
  2137. "movq %%mm3, (%0, %%"REG_c") \n\t" // L7
  2138. "4: \n\t"
  2139. :: "r" (src), "r" (tempBlurred), "r"((x86_reg)stride), "m" (tempBlurredPast)
  2140. : "%"REG_a, "%"REG_d, "%"REG_c, "memory"
  2141. );
  2142. #else //HAVE_MMX2 || HAVE_AMD3DNOW
  2143. {
  2144. int y;
  2145. int d=0;
  2146. // int sysd=0;
  2147. int i;
  2148. for(y=0; y<8; y++){
  2149. int x;
  2150. for(x=0; x<8; x++){
  2151. int ref= tempBlurred[ x + y*stride ];
  2152. int cur= src[ x + y*stride ];
  2153. int d1=ref - cur;
  2154. // if(x==0 || x==7) d1+= d1>>1;
  2155. // if(y==0 || y==7) d1+= d1>>1;
  2156. // d+= FFABS(d1);
  2157. d+= d1*d1;
  2158. // sysd+= d1;
  2159. }
  2160. }
  2161. i=d;
  2162. d= (
  2163. 4*d
  2164. +(*(tempBlurredPast-256))
  2165. +(*(tempBlurredPast-1))+ (*(tempBlurredPast+1))
  2166. +(*(tempBlurredPast+256))
  2167. +4)>>3;
  2168. *tempBlurredPast=i;
  2169. // ((*tempBlurredPast)*3 + d + 2)>>2;
  2170. /*
  2171. Switch between
  2172. 1 0 0 0 0 0 0 (0)
  2173. 64 32 16 8 4 2 1 (1)
  2174. 64 48 36 27 20 15 11 (33) (approx)
  2175. 64 56 49 43 37 33 29 (200) (approx)
  2176. */
  2177. if(d > maxNoise[1]){
  2178. if(d < maxNoise[2]){
  2179. for(y=0; y<8; y++){
  2180. int x;
  2181. for(x=0; x<8; x++){
  2182. int ref= tempBlurred[ x + y*stride ];
  2183. int cur= src[ x + y*stride ];
  2184. tempBlurred[ x + y*stride ]=
  2185. src[ x + y*stride ]=
  2186. (ref + cur + 1)>>1;
  2187. }
  2188. }
  2189. }else{
  2190. for(y=0; y<8; y++){
  2191. int x;
  2192. for(x=0; x<8; x++){
  2193. tempBlurred[ x + y*stride ]= src[ x + y*stride ];
  2194. }
  2195. }
  2196. }
  2197. }else{
  2198. if(d < maxNoise[0]){
  2199. for(y=0; y<8; y++){
  2200. int x;
  2201. for(x=0; x<8; x++){
  2202. int ref= tempBlurred[ x + y*stride ];
  2203. int cur= src[ x + y*stride ];
  2204. tempBlurred[ x + y*stride ]=
  2205. src[ x + y*stride ]=
  2206. (ref*7 + cur + 4)>>3;
  2207. }
  2208. }
  2209. }else{
  2210. for(y=0; y<8; y++){
  2211. int x;
  2212. for(x=0; x<8; x++){
  2213. int ref= tempBlurred[ x + y*stride ];
  2214. int cur= src[ x + y*stride ];
  2215. tempBlurred[ x + y*stride ]=
  2216. src[ x + y*stride ]=
  2217. (ref*3 + cur + 2)>>2;
  2218. }
  2219. }
  2220. }
  2221. }
  2222. }
  2223. #endif //HAVE_MMX2 || HAVE_AMD3DNOW
  2224. }
  2225. #endif //HAVE_ALTIVEC
  2226. #if HAVE_MMX
  2227. /**
  2228. * accurate deblock filter
  2229. */
  2230. static av_always_inline void RENAME(do_a_deblock)(uint8_t *src, int step, int stride, PPContext *c){
  2231. int64_t dc_mask, eq_mask, both_masks;
  2232. int64_t sums[10*8*2];
  2233. src+= step*3; // src points to begin of the 8x8 Block
  2234. //START_TIMER
  2235. __asm__ volatile(
  2236. "movq %0, %%mm7 \n\t"
  2237. "movq %1, %%mm6 \n\t"
  2238. : : "m" (c->mmxDcOffset[c->nonBQP]), "m" (c->mmxDcThreshold[c->nonBQP])
  2239. );
  2240. __asm__ volatile(
  2241. "lea (%2, %3), %%"REG_a" \n\t"
  2242. // 0 1 2 3 4 5 6 7 8 9
  2243. // %1 eax eax+%2 eax+2%2 %1+4%2 ecx ecx+%2 ecx+2%2 %1+8%2 ecx+4%2
  2244. "movq (%2), %%mm0 \n\t"
  2245. "movq (%%"REG_a"), %%mm1 \n\t"
  2246. "movq %%mm1, %%mm3 \n\t"
  2247. "movq %%mm1, %%mm4 \n\t"
  2248. "psubb %%mm1, %%mm0 \n\t" // mm0 = differnece
  2249. "paddb %%mm7, %%mm0 \n\t"
  2250. "pcmpgtb %%mm6, %%mm0 \n\t"
  2251. "movq (%%"REG_a",%3), %%mm2 \n\t"
  2252. PMAXUB(%%mm2, %%mm4)
  2253. PMINUB(%%mm2, %%mm3, %%mm5)
  2254. "psubb %%mm2, %%mm1 \n\t"
  2255. "paddb %%mm7, %%mm1 \n\t"
  2256. "pcmpgtb %%mm6, %%mm1 \n\t"
  2257. "paddb %%mm1, %%mm0 \n\t"
  2258. "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
  2259. PMAXUB(%%mm1, %%mm4)
  2260. PMINUB(%%mm1, %%mm3, %%mm5)
  2261. "psubb %%mm1, %%mm2 \n\t"
  2262. "paddb %%mm7, %%mm2 \n\t"
  2263. "pcmpgtb %%mm6, %%mm2 \n\t"
  2264. "paddb %%mm2, %%mm0 \n\t"
  2265. "lea (%%"REG_a", %3, 4), %%"REG_a" \n\t"
  2266. "movq (%2, %3, 4), %%mm2 \n\t"
  2267. PMAXUB(%%mm2, %%mm4)
  2268. PMINUB(%%mm2, %%mm3, %%mm5)
  2269. "psubb %%mm2, %%mm1 \n\t"
  2270. "paddb %%mm7, %%mm1 \n\t"
  2271. "pcmpgtb %%mm6, %%mm1 \n\t"
  2272. "paddb %%mm1, %%mm0 \n\t"
  2273. "movq (%%"REG_a"), %%mm1 \n\t"
  2274. PMAXUB(%%mm1, %%mm4)
  2275. PMINUB(%%mm1, %%mm3, %%mm5)
  2276. "psubb %%mm1, %%mm2 \n\t"
  2277. "paddb %%mm7, %%mm2 \n\t"
  2278. "pcmpgtb %%mm6, %%mm2 \n\t"
  2279. "paddb %%mm2, %%mm0 \n\t"
  2280. "movq (%%"REG_a", %3), %%mm2 \n\t"
  2281. PMAXUB(%%mm2, %%mm4)
  2282. PMINUB(%%mm2, %%mm3, %%mm5)
  2283. "psubb %%mm2, %%mm1 \n\t"
  2284. "paddb %%mm7, %%mm1 \n\t"
  2285. "pcmpgtb %%mm6, %%mm1 \n\t"
  2286. "paddb %%mm1, %%mm0 \n\t"
  2287. "movq (%%"REG_a", %3, 2), %%mm1 \n\t"
  2288. PMAXUB(%%mm1, %%mm4)
  2289. PMINUB(%%mm1, %%mm3, %%mm5)
  2290. "psubb %%mm1, %%mm2 \n\t"
  2291. "paddb %%mm7, %%mm2 \n\t"
  2292. "pcmpgtb %%mm6, %%mm2 \n\t"
  2293. "paddb %%mm2, %%mm0 \n\t"
  2294. "movq (%2, %3, 8), %%mm2 \n\t"
  2295. PMAXUB(%%mm2, %%mm4)
  2296. PMINUB(%%mm2, %%mm3, %%mm5)
  2297. "psubb %%mm2, %%mm1 \n\t"
  2298. "paddb %%mm7, %%mm1 \n\t"
  2299. "pcmpgtb %%mm6, %%mm1 \n\t"
  2300. "paddb %%mm1, %%mm0 \n\t"
  2301. "movq (%%"REG_a", %3, 4), %%mm1 \n\t"
  2302. "psubb %%mm1, %%mm2 \n\t"
  2303. "paddb %%mm7, %%mm2 \n\t"
  2304. "pcmpgtb %%mm6, %%mm2 \n\t"
  2305. "paddb %%mm2, %%mm0 \n\t"
  2306. "psubusb %%mm3, %%mm4 \n\t"
  2307. "pxor %%mm6, %%mm6 \n\t"
  2308. "movq %4, %%mm7 \n\t" // QP,..., QP
  2309. "paddusb %%mm7, %%mm7 \n\t" // 2QP ... 2QP
  2310. "psubusb %%mm4, %%mm7 \n\t" // Diff >=2QP -> 0
  2311. "pcmpeqb %%mm6, %%mm7 \n\t" // Diff < 2QP -> 0
  2312. "pcmpeqb %%mm6, %%mm7 \n\t" // Diff < 2QP -> 0
  2313. "movq %%mm7, %1 \n\t"
  2314. "movq %5, %%mm7 \n\t"
  2315. "punpcklbw %%mm7, %%mm7 \n\t"
  2316. "punpcklbw %%mm7, %%mm7 \n\t"
  2317. "punpcklbw %%mm7, %%mm7 \n\t"
  2318. "psubb %%mm0, %%mm6 \n\t"
  2319. "pcmpgtb %%mm7, %%mm6 \n\t"
  2320. "movq %%mm6, %0 \n\t"
  2321. : "=m" (eq_mask), "=m" (dc_mask)
  2322. : "r" (src), "r" ((x86_reg)step), "m" (c->pQPb), "m"(c->ppMode.flatnessThreshold)
  2323. : "%"REG_a
  2324. );
  2325. both_masks = dc_mask & eq_mask;
  2326. if(both_masks){
  2327. x86_reg offset= -8*step;
  2328. int64_t *temp_sums= sums;
  2329. __asm__ volatile(
  2330. "movq %2, %%mm0 \n\t" // QP,..., QP
  2331. "pxor %%mm4, %%mm4 \n\t"
  2332. "movq (%0), %%mm6 \n\t"
  2333. "movq (%0, %1), %%mm5 \n\t"
  2334. "movq %%mm5, %%mm1 \n\t"
  2335. "movq %%mm6, %%mm2 \n\t"
  2336. "psubusb %%mm6, %%mm5 \n\t"
  2337. "psubusb %%mm1, %%mm2 \n\t"
  2338. "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
  2339. "psubusb %%mm2, %%mm0 \n\t" // diff >= QP -> 0
  2340. "pcmpeqb %%mm4, %%mm0 \n\t" // diff >= QP -> FF
  2341. "pxor %%mm6, %%mm1 \n\t"
  2342. "pand %%mm0, %%mm1 \n\t"
  2343. "pxor %%mm1, %%mm6 \n\t"
  2344. // 0:QP 6:First
  2345. "movq (%0, %1, 8), %%mm5 \n\t"
  2346. "add %1, %0 \n\t" // %0 points to line 1 not 0
  2347. "movq (%0, %1, 8), %%mm7 \n\t"
  2348. "movq %%mm5, %%mm1 \n\t"
  2349. "movq %%mm7, %%mm2 \n\t"
  2350. "psubusb %%mm7, %%mm5 \n\t"
  2351. "psubusb %%mm1, %%mm2 \n\t"
  2352. "por %%mm5, %%mm2 \n\t" // ABS Diff of lines
  2353. "movq %2, %%mm0 \n\t" // QP,..., QP
  2354. "psubusb %%mm2, %%mm0 \n\t" // diff >= QP -> 0
  2355. "pcmpeqb %%mm4, %%mm0 \n\t" // diff >= QP -> FF
  2356. "pxor %%mm7, %%mm1 \n\t"
  2357. "pand %%mm0, %%mm1 \n\t"
  2358. "pxor %%mm1, %%mm7 \n\t"
  2359. "movq %%mm6, %%mm5 \n\t"
  2360. "punpckhbw %%mm4, %%mm6 \n\t"
  2361. "punpcklbw %%mm4, %%mm5 \n\t"
  2362. // 4:0 5/6:First 7:Last
  2363. "movq %%mm5, %%mm0 \n\t"
  2364. "movq %%mm6, %%mm1 \n\t"
  2365. "psllw $2, %%mm0 \n\t"
  2366. "psllw $2, %%mm1 \n\t"
  2367. "paddw "MANGLE(w04)", %%mm0 \n\t"
  2368. "paddw "MANGLE(w04)", %%mm1 \n\t"
  2369. #define NEXT\
  2370. "movq (%0), %%mm2 \n\t"\
  2371. "movq (%0), %%mm3 \n\t"\
  2372. "add %1, %0 \n\t"\
  2373. "punpcklbw %%mm4, %%mm2 \n\t"\
  2374. "punpckhbw %%mm4, %%mm3 \n\t"\
  2375. "paddw %%mm2, %%mm0 \n\t"\
  2376. "paddw %%mm3, %%mm1 \n\t"
  2377. #define PREV\
  2378. "movq (%0), %%mm2 \n\t"\
  2379. "movq (%0), %%mm3 \n\t"\
  2380. "add %1, %0 \n\t"\
  2381. "punpcklbw %%mm4, %%mm2 \n\t"\
  2382. "punpckhbw %%mm4, %%mm3 \n\t"\
  2383. "psubw %%mm2, %%mm0 \n\t"\
  2384. "psubw %%mm3, %%mm1 \n\t"
  2385. NEXT //0
  2386. NEXT //1
  2387. NEXT //2
  2388. "movq %%mm0, (%3) \n\t"
  2389. "movq %%mm1, 8(%3) \n\t"
  2390. NEXT //3
  2391. "psubw %%mm5, %%mm0 \n\t"
  2392. "psubw %%mm6, %%mm1 \n\t"
  2393. "movq %%mm0, 16(%3) \n\t"
  2394. "movq %%mm1, 24(%3) \n\t"
  2395. NEXT //4
  2396. "psubw %%mm5, %%mm0 \n\t"
  2397. "psubw %%mm6, %%mm1 \n\t"
  2398. "movq %%mm0, 32(%3) \n\t"
  2399. "movq %%mm1, 40(%3) \n\t"
  2400. NEXT //5
  2401. "psubw %%mm5, %%mm0 \n\t"
  2402. "psubw %%mm6, %%mm1 \n\t"
  2403. "movq %%mm0, 48(%3) \n\t"
  2404. "movq %%mm1, 56(%3) \n\t"
  2405. NEXT //6
  2406. "psubw %%mm5, %%mm0 \n\t"
  2407. "psubw %%mm6, %%mm1 \n\t"
  2408. "movq %%mm0, 64(%3) \n\t"
  2409. "movq %%mm1, 72(%3) \n\t"
  2410. "movq %%mm7, %%mm6 \n\t"
  2411. "punpckhbw %%mm4, %%mm7 \n\t"
  2412. "punpcklbw %%mm4, %%mm6 \n\t"
  2413. NEXT //7
  2414. "mov %4, %0 \n\t"
  2415. "add %1, %0 \n\t"
  2416. PREV //0
  2417. "movq %%mm0, 80(%3) \n\t"
  2418. "movq %%mm1, 88(%3) \n\t"
  2419. PREV //1
  2420. "paddw %%mm6, %%mm0 \n\t"
  2421. "paddw %%mm7, %%mm1 \n\t"
  2422. "movq %%mm0, 96(%3) \n\t"
  2423. "movq %%mm1, 104(%3) \n\t"
  2424. PREV //2
  2425. "paddw %%mm6, %%mm0 \n\t"
  2426. "paddw %%mm7, %%mm1 \n\t"
  2427. "movq %%mm0, 112(%3) \n\t"
  2428. "movq %%mm1, 120(%3) \n\t"
  2429. PREV //3
  2430. "paddw %%mm6, %%mm0 \n\t"
  2431. "paddw %%mm7, %%mm1 \n\t"
  2432. "movq %%mm0, 128(%3) \n\t"
  2433. "movq %%mm1, 136(%3) \n\t"
  2434. PREV //4
  2435. "paddw %%mm6, %%mm0 \n\t"
  2436. "paddw %%mm7, %%mm1 \n\t"
  2437. "movq %%mm0, 144(%3) \n\t"
  2438. "movq %%mm1, 152(%3) \n\t"
  2439. "mov %4, %0 \n\t" //FIXME
  2440. : "+&r"(src)
  2441. : "r" ((x86_reg)step), "m" (c->pQPb), "r"(sums), "g"(src)
  2442. );
  2443. src+= step; // src points to begin of the 8x8 Block
  2444. __asm__ volatile(
  2445. "movq %4, %%mm6 \n\t"
  2446. "pcmpeqb %%mm5, %%mm5 \n\t"
  2447. "pxor %%mm6, %%mm5 \n\t"
  2448. "pxor %%mm7, %%mm7 \n\t"
  2449. "1: \n\t"
  2450. "movq (%1), %%mm0 \n\t"
  2451. "movq 8(%1), %%mm1 \n\t"
  2452. "paddw 32(%1), %%mm0 \n\t"
  2453. "paddw 40(%1), %%mm1 \n\t"
  2454. "movq (%0, %3), %%mm2 \n\t"
  2455. "movq %%mm2, %%mm3 \n\t"
  2456. "movq %%mm2, %%mm4 \n\t"
  2457. "punpcklbw %%mm7, %%mm2 \n\t"
  2458. "punpckhbw %%mm7, %%mm3 \n\t"
  2459. "paddw %%mm2, %%mm0 \n\t"
  2460. "paddw %%mm3, %%mm1 \n\t"
  2461. "paddw %%mm2, %%mm0 \n\t"
  2462. "paddw %%mm3, %%mm1 \n\t"
  2463. "psrlw $4, %%mm0 \n\t"
  2464. "psrlw $4, %%mm1 \n\t"
  2465. "packuswb %%mm1, %%mm0 \n\t"
  2466. "pand %%mm6, %%mm0 \n\t"
  2467. "pand %%mm5, %%mm4 \n\t"
  2468. "por %%mm4, %%mm0 \n\t"
  2469. "movq %%mm0, (%0, %3) \n\t"
  2470. "add $16, %1 \n\t"
  2471. "add %2, %0 \n\t"
  2472. " js 1b \n\t"
  2473. : "+r"(offset), "+r"(temp_sums)
  2474. : "r" ((x86_reg)step), "r"(src - offset), "m"(both_masks)
  2475. );
  2476. }else
  2477. src+= step; // src points to begin of the 8x8 Block
  2478. if(eq_mask != -1LL){
  2479. uint8_t *temp_src= src;
  2480. __asm__ volatile(
  2481. "pxor %%mm7, %%mm7 \n\t"
  2482. "lea -40(%%"REG_SP"), %%"REG_c" \n\t" // make space for 4 8-byte vars
  2483. "and "ALIGN_MASK", %%"REG_c" \n\t" // align
  2484. // 0 1 2 3 4 5 6 7 8 9
  2485. // %0 eax eax+%1 eax+2%1 %0+4%1 ecx ecx+%1 ecx+2%1 %1+8%1 ecx+4%1
  2486. "movq (%0), %%mm0 \n\t"
  2487. "movq %%mm0, %%mm1 \n\t"
  2488. "punpcklbw %%mm7, %%mm0 \n\t" // low part of line 0
  2489. "punpckhbw %%mm7, %%mm1 \n\t" // high part of line 0
  2490. "movq (%0, %1), %%mm2 \n\t"
  2491. "lea (%0, %1, 2), %%"REG_a" \n\t"
  2492. "movq %%mm2, %%mm3 \n\t"
  2493. "punpcklbw %%mm7, %%mm2 \n\t" // low part of line 1
  2494. "punpckhbw %%mm7, %%mm3 \n\t" // high part of line 1
  2495. "movq (%%"REG_a"), %%mm4 \n\t"
  2496. "movq %%mm4, %%mm5 \n\t"
  2497. "punpcklbw %%mm7, %%mm4 \n\t" // low part of line 2
  2498. "punpckhbw %%mm7, %%mm5 \n\t" // high part of line 2
  2499. "paddw %%mm0, %%mm0 \n\t" // 2L0
  2500. "paddw %%mm1, %%mm1 \n\t" // 2H0
  2501. "psubw %%mm4, %%mm2 \n\t" // L1 - L2
  2502. "psubw %%mm5, %%mm3 \n\t" // H1 - H2
  2503. "psubw %%mm2, %%mm0 \n\t" // 2L0 - L1 + L2
  2504. "psubw %%mm3, %%mm1 \n\t" // 2H0 - H1 + H2
  2505. "psllw $2, %%mm2 \n\t" // 4L1 - 4L2
  2506. "psllw $2, %%mm3 \n\t" // 4H1 - 4H2
  2507. "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2
  2508. "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2
  2509. "movq (%%"REG_a", %1), %%mm2 \n\t"
  2510. "movq %%mm2, %%mm3 \n\t"
  2511. "punpcklbw %%mm7, %%mm2 \n\t" // L3
  2512. "punpckhbw %%mm7, %%mm3 \n\t" // H3
  2513. "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - L3
  2514. "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - H3
  2515. "psubw %%mm2, %%mm0 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
  2516. "psubw %%mm3, %%mm1 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
  2517. "movq %%mm0, (%%"REG_c") \n\t" // 2L0 - 5L1 + 5L2 - 2L3
  2518. "movq %%mm1, 8(%%"REG_c") \n\t" // 2H0 - 5H1 + 5H2 - 2H3
  2519. "movq (%%"REG_a", %1, 2), %%mm0 \n\t"
  2520. "movq %%mm0, %%mm1 \n\t"
  2521. "punpcklbw %%mm7, %%mm0 \n\t" // L4
  2522. "punpckhbw %%mm7, %%mm1 \n\t" // H4
  2523. "psubw %%mm0, %%mm2 \n\t" // L3 - L4
  2524. "psubw %%mm1, %%mm3 \n\t" // H3 - H4
  2525. "movq %%mm2, 16(%%"REG_c") \n\t" // L3 - L4
  2526. "movq %%mm3, 24(%%"REG_c") \n\t" // H3 - H4
  2527. "paddw %%mm4, %%mm4 \n\t" // 2L2
  2528. "paddw %%mm5, %%mm5 \n\t" // 2H2
  2529. "psubw %%mm2, %%mm4 \n\t" // 2L2 - L3 + L4
  2530. "psubw %%mm3, %%mm5 \n\t" // 2H2 - H3 + H4
  2531. "lea (%%"REG_a", %1), %0 \n\t"
  2532. "psllw $2, %%mm2 \n\t" // 4L3 - 4L4
  2533. "psllw $2, %%mm3 \n\t" // 4H3 - 4H4
  2534. "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4
  2535. "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4
  2536. //50 opcodes so far
  2537. "movq (%0, %1, 2), %%mm2 \n\t"
  2538. "movq %%mm2, %%mm3 \n\t"
  2539. "punpcklbw %%mm7, %%mm2 \n\t" // L5
  2540. "punpckhbw %%mm7, %%mm3 \n\t" // H5
  2541. "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - L5
  2542. "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - H5
  2543. "psubw %%mm2, %%mm4 \n\t" // 2L2 - 5L3 + 5L4 - 2L5
  2544. "psubw %%mm3, %%mm5 \n\t" // 2H2 - 5H3 + 5H4 - 2H5
  2545. "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
  2546. "punpcklbw %%mm7, %%mm6 \n\t" // L6
  2547. "psubw %%mm6, %%mm2 \n\t" // L5 - L6
  2548. "movq (%%"REG_a", %1, 4), %%mm6 \n\t"
  2549. "punpckhbw %%mm7, %%mm6 \n\t" // H6
  2550. "psubw %%mm6, %%mm3 \n\t" // H5 - H6
  2551. "paddw %%mm0, %%mm0 \n\t" // 2L4
  2552. "paddw %%mm1, %%mm1 \n\t" // 2H4
  2553. "psubw %%mm2, %%mm0 \n\t" // 2L4 - L5 + L6
  2554. "psubw %%mm3, %%mm1 \n\t" // 2H4 - H5 + H6
  2555. "psllw $2, %%mm2 \n\t" // 4L5 - 4L6
  2556. "psllw $2, %%mm3 \n\t" // 4H5 - 4H6
  2557. "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6
  2558. "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6
  2559. "movq (%0, %1, 4), %%mm2 \n\t"
  2560. "movq %%mm2, %%mm3 \n\t"
  2561. "punpcklbw %%mm7, %%mm2 \n\t" // L7
  2562. "punpckhbw %%mm7, %%mm3 \n\t" // H7
  2563. "paddw %%mm2, %%mm2 \n\t" // 2L7
  2564. "paddw %%mm3, %%mm3 \n\t" // 2H7
  2565. "psubw %%mm2, %%mm0 \n\t" // 2L4 - 5L5 + 5L6 - 2L7
  2566. "psubw %%mm3, %%mm1 \n\t" // 2H4 - 5H5 + 5H6 - 2H7
  2567. "movq (%%"REG_c"), %%mm2 \n\t" // 2L0 - 5L1 + 5L2 - 2L3
  2568. "movq 8(%%"REG_c"), %%mm3 \n\t" // 2H0 - 5H1 + 5H2 - 2H3
  2569. #if HAVE_MMX2
  2570. "movq %%mm7, %%mm6 \n\t" // 0
  2571. "psubw %%mm0, %%mm6 \n\t"
  2572. "pmaxsw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
  2573. "movq %%mm7, %%mm6 \n\t" // 0
  2574. "psubw %%mm1, %%mm6 \n\t"
  2575. "pmaxsw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
  2576. "movq %%mm7, %%mm6 \n\t" // 0
  2577. "psubw %%mm2, %%mm6 \n\t"
  2578. "pmaxsw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
  2579. "movq %%mm7, %%mm6 \n\t" // 0
  2580. "psubw %%mm3, %%mm6 \n\t"
  2581. "pmaxsw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
  2582. #else
  2583. "movq %%mm7, %%mm6 \n\t" // 0
  2584. "pcmpgtw %%mm0, %%mm6 \n\t"
  2585. "pxor %%mm6, %%mm0 \n\t"
  2586. "psubw %%mm6, %%mm0 \n\t" // |2L4 - 5L5 + 5L6 - 2L7|
  2587. "movq %%mm7, %%mm6 \n\t" // 0
  2588. "pcmpgtw %%mm1, %%mm6 \n\t"
  2589. "pxor %%mm6, %%mm1 \n\t"
  2590. "psubw %%mm6, %%mm1 \n\t" // |2H4 - 5H5 + 5H6 - 2H7|
  2591. "movq %%mm7, %%mm6 \n\t" // 0
  2592. "pcmpgtw %%mm2, %%mm6 \n\t"
  2593. "pxor %%mm6, %%mm2 \n\t"
  2594. "psubw %%mm6, %%mm2 \n\t" // |2L0 - 5L1 + 5L2 - 2L3|
  2595. "movq %%mm7, %%mm6 \n\t" // 0
  2596. "pcmpgtw %%mm3, %%mm6 \n\t"
  2597. "pxor %%mm6, %%mm3 \n\t"
  2598. "psubw %%mm6, %%mm3 \n\t" // |2H0 - 5H1 + 5H2 - 2H3|
  2599. #endif
  2600. #if HAVE_MMX2
  2601. "pminsw %%mm2, %%mm0 \n\t"
  2602. "pminsw %%mm3, %%mm1 \n\t"
  2603. #else
  2604. "movq %%mm0, %%mm6 \n\t"
  2605. "psubusw %%mm2, %%mm6 \n\t"
  2606. "psubw %%mm6, %%mm0 \n\t"
  2607. "movq %%mm1, %%mm6 \n\t"
  2608. "psubusw %%mm3, %%mm6 \n\t"
  2609. "psubw %%mm6, %%mm1 \n\t"
  2610. #endif
  2611. "movd %2, %%mm2 \n\t" // QP
  2612. "punpcklbw %%mm7, %%mm2 \n\t"
  2613. "movq %%mm7, %%mm6 \n\t" // 0
  2614. "pcmpgtw %%mm4, %%mm6 \n\t" // sign(2L2 - 5L3 + 5L4 - 2L5)
  2615. "pxor %%mm6, %%mm4 \n\t"
  2616. "psubw %%mm6, %%mm4 \n\t" // |2L2 - 5L3 + 5L4 - 2L5|
  2617. "pcmpgtw %%mm5, %%mm7 \n\t" // sign(2H2 - 5H3 + 5H4 - 2H5)
  2618. "pxor %%mm7, %%mm5 \n\t"
  2619. "psubw %%mm7, %%mm5 \n\t" // |2H2 - 5H3 + 5H4 - 2H5|
  2620. // 100 opcodes
  2621. "psllw $3, %%mm2 \n\t" // 8QP
  2622. "movq %%mm2, %%mm3 \n\t" // 8QP
  2623. "pcmpgtw %%mm4, %%mm2 \n\t"
  2624. "pcmpgtw %%mm5, %%mm3 \n\t"
  2625. "pand %%mm2, %%mm4 \n\t"
  2626. "pand %%mm3, %%mm5 \n\t"
  2627. "psubusw %%mm0, %%mm4 \n\t" // hd
  2628. "psubusw %%mm1, %%mm5 \n\t" // ld
  2629. "movq "MANGLE(w05)", %%mm2 \n\t" // 5
  2630. "pmullw %%mm2, %%mm4 \n\t"
  2631. "pmullw %%mm2, %%mm5 \n\t"
  2632. "movq "MANGLE(w20)", %%mm2 \n\t" // 32
  2633. "paddw %%mm2, %%mm4 \n\t"
  2634. "paddw %%mm2, %%mm5 \n\t"
  2635. "psrlw $6, %%mm4 \n\t"
  2636. "psrlw $6, %%mm5 \n\t"
  2637. "movq 16(%%"REG_c"), %%mm0 \n\t" // L3 - L4
  2638. "movq 24(%%"REG_c"), %%mm1 \n\t" // H3 - H4
  2639. "pxor %%mm2, %%mm2 \n\t"
  2640. "pxor %%mm3, %%mm3 \n\t"
  2641. "pcmpgtw %%mm0, %%mm2 \n\t" // sign (L3-L4)
  2642. "pcmpgtw %%mm1, %%mm3 \n\t" // sign (H3-H4)
  2643. "pxor %%mm2, %%mm0 \n\t"
  2644. "pxor %%mm3, %%mm1 \n\t"
  2645. "psubw %%mm2, %%mm0 \n\t" // |L3-L4|
  2646. "psubw %%mm3, %%mm1 \n\t" // |H3-H4|
  2647. "psrlw $1, %%mm0 \n\t" // |L3 - L4|/2
  2648. "psrlw $1, %%mm1 \n\t" // |H3 - H4|/2
  2649. "pxor %%mm6, %%mm2 \n\t"
  2650. "pxor %%mm7, %%mm3 \n\t"
  2651. "pand %%mm2, %%mm4 \n\t"
  2652. "pand %%mm3, %%mm5 \n\t"
  2653. #if HAVE_MMX2
  2654. "pminsw %%mm0, %%mm4 \n\t"
  2655. "pminsw %%mm1, %%mm5 \n\t"
  2656. #else
  2657. "movq %%mm4, %%mm2 \n\t"
  2658. "psubusw %%mm0, %%mm2 \n\t"
  2659. "psubw %%mm2, %%mm4 \n\t"
  2660. "movq %%mm5, %%mm2 \n\t"
  2661. "psubusw %%mm1, %%mm2 \n\t"
  2662. "psubw %%mm2, %%mm5 \n\t"
  2663. #endif
  2664. "pxor %%mm6, %%mm4 \n\t"
  2665. "pxor %%mm7, %%mm5 \n\t"
  2666. "psubw %%mm6, %%mm4 \n\t"
  2667. "psubw %%mm7, %%mm5 \n\t"
  2668. "packsswb %%mm5, %%mm4 \n\t"
  2669. "movq %3, %%mm1 \n\t"
  2670. "pandn %%mm4, %%mm1 \n\t"
  2671. "movq (%0), %%mm0 \n\t"
  2672. "paddb %%mm1, %%mm0 \n\t"
  2673. "movq %%mm0, (%0) \n\t"
  2674. "movq (%0, %1), %%mm0 \n\t"
  2675. "psubb %%mm1, %%mm0 \n\t"
  2676. "movq %%mm0, (%0, %1) \n\t"
  2677. : "+r" (temp_src)
  2678. : "r" ((x86_reg)step), "m" (c->pQPb), "m"(eq_mask)
  2679. : "%"REG_a, "%"REG_c
  2680. );
  2681. }
  2682. /*if(step==16){
  2683. STOP_TIMER("step16")
  2684. }else{
  2685. STOP_TIMER("stepX")
  2686. }*/
  2687. }
  2688. #endif //HAVE_MMX
  2689. static void RENAME(postProcess)(const uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  2690. const QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c);
  2691. /**
  2692. * Copies a block from src to dst and fixes the blacklevel.
  2693. * levelFix == 0 -> do not touch the brighness & contrast
  2694. */
  2695. #undef REAL_SCALED_CPY
  2696. #undef SCALED_CPY
  2697. static inline void RENAME(blockCopy)(uint8_t dst[], int dstStride, const uint8_t src[], int srcStride,
  2698. int levelFix, int64_t *packedOffsetAndScale)
  2699. {
  2700. #if !HAVE_MMX
  2701. int i;
  2702. #endif
  2703. if(levelFix){
  2704. #if HAVE_MMX
  2705. __asm__ volatile(
  2706. "movq (%%"REG_a"), %%mm2 \n\t" // packedYOffset
  2707. "movq 8(%%"REG_a"), %%mm3 \n\t" // packedYScale
  2708. "lea (%2,%4), %%"REG_a" \n\t"
  2709. "lea (%3,%5), %%"REG_d" \n\t"
  2710. "pxor %%mm4, %%mm4 \n\t"
  2711. #if HAVE_MMX2
  2712. #define REAL_SCALED_CPY(src1, src2, dst1, dst2) \
  2713. "movq " #src1 ", %%mm0 \n\t"\
  2714. "movq " #src1 ", %%mm5 \n\t"\
  2715. "movq " #src2 ", %%mm1 \n\t"\
  2716. "movq " #src2 ", %%mm6 \n\t"\
  2717. "punpcklbw %%mm0, %%mm0 \n\t"\
  2718. "punpckhbw %%mm5, %%mm5 \n\t"\
  2719. "punpcklbw %%mm1, %%mm1 \n\t"\
  2720. "punpckhbw %%mm6, %%mm6 \n\t"\
  2721. "pmulhuw %%mm3, %%mm0 \n\t"\
  2722. "pmulhuw %%mm3, %%mm5 \n\t"\
  2723. "pmulhuw %%mm3, %%mm1 \n\t"\
  2724. "pmulhuw %%mm3, %%mm6 \n\t"\
  2725. "psubw %%mm2, %%mm0 \n\t"\
  2726. "psubw %%mm2, %%mm5 \n\t"\
  2727. "psubw %%mm2, %%mm1 \n\t"\
  2728. "psubw %%mm2, %%mm6 \n\t"\
  2729. "packuswb %%mm5, %%mm0 \n\t"\
  2730. "packuswb %%mm6, %%mm1 \n\t"\
  2731. "movq %%mm0, " #dst1 " \n\t"\
  2732. "movq %%mm1, " #dst2 " \n\t"\
  2733. #else //HAVE_MMX2
  2734. #define REAL_SCALED_CPY(src1, src2, dst1, dst2) \
  2735. "movq " #src1 ", %%mm0 \n\t"\
  2736. "movq " #src1 ", %%mm5 \n\t"\
  2737. "punpcklbw %%mm4, %%mm0 \n\t"\
  2738. "punpckhbw %%mm4, %%mm5 \n\t"\
  2739. "psubw %%mm2, %%mm0 \n\t"\
  2740. "psubw %%mm2, %%mm5 \n\t"\
  2741. "movq " #src2 ", %%mm1 \n\t"\
  2742. "psllw $6, %%mm0 \n\t"\
  2743. "psllw $6, %%mm5 \n\t"\
  2744. "pmulhw %%mm3, %%mm0 \n\t"\
  2745. "movq " #src2 ", %%mm6 \n\t"\
  2746. "pmulhw %%mm3, %%mm5 \n\t"\
  2747. "punpcklbw %%mm4, %%mm1 \n\t"\
  2748. "punpckhbw %%mm4, %%mm6 \n\t"\
  2749. "psubw %%mm2, %%mm1 \n\t"\
  2750. "psubw %%mm2, %%mm6 \n\t"\
  2751. "psllw $6, %%mm1 \n\t"\
  2752. "psllw $6, %%mm6 \n\t"\
  2753. "pmulhw %%mm3, %%mm1 \n\t"\
  2754. "pmulhw %%mm3, %%mm6 \n\t"\
  2755. "packuswb %%mm5, %%mm0 \n\t"\
  2756. "packuswb %%mm6, %%mm1 \n\t"\
  2757. "movq %%mm0, " #dst1 " \n\t"\
  2758. "movq %%mm1, " #dst2 " \n\t"\
  2759. #endif //HAVE_MMX2
  2760. #define SCALED_CPY(src1, src2, dst1, dst2)\
  2761. REAL_SCALED_CPY(src1, src2, dst1, dst2)
  2762. SCALED_CPY((%2) , (%2, %4) , (%3) , (%3, %5))
  2763. SCALED_CPY((%2, %4, 2), (%%REGa, %4, 2), (%3, %5, 2), (%%REGd, %5, 2))
  2764. SCALED_CPY((%2, %4, 4), (%%REGa, %4, 4), (%3, %5, 4), (%%REGd, %5, 4))
  2765. "lea (%%"REG_a",%4,4), %%"REG_a" \n\t"
  2766. "lea (%%"REG_d",%5,4), %%"REG_d" \n\t"
  2767. SCALED_CPY((%%REGa, %4), (%%REGa, %4, 2), (%%REGd, %5), (%%REGd, %5, 2))
  2768. : "=&a" (packedOffsetAndScale)
  2769. : "0" (packedOffsetAndScale),
  2770. "r"(src),
  2771. "r"(dst),
  2772. "r" ((x86_reg)srcStride),
  2773. "r" ((x86_reg)dstStride)
  2774. : "%"REG_d
  2775. );
  2776. #else //HAVE_MMX
  2777. for(i=0; i<8; i++)
  2778. memcpy( &(dst[dstStride*i]),
  2779. &(src[srcStride*i]), BLOCK_SIZE);
  2780. #endif //HAVE_MMX
  2781. }else{
  2782. #if HAVE_MMX
  2783. __asm__ volatile(
  2784. "lea (%0,%2), %%"REG_a" \n\t"
  2785. "lea (%1,%3), %%"REG_d" \n\t"
  2786. #define REAL_SIMPLE_CPY(src1, src2, dst1, dst2) \
  2787. "movq " #src1 ", %%mm0 \n\t"\
  2788. "movq " #src2 ", %%mm1 \n\t"\
  2789. "movq %%mm0, " #dst1 " \n\t"\
  2790. "movq %%mm1, " #dst2 " \n\t"\
  2791. #define SIMPLE_CPY(src1, src2, dst1, dst2)\
  2792. REAL_SIMPLE_CPY(src1, src2, dst1, dst2)
  2793. SIMPLE_CPY((%0) , (%0, %2) , (%1) , (%1, %3))
  2794. SIMPLE_CPY((%0, %2, 2), (%%REGa, %2, 2), (%1, %3, 2), (%%REGd, %3, 2))
  2795. SIMPLE_CPY((%0, %2, 4), (%%REGa, %2, 4), (%1, %3, 4), (%%REGd, %3, 4))
  2796. "lea (%%"REG_a",%2,4), %%"REG_a" \n\t"
  2797. "lea (%%"REG_d",%3,4), %%"REG_d" \n\t"
  2798. SIMPLE_CPY((%%REGa, %2), (%%REGa, %2, 2), (%%REGd, %3), (%%REGd, %3, 2))
  2799. : : "r" (src),
  2800. "r" (dst),
  2801. "r" ((x86_reg)srcStride),
  2802. "r" ((x86_reg)dstStride)
  2803. : "%"REG_a, "%"REG_d
  2804. );
  2805. #else //HAVE_MMX
  2806. for(i=0; i<8; i++)
  2807. memcpy( &(dst[dstStride*i]),
  2808. &(src[srcStride*i]), BLOCK_SIZE);
  2809. #endif //HAVE_MMX
  2810. }
  2811. }
  2812. /**
  2813. * Duplicates the given 8 src pixels ? times upward
  2814. */
  2815. static inline void RENAME(duplicate)(uint8_t src[], int stride)
  2816. {
  2817. #if HAVE_MMX
  2818. __asm__ volatile(
  2819. "movq (%0), %%mm0 \n\t"
  2820. "add %1, %0 \n\t"
  2821. "movq %%mm0, (%0) \n\t"
  2822. "movq %%mm0, (%0, %1) \n\t"
  2823. "movq %%mm0, (%0, %1, 2) \n\t"
  2824. : "+r" (src)
  2825. : "r" ((x86_reg)-stride)
  2826. );
  2827. #else
  2828. int i;
  2829. uint8_t *p=src;
  2830. for(i=0; i<3; i++){
  2831. p-= stride;
  2832. memcpy(p, src, 8);
  2833. }
  2834. #endif
  2835. }
  2836. /**
  2837. * Filters array of bytes (Y or U or V values)
  2838. */
  2839. static void RENAME(postProcess)(const uint8_t src[], int srcStride, uint8_t dst[], int dstStride, int width, int height,
  2840. const QP_STORE_T QPs[], int QPStride, int isColor, PPContext *c2)
  2841. {
  2842. DECLARE_ALIGNED(8, PPContext, c)= *c2; //copy to stack for faster access
  2843. int x,y;
  2844. #ifdef COMPILE_TIME_MODE
  2845. const int mode= COMPILE_TIME_MODE;
  2846. #else
  2847. const int mode= isColor ? c.ppMode.chromMode : c.ppMode.lumMode;
  2848. #endif
  2849. int black=0, white=255; // blackest black and whitest white in the picture
  2850. int QPCorrecture= 256*256;
  2851. int copyAhead;
  2852. #if HAVE_MMX
  2853. int i;
  2854. #endif
  2855. const int qpHShift= isColor ? 4-c.hChromaSubSample : 4;
  2856. const int qpVShift= isColor ? 4-c.vChromaSubSample : 4;
  2857. //FIXME remove
  2858. uint64_t * const yHistogram= c.yHistogram;
  2859. uint8_t * const tempSrc= srcStride > 0 ? c.tempSrc : c.tempSrc - 23*srcStride;
  2860. uint8_t * const tempDst= dstStride > 0 ? c.tempDst : c.tempDst - 23*dstStride;
  2861. //const int mbWidth= isColor ? (width+7)>>3 : (width+15)>>4;
  2862. #if HAVE_MMX
  2863. for(i=0; i<57; i++){
  2864. int offset= ((i*c.ppMode.baseDcDiff)>>8) + 1;
  2865. int threshold= offset*2 + 1;
  2866. c.mmxDcOffset[i]= 0x7F - offset;
  2867. c.mmxDcThreshold[i]= 0x7F - threshold;
  2868. c.mmxDcOffset[i]*= 0x0101010101010101LL;
  2869. c.mmxDcThreshold[i]*= 0x0101010101010101LL;
  2870. }
  2871. #endif
  2872. if(mode & CUBIC_IPOL_DEINT_FILTER) copyAhead=16;
  2873. else if( (mode & LINEAR_BLEND_DEINT_FILTER)
  2874. || (mode & FFMPEG_DEINT_FILTER)
  2875. || (mode & LOWPASS5_DEINT_FILTER)) copyAhead=14;
  2876. else if( (mode & V_DEBLOCK)
  2877. || (mode & LINEAR_IPOL_DEINT_FILTER)
  2878. || (mode & MEDIAN_DEINT_FILTER)
  2879. || (mode & V_A_DEBLOCK)) copyAhead=13;
  2880. else if(mode & V_X1_FILTER) copyAhead=11;
  2881. // else if(mode & V_RK1_FILTER) copyAhead=10;
  2882. else if(mode & DERING) copyAhead=9;
  2883. else copyAhead=8;
  2884. copyAhead-= 8;
  2885. if(!isColor){
  2886. uint64_t sum= 0;
  2887. int i;
  2888. uint64_t maxClipped;
  2889. uint64_t clipped;
  2890. double scale;
  2891. c.frameNum++;
  2892. // first frame is fscked so we ignore it
  2893. if(c.frameNum == 1) yHistogram[0]= width*height/64*15/256;
  2894. for(i=0; i<256; i++){
  2895. sum+= yHistogram[i];
  2896. }
  2897. /* We always get a completely black picture first. */
  2898. maxClipped= (uint64_t)(sum * c.ppMode.maxClippedThreshold);
  2899. clipped= sum;
  2900. for(black=255; black>0; black--){
  2901. if(clipped < maxClipped) break;
  2902. clipped-= yHistogram[black];
  2903. }
  2904. clipped= sum;
  2905. for(white=0; white<256; white++){
  2906. if(clipped < maxClipped) break;
  2907. clipped-= yHistogram[white];
  2908. }
  2909. scale= (double)(c.ppMode.maxAllowedY - c.ppMode.minAllowedY) / (double)(white-black);
  2910. #if HAVE_MMX2
  2911. c.packedYScale= (uint16_t)(scale*256.0 + 0.5);
  2912. c.packedYOffset= (((black*c.packedYScale)>>8) - c.ppMode.minAllowedY) & 0xFFFF;
  2913. #else
  2914. c.packedYScale= (uint16_t)(scale*1024.0 + 0.5);
  2915. c.packedYOffset= (black - c.ppMode.minAllowedY) & 0xFFFF;
  2916. #endif
  2917. c.packedYOffset|= c.packedYOffset<<32;
  2918. c.packedYOffset|= c.packedYOffset<<16;
  2919. c.packedYScale|= c.packedYScale<<32;
  2920. c.packedYScale|= c.packedYScale<<16;
  2921. if(mode & LEVEL_FIX) QPCorrecture= (int)(scale*256*256 + 0.5);
  2922. else QPCorrecture= 256*256;
  2923. }else{
  2924. c.packedYScale= 0x0100010001000100LL;
  2925. c.packedYOffset= 0;
  2926. QPCorrecture= 256*256;
  2927. }
  2928. /* copy & deinterlace first row of blocks */
  2929. y=-BLOCK_SIZE;
  2930. {
  2931. const uint8_t *srcBlock= &(src[y*srcStride]);
  2932. uint8_t *dstBlock= tempDst + dstStride;
  2933. // From this point on it is guaranteed that we can read and write 16 lines downward
  2934. // finish 1 block before the next otherwise we might have a problem
  2935. // with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
  2936. for(x=0; x<width; x+=BLOCK_SIZE){
  2937. #if HAVE_MMX2
  2938. /*
  2939. prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
  2940. prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
  2941. prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
  2942. prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
  2943. */
  2944. __asm__(
  2945. "mov %4, %%"REG_a" \n\t"
  2946. "shr $2, %%"REG_a" \n\t"
  2947. "and $6, %%"REG_a" \n\t"
  2948. "add %5, %%"REG_a" \n\t"
  2949. "mov %%"REG_a", %%"REG_d" \n\t"
  2950. "imul %1, %%"REG_a" \n\t"
  2951. "imul %3, %%"REG_d" \n\t"
  2952. "prefetchnta 32(%%"REG_a", %0) \n\t"
  2953. "prefetcht0 32(%%"REG_d", %2) \n\t"
  2954. "add %1, %%"REG_a" \n\t"
  2955. "add %3, %%"REG_d" \n\t"
  2956. "prefetchnta 32(%%"REG_a", %0) \n\t"
  2957. "prefetcht0 32(%%"REG_d", %2) \n\t"
  2958. :: "r" (srcBlock), "r" ((x86_reg)srcStride), "r" (dstBlock), "r" ((x86_reg)dstStride),
  2959. "g" ((x86_reg)x), "g" ((x86_reg)copyAhead)
  2960. : "%"REG_a, "%"REG_d
  2961. );
  2962. #elif HAVE_AMD3DNOW
  2963. //FIXME check if this is faster on an 3dnow chip or if it is faster without the prefetch or ...
  2964. /* prefetch(srcBlock + (((x>>3)&3) + 5)*srcStride + 32);
  2965. prefetch(srcBlock + (((x>>3)&3) + 9)*srcStride + 32);
  2966. prefetchw(dstBlock + (((x>>3)&3) + 5)*dstStride + 32);
  2967. prefetchw(dstBlock + (((x>>3)&3) + 9)*dstStride + 32);
  2968. */
  2969. #endif
  2970. RENAME(blockCopy)(dstBlock + dstStride*8, dstStride,
  2971. srcBlock + srcStride*8, srcStride, mode & LEVEL_FIX, &c.packedYOffset);
  2972. RENAME(duplicate)(dstBlock + dstStride*8, dstStride);
  2973. if(mode & LINEAR_IPOL_DEINT_FILTER)
  2974. RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
  2975. else if(mode & LINEAR_BLEND_DEINT_FILTER)
  2976. RENAME(deInterlaceBlendLinear)(dstBlock, dstStride, c.deintTemp + x);
  2977. else if(mode & MEDIAN_DEINT_FILTER)
  2978. RENAME(deInterlaceMedian)(dstBlock, dstStride);
  2979. else if(mode & CUBIC_IPOL_DEINT_FILTER)
  2980. RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
  2981. else if(mode & FFMPEG_DEINT_FILTER)
  2982. RENAME(deInterlaceFF)(dstBlock, dstStride, c.deintTemp + x);
  2983. else if(mode & LOWPASS5_DEINT_FILTER)
  2984. RENAME(deInterlaceL5)(dstBlock, dstStride, c.deintTemp + x, c.deintTemp + width + x);
  2985. /* else if(mode & CUBIC_BLEND_DEINT_FILTER)
  2986. RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
  2987. */
  2988. dstBlock+=8;
  2989. srcBlock+=8;
  2990. }
  2991. if(width==FFABS(dstStride))
  2992. linecpy(dst, tempDst + 9*dstStride, copyAhead, dstStride);
  2993. else{
  2994. int i;
  2995. for(i=0; i<copyAhead; i++){
  2996. memcpy(dst + i*dstStride, tempDst + (9+i)*dstStride, width);
  2997. }
  2998. }
  2999. }
  3000. for(y=0; y<height; y+=BLOCK_SIZE){
  3001. //1% speedup if these are here instead of the inner loop
  3002. const uint8_t *srcBlock= &(src[y*srcStride]);
  3003. uint8_t *dstBlock= &(dst[y*dstStride]);
  3004. #if HAVE_MMX
  3005. uint8_t *tempBlock1= c.tempBlocks;
  3006. uint8_t *tempBlock2= c.tempBlocks + 8;
  3007. #endif
  3008. const int8_t *QPptr= &QPs[(y>>qpVShift)*QPStride];
  3009. int8_t *nonBQPptr= &c.nonBQPTable[(y>>qpVShift)*FFABS(QPStride)];
  3010. int QP=0;
  3011. /* can we mess with a 8x16 block from srcBlock/dstBlock downwards and 1 line upwards
  3012. if not than use a temporary buffer */
  3013. if(y+15 >= height){
  3014. int i;
  3015. /* copy from line (copyAhead) to (copyAhead+7) of src, these will be copied with
  3016. blockcopy to dst later */
  3017. linecpy(tempSrc + srcStride*copyAhead, srcBlock + srcStride*copyAhead,
  3018. FFMAX(height-y-copyAhead, 0), srcStride);
  3019. /* duplicate last line of src to fill the void upto line (copyAhead+7) */
  3020. for(i=FFMAX(height-y, 8); i<copyAhead+8; i++)
  3021. memcpy(tempSrc + srcStride*i, src + srcStride*(height-1), FFABS(srcStride));
  3022. /* copy up to (copyAhead+1) lines of dst (line -1 to (copyAhead-1))*/
  3023. linecpy(tempDst, dstBlock - dstStride, FFMIN(height-y+1, copyAhead+1), dstStride);
  3024. /* duplicate last line of dst to fill the void upto line (copyAhead) */
  3025. for(i=height-y+1; i<=copyAhead; i++)
  3026. memcpy(tempDst + dstStride*i, dst + dstStride*(height-1), FFABS(dstStride));
  3027. dstBlock= tempDst + dstStride;
  3028. srcBlock= tempSrc;
  3029. }
  3030. // From this point on it is guaranteed that we can read and write 16 lines downward
  3031. // finish 1 block before the next otherwise we might have a problem
  3032. // with the L1 Cache of the P4 ... or only a few blocks at a time or soemthing
  3033. for(x=0; x<width; x+=BLOCK_SIZE){
  3034. const int stride= dstStride;
  3035. #if HAVE_MMX
  3036. uint8_t *tmpXchg;
  3037. #endif
  3038. if(isColor){
  3039. QP= QPptr[x>>qpHShift];
  3040. c.nonBQP= nonBQPptr[x>>qpHShift];
  3041. }else{
  3042. QP= QPptr[x>>4];
  3043. QP= (QP* QPCorrecture + 256*128)>>16;
  3044. c.nonBQP= nonBQPptr[x>>4];
  3045. c.nonBQP= (c.nonBQP* QPCorrecture + 256*128)>>16;
  3046. yHistogram[ srcBlock[srcStride*12 + 4] ]++;
  3047. }
  3048. c.QP= QP;
  3049. #if HAVE_MMX
  3050. __asm__ volatile(
  3051. "movd %1, %%mm7 \n\t"
  3052. "packuswb %%mm7, %%mm7 \n\t" // 0, 0, 0, QP, 0, 0, 0, QP
  3053. "packuswb %%mm7, %%mm7 \n\t" // 0,QP, 0, QP, 0,QP, 0, QP
  3054. "packuswb %%mm7, %%mm7 \n\t" // QP,..., QP
  3055. "movq %%mm7, %0 \n\t"
  3056. : "=m" (c.pQPb)
  3057. : "r" (QP)
  3058. );
  3059. #endif
  3060. #if HAVE_MMX2
  3061. /*
  3062. prefetchnta(srcBlock + (((x>>2)&6) + 5)*srcStride + 32);
  3063. prefetchnta(srcBlock + (((x>>2)&6) + 6)*srcStride + 32);
  3064. prefetcht0(dstBlock + (((x>>2)&6) + 5)*dstStride + 32);
  3065. prefetcht0(dstBlock + (((x>>2)&6) + 6)*dstStride + 32);
  3066. */
  3067. __asm__(
  3068. "mov %4, %%"REG_a" \n\t"
  3069. "shr $2, %%"REG_a" \n\t"
  3070. "and $6, %%"REG_a" \n\t"
  3071. "add %5, %%"REG_a" \n\t"
  3072. "mov %%"REG_a", %%"REG_d" \n\t"
  3073. "imul %1, %%"REG_a" \n\t"
  3074. "imul %3, %%"REG_d" \n\t"
  3075. "prefetchnta 32(%%"REG_a", %0) \n\t"
  3076. "prefetcht0 32(%%"REG_d", %2) \n\t"
  3077. "add %1, %%"REG_a" \n\t"
  3078. "add %3, %%"REG_d" \n\t"
  3079. "prefetchnta 32(%%"REG_a", %0) \n\t"
  3080. "prefetcht0 32(%%"REG_d", %2) \n\t"
  3081. :: "r" (srcBlock), "r" ((x86_reg)srcStride), "r" (dstBlock), "r" ((x86_reg)dstStride),
  3082. "g" ((x86_reg)x), "g" ((x86_reg)copyAhead)
  3083. : "%"REG_a, "%"REG_d
  3084. );
  3085. #elif HAVE_AMD3DNOW
  3086. //FIXME check if this is faster on an 3dnow chip or if it is faster without the prefetch or ...
  3087. /* prefetch(srcBlock + (((x>>3)&3) + 5)*srcStride + 32);
  3088. prefetch(srcBlock + (((x>>3)&3) + 9)*srcStride + 32);
  3089. prefetchw(dstBlock + (((x>>3)&3) + 5)*dstStride + 32);
  3090. prefetchw(dstBlock + (((x>>3)&3) + 9)*dstStride + 32);
  3091. */
  3092. #endif
  3093. RENAME(blockCopy)(dstBlock + dstStride*copyAhead, dstStride,
  3094. srcBlock + srcStride*copyAhead, srcStride, mode & LEVEL_FIX, &c.packedYOffset);
  3095. if(mode & LINEAR_IPOL_DEINT_FILTER)
  3096. RENAME(deInterlaceInterpolateLinear)(dstBlock, dstStride);
  3097. else if(mode & LINEAR_BLEND_DEINT_FILTER)
  3098. RENAME(deInterlaceBlendLinear)(dstBlock, dstStride, c.deintTemp + x);
  3099. else if(mode & MEDIAN_DEINT_FILTER)
  3100. RENAME(deInterlaceMedian)(dstBlock, dstStride);
  3101. else if(mode & CUBIC_IPOL_DEINT_FILTER)
  3102. RENAME(deInterlaceInterpolateCubic)(dstBlock, dstStride);
  3103. else if(mode & FFMPEG_DEINT_FILTER)
  3104. RENAME(deInterlaceFF)(dstBlock, dstStride, c.deintTemp + x);
  3105. else if(mode & LOWPASS5_DEINT_FILTER)
  3106. RENAME(deInterlaceL5)(dstBlock, dstStride, c.deintTemp + x, c.deintTemp + width + x);
  3107. /* else if(mode & CUBIC_BLEND_DEINT_FILTER)
  3108. RENAME(deInterlaceBlendCubic)(dstBlock, dstStride);
  3109. */
  3110. /* only deblock if we have 2 blocks */
  3111. if(y + 8 < height){
  3112. if(mode & V_X1_FILTER)
  3113. RENAME(vertX1Filter)(dstBlock, stride, &c);
  3114. else if(mode & V_DEBLOCK){
  3115. const int t= RENAME(vertClassify)(dstBlock, stride, &c);
  3116. if(t==1)
  3117. RENAME(doVertLowPass)(dstBlock, stride, &c);
  3118. else if(t==2)
  3119. RENAME(doVertDefFilter)(dstBlock, stride, &c);
  3120. }else if(mode & V_A_DEBLOCK){
  3121. RENAME(do_a_deblock)(dstBlock, stride, 1, &c);
  3122. }
  3123. }
  3124. #if HAVE_MMX
  3125. RENAME(transpose1)(tempBlock1, tempBlock2, dstBlock, dstStride);
  3126. #endif
  3127. /* check if we have a previous block to deblock it with dstBlock */
  3128. if(x - 8 >= 0){
  3129. #if HAVE_MMX
  3130. if(mode & H_X1_FILTER)
  3131. RENAME(vertX1Filter)(tempBlock1, 16, &c);
  3132. else if(mode & H_DEBLOCK){
  3133. //START_TIMER
  3134. const int t= RENAME(vertClassify)(tempBlock1, 16, &c);
  3135. //STOP_TIMER("dc & minmax")
  3136. if(t==1)
  3137. RENAME(doVertLowPass)(tempBlock1, 16, &c);
  3138. else if(t==2)
  3139. RENAME(doVertDefFilter)(tempBlock1, 16, &c);
  3140. }else if(mode & H_A_DEBLOCK){
  3141. RENAME(do_a_deblock)(tempBlock1, 16, 1, &c);
  3142. }
  3143. RENAME(transpose2)(dstBlock-4, dstStride, tempBlock1 + 4*16);
  3144. #else
  3145. if(mode & H_X1_FILTER)
  3146. horizX1Filter(dstBlock-4, stride, QP);
  3147. else if(mode & H_DEBLOCK){
  3148. #if HAVE_ALTIVEC
  3149. DECLARE_ALIGNED(16, unsigned char, tempBlock)[272];
  3150. transpose_16x8_char_toPackedAlign_altivec(tempBlock, dstBlock - (4 + 1), stride);
  3151. const int t=vertClassify_altivec(tempBlock-48, 16, &c);
  3152. if(t==1) {
  3153. doVertLowPass_altivec(tempBlock-48, 16, &c);
  3154. transpose_8x16_char_fromPackedAlign_altivec(dstBlock - (4 + 1), tempBlock, stride);
  3155. }
  3156. else if(t==2) {
  3157. doVertDefFilter_altivec(tempBlock-48, 16, &c);
  3158. transpose_8x16_char_fromPackedAlign_altivec(dstBlock - (4 + 1), tempBlock, stride);
  3159. }
  3160. #else
  3161. const int t= RENAME(horizClassify)(dstBlock-4, stride, &c);
  3162. if(t==1)
  3163. RENAME(doHorizLowPass)(dstBlock-4, stride, &c);
  3164. else if(t==2)
  3165. RENAME(doHorizDefFilter)(dstBlock-4, stride, &c);
  3166. #endif
  3167. }else if(mode & H_A_DEBLOCK){
  3168. RENAME(do_a_deblock)(dstBlock-8, 1, stride, &c);
  3169. }
  3170. #endif //HAVE_MMX
  3171. if(mode & DERING){
  3172. //FIXME filter first line
  3173. if(y>0) RENAME(dering)(dstBlock - stride - 8, stride, &c);
  3174. }
  3175. if(mode & TEMP_NOISE_FILTER)
  3176. {
  3177. RENAME(tempNoiseReducer)(dstBlock-8, stride,
  3178. c.tempBlurred[isColor] + y*dstStride + x,
  3179. c.tempBlurredPast[isColor] + (y>>3)*256 + (x>>3),
  3180. c.ppMode.maxTmpNoise);
  3181. }
  3182. }
  3183. dstBlock+=8;
  3184. srcBlock+=8;
  3185. #if HAVE_MMX
  3186. tmpXchg= tempBlock1;
  3187. tempBlock1= tempBlock2;
  3188. tempBlock2 = tmpXchg;
  3189. #endif
  3190. }
  3191. if(mode & DERING){
  3192. if(y > 0) RENAME(dering)(dstBlock - dstStride - 8, dstStride, &c);
  3193. }
  3194. if((mode & TEMP_NOISE_FILTER)){
  3195. RENAME(tempNoiseReducer)(dstBlock-8, dstStride,
  3196. c.tempBlurred[isColor] + y*dstStride + x,
  3197. c.tempBlurredPast[isColor] + (y>>3)*256 + (x>>3),
  3198. c.ppMode.maxTmpNoise);
  3199. }
  3200. /* did we use a tmp buffer for the last lines*/
  3201. if(y+15 >= height){
  3202. uint8_t *dstBlock= &(dst[y*dstStride]);
  3203. if(width==FFABS(dstStride))
  3204. linecpy(dstBlock, tempDst + dstStride, height-y, dstStride);
  3205. else{
  3206. int i;
  3207. for(i=0; i<height-y; i++){
  3208. memcpy(dstBlock + i*dstStride, tempDst + (i+1)*dstStride, width);
  3209. }
  3210. }
  3211. }
  3212. /*
  3213. for(x=0; x<width; x+=32){
  3214. volatile int i;
  3215. i+= + dstBlock[x + 7*dstStride] + dstBlock[x + 8*dstStride]
  3216. + dstBlock[x + 9*dstStride] + dstBlock[x +10*dstStride]
  3217. + dstBlock[x +11*dstStride] + dstBlock[x +12*dstStride];
  3218. + dstBlock[x +13*dstStride]
  3219. + dstBlock[x +14*dstStride] + dstBlock[x +15*dstStride];
  3220. }*/
  3221. }
  3222. #if HAVE_AMD3DNOW
  3223. __asm__ volatile("femms");
  3224. #elif HAVE_MMX
  3225. __asm__ volatile("emms");
  3226. #endif
  3227. #ifdef DEBUG_BRIGHTNESS
  3228. if(!isColor){
  3229. int max=1;
  3230. int i;
  3231. for(i=0; i<256; i++)
  3232. if(yHistogram[i] > max) max=yHistogram[i];
  3233. for(i=1; i<256; i++){
  3234. int x;
  3235. int start=yHistogram[i-1]/(max/256+1);
  3236. int end=yHistogram[i]/(max/256+1);
  3237. int inc= end > start ? 1 : -1;
  3238. for(x=start; x!=end+inc; x+=inc)
  3239. dst[ i*dstStride + x]+=128;
  3240. }
  3241. for(i=0; i<100; i+=2){
  3242. dst[ (white)*dstStride + i]+=128;
  3243. dst[ (black)*dstStride + i]+=128;
  3244. }
  3245. }
  3246. #endif
  3247. *c2= c; //copy local context back
  3248. }