You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

275 lines
8.9KB

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