swscale.c 104 KB

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