Example of use:

ffmpeg -f mpegts -i INPUT.ts -map i:101 -c:v copy -map i:102 -bsf:a 
delayer=offset=3600 -c:a copy -f mpegts OUTPUT.ts

You can use it with VIDEO, AUDIO or DATA streams.

Regards.
A.H.

---
From 414a3c78dfa208b8253448d5c09fde9a8bee56cb Mon Sep 17 00:00:00 2001
From: Andreas Hakon <andreas.ha...@protonmail.com>
Date: Tue, 25 Jun 2019 20:03:48 +0100
Subject: [PATCH] avcodec: add delayer bitstream filter

This implements a new delayer bitstream filter.

Signed-off-by: Andreas Hakon <andreas.ha...@protonmail.com>
---
 doc/bitstream_filters.texi     |    4 ++
 libavcodec/Makefile            |    1 +
 libavcodec/bitstream_filters.c |    1 +
 libavcodec/delayer_bsf.c       |   81 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 87 insertions(+)
 create mode 100644 libavcodec/delayer_bsf.c

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 40e8ada..563dfa9 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -96,6 +96,10 @@ Deletes Padding OBUs.
 
 Remove zero padding at the end of a packet.
 
+@section delayer
+
+Apply an offset to the PTS/DTS timestamps.
+
 @section dca_core
 
 Extract the core from a DCA/DTS stream, dropping extensions such as
diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index edccd73..e723e62 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1071,6 +1071,7 @@ OBJS-$(CONFIG_AAC_ADTSTOASC_BSF)          += 
aac_adtstoasc_bsf.o mpeg4audio.o
 OBJS-$(CONFIG_AV1_METADATA_BSF)           += av1_metadata_bsf.o
 OBJS-$(CONFIG_AV1_FRAME_SPLIT_BSF)        += av1_frame_split_bsf.o
 OBJS-$(CONFIG_CHOMP_BSF)                  += chomp_bsf.o
+OBJS-$(CONFIG_DELAYER_BSF)                += delayer_bsf.o
 OBJS-$(CONFIG_DUMP_EXTRADATA_BSF)         += dump_extradata_bsf.o
 OBJS-$(CONFIG_DCA_CORE_BSF)               += dca_core_bsf.o
 OBJS-$(CONFIG_EAC3_CORE_BSF)              += eac3_core_bsf.o
diff --git a/libavcodec/bitstream_filters.c b/libavcodec/bitstream_filters.c
index 4630039..a3c509a 100644
--- a/libavcodec/bitstream_filters.c
+++ b/libavcodec/bitstream_filters.c
@@ -28,6 +28,7 @@ extern const AVBitStreamFilter ff_aac_adtstoasc_bsf;
 extern const AVBitStreamFilter ff_av1_frame_split_bsf;
 extern const AVBitStreamFilter ff_av1_metadata_bsf;
 extern const AVBitStreamFilter ff_chomp_bsf;
+extern const AVBitStreamFilter ff_delayer_bsf;
 extern const AVBitStreamFilter ff_dump_extradata_bsf;
 extern const AVBitStreamFilter ff_dca_core_bsf;
 extern const AVBitStreamFilter ff_eac3_core_bsf;
diff --git a/libavcodec/delayer_bsf.c b/libavcodec/delayer_bsf.c
new file mode 100644
index 0000000..81ef625
--- /dev/null
+++ b/libavcodec/delayer_bsf.c
@@ -0,0 +1,81 @@
+/*
+ * 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
+ */
+
+/**
+ * @file
+ * This bitstream filter applies an offset to the PTS/DTS timestamps.
+ *
+ */
+
+// TODO: Control time wrapping
+// TODO: Instead of using absolute timestamp offsets, use frame numbers
+// TODO: Advance in time? (aka negative offset)
+
+#include "avcodec.h"
+#include "bsf.h"
+
+#include "libavutil/opt.h"
+
+typedef struct DelayerContext {
+    const AVClass *class;
+    int offset;
+} DelayerContext;
+
+static int delayer_filter(AVBSFContext *ctx, AVPacket *pkt)
+{
+    int ret;
+    DelayerContext *s = ctx->priv_data;
+    int64_t opts,odts;
+
+    ret = ff_bsf_get_packet_ref(ctx, pkt);
+    if (ret < 0)
+        return ret;
+
+    opts = pkt->pts;
+    if (pkt->pts != AV_NOPTS_VALUE) {
+        pkt->pts += s->offset;
+    }
+    odts = pkt->dts;
+    if (pkt->dts != AV_NOPTS_VALUE) {
+        pkt->dts += s->offset;
+    }
+    av_log(ctx, AV_LOG_DEBUG, "Updated PTS/DTS (%"PRId64"/%"PRId64" : 
%"PRId64"/%"PRId64") with offset:%d\n", pkt->pts,pkt->dts, opts,odts, s->offset 
);
+
+    return 0;
+}
+
+#define OFFSET(x) offsetof(DelayerContext, x)
+#define FLAGS 
(AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_AUDIO_PARAM|AV_OPT_FLAG_SUBTITLE_PARAM|AV_OPT_FLAG_BSF_PARAM)
+static const AVOption options[] = {
+    { "offset", NULL, OFFSET(offset), AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 
INT_MAX, FLAGS },
+    { NULL },
+};
+
+static const AVClass delayer_class = {
+    .class_name = "delayer",
+    .item_name  = av_default_item_name,
+    .option     = options,
+    .version    = LIBAVUTIL_VERSION_INT,
+};
+
+const AVBitStreamFilter ff_delayer_bsf = {
+    .name   = "delayer",
+    .priv_data_size = sizeof(DelayerContext),
+    .priv_class     = &delayer_class,
+    .filter = delayer_filter,
+};
-- 
1.7.10.4

_______________________________________________
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".

Reply via email to