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
22
24 {
26 for (
i = 0;
i < 2; ++
i) {
27 int n =
s->plane[
i].available_lines;
28 int j;
29 for (j = 0; j < n; ++j) {
32 s->plane[
i].line[j+n] =
NULL;
33 }
34 }
35
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;
39 }
40
41 /*
42 slice lines contains extra bytes for vectorial code thus @size
43 is the allocated memory size and @width is the number of pixels
44 */
46 {
48 int idx[2] = {3, 2};
49
50 s->should_free_lines = 1;
52
53 for (
i = 0;
i < 2; ++
i) {
54 int n =
s->plane[
i].available_lines;
55 int j;
57
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
63 if (!
s->plane[
i].line[j]) {
66 }
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];
71 }
72 }
73 }
74
75 return 0;
76 }
77
79 {
81 int size[4] = { lumLines,
82 chrLines,
83 chrLines,
84 lumLines };
85
86 s->h_chr_sub_sample = h_sub_sample;
87 s->v_chr_sub_sample = v_sub_sample;
90 s->should_free_lines = 0;
91
92 for (
i = 0;
i < 4; ++
i) {
93 int n =
size[
i] * ( ring == 0 ? 1 : 3);
95 if (!
s->plane[
i].line)
97
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;
102 }
103 return 0;
104 }
105
107 {
110 if (
s->should_free_lines)
112 for (
i = 0;
i < 4; ++
i) {
115 }
116 }
117 }
118
120 {
123 for (
i = 0;
i < 4;
i+=3) {
124 int n =
s->plane[
i].available_lines;
125 int l =
lum -
s->plane[
i].sliceY;
126
127 if (l >= n * 2) {
128 s->plane[
i].sliceY += n;
129 s->plane[
i].sliceH -= n;
130 }
131 }
132 }
133 if (chr) {
134 for (
i = 1;
i < 3; ++
i) {
135 int n =
s->plane[
i].available_lines;
136 int l = chr -
s->plane[
i].sliceY;
137
138 if (l >= n * 2) {
139 s->plane[
i].sliceY += n;
140 s->plane[
i].sliceH -= n;
141 }
142 }
143 }
144 return 0;
145 }
146
148 {
150
151 const int start[4] = {lumY,
152 chrY,
153 chrY,
154 lumY};
155
156 const int end[4] = {lumY +lumH,
157 chrY + chrH,
158 chrY + chrH,
159 lumY + lumH};
160
162
164 uint8_t *
const src_i =
src[
i] + (relative ? 0 : start[
i]) *
stride[
i];
165 int j;
166 int first =
s->plane[
i].sliceY;
167 int n =
s->plane[
i].available_lines;
168 int lines = end[
i] - start[
i];
169 int tot_lines = end[
i] -
first;
170
171 if (start[
i] >=
first && n >= tot_lines) {
172 s->plane[
i].sliceH =
FFMAX(tot_lines,
s->plane[
i].sliceH);
173 for (j = 0; j < lines; j+= 1)
175 } else {
176 s->plane[
i].sliceY = start[
i];
177 lines = lines > n ? n : lines;
178 s->plane[
i].sliceH = lines;
179 for (j = 0; j < lines; j+= 1)
180 s->plane[
i].line[j] = src_i + j *
stride[
i];
181 }
182
183 }
184
185 return 0;
186 }
187
189 {
190 int i, j, k,
size, end;
191
192 for (
i = 0;
i < 4; ++
i) {
193 size =
s->plane[
i].available_lines;
194 for (j = 0; j <
size; ++j) {
195 if (bpc == 16) {
196 end = (n>>1) + 1;
197 for (k = 0; k < end; ++k)
198 ((
int32_t*)(
s->plane[
i].line[j]))[k] = 1<<18;
199 } else if (bpc == 32) {
200 end = (n>>2) + 1;
201 for (k = 0; k < end; ++k)
202 ((int64_t*)(
s->plane[
i].line[j]))[k] = 1LL<<34;
203 } else {
204 end = n + 1;
205 for (k = 0; k < end; ++k)
206 ((int16_t*)(
s->plane[
i].line[j]))[k] = 1<<14;
207 }
208 }
209 }
210 }
211
212 /*
213 Calculates the minimum ring buffer size, it should be able to store vFilterSize
214 more n lines where n is the max difference between each adjacent slice which
215 outputs a line.
216 The n lines are needed only when there is not enough src lines to output a single
217 dst line, then we should buffer these lines to process them on the next call to scale.
218 */
220 {
221 int lumY;
223 int chrDstH =
c->chrDstH;
224 int *lumFilterPos =
c->vLumFilterPos;
225 int *chrFilterPos =
c->vChrFilterPos;
226 int lumFilterSize =
c->vLumFilterSize;
227 int chrFilterSize =
c->vChrFilterSize;
228 int chrSubSample =
c->chrSrcVSubSample;
229
230 *out_lum_size = lumFilterSize;
231 *out_chr_size = chrFilterSize;
232
233 for (lumY = 0; lumY < dstH; lumY++) {
234 int chrY = (int64_t)lumY * chrDstH / dstH;
235 int nextSlice =
FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
236 ((chrFilterPos[chrY] + chrFilterSize - 1)
237 << chrSubSample));
238
239 nextSlice >>= chrSubSample;
240 nextSlice <<= chrSubSample;
241 (*out_lum_size) =
FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
242 (*out_chr_size) =
FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
243 }
244 }
245
246
247
249 {
252 int num_ydesc;
253 int num_cdesc;
255 int need_lum_conv =
c->lumToYV12 ||
c->readLumPlanar ||
c->alpToYV12 ||
c->readAlpPlanar;
256 int need_chr_conv =
c->chrToYV12 ||
c->readChrPlanar;
257 int need_gamma =
c->is_internal_gamma;
258 int srcIdx, dstIdx;
259 int dst_stride =
FFALIGN(
c->dstW *
sizeof(int16_t) + 66, 16);
260
261 uint32_t * pal =
usePal(
c->srcFormat) ?
c->pal_yuv : (uint32_t*)
c->input_rgb2yuv_table;
262 int res = 0;
263
264 int lumBufSize;
265 int chrBufSize;
266
270
272 dst_stride <<= 1;
273
275 dst_stride <<= 2;
276
277 num_ydesc = need_lum_conv ? 2 : 1;
278 num_cdesc = need_chr_conv ? 2 : 1;
279
280 c->numSlice =
FFMAX(num_ydesc, num_cdesc) + 2;
281 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
282 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
283 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
284
285
286
294 }
295
296 res =
alloc_slice(&
c->slice[0],
c->srcFormat,
c->srcH,
c->chrSrcH,
c->chrSrcHSubSample,
c->chrSrcVSubSample, 0);
298 for (
i = 1;
i <
c->numSlice-2; ++
i) {
299 res =
alloc_slice(&
c->slice[
i],
c->srcFormat, lumBufSize, chrBufSize,
c->chrSrcHSubSample,
c->chrSrcVSubSample, 0);
303 }
304 // horizontal scaler output
305 res =
alloc_slice(&
c->slice[
i],
c->srcFormat, lumBufSize, chrBufSize,
c->chrDstHSubSample,
c->chrDstVSubSample, 1);
309
311
312 // vertical scaler output
314 res =
alloc_slice(&
c->slice[
i],
c->dstFormat,
c->dstH,
c->chrDstH,
c->chrDstHSubSample,
c->chrDstVSubSample, 0);
316
318 srcIdx = 0;
319 dstIdx = 1;
320
321 if (need_gamma) {
325 }
326
327 if (need_lum_conv) {
330 c->desc[
index].alpha =
c->needAlpha;
332 srcIdx = dstIdx;
333 }
334
335
336 dstIdx =
FFMAX(num_ydesc, num_cdesc);
339 c->desc[
index].alpha =
c->needAlpha;
340
341
343 {
344 srcIdx = 0;
345 dstIdx = 1;
346 if (need_chr_conv) {
350 srcIdx = dstIdx;
351 }
352
353 dstIdx =
FFMAX(num_ydesc, num_cdesc);
354 if (
c->needs_hcscale)
356 else
359 }
360
362 {
363 srcIdx =
c->numSlice - 2;
364 dstIdx =
c->numSlice - 1;
367 }
368
370 if (need_gamma) {
373 }
374
375 return 0;
376
379 return res;
380 }
381
383 {
386 for (
i = 0;
i <
c->numDesc; ++
i)
389 }
390
392 for (
i = 0;
i <
c->numSlice; ++
i)
395 }
396 return 0;
397 }