hscale.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /// Scaler instance data
  22. typedef struct FilterContext
  23. {
  24. uint16_t *filter;
  25. int *filter_pos;
  26. int filter_size;
  27. int xInc;
  28. } FilterContext;
  29. /// Color conversion instance data
  30. typedef struct ColorContext
  31. {
  32. uint32_t *pal;
  33. } ColorContext;
  34. static int lum_h_scale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  35. {
  36. FilterContext *instance = desc->instance;
  37. int srcW = desc->src->width;
  38. int dstW = desc->dst->width;
  39. int xInc = instance->xInc;
  40. int i;
  41. for (i = 0; i < sliceH; ++i) {
  42. uint8_t ** src = desc->src->plane[0].line;
  43. uint8_t ** dst = desc->dst->plane[0].line;
  44. int src_pos = sliceY+i - desc->src->plane[0].sliceY;
  45. int dst_pos = sliceY+i - desc->dst->plane[0].sliceY;
  46. if (c->hyscale_fast) {
  47. c->hyscale_fast(c, (int16_t*)dst[dst_pos], dstW, src[src_pos], srcW, xInc);
  48. } else {
  49. c->hyScale(c, (int16_t*)dst[dst_pos], dstW, (const uint8_t *)src[src_pos], instance->filter,
  50. instance->filter_pos, instance->filter_size);
  51. }
  52. if (c->lumConvertRange)
  53. c->lumConvertRange((int16_t*)dst[dst_pos], dstW);
  54. desc->dst->plane[0].sliceH += 1;
  55. if (desc->alpha) {
  56. src = desc->src->plane[3].line;
  57. dst = desc->dst->plane[3].line;
  58. src_pos = sliceY+i - desc->src->plane[3].sliceY;
  59. dst_pos = sliceY+i - desc->dst->plane[3].sliceY;
  60. desc->dst->plane[3].sliceH += 1;
  61. if (c->hyscale_fast) {
  62. c->hyscale_fast(c, (int16_t*)dst[dst_pos], dstW, src[src_pos], srcW, xInc);
  63. } else {
  64. c->hyScale(c, (int16_t*)dst[dst_pos], dstW, (const uint8_t *)src[src_pos], instance->filter,
  65. instance->filter_pos, instance->filter_size);
  66. }
  67. }
  68. }
  69. return sliceH;
  70. }
  71. static int lum_convert(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  72. {
  73. int srcW = desc->src->width;
  74. ColorContext * instance = desc->instance;
  75. uint32_t * pal = instance->pal;
  76. int i;
  77. desc->dst->plane[0].sliceY = sliceY;
  78. desc->dst->plane[0].sliceH = sliceH;
  79. desc->dst->plane[3].sliceY = sliceY;
  80. desc->dst->plane[3].sliceH = sliceH;
  81. for (i = 0; i < sliceH; ++i) {
  82. int sp0 = sliceY+i - desc->src->plane[0].sliceY;
  83. int sp1 = ((sliceY+i) >> desc->src->v_chr_sub_sample) - desc->src->plane[1].sliceY;
  84. const uint8_t * src[4] = { desc->src->plane[0].line[sp0],
  85. desc->src->plane[1].line[sp1],
  86. desc->src->plane[2].line[sp1],
  87. desc->src->plane[3].line[sp0]};
  88. uint8_t * dst = desc->dst->plane[0].line[i];
  89. if (c->lumToYV12) {
  90. c->lumToYV12(dst, src[0], src[1], src[2], srcW, pal);
  91. } else if (c->readLumPlanar) {
  92. c->readLumPlanar(dst, src, srcW, c->input_rgb2yuv_table);
  93. }
  94. if (desc->alpha) {
  95. dst = desc->dst->plane[3].line[i];
  96. if (c->alpToYV12) {
  97. c->alpToYV12(dst, src[3], src[1], src[2], srcW, pal);
  98. } else if (c->readAlpPlanar) {
  99. c->readAlpPlanar(dst, src, srcW, NULL);
  100. }
  101. }
  102. }
  103. return sliceH;
  104. }
  105. int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal)
  106. {
  107. ColorContext * li = av_malloc(sizeof(ColorContext));
  108. if (!li)
  109. return AVERROR(ENOMEM);
  110. li->pal = pal;
  111. desc->instance = li;
  112. desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
  113. desc->src =src;
  114. desc->dst = dst;
  115. desc->process = &lum_convert;
  116. return 0;
  117. }
  118. int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc)
  119. {
  120. FilterContext *li = av_malloc(sizeof(FilterContext));
  121. if (!li)
  122. return AVERROR(ENOMEM);
  123. li->filter = filter;
  124. li->filter_pos = filter_pos;
  125. li->filter_size = filter_size;
  126. li->xInc = xInc;
  127. desc->instance = li;
  128. desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
  129. desc->src = src;
  130. desc->dst = dst;
  131. desc->process = &lum_h_scale;
  132. return 0;
  133. }
  134. static int chr_h_scale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  135. {
  136. FilterContext *instance = desc->instance;
  137. int srcW = AV_CEIL_RSHIFT(desc->src->width, desc->src->h_chr_sub_sample);
  138. int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample);
  139. int xInc = instance->xInc;
  140. uint8_t ** src1 = desc->src->plane[1].line;
  141. uint8_t ** dst1 = desc->dst->plane[1].line;
  142. uint8_t ** src2 = desc->src->plane[2].line;
  143. uint8_t ** dst2 = desc->dst->plane[2].line;
  144. int src_pos1 = sliceY - desc->src->plane[1].sliceY;
  145. int dst_pos1 = sliceY - desc->dst->plane[1].sliceY;
  146. int src_pos2 = sliceY - desc->src->plane[2].sliceY;
  147. int dst_pos2 = sliceY - desc->dst->plane[2].sliceY;
  148. int i;
  149. for (i = 0; i < sliceH; ++i) {
  150. if (c->hcscale_fast) {
  151. c->hcscale_fast(c, (uint16_t*)dst1[dst_pos1+i], (uint16_t*)dst2[dst_pos2+i], dstW, src1[src_pos1+i], src2[src_pos2+i], srcW, xInc);
  152. } else {
  153. c->hcScale(c, (uint16_t*)dst1[dst_pos1+i], dstW, src1[src_pos1+i], instance->filter, instance->filter_pos, instance->filter_size);
  154. c->hcScale(c, (uint16_t*)dst2[dst_pos2+i], dstW, src2[src_pos2+i], instance->filter, instance->filter_pos, instance->filter_size);
  155. }
  156. if (c->chrConvertRange)
  157. c->chrConvertRange((uint16_t*)dst1[dst_pos1+i], (uint16_t*)dst2[dst_pos2+i], dstW);
  158. desc->dst->plane[1].sliceH += 1;
  159. desc->dst->plane[2].sliceH += 1;
  160. }
  161. return sliceH;
  162. }
  163. static int chr_convert(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  164. {
  165. int srcW = AV_CEIL_RSHIFT(desc->src->width, desc->src->h_chr_sub_sample);
  166. ColorContext * instance = desc->instance;
  167. uint32_t * pal = instance->pal;
  168. int sp0 = (sliceY - (desc->src->plane[0].sliceY >> desc->src->v_chr_sub_sample)) << desc->src->v_chr_sub_sample;
  169. int sp1 = sliceY - desc->src->plane[1].sliceY;
  170. int i;
  171. desc->dst->plane[1].sliceY = sliceY;
  172. desc->dst->plane[1].sliceH = sliceH;
  173. desc->dst->plane[2].sliceY = sliceY;
  174. desc->dst->plane[2].sliceH = sliceH;
  175. for (i = 0; i < sliceH; ++i) {
  176. const uint8_t * src[4] = { desc->src->plane[0].line[sp0+i],
  177. desc->src->plane[1].line[sp1+i],
  178. desc->src->plane[2].line[sp1+i],
  179. desc->src->plane[3].line[sp0+i]};
  180. uint8_t * dst1 = desc->dst->plane[1].line[i];
  181. uint8_t * dst2 = desc->dst->plane[2].line[i];
  182. if (c->chrToYV12) {
  183. c->chrToYV12(dst1, dst2, src[0], src[1], src[2], srcW, pal);
  184. } else if (c->readChrPlanar) {
  185. c->readChrPlanar(dst1, dst2, src, srcW, c->input_rgb2yuv_table);
  186. }
  187. }
  188. return sliceH;
  189. }
  190. int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal)
  191. {
  192. ColorContext * li = av_malloc(sizeof(ColorContext));
  193. if (!li)
  194. return AVERROR(ENOMEM);
  195. li->pal = pal;
  196. desc->instance = li;
  197. desc->src =src;
  198. desc->dst = dst;
  199. desc->process = &chr_convert;
  200. return 0;
  201. }
  202. int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc)
  203. {
  204. FilterContext *li = av_malloc(sizeof(FilterContext));
  205. if (!li)
  206. return AVERROR(ENOMEM);
  207. li->filter = filter;
  208. li->filter_pos = filter_pos;
  209. li->filter_size = filter_size;
  210. li->xInc = xInc;
  211. desc->instance = li;
  212. desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
  213. desc->src = src;
  214. desc->dst = dst;
  215. desc->process = &chr_h_scale;
  216. return 0;
  217. }
  218. static int no_chr_scale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
  219. {
  220. desc->dst->plane[1].sliceY = sliceY + sliceH - desc->dst->plane[1].available_lines;
  221. desc->dst->plane[1].sliceH = desc->dst->plane[1].available_lines;
  222. desc->dst->plane[2].sliceY = sliceY + sliceH - desc->dst->plane[2].available_lines;
  223. desc->dst->plane[2].sliceH = desc->dst->plane[2].available_lines;
  224. return 0;
  225. }
  226. int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst)
  227. {
  228. desc->src = src;
  229. desc->dst = dst;
  230. desc->alpha = 0;
  231. desc->instance = NULL;
  232. desc->process = &no_chr_scale;
  233. return 0;
  234. }