FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/slice.c
Date: 2024-03-28 04:31:58
Exec Total Coverage
Lines: 212 231 91.8%
Functions: 10 10 100.0%
Branches: 128 158 81.0%

Line Branch Exec Source
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
21 #include "swscale_internal.h"
22
23 5635 static void free_lines(SwsSlice *s)
24 {
25 int i;
26
2/2
✓ Branch 0 taken 11270 times.
✓ Branch 1 taken 5635 times.
16905 for (i = 0; i < 2; ++i) {
27 11270 int n = s->plane[i].available_lines;
28 int j;
29
2/2
✓ Branch 0 taken 90478 times.
✓ Branch 1 taken 11270 times.
101748 for (j = 0; j < n; ++j) {
30 90478 av_freep(&s->plane[i].line[j]);
31
2/2
✓ Branch 0 taken 76373 times.
✓ Branch 1 taken 14105 times.
90478 if (s->is_ring)
32 76373 s->plane[i].line[j+n] = NULL;
33 }
34 }
35
36
2/2
✓ Branch 0 taken 22540 times.
✓ Branch 1 taken 5635 times.
28175 for (i = 0; i < 4; ++i)
37
2/2
✓ Branch 0 taken 19044 times.
✓ Branch 1 taken 3496 times.
22540 memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
38 5635 s->should_free_lines = 0;
39 5635 }
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 */
45 5635 static int alloc_lines(SwsSlice *s, int size, int width)
46 {
47 int i;
48 5635 int idx[2] = {3, 2};
49
50 5635 s->should_free_lines = 1;
51 5635 s->width = width;
52
53
2/2
✓ Branch 0 taken 11270 times.
✓ Branch 1 taken 5635 times.
16905 for (i = 0; i < 2; ++i) {
54 11270 int n = s->plane[i].available_lines;
55 int j;
56 11270 int ii = idx[i];
57
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11270 times.
11270 av_assert0(n == s->plane[ii].available_lines);
59
2/2
✓ Branch 0 taken 90478 times.
✓ Branch 1 taken 11270 times.
101748 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 90478 s->plane[i].line[j] = av_malloc(size * 2 + 32);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 90478 times.
90478 if (!s->plane[i].line[j]) {
64 free_lines(s);
65 return AVERROR(ENOMEM);
66 }
67 90478 s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
68
2/2
✓ Branch 0 taken 76373 times.
✓ Branch 1 taken 14105 times.
90478 if (s->is_ring) {
69 76373 s->plane[i].line[j+n] = s->plane[i].line[j];
70 76373 s->plane[ii].line[j+n] = s->plane[ii].line[j];
71 }
72 }
73 }
74
75 5635 return 0;
76 }
77
78 15157 static int alloc_slice(SwsSlice *s, enum AVPixelFormat fmt, int lumLines, int chrLines, int h_sub_sample, int v_sub_sample, int ring)
79 {
80 int i;
81 15157 int size[4] = { lumLines,
82 chrLines,
83 chrLines,
84 lumLines };
85
86 15157 s->h_chr_sub_sample = h_sub_sample;
87 15157 s->v_chr_sub_sample = v_sub_sample;
88 15157 s->fmt = fmt;
89 15157 s->is_ring = ring;
90 15157 s->should_free_lines = 0;
91
92
2/2
✓ Branch 0 taken 60628 times.
✓ Branch 1 taken 15157 times.
75785 for (i = 0; i < 4; ++i) {
93
2/2
✓ Branch 0 taken 41584 times.
✓ Branch 1 taken 19044 times.
60628 int n = size[i] * ( ring == 0 ? 1 : 3);
94 60628 s->plane[i].line = av_calloc(n, sizeof(*s->plane[i].line));
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 60628 times.
60628 if (!s->plane[i].line)
96 return AVERROR(ENOMEM);
97
98
2/2
✓ Branch 0 taken 19044 times.
✓ Branch 1 taken 41584 times.
60628 s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
99 60628 s->plane[i].available_lines = size[i];
100 60628 s->plane[i].sliceY = 0;
101 60628 s->plane[i].sliceH = 0;
102 }
103 15157 return 0;
104 }
105
106 15157 static void free_slice(SwsSlice *s)
107 {
108 int i;
109
1/2
✓ Branch 0 taken 15157 times.
✗ Branch 1 not taken.
15157 if (s) {
110
2/2
✓ Branch 0 taken 5635 times.
✓ Branch 1 taken 9522 times.
15157 if (s->should_free_lines)
111 5635 free_lines(s);
112
2/2
✓ Branch 0 taken 60628 times.
✓ Branch 1 taken 15157 times.
75785 for (i = 0; i < 4; ++i) {
113 60628 av_freep(&s->plane[i].line);
114 60628 s->plane[i].tmp = NULL;
115 }
116 }
117 15157 }
118
119 11355511 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
120 {
121 int i;
122
2/2
✓ Branch 0 taken 11351160 times.
✓ Branch 1 taken 4351 times.
11355511 if (lum) {
123
2/2
✓ Branch 0 taken 22702320 times.
✓ Branch 1 taken 11351160 times.
34053480 for (i = 0; i < 4; i+=3) {
124 22702320 int n = s->plane[i].available_lines;
125 22702320 int l = lum - s->plane[i].sliceY;
126
127
2/2
✓ Branch 0 taken 2552974 times.
✓ Branch 1 taken 20149346 times.
22702320 if (l >= n * 2) {
128 2552974 s->plane[i].sliceY += n;
129 2552974 s->plane[i].sliceH -= n;
130 }
131 }
132 }
133
2/2
✓ Branch 0 taken 11342347 times.
✓ Branch 1 taken 13164 times.
11355511 if (chr) {
134
2/2
✓ Branch 0 taken 22684694 times.
✓ Branch 1 taken 11342347 times.
34027041 for (i = 1; i < 3; ++i) {
135 22684694 int n = s->plane[i].available_lines;
136 22684694 int l = chr - s->plane[i].sliceY;
137
138
2/2
✓ Branch 0 taken 1091920 times.
✓ Branch 1 taken 21592774 times.
22684694 if (l >= n * 2) {
139 1091920 s->plane[i].sliceY += n;
140 1091920 s->plane[i].sliceH -= n;
141 }
142 }
143 }
144 11355511 return 0;
145 }
146
147 136978 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)
148 {
149 136978 int i = 0;
150
151 136978 const int start[4] = {lumY,
152 chrY,
153 chrY,
154 lumY};
155
156 136978 const int end[4] = {lumY +lumH,
157 136978 chrY + chrH,
158 136978 chrY + chrH,
159 136978 lumY + lumH};
160
161 136978 s->width = srcW;
162
163
4/4
✓ Branch 0 taken 510219 times.
✓ Branch 1 taken 20175 times.
✓ Branch 2 taken 393416 times.
✓ Branch 3 taken 116803 times.
530394 for (i = 0; i < 4 && src[i] != NULL; ++i) {
164
2/2
✓ Branch 0 taken 79563 times.
✓ Branch 1 taken 313853 times.
393416 uint8_t *const src_i = src[i] + (relative ? 0 : start[i]) * stride[i];
165 int j;
166 393416 int first = s->plane[i].sliceY;
167 393416 int n = s->plane[i].available_lines;
168 393416 int lines = end[i] - start[i];
169 393416 int tot_lines = end[i] - first;
170
171
4/4
✓ Branch 0 taken 393407 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 393356 times.
✓ Branch 3 taken 51 times.
393416 if (start[i] >= first && n >= tot_lines) {
172 393356 s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
173
2/2
✓ Branch 0 taken 69312053 times.
✓ Branch 1 taken 393356 times.
69705409 for (j = 0; j < lines; j+= 1)
174 69312053 s->plane[i].line[start[i] - first + j] = src_i + j * stride[i];
175 } else {
176 60 s->plane[i].sliceY = start[i];
177 60 lines = lines > n ? n : lines;
178 60 s->plane[i].sliceH = lines;
179
2/2
✓ Branch 0 taken 5584 times.
✓ Branch 1 taken 60 times.
5644 for (j = 0; j < lines; j+= 1)
180 5584 s->plane[i].line[j] = src_i + j * stride[i];
181 }
182
183 }
184
185 136978 return 0;
186 }
187
188 4761 static void fill_ones(SwsSlice *s, int n, int bpc)
189 {
190 int i, j, k, size, end;
191
192
2/2
✓ Branch 0 taken 19044 times.
✓ Branch 1 taken 4761 times.
23805 for (i = 0; i < 4; ++i) {
193 19044 size = s->plane[i].available_lines;
194
2/2
✓ Branch 0 taken 152746 times.
✓ Branch 1 taken 19044 times.
171790 for (j = 0; j < size; ++j) {
195
2/2
✓ Branch 0 taken 14796 times.
✓ Branch 1 taken 137950 times.
152746 if (bpc == 16) {
196 14796 end = (n>>1) + 1;
197
2/2
✓ Branch 0 taken 5219532 times.
✓ Branch 1 taken 14796 times.
5234328 for (k = 0; k < end; ++k)
198 5219532 ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
199
2/2
✓ Branch 0 taken 3280 times.
✓ Branch 1 taken 134670 times.
137950 } else if (bpc == 32) {
200 3280 end = (n>>2) + 1;
201
2/2
✓ Branch 0 taken 1077072 times.
✓ Branch 1 taken 3280 times.
1080352 for (k = 0; k < end; ++k)
202 1077072 ((int64_t*)(s->plane[i].line[j]))[k] = 1LL<<34;
203 } else {
204 134670 end = n + 1;
205
2/2
✓ Branch 0 taken 48153310 times.
✓ Branch 1 taken 134670 times.
48287980 for (k = 0; k < end; ++k)
206 48153310 ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
207 }
208 }
209 }
210 4761 }
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 */
219 4761 static void get_min_buffer_size(SwsContext *c, int *out_lum_size, int *out_chr_size)
220 {
221 int lumY;
222 4761 int dstH = c->dstH;
223 4761 int chrDstH = c->chrDstH;
224 4761 int *lumFilterPos = c->vLumFilterPos;
225 4761 int *chrFilterPos = c->vChrFilterPos;
226 4761 int lumFilterSize = c->vLumFilterSize;
227 4761 int chrFilterSize = c->vChrFilterSize;
228 4761 int chrSubSample = c->chrSrcVSubSample;
229
230 4761 *out_lum_size = lumFilterSize;
231 4761 *out_chr_size = chrFilterSize;
232
233
2/2
✓ Branch 0 taken 1344621 times.
✓ Branch 1 taken 4761 times.
1349382 for (lumY = 0; lumY < dstH; lumY++) {
234 1344621 int chrY = (int64_t)lumY * chrDstH / dstH;
235 1344621 int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
236 ((chrFilterPos[chrY] + chrFilterSize - 1)
237 << chrSubSample));
238
239 1344621 nextSlice >>= chrSubSample;
240 1344621 nextSlice <<= chrSubSample;
241 1344621 (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
242 1344621 (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
243 }
244 4761 }
245
246
247
248 4761 int ff_init_filters(SwsContext * c)
249 {
250 int i;
251 int index;
252 int num_ydesc;
253 int num_cdesc;
254
3/4
✓ Branch 1 taken 2549 times.
✓ Branch 2 taken 2212 times.
✓ Branch 4 taken 2549 times.
✗ Branch 5 not taken.
4761 int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
255
6/8
✓ Branch 0 taken 4021 times.
✓ Branch 1 taken 740 times.
✓ Branch 2 taken 3895 times.
✓ Branch 3 taken 126 times.
✓ Branch 4 taken 3895 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3895 times.
4761 int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
256
4/4
✓ Branch 0 taken 4166 times.
✓ Branch 1 taken 595 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 4040 times.
4761 int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
257 4761 int need_gamma = c->is_internal_gamma;
258 int srcIdx, dstIdx;
259 4761 int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
260
261
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 4730 times.
4761 uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
262 4761 int res = 0;
263
264 int lumBufSize;
265 int chrBufSize;
266
267 4761 get_min_buffer_size(c, &lumBufSize, &chrBufSize);
268 4761 lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
269 4761 chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
270
271
2/2
✓ Branch 0 taken 493 times.
✓ Branch 1 taken 4268 times.
4761 if (c->dstBpc == 16)
272 493 dst_stride <<= 1;
273
274
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 4643 times.
4761 if (c->dstBpc == 32)
275 118 dst_stride <<= 2;
276
277
2/2
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 3895 times.
4761 num_ydesc = need_lum_conv ? 2 : 1;
278
2/2
✓ Branch 0 taken 721 times.
✓ Branch 1 taken 4040 times.
4761 num_cdesc = need_chr_conv ? 2 : 1;
279
280 4761 c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
282 4761 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
283 4761 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
284
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4761 times.
4761 if (isFloat16(c->srcFormat)) {
286 c->h2f_tables = av_malloc(sizeof(*c->h2f_tables));
287 if (!c->h2f_tables)
288 return AVERROR(ENOMEM);
289 ff_init_half2float_tables(c->h2f_tables);
290 c->input_opaque = c->h2f_tables;
291 }
292
293 4761 c->desc = av_calloc(c->numDesc, sizeof(*c->desc));
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (!c->desc)
295 return AVERROR(ENOMEM);
296 4761 c->slice = av_calloc(c->numSlice, sizeof(*c->slice));
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (!c->slice) {
298 res = AVERROR(ENOMEM);
299 goto cleanup;
300 }
301
302 4761 res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
303
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
304
2/2
✓ Branch 0 taken 874 times.
✓ Branch 1 taken 4761 times.
5635 for (i = 1; i < c->numSlice-2; ++i) {
305 874 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 874 times.
874 if (res < 0) goto cleanup;
307 874 res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 874 times.
874 if (res < 0) goto cleanup;
309 }
310 // horizontal scaler output
311 4761 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
313 4761 res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
315
316 4761 fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc);
317
318 // vertical scaler output
319 4761 ++i;
320 4761 res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
321
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
322
323 4761 index = 0;
324 4761 srcIdx = 0;
325 4761 dstIdx = 1;
326
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (need_gamma) {
328 res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
329 if (res < 0) goto cleanup;
330 ++index;
331 }
332
333
2/2
✓ Branch 0 taken 866 times.
✓ Branch 1 taken 3895 times.
4761 if (need_lum_conv) {
334 866 res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
335
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 866 times.
866 if (res < 0) goto cleanup;
336 866 c->desc[index].alpha = c->needAlpha;
337 866 ++index;
338 866 srcIdx = dstIdx;
339 }
340
341
342 4761 dstIdx = FFMAX(num_ydesc, num_cdesc);
343 4761 res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
345 4761 c->desc[index].alpha = c->needAlpha;
346
347
348 4761 ++index;
349 {
350 4761 srcIdx = 0;
351 4761 dstIdx = 1;
352
2/2
✓ Branch 0 taken 721 times.
✓ Branch 1 taken 4040 times.
4761 if (need_chr_conv) {
353 721 res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
354
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 721 times.
721 if (res < 0) goto cleanup;
355 721 ++index;
356 721 srcIdx = dstIdx;
357 }
358
359 4761 dstIdx = FFMAX(num_ydesc, num_cdesc);
360
2/2
✓ Branch 0 taken 4296 times.
✓ Branch 1 taken 465 times.
4761 if (c->needs_hcscale)
361 4296 res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
362 else
363 465 res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
364
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
365 }
366
367 4761 ++index;
368 {
369 4761 srcIdx = c->numSlice - 2;
370 4761 dstIdx = c->numSlice - 1;
371 4761 res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (res < 0) goto cleanup;
373 }
374
375 4761 ++index;
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4761 times.
4761 if (need_gamma) {
377 res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
378 if (res < 0) goto cleanup;
379 }
380
381 4761 return 0;
382
383 cleanup:
384 ff_free_filters(c);
385 return res;
386 }
387
388 13980 int ff_free_filters(SwsContext *c)
389 {
390 int i;
391
2/2
✓ Branch 0 taken 4761 times.
✓ Branch 1 taken 9219 times.
13980 if (c->desc) {
392
2/2
✓ Branch 0 taken 18419 times.
✓ Branch 1 taken 4761 times.
23180 for (i = 0; i < c->numDesc; ++i)
393 18419 av_freep(&c->desc[i].instance);
394 4761 av_freep(&c->desc);
395 }
396
397
2/2
✓ Branch 0 taken 4761 times.
✓ Branch 1 taken 9219 times.
13980 if (c->slice) {
398
2/2
✓ Branch 0 taken 15157 times.
✓ Branch 1 taken 4761 times.
19918 for (i = 0; i < c->numSlice; ++i)
399 15157 free_slice(&c->slice[i]);
400 4761 av_freep(&c->slice);
401 }
402 13980 av_freep(&c->h2f_tables);
403 13980 return 0;
404 }
405