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.

330 lines
9.6KB

  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 void free_lines(SwsSlice *s)
  22. {
  23. int i;
  24. for (i = 0; i < 2; ++i) {
  25. int n = s->plane[i].available_lines;
  26. int j;
  27. for (j = 0; j < n; ++j) {
  28. av_freep(&s->plane[i].line[j]);
  29. if (s->is_ring)
  30. s->plane[i].line[j+n] = NULL;
  31. }
  32. }
  33. for (i = 0; i < 4; ++i)
  34. memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
  35. s->should_free_lines = 0;
  36. }
  37. /*
  38. slice lines contains extra bytes for vetorial code thus @size
  39. is the allocated memory size and @width is the number of pixels
  40. */
  41. static int alloc_lines(SwsSlice *s, int size, int width)
  42. {
  43. int i;
  44. int idx[2] = {3, 2};
  45. s->should_free_lines = 1;
  46. s->width = width;
  47. for (i = 0; i < 2; ++i) {
  48. int n = s->plane[i].available_lines;
  49. int j;
  50. int ii = idx[i];
  51. av_assert0(n == s->plane[ii].available_lines);
  52. for (j = 0; j < n; ++j) {
  53. // chroma plane line U and V are expected to be contiguous in memory
  54. // by mmx vertical scaler code
  55. s->plane[i].line[j] = av_malloc(size * 2 + 32);
  56. if (!s->plane[i].line[j]) {
  57. free_lines(s);
  58. return AVERROR(ENOMEM);
  59. }
  60. s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
  61. if (s->is_ring) {
  62. s->plane[i].line[j+n] = s->plane[i].line[j];
  63. s->plane[ii].line[j+n] = s->plane[ii].line[j];
  64. }
  65. }
  66. }
  67. return 0;
  68. }
  69. static int alloc_slice(SwsSlice *s, enum AVPixelFormat fmt, int lumLines, int chrLines, int h_sub_sample, int v_sub_sample, int ring)
  70. {
  71. int i;
  72. int size[4] = { lumLines,
  73. chrLines,
  74. chrLines,
  75. lumLines };
  76. s->h_chr_sub_sample = h_sub_sample;
  77. s->v_chr_sub_sample = v_sub_sample;
  78. s->fmt = fmt;
  79. s->is_ring = ring;
  80. s->should_free_lines = 0;
  81. for (i = 0; i < 4; ++i) {
  82. int n = size[i] * ( ring == 0 ? 1 : 3);
  83. s->plane[i].line = av_mallocz_array(sizeof(uint8_t*), n);
  84. if (!s->plane[i].line)
  85. return AVERROR(ENOMEM);
  86. s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
  87. s->plane[i].available_lines = size[i];
  88. s->plane[i].sliceY = 0;
  89. s->plane[i].sliceH = 0;
  90. }
  91. return 0;
  92. }
  93. static void free_slice(SwsSlice *s)
  94. {
  95. int i;
  96. if (s) {
  97. if (s->should_free_lines)
  98. free_lines(s);
  99. for (i = 0; i < 4; ++i) {
  100. av_freep(&s->plane[i].line);
  101. s->plane[i].tmp = NULL;
  102. }
  103. }
  104. }
  105. int ff_rotate_slice(SwsSlice *s, int lum, int chr)
  106. {
  107. int i;
  108. if (lum) {
  109. for (i = 0; i < 4; i+=3) {
  110. int n = s->plane[i].available_lines;
  111. int l = lum - s->plane[i].sliceY;
  112. if (l >= n * 2) {
  113. s->plane[i].sliceY += n;
  114. s->plane[i].sliceH -= n;
  115. }
  116. }
  117. }
  118. if (chr) {
  119. for (i = 1; i < 3; ++i) {
  120. int n = s->plane[i].available_lines;
  121. int l = chr - s->plane[i].sliceY;
  122. if (l >= n * 2) {
  123. s->plane[i].sliceY += n;
  124. s->plane[i].sliceH -= n;
  125. }
  126. }
  127. }
  128. return 0;
  129. }
  130. int ff_init_slice_from_src(SwsSlice * s, uint8_t *src[4], int stride[4], int srcW, int lumY, int lumH, int chrY, int chrH)
  131. {
  132. int i = 0;
  133. const int start[4] = {lumY,
  134. chrY,
  135. chrY,
  136. lumY};
  137. const int end[4] = {lumY +lumH,
  138. chrY + chrH,
  139. chrY + chrH,
  140. lumY + lumH};
  141. s->width = srcW;
  142. for (i = 0; i < 4; ++i) {
  143. int j;
  144. int lines = end[i];
  145. lines = s->plane[i].available_lines < lines ? s->plane[i].available_lines : lines;
  146. if (end[i] > s->plane[i].sliceY+s->plane[i].sliceH) {
  147. if (start[i] <= s->plane[i].sliceY+1)
  148. s->plane[i].sliceY = FFMIN(start[i], s->plane[i].sliceY);
  149. else
  150. s->plane[i].sliceY = start[i];
  151. s->plane[i].sliceH = end[i] - s->plane[i].sliceY;
  152. } else {
  153. if (end[i] >= s->plane[i].sliceY)
  154. s->plane[i].sliceH = s->plane[i].sliceY + s->plane[i].sliceH - start[i];
  155. else
  156. s->plane[i].sliceH = end[i] - start[i];
  157. s->plane[i].sliceY = start[i];
  158. }
  159. for (j = start[i]; j < lines; j+= 1)
  160. s->plane[i].line[j] = src[i] + (start[i] + j) * stride[i];
  161. }
  162. return 0;
  163. }
  164. static void fill_ones(SwsSlice *s, int n, int is16bit)
  165. {
  166. int i;
  167. for (i = 0; i < 4; ++i) {
  168. int j;
  169. int size = s->plane[i].available_lines;
  170. for (j = 0; j < size; ++j) {
  171. int k;
  172. int end = is16bit ? n>>1: n;
  173. // fill also one extra element
  174. end += 1;
  175. if (is16bit)
  176. for (k = 0; k < end; ++k)
  177. ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
  178. else
  179. for (k = 0; k < end; ++k)
  180. ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
  181. }
  182. }
  183. }
  184. int ff_init_filters(SwsContext * c)
  185. {
  186. int i;
  187. int index;
  188. int num_ydesc;
  189. int num_cdesc;
  190. int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
  191. int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
  192. int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
  193. int srcIdx, dstIdx;
  194. int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
  195. uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
  196. int res = 0;
  197. if (c->dstBpc == 16)
  198. dst_stride <<= 1;
  199. num_ydesc = need_lum_conv ? 2 : 1;
  200. num_cdesc = need_chr_conv ? 2 : 1;
  201. c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
  202. c->numDesc = num_ydesc + num_cdesc + num_vdesc;
  203. c->descIndex[0] = num_ydesc;
  204. c->descIndex[1] = num_ydesc + num_cdesc;
  205. c->desc = av_mallocz_array(sizeof(SwsFilterDescriptor), c->numDesc);
  206. if (!c->desc)
  207. return AVERROR(ENOMEM);
  208. c->slice = av_mallocz_array(sizeof(SwsSlice), c->numSlice);
  209. res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
  210. if (res < 0) goto cleanup;
  211. for (i = 1; i < c->numSlice-2; ++i) {
  212. res = alloc_slice(&c->slice[i], c->srcFormat, c->vLumFilterSize + MAX_LINES_AHEAD, c->vChrFilterSize + MAX_LINES_AHEAD, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
  213. if (res < 0) goto cleanup;
  214. res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
  215. if (res < 0) goto cleanup;
  216. }
  217. // horizontal scaler output
  218. res = alloc_slice(&c->slice[i], c->srcFormat, c->vLumFilterSize + MAX_LINES_AHEAD, c->vChrFilterSize + MAX_LINES_AHEAD, c->chrDstHSubSample, c->chrDstVSubSample, 1);
  219. if (res < 0) goto cleanup;
  220. res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
  221. if (res < 0) goto cleanup;
  222. fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc == 16);
  223. // vertical scaler output
  224. ++i;
  225. res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
  226. if (res < 0) goto cleanup;
  227. index = 0;
  228. srcIdx = 0;
  229. dstIdx = 1;
  230. if (need_lum_conv) {
  231. ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
  232. c->desc[index].alpha = c->alpPixBuf != 0;
  233. ++index;
  234. srcIdx = dstIdx;
  235. }
  236. dstIdx = FFMAX(num_ydesc, num_cdesc);
  237. ff_init_desc_hscale(&c->desc[index], &c->slice[index], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
  238. c->desc[index].alpha = c->alpPixBuf != 0;
  239. ++index;
  240. {
  241. srcIdx = 0;
  242. dstIdx = 1;
  243. if (need_chr_conv) {
  244. ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
  245. ++index;
  246. srcIdx = dstIdx;
  247. }
  248. dstIdx = FFMAX(num_ydesc, num_cdesc);
  249. if (c->needs_hcscale)
  250. ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
  251. else
  252. ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
  253. }
  254. ++index;
  255. {
  256. srcIdx = c->numSlice - 2;
  257. dstIdx = c->numSlice - 1;
  258. ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
  259. }
  260. return 0;
  261. cleanup:
  262. ff_free_filters(c);
  263. return res;
  264. }
  265. int ff_free_filters(SwsContext *c)
  266. {
  267. int i;
  268. if (c->desc) {
  269. for (i = 0; i < c->numDesc; ++i)
  270. av_freep(&c->desc[i].instance);
  271. av_freep(&c->desc);
  272. }
  273. if (c->slice) {
  274. for (i = 0; i < c->numSlice; ++i)
  275. free_slice(&c->slice[i]);
  276. av_freep(&c->slice);
  277. }
  278. return 0;
  279. }