SDL_yuv.c 65 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. #include "SDL_endian.h"
  20. #include "SDL_video.h"
  21. #include "SDL_pixels_c.h"
  22. #include "SDL_yuv_c.h"
  23. #include "yuv2rgb/yuv_rgb.h"
  24. #define SDL_YUV_SD_THRESHOLD 576
  25. static SDL_YUV_CONVERSION_MODE SDL_YUV_ConversionMode = SDL_YUV_CONVERSION_BT601;
  26. void SDL_SetYUVConversionMode(SDL_YUV_CONVERSION_MODE mode)
  27. {
  28. SDL_YUV_ConversionMode = mode;
  29. }
  30. SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionMode()
  31. {
  32. return SDL_YUV_ConversionMode;
  33. }
  34. SDL_YUV_CONVERSION_MODE SDL_GetYUVConversionModeForResolution(int width, int height)
  35. {
  36. SDL_YUV_CONVERSION_MODE mode = SDL_GetYUVConversionMode();
  37. if (mode == SDL_YUV_CONVERSION_AUTOMATIC) {
  38. if (height <= SDL_YUV_SD_THRESHOLD) {
  39. mode = SDL_YUV_CONVERSION_BT601;
  40. } else {
  41. mode = SDL_YUV_CONVERSION_BT709;
  42. }
  43. }
  44. return mode;
  45. }
  46. #if SDL_HAVE_YUV
  47. static int GetYUVConversionType(int width, int height, YCbCrType *yuv_type)
  48. {
  49. switch (SDL_GetYUVConversionModeForResolution(width, height)) {
  50. case SDL_YUV_CONVERSION_JPEG:
  51. *yuv_type = YCBCR_JPEG;
  52. break;
  53. case SDL_YUV_CONVERSION_BT601:
  54. *yuv_type = YCBCR_601;
  55. break;
  56. case SDL_YUV_CONVERSION_BT709:
  57. *yuv_type = YCBCR_709;
  58. break;
  59. default:
  60. return SDL_SetError("Unexpected YUV conversion mode");
  61. }
  62. return 0;
  63. }
  64. static SDL_bool IsPlanar2x2Format(Uint32 format)
  65. {
  66. return (format == SDL_PIXELFORMAT_YV12 ||
  67. format == SDL_PIXELFORMAT_IYUV ||
  68. format == SDL_PIXELFORMAT_NV12 ||
  69. format == SDL_PIXELFORMAT_NV21);
  70. }
  71. static SDL_bool IsPacked4Format(Uint32 format)
  72. {
  73. return (format == SDL_PIXELFORMAT_YUY2 ||
  74. format == SDL_PIXELFORMAT_UYVY ||
  75. format == SDL_PIXELFORMAT_YVYU);
  76. }
  77. static int GetYUVPlanes(int width, int height, Uint32 format, const void *yuv, int yuv_pitch,
  78. const Uint8 **y, const Uint8 **u, const Uint8 **v, Uint32 *y_stride, Uint32 *uv_stride)
  79. {
  80. const Uint8 *planes[3] = { NULL, NULL, NULL };
  81. int pitches[3] = { 0, 0, 0 };
  82. switch (format) {
  83. case SDL_PIXELFORMAT_YV12:
  84. case SDL_PIXELFORMAT_IYUV:
  85. pitches[0] = yuv_pitch;
  86. pitches[1] = (pitches[0] + 1) / 2;
  87. pitches[2] = (pitches[0] + 1) / 2;
  88. planes[0] = (const Uint8 *)yuv;
  89. planes[1] = planes[0] + pitches[0] * height;
  90. planes[2] = planes[1] + pitches[1] * ((height + 1) / 2);
  91. break;
  92. case SDL_PIXELFORMAT_YUY2:
  93. case SDL_PIXELFORMAT_UYVY:
  94. case SDL_PIXELFORMAT_YVYU:
  95. pitches[0] = yuv_pitch;
  96. planes[0] = (const Uint8 *)yuv;
  97. break;
  98. case SDL_PIXELFORMAT_NV12:
  99. case SDL_PIXELFORMAT_NV21:
  100. pitches[0] = yuv_pitch;
  101. pitches[1] = 2 * ((pitches[0] + 1) / 2);
  102. planes[0] = (const Uint8 *)yuv;
  103. planes[1] = planes[0] + pitches[0] * height;
  104. break;
  105. default:
  106. return SDL_SetError("GetYUVPlanes(): Unsupported YUV format: %s", SDL_GetPixelFormatName(format));
  107. }
  108. switch (format) {
  109. case SDL_PIXELFORMAT_YV12:
  110. *y = planes[0];
  111. *y_stride = pitches[0];
  112. *v = planes[1];
  113. *u = planes[2];
  114. *uv_stride = pitches[1];
  115. break;
  116. case SDL_PIXELFORMAT_IYUV:
  117. *y = planes[0];
  118. *y_stride = pitches[0];
  119. *v = planes[2];
  120. *u = planes[1];
  121. *uv_stride = pitches[1];
  122. break;
  123. case SDL_PIXELFORMAT_YUY2:
  124. *y = planes[0];
  125. *y_stride = pitches[0];
  126. *v = *y + 3;
  127. *u = *y + 1;
  128. *uv_stride = pitches[0];
  129. break;
  130. case SDL_PIXELFORMAT_UYVY:
  131. *y = planes[0] + 1;
  132. *y_stride = pitches[0];
  133. *v = *y + 1;
  134. *u = *y - 1;
  135. *uv_stride = pitches[0];
  136. break;
  137. case SDL_PIXELFORMAT_YVYU:
  138. *y = planes[0];
  139. *y_stride = pitches[0];
  140. *v = *y + 1;
  141. *u = *y + 3;
  142. *uv_stride = pitches[0];
  143. break;
  144. case SDL_PIXELFORMAT_NV12:
  145. *y = planes[0];
  146. *y_stride = pitches[0];
  147. *u = planes[1];
  148. *v = *u + 1;
  149. *uv_stride = pitches[1];
  150. break;
  151. case SDL_PIXELFORMAT_NV21:
  152. *y = planes[0];
  153. *y_stride = pitches[0];
  154. *v = planes[1];
  155. *u = *v + 1;
  156. *uv_stride = pitches[1];
  157. break;
  158. default:
  159. /* Should have caught this above */
  160. return SDL_SetError("GetYUVPlanes[2]: Unsupported YUV format: %s", SDL_GetPixelFormatName(format));
  161. }
  162. return 0;
  163. }
  164. static SDL_bool yuv_rgb_sse(
  165. Uint32 src_format, Uint32 dst_format,
  166. Uint32 width, Uint32 height,
  167. const Uint8 *y, const Uint8 *u, const Uint8 *v, Uint32 y_stride, Uint32 uv_stride,
  168. Uint8 *rgb, Uint32 rgb_stride,
  169. YCbCrType yuv_type)
  170. {
  171. #ifdef __SSE2__
  172. if (!SDL_HasSSE2()) {
  173. return SDL_FALSE;
  174. }
  175. if (src_format == SDL_PIXELFORMAT_YV12 ||
  176. src_format == SDL_PIXELFORMAT_IYUV) {
  177. switch (dst_format) {
  178. case SDL_PIXELFORMAT_RGB565:
  179. yuv420_rgb565_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  180. return SDL_TRUE;
  181. case SDL_PIXELFORMAT_RGB24:
  182. yuv420_rgb24_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  183. return SDL_TRUE;
  184. case SDL_PIXELFORMAT_RGBX8888:
  185. case SDL_PIXELFORMAT_RGBA8888:
  186. yuv420_rgba_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  187. return SDL_TRUE;
  188. case SDL_PIXELFORMAT_BGRX8888:
  189. case SDL_PIXELFORMAT_BGRA8888:
  190. yuv420_bgra_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  191. return SDL_TRUE;
  192. case SDL_PIXELFORMAT_RGB888:
  193. case SDL_PIXELFORMAT_ARGB8888:
  194. yuv420_argb_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  195. return SDL_TRUE;
  196. case SDL_PIXELFORMAT_BGR888:
  197. case SDL_PIXELFORMAT_ABGR8888:
  198. yuv420_abgr_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  199. return SDL_TRUE;
  200. default:
  201. break;
  202. }
  203. }
  204. if (src_format == SDL_PIXELFORMAT_YUY2 ||
  205. src_format == SDL_PIXELFORMAT_UYVY ||
  206. src_format == SDL_PIXELFORMAT_YVYU) {
  207. switch (dst_format) {
  208. case SDL_PIXELFORMAT_RGB565:
  209. yuv422_rgb565_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  210. return SDL_TRUE;
  211. case SDL_PIXELFORMAT_RGB24:
  212. yuv422_rgb24_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  213. return SDL_TRUE;
  214. case SDL_PIXELFORMAT_RGBX8888:
  215. case SDL_PIXELFORMAT_RGBA8888:
  216. yuv422_rgba_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  217. return SDL_TRUE;
  218. case SDL_PIXELFORMAT_BGRX8888:
  219. case SDL_PIXELFORMAT_BGRA8888:
  220. yuv422_bgra_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  221. return SDL_TRUE;
  222. case SDL_PIXELFORMAT_RGB888:
  223. case SDL_PIXELFORMAT_ARGB8888:
  224. yuv422_argb_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  225. return SDL_TRUE;
  226. case SDL_PIXELFORMAT_BGR888:
  227. case SDL_PIXELFORMAT_ABGR8888:
  228. yuv422_abgr_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  229. return SDL_TRUE;
  230. default:
  231. break;
  232. }
  233. }
  234. if (src_format == SDL_PIXELFORMAT_NV12 ||
  235. src_format == SDL_PIXELFORMAT_NV21) {
  236. switch (dst_format) {
  237. case SDL_PIXELFORMAT_RGB565:
  238. yuvnv12_rgb565_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  239. return SDL_TRUE;
  240. case SDL_PIXELFORMAT_RGB24:
  241. yuvnv12_rgb24_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  242. return SDL_TRUE;
  243. case SDL_PIXELFORMAT_RGBX8888:
  244. case SDL_PIXELFORMAT_RGBA8888:
  245. yuvnv12_rgba_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  246. return SDL_TRUE;
  247. case SDL_PIXELFORMAT_BGRX8888:
  248. case SDL_PIXELFORMAT_BGRA8888:
  249. yuvnv12_bgra_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  250. return SDL_TRUE;
  251. case SDL_PIXELFORMAT_RGB888:
  252. case SDL_PIXELFORMAT_ARGB8888:
  253. yuvnv12_argb_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  254. return SDL_TRUE;
  255. case SDL_PIXELFORMAT_BGR888:
  256. case SDL_PIXELFORMAT_ABGR8888:
  257. yuvnv12_abgr_sseu(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  258. return SDL_TRUE;
  259. default:
  260. break;
  261. }
  262. }
  263. #endif
  264. return SDL_FALSE;
  265. }
  266. static SDL_bool yuv_rgb_lsx(
  267. Uint32 src_format, Uint32 dst_format,
  268. Uint32 width, Uint32 height,
  269. const Uint8 *y, const Uint8 *u, const Uint8 *v, Uint32 y_stride, Uint32 uv_stride,
  270. Uint8 *rgb, Uint32 rgb_stride,
  271. YCbCrType yuv_type)
  272. {
  273. #ifdef __loongarch_sx
  274. if (!SDL_HasLSX()) {
  275. return SDL_FALSE;
  276. }
  277. if (src_format == SDL_PIXELFORMAT_YV12 ||
  278. src_format == SDL_PIXELFORMAT_IYUV) {
  279. switch (dst_format) {
  280. case SDL_PIXELFORMAT_RGB24:
  281. yuv420_rgb24_lsx(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  282. return SDL_TRUE;
  283. case SDL_PIXELFORMAT_RGBX8888:
  284. case SDL_PIXELFORMAT_RGBA8888:
  285. yuv420_rgba_lsx(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  286. return SDL_TRUE;
  287. case SDL_PIXELFORMAT_BGRX8888:
  288. case SDL_PIXELFORMAT_BGRA8888:
  289. yuv420_bgra_lsx(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  290. return SDL_TRUE;
  291. case SDL_PIXELFORMAT_RGB888:
  292. case SDL_PIXELFORMAT_ARGB8888:
  293. yuv420_argb_lsx(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  294. return SDL_TRUE;
  295. case SDL_PIXELFORMAT_BGR888:
  296. case SDL_PIXELFORMAT_ABGR8888:
  297. yuv420_abgr_lsx(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  298. return SDL_TRUE;
  299. default:
  300. break;
  301. }
  302. }
  303. #endif
  304. return SDL_FALSE;
  305. }
  306. static SDL_bool yuv_rgb_std(
  307. Uint32 src_format, Uint32 dst_format,
  308. Uint32 width, Uint32 height,
  309. const Uint8 *y, const Uint8 *u, const Uint8 *v, Uint32 y_stride, Uint32 uv_stride,
  310. Uint8 *rgb, Uint32 rgb_stride,
  311. YCbCrType yuv_type)
  312. {
  313. if (src_format == SDL_PIXELFORMAT_YV12 ||
  314. src_format == SDL_PIXELFORMAT_IYUV) {
  315. switch (dst_format) {
  316. case SDL_PIXELFORMAT_RGB565:
  317. yuv420_rgb565_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  318. return SDL_TRUE;
  319. case SDL_PIXELFORMAT_RGB24:
  320. yuv420_rgb24_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  321. return SDL_TRUE;
  322. case SDL_PIXELFORMAT_RGBX8888:
  323. case SDL_PIXELFORMAT_RGBA8888:
  324. yuv420_rgba_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  325. return SDL_TRUE;
  326. case SDL_PIXELFORMAT_BGRX8888:
  327. case SDL_PIXELFORMAT_BGRA8888:
  328. yuv420_bgra_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  329. return SDL_TRUE;
  330. case SDL_PIXELFORMAT_RGB888:
  331. case SDL_PIXELFORMAT_ARGB8888:
  332. yuv420_argb_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  333. return SDL_TRUE;
  334. case SDL_PIXELFORMAT_BGR888:
  335. case SDL_PIXELFORMAT_ABGR8888:
  336. yuv420_abgr_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  337. return SDL_TRUE;
  338. default:
  339. break;
  340. }
  341. }
  342. if (src_format == SDL_PIXELFORMAT_YUY2 ||
  343. src_format == SDL_PIXELFORMAT_UYVY ||
  344. src_format == SDL_PIXELFORMAT_YVYU) {
  345. switch (dst_format) {
  346. case SDL_PIXELFORMAT_RGB565:
  347. yuv422_rgb565_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  348. return SDL_TRUE;
  349. case SDL_PIXELFORMAT_RGB24:
  350. yuv422_rgb24_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  351. return SDL_TRUE;
  352. case SDL_PIXELFORMAT_RGBX8888:
  353. case SDL_PIXELFORMAT_RGBA8888:
  354. yuv422_rgba_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  355. return SDL_TRUE;
  356. case SDL_PIXELFORMAT_BGRX8888:
  357. case SDL_PIXELFORMAT_BGRA8888:
  358. yuv422_bgra_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  359. return SDL_TRUE;
  360. case SDL_PIXELFORMAT_RGB888:
  361. case SDL_PIXELFORMAT_ARGB8888:
  362. yuv422_argb_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  363. return SDL_TRUE;
  364. case SDL_PIXELFORMAT_BGR888:
  365. case SDL_PIXELFORMAT_ABGR8888:
  366. yuv422_abgr_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  367. return SDL_TRUE;
  368. default:
  369. break;
  370. }
  371. }
  372. if (src_format == SDL_PIXELFORMAT_NV12 ||
  373. src_format == SDL_PIXELFORMAT_NV21) {
  374. switch (dst_format) {
  375. case SDL_PIXELFORMAT_RGB565:
  376. yuvnv12_rgb565_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  377. return SDL_TRUE;
  378. case SDL_PIXELFORMAT_RGB24:
  379. yuvnv12_rgb24_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  380. return SDL_TRUE;
  381. case SDL_PIXELFORMAT_RGBX8888:
  382. case SDL_PIXELFORMAT_RGBA8888:
  383. yuvnv12_rgba_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  384. return SDL_TRUE;
  385. case SDL_PIXELFORMAT_BGRX8888:
  386. case SDL_PIXELFORMAT_BGRA8888:
  387. yuvnv12_bgra_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  388. return SDL_TRUE;
  389. case SDL_PIXELFORMAT_RGB888:
  390. case SDL_PIXELFORMAT_ARGB8888:
  391. yuvnv12_argb_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  392. return SDL_TRUE;
  393. case SDL_PIXELFORMAT_BGR888:
  394. case SDL_PIXELFORMAT_ABGR8888:
  395. yuvnv12_abgr_std(width, height, y, u, v, y_stride, uv_stride, rgb, rgb_stride, yuv_type);
  396. return SDL_TRUE;
  397. default:
  398. break;
  399. }
  400. }
  401. return SDL_FALSE;
  402. }
  403. int
  404. SDL_ConvertPixels_YUV_to_RGB(int width, int height,
  405. Uint32 src_format, const void *src, int src_pitch,
  406. Uint32 dst_format, void *dst, int dst_pitch)
  407. {
  408. const Uint8 *y = NULL;
  409. const Uint8 *u = NULL;
  410. const Uint8 *v = NULL;
  411. Uint32 y_stride = 0;
  412. Uint32 uv_stride = 0;
  413. YCbCrType yuv_type = YCBCR_601;
  414. if (GetYUVPlanes(width, height, src_format, src, src_pitch, &y, &u, &v, &y_stride, &uv_stride) < 0) {
  415. return -1;
  416. }
  417. if (GetYUVConversionType(width, height, &yuv_type) < 0) {
  418. return -1;
  419. }
  420. if (yuv_rgb_sse(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8*)dst, dst_pitch, yuv_type)) {
  421. return 0;
  422. }
  423. if (yuv_rgb_lsx(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8*)dst, dst_pitch, yuv_type)) {
  424. return 0;
  425. }
  426. if (yuv_rgb_std(src_format, dst_format, width, height, y, u, v, y_stride, uv_stride, (Uint8*)dst, dst_pitch, yuv_type)) {
  427. return 0;
  428. }
  429. /* No fast path for the RGB format, instead convert using an intermediate buffer */
  430. if (dst_format != SDL_PIXELFORMAT_ARGB8888) {
  431. int ret;
  432. void *tmp;
  433. int tmp_pitch = (width * sizeof(Uint32));
  434. tmp = SDL_malloc(tmp_pitch * height);
  435. if (tmp == NULL) {
  436. return SDL_OutOfMemory();
  437. }
  438. /* convert src/src_format to tmp/ARGB8888 */
  439. ret = SDL_ConvertPixels_YUV_to_RGB(width, height, src_format, src, src_pitch, SDL_PIXELFORMAT_ARGB8888, tmp, tmp_pitch);
  440. if (ret < 0) {
  441. SDL_free(tmp);
  442. return ret;
  443. }
  444. /* convert tmp/ARGB8888 to dst/RGB */
  445. ret = SDL_ConvertPixels(width, height, SDL_PIXELFORMAT_ARGB8888, tmp, tmp_pitch, dst_format, dst, dst_pitch);
  446. SDL_free(tmp);
  447. return ret;
  448. }
  449. return SDL_SetError("Unsupported YUV conversion");
  450. }
  451. struct RGB2YUVFactors
  452. {
  453. int y_offset;
  454. float y[3]; /* Rfactor, Gfactor, Bfactor */
  455. float u[3]; /* Rfactor, Gfactor, Bfactor */
  456. float v[3]; /* Rfactor, Gfactor, Bfactor */
  457. };
  458. static int
  459. SDL_ConvertPixels_ARGB8888_to_YUV(int width, int height, const void *src, int src_pitch, Uint32 dst_format, void *dst, int dst_pitch)
  460. {
  461. const int src_pitch_x_2 = src_pitch * 2;
  462. const int height_half = height / 2;
  463. const int height_remainder = (height & 0x1);
  464. const int width_half = width / 2;
  465. const int width_remainder = (width & 0x1);
  466. int i, j;
  467. static struct RGB2YUVFactors RGB2YUVFactorTables[SDL_YUV_CONVERSION_BT709 + 1] =
  468. {
  469. /* ITU-T T.871 (JPEG) */
  470. {
  471. 0,
  472. { 0.2990f, 0.5870f, 0.1140f },
  473. { -0.1687f, -0.3313f, 0.5000f },
  474. { 0.5000f, -0.4187f, -0.0813f },
  475. },
  476. /* ITU-R BT.601-7 */
  477. {
  478. 16,
  479. { 0.2568f, 0.5041f, 0.0979f },
  480. { -0.1482f, -0.2910f, 0.4392f },
  481. { 0.4392f, -0.3678f, -0.0714f },
  482. },
  483. /* ITU-R BT.709-6 */
  484. {
  485. 16,
  486. { 0.1826f, 0.6142f, 0.0620f },
  487. {-0.1006f, -0.3386f, 0.4392f },
  488. { 0.4392f, -0.3989f, -0.0403f },
  489. },
  490. };
  491. const struct RGB2YUVFactors *cvt = &RGB2YUVFactorTables[SDL_GetYUVConversionModeForResolution(width, height)];
  492. #define MAKE_Y(r, g, b) (Uint8)((int)(cvt->y[0] * (r) + cvt->y[1] * (g) + cvt->y[2] * (b) + 0.5f) + cvt->y_offset)
  493. #define MAKE_U(r, g, b) (Uint8)((int)(cvt->u[0] * (r) + cvt->u[1] * (g) + cvt->u[2] * (b) + 0.5f) + 128)
  494. #define MAKE_V(r, g, b) (Uint8)((int)(cvt->v[0] * (r) + cvt->v[1] * (g) + cvt->v[2] * (b) + 0.5f) + 128)
  495. #define READ_2x2_PIXELS \
  496. const Uint32 p1 = ((const Uint32 *)curr_row)[2 * i]; \
  497. const Uint32 p2 = ((const Uint32 *)curr_row)[2 * i + 1]; \
  498. const Uint32 p3 = ((const Uint32 *)next_row)[2 * i]; \
  499. const Uint32 p4 = ((const Uint32 *)next_row)[2 * i + 1]; \
  500. const Uint32 r = ((p1 & 0x00ff0000) + (p2 & 0x00ff0000) + (p3 & 0x00ff0000) + (p4 & 0x00ff0000)) >> 18; \
  501. const Uint32 g = ((p1 & 0x0000ff00) + (p2 & 0x0000ff00) + (p3 & 0x0000ff00) + (p4 & 0x0000ff00)) >> 10; \
  502. const Uint32 b = ((p1 & 0x000000ff) + (p2 & 0x000000ff) + (p3 & 0x000000ff) + (p4 & 0x000000ff)) >> 2; \
  503. #define READ_2x1_PIXELS \
  504. const Uint32 p1 = ((const Uint32 *)curr_row)[2 * i]; \
  505. const Uint32 p2 = ((const Uint32 *)next_row)[2 * i]; \
  506. const Uint32 r = ((p1 & 0x00ff0000) + (p2 & 0x00ff0000)) >> 17; \
  507. const Uint32 g = ((p1 & 0x0000ff00) + (p2 & 0x0000ff00)) >> 9; \
  508. const Uint32 b = ((p1 & 0x000000ff) + (p2 & 0x000000ff)) >> 1; \
  509. #define READ_1x2_PIXELS \
  510. const Uint32 p1 = ((const Uint32 *)curr_row)[2 * i]; \
  511. const Uint32 p2 = ((const Uint32 *)curr_row)[2 * i + 1]; \
  512. const Uint32 r = ((p1 & 0x00ff0000) + (p2 & 0x00ff0000)) >> 17; \
  513. const Uint32 g = ((p1 & 0x0000ff00) + (p2 & 0x0000ff00)) >> 9; \
  514. const Uint32 b = ((p1 & 0x000000ff) + (p2 & 0x000000ff)) >> 1; \
  515. #define READ_1x1_PIXEL \
  516. const Uint32 p = ((const Uint32 *)curr_row)[2 * i]; \
  517. const Uint32 r = (p & 0x00ff0000) >> 16; \
  518. const Uint32 g = (p & 0x0000ff00) >> 8; \
  519. const Uint32 b = (p & 0x000000ff); \
  520. #define READ_TWO_RGB_PIXELS \
  521. const Uint32 p = ((const Uint32 *)curr_row)[2 * i]; \
  522. const Uint32 r = (p & 0x00ff0000) >> 16; \
  523. const Uint32 g = (p & 0x0000ff00) >> 8; \
  524. const Uint32 b = (p & 0x000000ff); \
  525. const Uint32 p1 = ((const Uint32 *)curr_row)[2 * i + 1]; \
  526. const Uint32 r1 = (p1 & 0x00ff0000) >> 16; \
  527. const Uint32 g1 = (p1 & 0x0000ff00) >> 8; \
  528. const Uint32 b1 = (p1 & 0x000000ff); \
  529. const Uint32 R = (r + r1)/2; \
  530. const Uint32 G = (g + g1)/2; \
  531. const Uint32 B = (b + b1)/2; \
  532. #define READ_ONE_RGB_PIXEL READ_1x1_PIXEL
  533. switch (dst_format)
  534. {
  535. case SDL_PIXELFORMAT_YV12:
  536. case SDL_PIXELFORMAT_IYUV:
  537. case SDL_PIXELFORMAT_NV12:
  538. case SDL_PIXELFORMAT_NV21:
  539. {
  540. const Uint8 *curr_row, *next_row;
  541. Uint8 *plane_y;
  542. Uint8 *plane_u;
  543. Uint8 *plane_v;
  544. Uint8 *plane_interleaved_uv;
  545. Uint32 y_stride, uv_stride, y_skip, uv_skip;
  546. if (GetYUVPlanes(width, height, dst_format, dst, dst_pitch,
  547. (const Uint8 **)&plane_y, (const Uint8 **)&plane_u, (const Uint8 **)&plane_v,
  548. &y_stride, &uv_stride) != 0) {
  549. return -1;
  550. }
  551. plane_interleaved_uv = (plane_y + height * y_stride);
  552. y_skip = (y_stride - width);
  553. curr_row = (const Uint8*)src;
  554. /* Write Y plane */
  555. for (j = 0; j < height; j++) {
  556. for (i = 0; i < width; i++) {
  557. const Uint32 p1 = ((const Uint32 *)curr_row)[i];
  558. const Uint32 r = (p1 & 0x00ff0000) >> 16;
  559. const Uint32 g = (p1 & 0x0000ff00) >> 8;
  560. const Uint32 b = (p1 & 0x000000ff);
  561. *plane_y++ = MAKE_Y(r, g, b);
  562. }
  563. plane_y += y_skip;
  564. curr_row += src_pitch;
  565. }
  566. curr_row = (const Uint8*)src;
  567. next_row = (const Uint8*)src;
  568. next_row += src_pitch;
  569. if (dst_format == SDL_PIXELFORMAT_YV12 || dst_format == SDL_PIXELFORMAT_IYUV)
  570. {
  571. /* Write UV planes, not interleaved */
  572. uv_skip = (uv_stride - (width + 1)/2);
  573. for (j = 0; j < height_half; j++) {
  574. for (i = 0; i < width_half; i++) {
  575. READ_2x2_PIXELS;
  576. *plane_u++ = MAKE_U(r, g, b);
  577. *plane_v++ = MAKE_V(r, g, b);
  578. }
  579. if (width_remainder) {
  580. READ_2x1_PIXELS;
  581. *plane_u++ = MAKE_U(r, g, b);
  582. *plane_v++ = MAKE_V(r, g, b);
  583. }
  584. plane_u += uv_skip;
  585. plane_v += uv_skip;
  586. curr_row += src_pitch_x_2;
  587. next_row += src_pitch_x_2;
  588. }
  589. if (height_remainder) {
  590. for (i = 0; i < width_half; i++) {
  591. READ_1x2_PIXELS;
  592. *plane_u++ = MAKE_U(r, g, b);
  593. *plane_v++ = MAKE_V(r, g, b);
  594. }
  595. if (width_remainder) {
  596. READ_1x1_PIXEL;
  597. *plane_u++ = MAKE_U(r, g, b);
  598. *plane_v++ = MAKE_V(r, g, b);
  599. }
  600. plane_u += uv_skip;
  601. plane_v += uv_skip;
  602. }
  603. }
  604. else if (dst_format == SDL_PIXELFORMAT_NV12)
  605. {
  606. uv_skip = (uv_stride - ((width + 1)/2)*2);
  607. for (j = 0; j < height_half; j++) {
  608. for (i = 0; i < width_half; i++) {
  609. READ_2x2_PIXELS;
  610. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  611. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  612. }
  613. if (width_remainder) {
  614. READ_2x1_PIXELS;
  615. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  616. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  617. }
  618. plane_interleaved_uv += uv_skip;
  619. curr_row += src_pitch_x_2;
  620. next_row += src_pitch_x_2;
  621. }
  622. if (height_remainder) {
  623. for (i = 0; i < width_half; i++) {
  624. READ_1x2_PIXELS;
  625. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  626. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  627. }
  628. if (width_remainder) {
  629. READ_1x1_PIXEL;
  630. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  631. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  632. }
  633. }
  634. }
  635. else /* dst_format == SDL_PIXELFORMAT_NV21 */
  636. {
  637. uv_skip = (uv_stride - ((width + 1)/2)*2);
  638. for (j = 0; j < height_half; j++) {
  639. for (i = 0; i < width_half; i++) {
  640. READ_2x2_PIXELS;
  641. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  642. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  643. }
  644. if (width_remainder) {
  645. READ_2x1_PIXELS;
  646. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  647. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  648. }
  649. plane_interleaved_uv += uv_skip;
  650. curr_row += src_pitch_x_2;
  651. next_row += src_pitch_x_2;
  652. }
  653. if (height_remainder) {
  654. for (i = 0; i < width_half; i++) {
  655. READ_1x2_PIXELS;
  656. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  657. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  658. }
  659. if (width_remainder) {
  660. READ_1x1_PIXEL;
  661. *plane_interleaved_uv++ = MAKE_V(r, g, b);
  662. *plane_interleaved_uv++ = MAKE_U(r, g, b);
  663. }
  664. }
  665. }
  666. }
  667. break;
  668. case SDL_PIXELFORMAT_YUY2:
  669. case SDL_PIXELFORMAT_UYVY:
  670. case SDL_PIXELFORMAT_YVYU:
  671. {
  672. const Uint8 *curr_row = (const Uint8*) src;
  673. Uint8 *plane = (Uint8*) dst;
  674. const int row_size = (4 * ((width + 1) / 2));
  675. int plane_skip;
  676. if (dst_pitch < row_size) {
  677. return SDL_SetError("Destination pitch is too small, expected at least %d\n", row_size);
  678. }
  679. plane_skip = (dst_pitch - row_size);
  680. /* Write YUV plane, packed */
  681. if (dst_format == SDL_PIXELFORMAT_YUY2)
  682. {
  683. for (j = 0; j < height; j++) {
  684. for (i = 0; i < width_half; i++) {
  685. READ_TWO_RGB_PIXELS;
  686. /* Y U Y1 V */
  687. *plane++ = MAKE_Y(r, g, b);
  688. *plane++ = MAKE_U(R, G, B);
  689. *plane++ = MAKE_Y(r1, g1, b1);
  690. *plane++ = MAKE_V(R, G, B);
  691. }
  692. if (width_remainder) {
  693. READ_ONE_RGB_PIXEL;
  694. /* Y U Y V */
  695. *plane++ = MAKE_Y(r, g, b);
  696. *plane++ = MAKE_U(r, g, b);
  697. *plane++ = MAKE_Y(r, g, b);
  698. *plane++ = MAKE_V(r, g, b);
  699. }
  700. plane += plane_skip;
  701. curr_row += src_pitch;
  702. }
  703. }
  704. else if (dst_format == SDL_PIXELFORMAT_UYVY)
  705. {
  706. for (j = 0; j < height; j++) {
  707. for (i = 0; i < width_half; i++) {
  708. READ_TWO_RGB_PIXELS;
  709. /* U Y V Y1 */
  710. *plane++ = MAKE_U(R, G, B);
  711. *plane++ = MAKE_Y(r, g, b);
  712. *plane++ = MAKE_V(R, G, B);
  713. *plane++ = MAKE_Y(r1, g1, b1);
  714. }
  715. if (width_remainder) {
  716. READ_ONE_RGB_PIXEL;
  717. /* U Y V Y */
  718. *plane++ = MAKE_U(r, g, b);
  719. *plane++ = MAKE_Y(r, g, b);
  720. *plane++ = MAKE_V(r, g, b);
  721. *plane++ = MAKE_Y(r, g, b);
  722. }
  723. plane += plane_skip;
  724. curr_row += src_pitch;
  725. }
  726. }
  727. else if (dst_format == SDL_PIXELFORMAT_YVYU)
  728. {
  729. for (j = 0; j < height; j++) {
  730. for (i = 0; i < width_half; i++) {
  731. READ_TWO_RGB_PIXELS;
  732. /* Y V Y1 U */
  733. *plane++ = MAKE_Y(r, g, b);
  734. *plane++ = MAKE_V(R, G, B);
  735. *plane++ = MAKE_Y(r1, g1, b1);
  736. *plane++ = MAKE_U(R, G, B);
  737. }
  738. if (width_remainder) {
  739. READ_ONE_RGB_PIXEL;
  740. /* Y V Y U */
  741. *plane++ = MAKE_Y(r, g, b);
  742. *plane++ = MAKE_V(r, g, b);
  743. *plane++ = MAKE_Y(r, g, b);
  744. *plane++ = MAKE_U(r, g, b);
  745. }
  746. plane += plane_skip;
  747. curr_row += src_pitch;
  748. }
  749. }
  750. }
  751. break;
  752. default:
  753. return SDL_SetError("Unsupported YUV destination format: %s", SDL_GetPixelFormatName(dst_format));
  754. }
  755. #undef MAKE_Y
  756. #undef MAKE_U
  757. #undef MAKE_V
  758. #undef READ_2x2_PIXELS
  759. #undef READ_2x1_PIXELS
  760. #undef READ_1x2_PIXELS
  761. #undef READ_1x1_PIXEL
  762. #undef READ_TWO_RGB_PIXELS
  763. #undef READ_ONE_RGB_PIXEL
  764. return 0;
  765. }
  766. int
  767. SDL_ConvertPixels_RGB_to_YUV(int width, int height,
  768. Uint32 src_format, const void *src, int src_pitch,
  769. Uint32 dst_format, void *dst, int dst_pitch)
  770. {
  771. #if 0 /* Doesn't handle odd widths */
  772. /* RGB24 to FOURCC */
  773. if (src_format == SDL_PIXELFORMAT_RGB24) {
  774. Uint8 *y;
  775. Uint8 *u;
  776. Uint8 *v;
  777. Uint32 y_stride;
  778. Uint32 uv_stride;
  779. YCbCrType yuv_type;
  780. if (GetYUVPlanes(width, height, dst_format, dst, dst_pitch, (const Uint8 **)&y, (const Uint8 **)&u, (const Uint8 **)&v, &y_stride, &uv_stride) < 0) {
  781. return -1;
  782. }
  783. if (GetYUVConversionType(width, height, &yuv_type) < 0) {
  784. return -1;
  785. }
  786. rgb24_yuv420_std(width, height, src, src_pitch, y, u, v, y_stride, uv_stride, yuv_type);
  787. return 0;
  788. }
  789. #endif
  790. /* ARGB8888 to FOURCC */
  791. if (src_format == SDL_PIXELFORMAT_ARGB8888) {
  792. return SDL_ConvertPixels_ARGB8888_to_YUV(width, height, src, src_pitch, dst_format, dst, dst_pitch);
  793. }
  794. /* not ARGB8888 to FOURCC : need an intermediate conversion */
  795. {
  796. int ret;
  797. void *tmp;
  798. int tmp_pitch = (width * sizeof(Uint32));
  799. tmp = SDL_malloc(tmp_pitch * height);
  800. if (tmp == NULL) {
  801. return SDL_OutOfMemory();
  802. }
  803. /* convert src/src_format to tmp/ARGB8888 */
  804. ret = SDL_ConvertPixels(width, height, src_format, src, src_pitch, SDL_PIXELFORMAT_ARGB8888, tmp, tmp_pitch);
  805. if (ret == -1) {
  806. SDL_free(tmp);
  807. return ret;
  808. }
  809. /* convert tmp/ARGB8888 to dst/FOURCC */
  810. ret = SDL_ConvertPixels_ARGB8888_to_YUV(width, height, tmp, tmp_pitch, dst_format, dst, dst_pitch);
  811. SDL_free(tmp);
  812. return ret;
  813. }
  814. }
  815. static int
  816. SDL_ConvertPixels_YUV_to_YUV_Copy(int width, int height, Uint32 format,
  817. const void *src, int src_pitch, void *dst, int dst_pitch)
  818. {
  819. int i;
  820. if (IsPlanar2x2Format(format)) {
  821. /* Y plane */
  822. for (i = height; i--;) {
  823. SDL_memcpy(dst, src, width);
  824. src = (const Uint8*)src + src_pitch;
  825. dst = (Uint8*)dst + dst_pitch;
  826. }
  827. if (format == SDL_PIXELFORMAT_YV12 || format == SDL_PIXELFORMAT_IYUV) {
  828. /* U and V planes are a quarter the size of the Y plane, rounded up */
  829. width = (width + 1) / 2;
  830. height = (height + 1) / 2;
  831. src_pitch = (src_pitch + 1) / 2;
  832. dst_pitch = (dst_pitch + 1) / 2;
  833. for (i = height * 2; i--;) {
  834. SDL_memcpy(dst, src, width);
  835. src = (const Uint8*)src + src_pitch;
  836. dst = (Uint8*)dst + dst_pitch;
  837. }
  838. } else if (format == SDL_PIXELFORMAT_NV12 || format == SDL_PIXELFORMAT_NV21) {
  839. /* U/V plane is half the height of the Y plane, rounded up */
  840. height = (height + 1) / 2;
  841. width = ((width + 1) / 2)*2;
  842. src_pitch = ((src_pitch + 1) / 2)*2;
  843. dst_pitch = ((dst_pitch + 1) / 2)*2;
  844. for (i = height; i--;) {
  845. SDL_memcpy(dst, src, width);
  846. src = (const Uint8*)src + src_pitch;
  847. dst = (Uint8*)dst + dst_pitch;
  848. }
  849. }
  850. return 0;
  851. }
  852. if (IsPacked4Format(format)) {
  853. /* Packed planes */
  854. width = 4 * ((width + 1) / 2);
  855. for (i = height; i--;) {
  856. SDL_memcpy(dst, src, width);
  857. src = (const Uint8*)src + src_pitch;
  858. dst = (Uint8*)dst + dst_pitch;
  859. }
  860. return 0;
  861. }
  862. return SDL_SetError("SDL_ConvertPixels_YUV_to_YUV_Copy: Unsupported YUV format: %s", SDL_GetPixelFormatName(format));
  863. }
  864. static int
  865. SDL_ConvertPixels_SwapUVPlanes(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  866. {
  867. int y;
  868. const int UVwidth = (width + 1)/2;
  869. const int UVheight = (height + 1)/2;
  870. /* Skip the Y plane */
  871. src = (const Uint8 *)src + height * src_pitch;
  872. dst = (Uint8 *)dst + height * dst_pitch;
  873. if (src == dst) {
  874. int UVpitch = (dst_pitch + 1)/2;
  875. Uint8 *tmp;
  876. Uint8 *row1 = dst;
  877. Uint8 *row2 = (Uint8 *)dst + UVheight * UVpitch;
  878. /* Allocate a temporary row for the swap */
  879. tmp = (Uint8 *)SDL_malloc(UVwidth);
  880. if (!tmp) {
  881. return SDL_OutOfMemory();
  882. }
  883. for (y = 0; y < UVheight; ++y) {
  884. SDL_memcpy(tmp, row1, UVwidth);
  885. SDL_memcpy(row1, row2, UVwidth);
  886. SDL_memcpy(row2, tmp, UVwidth);
  887. row1 += UVpitch;
  888. row2 += UVpitch;
  889. }
  890. SDL_free(tmp);
  891. } else {
  892. const Uint8 *srcUV;
  893. Uint8 *dstUV;
  894. int srcUVPitch = ((src_pitch + 1)/2);
  895. int dstUVPitch = ((dst_pitch + 1)/2);
  896. /* Copy the first plane */
  897. srcUV = (const Uint8 *)src;
  898. dstUV = (Uint8 *)dst + UVheight * dstUVPitch;
  899. for (y = 0; y < UVheight; ++y) {
  900. SDL_memcpy(dstUV, srcUV, UVwidth);
  901. srcUV += srcUVPitch;
  902. dstUV += dstUVPitch;
  903. }
  904. /* Copy the second plane */
  905. dstUV = (Uint8 *)dst;
  906. for (y = 0; y < UVheight; ++y) {
  907. SDL_memcpy(dstUV, srcUV, UVwidth);
  908. srcUV += srcUVPitch;
  909. dstUV += dstUVPitch;
  910. }
  911. }
  912. return 0;
  913. }
  914. static int
  915. SDL_ConvertPixels_PackUVPlanes_to_NV(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch, SDL_bool reverseUV)
  916. {
  917. int x, y;
  918. const int UVwidth = (width + 1)/2;
  919. const int UVheight = (height + 1)/2;
  920. const int srcUVPitch = ((src_pitch + 1)/2);
  921. const int srcUVPitchLeft = srcUVPitch - UVwidth;
  922. const int dstUVPitch = ((dst_pitch + 1)/2)*2;
  923. const int dstUVPitchLeft = dstUVPitch - UVwidth*2;
  924. const Uint8 *src1, *src2;
  925. Uint8 *dstUV;
  926. Uint8 *tmp = NULL;
  927. #ifdef __SSE2__
  928. const SDL_bool use_SSE2 = SDL_HasSSE2();
  929. #endif
  930. /* Skip the Y plane */
  931. src = (const Uint8 *)src + height * src_pitch;
  932. dst = (Uint8 *)dst + height * dst_pitch;
  933. if (src == dst) {
  934. /* Need to make a copy of the buffer so we don't clobber it while converting */
  935. tmp = (Uint8 *)SDL_malloc(2*UVheight*srcUVPitch);
  936. if (!tmp) {
  937. return SDL_OutOfMemory();
  938. }
  939. SDL_memcpy(tmp, src, 2*UVheight*srcUVPitch);
  940. src = tmp;
  941. }
  942. if (reverseUV) {
  943. src2 = (const Uint8 *)src;
  944. src1 = src2 + UVheight * srcUVPitch;
  945. } else {
  946. src1 = (const Uint8 *)src;
  947. src2 = src1 + UVheight * srcUVPitch;
  948. }
  949. dstUV = (Uint8 *)dst;
  950. y = UVheight;
  951. while (y--) {
  952. x = UVwidth;
  953. #ifdef __SSE2__
  954. if (use_SSE2) {
  955. while (x >= 16) {
  956. __m128i u = _mm_loadu_si128((__m128i *)src1);
  957. __m128i v = _mm_loadu_si128((__m128i *)src2);
  958. __m128i uv1 = _mm_unpacklo_epi8(u, v);
  959. __m128i uv2 = _mm_unpackhi_epi8(u, v);
  960. _mm_storeu_si128((__m128i*)dstUV, uv1);
  961. _mm_storeu_si128((__m128i*)(dstUV + 16), uv2);
  962. src1 += 16;
  963. src2 += 16;
  964. dstUV += 32;
  965. x -= 16;
  966. }
  967. }
  968. #endif
  969. while (x--) {
  970. *dstUV++ = *src1++;
  971. *dstUV++ = *src2++;
  972. }
  973. src1 += srcUVPitchLeft;
  974. src2 += srcUVPitchLeft;
  975. dstUV += dstUVPitchLeft;
  976. }
  977. if (tmp) {
  978. SDL_free(tmp);
  979. }
  980. return 0;
  981. }
  982. static int
  983. SDL_ConvertPixels_SplitNV_to_UVPlanes(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch, SDL_bool reverseUV)
  984. {
  985. int x, y;
  986. const int UVwidth = (width + 1)/2;
  987. const int UVheight = (height + 1)/2;
  988. const int srcUVPitch = ((src_pitch + 1)/2)*2;
  989. const int srcUVPitchLeft = srcUVPitch - UVwidth*2;
  990. const int dstUVPitch = ((dst_pitch + 1)/2);
  991. const int dstUVPitchLeft = dstUVPitch - UVwidth;
  992. const Uint8 *srcUV;
  993. Uint8 *dst1, *dst2;
  994. Uint8 *tmp = NULL;
  995. #ifdef __SSE2__
  996. const SDL_bool use_SSE2 = SDL_HasSSE2();
  997. #endif
  998. /* Skip the Y plane */
  999. src = (const Uint8 *)src + height * src_pitch;
  1000. dst = (Uint8 *)dst + height * dst_pitch;
  1001. if (src == dst) {
  1002. /* Need to make a copy of the buffer so we don't clobber it while converting */
  1003. tmp = (Uint8 *)SDL_malloc(UVheight*srcUVPitch);
  1004. if (!tmp) {
  1005. return SDL_OutOfMemory();
  1006. }
  1007. SDL_memcpy(tmp, src, UVheight*srcUVPitch);
  1008. src = tmp;
  1009. }
  1010. if (reverseUV) {
  1011. dst2 = (Uint8 *)dst;
  1012. dst1 = dst2 + UVheight * dstUVPitch;
  1013. } else {
  1014. dst1 = (Uint8 *)dst;
  1015. dst2 = dst1 + UVheight * dstUVPitch;
  1016. }
  1017. srcUV = (const Uint8 *)src;
  1018. y = UVheight;
  1019. while (y--) {
  1020. x = UVwidth;
  1021. #ifdef __SSE2__
  1022. if (use_SSE2) {
  1023. __m128i mask = _mm_set1_epi16(0x00FF);
  1024. while (x >= 16) {
  1025. __m128i uv1 = _mm_loadu_si128((__m128i*)srcUV);
  1026. __m128i uv2 = _mm_loadu_si128((__m128i*)(srcUV+16));
  1027. __m128i u1 = _mm_and_si128(uv1, mask);
  1028. __m128i u2 = _mm_and_si128(uv2, mask);
  1029. __m128i u = _mm_packus_epi16(u1, u2);
  1030. __m128i v1 = _mm_srli_epi16(uv1, 8);
  1031. __m128i v2 = _mm_srli_epi16(uv2, 8);
  1032. __m128i v = _mm_packus_epi16(v1, v2);
  1033. _mm_storeu_si128((__m128i*)dst1, u);
  1034. _mm_storeu_si128((__m128i*)dst2, v);
  1035. srcUV += 32;
  1036. dst1 += 16;
  1037. dst2 += 16;
  1038. x -= 16;
  1039. }
  1040. }
  1041. #endif
  1042. while (x--) {
  1043. *dst1++ = *srcUV++;
  1044. *dst2++ = *srcUV++;
  1045. }
  1046. srcUV += srcUVPitchLeft;
  1047. dst1 += dstUVPitchLeft;
  1048. dst2 += dstUVPitchLeft;
  1049. }
  1050. if (tmp) {
  1051. SDL_free(tmp);
  1052. }
  1053. return 0;
  1054. }
  1055. static int
  1056. SDL_ConvertPixels_SwapNV(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1057. {
  1058. int x, y;
  1059. const int UVwidth = (width + 1)/2;
  1060. const int UVheight = (height + 1)/2;
  1061. const int srcUVPitch = ((src_pitch + 1)/2)*2;
  1062. const int srcUVPitchLeft = (srcUVPitch - UVwidth*2)/sizeof(Uint16);
  1063. const int dstUVPitch = ((dst_pitch + 1)/2)*2;
  1064. const int dstUVPitchLeft = (dstUVPitch - UVwidth*2)/sizeof(Uint16);
  1065. const Uint16 *srcUV;
  1066. Uint16 *dstUV;
  1067. #ifdef __SSE2__
  1068. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1069. #endif
  1070. /* Skip the Y plane */
  1071. src = (const Uint8 *)src + height * src_pitch;
  1072. dst = (Uint8 *)dst + height * dst_pitch;
  1073. srcUV = (const Uint16 *)src;
  1074. dstUV = (Uint16 *)dst;
  1075. y = UVheight;
  1076. while (y--) {
  1077. x = UVwidth;
  1078. #ifdef __SSE2__
  1079. if (use_SSE2) {
  1080. while (x >= 8) {
  1081. __m128i uv = _mm_loadu_si128((__m128i*)srcUV);
  1082. __m128i v = _mm_slli_epi16(uv, 8);
  1083. __m128i u = _mm_srli_epi16(uv, 8);
  1084. __m128i vu = _mm_or_si128(v, u);
  1085. _mm_storeu_si128((__m128i*)dstUV, vu);
  1086. srcUV += 8;
  1087. dstUV += 8;
  1088. x -= 8;
  1089. }
  1090. }
  1091. #endif
  1092. while (x--) {
  1093. *dstUV++ = SDL_Swap16(*srcUV++);
  1094. }
  1095. srcUV += srcUVPitchLeft;
  1096. dstUV += dstUVPitchLeft;
  1097. }
  1098. return 0;
  1099. }
  1100. static int
  1101. SDL_ConvertPixels_Planar2x2_to_Planar2x2(int width, int height,
  1102. Uint32 src_format, const void *src, int src_pitch,
  1103. Uint32 dst_format, void *dst, int dst_pitch)
  1104. {
  1105. if (src != dst) {
  1106. /* Copy Y plane */
  1107. int i;
  1108. const Uint8 *srcY = (const Uint8 *)src;
  1109. Uint8 *dstY = (Uint8 *)dst;
  1110. for (i = height; i--; ) {
  1111. SDL_memcpy(dstY, srcY, width);
  1112. srcY += src_pitch;
  1113. dstY += dst_pitch;
  1114. }
  1115. }
  1116. switch (src_format) {
  1117. case SDL_PIXELFORMAT_YV12:
  1118. switch (dst_format) {
  1119. case SDL_PIXELFORMAT_IYUV:
  1120. return SDL_ConvertPixels_SwapUVPlanes(width, height, src, src_pitch, dst, dst_pitch);
  1121. case SDL_PIXELFORMAT_NV12:
  1122. return SDL_ConvertPixels_PackUVPlanes_to_NV(width, height, src, src_pitch, dst, dst_pitch, SDL_TRUE);
  1123. case SDL_PIXELFORMAT_NV21:
  1124. return SDL_ConvertPixels_PackUVPlanes_to_NV(width, height, src, src_pitch, dst, dst_pitch, SDL_FALSE);
  1125. default:
  1126. break;
  1127. }
  1128. break;
  1129. case SDL_PIXELFORMAT_IYUV:
  1130. switch (dst_format) {
  1131. case SDL_PIXELFORMAT_YV12:
  1132. return SDL_ConvertPixels_SwapUVPlanes(width, height, src, src_pitch, dst, dst_pitch);
  1133. case SDL_PIXELFORMAT_NV12:
  1134. return SDL_ConvertPixels_PackUVPlanes_to_NV(width, height, src, src_pitch, dst, dst_pitch, SDL_FALSE);
  1135. case SDL_PIXELFORMAT_NV21:
  1136. return SDL_ConvertPixels_PackUVPlanes_to_NV(width, height, src, src_pitch, dst, dst_pitch, SDL_TRUE);
  1137. default:
  1138. break;
  1139. }
  1140. break;
  1141. case SDL_PIXELFORMAT_NV12:
  1142. switch (dst_format) {
  1143. case SDL_PIXELFORMAT_YV12:
  1144. return SDL_ConvertPixels_SplitNV_to_UVPlanes(width, height, src, src_pitch, dst, dst_pitch, SDL_TRUE);
  1145. case SDL_PIXELFORMAT_IYUV:
  1146. return SDL_ConvertPixels_SplitNV_to_UVPlanes(width, height, src, src_pitch, dst, dst_pitch, SDL_FALSE);
  1147. case SDL_PIXELFORMAT_NV21:
  1148. return SDL_ConvertPixels_SwapNV(width, height, src, src_pitch, dst, dst_pitch);
  1149. default:
  1150. break;
  1151. }
  1152. break;
  1153. case SDL_PIXELFORMAT_NV21:
  1154. switch (dst_format) {
  1155. case SDL_PIXELFORMAT_YV12:
  1156. return SDL_ConvertPixels_SplitNV_to_UVPlanes(width, height, src, src_pitch, dst, dst_pitch, SDL_FALSE);
  1157. case SDL_PIXELFORMAT_IYUV:
  1158. return SDL_ConvertPixels_SplitNV_to_UVPlanes(width, height, src, src_pitch, dst, dst_pitch, SDL_TRUE);
  1159. case SDL_PIXELFORMAT_NV12:
  1160. return SDL_ConvertPixels_SwapNV(width, height, src, src_pitch, dst, dst_pitch);
  1161. default:
  1162. break;
  1163. }
  1164. break;
  1165. default:
  1166. break;
  1167. }
  1168. return SDL_SetError("SDL_ConvertPixels_Planar2x2_to_Planar2x2: Unsupported YUV conversion: %s -> %s", SDL_GetPixelFormatName(src_format), SDL_GetPixelFormatName(dst_format));
  1169. }
  1170. #ifdef __SSE2__
  1171. #define PACKED4_TO_PACKED4_ROW_SSE2(shuffle) \
  1172. while (x >= 4) { \
  1173. __m128i yuv = _mm_loadu_si128((__m128i*)srcYUV); \
  1174. __m128i lo = _mm_unpacklo_epi8(yuv, _mm_setzero_si128()); \
  1175. __m128i hi = _mm_unpackhi_epi8(yuv, _mm_setzero_si128()); \
  1176. lo = _mm_shufflelo_epi16(lo, shuffle); \
  1177. lo = _mm_shufflehi_epi16(lo, shuffle); \
  1178. hi = _mm_shufflelo_epi16(hi, shuffle); \
  1179. hi = _mm_shufflehi_epi16(hi, shuffle); \
  1180. yuv = _mm_packus_epi16(lo, hi); \
  1181. _mm_storeu_si128((__m128i*)dstYUV, yuv); \
  1182. srcYUV += 16; \
  1183. dstYUV += 16; \
  1184. x -= 4; \
  1185. } \
  1186. #endif
  1187. static int
  1188. SDL_ConvertPixels_YUY2_to_UYVY(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1189. {
  1190. int x, y;
  1191. const int YUVwidth = (width + 1)/2;
  1192. const int srcYUVPitchLeft = (src_pitch - YUVwidth*4);
  1193. const int dstYUVPitchLeft = (dst_pitch - YUVwidth*4);
  1194. const Uint8 *srcYUV = (const Uint8 *)src;
  1195. Uint8 *dstYUV = (Uint8 *)dst;
  1196. #ifdef __SSE2__
  1197. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1198. #endif
  1199. y = height;
  1200. while (y--) {
  1201. x = YUVwidth;
  1202. #ifdef __SSE2__
  1203. if (use_SSE2) {
  1204. PACKED4_TO_PACKED4_ROW_SSE2(_MM_SHUFFLE(2, 3, 0, 1));
  1205. }
  1206. #endif
  1207. while (x--) {
  1208. Uint8 Y1, U, Y2, V;
  1209. Y1 = srcYUV[0];
  1210. U = srcYUV[1];
  1211. Y2 = srcYUV[2];
  1212. V = srcYUV[3];
  1213. srcYUV += 4;
  1214. dstYUV[0] = U;
  1215. dstYUV[1] = Y1;
  1216. dstYUV[2] = V;
  1217. dstYUV[3] = Y2;
  1218. dstYUV += 4;
  1219. }
  1220. srcYUV += srcYUVPitchLeft;
  1221. dstYUV += dstYUVPitchLeft;
  1222. }
  1223. return 0;
  1224. }
  1225. static int
  1226. SDL_ConvertPixels_YUY2_to_YVYU(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1227. {
  1228. int x, y;
  1229. const int YUVwidth = (width + 1)/2;
  1230. const int srcYUVPitchLeft = (src_pitch - YUVwidth*4);
  1231. const int dstYUVPitchLeft = (dst_pitch - YUVwidth*4);
  1232. const Uint8 *srcYUV = (const Uint8 *)src;
  1233. Uint8 *dstYUV = (Uint8 *)dst;
  1234. #ifdef __SSE2__
  1235. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1236. #endif
  1237. y = height;
  1238. while (y--) {
  1239. x = YUVwidth;
  1240. #ifdef __SSE2__
  1241. if (use_SSE2) {
  1242. PACKED4_TO_PACKED4_ROW_SSE2(_MM_SHUFFLE(1, 2, 3, 0));
  1243. }
  1244. #endif
  1245. while (x--) {
  1246. Uint8 Y1, U, Y2, V;
  1247. Y1 = srcYUV[0];
  1248. U = srcYUV[1];
  1249. Y2 = srcYUV[2];
  1250. V = srcYUV[3];
  1251. srcYUV += 4;
  1252. dstYUV[0] = Y1;
  1253. dstYUV[1] = V;
  1254. dstYUV[2] = Y2;
  1255. dstYUV[3] = U;
  1256. dstYUV += 4;
  1257. }
  1258. srcYUV += srcYUVPitchLeft;
  1259. dstYUV += dstYUVPitchLeft;
  1260. }
  1261. return 0;
  1262. }
  1263. static int
  1264. SDL_ConvertPixels_UYVY_to_YUY2(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1265. {
  1266. int x, y;
  1267. const int YUVwidth = (width + 1)/2;
  1268. const int srcYUVPitchLeft = (src_pitch - YUVwidth*4);
  1269. const int dstYUVPitchLeft = (dst_pitch - YUVwidth*4);
  1270. const Uint8 *srcYUV = (const Uint8 *)src;
  1271. Uint8 *dstYUV = (Uint8 *)dst;
  1272. #ifdef __SSE2__
  1273. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1274. #endif
  1275. y = height;
  1276. while (y--) {
  1277. x = YUVwidth;
  1278. #ifdef __SSE2__
  1279. if (use_SSE2) {
  1280. PACKED4_TO_PACKED4_ROW_SSE2(_MM_SHUFFLE(2, 3, 0, 1));
  1281. }
  1282. #endif
  1283. while (x--) {
  1284. Uint8 Y1, U, Y2, V;
  1285. U = srcYUV[0];
  1286. Y1 = srcYUV[1];
  1287. V = srcYUV[2];
  1288. Y2 = srcYUV[3];
  1289. srcYUV += 4;
  1290. dstYUV[0] = Y1;
  1291. dstYUV[1] = U;
  1292. dstYUV[2] = Y2;
  1293. dstYUV[3] = V;
  1294. dstYUV += 4;
  1295. }
  1296. srcYUV += srcYUVPitchLeft;
  1297. dstYUV += dstYUVPitchLeft;
  1298. }
  1299. return 0;
  1300. }
  1301. static int
  1302. SDL_ConvertPixels_UYVY_to_YVYU(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1303. {
  1304. int x, y;
  1305. const int YUVwidth = (width + 1)/2;
  1306. const int srcYUVPitchLeft = (src_pitch - YUVwidth*4);
  1307. const int dstYUVPitchLeft = (dst_pitch - YUVwidth*4);
  1308. const Uint8 *srcYUV = (const Uint8 *)src;
  1309. Uint8 *dstYUV = (Uint8 *)dst;
  1310. #ifdef __SSE2__
  1311. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1312. #endif
  1313. y = height;
  1314. while (y--) {
  1315. x = YUVwidth;
  1316. #ifdef __SSE2__
  1317. if (use_SSE2) {
  1318. PACKED4_TO_PACKED4_ROW_SSE2(_MM_SHUFFLE(0, 3, 2, 1));
  1319. }
  1320. #endif
  1321. while (x--) {
  1322. Uint8 Y1, U, Y2, V;
  1323. U = srcYUV[0];
  1324. Y1 = srcYUV[1];
  1325. V = srcYUV[2];
  1326. Y2 = srcYUV[3];
  1327. srcYUV += 4;
  1328. dstYUV[0] = Y1;
  1329. dstYUV[1] = V;
  1330. dstYUV[2] = Y2;
  1331. dstYUV[3] = U;
  1332. dstYUV += 4;
  1333. }
  1334. srcYUV += srcYUVPitchLeft;
  1335. dstYUV += dstYUVPitchLeft;
  1336. }
  1337. return 0;
  1338. }
  1339. static int
  1340. SDL_ConvertPixels_YVYU_to_YUY2(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1341. {
  1342. int x, y;
  1343. const int YUVwidth = (width + 1)/2;
  1344. const int srcYUVPitchLeft = (src_pitch - YUVwidth*4);
  1345. const int dstYUVPitchLeft = (dst_pitch - YUVwidth*4);
  1346. const Uint8 *srcYUV = (const Uint8 *)src;
  1347. Uint8 *dstYUV = (Uint8 *)dst;
  1348. #ifdef __SSE2__
  1349. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1350. #endif
  1351. y = height;
  1352. while (y--) {
  1353. x = YUVwidth;
  1354. #ifdef __SSE2__
  1355. if (use_SSE2) {
  1356. PACKED4_TO_PACKED4_ROW_SSE2(_MM_SHUFFLE(1, 2, 3, 0));
  1357. }
  1358. #endif
  1359. while (x--) {
  1360. Uint8 Y1, U, Y2, V;
  1361. Y1 = srcYUV[0];
  1362. V = srcYUV[1];
  1363. Y2 = srcYUV[2];
  1364. U = srcYUV[3];
  1365. srcYUV += 4;
  1366. dstYUV[0] = Y1;
  1367. dstYUV[1] = U;
  1368. dstYUV[2] = Y2;
  1369. dstYUV[3] = V;
  1370. dstYUV += 4;
  1371. }
  1372. srcYUV += srcYUVPitchLeft;
  1373. dstYUV += dstYUVPitchLeft;
  1374. }
  1375. return 0;
  1376. }
  1377. static int
  1378. SDL_ConvertPixels_YVYU_to_UYVY(int width, int height, const void *src, int src_pitch, void *dst, int dst_pitch)
  1379. {
  1380. int x, y;
  1381. const int YUVwidth = (width + 1)/2;
  1382. const int srcYUVPitchLeft = (src_pitch - YUVwidth*4);
  1383. const int dstYUVPitchLeft = (dst_pitch - YUVwidth*4);
  1384. const Uint8 *srcYUV = (const Uint8 *)src;
  1385. Uint8 *dstYUV = (Uint8 *)dst;
  1386. #ifdef __SSE2__
  1387. const SDL_bool use_SSE2 = SDL_HasSSE2();
  1388. #endif
  1389. y = height;
  1390. while (y--) {
  1391. x = YUVwidth;
  1392. #ifdef __SSE2__
  1393. if (use_SSE2) {
  1394. PACKED4_TO_PACKED4_ROW_SSE2(_MM_SHUFFLE(2, 1, 0, 3));
  1395. }
  1396. #endif
  1397. while (x--) {
  1398. Uint8 Y1, U, Y2, V;
  1399. Y1 = srcYUV[0];
  1400. V = srcYUV[1];
  1401. Y2 = srcYUV[2];
  1402. U = srcYUV[3];
  1403. srcYUV += 4;
  1404. dstYUV[0] = U;
  1405. dstYUV[1] = Y1;
  1406. dstYUV[2] = V;
  1407. dstYUV[3] = Y2;
  1408. dstYUV += 4;
  1409. }
  1410. srcYUV += srcYUVPitchLeft;
  1411. dstYUV += dstYUVPitchLeft;
  1412. }
  1413. return 0;
  1414. }
  1415. static int
  1416. SDL_ConvertPixels_Packed4_to_Packed4(int width, int height,
  1417. Uint32 src_format, const void *src, int src_pitch,
  1418. Uint32 dst_format, void *dst, int dst_pitch)
  1419. {
  1420. switch (src_format) {
  1421. case SDL_PIXELFORMAT_YUY2:
  1422. switch (dst_format) {
  1423. case SDL_PIXELFORMAT_UYVY:
  1424. return SDL_ConvertPixels_YUY2_to_UYVY(width, height, src, src_pitch, dst, dst_pitch);
  1425. case SDL_PIXELFORMAT_YVYU:
  1426. return SDL_ConvertPixels_YUY2_to_YVYU(width, height, src, src_pitch, dst, dst_pitch);
  1427. default:
  1428. break;
  1429. }
  1430. break;
  1431. case SDL_PIXELFORMAT_UYVY:
  1432. switch (dst_format) {
  1433. case SDL_PIXELFORMAT_YUY2:
  1434. return SDL_ConvertPixels_UYVY_to_YUY2(width, height, src, src_pitch, dst, dst_pitch);
  1435. case SDL_PIXELFORMAT_YVYU:
  1436. return SDL_ConvertPixels_UYVY_to_YVYU(width, height, src, src_pitch, dst, dst_pitch);
  1437. default:
  1438. break;
  1439. }
  1440. break;
  1441. case SDL_PIXELFORMAT_YVYU:
  1442. switch (dst_format) {
  1443. case SDL_PIXELFORMAT_YUY2:
  1444. return SDL_ConvertPixels_YVYU_to_YUY2(width, height, src, src_pitch, dst, dst_pitch);
  1445. case SDL_PIXELFORMAT_UYVY:
  1446. return SDL_ConvertPixels_YVYU_to_UYVY(width, height, src, src_pitch, dst, dst_pitch);
  1447. default:
  1448. break;
  1449. }
  1450. break;
  1451. default:
  1452. break;
  1453. }
  1454. return SDL_SetError("SDL_ConvertPixels_Packed4_to_Packed4: Unsupported YUV conversion: %s -> %s", SDL_GetPixelFormatName(src_format), SDL_GetPixelFormatName(dst_format));
  1455. }
  1456. static int
  1457. SDL_ConvertPixels_Planar2x2_to_Packed4(int width, int height,
  1458. Uint32 src_format, const void *src, int src_pitch,
  1459. Uint32 dst_format, void *dst, int dst_pitch)
  1460. {
  1461. int x, y;
  1462. const Uint8 *srcY1, *srcY2, *srcU, *srcV;
  1463. Uint32 srcY_pitch, srcUV_pitch;
  1464. Uint32 srcY_pitch_left, srcUV_pitch_left, srcUV_pixel_stride;
  1465. Uint8 *dstY1, *dstY2, *dstU1, *dstU2, *dstV1, *dstV2;
  1466. Uint32 dstY_pitch, dstUV_pitch;
  1467. Uint32 dst_pitch_left;
  1468. if (src == dst) {
  1469. return SDL_SetError("Can't change YUV plane types in-place");
  1470. }
  1471. if (GetYUVPlanes(width, height, src_format, src, src_pitch,
  1472. &srcY1, &srcU, &srcV, &srcY_pitch, &srcUV_pitch) < 0) {
  1473. return -1;
  1474. }
  1475. srcY2 = srcY1 + srcY_pitch;
  1476. srcY_pitch_left = (srcY_pitch - width);
  1477. if (src_format == SDL_PIXELFORMAT_NV12 || src_format == SDL_PIXELFORMAT_NV21) {
  1478. srcUV_pixel_stride = 2;
  1479. srcUV_pitch_left = (srcUV_pitch - 2*((width + 1)/2));
  1480. } else {
  1481. srcUV_pixel_stride = 1;
  1482. srcUV_pitch_left = (srcUV_pitch - ((width + 1)/2));
  1483. }
  1484. if (GetYUVPlanes(width, height, dst_format, dst, dst_pitch,
  1485. (const Uint8 **)&dstY1, (const Uint8 **)&dstU1, (const Uint8 **)&dstV1,
  1486. &dstY_pitch, &dstUV_pitch) < 0) {
  1487. return -1;
  1488. }
  1489. dstY2 = dstY1 + dstY_pitch;
  1490. dstU2 = dstU1 + dstUV_pitch;
  1491. dstV2 = dstV1 + dstUV_pitch;
  1492. dst_pitch_left = (dstY_pitch - 4*((width + 1)/2));
  1493. /* Copy 2x2 blocks of pixels at a time */
  1494. for (y = 0; y < (height - 1); y += 2) {
  1495. for (x = 0; x < (width - 1); x += 2) {
  1496. /* Row 1 */
  1497. *dstY1 = *srcY1++;
  1498. dstY1 += 2;
  1499. *dstY1 = *srcY1++;
  1500. dstY1 += 2;
  1501. *dstU1 = *srcU;
  1502. *dstV1 = *srcV;
  1503. /* Row 2 */
  1504. *dstY2 = *srcY2++;
  1505. dstY2 += 2;
  1506. *dstY2 = *srcY2++;
  1507. dstY2 += 2;
  1508. *dstU2 = *srcU;
  1509. *dstV2 = *srcV;
  1510. srcU += srcUV_pixel_stride;
  1511. srcV += srcUV_pixel_stride;
  1512. dstU1 += 4;
  1513. dstU2 += 4;
  1514. dstV1 += 4;
  1515. dstV2 += 4;
  1516. }
  1517. /* Last column */
  1518. if (x == (width - 1)) {
  1519. /* Row 1 */
  1520. *dstY1 = *srcY1;
  1521. dstY1 += 2;
  1522. *dstY1 = *srcY1++;
  1523. dstY1 += 2;
  1524. *dstU1 = *srcU;
  1525. *dstV1 = *srcV;
  1526. /* Row 2 */
  1527. *dstY2 = *srcY2;
  1528. dstY2 += 2;
  1529. *dstY2 = *srcY2++;
  1530. dstY2 += 2;
  1531. *dstU2 = *srcU;
  1532. *dstV2 = *srcV;
  1533. srcU += srcUV_pixel_stride;
  1534. srcV += srcUV_pixel_stride;
  1535. dstU1 += 4;
  1536. dstU2 += 4;
  1537. dstV1 += 4;
  1538. dstV2 += 4;
  1539. }
  1540. srcY1 += srcY_pitch_left + srcY_pitch;
  1541. srcY2 += srcY_pitch_left + srcY_pitch;
  1542. srcU += srcUV_pitch_left;
  1543. srcV += srcUV_pitch_left;
  1544. dstY1 += dst_pitch_left + dstY_pitch;
  1545. dstY2 += dst_pitch_left + dstY_pitch;
  1546. dstU1 += dst_pitch_left + dstUV_pitch;
  1547. dstU2 += dst_pitch_left + dstUV_pitch;
  1548. dstV1 += dst_pitch_left + dstUV_pitch;
  1549. dstV2 += dst_pitch_left + dstUV_pitch;
  1550. }
  1551. /* Last row */
  1552. if (y == (height - 1)) {
  1553. for (x = 0; x < (width - 1); x += 2) {
  1554. /* Row 1 */
  1555. *dstY1 = *srcY1++;
  1556. dstY1 += 2;
  1557. *dstY1 = *srcY1++;
  1558. dstY1 += 2;
  1559. *dstU1 = *srcU;
  1560. *dstV1 = *srcV;
  1561. srcU += srcUV_pixel_stride;
  1562. srcV += srcUV_pixel_stride;
  1563. dstU1 += 4;
  1564. dstV1 += 4;
  1565. }
  1566. /* Last column */
  1567. if (x == (width - 1)) {
  1568. /* Row 1 */
  1569. *dstY1 = *srcY1;
  1570. dstY1 += 2;
  1571. *dstY1 = *srcY1++;
  1572. dstY1 += 2;
  1573. *dstU1 = *srcU;
  1574. *dstV1 = *srcV;
  1575. srcU += srcUV_pixel_stride;
  1576. srcV += srcUV_pixel_stride;
  1577. dstU1 += 4;
  1578. dstV1 += 4;
  1579. }
  1580. }
  1581. return 0;
  1582. }
  1583. static int
  1584. SDL_ConvertPixels_Packed4_to_Planar2x2(int width, int height,
  1585. Uint32 src_format, const void *src, int src_pitch,
  1586. Uint32 dst_format, void *dst, int dst_pitch)
  1587. {
  1588. int x, y;
  1589. const Uint8 *srcY1, *srcY2, *srcU1, *srcU2, *srcV1, *srcV2;
  1590. Uint32 srcY_pitch, srcUV_pitch;
  1591. Uint32 src_pitch_left;
  1592. Uint8 *dstY1, *dstY2, *dstU, *dstV;
  1593. Uint32 dstY_pitch, dstUV_pitch;
  1594. Uint32 dstY_pitch_left, dstUV_pitch_left, dstUV_pixel_stride;
  1595. if (src == dst) {
  1596. return SDL_SetError("Can't change YUV plane types in-place");
  1597. }
  1598. if (GetYUVPlanes(width, height, src_format, src, src_pitch,
  1599. &srcY1, &srcU1, &srcV1, &srcY_pitch, &srcUV_pitch) < 0) {
  1600. return -1;
  1601. }
  1602. srcY2 = srcY1 + srcY_pitch;
  1603. srcU2 = srcU1 + srcUV_pitch;
  1604. srcV2 = srcV1 + srcUV_pitch;
  1605. src_pitch_left = (srcY_pitch - 4*((width + 1)/2));
  1606. if (GetYUVPlanes(width, height, dst_format, dst, dst_pitch,
  1607. (const Uint8 **)&dstY1, (const Uint8 **)&dstU, (const Uint8 **)&dstV,
  1608. &dstY_pitch, &dstUV_pitch) < 0) {
  1609. return -1;
  1610. }
  1611. dstY2 = dstY1 + dstY_pitch;
  1612. dstY_pitch_left = (dstY_pitch - width);
  1613. if (dst_format == SDL_PIXELFORMAT_NV12 || dst_format == SDL_PIXELFORMAT_NV21) {
  1614. dstUV_pixel_stride = 2;
  1615. dstUV_pitch_left = (dstUV_pitch - 2*((width + 1)/2));
  1616. } else {
  1617. dstUV_pixel_stride = 1;
  1618. dstUV_pitch_left = (dstUV_pitch - ((width + 1)/2));
  1619. }
  1620. /* Copy 2x2 blocks of pixels at a time */
  1621. for (y = 0; y < (height - 1); y += 2) {
  1622. for (x = 0; x < (width - 1); x += 2) {
  1623. /* Row 1 */
  1624. *dstY1++ = *srcY1;
  1625. srcY1 += 2;
  1626. *dstY1++ = *srcY1;
  1627. srcY1 += 2;
  1628. /* Row 2 */
  1629. *dstY2++ = *srcY2;
  1630. srcY2 += 2;
  1631. *dstY2++ = *srcY2;
  1632. srcY2 += 2;
  1633. *dstU = (Uint8)(((Uint32)*srcU1 + *srcU2)/2);
  1634. *dstV = (Uint8)(((Uint32)*srcV1 + *srcV2)/2);
  1635. srcU1 += 4;
  1636. srcU2 += 4;
  1637. srcV1 += 4;
  1638. srcV2 += 4;
  1639. dstU += dstUV_pixel_stride;
  1640. dstV += dstUV_pixel_stride;
  1641. }
  1642. /* Last column */
  1643. if (x == (width - 1)) {
  1644. /* Row 1 */
  1645. *dstY1 = *srcY1;
  1646. srcY1 += 2;
  1647. *dstY1++ = *srcY1;
  1648. srcY1 += 2;
  1649. /* Row 2 */
  1650. *dstY2 = *srcY2;
  1651. srcY2 += 2;
  1652. *dstY2++ = *srcY2;
  1653. srcY2 += 2;
  1654. *dstU = (Uint8)(((Uint32)*srcU1 + *srcU2)/2);
  1655. *dstV = (Uint8)(((Uint32)*srcV1 + *srcV2)/2);
  1656. srcU1 += 4;
  1657. srcU2 += 4;
  1658. srcV1 += 4;
  1659. srcV2 += 4;
  1660. dstU += dstUV_pixel_stride;
  1661. dstV += dstUV_pixel_stride;
  1662. }
  1663. srcY1 += src_pitch_left + srcY_pitch;
  1664. srcY2 += src_pitch_left + srcY_pitch;
  1665. srcU1 += src_pitch_left + srcUV_pitch;
  1666. srcU2 += src_pitch_left + srcUV_pitch;
  1667. srcV1 += src_pitch_left + srcUV_pitch;
  1668. srcV2 += src_pitch_left + srcUV_pitch;
  1669. dstY1 += dstY_pitch_left + dstY_pitch;
  1670. dstY2 += dstY_pitch_left + dstY_pitch;
  1671. dstU += dstUV_pitch_left;
  1672. dstV += dstUV_pitch_left;
  1673. }
  1674. /* Last row */
  1675. if (y == (height - 1)) {
  1676. for (x = 0; x < (width - 1); x += 2) {
  1677. *dstY1++ = *srcY1;
  1678. srcY1 += 2;
  1679. *dstY1++ = *srcY1;
  1680. srcY1 += 2;
  1681. *dstU = *srcU1;
  1682. *dstV = *srcV1;
  1683. srcU1 += 4;
  1684. srcV1 += 4;
  1685. dstU += dstUV_pixel_stride;
  1686. dstV += dstUV_pixel_stride;
  1687. }
  1688. /* Last column */
  1689. if (x == (width - 1)) {
  1690. *dstY1 = *srcY1;
  1691. *dstU = *srcU1;
  1692. *dstV = *srcV1;
  1693. }
  1694. }
  1695. return 0;
  1696. }
  1697. #endif /* SDL_HAVE_YUV */
  1698. int
  1699. SDL_ConvertPixels_YUV_to_YUV(int width, int height,
  1700. Uint32 src_format, const void *src, int src_pitch,
  1701. Uint32 dst_format, void *dst, int dst_pitch)
  1702. {
  1703. #if SDL_HAVE_YUV
  1704. if (src_format == dst_format) {
  1705. if (src == dst) {
  1706. /* Nothing to do */
  1707. return 0;
  1708. }
  1709. return SDL_ConvertPixels_YUV_to_YUV_Copy(width, height, src_format, src, src_pitch, dst, dst_pitch);
  1710. }
  1711. if (IsPlanar2x2Format(src_format) && IsPlanar2x2Format(dst_format)) {
  1712. return SDL_ConvertPixels_Planar2x2_to_Planar2x2(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1713. } else if (IsPacked4Format(src_format) && IsPacked4Format(dst_format)) {
  1714. return SDL_ConvertPixels_Packed4_to_Packed4(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1715. } else if (IsPlanar2x2Format(src_format) && IsPacked4Format(dst_format)) {
  1716. return SDL_ConvertPixels_Planar2x2_to_Packed4(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1717. } else if (IsPacked4Format(src_format) && IsPlanar2x2Format(dst_format)) {
  1718. return SDL_ConvertPixels_Packed4_to_Planar2x2(width, height, src_format, src, src_pitch, dst_format, dst, dst_pitch);
  1719. } else {
  1720. return SDL_SetError("SDL_ConvertPixels_YUV_to_YUV: Unsupported YUV conversion: %s -> %s", SDL_GetPixelFormatName(src_format), SDL_GetPixelFormatName(dst_format));
  1721. }
  1722. #else
  1723. return SDL_SetError("SDL not built with YUV support");
  1724. #endif
  1725. }
  1726. /* vi: set ts=4 sw=4 expandtab: */