annotate rle.c @ 4957:3fcb2f0d9ef1 libavcodec

move ff_mpeg4_find_frame_end() from h263dec.c to mpeg4video_parser.c now h263dec depends on mpeg4video_parser this fixes compilation when h263 decoder is disabled
author aurel
date Wed, 09 May 2007 23:13:43 +0000
parents 3cd67a410b68
children 2b72f9bc4f06
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
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,
4772
3cd67a410b68 dont use *int8_t for the arguments (ive missed that in the patches ...)
michael
parents: 4771
diff changeset
59 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
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
a6d1c26e8b6f cosmetic
michael
parents: 4769
diff changeset
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 }