Coverage Report

Created: 2026-10-28 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/ffmpeg/libavcodec/rle.c
Line
Count
Source
1
/*
2
 * RLE encoder
3
 * Copyright (c) 2007 Bobby Bingham
4
 *
5
 * This file is part of FFmpeg.
6
 *
7
 * FFmpeg is free software; you can redistribute it and/or
8
 * modify it under the terms of the GNU Lesser General Public
9
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * FFmpeg is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15
 * Lesser General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Lesser General Public
18
 * License along with FFmpeg; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
 */
21
22
#include <string.h>
23
24
#include "libavutil/macros.h"
25
26
#include "rle.h"
27
28
int ff_rle_count_pixels(const uint8_t *start, int len, int bpp, int same)
29
15.5M
{
30
15.5M
    const uint8_t *pos;
31
15.5M
    int count = 1;
32
33
148M
    for (pos = start + bpp; count < FFMIN(127, len); pos += bpp, count++) {
34
134M
        if (same != !memcmp(pos - bpp, pos, bpp)) {
35
1.56M
            if (!same) {
36
                /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a
37
                 * single raw block of pixels. For larger bpp, RLE is as good
38
                 * or better */
39
598k
                if (bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos + 1))
40
234k
                    continue;
41
42
                /* if RLE can encode the next block better than as a raw block,
43
                 * back up and leave _all_ the identical pixels for RLE */
44
364k
                count--;
45
364k
            }
46
1.32M
            break;
47
1.56M
        }
48
134M
    }
49
50
15.5M
    return count;
51
15.5M
}
52
53
int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr, int bpp,
54
                  int w, int add_rep, int xor_rep, int add_raw, int xor_raw)
55
4.09M
{
56
4.09M
    int count, x;
57
4.09M
    uint8_t *out = outbuf;
58
59
9.33M
    for (x = 0; x < w; x += count) {
60
        /* see if we can encode the next set of pixels with RLE */
61
5.24M
        if ((count = ff_rle_count_pixels(ptr, w - x, bpp, 1)) > 1) {
62
4.06M
            if (out + bpp + 1 > outbuf + out_size)
63
1.34k
                return -1;
64
65
4.05M
            *out++ = (count ^ xor_rep) + add_rep;
66
4.05M
            memcpy(out, ptr, bpp);
67
4.05M
            out += bpp;
68
4.05M
        } else {
69
            /* fall back on uncompressed */
70
1.18M
            count = ff_rle_count_pixels(ptr, w - x, bpp, 0);
71
1.18M
            if (out + bpp * count >= outbuf + out_size)
72
3.04k
                return -1;
73
74
1.18M
            *out++ = (count ^ xor_raw) + add_raw;
75
1.18M
            memcpy(out, ptr, bpp * count);
76
1.18M
            out += bpp * count;
77
1.18M
        }
78
79
5.24M
        ptr += count * bpp;
80
5.24M
    }
81
82
4.09M
    return out - outbuf;
83
4.09M
}