1
0

video-scaler-ffmpeg.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 <libavutil/imgutils.h>
  17. #include <libswscale/swscale.h>
  18. struct video_scaler {
  19. struct SwsContext *swscale;
  20. int src_height;
  21. int dst_heights[4];
  22. uint8_t *dst_pointers[4];
  23. int dst_linesizes[4];
  24. };
  25. static inline enum AVPixelFormat
  26. get_ffmpeg_video_format(enum video_format format)
  27. {
  28. switch (format) {
  29. case VIDEO_FORMAT_I420:
  30. return AV_PIX_FMT_YUV420P;
  31. case VIDEO_FORMAT_NV12:
  32. return AV_PIX_FMT_NV12;
  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. case VIDEO_FORMAT_I40A:
  52. return AV_PIX_FMT_YUVA420P;
  53. case VIDEO_FORMAT_I42A:
  54. return AV_PIX_FMT_YUVA422P;
  55. case VIDEO_FORMAT_YUVA:
  56. return AV_PIX_FMT_YUVA444P;
  57. case VIDEO_FORMAT_NONE:
  58. case VIDEO_FORMAT_YVYU:
  59. case VIDEO_FORMAT_AYUV:
  60. /* not supported by FFmpeg */
  61. return AV_PIX_FMT_NONE;
  62. }
  63. return AV_PIX_FMT_NONE;
  64. }
  65. static inline int get_ffmpeg_scale_type(enum video_scale_type type)
  66. {
  67. switch (type) {
  68. case VIDEO_SCALE_DEFAULT:
  69. return SWS_FAST_BILINEAR;
  70. case VIDEO_SCALE_POINT:
  71. return SWS_POINT;
  72. case VIDEO_SCALE_FAST_BILINEAR:
  73. return SWS_FAST_BILINEAR;
  74. case VIDEO_SCALE_BILINEAR:
  75. return SWS_BILINEAR | SWS_AREA;
  76. case VIDEO_SCALE_BICUBIC:
  77. return SWS_BICUBIC;
  78. }
  79. return SWS_POINT;
  80. }
  81. static inline const int *get_ffmpeg_coeffs(enum video_colorspace cs)
  82. {
  83. const int colorspace = (cs == VIDEO_CS_601) ? SWS_CS_ITU601
  84. : SWS_CS_ITU709;
  85. return sws_getCoefficients(colorspace);
  86. }
  87. static inline int get_ffmpeg_range_type(enum video_range_type type)
  88. {
  89. switch (type) {
  90. case VIDEO_RANGE_DEFAULT:
  91. return 0;
  92. case VIDEO_RANGE_PARTIAL:
  93. return 0;
  94. case VIDEO_RANGE_FULL:
  95. return 1;
  96. }
  97. return 0;
  98. }
  99. #define FIXED_1_0 (1 << 16)
  100. int video_scaler_create(video_scaler_t **scaler_out,
  101. const struct video_scale_info *dst,
  102. const struct video_scale_info *src,
  103. enum video_scale_type type)
  104. {
  105. enum AVPixelFormat format_src = get_ffmpeg_video_format(src->format);
  106. enum AVPixelFormat format_dst = get_ffmpeg_video_format(dst->format);
  107. int scale_type = get_ffmpeg_scale_type(type);
  108. const int *coeff_src = get_ffmpeg_coeffs(src->colorspace);
  109. const int *coeff_dst = get_ffmpeg_coeffs(dst->colorspace);
  110. int range_src = get_ffmpeg_range_type(src->range);
  111. int range_dst = get_ffmpeg_range_type(dst->range);
  112. struct video_scaler *scaler;
  113. int ret;
  114. if (!scaler_out)
  115. return VIDEO_SCALER_FAILED;
  116. if (format_src == AV_PIX_FMT_NONE || format_dst == AV_PIX_FMT_NONE)
  117. return VIDEO_SCALER_BAD_CONVERSION;
  118. scaler = bzalloc(sizeof(struct video_scaler));
  119. scaler->src_height = src->height;
  120. const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(format_dst);
  121. bool has_plane[4] = {0};
  122. for (size_t i = 0; i < 4; i++)
  123. has_plane[desc->comp[i].plane] = 1;
  124. scaler->dst_heights[0] = dst->height;
  125. for (size_t i = 1; i < 4; ++i) {
  126. if (has_plane[i]) {
  127. const int s = (i == 1 || i == 2) ? desc->log2_chroma_h
  128. : 0;
  129. scaler->dst_heights[i] = dst->height >> s;
  130. }
  131. }
  132. ret = av_image_alloc(scaler->dst_pointers, scaler->dst_linesizes,
  133. dst->width, dst->height, format_dst, 32);
  134. if (ret < 0) {
  135. blog(LOG_WARNING,
  136. "video_scaler_create: av_image_alloc failed: %d", ret);
  137. goto fail;
  138. }
  139. scaler->swscale = sws_getCachedContext(NULL, src->width, src->height,
  140. format_src, dst->width,
  141. dst->height, format_dst,
  142. scale_type, NULL, NULL, NULL);
  143. if (!scaler->swscale) {
  144. blog(LOG_ERROR, "video_scaler_create: Could not create "
  145. "swscale");
  146. goto fail;
  147. }
  148. ret = sws_setColorspaceDetails(scaler->swscale, coeff_src, range_src,
  149. coeff_dst, range_dst, 0, FIXED_1_0,
  150. FIXED_1_0);
  151. if (ret < 0) {
  152. blog(LOG_DEBUG, "video_scaler_create: "
  153. "sws_setColorspaceDetails failed, ignoring");
  154. }
  155. *scaler_out = scaler;
  156. return VIDEO_SCALER_SUCCESS;
  157. fail:
  158. video_scaler_destroy(scaler);
  159. return VIDEO_SCALER_FAILED;
  160. }
  161. void video_scaler_destroy(video_scaler_t *scaler)
  162. {
  163. if (scaler) {
  164. sws_freeContext(scaler->swscale);
  165. if (scaler->dst_pointers[0])
  166. av_freep(scaler->dst_pointers);
  167. bfree(scaler);
  168. }
  169. }
  170. bool video_scaler_scale(video_scaler_t *scaler, uint8_t *output[],
  171. const uint32_t out_linesize[],
  172. const uint8_t *const input[],
  173. const uint32_t in_linesize[])
  174. {
  175. if (!scaler)
  176. return false;
  177. int ret = sws_scale(scaler->swscale, input, (const int *)in_linesize, 0,
  178. scaler->src_height, scaler->dst_pointers,
  179. scaler->dst_linesizes);
  180. if (ret <= 0) {
  181. blog(LOG_ERROR, "video_scaler_scale: sws_scale failed: %d",
  182. ret);
  183. return false;
  184. }
  185. for (size_t plane = 0; plane < 4; ++plane) {
  186. if (!scaler->dst_pointers[plane])
  187. continue;
  188. const size_t scaled_linesize = scaler->dst_linesizes[plane];
  189. const size_t plane_linesize = out_linesize[plane];
  190. uint8_t *dst = output[plane];
  191. const uint8_t *src = scaler->dst_pointers[plane];
  192. const size_t height = scaler->dst_heights[plane];
  193. if (scaled_linesize == plane_linesize) {
  194. memcpy(dst, src, scaled_linesize * height);
  195. } else {
  196. size_t linesize = scaled_linesize;
  197. if (linesize > plane_linesize)
  198. linesize = plane_linesize;
  199. for (size_t y = 0; y < height; y++) {
  200. memcpy(dst, src, linesize);
  201. dst += plane_linesize;
  202. src += scaled_linesize;
  203. }
  204. }
  205. }
  206. return true;
  207. }