swscale.c 115 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067
  1. /*
  2. * Copyright (C) 2001-2011 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
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (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 GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /*
  21. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  22. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  23. {BGR,RGB}{1,4,8,15,16} support dithering
  24. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  25. YV12 -> {BGR,RGB}{1,4,8,12,15,16,24,32}
  26. x -> x
  27. YUV9 -> YV12
  28. YUV9/YV12 -> Y800
  29. Y800 -> YUV9/YV12
  30. BGR24 -> BGR32 & RGB24 -> RGB32
  31. BGR32 -> BGR24 & RGB32 -> RGB24
  32. BGR15 -> BGR16
  33. */
  34. /*
  35. tested special converters (most are tested actually, but I did not write it down ...)
  36. YV12 -> BGR12/BGR16
  37. YV12 -> YV12
  38. BGR15 -> BGR16
  39. BGR16 -> BGR16
  40. YVU9 -> YV12
  41. untested special converters
  42. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  43. YV12/I420 -> YV12/I420
  44. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  45. BGR24 -> BGR32 & RGB24 -> RGB32
  46. BGR32 -> BGR24 & RGB32 -> RGB24
  47. BGR24 -> YV12
  48. */
  49. #include <inttypes.h>
  50. #include <string.h>
  51. #include <math.h>
  52. #include <stdio.h>
  53. #include "config.h"
  54. #include <assert.h>
  55. #include "swscale.h"
  56. #include "swscale_internal.h"
  57. #include "rgb2rgb.h"
  58. #include "libavutil/avassert.h"
  59. #include "libavutil/intreadwrite.h"
  60. #include "libavutil/cpu.h"
  61. #include "libavutil/avutil.h"
  62. #include "libavutil/mathematics.h"
  63. #include "libavutil/bswap.h"
  64. #include "libavutil/pixdesc.h"
  65. #define RGB2YUV_SHIFT 15
  66. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  67. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  68. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  69. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  70. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  71. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  72. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  73. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  74. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  75. /*
  76. NOTES
  77. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  78. TODO
  79. more intelligent misalignment avoidance for the horizontal scaler
  80. write special vertical cubic upscale version
  81. optimize C code (YV12 / minmax)
  82. add support for packed pixel YUV input & output
  83. add support for Y8 output
  84. optimize BGR24 & BGR32
  85. add BGR4 output support
  86. write special BGR->BGR scaler
  87. */
  88. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  89. { 1, 3, 1, 3, 1, 3, 1, 3, },
  90. { 2, 0, 2, 0, 2, 0, 2, 0, },
  91. };
  92. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  93. { 6, 2, 6, 2, 6, 2, 6, 2, },
  94. { 0, 4, 0, 4, 0, 4, 0, 4, },
  95. };
  96. DECLARE_ALIGNED(8, const uint8_t, dither_4x4_16)[4][8]={
  97. { 8, 4, 11, 7, 8, 4, 11, 7, },
  98. { 2, 14, 1, 13, 2, 14, 1, 13, },
  99. { 10, 6, 9, 5, 10, 6, 9, 5, },
  100. { 0, 12, 3, 15, 0, 12, 3, 15, },
  101. };
  102. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  103. { 17, 9, 23, 15, 16, 8, 22, 14, },
  104. { 5, 29, 3, 27, 4, 28, 2, 26, },
  105. { 21, 13, 19, 11, 20, 12, 18, 10, },
  106. { 0, 24, 6, 30, 1, 25, 7, 31, },
  107. { 16, 8, 22, 14, 17, 9, 23, 15, },
  108. { 4, 28, 2, 26, 5, 29, 3, 27, },
  109. { 20, 12, 18, 10, 21, 13, 19, 11, },
  110. { 1, 25, 7, 31, 0, 24, 6, 30, },
  111. };
  112. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  113. { 0, 55, 14, 68, 3, 58, 17, 72, },
  114. { 37, 18, 50, 32, 40, 22, 54, 35, },
  115. { 9, 64, 5, 59, 13, 67, 8, 63, },
  116. { 46, 27, 41, 23, 49, 31, 44, 26, },
  117. { 2, 57, 16, 71, 1, 56, 15, 70, },
  118. { 39, 21, 52, 34, 38, 19, 51, 33, },
  119. { 11, 66, 7, 62, 10, 65, 6, 60, },
  120. { 48, 30, 43, 25, 47, 29, 42, 24, },
  121. };
  122. #if 1
  123. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  124. {117, 62, 158, 103, 113, 58, 155, 100, },
  125. { 34, 199, 21, 186, 31, 196, 17, 182, },
  126. {144, 89, 131, 76, 141, 86, 127, 72, },
  127. { 0, 165, 41, 206, 10, 175, 52, 217, },
  128. {110, 55, 151, 96, 120, 65, 162, 107, },
  129. { 28, 193, 14, 179, 38, 203, 24, 189, },
  130. {138, 83, 124, 69, 148, 93, 134, 79, },
  131. { 7, 172, 48, 213, 3, 168, 45, 210, },
  132. };
  133. #elif 1
  134. // tries to correct a gamma of 1.5
  135. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  136. { 0, 143, 18, 200, 2, 156, 25, 215, },
  137. { 78, 28, 125, 64, 89, 36, 138, 74, },
  138. { 10, 180, 3, 161, 16, 195, 8, 175, },
  139. {109, 51, 93, 38, 121, 60, 105, 47, },
  140. { 1, 152, 23, 210, 0, 147, 20, 205, },
  141. { 85, 33, 134, 71, 81, 30, 130, 67, },
  142. { 14, 190, 6, 171, 12, 185, 5, 166, },
  143. {117, 57, 101, 44, 113, 54, 97, 41, },
  144. };
  145. #elif 1
  146. // tries to correct a gamma of 2.0
  147. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  148. { 0, 124, 8, 193, 0, 140, 12, 213, },
  149. { 55, 14, 104, 42, 66, 19, 119, 52, },
  150. { 3, 168, 1, 145, 6, 187, 3, 162, },
  151. { 86, 31, 70, 21, 99, 39, 82, 28, },
  152. { 0, 134, 11, 206, 0, 129, 9, 200, },
  153. { 62, 17, 114, 48, 58, 16, 109, 45, },
  154. { 5, 181, 2, 157, 4, 175, 1, 151, },
  155. { 95, 36, 78, 26, 90, 34, 74, 24, },
  156. };
  157. #else
  158. // tries to correct a gamma of 2.5
  159. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  160. { 0, 107, 3, 187, 0, 125, 6, 212, },
  161. { 39, 7, 86, 28, 49, 11, 102, 36, },
  162. { 1, 158, 0, 131, 3, 180, 1, 151, },
  163. { 68, 19, 52, 12, 81, 25, 64, 17, },
  164. { 0, 119, 5, 203, 0, 113, 4, 195, },
  165. { 45, 9, 96, 33, 42, 8, 91, 30, },
  166. { 2, 172, 1, 144, 2, 165, 0, 137, },
  167. { 77, 23, 60, 15, 72, 21, 56, 14, },
  168. };
  169. #endif
  170. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_128)[8][8] = {
  171. { 36, 68, 60, 92, 34, 66, 58, 90,},
  172. { 100, 4,124, 28, 98, 2,122, 26,},
  173. { 52, 84, 44, 76, 50, 82, 42, 74,},
  174. { 116, 20,108, 12,114, 18,106, 10,},
  175. { 32, 64, 56, 88, 38, 70, 62, 94,},
  176. { 96, 0,120, 24,102, 6,126, 30,},
  177. { 48, 80, 40, 72, 54, 86, 46, 78,},
  178. { 112, 16,104, 8,118, 22,110, 14,},
  179. };
  180. DECLARE_ALIGNED(8, const uint8_t, ff_sws_pb_64)[8] =
  181. { 64, 64, 64, 64, 64, 64, 64, 64 };
  182. DECLARE_ALIGNED(8, const uint8_t, dithers)[8][8][8]={
  183. {
  184. { 0, 1, 0, 1, 0, 1, 0, 1,},
  185. { 1, 0, 1, 0, 1, 0, 1, 0,},
  186. { 0, 1, 0, 1, 0, 1, 0, 1,},
  187. { 1, 0, 1, 0, 1, 0, 1, 0,},
  188. { 0, 1, 0, 1, 0, 1, 0, 1,},
  189. { 1, 0, 1, 0, 1, 0, 1, 0,},
  190. { 0, 1, 0, 1, 0, 1, 0, 1,},
  191. { 1, 0, 1, 0, 1, 0, 1, 0,},
  192. },{
  193. { 1, 2, 1, 2, 1, 2, 1, 2,},
  194. { 3, 0, 3, 0, 3, 0, 3, 0,},
  195. { 1, 2, 1, 2, 1, 2, 1, 2,},
  196. { 3, 0, 3, 0, 3, 0, 3, 0,},
  197. { 1, 2, 1, 2, 1, 2, 1, 2,},
  198. { 3, 0, 3, 0, 3, 0, 3, 0,},
  199. { 1, 2, 1, 2, 1, 2, 1, 2,},
  200. { 3, 0, 3, 0, 3, 0, 3, 0,},
  201. },{
  202. { 2, 4, 3, 5, 2, 4, 3, 5,},
  203. { 6, 0, 7, 1, 6, 0, 7, 1,},
  204. { 3, 5, 2, 4, 3, 5, 2, 4,},
  205. { 7, 1, 6, 0, 7, 1, 6, 0,},
  206. { 2, 4, 3, 5, 2, 4, 3, 5,},
  207. { 6, 0, 7, 1, 6, 0, 7, 1,},
  208. { 3, 5, 2, 4, 3, 5, 2, 4,},
  209. { 7, 1, 6, 0, 7, 1, 6, 0,},
  210. },{
  211. { 4, 8, 7, 11, 4, 8, 7, 11,},
  212. { 12, 0, 15, 3, 12, 0, 15, 3,},
  213. { 6, 10, 5, 9, 6, 10, 5, 9,},
  214. { 14, 2, 13, 1, 14, 2, 13, 1,},
  215. { 4, 8, 7, 11, 4, 8, 7, 11,},
  216. { 12, 0, 15, 3, 12, 0, 15, 3,},
  217. { 6, 10, 5, 9, 6, 10, 5, 9,},
  218. { 14, 2, 13, 1, 14, 2, 13, 1,},
  219. },{
  220. { 9, 17, 15, 23, 8, 16, 14, 22,},
  221. { 25, 1, 31, 7, 24, 0, 30, 6,},
  222. { 13, 21, 11, 19, 12, 20, 10, 18,},
  223. { 29, 5, 27, 3, 28, 4, 26, 2,},
  224. { 8, 16, 14, 22, 9, 17, 15, 23,},
  225. { 24, 0, 30, 6, 25, 1, 31, 7,},
  226. { 12, 20, 10, 18, 13, 21, 11, 19,},
  227. { 28, 4, 26, 2, 29, 5, 27, 3,},
  228. },{
  229. { 18, 34, 30, 46, 17, 33, 29, 45,},
  230. { 50, 2, 62, 14, 49, 1, 61, 13,},
  231. { 26, 42, 22, 38, 25, 41, 21, 37,},
  232. { 58, 10, 54, 6, 57, 9, 53, 5,},
  233. { 16, 32, 28, 44, 19, 35, 31, 47,},
  234. { 48, 0, 60, 12, 51, 3, 63, 15,},
  235. { 24, 40, 20, 36, 27, 43, 23, 39,},
  236. { 56, 8, 52, 4, 59, 11, 55, 7,},
  237. },{
  238. { 18, 34, 30, 46, 17, 33, 29, 45,},
  239. { 50, 2, 62, 14, 49, 1, 61, 13,},
  240. { 26, 42, 22, 38, 25, 41, 21, 37,},
  241. { 58, 10, 54, 6, 57, 9, 53, 5,},
  242. { 16, 32, 28, 44, 19, 35, 31, 47,},
  243. { 48, 0, 60, 12, 51, 3, 63, 15,},
  244. { 24, 40, 20, 36, 27, 43, 23, 39,},
  245. { 56, 8, 52, 4, 59, 11, 55, 7,},
  246. },{
  247. { 36, 68, 60, 92, 34, 66, 58, 90,},
  248. { 100, 4,124, 28, 98, 2,122, 26,},
  249. { 52, 84, 44, 76, 50, 82, 42, 74,},
  250. { 116, 20,108, 12,114, 18,106, 10,},
  251. { 32, 64, 56, 88, 38, 70, 62, 94,},
  252. { 96, 0,120, 24,102, 6,126, 30,},
  253. { 48, 80, 40, 72, 54, 86, 46, 78,},
  254. { 112, 16,104, 8,118, 22,110, 14,},
  255. }};
  256. static const uint8_t flat64[8]={64,64,64,64,64,64,64,64};
  257. const uint16_t dither_scale[15][16]={
  258. { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
  259. { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
  260. { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
  261. { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
  262. { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
  263. { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
  264. { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
  265. { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
  266. { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
  267. { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
  268. { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
  269. { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
  270. { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
  271. { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
  272. { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
  273. };
  274. #define output_pixel(pos, val, bias, signedness) \
  275. if (big_endian) { \
  276. AV_WB16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  277. } else { \
  278. AV_WL16(pos, bias + av_clip_ ## signedness ## 16(val >> shift)); \
  279. }
  280. static av_always_inline void
  281. yuv2plane1_16_c_template(const int32_t *src, uint16_t *dest, int dstW,
  282. int big_endian, int output_bits)
  283. {
  284. int i;
  285. int shift = 3;
  286. av_assert0(output_bits == 16);
  287. for (i = 0; i < dstW; i++) {
  288. int val = src[i] + (1 << (shift - 1));
  289. output_pixel(&dest[i], val, 0, uint);
  290. }
  291. }
  292. static av_always_inline void
  293. yuv2planeX_16_c_template(const int16_t *filter, int filterSize,
  294. const int32_t **src, uint16_t *dest, int dstW,
  295. int big_endian, int output_bits)
  296. {
  297. int i;
  298. int shift = 15;
  299. av_assert0(output_bits == 16);
  300. for (i = 0; i < dstW; i++) {
  301. int val = 1 << (shift - 1);
  302. int j;
  303. /* range of val is [0,0x7FFFFFFF], so 31 bits, but with lanczos/spline
  304. * filters (or anything with negative coeffs, the range can be slightly
  305. * wider in both directions. To account for this overflow, we subtract
  306. * a constant so it always fits in the signed range (assuming a
  307. * reasonable filterSize), and re-add that at the end. */
  308. val -= 0x40000000;
  309. for (j = 0; j < filterSize; j++)
  310. val += src[j][i] * filter[j];
  311. output_pixel(&dest[i], val, 0x8000, int);
  312. }
  313. }
  314. #undef output_pixel
  315. #define output_pixel(pos, val) \
  316. if (big_endian) { \
  317. AV_WB16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  318. } else { \
  319. AV_WL16(pos, av_clip_uintp2(val >> shift, output_bits)); \
  320. }
  321. static av_always_inline void
  322. yuv2plane1_10_c_template(const int16_t *src, uint16_t *dest, int dstW,
  323. int big_endian, int output_bits)
  324. {
  325. int i;
  326. int shift = 15 - output_bits;
  327. for (i = 0; i < dstW; i++) {
  328. int val = src[i] + (1 << (shift - 1));
  329. output_pixel(&dest[i], val);
  330. }
  331. }
  332. static av_always_inline void
  333. yuv2planeX_10_c_template(const int16_t *filter, int filterSize,
  334. const int16_t **src, uint16_t *dest, int dstW,
  335. int big_endian, int output_bits)
  336. {
  337. int i;
  338. int shift = 11 + 16 - output_bits;
  339. for (i = 0; i < dstW; i++) {
  340. int val = 1 << (shift - 1);
  341. int j;
  342. for (j = 0; j < filterSize; j++)
  343. val += src[j][i] * filter[j];
  344. output_pixel(&dest[i], val);
  345. }
  346. }
  347. #undef output_pixel
  348. #define yuv2NBPS(bits, BE_LE, is_be, template_size, typeX_t) \
  349. static void yuv2plane1_ ## bits ## BE_LE ## _c(const int16_t *src, \
  350. uint8_t *dest, int dstW, \
  351. const uint8_t *dither, int offset)\
  352. { \
  353. yuv2plane1_ ## template_size ## _c_template((const typeX_t *) src, \
  354. (uint16_t *) dest, dstW, is_be, bits); \
  355. }\
  356. static void yuv2planeX_ ## bits ## BE_LE ## _c(const int16_t *filter, int filterSize, \
  357. const int16_t **src, uint8_t *dest, int dstW, \
  358. const uint8_t *dither, int offset)\
  359. { \
  360. yuv2planeX_## template_size ## _c_template(filter, \
  361. filterSize, (const typeX_t **) src, \
  362. (uint16_t *) dest, dstW, is_be, bits); \
  363. }
  364. yuv2NBPS( 9, BE, 1, 10, int16_t);
  365. yuv2NBPS( 9, LE, 0, 10, int16_t);
  366. yuv2NBPS(10, BE, 1, 10, int16_t);
  367. yuv2NBPS(10, LE, 0, 10, int16_t);
  368. yuv2NBPS(16, BE, 1, 16, int32_t);
  369. yuv2NBPS(16, LE, 0, 16, int32_t);
  370. static void yuv2planeX_8_c(const int16_t *filter, int filterSize,
  371. const int16_t **src, uint8_t *dest, int dstW,
  372. const uint8_t *dither, int offset)
  373. {
  374. int i;
  375. for (i=0; i<dstW; i++) {
  376. int val = dither[(i + offset) & 7] << 12;
  377. int j;
  378. for (j=0; j<filterSize; j++)
  379. val += src[j][i] * filter[j];
  380. dest[i]= av_clip_uint8(val>>19);
  381. }
  382. }
  383. static void yuv2plane1_8_c(const int16_t *src, uint8_t *dest, int dstW,
  384. const uint8_t *dither, int offset)
  385. {
  386. int i;
  387. for (i=0; i<dstW; i++) {
  388. int val = (src[i] + dither[(i + offset) & 7]) >> 7;
  389. dest[i]= av_clip_uint8(val);
  390. }
  391. }
  392. static void yuv2nv12cX_c(SwsContext *c, const int16_t *chrFilter, int chrFilterSize,
  393. const int16_t **chrUSrc, const int16_t **chrVSrc,
  394. uint8_t *dest, int chrDstW)
  395. {
  396. enum PixelFormat dstFormat = c->dstFormat;
  397. const uint8_t *chrDither = c->chrDither8;
  398. int i;
  399. if (dstFormat == PIX_FMT_NV12)
  400. for (i=0; i<chrDstW; i++) {
  401. int u = chrDither[i & 7] << 12;
  402. int v = chrDither[(i + 3) & 7] << 12;
  403. int j;
  404. for (j=0; j<chrFilterSize; j++) {
  405. u += chrUSrc[j][i] * chrFilter[j];
  406. v += chrVSrc[j][i] * chrFilter[j];
  407. }
  408. dest[2*i]= av_clip_uint8(u>>19);
  409. dest[2*i+1]= av_clip_uint8(v>>19);
  410. }
  411. else
  412. for (i=0; i<chrDstW; i++) {
  413. int u = chrDither[i & 7] << 12;
  414. int v = chrDither[(i + 3) & 7] << 12;
  415. int j;
  416. for (j=0; j<chrFilterSize; j++) {
  417. u += chrUSrc[j][i] * chrFilter[j];
  418. v += chrVSrc[j][i] * chrFilter[j];
  419. }
  420. dest[2*i]= av_clip_uint8(v>>19);
  421. dest[2*i+1]= av_clip_uint8(u>>19);
  422. }
  423. }
  424. #define output_pixel(pos, val) \
  425. if (target == PIX_FMT_GRAY16BE) { \
  426. AV_WB16(pos, val); \
  427. } else { \
  428. AV_WL16(pos, val); \
  429. }
  430. static av_always_inline void
  431. yuv2gray16_X_c_template(SwsContext *c, const int16_t *lumFilter,
  432. const int32_t **lumSrc, int lumFilterSize,
  433. const int16_t *chrFilter, const int32_t **chrUSrc,
  434. const int32_t **chrVSrc, int chrFilterSize,
  435. const int32_t **alpSrc, uint16_t *dest, int dstW,
  436. int y, enum PixelFormat target)
  437. {
  438. int i;
  439. for (i = 0; i < (dstW >> 1); i++) {
  440. int j;
  441. int Y1 = 1 << 14;
  442. int Y2 = 1 << 14;
  443. for (j = 0; j < lumFilterSize; j++) {
  444. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  445. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  446. }
  447. Y1 >>= 15;
  448. Y2 >>= 15;
  449. if ((Y1 | Y2) & 0x10000) {
  450. Y1 = av_clip_uint16(Y1);
  451. Y2 = av_clip_uint16(Y2);
  452. }
  453. output_pixel(&dest[i * 2 + 0], Y1);
  454. output_pixel(&dest[i * 2 + 1], Y2);
  455. }
  456. }
  457. static av_always_inline void
  458. yuv2gray16_2_c_template(SwsContext *c, const int32_t *buf[2],
  459. const int32_t *ubuf[2], const int32_t *vbuf[2],
  460. const int32_t *abuf[2], uint16_t *dest, int dstW,
  461. int yalpha, int uvalpha, int y,
  462. enum PixelFormat target)
  463. {
  464. int yalpha1 = 4095 - yalpha;
  465. int i;
  466. const int32_t *buf0 = buf[0], *buf1 = buf[1];
  467. for (i = 0; i < (dstW >> 1); i++) {
  468. int Y1 = (buf0[i * 2 ] * yalpha1 + buf1[i * 2 ] * yalpha) >> 15;
  469. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 15;
  470. output_pixel(&dest[i * 2 + 0], Y1);
  471. output_pixel(&dest[i * 2 + 1], Y2);
  472. }
  473. }
  474. static av_always_inline void
  475. yuv2gray16_1_c_template(SwsContext *c, const int32_t *buf0,
  476. const int32_t *ubuf[2], const int32_t *vbuf[2],
  477. const int32_t *abuf0, uint16_t *dest, int dstW,
  478. int uvalpha, int y, enum PixelFormat target)
  479. {
  480. int i;
  481. for (i = 0; i < (dstW >> 1); i++) {
  482. int Y1 = (buf0[i * 2 ]+4)>>3;
  483. int Y2 = (buf0[i * 2 + 1]+4)>>3;
  484. output_pixel(&dest[i * 2 + 0], Y1);
  485. output_pixel(&dest[i * 2 + 1], Y2);
  486. }
  487. }
  488. #undef output_pixel
  489. #define YUV2PACKED16WRAPPER(name, base, ext, fmt) \
  490. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  491. const int16_t **_lumSrc, int lumFilterSize, \
  492. const int16_t *chrFilter, const int16_t **_chrUSrc, \
  493. const int16_t **_chrVSrc, int chrFilterSize, \
  494. const int16_t **_alpSrc, uint8_t *_dest, int dstW, \
  495. int y) \
  496. { \
  497. const int32_t **lumSrc = (const int32_t **) _lumSrc, \
  498. **chrUSrc = (const int32_t **) _chrUSrc, \
  499. **chrVSrc = (const int32_t **) _chrVSrc, \
  500. **alpSrc = (const int32_t **) _alpSrc; \
  501. uint16_t *dest = (uint16_t *) _dest; \
  502. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  503. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  504. alpSrc, dest, dstW, y, fmt); \
  505. } \
  506. \
  507. static void name ## ext ## _2_c(SwsContext *c, const int16_t *_buf[2], \
  508. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  509. const int16_t *_abuf[2], uint8_t *_dest, int dstW, \
  510. int yalpha, int uvalpha, int y) \
  511. { \
  512. const int32_t **buf = (const int32_t **) _buf, \
  513. **ubuf = (const int32_t **) _ubuf, \
  514. **vbuf = (const int32_t **) _vbuf, \
  515. **abuf = (const int32_t **) _abuf; \
  516. uint16_t *dest = (uint16_t *) _dest; \
  517. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  518. dest, dstW, yalpha, uvalpha, y, fmt); \
  519. } \
  520. \
  521. static void name ## ext ## _1_c(SwsContext *c, const int16_t *_buf0, \
  522. const int16_t *_ubuf[2], const int16_t *_vbuf[2], \
  523. const int16_t *_abuf0, uint8_t *_dest, int dstW, \
  524. int uvalpha, int y) \
  525. { \
  526. const int32_t *buf0 = (const int32_t *) _buf0, \
  527. **ubuf = (const int32_t **) _ubuf, \
  528. **vbuf = (const int32_t **) _vbuf, \
  529. *abuf0 = (const int32_t *) _abuf0; \
  530. uint16_t *dest = (uint16_t *) _dest; \
  531. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  532. dstW, uvalpha, y, fmt); \
  533. }
  534. YUV2PACKED16WRAPPER(yuv2gray16,, LE, PIX_FMT_GRAY16LE);
  535. YUV2PACKED16WRAPPER(yuv2gray16,, BE, PIX_FMT_GRAY16BE);
  536. #define output_pixel(pos, acc) \
  537. if (target == PIX_FMT_MONOBLACK) { \
  538. pos = acc; \
  539. } else { \
  540. pos = ~acc; \
  541. }
  542. static av_always_inline void
  543. yuv2mono_X_c_template(SwsContext *c, const int16_t *lumFilter,
  544. const int16_t **lumSrc, int lumFilterSize,
  545. const int16_t *chrFilter, const int16_t **chrUSrc,
  546. const int16_t **chrVSrc, int chrFilterSize,
  547. const int16_t **alpSrc, uint8_t *dest, int dstW,
  548. int y, enum PixelFormat target)
  549. {
  550. const uint8_t * const d128=dither_8x8_220[y&7];
  551. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  552. int i;
  553. unsigned acc = 0;
  554. for (i = 0; i < dstW - 1; i += 2) {
  555. int j;
  556. int Y1 = 1 << 18;
  557. int Y2 = 1 << 18;
  558. for (j = 0; j < lumFilterSize; j++) {
  559. Y1 += lumSrc[j][i] * lumFilter[j];
  560. Y2 += lumSrc[j][i+1] * lumFilter[j];
  561. }
  562. Y1 >>= 19;
  563. Y2 >>= 19;
  564. if ((Y1 | Y2) & 0x100) {
  565. Y1 = av_clip_uint8(Y1);
  566. Y2 = av_clip_uint8(Y2);
  567. }
  568. acc += acc + g[Y1 + d128[(i + 0) & 7]];
  569. acc += acc + g[Y2 + d128[(i + 1) & 7]];
  570. if ((i & 7) == 6) {
  571. output_pixel(*dest++, acc);
  572. }
  573. }
  574. }
  575. static av_always_inline void
  576. yuv2mono_2_c_template(SwsContext *c, const int16_t *buf[2],
  577. const int16_t *ubuf[2], const int16_t *vbuf[2],
  578. const int16_t *abuf[2], uint8_t *dest, int dstW,
  579. int yalpha, int uvalpha, int y,
  580. enum PixelFormat target)
  581. {
  582. const int16_t *buf0 = buf[0], *buf1 = buf[1];
  583. const uint8_t * const d128 = dither_8x8_220[y & 7];
  584. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  585. int yalpha1 = 4095 - yalpha;
  586. int i;
  587. for (i = 0; i < dstW - 7; i += 8) {
  588. int acc = g[((buf0[i ] * yalpha1 + buf1[i ] * yalpha) >> 19) + d128[0]];
  589. acc += acc + g[((buf0[i + 1] * yalpha1 + buf1[i + 1] * yalpha) >> 19) + d128[1]];
  590. acc += acc + g[((buf0[i + 2] * yalpha1 + buf1[i + 2] * yalpha) >> 19) + d128[2]];
  591. acc += acc + g[((buf0[i + 3] * yalpha1 + buf1[i + 3] * yalpha) >> 19) + d128[3]];
  592. acc += acc + g[((buf0[i + 4] * yalpha1 + buf1[i + 4] * yalpha) >> 19) + d128[4]];
  593. acc += acc + g[((buf0[i + 5] * yalpha1 + buf1[i + 5] * yalpha) >> 19) + d128[5]];
  594. acc += acc + g[((buf0[i + 6] * yalpha1 + buf1[i + 6] * yalpha) >> 19) + d128[6]];
  595. acc += acc + g[((buf0[i + 7] * yalpha1 + buf1[i + 7] * yalpha) >> 19) + d128[7]];
  596. output_pixel(*dest++, acc);
  597. }
  598. }
  599. static av_always_inline void
  600. yuv2mono_1_c_template(SwsContext *c, const int16_t *buf0,
  601. const int16_t *ubuf[2], const int16_t *vbuf[2],
  602. const int16_t *abuf0, uint8_t *dest, int dstW,
  603. int uvalpha, int y, enum PixelFormat target)
  604. {
  605. const uint8_t * const d128 = dither_8x8_220[y & 7];
  606. uint8_t *g = c->table_gU[128] + c->table_gV[128];
  607. int i;
  608. for (i = 0; i < dstW - 7; i += 8) {
  609. int acc = g[(buf0[i ] >> 7) + d128[0]];
  610. acc += acc + g[(buf0[i + 1] >> 7) + d128[1]];
  611. acc += acc + g[(buf0[i + 2] >> 7) + d128[2]];
  612. acc += acc + g[(buf0[i + 3] >> 7) + d128[3]];
  613. acc += acc + g[(buf0[i + 4] >> 7) + d128[4]];
  614. acc += acc + g[(buf0[i + 5] >> 7) + d128[5]];
  615. acc += acc + g[(buf0[i + 6] >> 7) + d128[6]];
  616. acc += acc + g[(buf0[i + 7] >> 7) + d128[7]];
  617. output_pixel(*dest++, acc);
  618. }
  619. }
  620. #undef output_pixel
  621. #define YUV2PACKEDWRAPPER(name, base, ext, fmt) \
  622. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  623. const int16_t **lumSrc, int lumFilterSize, \
  624. const int16_t *chrFilter, const int16_t **chrUSrc, \
  625. const int16_t **chrVSrc, int chrFilterSize, \
  626. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  627. int y) \
  628. { \
  629. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  630. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  631. alpSrc, dest, dstW, y, fmt); \
  632. } \
  633. \
  634. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  635. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  636. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  637. int yalpha, int uvalpha, int y) \
  638. { \
  639. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  640. dest, dstW, yalpha, uvalpha, y, fmt); \
  641. } \
  642. \
  643. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  644. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  645. const int16_t *abuf0, uint8_t *dest, int dstW, \
  646. int uvalpha, int y) \
  647. { \
  648. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, \
  649. abuf0, dest, dstW, uvalpha, \
  650. y, fmt); \
  651. }
  652. YUV2PACKEDWRAPPER(yuv2mono,, white, PIX_FMT_MONOWHITE);
  653. YUV2PACKEDWRAPPER(yuv2mono,, black, PIX_FMT_MONOBLACK);
  654. #define output_pixels(pos, Y1, U, Y2, V) \
  655. if (target == PIX_FMT_YUYV422) { \
  656. dest[pos + 0] = Y1; \
  657. dest[pos + 1] = U; \
  658. dest[pos + 2] = Y2; \
  659. dest[pos + 3] = V; \
  660. } else { \
  661. dest[pos + 0] = U; \
  662. dest[pos + 1] = Y1; \
  663. dest[pos + 2] = V; \
  664. dest[pos + 3] = Y2; \
  665. }
  666. static av_always_inline void
  667. yuv2422_X_c_template(SwsContext *c, const int16_t *lumFilter,
  668. const int16_t **lumSrc, int lumFilterSize,
  669. const int16_t *chrFilter, const int16_t **chrUSrc,
  670. const int16_t **chrVSrc, int chrFilterSize,
  671. const int16_t **alpSrc, uint8_t *dest, int dstW,
  672. int y, enum PixelFormat target)
  673. {
  674. int i;
  675. for (i = 0; i < (dstW >> 1); i++) {
  676. int j;
  677. int Y1 = 1 << 18;
  678. int Y2 = 1 << 18;
  679. int U = 1 << 18;
  680. int V = 1 << 18;
  681. for (j = 0; j < lumFilterSize; j++) {
  682. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  683. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  684. }
  685. for (j = 0; j < chrFilterSize; j++) {
  686. U += chrUSrc[j][i] * chrFilter[j];
  687. V += chrVSrc[j][i] * chrFilter[j];
  688. }
  689. Y1 >>= 19;
  690. Y2 >>= 19;
  691. U >>= 19;
  692. V >>= 19;
  693. if ((Y1 | Y2 | U | V) & 0x100) {
  694. Y1 = av_clip_uint8(Y1);
  695. Y2 = av_clip_uint8(Y2);
  696. U = av_clip_uint8(U);
  697. V = av_clip_uint8(V);
  698. }
  699. output_pixels(4*i, Y1, U, Y2, V);
  700. }
  701. }
  702. static av_always_inline void
  703. yuv2422_2_c_template(SwsContext *c, const int16_t *buf[2],
  704. const int16_t *ubuf[2], const int16_t *vbuf[2],
  705. const int16_t *abuf[2], uint8_t *dest, int dstW,
  706. int yalpha, int uvalpha, int y,
  707. enum PixelFormat target)
  708. {
  709. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  710. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  711. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  712. int yalpha1 = 4095 - yalpha;
  713. int uvalpha1 = 4095 - uvalpha;
  714. int i;
  715. for (i = 0; i < (dstW >> 1); i++) {
  716. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  717. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  718. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  719. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  720. output_pixels(i * 4, Y1, U, Y2, V);
  721. }
  722. }
  723. static av_always_inline void
  724. yuv2422_1_c_template(SwsContext *c, const int16_t *buf0,
  725. const int16_t *ubuf[2], const int16_t *vbuf[2],
  726. const int16_t *abuf0, uint8_t *dest, int dstW,
  727. int uvalpha, int y, enum PixelFormat target)
  728. {
  729. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  730. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  731. int i;
  732. if (uvalpha < 2048) {
  733. for (i = 0; i < (dstW >> 1); i++) {
  734. int Y1 = buf0[i * 2] >> 7;
  735. int Y2 = buf0[i * 2 + 1] >> 7;
  736. int U = ubuf1[i] >> 7;
  737. int V = vbuf1[i] >> 7;
  738. output_pixels(i * 4, Y1, U, Y2, V);
  739. }
  740. } else {
  741. for (i = 0; i < (dstW >> 1); i++) {
  742. int Y1 = buf0[i * 2] >> 7;
  743. int Y2 = buf0[i * 2 + 1] >> 7;
  744. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  745. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  746. output_pixels(i * 4, Y1, U, Y2, V);
  747. }
  748. }
  749. }
  750. #undef output_pixels
  751. YUV2PACKEDWRAPPER(yuv2, 422, yuyv422, PIX_FMT_YUYV422);
  752. YUV2PACKEDWRAPPER(yuv2, 422, uyvy422, PIX_FMT_UYVY422);
  753. #define R_B ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? R : B)
  754. #define B_R ((target == PIX_FMT_RGB48LE || target == PIX_FMT_RGB48BE) ? B : R)
  755. #define output_pixel(pos, val) \
  756. if (isBE(target)) { \
  757. AV_WB16(pos, val); \
  758. } else { \
  759. AV_WL16(pos, val); \
  760. }
  761. static av_always_inline void
  762. yuv2rgb48_X_c_template(SwsContext *c, const int16_t *lumFilter,
  763. const int32_t **lumSrc, int lumFilterSize,
  764. const int16_t *chrFilter, const int32_t **chrUSrc,
  765. const int32_t **chrVSrc, int chrFilterSize,
  766. const int32_t **alpSrc, uint16_t *dest, int dstW,
  767. int y, enum PixelFormat target)
  768. {
  769. int i;
  770. for (i = 0; i < (dstW >> 1); i++) {
  771. int j;
  772. int Y1 = 0;
  773. int Y2 = 0;
  774. int U = -128 << 23; // 19
  775. int V = -128 << 23;
  776. int R, G, B;
  777. for (j = 0; j < lumFilterSize; j++) {
  778. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  779. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  780. }
  781. for (j = 0; j < chrFilterSize; j++) {
  782. U += chrUSrc[j][i] * chrFilter[j];
  783. V += chrVSrc[j][i] * chrFilter[j];
  784. }
  785. // 8bit: 12+15=27; 16-bit: 12+19=31
  786. Y1 >>= 14; // 10
  787. Y2 >>= 14;
  788. U >>= 14;
  789. V >>= 14;
  790. // 8bit: 27 -> 17bit, 16bit: 31 - 14 = 17bit
  791. Y1 -= c->yuv2rgb_y_offset;
  792. Y2 -= c->yuv2rgb_y_offset;
  793. Y1 *= c->yuv2rgb_y_coeff;
  794. Y2 *= c->yuv2rgb_y_coeff;
  795. Y1 += 1 << 13; // 21
  796. Y2 += 1 << 13;
  797. // 8bit: 17 + 13bit = 30bit, 16bit: 17 + 13bit = 30bit
  798. R = V * c->yuv2rgb_v2r_coeff;
  799. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  800. B = U * c->yuv2rgb_u2b_coeff;
  801. // 8bit: 30 - 22 = 8bit, 16bit: 30bit - 14 = 16bit
  802. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  803. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  804. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  805. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  806. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  807. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  808. dest += 6;
  809. }
  810. }
  811. static av_always_inline void
  812. yuv2rgb48_2_c_template(SwsContext *c, const int32_t *buf[2],
  813. const int32_t *ubuf[2], const int32_t *vbuf[2],
  814. const int32_t *abuf[2], uint16_t *dest, int dstW,
  815. int yalpha, int uvalpha, int y,
  816. enum PixelFormat target)
  817. {
  818. const int32_t *buf0 = buf[0], *buf1 = buf[1],
  819. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  820. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  821. int yalpha1 = 4095 - yalpha;
  822. int uvalpha1 = 4095 - uvalpha;
  823. int i;
  824. for (i = 0; i < (dstW >> 1); i++) {
  825. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 14;
  826. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 14;
  827. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha + (-128 << 23)) >> 14;
  828. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha + (-128 << 23)) >> 14;
  829. int R, G, B;
  830. Y1 -= c->yuv2rgb_y_offset;
  831. Y2 -= c->yuv2rgb_y_offset;
  832. Y1 *= c->yuv2rgb_y_coeff;
  833. Y2 *= c->yuv2rgb_y_coeff;
  834. Y1 += 1 << 13;
  835. Y2 += 1 << 13;
  836. R = V * c->yuv2rgb_v2r_coeff;
  837. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  838. B = U * c->yuv2rgb_u2b_coeff;
  839. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  840. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  841. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  842. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  843. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  844. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  845. dest += 6;
  846. }
  847. }
  848. static av_always_inline void
  849. yuv2rgb48_1_c_template(SwsContext *c, const int32_t *buf0,
  850. const int32_t *ubuf[2], const int32_t *vbuf[2],
  851. const int32_t *abuf0, uint16_t *dest, int dstW,
  852. int uvalpha, int y, enum PixelFormat target)
  853. {
  854. const int32_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  855. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  856. int i;
  857. if (uvalpha < 2048) {
  858. for (i = 0; i < (dstW >> 1); i++) {
  859. int Y1 = (buf0[i * 2] ) >> 2;
  860. int Y2 = (buf0[i * 2 + 1]) >> 2;
  861. int U = (ubuf0[i] + (-128 << 11)) >> 2;
  862. int V = (vbuf0[i] + (-128 << 11)) >> 2;
  863. int R, G, B;
  864. Y1 -= c->yuv2rgb_y_offset;
  865. Y2 -= c->yuv2rgb_y_offset;
  866. Y1 *= c->yuv2rgb_y_coeff;
  867. Y2 *= c->yuv2rgb_y_coeff;
  868. Y1 += 1 << 13;
  869. Y2 += 1 << 13;
  870. R = V * c->yuv2rgb_v2r_coeff;
  871. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  872. B = U * c->yuv2rgb_u2b_coeff;
  873. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  874. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  875. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  876. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  877. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  878. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  879. dest += 6;
  880. }
  881. } else {
  882. for (i = 0; i < (dstW >> 1); i++) {
  883. int Y1 = (buf0[i * 2] ) >> 2;
  884. int Y2 = (buf0[i * 2 + 1]) >> 2;
  885. int U = (ubuf0[i] + ubuf1[i] + (-128 << 12)) >> 3;
  886. int V = (vbuf0[i] + vbuf1[i] + (-128 << 12)) >> 3;
  887. int R, G, B;
  888. Y1 -= c->yuv2rgb_y_offset;
  889. Y2 -= c->yuv2rgb_y_offset;
  890. Y1 *= c->yuv2rgb_y_coeff;
  891. Y2 *= c->yuv2rgb_y_coeff;
  892. Y1 += 1 << 13;
  893. Y2 += 1 << 13;
  894. R = V * c->yuv2rgb_v2r_coeff;
  895. G = V * c->yuv2rgb_v2g_coeff + U * c->yuv2rgb_u2g_coeff;
  896. B = U * c->yuv2rgb_u2b_coeff;
  897. output_pixel(&dest[0], av_clip_uintp2(R_B + Y1, 30) >> 14);
  898. output_pixel(&dest[1], av_clip_uintp2( G + Y1, 30) >> 14);
  899. output_pixel(&dest[2], av_clip_uintp2(B_R + Y1, 30) >> 14);
  900. output_pixel(&dest[3], av_clip_uintp2(R_B + Y2, 30) >> 14);
  901. output_pixel(&dest[4], av_clip_uintp2( G + Y2, 30) >> 14);
  902. output_pixel(&dest[5], av_clip_uintp2(B_R + Y2, 30) >> 14);
  903. dest += 6;
  904. }
  905. }
  906. }
  907. #undef output_pixel
  908. #undef r_b
  909. #undef b_r
  910. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48be, PIX_FMT_RGB48BE);
  911. YUV2PACKED16WRAPPER(yuv2, rgb48, rgb48le, PIX_FMT_RGB48LE);
  912. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48be, PIX_FMT_BGR48BE);
  913. YUV2PACKED16WRAPPER(yuv2, rgb48, bgr48le, PIX_FMT_BGR48LE);
  914. static av_always_inline void
  915. yuv2rgb_write(uint8_t *_dest, int i, int Y1, int Y2,
  916. int U, int V, int A1, int A2,
  917. const void *_r, const void *_g, const void *_b, int y,
  918. enum PixelFormat target, int hasAlpha)
  919. {
  920. if (target == PIX_FMT_ARGB || target == PIX_FMT_RGBA ||
  921. target == PIX_FMT_ABGR || target == PIX_FMT_BGRA) {
  922. uint32_t *dest = (uint32_t *) _dest;
  923. const uint32_t *r = (const uint32_t *) _r;
  924. const uint32_t *g = (const uint32_t *) _g;
  925. const uint32_t *b = (const uint32_t *) _b;
  926. #if CONFIG_SMALL
  927. int sh = hasAlpha ? ((target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24) : 0;
  928. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (hasAlpha ? A1 << sh : 0);
  929. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (hasAlpha ? A2 << sh : 0);
  930. #else
  931. if (hasAlpha) {
  932. int sh = (target == PIX_FMT_RGB32_1 || target == PIX_FMT_BGR32_1) ? 0 : 24;
  933. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1] + (A1 << sh);
  934. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2] + (A2 << sh);
  935. } else {
  936. dest[i * 2 + 0] = r[Y1] + g[Y1] + b[Y1];
  937. dest[i * 2 + 1] = r[Y2] + g[Y2] + b[Y2];
  938. }
  939. #endif
  940. } else if (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) {
  941. uint8_t *dest = (uint8_t *) _dest;
  942. const uint8_t *r = (const uint8_t *) _r;
  943. const uint8_t *g = (const uint8_t *) _g;
  944. const uint8_t *b = (const uint8_t *) _b;
  945. #define r_b ((target == PIX_FMT_RGB24) ? r : b)
  946. #define b_r ((target == PIX_FMT_RGB24) ? b : r)
  947. dest[i * 6 + 0] = r_b[Y1];
  948. dest[i * 6 + 1] = g[Y1];
  949. dest[i * 6 + 2] = b_r[Y1];
  950. dest[i * 6 + 3] = r_b[Y2];
  951. dest[i * 6 + 4] = g[Y2];
  952. dest[i * 6 + 5] = b_r[Y2];
  953. #undef r_b
  954. #undef b_r
  955. } else if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565 ||
  956. target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555 ||
  957. target == PIX_FMT_RGB444 || target == PIX_FMT_BGR444) {
  958. uint16_t *dest = (uint16_t *) _dest;
  959. const uint16_t *r = (const uint16_t *) _r;
  960. const uint16_t *g = (const uint16_t *) _g;
  961. const uint16_t *b = (const uint16_t *) _b;
  962. int dr1, dg1, db1, dr2, dg2, db2;
  963. if (target == PIX_FMT_RGB565 || target == PIX_FMT_BGR565) {
  964. dr1 = dither_2x2_8[ y & 1 ][0];
  965. dg1 = dither_2x2_4[ y & 1 ][0];
  966. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  967. dr2 = dither_2x2_8[ y & 1 ][1];
  968. dg2 = dither_2x2_4[ y & 1 ][1];
  969. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  970. } else if (target == PIX_FMT_RGB555 || target == PIX_FMT_BGR555) {
  971. dr1 = dither_2x2_8[ y & 1 ][0];
  972. dg1 = dither_2x2_8[ y & 1 ][1];
  973. db1 = dither_2x2_8[(y & 1) ^ 1][0];
  974. dr2 = dither_2x2_8[ y & 1 ][1];
  975. dg2 = dither_2x2_8[ y & 1 ][0];
  976. db2 = dither_2x2_8[(y & 1) ^ 1][1];
  977. } else {
  978. dr1 = dither_4x4_16[ y & 3 ][0];
  979. dg1 = dither_4x4_16[ y & 3 ][1];
  980. db1 = dither_4x4_16[(y & 3) ^ 3][0];
  981. dr2 = dither_4x4_16[ y & 3 ][1];
  982. dg2 = dither_4x4_16[ y & 3 ][0];
  983. db2 = dither_4x4_16[(y & 3) ^ 3][1];
  984. }
  985. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  986. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  987. } else /* 8/4-bit */ {
  988. uint8_t *dest = (uint8_t *) _dest;
  989. const uint8_t *r = (const uint8_t *) _r;
  990. const uint8_t *g = (const uint8_t *) _g;
  991. const uint8_t *b = (const uint8_t *) _b;
  992. int dr1, dg1, db1, dr2, dg2, db2;
  993. if (target == PIX_FMT_RGB8 || target == PIX_FMT_BGR8) {
  994. const uint8_t * const d64 = dither_8x8_73[y & 7];
  995. const uint8_t * const d32 = dither_8x8_32[y & 7];
  996. dr1 = dg1 = d32[(i * 2 + 0) & 7];
  997. db1 = d64[(i * 2 + 0) & 7];
  998. dr2 = dg2 = d32[(i * 2 + 1) & 7];
  999. db2 = d64[(i * 2 + 1) & 7];
  1000. } else {
  1001. const uint8_t * const d64 = dither_8x8_73 [y & 7];
  1002. const uint8_t * const d128 = dither_8x8_220[y & 7];
  1003. dr1 = db1 = d128[(i * 2 + 0) & 7];
  1004. dg1 = d64[(i * 2 + 0) & 7];
  1005. dr2 = db2 = d128[(i * 2 + 1) & 7];
  1006. dg2 = d64[(i * 2 + 1) & 7];
  1007. }
  1008. if (target == PIX_FMT_RGB4 || target == PIX_FMT_BGR4) {
  1009. dest[i] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1] +
  1010. ((r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2]) << 4);
  1011. } else {
  1012. dest[i * 2 + 0] = r[Y1 + dr1] + g[Y1 + dg1] + b[Y1 + db1];
  1013. dest[i * 2 + 1] = r[Y2 + dr2] + g[Y2 + dg2] + b[Y2 + db2];
  1014. }
  1015. }
  1016. }
  1017. static av_always_inline void
  1018. yuv2rgb_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1019. const int16_t **lumSrc, int lumFilterSize,
  1020. const int16_t *chrFilter, const int16_t **chrUSrc,
  1021. const int16_t **chrVSrc, int chrFilterSize,
  1022. const int16_t **alpSrc, uint8_t *dest, int dstW,
  1023. int y, enum PixelFormat target, int hasAlpha)
  1024. {
  1025. int i;
  1026. for (i = 0; i < (dstW >> 1); i++) {
  1027. int j;
  1028. int Y1 = 1 << 18;
  1029. int Y2 = 1 << 18;
  1030. int U = 1 << 18;
  1031. int V = 1 << 18;
  1032. int av_unused A1, A2;
  1033. const void *r, *g, *b;
  1034. for (j = 0; j < lumFilterSize; j++) {
  1035. Y1 += lumSrc[j][i * 2] * lumFilter[j];
  1036. Y2 += lumSrc[j][i * 2 + 1] * lumFilter[j];
  1037. }
  1038. for (j = 0; j < chrFilterSize; j++) {
  1039. U += chrUSrc[j][i] * chrFilter[j];
  1040. V += chrVSrc[j][i] * chrFilter[j];
  1041. }
  1042. Y1 >>= 19;
  1043. Y2 >>= 19;
  1044. U >>= 19;
  1045. V >>= 19;
  1046. if ((Y1 | Y2 | U | V) & 0x100) {
  1047. Y1 = av_clip_uint8(Y1);
  1048. Y2 = av_clip_uint8(Y2);
  1049. U = av_clip_uint8(U);
  1050. V = av_clip_uint8(V);
  1051. }
  1052. if (hasAlpha) {
  1053. A1 = 1 << 18;
  1054. A2 = 1 << 18;
  1055. for (j = 0; j < lumFilterSize; j++) {
  1056. A1 += alpSrc[j][i * 2 ] * lumFilter[j];
  1057. A2 += alpSrc[j][i * 2 + 1] * lumFilter[j];
  1058. }
  1059. A1 >>= 19;
  1060. A2 >>= 19;
  1061. if ((A1 | A2) & 0x100) {
  1062. A1 = av_clip_uint8(A1);
  1063. A2 = av_clip_uint8(A2);
  1064. }
  1065. }
  1066. /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/
  1067. r = c->table_rV[V];
  1068. g = (c->table_gU[U] + c->table_gV[V]);
  1069. b = c->table_bU[U];
  1070. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1071. r, g, b, y, target, hasAlpha);
  1072. }
  1073. }
  1074. static av_always_inline void
  1075. yuv2rgb_2_c_template(SwsContext *c, const int16_t *buf[2],
  1076. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1077. const int16_t *abuf[2], uint8_t *dest, int dstW,
  1078. int yalpha, int uvalpha, int y,
  1079. enum PixelFormat target, int hasAlpha)
  1080. {
  1081. const int16_t *buf0 = buf[0], *buf1 = buf[1],
  1082. *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1083. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1],
  1084. *abuf0 = hasAlpha ? abuf[0] : NULL,
  1085. *abuf1 = hasAlpha ? abuf[1] : NULL;
  1086. int yalpha1 = 4095 - yalpha;
  1087. int uvalpha1 = 4095 - uvalpha;
  1088. int i;
  1089. for (i = 0; i < (dstW >> 1); i++) {
  1090. int Y1 = (buf0[i * 2] * yalpha1 + buf1[i * 2] * yalpha) >> 19;
  1091. int Y2 = (buf0[i * 2 + 1] * yalpha1 + buf1[i * 2 + 1] * yalpha) >> 19;
  1092. int U = (ubuf0[i] * uvalpha1 + ubuf1[i] * uvalpha) >> 19;
  1093. int V = (vbuf0[i] * uvalpha1 + vbuf1[i] * uvalpha) >> 19;
  1094. int A1, A2;
  1095. const void *r = c->table_rV[V],
  1096. *g = (c->table_gU[U] + c->table_gV[V]),
  1097. *b = c->table_bU[U];
  1098. if (hasAlpha) {
  1099. A1 = (abuf0[i * 2 ] * yalpha1 + abuf1[i * 2 ] * yalpha) >> 19;
  1100. A2 = (abuf0[i * 2 + 1] * yalpha1 + abuf1[i * 2 + 1] * yalpha) >> 19;
  1101. }
  1102. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1103. r, g, b, y, target, hasAlpha);
  1104. }
  1105. }
  1106. static av_always_inline void
  1107. yuv2rgb_1_c_template(SwsContext *c, const int16_t *buf0,
  1108. const int16_t *ubuf[2], const int16_t *vbuf[2],
  1109. const int16_t *abuf0, uint8_t *dest, int dstW,
  1110. int uvalpha, int y, enum PixelFormat target,
  1111. int hasAlpha)
  1112. {
  1113. const int16_t *ubuf0 = ubuf[0], *ubuf1 = ubuf[1],
  1114. *vbuf0 = vbuf[0], *vbuf1 = vbuf[1];
  1115. int i;
  1116. if (uvalpha < 2048) {
  1117. for (i = 0; i < (dstW >> 1); i++) {
  1118. int Y1 = buf0[i * 2] >> 7;
  1119. int Y2 = buf0[i * 2 + 1] >> 7;
  1120. int U = ubuf1[i] >> 7;
  1121. int V = vbuf1[i] >> 7;
  1122. int A1, A2;
  1123. const void *r = c->table_rV[V],
  1124. *g = (c->table_gU[U] + c->table_gV[V]),
  1125. *b = c->table_bU[U];
  1126. if (hasAlpha) {
  1127. A1 = abuf0[i * 2 ] >> 7;
  1128. A2 = abuf0[i * 2 + 1] >> 7;
  1129. }
  1130. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1131. r, g, b, y, target, hasAlpha);
  1132. }
  1133. } else {
  1134. for (i = 0; i < (dstW >> 1); i++) {
  1135. int Y1 = buf0[i * 2] >> 7;
  1136. int Y2 = buf0[i * 2 + 1] >> 7;
  1137. int U = (ubuf0[i] + ubuf1[i]) >> 8;
  1138. int V = (vbuf0[i] + vbuf1[i]) >> 8;
  1139. int A1, A2;
  1140. const void *r = c->table_rV[V],
  1141. *g = (c->table_gU[U] + c->table_gV[V]),
  1142. *b = c->table_bU[U];
  1143. if (hasAlpha) {
  1144. A1 = abuf0[i * 2 ] >> 7;
  1145. A2 = abuf0[i * 2 + 1] >> 7;
  1146. }
  1147. yuv2rgb_write(dest, i, Y1, Y2, U, V, hasAlpha ? A1 : 0, hasAlpha ? A2 : 0,
  1148. r, g, b, y, target, hasAlpha);
  1149. }
  1150. }
  1151. }
  1152. #define YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1153. static void name ## ext ## _X_c(SwsContext *c, const int16_t *lumFilter, \
  1154. const int16_t **lumSrc, int lumFilterSize, \
  1155. const int16_t *chrFilter, const int16_t **chrUSrc, \
  1156. const int16_t **chrVSrc, int chrFilterSize, \
  1157. const int16_t **alpSrc, uint8_t *dest, int dstW, \
  1158. int y) \
  1159. { \
  1160. name ## base ## _X_c_template(c, lumFilter, lumSrc, lumFilterSize, \
  1161. chrFilter, chrUSrc, chrVSrc, chrFilterSize, \
  1162. alpSrc, dest, dstW, y, fmt, hasAlpha); \
  1163. }
  1164. #define YUV2RGBWRAPPER(name, base, ext, fmt, hasAlpha) \
  1165. YUV2RGBWRAPPERX(name, base, ext, fmt, hasAlpha) \
  1166. static void name ## ext ## _2_c(SwsContext *c, const int16_t *buf[2], \
  1167. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1168. const int16_t *abuf[2], uint8_t *dest, int dstW, \
  1169. int yalpha, int uvalpha, int y) \
  1170. { \
  1171. name ## base ## _2_c_template(c, buf, ubuf, vbuf, abuf, \
  1172. dest, dstW, yalpha, uvalpha, y, fmt, hasAlpha); \
  1173. } \
  1174. \
  1175. static void name ## ext ## _1_c(SwsContext *c, const int16_t *buf0, \
  1176. const int16_t *ubuf[2], const int16_t *vbuf[2], \
  1177. const int16_t *abuf0, uint8_t *dest, int dstW, \
  1178. int uvalpha, int y) \
  1179. { \
  1180. name ## base ## _1_c_template(c, buf0, ubuf, vbuf, abuf0, dest, \
  1181. dstW, uvalpha, y, fmt, hasAlpha); \
  1182. }
  1183. #if CONFIG_SMALL
  1184. YUV2RGBWRAPPER(yuv2rgb,, 32_1, PIX_FMT_RGB32_1, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1185. YUV2RGBWRAPPER(yuv2rgb,, 32, PIX_FMT_RGB32, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1186. #else
  1187. #if CONFIG_SWSCALE_ALPHA
  1188. YUV2RGBWRAPPER(yuv2rgb,, a32_1, PIX_FMT_RGB32_1, 1);
  1189. YUV2RGBWRAPPER(yuv2rgb,, a32, PIX_FMT_RGB32, 1);
  1190. #endif
  1191. YUV2RGBWRAPPER(yuv2rgb,, x32_1, PIX_FMT_RGB32_1, 0);
  1192. YUV2RGBWRAPPER(yuv2rgb,, x32, PIX_FMT_RGB32, 0);
  1193. #endif
  1194. YUV2RGBWRAPPER(yuv2, rgb, rgb24, PIX_FMT_RGB24, 0);
  1195. YUV2RGBWRAPPER(yuv2, rgb, bgr24, PIX_FMT_BGR24, 0);
  1196. YUV2RGBWRAPPER(yuv2rgb,, 16, PIX_FMT_RGB565, 0);
  1197. YUV2RGBWRAPPER(yuv2rgb,, 15, PIX_FMT_RGB555, 0);
  1198. YUV2RGBWRAPPER(yuv2rgb,, 12, PIX_FMT_RGB444, 0);
  1199. YUV2RGBWRAPPER(yuv2rgb,, 8, PIX_FMT_RGB8, 0);
  1200. YUV2RGBWRAPPER(yuv2rgb,, 4, PIX_FMT_RGB4, 0);
  1201. YUV2RGBWRAPPER(yuv2rgb,, 4b, PIX_FMT_RGB4_BYTE, 0);
  1202. static av_always_inline void
  1203. yuv2rgb_full_X_c_template(SwsContext *c, const int16_t *lumFilter,
  1204. const int16_t **lumSrc, int lumFilterSize,
  1205. const int16_t *chrFilter, const int16_t **chrUSrc,
  1206. const int16_t **chrVSrc, int chrFilterSize,
  1207. const int16_t **alpSrc, uint8_t *dest,
  1208. int dstW, int y, enum PixelFormat target, int hasAlpha)
  1209. {
  1210. int i;
  1211. int step = (target == PIX_FMT_RGB24 || target == PIX_FMT_BGR24) ? 3 : 4;
  1212. for (i = 0; i < dstW; i++) {
  1213. int j;
  1214. int Y = 1<<9;
  1215. int U = (1<<9)-(128 << 19);
  1216. int V = (1<<9)-(128 << 19);
  1217. int av_unused A;
  1218. int R, G, B;
  1219. for (j = 0; j < lumFilterSize; j++) {
  1220. Y += lumSrc[j][i] * lumFilter[j];
  1221. }
  1222. for (j = 0; j < chrFilterSize; j++) {
  1223. U += chrUSrc[j][i] * chrFilter[j];
  1224. V += chrVSrc[j][i] * chrFilter[j];
  1225. }
  1226. Y >>= 10;
  1227. U >>= 10;
  1228. V >>= 10;
  1229. if (hasAlpha) {
  1230. A = 1 << 18;
  1231. for (j = 0; j < lumFilterSize; j++) {
  1232. A += alpSrc[j][i] * lumFilter[j];
  1233. }
  1234. A >>= 19;
  1235. if (A & 0x100)
  1236. A = av_clip_uint8(A);
  1237. }
  1238. Y -= c->yuv2rgb_y_offset;
  1239. Y *= c->yuv2rgb_y_coeff;
  1240. Y += 1 << 21;
  1241. R = Y + V*c->yuv2rgb_v2r_coeff;
  1242. G = Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;
  1243. B = Y + U*c->yuv2rgb_u2b_coeff;
  1244. if ((R | G | B) & 0xC0000000) {
  1245. R = av_clip_uintp2(R, 30);
  1246. G = av_clip_uintp2(G, 30);
  1247. B = av_clip_uintp2(B, 30);
  1248. }
  1249. switch(target) {
  1250. case PIX_FMT_ARGB:
  1251. dest[0] = hasAlpha ? A : 255;
  1252. dest[1] = R >> 22;
  1253. dest[2] = G >> 22;
  1254. dest[3] = B >> 22;
  1255. break;
  1256. case PIX_FMT_RGB24:
  1257. dest[0] = R >> 22;
  1258. dest[1] = G >> 22;
  1259. dest[2] = B >> 22;
  1260. break;
  1261. case PIX_FMT_RGBA:
  1262. dest[0] = R >> 22;
  1263. dest[1] = G >> 22;
  1264. dest[2] = B >> 22;
  1265. dest[3] = hasAlpha ? A : 255;
  1266. break;
  1267. case PIX_FMT_ABGR:
  1268. dest[0] = hasAlpha ? A : 255;
  1269. dest[1] = B >> 22;
  1270. dest[2] = G >> 22;
  1271. dest[3] = R >> 22;
  1272. break;
  1273. case PIX_FMT_BGR24:
  1274. dest[0] = B >> 22;
  1275. dest[1] = G >> 22;
  1276. dest[2] = R >> 22;
  1277. break;
  1278. case PIX_FMT_BGRA:
  1279. dest[0] = B >> 22;
  1280. dest[1] = G >> 22;
  1281. dest[2] = R >> 22;
  1282. dest[3] = hasAlpha ? A : 255;
  1283. break;
  1284. }
  1285. dest += step;
  1286. }
  1287. }
  1288. #if CONFIG_SMALL
  1289. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1290. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1291. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1292. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, CONFIG_SWSCALE_ALPHA && c->alpPixBuf);
  1293. #else
  1294. #if CONFIG_SWSCALE_ALPHA
  1295. YUV2RGBWRAPPERX(yuv2, rgb_full, bgra32_full, PIX_FMT_BGRA, 1);
  1296. YUV2RGBWRAPPERX(yuv2, rgb_full, abgr32_full, PIX_FMT_ABGR, 1);
  1297. YUV2RGBWRAPPERX(yuv2, rgb_full, rgba32_full, PIX_FMT_RGBA, 1);
  1298. YUV2RGBWRAPPERX(yuv2, rgb_full, argb32_full, PIX_FMT_ARGB, 1);
  1299. #endif
  1300. YUV2RGBWRAPPERX(yuv2, rgb_full, bgrx32_full, PIX_FMT_BGRA, 0);
  1301. YUV2RGBWRAPPERX(yuv2, rgb_full, xbgr32_full, PIX_FMT_ABGR, 0);
  1302. YUV2RGBWRAPPERX(yuv2, rgb_full, rgbx32_full, PIX_FMT_RGBA, 0);
  1303. YUV2RGBWRAPPERX(yuv2, rgb_full, xrgb32_full, PIX_FMT_ARGB, 0);
  1304. #endif
  1305. YUV2RGBWRAPPERX(yuv2, rgb_full, bgr24_full, PIX_FMT_BGR24, 0);
  1306. YUV2RGBWRAPPERX(yuv2, rgb_full, rgb24_full, PIX_FMT_RGB24, 0);
  1307. static av_always_inline void fillPlane(uint8_t* plane, int stride,
  1308. int width, int height,
  1309. int y, uint8_t val)
  1310. {
  1311. int i;
  1312. uint8_t *ptr = plane + stride*y;
  1313. for (i=0; i<height; i++) {
  1314. memset(ptr, val, width);
  1315. ptr += stride;
  1316. }
  1317. }
  1318. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1319. #define r ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? b_r : r_b)
  1320. #define b ((origin == PIX_FMT_BGR48BE || origin == PIX_FMT_BGR48LE) ? r_b : b_r)
  1321. static av_always_inline void
  1322. rgb48ToY_c_template(uint16_t *dst, const uint16_t *src, int width,
  1323. enum PixelFormat origin)
  1324. {
  1325. int i;
  1326. for (i = 0; i < width; i++) {
  1327. unsigned int r_b = input_pixel(&src[i*3+0]);
  1328. unsigned int g = input_pixel(&src[i*3+1]);
  1329. unsigned int b_r = input_pixel(&src[i*3+2]);
  1330. dst[i] = (RY*r + GY*g + BY*b + (0x2001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1331. }
  1332. }
  1333. static av_always_inline void
  1334. rgb48ToUV_c_template(uint16_t *dstU, uint16_t *dstV,
  1335. const uint16_t *src1, const uint16_t *src2,
  1336. int width, enum PixelFormat origin)
  1337. {
  1338. int i;
  1339. assert(src1==src2);
  1340. for (i = 0; i < width; i++) {
  1341. int r_b = input_pixel(&src1[i*3+0]);
  1342. int g = input_pixel(&src1[i*3+1]);
  1343. int b_r = input_pixel(&src1[i*3+2]);
  1344. dstU[i] = (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1345. dstV[i] = (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1346. }
  1347. }
  1348. static av_always_inline void
  1349. rgb48ToUV_half_c_template(uint16_t *dstU, uint16_t *dstV,
  1350. const uint16_t *src1, const uint16_t *src2,
  1351. int width, enum PixelFormat origin)
  1352. {
  1353. int i;
  1354. assert(src1==src2);
  1355. for (i = 0; i < width; i++) {
  1356. int r_b = (input_pixel(&src1[6 * i + 0]) + input_pixel(&src1[6 * i + 3]) + 1) >> 1;
  1357. int g = (input_pixel(&src1[6 * i + 1]) + input_pixel(&src1[6 * i + 4]) + 1) >> 1;
  1358. int b_r = (input_pixel(&src1[6 * i + 2]) + input_pixel(&src1[6 * i + 5]) + 1) >> 1;
  1359. dstU[i]= (RU*r + GU*g + BU*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1360. dstV[i]= (RV*r + GV*g + BV*b + (0x10001<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  1361. }
  1362. }
  1363. #undef r
  1364. #undef b
  1365. #undef input_pixel
  1366. #define rgb48funcs(pattern, BE_LE, origin) \
  1367. static void pattern ## 48 ## BE_LE ## ToY_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused0, const uint8_t *unused1,\
  1368. int width, uint32_t *unused) \
  1369. { \
  1370. const uint16_t *src = (const uint16_t *) _src; \
  1371. uint16_t *dst = (uint16_t *) _dst; \
  1372. rgb48ToY_c_template(dst, src, width, origin); \
  1373. } \
  1374. \
  1375. static void pattern ## 48 ## BE_LE ## ToUV_c(uint8_t *_dstU, uint8_t *_dstV, \
  1376. const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
  1377. int width, uint32_t *unused) \
  1378. { \
  1379. const uint16_t *src1 = (const uint16_t *) _src1, \
  1380. *src2 = (const uint16_t *) _src2; \
  1381. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1382. rgb48ToUV_c_template(dstU, dstV, src1, src2, width, origin); \
  1383. } \
  1384. \
  1385. static void pattern ## 48 ## BE_LE ## ToUV_half_c(uint8_t *_dstU, uint8_t *_dstV, \
  1386. const uint8_t *unused0, const uint8_t *_src1, const uint8_t *_src2, \
  1387. int width, uint32_t *unused) \
  1388. { \
  1389. const uint16_t *src1 = (const uint16_t *) _src1, \
  1390. *src2 = (const uint16_t *) _src2; \
  1391. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV; \
  1392. rgb48ToUV_half_c_template(dstU, dstV, src1, src2, width, origin); \
  1393. }
  1394. rgb48funcs(rgb, LE, PIX_FMT_RGB48LE);
  1395. rgb48funcs(rgb, BE, PIX_FMT_RGB48BE);
  1396. rgb48funcs(bgr, LE, PIX_FMT_BGR48LE);
  1397. rgb48funcs(bgr, BE, PIX_FMT_BGR48BE);
  1398. #define input_pixel(i) ((origin == PIX_FMT_RGBA || origin == PIX_FMT_BGRA || \
  1399. origin == PIX_FMT_ARGB || origin == PIX_FMT_ABGR) ? AV_RN32A(&src[(i)*4]) : \
  1400. (isBE(origin) ? AV_RB16(&src[(i)*2]) : AV_RL16(&src[(i)*2])))
  1401. static av_always_inline void
  1402. rgb16_32ToY_c_template(int16_t *dst, const uint8_t *src,
  1403. int width, enum PixelFormat origin,
  1404. int shr, int shg, int shb, int shp,
  1405. int maskr, int maskg, int maskb,
  1406. int rsh, int gsh, int bsh, int S)
  1407. {
  1408. const int ry = RY << rsh, gy = GY << gsh, by = BY << bsh,
  1409. rnd = (32<<((S)-1)) + (1<<(S-7));
  1410. int i;
  1411. for (i = 0; i < width; i++) {
  1412. int px = input_pixel(i) >> shp;
  1413. int b = (px & maskb) >> shb;
  1414. int g = (px & maskg) >> shg;
  1415. int r = (px & maskr) >> shr;
  1416. dst[i] = (ry * r + gy * g + by * b + rnd) >> ((S)-6);
  1417. }
  1418. }
  1419. static av_always_inline void
  1420. rgb16_32ToUV_c_template(int16_t *dstU, int16_t *dstV,
  1421. const uint8_t *src, int width,
  1422. enum PixelFormat origin,
  1423. int shr, int shg, int shb, int shp,
  1424. int maskr, int maskg, int maskb,
  1425. int rsh, int gsh, int bsh, int S)
  1426. {
  1427. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1428. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1429. rnd = (256<<((S)-1)) + (1<<(S-7));
  1430. int i;
  1431. for (i = 0; i < width; i++) {
  1432. int px = input_pixel(i) >> shp;
  1433. int b = (px & maskb) >> shb;
  1434. int g = (px & maskg) >> shg;
  1435. int r = (px & maskr) >> shr;
  1436. dstU[i] = (ru * r + gu * g + bu * b + rnd) >> ((S)-6);
  1437. dstV[i] = (rv * r + gv * g + bv * b + rnd) >> ((S)-6);
  1438. }
  1439. }
  1440. static av_always_inline void
  1441. rgb16_32ToUV_half_c_template(int16_t *dstU, int16_t *dstV,
  1442. const uint8_t *src, int width,
  1443. enum PixelFormat origin,
  1444. int shr, int shg, int shb, int shp,
  1445. int maskr, int maskg, int maskb,
  1446. int rsh, int gsh, int bsh, int S)
  1447. {
  1448. const int ru = RU << rsh, gu = GU << gsh, bu = BU << bsh,
  1449. rv = RV << rsh, gv = GV << gsh, bv = BV << bsh,
  1450. rnd = (256U<<(S)) + (1<<(S-6)), maskgx = ~(maskr | maskb);
  1451. int i;
  1452. maskr |= maskr << 1; maskb |= maskb << 1; maskg |= maskg << 1;
  1453. for (i = 0; i < width; i++) {
  1454. int px0 = input_pixel(2 * i + 0) >> shp;
  1455. int px1 = input_pixel(2 * i + 1) >> shp;
  1456. int b, r, g = (px0 & maskgx) + (px1 & maskgx);
  1457. int rb = px0 + px1 - g;
  1458. b = (rb & maskb) >> shb;
  1459. if (shp || origin == PIX_FMT_BGR565LE || origin == PIX_FMT_BGR565BE ||
  1460. origin == PIX_FMT_RGB565LE || origin == PIX_FMT_RGB565BE) {
  1461. g >>= shg;
  1462. } else {
  1463. g = (g & maskg) >> shg;
  1464. }
  1465. r = (rb & maskr) >> shr;
  1466. dstU[i] = (ru * r + gu * g + bu * b + (unsigned)rnd) >> ((S)-6+1);
  1467. dstV[i] = (rv * r + gv * g + bv * b + (unsigned)rnd) >> ((S)-6+1);
  1468. }
  1469. }
  1470. #undef input_pixel
  1471. #define rgb16_32_wrapper(fmt, name, shr, shg, shb, shp, maskr, \
  1472. maskg, maskb, rsh, gsh, bsh, S) \
  1473. static void name ## ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, \
  1474. int width, uint32_t *unused) \
  1475. { \
  1476. rgb16_32ToY_c_template((int16_t*)dst, src, width, fmt, \
  1477. shr, shg, shb, shp, \
  1478. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1479. } \
  1480. \
  1481. static void name ## ToUV_c(uint8_t *dstU, uint8_t *dstV, \
  1482. const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy, \
  1483. int width, uint32_t *unused) \
  1484. { \
  1485. rgb16_32ToUV_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt, \
  1486. shr, shg, shb, shp, \
  1487. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1488. } \
  1489. \
  1490. static void name ## ToUV_half_c(uint8_t *dstU, uint8_t *dstV, \
  1491. const uint8_t *unused0, const uint8_t *src, const uint8_t *dummy, \
  1492. int width, uint32_t *unused) \
  1493. { \
  1494. rgb16_32ToUV_half_c_template((int16_t*)dstU, (int16_t*)dstV, src, width, fmt, \
  1495. shr, shg, shb, shp, \
  1496. maskr, maskg, maskb, rsh, gsh, bsh, S); \
  1497. }
  1498. rgb16_32_wrapper(PIX_FMT_BGR32, bgr32, 16, 0, 0, 0, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1499. rgb16_32_wrapper(PIX_FMT_BGR32_1, bgr321, 16, 0, 0, 8, 0xFF0000, 0xFF00, 0x00FF, 8, 0, 8, RGB2YUV_SHIFT+8);
  1500. rgb16_32_wrapper(PIX_FMT_RGB32, rgb32, 0, 0, 16, 0, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1501. rgb16_32_wrapper(PIX_FMT_RGB32_1, rgb321, 0, 0, 16, 8, 0x00FF, 0xFF00, 0xFF0000, 8, 0, 8, RGB2YUV_SHIFT+8);
  1502. rgb16_32_wrapper(PIX_FMT_BGR565LE, bgr16le, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1503. rgb16_32_wrapper(PIX_FMT_BGR555LE, bgr15le, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1504. rgb16_32_wrapper(PIX_FMT_RGB565LE, rgb16le, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1505. rgb16_32_wrapper(PIX_FMT_RGB555LE, rgb15le, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1506. rgb16_32_wrapper(PIX_FMT_BGR565BE, bgr16be, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, 11, 5, 0, RGB2YUV_SHIFT+8);
  1507. rgb16_32_wrapper(PIX_FMT_BGR555BE, bgr15be, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, 10, 5, 0, RGB2YUV_SHIFT+7);
  1508. rgb16_32_wrapper(PIX_FMT_RGB565BE, rgb16be, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, 0, 5, 11, RGB2YUV_SHIFT+8);
  1509. rgb16_32_wrapper(PIX_FMT_RGB555BE, rgb15be, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, 0, 5, 10, RGB2YUV_SHIFT+7);
  1510. static void gbr24pToUV_half_c(uint16_t *dstU, uint16_t *dstV,
  1511. const uint8_t *gsrc, const uint8_t *bsrc, const uint8_t *rsrc,
  1512. int width, enum PixelFormat origin)
  1513. {
  1514. int i;
  1515. for (i = 0; i < width; i++) {
  1516. unsigned int g = gsrc[2*i] + gsrc[2*i+1];
  1517. unsigned int b = bsrc[2*i] + bsrc[2*i+1];
  1518. unsigned int r = rsrc[2*i] + rsrc[2*i+1];
  1519. dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
  1520. dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-6))) >> (RGB2YUV_SHIFT-6+1);
  1521. }
  1522. }
  1523. static void abgrToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1524. {
  1525. int i;
  1526. for (i=0; i<width; i++) {
  1527. dst[i]= src[4*i]<<6;
  1528. }
  1529. }
  1530. static void rgbaToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1531. {
  1532. int i;
  1533. for (i=0; i<width; i++) {
  1534. dst[i]= src[4*i+3]<<6;
  1535. }
  1536. }
  1537. static void palToA_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *pal)
  1538. {
  1539. int i;
  1540. for (i=0; i<width; i++) {
  1541. int d= src[i];
  1542. dst[i]= (pal[d] >> 24)<<6;
  1543. }
  1544. }
  1545. static void palToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, long width, uint32_t *pal)
  1546. {
  1547. int i;
  1548. for (i=0; i<width; i++) {
  1549. int d= src[i];
  1550. dst[i]= (pal[d] & 0xFF)<<6;
  1551. }
  1552. }
  1553. static void palToUV_c(uint16_t *dstU, int16_t *dstV,
  1554. const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
  1555. int width, uint32_t *pal)
  1556. {
  1557. int i;
  1558. assert(src1 == src2);
  1559. for (i=0; i<width; i++) {
  1560. int p= pal[src1[i]];
  1561. dstU[i]= (uint8_t)(p>> 8)<<6;
  1562. dstV[i]= (uint8_t)(p>>16)<<6;
  1563. }
  1564. }
  1565. static void monowhite2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1566. {
  1567. int i, j;
  1568. for (i=0; i<width/8; i++) {
  1569. int d= ~src[i];
  1570. for(j=0; j<8; j++)
  1571. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1572. }
  1573. if(width&7){
  1574. int d= ~src[i];
  1575. for(j=0; j<(width&7); j++)
  1576. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1577. }
  1578. }
  1579. static void monoblack2Y_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1580. {
  1581. int i, j;
  1582. for (i=0; i<width/8; i++) {
  1583. int d= src[i];
  1584. for(j=0; j<8; j++)
  1585. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1586. }
  1587. if(width&7){
  1588. int d= src[i];
  1589. for(j=0; j<(width&7); j++)
  1590. dst[8*i+j]= ((d>>(7-j))&1)*16383;
  1591. }
  1592. }
  1593. //FIXME yuy2* can read up to 7 samples too much
  1594. static void yuy2ToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  1595. uint32_t *unused)
  1596. {
  1597. int i;
  1598. for (i=0; i<width; i++)
  1599. dst[i]= src[2*i];
  1600. }
  1601. static void yuy2ToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1602. const uint8_t *src2, int width, uint32_t *unused)
  1603. {
  1604. int i;
  1605. for (i=0; i<width; i++) {
  1606. dstU[i]= src1[4*i + 1];
  1607. dstV[i]= src1[4*i + 3];
  1608. }
  1609. assert(src1 == src2);
  1610. }
  1611. static void bswap16Y_c(uint8_t *_dst, const uint8_t *_src, const uint8_t *unused1, const uint8_t *unused2, int width, uint32_t *unused)
  1612. {
  1613. int i;
  1614. const uint16_t *src = (const uint16_t *) _src;
  1615. uint16_t *dst = (uint16_t *) _dst;
  1616. for (i=0; i<width; i++) {
  1617. dst[i] = av_bswap16(src[i]);
  1618. }
  1619. }
  1620. static void bswap16UV_c(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *unused0, const uint8_t *_src1,
  1621. const uint8_t *_src2, int width, uint32_t *unused)
  1622. {
  1623. int i;
  1624. const uint16_t *src1 = (const uint16_t *) _src1,
  1625. *src2 = (const uint16_t *) _src2;
  1626. uint16_t *dstU = (uint16_t *) _dstU, *dstV = (uint16_t *) _dstV;
  1627. for (i=0; i<width; i++) {
  1628. dstU[i] = av_bswap16(src1[i]);
  1629. dstV[i] = av_bswap16(src2[i]);
  1630. }
  1631. }
  1632. /* This is almost identical to the previous, end exists only because
  1633. * yuy2ToY/UV)(dst, src+1, ...) would have 100% unaligned accesses. */
  1634. static void uyvyToY_c(uint8_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  1635. uint32_t *unused)
  1636. {
  1637. int i;
  1638. for (i=0; i<width; i++)
  1639. dst[i]= src[2*i+1];
  1640. }
  1641. static void uyvyToUV_c(uint8_t *dstU, uint8_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1642. const uint8_t *src2, int width, uint32_t *unused)
  1643. {
  1644. int i;
  1645. for (i=0; i<width; i++) {
  1646. dstU[i]= src1[4*i + 0];
  1647. dstV[i]= src1[4*i + 2];
  1648. }
  1649. assert(src1 == src2);
  1650. }
  1651. static av_always_inline void nvXXtoUV_c(uint8_t *dst1, uint8_t *dst2,
  1652. const uint8_t *src, int width)
  1653. {
  1654. int i;
  1655. for (i = 0; i < width; i++) {
  1656. dst1[i] = src[2*i+0];
  1657. dst2[i] = src[2*i+1];
  1658. }
  1659. }
  1660. static void nv12ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1661. const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
  1662. int width, uint32_t *unused)
  1663. {
  1664. nvXXtoUV_c(dstU, dstV, src1, width);
  1665. }
  1666. static void nv21ToUV_c(uint8_t *dstU, uint8_t *dstV,
  1667. const uint8_t *unused0, const uint8_t *src1, const uint8_t *src2,
  1668. int width, uint32_t *unused)
  1669. {
  1670. nvXXtoUV_c(dstV, dstU, src1, width);
  1671. }
  1672. #define input_pixel(pos) (isBE(origin) ? AV_RB16(pos) : AV_RL16(pos))
  1673. static void bgr24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2,
  1674. int width, uint32_t *unused)
  1675. {
  1676. int i;
  1677. for (i=0; i<width; i++) {
  1678. int b= src[i*3+0];
  1679. int g= src[i*3+1];
  1680. int r= src[i*3+2];
  1681. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1682. }
  1683. }
  1684. static void bgr24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1685. const uint8_t *src2, int width, uint32_t *unused)
  1686. {
  1687. int i;
  1688. for (i=0; i<width; i++) {
  1689. int b= src1[3*i + 0];
  1690. int g= src1[3*i + 1];
  1691. int r= src1[3*i + 2];
  1692. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1693. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1694. }
  1695. assert(src1 == src2);
  1696. }
  1697. static void bgr24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1698. const uint8_t *src2, int width, uint32_t *unused)
  1699. {
  1700. int i;
  1701. for (i=0; i<width; i++) {
  1702. int b= src1[6*i + 0] + src1[6*i + 3];
  1703. int g= src1[6*i + 1] + src1[6*i + 4];
  1704. int r= src1[6*i + 2] + src1[6*i + 5];
  1705. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1706. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1707. }
  1708. assert(src1 == src2);
  1709. }
  1710. static void rgb24ToY_c(int16_t *dst, const uint8_t *src, const uint8_t *unused1, const uint8_t *unused2, int width,
  1711. uint32_t *unused)
  1712. {
  1713. int i;
  1714. for (i=0; i<width; i++) {
  1715. int r= src[i*3+0];
  1716. int g= src[i*3+1];
  1717. int b= src[i*3+2];
  1718. dst[i]= ((RY*r + GY*g + BY*b + (32<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6));
  1719. }
  1720. }
  1721. static void rgb24ToUV_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1722. const uint8_t *src2, int width, uint32_t *unused)
  1723. {
  1724. int i;
  1725. assert(src1==src2);
  1726. for (i=0; i<width; i++) {
  1727. int r= src1[3*i + 0];
  1728. int g= src1[3*i + 1];
  1729. int b= src1[3*i + 2];
  1730. dstU[i]= (RU*r + GU*g + BU*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1731. dstV[i]= (RV*r + GV*g + BV*b + (256<<(RGB2YUV_SHIFT-1)) + (1<<(RGB2YUV_SHIFT-7)))>>(RGB2YUV_SHIFT-6);
  1732. }
  1733. }
  1734. static void rgb24ToUV_half_c(int16_t *dstU, int16_t *dstV, const uint8_t *unused0, const uint8_t *src1,
  1735. const uint8_t *src2, int width, uint32_t *unused)
  1736. {
  1737. int i;
  1738. assert(src1==src2);
  1739. for (i=0; i<width; i++) {
  1740. int r= src1[6*i + 0] + src1[6*i + 3];
  1741. int g= src1[6*i + 1] + src1[6*i + 4];
  1742. int b= src1[6*i + 2] + src1[6*i + 5];
  1743. dstU[i]= (RU*r + GU*g + BU*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1744. dstV[i]= (RV*r + GV*g + BV*b + (256<<RGB2YUV_SHIFT) + (1<<(RGB2YUV_SHIFT-6)))>>(RGB2YUV_SHIFT-5);
  1745. }
  1746. }
  1747. static void planar_rgb_to_y(uint16_t *dst, const uint8_t *src[4], int width)
  1748. {
  1749. int i;
  1750. for (i = 0; i < width; i++) {
  1751. int g = src[0][i];
  1752. int b = src[1][i];
  1753. int r = src[2][i];
  1754. dst[i] = (RY*r + GY*g + BY*b + (0x801<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  1755. }
  1756. }
  1757. static void planar_rgb16le_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
  1758. {
  1759. int i;
  1760. const uint16_t **src = (const uint16_t **) _src;
  1761. uint16_t *dst = (uint16_t *) _dst;
  1762. for (i = 0; i < width; i++) {
  1763. int g = AV_RL16(src[0] + i);
  1764. int b = AV_RL16(src[1] + i);
  1765. int r = AV_RL16(src[2] + i);
  1766. dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  1767. }
  1768. }
  1769. static void planar_rgb16be_to_y(uint8_t *_dst, const uint8_t *_src[4], int width)
  1770. {
  1771. int i;
  1772. const uint16_t **src = (const uint16_t **) _src;
  1773. uint16_t *dst = (uint16_t *) _dst;
  1774. for (i = 0; i < width; i++) {
  1775. int g = AV_RB16(src[0] + i);
  1776. int b = AV_RB16(src[1] + i);
  1777. int r = AV_RB16(src[2] + i);
  1778. dst[i] = ((RY * r + GY * g + BY * b + (33 << (RGB2YUV_SHIFT - 1))) >> RGB2YUV_SHIFT);
  1779. }
  1780. }
  1781. static void planar_rgb_to_uv(uint16_t *dstU, uint16_t *dstV, const uint8_t *src[4], int width)
  1782. {
  1783. int i;
  1784. for (i = 0; i < width; i++) {
  1785. int g = src[0][i];
  1786. int b = src[1][i];
  1787. int r = src[2][i];
  1788. dstU[i] = (RU*r + GU*g + BU*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  1789. dstV[i] = (RV*r + GV*g + BV*b + (0x4001<<(RGB2YUV_SHIFT-7))) >> (RGB2YUV_SHIFT-6);
  1790. }
  1791. }
  1792. static void planar_rgb16le_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
  1793. {
  1794. int i;
  1795. const uint16_t **src = (const uint16_t **) _src;
  1796. uint16_t *dstU = (uint16_t *) _dstU;
  1797. uint16_t *dstV = (uint16_t *) _dstV;
  1798. for (i = 0; i < width; i++) {
  1799. int g = AV_RL16(src[0] + i);
  1800. int b = AV_RL16(src[1] + i);
  1801. int r = AV_RL16(src[2] + i);
  1802. dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  1803. dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  1804. }
  1805. }
  1806. static void planar_rgb16be_to_uv(uint8_t *_dstU, uint8_t *_dstV, const uint8_t *_src[4], int width)
  1807. {
  1808. int i;
  1809. const uint16_t **src = (const uint16_t **) _src;
  1810. uint16_t *dstU = (uint16_t *) _dstU;
  1811. uint16_t *dstV = (uint16_t *) _dstV;
  1812. for (i = 0; i < width; i++) {
  1813. int g = AV_RB16(src[0] + i);
  1814. int b = AV_RB16(src[1] + i);
  1815. int r = AV_RB16(src[2] + i);
  1816. dstU[i] = (RU * r + GU * g + BU * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  1817. dstV[i] = (RV * r + GV * g + BV * b + (257 << RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT + 1);
  1818. }
  1819. }
  1820. static void hScale16To19_c(SwsContext *c, int16_t *_dst, int dstW, const uint8_t *_src,
  1821. const int16_t *filter,
  1822. const int16_t *filterPos, int filterSize)
  1823. {
  1824. int i;
  1825. int32_t *dst = (int32_t *) _dst;
  1826. const uint16_t *src = (const uint16_t *) _src;
  1827. int bits = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1828. int sh = bits - 4;
  1829. if((isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8) && av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1<15)
  1830. sh= 9;
  1831. for (i = 0; i < dstW; i++) {
  1832. int j;
  1833. int srcPos = filterPos[i];
  1834. int val = 0;
  1835. for (j = 0; j < filterSize; j++) {
  1836. val += src[srcPos + j] * filter[filterSize * i + j];
  1837. }
  1838. // filter=14 bit, input=16 bit, output=30 bit, >> 11 makes 19 bit
  1839. dst[i] = FFMIN(val >> sh, (1 << 19) - 1);
  1840. }
  1841. }
  1842. static void hScale16To15_c(SwsContext *c, int16_t *dst, int dstW, const uint8_t *_src,
  1843. const int16_t *filter,
  1844. const int16_t *filterPos, int filterSize)
  1845. {
  1846. int i;
  1847. const uint16_t *src = (const uint16_t *) _src;
  1848. int sh = av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1849. if(sh<15)
  1850. sh= isAnyRGB(c->srcFormat) || c->srcFormat==PIX_FMT_PAL8 ? 13 : av_pix_fmt_descriptors[c->srcFormat].comp[0].depth_minus1;
  1851. for (i = 0; i < dstW; i++) {
  1852. int j;
  1853. int srcPos = filterPos[i];
  1854. int val = 0;
  1855. for (j = 0; j < filterSize; j++) {
  1856. val += src[srcPos + j] * filter[filterSize * i + j];
  1857. }
  1858. // filter=14 bit, input=16 bit, output=30 bit, >> 15 makes 15 bit
  1859. dst[i] = FFMIN(val >> sh, (1 << 15) - 1);
  1860. }
  1861. }
  1862. // bilinear / bicubic scaling
  1863. static void hScale8To15_c(SwsContext *c, int16_t *dst, int dstW, const uint8_t *src,
  1864. const int16_t *filter, const int16_t *filterPos,
  1865. int filterSize)
  1866. {
  1867. int i;
  1868. for (i=0; i<dstW; i++) {
  1869. int j;
  1870. int srcPos= filterPos[i];
  1871. int val=0;
  1872. for (j=0; j<filterSize; j++) {
  1873. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1874. }
  1875. //filter += hFilterSize;
  1876. dst[i] = FFMIN(val>>7, (1<<15)-1); // the cubic equation does overflow ...
  1877. //dst[i] = val>>7;
  1878. }
  1879. }
  1880. static void hScale8To19_c(SwsContext *c, int16_t *_dst, int dstW, const uint8_t *src,
  1881. const int16_t *filter, const int16_t *filterPos,
  1882. int filterSize)
  1883. {
  1884. int i;
  1885. int32_t *dst = (int32_t *) _dst;
  1886. for (i=0; i<dstW; i++) {
  1887. int j;
  1888. int srcPos= filterPos[i];
  1889. int val=0;
  1890. for (j=0; j<filterSize; j++) {
  1891. val += ((int)src[srcPos + j])*filter[filterSize*i + j];
  1892. }
  1893. //filter += hFilterSize;
  1894. dst[i] = FFMIN(val>>3, (1<<19)-1); // the cubic equation does overflow ...
  1895. //dst[i] = val>>7;
  1896. }
  1897. }
  1898. //FIXME all pal and rgb srcFormats could do this convertion as well
  1899. //FIXME all scalers more complex than bilinear could do half of this transform
  1900. static void chrRangeToJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1901. {
  1902. int i;
  1903. for (i = 0; i < width; i++) {
  1904. dstU[i] = (FFMIN(dstU[i],30775)*4663 - 9289992)>>12; //-264
  1905. dstV[i] = (FFMIN(dstV[i],30775)*4663 - 9289992)>>12; //-264
  1906. }
  1907. }
  1908. static void chrRangeFromJpeg_c(int16_t *dstU, int16_t *dstV, int width)
  1909. {
  1910. int i;
  1911. for (i = 0; i < width; i++) {
  1912. dstU[i] = (dstU[i]*1799 + 4081085)>>11; //1469
  1913. dstV[i] = (dstV[i]*1799 + 4081085)>>11; //1469
  1914. }
  1915. }
  1916. static void lumRangeToJpeg_c(int16_t *dst, int width)
  1917. {
  1918. int i;
  1919. for (i = 0; i < width; i++)
  1920. dst[i] = (FFMIN(dst[i],30189)*19077 - 39057361)>>14;
  1921. }
  1922. static void lumRangeFromJpeg_c(int16_t *dst, int width)
  1923. {
  1924. int i;
  1925. for (i = 0; i < width; i++)
  1926. dst[i] = (dst[i]*14071 + 33561947)>>14;
  1927. }
  1928. static void chrRangeToJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1929. {
  1930. int i;
  1931. int32_t *dstU = (int32_t *) _dstU;
  1932. int32_t *dstV = (int32_t *) _dstV;
  1933. for (i = 0; i < width; i++) {
  1934. dstU[i] = (FFMIN(dstU[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1935. dstV[i] = (FFMIN(dstV[i],30775<<4)*4663 - (9289992<<4))>>12; //-264
  1936. }
  1937. }
  1938. static void chrRangeFromJpeg16_c(int16_t *_dstU, int16_t *_dstV, int width)
  1939. {
  1940. int i;
  1941. int32_t *dstU = (int32_t *) _dstU;
  1942. int32_t *dstV = (int32_t *) _dstV;
  1943. for (i = 0; i < width; i++) {
  1944. dstU[i] = (dstU[i]*1799 + (4081085<<4))>>11; //1469
  1945. dstV[i] = (dstV[i]*1799 + (4081085<<4))>>11; //1469
  1946. }
  1947. }
  1948. static void lumRangeToJpeg16_c(int16_t *_dst, int width)
  1949. {
  1950. int i;
  1951. int32_t *dst = (int32_t *) _dst;
  1952. for (i = 0; i < width; i++)
  1953. dst[i] = (FFMIN(dst[i],30189<<4)*4769 - (39057361<<2))>>12;
  1954. }
  1955. static void lumRangeFromJpeg16_c(int16_t *_dst, int width)
  1956. {
  1957. int i;
  1958. int32_t *dst = (int32_t *) _dst;
  1959. for (i = 0; i < width; i++)
  1960. dst[i] = (dst[i]*(14071/4) + (33561947<<4)/4)>>12;
  1961. }
  1962. static void hyscale_fast_c(SwsContext *c, int16_t *dst, int dstWidth,
  1963. const uint8_t *src, int srcW, int xInc)
  1964. {
  1965. int i;
  1966. unsigned int xpos=0;
  1967. for (i=0;i<dstWidth;i++) {
  1968. register unsigned int xx=xpos>>16;
  1969. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  1970. dst[i]= (src[xx]<<7) + (src[xx+1] - src[xx])*xalpha;
  1971. xpos+=xInc;
  1972. }
  1973. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--)
  1974. dst[i] = src[srcW-1]*128;
  1975. }
  1976. // *** horizontal scale Y line to temp buffer
  1977. static av_always_inline void hyscale(SwsContext *c, int16_t *dst, int dstWidth,
  1978. const uint8_t *src_in[4], int srcW, int xInc,
  1979. const int16_t *hLumFilter,
  1980. const int16_t *hLumFilterPos, int hLumFilterSize,
  1981. uint8_t *formatConvBuffer,
  1982. uint32_t *pal, int isAlpha)
  1983. {
  1984. void (*toYV12)(uint8_t *, const uint8_t *, const uint8_t *, const uint8_t *, int, uint32_t *) = isAlpha ? c->alpToYV12 : c->lumToYV12;
  1985. void (*convertRange)(int16_t *, int) = isAlpha ? NULL : c->lumConvertRange;
  1986. const uint8_t *src = src_in[isAlpha ? 3 : 0];
  1987. if (toYV12) {
  1988. toYV12(formatConvBuffer, src, src_in[1], src_in[2], srcW, pal);
  1989. src= formatConvBuffer;
  1990. } else if (c->readLumPlanar && !isAlpha) {
  1991. c->readLumPlanar(formatConvBuffer, src_in, srcW);
  1992. src = formatConvBuffer;
  1993. }
  1994. if (!c->hyscale_fast) {
  1995. c->hyScale(c, dst, dstWidth, src, hLumFilter, hLumFilterPos, hLumFilterSize);
  1996. } else { // fast bilinear upscale / crap downscale
  1997. c->hyscale_fast(c, dst, dstWidth, src, srcW, xInc);
  1998. }
  1999. if (convertRange)
  2000. convertRange(dst, dstWidth);
  2001. }
  2002. static void hcscale_fast_c(SwsContext *c, int16_t *dst1, int16_t *dst2,
  2003. int dstWidth, const uint8_t *src1,
  2004. const uint8_t *src2, int srcW, int xInc)
  2005. {
  2006. int i;
  2007. unsigned int xpos=0;
  2008. for (i=0;i<dstWidth;i++) {
  2009. register unsigned int xx=xpos>>16;
  2010. register unsigned int xalpha=(xpos&0xFFFF)>>9;
  2011. dst1[i]=(src1[xx]*(xalpha^127)+src1[xx+1]*xalpha);
  2012. dst2[i]=(src2[xx]*(xalpha^127)+src2[xx+1]*xalpha);
  2013. xpos+=xInc;
  2014. }
  2015. for (i=dstWidth-1; (i*xInc)>>16 >=srcW-1; i--) {
  2016. dst1[i] = src1[srcW-1]*128;
  2017. dst2[i] = src2[srcW-1]*128;
  2018. }
  2019. }
  2020. static av_always_inline void hcscale(SwsContext *c, int16_t *dst1, int16_t *dst2, int dstWidth,
  2021. const uint8_t *src_in[4],
  2022. int srcW, int xInc, const int16_t *hChrFilter,
  2023. const int16_t *hChrFilterPos, int hChrFilterSize,
  2024. uint8_t *formatConvBuffer, uint32_t *pal)
  2025. {
  2026. const uint8_t *src1 = src_in[1], *src2 = src_in[2];
  2027. if (c->chrToYV12) {
  2028. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW*2+78, 16);
  2029. c->chrToYV12(formatConvBuffer, buf2, src_in[0], src1, src2, srcW, pal);
  2030. src1= formatConvBuffer;
  2031. src2= buf2;
  2032. } else if (c->readChrPlanar) {
  2033. uint8_t *buf2 = formatConvBuffer + FFALIGN(srcW*2+78, 16);
  2034. c->readChrPlanar(formatConvBuffer, buf2, src_in, srcW);
  2035. src1= formatConvBuffer;
  2036. src2= buf2;
  2037. }
  2038. if (!c->hcscale_fast) {
  2039. c->hcScale(c, dst1, dstWidth, src1, hChrFilter, hChrFilterPos, hChrFilterSize);
  2040. c->hcScale(c, dst2, dstWidth, src2, hChrFilter, hChrFilterPos, hChrFilterSize);
  2041. } else { // fast bilinear upscale / crap downscale
  2042. c->hcscale_fast(c, dst1, dst2, dstWidth, src1, src2, srcW, xInc);
  2043. }
  2044. if (c->chrConvertRange)
  2045. c->chrConvertRange(dst1, dst2, dstWidth);
  2046. }
  2047. static av_always_inline void
  2048. find_c_packed_planar_out_funcs(SwsContext *c,
  2049. yuv2planar1_fn *yuv2plane1, yuv2planarX_fn *yuv2planeX,
  2050. yuv2interleavedX_fn *yuv2nv12cX,
  2051. yuv2packed1_fn *yuv2packed1, yuv2packed2_fn *yuv2packed2,
  2052. yuv2packedX_fn *yuv2packedX)
  2053. {
  2054. enum PixelFormat dstFormat = c->dstFormat;
  2055. if (is16BPS(dstFormat)) {
  2056. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_16BE_c : yuv2planeX_16LE_c;
  2057. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_16BE_c : yuv2plane1_16LE_c;
  2058. } else if (is9_OR_10BPS(dstFormat)) {
  2059. if (av_pix_fmt_descriptors[dstFormat].comp[0].depth_minus1 == 8) {
  2060. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_9BE_c : yuv2planeX_9LE_c;
  2061. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_9BE_c : yuv2plane1_9LE_c;
  2062. } else {
  2063. *yuv2planeX = isBE(dstFormat) ? yuv2planeX_10BE_c : yuv2planeX_10LE_c;
  2064. *yuv2plane1 = isBE(dstFormat) ? yuv2plane1_10BE_c : yuv2plane1_10LE_c;
  2065. }
  2066. } else {
  2067. *yuv2plane1 = yuv2plane1_8_c;
  2068. *yuv2planeX = yuv2planeX_8_c;
  2069. if (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)
  2070. *yuv2nv12cX = yuv2nv12cX_c;
  2071. }
  2072. if(c->flags & SWS_FULL_CHR_H_INT) {
  2073. switch (dstFormat) {
  2074. case PIX_FMT_RGBA:
  2075. #if CONFIG_SMALL
  2076. *yuv2packedX = yuv2rgba32_full_X_c;
  2077. #else
  2078. #if CONFIG_SWSCALE_ALPHA
  2079. if (c->alpPixBuf) {
  2080. *yuv2packedX = yuv2rgba32_full_X_c;
  2081. } else
  2082. #endif /* CONFIG_SWSCALE_ALPHA */
  2083. {
  2084. *yuv2packedX = yuv2rgbx32_full_X_c;
  2085. }
  2086. #endif /* !CONFIG_SMALL */
  2087. break;
  2088. case PIX_FMT_ARGB:
  2089. #if CONFIG_SMALL
  2090. *yuv2packedX = yuv2argb32_full_X_c;
  2091. #else
  2092. #if CONFIG_SWSCALE_ALPHA
  2093. if (c->alpPixBuf) {
  2094. *yuv2packedX = yuv2argb32_full_X_c;
  2095. } else
  2096. #endif /* CONFIG_SWSCALE_ALPHA */
  2097. {
  2098. *yuv2packedX = yuv2xrgb32_full_X_c;
  2099. }
  2100. #endif /* !CONFIG_SMALL */
  2101. break;
  2102. case PIX_FMT_BGRA:
  2103. #if CONFIG_SMALL
  2104. *yuv2packedX = yuv2bgra32_full_X_c;
  2105. #else
  2106. #if CONFIG_SWSCALE_ALPHA
  2107. if (c->alpPixBuf) {
  2108. *yuv2packedX = yuv2bgra32_full_X_c;
  2109. } else
  2110. #endif /* CONFIG_SWSCALE_ALPHA */
  2111. {
  2112. *yuv2packedX = yuv2bgrx32_full_X_c;
  2113. }
  2114. #endif /* !CONFIG_SMALL */
  2115. break;
  2116. case PIX_FMT_ABGR:
  2117. #if CONFIG_SMALL
  2118. *yuv2packedX = yuv2abgr32_full_X_c;
  2119. #else
  2120. #if CONFIG_SWSCALE_ALPHA
  2121. if (c->alpPixBuf) {
  2122. *yuv2packedX = yuv2abgr32_full_X_c;
  2123. } else
  2124. #endif /* CONFIG_SWSCALE_ALPHA */
  2125. {
  2126. *yuv2packedX = yuv2xbgr32_full_X_c;
  2127. }
  2128. #endif /* !CONFIG_SMALL */
  2129. break;
  2130. case PIX_FMT_RGB24:
  2131. *yuv2packedX = yuv2rgb24_full_X_c;
  2132. break;
  2133. case PIX_FMT_BGR24:
  2134. *yuv2packedX = yuv2bgr24_full_X_c;
  2135. break;
  2136. }
  2137. if(!*yuv2packedX)
  2138. goto YUV_PACKED;
  2139. } else {
  2140. YUV_PACKED:
  2141. switch (dstFormat) {
  2142. case PIX_FMT_GRAY16BE:
  2143. *yuv2packed1 = yuv2gray16BE_1_c;
  2144. *yuv2packed2 = yuv2gray16BE_2_c;
  2145. *yuv2packedX = yuv2gray16BE_X_c;
  2146. break;
  2147. case PIX_FMT_GRAY16LE:
  2148. *yuv2packed1 = yuv2gray16LE_1_c;
  2149. *yuv2packed2 = yuv2gray16LE_2_c;
  2150. *yuv2packedX = yuv2gray16LE_X_c;
  2151. break;
  2152. case PIX_FMT_MONOWHITE:
  2153. *yuv2packed1 = yuv2monowhite_1_c;
  2154. *yuv2packed2 = yuv2monowhite_2_c;
  2155. *yuv2packedX = yuv2monowhite_X_c;
  2156. break;
  2157. case PIX_FMT_MONOBLACK:
  2158. *yuv2packed1 = yuv2monoblack_1_c;
  2159. *yuv2packed2 = yuv2monoblack_2_c;
  2160. *yuv2packedX = yuv2monoblack_X_c;
  2161. break;
  2162. case PIX_FMT_YUYV422:
  2163. *yuv2packed1 = yuv2yuyv422_1_c;
  2164. *yuv2packed2 = yuv2yuyv422_2_c;
  2165. *yuv2packedX = yuv2yuyv422_X_c;
  2166. break;
  2167. case PIX_FMT_UYVY422:
  2168. *yuv2packed1 = yuv2uyvy422_1_c;
  2169. *yuv2packed2 = yuv2uyvy422_2_c;
  2170. *yuv2packedX = yuv2uyvy422_X_c;
  2171. break;
  2172. case PIX_FMT_RGB48LE:
  2173. *yuv2packed1 = yuv2rgb48le_1_c;
  2174. *yuv2packed2 = yuv2rgb48le_2_c;
  2175. *yuv2packedX = yuv2rgb48le_X_c;
  2176. break;
  2177. case PIX_FMT_RGB48BE:
  2178. *yuv2packed1 = yuv2rgb48be_1_c;
  2179. *yuv2packed2 = yuv2rgb48be_2_c;
  2180. *yuv2packedX = yuv2rgb48be_X_c;
  2181. break;
  2182. case PIX_FMT_BGR48LE:
  2183. *yuv2packed1 = yuv2bgr48le_1_c;
  2184. *yuv2packed2 = yuv2bgr48le_2_c;
  2185. *yuv2packedX = yuv2bgr48le_X_c;
  2186. break;
  2187. case PIX_FMT_BGR48BE:
  2188. *yuv2packed1 = yuv2bgr48be_1_c;
  2189. *yuv2packed2 = yuv2bgr48be_2_c;
  2190. *yuv2packedX = yuv2bgr48be_X_c;
  2191. break;
  2192. case PIX_FMT_RGB32:
  2193. case PIX_FMT_BGR32:
  2194. #if CONFIG_SMALL
  2195. *yuv2packed1 = yuv2rgb32_1_c;
  2196. *yuv2packed2 = yuv2rgb32_2_c;
  2197. *yuv2packedX = yuv2rgb32_X_c;
  2198. #else
  2199. #if CONFIG_SWSCALE_ALPHA
  2200. if (c->alpPixBuf) {
  2201. *yuv2packed1 = yuv2rgba32_1_c;
  2202. *yuv2packed2 = yuv2rgba32_2_c;
  2203. *yuv2packedX = yuv2rgba32_X_c;
  2204. } else
  2205. #endif /* CONFIG_SWSCALE_ALPHA */
  2206. {
  2207. *yuv2packed1 = yuv2rgbx32_1_c;
  2208. *yuv2packed2 = yuv2rgbx32_2_c;
  2209. *yuv2packedX = yuv2rgbx32_X_c;
  2210. }
  2211. #endif /* !CONFIG_SMALL */
  2212. break;
  2213. case PIX_FMT_RGB32_1:
  2214. case PIX_FMT_BGR32_1:
  2215. #if CONFIG_SMALL
  2216. *yuv2packed1 = yuv2rgb32_1_1_c;
  2217. *yuv2packed2 = yuv2rgb32_1_2_c;
  2218. *yuv2packedX = yuv2rgb32_1_X_c;
  2219. #else
  2220. #if CONFIG_SWSCALE_ALPHA
  2221. if (c->alpPixBuf) {
  2222. *yuv2packed1 = yuv2rgba32_1_1_c;
  2223. *yuv2packed2 = yuv2rgba32_1_2_c;
  2224. *yuv2packedX = yuv2rgba32_1_X_c;
  2225. } else
  2226. #endif /* CONFIG_SWSCALE_ALPHA */
  2227. {
  2228. *yuv2packed1 = yuv2rgbx32_1_1_c;
  2229. *yuv2packed2 = yuv2rgbx32_1_2_c;
  2230. *yuv2packedX = yuv2rgbx32_1_X_c;
  2231. }
  2232. #endif /* !CONFIG_SMALL */
  2233. break;
  2234. case PIX_FMT_RGB24:
  2235. *yuv2packed1 = yuv2rgb24_1_c;
  2236. *yuv2packed2 = yuv2rgb24_2_c;
  2237. *yuv2packedX = yuv2rgb24_X_c;
  2238. break;
  2239. case PIX_FMT_BGR24:
  2240. *yuv2packed1 = yuv2bgr24_1_c;
  2241. *yuv2packed2 = yuv2bgr24_2_c;
  2242. *yuv2packedX = yuv2bgr24_X_c;
  2243. break;
  2244. case PIX_FMT_RGB565LE:
  2245. case PIX_FMT_RGB565BE:
  2246. case PIX_FMT_BGR565LE:
  2247. case PIX_FMT_BGR565BE:
  2248. *yuv2packed1 = yuv2rgb16_1_c;
  2249. *yuv2packed2 = yuv2rgb16_2_c;
  2250. *yuv2packedX = yuv2rgb16_X_c;
  2251. break;
  2252. case PIX_FMT_RGB555LE:
  2253. case PIX_FMT_RGB555BE:
  2254. case PIX_FMT_BGR555LE:
  2255. case PIX_FMT_BGR555BE:
  2256. *yuv2packed1 = yuv2rgb15_1_c;
  2257. *yuv2packed2 = yuv2rgb15_2_c;
  2258. *yuv2packedX = yuv2rgb15_X_c;
  2259. break;
  2260. case PIX_FMT_RGB444LE:
  2261. case PIX_FMT_RGB444BE:
  2262. case PIX_FMT_BGR444LE:
  2263. case PIX_FMT_BGR444BE:
  2264. *yuv2packed1 = yuv2rgb12_1_c;
  2265. *yuv2packed2 = yuv2rgb12_2_c;
  2266. *yuv2packedX = yuv2rgb12_X_c;
  2267. break;
  2268. case PIX_FMT_RGB8:
  2269. case PIX_FMT_BGR8:
  2270. *yuv2packed1 = yuv2rgb8_1_c;
  2271. *yuv2packed2 = yuv2rgb8_2_c;
  2272. *yuv2packedX = yuv2rgb8_X_c;
  2273. break;
  2274. case PIX_FMT_RGB4:
  2275. case PIX_FMT_BGR4:
  2276. *yuv2packed1 = yuv2rgb4_1_c;
  2277. *yuv2packed2 = yuv2rgb4_2_c;
  2278. *yuv2packedX = yuv2rgb4_X_c;
  2279. break;
  2280. case PIX_FMT_RGB4_BYTE:
  2281. case PIX_FMT_BGR4_BYTE:
  2282. *yuv2packed1 = yuv2rgb4b_1_c;
  2283. *yuv2packed2 = yuv2rgb4b_2_c;
  2284. *yuv2packedX = yuv2rgb4b_X_c;
  2285. break;
  2286. }
  2287. }
  2288. }
  2289. #define DEBUG_SWSCALE_BUFFERS 0
  2290. #define DEBUG_BUFFERS(...) if (DEBUG_SWSCALE_BUFFERS) av_log(c, AV_LOG_DEBUG, __VA_ARGS__)
  2291. static int swScale(SwsContext *c, const uint8_t* src[],
  2292. int srcStride[], int srcSliceY,
  2293. int srcSliceH, uint8_t* dst[], int dstStride[])
  2294. {
  2295. /* load a few things into local vars to make the code more readable? and faster */
  2296. const int srcW= c->srcW;
  2297. const int dstW= c->dstW;
  2298. const int dstH= c->dstH;
  2299. const int chrDstW= c->chrDstW;
  2300. const int chrSrcW= c->chrSrcW;
  2301. const int lumXInc= c->lumXInc;
  2302. const int chrXInc= c->chrXInc;
  2303. const enum PixelFormat dstFormat= c->dstFormat;
  2304. const int flags= c->flags;
  2305. int16_t *vLumFilterPos= c->vLumFilterPos;
  2306. int16_t *vChrFilterPos= c->vChrFilterPos;
  2307. int16_t *hLumFilterPos= c->hLumFilterPos;
  2308. int16_t *hChrFilterPos= c->hChrFilterPos;
  2309. int16_t *hLumFilter= c->hLumFilter;
  2310. int16_t *hChrFilter= c->hChrFilter;
  2311. int32_t *lumMmxFilter= c->lumMmxFilter;
  2312. int32_t *chrMmxFilter= c->chrMmxFilter;
  2313. int32_t av_unused *alpMmxFilter= c->alpMmxFilter;
  2314. const int vLumFilterSize= c->vLumFilterSize;
  2315. const int vChrFilterSize= c->vChrFilterSize;
  2316. const int hLumFilterSize= c->hLumFilterSize;
  2317. const int hChrFilterSize= c->hChrFilterSize;
  2318. int16_t **lumPixBuf= c->lumPixBuf;
  2319. int16_t **chrUPixBuf= c->chrUPixBuf;
  2320. int16_t **chrVPixBuf= c->chrVPixBuf;
  2321. int16_t **alpPixBuf= c->alpPixBuf;
  2322. const int vLumBufSize= c->vLumBufSize;
  2323. const int vChrBufSize= c->vChrBufSize;
  2324. uint8_t *formatConvBuffer= c->formatConvBuffer;
  2325. const int chrSrcSliceY= srcSliceY >> c->chrSrcVSubSample;
  2326. const int chrSrcSliceH= -((-srcSliceH) >> c->chrSrcVSubSample);
  2327. int lastDstY;
  2328. uint32_t *pal=c->pal_yuv;
  2329. int should_dither= isNBPS(c->srcFormat) || is16BPS(c->srcFormat);
  2330. yuv2planar1_fn yuv2plane1 = c->yuv2plane1;
  2331. yuv2planarX_fn yuv2planeX = c->yuv2planeX;
  2332. yuv2interleavedX_fn yuv2nv12cX = c->yuv2nv12cX;
  2333. yuv2packed1_fn yuv2packed1 = c->yuv2packed1;
  2334. yuv2packed2_fn yuv2packed2 = c->yuv2packed2;
  2335. yuv2packedX_fn yuv2packedX = c->yuv2packedX;
  2336. /* vars which will change and which we need to store back in the context */
  2337. int dstY= c->dstY;
  2338. int lumBufIndex= c->lumBufIndex;
  2339. int chrBufIndex= c->chrBufIndex;
  2340. int lastInLumBuf= c->lastInLumBuf;
  2341. int lastInChrBuf= c->lastInChrBuf;
  2342. if (isPacked(c->srcFormat)) {
  2343. src[0]=
  2344. src[1]=
  2345. src[2]=
  2346. src[3]= src[0];
  2347. srcStride[0]=
  2348. srcStride[1]=
  2349. srcStride[2]=
  2350. srcStride[3]= srcStride[0];
  2351. }
  2352. srcStride[1]<<= c->vChrDrop;
  2353. srcStride[2]<<= c->vChrDrop;
  2354. DEBUG_BUFFERS("swScale() %p[%d] %p[%d] %p[%d] %p[%d] -> %p[%d] %p[%d] %p[%d] %p[%d]\n",
  2355. src[0], srcStride[0], src[1], srcStride[1], src[2], srcStride[2], src[3], srcStride[3],
  2356. dst[0], dstStride[0], dst[1], dstStride[1], dst[2], dstStride[2], dst[3], dstStride[3]);
  2357. DEBUG_BUFFERS("srcSliceY: %d srcSliceH: %d dstY: %d dstH: %d\n",
  2358. srcSliceY, srcSliceH, dstY, dstH);
  2359. DEBUG_BUFFERS("vLumFilterSize: %d vLumBufSize: %d vChrFilterSize: %d vChrBufSize: %d\n",
  2360. vLumFilterSize, vLumBufSize, vChrFilterSize, vChrBufSize);
  2361. if (dstStride[0]%16 !=0 || dstStride[1]%16 !=0 || dstStride[2]%16 !=0 || dstStride[3]%16 != 0) {
  2362. static int warnedAlready=0; //FIXME move this into the context perhaps
  2363. if (flags & SWS_PRINT_INFO && !warnedAlready) {
  2364. av_log(c, AV_LOG_WARNING, "Warning: dstStride is not aligned!\n"
  2365. " ->cannot do aligned memory accesses anymore\n");
  2366. warnedAlready=1;
  2367. }
  2368. }
  2369. if ((int)dst[0]%16 || (int)dst[1]%16 || (int)dst[2]%16 || (int)src[0]%16 || (int)src[1]%16 || (int)src[2]%16
  2370. || dstStride[0]%16 || dstStride[1]%16 || dstStride[2]%16 || dstStride[3]%16
  2371. || srcStride[0]%16 || srcStride[1]%16 || srcStride[2]%16 || srcStride[3]%16
  2372. ) {
  2373. static int warnedAlready=0;
  2374. int cpu_flags = av_get_cpu_flags();
  2375. if (HAVE_MMX2 && (cpu_flags & AV_CPU_FLAG_SSE2) && !warnedAlready){
  2376. av_log(c, AV_LOG_WARNING, "Warning: data is not aligned! This can lead to a speedloss\n");
  2377. warnedAlready=1;
  2378. }
  2379. }
  2380. /* Note the user might start scaling the picture in the middle so this
  2381. will not get executed. This is not really intended but works
  2382. currently, so people might do it. */
  2383. if (srcSliceY ==0) {
  2384. lumBufIndex=-1;
  2385. chrBufIndex=-1;
  2386. dstY=0;
  2387. lastInLumBuf= -1;
  2388. lastInChrBuf= -1;
  2389. }
  2390. if (!should_dither) {
  2391. c->chrDither8 = c->lumDither8 = ff_sws_pb_64;
  2392. }
  2393. lastDstY= dstY;
  2394. for (;dstY < dstH; dstY++) {
  2395. const int chrDstY= dstY>>c->chrDstVSubSample;
  2396. uint8_t *dest[4] = {
  2397. dst[0] + dstStride[0] * dstY,
  2398. dst[1] + dstStride[1] * chrDstY,
  2399. dst[2] + dstStride[2] * chrDstY,
  2400. (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? dst[3] + dstStride[3] * dstY : NULL,
  2401. };
  2402. int use_mmx_vfilter= c->use_mmx_vfilter;
  2403. const int firstLumSrcY= vLumFilterPos[dstY]; //First line needed as input
  2404. const int firstLumSrcY2= vLumFilterPos[FFMIN(dstY | ((1<<c->chrDstVSubSample) - 1), dstH-1)];
  2405. const int firstChrSrcY= vChrFilterPos[chrDstY]; //First line needed as input
  2406. int lastLumSrcY= firstLumSrcY + vLumFilterSize -1; // Last line needed as input
  2407. int lastLumSrcY2=firstLumSrcY2+ vLumFilterSize -1; // Last line needed as input
  2408. int lastChrSrcY= firstChrSrcY + vChrFilterSize -1; // Last line needed as input
  2409. int enough_lines;
  2410. //handle holes (FAST_BILINEAR & weird filters)
  2411. if (firstLumSrcY > lastInLumBuf) lastInLumBuf= firstLumSrcY-1;
  2412. if (firstChrSrcY > lastInChrBuf) lastInChrBuf= firstChrSrcY-1;
  2413. assert(firstLumSrcY >= lastInLumBuf - vLumBufSize + 1);
  2414. assert(firstChrSrcY >= lastInChrBuf - vChrBufSize + 1);
  2415. DEBUG_BUFFERS("dstY: %d\n", dstY);
  2416. DEBUG_BUFFERS("\tfirstLumSrcY: %d lastLumSrcY: %d lastInLumBuf: %d\n",
  2417. firstLumSrcY, lastLumSrcY, lastInLumBuf);
  2418. DEBUG_BUFFERS("\tfirstChrSrcY: %d lastChrSrcY: %d lastInChrBuf: %d\n",
  2419. firstChrSrcY, lastChrSrcY, lastInChrBuf);
  2420. // Do we have enough lines in this slice to output the dstY line
  2421. enough_lines = lastLumSrcY2 < srcSliceY + srcSliceH && lastChrSrcY < -((-srcSliceY - srcSliceH)>>c->chrSrcVSubSample);
  2422. if (!enough_lines) {
  2423. lastLumSrcY = srcSliceY + srcSliceH - 1;
  2424. lastChrSrcY = chrSrcSliceY + chrSrcSliceH - 1;
  2425. DEBUG_BUFFERS("buffering slice: lastLumSrcY %d lastChrSrcY %d\n",
  2426. lastLumSrcY, lastChrSrcY);
  2427. }
  2428. //Do horizontal scaling
  2429. while(lastInLumBuf < lastLumSrcY) {
  2430. const uint8_t *src1[4] = {
  2431. src[0] + (lastInLumBuf + 1 - srcSliceY) * srcStride[0],
  2432. src[1] + (lastInLumBuf + 1 - srcSliceY) * srcStride[1],
  2433. src[2] + (lastInLumBuf + 1 - srcSliceY) * srcStride[2],
  2434. src[3] + (lastInLumBuf + 1 - srcSliceY) * srcStride[3],
  2435. };
  2436. lumBufIndex++;
  2437. assert(lumBufIndex < 2*vLumBufSize);
  2438. assert(lastInLumBuf + 1 - srcSliceY < srcSliceH);
  2439. assert(lastInLumBuf + 1 - srcSliceY >= 0);
  2440. hyscale(c, lumPixBuf[ lumBufIndex ], dstW, src1, srcW, lumXInc,
  2441. hLumFilter, hLumFilterPos, hLumFilterSize,
  2442. formatConvBuffer,
  2443. pal, 0);
  2444. if (CONFIG_SWSCALE_ALPHA && alpPixBuf)
  2445. hyscale(c, alpPixBuf[ lumBufIndex ], dstW, src1, srcW,
  2446. lumXInc, hLumFilter, hLumFilterPos, hLumFilterSize,
  2447. formatConvBuffer,
  2448. pal, 1);
  2449. lastInLumBuf++;
  2450. DEBUG_BUFFERS("\t\tlumBufIndex %d: lastInLumBuf: %d\n",
  2451. lumBufIndex, lastInLumBuf);
  2452. }
  2453. while(lastInChrBuf < lastChrSrcY) {
  2454. const uint8_t *src1[4] = {
  2455. src[0] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[0],
  2456. src[1] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[1],
  2457. src[2] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[2],
  2458. src[3] + (lastInChrBuf + 1 - chrSrcSliceY) * srcStride[3],
  2459. };
  2460. chrBufIndex++;
  2461. assert(chrBufIndex < 2*vChrBufSize);
  2462. assert(lastInChrBuf + 1 - chrSrcSliceY < (chrSrcSliceH));
  2463. assert(lastInChrBuf + 1 - chrSrcSliceY >= 0);
  2464. //FIXME replace parameters through context struct (some at least)
  2465. if (c->needs_hcscale)
  2466. hcscale(c, chrUPixBuf[chrBufIndex], chrVPixBuf[chrBufIndex],
  2467. chrDstW, src1, chrSrcW, chrXInc,
  2468. hChrFilter, hChrFilterPos, hChrFilterSize,
  2469. formatConvBuffer, pal);
  2470. lastInChrBuf++;
  2471. DEBUG_BUFFERS("\t\tchrBufIndex %d: lastInChrBuf: %d\n",
  2472. chrBufIndex, lastInChrBuf);
  2473. }
  2474. //wrap buf index around to stay inside the ring buffer
  2475. if (lumBufIndex >= vLumBufSize) lumBufIndex-= vLumBufSize;
  2476. if (chrBufIndex >= vChrBufSize) chrBufIndex-= vChrBufSize;
  2477. if (!enough_lines)
  2478. break; //we can't output a dstY line so let's try with the next slice
  2479. #if HAVE_MMX
  2480. updateMMXDitherTables(c, dstY, lumBufIndex, chrBufIndex, lastInLumBuf, lastInChrBuf);
  2481. #endif
  2482. if (should_dither) {
  2483. c->chrDither8 = dither_8x8_128[chrDstY & 7];
  2484. c->lumDither8 = dither_8x8_128[dstY & 7];
  2485. }
  2486. if (dstY >= dstH-2) {
  2487. // hmm looks like we can't use MMX here without overwriting this array's tail
  2488. find_c_packed_planar_out_funcs(c, &yuv2plane1, &yuv2planeX, &yuv2nv12cX,
  2489. &yuv2packed1, &yuv2packed2, &yuv2packedX);
  2490. use_mmx_vfilter= 0;
  2491. }
  2492. {
  2493. const int16_t **lumSrcPtr= (const int16_t **)(void*) lumPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize;
  2494. const int16_t **chrUSrcPtr= (const int16_t **)(void*) chrUPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2495. const int16_t **chrVSrcPtr= (const int16_t **)(void*) chrVPixBuf + chrBufIndex + firstChrSrcY - lastInChrBuf + vChrBufSize;
  2496. const int16_t **alpSrcPtr= (CONFIG_SWSCALE_ALPHA && alpPixBuf) ? (const int16_t **)(void*) alpPixBuf + lumBufIndex + firstLumSrcY - lastInLumBuf + vLumBufSize : NULL;
  2497. int16_t *vLumFilter= c->vLumFilter;
  2498. int16_t *vChrFilter= c->vChrFilter;
  2499. if (isPlanarYUV(dstFormat) || dstFormat==PIX_FMT_GRAY8) { //YV12 like
  2500. const int chrSkipMask= (1<<c->chrDstVSubSample)-1;
  2501. vLumFilter += dstY * vLumFilterSize;
  2502. vChrFilter += chrDstY * vChrFilterSize;
  2503. av_assert0(use_mmx_vfilter != (
  2504. yuv2planeX == yuv2planeX_10BE_c
  2505. || yuv2planeX == yuv2planeX_10LE_c
  2506. || yuv2planeX == yuv2planeX_9BE_c
  2507. || yuv2planeX == yuv2planeX_9LE_c
  2508. || yuv2planeX == yuv2planeX_16BE_c
  2509. || yuv2planeX == yuv2planeX_16LE_c
  2510. || yuv2planeX == yuv2planeX_8_c) || !ARCH_X86);
  2511. if(use_mmx_vfilter){
  2512. vLumFilter= c->lumMmxFilter;
  2513. vChrFilter= c->chrMmxFilter;
  2514. }
  2515. if (vLumFilterSize == 1) {
  2516. yuv2plane1(lumSrcPtr[0], dest[0], dstW, c->lumDither8, 0);
  2517. } else {
  2518. yuv2planeX(vLumFilter, vLumFilterSize,
  2519. lumSrcPtr, dest[0], dstW, c->lumDither8, 0);
  2520. }
  2521. if (!((dstY&chrSkipMask) || isGray(dstFormat))) {
  2522. if (yuv2nv12cX) {
  2523. yuv2nv12cX(c, vChrFilter, vChrFilterSize, chrUSrcPtr, chrVSrcPtr, dest[1], chrDstW);
  2524. } else if (vChrFilterSize == 1) {
  2525. yuv2plane1(chrUSrcPtr[0], dest[1], chrDstW, c->chrDither8, 0);
  2526. yuv2plane1(chrVSrcPtr[0], dest[2], chrDstW, c->chrDither8, 3);
  2527. } else {
  2528. yuv2planeX(vChrFilter, vChrFilterSize,
  2529. chrUSrcPtr, dest[1], chrDstW, c->chrDither8, 0);
  2530. yuv2planeX(vChrFilter, vChrFilterSize,
  2531. chrVSrcPtr, dest[2], chrDstW, c->chrDither8, use_mmx_vfilter ? (c->uv_offx2 >> 1) : 3);
  2532. }
  2533. }
  2534. if (CONFIG_SWSCALE_ALPHA && alpPixBuf){
  2535. if(use_mmx_vfilter){
  2536. vLumFilter= c->alpMmxFilter;
  2537. }
  2538. if (vLumFilterSize == 1) {
  2539. yuv2plane1(alpSrcPtr[0], dest[3], dstW, c->lumDither8, 0);
  2540. } else {
  2541. yuv2planeX(vLumFilter, vLumFilterSize,
  2542. alpSrcPtr, dest[3], dstW, c->lumDither8, 0);
  2543. }
  2544. }
  2545. } else {
  2546. assert(lumSrcPtr + vLumFilterSize - 1 < lumPixBuf + vLumBufSize*2);
  2547. assert(chrUSrcPtr + vChrFilterSize - 1 < chrUPixBuf + vChrBufSize*2);
  2548. if (c->yuv2packed1 && vLumFilterSize == 1 && vChrFilterSize == 2) { //unscaled RGB
  2549. int chrAlpha = vChrFilter[2 * dstY + 1];
  2550. yuv2packed1(c, *lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2551. alpPixBuf ? *alpSrcPtr : NULL,
  2552. dest[0], dstW, chrAlpha, dstY);
  2553. } else if (c->yuv2packed2 && vLumFilterSize == 2 && vChrFilterSize == 2) { //bilinear upscale RGB
  2554. int lumAlpha = vLumFilter[2 * dstY + 1];
  2555. int chrAlpha = vChrFilter[2 * dstY + 1];
  2556. lumMmxFilter[2] =
  2557. lumMmxFilter[3] = vLumFilter[2 * dstY ] * 0x10001;
  2558. chrMmxFilter[2] =
  2559. chrMmxFilter[3] = vChrFilter[2 * chrDstY] * 0x10001;
  2560. yuv2packed2(c, lumSrcPtr, chrUSrcPtr, chrVSrcPtr,
  2561. alpPixBuf ? alpSrcPtr : NULL,
  2562. dest[0], dstW, lumAlpha, chrAlpha, dstY);
  2563. } else { //general RGB
  2564. yuv2packedX(c, vLumFilter + dstY * vLumFilterSize,
  2565. lumSrcPtr, vLumFilterSize,
  2566. vChrFilter + dstY * vChrFilterSize,
  2567. chrUSrcPtr, chrVSrcPtr, vChrFilterSize,
  2568. alpSrcPtr, dest[0], dstW, dstY);
  2569. }
  2570. }
  2571. }
  2572. }
  2573. if ((dstFormat == PIX_FMT_YUVA420P) && !alpPixBuf)
  2574. fillPlane(dst[3], dstStride[3], dstW, dstY-lastDstY, lastDstY, 255);
  2575. #if HAVE_MMX2
  2576. if (av_get_cpu_flags() & AV_CPU_FLAG_MMX2)
  2577. __asm__ volatile("sfence":::"memory");
  2578. #endif
  2579. emms_c();
  2580. /* store changed local vars back in the context */
  2581. c->dstY= dstY;
  2582. c->lumBufIndex= lumBufIndex;
  2583. c->chrBufIndex= chrBufIndex;
  2584. c->lastInLumBuf= lastInLumBuf;
  2585. c->lastInChrBuf= lastInChrBuf;
  2586. return dstY - lastDstY;
  2587. }
  2588. static av_cold void sws_init_swScale_c(SwsContext *c)
  2589. {
  2590. enum PixelFormat srcFormat = c->srcFormat;
  2591. find_c_packed_planar_out_funcs(c, &c->yuv2plane1, &c->yuv2planeX,
  2592. &c->yuv2nv12cX, &c->yuv2packed1, &c->yuv2packed2,
  2593. &c->yuv2packedX);
  2594. c->chrToYV12 = NULL;
  2595. switch(srcFormat) {
  2596. case PIX_FMT_YUYV422 : c->chrToYV12 = yuy2ToUV_c; break;
  2597. case PIX_FMT_UYVY422 : c->chrToYV12 = uyvyToUV_c; break;
  2598. case PIX_FMT_NV12 : c->chrToYV12 = nv12ToUV_c; break;
  2599. case PIX_FMT_NV21 : c->chrToYV12 = nv21ToUV_c; break;
  2600. case PIX_FMT_RGB8 :
  2601. case PIX_FMT_BGR8 :
  2602. case PIX_FMT_PAL8 :
  2603. case PIX_FMT_BGR4_BYTE:
  2604. case PIX_FMT_RGB4_BYTE: c->chrToYV12 = palToUV_c; break;
  2605. case PIX_FMT_GBRP9LE:
  2606. case PIX_FMT_GBRP10LE:
  2607. case PIX_FMT_GBRP16LE: c->readChrPlanar = planar_rgb16le_to_uv; break;
  2608. case PIX_FMT_GBRP9BE:
  2609. case PIX_FMT_GBRP10BE:
  2610. case PIX_FMT_GBRP16BE: c->readChrPlanar = planar_rgb16be_to_uv; break;
  2611. case PIX_FMT_GBRP: c->readChrPlanar = planar_rgb_to_uv; break;
  2612. #if HAVE_BIGENDIAN
  2613. case PIX_FMT_YUV444P9LE:
  2614. case PIX_FMT_YUV422P9LE:
  2615. case PIX_FMT_YUV420P9LE:
  2616. case PIX_FMT_YUV422P10LE:
  2617. case PIX_FMT_YUV420P10LE:
  2618. case PIX_FMT_YUV444P10LE:
  2619. case PIX_FMT_YUV420P16LE:
  2620. case PIX_FMT_YUV422P16LE:
  2621. case PIX_FMT_YUV444P16LE: c->chrToYV12 = bswap16UV_c; break;
  2622. #else
  2623. case PIX_FMT_YUV444P9BE:
  2624. case PIX_FMT_YUV422P9BE:
  2625. case PIX_FMT_YUV420P9BE:
  2626. case PIX_FMT_YUV444P10BE:
  2627. case PIX_FMT_YUV422P10BE:
  2628. case PIX_FMT_YUV420P10BE:
  2629. case PIX_FMT_YUV420P16BE:
  2630. case PIX_FMT_YUV422P16BE:
  2631. case PIX_FMT_YUV444P16BE: c->chrToYV12 = bswap16UV_c; break;
  2632. #endif
  2633. }
  2634. if (c->chrSrcHSubSample) {
  2635. switch(srcFormat) {
  2636. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_half_c; break;
  2637. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_half_c; break;
  2638. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_half_c; break;
  2639. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_half_c; break;
  2640. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_half_c; break;
  2641. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_half_c; break;
  2642. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_half_c; break;
  2643. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_half_c; break;
  2644. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_half_c; break;
  2645. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_half_c; break;
  2646. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_half_c; break;
  2647. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_half_c; break;
  2648. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_half_c; break;
  2649. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_half_c; break;
  2650. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_half_c; break;
  2651. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_half_c; break;
  2652. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_half_c; break;
  2653. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_half_c; break;
  2654. case PIX_FMT_GBR24P : c->chrToYV12 = gbr24pToUV_half_c; break;
  2655. }
  2656. } else {
  2657. switch(srcFormat) {
  2658. case PIX_FMT_RGB48BE : c->chrToYV12 = rgb48BEToUV_c; break;
  2659. case PIX_FMT_RGB48LE : c->chrToYV12 = rgb48LEToUV_c; break;
  2660. case PIX_FMT_BGR48BE : c->chrToYV12 = bgr48BEToUV_c; break;
  2661. case PIX_FMT_BGR48LE : c->chrToYV12 = bgr48LEToUV_c; break;
  2662. case PIX_FMT_RGB32 : c->chrToYV12 = bgr32ToUV_c; break;
  2663. case PIX_FMT_RGB32_1 : c->chrToYV12 = bgr321ToUV_c; break;
  2664. case PIX_FMT_BGR24 : c->chrToYV12 = bgr24ToUV_c; break;
  2665. case PIX_FMT_BGR565LE: c->chrToYV12 = bgr16leToUV_c; break;
  2666. case PIX_FMT_BGR565BE: c->chrToYV12 = bgr16beToUV_c; break;
  2667. case PIX_FMT_BGR555LE: c->chrToYV12 = bgr15leToUV_c; break;
  2668. case PIX_FMT_BGR555BE: c->chrToYV12 = bgr15beToUV_c; break;
  2669. case PIX_FMT_BGR32 : c->chrToYV12 = rgb32ToUV_c; break;
  2670. case PIX_FMT_BGR32_1 : c->chrToYV12 = rgb321ToUV_c; break;
  2671. case PIX_FMT_RGB24 : c->chrToYV12 = rgb24ToUV_c; break;
  2672. case PIX_FMT_RGB565LE: c->chrToYV12 = rgb16leToUV_c; break;
  2673. case PIX_FMT_RGB565BE: c->chrToYV12 = rgb16beToUV_c; break;
  2674. case PIX_FMT_RGB555LE: c->chrToYV12 = rgb15leToUV_c; break;
  2675. case PIX_FMT_RGB555BE: c->chrToYV12 = rgb15beToUV_c; break;
  2676. }
  2677. }
  2678. c->lumToYV12 = NULL;
  2679. c->alpToYV12 = NULL;
  2680. switch (srcFormat) {
  2681. case PIX_FMT_GBRP9LE:
  2682. case PIX_FMT_GBRP10LE:
  2683. case PIX_FMT_GBRP16LE: c->readLumPlanar = planar_rgb16le_to_y; break;
  2684. case PIX_FMT_GBRP9BE:
  2685. case PIX_FMT_GBRP10BE:
  2686. case PIX_FMT_GBRP16BE: c->readLumPlanar = planar_rgb16be_to_y; break;
  2687. case PIX_FMT_GBRP: c->readLumPlanar = planar_rgb_to_y; break;
  2688. #if HAVE_BIGENDIAN
  2689. case PIX_FMT_YUV444P9LE:
  2690. case PIX_FMT_YUV422P9LE:
  2691. case PIX_FMT_YUV420P9LE:
  2692. case PIX_FMT_YUV422P10LE:
  2693. case PIX_FMT_YUV420P10LE:
  2694. case PIX_FMT_YUV444P10LE:
  2695. case PIX_FMT_YUV420P16LE:
  2696. case PIX_FMT_YUV422P16LE:
  2697. case PIX_FMT_YUV444P16LE:
  2698. case PIX_FMT_GRAY16LE: c->lumToYV12 = bswap16Y_c; break;
  2699. #else
  2700. case PIX_FMT_YUV444P9BE:
  2701. case PIX_FMT_YUV422P9BE:
  2702. case PIX_FMT_YUV420P9BE:
  2703. case PIX_FMT_YUV444P10BE:
  2704. case PIX_FMT_YUV422P10BE:
  2705. case PIX_FMT_YUV420P10BE:
  2706. case PIX_FMT_YUV420P16BE:
  2707. case PIX_FMT_YUV422P16BE:
  2708. case PIX_FMT_YUV444P16BE:
  2709. case PIX_FMT_GRAY16BE: c->lumToYV12 = bswap16Y_c; break;
  2710. #endif
  2711. case PIX_FMT_YUYV422 :
  2712. case PIX_FMT_Y400A : c->lumToYV12 = yuy2ToY_c; break;
  2713. case PIX_FMT_UYVY422 : c->lumToYV12 = uyvyToY_c; break;
  2714. case PIX_FMT_BGR24 : c->lumToYV12 = bgr24ToY_c; break;
  2715. case PIX_FMT_BGR565LE : c->lumToYV12 = bgr16leToY_c; break;
  2716. case PIX_FMT_BGR565BE : c->lumToYV12 = bgr16beToY_c; break;
  2717. case PIX_FMT_BGR555LE : c->lumToYV12 = bgr15leToY_c; break;
  2718. case PIX_FMT_BGR555BE : c->lumToYV12 = bgr15beToY_c; break;
  2719. case PIX_FMT_RGB24 : c->lumToYV12 = rgb24ToY_c; break;
  2720. case PIX_FMT_RGB565LE : c->lumToYV12 = rgb16leToY_c; break;
  2721. case PIX_FMT_RGB565BE : c->lumToYV12 = rgb16beToY_c; break;
  2722. case PIX_FMT_RGB555LE : c->lumToYV12 = rgb15leToY_c; break;
  2723. case PIX_FMT_RGB555BE : c->lumToYV12 = rgb15beToY_c; break;
  2724. case PIX_FMT_RGB8 :
  2725. case PIX_FMT_BGR8 :
  2726. case PIX_FMT_PAL8 :
  2727. case PIX_FMT_BGR4_BYTE:
  2728. case PIX_FMT_RGB4_BYTE: c->lumToYV12 = palToY_c; break;
  2729. case PIX_FMT_MONOBLACK: c->lumToYV12 = monoblack2Y_c; break;
  2730. case PIX_FMT_MONOWHITE: c->lumToYV12 = monowhite2Y_c; break;
  2731. case PIX_FMT_RGB32 : c->lumToYV12 = bgr32ToY_c; break;
  2732. case PIX_FMT_RGB32_1: c->lumToYV12 = bgr321ToY_c; break;
  2733. case PIX_FMT_BGR32 : c->lumToYV12 = rgb32ToY_c; break;
  2734. case PIX_FMT_BGR32_1: c->lumToYV12 = rgb321ToY_c; break;
  2735. case PIX_FMT_RGB48BE: c->lumToYV12 = rgb48BEToY_c; break;
  2736. case PIX_FMT_RGB48LE: c->lumToYV12 = rgb48LEToY_c; break;
  2737. case PIX_FMT_BGR48BE: c->lumToYV12 = bgr48BEToY_c; break;
  2738. case PIX_FMT_BGR48LE: c->lumToYV12 = bgr48LEToY_c; break;
  2739. }
  2740. if (c->alpPixBuf) {
  2741. switch (srcFormat) {
  2742. case PIX_FMT_BGRA:
  2743. case PIX_FMT_RGBA: c->alpToYV12 = rgbaToA_c; break;
  2744. case PIX_FMT_ABGR:
  2745. case PIX_FMT_ARGB: c->alpToYV12 = abgrToA_c; break;
  2746. case PIX_FMT_Y400A: c->alpToYV12 = uyvyToY_c; break;
  2747. case PIX_FMT_PAL8 : c->alpToYV12 = palToA_c; break;
  2748. }
  2749. }
  2750. if (c->srcBpc == 8) {
  2751. if (c->dstBpc <= 10) {
  2752. c->hyScale = c->hcScale = hScale8To15_c;
  2753. if (c->flags & SWS_FAST_BILINEAR) {
  2754. c->hyscale_fast = hyscale_fast_c;
  2755. c->hcscale_fast = hcscale_fast_c;
  2756. }
  2757. } else {
  2758. c->hyScale = c->hcScale = hScale8To19_c;
  2759. }
  2760. } else {
  2761. c->hyScale = c->hcScale = c->dstBpc > 10 ? hScale16To19_c : hScale16To15_c;
  2762. }
  2763. if (c->srcRange != c->dstRange && !isAnyRGB(c->dstFormat)) {
  2764. if (c->dstBpc <= 10) {
  2765. if (c->srcRange) {
  2766. c->lumConvertRange = lumRangeFromJpeg_c;
  2767. c->chrConvertRange = chrRangeFromJpeg_c;
  2768. } else {
  2769. c->lumConvertRange = lumRangeToJpeg_c;
  2770. c->chrConvertRange = chrRangeToJpeg_c;
  2771. }
  2772. } else {
  2773. if (c->srcRange) {
  2774. c->lumConvertRange = lumRangeFromJpeg16_c;
  2775. c->chrConvertRange = chrRangeFromJpeg16_c;
  2776. } else {
  2777. c->lumConvertRange = lumRangeToJpeg16_c;
  2778. c->chrConvertRange = chrRangeToJpeg16_c;
  2779. }
  2780. }
  2781. }
  2782. if (!(isGray(srcFormat) || isGray(c->dstFormat) ||
  2783. srcFormat == PIX_FMT_MONOBLACK || srcFormat == PIX_FMT_MONOWHITE))
  2784. c->needs_hcscale = 1;
  2785. }
  2786. SwsFunc ff_getSwsFunc(SwsContext *c)
  2787. {
  2788. sws_init_swScale_c(c);
  2789. if (HAVE_MMX)
  2790. ff_sws_init_swScale_mmx(c);
  2791. if (HAVE_ALTIVEC)
  2792. ff_sws_init_swScale_altivec(c);
  2793. return swScale;
  2794. }