GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavcodec/rle.c Lines: 26 27 96.3 %
Date: 2020-10-23 17:01:47 Branches: 19 20 95.0 %

Line Branch Exec 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 "libavutil/common.h"
23
24
#include "avcodec.h"
25
#include "rle.h"
26
27
239474
int ff_rle_count_pixels(const uint8_t *start, int len, int bpp, int same)
28
{
29
    const uint8_t *pos;
30
239474
    int count = 1;
31
32
9354597
    for (pos = start + bpp; count < FFMIN(127, len); pos += bpp, count++) {
33
9285053
        if (same != !memcmp(pos - bpp, pos, bpp)) {
34
268613
            if (!same) {
35
                /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a
36
                 * single raw block of pixels. For larger bpp, RLE is as good
37
                 * or better */
38

129548
                if (bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos + 1))
39
98683
                    continue;
40
41
                /* if RLE can encode the next block better than as a raw block,
42
                 * back up and leave _all_ the identical pixels for RLE */
43
30865
                count--;
44
            }
45
169930
            break;
46
        }
47
    }
48
49
239474
    return count;
50
}
51
52
7488
int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr, int bpp,
53
                  int w, int add_rep, int xor_rep, int add_raw, int xor_raw)
54
{
55
    int count, x;
56
7488
    uint8_t *out = outbuf;
57
58
60989
    for (x = 0; x < w; x += count) {
59
        /* see if we can encode the next set of pixels with RLE */
60
53514
        if ((count = ff_rle_count_pixels(ptr, w - x, bpp, 1)) > 1) {
61
5546
            if (out + bpp + 1 > outbuf + out_size)
62
                return -1;
63
64
5546
            *out++ = (count ^ xor_rep) + add_rep;
65
5546
            memcpy(out, ptr, bpp);
66
5546
            out += bpp;
67
        } else {
68
            /* fall back on uncompressed */
69
47968
            count = ff_rle_count_pixels(ptr, w - x, bpp, 0);
70
47968
            if (out + bpp * count >= outbuf + out_size)
71
13
                return -1;
72
73
47955
            *out++ = (count ^ xor_raw) + add_raw;
74
47955
            memcpy(out, ptr, bpp * count);
75
47955
            out += bpp * count;
76
        }
77
78
53501
        ptr += count * bpp;
79
    }
80
81
7475
    return out - outbuf;
82
}