| 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 <string.h> | ||
| 23 | |||
| 24 | #include "libavutil/macros.h" | ||
| 25 | |||
| 26 | #include "rle.h" | ||
| 27 | |||
| 28 | 245056 | int ff_rle_count_pixels(const uint8_t *start, int len, int bpp, int same) | |
| 29 | { | ||
| 30 | const uint8_t *pos; | ||
| 31 | 245056 | int count = 1; | |
| 32 | |||
| 33 |
2/2✓ Branch 0 taken 9467002 times.
✓ Branch 1 taken 70766 times.
|
9537768 | for (pos = start + bpp; count < FFMIN(127, len); pos += bpp, count++) { |
| 34 |
2/2✓ Branch 0 taken 281030 times.
✓ Branch 1 taken 9185972 times.
|
9467002 | if (same != !memcmp(pos - bpp, pos, bpp)) { |
| 35 |
2/2✓ Branch 0 taken 138596 times.
✓ Branch 1 taken 142434 times.
|
281030 | 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 |
6/6✓ Branch 0 taken 135252 times.
✓ Branch 1 taken 3344 times.
✓ Branch 2 taken 134453 times.
✓ Branch 3 taken 799 times.
✓ Branch 4 taken 106740 times.
✓ Branch 5 taken 27713 times.
|
138596 | if (bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos + 1)) |
| 40 | 106740 | 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 | 31856 | count--; | |
| 45 | } | ||
| 46 | 174290 | break; | |
| 47 | } | ||
| 48 | } | ||
| 49 | |||
| 50 | 245056 | return count; | |
| 51 | } | ||
| 52 | |||
| 53 | 7738 | 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 | { | ||
| 56 | int count, x; | ||
| 57 | 7738 | uint8_t *out = outbuf; | |
| 58 | |||
| 59 |
2/2✓ Branch 0 taken 56916 times.
✓ Branch 1 taken 7725 times.
|
64641 | for (x = 0; x < w; x += count) { |
| 60 | /* see if we can encode the next set of pixels with RLE */ | ||
| 61 |
2/2✓ Branch 1 taken 6768 times.
✓ Branch 2 taken 50148 times.
|
56916 | if ((count = ff_rle_count_pixels(ptr, w - x, bpp, 1)) > 1) { |
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 6768 times.
|
6768 | if (out + bpp + 1 > outbuf + out_size) |
| 63 | ✗ | return -1; | |
| 64 | |||
| 65 | 6768 | *out++ = (count ^ xor_rep) + add_rep; | |
| 66 | 6768 | memcpy(out, ptr, bpp); | |
| 67 | 6768 | out += bpp; | |
| 68 | } else { | ||
| 69 | /* fall back on uncompressed */ | ||
| 70 | 50148 | count = ff_rle_count_pixels(ptr, w - x, bpp, 0); | |
| 71 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 50135 times.
|
50148 | if (out + bpp * count >= outbuf + out_size) |
| 72 | 13 | return -1; | |
| 73 | |||
| 74 | 50135 | *out++ = (count ^ xor_raw) + add_raw; | |
| 75 | 50135 | memcpy(out, ptr, bpp * count); | |
| 76 | 50135 | out += bpp * count; | |
| 77 | } | ||
| 78 | |||
| 79 | 56903 | ptr += count * bpp; | |
| 80 | } | ||
| 81 | |||
| 82 | 7725 | return out - outbuf; | |
| 83 | } | ||
| 84 |