swscale.c 81 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864
  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * the C code (not assembly, mmx, ...) of the swscaler which has been written
  21. * by Michael Niedermayer can be used under the LGPL license too
  22. */
  23. /*
  24. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR24, BGR16, BGR15, RGB32, RGB24, Y8/Y800, YVU9/IF09
  25. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  26. {BGR,RGB}{1,4,8,15,16} support dithering
  27. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  28. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  29. x -> x
  30. YUV9 -> YV12
  31. YUV9/YV12 -> Y800
  32. Y800 -> YUV9/YV12
  33. BGR24 -> BGR32 & RGB24 -> RGB32
  34. BGR32 -> BGR24 & RGB32 -> RGB24
  35. BGR15 -> BGR16
  36. */
  37. /*
  38. tested special converters (most are tested actually but i didnt write it down ...)
  39. YV12 -> BGR16
  40. YV12 -> YV12
  41. BGR15 -> BGR16
  42. BGR16 -> BGR16
  43. YVU9 -> YV12
  44. untested special converters
  45. YV12/I420 -> BGR15/BGR24/BGR32 (its the yuv2rgb stuff, so it should be ok)
  46. YV12/I420 -> YV12/I420
  47. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  48. BGR24 -> BGR32 & RGB24 -> RGB32
  49. BGR32 -> BGR24 & RGB32 -> RGB24
  50. BGR24 -> YV12
  51. */
  52. #include <inttypes.h>
  53. #include <string.h>
  54. #include <math.h>
  55. #include <stdio.h>
  56. #include <unistd.h>
  57. #include "config.h"
  58. #include <assert.h>
  59. #ifdef HAVE_MALLOC_H
  60. #include <malloc.h>
  61. #else
  62. #include <stdlib.h>
  63. #endif
  64. #ifdef HAVE_SYS_MMAN_H
  65. #include <sys/mman.h>
  66. #if defined(MAP_ANON) && !defined(MAP_ANONYMOUS)
  67. #define MAP_ANONYMOUS MAP_ANON
  68. #endif
  69. #endif
  70. #include "swscale.h"
  71. #include "swscale_internal.h"
  72. #include "x86_cpu.h"
  73. #include "bswap.h"
  74. #include "rgb2rgb.h"
  75. #ifdef USE_FASTMEMCPY
  76. #include "libvo/fastmemcpy.h"
  77. #endif
  78. #undef MOVNTQ
  79. #undef PAVGB
  80. //#undef HAVE_MMX2
  81. //#define HAVE_3DNOW
  82. //#undef HAVE_MMX
  83. //#undef ARCH_X86
  84. //#define WORDS_BIGENDIAN
  85. #define DITHER1XBPP
  86. #define FAST_BGR2YV12 // use 7 bit coeffs instead of 15bit
  87. #define RET 0xC3 //near return opcode for X86
  88. #ifdef MP_DEBUG
  89. #define ASSERT(x) assert(x);
  90. #else
  91. #define ASSERT(x) ;
  92. #endif
  93. #ifdef M_PI
  94. #define PI M_PI
  95. #else
  96. #define PI 3.14159265358979323846
  97. #endif
  98. #define isSupportedIn(x) ((x)==PIX_FMT_YUV420P || (x)==PIX_FMT_YUYV422 || (x)==PIX_FMT_UYVY422\
  99. || (x)==PIX_FMT_RGB32|| (x)==PIX_FMT_BGR24|| (x)==PIX_FMT_BGR565|| (x)==PIX_FMT_BGR555\
  100. || (x)==PIX_FMT_BGR32|| (x)==PIX_FMT_RGB24\
  101. || (x)==PIX_FMT_GRAY8 || (x)==PIX_FMT_YUV410P\
  102. || (x)==PIX_FMT_GRAY16BE || (x)==PIX_FMT_GRAY16LE\
  103. || (x)==PIX_FMT_YUV444P || (x)==PIX_FMT_YUV422P || (x)==PIX_FMT_YUV411P)
  104. #define isSupportedOut(x) ((x)==PIX_FMT_YUV420P || (x)==PIX_FMT_YUYV422 || (x)==PIX_FMT_UYVY422\
  105. || (x)==PIX_FMT_YUV444P || (x)==PIX_FMT_YUV422P || (x)==PIX_FMT_YUV411P\
  106. || isRGB(x) || isBGR(x)\
  107. || (x)==PIX_FMT_NV12 || (x)==PIX_FMT_NV21\
  108. || (x)==PIX_FMT_GRAY16BE || (x)==PIX_FMT_GRAY16LE\
  109. || (x)==PIX_FMT_GRAY8 || (x)==PIX_FMT_YUV410P)
  110. #define isPacked(x) ((x)==PIX_FMT_YUYV422 || (x)==PIX_FMT_UYVY422 ||isRGB(x) || isBGR(x))
  111. #define RGB2YUV_SHIFT 16
  112. #define BY ((int)( 0.098*(1<<RGB2YUV_SHIFT)+0.5))
  113. #define BV ((int)(-0.071*(1<<RGB2YUV_SHIFT)+0.5))
  114. #define BU ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  115. #define GY ((int)( 0.504*(1<<RGB2YUV_SHIFT)+0.5))
  116. #define GV ((int)(-0.368*(1<<RGB2YUV_SHIFT)+0.5))
  117. #define GU ((int)(-0.291*(1<<RGB2YUV_SHIFT)+0.5))
  118. #define RY ((int)( 0.257*(1<<RGB2YUV_SHIFT)+0.5))
  119. #define RV ((int)( 0.439*(1<<RGB2YUV_SHIFT)+0.5))
  120. #define RU ((int)(-0.148*(1<<RGB2YUV_SHIFT)+0.5))
  121. extern const int32_t Inverse_Table_6_9[8][4];
  122. /*
  123. NOTES
  124. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  125. TODO
  126. more intelligent missalignment avoidance for the horizontal scaler
  127. write special vertical cubic upscale version
  128. Optimize C code (yv12 / minmax)
  129. add support for packed pixel yuv input & output
  130. add support for Y8 output
  131. optimize bgr24 & bgr32
  132. add BGR4 output support
  133. write special BGR->BGR scaler
  134. */
  135. #if defined(ARCH_X86)
  136. static uint64_t attribute_used __attribute__((aligned(8))) bF8= 0xF8F8F8F8F8F8F8F8LL;
  137. static uint64_t attribute_used __attribute__((aligned(8))) bFC= 0xFCFCFCFCFCFCFCFCLL;
  138. static uint64_t __attribute__((aligned(8))) w10= 0x0010001000100010LL;
  139. static uint64_t attribute_used __attribute__((aligned(8))) w02= 0x0002000200020002LL;
  140. static uint64_t attribute_used __attribute__((aligned(8))) bm00001111=0x00000000FFFFFFFFLL;
  141. static uint64_t attribute_used __attribute__((aligned(8))) bm00000111=0x0000000000FFFFFFLL;
  142. static uint64_t attribute_used __attribute__((aligned(8))) bm11111000=0xFFFFFFFFFF000000LL;
  143. static uint64_t attribute_used __attribute__((aligned(8))) bm01010101=0x00FF00FF00FF00FFLL;
  144. static volatile uint64_t attribute_used __attribute__((aligned(8))) b5Dither;
  145. static volatile uint64_t attribute_used __attribute__((aligned(8))) g5Dither;
  146. static volatile uint64_t attribute_used __attribute__((aligned(8))) g6Dither;
  147. static volatile uint64_t attribute_used __attribute__((aligned(8))) r5Dither;
  148. static uint64_t __attribute__((aligned(8))) dither4[2]={
  149. 0x0103010301030103LL,
  150. 0x0200020002000200LL,};
  151. static uint64_t __attribute__((aligned(8))) dither8[2]={
  152. 0x0602060206020602LL,
  153. 0x0004000400040004LL,};
  154. static uint64_t __attribute__((aligned(8))) b16Mask= 0x001F001F001F001FLL;
  155. static uint64_t attribute_used __attribute__((aligned(8))) g16Mask= 0x07E007E007E007E0LL;
  156. static uint64_t attribute_used __attribute__((aligned(8))) r16Mask= 0xF800F800F800F800LL;
  157. static uint64_t __attribute__((aligned(8))) b15Mask= 0x001F001F001F001FLL;
  158. static uint64_t attribute_used __attribute__((aligned(8))) g15Mask= 0x03E003E003E003E0LL;
  159. static uint64_t attribute_used __attribute__((aligned(8))) r15Mask= 0x7C007C007C007C00LL;
  160. static uint64_t attribute_used __attribute__((aligned(8))) M24A= 0x00FF0000FF0000FFLL;
  161. static uint64_t attribute_used __attribute__((aligned(8))) M24B= 0xFF0000FF0000FF00LL;
  162. static uint64_t attribute_used __attribute__((aligned(8))) M24C= 0x0000FF0000FF0000LL;
  163. #ifdef FAST_BGR2YV12
  164. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000000210041000DULL;
  165. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000FFEEFFDC0038ULL;
  166. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00000038FFD2FFF8ULL;
  167. #else
  168. static const uint64_t bgr2YCoeff attribute_used __attribute__((aligned(8))) = 0x000020E540830C8BULL;
  169. static const uint64_t bgr2UCoeff attribute_used __attribute__((aligned(8))) = 0x0000ED0FDAC23831ULL;
  170. static const uint64_t bgr2VCoeff attribute_used __attribute__((aligned(8))) = 0x00003831D0E6F6EAULL;
  171. #endif /* FAST_BGR2YV12 */
  172. static const uint64_t bgr2YOffset attribute_used __attribute__((aligned(8))) = 0x1010101010101010ULL;
  173. static const uint64_t bgr2UVOffset attribute_used __attribute__((aligned(8)))= 0x8080808080808080ULL;
  174. static const uint64_t w1111 attribute_used __attribute__((aligned(8))) = 0x0001000100010001ULL;
  175. #endif /* defined(ARCH_X86) */
  176. // clipping helper table for C implementations:
  177. static unsigned char clip_table[768];
  178. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b);
  179. extern const uint8_t dither_2x2_4[2][8];
  180. extern const uint8_t dither_2x2_8[2][8];
  181. extern const uint8_t dither_8x8_32[8][8];
  182. extern const uint8_t dither_8x8_73[8][8];
  183. extern const uint8_t dither_8x8_220[8][8];
  184. char *sws_format_name(enum PixelFormat format)
  185. {
  186. switch (format) {
  187. case PIX_FMT_YUV420P:
  188. return "yuv420p";
  189. case PIX_FMT_YUYV422:
  190. return "yuyv422";
  191. case PIX_FMT_RGB24:
  192. return "rgb24";
  193. case PIX_FMT_BGR24:
  194. return "bgr24";
  195. case PIX_FMT_YUV422P:
  196. return "yuv422p";
  197. case PIX_FMT_YUV444P:
  198. return "yuv444p";
  199. case PIX_FMT_RGB32:
  200. return "rgb32";
  201. case PIX_FMT_YUV410P:
  202. return "yuv410p";
  203. case PIX_FMT_YUV411P:
  204. return "yuv411p";
  205. case PIX_FMT_RGB565:
  206. return "rgb565";
  207. case PIX_FMT_RGB555:
  208. return "rgb555";
  209. case PIX_FMT_GRAY16BE:
  210. return "gray16be";
  211. case PIX_FMT_GRAY16LE:
  212. return "gray16le";
  213. case PIX_FMT_GRAY8:
  214. return "gray8";
  215. case PIX_FMT_MONOWHITE:
  216. return "mono white";
  217. case PIX_FMT_MONOBLACK:
  218. return "mono black";
  219. case PIX_FMT_PAL8:
  220. return "Palette";
  221. case PIX_FMT_YUVJ420P:
  222. return "yuvj420p";
  223. case PIX_FMT_YUVJ422P:
  224. return "yuvj422p";
  225. case PIX_FMT_YUVJ444P:
  226. return "yuvj444p";
  227. case PIX_FMT_XVMC_MPEG2_MC:
  228. return "xvmc_mpeg2_mc";
  229. case PIX_FMT_XVMC_MPEG2_IDCT:
  230. return "xvmc_mpeg2_idct";
  231. case PIX_FMT_UYVY422:
  232. return "uyvy422";
  233. case PIX_FMT_UYYVYY411:
  234. return "uyyvyy411";
  235. case PIX_FMT_RGB32_1:
  236. return "rgb32x";
  237. case PIX_FMT_BGR32_1:
  238. return "bgr32x";
  239. case PIX_FMT_BGR32:
  240. return "bgr32";
  241. case PIX_FMT_BGR565:
  242. return "bgr565";
  243. case PIX_FMT_BGR555:
  244. return "bgr555";
  245. case PIX_FMT_BGR8:
  246. return "bgr8";
  247. case PIX_FMT_BGR4:
  248. return "bgr4";
  249. case PIX_FMT_BGR4_BYTE:
  250. return "bgr4 byte";
  251. case PIX_FMT_RGB8:
  252. return "rgb8";
  253. case PIX_FMT_RGB4:
  254. return "rgb4";
  255. case PIX_FMT_RGB4_BYTE:
  256. return "rgb4 byte";
  257. case PIX_FMT_NV12:
  258. return "nv12";
  259. case PIX_FMT_NV21:
  260. return "nv21";
  261. default:
  262. return "Unknown format";
  263. }
  264. }
  265. #if defined(ARCH_X86)
  266. void in_asm_used_var_warning_killer()
  267. {
  268. volatile int i= bF8+bFC+w10+
  269. bm00001111+bm00000111+bm11111000+b16Mask+g16Mask+r16Mask+b15Mask+g15Mask+r15Mask+
  270. M24A+M24B+M24C+w02 + b5Dither+g5Dither+r5Dither+g6Dither+dither4[0]+dither8[0]+bm01010101;
  271. if(i) i=0;
  272. }
  273. #endif
  274. static inline void yuv2yuvXinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  275. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  276. uint8_t *dest, uint8_t *uDest, uint8_t *vDest, int dstW, int chrDstW)
  277. {
  278. //FIXME Optimize (just quickly writen not opti..)
  279. int i;
  280. for(i=0; i<dstW; i++)
  281. {
  282. int val=1<<18;
  283. int j;
  284. for(j=0; j<lumFilterSize; j++)
  285. val += lumSrc[j][i] * lumFilter[j];
  286. dest[i]= FFMIN(FFMAX(val>>19, 0), 255);
  287. }
  288. if(uDest != NULL)
  289. for(i=0; i<chrDstW; i++)
  290. {
  291. int u=1<<18;
  292. int v=1<<18;
  293. int j;
  294. for(j=0; j<chrFilterSize; j++)
  295. {
  296. u += chrSrc[j][i] * chrFilter[j];
  297. v += chrSrc[j][i + 2048] * chrFilter[j];
  298. }
  299. uDest[i]= FFMIN(FFMAX(u>>19, 0), 255);
  300. vDest[i]= FFMIN(FFMAX(v>>19, 0), 255);
  301. }
  302. }
  303. static inline void yuv2nv12XinC(int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  304. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  305. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  306. {
  307. //FIXME Optimize (just quickly writen not opti..)
  308. int i;
  309. for(i=0; i<dstW; i++)
  310. {
  311. int val=1<<18;
  312. int j;
  313. for(j=0; j<lumFilterSize; j++)
  314. val += lumSrc[j][i] * lumFilter[j];
  315. dest[i]= FFMIN(FFMAX(val>>19, 0), 255);
  316. }
  317. if(uDest == NULL)
  318. return;
  319. if(dstFormat == PIX_FMT_NV12)
  320. for(i=0; i<chrDstW; i++)
  321. {
  322. int u=1<<18;
  323. int v=1<<18;
  324. int j;
  325. for(j=0; j<chrFilterSize; j++)
  326. {
  327. u += chrSrc[j][i] * chrFilter[j];
  328. v += chrSrc[j][i + 2048] * chrFilter[j];
  329. }
  330. uDest[2*i]= FFMIN(FFMAX(u>>19, 0), 255);
  331. uDest[2*i+1]= FFMIN(FFMAX(v>>19, 0), 255);
  332. }
  333. else
  334. for(i=0; i<chrDstW; i++)
  335. {
  336. int u=1<<18;
  337. int v=1<<18;
  338. int j;
  339. for(j=0; j<chrFilterSize; j++)
  340. {
  341. u += chrSrc[j][i] * chrFilter[j];
  342. v += chrSrc[j][i + 2048] * chrFilter[j];
  343. }
  344. uDest[2*i]= FFMIN(FFMAX(v>>19, 0), 255);
  345. uDest[2*i+1]= FFMIN(FFMAX(u>>19, 0), 255);
  346. }
  347. }
  348. #define YSCALE_YUV_2_PACKEDX_C(type) \
  349. for(i=0; i<(dstW>>1); i++){\
  350. int j;\
  351. int Y1=1<<18;\
  352. int Y2=1<<18;\
  353. int U=1<<18;\
  354. int V=1<<18;\
  355. type *r, *b, *g;\
  356. const int i2= 2*i;\
  357. \
  358. for(j=0; j<lumFilterSize; j++)\
  359. {\
  360. Y1 += lumSrc[j][i2] * lumFilter[j];\
  361. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  362. }\
  363. for(j=0; j<chrFilterSize; j++)\
  364. {\
  365. U += chrSrc[j][i] * chrFilter[j];\
  366. V += chrSrc[j][i+2048] * chrFilter[j];\
  367. }\
  368. Y1>>=19;\
  369. Y2>>=19;\
  370. U >>=19;\
  371. V >>=19;\
  372. if((Y1|Y2|U|V)&256)\
  373. {\
  374. if(Y1>255) Y1=255;\
  375. else if(Y1<0)Y1=0;\
  376. if(Y2>255) Y2=255;\
  377. else if(Y2<0)Y2=0;\
  378. if(U>255) U=255;\
  379. else if(U<0) U=0;\
  380. if(V>255) V=255;\
  381. else if(V<0) V=0;\
  382. }
  383. #define YSCALE_YUV_2_RGBX_C(type) \
  384. YSCALE_YUV_2_PACKEDX_C(type)\
  385. r = c->table_rV[V];\
  386. g = c->table_gU[U] + c->table_gV[V];\
  387. b = c->table_bU[U];\
  388. #define YSCALE_YUV_2_PACKED2_C \
  389. for(i=0; i<(dstW>>1); i++){\
  390. const int i2= 2*i;\
  391. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19;\
  392. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19;\
  393. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19;\
  394. int V= (uvbuf0[i+2048]*uvalpha1+uvbuf1[i+2048]*uvalpha)>>19;\
  395. #define YSCALE_YUV_2_RGB2_C(type) \
  396. YSCALE_YUV_2_PACKED2_C\
  397. type *r, *b, *g;\
  398. r = c->table_rV[V];\
  399. g = c->table_gU[U] + c->table_gV[V];\
  400. b = c->table_bU[U];\
  401. #define YSCALE_YUV_2_PACKED1_C \
  402. for(i=0; i<(dstW>>1); i++){\
  403. const int i2= 2*i;\
  404. int Y1= buf0[i2 ]>>7;\
  405. int Y2= buf0[i2+1]>>7;\
  406. int U= (uvbuf1[i ])>>7;\
  407. int V= (uvbuf1[i+2048])>>7;\
  408. #define YSCALE_YUV_2_RGB1_C(type) \
  409. YSCALE_YUV_2_PACKED1_C\
  410. type *r, *b, *g;\
  411. r = c->table_rV[V];\
  412. g = c->table_gU[U] + c->table_gV[V];\
  413. b = c->table_bU[U];\
  414. #define YSCALE_YUV_2_PACKED1B_C \
  415. for(i=0; i<(dstW>>1); i++){\
  416. const int i2= 2*i;\
  417. int Y1= buf0[i2 ]>>7;\
  418. int Y2= buf0[i2+1]>>7;\
  419. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  420. int V= (uvbuf0[i+2048] + uvbuf1[i+2048])>>8;\
  421. #define YSCALE_YUV_2_RGB1B_C(type) \
  422. YSCALE_YUV_2_PACKED1B_C\
  423. type *r, *b, *g;\
  424. r = c->table_rV[V];\
  425. g = c->table_gU[U] + c->table_gV[V];\
  426. b = c->table_bU[U];\
  427. #define YSCALE_YUV_2_ANYRGB_C(func, func2)\
  428. switch(c->dstFormat)\
  429. {\
  430. case PIX_FMT_RGB32:\
  431. case PIX_FMT_BGR32:\
  432. func(uint32_t)\
  433. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  434. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  435. } \
  436. break;\
  437. case PIX_FMT_RGB24:\
  438. func(uint8_t)\
  439. ((uint8_t*)dest)[0]= r[Y1];\
  440. ((uint8_t*)dest)[1]= g[Y1];\
  441. ((uint8_t*)dest)[2]= b[Y1];\
  442. ((uint8_t*)dest)[3]= r[Y2];\
  443. ((uint8_t*)dest)[4]= g[Y2];\
  444. ((uint8_t*)dest)[5]= b[Y2];\
  445. dest+=6;\
  446. }\
  447. break;\
  448. case PIX_FMT_BGR24:\
  449. func(uint8_t)\
  450. ((uint8_t*)dest)[0]= b[Y1];\
  451. ((uint8_t*)dest)[1]= g[Y1];\
  452. ((uint8_t*)dest)[2]= r[Y1];\
  453. ((uint8_t*)dest)[3]= b[Y2];\
  454. ((uint8_t*)dest)[4]= g[Y2];\
  455. ((uint8_t*)dest)[5]= r[Y2];\
  456. dest+=6;\
  457. }\
  458. break;\
  459. case PIX_FMT_RGB565:\
  460. case PIX_FMT_BGR565:\
  461. {\
  462. const int dr1= dither_2x2_8[y&1 ][0];\
  463. const int dg1= dither_2x2_4[y&1 ][0];\
  464. const int db1= dither_2x2_8[(y&1)^1][0];\
  465. const int dr2= dither_2x2_8[y&1 ][1];\
  466. const int dg2= dither_2x2_4[y&1 ][1];\
  467. const int db2= dither_2x2_8[(y&1)^1][1];\
  468. func(uint16_t)\
  469. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  470. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  471. }\
  472. }\
  473. break;\
  474. case PIX_FMT_RGB555:\
  475. case PIX_FMT_BGR555:\
  476. {\
  477. const int dr1= dither_2x2_8[y&1 ][0];\
  478. const int dg1= dither_2x2_8[y&1 ][1];\
  479. const int db1= dither_2x2_8[(y&1)^1][0];\
  480. const int dr2= dither_2x2_8[y&1 ][1];\
  481. const int dg2= dither_2x2_8[y&1 ][0];\
  482. const int db2= dither_2x2_8[(y&1)^1][1];\
  483. func(uint16_t)\
  484. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  485. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  486. }\
  487. }\
  488. break;\
  489. case PIX_FMT_RGB8:\
  490. case PIX_FMT_BGR8:\
  491. {\
  492. const uint8_t * const d64= dither_8x8_73[y&7];\
  493. const uint8_t * const d32= dither_8x8_32[y&7];\
  494. func(uint8_t)\
  495. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  496. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  497. }\
  498. }\
  499. break;\
  500. case PIX_FMT_RGB4:\
  501. case PIX_FMT_BGR4:\
  502. {\
  503. const uint8_t * const d64= dither_8x8_73 [y&7];\
  504. const uint8_t * const d128=dither_8x8_220[y&7];\
  505. func(uint8_t)\
  506. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  507. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  508. }\
  509. }\
  510. break;\
  511. case PIX_FMT_RGB4_BYTE:\
  512. case PIX_FMT_BGR4_BYTE:\
  513. {\
  514. const uint8_t * const d64= dither_8x8_73 [y&7];\
  515. const uint8_t * const d128=dither_8x8_220[y&7];\
  516. func(uint8_t)\
  517. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  518. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  519. }\
  520. }\
  521. break;\
  522. case PIX_FMT_MONOBLACK:\
  523. {\
  524. const uint8_t * const d128=dither_8x8_220[y&7];\
  525. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  526. for(i=0; i<dstW-7; i+=8){\
  527. int acc;\
  528. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  529. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  530. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  531. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  532. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  533. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  534. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  535. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  536. ((uint8_t*)dest)[0]= acc;\
  537. dest++;\
  538. }\
  539. \
  540. /*\
  541. ((uint8_t*)dest)-= dstW>>4;\
  542. {\
  543. int acc=0;\
  544. int left=0;\
  545. static int top[1024];\
  546. static int last_new[1024][1024];\
  547. static int last_in3[1024][1024];\
  548. static int drift[1024][1024];\
  549. int topLeft=0;\
  550. int shift=0;\
  551. int count=0;\
  552. const uint8_t * const d128=dither_8x8_220[y&7];\
  553. int error_new=0;\
  554. int error_in3=0;\
  555. int f=0;\
  556. \
  557. for(i=dstW>>1; i<dstW; i++){\
  558. int in= ((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19);\
  559. int in2 = (76309 * (in - 16) + 32768) >> 16;\
  560. int in3 = (in2 < 0) ? 0 : ((in2 > 255) ? 255 : in2);\
  561. int old= (left*7 + topLeft + top[i]*5 + top[i+1]*3)/20 + in3\
  562. + (last_new[y][i] - in3)*f/256;\
  563. int new= old> 128 ? 255 : 0;\
  564. \
  565. error_new+= FFABS(last_new[y][i] - new);\
  566. error_in3+= FFABS(last_in3[y][i] - in3);\
  567. f= error_new - error_in3*4;\
  568. if(f<0) f=0;\
  569. if(f>256) f=256;\
  570. \
  571. topLeft= top[i];\
  572. left= top[i]= old - new;\
  573. last_new[y][i]= new;\
  574. last_in3[y][i]= in3;\
  575. \
  576. acc+= acc + (new&1);\
  577. if((i&7)==6){\
  578. ((uint8_t*)dest)[0]= acc;\
  579. ((uint8_t*)dest)++;\
  580. }\
  581. }\
  582. }\
  583. */\
  584. }\
  585. break;\
  586. case PIX_FMT_YUYV422:\
  587. func2\
  588. ((uint8_t*)dest)[2*i2+0]= Y1;\
  589. ((uint8_t*)dest)[2*i2+1]= U;\
  590. ((uint8_t*)dest)[2*i2+2]= Y2;\
  591. ((uint8_t*)dest)[2*i2+3]= V;\
  592. } \
  593. break;\
  594. case PIX_FMT_UYVY422:\
  595. func2\
  596. ((uint8_t*)dest)[2*i2+0]= U;\
  597. ((uint8_t*)dest)[2*i2+1]= Y1;\
  598. ((uint8_t*)dest)[2*i2+2]= V;\
  599. ((uint8_t*)dest)[2*i2+3]= Y2;\
  600. } \
  601. break;\
  602. }\
  603. static inline void yuv2packedXinC(SwsContext *c, int16_t *lumFilter, int16_t **lumSrc, int lumFilterSize,
  604. int16_t *chrFilter, int16_t **chrSrc, int chrFilterSize,
  605. uint8_t *dest, int dstW, int y)
  606. {
  607. int i;
  608. switch(c->dstFormat)
  609. {
  610. case PIX_FMT_BGR32:
  611. case PIX_FMT_RGB32:
  612. YSCALE_YUV_2_RGBX_C(uint32_t)
  613. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];
  614. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];
  615. }
  616. break;
  617. case PIX_FMT_RGB24:
  618. YSCALE_YUV_2_RGBX_C(uint8_t)
  619. ((uint8_t*)dest)[0]= r[Y1];
  620. ((uint8_t*)dest)[1]= g[Y1];
  621. ((uint8_t*)dest)[2]= b[Y1];
  622. ((uint8_t*)dest)[3]= r[Y2];
  623. ((uint8_t*)dest)[4]= g[Y2];
  624. ((uint8_t*)dest)[5]= b[Y2];
  625. dest+=6;
  626. }
  627. break;
  628. case PIX_FMT_BGR24:
  629. YSCALE_YUV_2_RGBX_C(uint8_t)
  630. ((uint8_t*)dest)[0]= b[Y1];
  631. ((uint8_t*)dest)[1]= g[Y1];
  632. ((uint8_t*)dest)[2]= r[Y1];
  633. ((uint8_t*)dest)[3]= b[Y2];
  634. ((uint8_t*)dest)[4]= g[Y2];
  635. ((uint8_t*)dest)[5]= r[Y2];
  636. dest+=6;
  637. }
  638. break;
  639. case PIX_FMT_RGB565:
  640. case PIX_FMT_BGR565:
  641. {
  642. const int dr1= dither_2x2_8[y&1 ][0];
  643. const int dg1= dither_2x2_4[y&1 ][0];
  644. const int db1= dither_2x2_8[(y&1)^1][0];
  645. const int dr2= dither_2x2_8[y&1 ][1];
  646. const int dg2= dither_2x2_4[y&1 ][1];
  647. const int db2= dither_2x2_8[(y&1)^1][1];
  648. YSCALE_YUV_2_RGBX_C(uint16_t)
  649. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  650. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  651. }
  652. }
  653. break;
  654. case PIX_FMT_RGB555:
  655. case PIX_FMT_BGR555:
  656. {
  657. const int dr1= dither_2x2_8[y&1 ][0];
  658. const int dg1= dither_2x2_8[y&1 ][1];
  659. const int db1= dither_2x2_8[(y&1)^1][0];
  660. const int dr2= dither_2x2_8[y&1 ][1];
  661. const int dg2= dither_2x2_8[y&1 ][0];
  662. const int db2= dither_2x2_8[(y&1)^1][1];
  663. YSCALE_YUV_2_RGBX_C(uint16_t)
  664. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];
  665. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];
  666. }
  667. }
  668. break;
  669. case PIX_FMT_RGB8:
  670. case PIX_FMT_BGR8:
  671. {
  672. const uint8_t * const d64= dither_8x8_73[y&7];
  673. const uint8_t * const d32= dither_8x8_32[y&7];
  674. YSCALE_YUV_2_RGBX_C(uint8_t)
  675. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];
  676. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];
  677. }
  678. }
  679. break;
  680. case PIX_FMT_RGB4:
  681. case PIX_FMT_BGR4:
  682. {
  683. const uint8_t * const d64= dither_8x8_73 [y&7];
  684. const uint8_t * const d128=dither_8x8_220[y&7];
  685. YSCALE_YUV_2_RGBX_C(uint8_t)
  686. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]
  687. +((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);
  688. }
  689. }
  690. break;
  691. case PIX_FMT_RGB4_BYTE:
  692. case PIX_FMT_BGR4_BYTE:
  693. {
  694. const uint8_t * const d64= dither_8x8_73 [y&7];
  695. const uint8_t * const d128=dither_8x8_220[y&7];
  696. YSCALE_YUV_2_RGBX_C(uint8_t)
  697. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];
  698. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];
  699. }
  700. }
  701. break;
  702. case PIX_FMT_MONOBLACK:
  703. {
  704. const uint8_t * const d128=dither_8x8_220[y&7];
  705. uint8_t *g= c->table_gU[128] + c->table_gV[128];
  706. int acc=0;
  707. for(i=0; i<dstW-1; i+=2){
  708. int j;
  709. int Y1=1<<18;
  710. int Y2=1<<18;
  711. for(j=0; j<lumFilterSize; j++)
  712. {
  713. Y1 += lumSrc[j][i] * lumFilter[j];
  714. Y2 += lumSrc[j][i+1] * lumFilter[j];
  715. }
  716. Y1>>=19;
  717. Y2>>=19;
  718. if((Y1|Y2)&256)
  719. {
  720. if(Y1>255) Y1=255;
  721. else if(Y1<0)Y1=0;
  722. if(Y2>255) Y2=255;
  723. else if(Y2<0)Y2=0;
  724. }
  725. acc+= acc + g[Y1+d128[(i+0)&7]];
  726. acc+= acc + g[Y2+d128[(i+1)&7]];
  727. if((i&7)==6){
  728. ((uint8_t*)dest)[0]= acc;
  729. dest++;
  730. }
  731. }
  732. }
  733. break;
  734. case PIX_FMT_YUYV422:
  735. YSCALE_YUV_2_PACKEDX_C(void)
  736. ((uint8_t*)dest)[2*i2+0]= Y1;
  737. ((uint8_t*)dest)[2*i2+1]= U;
  738. ((uint8_t*)dest)[2*i2+2]= Y2;
  739. ((uint8_t*)dest)[2*i2+3]= V;
  740. }
  741. break;
  742. case PIX_FMT_UYVY422:
  743. YSCALE_YUV_2_PACKEDX_C(void)
  744. ((uint8_t*)dest)[2*i2+0]= U;
  745. ((uint8_t*)dest)[2*i2+1]= Y1;
  746. ((uint8_t*)dest)[2*i2+2]= V;
  747. ((uint8_t*)dest)[2*i2+3]= Y2;
  748. }
  749. break;
  750. }
  751. }
  752. //Note: we have C, X86, MMX, MMX2, 3DNOW version therse no 3DNOW+MMX2 one
  753. //Plain C versions
  754. #if !defined (HAVE_MMX) || defined (RUNTIME_CPUDETECT)
  755. #define COMPILE_C
  756. #endif
  757. #ifdef ARCH_POWERPC
  758. #if defined (HAVE_ALTIVEC) || defined (RUNTIME_CPUDETECT)
  759. #define COMPILE_ALTIVEC
  760. #endif //HAVE_ALTIVEC
  761. #endif //ARCH_POWERPC
  762. #if defined(ARCH_X86)
  763. #if (defined (HAVE_MMX) && !defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  764. #define COMPILE_MMX
  765. #endif
  766. #if defined (HAVE_MMX2) || defined (RUNTIME_CPUDETECT)
  767. #define COMPILE_MMX2
  768. #endif
  769. #if (defined (HAVE_3DNOW) && !defined (HAVE_MMX2)) || defined (RUNTIME_CPUDETECT)
  770. #define COMPILE_3DNOW
  771. #endif
  772. #endif //ARCH_X86 || ARCH_X86_64
  773. #undef HAVE_MMX
  774. #undef HAVE_MMX2
  775. #undef HAVE_3DNOW
  776. #ifdef COMPILE_C
  777. #undef HAVE_MMX
  778. #undef HAVE_MMX2
  779. #undef HAVE_3DNOW
  780. #undef HAVE_ALTIVEC
  781. #define RENAME(a) a ## _C
  782. #include "swscale_template.c"
  783. #endif
  784. #ifdef ARCH_POWERPC
  785. #ifdef COMPILE_ALTIVEC
  786. #undef RENAME
  787. #define HAVE_ALTIVEC
  788. #define RENAME(a) a ## _altivec
  789. #include "swscale_template.c"
  790. #endif
  791. #endif //ARCH_POWERPC
  792. #if defined(ARCH_X86)
  793. //X86 versions
  794. /*
  795. #undef RENAME
  796. #undef HAVE_MMX
  797. #undef HAVE_MMX2
  798. #undef HAVE_3DNOW
  799. #define ARCH_X86
  800. #define RENAME(a) a ## _X86
  801. #include "swscale_template.c"
  802. */
  803. //MMX versions
  804. #ifdef COMPILE_MMX
  805. #undef RENAME
  806. #define HAVE_MMX
  807. #undef HAVE_MMX2
  808. #undef HAVE_3DNOW
  809. #define RENAME(a) a ## _MMX
  810. #include "swscale_template.c"
  811. #endif
  812. //MMX2 versions
  813. #ifdef COMPILE_MMX2
  814. #undef RENAME
  815. #define HAVE_MMX
  816. #define HAVE_MMX2
  817. #undef HAVE_3DNOW
  818. #define RENAME(a) a ## _MMX2
  819. #include "swscale_template.c"
  820. #endif
  821. //3DNOW versions
  822. #ifdef COMPILE_3DNOW
  823. #undef RENAME
  824. #define HAVE_MMX
  825. #undef HAVE_MMX2
  826. #define HAVE_3DNOW
  827. #define RENAME(a) a ## _3DNow
  828. #include "swscale_template.c"
  829. #endif
  830. #endif //ARCH_X86 || ARCH_X86_64
  831. // minor note: the HAVE_xyz is messed up after that line so don't use it
  832. static double getSplineCoeff(double a, double b, double c, double d, double dist)
  833. {
  834. // printf("%f %f %f %f %f\n", a,b,c,d,dist);
  835. if(dist<=1.0) return ((d*dist + c)*dist + b)*dist +a;
  836. else return getSplineCoeff( 0.0,
  837. b+ 2.0*c + 3.0*d,
  838. c + 3.0*d,
  839. -b- 3.0*c - 6.0*d,
  840. dist-1.0);
  841. }
  842. static inline int initFilter(int16_t **outFilter, int16_t **filterPos, int *outFilterSize, int xInc,
  843. int srcW, int dstW, int filterAlign, int one, int flags,
  844. SwsVector *srcFilter, SwsVector *dstFilter, double param[2])
  845. {
  846. int i;
  847. int filterSize;
  848. int filter2Size;
  849. int minFilterSize;
  850. double *filter=NULL;
  851. double *filter2=NULL;
  852. #if defined(ARCH_X86)
  853. if(flags & SWS_CPU_CAPS_MMX)
  854. asm volatile("emms\n\t"::: "memory"); //FIXME this shouldnt be required but it IS (even for non mmx versions)
  855. #endif
  856. // Note the +1 is for the MMXscaler which reads over the end
  857. *filterPos = av_malloc((dstW+1)*sizeof(int16_t));
  858. if(FFABS(xInc - 0x10000) <10) // unscaled
  859. {
  860. int i;
  861. filterSize= 1;
  862. filter= av_malloc(dstW*sizeof(double)*filterSize);
  863. for(i=0; i<dstW*filterSize; i++) filter[i]=0;
  864. for(i=0; i<dstW; i++)
  865. {
  866. filter[i*filterSize]=1;
  867. (*filterPos)[i]=i;
  868. }
  869. }
  870. else if(flags&SWS_POINT) // lame looking point sampling mode
  871. {
  872. int i;
  873. int xDstInSrc;
  874. filterSize= 1;
  875. filter= av_malloc(dstW*sizeof(double)*filterSize);
  876. xDstInSrc= xInc/2 - 0x8000;
  877. for(i=0; i<dstW; i++)
  878. {
  879. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  880. (*filterPos)[i]= xx;
  881. filter[i]= 1.0;
  882. xDstInSrc+= xInc;
  883. }
  884. }
  885. else if((xInc <= (1<<16) && (flags&SWS_AREA)) || (flags&SWS_FAST_BILINEAR)) // bilinear upscale
  886. {
  887. int i;
  888. int xDstInSrc;
  889. if (flags&SWS_BICUBIC) filterSize= 4;
  890. else if(flags&SWS_X ) filterSize= 4;
  891. else filterSize= 2; // SWS_BILINEAR / SWS_AREA
  892. filter= av_malloc(dstW*sizeof(double)*filterSize);
  893. xDstInSrc= xInc/2 - 0x8000;
  894. for(i=0; i<dstW; i++)
  895. {
  896. int xx= (xDstInSrc - ((filterSize-1)<<15) + (1<<15))>>16;
  897. int j;
  898. (*filterPos)[i]= xx;
  899. //Bilinear upscale / linear interpolate / Area averaging
  900. for(j=0; j<filterSize; j++)
  901. {
  902. double d= FFABS((xx<<16) - xDstInSrc)/(double)(1<<16);
  903. double coeff= 1.0 - d;
  904. if(coeff<0) coeff=0;
  905. filter[i*filterSize + j]= coeff;
  906. xx++;
  907. }
  908. xDstInSrc+= xInc;
  909. }
  910. }
  911. else
  912. {
  913. double xDstInSrc;
  914. double sizeFactor, filterSizeInSrc;
  915. const double xInc1= (double)xInc / (double)(1<<16);
  916. if (flags&SWS_BICUBIC) sizeFactor= 4.0;
  917. else if(flags&SWS_X) sizeFactor= 8.0;
  918. else if(flags&SWS_AREA) sizeFactor= 1.0; //downscale only, for upscale it is bilinear
  919. else if(flags&SWS_GAUSS) sizeFactor= 8.0; // infinite ;)
  920. else if(flags&SWS_LANCZOS) sizeFactor= param[0] != SWS_PARAM_DEFAULT ? 2.0*param[0] : 6.0;
  921. else if(flags&SWS_SINC) sizeFactor= 20.0; // infinite ;)
  922. else if(flags&SWS_SPLINE) sizeFactor= 20.0; // infinite ;)
  923. else if(flags&SWS_BILINEAR) sizeFactor= 2.0;
  924. else {
  925. sizeFactor= 0.0; //GCC warning killer
  926. ASSERT(0)
  927. }
  928. if(xInc1 <= 1.0) filterSizeInSrc= sizeFactor; // upscale
  929. else filterSizeInSrc= sizeFactor*srcW / (double)dstW;
  930. filterSize= (int)ceil(1 + filterSizeInSrc); // will be reduced later if possible
  931. if(filterSize > srcW-2) filterSize=srcW-2;
  932. filter= av_malloc(dstW*sizeof(double)*filterSize);
  933. xDstInSrc= xInc1 / 2.0 - 0.5;
  934. for(i=0; i<dstW; i++)
  935. {
  936. int xx= (int)(xDstInSrc - (filterSize-1)*0.5 + 0.5);
  937. int j;
  938. (*filterPos)[i]= xx;
  939. for(j=0; j<filterSize; j++)
  940. {
  941. double d= FFABS(xx - xDstInSrc)/filterSizeInSrc*sizeFactor;
  942. double coeff;
  943. if(flags & SWS_BICUBIC)
  944. {
  945. double B= param[0] != SWS_PARAM_DEFAULT ? param[0] : 0.0;
  946. double C= param[1] != SWS_PARAM_DEFAULT ? param[1] : 0.6;
  947. if(d<1.0)
  948. coeff = (12-9*B-6*C)*d*d*d + (-18+12*B+6*C)*d*d + 6-2*B;
  949. else if(d<2.0)
  950. coeff = (-B-6*C)*d*d*d + (6*B+30*C)*d*d + (-12*B-48*C)*d +8*B+24*C;
  951. else
  952. coeff=0.0;
  953. }
  954. /* else if(flags & SWS_X)
  955. {
  956. double p= param ? param*0.01 : 0.3;
  957. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  958. coeff*= pow(2.0, - p*d*d);
  959. }*/
  960. else if(flags & SWS_X)
  961. {
  962. double A= param[0] != SWS_PARAM_DEFAULT ? param[0] : 1.0;
  963. if(d<1.0)
  964. coeff = cos(d*PI);
  965. else
  966. coeff=-1.0;
  967. if(coeff<0.0) coeff= -pow(-coeff, A);
  968. else coeff= pow( coeff, A);
  969. coeff= coeff*0.5 + 0.5;
  970. }
  971. else if(flags & SWS_AREA)
  972. {
  973. double srcPixelSize= 1.0/xInc1;
  974. if(d + srcPixelSize/2 < 0.5) coeff= 1.0;
  975. else if(d - srcPixelSize/2 < 0.5) coeff= (0.5-d)/srcPixelSize + 0.5;
  976. else coeff=0.0;
  977. }
  978. else if(flags & SWS_GAUSS)
  979. {
  980. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  981. coeff = pow(2.0, - p*d*d);
  982. }
  983. else if(flags & SWS_SINC)
  984. {
  985. coeff = d ? sin(d*PI)/(d*PI) : 1.0;
  986. }
  987. else if(flags & SWS_LANCZOS)
  988. {
  989. double p= param[0] != SWS_PARAM_DEFAULT ? param[0] : 3.0;
  990. coeff = d ? sin(d*PI)*sin(d*PI/p)/(d*d*PI*PI/p) : 1.0;
  991. if(d>p) coeff=0;
  992. }
  993. else if(flags & SWS_BILINEAR)
  994. {
  995. coeff= 1.0 - d;
  996. if(coeff<0) coeff=0;
  997. }
  998. else if(flags & SWS_SPLINE)
  999. {
  1000. double p=-2.196152422706632;
  1001. coeff = getSplineCoeff(1.0, 0.0, p, -p-1.0, d);
  1002. }
  1003. else {
  1004. coeff= 0.0; //GCC warning killer
  1005. ASSERT(0)
  1006. }
  1007. filter[i*filterSize + j]= coeff;
  1008. xx++;
  1009. }
  1010. xDstInSrc+= xInc1;
  1011. }
  1012. }
  1013. /* apply src & dst Filter to filter -> filter2
  1014. av_free(filter);
  1015. */
  1016. ASSERT(filterSize>0)
  1017. filter2Size= filterSize;
  1018. if(srcFilter) filter2Size+= srcFilter->length - 1;
  1019. if(dstFilter) filter2Size+= dstFilter->length - 1;
  1020. ASSERT(filter2Size>0)
  1021. filter2= av_malloc(filter2Size*dstW*sizeof(double));
  1022. for(i=0; i<dstW; i++)
  1023. {
  1024. int j;
  1025. SwsVector scaleFilter;
  1026. SwsVector *outVec;
  1027. scaleFilter.coeff= filter + i*filterSize;
  1028. scaleFilter.length= filterSize;
  1029. if(srcFilter) outVec= sws_getConvVec(srcFilter, &scaleFilter);
  1030. else outVec= &scaleFilter;
  1031. ASSERT(outVec->length == filter2Size)
  1032. //FIXME dstFilter
  1033. for(j=0; j<outVec->length; j++)
  1034. {
  1035. filter2[i*filter2Size + j]= outVec->coeff[j];
  1036. }
  1037. (*filterPos)[i]+= (filterSize-1)/2 - (filter2Size-1)/2;
  1038. if(outVec != &scaleFilter) sws_freeVec(outVec);
  1039. }
  1040. av_free(filter); filter=NULL;
  1041. /* try to reduce the filter-size (step1 find size and shift left) */
  1042. // Assume its near normalized (*0.5 or *2.0 is ok but * 0.001 is not)
  1043. minFilterSize= 0;
  1044. for(i=dstW-1; i>=0; i--)
  1045. {
  1046. int min= filter2Size;
  1047. int j;
  1048. double cutOff=0.0;
  1049. /* get rid off near zero elements on the left by shifting left */
  1050. for(j=0; j<filter2Size; j++)
  1051. {
  1052. int k;
  1053. cutOff += FFABS(filter2[i*filter2Size]);
  1054. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1055. /* preserve Monotonicity because the core can't handle the filter otherwise */
  1056. if(i<dstW-1 && (*filterPos)[i] >= (*filterPos)[i+1]) break;
  1057. // Move filter coeffs left
  1058. for(k=1; k<filter2Size; k++)
  1059. filter2[i*filter2Size + k - 1]= filter2[i*filter2Size + k];
  1060. filter2[i*filter2Size + k - 1]= 0.0;
  1061. (*filterPos)[i]++;
  1062. }
  1063. cutOff=0.0;
  1064. /* count near zeros on the right */
  1065. for(j=filter2Size-1; j>0; j--)
  1066. {
  1067. cutOff += FFABS(filter2[i*filter2Size + j]);
  1068. if(cutOff > SWS_MAX_REDUCE_CUTOFF) break;
  1069. min--;
  1070. }
  1071. if(min>minFilterSize) minFilterSize= min;
  1072. }
  1073. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1074. // we can handle the special case 4,
  1075. // so we don't want to go to the full 8
  1076. if (minFilterSize < 5)
  1077. filterAlign = 4;
  1078. // we really don't want to waste our time
  1079. // doing useless computation, so fall-back on
  1080. // the scalar C code for very small filter.
  1081. // vectorizing is worth it only if you have
  1082. // decent-sized vector.
  1083. if (minFilterSize < 3)
  1084. filterAlign = 1;
  1085. }
  1086. if (flags & SWS_CPU_CAPS_MMX) {
  1087. // special case for unscaled vertical filtering
  1088. if(minFilterSize == 1 && filterAlign == 2)
  1089. filterAlign= 1;
  1090. }
  1091. ASSERT(minFilterSize > 0)
  1092. filterSize= (minFilterSize +(filterAlign-1)) & (~(filterAlign-1));
  1093. ASSERT(filterSize > 0)
  1094. filter= av_malloc(filterSize*dstW*sizeof(double));
  1095. if(filterSize >= MAX_FILTER_SIZE)
  1096. return -1;
  1097. *outFilterSize= filterSize;
  1098. if(flags&SWS_PRINT_INFO)
  1099. MSG_V("SwScaler: reducing / aligning filtersize %d -> %d\n", filter2Size, filterSize);
  1100. /* try to reduce the filter-size (step2 reduce it) */
  1101. for(i=0; i<dstW; i++)
  1102. {
  1103. int j;
  1104. for(j=0; j<filterSize; j++)
  1105. {
  1106. if(j>=filter2Size) filter[i*filterSize + j]= 0.0;
  1107. else filter[i*filterSize + j]= filter2[i*filter2Size + j];
  1108. }
  1109. }
  1110. av_free(filter2); filter2=NULL;
  1111. //FIXME try to align filterpos if possible
  1112. //fix borders
  1113. for(i=0; i<dstW; i++)
  1114. {
  1115. int j;
  1116. if((*filterPos)[i] < 0)
  1117. {
  1118. // Move filter coeffs left to compensate for filterPos
  1119. for(j=1; j<filterSize; j++)
  1120. {
  1121. int left= FFMAX(j + (*filterPos)[i], 0);
  1122. filter[i*filterSize + left] += filter[i*filterSize + j];
  1123. filter[i*filterSize + j]=0;
  1124. }
  1125. (*filterPos)[i]= 0;
  1126. }
  1127. if((*filterPos)[i] + filterSize > srcW)
  1128. {
  1129. int shift= (*filterPos)[i] + filterSize - srcW;
  1130. // Move filter coeffs right to compensate for filterPos
  1131. for(j=filterSize-2; j>=0; j--)
  1132. {
  1133. int right= FFMIN(j + shift, filterSize-1);
  1134. filter[i*filterSize +right] += filter[i*filterSize +j];
  1135. filter[i*filterSize +j]=0;
  1136. }
  1137. (*filterPos)[i]= srcW - filterSize;
  1138. }
  1139. }
  1140. // Note the +1 is for the MMXscaler which reads over the end
  1141. /* align at 16 for AltiVec (needed by hScale_altivec_real) */
  1142. *outFilter= av_malloc(*outFilterSize*(dstW+1)*sizeof(int16_t));
  1143. memset(*outFilter, 0, *outFilterSize*(dstW+1)*sizeof(int16_t));
  1144. /* Normalize & Store in outFilter */
  1145. for(i=0; i<dstW; i++)
  1146. {
  1147. int j;
  1148. double error=0;
  1149. double sum=0;
  1150. double scale= one;
  1151. for(j=0; j<filterSize; j++)
  1152. {
  1153. sum+= filter[i*filterSize + j];
  1154. }
  1155. scale/= sum;
  1156. for(j=0; j<*outFilterSize; j++)
  1157. {
  1158. double v= filter[i*filterSize + j]*scale + error;
  1159. int intV= floor(v + 0.5);
  1160. (*outFilter)[i*(*outFilterSize) + j]= intV;
  1161. error = v - intV;
  1162. }
  1163. }
  1164. (*filterPos)[dstW]= (*filterPos)[dstW-1]; // the MMX scaler will read over the end
  1165. for(i=0; i<*outFilterSize; i++)
  1166. {
  1167. int j= dstW*(*outFilterSize);
  1168. (*outFilter)[j + i]= (*outFilter)[j + i - (*outFilterSize)];
  1169. }
  1170. av_free(filter);
  1171. return 0;
  1172. }
  1173. #ifdef COMPILE_MMX2
  1174. static void initMMX2HScaler(int dstW, int xInc, uint8_t *funnyCode, int16_t *filter, int32_t *filterPos, int numSplits)
  1175. {
  1176. uint8_t *fragmentA;
  1177. long imm8OfPShufW1A;
  1178. long imm8OfPShufW2A;
  1179. long fragmentLengthA;
  1180. uint8_t *fragmentB;
  1181. long imm8OfPShufW1B;
  1182. long imm8OfPShufW2B;
  1183. long fragmentLengthB;
  1184. int fragmentPos;
  1185. int xpos, i;
  1186. // create an optimized horizontal scaling routine
  1187. //code fragment
  1188. asm volatile(
  1189. "jmp 9f \n\t"
  1190. // Begin
  1191. "0: \n\t"
  1192. "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t"
  1193. "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t"
  1194. "movd 1(%%"REG_c", %%"REG_S"), %%mm1\n\t"
  1195. "punpcklbw %%mm7, %%mm1 \n\t"
  1196. "punpcklbw %%mm7, %%mm0 \n\t"
  1197. "pshufw $0xFF, %%mm1, %%mm1 \n\t"
  1198. "1: \n\t"
  1199. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1200. "2: \n\t"
  1201. "psubw %%mm1, %%mm0 \n\t"
  1202. "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
  1203. "pmullw %%mm3, %%mm0 \n\t"
  1204. "psllw $7, %%mm1 \n\t"
  1205. "paddw %%mm1, %%mm0 \n\t"
  1206. "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
  1207. "add $8, %%"REG_a" \n\t"
  1208. // End
  1209. "9: \n\t"
  1210. // "int $3\n\t"
  1211. "lea 0b, %0 \n\t"
  1212. "lea 1b, %1 \n\t"
  1213. "lea 2b, %2 \n\t"
  1214. "dec %1 \n\t"
  1215. "dec %2 \n\t"
  1216. "sub %0, %1 \n\t"
  1217. "sub %0, %2 \n\t"
  1218. "lea 9b, %3 \n\t"
  1219. "sub %0, %3 \n\t"
  1220. :"=r" (fragmentA), "=r" (imm8OfPShufW1A), "=r" (imm8OfPShufW2A),
  1221. "=r" (fragmentLengthA)
  1222. );
  1223. asm volatile(
  1224. "jmp 9f \n\t"
  1225. // Begin
  1226. "0: \n\t"
  1227. "movq (%%"REG_d", %%"REG_a"), %%mm3\n\t"
  1228. "movd (%%"REG_c", %%"REG_S"), %%mm0\n\t"
  1229. "punpcklbw %%mm7, %%mm0 \n\t"
  1230. "pshufw $0xFF, %%mm0, %%mm1 \n\t"
  1231. "1: \n\t"
  1232. "pshufw $0xFF, %%mm0, %%mm0 \n\t"
  1233. "2: \n\t"
  1234. "psubw %%mm1, %%mm0 \n\t"
  1235. "movl 8(%%"REG_b", %%"REG_a"), %%esi\n\t"
  1236. "pmullw %%mm3, %%mm0 \n\t"
  1237. "psllw $7, %%mm1 \n\t"
  1238. "paddw %%mm1, %%mm0 \n\t"
  1239. "movq %%mm0, (%%"REG_D", %%"REG_a")\n\t"
  1240. "add $8, %%"REG_a" \n\t"
  1241. // End
  1242. "9: \n\t"
  1243. // "int $3\n\t"
  1244. "lea 0b, %0 \n\t"
  1245. "lea 1b, %1 \n\t"
  1246. "lea 2b, %2 \n\t"
  1247. "dec %1 \n\t"
  1248. "dec %2 \n\t"
  1249. "sub %0, %1 \n\t"
  1250. "sub %0, %2 \n\t"
  1251. "lea 9b, %3 \n\t"
  1252. "sub %0, %3 \n\t"
  1253. :"=r" (fragmentB), "=r" (imm8OfPShufW1B), "=r" (imm8OfPShufW2B),
  1254. "=r" (fragmentLengthB)
  1255. );
  1256. xpos= 0; //lumXInc/2 - 0x8000; // difference between pixel centers
  1257. fragmentPos=0;
  1258. for(i=0; i<dstW/numSplits; i++)
  1259. {
  1260. int xx=xpos>>16;
  1261. if((i&3) == 0)
  1262. {
  1263. int a=0;
  1264. int b=((xpos+xInc)>>16) - xx;
  1265. int c=((xpos+xInc*2)>>16) - xx;
  1266. int d=((xpos+xInc*3)>>16) - xx;
  1267. filter[i ] = (( xpos & 0xFFFF) ^ 0xFFFF)>>9;
  1268. filter[i+1] = (((xpos+xInc ) & 0xFFFF) ^ 0xFFFF)>>9;
  1269. filter[i+2] = (((xpos+xInc*2) & 0xFFFF) ^ 0xFFFF)>>9;
  1270. filter[i+3] = (((xpos+xInc*3) & 0xFFFF) ^ 0xFFFF)>>9;
  1271. filterPos[i/2]= xx;
  1272. if(d+1<4)
  1273. {
  1274. int maxShift= 3-(d+1);
  1275. int shift=0;
  1276. memcpy(funnyCode + fragmentPos, fragmentB, fragmentLengthB);
  1277. funnyCode[fragmentPos + imm8OfPShufW1B]=
  1278. (a+1) | ((b+1)<<2) | ((c+1)<<4) | ((d+1)<<6);
  1279. funnyCode[fragmentPos + imm8OfPShufW2B]=
  1280. a | (b<<2) | (c<<4) | (d<<6);
  1281. if(i+3>=dstW) shift=maxShift; //avoid overread
  1282. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //Align
  1283. if(shift && i>=shift)
  1284. {
  1285. funnyCode[fragmentPos + imm8OfPShufW1B]+= 0x55*shift;
  1286. funnyCode[fragmentPos + imm8OfPShufW2B]+= 0x55*shift;
  1287. filterPos[i/2]-=shift;
  1288. }
  1289. fragmentPos+= fragmentLengthB;
  1290. }
  1291. else
  1292. {
  1293. int maxShift= 3-d;
  1294. int shift=0;
  1295. memcpy(funnyCode + fragmentPos, fragmentA, fragmentLengthA);
  1296. funnyCode[fragmentPos + imm8OfPShufW1A]=
  1297. funnyCode[fragmentPos + imm8OfPShufW2A]=
  1298. a | (b<<2) | (c<<4) | (d<<6);
  1299. if(i+4>=dstW) shift=maxShift; //avoid overread
  1300. else if((filterPos[i/2]&3) <= maxShift) shift=filterPos[i/2]&3; //partial align
  1301. if(shift && i>=shift)
  1302. {
  1303. funnyCode[fragmentPos + imm8OfPShufW1A]+= 0x55*shift;
  1304. funnyCode[fragmentPos + imm8OfPShufW2A]+= 0x55*shift;
  1305. filterPos[i/2]-=shift;
  1306. }
  1307. fragmentPos+= fragmentLengthA;
  1308. }
  1309. funnyCode[fragmentPos]= RET;
  1310. }
  1311. xpos+=xInc;
  1312. }
  1313. filterPos[i/2]= xpos>>16; // needed to jump to the next part
  1314. }
  1315. #endif /* COMPILE_MMX2 */
  1316. static void globalInit(void){
  1317. // generating tables:
  1318. int i;
  1319. for(i=0; i<768; i++){
  1320. int c= FFMIN(FFMAX(i-256, 0), 255);
  1321. clip_table[i]=c;
  1322. }
  1323. }
  1324. static SwsFunc getSwsFunc(int flags){
  1325. #ifdef RUNTIME_CPUDETECT
  1326. #if defined(ARCH_X86)
  1327. // ordered per speed fasterst first
  1328. if(flags & SWS_CPU_CAPS_MMX2)
  1329. return swScale_MMX2;
  1330. else if(flags & SWS_CPU_CAPS_3DNOW)
  1331. return swScale_3DNow;
  1332. else if(flags & SWS_CPU_CAPS_MMX)
  1333. return swScale_MMX;
  1334. else
  1335. return swScale_C;
  1336. #else
  1337. #ifdef ARCH_POWERPC
  1338. if(flags & SWS_CPU_CAPS_ALTIVEC)
  1339. return swScale_altivec;
  1340. else
  1341. return swScale_C;
  1342. #endif
  1343. return swScale_C;
  1344. #endif /* defined(ARCH_X86) */
  1345. #else //RUNTIME_CPUDETECT
  1346. #ifdef HAVE_MMX2
  1347. return swScale_MMX2;
  1348. #elif defined (HAVE_3DNOW)
  1349. return swScale_3DNow;
  1350. #elif defined (HAVE_MMX)
  1351. return swScale_MMX;
  1352. #elif defined (HAVE_ALTIVEC)
  1353. return swScale_altivec;
  1354. #else
  1355. return swScale_C;
  1356. #endif
  1357. #endif //!RUNTIME_CPUDETECT
  1358. }
  1359. static int PlanarToNV12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1360. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1361. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1362. /* Copy Y plane */
  1363. if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1364. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1365. else
  1366. {
  1367. int i;
  1368. uint8_t *srcPtr= src[0];
  1369. uint8_t *dstPtr= dst;
  1370. for(i=0; i<srcSliceH; i++)
  1371. {
  1372. memcpy(dstPtr, srcPtr, c->srcW);
  1373. srcPtr+= srcStride[0];
  1374. dstPtr+= dstStride[0];
  1375. }
  1376. }
  1377. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1378. if (c->dstFormat == PIX_FMT_NV12)
  1379. interleaveBytes( src[1],src[2],dst,c->srcW/2,srcSliceH/2,srcStride[1],srcStride[2],dstStride[0] );
  1380. else
  1381. interleaveBytes( src[2],src[1],dst,c->srcW/2,srcSliceH/2,srcStride[2],srcStride[1],dstStride[0] );
  1382. return srcSliceH;
  1383. }
  1384. static int PlanarToYuy2Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1385. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1386. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1387. yv12toyuy2( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1388. return srcSliceH;
  1389. }
  1390. static int PlanarToUyvyWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1391. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  1392. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1393. yv12touyvy( src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0] );
  1394. return srcSliceH;
  1395. }
  1396. /* {RGB,BGR}{15,16,24,32} -> {RGB,BGR}{15,16,24,32} */
  1397. static int rgb2rgbWrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1398. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1399. const int srcFormat= c->srcFormat;
  1400. const int dstFormat= c->dstFormat;
  1401. const int srcBpp= (fmt_depth(srcFormat) + 7) >> 3;
  1402. const int dstBpp= (fmt_depth(dstFormat) + 7) >> 3;
  1403. const int srcId= fmt_depth(srcFormat) >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1404. const int dstId= fmt_depth(dstFormat) >> 2;
  1405. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1406. /* BGR -> BGR */
  1407. if( (isBGR(srcFormat) && isBGR(dstFormat))
  1408. || (isRGB(srcFormat) && isRGB(dstFormat))){
  1409. switch(srcId | (dstId<<4)){
  1410. case 0x34: conv= rgb16to15; break;
  1411. case 0x36: conv= rgb24to15; break;
  1412. case 0x38: conv= rgb32to15; break;
  1413. case 0x43: conv= rgb15to16; break;
  1414. case 0x46: conv= rgb24to16; break;
  1415. case 0x48: conv= rgb32to16; break;
  1416. case 0x63: conv= rgb15to24; break;
  1417. case 0x64: conv= rgb16to24; break;
  1418. case 0x68: conv= rgb32to24; break;
  1419. case 0x83: conv= rgb15to32; break;
  1420. case 0x84: conv= rgb16to32; break;
  1421. case 0x86: conv= rgb24to32; break;
  1422. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1423. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1424. }
  1425. }else if( (isBGR(srcFormat) && isRGB(dstFormat))
  1426. || (isRGB(srcFormat) && isBGR(dstFormat))){
  1427. switch(srcId | (dstId<<4)){
  1428. case 0x33: conv= rgb15tobgr15; break;
  1429. case 0x34: conv= rgb16tobgr15; break;
  1430. case 0x36: conv= rgb24tobgr15; break;
  1431. case 0x38: conv= rgb32tobgr15; break;
  1432. case 0x43: conv= rgb15tobgr16; break;
  1433. case 0x44: conv= rgb16tobgr16; break;
  1434. case 0x46: conv= rgb24tobgr16; break;
  1435. case 0x48: conv= rgb32tobgr16; break;
  1436. case 0x63: conv= rgb15tobgr24; break;
  1437. case 0x64: conv= rgb16tobgr24; break;
  1438. case 0x66: conv= rgb24tobgr24; break;
  1439. case 0x68: conv= rgb32tobgr24; break;
  1440. case 0x83: conv= rgb15tobgr32; break;
  1441. case 0x84: conv= rgb16tobgr32; break;
  1442. case 0x86: conv= rgb24tobgr32; break;
  1443. case 0x88: conv= rgb32tobgr32; break;
  1444. default: MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1445. sws_format_name(srcFormat), sws_format_name(dstFormat)); break;
  1446. }
  1447. }else{
  1448. MSG_ERR("swScaler: internal error %s -> %s converter\n",
  1449. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1450. }
  1451. if(dstStride[0]*srcBpp == srcStride[0]*dstBpp)
  1452. conv(src[0], dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1453. else
  1454. {
  1455. int i;
  1456. uint8_t *srcPtr= src[0];
  1457. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1458. for(i=0; i<srcSliceH; i++)
  1459. {
  1460. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1461. srcPtr+= srcStride[0];
  1462. dstPtr+= dstStride[0];
  1463. }
  1464. }
  1465. return srcSliceH;
  1466. }
  1467. static int bgr24toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1468. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1469. rgb24toyv12(
  1470. src[0],
  1471. dst[0]+ srcSliceY *dstStride[0],
  1472. dst[1]+(srcSliceY>>1)*dstStride[1],
  1473. dst[2]+(srcSliceY>>1)*dstStride[2],
  1474. c->srcW, srcSliceH,
  1475. dstStride[0], dstStride[1], srcStride[0]);
  1476. return srcSliceH;
  1477. }
  1478. static int yvu9toyv12Wrapper(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1479. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1480. int i;
  1481. /* copy Y */
  1482. if(srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1483. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1484. else{
  1485. uint8_t *srcPtr= src[0];
  1486. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1487. for(i=0; i<srcSliceH; i++)
  1488. {
  1489. memcpy(dstPtr, srcPtr, c->srcW);
  1490. srcPtr+= srcStride[0];
  1491. dstPtr+= dstStride[0];
  1492. }
  1493. }
  1494. if(c->dstFormat==PIX_FMT_YUV420P){
  1495. planar2x(src[1], dst[1], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[1]);
  1496. planar2x(src[2], dst[2], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[2]);
  1497. }else{
  1498. planar2x(src[1], dst[2], c->chrSrcW, c->chrSrcH, srcStride[1], dstStride[2]);
  1499. planar2x(src[2], dst[1], c->chrSrcW, c->chrSrcH, srcStride[2], dstStride[1]);
  1500. }
  1501. return srcSliceH;
  1502. }
  1503. /* unscaled copy like stuff (assumes nearly identical formats) */
  1504. static int simpleCopy(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1505. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1506. if(isPacked(c->srcFormat))
  1507. {
  1508. if(dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1509. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1510. else
  1511. {
  1512. int i;
  1513. uint8_t *srcPtr= src[0];
  1514. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1515. int length=0;
  1516. /* universal length finder */
  1517. while(length+c->srcW <= FFABS(dstStride[0])
  1518. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1519. ASSERT(length!=0);
  1520. for(i=0; i<srcSliceH; i++)
  1521. {
  1522. memcpy(dstPtr, srcPtr, length);
  1523. srcPtr+= srcStride[0];
  1524. dstPtr+= dstStride[0];
  1525. }
  1526. }
  1527. }
  1528. else
  1529. { /* Planar YUV or gray */
  1530. int plane;
  1531. for(plane=0; plane<3; plane++)
  1532. {
  1533. int length= plane==0 ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1534. int y= plane==0 ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1535. int height= plane==0 ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1536. if((isGray(c->srcFormat) || isGray(c->dstFormat)) && plane>0)
  1537. {
  1538. if(!isGray(c->dstFormat))
  1539. memset(dst[plane], 128, dstStride[plane]*height);
  1540. }
  1541. else
  1542. {
  1543. if(dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1544. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1545. else
  1546. {
  1547. int i;
  1548. uint8_t *srcPtr= src[plane];
  1549. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1550. for(i=0; i<height; i++)
  1551. {
  1552. memcpy(dstPtr, srcPtr, length);
  1553. srcPtr+= srcStride[plane];
  1554. dstPtr+= dstStride[plane];
  1555. }
  1556. }
  1557. }
  1558. }
  1559. }
  1560. return srcSliceH;
  1561. }
  1562. static int gray16togray(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1563. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1564. int length= c->srcW;
  1565. int y= srcSliceY;
  1566. int height= srcSliceH;
  1567. int i, j;
  1568. uint8_t *srcPtr= src[0];
  1569. uint8_t *dstPtr= dst[0] + dstStride[0]*y;
  1570. if(!isGray(c->dstFormat)){
  1571. int height= -((-srcSliceH)>>c->chrDstVSubSample);
  1572. memset(dst[1], 128, dstStride[1]*height);
  1573. memset(dst[2], 128, dstStride[2]*height);
  1574. }
  1575. if(c->srcFormat == PIX_FMT_GRAY16LE) srcPtr++;
  1576. for(i=0; i<height; i++)
  1577. {
  1578. for(j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1579. srcPtr+= srcStride[0];
  1580. dstPtr+= dstStride[0];
  1581. }
  1582. return srcSliceH;
  1583. }
  1584. static int graytogray16(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1585. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1586. int length= c->srcW;
  1587. int y= srcSliceY;
  1588. int height= srcSliceH;
  1589. int i, j;
  1590. uint8_t *srcPtr= src[0];
  1591. uint8_t *dstPtr= dst[0] + dstStride[0]*y;
  1592. for(i=0; i<height; i++)
  1593. {
  1594. for(j=0; j<length; j++)
  1595. {
  1596. dstPtr[j<<1] = srcPtr[j];
  1597. dstPtr[(j<<1)+1] = srcPtr[j];
  1598. }
  1599. srcPtr+= srcStride[0];
  1600. dstPtr+= dstStride[0];
  1601. }
  1602. return srcSliceH;
  1603. }
  1604. static int gray16swap(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  1605. int srcSliceH, uint8_t* dst[], int dstStride[]){
  1606. int length= c->srcW;
  1607. int y= srcSliceY;
  1608. int height= srcSliceH;
  1609. int i, j;
  1610. uint16_t *srcPtr= src[0];
  1611. uint16_t *dstPtr= dst[0] + dstStride[0]*y/2;
  1612. for(i=0; i<height; i++)
  1613. {
  1614. for(j=0; j<length; j++) dstPtr[j] = bswap_16(srcPtr[j]);
  1615. srcPtr+= srcStride[0]/2;
  1616. dstPtr+= dstStride[0]/2;
  1617. }
  1618. return srcSliceH;
  1619. }
  1620. static void getSubSampleFactors(int *h, int *v, int format){
  1621. switch(format){
  1622. case PIX_FMT_UYVY422:
  1623. case PIX_FMT_YUYV422:
  1624. *h=1;
  1625. *v=0;
  1626. break;
  1627. case PIX_FMT_YUV420P:
  1628. case PIX_FMT_GRAY16BE:
  1629. case PIX_FMT_GRAY16LE:
  1630. case PIX_FMT_GRAY8: //FIXME remove after different subsamplings are fully implemented
  1631. case PIX_FMT_NV12:
  1632. case PIX_FMT_NV21:
  1633. *h=1;
  1634. *v=1;
  1635. break;
  1636. case PIX_FMT_YUV410P:
  1637. *h=2;
  1638. *v=2;
  1639. break;
  1640. case PIX_FMT_YUV444P:
  1641. *h=0;
  1642. *v=0;
  1643. break;
  1644. case PIX_FMT_YUV422P:
  1645. *h=1;
  1646. *v=0;
  1647. break;
  1648. case PIX_FMT_YUV411P:
  1649. *h=2;
  1650. *v=0;
  1651. break;
  1652. default:
  1653. *h=0;
  1654. *v=0;
  1655. break;
  1656. }
  1657. }
  1658. static uint16_t roundToInt16(int64_t f){
  1659. int r= (f + (1<<15))>>16;
  1660. if(r<-0x7FFF) return 0x8000;
  1661. else if(r> 0x7FFF) return 0x7FFF;
  1662. else return r;
  1663. }
  1664. /**
  1665. * @param inv_table the yuv2rgb coeffs, normally Inverse_Table_6_9[x]
  1666. * @param fullRange if 1 then the luma range is 0..255 if 0 its 16..235
  1667. * @return -1 if not supported
  1668. */
  1669. int sws_setColorspaceDetails(SwsContext *c, const int inv_table[4], int srcRange, const int table[4], int dstRange, int brightness, int contrast, int saturation){
  1670. int64_t crv = inv_table[0];
  1671. int64_t cbu = inv_table[1];
  1672. int64_t cgu = -inv_table[2];
  1673. int64_t cgv = -inv_table[3];
  1674. int64_t cy = 1<<16;
  1675. int64_t oy = 0;
  1676. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1677. memcpy(c->srcColorspaceTable, inv_table, sizeof(int)*4);
  1678. memcpy(c->dstColorspaceTable, table, sizeof(int)*4);
  1679. c->brightness= brightness;
  1680. c->contrast = contrast;
  1681. c->saturation= saturation;
  1682. c->srcRange = srcRange;
  1683. c->dstRange = dstRange;
  1684. c->uOffset= 0x0400040004000400LL;
  1685. c->vOffset= 0x0400040004000400LL;
  1686. if(!srcRange){
  1687. cy= (cy*255) / 219;
  1688. oy= 16<<16;
  1689. }
  1690. cy = (cy *contrast )>>16;
  1691. crv= (crv*contrast * saturation)>>32;
  1692. cbu= (cbu*contrast * saturation)>>32;
  1693. cgu= (cgu*contrast * saturation)>>32;
  1694. cgv= (cgv*contrast * saturation)>>32;
  1695. oy -= 256*brightness;
  1696. c->yCoeff= roundToInt16(cy *8192) * 0x0001000100010001ULL;
  1697. c->vrCoeff= roundToInt16(crv*8192) * 0x0001000100010001ULL;
  1698. c->ubCoeff= roundToInt16(cbu*8192) * 0x0001000100010001ULL;
  1699. c->vgCoeff= roundToInt16(cgv*8192) * 0x0001000100010001ULL;
  1700. c->ugCoeff= roundToInt16(cgu*8192) * 0x0001000100010001ULL;
  1701. c->yOffset= roundToInt16(oy * 8) * 0x0001000100010001ULL;
  1702. yuv2rgb_c_init_tables(c, inv_table, srcRange, brightness, contrast, saturation);
  1703. //FIXME factorize
  1704. #ifdef COMPILE_ALTIVEC
  1705. if (c->flags & SWS_CPU_CAPS_ALTIVEC)
  1706. yuv2rgb_altivec_init_tables (c, inv_table, brightness, contrast, saturation);
  1707. #endif
  1708. return 0;
  1709. }
  1710. /**
  1711. * @return -1 if not supported
  1712. */
  1713. int sws_getColorspaceDetails(SwsContext *c, int **inv_table, int *srcRange, int **table, int *dstRange, int *brightness, int *contrast, int *saturation){
  1714. if(isYUV(c->dstFormat) || isGray(c->dstFormat)) return -1;
  1715. *inv_table = c->srcColorspaceTable;
  1716. *table = c->dstColorspaceTable;
  1717. *srcRange = c->srcRange;
  1718. *dstRange = c->dstRange;
  1719. *brightness= c->brightness;
  1720. *contrast = c->contrast;
  1721. *saturation= c->saturation;
  1722. return 0;
  1723. }
  1724. static int handle_jpeg(int *format)
  1725. {
  1726. switch (*format) {
  1727. case PIX_FMT_YUVJ420P:
  1728. *format = PIX_FMT_YUV420P;
  1729. return 1;
  1730. case PIX_FMT_YUVJ422P:
  1731. *format = PIX_FMT_YUV422P;
  1732. return 1;
  1733. case PIX_FMT_YUVJ444P:
  1734. *format = PIX_FMT_YUV444P;
  1735. return 1;
  1736. default:
  1737. return 0;
  1738. }
  1739. }
  1740. SwsContext *sws_getContext(int srcW, int srcH, int srcFormat, int dstW, int dstH, int dstFormat, int flags,
  1741. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param){
  1742. SwsContext *c;
  1743. int i;
  1744. int usesVFilter, usesHFilter;
  1745. int unscaled, needsDither;
  1746. int srcRange, dstRange;
  1747. SwsFilter dummyFilter= {NULL, NULL, NULL, NULL};
  1748. #if defined(ARCH_X86)
  1749. if(flags & SWS_CPU_CAPS_MMX)
  1750. asm volatile("emms\n\t"::: "memory");
  1751. #endif
  1752. #ifndef RUNTIME_CPUDETECT //ensure that the flags match the compiled variant if cpudetect is off
  1753. flags &= ~(SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2|SWS_CPU_CAPS_3DNOW|SWS_CPU_CAPS_ALTIVEC);
  1754. #ifdef HAVE_MMX2
  1755. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1756. #elif defined (HAVE_3DNOW)
  1757. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1758. #elif defined (HAVE_MMX)
  1759. flags |= SWS_CPU_CAPS_MMX;
  1760. #elif defined (HAVE_ALTIVEC)
  1761. flags |= SWS_CPU_CAPS_ALTIVEC;
  1762. #endif
  1763. #endif /* RUNTIME_CPUDETECT */
  1764. if(clip_table[512] != 255) globalInit();
  1765. if(rgb15to16 == NULL) sws_rgb2rgb_init(flags);
  1766. unscaled = (srcW == dstW && srcH == dstH);
  1767. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1768. && (fmt_depth(dstFormat))<24
  1769. && ((fmt_depth(dstFormat))<(fmt_depth(srcFormat)) || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1770. srcRange = handle_jpeg(&srcFormat);
  1771. dstRange = handle_jpeg(&dstFormat);
  1772. if(!isSupportedIn(srcFormat))
  1773. {
  1774. MSG_ERR("swScaler: %s is not supported as input format\n", sws_format_name(srcFormat));
  1775. return NULL;
  1776. }
  1777. if(!isSupportedOut(dstFormat))
  1778. {
  1779. MSG_ERR("swScaler: %s is not supported as output format\n", sws_format_name(dstFormat));
  1780. return NULL;
  1781. }
  1782. /* sanity check */
  1783. if(srcW<4 || srcH<1 || dstW<8 || dstH<1) //FIXME check if these are enough and try to lowwer them after fixing the relevant parts of the code
  1784. {
  1785. MSG_ERR("swScaler: %dx%d -> %dx%d is invalid scaling dimension\n",
  1786. srcW, srcH, dstW, dstH);
  1787. return NULL;
  1788. }
  1789. if(!dstFilter) dstFilter= &dummyFilter;
  1790. if(!srcFilter) srcFilter= &dummyFilter;
  1791. c= av_malloc(sizeof(SwsContext));
  1792. memset(c, 0, sizeof(SwsContext));
  1793. c->srcW= srcW;
  1794. c->srcH= srcH;
  1795. c->dstW= dstW;
  1796. c->dstH= dstH;
  1797. c->lumXInc= ((srcW<<16) + (dstW>>1))/dstW;
  1798. c->lumYInc= ((srcH<<16) + (dstH>>1))/dstH;
  1799. c->flags= flags;
  1800. c->dstFormat= dstFormat;
  1801. c->srcFormat= srcFormat;
  1802. c->vRounder= 4* 0x0001000100010001ULL;
  1803. usesHFilter= usesVFilter= 0;
  1804. if(dstFilter->lumV!=NULL && dstFilter->lumV->length>1) usesVFilter=1;
  1805. if(dstFilter->lumH!=NULL && dstFilter->lumH->length>1) usesHFilter=1;
  1806. if(dstFilter->chrV!=NULL && dstFilter->chrV->length>1) usesVFilter=1;
  1807. if(dstFilter->chrH!=NULL && dstFilter->chrH->length>1) usesHFilter=1;
  1808. if(srcFilter->lumV!=NULL && srcFilter->lumV->length>1) usesVFilter=1;
  1809. if(srcFilter->lumH!=NULL && srcFilter->lumH->length>1) usesHFilter=1;
  1810. if(srcFilter->chrV!=NULL && srcFilter->chrV->length>1) usesVFilter=1;
  1811. if(srcFilter->chrH!=NULL && srcFilter->chrH->length>1) usesHFilter=1;
  1812. getSubSampleFactors(&c->chrSrcHSubSample, &c->chrSrcVSubSample, srcFormat);
  1813. getSubSampleFactors(&c->chrDstHSubSample, &c->chrDstVSubSample, dstFormat);
  1814. // reuse chroma for 2 pixles rgb/bgr unless user wants full chroma interpolation
  1815. if((isBGR(dstFormat) || isRGB(dstFormat)) && !(flags&SWS_FULL_CHR_H_INT)) c->chrDstHSubSample=1;
  1816. // drop some chroma lines if the user wants it
  1817. c->vChrDrop= (flags&SWS_SRC_V_CHR_DROP_MASK)>>SWS_SRC_V_CHR_DROP_SHIFT;
  1818. c->chrSrcVSubSample+= c->vChrDrop;
  1819. // drop every 2. pixel for chroma calculation unless user wants full chroma
  1820. if((isBGR(srcFormat) || isRGB(srcFormat)) && !(flags&SWS_FULL_CHR_H_INP))
  1821. c->chrSrcHSubSample=1;
  1822. if(param){
  1823. c->param[0] = param[0];
  1824. c->param[1] = param[1];
  1825. }else{
  1826. c->param[0] =
  1827. c->param[1] = SWS_PARAM_DEFAULT;
  1828. }
  1829. c->chrIntHSubSample= c->chrDstHSubSample;
  1830. c->chrIntVSubSample= c->chrSrcVSubSample;
  1831. // note the -((-x)>>y) is so that we allways round toward +inf
  1832. c->chrSrcW= -((-srcW) >> c->chrSrcHSubSample);
  1833. c->chrSrcH= -((-srcH) >> c->chrSrcVSubSample);
  1834. c->chrDstW= -((-dstW) >> c->chrDstHSubSample);
  1835. c->chrDstH= -((-dstH) >> c->chrDstVSubSample);
  1836. sws_setColorspaceDetails(c, Inverse_Table_6_9[SWS_CS_DEFAULT], srcRange, Inverse_Table_6_9[SWS_CS_DEFAULT] /* FIXME*/, dstRange, 0, 1<<16, 1<<16);
  1837. /* unscaled special Cases */
  1838. if(unscaled && !usesHFilter && !usesVFilter)
  1839. {
  1840. /* yv12_to_nv12 */
  1841. if(srcFormat == PIX_FMT_YUV420P && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21))
  1842. {
  1843. c->swScale= PlanarToNV12Wrapper;
  1844. }
  1845. /* yuv2bgr */
  1846. if((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P) && (isBGR(dstFormat) || isRGB(dstFormat)))
  1847. {
  1848. c->swScale= yuv2rgb_get_func_ptr(c);
  1849. }
  1850. if( srcFormat==PIX_FMT_YUV410P && dstFormat==PIX_FMT_YUV420P )
  1851. {
  1852. c->swScale= yvu9toyv12Wrapper;
  1853. }
  1854. /* bgr24toYV12 */
  1855. if(srcFormat==PIX_FMT_BGR24 && dstFormat==PIX_FMT_YUV420P)
  1856. c->swScale= bgr24toyv12Wrapper;
  1857. /* rgb/bgr -> rgb/bgr (no dither needed forms) */
  1858. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1859. && (isBGR(dstFormat) || isRGB(dstFormat))
  1860. && !needsDither)
  1861. c->swScale= rgb2rgbWrapper;
  1862. /* LQ converters if -sws 0 or -sws 4*/
  1863. if(c->flags&(SWS_FAST_BILINEAR|SWS_POINT)){
  1864. /* rgb/bgr -> rgb/bgr (dither needed forms) */
  1865. if( (isBGR(srcFormat) || isRGB(srcFormat))
  1866. && (isBGR(dstFormat) || isRGB(dstFormat))
  1867. && needsDither)
  1868. c->swScale= rgb2rgbWrapper;
  1869. /* yv12_to_yuy2 */
  1870. if(srcFormat == PIX_FMT_YUV420P &&
  1871. (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422))
  1872. {
  1873. if (dstFormat == PIX_FMT_YUYV422)
  1874. c->swScale= PlanarToYuy2Wrapper;
  1875. else
  1876. c->swScale= PlanarToUyvyWrapper;
  1877. }
  1878. }
  1879. #ifdef COMPILE_ALTIVEC
  1880. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1881. ((srcFormat == PIX_FMT_YUV420P &&
  1882. (dstFormat == PIX_FMT_YUYV422 || dstFormat == PIX_FMT_UYVY422)))) {
  1883. // unscaled YV12 -> packed YUV, we want speed
  1884. if (dstFormat == PIX_FMT_YUYV422)
  1885. c->swScale= yv12toyuy2_unscaled_altivec;
  1886. else
  1887. c->swScale= yv12touyvy_unscaled_altivec;
  1888. }
  1889. #endif
  1890. /* simple copy */
  1891. if( srcFormat == dstFormat
  1892. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1893. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1894. )
  1895. {
  1896. c->swScale= simpleCopy;
  1897. }
  1898. /* gray16{le,be} conversions */
  1899. if(isGray16(srcFormat) && (isPlanarYUV(dstFormat) || (dstFormat == PIX_FMT_GRAY8)))
  1900. {
  1901. c->swScale= gray16togray;
  1902. }
  1903. if((isPlanarYUV(srcFormat) || (srcFormat == PIX_FMT_GRAY8)) && isGray16(dstFormat))
  1904. {
  1905. c->swScale= graytogray16;
  1906. }
  1907. if(srcFormat != dstFormat && isGray16(srcFormat) && isGray16(dstFormat))
  1908. {
  1909. c->swScale= gray16swap;
  1910. }
  1911. if(c->swScale){
  1912. if(flags&SWS_PRINT_INFO)
  1913. MSG_INFO("SwScaler: using unscaled %s -> %s special converter\n",
  1914. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1915. return c;
  1916. }
  1917. }
  1918. if(flags & SWS_CPU_CAPS_MMX2)
  1919. {
  1920. c->canMMX2BeUsed= (dstW >=srcW && (dstW&31)==0 && (srcW&15)==0) ? 1 : 0;
  1921. if(!c->canMMX2BeUsed && dstW >=srcW && (srcW&15)==0 && (flags&SWS_FAST_BILINEAR))
  1922. {
  1923. if(flags&SWS_PRINT_INFO)
  1924. MSG_INFO("SwScaler: output Width is not a multiple of 32 -> no MMX2 scaler\n");
  1925. }
  1926. if(usesHFilter) c->canMMX2BeUsed=0;
  1927. }
  1928. else
  1929. c->canMMX2BeUsed=0;
  1930. c->chrXInc= ((c->chrSrcW<<16) + (c->chrDstW>>1))/c->chrDstW;
  1931. c->chrYInc= ((c->chrSrcH<<16) + (c->chrDstH>>1))/c->chrDstH;
  1932. // match pixel 0 of the src to pixel 0 of dst and match pixel n-2 of src to pixel n-2 of dst
  1933. // but only for the FAST_BILINEAR mode otherwise do correct scaling
  1934. // n-2 is the last chrominance sample available
  1935. // this is not perfect, but noone shuld notice the difference, the more correct variant
  1936. // would be like the vertical one, but that would require some special code for the
  1937. // first and last pixel
  1938. if(flags&SWS_FAST_BILINEAR)
  1939. {
  1940. if(c->canMMX2BeUsed)
  1941. {
  1942. c->lumXInc+= 20;
  1943. c->chrXInc+= 20;
  1944. }
  1945. //we don't use the x86asm scaler if mmx is available
  1946. else if(flags & SWS_CPU_CAPS_MMX)
  1947. {
  1948. c->lumXInc = ((srcW-2)<<16)/(dstW-2) - 20;
  1949. c->chrXInc = ((c->chrSrcW-2)<<16)/(c->chrDstW-2) - 20;
  1950. }
  1951. }
  1952. /* precalculate horizontal scaler filter coefficients */
  1953. {
  1954. const int filterAlign=
  1955. (flags & SWS_CPU_CAPS_MMX) ? 4 :
  1956. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  1957. 1;
  1958. initFilter(&c->hLumFilter, &c->hLumFilterPos, &c->hLumFilterSize, c->lumXInc,
  1959. srcW , dstW, filterAlign, 1<<14,
  1960. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1961. srcFilter->lumH, dstFilter->lumH, c->param);
  1962. initFilter(&c->hChrFilter, &c->hChrFilterPos, &c->hChrFilterSize, c->chrXInc,
  1963. c->chrSrcW, c->chrDstW, filterAlign, 1<<14,
  1964. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  1965. srcFilter->chrH, dstFilter->chrH, c->param);
  1966. #define MAX_FUNNY_CODE_SIZE 10000
  1967. #if defined(COMPILE_MMX2)
  1968. // can't downscale !!!
  1969. if(c->canMMX2BeUsed && (flags & SWS_FAST_BILINEAR))
  1970. {
  1971. #ifdef MAP_ANONYMOUS
  1972. c->funnyYCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  1973. c->funnyUVCode = (uint8_t*)mmap(NULL, MAX_FUNNY_CODE_SIZE, PROT_EXEC | PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  1974. #else
  1975. c->funnyYCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  1976. c->funnyUVCode = av_malloc(MAX_FUNNY_CODE_SIZE);
  1977. #endif
  1978. c->lumMmx2Filter = av_malloc((dstW /8+8)*sizeof(int16_t));
  1979. c->chrMmx2Filter = av_malloc((c->chrDstW /4+8)*sizeof(int16_t));
  1980. c->lumMmx2FilterPos= av_malloc((dstW /2/8+8)*sizeof(int32_t));
  1981. c->chrMmx2FilterPos= av_malloc((c->chrDstW/2/4+8)*sizeof(int32_t));
  1982. initMMX2HScaler( dstW, c->lumXInc, c->funnyYCode , c->lumMmx2Filter, c->lumMmx2FilterPos, 8);
  1983. initMMX2HScaler(c->chrDstW, c->chrXInc, c->funnyUVCode, c->chrMmx2Filter, c->chrMmx2FilterPos, 4);
  1984. }
  1985. #endif /* defined(COMPILE_MMX2) */
  1986. } // Init Horizontal stuff
  1987. /* precalculate vertical scaler filter coefficients */
  1988. {
  1989. const int filterAlign=
  1990. (flags & SWS_CPU_CAPS_MMX) && (flags & SWS_ACCURATE_RND) ? 2 :
  1991. (flags & SWS_CPU_CAPS_ALTIVEC) ? 8 :
  1992. 1;
  1993. initFilter(&c->vLumFilter, &c->vLumFilterPos, &c->vLumFilterSize, c->lumYInc,
  1994. srcH , dstH, filterAlign, (1<<12)-4,
  1995. (flags&SWS_BICUBLIN) ? (flags|SWS_BICUBIC) : flags,
  1996. srcFilter->lumV, dstFilter->lumV, c->param);
  1997. initFilter(&c->vChrFilter, &c->vChrFilterPos, &c->vChrFilterSize, c->chrYInc,
  1998. c->chrSrcH, c->chrDstH, filterAlign, (1<<12)-4,
  1999. (flags&SWS_BICUBLIN) ? (flags|SWS_BILINEAR) : flags,
  2000. srcFilter->chrV, dstFilter->chrV, c->param);
  2001. #ifdef HAVE_ALTIVEC
  2002. c->vYCoeffsBank = av_malloc(sizeof (vector signed short)*c->vLumFilterSize*c->dstH);
  2003. c->vCCoeffsBank = av_malloc(sizeof (vector signed short)*c->vChrFilterSize*c->chrDstH);
  2004. for (i=0;i<c->vLumFilterSize*c->dstH;i++) {
  2005. int j;
  2006. short *p = (short *)&c->vYCoeffsBank[i];
  2007. for (j=0;j<8;j++)
  2008. p[j] = c->vLumFilter[i];
  2009. }
  2010. for (i=0;i<c->vChrFilterSize*c->chrDstH;i++) {
  2011. int j;
  2012. short *p = (short *)&c->vCCoeffsBank[i];
  2013. for (j=0;j<8;j++)
  2014. p[j] = c->vChrFilter[i];
  2015. }
  2016. #endif
  2017. }
  2018. // Calculate Buffer Sizes so that they won't run out while handling these damn slices
  2019. c->vLumBufSize= c->vLumFilterSize;
  2020. c->vChrBufSize= c->vChrFilterSize;
  2021. for(i=0; i<dstH; i++)
  2022. {
  2023. int chrI= i*c->chrDstH / dstH;
  2024. int nextSlice= FFMAX(c->vLumFilterPos[i ] + c->vLumFilterSize - 1,
  2025. ((c->vChrFilterPos[chrI] + c->vChrFilterSize - 1)<<c->chrSrcVSubSample));
  2026. nextSlice>>= c->chrSrcVSubSample;
  2027. nextSlice<<= c->chrSrcVSubSample;
  2028. if(c->vLumFilterPos[i ] + c->vLumBufSize < nextSlice)
  2029. c->vLumBufSize= nextSlice - c->vLumFilterPos[i ];
  2030. if(c->vChrFilterPos[chrI] + c->vChrBufSize < (nextSlice>>c->chrSrcVSubSample))
  2031. c->vChrBufSize= (nextSlice>>c->chrSrcVSubSample) - c->vChrFilterPos[chrI];
  2032. }
  2033. // allocate pixbufs (we use dynamic allocation because otherwise we would need to
  2034. c->lumPixBuf= av_malloc(c->vLumBufSize*2*sizeof(int16_t*));
  2035. c->chrPixBuf= av_malloc(c->vChrBufSize*2*sizeof(int16_t*));
  2036. //Note we need at least one pixel more at the end because of the mmx code (just in case someone wanna replace the 4000/8000)
  2037. /* align at 16 bytes for AltiVec */
  2038. for(i=0; i<c->vLumBufSize; i++)
  2039. c->lumPixBuf[i]= c->lumPixBuf[i+c->vLumBufSize]= av_malloc(4000);
  2040. for(i=0; i<c->vChrBufSize; i++)
  2041. c->chrPixBuf[i]= c->chrPixBuf[i+c->vChrBufSize]= av_malloc(8000);
  2042. //try to avoid drawing green stuff between the right end and the stride end
  2043. for(i=0; i<c->vLumBufSize; i++) memset(c->lumPixBuf[i], 0, 4000);
  2044. for(i=0; i<c->vChrBufSize; i++) memset(c->chrPixBuf[i], 64, 8000);
  2045. ASSERT(c->chrDstH <= dstH)
  2046. if(flags&SWS_PRINT_INFO)
  2047. {
  2048. #ifdef DITHER1XBPP
  2049. char *dither= " dithered";
  2050. #else
  2051. char *dither= "";
  2052. #endif
  2053. if(flags&SWS_FAST_BILINEAR)
  2054. MSG_INFO("\nSwScaler: FAST_BILINEAR scaler, ");
  2055. else if(flags&SWS_BILINEAR)
  2056. MSG_INFO("\nSwScaler: BILINEAR scaler, ");
  2057. else if(flags&SWS_BICUBIC)
  2058. MSG_INFO("\nSwScaler: BICUBIC scaler, ");
  2059. else if(flags&SWS_X)
  2060. MSG_INFO("\nSwScaler: Experimental scaler, ");
  2061. else if(flags&SWS_POINT)
  2062. MSG_INFO("\nSwScaler: Nearest Neighbor / POINT scaler, ");
  2063. else if(flags&SWS_AREA)
  2064. MSG_INFO("\nSwScaler: Area Averageing scaler, ");
  2065. else if(flags&SWS_BICUBLIN)
  2066. MSG_INFO("\nSwScaler: luma BICUBIC / chroma BILINEAR scaler, ");
  2067. else if(flags&SWS_GAUSS)
  2068. MSG_INFO("\nSwScaler: Gaussian scaler, ");
  2069. else if(flags&SWS_SINC)
  2070. MSG_INFO("\nSwScaler: Sinc scaler, ");
  2071. else if(flags&SWS_LANCZOS)
  2072. MSG_INFO("\nSwScaler: Lanczos scaler, ");
  2073. else if(flags&SWS_SPLINE)
  2074. MSG_INFO("\nSwScaler: Bicubic spline scaler, ");
  2075. else
  2076. MSG_INFO("\nSwScaler: ehh flags invalid?! ");
  2077. if(dstFormat==PIX_FMT_BGR555 || dstFormat==PIX_FMT_BGR565)
  2078. MSG_INFO("from %s to%s %s ",
  2079. sws_format_name(srcFormat), dither, sws_format_name(dstFormat));
  2080. else
  2081. MSG_INFO("from %s to %s ",
  2082. sws_format_name(srcFormat), sws_format_name(dstFormat));
  2083. if(flags & SWS_CPU_CAPS_MMX2)
  2084. MSG_INFO("using MMX2\n");
  2085. else if(flags & SWS_CPU_CAPS_3DNOW)
  2086. MSG_INFO("using 3DNOW\n");
  2087. else if(flags & SWS_CPU_CAPS_MMX)
  2088. MSG_INFO("using MMX\n");
  2089. else if(flags & SWS_CPU_CAPS_ALTIVEC)
  2090. MSG_INFO("using AltiVec\n");
  2091. else
  2092. MSG_INFO("using C\n");
  2093. }
  2094. if(flags & SWS_PRINT_INFO)
  2095. {
  2096. if(flags & SWS_CPU_CAPS_MMX)
  2097. {
  2098. if(c->canMMX2BeUsed && (flags&SWS_FAST_BILINEAR))
  2099. MSG_V("SwScaler: using FAST_BILINEAR MMX2 scaler for horizontal scaling\n");
  2100. else
  2101. {
  2102. if(c->hLumFilterSize==4)
  2103. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal luminance scaling\n");
  2104. else if(c->hLumFilterSize==8)
  2105. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal luminance scaling\n");
  2106. else
  2107. MSG_V("SwScaler: using n-tap MMX scaler for horizontal luminance scaling\n");
  2108. if(c->hChrFilterSize==4)
  2109. MSG_V("SwScaler: using 4-tap MMX scaler for horizontal chrominance scaling\n");
  2110. else if(c->hChrFilterSize==8)
  2111. MSG_V("SwScaler: using 8-tap MMX scaler for horizontal chrominance scaling\n");
  2112. else
  2113. MSG_V("SwScaler: using n-tap MMX scaler for horizontal chrominance scaling\n");
  2114. }
  2115. }
  2116. else
  2117. {
  2118. #if defined(ARCH_X86)
  2119. MSG_V("SwScaler: using X86-Asm scaler for horizontal scaling\n");
  2120. #else
  2121. if(flags & SWS_FAST_BILINEAR)
  2122. MSG_V("SwScaler: using FAST_BILINEAR C scaler for horizontal scaling\n");
  2123. else
  2124. MSG_V("SwScaler: using C scaler for horizontal scaling\n");
  2125. #endif
  2126. }
  2127. if(isPlanarYUV(dstFormat))
  2128. {
  2129. if(c->vLumFilterSize==1)
  2130. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2131. else
  2132. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (YV12 like)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2133. }
  2134. else
  2135. {
  2136. if(c->vLumFilterSize==1 && c->vChrFilterSize==2)
  2137. MSG_V("SwScaler: using 1-tap %s \"scaler\" for vertical luminance scaling (BGR)\n"
  2138. "SwScaler: 2-tap scaler for vertical chrominance scaling (BGR)\n",(flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2139. else if(c->vLumFilterSize==2 && c->vChrFilterSize==2)
  2140. MSG_V("SwScaler: using 2-tap linear %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2141. else
  2142. MSG_V("SwScaler: using n-tap %s scaler for vertical scaling (BGR)\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2143. }
  2144. if(dstFormat==PIX_FMT_BGR24)
  2145. MSG_V("SwScaler: using %s YV12->BGR24 Converter\n",
  2146. (flags & SWS_CPU_CAPS_MMX2) ? "MMX2" : ((flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C"));
  2147. else if(dstFormat==PIX_FMT_RGB32)
  2148. MSG_V("SwScaler: using %s YV12->BGR32 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2149. else if(dstFormat==PIX_FMT_BGR565)
  2150. MSG_V("SwScaler: using %s YV12->BGR16 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2151. else if(dstFormat==PIX_FMT_BGR555)
  2152. MSG_V("SwScaler: using %s YV12->BGR15 Converter\n", (flags & SWS_CPU_CAPS_MMX) ? "MMX" : "C");
  2153. MSG_V("SwScaler: %dx%d -> %dx%d\n", srcW, srcH, dstW, dstH);
  2154. }
  2155. if(flags & SWS_PRINT_INFO)
  2156. {
  2157. MSG_DBG2("SwScaler:Lum srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2158. c->srcW, c->srcH, c->dstW, c->dstH, c->lumXInc, c->lumYInc);
  2159. MSG_DBG2("SwScaler:Chr srcW=%d srcH=%d dstW=%d dstH=%d xInc=%d yInc=%d\n",
  2160. c->chrSrcW, c->chrSrcH, c->chrDstW, c->chrDstH, c->chrXInc, c->chrYInc);
  2161. }
  2162. c->swScale= getSwsFunc(flags);
  2163. return c;
  2164. }
  2165. /**
  2166. * swscale warper, so we don't need to export the SwsContext.
  2167. * assumes planar YUV to be in YUV order instead of YVU
  2168. */
  2169. int sws_scale_ordered(SwsContext *c, uint8_t* src[], int srcStride[], int srcSliceY,
  2170. int srcSliceH, uint8_t* dst[], int dstStride[]){
  2171. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  2172. MSG_ERR("swScaler: slices start in the middle!\n");
  2173. return 0;
  2174. }
  2175. if (c->sliceDir == 0) {
  2176. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  2177. }
  2178. // copy strides, so they can safely be modified
  2179. if (c->sliceDir == 1) {
  2180. // slices go from top to bottom
  2181. int srcStride2[3]= {srcStride[0], srcStride[1], srcStride[2]};
  2182. int dstStride2[3]= {dstStride[0], dstStride[1], dstStride[2]};
  2183. return c->swScale(c, src, srcStride2, srcSliceY, srcSliceH, dst, dstStride2);
  2184. } else {
  2185. // slices go from bottom to top => we flip the image internally
  2186. uint8_t* src2[3]= {src[0] + (srcSliceH-1)*srcStride[0],
  2187. src[1] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1],
  2188. src[2] + ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2]
  2189. };
  2190. uint8_t* dst2[3]= {dst[0] + (c->dstH-1)*dstStride[0],
  2191. dst[1] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1],
  2192. dst[2] + ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2]};
  2193. int srcStride2[3]= {-srcStride[0], -srcStride[1], -srcStride[2]};
  2194. int dstStride2[3]= {-dstStride[0], -dstStride[1], -dstStride[2]};
  2195. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  2196. }
  2197. }
  2198. /**
  2199. * swscale warper, so we don't need to export the SwsContext
  2200. */
  2201. int sws_scale(SwsContext *c, uint8_t* srcParam[], int srcStride[], int srcSliceY,
  2202. int srcSliceH, uint8_t* dstParam[], int dstStride[]){
  2203. uint8_t *src[3];
  2204. uint8_t *dst[3];
  2205. src[0] = srcParam[0]; src[1] = srcParam[1]; src[2] = srcParam[2];
  2206. dst[0] = dstParam[0]; dst[1] = dstParam[1]; dst[2] = dstParam[2];
  2207. //printf("sws: slice %d %d\n", srcSliceY, srcSliceH);
  2208. return c->swScale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  2209. }
  2210. SwsFilter *sws_getDefaultFilter(float lumaGBlur, float chromaGBlur,
  2211. float lumaSharpen, float chromaSharpen,
  2212. float chromaHShift, float chromaVShift,
  2213. int verbose)
  2214. {
  2215. SwsFilter *filter= av_malloc(sizeof(SwsFilter));
  2216. if(lumaGBlur!=0.0){
  2217. filter->lumH= sws_getGaussianVec(lumaGBlur, 3.0);
  2218. filter->lumV= sws_getGaussianVec(lumaGBlur, 3.0);
  2219. }else{
  2220. filter->lumH= sws_getIdentityVec();
  2221. filter->lumV= sws_getIdentityVec();
  2222. }
  2223. if(chromaGBlur!=0.0){
  2224. filter->chrH= sws_getGaussianVec(chromaGBlur, 3.0);
  2225. filter->chrV= sws_getGaussianVec(chromaGBlur, 3.0);
  2226. }else{
  2227. filter->chrH= sws_getIdentityVec();
  2228. filter->chrV= sws_getIdentityVec();
  2229. }
  2230. if(chromaSharpen!=0.0){
  2231. SwsVector *id= sws_getIdentityVec();
  2232. sws_scaleVec(filter->chrH, -chromaSharpen);
  2233. sws_scaleVec(filter->chrV, -chromaSharpen);
  2234. sws_addVec(filter->chrH, id);
  2235. sws_addVec(filter->chrV, id);
  2236. sws_freeVec(id);
  2237. }
  2238. if(lumaSharpen!=0.0){
  2239. SwsVector *id= sws_getIdentityVec();
  2240. sws_scaleVec(filter->lumH, -lumaSharpen);
  2241. sws_scaleVec(filter->lumV, -lumaSharpen);
  2242. sws_addVec(filter->lumH, id);
  2243. sws_addVec(filter->lumV, id);
  2244. sws_freeVec(id);
  2245. }
  2246. if(chromaHShift != 0.0)
  2247. sws_shiftVec(filter->chrH, (int)(chromaHShift+0.5));
  2248. if(chromaVShift != 0.0)
  2249. sws_shiftVec(filter->chrV, (int)(chromaVShift+0.5));
  2250. sws_normalizeVec(filter->chrH, 1.0);
  2251. sws_normalizeVec(filter->chrV, 1.0);
  2252. sws_normalizeVec(filter->lumH, 1.0);
  2253. sws_normalizeVec(filter->lumV, 1.0);
  2254. if(verbose) sws_printVec(filter->chrH);
  2255. if(verbose) sws_printVec(filter->lumH);
  2256. return filter;
  2257. }
  2258. /**
  2259. * returns a normalized gaussian curve used to filter stuff
  2260. * quality=3 is high quality, lowwer is lowwer quality
  2261. */
  2262. SwsVector *sws_getGaussianVec(double variance, double quality){
  2263. const int length= (int)(variance*quality + 0.5) | 1;
  2264. int i;
  2265. double *coeff= av_malloc(length*sizeof(double));
  2266. double middle= (length-1)*0.5;
  2267. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2268. vec->coeff= coeff;
  2269. vec->length= length;
  2270. for(i=0; i<length; i++)
  2271. {
  2272. double dist= i-middle;
  2273. coeff[i]= exp( -dist*dist/(2*variance*variance) ) / sqrt(2*variance*PI);
  2274. }
  2275. sws_normalizeVec(vec, 1.0);
  2276. return vec;
  2277. }
  2278. SwsVector *sws_getConstVec(double c, int length){
  2279. int i;
  2280. double *coeff= av_malloc(length*sizeof(double));
  2281. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2282. vec->coeff= coeff;
  2283. vec->length= length;
  2284. for(i=0; i<length; i++)
  2285. coeff[i]= c;
  2286. return vec;
  2287. }
  2288. SwsVector *sws_getIdentityVec(void){
  2289. return sws_getConstVec(1.0, 1);
  2290. }
  2291. double sws_dcVec(SwsVector *a){
  2292. int i;
  2293. double sum=0;
  2294. for(i=0; i<a->length; i++)
  2295. sum+= a->coeff[i];
  2296. return sum;
  2297. }
  2298. void sws_scaleVec(SwsVector *a, double scalar){
  2299. int i;
  2300. for(i=0; i<a->length; i++)
  2301. a->coeff[i]*= scalar;
  2302. }
  2303. void sws_normalizeVec(SwsVector *a, double height){
  2304. sws_scaleVec(a, height/sws_dcVec(a));
  2305. }
  2306. static SwsVector *sws_getConvVec(SwsVector *a, SwsVector *b){
  2307. int length= a->length + b->length - 1;
  2308. double *coeff= av_malloc(length*sizeof(double));
  2309. int i, j;
  2310. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2311. vec->coeff= coeff;
  2312. vec->length= length;
  2313. for(i=0; i<length; i++) coeff[i]= 0.0;
  2314. for(i=0; i<a->length; i++)
  2315. {
  2316. for(j=0; j<b->length; j++)
  2317. {
  2318. coeff[i+j]+= a->coeff[i]*b->coeff[j];
  2319. }
  2320. }
  2321. return vec;
  2322. }
  2323. static SwsVector *sws_sumVec(SwsVector *a, SwsVector *b){
  2324. int length= FFMAX(a->length, b->length);
  2325. double *coeff= av_malloc(length*sizeof(double));
  2326. int i;
  2327. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2328. vec->coeff= coeff;
  2329. vec->length= length;
  2330. for(i=0; i<length; i++) coeff[i]= 0.0;
  2331. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2332. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]+= b->coeff[i];
  2333. return vec;
  2334. }
  2335. static SwsVector *sws_diffVec(SwsVector *a, SwsVector *b){
  2336. int length= FFMAX(a->length, b->length);
  2337. double *coeff= av_malloc(length*sizeof(double));
  2338. int i;
  2339. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2340. vec->coeff= coeff;
  2341. vec->length= length;
  2342. for(i=0; i<length; i++) coeff[i]= 0.0;
  2343. for(i=0; i<a->length; i++) coeff[i + (length-1)/2 - (a->length-1)/2]+= a->coeff[i];
  2344. for(i=0; i<b->length; i++) coeff[i + (length-1)/2 - (b->length-1)/2]-= b->coeff[i];
  2345. return vec;
  2346. }
  2347. /* shift left / or right if "shift" is negative */
  2348. static SwsVector *sws_getShiftedVec(SwsVector *a, int shift){
  2349. int length= a->length + FFABS(shift)*2;
  2350. double *coeff= av_malloc(length*sizeof(double));
  2351. int i;
  2352. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2353. vec->coeff= coeff;
  2354. vec->length= length;
  2355. for(i=0; i<length; i++) coeff[i]= 0.0;
  2356. for(i=0; i<a->length; i++)
  2357. {
  2358. coeff[i + (length-1)/2 - (a->length-1)/2 - shift]= a->coeff[i];
  2359. }
  2360. return vec;
  2361. }
  2362. void sws_shiftVec(SwsVector *a, int shift){
  2363. SwsVector *shifted= sws_getShiftedVec(a, shift);
  2364. av_free(a->coeff);
  2365. a->coeff= shifted->coeff;
  2366. a->length= shifted->length;
  2367. av_free(shifted);
  2368. }
  2369. void sws_addVec(SwsVector *a, SwsVector *b){
  2370. SwsVector *sum= sws_sumVec(a, b);
  2371. av_free(a->coeff);
  2372. a->coeff= sum->coeff;
  2373. a->length= sum->length;
  2374. av_free(sum);
  2375. }
  2376. void sws_subVec(SwsVector *a, SwsVector *b){
  2377. SwsVector *diff= sws_diffVec(a, b);
  2378. av_free(a->coeff);
  2379. a->coeff= diff->coeff;
  2380. a->length= diff->length;
  2381. av_free(diff);
  2382. }
  2383. void sws_convVec(SwsVector *a, SwsVector *b){
  2384. SwsVector *conv= sws_getConvVec(a, b);
  2385. av_free(a->coeff);
  2386. a->coeff= conv->coeff;
  2387. a->length= conv->length;
  2388. av_free(conv);
  2389. }
  2390. SwsVector *sws_cloneVec(SwsVector *a){
  2391. double *coeff= av_malloc(a->length*sizeof(double));
  2392. int i;
  2393. SwsVector *vec= av_malloc(sizeof(SwsVector));
  2394. vec->coeff= coeff;
  2395. vec->length= a->length;
  2396. for(i=0; i<a->length; i++) coeff[i]= a->coeff[i];
  2397. return vec;
  2398. }
  2399. void sws_printVec(SwsVector *a){
  2400. int i;
  2401. double max=0;
  2402. double min=0;
  2403. double range;
  2404. for(i=0; i<a->length; i++)
  2405. if(a->coeff[i]>max) max= a->coeff[i];
  2406. for(i=0; i<a->length; i++)
  2407. if(a->coeff[i]<min) min= a->coeff[i];
  2408. range= max - min;
  2409. for(i=0; i<a->length; i++)
  2410. {
  2411. int x= (int)((a->coeff[i]-min)*60.0/range +0.5);
  2412. MSG_DBG2("%1.3f ", a->coeff[i]);
  2413. for(;x>0; x--) MSG_DBG2(" ");
  2414. MSG_DBG2("|\n");
  2415. }
  2416. }
  2417. void sws_freeVec(SwsVector *a){
  2418. if(!a) return;
  2419. av_free(a->coeff);
  2420. a->coeff=NULL;
  2421. a->length=0;
  2422. av_free(a);
  2423. }
  2424. void sws_freeFilter(SwsFilter *filter){
  2425. if(!filter) return;
  2426. if(filter->lumH) sws_freeVec(filter->lumH);
  2427. if(filter->lumV) sws_freeVec(filter->lumV);
  2428. if(filter->chrH) sws_freeVec(filter->chrH);
  2429. if(filter->chrV) sws_freeVec(filter->chrV);
  2430. av_free(filter);
  2431. }
  2432. void sws_freeContext(SwsContext *c){
  2433. int i;
  2434. if(!c) return;
  2435. if(c->lumPixBuf)
  2436. {
  2437. for(i=0; i<c->vLumBufSize; i++)
  2438. {
  2439. av_free(c->lumPixBuf[i]);
  2440. c->lumPixBuf[i]=NULL;
  2441. }
  2442. av_free(c->lumPixBuf);
  2443. c->lumPixBuf=NULL;
  2444. }
  2445. if(c->chrPixBuf)
  2446. {
  2447. for(i=0; i<c->vChrBufSize; i++)
  2448. {
  2449. av_free(c->chrPixBuf[i]);
  2450. c->chrPixBuf[i]=NULL;
  2451. }
  2452. av_free(c->chrPixBuf);
  2453. c->chrPixBuf=NULL;
  2454. }
  2455. av_free(c->vLumFilter);
  2456. c->vLumFilter = NULL;
  2457. av_free(c->vChrFilter);
  2458. c->vChrFilter = NULL;
  2459. av_free(c->hLumFilter);
  2460. c->hLumFilter = NULL;
  2461. av_free(c->hChrFilter);
  2462. c->hChrFilter = NULL;
  2463. #ifdef HAVE_ALTIVEC
  2464. av_free(c->vYCoeffsBank);
  2465. c->vYCoeffsBank = NULL;
  2466. av_free(c->vCCoeffsBank);
  2467. c->vCCoeffsBank = NULL;
  2468. #endif
  2469. av_free(c->vLumFilterPos);
  2470. c->vLumFilterPos = NULL;
  2471. av_free(c->vChrFilterPos);
  2472. c->vChrFilterPos = NULL;
  2473. av_free(c->hLumFilterPos);
  2474. c->hLumFilterPos = NULL;
  2475. av_free(c->hChrFilterPos);
  2476. c->hChrFilterPos = NULL;
  2477. #if defined(ARCH_X86)
  2478. #ifdef MAP_ANONYMOUS
  2479. if(c->funnyYCode) munmap(c->funnyYCode, MAX_FUNNY_CODE_SIZE);
  2480. if(c->funnyUVCode) munmap(c->funnyUVCode, MAX_FUNNY_CODE_SIZE);
  2481. #else
  2482. av_free(c->funnyYCode);
  2483. av_free(c->funnyUVCode);
  2484. #endif
  2485. c->funnyYCode=NULL;
  2486. c->funnyUVCode=NULL;
  2487. #endif /* defined(ARCH_X86) */
  2488. av_free(c->lumMmx2Filter);
  2489. c->lumMmx2Filter=NULL;
  2490. av_free(c->chrMmx2Filter);
  2491. c->chrMmx2Filter=NULL;
  2492. av_free(c->lumMmx2FilterPos);
  2493. c->lumMmx2FilterPos=NULL;
  2494. av_free(c->chrMmx2FilterPos);
  2495. c->chrMmx2FilterPos=NULL;
  2496. av_free(c->yuvTable);
  2497. c->yuvTable=NULL;
  2498. av_free(c);
  2499. }
  2500. /**
  2501. * Checks if context is valid or reallocs a new one instead.
  2502. * If context is NULL, just calls sws_getContext() to get a new one.
  2503. * Otherwise, checks if the parameters are the same already saved in context.
  2504. * If that is the case, returns the current context.
  2505. * Otherwise, frees context and gets a new one.
  2506. *
  2507. * Be warned that srcFilter, dstFilter are not checked, they are
  2508. * asumed to remain valid.
  2509. */
  2510. struct SwsContext *sws_getCachedContext(struct SwsContext *context,
  2511. int srcW, int srcH, int srcFormat,
  2512. int dstW, int dstH, int dstFormat, int flags,
  2513. SwsFilter *srcFilter, SwsFilter *dstFilter, double *param)
  2514. {
  2515. if (context != NULL) {
  2516. if ((context->srcW != srcW) || (context->srcH != srcH) ||
  2517. (context->srcFormat != srcFormat) ||
  2518. (context->dstW != dstW) || (context->dstH != dstH) ||
  2519. (context->dstFormat != dstFormat) || (context->flags != flags) ||
  2520. (context->param != param))
  2521. {
  2522. sws_freeContext(context);
  2523. context = NULL;
  2524. }
  2525. }
  2526. if (context == NULL) {
  2527. return sws_getContext(srcW, srcH, srcFormat,
  2528. dstW, dstH, dstFormat, flags,
  2529. srcFilter, dstFilter, param);
  2530. }
  2531. return context;
  2532. }