FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/hscale.c
Date: 2026-11-04 23:20:56
Exec Total Coverage
Lines: 157 162 96.9%
Functions: 10 10 100.0%
Branches: 43 54 79.6%

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 /// Scaler instance data
25 typedef struct FilterContext
26 {
27 uint16_t *filter;
28 int *filter_pos;
29 int filter_size;
30 int xInc;
31 } FilterContext;
32
33 /// Color conversion instance data
34 typedef struct ColorContext
35 {
36 uint32_t *pal;
37 } ColorContext;
38
39 8042966 static int lum_h_scale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
40 {
41 8042966 FilterContext *instance = desc->instance;
42 8042966 int srcW = desc->src->width;
43 8042966 int dstW = desc->dst->width;
44 8042966 int xInc = instance->xInc;
45
46 int i;
47
2/2
✓ Branch 0 taken 39108238 times.
✓ Branch 1 taken 8042966 times.
47151204 for (i = 0; i < sliceH; ++i) {
48 39108238 uint8_t ** src = desc->src->plane[0].line;
49 39108238 uint8_t ** dst = desc->dst->plane[0].line;
50 39108238 int src_pos = sliceY+i - desc->src->plane[0].sliceY;
51 39108238 int dst_pos = sliceY+i - desc->dst->plane[0].sliceY;
52
53
54
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 39108237 times.
39108238 if (c->hyscale_fast) {
55 1 c->hyscale_fast(c, (int16_t*)dst[dst_pos], dstW, src[src_pos], srcW, xInc);
56 } else {
57 39108237 c->hyScale(c, (int16_t*)dst[dst_pos], dstW, (const uint8_t *)src[src_pos], instance->filter,
58 39108237 instance->filter_pos, instance->filter_size);
59 }
60
61
2/2
✓ Branch 0 taken 4383585 times.
✓ Branch 1 taken 34724653 times.
39108238 if (c->lumConvertRange)
62 4383585 c->lumConvertRange((int16_t*)dst[dst_pos], dstW,
63 c->lumConvertRange_coeff, c->lumConvertRange_offset);
64
65 39108238 desc->dst->plane[0].sliceH += 1;
66
67
2/2
✓ Branch 0 taken 3268885 times.
✓ Branch 1 taken 35839353 times.
39108238 if (desc->alpha) {
68 3268885 src = desc->src->plane[3].line;
69 3268885 dst = desc->dst->plane[3].line;
70
71 3268885 src_pos = sliceY+i - desc->src->plane[3].sliceY;
72 3268885 dst_pos = sliceY+i - desc->dst->plane[3].sliceY;
73
74 3268885 desc->dst->plane[3].sliceH += 1;
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3268885 times.
3268885 if (c->hyscale_fast) {
77 ✗ c->hyscale_fast(c, (int16_t*)dst[dst_pos], dstW, src[src_pos], srcW, xInc);
78 } else {
79 3268885 c->hyScale(c, (int16_t*)dst[dst_pos], dstW, (const uint8_t *)src[src_pos], instance->filter,
80 3268885 instance->filter_pos, instance->filter_size);
81 }
82 }
83 }
84
85 8042966 return sliceH;
86 }
87
88 4330003 static int lum_convert(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
89 {
90 4330003 int srcW = desc->src->width;
91 4330003 ColorContext * instance = desc->instance;
92 4330003 uint32_t * pal = instance->pal;
93 int i;
94
95 4330003 desc->dst->plane[0].sliceY = sliceY;
96 4330003 desc->dst->plane[0].sliceH = sliceH;
97 4330003 desc->dst->plane[3].sliceY = sliceY;
98 4330003 desc->dst->plane[3].sliceH = sliceH;
99
100
2/2
✓ Branch 0 taken 21198535 times.
✓ Branch 1 taken 4330003 times.
25528538 for (i = 0; i < sliceH; ++i) {
101 21198535 int sp0 = sliceY+i - desc->src->plane[0].sliceY;
102 21198535 int sp1 = ((sliceY+i) >> desc->src->v_chr_sub_sample) - desc->src->plane[1].sliceY;
103 21198535 const uint8_t * src[4] = { desc->src->plane[0].line[sp0],
104 21198535 desc->src->plane[1].line[sp1],
105 21198535 desc->src->plane[2].line[sp1],
106 21198535 desc->src->plane[3].line[sp0]};
107 21198535 uint8_t * dst = desc->dst->plane[0].line[i];
108
109
2/2
✓ Branch 0 taken 16036355 times.
✓ Branch 1 taken 5162180 times.
21198535 if (c->lumToYV12) {
110 16036355 c->lumToYV12(dst, src[0], src[1], src[2], srcW, pal, c->input_opaque);
111
1/2
✓ Branch 0 taken 5162180 times.
✗ Branch 1 not taken.
5162180 } else if (c->readLumPlanar) {
112 5162180 c->readLumPlanar(dst, src, srcW, c->input_rgb2yuv_table, c->input_opaque);
113 }
114
115
116
2/2
✓ Branch 0 taken 2958149 times.
✓ Branch 1 taken 18240386 times.
21198535 if (desc->alpha) {
117 2958149 dst = desc->dst->plane[3].line[i];
118
2/2
✓ Branch 0 taken 2260789 times.
✓ Branch 1 taken 697360 times.
2958149 if (c->alpToYV12) {
119 2260789 c->alpToYV12(dst, src[3], src[1], src[2], srcW, pal, c->input_opaque);
120
1/2
✓ Branch 0 taken 697360 times.
✗ Branch 1 not taken.
697360 } else if (c->readAlpPlanar) {
121 697360 c->readAlpPlanar(dst, src, srcW, NULL, c->input_opaque);
122 }
123 }
124 }
125
126 4330003 return sliceH;
127 }
128
129 99033 int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal)
130 {
131 99033 ColorContext * li = av_malloc(sizeof(ColorContext));
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 99033 times.
99033 if (!li)
133 ✗ return AVERROR(ENOMEM);
134 99033 li->pal = pal;
135 99033 desc->instance = li;
136
137
3/4
✓ Branch 1 taken 29056 times.
✓ Branch 2 taken 69977 times.
✓ Branch 4 taken 29056 times.
✗ Branch 5 not taken.
99033 desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
138 99033 desc->src =src;
139 99033 desc->dst = dst;
140 99033 desc->process = &lum_convert;
141
142 99033 return 0;
143 }
144
145
146 148817 int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc)
147 {
148 148817 FilterContext *li = av_malloc(sizeof(FilterContext));
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 148817 times.
148817 if (!li)
150 ✗ return AVERROR(ENOMEM);
151
152 148817 li->filter = filter;
153 148817 li->filter_pos = filter_pos;
154 148817 li->filter_size = filter_size;
155 148817 li->xInc = xInc;
156
157 148817 desc->instance = li;
158
159
3/4
✓ Branch 1 taken 36900 times.
✓ Branch 2 taken 111917 times.
✓ Branch 4 taken 36900 times.
✗ Branch 5 not taken.
148817 desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
160 148817 desc->src = src;
161 148817 desc->dst = dst;
162
163 148817 desc->process = &lum_h_scale;
164
165 148817 return 0;
166 }
167
168 7829610 static int chr_h_scale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
169 {
170 7829610 FilterContext *instance = desc->instance;
171 7829610 int srcW = AV_CEIL_RSHIFT(desc->src->width, desc->src->h_chr_sub_sample);
172 7829610 int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample);
173 7829610 int xInc = instance->xInc;
174
175 7829610 uint8_t ** src1 = desc->src->plane[1].line;
176 7829610 uint8_t ** dst1 = desc->dst->plane[1].line;
177 7829610 uint8_t ** src2 = desc->src->plane[2].line;
178 7829610 uint8_t ** dst2 = desc->dst->plane[2].line;
179
180 7829610 int src_pos1 = sliceY - desc->src->plane[1].sliceY;
181 7829610 int dst_pos1 = sliceY - desc->dst->plane[1].sliceY;
182
183 7829610 int src_pos2 = sliceY - desc->src->plane[2].sliceY;
184 7829610 int dst_pos2 = sliceY - desc->dst->plane[2].sliceY;
185
186 int i;
187
2/2
✓ Branch 0 taken 30169727 times.
✓ Branch 1 taken 7829610 times.
37999337 for (i = 0; i < sliceH; ++i) {
188
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 30169726 times.
30169727 if (c->hcscale_fast) {
189 1 c->hcscale_fast(c, (uint16_t*)dst1[dst_pos1+i], (uint16_t*)dst2[dst_pos2+i], dstW, src1[src_pos1+i], src2[src_pos2+i], srcW, xInc);
190 } else {
191 30169726 c->hcScale(c, (uint16_t*)dst1[dst_pos1+i], dstW, src1[src_pos1+i], instance->filter, instance->filter_pos, instance->filter_size);
192 30169726 c->hcScale(c, (uint16_t*)dst2[dst_pos2+i], dstW, src2[src_pos2+i], instance->filter, instance->filter_pos, instance->filter_size);
193 }
194
195
2/2
✓ Branch 0 taken 1112380 times.
✓ Branch 1 taken 29057347 times.
30169727 if (c->chrConvertRange)
196 1112380 c->chrConvertRange((uint16_t*)dst1[dst_pos1+i], (uint16_t*)dst2[dst_pos2+i], dstW,
197 c->chrConvertRange_coeff, c->chrConvertRange_offset);
198
199 30169727 desc->dst->plane[1].sliceH += 1;
200 30169727 desc->dst->plane[2].sliceH += 1;
201 }
202 7829610 return sliceH;
203 }
204
205 5117699 static int chr_convert(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
206 {
207 5117699 int srcW = AV_CEIL_RSHIFT(desc->src->width, desc->src->h_chr_sub_sample);
208 5117699 ColorContext * instance = desc->instance;
209 5117699 uint32_t * pal = instance->pal;
210
211 5117699 int sp0 = (sliceY - (desc->src->plane[0].sliceY >> desc->src->v_chr_sub_sample)) << desc->src->v_chr_sub_sample;
212 5117699 int sp1 = sliceY - desc->src->plane[1].sliceY;
213
214 int i;
215
216 5117699 desc->dst->plane[1].sliceY = sliceY;
217 5117699 desc->dst->plane[1].sliceH = sliceH;
218 5117699 desc->dst->plane[2].sliceY = sliceY;
219 5117699 desc->dst->plane[2].sliceH = sliceH;
220
221
2/2
✓ Branch 0 taken 20538445 times.
✓ Branch 1 taken 5117699 times.
25656144 for (i = 0; i < sliceH; ++i) {
222 20538445 const uint8_t * src[4] = { desc->src->plane[0].line[sp0+i],
223 20538445 desc->src->plane[1].line[sp1+i],
224 20538445 desc->src->plane[2].line[sp1+i],
225 20538445 desc->src->plane[3].line[sp0+i]};
226
227 20538445 uint8_t * dst1 = desc->dst->plane[1].line[i];
228 20538445 uint8_t * dst2 = desc->dst->plane[2].line[i];
229
2/2
✓ Branch 0 taken 15741131 times.
✓ Branch 1 taken 4797314 times.
20538445 if (c->chrToYV12) {
230 15741131 c->chrToYV12(dst1, dst2, src[0], src[1], src[2], srcW, pal, c->input_opaque);
231
1/2
✓ Branch 0 taken 4797314 times.
✗ Branch 1 not taken.
4797314 } else if (c->readChrPlanar) {
232 4797314 c->readChrPlanar(dst1, dst2, src, srcW, c->input_rgb2yuv_table, c->input_opaque);
233 }
234 }
235 5117699 return sliceH;
236 }
237
238 100196 int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal)
239 {
240 100196 ColorContext * li = av_malloc(sizeof(ColorContext));
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 100196 times.
100196 if (!li)
242 ✗ return AVERROR(ENOMEM);
243 100196 li->pal = pal;
244 100196 desc->instance = li;
245
246 100196 desc->src =src;
247 100196 desc->dst = dst;
248 100196 desc->process = &chr_convert;
249
250 100196 return 0;
251 }
252
253 130680 int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc)
254 {
255 130680 FilterContext *li = av_malloc(sizeof(FilterContext));
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130680 times.
130680 if (!li)
257 ✗ return AVERROR(ENOMEM);
258
259 130680 li->filter = filter;
260 130680 li->filter_pos = filter_pos;
261 130680 li->filter_size = filter_size;
262 130680 li->xInc = xInc;
263
264 130680 desc->instance = li;
265
266
3/4
✓ Branch 1 taken 33705 times.
✓ Branch 2 taken 96975 times.
✓ Branch 4 taken 33705 times.
✗ Branch 5 not taken.
130680 desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
267 130680 desc->src = src;
268 130680 desc->dst = dst;
269
270 130680 desc->process = &chr_h_scale;
271
272 130680 return 0;
273 }
274
275 734427 static int no_chr_scale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
276 {
277 734427 desc->dst->plane[1].sliceY = sliceY + sliceH - desc->dst->plane[1].available_lines;
278 734427 desc->dst->plane[1].sliceH = desc->dst->plane[1].available_lines;
279 734427 desc->dst->plane[2].sliceY = sliceY + sliceH - desc->dst->plane[2].available_lines;
280 734427 desc->dst->plane[2].sliceH = desc->dst->plane[2].available_lines;
281 734427 return 0;
282 }
283
284 18137 int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst)
285 {
286 18137 desc->src = src;
287 18137 desc->dst = dst;
288 18137 desc->alpha = 0;
289 18137 desc->instance = NULL;
290 18137 desc->process = &no_chr_scale;
291 18137 return 0;
292 }
293