spherical.h 7.8 KB

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