annotate 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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
1 /*
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
2 * RLE encoder
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
3 * Copyright (c) 2007 Bobby Bingham
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
4 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
5 * This file is part of FFmpeg.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
6 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
7 * FFmpeg is free software; you can redistribute it and/or
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
8 * modify it under the terms of the GNU Lesser General Public
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
9 * License as published by the Free Software Foundation; either
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
10 * version 2.1 of the License, or (at your option) any later version.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
11 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
12 * FFmpeg is distributed in the hope that it will be useful,
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
15 * Lesser General Public License for more details.
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
16 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
17 * You should have received a copy of the GNU Lesser General Public
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
18 * License along with FFmpeg; if not, write to the Free Software
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
20 *
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
21 */
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
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
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
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
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
58 /**
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
59 * RLE compress the row, with maximum size of out_size. Value before repeated bytes is (count ^ xor) + add.
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
60 * @param outbuf Output buffer
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
61 * @param out_size Maximum output size
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
62 * @param ptr Input buffer
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
63 * @param bpp Bytes per pixel
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
64 * @param w Image width
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
65 * @return Size of output in bytes, or -1 if larger than out_size
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
66 */
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
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)
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
68 {
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
69 int count, x;
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
70 uint8_t *out;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
71
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
72 out = outbuf;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
73
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
74
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
75 for(x = 0; x < w; x += count) {
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
76 /* see if we can encode the next set of pixels with RLE */
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
77 if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
78 if(out + bpp + 1 > outbuf + out_size) return -1;
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
79 *out++ = (count ^ xor) + add;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
80 memcpy(out, ptr, bpp);
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
81 out += bpp;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
82 } else {
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
83 /* fall back on uncompressed */
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
84 count = count_pixels(ptr, w-x, bpp, 0);
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
85 *out++ = count - 1;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
86
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
87 if(out + bpp*count > outbuf + out_size) return -1;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
88 memcpy(out, ptr, bpp * count);
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
89 out += bpp * count;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
90 }
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
91
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
92 ptr += count * bpp;
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
93 }
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
94
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
95 return out - outbuf;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
96 }