spherical.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /*
  2. * Copyright (c) 2016 Vittorio Giovara <vittorio.giovara@gmail.com>
  3. *
  4. * This file is part of FFmpeg.
  5. *
  6. * FFmpeg is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * FFmpeg is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with FFmpeg; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /**
  21. * @file
  22. * Spherical video
  23. */
  24. #ifndef AVUTIL_SPHERICAL_H
  25. #define AVUTIL_SPHERICAL_H
  26. #include <stddef.h>
  27. #include <stdint.h>
  28. /**
  29. * @addtogroup lavu_video
  30. * @{
  31. *
  32. * @defgroup lavu_video_spherical Spherical video mapping
  33. * @{
  34. */
  35. /**
  36. * @addtogroup lavu_video_spherical
  37. * A spherical video file contains surfaces that need to be mapped onto a
  38. * sphere. Depending on how the frame was converted, a different distortion
  39. * transformation or surface recomposition function needs to be applied before
  40. * the video should be mapped and displayed.
  41. */
  42. /**
  43. * Projection of the video surface(s) on a sphere.
  44. */
  45. enum AVSphericalProjection {
  46. /**
  47. * Video represents a sphere mapped on a flat surface using
  48. * equirectangular projection.
  49. */
  50. AV_SPHERICAL_EQUIRECTANGULAR,
  51. /**
  52. * Video frame is split into 6 faces of a cube, and arranged on a
  53. * 3x2 layout. Faces are oriented upwards for the front, left, right,
  54. * and back faces. The up face is oriented so the top of the face is
  55. * forwards and the down face is oriented so the top of the face is
  56. * to the back.
  57. */
  58. AV_SPHERICAL_CUBEMAP,
  59. /**
  60. * Video represents a portion of a sphere mapped on a flat surface
  61. * using equirectangular projection. The @ref bounding fields indicate
  62. * the position of the current video in a larger surface.
  63. */
  64. AV_SPHERICAL_EQUIRECTANGULAR_TILE,
  65. };
  66. /**
  67. * This structure describes how to handle spherical videos, outlining
  68. * information about projection, initial layout, and any other view modifier.
  69. *
  70. * @note The struct must be allocated with av_spherical_alloc() and
  71. * its size is not a part of the public ABI.
  72. */
  73. typedef struct AVSphericalMapping {
  74. /**
  75. * Projection type.
  76. */
  77. enum AVSphericalProjection projection;
  78. /**
  79. * @name Initial orientation
  80. * @{
  81. * There fields describe additional rotations applied to the sphere after
  82. * the video frame is mapped onto it. The sphere is rotated around the
  83. * viewer, who remains stationary. The order of transformation is always
  84. * yaw, followed by pitch, and finally by roll.
  85. *
  86. * The coordinate system matches the one defined in OpenGL, where the
  87. * forward vector (z) is coming out of screen, and it is equivalent to
  88. * a rotation matrix of R = r_y(yaw) * r_x(pitch) * r_z(roll).
  89. *
  90. * A positive yaw rotates the portion of the sphere in front of the viewer
  91. * toward their right. A positive pitch rotates the portion of the sphere
  92. * in front of the viewer upwards. A positive roll tilts the portion of
  93. * the sphere in front of the viewer to the viewer's right.
  94. *
  95. * These values are exported as 16.16 fixed point.
  96. *
  97. * See this equirectangular projection as example:
  98. *
  99. * @code{.unparsed}
  100. * Yaw
  101. * -180 0 180
  102. * 90 +-------------+-------------+ 180
  103. * | | | up
  104. * P | | | y| forward
  105. * i | ^ | | /z
  106. * t 0 +-------------X-------------+ 0 Roll | /
  107. * c | | | | /
  108. * h | | | 0|/_____right
  109. * | | | x
  110. * -90 +-------------+-------------+ -180
  111. *
  112. * X - the default camera center
  113. * ^ - the default up vector
  114. * @endcode
  115. */
  116. int32_t yaw; ///< Rotation around the up vector [-180, 180].
  117. int32_t pitch; ///< Rotation around the right vector [-90, 90].
  118. int32_t roll; ///< Rotation around the forward vector [-180, 180].
  119. /**
  120. * @}
  121. */
  122. /**
  123. * @name Bounding rectangle
  124. * @anchor bounding
  125. * @{
  126. * These fields indicate the location of the current tile, and where
  127. * it should be mapped relative to the original surface. They are
  128. * exported as 0.32 fixed point, and can be converted to classic
  129. * pixel values with av_spherical_bounds().
  130. *
  131. * @code{.unparsed}
  132. * +----------------+----------+
  133. * | |bound_top |
  134. * | +--------+ |
  135. * | bound_left |tile | |
  136. * +<---------->| |<--->+bound_right
  137. * | +--------+ |
  138. * | | |
  139. * | bound_bottom| |
  140. * +----------------+----------+
  141. * @endcode
  142. *
  143. * If needed, the original video surface dimensions can be derived
  144. * by adding the current stream or frame size to the related bounds,
  145. * like in the following example:
  146. *
  147. * @code{c}
  148. * original_width = tile->width + bound_left + bound_right;
  149. * original_height = tile->height + bound_top + bound_bottom;
  150. * @endcode
  151. *
  152. * @note These values are valid only for the tiled equirectangular
  153. * projection type (@ref AV_SPHERICAL_EQUIRECTANGULAR_TILE),
  154. * and should be ignored in all other cases.
  155. */
  156. uint32_t bound_left; ///< Distance from the left edge
  157. uint32_t bound_top; ///< Distance from the top edge
  158. uint32_t bound_right; ///< Distance from the right edge
  159. uint32_t bound_bottom; ///< Distance from the bottom edge
  160. /**
  161. * @}
  162. */
  163. /**
  164. * Number of pixels to pad from the edge of each cube face.
  165. *
  166. * @note This value is valid for only for the cubemap projection type
  167. * (@ref AV_SPHERICAL_CUBEMAP), and should be ignored in all other
  168. * cases.
  169. */
  170. uint32_t padding;
  171. } AVSphericalMapping;
  172. /**
  173. * Allocate a AVSphericalVideo structure and initialize its fields to default
  174. * values.
  175. *
  176. * @return the newly allocated struct or NULL on failure
  177. */
  178. AVSphericalMapping *av_spherical_alloc(size_t *size);
  179. /**
  180. * Convert the @ref bounding fields from an AVSphericalVideo
  181. * from 0.32 fixed point to pixels.
  182. *
  183. * @param map The AVSphericalVideo map to read bound values from.
  184. * @param width Width of the current frame or stream.
  185. * @param height Height of the current frame or stream.
  186. * @param left Pixels from the left edge.
  187. * @param top Pixels from the top edge.
  188. * @param right Pixels from the right edge.
  189. * @param bottom Pixels from the bottom edge.
  190. */
  191. void av_spherical_tile_bounds(const AVSphericalMapping *map,
  192. size_t width, size_t height,
  193. size_t *left, size_t *top,
  194. size_t *right, size_t *bottom);
  195. /**
  196. * Provide a human-readable name of a given AVSphericalProjection.
  197. *
  198. * @param projection The input AVSphericalProjection.
  199. *
  200. * @return The name of the AVSphericalProjection, or "unknown".
  201. */
  202. const char *av_spherical_projection_name(enum AVSphericalProjection projection);
  203. /**
  204. * Get the AVSphericalProjection form a human-readable name.
  205. *
  206. * @param name The input string.
  207. *
  208. * @return The AVSphericalProjection value, or -1 if not found.
  209. */
  210. int av_spherical_from_name(const char *name);
  211. /**
  212. * @}
  213. * @}
  214. */
  215. #endif /* AVUTIL_SPHERICAL_H */