annotate ffmpeg/libavcodec/rle.c @ 13:844d341cf643 tip

Back up before ISMIR
author Yading Song <yading.song@eecs.qmul.ac.uk>
date Thu, 31 Oct 2013 13:17:06 +0000
parents 6840f77b83aa
children
rev   line source
yading@10 1 /*
yading@10 2 * RLE encoder
yading@10 3 * Copyright (c) 2007 Bobby Bingham
yading@10 4 *
yading@10 5 * This file is part of FFmpeg.
yading@10 6 *
yading@10 7 * FFmpeg is free software; you can redistribute it and/or
yading@10 8 * modify it under the terms of the GNU Lesser General Public
yading@10 9 * License as published by the Free Software Foundation; either
yading@10 10 * version 2.1 of the License, or (at your option) any later version.
yading@10 11 *
yading@10 12 * FFmpeg is distributed in the hope that it will be useful,
yading@10 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
yading@10 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
yading@10 15 * Lesser General Public License for more details.
yading@10 16 *
yading@10 17 * You should have received a copy of the GNU Lesser General Public
yading@10 18 * License along with FFmpeg; if not, write to the Free Software
yading@10 19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
yading@10 20 */
yading@10 21 #include "avcodec.h"
yading@10 22 #include "rle.h"
yading@10 23 #include "libavutil/common.h"
yading@10 24
yading@10 25 /**
yading@10 26 * Count up to 127 consecutive pixels which are either all the same or
yading@10 27 * all differ from the previous and next pixels.
yading@10 28 * @param start Pointer to the first pixel
yading@10 29 * @param len Maximum number of pixels
yading@10 30 * @param bpp Bytes per pixel
yading@10 31 * @param same 1 if searching for identical pixel values. 0 for differing
yading@10 32 * @return Number of matching consecutive pixels found
yading@10 33 */
yading@10 34 static int count_pixels(const uint8_t *start, int len, int bpp, int same)
yading@10 35 {
yading@10 36 const uint8_t *pos;
yading@10 37 int count = 1;
yading@10 38
yading@10 39 for(pos = start + bpp; count < FFMIN(127, len); pos += bpp, count ++) {
yading@10 40 if(same != !memcmp(pos-bpp, pos, bpp)) {
yading@10 41 if(!same) {
yading@10 42 /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single
yading@10 43 * raw block of pixels. for larger bpp, RLE is as good or better */
yading@10 44 if(bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos+1))
yading@10 45 continue;
yading@10 46
yading@10 47 /* if RLE can encode the next block better than as a raw block,
yading@10 48 * back up and leave _all_ the identical pixels for RLE */
yading@10 49 count --;
yading@10 50 }
yading@10 51 break;
yading@10 52 }
yading@10 53 }
yading@10 54
yading@10 55 return count;
yading@10 56 }
yading@10 57
yading@10 58 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w,
yading@10 59 int add_rep, int xor_rep, int add_raw, int xor_raw)
yading@10 60 {
yading@10 61 int count, x;
yading@10 62 uint8_t *out = outbuf;
yading@10 63
yading@10 64 for(x = 0; x < w; x += count) {
yading@10 65 /* see if we can encode the next set of pixels with RLE */
yading@10 66 if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
yading@10 67 if(out + bpp + 1 > outbuf + out_size) return -1;
yading@10 68 *out++ = (count ^ xor_rep) + add_rep;
yading@10 69 memcpy(out, ptr, bpp);
yading@10 70 out += bpp;
yading@10 71 } else {
yading@10 72 /* fall back on uncompressed */
yading@10 73 count = count_pixels(ptr, w-x, bpp, 0);
yading@10 74 if(out + bpp*count >= outbuf + out_size) return -1;
yading@10 75 *out++ = (count ^ xor_raw) + add_raw;
yading@10 76
yading@10 77 memcpy(out, ptr, bpp * count);
yading@10 78 out += bpp * count;
yading@10 79 }
yading@10 80
yading@10 81 ptr += count * bpp;
yading@10 82 }
yading@10 83
yading@10 84 return out - outbuf;
yading@10 85 }