Coverage Report

Created: 2026-04-01 07:42

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/mpegvideoencdsp.c
Line
Count
Source
1
/*
2
 * This file is part of FFmpeg.
3
 *
4
 * FFmpeg is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
6
 * License as published by the Free Software Foundation; either
7
 * version 2.1 of the License, or (at your option) any later version.
8
 *
9
 * FFmpeg is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12
 * Lesser General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
15
 * License along with FFmpeg; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
 */
18
19
#include <assert.h>
20
#include <stdint.h>
21
#include <string.h>
22
23
#include "config.h"
24
#include "libavutil/avassert.h"
25
#include "libavutil/attributes.h"
26
#include "libavutil/imgutils.h"
27
#include "avcodec.h"
28
#include "mathops.h"
29
#include "mpegvideoencdsp.h"
30
31
static void denoise_dct_c(int16_t block[64], int dct_error_sum[64],
32
                          const uint16_t dct_offset[64])
33
0
{
34
0
    for (int i = 0; i < 64; ++i) {
35
0
        int level = block[i];
36
37
0
        if (level) {
38
0
            if (level > 0) {
39
0
                dct_error_sum[i] += level;
40
0
                level -= dct_offset[i];
41
0
                if (level < 0)
42
0
                    level = 0;
43
0
            } else {
44
0
                dct_error_sum[i] -= level;
45
0
                level += dct_offset[i];
46
0
                if (level > 0)
47
0
                    level = 0;
48
0
            }
49
0
            block[i] = level;
50
0
        }
51
0
    }
52
0
}
53
54
static int try_8x8basis_c(const int16_t rem[64], const int16_t weight[64],
55
                          const int16_t basis[64], int scale)
56
0
{
57
0
    int i;
58
0
    unsigned int sum = 0;
59
60
0
    for (i = 0; i < 8 * 8; i++) {
61
0
        int b = rem[i] + ((basis[i] * scale +
62
0
                           (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
63
0
                          (BASIS_SHIFT - RECON_SHIFT));
64
0
        int w = weight[i];
65
0
        b >>= RECON_SHIFT;
66
0
        av_assert2(-512 < b && b < 512);
67
68
0
        sum += (w * b) * (w * b) >> 4;
69
0
    }
70
0
    return sum >> 2;
71
0
}
72
73
static void add_8x8basis_c(int16_t rem[64], const int16_t basis[64], int scale)
74
0
{
75
0
    int i;
76
77
0
    for (i = 0; i < 8 * 8; i++)
78
0
        rem[i] += (basis[i] * scale +
79
0
                   (1 << (BASIS_SHIFT - RECON_SHIFT - 1))) >>
80
0
                  (BASIS_SHIFT - RECON_SHIFT);
81
0
}
82
83
static int pix_sum_c(const uint8_t *pix, ptrdiff_t line_size)
84
4.12M
{
85
4.12M
    int s = 0, i, j;
86
87
70.1M
    for (i = 0; i < 16; i++) {
88
197M
        for (j = 0; j < 16; j += 8) {
89
131M
            s   += pix[0];
90
131M
            s   += pix[1];
91
131M
            s   += pix[2];
92
131M
            s   += pix[3];
93
131M
            s   += pix[4];
94
131M
            s   += pix[5];
95
131M
            s   += pix[6];
96
131M
            s   += pix[7];
97
131M
            pix += 8;
98
131M
        }
99
65.9M
        pix += line_size - 16;
100
65.9M
    }
101
4.12M
    return s;
102
4.12M
}
103
104
static int pix_norm1_c(const uint8_t *pix, ptrdiff_t line_size)
105
4.12M
{
106
4.12M
    int s = 0, i, j;
107
4.12M
    const uint32_t *sq = ff_square_tab + 256;
108
109
70.1M
    for (i = 0; i < 16; i++) {
110
197M
        for (j = 0; j < 16; j += 8) {
111
#if HAVE_FAST_64BIT
112
            register uint64_t x = *(uint64_t *) pix;
113
            s += sq[x         & 0xff];
114
            s += sq[(x >>  8) & 0xff];
115
            s += sq[(x >> 16) & 0xff];
116
            s += sq[(x >> 24) & 0xff];
117
            s += sq[(x >> 32) & 0xff];
118
            s += sq[(x >> 40) & 0xff];
119
            s += sq[(x >> 48) & 0xff];
120
            s += sq[(x >> 56) & 0xff];
121
#else
122
131M
            register uint32_t x = *(uint32_t *) pix;
123
131M
            s += sq[x         & 0xff];
124
131M
            s += sq[(x >>  8) & 0xff];
125
131M
            s += sq[(x >> 16) & 0xff];
126
131M
            s += sq[(x >> 24) & 0xff];
127
131M
            x  = *(uint32_t *) (pix + 4);
128
131M
            s += sq[x         & 0xff];
129
131M
            s += sq[(x >>  8) & 0xff];
130
131M
            s += sq[(x >> 16) & 0xff];
131
131M
            s += sq[(x >> 24) & 0xff];
132
131M
#endif
133
131M
            pix += 8;
134
131M
        }
135
65.9M
        pix += line_size - 16;
136
65.9M
    }
137
4.12M
    return s;
138
4.12M
}
139
140
static av_always_inline void draw_edges_lr(uint8_t *ptr, ptrdiff_t wrap, int width, int height, int w)
141
310k
{
142
131M
    for (int i = 0; i < height; i++) {
143
131M
        memset(ptr - w, ptr[0], w);
144
131M
        memset(ptr + width, ptr[width - 1], w);
145
131M
        ptr += wrap;
146
131M
    }
147
310k
}
148
149
/* draw the edges of width 'w' of an image of size width, height */
150
// FIXME: Check that this is OK for MPEG-4 interlaced.
151
static void draw_edges_8_c(uint8_t *buf, ptrdiff_t wrap, int width, int height,
152
                           int w, int h, int sides)
153
310k
{
154
310k
    uint8_t *last_line;
155
310k
    int i;
156
157
    /* left and right */
158
310k
    if (w == 16) {
159
95.3k
        draw_edges_lr(buf, wrap, width, height, 16);
160
215k
    } else if (w == 8) {
161
187k
        draw_edges_lr(buf, wrap, width, height, 8);
162
187k
    } else {
163
28.0k
        av_assert1(w == 4);
164
28.0k
        draw_edges_lr(buf, wrap, width, height, 4);
165
28.0k
    }
166
167
    /* top and bottom + corners */
168
310k
    buf -= w;
169
310k
    last_line = buf + (height - 1) * wrap;
170
310k
    if (sides & EDGE_TOP)
171
3.28M
        for (i = 0; i < h; i++)
172
            // top
173
2.98M
            memcpy(buf - (i + 1) * wrap, buf, width + w + w);
174
310k
    if (sides & EDGE_BOTTOM)
175
3.45M
        for (i = 0; i < h; i++)
176
            // bottom
177
3.13M
            memcpy(last_line + (i + 1) * wrap, last_line, width + w + w);
178
310k
}
179
180
/* This wrapper function only serves to convert the stride parameters
181
 * from ptrdiff_t to int for av_image_copy_plane(). */
182
static void copy_plane_wrapper(uint8_t *dst, ptrdiff_t dst_wrap,
183
                               const uint8_t *src, ptrdiff_t src_wrap,
184
                               int width, int height)
185
0
{
186
0
    av_image_copy_plane(dst, dst_wrap, src, src_wrap, width, height);
187
0
}
188
189
/* 2x2 -> 1x1 */
190
static void shrink22(uint8_t *dst, ptrdiff_t dst_wrap,
191
                     const uint8_t *src, ptrdiff_t src_wrap,
192
                     int width, int height)
193
0
{
194
0
    int w;
195
0
    const uint8_t *s1, *s2;
196
0
    uint8_t *d;
197
198
0
    for (; height > 0; height--) {
199
0
        s1 = src;
200
0
        s2 = s1 + src_wrap;
201
0
        d = dst;
202
0
        for (w = width; w >= 4; w -= 4) {
203
0
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
204
0
            d[1] = (s1[2] + s1[3] + s2[2] + s2[3] + 2) >> 2;
205
0
            d[2] = (s1[4] + s1[5] + s2[4] + s2[5] + 2) >> 2;
206
0
            d[3] = (s1[6] + s1[7] + s2[6] + s2[7] + 2) >> 2;
207
0
            s1 += 8;
208
0
            s2 += 8;
209
0
            d += 4;
210
0
        }
211
0
        for (; w > 0; w--) {
212
0
            d[0] = (s1[0] + s1[1] + s2[0] + s2[1] + 2) >> 2;
213
0
            s1 += 2;
214
0
            s2 += 2;
215
0
            d++;
216
0
        }
217
0
        src += 2 * src_wrap;
218
0
        dst += dst_wrap;
219
0
    }
220
0
}
221
222
/* 4x4 -> 1x1 */
223
static void shrink44(uint8_t *dst, ptrdiff_t dst_wrap,
224
                     const uint8_t *src, ptrdiff_t src_wrap,
225
                     int width, int height)
226
0
{
227
0
    int w;
228
0
    const uint8_t *s1, *s2, *s3, *s4;
229
0
    uint8_t *d;
230
231
0
    for (; height > 0; height--) {
232
0
        s1 = src;
233
0
        s2 = s1 + src_wrap;
234
0
        s3 = s2 + src_wrap;
235
0
        s4 = s3 + src_wrap;
236
0
        d = dst;
237
0
        for (w = width; w > 0; w--) {
238
0
            d[0] = (s1[0] + s1[1] + s1[2] + s1[3] +
239
0
                    s2[0] + s2[1] + s2[2] + s2[3] +
240
0
                    s3[0] + s3[1] + s3[2] + s3[3] +
241
0
                    s4[0] + s4[1] + s4[2] + s4[3] + 8) >> 4;
242
0
            s1 += 4;
243
0
            s2 += 4;
244
0
            s3 += 4;
245
0
            s4 += 4;
246
0
            d++;
247
0
        }
248
0
        src += 4 * src_wrap;
249
0
        dst += dst_wrap;
250
0
    }
251
0
}
252
253
/* 8x8 -> 1x1 */
254
static void shrink88(uint8_t *dst, ptrdiff_t dst_wrap,
255
                     const uint8_t *src, ptrdiff_t src_wrap,
256
                     int width, int height)
257
0
{
258
0
    int w, i;
259
260
0
    for (; height > 0; height--) {
261
0
        for(w = width;w > 0; w--) {
262
0
            int tmp = 0;
263
0
            for (i = 0; i < 8; i++) {
264
0
                tmp += src[0] + src[1] + src[2] + src[3] +
265
0
                       src[4] + src[5] + src[6] + src[7];
266
0
                src += src_wrap;
267
0
            }
268
0
            *(dst++) = (tmp + 32) >> 6;
269
0
            src += 8 - 8 * src_wrap;
270
0
        }
271
0
        src += 8 * src_wrap - 8 * width;
272
0
        dst += dst_wrap - width;
273
0
    }
274
0
}
275
276
av_cold void ff_mpegvideoencdsp_init(MpegvideoEncDSPContext *c,
277
                                     AVCodecContext *avctx)
278
21.9k
{
279
21.9k
    c->denoise_dct  = denoise_dct_c;
280
281
21.9k
    c->try_8x8basis = try_8x8basis_c;
282
21.9k
    c->add_8x8basis = add_8x8basis_c;
283
284
21.9k
    c->shrink[0] = copy_plane_wrapper;
285
21.9k
    c->shrink[1] = shrink22;
286
21.9k
    c->shrink[2] = shrink44;
287
21.9k
    c->shrink[3] = shrink88;
288
289
21.9k
    c->pix_sum   = pix_sum_c;
290
21.9k
    c->pix_norm1 = pix_norm1_c;
291
292
21.9k
    c->draw_edges = draw_edges_8_c;
293
294
#if ARCH_AARCH64
295
    ff_mpegvideoencdsp_init_aarch64(c, avctx);
296
#elif ARCH_ARM
297
    ff_mpegvideoencdsp_init_arm(c, avctx);
298
#elif ARCH_PPC
299
    ff_mpegvideoencdsp_init_ppc(c, avctx);
300
#elif ARCH_RISCV
301
    ff_mpegvideoencdsp_init_riscv(c, avctx);
302
#elif ARCH_X86
303
    ff_mpegvideoencdsp_init_x86(c, avctx);
304
#elif ARCH_MIPS
305
    ff_mpegvideoencdsp_init_mips(c, avctx);
306
#endif
307
21.9k
}