Mercurial > libavcodec.hg
comparison rle.c @ 4767:a3667e74f44b libavcodec
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
author | michael |
---|---|
date | Tue, 03 Apr 2007 01:01:46 +0000 |
parents | targaenc.c@ae5abc4dc946 |
children | 542e4c4fc15a |
comparison
equal
deleted
inserted
replaced
4766:f43b50149387 | 4767:a3667e74f44b |
---|---|
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 "avcodec.h" | |
23 #include "rle.h" | |
24 | |
25 /** | |
26 * Count up to 127 consecutive pixels which are either all the same or | |
27 * all differ from the previous and next pixels. | |
28 * @param start Pointer to the first pixel | |
29 * @param len Maximum number of pixels | |
30 * @param bpp Bytes per pixel | |
31 * @param same 1 if searching for identical pixel values. 0 for differing | |
32 * @return Number of matching consecutive pixels found | |
33 */ | |
34 static int count_pixels(const uint8_t *start, int len, int bpp, int same) | |
35 { | |
36 const uint8_t *pos; | |
37 int count = 1; | |
38 | |
39 for(pos = start + bpp; count < FFMIN(128, len); pos += bpp, count ++) { | |
40 if(same != !memcmp(pos-bpp, pos, bpp)) { | |
41 if(!same) { | |
42 /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single | |
43 * raw block of pixels. for larger bpp, RLE is as good or better */ | |
44 if(bpp == 1 && count + 1 < FFMIN(128, len) && *pos != *(pos+1)) | |
45 continue; | |
46 | |
47 /* if RLE can encode the next block better than as a raw block, | |
48 * back up and leave _all_ the identical pixels for RLE */ | |
49 count --; | |
50 } | |
51 break; | |
52 } | |
53 } | |
54 | |
55 return count; | |
56 } | |
57 | |
58 /** | |
59 * RLE compress the row, with maximum size of out_size. Value before repeated bytes is (count ^ xor) + add. | |
60 * @param outbuf Output buffer | |
61 * @param out_size Maximum output size | |
62 * @param ptr Input buffer | |
63 * @param bpp Bytes per pixel | |
64 * @param w Image width | |
65 * @return Size of output in bytes, or -1 if larger than out_size | |
66 */ | |
67 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w, int8_t add, uint8_t xor) | |
68 { | |
69 int count, x; | |
70 uint8_t *out; | |
71 | |
72 out = outbuf; | |
73 | |
74 | |
75 for(x = 0; x < w; x += count) { | |
76 /* see if we can encode the next set of pixels with RLE */ | |
77 if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) { | |
78 if(out + bpp + 1 > outbuf + out_size) return -1; | |
79 *out++ = (count ^ xor) + add; | |
80 memcpy(out, ptr, bpp); | |
81 out += bpp; | |
82 } else { | |
83 /* fall back on uncompressed */ | |
84 count = count_pixels(ptr, w-x, bpp, 0); | |
85 *out++ = count - 1; | |
86 | |
87 if(out + bpp*count > outbuf + out_size) return -1; | |
88 memcpy(out, ptr, bpp * count); | |
89 out += bpp * count; | |
90 } | |
91 | |
92 ptr += count * bpp; | |
93 } | |
94 | |
95 return out - outbuf; | |
96 } |