FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/slice.c
Date: 2024-02-16 17:37:06
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 9161 static void free_lines(SwsSlice *s)
24 {
25 int i;
26
2/2
✓ Branch 0 taken 18322 times.
✓ Branch 1 taken 9161 times.
27483 for (i = 0; i < 2; ++i) {
27 18322 int n = s->plane[i].available_lines;
28 int j;
29
2/2
✓ Branch 0 taken 125738 times.
✓ Branch 1 taken 18322 times.
144060 for (j = 0; j < n; ++j) {
30 125738 av_freep(&s->plane[i].line[j]);
31
2/2
✓ Branch 0 taken 97233 times.
✓ Branch 1 taken 28505 times.
125738 if (s->is_ring)
32 97233 s->plane[i].line[j+n] = NULL;
33 }
34 }
35
36
2/2
✓ Branch 0 taken 36644 times.
✓ Branch 1 taken 9161 times.
45805 for (i = 0; i < 4; ++i)
37
2/2
✓ Branch 0 taken 27388 times.
✓ Branch 1 taken 9256 times.
36644 memset(s->plane[i].line, 0, sizeof(uint8_t*) * s->plane[i].available_lines * (s->is_ring ? 3 : 1));
38 9161 s->should_free_lines = 0;
39 9161 }
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 9161 static int alloc_lines(SwsSlice *s, int size, int width)
46 {
47 int i;
48 9161 int idx[2] = {3, 2};
49
50 9161 s->should_free_lines = 1;
51 9161 s->width = width;
52
53
2/2
✓ Branch 0 taken 18322 times.
✓ Branch 1 taken 9161 times.
27483 for (i = 0; i < 2; ++i) {
54 18322 int n = s->plane[i].available_lines;
55 int j;
56 18322 int ii = idx[i];
57
58
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18322 times.
18322 av_assert0(n == s->plane[ii].available_lines);
59
2/2
✓ Branch 0 taken 125738 times.
✓ Branch 1 taken 18322 times.
144060 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 125738 s->plane[i].line[j] = av_malloc(size * 2 + 32);
63
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 125738 times.
125738 if (!s->plane[i].line[j]) {
64 free_lines(s);
65 return AVERROR(ENOMEM);
66 }
67 125738 s->plane[ii].line[j] = s->plane[i].line[j] + size + 16;
68
2/2
✓ Branch 0 taken 97233 times.
✓ Branch 1 taken 28505 times.
125738 if (s->is_ring) {
69 97233 s->plane[i].line[j+n] = s->plane[i].line[j];
70 97233 s->plane[ii].line[j+n] = s->plane[ii].line[j];
71 }
72 }
73 }
74
75 9161 return 0;
76 }
77
78 22855 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 22855 int size[4] = { lumLines,
82 chrLines,
83 chrLines,
84 lumLines };
85
86 22855 s->h_chr_sub_sample = h_sub_sample;
87 22855 s->v_chr_sub_sample = v_sub_sample;
88 22855 s->fmt = fmt;
89 22855 s->is_ring = ring;
90 22855 s->should_free_lines = 0;
91
92
2/2
✓ Branch 0 taken 91420 times.
✓ Branch 1 taken 22855 times.
114275 for (i = 0; i < 4; ++i) {
93
2/2
✓ Branch 0 taken 64032 times.
✓ Branch 1 taken 27388 times.
91420 int n = size[i] * ( ring == 0 ? 1 : 3);
94 91420 s->plane[i].line = av_calloc(n, sizeof(*s->plane[i].line));
95
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91420 times.
91420 if (!s->plane[i].line)
96 return AVERROR(ENOMEM);
97
98
2/2
✓ Branch 0 taken 27388 times.
✓ Branch 1 taken 64032 times.
91420 s->plane[i].tmp = ring ? s->plane[i].line + size[i] * 2 : NULL;
99 91420 s->plane[i].available_lines = size[i];
100 91420 s->plane[i].sliceY = 0;
101 91420 s->plane[i].sliceH = 0;
102 }
103 22855 return 0;
104 }
105
106 22855 static void free_slice(SwsSlice *s)
107 {
108 int i;
109
1/2
✓ Branch 0 taken 22855 times.
✗ Branch 1 not taken.
22855 if (s) {
110
2/2
✓ Branch 0 taken 9161 times.
✓ Branch 1 taken 13694 times.
22855 if (s->should_free_lines)
111 9161 free_lines(s);
112
2/2
✓ Branch 0 taken 91420 times.
✓ Branch 1 taken 22855 times.
114275 for (i = 0; i < 4; ++i) {
113 91420 av_freep(&s->plane[i].line);
114 91420 s->plane[i].tmp = NULL;
115 }
116 }
117 22855 }
118
119 11355513 int ff_rotate_slice(SwsSlice *s, int lum, int chr)
120 {
121 int i;
122
2/2
✓ Branch 0 taken 11351162 times.
✓ Branch 1 taken 4351 times.
11355513 if (lum) {
123
2/2
✓ Branch 0 taken 22702324 times.
✓ Branch 1 taken 11351162 times.
34053486 for (i = 0; i < 4; i+=3) {
124 22702324 int n = s->plane[i].available_lines;
125 22702324 int l = lum - s->plane[i].sliceY;
126
127
2/2
✓ Branch 0 taken 2552974 times.
✓ Branch 1 taken 20149350 times.
22702324 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 11342350 times.
✓ Branch 1 taken 13163 times.
11355513 if (chr) {
134
2/2
✓ Branch 0 taken 22684700 times.
✓ Branch 1 taken 11342350 times.
34027050 for (i = 1; i < 3; ++i) {
135 22684700 int n = s->plane[i].available_lines;
136 22684700 int l = chr - s->plane[i].sliceY;
137
138
2/2
✓ Branch 0 taken 1091920 times.
✓ Branch 1 taken 21592780 times.
22684700 if (l >= n * 2) {
139 1091920 s->plane[i].sliceY += n;
140 1091920 s->plane[i].sliceH -= n;
141 }
142 }
143 }
144 11355513 return 0;
145 }
146
147 136982 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 136982 int i = 0;
150
151 136982 const int start[4] = {lumY,
152 chrY,
153 chrY,
154 lumY};
155
156 136982 const int end[4] = {lumY +lumH,
157 136982 chrY + chrH,
158 136982 chrY + chrH,
159 136982 lumY + lumH};
160
161 136982 s->width = srcW;
162
163
4/4
✓ Branch 0 taken 510235 times.
✓ Branch 1 taken 20179 times.
✓ Branch 2 taken 393432 times.
✓ Branch 3 taken 116803 times.
530414 for (i = 0; i < 4 && src[i] != NULL; ++i) {
164
2/2
✓ Branch 0 taken 79569 times.
✓ Branch 1 taken 313863 times.
393432 uint8_t *const src_i = src[i] + (relative ? 0 : start[i]) * stride[i];
165 int j;
166 393432 int first = s->plane[i].sliceY;
167 393432 int n = s->plane[i].available_lines;
168 393432 int lines = end[i] - start[i];
169 393432 int tot_lines = end[i] - first;
170
171
4/4
✓ Branch 0 taken 393423 times.
✓ Branch 1 taken 9 times.
✓ Branch 2 taken 393369 times.
✓ Branch 3 taken 54 times.
393432 if (start[i] >= first && n >= tot_lines) {
172 393369 s->plane[i].sliceH = FFMAX(tot_lines, s->plane[i].sliceH);
173
2/2
✓ Branch 0 taken 69312145 times.
✓ Branch 1 taken 393369 times.
69705514 for (j = 0; j < lines; j+= 1)
174 69312145 s->plane[i].line[start[i] - first + j] = src_i + j * stride[i];
175 } else {
176 63 s->plane[i].sliceY = start[i];
177 63 lines = lines > n ? n : lines;
178 63 s->plane[i].sliceH = lines;
179
2/2
✓ Branch 0 taken 5676 times.
✓ Branch 1 taken 63 times.
5739 for (j = 0; j < lines; j+= 1)
180 5676 s->plane[i].line[j] = src_i + j * stride[i];
181 }
182
183 }
184
185 136982 return 0;
186 }
187
188 6847 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 27388 times.
✓ Branch 1 taken 6847 times.
34235 for (i = 0; i < 4; ++i) {
193 27388 size = s->plane[i].available_lines;
194
2/2
✓ Branch 0 taken 194466 times.
✓ Branch 1 taken 27388 times.
221854 for (j = 0; j < size; ++j) {
195
2/2
✓ Branch 0 taken 17256 times.
✓ Branch 1 taken 177210 times.
194466 if (bpc == 16) {
196 17256 end = (n>>1) + 1;
197
2/2
✓ Branch 0 taken 6926152 times.
✓ Branch 1 taken 17256 times.
6943408 for (k = 0; k < end; ++k)
198 6926152 ((int32_t*)(s->plane[i].line[j]))[k] = 1<<18;
199
2/2
✓ Branch 0 taken 3320 times.
✓ Branch 1 taken 173890 times.
177210 } else if (bpc == 32) {
200 3320 end = (n>>2) + 1;
201
2/2
✓ Branch 0 taken 1092792 times.
✓ Branch 1 taken 3320 times.
1096112 for (k = 0; k < end; ++k)
202 1092792 ((int64_t*)(s->plane[i].line[j]))[k] = 1LL<<34;
203 } else {
204 173890 end = n + 1;
205
2/2
✓ Branch 0 taken 62597490 times.
✓ Branch 1 taken 173890 times.
62771380 for (k = 0; k < end; ++k)
206 62597490 ((int16_t*)(s->plane[i].line[j]))[k] = 1<<14;
207 }
208 }
209 }
210 6847 }
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 6847 static void get_min_buffer_size(SwsContext *c, int *out_lum_size, int *out_chr_size)
220 {
221 int lumY;
222 6847 int dstH = c->dstH;
223 6847 int chrDstH = c->chrDstH;
224 6847 int *lumFilterPos = c->vLumFilterPos;
225 6847 int *chrFilterPos = c->vChrFilterPos;
226 6847 int lumFilterSize = c->vLumFilterSize;
227 6847 int chrFilterSize = c->vChrFilterSize;
228 6847 int chrSubSample = c->chrSrcVSubSample;
229
230 6847 *out_lum_size = lumFilterSize;
231 6847 *out_chr_size = chrFilterSize;
232
233
2/2
✓ Branch 0 taken 1898197 times.
✓ Branch 1 taken 6847 times.
1905044 for (lumY = 0; lumY < dstH; lumY++) {
234 1898197 int chrY = (int64_t)lumY * chrDstH / dstH;
235 1898197 int nextSlice = FFMAX(lumFilterPos[lumY] + lumFilterSize - 1,
236 ((chrFilterPos[chrY] + chrFilterSize - 1)
237 << chrSubSample));
238
239 1898197 nextSlice >>= chrSubSample;
240 1898197 nextSlice <<= chrSubSample;
241 1898197 (*out_lum_size) = FFMAX((*out_lum_size), nextSlice - lumFilterPos[lumY]);
242 1898197 (*out_chr_size) = FFMAX((*out_chr_size), (nextSlice >> chrSubSample) - chrFilterPos[chrY]);
243 }
244 6847 }
245
246
247
248 6847 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 3156 times.
✓ Branch 2 taken 3691 times.
✓ Branch 4 taken 3156 times.
✗ Branch 5 not taken.
6847 int num_vdesc = isPlanarYUV(c->dstFormat) && !isGray(c->dstFormat) ? 2 : 1;
255
6/8
✓ Branch 0 taken 4734 times.
✓ Branch 1 taken 2113 times.
✓ Branch 2 taken 4559 times.
✓ Branch 3 taken 175 times.
✓ Branch 4 taken 4559 times.
✗ Branch 5 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 4559 times.
6847 int need_lum_conv = c->lumToYV12 || c->readLumPlanar || c->alpToYV12 || c->readAlpPlanar;
256
4/4
✓ Branch 0 taken 4862 times.
✓ Branch 1 taken 1985 times.
✓ Branch 2 taken 175 times.
✓ Branch 3 taken 4687 times.
6847 int need_chr_conv = c->chrToYV12 || c->readChrPlanar;
257 6847 int need_gamma = c->is_internal_gamma;
258 int srcIdx, dstIdx;
259 6847 int dst_stride = FFALIGN(c->dstW * sizeof(int16_t) + 66, 16);
260
261
2/2
✓ Branch 1 taken 914 times.
✓ Branch 2 taken 5933 times.
6847 uint32_t * pal = usePal(c->srcFormat) ? c->pal_yuv : (uint32_t*)c->input_rgb2yuv_table;
262 6847 int res = 0;
263
264 int lumBufSize;
265 int chrBufSize;
266
267 6847 get_min_buffer_size(c, &lumBufSize, &chrBufSize);
268 6847 lumBufSize = FFMAX(lumBufSize, c->vLumFilterSize + MAX_LINES_AHEAD);
269 6847 chrBufSize = FFMAX(chrBufSize, c->vChrFilterSize + MAX_LINES_AHEAD);
270
271
2/2
✓ Branch 0 taken 616 times.
✓ Branch 1 taken 6231 times.
6847 if (c->dstBpc == 16)
272 616 dst_stride <<= 1;
273
274
2/2
✓ Branch 0 taken 120 times.
✓ Branch 1 taken 6727 times.
6847 if (c->dstBpc == 32)
275 120 dst_stride <<= 2;
276
277
2/2
✓ Branch 0 taken 2288 times.
✓ Branch 1 taken 4559 times.
6847 num_ydesc = need_lum_conv ? 2 : 1;
278
2/2
✓ Branch 0 taken 2160 times.
✓ Branch 1 taken 4687 times.
6847 num_cdesc = need_chr_conv ? 2 : 1;
279
280 6847 c->numSlice = FFMAX(num_ydesc, num_cdesc) + 2;
281
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 c->numDesc = num_ydesc + num_cdesc + num_vdesc + (need_gamma ? 2 : 0);
282 6847 c->descIndex[0] = num_ydesc + (need_gamma ? 1 : 0);
283 6847 c->descIndex[1] = num_ydesc + num_cdesc + (need_gamma ? 1 : 0);
284
285
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 6847 times.
6847 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 6847 c->desc = av_calloc(c->numDesc, sizeof(*c->desc));
294
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 if (!c->desc)
295 return AVERROR(ENOMEM);
296 6847 c->slice = av_calloc(c->numSlice, sizeof(*c->slice));
297
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 if (!c->slice) {
298 res = AVERROR(ENOMEM);
299 goto cleanup;
300 }
301
302 6847 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 6847 times.
6847 if (res < 0) goto cleanup;
304
2/2
✓ Branch 0 taken 2314 times.
✓ Branch 1 taken 6847 times.
9161 for (i = 1; i < c->numSlice-2; ++i) {
305 2314 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 2314 times.
2314 if (res < 0) goto cleanup;
307 2314 res = alloc_lines(&c->slice[i], FFALIGN(c->srcW*2+78, 16), c->srcW);
308
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2314 times.
2314 if (res < 0) goto cleanup;
309 }
310 // horizontal scaler output
311 6847 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 6847 times.
6847 if (res < 0) goto cleanup;
313 6847 res = alloc_lines(&c->slice[i], dst_stride, c->dstW);
314
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 if (res < 0) goto cleanup;
315
316 6847 fill_ones(&c->slice[i], dst_stride>>1, c->dstBpc);
317
318 // vertical scaler output
319 6847 ++i;
320 6847 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 6847 times.
6847 if (res < 0) goto cleanup;
322
323 6847 index = 0;
324 6847 srcIdx = 0;
325 6847 dstIdx = 1;
326
327
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 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 2288 times.
✓ Branch 1 taken 4559 times.
6847 if (need_lum_conv) {
334 2288 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 2288 times.
2288 if (res < 0) goto cleanup;
336 2288 c->desc[index].alpha = c->needAlpha;
337 2288 ++index;
338 2288 srcIdx = dstIdx;
339 }
340
341
342 6847 dstIdx = FFMAX(num_ydesc, num_cdesc);
343 6847 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 6847 times.
6847 if (res < 0) goto cleanup;
345 6847 c->desc[index].alpha = c->needAlpha;
346
347
348 6847 ++index;
349 {
350 6847 srcIdx = 0;
351 6847 dstIdx = 1;
352
2/2
✓ Branch 0 taken 2160 times.
✓ Branch 1 taken 4687 times.
6847 if (need_chr_conv) {
353 2160 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 2160 times.
2160 if (res < 0) goto cleanup;
355 2160 ++index;
356 2160 srcIdx = dstIdx;
357 }
358
359 6847 dstIdx = FFMAX(num_ydesc, num_cdesc);
360
2/2
✓ Branch 0 taken 6324 times.
✓ Branch 1 taken 523 times.
6847 if (c->needs_hcscale)
361 6324 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 523 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 6847 times.
6847 if (res < 0) goto cleanup;
365 }
366
367 6847 ++index;
368 {
369 6847 srcIdx = c->numSlice - 2;
370 6847 dstIdx = c->numSlice - 1;
371 6847 res = ff_init_vscale(c, c->desc + index, c->slice + srcIdx, c->slice + dstIdx);
372
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 if (res < 0) goto cleanup;
373 }
374
375 6847 ++index;
376
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6847 times.
6847 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 6847 return 0;
382
383 cleanup:
384 ff_free_filters(c);
385 return res;
386 }
387
388 13991 int ff_free_filters(SwsContext *c)
389 {
390 int i;
391
2/2
✓ Branch 0 taken 6847 times.
✓ Branch 1 taken 7144 times.
13991 if (c->desc) {
392
2/2
✓ Branch 0 taken 28145 times.
✓ Branch 1 taken 6847 times.
34992 for (i = 0; i < c->numDesc; ++i)
393 28145 av_freep(&c->desc[i].instance);
394 6847 av_freep(&c->desc);
395 }
396
397
2/2
✓ Branch 0 taken 6847 times.
✓ Branch 1 taken 7144 times.
13991 if (c->slice) {
398
2/2
✓ Branch 0 taken 22855 times.
✓ Branch 1 taken 6847 times.
29702 for (i = 0; i < c->numSlice; ++i)
399 22855 free_slice(&c->slice[i]);
400 6847 av_freep(&c->slice);
401 }
402 13991 av_freep(&c->h2f_tables);
403 13991 return 0;
404 }
405