slice.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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. static void free_lines(SwsSlice *s)
  23. {
  24. int i;
  25. for (i = 0; i < 2; ++i) {
  26. int n = s->plane[i].available_lines;
  27. int j;
  28. for (j = 0; j < n; ++j) {
  29. av_freep(&s->plane[i].line[j]);
  30. if (s->is_ring)
  31. s->plane[i].line[j+n] = NULL;
  32. }
  33. }
  34. for (i = 0; i < 4; ++i)
  35. memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
  36. s->should_free_lines = 0;
  37. }
  38. /*
  39. slice lines contains extra bytes for vectorial code thus @size
  40. is the allocated memory size and @width is the number of pixels
  41. */
  42. static int alloc_lines(SwsSlice *s, int size, int width)
  43. {
  44. int i;
  45. int idx[2] = {3, 2};
  46. s->should_free_lines = 1;
  47. s->width = width;
  48. for (i = 0; i < 2; ++i) {
  49. int n = s->plane[i].available_lines;
  50. int j;
  51. int ii = idx[i];
  52. av_assert0(n == s->plane[ii].available_lines);
  53. for (j = 0; j < n; ++j) {
  54. // chroma plane line U and V are expected to be contiguous in memory
  55. // by mmx vertical scaler code
  56. s->plane[i].line[j] = av_malloc(size * 2 + 32);
  57. if (!s->plane[i].line[j]) {
  58. free_lines(s);
  59. return AVERROR(ENOMEM);
  60. }
  61. s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
  62. if (s->is_ring) {
  63. s->plane[i].line[j+n] = s->plane[i].line[j];
  64. s->plane[ii].line[j+n] = s->plane[ii].line[j];
  65. }
  66. }
  67. }
  68. return 0;
  69. }
  70. static int alloc_slice(SwsSlice *s, enum AVPixelFormat fmt, int lumLines, int chrLines, int h_sub_sample, int v_sub_sample, int ring)
  71. {
  72. int i;
  73. int size[4] = { lumLines,
  74. chrLines,
  75. chrLines,
  76. lumLines };
  77. s->h_chr_sub_sample = h_sub_sample;
  78. s->v_chr_sub_sample = v_sub_sample;
  79. s->fmt = fmt;
  80. s->is_ring = ring;
  81. s->should_free_lines = 0;
  82. for (i = 0; i < 4; ++i) {
  83. int n = size[i] * ( ring == 0 ? 1 : 3);
  84. s->plane[i].line = av_calloc(n, sizeof(*s->plane[i].line));
  85. if (!s->plane[i].line)
  86. return AVERROR(ENOMEM);
  87. s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
  88. s->plane[i].available_lines = size[i];
  89. s->plane[i].sliceY = 0;
  90. s->plane[i].sliceH = 0;
  91. }
  92. return 0;
  93. }
  94. static void free_slice(SwsSlice *s)
  95. {
  96. int i;
  97. if (s) {
  98. if (s->should_free_lines)
  99. free_lines(s);
  100. for (i = 0; i < 4; ++i) {
  101. av_freep(&s->plane[i].line);
  102. s->plane[i].tmp = NULL;
  103. }
  104. }
  105. }
  106. int ff_rotate_slice(SwsSlice *s, int lum, int chr)
  107. {
  108. int i;
  109. if (lum) {
  110. for (i = 0; i < 4; i+=3) {
  111. int n = s->plane[i].available_lines;
  112. int l = lum - s->plane[i].sliceY;
  113. if (l >= n * 2) {
  114. s->plane[i].sliceY += n;
  115. s->plane[i].sliceH -= n;
  116. }
  117. }
  118. }
  119. if (chr) {
  120. for (i = 1; i < 3; ++i) {
  121. int n = s->plane[i].available_lines;
  122. int l = chr - s->plane[i].sliceY;
  123. if (l >= n * 2) {
  124. s->plane[i].sliceY += n;
  125. s->plane[i].sliceH -= n;
  126. }
  127. }
  128. }
  129. return 0;
  130. }
  131. int ff_init_slice_from_src(SwsSlice * s, uint8_t *const src[4], const int stride[4],
  132. int srcW, int lumY, int lumH, int chrY, int chrH, int relative)
  133. {
  134. int i = 0;
  135. const int start[4] = {lumY,
  136. chrY,
  137. chrY,
  138. lumY};
  139. const int end[4] = {lumY +lumH,
  140. chrY + chrH,
  141. chrY + chrH,
  142. lumY + lumH};
  143. s->width = srcW;
  144. for (i = 0; i < 4 && src[i] != NULL; ++i) {
  145. uint8_t *const src_i = src[i] + (relative ? 0 : start[i]) * stride[i];
  146. int j;
  147. int first = s->plane[i].sliceY;
  148. int n = s->plane[i].available_lines;
  149. int lines = end[i] - start[i];
  150. int tot_lines = end[i] - first;
  151. if (start[i] >= first && n >= tot_lines) {
  152. s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
  153. for (j = 0; j < lines; j+= 1)
  154. s->plane[i].line[start[i] - first + j] = src_i + j * stride[i];
  155. } else {
  156. s->plane[i].sliceY = start[i];
  157. lines = lines > n ? n : lines;
  158. s->plane[i].sliceH = lines;
  159. for (j = 0; j < lines; j+= 1)
  160. s->plane[i].line[j] = src_i + j * stride[i];
  161. }
  162. }
  163. return 0;
  164. }
  165. static void fill_ones(SwsSlice *s, int n, int bpc)
  166. {
  167. int i, j, k, size, end;
  168. for (i = 0; i < 4; ++i) {
  169. size = s->plane[i].available_lines;
  170. for (j = 0; j < size; ++j) {
  171. if (bpc == 16) {
  172. end = (n>>1) + 1;
  173. for (k = 0; k < end; ++k)
  174. ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
  175. } else if (bpc == 32) {
  176. end = (n>>2) + 1;
  177. for (k = 0; k < end; ++k)
  178. ((int64_t*)(s->plane[i].line[j]))[k] = 1LL<<34;
  179. } else {
  180. end = n + 1;
  181. for (k = 0; k < end; ++k)
  182. ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
  183. }
  184. }
  185. }
  186. }
  187. /*
  188. Calculates the minimum ring buffer size, it should be able to store vFilterSize
  189. more n lines where n is the max difference between each adjacent slice which
  190. outputs a line.
  191. The n lines are needed only when there is not enough src lines to output a single
  192. dst line, then we should buffer these lines to process them on the next call to scale.
  193. */
  194. static void get_min_buffer_size(SwsInternal *c, int *out_lum_size, int *out_chr_size)
  195. {
  196. int lumY;
  197. int dstH = c->dstH;
  198. int chrDstH = c->chrDstH;
  199. int *lumFilterPos = c->vLumFilterPos;
  200. int *chrFilterPos = c->vChrFilterPos;
  201. int lumFilterSize = c->vLumFilterSize;
  202. int chrFilterSize = c->vChrFilterSize;
  203. int chrSubSample = c->chrSrcVSubSample;
  204. *out_lum_size = lumFilterSize;
  205. *out_chr_size = chrFilterSize;
  206. for (lumY = 0; lumY < dstH; lumY++) {
  207. int chrY = (int64_t)lumY * chrDstH / dstH;
  208. int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
  209. ((chrFilterPos[chrY] + chrFilterSize - 1)
  210. << chrSubSample));
  211. nextSlice >>= chrSubSample;
  212. nextSlice <<= chrSubSample;
  213. (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
  214. (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
  215. }
  216. }
  217. int ff_init_filters(SwsInternal * c)
  218. {
  219. int i;
  220. int index;
  221. int num_ydesc;
  222. int num_cdesc;
  223. int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
  224. int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
  225. int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
  226. int need_gamma = c->is_internal_gamma;
  227. int srcIdx, dstIdx;
  228. int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
  229. uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
  230. int res = 0;
  231. int lumBufSize;
  232. int chrBufSize;
  233. get_min_buffer_size(c, &lumBufSize, &chrBufSize);
  234. lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
  235. chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
  236. if (c->dstBpc == 16)
  237. dst_stride <<= 1;
  238. if (c->dstBpc == 32)
  239. dst_stride <<= 2;
  240. num_ydesc = need_lum_conv ? 2 : 1;
  241. num_cdesc = need_chr_conv ? 2 : 1;
  242. c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
  243. c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
  244. c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
  245. c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
  246. if (isFloat16(c->srcFormat)) {
  247. c->h2f_tables = av_malloc(sizeof(*c->h2f_tables));
  248. if (!c->h2f_tables)
  249. return AVERROR(ENOMEM);
  250. ff_init_half2float_tables(c->h2f_tables);
  251. c->input_opaque = c->h2f_tables;
  252. }
  253. c->desc = av_calloc(c->numDesc, sizeof(*c->desc));
  254. if (!c->desc)
  255. return AVERROR(ENOMEM);
  256. c->slice = av_calloc(c->numSlice, sizeof(*c->slice));
  257. if (!c->slice) {
  258. res = AVERROR(ENOMEM);
  259. goto cleanup;
  260. }
  261. res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
  262. if (res < 0) goto cleanup;
  263. for (i = 1; i < c->numSlice-2; ++i) {
  264. res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
  265. if (res < 0) goto cleanup;
  266. res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
  267. if (res < 0) goto cleanup;
  268. }
  269. // horizontal scaler output
  270. res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
  271. if (res < 0) goto cleanup;
  272. res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
  273. if (res < 0) goto cleanup;
  274. fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc);
  275. // vertical scaler output
  276. ++i;
  277. res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
  278. if (res < 0) goto cleanup;
  279. index = 0;
  280. srcIdx = 0;
  281. dstIdx = 1;
  282. if (need_gamma) {
  283. res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
  284. if (res < 0) goto cleanup;
  285. ++index;
  286. }
  287. if (need_lum_conv) {
  288. res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
  289. if (res < 0) goto cleanup;
  290. c->desc[index].alpha = c->needAlpha;
  291. ++index;
  292. srcIdx = dstIdx;
  293. }
  294. dstIdx = FFMAX(num_ydesc, num_cdesc);
  295. res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
  296. if (res < 0) goto cleanup;
  297. c->desc[index].alpha = c->needAlpha;
  298. ++index;
  299. {
  300. srcIdx = 0;
  301. dstIdx = 1;
  302. if (need_chr_conv) {
  303. res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
  304. if (res < 0) goto cleanup;
  305. ++index;
  306. srcIdx = dstIdx;
  307. }
  308. dstIdx = FFMAX(num_ydesc, num_cdesc);
  309. if (c->needs_hcscale)
  310. res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
  311. else
  312. res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
  313. if (res < 0) goto cleanup;
  314. }
  315. ++index;
  316. {
  317. srcIdx = c->numSlice - 2;
  318. dstIdx = c->numSlice - 1;
  319. res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
  320. if (res < 0) goto cleanup;
  321. }
  322. ++index;
  323. if (need_gamma) {
  324. res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
  325. if (res < 0) goto cleanup;
  326. }
  327. return 0;
  328. cleanup:
  329. ff_free_filters(c);
  330. return res;
  331. }
  332. int ff_free_filters(SwsInternal *c)
  333. {
  334. int i;
  335. if (c->desc) {
  336. for (i = 0; i < c->numDesc; ++i)
  337. av_freep(&c->desc[i].instance);
  338. av_freep(&c->desc);
  339. }
  340. if (c->slice) {
  341. for (i = 0; i < c->numSlice; ++i)
  342. free_slice(&c->slice[i]);
  343. av_freep(&c->slice);
  344. }
  345. av_freep(&c->h2f_tables);
  346. return 0;
  347. }