FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/slice.c
Date: 2026-10-11 16:25:10
Exec Total Coverage
Lines: 212 227 93.4%
Functions: 10 10 100.0%
Branches: 126 154 81.8%

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 158671 static void free_lines(SwsSlice *s)
25 {
26 int i;
27
2/2
✓ Branch 0 taken 317342 times.
✓ Branch 1 taken 158671 times.
476013 for (i = 0; i < 2; ++i) {
28 317342 int n = s->plane[i].available_lines;
29 int j;
30
2/2
✓ Branch 0 taken 1880752 times.
✓ Branch 1 taken 317342 times.
2198094 for (j = 0; j < n; ++j) {
31 1880752 av_freep(&s->plane[i].line[j]);
32
2/2
✓ Branch 0 taken 1069515 times.
✓ Branch 1 taken 811237 times.
1880752 if (s->is_ring)
33 1069515 s->plane[i].line[j+n] = NULL;
34 }
35 }
36
37
2/2
✓ Branch 0 taken 634684 times.
✓ Branch 1 taken 158671 times.
793355 for (i = 0; i < 4; ++i)
38
2/2
✓ Branch 0 taken 356200 times.
✓ Branch 1 taken 278484 times.
634684 memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
39 158671 s->should_free_lines = 0;
40 158671 }
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 158671 static int alloc_lines(SwsSlice *s, int size, int width)
47 {
48 int i;
49 158671 int idx[2] = {3, 2};
50
51 158671 s->should_free_lines = 1;
52 158671 s->width = width;
53
54
2/2
✓ Branch 0 taken 317342 times.
✓ Branch 1 taken 158671 times.
476013 for (i = 0; i < 2; ++i) {
55 317342 int n = s->plane[i].available_lines;
56 int j;
57 317342 int ii = idx[i];
58
59
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 317342 times.
317342 av_assert0(n == s->plane[ii].available_lines);
60
2/2
✓ Branch 0 taken 1880752 times.
✓ Branch 1 taken 317342 times.
2198094 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 1880752 s->plane[i].line[j] = av_mallocz(size * 2 + 32);
64
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1880752 times.
1880752 if (!s->plane[i].line[j]) {
65 ✗ free_lines(s);
66 ✗ return AVERROR(ENOMEM);
67 }
68 1880752 s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
69
2/2
✓ Branch 0 taken 1069515 times.
✓ Branch 1 taken 811237 times.
1880752 if (s->is_ring) {
70 1069515 s->plane[i].line[j+n] = s->plane[i].line[j];
71 1069515 s->plane[ii].line[j+n] = s->plane[ii].line[j];
72 }
73 }
74 }
75
76 158671 return 0;
77 }
78
79 336771 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 336771 int size[4] = { lumLines,
83 chrLines,
84 chrLines,
85 lumLines };
86
87 336771 s->h_chr_sub_sample = h_sub_sample;
88 336771 s->v_chr_sub_sample = v_sub_sample;
89 336771 s->fmt = fmt;
90 336771 s->is_ring = ring;
91 336771 s->should_free_lines = 0;
92
93
2/2
✓ Branch 0 taken 1347084 times.
✓ Branch 1 taken 336771 times.
1683855 for (i = 0; i < 4; ++i) {
94
2/2
✓ Branch 0 taken 990884 times.
✓ Branch 1 taken 356200 times.
1347084 int n = size[i] * ( ring == 0 ? 1 : 3);
95 1347084 s->plane[i].line = av_calloc(n, sizeof(*s->plane[i].line));
96
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1347084 times.
1347084 if (!s->plane[i].line)
97 ✗ return AVERROR(ENOMEM);
98
99
2/2
✓ Branch 0 taken 356200 times.
✓ Branch 1 taken 990884 times.
1347084 s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
100 1347084 s->plane[i].available_lines = size[i];
101 1347084 s->plane[i].sliceY = 0;
102 1347084 s->plane[i].sliceH = 0;
103 }
104 336771 return 0;
105 }
106
107 336771 static void free_slice(SwsSlice *s)
108 {
109 int i;
110
1/2
✓ Branch 0 taken 336771 times.
✗ Branch 1 not taken.
336771 if (s) {
111
2/2
✓ Branch 0 taken 158671 times.
✓ Branch 1 taken 178100 times.
336771 if (s->should_free_lines)
112 158671 free_lines(s);
113
2/2
✓ Branch 0 taken 1347084 times.
✓ Branch 1 taken 336771 times.
1683855 for (i = 0; i < 4; ++i) {
114 1347084 av_freep(&s->plane[i].line);
115 1347084 s->plane[i].tmp = NULL;
116 }
117 }
118 336771 }
119
120 35549914 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
121 {
122 int i;
123
2/2
✓ Branch 0 taken 35545560 times.
✓ Branch 1 taken 4354 times.
35549914 if (lum) {
124
2/2
✓ Branch 0 taken 71091120 times.
✓ Branch 1 taken 35545560 times.
106636680 for (i = 0; i < 4; i+=3) {
125 71091120 int n = s->plane[i].available_lines;
126 71091120 int l = lum - s->plane[i].sliceY;
127
128
2/2
✓ Branch 0 taken 10602662 times.
✓ Branch 1 taken 60488458 times.
71091120 if (l >= n * 2) {
129 10602662 s->plane[i].sliceY += n;
130 10602662 s->plane[i].sliceH -= n;
131 }
132 }
133 }
134
2/2
✓ Branch 0 taken 35529132 times.
✓ Branch 1 taken 20782 times.
35549914 if (chr) {
135
2/2
✓ Branch 0 taken 71058264 times.
✓ Branch 1 taken 35529132 times.
106587396 for (i = 1; i < 3; ++i) {
136 71058264 int n = s->plane[i].available_lines;
137 71058264 int l = chr - s->plane[i].sliceY;
138
139
2/2
✓ Branch 0 taken 7022114 times.
✓ Branch 1 taken 64036150 times.
71058264 if (l >= n * 2) {
140 7022114 s->plane[i].sliceY += n;
141 7022114 s->plane[i].sliceH -= n;
142 }
143 }
144 }
145 35549914 return 0;
146 }
147
148 1244036 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 1244036 int i = 0;
152
153 1244036 const int start[4] = {lumY,
154 chrY,
155 chrY,
156 lumY};
157
158 1244036 const int end[4] = {lumY +lumH,
159 1244036 chrY + chrH,
160 1244036 chrY + chrH,
161 1244036 lumY + lumH};
162
163 1244036 s->width = srcW;
164
165
4/4
✓ Branch 0 taken 4394877 times.
✓ Branch 1 taken 227559 times.
✓ Branch 2 taken 3378400 times.
✓ Branch 3 taken 1016477 times.
4622436 for (i = 0; i < 4 && src[i] != NULL; ++i) {
166
2/2
✓ Branch 0 taken 356544 times.
✓ Branch 1 taken 3021856 times.
3378400 uint8_t *const src_i = src[i] + (relative ? 0 : start[i]) * stride[i];
167 int j;
168 3378400 int first = s->plane[i].sliceY;
169 3378400 int n = s->plane[i].available_lines;
170 3378400 int lines = end[i] - start[i];
171 3378400 int tot_lines = end[i] - first;
172
173
4/4
✓ Branch 0 taken 3378391 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 3378331 times.
✓ Branch 3 taken 60 times.
3378400 if (start[i] >= first && n >= tot_lines) {
174 3378331 s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
175
2/2
✓ Branch 0 taken 541756198 times.
✓ Branch 1 taken 3378331 times.
545134529 for (j = 0; j < lines; j+= 1)
176 541756198 s->plane[i].line[start[i] - first + j] = src_i + j * stride[i];
177 } else {
178 69 s->plane[i].sliceY = start[i];
179 69 lines = lines > n ? n : lines;
180 69 s->plane[i].sliceH = lines;
181
2/2
✓ Branch 0 taken 6484 times.
✓ Branch 1 taken 69 times.
6553 for (j = 0; j < lines; j+= 1)
182 6484 s->plane[i].line[j] = src_i + j * stride[i];
183 }
184
185 }
186
187 1244036 return 0;
188 }
189
190 89050 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 356200 times.
✓ Branch 1 taken 89050 times.
445250 for (i = 0; i < 4; ++i) {
195 356200 size = s->plane[i].available_lines;
196
2/2
✓ Branch 0 taken 2139030 times.
✓ Branch 1 taken 356200 times.
2495230 for (j = 0; j < size; ++j) {
197
2/2
✓ Branch 0 taken 135452 times.
✓ Branch 1 taken 2003578 times.
2139030 if (bpc >= 16) {
198 135452 end = (n>>1) + 1;
199
2/2
✓ Branch 0 taken 67682812 times.
✓ Branch 1 taken 135452 times.
67818264 for (k = 0; k < end; ++k)
200 67682812 ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
201 } else {
202 2003578 end = n + 1;
203
2/2
✓ Branch 0 taken 489368266 times.
✓ Branch 1 taken 2003578 times.
491371844 for (k = 0; k < end; ++k)
204 489368266 ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
205 }
206 }
207 }
208 89050 }
209
210 /*
211 Calculates the minimum ring buffer size, it should be able to store vFilterSize
212 more n lines where n is the max difference between each adjacent slice which
213 outputs a line.
214 The n lines are needed only when there is not enough src lines to output a single
215 dst line, then we should buffer these lines to process them on the next call to scale.
216 */
217 89050 static void get_min_buffer_size(SwsInternal *c, int *out_lum_size, int *out_chr_size)
218 {
219 int lumY;
220 89050 int dstH = c->opts.dst_h;
221 89050 int chrDstH = c->chrDstH;
222 89050 int *lumFilterPos = c->vLumFilterPos;
223 89050 int *chrFilterPos = c->vChrFilterPos;
224 89050 int lumFilterSize = c->vLumFilterSize;
225 89050 int chrFilterSize = c->vChrFilterSize;
226 89050 int chrSubSample = c->chrSrcVSubSample;
227
228 89050 *out_lum_size = lumFilterSize;
229 89050 *out_chr_size = chrFilterSize;
230
231
2/2
✓ Branch 0 taken 15801386 times.
✓ Branch 1 taken 89050 times.
15890436 for (lumY = 0; lumY < dstH; lumY++) {
232 15801386 int chrY = (int64_t)lumY * chrDstH / dstH;
233 15801386 int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
234 ((chrFilterPos[chrY] + chrFilterSize - 1)
235 << chrSubSample));
236
237 15801386 nextSlice >>= chrSubSample;
238 15801386 nextSlice <<= chrSubSample;
239 15801386 (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
240 15801386 (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
241 }
242 89050 }
243
244
245
246 89050 int ff_init_filters(SwsInternal * c)
247 {
248 int i;
249 int index;
250 int num_ydesc;
251 int num_cdesc;
252
3/4
✓ Branch 1 taken 73770 times.
✓ Branch 2 taken 15280 times.
✓ Branch 4 taken 73770 times.
✗ Branch 5 not taken.
89050 int num_vdesc = isPlanarYUV(c->opts.dst_format) && !isGray(c->opts.dst_format) ? 2 : 1;
253
6/8
✓ Branch 0 taken 40259 times.
✓ Branch 1 taken 48791 times.
✓ Branch 2 taken 22838 times.
✓ Branch 3 taken 17421 times.
✓ Branch 4 taken 22838 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 22838 times.
89050 int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
254
4/4
✓ Branch 0 taken 43254 times.
✓ Branch 1 taken 45796 times.
✓ Branch 2 taken 17000 times.
✓ Branch 3 taken 26254 times.
89050 int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
255 89050 int need_gamma = c->is_internal_gamma;
256 int srcIdx, dstIdx;
257 89050 int dst_stride = FFALIGN(c->opts.dst_w * sizeof(int16_t) + 66, 16);
258
259
2/2
✓ Branch 1 taken 3164 times.
✓ Branch 2 taken 85886 times.
89050 uint32_t * pal = usePal(c->opts.src_format) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
260 89050 int res = 0;
261
262 int lumBufSize;
263 int chrBufSize;
264
265 89050 get_min_buffer_size(c, &lumBufSize, &chrBufSize);
266 89050 lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
267 89050 chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
268
269
2/2
✓ Branch 0 taken 5055 times.
✓ Branch 1 taken 83995 times.
89050 if (c->dstBpc == 16)
270 5055 dst_stride <<= 1;
271
272
2/2
✓ Branch 0 taken 584 times.
✓ Branch 1 taken 88466 times.
89050 if (c->dstBpc == 32)
273 584 dst_stride <<= 2;
274
275
2/2
✓ Branch 0 taken 66212 times.
✓ Branch 1 taken 22838 times.
89050 num_ydesc = need_lum_conv ? 2 : 1;
276
2/2
✓ Branch 0 taken 62796 times.
✓ Branch 1 taken 26254 times.
89050 num_cdesc = need_chr_conv ? 2 : 1;
277
278 89050 c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
279
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
280 89050 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
281 89050 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
282
283
2/2
✓ Branch 1 taken 411 times.
✓ Branch 2 taken 88639 times.
89050 if (isFloat16(c->opts.src_format)) {
284 411 c->h2f_tables = av_malloc(sizeof(*c->h2f_tables));
285
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 411 times.
411 if (!c->h2f_tables)
286 ✗ return AVERROR(ENOMEM);
287 411 ff_init_half2float_tables(c->h2f_tables);
288 411 c->input_opaque = c->h2f_tables;
289 }
290
291 89050 c->desc = av_calloc(c->numDesc, sizeof(*c->desc));
292
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (!c->desc)
293 ✗ return AVERROR(ENOMEM);
294 89050 c->slice = av_calloc(c->numSlice, sizeof(*c->slice));
295
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (!c->slice) {
296 ✗ res = AVERROR(ENOMEM);
297 ✗ goto cleanup;
298 }
299
300 89050 res = alloc_slice(&c->slice[0], c->opts.src_format, c->opts.src_h, c->chrSrcH, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
301
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
302
2/2
✓ Branch 0 taken 69621 times.
✓ Branch 1 taken 89050 times.
158671 for (i = 1; i < c->numSlice-2; ++i) {
303 69621 res = alloc_slice(&c->slice[i], c->opts.src_format, lumBufSize, chrBufSize, c->chrSrcHSubSample, c->chrSrcVSubSample, 0);
304
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69621 times.
69621 if (res < 0) goto cleanup;
305 69621 res = alloc_lines(&c->slice[i], FFALIGN(c->opts.src_w*2+78, 16), c->opts.src_w);
306
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 69621 times.
69621 if (res < 0) goto cleanup;
307 }
308 // horizontal scaler output
309 89050 res = alloc_slice(&c->slice[i], c->opts.src_format, lumBufSize, chrBufSize, c->chrDstHSubSample, c->chrDstVSubSample, 1);
310
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
311 89050 res = alloc_lines(&c->slice[i], dst_stride, c->opts.dst_w);
312
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
313
314 89050 fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc);
315
316 // vertical scaler output
317 89050 ++i;
318 89050 res = alloc_slice(&c->slice[i], c->opts.dst_format, c->opts.dst_h, c->chrDstH, c->chrDstHSubSample, c->chrDstVSubSample, 0);
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
320
321 89050 index = 0;
322 89050 srcIdx = 0;
323 89050 dstIdx = 1;
324
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (need_gamma) {
326 ✗ res = ff_init_gamma_convert(c->desc + index, c->slice + srcIdx, c->inv_gamma);
327 ✗ if (res < 0) goto cleanup;
328 ✗ ++index;
329 }
330
331
2/2
✓ Branch 0 taken 66212 times.
✓ Branch 1 taken 22838 times.
89050 if (need_lum_conv) {
332 66212 res = ff_init_desc_fmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
333
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 66212 times.
66212 if (res < 0) goto cleanup;
334 66212 c->desc[index].alpha = c->needAlpha;
335 66212 ++index;
336 66212 srcIdx = dstIdx;
337 }
338
339
340 89050 dstIdx = FFMAX(num_ydesc, num_cdesc);
341 89050 res = ff_init_desc_hscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hLumFilter, c->hLumFilterPos, c->hLumFilterSize, c->lumXInc);
342
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
343 89050 c->desc[index].alpha = c->needAlpha;
344
345
346 89050 ++index;
347 {
348 89050 srcIdx = 0;
349 89050 dstIdx = 1;
350
2/2
✓ Branch 0 taken 62796 times.
✓ Branch 1 taken 26254 times.
89050 if (need_chr_conv) {
351 62796 res = ff_init_desc_cfmt_convert(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], pal);
352
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 62796 times.
62796 if (res < 0) goto cleanup;
353 62796 ++index;
354 62796 srcIdx = dstIdx;
355 }
356
357 89050 dstIdx = FFMAX(num_ydesc, num_cdesc);
358
2/2
✓ Branch 0 taken 76908 times.
✓ Branch 1 taken 12142 times.
89050 if (c->needs_hcscale)
359 76908 res = ff_init_desc_chscale(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx], c->hChrFilter, c->hChrFilterPos, c->hChrFilterSize, c->chrXInc);
360 else
361 12142 res = ff_init_desc_no_chr(&c->desc[index], &c->slice[srcIdx], &c->slice[dstIdx]);
362
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
363 }
364
365 89050 ++index;
366 {
367 89050 srcIdx = c->numSlice - 2;
368 89050 dstIdx = c->numSlice - 1;
369 89050 res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
370
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (res < 0) goto cleanup;
371 }
372
373 89050 ++index;
374
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 89050 times.
89050 if (need_gamma) {
375 ✗ res = ff_init_gamma_convert(c->desc + index, c->slice + dstIdx, c->gamma);
376 ✗ if (res < 0) goto cleanup;
377 }
378
379 89050 return 0;
380
381 ✗ cleanup:
382 ✗ ff_free_filters(c);
383 ✗ return res;
384 }
385
386 122957 int ff_free_filters(SwsInternal *c)
387 {
388 int i;
389
2/2
✓ Branch 0 taken 89050 times.
✓ Branch 1 taken 33907 times.
122957 if (c->desc) {
390
2/2
✓ Branch 0 taken 469928 times.
✓ Branch 1 taken 89050 times.
558978 for (i = 0; i < c->numDesc; ++i)
391 469928 av_freep(&c->desc[i].instance);
392 89050 av_freep(&c->desc);
393 }
394
395
2/2
✓ Branch 0 taken 89050 times.
✓ Branch 1 taken 33907 times.
122957 if (c->slice) {
396
2/2
✓ Branch 0 taken 336771 times.
✓ Branch 1 taken 89050 times.
425821 for (i = 0; i < c->numSlice; ++i)
397 336771 free_slice(&c->slice[i]);
398 89050 av_freep(&c->slice);
399 }
400 122957 av_freep(&c->h2f_tables);
401 122957 return 0;
402 }
403