postprocess_template.c 139 KB

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