~ubuntu-branches/ubuntu/jaunty/xvidcap/jaunty-proposed

« back to all changes in this revision

Viewing changes to ffmpeg/libavcodec/rle.c

  • Committer: Bazaar Package Importer
  • Author(s): Lionel Le Folgoc, Andrew Starr-Bochicchio, Lionel Le Folgoc
  • Date: 2008-12-26 00:10:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20081226001006-2040ls9680bd1blt
Tags: 1.1.7-0.2ubuntu1
[ Andrew Starr-Bochicchio ]
* Merge from debian-multimedia (LP: #298547), Ubuntu Changes:
 - For ffmpeg-related build-deps, fix versionized dependencies
   as the ubuntu versioning is different than debian-multimedia's.

[ Lionel Le Folgoc ]
* LP: #311412 is fixed since the 1.1.7~rc1-0.1 revision.
* debian/patches/03_ffmpeg.diff: updated to fix FTBFS due to libswscale API
  change (cherry-pick from Gentoo #234383).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#include "avcodec.h"
 
22
#include "rle.h"
 
23
 
 
24
/**
 
25
 * Count up to 127 consecutive pixels which are either all the same or
 
26
 * all differ from the previous and next pixels.
 
27
 * @param start Pointer to the first pixel
 
28
 * @param len Maximum number of pixels
 
29
 * @param bpp Bytes per pixel
 
30
 * @param same 1 if searching for identical pixel values.  0 for differing
 
31
 * @return Number of matching consecutive pixels found
 
32
 */
 
33
static int count_pixels(const uint8_t *start, int len, int bpp, int same)
 
34
{
 
35
    const uint8_t *pos;
 
36
    int count = 1;
 
37
 
 
38
    for(pos = start + bpp; count < FFMIN(127, len); pos += bpp, count ++) {
 
39
        if(same != !memcmp(pos-bpp, pos, bpp)) {
 
40
            if(!same) {
 
41
                /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single
 
42
                 * raw block of pixels.  for larger bpp, RLE is as good or better */
 
43
                if(bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos+1))
 
44
                    continue;
 
45
 
 
46
                /* if RLE can encode the next block better than as a raw block,
 
47
                 * back up and leave _all_ the identical pixels for RLE */
 
48
                count --;
 
49
            }
 
50
            break;
 
51
        }
 
52
    }
 
53
 
 
54
    return count;
 
55
}
 
56
 
 
57
int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w,
 
58
                  int add_rep, int xor_rep, int add_raw, int xor_raw)
 
59
{
 
60
    int count, x;
 
61
    uint8_t *out = outbuf;
 
62
 
 
63
    for(x = 0; x < w; x += count) {
 
64
        /* see if we can encode the next set of pixels with RLE */
 
65
        if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
 
66
            if(out + bpp + 1 > outbuf + out_size) return -1;
 
67
            *out++ = (count ^ xor_rep) + add_rep;
 
68
            memcpy(out, ptr, bpp);
 
69
            out += bpp;
 
70
        } else {
 
71
            /* fall back on uncompressed */
 
72
            count = count_pixels(ptr, w-x, bpp, 0);
 
73
            *out++ = (count ^ xor_raw) + add_raw;
 
74
 
 
75
            if(out + bpp*count > outbuf + out_size) return -1;
 
76
            memcpy(out, ptr, bpp * count);
 
77
            out += bpp * count;
 
78
        }
 
79
 
 
80
        ptr += count * bpp;
 
81
    }
 
82
 
 
83
    return out - outbuf;
 
84
}