video-scaler-ffmpeg.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /******************************************************************************
  2. Copyright (C) 2014 by Hugh 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 "../util/bmem.h"
  15. #include "video-scaler.h"
  16. #include <libswscale/swscale.h>
  17. struct video_scaler {
  18. struct SwsContext *swscale;
  19. int src_height;
  20. };
  21. static inline enum AVPixelFormat get_ffmpeg_video_format(
  22. enum video_format format)
  23. {
  24. switch (format) {
  25. case VIDEO_FORMAT_NONE: return AV_PIX_FMT_NONE;
  26. case VIDEO_FORMAT_I420: return AV_PIX_FMT_YUV420P;
  27. case VIDEO_FORMAT_NV12: return AV_PIX_FMT_NV12;
  28. case VIDEO_FORMAT_YVYU: return AV_PIX_FMT_NONE;
  29. case VIDEO_FORMAT_YUY2: return AV_PIX_FMT_YUYV422;
  30. case VIDEO_FORMAT_UYVY: return AV_PIX_FMT_UYVY422;
  31. case VIDEO_FORMAT_RGBA: return AV_PIX_FMT_RGBA;
  32. case VIDEO_FORMAT_BGRA: return AV_PIX_FMT_BGRA;
  33. case VIDEO_FORMAT_BGRX: return AV_PIX_FMT_BGRA;
  34. case VIDEO_FORMAT_Y800: return AV_PIX_FMT_GRAY8;
  35. case VIDEO_FORMAT_I444: return AV_PIX_FMT_YUV444P;
  36. }
  37. return AV_PIX_FMT_NONE;
  38. }
  39. static inline int get_ffmpeg_scale_type(enum video_scale_type type)
  40. {
  41. switch (type) {
  42. case VIDEO_SCALE_DEFAULT: return SWS_FAST_BILINEAR;
  43. case VIDEO_SCALE_POINT: return SWS_POINT;
  44. case VIDEO_SCALE_FAST_BILINEAR: return SWS_FAST_BILINEAR;
  45. case VIDEO_SCALE_BILINEAR: return SWS_BILINEAR | SWS_AREA;
  46. case VIDEO_SCALE_BICUBIC: return SWS_BICUBIC;
  47. }
  48. return SWS_POINT;
  49. }
  50. static inline const int *get_ffmpeg_coeffs(enum video_colorspace cs)
  51. {
  52. switch (cs) {
  53. case VIDEO_CS_DEFAULT: return sws_getCoefficients(SWS_CS_ITU601);
  54. case VIDEO_CS_601: return sws_getCoefficients(SWS_CS_ITU601);
  55. case VIDEO_CS_709: return sws_getCoefficients(SWS_CS_ITU709);
  56. }
  57. return sws_getCoefficients(SWS_CS_ITU601);
  58. }
  59. static inline int get_ffmpeg_range_type(enum video_range_type type)
  60. {
  61. switch (type) {
  62. case VIDEO_RANGE_DEFAULT: return 0;
  63. case VIDEO_RANGE_PARTIAL: return 0;
  64. case VIDEO_RANGE_FULL: return 1;
  65. }
  66. return 0;
  67. }
  68. #define FIXED_1_0 (1<<16)
  69. int video_scaler_create(video_scaler_t **scaler_out,
  70. const struct video_scale_info *dst,
  71. const struct video_scale_info *src,
  72. enum video_scale_type type)
  73. {
  74. enum AVPixelFormat format_src = get_ffmpeg_video_format(src->format);
  75. enum AVPixelFormat format_dst = get_ffmpeg_video_format(dst->format);
  76. int scale_type = get_ffmpeg_scale_type(type);
  77. const int *coeff_src = get_ffmpeg_coeffs(src->colorspace);
  78. const int *coeff_dst = get_ffmpeg_coeffs(dst->colorspace);
  79. int range_src = get_ffmpeg_range_type(src->range);
  80. int range_dst = get_ffmpeg_range_type(dst->range);
  81. struct video_scaler *scaler;
  82. int ret;
  83. if (!scaler_out)
  84. return VIDEO_SCALER_FAILED;
  85. if (format_src == AV_PIX_FMT_NONE ||
  86. format_dst == AV_PIX_FMT_NONE)
  87. return VIDEO_SCALER_BAD_CONVERSION;
  88. scaler = bzalloc(sizeof(struct video_scaler));
  89. scaler->src_height = src->height;
  90. scaler->swscale = sws_getCachedContext(NULL,
  91. src->width, src->height, format_src,
  92. dst->width, dst->height, format_dst,
  93. scale_type, NULL, NULL, NULL);
  94. if (!scaler->swscale) {
  95. blog(LOG_ERROR, "video_scaler_create: Could not create "
  96. "swscale");
  97. goto fail;
  98. }
  99. ret = sws_setColorspaceDetails(scaler->swscale,
  100. coeff_src, range_src,
  101. coeff_dst, range_dst,
  102. 0, FIXED_1_0, FIXED_1_0);
  103. if (ret < 0) {
  104. blog(LOG_DEBUG, "video_scaler_create: "
  105. "sws_setColorspaceDetails failed, ignoring");
  106. }
  107. *scaler_out = scaler;
  108. return VIDEO_SCALER_SUCCESS;
  109. fail:
  110. video_scaler_destroy(scaler);
  111. return VIDEO_SCALER_FAILED;
  112. }
  113. void video_scaler_destroy(video_scaler_t *scaler)
  114. {
  115. if (scaler) {
  116. sws_freeContext(scaler->swscale);
  117. bfree(scaler);
  118. }
  119. }
  120. bool video_scaler_scale(video_scaler_t *scaler,
  121. uint8_t *output[], const uint32_t out_linesize[],
  122. const uint8_t *const input[], const uint32_t in_linesize[])
  123. {
  124. if (!scaler)
  125. return false;
  126. int ret = sws_scale(scaler->swscale,
  127. input, (const int *)in_linesize,
  128. 0, scaler->src_height,
  129. output, (const int *)out_linesize);
  130. if (ret <= 0) {
  131. blog(LOG_ERROR, "video_scaler_scale: sws_scale failed: %d",
  132. ret);
  133. return false;
  134. }
  135. return true;
  136. }