2 * Copyright (C) 2015 Pedro Arthur <bygrandao@gmail.com>
4 * This file is part of FFmpeg.
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.
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.
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
21 #include "swscale_internal.h"
23 static void free_lines(SwsSlice *s)
26 for (i = 0; i < 2; ++i) {
27 int n = s->plane[i].available_lines;
29 for (j = 0; j < n; ++j) {
30 av_freep(&s->plane[i].line[j]);
32 s->plane[i].line[j+n] = NULL;
36 for (i = 0; i < 4; ++i)
37 memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
38 s->should_free_lines = 0;
42 slice lines contains extra bytes for vectorial code thus @size
43 is the allocated memory size and @width is the number of pixels
45 static int alloc_lines(SwsSlice *s, int size, int width)
50 s->should_free_lines = 1;
53 for (i = 0; i < 2; ++i) {
54 int n = s->plane[i].available_lines;
58 av_assert0(n == s->plane[ii].available_lines);
59 for (j = 0; j < n; ++j) {
60 // chroma plane line U and V are expected to be contiguous in memory
61 // by mmx vertical scaler code
62 s->plane[i].line[j] = av_malloc(size * 2 + 32);
63 if (!s->plane[i].line[j]) {
65 return AVERROR(ENOMEM);
67 s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
69 s->plane[i].line[j+n] = s->plane[i].line[j];
70 s->plane[ii].line[j+n] = s->plane[ii].line[j];
78 static int alloc_slice(SwsSlice *s, enum AVPixelFormat fmt, int lumLines, int chrLines, int h_sub_sample, int v_sub_sample, int ring)
81 int size[4] = { lumLines,
86 s->h_chr_sub_sample = h_sub_sample;
87 s->v_chr_sub_sample = v_sub_sample;
90 s->should_free_lines = 0;
92 for (i = 0; i < 4; ++i) {
93 int n = size[i] * ( ring == 0 ? 1 : 3);
94 s->plane[i].line = av_mallocz_array(sizeof(uint8_t*), n);
95 if (!s->plane[i].line)
96 return AVERROR(ENOMEM);
98 s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
99 s->plane[i].available_lines = size[i];
100 s->plane[i].sliceY = 0;
101 s->plane[i].sliceH = 0;
106 static void free_slice(SwsSlice *s)
110 if (s->should_free_lines)
112 for (i = 0; i < 4; ++i) {
113 av_freep(&s->plane[i].line);
114 s->plane[i].tmp = NULL;
119 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
123 for (i = 0; i < 4; i+=3) {
124 int n = s->plane[i].available_lines;
125 int l = lum - s->plane[i].sliceY;
128 s->plane[i].sliceY += n;
129 s->plane[i].sliceH -= n;
134 for (i = 1; i < 3; ++i) {
135 int n = s->plane[i].available_lines;
136 int l = chr - s->plane[i].sliceY;
139 s->plane[i].sliceY += n;
140 s->plane[i].sliceH -= n;
147 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, int relative)
151 const int start[4] = {lumY,
156 const int end[4] = {lumY +lumH,
161 const uint8_t *src_[4] = {src[0] + (relative ? 0 : start[0]) * stride[0],
162 src[1] + (relative ? 0 : start[1]) * stride[1],
163 src[2] + (relative ? 0 : start[2]) * stride[2],
164 src[3] + (relative ? 0 : start[3]) * stride[3]};
168 for (i = 0; i < 4; ++i) {
170 int first = s->plane[i].sliceY;
171 int n = s->plane[i].available_lines;
172 int lines = end[i] - start[i];
173 int tot_lines = end[i] - first;
175 if (start[i] >= first && n >= tot_lines) {
176 s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
177 for (j = 0; j < lines; j+= 1)
178 s->plane[i].line[start[i] - first + j] = src_[i] + j * stride[i];
180 s->plane[i].sliceY = start[i];
181 lines = lines > n ? n : lines;
182 s->plane[i].sliceH = lines;
183 for (j = 0; j < lines; j+= 1)
184 s->plane[i].line[j] = src_[i] + j * stride[i];
192 static void fill_ones(SwsSlice *s, int n, int is16bit)
195 for (i = 0; i < 4; ++i) {
197 int size = s->plane[i].available_lines;
198 for (j = 0; j < size; ++j) {
200 int end = is16bit ? n>>1: n;
201 // fill also one extra element
204 for (k = 0; k < end; ++k)
205 ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
207 for (k = 0; k < end; ++k)
208 ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
214 Calculates the minimum ring buffer size, it should be able to store vFilterSize
215 more n lines where n is the max difference between each adjacent slice which
217 The n lines are needed only when there is not enough src lines to output a single
218 dst line, then we should buffer these lines to process them on the next call to scale.
220 static void get_min_buffer_size(SwsContext *c, int *out_lum_size, int *out_chr_size)
224 int chrDstH = c->chrDstH;
225 int *lumFilterPos = c->vLumFilterPos;
226 int *chrFilterPos = c->vChrFilterPos;
227 int lumFilterSize = c->vLumFilterSize;
228 int chrFilterSize = c->vChrFilterSize;
229 int chrSubSample = c->chrSrcVSubSample;
231 *out_lum_size = lumFilterSize;
232 *out_chr_size = chrFilterSize;
234 for (lumY = 0; lumY < dstH; lumY++) {
235 int chrY = (int64_t)lumY * chrDstH / dstH;
236 int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
237 ((chrFilterPos[chrY] + chrFilterSize - 1)
240 nextSlice >>= chrSubSample;
241 nextSlice <<= chrSubSample;
242 (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
243 (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
249 int ff_init_filters(SwsContext * c)
255 int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
256 int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
257 int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
258 int need_gamma = c->is_internal_gamma;
260 int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
262 uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
268 get_min_buffer_size(c, &lumBufSize, &chrBufSize);
269 lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
270 chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
275 num_ydesc = need_lum_conv ? 2 : 1;
276 num_cdesc = need_chr_conv ? 2 : 1;
278 c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
279 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
280 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
281 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
285 c->desc = av_mallocz_array(sizeof(SwsFilterDescriptor), c->numDesc);
287 return AVERROR(ENOMEM);
288 c->slice = av_mallocz_array(sizeof(SwsSlice), c->numSlice);
291 res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
292 if (res < 0) goto cleanup;
293 for (i = 1; i < c->numSlice-2; ++i) {
294 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
295 if (res < 0) goto cleanup;
296 res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
297 if (res < 0) goto cleanup;
299 // horizontal scaler output
300 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
301 if (res < 0) goto cleanup;
302 res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
303 if (res < 0) goto cleanup;
305 fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc == 16);
307 // vertical scaler output
309 res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
310 if (res < 0) goto cleanup;
317 res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
318 if (res < 0) goto cleanup;
323 res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
324 if (res < 0) goto cleanup;
325 c->desc[index].alpha = c->needAlpha;
331 dstIdx = FFMAX(num_ydesc, num_cdesc);
332 res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
333 if (res < 0) goto cleanup;
334 c->desc[index].alpha = c->needAlpha;
342 res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
343 if (res < 0) goto cleanup;
348 dstIdx = FFMAX(num_ydesc, num_cdesc);
349 if (c->needs_hcscale)
350 res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
352 res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
353 if (res < 0) goto cleanup;
358 srcIdx = c->numSlice - 2;
359 dstIdx = c->numSlice - 1;
360 res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
361 if (res < 0) goto cleanup;
366 res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
367 if (res < 0) goto cleanup;
377 int ff_free_filters(SwsContext *c)
381 for (i = 0; i < c->numDesc; ++i)
382 av_freep(&c->desc[i].instance);
387 for (i = 0; i < c->numSlice; ++i)
388 free_slice(&c->slice[i]);