vscale.c 13 KB

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