FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswscale/hscale.c
Date: 2025-06-01 09:29:47
Exec Total Coverage
Lines: 155 162 95.7%
Functions: 10 10 100.0%
Branches: 41 54 75.9%

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 5440562 static int lum_h_scale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
40 {
41 5440562 FilterContext *instance = desc->instance;
42 5440562 int srcW = desc->src->width;
43 5440562 int dstW = desc->dst->width;
44 5440562 int xInc = instance->xInc;
45
46 int i;
47
2/2
✓ Branch 0 taken 26092944 times.
✓ Branch 1 taken 5440562 times.
31533506 for (i = 0; i < sliceH; ++i) {
48 26092944 uint8_t ** src = desc->src->plane[0].line;
49 26092944 uint8_t ** dst = desc->dst->plane[0].line;
50 26092944 int src_pos = sliceY+i - desc->src->plane[0].sliceY;
51 26092944 int dst_pos = sliceY+i - desc->dst->plane[0].sliceY;
52
53
54
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26092944 times.
26092944 if (c->hyscale_fast) {
55 c->hyscale_fast(c, (int16_t*)dst[dst_pos], dstW, src[src_pos], srcW, xInc);
56 } else {
57 26092944 c->hyScale(c, (int16_t*)dst[dst_pos], dstW, (const uint8_t *)src[src_pos], instance->filter,
58 26092944 instance->filter_pos, instance->filter_size);
59 }
60
61
2/2
✓ Branch 0 taken 2692586 times.
✓ Branch 1 taken 23400358 times.
26092944 if (c->lumConvertRange)
62 2692586 c->lumConvertRange((int16_t*)dst[dst_pos], dstW,
63 c->lumConvertRange_coeff, c->lumConvertRange_offset);
64
65 26092944 desc->dst->plane[0].sliceH += 1;
66
67
2/2
✓ Branch 0 taken 115269 times.
✓ Branch 1 taken 25977675 times.
26092944 if (desc->alpha) {
68 115269 src = desc->src->plane[3].line;
69 115269 dst = desc->dst->plane[3].line;
70
71 115269 src_pos = sliceY+i - desc->src->plane[3].sliceY;
72 115269 dst_pos = sliceY+i - desc->dst->plane[3].sliceY;
73
74 115269 desc->dst->plane[3].sliceH += 1;
75
76
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 115269 times.
115269 if (c->hyscale_fast) {
77 c->hyscale_fast(c, (int16_t*)dst[dst_pos], dstW, src[src_pos], srcW, xInc);
78 } else {
79 115269 c->hyScale(c, (int16_t*)dst[dst_pos], dstW, (const uint8_t *)src[src_pos], instance->filter,
80 115269 instance->filter_pos, instance->filter_size);
81 }
82 }
83 }
84
85 5440562 return sliceH;
86 }
87
88 2520330 static int lum_convert(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
89 {
90 2520330 int srcW = desc->src->width;
91 2520330 ColorContext * instance = desc->instance;
92 2520330 uint32_t * pal = instance->pal;
93 int i;
94
95 2520330 desc->dst->plane[0].sliceY = sliceY;
96 2520330 desc->dst->plane[0].sliceH = sliceH;
97 2520330 desc->dst->plane[3].sliceY = sliceY;
98 2520330 desc->dst->plane[3].sliceH = sliceH;
99
100
2/2
✓ Branch 0 taken 12205650 times.
✓ Branch 1 taken 2520330 times.
14725980 for (i = 0; i < sliceH; ++i) {
101 12205650 int sp0 = sliceY+i - desc->src->plane[0].sliceY;
102 12205650 int sp1 = ((sliceY+i) >> desc->src->v_chr_sub_sample) - desc->src->plane[1].sliceY;
103 12205650 const uint8_t * src[4] = { desc->src->plane[0].line[sp0],
104 12205650 desc->src->plane[1].line[sp1],
105 12205650 desc->src->plane[2].line[sp1],
106 12205650 desc->src->plane[3].line[sp0]};
107 12205650 uint8_t * dst = desc->dst->plane[0].line[i];
108
109
2/2
✓ Branch 0 taken 9298066 times.
✓ Branch 1 taken 2907584 times.
12205650 if (c->lumToYV12) {
110 9298066 c->lumToYV12(dst, src[0], src[1], src[2], srcW, pal, c->input_opaque);
111
1/2
✓ Branch 0 taken 2907584 times.
✗ Branch 1 not taken.
2907584 } else if (c->readLumPlanar) {
112 2907584 c->readLumPlanar(dst, src, srcW, c->input_rgb2yuv_table, c->input_opaque);
113 }
114
115
116
2/2
✓ Branch 0 taken 111237 times.
✓ Branch 1 taken 12094413 times.
12205650 if (desc->alpha) {
117 111237 dst = desc->dst->plane[3].line[i];
118
2/2
✓ Branch 0 taken 104693 times.
✓ Branch 1 taken 6544 times.
111237 if (c->alpToYV12) {
119 104693 c->alpToYV12(dst, src[3], src[1], src[2], srcW, pal, c->input_opaque);
120
1/2
✓ Branch 0 taken 6544 times.
✗ Branch 1 not taken.
6544 } else if (c->readAlpPlanar) {
121 6544 c->readAlpPlanar(dst, src, srcW, NULL, c->input_opaque);
122 }
123 }
124 }
125
126 2520330 return sliceH;
127 }
128
129 13744 int ff_init_desc_fmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal)
130 {
131 13744 ColorContext * li = av_malloc(sizeof(ColorContext));
132
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 13744 times.
13744 if (!li)
133 return AVERROR(ENOMEM);
134 13744 li->pal = pal;
135 13744 desc->instance = li;
136
137
3/4
✓ Branch 1 taken 608 times.
✓ Branch 2 taken 13136 times.
✓ Branch 4 taken 608 times.
✗ Branch 5 not taken.
13744 desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
138 13744 desc->src =src;
139 13744 desc->dst = dst;
140 13744 desc->process = &lum_convert;
141
142 13744 return 0;
143 }
144
145
146 26280 int ff_init_desc_hscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc)
147 {
148 26280 FilterContext *li = av_malloc(sizeof(FilterContext));
149
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 26280 times.
26280 if (!li)
150 return AVERROR(ENOMEM);
151
152 26280 li->filter = filter;
153 26280 li->filter_pos = filter_pos;
154 26280 li->filter_size = filter_size;
155 26280 li->xInc = xInc;
156
157 26280 desc->instance = li;
158
159
3/4
✓ Branch 1 taken 629 times.
✓ Branch 2 taken 25651 times.
✓ Branch 4 taken 629 times.
✗ Branch 5 not taken.
26280 desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
160 26280 desc->src = src;
161 26280 desc->dst = dst;
162
163 26280 desc->process = &lum_h_scale;
164
165 26280 return 0;
166 }
167
168 5760890 static int chr_h_scale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
169 {
170 5760890 FilterContext *instance = desc->instance;
171 5760890 int srcW = AV_CEIL_RSHIFT(desc->src->width, desc->src->h_chr_sub_sample);
172 5760890 int dstW = AV_CEIL_RSHIFT(desc->dst->width, desc->dst->h_chr_sub_sample);
173 5760890 int xInc = instance->xInc;
174
175 5760890 uint8_t ** src1 = desc->src->plane[1].line;
176 5760890 uint8_t ** dst1 = desc->dst->plane[1].line;
177 5760890 uint8_t ** src2 = desc->src->plane[2].line;
178 5760890 uint8_t ** dst2 = desc->dst->plane[2].line;
179
180 5760890 int src_pos1 = sliceY - desc->src->plane[1].sliceY;
181 5760890 int dst_pos1 = sliceY - desc->dst->plane[1].sliceY;
182
183 5760890 int src_pos2 = sliceY - desc->src->plane[2].sliceY;
184 5760890 int dst_pos2 = sliceY - desc->dst->plane[2].sliceY;
185
186 int i;
187
2/2
✓ Branch 0 taken 20037634 times.
✓ Branch 1 taken 5760890 times.
25798524 for (i = 0; i < sliceH; ++i) {
188
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20037634 times.
20037634 if (c->hcscale_fast) {
189 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 20037634 c->hcScale(c, (uint16_t*)dst1[dst_pos1+i], dstW, src1[src_pos1+i], instance->filter, instance->filter_pos, instance->filter_size);
192 20037634 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 950448 times.
✓ Branch 1 taken 19087186 times.
20037634 if (c->chrConvertRange)
196 950448 c->chrConvertRange((uint16_t*)dst1[dst_pos1+i], (uint16_t*)dst2[dst_pos2+i], dstW,
197 c->chrConvertRange_coeff, c->chrConvertRange_offset);
198
199 20037634 desc->dst->plane[1].sliceH += 1;
200 20037634 desc->dst->plane[2].sliceH += 1;
201 }
202 5760890 return sliceH;
203 }
204
205 3481986 static int chr_convert(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
206 {
207 3481986 int srcW = AV_CEIL_RSHIFT(desc->src->width, desc->src->h_chr_sub_sample);
208 3481986 ColorContext * instance = desc->instance;
209 3481986 uint32_t * pal = instance->pal;
210
211 3481986 int sp0 = (sliceY - (desc->src->plane[0].sliceY >> desc->src->v_chr_sub_sample)) << desc->src->v_chr_sub_sample;
212 3481986 int sp1 = sliceY - desc->src->plane[1].sliceY;
213
214 int i;
215
216 3481986 desc->dst->plane[1].sliceY = sliceY;
217 3481986 desc->dst->plane[1].sliceH = sliceH;
218 3481986 desc->dst->plane[2].sliceY = sliceY;
219 3481986 desc->dst->plane[2].sliceH = sliceH;
220
221
2/2
✓ Branch 0 taken 12467496 times.
✓ Branch 1 taken 3481986 times.
15949482 for (i = 0; i < sliceH; ++i) {
222 12467496 const uint8_t * src[4] = { desc->src->plane[0].line[sp0+i],
223 12467496 desc->src->plane[1].line[sp1+i],
224 12467496 desc->src->plane[2].line[sp1+i],
225 12467496 desc->src->plane[3].line[sp0+i]};
226
227 12467496 uint8_t * dst1 = desc->dst->plane[1].line[i];
228 12467496 uint8_t * dst2 = desc->dst->plane[2].line[i];
229
2/2
✓ Branch 0 taken 9924778 times.
✓ Branch 1 taken 2542718 times.
12467496 if (c->chrToYV12) {
230 9924778 c->chrToYV12(dst1, dst2, src[0], src[1], src[2], srcW, pal, c->input_opaque);
231
1/2
✓ Branch 0 taken 2542718 times.
✗ Branch 1 not taken.
2542718 } else if (c->readChrPlanar) {
232 2542718 c->readChrPlanar(dst1, dst2, src, srcW, c->input_rgb2yuv_table, c->input_opaque);
233 }
234 }
235 3481986 return sliceH;
236 }
237
238 14739 int ff_init_desc_cfmt_convert(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst, uint32_t *pal)
239 {
240 14739 ColorContext * li = av_malloc(sizeof(ColorContext));
241
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 14739 times.
14739 if (!li)
242 return AVERROR(ENOMEM);
243 14739 li->pal = pal;
244 14739 desc->instance = li;
245
246 14739 desc->src =src;
247 14739 desc->dst = dst;
248 14739 desc->process = &chr_convert;
249
250 14739 return 0;
251 }
252
253 24005 int ff_init_desc_chscale(SwsFilterDescriptor *desc, SwsSlice *src, SwsSlice *dst, uint16_t *filter, int * filter_pos, int filter_size, int xInc)
254 {
255 24005 FilterContext *li = av_malloc(sizeof(FilterContext));
256
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 24005 times.
24005 if (!li)
257 return AVERROR(ENOMEM);
258
259 24005 li->filter = filter;
260 24005 li->filter_pos = filter_pos;
261 24005 li->filter_size = filter_size;
262 24005 li->xInc = xInc;
263
264 24005 desc->instance = li;
265
266
3/4
✓ Branch 1 taken 527 times.
✓ Branch 2 taken 23478 times.
✓ Branch 4 taken 527 times.
✗ Branch 5 not taken.
24005 desc->alpha = isALPHA(src->fmt) && isALPHA(dst->fmt);
267 24005 desc->src = src;
268 24005 desc->dst = dst;
269
270 24005 desc->process = &chr_h_scale;
271
272 24005 return 0;
273 }
274
275 405749 static int no_chr_scale(SwsInternal *c, SwsFilterDescriptor *desc, int sliceY, int sliceH)
276 {
277 405749 desc->dst->plane[1].sliceY = sliceY + sliceH - desc->dst->plane[1].available_lines;
278 405749 desc->dst->plane[1].sliceH = desc->dst->plane[1].available_lines;
279 405749 desc->dst->plane[2].sliceY = sliceY + sliceH - desc->dst->plane[2].available_lines;
280 405749 desc->dst->plane[2].sliceH = desc->dst->plane[2].available_lines;
281 405749 return 0;
282 }
283
284 2275 int ff_init_desc_no_chr(SwsFilterDescriptor *desc, SwsSlice * src, SwsSlice *dst)
285 {
286 2275 desc->src = src;
287 2275 desc->dst = dst;
288 2275 desc->alpha = 0;
289 2275 desc->instance = NULL;
290 2275 desc->process = &no_chr_scale;
291 2275 return 0;
292 }
293