swscale_unscaled.c 93 KB

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