vscale.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. /*
  2. * Copyright (C) 2015 Pedro Arthur <bygrandao@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 "swscale_internal.h"
  21. typedef struct VScalerContext
  22. {
  23. uint16_t *filter[2];
  24. int32_t *filter_pos;
  25. int filter_size;
  26. int isMMX;
  27. void *pfn;
  28. yuv2packedX_fn yuv2packedX;
  29. } VScalerContext;
  30. static int lum_planar_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  31. {
  32. VScalerContext *inst = desc->instance;
  33. int dstW = desc->dst->width;
  34. int first = FFMAX(1-inst->filter_size, inst->filter_pos[sliceY]);
  35. int sp = first - desc->src->plane[0].sliceY;
  36. int dp = sliceY - desc->dst->plane[0].sliceY;
  37. uint8_t **src = desc->src->plane[0].line + sp;
  38. uint8_t **dst = desc->dst->plane[0].line + dp;
  39. uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : sliceY * inst->filter_size);
  40. if (inst->filter_size == 1)
  41. ((yuv2planar1_fn)inst->pfn)((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0);
  42. else
  43. ((yuv2planarX_fn)inst->pfn)(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0);
  44. if (desc->alpha) {
  45. int sp = first - desc->src->plane[3].sliceY;
  46. int dp = sliceY - desc->dst->plane[3].sliceY;
  47. uint8_t **src = desc->src->plane[3].line + sp;
  48. uint8_t **dst = desc->dst->plane[3].line + dp;
  49. uint16_t *filter = inst->filter[1] + (inst->isMMX ? 0 : sliceY * inst->filter_size);
  50. if (inst->filter_size == 1)
  51. ((yuv2planar1_fn)inst->pfn)((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0);
  52. else
  53. ((yuv2planarX_fn)inst->pfn)(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0);
  54. }
  55. return 1;
  56. }
  57. static int chr_planar_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  58. {
  59. const int chrSkipMask = (1 << desc->dst->v_chr_sub_sample) - 1;
  60. if (sliceY & chrSkipMask)
  61. return 0;
  62. else {
  63. VScalerContext *inst = desc->instance;
  64. int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample);
  65. int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
  66. int first = FFMAX(1-inst->filter_size, inst->filter_pos[chrSliceY]);
  67. int sp1 = first - desc->src->plane[1].sliceY;
  68. int sp2 = first - desc->src->plane[2].sliceY;
  69. int dp1 = chrSliceY - desc->dst->plane[1].sliceY;
  70. int dp2 = chrSliceY - desc->dst->plane[2].sliceY;
  71. uint8_t **src1 = desc->src->plane[1].line + sp1;
  72. uint8_t **src2 = desc->src->plane[2].line + sp2;
  73. uint8_t **dst1 = desc->dst->plane[1].line + dp1;
  74. uint8_t **dst2 = desc->dst->plane[2].line + dp2;
  75. uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : chrSliceY * inst->filter_size);
  76. if (c->yuv2nv12cX) {
  77. ((yuv2interleavedX_fn)inst->pfn)(c, filter, inst->filter_size, (const int16_t**)src1, (const int16_t**)src2, dst1[0], dstW);
  78. } else if (inst->filter_size == 1) {
  79. ((yuv2planar1_fn)inst->pfn)((const int16_t*)src1[0], dst1[0], dstW, c->chrDither8, 0);
  80. ((yuv2planar1_fn)inst->pfn)((const int16_t*)src2[0], dst2[0], dstW, c->chrDither8, 3);
  81. } else {
  82. ((yuv2planarX_fn)inst->pfn)(filter, inst->filter_size, (const int16_t**)src1, dst1[0], dstW, c->chrDither8, 0);
  83. ((yuv2planarX_fn)inst->pfn)(filter, inst->filter_size, (const int16_t**)src2, dst2[0], dstW, c->chrDither8, inst->isMMX ? (c->uv_offx2 >> 1) : 3);
  84. }
  85. }
  86. return 1;
  87. }
  88. static int packed_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  89. {
  90. VScalerContext *inst = desc->instance;
  91. int dstW = desc->dst->width;
  92. int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
  93. int lum_fsize = inst[0].filter_size;
  94. int chr_fsize = inst[1].filter_size;
  95. uint16_t *lum_filter = inst[0].filter[0];
  96. uint16_t *chr_filter = inst[1].filter[0];
  97. int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]);
  98. int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]);
  99. int sp0 = firstLum - desc->src->plane[0].sliceY;
  100. int sp1 = firstChr - desc->src->plane[1].sliceY;
  101. int sp2 = firstChr - desc->src->plane[2].sliceY;
  102. int sp3 = firstLum - desc->src->plane[3].sliceY;
  103. int dp = sliceY - desc->dst->plane[0].sliceY;
  104. uint8_t **src0 = desc->src->plane[0].line + sp0;
  105. uint8_t **src1 = desc->src->plane[1].line + sp1;
  106. uint8_t **src2 = desc->src->plane[2].line + sp2;
  107. uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL;
  108. uint8_t **dst = desc->dst->plane[0].line + dp;
  109. if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 1) { // unscaled RGB
  110. ((yuv2packed1_fn)inst->pfn)(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2,
  111. (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, 0, sliceY);
  112. } else if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2 &&
  113. chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 &&
  114. chr_filter[2 * chrSliceY + 1] <= 4096U) { // unscaled RGB
  115. int chrAlpha = chr_filter[2 * chrSliceY + 1];
  116. ((yuv2packed1_fn)inst->pfn)(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2,
  117. (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, chrAlpha, sliceY);
  118. } else if (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2 &&
  119. lum_filter[2 * sliceY + 1] + lum_filter[2 * sliceY] == 4096 &&
  120. lum_filter[2 * sliceY + 1] <= 4096U &&
  121. chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 &&
  122. chr_filter[2 * chrSliceY + 1] <= 4096U
  123. ) { // bilinear upscale RGB
  124. int lumAlpha = lum_filter[2 * sliceY + 1];
  125. int chrAlpha = chr_filter[2 * chrSliceY + 1];
  126. c->lumMmxFilter[2] =
  127. c->lumMmxFilter[3] = lum_filter[2 * sliceY] * 0x10001;
  128. c->chrMmxFilter[2] =
  129. c->chrMmxFilter[3] = chr_filter[2 * chrSliceY] * 0x10001;
  130. ((yuv2packed2_fn)inst->pfn)(c, (const int16_t**)src0, (const int16_t**)src1, (const int16_t**)src2, (const int16_t**)src3,
  131. *dst, dstW, lumAlpha, chrAlpha, sliceY);
  132. } else { // general RGB
  133. if ((c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2) ||
  134. (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2)) {
  135. if (!c->warned_unuseable_bilinear)
  136. av_log(c, AV_LOG_INFO, "Optimized 2 tap filter code cannot be used\n");
  137. c->warned_unuseable_bilinear = 1;
  138. }
  139. inst->yuv2packedX(c, lum_filter + sliceY * lum_fsize,
  140. (const int16_t**)src0, lum_fsize, chr_filter + chrSliceY * chr_fsize,
  141. (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, *dst, dstW, sliceY);
  142. }
  143. return 1;
  144. }
  145. static int any_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  146. {
  147. VScalerContext *inst = desc->instance;
  148. int dstW = desc->dst->width;
  149. int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
  150. int lum_fsize = inst[0].filter_size;
  151. int chr_fsize = inst[1].filter_size;
  152. uint16_t *lum_filter = inst[0].filter[0];
  153. uint16_t *chr_filter = inst[1].filter[0];
  154. int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]);
  155. int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]);
  156. int sp0 = firstLum - desc->src->plane[0].sliceY;
  157. int sp1 = firstChr - desc->src->plane[1].sliceY;
  158. int sp2 = firstChr - desc->src->plane[2].sliceY;
  159. int sp3 = firstLum - desc->src->plane[3].sliceY;
  160. int dp0 = sliceY - desc->dst->plane[0].sliceY;
  161. int dp1 = chrSliceY - desc->dst->plane[1].sliceY;
  162. int dp2 = chrSliceY - desc->dst->plane[2].sliceY;
  163. int dp3 = sliceY - desc->dst->plane[3].sliceY;
  164. uint8_t **src0 = desc->src->plane[0].line + sp0;
  165. uint8_t **src1 = desc->src->plane[1].line + sp1;
  166. uint8_t **src2 = desc->src->plane[2].line + sp2;
  167. uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL;
  168. uint8_t *dst[4] = { desc->dst->plane[0].line[dp0],
  169. desc->dst->plane[1].line[dp1],
  170. desc->dst->plane[2].line[dp2],
  171. desc->alpha ? desc->dst->plane[3].line[dp3] : NULL };
  172. av_assert1(!c->yuv2packed1 && !c->yuv2packed2);
  173. ((yuv2anyX_fn)inst->pfn)(c, lum_filter + sliceY * lum_fsize,
  174. (const int16_t**)src0, lum_fsize, chr_filter + sliceY * chr_fsize,
  175. (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, dst, dstW, sliceY);
  176. return 1;
  177. }
  178. int ff_init_vscale(SwsContext *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst)
  179. {
  180. VScalerContext *lumCtx = NULL;
  181. VScalerContext *chrCtx = NULL;
  182. if (isPlanarYUV(c->dstFormat) || (isGray(c->dstFormat) && !isALPHA(c->dstFormat))) {
  183. lumCtx = av_mallocz(sizeof(VScalerContext));
  184. if (!lumCtx)
  185. return AVERROR(ENOMEM);
  186. desc[0].process = lum_planar_vscale;
  187. desc[0].instance = lumCtx;
  188. desc[0].src = src;
  189. desc[0].dst = dst;
  190. desc[0].alpha = c->needAlpha;
  191. if (!isGray(c->dstFormat)) {
  192. chrCtx = av_mallocz(sizeof(VScalerContext));
  193. if (!chrCtx)
  194. return AVERROR(ENOMEM);
  195. desc[1].process = chr_planar_vscale;
  196. desc[1].instance = chrCtx;
  197. desc[1].src = src;
  198. desc[1].dst = dst;
  199. }
  200. } else {
  201. lumCtx = av_mallocz_array(sizeof(VScalerContext), 2);
  202. if (!lumCtx)
  203. return AVERROR(ENOMEM);
  204. chrCtx = &lumCtx[1];
  205. desc[0].process = c->yuv2packedX ? packed_vscale : any_vscale;
  206. desc[0].instance = lumCtx;
  207. desc[0].src = src;
  208. desc[0].dst = dst;
  209. desc[0].alpha = c->needAlpha;
  210. }
  211. ff_init_vscale_pfn(c, c->yuv2plane1, c->yuv2planeX, c->yuv2nv12cX,
  212. c->yuv2packed1, c->yuv2packed2, c->yuv2packedX, c->yuv2anyX, c->use_mmx_vfilter);
  213. return 0;
  214. }
  215. void ff_init_vscale_pfn(SwsContext *c,
  216. yuv2planar1_fn yuv2plane1,
  217. yuv2planarX_fn yuv2planeX,
  218. yuv2interleavedX_fn yuv2nv12cX,
  219. yuv2packed1_fn yuv2packed1,
  220. yuv2packed2_fn yuv2packed2,
  221. yuv2packedX_fn yuv2packedX,
  222. yuv2anyX_fn yuv2anyX, int use_mmx)
  223. {
  224. VScalerContext *lumCtx = NULL;
  225. VScalerContext *chrCtx = NULL;
  226. int idx = c->numDesc - (c->is_internal_gamma ? 2 : 1); //FIXME avoid hardcoding indexes
  227. if (isPlanarYUV(c->dstFormat) || (isGray(c->dstFormat) && !isALPHA(c->dstFormat))) {
  228. if (!isGray(c->dstFormat)) {
  229. chrCtx = c->desc[idx].instance;
  230. chrCtx->filter[0] = use_mmx ? (int16_t*)c->chrMmxFilter : c->vChrFilter;
  231. chrCtx->filter_size = c->vChrFilterSize;
  232. chrCtx->filter_pos = c->vChrFilterPos;
  233. chrCtx->isMMX = use_mmx;
  234. --idx;
  235. if (yuv2nv12cX) chrCtx->pfn = yuv2nv12cX;
  236. else if (c->vChrFilterSize == 1) chrCtx->pfn = yuv2plane1;
  237. else chrCtx->pfn = yuv2planeX;
  238. }
  239. lumCtx = c->desc[idx].instance;
  240. lumCtx->filter[0] = use_mmx ? (int16_t*)c->lumMmxFilter : c->vLumFilter;
  241. lumCtx->filter[1] = use_mmx ? (int16_t*)c->alpMmxFilter : c->vLumFilter;
  242. lumCtx->filter_size = c->vLumFilterSize;
  243. lumCtx->filter_pos = c->vLumFilterPos;
  244. lumCtx->isMMX = use_mmx;
  245. if (c->vLumFilterSize == 1) lumCtx->pfn = yuv2plane1;
  246. else lumCtx->pfn = yuv2planeX;
  247. } else {
  248. lumCtx = c->desc[idx].instance;
  249. chrCtx = &lumCtx[1];
  250. lumCtx->filter[0] = c->vLumFilter;
  251. lumCtx->filter_size = c->vLumFilterSize;
  252. lumCtx->filter_pos = c->vLumFilterPos;
  253. chrCtx->filter[0] = c->vChrFilter;
  254. chrCtx->filter_size = c->vChrFilterSize;
  255. chrCtx->filter_pos = c->vChrFilterPos;
  256. lumCtx->isMMX = use_mmx;
  257. chrCtx->isMMX = use_mmx;
  258. if (yuv2packedX) {
  259. if (c->yuv2packed1 && c->vLumFilterSize == 1 && c->vChrFilterSize <= 2)
  260. lumCtx->pfn = yuv2packed1;
  261. else if (c->yuv2packed2 && c->vLumFilterSize == 2 && c->vChrFilterSize == 2)
  262. lumCtx->pfn = yuv2packed2;
  263. lumCtx->yuv2packedX = yuv2packedX;
  264. } else
  265. lumCtx->pfn = yuv2anyX;
  266. }
  267. }