FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/slice.c
Date: 2024-11-20 23:03:26
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 "libavutil/mem.h"
22 #include "swscale_internal.h"
23
24 27280 static void free_lines(SwsSlice *s)
25 {
26 int i;
27
2/2
✓ Branch 0 taken 54560 times.
✓ Branch 1 taken 27280 times.
81840 for (i = 0; i < 2; ++i) {
28 54560 int n = s->plane[i].available_lines;
29 int j;
30
2/2
✓ Branch 0 taken 346455 times.
✓ Branch 1 taken 54560 times.
401015 for (j = 0; j < n; ++j) {
31 346455 av_freep(&s->plane[i].line[j]);
32
2/2
✓ Branch 0 taken 232055 times.
✓ Branch 1 taken 114400 times.
346455 if (s->is_ring)
33 232055 s->plane[i].line[j+n] = NULL;
34 }
35 }
36
37
2/2
✓ Branch 0 taken 109120 times.
✓ Branch 1 taken 27280 times.
136400 for (i = 0; i < 4; ++i)
38
2/2
✓ Branch 0 taken 71084 times.
✓ Branch 1 taken 38036 times.
109120 memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
39 27280 s->should_free_lines = 0;
40 27280 }
41
42 /*
43 slice lines contains extra bytes for vectorial code thus @size
44 is the allocated memory size and @width is the number of pixels
45 */
46 27280 static int alloc_lines(SwsSlice *s, int size, int width)
47 {
48 int i;
49 27280 int idx[2] = {3, 2};
50
51 27280 s->should_free_lines = 1;
52 27280 s->width = width;
53
54
2/2
✓ Branch 0 taken 54560 times.
✓ Branch 1 taken 27280 times.
81840 for (i = 0; i < 2; ++i) {
55 54560 int n = s->plane[i].available_lines;
56 int j;
57 54560 int ii = idx[i];
58
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 54560 times.
54560 av_assert0(n == s->plane[ii].available_lines);
60
2/2
✓ Branch 0 taken 346455 times.
✓ Branch 1 taken 54560 times.
401015 for (j = 0; j < n; ++j) {
61 // chroma plane line U and V are expected to be contiguous in memory
62 // by mmx vertical scaler code
63 346455 s->plane[i].line[j] = av_malloc(size * 2 + 32);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 346455 times.
346455 if (!s->plane[i].line[j]) {
65 free_lines(s);
66 return AVERROR(ENOMEM);
67 }
68 346455 s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
69
2/2
✓ Branch 0 taken 232055 times.
✓ Branch 1 taken 114400 times.
346455 if (s->is_ring) {
70 232055 s->plane[i].line[j+n] = s->plane[i].line[j];
71 232055 s->plane[ii].line[j+n] = s->plane[ii].line[j];
72 }
73 }
74 }
75
76 27280 return 0;
77 }
78
79 62822 static int alloc_slice(SwsSlice *s, enum AVPixelFormat fmt, int lumLines, int chrLines, int h_sub_sample, int v_sub_sample, int ring)
80 {
81 int i;
82 62822 int size[4] = { lumLines,
83 chrLines,
84 chrLines,
85 lumLines };
86
87 62822 s->h_chr_sub_sample = h_sub_sample;
88 62822 s->v_chr_sub_sample = v_sub_sample;
89 62822 s->fmt = fmt;
90 62822 s->is_ring = ring;
91 62822 s->should_free_lines = 0;
92
93
2/2
✓ Branch 0 taken 251288 times.
✓ Branch 1 taken 62822 times.
314110 for (i = 0; i < 4; ++i) {
94
2/2
✓ Branch 0 taken 180204 times.
✓ Branch 1 taken 71084 times.
251288 int n = size[i] * ( ring == 0 ? 1 : 3);
95 251288 s->plane[i].line = av_calloc(n, sizeof(*s->plane[i].line));
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 251288 times.
251288 if (!s->plane[i].line)
97 return AVERROR(ENOMEM);
98
99
2/2
✓ Branch 0 taken 71084 times.
✓ Branch 1 taken 180204 times.
251288 s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
100 251288 s->plane[i].available_lines = size[i];
101 251288 s->plane[i].sliceY = 0;
102 251288 s->plane[i].sliceH = 0;
103 }
104 62822 return 0;
105 }
106
107 62822 static void free_slice(SwsSlice *s)
108 {
109 int i;
110
1/2
✓ Branch 0 taken 62822 times.
✗ Branch 1 not taken.
62822 if (s) {
111
2/2
✓ Branch 0 taken 27280 times.
✓ Branch 1 taken 35542 times.
62822 if (s->should_free_lines)
112 27280 free_lines(s);
113
2/2
✓ Branch 0 taken 251288 times.
✓ Branch 1 taken 62822 times.
314110 for (i = 0; i < 4; ++i) {
114 251288 av_freep(&s->plane[i].line);
115 251288 s->plane[i].tmp = NULL;
116 }
117 }
118 62822 }
119
120 22621742 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
121 {
122 int i;
123
2/2
✓ Branch 0 taken 22617392 times.
✓ Branch 1 taken 4350 times.
22621742 if (lum) {
124
2/2
✓ Branch 0 taken 45234784 times.
✓ Branch 1 taken 22617392 times.
67852176 for (i = 0; i < 4; i+=3) {
125 45234784 int n = s->plane[i].available_lines;
126 45234784 int l = lum - s->plane[i].sliceY;
127
128
2/2
✓ Branch 0 taken 6331556 times.
✓ Branch 1 taken 38903228 times.
45234784 if (l >= n * 2) {
129 6331556 s->plane[i].sliceY += n;
130 6331556 s->plane[i].sliceH -= n;
131 }
132 }
133 }
134
2/2
✓ Branch 0 taken 22607724 times.
✓ Branch 1 taken 14018 times.
22621742 if (chr) {
135
2/2
✓ Branch 0 taken 45215448 times.
✓ Branch 1 taken 22607724 times.
67823172 for (i = 1; i < 3; ++i) {
136 45215448 int n = s->plane[i].available_lines;
137 45215448 int l = chr - s->plane[i].sliceY;
138
139
2/2
✓ Branch 0 taken 3884548 times.
✓ Branch 1 taken 41330900 times.
45215448 if (l >= n * 2) {
140 3884548 s->plane[i].sliceY += n;
141 3884548 s->plane[i].sliceH -= n;
142 }
143 }
144 }
145 22621742 return 0;
146 }
147
148 813410 int ff_init_slice_from_src(SwsSlice * s, uint8_t *const src[4], const int stride[4],
149 int srcW, int lumY, int lumH, int chrY, int chrH, int relative)
150 {
151 813410 int i = 0;
152
153 813410 const int start[4] = {lumY,
154 chrY,
155 chrY,
156 lumY};
157
158 813410 const int end[4] = {lumY +lumH,
159 813410 chrY + chrH,
160 813410 chrY + chrH,
161 813410 lumY + lumH};
162
163 813410 s->width = srcW;
164
165
4/4
✓ Branch 0 taken 2906578 times.
✓ Branch 1 taken 104316 times.
✓ Branch 2 taken 2197484 times.
✓ Branch 3 taken 709094 times.
3010894 for (i = 0; i < 4 && src[i] != NULL; ++i) {
166
2/2
✓ Branch 0 taken 83209 times.
✓ Branch 1 taken 2114275 times.
2197484 uint8_t *const src_i = src[i] + (relative ? 0 : start[i]) * stride[i];
167 int j;
168 2197484 int first = s->plane[i].sliceY;
169 2197484 int n = s->plane[i].available_lines;
170 2197484 int lines = end[i] - start[i];
171 2197484 int tot_lines = end[i] - first;
172
173
4/4
✓ Branch 0 taken 2197475 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 2197418 times.
✓ Branch 3 taken 57 times.
2197484 if (start[i] >= first && n >= tot_lines) {
174 2197418 s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
175
2/2
✓ Branch 0 taken 379061205 times.
✓ Branch 1 taken 2197418 times.
381258623 for (j = 0; j < lines; j+= 1)
176 379061205 s->plane[i].line[start[i] - first + j] = src_i + j * stride[i];
177 } else {
178 66 s->plane[i].sliceY = start[i];
179 66 lines = lines > n ? n : lines;
180 66 s->plane[i].sliceH = lines;
181
2/2
✓ Branch 0 taken 5976 times.
✓ Branch 1 taken 66 times.
6042 for (j = 0; j < lines; j+= 1)
182 5976 s->plane[i].line[j] = src_i + j * stride[i];
183 }
184
185 }
186
187 813410 return 0;
188 }
189
190 17771 static void fill_ones(SwsSlice *s, int n, int bpc)
191 {
192 int i, j, k, size, end;
193
194
2/2
✓ Branch 0 taken 71084 times.
✓ Branch 1 taken 17771 times.
88855 for (i = 0; i < 4; ++i) {
195 71084 size = s->plane[i].available_lines;
196
2/2
✓ Branch 0 taken 464110 times.
✓ Branch 1 taken 71084 times.
535194 for (j = 0; j < size; ++j) {
197
2/2
✓ Branch 0 taken 80848 times.
✓ Branch 1 taken 383262 times.
464110 if (bpc == 16) {
198 80848 end = (n>>1) + 1;
199
2/2
✓ Branch 0 taken 31139056 times.
✓ Branch 1 taken 80848 times.
31219904 for (k = 0; k < end; ++k)
200 31139056 ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
201
2/2
✓ Branch 0 taken 3448 times.
✓ Branch 1 taken 379814 times.
383262 } else if (bpc == 32) {
202 3448 end = (n>>2) + 1;
203
2/2
✓ Branch 0 taken 1143096 times.
✓ Branch 1 taken 3448 times.
1146544 for (k = 0; k < end; ++k)
204 1143096 ((int64_t*)(s->plane[i].line[j]))[k] = 1LL<<34;
205 } else {
206 379814 end = n + 1;
207
2/2
✓ Branch 0 taken 145787062 times.
✓ Branch 1 taken 379814 times.
146166876 for (k = 0; k < end; ++k)
208 145787062 ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
209 }
210 }
211 }
212 17771 }
213
214 /*
215 Calculates the minimum ring buffer size, it should be able to store vFilterSize
216 more n lines where n is the max difference between each adjacent slice which
217 outputs a line.
218 The n lines are needed only when there is not enough src lines to output a single
219 dst line, then we should buffer these lines to process them on the next call to scale.
220 */
221 17771 static void get_min_buffer_size(SwsInternal *c, int *out_lum_size, int *out_chr_size)
222 {
223 int lumY;
224 17771 int dstH = c->dstH;
225 17771 int chrDstH = c->chrDstH;
226 17771 int *lumFilterPos = c->vLumFilterPos;
227 17771 int *chrFilterPos = c->vChrFilterPos;
228 17771 int lumFilterSize = c->vLumFilterSize;
229 17771 int chrFilterSize = c->vChrFilterSize;
230 17771 int chrSubSample = c->chrSrcVSubSample;
231
232 17771 *out_lum_size = lumFilterSize;
233 17771 *out_chr_size = chrFilterSize;
234
235
2/2
✓ Branch 0 taken 5130285 times.
✓ Branch 1 taken 17771 times.
5148056 for (lumY = 0; lumY < dstH; lumY++) {
236 5130285 int chrY = (int64_t)lumY * chrDstH / dstH;
237 5130285 int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
238 ((chrFilterPos[chrY] + chrFilterSize - 1)
239 << chrSubSample));
240
241 5130285 nextSlice >>= chrSubSample;
242 5130285 nextSlice <<= chrSubSample;
243 5130285 (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
244 5130285 (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
245 }
246 17771 }
247
248
249
250 17771 int ff_init_filters(SwsInternal * c)
251 {
252 int i;
253 int index;
254 int num_ydesc;
255 int num_cdesc;
256
3/4
✓ Branch 1 taken 8840 times.
✓ Branch 2 taken 8931 times.
✓ Branch 4 taken 8840 times.
✗ Branch 5 not taken.
17771 int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
257
6/8
✓ Branch 0 taken 11710 times.
✓ Branch 1 taken 6061 times.
✓ Branch 2 taken 8749 times.
✓ Branch 3 taken 2961 times.
✓ Branch 4 taken 8749 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 8749 times.
17771 int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
258
4/4
✓ Branch 0 taken 11376 times.
✓ Branch 1 taken 6395 times.
✓ Branch 2 taken 2583 times.
✓ Branch 3 taken 8793 times.
17771 int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
259 17771 int need_gamma = c->is_internal_gamma;
260 int srcIdx, dstIdx;
261 17771 int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
262
263
2/2
✓ Branch 1 taken 94 times.
✓ Branch 2 taken 17677 times.
17771 uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
264 17771 int res = 0;
265
266 int lumBufSize;
267 int chrBufSize;
268
269 17771 get_min_buffer_size(c, &lumBufSize, &chrBufSize);
270 17771 lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
271 17771 chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
272
273
2/2
✓ Branch 0 taken 3319 times.
✓ Branch 1 taken 14452 times.
17771 if (c->dstBpc == 16)
274 3319 dst_stride <<= 1;
275
276
2/2
✓ Branch 0 taken 124 times.
✓ Branch 1 taken 17647 times.
17771 if (c->dstBpc == 32)
277 124 dst_stride <<= 2;
278
279
2/2
✓ Branch 0 taken 9022 times.
✓ Branch 1 taken 8749 times.
17771 num_ydesc = need_lum_conv ? 2 : 1;
280
2/2
✓ Branch 0 taken 8978 times.
✓ Branch 1 taken 8793 times.
17771 num_cdesc = need_chr_conv ? 2 : 1;
281
282 17771 c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
283
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
284 17771 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
285 17771 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
286
287
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 17771 times.
17771 if (isFloat16(c->srcFormat)) {
288 c->h2f_tables = av_malloc(sizeof(*c->h2f_tables));
289 if (!c->h2f_tables)
290 return AVERROR(ENOMEM);
291 ff_init_half2float_tables(c->h2f_tables);
292 c->input_opaque = c->h2f_tables;
293 }
294
295 17771 c->desc = av_calloc(c->numDesc, sizeof(*c->desc));
296
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (!c->desc)
297 return AVERROR(ENOMEM);
298 17771 c->slice = av_calloc(c->numSlice, sizeof(*c->slice));
299
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (!c->slice) {
300 res = AVERROR(ENOMEM);
301 goto cleanup;
302 }
303
304 17771 res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
305
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
306
2/2
✓ Branch 0 taken 9509 times.
✓ Branch 1 taken 17771 times.
27280 for (i = 1; i < c->numSlice-2; ++i) {
307 9509 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9509 times.
9509 if (res < 0) goto cleanup;
309 9509 res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9509 times.
9509 if (res < 0) goto cleanup;
311 }
312 // horizontal scaler output
313 17771 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
315 17771 res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
316
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
317
318 17771 fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc);
319
320 // vertical scaler output
321 17771 ++i;
322 17771 res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
324
325 17771 index = 0;
326 17771 srcIdx = 0;
327 17771 dstIdx = 1;
328
329
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (need_gamma) {
330 res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
331 if (res < 0) goto cleanup;
332 ++index;
333 }
334
335
2/2
✓ Branch 0 taken 9022 times.
✓ Branch 1 taken 8749 times.
17771 if (need_lum_conv) {
336 9022 res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9022 times.
9022 if (res < 0) goto cleanup;
338 9022 c->desc[index].alpha = c->needAlpha;
339 9022 ++index;
340 9022 srcIdx = dstIdx;
341 }
342
343
344 17771 dstIdx = FFMAX(num_ydesc, num_cdesc);
345 17771 res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
346
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
347 17771 c->desc[index].alpha = c->needAlpha;
348
349
350 17771 ++index;
351 {
352 17771 srcIdx = 0;
353 17771 dstIdx = 1;
354
2/2
✓ Branch 0 taken 8978 times.
✓ Branch 1 taken 8793 times.
17771 if (need_chr_conv) {
355 8978 res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8978 times.
8978 if (res < 0) goto cleanup;
357 8978 ++index;
358 8978 srcIdx = dstIdx;
359 }
360
361 17771 dstIdx = FFMAX(num_ydesc, num_cdesc);
362
2/2
✓ Branch 0 taken 16075 times.
✓ Branch 1 taken 1696 times.
17771 if (c->needs_hcscale)
363 16075 res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
364 else
365 1696 res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
367 }
368
369 17771 ++index;
370 {
371 17771 srcIdx = c->numSlice - 2;
372 17771 dstIdx = c->numSlice - 1;
373 17771 res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (res < 0) goto cleanup;
375 }
376
377 17771 ++index;
378
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17771 times.
17771 if (need_gamma) {
379 res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
380 if (res < 0) goto cleanup;
381 }
382
383 17771 return 0;
384
385 cleanup:
386 ff_free_filters(c);
387 return res;
388 }
389
390 34473 int ff_free_filters(SwsInternal *c)
391 {
392 int i;
393
2/2
✓ Branch 0 taken 17771 times.
✓ Branch 1 taken 16702 times.
34473 if (c->desc) {
394
2/2
✓ Branch 0 taken 80153 times.
✓ Branch 1 taken 17771 times.
97924 for (i = 0; i < c->numDesc; ++i)
395 80153 av_freep(&c->desc[i].instance);
396 17771 av_freep(&c->desc);
397 }
398
399
2/2
✓ Branch 0 taken 17771 times.
✓ Branch 1 taken 16702 times.
34473 if (c->slice) {
400
2/2
✓ Branch 0 taken 62822 times.
✓ Branch 1 taken 17771 times.
80593 for (i = 0; i < c->numSlice; ++i)
401 62822 free_slice(&c->slice[i]);
402 17771 av_freep(&c->slice);
403 }
404 34473 av_freep(&c->h2f_tables);
405 34473 return 0;
406 }
407