video-scaler-ffmpeg.c 4.8 KB

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