spherical.c 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. #include "mem.h"
  21. #include "spherical.h"
  22. AVSphericalMapping *av_spherical_alloc(size_t *size)
  23. {
  24. AVSphericalMapping *spherical = av_mallocz(sizeof(AVSphericalMapping));
  25. if (!spherical)
  26. return NULL;
  27. if (size)
  28. *size = sizeof(*spherical);
  29. return spherical;
  30. }
  31. void av_spherical_tile_bounds(const AVSphericalMapping *map,
  32. size_t width, size_t height,
  33. size_t *left, size_t *top,
  34. size_t *right, size_t *bottom)
  35. {
  36. /* conversion from 0.32 coordinates to pixels */
  37. uint64_t orig_width = (uint64_t) width * UINT32_MAX /
  38. (UINT32_MAX - map->bound_right - map->bound_left);
  39. uint64_t orig_height = (uint64_t) height * UINT32_MAX /
  40. (UINT32_MAX - map->bound_bottom - map->bound_top);
  41. /* add a (UINT32_MAX - 1) to round up integer division */
  42. *left = (orig_width * map->bound_left + UINT32_MAX - 1) / UINT32_MAX;
  43. *top = (orig_height * map->bound_top + UINT32_MAX - 1) / UINT32_MAX;
  44. *right = orig_width - width - *left;
  45. *bottom = orig_height - height - *top;
  46. }
  47. static const char *spherical_projection_names[] = {
  48. [AV_SPHERICAL_EQUIRECTANGULAR] = "equirectangular",
  49. [AV_SPHERICAL_CUBEMAP] = "cubemap",
  50. [AV_SPHERICAL_EQUIRECTANGULAR_TILE] = "tiled equirectangular",
  51. };
  52. const char *av_spherical_projection_name(enum AVSphericalProjection projection)
  53. {
  54. if ((unsigned)projection >= FF_ARRAY_ELEMS(spherical_projection_names))
  55. return "unknown";
  56. return spherical_projection_names[projection];
  57. }
  58. int av_spherical_from_name(const char *name)
  59. {
  60. int i;
  61. for (i = 0; i < FF_ARRAY_ELEMS(spherical_projection_names); i++) {
  62. size_t len = strlen(spherical_projection_names[i]);
  63. if (!strncmp(spherical_projection_names[i], name, len))
  64. return i;
  65. }
  66. return -1;
  67. }