rgb2rgb_template.c 112 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 HAVE_SSE2
  34. #define MMREG_SIZE 16
  35. #else
  36. #define MMREG_SIZE 8
  37. #endif
  38. #if HAVE_AMD3DNOW
  39. #define PREFETCH "prefetch"
  40. #define PAVGB "pavgusb"
  41. #elif HAVE_MMX2
  42. #define PREFETCH "prefetchnta"
  43. #define PAVGB "pavgb"
  44. #else
  45. #define PREFETCH " # nop"
  46. #endif
  47. #if HAVE_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 HAVE_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 HAVE_MMX
  66. const uint8_t *mm_end;
  67. #endif
  68. end = s + src_size;
  69. #if HAVE_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 HAVE_MMX
  158. const uint8_t *mm_end;
  159. #endif
  160. end = s + src_size;
  161. #if HAVE_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 HAVE_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 HAVE_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 HAVE_MMX
  307. const uint8_t *mm_end;
  308. #endif
  309. uint16_t *d = (uint16_t *)dst;
  310. end = s + src_size;
  311. #if HAVE_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. ASMALIGN(4)
  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 HAVE_MMX
  402. const uint8_t *mm_end;
  403. #endif
  404. uint16_t *d = (uint16_t *)dst;
  405. end = s + src_size;
  406. #if HAVE_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 HAVE_MMX
  460. const uint8_t *mm_end;
  461. #endif
  462. uint16_t *d = (uint16_t *)dst;
  463. end = s + src_size;
  464. #if HAVE_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. ASMALIGN(4)
  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 HAVE_MMX
  555. const uint8_t *mm_end;
  556. #endif
  557. uint16_t *d = (uint16_t *)dst;
  558. end = s + src_size;
  559. #if HAVE_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 HAVE_MMX
  613. const uint8_t *mm_end;
  614. #endif
  615. uint16_t *d = (uint16_t *)dst;
  616. end = s + src_size;
  617. #if HAVE_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 HAVE_MMX
  673. const uint8_t *mm_end;
  674. #endif
  675. uint16_t *d = (uint16_t *)dst;
  676. end = s + src_size;
  677. #if HAVE_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 HAVE_MMX
  733. const uint8_t *mm_end;
  734. #endif
  735. uint16_t *d = (uint16_t *)dst;
  736. end = s + src_size;
  737. #if HAVE_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 HAVE_MMX
  793. const uint8_t *mm_end;
  794. #endif
  795. uint16_t *d = (uint16_t *)dst;
  796. end = s + src_size;
  797. #if HAVE_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 HAVE_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 HAVE_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 HAVE_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 HAVE_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 HAVE_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 HAVE_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 HAVE_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 HAVE_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(rgb32tobgr32)(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 HAVE_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. ASMALIGN(4)
  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 HAVE_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 HAVE_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. ASMALIGN(4)
  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 HAVE_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. ASMALIGN(4)
  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 HAVE_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 HAVE_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. ASMALIGN(4)
  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 HAVE_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 HAVE_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. ASMALIGN(4)
  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. ASMALIGN(4)
  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 HAVE_MMX
  1673. __asm__ volatile(EMMS" \n\t"
  1674. SFENCE" \n\t"
  1675. :::"memory");
  1676. #endif
  1677. }
  1678. static inline void RENAME(yvu9toyv12)(const uint8_t *ysrc, const uint8_t *usrc, const uint8_t *vsrc,
  1679. uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  1680. long width, long height, long lumStride, long chromStride)
  1681. {
  1682. /* Y Plane */
  1683. memcpy(ydst, ysrc, width*height);
  1684. /* XXX: implement upscaling for U,V */
  1685. }
  1686. static inline void RENAME(planar2x)(const uint8_t *src, uint8_t *dst, long srcWidth, long srcHeight, long srcStride, long dstStride)
  1687. {
  1688. long x,y;
  1689. dst[0]= src[0];
  1690. // first line
  1691. for (x=0; x<srcWidth-1; x++) {
  1692. dst[2*x+1]= (3*src[x] + src[x+1])>>2;
  1693. dst[2*x+2]= ( src[x] + 3*src[x+1])>>2;
  1694. }
  1695. dst[2*srcWidth-1]= src[srcWidth-1];
  1696. dst+= dstStride;
  1697. for (y=1; y<srcHeight; y++) {
  1698. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1699. const x86_reg mmxSize= srcWidth&~15;
  1700. __asm__ volatile(
  1701. "mov %4, %%"REG_a" \n\t"
  1702. "1: \n\t"
  1703. "movq (%0, %%"REG_a"), %%mm0 \n\t"
  1704. "movq (%1, %%"REG_a"), %%mm1 \n\t"
  1705. "movq 1(%0, %%"REG_a"), %%mm2 \n\t"
  1706. "movq 1(%1, %%"REG_a"), %%mm3 \n\t"
  1707. "movq -1(%0, %%"REG_a"), %%mm4 \n\t"
  1708. "movq -1(%1, %%"REG_a"), %%mm5 \n\t"
  1709. PAVGB" %%mm0, %%mm5 \n\t"
  1710. PAVGB" %%mm0, %%mm3 \n\t"
  1711. PAVGB" %%mm0, %%mm5 \n\t"
  1712. PAVGB" %%mm0, %%mm3 \n\t"
  1713. PAVGB" %%mm1, %%mm4 \n\t"
  1714. PAVGB" %%mm1, %%mm2 \n\t"
  1715. PAVGB" %%mm1, %%mm4 \n\t"
  1716. PAVGB" %%mm1, %%mm2 \n\t"
  1717. "movq %%mm5, %%mm7 \n\t"
  1718. "movq %%mm4, %%mm6 \n\t"
  1719. "punpcklbw %%mm3, %%mm5 \n\t"
  1720. "punpckhbw %%mm3, %%mm7 \n\t"
  1721. "punpcklbw %%mm2, %%mm4 \n\t"
  1722. "punpckhbw %%mm2, %%mm6 \n\t"
  1723. #if 1
  1724. MOVNTQ" %%mm5, (%2, %%"REG_a", 2) \n\t"
  1725. MOVNTQ" %%mm7, 8(%2, %%"REG_a", 2) \n\t"
  1726. MOVNTQ" %%mm4, (%3, %%"REG_a", 2) \n\t"
  1727. MOVNTQ" %%mm6, 8(%3, %%"REG_a", 2) \n\t"
  1728. #else
  1729. "movq %%mm5, (%2, %%"REG_a", 2) \n\t"
  1730. "movq %%mm7, 8(%2, %%"REG_a", 2) \n\t"
  1731. "movq %%mm4, (%3, %%"REG_a", 2) \n\t"
  1732. "movq %%mm6, 8(%3, %%"REG_a", 2) \n\t"
  1733. #endif
  1734. "add $8, %%"REG_a" \n\t"
  1735. " js 1b \n\t"
  1736. :: "r" (src + mmxSize ), "r" (src + srcStride + mmxSize ),
  1737. "r" (dst + mmxSize*2), "r" (dst + dstStride + mmxSize*2),
  1738. "g" (-mmxSize)
  1739. : "%"REG_a
  1740. );
  1741. #else
  1742. const x86_reg mmxSize=1;
  1743. #endif
  1744. dst[0 ]= (3*src[0] + src[srcStride])>>2;
  1745. dst[dstStride]= ( src[0] + 3*src[srcStride])>>2;
  1746. for (x=mmxSize-1; x<srcWidth-1; x++) {
  1747. dst[2*x +1]= (3*src[x+0] + src[x+srcStride+1])>>2;
  1748. dst[2*x+dstStride+2]= ( src[x+0] + 3*src[x+srcStride+1])>>2;
  1749. dst[2*x+dstStride+1]= ( src[x+1] + 3*src[x+srcStride ])>>2;
  1750. dst[2*x +2]= (3*src[x+1] + src[x+srcStride ])>>2;
  1751. }
  1752. dst[srcWidth*2 -1 ]= (3*src[srcWidth-1] + src[srcWidth-1 + srcStride])>>2;
  1753. dst[srcWidth*2 -1 + dstStride]= ( src[srcWidth-1] + 3*src[srcWidth-1 + srcStride])>>2;
  1754. dst+=dstStride*2;
  1755. src+=srcStride;
  1756. }
  1757. // last line
  1758. #if 1
  1759. dst[0]= src[0];
  1760. for (x=0; x<srcWidth-1; x++) {
  1761. dst[2*x+1]= (3*src[x] + src[x+1])>>2;
  1762. dst[2*x+2]= ( src[x] + 3*src[x+1])>>2;
  1763. }
  1764. dst[2*srcWidth-1]= src[srcWidth-1];
  1765. #else
  1766. for (x=0; x<srcWidth; x++) {
  1767. dst[2*x+0]=
  1768. dst[2*x+1]= src[x];
  1769. }
  1770. #endif
  1771. #if HAVE_MMX
  1772. __asm__ volatile(EMMS" \n\t"
  1773. SFENCE" \n\t"
  1774. :::"memory");
  1775. #endif
  1776. }
  1777. /**
  1778. * Height should be a multiple of 2 and width should be a multiple of 16.
  1779. * (If this is a problem for anyone then tell me, and I will fix it.)
  1780. * Chrominance data is only taken from every second line, others are ignored.
  1781. * FIXME: Write HQ version.
  1782. */
  1783. static inline void RENAME(uyvytoyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  1784. long width, long height,
  1785. long lumStride, long chromStride, long srcStride)
  1786. {
  1787. long y;
  1788. const x86_reg chromWidth= width>>1;
  1789. for (y=0; y<height; y+=2) {
  1790. #if HAVE_MMX
  1791. __asm__ volatile(
  1792. "xor %%"REG_a", %%"REG_a" \n\t"
  1793. "pcmpeqw %%mm7, %%mm7 \n\t"
  1794. "psrlw $8, %%mm7 \n\t" // FF,00,FF,00...
  1795. ASMALIGN(4)
  1796. "1: \n\t"
  1797. PREFETCH" 64(%0, %%"REG_a", 4) \n\t"
  1798. "movq (%0, %%"REG_a", 4), %%mm0 \n\t" // UYVY UYVY(0)
  1799. "movq 8(%0, %%"REG_a", 4), %%mm1 \n\t" // UYVY UYVY(4)
  1800. "movq %%mm0, %%mm2 \n\t" // UYVY UYVY(0)
  1801. "movq %%mm1, %%mm3 \n\t" // UYVY UYVY(4)
  1802. "pand %%mm7, %%mm0 \n\t" // U0V0 U0V0(0)
  1803. "pand %%mm7, %%mm1 \n\t" // U0V0 U0V0(4)
  1804. "psrlw $8, %%mm2 \n\t" // Y0Y0 Y0Y0(0)
  1805. "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(4)
  1806. "packuswb %%mm1, %%mm0 \n\t" // UVUV UVUV(0)
  1807. "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(0)
  1808. MOVNTQ" %%mm2, (%1, %%"REG_a", 2) \n\t"
  1809. "movq 16(%0, %%"REG_a", 4), %%mm1 \n\t" // UYVY UYVY(8)
  1810. "movq 24(%0, %%"REG_a", 4), %%mm2 \n\t" // UYVY UYVY(12)
  1811. "movq %%mm1, %%mm3 \n\t" // UYVY UYVY(8)
  1812. "movq %%mm2, %%mm4 \n\t" // UYVY UYVY(12)
  1813. "pand %%mm7, %%mm1 \n\t" // U0V0 U0V0(8)
  1814. "pand %%mm7, %%mm2 \n\t" // U0V0 U0V0(12)
  1815. "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(8)
  1816. "psrlw $8, %%mm4 \n\t" // Y0Y0 Y0Y0(12)
  1817. "packuswb %%mm2, %%mm1 \n\t" // UVUV UVUV(8)
  1818. "packuswb %%mm4, %%mm3 \n\t" // YYYY YYYY(8)
  1819. MOVNTQ" %%mm3, 8(%1, %%"REG_a", 2) \n\t"
  1820. "movq %%mm0, %%mm2 \n\t" // UVUV UVUV(0)
  1821. "movq %%mm1, %%mm3 \n\t" // UVUV UVUV(8)
  1822. "psrlw $8, %%mm0 \n\t" // V0V0 V0V0(0)
  1823. "psrlw $8, %%mm1 \n\t" // V0V0 V0V0(8)
  1824. "pand %%mm7, %%mm2 \n\t" // U0U0 U0U0(0)
  1825. "pand %%mm7, %%mm3 \n\t" // U0U0 U0U0(8)
  1826. "packuswb %%mm1, %%mm0 \n\t" // VVVV VVVV(0)
  1827. "packuswb %%mm3, %%mm2 \n\t" // UUUU UUUU(0)
  1828. MOVNTQ" %%mm0, (%3, %%"REG_a") \n\t"
  1829. MOVNTQ" %%mm2, (%2, %%"REG_a") \n\t"
  1830. "add $8, %%"REG_a" \n\t"
  1831. "cmp %4, %%"REG_a" \n\t"
  1832. " jb 1b \n\t"
  1833. ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
  1834. : "memory", "%"REG_a
  1835. );
  1836. ydst += lumStride;
  1837. src += srcStride;
  1838. __asm__ volatile(
  1839. "xor %%"REG_a", %%"REG_a" \n\t"
  1840. ASMALIGN(4)
  1841. "1: \n\t"
  1842. PREFETCH" 64(%0, %%"REG_a", 4) \n\t"
  1843. "movq (%0, %%"REG_a", 4), %%mm0 \n\t" // YUYV YUYV(0)
  1844. "movq 8(%0, %%"REG_a", 4), %%mm1 \n\t" // YUYV YUYV(4)
  1845. "movq 16(%0, %%"REG_a", 4), %%mm2 \n\t" // YUYV YUYV(8)
  1846. "movq 24(%0, %%"REG_a", 4), %%mm3 \n\t" // YUYV YUYV(12)
  1847. "psrlw $8, %%mm0 \n\t" // Y0Y0 Y0Y0(0)
  1848. "psrlw $8, %%mm1 \n\t" // Y0Y0 Y0Y0(4)
  1849. "psrlw $8, %%mm2 \n\t" // Y0Y0 Y0Y0(8)
  1850. "psrlw $8, %%mm3 \n\t" // Y0Y0 Y0Y0(12)
  1851. "packuswb %%mm1, %%mm0 \n\t" // YYYY YYYY(0)
  1852. "packuswb %%mm3, %%mm2 \n\t" // YYYY YYYY(8)
  1853. MOVNTQ" %%mm0, (%1, %%"REG_a", 2) \n\t"
  1854. MOVNTQ" %%mm2, 8(%1, %%"REG_a", 2) \n\t"
  1855. "add $8, %%"REG_a" \n\t"
  1856. "cmp %4, %%"REG_a" \n\t"
  1857. " jb 1b \n\t"
  1858. ::"r"(src), "r"(ydst), "r"(udst), "r"(vdst), "g" (chromWidth)
  1859. : "memory", "%"REG_a
  1860. );
  1861. #else
  1862. long i;
  1863. for (i=0; i<chromWidth; i++) {
  1864. udst[i] = src[4*i+0];
  1865. ydst[2*i+0] = src[4*i+1];
  1866. vdst[i] = src[4*i+2];
  1867. ydst[2*i+1] = src[4*i+3];
  1868. }
  1869. ydst += lumStride;
  1870. src += srcStride;
  1871. for (i=0; i<chromWidth; i++) {
  1872. ydst[2*i+0] = src[4*i+1];
  1873. ydst[2*i+1] = src[4*i+3];
  1874. }
  1875. #endif
  1876. udst += chromStride;
  1877. vdst += chromStride;
  1878. ydst += lumStride;
  1879. src += srcStride;
  1880. }
  1881. #if HAVE_MMX
  1882. __asm__ volatile(EMMS" \n\t"
  1883. SFENCE" \n\t"
  1884. :::"memory");
  1885. #endif
  1886. }
  1887. /**
  1888. * Height should be a multiple of 2 and width should be a multiple of 2.
  1889. * (If this is a problem for anyone then tell me, and I will fix it.)
  1890. * Chrominance data is only taken from every second line,
  1891. * others are ignored in the C version.
  1892. * FIXME: Write HQ version.
  1893. */
  1894. static inline void RENAME(rgb24toyv12)(const uint8_t *src, uint8_t *ydst, uint8_t *udst, uint8_t *vdst,
  1895. long width, long height,
  1896. long lumStride, long chromStride, long srcStride)
  1897. {
  1898. long y;
  1899. const x86_reg chromWidth= width>>1;
  1900. #if HAVE_MMX
  1901. for (y=0; y<height-2; y+=2) {
  1902. long i;
  1903. for (i=0; i<2; i++) {
  1904. __asm__ volatile(
  1905. "mov %2, %%"REG_a" \n\t"
  1906. "movq "MANGLE(ff_bgr2YCoeff)", %%mm6 \n\t"
  1907. "movq "MANGLE(ff_w1111)", %%mm5 \n\t"
  1908. "pxor %%mm7, %%mm7 \n\t"
  1909. "lea (%%"REG_a", %%"REG_a", 2), %%"REG_d" \n\t"
  1910. ASMALIGN(4)
  1911. "1: \n\t"
  1912. PREFETCH" 64(%0, %%"REG_d") \n\t"
  1913. "movd (%0, %%"REG_d"), %%mm0 \n\t"
  1914. "movd 3(%0, %%"REG_d"), %%mm1 \n\t"
  1915. "punpcklbw %%mm7, %%mm0 \n\t"
  1916. "punpcklbw %%mm7, %%mm1 \n\t"
  1917. "movd 6(%0, %%"REG_d"), %%mm2 \n\t"
  1918. "movd 9(%0, %%"REG_d"), %%mm3 \n\t"
  1919. "punpcklbw %%mm7, %%mm2 \n\t"
  1920. "punpcklbw %%mm7, %%mm3 \n\t"
  1921. "pmaddwd %%mm6, %%mm0 \n\t"
  1922. "pmaddwd %%mm6, %%mm1 \n\t"
  1923. "pmaddwd %%mm6, %%mm2 \n\t"
  1924. "pmaddwd %%mm6, %%mm3 \n\t"
  1925. #ifndef FAST_BGR2YV12
  1926. "psrad $8, %%mm0 \n\t"
  1927. "psrad $8, %%mm1 \n\t"
  1928. "psrad $8, %%mm2 \n\t"
  1929. "psrad $8, %%mm3 \n\t"
  1930. #endif
  1931. "packssdw %%mm1, %%mm0 \n\t"
  1932. "packssdw %%mm3, %%mm2 \n\t"
  1933. "pmaddwd %%mm5, %%mm0 \n\t"
  1934. "pmaddwd %%mm5, %%mm2 \n\t"
  1935. "packssdw %%mm2, %%mm0 \n\t"
  1936. "psraw $7, %%mm0 \n\t"
  1937. "movd 12(%0, %%"REG_d"), %%mm4 \n\t"
  1938. "movd 15(%0, %%"REG_d"), %%mm1 \n\t"
  1939. "punpcklbw %%mm7, %%mm4 \n\t"
  1940. "punpcklbw %%mm7, %%mm1 \n\t"
  1941. "movd 18(%0, %%"REG_d"), %%mm2 \n\t"
  1942. "movd 21(%0, %%"REG_d"), %%mm3 \n\t"
  1943. "punpcklbw %%mm7, %%mm2 \n\t"
  1944. "punpcklbw %%mm7, %%mm3 \n\t"
  1945. "pmaddwd %%mm6, %%mm4 \n\t"
  1946. "pmaddwd %%mm6, %%mm1 \n\t"
  1947. "pmaddwd %%mm6, %%mm2 \n\t"
  1948. "pmaddwd %%mm6, %%mm3 \n\t"
  1949. #ifndef FAST_BGR2YV12
  1950. "psrad $8, %%mm4 \n\t"
  1951. "psrad $8, %%mm1 \n\t"
  1952. "psrad $8, %%mm2 \n\t"
  1953. "psrad $8, %%mm3 \n\t"
  1954. #endif
  1955. "packssdw %%mm1, %%mm4 \n\t"
  1956. "packssdw %%mm3, %%mm2 \n\t"
  1957. "pmaddwd %%mm5, %%mm4 \n\t"
  1958. "pmaddwd %%mm5, %%mm2 \n\t"
  1959. "add $24, %%"REG_d" \n\t"
  1960. "packssdw %%mm2, %%mm4 \n\t"
  1961. "psraw $7, %%mm4 \n\t"
  1962. "packuswb %%mm4, %%mm0 \n\t"
  1963. "paddusb "MANGLE(ff_bgr2YOffset)", %%mm0 \n\t"
  1964. MOVNTQ" %%mm0, (%1, %%"REG_a") \n\t"
  1965. "add $8, %%"REG_a" \n\t"
  1966. " js 1b \n\t"
  1967. : : "r" (src+width*3), "r" (ydst+width), "g" ((x86_reg)-width)
  1968. : "%"REG_a, "%"REG_d
  1969. );
  1970. ydst += lumStride;
  1971. src += srcStride;
  1972. }
  1973. src -= srcStride*2;
  1974. __asm__ volatile(
  1975. "mov %4, %%"REG_a" \n\t"
  1976. "movq "MANGLE(ff_w1111)", %%mm5 \n\t"
  1977. "movq "MANGLE(ff_bgr2UCoeff)", %%mm6 \n\t"
  1978. "pxor %%mm7, %%mm7 \n\t"
  1979. "lea (%%"REG_a", %%"REG_a", 2), %%"REG_d" \n\t"
  1980. "add %%"REG_d", %%"REG_d" \n\t"
  1981. ASMALIGN(4)
  1982. "1: \n\t"
  1983. PREFETCH" 64(%0, %%"REG_d") \n\t"
  1984. PREFETCH" 64(%1, %%"REG_d") \n\t"
  1985. #if HAVE_MMX2 || HAVE_AMD3DNOW
  1986. "movq (%0, %%"REG_d"), %%mm0 \n\t"
  1987. "movq (%1, %%"REG_d"), %%mm1 \n\t"
  1988. "movq 6(%0, %%"REG_d"), %%mm2 \n\t"
  1989. "movq 6(%1, %%"REG_d"), %%mm3 \n\t"
  1990. PAVGB" %%mm1, %%mm0 \n\t"
  1991. PAVGB" %%mm3, %%mm2 \n\t"
  1992. "movq %%mm0, %%mm1 \n\t"
  1993. "movq %%mm2, %%mm3 \n\t"
  1994. "psrlq $24, %%mm0 \n\t"
  1995. "psrlq $24, %%mm2 \n\t"
  1996. PAVGB" %%mm1, %%mm0 \n\t"
  1997. PAVGB" %%mm3, %%mm2 \n\t"
  1998. "punpcklbw %%mm7, %%mm0 \n\t"
  1999. "punpcklbw %%mm7, %%mm2 \n\t"
  2000. #else
  2001. "movd (%0, %%"REG_d"), %%mm0 \n\t"
  2002. "movd (%1, %%"REG_d"), %%mm1 \n\t"
  2003. "movd 3(%0, %%"REG_d"), %%mm2 \n\t"
  2004. "movd 3(%1, %%"REG_d"), %%mm3 \n\t"
  2005. "punpcklbw %%mm7, %%mm0 \n\t"
  2006. "punpcklbw %%mm7, %%mm1 \n\t"
  2007. "punpcklbw %%mm7, %%mm2 \n\t"
  2008. "punpcklbw %%mm7, %%mm3 \n\t"
  2009. "paddw %%mm1, %%mm0 \n\t"
  2010. "paddw %%mm3, %%mm2 \n\t"
  2011. "paddw %%mm2, %%mm0 \n\t"
  2012. "movd 6(%0, %%"REG_d"), %%mm4 \n\t"
  2013. "movd 6(%1, %%"REG_d"), %%mm1 \n\t"
  2014. "movd 9(%0, %%"REG_d"), %%mm2 \n\t"
  2015. "movd 9(%1, %%"REG_d"), %%mm3 \n\t"
  2016. "punpcklbw %%mm7, %%mm4 \n\t"
  2017. "punpcklbw %%mm7, %%mm1 \n\t"
  2018. "punpcklbw %%mm7, %%mm2 \n\t"
  2019. "punpcklbw %%mm7, %%mm3 \n\t"
  2020. "paddw %%mm1, %%mm4 \n\t"
  2021. "paddw %%mm3, %%mm2 \n\t"
  2022. "paddw %%mm4, %%mm2 \n\t"
  2023. "psrlw $2, %%mm0 \n\t"
  2024. "psrlw $2, %%mm2 \n\t"
  2025. #endif
  2026. "movq "MANGLE(ff_bgr2VCoeff)", %%mm1 \n\t"
  2027. "movq "MANGLE(ff_bgr2VCoeff)", %%mm3 \n\t"
  2028. "pmaddwd %%mm0, %%mm1 \n\t"
  2029. "pmaddwd %%mm2, %%mm3 \n\t"
  2030. "pmaddwd %%mm6, %%mm0 \n\t"
  2031. "pmaddwd %%mm6, %%mm2 \n\t"
  2032. #ifndef FAST_BGR2YV12
  2033. "psrad $8, %%mm0 \n\t"
  2034. "psrad $8, %%mm1 \n\t"
  2035. "psrad $8, %%mm2 \n\t"
  2036. "psrad $8, %%mm3 \n\t"
  2037. #endif
  2038. "packssdw %%mm2, %%mm0 \n\t"
  2039. "packssdw %%mm3, %%mm1 \n\t"
  2040. "pmaddwd %%mm5, %%mm0 \n\t"
  2041. "pmaddwd %%mm5, %%mm1 \n\t"
  2042. "packssdw %%mm1, %%mm0 \n\t" // V1 V0 U1 U0
  2043. "psraw $7, %%mm0 \n\t"
  2044. #if HAVE_MMX2 || HAVE_AMD3DNOW
  2045. "movq 12(%0, %%"REG_d"), %%mm4 \n\t"
  2046. "movq 12(%1, %%"REG_d"), %%mm1 \n\t"
  2047. "movq 18(%0, %%"REG_d"), %%mm2 \n\t"
  2048. "movq 18(%1, %%"REG_d"), %%mm3 \n\t"
  2049. PAVGB" %%mm1, %%mm4 \n\t"
  2050. PAVGB" %%mm3, %%mm2 \n\t"
  2051. "movq %%mm4, %%mm1 \n\t"
  2052. "movq %%mm2, %%mm3 \n\t"
  2053. "psrlq $24, %%mm4 \n\t"
  2054. "psrlq $24, %%mm2 \n\t"
  2055. PAVGB" %%mm1, %%mm4 \n\t"
  2056. PAVGB" %%mm3, %%mm2 \n\t"
  2057. "punpcklbw %%mm7, %%mm4 \n\t"
  2058. "punpcklbw %%mm7, %%mm2 \n\t"
  2059. #else
  2060. "movd 12(%0, %%"REG_d"), %%mm4 \n\t"
  2061. "movd 12(%1, %%"REG_d"), %%mm1 \n\t"
  2062. "movd 15(%0, %%"REG_d"), %%mm2 \n\t"
  2063. "movd 15(%1, %%"REG_d"), %%mm3 \n\t"
  2064. "punpcklbw %%mm7, %%mm4 \n\t"
  2065. "punpcklbw %%mm7, %%mm1 \n\t"
  2066. "punpcklbw %%mm7, %%mm2 \n\t"
  2067. "punpcklbw %%mm7, %%mm3 \n\t"
  2068. "paddw %%mm1, %%mm4 \n\t"
  2069. "paddw %%mm3, %%mm2 \n\t"
  2070. "paddw %%mm2, %%mm4 \n\t"
  2071. "movd 18(%0, %%"REG_d"), %%mm5 \n\t"
  2072. "movd 18(%1, %%"REG_d"), %%mm1 \n\t"
  2073. "movd 21(%0, %%"REG_d"), %%mm2 \n\t"
  2074. "movd 21(%1, %%"REG_d"), %%mm3 \n\t"
  2075. "punpcklbw %%mm7, %%mm5 \n\t"
  2076. "punpcklbw %%mm7, %%mm1 \n\t"
  2077. "punpcklbw %%mm7, %%mm2 \n\t"
  2078. "punpcklbw %%mm7, %%mm3 \n\t"
  2079. "paddw %%mm1, %%mm5 \n\t"
  2080. "paddw %%mm3, %%mm2 \n\t"
  2081. "paddw %%mm5, %%mm2 \n\t"
  2082. "movq "MANGLE(ff_w1111)", %%mm5 \n\t"
  2083. "psrlw $2, %%mm4 \n\t"
  2084. "psrlw $2, %%mm2 \n\t"
  2085. #endif
  2086. "movq "MANGLE(ff_bgr2VCoeff)", %%mm1 \n\t"
  2087. "movq "MANGLE(ff_bgr2VCoeff)", %%mm3 \n\t"
  2088. "pmaddwd %%mm4, %%mm1 \n\t"
  2089. "pmaddwd %%mm2, %%mm3 \n\t"
  2090. "pmaddwd %%mm6, %%mm4 \n\t"
  2091. "pmaddwd %%mm6, %%mm2 \n\t"
  2092. #ifndef FAST_BGR2YV12
  2093. "psrad $8, %%mm4 \n\t"
  2094. "psrad $8, %%mm1 \n\t"
  2095. "psrad $8, %%mm2 \n\t"
  2096. "psrad $8, %%mm3 \n\t"
  2097. #endif
  2098. "packssdw %%mm2, %%mm4 \n\t"
  2099. "packssdw %%mm3, %%mm1 \n\t"
  2100. "pmaddwd %%mm5, %%mm4 \n\t"
  2101. "pmaddwd %%mm5, %%mm1 \n\t"
  2102. "add $24, %%"REG_d" \n\t"
  2103. "packssdw %%mm1, %%mm4 \n\t" // V3 V2 U3 U2
  2104. "psraw $7, %%mm4 \n\t"
  2105. "movq %%mm0, %%mm1 \n\t"
  2106. "punpckldq %%mm4, %%mm0 \n\t"
  2107. "punpckhdq %%mm4, %%mm1 \n\t"
  2108. "packsswb %%mm1, %%mm0 \n\t"
  2109. "paddb "MANGLE(ff_bgr2UVOffset)", %%mm0 \n\t"
  2110. "movd %%mm0, (%2, %%"REG_a") \n\t"
  2111. "punpckhdq %%mm0, %%mm0 \n\t"
  2112. "movd %%mm0, (%3, %%"REG_a") \n\t"
  2113. "add $4, %%"REG_a" \n\t"
  2114. " js 1b \n\t"
  2115. : : "r" (src+chromWidth*6), "r" (src+srcStride+chromWidth*6), "r" (udst+chromWidth), "r" (vdst+chromWidth), "g" (-chromWidth)
  2116. : "%"REG_a, "%"REG_d
  2117. );
  2118. udst += chromStride;
  2119. vdst += chromStride;
  2120. src += srcStride*2;
  2121. }
  2122. __asm__ volatile(EMMS" \n\t"
  2123. SFENCE" \n\t"
  2124. :::"memory");
  2125. #else
  2126. y=0;
  2127. #endif
  2128. for (; y<height; y+=2) {
  2129. long i;
  2130. for (i=0; i<chromWidth; i++) {
  2131. unsigned int b = src[6*i+0];
  2132. unsigned int g = src[6*i+1];
  2133. unsigned int r = src[6*i+2];
  2134. unsigned int Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2135. unsigned int V = ((RV*r + GV*g + BV*b)>>RGB2YUV_SHIFT) + 128;
  2136. unsigned int U = ((RU*r + GU*g + BU*b)>>RGB2YUV_SHIFT) + 128;
  2137. udst[i] = U;
  2138. vdst[i] = V;
  2139. ydst[2*i] = Y;
  2140. b = src[6*i+3];
  2141. g = src[6*i+4];
  2142. r = src[6*i+5];
  2143. Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2144. ydst[2*i+1] = Y;
  2145. }
  2146. ydst += lumStride;
  2147. src += srcStride;
  2148. for (i=0; i<chromWidth; i++) {
  2149. unsigned int b = src[6*i+0];
  2150. unsigned int g = src[6*i+1];
  2151. unsigned int r = src[6*i+2];
  2152. unsigned int Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2153. ydst[2*i] = Y;
  2154. b = src[6*i+3];
  2155. g = src[6*i+4];
  2156. r = src[6*i+5];
  2157. Y = ((RY*r + GY*g + BY*b)>>RGB2YUV_SHIFT) + 16;
  2158. ydst[2*i+1] = Y;
  2159. }
  2160. udst += chromStride;
  2161. vdst += chromStride;
  2162. ydst += lumStride;
  2163. src += srcStride;
  2164. }
  2165. }
  2166. static void RENAME(interleaveBytes)(const uint8_t *src1, const uint8_t *src2, uint8_t *dest,
  2167. long width, long height, long src1Stride,
  2168. long src2Stride, long dstStride)
  2169. {
  2170. long h;
  2171. for (h=0; h < height; h++) {
  2172. long w;
  2173. #if HAVE_MMX
  2174. #if HAVE_SSE2
  2175. __asm__(
  2176. "xor %%"REG_a", %%"REG_a" \n\t"
  2177. "1: \n\t"
  2178. PREFETCH" 64(%1, %%"REG_a") \n\t"
  2179. PREFETCH" 64(%2, %%"REG_a") \n\t"
  2180. "movdqa (%1, %%"REG_a"), %%xmm0 \n\t"
  2181. "movdqa (%1, %%"REG_a"), %%xmm1 \n\t"
  2182. "movdqa (%2, %%"REG_a"), %%xmm2 \n\t"
  2183. "punpcklbw %%xmm2, %%xmm0 \n\t"
  2184. "punpckhbw %%xmm2, %%xmm1 \n\t"
  2185. "movntdq %%xmm0, (%0, %%"REG_a", 2) \n\t"
  2186. "movntdq %%xmm1, 16(%0, %%"REG_a", 2) \n\t"
  2187. "add $16, %%"REG_a" \n\t"
  2188. "cmp %3, %%"REG_a" \n\t"
  2189. " jb 1b \n\t"
  2190. ::"r"(dest), "r"(src1), "r"(src2), "r" ((x86_reg)width-15)
  2191. : "memory", "%"REG_a""
  2192. );
  2193. #else
  2194. __asm__(
  2195. "xor %%"REG_a", %%"REG_a" \n\t"
  2196. "1: \n\t"
  2197. PREFETCH" 64(%1, %%"REG_a") \n\t"
  2198. PREFETCH" 64(%2, %%"REG_a") \n\t"
  2199. "movq (%1, %%"REG_a"), %%mm0 \n\t"
  2200. "movq 8(%1, %%"REG_a"), %%mm2 \n\t"
  2201. "movq %%mm0, %%mm1 \n\t"
  2202. "movq %%mm2, %%mm3 \n\t"
  2203. "movq (%2, %%"REG_a"), %%mm4 \n\t"
  2204. "movq 8(%2, %%"REG_a"), %%mm5 \n\t"
  2205. "punpcklbw %%mm4, %%mm0 \n\t"
  2206. "punpckhbw %%mm4, %%mm1 \n\t"
  2207. "punpcklbw %%mm5, %%mm2 \n\t"
  2208. "punpckhbw %%mm5, %%mm3 \n\t"
  2209. MOVNTQ" %%mm0, (%0, %%"REG_a", 2) \n\t"
  2210. MOVNTQ" %%mm1, 8(%0, %%"REG_a", 2) \n\t"
  2211. MOVNTQ" %%mm2, 16(%0, %%"REG_a", 2) \n\t"
  2212. MOVNTQ" %%mm3, 24(%0, %%"REG_a", 2) \n\t"
  2213. "add $16, %%"REG_a" \n\t"
  2214. "cmp %3, %%"REG_a" \n\t"
  2215. " jb 1b \n\t"
  2216. ::"r"(dest), "r"(src1), "r"(src2), "r" ((x86_reg)width-15)
  2217. : "memory", "%"REG_a
  2218. );
  2219. #endif
  2220. for (w= (width&(~15)); w < width; w++) {
  2221. dest[2*w+0] = src1[w];
  2222. dest[2*w+1] = src2[w];
  2223. }
  2224. #else
  2225. for (w=0; w < width; w++) {
  2226. dest[2*w+0] = src1[w];
  2227. dest[2*w+1] = src2[w];
  2228. }
  2229. #endif
  2230. dest += dstStride;
  2231. src1 += src1Stride;
  2232. src2 += src2Stride;
  2233. }
  2234. #if HAVE_MMX
  2235. __asm__(
  2236. EMMS" \n\t"
  2237. SFENCE" \n\t"
  2238. ::: "memory"
  2239. );
  2240. #endif
  2241. }
  2242. static inline void RENAME(vu9_to_vu12)(const uint8_t *src1, const uint8_t *src2,
  2243. uint8_t *dst1, uint8_t *dst2,
  2244. long width, long height,
  2245. long srcStride1, long srcStride2,
  2246. long dstStride1, long dstStride2)
  2247. {
  2248. x86_reg y;
  2249. long x,w,h;
  2250. w=width/2; h=height/2;
  2251. #if HAVE_MMX
  2252. __asm__ volatile(
  2253. PREFETCH" %0 \n\t"
  2254. PREFETCH" %1 \n\t"
  2255. ::"m"(*(src1+srcStride1)),"m"(*(src2+srcStride2)):"memory");
  2256. #endif
  2257. for (y=0;y<h;y++) {
  2258. const uint8_t* s1=src1+srcStride1*(y>>1);
  2259. uint8_t* d=dst1+dstStride1*y;
  2260. x=0;
  2261. #if HAVE_MMX
  2262. for (;x<w-31;x+=32) {
  2263. __asm__ volatile(
  2264. PREFETCH" 32%1 \n\t"
  2265. "movq %1, %%mm0 \n\t"
  2266. "movq 8%1, %%mm2 \n\t"
  2267. "movq 16%1, %%mm4 \n\t"
  2268. "movq 24%1, %%mm6 \n\t"
  2269. "movq %%mm0, %%mm1 \n\t"
  2270. "movq %%mm2, %%mm3 \n\t"
  2271. "movq %%mm4, %%mm5 \n\t"
  2272. "movq %%mm6, %%mm7 \n\t"
  2273. "punpcklbw %%mm0, %%mm0 \n\t"
  2274. "punpckhbw %%mm1, %%mm1 \n\t"
  2275. "punpcklbw %%mm2, %%mm2 \n\t"
  2276. "punpckhbw %%mm3, %%mm3 \n\t"
  2277. "punpcklbw %%mm4, %%mm4 \n\t"
  2278. "punpckhbw %%mm5, %%mm5 \n\t"
  2279. "punpcklbw %%mm6, %%mm6 \n\t"
  2280. "punpckhbw %%mm7, %%mm7 \n\t"
  2281. MOVNTQ" %%mm0, %0 \n\t"
  2282. MOVNTQ" %%mm1, 8%0 \n\t"
  2283. MOVNTQ" %%mm2, 16%0 \n\t"
  2284. MOVNTQ" %%mm3, 24%0 \n\t"
  2285. MOVNTQ" %%mm4, 32%0 \n\t"
  2286. MOVNTQ" %%mm5, 40%0 \n\t"
  2287. MOVNTQ" %%mm6, 48%0 \n\t"
  2288. MOVNTQ" %%mm7, 56%0"
  2289. :"=m"(d[2*x])
  2290. :"m"(s1[x])
  2291. :"memory");
  2292. }
  2293. #endif
  2294. for (;x<w;x++) d[2*x]=d[2*x+1]=s1[x];
  2295. }
  2296. for (y=0;y<h;y++) {
  2297. const uint8_t* s2=src2+srcStride2*(y>>1);
  2298. uint8_t* d=dst2+dstStride2*y;
  2299. x=0;
  2300. #if HAVE_MMX
  2301. for (;x<w-31;x+=32) {
  2302. __asm__ volatile(
  2303. PREFETCH" 32%1 \n\t"
  2304. "movq %1, %%mm0 \n\t"
  2305. "movq 8%1, %%mm2 \n\t"
  2306. "movq 16%1, %%mm4 \n\t"
  2307. "movq 24%1, %%mm6 \n\t"
  2308. "movq %%mm0, %%mm1 \n\t"
  2309. "movq %%mm2, %%mm3 \n\t"
  2310. "movq %%mm4, %%mm5 \n\t"
  2311. "movq %%mm6, %%mm7 \n\t"
  2312. "punpcklbw %%mm0, %%mm0 \n\t"
  2313. "punpckhbw %%mm1, %%mm1 \n\t"
  2314. "punpcklbw %%mm2, %%mm2 \n\t"
  2315. "punpckhbw %%mm3, %%mm3 \n\t"
  2316. "punpcklbw %%mm4, %%mm4 \n\t"
  2317. "punpckhbw %%mm5, %%mm5 \n\t"
  2318. "punpcklbw %%mm6, %%mm6 \n\t"
  2319. "punpckhbw %%mm7, %%mm7 \n\t"
  2320. MOVNTQ" %%mm0, %0 \n\t"
  2321. MOVNTQ" %%mm1, 8%0 \n\t"
  2322. MOVNTQ" %%mm2, 16%0 \n\t"
  2323. MOVNTQ" %%mm3, 24%0 \n\t"
  2324. MOVNTQ" %%mm4, 32%0 \n\t"
  2325. MOVNTQ" %%mm5, 40%0 \n\t"
  2326. MOVNTQ" %%mm6, 48%0 \n\t"
  2327. MOVNTQ" %%mm7, 56%0"
  2328. :"=m"(d[2*x])
  2329. :"m"(s2[x])
  2330. :"memory");
  2331. }
  2332. #endif
  2333. for (;x<w;x++) d[2*x]=d[2*x+1]=s2[x];
  2334. }
  2335. #if HAVE_MMX
  2336. __asm__(
  2337. EMMS" \n\t"
  2338. SFENCE" \n\t"
  2339. ::: "memory"
  2340. );
  2341. #endif
  2342. }
  2343. static inline void RENAME(yvu9_to_yuy2)(const uint8_t *src1, const uint8_t *src2, const uint8_t *src3,
  2344. uint8_t *dst,
  2345. long width, long height,
  2346. long srcStride1, long srcStride2,
  2347. long srcStride3, long dstStride)
  2348. {
  2349. x86_reg x;
  2350. long y,w,h;
  2351. w=width/2; h=height;
  2352. for (y=0;y<h;y++) {
  2353. const uint8_t* yp=src1+srcStride1*y;
  2354. const uint8_t* up=src2+srcStride2*(y>>2);
  2355. const uint8_t* vp=src3+srcStride3*(y>>2);
  2356. uint8_t* d=dst+dstStride*y;
  2357. x=0;
  2358. #if HAVE_MMX
  2359. for (;x<w-7;x+=8) {
  2360. __asm__ volatile(
  2361. PREFETCH" 32(%1, %0) \n\t"
  2362. PREFETCH" 32(%2, %0) \n\t"
  2363. PREFETCH" 32(%3, %0) \n\t"
  2364. "movq (%1, %0, 4), %%mm0 \n\t" /* Y0Y1Y2Y3Y4Y5Y6Y7 */
  2365. "movq (%2, %0), %%mm1 \n\t" /* U0U1U2U3U4U5U6U7 */
  2366. "movq (%3, %0), %%mm2 \n\t" /* V0V1V2V3V4V5V6V7 */
  2367. "movq %%mm0, %%mm3 \n\t" /* Y0Y1Y2Y3Y4Y5Y6Y7 */
  2368. "movq %%mm1, %%mm4 \n\t" /* U0U1U2U3U4U5U6U7 */
  2369. "movq %%mm2, %%mm5 \n\t" /* V0V1V2V3V4V5V6V7 */
  2370. "punpcklbw %%mm1, %%mm1 \n\t" /* U0U0 U1U1 U2U2 U3U3 */
  2371. "punpcklbw %%mm2, %%mm2 \n\t" /* V0V0 V1V1 V2V2 V3V3 */
  2372. "punpckhbw %%mm4, %%mm4 \n\t" /* U4U4 U5U5 U6U6 U7U7 */
  2373. "punpckhbw %%mm5, %%mm5 \n\t" /* V4V4 V5V5 V6V6 V7V7 */
  2374. "movq %%mm1, %%mm6 \n\t"
  2375. "punpcklbw %%mm2, %%mm1 \n\t" /* U0V0 U0V0 U1V1 U1V1*/
  2376. "punpcklbw %%mm1, %%mm0 \n\t" /* Y0U0 Y1V0 Y2U0 Y3V0*/
  2377. "punpckhbw %%mm1, %%mm3 \n\t" /* Y4U1 Y5V1 Y6U1 Y7V1*/
  2378. MOVNTQ" %%mm0, (%4, %0, 8) \n\t"
  2379. MOVNTQ" %%mm3, 8(%4, %0, 8) \n\t"
  2380. "punpckhbw %%mm2, %%mm6 \n\t" /* U2V2 U2V2 U3V3 U3V3*/
  2381. "movq 8(%1, %0, 4), %%mm0 \n\t"
  2382. "movq %%mm0, %%mm3 \n\t"
  2383. "punpcklbw %%mm6, %%mm0 \n\t" /* Y U2 Y V2 Y U2 Y V2*/
  2384. "punpckhbw %%mm6, %%mm3 \n\t" /* Y U3 Y V3 Y U3 Y V3*/
  2385. MOVNTQ" %%mm0, 16(%4, %0, 8) \n\t"
  2386. MOVNTQ" %%mm3, 24(%4, %0, 8) \n\t"
  2387. "movq %%mm4, %%mm6 \n\t"
  2388. "movq 16(%1, %0, 4), %%mm0 \n\t"
  2389. "movq %%mm0, %%mm3 \n\t"
  2390. "punpcklbw %%mm5, %%mm4 \n\t"
  2391. "punpcklbw %%mm4, %%mm0 \n\t" /* Y U4 Y V4 Y U4 Y V4*/
  2392. "punpckhbw %%mm4, %%mm3 \n\t" /* Y U5 Y V5 Y U5 Y V5*/
  2393. MOVNTQ" %%mm0, 32(%4, %0, 8) \n\t"
  2394. MOVNTQ" %%mm3, 40(%4, %0, 8) \n\t"
  2395. "punpckhbw %%mm5, %%mm6 \n\t"
  2396. "movq 24(%1, %0, 4), %%mm0 \n\t"
  2397. "movq %%mm0, %%mm3 \n\t"
  2398. "punpcklbw %%mm6, %%mm0 \n\t" /* Y U6 Y V6 Y U6 Y V6*/
  2399. "punpckhbw %%mm6, %%mm3 \n\t" /* Y U7 Y V7 Y U7 Y V7*/
  2400. MOVNTQ" %%mm0, 48(%4, %0, 8) \n\t"
  2401. MOVNTQ" %%mm3, 56(%4, %0, 8) \n\t"
  2402. : "+r" (x)
  2403. : "r"(yp), "r" (up), "r"(vp), "r"(d)
  2404. :"memory");
  2405. }
  2406. #endif
  2407. for (; x<w; x++) {
  2408. const long x2 = x<<2;
  2409. d[8*x+0] = yp[x2];
  2410. d[8*x+1] = up[x];
  2411. d[8*x+2] = yp[x2+1];
  2412. d[8*x+3] = vp[x];
  2413. d[8*x+4] = yp[x2+2];
  2414. d[8*x+5] = up[x];
  2415. d[8*x+6] = yp[x2+3];
  2416. d[8*x+7] = vp[x];
  2417. }
  2418. }
  2419. #if HAVE_MMX
  2420. __asm__(
  2421. EMMS" \n\t"
  2422. SFENCE" \n\t"
  2423. ::: "memory"
  2424. );
  2425. #endif
  2426. }
  2427. static void RENAME(extract_even)(const uint8_t *src, uint8_t *dst, x86_reg count)
  2428. {
  2429. dst += count;
  2430. src += 2*count;
  2431. count= - count;
  2432. #if HAVE_MMX
  2433. if(count <= -16) {
  2434. count += 15;
  2435. __asm__ volatile(
  2436. "pcmpeqw %%mm7, %%mm7 \n\t"
  2437. "psrlw $8, %%mm7 \n\t"
  2438. "1: \n\t"
  2439. "movq -30(%1, %0, 2), %%mm0 \n\t"
  2440. "movq -22(%1, %0, 2), %%mm1 \n\t"
  2441. "movq -14(%1, %0, 2), %%mm2 \n\t"
  2442. "movq -6(%1, %0, 2), %%mm3 \n\t"
  2443. "pand %%mm7, %%mm0 \n\t"
  2444. "pand %%mm7, %%mm1 \n\t"
  2445. "pand %%mm7, %%mm2 \n\t"
  2446. "pand %%mm7, %%mm3 \n\t"
  2447. "packuswb %%mm1, %%mm0 \n\t"
  2448. "packuswb %%mm3, %%mm2 \n\t"
  2449. MOVNTQ" %%mm0,-15(%2, %0) \n\t"
  2450. MOVNTQ" %%mm2,- 7(%2, %0) \n\t"
  2451. "add $16, %0 \n\t"
  2452. " js 1b \n\t"
  2453. : "+r"(count)
  2454. : "r"(src), "r"(dst)
  2455. );
  2456. count -= 15;
  2457. }
  2458. #endif
  2459. while(count<0) {
  2460. dst[count]= src[2*count];
  2461. count++;
  2462. }
  2463. }
  2464. static void RENAME(extract_even2)(const uint8_t *src, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2465. {
  2466. dst0+= count;
  2467. dst1+= count;
  2468. src += 4*count;
  2469. count= - count;
  2470. #if HAVE_MMX
  2471. if(count <= -8) {
  2472. count += 7;
  2473. __asm__ volatile(
  2474. "pcmpeqw %%mm7, %%mm7 \n\t"
  2475. "psrlw $8, %%mm7 \n\t"
  2476. "1: \n\t"
  2477. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2478. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2479. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2480. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2481. "pand %%mm7, %%mm0 \n\t"
  2482. "pand %%mm7, %%mm1 \n\t"
  2483. "pand %%mm7, %%mm2 \n\t"
  2484. "pand %%mm7, %%mm3 \n\t"
  2485. "packuswb %%mm1, %%mm0 \n\t"
  2486. "packuswb %%mm3, %%mm2 \n\t"
  2487. "movq %%mm0, %%mm1 \n\t"
  2488. "movq %%mm2, %%mm3 \n\t"
  2489. "psrlw $8, %%mm0 \n\t"
  2490. "psrlw $8, %%mm2 \n\t"
  2491. "pand %%mm7, %%mm1 \n\t"
  2492. "pand %%mm7, %%mm3 \n\t"
  2493. "packuswb %%mm2, %%mm0 \n\t"
  2494. "packuswb %%mm3, %%mm1 \n\t"
  2495. MOVNTQ" %%mm0,- 7(%3, %0) \n\t"
  2496. MOVNTQ" %%mm1,- 7(%2, %0) \n\t"
  2497. "add $8, %0 \n\t"
  2498. " js 1b \n\t"
  2499. : "+r"(count)
  2500. : "r"(src), "r"(dst0), "r"(dst1)
  2501. );
  2502. count -= 7;
  2503. }
  2504. #endif
  2505. while(count<0) {
  2506. dst0[count]= src[4*count+0];
  2507. dst1[count]= src[4*count+2];
  2508. count++;
  2509. }
  2510. }
  2511. static void RENAME(extract_even2avg)(const uint8_t *src0, const uint8_t *src1, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2512. {
  2513. dst0 += count;
  2514. dst1 += count;
  2515. src0 += 4*count;
  2516. src1 += 4*count;
  2517. count= - count;
  2518. #ifdef PAVGB
  2519. if(count <= -8) {
  2520. count += 7;
  2521. __asm__ volatile(
  2522. "pcmpeqw %%mm7, %%mm7 \n\t"
  2523. "psrlw $8, %%mm7 \n\t"
  2524. "1: \n\t"
  2525. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2526. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2527. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2528. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2529. PAVGB" -28(%2, %0, 4), %%mm0 \n\t"
  2530. PAVGB" -20(%2, %0, 4), %%mm1 \n\t"
  2531. PAVGB" -12(%2, %0, 4), %%mm2 \n\t"
  2532. PAVGB" - 4(%2, %0, 4), %%mm3 \n\t"
  2533. "pand %%mm7, %%mm0 \n\t"
  2534. "pand %%mm7, %%mm1 \n\t"
  2535. "pand %%mm7, %%mm2 \n\t"
  2536. "pand %%mm7, %%mm3 \n\t"
  2537. "packuswb %%mm1, %%mm0 \n\t"
  2538. "packuswb %%mm3, %%mm2 \n\t"
  2539. "movq %%mm0, %%mm1 \n\t"
  2540. "movq %%mm2, %%mm3 \n\t"
  2541. "psrlw $8, %%mm0 \n\t"
  2542. "psrlw $8, %%mm2 \n\t"
  2543. "pand %%mm7, %%mm1 \n\t"
  2544. "pand %%mm7, %%mm3 \n\t"
  2545. "packuswb %%mm2, %%mm0 \n\t"
  2546. "packuswb %%mm3, %%mm1 \n\t"
  2547. MOVNTQ" %%mm0,- 7(%4, %0) \n\t"
  2548. MOVNTQ" %%mm1,- 7(%3, %0) \n\t"
  2549. "add $8, %0 \n\t"
  2550. " js 1b \n\t"
  2551. : "+r"(count)
  2552. : "r"(src0), "r"(src1), "r"(dst0), "r"(dst1)
  2553. );
  2554. count -= 7;
  2555. }
  2556. #endif
  2557. while(count<0) {
  2558. dst0[count]= (src0[4*count+0]+src1[4*count+0])>>1;
  2559. dst1[count]= (src0[4*count+2]+src1[4*count+2])>>1;
  2560. count++;
  2561. }
  2562. }
  2563. static void RENAME(extract_odd2)(const uint8_t *src, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2564. {
  2565. dst0+= count;
  2566. dst1+= count;
  2567. src += 4*count;
  2568. count= - count;
  2569. #if HAVE_MMX
  2570. if(count <= -8) {
  2571. count += 7;
  2572. __asm__ volatile(
  2573. "pcmpeqw %%mm7, %%mm7 \n\t"
  2574. "psrlw $8, %%mm7 \n\t"
  2575. "1: \n\t"
  2576. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2577. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2578. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2579. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2580. "psrlw $8, %%mm0 \n\t"
  2581. "psrlw $8, %%mm1 \n\t"
  2582. "psrlw $8, %%mm2 \n\t"
  2583. "psrlw $8, %%mm3 \n\t"
  2584. "packuswb %%mm1, %%mm0 \n\t"
  2585. "packuswb %%mm3, %%mm2 \n\t"
  2586. "movq %%mm0, %%mm1 \n\t"
  2587. "movq %%mm2, %%mm3 \n\t"
  2588. "psrlw $8, %%mm0 \n\t"
  2589. "psrlw $8, %%mm2 \n\t"
  2590. "pand %%mm7, %%mm1 \n\t"
  2591. "pand %%mm7, %%mm3 \n\t"
  2592. "packuswb %%mm2, %%mm0 \n\t"
  2593. "packuswb %%mm3, %%mm1 \n\t"
  2594. MOVNTQ" %%mm0,- 7(%3, %0) \n\t"
  2595. MOVNTQ" %%mm1,- 7(%2, %0) \n\t"
  2596. "add $8, %0 \n\t"
  2597. " js 1b \n\t"
  2598. : "+r"(count)
  2599. : "r"(src), "r"(dst0), "r"(dst1)
  2600. );
  2601. count -= 7;
  2602. }
  2603. #endif
  2604. src++;
  2605. while(count<0) {
  2606. dst0[count]= src[4*count+0];
  2607. dst1[count]= src[4*count+2];
  2608. count++;
  2609. }
  2610. }
  2611. static void RENAME(extract_odd2avg)(const uint8_t *src0, const uint8_t *src1, uint8_t *dst0, uint8_t *dst1, x86_reg count)
  2612. {
  2613. dst0 += count;
  2614. dst1 += count;
  2615. src0 += 4*count;
  2616. src1 += 4*count;
  2617. count= - count;
  2618. #ifdef PAVGB
  2619. if(count <= -8) {
  2620. count += 7;
  2621. __asm__ volatile(
  2622. "pcmpeqw %%mm7, %%mm7 \n\t"
  2623. "psrlw $8, %%mm7 \n\t"
  2624. "1: \n\t"
  2625. "movq -28(%1, %0, 4), %%mm0 \n\t"
  2626. "movq -20(%1, %0, 4), %%mm1 \n\t"
  2627. "movq -12(%1, %0, 4), %%mm2 \n\t"
  2628. "movq -4(%1, %0, 4), %%mm3 \n\t"
  2629. PAVGB" -28(%2, %0, 4), %%mm0 \n\t"
  2630. PAVGB" -20(%2, %0, 4), %%mm1 \n\t"
  2631. PAVGB" -12(%2, %0, 4), %%mm2 \n\t"
  2632. PAVGB" - 4(%2, %0, 4), %%mm3 \n\t"
  2633. "psrlw $8, %%mm0 \n\t"
  2634. "psrlw $8, %%mm1 \n\t"
  2635. "psrlw $8, %%mm2 \n\t"
  2636. "psrlw $8, %%mm3 \n\t"
  2637. "packuswb %%mm1, %%mm0 \n\t"
  2638. "packuswb %%mm3, %%mm2 \n\t"
  2639. "movq %%mm0, %%mm1 \n\t"
  2640. "movq %%mm2, %%mm3 \n\t"
  2641. "psrlw $8, %%mm0 \n\t"
  2642. "psrlw $8, %%mm2 \n\t"
  2643. "pand %%mm7, %%mm1 \n\t"
  2644. "pand %%mm7, %%mm3 \n\t"
  2645. "packuswb %%mm2, %%mm0 \n\t"
  2646. "packuswb %%mm3, %%mm1 \n\t"
  2647. MOVNTQ" %%mm0,- 7(%4, %0) \n\t"
  2648. MOVNTQ" %%mm1,- 7(%3, %0) \n\t"
  2649. "add $8, %0 \n\t"
  2650. " js 1b \n\t"
  2651. : "+r"(count)
  2652. : "r"(src0), "r"(src1), "r"(dst0), "r"(dst1)
  2653. );
  2654. count -= 7;
  2655. }
  2656. #endif
  2657. src0++;
  2658. src1++;
  2659. while(count<0) {
  2660. dst0[count]= (src0[4*count+0]+src1[4*count+0])>>1;
  2661. dst1[count]= (src0[4*count+2]+src1[4*count+2])>>1;
  2662. count++;
  2663. }
  2664. }
  2665. static void RENAME(yuyvtoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2666. long width, long height,
  2667. long lumStride, long chromStride, long srcStride)
  2668. {
  2669. long y;
  2670. const long chromWidth= -((-width)>>1);
  2671. for (y=0; y<height; y++) {
  2672. RENAME(extract_even)(src, ydst, width);
  2673. if(y&1) {
  2674. RENAME(extract_odd2avg)(src-srcStride, src, udst, vdst, chromWidth);
  2675. udst+= chromStride;
  2676. vdst+= chromStride;
  2677. }
  2678. src += srcStride;
  2679. ydst+= lumStride;
  2680. }
  2681. #if HAVE_MMX
  2682. __asm__(
  2683. EMMS" \n\t"
  2684. SFENCE" \n\t"
  2685. ::: "memory"
  2686. );
  2687. #endif
  2688. }
  2689. static void RENAME(yuyvtoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2690. long width, long height,
  2691. long lumStride, long chromStride, long srcStride)
  2692. {
  2693. long y;
  2694. const long chromWidth= -((-width)>>1);
  2695. for (y=0; y<height; y++) {
  2696. RENAME(extract_even)(src, ydst, width);
  2697. RENAME(extract_odd2)(src, udst, vdst, chromWidth);
  2698. src += srcStride;
  2699. ydst+= lumStride;
  2700. udst+= chromStride;
  2701. vdst+= chromStride;
  2702. }
  2703. #if HAVE_MMX
  2704. __asm__(
  2705. EMMS" \n\t"
  2706. SFENCE" \n\t"
  2707. ::: "memory"
  2708. );
  2709. #endif
  2710. }
  2711. static void RENAME(uyvytoyuv420)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2712. long width, long height,
  2713. long lumStride, long chromStride, long srcStride)
  2714. {
  2715. long y;
  2716. const long chromWidth= -((-width)>>1);
  2717. for (y=0; y<height; y++) {
  2718. RENAME(extract_even)(src+1, ydst, width);
  2719. if(y&1) {
  2720. RENAME(extract_even2avg)(src-srcStride, src, udst, vdst, chromWidth);
  2721. udst+= chromStride;
  2722. vdst+= chromStride;
  2723. }
  2724. src += srcStride;
  2725. ydst+= lumStride;
  2726. }
  2727. #if HAVE_MMX
  2728. __asm__(
  2729. EMMS" \n\t"
  2730. SFENCE" \n\t"
  2731. ::: "memory"
  2732. );
  2733. #endif
  2734. }
  2735. static void RENAME(uyvytoyuv422)(uint8_t *ydst, uint8_t *udst, uint8_t *vdst, const uint8_t *src,
  2736. long width, long height,
  2737. long lumStride, long chromStride, long srcStride)
  2738. {
  2739. long y;
  2740. const long chromWidth= -((-width)>>1);
  2741. for (y=0; y<height; y++) {
  2742. RENAME(extract_even)(src+1, ydst, width);
  2743. RENAME(extract_even2)(src, udst, vdst, chromWidth);
  2744. src += srcStride;
  2745. ydst+= lumStride;
  2746. udst+= chromStride;
  2747. vdst+= chromStride;
  2748. }
  2749. #if HAVE_MMX
  2750. __asm__(
  2751. EMMS" \n\t"
  2752. SFENCE" \n\t"
  2753. ::: "memory"
  2754. );
  2755. #endif
  2756. }
  2757. static inline void RENAME(rgb2rgb_init)(void)
  2758. {
  2759. rgb15to16 = RENAME(rgb15to16);
  2760. rgb15tobgr24 = RENAME(rgb15tobgr24);
  2761. rgb15to32 = RENAME(rgb15to32);
  2762. rgb16tobgr24 = RENAME(rgb16tobgr24);
  2763. rgb16to32 = RENAME(rgb16to32);
  2764. rgb16to15 = RENAME(rgb16to15);
  2765. rgb24tobgr16 = RENAME(rgb24tobgr16);
  2766. rgb24tobgr15 = RENAME(rgb24tobgr15);
  2767. rgb24tobgr32 = RENAME(rgb24tobgr32);
  2768. rgb32to16 = RENAME(rgb32to16);
  2769. rgb32to15 = RENAME(rgb32to15);
  2770. rgb32tobgr24 = RENAME(rgb32tobgr24);
  2771. rgb24to15 = RENAME(rgb24to15);
  2772. rgb24to16 = RENAME(rgb24to16);
  2773. rgb24tobgr24 = RENAME(rgb24tobgr24);
  2774. rgb32tobgr32 = RENAME(rgb32tobgr32);
  2775. rgb32tobgr16 = RENAME(rgb32tobgr16);
  2776. rgb32tobgr15 = RENAME(rgb32tobgr15);
  2777. yv12toyuy2 = RENAME(yv12toyuy2);
  2778. yv12touyvy = RENAME(yv12touyvy);
  2779. yuv422ptoyuy2 = RENAME(yuv422ptoyuy2);
  2780. yuv422ptouyvy = RENAME(yuv422ptouyvy);
  2781. yuy2toyv12 = RENAME(yuy2toyv12);
  2782. // yvu9toyv12 = RENAME(yvu9toyv12);
  2783. planar2x = RENAME(planar2x);
  2784. rgb24toyv12 = RENAME(rgb24toyv12);
  2785. interleaveBytes = RENAME(interleaveBytes);
  2786. vu9_to_vu12 = RENAME(vu9_to_vu12);
  2787. yvu9_to_yuy2 = RENAME(yvu9_to_yuy2);
  2788. uyvytoyuv420 = RENAME(uyvytoyuv420);
  2789. uyvytoyuv422 = RENAME(uyvytoyuv422);
  2790. yuyvtoyuv420 = RENAME(yuyvtoyuv420);
  2791. yuyvtoyuv422 = RENAME(yuyvtoyuv422);
  2792. }