hscale.c 9.5 KB

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