video-io.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #pragma once
  15. #include "media-io-defs.h"
  16. #include "../util/c99defs.h"
  17. #ifdef __cplusplus
  18. extern "C" {
  19. #endif
  20. struct video_frame;
  21. /* Base video output component. Use this to create a video output track. */
  22. struct video_output;
  23. typedef struct video_output video_t;
  24. enum video_format {
  25. VIDEO_FORMAT_NONE,
  26. /* planar 4:2:0 formats */
  27. VIDEO_FORMAT_I420, /* three-plane */
  28. VIDEO_FORMAT_NV12, /* two-plane, luma and packed chroma */
  29. /* packed 4:2:2 formats */
  30. VIDEO_FORMAT_YVYU,
  31. VIDEO_FORMAT_YUY2, /* YUYV */
  32. VIDEO_FORMAT_UYVY,
  33. /* packed uncompressed formats */
  34. VIDEO_FORMAT_RGBA,
  35. VIDEO_FORMAT_BGRA,
  36. VIDEO_FORMAT_BGRX,
  37. VIDEO_FORMAT_Y800, /* grayscale */
  38. /* planar 4:4:4 */
  39. VIDEO_FORMAT_I444,
  40. /* more packed uncompressed formats */
  41. VIDEO_FORMAT_BGR3,
  42. /* planar 4:2:2 */
  43. VIDEO_FORMAT_I422,
  44. /* planar 4:2:0 with alpha */
  45. VIDEO_FORMAT_I40A,
  46. /* planar 4:2:2 with alpha */
  47. VIDEO_FORMAT_I42A,
  48. /* planar 4:4:4 with alpha */
  49. VIDEO_FORMAT_YUVA,
  50. /* packed 4:4:4 with alpha */
  51. VIDEO_FORMAT_AYUV,
  52. /* planar 4:2:0 format, 10 bpp */
  53. VIDEO_FORMAT_I010, /* three-plane */
  54. VIDEO_FORMAT_P010, /* two-plane, luma and packed chroma */
  55. /* planar 4:2:2 format, 10 bpp */
  56. VIDEO_FORMAT_I210,
  57. /* planar 4:4:4 format, 12 bpp */
  58. VIDEO_FORMAT_I412,
  59. /* planar 4:4:4:4 format, 12 bpp */
  60. VIDEO_FORMAT_YA2L,
  61. /* planar 4:2:2 format, 16 bpp */
  62. VIDEO_FORMAT_P216, /* two-plane, luma and packed chroma */
  63. /* planar 4:4:4 format, 16 bpp */
  64. VIDEO_FORMAT_P416, /* two-plane, luma and packed chroma */
  65. /* packed 4:2:2 format, 10 bpp */
  66. VIDEO_FORMAT_V210,
  67. /* packed uncompressed 10-bit format */
  68. VIDEO_FORMAT_R10L,
  69. };
  70. enum video_trc {
  71. VIDEO_TRC_DEFAULT,
  72. VIDEO_TRC_SRGB,
  73. VIDEO_TRC_PQ,
  74. VIDEO_TRC_HLG,
  75. };
  76. enum video_colorspace {
  77. VIDEO_CS_DEFAULT,
  78. VIDEO_CS_601,
  79. VIDEO_CS_709,
  80. VIDEO_CS_SRGB,
  81. VIDEO_CS_2100_PQ,
  82. VIDEO_CS_2100_HLG,
  83. };
  84. enum video_range_type {
  85. VIDEO_RANGE_DEFAULT,
  86. VIDEO_RANGE_PARTIAL,
  87. VIDEO_RANGE_FULL,
  88. };
  89. struct video_data {
  90. uint8_t *data[MAX_AV_PLANES];
  91. uint32_t linesize[MAX_AV_PLANES];
  92. uint64_t timestamp;
  93. };
  94. struct video_output_info {
  95. const char *name;
  96. enum video_format format;
  97. uint32_t fps_num;
  98. uint32_t fps_den;
  99. uint32_t width;
  100. uint32_t height;
  101. size_t cache_size;
  102. enum video_colorspace colorspace;
  103. enum video_range_type range;
  104. };
  105. static inline bool format_is_yuv(enum video_format format)
  106. {
  107. switch (format) {
  108. case VIDEO_FORMAT_I420:
  109. case VIDEO_FORMAT_NV12:
  110. case VIDEO_FORMAT_I422:
  111. case VIDEO_FORMAT_I210:
  112. case VIDEO_FORMAT_YVYU:
  113. case VIDEO_FORMAT_YUY2:
  114. case VIDEO_FORMAT_UYVY:
  115. case VIDEO_FORMAT_I444:
  116. case VIDEO_FORMAT_I412:
  117. case VIDEO_FORMAT_I40A:
  118. case VIDEO_FORMAT_I42A:
  119. case VIDEO_FORMAT_YUVA:
  120. case VIDEO_FORMAT_YA2L:
  121. case VIDEO_FORMAT_AYUV:
  122. case VIDEO_FORMAT_I010:
  123. case VIDEO_FORMAT_P010:
  124. case VIDEO_FORMAT_P216:
  125. case VIDEO_FORMAT_P416:
  126. case VIDEO_FORMAT_V210:
  127. return true;
  128. case VIDEO_FORMAT_NONE:
  129. case VIDEO_FORMAT_RGBA:
  130. case VIDEO_FORMAT_BGRA:
  131. case VIDEO_FORMAT_BGRX:
  132. case VIDEO_FORMAT_Y800:
  133. case VIDEO_FORMAT_BGR3:
  134. case VIDEO_FORMAT_R10L:
  135. return false;
  136. }
  137. return false;
  138. }
  139. static inline const char *get_video_format_name(enum video_format format)
  140. {
  141. switch (format) {
  142. case VIDEO_FORMAT_I420:
  143. return "I420";
  144. case VIDEO_FORMAT_NV12:
  145. return "NV12";
  146. case VIDEO_FORMAT_I422:
  147. return "I422";
  148. case VIDEO_FORMAT_I210:
  149. return "I210";
  150. case VIDEO_FORMAT_YVYU:
  151. return "YVYU";
  152. case VIDEO_FORMAT_YUY2:
  153. return "YUY2";
  154. case VIDEO_FORMAT_UYVY:
  155. return "UYVY";
  156. case VIDEO_FORMAT_RGBA:
  157. return "RGBA";
  158. case VIDEO_FORMAT_BGRA:
  159. return "BGRA";
  160. case VIDEO_FORMAT_BGRX:
  161. return "BGRX";
  162. case VIDEO_FORMAT_I444:
  163. return "I444";
  164. case VIDEO_FORMAT_I412:
  165. return "I412";
  166. case VIDEO_FORMAT_Y800:
  167. return "Y800";
  168. case VIDEO_FORMAT_BGR3:
  169. return "BGR3";
  170. case VIDEO_FORMAT_I40A:
  171. return "I40A";
  172. case VIDEO_FORMAT_I42A:
  173. return "I42A";
  174. case VIDEO_FORMAT_YUVA:
  175. return "YUVA";
  176. case VIDEO_FORMAT_YA2L:
  177. return "YA2L";
  178. case VIDEO_FORMAT_AYUV:
  179. return "AYUV";
  180. case VIDEO_FORMAT_I010:
  181. return "I010";
  182. case VIDEO_FORMAT_P010:
  183. return "P010";
  184. case VIDEO_FORMAT_P216:
  185. return "P216";
  186. case VIDEO_FORMAT_P416:
  187. return "P416";
  188. case VIDEO_FORMAT_V210:
  189. return "v210";
  190. case VIDEO_FORMAT_R10L:
  191. return "R10l";
  192. case VIDEO_FORMAT_NONE:;
  193. }
  194. return "None";
  195. }
  196. static inline const char *get_video_colorspace_name(enum video_colorspace cs)
  197. {
  198. switch (cs) {
  199. case VIDEO_CS_DEFAULT:
  200. case VIDEO_CS_709:
  201. return "Rec. 709";
  202. case VIDEO_CS_SRGB:
  203. return "sRGB";
  204. case VIDEO_CS_601:
  205. return "Rec. 601";
  206. case VIDEO_CS_2100_PQ:
  207. return "Rec. 2100 (PQ)";
  208. case VIDEO_CS_2100_HLG:
  209. return "Rec. 2100 (HLG)";
  210. }
  211. return "Unknown";
  212. }
  213. static inline enum video_range_type
  214. resolve_video_range(enum video_format format, enum video_range_type range)
  215. {
  216. if (range == VIDEO_RANGE_DEFAULT) {
  217. range = format_is_yuv(format) ? VIDEO_RANGE_PARTIAL
  218. : VIDEO_RANGE_FULL;
  219. }
  220. return range;
  221. }
  222. static inline const char *get_video_range_name(enum video_format format,
  223. enum video_range_type range)
  224. {
  225. range = resolve_video_range(format, range);
  226. return range == VIDEO_RANGE_FULL ? "Full" : "Partial";
  227. }
  228. enum video_scale_type {
  229. VIDEO_SCALE_DEFAULT,
  230. VIDEO_SCALE_POINT,
  231. VIDEO_SCALE_FAST_BILINEAR,
  232. VIDEO_SCALE_BILINEAR,
  233. VIDEO_SCALE_BICUBIC,
  234. };
  235. struct video_scale_info {
  236. enum video_format format;
  237. uint32_t width;
  238. uint32_t height;
  239. enum video_range_type range;
  240. enum video_colorspace colorspace;
  241. };
  242. EXPORT enum video_format video_format_from_fourcc(uint32_t fourcc);
  243. EXPORT bool video_format_get_parameters(enum video_colorspace color_space,
  244. enum video_range_type range,
  245. float matrix[16], float min_range[3],
  246. float max_range[3]);
  247. EXPORT bool video_format_get_parameters_for_format(
  248. enum video_colorspace color_space, enum video_range_type range,
  249. enum video_format format, float matrix[16], float min_range[3],
  250. float max_range[3]);
  251. #define VIDEO_OUTPUT_SUCCESS 0
  252. #define VIDEO_OUTPUT_INVALIDPARAM -1
  253. #define VIDEO_OUTPUT_FAIL -2
  254. EXPORT int video_output_open(video_t **video, struct video_output_info *info);
  255. EXPORT void video_output_close(video_t *video);
  256. EXPORT bool
  257. video_output_connect(video_t *video, const struct video_scale_info *conversion,
  258. void (*callback)(void *param, struct video_data *frame),
  259. void *param);
  260. EXPORT bool
  261. video_output_connect2(video_t *video, const struct video_scale_info *conversion,
  262. uint32_t frame_rate_divisor,
  263. void (*callback)(void *param, struct video_data *frame),
  264. void *param);
  265. EXPORT void video_output_disconnect(video_t *video,
  266. void (*callback)(void *param,
  267. struct video_data *frame),
  268. void *param);
  269. EXPORT bool video_output_active(const video_t *video);
  270. EXPORT const struct video_output_info *
  271. video_output_get_info(const video_t *video);
  272. EXPORT bool video_output_lock_frame(video_t *video, struct video_frame *frame,
  273. int count, uint64_t timestamp);
  274. EXPORT void video_output_unlock_frame(video_t *video);
  275. EXPORT uint64_t video_output_get_frame_time(const video_t *video);
  276. EXPORT void video_output_stop(video_t *video);
  277. EXPORT bool video_output_stopped(video_t *video);
  278. EXPORT enum video_format video_output_get_format(const video_t *video);
  279. EXPORT uint32_t video_output_get_width(const video_t *video);
  280. EXPORT uint32_t video_output_get_height(const video_t *video);
  281. EXPORT double video_output_get_frame_rate(const video_t *video);
  282. EXPORT uint32_t video_output_get_skipped_frames(const video_t *video);
  283. EXPORT uint32_t video_output_get_total_frames(const video_t *video);
  284. extern void video_output_inc_texture_encoders(video_t *video);
  285. extern void video_output_dec_texture_encoders(video_t *video);
  286. extern void video_output_inc_texture_frames(video_t *video);
  287. extern void video_output_inc_texture_skipped_frames(video_t *video);
  288. extern video_t *video_output_create_with_frame_rate_divisor(video_t *video,
  289. uint32_t divisor);
  290. extern void video_output_free_frame_rate_divisor(video_t *video);
  291. #ifdef __cplusplus
  292. }
  293. #endif