On date Saturday 2011-07-16 01:33:12 +0200, Stefano Sabatini encoded:
> On date Friday 2011-07-15 16:32:52 +0200, Michael Niedermayer encoded:
> > On Thu, Jul 14, 2011 at 12:18:49AM +0200, Stefano Sabatini wrote:
[...]
> > > > From 2ec24f2f0c79414edfd558f3c55b7d7969018912 Mon Sep 17 00:00:00 2001
> > > > From: Stefano Sabatini <stefano.sabatini-l...@poste.it>
> > > > Date: Wed, 29 Jun 2011 17:30:23 +0200
> > > > Subject: [PATCH] fifo: add av_fifo_peek2()
> > > > 
> > > > The new function provides a more generic interface than av_fifo_peek()
> > > > for peeking at a FIFO buffer data.
> > > > ---
> > > >  libavutil/fifo.h |   21 +++++++++++++++++++++
> > > >  1 files changed, 21 insertions(+), 0 deletions(-)
> > > 
> > > Ping.
> > 
> > I still think the % are a bad idea, they probably make the code 10
> > times slower ...
> > and in what use case would they be needed ?
> 
> Possibly none, I thought it could be useful to return a pointer to the
> buffer rather than NULL, without considering performance issues. I'll
> update the patch accordingly.

Patches updated.

I dropped the "circular buffer" idea due to the use of the module
operator, I'm still keeping support to negative offsets although the
same could be achieved with av_fifo_peek2(fifo, size - offs), but it
may slightly simplify the code use in some situations and with almost
no performance hits.
>From 8c3618c4c65be777a2a3769e3c235b7510489cd0 Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefano.sabatini-l...@poste.it>
Date: Wed, 29 Jun 2011 17:30:23 +0200
Subject: [PATCH] fifo: add av_fifo_peek2(), and deprecate av_fifo_peek()

The new function provides a more generic interface than av_fifo_peek()
for peeking at a FIFO buffer data.
---
 doc/APIchanges      |    3 +++
 libavformat/dvenc.c |    4 ++--
 libavutil/avutil.h  |    3 +++
 libavutil/fifo.h    |   30 +++++++++++++++++++++++++++---
 4 files changed, 35 insertions(+), 5 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index c533818..f7f604b 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -13,6 +13,9 @@ libavutil:   2011-04-18
 
 API changes, most recent first:
 
+2011-07-XX - xxxxxx - lavu 52.X.0
+  Add av_fifo_peek2(), deprecate av_fifo_peek().
+
 2011-07-10 - a67c061 - lavf 53.3.0
   Add avformat_find_stream_info(), deprecate av_find_stream_info().
 
diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c
index 5aab683..d1cca9e 100644
--- a/libavformat/dvenc.c
+++ b/libavformat/dvenc.c
@@ -192,8 +192,8 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
                 if (of*2 >= size)
                     continue;
 
-                frame_ptr[d]   = av_fifo_peek(c->audio_data[channel], of*2+1); // FIXME: maybe we have to admit
-                frame_ptr[d+1] = av_fifo_peek(c->audio_data[channel], of*2);   //        that DV is a big-endian PCM
+                frame_ptr[d]   = *av_fifo_peek2(c->audio_data[channel], of*2+1); // FIXME: maybe we have to admit
+                frame_ptr[d+1] = *av_fifo_peek2(c->audio_data[channel], of*2);   //        that DV is a big-endian PCM
             }
             frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
         }
diff --git a/libavutil/avutil.h b/libavutil/avutil.h
index 01e4e2f..dd75b8d 100644
--- a/libavutil/avutil.h
+++ b/libavutil/avutil.h
@@ -63,6 +63,9 @@
 #ifndef FF_API_FIND_OPT
 #define FF_API_FIND_OPT                 (LIBAVUTIL_VERSION_MAJOR < 52)
 #endif
+#ifndef FF_API_AV_FIFO_PEEK
+#define FF_API_AV_FIFO_PEEK             (LIBAVUTIL_VERSION_MAJOR < 52)
+#endif
 
 /**
  * Return the LIBAVUTIL_VERSION_INT constant.
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index cd361b0..82b34af 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -25,6 +25,7 @@
 #define AVUTIL_FIFO_H
 
 #include <stdint.h>
+#include "avutil.h"
 
 typedef struct AVFifoBuffer {
     uint8_t *buffer;
@@ -106,11 +107,34 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
  */
 void av_fifo_drain(AVFifoBuffer *f, int size);
 
-static inline uint8_t av_fifo_peek(AVFifoBuffer *f, int offs)
+/**
+ * Return a pointer to the data stored in a FIFO buffer at a certain offset.
+ * The FIFO buffer is not modified.
+ *
+ * @param f pointer to the FIFO buffer to peek at, must be non-NULL
+ * @param offs an offset in bytes, its absolute value must be less
+ * than the used buffer size or the returned pointer will point
+ * outside to the buffer data
+ */
+static inline uint8_t *av_fifo_peek2(const AVFifoBuffer *f, int offs)
 {
     uint8_t *ptr = f->rptr + offs;
     if (ptr >= f->end)
-        ptr -= f->end - f->buffer;
-    return *ptr;
+        ptr = f->buffer + (ptr - f->end);
+    else if (ptr < f->buffer)
+        ptr = f->end - (f->buffer - ptr);
+    return ptr;
 }
+
+#if FF_API_AV_FIFO_PEEK
+/**
+ * @deprecated Use av_fifo_peek2() instead.
+ */
+attribute_deprecated
+static inline uint8_t av_fifo_peek(AVFifoBuffer *f, int offs)
+{
+    return *av_fifo_peek2(f, offs);
+}
+#endif
+
 #endif /* AVUTIL_FIFO_H */
-- 
1.7.2.5

>From ac02ac3664f944bffbb5b91ae34e197aecf80da9 Mon Sep 17 00:00:00 2001
From: Stefano Sabatini <stefano.sabatini-l...@poste.it>
Date: Sat, 16 Jul 2011 09:49:42 +0200
Subject: [PATCH] fifo: add FIFO API test program, and fate test

---
 libavutil/Makefile       |    2 +-
 libavutil/fifo.c         |   36 ++++++++++++++++++++++++++++++++++++
 tests/fate/libavutil.mak |    4 ++++
 tests/ref/fate/fifo      |   27 +++++++++++++++++++++++++++
 4 files changed, 68 insertions(+), 1 deletions(-)
 create mode 100644 tests/ref/fate/fifo

diff --git a/libavutil/Makefile b/libavutil/Makefile
index cfe7bae..bded2c6 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -75,7 +75,7 @@ OBJS-$(ARCH_ARM) += arm/cpu.o
 OBJS-$(ARCH_PPC) += ppc/cpu.o
 OBJS-$(ARCH_X86) += x86/cpu.o
 
-TESTPROGS = adler32 aes avstring base64 cpu crc des eval file lfg lls \
+TESTPROGS = adler32 aes avstring base64 cpu crc des eval file fifo lfg lls \
             md5 opt parseutils rational sha tree
 TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo
 
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index f87a99d..5774d33 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -127,3 +127,39 @@ void av_fifo_drain(AVFifoBuffer *f, int size)
         f->rptr -= f->end - f->buffer;
     f->rndx += size;
 }
+
+#ifdef TEST
+
+#undef printf
+
+int main(void)
+{
+    /* create a FIFO buffer */
+    AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
+    int i, j, n;
+
+    /* fill data */
+    for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
+        av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
+
+    /* peek at FIFO */
+    n = av_fifo_size(fifo)/sizeof(int);
+    for (i = -n+1; i < n; i++) {
+        int *v = (int *)av_fifo_peek2(fifo, i*sizeof(int));
+        printf("%d: %d\n", i, *v);
+    }
+    printf("\n");
+
+    /* read data */
+    for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
+        av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
+        printf("%d ", j);
+    }
+    printf("\n");
+
+    av_fifo_free(fifo);
+
+    return 0;
+}
+
+#endif
diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak
index 4299f08..a65b724 100644
--- a/tests/fate/libavutil.mak
+++ b/tests/fate/libavutil.mak
@@ -25,6 +25,10 @@ FATE_TESTS += fate-eval
 fate-eval: libavutil/eval-test$(EXESUF)
 fate-eval: CMD = run libavutil/eval-test
 
+FATE_TESTS += fate-fifo
+fate-fifo: libavutil/fifo-test$(EXESUF)
+fate-fifo: CMD = run libavutil/fifo-test
+
 FATE_TESTS += fate-md5
 fate-md5: libavutil/md5-test$(EXESUF)
 fate-md5: CMD = run libavutil/md5-test
diff --git a/tests/ref/fate/fifo b/tests/ref/fate/fifo
new file mode 100644
index 0000000..2c076c1
--- /dev/null
+++ b/tests/ref/fate/fifo
@@ -0,0 +1,27 @@
+-12: 1
+-11: 2
+-10: 3
+-9: 4
+-8: 5
+-7: 6
+-6: 7
+-5: 8
+-4: 9
+-3: 10
+-2: 11
+-1: 12
+0: 0
+1: 1
+2: 2
+3: 3
+4: 4
+5: 5
+6: 6
+7: 7
+8: 8
+9: 9
+10: 10
+11: 11
+12: 12
+
+0 1 2 3 4 5 6 7 8 9 10 11 12 
-- 
1.7.2.5

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

Reply via email to