SDL_yuv_sw.c 13 KB

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