Mercurial > libavcodec.hg
annotate rle.c @ 4771:7cd3ffe4897f libavcodec
Changed the rle encoder a little and made it more universal.
Not only the repeated byte is calculated as (count ^ xor) + add
but also the raw encoding lenth byte is calculated as that too
patch by Xiaohui Sun sunxiaohui dsp ac cn
| author | michael |
|---|---|
| date | Tue, 03 Apr 2007 06:40:21 +0000 |
| parents | a6d1c26e8b6f |
| children | 3cd67a410b68 |
| rev | line source |
|---|---|
| 4673 | 1 /* |
|
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
2 * RLE encoder |
| 4673 | 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" | |
|
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
23 #include "rle.h" |
| 4673 | 24 |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
25 /** |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
26 * Count up to 127 consecutive pixels which are either all the same or |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
27 * all differ from the previous and next pixels. |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
28 * @param start Pointer to the first pixel |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
29 * @param len Maximum number of pixels |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
30 * @param bpp Bytes per pixel |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
31 * @param same 1 if searching for identical pixel values. 0 for differing |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
32 * @return Number of matching consecutive pixels found |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
33 */ |
|
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
34 static int count_pixels(const uint8_t *start, int len, int bpp, int same) |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
35 { |
|
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
36 const uint8_t *pos; |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
37 int count = 1; |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
38 |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
39 for(pos = start + bpp; count < FFMIN(128, len); pos += bpp, count ++) { |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
40 if(same != !memcmp(pos-bpp, pos, bpp)) { |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
41 if(!same) { |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
42 /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
43 * raw block of pixels. for larger bpp, RLE is as good or better */ |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
44 if(bpp == 1 && count + 1 < FFMIN(128, len) && *pos != *(pos+1)) |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
45 continue; |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
46 |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
47 /* if RLE can encode the next block better than as a raw block, |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
48 * back up and leave _all_ the identical pixels for RLE */ |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
49 count --; |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
50 } |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
51 break; |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
52 } |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
53 } |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
54 |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
55 return count; |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
56 } |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
57 |
|
4771
7cd3ffe4897f
Changed the rle encoder a little and made it more universal.
michael
parents:
4770
diff
changeset
|
58 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w, |
|
7cd3ffe4897f
Changed the rle encoder a little and made it more universal.
michael
parents:
4770
diff
changeset
|
59 int8_t add_rep, uint8_t xor_rep,int8_t add_raw,uint8_t xor_raw) |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
60 { |
|
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
61 int count, x; |
| 4770 | 62 uint8_t *out = outbuf; |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
63 |
|
4769
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
64 for(x = 0; x < w; x += count) { |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
65 /* see if we can encode the next set of pixels with RLE */ |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
66 if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) { |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
67 if(out + bpp + 1 > outbuf + out_size) return -1; |
|
4771
7cd3ffe4897f
Changed the rle encoder a little and made it more universal.
michael
parents:
4770
diff
changeset
|
68 *out++ = (count ^ xor_rep) + add_rep; |
|
4769
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
69 memcpy(out, ptr, bpp); |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
70 out += bpp; |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
71 } else { |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
72 /* fall back on uncompressed */ |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
73 count = count_pixels(ptr, w-x, bpp, 0); |
|
4771
7cd3ffe4897f
Changed the rle encoder a little and made it more universal.
michael
parents:
4770
diff
changeset
|
74 *out++ = (count ^ xor_raw) + add_raw; |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
75 |
|
4769
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
76 if(out + bpp*count > outbuf + out_size) return -1; |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
77 memcpy(out, ptr, bpp * count); |
|
79db9e9df40a
fix indention (less work to fix it myself than to check if a indention fix patch is ok ...)
michael
parents:
4768
diff
changeset
|
78 out += bpp * count; |
|
4678
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
79 } |
|
ae5abc4dc946
Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents:
4677
diff
changeset
|
80 |
|
4767
a3667e74f44b
generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents:
4678
diff
changeset
|
81 ptr += count * bpp; |
|
4677
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
82 } |
|
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
83 |
|
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
84 return out - outbuf; |
|
47237f2638b2
Move the encoding of the image data to its own function.
diego
parents:
4676
diff
changeset
|
85 } |
