SDL_yuv_sw.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2026 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. // This is the software implementation of the YUV texture support
  20. #ifdef SDL_HAVE_YUV
  21. #include "SDL_yuv_sw_c.h"
  22. #include "../video/SDL_surface_c.h"
  23. #include "../video/SDL_yuv_c.h"
  24. SDL_SW_YUVTexture *SDL_SW_CreateYUVTexture(SDL_PixelFormat format, SDL_Colorspace colorspace, int w, int h)
  25. {
  26. SDL_SW_YUVTexture *swdata;
  27. switch (format) {
  28. case SDL_PIXELFORMAT_YV12:
  29. case SDL_PIXELFORMAT_IYUV:
  30. case SDL_PIXELFORMAT_I444:
  31. case SDL_PIXELFORMAT_I0FL:
  32. case SDL_PIXELFORMAT_I4FL:
  33. case SDL_PIXELFORMAT_YUY2:
  34. case SDL_PIXELFORMAT_UYVY:
  35. case SDL_PIXELFORMAT_YVYU:
  36. case SDL_PIXELFORMAT_NV12:
  37. case SDL_PIXELFORMAT_NV21:
  38. break;
  39. default:
  40. SDL_SetError("Unsupported YUV format");
  41. return NULL;
  42. }
  43. swdata = (SDL_SW_YUVTexture *)SDL_calloc(1, sizeof(*swdata));
  44. if (!swdata) {
  45. return NULL;
  46. }
  47. swdata->format = format;
  48. swdata->colorspace = colorspace;
  49. swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
  50. swdata->w = w;
  51. swdata->h = h;
  52. {
  53. size_t dst_size;
  54. if (!SDL_CalculateYUVSize(format, w, h, &dst_size, NULL)) {
  55. SDL_SW_DestroyYUVTexture(swdata);
  56. return NULL;
  57. }
  58. swdata->pixels = (Uint8 *)SDL_aligned_alloc(SDL_GetSIMDAlignment(), dst_size);
  59. if (!swdata->pixels) {
  60. SDL_SW_DestroyYUVTexture(swdata);
  61. return NULL;
  62. }
  63. }
  64. // Find the pitch and offset values for the texture
  65. const int bpp = SDL_BYTESPERPIXEL(format);
  66. switch (format) {
  67. case SDL_PIXELFORMAT_YV12:
  68. case SDL_PIXELFORMAT_IYUV:
  69. case SDL_PIXELFORMAT_I0FL:
  70. swdata->pitches[0] = w * bpp;
  71. swdata->pitches[1] = (((swdata->pitches[0] / bpp) + 1) / 2) * bpp;
  72. swdata->pitches[2] = swdata->pitches[1];
  73. swdata->planes[0] = swdata->pixels;
  74. swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
  75. swdata->planes[2] = swdata->planes[1] + swdata->pitches[1] * ((h + 1) / 2);
  76. break;
  77. case SDL_PIXELFORMAT_I444:
  78. case SDL_PIXELFORMAT_I4FL:
  79. swdata->pitches[0] = w * bpp;
  80. swdata->pitches[1] = swdata->pitches[0];
  81. swdata->pitches[2] = swdata->pitches[1];
  82. swdata->planes[0] = swdata->pixels;
  83. swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
  84. swdata->planes[2] = swdata->planes[1] + swdata->pitches[1] * h;
  85. break;
  86. case SDL_PIXELFORMAT_YUY2:
  87. case SDL_PIXELFORMAT_UYVY:
  88. case SDL_PIXELFORMAT_YVYU:
  89. swdata->pitches[0] = ((w + 1) / 2) * 4;
  90. swdata->planes[0] = swdata->pixels;
  91. break;
  92. case SDL_PIXELFORMAT_NV12:
  93. case SDL_PIXELFORMAT_NV21:
  94. swdata->pitches[0] = w;
  95. swdata->pitches[1] = 2 * ((swdata->pitches[0] + 1) / 2);
  96. swdata->planes[0] = swdata->pixels;
  97. swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
  98. break;
  99. default:
  100. SDL_assert(!"We should never get here (caught above)");
  101. break;
  102. }
  103. // We're all done..
  104. return swdata;
  105. }
  106. bool SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture *swdata, void **pixels,
  107. int *pitch)
  108. {
  109. *pixels = swdata->planes[0];
  110. *pitch = swdata->pitches[0];
  111. return true;
  112. }
  113. bool SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect,
  114. const void *pixels, int pitch)
  115. {
  116. const int bpp = SDL_BYTESPERPIXEL(swdata->format);
  117. switch (swdata->format) {
  118. case SDL_PIXELFORMAT_YV12:
  119. case SDL_PIXELFORMAT_IYUV:
  120. case SDL_PIXELFORMAT_I0FL:
  121. if (rect->x == 0 && rect->y == 0 &&
  122. rect->w == swdata->w && rect->h == swdata->h && pitch == swdata->pitches[0]) {
  123. SDL_memcpy(swdata->pixels, pixels,
  124. (size_t)(swdata->h * swdata->w * bpp) + 2 * ((swdata->h + 1) / 2) * ((swdata->w + 1) / 2) * bpp);
  125. } else {
  126. Uint8 *src, *dst;
  127. int row;
  128. size_t length;
  129. const int UVpitch = ((pitch / bpp + 1) / 2) * bpp;
  130. // Copy the Y plane
  131. src = (Uint8 *)pixels;
  132. dst = swdata->pixels + rect->y * swdata->w * bpp + rect->x * bpp;
  133. length = rect->w * bpp;
  134. for (row = 0; row < rect->h; ++row) {
  135. SDL_memcpy(dst, src, length);
  136. src += pitch;
  137. dst += swdata->pitches[0];
  138. }
  139. // Copy the next plane
  140. src = (Uint8 *)pixels + rect->h * pitch;
  141. dst = swdata->pixels + swdata->h * swdata->pitches[0];
  142. dst += (rect->y / 2) * swdata->pitches[1] + (rect->x / 2) * bpp;
  143. length = ((rect->w + 1) / 2) * bpp;
  144. for (row = 0; row < (rect->h + 1) / 2; ++row) {
  145. SDL_memcpy(dst, src, length);
  146. src += UVpitch;
  147. dst += swdata->pitches[1];
  148. }
  149. // Copy the next plane
  150. src = (Uint8 *)pixels + rect->h * pitch + ((rect->h + 1) / 2) * UVpitch;
  151. dst = swdata->pixels + swdata->h * swdata->pitches[0] + ((rect->h + 1) / 2) * swdata->pitches[1];
  152. dst += (rect->y / 2) * swdata->pitches[2] + (rect->x / 2) * bpp;
  153. length = ((rect->w + 1) / 2) * bpp;
  154. for (row = 0; row < (rect->h + 1) / 2; ++row) {
  155. SDL_memcpy(dst, src, length);
  156. src += UVpitch;
  157. dst += swdata->pitches[2];
  158. }
  159. }
  160. break;
  161. case SDL_PIXELFORMAT_I444:
  162. case SDL_PIXELFORMAT_I4FL:
  163. if (rect->x == 0 && rect->y == 0 &&
  164. rect->w == swdata->w && rect->h == swdata->h && pitch == swdata->pitches[0]) {
  165. SDL_memcpy(swdata->pixels, pixels, (size_t)(swdata->h * pitch * 3));
  166. } else {
  167. Uint8 *src, *dst;
  168. int row;
  169. size_t length;
  170. // Copy the Y plane
  171. src = (Uint8 *)pixels;
  172. dst = swdata->pixels;
  173. dst += rect->y * swdata->pitches[0] + rect->x * bpp;
  174. length = rect->w * bpp;
  175. for (row = 0; row < rect->h; ++row) {
  176. SDL_memcpy(dst, src, length);
  177. src += pitch;
  178. dst += swdata->pitches[0];
  179. }
  180. // Copy the next plane
  181. dst = swdata->pixels + swdata->h * swdata->pitches[0];
  182. dst += rect->y * swdata->pitches[1] + rect->x * bpp;
  183. for (row = 0; row < rect->h; ++row) {
  184. SDL_memcpy(dst, src, length);
  185. src += pitch;
  186. dst += swdata->pitches[1];
  187. }
  188. // Copy the next plane
  189. dst = swdata->pixels + swdata->h * swdata->pitches[0] + swdata->h * swdata->pitches[1];
  190. dst += rect->y * swdata->pitches[2] + rect->x * bpp;
  191. for (row = 0; row < rect->h; ++row) {
  192. SDL_memcpy(dst, src, length);
  193. src += pitch;
  194. dst += swdata->pitches[2];
  195. }
  196. }
  197. break;
  198. case SDL_PIXELFORMAT_YUY2:
  199. case SDL_PIXELFORMAT_UYVY:
  200. case SDL_PIXELFORMAT_YVYU:
  201. {
  202. Uint8 *src, *dst;
  203. int row;
  204. size_t length;
  205. src = (Uint8 *)pixels;
  206. dst =
  207. swdata->planes[0] + rect->y * swdata->pitches[0] +
  208. rect->x * 2;
  209. length = 4 * (((size_t)rect->w + 1) / 2);
  210. for (row = 0; row < rect->h; ++row) {
  211. SDL_memcpy(dst, src, length);
  212. src += pitch;
  213. dst += swdata->pitches[0];
  214. }
  215. } break;
  216. case SDL_PIXELFORMAT_NV12:
  217. case SDL_PIXELFORMAT_NV21:
  218. {
  219. if (rect->x == 0 && rect->y == 0 && rect->w == swdata->w && rect->h == swdata->h) {
  220. SDL_memcpy(swdata->pixels, pixels,
  221. (size_t)(swdata->h * swdata->w) + 2 * ((swdata->h + 1) / 2) * ((swdata->w + 1) / 2));
  222. } else {
  223. Uint8 *src, *dst;
  224. int row;
  225. size_t length;
  226. // Copy the Y plane
  227. src = (Uint8 *)pixels;
  228. dst = swdata->pixels + rect->y * swdata->w + rect->x;
  229. length = rect->w;
  230. for (row = 0; row < rect->h; ++row) {
  231. SDL_memcpy(dst, src, length);
  232. src += pitch;
  233. dst += swdata->w;
  234. }
  235. // Copy the next plane
  236. src = (Uint8 *)pixels + rect->h * pitch;
  237. dst = swdata->pixels + swdata->h * swdata->w;
  238. dst += 2 * ((rect->y + 1) / 2) * ((swdata->w + 1) / 2) + 2 * (rect->x / 2);
  239. length = 2 * (((size_t)rect->w + 1) / 2);
  240. for (row = 0; row < (rect->h + 1) / 2; ++row) {
  241. SDL_memcpy(dst, src, length);
  242. src += 2 * ((pitch + 1) / 2);
  243. dst += 2 * ((swdata->w + 1) / 2);
  244. }
  245. }
  246. } break;
  247. default:
  248. return SDL_SetError("Unsupported YUV format");
  249. }
  250. return true;
  251. }
  252. bool SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect,
  253. const Uint8 *Yplane, int Ypitch,
  254. const Uint8 *Uplane, int Upitch,
  255. const Uint8 *Vplane, int Vpitch)
  256. {
  257. const Uint8 *src;
  258. Uint8 *dst;
  259. int row;
  260. size_t length;
  261. const int bpp = SDL_BYTESPERPIXEL(swdata->format);
  262. // Copy the Y plane
  263. src = Yplane;
  264. dst = swdata->pixels + rect->y * swdata->pitches[0] + rect->x * bpp;
  265. length = rect->w * bpp;
  266. for (row = 0; row < rect->h; ++row) {
  267. SDL_memcpy(dst, src, length);
  268. src += Ypitch;
  269. dst += swdata->pitches[0];
  270. }
  271. // Copy the U plane
  272. src = Uplane;
  273. if (swdata->format == SDL_PIXELFORMAT_I444 ||
  274. swdata->format == SDL_PIXELFORMAT_I4FL) {
  275. dst = swdata->pixels + swdata->h * swdata->pitches[0];
  276. dst += rect->y * swdata->pitches[1] + rect->x * bpp;
  277. length = rect->w * bpp;
  278. for (row = 0; row < rect->h; ++row) {
  279. SDL_memcpy(dst, src, length);
  280. src += Upitch;
  281. dst += swdata->pitches[1];
  282. }
  283. } else {
  284. if (swdata->format == SDL_PIXELFORMAT_IYUV ||
  285. swdata->format == SDL_PIXELFORMAT_I0FL) {
  286. dst = swdata->pixels + swdata->h * swdata->pitches[0];
  287. } else {
  288. dst = swdata->pixels + swdata->h * swdata->pitches[0] + ((swdata->h + 1) / 2) * swdata->pitches[1];
  289. }
  290. dst += rect->y / 2 * ((swdata->w + 1) / 2) * bpp + (rect->x / 2) * bpp;
  291. length = ((rect->w + 1) / 2) * bpp;
  292. for (row = 0; row < (rect->h + 1) / 2; ++row) {
  293. SDL_memcpy(dst, src, length);
  294. src += Upitch;
  295. dst += swdata->pitches[1];
  296. }
  297. }
  298. // Copy the V plane
  299. src = Vplane;
  300. if (swdata->format == SDL_PIXELFORMAT_I444 ||
  301. swdata->format == SDL_PIXELFORMAT_I4FL) {
  302. dst = swdata->pixels + swdata->h * swdata->pitches[0] + swdata->h * swdata->pitches[1];
  303. dst += rect->y * swdata->pitches[2] + rect->x * bpp;
  304. length = rect->w * bpp;
  305. for (row = 0; row < rect->h; ++row) {
  306. SDL_memcpy(dst, src, length);
  307. src += Vpitch;
  308. dst += swdata->pitches[2];
  309. }
  310. } else {
  311. if (swdata->format == SDL_PIXELFORMAT_YV12) {
  312. dst = swdata->pixels + swdata->h * swdata->pitches[0];
  313. } else {
  314. dst = swdata->pixels + swdata->h * swdata->pitches[0] + ((swdata->h + 1) / 2) * swdata->pitches[1];
  315. }
  316. dst += rect->y / 2 * ((swdata->w + 1) / 2) * bpp + (rect->x / 2) * bpp;
  317. length = ((rect->w + 1) / 2) * bpp;
  318. for (row = 0; row < (rect->h + 1) / 2; ++row) {
  319. SDL_memcpy(dst, src, length);
  320. src += Vpitch;
  321. dst += swdata->pitches[2];
  322. }
  323. }
  324. return true;
  325. }
  326. bool SDL_SW_UpdateNVTexturePlanar(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect,
  327. const Uint8 *Yplane, int Ypitch,
  328. const Uint8 *UVplane, int UVpitch)
  329. {
  330. const Uint8 *src;
  331. Uint8 *dst;
  332. int row;
  333. size_t length;
  334. // Copy the Y plane
  335. src = Yplane;
  336. dst = swdata->pixels + rect->y * swdata->w + rect->x;
  337. length = rect->w;
  338. for (row = 0; row < rect->h; ++row) {
  339. SDL_memcpy(dst, src, length);
  340. src += Ypitch;
  341. dst += swdata->w;
  342. }
  343. // Copy the UV or VU plane
  344. src = UVplane;
  345. dst = swdata->pixels + swdata->h * swdata->w;
  346. dst += rect->y * ((swdata->w + 1) / 2) + rect->x;
  347. length = (rect->w + 1) / 2;
  348. length *= 2;
  349. for (row = 0; row < (rect->h + 1) / 2; ++row) {
  350. SDL_memcpy(dst, src, length);
  351. src += UVpitch;
  352. dst += 2 * ((swdata->w + 1) / 2);
  353. }
  354. return true;
  355. }
  356. bool SDL_SW_LockYUVTexture(SDL_SW_YUVTexture *swdata, const SDL_Rect *rect,
  357. void **pixels, int *pitch)
  358. {
  359. switch (swdata->format) {
  360. case SDL_PIXELFORMAT_YV12:
  361. case SDL_PIXELFORMAT_IYUV:
  362. case SDL_PIXELFORMAT_I444:
  363. case SDL_PIXELFORMAT_I4FL:
  364. case SDL_PIXELFORMAT_NV12:
  365. case SDL_PIXELFORMAT_NV21:
  366. if (rect && (rect->x != 0 || rect->y != 0 || rect->w != swdata->w || rect->h != swdata->h)) {
  367. return SDL_SetError("YV12, IYUV, I444, I4FL, NV12, NV21 textures only support full surface locks");
  368. }
  369. break;
  370. default:
  371. break;
  372. }
  373. if (rect) {
  374. *pixels = swdata->planes[0] + rect->y * swdata->pitches[0] + rect->x * SDL_BYTESPERPIXEL(swdata->format);
  375. } else {
  376. *pixels = swdata->planes[0];
  377. }
  378. *pitch = swdata->pitches[0];
  379. return true;
  380. }
  381. void SDL_SW_UnlockYUVTexture(SDL_SW_YUVTexture *swdata)
  382. {
  383. }
  384. bool SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture *swdata, const SDL_Rect *srcrect, SDL_PixelFormat target_format, int w, int h, void *pixels, int pitch)
  385. {
  386. int stretch;
  387. // Make sure we're set up to display in the desired format
  388. if (target_format != swdata->target_format && swdata->display) {
  389. SDL_DestroySurface(swdata->display);
  390. swdata->display = NULL;
  391. }
  392. stretch = 0;
  393. if (srcrect->x || srcrect->y || srcrect->w < swdata->w || srcrect->h < swdata->h) {
  394. /* The source rectangle has been clipped.
  395. Using a scratch surface is easier than adding clipped
  396. source support to all the blitters, plus that would
  397. slow them down in the general unclipped case.
  398. */
  399. stretch = 1;
  400. } else if ((srcrect->w != w) || (srcrect->h != h)) {
  401. stretch = 1;
  402. }
  403. if (stretch) {
  404. if (swdata->display) {
  405. swdata->display->w = w;
  406. swdata->display->h = h;
  407. swdata->display->pixels = pixels;
  408. swdata->display->pitch = pitch;
  409. } else {
  410. swdata->display = SDL_CreateSurfaceFrom(w, h, target_format, pixels, pitch);
  411. if (!swdata->display) {
  412. return false;
  413. }
  414. swdata->target_format = target_format;
  415. }
  416. if (!swdata->stretch) {
  417. swdata->stretch = SDL_CreateSurfaceUninitialized(swdata->w, swdata->h, target_format);
  418. if (!swdata->stretch) {
  419. return false;
  420. }
  421. }
  422. pixels = swdata->stretch->pixels;
  423. pitch = swdata->stretch->pitch;
  424. }
  425. if (!SDL_ConvertPixelsAndColorspace(swdata->w, swdata->h, swdata->format, swdata->colorspace, 0, swdata->planes[0], swdata->pitches[0], target_format, SDL_COLORSPACE_SRGB, 0, pixels, pitch)) {
  426. return false;
  427. }
  428. if (stretch) {
  429. SDL_Rect rect = *srcrect;
  430. return SDL_StretchSurface(swdata->stretch, &rect, swdata->display, NULL, SDL_SCALEMODE_NEAREST);
  431. } else {
  432. return true;
  433. }
  434. }
  435. void SDL_SW_DestroyYUVTexture(SDL_SW_YUVTexture *swdata)
  436. {
  437. if (swdata) {
  438. SDL_aligned_free(swdata->pixels);
  439. SDL_DestroySurface(swdata->stretch);
  440. SDL_DestroySurface(swdata->display);
  441. SDL_free(swdata);
  442. }
  443. }
  444. #endif // SDL_HAVE_YUV