rgb2rgb_template.c 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944
  1. /*
  2. * software RGB to RGB converter
  3. * pluralize by software PAL8 to RGB converter
  4. * software YUV to YUV converter
  5. * software YUV to RGB converter
  6. * Written by Nick Kurshev.
  7. * palette & YUV & runtime CPU stuff by Michael (michaelni@gmx.at)
  8. * lot of big-endian byte order fixes by Alex Beregszaszi
  9. *
  10. * This file is part of FFmpeg.
  11. *
  12. * FFmpeg is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU Lesser General Public
  14. * License as published by the Free Software Foundation; either
  15. * version 2.1 of the License, or (at your option) any later version.
  16. *
  17. * FFmpeg is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  20. * Lesser General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Lesser General Public
  23. * License along with FFmpeg; if not, write to the Free Software
  24. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  25. */
  26. #include <stddef.h>
  27. #undef PREFETCH
  28. #undef MOVNTQ
  29. #undef EMMS
  30. #undef SFENCE
  31. #undef MMREG_SIZE
  32. #undef PAVGB
  33. #if COMPILE_TEMPLATE_SSE2
  34. #define MMREG_SIZE 16
  35. #else
  36. #define MMREG_SIZE 8
  37. #endif
  38. #if COMPILE_TEMPLATE_AMD3DNOW
  39. #define PREFETCH "prefetch"
  40. #define PAVGB "pavgusb"
  41. #elif COMPILE_TEMPLATE_MMX2
  42. #define PREFETCH "prefetchnta"
  43. #define PAVGB "pavgb"
  44. #else
  45. #define PREFETCH " # nop"
  46. #endif
  47. #if COMPILE_TEMPLATE_AMD3DNOW
  48. /* On K6 femms is faster than emms. On K7 femms is directly mapped to emms. */
  49. #define EMMS "femms"
  50. #else
  51. #define EMMS "emms"
  52. #endif
  53. #if COMPILE_TEMPLATE_MMX2
  54. #define MOVNTQ "movntq"
  55. #define SFENCE "sfence"
  56. #else
  57. #define MOVNTQ "movq"
  58. #define SFENCE " # nop"
  59. #endif
  60. static inline void RENAME(rgb24tobgr32)(const uint8_t *src, uint8_t *dst, long src_size)
  61. {
  62. uint8_t *dest = dst;
  63. const uint8_t *s = src;
  64. const uint8_t *end;
  65. #if COMPILE_TEMPLATE_MMX
  66. const uint8_t *mm_end;
  67. #endif
  68. end = s + src_size;
  69. #if COMPILE_TEMPLATE_MMX
  70. __asm__ volatile(PREFETCH" %0"::"m"(*s):"memory");
  71. mm_end = end - 23;
  72. __asm__ volatile("movq %0, %%mm7"::"m"(mask32a):"memory");
  73. while (s < mm_end) {
  74. __asm__ volatile(
  75. PREFETCH" 32%1 \n\t"
  76. "movd %1, %%mm0 \n\t"
  77. "punpckldq 3%1, %%mm0 \n\t"
  78. "movd 6%1, %%mm1 \n\t"
  79. "punpckldq 9%1, %%mm1 \n\t"
  80. "movd 12%1, %%mm2 \n\t"
  81. "punpckldq 15%1, %%mm2 \n\t"
  82. "movd 18%1, %%mm3 \n\t"
  83. "punpckldq 21%1, %%mm3 \n\t"
  84. "por %%mm7, %%mm0 \n\t"
  85. "por %%mm7, %%mm1 \n\t"
  86. "por %%mm7, %%mm2 \n\t"
  87. "por %%mm7, %%mm3 \n\t"
  88. MOVNTQ" %%mm0, %0 \n\t"
  89. MOVNTQ" %%mm1, 8%0 \n\t"
  90. MOVNTQ" %%mm2, 16%0 \n\t"
  91. MOVNTQ" %%mm3, 24%0"
  92. :"=m"(*dest)
  93. :"m"(*s)
  94. :"memory");
  95. dest += 32;
  96. s += 24;
  97. }
  98. __asm__ volatile(SFENCE:::"memory");
  99. __asm__ volatile(EMMS:::"memory");
  100. #endif
  101. while (s < end) {
  102. #if HAVE_BIGENDIAN
  103. /* RGB24 (= R,G,B) -> RGB32 (= A,B,G,R) */
  104. *dest++ = 255;
  105. *dest++ = s[2];
  106. *dest++ = s[1];
  107. *dest++ = s[0];
  108. s+=3;
  109. #else
  110. *dest++ = *s++;
  111. *dest++ = *s++;
  112. *dest++ = *s++;
  113. *dest++ = 255;
  114. #endif
  115. }
  116. }
  117. #define STORE_BGR24_MMX \
  118. "psrlq $8, %%mm2 \n\t" \
  119. "psrlq $8, %%mm3 \n\t" \
  120. "psrlq $8, %%mm6 \n\t" \
  121. "psrlq $8, %%mm7 \n\t" \
  122. "pand "MANGLE(mask24l)", %%mm0\n\t" \
  123. "pand "MANGLE(mask24l)", %%mm1\n\t" \
  124. "pand "MANGLE(mask24l)", %%mm4\n\t" \
  125. "pand "MANGLE(mask24l)", %%mm5\n\t" \
  126. "pand "MANGLE(mask24h)", %%mm2\n\t" \
  127. "pand "MANGLE(mask24h)", %%mm3\n\t" \
  128. "pand "MANGLE(mask24h)", %%mm6\n\t" \
  129. "pand "MANGLE(mask24h)", %%mm7\n\t" \
  130. "por %%mm2, %%mm0 \n\t" \
  131. "por %%mm3, %%mm1 \n\t" \
  132. "por %%mm6, %%mm4 \n\t" \
  133. "por %%mm7, %%mm5 \n\t" \
  134. \
  135. "movq %%mm1, %%mm2 \n\t" \
  136. "movq %%mm4, %%mm3 \n\t" \
  137. "psllq $48, %%mm2 \n\t" \
  138. "psllq $32, %%mm3 \n\t" \
  139. "pand "MANGLE(mask24hh)", %%mm2\n\t" \
  140. "pand "MANGLE(mask24hhh)", %%mm3\n\t" \
  141. "por %%mm2, %%mm0 \n\t" \
  142. "psrlq $16, %%mm1 \n\t" \
  143. "psrlq $32, %%mm4 \n\t" \
  144. "psllq $16, %%mm5 \n\t" \
  145. "por %%mm3, %%mm1 \n\t" \
  146. "pand "MANGLE(mask24hhhh)", %%mm5\n\t" \
  147. "por %%mm5, %%mm4 \n\t" \
  148. \
  149. MOVNTQ" %%mm0, %0 \n\t" \
  150. MOVNTQ" %%mm1, 8%0 \n\t" \
  151. MOVNTQ" %%mm4, 16%0"
  152. static inline void RENAME(rgb32tobgr24)(const uint8_t *src, uint8_t *dst, long src_size)
  153. {
  154. uint8_t *dest = dst;
  155. const uint8_t *s = src;
  156. const uint8_t *end;
  157. #if COMPILE_TEMPLATE_MMX
  158. const uint8_t *mm_end;
  159. #endif
  160. end = s + src_size;
  161. #if COMPILE_TEMPLATE_MMX
  162. __asm__ volatile(PREFETCH" %0"::"m"(*s):"memory");
  163. mm_end = end - 31;
  164. while (s < mm_end) {
  165. __asm__ volatile(
  166. PREFETCH" 32%1 \n\t"
  167. "movq %1, %%mm0 \n\t"
  168. "movq 8%1, %%mm1 \n\t"
  169. "movq 16%1, %%mm4 \n\t"
  170. "movq 24%1, %%mm5 \n\t"
  171. "movq %%mm0, %%mm2 \n\t"
  172. "movq %%mm1, %%mm3 \n\t"
  173. "movq %%mm4, %%mm6 \n\t"
  174. "movq %%mm5, %%mm7 \n\t"
  175. STORE_BGR24_MMX
  176. :"=m"(*dest)
  177. :"m"(*s)
  178. :"memory");
  179. dest += 24;
  180. s += 32;
  181. }
  182. __asm__ volatile(SFENCE:::"memory");
  183. __asm__ volatile(EMMS:::"memory");
  184. #endif
  185. while (s < end) {
  186. #if HAVE_BIGENDIAN
  187. /* RGB32 (= A,B,G,R) -> RGB24 (= R,G,B) */
  188. s++;
  189. dest[2] = *s++;
  190. dest[1] = *s++;
  191. dest[0] = *s++;
  192. dest += 3;
  193. #else
  194. *dest++ = *s++;
  195. *dest++ = *s++;
  196. *dest++ = *s++;
  197. s++;
  198. #endif
  199. }
  200. }
  201. /*
  202. original by Strepto/Astral
  203. ported to gcc & bugfixed: A'rpi
  204. MMX2, 3DNOW optimization by Nick Kurshev
  205. 32-bit C version, and and&add trick by Michael Niedermayer
  206. */
  207. static inline void RENAME(rgb15to16)(const uint8_t *src, uint8_t *dst, long src_size)
  208. {
  209. register const uint8_t* s=src;
  210. register uint8_t* d=dst;
  211. register const uint8_t *end;
  212. const uint8_t *mm_end;
  213. end = s + src_size;
  214. #if COMPILE_TEMPLATE_MMX
  215. __asm__ volatile(PREFETCH" %0"::"m"(*s));
  216. __asm__ volatile("movq %0, %%mm4"::"m"(mask15s));
  217. mm_end = end - 15;
  218. while (s<mm_end) {
  219. __asm__ volatile(
  220. PREFETCH" 32%1 \n\t"
  221. "movq %1, %%mm0 \n\t"
  222. "movq 8%1, %%mm2 \n\t"
  223. "movq %%mm0, %%mm1 \n\t"
  224. "movq %%mm2, %%mm3 \n\t"
  225. "pand %%mm4, %%mm0 \n\t"
  226. "pand %%mm4, %%mm2 \n\t"
  227. "paddw %%mm1, %%mm0 \n\t"
  228. "paddw %%mm3, %%mm2 \n\t"
  229. MOVNTQ" %%mm0, %0 \n\t"
  230. MOVNTQ" %%mm2, 8%0"
  231. :"=m"(*d)
  232. :"m"(*s)
  233. );
  234. d+=16;
  235. s+=16;
  236. }
  237. __asm__ volatile(SFENCE:::"memory");
  238. __asm__ volatile(EMMS:::"memory");
  239. #endif
  240. mm_end = end - 3;
  241. while (s < mm_end) {
  242. register unsigned x= *((const uint32_t *)s);
  243. *((uint32_t *)d) = (x&0x7FFF7FFF) + (x&0x7FE07FE0);
  244. d+=4;
  245. s+=4;
  246. }
  247. if (s < end) {
  248. register unsigned short x= *((const uint16_t *)s);
  249. *((uint16_t *)d) = (x&0x7FFF) + (x&0x7FE0);
  250. }
  251. }
  252. static inline void RENAME(rgb16to15)(const uint8_t *src, uint8_t *dst, long src_size)
  253. {
  254. register const uint8_t* s=src;
  255. register uint8_t* d=dst;
  256. register const uint8_t *end;
  257. const uint8_t *mm_end;
  258. end = s + src_size;
  259. #if COMPILE_TEMPLATE_MMX
  260. __asm__ volatile(PREFETCH" %0"::"m"(*s));
  261. __asm__ volatile("movq %0, %%mm7"::"m"(mask15rg));
  262. __asm__ volatile("movq %0, %%mm6"::"m"(mask15b));
  263. mm_end = end - 15;
  264. while (s<mm_end) {
  265. __asm__ volatile(
  266. PREFETCH" 32%1 \n\t"
  267. "movq %1, %%mm0 \n\t"
  268. "movq 8%1, %%mm2 \n\t"
  269. "movq %%mm0, %%mm1 \n\t"
  270. "movq %%mm2, %%mm3 \n\t"
  271. "psrlq $1, %%mm0 \n\t"
  272. "psrlq $1, %%mm2 \n\t"
  273. "pand %%mm7, %%mm0 \n\t"
  274. "pand %%mm7, %%mm2 \n\t"
  275. "pand %%mm6, %%mm1 \n\t"
  276. "pand %%mm6, %%mm3 \n\t"
  277. "por %%mm1, %%mm0 \n\t"
  278. "por %%mm3, %%mm2 \n\t"
  279. MOVNTQ" %%mm0, %0 \n\t"
  280. MOVNTQ" %%mm2, 8%0"
  281. :"=m"(*d)
  282. :"m"(*s)
  283. );
  284. d+=16;
  285. s+=16;
  286. }
  287. __asm__ volatile(SFENCE:::"memory");
  288. __asm__ volatile(EMMS:::"memory");
  289. #endif
  290. mm_end = end - 3;
  291. while (s < mm_end) {
  292. register uint32_t x= *((const uint32_t*)s);
  293. *((uint32_t *)d) = ((x>>1)&0x7FE07FE0) | (x&0x001F001F);
  294. s+=4;
  295. d+=4;
  296. }
  297. if (s < end) {
  298. register uint16_t x= *((const uint16_t*)s);
  299. *((uint16_t *)d) = ((x>>1)&0x7FE0) | (x&0x001F);
  300. }
  301. }
  302. static inline void RENAME(rgb32to16)(const uint8_t *src, uint8_t *dst, long src_size)
  303. {
  304. const uint8_t *s = src;
  305. const uint8_t *end;
  306. #if COMPILE_TEMPLATE_MMX
  307. const uint8_t *mm_end;
  308. #endif
  309. uint16_t *d = (uint16_t *)dst;
  310. end = s + src_size;
  311. #if COMPILE_TEMPLATE_MMX
  312. mm_end = end - 15;
  313. #if 1 //is faster only if multiplies are reasonably fast (FIXME figure out on which CPUs this is faster, on Athlon it is slightly faster)
  314. __asm__ volatile(
  315. "movq %3, %%mm5 \n\t"
  316. "movq %4, %%mm6 \n\t"
  317. "movq %5, %%mm7 \n\t"
  318. "jmp 2f \n\t"
  319. ".p2align 4 \n\t"
  320. "1: \n\t"
  321. PREFETCH" 32(%1) \n\t"
  322. "movd (%1), %%mm0 \n\t"
  323. "movd 4(%1), %%mm3 \n\t"
  324. "punpckldq 8(%1), %%mm0 \n\t"
  325. "punpckldq 12(%1), %%mm3 \n\t"
  326. "movq %%mm0, %%mm1 \n\t"
  327. "movq %%mm3, %%mm4 \n\t"
  328. "pand %%mm6, %%mm0 \n\t"
  329. "pand %%mm6, %%mm3 \n\t"
  330. "pmaddwd %%mm7, %%mm0 \n\t"
  331. "pmaddwd %%mm7, %%mm3 \n\t"
  332. "pand %%mm5, %%mm1 \n\t"
  333. "pand %%mm5, %%mm4 \n\t"
  334. "por %%mm1, %%mm0 \n\t"
  335. "por %%mm4, %%mm3 \n\t"
  336. "psrld $5, %%mm0 \n\t"
  337. "pslld $11, %%mm3 \n\t"
  338. "por %%mm3, %%mm0 \n\t"
  339. MOVNTQ" %%mm0, (%0) \n\t"
  340. "add $16, %1 \n\t"
  341. "add $8, %0 \n\t"
  342. "2: \n\t"
  343. "cmp %2, %1 \n\t"
  344. " jb 1b \n\t"
  345. : "+r" (d), "+r"(s)
  346. : "r" (mm_end), "m" (mask3216g), "m" (mask3216br), "m" (mul3216)
  347. );
  348. #else
  349. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  350. __asm__ volatile(
  351. "movq %0, %%mm7 \n\t"
  352. "movq %1, %%mm6 \n\t"
  353. ::"m"(red_16mask),"m"(green_16mask));
  354. while (s < mm_end) {
  355. __asm__ volatile(
  356. PREFETCH" 32%1 \n\t"
  357. "movd %1, %%mm0 \n\t"
  358. "movd 4%1, %%mm3 \n\t"
  359. "punpckldq 8%1, %%mm0 \n\t"
  360. "punpckldq 12%1, %%mm3 \n\t"
  361. "movq %%mm0, %%mm1 \n\t"
  362. "movq %%mm0, %%mm2 \n\t"
  363. "movq %%mm3, %%mm4 \n\t"
  364. "movq %%mm3, %%mm5 \n\t"
  365. "psrlq $3, %%mm0 \n\t"
  366. "psrlq $3, %%mm3 \n\t"
  367. "pand %2, %%mm0 \n\t"
  368. "pand %2, %%mm3 \n\t"
  369. "psrlq $5, %%mm1 \n\t"
  370. "psrlq $5, %%mm4 \n\t"
  371. "pand %%mm6, %%mm1 \n\t"
  372. "pand %%mm6, %%mm4 \n\t"
  373. "psrlq $8, %%mm2 \n\t"
  374. "psrlq $8, %%mm5 \n\t"
  375. "pand %%mm7, %%mm2 \n\t"
  376. "pand %%mm7, %%mm5 \n\t"
  377. "por %%mm1, %%mm0 \n\t"
  378. "por %%mm4, %%mm3 \n\t"
  379. "por %%mm2, %%mm0 \n\t"
  380. "por %%mm5, %%mm3 \n\t"
  381. "psllq $16, %%mm3 \n\t"
  382. "por %%mm3, %%mm0 \n\t"
  383. MOVNTQ" %%mm0, %0 \n\t"
  384. :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
  385. d += 4;
  386. s += 16;
  387. }
  388. #endif
  389. __asm__ volatile(SFENCE:::"memory");
  390. __asm__ volatile(EMMS:::"memory");
  391. #endif
  392. while (s < end) {
  393. register int rgb = *(const uint32_t*)s; s += 4;
  394. *d++ = ((rgb&0xFF)>>3) + ((rgb&0xFC00)>>5) + ((rgb&0xF80000)>>8);
  395. }
  396. }
  397. static inline void RENAME(rgb32tobgr16)(const uint8_t *src, uint8_t *dst, long src_size)
  398. {
  399. const uint8_t *s = src;
  400. const uint8_t *end;
  401. #if COMPILE_TEMPLATE_MMX
  402. const uint8_t *mm_end;
  403. #endif
  404. uint16_t *d = (uint16_t *)dst;
  405. end = s + src_size;
  406. #if COMPILE_TEMPLATE_MMX
  407. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  408. __asm__ volatile(
  409. "movq %0, %%mm7 \n\t"
  410. "movq %1, %%mm6 \n\t"
  411. ::"m"(red_16mask),"m"(green_16mask));
  412. mm_end = end - 15;
  413. while (s < mm_end) {
  414. __asm__ volatile(
  415. PREFETCH" 32%1 \n\t"
  416. "movd %1, %%mm0 \n\t"
  417. "movd 4%1, %%mm3 \n\t"
  418. "punpckldq 8%1, %%mm0 \n\t"
  419. "punpckldq 12%1, %%mm3 \n\t"
  420. "movq %%mm0, %%mm1 \n\t"
  421. "movq %%mm0, %%mm2 \n\t"
  422. "movq %%mm3, %%mm4 \n\t"
  423. "movq %%mm3, %%mm5 \n\t"
  424. "psllq $8, %%mm0 \n\t"
  425. "psllq $8, %%mm3 \n\t"
  426. "pand %%mm7, %%mm0 \n\t"
  427. "pand %%mm7, %%mm3 \n\t"
  428. "psrlq $5, %%mm1 \n\t"
  429. "psrlq $5, %%mm4 \n\t"
  430. "pand %%mm6, %%mm1 \n\t"
  431. "pand %%mm6, %%mm4 \n\t"
  432. "psrlq $19, %%mm2 \n\t"
  433. "psrlq $19, %%mm5 \n\t"
  434. "pand %2, %%mm2 \n\t"
  435. "pand %2, %%mm5 \n\t"
  436. "por %%mm1, %%mm0 \n\t"
  437. "por %%mm4, %%mm3 \n\t"
  438. "por %%mm2, %%mm0 \n\t"
  439. "por %%mm5, %%mm3 \n\t"
  440. "psllq $16, %%mm3 \n\t"
  441. "por %%mm3, %%mm0 \n\t"
  442. MOVNTQ" %%mm0, %0 \n\t"
  443. :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
  444. d += 4;
  445. s += 16;
  446. }
  447. __asm__ volatile(SFENCE:::"memory");
  448. __asm__ volatile(EMMS:::"memory");
  449. #endif
  450. while (s < end) {
  451. register int rgb = *(const uint32_t*)s; s += 4;
  452. *d++ = ((rgb&0xF8)<<8) + ((rgb&0xFC00)>>5) + ((rgb&0xF80000)>>19);
  453. }
  454. }
  455. static inline void RENAME(rgb32to15)(const uint8_t *src, uint8_t *dst, long src_size)
  456. {
  457. const uint8_t *s = src;
  458. const uint8_t *end;
  459. #if COMPILE_TEMPLATE_MMX
  460. const uint8_t *mm_end;
  461. #endif
  462. uint16_t *d = (uint16_t *)dst;
  463. end = s + src_size;
  464. #if COMPILE_TEMPLATE_MMX
  465. mm_end = end - 15;
  466. #if 1 //is faster only if multiplies are reasonably fast (FIXME figure out on which CPUs this is faster, on Athlon it is slightly faster)
  467. __asm__ volatile(
  468. "movq %3, %%mm5 \n\t"
  469. "movq %4, %%mm6 \n\t"
  470. "movq %5, %%mm7 \n\t"
  471. "jmp 2f \n\t"
  472. ".p2align 4 \n\t"
  473. "1: \n\t"
  474. PREFETCH" 32(%1) \n\t"
  475. "movd (%1), %%mm0 \n\t"
  476. "movd 4(%1), %%mm3 \n\t"
  477. "punpckldq 8(%1), %%mm0 \n\t"
  478. "punpckldq 12(%1), %%mm3 \n\t"
  479. "movq %%mm0, %%mm1 \n\t"
  480. "movq %%mm3, %%mm4 \n\t"
  481. "pand %%mm6, %%mm0 \n\t"
  482. "pand %%mm6, %%mm3 \n\t"
  483. "pmaddwd %%mm7, %%mm0 \n\t"
  484. "pmaddwd %%mm7, %%mm3 \n\t"
  485. "pand %%mm5, %%mm1 \n\t"
  486. "pand %%mm5, %%mm4 \n\t"
  487. "por %%mm1, %%mm0 \n\t"
  488. "por %%mm4, %%mm3 \n\t"
  489. "psrld $6, %%mm0 \n\t"
  490. "pslld $10, %%mm3 \n\t"
  491. "por %%mm3, %%mm0 \n\t"
  492. MOVNTQ" %%mm0, (%0) \n\t"
  493. "add $16, %1 \n\t"
  494. "add $8, %0 \n\t"
  495. "2: \n\t"
  496. "cmp %2, %1 \n\t"
  497. " jb 1b \n\t"
  498. : "+r" (d), "+r"(s)
  499. : "r" (mm_end), "m" (mask3215g), "m" (mask3216br), "m" (mul3215)
  500. );
  501. #else
  502. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  503. __asm__ volatile(
  504. "movq %0, %%mm7 \n\t"
  505. "movq %1, %%mm6 \n\t"
  506. ::"m"(red_15mask),"m"(green_15mask));
  507. while (s < mm_end) {
  508. __asm__ volatile(
  509. PREFETCH" 32%1 \n\t"
  510. "movd %1, %%mm0 \n\t"
  511. "movd 4%1, %%mm3 \n\t"
  512. "punpckldq 8%1, %%mm0 \n\t"
  513. "punpckldq 12%1, %%mm3 \n\t"
  514. "movq %%mm0, %%mm1 \n\t"
  515. "movq %%mm0, %%mm2 \n\t"
  516. "movq %%mm3, %%mm4 \n\t"
  517. "movq %%mm3, %%mm5 \n\t"
  518. "psrlq $3, %%mm0 \n\t"
  519. "psrlq $3, %%mm3 \n\t"
  520. "pand %2, %%mm0 \n\t"
  521. "pand %2, %%mm3 \n\t"
  522. "psrlq $6, %%mm1 \n\t"
  523. "psrlq $6, %%mm4 \n\t"
  524. "pand %%mm6, %%mm1 \n\t"
  525. "pand %%mm6, %%mm4 \n\t"
  526. "psrlq $9, %%mm2 \n\t"
  527. "psrlq $9, %%mm5 \n\t"
  528. "pand %%mm7, %%mm2 \n\t"
  529. "pand %%mm7, %%mm5 \n\t"
  530. "por %%mm1, %%mm0 \n\t"
  531. "por %%mm4, %%mm3 \n\t"
  532. "por %%mm2, %%mm0 \n\t"
  533. "por %%mm5, %%mm3 \n\t"
  534. "psllq $16, %%mm3 \n\t"
  535. "por %%mm3, %%mm0 \n\t"
  536. MOVNTQ" %%mm0, %0 \n\t"
  537. :"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
  538. d += 4;
  539. s += 16;
  540. }
  541. #endif
  542. __asm__ volatile(SFENCE:::"memory");
  543. __asm__ volatile(EMMS:::"memory");
  544. #endif
  545. while (s < end) {
  546. register int rgb = *(const uint32_t*)s; s += 4;
  547. *d++ = ((rgb&0xFF)>>3) + ((rgb&0xF800)>>6) + ((rgb&0xF80000)>>9);
  548. }
  549. }
  550. static inline void RENAME(rgb32tobgr15)(const uint8_t *src, uint8_t *dst, long src_size)
  551. {
  552. const uint8_t *s = src;
  553. const uint8_t *end;
  554. #if COMPILE_TEMPLATE_MMX
  555. const uint8_t *mm_end;
  556. #endif
  557. uint16_t *d = (uint16_t *)dst;
  558. end = s + src_size;
  559. #if COMPILE_TEMPLATE_MMX
  560. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  561. __asm__ volatile(
  562. "movq %0, %%mm7 \n\t"
  563. "movq %1, %%mm6 \n\t"
  564. ::"m"(red_15mask),"m"(green_15mask));
  565. mm_end = end - 15;
  566. while (s < mm_end) {
  567. __asm__ volatile(
  568. PREFETCH" 32%1 \n\t"
  569. "movd %1, %%mm0 \n\t"
  570. "movd 4%1, %%mm3 \n\t"
  571. "punpckldq 8%1, %%mm0 \n\t"
  572. "punpckldq 12%1, %%mm3 \n\t"
  573. "movq %%mm0, %%mm1 \n\t"
  574. "movq %%mm0, %%mm2 \n\t"
  575. "movq %%mm3, %%mm4 \n\t"
  576. "movq %%mm3, %%mm5 \n\t"
  577. "psllq $7, %%mm0 \n\t"
  578. "psllq $7, %%mm3 \n\t"
  579. "pand %%mm7, %%mm0 \n\t"
  580. "pand %%mm7, %%mm3 \n\t"
  581. "psrlq $6, %%mm1 \n\t"
  582. "psrlq $6, %%mm4 \n\t"
  583. "pand %%mm6, %%mm1 \n\t"
  584. "pand %%mm6, %%mm4 \n\t"
  585. "psrlq $19, %%mm2 \n\t"
  586. "psrlq $19, %%mm5 \n\t"
  587. "pand %2, %%mm2 \n\t"
  588. "pand %2, %%mm5 \n\t"
  589. "por %%mm1, %%mm0 \n\t"
  590. "por %%mm4, %%mm3 \n\t"
  591. "por %%mm2, %%mm0 \n\t"
  592. "por %%mm5, %%mm3 \n\t"
  593. "psllq $16, %%mm3 \n\t"
  594. "por %%mm3, %%mm0 \n\t"
  595. MOVNTQ" %%mm0, %0 \n\t"
  596. :"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
  597. d += 4;
  598. s += 16;
  599. }
  600. __asm__ volatile(SFENCE:::"memory");
  601. __asm__ volatile(EMMS:::"memory");
  602. #endif
  603. while (s < end) {
  604. register int rgb = *(const uint32_t*)s; s += 4;
  605. *d++ = ((rgb&0xF8)<<7) + ((rgb&0xF800)>>6) + ((rgb&0xF80000)>>19);
  606. }
  607. }
  608. static inline void RENAME(rgb24tobgr16)(const uint8_t *src, uint8_t *dst, long src_size)
  609. {
  610. const uint8_t *s = src;
  611. const uint8_t *end;
  612. #if COMPILE_TEMPLATE_MMX
  613. const uint8_t *mm_end;
  614. #endif
  615. uint16_t *d = (uint16_t *)dst;
  616. end = s + src_size;
  617. #if COMPILE_TEMPLATE_MMX
  618. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  619. __asm__ volatile(
  620. "movq %0, %%mm7 \n\t"
  621. "movq %1, %%mm6 \n\t"
  622. ::"m"(red_16mask),"m"(green_16mask));
  623. mm_end = end - 11;
  624. while (s < mm_end) {
  625. __asm__ volatile(
  626. PREFETCH" 32%1 \n\t"
  627. "movd %1, %%mm0 \n\t"
  628. "movd 3%1, %%mm3 \n\t"
  629. "punpckldq 6%1, %%mm0 \n\t"
  630. "punpckldq 9%1, %%mm3 \n\t"
  631. "movq %%mm0, %%mm1 \n\t"
  632. "movq %%mm0, %%mm2 \n\t"
  633. "movq %%mm3, %%mm4 \n\t"
  634. "movq %%mm3, %%mm5 \n\t"
  635. "psrlq $3, %%mm0 \n\t"
  636. "psrlq $3, %%mm3 \n\t"
  637. "pand %2, %%mm0 \n\t"
  638. "pand %2, %%mm3 \n\t"
  639. "psrlq $5, %%mm1 \n\t"
  640. "psrlq $5, %%mm4 \n\t"
  641. "pand %%mm6, %%mm1 \n\t"
  642. "pand %%mm6, %%mm4 \n\t"
  643. "psrlq $8, %%mm2 \n\t"
  644. "psrlq $8, %%mm5 \n\t"
  645. "pand %%mm7, %%mm2 \n\t"
  646. "pand %%mm7, %%mm5 \n\t"
  647. "por %%mm1, %%mm0 \n\t"
  648. "por %%mm4, %%mm3 \n\t"
  649. "por %%mm2, %%mm0 \n\t"
  650. "por %%mm5, %%mm3 \n\t"
  651. "psllq $16, %%mm3 \n\t"
  652. "por %%mm3, %%mm0 \n\t"
  653. MOVNTQ" %%mm0, %0 \n\t"
  654. :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
  655. d += 4;
  656. s += 12;
  657. }
  658. __asm__ volatile(SFENCE:::"memory");
  659. __asm__ volatile(EMMS:::"memory");
  660. #endif
  661. while (s < end) {
  662. const int b = *s++;
  663. const int g = *s++;
  664. const int r = *s++;
  665. *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
  666. }
  667. }
  668. static inline void RENAME(rgb24to16)(const uint8_t *src, uint8_t *dst, long src_size)
  669. {
  670. const uint8_t *s = src;
  671. const uint8_t *end;
  672. #if COMPILE_TEMPLATE_MMX
  673. const uint8_t *mm_end;
  674. #endif
  675. uint16_t *d = (uint16_t *)dst;
  676. end = s + src_size;
  677. #if COMPILE_TEMPLATE_MMX
  678. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  679. __asm__ volatile(
  680. "movq %0, %%mm7 \n\t"
  681. "movq %1, %%mm6 \n\t"
  682. ::"m"(red_16mask),"m"(green_16mask));
  683. mm_end = end - 15;
  684. while (s < mm_end) {
  685. __asm__ volatile(
  686. PREFETCH" 32%1 \n\t"
  687. "movd %1, %%mm0 \n\t"
  688. "movd 3%1, %%mm3 \n\t"
  689. "punpckldq 6%1, %%mm0 \n\t"
  690. "punpckldq 9%1, %%mm3 \n\t"
  691. "movq %%mm0, %%mm1 \n\t"
  692. "movq %%mm0, %%mm2 \n\t"
  693. "movq %%mm3, %%mm4 \n\t"
  694. "movq %%mm3, %%mm5 \n\t"
  695. "psllq $8, %%mm0 \n\t"
  696. "psllq $8, %%mm3 \n\t"
  697. "pand %%mm7, %%mm0 \n\t"
  698. "pand %%mm7, %%mm3 \n\t"
  699. "psrlq $5, %%mm1 \n\t"
  700. "psrlq $5, %%mm4 \n\t"
  701. "pand %%mm6, %%mm1 \n\t"
  702. "pand %%mm6, %%mm4 \n\t"
  703. "psrlq $19, %%mm2 \n\t"
  704. "psrlq $19, %%mm5 \n\t"
  705. "pand %2, %%mm2 \n\t"
  706. "pand %2, %%mm5 \n\t"
  707. "por %%mm1, %%mm0 \n\t"
  708. "por %%mm4, %%mm3 \n\t"
  709. "por %%mm2, %%mm0 \n\t"
  710. "por %%mm5, %%mm3 \n\t"
  711. "psllq $16, %%mm3 \n\t"
  712. "por %%mm3, %%mm0 \n\t"
  713. MOVNTQ" %%mm0, %0 \n\t"
  714. :"=m"(*d):"m"(*s),"m"(blue_16mask):"memory");
  715. d += 4;
  716. s += 12;
  717. }
  718. __asm__ volatile(SFENCE:::"memory");
  719. __asm__ volatile(EMMS:::"memory");
  720. #endif
  721. while (s < end) {
  722. const int r = *s++;
  723. const int g = *s++;
  724. const int b = *s++;
  725. *d++ = (b>>3) | ((g&0xFC)<<3) | ((r&0xF8)<<8);
  726. }
  727. }
  728. static inline void RENAME(rgb24tobgr15)(const uint8_t *src, uint8_t *dst, long src_size)
  729. {
  730. const uint8_t *s = src;
  731. const uint8_t *end;
  732. #if COMPILE_TEMPLATE_MMX
  733. const uint8_t *mm_end;
  734. #endif
  735. uint16_t *d = (uint16_t *)dst;
  736. end = s + src_size;
  737. #if COMPILE_TEMPLATE_MMX
  738. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  739. __asm__ volatile(
  740. "movq %0, %%mm7 \n\t"
  741. "movq %1, %%mm6 \n\t"
  742. ::"m"(red_15mask),"m"(green_15mask));
  743. mm_end = end - 11;
  744. while (s < mm_end) {
  745. __asm__ volatile(
  746. PREFETCH" 32%1 \n\t"
  747. "movd %1, %%mm0 \n\t"
  748. "movd 3%1, %%mm3 \n\t"
  749. "punpckldq 6%1, %%mm0 \n\t"
  750. "punpckldq 9%1, %%mm3 \n\t"
  751. "movq %%mm0, %%mm1 \n\t"
  752. "movq %%mm0, %%mm2 \n\t"
  753. "movq %%mm3, %%mm4 \n\t"
  754. "movq %%mm3, %%mm5 \n\t"
  755. "psrlq $3, %%mm0 \n\t"
  756. "psrlq $3, %%mm3 \n\t"
  757. "pand %2, %%mm0 \n\t"
  758. "pand %2, %%mm3 \n\t"
  759. "psrlq $6, %%mm1 \n\t"
  760. "psrlq $6, %%mm4 \n\t"
  761. "pand %%mm6, %%mm1 \n\t"
  762. "pand %%mm6, %%mm4 \n\t"
  763. "psrlq $9, %%mm2 \n\t"
  764. "psrlq $9, %%mm5 \n\t"
  765. "pand %%mm7, %%mm2 \n\t"
  766. "pand %%mm7, %%mm5 \n\t"
  767. "por %%mm1, %%mm0 \n\t"
  768. "por %%mm4, %%mm3 \n\t"
  769. "por %%mm2, %%mm0 \n\t"
  770. "por %%mm5, %%mm3 \n\t"
  771. "psllq $16, %%mm3 \n\t"
  772. "por %%mm3, %%mm0 \n\t"
  773. MOVNTQ" %%mm0, %0 \n\t"
  774. :"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
  775. d += 4;
  776. s += 12;
  777. }
  778. __asm__ volatile(SFENCE:::"memory");
  779. __asm__ volatile(EMMS:::"memory");
  780. #endif
  781. while (s < end) {
  782. const int b = *s++;
  783. const int g = *s++;
  784. const int r = *s++;
  785. *d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
  786. }
  787. }
  788. static inline void RENAME(rgb24to15)(const uint8_t *src, uint8_t *dst, long src_size)
  789. {
  790. const uint8_t *s = src;
  791. const uint8_t *end;
  792. #if COMPILE_TEMPLATE_MMX
  793. const uint8_t *mm_end;
  794. #endif
  795. uint16_t *d = (uint16_t *)dst;
  796. end = s + src_size;
  797. #if COMPILE_TEMPLATE_MMX
  798. __asm__ volatile(PREFETCH" %0"::"m"(*src):"memory");
  799. __asm__ volatile(
  800. "movq %0, %%mm7 \n\t"
  801. "movq %1, %%mm6 \n\t"
  802. ::"m"(red_15mask),"m"(green_15mask));
  803. mm_end = end - 15;
  804. while (s < mm_end) {
  805. __asm__ volatile(
  806. PREFETCH" 32%1 \n\t"
  807. "movd %1, %%mm0 \n\t"
  808. "movd 3%1, %%mm3 \n\t"
  809. "punpckldq 6%1, %%mm0 \n\t"
  810. "punpckldq 9%1, %%mm3 \n\t"
  811. "movq %%mm0, %%mm1 \n\t"
  812. "movq %%mm0, %%mm2 \n\t"
  813. "movq %%mm3, %%mm4 \n\t"
  814. "movq %%mm3, %%mm5 \n\t"
  815. "psllq $7, %%mm0 \n\t"
  816. "psllq $7, %%mm3 \n\t"
  817. "pand %%mm7, %%mm0 \n\t"
  818. "pand %%mm7, %%mm3 \n\t"
  819. "psrlq $6, %%mm1 \n\t"
  820. "psrlq $6, %%mm4 \n\t"
  821. "pand %%mm6, %%mm1 \n\t"
  822. "pand %%mm6, %%mm4 \n\t"
  823. "psrlq $19, %%mm2 \n\t"
  824. "psrlq $19, %%mm5 \n\t"
  825. "pand %2, %%mm2 \n\t"
  826. "pand %2, %%mm5 \n\t"
  827. "por %%mm1, %%mm0 \n\t"
  828. "por %%mm4, %%mm3 \n\t"
  829. "por %%mm2, %%mm0 \n\t"
  830. "por %%mm5, %%mm3 \n\t"
  831. "psllq $16, %%mm3 \n\t"
  832. "por %%mm3, %%mm0 \n\t"
  833. MOVNTQ" %%mm0, %0 \n\t"
  834. :"=m"(*d):"m"(*s),"m"(blue_15mask):"memory");
  835. d += 4;
  836. s += 12;
  837. }
  838. __asm__ volatile(SFENCE:::"memory");
  839. __asm__ volatile(EMMS:::"memory");
  840. #endif
  841. while (s < end) {
  842. const int r = *s++;
  843. const int g = *s++;
  844. const int b = *s++;
  845. *d++ = (b>>3) | ((g&0xF8)<<2) | ((r&0xF8)<<7);
  846. }
  847. }
  848. /*
  849. I use less accurate approximation here by simply left-shifting the input
  850. value and filling the low order bits with zeroes. This method improves PNG
  851. compression but this scheme cannot reproduce white exactly, since it does
  852. not generate an all-ones maximum value; the net effect is to darken the
  853. image slightly.
  854. The better method should be "left bit replication":
  855. 4 3 2 1 0
  856. ---------
  857. 1 1 0 1 1
  858. 7 6 5 4 3 2 1 0
  859. ----------------
  860. 1 1 0 1 1 1 1 0
  861. |=======| |===|
  862. | leftmost bits repeated to fill open bits
  863. |
  864. original bits
  865. */
  866. static inline void RENAME(rgb15tobgr24)(const uint8_t *src, uint8_t *dst, long src_size)
  867. {
  868. const uint16_t *end;
  869. #if COMPILE_TEMPLATE_MMX
  870. const uint16_t *mm_end;
  871. #endif
  872. uint8_t *d = dst;
  873. const uint16_t *s = (const uint16_t*)src;
  874. end = s + src_size/2;
  875. #if COMPILE_TEMPLATE_MMX
  876. __asm__ volatile(PREFETCH" %0"::"m"(*s):"memory");
  877. mm_end = end - 7;
  878. while (s < mm_end) {
  879. __asm__ volatile(
  880. PREFETCH" 32%1 \n\t"
  881. "movq %1, %%mm0 \n\t"
  882. "movq %1, %%mm1 \n\t"
  883. "movq %1, %%mm2 \n\t"
  884. "pand %2, %%mm0 \n\t"
  885. "pand %3, %%mm1 \n\t"
  886. "pand %4, %%mm2 \n\t"
  887. "psllq $3, %%mm0 \n\t"
  888. "psrlq $2, %%mm1 \n\t"
  889. "psrlq $7, %%mm2 \n\t"
  890. "movq %%mm0, %%mm3 \n\t"
  891. "movq %%mm1, %%mm4 \n\t"
  892. "movq %%mm2, %%mm5 \n\t"
  893. "punpcklwd %5, %%mm0 \n\t"
  894. "punpcklwd %5, %%mm1 \n\t"
  895. "punpcklwd %5, %%mm2 \n\t"
  896. "punpckhwd %5, %%mm3 \n\t"
  897. "punpckhwd %5, %%mm4 \n\t"
  898. "punpckhwd %5, %%mm5 \n\t"
  899. "psllq $8, %%mm1 \n\t"
  900. "psllq $16, %%mm2 \n\t"
  901. "por %%mm1, %%mm0 \n\t"
  902. "por %%mm2, %%mm0 \n\t"
  903. "psllq $8, %%mm4 \n\t"
  904. "psllq $16, %%mm5 \n\t"
  905. "por %%mm4, %%mm3 \n\t"
  906. "por %%mm5, %%mm3 \n\t"
  907. "movq %%mm0, %%mm6 \n\t"
  908. "movq %%mm3, %%mm7 \n\t"
  909. "movq 8%1, %%mm0 \n\t"
  910. "movq 8%1, %%mm1 \n\t"
  911. "movq 8%1, %%mm2 \n\t"
  912. "pand %2, %%mm0 \n\t"
  913. "pand %3, %%mm1 \n\t"
  914. "pand %4, %%mm2 \n\t"
  915. "psllq $3, %%mm0 \n\t"
  916. "psrlq $2, %%mm1 \n\t"
  917. "psrlq $7, %%mm2 \n\t"
  918. "movq %%mm0, %%mm3 \n\t"
  919. "movq %%mm1, %%mm4 \n\t"
  920. "movq %%mm2, %%mm5 \n\t"
  921. "punpcklwd %5, %%mm0 \n\t"
  922. "punpcklwd %5, %%mm1 \n\t"
  923. "punpcklwd %5, %%mm2 \n\t"
  924. "punpckhwd %5, %%mm3 \n\t"
  925. "punpckhwd %5, %%mm4 \n\t"
  926. "punpckhwd %5, %%mm5 \n\t"
  927. "psllq $8, %%mm1 \n\t"
  928. "psllq $16, %%mm2 \n\t"
  929. "por %%mm1, %%mm0 \n\t"
  930. "por %%mm2, %%mm0 \n\t"
  931. "psllq $8, %%mm4 \n\t"
  932. "psllq $16, %%mm5 \n\t"
  933. "por %%mm4, %%mm3 \n\t"
  934. "por %%mm5, %%mm3 \n\t"
  935. :"=m"(*d)
  936. :"m"(*s),"m"(mask15b),"m"(mask15g),"m"(mask15r), "m"(mmx_null)
  937. :"memory");
  938. /* borrowed 32 to 24 */
  939. __asm__ volatile(
  940. "movq %%mm0, %%mm4 \n\t"
  941. "movq %%mm3, %%mm5 \n\t"
  942. "movq %%mm6, %%mm0 \n\t"
  943. "movq %%mm7, %%mm1 \n\t"
  944. "movq %%mm4, %%mm6 \n\t"
  945. "movq %%mm5, %%mm7 \n\t"
  946. "movq %%mm0, %%mm2 \n\t"
  947. "movq %%mm1, %%mm3 \n\t"
  948. STORE_BGR24_MMX
  949. :"=m"(*d)
  950. :"m"(*s)
  951. :"memory");
  952. d += 24;
  953. s += 8;
  954. }
  955. __asm__ volatile(SFENCE:::"memory");
  956. __asm__ volatile(EMMS:::"memory");
  957. #endif
  958. while (s < end) {
  959. register uint16_t bgr;
  960. bgr = *s++;
  961. *d++ = (bgr&0x1F)<<3;
  962. *d++ = (bgr&0x3E0)>>2;
  963. *d++ = (bgr&0x7C00)>>7;
  964. }
  965. }
  966. static inline void RENAME(rgb16tobgr24)(const uint8_t *src, uint8_t *dst, long src_size)
  967. {
  968. const uint16_t *end;
  969. #if COMPILE_TEMPLATE_MMX
  970. const uint16_t *mm_end;
  971. #endif
  972. uint8_t *d = (uint8_t *)dst;
  973. const uint16_t *s = (const uint16_t *)src;
  974. end = s + src_size/2;
  975. #if COMPILE_TEMPLATE_MMX
  976. __asm__ volatile(PREFETCH" %0"::"m"(*s):"memory");
  977. mm_end = end - 7;
  978. while (s < mm_end) {
  979. __asm__ volatile(
  980. PREFETCH" 32%1 \n\t"
  981. "movq %1, %%mm0 \n\t"
  982. "movq %1, %%mm1 \n\t"
  983. "movq %1, %%mm2 \n\t"
  984. "pand %2, %%mm0 \n\t"
  985. "pand %3, %%mm1 \n\t"
  986. "pand %4, %%mm2 \n\t"
  987. "psllq $3, %%mm0 \n\t"
  988. "psrlq $3, %%mm1 \n\t"
  989. "psrlq $8, %%mm2 \n\t"
  990. "movq %%mm0, %%mm3 \n\t"
  991. "movq %%mm1, %%mm4 \n\t"
  992. "movq %%mm2, %%mm5 \n\t"
  993. "punpcklwd %5, %%mm0 \n\t"
  994. "punpcklwd %5, %%mm1 \n\t"
  995. "punpcklwd %5, %%mm2 \n\t"
  996. "punpckhwd %5, %%mm3 \n\t"
  997. "punpckhwd %5, %%mm4 \n\t"
  998. "punpckhwd %5, %%mm5 \n\t"
  999. "psllq $8, %%mm1 \n\t"
  1000. "psllq $16, %%mm2 \n\t"
  1001. "por %%mm1, %%mm0 \n\t"
  1002. "por %%mm2, %%mm0 \n\t"
  1003. "psllq $8, %%mm4 \n\t"
  1004. "psllq $16, %%mm5 \n\t"
  1005. "por %%mm4, %%mm3 \n\t"
  1006. "por %%mm5, %%mm3 \n\t"
  1007. "movq %%mm0, %%mm6 \n\t"
  1008. "movq %%mm3, %%mm7 \n\t"
  1009. "movq 8%1, %%mm0 \n\t"
  1010. "movq 8%1, %%mm1 \n\t"
  1011. "movq 8%1, %%mm2 \n\t"
  1012. "pand %2, %%mm0 \n\t"
  1013. "pand %3, %%mm1 \n\t"
  1014. "pand %4, %%mm2 \n\t"
  1015. "psllq $3, %%mm0 \n\t"
  1016. "psrlq $3, %%mm1 \n\t"
  1017. "psrlq $8, %%mm2 \n\t"
  1018. "movq %%mm0, %%mm3 \n\t"
  1019. "movq %%mm1, %%mm4 \n\t"
  1020. "movq %%mm2, %%mm5 \n\t"
  1021. "punpcklwd %5, %%mm0 \n\t"
  1022. "punpcklwd %5, %%mm1 \n\t"
  1023. "punpcklwd %5, %%mm2 \n\t"
  1024. "punpckhwd %5, %%mm3 \n\t"
  1025. "punpckhwd %5, %%mm4 \n\t"
  1026. "punpckhwd %5, %%mm5 \n\t"
  1027. "psllq $8, %%mm1 \n\t"
  1028. "psllq $16, %%mm2 \n\t"
  1029. "por %%mm1, %%mm0 \n\t"
  1030. "por %%mm2, %%mm0 \n\t"
  1031. "psllq $8, %%mm4 \n\t"
  1032. "psllq $16, %%mm5 \n\t"
  1033. "por %%mm4, %%mm3 \n\t"
  1034. "por %%mm5, %%mm3 \n\t"
  1035. :"=m"(*d)
  1036. :"m"(*s),"m"(mask16b),"m"(mask16g),"m"(mask16r),"m"(mmx_null)
  1037. :"memory");
  1038. /* borrowed 32 to 24 */
  1039. __asm__ volatile(
  1040. "movq %%mm0, %%mm4 \n\t"
  1041. "movq %%mm3, %%mm5 \n\t"
  1042. "movq %%mm6, %%mm0 \n\t"
  1043. "movq %%mm7, %%mm1 \n\t"
  1044. "movq %%mm4, %%mm6 \n\t"
  1045. "movq %%mm5, %%mm7 \n\t"
  1046. "movq %%mm0, %%mm2 \n\t"
  1047. "movq %%mm1, %%mm3 \n\t"
  1048. STORE_BGR24_MMX
  1049. :"=m"(*d)
  1050. :"m"(*s)
  1051. :"memory");
  1052. d += 24;
  1053. s += 8;
  1054. }
  1055. __asm__ volatile(SFENCE:::"memory");
  1056. __asm__ volatile(EMMS:::"memory");
  1057. #endif
  1058. while (s < end) {
  1059. register uint16_t bgr;
  1060. bgr = *s++;
  1061. *d++ = (bgr&0x1F)<<3;
  1062. *d++ = (bgr&0x7E0)>>3;
  1063. *d++ = (bgr&0xF800)>>8;
  1064. }
  1065. }
  1066. /*
  1067. * mm0 = 00 B3 00 B2 00 B1 00 B0
  1068. * mm1 = 00 G3 00 G2 00 G1 00 G0
  1069. * mm2 = 00 R3 00 R2 00 R1 00 R0
  1070. * mm6 = FF FF FF FF FF FF FF FF
  1071. * mm7 = 00 00 00 00 00 00 00 00
  1072. */
  1073. #define PACK_RGB32 \
  1074. "packuswb %%mm7, %%mm0 \n\t" /* 00 00 00 00 B3 B2 B1 B0 */ \
  1075. "packuswb %%mm7, %%mm1 \n\t" /* 00 00 00 00 G3 G2 G1 G0 */ \
  1076. "packuswb %%mm7, %%mm2 \n\t" /* 00 00 00 00 R3 R2 R1 R0 */ \
  1077. "punpcklbw %%mm1, %%mm0 \n\t" /* G3 B3 G2 B2 G1 B1 G0 B0 */ \
  1078. "punpcklbw %%mm6, %%mm2 \n\t" /* FF R3 FF R2 FF R1 FF R0 */ \
  1079. "movq %%mm0, %%mm3 \n\t" \
  1080. "punpcklwd %%mm2, %%mm0 \n\t" /* FF R1 G1 B1 FF R0 G0 B0 */ \
  1081. "punpckhwd %%mm2, %%mm3 \n\t" /* FF R3 G3 B3 FF R2 G2 B2 */ \
  1082. MOVNTQ" %%mm0, %0 \n\t" \
  1083. MOVNTQ" %%mm3, 8%0 \n\t" \
  1084. static inline void RENAME(rgb15to32)(const uint8_t *src, uint8_t *dst, long src_size)
  1085. {
  1086. const uint16_t *end;
  1087. #if COMPILE_TEMPLATE_MMX
  1088. const uint16_t *mm_end;
  1089. #endif
  1090. uint8_t *d = dst;
  1091. const uint16_t *s = (const uint16_t *)src;
  1092. end = s + src_size/2;
  1093. #if COMPILE_TEMPLATE_MMX
  1094. __asm__ volatile(PREFETCH" %0"::"m"(*s):"memory");
  1095. __asm__ volatile("pxor %%mm7,%%mm7 \n\t":::"memory");
  1096. __asm__ volatile("pcmpeqd %%mm6,%%mm6 \n\t":::"memory");
  1097. mm_end = end - 3;
  1098. while (s < mm_end) {
  1099. __asm__ volatile(
  1100. PREFETCH" 32%1 \n\t"
  1101. "movq %1, %%mm0 \n\t"
  1102. "movq %1, %%mm1 \n\t"
  1103. "movq %1, %%mm2 \n\t"
  1104. "pand %2, %%mm0 \n\t"
  1105. "pand %3, %%mm1 \n\t"
  1106. "pand %4, %%mm2 \n\t"
  1107. "psllq $3, %%mm0 \n\t"
  1108. "psrlq $2, %%mm1 \n\t"
  1109. "psrlq $7, %%mm2 \n\t"
  1110. PACK_RGB32
  1111. :"=m"(*d)
  1112. :"m"(*s),"m"(mask15b),"m"(mask15g),"m"(mask15r)
  1113. :"memory");
  1114. d += 16;
  1115. s += 4;
  1116. }
  1117. __asm__ volatile(SFENCE:::"memory");
  1118. __asm__ volatile(EMMS:::"memory");
  1119. #endif
  1120. while (s < end) {
  1121. register uint16_t bgr;
  1122. bgr = *s++;
  1123. #if HAVE_BIGENDIAN
  1124. *d++ = 255;
  1125. *d++ = (bgr&0x7C00)>>7;
  1126. *d++ = (bgr&0x3E0)>>2;
  1127. *d++ = (bgr&0x1F)<<3;
  1128. #else
  1129. *d++ = (bgr&0x1F)<<3;
  1130. *d++ = (bgr&0x3E0)>>2;
  1131. *d++ = (bgr&0x7C00)>>7;
  1132. *d++ = 255;
  1133. #endif
  1134. }
  1135. }
  1136. static inline void RENAME(rgb16to32)(const uint8_t *src, uint8_t *dst, long src_size)
  1137. {
  1138. const uint16_t *end;
  1139. #if COMPILE_TEMPLATE_MMX
  1140. const uint16_t *mm_end;
  1141. #endif
  1142. uint8_t *d = dst;
  1143. const uint16_t *s = (const uint16_t*)src;
  1144. end = s + src_size/2;
  1145. #if COMPILE_TEMPLATE_MMX
  1146. __asm__ volatile(PREFETCH" %0"::"m"(*s):"memory");
  1147. __asm__ volatile("pxor %%mm7,%%mm7 \n\t":::"memory");
  1148. __asm__ volatile("pcmpeqd %%mm6,%%mm6 \n\t":::"memory");
  1149. mm_end = end - 3;
  1150. while (s < mm_end) {
  1151. __asm__ volatile(
  1152. PREFETCH" 32%1 \n\t"
  1153. "movq %1, %%mm0 \n\t"
  1154. "movq %1, %%mm1 \n\t"
  1155. "movq %1, %%mm2 \n\t"
  1156. "pand %2, %%mm0 \n\t"
  1157. "pand %3, %%mm1 \n\t"
  1158. "pand %4, %%mm2 \n\t"
  1159. "psllq $3, %%mm0 \n\t"
  1160. "psrlq $3, %%mm1 \n\t"
  1161. "psrlq $8, %%mm2 \n\t"
  1162. PACK_RGB32
  1163. :"=m"(*d)
  1164. :"m"(*s),"m"(mask16b),"m"(mask16g),"m"(mask16r)
  1165. :"memory");
  1166. d += 16;
  1167. s += 4;
  1168. }
  1169. __asm__ volatile(SFENCE:::"memory");
  1170. __asm__ volatile(EMMS:::"memory");
  1171. #endif
  1172. while (s < end) {
  1173. register uint16_t bgr;
  1174. bgr = *s++;
  1175. #if HAVE_BIGENDIAN
  1176. *d++ = 255;
  1177. *d++ = (bgr&0xF800)>>8;
  1178. *d++ = (bgr&0x7E0)>>3;
  1179. *d++ = (bgr&0x1F)<<3;
  1180. #else
  1181. *d++ = (bgr&0x1F)<<3;
  1182. *d++ = (bgr&0x7E0)>>3;
  1183. *d++ = (bgr&0xF800)>>8;
  1184. *d++ = 255;
  1185. #endif
  1186. }
  1187. }
  1188. static inline void RENAME(shuffle_bytes_2103)(const uint8_t *src, uint8_t *dst, long src_size)
  1189. {
  1190. x86_reg idx = 15 - src_size;
  1191. const uint8_t *s = src-idx;
  1192. uint8_t *d = dst-idx;
  1193. #if COMPILE_TEMPLATE_MMX
  1194. __asm__ volatile(
  1195. "test %0, %0 \n\t"
  1196. "jns 2f \n\t"
  1197. PREFETCH" (%1, %0) \n\t"
  1198. "movq %3, %%mm7 \n\t"
  1199. "pxor %4, %%mm7 \n\t"
  1200. "movq %%mm7, %%mm6 \n\t"
  1201. "pxor %5, %%mm7 \n\t"
  1202. ".p2align 4 \n\t"
  1203. "1: \n\t"
  1204. PREFETCH" 32(%1, %0) \n\t"
  1205. "movq (%1, %0), %%mm0 \n\t"
  1206. "movq 8(%1, %0), %%mm1 \n\t"
  1207. # if COMPILE_TEMPLATE_MMX2
  1208. "pshufw $177, %%mm0, %%mm3 \n\t"
  1209. "pshufw $177, %%mm1, %%mm5 \n\t"
  1210. "pand %%mm7, %%mm0 \n\t"
  1211. "pand %%mm6, %%mm3 \n\t"
  1212. "pand %%mm7, %%mm1 \n\t"
  1213. "pand %%mm6, %%mm5 \n\t"
  1214. "por %%mm3, %%mm0 \n\t"
  1215. "por %%mm5, %%mm1 \n\t"
  1216. # else
  1217. "movq %%mm0, %%mm2 \n\t"
  1218. "movq %%mm1, %%mm4 \n\t"
  1219. "pand %%mm7, %%mm0 \n\t"
  1220. "pand %%mm6, %%mm2 \n\t"
  1221. "pand %%mm7, %%mm1 \n\t"
  1222. "pand %%mm6, %%mm4 \n\t"
  1223. "movq %%mm2, %%mm3 \n\t"
  1224. "movq %%mm4, %%mm5 \n\t"
  1225. "pslld $16, %%mm2 \n\t"
  1226. "psrld $16, %%mm3 \n\t"
  1227. "pslld $16, %%mm4 \n\t"
  1228. "psrld $16, %%mm5 \n\t"
  1229. "por %%mm2, %%mm0 \n\t"
  1230. "por %%mm4, %%mm1 \n\t"
  1231. "por %%mm3, %%mm0 \n\t"
  1232. "por %%mm5, %%mm1 \n\t"
  1233. # endif
  1234. MOVNTQ" %%mm0, (%2, %0) \n\t"
  1235. MOVNTQ" %%mm1, 8(%2, %0) \n\t"
  1236. "add $16, %0 \n\t"
  1237. "js 1b \n\t"
  1238. SFENCE" \n\t"
  1239. EMMS" \n\t"
  1240. "2: \n\t"
  1241. : "+&r"(idx)
  1242. : "r" (s), "r" (d), "m" (mask32b), "m" (mask32r), "m" (mmx_one)
  1243. : "memory");
  1244. #endif
  1245. for (; idx<15; idx+=4) {
  1246. register int v = *(const uint32_t *)&s[idx], g = v & 0xff00ff00;
  1247. v &= 0xff00ff;
  1248. *(uint32_t *)&d[idx] = (v>>16) + g + (v<<16);
  1249. }
  1250. }
  1251. static inline void RENAME(rgb24tobgr24)(const uint8_t *src, uint8_t *dst, long src_size)
  1252. {
  1253. unsigned i;
  1254. #if COMPILE_TEMPLATE_MMX
  1255. x86_reg mmx_size= 23 - src_size;
  1256. __asm__ volatile (
  1257. "test %%"REG_a", %%"REG_a" \n\t"
  1258. "jns 2f \n\t"
  1259. "movq "MANGLE(mask24r)", %%mm5 \n\t"
  1260. "movq "MANGLE(mask24g)", %%mm6 \n\t"
  1261. "movq "MANGLE(mask24b)", %%mm7 \n\t"
  1262. ".p2align 4 \n\t"
  1263. "1: \n\t"
  1264. PREFETCH" 32(%1, %%"REG_a") \n\t"
  1265. "movq (%1, %%"REG_a"), %%mm0 \n\t" // BGR BGR BG
  1266. "movq (%1, %%"REG_a"), %%mm1 \n\t" // BGR BGR BG
  1267. "movq 2(%1, %%"REG_a"), %%mm2 \n\t" // R BGR BGR B
  1268. "psllq $16, %%mm0 \n\t" // 00 BGR BGR
  1269. "pand %%mm5, %%mm0 \n\t"
  1270. "pand %%mm6, %%mm1 \n\t"
  1271. "pand %%mm7, %%mm2 \n\t"
  1272. "por %%mm0, %%mm1 \n\t"
  1273. "por %%mm2, %%mm1 \n\t"
  1274. "movq 6(%1, %%"REG_a"), %%mm0 \n\t" // BGR BGR BG
  1275. MOVNTQ" %%mm1, (%2, %%"REG_a") \n\t" // RGB RGB RG
  1276. "movq 8(%1, %%"REG_a"), %%mm1 \n\t" // R BGR BGR B
  1277. "movq 10(%1, %%"REG_a"), %%mm2 \n\t" // GR BGR BGR
  1278. "pand %%mm7, %%mm0 \n\t"
  1279. "pand %%mm5, %%mm1 \n\t"
  1280. "pand %%mm6, %%mm2 \n\t"
  1281. "por %%mm0, %%mm1 \n\t"
  1282. "por %%mm2, %%mm1 \n\t"
  1283. "movq 14(%1, %%"REG_a"), %%mm0 \n\t" // R BGR BGR B
  1284. MOVNTQ" %%mm1, 8(%2, %%"REG_a") \n\t" // B RGB RGB R
  1285. "movq 16(%1, %%"REG_a"), %%mm1 \n\t" // GR BGR BGR
  1286. "movq 18(%1, %%"REG_a"), %%mm2 \n\t" // BGR BGR BG
  1287. "pand %%mm6, %%mm0 \n\t"
  1288. "pand %%mm7, %%mm1 \n\t"
  1289. "pand %%mm5, %%mm2 \n\t"
  1290. "por %%mm0, %%mm1 \n\t"
  1291. "por %%mm2, %%mm1 \n\t"
  1292. MOVNTQ" %%mm1, 16(%2, %%"REG_a") \n\t"
  1293. "add $24, %%"REG_a" \n\t"
  1294. " js 1b \n\t"
  1295. "2: \n\t"
  1296. : "+a" (mmx_size)
  1297. : "r" (src-mmx_size), "r"(dst-mmx_size)
  1298. );
  1299. __asm__ volatile(SFENCE:::"memory");
  1300. __asm__ volatile(EMMS:::"memory");
  1301. if (mmx_size==23) return; //finished, was multiple of 8
  1302. src+= src_size;
  1303. dst+= src_size;
  1304. src_size= 23-mmx_size;
  1305. src-= src_size;
  1306. dst-= src_size;
  1307. #endif
  1308. for (i=0; i<src_size; i+=3) {
  1309. register uint8_t x;
  1310. x = src[i + 2];
  1311. dst[i + 1] = src[i + 1];
  1312. dst[i + 2] = src[i + 0];
  1313. dst[i + 0] = x;
  1314. }
  1315. }
  1316. static inline void RENAME(yuvPlanartoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  1317. long width, long height,
  1318. long lumStride, long chromStride, long dstStride, long vertLumPerChroma)
  1319. {
  1320. long y;
  1321. const x86_reg chromWidth= width>>1;
  1322. for (y=0; y<height; y++) {
  1323. #if COMPILE_TEMPLATE_MMX
  1324. //FIXME handle 2 lines at once (fewer prefetches, reuse some chroma, but very likely memory-limited anyway)
  1325. __asm__ volatile(
  1326. "xor %%"REG_a", %%"REG_a" \n\t"
  1327. ".p2align 4 \n\t"
  1328. "1: \n\t"
  1329. PREFETCH" 32(%1, %%"REG_a", 2) \n\t"
  1330. PREFETCH" 32(%2, %%"REG_a") \n\t"
  1331. PREFETCH" 32(%3, %%"REG_a") \n\t"
  1332. "movq (%2, %%"REG_a"), %%mm0 \n\t" // U(0)
  1333. "movq %%mm0, %%mm2 \n\t" // U(0)
  1334. "movq (%3, %%"REG_a"), %%mm1 \n\t" // V(0)
  1335. "punpcklbw %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
  1336. "punpckhbw %%mm1, %%mm2 \n\t" // UVUV UVUV(8)
  1337. "movq (%1, %%"REG_a",2), %%mm3 \n\t" // Y(0)
  1338. "movq 8(%1, %%"REG_a",2), %%mm5 \n\t" // Y(8)
  1339. "movq %%mm3, %%mm4 \n\t" // Y(0)
  1340. "movq %%mm5, %%mm6 \n\t" // Y(8)
  1341. "punpcklbw %%mm0, %%mm3 \n\t" // YUYV YUYV(0)
  1342. "punpckhbw %%mm0, %%mm4 \n\t" // YUYV YUYV(4)
  1343. "punpcklbw %%mm2, %%mm5 \n\t" // YUYV YUYV(8)
  1344. "punpckhbw %%mm2, %%mm6 \n\t" // YUYV YUYV(12)
  1345. MOVNTQ" %%mm3, (%0, %%"REG_a", 4) \n\t"
  1346. MOVNTQ" %%mm4, 8(%0, %%"REG_a", 4) \n\t"
  1347. MOVNTQ" %%mm5, 16(%0, %%"REG_a", 4) \n\t"
  1348. MOVNTQ" %%mm6, 24(%0, %%"REG_a", 4) \n\t"
  1349. "add $8, %%"REG_a" \n\t"
  1350. "cmp %4, %%"REG_a" \n\t"
  1351. " jb 1b \n\t"
  1352. ::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "g" (chromWidth)
  1353. : "%"REG_a
  1354. );
  1355. #else
  1356. #if ARCH_ALPHA && HAVE_MVI
  1357. #define pl2yuy2(n) \
  1358. y1 = yc[n]; \
  1359. y2 = yc2[n]; \
  1360. u = uc[n]; \
  1361. v = vc[n]; \
  1362. __asm__("unpkbw %1, %0" : "=r"(y1) : "r"(y1)); \
  1363. __asm__("unpkbw %1, %0" : "=r"(y2) : "r"(y2)); \
  1364. __asm__("unpkbl %1, %0" : "=r"(u) : "r"(u)); \
  1365. __asm__("unpkbl %1, %0" : "=r"(v) : "r"(v)); \
  1366. yuv1 = (u << 8) + (v << 24); \
  1367. yuv2 = yuv1 + y2; \
  1368. yuv1 += y1; \
  1369. qdst[n] = yuv1; \
  1370. qdst2[n] = yuv2;
  1371. int i;
  1372. uint64_t *qdst = (uint64_t *) dst;
  1373. uint64_t *qdst2 = (uint64_t *) (dst + dstStride);
  1374. const uint32_t *yc = (uint32_t *) ysrc;
  1375. const uint32_t *yc2 = (uint32_t *) (ysrc + lumStride);
  1376. const uint16_t *uc = (uint16_t*) usrc, *vc = (uint16_t*) vsrc;
  1377. for (i = 0; i < chromWidth; i += 8) {
  1378. uint64_t y1, y2, yuv1, yuv2;
  1379. uint64_t u, v;
  1380. /* Prefetch */
  1381. __asm__("ldq $31,64(%0)" :: "r"(yc));
  1382. __asm__("ldq $31,64(%0)" :: "r"(yc2));
  1383. __asm__("ldq $31,64(%0)" :: "r"(uc));
  1384. __asm__("ldq $31,64(%0)" :: "r"(vc));
  1385. pl2yuy2(0);
  1386. pl2yuy2(1);
  1387. pl2yuy2(2);
  1388. pl2yuy2(3);
  1389. yc += 4;
  1390. yc2 += 4;
  1391. uc += 4;
  1392. vc += 4;
  1393. qdst += 4;
  1394. qdst2 += 4;
  1395. }
  1396. y++;
  1397. ysrc += lumStride;
  1398. dst += dstStride;
  1399. #elif HAVE_FAST_64BIT
  1400. int i;
  1401. uint64_t *ldst = (uint64_t *) dst;
  1402. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  1403. for (i = 0; i < chromWidth; i += 2) {
  1404. uint64_t k, l;
  1405. k = yc[0] + (uc[0] << 8) +
  1406. (yc[1] << 16) + (vc[0] << 24);
  1407. l = yc[2] + (uc[1] << 8) +
  1408. (yc[3] << 16) + (vc[1] << 24);
  1409. *ldst++ = k + (l << 32);
  1410. yc += 4;
  1411. uc += 2;
  1412. vc += 2;
  1413. }
  1414. #else
  1415. int i, *idst = (int32_t *) dst;
  1416. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  1417. for (i = 0; i < chromWidth; i++) {
  1418. #if HAVE_BIGENDIAN
  1419. *idst++ = (yc[0] << 24)+ (uc[0] << 16) +
  1420. (yc[1] << 8) + (vc[0] << 0);
  1421. #else
  1422. *idst++ = yc[0] + (uc[0] << 8) +
  1423. (yc[1] << 16) + (vc[0] << 24);
  1424. #endif
  1425. yc += 2;
  1426. uc++;
  1427. vc++;
  1428. }
  1429. #endif
  1430. #endif
  1431. if ((y&(vertLumPerChroma-1)) == vertLumPerChroma-1) {
  1432. usrc += chromStride;
  1433. vsrc += chromStride;
  1434. }
  1435. ysrc += lumStride;
  1436. dst += dstStride;
  1437. }
  1438. #if COMPILE_TEMPLATE_MMX
  1439. __asm__(EMMS" \n\t"
  1440. SFENCE" \n\t"
  1441. :::"memory");
  1442. #endif
  1443. }
  1444. /**
  1445. * Height should be a multiple of 2 and width should be a multiple of 16.
  1446. * (If this is a problem for anyone then tell me, and I will fix it.)
  1447. */
  1448. static inline void RENAME(yv12toyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  1449. long width, long height,
  1450. long lumStride, long chromStride, long dstStride)
  1451. {
  1452. //FIXME interpolate chroma
  1453. RENAME(yuvPlanartoyuy2)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 2);
  1454. }
  1455. static inline void RENAME(yuvPlanartouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  1456. long width, long height,
  1457. long lumStride, long chromStride, long dstStride, long vertLumPerChroma)
  1458. {
  1459. long y;
  1460. const x86_reg chromWidth= width>>1;
  1461. for (y=0; y<height; y++) {
  1462. #if COMPILE_TEMPLATE_MMX
  1463. //FIXME handle 2 lines at once (fewer prefetches, reuse some chroma, but very likely memory-limited anyway)
  1464. __asm__ volatile(
  1465. "xor %%"REG_a", %%"REG_a" \n\t"
  1466. ".p2align 4 \n\t"
  1467. "1: \n\t"
  1468. PREFETCH" 32(%1, %%"REG_a", 2) \n\t"
  1469. PREFETCH" 32(%2, %%"REG_a") \n\t"
  1470. PREFETCH" 32(%3, %%"REG_a") \n\t"
  1471. "movq (%2, %%"REG_a"), %%mm0 \n\t" // U(0)
  1472. "movq %%mm0, %%mm2 \n\t" // U(0)
  1473. "movq (%3, %%"REG_a"), %%mm1 \n\t" // V(0)
  1474. "punpcklbw %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
  1475. "punpckhbw %%mm1, %%mm2 \n\t" // UVUV UVUV(8)
  1476. "movq (%1, %%"REG_a",2), %%mm3 \n\t" // Y(0)
  1477. "movq 8(%1, %%"REG_a",2), %%mm5 \n\t" // Y(8)
  1478. "movq %%mm0, %%mm4 \n\t" // Y(0)
  1479. "movq %%mm2, %%mm6 \n\t" // Y(8)
  1480. "punpcklbw %%mm3, %%mm0 \n\t" // YUYV YUYV(0)
  1481. "punpckhbw %%mm3, %%mm4 \n\t" // YUYV YUYV(4)
  1482. "punpcklbw %%mm5, %%mm2 \n\t" // YUYV YUYV(8)
  1483. "punpckhbw %%mm5, %%mm6 \n\t" // YUYV YUYV(12)
  1484. MOVNTQ" %%mm0, (%0, %%"REG_a", 4) \n\t"
  1485. MOVNTQ" %%mm4, 8(%0, %%"REG_a", 4) \n\t"
  1486. MOVNTQ" %%mm2, 16(%0, %%"REG_a", 4) \n\t"
  1487. MOVNTQ" %%mm6, 24(%0, %%"REG_a", 4) \n\t"
  1488. "add $8, %%"REG_a" \n\t"
  1489. "cmp %4, %%"REG_a" \n\t"
  1490. " jb 1b \n\t"
  1491. ::"r"(dst), "r"(ysrc), "r"(usrc), "r"(vsrc), "g" (chromWidth)
  1492. : "%"REG_a
  1493. );
  1494. #else
  1495. //FIXME adapt the Alpha ASM code from yv12->yuy2
  1496. #if HAVE_FAST_64BIT
  1497. int i;
  1498. uint64_t *ldst = (uint64_t *) dst;
  1499. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  1500. for (i = 0; i < chromWidth; i += 2) {
  1501. uint64_t k, l;
  1502. k = uc[0] + (yc[0] << 8) +
  1503. (vc[0] << 16) + (yc[1] << 24);
  1504. l = uc[1] + (yc[2] << 8) +
  1505. (vc[1] << 16) + (yc[3] << 24);
  1506. *ldst++ = k + (l << 32);
  1507. yc += 4;
  1508. uc += 2;
  1509. vc += 2;
  1510. }
  1511. #else
  1512. int i, *idst = (int32_t *) dst;
  1513. const uint8_t *yc = ysrc, *uc = usrc, *vc = vsrc;
  1514. for (i = 0; i < chromWidth; i++) {
  1515. #if HAVE_BIGENDIAN
  1516. *idst++ = (uc[0] << 24)+ (yc[0] << 16) +
  1517. (vc[0] << 8) + (yc[1] << 0);
  1518. #else
  1519. *idst++ = uc[0] + (yc[0] << 8) +
  1520. (vc[0] << 16) + (yc[1] << 24);
  1521. #endif
  1522. yc += 2;
  1523. uc++;
  1524. vc++;
  1525. }
  1526. #endif
  1527. #endif
  1528. if ((y&(vertLumPerChroma-1)) == vertLumPerChroma-1) {
  1529. usrc += chromStride;
  1530. vsrc += chromStride;
  1531. }
  1532. ysrc += lumStride;
  1533. dst += dstStride;
  1534. }
  1535. #if COMPILE_TEMPLATE_MMX
  1536. __asm__(EMMS" \n\t"
  1537. SFENCE" \n\t"
  1538. :::"memory");
  1539. #endif
  1540. }
  1541. /**
  1542. * Height should be a multiple of 2 and width should be a multiple of 16
  1543. * (If this is a problem for anyone then tell me, and I will fix it.)
  1544. */
  1545. static inline void RENAME(yv12touyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  1546. long width, long height,
  1547. long lumStride, long chromStride, long dstStride)
  1548. {
  1549. //FIXME interpolate chroma
  1550. RENAME(yuvPlanartouyvy)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 2);
  1551. }
  1552. /**
  1553. * Width should be a multiple of 16.
  1554. */
  1555. static inline void RENAME(yuv422ptouyvy)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  1556. long width, long height,
  1557. long lumStride, long chromStride, long dstStride)
  1558. {
  1559. RENAME(yuvPlanartouyvy)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 1);
  1560. }
  1561. /**
  1562. * Width should be a multiple of 16.
  1563. */
  1564. static inline void RENAME(yuv422ptoyuy2)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc, uint8_t *dst,
  1565. long width, long height,
  1566. long lumStride, long chromStride, long dstStride)
  1567. {
  1568. RENAME(yuvPlanartoyuy2)(ysrc, usrc, vsrc, dst, width, height, lumStride, chromStride, dstStride, 1);
  1569. }
  1570. /**
  1571. * Height should be a multiple of 2 and width should be a multiple of 16.
  1572. * (If this is a problem for anyone then tell me, and I will fix it.)
  1573. */
  1574. static inline void RENAME(yuy2toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  1575. long width, long height,
  1576. long lumStride, long chromStride, long srcStride)
  1577. {
  1578. long y;
  1579. const x86_reg chromWidth= width>>1;
  1580. for (y=0; y<height; y+=2) {
  1581. #if COMPILE_TEMPLATE_MMX
  1582. __asm__ volatile(
  1583. "xor %%"REG_a", %%"REG_a" \n\t"
  1584. "pcmpeqw %%mm7, %%mm7 \n\t"
  1585. "psrlw $8, %%mm7 \n\t" // FF,00,FF,00...
  1586. ".p2align 4 \n\t"
  1587. "1: \n\t"
  1588. PREFETCH" 64(%0, %%"REG_a", 4) \n\t"
  1589. "movq (%0, %%"REG_a", 4), %%mm0 \n\t" // YUYV YUYV(0)
  1590. "movq 8(%0, %%"REG_a", 4), %%mm1 \n\t" // YUYV YUYV(4)
  1591. "movq %%mm0, %%mm2 \n\t" // YUYV YUYV(0)
  1592. "movq %%mm1, %%mm3 \n\t" // YUYV YUYV(4)
  1593. "psrlw $8, %%mm0 \n\t" // U0V0 U0V0(0)
  1594. "psrlw $8, %%mm1 \n\t" // U0V0 U0V0(4)
  1595. "pand %%mm7, %%mm2 \n\t" // Y0Y0 Y0Y0(0)
  1596. "pand %%mm7, %%mm3 \n\t" // Y0Y0 Y0Y0(4)
  1597. "packuswb %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
  1598. "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(0)
  1599. MOVNTQ" %%mm2, (%1, %%"REG_a", 2) \n\t"
  1600. "movq 16(%0, %%"REG_a", 4), %%mm1 \n\t" // YUYV YUYV(8)
  1601. "movq 24(%0, %%"REG_a", 4), %%mm2 \n\t" // YUYV YUYV(12)
  1602. "movq %%mm1, %%mm3 \n\t" // YUYV YUYV(8)
  1603. "movq %%mm2, %%mm4 \n\t" // YUYV YUYV(12)
  1604. "psrlw $8, %%mm1 \n\t" // U0V0 U0V0(8)
  1605. "psrlw $8, %%mm2 \n\t" // U0V0 U0V0(12)
  1606. "pand %%mm7, %%mm3 \n\t" // Y0Y0 Y0Y0(8)
  1607. "pand %%mm7, %%mm4 \n\t" // Y0Y0 Y0Y0(12)
  1608. "packuswb %%mm2, %%mm1 \n\t" // UVUV UVUV(8)
  1609. "packuswb %%mm4, %%mm3 \n\t" // YYYY YYYY(8)
  1610. MOVNTQ" %%mm3, 8(%1, %%"REG_a", 2) \n\t"
  1611. "movq %%mm0, %%mm2 \n\t" // UVUV UVUV(0)
  1612. "movq %%mm1, %%mm3 \n\t" // UVUV UVUV(8)
  1613. "psrlw $8, %%mm0 \n\t" // V0V0 V0V0(0)
  1614. "psrlw $8, %%mm1 \n\t" // V0V0 V0V0(8)
  1615. "pand %%mm7, %%mm2 \n\t" // U0U0 U0U0(0)
  1616. "pand %%mm7, %%mm3 \n\t" // U0U0 U0U0(8)
  1617. "packuswb %%mm1, %%mm0 \n\t" // VVVV VVVV(0)
  1618. "packuswb %%mm3, %%mm2 \n\t" // UUUU UUUU(0)
  1619. MOVNTQ" %%mm0, (%3, %%"REG_a") \n\t"
  1620. MOVNTQ" %%mm2, (%2, %%"REG_a") \n\t"
  1621. "add $8, %%"REG_a" \n\t"
  1622. "cmp %4, %%"REG_a" \n\t"
  1623. " jb 1b \n\t"
  1624. ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
  1625. : "memory", "%"REG_a
  1626. );
  1627. ydst += lumStride;
  1628. src += srcStride;
  1629. __asm__ volatile(
  1630. "xor %%"REG_a", %%"REG_a" \n\t"
  1631. ".p2align 4 \n\t"
  1632. "1: \n\t"
  1633. PREFETCH" 64(%0, %%"REG_a", 4) \n\t"
  1634. "movq (%0, %%"REG_a", 4), %%mm0 \n\t" // YUYV YUYV(0)
  1635. "movq 8(%0, %%"REG_a", 4), %%mm1 \n\t" // YUYV YUYV(4)
  1636. "movq 16(%0, %%"REG_a", 4), %%mm2 \n\t" // YUYV YUYV(8)
  1637. "movq 24(%0, %%"REG_a", 4), %%mm3 \n\t" // YUYV YUYV(12)
  1638. "pand %%mm7, %%mm0 \n\t" // Y0Y0 Y0Y0(0)
  1639. "pand %%mm7, %%mm1 \n\t" // Y0Y0 Y0Y0(4)
  1640. "pand %%mm7, %%mm2 \n\t" // Y0Y0 Y0Y0(8)
  1641. "pand %%mm7, %%mm3 \n\t" // Y0Y0 Y0Y0(12)
  1642. "packuswb %%mm1, %%mm0 \n\t" // YYYY YYYY(0)
  1643. "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(8)
  1644. MOVNTQ" %%mm0, (%1, %%"REG_a", 2) \n\t"
  1645. MOVNTQ" %%mm2, 8(%1, %%"REG_a", 2) \n\t"
  1646. "add $8, %%"REG_a" \n\t"
  1647. "cmp %4, %%"REG_a" \n\t"
  1648. " jb 1b \n\t"
  1649. ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
  1650. : "memory", "%"REG_a
  1651. );
  1652. #else
  1653. long i;
  1654. for (i=0; i<chromWidth; i++) {
  1655. ydst[2*i+0] = src[4*i+0];
  1656. udst[i] = src[4*i+1];
  1657. ydst[2*i+1] = src[4*i+2];
  1658. vdst[i] = src[4*i+3];
  1659. }
  1660. ydst += lumStride;
  1661. src += srcStride;
  1662. for (i=0; i<chromWidth; i++) {
  1663. ydst[2*i+0] = src[4*i+0];
  1664. ydst[2*i+1] = src[4*i+2];
  1665. }
  1666. #endif
  1667. udst += chromStride;
  1668. vdst += chromStride;
  1669. ydst += lumStride;
  1670. src += srcStride;
  1671. }
  1672. #if COMPILE_TEMPLATE_MMX
  1673. __asm__ volatile(EMMS" \n\t"
  1674. SFENCE" \n\t"
  1675. :::"memory");
  1676. #endif
  1677. }
  1678. static inline void RENAME(planar2x)(const uint8_t *src, uint8_t *dst, long srcWidth, long srcHeight, long srcStride, long dstStride)
  1679. {
  1680. long x,y;
  1681. dst[0]= src[0];
  1682. // first line
  1683. for (x=0; x<srcWidth-1; x++) {
  1684. dst[2*x+1]= (3*src[x] + src[x+1])>>2;
  1685. dst[2*x+2]= ( src[x] + 3*src[x+1])>>2;
  1686. }
  1687. dst[2*srcWidth-1]= src[srcWidth-1];
  1688. dst+= dstStride;
  1689. for (y=1; y<srcHeight; y++) {
  1690. #if COMPILE_TEMPLATE_MMX2 || COMPILE_TEMPLATE_AMD3DNOW
  1691. const x86_reg mmxSize= srcWidth&~15;
  1692. __asm__ volatile(
  1693. "mov %4, %%"REG_a" \n\t"
  1694. "movq "MANGLE(mmx_ff)", %%mm0 \n\t"
  1695. "movq (%0, %%"REG_a"), %%mm4 \n\t"
  1696. "movq %%mm4, %%mm2 \n\t"
  1697. "psllq $8, %%mm4 \n\t"
  1698. "pand %%mm0, %%mm2 \n\t"
  1699. "por %%mm2, %%mm4 \n\t"
  1700. "movq (%1, %%"REG_a"), %%mm5 \n\t"
  1701. "movq %%mm5, %%mm3 \n\t"
  1702. "psllq $8, %%mm5 \n\t"
  1703. "pand %%mm0, %%mm3 \n\t"
  1704. "por %%mm3, %%mm5 \n\t"
  1705. "1: \n\t"
  1706. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  1707. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  1708. "movq 1(%0, %%"REG_a"), %%mm2 \n\t"
  1709. "movq 1(%1, %%"REG_a"), %%mm3 \n\t"
  1710. PAVGB" %%mm0, %%mm5 \n\t"
  1711. PAVGB" %%mm0, %%mm3 \n\t"
  1712. PAVGB" %%mm0, %%mm5 \n\t"
  1713. PAVGB" %%mm0, %%mm3 \n\t"
  1714. PAVGB" %%mm1, %%mm4 \n\t"
  1715. PAVGB" %%mm1, %%mm2 \n\t"
  1716. PAVGB" %%mm1, %%mm4 \n\t"
  1717. PAVGB" %%mm1, %%mm2 \n\t"
  1718. "movq %%mm5, %%mm7 \n\t"
  1719. "movq %%mm4, %%mm6 \n\t"
  1720. "punpcklbw %%mm3, %%mm5 \n\t"
  1721. "punpckhbw %%mm3, %%mm7 \n\t"
  1722. "punpcklbw %%mm2, %%mm4 \n\t"
  1723. "punpckhbw %%mm2, %%mm6 \n\t"
  1724. #if 1
  1725. MOVNTQ" %%mm5, (%2, %%"REG_a", 2) \n\t"
  1726. MOVNTQ" %%mm7, 8(%2, %%"REG_a", 2) \n\t"
  1727. MOVNTQ" %%mm4, (%3, %%"REG_a", 2) \n\t"
  1728. MOVNTQ" %%mm6, 8(%3, %%"REG_a", 2) \n\t"
  1729. #else
  1730. "movq %%mm5, (%2, %%"REG_a", 2) \n\t"
  1731. "movq %%mm7, 8(%2, %%"REG_a", 2) \n\t"
  1732. "movq %%mm4, (%3, %%"REG_a", 2) \n\t"
  1733. "movq %%mm6, 8(%3, %%"REG_a", 2) \n\t"
  1734. #endif
  1735. "add $8, %%"REG_a" \n\t"
  1736. "movq -1(%0, %%"REG_a"), %%mm4 \n\t"
  1737. "movq -1(%1, %%"REG_a"), %%mm5 \n\t"
  1738. " js 1b \n\t"
  1739. :: "r" (src + mmxSize ), "r" (src + srcStride + mmxSize ),
  1740. "r" (dst + mmxSize*2), "r" (dst + dstStride + mmxSize*2),
  1741. "g" (-mmxSize)
  1742. : "%"REG_a
  1743. );
  1744. #else
  1745. const x86_reg mmxSize=1;
  1746. dst[0 ]= (3*src[0] + src[srcStride])>>2;
  1747. dst[dstStride]= ( src[0] + 3*src[srcStride])>>2;
  1748. #endif
  1749. for (x=mmxSize-1; x<srcWidth-1; x++) {
  1750. dst[2*x +1]= (3*src[x+0] + src[x+srcStride+1])>>2;
  1751. dst[2*x+dstStride+2]= ( src[x+0] + 3*src[x+srcStride+1])>>2;
  1752. dst[2*x+dstStride+1]= ( src[x+1] + 3*src[x+srcStride ])>>2;
  1753. dst[2*x +2]= (3*src[x+1] + src[x+srcStride ])>>2;
  1754. }
  1755. dst[srcWidth*2 -1 ]= (3*src[srcWidth-1] + src[srcWidth-1 + srcStride])>>2;
  1756. dst[srcWidth*2 -1 + dstStride]= ( src[srcWidth-1] + 3*src[srcWidth-1 + srcStride])>>2;
  1757. dst+=dstStride*2;
  1758. src+=srcStride;
  1759. }
  1760. // last line
  1761. #if 1
  1762. dst[0]= src[0];
  1763. for (x=0; x<srcWidth-1; x++) {
  1764. dst[2*x+1]= (3*src[x] + src[x+1])>>2;
  1765. dst[2*x+2]= ( src[x] + 3*src[x+1])>>2;
  1766. }
  1767. dst[2*srcWidth-1]= src[srcWidth-1];
  1768. #else
  1769. for (x=0; x<srcWidth; x++) {
  1770. dst[2*x+0]=
  1771. dst[2*x+1]= src[x];
  1772. }
  1773. #endif
  1774. #if COMPILE_TEMPLATE_MMX
  1775. __asm__ volatile(EMMS" \n\t"
  1776. SFENCE" \n\t"
  1777. :::"memory");
  1778. #endif
  1779. }
  1780. /**
  1781. * Height should be a multiple of 2 and width should be a multiple of 16.
  1782. * (If this is a problem for anyone then tell me, and I will fix it.)
  1783. * Chrominance data is only taken from every second line, others are ignored.
  1784. * FIXME: Write HQ version.
  1785. */
  1786. static inline void RENAME(uyvytoyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  1787. long width, long height,
  1788. long lumStride, long chromStride, long srcStride)
  1789. {
  1790. long y;
  1791. const x86_reg chromWidth= width>>1;
  1792. for (y=0; y<height; y+=2) {
  1793. #if COMPILE_TEMPLATE_MMX
  1794. __asm__ volatile(
  1795. "xor %%"REG_a", %%"REG_a" \n\t"
  1796. "pcmpeqw %%mm7, %%mm7 \n\t"
  1797. "psrlw $8, %%mm7 \n\t" // FF,00,FF,00...
  1798. ".p2align 4 \n\t"
  1799. "1: \n\t"
  1800. PREFETCH" 64(%0, %%"REG_a", 4) \n\t"
  1801. "movq (%0, %%"REG_a", 4), %%mm0 \n\t" // UYVY UYVY(0)
  1802. "movq 8(%0, %%"REG_a", 4), %%mm1 \n\t" // UYVY UYVY(4)
  1803. "movq %%mm0, %%mm2 \n\t" // UYVY UYVY(0)
  1804. "movq %%mm1, %%mm3 \n\t" // UYVY UYVY(4)
  1805. "pand %%mm7, %%mm0 \n\t" // U0V0 U0V0(0)
  1806. "pand %%mm7, %%mm1 \n\t" // U0V0 U0V0(4)
  1807. "psrlw $8, %%mm2 \n\t" // Y0Y0 Y0Y0(0)
  1808. "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(4)
  1809. "packuswb %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
  1810. "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(0)
  1811. MOVNTQ" %%mm2, (%1, %%"REG_a", 2) \n\t"
  1812. "movq 16(%0, %%"REG_a", 4), %%mm1 \n\t" // UYVY UYVY(8)
  1813. "movq 24(%0, %%"REG_a", 4), %%mm2 \n\t" // UYVY UYVY(12)
  1814. "movq %%mm1, %%mm3 \n\t" // UYVY UYVY(8)
  1815. "movq %%mm2, %%mm4 \n\t" // UYVY UYVY(12)
  1816. "pand %%mm7, %%mm1 \n\t" // U0V0 U0V0(8)
  1817. "pand %%mm7, %%mm2 \n\t" // U0V0 U0V0(12)
  1818. "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(8)
  1819. "psrlw $8, %%mm4 \n\t" // Y0Y0 Y0Y0(12)
  1820. "packuswb %%mm2, %%mm1 \n\t" // UVUV UVUV(8)
  1821. "packuswb %%mm4, %%mm3 \n\t" // YYYY YYYY(8)
  1822. MOVNTQ" %%mm3, 8(%1, %%"REG_a", 2) \n\t"
  1823. "movq %%mm0, %%mm2 \n\t" // UVUV UVUV(0)
  1824. "movq %%mm1, %%mm3 \n\t" // UVUV UVUV(8)
  1825. "psrlw $8, %%mm0 \n\t" // V0V0 V0V0(0)
  1826. "psrlw $8, %%mm1 \n\t" // V0V0 V0V0(8)
  1827. "pand %%mm7, %%mm2 \n\t" // U0U0 U0U0(0)
  1828. "pand %%mm7, %%mm3 \n\t" // U0U0 U0U0(8)
  1829. "packuswb %%mm1, %%mm0 \n\t" // VVVV VVVV(0)
  1830. "packuswb %%mm3, %%mm2 \n\t" // UUUU UUUU(0)
  1831. MOVNTQ" %%mm0, (%3, %%"REG_a") \n\t"
  1832. MOVNTQ" %%mm2, (%2, %%"REG_a") \n\t"
  1833. "add $8, %%"REG_a" \n\t"
  1834. "cmp %4, %%"REG_a" \n\t"
  1835. " jb 1b \n\t"
  1836. ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
  1837. : "memory", "%"REG_a
  1838. );
  1839. ydst += lumStride;
  1840. src += srcStride;
  1841. __asm__ volatile(
  1842. "xor %%"REG_a", %%"REG_a" \n\t"
  1843. ".p2align 4 \n\t"
  1844. "1: \n\t"
  1845. PREFETCH" 64(%0, %%"REG_a", 4) \n\t"
  1846. "movq (%0, %%"REG_a", 4), %%mm0 \n\t" // YUYV YUYV(0)
  1847. "movq 8(%0, %%"REG_a", 4), %%mm1 \n\t" // YUYV YUYV(4)
  1848. "movq 16(%0, %%"REG_a", 4), %%mm2 \n\t" // YUYV YUYV(8)
  1849. "movq 24(%0, %%"REG_a", 4), %%mm3 \n\t" // YUYV YUYV(12)
  1850. "psrlw $8, %%mm0 \n\t" // Y0Y0 Y0Y0(0)
  1851. "psrlw $8, %%mm1 \n\t" // Y0Y0 Y0Y0(4)
  1852. "psrlw $8, %%mm2 \n\t" // Y0Y0 Y0Y0(8)
  1853. "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(12)
  1854. "packuswb %%mm1, %%mm0 \n\t" // YYYY YYYY(0)
  1855. "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(8)
  1856. MOVNTQ" %%mm0, (%1, %%"REG_a", 2) \n\t"
  1857. MOVNTQ" %%mm2, 8(%1, %%"REG_a", 2) \n\t"
  1858. "add $8, %%"REG_a" \n\t"
  1859. "cmp %4, %%"REG_a" \n\t"
  1860. " jb 1b \n\t"
  1861. ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
  1862. : "memory", "%"REG_a
  1863. );
  1864. #else
  1865. long i;
  1866. for (i=0; i<chromWidth; i++) {
  1867. udst[i] = src[4*i+0];
  1868. ydst[2*i+0] = src[4*i+1];
  1869. vdst[i] = src[4*i+2];
  1870. ydst[2*i+1] = src[4*i+3];
  1871. }
  1872. ydst += lumStride;
  1873. src += srcStride;
  1874. for (i=0; i<chromWidth; i++) {
  1875. ydst[2*i+0] = src[4*i+1];
  1876. ydst[2*i+1] = src[4*i+3];
  1877. }
  1878. #endif
  1879. udst += chromStride;
  1880. vdst += chromStride;
  1881. ydst += lumStride;
  1882. src += srcStride;
  1883. }
  1884. #if COMPILE_TEMPLATE_MMX
  1885. __asm__ volatile(EMMS" \n\t"
  1886. SFENCE" \n\t"
  1887. :::"memory");
  1888. #endif
  1889. }
  1890. /**
  1891. * Height should be a multiple of 2 and width should be a multiple of 2.
  1892. * (If this is a problem for anyone then tell me, and I will fix it.)
  1893. * Chrominance data is only taken from every second line,
  1894. * others are ignored in the C version.
  1895. * FIXME: Write HQ version.
  1896. */
  1897. static inline void RENAME(rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  1898. long width, long height,
  1899. long lumStride, long chromStride, long srcStride)
  1900. {
  1901. long y;
  1902. const x86_reg chromWidth= width>>1;
  1903. #if COMPILE_TEMPLATE_MMX
  1904. for (y=0; y<height-2; y+=2) {
  1905. long i;
  1906. for (i=0; i<2; i++) {
  1907. __asm__ volatile(
  1908. "mov %2, %%"REG_a" \n\t"
  1909. "movq "MANGLE(ff_bgr2YCoeff)", %%mm6 \n\t"
  1910. "movq "MANGLE(ff_w1111)", %%mm5 \n\t"
  1911. "pxor %%mm7, %%mm7 \n\t"
  1912. "lea (%%"REG_a", %%"REG_a", 2), %%"REG_d" \n\t"
  1913. ".p2align 4 \n\t"
  1914. "1: \n\t"
  1915. PREFETCH" 64(%0, %%"REG_d") \n\t"
  1916. "movd (%0, %%"REG_d"), %%mm0 \n\t"
  1917. "movd 3(%0, %%"REG_d"), %%mm1 \n\t"
  1918. "punpcklbw %%mm7, %%mm0 \n\t"
  1919. "punpcklbw %%mm7, %%mm1 \n\t"
  1920. "movd 6(%0, %%"REG_d"), %%mm2 \n\t"
  1921. "movd 9(%0, %%"REG_d"), %%mm3 \n\t"
  1922. "punpcklbw %%mm7, %%mm2 \n\t"
  1923. "punpcklbw %%mm7, %%mm3 \n\t"
  1924. "pmaddwd %%mm6, %%mm0 \n\t"
  1925. "pmaddwd %%mm6, %%mm1 \n\t"
  1926. "pmaddwd %%mm6, %%mm2 \n\t"
  1927. "pmaddwd %%mm6, %%mm3 \n\t"
  1928. #ifndef FAST_BGR2YV12
  1929. "psrad $8, %%mm0 \n\t"
  1930. "psrad $8, %%mm1 \n\t"
  1931. "psrad $8, %%mm2 \n\t"
  1932. "psrad $8, %%mm3 \n\t"
  1933. #endif
  1934. "packssdw %%mm1, %%mm0 \n\t"
  1935. "packssdw %%mm3, %%mm2 \n\t"
  1936. "pmaddwd %%mm5, %%mm0 \n\t"
  1937. "pmaddwd %%mm5, %%mm2 \n\t"
  1938. "packssdw %%mm2, %%mm0 \n\t"
  1939. "psraw $7, %%mm0 \n\t"
  1940. "movd 12(%0, %%"REG_d"), %%mm4 \n\t"
  1941. "movd 15(%0, %%"REG_d"), %%mm1 \n\t"
  1942. "punpcklbw %%mm7, %%mm4 \n\t"
  1943. "punpcklbw %%mm7, %%mm1 \n\t"
  1944. "movd 18(%0, %%"REG_d"), %%mm2 \n\t"
  1945. "movd 21(%0, %%"REG_d"), %%mm3 \n\t"
  1946. "punpcklbw %%mm7, %%mm2 \n\t"
  1947. "punpcklbw %%mm7, %%mm3 \n\t"
  1948. "pmaddwd %%mm6, %%mm4 \n\t"
  1949. "pmaddwd %%mm6, %%mm1 \n\t"
  1950. "pmaddwd %%mm6, %%mm2 \n\t"
  1951. "pmaddwd %%mm6, %%mm3 \n\t"
  1952. #ifndef FAST_BGR2YV12
  1953. "psrad $8, %%mm4 \n\t"
  1954. "psrad $8, %%mm1 \n\t"
  1955. "psrad $8, %%mm2 \n\t"
  1956. "psrad $8, %%mm3 \n\t"
  1957. #endif
  1958. "packssdw %%mm1, %%mm4 \n\t"
  1959. "packssdw %%mm3, %%mm2 \n\t"
  1960. "pmaddwd %%mm5, %%mm4 \n\t"
  1961. "pmaddwd %%mm5, %%mm2 \n\t"
  1962. "add $24, %%"REG_d" \n\t"
  1963. "packssdw %%mm2, %%mm4 \n\t"
  1964. "psraw $7, %%mm4 \n\t"
  1965. "packuswb %%mm4, %%mm0 \n\t"
  1966. "paddusb "MANGLE(ff_bgr2YOffset)", %%mm0 \n\t"
  1967. MOVNTQ" %%mm0, (%1, %%"REG_a") \n\t"
  1968. "add $8, %%"REG_a" \n\t"
  1969. " js 1b \n\t"
  1970. : : "r" (src+width*3), "r" (ydst+width), "g" ((x86_reg)-width)
  1971. : "%"REG_a, "%"REG_d
  1972. );
  1973. ydst += lumStride;
  1974. src += srcStride;
  1975. }
  1976. src -= srcStride*2;
  1977. __asm__ volatile(
  1978. "mov %4, %%"REG_a" \n\t"
  1979. "movq "MANGLE(ff_w1111)", %%mm5 \n\t"
  1980. "movq "MANGLE(ff_bgr2UCoeff)", %%mm6 \n\t"
  1981. "pxor %%mm7, %%mm7 \n\t"
  1982. "lea (%%"REG_a", %%"REG_a", 2), %%"REG_d" \n\t"
  1983. "add %%"REG_d", %%"REG_d" \n\t"
  1984. ".p2align 4 \n\t"
  1985. "1: \n\t"
  1986. PREFETCH" 64(%0, %%"REG_d") \n\t"
  1987. PREFETCH" 64(%1, %%"REG_d") \n\t"
  1988. #if COMPILE_TEMPLATE_MMX2 || COMPILE_TEMPLATE_AMD3DNOW
  1989. "movq (%0, %%"REG_d"), %%mm0 \n\t"
  1990. "movq (%1, %%"REG_d"), %%mm1 \n\t"
  1991. "movq 6(%0, %%"REG_d"), %%mm2 \n\t"
  1992. "movq 6(%1, %%"REG_d"), %%mm3 \n\t"
  1993. PAVGB" %%mm1, %%mm0 \n\t"
  1994. PAVGB" %%mm3, %%mm2 \n\t"
  1995. "movq %%mm0, %%mm1 \n\t"
  1996. "movq %%mm2, %%mm3 \n\t"
  1997. "psrlq $24, %%mm0 \n\t"
  1998. "psrlq $24, %%mm2 \n\t"
  1999. PAVGB" %%mm1, %%mm0 \n\t"
  2000. PAVGB" %%mm3, %%mm2 \n\t"
  2001. "punpcklbw %%mm7, %%mm0 \n\t"
  2002. "punpcklbw %%mm7, %%mm2 \n\t"
  2003. #else
  2004. "movd (%0, %%"REG_d"), %%mm0 \n\t"
  2005. "movd (%1, %%"REG_d"), %%mm1 \n\t"
  2006. "movd 3(%0, %%"REG_d"), %%mm2 \n\t"
  2007. "movd 3(%1, %%"REG_d"), %%mm3 \n\t"
  2008. "punpcklbw %%mm7, %%mm0 \n\t"
  2009. "punpcklbw %%mm7, %%mm1 \n\t"
  2010. "punpcklbw %%mm7, %%mm2 \n\t"
  2011. "punpcklbw %%mm7, %%mm3 \n\t"
  2012. "paddw %%mm1, %%mm0 \n\t"
  2013. "paddw %%mm3, %%mm2 \n\t"
  2014. "paddw %%mm2, %%mm0 \n\t"
  2015. "movd 6(%0, %%"REG_d"), %%mm4 \n\t"
  2016. "movd 6(%1, %%"REG_d"), %%mm1 \n\t"
  2017. "movd 9(%0, %%"REG_d"), %%mm2 \n\t"
  2018. "movd 9(%1, %%"REG_d"), %%mm3 \n\t"
  2019. "punpcklbw %%mm7, %%mm4 \n\t"
  2020. "punpcklbw %%mm7, %%mm1 \n\t"
  2021. "punpcklbw %%mm7, %%mm2 \n\t"
  2022. "punpcklbw %%mm7, %%mm3 \n\t"
  2023. "paddw %%mm1, %%mm4 \n\t"
  2024. "paddw %%mm3, %%mm2 \n\t"
  2025. "paddw %%mm4, %%mm2 \n\t"
  2026. "psrlw $2, %%mm0 \n\t"
  2027. "psrlw $2, %%mm2 \n\t"
  2028. #endif
  2029. "movq "MANGLE(ff_bgr2VCoeff)", %%mm1 \n\t"
  2030. "movq "MANGLE(ff_bgr2VCoeff)", %%mm3 \n\t"
  2031. "pmaddwd %%mm0, %%mm1 \n\t"
  2032. "pmaddwd %%mm2, %%mm3 \n\t"
  2033. "pmaddwd %%mm6, %%mm0 \n\t"
  2034. "pmaddwd %%mm6, %%mm2 \n\t"
  2035. #ifndef FAST_BGR2YV12
  2036. "psrad $8, %%mm0 \n\t"
  2037. "psrad $8, %%mm1 \n\t"
  2038. "psrad $8, %%mm2 \n\t"
  2039. "psrad $8, %%mm3 \n\t"
  2040. #endif
  2041. "packssdw %%mm2, %%mm0 \n\t"
  2042. "packssdw %%mm3, %%mm1 \n\t"
  2043. "pmaddwd %%mm5, %%mm0 \n\t"
  2044. "pmaddwd %%mm5, %%mm1 \n\t"
  2045. "packssdw %%mm1, %%mm0 \n\t" // V1 V0 U1 U0
  2046. "psraw $7, %%mm0 \n\t"
  2047. #if COMPILE_TEMPLATE_MMX2 || COMPILE_TEMPLATE_AMD3DNOW
  2048. "movq 12(%0, %%"REG_d"), %%mm4 \n\t"
  2049. "movq 12(%1, %%"REG_d"), %%mm1 \n\t"
  2050. "movq 18(%0, %%"REG_d"), %%mm2 \n\t"
  2051. "movq 18(%1, %%"REG_d"), %%mm3 \n\t"
  2052. PAVGB" %%mm1, %%mm4 \n\t"
  2053. PAVGB" %%mm3, %%mm2 \n\t"
  2054. "movq %%mm4, %%mm1 \n\t"
  2055. "movq %%mm2, %%mm3 \n\t"
  2056. "psrlq $24, %%mm4 \n\t"
  2057. "psrlq $24, %%mm2 \n\t"
  2058. PAVGB" %%mm1, %%mm4 \n\t"
  2059. PAVGB" %%mm3, %%mm2 \n\t"
  2060. "punpcklbw %%mm7, %%mm4 \n\t"
  2061. "punpcklbw %%mm7, %%mm2 \n\t"
  2062. #else
  2063. "movd 12(%0, %%"REG_d"), %%mm4 \n\t"
  2064. "movd 12(%1, %%"REG_d"), %%mm1 \n\t"
  2065. "movd 15(%0, %%"REG_d"), %%mm2 \n\t"
  2066. "movd 15(%1, %%"REG_d"), %%mm3 \n\t"
  2067. "punpcklbw %%mm7, %%mm4 \n\t"
  2068. "punpcklbw %%mm7, %%mm1 \n\t"
  2069. "punpcklbw %%mm7, %%mm2 \n\t"
  2070. "punpcklbw %%mm7, %%mm3 \n\t"
  2071. "paddw %%mm1, %%mm4 \n\t"
  2072. "paddw %%mm3, %%mm2 \n\t"
  2073. "paddw %%mm2, %%mm4 \n\t"
  2074. "movd 18(%0, %%"REG_d"), %%mm5 \n\t"
  2075. "movd 18(%1, %%"REG_d"), %%mm1 \n\t"
  2076. "movd 21(%0, %%"REG_d"), %%mm2 \n\t"
  2077. "movd 21(%1, %%"REG_d"), %%mm3 \n\t"
  2078. "punpcklbw %%mm7, %%mm5 \n\t"
  2079. "punpcklbw %%mm7, %%mm1 \n\t"
  2080. "punpcklbw %%mm7, %%mm2 \n\t"
  2081. "punpcklbw %%mm7, %%mm3 \n\t"
  2082. "paddw %%mm1, %%mm5 \n\t"
  2083. "paddw %%mm3, %%mm2 \n\t"
  2084. "paddw %%mm5, %%mm2 \n\t"
  2085. "movq "MANGLE(ff_w1111)", %%mm5 \n\t"
  2086. "psrlw $2, %%mm4 \n\t"
  2087. "psrlw $2, %%mm2 \n\t"
  2088. #endif
  2089. "movq "MANGLE(ff_bgr2VCoeff)", %%mm1 \n\t"
  2090. "movq "MANGLE(ff_bgr2VCoeff)", %%mm3 \n\t"
  2091. "pmaddwd %%mm4, %%mm1 \n\t"
  2092. "pmaddwd %%mm2, %%mm3 \n\t"
  2093. "pmaddwd %%mm6, %%mm4 \n\t"
  2094. "pmaddwd %%mm6, %%mm2 \n\t"
  2095. #ifndef FAST_BGR2YV12
  2096. "psrad $8, %%mm4 \n\t"
  2097. "psrad $8, %%mm1 \n\t"
  2098. "psrad $8, %%mm2 \n\t"
  2099. "psrad $8, %%mm3 \n\t"
  2100. #endif
  2101. "packssdw %%mm2, %%mm4 \n\t"
  2102. "packssdw %%mm3, %%mm1 \n\t"
  2103. "pmaddwd %%mm5, %%mm4 \n\t"
  2104. "pmaddwd %%mm5, %%mm1 \n\t"
  2105. "add $24, %%"REG_d" \n\t"
  2106. "packssdw %%mm1, %%mm4 \n\t" // V3 V2 U3 U2
  2107. "psraw $7, %%mm4 \n\t"
  2108. "movq %%mm0, %%mm1 \n\t"
  2109. "punpckldq %%mm4, %%mm0 \n\t"
  2110. "punpckhdq %%mm4, %%mm1 \n\t"
  2111. "packsswb %%mm1, %%mm0 \n\t"
  2112. "paddb "MANGLE(ff_bgr2UVOffset)", %%mm0 \n\t"
  2113. "movd %%mm0, (%2, %%"REG_a") \n\t"
  2114. "punpckhdq %%mm0, %%mm0 \n\t"
  2115. "movd %%mm0, (%3, %%"REG_a") \n\t"
  2116. "add $4, %%"REG_a" \n\t"
  2117. " js 1b \n\t"
  2118. : : "r" (src+chromWidth*6), "r" (src+srcStride+chromWidth*6), "r" (udst+chromWidth), "r" (vdst+chromWidth), "g" (-chromWidth)
  2119. : "%"REG_a, "%"REG_d
  2120. );
  2121. udst += chromStride;
  2122. vdst += chromStride;
  2123. src += srcStride*2;
  2124. }
  2125. __asm__ volatile(EMMS" \n\t"
  2126. SFENCE" \n\t"
  2127. :::"memory");
  2128. #else
  2129. y=0;
  2130. #endif
  2131. for (; y<height; y+=2) {
  2132. long i;
  2133. for (i=0; i<chromWidth; i++) {
  2134. unsigned int b = src[6*i+0];
  2135. unsigned int g = src[6*i+1];
  2136. unsigned int r = src[6*i+2];
  2137. unsigned int Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2138. unsigned int V = ((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128;
  2139. unsigned int U = ((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128;
  2140. udst[i] = U;
  2141. vdst[i] = V;
  2142. ydst[2*i] = Y;
  2143. b = src[6*i+3];
  2144. g = src[6*i+4];
  2145. r = src[6*i+5];
  2146. Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2147. ydst[2*i+1] = Y;
  2148. }
  2149. ydst += lumStride;
  2150. src += srcStride;
  2151. for (i=0; i<chromWidth; i++) {
  2152. unsigned int b = src[6*i+0];
  2153. unsigned int g = src[6*i+1];
  2154. unsigned int r = src[6*i+2];
  2155. unsigned int Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2156. ydst[2*i] = Y;
  2157. b = src[6*i+3];
  2158. g = src[6*i+4];
  2159. r = src[6*i+5];
  2160. Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2161. ydst[2*i+1] = Y;
  2162. }
  2163. udst += chromStride;
  2164. vdst += chromStride;
  2165. ydst += lumStride;
  2166. src += srcStride;
  2167. }
  2168. }
  2169. static void RENAME(interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dest,
  2170. long width, long height, long src1Stride,
  2171. long src2Stride, long dstStride)
  2172. {
  2173. long h;
  2174. for (h=0; h < height; h++) {
  2175. long w;
  2176. #if COMPILE_TEMPLATE_MMX
  2177. #if COMPILE_TEMPLATE_SSE2
  2178. __asm__(
  2179. "xor %%"REG_a", %%"REG_a" \n\t"
  2180. "1: \n\t"
  2181. PREFETCH" 64(%1, %%"REG_a") \n\t"
  2182. PREFETCH" 64(%2, %%"REG_a") \n\t"
  2183. "movdqa (%1, %%"REG_a"), %%xmm0 \n\t"
  2184. "movdqa (%1, %%"REG_a"), %%xmm1 \n\t"
  2185. "movdqa (%2, %%"REG_a"), %%xmm2 \n\t"
  2186. "punpcklbw %%xmm2, %%xmm0 \n\t"
  2187. "punpckhbw %%xmm2, %%xmm1 \n\t"
  2188. "movntdq %%xmm0, (%0, %%"REG_a", 2) \n\t"
  2189. "movntdq %%xmm1, 16(%0, %%"REG_a", 2) \n\t"
  2190. "add $16, %%"REG_a" \n\t"
  2191. "cmp %3, %%"REG_a" \n\t"
  2192. " jb 1b \n\t"
  2193. ::"r"(dest), "r"(src1), "r"(src2), "r" ((x86_reg)width-15)
  2194. : "memory", "%"REG_a""
  2195. );
  2196. #else
  2197. __asm__(
  2198. "xor %%"REG_a", %%"REG_a" \n\t"
  2199. "1: \n\t"
  2200. PREFETCH" 64(%1, %%"REG_a") \n\t"
  2201. PREFETCH" 64(%2, %%"REG_a") \n\t"
  2202. "movq (%1, %%"REG_a"), %%mm0 \n\t"
  2203. "movq 8(%1, %%"REG_a"), %%mm2 \n\t"
  2204. "movq %%mm0, %%mm1 \n\t"
  2205. "movq %%mm2, %%mm3 \n\t"
  2206. "movq (%2, %%"REG_a"), %%mm4 \n\t"
  2207. "movq 8(%2, %%"REG_a"), %%mm5 \n\t"
  2208. "punpcklbw %%mm4, %%mm0 \n\t"
  2209. "punpckhbw %%mm4, %%mm1 \n\t"
  2210. "punpcklbw %%mm5, %%mm2 \n\t"
  2211. "punpckhbw %%mm5, %%mm3 \n\t"
  2212. MOVNTQ" %%mm0, (%0, %%"REG_a", 2) \n\t"
  2213. MOVNTQ" %%mm1, 8(%0, %%"REG_a", 2) \n\t"
  2214. MOVNTQ" %%mm2, 16(%0, %%"REG_a", 2) \n\t"
  2215. MOVNTQ" %%mm3, 24(%0, %%"REG_a", 2) \n\t"
  2216. "add $16, %%"REG_a" \n\t"
  2217. "cmp %3, %%"REG_a" \n\t"
  2218. " jb 1b \n\t"
  2219. ::"r"(dest), "r"(src1), "r"(src2), "r" ((x86_reg)width-15)
  2220. : "memory", "%"REG_a
  2221. );
  2222. #endif
  2223. for (w= (width&(~15)); w < width; w++) {
  2224. dest[2*w+0] = src1[w];
  2225. dest[2*w+1] = src2[w];
  2226. }
  2227. #else
  2228. for (w=0; w < width; w++) {
  2229. dest[2*w+0] = src1[w];
  2230. dest[2*w+1] = src2[w];
  2231. }
  2232. #endif
  2233. dest += dstStride;
  2234. src1 += src1Stride;
  2235. src2 += src2Stride;
  2236. }
  2237. #if COMPILE_TEMPLATE_MMX
  2238. __asm__(
  2239. EMMS" \n\t"
  2240. SFENCE" \n\t"
  2241. ::: "memory"
  2242. );
  2243. #endif
  2244. }
  2245. static inline void RENAME(vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  2246. uint8_t *dst1, uint8_t *dst2,
  2247. long width, long height,
  2248. long srcStride1, long srcStride2,
  2249. long dstStride1, long dstStride2)
  2250. {
  2251. x86_reg y;
  2252. long x,w,h;
  2253. w=width/2; h=height/2;
  2254. #if COMPILE_TEMPLATE_MMX
  2255. __asm__ volatile(
  2256. PREFETCH" %0 \n\t"
  2257. PREFETCH" %1 \n\t"
  2258. ::"m"(*(src1+srcStride1)),"m"(*(src2+srcStride2)):"memory");
  2259. #endif
  2260. for (y=0;y<h;y++) {
  2261. const uint8_t* s1=src1+srcStride1*(y>>1);
  2262. uint8_t* d=dst1+dstStride1*y;
  2263. x=0;
  2264. #if COMPILE_TEMPLATE_MMX
  2265. for (;x<w-31;x+=32) {
  2266. __asm__ volatile(
  2267. PREFETCH" 32%1 \n\t"
  2268. "movq %1, %%mm0 \n\t"
  2269. "movq 8%1, %%mm2 \n\t"
  2270. "movq 16%1, %%mm4 \n\t"
  2271. "movq 24%1, %%mm6 \n\t"
  2272. "movq %%mm0, %%mm1 \n\t"
  2273. "movq %%mm2, %%mm3 \n\t"
  2274. "movq %%mm4, %%mm5 \n\t"
  2275. "movq %%mm6, %%mm7 \n\t"
  2276. "punpcklbw %%mm0, %%mm0 \n\t"
  2277. "punpckhbw %%mm1, %%mm1 \n\t"
  2278. "punpcklbw %%mm2, %%mm2 \n\t"
  2279. "punpckhbw %%mm3, %%mm3 \n\t"
  2280. "punpcklbw %%mm4, %%mm4 \n\t"
  2281. "punpckhbw %%mm5, %%mm5 \n\t"
  2282. "punpcklbw %%mm6, %%mm6 \n\t"
  2283. "punpckhbw %%mm7, %%mm7 \n\t"
  2284. MOVNTQ" %%mm0, %0 \n\t"
  2285. MOVNTQ" %%mm1, 8%0 \n\t"
  2286. MOVNTQ" %%mm2, 16%0 \n\t"
  2287. MOVNTQ" %%mm3, 24%0 \n\t"
  2288. MOVNTQ" %%mm4, 32%0 \n\t"
  2289. MOVNTQ" %%mm5, 40%0 \n\t"
  2290. MOVNTQ" %%mm6, 48%0 \n\t"
  2291. MOVNTQ" %%mm7, 56%0"
  2292. :"=m"(d[2*x])
  2293. :"m"(s1[x])
  2294. :"memory");
  2295. }
  2296. #endif
  2297. for (;x<w;x++) d[2*x]=d[2*x+1]=s1[x];
  2298. }
  2299. for (y=0;y<h;y++) {
  2300. const uint8_t* s2=src2+srcStride2*(y>>1);
  2301. uint8_t* d=dst2+dstStride2*y;
  2302. x=0;
  2303. #if COMPILE_TEMPLATE_MMX
  2304. for (;x<w-31;x+=32) {
  2305. __asm__ volatile(
  2306. PREFETCH" 32%1 \n\t"
  2307. "movq %1, %%mm0 \n\t"
  2308. "movq 8%1, %%mm2 \n\t"
  2309. "movq 16%1, %%mm4 \n\t"
  2310. "movq 24%1, %%mm6 \n\t"
  2311. "movq %%mm0, %%mm1 \n\t"
  2312. "movq %%mm2, %%mm3 \n\t"
  2313. "movq %%mm4, %%mm5 \n\t"
  2314. "movq %%mm6, %%mm7 \n\t"
  2315. "punpcklbw %%mm0, %%mm0 \n\t"
  2316. "punpckhbw %%mm1, %%mm1 \n\t"
  2317. "punpcklbw %%mm2, %%mm2 \n\t"
  2318. "punpckhbw %%mm3, %%mm3 \n\t"
  2319. "punpcklbw %%mm4, %%mm4 \n\t"
  2320. "punpckhbw %%mm5, %%mm5 \n\t"
  2321. "punpcklbw %%mm6, %%mm6 \n\t"
  2322. "punpckhbw %%mm7, %%mm7 \n\t"
  2323. MOVNTQ" %%mm0, %0 \n\t"
  2324. MOVNTQ" %%mm1, 8%0 \n\t"
  2325. MOVNTQ" %%mm2, 16%0 \n\t"
  2326. MOVNTQ" %%mm3, 24%0 \n\t"
  2327. MOVNTQ" %%mm4, 32%0 \n\t"
  2328. MOVNTQ" %%mm5, 40%0 \n\t"
  2329. MOVNTQ" %%mm6, 48%0 \n\t"
  2330. MOVNTQ" %%mm7, 56%0"
  2331. :"=m"(d[2*x])
  2332. :"m"(s2[x])
  2333. :"memory");
  2334. }
  2335. #endif
  2336. for (;x<w;x++) d[2*x]=d[2*x+1]=s2[x];
  2337. }
  2338. #if COMPILE_TEMPLATE_MMX
  2339. __asm__(
  2340. EMMS" \n\t"
  2341. SFENCE" \n\t"
  2342. ::: "memory"
  2343. );
  2344. #endif
  2345. }
  2346. static inline void RENAME(yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  2347. uint8_t *dst,
  2348. long width, long height,
  2349. long srcStride1, long srcStride2,
  2350. long srcStride3, long dstStride)
  2351. {
  2352. x86_reg x;
  2353. long y,w,h;
  2354. w=width/2; h=height;
  2355. for (y=0;y<h;y++) {
  2356. const uint8_t* yp=src1+srcStride1*y;
  2357. const uint8_t* up=src2+srcStride2*(y>>2);
  2358. const uint8_t* vp=src3+srcStride3*(y>>2);
  2359. uint8_t* d=dst+dstStride*y;
  2360. x=0;
  2361. #if COMPILE_TEMPLATE_MMX
  2362. for (;x<w-7;x+=8) {
  2363. __asm__ volatile(
  2364. PREFETCH" 32(%1, %0) \n\t"
  2365. PREFETCH" 32(%2, %0) \n\t"
  2366. PREFETCH" 32(%3, %0) \n\t"
  2367. "movq (%1, %0, 4), %%mm0 \n\t" /* Y0Y1Y2Y3Y4Y5Y6Y7 */
  2368. "movq (%2, %0), %%mm1 \n\t" /* U0U1U2U3U4U5U6U7 */
  2369. "movq (%3, %0), %%mm2 \n\t" /* V0V1V2V3V4V5V6V7 */
  2370. "movq %%mm0, %%mm3 \n\t" /* Y0Y1Y2Y3Y4Y5Y6Y7 */
  2371. "movq %%mm1, %%mm4 \n\t" /* U0U1U2U3U4U5U6U7 */
  2372. "movq %%mm2, %%mm5 \n\t" /* V0V1V2V3V4V5V6V7 */
  2373. "punpcklbw %%mm1, %%mm1 \n\t" /* U0U0 U1U1 U2U2 U3U3 */
  2374. "punpcklbw %%mm2, %%mm2 \n\t" /* V0V0 V1V1 V2V2 V3V3 */
  2375. "punpckhbw %%mm4, %%mm4 \n\t" /* U4U4 U5U5 U6U6 U7U7 */
  2376. "punpckhbw %%mm5, %%mm5 \n\t" /* V4V4 V5V5 V6V6 V7V7 */
  2377. "movq %%mm1, %%mm6 \n\t"
  2378. "punpcklbw %%mm2, %%mm1 \n\t" /* U0V0 U0V0 U1V1 U1V1*/
  2379. "punpcklbw %%mm1, %%mm0 \n\t" /* Y0U0 Y1V0 Y2U0 Y3V0*/
  2380. "punpckhbw %%mm1, %%mm3 \n\t" /* Y4U1 Y5V1 Y6U1 Y7V1*/
  2381. MOVNTQ" %%mm0, (%4, %0, 8) \n\t"
  2382. MOVNTQ" %%mm3, 8(%4, %0, 8) \n\t"
  2383. "punpckhbw %%mm2, %%mm6 \n\t" /* U2V2 U2V2 U3V3 U3V3*/
  2384. "movq 8(%1, %0, 4), %%mm0 \n\t"
  2385. "movq %%mm0, %%mm3 \n\t"
  2386. "punpcklbw %%mm6, %%mm0 \n\t" /* Y U2 Y V2 Y U2 Y V2*/
  2387. "punpckhbw %%mm6, %%mm3 \n\t" /* Y U3 Y V3 Y U3 Y V3*/
  2388. MOVNTQ" %%mm0, 16(%4, %0, 8) \n\t"
  2389. MOVNTQ" %%mm3, 24(%4, %0, 8) \n\t"
  2390. "movq %%mm4, %%mm6 \n\t"
  2391. "movq 16(%1, %0, 4), %%mm0 \n\t"
  2392. "movq %%mm0, %%mm3 \n\t"
  2393. "punpcklbw %%mm5, %%mm4 \n\t"
  2394. "punpcklbw %%mm4, %%mm0 \n\t" /* Y U4 Y V4 Y U4 Y V4*/
  2395. "punpckhbw %%mm4, %%mm3 \n\t" /* Y U5 Y V5 Y U5 Y V5*/
  2396. MOVNTQ" %%mm0, 32(%4, %0, 8) \n\t"
  2397. MOVNTQ" %%mm3, 40(%4, %0, 8) \n\t"
  2398. "punpckhbw %%mm5, %%mm6 \n\t"
  2399. "movq 24(%1, %0, 4), %%mm0 \n\t"
  2400. "movq %%mm0, %%mm3 \n\t"
  2401. "punpcklbw %%mm6, %%mm0 \n\t" /* Y U6 Y V6 Y U6 Y V6*/
  2402. "punpckhbw %%mm6, %%mm3 \n\t" /* Y U7 Y V7 Y U7 Y V7*/
  2403. MOVNTQ" %%mm0, 48(%4, %0, 8) \n\t"
  2404. MOVNTQ" %%mm3, 56(%4, %0, 8) \n\t"
  2405. : "+r" (x)
  2406. : "r"(yp), "r" (up), "r"(vp), "r"(d)
  2407. :"memory");
  2408. }
  2409. #endif
  2410. for (; x<w; x++) {
  2411. const long x2 = x<<2;
  2412. d[8*x+0] = yp[x2];
  2413. d[8*x+1] = up[x];
  2414. d[8*x+2] = yp[x2+1];
  2415. d[8*x+3] = vp[x];
  2416. d[8*x+4] = yp[x2+2];
  2417. d[8*x+5] = up[x];
  2418. d[8*x+6] = yp[x2+3];
  2419. d[8*x+7] = vp[x];
  2420. }
  2421. }
  2422. #if COMPILE_TEMPLATE_MMX
  2423. __asm__(
  2424. EMMS" \n\t"
  2425. SFENCE" \n\t"
  2426. ::: "memory"
  2427. );
  2428. #endif
  2429. }
  2430. static void RENAME(extract_even)(const uint8_t *src, uint8_t *dst, x86_reg count)
  2431. {
  2432. dst += count;
  2433. src += 2*count;
  2434. count= - count;
  2435. #if COMPILE_TEMPLATE_MMX
  2436. if(count <= -16) {
  2437. count += 15;
  2438. __asm__ volatile(
  2439. "pcmpeqw %%mm7, %%mm7 \n\t"
  2440. "psrlw $8, %%mm7 \n\t"
  2441. "1: \n\t"
  2442. "movq -30(%1, %0, 2), %%mm0 \n\t"
  2443. "movq -22(%1, %0, 2), %%mm1 \n\t"
  2444. "movq -14(%1, %0, 2), %%mm2 \n\t"
  2445. "movq -6(%1, %0, 2), %%mm3 \n\t"
  2446. "pand %%mm7, %%mm0 \n\t"
  2447. "pand %%mm7, %%mm1 \n\t"
  2448. "pand %%mm7, %%mm2 \n\t"
  2449. "pand %%mm7, %%mm3 \n\t"
  2450. "packuswb %%mm1, %%mm0 \n\t"
  2451. "packuswb %%mm3, %%mm2 \n\t"
  2452. MOVNTQ" %%mm0,-15(%2, %0) \n\t"
  2453. MOVNTQ" %%mm2,- 7(%2, %0) \n\t"
  2454. "add $16, %0 \n\t"
  2455. " js 1b \n\t"
  2456. : "+r"(count)
  2457. : "r"(src), "r"(dst)
  2458. );
  2459. count -= 15;
  2460. }
  2461. #endif
  2462. while(count<0) {
  2463. dst[count]= src[2*count];
  2464. count++;
  2465. }
  2466. }
  2467. static void RENAME(extract_even2)(const uint8_t *src, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2468. {
  2469. dst0+= count;
  2470. dst1+= count;
  2471. src += 4*count;
  2472. count= - count;
  2473. #if COMPILE_TEMPLATE_MMX
  2474. if(count <= -8) {
  2475. count += 7;
  2476. __asm__ volatile(
  2477. "pcmpeqw %%mm7, %%mm7 \n\t"
  2478. "psrlw $8, %%mm7 \n\t"
  2479. "1: \n\t"
  2480. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2481. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2482. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2483. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2484. "pand %%mm7, %%mm0 \n\t"
  2485. "pand %%mm7, %%mm1 \n\t"
  2486. "pand %%mm7, %%mm2 \n\t"
  2487. "pand %%mm7, %%mm3 \n\t"
  2488. "packuswb %%mm1, %%mm0 \n\t"
  2489. "packuswb %%mm3, %%mm2 \n\t"
  2490. "movq %%mm0, %%mm1 \n\t"
  2491. "movq %%mm2, %%mm3 \n\t"
  2492. "psrlw $8, %%mm0 \n\t"
  2493. "psrlw $8, %%mm2 \n\t"
  2494. "pand %%mm7, %%mm1 \n\t"
  2495. "pand %%mm7, %%mm3 \n\t"
  2496. "packuswb %%mm2, %%mm0 \n\t"
  2497. "packuswb %%mm3, %%mm1 \n\t"
  2498. MOVNTQ" %%mm0,- 7(%3, %0) \n\t"
  2499. MOVNTQ" %%mm1,- 7(%2, %0) \n\t"
  2500. "add $8, %0 \n\t"
  2501. " js 1b \n\t"
  2502. : "+r"(count)
  2503. : "r"(src), "r"(dst0), "r"(dst1)
  2504. );
  2505. count -= 7;
  2506. }
  2507. #endif
  2508. while(count<0) {
  2509. dst0[count]= src[4*count+0];
  2510. dst1[count]= src[4*count+2];
  2511. count++;
  2512. }
  2513. }
  2514. static void RENAME(extract_even2avg)(const uint8_t *src0, const uint8_t *src1, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2515. {
  2516. dst0 += count;
  2517. dst1 += count;
  2518. src0 += 4*count;
  2519. src1 += 4*count;
  2520. count= - count;
  2521. #ifdef PAVGB
  2522. if(count <= -8) {
  2523. count += 7;
  2524. __asm__ volatile(
  2525. "pcmpeqw %%mm7, %%mm7 \n\t"
  2526. "psrlw $8, %%mm7 \n\t"
  2527. "1: \n\t"
  2528. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2529. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2530. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2531. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2532. PAVGB" -28(%2, %0, 4), %%mm0 \n\t"
  2533. PAVGB" -20(%2, %0, 4), %%mm1 \n\t"
  2534. PAVGB" -12(%2, %0, 4), %%mm2 \n\t"
  2535. PAVGB" - 4(%2, %0, 4), %%mm3 \n\t"
  2536. "pand %%mm7, %%mm0 \n\t"
  2537. "pand %%mm7, %%mm1 \n\t"
  2538. "pand %%mm7, %%mm2 \n\t"
  2539. "pand %%mm7, %%mm3 \n\t"
  2540. "packuswb %%mm1, %%mm0 \n\t"
  2541. "packuswb %%mm3, %%mm2 \n\t"
  2542. "movq %%mm0, %%mm1 \n\t"
  2543. "movq %%mm2, %%mm3 \n\t"
  2544. "psrlw $8, %%mm0 \n\t"
  2545. "psrlw $8, %%mm2 \n\t"
  2546. "pand %%mm7, %%mm1 \n\t"
  2547. "pand %%mm7, %%mm3 \n\t"
  2548. "packuswb %%mm2, %%mm0 \n\t"
  2549. "packuswb %%mm3, %%mm1 \n\t"
  2550. MOVNTQ" %%mm0,- 7(%4, %0) \n\t"
  2551. MOVNTQ" %%mm1,- 7(%3, %0) \n\t"
  2552. "add $8, %0 \n\t"
  2553. " js 1b \n\t"
  2554. : "+r"(count)
  2555. : "r"(src0), "r"(src1), "r"(dst0), "r"(dst1)
  2556. );
  2557. count -= 7;
  2558. }
  2559. #endif
  2560. while(count<0) {
  2561. dst0[count]= (src0[4*count+0]+src1[4*count+0])>>1;
  2562. dst1[count]= (src0[4*count+2]+src1[4*count+2])>>1;
  2563. count++;
  2564. }
  2565. }
  2566. static void RENAME(extract_odd2)(const uint8_t *src, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2567. {
  2568. dst0+= count;
  2569. dst1+= count;
  2570. src += 4*count;
  2571. count= - count;
  2572. #if COMPILE_TEMPLATE_MMX
  2573. if(count <= -8) {
  2574. count += 7;
  2575. __asm__ volatile(
  2576. "pcmpeqw %%mm7, %%mm7 \n\t"
  2577. "psrlw $8, %%mm7 \n\t"
  2578. "1: \n\t"
  2579. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2580. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2581. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2582. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2583. "psrlw $8, %%mm0 \n\t"
  2584. "psrlw $8, %%mm1 \n\t"
  2585. "psrlw $8, %%mm2 \n\t"
  2586. "psrlw $8, %%mm3 \n\t"
  2587. "packuswb %%mm1, %%mm0 \n\t"
  2588. "packuswb %%mm3, %%mm2 \n\t"
  2589. "movq %%mm0, %%mm1 \n\t"
  2590. "movq %%mm2, %%mm3 \n\t"
  2591. "psrlw $8, %%mm0 \n\t"
  2592. "psrlw $8, %%mm2 \n\t"
  2593. "pand %%mm7, %%mm1 \n\t"
  2594. "pand %%mm7, %%mm3 \n\t"
  2595. "packuswb %%mm2, %%mm0 \n\t"
  2596. "packuswb %%mm3, %%mm1 \n\t"
  2597. MOVNTQ" %%mm0,- 7(%3, %0) \n\t"
  2598. MOVNTQ" %%mm1,- 7(%2, %0) \n\t"
  2599. "add $8, %0 \n\t"
  2600. " js 1b \n\t"
  2601. : "+r"(count)
  2602. : "r"(src), "r"(dst0), "r"(dst1)
  2603. );
  2604. count -= 7;
  2605. }
  2606. #endif
  2607. src++;
  2608. while(count<0) {
  2609. dst0[count]= src[4*count+0];
  2610. dst1[count]= src[4*count+2];
  2611. count++;
  2612. }
  2613. }
  2614. static void RENAME(extract_odd2avg)(const uint8_t *src0, const uint8_t *src1, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2615. {
  2616. dst0 += count;
  2617. dst1 += count;
  2618. src0 += 4*count;
  2619. src1 += 4*count;
  2620. count= - count;
  2621. #ifdef PAVGB
  2622. if(count <= -8) {
  2623. count += 7;
  2624. __asm__ volatile(
  2625. "pcmpeqw %%mm7, %%mm7 \n\t"
  2626. "psrlw $8, %%mm7 \n\t"
  2627. "1: \n\t"
  2628. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2629. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2630. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2631. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2632. PAVGB" -28(%2, %0, 4), %%mm0 \n\t"
  2633. PAVGB" -20(%2, %0, 4), %%mm1 \n\t"
  2634. PAVGB" -12(%2, %0, 4), %%mm2 \n\t"
  2635. PAVGB" - 4(%2, %0, 4), %%mm3 \n\t"
  2636. "psrlw $8, %%mm0 \n\t"
  2637. "psrlw $8, %%mm1 \n\t"
  2638. "psrlw $8, %%mm2 \n\t"
  2639. "psrlw $8, %%mm3 \n\t"
  2640. "packuswb %%mm1, %%mm0 \n\t"
  2641. "packuswb %%mm3, %%mm2 \n\t"
  2642. "movq %%mm0, %%mm1 \n\t"
  2643. "movq %%mm2, %%mm3 \n\t"
  2644. "psrlw $8, %%mm0 \n\t"
  2645. "psrlw $8, %%mm2 \n\t"
  2646. "pand %%mm7, %%mm1 \n\t"
  2647. "pand %%mm7, %%mm3 \n\t"
  2648. "packuswb %%mm2, %%mm0 \n\t"
  2649. "packuswb %%mm3, %%mm1 \n\t"
  2650. MOVNTQ" %%mm0,- 7(%4, %0) \n\t"
  2651. MOVNTQ" %%mm1,- 7(%3, %0) \n\t"
  2652. "add $8, %0 \n\t"
  2653. " js 1b \n\t"
  2654. : "+r"(count)
  2655. : "r"(src0), "r"(src1), "r"(dst0), "r"(dst1)
  2656. );
  2657. count -= 7;
  2658. }
  2659. #endif
  2660. src0++;
  2661. src1++;
  2662. while(count<0) {
  2663. dst0[count]= (src0[4*count+0]+src1[4*count+0])>>1;
  2664. dst1[count]= (src0[4*count+2]+src1[4*count+2])>>1;
  2665. count++;
  2666. }
  2667. }
  2668. static void RENAME(yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2669. long width, long height,
  2670. long lumStride, long chromStride, long srcStride)
  2671. {
  2672. long y;
  2673. const long chromWidth= -((-width)>>1);
  2674. for (y=0; y<height; y++) {
  2675. RENAME(extract_even)(src, ydst, width);
  2676. if(y&1) {
  2677. RENAME(extract_odd2avg)(src-srcStride, src, udst, vdst, chromWidth);
  2678. udst+= chromStride;
  2679. vdst+= chromStride;
  2680. }
  2681. src += srcStride;
  2682. ydst+= lumStride;
  2683. }
  2684. #if COMPILE_TEMPLATE_MMX
  2685. __asm__(
  2686. EMMS" \n\t"
  2687. SFENCE" \n\t"
  2688. ::: "memory"
  2689. );
  2690. #endif
  2691. }
  2692. static void RENAME(yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2693. long width, long height,
  2694. long lumStride, long chromStride, long srcStride)
  2695. {
  2696. long y;
  2697. const long chromWidth= -((-width)>>1);
  2698. for (y=0; y<height; y++) {
  2699. RENAME(extract_even)(src, ydst, width);
  2700. RENAME(extract_odd2)(src, udst, vdst, chromWidth);
  2701. src += srcStride;
  2702. ydst+= lumStride;
  2703. udst+= chromStride;
  2704. vdst+= chromStride;
  2705. }
  2706. #if COMPILE_TEMPLATE_MMX
  2707. __asm__(
  2708. EMMS" \n\t"
  2709. SFENCE" \n\t"
  2710. ::: "memory"
  2711. );
  2712. #endif
  2713. }
  2714. static void RENAME(uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2715. long width, long height,
  2716. long lumStride, long chromStride, long srcStride)
  2717. {
  2718. long y;
  2719. const long chromWidth= -((-width)>>1);
  2720. for (y=0; y<height; y++) {
  2721. RENAME(extract_even)(src+1, ydst, width);
  2722. if(y&1) {
  2723. RENAME(extract_even2avg)(src-srcStride, src, udst, vdst, chromWidth);
  2724. udst+= chromStride;
  2725. vdst+= chromStride;
  2726. }
  2727. src += srcStride;
  2728. ydst+= lumStride;
  2729. }
  2730. #if COMPILE_TEMPLATE_MMX
  2731. __asm__(
  2732. EMMS" \n\t"
  2733. SFENCE" \n\t"
  2734. ::: "memory"
  2735. );
  2736. #endif
  2737. }
  2738. static void RENAME(uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2739. long width, long height,
  2740. long lumStride, long chromStride, long srcStride)
  2741. {
  2742. long y;
  2743. const long chromWidth= -((-width)>>1);
  2744. for (y=0; y<height; y++) {
  2745. RENAME(extract_even)(src+1, ydst, width);
  2746. RENAME(extract_even2)(src, udst, vdst, chromWidth);
  2747. src += srcStride;
  2748. ydst+= lumStride;
  2749. udst+= chromStride;
  2750. vdst+= chromStride;
  2751. }
  2752. #if COMPILE_TEMPLATE_MMX
  2753. __asm__(
  2754. EMMS" \n\t"
  2755. SFENCE" \n\t"
  2756. ::: "memory"
  2757. );
  2758. #endif
  2759. }
  2760. static inline void RENAME(rgb2rgb_init)(void)
  2761. {
  2762. rgb15to16 = RENAME(rgb15to16);
  2763. rgb15tobgr24 = RENAME(rgb15tobgr24);
  2764. rgb15to32 = RENAME(rgb15to32);
  2765. rgb16tobgr24 = RENAME(rgb16tobgr24);
  2766. rgb16to32 = RENAME(rgb16to32);
  2767. rgb16to15 = RENAME(rgb16to15);
  2768. rgb24tobgr16 = RENAME(rgb24tobgr16);
  2769. rgb24tobgr15 = RENAME(rgb24tobgr15);
  2770. rgb24tobgr32 = RENAME(rgb24tobgr32);
  2771. rgb32to16 = RENAME(rgb32to16);
  2772. rgb32to15 = RENAME(rgb32to15);
  2773. rgb32tobgr24 = RENAME(rgb32tobgr24);
  2774. rgb24to15 = RENAME(rgb24to15);
  2775. rgb24to16 = RENAME(rgb24to16);
  2776. rgb24tobgr24 = RENAME(rgb24tobgr24);
  2777. shuffle_bytes_2103 = RENAME(shuffle_bytes_2103);
  2778. rgb32tobgr16 = RENAME(rgb32tobgr16);
  2779. rgb32tobgr15 = RENAME(rgb32tobgr15);
  2780. yv12toyuy2 = RENAME(yv12toyuy2);
  2781. yv12touyvy = RENAME(yv12touyvy);
  2782. yuv422ptoyuy2 = RENAME(yuv422ptoyuy2);
  2783. yuv422ptouyvy = RENAME(yuv422ptouyvy);
  2784. yuy2toyv12 = RENAME(yuy2toyv12);
  2785. planar2x = RENAME(planar2x);
  2786. rgb24toyv12 = RENAME(rgb24toyv12);
  2787. interleaveBytes = RENAME(interleaveBytes);
  2788. vu9_to_vu12 = RENAME(vu9_to_vu12);
  2789. yvu9_to_yuy2 = RENAME(yvu9_to_yuy2);
  2790. uyvytoyuv420 = RENAME(uyvytoyuv420);
  2791. uyvytoyuv422 = RENAME(uyvytoyuv422);
  2792. yuyvtoyuv420 = RENAME(yuyvtoyuv420);
  2793. yuyvtoyuv422 = RENAME(yuyvtoyuv422);
  2794. }