swscale_unscaled.c 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813
  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. #include <inttypes.h>
  21. #include <string.h>
  22. #include <math.h>
  23. #include <stdio.h>
  24. #include "config.h"
  25. #include "swscale.h"
  26. #include "swscale_internal.h"
  27. #include "rgb2rgb.h"
  28. #include "libavutil/intreadwrite.h"
  29. #include "libavutil/cpu.h"
  30. #include "libavutil/avutil.h"
  31. #include "libavutil/mathematics.h"
  32. #include "libavutil/bswap.h"
  33. #include "libavutil/pixdesc.h"
  34. #include "libavutil/avassert.h"
  35. DECLARE_ALIGNED(8, static const uint8_t, dithers)[8][8][8]={
  36. {
  37. { 0, 1, 0, 1, 0, 1, 0, 1,},
  38. { 1, 0, 1, 0, 1, 0, 1, 0,},
  39. { 0, 1, 0, 1, 0, 1, 0, 1,},
  40. { 1, 0, 1, 0, 1, 0, 1, 0,},
  41. { 0, 1, 0, 1, 0, 1, 0, 1,},
  42. { 1, 0, 1, 0, 1, 0, 1, 0,},
  43. { 0, 1, 0, 1, 0, 1, 0, 1,},
  44. { 1, 0, 1, 0, 1, 0, 1, 0,},
  45. },{
  46. { 1, 2, 1, 2, 1, 2, 1, 2,},
  47. { 3, 0, 3, 0, 3, 0, 3, 0,},
  48. { 1, 2, 1, 2, 1, 2, 1, 2,},
  49. { 3, 0, 3, 0, 3, 0, 3, 0,},
  50. { 1, 2, 1, 2, 1, 2, 1, 2,},
  51. { 3, 0, 3, 0, 3, 0, 3, 0,},
  52. { 1, 2, 1, 2, 1, 2, 1, 2,},
  53. { 3, 0, 3, 0, 3, 0, 3, 0,},
  54. },{
  55. { 2, 4, 3, 5, 2, 4, 3, 5,},
  56. { 6, 0, 7, 1, 6, 0, 7, 1,},
  57. { 3, 5, 2, 4, 3, 5, 2, 4,},
  58. { 7, 1, 6, 0, 7, 1, 6, 0,},
  59. { 2, 4, 3, 5, 2, 4, 3, 5,},
  60. { 6, 0, 7, 1, 6, 0, 7, 1,},
  61. { 3, 5, 2, 4, 3, 5, 2, 4,},
  62. { 7, 1, 6, 0, 7, 1, 6, 0,},
  63. },{
  64. { 4, 8, 7, 11, 4, 8, 7, 11,},
  65. { 12, 0, 15, 3, 12, 0, 15, 3,},
  66. { 6, 10, 5, 9, 6, 10, 5, 9,},
  67. { 14, 2, 13, 1, 14, 2, 13, 1,},
  68. { 4, 8, 7, 11, 4, 8, 7, 11,},
  69. { 12, 0, 15, 3, 12, 0, 15, 3,},
  70. { 6, 10, 5, 9, 6, 10, 5, 9,},
  71. { 14, 2, 13, 1, 14, 2, 13, 1,},
  72. },{
  73. { 9, 17, 15, 23, 8, 16, 14, 22,},
  74. { 25, 1, 31, 7, 24, 0, 30, 6,},
  75. { 13, 21, 11, 19, 12, 20, 10, 18,},
  76. { 29, 5, 27, 3, 28, 4, 26, 2,},
  77. { 8, 16, 14, 22, 9, 17, 15, 23,},
  78. { 24, 0, 30, 6, 25, 1, 31, 7,},
  79. { 12, 20, 10, 18, 13, 21, 11, 19,},
  80. { 28, 4, 26, 2, 29, 5, 27, 3,},
  81. },{
  82. { 18, 34, 30, 46, 17, 33, 29, 45,},
  83. { 50, 2, 62, 14, 49, 1, 61, 13,},
  84. { 26, 42, 22, 38, 25, 41, 21, 37,},
  85. { 58, 10, 54, 6, 57, 9, 53, 5,},
  86. { 16, 32, 28, 44, 19, 35, 31, 47,},
  87. { 48, 0, 60, 12, 51, 3, 63, 15,},
  88. { 24, 40, 20, 36, 27, 43, 23, 39,},
  89. { 56, 8, 52, 4, 59, 11, 55, 7,},
  90. },{
  91. { 18, 34, 30, 46, 17, 33, 29, 45,},
  92. { 50, 2, 62, 14, 49, 1, 61, 13,},
  93. { 26, 42, 22, 38, 25, 41, 21, 37,},
  94. { 58, 10, 54, 6, 57, 9, 53, 5,},
  95. { 16, 32, 28, 44, 19, 35, 31, 47,},
  96. { 48, 0, 60, 12, 51, 3, 63, 15,},
  97. { 24, 40, 20, 36, 27, 43, 23, 39,},
  98. { 56, 8, 52, 4, 59, 11, 55, 7,},
  99. },{
  100. { 36, 68, 60, 92, 34, 66, 58, 90,},
  101. { 100, 4,124, 28, 98, 2,122, 26,},
  102. { 52, 84, 44, 76, 50, 82, 42, 74,},
  103. { 116, 20,108, 12,114, 18,106, 10,},
  104. { 32, 64, 56, 88, 38, 70, 62, 94,},
  105. { 96, 0,120, 24,102, 6,126, 30,},
  106. { 48, 80, 40, 72, 54, 86, 46, 78,},
  107. { 112, 16,104, 8,118, 22,110, 14,},
  108. }};
  109. static const uint16_t dither_scale[15][16]={
  110. { 2, 3, 3, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5,},
  111. { 2, 3, 7, 7, 13, 13, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25,},
  112. { 3, 3, 4, 15, 15, 29, 57, 57, 57, 113, 113, 113, 113, 113, 113, 113,},
  113. { 3, 4, 4, 5, 31, 31, 61, 121, 241, 241, 241, 241, 481, 481, 481, 481,},
  114. { 3, 4, 5, 5, 6, 63, 63, 125, 249, 497, 993, 993, 993, 993, 993, 1985,},
  115. { 3, 5, 6, 6, 6, 7, 127, 127, 253, 505, 1009, 2017, 4033, 4033, 4033, 4033,},
  116. { 3, 5, 6, 7, 7, 7, 8, 255, 255, 509, 1017, 2033, 4065, 8129,16257,16257,},
  117. { 3, 5, 6, 8, 8, 8, 8, 9, 511, 511, 1021, 2041, 4081, 8161,16321,32641,},
  118. { 3, 5, 7, 8, 9, 9, 9, 9, 10, 1023, 1023, 2045, 4089, 8177,16353,32705,},
  119. { 3, 5, 7, 8, 10, 10, 10, 10, 10, 11, 2047, 2047, 4093, 8185,16369,32737,},
  120. { 3, 5, 7, 8, 10, 11, 11, 11, 11, 11, 12, 4095, 4095, 8189,16377,32753,},
  121. { 3, 5, 7, 9, 10, 12, 12, 12, 12, 12, 12, 13, 8191, 8191,16381,32761,},
  122. { 3, 5, 7, 9, 10, 12, 13, 13, 13, 13, 13, 13, 14,16383,16383,32765,},
  123. { 3, 5, 7, 9, 10, 12, 14, 14, 14, 14, 14, 14, 14, 15,32767,32767,},
  124. { 3, 5, 7, 9, 11, 12, 14, 15, 15, 15, 15, 15, 15, 15, 16,65535,},
  125. };
  126. static void fillPlane(uint8_t *plane, int stride, int width, int height, int y,
  127. uint8_t val)
  128. {
  129. int i;
  130. uint8_t *ptr = plane + stride * y;
  131. for (i = 0; i < height; i++) {
  132. memset(ptr, val, width);
  133. ptr += stride;
  134. }
  135. }
  136. static void copyPlane(const uint8_t *src, int srcStride,
  137. int srcSliceY, int srcSliceH, int width,
  138. uint8_t *dst, int dstStride)
  139. {
  140. dst += dstStride * srcSliceY;
  141. if (dstStride == srcStride && srcStride > 0) {
  142. memcpy(dst, src, srcSliceH * dstStride);
  143. } else {
  144. int i;
  145. for (i = 0; i < srcSliceH; i++) {
  146. memcpy(dst, src, width);
  147. src += srcStride;
  148. dst += dstStride;
  149. }
  150. }
  151. }
  152. static int planarToNv12Wrapper(SwsContext *c, const uint8_t *src[],
  153. int srcStride[], int srcSliceY,
  154. int srcSliceH, uint8_t *dstParam[],
  155. int dstStride[])
  156. {
  157. uint8_t *dst = dstParam[1] + dstStride[1] * srcSliceY / 2;
  158. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  159. dstParam[0], dstStride[0]);
  160. if (c->dstFormat == AV_PIX_FMT_NV12)
  161. interleaveBytes(src[1], src[2], dst, c->srcW / 2, srcSliceH / 2,
  162. srcStride[1], srcStride[2], dstStride[1]);
  163. else
  164. interleaveBytes(src[2], src[1], dst, c->srcW / 2, srcSliceH / 2,
  165. srcStride[2], srcStride[1], dstStride[1]);
  166. return srcSliceH;
  167. }
  168. static int nv12ToPlanarWrapper(SwsContext *c, const uint8_t *src[],
  169. int srcStride[], int srcSliceY,
  170. int srcSliceH, uint8_t *dstParam[],
  171. int dstStride[])
  172. {
  173. uint8_t *dst1 = dstParam[1] + dstStride[1] * srcSliceY / 2;
  174. uint8_t *dst2 = dstParam[2] + dstStride[2] * srcSliceY / 2;
  175. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  176. dstParam[0], dstStride[0]);
  177. if (c->srcFormat == AV_PIX_FMT_NV12)
  178. deinterleaveBytes(src[1], dst1, dst2,c->srcW / 2, srcSliceH / 2,
  179. srcStride[1], dstStride[1], dstStride[2]);
  180. else
  181. deinterleaveBytes(src[1], dst2, dst1, c->srcW / 2, srcSliceH / 2,
  182. srcStride[1], dstStride[2], dstStride[1]);
  183. return srcSliceH;
  184. }
  185. static int planarToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
  186. int srcStride[], int srcSliceY, int srcSliceH,
  187. uint8_t *dstParam[], int dstStride[])
  188. {
  189. uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
  190. yv12toyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
  191. srcStride[1], dstStride[0]);
  192. return srcSliceH;
  193. }
  194. static int planarToUyvyWrapper(SwsContext *c, const uint8_t *src[],
  195. int srcStride[], int srcSliceY, int srcSliceH,
  196. uint8_t *dstParam[], int dstStride[])
  197. {
  198. uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
  199. yv12touyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
  200. srcStride[1], dstStride[0]);
  201. return srcSliceH;
  202. }
  203. static int yuv422pToYuy2Wrapper(SwsContext *c, const uint8_t *src[],
  204. int srcStride[], int srcSliceY, int srcSliceH,
  205. uint8_t *dstParam[], int dstStride[])
  206. {
  207. uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
  208. yuv422ptoyuy2(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
  209. srcStride[1], dstStride[0]);
  210. return srcSliceH;
  211. }
  212. static int yuv422pToUyvyWrapper(SwsContext *c, const uint8_t *src[],
  213. int srcStride[], int srcSliceY, int srcSliceH,
  214. uint8_t *dstParam[], int dstStride[])
  215. {
  216. uint8_t *dst = dstParam[0] + dstStride[0] * srcSliceY;
  217. yuv422ptouyvy(src[0], src[1], src[2], dst, c->srcW, srcSliceH, srcStride[0],
  218. srcStride[1], dstStride[0]);
  219. return srcSliceH;
  220. }
  221. static int yuyvToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
  222. int srcStride[], int srcSliceY, int srcSliceH,
  223. uint8_t *dstParam[], int dstStride[])
  224. {
  225. uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
  226. uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
  227. uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
  228. yuyvtoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
  229. dstStride[1], srcStride[0]);
  230. if (dstParam[3])
  231. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  232. return srcSliceH;
  233. }
  234. static int yuyvToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
  235. int srcStride[], int srcSliceY, int srcSliceH,
  236. uint8_t *dstParam[], int dstStride[])
  237. {
  238. uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
  239. uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
  240. uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
  241. yuyvtoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
  242. dstStride[1], srcStride[0]);
  243. return srcSliceH;
  244. }
  245. static int uyvyToYuv420Wrapper(SwsContext *c, const uint8_t *src[],
  246. int srcStride[], int srcSliceY, int srcSliceH,
  247. uint8_t *dstParam[], int dstStride[])
  248. {
  249. uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
  250. uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY / 2;
  251. uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY / 2;
  252. uyvytoyuv420(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
  253. dstStride[1], srcStride[0]);
  254. if (dstParam[3])
  255. fillPlane(dstParam[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  256. return srcSliceH;
  257. }
  258. static int uyvyToYuv422Wrapper(SwsContext *c, const uint8_t *src[],
  259. int srcStride[], int srcSliceY, int srcSliceH,
  260. uint8_t *dstParam[], int dstStride[])
  261. {
  262. uint8_t *ydst = dstParam[0] + dstStride[0] * srcSliceY;
  263. uint8_t *udst = dstParam[1] + dstStride[1] * srcSliceY;
  264. uint8_t *vdst = dstParam[2] + dstStride[2] * srcSliceY;
  265. uyvytoyuv422(ydst, udst, vdst, src[0], c->srcW, srcSliceH, dstStride[0],
  266. dstStride[1], srcStride[0]);
  267. return srcSliceH;
  268. }
  269. static void gray8aToPacked32(const uint8_t *src, uint8_t *dst, int num_pixels,
  270. const uint8_t *palette)
  271. {
  272. int i;
  273. for (i = 0; i < num_pixels; i++)
  274. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | (src[(i << 1) + 1] << 24);
  275. }
  276. static void gray8aToPacked32_1(const uint8_t *src, uint8_t *dst, int num_pixels,
  277. const uint8_t *palette)
  278. {
  279. int i;
  280. for (i = 0; i < num_pixels; i++)
  281. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i << 1]] | src[(i << 1) + 1];
  282. }
  283. static void gray8aToPacked24(const uint8_t *src, uint8_t *dst, int num_pixels,
  284. const uint8_t *palette)
  285. {
  286. int i;
  287. for (i = 0; i < num_pixels; i++) {
  288. //FIXME slow?
  289. dst[0] = palette[src[i << 1] * 4 + 0];
  290. dst[1] = palette[src[i << 1] * 4 + 1];
  291. dst[2] = palette[src[i << 1] * 4 + 2];
  292. dst += 3;
  293. }
  294. }
  295. static int packed_16bpc_bswap(SwsContext *c, const uint8_t *src[],
  296. int srcStride[], int srcSliceY, int srcSliceH,
  297. uint8_t *dst[], int dstStride[])
  298. {
  299. int i, j, p;
  300. for (p = 0; p < 4; p++) {
  301. int srcstr = srcStride[p] / 2;
  302. int dststr = dstStride[p] / 2;
  303. uint16_t *dstPtr = (uint16_t *) dst[p];
  304. const uint16_t *srcPtr = (const uint16_t *) src[p];
  305. int min_stride = FFMIN(FFABS(srcstr), FFABS(dststr));
  306. if(!dstPtr || !srcPtr)
  307. continue;
  308. dstPtr += (srcSliceY >> c->chrDstVSubSample) * dststr;
  309. for (i = 0; i < (srcSliceH >> c->chrDstVSubSample); i++) {
  310. for (j = 0; j < min_stride; j++) {
  311. dstPtr[j] = av_bswap16(srcPtr[j]);
  312. }
  313. srcPtr += srcstr;
  314. dstPtr += dststr;
  315. }
  316. }
  317. return srcSliceH;
  318. }
  319. static int palToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
  320. int srcSliceY, int srcSliceH, uint8_t *dst[],
  321. int dstStride[])
  322. {
  323. const enum AVPixelFormat srcFormat = c->srcFormat;
  324. const enum AVPixelFormat dstFormat = c->dstFormat;
  325. void (*conv)(const uint8_t *src, uint8_t *dst, int num_pixels,
  326. const uint8_t *palette) = NULL;
  327. int i;
  328. uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
  329. const uint8_t *srcPtr = src[0];
  330. if (srcFormat == AV_PIX_FMT_YA8) {
  331. switch (dstFormat) {
  332. case AV_PIX_FMT_RGB32 : conv = gray8aToPacked32; break;
  333. case AV_PIX_FMT_BGR32 : conv = gray8aToPacked32; break;
  334. case AV_PIX_FMT_BGR32_1: conv = gray8aToPacked32_1; break;
  335. case AV_PIX_FMT_RGB32_1: conv = gray8aToPacked32_1; break;
  336. case AV_PIX_FMT_RGB24 : conv = gray8aToPacked24; break;
  337. case AV_PIX_FMT_BGR24 : conv = gray8aToPacked24; break;
  338. }
  339. } else if (usePal(srcFormat)) {
  340. switch (dstFormat) {
  341. case AV_PIX_FMT_RGB32 : conv = sws_convertPalette8ToPacked32; break;
  342. case AV_PIX_FMT_BGR32 : conv = sws_convertPalette8ToPacked32; break;
  343. case AV_PIX_FMT_BGR32_1: conv = sws_convertPalette8ToPacked32; break;
  344. case AV_PIX_FMT_RGB32_1: conv = sws_convertPalette8ToPacked32; break;
  345. case AV_PIX_FMT_RGB24 : conv = sws_convertPalette8ToPacked24; break;
  346. case AV_PIX_FMT_BGR24 : conv = sws_convertPalette8ToPacked24; break;
  347. }
  348. }
  349. if (!conv)
  350. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  351. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  352. else {
  353. for (i = 0; i < srcSliceH; i++) {
  354. conv(srcPtr, dstPtr, c->srcW, (uint8_t *) c->pal_rgb);
  355. srcPtr += srcStride[0];
  356. dstPtr += dstStride[0];
  357. }
  358. }
  359. return srcSliceH;
  360. }
  361. static void packed16togbra16(const uint8_t *src, int srcStride,
  362. uint16_t *dst[], int dstStride[], int srcSliceH,
  363. int src_alpha, int swap, int shift, int width)
  364. {
  365. int x, h, i;
  366. int dst_alpha = dst[3] != NULL;
  367. for (h = 0; h < srcSliceH; h++) {
  368. uint16_t *src_line = (uint16_t *)(src + srcStride * h);
  369. switch (swap) {
  370. case 3:
  371. if (src_alpha && dst_alpha) {
  372. for (x = 0; x < width; x++) {
  373. dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  374. dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  375. dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  376. dst[3][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  377. }
  378. } else if (dst_alpha) {
  379. for (x = 0; x < width; x++) {
  380. dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  381. dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  382. dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  383. dst[3][x] = 0xFFFF;
  384. }
  385. } else if (src_alpha) {
  386. for (x = 0; x < width; x++) {
  387. dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  388. dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  389. dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  390. src_line++;
  391. }
  392. } else {
  393. for (x = 0; x < width; x++) {
  394. dst[0][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  395. dst[1][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  396. dst[2][x] = av_bswap16(av_bswap16(*src_line++) >> shift);
  397. }
  398. }
  399. break;
  400. case 2:
  401. if (src_alpha && dst_alpha) {
  402. for (x = 0; x < width; x++) {
  403. dst[0][x] = av_bswap16(*src_line++ >> shift);
  404. dst[1][x] = av_bswap16(*src_line++ >> shift);
  405. dst[2][x] = av_bswap16(*src_line++ >> shift);
  406. dst[3][x] = av_bswap16(*src_line++ >> shift);
  407. }
  408. } else if (dst_alpha) {
  409. for (x = 0; x < width; x++) {
  410. dst[0][x] = av_bswap16(*src_line++ >> shift);
  411. dst[1][x] = av_bswap16(*src_line++ >> shift);
  412. dst[2][x] = av_bswap16(*src_line++ >> shift);
  413. dst[3][x] = 0xFFFF;
  414. }
  415. } else if (src_alpha) {
  416. for (x = 0; x < width; x++) {
  417. dst[0][x] = av_bswap16(*src_line++ >> shift);
  418. dst[1][x] = av_bswap16(*src_line++ >> shift);
  419. dst[2][x] = av_bswap16(*src_line++ >> shift);
  420. src_line++;
  421. }
  422. } else {
  423. for (x = 0; x < width; x++) {
  424. dst[0][x] = av_bswap16(*src_line++ >> shift);
  425. dst[1][x] = av_bswap16(*src_line++ >> shift);
  426. dst[2][x] = av_bswap16(*src_line++ >> shift);
  427. }
  428. }
  429. break;
  430. case 1:
  431. if (src_alpha && dst_alpha) {
  432. for (x = 0; x < width; x++) {
  433. dst[0][x] = av_bswap16(*src_line++) >> shift;
  434. dst[1][x] = av_bswap16(*src_line++) >> shift;
  435. dst[2][x] = av_bswap16(*src_line++) >> shift;
  436. dst[3][x] = av_bswap16(*src_line++) >> shift;
  437. }
  438. } else if (dst_alpha) {
  439. for (x = 0; x < width; x++) {
  440. dst[0][x] = av_bswap16(*src_line++) >> shift;
  441. dst[1][x] = av_bswap16(*src_line++) >> shift;
  442. dst[2][x] = av_bswap16(*src_line++) >> shift;
  443. dst[3][x] = 0xFFFF;
  444. }
  445. } else if (src_alpha) {
  446. for (x = 0; x < width; x++) {
  447. dst[0][x] = av_bswap16(*src_line++) >> shift;
  448. dst[1][x] = av_bswap16(*src_line++) >> shift;
  449. dst[2][x] = av_bswap16(*src_line++) >> shift;
  450. src_line++;
  451. }
  452. } else {
  453. for (x = 0; x < width; x++) {
  454. dst[0][x] = av_bswap16(*src_line++) >> shift;
  455. dst[1][x] = av_bswap16(*src_line++) >> shift;
  456. dst[2][x] = av_bswap16(*src_line++) >> shift;
  457. }
  458. }
  459. break;
  460. default:
  461. if (src_alpha && dst_alpha) {
  462. for (x = 0; x < width; x++) {
  463. dst[0][x] = *src_line++ >> shift;
  464. dst[1][x] = *src_line++ >> shift;
  465. dst[2][x] = *src_line++ >> shift;
  466. dst[3][x] = *src_line++ >> shift;
  467. }
  468. } else if (dst_alpha) {
  469. for (x = 0; x < width; x++) {
  470. dst[0][x] = *src_line++ >> shift;
  471. dst[1][x] = *src_line++ >> shift;
  472. dst[2][x] = *src_line++ >> shift;
  473. dst[3][x] = 0xFFFF;
  474. }
  475. } else if (src_alpha) {
  476. for (x = 0; x < width; x++) {
  477. dst[0][x] = *src_line++ >> shift;
  478. dst[1][x] = *src_line++ >> shift;
  479. dst[2][x] = *src_line++ >> shift;
  480. src_line++;
  481. }
  482. } else {
  483. for (x = 0; x < width; x++) {
  484. dst[0][x] = *src_line++ >> shift;
  485. dst[1][x] = *src_line++ >> shift;
  486. dst[2][x] = *src_line++ >> shift;
  487. }
  488. }
  489. }
  490. for (i = 0; i < 4; i++)
  491. dst[i] += dstStride[i] >> 1;
  492. }
  493. }
  494. static int Rgb16ToPlanarRgb16Wrapper(SwsContext *c, const uint8_t *src[],
  495. int srcStride[], int srcSliceY, int srcSliceH,
  496. uint8_t *dst[], int dstStride[])
  497. {
  498. uint16_t *dst2013[] = { (uint16_t *)dst[2], (uint16_t *)dst[0], (uint16_t *)dst[1], (uint16_t *)dst[3] };
  499. uint16_t *dst1023[] = { (uint16_t *)dst[1], (uint16_t *)dst[0], (uint16_t *)dst[2], (uint16_t *)dst[3] };
  500. int stride2013[] = { dstStride[2], dstStride[0], dstStride[1], dstStride[3] };
  501. int stride1023[] = { dstStride[1], dstStride[0], dstStride[2], dstStride[3] };
  502. const AVPixFmtDescriptor *src_format = av_pix_fmt_desc_get(c->srcFormat);
  503. const AVPixFmtDescriptor *dst_format = av_pix_fmt_desc_get(c->dstFormat);
  504. int bpc = dst_format->comp[0].depth;
  505. int alpha = src_format->flags & AV_PIX_FMT_FLAG_ALPHA;
  506. int swap = 0;
  507. int i;
  508. if ( HAVE_BIGENDIAN && !(src_format->flags & AV_PIX_FMT_FLAG_BE) ||
  509. !HAVE_BIGENDIAN && src_format->flags & AV_PIX_FMT_FLAG_BE)
  510. swap++;
  511. if ( HAVE_BIGENDIAN && !(dst_format->flags & AV_PIX_FMT_FLAG_BE) ||
  512. !HAVE_BIGENDIAN && dst_format->flags & AV_PIX_FMT_FLAG_BE)
  513. swap += 2;
  514. if ((dst_format->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) !=
  515. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB) || bpc < 9) {
  516. av_log(c, AV_LOG_ERROR, "unsupported conversion to planar RGB %s -> %s\n",
  517. src_format->name, dst_format->name);
  518. return srcSliceH;
  519. }
  520. for(i=0; i<4; i++) {
  521. dst2013[i] += stride2013[i] * srcSliceY / 2;
  522. dst1023[i] += stride1023[i] * srcSliceY / 2;
  523. }
  524. switch (c->srcFormat) {
  525. case AV_PIX_FMT_RGB48LE:
  526. case AV_PIX_FMT_RGB48BE:
  527. case AV_PIX_FMT_RGBA64LE:
  528. case AV_PIX_FMT_RGBA64BE:
  529. packed16togbra16(src[0] + srcSliceY * srcStride[0], srcStride[0],
  530. dst2013, stride2013, srcSliceH, alpha, swap,
  531. 16 - bpc, c->srcW);
  532. break;
  533. case AV_PIX_FMT_BGR48LE:
  534. case AV_PIX_FMT_BGR48BE:
  535. case AV_PIX_FMT_BGRA64LE:
  536. case AV_PIX_FMT_BGRA64BE:
  537. packed16togbra16(src[0] + srcSliceY * srcStride[0], srcStride[0],
  538. dst1023, stride1023, srcSliceH, alpha, swap,
  539. 16 - bpc, c->srcW);
  540. break;
  541. default:
  542. av_log(c, AV_LOG_ERROR,
  543. "unsupported conversion to planar RGB %s -> %s\n",
  544. src_format->name, dst_format->name);
  545. }
  546. return srcSliceH;
  547. }
  548. static void gbr16ptopacked16(const uint16_t *src[], int srcStride[],
  549. uint8_t *dst, int dstStride, int srcSliceH,
  550. int alpha, int swap, int bpp, int width)
  551. {
  552. int x, h, i;
  553. int src_alpha = src[3] != NULL;
  554. int scale_high = 16 - bpp, scale_low = (bpp - 8) * 2;
  555. for (h = 0; h < srcSliceH; h++) {
  556. uint16_t *dest = (uint16_t *)(dst + dstStride * h);
  557. uint16_t component;
  558. switch(swap) {
  559. case 3:
  560. if (alpha && !src_alpha) {
  561. for (x = 0; x < width; x++) {
  562. component = av_bswap16(src[0][x]);
  563. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  564. component = av_bswap16(src[1][x]);
  565. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  566. component = av_bswap16(src[2][x]);
  567. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  568. *dest++ = 0xffff;
  569. }
  570. } else if (alpha && src_alpha) {
  571. for (x = 0; x < width; x++) {
  572. component = av_bswap16(src[0][x]);
  573. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  574. component = av_bswap16(src[1][x]);
  575. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  576. component = av_bswap16(src[2][x]);
  577. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  578. component = av_bswap16(src[3][x]);
  579. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  580. }
  581. } else {
  582. for (x = 0; x < width; x++) {
  583. component = av_bswap16(src[0][x]);
  584. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  585. component = av_bswap16(src[1][x]);
  586. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  587. component = av_bswap16(src[2][x]);
  588. *dest++ = av_bswap16(component << scale_high | component >> scale_low);
  589. }
  590. }
  591. break;
  592. case 2:
  593. if (alpha && !src_alpha) {
  594. for (x = 0; x < width; x++) {
  595. *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
  596. *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
  597. *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
  598. *dest++ = 0xffff;
  599. }
  600. } else if (alpha && src_alpha) {
  601. for (x = 0; x < width; x++) {
  602. *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
  603. *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
  604. *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
  605. *dest++ = av_bswap16(src[3][x] << scale_high | src[3][x] >> scale_low);
  606. }
  607. } else {
  608. for (x = 0; x < width; x++) {
  609. *dest++ = av_bswap16(src[0][x] << scale_high | src[0][x] >> scale_low);
  610. *dest++ = av_bswap16(src[1][x] << scale_high | src[1][x] >> scale_low);
  611. *dest++ = av_bswap16(src[2][x] << scale_high | src[2][x] >> scale_low);
  612. }
  613. }
  614. break;
  615. case 1:
  616. if (alpha && !src_alpha) {
  617. for (x = 0; x < width; x++) {
  618. *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
  619. *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
  620. *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
  621. *dest++ = 0xffff;
  622. }
  623. } else if (alpha && src_alpha) {
  624. for (x = 0; x < width; x++) {
  625. *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
  626. *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
  627. *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
  628. *dest++ = av_bswap16(src[3][x]) << scale_high | av_bswap16(src[3][x]) >> scale_low;
  629. }
  630. } else {
  631. for (x = 0; x < width; x++) {
  632. *dest++ = av_bswap16(src[0][x]) << scale_high | av_bswap16(src[0][x]) >> scale_low;
  633. *dest++ = av_bswap16(src[1][x]) << scale_high | av_bswap16(src[1][x]) >> scale_low;
  634. *dest++ = av_bswap16(src[2][x]) << scale_high | av_bswap16(src[2][x]) >> scale_low;
  635. }
  636. }
  637. break;
  638. default:
  639. if (alpha && !src_alpha) {
  640. for (x = 0; x < width; x++) {
  641. *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
  642. *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
  643. *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
  644. *dest++ = 0xffff;
  645. }
  646. } else if (alpha && src_alpha) {
  647. for (x = 0; x < width; x++) {
  648. *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
  649. *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
  650. *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
  651. *dest++ = src[3][x] << scale_high | src[3][x] >> scale_low;
  652. }
  653. } else {
  654. for (x = 0; x < width; x++) {
  655. *dest++ = src[0][x] << scale_high | src[0][x] >> scale_low;
  656. *dest++ = src[1][x] << scale_high | src[1][x] >> scale_low;
  657. *dest++ = src[2][x] << scale_high | src[2][x] >> scale_low;
  658. }
  659. }
  660. }
  661. for (i = 0; i < 3 + src_alpha; i++)
  662. src[i] += srcStride[i] >> 1;
  663. }
  664. }
  665. static int planarRgb16ToRgb16Wrapper(SwsContext *c, const uint8_t *src[],
  666. int srcStride[], int srcSliceY, int srcSliceH,
  667. uint8_t *dst[], int dstStride[])
  668. {
  669. const uint16_t *src102[] = { (uint16_t *)src[1], (uint16_t *)src[0], (uint16_t *)src[2], (uint16_t *)src[3] };
  670. const uint16_t *src201[] = { (uint16_t *)src[2], (uint16_t *)src[0], (uint16_t *)src[1], (uint16_t *)src[3] };
  671. int stride102[] = { srcStride[1], srcStride[0], srcStride[2], srcStride[3] };
  672. int stride201[] = { srcStride[2], srcStride[0], srcStride[1], srcStride[3] };
  673. const AVPixFmtDescriptor *src_format = av_pix_fmt_desc_get(c->srcFormat);
  674. const AVPixFmtDescriptor *dst_format = av_pix_fmt_desc_get(c->dstFormat);
  675. int bits_per_sample = src_format->comp[0].depth;
  676. int swap = 0;
  677. if ( HAVE_BIGENDIAN && !(src_format->flags & AV_PIX_FMT_FLAG_BE) ||
  678. !HAVE_BIGENDIAN && src_format->flags & AV_PIX_FMT_FLAG_BE)
  679. swap++;
  680. if ( HAVE_BIGENDIAN && !(dst_format->flags & AV_PIX_FMT_FLAG_BE) ||
  681. !HAVE_BIGENDIAN && dst_format->flags & AV_PIX_FMT_FLAG_BE)
  682. swap += 2;
  683. if ((src_format->flags & (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB)) !=
  684. (AV_PIX_FMT_FLAG_PLANAR | AV_PIX_FMT_FLAG_RGB) ||
  685. bits_per_sample <= 8) {
  686. av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
  687. src_format->name, dst_format->name);
  688. return srcSliceH;
  689. }
  690. switch (c->dstFormat) {
  691. case AV_PIX_FMT_BGR48LE:
  692. case AV_PIX_FMT_BGR48BE:
  693. gbr16ptopacked16(src102, stride102,
  694. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  695. srcSliceH, 0, swap, bits_per_sample, c->srcW);
  696. break;
  697. case AV_PIX_FMT_RGB48LE:
  698. case AV_PIX_FMT_RGB48BE:
  699. gbr16ptopacked16(src201, stride201,
  700. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  701. srcSliceH, 0, swap, bits_per_sample, c->srcW);
  702. break;
  703. case AV_PIX_FMT_RGBA64LE:
  704. case AV_PIX_FMT_RGBA64BE:
  705. gbr16ptopacked16(src201, stride201,
  706. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  707. srcSliceH, 1, swap, bits_per_sample, c->srcW);
  708. break;
  709. case AV_PIX_FMT_BGRA64LE:
  710. case AV_PIX_FMT_BGRA64BE:
  711. gbr16ptopacked16(src102, stride102,
  712. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  713. srcSliceH, 1, swap, bits_per_sample, c->srcW);
  714. break;
  715. default:
  716. av_log(c, AV_LOG_ERROR,
  717. "unsupported planar RGB conversion %s -> %s\n",
  718. src_format->name, dst_format->name);
  719. }
  720. return srcSliceH;
  721. }
  722. static void gbr24ptopacked24(const uint8_t *src[], int srcStride[],
  723. uint8_t *dst, int dstStride, int srcSliceH,
  724. int width)
  725. {
  726. int x, h, i;
  727. for (h = 0; h < srcSliceH; h++) {
  728. uint8_t *dest = dst + dstStride * h;
  729. for (x = 0; x < width; x++) {
  730. *dest++ = src[0][x];
  731. *dest++ = src[1][x];
  732. *dest++ = src[2][x];
  733. }
  734. for (i = 0; i < 3; i++)
  735. src[i] += srcStride[i];
  736. }
  737. }
  738. static void gbr24ptopacked32(const uint8_t *src[], int srcStride[],
  739. uint8_t *dst, int dstStride, int srcSliceH,
  740. int alpha_first, int width)
  741. {
  742. int x, h, i;
  743. for (h = 0; h < srcSliceH; h++) {
  744. uint8_t *dest = dst + dstStride * h;
  745. if (alpha_first) {
  746. for (x = 0; x < width; x++) {
  747. *dest++ = 0xff;
  748. *dest++ = src[0][x];
  749. *dest++ = src[1][x];
  750. *dest++ = src[2][x];
  751. }
  752. } else {
  753. for (x = 0; x < width; x++) {
  754. *dest++ = src[0][x];
  755. *dest++ = src[1][x];
  756. *dest++ = src[2][x];
  757. *dest++ = 0xff;
  758. }
  759. }
  760. for (i = 0; i < 3; i++)
  761. src[i] += srcStride[i];
  762. }
  763. }
  764. static int planarRgbToRgbWrapper(SwsContext *c, const uint8_t *src[],
  765. int srcStride[], int srcSliceY, int srcSliceH,
  766. uint8_t *dst[], int dstStride[])
  767. {
  768. int alpha_first = 0;
  769. const uint8_t *src102[] = { src[1], src[0], src[2] };
  770. const uint8_t *src201[] = { src[2], src[0], src[1] };
  771. int stride102[] = { srcStride[1], srcStride[0], srcStride[2] };
  772. int stride201[] = { srcStride[2], srcStride[0], srcStride[1] };
  773. if (c->srcFormat != AV_PIX_FMT_GBRP) {
  774. av_log(c, AV_LOG_ERROR, "unsupported planar RGB conversion %s -> %s\n",
  775. av_get_pix_fmt_name(c->srcFormat),
  776. av_get_pix_fmt_name(c->dstFormat));
  777. return srcSliceH;
  778. }
  779. switch (c->dstFormat) {
  780. case AV_PIX_FMT_BGR24:
  781. gbr24ptopacked24(src102, stride102,
  782. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  783. srcSliceH, c->srcW);
  784. break;
  785. case AV_PIX_FMT_RGB24:
  786. gbr24ptopacked24(src201, stride201,
  787. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  788. srcSliceH, c->srcW);
  789. break;
  790. case AV_PIX_FMT_ARGB:
  791. alpha_first = 1;
  792. case AV_PIX_FMT_RGBA:
  793. gbr24ptopacked32(src201, stride201,
  794. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  795. srcSliceH, alpha_first, c->srcW);
  796. break;
  797. case AV_PIX_FMT_ABGR:
  798. alpha_first = 1;
  799. case AV_PIX_FMT_BGRA:
  800. gbr24ptopacked32(src102, stride102,
  801. dst[0] + srcSliceY * dstStride[0], dstStride[0],
  802. srcSliceH, alpha_first, c->srcW);
  803. break;
  804. default:
  805. av_log(c, AV_LOG_ERROR,
  806. "unsupported planar RGB conversion %s -> %s\n",
  807. av_get_pix_fmt_name(c->srcFormat),
  808. av_get_pix_fmt_name(c->dstFormat));
  809. }
  810. return srcSliceH;
  811. }
  812. static int planarRgbToplanarRgbWrapper(SwsContext *c,
  813. const uint8_t *src[], int srcStride[],
  814. int srcSliceY, int srcSliceH,
  815. uint8_t *dst[], int dstStride[])
  816. {
  817. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  818. dst[0], dstStride[0]);
  819. copyPlane(src[1], srcStride[1], srcSliceY, srcSliceH, c->srcW,
  820. dst[1], dstStride[1]);
  821. copyPlane(src[2], srcStride[2], srcSliceY, srcSliceH, c->srcW,
  822. dst[2], dstStride[2]);
  823. if (dst[3])
  824. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  825. return srcSliceH;
  826. }
  827. static void packedtogbr24p(const uint8_t *src, int srcStride,
  828. uint8_t *dst[], int dstStride[], int srcSliceH,
  829. int alpha_first, int inc_size, int width)
  830. {
  831. uint8_t *dest[3];
  832. int x, h;
  833. dest[0] = dst[0];
  834. dest[1] = dst[1];
  835. dest[2] = dst[2];
  836. if (alpha_first)
  837. src++;
  838. for (h = 0; h < srcSliceH; h++) {
  839. for (x = 0; x < width; x++) {
  840. dest[0][x] = src[0];
  841. dest[1][x] = src[1];
  842. dest[2][x] = src[2];
  843. src += inc_size;
  844. }
  845. src += srcStride - width * inc_size;
  846. dest[0] += dstStride[0];
  847. dest[1] += dstStride[1];
  848. dest[2] += dstStride[2];
  849. }
  850. }
  851. static int rgbToPlanarRgbWrapper(SwsContext *c, const uint8_t *src[],
  852. int srcStride[], int srcSliceY, int srcSliceH,
  853. uint8_t *dst[], int dstStride[])
  854. {
  855. int alpha_first = 0;
  856. int stride102[] = { dstStride[1], dstStride[0], dstStride[2] };
  857. int stride201[] = { dstStride[2], dstStride[0], dstStride[1] };
  858. uint8_t *dst102[] = { dst[1] + srcSliceY * dstStride[1],
  859. dst[0] + srcSliceY * dstStride[0],
  860. dst[2] + srcSliceY * dstStride[2] };
  861. uint8_t *dst201[] = { dst[2] + srcSliceY * dstStride[2],
  862. dst[0] + srcSliceY * dstStride[0],
  863. dst[1] + srcSliceY * dstStride[1] };
  864. switch (c->srcFormat) {
  865. case AV_PIX_FMT_RGB24:
  866. packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
  867. stride201, srcSliceH, alpha_first, 3, c->srcW);
  868. break;
  869. case AV_PIX_FMT_BGR24:
  870. packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
  871. stride102, srcSliceH, alpha_first, 3, c->srcW);
  872. break;
  873. case AV_PIX_FMT_ARGB:
  874. alpha_first = 1;
  875. case AV_PIX_FMT_RGBA:
  876. packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst201,
  877. stride201, srcSliceH, alpha_first, 4, c->srcW);
  878. break;
  879. case AV_PIX_FMT_ABGR:
  880. alpha_first = 1;
  881. case AV_PIX_FMT_BGRA:
  882. packedtogbr24p((const uint8_t *) src[0], srcStride[0], dst102,
  883. stride102, srcSliceH, alpha_first, 4, c->srcW);
  884. break;
  885. default:
  886. av_log(c, AV_LOG_ERROR,
  887. "unsupported planar RGB conversion %s -> %s\n",
  888. av_get_pix_fmt_name(c->srcFormat),
  889. av_get_pix_fmt_name(c->dstFormat));
  890. }
  891. return srcSliceH;
  892. }
  893. #define BAYER_GBRG
  894. #define BAYER_8
  895. #define BAYER_RENAME(x) bayer_gbrg8_to_##x
  896. #include "bayer_template.c"
  897. #define BAYER_GBRG
  898. #define BAYER_16LE
  899. #define BAYER_RENAME(x) bayer_gbrg16le_to_##x
  900. #include "bayer_template.c"
  901. #define BAYER_GBRG
  902. #define BAYER_16BE
  903. #define BAYER_RENAME(x) bayer_gbrg16be_to_##x
  904. #include "bayer_template.c"
  905. #define BAYER_GRBG
  906. #define BAYER_8
  907. #define BAYER_RENAME(x) bayer_grbg8_to_##x
  908. #include "bayer_template.c"
  909. #define BAYER_GRBG
  910. #define BAYER_16LE
  911. #define BAYER_RENAME(x) bayer_grbg16le_to_##x
  912. #include "bayer_template.c"
  913. #define BAYER_GRBG
  914. #define BAYER_16BE
  915. #define BAYER_RENAME(x) bayer_grbg16be_to_##x
  916. #include "bayer_template.c"
  917. #define BAYER_BGGR
  918. #define BAYER_8
  919. #define BAYER_RENAME(x) bayer_bggr8_to_##x
  920. #include "bayer_template.c"
  921. #define BAYER_BGGR
  922. #define BAYER_16LE
  923. #define BAYER_RENAME(x) bayer_bggr16le_to_##x
  924. #include "bayer_template.c"
  925. #define BAYER_BGGR
  926. #define BAYER_16BE
  927. #define BAYER_RENAME(x) bayer_bggr16be_to_##x
  928. #include "bayer_template.c"
  929. #define BAYER_RGGB
  930. #define BAYER_8
  931. #define BAYER_RENAME(x) bayer_rggb8_to_##x
  932. #include "bayer_template.c"
  933. #define BAYER_RGGB
  934. #define BAYER_16LE
  935. #define BAYER_RENAME(x) bayer_rggb16le_to_##x
  936. #include "bayer_template.c"
  937. #define BAYER_RGGB
  938. #define BAYER_16BE
  939. #define BAYER_RENAME(x) bayer_rggb16be_to_##x
  940. #include "bayer_template.c"
  941. static int bayer_to_rgb24_wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  942. int srcSliceH, uint8_t* dst[], int dstStride[])
  943. {
  944. uint8_t *dstPtr= dst[0];
  945. const uint8_t *srcPtr= src[0];
  946. int i;
  947. void (*copy) (const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width);
  948. void (*interpolate)(const uint8_t *src, int src_stride, uint8_t *dst, int dst_stride, int width);
  949. switch(c->srcFormat) {
  950. #define CASE(pixfmt, prefix) \
  951. case pixfmt: copy = bayer_##prefix##_to_rgb24_copy; \
  952. interpolate = bayer_##prefix##_to_rgb24_interpolate; \
  953. break;
  954. CASE(AV_PIX_FMT_BAYER_BGGR8, bggr8)
  955. CASE(AV_PIX_FMT_BAYER_BGGR16LE, bggr16le)
  956. CASE(AV_PIX_FMT_BAYER_BGGR16BE, bggr16be)
  957. CASE(AV_PIX_FMT_BAYER_RGGB8, rggb8)
  958. CASE(AV_PIX_FMT_BAYER_RGGB16LE, rggb16le)
  959. CASE(AV_PIX_FMT_BAYER_RGGB16BE, rggb16be)
  960. CASE(AV_PIX_FMT_BAYER_GBRG8, gbrg8)
  961. CASE(AV_PIX_FMT_BAYER_GBRG16LE, gbrg16le)
  962. CASE(AV_PIX_FMT_BAYER_GBRG16BE, gbrg16be)
  963. CASE(AV_PIX_FMT_BAYER_GRBG8, grbg8)
  964. CASE(AV_PIX_FMT_BAYER_GRBG16LE, grbg16le)
  965. CASE(AV_PIX_FMT_BAYER_GRBG16BE, grbg16be)
  966. #undef CASE
  967. default: return 0;
  968. }
  969. av_assert0(srcSliceH > 1);
  970. copy(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW);
  971. srcPtr += 2 * srcStride[0];
  972. dstPtr += 2 * dstStride[0];
  973. for (i = 2; i < srcSliceH - 2; i += 2) {
  974. interpolate(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW);
  975. srcPtr += 2 * srcStride[0];
  976. dstPtr += 2 * dstStride[0];
  977. }
  978. if (i + 1 == srcSliceH) {
  979. copy(srcPtr, -srcStride[0], dstPtr, -dstStride[0], c->srcW);
  980. } else if (i < srcSliceH)
  981. copy(srcPtr, srcStride[0], dstPtr, dstStride[0], c->srcW);
  982. return srcSliceH;
  983. }
  984. static int bayer_to_yv12_wrapper(SwsContext *c, const uint8_t* src[], int srcStride[], int srcSliceY,
  985. int srcSliceH, uint8_t* dst[], int dstStride[])
  986. {
  987. const uint8_t *srcPtr= src[0];
  988. uint8_t *dstY= dst[0];
  989. uint8_t *dstU= dst[1];
  990. uint8_t *dstV= dst[2];
  991. int i;
  992. void (*copy) (const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv);
  993. void (*interpolate)(const uint8_t *src, int src_stride, uint8_t *dstY, uint8_t *dstU, uint8_t *dstV, int luma_stride, int width, int32_t *rgb2yuv);
  994. switch(c->srcFormat) {
  995. #define CASE(pixfmt, prefix) \
  996. case pixfmt: copy = bayer_##prefix##_to_yv12_copy; \
  997. interpolate = bayer_##prefix##_to_yv12_interpolate; \
  998. break;
  999. CASE(AV_PIX_FMT_BAYER_BGGR8, bggr8)
  1000. CASE(AV_PIX_FMT_BAYER_BGGR16LE, bggr16le)
  1001. CASE(AV_PIX_FMT_BAYER_BGGR16BE, bggr16be)
  1002. CASE(AV_PIX_FMT_BAYER_RGGB8, rggb8)
  1003. CASE(AV_PIX_FMT_BAYER_RGGB16LE, rggb16le)
  1004. CASE(AV_PIX_FMT_BAYER_RGGB16BE, rggb16be)
  1005. CASE(AV_PIX_FMT_BAYER_GBRG8, gbrg8)
  1006. CASE(AV_PIX_FMT_BAYER_GBRG16LE, gbrg16le)
  1007. CASE(AV_PIX_FMT_BAYER_GBRG16BE, gbrg16be)
  1008. CASE(AV_PIX_FMT_BAYER_GRBG8, grbg8)
  1009. CASE(AV_PIX_FMT_BAYER_GRBG16LE, grbg16le)
  1010. CASE(AV_PIX_FMT_BAYER_GRBG16BE, grbg16be)
  1011. #undef CASE
  1012. default: return 0;
  1013. }
  1014. av_assert0(srcSliceH > 1);
  1015. copy(srcPtr, srcStride[0], dstY, dstU, dstV, dstStride[0], c->srcW, c->input_rgb2yuv_table);
  1016. srcPtr += 2 * srcStride[0];
  1017. dstY += 2 * dstStride[0];
  1018. dstU += dstStride[1];
  1019. dstV += dstStride[1];
  1020. for (i = 2; i < srcSliceH - 2; i += 2) {
  1021. interpolate(srcPtr, srcStride[0], dstY, dstU, dstV, dstStride[0], c->srcW, c->input_rgb2yuv_table);
  1022. srcPtr += 2 * srcStride[0];
  1023. dstY += 2 * dstStride[0];
  1024. dstU += dstStride[1];
  1025. dstV += dstStride[1];
  1026. }
  1027. if (i + 1 == srcSliceH) {
  1028. copy(srcPtr, -srcStride[0], dstY, dstU, dstV, -dstStride[0], c->srcW, c->input_rgb2yuv_table);
  1029. } else if (i < srcSliceH)
  1030. copy(srcPtr, srcStride[0], dstY, dstU, dstV, dstStride[0], c->srcW, c->input_rgb2yuv_table);
  1031. return srcSliceH;
  1032. }
  1033. #define isRGBA32(x) ( \
  1034. (x) == AV_PIX_FMT_ARGB \
  1035. || (x) == AV_PIX_FMT_RGBA \
  1036. || (x) == AV_PIX_FMT_BGRA \
  1037. || (x) == AV_PIX_FMT_ABGR \
  1038. )
  1039. #define isRGBA64(x) ( \
  1040. (x) == AV_PIX_FMT_RGBA64LE \
  1041. || (x) == AV_PIX_FMT_RGBA64BE \
  1042. || (x) == AV_PIX_FMT_BGRA64LE \
  1043. || (x) == AV_PIX_FMT_BGRA64BE \
  1044. )
  1045. #define isRGB48(x) ( \
  1046. (x) == AV_PIX_FMT_RGB48LE \
  1047. || (x) == AV_PIX_FMT_RGB48BE \
  1048. || (x) == AV_PIX_FMT_BGR48LE \
  1049. || (x) == AV_PIX_FMT_BGR48BE \
  1050. )
  1051. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1052. typedef void (* rgbConvFn) (const uint8_t *, uint8_t *, int);
  1053. static rgbConvFn findRgbConvFn(SwsContext *c)
  1054. {
  1055. const enum AVPixelFormat srcFormat = c->srcFormat;
  1056. const enum AVPixelFormat dstFormat = c->dstFormat;
  1057. const int srcId = c->srcFormatBpp;
  1058. const int dstId = c->dstFormatBpp;
  1059. rgbConvFn conv = NULL;
  1060. #define IS_NOT_NE(bpp, desc) \
  1061. (((bpp + 7) >> 3) == 2 && \
  1062. (!(desc->flags & AV_PIX_FMT_FLAG_BE) != !HAVE_BIGENDIAN))
  1063. #define CONV_IS(src, dst) (srcFormat == AV_PIX_FMT_##src && dstFormat == AV_PIX_FMT_##dst)
  1064. if (isRGBA32(srcFormat) && isRGBA32(dstFormat)) {
  1065. if ( CONV_IS(ABGR, RGBA)
  1066. || CONV_IS(ARGB, BGRA)
  1067. || CONV_IS(BGRA, ARGB)
  1068. || CONV_IS(RGBA, ABGR)) conv = shuffle_bytes_3210;
  1069. else if (CONV_IS(ABGR, ARGB)
  1070. || CONV_IS(ARGB, ABGR)) conv = shuffle_bytes_0321;
  1071. else if (CONV_IS(ABGR, BGRA)
  1072. || CONV_IS(ARGB, RGBA)) conv = shuffle_bytes_1230;
  1073. else if (CONV_IS(BGRA, RGBA)
  1074. || CONV_IS(RGBA, BGRA)) conv = shuffle_bytes_2103;
  1075. else if (CONV_IS(BGRA, ABGR)
  1076. || CONV_IS(RGBA, ARGB)) conv = shuffle_bytes_3012;
  1077. } else if (isRGB48(srcFormat) && isRGB48(dstFormat)) {
  1078. if (CONV_IS(RGB48LE, BGR48LE)
  1079. || CONV_IS(BGR48LE, RGB48LE)
  1080. || CONV_IS(RGB48BE, BGR48BE)
  1081. || CONV_IS(BGR48BE, RGB48BE)) conv = rgb48tobgr48_nobswap;
  1082. else if (CONV_IS(RGB48LE, BGR48BE)
  1083. || CONV_IS(BGR48LE, RGB48BE)
  1084. || CONV_IS(RGB48BE, BGR48LE)
  1085. || CONV_IS(BGR48BE, RGB48LE)) conv = rgb48tobgr48_bswap;
  1086. } else if (isRGBA64(srcFormat) && isRGB48(dstFormat)) {
  1087. if (CONV_IS(RGBA64LE, BGR48LE)
  1088. || CONV_IS(BGRA64LE, RGB48LE)
  1089. || CONV_IS(RGBA64BE, BGR48BE)
  1090. || CONV_IS(BGRA64BE, RGB48BE)) conv = rgb64tobgr48_nobswap;
  1091. else if (CONV_IS(RGBA64LE, BGR48BE)
  1092. || CONV_IS(BGRA64LE, RGB48BE)
  1093. || CONV_IS(RGBA64BE, BGR48LE)
  1094. || CONV_IS(BGRA64BE, RGB48LE)) conv = rgb64tobgr48_bswap;
  1095. else if (CONV_IS(RGBA64LE, RGB48LE)
  1096. || CONV_IS(BGRA64LE, BGR48LE)
  1097. || CONV_IS(RGBA64BE, RGB48BE)
  1098. || CONV_IS(BGRA64BE, BGR48BE)) conv = rgb64to48_nobswap;
  1099. else if (CONV_IS(RGBA64LE, RGB48BE)
  1100. || CONV_IS(BGRA64LE, BGR48BE)
  1101. || CONV_IS(RGBA64BE, RGB48LE)
  1102. || CONV_IS(BGRA64BE, BGR48LE)) conv = rgb64to48_bswap;
  1103. } else
  1104. /* BGR -> BGR */
  1105. if ((isBGRinInt(srcFormat) && isBGRinInt(dstFormat)) ||
  1106. (isRGBinInt(srcFormat) && isRGBinInt(dstFormat))) {
  1107. switch (srcId | (dstId << 16)) {
  1108. case 0x000F000C: conv = rgb12to15; break;
  1109. case 0x000F0010: conv = rgb16to15; break;
  1110. case 0x000F0018: conv = rgb24to15; break;
  1111. case 0x000F0020: conv = rgb32to15; break;
  1112. case 0x0010000F: conv = rgb15to16; break;
  1113. case 0x00100018: conv = rgb24to16; break;
  1114. case 0x00100020: conv = rgb32to16; break;
  1115. case 0x0018000F: conv = rgb15to24; break;
  1116. case 0x00180010: conv = rgb16to24; break;
  1117. case 0x00180020: conv = rgb32to24; break;
  1118. case 0x0020000F: conv = rgb15to32; break;
  1119. case 0x00200010: conv = rgb16to32; break;
  1120. case 0x00200018: conv = rgb24to32; break;
  1121. }
  1122. } else if ((isBGRinInt(srcFormat) && isRGBinInt(dstFormat)) ||
  1123. (isRGBinInt(srcFormat) && isBGRinInt(dstFormat))) {
  1124. switch (srcId | (dstId << 16)) {
  1125. case 0x000C000C: conv = rgb12tobgr12; break;
  1126. case 0x000F000F: conv = rgb15tobgr15; break;
  1127. case 0x000F0010: conv = rgb16tobgr15; break;
  1128. case 0x000F0018: conv = rgb24tobgr15; break;
  1129. case 0x000F0020: conv = rgb32tobgr15; break;
  1130. case 0x0010000F: conv = rgb15tobgr16; break;
  1131. case 0x00100010: conv = rgb16tobgr16; break;
  1132. case 0x00100018: conv = rgb24tobgr16; break;
  1133. case 0x00100020: conv = rgb32tobgr16; break;
  1134. case 0x0018000F: conv = rgb15tobgr24; break;
  1135. case 0x00180010: conv = rgb16tobgr24; break;
  1136. case 0x00180018: conv = rgb24tobgr24; break;
  1137. case 0x00180020: conv = rgb32tobgr24; break;
  1138. case 0x0020000F: conv = rgb15tobgr32; break;
  1139. case 0x00200010: conv = rgb16tobgr32; break;
  1140. case 0x00200018: conv = rgb24tobgr32; break;
  1141. }
  1142. }
  1143. if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) && !isRGBA32(srcFormat) && ALT32_CORR<0)
  1144. return NULL;
  1145. // Maintain symmetry between endianness
  1146. if (c->flags & SWS_BITEXACT)
  1147. if ((dstFormat == AV_PIX_FMT_RGB32 || dstFormat == AV_PIX_FMT_BGR32 ) && !isRGBA32(srcFormat) && ALT32_CORR>0)
  1148. return NULL;
  1149. return conv;
  1150. }
  1151. /* {RGB,BGR}{15,16,24,32,32_1} -> {RGB,BGR}{15,16,24,32} */
  1152. static int rgbToRgbWrapper(SwsContext *c, const uint8_t *src[], int srcStride[],
  1153. int srcSliceY, int srcSliceH, uint8_t *dst[],
  1154. int dstStride[])
  1155. {
  1156. const enum AVPixelFormat srcFormat = c->srcFormat;
  1157. const enum AVPixelFormat dstFormat = c->dstFormat;
  1158. const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
  1159. const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
  1160. const int srcBpp = (c->srcFormatBpp + 7) >> 3;
  1161. const int dstBpp = (c->dstFormatBpp + 7) >> 3;
  1162. rgbConvFn conv = findRgbConvFn(c);
  1163. if (!conv) {
  1164. av_log(c, AV_LOG_ERROR, "internal error %s -> %s converter\n",
  1165. av_get_pix_fmt_name(srcFormat), av_get_pix_fmt_name(dstFormat));
  1166. } else {
  1167. const uint8_t *srcPtr = src[0];
  1168. uint8_t *dstPtr = dst[0];
  1169. int src_bswap = IS_NOT_NE(c->srcFormatBpp, desc_src);
  1170. int dst_bswap = IS_NOT_NE(c->dstFormatBpp, desc_dst);
  1171. if ((srcFormat == AV_PIX_FMT_RGB32_1 || srcFormat == AV_PIX_FMT_BGR32_1) &&
  1172. !isRGBA32(dstFormat))
  1173. srcPtr += ALT32_CORR;
  1174. if ((dstFormat == AV_PIX_FMT_RGB32_1 || dstFormat == AV_PIX_FMT_BGR32_1) &&
  1175. !isRGBA32(srcFormat)) {
  1176. int i;
  1177. av_assert0(ALT32_CORR == 1);
  1178. for (i = 0; i < srcSliceH; i++)
  1179. dstPtr[dstStride[0] * (srcSliceY + i)] = 255;
  1180. dstPtr += ALT32_CORR;
  1181. }
  1182. if (dstStride[0] * srcBpp == srcStride[0] * dstBpp && srcStride[0] > 0 &&
  1183. !(srcStride[0] % srcBpp) && !dst_bswap && !src_bswap)
  1184. conv(srcPtr, dstPtr + dstStride[0] * srcSliceY,
  1185. (srcSliceH - 1) * srcStride[0] + c->srcW * srcBpp);
  1186. else {
  1187. int i, j;
  1188. dstPtr += dstStride[0] * srcSliceY;
  1189. for (i = 0; i < srcSliceH; i++) {
  1190. if(src_bswap) {
  1191. for(j=0; j<c->srcW; j++)
  1192. ((uint16_t*)c->formatConvBuffer)[j] = av_bswap16(((uint16_t*)srcPtr)[j]);
  1193. conv(c->formatConvBuffer, dstPtr, c->srcW * srcBpp);
  1194. }else
  1195. conv(srcPtr, dstPtr, c->srcW * srcBpp);
  1196. if(dst_bswap)
  1197. for(j=0; j<c->srcW; j++)
  1198. ((uint16_t*)dstPtr)[j] = av_bswap16(((uint16_t*)dstPtr)[j]);
  1199. srcPtr += srcStride[0];
  1200. dstPtr += dstStride[0];
  1201. }
  1202. }
  1203. }
  1204. return srcSliceH;
  1205. }
  1206. static int bgr24ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
  1207. int srcStride[], int srcSliceY, int srcSliceH,
  1208. uint8_t *dst[], int dstStride[])
  1209. {
  1210. ff_rgb24toyv12(
  1211. src[0],
  1212. dst[0] + srcSliceY * dstStride[0],
  1213. dst[1] + (srcSliceY >> 1) * dstStride[1],
  1214. dst[2] + (srcSliceY >> 1) * dstStride[2],
  1215. c->srcW, srcSliceH,
  1216. dstStride[0], dstStride[1], srcStride[0],
  1217. c->input_rgb2yuv_table);
  1218. if (dst[3])
  1219. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1220. return srcSliceH;
  1221. }
  1222. static int yvu9ToYv12Wrapper(SwsContext *c, const uint8_t *src[],
  1223. int srcStride[], int srcSliceY, int srcSliceH,
  1224. uint8_t *dst[], int dstStride[])
  1225. {
  1226. copyPlane(src[0], srcStride[0], srcSliceY, srcSliceH, c->srcW,
  1227. dst[0], dstStride[0]);
  1228. planar2x(src[1], dst[1] + dstStride[1] * (srcSliceY >> 1), c->chrSrcW,
  1229. srcSliceH >> 2, srcStride[1], dstStride[1]);
  1230. planar2x(src[2], dst[2] + dstStride[2] * (srcSliceY >> 1), c->chrSrcW,
  1231. srcSliceH >> 2, srcStride[2], dstStride[2]);
  1232. if (dst[3])
  1233. fillPlane(dst[3], dstStride[3], c->srcW, srcSliceH, srcSliceY, 255);
  1234. return srcSliceH;
  1235. }
  1236. /* unscaled copy like stuff (assumes nearly identical formats) */
  1237. static int packedCopyWrapper(SwsContext *c, const uint8_t *src[],
  1238. int srcStride[], int srcSliceY, int srcSliceH,
  1239. uint8_t *dst[], int dstStride[])
  1240. {
  1241. if (dstStride[0] == srcStride[0] && srcStride[0] > 0)
  1242. memcpy(dst[0] + dstStride[0] * srcSliceY, src[0], srcSliceH * dstStride[0]);
  1243. else {
  1244. int i;
  1245. const uint8_t *srcPtr = src[0];
  1246. uint8_t *dstPtr = dst[0] + dstStride[0] * srcSliceY;
  1247. int length = 0;
  1248. /* universal length finder */
  1249. while (length + c->srcW <= FFABS(dstStride[0]) &&
  1250. length + c->srcW <= FFABS(srcStride[0]))
  1251. length += c->srcW;
  1252. av_assert1(length != 0);
  1253. for (i = 0; i < srcSliceH; i++) {
  1254. memcpy(dstPtr, srcPtr, length);
  1255. srcPtr += srcStride[0];
  1256. dstPtr += dstStride[0];
  1257. }
  1258. }
  1259. return srcSliceH;
  1260. }
  1261. #define DITHER_COPY(dst, dstStride, src, srcStride, bswap, dbswap)\
  1262. uint16_t scale= dither_scale[dst_depth-1][src_depth-1];\
  1263. int shift= src_depth-dst_depth + dither_scale[src_depth-2][dst_depth-1];\
  1264. for (i = 0; i < height; i++) {\
  1265. const uint8_t *dither= dithers[src_depth-9][i&7];\
  1266. for (j = 0; j < length-7; j+=8){\
  1267. dst[j+0] = dbswap((bswap(src[j+0]) + dither[0])*scale>>shift);\
  1268. dst[j+1] = dbswap((bswap(src[j+1]) + dither[1])*scale>>shift);\
  1269. dst[j+2] = dbswap((bswap(src[j+2]) + dither[2])*scale>>shift);\
  1270. dst[j+3] = dbswap((bswap(src[j+3]) + dither[3])*scale>>shift);\
  1271. dst[j+4] = dbswap((bswap(src[j+4]) + dither[4])*scale>>shift);\
  1272. dst[j+5] = dbswap((bswap(src[j+5]) + dither[5])*scale>>shift);\
  1273. dst[j+6] = dbswap((bswap(src[j+6]) + dither[6])*scale>>shift);\
  1274. dst[j+7] = dbswap((bswap(src[j+7]) + dither[7])*scale>>shift);\
  1275. }\
  1276. for (; j < length; j++)\
  1277. dst[j] = dbswap((bswap(src[j]) + dither[j&7])*scale>>shift);\
  1278. dst += dstStride;\
  1279. src += srcStride;\
  1280. }
  1281. static int planarCopyWrapper(SwsContext *c, const uint8_t *src[],
  1282. int srcStride[], int srcSliceY, int srcSliceH,
  1283. uint8_t *dst[], int dstStride[])
  1284. {
  1285. const AVPixFmtDescriptor *desc_src = av_pix_fmt_desc_get(c->srcFormat);
  1286. const AVPixFmtDescriptor *desc_dst = av_pix_fmt_desc_get(c->dstFormat);
  1287. int plane, i, j;
  1288. for (plane = 0; plane < 4; plane++) {
  1289. int length = (plane == 0 || plane == 3) ? c->srcW : AV_CEIL_RSHIFT(c->srcW, c->chrDstHSubSample);
  1290. int y = (plane == 0 || plane == 3) ? srcSliceY: AV_CEIL_RSHIFT(srcSliceY, c->chrDstVSubSample);
  1291. int height = (plane == 0 || plane == 3) ? srcSliceH: AV_CEIL_RSHIFT(srcSliceH, c->chrDstVSubSample);
  1292. const uint8_t *srcPtr = src[plane];
  1293. uint8_t *dstPtr = dst[plane] + dstStride[plane] * y;
  1294. int shiftonly = plane == 1 || plane == 2 || (!c->srcRange && plane == 0);
  1295. if (!dst[plane])
  1296. continue;
  1297. // ignore palette for GRAY8
  1298. if (plane == 1 && !dst[2]) continue;
  1299. if (!src[plane] || (plane == 1 && !src[2])) {
  1300. if (is16BPS(c->dstFormat) || isNBPS(c->dstFormat)) {
  1301. fillPlane16(dst[plane], dstStride[plane], length, height, y,
  1302. plane == 3, desc_dst->comp[plane].depth,
  1303. isBE(c->dstFormat));
  1304. } else {
  1305. fillPlane(dst[plane], dstStride[plane], length, height, y,
  1306. (plane == 3) ? 255 : 128);
  1307. }
  1308. } else {
  1309. if(isNBPS(c->srcFormat) || isNBPS(c->dstFormat)
  1310. || (is16BPS(c->srcFormat) != is16BPS(c->dstFormat))
  1311. ) {
  1312. const int src_depth = desc_src->comp[plane].depth;
  1313. const int dst_depth = desc_dst->comp[plane].depth;
  1314. const uint16_t *srcPtr2 = (const uint16_t *) srcPtr;
  1315. uint16_t *dstPtr2 = (uint16_t*)dstPtr;
  1316. if (dst_depth == 8) {
  1317. if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
  1318. DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, , )
  1319. } else {
  1320. DITHER_COPY(dstPtr, dstStride[plane], srcPtr2, srcStride[plane]/2, av_bswap16, )
  1321. }
  1322. } else if (src_depth == 8) {
  1323. for (i = 0; i < height; i++) {
  1324. #define COPY816(w)\
  1325. if (shiftonly) {\
  1326. for (j = 0; j < length; j++)\
  1327. w(&dstPtr2[j], srcPtr[j]<<(dst_depth-8));\
  1328. } else {\
  1329. for (j = 0; j < length; j++)\
  1330. w(&dstPtr2[j], (srcPtr[j]<<(dst_depth-8)) |\
  1331. (srcPtr[j]>>(2*8-dst_depth)));\
  1332. }
  1333. if(isBE(c->dstFormat)){
  1334. COPY816(AV_WB16)
  1335. } else {
  1336. COPY816(AV_WL16)
  1337. }
  1338. dstPtr2 += dstStride[plane]/2;
  1339. srcPtr += srcStride[plane];
  1340. }
  1341. } else if (src_depth <= dst_depth) {
  1342. for (i = 0; i < height; i++) {
  1343. j = 0;
  1344. if(isBE(c->srcFormat) == HAVE_BIGENDIAN &&
  1345. isBE(c->dstFormat) == HAVE_BIGENDIAN &&
  1346. shiftonly) {
  1347. unsigned shift = dst_depth - src_depth;
  1348. #if HAVE_FAST_64BIT
  1349. #define FAST_COPY_UP(shift) \
  1350. for (; j < length - 3; j += 4) { \
  1351. uint64_t v = AV_RN64A(srcPtr2 + j); \
  1352. AV_WN64A(dstPtr2 + j, v << shift); \
  1353. }
  1354. #else
  1355. #define FAST_COPY_UP(shift) \
  1356. for (; j < length - 1; j += 2) { \
  1357. uint32_t v = AV_RN32A(srcPtr2 + j); \
  1358. AV_WN32A(dstPtr2 + j, v << shift); \
  1359. }
  1360. #endif
  1361. switch (shift)
  1362. {
  1363. case 6: FAST_COPY_UP(6); break;
  1364. case 7: FAST_COPY_UP(7); break;
  1365. }
  1366. }
  1367. #define COPY_UP(r,w) \
  1368. if(shiftonly){\
  1369. for (; j < length; j++){ \
  1370. unsigned int v= r(&srcPtr2[j]);\
  1371. w(&dstPtr2[j], v<<(dst_depth-src_depth));\
  1372. }\
  1373. }else{\
  1374. for (; j < length; j++){ \
  1375. unsigned int v= r(&srcPtr2[j]);\
  1376. w(&dstPtr2[j], (v<<(dst_depth-src_depth)) | \
  1377. (v>>(2*src_depth-dst_depth)));\
  1378. }\
  1379. }
  1380. if(isBE(c->srcFormat)){
  1381. if(isBE(c->dstFormat)){
  1382. COPY_UP(AV_RB16, AV_WB16)
  1383. } else {
  1384. COPY_UP(AV_RB16, AV_WL16)
  1385. }
  1386. } else {
  1387. if(isBE(c->dstFormat)){
  1388. COPY_UP(AV_RL16, AV_WB16)
  1389. } else {
  1390. COPY_UP(AV_RL16, AV_WL16)
  1391. }
  1392. }
  1393. dstPtr2 += dstStride[plane]/2;
  1394. srcPtr2 += srcStride[plane]/2;
  1395. }
  1396. } else {
  1397. if(isBE(c->srcFormat) == HAVE_BIGENDIAN){
  1398. if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
  1399. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , )
  1400. } else {
  1401. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, , av_bswap16)
  1402. }
  1403. }else{
  1404. if(isBE(c->dstFormat) == HAVE_BIGENDIAN){
  1405. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, )
  1406. } else {
  1407. DITHER_COPY(dstPtr2, dstStride[plane]/2, srcPtr2, srcStride[plane]/2, av_bswap16, av_bswap16)
  1408. }
  1409. }
  1410. }
  1411. } else if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat) &&
  1412. isBE(c->srcFormat) != isBE(c->dstFormat)) {
  1413. for (i = 0; i < height; i++) {
  1414. for (j = 0; j < length; j++)
  1415. ((uint16_t *) dstPtr)[j] = av_bswap16(((const uint16_t *) srcPtr)[j]);
  1416. srcPtr += srcStride[plane];
  1417. dstPtr += dstStride[plane];
  1418. }
  1419. } else if (dstStride[plane] == srcStride[plane] &&
  1420. srcStride[plane] > 0 && srcStride[plane] == length) {
  1421. memcpy(dst[plane] + dstStride[plane] * y, src[plane],
  1422. height * dstStride[plane]);
  1423. } else {
  1424. if (is16BPS(c->srcFormat) && is16BPS(c->dstFormat))
  1425. length *= 2;
  1426. else if (desc_src->comp[0].depth == 1)
  1427. length >>= 3; // monowhite/black
  1428. for (i = 0; i < height; i++) {
  1429. memcpy(dstPtr, srcPtr, length);
  1430. srcPtr += srcStride[plane];
  1431. dstPtr += dstStride[plane];
  1432. }
  1433. }
  1434. }
  1435. }
  1436. return srcSliceH;
  1437. }
  1438. #define IS_DIFFERENT_ENDIANESS(src_fmt, dst_fmt, pix_fmt) \
  1439. ((src_fmt == pix_fmt ## BE && dst_fmt == pix_fmt ## LE) || \
  1440. (src_fmt == pix_fmt ## LE && dst_fmt == pix_fmt ## BE))
  1441. void ff_get_unscaled_swscale(SwsContext *c)
  1442. {
  1443. const enum AVPixelFormat srcFormat = c->srcFormat;
  1444. const enum AVPixelFormat dstFormat = c->dstFormat;
  1445. const int flags = c->flags;
  1446. const int dstH = c->dstH;
  1447. int needsDither;
  1448. needsDither = isAnyRGB(dstFormat) &&
  1449. c->dstFormatBpp < 24 &&
  1450. (c->dstFormatBpp < c->srcFormatBpp || (!isAnyRGB(srcFormat)));
  1451. /* yv12_to_nv12 */
  1452. if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) &&
  1453. (dstFormat == AV_PIX_FMT_NV12 || dstFormat == AV_PIX_FMT_NV21)) {
  1454. c->swscale = planarToNv12Wrapper;
  1455. }
  1456. /* nv12_to_yv12 */
  1457. if (dstFormat == AV_PIX_FMT_YUV420P &&
  1458. (srcFormat == AV_PIX_FMT_NV12 || srcFormat == AV_PIX_FMT_NV21)) {
  1459. c->swscale = nv12ToPlanarWrapper;
  1460. }
  1461. /* yuv2bgr */
  1462. if ((srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUV422P ||
  1463. srcFormat == AV_PIX_FMT_YUVA420P) && isAnyRGB(dstFormat) &&
  1464. !(flags & SWS_ACCURATE_RND) && (c->dither == SWS_DITHER_BAYER || c->dither == SWS_DITHER_AUTO) && !(dstH & 1)) {
  1465. c->swscale = ff_yuv2rgb_get_func_ptr(c);
  1466. }
  1467. if (srcFormat == AV_PIX_FMT_YUV410P && !(dstH & 3) &&
  1468. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
  1469. !(flags & SWS_BITEXACT)) {
  1470. c->swscale = yvu9ToYv12Wrapper;
  1471. }
  1472. /* bgr24toYV12 */
  1473. if (srcFormat == AV_PIX_FMT_BGR24 &&
  1474. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P) &&
  1475. !(flags & SWS_ACCURATE_RND))
  1476. c->swscale = bgr24ToYv12Wrapper;
  1477. /* RGB/BGR -> RGB/BGR (no dither needed forms) */
  1478. if (isAnyRGB(srcFormat) && isAnyRGB(dstFormat) && findRgbConvFn(c)
  1479. && (!needsDither || (c->flags&(SWS_FAST_BILINEAR|SWS_POINT))))
  1480. c->swscale = rgbToRgbWrapper;
  1481. /* RGB to planar RGB */
  1482. if ((srcFormat == AV_PIX_FMT_GBRP && dstFormat == AV_PIX_FMT_GBRAP) ||
  1483. (srcFormat == AV_PIX_FMT_GBRAP && dstFormat == AV_PIX_FMT_GBRP))
  1484. c->swscale = planarRgbToplanarRgbWrapper;
  1485. #define isByteRGB(f) ( \
  1486. f == AV_PIX_FMT_RGB32 || \
  1487. f == AV_PIX_FMT_RGB32_1 || \
  1488. f == AV_PIX_FMT_RGB24 || \
  1489. f == AV_PIX_FMT_BGR32 || \
  1490. f == AV_PIX_FMT_BGR32_1 || \
  1491. f == AV_PIX_FMT_BGR24)
  1492. if (srcFormat == AV_PIX_FMT_GBRP && isPlanar(srcFormat) && isByteRGB(dstFormat))
  1493. c->swscale = planarRgbToRgbWrapper;
  1494. if ((srcFormat == AV_PIX_FMT_RGB48LE || srcFormat == AV_PIX_FMT_RGB48BE ||
  1495. srcFormat == AV_PIX_FMT_BGR48LE || srcFormat == AV_PIX_FMT_BGR48BE ||
  1496. srcFormat == AV_PIX_FMT_RGBA64LE || srcFormat == AV_PIX_FMT_RGBA64BE ||
  1497. srcFormat == AV_PIX_FMT_BGRA64LE || srcFormat == AV_PIX_FMT_BGRA64BE) &&
  1498. (dstFormat == AV_PIX_FMT_GBRP9LE || dstFormat == AV_PIX_FMT_GBRP9BE ||
  1499. dstFormat == AV_PIX_FMT_GBRP10LE || dstFormat == AV_PIX_FMT_GBRP10BE ||
  1500. dstFormat == AV_PIX_FMT_GBRP12LE || dstFormat == AV_PIX_FMT_GBRP12BE ||
  1501. dstFormat == AV_PIX_FMT_GBRP14LE || dstFormat == AV_PIX_FMT_GBRP14BE ||
  1502. dstFormat == AV_PIX_FMT_GBRP16LE || dstFormat == AV_PIX_FMT_GBRP16BE ||
  1503. dstFormat == AV_PIX_FMT_GBRAP16LE || dstFormat == AV_PIX_FMT_GBRAP16BE ))
  1504. c->swscale = Rgb16ToPlanarRgb16Wrapper;
  1505. if ((srcFormat == AV_PIX_FMT_GBRP9LE || srcFormat == AV_PIX_FMT_GBRP9BE ||
  1506. srcFormat == AV_PIX_FMT_GBRP16LE || srcFormat == AV_PIX_FMT_GBRP16BE ||
  1507. srcFormat == AV_PIX_FMT_GBRP10LE || srcFormat == AV_PIX_FMT_GBRP10BE ||
  1508. srcFormat == AV_PIX_FMT_GBRP12LE || srcFormat == AV_PIX_FMT_GBRP12BE ||
  1509. srcFormat == AV_PIX_FMT_GBRP14LE || srcFormat == AV_PIX_FMT_GBRP14BE ||
  1510. srcFormat == AV_PIX_FMT_GBRAP16LE || srcFormat == AV_PIX_FMT_GBRAP16BE) &&
  1511. (dstFormat == AV_PIX_FMT_RGB48LE || dstFormat == AV_PIX_FMT_RGB48BE ||
  1512. dstFormat == AV_PIX_FMT_BGR48LE || dstFormat == AV_PIX_FMT_BGR48BE ||
  1513. dstFormat == AV_PIX_FMT_RGBA64LE || dstFormat == AV_PIX_FMT_RGBA64BE ||
  1514. dstFormat == AV_PIX_FMT_BGRA64LE || dstFormat == AV_PIX_FMT_BGRA64BE))
  1515. c->swscale = planarRgb16ToRgb16Wrapper;
  1516. if (av_pix_fmt_desc_get(srcFormat)->comp[0].depth == 8 &&
  1517. isPackedRGB(srcFormat) && dstFormat == AV_PIX_FMT_GBRP)
  1518. c->swscale = rgbToPlanarRgbWrapper;
  1519. if (isBayer(srcFormat)) {
  1520. if (dstFormat == AV_PIX_FMT_RGB24)
  1521. c->swscale = bayer_to_rgb24_wrapper;
  1522. else if (dstFormat == AV_PIX_FMT_YUV420P)
  1523. c->swscale = bayer_to_yv12_wrapper;
  1524. else if (!isBayer(dstFormat)) {
  1525. av_log(c, AV_LOG_ERROR, "unsupported bayer conversion\n");
  1526. av_assert0(0);
  1527. }
  1528. }
  1529. /* bswap 16 bits per pixel/component packed formats */
  1530. if (IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_BGGR16) ||
  1531. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_RGGB16) ||
  1532. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_GBRG16) ||
  1533. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BAYER_GRBG16) ||
  1534. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR444) ||
  1535. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR48) ||
  1536. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
  1537. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR555) ||
  1538. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGR565) ||
  1539. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_BGRA64) ||
  1540. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GRAY16) ||
  1541. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YA16) ||
  1542. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_AYUV64) ||
  1543. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP9) ||
  1544. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP10) ||
  1545. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP12) ||
  1546. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP14) ||
  1547. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRP16) ||
  1548. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_GBRAP16) ||
  1549. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB444) ||
  1550. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB48) ||
  1551. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
  1552. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB555) ||
  1553. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGB565) ||
  1554. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_RGBA64) ||
  1555. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_XYZ12) ||
  1556. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P9) ||
  1557. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P10) ||
  1558. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P12) ||
  1559. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P14) ||
  1560. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV420P16) ||
  1561. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P9) ||
  1562. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P10) ||
  1563. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P12) ||
  1564. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P14) ||
  1565. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV422P16) ||
  1566. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV440P10) ||
  1567. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV440P12) ||
  1568. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P9) ||
  1569. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P10) ||
  1570. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P12) ||
  1571. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P14) ||
  1572. IS_DIFFERENT_ENDIANESS(srcFormat, dstFormat, AV_PIX_FMT_YUV444P16))
  1573. c->swscale = packed_16bpc_bswap;
  1574. if (usePal(srcFormat) && isByteRGB(dstFormat))
  1575. c->swscale = palToRgbWrapper;
  1576. if (srcFormat == AV_PIX_FMT_YUV422P) {
  1577. if (dstFormat == AV_PIX_FMT_YUYV422)
  1578. c->swscale = yuv422pToYuy2Wrapper;
  1579. else if (dstFormat == AV_PIX_FMT_UYVY422)
  1580. c->swscale = yuv422pToUyvyWrapper;
  1581. }
  1582. /* LQ converters if -sws 0 or -sws 4*/
  1583. if (c->flags&(SWS_FAST_BILINEAR|SWS_POINT)) {
  1584. /* yv12_to_yuy2 */
  1585. if (srcFormat == AV_PIX_FMT_YUV420P || srcFormat == AV_PIX_FMT_YUVA420P) {
  1586. if (dstFormat == AV_PIX_FMT_YUYV422)
  1587. c->swscale = planarToYuy2Wrapper;
  1588. else if (dstFormat == AV_PIX_FMT_UYVY422)
  1589. c->swscale = planarToUyvyWrapper;
  1590. }
  1591. }
  1592. if (srcFormat == AV_PIX_FMT_YUYV422 &&
  1593. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
  1594. c->swscale = yuyvToYuv420Wrapper;
  1595. if (srcFormat == AV_PIX_FMT_UYVY422 &&
  1596. (dstFormat == AV_PIX_FMT_YUV420P || dstFormat == AV_PIX_FMT_YUVA420P))
  1597. c->swscale = uyvyToYuv420Wrapper;
  1598. if (srcFormat == AV_PIX_FMT_YUYV422 && dstFormat == AV_PIX_FMT_YUV422P)
  1599. c->swscale = yuyvToYuv422Wrapper;
  1600. if (srcFormat == AV_PIX_FMT_UYVY422 && dstFormat == AV_PIX_FMT_YUV422P)
  1601. c->swscale = uyvyToYuv422Wrapper;
  1602. #define isPlanarGray(x) (isGray(x) && (x) != AV_PIX_FMT_YA8 && (x) != AV_PIX_FMT_YA16LE && (x) != AV_PIX_FMT_YA16BE)
  1603. /* simple copy */
  1604. if ( srcFormat == dstFormat ||
  1605. (srcFormat == AV_PIX_FMT_YUVA420P && dstFormat == AV_PIX_FMT_YUV420P) ||
  1606. (srcFormat == AV_PIX_FMT_YUV420P && dstFormat == AV_PIX_FMT_YUVA420P) ||
  1607. (isPlanarYUV(srcFormat) && isPlanarGray(dstFormat)) ||
  1608. (isPlanarYUV(dstFormat) && isPlanarGray(srcFormat)) ||
  1609. (isPlanarGray(dstFormat) && isPlanarGray(srcFormat)) ||
  1610. (isPlanarYUV(srcFormat) && isPlanarYUV(dstFormat) &&
  1611. c->chrDstHSubSample == c->chrSrcHSubSample &&
  1612. c->chrDstVSubSample == c->chrSrcVSubSample &&
  1613. dstFormat != AV_PIX_FMT_NV12 && dstFormat != AV_PIX_FMT_NV21 &&
  1614. dstFormat != AV_PIX_FMT_P010LE && dstFormat != AV_PIX_FMT_P010BE &&
  1615. srcFormat != AV_PIX_FMT_NV12 && srcFormat != AV_PIX_FMT_NV21 &&
  1616. srcFormat != AV_PIX_FMT_P010LE && srcFormat != AV_PIX_FMT_P010BE))
  1617. {
  1618. if (isPacked(c->srcFormat))
  1619. c->swscale = packedCopyWrapper;
  1620. else /* Planar YUV or gray */
  1621. c->swscale = planarCopyWrapper;
  1622. }
  1623. if (ARCH_PPC)
  1624. ff_get_unscaled_swscale_ppc(c);
  1625. if (ARCH_ARM)
  1626. ff_get_unscaled_swscale_arm(c);
  1627. }
  1628. /* Convert the palette to the same packed 32-bit format as the palette */
  1629. void sws_convertPalette8ToPacked32(const uint8_t *src, uint8_t *dst,
  1630. int num_pixels, const uint8_t *palette)
  1631. {
  1632. int i;
  1633. for (i = 0; i < num_pixels; i++)
  1634. ((uint32_t *) dst)[i] = ((const uint32_t *) palette)[src[i]];
  1635. }
  1636. /* Palette format: ABCD -> dst format: ABC */
  1637. void sws_convertPalette8ToPacked24(const uint8_t *src, uint8_t *dst,
  1638. int num_pixels, const uint8_t *palette)
  1639. {
  1640. int i;
  1641. for (i = 0; i < num_pixels; i++) {
  1642. //FIXME slow?
  1643. dst[0] = palette[src[i] * 4 + 0];
  1644. dst[1] = palette[src[i] * 4 + 1];
  1645. dst[2] = palette[src[i] * 4 + 2];
  1646. dst += 3;
  1647. }
  1648. }