SDL_yuv.c 63 KB

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