video-scaler-ffmpeg.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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
  22. get_ffmpeg_video_format(enum video_format format)
  23. {
  24. switch (format) {
  25. case VIDEO_FORMAT_I420:
  26. return AV_PIX_FMT_YUV420P;
  27. case VIDEO_FORMAT_NV12:
  28. return AV_PIX_FMT_NV12;
  29. case VIDEO_FORMAT_YUY2:
  30. return AV_PIX_FMT_YUYV422;
  31. case VIDEO_FORMAT_UYVY:
  32. return AV_PIX_FMT_UYVY422;
  33. case VIDEO_FORMAT_RGBA:
  34. return AV_PIX_FMT_RGBA;
  35. case VIDEO_FORMAT_BGRA:
  36. return AV_PIX_FMT_BGRA;
  37. case VIDEO_FORMAT_BGRX:
  38. return AV_PIX_FMT_BGRA;
  39. case VIDEO_FORMAT_Y800:
  40. return AV_PIX_FMT_GRAY8;
  41. case VIDEO_FORMAT_I444:
  42. return AV_PIX_FMT_YUV444P;
  43. case VIDEO_FORMAT_BGR3:
  44. return AV_PIX_FMT_BGR24;
  45. case VIDEO_FORMAT_I422:
  46. return AV_PIX_FMT_YUV422P;
  47. case VIDEO_FORMAT_I40A:
  48. return AV_PIX_FMT_YUVA420P;
  49. case VIDEO_FORMAT_I42A:
  50. return AV_PIX_FMT_YUVA422P;
  51. case VIDEO_FORMAT_YUVA:
  52. return AV_PIX_FMT_YUVA444P;
  53. case VIDEO_FORMAT_NONE:
  54. case VIDEO_FORMAT_YVYU:
  55. case VIDEO_FORMAT_AYUV:
  56. /* not supported by FFmpeg */
  57. return AV_PIX_FMT_NONE;
  58. }
  59. return AV_PIX_FMT_NONE;
  60. }
  61. static inline int get_ffmpeg_scale_type(enum video_scale_type type)
  62. {
  63. switch (type) {
  64. case VIDEO_SCALE_DEFAULT:
  65. return SWS_FAST_BILINEAR;
  66. case VIDEO_SCALE_POINT:
  67. return SWS_POINT;
  68. case VIDEO_SCALE_FAST_BILINEAR:
  69. return SWS_FAST_BILINEAR;
  70. case VIDEO_SCALE_BILINEAR:
  71. return SWS_BILINEAR | SWS_AREA;
  72. case VIDEO_SCALE_BICUBIC:
  73. return SWS_BICUBIC;
  74. }
  75. return SWS_POINT;
  76. }
  77. static inline const int *get_ffmpeg_coeffs(enum video_colorspace cs)
  78. {
  79. switch (cs) {
  80. case VIDEO_CS_DEFAULT:
  81. return sws_getCoefficients(SWS_CS_ITU601);
  82. case VIDEO_CS_601:
  83. return sws_getCoefficients(SWS_CS_ITU601);
  84. case VIDEO_CS_709:
  85. return sws_getCoefficients(SWS_CS_ITU709);
  86. default:
  87. return sws_getCoefficients(SWS_CS_ITU601);
  88. }
  89. }
  90. static inline int get_ffmpeg_range_type(enum video_range_type type)
  91. {
  92. switch (type) {
  93. case VIDEO_RANGE_DEFAULT:
  94. return 0;
  95. case VIDEO_RANGE_PARTIAL:
  96. return 0;
  97. case VIDEO_RANGE_FULL:
  98. return 1;
  99. }
  100. return 0;
  101. }
  102. #define FIXED_1_0 (1 << 16)
  103. int video_scaler_create(video_scaler_t **scaler_out,
  104. const struct video_scale_info *dst,
  105. const struct video_scale_info *src,
  106. enum video_scale_type type)
  107. {
  108. enum AVPixelFormat format_src = get_ffmpeg_video_format(src->format);
  109. enum AVPixelFormat format_dst = get_ffmpeg_video_format(dst->format);
  110. int scale_type = get_ffmpeg_scale_type(type);
  111. const int *coeff_src = get_ffmpeg_coeffs(src->colorspace);
  112. const int *coeff_dst = get_ffmpeg_coeffs(dst->colorspace);
  113. int range_src = get_ffmpeg_range_type(src->range);
  114. int range_dst = get_ffmpeg_range_type(dst->range);
  115. struct video_scaler *scaler;
  116. int ret;
  117. if (!scaler_out)
  118. return VIDEO_SCALER_FAILED;
  119. if (format_src == AV_PIX_FMT_NONE || format_dst == AV_PIX_FMT_NONE)
  120. return VIDEO_SCALER_BAD_CONVERSION;
  121. scaler = bzalloc(sizeof(struct video_scaler));
  122. scaler->src_height = src->height;
  123. scaler->swscale = sws_getCachedContext(NULL, src->width, src->height,
  124. format_src, dst->width,
  125. dst->height, format_dst,
  126. scale_type, NULL, NULL, NULL);
  127. if (!scaler->swscale) {
  128. blog(LOG_ERROR, "video_scaler_create: Could not create "
  129. "swscale");
  130. goto fail;
  131. }
  132. ret = sws_setColorspaceDetails(scaler->swscale, coeff_src, range_src,
  133. coeff_dst, range_dst, 0, FIXED_1_0,
  134. FIXED_1_0);
  135. if (ret < 0) {
  136. blog(LOG_DEBUG, "video_scaler_create: "
  137. "sws_setColorspaceDetails failed, ignoring");
  138. }
  139. *scaler_out = scaler;
  140. return VIDEO_SCALER_SUCCESS;
  141. fail:
  142. video_scaler_destroy(scaler);
  143. return VIDEO_SCALER_FAILED;
  144. }
  145. void video_scaler_destroy(video_scaler_t *scaler)
  146. {
  147. if (scaler) {
  148. sws_freeContext(scaler->swscale);
  149. bfree(scaler);
  150. }
  151. }
  152. bool video_scaler_scale(video_scaler_t *scaler, uint8_t *output[],
  153. const uint32_t out_linesize[],
  154. const uint8_t *const input[],
  155. const uint32_t in_linesize[])
  156. {
  157. if (!scaler)
  158. return false;
  159. int ret = sws_scale(scaler->swscale, input, (const int *)in_linesize, 0,
  160. scaler->src_height, output,
  161. (const int *)out_linesize);
  162. if (ret <= 0) {
  163. blog(LOG_ERROR, "video_scaler_scale: sws_scale failed: %d",
  164. ret);
  165. return false;
  166. }
  167. return true;
  168. }