video-frame.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <assert.h>
  15. #include "video-frame.h"
  16. #define HALF(size) ((size + 1) / 2)
  17. #define ALIGN(size, alignment) \
  18. *size = (*size + alignment - 1) & (~(alignment - 1));
  19. static inline void align_size(size_t *size, size_t alignment)
  20. {
  21. ALIGN(size, alignment);
  22. }
  23. static inline void align_uint32(uint32_t *size, size_t alignment)
  24. {
  25. ALIGN(size, (uint32_t)alignment);
  26. }
  27. /* assumes already-zeroed array */
  28. void video_frame_get_linesizes(uint32_t linesize[MAX_AV_PLANES],
  29. enum video_format format, uint32_t width)
  30. {
  31. switch (format) {
  32. default:
  33. case VIDEO_FORMAT_NONE:
  34. break;
  35. case VIDEO_FORMAT_BGR3: /* one plane: triple width */
  36. linesize[0] = width * 3;
  37. break;
  38. case VIDEO_FORMAT_RGBA: /* one plane: quadruple width */
  39. case VIDEO_FORMAT_BGRA:
  40. case VIDEO_FORMAT_BGRX:
  41. case VIDEO_FORMAT_AYUV:
  42. case VIDEO_FORMAT_R10L:
  43. linesize[0] = width * 4;
  44. break;
  45. case VIDEO_FORMAT_P416: /* two planes: double width, quadruple width */
  46. linesize[0] = width * 2;
  47. linesize[1] = width * 4;
  48. break;
  49. case VIDEO_FORMAT_I420: /* three planes: full width, half width, half width */
  50. case VIDEO_FORMAT_I422:
  51. linesize[0] = width;
  52. linesize[1] = HALF(width);
  53. linesize[2] = HALF(width);
  54. break;
  55. case VIDEO_FORMAT_I210: /* three planes: double width, full width, full width */
  56. case VIDEO_FORMAT_I010:
  57. linesize[0] = width * 2;
  58. linesize[1] = width;
  59. linesize[2] = width;
  60. break;
  61. case VIDEO_FORMAT_I40A: /* four planes: full width, half width, half width, full width */
  62. case VIDEO_FORMAT_I42A:
  63. linesize[0] = width;
  64. linesize[1] = HALF(width);
  65. linesize[2] = HALF(width);
  66. linesize[3] = width;
  67. break;
  68. case VIDEO_FORMAT_YVYU: /* one plane: double width */
  69. case VIDEO_FORMAT_YUY2:
  70. case VIDEO_FORMAT_UYVY:
  71. linesize[0] = width * 2;
  72. break;
  73. case VIDEO_FORMAT_P010: /* two planes: all double width */
  74. case VIDEO_FORMAT_P216:
  75. linesize[0] = width * 2;
  76. linesize[1] = width * 2;
  77. break;
  78. case VIDEO_FORMAT_I412: /* three planes: all double width */
  79. linesize[0] = width * 2;
  80. linesize[1] = width * 2;
  81. linesize[2] = width * 2;
  82. break;
  83. case VIDEO_FORMAT_YA2L: /* four planes: all double width */
  84. linesize[0] = width * 2;
  85. linesize[1] = width * 2;
  86. linesize[2] = width * 2;
  87. linesize[3] = width * 2;
  88. break;
  89. case VIDEO_FORMAT_Y800: /* one plane: full width */
  90. linesize[0] = width;
  91. break;
  92. case VIDEO_FORMAT_NV12: /* two planes: all full width */
  93. linesize[0] = width;
  94. linesize[1] = width;
  95. break;
  96. case VIDEO_FORMAT_I444: /* three planes: all full width */
  97. linesize[0] = width;
  98. linesize[1] = width;
  99. linesize[2] = width;
  100. break;
  101. case VIDEO_FORMAT_YUVA: /* four planes: all full width */
  102. linesize[0] = width;
  103. linesize[1] = width;
  104. linesize[2] = width;
  105. linesize[3] = width;
  106. break;
  107. case VIDEO_FORMAT_V210: { /* one plane: bruh (Little Endian Compressed) */
  108. align_uint32(&width, 48);
  109. linesize[0] = ((width + 5) / 6) * 16;
  110. break;
  111. }
  112. }
  113. }
  114. void video_frame_get_plane_heights(uint32_t heights[MAX_AV_PLANES],
  115. enum video_format format, uint32_t height)
  116. {
  117. switch (format) {
  118. default:
  119. case VIDEO_FORMAT_NONE:
  120. return;
  121. case VIDEO_FORMAT_I420: /* three planes: full height, half height, half height */
  122. case VIDEO_FORMAT_I010:
  123. heights[0] = height;
  124. heights[1] = height / 2;
  125. heights[2] = height / 2;
  126. break;
  127. case VIDEO_FORMAT_NV12: /* two planes: full height, half height */
  128. case VIDEO_FORMAT_P010:
  129. heights[0] = height;
  130. heights[1] = height / 2;
  131. break;
  132. case VIDEO_FORMAT_Y800: /* one plane: full height */
  133. case VIDEO_FORMAT_YVYU:
  134. case VIDEO_FORMAT_YUY2:
  135. case VIDEO_FORMAT_UYVY:
  136. case VIDEO_FORMAT_RGBA:
  137. case VIDEO_FORMAT_BGRA:
  138. case VIDEO_FORMAT_BGRX:
  139. case VIDEO_FORMAT_BGR3:
  140. case VIDEO_FORMAT_AYUV:
  141. case VIDEO_FORMAT_V210:
  142. case VIDEO_FORMAT_R10L:
  143. heights[0] = height;
  144. break;
  145. case VIDEO_FORMAT_I444: /* three planes: all full height */
  146. case VIDEO_FORMAT_I422:
  147. case VIDEO_FORMAT_I210:
  148. case VIDEO_FORMAT_I412:
  149. heights[0] = height;
  150. heights[1] = height;
  151. heights[2] = height;
  152. break;
  153. case VIDEO_FORMAT_I40A: /* four planes: full height, half height, half height, full height */
  154. heights[0] = height;
  155. heights[1] = height / 2;
  156. heights[2] = height / 2;
  157. heights[0] = height;
  158. break;
  159. case VIDEO_FORMAT_I42A: /* four planes: all full height */
  160. case VIDEO_FORMAT_YUVA:
  161. case VIDEO_FORMAT_YA2L:
  162. heights[0] = height;
  163. heights[1] = height;
  164. heights[2] = height;
  165. heights[3] = height;
  166. break;
  167. case VIDEO_FORMAT_P216: /* two planes: all full height */
  168. case VIDEO_FORMAT_P416:
  169. heights[0] = height;
  170. heights[1] = height;
  171. break;
  172. }
  173. }
  174. void video_frame_init(struct video_frame *frame, enum video_format format,
  175. uint32_t width, uint32_t height)
  176. {
  177. size_t size = 0;
  178. uint32_t linesizes[MAX_AV_PLANES];
  179. uint32_t heights[MAX_AV_PLANES];
  180. size_t offsets[MAX_AV_PLANES - 1];
  181. int alignment = base_get_alignment();
  182. if (!frame)
  183. return;
  184. memset(frame, 0, sizeof(struct video_frame));
  185. memset(linesizes, 0, sizeof(linesizes));
  186. memset(heights, 0, sizeof(heights));
  187. memset(offsets, 0, sizeof(offsets));
  188. /* determine linesizes for each plane */
  189. video_frame_get_linesizes(linesizes, format, width);
  190. /* determine line count for each plane */
  191. video_frame_get_plane_heights(heights, format, height);
  192. /* calculate total buffer required */
  193. for (uint32_t i = 0; i < MAX_AV_PLANES; i++) {
  194. if (!linesizes[i] || !heights[i])
  195. continue;
  196. size_t plane_size = (size_t)linesizes[i] * (size_t)heights[i];
  197. align_size(&plane_size, alignment);
  198. size += plane_size;
  199. offsets[i] = size;
  200. }
  201. /* allocate memory */
  202. frame->data[0] = bmalloc(size);
  203. frame->linesize[0] = linesizes[0];
  204. /* apply plane data pointers according to offsets */
  205. for (uint32_t i = 1; i < MAX_AV_PLANES; i++) {
  206. if (!linesizes[i] || !heights[i])
  207. continue;
  208. frame->data[i] = frame->data[0] + offsets[i - 1];
  209. frame->linesize[i] = linesizes[i];
  210. }
  211. }
  212. void video_frame_copy(struct video_frame *dst, const struct video_frame *src,
  213. enum video_format format, uint32_t cy)
  214. {
  215. uint32_t heights[MAX_AV_PLANES];
  216. memset(heights, 0, sizeof(heights));
  217. /* determine line count for each plane */
  218. video_frame_get_plane_heights(heights, format, cy);
  219. /* copy each plane */
  220. for (uint32_t i = 0; i < MAX_AV_PLANES; i++) {
  221. if (!heights[i])
  222. continue;
  223. if (src->linesize[i] == dst->linesize[i]) {
  224. memcpy(dst->data[i], src->data[i],
  225. src->linesize[i] * heights[i]);
  226. } else { /* linesizes which do not match must be copied line-by-line */
  227. size_t src_linesize = src->linesize[i];
  228. size_t dst_linesize = dst->linesize[i];
  229. /* determine how much we can write (frames with different line sizes require more )*/
  230. size_t linesize = src_linesize < dst_linesize
  231. ? src_linesize
  232. : dst_linesize;
  233. for (uint32_t i = 0; i < heights[i]; i++) {
  234. uint8_t *src_pos =
  235. src->data[i] + (src_linesize * i);
  236. uint8_t *dst_pos =
  237. dst->data[i] + (dst_linesize * i);
  238. memcpy(dst_pos, src_pos, linesize);
  239. }
  240. }
  241. }
  242. }