* [FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 14:22 ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size Anton Khirnov
` (34 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
There should be no good reason for the callers to access any of its
contents.
Define a new type for the internal struct that currently matches
AVFifoBuffer. This will allow adding new private fields without waiting
for the major bump and will be useful in the following commits.
Unfortunately AVFifoBuffer fields cannot be marked as deprecated because
it would trigger a warning wherever fifo.h is #included, due to
inlined av_fifo_peek2().
---
doc/APIchanges | 4 ++++
libavutil/fifo.c | 23 +++++++++++++++++++----
libavutil/fifo.h | 14 ++++++++++++--
libavutil/tests/fifo.c | 2 +-
libavutil/version.h | 1 +
5 files changed, 37 insertions(+), 7 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 8df0364e4c..21fa02ae9d 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,10 @@ libavutil: 2021-04-27
API changes, most recent first:
+2022-01-xx - xxxxxxxxxx - lavu fifo.h
+ Access to all AVFifoBuffer members is deprecated. The struct will
+ become an incomplete type in a future major libavutil version.
+
2022-01-04 - 78dc21b123e - lavu 57.16.100 - frame.h
Add AV_FRAME_DATA_DOVI_METADATA.
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index f2f046b1f3..aaade01333 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -28,9 +28,24 @@
#define FIFO_SIZE_MAX FFMIN3((uint64_t)INT_MAX, (uint64_t)UINT32_MAX, (uint64_t)SIZE_MAX)
+#if FF_API_FIFO_PUBLIC
+# define CTX_STRUCT_NAME FifoBuffer
+#else
+# define CTX_STRUCT_NAME AVFifoBuffer
+#endif
+
+typedef struct CTX_STRUCT_NAME {
+ // These fields must match then contents of AVFifoBuffer in fifo.h
+ // until FF_API_FIFO_PUBLIC is removed
+ uint8_t *buffer;
+ uint8_t *rptr, *wptr, *end;
+ uint32_t rndx, wndx;
+ /////////////////////////////////////////
+} FifoBuffer;
+
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
{
- AVFifoBuffer *f;
+ FifoBuffer *f;
void *buffer;
if (nmemb > FIFO_SIZE_MAX / size)
@@ -39,15 +54,15 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
buffer = av_realloc_array(NULL, nmemb, size);
if (!buffer)
return NULL;
- f = av_mallocz(sizeof(AVFifoBuffer));
+ f = av_mallocz(sizeof(*f));
if (!f) {
av_free(buffer);
return NULL;
}
f->buffer = buffer;
f->end = f->buffer + nmemb * size;
- av_fifo_reset(f);
- return f;
+ av_fifo_reset((AVFifoBuffer*)f);
+ return (AVFifoBuffer*)f;
}
AVFifoBuffer *av_fifo_alloc(unsigned int size)
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index f4fd291e59..ca4e7fe060 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -28,11 +28,21 @@
#include "avutil.h"
#include "attributes.h"
-typedef struct AVFifoBuffer {
+#if FF_API_FIFO_PUBLIC
+/**
+ * The contents of the struct are private and should not be accessed by the
+ * callers in any way.
+ */
+#endif
+typedef struct AVFifoBuffer
+#if FF_API_FIFO_PUBLIC
+{
uint8_t *buffer;
uint8_t *rptr, *wptr, *end;
uint32_t rndx, wndx;
-} AVFifoBuffer;
+}
+#endif
+AVFifoBuffer;
/**
* Initialize an AVFifoBuffer.
diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c
index a17d913233..e5aa88d252 100644
--- a/libavutil/tests/fifo.c
+++ b/libavutil/tests/fifo.c
@@ -18,7 +18,7 @@
#include <stdio.h>
#include <stdlib.h>
-#include "libavutil/fifo.h"
+#include "libavutil/fifo.c"
int main(void)
{
diff --git a/libavutil/version.h b/libavutil/version.h
index 953aac9d94..7c031f547e 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -110,6 +110,7 @@
#define FF_API_COLORSPACE_NAME (LIBAVUTIL_VERSION_MAJOR < 58)
#define FF_API_AV_MALLOCZ_ARRAY (LIBAVUTIL_VERSION_MAJOR < 58)
#define FF_API_FIFO_PEEK2 (LIBAVUTIL_VERSION_MAJOR < 58)
+#define FF_API_FIFO_PUBLIC (LIBAVUTIL_VERSION_MAJOR < 58)
/**
* @}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump Anton Khirnov
@ 2022-01-13 14:22 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 14:22 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> There should be no good reason for the callers to access any of its
> contents.
>
> Define a new type for the internal struct that currently matches
> AVFifoBuffer. This will allow adding new private fields without waiting
> for the major bump and will be useful in the following commits.
>
> Unfortunately AVFifoBuffer fields cannot be marked as deprecated because
> it would trigger a warning wherever fifo.h is #included, due to
> inlined av_fifo_peek2().
> ---
> doc/APIchanges | 4 ++++
> libavutil/fifo.c | 23 +++++++++++++++++++----
> libavutil/fifo.h | 14 ++++++++++++--
> libavutil/tests/fifo.c | 2 +-
> libavutil/version.h | 1 +
> 5 files changed, 37 insertions(+), 7 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 8df0364e4c..21fa02ae9d 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,10 @@ libavutil: 2021-04-27
>
> API changes, most recent first:
>
> +2022-01-xx - xxxxxxxxxx - lavu fifo.h
> + Access to all AVFifoBuffer members is deprecated. The struct will
> + become an incomplete type in a future major libavutil version.
> +
> 2022-01-04 - 78dc21b123e - lavu 57.16.100 - frame.h
> Add AV_FRAME_DATA_DOVI_METADATA.
>
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index f2f046b1f3..aaade01333 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -28,9 +28,24 @@
>
> #define FIFO_SIZE_MAX FFMIN3((uint64_t)INT_MAX, (uint64_t)UINT32_MAX, (uint64_t)SIZE_MAX)
>
> +#if FF_API_FIFO_PUBLIC
> +# define CTX_STRUCT_NAME FifoBuffer
> +#else
> +# define CTX_STRUCT_NAME AVFifoBuffer
This is invalid in pre-C11 (and will lead to compilation failures on old
GCC versions): Pre-C11, typedefs were subject to the ODR, yet you
already typedef AVFifoBuffer in fifo.h.
> +#endif
> +
> +typedef struct CTX_STRUCT_NAME {
> + // These fields must match then contents of AVFifoBuffer in fifo.h
the contents
> + // until FF_API_FIFO_PUBLIC is removed
The actual spec-compliant way for this is to add an AVFifoBuffer at the
start of this struct; it will also avoid the casts from FifoBuffer to
AVFifoBuffer. This will make the accesses to it a bit more cumbersome
and will mean more changes when FF_API_FIFO_PUBLIC is removed. (This
would not be an issue if we required support for anonymous structs
(mandatory in C11, supported by GCC and others long before that).)
> + uint8_t *buffer;
> + uint8_t *rptr, *wptr, *end;
> + uint32_t rndx, wndx;
> + /////////////////////////////////////////
> +} FifoBuffer;
> +
> AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> {
> - AVFifoBuffer *f;
> + FifoBuffer *f;
> void *buffer;
>
> if (nmemb > FIFO_SIZE_MAX / size)
> @@ -39,15 +54,15 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> buffer = av_realloc_array(NULL, nmemb, size);
> if (!buffer)
> return NULL;
> - f = av_mallocz(sizeof(AVFifoBuffer));
> + f = av_mallocz(sizeof(*f));
> if (!f) {
> av_free(buffer);
> return NULL;
> }
> f->buffer = buffer;
> f->end = f->buffer + nmemb * size;
> - av_fifo_reset(f);
> - return f;
> + av_fifo_reset((AVFifoBuffer*)f);
> + return (AVFifoBuffer*)f;
> }
>
> AVFifoBuffer *av_fifo_alloc(unsigned int size)
> diff --git a/libavutil/fifo.h b/libavutil/fifo.h
> index f4fd291e59..ca4e7fe060 100644
> --- a/libavutil/fifo.h
> +++ b/libavutil/fifo.h
> @@ -28,11 +28,21 @@
> #include "avutil.h"
> #include "attributes.h"
>
> -typedef struct AVFifoBuffer {
> +#if FF_API_FIFO_PUBLIC
> +/**
> + * The contents of the struct are private and should not be accessed by the
> + * callers in any way.
> + */
> +#endif
> +typedef struct AVFifoBuffer
> +#if FF_API_FIFO_PUBLIC
> +{
> uint8_t *buffer;
> uint8_t *rptr, *wptr, *end;
> uint32_t rndx, wndx;
> -} AVFifoBuffer;
> +}
> +#endif
> +AVFifoBuffer;
>
> /**
> * Initialize an AVFifoBuffer.
> diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c
> index a17d913233..e5aa88d252 100644
> --- a/libavutil/tests/fifo.c
> +++ b/libavutil/tests/fifo.c
> @@ -18,7 +18,7 @@
>
> #include <stdio.h>
> #include <stdlib.h>
> -#include "libavutil/fifo.h"
> +#include "libavutil/fifo.c"
>
> int main(void)
> {
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 953aac9d94..7c031f547e 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -110,6 +110,7 @@
> #define FF_API_COLORSPACE_NAME (LIBAVUTIL_VERSION_MAJOR < 58)
> #define FF_API_AV_MALLOCZ_ARRAY (LIBAVUTIL_VERSION_MAJOR < 58)
> #define FF_API_FIFO_PEEK2 (LIBAVUTIL_VERSION_MAJOR < 58)
> +#define FF_API_FIFO_PUBLIC (LIBAVUTIL_VERSION_MAJOR < 58)
>
> /**
> * @}
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 16:50 ` Andreas Rheinhardt
2022-01-13 16:59 ` James Almer
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 04/35] lavu/fifo: add new functions for determinining reading/writing size Anton Khirnov
` (33 subsequent siblings)
35 siblings, 2 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
Many AVFifoBuffer users operate on fixed-size elements (e.g. pointers),
but the current FIFO API deals exclusively in bytes, requiring extra
complexity in all these callers.
Add a new AVFifoBuffer constructor creating a FIFO with an element size
that may be larger than a byte. All operations on such a FIFO then
operate on complete elements.
---
doc/APIchanges | 6 ++
libavutil/fifo.c | 194 ++++++++++++++++++++++++++++++--------------
libavutil/fifo.h | 53 +++++++++---
libavutil/version.h | 2 +-
4 files changed, 179 insertions(+), 76 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 21fa02ae9d..5646cf2278 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -14,6 +14,12 @@ libavutil: 2021-04-27
API changes, most recent first:
+2022-01-xx - xxxxxxxxxx - lavu 57.19.100 - fifo.h
+ Add av_fifo_alloc2(), which allows setting a FIFO element size.
+ Operations on FIFOs created with this function on these elements
+ rather than bytes.
+ Add av_fifo_elem_size().
+
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
become an incomplete type in a future major libavutil version.
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index aaade01333..fc7c93470f 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -38,20 +38,28 @@ typedef struct CTX_STRUCT_NAME {
// These fields must match then contents of AVFifoBuffer in fifo.h
// until FF_API_FIFO_PUBLIC is removed
uint8_t *buffer;
+#if FF_API_FIFO_PUBLIC
uint8_t *rptr, *wptr, *end;
uint32_t rndx, wndx;
+#endif
/////////////////////////////////////////
+
+ size_t elem_size, nb_elems;
+ size_t offset_r, offset_w;
+ // distinguishes the ambigous situation offset_r == offset_w
+ int is_empty;
} FifoBuffer;
-AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
+AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
+ unsigned int flags)
{
FifoBuffer *f;
void *buffer;
- if (nmemb > FIFO_SIZE_MAX / size)
+ if (nb_elems > FIFO_SIZE_MAX / elem_size)
return NULL;
- buffer = av_realloc_array(NULL, nmemb, size);
+ buffer = av_realloc_array(NULL, nb_elems, elem_size);
if (!buffer)
return NULL;
f = av_mallocz(sizeof(*f));
@@ -60,11 +68,24 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
return NULL;
}
f->buffer = buffer;
- f->end = f->buffer + nmemb * size;
+#if FF_API_FIFO_PUBLIC
+ f->end = f->buffer + nb_elems * elem_size;
+#endif
+
+ f->nb_elems = nb_elems;
+ f->elem_size = elem_size;
+
av_fifo_reset((AVFifoBuffer*)f);
return (AVFifoBuffer*)f;
}
+AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
+{
+ if (nmemb > SIZE_MAX / size)
+ return NULL;
+ return av_fifo_alloc2(nmemb * size, 1, 0);
+}
+
AVFifoBuffer *av_fifo_alloc(unsigned int size)
{
return av_fifo_alloc_array(size, 1);
@@ -88,67 +109,85 @@ void av_fifo_freep(AVFifoBuffer **f)
void av_fifo_reset(AVFifoBuffer *f)
{
+ FifoBuffer *fb = (FifoBuffer*)f;
+
+ fb->offset_r = 0;
+ fb->offset_w = 0;
+ fb->is_empty = 1;
+#if FF_API_FIFO_PUBLIC
f->wptr = f->rptr = f->buffer;
f->wndx = f->rndx = 0;
+#endif
+}
+
+size_t av_fifo_elem_size(const AVFifoBuffer *f)
+{
+ const FifoBuffer *fb = (const FifoBuffer*)f;
+ return fb->elem_size;
}
int av_fifo_size(const AVFifoBuffer *f)
{
- return (uint32_t)(f->wndx - f->rndx);
+ const FifoBuffer *fb = (const FifoBuffer*)f;
+ if (fb->offset_w <= fb->offset_r && !fb->is_empty)
+ return fb->nb_elems - fb->offset_r + fb->offset_w;
+ return fb->offset_w - fb->offset_r;
}
int av_fifo_space(const AVFifoBuffer *f)
{
- return f->end - f->buffer - av_fifo_size(f);
+ const FifoBuffer *fb = (const FifoBuffer*)f;
+ return fb->nb_elems - av_fifo_size(f);
}
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
{
- unsigned int old_size = f->end - f->buffer;
+ FifoBuffer *fb = (FifoBuffer*)f;
if (new_size > FIFO_SIZE_MAX)
return AVERROR(EINVAL);
- if (old_size < new_size) {
- size_t offset_r = f->rptr - f->buffer;
- size_t offset_w = f->wptr - f->buffer;
+ if (fb->nb_elems < new_size) {
uint8_t *tmp;
- tmp = av_realloc(f->buffer, new_size);
+ tmp = av_realloc_array(f->buffer, new_size, fb->elem_size);
if (!tmp)
return AVERROR(ENOMEM);
// move the data from the beginning of the ring buffer
// to the newly allocated space
- // the second condition distinguishes full vs empty fifo
- if (offset_w <= offset_r && av_fifo_size(f)) {
- const size_t copy = FFMIN(new_size - old_size, offset_w);
- memcpy(tmp + old_size, tmp, copy);
- if (copy < offset_w) {
- memmove(tmp, tmp + copy , offset_w - copy);
- offset_w -= copy;
+ if (fb->offset_w <= fb->offset_r && !fb->is_empty) {
+ const size_t copy = FFMIN(new_size - fb->nb_elems, fb->offset_w);
+ memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size);
+ if (copy < fb->offset_w) {
+ memmove(tmp, tmp + copy * fb->elem_size,
+ (fb->offset_w - copy) * fb->elem_size);
+ fb->offset_w -= copy;
} else
- offset_w = old_size + copy;
+ fb->offset_w = fb->nb_elems + copy;
}
f->buffer = tmp;
+#if FF_API_FIFO_PUBLIC
f->end = f->buffer + new_size;
- f->rptr = f->buffer + offset_r;
- f->wptr = f->buffer + offset_w;
+ f->rptr = f->buffer + fb->offset_r * fb->elem_size;
+ f->wptr = f->buffer + fb->offset_w * fb->elem_size;
+#endif
+ fb->nb_elems = new_size;
}
return 0;
}
int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
{
- unsigned int old_size = f->end - f->buffer;
- if(size + (unsigned)av_fifo_size(f) < size)
+ FifoBuffer *fb = (FifoBuffer*)f;
+
+ if (size > UINT_MAX - av_fifo_size(f))
return AVERROR(EINVAL);
size += av_fifo_size(f);
-
- if (old_size < size)
- return av_fifo_realloc2(f, FFMAX(size, 2*old_size));
+ if (fb->nb_elems < size)
+ return av_fifo_realloc2(f, FFMAX(size, 2 * fb->nb_elems));
return 0;
}
@@ -157,62 +196,78 @@ int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
int (*func)(void *, void *, int))
{
+ FifoBuffer *fb = (FifoBuffer*)f;
int total = size;
+ size_t offset_w = fb->offset_w;
+#if FF_API_FIFO_PUBLIC
uint32_t wndx= f->wndx;
- uint8_t *wptr= f->wptr;
+#endif
if (size > av_fifo_space(f))
return AVERROR(ENOSPC);
do {
- int len = FFMIN(f->end - wptr, size);
+ size_t len = FFMIN(fb->nb_elems - offset_w, size);
+ uint8_t *wptr = f->buffer + offset_w * fb->elem_size;
+
if (func) {
- len = func(src, wptr, len);
- if (len <= 0)
+ int ret = func(src, wptr, len);
+ if (ret <= 0)
break;
+ len = ret;
} else {
- memcpy(wptr, src, len);
- src = (uint8_t *)src + len;
+ memcpy(wptr, src, len * fb->elem_size);
+ src = (uint8_t *)src + len * fb->elem_size;
}
- wptr += len;
- if (wptr >= f->end)
- wptr = f->buffer;
+ offset_w += len;
+ if (offset_w >= fb->nb_elems)
+ offset_w = 0;
+#if FF_API_FIFO_PUBLIC
wndx += len;
+#endif
size -= len;
} while (size > 0);
+#if FF_API_FIFO_PUBLIC
f->wndx= wndx;
- f->wptr= wptr;
+ f->wptr= f->buffer + offset_w * fb->elem_size;
+#endif
+ fb->offset_w = offset_w;
+
+ if (total - size > 0)
+ fb->is_empty = 0;
+
return total - size;
}
int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int))
{
- uint8_t *rptr = f->rptr;
+ FifoBuffer *fb = (FifoBuffer*)f;
+ size_t offset_r = fb->offset_r;
if (offset < 0 || buf_size > av_fifo_size(f) - offset)
return AVERROR(EINVAL);
- if (offset >= f->end - rptr)
- rptr += offset - (f->end - f->buffer);
+ if (offset >= fb->nb_elems - offset_r)
+ offset_r -= fb->nb_elems - offset;
else
- rptr += offset;
+ offset_r += offset;
while (buf_size > 0) {
+ uint8_t *rptr = f->buffer + offset_r * fb->elem_size;
int len;
- if (rptr >= f->end)
- rptr -= f->end - f->buffer;
-
- len = FFMIN(f->end - rptr, buf_size);
+ len = FFMIN(fb->nb_elems - offset_r, buf_size);
if (func)
func(dest, rptr, len);
else {
- memcpy(dest, rptr, len);
- dest = (uint8_t *)dest + len;
+ memcpy(dest, rptr, len * fb->elem_size);
+ dest = (uint8_t *)dest + len * fb->elem_size;
}
buf_size -= len;
- rptr += len;
+ offset_r += len;
+ if (offset_r >= fb->nb_elems)
+ offset_r -= fb->nb_elems;
}
return 0;
@@ -221,22 +276,24 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz
int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
void (*func)(void *, void *, int))
{
- uint8_t *rptr = f->rptr;
+ FifoBuffer *fb = (FifoBuffer*)f;
+ size_t offset_r = fb->offset_r;
if (buf_size > av_fifo_size(f))
return AVERROR(EINVAL);
do {
- int len = FFMIN(f->end - rptr, buf_size);
+ uint8_t *rptr = f->buffer + offset_r * fb->elem_size;
+ int len = FFMIN(fb->nb_elems - offset_r, buf_size);
if (func)
func(dest, rptr, len);
else {
- memcpy(dest, rptr, len);
- dest = (uint8_t *)dest + len;
+ memcpy(dest, rptr, len * fb->elem_size);
+ dest = (uint8_t *)dest + len * fb->elem_size;
}
- rptr += len;
- if (rptr >= f->end)
- rptr -= f->end - f->buffer;
+ offset_r += len;
+ if (offset_r >= fb->nb_elems)
+ offset_r -= fb->nb_elems;
buf_size -= len;
} while (buf_size > 0);
@@ -246,16 +303,19 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
void (*func)(void *, void *, int))
{
+ FifoBuffer *fb = (FifoBuffer*)f;
+
if (buf_size > av_fifo_size(f))
return AVERROR(EINVAL);
do {
- int len = FFMIN(f->end - f->rptr, buf_size);
+ uint8_t *rptr = f->buffer + fb->offset_r * fb->elem_size;
+ int len = FFMIN(fb->nb_elems - fb->offset_r, buf_size);
if (func)
- func(dest, f->rptr, len);
+ func(dest, rptr, len);
else {
- memcpy(dest, f->rptr, len);
- dest = (uint8_t *)dest + len;
+ memcpy(dest, rptr, len * fb->elem_size);
+ dest = (uint8_t *)dest + len * fb->elem_size;
}
av_fifo_drain(f, len);
buf_size -= len;
@@ -266,9 +326,19 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
/** Discard data from the FIFO. */
void av_fifo_drain(AVFifoBuffer *f, int size)
{
- av_assert2(av_fifo_size(f) >= size);
- f->rptr += size;
- if (f->rptr >= f->end)
- f->rptr -= f->end - f->buffer;
+ FifoBuffer *fb = (FifoBuffer*)f;
+ const size_t cur_size = av_fifo_size(f);
+
+ av_assert2(cur_size >= size);
+ if (cur_size == size)
+ fb->is_empty = 1;
+
+ if (fb->offset_r >= fb->nb_elems - size)
+ fb->offset_r -= fb->nb_elems - size;
+ else
+ fb->offset_r += size;
+#if FF_API_FIFO_PUBLIC
+ f->rptr = f->buffer + fb->offset_r * fb->elem_size;
f->rndx += size;
+#endif
}
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index ca4e7fe060..22362c0239 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -48,6 +48,9 @@ AVFifoBuffer;
* Initialize an AVFifoBuffer.
* @param size of FIFO
* @return AVFifoBuffer or NULL in case of memory allocation failure
+ *
+ * @note the element size of the allocated FIFO will be 1, i.e. all operations
+ * will be in bytes
*/
AVFifoBuffer *av_fifo_alloc(unsigned int size);
@@ -56,9 +59,26 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size);
* @param nmemb number of elements
* @param size size of the single element
* @return AVFifoBuffer or NULL in case of memory allocation failure
+ *
+ * @note the element size of the allocated FIFO will be 1, i.e. all operations
+ * will be in bytes
*/
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
+/**
+ * Allocate and initialize an AVFifoBuffer with a given element size.
+ *
+ * @param f pointer to the newly-allocated FIFO will be written here on success
+ * @param nb_elems initial number of elements that can be stored in the FIFO
+ * @param elem_size Size in bytes of a single element. Further operations on
+ * the returned FIFO will implicitly use this element size.
+ * @param flags currently unused, must be 0
+ *
+ * @return newly-allocated AVFifoBuffer on success, a negative error code on failure
+ */
+AVFifoBuffer *av_fifo_alloc2(size_t elems, size_t elem_size,
+ unsigned int flags);
+
/**
* Free an AVFifoBuffer.
* @param f AVFifoBuffer to free
@@ -71,6 +91,12 @@ void av_fifo_free(AVFifoBuffer *f);
*/
void av_fifo_freep(AVFifoBuffer **f);
+/**
+ * @return Element size for FIFO operations. This element size is set at
+ * FIFO allocation and remains constant during its lifetime
+ */
+size_t av_fifo_elem_size(const AVFifoBuffer *f);
+
/**
* Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
* @param f AVFifoBuffer to reset
@@ -78,16 +104,16 @@ void av_fifo_freep(AVFifoBuffer **f);
void av_fifo_reset(AVFifoBuffer *f);
/**
- * Return the amount of data in bytes in the AVFifoBuffer, that is the
- * amount of data you can read from it.
+ * Return the amount of data in the AVFifoBuffer, that is the amount of data
+ * (in elements, as returned by av_fifo_elem_size()) you can read from it.
* @param f AVFifoBuffer to read from
* @return size
*/
int av_fifo_size(const AVFifoBuffer *f);
/**
- * Return the amount of space in bytes in the AVFifoBuffer, that is the
- * amount of data you can write into it.
+ * Return the amount of space in the AVFifoBuffer, that is the amount of data
+ * (in elements, as returned by av_fifo_elem_size()) you can write into it.
* @param f AVFifoBuffer to write into
* @return size
*/
@@ -97,8 +123,8 @@ int av_fifo_space(const AVFifoBuffer *f);
* Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
* Similar as av_fifo_gereric_read but without discarding data.
* @param f AVFifoBuffer to read from
- * @param offset offset from current read position
- * @param buf_size number of bytes to read
+ * @param offset offset in elements from current read position
+ * @param buf_size number of elements to read
* @param func generic read function
* @param dest data destination
*
@@ -110,7 +136,7 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz
* Feed data from an AVFifoBuffer to a user-supplied callback.
* Similar as av_fifo_gereric_read but without discarding data.
* @param f AVFifoBuffer to read from
- * @param buf_size number of bytes to read
+ * @param buf_size number of elements to read
* @param func generic read function
* @param dest data destination
*
@@ -121,7 +147,7 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)
/**
* Feed data from an AVFifoBuffer to a user-supplied callback.
* @param f AVFifoBuffer to read from
- * @param buf_size number of bytes to read
+ * @param buf_size number of elements to read
* @param func generic read function
* @param dest data destination
*
@@ -134,13 +160,13 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)
* @param f AVFifoBuffer to write to
* @param src data source; non-const since it may be used as a
* modifiable context by the function defined in func
- * @param size number of bytes to write
+ * @param size number of elements to write
* @param func generic write function; the first parameter is src,
* the second is dest_buf, the third is dest_buf_size.
* func must return the number of bytes written to dest_buf, or <= 0 to
* indicate no more data available to write.
* If func is NULL, src is interpreted as a simple byte array for source data.
- * @return the number of bytes written to the FIFO or a negative error code on failure
+ * @return the number of elements written to the FIFO or a negative error code on failure
*/
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
@@ -149,7 +175,7 @@ int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void
* In case of reallocation failure, the old FIFO is kept unchanged.
*
* @param f AVFifoBuffer to resize
- * @param size new AVFifoBuffer size in bytes
+ * @param size new AVFifoBuffer size in elements
* @return <0 for failure, >=0 otherwise
*/
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
@@ -160,7 +186,8 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
* The new fifo size may be larger than the requested size.
*
* @param f AVFifoBuffer to resize
- * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
+ * @param additional_space the amount of space in elements to allocate in
+ * addition to av_fifo_size()
* @return <0 for failure, >=0 otherwise
*/
int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
@@ -168,7 +195,7 @@ int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
/**
* Read and discard the specified amount of data from an AVFifoBuffer.
* @param f AVFifoBuffer to read from
- * @param size amount of data to read in bytes
+ * @param size amount of data to read in elements
*/
void av_fifo_drain(AVFifoBuffer *f, int size);
diff --git a/libavutil/version.h b/libavutil/version.h
index 7c031f547e..180ebd5223 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
*/
#define LIBAVUTIL_VERSION_MAJOR 57
-#define LIBAVUTIL_VERSION_MINOR 18
+#define LIBAVUTIL_VERSION_MINOR 19
#define LIBAVUTIL_VERSION_MICRO 100
#define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size Anton Khirnov
@ 2022-01-13 16:50 ` Andreas Rheinhardt
2022-01-13 16:59 ` James Almer
1 sibling, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 16:50 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> Many AVFifoBuffer users operate on fixed-size elements (e.g. pointers),
> but the current FIFO API deals exclusively in bytes, requiring extra
> complexity in all these callers.
>
> Add a new AVFifoBuffer constructor creating a FIFO with an element size
> that may be larger than a byte. All operations on such a FIFO then
> operate on complete elements.
> ---
> doc/APIchanges | 6 ++
> libavutil/fifo.c | 194 ++++++++++++++++++++++++++++++--------------
> libavutil/fifo.h | 53 +++++++++---
> libavutil/version.h | 2 +-
> 4 files changed, 179 insertions(+), 76 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 21fa02ae9d..5646cf2278 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -14,6 +14,12 @@ libavutil: 2021-04-27
>
> API changes, most recent first:
>
> +2022-01-xx - xxxxxxxxxx - lavu 57.19.100 - fifo.h
> + Add av_fifo_alloc2(), which allows setting a FIFO element size.
> + Operations on FIFOs created with this function on these elements
> + rather than bytes.
> + Add av_fifo_elem_size().
> +
> 2022-01-xx - xxxxxxxxxx - lavu fifo.h
> Access to all AVFifoBuffer members is deprecated. The struct will
> become an incomplete type in a future major libavutil version.
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index aaade01333..fc7c93470f 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -38,20 +38,28 @@ typedef struct CTX_STRUCT_NAME {
> // These fields must match then contents of AVFifoBuffer in fifo.h
> // until FF_API_FIFO_PUBLIC is removed
> uint8_t *buffer;
> +#if FF_API_FIFO_PUBLIC
> uint8_t *rptr, *wptr, *end;
> uint32_t rndx, wndx;
> +#endif
> /////////////////////////////////////////
> +
> + size_t elem_size, nb_elems;
> + size_t offset_r, offset_w;
> + // distinguishes the ambigous situation offset_r == offset_w
ambiguous
> + int is_empty;
> } FifoBuffer;
>
> -AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> +AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
> + unsigned int flags)
> {
> FifoBuffer *f;
> void *buffer;
>
> - if (nmemb > FIFO_SIZE_MAX / size)
> + if (nb_elems > FIFO_SIZE_MAX / elem_size)
> return NULL;
>
> - buffer = av_realloc_array(NULL, nmemb, size);
> + buffer = av_realloc_array(NULL, nb_elems, elem_size);
> if (!buffer)
> return NULL;
> f = av_mallocz(sizeof(*f));
> @@ -60,11 +68,24 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> return NULL;
> }
> f->buffer = buffer;
> - f->end = f->buffer + nmemb * size;
> +#if FF_API_FIFO_PUBLIC
> + f->end = f->buffer + nb_elems * elem_size;
> +#endif
> +
> + f->nb_elems = nb_elems;
> + f->elem_size = elem_size;
> +
> av_fifo_reset((AVFifoBuffer*)f);
> return (AVFifoBuffer*)f;
> }
>
> +AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> +{
> + if (nmemb > SIZE_MAX / size)
> + return NULL;
> + return av_fifo_alloc2(nmemb * size, 1, 0);
> +}
> +
> AVFifoBuffer *av_fifo_alloc(unsigned int size)
> {
> return av_fifo_alloc_array(size, 1);
> @@ -88,67 +109,85 @@ void av_fifo_freep(AVFifoBuffer **f)
>
> void av_fifo_reset(AVFifoBuffer *f)
> {
> + FifoBuffer *fb = (FifoBuffer*)f;
> +
> + fb->offset_r = 0;
> + fb->offset_w = 0;
> + fb->is_empty = 1;
> +#if FF_API_FIFO_PUBLIC
> f->wptr = f->rptr = f->buffer;
> f->wndx = f->rndx = 0;
> +#endif
> +}
> +
> +size_t av_fifo_elem_size(const AVFifoBuffer *f)
> +{
> + const FifoBuffer *fb = (const FifoBuffer*)f;
> + return fb->elem_size;
> }
>
> int av_fifo_size(const AVFifoBuffer *f)
> {
> - return (uint32_t)(f->wndx - f->rndx);
> + const FifoBuffer *fb = (const FifoBuffer*)f;
> + if (fb->offset_w <= fb->offset_r && !fb->is_empty)
> + return fb->nb_elems - fb->offset_r + fb->offset_w;
> + return fb->offset_w - fb->offset_r;
> }
>
> int av_fifo_space(const AVFifoBuffer *f)
> {
> - return f->end - f->buffer - av_fifo_size(f);
> + const FifoBuffer *fb = (const FifoBuffer*)f;
> + return fb->nb_elems - av_fifo_size(f);
> }
>
> int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
> {
> - unsigned int old_size = f->end - f->buffer;
> + FifoBuffer *fb = (FifoBuffer*)f;
>
> if (new_size > FIFO_SIZE_MAX)
> return AVERROR(EINVAL);
>
> - if (old_size < new_size) {
> - size_t offset_r = f->rptr - f->buffer;
> - size_t offset_w = f->wptr - f->buffer;
> + if (fb->nb_elems < new_size) {
> uint8_t *tmp;
>
> - tmp = av_realloc(f->buffer, new_size);
> + tmp = av_realloc_array(f->buffer, new_size, fb->elem_size);
> if (!tmp)
> return AVERROR(ENOMEM);
>
> // move the data from the beginning of the ring buffer
> // to the newly allocated space
> - // the second condition distinguishes full vs empty fifo
> - if (offset_w <= offset_r && av_fifo_size(f)) {
> - const size_t copy = FFMIN(new_size - old_size, offset_w);
> - memcpy(tmp + old_size, tmp, copy);
> - if (copy < offset_w) {
> - memmove(tmp, tmp + copy , offset_w - copy);
> - offset_w -= copy;
> + if (fb->offset_w <= fb->offset_r && !fb->is_empty) {
> + const size_t copy = FFMIN(new_size - fb->nb_elems, fb->offset_w);
> + memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size);
> + if (copy < fb->offset_w) {
> + memmove(tmp, tmp + copy * fb->elem_size,
> + (fb->offset_w - copy) * fb->elem_size);
> + fb->offset_w -= copy;
> } else
> - offset_w = old_size + copy;
> + fb->offset_w = fb->nb_elems + copy;
> }
>
> f->buffer = tmp;
> +#if FF_API_FIFO_PUBLIC
> f->end = f->buffer + new_size;
> - f->rptr = f->buffer + offset_r;
> - f->wptr = f->buffer + offset_w;
> + f->rptr = f->buffer + fb->offset_r * fb->elem_size;
> + f->wptr = f->buffer + fb->offset_w * fb->elem_size;
> +#endif
> + fb->nb_elems = new_size;
> }
> return 0;
> }
>
> int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
> {
> - unsigned int old_size = f->end - f->buffer;
> - if(size + (unsigned)av_fifo_size(f) < size)
> + FifoBuffer *fb = (FifoBuffer*)f;
> +
> + if (size > UINT_MAX - av_fifo_size(f))
> return AVERROR(EINVAL);
>
> size += av_fifo_size(f);
> -
> - if (old_size < size)
> - return av_fifo_realloc2(f, FFMAX(size, 2*old_size));
> + if (fb->nb_elems < size)
> + return av_fifo_realloc2(f, FFMAX(size, 2 * fb->nb_elems));
> return 0;
> }
>
> @@ -157,62 +196,78 @@ int av_fifo_grow(AVFifoBuffer *f, unsigned int size)
> int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size,
> int (*func)(void *, void *, int))
> {
> + FifoBuffer *fb = (FifoBuffer*)f;
> int total = size;
> + size_t offset_w = fb->offset_w;
> +#if FF_API_FIFO_PUBLIC
> uint32_t wndx= f->wndx;
> - uint8_t *wptr= f->wptr;
> +#endif
>
> if (size > av_fifo_space(f))
> return AVERROR(ENOSPC);
>
> do {
> - int len = FFMIN(f->end - wptr, size);
> + size_t len = FFMIN(fb->nb_elems - offset_w, size);
> + uint8_t *wptr = f->buffer + offset_w * fb->elem_size;
> +
> if (func) {
> - len = func(src, wptr, len);
> - if (len <= 0)
> + int ret = func(src, wptr, len);
> + if (ret <= 0)
> break;
> + len = ret;
> } else {
> - memcpy(wptr, src, len);
> - src = (uint8_t *)src + len;
> + memcpy(wptr, src, len * fb->elem_size);
> + src = (uint8_t *)src + len * fb->elem_size;
> }
> - wptr += len;
> - if (wptr >= f->end)
> - wptr = f->buffer;
> + offset_w += len;
> + if (offset_w >= fb->nb_elems)
> + offset_w = 0;
> +#if FF_API_FIFO_PUBLIC
> wndx += len;
> +#endif
> size -= len;
> } while (size > 0);
> +#if FF_API_FIFO_PUBLIC
> f->wndx= wndx;
> - f->wptr= wptr;
> + f->wptr= f->buffer + offset_w * fb->elem_size;
> +#endif
> + fb->offset_w = offset_w;
> +
> + if (total - size > 0)
size < total
> + fb->is_empty = 0;
> +
> return total - size;
> }
>
> int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int))
> {
> - uint8_t *rptr = f->rptr;
> + FifoBuffer *fb = (FifoBuffer*)f;
> + size_t offset_r = fb->offset_r;
>
> if (offset < 0 || buf_size > av_fifo_size(f) - offset)
> return AVERROR(EINVAL);
>
> - if (offset >= f->end - rptr)
> - rptr += offset - (f->end - f->buffer);
> + if (offset >= fb->nb_elems - offset_r)
> + offset_r -= fb->nb_elems - offset;
> else
> - rptr += offset;
> + offset_r += offset;
>
> while (buf_size > 0) {
> + uint8_t *rptr = f->buffer + offset_r * fb->elem_size;
> int len;
>
> - if (rptr >= f->end)
> - rptr -= f->end - f->buffer;
> -
> - len = FFMIN(f->end - rptr, buf_size);
> + len = FFMIN(fb->nb_elems - offset_r, buf_size);
> if (func)
> func(dest, rptr, len);
> else {
> - memcpy(dest, rptr, len);
> - dest = (uint8_t *)dest + len;
> + memcpy(dest, rptr, len * fb->elem_size);
> + dest = (uint8_t *)dest + len * fb->elem_size;
> }
>
> buf_size -= len;
> - rptr += len;
> + offset_r += len;
> + if (offset_r >= fb->nb_elems)
> + offset_r -= fb->nb_elems;
> }
>
> return 0;
> @@ -221,22 +276,24 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz
> int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
> void (*func)(void *, void *, int))
> {
> - uint8_t *rptr = f->rptr;
> + FifoBuffer *fb = (FifoBuffer*)f;
> + size_t offset_r = fb->offset_r;
>
> if (buf_size > av_fifo_size(f))
> return AVERROR(EINVAL);
>
> do {
> - int len = FFMIN(f->end - rptr, buf_size);
> + uint8_t *rptr = f->buffer + offset_r * fb->elem_size;
> + int len = FFMIN(fb->nb_elems - offset_r, buf_size);
> if (func)
> func(dest, rptr, len);
> else {
> - memcpy(dest, rptr, len);
> - dest = (uint8_t *)dest + len;
> + memcpy(dest, rptr, len * fb->elem_size);
> + dest = (uint8_t *)dest + len * fb->elem_size;
> }
> - rptr += len;
> - if (rptr >= f->end)
> - rptr -= f->end - f->buffer;
> + offset_r += len;
> + if (offset_r >= fb->nb_elems)
> + offset_r -= fb->nb_elems;
> buf_size -= len;
> } while (buf_size > 0);
>
> @@ -246,16 +303,19 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size,
> int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
> void (*func)(void *, void *, int))
> {
> + FifoBuffer *fb = (FifoBuffer*)f;
> +
> if (buf_size > av_fifo_size(f))
> return AVERROR(EINVAL);
>
> do {
> - int len = FFMIN(f->end - f->rptr, buf_size);
> + uint8_t *rptr = f->buffer + fb->offset_r * fb->elem_size;
> + int len = FFMIN(fb->nb_elems - fb->offset_r, buf_size);
> if (func)
> - func(dest, f->rptr, len);
> + func(dest, rptr, len);
> else {
> - memcpy(dest, f->rptr, len);
> - dest = (uint8_t *)dest + len;
> + memcpy(dest, rptr, len * fb->elem_size);
> + dest = (uint8_t *)dest + len * fb->elem_size;
> }
> av_fifo_drain(f, len);
> buf_size -= len;
> @@ -266,9 +326,19 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
> /** Discard data from the FIFO. */
> void av_fifo_drain(AVFifoBuffer *f, int size)
> {
> - av_assert2(av_fifo_size(f) >= size);
> - f->rptr += size;
> - if (f->rptr >= f->end)
> - f->rptr -= f->end - f->buffer;
> + FifoBuffer *fb = (FifoBuffer*)f;
> + const size_t cur_size = av_fifo_size(f);
> +
> + av_assert2(cur_size >= size);
> + if (cur_size == size)
> + fb->is_empty = 1;
> +
> + if (fb->offset_r >= fb->nb_elems - size)
> + fb->offset_r -= fb->nb_elems - size;
> + else
> + fb->offset_r += size;
> +#if FF_API_FIFO_PUBLIC
> + f->rptr = f->buffer + fb->offset_r * fb->elem_size;
> f->rndx += size;
> +#endif
> }
> diff --git a/libavutil/fifo.h b/libavutil/fifo.h
> index ca4e7fe060..22362c0239 100644
> --- a/libavutil/fifo.h
> +++ b/libavutil/fifo.h
> @@ -48,6 +48,9 @@ AVFifoBuffer;
> * Initialize an AVFifoBuffer.
> * @param size of FIFO
> * @return AVFifoBuffer or NULL in case of memory allocation failure
> + *
> + * @note the element size of the allocated FIFO will be 1, i.e. all operations
> + * will be in bytes
> */
> AVFifoBuffer *av_fifo_alloc(unsigned int size);
>
> @@ -56,9 +59,26 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size);
> * @param nmemb number of elements
> * @param size size of the single element
> * @return AVFifoBuffer or NULL in case of memory allocation failure
> + *
> + * @note the element size of the allocated FIFO will be 1, i.e. all operations
> + * will be in bytes
> */
> AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
>
> +/**
> + * Allocate and initialize an AVFifoBuffer with a given element size.
> + *
> + * @param f pointer to the newly-allocated FIFO will be written here on success
> + * @param nb_elems initial number of elements that can be stored in the FIFO
> + * @param elem_size Size in bytes of a single element. Further operations on
> + * the returned FIFO will implicitly use this element size.
> + * @param flags currently unused, must be 0
> + *
> + * @return newly-allocated AVFifoBuffer on success, a negative error code on failure
> + */
> +AVFifoBuffer *av_fifo_alloc2(size_t elems, size_t elem_size,
> + unsigned int flags);
> +
> /**
> * Free an AVFifoBuffer.
> * @param f AVFifoBuffer to free
> @@ -71,6 +91,12 @@ void av_fifo_free(AVFifoBuffer *f);
> */
> void av_fifo_freep(AVFifoBuffer **f);
>
> +/**
> + * @return Element size for FIFO operations. This element size is set at
> + * FIFO allocation and remains constant during its lifetime
> + */
> +size_t av_fifo_elem_size(const AVFifoBuffer *f);
> +
> /**
> * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
> * @param f AVFifoBuffer to reset
> @@ -78,16 +104,16 @@ void av_fifo_freep(AVFifoBuffer **f);
> void av_fifo_reset(AVFifoBuffer *f);
>
> /**
> - * Return the amount of data in bytes in the AVFifoBuffer, that is the
> - * amount of data you can read from it.
> + * Return the amount of data in the AVFifoBuffer, that is the amount of data
> + * (in elements, as returned by av_fifo_elem_size()) you can read from it.
> * @param f AVFifoBuffer to read from
> * @return size
> */
> int av_fifo_size(const AVFifoBuffer *f);
>
> /**
> - * Return the amount of space in bytes in the AVFifoBuffer, that is the
> - * amount of data you can write into it.
> + * Return the amount of space in the AVFifoBuffer, that is the amount of data
> + * (in elements, as returned by av_fifo_elem_size()) you can write into it.
> * @param f AVFifoBuffer to write into
> * @return size
> */
> @@ -97,8 +123,8 @@ int av_fifo_space(const AVFifoBuffer *f);
> * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
> * Similar as av_fifo_gereric_read but without discarding data.
> * @param f AVFifoBuffer to read from
> - * @param offset offset from current read position
> - * @param buf_size number of bytes to read
> + * @param offset offset in elements from current read position
> + * @param buf_size number of elements to read
> * @param func generic read function
> * @param dest data destination
> *
> @@ -110,7 +136,7 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz
> * Feed data from an AVFifoBuffer to a user-supplied callback.
> * Similar as av_fifo_gereric_read but without discarding data.
> * @param f AVFifoBuffer to read from
> - * @param buf_size number of bytes to read
> + * @param buf_size number of elements to read
> * @param func generic read function
> * @param dest data destination
> *
> @@ -121,7 +147,7 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)
> /**
> * Feed data from an AVFifoBuffer to a user-supplied callback.
> * @param f AVFifoBuffer to read from
> - * @param buf_size number of bytes to read
> + * @param buf_size number of elements to read
> * @param func generic read function
> * @param dest data destination
> *
> @@ -134,13 +160,13 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)
> * @param f AVFifoBuffer to write to
> * @param src data source; non-const since it may be used as a
> * modifiable context by the function defined in func
> - * @param size number of bytes to write
> + * @param size number of elements to write
> * @param func generic write function; the first parameter is src,
> * the second is dest_buf, the third is dest_buf_size.
> * func must return the number of bytes written to dest_buf, or <= 0 to
> * indicate no more data available to write.
> * If func is NULL, src is interpreted as a simple byte array for source data.
> - * @return the number of bytes written to the FIFO or a negative error code on failure
> + * @return the number of elements written to the FIFO or a negative error code on failure
> */
> int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
>
> @@ -149,7 +175,7 @@ int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void
> * In case of reallocation failure, the old FIFO is kept unchanged.
> *
> * @param f AVFifoBuffer to resize
> - * @param size new AVFifoBuffer size in bytes
> + * @param size new AVFifoBuffer size in elements
> * @return <0 for failure, >=0 otherwise
> */
> int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
> @@ -160,7 +186,8 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
> * The new fifo size may be larger than the requested size.
> *
> * @param f AVFifoBuffer to resize
> - * @param additional_space the amount of space in bytes to allocate in addition to av_fifo_size()
> + * @param additional_space the amount of space in elements to allocate in
> + * addition to av_fifo_size()
> * @return <0 for failure, >=0 otherwise
> */
> int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
> @@ -168,7 +195,7 @@ int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
> /**
> * Read and discard the specified amount of data from an AVFifoBuffer.
> * @param f AVFifoBuffer to read from
> - * @param size amount of data to read in bytes
> + * @param size amount of data to read in elements
> */
> void av_fifo_drain(AVFifoBuffer *f, int size);
>
> diff --git a/libavutil/version.h b/libavutil/version.h
> index 7c031f547e..180ebd5223 100644
> --- a/libavutil/version.h
> +++ b/libavutil/version.h
> @@ -79,7 +79,7 @@
> */
>
> #define LIBAVUTIL_VERSION_MAJOR 57
> -#define LIBAVUTIL_VERSION_MINOR 18
> +#define LIBAVUTIL_VERSION_MINOR 19
> #define LIBAVUTIL_VERSION_MICRO 100
>
> #define LIBAVUTIL_VERSION_INT AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
>
You should document that av_fifo_peek2() must not be used with a fifo
returned by av_fifo_alloc2(). (It has not been ported to use elements
instead of bytes and its documentation just claims "The used buffer size
can be checked with av_fifo_size()" which is not wrong but does not
really mention that this now returns the element count.)
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size Anton Khirnov
2022-01-13 16:50 ` Andreas Rheinhardt
@ 2022-01-13 16:59 ` James Almer
1 sibling, 0 replies; 53+ messages in thread
From: James Almer @ 2022-01-13 16:59 UTC (permalink / raw)
To: ffmpeg-devel
On 1/11/2022 5:45 PM, Anton Khirnov wrote:
> +/**
> + * Allocate and initialize an AVFifoBuffer with a given element size.
> + *
> + * @param f pointer to the newly-allocated FIFO will be written here on success
> + * @param nb_elems initial number of elements that can be stored in the FIFO
> + * @param elem_size Size in bytes of a single element. Further operations on
> + * the returned FIFO will implicitly use this element size.
> + * @param flags currently unused, must be 0
> + *
> + * @return newly-allocated AVFifoBuffer on success, a negative error code on failure
> + */
> +AVFifoBuffer *av_fifo_alloc2(size_t elems, size_t elem_size,
> + unsigned int flags);
The documentation doesn't match the signature.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 04/35] lavu/fifo: add new functions for determinining reading/writing size
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function Anton Khirnov
` (32 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
Consistently use size_t for sizes.
The new names should also be more clear - e.g. current av_fifo_size()
could be misinterpreted as meaning the allocated size.
---
doc/APIchanges | 2 +-
libavutil/fifo.c | 16 +++++++++++++---
libavutil/fifo.h | 10 ++++++++++
3 files changed, 24 insertions(+), 4 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 5646cf2278..9400c5147a 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -18,7 +18,7 @@ API changes, most recent first:
Add av_fifo_alloc2(), which allows setting a FIFO element size.
Operations on FIFOs created with this function on these elements
rather than bytes.
- Add av_fifo_elem_size().
+ Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index fc7c93470f..8cde2c20e1 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -126,7 +126,7 @@ size_t av_fifo_elem_size(const AVFifoBuffer *f)
return fb->elem_size;
}
-int av_fifo_size(const AVFifoBuffer *f)
+size_t av_fifo_can_read(const AVFifoBuffer *f)
{
const FifoBuffer *fb = (const FifoBuffer*)f;
if (fb->offset_w <= fb->offset_r && !fb->is_empty)
@@ -134,10 +134,20 @@ int av_fifo_size(const AVFifoBuffer *f)
return fb->offset_w - fb->offset_r;
}
-int av_fifo_space(const AVFifoBuffer *f)
+size_t av_fifo_can_write(const AVFifoBuffer *f)
{
const FifoBuffer *fb = (const FifoBuffer*)f;
- return fb->nb_elems - av_fifo_size(f);
+ return fb->nb_elems - av_fifo_can_read(f);
+}
+
+int av_fifo_size(const AVFifoBuffer *f)
+{
+ return av_fifo_can_read(f);
+}
+
+int av_fifo_space(const AVFifoBuffer *f)
+{
+ return av_fifo_can_write(f);
}
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 22362c0239..9e78082b3b 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -119,6 +119,16 @@ int av_fifo_size(const AVFifoBuffer *f);
*/
int av_fifo_space(const AVFifoBuffer *f);
+/**
+ * @return number of elements available for reading from the given FIFO.
+ */
+size_t av_fifo_can_read(const AVFifoBuffer *f);
+
+/**
+ * @return number of elements that can be written into the given FIFO.
+ */
+size_t av_fifo_can_write(const AVFifoBuffer *f);
+
/**
* Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
* Similar as av_fifo_gereric_read but without discarding data.
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (2 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 04/35] lavu/fifo: add new functions for determinining reading/writing size Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 17:04 ` James Almer
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 06/35] lavu/fifo: add a new function for draining the FIFO Anton Khirnov
` (31 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
Consistently use size_t for sizes.
Unlike av_fifo_grow(), which addds to the currently used size, this
function adds to the allocated size.
No new function is provided for a generic realloc, since the current code
only supports increasing the FIFO size.
---
doc/APIchanges | 3 ++-
libavutil/fifo.c | 61 +++++++++++++++++++++++++++---------------------
libavutil/fifo.h | 14 +++++++++++
3 files changed, 50 insertions(+), 28 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 9400c5147a..98eae55719 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -18,7 +18,8 @@ API changes, most recent first:
Add av_fifo_alloc2(), which allows setting a FIFO element size.
Operations on FIFOs created with this function on these elements
rather than bytes.
- Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write().
+ Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
+ av_fifo_grow2().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index 8cde2c20e1..28d6d1b2e6 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -150,41 +150,48 @@ int av_fifo_space(const AVFifoBuffer *f)
return av_fifo_can_write(f);
}
-int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
+int av_fifo_grow2(AVFifoBuffer *f, size_t inc)
{
FifoBuffer *fb = (FifoBuffer*)f;
+ uint8_t *tmp;
- if (new_size > FIFO_SIZE_MAX)
+ if (inc > FIFO_SIZE_MAX - fb->nb_elems)
return AVERROR(EINVAL);
- if (fb->nb_elems < new_size) {
- uint8_t *tmp;
-
- tmp = av_realloc_array(f->buffer, new_size, fb->elem_size);
- if (!tmp)
- return AVERROR(ENOMEM);
-
- // move the data from the beginning of the ring buffer
- // to the newly allocated space
- if (fb->offset_w <= fb->offset_r && !fb->is_empty) {
- const size_t copy = FFMIN(new_size - fb->nb_elems, fb->offset_w);
- memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size);
- if (copy < fb->offset_w) {
- memmove(tmp, tmp + copy * fb->elem_size,
- (fb->offset_w - copy) * fb->elem_size);
- fb->offset_w -= copy;
- } else
- fb->offset_w = fb->nb_elems + copy;
- }
+ tmp = av_realloc_array(f->buffer, fb->nb_elems + inc, fb->elem_size);
+ if (!tmp)
+ return AVERROR(ENOMEM);
+
+ // move the data from the beginning of the ring buffer
+ // to the newly allocated space
+ if (fb->offset_w <= fb->offset_r && !fb->is_empty) {
+ const size_t copy = FFMIN(inc, fb->offset_w);
+ memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size);
+ if (copy < fb->offset_w) {
+ memmove(tmp, tmp + copy * fb->elem_size,
+ (fb->offset_w - copy) * fb->elem_size);
+ fb->offset_w -= copy;
+ } else
+ fb->offset_w = fb->nb_elems + copy;
+ }
+
+ f->buffer = tmp;
+ fb->nb_elems += inc;
- f->buffer = tmp;
#if FF_API_FIFO_PUBLIC
- f->end = f->buffer + new_size;
- f->rptr = f->buffer + fb->offset_r * fb->elem_size;
- f->wptr = f->buffer + fb->offset_w * fb->elem_size;
+ f->end = f->buffer + fb->nb_elems * fb->elem_size;
+ f->rptr = f->buffer + fb->offset_r * fb->elem_size;
+ f->wptr = f->buffer + fb->offset_w * fb->elem_size;
#endif
- fb->nb_elems = new_size;
- }
+
+ return 0;
+}
+
+int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ if (fb->nb_elems < new_size)
+ return av_fifo_grow2(f, new_size - fb->nb_elems);
return 0;
}
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 9e78082b3b..375d0d133b 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -129,6 +129,20 @@ size_t av_fifo_can_read(const AVFifoBuffer *f);
*/
size_t av_fifo_can_write(const AVFifoBuffer *f);
+/**
+ * Enlarge an AVFifoBuffer.
+ *
+ * On success, the FIFO will be large enough to hold exactly
+ * inc + av_fifo_can_read() + av_fifo_can_write()
+ * elements. In case of failure, the old FIFO is kept unchanged.
+ *
+ * @param f AVFifoBuffer to resize
+ * @param inc number of elements to allocate for, in addition to the current
+ * allocated size
+ * @return a non-negative number on success, a negative error code on failure
+ */
+int av_fifo_grow2(AVFifoBuffer *f, size_t inc);
+
/**
* Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
* Similar as av_fifo_gereric_read but without discarding data.
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function Anton Khirnov
@ 2022-01-13 17:04 ` James Almer
0 siblings, 0 replies; 53+ messages in thread
From: James Almer @ 2022-01-13 17:04 UTC (permalink / raw)
To: ffmpeg-devel
On 1/11/2022 5:45 PM, Anton Khirnov wrote:
> Consistently use size_t for sizes.
>
> Unlike av_fifo_grow(), which addds to the currently used size, this
> function adds to the allocated size.
>
> No new function is provided for a generic realloc, since the current code
> only supports increasing the FIFO size.
> ---
> doc/APIchanges | 3 ++-
> libavutil/fifo.c | 61 +++++++++++++++++++++++++++---------------------
> libavutil/fifo.h | 14 +++++++++++
> 3 files changed, 50 insertions(+), 28 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 9400c5147a..98eae55719 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -18,7 +18,8 @@ API changes, most recent first:
> Add av_fifo_alloc2(), which allows setting a FIFO element size.
> Operations on FIFOs created with this function on these elements
> rather than bytes.
> - Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write().
> + Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
> + av_fifo_grow2().
>
> 2022-01-xx - xxxxxxxxxx - lavu fifo.h
> Access to all AVFifoBuffer members is deprecated. The struct will
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index 8cde2c20e1..28d6d1b2e6 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -150,41 +150,48 @@ int av_fifo_space(const AVFifoBuffer *f)
> return av_fifo_can_write(f);
> }
>
> -int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
> +int av_fifo_grow2(AVFifoBuffer *f, size_t inc)
> {
> FifoBuffer *fb = (FifoBuffer*)f;
> + uint8_t *tmp;
>
> - if (new_size > FIFO_SIZE_MAX)
> + if (inc > FIFO_SIZE_MAX - fb->nb_elems)
> return AVERROR(EINVAL);
>
> - if (fb->nb_elems < new_size) {
> - uint8_t *tmp;
> -
> - tmp = av_realloc_array(f->buffer, new_size, fb->elem_size);
> - if (!tmp)
> - return AVERROR(ENOMEM);
> -
> - // move the data from the beginning of the ring buffer
> - // to the newly allocated space
> - if (fb->offset_w <= fb->offset_r && !fb->is_empty) {
> - const size_t copy = FFMIN(new_size - fb->nb_elems, fb->offset_w);
> - memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size);
> - if (copy < fb->offset_w) {
> - memmove(tmp, tmp + copy * fb->elem_size,
> - (fb->offset_w - copy) * fb->elem_size);
> - fb->offset_w -= copy;
> - } else
> - fb->offset_w = fb->nb_elems + copy;
> - }
> + tmp = av_realloc_array(f->buffer, fb->nb_elems + inc, fb->elem_size);
This sounds like a good opportunity to introduce av_fast_realloc_array()
in mem.h, with FifoBuffer being a private struct where you can safely
store the required buffer size field.
av_realloc_array() can be slow in an auto-grow scenario if there's a lot
of buffering before stuff starts being drained.
> + if (!tmp)
> + return AVERROR(ENOMEM);
> +
> + // move the data from the beginning of the ring buffer
> + // to the newly allocated space
> + if (fb->offset_w <= fb->offset_r && !fb->is_empty) {
> + const size_t copy = FFMIN(inc, fb->offset_w);
> + memcpy(tmp + fb->nb_elems * fb->elem_size, tmp, copy * fb->elem_size);
> + if (copy < fb->offset_w) {
> + memmove(tmp, tmp + copy * fb->elem_size,
> + (fb->offset_w - copy) * fb->elem_size);
> + fb->offset_w -= copy;
> + } else
> + fb->offset_w = fb->nb_elems + copy;
> + }
> +
> + f->buffer = tmp;
> + fb->nb_elems += inc;
>
> - f->buffer = tmp;
> #if FF_API_FIFO_PUBLIC
> - f->end = f->buffer + new_size;
> - f->rptr = f->buffer + fb->offset_r * fb->elem_size;
> - f->wptr = f->buffer + fb->offset_w * fb->elem_size;
> + f->end = f->buffer + fb->nb_elems * fb->elem_size;
> + f->rptr = f->buffer + fb->offset_r * fb->elem_size;
> + f->wptr = f->buffer + fb->offset_w * fb->elem_size;
> #endif
> - fb->nb_elems = new_size;
> - }
> +
> + return 0;
> +}
> +
> +int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
> +{
> + FifoBuffer *fb = (FifoBuffer*)f;
> + if (fb->nb_elems < new_size)
> + return av_fifo_grow2(f, new_size - fb->nb_elems);
> return 0;
> }
>
> diff --git a/libavutil/fifo.h b/libavutil/fifo.h
> index 9e78082b3b..375d0d133b 100644
> --- a/libavutil/fifo.h
> +++ b/libavutil/fifo.h
> @@ -129,6 +129,20 @@ size_t av_fifo_can_read(const AVFifoBuffer *f);
> */
> size_t av_fifo_can_write(const AVFifoBuffer *f);
>
> +/**
> + * Enlarge an AVFifoBuffer.
> + *
> + * On success, the FIFO will be large enough to hold exactly
> + * inc + av_fifo_can_read() + av_fifo_can_write()
> + * elements. In case of failure, the old FIFO is kept unchanged.
> + *
> + * @param f AVFifoBuffer to resize
> + * @param inc number of elements to allocate for, in addition to the current
> + * allocated size
> + * @return a non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_grow2(AVFifoBuffer *f, size_t inc);
> +
> /**
> * Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
> * Similar as av_fifo_gereric_read but without discarding data.
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 06/35] lavu/fifo: add a new function for draining the FIFO
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (3 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions Anton Khirnov
` (30 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
Consistently use size_t for sizes
---
doc/APIchanges | 2 +-
libavutil/fifo.c | 36 +++++++++++++++++++++---------------
libavutil/fifo.h | 7 +++++++
3 files changed, 29 insertions(+), 16 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 98eae55719..f2769d4165 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -19,7 +19,7 @@ API changes, most recent first:
Operations on FIFOs created with this function on these elements
rather than bytes.
Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
- av_fifo_grow2().
+ av_fifo_grow2(), av_fifo_drain2().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index 28d6d1b2e6..e9f439e219 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -187,6 +187,26 @@ int av_fifo_grow2(AVFifoBuffer *f, size_t inc)
return 0;
}
+void av_fifo_drain2(AVFifoBuffer *f, size_t size)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ const size_t cur_size = av_fifo_can_read(f);
+
+ av_assert0(cur_size >= size);
+ if (cur_size == size)
+ fb->is_empty = 1;
+
+ if (fb->offset_r >= fb->nb_elems - size)
+ fb->offset_r -= fb->nb_elems - size;
+ else
+ fb->offset_r += size;
+
+#if FF_API_FIFO_PUBLIC
+ f->rptr = f->buffer + fb->offset_r * fb->elem_size;
+ f->rndx += size;
+#endif
+}
+
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
{
FifoBuffer *fb = (FifoBuffer*)f;
@@ -343,19 +363,5 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size,
/** Discard data from the FIFO. */
void av_fifo_drain(AVFifoBuffer *f, int size)
{
- FifoBuffer *fb = (FifoBuffer*)f;
- const size_t cur_size = av_fifo_size(f);
-
- av_assert2(cur_size >= size);
- if (cur_size == size)
- fb->is_empty = 1;
-
- if (fb->offset_r >= fb->nb_elems - size)
- fb->offset_r -= fb->nb_elems - size;
- else
- fb->offset_r += size;
-#if FF_API_FIFO_PUBLIC
- f->rptr = f->buffer + fb->offset_r * fb->elem_size;
- f->rndx += size;
-#endif
+ return av_fifo_drain2(f, size);
}
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 375d0d133b..d593304edb 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -143,6 +143,13 @@ size_t av_fifo_can_write(const AVFifoBuffer *f);
*/
int av_fifo_grow2(AVFifoBuffer *f, size_t inc);
+/**
+ * Discard the specified amount of data from an AVFifoBuffer.
+ * @param size number of elements to discard, MUST NOT be larger than
+ * av_fifo_can_read(f)
+ */
+void av_fifo_drain2(AVFifoBuffer *f, size_t size);
+
/**
* Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
* Similar as av_fifo_gereric_read but without discarding data.
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (4 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 06/35] lavu/fifo: add a new function for draining the FIFO Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 17:31 ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions Anton Khirnov
` (29 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
Use separate functions for writing from a buffer and a callback, since
the overwhelming majority of callers use a buffer and should not be
forced to pass extra NULL parameters or use a longer name.
Consistently use size_t for sizes.
---
doc/APIchanges | 2 +-
libavutil/fifo.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
libavutil/fifo.h | 46 ++++++++++++++++++++++++++++++++++++++
3 files changed, 105 insertions(+), 1 deletion(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index f2769d4165..0b179c30e5 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -19,7 +19,7 @@ API changes, most recent first:
Operations on FIFOs created with this function on these elements
rather than bytes.
Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
- av_fifo_grow2(), av_fifo_drain2().
+ av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index e9f439e219..1d94fff457 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -207,6 +207,64 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size)
#endif
}
+static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_elems,
+ AVFifoCB read_cb, void *opaque)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ size_t to_write = *nb_elems;
+ size_t offset_w = fb->offset_w;
+ int ret = 0;
+#if FF_API_FIFO_PUBLIC
+ uint32_t wndx= f->wndx;
+#endif
+
+ if (to_write > av_fifo_can_write(f))
+ return AVERROR(ENOSPC);
+
+ do {
+ size_t len = FFMIN(fb->nb_elems - offset_w, to_write);
+ uint8_t *wptr = f->buffer + offset_w * fb->elem_size;
+
+ if (read_cb) {
+ ret = read_cb(opaque, wptr, &len);
+ if (ret < 0 || len == 0)
+ break;
+ } else {
+ memcpy(wptr, buf, len * fb->elem_size);
+ buf += len * fb->elem_size;
+ }
+ offset_w += len;
+ if (offset_w >= fb->nb_elems)
+ offset_w = 0;
+#if FF_API_FIFO_PUBLIC
+ wndx += len;
+#endif
+ to_write -= len;
+ } while (to_write > 0);
+#if FF_API_FIFO_PUBLIC
+ f->wndx= wndx;
+ f->wptr= f->buffer + offset_w * fb->elem_size;
+#endif
+ fb->offset_w = offset_w;
+
+ if (*nb_elems - to_write > 0)
+ fb->is_empty = 0;
+ *nb_elems -= to_write;
+
+ return ret;
+}
+
+int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems)
+{
+ return fifo_write_common(f, buf, &nb_elems, NULL, NULL);
+}
+
+int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
+ void *opaque, size_t *nb_elems)
+{
+ return fifo_write_common(f, NULL, nb_elems, read_cb, opaque);
+}
+
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
{
FifoBuffer *fb = (FifoBuffer*)f;
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index d593304edb..ac1245ff39 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -44,6 +44,24 @@ typedef struct AVFifoBuffer
#endif
AVFifoBuffer;
+/**
+ * Callback for writing or reading from a FIFO, passed to (and invoked from) the
+ * av_fifo_*_cb() functions. It may be invoked multiple times from a single
+ * av_fifo_*_cb() call and may process less data than the maximum size indicated
+ * by nb_elems.
+ *
+ * @param opaque the opaque pointer provided to the av_fifo_*_cb() function
+ * @param buf the buffer for reading or writing the data, depending on which
+ * av_fifo_*_cb function is called
+ * @param nb_elems On entry contains the maximum number of elements that can be
+ * read from / written into buf. On success, the callback should
+ * update it to contain the number of elements actually written.
+ *
+ * @return 0 on success, a negative error code on failure (will be returned from
+ * the invoking av_fifo_*_cb() function)
+ */
+typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);
+
/**
* Initialize an AVFifoBuffer.
* @param size of FIFO
@@ -143,6 +161,34 @@ size_t av_fifo_can_write(const AVFifoBuffer *f);
*/
int av_fifo_grow2(AVFifoBuffer *f, size_t inc);
+/**
+ * Write data into a FIFO.
+ *
+ * @param f the FIFO buffer
+ * @param buf Data to be written. nb_elems * av_fifo_elem_size(f) bytes will be
+ * read from buf.
+ * @param nb_elems number of elements to write into FIFO
+ *
+ * @return a non-negative number on success, a negative error code on failure
+ */
+int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems);
+
+/**
+ * Write data from a user-provided callback into a FIFO.
+ *
+ * @param f the FIFO buffer
+ * @param read_cb Callback supplying the data to the FIFO. May be called
+ * multiple times.
+ * @param opaque opaque user data to be provided to read_cb
+ * @param nb_elems Should point to the maximum number of elements that can be
+ * written. Will be updated to contain the number of elements
+ * actually written.
+ *
+ * @return non-negative number on success, a negative error code on failure
+ */
+int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
+ void *opaque, size_t *nb_elems);
+
/**
* Discard the specified amount of data from an AVFifoBuffer.
* @param size number of elements to discard, MUST NOT be larger than
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions Anton Khirnov
@ 2022-01-13 17:31 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 17:31 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> Use separate functions for writing from a buffer and a callback, since
> the overwhelming majority of callers use a buffer and should not be
> forced to pass extra NULL parameters or use a longer name.
>
> Consistently use size_t for sizes.
> ---
> doc/APIchanges | 2 +-
> libavutil/fifo.c | 58 ++++++++++++++++++++++++++++++++++++++++++++++++
> libavutil/fifo.h | 46 ++++++++++++++++++++++++++++++++++++++
> 3 files changed, 105 insertions(+), 1 deletion(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index f2769d4165..0b179c30e5 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -19,7 +19,7 @@ API changes, most recent first:
> Operations on FIFOs created with this function on these elements
> rather than bytes.
> Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
> - av_fifo_grow2(), av_fifo_drain2().
> + av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb().
>
> 2022-01-xx - xxxxxxxxxx - lavu fifo.h
> Access to all AVFifoBuffer members is deprecated. The struct will
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index e9f439e219..1d94fff457 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -207,6 +207,64 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size)
> #endif
> }
>
> +static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_elems,
> + AVFifoCB read_cb, void *opaque)
Should be write_cb.
> +{
> + FifoBuffer *fb = (FifoBuffer*)f;
> + size_t to_write = *nb_elems;
How about calling this "left"?
> + size_t offset_w = fb->offset_w;
> + int ret = 0;
> +#if FF_API_FIFO_PUBLIC
> + uint32_t wndx= f->wndx;
> +#endif
> +
> + if (to_write > av_fifo_can_write(f))
> + return AVERROR(ENOSPC);
> +
> + do {
> + size_t len = FFMIN(fb->nb_elems - offset_w, to_write);
> + uint8_t *wptr = f->buffer + offset_w * fb->elem_size;
> +
> + if (read_cb) {
> + ret = read_cb(opaque, wptr, &len);
> + if (ret < 0 || len == 0)
> + break;
> + } else {
> + memcpy(wptr, buf, len * fb->elem_size);
> + buf += len * fb->elem_size;
> + }
> + offset_w += len;
> + if (offset_w >= fb->nb_elems)
> + offset_w = 0;
> +#if FF_API_FIFO_PUBLIC
> + wndx += len;
> +#endif
> + to_write -= len;
> + } while (to_write > 0);
> +#if FF_API_FIFO_PUBLIC
> + f->wndx= wndx;
> + f->wptr= f->buffer + offset_w * fb->elem_size;
> +#endif
> + fb->offset_w = offset_w;
> +
> + if (*nb_elems - to_write > 0)
to_write < *nb_elems
> + fb->is_empty = 0;
> + *nb_elems -= to_write;
> +
> + return ret;
> +}
> +
> +int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems)
> +{
> + return fifo_write_common(f, buf, &nb_elems, NULL, NULL);
> +}
> +
> +int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
> + void *opaque, size_t *nb_elems)
> +{
> + return fifo_write_common(f, NULL, nb_elems, read_cb, opaque);
> +}
> +
> int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
> {
> FifoBuffer *fb = (FifoBuffer*)f;
> diff --git a/libavutil/fifo.h b/libavutil/fifo.h
> index d593304edb..ac1245ff39 100644
> --- a/libavutil/fifo.h
> +++ b/libavutil/fifo.h
> @@ -44,6 +44,24 @@ typedef struct AVFifoBuffer
> #endif
> AVFifoBuffer;
>
> +/**
> + * Callback for writing or reading from a FIFO, passed to (and invoked from) the
> + * av_fifo_*_cb() functions. It may be invoked multiple times from a single
> + * av_fifo_*_cb() call and may process less data than the maximum size indicated
> + * by nb_elems.
> + *
> + * @param opaque the opaque pointer provided to the av_fifo_*_cb() function
> + * @param buf the buffer for reading or writing the data, depending on which
> + * av_fifo_*_cb function is called
> + * @param nb_elems On entry contains the maximum number of elements that can be
> + * read from / written into buf. On success, the callback should
> + * update it to contain the number of elements actually written.
The last sentence is write-only, although this callback is also used for
reading.
> + *
> + * @return 0 on success, a negative error code on failure (will be returned from
> + * the invoking av_fifo_*_cb() function)
> + */
> +typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);
> +
> /**
> * Initialize an AVFifoBuffer.
> * @param size of FIFO
> @@ -143,6 +161,34 @@ size_t av_fifo_can_write(const AVFifoBuffer *f);
> */
> int av_fifo_grow2(AVFifoBuffer *f, size_t inc);
>
> +/**
> + * Write data into a FIFO.
> + *
> + * @param f the FIFO buffer
> + * @param buf Data to be written. nb_elems * av_fifo_elem_size(f) bytes will be
> + * read from buf.
> + * @param nb_elems number of elements to write into FIFO
> + *
> + * @return a non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems);
> +
> +/**
> + * Write data from a user-provided callback into a FIFO.
> + *
> + * @param f the FIFO buffer
> + * @param read_cb Callback supplying the data to the FIFO. May be called
> + * multiple times.
> + * @param opaque opaque user data to be provided to read_cb
> + * @param nb_elems Should point to the maximum number of elements that can be
> + * written. Will be updated to contain the number of elements
> + * actually written.
> + *
> + * @return non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
s/read_cb/write_cb/
(also above)
> + void *opaque, size_t *nb_elems);
> +
> /**
> * Discard the specified amount of data from an AVFifoBuffer.
> * @param size number of elements to discard, MUST NOT be larger than
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (5 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 17:41 ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed Anton Khirnov
` (28 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
As for writing, use separate functions for reading to a buffer and a
callback. Allow the callbacks to limit the amount of data read,
similarly to what is done for writing.
Consistently use size_t for sizes.
---
doc/APIchanges | 3 ++-
libavutil/fifo.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
libavutil/fifo.h | 60 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 130 insertions(+), 1 deletion(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index 0b179c30e5..f64759d69d 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -19,7 +19,8 @@ API changes, most recent first:
Operations on FIFOs created with this function on these elements
rather than bytes.
Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
- av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb().
+ av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(),
+ av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index 1d94fff457..ea944bc936 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -265,6 +265,74 @@ int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
return fifo_write_common(f, NULL, nb_elems, read_cb, opaque);
}
+static int fifo_peek_common(const AVFifoBuffer *f, uint8_t *buf, size_t *nb_elems,
+ size_t offset, AVFifoCB write_cb, void *opaque)
+{
+ const FifoBuffer *fb = (FifoBuffer*)f;
+ size_t to_read = *nb_elems;
+ size_t offset_r = fb->offset_r;
+ int ret = 0;
+
+ if (offset > av_fifo_can_read(f) ||
+ to_read > av_fifo_can_read(f) - offset) {
+ *nb_elems = 0;
+ return AVERROR(EINVAL);
+ }
+
+ if (offset_r >= fb->nb_elems - offset)
+ offset_r -= fb->nb_elems - offset;
+ else
+ offset_r += offset;
+
+ do {
+ size_t len = FFMIN(fb->nb_elems - offset_r, to_read);
+ uint8_t *rptr = f->buffer + offset_r * fb->elem_size;
+
+ if (write_cb) {
+ ret = write_cb(opaque, rptr, &len);
+ if (ret < 0 || len == 0)
+ break;
+ } else {
+ memcpy(buf, rptr, len * fb->elem_size);
+ buf += len * fb->elem_size;
+ }
+ offset_r += len;
+ if (offset_r >= fb->nb_elems)
+ offset_r = 0;
+ to_read -= len;
+ } while (to_read > 0);
+
+ *nb_elems -= to_read;
+
+ return ret;
+}
+
+int av_fifo_read(AVFifoBuffer *f, void *buf, size_t nb_elems)
+{
+ int ret = fifo_peek_common(f, buf, &nb_elems, 0, NULL, NULL);
+ av_fifo_drain2(f, nb_elems);
+ return ret;
+}
+
+int av_fifo_read_to_cb(AVFifoBuffer *f, AVFifoCB write_cb,
+ void *opaque, size_t *nb_elems)
+{
+ int ret = fifo_peek_common(f, NULL, nb_elems, 0, write_cb, opaque);
+ av_fifo_drain2(f, *nb_elems);
+ return ret;
+}
+
+int av_fifo_peek(AVFifoBuffer *f, void *buf, size_t nb_elems, size_t offset)
+{
+ return fifo_peek_common(f, buf, &nb_elems, offset, NULL, NULL);
+}
+
+int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque,
+ size_t *nb_elems, size_t offset)
+{
+ return fifo_peek_common(f, NULL, nb_elems, offset, write_cb, opaque);
+}
+
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
{
FifoBuffer *fb = (FifoBuffer*)f;
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index ac1245ff39..c7be5e8f7d 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -189,6 +189,66 @@ int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems);
int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
void *opaque, size_t *nb_elems);
+/**
+ * Read data from a FIFO.
+ *
+ * @param f the FIFO buffer
+ * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
+ * will be written into buf.
+ * @param nb_elems number of elements to read from FIFO
+ *
+ * @return a non-negative number on success, a negative error code on failure
+ */
+int av_fifo_read(AVFifoBuffer *f, void *buf, size_t nb_elems);
+
+/**
+ * Feed data from a FIFO into a user-provided callback.
+ *
+ * @param f the FIFO buffer
+ * @param write_cb Callback the data will be supplied to. May be called
+ * multiple times.
+ * @param opaque opaque user data to be provided to write_cb
+ * @param nb_elems Should point to the maximum number of elements that can be
+ * read. Will be updated to contain the total number of elements
+ * actually sent to the callback.
+ *
+ * @return non-negative number on success, a negative error code on failure
+ */
+int av_fifo_read_to_cb(AVFifoBuffer *f, AVFifoCB write_cb,
+ void *opaque, size_t *nb_elems);
+
+/**
+ * Read data from a FIFO without modifying FIFO state.
+ *
+ * @param f the FIFO buffer
+ * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
+ * will be written into buf.
+ * @param nb_elems number of elements to read from FIFO
+ * @param offset number of initial elements to skip; offset + nb_elems must not
+ * be larger than av_fifo_can_read(f).
+ *
+ * @return a non-negative number on success, a negative error code on failure
+ */
+int av_fifo_peek(AVFifoBuffer *f, void *buf, size_t nb_elems, size_t offset);
+
+/**
+ * Feed data from a FIFO into a user-provided callback.
+ *
+ * @param f the FIFO buffer
+ * @param write_cb Callback the data will be supplied to. May be called
+ * multiple times.
+ * @param opaque opaque user data to be provided to write_cb
+ * @param nb_elems Should point to the maximum number of elements that can be
+ * read. Will be updated to contain the total number of elements
+ * actually sent to the callback.
+ * @param offset number of initial elements to skip; offset + *nb_elems must not
+ * be larger than av_fifo_can_read(f).
+ *
+ * @return a non-negative number on success, a negative error code on failure
+ */
+int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque,
+ size_t *nb_elems, size_t offset);
+
/**
* Discard the specified amount of data from an AVFifoBuffer.
* @param size number of elements to discard, MUST NOT be larger than
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions Anton Khirnov
@ 2022-01-13 17:41 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 17:41 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> As for writing, use separate functions for reading to a buffer and a
> callback. Allow the callbacks to limit the amount of data read,
> similarly to what is done for writing.
>
> Consistently use size_t for sizes.
> ---
> doc/APIchanges | 3 ++-
> libavutil/fifo.c | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
> libavutil/fifo.h | 60 ++++++++++++++++++++++++++++++++++++++++++
> 3 files changed, 130 insertions(+), 1 deletion(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index 0b179c30e5..f64759d69d 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -19,7 +19,8 @@ API changes, most recent first:
> Operations on FIFOs created with this function on these elements
> rather than bytes.
> Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
> - av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb().
> + av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(),
> + av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb().
>
> 2022-01-xx - xxxxxxxxxx - lavu fifo.h
> Access to all AVFifoBuffer members is deprecated. The struct will
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index 1d94fff457..ea944bc936 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -265,6 +265,74 @@ int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
> return fifo_write_common(f, NULL, nb_elems, read_cb, opaque);
> }
>
> +static int fifo_peek_common(const AVFifoBuffer *f, uint8_t *buf, size_t *nb_elems,
> + size_t offset, AVFifoCB write_cb, void *opaque)
> +{
> + const FifoBuffer *fb = (FifoBuffer*)f;
> + size_t to_read = *nb_elems;
> + size_t offset_r = fb->offset_r;
> + int ret = 0;
> +
> + if (offset > av_fifo_can_read(f) ||
> + to_read > av_fifo_can_read(f) - offset) {
You are calling av_fifo_can_read() multiple times.
> + *nb_elems = 0;
> + return AVERROR(EINVAL);
> + }
> +
> + if (offset_r >= fb->nb_elems - offset)
> + offset_r -= fb->nb_elems - offset;
> + else
> + offset_r += offset;
> +
> + do {
Shouldn't this be a while loop?
> + size_t len = FFMIN(fb->nb_elems - offset_r, to_read);
> + uint8_t *rptr = f->buffer + offset_r * fb->elem_size;
> +
> + if (write_cb) {
> + ret = write_cb(opaque, rptr, &len);
> + if (ret < 0 || len == 0)
> + break;
> + } else {
> + memcpy(buf, rptr, len * fb->elem_size);
> + buf += len * fb->elem_size;
> + }
> + offset_r += len;
> + if (offset_r >= fb->nb_elems)
> + offset_r = 0;
> + to_read -= len;
> + } while (to_read > 0);
> +
> + *nb_elems -= to_read;
> +
> + return ret;
> +}
> +
> +int av_fifo_read(AVFifoBuffer *f, void *buf, size_t nb_elems)
> +{
> + int ret = fifo_peek_common(f, buf, &nb_elems, 0, NULL, NULL);
> + av_fifo_drain2(f, nb_elems);
In contrast to the current av_fifo_generic_read() the callback will
still see the non-drained FIFO if the callback is called multiple times.
I don't know whether this slight behaviour change can cause problems
when updating.
> + return ret;
> +}
> +
> +int av_fifo_read_to_cb(AVFifoBuffer *f, AVFifoCB write_cb,
> + void *opaque, size_t *nb_elems)
> +{
> + int ret = fifo_peek_common(f, NULL, nb_elems, 0, write_cb, opaque);
> + av_fifo_drain2(f, *nb_elems);
> + return ret;
> +}
> +
> +int av_fifo_peek(AVFifoBuffer *f, void *buf, size_t nb_elems, size_t offset)
> +{
> + return fifo_peek_common(f, buf, &nb_elems, offset, NULL, NULL);
> +}
> +
> +int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque,
> + size_t *nb_elems, size_t offset)
> +{
> + return fifo_peek_common(f, NULL, nb_elems, offset, write_cb, opaque);
> +}
> +
> int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
> {
> FifoBuffer *fb = (FifoBuffer*)f;
> diff --git a/libavutil/fifo.h b/libavutil/fifo.h
> index ac1245ff39..c7be5e8f7d 100644
> --- a/libavutil/fifo.h
> +++ b/libavutil/fifo.h
> @@ -189,6 +189,66 @@ int av_fifo_write(AVFifoBuffer *f, const void *buf, size_t nb_elems);
> int av_fifo_write_from_cb(AVFifoBuffer *f, AVFifoCB read_cb,
> void *opaque, size_t *nb_elems);
>
> +/**
> + * Read data from a FIFO.
> + *
> + * @param f the FIFO buffer
> + * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
> + * will be written into buf.
> + * @param nb_elems number of elements to read from FIFO
> + *
> + * @return a non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_read(AVFifoBuffer *f, void *buf, size_t nb_elems);
> +
> +/**
> + * Feed data from a FIFO into a user-provided callback.
> + *
> + * @param f the FIFO buffer
> + * @param write_cb Callback the data will be supplied to. May be called
> + * multiple times.
> + * @param opaque opaque user data to be provided to write_cb
> + * @param nb_elems Should point to the maximum number of elements that can be
> + * read. Will be updated to contain the total number of elements
> + * actually sent to the callback.
> + *
> + * @return non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_read_to_cb(AVFifoBuffer *f, AVFifoCB write_cb,
> + void *opaque, size_t *nb_elems);
> +
> +/**
> + * Read data from a FIFO without modifying FIFO state.
> + *
> + * @param f the FIFO buffer
> + * @param buf Buffer to store the data. nb_elems * av_fifo_elem_size(f) bytes
> + * will be written into buf.
> + * @param nb_elems number of elements to read from FIFO
> + * @param offset number of initial elements to skip; offset + nb_elems must not
> + * be larger than av_fifo_can_read(f).
> + *
> + * @return a non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_peek(AVFifoBuffer *f, void *buf, size_t nb_elems, size_t offset);
> +
> +/**
> + * Feed data from a FIFO into a user-provided callback.
> + *
> + * @param f the FIFO buffer
> + * @param write_cb Callback the data will be supplied to. May be called
> + * multiple times.
> + * @param opaque opaque user data to be provided to write_cb
> + * @param nb_elems Should point to the maximum number of elements that can be
> + * read. Will be updated to contain the total number of elements
> + * actually sent to the callback.
> + * @param offset number of initial elements to skip; offset + *nb_elems must not
> + * be larger than av_fifo_can_read(f).
> + *
> + * @return a non-negative number on success, a negative error code on failure
> + */
> +int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque,
Ok, seems like we differ on our naming: I'd call this a read_cb, because
it reads from the fifo, but it of course also writes, so it seems fine
given that you are consistent.
> + size_t *nb_elems, size_t offset);
> +
> /**
> * Discard the specified amount of data from an AVFifoBuffer.
> * @param size number of elements to discard, MUST NOT be larger than
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (6 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 17:53 ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 10/35] lavu/fifo: deprecate old API Anton Khirnov
` (27 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
This will not increase the FIFO beyond 1MB, unless the caller explicitly
specifies otherwise.
---
doc/APIchanges | 3 ++-
libavutil/fifo.c | 41 +++++++++++++++++++++++++++++++++++++++--
libavutil/fifo.h | 15 ++++++++++++++-
3 files changed, 55 insertions(+), 4 deletions(-)
diff --git a/doc/APIchanges b/doc/APIchanges
index f64759d69d..52b42762ea 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -20,7 +20,8 @@ API changes, most recent first:
rather than bytes.
Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(),
- av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb().
+ av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb(),
+ av_fifo_auto_grow_limit().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index ea944bc936..2c15df5d5c 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -34,6 +34,9 @@
# define CTX_STRUCT_NAME AVFifoBuffer
#endif
+// by default the FIFO can be auto-grown to 1MB
+#define AUTO_GROW_DEFAULT_BYTES (1024 * 1024)
+
typedef struct CTX_STRUCT_NAME {
// These fields must match then contents of AVFifoBuffer in fifo.h
// until FF_API_FIFO_PUBLIC is removed
@@ -48,6 +51,9 @@ typedef struct CTX_STRUCT_NAME {
size_t offset_r, offset_w;
// distinguishes the ambigous situation offset_r == offset_w
int is_empty;
+
+ unsigned int flags;
+ size_t auto_grow_limit;
} FifoBuffer;
AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
@@ -75,10 +81,19 @@ AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
f->nb_elems = nb_elems;
f->elem_size = elem_size;
+ f->flags = flags;
+ f->auto_grow_limit = FFMAX(AUTO_GROW_DEFAULT_BYTES / elem_size, 1);
+
av_fifo_reset((AVFifoBuffer*)f);
return (AVFifoBuffer*)f;
}
+void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ fb->auto_grow_limit = max_elems;
+}
+
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
{
if (nmemb > SIZE_MAX / size)
@@ -207,6 +222,27 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size)
#endif
}
+static int fifo_check_space(AVFifoBuffer *f, size_t to_write)
+{
+ FifoBuffer *fb = (FifoBuffer*)f;
+ const size_t can_write = av_fifo_can_write(f);
+ const size_t need_grow = to_write > can_write ? to_write - can_write : 0;
+ size_t can_grow;
+
+ if (!need_grow)
+ return 0;
+
+ can_grow = fb->auto_grow_limit > fb->nb_elems ?
+ fb->auto_grow_limit - fb->nb_elems : 0;
+ if ((fb->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) {
+ // allocate a bit more than necessary, if we can
+ const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow;
+ return av_fifo_grow2(f, inc);
+ }
+
+ return AVERROR(ENOSPC);
+}
+
static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_elems,
AVFifoCB read_cb, void *opaque)
{
@@ -218,8 +254,9 @@ static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_ele
uint32_t wndx= f->wndx;
#endif
- if (to_write > av_fifo_can_write(f))
- return AVERROR(ENOSPC);
+ ret = fifo_check_space(f, to_write);
+ if (ret < 0)
+ return ret;
do {
size_t len = FFMIN(fb->nb_elems - offset_w, to_write);
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index c7be5e8f7d..11eb36944a 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -83,6 +83,13 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size);
*/
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
+/**
+ * Automatically resize the FIFO on writes, so that the data fits. This
+ * automatic resizing happens up to a limit that can be modified with
+ * av_fifo_auto_grow_limit().
+ */
+#define AV_FIFO_FLAG_AUTO_GROW (1 << 0)
+
/**
* Allocate and initialize an AVFifoBuffer with a given element size.
*
@@ -90,7 +97,7 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
* @param nb_elems initial number of elements that can be stored in the FIFO
* @param elem_size Size in bytes of a single element. Further operations on
* the returned FIFO will implicitly use this element size.
- * @param flags currently unused, must be 0
+ * @param flags a combination of AV_FIFO_FLAG_*
*
* @return newly-allocated AVFifoBuffer on success, a negative error code on failure
*/
@@ -115,6 +122,12 @@ void av_fifo_freep(AVFifoBuffer **f);
*/
size_t av_fifo_elem_size(const AVFifoBuffer *f);
+/**
+ * Set the maximum size (in elements) to which the FIFO can be resized
+ * automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.
+ */
+void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems);
+
/**
* Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
* @param f AVFifoBuffer to reset
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed Anton Khirnov
@ 2022-01-13 17:53 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 17:53 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> This will not increase the FIFO beyond 1MB, unless the caller explicitly
> specifies otherwise.
> ---
> doc/APIchanges | 3 ++-
> libavutil/fifo.c | 41 +++++++++++++++++++++++++++++++++++++++--
> libavutil/fifo.h | 15 ++++++++++++++-
> 3 files changed, 55 insertions(+), 4 deletions(-)
>
> diff --git a/doc/APIchanges b/doc/APIchanges
> index f64759d69d..52b42762ea 100644
> --- a/doc/APIchanges
> +++ b/doc/APIchanges
> @@ -20,7 +20,8 @@ API changes, most recent first:
> rather than bytes.
> Add av_fifo_elem_size(), av_fifo_can_read(), av_fifo_can_write(),
> av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(),
> - av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb().
> + av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb(),
> + av_fifo_auto_grow_limit().
>
> 2022-01-xx - xxxxxxxxxx - lavu fifo.h
> Access to all AVFifoBuffer members is deprecated. The struct will
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index ea944bc936..2c15df5d5c 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -34,6 +34,9 @@
> # define CTX_STRUCT_NAME AVFifoBuffer
> #endif
>
> +// by default the FIFO can be auto-grown to 1MB
> +#define AUTO_GROW_DEFAULT_BYTES (1024 * 1024)
> +
> typedef struct CTX_STRUCT_NAME {
> // These fields must match then contents of AVFifoBuffer in fifo.h
> // until FF_API_FIFO_PUBLIC is removed
> @@ -48,6 +51,9 @@ typedef struct CTX_STRUCT_NAME {
> size_t offset_r, offset_w;
> // distinguishes the ambigous situation offset_r == offset_w
> int is_empty;
> +
> + unsigned int flags;
> + size_t auto_grow_limit;
> } FifoBuffer;
>
> AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
> @@ -75,10 +81,19 @@ AVFifoBuffer *av_fifo_alloc2(size_t nb_elems, size_t elem_size,
> f->nb_elems = nb_elems;
> f->elem_size = elem_size;
>
> + f->flags = flags;
> + f->auto_grow_limit = FFMAX(AUTO_GROW_DEFAULT_BYTES / elem_size, 1);
> +
> av_fifo_reset((AVFifoBuffer*)f);
> return (AVFifoBuffer*)f;
> }
>
> +void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems)
> +{
> + FifoBuffer *fb = (FifoBuffer*)f;
> + fb->auto_grow_limit = max_elems;
> +}
> +
> AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> {
> if (nmemb > SIZE_MAX / size)
> @@ -207,6 +222,27 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size)
> #endif
> }
>
> +static int fifo_check_space(AVFifoBuffer *f, size_t to_write)
> +{
> + FifoBuffer *fb = (FifoBuffer*)f;
> + const size_t can_write = av_fifo_can_write(f);
> + const size_t need_grow = to_write > can_write ? to_write - can_write : 0;
> + size_t can_grow;
> +
> + if (!need_grow)
> + return 0;
> +
> + can_grow = fb->auto_grow_limit > fb->nb_elems ?
> + fb->auto_grow_limit - fb->nb_elems : 0;
> + if ((fb->flags & AV_FIFO_FLAG_AUTO_GROW) && need_grow <= can_grow) {
> + // allocate a bit more than necessary, if we can
> + const size_t inc = (need_grow < can_grow / 2 ) ? need_grow * 2 : can_grow;
This is still linear if one always writes a fixed amount of elements.
Using av_fast_realloc_array() would actually fit very well in here.
Shall I resend a size_t version?
> + return av_fifo_grow2(f, inc);
> + }
> +
> + return AVERROR(ENOSPC);
> +}
> +
> static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_elems,
> AVFifoCB read_cb, void *opaque)
> {
> @@ -218,8 +254,9 @@ static int fifo_write_common(AVFifoBuffer *f, const uint8_t *buf, size_t *nb_ele
> uint32_t wndx= f->wndx;
> #endif
>
> - if (to_write > av_fifo_can_write(f))
> - return AVERROR(ENOSPC);
> + ret = fifo_check_space(f, to_write);
> + if (ret < 0)
> + return ret;
>
> do {
> size_t len = FFMIN(fb->nb_elems - offset_w, to_write);
> diff --git a/libavutil/fifo.h b/libavutil/fifo.h
> index c7be5e8f7d..11eb36944a 100644
> --- a/libavutil/fifo.h
> +++ b/libavutil/fifo.h
> @@ -83,6 +83,13 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size);
> */
> AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
>
> +/**
> + * Automatically resize the FIFO on writes, so that the data fits. This
> + * automatic resizing happens up to a limit that can be modified with
> + * av_fifo_auto_grow_limit().
> + */
> +#define AV_FIFO_FLAG_AUTO_GROW (1 << 0)
> +
> /**
> * Allocate and initialize an AVFifoBuffer with a given element size.
> *
> @@ -90,7 +97,7 @@ AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
> * @param nb_elems initial number of elements that can be stored in the FIFO
> * @param elem_size Size in bytes of a single element. Further operations on
> * the returned FIFO will implicitly use this element size.
> - * @param flags currently unused, must be 0
> + * @param flags a combination of AV_FIFO_FLAG_*
> *
> * @return newly-allocated AVFifoBuffer on success, a negative error code on failure
> */
> @@ -115,6 +122,12 @@ void av_fifo_freep(AVFifoBuffer **f);
> */
> size_t av_fifo_elem_size(const AVFifoBuffer *f);
>
> +/**
> + * Set the maximum size (in elements) to which the FIFO can be resized
> + * automatically. Has no effect unless AV_FIFO_FLAG_AUTO_GROW is used.
> + */
> +void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems);
> +
> /**
> * Reset the AVFifoBuffer to the state right after av_fifo_alloc, in particular it is emptied.
> * @param f AVFifoBuffer to reset
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 10/35] lavu/fifo: deprecate old API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (7 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 11/35] lavu/tests/fifo: switch to the new API Anton Khirnov
` (26 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
doc/APIchanges | 3 +++
libavutil/fifo.c | 10 ++++++++++
libavutil/fifo.h | 37 +++++++++++++++++++++++++++++++++++++
libavutil/version.h | 1 +
4 files changed, 51 insertions(+)
diff --git a/doc/APIchanges b/doc/APIchanges
index 52b42762ea..3531596a7f 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -22,6 +22,9 @@ API changes, most recent first:
av_fifo_grow2(), av_fifo_drain2(), av_fifo_write(), av_fifo_write_from_cb(),
av_fifo_read(), av_fifo_read_to_cb(), av_fifo_peek(), av_fifo_peek_to_cb(),
av_fifo_auto_grow_limit().
+ Deprecate av_fifo_alloc(), av_fifo_alloc_array(), av_fifo_size(), av_fifo_space(),
+ av_fifo_generic_peek(), av_fifo_generic_peek_at(), av_fifo_generic_read(),
+ av_fifo_generic_write(), av_fifo_realloc2(), av_fifo_grow(), av_fifo_drain().
2022-01-xx - xxxxxxxxxx - lavu fifo.h
Access to all AVFifoBuffer members is deprecated. The struct will
diff --git a/libavutil/fifo.c b/libavutil/fifo.c
index 2c15df5d5c..bd227d23e8 100644
--- a/libavutil/fifo.c
+++ b/libavutil/fifo.c
@@ -94,6 +94,8 @@ void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems)
fb->auto_grow_limit = max_elems;
}
+#if FF_API_FIFO_OLD_API
+FF_DISABLE_DEPRECATION_WARNINGS
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
{
if (nmemb > SIZE_MAX / size)
@@ -105,6 +107,8 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size)
{
return av_fifo_alloc_array(size, 1);
}
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
void av_fifo_free(AVFifoBuffer *f)
{
@@ -155,6 +159,7 @@ size_t av_fifo_can_write(const AVFifoBuffer *f)
return fb->nb_elems - av_fifo_can_read(f);
}
+#if FF_API_FIFO_OLD_API
int av_fifo_size(const AVFifoBuffer *f)
{
return av_fifo_can_read(f);
@@ -164,6 +169,7 @@ int av_fifo_space(const AVFifoBuffer *f)
{
return av_fifo_can_write(f);
}
+#endif
int av_fifo_grow2(AVFifoBuffer *f, size_t inc)
{
@@ -370,6 +376,8 @@ int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque,
return fifo_peek_common(f, NULL, nb_elems, offset, write_cb, opaque);
}
+#if FF_API_FIFO_OLD_API
+FF_DISABLE_DEPRECATION_WARNINGS
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
{
FifoBuffer *fb = (FifoBuffer*)f;
@@ -528,3 +536,5 @@ void av_fifo_drain(AVFifoBuffer *f, int size)
{
return av_fifo_drain2(f, size);
}
+FF_ENABLE_DEPRECATION_WARNINGS
+#endif
diff --git a/libavutil/fifo.h b/libavutil/fifo.h
index 11eb36944a..c2e11e11ab 100644
--- a/libavutil/fifo.h
+++ b/libavutil/fifo.h
@@ -62,6 +62,7 @@ AVFifoBuffer;
*/
typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);
+#if FF_API_FIFO_OLD_API
/**
* Initialize an AVFifoBuffer.
* @param size of FIFO
@@ -69,7 +70,9 @@ typedef int AVFifoCB(void *opaque, void *buf, size_t *nb_elems);
*
* @note the element size of the allocated FIFO will be 1, i.e. all operations
* will be in bytes
+ * @deprecated use av_fifo_alloc2
*/
+attribute_deprecated
AVFifoBuffer *av_fifo_alloc(unsigned int size);
/**
@@ -80,8 +83,11 @@ AVFifoBuffer *av_fifo_alloc(unsigned int size);
*
* @note the element size of the allocated FIFO will be 1, i.e. all operations
* will be in bytes
+ * @deprecated use av_fifo_alloc2
*/
+attribute_deprecated
AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size);
+#endif
/**
* Automatically resize the FIFO on writes, so that the data fits. This
@@ -134,12 +140,15 @@ void av_fifo_auto_grow_limit(AVFifoBuffer *f, size_t max_elems);
*/
void av_fifo_reset(AVFifoBuffer *f);
+#if FF_API_FIFO_OLD_API
/**
* Return the amount of data in the AVFifoBuffer, that is the amount of data
* (in elements, as returned by av_fifo_elem_size()) you can read from it.
* @param f AVFifoBuffer to read from
* @return size
+ * @deprecated use av_fifo_can_read()
*/
+attribute_deprecated
int av_fifo_size(const AVFifoBuffer *f);
/**
@@ -147,8 +156,11 @@ int av_fifo_size(const AVFifoBuffer *f);
* (in elements, as returned by av_fifo_elem_size()) you can write into it.
* @param f AVFifoBuffer to write into
* @return size
+ * @deprecated use av_fifo_can_write()
*/
+attribute_deprecated
int av_fifo_space(const AVFifoBuffer *f);
+#endif
/**
* @return number of elements available for reading from the given FIFO.
@@ -269,6 +281,7 @@ int av_fifo_peek_to_cb(AVFifoBuffer *f, AVFifoCB write_cb, void *opaque,
*/
void av_fifo_drain2(AVFifoBuffer *f, size_t size);
+#if FF_API_FIFO_OLD_API
/**
* Feed data at specific position from an AVFifoBuffer to a user-supplied callback.
* Similar as av_fifo_gereric_read but without discarding data.
@@ -279,7 +292,10 @@ void av_fifo_drain2(AVFifoBuffer *f, size_t size);
* @param dest data destination
*
* @return a non-negative number on success, a negative error code on failure
+ *
+ * @deprecated use av_fifo_peek() when func==NULL, av_fifo_peek_to_cb() otherwise
*/
+attribute_deprecated
int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_size, void (*func)(void*, void*, int));
/**
@@ -291,7 +307,10 @@ int av_fifo_generic_peek_at(AVFifoBuffer *f, void *dest, int offset, int buf_siz
* @param dest data destination
*
* @return a non-negative number on success, a negative error code on failure
+ *
+ * @deprecated use av_fifo_peek() when func==NULL, av_fifo_peek_to_cb() otherwise
*/
+attribute_deprecated
int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
/**
@@ -302,7 +321,10 @@ int av_fifo_generic_peek(AVFifoBuffer *f, void *dest, int buf_size, void (*func)
* @param dest data destination
*
* @return a non-negative number on success, a negative error code on failure
+ *
+ * @deprecated use av_fifo_read() when func==NULL, av_fifo_read_to_cb() otherwise
*/
+attribute_deprecated
int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)(void*, void*, int));
/**
@@ -317,7 +339,10 @@ int av_fifo_generic_read(AVFifoBuffer *f, void *dest, int buf_size, void (*func)
* indicate no more data available to write.
* If func is NULL, src is interpreted as a simple byte array for source data.
* @return the number of elements written to the FIFO or a negative error code on failure
+ *
+ * @deprecated use av_fifo_write() when func==NULL, av_fifo_write_to_cb() otherwise
*/
+attribute_deprecated
int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void*, void*, int));
/**
@@ -327,7 +352,11 @@ int av_fifo_generic_write(AVFifoBuffer *f, void *src, int size, int (*func)(void
* @param f AVFifoBuffer to resize
* @param size new AVFifoBuffer size in elements
* @return <0 for failure, >=0 otherwise
+ *
+ * @deprecated use av_fifo_grow2() to increase fifo size, decreasing fifo size
+ * is not supported
*/
+attribute_deprecated
int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
/**
@@ -339,15 +368,23 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int size);
* @param additional_space the amount of space in elements to allocate in
* addition to av_fifo_size()
* @return <0 for failure, >=0 otherwise
+ *
+ * @deprecated use av_fifo_grow2(); note that unlike this function it adds to
+ * allocated size, rather than to used size
*/
+attribute_deprecated
int av_fifo_grow(AVFifoBuffer *f, unsigned int additional_space);
/**
* Read and discard the specified amount of data from an AVFifoBuffer.
* @param f AVFifoBuffer to read from
* @param size amount of data to read in elements
+ *
+ * @deprecated use av_fifo_drain2()
*/
+attribute_deprecated
void av_fifo_drain(AVFifoBuffer *f, int size);
+#endif
#if FF_API_FIFO_PEEK2
/**
diff --git a/libavutil/version.h b/libavutil/version.h
index 180ebd5223..756318f495 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -111,6 +111,7 @@
#define FF_API_AV_MALLOCZ_ARRAY (LIBAVUTIL_VERSION_MAJOR < 58)
#define FF_API_FIFO_PEEK2 (LIBAVUTIL_VERSION_MAJOR < 58)
#define FF_API_FIFO_PUBLIC (LIBAVUTIL_VERSION_MAJOR < 58)
+#define FF_API_FIFO_OLD_API (LIBAVUTIL_VERSION_MAJOR < 58)
/**
* @}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 11/35] lavu/tests/fifo: switch to the new API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (8 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 10/35] lavu/fifo: deprecate old API Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API Anton Khirnov
` (25 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavutil/tests/fifo.c | 45 +++++++++++++++++++-----------------------
1 file changed, 20 insertions(+), 25 deletions(-)
diff --git a/libavutil/tests/fifo.c b/libavutil/tests/fifo.c
index e5aa88d252..952d0a5b2c 100644
--- a/libavutil/tests/fifo.c
+++ b/libavutil/tests/fifo.c
@@ -23,74 +23,69 @@
int main(void)
{
/* create a FIFO buffer */
- AVFifoBuffer *fifo = av_fifo_alloc(13 * sizeof(int));
+ AVFifoBuffer *fifo = av_fifo_alloc2(13, sizeof(int), 0);
int i, j, n, *p;
/* fill data */
- for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
- av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
+ for (i = 0; av_fifo_can_write(fifo); i++)
+ av_fifo_write(fifo, &i, 1);
/* peek_at at FIFO */
- n = av_fifo_size(fifo) / sizeof(int);
+ n = av_fifo_can_read(fifo);
for (i = 0; i < n; i++) {
- av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL);
+ av_fifo_peek(fifo, &j, 1, i);
printf("%d: %d\n", i, j);
}
printf("\n");
/* generic peek at FIFO */
- n = av_fifo_size(fifo);
- p = malloc(n);
+ n = av_fifo_can_read(fifo);
+ p = malloc(n * av_fifo_elem_size(fifo));
if (p == NULL) {
fprintf(stderr, "failed to allocate memory.\n");
exit(1);
}
- (void) av_fifo_generic_peek(fifo, p, n, NULL);
+ (void) av_fifo_peek(fifo, p, n, 0);
/* read data at p */
- n /= sizeof(int);
for(i = 0; i < n; ++i)
printf("%d: %d\n", i, p[i]);
putchar('\n');
/* read data */
- for (i = 0; av_fifo_size(fifo) >= sizeof(int); i++) {
- av_fifo_generic_read(fifo, &j, sizeof(int), NULL);
+ for (i = 0; av_fifo_can_read(fifo); i++) {
+ av_fifo_read(fifo, &j, 1);
printf("%d ", j);
}
printf("\n");
- /* test *ndx overflow */
- av_fifo_reset(fifo);
- fifo->rndx = fifo->wndx = ~(uint32_t)0 - 5;
-
/* fill data */
- for (i = 0; av_fifo_space(fifo) >= sizeof(int); i++)
- av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
+ for (i = 0; av_fifo_can_write(fifo); i++)
+ av_fifo_write(fifo, &i, 1);
/* peek_at at FIFO */
- n = av_fifo_size(fifo) / sizeof(int);
+ n = av_fifo_can_read(fifo);
for (i = 0; i < n; i++) {
- av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL);
+ av_fifo_peek(fifo, &j, 1, i);
printf("%d: %d\n", i, j);
}
putchar('\n');
/* test fifo_grow */
- (void) av_fifo_grow(fifo, 15 * sizeof(int));
+ (void) av_fifo_grow2(fifo, 15);
/* fill data */
- n = av_fifo_size(fifo) / sizeof(int);
- for (i = n; av_fifo_space(fifo) >= sizeof(int); ++i)
- av_fifo_generic_write(fifo, &i, sizeof(int), NULL);
+ n = av_fifo_can_read(fifo);
+ for (i = n; av_fifo_can_write(fifo); ++i)
+ av_fifo_write(fifo, &i, 1);
/* peek_at at FIFO */
- n = av_fifo_size(fifo) / sizeof(int);
+ n = av_fifo_can_read(fifo);
for (i = 0; i < n; i++) {
- av_fifo_generic_peek_at(fifo, &j, i * sizeof(int), sizeof(j), NULL);
+ av_fifo_peek(fifo, &j, 1, i);
printf("%d: %d\n", i, j);
}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (9 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 11/35] lavu/tests/fifo: switch to the new API Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-13 18:21 ` Andreas Rheinhardt
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: " Anton Khirnov
` (24 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/avcodec.c | 15 ++++++---------
libavcodec/decode.c | 22 ++++++++--------------
2 files changed, 14 insertions(+), 23 deletions(-)
diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
index c00a9b2af8..a75bbe721f 100644
--- a/libavcodec/avcodec.c
+++ b/libavcodec/avcodec.c
@@ -183,7 +183,8 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
avci->es.in_frame = av_frame_alloc();
avci->in_pkt = av_packet_alloc();
avci->last_pkt_props = av_packet_alloc();
- avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
+ avci->pkt_props = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
+ AV_FIFO_FLAG_AUTO_GROW);
if (!avci->buffer_frame || !avci->buffer_pkt ||
!avci->es.in_frame || !avci->in_pkt ||
!avci->last_pkt_props || !avci->pkt_props) {
@@ -399,13 +400,10 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
av_packet_unref(avci->buffer_pkt);
av_packet_unref(avci->last_pkt_props);
- while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
- av_fifo_generic_read(avci->pkt_props,
- avci->last_pkt_props, sizeof(*avci->last_pkt_props),
- NULL);
+ while (av_fifo_can_read(avci->pkt_props)) {
+ av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
av_packet_unref(avci->last_pkt_props);
}
- av_fifo_reset(avci->pkt_props);
av_frame_unref(avci->es.in_frame);
av_packet_unref(avci->in_pkt);
@@ -464,10 +462,9 @@ av_cold int avcodec_close(AVCodecContext *avctx)
av_frame_free(&avci->buffer_frame);
av_packet_free(&avci->buffer_pkt);
if (avci->pkt_props) {
- while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
+ while (av_fifo_can_read(avci->pkt_props)) {
av_packet_unref(avci->last_pkt_props);
- av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
- sizeof(*avci->last_pkt_props), NULL);
+ av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
}
av_fifo_freep(&avci->pkt_props);
}
diff --git a/libavcodec/decode.c b/libavcodec/decode.c
index 0912f86a14..9f6f2e7fa6 100644
--- a/libavcodec/decode.c
+++ b/libavcodec/decode.c
@@ -165,26 +165,21 @@ static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
int ret = 0;
if (IS_EMPTY(avci->last_pkt_props)) {
- if (av_fifo_size(avci->pkt_props) >= sizeof(*pkt)) {
- av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
- sizeof(*avci->last_pkt_props), NULL);
+ if (av_fifo_can_read(avci->pkt_props)) {
+ av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
} else
return copy_packet_props(avci->last_pkt_props, pkt);
}
- if (av_fifo_space(avci->pkt_props) < sizeof(*pkt)) {
- ret = av_fifo_grow(avci->pkt_props, sizeof(*pkt));
- if (ret < 0)
- return ret;
- }
-
ret = copy_packet_props(&tmp, pkt);
if (ret < 0)
return ret;
- av_fifo_generic_write(avci->pkt_props, &tmp, sizeof(tmp), NULL);
+ ret = av_fifo_write(avci->pkt_props, &tmp, 1);
+ if (ret < 0)
+ av_packet_unref(&tmp);
- return 0;
+ return ret;
}
static int decode_bsfs_init(AVCodecContext *avctx)
@@ -543,9 +538,8 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
avci->draining_done = 1;
if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS) &&
- IS_EMPTY(avci->last_pkt_props) && av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props))
- av_fifo_generic_read(avci->pkt_props,
- avci->last_pkt_props, sizeof(*avci->last_pkt_props), NULL);
+ IS_EMPTY(avci->last_pkt_props) && av_fifo_can_read(avci->pkt_props))
+ av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
if (!ret) {
frame->best_effort_timestamp = guess_correct_pts(avctx,
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API Anton Khirnov
@ 2022-01-13 18:21 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 18:21 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> ---
> libavcodec/avcodec.c | 15 ++++++---------
> libavcodec/decode.c | 22 ++++++++--------------
> 2 files changed, 14 insertions(+), 23 deletions(-)
>
> diff --git a/libavcodec/avcodec.c b/libavcodec/avcodec.c
> index c00a9b2af8..a75bbe721f 100644
> --- a/libavcodec/avcodec.c
> +++ b/libavcodec/avcodec.c
> @@ -183,7 +183,8 @@ int attribute_align_arg avcodec_open2(AVCodecContext *avctx, const AVCodec *code
> avci->es.in_frame = av_frame_alloc();
> avci->in_pkt = av_packet_alloc();
> avci->last_pkt_props = av_packet_alloc();
> - avci->pkt_props = av_fifo_alloc(sizeof(*avci->last_pkt_props));
> + avci->pkt_props = av_fifo_alloc2(1, sizeof(*avci->last_pkt_props),
> + AV_FIFO_FLAG_AUTO_GROW);
> if (!avci->buffer_frame || !avci->buffer_pkt ||
> !avci->es.in_frame || !avci->in_pkt ||
> !avci->last_pkt_props || !avci->pkt_props) {
> @@ -399,13 +400,10 @@ void avcodec_flush_buffers(AVCodecContext *avctx)
> av_packet_unref(avci->buffer_pkt);
>
> av_packet_unref(avci->last_pkt_props);
> - while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
> - av_fifo_generic_read(avci->pkt_props,
> - avci->last_pkt_props, sizeof(*avci->last_pkt_props),
> - NULL);
> + while (av_fifo_can_read(avci->pkt_props)) {
> + av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
> av_packet_unref(avci->last_pkt_props);
Could be simplified to
do {
av_packet_unref(avci->last_pkt_props);
} while (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) >= 0);
The same goes for other loop below and probably more loops in the next
patches.
(This of course presumes that one is allowed to call av_fifo_read() to
tentatively read more elements than are currently available. I think
this should be allowed.)
> }
> - av_fifo_reset(avci->pkt_props);
>
> av_frame_unref(avci->es.in_frame);
> av_packet_unref(avci->in_pkt);
> @@ -464,10 +462,9 @@ av_cold int avcodec_close(AVCodecContext *avctx)
> av_frame_free(&avci->buffer_frame);
> av_packet_free(&avci->buffer_pkt);
> if (avci->pkt_props) {
> - while (av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props)) {
> + while (av_fifo_can_read(avci->pkt_props)) {
> av_packet_unref(avci->last_pkt_props);
> - av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
> - sizeof(*avci->last_pkt_props), NULL);
> + av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
> }
> av_fifo_freep(&avci->pkt_props);
> }
> diff --git a/libavcodec/decode.c b/libavcodec/decode.c
> index 0912f86a14..9f6f2e7fa6 100644
> --- a/libavcodec/decode.c
> +++ b/libavcodec/decode.c
> @@ -165,26 +165,21 @@ static int extract_packet_props(AVCodecInternal *avci, const AVPacket *pkt)
> int ret = 0;
>
> if (IS_EMPTY(avci->last_pkt_props)) {
> - if (av_fifo_size(avci->pkt_props) >= sizeof(*pkt)) {
> - av_fifo_generic_read(avci->pkt_props, avci->last_pkt_props,
> - sizeof(*avci->last_pkt_props), NULL);
> + if (av_fifo_can_read(avci->pkt_props)) {
> + av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
> } else
if (av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1) < 0)
> return copy_packet_props(avci->last_pkt_props, pkt);
> }
>
> - if (av_fifo_space(avci->pkt_props) < sizeof(*pkt)) {
> - ret = av_fifo_grow(avci->pkt_props, sizeof(*pkt));
> - if (ret < 0)
> - return ret;
> - }
> -
> ret = copy_packet_props(&tmp, pkt);
> if (ret < 0)
> return ret;
>
> - av_fifo_generic_write(avci->pkt_props, &tmp, sizeof(tmp), NULL);
> + ret = av_fifo_write(avci->pkt_props, &tmp, 1);
> + if (ret < 0)
> + av_packet_unref(&tmp);
>
> - return 0;
> + return ret;
> }
>
> static int decode_bsfs_init(AVCodecContext *avctx)
> @@ -543,9 +538,8 @@ static int decode_receive_frame_internal(AVCodecContext *avctx, AVFrame *frame)
> avci->draining_done = 1;
>
> if (!(avctx->codec->caps_internal & FF_CODEC_CAP_SETS_FRAME_PROPS) &&
> - IS_EMPTY(avci->last_pkt_props) && av_fifo_size(avci->pkt_props) >= sizeof(*avci->last_pkt_props))
> - av_fifo_generic_read(avci->pkt_props,
> - avci->last_pkt_props, sizeof(*avci->last_pkt_props), NULL);
> + IS_EMPTY(avci->last_pkt_props) && av_fifo_can_read(avci->pkt_props))
The av_fifo_can_read() check could be dropped.
> + av_fifo_read(avci->pkt_props, avci->last_pkt_props, 1);
>
> if (!ret) {
> frame->best_effort_timestamp = guess_correct_pts(avctx,
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (10 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-12 14:46 ` Michael Niedermayer
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 14/35] lavc/cuviddec: do not reallocate the fifo unnecessarily Anton Khirnov
` (23 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
fftools/ffmpeg_mux.c | 22 +++++++++++-----------
fftools/ffmpeg_opt.c | 2 --
libavcodec/amfenc.c | 39 +++++++++++++--------------------------
3 files changed, 24 insertions(+), 39 deletions(-)
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 52986b002a..e97d7d50c6 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -126,11 +126,11 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
AVPacket *tmp_pkt;
int ret;
- if (!av_fifo_space(ms->muxing_queue)) {
- size_t cur_size = av_fifo_size(ms->muxing_queue);
+ if (!av_fifo_can_write(ms->muxing_queue)) {
+ size_t cur_size = av_fifo_can_read(ms->muxing_queue);
unsigned int are_we_over_size =
(ms->muxing_queue_data_size + pkt->size) > ost->muxing_queue_data_threshold;
- size_t limit = are_we_over_size ? ost->max_muxing_queue_size : INT_MAX;
+ size_t limit = are_we_over_size ? ost->max_muxing_queue_size : SIZE_MAX;
size_t new_size = FFMIN(2 * cur_size, limit);
if (new_size <= cur_size) {
@@ -139,7 +139,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
ost->file_index, ost->st->index);
return AVERROR(ENOSPC);
}
- ret = av_fifo_realloc2(ms->muxing_queue, new_size);
+ ret = av_fifo_grow2(ms->muxing_queue, new_size - cur_size);
if (ret < 0)
return ret;
}
@@ -154,7 +154,7 @@ static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
av_packet_move_ref(tmp_pkt, pkt);
ms->muxing_queue_data_size += tmp_pkt->size;
- av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), NULL);
+ av_fifo_write(ms->muxing_queue, &tmp_pkt, 1);
return 0;
}
@@ -369,12 +369,12 @@ int of_check_init(OutputFile *of)
OutputStream *ost = output_streams[of->ost_index + i];
/* try to improve muxing time_base (only possible if nothing has been written yet) */
- if (!av_fifo_size(ms->muxing_queue))
+ if (!av_fifo_can_read(ms->muxing_queue))
ost->mux_timebase = ost->st->time_base;
- while (av_fifo_size(ms->muxing_queue)) {
+ while (av_fifo_can_read(ms->muxing_queue)) {
AVPacket *pkt;
- av_fifo_generic_read(ms->muxing_queue, &pkt, sizeof(pkt), NULL);
+ av_fifo_read(ms->muxing_queue, &pkt, 1);
ms->muxing_queue_data_size -= pkt->size;
write_packet(of, ost, pkt);
av_packet_free(&pkt);
@@ -429,9 +429,9 @@ static void mux_free(Muxer **pmux, int nb_streams)
if (!ms->muxing_queue)
continue;
- while (av_fifo_size(ms->muxing_queue)) {
+ while (av_fifo_can_read(ms->muxing_queue)) {
AVPacket *pkt;
- av_fifo_generic_read(ms->muxing_queue, &pkt, sizeof(pkt), NULL);
+ av_fifo_read(ms->muxing_queue, &pkt, 1);
av_packet_free(&pkt);
}
av_fifo_freep(&ms->muxing_queue);
@@ -479,7 +479,7 @@ int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize)
for (int i = 0; i < of->ctx->nb_streams; i++) {
MuxStream *ms = &mux->streams[i];
- ms->muxing_queue = av_fifo_alloc(8 * sizeof(AVPacket*));
+ ms->muxing_queue = av_fifo_alloc2(8, sizeof(AVPacket*), 0);
if (!ms->muxing_queue) {
ret = AVERROR(ENOMEM);
goto fail;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index cb21a6a42c..4fede8471e 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1610,8 +1610,6 @@ static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
ost->max_muxing_queue_size = 128;
MATCH_PER_STREAM_OPT(max_muxing_queue_size, i, ost->max_muxing_queue_size, oc, st);
- ost->max_muxing_queue_size = FFMIN(ost->max_muxing_queue_size, INT_MAX / sizeof(ost->pkt));
- ost->max_muxing_queue_size *= sizeof(ost->pkt);
ost->muxing_queue_data_threshold = 50*1024*1024;
MATCH_PER_STREAM_OPT(muxing_queue_data_threshold, i, ost->muxing_queue_data_threshold, oc, st);
diff --git a/libavcodec/amfenc.c b/libavcodec/amfenc.c
index fb23ed738c..3a8dd2b007 100644
--- a/libavcodec/amfenc.c
+++ b/libavcodec/amfenc.c
@@ -117,8 +117,9 @@ static int amf_load_library(AVCodecContext *avctx)
if (!ctx->delayed_frame) {
return AVERROR(ENOMEM);
}
- // hardcoded to current HW queue size - will realloc in timestamp_queue_enqueue() if too small
- ctx->timestamp_list = av_fifo_alloc((avctx->max_b_frames + 16) * sizeof(int64_t));
+ // hardcoded to current HW queue size - will auto-realloc if too small
+ ctx->timestamp_list = av_fifo_alloc2(avctx->max_b_frames + 16, sizeof(int64_t),
+ AV_FIFO_FLAG_AUTO_GROW);
if (!ctx->timestamp_list) {
return AVERROR(ENOMEM);
}
@@ -432,18 +433,6 @@ static int amf_copy_surface(AVCodecContext *avctx, const AVFrame *frame,
return 0;
}
-static inline int timestamp_queue_enqueue(AVCodecContext *avctx, int64_t timestamp)
-{
- AmfContext *ctx = avctx->priv_data;
- if (av_fifo_space(ctx->timestamp_list) < sizeof(timestamp)) {
- if (av_fifo_grow(ctx->timestamp_list, sizeof(timestamp)) < 0) {
- return AVERROR(ENOMEM);
- }
- }
- av_fifo_generic_write(ctx->timestamp_list, ×tamp, sizeof(timestamp), NULL);
- return 0;
-}
-
static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buffer)
{
AmfContext *ctx = avctx->priv_data;
@@ -479,21 +468,19 @@ static int amf_copy_buffer(AVCodecContext *avctx, AVPacket *pkt, AMFBuffer *buff
pkt->pts = var.int64Value; // original pts
- AMF_RETURN_IF_FALSE(ctx, av_fifo_size(ctx->timestamp_list) > 0, AVERROR_UNKNOWN, "timestamp_list is empty\n");
+ AMF_RETURN_IF_FALSE(ctx, av_fifo_can_read(ctx->timestamp_list) > 0, AVERROR_UNKNOWN, "timestamp_list is empty\n");
- av_fifo_generic_read(ctx->timestamp_list, ×tamp, sizeof(timestamp), NULL);
+ av_fifo_read(ctx->timestamp_list, ×tamp, 1);
// calc dts shift if max_b_frames > 0
if (avctx->max_b_frames > 0 && ctx->dts_delay == 0) {
int64_t timestamp_last = AV_NOPTS_VALUE;
- AMF_RETURN_IF_FALSE(ctx, av_fifo_size(ctx->timestamp_list) > 0, AVERROR_UNKNOWN,
+ AMF_RETURN_IF_FALSE(ctx, av_fifo_can_read(ctx->timestamp_list) > 0, AVERROR_UNKNOWN,
"timestamp_list is empty while max_b_frames = %d\n", avctx->max_b_frames);
- av_fifo_generic_peek_at(
+ av_fifo_peek(
ctx->timestamp_list,
- ×tamp_last,
- (av_fifo_size(ctx->timestamp_list) / sizeof(timestamp) - 1) * sizeof(timestamp_last),
- sizeof(timestamp_last),
- NULL);
+ ×tamp_last, 1,
+ (av_fifo_can_read(ctx->timestamp_list) - 1));
if (timestamp < 0 || timestamp_last < AV_NOPTS_VALUE) {
return AVERROR(ERANGE);
}
@@ -710,9 +697,9 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "SubmitInput() failed with error %d\n", res);
av_frame_unref(frame);
- if ((ret = timestamp_queue_enqueue(avctx, pts)) < 0) {
+ ret = av_fifo_write(ctx->timestamp_list, &pts, 1);
+ if (ret < 0)
return ret;
- }
}
}
@@ -751,9 +738,9 @@ int ff_amf_receive_packet(AVCodecContext *avctx, AVPacket *avpkt)
av_frame_unref(ctx->delayed_frame);
AMF_RETURN_IF_FALSE(ctx, res == AMF_OK, AVERROR_UNKNOWN, "Repeated SubmitInput() failed with error %d\n", res);
- if ((ret = timestamp_queue_enqueue(avctx, pts)) < 0) {
+ ret = av_fifo_write(ctx->timestamp_list, &pts, 1);
+ if (ret < 0)
return ret;
- }
} else {
av_log(avctx, AV_LOG_WARNING, "Data acquired but delayed frame submission got AMF_INPUT_FULL- should not happen\n");
}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: switch to new FIFO API
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: " Anton Khirnov
@ 2022-01-12 14:46 ` Michael Niedermayer
2022-01-12 19:29 ` Anton Khirnov
0 siblings, 1 reply; 53+ messages in thread
From: Michael Niedermayer @ 2022-01-12 14:46 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1.1: Type: text/plain, Size: 549 bytes --]
On Tue, Jan 11, 2022 at 09:45:48PM +0100, Anton Khirnov wrote:
> ---
> fftools/ffmpeg_mux.c | 22 +++++++++++-----------
This doesnt seem to apply to git master without some other patches
thx
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Old school: Use the lowest level language in which you can solve the problem
conveniently.
New school: Use the highest level language in which the latest supercomputer
can solve the problem without the user falling asleep waiting.
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: switch to new FIFO API
2022-01-12 14:46 ` Michael Niedermayer
@ 2022-01-12 19:29 ` Anton Khirnov
2022-01-13 14:14 ` Michael Niedermayer
0 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-12 19:29 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Quoting Michael Niedermayer (2022-01-12 15:46:45)
> On Tue, Jan 11, 2022 at 09:45:48PM +0100, Anton Khirnov wrote:
> > ---
> > fftools/ffmpeg_mux.c | 22 +++++++++++-----------
>
> This doesnt seem to apply to git master without some other patches
sorry, should have mentioned this is on top of my ffmpeg.c patchset
You can look at the tree at
git://git.khirnov.net/libav
branch fifo
--
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: switch to new FIFO API
2022-01-12 19:29 ` Anton Khirnov
@ 2022-01-13 14:14 ` Michael Niedermayer
0 siblings, 0 replies; 53+ messages in thread
From: Michael Niedermayer @ 2022-01-13 14:14 UTC (permalink / raw)
To: FFmpeg development discussions and patches
[-- Attachment #1.1: Type: text/plain, Size: 744 bytes --]
On Wed, Jan 12, 2022 at 08:29:54PM +0100, Anton Khirnov wrote:
> Quoting Michael Niedermayer (2022-01-12 15:46:45)
> > On Tue, Jan 11, 2022 at 09:45:48PM +0100, Anton Khirnov wrote:
> > > ---
> > > fftools/ffmpeg_mux.c | 22 +++++++++++-----------
> >
> > This doesnt seem to apply to git master without some other patches
>
> sorry, should have mentioned this is on top of my ffmpeg.c patchset
>
> You can look at the tree at
> git://git.khirnov.net/libav
> branch fifo
thx
[...]
--
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
Take away the freedom of one citizen and you will be jailed, take away
the freedom of all citizens and you will be congratulated by your peers
in Parliament.
[-- Attachment #1.2: signature.asc --]
[-- Type: application/pgp-signature, Size: 195 bytes --]
[-- Attachment #2: Type: text/plain, Size: 251 bytes --]
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 14/35] lavc/cuviddec: do not reallocate the fifo unnecessarily
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (11 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 13/35] lavc/amfenc: " Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 15/35] lavc/cuviddec: convert to the new FIFO API Anton Khirnov
` (22 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/cuviddec.c | 8 +-------
1 file changed, 1 insertion(+), 7 deletions(-)
diff --git a/libavcodec/cuviddec.c b/libavcodec/cuviddec.c
index f03bbd8c4b..b1a3d674ab 100644
--- a/libavcodec/cuviddec.c
+++ b/libavcodec/cuviddec.c
@@ -1030,13 +1030,7 @@ static void cuvid_flush(AVCodecContext *avctx)
if (ret < 0)
goto error;
- av_fifo_freep(&ctx->frame_queue);
-
- ctx->frame_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(CuvidParsedFrame));
- if (!ctx->frame_queue) {
- av_log(avctx, AV_LOG_ERROR, "Failed to recreate frame queue on flush\n");
- return;
- }
+ av_fifo_reset(ctx->frame_queue);
if (ctx->cudecoder) {
ctx->cvdl->cuvidDestroyDecoder(ctx->cudecoder);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 15/35] lavc/cuviddec: convert to the new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (12 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 14/35] lavc/cuviddec: do not reallocate the fifo unnecessarily Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 16/35] lavc/libvorbisenc: switch to " Anton Khirnov
` (21 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/cuviddec.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/libavcodec/cuviddec.c b/libavcodec/cuviddec.c
index b1a3d674ab..5121fdb6a4 100644
--- a/libavcodec/cuviddec.c
+++ b/libavcodec/cuviddec.c
@@ -363,13 +363,13 @@ static int CUDAAPI cuvid_handle_picture_display(void *opaque, CUVIDPARSERDISPINF
parsed_frame.dispinfo.progressive_frame = ctx->progressive_sequence;
if (ctx->deint_mode_current == cudaVideoDeinterlaceMode_Weave) {
- av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
+ av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
} else {
parsed_frame.is_deinterlacing = 1;
- av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
+ av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
if (!ctx->drop_second_field) {
parsed_frame.second_field = 1;
- av_fifo_generic_write(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
+ av_fifo_write(ctx->frame_queue, &parsed_frame, 1);
}
}
@@ -384,7 +384,7 @@ static int cuvid_is_buffer_full(AVCodecContext *avctx)
if (ctx->deint_mode != cudaVideoDeinterlaceMode_Weave && !ctx->drop_second_field)
delay *= 2;
- return (av_fifo_size(ctx->frame_queue) / sizeof(CuvidParsedFrame)) + delay >= ctx->nb_surfaces;
+ return av_fifo_can_read(ctx->frame_queue) + delay >= ctx->nb_surfaces;
}
static int cuvid_decode_packet(AVCodecContext *avctx, const AVPacket *avpkt)
@@ -487,7 +487,7 @@ static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
if (ret < 0)
return ret;
- if (av_fifo_size(ctx->frame_queue)) {
+ if (av_fifo_can_read(ctx->frame_queue)) {
const AVPixFmtDescriptor *pixdesc;
CuvidParsedFrame parsed_frame;
CUVIDPROCPARAMS params;
@@ -495,7 +495,7 @@ static int cuvid_output_frame(AVCodecContext *avctx, AVFrame *frame)
int offset = 0;
int i;
- av_fifo_generic_read(ctx->frame_queue, &parsed_frame, sizeof(CuvidParsedFrame), NULL);
+ av_fifo_read(ctx->frame_queue, &parsed_frame, 1);
memset(¶ms, 0, sizeof(params));
params.progressive_frame = parsed_frame.dispinfo.progressive_frame;
@@ -834,7 +834,7 @@ static av_cold int cuvid_decode_init(AVCodecContext *avctx)
goto error;
}
- ctx->frame_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(CuvidParsedFrame));
+ ctx->frame_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(CuvidParsedFrame), 0);
if (!ctx->frame_queue) {
ret = AVERROR(ENOMEM);
goto error;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 16/35] lavc/libvorbisenc: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (13 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 15/35] lavc/cuviddec: convert to the new FIFO API Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 17/35] lavc/libvpxenc: switch to the " Anton Khirnov
` (20 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/libvorbisenc.c | 14 +++++++-------
1 file changed, 7 insertions(+), 7 deletions(-)
diff --git a/libavcodec/libvorbisenc.c b/libavcodec/libvorbisenc.c
index fa0d5f4b42..8db782b188 100644
--- a/libavcodec/libvorbisenc.c
+++ b/libavcodec/libvorbisenc.c
@@ -271,7 +271,7 @@ static av_cold int libvorbis_encode_init(AVCodecContext *avctx)
avctx->frame_size = LIBVORBIS_FRAME_SIZE;
ff_af_queue_init(avctx, &s->afq);
- s->pkt_fifo = av_fifo_alloc(BUFFER_SIZE);
+ s->pkt_fifo = av_fifo_alloc2(BUFFER_SIZE, 1, 0);
if (!s->pkt_fifo) {
ret = AVERROR(ENOMEM);
goto error;
@@ -327,12 +327,12 @@ static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
/* add any available packets to the output packet buffer */
while ((ret = vorbis_bitrate_flushpacket(&s->vd, &op)) == 1) {
- if (av_fifo_space(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
+ if (av_fifo_can_write(s->pkt_fifo) < sizeof(ogg_packet) + op.bytes) {
av_log(avctx, AV_LOG_ERROR, "packet buffer is too small\n");
return AVERROR_BUG;
}
- av_fifo_generic_write(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
- av_fifo_generic_write(s->pkt_fifo, op.packet, op.bytes, NULL);
+ av_fifo_write(s->pkt_fifo, &op, sizeof(ogg_packet));
+ av_fifo_write(s->pkt_fifo, op.packet, op.bytes);
}
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "error getting available packets\n");
@@ -345,14 +345,14 @@ static int libvorbis_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
}
/* check for available packets */
- if (av_fifo_size(s->pkt_fifo) < sizeof(ogg_packet))
+ if (av_fifo_can_read(s->pkt_fifo) < sizeof(ogg_packet))
return 0;
- av_fifo_generic_read(s->pkt_fifo, &op, sizeof(ogg_packet), NULL);
+ av_fifo_read(s->pkt_fifo, &op, sizeof(ogg_packet));
if ((ret = ff_get_encode_buffer(avctx, avpkt, op.bytes, 0)) < 0)
return ret;
- av_fifo_generic_read(s->pkt_fifo, avpkt->data, op.bytes, NULL);
+ av_fifo_read(s->pkt_fifo, avpkt->data, op.bytes);
avpkt->pts = ff_samples_to_time_base(avctx, op.granulepos);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 17/35] lavc/libvpxenc: switch to the new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (14 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 16/35] lavc/libvorbisenc: switch to " Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-12 18:15 ` James Zern
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 18/35] lavc/libvpxenc: remove unneeded context variable Anton Khirnov
` (19 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/libvpxenc.c | 28 ++++++++--------------------
1 file changed, 8 insertions(+), 20 deletions(-)
diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 10e5a22fa9..55f587c490 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -324,20 +324,11 @@ static av_cold void free_frame_list(struct FrameListData *list)
}
}
-static av_cold int add_hdr10_plus(AVFifoBuffer *fifo, struct FrameHDR10Plus *data)
-{
- int err = av_fifo_grow(fifo, sizeof(*data));
- if (err < 0)
- return err;
- av_fifo_generic_write(fifo, data, sizeof(*data), NULL);
- return 0;
-}
-
static av_cold void free_hdr10_plus_fifo(AVFifoBuffer **fifo)
{
FrameHDR10Plus frame_hdr10_plus;
- while (av_fifo_size(*fifo) >= sizeof(frame_hdr10_plus)) {
- av_fifo_generic_read(*fifo, &frame_hdr10_plus, sizeof(frame_hdr10_plus), NULL);
+ while (av_fifo_can_read(*fifo)) {
+ av_fifo_read(*fifo, &frame_hdr10_plus, 1);
av_buffer_unref(&frame_hdr10_plus.hdr10_plus);
}
av_fifo_freep(fifo);
@@ -347,16 +338,12 @@ static int copy_hdr10_plus_to_pkt(AVFifoBuffer *fifo, AVPacket *pkt)
{
FrameHDR10Plus frame_hdr10_plus;
uint8_t *data;
- if (!pkt)
- return 0;
- if (av_fifo_size(fifo) < sizeof(frame_hdr10_plus))
+ if (!pkt || !av_fifo_can_read(fifo))
return 0;
- av_fifo_generic_peek(fifo, &frame_hdr10_plus, sizeof(frame_hdr10_plus), NULL);
+ av_fifo_peek(fifo, &frame_hdr10_plus, 1, 0);
if (!frame_hdr10_plus.hdr10_plus || frame_hdr10_plus.pts != pkt->pts)
return 0;
- av_fifo_generic_read(fifo, &frame_hdr10_plus, sizeof(frame_hdr10_plus), NULL);
- if (!frame_hdr10_plus.hdr10_plus)
- return 0;
+ av_fifo_drain2(fifo, 1);
data = av_packet_new_side_data(pkt, AV_PKT_DATA_DYNAMIC_HDR10_PLUS, frame_hdr10_plus.hdr10_plus->size);
if (!data) {
@@ -933,7 +920,8 @@ static av_cold int vpx_init(AVCodecContext *avctx,
// it has PQ trc (SMPTE2084).
if (enccfg.g_bit_depth > 8 && avctx->color_trc == AVCOL_TRC_SMPTE2084) {
ctx->discard_hdr10_plus = 0;
- ctx->hdr10_plus_fifo = av_fifo_alloc(sizeof(FrameHDR10Plus));
+ ctx->hdr10_plus_fifo = av_fifo_alloc2(1, sizeof(FrameHDR10Plus),
+ AV_FIFO_FLAG_AUTO_GROW);
if (!ctx->hdr10_plus_fifo)
return AVERROR(ENOMEM);
}
@@ -1727,7 +1715,7 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
data.hdr10_plus = av_buffer_ref(hdr10_plus_metadata->buf);
if (!data.hdr10_plus)
return AVERROR(ENOMEM);
- err = add_hdr10_plus(ctx->hdr10_plus_fifo, &data);
+ err = av_fifo_write(ctx->hdr10_plus_fifo, &data, 1);
if (err < 0) {
av_buffer_unref(&data.hdr10_plus);
return err;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 18/35] lavc/libvpxenc: remove unneeded context variable
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (15 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 17/35] lavc/libvpxenc: switch to the " Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-12 18:15 ` James Zern
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 19/35] lavc/nvenc: switch to the new FIFO API Anton Khirnov
` (18 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
discard_hdr10_plus is 0 IFF hdr10_plus_fifo is non-NULL, so we can test
for the latter and avoid an extra variable.
---
libavcodec/libvpxenc.c | 7 ++-----
1 file changed, 2 insertions(+), 5 deletions(-)
diff --git a/libavcodec/libvpxenc.c b/libavcodec/libvpxenc.c
index 55f587c490..6bb19289ff 100644
--- a/libavcodec/libvpxenc.c
+++ b/libavcodec/libvpxenc.c
@@ -127,7 +127,6 @@ typedef struct VPxEncoderContext {
int tune_content;
int corpus_complexity;
int tpl_model;
- int discard_hdr10_plus;
AVFifoBuffer *hdr10_plus_fifo;
/**
* If the driver does not support ROI then warn the first time we
@@ -899,7 +898,6 @@ static av_cold int vpx_init(AVCodecContext *avctx,
#endif
AVDictionaryEntry* en = NULL;
- ctx->discard_hdr10_plus = 1;
av_log(avctx, AV_LOG_INFO, "%s\n", vpx_codec_version_str());
av_log(avctx, AV_LOG_VERBOSE, "%s\n", vpx_codec_build_config());
@@ -919,7 +917,6 @@ static av_cold int vpx_init(AVCodecContext *avctx,
// Keep HDR10+ if it has bit depth higher than 8 and
// it has PQ trc (SMPTE2084).
if (enccfg.g_bit_depth > 8 && avctx->color_trc == AVCOL_TRC_SMPTE2084) {
- ctx->discard_hdr10_plus = 0;
ctx->hdr10_plus_fifo = av_fifo_alloc2(1, sizeof(FrameHDR10Plus),
AV_FIFO_FLAG_AUTO_GROW);
if (!ctx->hdr10_plus_fifo)
@@ -1289,7 +1286,7 @@ static int storeframe(AVCodecContext *avctx, struct FrameListData *cx_frame,
}
if (cx_frame->frame_number != -1) {
VPxContext *ctx = avctx->priv_data;
- if (!ctx->discard_hdr10_plus) {
+ if (ctx->hdr10_plus_fifo) {
int err = copy_hdr10_plus_to_pkt(ctx->hdr10_plus_fifo, pkt);
if (err < 0)
return err;
@@ -1704,7 +1701,7 @@ static int vpx_encode(AVCodecContext *avctx, AVPacket *pkt,
}
}
- if (!ctx->discard_hdr10_plus) {
+ if (ctx->hdr10_plus_fifo) {
AVFrameSideData *hdr10_plus_metadata;
// Add HDR10+ metadata to queue.
hdr10_plus_metadata = av_frame_get_side_data(frame, AV_FRAME_DATA_DYNAMIC_HDR_PLUS);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 19/35] lavc/nvenc: switch to the new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (16 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 18/35] lavc/libvpxenc: remove unneeded context variable Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 20/35] lavc/qsvdec: " Anton Khirnov
` (17 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/nvenc.c | 36 ++++++++++++++++++------------------
1 file changed, 18 insertions(+), 18 deletions(-)
diff --git a/libavcodec/nvenc.c b/libavcodec/nvenc.c
index 850c46022b..ae57399e96 100644
--- a/libavcodec/nvenc.c
+++ b/libavcodec/nvenc.c
@@ -1568,7 +1568,7 @@ static av_cold int nvenc_alloc_surface(AVCodecContext *avctx, int idx)
ctx->surfaces[idx].output_surface = allocOut.bitstreamBuffer;
- av_fifo_generic_write(ctx->unused_surface_queue, &tmp_surface, sizeof(tmp_surface), NULL);
+ av_fifo_write(ctx->unused_surface_queue, &tmp_surface, 1);
return 0;
}
@@ -1582,18 +1582,18 @@ static av_cold int nvenc_setup_surfaces(AVCodecContext *avctx)
if (!ctx->surfaces)
return AVERROR(ENOMEM);
- ctx->timestamp_list = av_fifo_alloc(ctx->nb_surfaces * sizeof(int64_t));
+ ctx->timestamp_list = av_fifo_alloc2(ctx->nb_surfaces, sizeof(int64_t), 0);
if (!ctx->timestamp_list)
return AVERROR(ENOMEM);
- ctx->unused_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*));
+ ctx->unused_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
if (!ctx->unused_surface_queue)
return AVERROR(ENOMEM);
- ctx->output_surface_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*));
+ ctx->output_surface_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
if (!ctx->output_surface_queue)
return AVERROR(ENOMEM);
- ctx->output_surface_ready_queue = av_fifo_alloc(ctx->nb_surfaces * sizeof(NvencSurface*));
+ ctx->output_surface_ready_queue = av_fifo_alloc2(ctx->nb_surfaces, sizeof(NvencSurface*), 0);
if (!ctx->output_surface_ready_queue)
return AVERROR(ENOMEM);
@@ -1777,11 +1777,11 @@ static NvencSurface *get_free_frame(NvencContext *ctx)
{
NvencSurface *tmp_surf;
- if (!(av_fifo_size(ctx->unused_surface_queue) > 0))
+ if (!av_fifo_can_read(ctx->unused_surface_queue))
// queue empty
return NULL;
- av_fifo_generic_read(ctx->unused_surface_queue, &tmp_surf, sizeof(tmp_surf), NULL);
+ av_fifo_read(ctx->unused_surface_queue, &tmp_surf, 1);
return tmp_surf;
}
@@ -2000,14 +2000,14 @@ static void nvenc_codec_specific_pic_params(AVCodecContext *avctx,
static inline void timestamp_queue_enqueue(AVFifoBuffer* queue, int64_t timestamp)
{
- av_fifo_generic_write(queue, ×tamp, sizeof(timestamp), NULL);
+ av_fifo_write(queue, ×tamp, 1);
}
static inline int64_t timestamp_queue_dequeue(AVFifoBuffer* queue)
{
int64_t timestamp = AV_NOPTS_VALUE;
- if (av_fifo_size(queue) > 0)
- av_fifo_generic_read(queue, ×tamp, sizeof(timestamp), NULL);
+ if (av_fifo_can_read(queue))
+ av_fifo_read(queue, ×tamp, 1);
return timestamp;
}
@@ -2152,8 +2152,8 @@ static int output_ready(AVCodecContext *avctx, int flush)
NvencContext *ctx = avctx->priv_data;
int nb_ready, nb_pending;
- nb_ready = av_fifo_size(ctx->output_surface_ready_queue) / sizeof(NvencSurface*);
- nb_pending = av_fifo_size(ctx->output_surface_queue) / sizeof(NvencSurface*);
+ nb_ready = av_fifo_can_read(ctx->output_surface_ready_queue);
+ nb_pending = av_fifo_can_read(ctx->output_surface_queue);
if (flush)
return nb_ready > 0;
return (nb_ready > 0) && (nb_ready + nb_pending >= ctx->async_depth);
@@ -2442,15 +2442,15 @@ static int nvenc_send_frame(AVCodecContext *avctx, const AVFrame *frame)
return nvenc_print_error(avctx, nv_status, "EncodePicture failed!");
if (frame && frame->buf[0]) {
- av_fifo_generic_write(ctx->output_surface_queue, &in_surf, sizeof(in_surf), NULL);
+ av_fifo_write(ctx->output_surface_queue, &in_surf, 1);
timestamp_queue_enqueue(ctx->timestamp_list, frame->pts);
}
/* all the pending buffers are now ready for output */
if (nv_status == NV_ENC_SUCCESS) {
- while (av_fifo_size(ctx->output_surface_queue) > 0) {
- av_fifo_generic_read(ctx->output_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
- av_fifo_generic_write(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
+ while (av_fifo_can_read(ctx->output_surface_queue)) {
+ av_fifo_read (ctx->output_surface_queue, &tmp_out_surf, 1);
+ av_fifo_write(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
}
}
@@ -2483,7 +2483,7 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
av_frame_unref(frame);
if (output_ready(avctx, avctx->internal->draining)) {
- av_fifo_generic_read(ctx->output_surface_ready_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
+ av_fifo_read(ctx->output_surface_ready_queue, &tmp_out_surf, 1);
res = nvenc_push_context(avctx);
if (res < 0)
@@ -2498,7 +2498,7 @@ int ff_nvenc_receive_packet(AVCodecContext *avctx, AVPacket *pkt)
if (res)
return res;
- av_fifo_generic_write(ctx->unused_surface_queue, &tmp_out_surf, sizeof(tmp_out_surf), NULL);
+ av_fifo_write(ctx->unused_surface_queue, &tmp_out_surf, 1);
} else if (avctx->internal->draining) {
return AVERROR_EOF;
} else {
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 20/35] lavc/qsvdec: switch to the new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (17 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 19/35] lavc/nvenc: switch to the new FIFO API Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 21/35] lavc/qsvenc: switch to " Anton Khirnov
` (16 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/qsvdec.c | 80 +++++++++++++++++++--------------------------
1 file changed, 33 insertions(+), 47 deletions(-)
diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
index 08370c8a0b..acdf7804f6 100644
--- a/libavcodec/qsvdec.c
+++ b/libavcodec/qsvdec.c
@@ -56,6 +56,11 @@ static const AVRational mfx_tb = { 1, 90000 };
AV_NOPTS_VALUE : pts_tb.num ? \
av_rescale_q(mfx_pts, mfx_tb, pts_tb) : mfx_pts)
+typedef struct QSVAsyncFrame {
+ mfxSyncPoint *sync;
+ QSVFrame *frame;
+} QSVAsyncFrame;
+
typedef struct QSVContext {
// the session used for decoding
mfxSession session;
@@ -208,16 +213,6 @@ static int qsv_init_session(AVCodecContext *avctx, QSVContext *q, mfxSession ses
return 0;
}
-static inline unsigned int qsv_fifo_item_size(void)
-{
- return sizeof(mfxSyncPoint*) + sizeof(QSVFrame*);
-}
-
-static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
-{
- return av_fifo_size(fifo) / qsv_fifo_item_size();
-}
-
static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixelFormat pix_fmt, mfxVideoParam *param)
{
mfxSession session = NULL;
@@ -235,7 +230,7 @@ static int qsv_decode_preinit(AVCodecContext *avctx, QSVContext *q, enum AVPixel
}
if (!q->async_fifo) {
- q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
+ q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVAsyncFrame), 0);
if (!q->async_fifo)
return AVERROR(ENOMEM);
}
@@ -502,7 +497,6 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
AVFrame *frame, int *got_frame,
const AVPacket *avpkt)
{
- QSVFrame *out_frame;
mfxFrameSurface1 *insurf;
mfxFrameSurface1 *outsurf;
mfxSyncPoint *sync;
@@ -561,6 +555,7 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
}
if (*sync) {
+ QSVAsyncFrame aframe;
QSVFrame *out_frame = find_frame(q, outsurf);
if (!out_frame) {
@@ -571,35 +566,36 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
}
out_frame->queued = 1;
- av_fifo_generic_write(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
- av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
+
+ aframe = (QSVAsyncFrame){ sync, out_frame };
+ av_fifo_write(q->async_fifo, &aframe, 1);
} else {
av_freep(&sync);
}
- if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
- (!avpkt->size && av_fifo_size(q->async_fifo))) {
+ if ((av_fifo_can_read(q->async_fifo) >= q->async_depth) ||
+ (!avpkt->size && av_fifo_can_read(q->async_fifo))) {
+ QSVAsyncFrame aframe;
AVFrame *src_frame;
- av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
- av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
- out_frame->queued = 0;
+ av_fifo_read(q->async_fifo, &aframe, 1);
+ aframe.frame->queued = 0;
if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
do {
- ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
+ ret = MFXVideoCORE_SyncOperation(q->session, *aframe.sync, 1000);
} while (ret == MFX_WRN_IN_EXECUTION);
}
- av_freep(&sync);
+ av_freep(&aframe.sync);
- src_frame = out_frame->frame;
+ src_frame = aframe.frame->frame;
ret = av_frame_ref(frame, src_frame);
if (ret < 0)
return ret;
- outsurf = &out_frame->surface;
+ outsurf = &aframe.frame->surface;
frame->pts = MFX_PTS_TO_PTS(outsurf->Data.TimeStamp, avctx->pkt_timebase);
@@ -611,10 +607,10 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext *q,
outsurf->Info.PicStruct & MFX_PICSTRUCT_FIELD_TFF;
frame->interlaced_frame =
!(outsurf->Info.PicStruct & MFX_PICSTRUCT_PROGRESSIVE);
- frame->pict_type = ff_qsv_map_pictype(out_frame->dec_info.FrameType);
+ frame->pict_type = ff_qsv_map_pictype(aframe.frame->dec_info.FrameType);
//Key frame is IDR frame is only suitable for H264. For HEVC, IRAPs are key frames.
if (avctx->codec_id == AV_CODEC_ID_H264)
- frame->key_frame = !!(out_frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
+ frame->key_frame = !!(aframe.frame->dec_info.FrameType & MFX_FRAMETYPE_IDR);
/* update the surface properties */
if (avctx->pix_fmt == AV_PIX_FMT_QSV)
@@ -633,14 +629,11 @@ static void qsv_decode_close_qsvcontext(QSVContext *q)
if (q->session)
MFXVideoDECODE_Close(q->session);
- while (q->async_fifo && av_fifo_size(q->async_fifo)) {
- QSVFrame *out_frame;
- mfxSyncPoint *sync;
+ while (q->async_fifo && av_fifo_can_read(q->async_fifo)) {
+ QSVAsyncFrame aframe;
- av_fifo_generic_read(q->async_fifo, &out_frame, sizeof(out_frame), NULL);
- av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
-
- av_freep(&sync);
+ av_fifo_read(q->async_fifo, &aframe, 1);
+ av_freep(&aframe.sync);
}
while (cur) {
@@ -650,8 +643,7 @@ static void qsv_decode_close_qsvcontext(QSVContext *q)
cur = q->work_frames;
}
- av_fifo_free(q->async_fifo);
- q->async_fifo = NULL;
+ av_fifo_freep(&q->async_fifo);
ff_qsv_close_internal_session(&q->internal_qs);
@@ -742,8 +734,8 @@ typedef struct QSVDecContext {
static void qsv_clear_buffers(QSVDecContext *s)
{
AVPacket pkt;
- while (av_fifo_size(s->packet_fifo) >= sizeof(pkt)) {
- av_fifo_generic_read(s->packet_fifo, &pkt, sizeof(pkt), NULL);
+ while (av_fifo_can_read(s->packet_fifo)) {
+ av_fifo_read(s->packet_fifo, &pkt, 1);
av_packet_unref(&pkt);
}
@@ -797,7 +789,8 @@ static av_cold int qsv_decode_init(AVCodecContext *avctx)
}
s->qsv.orig_pix_fmt = AV_PIX_FMT_NV12;
- s->packet_fifo = av_fifo_alloc(sizeof(AVPacket));
+ s->packet_fifo = av_fifo_alloc2(1, sizeof(AVPacket),
+ AV_FIFO_FLAG_AUTO_GROW);
if (!s->packet_fifo) {
ret = AVERROR(ENOMEM);
goto fail;
@@ -823,17 +816,10 @@ static int qsv_decode_frame(AVCodecContext *avctx, void *data,
if (avpkt->size) {
AVPacket input_ref;
- if (av_fifo_space(s->packet_fifo) < sizeof(input_ref)) {
- ret = av_fifo_realloc2(s->packet_fifo,
- av_fifo_size(s->packet_fifo) + sizeof(input_ref));
- if (ret < 0)
- return ret;
- }
-
ret = av_packet_ref(&input_ref, avpkt);
if (ret < 0)
return ret;
- av_fifo_generic_write(s->packet_fifo, &input_ref, sizeof(input_ref), NULL);
+ av_fifo_write(s->packet_fifo, &input_ref, 1);
}
/* process buffered data */
@@ -841,12 +827,12 @@ static int qsv_decode_frame(AVCodecContext *avctx, void *data,
/* prepare the input data */
if (s->buffer_pkt.size <= 0) {
/* no more data */
- if (av_fifo_size(s->packet_fifo) < sizeof(AVPacket))
+ if (!av_fifo_can_read(s->packet_fifo))
return avpkt->size ? avpkt->size : qsv_process_data(avctx, &s->qsv, frame, got_frame, avpkt);
/* in progress of reinit, no read from fifo and keep the buffer_pkt */
if (!s->qsv.reinit_flag) {
av_packet_unref(&s->buffer_pkt);
- av_fifo_generic_read(s->packet_fifo, &s->buffer_pkt, sizeof(s->buffer_pkt), NULL);
+ av_fifo_read(s->packet_fifo, &s->buffer_pkt, 1);
}
}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 21/35] lavc/qsvenc: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (18 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 20/35] lavc/qsvdec: " Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 22/35] lavf/dvenc: return an error on audio/video desync Anton Khirnov
` (15 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavcodec/qsvenc.c | 120 +++++++++++++++++++-------------------------
1 file changed, 52 insertions(+), 68 deletions(-)
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 4e7a15f060..ee489325f5 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -85,6 +85,12 @@ static const struct profile_names vp9_profiles[] = {
#endif
};
+typedef struct QSVPacket {
+ AVPacket pkt;
+ mfxSyncPoint *sync;
+ mfxBitstream *bs;
+} QSVPacket;
+
static const char *print_profile(enum AVCodecID codec_id, mfxU16 profile)
{
const struct profile_names *profiles;
@@ -1258,16 +1264,6 @@ static int qsvenc_init_session(AVCodecContext *avctx, QSVEncContext *q)
return 0;
}
-static inline unsigned int qsv_fifo_item_size(void)
-{
- return sizeof(AVPacket) + sizeof(mfxSyncPoint*) + sizeof(mfxBitstream*);
-}
-
-static inline unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
-{
- return av_fifo_size(fifo)/qsv_fifo_item_size();
-}
-
int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
{
int iopattern = 0;
@@ -1276,7 +1272,7 @@ int ff_qsv_enc_init(AVCodecContext *avctx, QSVEncContext *q)
q->param.AsyncDepth = q->async_depth;
- q->async_fifo = av_fifo_alloc(q->async_depth * qsv_fifo_item_size());
+ q->async_fifo = av_fifo_alloc2(q->async_depth, sizeof(QSVPacket), 0);
if (!q->async_fifo)
return AVERROR(ENOMEM);
@@ -1578,15 +1574,13 @@ static void print_interlace_msg(AVCodecContext *avctx, QSVEncContext *q)
static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
const AVFrame *frame)
{
- AVPacket new_pkt = { 0 };
- mfxBitstream *bs = NULL;
+ QSVPacket pkt = { { 0 } };
#if QSV_VERSION_ATLEAST(1, 26)
mfxExtAVCEncodedFrameInfo *enc_info = NULL;
mfxExtBuffer **enc_buf = NULL;
#endif
mfxFrameSurface1 *surf = NULL;
- mfxSyncPoint *sync = NULL;
QSVFrame *qsv_frame = NULL;
mfxEncodeCtrl* enc_ctrl = NULL;
int ret;
@@ -1609,17 +1603,17 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
}
}
- ret = av_new_packet(&new_pkt, q->packet_size);
+ ret = av_new_packet(&pkt.pkt, q->packet_size);
if (ret < 0) {
av_log(avctx, AV_LOG_ERROR, "Error allocating the output packet\n");
return ret;
}
- bs = av_mallocz(sizeof(*bs));
- if (!bs)
+ pkt.bs = av_mallocz(sizeof(*pkt.bs));
+ if (!pkt.bs)
goto nomem;
- bs->Data = new_pkt.data;
- bs->MaxLength = new_pkt.size;
+ pkt.bs->Data = pkt.pkt.data;
+ pkt.bs->MaxLength = pkt.pkt.size;
#if QSV_VERSION_ATLEAST(1, 26)
if (avctx->codec_id == AV_CODEC_ID_H264) {
@@ -1629,13 +1623,13 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
enc_info->Header.BufferId = MFX_EXTBUFF_ENCODED_FRAME_INFO;
enc_info->Header.BufferSz = sizeof (*enc_info);
- bs->NumExtParam = 1;
+ pkt.bs->NumExtParam = 1;
enc_buf = av_mallocz(sizeof(mfxExtBuffer *));
if (!enc_buf)
goto nomem;
enc_buf[0] = (mfxExtBuffer *)enc_info;
- bs->ExtParam = enc_buf;
+ pkt.bs->ExtParam = enc_buf;
}
#endif
@@ -1643,12 +1637,12 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
q->set_encode_ctrl_cb(avctx, frame, &qsv_frame->enc_ctrl);
}
- sync = av_mallocz(sizeof(*sync));
- if (!sync)
+ pkt.sync = av_mallocz(sizeof(*pkt.sync));
+ if (!pkt.sync)
goto nomem;
do {
- ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, bs, sync);
+ ret = MFXVideoENCODE_EncodeFrameAsync(q->session, enc_ctrl, surf, pkt.bs, pkt.sync);
if (ret == MFX_WRN_DEVICE_BUSY)
av_usleep(500);
} while (ret == MFX_WRN_DEVICE_BUSY || ret == MFX_WRN_IN_EXECUTION);
@@ -1667,15 +1661,13 @@ static int encode_frame(AVCodecContext *avctx, QSVEncContext *q,
ret = 0;
- if (*sync) {
- av_fifo_generic_write(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
- av_fifo_generic_write(q->async_fifo, &sync, sizeof(sync), NULL);
- av_fifo_generic_write(q->async_fifo, &bs, sizeof(bs), NULL);
+ if (*pkt.sync) {
+ av_fifo_write(q->async_fifo, &pkt, 1);
} else {
free:
- av_freep(&sync);
- av_packet_unref(&new_pkt);
- av_freep(&bs);
+ av_freep(&pkt.sync);
+ av_packet_unref(&pkt.pkt);
+ av_freep(&pkt.bs);
#if QSV_VERSION_ATLEAST(1, 26)
if (avctx->codec_id == AV_CODEC_ID_H264) {
av_freep(&enc_info);
@@ -1699,60 +1691,56 @@ int ff_qsv_encode(AVCodecContext *avctx, QSVEncContext *q,
if (ret < 0)
return ret;
- if ((qsv_fifo_size(q->async_fifo) >= q->async_depth) ||
- (!frame && av_fifo_size(q->async_fifo))) {
- AVPacket new_pkt;
- mfxBitstream *bs;
- mfxSyncPoint *sync;
+ if ((av_fifo_can_read(q->async_fifo) >= q->async_depth) ||
+ (!frame && av_fifo_can_read(q->async_fifo))) {
+ QSVPacket qpkt;
#if QSV_VERSION_ATLEAST(1, 26)
mfxExtAVCEncodedFrameInfo *enc_info;
mfxExtBuffer **enc_buf;
#endif
enum AVPictureType pict_type;
- av_fifo_generic_read(q->async_fifo, &new_pkt, sizeof(new_pkt), NULL);
- av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
- av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
+ av_fifo_read(q->async_fifo, &qpkt, 1);
do {
- ret = MFXVideoCORE_SyncOperation(q->session, *sync, 1000);
+ ret = MFXVideoCORE_SyncOperation(q->session, *qpkt.sync, 1000);
} while (ret == MFX_WRN_IN_EXECUTION);
- new_pkt.dts = av_rescale_q(bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
- new_pkt.pts = av_rescale_q(bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
- new_pkt.size = bs->DataLength;
+ qpkt.pkt.dts = av_rescale_q(qpkt.bs->DecodeTimeStamp, (AVRational){1, 90000}, avctx->time_base);
+ qpkt.pkt.pts = av_rescale_q(qpkt.bs->TimeStamp, (AVRational){1, 90000}, avctx->time_base);
+ qpkt.pkt.size = qpkt.bs->DataLength;
- if (bs->FrameType & MFX_FRAMETYPE_IDR || bs->FrameType & MFX_FRAMETYPE_xIDR) {
- new_pkt.flags |= AV_PKT_FLAG_KEY;
+ if (qpkt.bs->FrameType & MFX_FRAMETYPE_IDR || qpkt.bs->FrameType & MFX_FRAMETYPE_xIDR) {
+ qpkt.pkt.flags |= AV_PKT_FLAG_KEY;
pict_type = AV_PICTURE_TYPE_I;
- } else if (bs->FrameType & MFX_FRAMETYPE_I || bs->FrameType & MFX_FRAMETYPE_xI)
+ } else if (qpkt.bs->FrameType & MFX_FRAMETYPE_I || qpkt.bs->FrameType & MFX_FRAMETYPE_xI)
pict_type = AV_PICTURE_TYPE_I;
- else if (bs->FrameType & MFX_FRAMETYPE_P || bs->FrameType & MFX_FRAMETYPE_xP)
+ else if (qpkt.bs->FrameType & MFX_FRAMETYPE_P || qpkt.bs->FrameType & MFX_FRAMETYPE_xP)
pict_type = AV_PICTURE_TYPE_P;
- else if (bs->FrameType & MFX_FRAMETYPE_B || bs->FrameType & MFX_FRAMETYPE_xB)
+ else if (qpkt.bs->FrameType & MFX_FRAMETYPE_B || qpkt.bs->FrameType & MFX_FRAMETYPE_xB)
pict_type = AV_PICTURE_TYPE_B;
- else if (bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
+ else if (qpkt.bs->FrameType == MFX_FRAMETYPE_UNKNOWN) {
pict_type = AV_PICTURE_TYPE_NONE;
av_log(avctx, AV_LOG_WARNING, "Unknown FrameType, set pict_type to AV_PICTURE_TYPE_NONE.\n");
} else {
- av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", bs->FrameType);
+ av_log(avctx, AV_LOG_ERROR, "Invalid FrameType:%d.\n", qpkt.bs->FrameType);
return AVERROR_INVALIDDATA;
}
#if QSV_VERSION_ATLEAST(1, 26)
if (avctx->codec_id == AV_CODEC_ID_H264) {
- enc_buf = bs->ExtParam;
- enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam);
- ff_side_data_set_encoder_stats(&new_pkt,
+ enc_buf = qpkt.bs->ExtParam;
+ enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
+ ff_side_data_set_encoder_stats(&qpkt.pkt,
enc_info->QP * FF_QP2LAMBDA, NULL, 0, pict_type);
av_freep(&enc_info);
av_freep(&enc_buf);
}
#endif
- av_freep(&bs);
- av_freep(&sync);
+ av_freep(&qpkt.bs);
+ av_freep(&qpkt.sync);
- av_packet_move_ref(pkt, &new_pkt);
+ av_packet_move_ref(pkt, &qpkt.pkt);
*got_packet = 1;
}
@@ -1782,26 +1770,22 @@ int ff_qsv_enc_close(AVCodecContext *avctx, QSVEncContext *q)
cur = q->work_frames;
}
- while (q->async_fifo && av_fifo_size(q->async_fifo)) {
- AVPacket pkt;
- mfxSyncPoint *sync;
- mfxBitstream *bs;
+ while (q->async_fifo && av_fifo_can_read(q->async_fifo)) {
+ QSVPacket pkt;
- av_fifo_generic_read(q->async_fifo, &pkt, sizeof(pkt), NULL);
- av_fifo_generic_read(q->async_fifo, &sync, sizeof(sync), NULL);
- av_fifo_generic_read(q->async_fifo, &bs, sizeof(bs), NULL);
+ av_fifo_read(q->async_fifo, &pkt, 1);
#if QSV_VERSION_ATLEAST(1, 26)
if (avctx->codec_id == AV_CODEC_ID_H264) {
- mfxExtBuffer **enc_buf = bs->ExtParam;
- mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*bs->ExtParam);
+ mfxExtBuffer **enc_buf = pkt.bs->ExtParam;
+ mfxExtAVCEncodedFrameInfo *enc_info = (mfxExtAVCEncodedFrameInfo *)(*enc_buf);
av_freep(&enc_info);
av_freep(&enc_buf);
}
#endif
- av_freep(&sync);
- av_freep(&bs);
- av_packet_unref(&pkt);
+ av_freep(&pkt.sync);
+ av_freep(&pkt.bs);
+ av_packet_unref(&pkt.pkt);
}
av_fifo_free(q->async_fifo);
q->async_fifo = NULL;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 22/35] lavf/dvenc: return an error on audio/video desync
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (19 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 21/35] lavc/qsvenc: switch to " Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 23/35] lavf/dvenc: switch to new FIFO API Anton Khirnov
` (14 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavformat/dvenc.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c
index b76539b59f..03b63cff89 100644
--- a/libavformat/dvenc.c
+++ b/libavformat/dvenc.c
@@ -255,8 +255,10 @@ static int dv_assemble_frame(AVFormatContext *s,
switch (st->codecpar->codec_type) {
case AVMEDIA_TYPE_VIDEO:
/* FIXME: we have to have more sensible approach than this one */
- if (c->has_video)
+ if (c->has_video) {
av_log(s, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient audio data or severe sync problem.\n", c->frames);
+ return AVERROR(EINVAL);
+ }
if (data_size != c->sys->frame_size) {
av_log(s, AV_LOG_ERROR, "Unexpected frame size, %d != %d\n",
data_size, c->sys->frame_size);
@@ -270,8 +272,10 @@ static int dv_assemble_frame(AVFormatContext *s,
for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
/* FIXME: we have to have more sensible approach than this one */
- if (av_fifo_size(c->audio_data[i]) + data_size >= 100*MAX_AUDIO_FRAME_SIZE)
+ if (av_fifo_size(c->audio_data[i]) + data_size >= 100*MAX_AUDIO_FRAME_SIZE) {
av_log(s, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
+ return AVERROR(EINVAL);
+ }
av_fifo_generic_write(c->audio_data[i], data, data_size, NULL);
reqasize = 4 * dv_audio_frame_size(c->sys, c->frames, st->codecpar->sample_rate);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 23/35] lavf/dvenc: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (20 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 22/35] lavf/dvenc: return an error on audio/video desync Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 24/35] lavf/mpegenc: " Anton Khirnov
` (13 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavformat/dvenc.c | 18 ++++++++++--------
1 file changed, 10 insertions(+), 8 deletions(-)
diff --git a/libavformat/dvenc.c b/libavformat/dvenc.c
index 03b63cff89..b9c9ba236d 100644
--- a/libavformat/dvenc.c
+++ b/libavformat/dvenc.c
@@ -202,7 +202,7 @@ static void dv_inject_audio(DVMuxContext *c, int channel, uint8_t* frame_ptr)
continue;
// FIXME: maybe we have to admit that DV is a big-endian PCM
- av_fifo_generic_peek_at(c->audio_data[channel], frame_ptr + d, of * 2, 2, NULL);
+ av_fifo_peek(c->audio_data[channel], frame_ptr + d, 2, of * 2);
FFSWAP(uint8_t, frame_ptr[d], frame_ptr[d + 1]);
}
frame_ptr += 16 * 80; /* 15 Video DIFs + 1 Audio DIF */
@@ -272,16 +272,16 @@ static int dv_assemble_frame(AVFormatContext *s,
for (i = 0; i < c->n_ast && st != c->ast[i]; i++);
/* FIXME: we have to have more sensible approach than this one */
- if (av_fifo_size(c->audio_data[i]) + data_size >= 100*MAX_AUDIO_FRAME_SIZE) {
+ if (av_fifo_can_write(c->audio_data[i]) < data_size) {
av_log(s, AV_LOG_ERROR, "Can't process DV frame #%d. Insufficient video data or severe sync problem.\n", c->frames);
return AVERROR(EINVAL);
}
- av_fifo_generic_write(c->audio_data[i], data, data_size, NULL);
+ av_fifo_write(c->audio_data[i], data, data_size);
reqasize = 4 * dv_audio_frame_size(c->sys, c->frames, st->codecpar->sample_rate);
/* Let us see if we've got enough audio for one DV frame. */
- c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i);
+ c->has_audio |= ((reqasize <= av_fifo_can_read(c->audio_data[i])) << i);
break;
default:
@@ -295,8 +295,8 @@ static int dv_assemble_frame(AVFormatContext *s,
for (i=0; i < c->n_ast; i++) {
dv_inject_audio(c, i, *frame);
reqasize = 4 * dv_audio_frame_size(c->sys, c->frames, c->ast[i]->codecpar->sample_rate);
- av_fifo_drain(c->audio_data[i], reqasize);
- c->has_audio |= ((reqasize <= av_fifo_size(c->audio_data[i])) << i);
+ av_fifo_drain2(c->audio_data[i], reqasize);
+ c->has_audio |= ((reqasize <= av_fifo_can_read(c->audio_data[i])) << i);
}
c->has_video = 0;
@@ -374,9 +374,11 @@ static DVMuxContext* dv_init_mux(AVFormatContext* s)
ff_parse_creation_time_metadata(s, &c->start_time, 1);
for (i=0; i < c->n_ast; i++) {
- if (c->ast[i] && !(c->audio_data[i]=av_fifo_alloc_array(100, MAX_AUDIO_FRAME_SIZE))) {
+ if (!c->ast[i])
+ continue;
+ c->audio_data[i] = av_fifo_alloc2(100 * MAX_AUDIO_FRAME_SIZE, 1, 0);
+ if (!c->audio_data[i])
goto bail_out;
- }
}
return c;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 24/35] lavf/mpegenc: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (21 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 23/35] lavf/dvenc: switch to new FIFO API Anton Khirnov
@ 2022-01-11 20:45 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 25/35] lavf/swfenc: " Anton Khirnov
` (12 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:45 UTC (permalink / raw)
To: ffmpeg-devel
---
libavformat/mpegenc.c | 36 +++++++++++++++++++++---------------
1 file changed, 21 insertions(+), 15 deletions(-)
diff --git a/libavformat/mpegenc.c b/libavformat/mpegenc.c
index b1d8bf9c38..909df7f484 100644
--- a/libavformat/mpegenc.c
+++ b/libavformat/mpegenc.c
@@ -459,7 +459,7 @@ static av_cold int mpeg_mux_init(AVFormatContext *ctx)
av_get_media_type_string(st->codecpar->codec_type), i);
return AVERROR(EINVAL);
}
- stream->fifo = av_fifo_alloc(16);
+ stream->fifo = av_fifo_alloc2(16, 1, 0);
if (!stream->fifo)
return AVERROR(ENOMEM);
}
@@ -626,6 +626,12 @@ static int get_nb_frames(AVFormatContext *ctx, StreamInfo *stream, int len)
return nb_frames;
}
+static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems)
+{
+ avio_write(opaque, buf, *nb_elems);
+ return 0;
+}
+
/* flush the packet on stream stream_index */
static int flush_packet(AVFormatContext *ctx, int stream_index,
int64_t pts, int64_t dts, int64_t scr, int trailer_size)
@@ -741,6 +747,7 @@ static int flush_packet(AVFormatContext *ctx, int stream_index,
packet_size -= pad_packet_bytes + zero_trail_bytes;
if (packet_size > 0) {
+ size_t fifo_data;
/* packet header size */
packet_size -= 6;
@@ -776,7 +783,7 @@ static int flush_packet(AVFormatContext *ctx, int stream_index,
startcode = 0x100 + id;
}
- stuffing_size = payload_size - av_fifo_size(stream->fifo);
+ stuffing_size = payload_size - av_fifo_can_read(stream->fifo);
// first byte does not fit -> reset pts/dts + stuffing
if (payload_size <= trailer_size && pts != AV_NOPTS_VALUE) {
@@ -814,7 +821,7 @@ static int flush_packet(AVFormatContext *ctx, int stream_index,
stuffing_size = 0;
if (startcode == PRIVATE_STREAM_1 && id >= 0xa0) {
- if (payload_size < av_fifo_size(stream->fifo))
+ if (payload_size < av_fifo_can_read(stream->fifo))
stuffing_size += payload_size % stream->lpcm_align;
}
@@ -907,11 +914,10 @@ static int flush_packet(AVFormatContext *ctx, int stream_index,
}
/* output data */
- av_assert0(payload_size - stuffing_size <= av_fifo_size(stream->fifo));
- av_fifo_generic_read(stream->fifo, ctx->pb,
- payload_size - stuffing_size,
- (void (*)(void*, void*, int))avio_write);
- stream->bytes_to_iframe -= payload_size - stuffing_size;
+ fifo_data = payload_size - stuffing_size;
+ av_assert0(fifo_data <= av_fifo_can_read(stream->fifo));
+ av_fifo_read_to_cb(stream->fifo, fifo_avio_wrapper, ctx->pb, &fifo_data);
+ stream->bytes_to_iframe -= fifo_data;
} else {
payload_size =
stuffing_size = 0;
@@ -1005,7 +1011,7 @@ retry:
for (i = 0; i < ctx->nb_streams; i++) {
AVStream *st = ctx->streams[i];
StreamInfo *stream = st->priv_data;
- const int avail_data = av_fifo_size(stream->fifo);
+ const size_t avail_data = av_fifo_can_read(stream->fifo);
const int space = stream->max_buffer_size - stream->buffer_index;
int rel_space = 1024LL * space / stream->max_buffer_size;
PacketDesc *next_pkt = stream->premux_packet;
@@ -1075,7 +1081,7 @@ retry:
st = ctx->streams[best_i];
stream = st->priv_data;
- av_assert0(av_fifo_size(stream->fifo) > 0);
+ av_assert0(av_fifo_can_read(stream->fifo) > 0);
av_assert0(avail_space >= s->packet_size || ignore_constraints);
@@ -1095,7 +1101,7 @@ retry:
es_size = flush_packet(ctx, best_i, timestamp_packet->pts,
timestamp_packet->dts, scr, trailer_size);
} else {
- av_assert0(av_fifo_size(stream->fifo) == trailer_size);
+ av_assert0(av_fifo_can_read(stream->fifo) == trailer_size);
es_size = flush_packet(ctx, best_i, AV_NOPTS_VALUE, AV_NOPTS_VALUE, scr,
trailer_size);
}
@@ -1199,7 +1205,7 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
pkt_desc->unwritten_size =
pkt_desc->size = size;
- ret = av_fifo_realloc2(stream->fifo, av_fifo_size(stream->fifo) + size);
+ ret = av_fifo_grow2(stream->fifo, size);
if (ret < 0)
return ret;
@@ -1208,13 +1214,13 @@ static int mpeg_mux_write_packet(AVFormatContext *ctx, AVPacket *pkt)
if (is_iframe &&
(s->packet_number == 0 || pts != AV_NOPTS_VALUE &&
(pts - stream->vobu_start_pts >= 36000))) {
- stream->bytes_to_iframe = av_fifo_size(stream->fifo);
+ stream->bytes_to_iframe = av_fifo_can_read(stream->fifo);
stream->align_iframe = 1;
stream->vobu_start_pts = pts;
}
}
- av_fifo_generic_write(stream->fifo, buf, size, NULL);
+ av_fifo_write(stream->fifo, buf, size);
for (;;) {
int ret = output_packet(ctx, 0);
@@ -1244,7 +1250,7 @@ static int mpeg_mux_end(AVFormatContext *ctx)
for (i = 0; i < ctx->nb_streams; i++) {
stream = ctx->streams[i]->priv_data;
- av_assert0(av_fifo_size(stream->fifo) == 0);
+ av_assert0(av_fifo_can_read(stream->fifo) == 0);
}
return 0;
}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 25/35] lavf/swfenc: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (22 preceding siblings ...)
2022-01-11 20:45 ` [FFmpeg-devel] [PATCH 24/35] lavf/mpegenc: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 26/35] lavf/udp: " Anton Khirnov
` (11 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavformat/swfenc.c | 18 ++++++++++++------
1 file changed, 12 insertions(+), 6 deletions(-)
diff --git a/libavformat/swfenc.c b/libavformat/swfenc.c
index 1fd2ad81a3..ca9a358ccc 100644
--- a/libavformat/swfenc.c
+++ b/libavformat/swfenc.c
@@ -211,7 +211,7 @@ static int swf_write_header(AVFormatContext *s)
}
if (par->codec_id == AV_CODEC_ID_MP3) {
swf->audio_par = par;
- swf->audio_fifo= av_fifo_alloc(AUDIO_FIFO_SIZE);
+ swf->audio_fifo = av_fifo_alloc2(AUDIO_FIFO_SIZE, 1, 0);
if (!swf->audio_fifo)
return AVERROR(ENOMEM);
} else {
@@ -362,6 +362,12 @@ static int swf_write_header(AVFormatContext *s)
return 0;
}
+static int fifo_avio_wrapper(void *opaque, void *buf, size_t *nb_elems)
+{
+ avio_write(opaque, buf, *nb_elems);
+ return 0;
+}
+
static int swf_write_video(AVFormatContext *s,
AVCodecParameters *par, const uint8_t *buf, int size, unsigned pkt_flags)
{
@@ -454,12 +460,12 @@ static int swf_write_video(AVFormatContext *s,
swf->swf_frame_number++;
/* streaming sound always should be placed just before showframe tags */
- if (swf->audio_par && av_fifo_size(swf->audio_fifo)) {
- int frame_size = av_fifo_size(swf->audio_fifo);
+ if (swf->audio_par && av_fifo_can_read(swf->audio_fifo)) {
+ size_t frame_size = av_fifo_can_read(swf->audio_fifo);
put_swf_tag(s, TAG_STREAMBLOCK | TAG_LONG);
avio_wl16(pb, swf->sound_samples);
avio_wl16(pb, 0); // seek samples
- av_fifo_generic_read(swf->audio_fifo, pb, frame_size, (void*)avio_write);
+ av_fifo_read_to_cb(swf->audio_fifo, fifo_avio_wrapper, pb, &frame_size);
put_swf_end_tag(s);
/* update FIFO */
@@ -482,12 +488,12 @@ static int swf_write_audio(AVFormatContext *s,
if (swf->swf_frame_number == 16000)
av_log(s, AV_LOG_INFO, "warning: Flash Player limit of 16000 frames reached\n");
- if (av_fifo_size(swf->audio_fifo) + size > AUDIO_FIFO_SIZE) {
+ if (av_fifo_can_write(swf->audio_fifo) < size) {
av_log(s, AV_LOG_ERROR, "audio fifo too small to mux audio essence\n");
return -1;
}
- av_fifo_generic_write(swf->audio_fifo, buf, size, NULL);
+ av_fifo_write(swf->audio_fifo, buf, size);
swf->sound_samples += av_get_audio_frame_duration2(par, size);
/* if audio only stream make sure we add swf frames */
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 26/35] lavf/udp: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (23 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 25/35] lavf/swfenc: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-13 18:45 ` Andreas Rheinhardt
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 27/35] lavf/async: " Anton Khirnov
` (10 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavformat/udp.c | 28 ++++++++++++++--------------
1 file changed, 14 insertions(+), 14 deletions(-)
diff --git a/libavformat/udp.c b/libavformat/udp.c
index 83c042d079..4c8f104d9d 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -500,7 +500,7 @@ static void *circular_buffer_task_rx( void *_URLContext)
continue;
AV_WL32(s->tmp, len);
- if(av_fifo_space(s->fifo) < len + 4) {
+ if (av_fifo_can_write(s->fifo) < len + 4) {
/* No Space left */
if (s->overrun_nonfatal) {
av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
@@ -514,7 +514,7 @@ static void *circular_buffer_task_rx( void *_URLContext)
goto end;
}
}
- av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
+ av_fifo_write(s->fifo, s->tmp, len + 4);
pthread_cond_signal(&s->cond);
}
@@ -548,22 +548,22 @@ static void *circular_buffer_task_tx( void *_URLContext)
uint8_t tmp[4];
int64_t timestamp;
- len = av_fifo_size(s->fifo);
+ len = av_fifo_can_read(s->fifo);
while (len<4) {
if (s->close_req)
goto end;
pthread_cond_wait(&s->cond, &s->mutex);
- len = av_fifo_size(s->fifo);
+ len = av_fifo_can_read(s->fifo);
}
- av_fifo_generic_read(s->fifo, tmp, 4, NULL);
+ av_fifo_read(s->fifo, tmp, 4);
len = AV_RL32(tmp);
av_assert0(len >= 0);
av_assert0(len <= sizeof(s->tmp));
- av_fifo_generic_read(s->fifo, s->tmp, len, NULL);
+ av_fifo_read(s->fifo, s->tmp, len);
pthread_mutex_unlock(&s->mutex);
@@ -906,7 +906,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
/* start the task going */
- s->fifo = av_fifo_alloc(s->circular_buffer_size);
+ s->fifo = av_fifo_alloc2(s->circular_buffer_size, 1, 0);
if (!s->fifo) {
ret = AVERROR(ENOMEM);
goto fail;
@@ -970,19 +970,19 @@ static int udp_read(URLContext *h, uint8_t *buf, int size)
if (s->fifo) {
pthread_mutex_lock(&s->mutex);
do {
- avail = av_fifo_size(s->fifo);
+ avail = av_fifo_can_read(s->fifo);
if (avail) { // >=size) {
uint8_t tmp[4];
- av_fifo_generic_read(s->fifo, tmp, 4, NULL);
+ av_fifo_read(s->fifo, tmp, 4);
avail = AV_RL32(tmp);
if(avail > size){
av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
avail = size;
}
- av_fifo_generic_read(s->fifo, buf, avail, NULL);
- av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
+ av_fifo_read(s->fifo, buf, avail);
+ av_fifo_drain2(s->fifo, AV_RL32(tmp) - avail);
pthread_mutex_unlock(&s->mutex);
return avail;
} else if(s->circular_buffer_error){
@@ -1043,14 +1043,14 @@ static int udp_write(URLContext *h, const uint8_t *buf, int size)
return err;
}
- if(av_fifo_space(s->fifo) < size + 4) {
+ if (av_fifo_can_write(s->fifo) < size + 4) {
/* What about a partial packet tx ? */
pthread_mutex_unlock(&s->mutex);
return AVERROR(ENOMEM);
}
AV_WL32(tmp, size);
- av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
- av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
+ av_fifo_write(s->fifo, tmp, 4); /* size of packet */
+ av_fifo_write(s->fifo, (uint8_t *)buf, size); /* the data */
pthread_cond_signal(&s->cond);
pthread_mutex_unlock(&s->mutex);
return size;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 26/35] lavf/udp: switch to new FIFO API
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 26/35] lavf/udp: " Anton Khirnov
@ 2022-01-13 18:45 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 18:45 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> ---
> libavformat/udp.c | 28 ++++++++++++++--------------
> 1 file changed, 14 insertions(+), 14 deletions(-)
>
> diff --git a/libavformat/udp.c b/libavformat/udp.c
> index 83c042d079..4c8f104d9d 100644
> --- a/libavformat/udp.c
> +++ b/libavformat/udp.c
> @@ -500,7 +500,7 @@ static void *circular_buffer_task_rx( void *_URLContext)
> continue;
> AV_WL32(s->tmp, len);
>
> - if(av_fifo_space(s->fifo) < len + 4) {
> + if (av_fifo_can_write(s->fifo) < len + 4) {
> /* No Space left */
> if (s->overrun_nonfatal) {
> av_log(h, AV_LOG_WARNING, "Circular buffer overrun. "
> @@ -514,7 +514,7 @@ static void *circular_buffer_task_rx( void *_URLContext)
> goto end;
> }
> }
> - av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL);
> + av_fifo_write(s->fifo, s->tmp, len + 4);
> pthread_cond_signal(&s->cond);
> }
>
> @@ -548,22 +548,22 @@ static void *circular_buffer_task_tx( void *_URLContext)
> uint8_t tmp[4];
> int64_t timestamp;
>
> - len = av_fifo_size(s->fifo);
> + len = av_fifo_can_read(s->fifo);
len should be made size_t (although the values always fit into an int).
>
> while (len<4) {
> if (s->close_req)
> goto end;
> pthread_cond_wait(&s->cond, &s->mutex);
> - len = av_fifo_size(s->fifo);
> + len = av_fifo_can_read(s->fifo);
> }
>
> - av_fifo_generic_read(s->fifo, tmp, 4, NULL);
> + av_fifo_read(s->fifo, tmp, 4);
> len = AV_RL32(tmp);
>
> av_assert0(len >= 0);
> av_assert0(len <= sizeof(s->tmp));
>
> - av_fifo_generic_read(s->fifo, s->tmp, len, NULL);
> + av_fifo_read(s->fifo, s->tmp, len);
>
> pthread_mutex_unlock(&s->mutex);
>
> @@ -906,7 +906,7 @@ static int udp_open(URLContext *h, const char *uri, int flags)
>
> if ((!is_output && s->circular_buffer_size) || (is_output && s->bitrate && s->circular_buffer_size)) {
> /* start the task going */
> - s->fifo = av_fifo_alloc(s->circular_buffer_size);
> + s->fifo = av_fifo_alloc2(s->circular_buffer_size, 1, 0);
> if (!s->fifo) {
> ret = AVERROR(ENOMEM);
> goto fail;
> @@ -970,19 +970,19 @@ static int udp_read(URLContext *h, uint8_t *buf, int size)
> if (s->fifo) {
> pthread_mutex_lock(&s->mutex);
> do {
> - avail = av_fifo_size(s->fifo);
> + avail = av_fifo_can_read(s->fifo);
> if (avail) { // >=size) {
> uint8_t tmp[4];
>
> - av_fifo_generic_read(s->fifo, tmp, 4, NULL);
> + av_fifo_read(s->fifo, tmp, 4);
> avail = AV_RL32(tmp);
> if(avail > size){
> av_log(h, AV_LOG_WARNING, "Part of datagram lost due to insufficient buffer size\n");
> avail = size;
> }
>
> - av_fifo_generic_read(s->fifo, buf, avail, NULL);
> - av_fifo_drain(s->fifo, AV_RL32(tmp) - avail);
> + av_fifo_read(s->fifo, buf, avail);
> + av_fifo_drain2(s->fifo, AV_RL32(tmp) - avail);
> pthread_mutex_unlock(&s->mutex);
> return avail;
> } else if(s->circular_buffer_error){
> @@ -1043,14 +1043,14 @@ static int udp_write(URLContext *h, const uint8_t *buf, int size)
> return err;
> }
>
> - if(av_fifo_space(s->fifo) < size + 4) {
> + if (av_fifo_can_write(s->fifo) < size + 4) {
> /* What about a partial packet tx ? */
> pthread_mutex_unlock(&s->mutex);
> return AVERROR(ENOMEM);
> }
> AV_WL32(tmp, size);
> - av_fifo_generic_write(s->fifo, tmp, 4, NULL); /* size of packet */
> - av_fifo_generic_write(s->fifo, (uint8_t *)buf, size, NULL); /* the data */
> + av_fifo_write(s->fifo, tmp, 4); /* size of packet */
> + av_fifo_write(s->fifo, (uint8_t *)buf, size); /* the data */
The cast can now be dropped.
> pthread_cond_signal(&s->cond);
> pthread_mutex_unlock(&s->mutex);
> return size;
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 27/35] lavf/async: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (24 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 26/35] lavf/udp: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 28/35] lavd/jack: switch to the " Anton Khirnov
` (9 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavformat/async.c | 62 ++++++++++++++++++++++-----------------------
1 file changed, 30 insertions(+), 32 deletions(-)
diff --git a/libavformat/async.c b/libavformat/async.c
index 5a81507ef1..59b0e7d352 100644
--- a/libavformat/async.c
+++ b/libavformat/async.c
@@ -83,7 +83,7 @@ typedef struct Context {
static int ring_init(RingBuffer *ring, unsigned int capacity, int read_back_capacity)
{
memset(ring, 0, sizeof(RingBuffer));
- ring->fifo = av_fifo_alloc(capacity + read_back_capacity);
+ ring->fifo = av_fifo_alloc2(capacity + read_back_capacity, 1, 0);
if (!ring->fifo)
return AVERROR(ENOMEM);
@@ -104,34 +104,48 @@ static void ring_reset(RingBuffer *ring)
static int ring_size(RingBuffer *ring)
{
- return av_fifo_size(ring->fifo) - ring->read_pos;
+ return av_fifo_can_read(ring->fifo) - ring->read_pos;
}
static int ring_space(RingBuffer *ring)
{
- return av_fifo_space(ring->fifo);
+ return av_fifo_can_write(ring->fifo);
}
-static int ring_generic_read(RingBuffer *ring, void *dest, int buf_size, void (*func)(void*, void*, int))
+static int ring_read(RingBuffer *ring, void *dest, int buf_size)
{
- int ret;
+ int ret = 0;
av_assert2(buf_size <= ring_size(ring));
- ret = av_fifo_generic_peek_at(ring->fifo, dest, ring->read_pos, buf_size, func);
+ if (dest)
+ ret = av_fifo_peek(ring->fifo, dest, buf_size, ring->read_pos);
ring->read_pos += buf_size;
if (ring->read_pos > ring->read_back_capacity) {
- av_fifo_drain(ring->fifo, ring->read_pos - ring->read_back_capacity);
+ av_fifo_drain2(ring->fifo, ring->read_pos - ring->read_back_capacity);
ring->read_pos = ring->read_back_capacity;
}
return ret;
}
-static int ring_generic_write(RingBuffer *ring, void *src, int size, int (*func)(void*, void*, int))
+static int wrapped_url_read(void *src, void *dst, size_t *size)
+{
+ URLContext *h = src;
+ Context *c = h->priv_data;
+ int ret;
+
+ ret = ffurl_read(c->inner, dst, *size);
+ *size = ret > 0 ? ret : 0;
+ c->inner_io_error = ret < 0 ? ret : 0;
+
+ return c->inner_io_error;
+}
+
+static int ring_write(RingBuffer *ring, URLContext *h, size_t size)
{
av_assert2(size <= ring_space(ring));
- return av_fifo_generic_write(ring->fifo, src, size, func);
+ return av_fifo_write_from_cb(ring->fifo, wrapped_url_read, h, &size);
}
static int ring_size_of_read_back(RingBuffer *ring)
@@ -161,18 +175,6 @@ static int async_check_interrupt(void *arg)
return c->abort_request;
}
-static int wrapped_url_read(void *src, void *dst, int size)
-{
- URLContext *h = src;
- Context *c = h->priv_data;
- int ret;
-
- ret = ffurl_read(c->inner, dst, size);
- c->inner_io_error = ret < 0 ? ret : 0;
-
- return ret;
-}
-
static void *async_buffer_task(void *arg)
{
URLContext *h = arg;
@@ -221,7 +223,7 @@ static void *async_buffer_task(void *arg)
pthread_mutex_unlock(&c->mutex);
to_copy = FFMIN(4096, fifo_space);
- ret = ring_generic_write(ring, (void *)h, to_copy, wrapped_url_read);
+ ret = ring_write(ring, h, to_copy);
pthread_mutex_lock(&c->mutex);
if (ret <= 0) {
@@ -327,11 +329,11 @@ static int async_close(URLContext *h)
return 0;
}
-static int async_read_internal(URLContext *h, void *dest, int size, int read_complete,
- void (*func)(void*, void*, int))
+static int async_read_internal(URLContext *h, void *dest, int size)
{
Context *c = h->priv_data;
RingBuffer *ring = &c->ring;
+ int read_complete = !dest;
int to_read = size;
int ret = 0;
@@ -346,8 +348,8 @@ static int async_read_internal(URLContext *h, void *dest, int size, int read_com
fifo_size = ring_size(ring);
to_copy = FFMIN(to_read, fifo_size);
if (to_copy > 0) {
- ring_generic_read(ring, dest, to_copy, func);
- if (!func)
+ ring_read(ring, dest, to_copy);
+ if (dest)
dest = (uint8_t *)dest + to_copy;
c->logical_pos += to_copy;
to_read -= to_copy;
@@ -376,11 +378,7 @@ static int async_read_internal(URLContext *h, void *dest, int size, int read_com
static int async_read(URLContext *h, unsigned char *buf, int size)
{
- return async_read_internal(h, buf, size, 0, NULL);
-}
-
-static void fifo_do_not_copy_func(void* dest, void* src, int size) {
- // do not copy
+ return async_read_internal(h, buf, size);
}
static int64_t async_seek(URLContext *h, int64_t pos, int whence)
@@ -422,7 +420,7 @@ static int64_t async_seek(URLContext *h, int64_t pos, int whence)
if (pos_delta > 0) {
// fast seek forwards
- async_read_internal(h, NULL, pos_delta, 1, fifo_do_not_copy_func);
+ async_read_internal(h, NULL, pos_delta);
} else {
// fast seek backwards
ring_drain(ring, pos_delta);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 28/35] lavd/jack: switch to the new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (25 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 27/35] lavf/async: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 29/35] lavu/audio_fifo: drop an unnecessary include Anton Khirnov
` (8 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavdevice/jack.c | 21 +++++++++++----------
1 file changed, 11 insertions(+), 10 deletions(-)
diff --git a/libavdevice/jack.c b/libavdevice/jack.c
index 0d5465e407..eb75cc9dc6 100644
--- a/libavdevice/jack.c
+++ b/libavdevice/jack.c
@@ -81,13 +81,14 @@ static int process_callback(jack_nframes_t nframes, void *arg)
self->buffer_size);
/* Check if an empty packet is available, and if there's enough space to send it back once filled */
- if ((av_fifo_size(self->new_pkts) < sizeof(pkt)) || (av_fifo_space(self->filled_pkts) < sizeof(pkt))) {
+ if (!av_fifo_can_read(self->new_pkts) ||
+ !av_fifo_can_write(self->filled_pkts)) {
self->pkt_xrun = 1;
return 0;
}
/* Retrieve empty (but allocated) packet */
- av_fifo_generic_read(self->new_pkts, &pkt, sizeof(pkt), NULL);
+ av_fifo_read(self->new_pkts, &pkt, 1);
pkt_data = (float *) pkt.data;
latency = 0;
@@ -106,7 +107,7 @@ static int process_callback(jack_nframes_t nframes, void *arg)
pkt.pts = (cycle_time - (double) latency / (self->nports * self->sample_rate)) * 1000000.0;
/* Send the now filled packet back, and increase packet counter */
- av_fifo_generic_write(self->filled_pkts, &pkt, sizeof(pkt), NULL);
+ av_fifo_write(self->filled_pkts, &pkt, 1);
sem_post(&self->packet_count);
return 0;
@@ -134,12 +135,12 @@ static int supply_new_packets(JackData *self, AVFormatContext *context)
/* Supply the process callback with new empty packets, by filling the new
* packets FIFO buffer with as many packets as possible. process_callback()
* can't do this by itself, because it can't allocate memory in realtime. */
- while (av_fifo_space(self->new_pkts) >= sizeof(pkt)) {
+ while (av_fifo_can_write(self->new_pkts)) {
if ((test = av_new_packet(&pkt, pkt_size)) < 0) {
av_log(context, AV_LOG_ERROR, "Could not create packet of size %d\n", pkt_size);
return test;
}
- av_fifo_generic_write(self->new_pkts, &pkt, sizeof(pkt), NULL);
+ av_fifo_write(self->new_pkts, &pkt, 1);
}
return 0;
}
@@ -193,9 +194,9 @@ static int start_jack(AVFormatContext *context)
}
/* Create FIFO buffers */
- self->filled_pkts = av_fifo_alloc_array(FIFO_PACKETS_NUM, sizeof(AVPacket));
+ self->filled_pkts = av_fifo_alloc2(FIFO_PACKETS_NUM, sizeof(AVPacket), 0);
/* New packets FIFO with one extra packet for safety against underruns */
- self->new_pkts = av_fifo_alloc_array((FIFO_PACKETS_NUM + 1), sizeof(AVPacket));
+ self->new_pkts = av_fifo_alloc2((FIFO_PACKETS_NUM + 1), sizeof(AVPacket), 0);
if (!self->new_pkts) {
jack_client_close(self->client);
return AVERROR(ENOMEM);
@@ -212,8 +213,8 @@ static int start_jack(AVFormatContext *context)
static void free_pkt_fifo(AVFifoBuffer **fifo)
{
AVPacket pkt;
- while (av_fifo_size(*fifo)) {
- av_fifo_generic_read(*fifo, &pkt, sizeof(pkt), NULL);
+ while (av_fifo_can_read(*fifo)) {
+ av_fifo_read(*fifo, &pkt, 1);
av_packet_unref(&pkt);
}
av_fifo_freep(fifo);
@@ -313,7 +314,7 @@ static int audio_read_packet(AVFormatContext *context, AVPacket *pkt)
}
/* Retrieve the packet filled with audio data by process_callback() */
- av_fifo_generic_read(self->filled_pkts, pkt, sizeof(*pkt), NULL);
+ av_fifo_read(self->filled_pkts, pkt, 1);
if ((test = supply_new_packets(self, context)))
return test;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 29/35] lavu/audio_fifo: drop an unnecessary include
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (26 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 28/35] lavd/jack: switch to the " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 30/35] lavu/audio_fifo: switch to new FIFO API Anton Khirnov
` (7 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
Nothing in audio_fifo.h uses anything from fifo.h
---
libavutil/audio_fifo.h | 1 -
1 file changed, 1 deletion(-)
diff --git a/libavutil/audio_fifo.h b/libavutil/audio_fifo.h
index d8a9194a8d..9d570b04c0 100644
--- a/libavutil/audio_fifo.h
+++ b/libavutil/audio_fifo.h
@@ -28,7 +28,6 @@
#define AVUTIL_AUDIO_FIFO_H
#include "avutil.h"
-#include "fifo.h"
#include "samplefmt.h"
/**
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 30/35] lavu/audio_fifo: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (27 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 29/35] lavu/audio_fifo: drop an unnecessary include Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: " Anton Khirnov
` (6 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavutil/audio_fifo.c | 38 ++++++++++++++------------------------
1 file changed, 14 insertions(+), 24 deletions(-)
diff --git a/libavutil/audio_fifo.c b/libavutil/audio_fifo.c
index 243efc39e4..1ab0c9bcdc 100644
--- a/libavutil/audio_fifo.c
+++ b/libavutil/audio_fifo.c
@@ -80,7 +80,7 @@ AVAudioFifo *av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels,
goto error;
for (i = 0; i < af->nb_buffers; i++) {
- af->buf[i] = av_fifo_alloc(buf_size);
+ af->buf[i] = av_fifo_alloc2(buf_size, 1, 0);
if (!af->buf[i])
goto error;
}
@@ -95,15 +95,19 @@ error:
int av_audio_fifo_realloc(AVAudioFifo *af, int nb_samples)
{
+ const size_t cur_size = av_fifo_can_read (af->buf[0]) +
+ av_fifo_can_write(af->buf[0]);
int i, ret, buf_size;
if ((ret = av_samples_get_buffer_size(&buf_size, af->channels, nb_samples,
af->sample_fmt, 1)) < 0)
return ret;
- for (i = 0; i < af->nb_buffers; i++) {
- if ((ret = av_fifo_realloc2(af->buf[i], buf_size)) < 0)
- return ret;
+ if (buf_size > cur_size) {
+ for (i = 0; i < af->nb_buffers; i++) {
+ if ((ret = av_fifo_grow2(af->buf[i], buf_size - cur_size)) < 0)
+ return ret;
+ }
}
af->allocated_samples = nb_samples;
return 0;
@@ -126,8 +130,8 @@ int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
size = nb_samples * af->sample_size;
for (i = 0; i < af->nb_buffers; i++) {
- ret = av_fifo_generic_write(af->buf[i], data[i], size, NULL);
- if (ret != size)
+ ret = av_fifo_write(af->buf[i], data[i], size);
+ if (ret < 0)
return AVERROR_BUG;
}
af->nb_samples += nb_samples;
@@ -137,21 +141,7 @@ int av_audio_fifo_write(AVAudioFifo *af, void **data, int nb_samples)
int av_audio_fifo_peek(AVAudioFifo *af, void **data, int nb_samples)
{
- int i, ret, size;
-
- if (nb_samples < 0)
- return AVERROR(EINVAL);
- nb_samples = FFMIN(nb_samples, af->nb_samples);
- if (!nb_samples)
- return 0;
-
- size = nb_samples * af->sample_size;
- for (i = 0; i < af->nb_buffers; i++) {
- if ((ret = av_fifo_generic_peek(af->buf[i], data[i], size, NULL)) < 0)
- return AVERROR_BUG;
- }
-
- return nb_samples;
+ return av_audio_fifo_peek_at(af, data, nb_samples, 0);
}
int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offset)
@@ -171,7 +161,7 @@ int av_audio_fifo_peek_at(AVAudioFifo *af, void **data, int nb_samples, int offs
offset *= af->sample_size;
size = nb_samples * af->sample_size;
for (i = 0; i < af->nb_buffers; i++) {
- if ((ret = av_fifo_generic_peek_at(af->buf[i], data[i], offset, size, NULL)) < 0)
+ if ((ret = av_fifo_peek(af->buf[i], data[i], size, offset)) < 0)
return AVERROR_BUG;
}
@@ -190,7 +180,7 @@ int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
size = nb_samples * af->sample_size;
for (i = 0; i < af->nb_buffers; i++) {
- if (av_fifo_generic_read(af->buf[i], data[i], size, NULL) < 0)
+ if (av_fifo_read(af->buf[i], data[i], size) < 0)
return AVERROR_BUG;
}
af->nb_samples -= nb_samples;
@@ -209,7 +199,7 @@ int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
if (nb_samples) {
size = nb_samples * af->sample_size;
for (i = 0; i < af->nb_buffers; i++)
- av_fifo_drain(af->buf[i], size);
+ av_fifo_drain2(af->buf[i], size);
af->nb_samples -= nb_samples;
}
return 0;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (28 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 30/35] lavu/audio_fifo: switch to new FIFO API Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-13 19:03 ` Andreas Rheinhardt
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 32/35] lavfi/qsvvpp: " Anton Khirnov
` (5 subsequent siblings)
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavutil/threadmessage.c | 34 +++++++++++++++++-----------------
1 file changed, 17 insertions(+), 17 deletions(-)
diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c
index 764b7fb813..39d3525a78 100644
--- a/libavutil/threadmessage.c
+++ b/libavutil/threadmessage.c
@@ -64,7 +64,7 @@ int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
av_free(rmq);
return AVERROR(ret);
}
- if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
+ if (!(rmq->fifo = av_fifo_alloc2(nelem, elsize, 0))) {
pthread_cond_destroy(&rmq->cond_send);
pthread_cond_destroy(&rmq->cond_recv);
pthread_mutex_destroy(&rmq->lock);
@@ -107,9 +107,9 @@ int av_thread_message_queue_nb_elems(AVThreadMessageQueue *mq)
#if HAVE_THREADS
int ret;
pthread_mutex_lock(&mq->lock);
- ret = av_fifo_size(mq->fifo);
+ ret = av_fifo_can_read(mq->fifo);
pthread_mutex_unlock(&mq->lock);
- return ret / mq->elsize;
+ return ret;
#else
return AVERROR(ENOSYS);
#endif
@@ -121,14 +121,14 @@ static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
void *msg,
unsigned flags)
{
- while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
+ while (!mq->err_send && !av_fifo_can_write(mq->fifo)) {
if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
return AVERROR(EAGAIN);
pthread_cond_wait(&mq->cond_send, &mq->lock);
}
if (mq->err_send)
return mq->err_send;
- av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
+ av_fifo_write(mq->fifo, msg, 1);
/* one message is sent, signal one receiver */
pthread_cond_signal(&mq->cond_recv);
return 0;
@@ -138,14 +138,14 @@ static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
void *msg,
unsigned flags)
{
- while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
+ while (!mq->err_recv && !av_fifo_can_read(mq->fifo)) {
if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
return AVERROR(EAGAIN);
pthread_cond_wait(&mq->cond_recv, &mq->lock);
}
- if (av_fifo_size(mq->fifo) < mq->elsize)
+ if (!av_fifo_can_read(mq->fifo))
return mq->err_recv;
- av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
+ av_fifo_read(mq->fifo, msg, 1);
/* one message space appeared, signal one sender */
pthread_cond_signal(&mq->cond_send);
return 0;
@@ -208,25 +208,25 @@ void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
}
#if HAVE_THREADS
-static void free_func_wrap(void *arg, void *msg, int size)
+static int free_func_wrap(void *arg, void *buf, size_t *nb_elems)
{
AVThreadMessageQueue *mq = arg;
- mq->free_func(msg);
+ uint8_t *msg = buf;
+ for (size_t i = 0; i < *nb_elems; i++)
+ mq->free_func(msg + i * mq->elsize);
+ return 0;
}
#endif
void av_thread_message_flush(AVThreadMessageQueue *mq)
{
#if HAVE_THREADS
- int used, off;
- void *free_func = mq->free_func;
+ size_t used;
pthread_mutex_lock(&mq->lock);
- used = av_fifo_size(mq->fifo);
- if (free_func)
- for (off = 0; off < used; off += mq->elsize)
- av_fifo_generic_peek_at(mq->fifo, mq, off, mq->elsize, free_func_wrap);
- av_fifo_drain(mq->fifo, used);
+ used = av_fifo_can_read(mq->fifo);
+ if (mq->free_func)
+ av_fifo_read_to_cb(mq->fifo, free_func_wrap, mq, &used);
/* only the senders need to be notified since the queue is empty and there
* is nothing to read */
pthread_cond_broadcast(&mq->cond_send);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: switch to new FIFO API
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: " Anton Khirnov
@ 2022-01-13 19:03 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 19:03 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> ---
> libavutil/threadmessage.c | 34 +++++++++++++++++-----------------
> 1 file changed, 17 insertions(+), 17 deletions(-)
>
> diff --git a/libavutil/threadmessage.c b/libavutil/threadmessage.c
> index 764b7fb813..39d3525a78 100644
> --- a/libavutil/threadmessage.c
> +++ b/libavutil/threadmessage.c
> @@ -64,7 +64,7 @@ int av_thread_message_queue_alloc(AVThreadMessageQueue **mq,
> av_free(rmq);
> return AVERROR(ret);
> }
> - if (!(rmq->fifo = av_fifo_alloc(elsize * nelem))) {
> + if (!(rmq->fifo = av_fifo_alloc2(nelem, elsize, 0))) {
> pthread_cond_destroy(&rmq->cond_send);
> pthread_cond_destroy(&rmq->cond_recv);
> pthread_mutex_destroy(&rmq->lock);
> @@ -107,9 +107,9 @@ int av_thread_message_queue_nb_elems(AVThreadMessageQueue *mq)
> #if HAVE_THREADS
> int ret;
> pthread_mutex_lock(&mq->lock);
> - ret = av_fifo_size(mq->fifo);
> + ret = av_fifo_can_read(mq->fifo);
> pthread_mutex_unlock(&mq->lock);
> - return ret / mq->elsize;
> + return ret;
> #else
> return AVERROR(ENOSYS);
> #endif
> @@ -121,14 +121,14 @@ static int av_thread_message_queue_send_locked(AVThreadMessageQueue *mq,
> void *msg,
> unsigned flags)
> {
> - while (!mq->err_send && av_fifo_space(mq->fifo) < mq->elsize) {
> + while (!mq->err_send && !av_fifo_can_write(mq->fifo)) {
> if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
> return AVERROR(EAGAIN);
> pthread_cond_wait(&mq->cond_send, &mq->lock);
> }
> if (mq->err_send)
> return mq->err_send;
> - av_fifo_generic_write(mq->fifo, msg, mq->elsize, NULL);
> + av_fifo_write(mq->fifo, msg, 1);
> /* one message is sent, signal one receiver */
> pthread_cond_signal(&mq->cond_recv);
> return 0;
> @@ -138,14 +138,14 @@ static int av_thread_message_queue_recv_locked(AVThreadMessageQueue *mq,
> void *msg,
> unsigned flags)
> {
> - while (!mq->err_recv && av_fifo_size(mq->fifo) < mq->elsize) {
> + while (!mq->err_recv && !av_fifo_can_read(mq->fifo)) {
> if ((flags & AV_THREAD_MESSAGE_NONBLOCK))
> return AVERROR(EAGAIN);
> pthread_cond_wait(&mq->cond_recv, &mq->lock);
> }
> - if (av_fifo_size(mq->fifo) < mq->elsize)
> + if (!av_fifo_can_read(mq->fifo))
> return mq->err_recv;
> - av_fifo_generic_read(mq->fifo, msg, mq->elsize, NULL);
> + av_fifo_read(mq->fifo, msg, 1);
> /* one message space appeared, signal one sender */
> pthread_cond_signal(&mq->cond_send);
> return 0;
> @@ -208,25 +208,25 @@ void av_thread_message_queue_set_err_recv(AVThreadMessageQueue *mq,
> }
>
> #if HAVE_THREADS
> -static void free_func_wrap(void *arg, void *msg, int size)
> +static int free_func_wrap(void *arg, void *buf, size_t *nb_elems)
> {
> AVThreadMessageQueue *mq = arg;
> - mq->free_func(msg);
> + uint8_t *msg = buf;
> + for (size_t i = 0; i < *nb_elems; i++)
> + mq->free_func(msg + i * mq->elsize);
> + return 0;
This function (like most nontrivial callbacks) relies on buf being
suitably aligned for whatever type msg is. Therefore one should document
that the src/dst in the callbacks is always suitable aligned for all
non-over-aligned types.
> }
> #endif
>
> void av_thread_message_flush(AVThreadMessageQueue *mq)
> {
> #if HAVE_THREADS
> - int used, off;
> - void *free_func = mq->free_func;
> + size_t used;
>
> pthread_mutex_lock(&mq->lock);
> - used = av_fifo_size(mq->fifo);
> - if (free_func)
> - for (off = 0; off < used; off += mq->elsize)
> - av_fifo_generic_peek_at(mq->fifo, mq, off, mq->elsize, free_func_wrap);
> - av_fifo_drain(mq->fifo, used);
> + used = av_fifo_can_read(mq->fifo);
> + if (mq->free_func)
> + av_fifo_read_to_cb(mq->fifo, free_func_wrap, mq, &used);
> /* only the senders need to be notified since the queue is empty and there
> * is nothing to read */
> pthread_cond_broadcast(&mq->cond_send);
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 32/35] lavfi/qsvvpp: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (29 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 33/35] lavfi/vf_deshake_opencl: " Anton Khirnov
` (4 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavfilter/qsvvpp.c | 46 +++++++++++++++++++-------------------------
1 file changed, 20 insertions(+), 26 deletions(-)
diff --git a/libavfilter/qsvvpp.c b/libavfilter/qsvvpp.c
index d1218355c7..60da1d47c5 100644
--- a/libavfilter/qsvvpp.c
+++ b/libavfilter/qsvvpp.c
@@ -40,6 +40,11 @@
static const AVRational default_tb = { 1, 90000 };
+typedef struct QSVAsyncFrame {
+ mfxSyncPoint sync;
+ QSVFrame *frame;
+} QSVAsyncFrame;
+
static const struct {
int mfx_iopattern;
const char *desc;
@@ -642,16 +647,6 @@ static int init_vpp_session(AVFilterContext *avctx, QSVVPPContext *s)
return 0;
}
-static unsigned int qsv_fifo_item_size(void)
-{
- return sizeof(mfxSyncPoint) + sizeof(QSVFrame*);
-}
-
-static unsigned int qsv_fifo_size(const AVFifoBuffer* fifo)
-{
- return av_fifo_size(fifo)/qsv_fifo_item_size();
-}
-
int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *param)
{
int i;
@@ -727,7 +722,7 @@ int ff_qsvvpp_create(AVFilterContext *avctx, QSVVPPContext **vpp, QSVVPPParam *p
s->got_frame = 0;
/** keep fifo size at least 1. Even when async_depth is 0, fifo is used. */
- s->async_fifo = av_fifo_alloc((param->async_depth + 1) * qsv_fifo_item_size());
+ s->async_fifo = av_fifo_alloc2(param->async_depth + 1, sizeof(QSVAsyncFrame), 0);
s->async_depth = param->async_depth;
if (!s->async_fifo) {
ret = AVERROR(ENOMEM);
@@ -799,24 +794,25 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr
{
AVFilterContext *ctx = inlink->dst;
AVFilterLink *outlink = ctx->outputs[0];
+ QSVAsyncFrame aframe;
mfxSyncPoint sync;
QSVFrame *in_frame, *out_frame, *tmp;
int ret, filter_ret;
- while (s->eof && qsv_fifo_size(s->async_fifo)) {
- av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL);
- av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL);
- if (MFXVideoCORE_SyncOperation(s->session, sync, 1000) < 0)
+ while (s->eof && av_fifo_can_read(s->async_fifo)) {
+ av_fifo_read(s->async_fifo, &aframe, 1);
+
+ if (MFXVideoCORE_SyncOperation(s->session, aframe.sync, 1000) < 0)
av_log(ctx, AV_LOG_WARNING, "Sync failed.\n");
- filter_ret = s->filter_frame(outlink, tmp->frame);
+ filter_ret = s->filter_frame(outlink, aframe.frame->frame);
if (filter_ret < 0) {
- av_frame_free(&tmp->frame);
+ av_frame_free(&aframe.frame->frame);
return filter_ret;
}
- tmp->queued--;
+ aframe.frame->queued--;
s->got_frame = 1;
- tmp->frame = NULL;
+ aframe.frame->frame = NULL;
};
if (!picref)
@@ -853,16 +849,14 @@ int ff_qsvvpp_filter_frame(QSVVPPContext *s, AVFilterLink *inlink, AVFrame *picr
default_tb, outlink->time_base);
out_frame->queued++;
- av_fifo_generic_write(s->async_fifo, &out_frame, sizeof(out_frame), NULL);
- av_fifo_generic_write(s->async_fifo, &sync, sizeof(sync), NULL);
-
+ aframe = (QSVAsyncFrame){ sync, out_frame };
+ av_fifo_write(s->async_fifo, &aframe, 1);
- if (qsv_fifo_size(s->async_fifo) > s->async_depth) {
- av_fifo_generic_read(s->async_fifo, &tmp, sizeof(tmp), NULL);
- av_fifo_generic_read(s->async_fifo, &sync, sizeof(sync), NULL);
+ if (av_fifo_can_read(s->async_fifo) > s->async_depth) {
+ av_fifo_read(s->async_fifo, &aframe, 1);
do {
- ret = MFXVideoCORE_SyncOperation(s->session, sync, 1000);
+ ret = MFXVideoCORE_SyncOperation(s->session, aframe.sync, 1000);
} while (ret == MFX_WRN_IN_EXECUTION);
filter_ret = s->filter_frame(outlink, tmp->frame);
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 33/35] lavfi/vf_deshake_opencl: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (30 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 32/35] lavfi/qsvvpp: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 34/35] ffplay: " Anton Khirnov
` (3 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
libavfilter/vf_deshake_opencl.c | 80 ++++++++++-----------------------
1 file changed, 24 insertions(+), 56 deletions(-)
diff --git a/libavfilter/vf_deshake_opencl.c b/libavfilter/vf_deshake_opencl.c
index 9c761ba5ad..c8ad1d2a4a 100644
--- a/libavfilter/vf_deshake_opencl.c
+++ b/libavfilter/vf_deshake_opencl.c
@@ -156,14 +156,8 @@ static void free_debug_matches(AbsoluteFrameMotion *afm) {
return;
}
- while (av_fifo_size(afm->debug_matches) > 0) {
- av_fifo_generic_read(
- afm->debug_matches,
- &dm,
- sizeof(DebugMatches),
- NULL
- );
-
+ while (av_fifo_can_read(afm->debug_matches)) {
+ av_fifo_read(afm->debug_matches, &dm, 1);
av_freep(&dm.matches);
}
@@ -863,7 +857,7 @@ static void ringbuf_float_at(
} else {
// This expression represents the last valid index in the buffer,
// which we use repeatedly at the end of the video.
- clip_end = deshake_ctx->smooth_window - (av_fifo_space(values) / sizeof(float)) - 1;
+ clip_end = deshake_ctx->smooth_window - av_fifo_can_write(values) - 1;
}
if (deshake_ctx->abs_motion.data_start_offset != -1) {
@@ -881,13 +875,7 @@ static void ringbuf_float_at(
clip_end
);
- av_fifo_generic_peek_at(
- values,
- val,
- offset_clipped * sizeof(float),
- sizeof(float),
- NULL
- );
+ av_fifo_peek(values, val, 1, offset_clipped);
}
// Returns smoothed current frame value of the given buffer of floats based on the
@@ -1188,10 +1176,8 @@ static int deshake_opencl_init(AVFilterContext *avctx)
}
for (int i = 0; i < RingbufCount; i++) {
- ctx->abs_motion.ringbuffers[i] = av_fifo_alloc_array(
- ctx->smooth_window,
- sizeof(float)
- );
+ ctx->abs_motion.ringbuffers[i] = av_fifo_alloc2(ctx->smooth_window,
+ sizeof(float), 0);
if (!ctx->abs_motion.ringbuffers[i]) {
err = AVERROR(ENOMEM);
@@ -1200,9 +1186,9 @@ static int deshake_opencl_init(AVFilterContext *avctx)
}
if (ctx->debug_on) {
- ctx->abs_motion.debug_matches = av_fifo_alloc_array(
+ ctx->abs_motion.debug_matches = av_fifo_alloc2(
ctx->smooth_window / 2,
- sizeof(DebugMatches)
+ sizeof(DebugMatches), 0
);
if (!ctx->abs_motion.debug_matches) {
@@ -1424,12 +1410,9 @@ static int filter_frame(AVFilterLink *link, AVFrame *input_frame)
const float luma_h_over_chroma_h = ((float)input_frame->height / (float)chroma_height);
if (deshake_ctx->debug_on) {
- av_fifo_generic_read(
+ av_fifo_read(
deshake_ctx->abs_motion.debug_matches,
- &debug_matches,
- sizeof(DebugMatches),
- NULL
- );
+ &debug_matches, 1);
}
if (input_frame->pkt_duration) {
@@ -1441,13 +1424,9 @@ static int filter_frame(AVFilterLink *link, AVFrame *input_frame)
// Get the absolute transform data for this frame
for (int i = 0; i < RingbufCount; i++) {
- av_fifo_generic_peek_at(
- deshake_ctx->abs_motion.ringbuffers[i],
- &old_vals[i],
- deshake_ctx->abs_motion.curr_frame_offset * sizeof(float),
- sizeof(float),
- NULL
- );
+ av_fifo_peek(deshake_ctx->abs_motion.ringbuffers[i],
+ &old_vals[i], 1,
+ deshake_ctx->abs_motion.curr_frame_offset);
}
if (deshake_ctx->tripod_mode) {
@@ -1842,7 +1821,7 @@ static int queue_frame(AVFilterLink *link, AVFrame *input_frame)
{ sizeof(cl_mem), &deshake_ctx->brief_pattern}
);
- if (av_fifo_size(deshake_ctx->abs_motion.ringbuffers[RingbufX]) == 0) {
+ if (!av_fifo_can_read(deshake_ctx->abs_motion.ringbuffers[RingbufX])) {
// This is the first frame we've been given to queue, meaning there is
// no previous frame to match descriptors to
@@ -1892,7 +1871,7 @@ static int queue_frame(AVFilterLink *link, AVFrame *input_frame)
// old data (and just treat them all as part of the new values)
if (deshake_ctx->abs_motion.data_end_offset == -1) {
deshake_ctx->abs_motion.data_end_offset =
- av_fifo_size(deshake_ctx->abs_motion.ringbuffers[RingbufX]) / sizeof(float) - 1;
+ av_fifo_can_read(deshake_ctx->abs_motion.ringbuffers[RingbufX]) - 1;
}
goto no_motion_data;
@@ -1934,13 +1913,10 @@ static int queue_frame(AVFilterLink *link, AVFrame *input_frame)
// Get the absolute transform data for the previous frame
for (int i = 0; i < RingbufCount; i++) {
- av_fifo_generic_peek_at(
+ av_fifo_peek(
deshake_ctx->abs_motion.ringbuffers[i],
- &prev_vals[i],
- av_fifo_size(deshake_ctx->abs_motion.ringbuffers[i]) - sizeof(float),
- sizeof(float),
- NULL
- );
+ &prev_vals[i], 1,
+ av_fifo_can_read(deshake_ctx->abs_motion.ringbuffers[i]) - 1);
}
new_vals[RingbufX] = prev_vals[RingbufX] + relative.translation.s[0];
@@ -2011,21 +1987,13 @@ end:
}
debug_matches.num_matches = num_vectors;
- av_fifo_generic_write(
+ av_fifo_write(
deshake_ctx->abs_motion.debug_matches,
- &debug_matches,
- sizeof(DebugMatches),
- NULL
- );
+ &debug_matches, 1);
}
for (int i = 0; i < RingbufCount; i++) {
- av_fifo_generic_write(
- deshake_ctx->abs_motion.ringbuffers[i],
- &new_vals[i],
- sizeof(float),
- NULL
- );
+ av_fifo_write(deshake_ctx->abs_motion.ringbuffers[i], &new_vals[i], 1);
}
return ff_framequeue_add(&deshake_ctx->fq, input_frame);
@@ -2063,9 +2031,9 @@ static int activate(AVFilterContext *ctx)
// If there is no more space in the ringbuffers, remove the oldest
// values to make room for the new ones
- if (av_fifo_space(deshake_ctx->abs_motion.ringbuffers[RingbufX]) == 0) {
+ if (!av_fifo_can_write(deshake_ctx->abs_motion.ringbuffers[RingbufX])) {
for (int i = 0; i < RingbufCount; i++) {
- av_fifo_drain(deshake_ctx->abs_motion.ringbuffers[i], sizeof(float));
+ av_fifo_drain2(deshake_ctx->abs_motion.ringbuffers[i], 1);
}
}
ret = queue_frame(inlink, frame);
@@ -2092,7 +2060,7 @@ static int activate(AVFilterContext *ctx)
// Finish processing the rest of the frames in the queue.
while(ff_framequeue_queued_frames(&deshake_ctx->fq) != 0) {
for (int i = 0; i < RingbufCount; i++) {
- av_fifo_drain(deshake_ctx->abs_motion.ringbuffers[i], sizeof(float));
+ av_fifo_drain2(deshake_ctx->abs_motion.ringbuffers[i], 1);
}
ret = filter_frame(inlink, ff_framequeue_take(&deshake_ctx->fq));
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 34/35] ffplay: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (31 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 33/35] lavfi/vf_deshake_opencl: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 35/35] ffmpeg: " Anton Khirnov
` (2 subsequent siblings)
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
fftools/ffplay.c | 19 +++++++++----------
1 file changed, 9 insertions(+), 10 deletions(-)
diff --git a/fftools/ffplay.c b/fftools/ffplay.c
index e7b20be76b..4bef9a6ecc 100644
--- a/fftools/ffplay.c
+++ b/fftools/ffplay.c
@@ -424,19 +424,18 @@ int64_t get_valid_channel_layout(int64_t channel_layout, int channels)
static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
{
MyAVPacketList pkt1;
+ int ret;
if (q->abort_request)
return -1;
- if (av_fifo_space(q->pkt_list) < sizeof(pkt1)) {
- if (av_fifo_grow(q->pkt_list, sizeof(pkt1)) < 0)
- return -1;
- }
pkt1.pkt = pkt;
pkt1.serial = q->serial;
- av_fifo_generic_write(q->pkt_list, &pkt1, sizeof(pkt1), NULL);
+ ret = av_fifo_write(q->pkt_list, &pkt1, 1);
+ if (ret < 0)
+ return ret;
q->nb_packets++;
q->size += pkt1.pkt->size + sizeof(pkt1);
q->duration += pkt1.pkt->duration;
@@ -477,7 +476,7 @@ static int packet_queue_put_nullpacket(PacketQueue *q, AVPacket *pkt, int stream
static int packet_queue_init(PacketQueue *q)
{
memset(q, 0, sizeof(PacketQueue));
- q->pkt_list = av_fifo_alloc(sizeof(MyAVPacketList));
+ q->pkt_list = av_fifo_alloc2(1, sizeof(MyAVPacketList), AV_FIFO_FLAG_AUTO_GROW);
if (!q->pkt_list)
return AVERROR(ENOMEM);
q->mutex = SDL_CreateMutex();
@@ -499,8 +498,8 @@ static void packet_queue_flush(PacketQueue *q)
MyAVPacketList pkt1;
SDL_LockMutex(q->mutex);
- while (av_fifo_size(q->pkt_list) >= sizeof(pkt1)) {
- av_fifo_generic_read(q->pkt_list, &pkt1, sizeof(pkt1), NULL);
+ while (av_fifo_can_read(q->pkt_list)) {
+ av_fifo_read(q->pkt_list, &pkt1, 1);
av_packet_free(&pkt1.pkt);
}
q->nb_packets = 0;
@@ -551,8 +550,8 @@ static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *seria
break;
}
- if (av_fifo_size(q->pkt_list) >= sizeof(pkt1)) {
- av_fifo_generic_read(q->pkt_list, &pkt1, sizeof(pkt1), NULL);
+ if (av_fifo_can_read(q->pkt_list)) {
+ av_fifo_read(q->pkt_list, &pkt1, 1);
q->nb_packets--;
q->size -= pkt1.pkt->size + sizeof(pkt1);
q->duration -= pkt1.pkt->duration;
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* [FFmpeg-devel] [PATCH 35/35] ffmpeg: switch to new FIFO API
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (32 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 34/35] ffplay: " Anton Khirnov
@ 2022-01-11 20:46 ` Anton Khirnov
2022-01-13 13:59 ` [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Andreas Rheinhardt
2022-01-13 14:27 ` Anton Khirnov
35 siblings, 0 replies; 53+ messages in thread
From: Anton Khirnov @ 2022-01-11 20:46 UTC (permalink / raw)
To: ffmpeg-devel
---
fftools/ffmpeg.c | 36 ++++++++++++++----------------------
fftools/ffmpeg_filter.c | 12 ++++++------
2 files changed, 20 insertions(+), 28 deletions(-)
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c89a95937e..319e295df3 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -525,19 +525,17 @@ static void ffmpeg_cleanup(int ret)
InputFilter *ifilter = fg->inputs[j];
struct InputStream *ist = ifilter->ist;
- while (av_fifo_size(ifilter->frame_queue)) {
+ while (av_fifo_can_read(ifilter->frame_queue)) {
AVFrame *frame;
- av_fifo_generic_read(ifilter->frame_queue, &frame,
- sizeof(frame), NULL);
+ av_fifo_read(ifilter->frame_queue, &frame, 1);
av_frame_free(&frame);
}
av_fifo_freep(&ifilter->frame_queue);
av_freep(&ifilter->displaymatrix);
if (ist->sub2video.sub_queue) {
- while (av_fifo_size(ist->sub2video.sub_queue)) {
+ while (av_fifo_can_read(ist->sub2video.sub_queue)) {
AVSubtitle sub;
- av_fifo_generic_read(ist->sub2video.sub_queue,
- &sub, sizeof(sub), NULL);
+ av_fifo_read(ist->sub2video.sub_queue, &sub, 1);
avsubtitle_free(&sub);
}
av_fifo_freep(&ist->sub2video.sub_queue);
@@ -1954,15 +1952,11 @@ static int ifilter_send_frame(InputFilter *ifilter, AVFrame *frame, int keep_ref
if (!tmp)
return AVERROR(ENOMEM);
- if (!av_fifo_space(ifilter->frame_queue)) {
- ret = av_fifo_realloc2(ifilter->frame_queue, 2 * av_fifo_size(ifilter->frame_queue));
- if (ret < 0) {
- av_frame_free(&tmp);
- return ret;
- }
- }
- av_fifo_generic_write(ifilter->frame_queue, &tmp, sizeof(tmp), NULL);
- return 0;
+ ret = av_fifo_write(ifilter->frame_queue, &tmp, 1);
+ if (ret < 0)
+ av_frame_free(&tmp);
+
+ return ret;
}
ret = reap_filters(1);
@@ -2285,15 +2279,13 @@ static int transcode_subtitles(InputStream *ist, AVPacket *pkt, int *got_output,
sub2video_update(ist, INT64_MIN, &subtitle);
} else if (ist->nb_filters) {
if (!ist->sub2video.sub_queue)
- ist->sub2video.sub_queue = av_fifo_alloc(8 * sizeof(AVSubtitle));
+ ist->sub2video.sub_queue = av_fifo_alloc2(8, sizeof(AVSubtitle), AV_FIFO_FLAG_AUTO_GROW);
if (!ist->sub2video.sub_queue)
exit_program(1);
- if (!av_fifo_space(ist->sub2video.sub_queue)) {
- ret = av_fifo_realloc2(ist->sub2video.sub_queue, 2 * av_fifo_size(ist->sub2video.sub_queue));
- if (ret < 0)
- exit_program(1);
- }
- av_fifo_generic_write(ist->sub2video.sub_queue, &subtitle, sizeof(subtitle), NULL);
+
+ ret = av_fifo_write(ist->sub2video.sub_queue, &subtitle, 1);
+ if (ret < 0)
+ exit_program(1);
free_sub = 0;
}
diff --git a/fftools/ffmpeg_filter.c b/fftools/ffmpeg_filter.c
index 1f6cba2c04..c468456d54 100644
--- a/fftools/ffmpeg_filter.c
+++ b/fftools/ffmpeg_filter.c
@@ -178,7 +178,7 @@ int init_simple_filtergraph(InputStream *ist, OutputStream *ost)
ifilter->graph = fg;
ifilter->format = -1;
- ifilter->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
+ ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
if (!ifilter->frame_queue)
exit_program(1);
@@ -286,7 +286,7 @@ static void init_input_filter(FilterGraph *fg, AVFilterInOut *in)
ifilter->type = ist->st->codecpar->codec_type;
ifilter->name = describe_filter_link(fg, in, 1);
- ifilter->frame_queue = av_fifo_alloc(8 * sizeof(AVFrame*));
+ ifilter->frame_queue = av_fifo_alloc2(8, sizeof(AVFrame*), AV_FIFO_FLAG_AUTO_GROW);
if (!ifilter->frame_queue)
exit_program(1);
@@ -1106,9 +1106,9 @@ int configure_filtergraph(FilterGraph *fg)
}
for (i = 0; i < fg->nb_inputs; i++) {
- while (av_fifo_size(fg->inputs[i]->frame_queue)) {
+ while (av_fifo_can_read(fg->inputs[i]->frame_queue)) {
AVFrame *tmp;
- av_fifo_generic_read(fg->inputs[i]->frame_queue, &tmp, sizeof(tmp), NULL);
+ av_fifo_read(fg->inputs[i]->frame_queue, &tmp, 1);
ret = av_buffersrc_add_frame(fg->inputs[i]->filter, tmp);
av_frame_free(&tmp);
if (ret < 0)
@@ -1129,9 +1129,9 @@ int configure_filtergraph(FilterGraph *fg)
for (i = 0; i < fg->nb_inputs; i++) {
InputStream *ist = fg->inputs[i]->ist;
if (ist->sub2video.sub_queue && ist->sub2video.frame) {
- while (av_fifo_size(ist->sub2video.sub_queue)) {
+ while (av_fifo_can_read(ist->sub2video.sub_queue)) {
AVSubtitle tmp;
- av_fifo_generic_read(ist->sub2video.sub_queue, &tmp, sizeof(tmp), NULL);
+ av_fifo_read(ist->sub2video.sub_queue, &tmp, 1);
sub2video_update(ist, INT64_MIN, &tmp);
avsubtitle_free(&tmp);
}
--
2.33.0
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (33 preceding siblings ...)
2022-01-11 20:46 ` [FFmpeg-devel] [PATCH 35/35] ffmpeg: " Anton Khirnov
@ 2022-01-13 13:59 ` Andreas Rheinhardt
2022-01-13 14:27 ` Anton Khirnov
35 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 13:59 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> The API currently allows creating FIFOs up to
> - UINT_MAX: av_fifo_alloc(), av_fifo_realloc(), av_fifo_grow()
> - SIZE_MAX: av_fifo_alloc_array()
> However the usable limit is determined by
> - rndx/wndx being uint32_t
> - av_fifo_[size,space] returning int
> so no FIFO should be larger than the smallest of
> - INT_MAX
> - UINT32_MAX
> - SIZE_MAX
> (which should be INT_MAX an all commonly used platforms).
> Return an error on trying to allocate FIFOs larger than this limit.
> ---
> libavutil/fifo.c | 14 +++++++++++++-
> 1 file changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> index d741bdd395..f2f046b1f3 100644
> --- a/libavutil/fifo.c
> +++ b/libavutil/fifo.c
> @@ -20,14 +20,23 @@
> * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> */
>
> +#include <stdint.h>
> +
> #include "avassert.h"
> #include "common.h"
> #include "fifo.h"
>
> +#define FIFO_SIZE_MAX FFMIN3((uint64_t)INT_MAX, (uint64_t)UINT32_MAX, (uint64_t)SIZE_MAX)
Aren't these casts unnecessary? And actually dangerous? (They add the
implicit requirement that INT_MAX and SIZE_MAX fit into an uint64_t.)
> +
> AVFifoBuffer *av_fifo_alloc_array(size_t nmemb, size_t size)
> {
> AVFifoBuffer *f;
> - void *buffer = av_realloc_array(NULL, nmemb, size);
> + void *buffer;
> +
> + if (nmemb > FIFO_SIZE_MAX / size)
> + return NULL;
> +
> + buffer = av_realloc_array(NULL, nmemb, size);
> if (!buffer)
> return NULL;
> f = av_mallocz(sizeof(AVFifoBuffer));
> @@ -82,6 +91,9 @@ int av_fifo_realloc2(AVFifoBuffer *f, unsigned int new_size)
> {
> unsigned int old_size = f->end - f->buffer;
>
> + if (new_size > FIFO_SIZE_MAX)
> + return AVERROR(EINVAL);
> +
> if (old_size < new_size) {
> size_t offset_r = f->rptr - f->buffer;
> size_t offset_w = f->wptr - f->buffer;
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes
2022-01-11 20:45 [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Anton Khirnov
` (34 preceding siblings ...)
2022-01-13 13:59 ` [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes Andreas Rheinhardt
@ 2022-01-13 14:27 ` Anton Khirnov
2022-01-13 14:38 ` Andreas Rheinhardt
35 siblings, 1 reply; 53+ messages in thread
From: Anton Khirnov @ 2022-01-13 14:27 UTC (permalink / raw)
To: FFmpeg development discussions and patches
Quoting Andreas Rheinhardt (2022-01-13 14:59:49)
> Anton Khirnov:
> > The API currently allows creating FIFOs up to
> > - UINT_MAX: av_fifo_alloc(), av_fifo_realloc(), av_fifo_grow()
> > - SIZE_MAX: av_fifo_alloc_array()
> > However the usable limit is determined by
> > - rndx/wndx being uint32_t
> > - av_fifo_[size,space] returning int
> > so no FIFO should be larger than the smallest of
> > - INT_MAX
> > - UINT32_MAX
> > - SIZE_MAX
> > (which should be INT_MAX an all commonly used platforms).
> > Return an error on trying to allocate FIFOs larger than this limit.
> > ---
> > libavutil/fifo.c | 14 +++++++++++++-
> > 1 file changed, 13 insertions(+), 1 deletion(-)
> >
> > diff --git a/libavutil/fifo.c b/libavutil/fifo.c
> > index d741bdd395..f2f046b1f3 100644
> > --- a/libavutil/fifo.c
> > +++ b/libavutil/fifo.c
> > @@ -20,14 +20,23 @@
> > * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> > */
> >
> > +#include <stdint.h>
> > +
> > #include "avassert.h"
> > #include "common.h"
> > #include "fifo.h"
> >
> > +#define FIFO_SIZE_MAX FFMIN3((uint64_t)INT_MAX, (uint64_t)UINT32_MAX, (uint64_t)SIZE_MAX)
>
> Aren't these casts unnecessary?
Possibly yes. I mainly added them so that FIFO_SIZE_MAX is always the
same type, which might prevent surprises. I can drop the casts if you
think they are never necessary.
> And actually dangerous? (They add the implicit requirement that
> INT_MAX and SIZE_MAX fit into an uint64_t.)
I'll grant you _theoretically_ dangerous - I've never heard of a
posix-compliant platform with sizeof(int) > 64.
And in any case that macro should be gone with the old API (was going to
include a patch scheduling its removal, but apparently forgot - will
send it later).
--
Anton Khirnov
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread
* Re: [FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes
2022-01-13 14:27 ` Anton Khirnov
@ 2022-01-13 14:38 ` Andreas Rheinhardt
0 siblings, 0 replies; 53+ messages in thread
From: Andreas Rheinhardt @ 2022-01-13 14:38 UTC (permalink / raw)
To: ffmpeg-devel
Anton Khirnov:
> Quoting Andreas Rheinhardt (2022-01-13 14:59:49)
>> Anton Khirnov:
>>> The API currently allows creating FIFOs up to
>>> - UINT_MAX: av_fifo_alloc(), av_fifo_realloc(), av_fifo_grow()
>>> - SIZE_MAX: av_fifo_alloc_array()
>>> However the usable limit is determined by
>>> - rndx/wndx being uint32_t
>>> - av_fifo_[size,space] returning int
>>> so no FIFO should be larger than the smallest of
>>> - INT_MAX
>>> - UINT32_MAX
>>> - SIZE_MAX
>>> (which should be INT_MAX an all commonly used platforms).
>>> Return an error on trying to allocate FIFOs larger than this limit.
>>> ---
>>> libavutil/fifo.c | 14 +++++++++++++-
>>> 1 file changed, 13 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/libavutil/fifo.c b/libavutil/fifo.c
>>> index d741bdd395..f2f046b1f3 100644
>>> --- a/libavutil/fifo.c
>>> +++ b/libavutil/fifo.c
>>> @@ -20,14 +20,23 @@
>>> * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
>>> */
>>>
>>> +#include <stdint.h>
>>> +
>>> #include "avassert.h"
>>> #include "common.h"
>>> #include "fifo.h"
>>>
>>> +#define FIFO_SIZE_MAX FFMIN3((uint64_t)INT_MAX, (uint64_t)UINT32_MAX, (uint64_t)SIZE_MAX)
>>
>> Aren't these casts unnecessary?
>
> Possibly yes. I mainly added them so that FIFO_SIZE_MAX is always the
> same type, which might prevent surprises. I can drop the casts if you
> think they are never necessary.
>
FIFO_SIZE is the type that results after integer promotions of an
expression involving an int, an uint32_t and a size_t parameter; all
three constants are representable in it and FIFO_SIZE is representable
in an int, an uint32_t and size_t. Given that it denotes a size I think
you should cast it to size_t if you want to avoid surprises.
>> And actually dangerous? (They add the implicit requirement that
>> INT_MAX and SIZE_MAX fit into an uint64_t.)
>
> I'll grant you _theoretically_ dangerous - I've never heard of a
> posix-compliant platform with sizeof(int) > 64.
sizeof(int) > 8 you mean? (Or even better: UINT_MAX > UINT64_MAX.)
>
> And in any case that macro should be gone with the old API (was going to
> include a patch scheduling its removal, but apparently forgot - will
> send it later).
>
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
^ permalink raw reply [flat|nested] 53+ messages in thread