vscale.c 13 KB

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