Hello Michael,
On 06/29/2016 04:53 AM, Michael Niedermayer wrote:
On Tue, Jun 28, 2016 at 01:33:13PM +0200, Nicolas George wrote:
Le primidi 11 messidor, an CCXXIV, Nicolas George a écrit :
Well, looking at the code, I am thinking that the current design is flawed:
the extra alloc in ff_bsf_get_packet() seems completely useless, and could
be removed as is without any other change in the current code, because all
current filters are 1:1, it would be different for future 1:N filters. Maybe
implementing ff_bsf_peek_packet() and using it to replace
ff_bsf_get_packet() in 1:1 cases would do the trick.
I have checked that the following quick-and-dirty changes pass FATE. Making
it in shape (testing filters not covered by FATE, making future-proof) would
take time that I would like to spend on lavfi right now. But it proves the
overhead can be reduced easily.
applying only the attached patch causes
./ffmpeg -i ~/videos/matrixbench_mpeg2.mpg -vbsf noise file.avi
to
...
Segmentation fault
valgrind is also unhappy
I think Nicolas has not meant the patch to be really used - he suggested
to create separate ff_bsf_peek_packet function which would just pass
pointer to packet from internal bsf structure and do not allocate new
packet structure each time the pointer to existing one is passed. In
this case caller must not free the packet - just dereference it.
Try attached patch(es) - valgrind seems quite happy.
Regards,
Jan
>From 05755a7f36fd8d0b135288d47dd51e11f93ca488 Mon Sep 17 00:00:00 2001
From: Jan Sebechlebsky <sebechlebsky...@gmail.com>
Date: Wed, 29 Jun 2016 19:41:37 +0200
Subject: [PATCH 1/2] avcodec/bsf: Add ff_bsf_peek_packet function
This functions allows to reuse packet in internal bsf
structure and saves extra allocation.
Signed-off-by: Jan Sebechlebsky <sebechlebsky...@gmail.com>
---
libavcodec/bsf.c | 16 ++++++++++++++++
libavcodec/bsf.h | 8 ++++++++
2 files changed, 24 insertions(+)
diff --git a/libavcodec/bsf.c b/libavcodec/bsf.c
index 88b7f29..1c2e5b6 100644
--- a/libavcodec/bsf.c
+++ b/libavcodec/bsf.c
@@ -217,3 +217,19 @@ int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt)
return 0;
}
+
+int ff_bsf_peek_packet(AVBSFContext *ctx, AVPacket **pkt)
+{
+ AVBSFInternal *in = ctx->internal;
+
+ if (in->eof)
+ return AVERROR_EOF;
+
+ if (!ctx->internal->buffer_pkt->data &&
+ !ctx->internal->buffer_pkt->side_data_elems)
+ return AVERROR(EAGAIN);
+
+ *pkt = ctx->internal->buffer_pkt;
+
+ return 0;
+}
diff --git a/libavcodec/bsf.h b/libavcodec/bsf.h
index 3435df5..ecad3a6 100644
--- a/libavcodec/bsf.h
+++ b/libavcodec/bsf.h
@@ -28,6 +28,14 @@
*/
int ff_bsf_get_packet(AVBSFContext *ctx, AVPacket **pkt);
+/**
+ * Called by bitstream filters to get packet for filtering.
+ * This function should be preferred in 1:1 bitstream filters.
+ * Caller must not free the packet, but is responsible for
+ * dereferencing it.
+ */
+int ff_bsf_peek_packet(AVBSFContext *ctx, AVPacket **pkt);
+
const AVClass *ff_bsf_child_class_next(const AVClass *prev);
#endif /* AVCODEC_BSF_H */
--
1.9.1
>From 3f917440555aad67d3c213e94499812f225c79d1 Mon Sep 17 00:00:00 2001
From: Jan Sebechlebsky <sebechlebsky...@gmail.com>
Date: Wed, 29 Jun 2016 19:46:43 +0200
Subject: [PATCH 2/2] avcodec/noise_bsf: Use ff_bsf_peek_packet function
Use of ff_bsf_peek_packet saves one malloc operation
compared to ff_bsf_get_packet.
Signed-off-by: Jan Sebechlebsky <sebechlebsky...@gmail.com>
---
libavcodec/noise_bsf.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/libavcodec/noise_bsf.c b/libavcodec/noise_bsf.c
index 0aebee1..9c21f7b 100644
--- a/libavcodec/noise_bsf.c
+++ b/libavcodec/noise_bsf.c
@@ -44,7 +44,7 @@ static int noise(AVBSFContext *ctx, AVPacket *out)
if (amount <= 0)
return AVERROR(EINVAL);
- ret = ff_bsf_get_packet(ctx, &in);
+ ret = ff_bsf_peek_packet(ctx, &in);
if (ret < 0)
return ret;
@@ -66,7 +66,7 @@ static int noise(AVBSFContext *ctx, AVPacket *out)
fail:
if (ret < 0)
av_packet_unref(out);
- av_packet_free(&in);
+ av_packet_unref(in);
return ret;
}
--
1.9.1
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
http://ffmpeg.org/mailman/listinfo/ffmpeg-devel