SDL_yuv_sw.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. #include "../SDL_internal.h"
  19. /* This is the software implementation of the YUV texture support */
  20. #if SDL_HAVE_YUV
  21. #include "SDL_yuv_sw_c.h"
  22. #include "SDL_cpuinfo.h"
  23. SDL_SW_YUVTexture *
  24. SDL_SW_CreateYUVTexture(Uint32 format, 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. SDL_OutOfMemory();
  43. return NULL;
  44. }
  45. swdata->format = format;
  46. swdata->target_format = SDL_PIXELFORMAT_UNKNOWN;
  47. swdata->w = w;
  48. swdata->h = h;
  49. {
  50. const int sz_plane = w * h;
  51. const int sz_plane_chroma = ((w + 1) / 2) * ((h + 1) / 2);
  52. const int sz_plane_packed = ((w + 1) / 2) * h;
  53. int dst_size = 0;
  54. switch(format)
  55. {
  56. case SDL_PIXELFORMAT_YV12: /**< Planar mode: Y + V + U (3 planes) */
  57. case SDL_PIXELFORMAT_IYUV: /**< Planar mode: Y + U + V (3 planes) */
  58. dst_size = sz_plane + sz_plane_chroma + sz_plane_chroma;
  59. break;
  60. case SDL_PIXELFORMAT_YUY2: /**< Packed mode: Y0+U0+Y1+V0 (1 plane) */
  61. case SDL_PIXELFORMAT_UYVY: /**< Packed mode: U0+Y0+V0+Y1 (1 plane) */
  62. case SDL_PIXELFORMAT_YVYU: /**< Packed mode: Y0+V0+Y1+U0 (1 plane) */
  63. dst_size = 4 * sz_plane_packed;
  64. break;
  65. case SDL_PIXELFORMAT_NV12: /**< Planar mode: Y + U/V interleaved (2 planes) */
  66. case SDL_PIXELFORMAT_NV21: /**< Planar mode: Y + V/U interleaved (2 planes) */
  67. dst_size = sz_plane + sz_plane_chroma + sz_plane_chroma;
  68. break;
  69. default:
  70. SDL_assert(0 && "We should never get here (caught above)");
  71. break;
  72. }
  73. swdata->pixels = (Uint8 *) SDL_SIMDAlloc(dst_size);
  74. if (!swdata->pixels) {
  75. SDL_SW_DestroyYUVTexture(swdata);
  76. SDL_OutOfMemory();
  77. return NULL;
  78. }
  79. }
  80. /* Find the pitch and offset values for the texture */
  81. switch (format) {
  82. case SDL_PIXELFORMAT_YV12:
  83. case SDL_PIXELFORMAT_IYUV:
  84. swdata->pitches[0] = w;
  85. swdata->pitches[1] = (swdata->pitches[0] + 1) / 2;
  86. swdata->pitches[2] = (swdata->pitches[0] + 1) / 2;
  87. swdata->planes[0] = swdata->pixels;
  88. swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
  89. swdata->planes[2] = swdata->planes[1] + swdata->pitches[1] * ((h + 1) / 2);
  90. break;
  91. case SDL_PIXELFORMAT_YUY2:
  92. case SDL_PIXELFORMAT_UYVY:
  93. case SDL_PIXELFORMAT_YVYU:
  94. swdata->pitches[0] = ((w + 1) / 2) * 4;
  95. swdata->planes[0] = swdata->pixels;
  96. break;
  97. case SDL_PIXELFORMAT_NV12:
  98. case SDL_PIXELFORMAT_NV21:
  99. swdata->pitches[0] = w;
  100. swdata->pitches[1] = 2 * ((swdata->pitches[0] + 1) / 2);
  101. swdata->planes[0] = swdata->pixels;
  102. swdata->planes[1] = swdata->planes[0] + swdata->pitches[0] * h;
  103. break;
  104. default:
  105. SDL_assert(0 && "We should never get here (caught above)");
  106. break;
  107. }
  108. /* We're all done.. */
  109. return (swdata);
  110. }
  111. int
  112. SDL_SW_QueryYUVTexturePixels(SDL_SW_YUVTexture * swdata, void **pixels,
  113. int *pitch)
  114. {
  115. *pixels = swdata->planes[0];
  116. *pitch = swdata->pitches[0];
  117. return 0;
  118. }
  119. int
  120. SDL_SW_UpdateYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
  121. const void *pixels, int pitch)
  122. {
  123. switch (swdata->format) {
  124. case SDL_PIXELFORMAT_YV12:
  125. case SDL_PIXELFORMAT_IYUV:
  126. if (rect->x == 0 && rect->y == 0 &&
  127. rect->w == swdata->w && rect->h == swdata->h) {
  128. SDL_memcpy(swdata->pixels, pixels,
  129. (swdata->h * swdata->w) + 2* ((swdata->h + 1) /2) * ((swdata->w + 1) / 2));
  130. } else {
  131. Uint8 *src, *dst;
  132. int row;
  133. size_t length;
  134. /* Copy the Y plane */
  135. src = (Uint8 *) pixels;
  136. dst = swdata->pixels + rect->y * swdata->w + rect->x;
  137. length = rect->w;
  138. for (row = 0; row < rect->h; ++row) {
  139. SDL_memcpy(dst, src, length);
  140. src += pitch;
  141. dst += swdata->w;
  142. }
  143. /* Copy the next plane */
  144. src = (Uint8 *) pixels + rect->h * pitch;
  145. dst = swdata->pixels + swdata->h * swdata->w;
  146. dst += rect->y/2 * ((swdata->w + 1) / 2) + rect->x/2;
  147. length = (rect->w + 1) / 2;
  148. for (row = 0; row < (rect->h + 1)/2; ++row) {
  149. SDL_memcpy(dst, src, length);
  150. src += (pitch + 1)/2;
  151. dst += (swdata->w + 1)/2;
  152. }
  153. /* Copy the next plane */
  154. src = (Uint8 *) pixels + rect->h * pitch + ((rect->h + 1) / 2) * ((pitch + 1) / 2);
  155. dst = swdata->pixels + swdata->h * swdata->w +
  156. ((swdata->h + 1)/2) * ((swdata->w+1) / 2);
  157. dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
  158. length = (rect->w + 1) / 2;
  159. for (row = 0; row < (rect->h + 1)/2; ++row) {
  160. SDL_memcpy(dst, src, length);
  161. src += (pitch + 1)/2;
  162. dst += (swdata->w + 1)/2;
  163. }
  164. }
  165. break;
  166. case SDL_PIXELFORMAT_YUY2:
  167. case SDL_PIXELFORMAT_UYVY:
  168. case SDL_PIXELFORMAT_YVYU:
  169. {
  170. Uint8 *src, *dst;
  171. int row;
  172. size_t length;
  173. src = (Uint8 *) pixels;
  174. dst =
  175. swdata->planes[0] + rect->y * swdata->pitches[0] +
  176. rect->x * 2;
  177. length = 4 * ((rect->w + 1) / 2);
  178. for (row = 0; row < rect->h; ++row) {
  179. SDL_memcpy(dst, src, length);
  180. src += pitch;
  181. dst += swdata->pitches[0];
  182. }
  183. }
  184. break;
  185. case SDL_PIXELFORMAT_NV12:
  186. case SDL_PIXELFORMAT_NV21:
  187. {
  188. if (rect->x == 0 && rect->y == 0 && rect->w == swdata->w && rect->h == swdata->h) {
  189. SDL_memcpy(swdata->pixels, pixels,
  190. (swdata->h * swdata->w) + 2* ((swdata->h + 1) /2) * ((swdata->w + 1) / 2));
  191. } else {
  192. Uint8 *src, *dst;
  193. int row;
  194. size_t length;
  195. /* Copy the Y plane */
  196. src = (Uint8 *) pixels;
  197. dst = swdata->pixels + rect->y * swdata->w + rect->x;
  198. length = rect->w;
  199. for (row = 0; row < rect->h; ++row) {
  200. SDL_memcpy(dst, src, length);
  201. src += pitch;
  202. dst += swdata->w;
  203. }
  204. /* Copy the next plane */
  205. src = (Uint8 *) pixels + rect->h * pitch;
  206. dst = swdata->pixels + swdata->h * swdata->w;
  207. dst += 2 * ((rect->y + 1)/2) * ((swdata->w + 1) / 2) + 2 * (rect->x/2);
  208. length = 2 * ((rect->w + 1) / 2);
  209. for (row = 0; row < (rect->h + 1)/2; ++row) {
  210. SDL_memcpy(dst, src, length);
  211. src += 2 * ((pitch + 1)/2);
  212. dst += 2 * ((swdata->w + 1)/2);
  213. }
  214. }
  215. }
  216. }
  217. return 0;
  218. }
  219. int
  220. SDL_SW_UpdateYUVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
  221. const Uint8 *Yplane, int Ypitch,
  222. const Uint8 *Uplane, int Upitch,
  223. const Uint8 *Vplane, int Vpitch)
  224. {
  225. const Uint8 *src;
  226. Uint8 *dst;
  227. int row;
  228. size_t length;
  229. /* Copy the Y plane */
  230. src = Yplane;
  231. dst = swdata->pixels + rect->y * swdata->w + rect->x;
  232. length = rect->w;
  233. for (row = 0; row < rect->h; ++row) {
  234. SDL_memcpy(dst, src, length);
  235. src += Ypitch;
  236. dst += swdata->w;
  237. }
  238. /* Copy the U plane */
  239. src = Uplane;
  240. if (swdata->format == SDL_PIXELFORMAT_IYUV) {
  241. dst = swdata->pixels + swdata->h * swdata->w;
  242. } else {
  243. dst = swdata->pixels + swdata->h * swdata->w +
  244. ((swdata->h + 1) / 2) * ((swdata->w + 1) / 2);
  245. }
  246. dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
  247. length = (rect->w + 1) / 2;
  248. for (row = 0; row < (rect->h + 1)/2; ++row) {
  249. SDL_memcpy(dst, src, length);
  250. src += Upitch;
  251. dst += (swdata->w + 1)/2;
  252. }
  253. /* Copy the V plane */
  254. src = Vplane;
  255. if (swdata->format == SDL_PIXELFORMAT_YV12) {
  256. dst = swdata->pixels + swdata->h * swdata->w;
  257. } else {
  258. dst = swdata->pixels + swdata->h * swdata->w +
  259. ((swdata->h + 1) / 2) * ((swdata->w + 1) / 2);
  260. }
  261. dst += rect->y/2 * ((swdata->w + 1)/2) + rect->x/2;
  262. length = (rect->w + 1) / 2;
  263. for (row = 0; row < (rect->h + 1)/2; ++row) {
  264. SDL_memcpy(dst, src, length);
  265. src += Vpitch;
  266. dst += (swdata->w + 1)/2;
  267. }
  268. return 0;
  269. }
  270. int SDL_SW_UpdateNVTexturePlanar(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
  271. const Uint8 *Yplane, int Ypitch,
  272. const Uint8 *UVplane, int UVpitch)
  273. {
  274. const Uint8 *src;
  275. Uint8 *dst;
  276. int row;
  277. size_t length;
  278. /* Copy the Y plane */
  279. src = Yplane;
  280. dst = swdata->pixels + rect->y * swdata->w + rect->x;
  281. length = rect->w;
  282. for (row = 0; row < rect->h; ++row) {
  283. SDL_memcpy(dst, src, length);
  284. src += Ypitch;
  285. dst += swdata->w;
  286. }
  287. /* Copy the UV or VU plane */
  288. src = UVplane;
  289. dst = swdata->pixels + swdata->h * swdata->w;
  290. dst += rect->y * ((swdata->w + 1)/2) + rect->x;
  291. length = (rect->w + 1) / 2;
  292. length *= 2;
  293. for (row = 0; row < (rect->h + 1)/2; ++row) {
  294. SDL_memcpy(dst, src, length);
  295. src += UVpitch;
  296. dst += 2 * ((swdata->w + 1)/2);
  297. }
  298. return 0;
  299. }
  300. int
  301. SDL_SW_LockYUVTexture(SDL_SW_YUVTexture * swdata, const SDL_Rect * rect,
  302. void **pixels, int *pitch)
  303. {
  304. switch (swdata->format) {
  305. case SDL_PIXELFORMAT_YV12:
  306. case SDL_PIXELFORMAT_IYUV:
  307. case SDL_PIXELFORMAT_NV12:
  308. case SDL_PIXELFORMAT_NV21:
  309. if (rect
  310. && (rect->x != 0 || rect->y != 0 || rect->w != swdata->w
  311. || rect->h != swdata->h)) {
  312. return SDL_SetError
  313. ("YV12, IYUV, NV12, NV21 textures only support full surface locks");
  314. }
  315. break;
  316. }
  317. if (rect) {
  318. *pixels = swdata->planes[0] + rect->y * swdata->pitches[0] + rect->x * 2;
  319. } else {
  320. *pixels = swdata->planes[0];
  321. }
  322. *pitch = swdata->pitches[0];
  323. return 0;
  324. }
  325. void
  326. SDL_SW_UnlockYUVTexture(SDL_SW_YUVTexture * swdata)
  327. {
  328. }
  329. int
  330. SDL_SW_CopyYUVToRGB(SDL_SW_YUVTexture * swdata, const SDL_Rect * srcrect,
  331. Uint32 target_format, int w, int h, void *pixels,
  332. int pitch)
  333. {
  334. int stretch;
  335. /* Make sure we're set up to display in the desired format */
  336. if (target_format != swdata->target_format && swdata->display) {
  337. SDL_FreeSurface(swdata->display);
  338. swdata->display = NULL;
  339. }
  340. stretch = 0;
  341. if (srcrect->x || srcrect->y || srcrect->w < swdata->w || srcrect->h < swdata->h) {
  342. /* The source rectangle has been clipped.
  343. Using a scratch surface is easier than adding clipped
  344. source support to all the blitters, plus that would
  345. slow them down in the general unclipped case.
  346. */
  347. stretch = 1;
  348. } else if ((srcrect->w != w) || (srcrect->h != h)) {
  349. stretch = 1;
  350. }
  351. if (stretch) {
  352. int bpp;
  353. Uint32 Rmask, Gmask, Bmask, Amask;
  354. if (swdata->display) {
  355. swdata->display->w = w;
  356. swdata->display->h = h;
  357. swdata->display->pixels = pixels;
  358. swdata->display->pitch = pitch;
  359. } else {
  360. /* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
  361. SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
  362. &Bmask, &Amask);
  363. swdata->display =
  364. SDL_CreateRGBSurfaceFrom(pixels, w, h, bpp, pitch, Rmask,
  365. Gmask, Bmask, Amask);
  366. if (!swdata->display) {
  367. return (-1);
  368. }
  369. }
  370. if (!swdata->stretch) {
  371. /* This must have succeeded in SDL_SW_SetupYUVDisplay() earlier */
  372. SDL_PixelFormatEnumToMasks(target_format, &bpp, &Rmask, &Gmask,
  373. &Bmask, &Amask);
  374. swdata->stretch =
  375. SDL_CreateRGBSurface(0, swdata->w, swdata->h, bpp, Rmask,
  376. Gmask, Bmask, Amask);
  377. if (!swdata->stretch) {
  378. return (-1);
  379. }
  380. }
  381. pixels = swdata->stretch->pixels;
  382. pitch = swdata->stretch->pitch;
  383. }
  384. if (SDL_ConvertPixels(swdata->w, swdata->h, swdata->format,
  385. swdata->planes[0], swdata->pitches[0],
  386. target_format, pixels, pitch) < 0) {
  387. return -1;
  388. }
  389. if (stretch) {
  390. SDL_Rect rect = *srcrect;
  391. SDL_SoftStretch(swdata->stretch, &rect, swdata->display, NULL);
  392. }
  393. return 0;
  394. }
  395. void
  396. SDL_SW_DestroyYUVTexture(SDL_SW_YUVTexture * swdata)
  397. {
  398. if (swdata) {
  399. SDL_SIMDFree(swdata->pixels);
  400. SDL_FreeSurface(swdata->stretch);
  401. SDL_FreeSurface(swdata->display);
  402. SDL_free(swdata);
  403. }
  404. }
  405. #endif /* SDL_HAVE_YUV */
  406. /* vi: set ts=4 sw=4 expandtab: */