FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/vscale.c
Date: 2025-07-28 20:30:09
Exec Total Coverage
Lines: 174 198 87.9%
Functions: 6 6 100.0%
Branches: 92 126 73.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 #include "libavutil/mem.h"
21 #include "swscale_internal.h"
22
23 typedef struct VScalerContext
24 {
25 uint16_t *filter[2];
26 int32_t *filter_pos;
27 int filter_size;
28 int isMMX;
29 union {
30 yuv2planar1_fn yuv2planar1;
31 yuv2planarX_fn yuv2planarX;
32 yuv2interleavedX_fn yuv2interleavedX;
33 yuv2packed1_fn yuv2packed1;
34 yuv2packed2_fn yuv2packed2;
35 yuv2anyX_fn yuv2anyX;
36 } pfn;
37 yuv2packedX_fn yuv2packedX;
38 } VScalerContext;
39
40
41 18820781 static int lum_planar_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
42 {
43 18820781 VScalerContext *inst = desc->instance;
44 18820781 int dstW = desc->dst->width;
45
46 18820781 int first = FFMAX(1-inst->filter_size, inst->filter_pos[sliceY]);
47 18820781 int sp = first - desc->src->plane[0].sliceY;
48 18820781 int dp = sliceY - desc->dst->plane[0].sliceY;
49 18820781 uint8_t **src = desc->src->plane[0].line + sp;
50 18820781 uint8_t **dst = desc->dst->plane[0].line + dp;
51
2/2
✓ Branch 0 taken 18820687 times.
✓ Branch 1 taken 94 times.
18820781 uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : sliceY * inst->filter_size);
52
53
2/2
✓ Branch 0 taken 15281841 times.
✓ Branch 1 taken 3538940 times.
18820781 if (inst->filter_size == 1)
54 15281841 inst->pfn.yuv2planar1((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0);
55 else
56 3538940 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0);
57
58
2/2
✓ Branch 0 taken 90984 times.
✓ Branch 1 taken 18729797 times.
18820781 if (desc->alpha) {
59 90984 int sp = first - desc->src->plane[3].sliceY;
60 90984 int dp = sliceY - desc->dst->plane[3].sliceY;
61 90984 uint8_t **src = desc->src->plane[3].line + sp;
62 90984 uint8_t **dst = desc->dst->plane[3].line + dp;
63
1/2
✓ Branch 0 taken 90984 times.
✗ Branch 1 not taken.
90984 uint16_t *filter = inst->filter[1] + (inst->isMMX ? 0 : sliceY * inst->filter_size);
64
65
2/2
✓ Branch 0 taken 85984 times.
✓ Branch 1 taken 5000 times.
90984 if (inst->filter_size == 1)
66 85984 inst->pfn.yuv2planar1((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0);
67 else
68 5000 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0);
69 }
70
71 18820781 return 1;
72 }
73
74 17853048 static int chr_planar_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
75 {
76 17853048 const int chrSkipMask = (1 << desc->dst->v_chr_sub_sample) - 1;
77
2/2
✓ Branch 0 taken 4157096 times.
✓ Branch 1 taken 13695952 times.
17853048 if (sliceY & chrSkipMask)
78 4157096 return 0;
79 else {
80 13695952 VScalerContext *inst = desc->instance;
81 13695952 int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample);
82 13695952 int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
83
84 13695952 int first = FFMAX(1-inst->filter_size, inst->filter_pos[chrSliceY]);
85 13695952 int sp1 = first - desc->src->plane[1].sliceY;
86 13695952 int sp2 = first - desc->src->plane[2].sliceY;
87 13695952 int dp1 = chrSliceY - desc->dst->plane[1].sliceY;
88 13695952 int dp2 = chrSliceY - desc->dst->plane[2].sliceY;
89 13695952 uint8_t **src1 = desc->src->plane[1].line + sp1;
90 13695952 uint8_t **src2 = desc->src->plane[2].line + sp2;
91 13695952 uint8_t **dst1 = desc->dst->plane[1].line + dp1;
92 13695952 uint8_t **dst2 = desc->dst->plane[2].line + dp2;
93
2/2
✓ Branch 0 taken 13695858 times.
✓ Branch 1 taken 94 times.
13695952 uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : chrSliceY * inst->filter_size);
94
95
2/2
✓ Branch 0 taken 3568552 times.
✓ Branch 1 taken 10127400 times.
13695952 if (c->yuv2nv12cX) {
96 3568552 inst->pfn.yuv2interleavedX(c->opts.dst_format, c->chrDither8, filter, inst->filter_size, (const int16_t**)src1, (const int16_t**)src2, dst1[0], dstW);
97
2/2
✓ Branch 0 taken 4129600 times.
✓ Branch 1 taken 5997800 times.
10127400 } else if (inst->filter_size == 1) {
98 4129600 inst->pfn.yuv2planar1((const int16_t*)src1[0], dst1[0], dstW, c->chrDither8, 0);
99 4129600 inst->pfn.yuv2planar1((const int16_t*)src2[0], dst2[0], dstW, c->chrDither8, 3);
100 } else {
101 5997800 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src1, dst1[0], dstW, c->chrDither8, 0);
102
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 5997800 times.
5997800 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src2, dst2[0], dstW, c->chrDither8, inst->isMMX ? (c->uv_offx2 >> 1) : 3);
103 }
104 }
105
106 13695952 return 1;
107 }
108
109 5682240 static int packed_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
110 {
111 5682240 VScalerContext *inst = desc->instance;
112 5682240 int dstW = desc->dst->width;
113 5682240 int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
114
115 5682240 int lum_fsize = inst[0].filter_size;
116 5682240 int chr_fsize = inst[1].filter_size;
117 5682240 uint16_t *lum_filter = inst[0].filter[0];
118 5682240 uint16_t *chr_filter = inst[1].filter[0];
119
120 5682240 int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]);
121 5682240 int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]);
122
123 5682240 int sp0 = firstLum - desc->src->plane[0].sliceY;
124 5682240 int sp1 = firstChr - desc->src->plane[1].sliceY;
125 5682240 int sp2 = firstChr - desc->src->plane[2].sliceY;
126 5682240 int sp3 = firstLum - desc->src->plane[3].sliceY;
127 5682240 int dp = sliceY - desc->dst->plane[0].sliceY;
128 5682240 uint8_t **src0 = desc->src->plane[0].line + sp0;
129 5682240 uint8_t **src1 = desc->src->plane[1].line + sp1;
130 5682240 uint8_t **src2 = desc->src->plane[2].line + sp2;
131
2/2
✓ Branch 0 taken 10601 times.
✓ Branch 1 taken 5671639 times.
5682240 uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL;
132 5682240 uint8_t **dst = desc->dst->plane[0].line + dp;
133
134
135
6/6
✓ Branch 0 taken 4730244 times.
✓ Branch 1 taken 951996 times.
✓ Branch 2 taken 4692253 times.
✓ Branch 3 taken 37991 times.
✓ Branch 4 taken 2908389 times.
✓ Branch 5 taken 1783864 times.
5682240 if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 1) { // unscaled RGB
136 2908389 inst->pfn.yuv2packed1(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2,
137
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2908389 times.
2908389 (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, 0, sliceY);
138
5/6
✓ Branch 0 taken 1821855 times.
✓ Branch 1 taken 951996 times.
✓ Branch 2 taken 1783864 times.
✓ Branch 3 taken 37991 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1783864 times.
2773851 } else if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2 &&
139 chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 &&
140 chr_filter[2 * chrSliceY + 1] <= 4096U) { // unscaled RGB
141 int chrAlpha = chr_filter[2 * chrSliceY + 1];
142 inst->pfn.yuv2packed1(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2,
143 (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, chrAlpha, sliceY);
144
3/6
✓ Branch 0 taken 1821855 times.
✓ Branch 1 taken 951996 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1821855 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2773851 } else if (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2 &&
145 lum_filter[2 * sliceY + 1] + lum_filter[2 * sliceY] == 4096 &&
146 lum_filter[2 * sliceY + 1] <= 4096U &&
147 chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 &&
148 chr_filter[2 * chrSliceY + 1] <= 4096U
149 ) { // bilinear upscale RGB
150 int lumAlpha = lum_filter[2 * sliceY + 1];
151 int chrAlpha = chr_filter[2 * chrSliceY + 1];
152 c->lumMmxFilter[2] =
153 c->lumMmxFilter[3] = lum_filter[2 * sliceY] * 0x10001;
154 c->chrMmxFilter[2] =
155 c->chrMmxFilter[3] = chr_filter[2 * chrSliceY] * 0x10001;
156 inst->pfn.yuv2packed2(c, (const int16_t**)src0, (const int16_t**)src1, (const int16_t**)src2, (const int16_t**)src3,
157 *dst, dstW, lumAlpha, chrAlpha, sliceY);
158 } else { // general RGB
159
5/6
✓ Branch 0 taken 1821855 times.
✓ Branch 1 taken 951996 times.
✓ Branch 2 taken 1783864 times.
✓ Branch 3 taken 37991 times.
✓ Branch 4 taken 1783864 times.
✗ Branch 5 not taken.
2773851 if ((c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2) ||
160
3/6
✓ Branch 0 taken 1821855 times.
✓ Branch 1 taken 951996 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1821855 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
2773851 (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2)) {
161 if (!c->warned_unuseable_bilinear)
162 av_log(c, AV_LOG_INFO, "Optimized 2 tap filter code cannot be used\n");
163 c->warned_unuseable_bilinear = 1;
164 }
165
166 2773851 inst->yuv2packedX(c, lum_filter + sliceY * lum_fsize,
167 2773851 (const int16_t**)src0, lum_fsize, chr_filter + chrSliceY * chr_fsize,
168 (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, *dst, dstW, sliceY);
169 }
170 5682240 return 1;
171 }
172
173 3067455 static int any_vscale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
174 {
175 3067455 VScalerContext *inst = desc->instance;
176 3067455 int dstW = desc->dst->width;
177 3067455 int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
178
179 3067455 int lum_fsize = inst[0].filter_size;
180 3067455 int chr_fsize = inst[1].filter_size;
181 3067455 uint16_t *lum_filter = inst[0].filter[0];
182 3067455 uint16_t *chr_filter = inst[1].filter[0];
183
184 3067455 int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]);
185 3067455 int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]);
186
187 3067455 int sp0 = firstLum - desc->src->plane[0].sliceY;
188 3067455 int sp1 = firstChr - desc->src->plane[1].sliceY;
189 3067455 int sp2 = firstChr - desc->src->plane[2].sliceY;
190 3067455 int sp3 = firstLum - desc->src->plane[3].sliceY;
191 3067455 int dp0 = sliceY - desc->dst->plane[0].sliceY;
192 3067455 int dp1 = chrSliceY - desc->dst->plane[1].sliceY;
193 3067455 int dp2 = chrSliceY - desc->dst->plane[2].sliceY;
194 3067455 int dp3 = sliceY - desc->dst->plane[3].sliceY;
195
196 3067455 uint8_t **src0 = desc->src->plane[0].line + sp0;
197 3067455 uint8_t **src1 = desc->src->plane[1].line + sp1;
198 3067455 uint8_t **src2 = desc->src->plane[2].line + sp2;
199
2/2
✓ Branch 0 taken 5421 times.
✓ Branch 1 taken 3062034 times.
3067455 uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL;
200 6134910 uint8_t *dst[4] = { desc->dst->plane[0].line[dp0],
201 3067455 desc->dst->plane[1].line[dp1],
202 3067455 desc->dst->plane[2].line[dp2],
203
2/2
✓ Branch 0 taken 5421 times.
✓ Branch 1 taken 3062034 times.
3067455 desc->alpha ? desc->dst->plane[3].line[dp3] : NULL };
204
205 av_assert1(!c->yuv2packed1 && !c->yuv2packed2);
206 3067455 inst->pfn.yuv2anyX(c, lum_filter + sliceY * lum_fsize,
207 3067455 (const int16_t**)src0, lum_fsize, chr_filter + sliceY * chr_fsize,
208 (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, dst, dstW, sliceY);
209
210 3067455 return 1;
211
212 }
213
214 26396 int ff_init_vscale(SwsInternal *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst)
215 {
216 26396 VScalerContext *lumCtx = NULL;
217 26396 VScalerContext *chrCtx = NULL;
218
219
6/6
✓ Branch 1 taken 12582 times.
✓ Branch 2 taken 13814 times.
✓ Branch 4 taken 1156 times.
✓ Branch 5 taken 11426 times.
✓ Branch 7 taken 1115 times.
✓ Branch 8 taken 41 times.
26396 if (isPlanarYUV(c->opts.dst_format) || (isGray(c->opts.dst_format) && !isALPHA(c->opts.dst_format))) {
220 14929 lumCtx = av_mallocz(sizeof(VScalerContext));
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14929 times.
14929 if (!lumCtx)
222 return AVERROR(ENOMEM);
223
224
225 14929 desc[0].process = lum_planar_vscale;
226 14929 desc[0].instance = lumCtx;
227 14929 desc[0].src = src;
228 14929 desc[0].dst = dst;
229 14929 desc[0].alpha = c->needAlpha;
230
231
2/2
✓ Branch 1 taken 13814 times.
✓ Branch 2 taken 1115 times.
28743 if (!isGray(c->opts.dst_format)) {
232 13814 chrCtx = av_mallocz(sizeof(VScalerContext));
233
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13814 times.
13814 if (!chrCtx)
234 return AVERROR(ENOMEM);
235 13814 desc[1].process = chr_planar_vscale;
236 13814 desc[1].instance = chrCtx;
237 13814 desc[1].src = src;
238 13814 desc[1].dst = dst;
239 }
240 } else {
241 11467 lumCtx = av_calloc(2, sizeof(*lumCtx));
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11467 times.
11467 if (!lumCtx)
243 return AVERROR(ENOMEM);
244 11467 chrCtx = &lumCtx[1];
245
246
2/2
✓ Branch 0 taken 7173 times.
✓ Branch 1 taken 4294 times.
11467 desc[0].process = c->yuv2packedX ? packed_vscale : any_vscale;
247 11467 desc[0].instance = lumCtx;
248 11467 desc[0].src = src;
249 11467 desc[0].dst = dst;
250 11467 desc[0].alpha = c->needAlpha;
251 }
252
253 26396 ff_init_vscale_pfn(c, c->yuv2plane1, c->yuv2planeX, c->yuv2nv12cX,
254 c->yuv2packed1, c->yuv2packed2, c->yuv2packedX, c->yuv2anyX, c->use_mmx_vfilter);
255 26396 return 0;
256 }
257
258 747504 void ff_init_vscale_pfn(SwsInternal *c,
259 yuv2planar1_fn yuv2plane1,
260 yuv2planarX_fn yuv2planeX,
261 yuv2interleavedX_fn yuv2nv12cX,
262 yuv2packed1_fn yuv2packed1,
263 yuv2packed2_fn yuv2packed2,
264 yuv2packedX_fn yuv2packedX,
265 yuv2anyX_fn yuv2anyX, int use_mmx)
266 {
267 747504 VScalerContext *lumCtx = NULL;
268 747504 VScalerContext *chrCtx = NULL;
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 747504 times.
747504 int idx = c->numDesc - (c->is_internal_gamma ? 2 : 1); //FIXME avoid hardcoding indexes
270
271
6/6
✓ Branch 1 taken 309575 times.
✓ Branch 2 taken 437929 times.
✓ Branch 4 taken 31486 times.
✓ Branch 5 taken 278089 times.
✓ Branch 7 taken 31127 times.
✓ Branch 8 taken 359 times.
747504 if (isPlanarYUV(c->opts.dst_format) || (isGray(c->opts.dst_format) && !isALPHA(c->opts.dst_format))) {
272
2/2
✓ Branch 1 taken 437929 times.
✓ Branch 2 taken 31127 times.
469056 if (!isGray(c->opts.dst_format)) {
273 437929 chrCtx = c->desc[idx].instance;
274
275
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 437927 times.
437929 chrCtx->filter[0] = use_mmx ? (int16_t*)c->chrMmxFilter : c->vChrFilter;
276 437929 chrCtx->filter_size = c->vChrFilterSize;
277 437929 chrCtx->filter_pos = c->vChrFilterPos;
278 437929 chrCtx->isMMX = use_mmx;
279
280 437929 --idx;
281
2/2
✓ Branch 0 taken 148681 times.
✓ Branch 1 taken 289248 times.
437929 if (yuv2nv12cX) chrCtx->pfn.yuv2interleavedX = yuv2nv12cX;
282
2/2
✓ Branch 0 taken 133912 times.
✓ Branch 1 taken 155336 times.
289248 else if (c->vChrFilterSize == 1) chrCtx->pfn.yuv2planar1 = yuv2plane1;
283 155336 else chrCtx->pfn.yuv2planarX = yuv2planeX;
284 }
285
286 469056 lumCtx = c->desc[idx].instance;
287
288
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 469054 times.
469056 lumCtx->filter[0] = use_mmx ? (int16_t*)c->lumMmxFilter : c->vLumFilter;
289
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 469054 times.
469056 lumCtx->filter[1] = use_mmx ? (int16_t*)c->alpMmxFilter : c->vLumFilter;
290 469056 lumCtx->filter_size = c->vLumFilterSize;
291 469056 lumCtx->filter_pos = c->vLumFilterPos;
292 469056 lumCtx->isMMX = use_mmx;
293
294
2/2
✓ Branch 0 taken 444144 times.
✓ Branch 1 taken 24912 times.
469056 if (c->vLumFilterSize == 1) lumCtx->pfn.yuv2planar1 = yuv2plane1;
295 24912 else lumCtx->pfn.yuv2planarX = yuv2planeX;
296
297 } else {
298 278448 lumCtx = c->desc[idx].instance;
299 278448 chrCtx = &lumCtx[1];
300
301 278448 lumCtx->filter[0] = c->vLumFilter;
302 278448 lumCtx->filter_size = c->vLumFilterSize;
303 278448 lumCtx->filter_pos = c->vLumFilterPos;
304
305 278448 chrCtx->filter[0] = c->vChrFilter;
306 278448 chrCtx->filter_size = c->vChrFilterSize;
307 278448 chrCtx->filter_pos = c->vChrFilterPos;
308
309 278448 lumCtx->isMMX = use_mmx;
310 278448 chrCtx->isMMX = use_mmx;
311
312
2/2
✓ Branch 0 taken 169324 times.
✓ Branch 1 taken 109124 times.
278448 if (yuv2packedX) {
313
6/6
✓ Branch 0 taken 135550 times.
✓ Branch 1 taken 33774 times.
✓ Branch 2 taken 133765 times.
✓ Branch 3 taken 1785 times.
✓ Branch 4 taken 98541 times.
✓ Branch 5 taken 35224 times.
169324 if (c->yuv2packed1 && c->vLumFilterSize == 1 && c->vChrFilterSize <= 2)
314 98541 lumCtx->pfn.yuv2packed1 = yuv2packed1;
315
3/6
✓ Branch 0 taken 37009 times.
✓ Branch 1 taken 33774 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 37009 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
70783 else if (c->yuv2packed2 && c->vLumFilterSize == 2 && c->vChrFilterSize == 2)
316 lumCtx->pfn.yuv2packed2 = yuv2packed2;
317 169324 lumCtx->yuv2packedX = yuv2packedX;
318 } else
319 109124 lumCtx->pfn.yuv2anyX = yuv2anyX;
320 }
321 747504 }
322