FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/vscale.c
Date: 2024-02-29 09:57:37
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 "swscale_internal.h"
21
22 typedef struct VScalerContext
23 {
24 uint16_t *filter[2];
25 int32_t *filter_pos;
26 int filter_size;
27 int isMMX;
28 union {
29 yuv2planar1_fn yuv2planar1;
30 yuv2planarX_fn yuv2planarX;
31 yuv2interleavedX_fn yuv2interleavedX;
32 yuv2packed1_fn yuv2packed1;
33 yuv2packed2_fn yuv2packed2;
34 yuv2anyX_fn yuv2anyX;
35 } pfn;
36 yuv2packedX_fn yuv2packedX;
37 } VScalerContext;
38
39
40 9076031 static int lum_planar_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
41 {
42 9076031 VScalerContext *inst = desc->instance;
43 9076031 int dstW = desc->dst->width;
44
45 9076031 int first = FFMAX(1-inst->filter_size, inst->filter_pos[sliceY]);
46 9076031 int sp = first - desc->src->plane[0].sliceY;
47 9076031 int dp = sliceY - desc->dst->plane[0].sliceY;
48 9076031 uint8_t **src = desc->src->plane[0].line + sp;
49 9076031 uint8_t **dst = desc->dst->plane[0].line + dp;
50
2/2
✓ Branch 0 taken 9075937 times.
✓ Branch 1 taken 94 times.
9076031 uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : sliceY * inst->filter_size);
51
52
2/2
✓ Branch 0 taken 5642827 times.
✓ Branch 1 taken 3433204 times.
9076031 if (inst->filter_size == 1)
53 5642827 inst->pfn.yuv2planar1((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0);
54 else
55 3433204 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0);
56
57
2/2
✓ Branch 0 taken 88484 times.
✓ Branch 1 taken 8987547 times.
9076031 if (desc->alpha) {
58 88484 int sp = first - desc->src->plane[3].sliceY;
59 88484 int dp = sliceY - desc->dst->plane[3].sliceY;
60 88484 uint8_t **src = desc->src->plane[3].line + sp;
61 88484 uint8_t **dst = desc->dst->plane[3].line + dp;
62
1/2
✓ Branch 0 taken 88484 times.
✗ Branch 1 not taken.
88484 uint16_t *filter = inst->filter[1] + (inst->isMMX ? 0 : sliceY * inst->filter_size);
63
64
2/2
✓ Branch 0 taken 85984 times.
✓ Branch 1 taken 2500 times.
88484 if (inst->filter_size == 1)
65 85984 inst->pfn.yuv2planar1((const int16_t*)src[0], dst[0], dstW, c->lumDither8, 0);
66 else
67 2500 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src, dst[0], dstW, c->lumDither8, 0);
68 }
69
70 9076031 return 1;
71 }
72
73 8818334 static int chr_planar_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
74 {
75 8818334 const int chrSkipMask = (1 << desc->dst->v_chr_sub_sample) - 1;
76
2/2
✓ Branch 0 taken 3063334 times.
✓ Branch 1 taken 5755000 times.
8818334 if (sliceY & chrSkipMask)
77 3063334 return 0;
78 else {
79 5755000 VScalerContext *inst = desc->instance;
80 5755000 int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample);
81 5755000 int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
82
83 5755000 int first = FFMAX(1-inst->filter_size, inst->filter_pos[chrSliceY]);
84 5755000 int sp1 = first - desc->src->plane[1].sliceY;
85 5755000 int sp2 = first - desc->src->plane[2].sliceY;
86 5755000 int dp1 = chrSliceY - desc->dst->plane[1].sliceY;
87 5755000 int dp2 = chrSliceY - desc->dst->plane[2].sliceY;
88 5755000 uint8_t **src1 = desc->src->plane[1].line + sp1;
89 5755000 uint8_t **src2 = desc->src->plane[2].line + sp2;
90 5755000 uint8_t **dst1 = desc->dst->plane[1].line + dp1;
91 5755000 uint8_t **dst2 = desc->dst->plane[2].line + dp2;
92
2/2
✓ Branch 0 taken 5754906 times.
✓ Branch 1 taken 94 times.
5755000 uint16_t *filter = inst->filter[0] + (inst->isMMX ? 0 : chrSliceY * inst->filter_size);
93
94
2/2
✓ Branch 0 taken 75548 times.
✓ Branch 1 taken 5679452 times.
5755000 if (c->yuv2nv12cX) {
95 75548 inst->pfn.yuv2interleavedX(c->dstFormat, c->chrDither8, filter, inst->filter_size, (const int16_t**)src1, (const int16_t**)src2, dst1[0], dstW);
96
2/2
✓ Branch 0 taken 1199162 times.
✓ Branch 1 taken 4480290 times.
5679452 } else if (inst->filter_size == 1) {
97 1199162 inst->pfn.yuv2planar1((const int16_t*)src1[0], dst1[0], dstW, c->chrDither8, 0);
98 1199162 inst->pfn.yuv2planar1((const int16_t*)src2[0], dst2[0], dstW, c->chrDither8, 3);
99 } else {
100 4480290 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src1, dst1[0], dstW, c->chrDither8, 0);
101
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4480290 times.
4480290 inst->pfn.yuv2planarX(filter, inst->filter_size, (const int16_t**)src2, dst2[0], dstW, c->chrDither8, inst->isMMX ? (c->uv_offx2 >> 1) : 3);
102 }
103 }
104
105 5755000 return 1;
106 }
107
108 1902756 static int packed_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
109 {
110 1902756 VScalerContext *inst = desc->instance;
111 1902756 int dstW = desc->dst->width;
112 1902756 int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
113
114 1902756 int lum_fsize = inst[0].filter_size;
115 1902756 int chr_fsize = inst[1].filter_size;
116 1902756 uint16_t *lum_filter = inst[0].filter[0];
117 1902756 uint16_t *chr_filter = inst[1].filter[0];
118
119 1902756 int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]);
120 1902756 int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]);
121
122 1902756 int sp0 = firstLum - desc->src->plane[0].sliceY;
123 1902756 int sp1 = firstChr - desc->src->plane[1].sliceY;
124 1902756 int sp2 = firstChr - desc->src->plane[2].sliceY;
125 1902756 int sp3 = firstLum - desc->src->plane[3].sliceY;
126 1902756 int dp = sliceY - desc->dst->plane[0].sliceY;
127 1902756 uint8_t **src0 = desc->src->plane[0].line + sp0;
128 1902756 uint8_t **src1 = desc->src->plane[1].line + sp1;
129 1902756 uint8_t **src2 = desc->src->plane[2].line + sp2;
130
2/2
✓ Branch 0 taken 8301 times.
✓ Branch 1 taken 1894455 times.
1902756 uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL;
131 1902756 uint8_t **dst = desc->dst->plane[0].line + dp;
132
133
134
6/6
✓ Branch 0 taken 1872968 times.
✓ Branch 1 taken 29788 times.
✓ Branch 2 taken 1840677 times.
✓ Branch 3 taken 32291 times.
✓ Branch 4 taken 684509 times.
✓ Branch 5 taken 1156168 times.
1902756 if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 1) { // unscaled RGB
135 684509 inst->pfn.yuv2packed1(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2,
136
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 684509 times.
684509 (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, 0, sliceY);
137
5/6
✓ Branch 0 taken 1188459 times.
✓ Branch 1 taken 29788 times.
✓ Branch 2 taken 1156168 times.
✓ Branch 3 taken 32291 times.
✗ Branch 4 not taken.
✓ Branch 5 taken 1156168 times.
1218247 } else if (c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2 &&
138 chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 &&
139 chr_filter[2 * chrSliceY + 1] <= 4096U) { // unscaled RGB
140 int chrAlpha = chr_filter[2 * chrSliceY + 1];
141 inst->pfn.yuv2packed1(c, (const int16_t*)*src0, (const int16_t**)src1, (const int16_t**)src2,
142 (const int16_t*)(desc->alpha ? *src3 : NULL), *dst, dstW, chrAlpha, sliceY);
143
3/6
✓ Branch 0 taken 1188459 times.
✓ Branch 1 taken 29788 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1188459 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1218247 } else if (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2 &&
144 lum_filter[2 * sliceY + 1] + lum_filter[2 * sliceY] == 4096 &&
145 lum_filter[2 * sliceY + 1] <= 4096U &&
146 chr_filter[2 * chrSliceY + 1] + chr_filter[2 * chrSliceY] == 4096 &&
147 chr_filter[2 * chrSliceY + 1] <= 4096U
148 ) { // bilinear upscale RGB
149 int lumAlpha = lum_filter[2 * sliceY + 1];
150 int chrAlpha = chr_filter[2 * chrSliceY + 1];
151 c->lumMmxFilter[2] =
152 c->lumMmxFilter[3] = lum_filter[2 * sliceY] * 0x10001;
153 c->chrMmxFilter[2] =
154 c->chrMmxFilter[3] = chr_filter[2 * chrSliceY] * 0x10001;
155 inst->pfn.yuv2packed2(c, (const int16_t**)src0, (const int16_t**)src1, (const int16_t**)src2, (const int16_t**)src3,
156 *dst, dstW, lumAlpha, chrAlpha, sliceY);
157 } else { // general RGB
158
5/6
✓ Branch 0 taken 1188459 times.
✓ Branch 1 taken 29788 times.
✓ Branch 2 taken 1156168 times.
✓ Branch 3 taken 32291 times.
✓ Branch 4 taken 1156168 times.
✗ Branch 5 not taken.
1218247 if ((c->yuv2packed1 && lum_fsize == 1 && chr_fsize == 2) ||
159
3/6
✓ Branch 0 taken 1188459 times.
✓ Branch 1 taken 29788 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1188459 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
1218247 (c->yuv2packed2 && lum_fsize == 2 && chr_fsize == 2)) {
160 if (!c->warned_unuseable_bilinear)
161 av_log(c, AV_LOG_INFO, "Optimized 2 tap filter code cannot be used\n");
162 c->warned_unuseable_bilinear = 1;
163 }
164
165 1218247 inst->yuv2packedX(c, lum_filter + sliceY * lum_fsize,
166 1218247 (const int16_t**)src0, lum_fsize, chr_filter + chrSliceY * chr_fsize,
167 (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, *dst, dstW, sliceY);
168 }
169 1902756 return 1;
170 }
171
172 376676 static int any_vscale(SwsContext *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
173 {
174 376676 VScalerContext *inst = desc->instance;
175 376676 int dstW = desc->dst->width;
176 376676 int chrSliceY = sliceY >> desc->dst->v_chr_sub_sample;
177
178 376676 int lum_fsize = inst[0].filter_size;
179 376676 int chr_fsize = inst[1].filter_size;
180 376676 uint16_t *lum_filter = inst[0].filter[0];
181 376676 uint16_t *chr_filter = inst[1].filter[0];
182
183 376676 int firstLum = FFMAX(1-lum_fsize, inst[0].filter_pos[ sliceY]);
184 376676 int firstChr = FFMAX(1-chr_fsize, inst[1].filter_pos[chrSliceY]);
185
186 376676 int sp0 = firstLum - desc->src->plane[0].sliceY;
187 376676 int sp1 = firstChr - desc->src->plane[1].sliceY;
188 376676 int sp2 = firstChr - desc->src->plane[2].sliceY;
189 376676 int sp3 = firstLum - desc->src->plane[3].sliceY;
190 376676 int dp0 = sliceY - desc->dst->plane[0].sliceY;
191 376676 int dp1 = chrSliceY - desc->dst->plane[1].sliceY;
192 376676 int dp2 = chrSliceY - desc->dst->plane[2].sliceY;
193 376676 int dp3 = sliceY - desc->dst->plane[3].sliceY;
194
195 376676 uint8_t **src0 = desc->src->plane[0].line + sp0;
196 376676 uint8_t **src1 = desc->src->plane[1].line + sp1;
197 376676 uint8_t **src2 = desc->src->plane[2].line + sp2;
198
2/2
✓ Branch 0 taken 2484 times.
✓ Branch 1 taken 374192 times.
376676 uint8_t **src3 = desc->alpha ? desc->src->plane[3].line + sp3 : NULL;
199 753352 uint8_t *dst[4] = { desc->dst->plane[0].line[dp0],
200 376676 desc->dst->plane[1].line[dp1],
201 376676 desc->dst->plane[2].line[dp2],
202
2/2
✓ Branch 0 taken 2484 times.
✓ Branch 1 taken 374192 times.
376676 desc->alpha ? desc->dst->plane[3].line[dp3] : NULL };
203
204 av_assert1(!c->yuv2packed1 && !c->yuv2packed2);
205 376676 inst->pfn.yuv2anyX(c, lum_filter + sliceY * lum_fsize,
206 376676 (const int16_t**)src0, lum_fsize, chr_filter + sliceY * chr_fsize,
207 (const int16_t**)src1, (const int16_t**)src2, chr_fsize, (const int16_t**)src3, dst, dstW, sliceY);
208
209 376676 return 1;
210
211 }
212
213 4761 int ff_init_vscale(SwsContext *c, SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst)
214 {
215 4761 VScalerContext *lumCtx = NULL;
216 4761 VScalerContext *chrCtx = NULL;
217
218
6/6
✓ Branch 1 taken 2212 times.
✓ Branch 2 taken 2549 times.
✓ Branch 4 taken 285 times.
✓ Branch 5 taken 1927 times.
✓ Branch 7 taken 247 times.
✓ Branch 8 taken 38 times.
4761 if (isPlanarYUV(c->dstFormat) || (isGray(c->dstFormat) && !isALPHA(c->dstFormat))) {
219 2796 lumCtx = av_mallocz(sizeof(VScalerContext));
220
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2796 times.
2796 if (!lumCtx)
221 return AVERROR(ENOMEM);
222
223
224 2796 desc[0].process = lum_planar_vscale;
225 2796 desc[0].instance = lumCtx;
226 2796 desc[0].src = src;
227 2796 desc[0].dst = dst;
228 2796 desc[0].alpha = c->needAlpha;
229
230
2/2
✓ Branch 1 taken 2549 times.
✓ Branch 2 taken 247 times.
5345 if (!isGray(c->dstFormat)) {
231 2549 chrCtx = av_mallocz(sizeof(VScalerContext));
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2549 times.
2549 if (!chrCtx)
233 return AVERROR(ENOMEM);
234 2549 desc[1].process = chr_planar_vscale;
235 2549 desc[1].instance = chrCtx;
236 2549 desc[1].src = src;
237 2549 desc[1].dst = dst;
238 }
239 } else {
240 1965 lumCtx = av_calloc(2, sizeof(*lumCtx));
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1965 times.
1965 if (!lumCtx)
242 return AVERROR(ENOMEM);
243 1965 chrCtx = &lumCtx[1];
244
245
2/2
✓ Branch 0 taken 1418 times.
✓ Branch 1 taken 547 times.
1965 desc[0].process = c->yuv2packedX ? packed_vscale : any_vscale;
246 1965 desc[0].instance = lumCtx;
247 1965 desc[0].src = src;
248 1965 desc[0].dst = dst;
249 1965 desc[0].alpha = c->needAlpha;
250 }
251
252 4761 ff_init_vscale_pfn(c, c->yuv2plane1, c->yuv2planeX, c->yuv2nv12cX,
253 c->yuv2packed1, c->yuv2packed2, c->yuv2packedX, c->yuv2anyX, c->use_mmx_vfilter);
254 4761 return 0;
255 }
256
257 144212 void ff_init_vscale_pfn(SwsContext *c,
258 yuv2planar1_fn yuv2plane1,
259 yuv2planarX_fn yuv2planeX,
260 yuv2interleavedX_fn yuv2nv12cX,
261 yuv2packed1_fn yuv2packed1,
262 yuv2packed2_fn yuv2packed2,
263 yuv2packedX_fn yuv2packedX,
264 yuv2anyX_fn yuv2anyX, int use_mmx)
265 {
266 144212 VScalerContext *lumCtx = NULL;
267 144212 VScalerContext *chrCtx = NULL;
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 144212 times.
144212 int idx = c->numDesc - (c->is_internal_gamma ? 2 : 1); //FIXME avoid hardcoding indexes
269
270
6/6
✓ Branch 1 taken 45725 times.
✓ Branch 2 taken 98487 times.
✓ Branch 4 taken 5733 times.
✓ Branch 5 taken 39992 times.
✓ Branch 7 taken 5545 times.
✓ Branch 8 taken 188 times.
144212 if (isPlanarYUV(c->dstFormat) || (isGray(c->dstFormat) && !isALPHA(c->dstFormat))) {
271
2/2
✓ Branch 1 taken 98487 times.
✓ Branch 2 taken 5545 times.
104032 if (!isGray(c->dstFormat)) {
272 98487 chrCtx = c->desc[idx].instance;
273
274
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 98485 times.
98487 chrCtx->filter[0] = use_mmx ? (int16_t*)c->chrMmxFilter : c->vChrFilter;
275 98487 chrCtx->filter_size = c->vChrFilterSize;
276 98487 chrCtx->filter_pos = c->vChrFilterPos;
277 98487 chrCtx->isMMX = use_mmx;
278
279 98487 --idx;
280
2/2
✓ Branch 0 taken 1167 times.
✓ Branch 1 taken 97320 times.
98487 if (yuv2nv12cX) chrCtx->pfn.yuv2interleavedX = yuv2nv12cX;
281
2/2
✓ Branch 0 taken 19649 times.
✓ Branch 1 taken 77671 times.
97320 else if (c->vChrFilterSize == 1) chrCtx->pfn.yuv2planar1 = yuv2plane1;
282 77671 else chrCtx->pfn.yuv2planarX = yuv2planeX;
283 }
284
285 104032 lumCtx = c->desc[idx].instance;
286
287
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 104030 times.
104032 lumCtx->filter[0] = use_mmx ? (int16_t*)c->lumMmxFilter : c->vLumFilter;
288
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 104030 times.
104032 lumCtx->filter[1] = use_mmx ? (int16_t*)c->alpMmxFilter : c->vLumFilter;
289 104032 lumCtx->filter_size = c->vLumFilterSize;
290 104032 lumCtx->filter_pos = c->vLumFilterPos;
291 104032 lumCtx->isMMX = use_mmx;
292
293
2/2
✓ Branch 0 taken 81412 times.
✓ Branch 1 taken 22620 times.
104032 if (c->vLumFilterSize == 1) lumCtx->pfn.yuv2planar1 = yuv2plane1;
294 22620 else lumCtx->pfn.yuv2planarX = yuv2planeX;
295
296 } else {
297 40180 lumCtx = c->desc[idx].instance;
298 40180 chrCtx = &lumCtx[1];
299
300 40180 lumCtx->filter[0] = c->vLumFilter;
301 40180 lumCtx->filter_size = c->vLumFilterSize;
302 40180 lumCtx->filter_pos = c->vLumFilterPos;
303
304 40180 chrCtx->filter[0] = c->vChrFilter;
305 40180 chrCtx->filter_size = c->vChrFilterSize;
306 40180 chrCtx->filter_pos = c->vChrFilterPos;
307
308 40180 lumCtx->isMMX = use_mmx;
309 40180 chrCtx->isMMX = use_mmx;
310
311
2/2
✓ Branch 0 taken 32633 times.
✓ Branch 1 taken 7547 times.
40180 if (yuv2packedX) {
312
6/6
✓ Branch 0 taken 32229 times.
✓ Branch 1 taken 404 times.
✓ Branch 2 taken 30626 times.
✓ Branch 3 taken 1603 times.
✓ Branch 4 taken 13038 times.
✓ Branch 5 taken 17588 times.
32633 if (c->yuv2packed1 && c->vLumFilterSize == 1 && c->vChrFilterSize <= 2)
313 13038 lumCtx->pfn.yuv2packed1 = yuv2packed1;
314
3/6
✓ Branch 0 taken 19191 times.
✓ Branch 1 taken 404 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 19191 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
19595 else if (c->yuv2packed2 && c->vLumFilterSize == 2 && c->vChrFilterSize == 2)
315 lumCtx->pfn.yuv2packed2 = yuv2packed2;
316 32633 lumCtx->yuv2packedX = yuv2packedX;
317 } else
318 7547 lumCtx->pfn.yuv2anyX = yuv2anyX;
319 }
320 144212 }
321
322
323