From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 184884A95D for ; Mon, 6 May 2024 05:32:49 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 1D51B68D587; Mon, 6 May 2024 08:32:47 +0300 (EEST) Received: from mail-40111.protonmail.ch (mail-40111.protonmail.ch [185.70.40.111]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id B44D468D561 for ; Mon, 6 May 2024 08:32:40 +0300 (EEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=marcusspencer.xyz; s=protonmail; t=1714973559; x=1715232759; bh=TAEvkU/Ud+yHfrKkM+pSoWLVASU95ueM8qdvKGxxRgg=; h=From:To:Cc:Subject:Date:Message-ID:From:To:Cc:Date:Subject: Reply-To:Feedback-ID:Message-ID:BIMI-Selector; b=A/NNiVyMt9Z9z3bjk3/nB6glEdg2KX3+mWBqNFRCzlCWzigd4tnwFxuwKQaMzlwje FwJgR/pQSEryy/b0E7HR/rHRdpJlwFrG1fCGxj91/sSPjVPxINLPCjPf01FL3CemN2 lYyOuY4204AAZKcQ6qAnQ2zLe+Lj63qsddZg5BvY76utPIvkD4L5+YtcP5Cg3Nc4+1 06QQLuzFE/tnWA4uwdDeQU1uZ2QOnmonz2bMSclWYRhRABiVttOJJ+qiZaPvOpGtdO KNCB/Jve1S5DPPStPql8mcjPrOrKAVHJR2vmz6E+fmQ3InyQm/yT6YmF5T6w5xmnof ya4mN8kk8m0RA== X-Pm-Submission-Id: 4VXqmj4JGtz3v8g From: Marcus B Spencer To: ffmpeg-devel@ffmpeg.org Date: Mon, 6 May 2024 00:32:18 -0500 Message-ID: <20240506053219.216068-1-marcus@marcusspencer.xyz> X-Mailer: git-send-email 2.45.0 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH 1/2] avcodec/bsf: add datamosh bitstream filter X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marcus B Spencer Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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 --- 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 + * + * 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 + +#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, +}; -- 2.45.0 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".