---
 configure             |  4 ++++
 libavcodec/Makefile   |  7 ++++---
 libavcodec/rle.c      | 17 ++++++++++++-----
 libavcodec/rle.h      | 50 +++++++++++++++++++++++++++-----------------------
 libavcodec/sgienc.c   | 15 ++++++++++-----
 libavcodec/targaenc.c | 17 +++++++++++++----
 libavcodec/tiffenc.c  |  7 +++++--
 7 files changed, 75 insertions(+), 42 deletions(-)

diff --git a/configure b/configure
index 1a58a4c..eafeecf 100755
--- a/configure
+++ b/configure
@@ -1718,6 +1718,7 @@ CONFIG_EXTRA="
     rangecoder
     riffdec
     riffenc
+    rledsp
     rtpdec
     rtpenc_chain
     rv34dsp
@@ -2049,17 +2050,20 @@ rv40_decoder_select="error_resilience golomb h264chroma 
h264pred h264qpel mpeg_e
 screenpresso_decoder_deps="zlib"
 shorten_decoder_select="golomb"
 sipr_decoder_select="lsp"
+sgi_encoder_select="rledsp"
 sp5x_decoder_select="mjpeg_decoder"
 svq1_decoder_select="hpeldsp"
 svq1_encoder_select="aandcttables hpeldsp me_cmp mpegvideoenc"
 svq3_decoder_select="golomb h264dsp h264pred hpeldsp tpeldsp videodsp"
 svq3_decoder_suggest="zlib"
 tak_decoder_select="audiodsp"
+targa_encoder_select="rledsp"
 tdsc_decoder_deps="zlib"
 tdsc_decoder_select="mjpeg_decoder"
 theora_decoder_select="vp3_decoder"
 thp_decoder_select="mjpeg_decoder"
 tiff_decoder_suggest="zlib"
+tiff_encoder_select="rledsp"
 tiff_encoder_suggest="zlib"
 truehd_decoder_select="mlp_decoder"
 truemotion2_decoder_select="bswapdsp"
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index 622e100..44d406c 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -98,6 +98,7 @@ OBJS-$(CONFIG_QSV)                     += qsv.o
 OBJS-$(CONFIG_QSVDEC)                  += qsvdec.o
 OBJS-$(CONFIG_QSVENC)                  += qsvenc.o
 OBJS-$(CONFIG_RANGECODER)              += rangecoder.o
+OBJS-$(CONFIG_RLEDSP)                  += rle.o
 RDFT-OBJS-$(CONFIG_HARDCODED_TABLES)   += sin_tables.o
 OBJS-$(CONFIG_RDFT)                    += rdft.o $(RDFT-OBJS-yes)
 OBJS-$(CONFIG_RV34DSP)                 += rv34dsp.o
@@ -409,7 +410,7 @@ OBJS-$(CONFIG_S302M_DECODER)           += s302m.o
 OBJS-$(CONFIG_SANM_DECODER)            += sanm.o
 OBJS-$(CONFIG_SCREENPRESSO_DECODER)    += screenpresso.o
 OBJS-$(CONFIG_SGI_DECODER)             += sgidec.o
-OBJS-$(CONFIG_SGI_ENCODER)             += sgienc.o rle.o
+OBJS-$(CONFIG_SGI_ENCODER)             += sgienc.o
 OBJS-$(CONFIG_SGIRLE_DECODER)          += sgirledec.o
 OBJS-$(CONFIG_SHORTEN_DECODER)         += shorten.o
 OBJS-$(CONFIG_SIPR_DECODER)            += sipr.o acelp_pitch_delay.o \
@@ -430,11 +431,11 @@ OBJS-$(CONFIG_SVQ1_ENCODER)            += svq1enc.o 
svq1.o  h263data.o  \
 OBJS-$(CONFIG_SVQ3_DECODER)            += svq3.o svq13.o mpegutils.o 
h264_parse.o h264data.o
 OBJS-$(CONFIG_TAK_DECODER)             += takdec.o tak.o
 OBJS-$(CONFIG_TARGA_DECODER)           += targa.o
-OBJS-$(CONFIG_TARGA_ENCODER)           += targaenc.o rle.o
+OBJS-$(CONFIG_TARGA_ENCODER)           += targaenc.o
 OBJS-$(CONFIG_TDSC_DECODER)            += tdsc.o
 OBJS-$(CONFIG_TIERTEXSEQVIDEO_DECODER) += tiertexseqv.o
 OBJS-$(CONFIG_TIFF_DECODER)            += tiff.o lzw.o faxcompr.o
-OBJS-$(CONFIG_TIFF_ENCODER)            += tiffenc.o rle.o lzwenc.o
+OBJS-$(CONFIG_TIFF_ENCODER)            += tiffenc.o lzwenc.o
 OBJS-$(CONFIG_TMV_DECODER)             += tmv.o cga_data.o
 OBJS-$(CONFIG_TRUEMOTION1_DECODER)     += truemotion1.o
 OBJS-$(CONFIG_TRUEMOTION2_DECODER)     += truemotion2.o
diff --git a/libavcodec/rle.c b/libavcodec/rle.c
index 6c8bf27..37d48f1 100644
--- a/libavcodec/rle.c
+++ b/libavcodec/rle.c
@@ -24,7 +24,7 @@
 #include "avcodec.h"
 #include "rle.h"
 
-int ff_rle_count_pixels(const uint8_t *start, int len, int bpp, int same)
+static int count_pixels(const uint8_t *start, int len, int bpp, int same)
 {
     const uint8_t *pos;
     int count = 1;
@@ -49,15 +49,16 @@ int ff_rle_count_pixels(const uint8_t *start, int len, int 
bpp, int same)
     return count;
 }
 
-int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr, int bpp,
-                  int w, int add_rep, int xor_rep, int add_raw, int xor_raw)
+static int rle_encode(uint8_t *outbuf, int out_size, const uint8_t *ptr,
+                      int bpp, int w, int add_rep, int xor_rep, int add_raw,
+                      int xor_raw)
 {
     int count, x;
     uint8_t *out = outbuf;
 
     for (x = 0; x < w; x += count) {
         /* see if we can encode the next set of pixels with RLE */
-        if ((count = ff_rle_count_pixels(ptr, w - x, bpp, 1)) > 1) {
+        if ((count = count_pixels(ptr, w - x, bpp, 1)) > 1) {
             if (out + bpp + 1 > outbuf + out_size)
                 return -1;
 
@@ -66,7 +67,7 @@ int ff_rle_encode(uint8_t *outbuf, int out_size, const 
uint8_t *ptr, int bpp,
             out += bpp;
         } else {
             /* fall back on uncompressed */
-            count = ff_rle_count_pixels(ptr, w - x, bpp, 0);
+            count = count_pixels(ptr, w - x, bpp, 0);
             if (out + bpp * count >= outbuf + out_size)
                 return -1;
 
@@ -80,3 +81,9 @@ int ff_rle_encode(uint8_t *outbuf, int out_size, const 
uint8_t *ptr, int bpp,
 
     return out - outbuf;
 }
+
+av_cold void ff_rledsp_init(RLEDSPContext *c)
+{
+    c->count  = count_pixels;
+    c->encode = rle_encode;
+}
diff --git a/libavcodec/rle.h b/libavcodec/rle.h
index f1b0c78..94f001f 100644
--- a/libavcodec/rle.h
+++ b/libavcodec/rle.h
@@ -23,29 +23,33 @@
 
 #include <stdint.h>
 
-/**
- * Count up to 127 consecutive pixels which are either all the same or
- * all differ from the previous and next pixels.
- * @param start Pointer to the first pixel
- * @param len Maximum number of pixels
- * @param bpp Bytes per pixel
- * @param same 1 if searching for identical pixel values, 0 for differing
- * @return Number of matching consecutive pixels found
- */
-int ff_rle_count_pixels(const uint8_t *start, int len, int bpp, int same);
+typedef struct RLEDSPContext {
+    /**
+     * Count up to 127 consecutive pixels which are either all the same or
+     * all differ from the previous and next pixels.
+     * @param start Pointer to the first pixel
+     * @param len Maximum number of pixels
+     * @param bpp Bytes per pixel
+     * @param same 1 if searching for identical pixel values, 0 for differing
+     * @return Number of matching consecutive pixels found
+     */
+    int (*count)  (const uint8_t *start, int len, int bpp, int same);
 
-/**
- * RLE compress the row, with maximum size of out_size.
- * Value before repeated bytes is (count ^ xor_rep) + add_rep.
- * Value before raw bytes is (count ^ xor_raw) + add_raw.
- * @param outbuf Output buffer
- * @param out_size Maximum output size
- * @param inbuf Input buffer
- * @param bpp Bytes per pixel
- * @param w Image width
- * @return Size of output in bytes, or -1 if larger than out_size
- */
-int ff_rle_encode(uint8_t *outbuf, int out_size, const uint8_t *inbuf, int bpp,
-                  int w, int add_rep, int xor_rep, int add_raw, int xor_raw);
+    /**
+     * RLE compress the row, with maximum size of out_size.
+     * Value before repeated bytes is (count ^ xor_rep) + add_rep.
+     * Value before raw bytes is (count ^ xor_raw) + add_raw.
+     * @param outbuf Output buffer
+     * @param out_size Maximum output size
+     * @param inbuf Input buffer
+     * @param bpp Bytes per pixel
+     * @param w Image width
+     * @return Size of output in bytes, or -1 if larger than out_size
+     */
+    int (*encode) (uint8_t *outbuf, int out_size, const uint8_t *inbuf, int 
bpp,
+                   int w, int add_rep, int xor_rep, int add_raw, int xor_raw);
+} RLEDSPContext;
+
+void ff_rledsp_init(RLEDSPContext *c);
 
 #endif /* AVCODEC_RLE_H */
diff --git a/libavcodec/sgienc.c b/libavcodec/sgienc.c
index 07e224c..50c4d73 100644
--- a/libavcodec/sgienc.c
+++ b/libavcodec/sgienc.c
@@ -32,23 +32,28 @@
 
 typedef struct SgiContext {
     AVClass *class;
+    RLEDSPContext rledsp;
 
     int rle;
 } SgiContext;
 
 static av_cold int encode_init(AVCodecContext *avctx)
 {
+    SgiContext *s = avctx->priv_data;
+
     if (avctx->width > 65535 || avctx->height > 65535) {
         av_log(avctx, AV_LOG_ERROR,
                "Unsupported resolution %dx%d.\n", avctx->width, avctx->height);
         return AVERROR_INVALIDDATA;
     }
 
+    ff_rledsp_init(&s->rledsp);
+
     return 0;
 }
 
-static int sgi_rle_encode(PutByteContext *pbc, const uint8_t *src,
-                          int w, int bpp)
+static int sgi_rle_encode(SgiContext *s, PutByteContext *pbc,
+                          const uint8_t *src, int w, int bpp)
 {
     int val, count, x, start = bytestream2_tell_p(pbc);
     void (*bytestream2_put)(PutByteContext *, unsigned int);
@@ -60,7 +65,7 @@ static int sgi_rle_encode(PutByteContext *pbc, const uint8_t 
*src,
 
     for (x = 0; x < w; x += count) {
         /* see if we can encode the next set of pixels with RLE */
-        count = ff_rle_count_pixels(src, w - x, bpp, 1);
+        count = s->rledsp.count(src, w - x, bpp, 1);
         if (count > 1) {
             if (bytestream2_get_bytes_left_p(pbc) < bpp * 2)
                 return AVERROR_INVALIDDATA;
@@ -71,7 +76,7 @@ static int sgi_rle_encode(PutByteContext *pbc, const uint8_t 
*src,
         } else {
             int i;
             /* fall back on uncompressed */
-            count = ff_rle_count_pixels(src, w - x, bpp, 0);
+            count = s->rledsp.count(src, w - x, bpp, 0);
             if (bytestream2_get_bytes_left_p(pbc) < bpp * (count + 1))
                 return AVERROR_INVALIDDATA;
 
@@ -222,7 +227,7 @@ FF_ENABLE_DEPRECATION_WARNINGS
                 for (x = 0; x < width * bytes_per_channel; x += 
bytes_per_channel)
                     encode_buf[x] = in_buf[depth * x];
 
-                length = sgi_rle_encode(&pbc, encode_buf, width,
+                length = sgi_rle_encode(s, &pbc, encode_buf, width,
                                         bytes_per_channel);
                 if (length < 1) {
                     av_free(encode_buf);
diff --git a/libavcodec/targaenc.c b/libavcodec/targaenc.c
index f0cee38..dcc6a51 100644
--- a/libavcodec/targaenc.c
+++ b/libavcodec/targaenc.c
@@ -33,12 +33,14 @@
 
 typedef struct TargaContext {
     AVClass *class;
+    RLEDSPContext rledsp;
 
     int rle;
 } TargaContext;
 
 /**
  * RLE compress the image, with maximum size of out_size
+ * @param s TargaContex
  * @param outbuf Output buffer
  * @param out_size Maximum output size
  * @param pic Image to compress
@@ -47,8 +49,8 @@ typedef struct TargaContext {
  * @param h Image height
  * @return Size of output in bytes, or -1 if larger than out_size
  */
-static int targa_encode_rle(uint8_t *outbuf, int out_size, const AVFrame *pic,
-                            int bpp, int w, int h)
+static int targa_encode_rle(TargaContext *s, uint8_t *outbuf, int out_size,
+                            const AVFrame *pic, int bpp, int w, int h)
 {
     int y,ret;
     uint8_t *out;
@@ -56,7 +58,9 @@ static int targa_encode_rle(uint8_t *outbuf, int out_size, 
const AVFrame *pic,
     out = outbuf;
 
     for(y = 0; y < h; y ++) {
-        ret = ff_rle_encode(out, out_size, pic->data[0] + pic->linesize[0] * 
y, bpp, w, 0x7f, 0, -1, 0);
+        ret = s->rledsp.encode(out, out_size,
+                               pic->data[0] + pic->linesize[0] * y,
+                               bpp, w, 0x7f, 0, -1, 0);
         if(ret == -1){
             return -1;
         }
@@ -138,7 +142,8 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
     /* try RLE compression */
     if (s->rle)
-        datasize = targa_encode_rle(out, picsize, p, bpp, avctx->width, 
avctx->height);
+        datasize = targa_encode_rle(s, out, picsize, p, bpp,
+                                    avctx->width, avctx->height);
 
     /* if that worked well, mark the picture as RLE compressed */
     if(datasize >= 0)
@@ -163,11 +168,15 @@ FF_ENABLE_DEPRECATION_WARNINGS
 
 static av_cold int targa_encode_init(AVCodecContext *avctx)
 {
+    TargaContext *s = avctx->priv_data;
+
     if (avctx->width > 0xffff || avctx->height > 0xffff) {
         av_log(avctx, AV_LOG_ERROR, "image dimensions too large\n");
         return AVERROR(EINVAL);
     }
 
+    ff_rledsp_init(&s->rledsp);
+
 #if FF_API_CODED_FRAME
 FF_DISABLE_DEPRECATION_WARNINGS
     avctx->coded_frame->key_frame = 1;
diff --git a/libavcodec/tiffenc.c b/libavcodec/tiffenc.c
index cf6e262..a8bff3c 100644
--- a/libavcodec/tiffenc.c
+++ b/libavcodec/tiffenc.c
@@ -50,6 +50,7 @@ static const uint8_t type_sizes2[6] = {
 typedef struct TiffEncoderContext {
     AVClass *class;                         ///< for private options
     AVCodecContext *avctx;
+    RLEDSPContext rledsp;
 
     int width;                              ///< picture width
     int height;                             ///< picture height
@@ -178,8 +179,8 @@ static int encode_strip(TiffEncoderContext *s, const int8_t 
*src,
         memcpy(dst, src, n);
         return n;
     case TIFF_PACKBITS:
-        return ff_rle_encode(dst, s->buf_size - (*s->buf - s->buf_start),
-                             src, 1, n, 2, 0xff, -1, 0);
+        return s->rledsp.encode(dst, s->buf_size - (*s->buf - s->buf_start),
+                                src, 1, n, 2, 0xff, -1, 0);
     case TIFF_LZW:
         return ff_lzw_encode(s->lzws, src, n);
     default:
@@ -500,6 +501,8 @@ static av_cold int encode_init(AVCodecContext *avctx)
         return AVERROR(ENOSYS);
     }
 
+    ff_rledsp_init(&s->rledsp);
+
 #if FF_API_CODED_FRAME
 FF_DISABLE_DEPRECATION_WARNINGS
     avctx->coded_frame->pict_type = AV_PICTURE_TYPE_I;
-- 
2.9.0

_______________________________________________
libav-devel mailing list
libav-devel@libav.org
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to