swscale_unscaled.c 91 KB

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