video-frame.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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) *size = (*size + alignment - 1) & (~(alignment - 1));
  18. static inline void align_size(size_t *size, size_t alignment)
  19. {
  20. ALIGN(size, alignment);
  21. }
  22. static inline void align_uint32(uint32_t *size, size_t alignment)
  23. {
  24. ALIGN(size, (uint32_t)alignment);
  25. }
  26. /* assumes already-zeroed array */
  27. void video_frame_get_linesizes(uint32_t linesize[MAX_AV_PLANES], enum video_format format, uint32_t width)
  28. {
  29. switch (format) {
  30. default:
  31. case VIDEO_FORMAT_NONE:
  32. break;
  33. case VIDEO_FORMAT_BGR3: /* one plane: triple width */
  34. linesize[0] = width * 3;
  35. break;
  36. case VIDEO_FORMAT_RGBA: /* one plane: quadruple width */
  37. case VIDEO_FORMAT_BGRA:
  38. case VIDEO_FORMAT_BGRX:
  39. case VIDEO_FORMAT_AYUV:
  40. case VIDEO_FORMAT_R10L:
  41. linesize[0] = width * 4;
  42. break;
  43. case VIDEO_FORMAT_P416: /* two planes: double width, quadruple width */
  44. linesize[0] = width * 2;
  45. linesize[1] = width * 4;
  46. break;
  47. case VIDEO_FORMAT_I420: /* three planes: full width, half width, half width */
  48. case VIDEO_FORMAT_I422:
  49. linesize[0] = width;
  50. linesize[1] = HALF(width);
  51. linesize[2] = HALF(width);
  52. break;
  53. case VIDEO_FORMAT_I210: /* three planes: double width, full width, full width */
  54. case VIDEO_FORMAT_I010:
  55. linesize[0] = width * 2;
  56. linesize[1] = width;
  57. linesize[2] = width;
  58. break;
  59. case VIDEO_FORMAT_I40A: /* four planes: full width, half width, half width, full width */
  60. case VIDEO_FORMAT_I42A:
  61. linesize[0] = width;
  62. linesize[1] = HALF(width);
  63. linesize[2] = HALF(width);
  64. linesize[3] = width;
  65. break;
  66. case VIDEO_FORMAT_YVYU: /* one plane: double width */
  67. case VIDEO_FORMAT_YUY2:
  68. case VIDEO_FORMAT_UYVY:
  69. linesize[0] = width * 2;
  70. break;
  71. case VIDEO_FORMAT_P010: /* two planes: all double width */
  72. case VIDEO_FORMAT_P216:
  73. linesize[0] = width * 2;
  74. linesize[1] = width * 2;
  75. break;
  76. case VIDEO_FORMAT_I412: /* three planes: all double width */
  77. linesize[0] = width * 2;
  78. linesize[1] = width * 2;
  79. linesize[2] = width * 2;
  80. break;
  81. case VIDEO_FORMAT_YA2L: /* four planes: all double width */
  82. linesize[0] = width * 2;
  83. linesize[1] = width * 2;
  84. linesize[2] = width * 2;
  85. linesize[3] = width * 2;
  86. break;
  87. case VIDEO_FORMAT_Y800: /* one plane: full width */
  88. linesize[0] = width;
  89. break;
  90. case VIDEO_FORMAT_NV12: /* two planes: all full width */
  91. linesize[0] = width;
  92. linesize[1] = width;
  93. break;
  94. case VIDEO_FORMAT_I444: /* three planes: all full width */
  95. linesize[0] = width;
  96. linesize[1] = width;
  97. linesize[2] = width;
  98. break;
  99. case VIDEO_FORMAT_YUVA: /* four planes: all full width */
  100. linesize[0] = width;
  101. linesize[1] = width;
  102. linesize[2] = width;
  103. linesize[3] = width;
  104. break;
  105. case VIDEO_FORMAT_V210: { /* one plane: bruh (Little Endian Compressed) */
  106. align_uint32(&width, 48);
  107. linesize[0] = ((width + 5) / 6) * 16;
  108. break;
  109. }
  110. }
  111. }
  112. void video_frame_get_plane_heights(uint32_t heights[MAX_AV_PLANES], enum video_format format, uint32_t height)
  113. {
  114. switch (format) {
  115. default:
  116. case VIDEO_FORMAT_NONE:
  117. return;
  118. case VIDEO_FORMAT_I420: /* three planes: full height, half height, half height */
  119. case VIDEO_FORMAT_I010:
  120. heights[0] = height;
  121. heights[1] = HALF(height);
  122. heights[2] = HALF(height);
  123. break;
  124. case VIDEO_FORMAT_NV12: /* two planes: full height, half height */
  125. case VIDEO_FORMAT_P010:
  126. heights[0] = height;
  127. heights[1] = HALF(height);
  128. break;
  129. case VIDEO_FORMAT_Y800: /* one plane: full height */
  130. case VIDEO_FORMAT_YVYU:
  131. case VIDEO_FORMAT_YUY2:
  132. case VIDEO_FORMAT_UYVY:
  133. case VIDEO_FORMAT_RGBA:
  134. case VIDEO_FORMAT_BGRA:
  135. case VIDEO_FORMAT_BGRX:
  136. case VIDEO_FORMAT_BGR3:
  137. case VIDEO_FORMAT_AYUV:
  138. case VIDEO_FORMAT_V210:
  139. case VIDEO_FORMAT_R10L:
  140. heights[0] = height;
  141. break;
  142. case VIDEO_FORMAT_I444: /* three planes: all full height */
  143. case VIDEO_FORMAT_I422:
  144. case VIDEO_FORMAT_I210:
  145. case VIDEO_FORMAT_I412:
  146. heights[0] = height;
  147. heights[1] = height;
  148. heights[2] = height;
  149. break;
  150. case VIDEO_FORMAT_I40A: /* four planes: full height, half height, half height, full height */
  151. heights[0] = height;
  152. heights[1] = HALF(height);
  153. heights[2] = HALF(height);
  154. heights[3] = height;
  155. break;
  156. case VIDEO_FORMAT_I42A: /* four planes: all full height */
  157. case VIDEO_FORMAT_YUVA:
  158. case VIDEO_FORMAT_YA2L:
  159. heights[0] = height;
  160. heights[1] = height;
  161. heights[2] = height;
  162. heights[3] = height;
  163. break;
  164. case VIDEO_FORMAT_P216: /* two planes: all full height */
  165. case VIDEO_FORMAT_P416:
  166. heights[0] = height;
  167. heights[1] = height;
  168. break;
  169. }
  170. }
  171. void video_frame_init(struct video_frame *frame, enum video_format format, uint32_t width, uint32_t height)
  172. {
  173. size_t size = 0;
  174. uint32_t linesizes[MAX_AV_PLANES];
  175. uint32_t heights[MAX_AV_PLANES];
  176. size_t offsets[MAX_AV_PLANES];
  177. int alignment = base_get_alignment();
  178. if (!frame)
  179. return;
  180. memset(frame, 0, sizeof(struct video_frame));
  181. memset(linesizes, 0, sizeof(linesizes));
  182. memset(heights, 0, sizeof(heights));
  183. memset(offsets, 0, sizeof(offsets));
  184. /* determine linesizes for each plane */
  185. video_frame_get_linesizes(linesizes, format, width);
  186. /* determine line count for each plane */
  187. video_frame_get_plane_heights(heights, format, height);
  188. /* calculate total buffer required */
  189. for (uint32_t i = 0; i < MAX_AV_PLANES; i++) {
  190. if (!linesizes[i] || !heights[i])
  191. continue;
  192. size_t plane_size = (size_t)linesizes[i] * (size_t)heights[i];
  193. align_size(&plane_size, alignment);
  194. size += plane_size;
  195. offsets[i] = size;
  196. }
  197. /* allocate memory */
  198. frame->data[0] = bmalloc(size);
  199. frame->linesize[0] = linesizes[0];
  200. /* apply plane data pointers according to offsets */
  201. for (uint32_t i = 1; i < MAX_AV_PLANES; i++) {
  202. if (!linesizes[i] || !heights[i])
  203. continue;
  204. frame->data[i] = frame->data[0] + offsets[i - 1];
  205. frame->linesize[i] = linesizes[i];
  206. }
  207. }
  208. void video_frame_copy(struct video_frame *dst, const struct video_frame *src, enum video_format format, uint32_t cy)
  209. {
  210. uint32_t heights[MAX_AV_PLANES];
  211. memset(heights, 0, sizeof(heights));
  212. /* determine line count for each plane */
  213. video_frame_get_plane_heights(heights, format, cy);
  214. /* copy each plane */
  215. for (uint32_t i = 0; i < MAX_AV_PLANES; i++) {
  216. if (!heights[i])
  217. continue;
  218. if (src->linesize[i] == dst->linesize[i]) {
  219. memcpy(dst->data[i], src->data[i], src->linesize[i] * heights[i]);
  220. } else { /* linesizes which do not match must be copied line-by-line */
  221. size_t src_linesize = src->linesize[i];
  222. size_t dst_linesize = dst->linesize[i];
  223. /* determine how much we can write (frames with different line sizes require more )*/
  224. size_t linesize = src_linesize < dst_linesize ? src_linesize : dst_linesize;
  225. for (uint32_t y = 0; y < heights[i]; y++) {
  226. uint8_t *src_pos = src->data[i] + (src_linesize * y);
  227. uint8_t *dst_pos = dst->data[i] + (dst_linesize * y);
  228. memcpy(dst_pos, src_pos, linesize);
  229. }
  230. }
  231. }
  232. }