FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/slice.c
Date: 2024-07-26 21:54:09
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 5682 static void free_lines(SwsSlice *s)
25 {
26 int i;
27
2/2
✓ Branch 0 taken 11364 times.
✓ Branch 1 taken 5682 times.
17046 for (i = 0; i < 2; ++i) {
28 11364 int n = s->plane[i].available_lines;
29 int j;
30
2/2
✓ Branch 0 taken 91546 times.
✓ Branch 1 taken 11364 times.
102910 for (j = 0; j < n; ++j) {
31 91546 av_freep(&s->plane[i].line[j]);
32
2/2
✓ Branch 0 taken 77194 times.
✓ Branch 1 taken 14352 times.
91546 if (s->is_ring)
33 77194 s->plane[i].line[j+n] = NULL;
34 }
35 }
36
37
2/2
✓ Branch 0 taken 22728 times.
✓ Branch 1 taken 5682 times.
28410 for (i = 0; i < 4; ++i)
38
2/2
✓ Branch 0 taken 19180 times.
✓ Branch 1 taken 3548 times.
22728 memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
39 5682 s->should_free_lines = 0;
40 5682 }
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 5682 static int alloc_lines(SwsSlice *s, int size, int width)
47 {
48 int i;
49 5682 int idx[2] = {3, 2};
50
51 5682 s->should_free_lines = 1;
52 5682 s->width = width;
53
54
2/2
✓ Branch 0 taken 11364 times.
✓ Branch 1 taken 5682 times.
17046 for (i = 0; i < 2; ++i) {
55 11364 int n = s->plane[i].available_lines;
56 int j;
57 11364 int ii = idx[i];
58
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11364 times.
11364 av_assert0(n == s->plane[ii].available_lines);
60
2/2
✓ Branch 0 taken 91546 times.
✓ Branch 1 taken 11364 times.
102910 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 91546 s->plane[i].line[j] = av_malloc(size * 2 + 32);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91546 times.
91546 if (!s->plane[i].line[j]) {
65 free_lines(s);
66 return AVERROR(ENOMEM);
67 }
68 91546 s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
69
2/2
✓ Branch 0 taken 77194 times.
✓ Branch 1 taken 14352 times.
91546 if (s->is_ring) {
70 77194 s->plane[i].line[j+n] = s->plane[i].line[j];
71 77194 s->plane[ii].line[j+n] = s->plane[ii].line[j];
72 }
73 }
74 }
75
76 5682 return 0;
77 }
78
79 15272 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 15272 int size[4] = { lumLines,
83 chrLines,
84 chrLines,
85 lumLines };
86
87 15272 s->h_chr_sub_sample = h_sub_sample;
88 15272 s->v_chr_sub_sample = v_sub_sample;
89 15272 s->fmt = fmt;
90 15272 s->is_ring = ring;
91 15272 s->should_free_lines = 0;
92
93
2/2
✓ Branch 0 taken 61088 times.
✓ Branch 1 taken 15272 times.
76360 for (i = 0; i < 4; ++i) {
94
2/2
✓ Branch 0 taken 41908 times.
✓ Branch 1 taken 19180 times.
61088 int n = size[i] * ( ring == 0 ? 1 : 3);
95 61088 s->plane[i].line = av_calloc(n, sizeof(*s->plane[i].line));
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 61088 times.
61088 if (!s->plane[i].line)
97 return AVERROR(ENOMEM);
98
99
2/2
✓ Branch 0 taken 19180 times.
✓ Branch 1 taken 41908 times.
61088 s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
100 61088 s->plane[i].available_lines = size[i];
101 61088 s->plane[i].sliceY = 0;
102 61088 s->plane[i].sliceH = 0;
103 }
104 15272 return 0;
105 }
106
107 15272 static void free_slice(SwsSlice *s)
108 {
109 int i;
110
1/2
✓ Branch 0 taken 15272 times.
✗ Branch 1 not taken.
15272 if (s) {
111
2/2
✓ Branch 0 taken 5682 times.
✓ Branch 1 taken 9590 times.
15272 if (s->should_free_lines)
112 5682 free_lines(s);
113
2/2
✓ Branch 0 taken 61088 times.
✓ Branch 1 taken 15272 times.
76360 for (i = 0; i < 4; ++i) {
114 61088 av_freep(&s->plane[i].line);
115 61088 s->plane[i].tmp = NULL;
116 }
117 }
118 15272 }
119
120 11400402 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
121 {
122 int i;
123
2/2
✓ Branch 0 taken 11396052 times.
✓ Branch 1 taken 4350 times.
11400402 if (lum) {
124
2/2
✓ Branch 0 taken 22792104 times.
✓ Branch 1 taken 11396052 times.
34188156 for (i = 0; i < 4; i+=3) {
125 22792104 int n = s->plane[i].available_lines;
126 22792104 int l = lum - s->plane[i].sliceY;
127
128
2/2
✓ Branch 0 taken 2563722 times.
✓ Branch 1 taken 20228382 times.
22792104 if (l >= n * 2) {
129 2563722 s->plane[i].sliceY += n;
130 2563722 s->plane[i].sliceH -= n;
131 }
132 }
133 }
134
2/2
✓ Branch 0 taken 11387239 times.
✓ Branch 1 taken 13163 times.
11400402 if (chr) {
135
2/2
✓ Branch 0 taken 22774478 times.
✓ Branch 1 taken 11387239 times.
34161717 for (i = 1; i < 3; ++i) {
136 22774478 int n = s->plane[i].available_lines;
137 22774478 int l = chr - s->plane[i].sliceY;
138
139
2/2
✓ Branch 0 taken 1093954 times.
✓ Branch 1 taken 21680524 times.
22774478 if (l >= n * 2) {
140 1093954 s->plane[i].sliceY += n;
141 1093954 s->plane[i].sliceH -= n;
142 }
143 }
144 }
145 11400402 return 0;
146 }
147
148 139682 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)
149 {
150 139682 int i = 0;
151
152 139682 const int start[4] = {lumY,
153 chrY,
154 chrY,
155 lumY};
156
157 139682 const int end[4] = {lumY +lumH,
158 139682 chrY + chrH,
159 139682 chrY + chrH,
160 139682 lumY + lumH};
161
162 139682 s->width = srcW;
163
164
4/4
✓ Branch 0 taken 521009 times.
✓ Branch 1 taken 20173 times.
✓ Branch 2 taken 401500 times.
✓ Branch 3 taken 119509 times.
541182 for (i = 0; i < 4 && src[i] != NULL; ++i) {
165
2/2
✓ Branch 0 taken 79585 times.
✓ Branch 1 taken 321915 times.
401500 uint8_t *const src_i = src[i] + (relative ? 0 : start[i]) * stride[i];
166 int j;
167 401500 int first = s->plane[i].sliceY;
168 401500 int n = s->plane[i].available_lines;
169 401500 int lines = end[i] - start[i];
170 401500 int tot_lines = end[i] - first;
171
172
4/4
✓ Branch 0 taken 401491 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 401430 times.
✓ Branch 3 taken 61 times.
401500 if (start[i] >= first && n >= tot_lines) {
173 401430 s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
174
2/2
✓ Branch 0 taken 70229025 times.
✓ Branch 1 taken 401430 times.
70630455 for (j = 0; j < lines; j+= 1)
175 70229025 s->plane[i].line[start[i] - first + j] = src_i + j * stride[i];
176 } else {
177 70 s->plane[i].sliceY = start[i];
178 70 lines = lines > n ? n : lines;
179 70 s->plane[i].sliceH = lines;
180
2/2
✓ Branch 0 taken 6792 times.
✓ Branch 1 taken 70 times.
6862 for (j = 0; j < lines; j+= 1)
181 6792 s->plane[i].line[j] = src_i + j * stride[i];
182 }
183
184 }
185
186 139682 return 0;
187 }
188
189 4795 static void fill_ones(SwsSlice *s, int n, int bpc)
190 {
191 int i, j, k, size, end;
192
193
2/2
✓ Branch 0 taken 19180 times.
✓ Branch 1 taken 4795 times.
23975 for (i = 0; i < 4; ++i) {
194 19180 size = s->plane[i].available_lines;
195
2/2
✓ Branch 0 taken 154388 times.
✓ Branch 1 taken 19180 times.
173568 for (j = 0; j < size; ++j) {
196
2/2
✓ Branch 0 taken 14796 times.
✓ Branch 1 taken 139592 times.
154388 if (bpc == 16) {
197 14796 end = (n>>1) + 1;
198
2/2
✓ Branch 0 taken 5219532 times.
✓ Branch 1 taken 14796 times.
5234328 for (k = 0; k < end; ++k)
199 5219532 ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
200
2/2
✓ Branch 0 taken 3280 times.
✓ Branch 1 taken 136312 times.
139592 } else if (bpc == 32) {
201 3280 end = (n>>2) + 1;
202
2/2
✓ Branch 0 taken 1077072 times.
✓ Branch 1 taken 3280 times.
1080352 for (k = 0; k < end; ++k)
203 1077072 ((int64_t*)(s->plane[i].line[j]))[k] = 1LL<<34;
204 } else {
205 136312 end = n + 1;
206
2/2
✓ Branch 0 taken 49854552 times.
✓ Branch 1 taken 136312 times.
49990864 for (k = 0; k < end; ++k)
207 49854552 ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
208 }
209 }
210 }
211 4795 }
212
213 /*
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
216 outputs a line.
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.
219 */
220 4795 static void get_min_buffer_size(SwsContext *c, int *out_lum_size, int *out_chr_size)
221 {
222 int lumY;
223 4795 int dstH = c->dstH;
224 4795 int chrDstH = c->chrDstH;
225 4795 int *lumFilterPos = c->vLumFilterPos;
226 4795 int *chrFilterPos = c->vChrFilterPos;
227 4795 int lumFilterSize = c->vLumFilterSize;
228 4795 int chrFilterSize = c->vChrFilterSize;
229 4795 int chrSubSample = c->chrSrcVSubSample;
230
231 4795 *out_lum_size = lumFilterSize;
232 4795 *out_chr_size = chrFilterSize;
233
234
2/2
✓ Branch 0 taken 1380349 times.
✓ Branch 1 taken 4795 times.
1385144 for (lumY = 0; lumY < dstH; lumY++) {
235 1380349 int chrY = (int64_t)lumY * chrDstH / dstH;
236 1380349 int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
237 ((chrFilterPos[chrY] + chrFilterSize - 1)
238 << chrSubSample));
239
240 1380349 nextSlice >>= chrSubSample;
241 1380349 nextSlice <<= chrSubSample;
242 1380349 (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
243 1380349 (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
244 }
245 4795 }
246
247
248
249 4795 int ff_init_filters(SwsContext * c)
250 {
251 int i;
252 int index;
253 int num_ydesc;
254 int num_cdesc;
255
3/4
✓ Branch 1 taken 2582 times.
✓ Branch 2 taken 2213 times.
✓ Branch 4 taken 2582 times.
✗ Branch 5 not taken.
4795 int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
256
6/8
✓ Branch 0 taken 4042 times.
✓ Branch 1 taken 753 times.
✓ Branch 2 taken 3916 times.
✓ Branch 3 taken 126 times.
✓ Branch 4 taken 3916 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 3916 times.
4795 int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
257
4/4
✓ Branch 0 taken 4187 times.
✓ Branch 1 taken 608 times.
✓ Branch 2 taken 126 times.
✓ Branch 3 taken 4061 times.
4795 int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
258 4795 int need_gamma = c->is_internal_gamma;
259 int srcIdx, dstIdx;
260 4795 int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
261
262
2/2
✓ Branch 1 taken 31 times.
✓ Branch 2 taken 4764 times.
4795 uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
263 4795 int res = 0;
264
265 int lumBufSize;
266 int chrBufSize;
267
268 4795 get_min_buffer_size(c, &lumBufSize, &chrBufSize);
269 4795 lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
270 4795 chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
271
272
2/2
✓ Branch 0 taken 493 times.
✓ Branch 1 taken 4302 times.
4795 if (c->dstBpc == 16)
273 493 dst_stride <<= 1;
274
275
2/2
✓ Branch 0 taken 118 times.
✓ Branch 1 taken 4677 times.
4795 if (c->dstBpc == 32)
276 118 dst_stride <<= 2;
277
278
2/2
✓ Branch 0 taken 879 times.
✓ Branch 1 taken 3916 times.
4795 num_ydesc = need_lum_conv ? 2 : 1;
279
2/2
✓ Branch 0 taken 734 times.
✓ Branch 1 taken 4061 times.
4795 num_cdesc = need_chr_conv ? 2 : 1;
280
281 4795 c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
282
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
283 4795 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
284 4795 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
285
286
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4795 times.
4795 if (isFloat16(c->srcFormat)) {
287 c->h2f_tables = av_malloc(sizeof(*c->h2f_tables));
288 if (!c->h2f_tables)
289 return AVERROR(ENOMEM);
290 ff_init_half2float_tables(c->h2f_tables);
291 c->input_opaque = c->h2f_tables;
292 }
293
294 4795 c->desc = av_calloc(c->numDesc, sizeof(*c->desc));
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (!c->desc)
296 return AVERROR(ENOMEM);
297 4795 c->slice = av_calloc(c->numSlice, sizeof(*c->slice));
298
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (!c->slice) {
299 res = AVERROR(ENOMEM);
300 goto cleanup;
301 }
302
303 4795 res = alloc_slice(&c->slice[0], c->srcFormat, c->srcH, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
305
2/2
✓ Branch 0 taken 887 times.
✓ Branch 1 taken 4795 times.
5682 for (i = 1; i < c->numSlice-2; ++i) {
306 887 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
307
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 887 times.
887 if (res < 0) goto cleanup;
308 887 res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
309
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 887 times.
887 if (res < 0) goto cleanup;
310 }
311 // horizontal scaler output
312 4795 res = alloc_slice(&c->slice[i], c->srcFormat, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
313
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
314 4795 res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
315
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
316
317 4795 fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc);
318
319 // vertical scaler output
320 4795 ++i;
321 4795 res = alloc_slice(&c->slice[i], c->dstFormat, c->dstH, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
322
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
323
324 4795 index = 0;
325 4795 srcIdx = 0;
326 4795 dstIdx = 1;
327
328
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (need_gamma) {
329 res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
330 if (res < 0) goto cleanup;
331 ++index;
332 }
333
334
2/2
✓ Branch 0 taken 879 times.
✓ Branch 1 taken 3916 times.
4795 if (need_lum_conv) {
335 879 res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
336
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 879 times.
879 if (res < 0) goto cleanup;
337 879 c->desc[index].alpha = c->needAlpha;
338 879 ++index;
339 879 srcIdx = dstIdx;
340 }
341
342
343 4795 dstIdx = FFMAX(num_ydesc, num_cdesc);
344 4795 res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
345
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
346 4795 c->desc[index].alpha = c->needAlpha;
347
348
349 4795 ++index;
350 {
351 4795 srcIdx = 0;
352 4795 dstIdx = 1;
353
2/2
✓ Branch 0 taken 734 times.
✓ Branch 1 taken 4061 times.
4795 if (need_chr_conv) {
354 734 res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 734 times.
734 if (res < 0) goto cleanup;
356 734 ++index;
357 734 srcIdx = dstIdx;
358 }
359
360 4795 dstIdx = FFMAX(num_ydesc, num_cdesc);
361
2/2
✓ Branch 0 taken 4330 times.
✓ Branch 1 taken 465 times.
4795 if (c->needs_hcscale)
362 4330 res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
363 else
364 465 res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
365
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
366 }
367
368 4795 ++index;
369 {
370 4795 srcIdx = c->numSlice - 2;
371 4795 dstIdx = c->numSlice - 1;
372 4795 res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
373
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (res < 0) goto cleanup;
374 }
375
376 4795 ++index;
377
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4795 times.
4795 if (need_gamma) {
378 res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
379 if (res < 0) goto cleanup;
380 }
381
382 4795 return 0;
383
384 cleanup:
385 ff_free_filters(c);
386 return res;
387 }
388
389 15802 int ff_free_filters(SwsContext *c)
390 {
391 int i;
392
2/2
✓ Branch 0 taken 4795 times.
✓ Branch 1 taken 11007 times.
15802 if (c->desc) {
393
2/2
✓ Branch 0 taken 18580 times.
✓ Branch 1 taken 4795 times.
23375 for (i = 0; i < c->numDesc; ++i)
394 18580 av_freep(&c->desc[i].instance);
395 4795 av_freep(&c->desc);
396 }
397
398
2/2
✓ Branch 0 taken 4795 times.
✓ Branch 1 taken 11007 times.
15802 if (c->slice) {
399
2/2
✓ Branch 0 taken 15272 times.
✓ Branch 1 taken 4795 times.
20067 for (i = 0; i < c->numSlice; ++i)
400 15272 free_slice(&c->slice[i]);
401 4795 av_freep(&c->slice);
402 }
403 15802 av_freep(&c->h2f_tables);
404 15802 return 0;
405 }
406