swscale.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. /*
  2. * Copyright (C) 2001-2003 Michael Niedermayer <michaelni@gmx.at>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. *
  20. * the C code (not assembly, mmx, ...) of this file can be used
  21. * under the LGPL license too
  22. */
  23. /*
  24. supported Input formats: YV12, I420/IYUV, YUY2, UYVY, BGR32, BGR32_1, BGR24, BGR16, BGR15, RGB32, RGB32_1, RGB24, Y8/Y800, YVU9/IF09, PAL8
  25. supported output formats: YV12, I420/IYUV, YUY2, UYVY, {BGR,RGB}{1,4,8,15,16,24,32}, Y8/Y800, YVU9/IF09
  26. {BGR,RGB}{1,4,8,15,16} support dithering
  27. unscaled special converters (YV12=I420=IYUV, Y800=Y8)
  28. YV12 -> {BGR,RGB}{1,4,8,15,16,24,32}
  29. x -> x
  30. YUV9 -> YV12
  31. YUV9/YV12 -> Y800
  32. Y800 -> YUV9/YV12
  33. BGR24 -> BGR32 & RGB24 -> RGB32
  34. BGR32 -> BGR24 & RGB32 -> RGB24
  35. BGR15 -> BGR16
  36. */
  37. /*
  38. tested special converters (most are tested actually, but I did not write it down ...)
  39. YV12 -> BGR16
  40. YV12 -> YV12
  41. BGR15 -> BGR16
  42. BGR16 -> BGR16
  43. YVU9 -> YV12
  44. untested special converters
  45. YV12/I420 -> BGR15/BGR24/BGR32 (it is the yuv2rgb stuff, so it should be OK)
  46. YV12/I420 -> YV12/I420
  47. YUY2/BGR15/BGR24/BGR32/RGB24/RGB32 -> same format
  48. BGR24 -> BGR32 & RGB24 -> RGB32
  49. BGR32 -> BGR24 & RGB32 -> RGB24
  50. BGR24 -> YV12
  51. */
  52. #include <inttypes.h>
  53. #include <string.h>
  54. #include <math.h>
  55. #include <stdio.h>
  56. #include "config.h"
  57. #include <assert.h>
  58. #include "swscale.h"
  59. #include "swscale_internal.h"
  60. #include "rgb2rgb.h"
  61. #include "libavutil/intreadwrite.h"
  62. #include "libavutil/x86_cpu.h"
  63. #include "libavutil/avutil.h"
  64. #include "libavutil/bswap.h"
  65. #include "libavutil/pixdesc.h"
  66. #undef MOVNTQ
  67. #undef PAVGB
  68. //#undef HAVE_MMX2
  69. //#define HAVE_AMD3DNOW
  70. //#undef HAVE_MMX
  71. //#undef ARCH_X86
  72. #define DITHER1XBPP
  73. #define FAST_BGR2YV12 // use 7 bit coefficients instead of 15 bit
  74. #ifdef M_PI
  75. #define PI M_PI
  76. #else
  77. #define PI 3.14159265358979323846
  78. #endif
  79. #define isPacked(x) ( \
  80. (x)==PIX_FMT_PAL8 \
  81. || (x)==PIX_FMT_YUYV422 \
  82. || (x)==PIX_FMT_UYVY422 \
  83. || isRGB(x) \
  84. || isBGR(x) \
  85. )
  86. #define usePal(x) (av_pix_fmt_descriptors[x].flags & PIX_FMT_PAL)
  87. #define RGB2YUV_SHIFT 15
  88. #define BY ( (int)(0.114*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  89. #define BV (-(int)(0.081*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  90. #define BU ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  91. #define GY ( (int)(0.587*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  92. #define GV (-(int)(0.419*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  93. #define GU (-(int)(0.331*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  94. #define RY ( (int)(0.299*219/255*(1<<RGB2YUV_SHIFT)+0.5))
  95. #define RV ( (int)(0.500*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  96. #define RU (-(int)(0.169*224/255*(1<<RGB2YUV_SHIFT)+0.5))
  97. static const double rgb2yuv_table[8][9]={
  98. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  99. {0.7152, 0.0722, 0.2126, -0.386, 0.5, -0.115, -0.454, -0.046, 0.5},
  100. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  101. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  102. {0.59 , 0.11 , 0.30 , -0.331, 0.5, -0.169, -0.421, -0.079, 0.5}, //FCC
  103. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5},
  104. {0.587 , 0.114 , 0.299 , -0.331, 0.5, -0.169, -0.419, -0.081, 0.5}, //SMPTE 170M
  105. {0.701 , 0.087 , 0.212 , -0.384, 0.5 -0.116, -0.445, -0.055, 0.5}, //SMPTE 240M
  106. };
  107. /*
  108. NOTES
  109. Special versions: fast Y 1:1 scaling (no interpolation in y direction)
  110. TODO
  111. more intelligent misalignment avoidance for the horizontal scaler
  112. write special vertical cubic upscale version
  113. optimize C code (YV12 / minmax)
  114. add support for packed pixel YUV input & output
  115. add support for Y8 output
  116. optimize BGR24 & BGR32
  117. add BGR4 output support
  118. write special BGR->BGR scaler
  119. */
  120. #if ARCH_X86 && CONFIG_GPL
  121. DECLARE_ASM_CONST(8, uint64_t, bF8)= 0xF8F8F8F8F8F8F8F8LL;
  122. DECLARE_ASM_CONST(8, uint64_t, bFC)= 0xFCFCFCFCFCFCFCFCLL;
  123. DECLARE_ASM_CONST(8, uint64_t, w10)= 0x0010001000100010LL;
  124. DECLARE_ASM_CONST(8, uint64_t, w02)= 0x0002000200020002LL;
  125. DECLARE_ASM_CONST(8, uint64_t, bm00001111)=0x00000000FFFFFFFFLL;
  126. DECLARE_ASM_CONST(8, uint64_t, bm00000111)=0x0000000000FFFFFFLL;
  127. DECLARE_ASM_CONST(8, uint64_t, bm11111000)=0xFFFFFFFFFF000000LL;
  128. DECLARE_ASM_CONST(8, uint64_t, bm01010101)=0x00FF00FF00FF00FFLL;
  129. const DECLARE_ALIGNED(8, uint64_t, ff_dither4)[2] = {
  130. 0x0103010301030103LL,
  131. 0x0200020002000200LL,};
  132. const DECLARE_ALIGNED(8, uint64_t, ff_dither8)[2] = {
  133. 0x0602060206020602LL,
  134. 0x0004000400040004LL,};
  135. DECLARE_ASM_CONST(8, uint64_t, b16Mask)= 0x001F001F001F001FLL;
  136. DECLARE_ASM_CONST(8, uint64_t, g16Mask)= 0x07E007E007E007E0LL;
  137. DECLARE_ASM_CONST(8, uint64_t, r16Mask)= 0xF800F800F800F800LL;
  138. DECLARE_ASM_CONST(8, uint64_t, b15Mask)= 0x001F001F001F001FLL;
  139. DECLARE_ASM_CONST(8, uint64_t, g15Mask)= 0x03E003E003E003E0LL;
  140. DECLARE_ASM_CONST(8, uint64_t, r15Mask)= 0x7C007C007C007C00LL;
  141. DECLARE_ALIGNED(8, const uint64_t, ff_M24A) = 0x00FF0000FF0000FFLL;
  142. DECLARE_ALIGNED(8, const uint64_t, ff_M24B) = 0xFF0000FF0000FF00LL;
  143. DECLARE_ALIGNED(8, const uint64_t, ff_M24C) = 0x0000FF0000FF0000LL;
  144. #ifdef FAST_BGR2YV12
  145. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000000210041000DULL;
  146. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000FFEEFFDC0038ULL;
  147. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00000038FFD2FFF8ULL;
  148. #else
  149. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YCoeff) = 0x000020E540830C8BULL;
  150. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UCoeff) = 0x0000ED0FDAC23831ULL;
  151. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2VCoeff) = 0x00003831D0E6F6EAULL;
  152. #endif /* FAST_BGR2YV12 */
  153. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2YOffset) = 0x1010101010101010ULL;
  154. DECLARE_ALIGNED(8, const uint64_t, ff_bgr2UVOffset) = 0x8080808080808080ULL;
  155. DECLARE_ALIGNED(8, const uint64_t, ff_w1111) = 0x0001000100010001ULL;
  156. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY1Coeff) = 0x0C88000040870C88ULL;
  157. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toY2Coeff) = 0x20DE4087000020DEULL;
  158. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY1Coeff) = 0x20DE0000408720DEULL;
  159. DECLARE_ASM_CONST(8, uint64_t, ff_rgb24toY2Coeff) = 0x0C88408700000C88ULL;
  160. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toYOffset) = 0x0008400000084000ULL;
  161. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUV)[2][4] = {
  162. {0x38380000DAC83838ULL, 0xECFFDAC80000ECFFULL, 0xF6E40000D0E3F6E4ULL, 0x3838D0E300003838ULL},
  163. {0xECFF0000DAC8ECFFULL, 0x3838DAC800003838ULL, 0x38380000D0E33838ULL, 0xF6E4D0E30000F6E4ULL},
  164. };
  165. DECLARE_ASM_CONST(8, uint64_t, ff_bgr24toUVOffset)= 0x0040400000404000ULL;
  166. #endif /* ARCH_X86 && CONFIG_GPL */
  167. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_4)[2][8]={
  168. { 1, 3, 1, 3, 1, 3, 1, 3, },
  169. { 2, 0, 2, 0, 2, 0, 2, 0, },
  170. };
  171. DECLARE_ALIGNED(8, static const uint8_t, dither_2x2_8)[2][8]={
  172. { 6, 2, 6, 2, 6, 2, 6, 2, },
  173. { 0, 4, 0, 4, 0, 4, 0, 4, },
  174. };
  175. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_32)[8][8]={
  176. { 17, 9, 23, 15, 16, 8, 22, 14, },
  177. { 5, 29, 3, 27, 4, 28, 2, 26, },
  178. { 21, 13, 19, 11, 20, 12, 18, 10, },
  179. { 0, 24, 6, 30, 1, 25, 7, 31, },
  180. { 16, 8, 22, 14, 17, 9, 23, 15, },
  181. { 4, 28, 2, 26, 5, 29, 3, 27, },
  182. { 20, 12, 18, 10, 21, 13, 19, 11, },
  183. { 1, 25, 7, 31, 0, 24, 6, 30, },
  184. };
  185. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_73)[8][8]={
  186. { 0, 55, 14, 68, 3, 58, 17, 72, },
  187. { 37, 18, 50, 32, 40, 22, 54, 35, },
  188. { 9, 64, 5, 59, 13, 67, 8, 63, },
  189. { 46, 27, 41, 23, 49, 31, 44, 26, },
  190. { 2, 57, 16, 71, 1, 56, 15, 70, },
  191. { 39, 21, 52, 34, 38, 19, 51, 33, },
  192. { 11, 66, 7, 62, 10, 65, 6, 60, },
  193. { 48, 30, 43, 25, 47, 29, 42, 24, },
  194. };
  195. #if 1
  196. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  197. {117, 62, 158, 103, 113, 58, 155, 100, },
  198. { 34, 199, 21, 186, 31, 196, 17, 182, },
  199. {144, 89, 131, 76, 141, 86, 127, 72, },
  200. { 0, 165, 41, 206, 10, 175, 52, 217, },
  201. {110, 55, 151, 96, 120, 65, 162, 107, },
  202. { 28, 193, 14, 179, 38, 203, 24, 189, },
  203. {138, 83, 124, 69, 148, 93, 134, 79, },
  204. { 7, 172, 48, 213, 3, 168, 45, 210, },
  205. };
  206. #elif 1
  207. // tries to correct a gamma of 1.5
  208. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  209. { 0, 143, 18, 200, 2, 156, 25, 215, },
  210. { 78, 28, 125, 64, 89, 36, 138, 74, },
  211. { 10, 180, 3, 161, 16, 195, 8, 175, },
  212. {109, 51, 93, 38, 121, 60, 105, 47, },
  213. { 1, 152, 23, 210, 0, 147, 20, 205, },
  214. { 85, 33, 134, 71, 81, 30, 130, 67, },
  215. { 14, 190, 6, 171, 12, 185, 5, 166, },
  216. {117, 57, 101, 44, 113, 54, 97, 41, },
  217. };
  218. #elif 1
  219. // tries to correct a gamma of 2.0
  220. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  221. { 0, 124, 8, 193, 0, 140, 12, 213, },
  222. { 55, 14, 104, 42, 66, 19, 119, 52, },
  223. { 3, 168, 1, 145, 6, 187, 3, 162, },
  224. { 86, 31, 70, 21, 99, 39, 82, 28, },
  225. { 0, 134, 11, 206, 0, 129, 9, 200, },
  226. { 62, 17, 114, 48, 58, 16, 109, 45, },
  227. { 5, 181, 2, 157, 4, 175, 1, 151, },
  228. { 95, 36, 78, 26, 90, 34, 74, 24, },
  229. };
  230. #else
  231. // tries to correct a gamma of 2.5
  232. DECLARE_ALIGNED(8, const uint8_t, dither_8x8_220)[8][8]={
  233. { 0, 107, 3, 187, 0, 125, 6, 212, },
  234. { 39, 7, 86, 28, 49, 11, 102, 36, },
  235. { 1, 158, 0, 131, 3, 180, 1, 151, },
  236. { 68, 19, 52, 12, 81, 25, 64, 17, },
  237. { 0, 119, 5, 203, 0, 113, 4, 195, },
  238. { 45, 9, 96, 33, 42, 8, 91, 30, },
  239. { 2, 172, 1, 144, 2, 165, 0, 137, },
  240. { 77, 23, 60, 15, 72, 21, 56, 14, },
  241. };
  242. #endif
  243. static av_always_inline void yuv2yuvX16inC_template(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  244. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  245. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest,
  246. int dstW, int chrDstW, int big_endian)
  247. {
  248. //FIXME Optimize (just quickly written not optimized..)
  249. int i;
  250. for (i = 0; i < dstW; i++) {
  251. int val = 1 << 10;
  252. int j;
  253. for (j = 0; j < lumFilterSize; j++)
  254. val += lumSrc[j][i] * lumFilter[j];
  255. if (big_endian) {
  256. AV_WB16(&dest[i], av_clip_uint16(val >> 11));
  257. } else {
  258. AV_WL16(&dest[i], av_clip_uint16(val >> 11));
  259. }
  260. }
  261. if (uDest) {
  262. for (i = 0; i < chrDstW; i++) {
  263. int u = 1 << 10;
  264. int v = 1 << 10;
  265. int j;
  266. for (j = 0; j < chrFilterSize; j++) {
  267. u += chrSrc[j][i ] * chrFilter[j];
  268. v += chrSrc[j][i + VOFW] * chrFilter[j];
  269. }
  270. if (big_endian) {
  271. AV_WB16(&uDest[i], av_clip_uint16(u >> 11));
  272. AV_WB16(&vDest[i], av_clip_uint16(v >> 11));
  273. } else {
  274. AV_WL16(&uDest[i], av_clip_uint16(u >> 11));
  275. AV_WL16(&vDest[i], av_clip_uint16(v >> 11));
  276. }
  277. }
  278. }
  279. if (CONFIG_SWSCALE_ALPHA && aDest) {
  280. for (i = 0; i < dstW; i++) {
  281. int val = 1 << 10;
  282. int j;
  283. for (j = 0; j < lumFilterSize; j++)
  284. val += alpSrc[j][i] * lumFilter[j];
  285. if (big_endian) {
  286. AV_WB16(&aDest[i], av_clip_uint16(val >> 11));
  287. } else {
  288. AV_WL16(&aDest[i], av_clip_uint16(val >> 11));
  289. }
  290. }
  291. }
  292. }
  293. static inline void yuv2yuvX16inC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  294. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  295. const int16_t **alpSrc, uint16_t *dest, uint16_t *uDest, uint16_t *vDest, uint16_t *aDest, int dstW, int chrDstW,
  296. enum PixelFormat dstFormat)
  297. {
  298. if (isBE(dstFormat)) {
  299. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  300. chrFilter, chrSrc, chrFilterSize,
  301. alpSrc,
  302. dest, uDest, vDest, aDest,
  303. dstW, chrDstW, 1);
  304. } else {
  305. yuv2yuvX16inC_template(lumFilter, lumSrc, lumFilterSize,
  306. chrFilter, chrSrc, chrFilterSize,
  307. alpSrc,
  308. dest, uDest, vDest, aDest,
  309. dstW, chrDstW, 0);
  310. }
  311. }
  312. static inline void yuv2yuvXinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  313. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  314. const int16_t **alpSrc, uint8_t *dest, uint8_t *uDest, uint8_t *vDest, uint8_t *aDest, int dstW, int chrDstW)
  315. {
  316. //FIXME Optimize (just quickly written not optimized..)
  317. int i;
  318. for (i=0; i<dstW; i++) {
  319. int val=1<<18;
  320. int j;
  321. for (j=0; j<lumFilterSize; j++)
  322. val += lumSrc[j][i] * lumFilter[j];
  323. dest[i]= av_clip_uint8(val>>19);
  324. }
  325. if (uDest)
  326. for (i=0; i<chrDstW; i++) {
  327. int u=1<<18;
  328. int v=1<<18;
  329. int j;
  330. for (j=0; j<chrFilterSize; j++) {
  331. u += chrSrc[j][i] * chrFilter[j];
  332. v += chrSrc[j][i + VOFW] * chrFilter[j];
  333. }
  334. uDest[i]= av_clip_uint8(u>>19);
  335. vDest[i]= av_clip_uint8(v>>19);
  336. }
  337. if (CONFIG_SWSCALE_ALPHA && aDest)
  338. for (i=0; i<dstW; i++) {
  339. int val=1<<18;
  340. int j;
  341. for (j=0; j<lumFilterSize; j++)
  342. val += alpSrc[j][i] * lumFilter[j];
  343. aDest[i]= av_clip_uint8(val>>19);
  344. }
  345. }
  346. static inline void yuv2nv12XinC(const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  347. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  348. uint8_t *dest, uint8_t *uDest, int dstW, int chrDstW, int dstFormat)
  349. {
  350. //FIXME Optimize (just quickly written not optimized..)
  351. int i;
  352. for (i=0; i<dstW; i++) {
  353. int val=1<<18;
  354. int j;
  355. for (j=0; j<lumFilterSize; j++)
  356. val += lumSrc[j][i] * lumFilter[j];
  357. dest[i]= av_clip_uint8(val>>19);
  358. }
  359. if (!uDest)
  360. return;
  361. if (dstFormat == PIX_FMT_NV12)
  362. for (i=0; i<chrDstW; i++) {
  363. int u=1<<18;
  364. int v=1<<18;
  365. int j;
  366. for (j=0; j<chrFilterSize; j++) {
  367. u += chrSrc[j][i] * chrFilter[j];
  368. v += chrSrc[j][i + VOFW] * chrFilter[j];
  369. }
  370. uDest[2*i]= av_clip_uint8(u>>19);
  371. uDest[2*i+1]= av_clip_uint8(v>>19);
  372. }
  373. else
  374. for (i=0; i<chrDstW; i++) {
  375. int u=1<<18;
  376. int v=1<<18;
  377. int j;
  378. for (j=0; j<chrFilterSize; j++) {
  379. u += chrSrc[j][i] * chrFilter[j];
  380. v += chrSrc[j][i + VOFW] * chrFilter[j];
  381. }
  382. uDest[2*i]= av_clip_uint8(v>>19);
  383. uDest[2*i+1]= av_clip_uint8(u>>19);
  384. }
  385. }
  386. #define YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha) \
  387. for (i=0; i<(dstW>>1); i++) {\
  388. int j;\
  389. int Y1 = 1<<18;\
  390. int Y2 = 1<<18;\
  391. int U = 1<<18;\
  392. int V = 1<<18;\
  393. int av_unused A1, A2;\
  394. type av_unused *r, *b, *g;\
  395. const int i2= 2*i;\
  396. \
  397. for (j=0; j<lumFilterSize; j++) {\
  398. Y1 += lumSrc[j][i2] * lumFilter[j];\
  399. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  400. }\
  401. for (j=0; j<chrFilterSize; j++) {\
  402. U += chrSrc[j][i] * chrFilter[j];\
  403. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  404. }\
  405. Y1>>=19;\
  406. Y2>>=19;\
  407. U >>=19;\
  408. V >>=19;\
  409. if (alpha) {\
  410. A1 = 1<<18;\
  411. A2 = 1<<18;\
  412. for (j=0; j<lumFilterSize; j++) {\
  413. A1 += alpSrc[j][i2 ] * lumFilter[j];\
  414. A2 += alpSrc[j][i2+1] * lumFilter[j];\
  415. }\
  416. A1>>=19;\
  417. A2>>=19;\
  418. }
  419. #define YSCALE_YUV_2_PACKEDX_C(type,alpha) \
  420. YSCALE_YUV_2_PACKEDX_NOCLIP_C(type,alpha)\
  421. if ((Y1|Y2|U|V)&256) {\
  422. if (Y1>255) Y1=255; \
  423. else if (Y1<0)Y1=0; \
  424. if (Y2>255) Y2=255; \
  425. else if (Y2<0)Y2=0; \
  426. if (U>255) U=255; \
  427. else if (U<0) U=0; \
  428. if (V>255) V=255; \
  429. else if (V<0) V=0; \
  430. }\
  431. if (alpha && ((A1|A2)&256)) {\
  432. A1=av_clip_uint8(A1);\
  433. A2=av_clip_uint8(A2);\
  434. }
  435. #define YSCALE_YUV_2_PACKEDX_FULL_C(rnd,alpha) \
  436. for (i=0; i<dstW; i++) {\
  437. int j;\
  438. int Y = 0;\
  439. int U = -128<<19;\
  440. int V = -128<<19;\
  441. int av_unused A;\
  442. int R,G,B;\
  443. \
  444. for (j=0; j<lumFilterSize; j++) {\
  445. Y += lumSrc[j][i ] * lumFilter[j];\
  446. }\
  447. for (j=0; j<chrFilterSize; j++) {\
  448. U += chrSrc[j][i ] * chrFilter[j];\
  449. V += chrSrc[j][i+VOFW] * chrFilter[j];\
  450. }\
  451. Y >>=10;\
  452. U >>=10;\
  453. V >>=10;\
  454. if (alpha) {\
  455. A = rnd;\
  456. for (j=0; j<lumFilterSize; j++)\
  457. A += alpSrc[j][i ] * lumFilter[j];\
  458. A >>=19;\
  459. if (A&256)\
  460. A = av_clip_uint8(A);\
  461. }
  462. #define YSCALE_YUV_2_RGBX_FULL_C(rnd,alpha) \
  463. YSCALE_YUV_2_PACKEDX_FULL_C(rnd>>3,alpha)\
  464. Y-= c->yuv2rgb_y_offset;\
  465. Y*= c->yuv2rgb_y_coeff;\
  466. Y+= rnd;\
  467. R= Y + V*c->yuv2rgb_v2r_coeff;\
  468. G= Y + V*c->yuv2rgb_v2g_coeff + U*c->yuv2rgb_u2g_coeff;\
  469. B= Y + U*c->yuv2rgb_u2b_coeff;\
  470. if ((R|G|B)&(0xC0000000)) {\
  471. if (R>=(256<<22)) R=(256<<22)-1; \
  472. else if (R<0)R=0; \
  473. if (G>=(256<<22)) G=(256<<22)-1; \
  474. else if (G<0)G=0; \
  475. if (B>=(256<<22)) B=(256<<22)-1; \
  476. else if (B<0)B=0; \
  477. }
  478. #define YSCALE_YUV_2_GRAY16_C \
  479. for (i=0; i<(dstW>>1); i++) {\
  480. int j;\
  481. int Y1 = 1<<18;\
  482. int Y2 = 1<<18;\
  483. int U = 1<<18;\
  484. int V = 1<<18;\
  485. \
  486. const int i2= 2*i;\
  487. \
  488. for (j=0; j<lumFilterSize; j++) {\
  489. Y1 += lumSrc[j][i2] * lumFilter[j];\
  490. Y2 += lumSrc[j][i2+1] * lumFilter[j];\
  491. }\
  492. Y1>>=11;\
  493. Y2>>=11;\
  494. if ((Y1|Y2|U|V)&65536) {\
  495. if (Y1>65535) Y1=65535; \
  496. else if (Y1<0)Y1=0; \
  497. if (Y2>65535) Y2=65535; \
  498. else if (Y2<0)Y2=0; \
  499. }
  500. #define YSCALE_YUV_2_RGBX_C(type,alpha) \
  501. YSCALE_YUV_2_PACKEDX_C(type,alpha) /* FIXME fix tables so that clipping is not needed and then use _NOCLIP*/\
  502. r = (type *)c->table_rV[V]; \
  503. g = (type *)(c->table_gU[U] + c->table_gV[V]); \
  504. b = (type *)c->table_bU[U];
  505. #define YSCALE_YUV_2_PACKED2_C(type,alpha) \
  506. for (i=0; i<(dstW>>1); i++) { \
  507. const int i2= 2*i; \
  508. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>19; \
  509. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>19; \
  510. int U= (uvbuf0[i ]*uvalpha1+uvbuf1[i ]*uvalpha)>>19; \
  511. int V= (uvbuf0[i+VOFW]*uvalpha1+uvbuf1[i+VOFW]*uvalpha)>>19; \
  512. type av_unused *r, *b, *g; \
  513. int av_unused A1, A2; \
  514. if (alpha) {\
  515. A1= (abuf0[i2 ]*yalpha1+abuf1[i2 ]*yalpha)>>19; \
  516. A2= (abuf0[i2+1]*yalpha1+abuf1[i2+1]*yalpha)>>19; \
  517. }
  518. #define YSCALE_YUV_2_GRAY16_2_C \
  519. for (i=0; i<(dstW>>1); i++) { \
  520. const int i2= 2*i; \
  521. int Y1= (buf0[i2 ]*yalpha1+buf1[i2 ]*yalpha)>>11; \
  522. int Y2= (buf0[i2+1]*yalpha1+buf1[i2+1]*yalpha)>>11;
  523. #define YSCALE_YUV_2_RGB2_C(type,alpha) \
  524. YSCALE_YUV_2_PACKED2_C(type,alpha)\
  525. r = (type *)c->table_rV[V];\
  526. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  527. b = (type *)c->table_bU[U];
  528. #define YSCALE_YUV_2_PACKED1_C(type,alpha) \
  529. for (i=0; i<(dstW>>1); i++) {\
  530. const int i2= 2*i;\
  531. int Y1= buf0[i2 ]>>7;\
  532. int Y2= buf0[i2+1]>>7;\
  533. int U= (uvbuf1[i ])>>7;\
  534. int V= (uvbuf1[i+VOFW])>>7;\
  535. type av_unused *r, *b, *g;\
  536. int av_unused A1, A2;\
  537. if (alpha) {\
  538. A1= abuf0[i2 ]>>7;\
  539. A2= abuf0[i2+1]>>7;\
  540. }
  541. #define YSCALE_YUV_2_GRAY16_1_C \
  542. for (i=0; i<(dstW>>1); i++) {\
  543. const int i2= 2*i;\
  544. int Y1= buf0[i2 ]<<1;\
  545. int Y2= buf0[i2+1]<<1;
  546. #define YSCALE_YUV_2_RGB1_C(type,alpha) \
  547. YSCALE_YUV_2_PACKED1_C(type,alpha)\
  548. r = (type *)c->table_rV[V];\
  549. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  550. b = (type *)c->table_bU[U];
  551. #define YSCALE_YUV_2_PACKED1B_C(type,alpha) \
  552. for (i=0; i<(dstW>>1); i++) {\
  553. const int i2= 2*i;\
  554. int Y1= buf0[i2 ]>>7;\
  555. int Y2= buf0[i2+1]>>7;\
  556. int U= (uvbuf0[i ] + uvbuf1[i ])>>8;\
  557. int V= (uvbuf0[i+VOFW] + uvbuf1[i+VOFW])>>8;\
  558. type av_unused *r, *b, *g;\
  559. int av_unused A1, A2;\
  560. if (alpha) {\
  561. A1= abuf0[i2 ]>>7;\
  562. A2= abuf0[i2+1]>>7;\
  563. }
  564. #define YSCALE_YUV_2_RGB1B_C(type,alpha) \
  565. YSCALE_YUV_2_PACKED1B_C(type,alpha)\
  566. r = (type *)c->table_rV[V];\
  567. g = (type *)(c->table_gU[U] + c->table_gV[V]);\
  568. b = (type *)c->table_bU[U];
  569. #define YSCALE_YUV_2_MONO2_C \
  570. const uint8_t * const d128=dither_8x8_220[y&7];\
  571. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  572. for (i=0; i<dstW-7; i+=8) {\
  573. int acc;\
  574. acc = g[((buf0[i ]*yalpha1+buf1[i ]*yalpha)>>19) + d128[0]];\
  575. acc+= acc + g[((buf0[i+1]*yalpha1+buf1[i+1]*yalpha)>>19) + d128[1]];\
  576. acc+= acc + g[((buf0[i+2]*yalpha1+buf1[i+2]*yalpha)>>19) + d128[2]];\
  577. acc+= acc + g[((buf0[i+3]*yalpha1+buf1[i+3]*yalpha)>>19) + d128[3]];\
  578. acc+= acc + g[((buf0[i+4]*yalpha1+buf1[i+4]*yalpha)>>19) + d128[4]];\
  579. acc+= acc + g[((buf0[i+5]*yalpha1+buf1[i+5]*yalpha)>>19) + d128[5]];\
  580. acc+= acc + g[((buf0[i+6]*yalpha1+buf1[i+6]*yalpha)>>19) + d128[6]];\
  581. acc+= acc + g[((buf0[i+7]*yalpha1+buf1[i+7]*yalpha)>>19) + d128[7]];\
  582. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  583. dest++;\
  584. }
  585. #define YSCALE_YUV_2_MONOX_C \
  586. const uint8_t * const d128=dither_8x8_220[y&7];\
  587. uint8_t *g= c->table_gU[128] + c->table_gV[128];\
  588. int acc=0;\
  589. for (i=0; i<dstW-1; i+=2) {\
  590. int j;\
  591. int Y1=1<<18;\
  592. int Y2=1<<18;\
  593. \
  594. for (j=0; j<lumFilterSize; j++) {\
  595. Y1 += lumSrc[j][i] * lumFilter[j];\
  596. Y2 += lumSrc[j][i+1] * lumFilter[j];\
  597. }\
  598. Y1>>=19;\
  599. Y2>>=19;\
  600. if ((Y1|Y2)&256) {\
  601. if (Y1>255) Y1=255;\
  602. else if (Y1<0)Y1=0;\
  603. if (Y2>255) Y2=255;\
  604. else if (Y2<0)Y2=0;\
  605. }\
  606. acc+= acc + g[Y1+d128[(i+0)&7]];\
  607. acc+= acc + g[Y2+d128[(i+1)&7]];\
  608. if ((i&7)==6) {\
  609. ((uint8_t*)dest)[0]= c->dstFormat == PIX_FMT_MONOBLACK ? acc : ~acc;\
  610. dest++;\
  611. }\
  612. }
  613. #define YSCALE_YUV_2_ANYRGB_C(func, func2, func_g16, func_monoblack)\
  614. switch(c->dstFormat) {\
  615. case PIX_FMT_RGB48BE:\
  616. case PIX_FMT_RGB48LE:\
  617. func(uint8_t,0)\
  618. ((uint8_t*)dest)[ 0]= r[Y1];\
  619. ((uint8_t*)dest)[ 1]= r[Y1];\
  620. ((uint8_t*)dest)[ 2]= g[Y1];\
  621. ((uint8_t*)dest)[ 3]= g[Y1];\
  622. ((uint8_t*)dest)[ 4]= b[Y1];\
  623. ((uint8_t*)dest)[ 5]= b[Y1];\
  624. ((uint8_t*)dest)[ 6]= r[Y2];\
  625. ((uint8_t*)dest)[ 7]= r[Y2];\
  626. ((uint8_t*)dest)[ 8]= g[Y2];\
  627. ((uint8_t*)dest)[ 9]= g[Y2];\
  628. ((uint8_t*)dest)[10]= b[Y2];\
  629. ((uint8_t*)dest)[11]= b[Y2];\
  630. dest+=12;\
  631. }\
  632. break;\
  633. case PIX_FMT_RGBA:\
  634. case PIX_FMT_BGRA:\
  635. if (CONFIG_SMALL) {\
  636. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  637. func(uint32_t,needAlpha)\
  638. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? (A1<<24) : 0);\
  639. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? (A2<<24) : 0);\
  640. }\
  641. } else {\
  642. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  643. func(uint32_t,1)\
  644. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (A1<<24);\
  645. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (A2<<24);\
  646. }\
  647. } else {\
  648. func(uint32_t,0)\
  649. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  650. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  651. }\
  652. }\
  653. }\
  654. break;\
  655. case PIX_FMT_ARGB:\
  656. case PIX_FMT_ABGR:\
  657. if (CONFIG_SMALL) {\
  658. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;\
  659. func(uint32_t,needAlpha)\
  660. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + (needAlpha ? A1 : 0);\
  661. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + (needAlpha ? A2 : 0);\
  662. }\
  663. } else {\
  664. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {\
  665. func(uint32_t,1)\
  666. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1] + A1;\
  667. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2] + A2;\
  668. }\
  669. } else {\
  670. func(uint32_t,0)\
  671. ((uint32_t*)dest)[i2+0]= r[Y1] + g[Y1] + b[Y1];\
  672. ((uint32_t*)dest)[i2+1]= r[Y2] + g[Y2] + b[Y2];\
  673. }\
  674. }\
  675. } \
  676. break;\
  677. case PIX_FMT_RGB24:\
  678. func(uint8_t,0)\
  679. ((uint8_t*)dest)[0]= r[Y1];\
  680. ((uint8_t*)dest)[1]= g[Y1];\
  681. ((uint8_t*)dest)[2]= b[Y1];\
  682. ((uint8_t*)dest)[3]= r[Y2];\
  683. ((uint8_t*)dest)[4]= g[Y2];\
  684. ((uint8_t*)dest)[5]= b[Y2];\
  685. dest+=6;\
  686. }\
  687. break;\
  688. case PIX_FMT_BGR24:\
  689. func(uint8_t,0)\
  690. ((uint8_t*)dest)[0]= b[Y1];\
  691. ((uint8_t*)dest)[1]= g[Y1];\
  692. ((uint8_t*)dest)[2]= r[Y1];\
  693. ((uint8_t*)dest)[3]= b[Y2];\
  694. ((uint8_t*)dest)[4]= g[Y2];\
  695. ((uint8_t*)dest)[5]= r[Y2];\
  696. dest+=6;\
  697. }\
  698. break;\
  699. case PIX_FMT_RGB565:\
  700. case PIX_FMT_BGR565:\
  701. {\
  702. const int dr1= dither_2x2_8[y&1 ][0];\
  703. const int dg1= dither_2x2_4[y&1 ][0];\
  704. const int db1= dither_2x2_8[(y&1)^1][0];\
  705. const int dr2= dither_2x2_8[y&1 ][1];\
  706. const int dg2= dither_2x2_4[y&1 ][1];\
  707. const int db2= dither_2x2_8[(y&1)^1][1];\
  708. func(uint16_t,0)\
  709. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  710. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  711. }\
  712. }\
  713. break;\
  714. case PIX_FMT_RGB555:\
  715. case PIX_FMT_BGR555:\
  716. {\
  717. const int dr1= dither_2x2_8[y&1 ][0];\
  718. const int dg1= dither_2x2_8[y&1 ][1];\
  719. const int db1= dither_2x2_8[(y&1)^1][0];\
  720. const int dr2= dither_2x2_8[y&1 ][1];\
  721. const int dg2= dither_2x2_8[y&1 ][0];\
  722. const int db2= dither_2x2_8[(y&1)^1][1];\
  723. func(uint16_t,0)\
  724. ((uint16_t*)dest)[i2+0]= r[Y1+dr1] + g[Y1+dg1] + b[Y1+db1];\
  725. ((uint16_t*)dest)[i2+1]= r[Y2+dr2] + g[Y2+dg2] + b[Y2+db2];\
  726. }\
  727. }\
  728. break;\
  729. case PIX_FMT_RGB8:\
  730. case PIX_FMT_BGR8:\
  731. {\
  732. const uint8_t * const d64= dither_8x8_73[y&7];\
  733. const uint8_t * const d32= dither_8x8_32[y&7];\
  734. func(uint8_t,0)\
  735. ((uint8_t*)dest)[i2+0]= r[Y1+d32[(i2+0)&7]] + g[Y1+d32[(i2+0)&7]] + b[Y1+d64[(i2+0)&7]];\
  736. ((uint8_t*)dest)[i2+1]= r[Y2+d32[(i2+1)&7]] + g[Y2+d32[(i2+1)&7]] + b[Y2+d64[(i2+1)&7]];\
  737. }\
  738. }\
  739. break;\
  740. case PIX_FMT_RGB4:\
  741. case PIX_FMT_BGR4:\
  742. {\
  743. const uint8_t * const d64= dither_8x8_73 [y&7];\
  744. const uint8_t * const d128=dither_8x8_220[y&7];\
  745. func(uint8_t,0)\
  746. ((uint8_t*)dest)[i]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]]\
  747. + ((r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]])<<4);\
  748. }\
  749. }\
  750. break;\
  751. case PIX_FMT_RGB4_BYTE:\
  752. case PIX_FMT_BGR4_BYTE:\
  753. {\
  754. const uint8_t * const d64= dither_8x8_73 [y&7];\
  755. const uint8_t * const d128=dither_8x8_220[y&7];\
  756. func(uint8_t,0)\
  757. ((uint8_t*)dest)[i2+0]= r[Y1+d128[(i2+0)&7]] + g[Y1+d64[(i2+0)&7]] + b[Y1+d128[(i2+0)&7]];\
  758. ((uint8_t*)dest)[i2+1]= r[Y2+d128[(i2+1)&7]] + g[Y2+d64[(i2+1)&7]] + b[Y2+d128[(i2+1)&7]];\
  759. }\
  760. }\
  761. break;\
  762. case PIX_FMT_MONOBLACK:\
  763. case PIX_FMT_MONOWHITE:\
  764. {\
  765. func_monoblack\
  766. }\
  767. break;\
  768. case PIX_FMT_YUYV422:\
  769. func2\
  770. ((uint8_t*)dest)[2*i2+0]= Y1;\
  771. ((uint8_t*)dest)[2*i2+1]= U;\
  772. ((uint8_t*)dest)[2*i2+2]= Y2;\
  773. ((uint8_t*)dest)[2*i2+3]= V;\
  774. } \
  775. break;\
  776. case PIX_FMT_UYVY422:\
  777. func2\
  778. ((uint8_t*)dest)[2*i2+0]= U;\
  779. ((uint8_t*)dest)[2*i2+1]= Y1;\
  780. ((uint8_t*)dest)[2*i2+2]= V;\
  781. ((uint8_t*)dest)[2*i2+3]= Y2;\
  782. } \
  783. break;\
  784. case PIX_FMT_GRAY16BE:\
  785. func_g16\
  786. ((uint8_t*)dest)[2*i2+0]= Y1>>8;\
  787. ((uint8_t*)dest)[2*i2+1]= Y1;\
  788. ((uint8_t*)dest)[2*i2+2]= Y2>>8;\
  789. ((uint8_t*)dest)[2*i2+3]= Y2;\
  790. } \
  791. break;\
  792. case PIX_FMT_GRAY16LE:\
  793. func_g16\
  794. ((uint8_t*)dest)[2*i2+0]= Y1;\
  795. ((uint8_t*)dest)[2*i2+1]= Y1>>8;\
  796. ((uint8_t*)dest)[2*i2+2]= Y2;\
  797. ((uint8_t*)dest)[2*i2+3]= Y2>>8;\
  798. } \
  799. break;\
  800. }
  801. static inline void yuv2packedXinC(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  802. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  803. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  804. {
  805. int i;
  806. YSCALE_YUV_2_ANYRGB_C(YSCALE_YUV_2_RGBX_C, YSCALE_YUV_2_PACKEDX_C(void,0), YSCALE_YUV_2_GRAY16_C, YSCALE_YUV_2_MONOX_C)
  807. }
  808. static inline void yuv2rgbXinC_full(SwsContext *c, const int16_t *lumFilter, const int16_t **lumSrc, int lumFilterSize,
  809. const int16_t *chrFilter, const int16_t **chrSrc, int chrFilterSize,
  810. const int16_t **alpSrc, uint8_t *dest, int dstW, int y)
  811. {
  812. int i;
  813. int step= c->dstFormatBpp/8;
  814. int aidx= 3;
  815. switch(c->dstFormat) {
  816. case PIX_FMT_ARGB:
  817. dest++;
  818. aidx= 0;
  819. case PIX_FMT_RGB24:
  820. aidx--;
  821. case PIX_FMT_RGBA:
  822. if (CONFIG_SMALL) {
  823. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  824. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  825. dest[aidx]= needAlpha ? A : 255;
  826. dest[0]= R>>22;
  827. dest[1]= G>>22;
  828. dest[2]= B>>22;
  829. dest+= step;
  830. }
  831. } else {
  832. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  833. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  834. dest[aidx]= A;
  835. dest[0]= R>>22;
  836. dest[1]= G>>22;
  837. dest[2]= B>>22;
  838. dest+= step;
  839. }
  840. } else {
  841. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  842. dest[aidx]= 255;
  843. dest[0]= R>>22;
  844. dest[1]= G>>22;
  845. dest[2]= B>>22;
  846. dest+= step;
  847. }
  848. }
  849. }
  850. break;
  851. case PIX_FMT_ABGR:
  852. dest++;
  853. aidx= 0;
  854. case PIX_FMT_BGR24:
  855. aidx--;
  856. case PIX_FMT_BGRA:
  857. if (CONFIG_SMALL) {
  858. int needAlpha = CONFIG_SWSCALE_ALPHA && c->alpPixBuf;
  859. YSCALE_YUV_2_RGBX_FULL_C(1<<21, needAlpha)
  860. dest[aidx]= needAlpha ? A : 255;
  861. dest[0]= B>>22;
  862. dest[1]= G>>22;
  863. dest[2]= R>>22;
  864. dest+= step;
  865. }
  866. } else {
  867. if (CONFIG_SWSCALE_ALPHA && c->alpPixBuf) {
  868. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 1)
  869. dest[aidx]= A;
  870. dest[0]= B>>22;
  871. dest[1]= G>>22;
  872. dest[2]= R>>22;
  873. dest+= step;
  874. }
  875. } else {
  876. YSCALE_YUV_2_RGBX_FULL_C(1<<21, 0)
  877. dest[aidx]= 255;
  878. dest[0]= B>>22;
  879. dest[1]= G>>22;
  880. dest[2]= R>>22;
  881. dest+= step;
  882. }
  883. }
  884. }
  885. break;
  886. default:
  887. assert(0);
  888. }
  889. }
  890. static void fillPlane(uint8_t* plane, int stride, int width, int height, int y, uint8_t val)
  891. {
  892. int i;
  893. uint8_t *ptr = plane + stride*y;
  894. for (i=0; i<height; i++) {
  895. memset(ptr, val, width);
  896. ptr += stride;
  897. }
  898. }
  899. static inline void rgb48ToY(uint8_t *dst, const uint8_t *src, int width,
  900. uint32_t *unused)
  901. {
  902. int i;
  903. for (i = 0; i < width; i++) {
  904. int r = src[i*6+0];
  905. int g = src[i*6+2];
  906. int b = src[i*6+4];
  907. dst[i] = (RY*r + GY*g + BY*b + (33<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  908. }
  909. }
  910. static inline void rgb48ToUV(uint8_t *dstU, uint8_t *dstV,
  911. const uint8_t *src1, const uint8_t *src2,
  912. int width, uint32_t *unused)
  913. {
  914. int i;
  915. assert(src1==src2);
  916. for (i = 0; i < width; i++) {
  917. int r = src1[6*i + 0];
  918. int g = src1[6*i + 2];
  919. int b = src1[6*i + 4];
  920. dstU[i] = (RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  921. dstV[i] = (RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1))) >> RGB2YUV_SHIFT;
  922. }
  923. }
  924. static inline void rgb48ToUV_half(uint8_t *dstU, uint8_t *dstV,
  925. const uint8_t *src1, const uint8_t *src2,
  926. int width, uint32_t *unused)
  927. {
  928. int i;
  929. assert(src1==src2);
  930. for (i = 0; i < width; i++) {
  931. int r= src1[12*i + 0] + src1[12*i + 6];
  932. int g= src1[12*i + 2] + src1[12*i + 8];
  933. int b= src1[12*i + 4] + src1[12*i + 10];
  934. dstU[i]= (RU*r + GU*g + BU*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  935. dstV[i]= (RV*r + GV*g + BV*b + (257<<RGB2YUV_SHIFT)) >> (RGB2YUV_SHIFT+1);
  936. }
  937. }
  938. #define BGR2Y(type, name, shr, shg, shb, maskr, maskg, maskb, RY, GY, BY, S)\
  939. static inline void name(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)\
  940. {\
  941. int i;\
  942. for (i=0; i<width; i++) {\
  943. int b= (((const type*)src)[i]>>shb)&maskb;\
  944. int g= (((const type*)src)[i]>>shg)&maskg;\
  945. int r= (((const type*)src)[i]>>shr)&maskr;\
  946. \
  947. dst[i]= (((RY)*r + (GY)*g + (BY)*b + (33<<((S)-1)))>>(S));\
  948. }\
  949. }
  950. BGR2Y(uint32_t, bgr32ToY,16, 0, 0, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  951. BGR2Y(uint32_t, rgb32ToY, 0, 0,16, 0x00FF, 0xFF00, 0x00FF, RY<< 8, GY , BY<< 8, RGB2YUV_SHIFT+8)
  952. BGR2Y(uint16_t, bgr16ToY, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RY<<11, GY<<5, BY , RGB2YUV_SHIFT+8)
  953. BGR2Y(uint16_t, bgr15ToY, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RY<<10, GY<<5, BY , RGB2YUV_SHIFT+7)
  954. BGR2Y(uint16_t, rgb16ToY, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RY , GY<<5, BY<<11, RGB2YUV_SHIFT+8)
  955. BGR2Y(uint16_t, rgb15ToY, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RY , GY<<5, BY<<10, RGB2YUV_SHIFT+7)
  956. static inline void abgrToA(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  957. {
  958. int i;
  959. for (i=0; i<width; i++) {
  960. dst[i]= src[4*i];
  961. }
  962. }
  963. #define BGR2UV(type, name, shr, shg, shb, maska, maskr, maskg, maskb, RU, GU, BU, RV, GV, BV, S)\
  964. static inline void name(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  965. {\
  966. int i;\
  967. for (i=0; i<width; i++) {\
  968. int b= (((const type*)src)[i]&maskb)>>shb;\
  969. int g= (((const type*)src)[i]&maskg)>>shg;\
  970. int r= (((const type*)src)[i]&maskr)>>shr;\
  971. \
  972. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<((S)-1)))>>(S);\
  973. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<((S)-1)))>>(S);\
  974. }\
  975. }\
  976. static inline void name ## _half(uint8_t *dstU, uint8_t *dstV, const uint8_t *src, const uint8_t *dummy, long width, uint32_t *unused)\
  977. {\
  978. int i;\
  979. for (i=0; i<width; i++) {\
  980. int pix0= ((const type*)src)[2*i+0];\
  981. int pix1= ((const type*)src)[2*i+1];\
  982. int g= (pix0&~(maskr|maskb))+(pix1&~(maskr|maskb));\
  983. int b= ((pix0+pix1-g)&(maskb|(2*maskb)))>>shb;\
  984. int r= ((pix0+pix1-g)&(maskr|(2*maskr)))>>shr;\
  985. g&= maskg|(2*maskg);\
  986. \
  987. g>>=shg;\
  988. \
  989. dstU[i]= ((RU)*r + (GU)*g + (BU)*b + (257<<(S)))>>((S)+1);\
  990. dstV[i]= ((RV)*r + (GV)*g + (BV)*b + (257<<(S)))>>((S)+1);\
  991. }\
  992. }
  993. BGR2UV(uint32_t, bgr32ToUV,16, 0, 0, 0xFF000000, 0xFF0000, 0xFF00, 0x00FF, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  994. BGR2UV(uint32_t, rgb32ToUV, 0, 0,16, 0xFF000000, 0x00FF, 0xFF00, 0xFF0000, RU<< 8, GU , BU<< 8, RV<< 8, GV , BV<< 8, RGB2YUV_SHIFT+8)
  995. BGR2UV(uint16_t, bgr16ToUV, 0, 0, 0, 0, 0x001F, 0x07E0, 0xF800, RU<<11, GU<<5, BU , RV<<11, GV<<5, BV , RGB2YUV_SHIFT+8)
  996. BGR2UV(uint16_t, bgr15ToUV, 0, 0, 0, 0, 0x001F, 0x03E0, 0x7C00, RU<<10, GU<<5, BU , RV<<10, GV<<5, BV , RGB2YUV_SHIFT+7)
  997. BGR2UV(uint16_t, rgb16ToUV, 0, 0, 0, 0, 0xF800, 0x07E0, 0x001F, RU , GU<<5, BU<<11, RV , GV<<5, BV<<11, RGB2YUV_SHIFT+8)
  998. BGR2UV(uint16_t, rgb15ToUV, 0, 0, 0, 0, 0x7C00, 0x03E0, 0x001F, RU , GU<<5, BU<<10, RV , GV<<5, BV<<10, RGB2YUV_SHIFT+7)
  999. static inline void palToY(uint8_t *dst, const uint8_t *src, long width, uint32_t *pal)
  1000. {
  1001. int i;
  1002. for (i=0; i<width; i++) {
  1003. int d= src[i];
  1004. dst[i]= pal[d] & 0xFF;
  1005. }
  1006. }
  1007. static inline void palToUV(uint8_t *dstU, uint8_t *dstV,
  1008. const uint8_t *src1, const uint8_t *src2,
  1009. long width, uint32_t *pal)
  1010. {
  1011. int i;
  1012. assert(src1 == src2);
  1013. for (i=0; i<width; i++) {
  1014. int p= pal[src1[i]];
  1015. dstU[i]= p>>8;
  1016. dstV[i]= p>>16;
  1017. }
  1018. }
  1019. static inline void monowhite2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1020. {
  1021. int i, j;
  1022. for (i=0; i<width/8; i++) {
  1023. int d= ~src[i];
  1024. for(j=0; j<8; j++)
  1025. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1026. }
  1027. }
  1028. static inline void monoblack2Y(uint8_t *dst, const uint8_t *src, long width, uint32_t *unused)
  1029. {
  1030. int i, j;
  1031. for (i=0; i<width/8; i++) {
  1032. int d= src[i];
  1033. for(j=0; j<8; j++)
  1034. dst[8*i+j]= ((d>>(7-j))&1)*255;
  1035. }
  1036. }
  1037. //Note: we have C, MMX, MMX2, 3DNOW versions, there is no 3DNOW+MMX2 one
  1038. //Plain C versions
  1039. #if ((!HAVE_MMX || !CONFIG_GPL) && !HAVE_ALTIVEC) || CONFIG_RUNTIME_CPUDETECT
  1040. #define COMPILE_C
  1041. #endif
  1042. #if ARCH_PPC
  1043. #if HAVE_ALTIVEC || CONFIG_RUNTIME_CPUDETECT
  1044. #define COMPILE_ALTIVEC
  1045. #endif
  1046. #endif //ARCH_PPC
  1047. #if ARCH_X86
  1048. #if ((HAVE_MMX && !HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1049. #define COMPILE_MMX
  1050. #endif
  1051. #if (HAVE_MMX2 || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1052. #define COMPILE_MMX2
  1053. #endif
  1054. #if ((HAVE_AMD3DNOW && !HAVE_MMX2) || CONFIG_RUNTIME_CPUDETECT) && CONFIG_GPL
  1055. #define COMPILE_3DNOW
  1056. #endif
  1057. #endif //ARCH_X86
  1058. #define COMPILE_TEMPLATE_MMX 0
  1059. #define COMPILE_TEMPLATE_MMX2 0
  1060. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1061. #define COMPILE_TEMPLATE_ALTIVEC 0
  1062. #ifdef COMPILE_C
  1063. #define RENAME(a) a ## _C
  1064. #include "swscale_template.c"
  1065. #endif
  1066. #ifdef COMPILE_ALTIVEC
  1067. #undef RENAME
  1068. #undef COMPILE_TEMPLATE_ALTIVEC
  1069. #define COMPILE_TEMPLATE_ALTIVEC 1
  1070. #define RENAME(a) a ## _altivec
  1071. #include "swscale_template.c"
  1072. #endif
  1073. #if ARCH_X86
  1074. //MMX versions
  1075. #ifdef COMPILE_MMX
  1076. #undef RENAME
  1077. #undef COMPILE_TEMPLATE_MMX
  1078. #undef COMPILE_TEMPLATE_MMX2
  1079. #undef COMPILE_TEMPLATE_AMD3DNOW
  1080. #define COMPILE_TEMPLATE_MMX 1
  1081. #define COMPILE_TEMPLATE_MMX2 0
  1082. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1083. #define RENAME(a) a ## _MMX
  1084. #include "swscale_template.c"
  1085. #endif
  1086. //MMX2 versions
  1087. #ifdef COMPILE_MMX2
  1088. #undef RENAME
  1089. #undef COMPILE_TEMPLATE_MMX
  1090. #undef COMPILE_TEMPLATE_MMX2
  1091. #undef COMPILE_TEMPLATE_AMD3DNOW
  1092. #define COMPILE_TEMPLATE_MMX 1
  1093. #define COMPILE_TEMPLATE_MMX2 1
  1094. #define COMPILE_TEMPLATE_AMD3DNOW 0
  1095. #define RENAME(a) a ## _MMX2
  1096. #include "swscale_template.c"
  1097. #endif
  1098. //3DNOW versions
  1099. #ifdef COMPILE_3DNOW
  1100. #undef RENAME
  1101. #undef COMPILE_TEMPLATE_MMX
  1102. #undef COMPILE_TEMPLATE_MMX2
  1103. #undef COMPILE_TEMPLATE_AMD3DNOW
  1104. #define COMPILE_TEMPLATE_MMX 1
  1105. #define COMPILE_TEMPLATE_MMX2 0
  1106. #define COMPILE_TEMPLATE_AMD3DNOW 1
  1107. #define RENAME(a) a ## _3DNow
  1108. #include "swscale_template.c"
  1109. #endif
  1110. #endif //ARCH_X86
  1111. SwsFunc ff_getSwsFunc(SwsContext *c)
  1112. {
  1113. #if CONFIG_RUNTIME_CPUDETECT
  1114. int flags = c->flags;
  1115. #if ARCH_X86 && CONFIG_GPL
  1116. // ordered per speed fastest first
  1117. if (flags & SWS_CPU_CAPS_MMX2) {
  1118. sws_init_swScale_MMX2(c);
  1119. return swScale_MMX2;
  1120. } else if (flags & SWS_CPU_CAPS_3DNOW) {
  1121. sws_init_swScale_3DNow(c);
  1122. return swScale_3DNow;
  1123. } else if (flags & SWS_CPU_CAPS_MMX) {
  1124. sws_init_swScale_MMX(c);
  1125. return swScale_MMX;
  1126. } else {
  1127. sws_init_swScale_C(c);
  1128. return swScale_C;
  1129. }
  1130. #else
  1131. #if ARCH_PPC
  1132. if (flags & SWS_CPU_CAPS_ALTIVEC) {
  1133. sws_init_swScale_altivec(c);
  1134. return swScale_altivec;
  1135. } else {
  1136. sws_init_swScale_C(c);
  1137. return swScale_C;
  1138. }
  1139. #endif
  1140. sws_init_swScale_C(c);
  1141. return swScale_C;
  1142. #endif /* ARCH_X86 && CONFIG_GPL */
  1143. #else //CONFIG_RUNTIME_CPUDETECT
  1144. #if COMPILE_TEMPLATE_MMX2
  1145. sws_init_swScale_MMX2(c);
  1146. return swScale_MMX2;
  1147. #elif COMPILE_TEMPLATE_AMD3DNOW
  1148. sws_init_swScale_3DNow(c);
  1149. return swScale_3DNow;
  1150. #elif COMPILE_TEMPLATE_MMX
  1151. sws_init_swScale_MMX(c);
  1152. return swScale_MMX;
  1153. #elif COMPILE_TEMPLATE_ALTIVEC
  1154. sws_init_swScale_altivec(c);
  1155. return swScale_altivec;
  1156. #else
  1157. sws_init_swScale_C(c);
  1158. return swScale_C;
  1159. #endif
  1160. #endif //!CONFIG_RUNTIME_CPUDETECT
  1161. }
  1162. static int PlanarToNV12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1163. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1164. {
  1165. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1166. /* Copy Y plane */
  1167. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1168. memcpy(dst, src[0], srcSliceH*dstStride[0]);
  1169. else {
  1170. int i;
  1171. const uint8_t *srcPtr= src[0];
  1172. uint8_t *dstPtr= dst;
  1173. for (i=0; i<srcSliceH; i++) {
  1174. memcpy(dstPtr, srcPtr, c->srcW);
  1175. srcPtr+= srcStride[0];
  1176. dstPtr+= dstStride[0];
  1177. }
  1178. }
  1179. dst = dstParam[1] + dstStride[1]*srcSliceY/2;
  1180. if (c->dstFormat == PIX_FMT_NV12)
  1181. interleaveBytes(src[1], src[2], dst, c->srcW/2, srcSliceH/2, srcStride[1], srcStride[2], dstStride[0]);
  1182. else
  1183. interleaveBytes(src[2], src[1], dst, c->srcW/2, srcSliceH/2, srcStride[2], srcStride[1], dstStride[0]);
  1184. return srcSliceH;
  1185. }
  1186. static int PlanarToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1187. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1188. {
  1189. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1190. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1191. return srcSliceH;
  1192. }
  1193. static int PlanarToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1194. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1195. {
  1196. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1197. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0], srcStride[1], dstStride[0]);
  1198. return srcSliceH;
  1199. }
  1200. static int YUV422PToYuy2Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1201. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1202. {
  1203. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1204. yuv422ptoyuy2(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1205. return srcSliceH;
  1206. }
  1207. static int YUV422PToUyvyWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1208. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1209. {
  1210. uint8_t *dst=dstParam[0] + dstStride[0]*srcSliceY;
  1211. yuv422ptouyvy(src[0],src[1],src[2],dst,c->srcW,srcSliceH,srcStride[0],srcStride[1],dstStride[0]);
  1212. return srcSliceH;
  1213. }
  1214. static int YUYV2YUV420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1215. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1216. {
  1217. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1218. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1219. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1220. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1221. if (dstParam[3])
  1222. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1223. return srcSliceH;
  1224. }
  1225. static int YUYV2YUV422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1226. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1227. {
  1228. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1229. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1230. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1231. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1232. return srcSliceH;
  1233. }
  1234. static int UYVY2YUV420Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1235. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1236. {
  1237. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1238. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY/2;
  1239. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY/2;
  1240. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1241. if (dstParam[3])
  1242. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1243. return srcSliceH;
  1244. }
  1245. static int UYVY2YUV422Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1246. int srcSliceH, uint8_t* dstParam[], int dstStride[])
  1247. {
  1248. uint8_t *ydst=dstParam[0] + dstStride[0]*srcSliceY;
  1249. uint8_t *udst=dstParam[1] + dstStride[1]*srcSliceY;
  1250. uint8_t *vdst=dstParam[2] + dstStride[2]*srcSliceY;
  1251. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0], dstStride[1], srcStride[0]);
  1252. return srcSliceH;
  1253. }
  1254. static int pal2rgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1255. int srcSliceH, uint8_t* dst[], int dstStride[])
  1256. {
  1257. const enum PixelFormat srcFormat= c->srcFormat;
  1258. const enum PixelFormat dstFormat= c->dstFormat;
  1259. void (*conv)(const uint8_t *src, uint8_t *dst, long num_pixels,
  1260. const uint8_t *palette)=NULL;
  1261. int i;
  1262. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1263. const uint8_t *srcPtr= src[0];
  1264. if (usePal(srcFormat)) {
  1265. switch (dstFormat) {
  1266. case PIX_FMT_RGB32 : conv = palette8topacked32; break;
  1267. case PIX_FMT_BGR32 : conv = palette8topacked32; break;
  1268. case PIX_FMT_BGR32_1: conv = palette8topacked32; break;
  1269. case PIX_FMT_RGB32_1: conv = palette8topacked32; break;
  1270. case PIX_FMT_RGB24 : conv = palette8topacked24; break;
  1271. case PIX_FMT_BGR24 : conv = palette8topacked24; break;
  1272. }
  1273. }
  1274. if (!conv)
  1275. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1276. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1277. else {
  1278. for (i=0; i<srcSliceH; i++) {
  1279. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  1280. srcPtr+= srcStride[0];
  1281. dstPtr+= dstStride[0];
  1282. }
  1283. }
  1284. return srcSliceH;
  1285. }
  1286. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1287. static int rgb2rgbWrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1288. int srcSliceH, uint8_t* dst[], int dstStride[])
  1289. {
  1290. const enum PixelFormat srcFormat= c->srcFormat;
  1291. const enum PixelFormat dstFormat= c->dstFormat;
  1292. const int srcBpp= (c->srcFormatBpp + 7) >> 3;
  1293. const int dstBpp= (c->dstFormatBpp + 7) >> 3;
  1294. const int srcId= c->srcFormatBpp >> 2; /* 1:0, 4:1, 8:2, 15:3, 16:4, 24:6, 32:8 */
  1295. const int dstId= c->dstFormatBpp >> 2;
  1296. void (*conv)(const uint8_t *src, uint8_t *dst, long src_size)=NULL;
  1297. /* BGR -> BGR */
  1298. if ( (isBGR(srcFormat) && isBGR(dstFormat))
  1299. || (isRGB(srcFormat) && isRGB(dstFormat))) {
  1300. switch(srcId | (dstId<<4)) {
  1301. case 0x34: conv= rgb16to15; break;
  1302. case 0x36: conv= rgb24to15; break;
  1303. case 0x38: conv= rgb32to15; break;
  1304. case 0x43: conv= rgb15to16; break;
  1305. case 0x46: conv= rgb24to16; break;
  1306. case 0x48: conv= rgb32to16; break;
  1307. case 0x63: conv= rgb15to24; break;
  1308. case 0x64: conv= rgb16to24; break;
  1309. case 0x68: conv= rgb32to24; break;
  1310. case 0x83: conv= rgb15to32; break;
  1311. case 0x84: conv= rgb16to32; break;
  1312. case 0x86: conv= rgb24to32; break;
  1313. }
  1314. } else if ( (isBGR(srcFormat) && isRGB(dstFormat))
  1315. || (isRGB(srcFormat) && isBGR(dstFormat))) {
  1316. switch(srcId | (dstId<<4)) {
  1317. case 0x33: conv= rgb15tobgr15; break;
  1318. case 0x34: conv= rgb16tobgr15; break;
  1319. case 0x36: conv= rgb24tobgr15; break;
  1320. case 0x38: conv= rgb32tobgr15; break;
  1321. case 0x43: conv= rgb15tobgr16; break;
  1322. case 0x44: conv= rgb16tobgr16; break;
  1323. case 0x46: conv= rgb24tobgr16; break;
  1324. case 0x48: conv= rgb32tobgr16; break;
  1325. case 0x63: conv= rgb15tobgr24; break;
  1326. case 0x64: conv= rgb16tobgr24; break;
  1327. case 0x66: conv= rgb24tobgr24; break;
  1328. case 0x68: conv= rgb32tobgr24; break;
  1329. case 0x83: conv= rgb15tobgr32; break;
  1330. case 0x84: conv= rgb16tobgr32; break;
  1331. case 0x86: conv= rgb24tobgr32; break;
  1332. case 0x88: conv= rgb32tobgr32; break;
  1333. }
  1334. }
  1335. if (!conv) {
  1336. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1337. sws_format_name(srcFormat), sws_format_name(dstFormat));
  1338. } else {
  1339. const uint8_t *srcPtr= src[0];
  1340. if(srcFormat == PIX_FMT_RGB32_1 || srcFormat == PIX_FMT_BGR32_1)
  1341. srcPtr += ALT32_CORR;
  1342. if (dstStride[0]*srcBpp == srcStride[0]*dstBpp && srcStride[0] > 0)
  1343. conv(srcPtr, dst[0] + dstStride[0]*srcSliceY, srcSliceH*srcStride[0]);
  1344. else {
  1345. int i;
  1346. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1347. for (i=0; i<srcSliceH; i++) {
  1348. conv(srcPtr, dstPtr, c->srcW*srcBpp);
  1349. srcPtr+= srcStride[0];
  1350. dstPtr+= dstStride[0];
  1351. }
  1352. }
  1353. }
  1354. return srcSliceH;
  1355. }
  1356. static int bgr24toyv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1357. int srcSliceH, uint8_t* dst[], int dstStride[])
  1358. {
  1359. rgb24toyv12(
  1360. src[0],
  1361. dst[0]+ srcSliceY *dstStride[0],
  1362. dst[1]+(srcSliceY>>1)*dstStride[1],
  1363. dst[2]+(srcSliceY>>1)*dstStride[2],
  1364. c->srcW, srcSliceH,
  1365. dstStride[0], dstStride[1], srcStride[0]);
  1366. if (dst[3])
  1367. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1368. return srcSliceH;
  1369. }
  1370. static int yvu9toyv12Wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1371. int srcSliceH, uint8_t* dst[], int dstStride[])
  1372. {
  1373. int i;
  1374. /* copy Y */
  1375. if (srcStride[0]==dstStride[0] && srcStride[0] > 0)
  1376. memcpy(dst[0]+ srcSliceY*dstStride[0], src[0], srcStride[0]*srcSliceH);
  1377. else {
  1378. const uint8_t *srcPtr= src[0];
  1379. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1380. for (i=0; i<srcSliceH; i++) {
  1381. memcpy(dstPtr, srcPtr, c->srcW);
  1382. srcPtr+= srcStride[0];
  1383. dstPtr+= dstStride[0];
  1384. }
  1385. }
  1386. if (c->dstFormat==PIX_FMT_YUV420P || c->dstFormat==PIX_FMT_YUVA420P) {
  1387. planar2x(src[1], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1388. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1389. planar2x(src[2], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1390. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1391. } else {
  1392. planar2x(src[1], dst[2] + dstStride[2]*(srcSliceY >> 1), c->chrSrcW,
  1393. srcSliceH >> 2, srcStride[1], dstStride[2]);
  1394. planar2x(src[2], dst[1] + dstStride[1]*(srcSliceY >> 1), c->chrSrcW,
  1395. srcSliceH >> 2, srcStride[2], dstStride[1]);
  1396. }
  1397. if (dst[3])
  1398. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1399. return srcSliceH;
  1400. }
  1401. /* unscaled copy like stuff (assumes nearly identical formats) */
  1402. static int packedCopy(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1403. int srcSliceH, uint8_t* dst[], int dstStride[])
  1404. {
  1405. if (dstStride[0]==srcStride[0] && srcStride[0] > 0)
  1406. memcpy(dst[0] + dstStride[0]*srcSliceY, src[0], srcSliceH*dstStride[0]);
  1407. else {
  1408. int i;
  1409. const uint8_t *srcPtr= src[0];
  1410. uint8_t *dstPtr= dst[0] + dstStride[0]*srcSliceY;
  1411. int length=0;
  1412. /* universal length finder */
  1413. while(length+c->srcW <= FFABS(dstStride[0])
  1414. && length+c->srcW <= FFABS(srcStride[0])) length+= c->srcW;
  1415. assert(length!=0);
  1416. for (i=0; i<srcSliceH; i++) {
  1417. memcpy(dstPtr, srcPtr, length);
  1418. srcPtr+= srcStride[0];
  1419. dstPtr+= dstStride[0];
  1420. }
  1421. }
  1422. return srcSliceH;
  1423. }
  1424. static int planarCopy(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  1425. int srcSliceH, uint8_t* dst[], int dstStride[])
  1426. {
  1427. int plane, i, j;
  1428. for (plane=0; plane<4; plane++) {
  1429. int length= (plane==0 || plane==3) ? c->srcW : -((-c->srcW )>>c->chrDstHSubSample);
  1430. int y= (plane==0 || plane==3) ? srcSliceY: -((-srcSliceY)>>c->chrDstVSubSample);
  1431. int height= (plane==0 || plane==3) ? srcSliceH: -((-srcSliceH)>>c->chrDstVSubSample);
  1432. const uint8_t *srcPtr= src[plane];
  1433. uint8_t *dstPtr= dst[plane] + dstStride[plane]*y;
  1434. if (!dst[plane]) continue;
  1435. // ignore palette for GRAY8
  1436. if (plane == 1 && !dst[2]) continue;
  1437. if (!src[plane] || (plane == 1 && !src[2])) {
  1438. if(is16BPS(c->dstFormat))
  1439. length*=2;
  1440. fillPlane(dst[plane], dstStride[plane], length, height, y, (plane==3) ? 255 : 128);
  1441. } else {
  1442. if(is16BPS(c->srcFormat) && !is16BPS(c->dstFormat)) {
  1443. if (!isBE(c->srcFormat)) srcPtr++;
  1444. for (i=0; i<height; i++) {
  1445. for (j=0; j<length; j++) dstPtr[j] = srcPtr[j<<1];
  1446. srcPtr+= srcStride[plane];
  1447. dstPtr+= dstStride[plane];
  1448. }
  1449. } else if(!is16BPS(c->srcFormat) && is16BPS(c->dstFormat)) {
  1450. for (i=0; i<height; i++) {
  1451. for (j=0; j<length; j++) {
  1452. dstPtr[ j<<1 ] = srcPtr[j];
  1453. dstPtr[(j<<1)+1] = srcPtr[j];
  1454. }
  1455. srcPtr+= srcStride[plane];
  1456. dstPtr+= dstStride[plane];
  1457. }
  1458. } else if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat)
  1459. && isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1460. for (i=0; i<height; i++) {
  1461. for (j=0; j<length; j++)
  1462. ((uint16_t*)dstPtr)[j] = bswap_16(((const uint16_t*)srcPtr)[j]);
  1463. srcPtr+= srcStride[plane];
  1464. dstPtr+= dstStride[plane];
  1465. }
  1466. } else if (dstStride[plane]==srcStride[plane] && srcStride[plane] > 0)
  1467. memcpy(dst[plane] + dstStride[plane]*y, src[plane], height*dstStride[plane]);
  1468. else {
  1469. if(is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1470. length*=2;
  1471. for (i=0; i<height; i++) {
  1472. memcpy(dstPtr, srcPtr, length);
  1473. srcPtr+= srcStride[plane];
  1474. dstPtr+= dstStride[plane];
  1475. }
  1476. }
  1477. }
  1478. }
  1479. return srcSliceH;
  1480. }
  1481. int ff_hardcodedcpuflags(void)
  1482. {
  1483. int flags = 0;
  1484. #if COMPILE_TEMPLATE_MMX2
  1485. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_MMX2;
  1486. #elif COMPILE_TEMPLATE_AMD3DNOW
  1487. flags |= SWS_CPU_CAPS_MMX|SWS_CPU_CAPS_3DNOW;
  1488. #elif COMPILE_TEMPLATE_MMX
  1489. flags |= SWS_CPU_CAPS_MMX;
  1490. #elif COMPILE_TEMPLATE_ALTIVEC
  1491. flags |= SWS_CPU_CAPS_ALTIVEC;
  1492. #elif ARCH_BFIN
  1493. flags |= SWS_CPU_CAPS_BFIN;
  1494. #endif
  1495. return flags;
  1496. }
  1497. void ff_get_unscaled_swscale(SwsContext *c)
  1498. {
  1499. const enum PixelFormat srcFormat = c->srcFormat;
  1500. const enum PixelFormat dstFormat = c->dstFormat;
  1501. const int flags = c->flags;
  1502. const int dstH = c->dstH;
  1503. int needsDither;
  1504. needsDither= (isBGR(dstFormat) || isRGB(dstFormat))
  1505. && c->dstFormatBpp < 24
  1506. && (c->dstFormatBpp < c->srcFormatBpp || (!(isRGB(srcFormat) || isBGR(srcFormat))));
  1507. /* yv12_to_nv12 */
  1508. if ((srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) && (dstFormat == PIX_FMT_NV12 || dstFormat == PIX_FMT_NV21)) {
  1509. c->swScale= PlanarToNV12Wrapper;
  1510. }
  1511. /* yuv2bgr */
  1512. if ((srcFormat==PIX_FMT_YUV420P || srcFormat==PIX_FMT_YUV422P || srcFormat==PIX_FMT_YUVA420P) && (isBGR(dstFormat) || isRGB(dstFormat))
  1513. && !(flags & SWS_ACCURATE_RND) && !(dstH&1)) {
  1514. c->swScale= ff_yuv2rgb_get_func_ptr(c);
  1515. }
  1516. if (srcFormat==PIX_FMT_YUV410P && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_BITEXACT)) {
  1517. c->swScale= yvu9toyv12Wrapper;
  1518. }
  1519. /* bgr24toYV12 */
  1520. if (srcFormat==PIX_FMT_BGR24 && (dstFormat==PIX_FMT_YUV420P || dstFormat==PIX_FMT_YUVA420P) && !(flags & SWS_ACCURATE_RND))
  1521. c->swScale= bgr24toyv12Wrapper;
  1522. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1523. if ( (isBGR(srcFormat) || isRGB(srcFormat))
  1524. && (isBGR(dstFormat) || isRGB(dstFormat))
  1525. && srcFormat != PIX_FMT_BGR8 && dstFormat != PIX_FMT_BGR8
  1526. && srcFormat != PIX_FMT_RGB8 && dstFormat != PIX_FMT_RGB8
  1527. && srcFormat != PIX_FMT_BGR4 && dstFormat != PIX_FMT_BGR4
  1528. && srcFormat != PIX_FMT_RGB4 && dstFormat != PIX_FMT_RGB4
  1529. && srcFormat != PIX_FMT_BGR4_BYTE && dstFormat != PIX_FMT_BGR4_BYTE
  1530. && srcFormat != PIX_FMT_RGB4_BYTE && dstFormat != PIX_FMT_RGB4_BYTE
  1531. && srcFormat != PIX_FMT_MONOBLACK && dstFormat != PIX_FMT_MONOBLACK
  1532. && srcFormat != PIX_FMT_MONOWHITE && dstFormat != PIX_FMT_MONOWHITE
  1533. && dstFormat != PIX_FMT_RGB32_1
  1534. && dstFormat != PIX_FMT_BGR32_1
  1535. && srcFormat != PIX_FMT_RGB48LE && dstFormat != PIX_FMT_RGB48LE
  1536. && srcFormat != PIX_FMT_RGB48BE && dstFormat != PIX_FMT_RGB48BE
  1537. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1538. c->swScale= rgb2rgbWrapper;
  1539. if ((usePal(srcFormat) && (
  1540. dstFormat == PIX_FMT_RGB32 ||
  1541. dstFormat == PIX_FMT_RGB32_1 ||
  1542. dstFormat == PIX_FMT_RGB24 ||
  1543. dstFormat == PIX_FMT_BGR32 ||
  1544. dstFormat == PIX_FMT_BGR32_1 ||
  1545. dstFormat == PIX_FMT_BGR24)))
  1546. c->swScale= pal2rgbWrapper;
  1547. if (srcFormat == PIX_FMT_YUV422P) {
  1548. if (dstFormat == PIX_FMT_YUYV422)
  1549. c->swScale= YUV422PToYuy2Wrapper;
  1550. else if (dstFormat == PIX_FMT_UYVY422)
  1551. c->swScale= YUV422PToUyvyWrapper;
  1552. }
  1553. /* LQ converters if -sws 0 or -sws 4*/
  1554. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1555. /* yv12_to_yuy2 */
  1556. if (srcFormat == PIX_FMT_YUV420P || srcFormat == PIX_FMT_YUVA420P) {
  1557. if (dstFormat == PIX_FMT_YUYV422)
  1558. c->swScale= PlanarToYuy2Wrapper;
  1559. else if (dstFormat == PIX_FMT_UYVY422)
  1560. c->swScale= PlanarToUyvyWrapper;
  1561. }
  1562. }
  1563. if(srcFormat == PIX_FMT_YUYV422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1564. c->swScale= YUYV2YUV420Wrapper;
  1565. if(srcFormat == PIX_FMT_UYVY422 && (dstFormat == PIX_FMT_YUV420P || dstFormat == PIX_FMT_YUVA420P))
  1566. c->swScale= UYVY2YUV420Wrapper;
  1567. if(srcFormat == PIX_FMT_YUYV422 && dstFormat == PIX_FMT_YUV422P)
  1568. c->swScale= YUYV2YUV422Wrapper;
  1569. if(srcFormat == PIX_FMT_UYVY422 && dstFormat == PIX_FMT_YUV422P)
  1570. c->swScale= UYVY2YUV422Wrapper;
  1571. #ifdef COMPILE_ALTIVEC
  1572. if ((c->flags & SWS_CPU_CAPS_ALTIVEC) &&
  1573. !(c->flags & SWS_BITEXACT) &&
  1574. srcFormat == PIX_FMT_YUV420P) {
  1575. // unscaled YV12 -> packed YUV, we want speed
  1576. if (dstFormat == PIX_FMT_YUYV422)
  1577. c->swScale= yv12toyuy2_unscaled_altivec;
  1578. else if (dstFormat == PIX_FMT_UYVY422)
  1579. c->swScale= yv12touyvy_unscaled_altivec;
  1580. }
  1581. #endif
  1582. /* simple copy */
  1583. if ( srcFormat == dstFormat
  1584. || (srcFormat == PIX_FMT_YUVA420P && dstFormat == PIX_FMT_YUV420P)
  1585. || (srcFormat == PIX_FMT_YUV420P && dstFormat == PIX_FMT_YUVA420P)
  1586. || (isPlanarYUV(srcFormat) && isGray(dstFormat))
  1587. || (isPlanarYUV(dstFormat) && isGray(srcFormat))
  1588. || (isGray(dstFormat) && isGray(srcFormat))
  1589. || (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat)
  1590. && c->chrDstHSubSample == c->chrSrcHSubSample
  1591. && c->chrDstVSubSample == c->chrSrcVSubSample
  1592. && dstFormat != PIX_FMT_NV12 && dstFormat != PIX_FMT_NV21
  1593. && srcFormat != PIX_FMT_NV12 && srcFormat != PIX_FMT_NV21))
  1594. {
  1595. if (isPacked(c->srcFormat))
  1596. c->swScale= packedCopy;
  1597. else /* Planar YUV or gray */
  1598. c->swScale= planarCopy;
  1599. }
  1600. #if ARCH_BFIN
  1601. if (flags & SWS_CPU_CAPS_BFIN)
  1602. ff_bfin_get_unscaled_swscale (c);
  1603. #endif
  1604. }
  1605. static void reset_ptr(const uint8_t* src[], int format)
  1606. {
  1607. if(!isALPHA(format))
  1608. src[3]=NULL;
  1609. if(!isPlanarYUV(format)) {
  1610. src[3]=src[2]=NULL;
  1611. if (!usePal(format))
  1612. src[1]= NULL;
  1613. }
  1614. }
  1615. /**
  1616. * swscale wrapper, so we don't need to export the SwsContext.
  1617. * Assumes planar YUV to be in YUV order instead of YVU.
  1618. */
  1619. int sws_scale(SwsContext *c, const uint8_t* const src[], const int srcStride[], int srcSliceY,
  1620. int srcSliceH, uint8_t* const dst[], const int dstStride[])
  1621. {
  1622. int i;
  1623. const uint8_t* src2[4]= {src[0], src[1], src[2], src[3]};
  1624. uint8_t* dst2[4]= {dst[0], dst[1], dst[2], dst[3]};
  1625. // do not mess up sliceDir if we have a "trailing" 0-size slice
  1626. if (srcSliceH == 0)
  1627. return 0;
  1628. if (c->sliceDir == 0 && srcSliceY != 0 && srcSliceY + srcSliceH != c->srcH) {
  1629. av_log(c, AV_LOG_ERROR, "Slices start in the middle!\n");
  1630. return 0;
  1631. }
  1632. if (c->sliceDir == 0) {
  1633. if (srcSliceY == 0) c->sliceDir = 1; else c->sliceDir = -1;
  1634. }
  1635. if (usePal(c->srcFormat)) {
  1636. for (i=0; i<256; i++) {
  1637. int p, r, g, b,y,u,v;
  1638. if(c->srcFormat == PIX_FMT_PAL8) {
  1639. p=((const uint32_t*)(src[1]))[i];
  1640. r= (p>>16)&0xFF;
  1641. g= (p>> 8)&0xFF;
  1642. b= p &0xFF;
  1643. } else if(c->srcFormat == PIX_FMT_RGB8) {
  1644. r= (i>>5 )*36;
  1645. g= ((i>>2)&7)*36;
  1646. b= (i&3 )*85;
  1647. } else if(c->srcFormat == PIX_FMT_BGR8) {
  1648. b= (i>>6 )*85;
  1649. g= ((i>>3)&7)*36;
  1650. r= (i&7 )*36;
  1651. } else if(c->srcFormat == PIX_FMT_RGB4_BYTE) {
  1652. r= (i>>3 )*255;
  1653. g= ((i>>1)&3)*85;
  1654. b= (i&1 )*255;
  1655. } else {
  1656. assert(c->srcFormat == PIX_FMT_BGR4_BYTE);
  1657. b= (i>>3 )*255;
  1658. g= ((i>>1)&3)*85;
  1659. r= (i&1 )*255;
  1660. }
  1661. y= av_clip_uint8((RY*r + GY*g + BY*b + ( 33<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1662. u= av_clip_uint8((RU*r + GU*g + BU*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1663. v= av_clip_uint8((RV*r + GV*g + BV*b + (257<<(RGB2YUV_SHIFT-1)))>>RGB2YUV_SHIFT);
  1664. c->pal_yuv[i]= y + (u<<8) + (v<<16);
  1665. switch(c->dstFormat) {
  1666. case PIX_FMT_BGR32:
  1667. #if !HAVE_BIGENDIAN
  1668. case PIX_FMT_RGB24:
  1669. #endif
  1670. c->pal_rgb[i]= r + (g<<8) + (b<<16);
  1671. break;
  1672. case PIX_FMT_BGR32_1:
  1673. #if HAVE_BIGENDIAN
  1674. case PIX_FMT_BGR24:
  1675. #endif
  1676. c->pal_rgb[i]= (r + (g<<8) + (b<<16)) << 8;
  1677. break;
  1678. case PIX_FMT_RGB32_1:
  1679. #if HAVE_BIGENDIAN
  1680. case PIX_FMT_RGB24:
  1681. #endif
  1682. c->pal_rgb[i]= (b + (g<<8) + (r<<16)) << 8;
  1683. break;
  1684. case PIX_FMT_RGB32:
  1685. #if !HAVE_BIGENDIAN
  1686. case PIX_FMT_BGR24:
  1687. #endif
  1688. default:
  1689. c->pal_rgb[i]= b + (g<<8) + (r<<16);
  1690. }
  1691. }
  1692. }
  1693. // copy strides, so they can safely be modified
  1694. if (c->sliceDir == 1) {
  1695. // slices go from top to bottom
  1696. int srcStride2[4]= {srcStride[0], srcStride[1], srcStride[2], srcStride[3]};
  1697. int dstStride2[4]= {dstStride[0], dstStride[1], dstStride[2], dstStride[3]};
  1698. reset_ptr(src2, c->srcFormat);
  1699. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1700. /* reset slice direction at end of frame */
  1701. if (srcSliceY + srcSliceH == c->srcH)
  1702. c->sliceDir = 0;
  1703. return c->swScale(c, src2, srcStride2, srcSliceY, srcSliceH, dst2, dstStride2);
  1704. } else {
  1705. // slices go from bottom to top => we flip the image internally
  1706. int srcStride2[4]= {-srcStride[0], -srcStride[1], -srcStride[2], -srcStride[3]};
  1707. int dstStride2[4]= {-dstStride[0], -dstStride[1], -dstStride[2], -dstStride[3]};
  1708. src2[0] += (srcSliceH-1)*srcStride[0];
  1709. if (!usePal(c->srcFormat))
  1710. src2[1] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[1];
  1711. src2[2] += ((srcSliceH>>c->chrSrcVSubSample)-1)*srcStride[2];
  1712. src2[3] += (srcSliceH-1)*srcStride[3];
  1713. dst2[0] += ( c->dstH -1)*dstStride[0];
  1714. dst2[1] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[1];
  1715. dst2[2] += ((c->dstH>>c->chrDstVSubSample)-1)*dstStride[2];
  1716. dst2[3] += ( c->dstH -1)*dstStride[3];
  1717. reset_ptr(src2, c->srcFormat);
  1718. reset_ptr((const uint8_t**)dst2, c->dstFormat);
  1719. /* reset slice direction at end of frame */
  1720. if (!srcSliceY)
  1721. c->sliceDir = 0;
  1722. return c->swScale(c, src2, srcStride2, c->srcH-srcSliceY-srcSliceH, srcSliceH, dst2, dstStride2);
  1723. }
  1724. }
  1725. #if LIBSWSCALE_VERSION_MAJOR < 1
  1726. int sws_scale_ordered(SwsContext *c, const uint8_t* const src[], int srcStride[], int srcSliceY,
  1727. int srcSliceH, uint8_t* dst[], int dstStride[])
  1728. {
  1729. return sws_scale(c, src, srcStride, srcSliceY, srcSliceH, dst, dstStride);
  1730. }
  1731. #endif