diff mbox series

[FFmpeg-devel,1/2] avcodec/bsf: add datamosh bitstream filter

Message ID 20240506053219.216068-1-marcus@marcusspencer.xyz
State New
Headers show
Series [FFmpeg-devel,1/2] avcodec/bsf: add datamosh bitstream filter | expand

Checks

Context Check Description
andriy/make_x86 fail Make failed

Commit Message

Marcus B Spencer May 6, 2024, 5:32 a.m. UTC
Has an option called "target".

For a positive target, drop the nth keyframe.
For a nonpositive target, allow the first n keyframe(s) and drop the remainder.

Its primary purpose is for datamoshing, but it can also be used for dropping parts of audio.

Signed-off-by: Marcus B Spencer <marcus@marcusspencer.xyz>
---
 libavcodec/bitstream_filters.c |  1 +
 libavcodec/bsf/Makefile        |  1 +
 libavcodec/bsf/datamosh.c      | 88 ++++++++++++++++++++++++++++++++++
 3 files changed, 90 insertions(+)
 create mode 100644 libavcodec/bsf/datamosh.c

Comments

Andreas Rheinhardt May 6, 2024, 5:47 a.m. UTC | #1
Marcus B Spencer:
> Has an option called "target".
> 
> For a positive target, drop the nth keyframe.
> For a nonpositive target, allow the first n keyframe(s) and drop the remainder.
> 
> Its primary purpose is for datamoshing, but it can also be used for dropping parts of audio.
> 
> Signed-off-by: Marcus B Spencer <marcus@marcusspencer.xyz>
> ---
>  libavcodec/bitstream_filters.c |  1 +
>  libavcodec/bsf/Makefile        |  1 +
>  libavcodec/bsf/datamosh.c      | 88 ++++++++++++++++++++++++++++++++++
>  3 files changed, 90 insertions(+)
>  create mode 100644 libavcodec/bsf/datamosh.c
> 
> diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
> index 12860c332b..cde6090c77 100644
> --- a/libavcodec/bitstream_filters.c
> +++ b/libavcodec/bitstream_filters.c
> @@ -29,6 +29,7 @@ extern const FFBitStreamFilter ff_av1_frame_merge_bsf;
>  extern const FFBitStreamFilter ff_av1_frame_split_bsf;
>  extern const FFBitStreamFilter ff_av1_metadata_bsf;
>  extern const FFBitStreamFilter ff_chomp_bsf;
> +extern const FFBitStreamFilter ff_datamosh_bsf;
>  extern const FFBitStreamFilter ff_dump_extradata_bsf;
>  extern const FFBitStreamFilter ff_dca_core_bsf;
>  extern const FFBitStreamFilter ff_dts2pts_bsf;
> diff --git a/libavcodec/bsf/Makefile b/libavcodec/bsf/Makefile
> index fb70ad0c21..ad3b870d12 100644
> --- a/libavcodec/bsf/Makefile
> +++ b/libavcodec/bsf/Makefile
> @@ -6,6 +6,7 @@ OBJS-$(CONFIG_AV1_FRAME_MERGE_BSF)        += bsf/av1_frame_merge.o
>  OBJS-$(CONFIG_AV1_FRAME_SPLIT_BSF)        += bsf/av1_frame_split.o
>  OBJS-$(CONFIG_AV1_METADATA_BSF)           += bsf/av1_metadata.o
>  OBJS-$(CONFIG_CHOMP_BSF)                  += bsf/chomp.o
> +OBJS-$(CONFIG_DATAMOSH_BSF)               += bsf/datamosh.o
>  OBJS-$(CONFIG_DCA_CORE_BSF)               += bsf/dca_core.o
>  OBJS-$(CONFIG_DTS2PTS_BSF)                += bsf/dts2pts.o
>  OBJS-$(CONFIG_DUMP_EXTRADATA_BSF)         += bsf/dump_extradata.o
> diff --git a/libavcodec/bsf/datamosh.c b/libavcodec/bsf/datamosh.c
> new file mode 100644
> index 0000000000..8314a4efc8
> --- /dev/null
> +++ b/libavcodec/bsf/datamosh.c
> @@ -0,0 +1,88 @@
> +/*
> + * Copyright (c) 2024 Marcus B Spencer <marcus@marcusspencer.xyz>
> + *
> + * This file is part of FFmpeg.
> + *
> + * FFmpeg is free software; you can redistribute it and/or
> + * modify it under the terms of the GNU Lesser General Public
> + * License as published by the Free Software Foundation; either
> + * version 2.1 of the License, or (at your option) any later version.
> + *
> + * FFmpeg is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
> + * Lesser General Public License for more details.
> + *
> + * You should have received a copy of the GNU Lesser General Public
> + * License along with FFmpeg; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> + */
> +
> +#include <stdbool.h>
> +
> +#include "bsf.h"
> +#include "bsf_internal.h"
> +
> +#include "libavutil/opt.h"
> +
> +typedef struct {
> +    const AVClass *class;
> +
> +    int target, i;
> +} DatamoshContext;
> +
> +static int datamosh_init(AVBSFContext *ctx)
> +{
> +    DatamoshContext *s = ctx->priv_data;
> +
> +    s->i = 0;
> +
> +    return 0;
> +}
> +
> +static int datamosh(AVBSFContext *ctx, AVPacket *pkt)
> +{
> +    DatamoshContext *s = ctx->priv_data;
> +    bool key;
> +    int ret;
> +
> +    ret = ff_bsf_get_packet_ref(ctx, pkt);
> +    if (ret < 0)
> +        return ret;
> +
> +    key = pkt->flags & AV_PKT_FLAG_KEY;
> +    if (key)
> +        ++s->i;
> +
> +    if (s->target < 1) {
> +        if (s->i <= -s->target)
> +            return 0;
> +        else if (key) {
> +            av_packet_unref(pkt);
> +            return AVERROR(EAGAIN);
> +        }
> +    }
> +
> +    if (key && s->i == s->target) {
> +        av_packet_unref(pkt);
> +        return AVERROR(EAGAIN);
> +    }
> +
> +    return 0;
> +}
> +
> +
> +#define OFFSET(x) offsetof(DatamoshContext, x)
> +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
> +
> +static const AVOption options[] = {
> +    { "target", NULL, OFFSET(target), AV_OPT_TYPE_INT, { .i64 = 0 }, -INT_MAX, INT_MAX, FLAGS },
> +    { NULL },
> +};
> +
> +static const AVClass datamosh_class = {
> +    .class_name = "datamosh",
> +    .item_name = av_default_item_name,
> +    .option = options,
> +    .version = LIBAVUTIL_VERSION_INT,
> +};

1. This won't compile, as you only add the FFBitStreamFilter in the
second patch.
2. Look at the noise bsf.

- Andreas
Marcus B Spencer May 6, 2024, 6:05 a.m. UTC | #2
I realized that my git history change failed which intended to retroactively fix issue #1 when the fix got added in the 2nd patch. Be careful when changing git history.



On Monday, May 6th, 2024 at 12:47 AM, Andreas Rheinhardt <andreas.rheinhardt@outlook.com> wrote:

>
>
> Marcus B Spencer:
>
> > Has an option called "target".
> >
> > For a positive target, drop the nth keyframe.
> > For a nonpositive target, allow the first n keyframe(s) and drop the remainder.
> >
> > Its primary purpose is for datamoshing, but it can also be used for dropping parts of audio.
> >
> > Signed-off-by: Marcus B Spencer marcus@marcusspencer.xyz
> > ---
> > libavcodec/bitstream_filters.c | 1 +
> > libavcodec/bsf/Makefile | 1 +
> > libavcodec/bsf/datamosh.c | 88 ++++++++++++++++++++++++++++++++++
> > 3 files changed, 90 insertions(+)
> > create mode 100644 libavcodec/bsf/datamosh.c
> >
> > diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
> > index 12860c332b..cde6090c77 100644
> > --- a/libavcodec/bitstream_filters.c
> > +++ b/libavcodec/bitstream_filters.c
> > @@ -29,6 +29,7 @@ extern const FFBitStreamFilter ff_av1_frame_merge_bsf;
> > extern const FFBitStreamFilter ff_av1_frame_split_bsf;
> > extern const FFBitStreamFilter ff_av1_metadata_bsf;
> > extern const FFBitStreamFilter ff_chomp_bsf;
> > +extern const FFBitStreamFilter ff_datamosh_bsf;
> > extern const FFBitStreamFilter ff_dump_extradata_bsf;
> > extern const FFBitStreamFilter ff_dca_core_bsf;
> > extern const FFBitStreamFilter ff_dts2pts_bsf;
> > diff --git a/libavcodec/bsf/Makefile b/libavcodec/bsf/Makefile
> > index fb70ad0c21..ad3b870d12 100644
> > --- a/libavcodec/bsf/Makefile
> > +++ b/libavcodec/bsf/Makefile
> > @@ -6,6 +6,7 @@ OBJS-$(CONFIG_AV1_FRAME_MERGE_BSF) += bsf/av1_frame_merge.o
> > OBJS-$(CONFIG_AV1_FRAME_SPLIT_BSF) += bsf/av1_frame_split.o
> > OBJS-$(CONFIG_AV1_METADATA_BSF) += bsf/av1_metadata.o
> > OBJS-$(CONFIG_CHOMP_BSF) += bsf/chomp.o
> > +OBJS-$(CONFIG_DATAMOSH_BSF) += bsf/datamosh.o
> > OBJS-$(CONFIG_DCA_CORE_BSF) += bsf/dca_core.o
> > OBJS-$(CONFIG_DTS2PTS_BSF) += bsf/dts2pts.o
> > OBJS-$(CONFIG_DUMP_EXTRADATA_BSF) += bsf/dump_extradata.o
> > diff --git a/libavcodec/bsf/datamosh.c b/libavcodec/bsf/datamosh.c
> > new file mode 100644
> > index 0000000000..8314a4efc8
> > --- /dev/null
> > +++ b/libavcodec/bsf/datamosh.c
> > @@ -0,0 +1,88 @@
> > +/*
> > + * Copyright (c) 2024 Marcus B Spencer marcus@marcusspencer.xyz
> > + *
> > + * This file is part of FFmpeg.
> > + *
> > + * FFmpeg is free software; you can redistribute it and/or
> > + * modify it under the terms of the GNU Lesser General Public
> > + * License as published by the Free Software Foundation; either
> > + * version 2.1 of the License, or (at your option) any later version.
> > + *
> > + * FFmpeg is distributed in the hope that it will be useful,
> > + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> > + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> > + * Lesser General Public License for more details.
> > + *
> > + * You should have received a copy of the GNU Lesser General Public
> > + * License along with FFmpeg; if not, write to the Free Software
> > + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
> > + */
> > +
> > +#include <stdbool.h>
> > +
> > +#include "bsf.h"
> > +#include "bsf_internal.h"
> > +
> > +#include "libavutil/opt.h"
> > +
> > +typedef struct {
> > + const AVClass *class;
> > +
> > + int target, i;
> > +} DatamoshContext;
> > +
> > +static int datamosh_init(AVBSFContext *ctx)
> > +{
> > + DatamoshContext *s = ctx->priv_data;
> > +
> > + s->i = 0;
> > +
> > + return 0;
> > +}
> > +
> > +static int datamosh(AVBSFContext *ctx, AVPacket *pkt)
> > +{
> > + DatamoshContext *s = ctx->priv_data;
> > + bool key;
> > + int ret;
> > +
> > + ret = ff_bsf_get_packet_ref(ctx, pkt);
> > + if (ret < 0)
> > + return ret;
> > +
> > + key = pkt->flags & AV_PKT_FLAG_KEY;
> > + if (key)
> > + ++s->i;
> > +
> > + if (s->target < 1) {
> > + if (s->i <= -s->target)
> > + return 0;
> > + else if (key) {
> > + av_packet_unref(pkt);
> > + return AVERROR(EAGAIN);
> > + }
> > + }
> > +
> > + if (key && s->i == s->target) {
> > + av_packet_unref(pkt);
> > + return AVERROR(EAGAIN);
> > + }
> > +
> > + return 0;
> > +}
> > +
> > +
> > +#define OFFSET(x) offsetof(DatamoshContext, x)
> > +#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
> > +
> > +static const AVOption options[] = {
> > + { "target", NULL, OFFSET(target), AV_OPT_TYPE_INT, { .i64 = 0 }, -INT_MAX, INT_MAX, FLAGS },
> > + { NULL },
> > +};
> > +
> > +static const AVClass datamosh_class = {
> > + .class_name = "datamosh",
> > + .item_name = av_default_item_name,
> > + .option = options,
> > + .version = LIBAVUTIL_VERSION_INT,
> > +};
>
>
> 1. This won't compile, as you only add the FFBitStreamFilter in the
> second patch.
> 2. Look at the noise bsf.
>
> - 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-request@ffmpeg.org with subject "unsubscribe".
diff mbox series

Patch

diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 12860c332b..cde6090c77 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -29,6 +29,7 @@  extern const FFBitStreamFilter ff_av1_frame_merge_bsf;
 extern const FFBitStreamFilter ff_av1_frame_split_bsf;
 extern const FFBitStreamFilter ff_av1_metadata_bsf;
 extern const FFBitStreamFilter ff_chomp_bsf;
+extern const FFBitStreamFilter ff_datamosh_bsf;
 extern const FFBitStreamFilter ff_dump_extradata_bsf;
 extern const FFBitStreamFilter ff_dca_core_bsf;
 extern const FFBitStreamFilter ff_dts2pts_bsf;
diff --git a/libavcodec/bsf/Makefile b/libavcodec/bsf/Makefile
index fb70ad0c21..ad3b870d12 100644
--- a/libavcodec/bsf/Makefile
+++ b/libavcodec/bsf/Makefile
@@ -6,6 +6,7 @@  OBJS-$(CONFIG_AV1_FRAME_MERGE_BSF)        += bsf/av1_frame_merge.o
 OBJS-$(CONFIG_AV1_FRAME_SPLIT_BSF)        += bsf/av1_frame_split.o
 OBJS-$(CONFIG_AV1_METADATA_BSF)           += bsf/av1_metadata.o
 OBJS-$(CONFIG_CHOMP_BSF)                  += bsf/chomp.o
+OBJS-$(CONFIG_DATAMOSH_BSF)               += bsf/datamosh.o
 OBJS-$(CONFIG_DCA_CORE_BSF)               += bsf/dca_core.o
 OBJS-$(CONFIG_DTS2PTS_BSF)                += bsf/dts2pts.o
 OBJS-$(CONFIG_DUMP_EXTRADATA_BSF)         += bsf/dump_extradata.o
diff --git a/libavcodec/bsf/datamosh.c b/libavcodec/bsf/datamosh.c
new file mode 100644
index 0000000000..8314a4efc8
--- /dev/null
+++ b/libavcodec/bsf/datamosh.c
@@ -0,0 +1,88 @@ 
+/*
+ * Copyright (c) 2024 Marcus B Spencer <marcus@marcusspencer.xyz>
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdbool.h>
+
+#include "bsf.h"
+#include "bsf_internal.h"
+
+#include "libavutil/opt.h"
+
+typedef struct {
+    const AVClass *class;
+
+    int target, i;
+} DatamoshContext;
+
+static int datamosh_init(AVBSFContext *ctx)
+{
+    DatamoshContext *s = ctx->priv_data;
+
+    s->i = 0;
+
+    return 0;
+}
+
+static int datamosh(AVBSFContext *ctx, AVPacket *pkt)
+{
+    DatamoshContext *s = ctx->priv_data;
+    bool key;
+    int ret;
+
+    ret = ff_bsf_get_packet_ref(ctx, pkt);
+    if (ret < 0)
+        return ret;
+
+    key = pkt->flags & AV_PKT_FLAG_KEY;
+    if (key)
+        ++s->i;
+
+    if (s->target < 1) {
+        if (s->i <= -s->target)
+            return 0;
+        else if (key) {
+            av_packet_unref(pkt);
+            return AVERROR(EAGAIN);
+        }
+    }
+
+    if (key && s->i == s->target) {
+        av_packet_unref(pkt);
+        return AVERROR(EAGAIN);
+    }
+
+    return 0;
+}
+
+
+#define OFFSET(x) offsetof(DatamoshContext, x)
+#define FLAGS (AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_BSF_PARAM)
+
+static const AVOption options[] = {
+    { "target", NULL, OFFSET(target), AV_OPT_TYPE_INT, { .i64 = 0 }, -INT_MAX, INT_MAX, FLAGS },
+    { NULL },
+};
+
+static const AVClass datamosh_class = {
+    .class_name = "datamosh",
+    .item_name = av_default_item_name,
+    .option = options,
+    .version = LIBAVUTIL_VERSION_INT,
+};