hscale.c 9.3 KB

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