• Main Page
  • Related Pages
  • Modules
  • Data Structures
  • Files
  • File List
  • Globals

libavcodec/rle.c

Go to the documentation of this file.
00001 /*
00002  * RLE encoder
00003  * Copyright (c) 2007 Bobby Bingham
00004  *
00005  * This file is part of FFmpeg.
00006  *
00007  * FFmpeg is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU Lesser General Public
00009  * License as published by the Free Software Foundation; either
00010  * version 2.1 of the License, or (at your option) any later version.
00011  *
00012  * FFmpeg is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00015  * Lesser General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU Lesser General Public
00018  * License along with FFmpeg; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00020  */
00021 #include "avcodec.h"
00022 #include "rle.h"
00023 
00033 static int count_pixels(const uint8_t *start, int len, int bpp, int same)
00034 {
00035     const uint8_t *pos;
00036     int count = 1;
00037 
00038     for(pos = start + bpp; count < FFMIN(127, len); pos += bpp, count ++) {
00039         if(same != !memcmp(pos-bpp, pos, bpp)) {
00040             if(!same) {
00041                 /* if bpp == 1, then 0 1 1 0 is more efficiently encoded as a single
00042                  * raw block of pixels.  for larger bpp, RLE is as good or better */
00043                 if(bpp == 1 && count + 1 < FFMIN(127, len) && *pos != *(pos+1))
00044                     continue;
00045 
00046                 /* if RLE can encode the next block better than as a raw block,
00047                  * back up and leave _all_ the identical pixels for RLE */
00048                 count --;
00049             }
00050             break;
00051         }
00052     }
00053 
00054     return count;
00055 }
00056 
00057 int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr , int bpp, int w,
00058                   int add_rep, int xor_rep, int add_raw, int xor_raw)
00059 {
00060     int count, x;
00061     uint8_t *out = outbuf;
00062 
00063     for(x = 0; x < w; x += count) {
00064         /* see if we can encode the next set of pixels with RLE */
00065         if((count = count_pixels(ptr, w-x, bpp, 1)) > 1) {
00066             if(out + bpp + 1 > outbuf + out_size) return -1;
00067             *out++ = (count ^ xor_rep) + add_rep;
00068             memcpy(out, ptr, bpp);
00069             out += bpp;
00070         } else {
00071             /* fall back on uncompressed */
00072             count = count_pixels(ptr, w-x, bpp, 0);
00073             if(out + bpp*count >= outbuf + out_size) return -1;
00074             *out++ = (count ^ xor_raw) + add_raw;
00075 
00076             memcpy(out, ptr, bpp * count);
00077             out += bpp * count;
00078         }
00079 
00080         ptr += count * bpp;
00081     }
00082 
00083     return out - outbuf;
00084 }

Generated on Fri Sep 16 2011 17:17:42 for FFmpeg by  doxygen 1.7.1