annotate rle.c @ 12530:63edd10ad4bc libavcodec tip

Try to fix crashes introduced by r25218 r25218 made assumptions about the existence of past reference frames that weren't necessarily true.
author darkshikari
date Tue, 28 Sep 2010 09:06:22 +0000
parents 0e9e36d55e5e
children
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 #include "avcodec.h"
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
22 #include "rle.h"
4673
e7bc1cf41f9f Targa (.tga) encoder,
gpoirier
parents:
diff changeset
23
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
24 /**
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
25 * 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
26 * 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
27 * @param start Pointer to the first pixel
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
28 * @param len Maximum number of pixels
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
29 * @param bpp Bytes per pixel
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
30 * @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
31 * @return Number of matching consecutive pixels found
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
32 */
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
33 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
34 {
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
35 const uint8_t *pos;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
36 int count = 1;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
37
6546
18ab078a42ab change rle encoder to count up to 127, sgi does not support 128
bcoudurier
parents: 5215
diff changeset
38 for(pos = start + bpp; count < FFMIN(127, len); pos += bpp, count ++) {
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
39 if(same != !memcmp(pos-bpp, pos, bpp)) {
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
40 if(!same) {
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
41 /* 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
42 * raw block of pixels. for larger bpp, RLE is as good or better */
6546
18ab078a42ab change rle encoder to count up to 127, sgi does not support 128
bcoudurier
parents: 5215
diff changeset
43 if(bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos+1))
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
44 continue;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
45
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
46 /* 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
47 * 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
48 count --;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
49 }
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
50 break;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
51 }
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 return count;
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
55 }
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
56
4771
7cd3ffe4897f Changed the rle encoder a little and made it more universal.
michael
parents: 4770
diff changeset
57 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w,
4772
3cd67a410b68 dont use *int8_t for the arguments (ive missed that in the patches ...)
michael
parents: 4771
diff changeset
58 int add_rep, int xor_rep, int add_raw, int xor_raw)
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
59 {
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
60 int count, x;
4770
a6d1c26e8b6f cosmetic
michael
parents: 4769
diff changeset
61 uint8_t *out = outbuf;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
62
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
63 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
64 /* 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
65 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
66 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
67 *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
68 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
69 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
70 } 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
71 /* 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
72 count = count_pixels(ptr, w-x, bpp, 0);
7455
0e9e36d55e5e Fix a potential out of array writing.
benoit
parents: 6546
diff changeset
73 if(out + bpp*count >= outbuf + out_size) return -1;
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 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
77 out += bpp * count;
4678
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
78 }
ae5abc4dc946 Add RLE encoding support, patch by Bobby Bingham, uhmmmm gmail com.
diego
parents: 4677
diff changeset
79
4767
a3667e74f44b generic rle encoder by Bartlomiej Wolowiec b.wolowiec students mimuw edu pl
michael
parents: 4678
diff changeset
80 ptr += count * bpp;
4677
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
81 }
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 return out - outbuf;
47237f2638b2 Move the encoding of the image data to its own function.
diego
parents: 4676
diff changeset
84 }