swscale_unscaled.c 107 KB

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