Re: [FFmpeg-devel] [PATCH v2] avfilter: add overlay vaapi filter

2022-01-11 Thread Xiang, Haihao
On Mon, 2021-11-22 at 15:53 +0800, Fei Wang wrote:
> From: Xinpeng Sun 
> 
> Overlay one video on the top of another.
> 
> It takes two inputs and has one output. The first input is the "main" video on
> which the second input is overlaid. This filter requires same memory layout
> for
> all the inputs.
> 
> An example command to use this filter to overlay overlay.mp4 at the top-left
> corner of the main.mp4:
> 
> ffmpeg -init_hw_device vaapi=foo:/dev/dri/renderD128 \
> -hwaccel vaapi -hwaccel_device foo -hwaccel_output_format vaapi -c:v h264 -i
> main.mp4 \
> -hwaccel vaapi -hwaccel_device foo -hwaccel_output_format vaapi -c:v h264 -i
> overlay.mp4 \
> -filter_complex "[0:v][1:v]overlay_vaapi=0:0:100:100:0.5[t1]" \
> -map "[t1]" -an -c:v h264_vaapi -y out_vaapi.mp4
> 
> Signed-off-by: U. Artie Eoff 
> Signed-off-by: Xinpeng Sun 
> Signed-off-by: Zachary Zhou 
> Signed-off-by: Fei Wang 
> ---
> V2 update:
> 1. rebase to master.
> 
>  Changelog  |   1 +
>  configure  |   2 +
>  doc/filters.texi   |  51 
>  libavfilter/Makefile   |   1 +
>  libavfilter/allfilters.c   |   1 +
>  libavfilter/version.h  |   2 +-
>  libavfilter/vf_overlay_vaapi.c | 423 +
>  7 files changed, 480 insertions(+), 1 deletion(-)
>  create mode 100644 libavfilter/vf_overlay_vaapi.c
> 
> diff --git a/Changelog b/Changelog
> index 31a0d5ef5d..4064a17c0b 100644
> --- a/Changelog
> +++ b/Changelog
> @@ -31,6 +31,7 @@ version :
>  - varblur video filter
>  - huesaturation video filter
>  - colorspectrum source video filter
> +- overlay_vaapi filter
>  
>  
>  version 4.4:
> diff --git a/configure b/configure
> index d068b11073..6d377caa1f 100755
> --- a/configure
> +++ b/configure
> @@ -3641,6 +3641,7 @@ openclsrc_filter_deps="opencl"
>  overlay_opencl_filter_deps="opencl"
>  overlay_qsv_filter_deps="libmfx"
>  overlay_qsv_filter_select="qsvvpp"
> +overlay_vaapi_filter_deps="vaapi VAProcPipelineCaps_blend_flags"
>  overlay_vulkan_filter_deps="vulkan spirv_compiler"
>  owdenoise_filter_deps="gpl"
>  pad_opencl_filter_deps="opencl"
> @@ -6801,6 +6802,7 @@ if enabled vaapi; then
>  check_struct "va/va.h" "VADecPictureParameterBufferAV1" bit_depth_idx
>  check_type   "va/va.h va/va_vpp.h"
> "VAProcFilterParameterBufferHDRToneMapping"
>  check_struct "va/va.h va/va_vpp.h" "VAProcPipelineCaps" rotation_flags
> +check_struct "va/va.h va/va_vpp.h" "VAProcPipelineCaps" blend_flags
>  check_type "va/va.h va/va_enc_hevc.h" "VAEncPictureParameterBufferHEVC"
>  check_type "va/va.h va/va_enc_jpeg.h" "VAEncPictureParameterBufferJPEG"
>  check_type "va/va.h va/va_enc_vp8.h"  "VAEncPictureParameterBufferVP8"
> diff --git a/doc/filters.texi b/doc/filters.texi
> index c3ccaf97c4..cf6ed584de 100644
> --- a/doc/filters.texi
> +++ b/doc/filters.texi
> @@ -24628,6 +24628,57 @@ To enable compilation of these filters you need to
> configure FFmpeg with
>  
>  To use vaapi filters, you need to setup the vaapi device correctly. For more
> information, please read @url{https://trac.ffmpeg.org/wiki/Hardware/VAAPI}
>  
> +@section overlay_vaapi
> +
> +Overlay one video on the top of another.
> +
> +It takes two inputs and has one output. The first input is the "main" video
> on which the second input is overlaid.
> +This filter requires same memory layout for all the inputs. So, format
> conversion may be needed.
> +
> +The filter accepts the following options:
> +
> +@table @option
> +
> +@item x
> +Set the x coordinate of the overlaid video on the main video.
> +Default value is @code{0}.
> +
> +@item y
> +Set the y coordinate of the overlaid video on the main video.
> +Default value is @code{0}.
> +
> +@item w
> +Set the width of the overlaid video on the main video.
> +Default value is the width of input overlay video.
> +
> +@item h
> +Set the height of the overlaid video on the main video.
> +Default value is the height of input overlay video.
> +
> +@item alpha
> +Set blocking detection thresholds. Allowed range is 0.0 to 1.0, it
> +requires an input video with alpha channel.
> +Default value is @code{0.0}.
> +
> +@end table
> +
> +@subsection Examples
> +
> +@itemize
> +@item
> +Overlay an image LOGO at the top-left corner of the INPUT video. Both inputs
> for this filter are yuv420p format.
> +@example
> +-i INPUT -i LOGO -filter_complex "[0:v]hwupload[a], [1:v]format=yuv420p,
> hwupload[b], [a][b]overlay_vaapi" OUTPUT
> +@end example
> +@item
> +Overlay an image LOGO at the offset (200, 100) from the top-left corner of
> the INPUT video.
> +The inputs have same memory layout for color channels, the overlay has
> additional alpha plane, like INPUT is yuv420p, and the LOGO is yuva420p.
> +@example
> +-i INPUT -i LOGO -filter_complex "[0:v]hwupload[a], [1:v]format=yuva420p,
> hwupload[b], [a][b]overlay_vaapi=x=200:y=100:w=400:h=300:alpha=1.0,
> hwdownload, format=nv12" OUTPUT
> +@end example
> +
> +@end itemize
> +
>  @section ton

[FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread Gijs Peskens
Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
behavior.

Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
and error out in that case.
---
 doc/protocols.texi|  9 +
 libavformat/librist.c | 40 +++-
 2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index d207df0b52..f1acf0cc77 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
retransmission of data.
 Default value is 0 which means the librist default (1 sec). Maximum value is 30
 seconds.
 
+@item fifo_size
+Size of the librist receiver output fifo in number of packets. This must be a 
+power of 2.
+Defaults to 8192 (vs the libRIST default of 1024).
+
+@item overrun_nonfatal=@var{1|0}
+Survive in case of libRIST fifo buffer overrun. Default
+value is 0.
+
 @item pkt_size
 Set maximum packet size for sending data. 1316 by default.
 
diff --git a/libavformat/librist.c b/libavformat/librist.c
index 378b635ea7..83bbf9f07a 100644
--- a/libavformat/librist.c
+++ b/libavformat/librist.c
@@ -43,6 +43,9 @@
 ((patch) + ((minor)* 0x100) + ((major) *0x1))
 #define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
 #define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
+#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
+
+#define FF_LIBRIST_FIFO_SIZE_DEFAULT (2 << 12)//8192
 
 typedef struct RISTContext {
 const AVClass *class;
@@ -52,6 +55,8 @@ typedef struct RISTContext {
 int packet_size;
 int log_level;
 int encryption;
+int fifo_size;
+bool overrun_nonfatal;
 char *secret;
 
 struct rist_logging_settings logging_settings;
@@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
 { "main",NULL,  0,   
AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" 
},
 { "advanced",NULL,  0,   
AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" 
},
 { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
+{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", 
OFFSET(fifo_size), AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 
9, 2 << 15, .flags = D|E },
+{ "overrun_nonfatal", "survive in case of libRIST receiving circular 
buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,  
  D },
 { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT,  
 {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
 { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT,  
 {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
 { "secret", "set encryption secret",OFFSET(secret),  
AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
@@ -90,7 +97,6 @@ static int risterr2ret(int err)
 static int log_cb(void *arg, enum rist_log_level log_level, const char *msg)
 {
 int level;
-
 switch (log_level) {
 case RIST_LOG_ERROR:level = AV_LOG_ERROR;   break;
 case RIST_LOG_WARN: level = AV_LOG_WARNING; break;
@@ -139,6 +145,7 @@ static int librist_open(URLContext *h, const char *uri, int 
flags)
 h->max_packet_size = s->packet_size;
 ret = rist_sender_create(&s->ctx, s->profile, 0, logging_settings);
 }
+
 if (ret < 0)
 goto err;
 
@@ -146,6 +153,7 @@ static int librist_open(URLContext *h, const char *uri, int 
flags)
 h->max_packet_size = MAX_PAYLOAD_SIZE;
 ret = rist_receiver_create(&s->ctx, s->profile, logging_settings);
 }
+
 if (ret < 0)
 goto err;
 
@@ -161,6 +169,20 @@ static int librist_open(URLContext *h, const char *uri, 
int flags)
 if (ret < 0)
 goto err;
 
+//Prior to 4.2.0 there was a bug in libRIST which made this call always 
fail.
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (flags & AVIO_FLAG_READ) {
+ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
+if (ret != 0) {
+goto err;
+}
+}
+#else 
+if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
+av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which fails 
setting the fifo buffer size");
+}
+#endif
+
 if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
 ((peer_config->key_size == 128 || peer_config->key_size == 256) && 
!peer_config->secret[0])) {
 av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is 
enabled\n");
@@ -223,8 +245,24 @@ static int librist_read(URLContext *h, uint8_t *buf, int 
size)
 return AVERROR_EXTERNAL;
 }
 
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERS

Re: [FFmpeg-devel] [PATCH] avformat/asfdec: fix crash caused by free wlid pointers

2022-01-11 Thread Andreas Rheinhardt
Yang Xiao:
> From: Yang Xiao 
> 
> This commit fixed a crash when seeking wma frames, asf decoder will try to 
> demux in function asf_read_pts().
> Pointer member side_data of AVPacket that allocated by stack may be wild 
> pointer.
> Prevent releasing wild pointers in AVPacket when some functions try to call 
> av_packet_unref, example av_read_frame().
> ---
>  libavformat/asfdec_f.c | 2 +-
>  libavformat/mpc.c  | 2 +-
>  2 files changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
> index a8f36ed286..bae0ecfc7c 100644
> --- a/libavformat/asfdec_f.c
> +++ b/libavformat/asfdec_f.c
> @@ -1433,7 +1433,7 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
> stream_index,
>  {
>  FFFormatContext *const si = ffformatcontext(s);
>  ASFContext *asf = s->priv_data;
> -AVPacket pkt1, *pkt = &pkt1;
> +AVPacket pkt1 = {0}, *pkt = &pkt1;
>  ASFStream *asf_st;
>  int64_t pts;
>  int64_t pos = *ppos;
> diff --git a/libavformat/mpc.c b/libavformat/mpc.c
> index b5b2bab33c..ad0d693152 100644
> --- a/libavformat/mpc.c
> +++ b/libavformat/mpc.c
> @@ -189,7 +189,7 @@ static int mpc_read_seek(AVFormatContext *s, int 
> stream_index, int64_t timestamp
>  AVStream *st = s->streams[stream_index];
>  FFStream *const sti = ffstream(st);
>  MPCContext *c = s->priv_data;
> -AVPacket pkt1, *pkt = &pkt1;
> +AVPacket pkt1 = {0}, *pkt = &pkt1;
>  int ret;
>  int index = av_index_search_timestamp(st, FFMAX(timestamp - 
> DELAY_FRAMES, 0), flags);
>  uint32_t lastframe;
> 

Do you have FF_API_INIT_PACKET set to 0 (it should still be set to 1)?
Because av_read_frame() is supposed to (and documented to) treat the
packet it is given as uninitialized.

- Andreas
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread zhilizhao(赵志立)


> On Jan 11, 2022, at 5:23 PM, Gijs Peskens  wrote:
> 
> Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
> behavior.
> 
> Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
> and error out in that case.
> ---
> doc/protocols.texi|  9 +
> libavformat/librist.c | 40 +++-
> 2 files changed, 48 insertions(+), 1 deletion(-)
> 
> diff --git a/doc/protocols.texi b/doc/protocols.texi
> index d207df0b52..f1acf0cc77 100644
> --- a/doc/protocols.texi
> +++ b/doc/protocols.texi
> @@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
> retransmission of data.
> Default value is 0 which means the librist default (1 sec). Maximum value is 
> 30
> seconds.
> 
> +@item fifo_size
> +Size of the librist receiver output fifo in number of packets. This must be 
> a 
> +power of 2.
> +Defaults to 8192 (vs the libRIST default of 1024).
> +
> +@item overrun_nonfatal=@var{1|0}
> +Survive in case of libRIST fifo buffer overrun. Default
> +value is 0.
> +
> @item pkt_size
> Set maximum packet size for sending data. 1316 by default.
> 
> diff --git a/libavformat/librist.c b/libavformat/librist.c
> index 378b635ea7..83bbf9f07a 100644
> --- a/libavformat/librist.c
> +++ b/libavformat/librist.c
> @@ -43,6 +43,9 @@
> ((patch) + ((minor)* 0x100) + ((major) *0x1))
> #define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
> LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
> #define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
> +#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
> +
> +#define FF_LIBRIST_FIFO_SIZE_DEFAULT (2 << 12)//8192

It’s not easy to read without space before ‘//8192’.
I prefer

#define FF_LIBRIST_FIFO_SIZE_DEFAULT 8192 

> 
> typedef struct RISTContext {
> const AVClass *class;
> @@ -52,6 +55,8 @@ typedef struct RISTContext {
> int packet_size;
> int log_level;
> int encryption;
> +int fifo_size;
> +bool overrun_nonfatal;

Should be ‘int’, please check opt.c.

> char *secret;
> 
> struct rist_logging_settings logging_settings;
> @@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
> { "main",NULL,  0,   
> AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, 
> "profile" },
> { "advanced",NULL,  0,   
> AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, 
> "profile" },
> { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
> AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
> +{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", 
> OFFSET(fifo_size), AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 
> 9, 2 << 15, .flags = D|E },
> +{ "overrun_nonfatal", "survive in case of libRIST receiving circular 
> buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 
> 1,D },
> { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, 
>   {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
> { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT, 
>   {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
> { "secret", "set encryption secret",OFFSET(secret),  
> AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
> @@ -90,7 +97,6 @@ static int risterr2ret(int err)
> static int log_cb(void *arg, enum rist_log_level log_level, const char *msg)
> {
> int level;
> -

Unrelated changes.

> switch (log_level) {
> case RIST_LOG_ERROR:level = AV_LOG_ERROR;   break;
> case RIST_LOG_WARN: level = AV_LOG_WARNING; break;
> @@ -139,6 +145,7 @@ static int librist_open(URLContext *h, const char *uri, 
> int flags)
> h->max_packet_size = s->packet_size;
> ret = rist_sender_create(&s->ctx, s->profile, 0, logging_settings);
> }
> +
> if (ret < 0)
> goto err;
> 
> @@ -146,6 +153,7 @@ static int librist_open(URLContext *h, const char *uri, 
> int flags)
> h->max_packet_size = MAX_PAYLOAD_SIZE;
> ret = rist_receiver_create(&s->ctx, s->profile, logging_settings);
> }
> +
> if (ret < 0)
> goto err;
> 
> @@ -161,6 +169,20 @@ static int librist_open(URLContext *h, const char *uri, 
> int flags)
> if (ret < 0)
> goto err;
> 
> +//Prior to 4.2.0 there was a bug in libRIST which made this call always 
> fail.
> +#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
> +if (flags & AVIO_FLAG_READ) {
> +ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
> +if (ret != 0) {
> +goto err;
> +}
> +}
> +#else 
> +if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
> +av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which 
> fails setting the fifo buffer size");

Missing newline at the end o

Re: [FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread Gijs Peskens

On 11-01-2022 10:43, "zhilizhao(赵志立)" wrote:



On Jan 11, 2022, at 5:23 PM, Gijs Peskens  wrote:

Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
behavior.

Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
and error out in that case.
---
doc/protocols.texi|  9 +
libavformat/librist.c | 40 +++-
2 files changed, 48 insertions(+), 1 deletion(-)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index d207df0b52..f1acf0cc77 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
retransmission of data.
Default value is 0 which means the librist default (1 sec). Maximum value is 30
seconds.

+@item fifo_size
+Size of the librist receiver output fifo in number of packets. This must be a
+power of 2.
+Defaults to 8192 (vs the libRIST default of 1024).
+
+@item overrun_nonfatal=@var{1|0}
+Survive in case of libRIST fifo buffer overrun. Default
+value is 0.
+
@item pkt_size
Set maximum packet size for sending data. 1316 by default.

diff --git a/libavformat/librist.c b/libavformat/librist.c
index 378b635ea7..83bbf9f07a 100644
--- a/libavformat/librist.c
+++ b/libavformat/librist.c
@@ -43,6 +43,9 @@
 ((patch) + ((minor)* 0x100) + ((major) *0x1))
#define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
#define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
+#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
+
+#define FF_LIBRIST_FIFO_SIZE_DEFAULT (2 << 12)//8192

It’s not easy to read without space before ‘//8192’.
I prefer

#define FF_LIBRIST_FIFO_SIZE_DEFAULT 8192


typedef struct RISTContext {
 const AVClass *class;
@@ -52,6 +55,8 @@ typedef struct RISTContext {
 int packet_size;
 int log_level;
 int encryption;
+int fifo_size;
+bool overrun_nonfatal;

Should be ‘int’, please check opt.c.


 char *secret;

 struct rist_logging_settings logging_settings;
@@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
 { "main",NULL,  0,   AV_OPT_TYPE_CONST, 
{.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" },
 { "advanced",NULL,  0,   AV_OPT_TYPE_CONST, 
{.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" },
 { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
+{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", OFFSET(fifo_size), 
AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 9, 2 << 15, .flags = D|E },
+{ "overrun_nonfatal", "survive in case of libRIST receiving circular buffer 
overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,D },
 { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT,  
 {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
 { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT,  
 {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
 { "secret", "set encryption secret",OFFSET(secret),  
AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
@@ -90,7 +97,6 @@ static int risterr2ret(int err)
static int log_cb(void *arg, enum rist_log_level log_level, const char *msg)
{
 int level;
-

Unrelated changes.


 switch (log_level) {
 case RIST_LOG_ERROR:level = AV_LOG_ERROR;   break;
 case RIST_LOG_WARN: level = AV_LOG_WARNING; break;
@@ -139,6 +145,7 @@ static int librist_open(URLContext *h, const char *uri, int 
flags)
 h->max_packet_size = s->packet_size;
 ret = rist_sender_create(&s->ctx, s->profile, 0, logging_settings);
 }
+
 if (ret < 0)
 goto err;

@@ -146,6 +153,7 @@ static int librist_open(URLContext *h, const char *uri, int 
flags)
 h->max_packet_size = MAX_PAYLOAD_SIZE;
 ret = rist_receiver_create(&s->ctx, s->profile, logging_settings);
 }
+
 if (ret < 0)
 goto err;

@@ -161,6 +169,20 @@ static int librist_open(URLContext *h, const char *uri, 
int flags)
 if (ret < 0)
 goto err;

+//Prior to 4.2.0 there was a bug in libRIST which made this call always 
fail.
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (flags & AVIO_FLAG_READ) {
+ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
+if (ret != 0) {
+goto err;
+}
+}
+#else
+if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
+av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which fails 
setting the fifo buffer size");

Missing newline at the end of log message.


+}
+#endif
+
 if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
 ((peer_config->key_size == 128 || peer_config->key_size == 25

[FFmpeg-devel] [PATCH 04/28] ffmpeg: move writing the trailer to ffmpeg_mux.c

2022-01-11 Thread Anton Khirnov
---
 fftools/ffmpeg.c | 16 +++-
 fftools/ffmpeg.h |  1 +
 fftools/ffmpeg_mux.c | 21 +
 3 files changed, 25 insertions(+), 13 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index e0aa533dc9..87e8b0be59 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -4440,19 +4440,9 @@ static int transcode(void)
 
 /* write the trailer if needed */
 for (i = 0; i < nb_output_files; i++) {
-os = output_files[i]->ctx;
-if (!output_files[i]->header_written) {
-av_log(NULL, AV_LOG_ERROR,
-   "Nothing was written into output file %d (%s), because "
-   "at least one of its streams received no packets.\n",
-   i, os->url);
-continue;
-}
-if ((ret = av_write_trailer(os)) < 0) {
-av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", 
os->url, av_err2str(ret));
-if (exit_on_error)
-exit_program(1);
-}
+ret = of_write_trailer(output_files[i]);
+if (ret < 0 && exit_on_error)
+exit_program(1);
 }
 
 /* dump report by using the first video and audio streams */
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index fed34b06f8..91c313d8ef 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -686,6 +686,7 @@ int hwaccel_decode_init(AVCodecContext *avctx);
 
 /* open the muxer when all the streams are initialized */
 int of_check_init(OutputFile *of);
+int of_write_trailer(OutputFile *of);
 
 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
  int unqueue);
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 3c2980ba90..c65ed3631a 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -291,3 +291,24 @@ int of_check_init(OutputFile *of)
 
 return 0;
 }
+
+int of_write_trailer(OutputFile *of)
+{
+int ret;
+
+if (!of->header_written) {
+av_log(NULL, AV_LOG_ERROR,
+   "Nothing was written into output file %d (%s), because "
+   "at least one of its streams received no packets.\n",
+   of->index, of->ctx->url);
+return AVERROR(EINVAL);
+}
+
+ret = av_write_trailer(of->ctx);
+if (ret < 0) {
+av_log(NULL, AV_LOG_ERROR, "Error writing trailer of %s: %s\n", 
of->ctx->url, av_err2str(ret));
+return ret;
+}
+
+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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 01/28] ffmpeg: pass the muxer context explicitly to some functions

2022-01-11 Thread Anton Khirnov
Stop accessing OutputFile.ctx. This will be useful in the following
commits, where it will become hidden.
---
 fftools/ffmpeg_opt.c | 16 
 1 file changed, 8 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 9c820ab73f..f638edace9 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2094,10 +2094,10 @@ static int opt_streamid(void *optctx, const char *opt, 
const char *arg)
 return 0;
 }
 
-static int copy_chapters(InputFile *ifile, OutputFile *ofile, int 
copy_metadata)
+static int copy_chapters(InputFile *ifile, OutputFile *ofile, AVFormatContext 
*os,
+ int copy_metadata)
 {
 AVFormatContext *is = ifile->ctx;
-AVFormatContext *os = ofile->ctx;
 AVChapter **tmp;
 int i;
 
@@ -2137,14 +2137,14 @@ static int copy_chapters(InputFile *ifile, OutputFile 
*ofile, int copy_metadata)
 return 0;
 }
 
-static int set_dispositions(OutputFile *of)
+static int set_dispositions(OutputFile *of, AVFormatContext *ctx)
 {
 int nb_streams[AVMEDIA_TYPE_NB]   = { 0 };
 int have_default[AVMEDIA_TYPE_NB] = { 0 };
 int have_manual = 0;
 
 // first, copy the input dispositions
-for (int i = 0; i< of->ctx->nb_streams; i++) {
+for (int i = 0; i < ctx->nb_streams; i++) {
 OutputStream *ost = output_streams[of->ost_index + i];
 
 nb_streams[ost->st->codecpar->codec_type]++;
@@ -2161,7 +2161,7 @@ static int set_dispositions(OutputFile *of)
 
 if (have_manual) {
 // process manually set dispositions - they override the above copy
-for (int i = 0; i< of->ctx->nb_streams; i++) {
+for (int i = 0; i < ctx->nb_streams; i++) {
 OutputStream *ost = output_streams[of->ost_index + i];
 int ret;
 
@@ -2187,7 +2187,7 @@ static int set_dispositions(OutputFile *of)
 // For each media type with more than one stream, find a suitable 
stream to
 // mark as default, unless one is already marked default.
 // "Suitable" means the first of that type, skipping attached pictures.
-for (int i = 0; i< of->ctx->nb_streams; i++) {
+for (int i = 0; i < ctx->nb_streams; i++) {
 OutputStream *ost = output_streams[of->ost_index + i];
 enum AVMediaType type = ost->st->codecpar->codec_type;
 
@@ -2725,7 +2725,7 @@ loop_end:
 }
 }
 if (o->chapters_input_file >= 0)
-copy_chapters(input_files[o->chapters_input_file], of,
+copy_chapters(input_files[o->chapters_input_file], of, oc,
   !o->metadata_chapters_manual);
 
 /* copy global metadata by default */
@@ -2878,7 +2878,7 @@ loop_end:
 }
 }
 
-err = set_dispositions(of);
+err = set_dispositions(of, oc);
 if (err < 0) {
 av_log(NULL, AV_LOG_FATAL, "Error setting output stream 
dispositions\n");
 exit_program(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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 05/28] ffmpeg: move freeing the output file to ffmpeg_mux.c

2022-01-11 Thread Anton Khirnov
---
 fftools/ffmpeg.c | 14 ++
 fftools/ffmpeg.h |  1 +
 fftools/ffmpeg_mux.c | 17 +
 3 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 87e8b0be59..320ab8e0ca 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -567,19 +567,9 @@ static void ffmpeg_cleanup(int ret)
 av_freep(&subtitle_out);
 
 /* close files */
-for (i = 0; i < nb_output_files; i++) {
-OutputFile *of = output_files[i];
-AVFormatContext *s;
-if (!of)
-continue;
-s = of->ctx;
-if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
-avio_closep(&s->pb);
-avformat_free_context(s);
-av_dict_free(&of->opts);
+for (i = 0; i < nb_output_files; i++)
+of_close(&output_files[i]);
 
-av_freep(&output_files[i]);
-}
 for (i = 0; i < nb_output_streams; i++) {
 OutputStream *ost = output_streams[i];
 
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 91c313d8ef..288e0f2981 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -687,6 +687,7 @@ int hwaccel_decode_init(AVCodecContext *avctx);
 /* open the muxer when all the streams are initialized */
 int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
+void of_close(OutputFile **pof);
 
 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
  int unqueue);
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index c65ed3631a..12ee3c2357 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -312,3 +312,20 @@ int of_write_trailer(OutputFile *of)
 
 return 0;
 }
+
+void of_close(OutputFile **pof)
+{
+OutputFile *of = *pof;
+AVFormatContext *s;
+
+if (!of)
+return;
+
+s = of->ctx;
+if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
+avio_closep(&s->pb);
+avformat_free_context(s);
+av_dict_free(&of->opts);
+
+av_freep(pof);
+}
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 03/28] ffmpeg: move some muxing-related code into a separate file

2022-01-11 Thread Anton Khirnov
This is a first step towards making muxers more independent from the
rest of the code.
---
 fftools/Makefile |   6 +-
 fftools/ffmpeg.c | 273 ++--
 fftools/ffmpeg.h |  10 ++
 fftools/ffmpeg_mux.c | 293 +++
 4 files changed, 315 insertions(+), 267 deletions(-)
 create mode 100644 fftools/ffmpeg_mux.c

diff --git a/fftools/Makefile b/fftools/Makefile
index da420786eb..27a22250b3 100644
--- a/fftools/Makefile
+++ b/fftools/Makefile
@@ -9,7 +9,11 @@ AVBASENAMES  = ffmpeg ffplay ffprobe
 ALLAVPROGS   = $(AVBASENAMES:%=%$(PROGSSUF)$(EXESUF))
 ALLAVPROGS_G = $(AVBASENAMES:%=%$(PROGSSUF)_g$(EXESUF))
 
-OBJS-ffmpeg+= fftools/ffmpeg_opt.o 
fftools/ffmpeg_filter.o fftools/ffmpeg_hw.o
+OBJS-ffmpeg +=  \
+fftools/ffmpeg_filter.o \
+fftools/ffmpeg_hw.o \
+fftools/ffmpeg_mux.o\
+fftools/ffmpeg_opt.o\
 
 define DOFFTOOL
 OBJS-$(1) += fftools/cmdutils.o fftools/$(1).o $(OBJS-$(1)-yes)
diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 1961653dcc..e0aa533dc9 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -136,9 +136,9 @@ static int nb_frames_dup = 0;
 static unsigned dup_warning = 1000;
 static int nb_frames_drop = 0;
 static int64_t decode_error_stat[2];
-static unsigned nb_output_dumped = 0;
+unsigned nb_output_dumped = 0;
 
-static int want_sdp = 1;
+int want_sdp = 1;
 
 static BenchmarkTimeStamps current_time;
 AVIOContext *progress_avio = NULL;
@@ -344,7 +344,7 @@ static volatile int received_sigterm = 0;
 static volatile int received_nb_signals = 0;
 static atomic_int transcode_init_done = ATOMIC_VAR_INIT(0);
 static volatile int ffmpeg_exited = 0;
-static int main_return_code = 0;
+int main_return_code = 0;
 static int64_t copy_ts_first_pts = AV_NOPTS_VALUE;
 
 static void
@@ -715,159 +715,6 @@ static void update_benchmark(const char *fmt, ...)
 }
 }
 
-static void close_all_output_streams(OutputStream *ost, OSTFinished 
this_stream, OSTFinished others)
-{
-int i;
-for (i = 0; i < nb_output_streams; i++) {
-OutputStream *ost2 = output_streams[i];
-ost2->finished |= ost == ost2 ? this_stream : others;
-}
-}
-
-static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int 
unqueue)
-{
-AVFormatContext *s = of->ctx;
-AVStream *st = ost->st;
-int ret;
-
-/*
- * Audio encoders may split the packets --  #frames in != #packets out.
- * But there is no reordering, so we can limit the number of output packets
- * by simply dropping them here.
- * Counting encoded video frames needs to be done separately because of
- * reordering, see do_video_out().
- * Do not count the packet when unqueued because it has been counted when 
queued.
- */
-if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && 
ost->encoding_needed) && !unqueue) {
-if (ost->frame_number >= ost->max_frames) {
-av_packet_unref(pkt);
-return;
-}
-ost->frame_number++;
-}
-
-if (!of->header_written) {
-AVPacket *tmp_pkt;
-/* the muxer is not initialized yet, buffer the packet */
-if (!av_fifo_space(ost->muxing_queue)) {
-size_t cur_size = av_fifo_size(ost->muxing_queue);
-unsigned int are_we_over_size =
-(ost->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 new_size = FFMIN(2 * cur_size, limit);
-
-if (new_size <= cur_size) {
-av_log(NULL, AV_LOG_ERROR,
-   "Too many packets buffered for output stream %d:%d.\n",
-   ost->file_index, ost->st->index);
-exit_program(1);
-}
-ret = av_fifo_realloc2(ost->muxing_queue, new_size);
-if (ret < 0)
-exit_program(1);
-}
-ret = av_packet_make_refcounted(pkt);
-if (ret < 0)
-exit_program(1);
-tmp_pkt = av_packet_alloc();
-if (!tmp_pkt)
-exit_program(1);
-av_packet_move_ref(tmp_pkt, pkt);
-ost->muxing_queue_data_size += tmp_pkt->size;
-av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), 
NULL);
-return;
-}
-
-if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method 
== VSYNC_DROP) ||
-(st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 
0))
-pkt->pts = pkt->dts = AV_NOPTS_VALUE;
-
-if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-int i;
-uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
-  NULL);
-ost->quality = sd ? AV_RL32(sd) : -1;
-ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
-
-fo

[FFmpeg-devel] [PATCH 07/28] ffmpeg_mux: add private muxer context

2022-01-11 Thread Anton Khirnov
Move header_written into it, which is not (and should not be) used by
any code outside of ffmpeg_mux.

In the future this context will contain more muxer-private state that
should not be visible to other code.
---
 fftools/ffmpeg.h |  6 --
 fftools/ffmpeg_mux.c | 26 ++
 fftools/ffmpeg_opt.c |  6 ++
 3 files changed, 32 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index c28acad4f1..279a99cc48 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -573,9 +573,12 @@ typedef struct OutputStream {
 int64_t error[4];
 } OutputStream;
 
+typedef struct Muxer Muxer;
+
 typedef struct OutputFile {
 int index;
 
+Muxer*mux;
 const AVOutputFormat *format;
 
 AVFormatContext *ctx;
@@ -586,8 +589,6 @@ typedef struct OutputFile {
 uint64_t limit_filesize; /* filesize limit expressed in bytes */
 
 int shortest;
-
-int header_written;
 } OutputFile;
 
 extern InputStream **input_streams;
@@ -686,6 +687,7 @@ int hw_device_setup_for_filter(FilterGraph *fg);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
+int of_muxer_init(OutputFile *of);
 /* open the muxer when all the streams are initialized */
 int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 12ee3c2357..538f153484 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -32,6 +32,10 @@
 #include "libavformat/avformat.h"
 #include "libavformat/avio.h"
 
+struct Muxer {
+int header_written;
+};
+
 static void close_all_output_streams(OutputStream *ost, OSTFinished 
this_stream, OSTFinished others)
 {
 int i;
@@ -64,7 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
 ost->frame_number++;
 }
 
-if (!of->header_written) {
+if (!of->mux->header_written) {
 AVPacket *tmp_pkt;
 /* the muxer is not initialized yet, buffer the packet */
 if (!av_fifo_space(ost->muxing_queue)) {
@@ -195,7 +199,7 @@ static int print_sdp(void)
 AVFormatContext **avc;
 
 for (i = 0; i < nb_output_files; i++) {
-if (!output_files[i]->header_written)
+if (!output_files[i]->mux->header_written)
 return 0;
 }
 
@@ -259,7 +263,7 @@ int of_check_init(OutputFile *of)
 return ret;
 }
 //assert_avoptions(of->opts);
-of->header_written = 1;
+of->mux->header_written = 1;
 
 av_dump_format(of->ctx, of->index, of->ctx->url, 1);
 nb_output_dumped++;
@@ -296,7 +300,7 @@ int of_write_trailer(OutputFile *of)
 {
 int ret;
 
-if (!of->header_written) {
+if (!of->mux->header_written) {
 av_log(NULL, AV_LOG_ERROR,
"Nothing was written into output file %d (%s), because "
"at least one of its streams received no packets.\n",
@@ -327,5 +331,19 @@ void of_close(OutputFile **pof)
 avformat_free_context(s);
 av_dict_free(&of->opts);
 
+av_freep(&of->mux);
+
 av_freep(pof);
 }
+
+int of_muxer_init(OutputFile *of)
+{
+Muxer *mux = av_mallocz(sizeof(*mux));
+
+if (!mux)
+return AVERROR(ENOMEM);
+
+of->mux  = mux;
+
+return 0;
+}
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 433102bb49..2ed09d4a7a 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2886,6 +2886,12 @@ loop_end:
 exit_program(1);
 }
 
+err = of_muxer_init(of);
+if (err < 0) {
+av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing 
state\n");
+exit_program(1);
+}
+
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 06/28] ffmpeg: store output format separately from the muxer context

2022-01-11 Thread Anton Khirnov
Allows accessing it without going through the muxer context. This will
be useful in the following commits, where the muxer context will be
hidden.
---
 fftools/ffmpeg.c | 18 ++
 fftools/ffmpeg.h |  2 ++
 fftools/ffmpeg_opt.c |  1 +
 3 files changed, 13 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 320ab8e0ca..9360c8a475 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2783,9 +2783,9 @@ static int init_output_stream_streamcopy(OutputStream 
*ost)
 
 if (!codec_tag) {
 unsigned int codec_tag_tmp;
-if (!of->ctx->oformat->codec_tag ||
-av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) 
== par_src->codec_id ||
-!av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, 
&codec_tag_tmp))
+if (!of->format->codec_tag ||
+av_codec_get_id (of->format->codec_tag, par_src->codec_tag) == 
par_src->codec_id ||
+!av_codec_get_tag2(of->format->codec_tag, par_src->codec_id, 
&codec_tag_tmp))
 codec_tag = par_src->codec_tag;
 }
 
@@ -2803,7 +2803,7 @@ static int init_output_stream_streamcopy(OutputStream 
*ost)
 else
 ost->st->avg_frame_rate = ist->st->avg_frame_rate;
 
-ret = avformat_transfer_internal_stream_timing_info(of->ctx->oformat, 
ost->st, ist->st, copy_tb);
+ret = avformat_transfer_internal_stream_timing_info(of->format, ost->st, 
ist->st, copy_tb);
 if (ret < 0)
 return ret;
 
@@ -3005,7 +3005,8 @@ static int init_output_stream_encode(OutputStream *ost, 
AVFrame *frame)
 InputStream *ist = get_input_stream(ost);
 AVCodecContext *enc_ctx = ost->enc_ctx;
 AVCodecContext *dec_ctx = NULL;
-AVFormatContext *oc = output_files[ost->file_index]->ctx;
+OutputFile  *of = output_files[ost->file_index];
+AVFormatContext *oc = of->ctx;
 int ret;
 
 set_encoder_id(output_files[ost->file_index], ost);
@@ -3065,7 +3066,8 @@ static int init_output_stream_encode(OutputStream *ost, 
AVFrame *frame)
 if (!(enc_ctx->time_base.num && enc_ctx->time_base.den))
 enc_ctx->time_base = 
av_buffersink_get_time_base(ost->filter->filter);
 if (   av_q2d(enc_ctx->time_base) < 0.001 && video_sync_method != 
VSYNC_PASSTHROUGH
-   && (video_sync_method == VSYNC_CFR || video_sync_method == 
VSYNC_VSCFR || (video_sync_method == VSYNC_AUTO && !(oc->oformat->flags & 
AVFMT_VARIABLE_FPS{
+   && (video_sync_method == VSYNC_CFR || video_sync_method == 
VSYNC_VSCFR ||
+   (video_sync_method == VSYNC_AUTO && !(of->format->flags & 
AVFMT_VARIABLE_FPS{
 av_log(oc, AV_LOG_WARNING, "Frame rate very high for a muxer not 
efficiently supporting it.\n"
"Please consider specifying a lower 
framerate, a different muxer or -vsync 2\n");
 }
@@ -3398,7 +3400,7 @@ static int transcode_init(void)
 /* write headers for files with no streams */
 for (i = 0; i < nb_output_files; i++) {
 oc = output_files[i]->ctx;
-if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
+if (output_files[i]->format->flags & AVFMT_NOSTREAMS && oc->nb_streams 
== 0) {
 ret = of_check_init(output_files[i]);
 if (ret < 0)
 goto dump_format;
@@ -4605,7 +4607,7 @@ int main(int argc, char **argv)
 }
 
 for (i = 0; i < nb_output_files; i++) {
-if (strcmp(output_files[i]->ctx->oformat->name, "rtp"))
+if (strcmp(output_files[i]->format->name, "rtp"))
 want_sdp = 0;
 }
 
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 288e0f2981..c28acad4f1 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -576,6 +576,8 @@ typedef struct OutputStream {
 typedef struct OutputFile {
 int index;
 
+const AVOutputFormat *format;
+
 AVFormatContext *ctx;
 AVDictionary *opts;
 int ost_index;   /* index of the first stream in output_streams */
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index e8db515e0c..433102bb49 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2301,6 +2301,7 @@ static int open_output_file(OptionsContext *o, const char 
*filename)
 }
 
 of->ctx = oc;
+of->format = oc->oformat;
 if (o->recording_time != INT64_MAX)
 oc->duration = o->recording_time;
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 02/28] ffmpeg: store the output file index in OutputFile

2022-01-11 Thread Anton Khirnov
Use it to simplify check_init_output_file(). Will allow further
simplifications in the following commits.
---
 fftools/ffmpeg.c | 10 +-
 fftools/ffmpeg.h |  2 ++
 fftools/ffmpeg_opt.c |  1 +
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5d134b025f..1961653dcc 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2941,7 +2941,7 @@ static int compare_int64(const void *a, const void *b)
 }
 
 /* open the muxer when all the streams are initialized */
-static int check_init_output_file(OutputFile *of, int file_index)
+static int check_init_output_file(OutputFile *of)
 {
 int ret, i;
 
@@ -2956,13 +2956,13 @@ static int check_init_output_file(OutputFile *of, int 
file_index)
 av_log(NULL, AV_LOG_ERROR,
"Could not write header for output file #%d "
"(incorrect codec parameters ?): %s\n",
-   file_index, av_err2str(ret));
+   of->index, av_err2str(ret));
 return ret;
 }
 //assert_avoptions(of->opts);
 of->header_written = 1;
 
-av_dump_format(of->ctx, file_index, of->ctx->url, 1);
+av_dump_format(of->ctx, of->index, of->ctx->url, 1);
 nb_output_dumped++;
 
 if (sdp_filename || want_sdp) {
@@ -3565,7 +3565,7 @@ static int init_output_stream(OutputStream *ost, AVFrame 
*frame,
 
 ost->initialized = 1;
 
-ret = check_init_output_file(output_files[ost->file_index], 
ost->file_index);
+ret = check_init_output_file(output_files[ost->file_index]);
 if (ret < 0)
 return ret;
 
@@ -3668,7 +3668,7 @@ static int transcode_init(void)
 for (i = 0; i < nb_output_files; i++) {
 oc = output_files[i]->ctx;
 if (oc->oformat->flags & AVFMT_NOSTREAMS && oc->nb_streams == 0) {
-ret = check_init_output_file(output_files[i], i);
+ret = check_init_output_file(output_files[i]);
 if (ret < 0)
 goto dump_format;
 }
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 9b200b806a..5fd5d2606b 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -574,6 +574,8 @@ typedef struct OutputStream {
 } OutputStream;
 
 typedef struct OutputFile {
+int index;
+
 AVFormatContext *ctx;
 AVDictionary *opts;
 int ost_index;   /* index of the first stream in output_streams */
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index f638edace9..e8db515e0c 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2283,6 +2283,7 @@ static int open_output_file(OptionsContext *o, const char 
*filename)
 
 of = ALLOC_ARRAY_ELEM(output_files, nb_output_files);
 
+of->index  = nb_output_files - 1;
 of->ost_index  = nb_output_streams;
 of->recording_time = o->recording_time;
 of->start_time = o->start_time;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 10/28] ffmpeg: refactor limiting output file size with -fs

2022-01-11 Thread Anton Khirnov
Move the file size checking code to ffmpeg_mux. Use the recently
introduced of_filesize(), making this code consistent with the size
shown by print_report().
---
 fftools/ffmpeg.c |  4 +---
 fftools/ffmpeg.h |  4 ++--
 fftools/ffmpeg_mux.c | 11 ++-
 fftools/ffmpeg_opt.c |  3 +--
 4 files changed, 14 insertions(+), 8 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 1b5cf4d9af..37c4a0d285 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3501,10 +3501,8 @@ static int need_output(void)
 for (i = 0; i < nb_output_streams; i++) {
 OutputStream *ost= output_streams[i];
 OutputFile *of   = output_files[ost->file_index];
-AVFormatContext *os  = output_files[ost->file_index]->ctx;
 
-if (ost->finished ||
-(os->pb && avio_tell(os->pb) >= of->limit_filesize))
+if (ost->finished || of_finished(of))
 continue;
 if (ost->frame_number >= ost->max_frames) {
 int j;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 80b90e372d..cae9800da3 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -586,7 +586,6 @@ typedef struct OutputFile {
 int ost_index;   /* index of the first stream in output_streams */
 int64_t recording_time;  ///< desired length of the resulting file in 
microseconds == AV_TIME_BASE units
 int64_t start_time;  ///< start time in microseconds == AV_TIME_BASE 
units
-int64_t limit_filesize; /* filesize limit expressed in bytes */
 
 int shortest;
 } OutputFile;
@@ -687,7 +686,7 @@ int hw_device_setup_for_filter(FilterGraph *fg);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
-int of_muxer_init(OutputFile *of);
+int of_muxer_init(OutputFile *of, int64_t limit_filesize);
 /* open the muxer when all the streams are initialized */
 int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
@@ -695,6 +694,7 @@ void of_close(OutputFile **pof);
 
 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
  int unqueue);
+int of_finished(OutputFile *of);
 int64_t of_filesize(OutputFile *of);
 
 #endif /* FFTOOLS_FFMPEG_H */
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 7c8414ac93..aff40776f6 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -33,6 +33,8 @@
 #include "libavformat/avio.h"
 
 struct Muxer {
+/* filesize limit expressed in bytes */
+int64_t limit_filesize;
 int header_written;
 };
 
@@ -336,7 +338,7 @@ void of_close(OutputFile **pof)
 av_freep(pof);
 }
 
-int of_muxer_init(OutputFile *of)
+int of_muxer_init(OutputFile *of, int64_t limit_filesize)
 {
 Muxer *mux = av_mallocz(sizeof(*mux));
 
@@ -345,9 +347,16 @@ int of_muxer_init(OutputFile *of)
 
 of->mux  = mux;
 
+mux->limit_filesize = limit_filesize;
+
 return 0;
 }
 
+int of_finished(OutputFile *of)
+{
+return of_filesize(of) >= of->mux->limit_filesize;
+}
+
 int64_t of_filesize(OutputFile *of)
 {
 AVIOContext *pb = of->ctx->pb;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 4d2b80683e..0b4f437912 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2287,7 +2287,6 @@ static int open_output_file(OptionsContext *o, const char 
*filename)
 of->ost_index  = nb_output_streams;
 of->recording_time = o->recording_time;
 of->start_time = o->start_time;
-of->limit_filesize = o->limit_filesize;
 of->shortest   = o->shortest;
 av_dict_copy(&of->opts, o->g->format_opts, 0);
 
@@ -2886,7 +2885,7 @@ loop_end:
 exit_program(1);
 }
 
-err = of_muxer_init(of);
+err = of_muxer_init(of, o->limit_filesize);
 if (err < 0) {
 av_log(NULL, AV_LOG_FATAL, "Error initializing internal muxing 
state\n");
 exit_program(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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 08/28] ffmpeg: add a helper function to access output file size

2022-01-11 Thread Anton Khirnov
Stop accessing muxer internals from outside of ffmpeg_mux.
---
 fftools/ffmpeg.c | 10 +-
 fftools/ffmpeg.h |  1 +
 fftools/ffmpeg_mux.c | 14 ++
 3 files changed, 16 insertions(+), 9 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 9360c8a475..1b5cf4d9af 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1514,8 +1514,7 @@ static void print_report(int is_last_report, int64_t 
timer_start, int64_t cur_ti
 {
 AVBPrint buf, buf_script;
 OutputStream *ost;
-AVFormatContext *oc;
-int64_t total_size;
+int64_t total_size = of_filesize(output_files[0]);
 AVCodecContext *enc;
 int frame_number, vid, i;
 double bitrate;
@@ -1544,13 +1543,6 @@ static void print_report(int is_last_report, int64_t 
timer_start, int64_t cur_ti
 
 t = (cur_time-timer_start) / 100.0;
 
-
-oc = output_files[0]->ctx;
-
-total_size = avio_size(oc->pb);
-if (total_size <= 0) // FIXME improve avio_size() so it works with non 
seekable output too
-total_size = avio_tell(oc->pb);
-
 vid = 0;
 av_bprint_init(&buf, 0, AV_BPRINT_SIZE_AUTOMATIC);
 av_bprint_init(&buf_script, 0, AV_BPRINT_SIZE_AUTOMATIC);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 279a99cc48..dfcaa875d9 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -695,5 +695,6 @@ void of_close(OutputFile **pof);
 
 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
  int unqueue);
+int64_t of_filesize(OutputFile *of);
 
 #endif /* FFTOOLS_FFMPEG_H */
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 538f153484..7c8414ac93 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -347,3 +347,17 @@ int of_muxer_init(OutputFile *of)
 
 return 0;
 }
+
+int64_t of_filesize(OutputFile *of)
+{
+AVIOContext *pb = of->ctx->pb;
+int64_t ret = -1;
+
+if (pb) {
+ret = avio_size(pb);
+if (ret <= 0) // FIXME improve avio_size() so it works with non 
seekable output too
+ret = avio_tell(pb);
+}
+
+return ret;
+}
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 16/28] ffmpeg: do not log to the muxer context

2022-01-11 Thread Anton Khirnov
All other logging goes to NULL context.
---
 fftools/ffmpeg.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 2637b25526..6c774e9615 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2954,7 +2954,6 @@ static void init_encoder_time_base(OutputStream *ost, 
AVRational default_time_ba
 {
 InputStream *ist = get_input_stream(ost);
 AVCodecContext *enc_ctx = ost->enc_ctx;
-AVFormatContext *oc;
 
 if (ost->enc_timebase.num > 0) {
 enc_ctx->time_base = ost->enc_timebase;
@@ -2967,8 +2966,9 @@ static void init_encoder_time_base(OutputStream *ost, 
AVRational default_time_ba
 return;
 }
 
-oc = output_files[ost->file_index]->ctx;
-av_log(oc, AV_LOG_WARNING, "Input stream data not available, using 
default time base\n");
+av_log(NULL, AV_LOG_WARNING,
+   "Input stream data for output stream #%d:%d not available, "
+   "using default time base\n", ost->file_index, ost->index);
 }
 
 enc_ctx->time_base = default_time_base;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 18/28] ffmpeg: fix initial muxing queue size

2022-01-11 Thread Anton Khirnov
It stores pointers to AVPacket, not AVPackets themselves.
---
 fftools/ffmpeg_mux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index f03202bbb7..f48031096d 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -411,7 +411,7 @@ int of_muxer_init(OutputFile *of, 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_alloc(8 * sizeof(AVPacket*));
 if (!ms->muxing_queue) {
 ret = AVERROR(ENOMEM);
 goto fail;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 21/28] ffmpeg: move a comment to a more appropriate place

2022-01-11 Thread Anton Khirnov
---
 fftools/ffmpeg.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 4215be0098..f39b1b9375 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -1064,6 +1064,11 @@ static void do_video_out(OutputFile *of,
 }
 }
 
+/*
+ * For video, number of frames in == number of packets out.
+ * But there may be reordering, so we can't throw away frames on encoder
+ * flush, we need to limit them here, before they go into encoder.
+ */
 nb_frames = FFMIN(nb_frames, ost->max_frames - ost->frame_number);
 nb0_frames = FFMIN(nb0_frames, nb_frames);
 
@@ -1217,11 +1222,6 @@ static void do_video_out(OutputFile *of,
 }
 }
 ost->sync_opts++;
-/*
- * For video, number of frames in == number of packets out.
- * But there may be reordering, so we can't throw away frames on 
encoder
- * flush, we need to limit them here, before they go into encoder.
- */
 ost->frame_number++;
 
 if (vstats_filename && frame_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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 20/28] ffmpeg_mux: split of_write_packet()

2022-01-11 Thread Anton Khirnov
It is currently called from two places:
- output_packet() in ffmpeg.c, which submits the newly available output
  packet to the muxer
- from of_check_init() in ffmpeg_mux.c after the header has been
  written, to flush the muxing queue

Some packets will thus be processed by this function twice, so it
requires an extra parameter to indicate the place it is called from and
avoid modifying some state twice.

This is fragile and hard to follow, so split this function into two.
Also rename of_write_packet() to of_submit_packet() to better reflect
its new purpose.
---
 fftools/ffmpeg.c |  4 +--
 fftools/ffmpeg.h |  3 +--
 fftools/ffmpeg_mux.c | 63 
 3 files changed, 37 insertions(+), 33 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index c1bb3926c4..4215be0098 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -728,11 +728,11 @@ static void output_packet(OutputFile *of, AVPacket *pkt,
 if (ret < 0)
 goto finish;
 while ((ret = av_bsf_receive_packet(ost->bsf_ctx, pkt)) >= 0)
-of_write_packet(of, pkt, ost, 0);
+of_submit_packet(of, pkt, ost);
 if (ret == AVERROR(EAGAIN))
 ret = 0;
 } else if (!eof)
-of_write_packet(of, pkt, ost, 0);
+of_submit_packet(of, pkt, ost);
 
 finish:
 if (ret < 0 && ret != AVERROR_EOF) {
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 28df1b179f..374dca9189 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -684,8 +684,7 @@ int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
 void of_close(OutputFile **pof);
 
-void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
- int unqueue);
+void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost);
 int of_finished(OutputFile *of);
 int64_t of_filesize(OutputFile *of);
 AVChapter * const *
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 4ab2279739..36f781ae11 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -102,39 +102,12 @@ static int queue_packet(OutputFile *of, OutputStream 
*ost, AVPacket *pkt)
 return 0;
 }
 
-void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
- int unqueue)
+static void write_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
 {
 AVFormatContext *s = of->ctx;
 AVStream *st = ost->st;
 int ret;
 
-/*
- * Audio encoders may split the packets --  #frames in != #packets out.
- * But there is no reordering, so we can limit the number of output packets
- * by simply dropping them here.
- * Counting encoded video frames needs to be done separately because of
- * reordering, see do_video_out().
- * Do not count the packet when unqueued because it has been counted when 
queued.
- */
-if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && 
ost->encoding_needed) && !unqueue) {
-if (ost->frame_number >= ost->max_frames) {
-av_packet_unref(pkt);
-return;
-}
-ost->frame_number++;
-}
-
-/* the muxer is not initialized yet, buffer the packet */
-if (!of->mux->header_written) {
-ret = queue_packet(of, ost, pkt);
-if (ret < 0) {
-av_packet_unref(pkt);
-exit_program(1);
-}
-return;
-}
-
 if ((st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && video_sync_method 
== VSYNC_DROP) ||
 (st->codecpar->codec_type == AVMEDIA_TYPE_AUDIO && audio_sync_method < 
0))
 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
@@ -225,6 +198,38 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
 }
 }
 
+void of_submit_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost)
+{
+AVStream *st = ost->st;
+int ret;
+
+/*
+ * Audio encoders may split the packets --  #frames in != #packets out.
+ * But there is no reordering, so we can limit the number of output packets
+ * by simply dropping them here.
+ * Counting encoded video frames needs to be done separately because of
+ * reordering, see do_video_out().
+ */
+if (!(st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && 
ost->encoding_needed)) {
+if (ost->frame_number >= ost->max_frames) {
+av_packet_unref(pkt);
+return;
+}
+ost->frame_number++;
+}
+
+if (of->mux->header_written) {
+write_packet(of, ost, pkt);
+} else {
+/* the muxer is not initialized yet, buffer the packet */
+ret = queue_packet(of, ost, pkt);
+if (ret < 0) {
+av_packet_unref(pkt);
+exit_program(1);
+}
+}
+}
+
 static int print_sdp(void)
 {
 char sdp[16384];
@@ -324,7 +329,7 @@ int of_check_init(OutputFile *of)
 AVPacket *pkt;
 av_fifo_generic_read(ms->muxing_queue, &pkt, sizeof(pkt), NULL);
 ms->muxing_queue_data_size

[FFmpeg-devel] [PATCH 22/28] ffmpeg: move output file opts into private context

2022-01-11 Thread Anton Khirnov
It is private to the muxer, no reason to access it from outside.
---
 fftools/ffmpeg.h |  3 +--
 fftools/ffmpeg_mux.c |  9 ++---
 fftools/ffmpeg_opt.c | 12 ++--
 3 files changed, 13 insertions(+), 11 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 374dca9189..d9f997bf7a 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -574,7 +574,6 @@ typedef struct OutputFile {
 const AVOutputFormat *format;
 
 AVFormatContext *ctx;
-AVDictionary *opts;
 int ost_index;   /* index of the first stream in output_streams */
 int64_t recording_time;  ///< desired length of the resulting file in 
microseconds == AV_TIME_BASE units
 int64_t start_time;  ///< start time in microseconds == AV_TIME_BASE 
units
@@ -678,7 +677,7 @@ int hw_device_setup_for_filter(FilterGraph *fg);
 
 int hwaccel_decode_init(AVCodecContext *avctx);
 
-int of_muxer_init(OutputFile *of, int64_t limit_filesize);
+int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize);
 /* open the muxer when all the streams are initialized */
 int of_check_init(OutputFile *of);
 int of_write_trailer(OutputFile *of);
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 36f781ae11..729cc1a7d8 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -46,6 +46,8 @@ typedef struct MuxStream {
 struct Muxer {
 MuxStream *streams;
 
+AVDictionary *opts;
+
 /* filesize limit expressed in bytes */
 int64_t limit_filesize;
 int64_t final_filesize;
@@ -294,7 +296,7 @@ int of_check_init(OutputFile *of)
 return 0;
 }
 
-ret = avformat_write_header(of->ctx, &of->opts);
+ret = avformat_write_header(of->ctx, &of->mux->opts);
 if (ret < 0) {
 av_log(NULL, AV_LOG_ERROR,
"Could not write header for output file #%d "
@@ -390,6 +392,7 @@ static void mux_free(Muxer **pmux, int nb_streams)
 av_fifo_freep(&ms->muxing_queue);
 }
 av_freep(&mux->streams);
+av_dict_free(&mux->opts);
 
 av_freep(pmux);
 }
@@ -409,12 +412,11 @@ void of_close(OutputFile **pof)
 if (s && s->oformat && !(s->oformat->flags & AVFMT_NOFILE))
 avio_closep(&s->pb);
 avformat_free_context(s);
-av_dict_free(&of->opts);
 
 av_freep(pof);
 }
 
-int of_muxer_init(OutputFile *of, int64_t limit_filesize)
+int of_muxer_init(OutputFile *of, AVDictionary *opts, int64_t limit_filesize)
 {
 Muxer *mux = av_mallocz(sizeof(*mux));
 int ret = 0;
@@ -440,6 +442,7 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize)
 }
 
 mux->limit_filesize = limit_filesize;
+mux->opts   = opts;
 
 if (strcmp(of->format->name, "rtp"))
 want_sdp = 0;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index c7d1d21a37..cb21a6a42c 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -2278,7 +2278,7 @@ static int open_output_file(OptionsContext *o, const char 
*filename)
 OutputFile *of;
 OutputStream *ost;
 InputStream  *ist;
-AVDictionary *unused_opts = NULL;
+AVDictionary *unused_opts = NULL, *format_opts = NULL;
 const AVDictionaryEntry *e = NULL;
 
 if (o->stop_time != INT64_MAX && o->recording_time != INT64_MAX) {
@@ -2303,7 +2303,7 @@ static int open_output_file(OptionsContext *o, const char 
*filename)
 of->recording_time = o->recording_time;
 of->start_time = o->start_time;
 of->shortest   = o->shortest;
-av_dict_copy(&of->opts, o->g->format_opts, 0);
+av_dict_copy(&format_opts, o->g->format_opts, 0);
 
 if (!strcmp(filename, "-"))
 filename = "pipe:";
@@ -2325,7 +2325,7 @@ static int open_output_file(OptionsContext *o, const char 
*filename)
 oc->flags|= AVFMT_FLAG_BITEXACT;
 of->bitexact  = 1;
 } else {
-of->bitexact  = check_opt_bitexact(oc, of->opts, "fflags",
+of->bitexact  = check_opt_bitexact(oc, format_opts, "fflags",
AVFMT_FLAG_BITEXACT);
 }
 
@@ -2702,7 +2702,7 @@ loop_end:
 /* open the file */
 if ((err = avio_open2(&oc->pb, filename, AVIO_FLAG_WRITE,
   &oc->interrupt_callback,
-  &of->opts)) < 0) {
+  &format_opts)) < 0) {
 print_error(filename, err);
 exit_program(1);
 }
@@ -2710,7 +2710,7 @@ loop_end:
 assert_file_overwrite(filename);
 
 if (o->mux_preload) {
-av_dict_set_int(&of->opts, "preload", o->mux_preload*AV_TIME_BASE, 0);
+av_dict_set_int(&format_opts, "preload", o->mux_preload*AV_TIME_BASE, 
0);
 }
 oc->max_delay = (int)(o->mux_max_delay * AV_TIME_BASE);
 
@@ -2904,7 +2904,7 @@ loop_end:
 exit_program(1);
 }
 
-err = of_muxer_init(of, o->limit_filesize);
+err = of_muxer_init(of, format_opts, o->limit_filesize);
 if (err < 0) {
 av_log(NULL, AV_LOG_FATAL, "Error initializing internal m

[FFmpeg-devel] [PATCH 13/28] ffmpeg: move closing the file into of_write_trailer()

2022-01-11 Thread Anton Khirnov
The current code postpones closing the files until after printing the
final report, which accesses the output file size. Deal with this by
storing the final file size before closing the file.
---
 fftools/ffmpeg.c | 13 -
 fftools/ffmpeg_mux.c | 16 +++-
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index f28cc17f51..4f5c2cc49a 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -4348,7 +4348,6 @@ static int transcode_step(void)
 static int transcode(void)
 {
 int ret, i;
-AVFormatContext *os;
 OutputStream *ost;
 InputStream *ist;
 int64_t timer_start;
@@ -4417,18 +4416,6 @@ static int transcode(void)
 /* dump report by using the first video and audio streams */
 print_report(1, timer_start, av_gettime_relative());
 
-/* close the output files */
-for (i = 0; i < nb_output_files; i++) {
-os = output_files[i]->ctx;
-if (os && os->oformat && !(os->oformat->flags & AVFMT_NOFILE)) {
-if ((ret = avio_closep(&os->pb)) < 0) {
-av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n", 
os->url, av_err2str(ret));
-if (exit_on_error)
-exit_program(1);
-}
-}
-}
-
 /* close each encoder */
 for (i = 0; i < nb_output_streams; i++) {
 ost = output_streams[i];
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 3b322ecb71..13b5d2d220 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -35,6 +35,7 @@
 struct Muxer {
 /* filesize limit expressed in bytes */
 int64_t limit_filesize;
+int64_t final_filesize;
 int header_written;
 };
 
@@ -318,6 +319,17 @@ int of_write_trailer(OutputFile *of)
 return ret;
 }
 
+of->mux->final_filesize = of_filesize(of);
+
+if (!(of->format->flags & AVFMT_NOFILE)) {
+ret = avio_closep(&of->ctx->pb);
+if (ret < 0) {
+av_log(NULL, AV_LOG_ERROR, "Error closing file %s: %s\n",
+   of->ctx->url, av_err2str(ret));
+return ret;
+}
+}
+
 return 0;
 }
 
@@ -374,7 +386,9 @@ int64_t of_filesize(OutputFile *of)
 AVIOContext *pb = of->ctx->pb;
 int64_t ret = -1;
 
-if (pb) {
+if (of->mux->final_filesize)
+ret = of->mux->final_filesize;
+else if (pb) {
 ret = avio_size(pb);
 if (ret <= 0) // FIXME improve avio_size() so it works with non 
seekable output too
 ret = avio_tell(pb);
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 17/28] ffmpeg: move the mux queue into muxer private data

2022-01-11 Thread Anton Khirnov
The muxing queue currently lives in OutputStream, which is a very large
struct storing the state for both encoding and muxing. The muxing queue
is only used by the code in ffmpeg_mux, so it makes sense to restrict it
to that file.

This makes the first step towards reducing the scope of OutputStream.
---
 fftools/ffmpeg.c |  9 -
 fftools/ffmpeg.h |  9 -
 fftools/ffmpeg_mux.c | 91 
 fftools/ffmpeg_opt.c |  6 ---
 4 files changed, 76 insertions(+), 39 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 6c774e9615..c1bb3926c4 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -595,15 +595,6 @@ static void ffmpeg_cleanup(int ret)
 avcodec_free_context(&ost->enc_ctx);
 avcodec_parameters_free(&ost->ref_par);
 
-if (ost->muxing_queue) {
-while (av_fifo_size(ost->muxing_queue)) {
-AVPacket *pkt;
-av_fifo_generic_read(ost->muxing_queue, &pkt, sizeof(pkt), 
NULL);
-av_packet_free(&pkt);
-}
-av_fifo_freep(&ost->muxing_queue);
-}
-
 av_freep(&output_streams[i]);
 }
 #if HAVE_THREADS
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index e828f71dc0..28df1b179f 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -555,15 +555,6 @@ typedef struct OutputStream {
 
 int max_muxing_queue_size;
 
-/* the packets are buffered here until the muxer is ready to be 
initialized */
-AVFifoBuffer *muxing_queue;
-
-/*
- * The size of the AVPackets' buffers in queue.
- * Updated when a packet is either pushed or pulled from the queue.
- */
-size_t muxing_queue_data_size;
-
 /* Threshold after which max_muxing_queue_size will be in effect */
 size_t muxing_queue_data_threshold;
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index f4d76e1533..f03202bbb7 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -32,7 +32,20 @@
 #include "libavformat/avformat.h"
 #include "libavformat/avio.h"
 
+typedef struct MuxStream {
+/* the packets are buffered here until the muxer is ready to be 
initialized */
+AVFifoBuffer *muxing_queue;
+
+/*
+ * The size of the AVPackets' buffers in queue.
+ * Updated when a packet is either pushed or pulled from the queue.
+ */
+size_t muxing_queue_data_size;
+} MuxStream;
+
 struct Muxer {
+MuxStream *streams;
+
 /* filesize limit expressed in bytes */
 int64_t limit_filesize;
 int64_t final_filesize;
@@ -55,6 +68,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
 {
 AVFormatContext *s = of->ctx;
 AVStream *st = ost->st;
+MuxStream *ms = &of->mux->streams[st->index];
 int ret;
 
 /*
@@ -76,10 +90,10 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
 if (!of->mux->header_written) {
 AVPacket *tmp_pkt;
 /* the muxer is not initialized yet, buffer the packet */
-if (!av_fifo_space(ost->muxing_queue)) {
-size_t cur_size = av_fifo_size(ost->muxing_queue);
+if (!av_fifo_space(ms->muxing_queue)) {
+size_t cur_size = av_fifo_size(ms->muxing_queue);
 unsigned int are_we_over_size =
-(ost->muxing_queue_data_size + pkt->size) > 
ost->muxing_queue_data_threshold;
+(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 new_size = FFMIN(2 * cur_size, limit);
 
@@ -89,7 +103,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
ost->file_index, ost->st->index);
 exit_program(1);
 }
-ret = av_fifo_realloc2(ost->muxing_queue, new_size);
+ret = av_fifo_realloc2(ms->muxing_queue, new_size);
 if (ret < 0)
 exit_program(1);
 }
@@ -100,8 +114,8 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
 if (!tmp_pkt)
 exit_program(1);
 av_packet_move_ref(tmp_pkt, pkt);
-ost->muxing_queue_data_size += tmp_pkt->size;
-av_fifo_generic_write(ost->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), 
NULL);
+ms->muxing_queue_data_size += tmp_pkt->size;
+av_fifo_generic_write(ms->muxing_queue, &tmp_pkt, sizeof(tmp_pkt), 
NULL);
 return;
 }
 
@@ -283,16 +297,17 @@ int of_check_init(OutputFile *of)
 
 /* flush the muxing queues */
 for (i = 0; i < of->ctx->nb_streams; i++) {
+MuxStream *ms = &of->mux->streams[i];
 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(ost->muxing_queue))
+if (!av_fifo_size(ms->muxing_queue))
 ost->mux_timebase = ost

[FFmpeg-devel] [PATCH 23/28] ffmpeg: move processing video stats to ffmpeg_mux

2022-01-11 Thread Anton Khirnov
Currently it is called from
- do_video_out(), at the end of each encode loop iteration
- from flush_encoders(), after muxing each packet

Since this function processes the data from the last encoded packet,
neither of the above is fully correct, because
- an encoder can in principle produce multiple packets per one submitted
  frame
- bitstream filters may modify the number of encoded packets or their
  properties.

It thus makes most sense to call this function right before sending the
packet to the muxer.
---
 fftools/ffmpeg.c | 61 +---
 fftools/ffmpeg.h |  5 
 fftools/ffmpeg_mux.c | 47 ++
 3 files changed, 53 insertions(+), 60 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index f39b1b9375..c89a95937e 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -109,7 +109,7 @@
 const char program_name[] = "ffmpeg";
 const int program_birth_year = 2000;
 
-static FILE *vstats_file;
+FILE *vstats_file;
 
 const char *const forced_keyframes_const_names[] = {
 "n",
@@ -126,7 +126,6 @@ typedef struct BenchmarkTimeStamps {
 int64_t sys_usec;
 } BenchmarkTimeStamps;
 
-static void do_video_stats(OutputStream *ost, int frame_size);
 static BenchmarkTimeStamps get_benchmark_time_stamps(void);
 static int64_t getmaxrss(void);
 static int ifilter_has_all_input_formats(FilterGraph *fg);
@@ -977,7 +976,6 @@ static void do_video_out(OutputFile *of,
 double delta, delta0;
 double duration = 0;
 double sync_ipts = AV_NOPTS_VALUE;
-int frame_size = 0;
 InputStream *ist = NULL;
 AVFilterContext *filter = ost->filter->filter;
 
@@ -1213,7 +1211,6 @@ static void do_video_out(OutputFile *of,
 av_ts2str(pkt->dts), av_ts2timestr(pkt->dts, 
&ost->mux_timebase));
 }
 
-frame_size = pkt->size;
 output_packet(of, pkt, ost, 0);
 
 /* if two pass, output log */
@@ -1223,9 +1220,6 @@ static void do_video_out(OutputFile *of,
 }
 ost->sync_opts++;
 ost->frame_number++;
-
-if (vstats_filename && frame_size)
-do_video_stats(ost, frame_size);
 }
 
 av_frame_unref(ost->last_frame);
@@ -1238,54 +1232,6 @@ error:
 exit_program(1);
 }
 
-static double psnr(double d)
-{
-return -10.0 * log10(d);
-}
-
-static void do_video_stats(OutputStream *ost, int frame_size)
-{
-AVCodecContext *enc;
-int frame_number;
-double ti1, bitrate, avg_bitrate;
-
-/* this is executed just the first time do_video_stats is called */
-if (!vstats_file) {
-vstats_file = fopen(vstats_filename, "w");
-if (!vstats_file) {
-perror("fopen");
-exit_program(1);
-}
-}
-
-enc = ost->enc_ctx;
-if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-frame_number = ost->st->nb_frames;
-if (vstats_version <= 1) {
-fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-ost->quality / (float)FF_QP2LAMBDA);
-} else  {
-fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", 
ost->file_index, ost->index, frame_number,
-ost->quality / (float)FF_QP2LAMBDA);
-}
-
-if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
-fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / 
(enc->width * enc->height * 255.0 * 255.0)));
-
-fprintf(vstats_file,"f_size= %6d ", frame_size);
-/* compute pts value */
-ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
-if (ti1 < 0.01)
-ti1 = 0.01;
-
-bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
-avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
-fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s 
avg_br= %7.1fkbits/s ",
-   (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
-fprintf(vstats_file, "type= %c\n", 
av_get_picture_type_char(ost->pict_type));
-}
-}
-
 static void finish_output_stream(OutputStream *ost)
 {
 OutputFile *of = output_files[ost->file_index];
@@ -1764,7 +1710,6 @@ static void flush_encoders(void)
 for (;;) {
 const char *desc = NULL;
 AVPacket *pkt = ost->pkt;
-int pkt_size;
 
 switch (enc->codec_type) {
 case AVMEDIA_TYPE_AUDIO:
@@ -1808,11 +1753,7 @@ static void flush_encoders(void)
 continue;
 }
 av_packet_rescale_ts(pkt, enc->time_base, ost->mux_timebase);
-pkt_size = pkt->size;
 output_packet(of, pkt, ost, 0);
-if (ost->enc_ctx->codec_type == AVMEDIA_TYPE_VIDEO && 
vstats_filename) {
-do_video_stats(ost, pkt_size);
-}
 }
 }
 }
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d9f997bf7a..908dc2eb38 100644
--- a/fftools/ffmpeg.h
+++ b/f

[FFmpeg-devel] [PATCH 24/28] ffmpeg_mux: drop a useless check and reduce indentation

2022-01-11 Thread Anton Khirnov
do_video_stats() is only ever called for video.
---
 fftools/ffmpeg_mux.c | 42 --
 1 file changed, 20 insertions(+), 22 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 76d9d4b9c4..8a64661c9c 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -72,31 +72,29 @@ static void do_video_stats(OutputStream *ost, int 
frame_size)
 }
 
 enc = ost->enc_ctx;
-if (enc->codec_type == AVMEDIA_TYPE_VIDEO) {
-frame_number = ost->st->nb_frames;
-if (vstats_version <= 1) {
-fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-ost->quality / (float)FF_QP2LAMBDA);
-} else  {
-fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", 
ost->file_index, ost->index, frame_number,
-ost->quality / (float)FF_QP2LAMBDA);
-}
+frame_number = ost->st->nb_frames;
+if (vstats_version <= 1) {
+fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
+ost->quality / (float)FF_QP2LAMBDA);
+} else  {
+fprintf(vstats_file, "out= %2d st= %2d frame= %5d q= %2.1f ", 
ost->file_index, ost->index, frame_number,
+ost->quality / (float)FF_QP2LAMBDA);
+}
 
-if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
-fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / 
(enc->width * enc->height * 255.0 * 255.0)));
+if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
+fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width 
* enc->height * 255.0 * 255.0)));
 
-fprintf(vstats_file,"f_size= %6d ", frame_size);
-/* compute pts value */
-ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
-if (ti1 < 0.01)
-ti1 = 0.01;
+fprintf(vstats_file,"f_size= %6d ", frame_size);
+/* compute pts value */
+ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
+if (ti1 < 0.01)
+ti1 = 0.01;
 
-bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
-avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
-fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s 
avg_br= %7.1fkbits/s ",
-   (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
-fprintf(vstats_file, "type= %c\n", 
av_get_picture_type_char(ost->pict_type));
-}
+bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
+avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
+fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= 
%7.1fkbits/s ",
+   (double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
+fprintf(vstats_file, "type= %c\n", 
av_get_picture_type_char(ost->pict_type));
 }
 
 static void close_all_output_streams(OutputStream *ost, OSTFinished 
this_stream, OSTFinished others)
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 27/28] ffmpeg_mux: merge variable declaration and initialization

2022-01-11 Thread Anton Khirnov
---
 fftools/ffmpeg_mux.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 54020880eb..21771b3ae6 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -58,7 +58,7 @@ static int want_sdp = 1;
 
 static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
 {
-AVCodecContext *enc;
+AVCodecContext *enc = ost->enc_ctx;
 int frame_number;
 double ti1, bitrate, avg_bitrate;
 
@@ -71,7 +71,6 @@ static void do_video_stats(OutputStream *ost, const AVPacket 
*pkt)
 }
 }
 
-enc = ost->enc_ctx;
 frame_number = ost->packets_written - 1;
 if (vstats_version <= 1) {
 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 25/28] ffmpeg_mux: stop using AVStream.nb_frames in do_video_stats()

2022-01-11 Thread Anton Khirnov
Its use for muxing is not documented, in practice it is incremented per
each packet successfully passed to the muxer's write_packet(). Since
there is a lot of indirection between ffmpeg submitting a packet to the
muxer and it actually being written (e.g. the interleaving queue), using
nb_frames to count packets sent to the muxer is incorrect. Use
OutputStream.packets_written instead.
---
 fftools/ffmpeg_mux.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 8a64661c9c..ef6b7ddd97 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -72,7 +72,7 @@ static void do_video_stats(OutputStream *ost, int frame_size)
 }
 
 enc = ost->enc_ctx;
-frame_number = ost->st->nb_frames;
+frame_number = ost->packets_written - 1;
 if (vstats_version <= 1) {
 fprintf(vstats_file, "frame= %5d q= %2.1f ", frame_number,
 ost->quality / (float)FF_QP2LAMBDA);
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 26/28] ffmpeg_mux: stop using av_stream_get_end_pts() in do_video_stats()

2022-01-11 Thread Anton Khirnov
It retrieves libavformat's internal dts value (contrary to the
function's name), which is not necessary here because we can access the
packet directly.
---
 fftools/ffmpeg_mux.c | 10 +-
 1 file changed, 5 insertions(+), 5 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index ef6b7ddd97..54020880eb 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -56,7 +56,7 @@ struct Muxer {
 
 static int want_sdp = 1;
 
-static void do_video_stats(OutputStream *ost, int frame_size)
+static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
 {
 AVCodecContext *enc;
 int frame_number;
@@ -84,13 +84,13 @@ static void do_video_stats(OutputStream *ost, int 
frame_size)
 if (ost->error[0]>=0 && (enc->flags & AV_CODEC_FLAG_PSNR))
 fprintf(vstats_file, "PSNR= %6.2f ", psnr(ost->error[0] / (enc->width 
* enc->height * 255.0 * 255.0)));
 
-fprintf(vstats_file,"f_size= %6d ", frame_size);
+fprintf(vstats_file,"f_size= %6d ", pkt->size);
 /* compute pts value */
-ti1 = av_stream_get_end_pts(ost->st) * av_q2d(ost->st->time_base);
+ti1 = pkt->dts * av_q2d(ost->st->time_base);
 if (ti1 < 0.01)
 ti1 = 0.01;
 
-bitrate = (frame_size * 8) / av_q2d(enc->time_base) / 1000.0;
+bitrate = (pkt->size * 8) / av_q2d(enc->time_base) / 1000.0;
 avg_bitrate = (double)(ost->data_size * 8) / ti1 / 1000.0;
 fprintf(vstats_file, "s_size= %8.0fkB time= %0.3f br= %7.1fkbits/s avg_br= 
%7.1fkbits/s ",
(double)ost->data_size / 1024, ti1, bitrate, avg_bitrate);
@@ -223,7 +223,7 @@ static void write_packet(OutputFile *of, OutputStream *ost, 
AVPacket *pkt)
 
 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
 ost->encoding_needed && vstats_filename)
-do_video_stats(ost, pkt->size);
+do_video_stats(ost, pkt);
 
 pkt->stream_index = ost->index;
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 12/28] ffmpeg: write the header for stream-less outputs when initializing the muxer

2022-01-11 Thread Anton Khirnov
There is no reason to delay this.
---
 fftools/ffmpeg.c | 11 ---
 fftools/ffmpeg_mux.c |  7 +++
 2 files changed, 7 insertions(+), 11 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index d856b5b38f..f28cc17f51 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -3313,7 +3313,6 @@ static void report_new_stream(int input_index, AVPacket 
*pkt)
 static int transcode_init(void)
 {
 int ret = 0, i, j, k;
-AVFormatContext *oc;
 OutputStream *ost;
 InputStream *ist;
 char error[1024] = {0};
@@ -3387,16 +3386,6 @@ static int transcode_init(void)
 }
 }
 
-/* write headers for files with no streams */
-for (i = 0; i < nb_output_files; i++) {
-oc = output_files[i]->ctx;
-if (output_files[i]->format->flags & AVFMT_NOSTREAMS && oc->nb_streams 
== 0) {
-ret = of_check_init(output_files[i]);
-if (ret < 0)
-goto dump_format;
-}
-}
-
  dump_format:
 /* dump the stream mapping */
 av_log(NULL, AV_LOG_INFO, "Stream mapping:\n");
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 6c3052ebe1..3b322ecb71 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -354,6 +354,13 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize)
 if (strcmp(of->format->name, "rtp"))
 want_sdp = 0;
 
+/* write the header for files with no streams */
+if (of->format->flags & AVFMT_NOSTREAMS && of->ctx->nb_streams == 0) {
+int ret = of_check_init(of);
+if (ret < 0)
+return ret;
+}
+
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 09/28] ffmpeg: fix the type of limit_filesize

2022-01-11 Thread Anton Khirnov
The option is parsed as INT64 (signed). It is also compared to the
output of avio_tell(), which is also int64_t.
---
 fftools/ffmpeg.h | 4 ++--
 fftools/ffmpeg_opt.c | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index dfcaa875d9..80b90e372d 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -145,7 +145,7 @@ typedef struct OptionsContext {
 
 int64_t recording_time;
 int64_t stop_time;
-uint64_t limit_filesize;
+int64_t limit_filesize;
 float mux_preload;
 float mux_max_delay;
 int shortest;
@@ -586,7 +586,7 @@ typedef struct OutputFile {
 int ost_index;   /* index of the first stream in output_streams */
 int64_t recording_time;  ///< desired length of the resulting file in 
microseconds == AV_TIME_BASE units
 int64_t start_time;  ///< start time in microseconds == AV_TIME_BASE 
units
-uint64_t limit_filesize; /* filesize limit expressed in bytes */
+int64_t limit_filesize; /* filesize limit expressed in bytes */
 
 int shortest;
 } OutputFile;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 2ed09d4a7a..4d2b80683e 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -220,7 +220,7 @@ static void init_options(OptionsContext *o)
 o->start_time = AV_NOPTS_VALUE;
 o->start_time_eof = AV_NOPTS_VALUE;
 o->recording_time = INT64_MAX;
-o->limit_filesize = UINT64_MAX;
+o->limit_filesize = INT64_MAX;
 o->chapters_input_file = INT_MAX;
 o->accurate_seek  = 1;
 o->thread_queue_size = -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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 15/28] ffmpeg: access output file chapters through a wrapper

2022-01-11 Thread Anton Khirnov
Avoid accessing the muxer context directly, as this will become
forbidden in future commits.
---
 fftools/ffmpeg.c | 15 +--
 fftools/ffmpeg.h |  2 ++
 fftools/ffmpeg_mux.c |  7 +++
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 5165573674..2637b25526 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2909,12 +2909,15 @@ static void parse_forced_key_frames(char *kf, 
OutputStream *ost,
 *next++ = 0;
 
 if (!memcmp(p, "chapters", 8)) {
-
-AVFormatContext *avf = output_files[ost->file_index]->ctx;
+OutputFile *of = output_files[ost->file_index];
+AVChapter * const *ch;
+unsigned intnb_ch;
 int j;
 
-if (avf->nb_chapters > INT_MAX - size ||
-!(pts = av_realloc_f(pts, size += avf->nb_chapters - 1,
+ch = of_get_chapters(of, &nb_ch);
+
+if (nb_ch > INT_MAX - size ||
+!(pts = av_realloc_f(pts, size += nb_ch - 1,
  sizeof(*pts {
 av_log(NULL, AV_LOG_FATAL,
"Could not allocate forced key frames array.\n");
@@ -2923,8 +2926,8 @@ static void parse_forced_key_frames(char *kf, 
OutputStream *ost,
 t = p[8] ? parse_time_or_die("force_key_frames", p + 8, 1) : 0;
 t = av_rescale_q(t, AV_TIME_BASE_Q, avctx->time_base);
 
-for (j = 0; j < avf->nb_chapters; j++) {
-AVChapter *c = avf->chapters[j];
+for (j = 0; j < nb_ch; j++) {
+const AVChapter *c = ch[j];
 av_assert1(index < size);
 pts[index++] = av_rescale_q(c->start, c->time_base,
 avctx->time_base) + t;
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index 66062f0072..e828f71dc0 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -697,5 +697,7 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
  int unqueue);
 int of_finished(OutputFile *of);
 int64_t of_filesize(OutputFile *of);
+AVChapter * const *
+of_get_chapters(OutputFile *of, unsigned int *nb_chapters);
 
 #endif /* FFTOOLS_FFMPEG_H */
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 13b5d2d220..f4d76e1533 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -396,3 +396,10 @@ int64_t of_filesize(OutputFile *of)
 
 return ret;
 }
+
+AVChapter * const *
+of_get_chapters(OutputFile *of, unsigned int *nb_chapters)
+{
+*nb_chapters = of->ctx->nb_chapters;
+return of->ctx->chapters;
+}
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 11/28] ffmpeg: set want_sdp when initializing the muxer

2022-01-11 Thread Anton Khirnov
Allows making the variable local to ffmpeg_mux.
---
 fftools/ffmpeg.c | 9 +
 fftools/ffmpeg.h | 1 -
 fftools/ffmpeg_mux.c | 5 +
 3 files changed, 6 insertions(+), 9 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 37c4a0d285..d856b5b38f 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -138,8 +138,6 @@ static int nb_frames_drop = 0;
 static int64_t decode_error_stat[2];
 unsigned nb_output_dumped = 0;
 
-int want_sdp = 1;
-
 static BenchmarkTimeStamps current_time;
 AVIOContext *progress_avio = NULL;
 
@@ -4553,7 +4551,7 @@ static void log_callback_null(void *ptr, int level, const 
char *fmt, va_list vl)
 
 int main(int argc, char **argv)
 {
-int i, ret;
+int ret;
 BenchmarkTimeStamps ti;
 
 init_dynload();
@@ -4596,11 +4594,6 @@ int main(int argc, char **argv)
 exit_program(1);
 }
 
-for (i = 0; i < nb_output_files; i++) {
-if (strcmp(output_files[i]->format->name, "rtp"))
-want_sdp = 0;
-}
-
 current_time = ti = get_benchmark_time_stamps();
 if (transcode() < 0)
 exit_program(1);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index cae9800da3..d926254a73 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -646,7 +646,6 @@ extern char *qsv_device;
 #endif
 extern HWDevice *filter_hw_device;
 
-extern int want_sdp;
 extern unsigned nb_output_dumped;
 extern int main_return_code;
 
diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index aff40776f6..6c3052ebe1 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -38,6 +38,8 @@ struct Muxer {
 int header_written;
 };
 
+static int want_sdp = 1;
+
 static void close_all_output_streams(OutputStream *ost, OSTFinished 
this_stream, OSTFinished others)
 {
 int i;
@@ -349,6 +351,9 @@ int of_muxer_init(OutputFile *of, int64_t limit_filesize)
 
 mux->limit_filesize = limit_filesize;
 
+if (strcmp(of->format->name, "rtp"))
+want_sdp = 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 14/28] ffmpeg: refactor the code checking for bitexact output

2022-01-11 Thread Anton Khirnov
Figure out earlier whether the output stream/file should be bitexact and
store this information in a flag in OutputFile/OutputStream.

Stop accessing the muxer in set_encoder_id(), which will become
forbidden in future commits.
---
 fftools/ffmpeg.c | 21 +
 fftools/ffmpeg.h |  2 ++
 fftools/ffmpeg_opt.c | 27 ++-
 3 files changed, 29 insertions(+), 21 deletions(-)

diff --git a/fftools/ffmpeg.c b/fftools/ffmpeg.c
index 4f5c2cc49a..5165573674 100644
--- a/fftools/ffmpeg.c
+++ b/fftools/ffmpeg.c
@@ -2864,37 +2864,18 @@ static int init_output_stream_streamcopy(OutputStream 
*ost)
 
 static void set_encoder_id(OutputFile *of, OutputStream *ost)
 {
-const AVDictionaryEntry *e;
-
 uint8_t *encoder_string;
 int encoder_string_len;
-int format_flags = 0;
-int codec_flags = ost->enc_ctx->flags;
 
 if (av_dict_get(ost->st->metadata, "encoder",  NULL, 0))
 return;
 
-e = av_dict_get(of->opts, "fflags", NULL, 0);
-if (e) {
-const AVOption *o = av_opt_find(of->ctx, "fflags", NULL, 0, 0);
-if (!o)
-return;
-av_opt_eval_flags(of->ctx, o, e->value, &format_flags);
-}
-e = av_dict_get(ost->encoder_opts, "flags", NULL, 0);
-if (e) {
-const AVOption *o = av_opt_find(ost->enc_ctx, "flags", NULL, 0, 0);
-if (!o)
-return;
-av_opt_eval_flags(ost->enc_ctx, o, e->value, &codec_flags);
-}
-
 encoder_string_len = sizeof(LIBAVCODEC_IDENT) + strlen(ost->enc->name) + 2;
 encoder_string = av_mallocz(encoder_string_len);
 if (!encoder_string)
 exit_program(1);
 
-if (!(format_flags & AVFMT_FLAG_BITEXACT) && !(codec_flags & 
AV_CODEC_FLAG_BITEXACT))
+if (!of->bitexact && !ost->bitexact)
 av_strlcpy(encoder_string, LIBAVCODEC_IDENT " ", encoder_string_len);
 else
 av_strlcpy(encoder_string, "Lavc ", encoder_string_len);
diff --git a/fftools/ffmpeg.h b/fftools/ffmpeg.h
index d926254a73..66062f0072 100644
--- a/fftools/ffmpeg.h
+++ b/fftools/ffmpeg.h
@@ -491,6 +491,7 @@ typedef struct OutputStream {
 int top_field_first;
 int rotate_overridden;
 int autoscale;
+int bitexact;
 int bits_per_raw_sample;
 double rotate_override_value;
 
@@ -588,6 +589,7 @@ typedef struct OutputFile {
 int64_t start_time;  ///< start time in microseconds == AV_TIME_BASE 
units
 
 int shortest;
+int bitexact;
 } OutputFile;
 
 extern InputStream **input_streams;
diff --git a/fftools/ffmpeg_opt.c b/fftools/ffmpeg_opt.c
index 0b4f437912..ed3fd818d0 100644
--- a/fftools/ffmpeg_opt.c
+++ b/fftools/ffmpeg_opt.c
@@ -1427,6 +1427,22 @@ static int choose_encoder(OptionsContext *o, 
AVFormatContext *s, OutputStream *o
 return 0;
 }
 
+static int check_opt_bitexact(void *ctx, const AVDictionary *opts,
+  const char *opt_name, int flag)
+{
+const AVDictionaryEntry *e = av_dict_get(opts, opt_name, NULL, 0);
+
+if (e) {
+const AVOption *o = av_opt_find(ctx, opt_name, NULL, 0, 0);
+int val = 0;
+if (!o)
+return 0;
+av_opt_eval_flags(ctx, o, e->value, &val);
+return !!(val & flag);
+}
+return 0;
+}
+
 static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, 
enum AVMediaType type, int source_index)
 {
 OutputStream *ost;
@@ -1522,8 +1538,13 @@ static OutputStream *new_output_stream(OptionsContext 
*o, AVFormatContext *oc, e
 }
 
 
-if (o->bitexact)
+if (o->bitexact) {
 ost->enc_ctx->flags |= AV_CODEC_FLAG_BITEXACT;
+ost->bitexact= 1;
+} else {
+ost->bitexact= check_opt_bitexact(ost->enc_ctx, 
ost->encoder_opts, "flags",
+  AV_CODEC_FLAG_BITEXACT);
+}
 
 MATCH_PER_STREAM_OPT(time_bases, str, time_base, oc, st);
 if (time_base) {
@@ -2308,6 +2329,10 @@ static int open_output_file(OptionsContext *o, const 
char *filename)
 
 if (o->bitexact) {
 oc->flags|= AVFMT_FLAG_BITEXACT;
+of->bitexact  = 1;
+} else {
+of->bitexact  = check_opt_bitexact(oc, of->opts, "fflags",
+   AVFMT_FLAG_BITEXACT);
 }
 
 /* create streams for all unlabeled output pads */
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 19/28] ffmpeg_mux: split queuing packets into a separate function

2022-01-11 Thread Anton Khirnov
---
 fftools/ffmpeg_mux.c | 72 +++-
 1 file changed, 44 insertions(+), 28 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index f48031096d..4ab2279739 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -63,12 +63,50 @@ static void close_all_output_streams(OutputStream *ost, 
OSTFinished this_stream,
 }
 }
 
+static int queue_packet(OutputFile *of, OutputStream *ost, AVPacket *pkt)
+{
+MuxStream *ms = &of->mux->streams[ost->index];
+AVPacket *tmp_pkt;
+int ret;
+
+if (!av_fifo_space(ms->muxing_queue)) {
+size_t cur_size = av_fifo_size(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 new_size = FFMIN(2 * cur_size, limit);
+
+if (new_size <= cur_size) {
+av_log(NULL, AV_LOG_ERROR,
+   "Too many packets buffered for output stream %d:%d.\n",
+   ost->file_index, ost->st->index);
+return AVERROR(ENOSPC);
+}
+ret = av_fifo_realloc2(ms->muxing_queue, new_size);
+if (ret < 0)
+return ret;
+}
+
+ret = av_packet_make_refcounted(pkt);
+if (ret < 0)
+return ret;
+
+tmp_pkt = av_packet_alloc();
+if (!tmp_pkt)
+return AVERROR(ENOMEM);
+
+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);
+
+return 0;
+}
+
 void of_write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost,
  int unqueue)
 {
 AVFormatContext *s = of->ctx;
 AVStream *st = ost->st;
-MuxStream *ms = &of->mux->streams[st->index];
 int ret;
 
 /*
@@ -87,35 +125,13 @@ void of_write_packet(OutputFile *of, AVPacket *pkt, 
OutputStream *ost,
 ost->frame_number++;
 }
 
+/* the muxer is not initialized yet, buffer the packet */
 if (!of->mux->header_written) {
-AVPacket *tmp_pkt;
-/* the muxer is not initialized yet, buffer the packet */
-if (!av_fifo_space(ms->muxing_queue)) {
-size_t cur_size = av_fifo_size(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 new_size = FFMIN(2 * cur_size, limit);
-
-if (new_size <= cur_size) {
-av_log(NULL, AV_LOG_ERROR,
-   "Too many packets buffered for output stream %d:%d.\n",
-   ost->file_index, ost->st->index);
-exit_program(1);
-}
-ret = av_fifo_realloc2(ms->muxing_queue, new_size);
-if (ret < 0)
-exit_program(1);
-}
-ret = av_packet_make_refcounted(pkt);
-if (ret < 0)
-exit_program(1);
-tmp_pkt = av_packet_alloc();
-if (!tmp_pkt)
+ret = queue_packet(of, ost, pkt);
+if (ret < 0) {
+av_packet_unref(pkt);
 exit_program(1);
-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);
+}
 return;
 }
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 28/28] ffmpeg_mux: move processing AV_PKT_DATA_QUALITY_STATS to do_video_stats()

2022-01-11 Thread Anton Khirnov
This is a more appropriate place for this code, since the values we read
from AV_PKT_DATA_QUALITY_STATS side data are primarily written into
video stats.

Rename the function to update_video_stats() to better reflect its new
purpose.
---
 fftools/ffmpeg_mux.c | 37 +++--
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/fftools/ffmpeg_mux.c b/fftools/ffmpeg_mux.c
index 21771b3ae6..52986b002a 100644
--- a/fftools/ffmpeg_mux.c
+++ b/fftools/ffmpeg_mux.c
@@ -56,13 +56,28 @@ struct Muxer {
 
 static int want_sdp = 1;
 
-static void do_video_stats(OutputStream *ost, const AVPacket *pkt)
+static void update_video_stats(OutputStream *ost, const AVPacket *pkt, int 
write_vstats)
 {
+const uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
+NULL);
 AVCodecContext *enc = ost->enc_ctx;
 int frame_number;
 double ti1, bitrate, avg_bitrate;
 
-/* this is executed just the first time do_video_stats is called */
+ost->quality = sd ? AV_RL32(sd) : -1;
+ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
+
+for (int i = 0; i < FF_ARRAY_ELEMS(ost->error); i++) {
+if (sd && i < sd[5])
+ost->error[i] = AV_RL64(sd + 8 + 8 * i);
+else
+ost->error[i] = -1;
+}
+
+if (!write_vstats)
+return;
+
+/* this is executed just the first time update_video_stats is called */
 if (!vstats_file) {
 vstats_file = fopen(vstats_filename, "w");
 if (!vstats_file) {
@@ -155,19 +170,6 @@ static void write_packet(OutputFile *of, OutputStream 
*ost, AVPacket *pkt)
 pkt->pts = pkt->dts = AV_NOPTS_VALUE;
 
 if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
-int i;
-uint8_t *sd = av_packet_get_side_data(pkt, AV_PKT_DATA_QUALITY_STATS,
-  NULL);
-ost->quality = sd ? AV_RL32(sd) : -1;
-ost->pict_type = sd ? sd[4] : AV_PICTURE_TYPE_NONE;
-
-for (i = 0; ierror); i++) {
-if (sd && i < sd[5])
-ost->error[i] = AV_RL64(sd + 8 + 8*i);
-else
-ost->error[i] = -1;
-}
-
 if (ost->frame_rate.num && ost->is_cfr) {
 if (pkt->duration > 0)
 av_log(NULL, AV_LOG_WARNING, "Overriding packet duration by 
frame rate, this should not happen\n");
@@ -220,9 +222,8 @@ static void write_packet(OutputFile *of, OutputStream *ost, 
AVPacket *pkt)
 ost->data_size += pkt->size;
 ost->packets_written++;
 
-if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO &&
-ost->encoding_needed && vstats_filename)
-do_video_stats(ost, pkt);
+if (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO && ost->encoding_needed)
+update_video_stats(ost, pkt, !!vstats_filename);
 
 pkt->stream_index = ost->index;
 
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread Gijs Peskens
Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
behavior.

Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
and error out in that case.
---
 doc/protocols.texi|  9 +
 libavformat/librist.c | 37 +
 2 files changed, 46 insertions(+)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index d207df0b52..f1acf0cc77 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
retransmission of data.
 Default value is 0 which means the librist default (1 sec). Maximum value is 30
 seconds.
 
+@item fifo_size
+Size of the librist receiver output fifo in number of packets. This must be a 
+power of 2.
+Defaults to 8192 (vs the libRIST default of 1024).
+
+@item overrun_nonfatal=@var{1|0}
+Survive in case of libRIST fifo buffer overrun. Default
+value is 0.
+
 @item pkt_size
 Set maximum packet size for sending data. 1316 by default.
 
diff --git a/libavformat/librist.c b/libavformat/librist.c
index 378b635ea7..53f83d3668 100644
--- a/libavformat/librist.c
+++ b/libavformat/librist.c
@@ -43,6 +43,9 @@
 ((patch) + ((minor)* 0x100) + ((major) *0x1))
 #define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
 #define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
+#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
+
+#define FF_LIBRIST_FIFO_SIZE_DEFAULT 8192
 
 typedef struct RISTContext {
 const AVClass *class;
@@ -52,6 +55,8 @@ typedef struct RISTContext {
 int packet_size;
 int log_level;
 int encryption;
+int fifo_size;
+int overrun_nonfatal;
 char *secret;
 
 struct rist_logging_settings logging_settings;
@@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
 { "main",NULL,  0,   
AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" 
},
 { "advanced",NULL,  0,   
AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" 
},
 { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
+{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", 
OFFSET(fifo_size), AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 
9, 2 << 15, .flags = D|E },
+{ "overrun_nonfatal", "survive in case of libRIST receiving circular 
buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,   
 D },
 { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT,  
 {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
 { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT,  
 {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
 { "secret", "set encryption secret",OFFSET(secret),  
AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
@@ -161,6 +168,20 @@ static int librist_open(URLContext *h, const char *uri, 
int flags)
 if (ret < 0)
 goto err;
 
+//Prior to 4.2.0 there was a bug in libRIST which made this call always 
fail.
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (flags & AVIO_FLAG_READ) {
+ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
+if (ret != 0) {
+goto err;
+}
+}
+#else
+if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
+av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which fails 
setting the fifo buffer size\n");
+}
+#endif
+
 if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
 ((peer_config->key_size == 128 || peer_config->key_size == 256) && 
!peer_config->secret[0])) {
 av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is 
enabled\n");
@@ -223,8 +244,24 @@ static int librist_read(URLContext *h, uint8_t *buf, int 
size)
 return AVERROR_EXTERNAL;
 }
 
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (data_block->flags & RIST_DATA_FLAGS_OVERFLOW == 
RIST_DATA_FLAGS_OVERFLOW) {
+if (!s->overrun_nonfatal) {
+av_log(h, AV_LOG_ERROR, "Fifo buffer overrun. "
+"To avoid, increase fifo_size URL option. "
+"To survive in such case, use overrun_nonfatal option\n");
+size = AVERROR(EIO);
+goto out_free;
+} else {
+av_log(h, AV_LOG_WARNING, "Fifo buffer overrun. "
+"Surviving due to overrun_nonfatal option\n");
+}
+}
+#endif
+
 size = data_block->payload_len;
 memcpy(buf, data_block->payload, size);
+out_free:
 #if FF_LIBRIST_VERSION < FF_LIBRIST_VERSION_41
 rist_receiver_data_block_free((struct rist_data_block**)&data_block);
 #else
-- 
2.32.0

__

Re: [FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread Gijs Peskens

V2, I don't know why send-patch didn't pick up the subject override :/

On 11-01-2022 11:34, Gijs Peskens wrote:

Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
behavior.

Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
and error out in that case.
---
  doc/protocols.texi|  9 +
  libavformat/librist.c | 37 +
  2 files changed, 46 insertions(+)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index d207df0b52..f1acf0cc77 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
retransmission of data.
  Default value is 0 which means the librist default (1 sec). Maximum value is 
30
  seconds.
  
+@item fifo_size

+Size of the librist receiver output fifo in number of packets. This must be a
+power of 2.
+Defaults to 8192 (vs the libRIST default of 1024).
+
+@item overrun_nonfatal=@var{1|0}
+Survive in case of libRIST fifo buffer overrun. Default
+value is 0.
+
  @item pkt_size
  Set maximum packet size for sending data. 1316 by default.
  
diff --git a/libavformat/librist.c b/libavformat/librist.c

index 378b635ea7..53f83d3668 100644
--- a/libavformat/librist.c
+++ b/libavformat/librist.c
@@ -43,6 +43,9 @@
  ((patch) + ((minor)* 0x100) + ((major) *0x1))
  #define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
  #define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
+#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
+
+#define FF_LIBRIST_FIFO_SIZE_DEFAULT 8192
  
  typedef struct RISTContext {

  const AVClass *class;
@@ -52,6 +55,8 @@ typedef struct RISTContext {
  int packet_size;
  int log_level;
  int encryption;
+int fifo_size;
+int overrun_nonfatal;
  char *secret;
  
  struct rist_logging_settings logging_settings;

@@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
  { "main",NULL,  0,   AV_OPT_TYPE_CONST, 
{.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" },
  { "advanced",NULL,  0,   AV_OPT_TYPE_CONST, 
{.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" },
  { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
+{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", OFFSET(fifo_size), 
AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 9, 2 << 15, .flags = D|E },
+{ "overrun_nonfatal", "survive in case of libRIST receiving circular buffer 
overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1,D },
  { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, 
  {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
  { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT, 
  {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
  { "secret", "set encryption secret",OFFSET(secret),  
AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
@@ -161,6 +168,20 @@ static int librist_open(URLContext *h, const char *uri, 
int flags)
  if (ret < 0)
  goto err;
  
+//Prior to 4.2.0 there was a bug in libRIST which made this call always fail.

+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (flags & AVIO_FLAG_READ) {
+ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
+if (ret != 0) {
+goto err;
+}
+}
+#else
+if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
+av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which fails 
setting the fifo buffer size\n");
+}
+#endif
+
  if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
  ((peer_config->key_size == 128 || peer_config->key_size == 256) && 
!peer_config->secret[0])) {
  av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is 
enabled\n");
@@ -223,8 +244,24 @@ static int librist_read(URLContext *h, uint8_t *buf, int 
size)
  return AVERROR_EXTERNAL;
  }
  
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42

+if (data_block->flags & RIST_DATA_FLAGS_OVERFLOW == 
RIST_DATA_FLAGS_OVERFLOW) {
+if (!s->overrun_nonfatal) {
+av_log(h, AV_LOG_ERROR, "Fifo buffer overrun. "
+"To avoid, increase fifo_size URL option. "
+"To survive in such case, use overrun_nonfatal option\n");
+size = AVERROR(EIO);
+goto out_free;
+} else {
+av_log(h, AV_LOG_WARNING, "Fifo buffer overrun. "
+"Surviving due to overrun_nonfatal option\n");
+}
+}
+#endif
+
  size = data_block->payload_len;
  memcpy(buf, data_block->payload, size);
+out_free:
  #if FF_LIBRIST_VERSI

Re: [FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread zhilizhao(赵志立)



> On Jan 11, 2022, at 6:34 PM, Gijs Peskens  wrote:
> 
> Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
> behavior.
> 
> Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
> and error out in that case.
> ---
> doc/protocols.texi|  9 +
> libavformat/librist.c | 37 +
> 2 files changed, 46 insertions(+)
> 
> diff --git a/doc/protocols.texi b/doc/protocols.texi
> index d207df0b52..f1acf0cc77 100644
> --- a/doc/protocols.texi
> +++ b/doc/protocols.texi
> @@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
> retransmission of data.
> Default value is 0 which means the librist default (1 sec). Maximum value is 
> 30
> seconds.
> 
> +@item fifo_size
> +Size of the librist receiver output fifo in number of packets. This must be 
> a 
> +power of 2.
> +Defaults to 8192 (vs the libRIST default of 1024).
> +
> +@item overrun_nonfatal=@var{1|0}
> +Survive in case of libRIST fifo buffer overrun. Default
> +value is 0.
> +
> @item pkt_size
> Set maximum packet size for sending data. 1316 by default.
> 
> diff --git a/libavformat/librist.c b/libavformat/librist.c
> index 378b635ea7..53f83d3668 100644
> --- a/libavformat/librist.c
> +++ b/libavformat/librist.c
> @@ -43,6 +43,9 @@
> ((patch) + ((minor)* 0x100) + ((major) *0x1))
> #define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
> LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
> #define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
> +#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
> +
> +#define FF_LIBRIST_FIFO_SIZE_DEFAULT 8192
> 
> typedef struct RISTContext {
> const AVClass *class;
> @@ -52,6 +55,8 @@ typedef struct RISTContext {
> int packet_size;
> int log_level;
> int encryption;
> +int fifo_size;
> +int overrun_nonfatal;
> char *secret;
> 
> struct rist_logging_settings logging_settings;
> @@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
> { "main",NULL,  0,   
> AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, 
> "profile" },
> { "advanced",NULL,  0,   
> AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, 
> "profile" },
> { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
> AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
> +{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", 
> OFFSET(fifo_size), AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 
> 9, 2 << 15, .flags = D|E },
> +{ "overrun_nonfatal", "survive in case of libRIST receiving circular 
> buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_INT, {.i64 = 0}, 0, 1, 
>D },

This is not what I mean. We use int type to hold the result of
AV_OPT_TYPE_BOOL configuration.

If overrun_nonfatal has bool type, it can trigger such error:

src/libavutil/opt.c:129:9: runtime error: store to misaligned
address 0x7fbf454121a3 for type 'int', which requires 4 byte alignment

For AVOption, the type is AV_OPT_TYPE_BOOL.

> { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT, 
>   {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
> { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT, 
>   {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
> { "secret", "set encryption secret",OFFSET(secret),  
> AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
> @@ -161,6 +168,20 @@ static int librist_open(URLContext *h, const char *uri, 
> int flags)
> if (ret < 0)
> goto err;
> 
> +//Prior to 4.2.0 there was a bug in libRIST which made this call always 
> fail.
> +#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
> +if (flags & AVIO_FLAG_READ) {
> +ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
> +if (ret != 0) {
> +goto err;
> +}
> +}
> +#else
> +if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
> +av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which 
> fails setting the fifo buffer size\n");
> +}
> +#endif
> +
> if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
> ((peer_config->key_size == 128 || peer_config->key_size == 256) && 
> !peer_config->secret[0])) {
> av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is 
> enabled\n");
> @@ -223,8 +244,24 @@ static int librist_read(URLContext *h, uint8_t *buf, int 
> size)
> return AVERROR_EXTERNAL;
> }
> 
> +#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
> +if (data_block->flags & RIST_DATA_FLAGS_OVERFLOW == 
> RIST_DATA_FLAGS_OVERFLOW) {
> +if (!s->overrun_nonfatal) {
> +av_log(h, AV_LOG_ERROR, "Fifo buffer overrun. "
> +"To avoid, increase fifo_size U

Re: [FFmpeg-devel] [PATCH v2] avcodec/av1dec: honor the requested skip_frame level

2022-01-11 Thread James Almer




On 1/9/2022 4:18 PM, James Almer wrote:

This supports dropping non-intra, non-key, or all frames.

Signed-off-by: James Almer 
---
Now dropping the frames without decoding them.

Untested with actual hardware.


Tested by nevcairiel and confirmed to work, so will apply.



  libavcodec/av1dec.c | 68 ++---
  1 file changed, 39 insertions(+), 29 deletions(-)

diff --git a/libavcodec/av1dec.c b/libavcodec/av1dec.c
index 09df2bf421..62d459cded 100644
--- a/libavcodec/av1dec.c
+++ b/libavcodec/av1dec.c
@@ -585,16 +585,19 @@ static int av1_frame_ref(AVCodecContext *avctx, AV1Frame 
*dst, const AV1Frame *s
  {
  int ret;
  
-ret = ff_thread_ref_frame(&dst->tf, &src->tf);

+ret = av_buffer_replace(&dst->header_ref, src->header_ref);
  if (ret < 0)
  return ret;
  
-dst->header_ref = av_buffer_ref(src->header_ref);

-if (!dst->header_ref)
-goto fail;
-
  dst->raw_frame_header = src->raw_frame_header;
  
+if (!src->tf.f->buf[0])

+return 0;
+
+ret = ff_thread_ref_frame(&dst->tf, &src->tf);
+if (ret < 0)
+goto fail;
+
  if (src->hwaccel_picture_private) {
  dst->hwaccel_priv_buf = av_buffer_ref(src->hwaccel_priv_buf);
  if (!dst->hwaccel_priv_buf)
@@ -800,12 +803,6 @@ static int av1_frame_alloc(AVCodecContext *avctx, AV1Frame 
*f)
  return ret;
  }
  
-f->header_ref = av_buffer_ref(s->header_ref);

-if (!f->header_ref)
-return AVERROR(ENOMEM);
-
-f->raw_frame_header = s->raw_frame_header;
-
  if ((ret = ff_thread_get_buffer(avctx, &f->tf, AV_GET_BUFFER_FLAG_REF)) < 
0)
  goto fail;
  
@@ -945,8 +942,7 @@ static int update_reference_list(AVCodecContext *avctx)
  
  for (int i = 0; i < AV1_NUM_REF_FRAMES; i++) {

  if (header->refresh_frame_flags & (1 << i)) {
-if (s->ref[i].tf.f->buf[0])
-av1_frame_unref(avctx, &s->ref[i]);
+av1_frame_unref(avctx, &s->ref[i]);
  if ((ret = av1_frame_ref(avctx, &s->ref[i], &s->cur_frame)) < 0) {
  av_log(avctx, AV_LOG_ERROR,
 "Failed to update frame %d in reference list\n", i);
@@ -962,19 +958,32 @@ static int get_current_frame(AVCodecContext *avctx)
  AV1DecContext *s = avctx->priv_data;
  int ret;
  
-if (s->cur_frame.tf.f->buf[0])

-av1_frame_unref(avctx, &s->cur_frame);
+av1_frame_unref(avctx, &s->cur_frame);
  
-ret = av1_frame_alloc(avctx, &s->cur_frame);

+s->cur_frame.header_ref = av_buffer_ref(s->header_ref);
+if (!s->cur_frame.header_ref)
+return AVERROR(ENOMEM);
+
+s->cur_frame.raw_frame_header = s->raw_frame_header;
+
+ret = init_tile_data(s);
  if (ret < 0) {
-av_log(avctx, AV_LOG_ERROR,
-   "Failed to allocate space for current frame.\n");
+av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
  return ret;
  }
  
-ret = init_tile_data(s);

+if ((avctx->skip_frame >= AVDISCARD_NONINTRA &&
+(s->raw_frame_header->frame_type != AV1_FRAME_KEY &&
+ s->raw_frame_header->frame_type != AV1_FRAME_INTRA_ONLY)) ||
+(avctx->skip_frame >= AVDISCARD_NONKEY   &&
+ s->raw_frame_header->frame_type != AV1_FRAME_KEY) ||
+avctx->skip_frame >= AVDISCARD_ALL)
+return 0;
+
+ret = av1_frame_alloc(avctx, &s->cur_frame);
  if (ret < 0) {
-av_log(avctx, AV_LOG_ERROR, "Failed to init tile data.\n");
+av_log(avctx, AV_LOG_ERROR,
+   "Failed to allocate space for current frame.\n");
  return ret;
  }
  
@@ -1077,8 +1086,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,

  s->raw_frame_header = &obu->obu.frame_header;
  
  if (s->raw_frame_header->show_existing_frame) {

-if (s->cur_frame.tf.f->buf[0])
-av1_frame_unref(avctx, &s->cur_frame);
+av1_frame_unref(avctx, &s->cur_frame);
  
  ret = av1_frame_ref(avctx, &s->cur_frame,

  
&s->ref[s->raw_frame_header->frame_to_show_map_idx]);
@@ -1093,9 +1101,11 @@ static int av1_decode_frame(AVCodecContext *avctx, void 
*frame,
  goto end;
  }
  
-ret = set_output_frame(avctx, frame, pkt, got_frame);

-if (ret < 0)
-av_log(avctx, AV_LOG_ERROR, "Set output frame error.\n");
+if (s->cur_frame.tf.f->buf[0]) {
+ret = set_output_frame(avctx, frame, pkt, got_frame);
+if (ret < 0)
+av_log(avctx, AV_LOG_ERROR, "Set output frame 
error.\n");
+}
  
  s->raw_frame_header = NULL;
  
@@ -,7 +1121,7 @@ static int av1_decode_frame(AVCodecContext *avctx, void *frame,

  s->cur_frame.spatial_id  = header->spatial_id;
  

[FFmpeg-devel] [PATCH] avformat/asfdec_f: init avpacket by av_packet_alloc()

2022-01-11 Thread yshaw1999
From: Yang Xiao 

Pointer member side_data of AVPacket that allocated by stack may be wild 
pointer.
Prevent releasing wild pointers in AVPacket when some functions try to call 
av_packet_unref()
---
 libavformat/asfdec_f.c | 10 --
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
index a8f36ed286..605d9f53a1 100644
--- a/libavformat/asfdec_f.c
+++ b/libavformat/asfdec_f.c
@@ -1433,7 +1433,9 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
stream_index,
 {
 FFFormatContext *const si = ffformatcontext(s);
 ASFContext *asf = s->priv_data;
-AVPacket pkt1, *pkt = &pkt1;
+AVPacket *pkt = av_packet_alloc();
+if(!pkt)
+return AVERROR(ENOMEM);
 ASFStream *asf_st;
 int64_t pts;
 int64_t pos = *ppos;
@@ -1448,13 +1450,16 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
stream_index,
   s->packet_size * s->packet_size +
   si->data_offset;
 *ppos = pos;
-if (avio_seek(s->pb, pos, SEEK_SET) < 0)
+if (avio_seek(s->pb, pos, SEEK_SET) < 0) {
+av_packet_free(&pkt);
 return AV_NOPTS_VALUE;
+}
 
 ff_read_frame_flush(s);
 asf_reset_header(s);
 for (;;) {
 if (av_read_frame(s, pkt) < 0) {
+av_packet_free(&pkt);
 av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
 return AV_NOPTS_VALUE;
 }
@@ -1483,6 +1488,7 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
stream_index,
 }
 
 *ppos = pos;
+av_packet_free(&pkt);
 return pts;
 }
 
-- 
2.30.1 (Apple Git-130)

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avformat/asfdec_f: init avpacket by av_packet_alloc()

2022-01-11 Thread Andreas Rheinhardt
yshaw1...@163.com:
> From: Yang Xiao 
> 
> Pointer member side_data of AVPacket that allocated by stack may be wild 
> pointer.
> Prevent releasing wild pointers in AVPacket when some functions try to call 
> av_packet_unref()
> ---
>  libavformat/asfdec_f.c | 10 --
>  1 file changed, 8 insertions(+), 2 deletions(-)
> 
> diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
> index a8f36ed286..605d9f53a1 100644
> --- a/libavformat/asfdec_f.c
> +++ b/libavformat/asfdec_f.c
> @@ -1433,7 +1433,9 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
> stream_index,
>  {
>  FFFormatContext *const si = ffformatcontext(s);
>  ASFContext *asf = s->priv_data;
> -AVPacket pkt1, *pkt = &pkt1;
> +AVPacket *pkt = av_packet_alloc();
> +if(!pkt)
> +return AVERROR(ENOMEM);
>  ASFStream *asf_st;
>  int64_t pts;
>  int64_t pos = *ppos;
> @@ -1448,13 +1450,16 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
> stream_index,
>s->packet_size * s->packet_size +
>si->data_offset;
>  *ppos = pos;
> -if (avio_seek(s->pb, pos, SEEK_SET) < 0)
> +if (avio_seek(s->pb, pos, SEEK_SET) < 0) {
> +av_packet_free(&pkt);
>  return AV_NOPTS_VALUE;
> +}
>  
>  ff_read_frame_flush(s);
>  asf_reset_header(s);
>  for (;;) {
>  if (av_read_frame(s, pkt) < 0) {
> +av_packet_free(&pkt);
>  av_log(s, AV_LOG_INFO, "asf_read_pts failed\n");
>  return AV_NOPTS_VALUE;
>  }
> @@ -1483,6 +1488,7 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
> stream_index,
>  }
>  
>  *ppos = pos;
> +av_packet_free(&pkt);
>  return pts;
>  }
>  
> 

To repeat myself: Do you have FF_API_INIT_PACKET set to 0 (it should
still be set to 1)? Because av_read_frame() is supposed to (and
documented to) treat the packet it is given as uninitialized.

- Andreas
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH] libRIST: allow setting fifo size and fail on overflow.

2022-01-11 Thread Gijs Peskens
Introduce fifo_size and overrun_nonfatal params to configure fifo buffer
behavior.

Use newly introduced RIST_DATA_FLAGS_OVERFLOW flag to check for overrun
and error out in that case.
---
 doc/protocols.texi|  9 +
 libavformat/librist.c | 37 +
 2 files changed, 46 insertions(+)

diff --git a/doc/protocols.texi b/doc/protocols.texi
index d207df0b52..f1acf0cc77 100644
--- a/doc/protocols.texi
+++ b/doc/protocols.texi
@@ -745,6 +745,15 @@ Set internal RIST buffer size in milliseconds for 
retransmission of data.
 Default value is 0 which means the librist default (1 sec). Maximum value is 30
 seconds.
 
+@item fifo_size
+Size of the librist receiver output fifo in number of packets. This must be a 
+power of 2.
+Defaults to 8192 (vs the libRIST default of 1024).
+
+@item overrun_nonfatal=@var{1|0}
+Survive in case of libRIST fifo buffer overrun. Default
+value is 0.
+
 @item pkt_size
 Set maximum packet size for sending data. 1316 by default.
 
diff --git a/libavformat/librist.c b/libavformat/librist.c
index 378b635ea7..87c14eb265 100644
--- a/libavformat/librist.c
+++ b/libavformat/librist.c
@@ -43,6 +43,9 @@
 ((patch) + ((minor)* 0x100) + ((major) *0x1))
 #define FF_LIBRIST_VERSION FF_LIBRIST_MAKE_VERSION(LIBRIST_API_VERSION_MAJOR, 
LIBRIST_API_VERSION_MINOR, LIBRIST_API_VERSION_PATCH)
 #define FF_LIBRIST_VERSION_41 FF_LIBRIST_MAKE_VERSION(4, 1, 0)
+#define FF_LIBRIST_VERSION_42 FF_LIBRIST_MAKE_VERSION(4, 2, 0)
+
+#define FF_LIBRIST_FIFO_SIZE_DEFAULT 8192
 
 typedef struct RISTContext {
 const AVClass *class;
@@ -52,6 +55,8 @@ typedef struct RISTContext {
 int packet_size;
 int log_level;
 int encryption;
+int fifo_size;
+int overrun_nonfatal;
 char *secret;
 
 struct rist_logging_settings logging_settings;
@@ -70,6 +75,8 @@ static const AVOption librist_options[] = {
 { "main",NULL,  0,   
AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_MAIN}, 0, 0, .flags = D|E, "profile" 
},
 { "advanced",NULL,  0,   
AV_OPT_TYPE_CONST, {.i64=RIST_PROFILE_ADVANCED}, 0, 0, .flags = D|E, "profile" 
},
 { "buffer_size", "set buffer_size in ms", OFFSET(buffer_size), 
AV_OPT_TYPE_INT, {.i64=0}, 0, 3, .flags = D|E },
+{ "fifo_size", "Set libRIST fifo buffer. Size must be power of 2", 
OFFSET(fifo_size), AV_OPT_TYPE_INT, {.i64=FF_LIBRIST_FIFO_SIZE_DEFAULT}, 2 << 
9, 2 << 15, .flags = D|E },
+{ "overrun_nonfatal", "survive in case of libRIST receiving circular 
buffer overrun", OFFSET(overrun_nonfatal), AV_OPT_TYPE_BOOL, {.i64 = 0}, 0, 1,  
  D },
 { "pkt_size","set packet size", OFFSET(packet_size), AV_OPT_TYPE_INT,  
 {.i64=1316},  1, MAX_PAYLOAD_SIZE,.flags = D|E },
 { "log_level",   "set loglevel",OFFSET(log_level),   AV_OPT_TYPE_INT,  
 {.i64=RIST_LOG_INFO},-1, INT_MAX, .flags = D|E },
 { "secret", "set encryption secret",OFFSET(secret),  
AV_OPT_TYPE_STRING,{.str=NULL},  0, 0,   .flags = D|E },
@@ -161,6 +168,20 @@ static int librist_open(URLContext *h, const char *uri, 
int flags)
 if (ret < 0)
 goto err;
 
+//Prior to 4.2.0 there was a bug in libRIST which made this call always 
fail.
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (flags & AVIO_FLAG_READ) {
+ret = rist_receiver_set_output_fifo_size(s->ctx, s->fifo_size);
+if (ret != 0) {
+goto err;
+}
+}
+#else
+if (s->fifo_size != FF_LIBRIST_FIFO_SIZE_DEFAULT) {
+av_log(h, AV_LOG_ERROR, "libRIST prior to 0.2.7 has a bug which fails 
setting the fifo buffer size\n");
+}
+#endif
+
 if (((s->encryption == 128 || s->encryption == 256) && !s->secret) ||
 ((peer_config->key_size == 128 || peer_config->key_size == 256) && 
!peer_config->secret[0])) {
 av_log(h, AV_LOG_ERROR, "secret is mandatory if encryption is 
enabled\n");
@@ -223,8 +244,24 @@ static int librist_read(URLContext *h, uint8_t *buf, int 
size)
 return AVERROR_EXTERNAL;
 }
 
+#if FF_LIBRIST_VERSION >= FF_LIBRIST_VERSION_42
+if (data_block->flags & RIST_DATA_FLAGS_OVERFLOW == 
RIST_DATA_FLAGS_OVERFLOW) {
+if (!s->overrun_nonfatal) {
+av_log(h, AV_LOG_ERROR, "Fifo buffer overrun. "
+"To avoid, increase fifo_size URL option. "
+"To survive in such case, use overrun_nonfatal option\n");
+size = AVERROR(EIO);
+goto out_free;
+} else {
+av_log(h, AV_LOG_WARNING, "Fifo buffer overrun. "
+"Surviving due to overrun_nonfatal option\n");
+}
+}
+#endif
+
 size = data_block->payload_len;
 memcpy(buf, data_block->payload, size);
+out_free:
 #if FF_LIBRIST_VERSION < FF_LIBRIST_VERSION_41
 rist_receiver_data_block_free((struct rist_data_block**)&data_block);
 #else
-- 
2.32.0

_

Re: [FFmpeg-devel] [PATCH] IEC61937_EAC3 decoding support

2022-01-11 Thread Denis Shulyaka
Hi all,

Is there anything I could do to kindly ask to review and accept the patch?

Best regards,
Denis Shulyaka

вт, 28 сент. 2021 г. в 17:53, Denis Shulyaka :

> Hi all,
>
> This is a kind reminder.
>
> Best regards,
> Denis Shulyaka
>
> вс, 11 апр. 2021 г. в 13:21, Denis Shulyaka :
>
>> Hi Carl,
>>
>> Thanks for the review.
>>
>> сб, 10 апр. 2021 г. в 18:05, Carl Eugen Hoyos :
>>
>>> Please use "&" instead of "%".
>>>
>>
>>  Fixed
>>
>> Please remove this line.
>>>
>>
>> Fixed
>>
>> See above.
>>>
>>
>> Fixed
>>
>> Please remove this line or the default branch above.
>>>
>>
>> Fixed. It was there as a safety feature in case someone adds a new
>> condition to the switch which wouldn't return.
>>
>> Is this an unrelated fix?
>>>
>>
>> Yes, you are right, will submit as a separate patch.
>>
>> > +if (!pkt_size)
>>> > +return AVERROR_BUG;
>>>
>>> This looks wrong or do I miss something?
>>>
>>
>> This is also just for safety to verify that future modifications of
>> spdif_read_burst_payload_length() work correctly. Should I remove it?
>>
>> --
>> Best regards,
>> Denis Shulyaka
>>
>
>
> --
> Best regards,
> Denis Shulyaka
>


-- 
Best regards,
Denis Shulyaka
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH 8/9] avcodec/mpeg4videodec: Avoid multiple consecutive av_log()

2022-01-11 Thread Michael Niedermayer
On Mon, Jan 10, 2022 at 11:55:35PM +0100, Andreas Rheinhardt wrote:
> These messages belong together, yet they can be torn apart
> if some other call to av_log() happens between them.
> 
> Signed-off-by: Andreas Rheinhardt 
> ---
>  libavcodec/mpeg4videodec.c | 59 +++---
>  1 file changed, 30 insertions(+), 29 deletions(-)

LGTM

thx

[...]
-- 
Michael GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB

Many things microsoft did are stupid, but not doing something just because
microsoft did it is even more stupid. If everything ms did were stupid they
would be bankrupt already.


signature.asc
Description: PGP 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-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] vf_tonemap: Fix order of planes

2022-01-11 Thread Vittorio Giovara
On Wed, Jan 5, 2022 at 4:15 PM Vittorio Giovara 
wrote:

> This resulted in a dimmed tonemapping due to bad resulting luma
> calculation.
>
> Found by: Derek Buitenhuis
>
> Signed-off-by: Vittorio Giovara 
> ---
>  libavfilter/vf_tonemap.c | 10 +-
>  1 file changed, 5 insertions(+), 5 deletions(-)
>
> diff --git a/libavfilter/vf_tonemap.c b/libavfilter/vf_tonemap.c
> index 363df8034b..644c9fd9e3 100644
> --- a/libavfilter/vf_tonemap.c
> +++ b/libavfilter/vf_tonemap.c
> @@ -120,17 +120,17 @@ static void tonemap(TonemapContext *s, AVFrame *out,
> const AVFrame *in,
>  const AVPixFmtDescriptor *desc, int x, int y, double
> peak)
>  {
>  const float *r_in = (const float *)(in->data[0] + x *
> desc->comp[0].step + y * in->linesize[0]);
> -const float *b_in = (const float *)(in->data[1] + x *
> desc->comp[1].step + y * in->linesize[1]);
> -const float *g_in = (const float *)(in->data[2] + x *
> desc->comp[2].step + y * in->linesize[2]);
> +const float *g_in = (const float *)(in->data[1] + x *
> desc->comp[1].step + y * in->linesize[1]);
> +const float *b_in = (const float *)(in->data[2] + x *
> desc->comp[2].step + y * in->linesize[2]);
>  float *r_out = (float *)(out->data[0] + x * desc->comp[0].step + y *
> out->linesize[0]);
> -float *b_out = (float *)(out->data[1] + x * desc->comp[1].step + y *
> out->linesize[1]);
> -float *g_out = (float *)(out->data[2] + x * desc->comp[2].step + y *
> out->linesize[2]);
> +float *g_out = (float *)(out->data[1] + x * desc->comp[1].step + y *
> out->linesize[1]);
> +float *b_out = (float *)(out->data[2] + x * desc->comp[2].step + y *
> out->linesize[2]);
>  float sig, sig_orig;
>
>  /* load values */
>  *r_out = *r_in;
> -*b_out = *b_in;
>  *g_out = *g_in;
> +*b_out = *b_in;
>
>  /* desaturate to prevent unnatural colors */
>  if (s->desat > 0) {
> --
> 2.34.1
>
>
Pushing soon unless objections. Possibly backporting it to 5.0 too.
-- 
Vittorio
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v3 1/2] swscale/x86/output.asm: add x86-optimized planer gbr yuv2anyX functions

2022-01-11 Thread Paul B Mahol
Can someone apply this?
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v3 1/2] swscale/x86/output.asm: add x86-optimized planer gbr yuv2anyX functions

2022-01-11 Thread James Almer




On 1/8/2022 8:13 PM, Mark Reid wrote:

On Sat, Jan 1, 2022 at 2:35 AM Paul B Mahol  wrote:


will apply soon



ping


Applied. Sorry for the delay.
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 04/35] lavu/fifo: add new functions for determinining reading/writing size

2022-01-11 Thread Anton Khirnov
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 - xx - 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 01/35] lavu/fifo: disallow overly large fifo sizes

2022-01-11 Thread 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 
+
 #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)
+
 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;
-- 
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 03/35] lavu/fifo: introduce the notion of element size

2022-01-11 Thread 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 - xx - 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 - xx - 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
+intis_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 -=

[FFmpeg-devel] [PATCH 05/35] lavu/fifo: add a new FIFO grow function

2022-01-11 Thread Anton Khirnov
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 - xx - 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 07/35] lavu/fifo: add new FIFO writing functions

2022-01-11 Thread 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 - xx - 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_tlen = 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 

[FFmpeg-devel] [PATCH 14/35] lavc/cuviddec: do not reallocate the fifo unnecessarily

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 24/35] lavf/mpegenc: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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, bu

[FFmpeg-devel] [PATCH 16/35] lavc/libvorbisenc: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 17/35] lavc/libvpxenc: switch to the new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 18/35] lavc/libvpxenc: remove unneeded context variable

2022-01-11 Thread Anton Khirnov
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 21/35] lavc/qsvenc: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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 {
+AVPacketpkt;
+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->

[FFmpeg-devel] [PATCH 19/35] lavc/nvenc: switch to the new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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 *p

[FFmpeg-devel] [PATCH 25/35] lavf/swfenc: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 27/35] lavf/async: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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, 

[FFmpeg-devel] [PATCH 15/35] lavc/cuviddec: convert to the new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 08/35] lavu/fifo: add new FIFO read/peek functions

2022-01-11 Thread 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 - xx - 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_tlen = 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 stat

[FFmpeg-devel] [PATCH 10/35] lavu/fifo: deprecate old API

2022-01-11 Thread Anton Khirnov
---
 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 - xx - 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,

[FFmpeg-devel] [PATCH 11/35] lavu/tests/fifo: switch to the new API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 06/35] lavu/fifo: add a new function for draining the FIFO

2022-01-11 Thread Anton Khirnov
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 - xx - 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 20/35] lavc/qsvdec: switch to the new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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, 9 };
 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_fi

[FFmpeg-devel] [PATCH 02/35] lavu/fifo: make the contents of AVFifoBuffer private on next major bump

2022-01-11 Thread 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 - xx - 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 
 #include 
-#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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 09/35] lavu/fifo: add a flag for automatically growing the FIFO as needed

2022-01-11 Thread 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 - xx - 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
 intis_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_tlen = 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 m

[FFmpeg-devel] [PATCH 13/35] lavc/amfenc: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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_s

[FFmpeg-devel] [PATCH 23/35] lavf/dvenc: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 12/35] lavc/avcodec: switch to new FIFO API

2022-01-11 Thread 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);
 }
-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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 26/35] lavf/udp: switch to new FIFO API

2022-01-11 Thread 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);
 
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 22/35] lavf/dvenc: return an error on audio/video desync

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 28/35] lavd/jack: switch to the new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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)) * 100.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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 29/35] lavu/audio_fifo: drop an unnecessary include

2022-01-11 Thread Anton Khirnov
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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 30/35] lavu/audio_fifo: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 34/35] ffplay: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 31/35] lavu/threadmessage: switch to new FIFO API

2022-01-11 Thread 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;
 }
 #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-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH 35/35] ffmpeg: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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(

[FFmpeg-devel] [PATCH 33/35] lavfi/vf_deshake_opencl: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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),
- 

[FFmpeg-devel] [PATCH 32/35] lavfi/qsvvpp: switch to new FIFO API

2022-01-11 Thread Anton Khirnov
---
 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, 9 };
 
+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-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [RFC] QSV: Introduce min Compile-SDK Version and check for Runtime-Versions instead

2022-01-11 Thread Xiang, Haihao
On Mon, 2022-01-03 at 21:53 +, Soft Works wrote:
> Hi,
> 
> this is a follow-up to my recently submitted patch:
> 
> “avfilter/vpp_qsv: fix regression on older api versions (e.g. 1.11)”
> 
> That patch only fixes the one important regression from multiple issues which 
> have been introduced by recent changes. Those changes have gone in a less
> than ideal direction, because they have introduced checks based on the version
> of the MSDK which ffmpeg is being compiled against, but they didn’t add 
> checks for the runtime libmfx/MSDK versions – and this causes failures, 
> sometimes even for functionality which has worked before (=>regressions).
> 
> Background
> 
> - the compile-sdk version determines which features can be used
>   but only when
>   - the runtime SDK version supports it
> and
>   - the hardware (GPU gen) supports it
> - ALL compile-sdk versions can interface with ALL runtime MSDK 
>   versions (basically), no matter whether runtime version is newer or
>   older
> - At least on Windows, some hw is stuck at a certain runtime version,
>   e.g.: there are new drivers for Broadwell, but the MSDK runtime
>   is always 1.11
> 
> 
> Conclusion
> 
> Adding checks for the runtime MSDK versions is required wherever a feature
> might not be supported by older MSDK runtimes - I think that's an obvious
> necessity.
> 
> 
> Question
> 
> Having both - run-time and compile-time checks all over the code is adding 
> a lot of complexity and makes it difficult to maintain and work with.
> 
> Hence, I'm wondering whether we couldn't/shouldn't introduce a minimum 
> MSDK compile-time version, for example 1.22, or even later?
> 
> This would allow simplification of the QSV code in many places where run-time
> version checks are actually needed instead.
> 
> Over time, there have been better and worse MSDK versions, and there 
> should still be enough room for choosing, but I don't think there's any
> reason why somebody would still want to compile against some really old 
> (e.g. < 1.22) MSDK version. 
> 
> Please share your thoughts on this subject..

I agree we may add a requirement for the minimal compiling version in configure,
version 1.28 was released 3 years ago, how about using this version as the
minimal compiling version ? 

BTW you may download the new Windows drivers for Broadwell from 
https://www.intel.com/content/www/us/en/download/18369/intel-graphics-driver-for-windows-15-40.html
 , the runtime version should be 1.20

Thanks
Haihao

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH V2] libavcodec/qsvdec.c: using queue count to unref frame

2022-01-11 Thread Xiang, Haihao
On Fri, 2021-03-12 at 06:21 +, Xiang, Haihao wrote:
> On Fri, 2021-03-12 at 10:44 +0800, wenbin.c...@intel.com wrote:
> > From: "Chen,Wenbin" 
> > 
> > MSDK vc1 and av1 sometimes output frame into the same suface, but
> > ffmpeg-qsv assume the surface will be used only once, so it will
> > unref the frame when it receives the output surface. Now change
> > it to unref frame according to queue count.
> > 
> > Signed-off-by Wenbin Chen 
> > ---
> >  libavcodec/qsvdec.c | 4 ++--
> >  1 file changed, 2 insertions(+), 2 deletions(-)
> > 
> > diff --git a/libavcodec/qsvdec.c b/libavcodec/qsvdec.c
> > index 5f2e641373..3abd7bd4a2 100644
> > --- a/libavcodec/qsvdec.c
> > +++ b/libavcodec/qsvdec.c
> > @@ -513,7 +513,7 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext
> > *q,
> >  return AVERROR_BUG;
> >  }
> >  
> > -out_frame->queued = 1;
> > +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);
> >  } else {
> > @@ -526,7 +526,7 @@ static int qsv_decode(AVCodecContext *avctx, QSVContext
> > *q,
> >  
> >  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;
> > +out_frame->queued -= 1;
> >  
> >  if (avctx->pix_fmt != AV_PIX_FMT_QSV) {
> >  do {
> 
> LGTM, thanks!

Applied, thx!

-Haihao

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH, v2] lavf/vf_deinterlace_vaapi: flush queued frame for field in DeinterlacingBob

2022-01-11 Thread Xiang, Haihao
On Mon, 2022-01-10 at 06:58 +, Xiang, Haihao wrote:
> On Sun, 2022-01-09 at 18:32 +, Mark Thompson wrote:
> > On 29/12/2021 03:45, Xiang, Haihao wrote:
> > > > -Original Message-
> > > > From: ffmpeg-devel  On Behalf Of Linjie
> > > > Fu
> > > > Sent: Wednesday, September 18, 2019 16:20
> > > > To: ffmpeg-devel@ffmpeg.org
> > > > Cc: Fu, Linjie 
> > > > Subject: [FFmpeg-devel] [PATCH, v2] lavf/vf_deinterlace_vaapi: flush
> > > > queued
> > > > frame for field in DeinterlacingBob
> > > > 
> > > > For DeinterlacingBob mode with rate=field, the frame number of output
> > > > should equal 2x input total since only intra deinterlace is used.
> > > > 
> > > > Currently for "backward_ref = 0, rate = field", extra_delay is
> > > > introduced. Due to the async without flush, frame number of output is
> > > > [expected_number - 2].
> > > > 
> > > > Specifically, if the input only has 1 frame, the output will be empty.
> > > > 
> > > > Add deint_vaapi_request_frame for deinterlace_vaapi, send NULL frame
> > > > to flush the queued frame.
> > > > 
> > > > For 1 frame input in Bob mode with rate=field,
> > > > before patch: 0 frame;
> > > > after  patch: 2 frames;
> > > > 
> > > > ffmpeg -hwaccel vaapi -hwaccel_device /dev/dri/renderD128
> > > >  -hwaccel_output_format vaapi -i input.h264 -an -vf
> > > >  deinterlace_vaapi=mode=bob:rate=field -f null -
> > > 
> > > Sorry for picking up the old thread.
> > > 
> > > Hi Mark,
> > > 
> > > Is your issue mentioned in 
> > > http://ffmpeg.org/pipermail/ffmpeg-devel/2019-September/250080.html
> > > fixed by this patch version ? I'd like to apply this patch if it may work
> > > for you.
> > 
> > Yep, it now gives the expected number of frames and I do not have any
> > segfaults.
> > 
> > I tested drivers with { forward = 0, backward = 0 } (iHD), { forward = 1,
> > backward = 0 } (i965) and { forward = 2, backward = 1 } (gallium).
> > 
> > Thanks,
> 
> Thanks for confirming it works, and I will apply this patch version if no
> objection.
> 

Applied, thx!

-Haihao

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH] avformat/asfdec: fix crash caused by free wlid pointers

2022-01-11 Thread XiaoYang




At 2022-01-11 17:29:35, "Andreas Rheinhardt"  
wrote:
>Yang Xiao:
>> From: Yang Xiao 
>> 
>> This commit fixed a crash when seeking wma frames, asf decoder will try to 
>> demux in function asf_read_pts().
>> Pointer member side_data of AVPacket that allocated by stack may be wild 
>> pointer.
>> Prevent releasing wild pointers in AVPacket when some functions try to call 
>> av_packet_unref, example av_read_frame().
>> ---
>>  libavformat/asfdec_f.c | 2 +-
>>  libavformat/mpc.c  | 2 +-
>>  2 files changed, 2 insertions(+), 2 deletions(-)
>> 
>> diff --git a/libavformat/asfdec_f.c b/libavformat/asfdec_f.c
>> index a8f36ed286..bae0ecfc7c 100644
>> --- a/libavformat/asfdec_f.c
>> +++ b/libavformat/asfdec_f.c
>> @@ -1433,7 +1433,7 @@ static int64_t asf_read_pts(AVFormatContext *s, int 
>> stream_index,
>>  {
>>  FFFormatContext *const si = ffformatcontext(s);
>>  ASFContext *asf = s->priv_data;
>> -AVPacket pkt1, *pkt = &pkt1;
>> +AVPacket pkt1 = {0}, *pkt = &pkt1;
>>  ASFStream *asf_st;
>>  int64_t pts;
>>  int64_t pos = *ppos;
>> diff --git a/libavformat/mpc.c b/libavformat/mpc.c
>> index b5b2bab33c..ad0d693152 100644
>> --- a/libavformat/mpc.c
>> +++ b/libavformat/mpc.c
>> @@ -189,7 +189,7 @@ static int mpc_read_seek(AVFormatContext *s, int 
>> stream_index, int64_t timestamp
>>  AVStream *st = s->streams[stream_index];
>>  FFStream *const sti = ffstream(st);
>>  MPCContext *c = s->priv_data;
>> -AVPacket pkt1, *pkt = &pkt1;
>> +AVPacket pkt1 = {0}, *pkt = &pkt1;
>>  int ret;
>>  int index = av_index_search_timestamp(st, FFMAX(timestamp - 
>> DELAY_FRAMES, 0), flags);
>>  uint32_t lastframe;
>> 
>
>Do you have FF_API_INIT_PACKET set to 0 (it should still be set to 1)?
>Because av_read_frame() is supposed to (and documented to) treat the
>packet it is given as uninitialized.

>
>- Andreas



Thanks for your comment!
But I have a question,  av_init_packet() has been markedattribute_deprecated, 
If av_init_packet() is deprecated completely in the future, how to ensure 
ff_read_packet() safely (support to input a uninitialized packet) ? 
AVPacket->side_data will not be safely initialized with only av_packet_unref().


And as a user of the FFmpeg API, I want to disable some of the functions marked 
attribute_deprecated early, because upgrading FFmpeg has a large impact. 
Do I have other ways to achieve this goal?


>___
>ffmpeg-devel mailing list
>ffmpeg-devel@ffmpeg.org
>https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
>To unsubscribe, visit link above, or email
>ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [RFC] QSV: Introduce min Compile-SDK Version and check for Runtime-Versions instead

2022-01-11 Thread Soft Works


> -Original Message-
> From: ffmpeg-devel  On Behalf Of Xiang,
> Haihao
> Sent: Wednesday, January 12, 2022 3:53 AM
> To: ffmpeg-devel@ffmpeg.org
> Subject: Re: [FFmpeg-devel] [RFC] QSV: Introduce min Compile-SDK Version and
> check for Runtime-Versions instead
> 
> On Mon, 2022-01-03 at 21:53 +, Soft Works wrote:
> > Hi,
> >
> > this is a follow-up to my recently submitted patch:
> >
> > “avfilter/vpp_qsv: fix regression on older api versions (e.g. 1.11)”
> >
> > That patch only fixes the one important regression from multiple issues
> which
> > have been introduced by recent changes. Those changes have gone in a less
> > than ideal direction, because they have introduced checks based on the
> version
> > of the MSDK which ffmpeg is being compiled against, but they didn’t add
> > checks for the runtime libmfx/MSDK versions – and this causes failures,
> > sometimes even for functionality which has worked before (=>regressions).
> >
> > Background
> >
> > - the compile-sdk version determines which features can be used
> >   but only when
> >   - the runtime SDK version supports it
> > and
> >   - the hardware (GPU gen) supports it
> > - ALL compile-sdk versions can interface with ALL runtime MSDK
> >   versions (basically), no matter whether runtime version is newer or
> >   older
> > - At least on Windows, some hw is stuck at a certain runtime version,
> >   e.g.: there are new drivers for Broadwell, but the MSDK runtime
> >   is always 1.11
> >
> >
> > Conclusion
> >
> > Adding checks for the runtime MSDK versions is required wherever a feature
> > might not be supported by older MSDK runtimes - I think that's an obvious
> > necessity.
> >
> >
> > Question
> >
> > Having both - run-time and compile-time checks all over the code is adding
> > a lot of complexity and makes it difficult to maintain and work with.
> >
> > Hence, I'm wondering whether we couldn't/shouldn't introduce a minimum
> > MSDK compile-time version, for example 1.22, or even later?
> >
> > This would allow simplification of the QSV code in many places where run-
> time
> > version checks are actually needed instead.
> >
> > Over time, there have been better and worse MSDK versions, and there
> > should still be enough room for choosing, but I don't think there's any
> > reason why somebody would still want to compile against some really old
> > (e.g. < 1.22) MSDK version.
> >
> > Please share your thoughts on this subject..
> 
> I agree we may add a requirement for the minimal compiling version in
> configure,
> version 1.28 was released 3 years ago, how about using this version as the
> minimal compiling version ?

I'm fine with 1.28!


> BTW you may download the new Windows drivers for Broadwell from
> https://www.intel.com/content/www/us/en/download/18369/intel-graphics-driver-
> for-windows-15-40.html
>  , the runtime version should be 1.20

My mistake, I mixed the code names up. It was about a 3rd gen CPU. Those 
are stuck at 1.11. 4th and 5th gen are stuck at 1.20 and the others get the 
latest MSDK runtime.

It's like I had described in 2020 here: 
https://github.com/softworkz/ffmpeg_dx11/issues/1

Thanks,
softworkz


___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v1] avfilter/tonemap_vaapi: set va parameters filters and numbers

2022-01-11 Thread Xiang, Haihao


> -Original Message-
> From: ffmpeg-devel  On Behalf Of Fei
> Wang
> Sent: Tuesday, June 30, 2020 09:19
> To: ffmpeg-devel@ffmpeg.org
> Cc: Wang, Fei W 
> Subject: [FFmpeg-devel] [PATCH v1] avfilter/tonemap_vaapi: set va parameters
> filters and numbers
> 
> This can fill VAProcPipelineParameterBuffer correctly and make the
> pipeline works.
> 
> Signed-off-by: Fei Wang 
> ---
>  libavfilter/vf_tonemap_vaapi.c | 5 +
>  1 file changed, 5 insertions(+)
> 
> diff --git a/libavfilter/vf_tonemap_vaapi.c b/libavfilter/vf_tonemap_vaapi.c
> index 2f41b90424..9f52590a49 100644
> --- a/libavfilter/vf_tonemap_vaapi.c
> +++ b/libavfilter/vf_tonemap_vaapi.c
> @@ -296,6 +296,11 @@ static int tonemap_vaapi_filter_frame(AVFilterLink
> *inlink, AVFrame *input_frame
>  if (err < 0)
>  goto fail;
> 
> +if (vpp_ctx->nb_filter_buffers) {
> +params.filters = &vpp_ctx->filter_buffers[0];
> +params.num_filters = vpp_ctx->nb_filter_buffers;
> +}
> +
>  err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_frame);
>  if (err < 0)
>  goto fail;

This fixed the broken tonemap_vaapi filter, I'll apply it if no objection.

Thanks
Haihao
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v1] avfilter/tonemap_vaapi: set va parameters filters and numbers

2022-01-11 Thread Soft Works



> -Original Message-
> From: ffmpeg-devel  On Behalf Of Xiang,
> Haihao
> Sent: Wednesday, January 12, 2022 5:16 AM
> To: FFmpeg development discussions and patches 
> Cc: Wang, Fei W 
> Subject: Re: [FFmpeg-devel] [PATCH v1] avfilter/tonemap_vaapi: set va
> parameters filters and numbers
> 
> 
> > -Original Message-
> > From: ffmpeg-devel  On Behalf Of Fei
> > Wang
> > Sent: Tuesday, June 30, 2020 09:19
> > To: ffmpeg-devel@ffmpeg.org
> > Cc: Wang, Fei W 
> > Subject: [FFmpeg-devel] [PATCH v1] avfilter/tonemap_vaapi: set va
> parameters
> > filters and numbers
> >
> > This can fill VAProcPipelineParameterBuffer correctly and make the
> > pipeline works.
> >
> > Signed-off-by: Fei Wang 
> > ---
> >  libavfilter/vf_tonemap_vaapi.c | 5 +
> >  1 file changed, 5 insertions(+)
> >
> > diff --git a/libavfilter/vf_tonemap_vaapi.c
> b/libavfilter/vf_tonemap_vaapi.c
> > index 2f41b90424..9f52590a49 100644
> > --- a/libavfilter/vf_tonemap_vaapi.c
> > +++ b/libavfilter/vf_tonemap_vaapi.c
> > @@ -296,6 +296,11 @@ static int tonemap_vaapi_filter_frame(AVFilterLink
> > *inlink, AVFrame *input_frame
> >  if (err < 0)
> >  goto fail;
> >
> > +if (vpp_ctx->nb_filter_buffers) {
> > +params.filters = &vpp_ctx->filter_buffers[0];
> > +params.num_filters = vpp_ctx->nb_filter_buffers;
> > +}
> > +
> >  err = ff_vaapi_vpp_render_picture(avctx, ¶ms, output_frame);
> >  if (err < 0)
> >  goto fail;
> 
> This fixed the broken tonemap_vaapi filter, I'll apply it if no objection.

LGTM. I have a similar patch. 

sw


___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v5 5/7] avcodec/v4l2_context: resume the decoding process after source change event received.

2022-01-11 Thread Ming Qian
On Tue, 04. Jan 17:41, Andriy Gelman wrote:
> On Tue, 04. Jan 17:08, Ming Qian wrote:
> > client need to resume the decoding process
> > after it dequeues the source change event.
> > no matter what's the return value of v4l2_resolution_changed().
> > if the client doesn't resume the decoding process,
> > the decoder may keep waiting
> > 
> > in documentation of v4l2 stateful decoder, we can see the following
> > description:
> > The client must continue the sequence as described below to
> > continue the decoding process.
> > 1.  Dequeue the source change event.
> > Important
> > A source change triggers an implicit decoder drain,
> > similar to the explicit Drain sequence. The decoder is
> > stopped after it completes. The decoding process must be
> > resumed with either a pair of calls to
> > VIDIOC_STREAMOFF() and VIDIOC_STREAMON() on the CAPTURE
> > queue, or a call to VIDIOC_DECODER_CMD() with the
> > V4L2_DEC_CMD_START command.
> > 2.  Continue with the Capture Setup sequence.
> > 
> 
> This is just a clean up to remove the reinit variable so the commit title and
> description are wrong.
> 

Updated the commit message and pushed.

Thanks,
-- 
Andriy
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH v2] lavc/qsvenc_hevc: add -pic_timing_sei option

2022-01-11 Thread Haihao Xiang
The SDK may insert picture timing SEI for hevc and the code to set mfx
parameter has been added in qsvenc, however the corresponding option is
missing in the hevc option array

Reviewed-by: Limin Wang 
Signed-off-by: Haihao Xiang 
---
v2: added option description in the doc

 doc/encoders.texi| 3 +++
 libavcodec/qsvenc_hevc.c | 1 +
 2 files changed, 4 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 7cc8be1209..1679f389d7 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3432,6 +3432,9 @@ Number of rows for tiled encoding.
 
 @item @var{aud}
 Insert the Access Unit Delimiter NAL.
+
+@item @var{pic_timing_sei}
+Insert picture timing SEI with pic_struct_syntax element.
 @end table
 
 @subsection MPEG2 Options
diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c
index 08aba3011d..342b6bdea7 100644
--- a/libavcodec/qsvenc_hevc.c
+++ b/libavcodec/qsvenc_hevc.c
@@ -251,6 +251,7 @@ static const AVOption options[] = {
 { "tile_rows",  "Number of rows for tiled encoding",  
OFFSET(qsv.tile_rows),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, VE },
 { "recovery_point_sei", "Insert recovery point SEI messages",   
OFFSET(qsv.recovery_point_sei),  AV_OPT_TYPE_INT, { .i64 = -1 },
   -1,  1, VE },
 { "aud", "Insert the Access Unit Delimiter NAL", OFFSET(qsv.aud), 
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
+{ "pic_timing_sei","Insert picture timing SEI with pic_struct_syntax 
element", OFFSET(qsv.pic_timing_sei), AV_OPT_TYPE_BOOL, { .i64 = 1 }, 0, 1, VE 
},
 
 { NULL },
 };
-- 
2.17.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH] Fix setsockopt IP_MULTICAST_TTL on OpenBSD

2022-01-11 Thread Brad Smith
Fix setsockopt() usage on OpenBSD with IP_MULTICAST_TTL. The field
type should be an unsigned char on anything but Linux.


diff --git a/libavformat/udp.c b/libavformat/udp.c
index 180d96a988..29aa865fff 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -163,7 +163,13 @@ static int udp_set_multicast_ttl(int sockfd, int mcastTTL,
 {
 #ifdef IP_MULTICAST_TTL
 if (addr->sa_family == AF_INET) {
-if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &mcastTTL, 
sizeof(mcastTTL)) < 0) {
+#ifdef __linux__
+int ttl = mcastTTL;
+#else
+unsigned char ttl = mcastTTL;
+#endif
+
+if (setsockopt(sockfd, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, 
sizeof(ttl)) < 0) {
 ff_log_net_error(NULL, AV_LOG_ERROR, 
"setsockopt(IP_MULTICAST_TTL)");
 return ff_neterrno();
 }
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] [PATCH v4 1/1] avutils/hwcontext: When deriving a hwdevice, search for existing device in both directions

2022-01-11 Thread Soft Works



> -Original Message-
> From: ffmpeg-devel  On Behalf Of Mark
> Thompson
> Sent: Monday, January 10, 2022 9:57 PM
> To: ffmpeg-devel@ffmpeg.org
> Subject: Re: [FFmpeg-devel] [PATCH v4 1/1] avutils/hwcontext: When deriving a
> hwdevice, search for existing device in both directions
> 
> On 10/01/2022 01:40, Soft Works wrote:
> >> -Original Message-
> >> From: ffmpeg-devel  On Behalf Of Mark
> >> Thompson
> >> Sent: Monday, January 10, 2022 1:57 AM
> >> To: ffmpeg-devel@ffmpeg.org
> >> Subject: Re: [FFmpeg-devel] [PATCH v4 1/1] avutils/hwcontext: When
> deriving a
> >> hwdevice, search for existing device in both directions
> >>
> >> [trimmed somewhat]

> > To wrap things up a bit:
> >
> > - you want an approach which requires even more complicated filter
> >command lines.
> 
> Ha, that characterisation isn't exactly neutral - the derivation in filter
> graphs is removed and replaced with explicit specification of devices.

The comparison in your other reply wasn't realistic. Actually it would
look similar to this:

BEFORE

ffmpeg -hwaccel qsv -c:v h264_qsv -i input.mp4 -vf "hwmap=derive_device=vaapi,
format=vaapi,hwmap=derive_device=vulkan,scale_vulkan=w=1920:h=1080" -f null -

AFTER

ffmpeg -init_hw_device vaapi=vadev -init_hw_device qsv=qsvdev@vadev 
-init_hw_device vulkan=vkdev@vadev -hwaccel qsv -c:v h264_qsv -i input.mp4 
-vf "hwmap=hw_device=vadev,format=vaapi,hwmap=hw_device=vkdev,scale_vulkan=w=
1920:h=1080" -f null -


This is more like a ** to all those who are writing command lines by 
hand and surely not an improvement.
I have not been involved in the cli design, but from looking at the result
it's clear that there has always been a focus on allowing things to be 
done as easily as possible instead of needing to do extensive reading 
until you get it right (or give up).


> Perhaps you even want to add to the av_hwdevice_ctx_create_derived() API so
> it can take a YKNOW_ACTUALLY_DONT flag to indicate you don't want to create a
> new derived device, I don't know.

When you mean to add av_hwdevice_ctx_create_derived2() or 
av_hwdevice_ctx_create_derived_ex() which takes an additional parameter to
control this and which the ffmpeg-tool will use instead, that would be fine 
I think.
It would allow to make the change only for the cl tool without affecting
API consumers (regardless of whether such cases may exist or not).

softworkz


___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH V2] libavcodec/qsvenc: Add DisableDeblockingIdc support to qsv

2022-01-11 Thread Wenbin Chen
Add dblk_idc option to 264_qsv and hevc_qsv. Turining on this opion can
disable deblocking.

Signed-off-by: Wenbin Chen 
---
 doc/encoders.texi   | 6 ++
 libavcodec/qsvenc.c | 8 
 libavcodec/qsvenc.h | 3 +++
 3 files changed, 17 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 68921fbd40..6fc94daa11 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3299,6 +3299,9 @@ This flag controls changing of frame type from B to P.
 @item @var{b_strategy}
 This option controls usage of B frames as reference.
 
+@item @var{dblk_idc}
+This option disable deblocking. It has value in range 0~2.
+
 @item @var{cavlc}
 If set, CAVLC is used; if unset, CABAC is used for encoding.
 
@@ -3391,6 +3394,9 @@ Enable rate distortion optimization.
 @item @var{max_frame_size}
 Maximum encoded frame size in bytes.
 
+@item @var{dblk_idc}
+This option disable deblocking. It has value in range 0~2.
+
 @item @var{idr_interval}
 Distance (in I-frames) between IDR frames.
 @table @samp
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index a13718652e..3a51d00ca9 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -346,6 +346,10 @@ static void dump_video_param(AVCodecContext *avctx, 
QSVEncContext *q,
 av_log(avctx, AV_LOG_VERBOSE, "FrameRateExtD: %"PRIu32"; FrameRateExtN: 
%"PRIu32" \n",
info->FrameInfo.FrameRateExtD, info->FrameInfo.FrameRateExtN);
 
+#if QSV_HAVE_DISABLEDEBLOCKIDC
+av_log(avctx, AV_LOG_VERBOSE, "DisableDeblockingIdc: %"PRIu32" \n", 
co2->DisableDeblockingIdc);
+#endif
+
 }
 
 static void dump_video_vp9_param(AVCodecContext *avctx, QSVEncContext *q,
@@ -885,6 +889,10 @@ static int init_video_param(AVCodecContext *avctx, 
QSVEncContext *q)
 q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : 
MFX_CODINGOPTION_OFF;
 if (q->max_frame_size >= 0)
 q->extco2.MaxFrameSize = q->max_frame_size;
+#if QSV_HAVE_DISABLEDEBLOCKIDC
+if(q->dblk_idc >= 0)
+q->extco2.DisableDeblockingIdc = q->dblk_idc;
+#endif
 
 #if QSV_VERSION_ATLEAST(1, 9)
 if (avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > 
avctx->qmax) {
diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
index 31516b8e55..aa49b35f07 100644
--- a/libavcodec/qsvenc.h
+++ b/libavcodec/qsvenc.h
@@ -44,6 +44,7 @@
 
 #define QSV_HAVE_TRELLIS QSV_VERSION_ATLEAST(1, 8)
 #define QSV_HAVE_MAX_SLICE_SIZE QSV_VERSION_ATLEAST(1, 9)
+#define QSV_HAVE_DISABLEDEBLOCKIDC QSV_VERSION_ATLEAST(1, 9)
 #define QSV_HAVE_BREF_TYPE  QSV_VERSION_ATLEAST(1, 8)
 
 #define QSV_HAVE_LA QSV_VERSION_ATLEAST(1, 7)
@@ -97,6 +98,7 @@
 { "b_strategy", "Strategy to choose between I/P/B-frames", 
OFFSET(qsv.b_strategy),AV_OPT_TYPE_INT, { .i64 = -1 }, -1,  1, VE 
}, \
 { "forced_idr", "Forcing I frames as IDR frames", 
OFFSET(qsv.forced_idr), AV_OPT_TYPE_BOOL,{ .i64 = 0  },  0,  1, VE 
}, \
 { "low_power", "enable low power mode(experimental: many limitations by mfx 
version, BRC modes, etc.)", OFFSET(qsv.low_power), AV_OPT_TYPE_BOOL, { .i64 = 
-1}, -1, 1, VE},\
+{ "dblk_idc", "This option disable deblocking. It has value in range 0~2.",   
OFFSET(qsv.dblk_idc),   AV_OPT_TYPE_INT,{ .i64 = -1 },   -1,  2,  VE},\
 
 extern const AVCodecHWConfigInternal *const ff_qsv_enc_hw_configs[];
 
@@ -169,6 +171,7 @@ typedef struct QSVEncContext {
 int rdo;
 int max_frame_size;
 int max_slice_size;
+int dblk_idc;
 
 int tile_cols;
 int tile_rows;
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH V2] libavcodec/qsvenc: Add low latency P-pyramid support to qsv

2022-01-11 Thread Wenbin Chen
Add low latency P-pyramid support to qsv. This feature relates to
command line option "-p_strategy". To enable this flag, user also
need to set "-bf" to 0. P-strategy has two modes "1-simple" and
"2-pyramid". The details of the two models refer to
https://github.com/Intel-Media-SDK/MediaSDK/blob/master/doc/mediasdk-man.md#preftype

Signed-off-by: Wenbin Chen 
---
 doc/encoders.texi   |  6 ++
 libavcodec/qsvenc.c | 36 
 libavcodec/qsvenc.h |  3 +++
 3 files changed, 45 insertions(+)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 6fc94daa11..3d7c944fba 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3296,6 +3296,9 @@ to allow changing of frame type from P and B to I.
 @item @var{adaptive_b}
 This flag controls changing of frame type from B to P.
 
+@item @var{p_strategy}
+Enable P-pyramid: 0-default 1-simple 2-pyramid(bf need to be set to 0).
+
 @item @var{b_strategy}
 This option controls usage of B frames as reference.
 
@@ -3394,6 +3397,9 @@ Enable rate distortion optimization.
 @item @var{max_frame_size}
 Maximum encoded frame size in bytes.
 
+@item @var{p_strategy}
+Enable P-pyramid: 0-default 1-simple 2-pyramid(bf need to be set to 0).
+
 @item @var{dblk_idc}
 This option disable deblocking. It has value in range 0~2.
 
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 3a51d00ca9..5f405c3d3a 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -315,6 +315,14 @@ static void dump_video_param(AVCodecContext *avctx, 
QSVEncContext *q,
 case MFX_B_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "pyramid");   break;
 default:av_log(avctx, AV_LOG_VERBOSE, "auto");  break;
 }
+
+av_log(avctx, AV_LOG_VERBOSE, "; PRefType: ");
+switch (co3->PRefType) {
+case MFX_P_REF_DEFAULT: av_log(avctx, AV_LOG_VERBOSE, "default");   break;
+case MFX_P_REF_SIMPLE:  av_log(avctx, AV_LOG_VERBOSE, "simple");break;
+case MFX_P_REF_PYRAMID: av_log(avctx, AV_LOG_VERBOSE, "pyramid");   break;
+default:av_log(avctx, AV_LOG_VERBOSE, "unknown");   break;
+}
 av_log(avctx, AV_LOG_VERBOSE, "\n");
 #endif
 
@@ -930,6 +938,34 @@ static int init_video_param(AVCodecContext *avctx, 
QSVEncContext *q)
 #if QSV_HAVE_CO3
 q->extco3.Header.BufferId  = MFX_EXTBUFF_CODING_OPTION3;
 q->extco3.Header.BufferSz  = sizeof(q->extco3);
+
+if (avctx->codec_id == AV_CODEC_ID_HEVC ||
+avctx->codec_id == AV_CODEC_ID_H264) {
+#if QSV_HAVE_PREF
+switch (q->p_strategy) {
+case 0:
+q->extco3.PRefType = MFX_P_REF_DEFAULT;
+break;
+case 1:
+q->extco3.PRefType = MFX_P_REF_SIMPLE;
+break;
+case 2:
+q->extco3.PRefType = MFX_P_REF_PYRAMID;
+break;
+default:
+q->extco3.PRefType = MFX_P_REF_DEFAULT;
+av_log(avctx, AV_LOG_WARNING,
+   "invalid p_strategy, set to default\n");
+break;
+}
+if (q->extco3.PRefType == MFX_P_REF_PYRAMID &&
+avctx->max_b_frames != 0) {
+av_log(avctx, AV_LOG_WARNING,
+   "Please set max_b_frames(-bf) to 0 to enable 
P-pyramid\n");
+}
+#endif
+}
+
 #if QSV_HAVE_GPB
 if (avctx->codec_id == AV_CODEC_ID_HEVC)
 q->extco3.GPB  = q->gpb ? MFX_CODINGOPTION_ON : 
MFX_CODINGOPTION_OFF;
diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
index aa49b35f07..960604cb9a 100644
--- a/libavcodec/qsvenc.h
+++ b/libavcodec/qsvenc.h
@@ -51,6 +51,7 @@
 #define QSV_HAVE_LA_DS  QSV_VERSION_ATLEAST(1, 8)
 #define QSV_HAVE_LA_HRD QSV_VERSION_ATLEAST(1, 11)
 #define QSV_HAVE_VDENC  QSV_VERSION_ATLEAST(1, 15)
+#define QSV_HAVE_PREF   QSV_VERSION_ATLEAST(1, 16)
 
 #define QSV_HAVE_GPBQSV_VERSION_ATLEAST(1, 18)
 
@@ -95,6 +96,7 @@
 { "extbrc", "Extended bitrate control",   
OFFSET(qsv.extbrc), AV_OPT_TYPE_INT, { .i64 = -1 }, -1,  1, VE 
}, \
 { "adaptive_i", "Adaptive I-frame placement", 
OFFSET(qsv.adaptive_i), AV_OPT_TYPE_INT, { .i64 = -1 }, -1,  1, VE 
}, \
 { "adaptive_b", "Adaptive B-frame placement", 
OFFSET(qsv.adaptive_b), AV_OPT_TYPE_INT, { .i64 = -1 }, -1,  1, VE 
}, \
+{ "p_strategy", "Enable P-pyramid: 0-default 1-simple 2-pyramid(bf need to 
be set to 0).",OFFSET(qsv.p_strategy), AV_OPT_TYPE_INT,{ .i64 = 0}, 0,  
  2, VE }, \
 { "b_strategy", "Strategy to choose between I/P/B-frames", 
OFFSET(qsv.b_strategy),AV_OPT_TYPE_INT, { .i64 = -1 }, -1,  1, VE 
}, \
 { "forced_idr", "Forcing I frames as IDR frames", 
OFFSET(qsv.forced_idr), AV_OPT_TYPE_BOOL,{ .i64 = 0

[FFmpeg-devel] [PATCH V2] libavcodec/qsvenc: Add max_frame_size support to hevc_qsv

2022-01-11 Thread Wenbin Chen
Add max_frame_size support to hevc_qsv as well.

Signed-off-by: Wenbin Chen 
---
 doc/encoders.texi   | 3 +++
 libavcodec/qsvenc.c | 4 ++--
 2 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 7cc8be1209..68921fbd40 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3388,6 +3388,9 @@ intra refresh cycle.
 @item @var{rdo}
 Enable rate distortion optimization.
 
+@item @var{max_frame_size}
+Maximum encoded frame size in bytes.
+
 @item @var{idr_interval}
 Distance (in I-frames) between IDR frames.
 @table @samp
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 602436da63..a13718652e 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -855,8 +855,6 @@ static int init_video_param(AVCodecContext *avctx, 
QSVEncContext *q)
 if (q->mbbrc >= 0)
 q->extco2.MBBRC = q->mbbrc ? MFX_CODINGOPTION_ON : 
MFX_CODINGOPTION_OFF;
 
-if (q->max_frame_size >= 0)
-q->extco2.MaxFrameSize = q->max_frame_size;
 #if QSV_HAVE_MAX_SLICE_SIZE
 if (q->max_slice_size >= 0)
 q->extco2.MaxSliceSize = q->max_slice_size;
@@ -885,6 +883,8 @@ static int init_video_param(AVCodecContext *avctx, 
QSVEncContext *q)
 if (avctx->codec_id == AV_CODEC_ID_H264 || avctx->codec_id == 
AV_CODEC_ID_HEVC) {
 if (q->extbrc >= 0)
 q->extco2.ExtBRC = q->extbrc ? MFX_CODINGOPTION_ON : 
MFX_CODINGOPTION_OFF;
+if (q->max_frame_size >= 0)
+q->extco2.MaxFrameSize = q->max_frame_size;
 
 #if QSV_VERSION_ATLEAST(1, 9)
 if (avctx->qmin >= 0 && avctx->qmax >= 0 && avctx->qmin > 
avctx->qmax) {
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


[FFmpeg-devel] [PATCH V2] libavcodec/qsvenc: Add transform skip to hevc_qsv

2022-01-11 Thread Wenbin Chen
Add transform_skip option to hevc_qsv. By enabling this option,
the transform_skip_enabled_flag in PPS will be set to 1.
This option is only supported in the platform newer than ICL.

Signed-off-by: Wenbin Chen 
---
 doc/encoders.texi|  3 +++
 libavcodec/qsvenc.c  | 10 +-
 libavcodec/qsvenc.h  |  1 +
 libavcodec/qsvenc_hevc.c |  3 +++
 4 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/doc/encoders.texi b/doc/encoders.texi
index 3d7c944fba..96e088f6ac 100644
--- a/doc/encoders.texi
+++ b/doc/encoders.texi
@@ -3447,6 +3447,9 @@ Number of rows for tiled encoding.
 
 @item @var{aud}
 Insert the Access Unit Delimiter NAL.
+
+@item @var{transform_skip}
+Turn this option ON to enable transformskip.
 @end table
 
 @subsection MPEG2 Options
diff --git a/libavcodec/qsvenc.c b/libavcodec/qsvenc.c
index 5f405c3d3a..2d0e13c91a 100644
--- a/libavcodec/qsvenc.c
+++ b/libavcodec/qsvenc.c
@@ -358,6 +358,9 @@ static void dump_video_param(AVCodecContext *avctx, 
QSVEncContext *q,
 av_log(avctx, AV_LOG_VERBOSE, "DisableDeblockingIdc: %"PRIu32" \n", 
co2->DisableDeblockingIdc);
 #endif
 
+#if QSV_VERSION_ATLEAST(1, 26)
+av_log(avctx, AV_LOG_VERBOSE, "TransformSkip: %s \n", 
print_threestate(co3->TransformSkip));
+#endif
 }
 
 static void dump_video_vp9_param(AVCodecContext *avctx, QSVEncContext *q,
@@ -966,10 +969,15 @@ static int init_video_param(AVCodecContext *avctx, 
QSVEncContext *q)
 #endif
 }
 
+if (avctx->codec_id == AV_CODEC_ID_HEVC) {
+#if QSV_VERSION_ATLEAST(1, 26)
+q->extco3.TransformSkip = q->transform_skip ? MFX_CODINGOPTION_ON :
+  MFX_CODINGOPTION_OFF;
+#endif
 #if QSV_HAVE_GPB
-if (avctx->codec_id == AV_CODEC_ID_HEVC)
 q->extco3.GPB  = q->gpb ? MFX_CODINGOPTION_ON : 
MFX_CODINGOPTION_OFF;
 #endif
+}
 q->extparam_internal[q->nb_extparam_internal++] = (mfxExtBuffer 
*)&q->extco3;
 #endif
 }
diff --git a/libavcodec/qsvenc.h b/libavcodec/qsvenc.h
index 960604cb9a..5a574ada30 100644
--- a/libavcodec/qsvenc.h
+++ b/libavcodec/qsvenc.h
@@ -200,6 +200,7 @@ typedef struct QSVEncContext {
 int repeat_pps;
 int low_power;
 int gpb;
+int transform_skip;
 
 int a53_cc;
 
diff --git a/libavcodec/qsvenc_hevc.c b/libavcodec/qsvenc_hevc.c
index 08aba3011d..679c09c858 100644
--- a/libavcodec/qsvenc_hevc.c
+++ b/libavcodec/qsvenc_hevc.c
@@ -251,6 +251,9 @@ static const AVOption options[] = {
 { "tile_rows",  "Number of rows for tiled encoding",  
OFFSET(qsv.tile_rows),AV_OPT_TYPE_INT, { .i64 = 0 }, 0, UINT16_MAX, VE },
 { "recovery_point_sei", "Insert recovery point SEI messages",   
OFFSET(qsv.recovery_point_sei),  AV_OPT_TYPE_INT, { .i64 = -1 },
   -1,  1, VE },
 { "aud", "Insert the Access Unit Delimiter NAL", OFFSET(qsv.aud), 
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1, VE},
+#if QSV_VERSION_ATLEAST(1, 26)
+{ "transform_skip", "Turn this option ON to enable transformskip",   
OFFSET(qsv.transform_skip),  AV_OPT_TYPE_INT,{ .i64 = -1},   -1, 1, 
 VE},
+#endif
 
 { NULL },
 };
-- 
2.25.1

___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".


Re: [FFmpeg-devel] 5.0 blocking issues

2022-01-11 Thread Lynne
8 Jan 2022, 17:30 by mich...@niedermayer.cc:

> Hi all
>
> This is a simple go/no go call
> if you know of something that still should go into 5.0 please reply here
> with a list of what you are working on and a timelimit until when you
> will be done with it
>
> if you think everything is ready for the release, then too feel free to
> reply (assuming few others said ok yet) this is not supposed to become a
> 100 reply thread with oks, just maybe 2-3 people confirming that noone
> is aware of things missing.
>
> I intend to do the release within 2-3 days of all "no go" things being
> resolved or timeouting
>
> thx
>

Both of elenril and mkver's patches got merged, isn't it
time to tag and make archives already?
___
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

To unsubscribe, visit link above, or email
ffmpeg-devel-requ...@ffmpeg.org with subject "unsubscribe".